From abdacfbc70a8ecf1cb1d0bc76fa3ad192577d4f1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Sirieix?= Date: Tue, 15 Oct 2024 14:06:58 +0200 Subject: [PATCH] feat: add release param --- src/index.ts | 16 ++++++++++++++-- tests/decorate.test.ts | 4 ++-- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/src/index.ts b/src/index.ts index 8426fe9..fd5eed7 100644 --- a/src/index.ts +++ b/src/index.ts @@ -44,6 +44,9 @@ export class LiteralClient { instrumentation: ReturnType; store: AsyncLocalStorage = new AsyncLocalStorage(); + globalMetadata: { + release?: string; + } = {}; /** * Initialize a new Literal AI Client. @@ -52,18 +55,21 @@ export class LiteralClient { * @param options.apiUrl The URL of the Literal AI API. Defaults to the LITERAL_API_URL env var, or https://cloud.getliteral.ai. * @param options.environment The environment to use for the Literal AI API. * @param options.disabled If set to true, no call will be made to the Literal AI API. + * @param options.release The release version of your application. This helps track which release an event came from. * @returns A new LiteralClient instance. */ constructor({ apiKey, apiUrl, environment, - disabled + disabled, + release }: { apiKey?: string; apiUrl?: string; environment?: Environment; disabled?: boolean; + release?: string; } = {}) { if (!apiKey) { apiKey = process.env.LITERAL_API_KEY; @@ -76,6 +82,11 @@ export class LiteralClient { this.api = new API(this, apiKey, apiUrl, environment, disabled); this.openai = openai(this); this.instrumentation = instrumentation(this); + const formattedRelease = release?.trim(); + + if (formattedRelease) { + this.globalMetadata.release = formattedRelease; + } } /** @@ -229,6 +240,7 @@ export class LiteralClient { return { wrap: async (cb: () => T) => { const currentStore = this.store.getStore(); + const metadata = { ...this.globalMetadata, ...options?.metadata }; return this.store.run( { @@ -237,7 +249,7 @@ export class LiteralClient { currentStore?.currentExperimentItemRunId ?? null, currentStep: currentStore?.currentStep ?? null, rootRun: currentStore?.rootRun ?? null, - metadata: options?.metadata ?? null, + metadata, tags: options?.tags ?? null, stepId: options?.stepId ?? null }, diff --git a/tests/decorate.test.ts b/tests/decorate.test.ts index 46b2820..aa649fb 100644 --- a/tests/decorate.test.ts +++ b/tests/decorate.test.ts @@ -16,14 +16,14 @@ if (!url || !apiKey) { throw new Error('Missing environment variables'); } -const client = new LiteralClient({ apiKey, apiUrl: url }); +const client = new LiteralClient({ apiKey, apiUrl: url, release: 'test' }); describe('Decorator', () => { describe('Manual logging', () => { it('adds metadata and tags to everything logged inside the wrapper', async () => { let threadId: Maybe; let stepId: Maybe; - const metadata = { key: 'value' }; + const metadata = { key: 'value', release: 'test' }; const tags = ['tag1', 'tag2']; await client.decorate({ metadata, tags }).wrap(async () => {