Skip to content
This repository was archived by the owner on Aug 5, 2025. It is now read-only.

Commit e1799c8

Browse files
Merge pull request #71 from Chainlit/clement/eng-1897-add-runtime-and-code-version-in-metadata-or-new-field
feat: add release param
2 parents 157bb88 + abdacfb commit e1799c8

File tree

2 files changed

+16
-4
lines changed

2 files changed

+16
-4
lines changed

src/index.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,9 @@ export class LiteralClient {
4444
instrumentation: ReturnType<typeof instrumentation>;
4545
store: AsyncLocalStorage<StoredContext> =
4646
new AsyncLocalStorage<StoredContext>();
47+
globalMetadata: {
48+
release?: string;
49+
} = {};
4750

4851
/**
4952
* Initialize a new Literal AI Client.
@@ -52,18 +55,21 @@ export class LiteralClient {
5255
* @param options.apiUrl The URL of the Literal AI API. Defaults to the LITERAL_API_URL env var, or https://cloud.getliteral.ai.
5356
* @param options.environment The environment to use for the Literal AI API.
5457
* @param options.disabled If set to true, no call will be made to the Literal AI API.
58+
* @param options.release The release version of your application. This helps track which release an event came from.
5559
* @returns A new LiteralClient instance.
5660
*/
5761
constructor({
5862
apiKey,
5963
apiUrl,
6064
environment,
61-
disabled
65+
disabled,
66+
release
6267
}: {
6368
apiKey?: string;
6469
apiUrl?: string;
6570
environment?: Environment;
6671
disabled?: boolean;
72+
release?: string;
6773
} = {}) {
6874
if (!apiKey) {
6975
apiKey = process.env.LITERAL_API_KEY;
@@ -76,6 +82,11 @@ export class LiteralClient {
7682
this.api = new API(this, apiKey, apiUrl, environment, disabled);
7783
this.openai = openai(this);
7884
this.instrumentation = instrumentation(this);
85+
const formattedRelease = release?.trim();
86+
87+
if (formattedRelease) {
88+
this.globalMetadata.release = formattedRelease;
89+
}
7990
}
8091

8192
/**
@@ -229,6 +240,7 @@ export class LiteralClient {
229240
return {
230241
wrap: async <T>(cb: () => T) => {
231242
const currentStore = this.store.getStore();
243+
const metadata = { ...this.globalMetadata, ...options?.metadata };
232244

233245
return this.store.run(
234246
{
@@ -237,7 +249,7 @@ export class LiteralClient {
237249
currentStore?.currentExperimentItemRunId ?? null,
238250
currentStep: currentStore?.currentStep ?? null,
239251
rootRun: currentStore?.rootRun ?? null,
240-
metadata: options?.metadata ?? null,
252+
metadata,
241253
tags: options?.tags ?? null,
242254
stepId: options?.stepId ?? null
243255
},

tests/decorate.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,14 @@ if (!url || !apiKey) {
1616
throw new Error('Missing environment variables');
1717
}
1818

19-
const client = new LiteralClient({ apiKey, apiUrl: url });
19+
const client = new LiteralClient({ apiKey, apiUrl: url, release: 'test' });
2020

2121
describe('Decorator', () => {
2222
describe('Manual logging', () => {
2323
it('adds metadata and tags to everything logged inside the wrapper', async () => {
2424
let threadId: Maybe<string>;
2525
let stepId: Maybe<string>;
26-
const metadata = { key: 'value' };
26+
const metadata = { key: 'value', release: 'test' };
2727
const tags = ['tag1', 'tag2'];
2828

2929
await client.decorate({ metadata, tags }).wrap(async () => {

0 commit comments

Comments
 (0)