@langchain/langgraph-sdk 0.0.40 → 0.0.41
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/react/stream.cjs +125 -120
- package/dist/react/stream.d.ts +131 -22
- package/dist/react/stream.js +125 -120
- package/package.json +5 -4
- package/dist/client.js +0 -888
- package/dist/index.js +0 -2
- package/dist/schema.cjs +0 -2
- package/dist/singletons/fetch.cjs +0 -27
- package/dist/singletons/fetch.d.ts +0 -11
- package/dist/singletons/fetch.js +0 -22
- package/dist/types.cjs +0 -2
- package/dist/types.d.ts +0 -141
- package/dist/types.js +0 -1
- package/dist/types.messages.d.ts +0 -88
- package/dist/types.stream.d.ts +0 -156
- package/dist/types.stream.js +0 -1
- package/dist/utils/async_caller.cjs +0 -197
- package/dist/utils/async_caller.d.ts +0 -48
- package/dist/utils/env.d.ts +0 -1
- package/dist/utils/env.js +0 -12
- package/dist/utils/signals.js +0 -18
- package/dist/utils/sse.d.ts +0 -11
- package/dist/utils/sse.js +0 -152
- package/dist/utils/stream.d.ts +0 -13
- package/dist/utils/stream.js +0 -111
package/dist/client.js
DELETED
|
@@ -1,888 +0,0 @@
|
|
|
1
|
-
import { AsyncCaller } from "./utils/async_caller.js";
|
|
2
|
-
import { IterableReadableStream } from "./utils/stream.js";
|
|
3
|
-
import { mergeSignals } from "./utils/signals.js";
|
|
4
|
-
import { getEnvironmentVariable } from "./utils/env.js";
|
|
5
|
-
import { BytesLineDecoder, SSEDecoder } from "./utils/sse.js";
|
|
6
|
-
/**
|
|
7
|
-
* Get the API key from the environment.
|
|
8
|
-
* Precedence:
|
|
9
|
-
* 1. explicit argument
|
|
10
|
-
* 2. LANGGRAPH_API_KEY
|
|
11
|
-
* 3. LANGSMITH_API_KEY
|
|
12
|
-
* 4. LANGCHAIN_API_KEY
|
|
13
|
-
*
|
|
14
|
-
* @param apiKey - Optional API key provided as an argument
|
|
15
|
-
* @returns The API key if found, otherwise undefined
|
|
16
|
-
*/
|
|
17
|
-
export function getApiKey(apiKey) {
|
|
18
|
-
if (apiKey) {
|
|
19
|
-
return apiKey;
|
|
20
|
-
}
|
|
21
|
-
const prefixes = ["LANGGRAPH", "LANGSMITH", "LANGCHAIN"];
|
|
22
|
-
for (const prefix of prefixes) {
|
|
23
|
-
const envKey = getEnvironmentVariable(`${prefix}_API_KEY`);
|
|
24
|
-
if (envKey) {
|
|
25
|
-
// Remove surrounding quotes
|
|
26
|
-
return envKey.trim().replace(/^["']|["']$/g, "");
|
|
27
|
-
}
|
|
28
|
-
}
|
|
29
|
-
return undefined;
|
|
30
|
-
}
|
|
31
|
-
class BaseClient {
|
|
32
|
-
constructor(config) {
|
|
33
|
-
Object.defineProperty(this, "asyncCaller", {
|
|
34
|
-
enumerable: true,
|
|
35
|
-
configurable: true,
|
|
36
|
-
writable: true,
|
|
37
|
-
value: void 0
|
|
38
|
-
});
|
|
39
|
-
Object.defineProperty(this, "timeoutMs", {
|
|
40
|
-
enumerable: true,
|
|
41
|
-
configurable: true,
|
|
42
|
-
writable: true,
|
|
43
|
-
value: void 0
|
|
44
|
-
});
|
|
45
|
-
Object.defineProperty(this, "apiUrl", {
|
|
46
|
-
enumerable: true,
|
|
47
|
-
configurable: true,
|
|
48
|
-
writable: true,
|
|
49
|
-
value: void 0
|
|
50
|
-
});
|
|
51
|
-
Object.defineProperty(this, "defaultHeaders", {
|
|
52
|
-
enumerable: true,
|
|
53
|
-
configurable: true,
|
|
54
|
-
writable: true,
|
|
55
|
-
value: void 0
|
|
56
|
-
});
|
|
57
|
-
this.asyncCaller = new AsyncCaller({
|
|
58
|
-
maxRetries: 4,
|
|
59
|
-
maxConcurrency: 4,
|
|
60
|
-
...config?.callerOptions,
|
|
61
|
-
});
|
|
62
|
-
this.timeoutMs = config?.timeoutMs;
|
|
63
|
-
// default limit being capped by Chrome
|
|
64
|
-
// https://github.com/nodejs/undici/issues/1373
|
|
65
|
-
// Regex to remove trailing slash, if present
|
|
66
|
-
this.apiUrl = config?.apiUrl?.replace(/\/$/, "") || "http://localhost:8123";
|
|
67
|
-
this.defaultHeaders = config?.defaultHeaders || {};
|
|
68
|
-
const apiKey = getApiKey(config?.apiKey);
|
|
69
|
-
if (apiKey) {
|
|
70
|
-
this.defaultHeaders["X-Api-Key"] = apiKey;
|
|
71
|
-
}
|
|
72
|
-
}
|
|
73
|
-
prepareFetchOptions(path, options) {
|
|
74
|
-
const mutatedOptions = {
|
|
75
|
-
...options,
|
|
76
|
-
headers: { ...this.defaultHeaders, ...options?.headers },
|
|
77
|
-
};
|
|
78
|
-
if (mutatedOptions.json) {
|
|
79
|
-
mutatedOptions.body = JSON.stringify(mutatedOptions.json);
|
|
80
|
-
mutatedOptions.headers = {
|
|
81
|
-
...mutatedOptions.headers,
|
|
82
|
-
"Content-Type": "application/json",
|
|
83
|
-
};
|
|
84
|
-
delete mutatedOptions.json;
|
|
85
|
-
}
|
|
86
|
-
let timeoutSignal = null;
|
|
87
|
-
if (typeof options?.timeoutMs !== "undefined") {
|
|
88
|
-
if (options.timeoutMs != null) {
|
|
89
|
-
timeoutSignal = AbortSignal.timeout(options.timeoutMs);
|
|
90
|
-
}
|
|
91
|
-
}
|
|
92
|
-
else if (this.timeoutMs != null) {
|
|
93
|
-
timeoutSignal = AbortSignal.timeout(this.timeoutMs);
|
|
94
|
-
}
|
|
95
|
-
mutatedOptions.signal = mergeSignals(timeoutSignal, mutatedOptions.signal);
|
|
96
|
-
const targetUrl = new URL(`${this.apiUrl}${path}`);
|
|
97
|
-
if (mutatedOptions.params) {
|
|
98
|
-
for (const [key, value] of Object.entries(mutatedOptions.params)) {
|
|
99
|
-
if (value == null)
|
|
100
|
-
continue;
|
|
101
|
-
let strValue = typeof value === "string" || typeof value === "number"
|
|
102
|
-
? value.toString()
|
|
103
|
-
: JSON.stringify(value);
|
|
104
|
-
targetUrl.searchParams.append(key, strValue);
|
|
105
|
-
}
|
|
106
|
-
delete mutatedOptions.params;
|
|
107
|
-
}
|
|
108
|
-
return [targetUrl, mutatedOptions];
|
|
109
|
-
}
|
|
110
|
-
async fetch(path, options) {
|
|
111
|
-
const response = await this.asyncCaller.fetch(...this.prepareFetchOptions(path, options));
|
|
112
|
-
if (response.status === 202 || response.status === 204) {
|
|
113
|
-
return undefined;
|
|
114
|
-
}
|
|
115
|
-
return response.json();
|
|
116
|
-
}
|
|
117
|
-
}
|
|
118
|
-
export class CronsClient extends BaseClient {
|
|
119
|
-
/**
|
|
120
|
-
*
|
|
121
|
-
* @param threadId The ID of the thread.
|
|
122
|
-
* @param assistantId Assistant ID to use for this cron job.
|
|
123
|
-
* @param payload Payload for creating a cron job.
|
|
124
|
-
* @returns The created background run.
|
|
125
|
-
*/
|
|
126
|
-
async createForThread(threadId, assistantId, payload) {
|
|
127
|
-
const json = {
|
|
128
|
-
schedule: payload?.schedule,
|
|
129
|
-
input: payload?.input,
|
|
130
|
-
config: payload?.config,
|
|
131
|
-
metadata: payload?.metadata,
|
|
132
|
-
assistant_id: assistantId,
|
|
133
|
-
interrupt_before: payload?.interruptBefore,
|
|
134
|
-
interrupt_after: payload?.interruptAfter,
|
|
135
|
-
webhook: payload?.webhook,
|
|
136
|
-
multitask_strategy: payload?.multitaskStrategy,
|
|
137
|
-
if_not_exists: payload?.ifNotExists,
|
|
138
|
-
};
|
|
139
|
-
return this.fetch(`/threads/${threadId}/runs/crons`, {
|
|
140
|
-
method: "POST",
|
|
141
|
-
json,
|
|
142
|
-
});
|
|
143
|
-
}
|
|
144
|
-
/**
|
|
145
|
-
*
|
|
146
|
-
* @param assistantId Assistant ID to use for this cron job.
|
|
147
|
-
* @param payload Payload for creating a cron job.
|
|
148
|
-
* @returns
|
|
149
|
-
*/
|
|
150
|
-
async create(assistantId, payload) {
|
|
151
|
-
const json = {
|
|
152
|
-
schedule: payload?.schedule,
|
|
153
|
-
input: payload?.input,
|
|
154
|
-
config: payload?.config,
|
|
155
|
-
metadata: payload?.metadata,
|
|
156
|
-
assistant_id: assistantId,
|
|
157
|
-
interrupt_before: payload?.interruptBefore,
|
|
158
|
-
interrupt_after: payload?.interruptAfter,
|
|
159
|
-
webhook: payload?.webhook,
|
|
160
|
-
multitask_strategy: payload?.multitaskStrategy,
|
|
161
|
-
if_not_exists: payload?.ifNotExists,
|
|
162
|
-
};
|
|
163
|
-
return this.fetch(`/runs/crons`, {
|
|
164
|
-
method: "POST",
|
|
165
|
-
json,
|
|
166
|
-
});
|
|
167
|
-
}
|
|
168
|
-
/**
|
|
169
|
-
*
|
|
170
|
-
* @param cronId Cron ID of Cron job to delete.
|
|
171
|
-
*/
|
|
172
|
-
async delete(cronId) {
|
|
173
|
-
await this.fetch(`/runs/crons/${cronId}`, {
|
|
174
|
-
method: "DELETE",
|
|
175
|
-
});
|
|
176
|
-
}
|
|
177
|
-
/**
|
|
178
|
-
*
|
|
179
|
-
* @param query Query options.
|
|
180
|
-
* @returns List of crons.
|
|
181
|
-
*/
|
|
182
|
-
async search(query) {
|
|
183
|
-
return this.fetch("/runs/crons/search", {
|
|
184
|
-
method: "POST",
|
|
185
|
-
json: {
|
|
186
|
-
assistant_id: query?.assistantId ?? undefined,
|
|
187
|
-
thread_id: query?.threadId ?? undefined,
|
|
188
|
-
limit: query?.limit ?? 10,
|
|
189
|
-
offset: query?.offset ?? 0,
|
|
190
|
-
},
|
|
191
|
-
});
|
|
192
|
-
}
|
|
193
|
-
}
|
|
194
|
-
export class AssistantsClient extends BaseClient {
|
|
195
|
-
/**
|
|
196
|
-
* Get an assistant by ID.
|
|
197
|
-
*
|
|
198
|
-
* @param assistantId The ID of the assistant.
|
|
199
|
-
* @returns Assistant
|
|
200
|
-
*/
|
|
201
|
-
async get(assistantId) {
|
|
202
|
-
return this.fetch(`/assistants/${assistantId}`);
|
|
203
|
-
}
|
|
204
|
-
/**
|
|
205
|
-
* Get the JSON representation of the graph assigned to a runnable
|
|
206
|
-
* @param assistantId The ID of the assistant.
|
|
207
|
-
* @param options.xray Whether to include subgraphs in the serialized graph representation. If an integer value is provided, only subgraphs with a depth less than or equal to the value will be included.
|
|
208
|
-
* @returns Serialized graph
|
|
209
|
-
*/
|
|
210
|
-
async getGraph(assistantId, options) {
|
|
211
|
-
return this.fetch(`/assistants/${assistantId}/graph`, {
|
|
212
|
-
params: { xray: options?.xray },
|
|
213
|
-
});
|
|
214
|
-
}
|
|
215
|
-
/**
|
|
216
|
-
* Get the state and config schema of the graph assigned to a runnable
|
|
217
|
-
* @param assistantId The ID of the assistant.
|
|
218
|
-
* @returns Graph schema
|
|
219
|
-
*/
|
|
220
|
-
async getSchemas(assistantId) {
|
|
221
|
-
return this.fetch(`/assistants/${assistantId}/schemas`);
|
|
222
|
-
}
|
|
223
|
-
/**
|
|
224
|
-
* Get the schemas of an assistant by ID.
|
|
225
|
-
*
|
|
226
|
-
* @param assistantId The ID of the assistant to get the schema of.
|
|
227
|
-
* @param options Additional options for getting subgraphs, such as namespace or recursion extraction.
|
|
228
|
-
* @returns The subgraphs of the assistant.
|
|
229
|
-
*/
|
|
230
|
-
async getSubgraphs(assistantId, options) {
|
|
231
|
-
if (options?.namespace) {
|
|
232
|
-
return this.fetch(`/assistants/${assistantId}/subgraphs/${options.namespace}`, { params: { recurse: options?.recurse } });
|
|
233
|
-
}
|
|
234
|
-
return this.fetch(`/assistants/${assistantId}/subgraphs`, {
|
|
235
|
-
params: { recurse: options?.recurse },
|
|
236
|
-
});
|
|
237
|
-
}
|
|
238
|
-
/**
|
|
239
|
-
* Create a new assistant.
|
|
240
|
-
* @param payload Payload for creating an assistant.
|
|
241
|
-
* @returns The created assistant.
|
|
242
|
-
*/
|
|
243
|
-
async create(payload) {
|
|
244
|
-
return this.fetch("/assistants", {
|
|
245
|
-
method: "POST",
|
|
246
|
-
json: {
|
|
247
|
-
graph_id: payload.graphId,
|
|
248
|
-
config: payload.config,
|
|
249
|
-
metadata: payload.metadata,
|
|
250
|
-
assistant_id: payload.assistantId,
|
|
251
|
-
if_exists: payload.ifExists,
|
|
252
|
-
name: payload.name,
|
|
253
|
-
},
|
|
254
|
-
});
|
|
255
|
-
}
|
|
256
|
-
/**
|
|
257
|
-
* Update an assistant.
|
|
258
|
-
* @param assistantId ID of the assistant.
|
|
259
|
-
* @param payload Payload for updating the assistant.
|
|
260
|
-
* @returns The updated assistant.
|
|
261
|
-
*/
|
|
262
|
-
async update(assistantId, payload) {
|
|
263
|
-
return this.fetch(`/assistants/${assistantId}`, {
|
|
264
|
-
method: "PATCH",
|
|
265
|
-
json: {
|
|
266
|
-
graph_id: payload.graphId,
|
|
267
|
-
config: payload.config,
|
|
268
|
-
metadata: payload.metadata,
|
|
269
|
-
name: payload.name,
|
|
270
|
-
},
|
|
271
|
-
});
|
|
272
|
-
}
|
|
273
|
-
/**
|
|
274
|
-
* Delete an assistant.
|
|
275
|
-
*
|
|
276
|
-
* @param assistantId ID of the assistant.
|
|
277
|
-
*/
|
|
278
|
-
async delete(assistantId) {
|
|
279
|
-
return this.fetch(`/assistants/${assistantId}`, {
|
|
280
|
-
method: "DELETE",
|
|
281
|
-
});
|
|
282
|
-
}
|
|
283
|
-
/**
|
|
284
|
-
* List assistants.
|
|
285
|
-
* @param query Query options.
|
|
286
|
-
* @returns List of assistants.
|
|
287
|
-
*/
|
|
288
|
-
async search(query) {
|
|
289
|
-
return this.fetch("/assistants/search", {
|
|
290
|
-
method: "POST",
|
|
291
|
-
json: {
|
|
292
|
-
graph_id: query?.graphId ?? undefined,
|
|
293
|
-
metadata: query?.metadata ?? undefined,
|
|
294
|
-
limit: query?.limit ?? 10,
|
|
295
|
-
offset: query?.offset ?? 0,
|
|
296
|
-
},
|
|
297
|
-
});
|
|
298
|
-
}
|
|
299
|
-
/**
|
|
300
|
-
* List all versions of an assistant.
|
|
301
|
-
*
|
|
302
|
-
* @param assistantId ID of the assistant.
|
|
303
|
-
* @returns List of assistant versions.
|
|
304
|
-
*/
|
|
305
|
-
async getVersions(assistantId, payload) {
|
|
306
|
-
return this.fetch(`/assistants/${assistantId}/versions`, {
|
|
307
|
-
method: "POST",
|
|
308
|
-
json: {
|
|
309
|
-
metadata: payload?.metadata ?? undefined,
|
|
310
|
-
limit: payload?.limit ?? 10,
|
|
311
|
-
offset: payload?.offset ?? 0,
|
|
312
|
-
},
|
|
313
|
-
});
|
|
314
|
-
}
|
|
315
|
-
/**
|
|
316
|
-
* Change the version of an assistant.
|
|
317
|
-
*
|
|
318
|
-
* @param assistantId ID of the assistant.
|
|
319
|
-
* @param version The version to change to.
|
|
320
|
-
* @returns The updated assistant.
|
|
321
|
-
*/
|
|
322
|
-
async setLatest(assistantId, version) {
|
|
323
|
-
return this.fetch(`/assistants/${assistantId}/latest`, {
|
|
324
|
-
method: "POST",
|
|
325
|
-
json: { version },
|
|
326
|
-
});
|
|
327
|
-
}
|
|
328
|
-
}
|
|
329
|
-
export class ThreadsClient extends BaseClient {
|
|
330
|
-
/**
|
|
331
|
-
* Get a thread by ID.
|
|
332
|
-
*
|
|
333
|
-
* @param threadId ID of the thread.
|
|
334
|
-
* @returns The thread.
|
|
335
|
-
*/
|
|
336
|
-
async get(threadId) {
|
|
337
|
-
return this.fetch(`/threads/${threadId}`);
|
|
338
|
-
}
|
|
339
|
-
/**
|
|
340
|
-
* Create a new thread.
|
|
341
|
-
*
|
|
342
|
-
* @param payload Payload for creating a thread.
|
|
343
|
-
* @returns The created thread.
|
|
344
|
-
*/
|
|
345
|
-
async create(payload) {
|
|
346
|
-
return this.fetch(`/threads`, {
|
|
347
|
-
method: "POST",
|
|
348
|
-
json: {
|
|
349
|
-
metadata: payload?.metadata,
|
|
350
|
-
thread_id: payload?.threadId,
|
|
351
|
-
if_exists: payload?.ifExists,
|
|
352
|
-
},
|
|
353
|
-
});
|
|
354
|
-
}
|
|
355
|
-
/**
|
|
356
|
-
* Copy an existing thread
|
|
357
|
-
* @param threadId ID of the thread to be copied
|
|
358
|
-
* @returns Newly copied thread
|
|
359
|
-
*/
|
|
360
|
-
async copy(threadId) {
|
|
361
|
-
return this.fetch(`/threads/${threadId}/copy`, {
|
|
362
|
-
method: "POST",
|
|
363
|
-
});
|
|
364
|
-
}
|
|
365
|
-
/**
|
|
366
|
-
* Update a thread.
|
|
367
|
-
*
|
|
368
|
-
* @param threadId ID of the thread.
|
|
369
|
-
* @param payload Payload for updating the thread.
|
|
370
|
-
* @returns The updated thread.
|
|
371
|
-
*/
|
|
372
|
-
async update(threadId, payload) {
|
|
373
|
-
return this.fetch(`/threads/${threadId}`, {
|
|
374
|
-
method: "PATCH",
|
|
375
|
-
json: { metadata: payload?.metadata },
|
|
376
|
-
});
|
|
377
|
-
}
|
|
378
|
-
/**
|
|
379
|
-
* Delete a thread.
|
|
380
|
-
*
|
|
381
|
-
* @param threadId ID of the thread.
|
|
382
|
-
*/
|
|
383
|
-
async delete(threadId) {
|
|
384
|
-
return this.fetch(`/threads/${threadId}`, {
|
|
385
|
-
method: "DELETE",
|
|
386
|
-
});
|
|
387
|
-
}
|
|
388
|
-
/**
|
|
389
|
-
* List threads
|
|
390
|
-
*
|
|
391
|
-
* @param query Query options
|
|
392
|
-
* @returns List of threads
|
|
393
|
-
*/
|
|
394
|
-
async search(query) {
|
|
395
|
-
return this.fetch("/threads/search", {
|
|
396
|
-
method: "POST",
|
|
397
|
-
json: {
|
|
398
|
-
metadata: query?.metadata ?? undefined,
|
|
399
|
-
limit: query?.limit ?? 10,
|
|
400
|
-
offset: query?.offset ?? 0,
|
|
401
|
-
status: query?.status,
|
|
402
|
-
},
|
|
403
|
-
});
|
|
404
|
-
}
|
|
405
|
-
/**
|
|
406
|
-
* Get state for a thread.
|
|
407
|
-
*
|
|
408
|
-
* @param threadId ID of the thread.
|
|
409
|
-
* @returns Thread state.
|
|
410
|
-
*/
|
|
411
|
-
async getState(threadId, checkpoint, options) {
|
|
412
|
-
if (checkpoint != null) {
|
|
413
|
-
if (typeof checkpoint !== "string") {
|
|
414
|
-
return this.fetch(`/threads/${threadId}/state/checkpoint`, {
|
|
415
|
-
method: "POST",
|
|
416
|
-
json: { checkpoint, subgraphs: options?.subgraphs },
|
|
417
|
-
});
|
|
418
|
-
}
|
|
419
|
-
// deprecated
|
|
420
|
-
return this.fetch(`/threads/${threadId}/state/${checkpoint}`, { params: { subgraphs: options?.subgraphs } });
|
|
421
|
-
}
|
|
422
|
-
return this.fetch(`/threads/${threadId}/state`, {
|
|
423
|
-
params: { subgraphs: options?.subgraphs },
|
|
424
|
-
});
|
|
425
|
-
}
|
|
426
|
-
/**
|
|
427
|
-
* Add state to a thread.
|
|
428
|
-
*
|
|
429
|
-
* @param threadId The ID of the thread.
|
|
430
|
-
* @returns
|
|
431
|
-
*/
|
|
432
|
-
async updateState(threadId, options) {
|
|
433
|
-
return this.fetch(`/threads/${threadId}/state`, {
|
|
434
|
-
method: "POST",
|
|
435
|
-
json: {
|
|
436
|
-
values: options.values,
|
|
437
|
-
checkpoint_id: options.checkpointId,
|
|
438
|
-
checkpoint: options.checkpoint,
|
|
439
|
-
as_node: options?.asNode,
|
|
440
|
-
},
|
|
441
|
-
});
|
|
442
|
-
}
|
|
443
|
-
/**
|
|
444
|
-
* Patch the metadata of a thread.
|
|
445
|
-
*
|
|
446
|
-
* @param threadIdOrConfig Thread ID or config to patch the state of.
|
|
447
|
-
* @param metadata Metadata to patch the state with.
|
|
448
|
-
*/
|
|
449
|
-
async patchState(threadIdOrConfig, metadata) {
|
|
450
|
-
let threadId;
|
|
451
|
-
if (typeof threadIdOrConfig !== "string") {
|
|
452
|
-
if (typeof threadIdOrConfig.configurable?.thread_id !== "string") {
|
|
453
|
-
throw new Error("Thread ID is required when updating state with a config.");
|
|
454
|
-
}
|
|
455
|
-
threadId = threadIdOrConfig.configurable.thread_id;
|
|
456
|
-
}
|
|
457
|
-
else {
|
|
458
|
-
threadId = threadIdOrConfig;
|
|
459
|
-
}
|
|
460
|
-
return this.fetch(`/threads/${threadId}/state`, {
|
|
461
|
-
method: "PATCH",
|
|
462
|
-
json: { metadata: metadata },
|
|
463
|
-
});
|
|
464
|
-
}
|
|
465
|
-
/**
|
|
466
|
-
* Get all past states for a thread.
|
|
467
|
-
*
|
|
468
|
-
* @param threadId ID of the thread.
|
|
469
|
-
* @param options Additional options.
|
|
470
|
-
* @returns List of thread states.
|
|
471
|
-
*/
|
|
472
|
-
async getHistory(threadId, options) {
|
|
473
|
-
return this.fetch(`/threads/${threadId}/history`, {
|
|
474
|
-
method: "POST",
|
|
475
|
-
json: {
|
|
476
|
-
limit: options?.limit ?? 10,
|
|
477
|
-
before: options?.before,
|
|
478
|
-
metadata: options?.metadata,
|
|
479
|
-
checkpoint: options?.checkpoint,
|
|
480
|
-
},
|
|
481
|
-
});
|
|
482
|
-
}
|
|
483
|
-
}
|
|
484
|
-
export class RunsClient extends BaseClient {
|
|
485
|
-
/**
|
|
486
|
-
* Create a run and stream the results.
|
|
487
|
-
*
|
|
488
|
-
* @param threadId The ID of the thread.
|
|
489
|
-
* @param assistantId Assistant ID to use for this run.
|
|
490
|
-
* @param payload Payload for creating a run.
|
|
491
|
-
*/
|
|
492
|
-
async *stream(threadId, assistantId, payload) {
|
|
493
|
-
const json = {
|
|
494
|
-
input: payload?.input,
|
|
495
|
-
command: payload?.command,
|
|
496
|
-
config: payload?.config,
|
|
497
|
-
metadata: payload?.metadata,
|
|
498
|
-
stream_mode: payload?.streamMode,
|
|
499
|
-
stream_subgraphs: payload?.streamSubgraphs,
|
|
500
|
-
feedback_keys: payload?.feedbackKeys,
|
|
501
|
-
assistant_id: assistantId,
|
|
502
|
-
interrupt_before: payload?.interruptBefore,
|
|
503
|
-
interrupt_after: payload?.interruptAfter,
|
|
504
|
-
checkpoint: payload?.checkpoint,
|
|
505
|
-
checkpoint_id: payload?.checkpointId,
|
|
506
|
-
webhook: payload?.webhook,
|
|
507
|
-
multitask_strategy: payload?.multitaskStrategy,
|
|
508
|
-
on_completion: payload?.onCompletion,
|
|
509
|
-
on_disconnect: payload?.onDisconnect,
|
|
510
|
-
after_seconds: payload?.afterSeconds,
|
|
511
|
-
if_not_exists: payload?.ifNotExists,
|
|
512
|
-
};
|
|
513
|
-
const endpoint = threadId == null ? `/runs/stream` : `/threads/${threadId}/runs/stream`;
|
|
514
|
-
const response = await this.asyncCaller.fetch(...this.prepareFetchOptions(endpoint, {
|
|
515
|
-
method: "POST",
|
|
516
|
-
json,
|
|
517
|
-
timeoutMs: null,
|
|
518
|
-
signal: payload?.signal,
|
|
519
|
-
}));
|
|
520
|
-
const stream = (response.body || new ReadableStream({ start: (ctrl) => ctrl.close() }))
|
|
521
|
-
.pipeThrough(new BytesLineDecoder())
|
|
522
|
-
.pipeThrough(new SSEDecoder());
|
|
523
|
-
yield* IterableReadableStream.fromReadableStream(stream);
|
|
524
|
-
}
|
|
525
|
-
/**
|
|
526
|
-
* Create a run.
|
|
527
|
-
*
|
|
528
|
-
* @param threadId The ID of the thread.
|
|
529
|
-
* @param assistantId Assistant ID to use for this run.
|
|
530
|
-
* @param payload Payload for creating a run.
|
|
531
|
-
* @returns The created run.
|
|
532
|
-
*/
|
|
533
|
-
async create(threadId, assistantId, payload) {
|
|
534
|
-
const json = {
|
|
535
|
-
input: payload?.input,
|
|
536
|
-
command: payload?.command,
|
|
537
|
-
config: payload?.config,
|
|
538
|
-
metadata: payload?.metadata,
|
|
539
|
-
stream_mode: payload?.streamMode,
|
|
540
|
-
stream_subgraphs: payload?.streamSubgraphs,
|
|
541
|
-
assistant_id: assistantId,
|
|
542
|
-
interrupt_before: payload?.interruptBefore,
|
|
543
|
-
interrupt_after: payload?.interruptAfter,
|
|
544
|
-
webhook: payload?.webhook,
|
|
545
|
-
checkpoint: payload?.checkpoint,
|
|
546
|
-
checkpoint_id: payload?.checkpointId,
|
|
547
|
-
multitask_strategy: payload?.multitaskStrategy,
|
|
548
|
-
after_seconds: payload?.afterSeconds,
|
|
549
|
-
if_not_exists: payload?.ifNotExists,
|
|
550
|
-
};
|
|
551
|
-
return this.fetch(`/threads/${threadId}/runs`, {
|
|
552
|
-
method: "POST",
|
|
553
|
-
json,
|
|
554
|
-
signal: payload?.signal,
|
|
555
|
-
});
|
|
556
|
-
}
|
|
557
|
-
/**
|
|
558
|
-
* Create a batch of stateless background runs.
|
|
559
|
-
*
|
|
560
|
-
* @param payloads An array of payloads for creating runs.
|
|
561
|
-
* @returns An array of created runs.
|
|
562
|
-
*/
|
|
563
|
-
async createBatch(payloads) {
|
|
564
|
-
const filteredPayloads = payloads
|
|
565
|
-
.map((payload) => ({ ...payload, assistant_id: payload.assistantId }))
|
|
566
|
-
.map((payload) => {
|
|
567
|
-
return Object.fromEntries(Object.entries(payload).filter(([_, v]) => v !== undefined));
|
|
568
|
-
});
|
|
569
|
-
return this.fetch("/runs/batch", {
|
|
570
|
-
method: "POST",
|
|
571
|
-
json: filteredPayloads,
|
|
572
|
-
});
|
|
573
|
-
}
|
|
574
|
-
/**
|
|
575
|
-
* Create a run and wait for it to complete.
|
|
576
|
-
*
|
|
577
|
-
* @param threadId The ID of the thread.
|
|
578
|
-
* @param assistantId Assistant ID to use for this run.
|
|
579
|
-
* @param payload Payload for creating a run.
|
|
580
|
-
* @returns The last values chunk of the thread.
|
|
581
|
-
*/
|
|
582
|
-
async wait(threadId, assistantId, payload) {
|
|
583
|
-
const json = {
|
|
584
|
-
input: payload?.input,
|
|
585
|
-
command: payload?.command,
|
|
586
|
-
config: payload?.config,
|
|
587
|
-
metadata: payload?.metadata,
|
|
588
|
-
assistant_id: assistantId,
|
|
589
|
-
interrupt_before: payload?.interruptBefore,
|
|
590
|
-
interrupt_after: payload?.interruptAfter,
|
|
591
|
-
checkpoint: payload?.checkpoint,
|
|
592
|
-
checkpoint_id: payload?.checkpointId,
|
|
593
|
-
webhook: payload?.webhook,
|
|
594
|
-
multitask_strategy: payload?.multitaskStrategy,
|
|
595
|
-
on_completion: payload?.onCompletion,
|
|
596
|
-
on_disconnect: payload?.onDisconnect,
|
|
597
|
-
after_seconds: payload?.afterSeconds,
|
|
598
|
-
if_not_exists: payload?.ifNotExists,
|
|
599
|
-
};
|
|
600
|
-
const endpoint = threadId == null ? `/runs/wait` : `/threads/${threadId}/runs/wait`;
|
|
601
|
-
const response = await this.fetch(endpoint, {
|
|
602
|
-
method: "POST",
|
|
603
|
-
json,
|
|
604
|
-
timeoutMs: null,
|
|
605
|
-
signal: payload?.signal,
|
|
606
|
-
});
|
|
607
|
-
const raiseError = payload?.raiseError !== undefined ? payload.raiseError : true;
|
|
608
|
-
if (raiseError &&
|
|
609
|
-
"__error__" in response &&
|
|
610
|
-
typeof response.__error__ === "object" &&
|
|
611
|
-
response.__error__ &&
|
|
612
|
-
"error" in response.__error__ &&
|
|
613
|
-
"message" in response.__error__) {
|
|
614
|
-
throw new Error(`${response.__error__?.error}: ${response.__error__?.message}`);
|
|
615
|
-
}
|
|
616
|
-
return response;
|
|
617
|
-
}
|
|
618
|
-
/**
|
|
619
|
-
* List all runs for a thread.
|
|
620
|
-
*
|
|
621
|
-
* @param threadId The ID of the thread.
|
|
622
|
-
* @param options Filtering and pagination options.
|
|
623
|
-
* @returns List of runs.
|
|
624
|
-
*/
|
|
625
|
-
async list(threadId, options) {
|
|
626
|
-
return this.fetch(`/threads/${threadId}/runs`, {
|
|
627
|
-
params: {
|
|
628
|
-
limit: options?.limit ?? 10,
|
|
629
|
-
offset: options?.offset ?? 0,
|
|
630
|
-
status: options?.status ?? undefined,
|
|
631
|
-
},
|
|
632
|
-
});
|
|
633
|
-
}
|
|
634
|
-
/**
|
|
635
|
-
* Get a run by ID.
|
|
636
|
-
*
|
|
637
|
-
* @param threadId The ID of the thread.
|
|
638
|
-
* @param runId The ID of the run.
|
|
639
|
-
* @returns The run.
|
|
640
|
-
*/
|
|
641
|
-
async get(threadId, runId) {
|
|
642
|
-
return this.fetch(`/threads/${threadId}/runs/${runId}`);
|
|
643
|
-
}
|
|
644
|
-
/**
|
|
645
|
-
* Cancel a run.
|
|
646
|
-
*
|
|
647
|
-
* @param threadId The ID of the thread.
|
|
648
|
-
* @param runId The ID of the run.
|
|
649
|
-
* @param wait Whether to block when canceling
|
|
650
|
-
* @param action Action to take when cancelling the run. Possible values are `interrupt` or `rollback`. Default is `interrupt`.
|
|
651
|
-
* @returns
|
|
652
|
-
*/
|
|
653
|
-
async cancel(threadId, runId, wait = false, action = "interrupt") {
|
|
654
|
-
return this.fetch(`/threads/${threadId}/runs/${runId}/cancel`, {
|
|
655
|
-
method: "POST",
|
|
656
|
-
params: {
|
|
657
|
-
wait: wait ? "1" : "0",
|
|
658
|
-
action: action,
|
|
659
|
-
},
|
|
660
|
-
});
|
|
661
|
-
}
|
|
662
|
-
/**
|
|
663
|
-
* Block until a run is done.
|
|
664
|
-
*
|
|
665
|
-
* @param threadId The ID of the thread.
|
|
666
|
-
* @param runId The ID of the run.
|
|
667
|
-
* @returns
|
|
668
|
-
*/
|
|
669
|
-
async join(threadId, runId, options) {
|
|
670
|
-
return this.fetch(`/threads/${threadId}/runs/${runId}/join`, {
|
|
671
|
-
timeoutMs: null,
|
|
672
|
-
signal: options?.signal,
|
|
673
|
-
});
|
|
674
|
-
}
|
|
675
|
-
/**
|
|
676
|
-
* Stream output from a run in real-time, until the run is done.
|
|
677
|
-
* Output is not buffered, so any output produced before this call will
|
|
678
|
-
* not be received here.
|
|
679
|
-
*
|
|
680
|
-
* @param threadId The ID of the thread.
|
|
681
|
-
* @param runId The ID of the run.
|
|
682
|
-
* @returns An async generator yielding stream parts.
|
|
683
|
-
*/
|
|
684
|
-
async *joinStream(threadId, runId, options) {
|
|
685
|
-
const opts = typeof options === "object" &&
|
|
686
|
-
options != null &&
|
|
687
|
-
options instanceof AbortSignal
|
|
688
|
-
? { signal: options }
|
|
689
|
-
: options;
|
|
690
|
-
const response = await this.asyncCaller.fetch(...this.prepareFetchOptions(`/threads/${threadId}/runs/${runId}/stream`, {
|
|
691
|
-
method: "GET",
|
|
692
|
-
timeoutMs: null,
|
|
693
|
-
signal: opts?.signal,
|
|
694
|
-
params: { cancel_on_disconnect: opts?.cancelOnDisconnect ? "1" : "0" },
|
|
695
|
-
}));
|
|
696
|
-
const stream = (response.body || new ReadableStream({ start: (ctrl) => ctrl.close() }))
|
|
697
|
-
.pipeThrough(new BytesLineDecoder())
|
|
698
|
-
.pipeThrough(new SSEDecoder());
|
|
699
|
-
yield* IterableReadableStream.fromReadableStream(stream);
|
|
700
|
-
}
|
|
701
|
-
/**
|
|
702
|
-
* Delete a run.
|
|
703
|
-
*
|
|
704
|
-
* @param threadId The ID of the thread.
|
|
705
|
-
* @param runId The ID of the run.
|
|
706
|
-
* @returns
|
|
707
|
-
*/
|
|
708
|
-
async delete(threadId, runId) {
|
|
709
|
-
return this.fetch(`/threads/${threadId}/runs/${runId}`, {
|
|
710
|
-
method: "DELETE",
|
|
711
|
-
});
|
|
712
|
-
}
|
|
713
|
-
}
|
|
714
|
-
export class StoreClient extends BaseClient {
|
|
715
|
-
/**
|
|
716
|
-
* Store or update an item.
|
|
717
|
-
*
|
|
718
|
-
* @param namespace A list of strings representing the namespace path.
|
|
719
|
-
* @param key The unique identifier for the item within the namespace.
|
|
720
|
-
* @param value A dictionary containing the item's data.
|
|
721
|
-
* @returns Promise<void>
|
|
722
|
-
*/
|
|
723
|
-
async putItem(namespace, key, value) {
|
|
724
|
-
namespace.forEach((label) => {
|
|
725
|
-
if (label.includes(".")) {
|
|
726
|
-
throw new Error(`Invalid namespace label '${label}'. Namespace labels cannot contain periods ('.')`);
|
|
727
|
-
}
|
|
728
|
-
});
|
|
729
|
-
const payload = {
|
|
730
|
-
namespace,
|
|
731
|
-
key,
|
|
732
|
-
value,
|
|
733
|
-
};
|
|
734
|
-
return this.fetch("/store/items", {
|
|
735
|
-
method: "PUT",
|
|
736
|
-
json: payload,
|
|
737
|
-
});
|
|
738
|
-
}
|
|
739
|
-
/**
|
|
740
|
-
* Retrieve a single item.
|
|
741
|
-
*
|
|
742
|
-
* @param namespace A list of strings representing the namespace path.
|
|
743
|
-
* @param key The unique identifier for the item.
|
|
744
|
-
* @returns Promise<Item>
|
|
745
|
-
*/
|
|
746
|
-
async getItem(namespace, key) {
|
|
747
|
-
namespace.forEach((label) => {
|
|
748
|
-
if (label.includes(".")) {
|
|
749
|
-
throw new Error(`Invalid namespace label '${label}'. Namespace labels cannot contain periods ('.')`);
|
|
750
|
-
}
|
|
751
|
-
});
|
|
752
|
-
const response = await this.fetch("/store/items", {
|
|
753
|
-
params: { namespace: namespace.join("."), key },
|
|
754
|
-
});
|
|
755
|
-
return response
|
|
756
|
-
? {
|
|
757
|
-
...response,
|
|
758
|
-
createdAt: response.created_at,
|
|
759
|
-
updatedAt: response.updated_at,
|
|
760
|
-
}
|
|
761
|
-
: null;
|
|
762
|
-
}
|
|
763
|
-
/**
|
|
764
|
-
* Delete an item.
|
|
765
|
-
*
|
|
766
|
-
* @param namespace A list of strings representing the namespace path.
|
|
767
|
-
* @param key The unique identifier for the item.
|
|
768
|
-
* @returns Promise<void>
|
|
769
|
-
*/
|
|
770
|
-
async deleteItem(namespace, key) {
|
|
771
|
-
namespace.forEach((label) => {
|
|
772
|
-
if (label.includes(".")) {
|
|
773
|
-
throw new Error(`Invalid namespace label '${label}'. Namespace labels cannot contain periods ('.')`);
|
|
774
|
-
}
|
|
775
|
-
});
|
|
776
|
-
return this.fetch("/store/items", {
|
|
777
|
-
method: "DELETE",
|
|
778
|
-
json: { namespace, key },
|
|
779
|
-
});
|
|
780
|
-
}
|
|
781
|
-
/**
|
|
782
|
-
* Search for items within a namespace prefix.
|
|
783
|
-
*
|
|
784
|
-
* @param namespacePrefix List of strings representing the namespace prefix.
|
|
785
|
-
* @param options.filter Optional dictionary of key-value pairs to filter results.
|
|
786
|
-
* @param options.limit Maximum number of items to return (default is 10).
|
|
787
|
-
* @param options.offset Number of items to skip before returning results (default is 0).
|
|
788
|
-
* @param options.query Optional search query.
|
|
789
|
-
* @returns Promise<SearchItemsResponse>
|
|
790
|
-
*/
|
|
791
|
-
async searchItems(namespacePrefix, options) {
|
|
792
|
-
const payload = {
|
|
793
|
-
namespace_prefix: namespacePrefix,
|
|
794
|
-
filter: options?.filter,
|
|
795
|
-
limit: options?.limit ?? 10,
|
|
796
|
-
offset: options?.offset ?? 0,
|
|
797
|
-
query: options?.query,
|
|
798
|
-
};
|
|
799
|
-
const response = await this.fetch("/store/items/search", {
|
|
800
|
-
method: "POST",
|
|
801
|
-
json: payload,
|
|
802
|
-
});
|
|
803
|
-
return {
|
|
804
|
-
items: response.items.map((item) => ({
|
|
805
|
-
...item,
|
|
806
|
-
createdAt: item.created_at,
|
|
807
|
-
updatedAt: item.updated_at,
|
|
808
|
-
})),
|
|
809
|
-
};
|
|
810
|
-
}
|
|
811
|
-
/**
|
|
812
|
-
* List namespaces with optional match conditions.
|
|
813
|
-
*
|
|
814
|
-
* @param options.prefix Optional list of strings representing the prefix to filter namespaces.
|
|
815
|
-
* @param options.suffix Optional list of strings representing the suffix to filter namespaces.
|
|
816
|
-
* @param options.maxDepth Optional integer specifying the maximum depth of namespaces to return.
|
|
817
|
-
* @param options.limit Maximum number of namespaces to return (default is 100).
|
|
818
|
-
* @param options.offset Number of namespaces to skip before returning results (default is 0).
|
|
819
|
-
* @returns Promise<ListNamespaceResponse>
|
|
820
|
-
*/
|
|
821
|
-
async listNamespaces(options) {
|
|
822
|
-
const payload = {
|
|
823
|
-
prefix: options?.prefix,
|
|
824
|
-
suffix: options?.suffix,
|
|
825
|
-
max_depth: options?.maxDepth,
|
|
826
|
-
limit: options?.limit ?? 100,
|
|
827
|
-
offset: options?.offset ?? 0,
|
|
828
|
-
};
|
|
829
|
-
return this.fetch("/store/namespaces", {
|
|
830
|
-
method: "POST",
|
|
831
|
-
json: payload,
|
|
832
|
-
});
|
|
833
|
-
}
|
|
834
|
-
}
|
|
835
|
-
export class Client {
|
|
836
|
-
constructor(config) {
|
|
837
|
-
/**
|
|
838
|
-
* The client for interacting with assistants.
|
|
839
|
-
*/
|
|
840
|
-
Object.defineProperty(this, "assistants", {
|
|
841
|
-
enumerable: true,
|
|
842
|
-
configurable: true,
|
|
843
|
-
writable: true,
|
|
844
|
-
value: void 0
|
|
845
|
-
});
|
|
846
|
-
/**
|
|
847
|
-
* The client for interacting with threads.
|
|
848
|
-
*/
|
|
849
|
-
Object.defineProperty(this, "threads", {
|
|
850
|
-
enumerable: true,
|
|
851
|
-
configurable: true,
|
|
852
|
-
writable: true,
|
|
853
|
-
value: void 0
|
|
854
|
-
});
|
|
855
|
-
/**
|
|
856
|
-
* The client for interacting with runs.
|
|
857
|
-
*/
|
|
858
|
-
Object.defineProperty(this, "runs", {
|
|
859
|
-
enumerable: true,
|
|
860
|
-
configurable: true,
|
|
861
|
-
writable: true,
|
|
862
|
-
value: void 0
|
|
863
|
-
});
|
|
864
|
-
/**
|
|
865
|
-
* The client for interacting with cron runs.
|
|
866
|
-
*/
|
|
867
|
-
Object.defineProperty(this, "crons", {
|
|
868
|
-
enumerable: true,
|
|
869
|
-
configurable: true,
|
|
870
|
-
writable: true,
|
|
871
|
-
value: void 0
|
|
872
|
-
});
|
|
873
|
-
/**
|
|
874
|
-
* The client for interacting with the KV store.
|
|
875
|
-
*/
|
|
876
|
-
Object.defineProperty(this, "store", {
|
|
877
|
-
enumerable: true,
|
|
878
|
-
configurable: true,
|
|
879
|
-
writable: true,
|
|
880
|
-
value: void 0
|
|
881
|
-
});
|
|
882
|
-
this.assistants = new AssistantsClient(config);
|
|
883
|
-
this.threads = new ThreadsClient(config);
|
|
884
|
-
this.runs = new RunsClient(config);
|
|
885
|
-
this.crons = new CronsClient(config);
|
|
886
|
-
this.store = new StoreClient(config);
|
|
887
|
-
}
|
|
888
|
-
}
|