diff --git a/src/data/types.ts b/src/data/types.ts index d18c4299..215bd082 100644 --- a/src/data/types.ts +++ b/src/data/types.ts @@ -43,9 +43,9 @@ export type PineconeConfiguration = { additionalHeaders?: HTTPHeaders; /** - * Optional integration id that is applied to the User-Agent header with all requests. + * Optional sourceTag that is applied to the User-Agent header with all requests. */ - integrationId?: string; + sourceTag?: string; }; export const RecordIdSchema = Type.String({ minLength: 1 }); diff --git a/src/utils/__tests__/user-agent.test.ts b/src/utils/__tests__/user-agent.test.ts index b167eb03..20aa63ce 100644 --- a/src/utils/__tests__/user-agent.test.ts +++ b/src/utils/__tests__/user-agent.test.ts @@ -1,22 +1,47 @@ import { buildUserAgent } from '../user-agent'; import * as EnvironmentModule from '../environment'; -describe('buildUserAgent', () => { - test('applies Edge Runtime when running in an edge environment', () => { - jest.spyOn(EnvironmentModule, 'isEdge').mockReturnValue(true); - const config = { apiKey: 'test-api-key' }; - const userAgent = buildUserAgent(config); +describe('user-agent', () => { + describe('buildUserAgent', () => { + test('applies Edge Runtime when running in an edge environment', () => { + jest.spyOn(EnvironmentModule, 'isEdge').mockReturnValue(true); + const config = { apiKey: 'test-api-key' }; + const userAgent = buildUserAgent(config); - expect(userAgent).toContain('Edge Runtime'); + expect(userAgent).toContain('Edge Runtime'); + }); + + test('applies source_tag when provided via PineconeConfiguration', () => { + const config = { + apiKey: 'test-api-key', + sourceTag: 'test source tag', + }; + + const userAgent = buildUserAgent(config); + expect(userAgent).toContain('source_tag=test_source_tag'); + }); }); - test('applies integrationId when provided via PineconeConfiguration', () => { - const config = { - apiKey: 'test-api-key', - integrationId: 'test-integration-id', - }; + describe('normalizeSourceTag', () => { + test('normalizes variations of sourceTag', () => { + const config = { + apiKey: 'test-api-key', + sourceTag: 'my source tag!!!', + }; + let userAgent = buildUserAgent(config); + expect(userAgent).toContain('source_tag=my_source_tag'); + + config.sourceTag = 'My Source Tag'; + userAgent = buildUserAgent(config); + expect(userAgent).toContain('source_tag=my_source_tag'); + + config.sourceTag = ' My Source Tag 123 '; + userAgent = buildUserAgent(config); + expect(userAgent).toContain('source_tag=my_source_tag_123'); - const userAgent = buildUserAgent(config); - expect(userAgent).toContain('integrationId=test-integration-id'); + config.sourceTag = ' MY SOURCE TAG 1234 ##### !!!!!!'; + userAgent = buildUserAgent(config); + expect(userAgent).toContain('source_tag=my_source_tag_1234'); + }); }); }); diff --git a/src/utils/user-agent.ts b/src/utils/user-agent.ts index e39b2356..82c08458 100644 --- a/src/utils/user-agent.ts +++ b/src/utils/user-agent.ts @@ -20,9 +20,28 @@ export const buildUserAgent = (config: PineconeConfiguration) => { userAgentParts.push(`node ${process.version}`); } - if (config.integrationId) { - userAgentParts.push(`integrationId=${config.integrationId}`); + if (config.sourceTag) { + userAgentParts.push(`source_tag=${normalizeSourceTag(config.sourceTag)}`); } return userAgentParts.join('; '); }; + +const normalizeSourceTag = (sourceTag: string) => { + if (!sourceTag) { + return; + } + + /** + * normalize sourceTag + * 1. Lowercase + * 2. Limit charset to [a-z0-9_ ] + * 3. Trim left/right spaces + * 4. Condense multiple spaces to one, and replace with underscore + */ + return sourceTag + .toLowerCase() + .replace(/[^a-z0-9_ ]/g, '') + .trim() + .replace(/[ ]+/g, '_'); +};