@@ -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+
9499export 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
354365export 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+
358391export 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