Skip to content
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

feat(attachments): create context-aware helper for attachments #44

Merged
merged 6 commits into from
Jul 25, 2024
Merged
Changes from 1 commit
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
Prev Previous commit
fix: review
Dam-Buty committed Jul 25, 2024
commit 26b8687164f28c929cbb700f19e66a5bf2a36738
15 changes: 2 additions & 13 deletions src/api.ts
Original file line number Diff line number Diff line change
@@ -726,19 +726,8 @@ export class API {
params.content = Buffer.from(params.content);
}

let threadFromStore: Thread | null = null;
try {
threadFromStore = this.client.getCurrentThread();
} catch (error) {
// Ignore error thrown if getCurrentThread is called outside of a context
}

let stepFromStore: Step | null = null;
try {
stepFromStore = this.client.getCurrentStep();
} catch (error) {
// Ignore error thrown if getCurrentStep is called outside of a context
}
const threadFromStore = this.client._currentThread();
const stepFromStore = this.client._currentStep();

if (threadFromStore) {
params.threadId = threadFromStore.id;
12 changes: 12 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -49,6 +49,18 @@ export class LiteralClient {
return this.step({ ...data, type: 'run' });
}

_currentThread(): Thread | null {
const store = storage.getStore();

return store?.currentThread || null;
}

_currentStep(): Step | null {
const store = storage.getStore();

return store?.currentStep || null;
}

/**
* Gets the current thread from the context.
* WARNING : this will throw if run outside of a thread context.
20 changes: 3 additions & 17 deletions src/instrumentation/openai.ts
Original file line number Diff line number Diff line change
@@ -13,9 +13,7 @@ import {
IGenerationMessage,
LiteralClient,
Maybe,
Step,
StepConstructor,
Thread
StepConstructor
} from '..';

// Define a generic type for the original function to be wrapped
@@ -310,25 +308,13 @@ const processOpenAIOutput = async (
tags: tags
};

let threadFromStore: Thread | null = null;
try {
threadFromStore = client.getCurrentThread();
} catch (error) {
// Ignore error thrown if getCurrentThread is called outside of a context
}

let stepFromStore: Step | null = null;
try {
stepFromStore = client.getCurrentStep();
} catch (error) {
// Ignore error thrown if getCurrentStep is called outside of a context
}
const threadFromStore = client._currentThread();
const stepFromStore = client._currentStep();

const parent = stepFromStore || threadFromStore;

if ('data' in output) {
// Image Generation

const stepData: StepConstructor = {
name: inputs.model || 'openai',
type: 'llm',
2 changes: 1 addition & 1 deletion tests/attachments.test.ts
Original file line number Diff line number Diff line change
@@ -24,7 +24,7 @@ describe('Attachments', () => {
const arrayBuffer = buffer.buffer;
const blob = new Blob([buffer]);
// We wrap the blob in a blob and simulate the structure of a File
const file = new Blob([blob], { type: 'image/jpeg' });
const file = new Blob([blob], { type: 'image/png' });

it.each([
{ type: 'Stream', content: stream! },