@@ -16,27 +16,229 @@ export interface MemoryCreateOptions {
1616 description ?: string ;
1717}
1818
19+ export interface MemoryDeleteOptions {
20+ name : string ;
21+ }
22+
23+ export interface MemoryRetrieveOptions {
24+ query : string ;
25+ memory : {
26+ name : string ;
27+ } [ ] ;
28+ topK ?: number ;
29+ }
30+
31+ export interface MemoryListDocOptions {
32+ memoryName : string ;
33+ }
34+
35+ export interface MemoryDeleteDocOptions {
36+ memoryName : string ;
37+ documentName : string ;
38+ }
39+
40+ export interface MemoryUploadDocOptions {
41+ memoryName : string ;
42+ fileName : string ;
43+ meta ?: Record < string , string > ;
44+ file : Buffer | File | FormData | ReadableStream ;
45+ contentType :
46+ | 'application/pdf'
47+ | 'text/plain'
48+ | 'text/markdown'
49+ | 'text/csv' ;
50+ }
51+
52+ export interface MemoryRetryDocEmbedOptions {
53+ memoryName : string ;
54+ documentName : string ;
55+ }
56+
1957export interface MemoryCreateResponse extends MemoryBaseResponse { }
2058export interface MemoryListResponse extends MemoryBaseResponse { }
59+ export interface BaseDeleteResponse {
60+ success : boolean ;
61+ }
62+
63+ export interface MemoryDeleteResponse extends BaseDeleteResponse { }
64+ export interface MemoryDeleteDocResponse extends BaseDeleteResponse { }
65+ export interface MemoryRetryDocEmbedResponse extends BaseDeleteResponse { }
66+
67+ export interface MemoryRetrieveResponse {
68+ text : string ;
69+ similarity : number ;
70+ meta : Record < string , string > ;
71+ }
72+
73+ export interface MemoryListDocResponse {
74+ name : string ;
75+ status : 'queued' | 'in_progress' | 'completed' | 'failed' ;
76+ status_message : string | null ;
77+ metadata : {
78+ size : number ;
79+ type :
80+ | 'application/pdf'
81+ | 'text/plain'
82+ | 'text/markdown'
83+ | 'text/csv'
84+ | 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
85+ | 'application/vnd.ms-excel' ;
86+ } ;
87+ enabled : boolean ;
88+ chunk_size : number ;
89+ chunk_overlap : number ;
90+ owner_login : string ;
91+ }
2192
2293export class Memory {
2394 private request : Request ;
95+ private apiKey : string ;
2496
2597 constructor ( options : MemoryOptions ) {
2698 const baseUrl = 'https://api.langbase.com' ;
99+ this . apiKey = options . apiKey ;
27100 this . request = new Request ( { apiKey : options . apiKey , baseUrl} ) ;
28101 }
29102
103+ /**
104+ * Creates a new memory on Langbase.
105+ *
106+ * @param {MemoryCreateOptions } options - The options to create the memory instance.
107+ * @param {string } options.name - The name of the memory.
108+ * @param {string } options.description - The description of the memory.
109+ * @returns {Promise<MemoryCreateResponse> } A promise that resolves to the response of the memory creation.
110+ */
30111 async create ( options : MemoryCreateOptions ) : Promise < MemoryCreateResponse > {
31112 return this . request . post ( {
32113 endpoint : '/v1/memory' ,
33114 body : options ,
34115 } ) ;
35116 }
36117
118+ /**
119+ * Retrieves a list of all memories on Langbase.
120+ *
121+ * @returns {Promise<MemoryListResponse[]> } A promise that resolves to an array of memory list responses.
122+ */
37123 async list ( ) : Promise < MemoryListResponse [ ] > {
38124 return this . request . get ( {
39125 endpoint : '/v1/memory' ,
40126 } ) ;
41127 }
128+
129+ /**
130+ * Deletes a memory on Langbase.
131+ *
132+ * @param {MemoryDeleteOptions } options - The options for deleting the memory resource.
133+ * @param {string } options.name - The name of the memory to delete.
134+ * @returns {Promise<MemoryDeleteResponse> } A promise that resolves to the response of the delete operation.
135+ */
136+ async delete ( options : MemoryDeleteOptions ) : Promise < MemoryDeleteResponse > {
137+ return this . request . delete ( {
138+ endpoint : `/v1/memory/${ options . name } ` ,
139+ } ) ;
140+ }
141+
142+ /**
143+ * Retrieves similar text from the memory.
144+ *
145+ * @param {MemoryRetrieveOptions } options - The options to use for retrieving memory data.
146+ * @param {string } options.query - The query text to search for.
147+ * @param {object[] } options.memory - The memory to search in.
148+ * @param {number } [options.topK] - The number of similar texts to retrieve.
149+ * @returns A promise that resolves to an array of `MemoryRetrieveResponse` objects.
150+ */
151+ async retrieve (
152+ options : MemoryRetrieveOptions ,
153+ ) : Promise < MemoryRetrieveResponse [ ] > {
154+ return this . request . post ( {
155+ endpoint : '/v1/memory/retrieve' ,
156+ body : options ,
157+ } ) ;
158+ }
159+
160+ /**
161+ * Retrieves a list of documents inside a memory.
162+ *
163+ * @param {MemoryListDocOptions } options - The options for listing documents, including the memory name.
164+ * @param {string } options.memoryName - The name of the memory to list documents from.
165+ * @returns A promise that resolves to an array of `MemoryListDocResponse` objects.
166+ */
167+ async listDocs (
168+ options : MemoryListDocOptions ,
169+ ) : Promise < MemoryListDocResponse [ ] > {
170+ return this . request . get ( {
171+ endpoint : `/v1/memory/${ options . memoryName } /documents` ,
172+ } ) ;
173+ }
174+
175+ /**
176+ * Deletes a document from a memory.
177+ *
178+ * @param {MemoryDeleteDocOptions } options - The options for deleting the document.
179+ * @param {string } options.memoryName - The name of the memory to delete the document from.
180+ * @param {string } options.documentName - The name of the document to delete.
181+ * @returns A promise that resolves to a `MemoryDeleteDocResponse` indicating the result of the delete operation.
182+ */
183+ async deleteDoc (
184+ options : MemoryDeleteDocOptions ,
185+ ) : Promise < MemoryDeleteDocResponse > {
186+ return this . request . delete ( {
187+ endpoint : `/v1/memory/${ options . memoryName } /documents/${ options . documentName } ` ,
188+ } ) ;
189+ }
190+
191+ /**
192+ * Uploads a document to the memory.
193+ *
194+ * @param {MemoryUploadDocOptions } options - The options for uploading the document.
195+ * @param {string } options.memoryName - The name of the memory to upload the document to.
196+ * @param {string } options.fileName - The name of the file being uploaded.
197+ * @param {object } [options.meta] - Optional metadata associated with the document.
198+ * @param {string } options.contentType - The MIME type of the file being uploaded.
199+ * @param {Blob | Buffer } options.file - The file content to be uploaded.
200+ * @returns {Promise<Response> } The response from the upload request.
201+ * @throws Will throw an error if the upload fails.
202+ */
203+ async uploadDoc ( options : MemoryUploadDocOptions ) : Promise < Response > {
204+ try {
205+ const response = ( await this . request . post ( {
206+ endpoint : `/v1/memory/documents` ,
207+ body : {
208+ memoryName : options . memoryName ,
209+ fileName : options . fileName ,
210+ meta : options . meta ,
211+ } ,
212+ } ) ) as unknown as { signedUrl : string } ;
213+
214+ const uploadUrl = response . signedUrl ;
215+
216+ return await fetch ( uploadUrl , {
217+ method : 'PUT' ,
218+ headers : {
219+ Authorization : `Bearer ${ this . apiKey } ` ,
220+ 'Content-Type' : options . contentType ,
221+ } ,
222+ body : options . file ,
223+ } ) ;
224+ } catch ( error ) {
225+ throw error ;
226+ }
227+ }
228+
229+ /**
230+ * Retries the embedding process for a specific document in memory.
231+ *
232+ * @param options - The options required to retry the document embedding.
233+ * @param options.memoryName - The name of the memory containing the document.
234+ * @param options.documentName - The name of the document to retry embedding for.
235+ * @returns A promise that resolves to the response of the retry operation.
236+ */
237+ async retryDocEmbed (
238+ options : MemoryRetryDocEmbedOptions ,
239+ ) : Promise < MemoryRetryDocEmbedResponse > {
240+ return this . request . get ( {
241+ endpoint : `/v1/memory/${ options . memoryName } /documents/${ options . documentName } /embeddings/retry` ,
242+ } ) ;
243+ }
42244}
0 commit comments