Skip to content

Commit 37de965

Browse files
committed
📦 NEW: Threads support
1 parent 8d6306a commit 37de965

File tree

8 files changed

+224
-46
lines changed

8 files changed

+224
-46
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import 'dotenv/config';
2+
import {Langbase} from 'langbase';
3+
4+
const langbase = new Langbase({
5+
apiKey: process.env.LANGBASE_API_KEY!,
6+
});
7+
8+
async function main() {
9+
const response = await langbase.threads.append({
10+
threadId: 'REPLACE_WITH_THREAD_ID',
11+
messages: [
12+
{
13+
role: 'assistant',
14+
content: 'Nice to meet you',
15+
metadata: {size: 'small'},
16+
},
17+
],
18+
});
19+
20+
console.log(response);
21+
}
22+
23+
main();
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import 'dotenv/config';
2+
import {Langbase} from 'langbase';
3+
4+
const langbase = new Langbase({
5+
apiKey: process.env.LANGBASE_API_KEY!,
6+
});
7+
8+
async function main() {
9+
const response = await langbase.threads.create({
10+
messages: [{role: 'user', content: 'hello', metadata: {size: 'small'}}],
11+
metadata: {company: 'langbase'},
12+
});
13+
14+
console.log(response);
15+
}
16+
17+
main();
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import 'dotenv/config';
2+
import {Langbase} from 'langbase';
3+
4+
const langbase = new Langbase({
5+
apiKey: process.env.LANGBASE_API_KEY!,
6+
});
7+
8+
async function main() {
9+
const response = await langbase.threads.delete({
10+
threadId: 'REPLACE_WITH_THREAD_ID',
11+
});
12+
13+
console.log(response);
14+
}
15+
16+
main();
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import 'dotenv/config';
2+
import {Langbase} from 'langbase';
3+
4+
const langbase = new Langbase({
5+
apiKey: process.env.LANGBASE_API_KEY!,
6+
});
7+
8+
async function main() {
9+
const response = await langbase.threads.get({
10+
threadId: 'REPLACE_WITH_THREAD_ID',
11+
});
12+
13+
console.log(response);
14+
}
15+
16+
main();
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import 'dotenv/config';
2+
import {Langbase} from 'langbase';
3+
4+
const langbase = new Langbase({
5+
apiKey: process.env.LANGBASE_API_KEY!,
6+
});
7+
8+
async function main() {
9+
const response = await langbase.threads.messages.list({
10+
threadId: 'REPLACE_WITH_THREAD_ID',
11+
});
12+
13+
console.log(response);
14+
}
15+
16+
main();
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import 'dotenv/config';
2+
import {Langbase} from 'langbase';
3+
4+
const langbase = new Langbase({
5+
apiKey: process.env.LANGBASE_API_KEY!,
6+
});
7+
8+
async function main() {
9+
const response = await langbase.threads.update({
10+
threadId: 'REPLACE_WITH_THREAD_ID',
11+
metadata: {
12+
company: 'langbase',
13+
about: 'Langbase is the most powerful serverless platform for building AI agents with memory.',
14+
},
15+
});
16+
17+
console.log(response);
18+
}
19+
20+
main();

examples/nodejs/package.json

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,12 @@
3535
"embed": "npx tsx ./examples/embed/index.ts",
3636
"chunk": "npx tsx ./examples/chunk/index.ts",
3737
"parse": "npx tsx ./examples/parse/index.ts",
38-
"thread.messages.add": "npx tsx ./examples/threads/thread.messages.add.ts",
39-
"thread.messages.list": "npx tsx ./examples/threads/thread.messages.list.ts",
40-
"thread.delete": "npx tsx ./examples/threads/thread.delete.ts"
38+
"threads.create": "npx tsx ./examples/threads/threads.create.ts",
39+
"threads.update": "npx tsx ./examples/threads/threads.update.ts",
40+
"threads.delete": "npx tsx ./examples/threads/threads.delete.ts",
41+
"threads.append": "npx tsx ./examples/threads/threads.append.ts",
42+
"threads.messages.list": "npx tsx ./examples/threads/threads.messages.list.ts",
43+
"threads.get": "npx tsx ./examples/threads/threads.get.ts"
4144
},
4245
"keywords": [],
4346
"author": "Ahmad Awais <[email protected]> (https://twitter.com/MrAhmadAwais)",

packages/langbase/src/langbase/langbase.ts

Lines changed: 110 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,11 @@ export interface Message {
9191
tool_calls?: ToolCall[];
9292
}
9393

94+
export interface ThreadMessage extends Message {
95+
attachments?: any[];
96+
metadata?: Record<string, string>;
97+
}
98+
9499
export interface Variable {
95100
name: string;
96101
value: string;
@@ -342,19 +347,47 @@ export type ParseResponse = {
342347
content: string;
343348
};
344349

345-
export interface AddMessageOptions {
350+
export interface ThreadsCreate {
351+
threadId?: string;
352+
metadata?: Record<string, string>;
353+
messages?: ThreadMessage[];
354+
}
355+
356+
export interface ThreadsUpdate {
346357
threadId: string;
347-
messages: Message[];
358+
metadata: Record<string, string>;
348359
}
349360

350-
export interface ListMessagesOptions {
361+
export interface ThreadsGet {
351362
threadId: string;
352363
}
353364

354365
export interface DeleteThreadOptions {
355366
threadId: string;
356367
}
357368

369+
export interface ThreadsBaseResponse {
370+
id: string;
371+
object: 'thread';
372+
created_at: number;
373+
metadata: Record<string, string>;
374+
}
375+
376+
export interface ThreadMessagesCreate {
377+
threadId: string;
378+
messages: ThreadMessage[];
379+
}
380+
381+
export interface ThreadMessagesList {
382+
threadId: string;
383+
}
384+
385+
export interface ThreadMessagesBaseResponse extends ThreadMessage {
386+
id: string;
387+
created_at: number;
388+
thread_id: string;
389+
}
390+
358391
export class Langbase {
359392
private request: Request;
360393
private apiKey: string;
@@ -435,13 +468,20 @@ export class Langbase {
435468
};
436469
};
437470

438-
// public thread: {
439-
// messages: {
440-
// add: (options: AddMessageOptions) => Promise<Message[]>;
441-
// list: (options: ListMessagesOptions) => Promise<Message[]>;
442-
// };
443-
// delete: (options: DeleteThreadOptions) => Promise<boolean>;
444-
// };
471+
public threads: {
472+
create: (options: ThreadsCreate) => Promise<ThreadsBaseResponse>;
473+
update: (options: ThreadsUpdate) => Promise<ThreadsBaseResponse>;
474+
get: (options: ThreadsGet) => Promise<ThreadsBaseResponse>;
475+
delete: (options: DeleteThreadOptions) => Promise<{success: boolean}>;
476+
append: (
477+
options: ThreadMessagesCreate,
478+
) => Promise<ThreadMessagesBaseResponse[]>;
479+
messages: {
480+
list: (
481+
options: ThreadMessagesList,
482+
) => Promise<ThreadMessagesBaseResponse[]>;
483+
};
484+
};
445485

446486
/**
447487
* @deprecated This method is deprecated and will be removed in a future version.
@@ -534,13 +574,16 @@ export class Langbase {
534574
this.embed = this.generateEmbeddings.bind(this);
535575
this.chunk = this.chunkDocument.bind(this);
536576
this.parse = this.parseDocument.bind(this);
537-
// this.thread = {
538-
// messages: {
539-
// add: this.addMessages.bind(this),
540-
// list: this.listMessages.bind(this),
541-
// },
542-
// delete: this.deleteThread.bind(this),
543-
// };
577+
this.threads = {
578+
create: this.createThread.bind(this),
579+
update: this.updateThread.bind(this),
580+
get: this.getThread.bind(this),
581+
delete: this.deleteThread.bind(this),
582+
append: this.appendThreadMessages.bind(this),
583+
messages: {
584+
list: this.listThreadMessages.bind(this),
585+
},
586+
};
544587
}
545588

546589
private async runPipe(
@@ -894,47 +937,71 @@ export class Langbase {
894937
}
895938

896939
/**
897-
* Adds multiple messages to a specified thread.
898-
*
899-
* @param options - The options for adding messages
900-
* @param options.threadId - The ID of the thread to add messages to
901-
* @param options.messages - The array of messages to be added
902-
* @returns A Promise that resolves to an array of Message objects
903-
* @throws May throw an error if the request fails
940+
* Creates a new thread with specified options.
941+
* @param {ThreadsCreate} options - The options object containing thread creation parameters.
942+
* @returns {Promise<ThreadsBaseResponse>} A promise that resolves to the created thread response.
943+
* @private
904944
*/
905-
private async addMessages(options: AddMessageOptions): Promise<Message[]> {
945+
private async createThread(
946+
options: ThreadsCreate,
947+
): Promise<ThreadsBaseResponse> {
906948
return this.request.post({
907-
endpoint: `/v1/threads/${options.threadId}/messages`,
908-
body: options.messages,
949+
endpoint: '/v1/threads',
950+
body: options,
909951
});
910952
}
911953

912954
/**
913-
* Retrieves all messages from a specified thread.
955+
* Updates an existing thread with the provided options.
914956
*
915-
* @param options - The options for listing messages
916-
* @param options.threadId - The unique identifier of the thread to list messages from
917-
* @returns Promise that resolves to an array of Message objects
918-
* @throws {Error} If the request fails or the thread ID is invalid
957+
* @param options - The options to update the thread with
958+
* @param options.threadId - The ID of the thread to update
959+
* @returns A promise that resolves to the updated thread response
960+
* @throws {Error} If the request fails
919961
*/
920-
private async listMessages(
921-
options: ListMessagesOptions,
922-
): Promise<Message[]> {
923-
return this.request.get({
924-
endpoint: `/v1/threads/${options.threadId}/messages`,
962+
private async updateThread(
963+
options: ThreadsUpdate,
964+
): Promise<ThreadsBaseResponse> {
965+
return this.request.post({
966+
endpoint: `/v1/threads/${options.threadId}`,
967+
body: options,
925968
});
926969
}
927970

928971
/**
929-
* Deletes a thread using the provided thread ID.
930-
* @param options - The options for deleting a thread
931-
* @param options.threadId - The unique identifier of the thread to delete
932-
* @returns A promise that resolves to true if the thread was successfully deleted
933-
* @throws Will throw an error if the deletion fails or if the thread ID is invalid
972+
* Retrieves a thread by its ID.
973+
* @param {ThreadsGet} options - The options object containing the thread ID.
974+
* @param {string} options.threadId - The unique identifier of the thread to retrieve.
975+
* @returns {Promise<ThreadsBaseResponse>} A promise that resolves to the thread data.
934976
*/
935-
private async deleteThread(options: DeleteThreadOptions): Promise<boolean> {
977+
private async getThread(options: ThreadsGet): Promise<ThreadsBaseResponse> {
978+
return this.request.get({
979+
endpoint: `/v1/threads/${options.threadId}`,
980+
});
981+
}
982+
983+
private async deleteThread(
984+
options: DeleteThreadOptions,
985+
): Promise<{success: boolean}> {
936986
return this.request.delete({
937987
endpoint: `/v1/threads/${options.threadId}`,
938988
});
939989
}
990+
991+
private async appendThreadMessages(
992+
options: ThreadMessagesCreate,
993+
): Promise<ThreadMessagesBaseResponse[]> {
994+
return this.request.post({
995+
endpoint: `/v1/threads/${options.threadId}/messages`,
996+
body: options.messages,
997+
});
998+
}
999+
1000+
private async listThreadMessages(
1001+
options: ThreadMessagesList,
1002+
): Promise<ThreadMessagesBaseResponse[]> {
1003+
return this.request.get({
1004+
endpoint: `/v1/threads/${options.threadId}/messages`,
1005+
});
1006+
}
9401007
}

0 commit comments

Comments
 (0)