@langchain/langgraph-sdk 0.0.9 → 0.0.10-rc.0
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/client.d.ts +1 -1
- package/client.js +1 -1
- package/dist/client.cjs +677 -0
- package/dist/{client.d.mts → client.d.ts} +2 -2
- package/dist/{client.mjs → client.js} +3 -3
- package/dist/index.cjs +5 -0
- package/dist/{index.d.mts → index.d.ts} +1 -1
- package/dist/index.js +1 -0
- package/dist/types.cjs +2 -0
- package/dist/utils/async_caller.cjs +195 -0
- package/dist/utils/eventsource-parser/index.cjs +7 -0
- package/dist/utils/eventsource-parser/index.d.ts +2 -0
- package/dist/utils/eventsource-parser/index.js +3 -0
- package/dist/utils/eventsource-parser/parse.cjs +150 -0
- package/dist/utils/eventsource-parser/parse.d.ts +18 -0
- package/dist/utils/eventsource-parser/parse.js +146 -0
- package/dist/utils/eventsource-parser/stream.cjs +34 -0
- package/dist/utils/eventsource-parser/stream.d.ts +17 -0
- package/dist/utils/eventsource-parser/stream.js +30 -0
- package/dist/utils/eventsource-parser/types.cjs +2 -0
- package/dist/utils/eventsource-parser/types.d.ts +81 -0
- package/dist/utils/eventsource-parser/types.js +1 -0
- package/dist/utils/stream.cjs +115 -0
- package/dist/utils/{stream.d.mts → stream.d.ts} +1 -0
- package/dist/utils/{stream.mjs → stream.js} +5 -0
- package/index.d.ts +1 -1
- package/index.js +1 -1
- package/package.json +1 -2
- package/dist/index.mjs +0 -1
- /package/dist/{types.d.mts → types.d.ts} +0 -0
- /package/dist/{types.mjs → types.js} +0 -0
- /package/dist/utils/{async_caller.d.mts → async_caller.d.ts} +0 -0
- /package/dist/utils/{async_caller.mjs → async_caller.js} +0 -0
package/client.d.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export * from './dist/client.
|
|
1
|
+
export * from './dist/client.js'
|
package/client.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export * from './dist/client.
|
|
1
|
+
export * from './dist/client.js'
|
package/dist/client.cjs
ADDED
|
@@ -0,0 +1,677 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Client = exports.RunsClient = exports.ThreadsClient = exports.AssistantsClient = exports.CronsClient = void 0;
|
|
4
|
+
const async_caller_js_1 = require("./utils/async_caller.cjs");
|
|
5
|
+
const index_js_1 = require("./utils/eventsource-parser/index.cjs");
|
|
6
|
+
const stream_js_1 = require("./utils/stream.cjs");
|
|
7
|
+
class BaseClient {
|
|
8
|
+
constructor(config) {
|
|
9
|
+
Object.defineProperty(this, "asyncCaller", {
|
|
10
|
+
enumerable: true,
|
|
11
|
+
configurable: true,
|
|
12
|
+
writable: true,
|
|
13
|
+
value: void 0
|
|
14
|
+
});
|
|
15
|
+
Object.defineProperty(this, "timeoutMs", {
|
|
16
|
+
enumerable: true,
|
|
17
|
+
configurable: true,
|
|
18
|
+
writable: true,
|
|
19
|
+
value: void 0
|
|
20
|
+
});
|
|
21
|
+
Object.defineProperty(this, "apiUrl", {
|
|
22
|
+
enumerable: true,
|
|
23
|
+
configurable: true,
|
|
24
|
+
writable: true,
|
|
25
|
+
value: void 0
|
|
26
|
+
});
|
|
27
|
+
Object.defineProperty(this, "defaultHeaders", {
|
|
28
|
+
enumerable: true,
|
|
29
|
+
configurable: true,
|
|
30
|
+
writable: true,
|
|
31
|
+
value: void 0
|
|
32
|
+
});
|
|
33
|
+
this.asyncCaller = new async_caller_js_1.AsyncCaller({
|
|
34
|
+
maxRetries: 4,
|
|
35
|
+
maxConcurrency: 4,
|
|
36
|
+
...config?.callerOptions,
|
|
37
|
+
});
|
|
38
|
+
this.timeoutMs = config?.timeoutMs || 12_000;
|
|
39
|
+
this.apiUrl = config?.apiUrl || "http://localhost:8123";
|
|
40
|
+
this.defaultHeaders = config?.defaultHeaders || {};
|
|
41
|
+
if (config?.apiKey != null) {
|
|
42
|
+
this.defaultHeaders["X-Api-Key"] = config.apiKey;
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
prepareFetchOptions(path, options) {
|
|
46
|
+
const mutatedOptions = {
|
|
47
|
+
...options,
|
|
48
|
+
headers: { ...this.defaultHeaders, ...options?.headers },
|
|
49
|
+
};
|
|
50
|
+
if (mutatedOptions.json) {
|
|
51
|
+
mutatedOptions.body = JSON.stringify(mutatedOptions.json);
|
|
52
|
+
mutatedOptions.headers = {
|
|
53
|
+
...mutatedOptions.headers,
|
|
54
|
+
"Content-Type": "application/json",
|
|
55
|
+
};
|
|
56
|
+
delete mutatedOptions.json;
|
|
57
|
+
}
|
|
58
|
+
const targetUrl = new URL(`${this.apiUrl}${path}`);
|
|
59
|
+
if (mutatedOptions.params) {
|
|
60
|
+
for (const [key, value] of Object.entries(mutatedOptions.params)) {
|
|
61
|
+
if (value == null)
|
|
62
|
+
continue;
|
|
63
|
+
let strValue = typeof value === "string" || typeof value === "number"
|
|
64
|
+
? value.toString()
|
|
65
|
+
: JSON.stringify(value);
|
|
66
|
+
targetUrl.searchParams.append(key, strValue);
|
|
67
|
+
}
|
|
68
|
+
delete mutatedOptions.params;
|
|
69
|
+
}
|
|
70
|
+
return [targetUrl, mutatedOptions];
|
|
71
|
+
}
|
|
72
|
+
async fetch(path, options) {
|
|
73
|
+
const response = await this.asyncCaller.fetch(...this.prepareFetchOptions(path, options));
|
|
74
|
+
if (response.status === 202 || response.status === 204) {
|
|
75
|
+
return undefined;
|
|
76
|
+
}
|
|
77
|
+
return response.json();
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
class CronsClient extends BaseClient {
|
|
81
|
+
/**
|
|
82
|
+
*
|
|
83
|
+
* @param threadId The ID of the thread.
|
|
84
|
+
* @param assistantId Assistant ID to use for this cron job.
|
|
85
|
+
* @param payload Payload for creating a cron job.
|
|
86
|
+
* @returns The created background run.
|
|
87
|
+
*/
|
|
88
|
+
async createForThread(threadId, assistantId, payload) {
|
|
89
|
+
const json = {
|
|
90
|
+
schedule: payload?.schedule,
|
|
91
|
+
input: payload?.input,
|
|
92
|
+
config: payload?.config,
|
|
93
|
+
metadata: payload?.metadata,
|
|
94
|
+
assistant_id: assistantId,
|
|
95
|
+
interrupt_before: payload?.interruptBefore,
|
|
96
|
+
interrupt_after: payload?.interruptAfter,
|
|
97
|
+
webhook: payload?.webhook,
|
|
98
|
+
multitask_strategy: payload?.multitaskStrategy,
|
|
99
|
+
};
|
|
100
|
+
return this.fetch(`/threads/${threadId}/runs/crons`, {
|
|
101
|
+
method: "POST",
|
|
102
|
+
json,
|
|
103
|
+
});
|
|
104
|
+
}
|
|
105
|
+
/**
|
|
106
|
+
*
|
|
107
|
+
* @param assistantId Assistant ID to use for this cron job.
|
|
108
|
+
* @param payload Payload for creating a cron job.
|
|
109
|
+
* @returns
|
|
110
|
+
*/
|
|
111
|
+
async create(assistantId, payload) {
|
|
112
|
+
const json = {
|
|
113
|
+
schedule: payload?.schedule,
|
|
114
|
+
input: payload?.input,
|
|
115
|
+
config: payload?.config,
|
|
116
|
+
metadata: payload?.metadata,
|
|
117
|
+
assistant_id: assistantId,
|
|
118
|
+
interrupt_before: payload?.interruptBefore,
|
|
119
|
+
interrupt_after: payload?.interruptAfter,
|
|
120
|
+
webhook: payload?.webhook,
|
|
121
|
+
multitask_strategy: payload?.multitaskStrategy,
|
|
122
|
+
};
|
|
123
|
+
return this.fetch(`/runs/crons`, {
|
|
124
|
+
method: "POST",
|
|
125
|
+
json,
|
|
126
|
+
});
|
|
127
|
+
}
|
|
128
|
+
/**
|
|
129
|
+
*
|
|
130
|
+
* @param cronId Cron ID of Cron job to delete.
|
|
131
|
+
*/
|
|
132
|
+
async delete(cronId) {
|
|
133
|
+
await this.fetch(`/runs/crons/${cronId}`, {
|
|
134
|
+
method: "DELETE",
|
|
135
|
+
});
|
|
136
|
+
}
|
|
137
|
+
/**
|
|
138
|
+
*
|
|
139
|
+
* @param query Query options.
|
|
140
|
+
* @returns List of crons.
|
|
141
|
+
*/
|
|
142
|
+
async search(query) {
|
|
143
|
+
return this.fetch("/runs/crons/search", {
|
|
144
|
+
method: "POST",
|
|
145
|
+
json: {
|
|
146
|
+
assistant_id: query?.assistantId ?? undefined,
|
|
147
|
+
thread_id: query?.threadId ?? undefined,
|
|
148
|
+
limit: query?.limit ?? 10,
|
|
149
|
+
offset: query?.offset ?? 0,
|
|
150
|
+
},
|
|
151
|
+
});
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
exports.CronsClient = CronsClient;
|
|
155
|
+
class AssistantsClient extends BaseClient {
|
|
156
|
+
/**
|
|
157
|
+
* Get an assistant by ID.
|
|
158
|
+
*
|
|
159
|
+
* @param assistantId The ID of the assistant.
|
|
160
|
+
* @returns Assistant
|
|
161
|
+
*/
|
|
162
|
+
async get(assistantId) {
|
|
163
|
+
return this.fetch(`/assistants/${assistantId}`);
|
|
164
|
+
}
|
|
165
|
+
/**
|
|
166
|
+
* Get the JSON representation of the graph assigned to a runnable
|
|
167
|
+
* @param assistantId The ID of the assistant.
|
|
168
|
+
* @returns Serialized graph
|
|
169
|
+
*/
|
|
170
|
+
async getGraph(assistantId) {
|
|
171
|
+
return this.fetch(`/assistants/${assistantId}/graph`);
|
|
172
|
+
}
|
|
173
|
+
/**
|
|
174
|
+
* Get the state and config schema of the graph assigned to a runnable
|
|
175
|
+
* @param assistantId The ID of the assistant.
|
|
176
|
+
* @returns Graph schema
|
|
177
|
+
*/
|
|
178
|
+
async getSchemas(assistantId) {
|
|
179
|
+
return this.fetch(`/assistants/${assistantId}/schemas`);
|
|
180
|
+
}
|
|
181
|
+
/**
|
|
182
|
+
* Create a new assistant.
|
|
183
|
+
* @param payload Payload for creating an assistant.
|
|
184
|
+
* @returns The created assistant.
|
|
185
|
+
*/
|
|
186
|
+
async create(payload) {
|
|
187
|
+
return this.fetch("/assistants", {
|
|
188
|
+
method: "POST",
|
|
189
|
+
json: {
|
|
190
|
+
graph_id: payload.graphId,
|
|
191
|
+
config: payload.config,
|
|
192
|
+
metadata: payload.metadata,
|
|
193
|
+
assistant_id: payload.assistantId,
|
|
194
|
+
if_exists: payload.ifExists,
|
|
195
|
+
},
|
|
196
|
+
});
|
|
197
|
+
}
|
|
198
|
+
/**
|
|
199
|
+
* Update an assistant.
|
|
200
|
+
* @param assistantId ID of the assistant.
|
|
201
|
+
* @param payload Payload for updating the assistant.
|
|
202
|
+
* @returns The updated assistant.
|
|
203
|
+
*/
|
|
204
|
+
async update(assistantId, payload) {
|
|
205
|
+
return this.fetch(`/assistants/${assistantId}`, {
|
|
206
|
+
method: "PATCH",
|
|
207
|
+
json: {
|
|
208
|
+
graph_id: payload.graphId,
|
|
209
|
+
config: payload.config,
|
|
210
|
+
metadata: payload.metadata,
|
|
211
|
+
},
|
|
212
|
+
});
|
|
213
|
+
}
|
|
214
|
+
/**
|
|
215
|
+
* Delete an assistant.
|
|
216
|
+
*
|
|
217
|
+
* @param assistantId ID of the assistant.
|
|
218
|
+
*/
|
|
219
|
+
async delete(assistantId) {
|
|
220
|
+
return this.fetch(`/assistants/${assistantId}`, {
|
|
221
|
+
method: "DELETE",
|
|
222
|
+
});
|
|
223
|
+
}
|
|
224
|
+
/**
|
|
225
|
+
* List assistants.
|
|
226
|
+
* @param query Query options.
|
|
227
|
+
* @returns List of assistants.
|
|
228
|
+
*/
|
|
229
|
+
async search(query) {
|
|
230
|
+
return this.fetch("/assistants/search", {
|
|
231
|
+
method: "POST",
|
|
232
|
+
json: {
|
|
233
|
+
graph_id: query?.graphId ?? undefined,
|
|
234
|
+
metadata: query?.metadata ?? undefined,
|
|
235
|
+
limit: query?.limit ?? 10,
|
|
236
|
+
offset: query?.offset ?? 0,
|
|
237
|
+
},
|
|
238
|
+
});
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
exports.AssistantsClient = AssistantsClient;
|
|
242
|
+
class ThreadsClient extends BaseClient {
|
|
243
|
+
/**
|
|
244
|
+
* Get a thread by ID.
|
|
245
|
+
*
|
|
246
|
+
* @param threadId ID of the thread.
|
|
247
|
+
* @returns The thread.
|
|
248
|
+
*/
|
|
249
|
+
async get(threadId) {
|
|
250
|
+
return this.fetch(`/threads/${threadId}`);
|
|
251
|
+
}
|
|
252
|
+
/**
|
|
253
|
+
* Create a new thread.
|
|
254
|
+
*
|
|
255
|
+
* @param payload Payload for creating a thread.
|
|
256
|
+
* @returns The created thread.
|
|
257
|
+
*/
|
|
258
|
+
async create(payload) {
|
|
259
|
+
return this.fetch(`/threads`, {
|
|
260
|
+
method: "POST",
|
|
261
|
+
json: {
|
|
262
|
+
metadata: payload?.metadata,
|
|
263
|
+
thread_id: payload?.threadId,
|
|
264
|
+
if_exists: payload?.ifExists,
|
|
265
|
+
},
|
|
266
|
+
});
|
|
267
|
+
}
|
|
268
|
+
/**
|
|
269
|
+
* Copy an existing thread
|
|
270
|
+
* @param threadId ID of the thread to be copied
|
|
271
|
+
* @returns Newly copied thread
|
|
272
|
+
*/
|
|
273
|
+
async copy(threadId) {
|
|
274
|
+
return this.fetch(`/threads/${threadId}/copy`, {
|
|
275
|
+
method: "POST",
|
|
276
|
+
});
|
|
277
|
+
}
|
|
278
|
+
/**
|
|
279
|
+
* Update a thread.
|
|
280
|
+
*
|
|
281
|
+
* @param threadId ID of the thread.
|
|
282
|
+
* @param payload Payload for updating the thread.
|
|
283
|
+
* @returns The updated thread.
|
|
284
|
+
*/
|
|
285
|
+
async update(threadId, payload) {
|
|
286
|
+
return this.fetch(`/threads/${threadId}`, {
|
|
287
|
+
method: "PATCH",
|
|
288
|
+
json: { metadata: payload?.metadata },
|
|
289
|
+
});
|
|
290
|
+
}
|
|
291
|
+
/**
|
|
292
|
+
* Delete a thread.
|
|
293
|
+
*
|
|
294
|
+
* @param threadId ID of the thread.
|
|
295
|
+
*/
|
|
296
|
+
async delete(threadId) {
|
|
297
|
+
return this.fetch(`/threads/${threadId}`, {
|
|
298
|
+
method: "DELETE",
|
|
299
|
+
});
|
|
300
|
+
}
|
|
301
|
+
/**
|
|
302
|
+
* List threads
|
|
303
|
+
*
|
|
304
|
+
* @param query Query options
|
|
305
|
+
* @returns List of threads
|
|
306
|
+
*/
|
|
307
|
+
async search(query) {
|
|
308
|
+
return this.fetch("/threads/search", {
|
|
309
|
+
method: "POST",
|
|
310
|
+
json: {
|
|
311
|
+
metadata: query?.metadata ?? undefined,
|
|
312
|
+
limit: query?.limit ?? 10,
|
|
313
|
+
offset: query?.offset ?? 0,
|
|
314
|
+
},
|
|
315
|
+
});
|
|
316
|
+
}
|
|
317
|
+
/**
|
|
318
|
+
* Get state for a thread.
|
|
319
|
+
*
|
|
320
|
+
* @param threadId ID of the thread.
|
|
321
|
+
* @returns Thread state.
|
|
322
|
+
*/
|
|
323
|
+
async getState(threadId, checkpointId) {
|
|
324
|
+
return this.fetch(checkpointId != null
|
|
325
|
+
? `/threads/${threadId}/state/${checkpointId}`
|
|
326
|
+
: `/threads/${threadId}/state`);
|
|
327
|
+
}
|
|
328
|
+
/**
|
|
329
|
+
* Add state to a thread.
|
|
330
|
+
*
|
|
331
|
+
* @param threadId The ID of the thread.
|
|
332
|
+
* @returns
|
|
333
|
+
*/
|
|
334
|
+
async updateState(threadId, options) {
|
|
335
|
+
return this.fetch(`/threads/${threadId}/state`, {
|
|
336
|
+
method: "POST",
|
|
337
|
+
json: {
|
|
338
|
+
values: options.values,
|
|
339
|
+
checkpoint_id: options.checkpointId,
|
|
340
|
+
as_node: options?.asNode,
|
|
341
|
+
},
|
|
342
|
+
});
|
|
343
|
+
}
|
|
344
|
+
/**
|
|
345
|
+
* Patch the metadata of a thread.
|
|
346
|
+
*
|
|
347
|
+
* @param threadIdOrConfig Thread ID or config to patch the state of.
|
|
348
|
+
* @param metadata Metadata to patch the state with.
|
|
349
|
+
*/
|
|
350
|
+
async patchState(threadIdOrConfig, metadata) {
|
|
351
|
+
let threadId;
|
|
352
|
+
if (typeof threadIdOrConfig !== "string") {
|
|
353
|
+
if (typeof threadIdOrConfig.configurable.thread_id !== "string") {
|
|
354
|
+
throw new Error("Thread ID is required when updating state with a config.");
|
|
355
|
+
}
|
|
356
|
+
threadId = threadIdOrConfig.configurable.thread_id;
|
|
357
|
+
}
|
|
358
|
+
else {
|
|
359
|
+
threadId = threadIdOrConfig;
|
|
360
|
+
}
|
|
361
|
+
return this.fetch(`/threads/${threadId}/state`, {
|
|
362
|
+
method: "PATCH",
|
|
363
|
+
json: { metadata: metadata },
|
|
364
|
+
});
|
|
365
|
+
}
|
|
366
|
+
/**
|
|
367
|
+
* Get all past states for a thread.
|
|
368
|
+
*
|
|
369
|
+
* @param threadId ID of the thread.
|
|
370
|
+
* @param options Additional options.
|
|
371
|
+
* @returns List of thread states.
|
|
372
|
+
*/
|
|
373
|
+
async getHistory(threadId, options) {
|
|
374
|
+
return this.fetch(`/threads/${threadId}/history`, {
|
|
375
|
+
method: "POST",
|
|
376
|
+
json: {
|
|
377
|
+
limit: options?.limit ?? 10,
|
|
378
|
+
before: options?.before,
|
|
379
|
+
metadata: options?.metadata,
|
|
380
|
+
},
|
|
381
|
+
});
|
|
382
|
+
}
|
|
383
|
+
}
|
|
384
|
+
exports.ThreadsClient = ThreadsClient;
|
|
385
|
+
class RunsClient extends BaseClient {
|
|
386
|
+
/**
|
|
387
|
+
* Create a run and stream the results.
|
|
388
|
+
*
|
|
389
|
+
* @param threadId The ID of the thread.
|
|
390
|
+
* @param assistantId Assistant ID to use for this run.
|
|
391
|
+
* @param payload Payload for creating a run.
|
|
392
|
+
*/
|
|
393
|
+
async *stream(threadId, assistantId, payload) {
|
|
394
|
+
const json = {
|
|
395
|
+
input: payload?.input,
|
|
396
|
+
config: payload?.config,
|
|
397
|
+
metadata: payload?.metadata,
|
|
398
|
+
stream_mode: payload?.streamMode,
|
|
399
|
+
feedback_keys: payload?.feedbackKeys,
|
|
400
|
+
assistant_id: assistantId,
|
|
401
|
+
interrupt_before: payload?.interruptBefore,
|
|
402
|
+
interrupt_after: payload?.interruptAfter,
|
|
403
|
+
checkpoint_id: payload?.checkpointId,
|
|
404
|
+
webhook: payload?.webhook,
|
|
405
|
+
multitask_strategy: payload?.multitaskStrategy,
|
|
406
|
+
on_completion: payload?.onCompletion,
|
|
407
|
+
on_disconnect: payload?.onDisconnect,
|
|
408
|
+
};
|
|
409
|
+
const endpoint = threadId == null ? `/runs/stream` : `/threads/${threadId}/runs/stream`;
|
|
410
|
+
const response = await this.asyncCaller.fetch(...this.prepareFetchOptions(endpoint, {
|
|
411
|
+
method: "POST",
|
|
412
|
+
json,
|
|
413
|
+
signal: payload?.signal,
|
|
414
|
+
}));
|
|
415
|
+
let parser;
|
|
416
|
+
let onEndEvent;
|
|
417
|
+
const textDecoder = new TextDecoder();
|
|
418
|
+
const stream = (response.body || new ReadableStream({ start: (ctrl) => ctrl.close() })).pipeThrough(new TransformStream({
|
|
419
|
+
async start(ctrl) {
|
|
420
|
+
parser = (0, index_js_1.createParser)((event) => {
|
|
421
|
+
if ((payload?.signal && payload.signal.aborted) ||
|
|
422
|
+
(event.type === "event" && event.data === "[DONE]")) {
|
|
423
|
+
ctrl.terminate();
|
|
424
|
+
return;
|
|
425
|
+
}
|
|
426
|
+
if ("data" in event) {
|
|
427
|
+
ctrl.enqueue({
|
|
428
|
+
event: event.event ?? "message",
|
|
429
|
+
data: JSON.parse(event.data),
|
|
430
|
+
});
|
|
431
|
+
}
|
|
432
|
+
});
|
|
433
|
+
onEndEvent = () => {
|
|
434
|
+
ctrl.enqueue({ event: "end", data: undefined });
|
|
435
|
+
};
|
|
436
|
+
},
|
|
437
|
+
async transform(chunk) {
|
|
438
|
+
const payload = textDecoder.decode(chunk);
|
|
439
|
+
parser.feed(payload);
|
|
440
|
+
// eventsource-parser will ignore events
|
|
441
|
+
// that are not terminated by a newline
|
|
442
|
+
if (payload.trim() === "event: end")
|
|
443
|
+
onEndEvent();
|
|
444
|
+
},
|
|
445
|
+
}));
|
|
446
|
+
yield* stream_js_1.IterableReadableStream.fromReadableStream(stream);
|
|
447
|
+
}
|
|
448
|
+
/**
|
|
449
|
+
* Create a run.
|
|
450
|
+
*
|
|
451
|
+
* @param threadId The ID of the thread.
|
|
452
|
+
* @param assistantId Assistant ID to use for this run.
|
|
453
|
+
* @param payload Payload for creating a run.
|
|
454
|
+
* @returns The created run.
|
|
455
|
+
*/
|
|
456
|
+
async create(threadId, assistantId, payload) {
|
|
457
|
+
const json = {
|
|
458
|
+
input: payload?.input,
|
|
459
|
+
config: payload?.config,
|
|
460
|
+
metadata: payload?.metadata,
|
|
461
|
+
assistant_id: assistantId,
|
|
462
|
+
interrupt_before: payload?.interruptBefore,
|
|
463
|
+
interrupt_after: payload?.interruptAfter,
|
|
464
|
+
webhook: payload?.webhook,
|
|
465
|
+
checkpoint_id: payload?.checkpointId,
|
|
466
|
+
multitask_strategy: payload?.multitaskStrategy,
|
|
467
|
+
};
|
|
468
|
+
return this.fetch(`/threads/${threadId}/runs`, {
|
|
469
|
+
method: "POST",
|
|
470
|
+
json,
|
|
471
|
+
signal: payload?.signal,
|
|
472
|
+
});
|
|
473
|
+
}
|
|
474
|
+
/**
|
|
475
|
+
* Create a batch of stateless background runs.
|
|
476
|
+
*
|
|
477
|
+
* @param payloads An array of payloads for creating runs.
|
|
478
|
+
* @returns An array of created runs.
|
|
479
|
+
*/
|
|
480
|
+
async createBatch(payloads) {
|
|
481
|
+
const filteredPayloads = payloads
|
|
482
|
+
.map((payload) => ({ ...payload, assistant_id: payload.assistantId }))
|
|
483
|
+
.map((payload) => {
|
|
484
|
+
return Object.fromEntries(Object.entries(payload).filter(([_, v]) => v !== undefined));
|
|
485
|
+
});
|
|
486
|
+
return this.fetch("/runs/batch", {
|
|
487
|
+
method: "POST",
|
|
488
|
+
json: filteredPayloads,
|
|
489
|
+
});
|
|
490
|
+
}
|
|
491
|
+
/**
|
|
492
|
+
* Create a run and wait for it to complete.
|
|
493
|
+
*
|
|
494
|
+
* @param threadId The ID of the thread.
|
|
495
|
+
* @param assistantId Assistant ID to use for this run.
|
|
496
|
+
* @param payload Payload for creating a run.
|
|
497
|
+
* @returns The last values chunk of the thread.
|
|
498
|
+
*/
|
|
499
|
+
async wait(threadId, assistantId, payload) {
|
|
500
|
+
const json = {
|
|
501
|
+
input: payload?.input,
|
|
502
|
+
config: payload?.config,
|
|
503
|
+
metadata: payload?.metadata,
|
|
504
|
+
assistant_id: assistantId,
|
|
505
|
+
interrupt_before: payload?.interruptBefore,
|
|
506
|
+
interrupt_after: payload?.interruptAfter,
|
|
507
|
+
checkpoint_id: payload?.checkpointId,
|
|
508
|
+
webhook: payload?.webhook,
|
|
509
|
+
multitask_strategy: payload?.multitaskStrategy,
|
|
510
|
+
on_completion: payload?.onCompletion,
|
|
511
|
+
on_disconnect: payload?.onDisconnect,
|
|
512
|
+
};
|
|
513
|
+
const endpoint = threadId == null ? `/runs/wait` : `/threads/${threadId}/runs/wait`;
|
|
514
|
+
return this.fetch(endpoint, {
|
|
515
|
+
method: "POST",
|
|
516
|
+
json,
|
|
517
|
+
signal: payload?.signal,
|
|
518
|
+
});
|
|
519
|
+
}
|
|
520
|
+
/**
|
|
521
|
+
* List all runs for a thread.
|
|
522
|
+
*
|
|
523
|
+
* @param threadId The ID of the thread.
|
|
524
|
+
* @param options Filtering and pagination options.
|
|
525
|
+
* @returns List of runs.
|
|
526
|
+
*/
|
|
527
|
+
async list(threadId, options) {
|
|
528
|
+
return this.fetch(`/threads/${threadId}/runs`, {
|
|
529
|
+
params: {
|
|
530
|
+
limit: options?.limit ?? 10,
|
|
531
|
+
offset: options?.offset ?? 0,
|
|
532
|
+
},
|
|
533
|
+
});
|
|
534
|
+
}
|
|
535
|
+
/**
|
|
536
|
+
* Get a run by ID.
|
|
537
|
+
*
|
|
538
|
+
* @param threadId The ID of the thread.
|
|
539
|
+
* @param runId The ID of the run.
|
|
540
|
+
* @returns The run.
|
|
541
|
+
*/
|
|
542
|
+
async get(threadId, runId) {
|
|
543
|
+
return this.fetch(`/threads/${threadId}/runs/${runId}`);
|
|
544
|
+
}
|
|
545
|
+
/**
|
|
546
|
+
* Cancel a run.
|
|
547
|
+
*
|
|
548
|
+
* @param threadId The ID of the thread.
|
|
549
|
+
* @param runId The ID of the run.
|
|
550
|
+
* @param wait Whether to block when canceling
|
|
551
|
+
* @returns
|
|
552
|
+
*/
|
|
553
|
+
async cancel(threadId, runId, wait = false) {
|
|
554
|
+
return this.fetch(`/threads/${threadId}/runs/${runId}/cancel`, {
|
|
555
|
+
method: "POST",
|
|
556
|
+
params: {
|
|
557
|
+
wait: wait ? "1" : "0",
|
|
558
|
+
},
|
|
559
|
+
});
|
|
560
|
+
}
|
|
561
|
+
/**
|
|
562
|
+
* Block until a run is done.
|
|
563
|
+
*
|
|
564
|
+
* @param threadId The ID of the thread.
|
|
565
|
+
* @param runId The ID of the run.
|
|
566
|
+
* @returns
|
|
567
|
+
*/
|
|
568
|
+
async join(threadId, runId) {
|
|
569
|
+
return this.fetch(`/threads/${threadId}/runs/${runId}/join`);
|
|
570
|
+
}
|
|
571
|
+
/**
|
|
572
|
+
* Stream output from a run in real-time, until the run is done.
|
|
573
|
+
* Output is not buffered, so any output produced before this call will
|
|
574
|
+
* not be received here.
|
|
575
|
+
*
|
|
576
|
+
* @param threadId The ID of the thread.
|
|
577
|
+
* @param runId The ID of the run.
|
|
578
|
+
* @param signal An optional abort signal.
|
|
579
|
+
* @returns An async generator yielding stream parts.
|
|
580
|
+
*/
|
|
581
|
+
async *joinStream(threadId, runId, signal) {
|
|
582
|
+
const response = await this.asyncCaller.fetch(...this.prepareFetchOptions(`/threads/${threadId}/runs/${runId}/stream`, {
|
|
583
|
+
method: "GET",
|
|
584
|
+
signal,
|
|
585
|
+
}));
|
|
586
|
+
let parser;
|
|
587
|
+
let onEndEvent;
|
|
588
|
+
const textDecoder = new TextDecoder();
|
|
589
|
+
const stream = (response.body || new ReadableStream({ start: (ctrl) => ctrl.close() })).pipeThrough(new TransformStream({
|
|
590
|
+
async start(ctrl) {
|
|
591
|
+
parser = (0, index_js_1.createParser)((event) => {
|
|
592
|
+
if ((signal && signal.aborted) ||
|
|
593
|
+
(event.type === "event" && event.data === "[DONE]")) {
|
|
594
|
+
ctrl.terminate();
|
|
595
|
+
return;
|
|
596
|
+
}
|
|
597
|
+
if ("data" in event) {
|
|
598
|
+
ctrl.enqueue({
|
|
599
|
+
event: event.event ?? "message",
|
|
600
|
+
data: JSON.parse(event.data),
|
|
601
|
+
});
|
|
602
|
+
}
|
|
603
|
+
});
|
|
604
|
+
onEndEvent = () => {
|
|
605
|
+
ctrl.enqueue({ event: "end", data: undefined });
|
|
606
|
+
};
|
|
607
|
+
},
|
|
608
|
+
async transform(chunk) {
|
|
609
|
+
const payload = textDecoder.decode(chunk);
|
|
610
|
+
parser.feed(payload);
|
|
611
|
+
// eventsource-parser will ignore events
|
|
612
|
+
// that are not terminated by a newline
|
|
613
|
+
if (payload.trim() === "event: end")
|
|
614
|
+
onEndEvent();
|
|
615
|
+
},
|
|
616
|
+
}));
|
|
617
|
+
yield* stream_js_1.IterableReadableStream.fromReadableStream(stream);
|
|
618
|
+
}
|
|
619
|
+
/**
|
|
620
|
+
* Delete a run.
|
|
621
|
+
*
|
|
622
|
+
* @param threadId The ID of the thread.
|
|
623
|
+
* @param runId The ID of the run.
|
|
624
|
+
* @returns
|
|
625
|
+
*/
|
|
626
|
+
async delete(threadId, runId) {
|
|
627
|
+
return this.fetch(`/threads/${threadId}/runs/${runId}`, {
|
|
628
|
+
method: "DELETE",
|
|
629
|
+
});
|
|
630
|
+
}
|
|
631
|
+
}
|
|
632
|
+
exports.RunsClient = RunsClient;
|
|
633
|
+
class Client {
|
|
634
|
+
constructor(config) {
|
|
635
|
+
/**
|
|
636
|
+
* The client for interacting with assistants.
|
|
637
|
+
*/
|
|
638
|
+
Object.defineProperty(this, "assistants", {
|
|
639
|
+
enumerable: true,
|
|
640
|
+
configurable: true,
|
|
641
|
+
writable: true,
|
|
642
|
+
value: void 0
|
|
643
|
+
});
|
|
644
|
+
/**
|
|
645
|
+
* The client for interacting with threads.
|
|
646
|
+
*/
|
|
647
|
+
Object.defineProperty(this, "threads", {
|
|
648
|
+
enumerable: true,
|
|
649
|
+
configurable: true,
|
|
650
|
+
writable: true,
|
|
651
|
+
value: void 0
|
|
652
|
+
});
|
|
653
|
+
/**
|
|
654
|
+
* The client for interacting with runs.
|
|
655
|
+
*/
|
|
656
|
+
Object.defineProperty(this, "runs", {
|
|
657
|
+
enumerable: true,
|
|
658
|
+
configurable: true,
|
|
659
|
+
writable: true,
|
|
660
|
+
value: void 0
|
|
661
|
+
});
|
|
662
|
+
/**
|
|
663
|
+
* The client for interacting with cron runs.
|
|
664
|
+
*/
|
|
665
|
+
Object.defineProperty(this, "crons", {
|
|
666
|
+
enumerable: true,
|
|
667
|
+
configurable: true,
|
|
668
|
+
writable: true,
|
|
669
|
+
value: void 0
|
|
670
|
+
});
|
|
671
|
+
this.assistants = new AssistantsClient(config);
|
|
672
|
+
this.threads = new ThreadsClient(config);
|
|
673
|
+
this.runs = new RunsClient(config);
|
|
674
|
+
this.crons = new CronsClient(config);
|
|
675
|
+
}
|
|
676
|
+
}
|
|
677
|
+
exports.Client = Client;
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { Assistant, AssistantGraph, Config, DefaultValues, GraphSchema, Metadata, Run, Thread, ThreadState, Cron } from "./schema.js";
|
|
2
|
-
import { AsyncCaller, AsyncCallerParams } from "./utils/async_caller.
|
|
3
|
-
import { RunsCreatePayload, RunsStreamPayload, RunsWaitPayload, StreamEvent, CronsCreatePayload, OnConflictBehavior } from "./types.
|
|
2
|
+
import { AsyncCaller, AsyncCallerParams } from "./utils/async_caller.js";
|
|
3
|
+
import { RunsCreatePayload, RunsStreamPayload, RunsWaitPayload, StreamEvent, CronsCreatePayload, OnConflictBehavior } from "./types.js";
|
|
4
4
|
interface ClientConfig {
|
|
5
5
|
apiUrl?: string;
|
|
6
6
|
apiKey?: string;
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { AsyncCaller } from "./utils/async_caller.
|
|
2
|
-
import { createParser } from "eventsource-parser";
|
|
3
|
-
import { IterableReadableStream } from "./utils/stream.
|
|
1
|
+
import { AsyncCaller } from "./utils/async_caller.js";
|
|
2
|
+
import { createParser, } from "./utils/eventsource-parser/index.js";
|
|
3
|
+
import { IterableReadableStream } from "./utils/stream.js";
|
|
4
4
|
class BaseClient {
|
|
5
5
|
constructor(config) {
|
|
6
6
|
Object.defineProperty(this, "asyncCaller", {
|