Skip to content

feat: add release param #71

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ export class LiteralClient {
instrumentation: ReturnType<typeof instrumentation>;
store: AsyncLocalStorage<StoredContext> =
new AsyncLocalStorage<StoredContext>();
globalMetadata: {
release?: string;
} = {};

/**
* Initialize a new Literal AI Client.
Expand All @@ -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;
Expand All @@ -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;
}
}

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

return this.store.run(
{
Expand All @@ -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
},
Expand Down
4 changes: 2 additions & 2 deletions tests/decorate.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string>;
let stepId: Maybe<string>;
const metadata = { key: 'value' };
const metadata = { key: 'value', release: 'test' };
const tags = ['tag1', 'tag2'];

await client.decorate({ metadata, tags }).wrap(async () => {
Expand Down
Loading