@langchain/langgraph-sdk 0.0.1-rc.0 → 0.0.1-rc.2
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/client.d.mts +195 -40
- package/dist/client.mjs +179 -37
- package/dist/schema.d.ts +18 -5
- package/dist/types.d.mts +44 -0
- package/dist/types.mjs +1 -0
- package/dist/utils/async_caller.d.mts +2 -2
- package/dist/utils/async_caller.mjs +60 -16
- package/dist/utils/stream.d.mts +2 -1
- package/package.json +1 -1
package/dist/client.d.mts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { Assistant, AssistantGraph, Config, GraphSchema, Metadata, Run, RunEvent, Thread, ThreadState } from "./schema.js";
|
|
1
|
+
import { Assistant, AssistantGraph, Config, DefaultValues, GraphSchema, Metadata, Run, RunEvent, Thread, ThreadState } from "./schema.js";
|
|
2
2
|
import { AsyncCaller, AsyncCallerParams } from "./utils/async_caller.mjs";
|
|
3
|
-
import {
|
|
3
|
+
import { RunsCreatePayload, RunsStreamPayload, RunsWaitPayload } from "./types.mjs";
|
|
4
4
|
interface ClientConfig {
|
|
5
5
|
apiUrl?: string;
|
|
6
6
|
callerOptions?: AsyncCallerParams;
|
|
@@ -21,90 +21,245 @@ declare class BaseClient {
|
|
|
21
21
|
}): Promise<T>;
|
|
22
22
|
}
|
|
23
23
|
declare class AssistantsClient extends BaseClient {
|
|
24
|
+
/**
|
|
25
|
+
* Get an assistant by ID.
|
|
26
|
+
*
|
|
27
|
+
* @param assistantId The ID of the assistant.
|
|
28
|
+
* @returns Assistant
|
|
29
|
+
*/
|
|
24
30
|
get(assistantId: string): Promise<Assistant>;
|
|
31
|
+
/**
|
|
32
|
+
* Get the JSON representation of the graph assigned to a runnable
|
|
33
|
+
* @param assistantId The ID of the assistant.
|
|
34
|
+
* @returns Serialized graph
|
|
35
|
+
*/
|
|
25
36
|
getGraph(assistantId: string): Promise<AssistantGraph>;
|
|
37
|
+
/**
|
|
38
|
+
* Get the state and config schema of the graph assigned to a runnable
|
|
39
|
+
* @param assistantId The ID of the assistant.
|
|
40
|
+
* @returns Graph schema
|
|
41
|
+
*/
|
|
26
42
|
getSchemas(assistantId: string): Promise<GraphSchema>;
|
|
43
|
+
/**
|
|
44
|
+
* Create a new assistant.
|
|
45
|
+
* @param payload Payload for creating an assistant.
|
|
46
|
+
* @returns The created assistant.
|
|
47
|
+
*/
|
|
27
48
|
create(payload: {
|
|
28
|
-
graphId
|
|
49
|
+
graphId: string;
|
|
29
50
|
config?: Config;
|
|
30
51
|
metadata?: Metadata;
|
|
31
52
|
}): Promise<Assistant>;
|
|
53
|
+
/**
|
|
54
|
+
* Update an assistant.
|
|
55
|
+
* @param assistantId ID of the assistant.
|
|
56
|
+
* @param payload Payload for updating the assistant.
|
|
57
|
+
* @returns The updated assistant.
|
|
58
|
+
*/
|
|
32
59
|
upsert(assistantId: string, payload: {
|
|
33
|
-
graphId
|
|
60
|
+
graphId: string;
|
|
34
61
|
config?: Config;
|
|
35
62
|
metadata?: Metadata;
|
|
36
63
|
}): Promise<Assistant>;
|
|
37
|
-
|
|
64
|
+
/**
|
|
65
|
+
* List assistants.
|
|
66
|
+
* @param query Query options.
|
|
67
|
+
* @returns List of assistants.
|
|
68
|
+
*/
|
|
69
|
+
search(query?: {
|
|
38
70
|
metadata?: Metadata;
|
|
39
71
|
limit?: number;
|
|
40
72
|
offset?: number;
|
|
41
73
|
}): Promise<Assistant[]>;
|
|
42
74
|
}
|
|
43
75
|
declare class ThreadsClient extends BaseClient {
|
|
76
|
+
/**
|
|
77
|
+
* Get a thread by ID.
|
|
78
|
+
*
|
|
79
|
+
* @param threadId ID of the thread.
|
|
80
|
+
* @returns The thread.
|
|
81
|
+
*/
|
|
44
82
|
get(threadId: string): Promise<Thread>;
|
|
45
|
-
|
|
83
|
+
/**
|
|
84
|
+
* Create a new thread.
|
|
85
|
+
*
|
|
86
|
+
* @param payload Payload for creating a thread.
|
|
87
|
+
* @returns The created thread.
|
|
88
|
+
*/
|
|
89
|
+
create(payload?: {
|
|
90
|
+
/**
|
|
91
|
+
* Metadata for the thread.
|
|
92
|
+
*/
|
|
46
93
|
metadata?: Metadata;
|
|
47
94
|
}): Promise<Thread>;
|
|
48
|
-
|
|
95
|
+
/**
|
|
96
|
+
* Update a thread.
|
|
97
|
+
*
|
|
98
|
+
* @param threadId ID of the thread.
|
|
99
|
+
* @param payload Payload for updating the thread.
|
|
100
|
+
* @returns The updated thread.
|
|
101
|
+
*/
|
|
102
|
+
upsert(threadId: string, payload?: {
|
|
103
|
+
/**
|
|
104
|
+
* Metadata for the thread.
|
|
105
|
+
*/
|
|
49
106
|
metadata?: Metadata;
|
|
50
107
|
}): Promise<Thread>;
|
|
108
|
+
/**
|
|
109
|
+
* Delete a thread.
|
|
110
|
+
*
|
|
111
|
+
* @param threadId ID of the thread.
|
|
112
|
+
*/
|
|
51
113
|
delete(threadId: string): Promise<void>;
|
|
52
|
-
|
|
114
|
+
/**
|
|
115
|
+
* List threads
|
|
116
|
+
*
|
|
117
|
+
* @param query Query options
|
|
118
|
+
* @returns List of threads
|
|
119
|
+
*/
|
|
120
|
+
search(query?: {
|
|
121
|
+
/**
|
|
122
|
+
* Metadata to filter threads by.
|
|
123
|
+
*/
|
|
53
124
|
metadata?: Metadata;
|
|
125
|
+
/**
|
|
126
|
+
* Maximum number of threads to return.
|
|
127
|
+
* Defaults to 10
|
|
128
|
+
*/
|
|
54
129
|
limit?: number;
|
|
130
|
+
/**
|
|
131
|
+
* Offset to start from.
|
|
132
|
+
*/
|
|
55
133
|
offset?: number;
|
|
56
134
|
}): Promise<Thread[]>;
|
|
57
|
-
|
|
58
|
-
|
|
135
|
+
/**
|
|
136
|
+
* Get state for a thread.
|
|
137
|
+
*
|
|
138
|
+
* @param threadId ID of the thread.
|
|
139
|
+
* @returns Thread state.
|
|
140
|
+
*/
|
|
141
|
+
getState<ValuesType = DefaultValues>(threadId: string): Promise<ThreadState<ValuesType>>;
|
|
142
|
+
/**
|
|
143
|
+
* Add state to a thread.
|
|
144
|
+
*
|
|
145
|
+
* @param threadIdOrConfig Thread ID or config that identifies the state to start from.
|
|
146
|
+
* @param values The state update
|
|
147
|
+
* @param options Additional options.
|
|
148
|
+
* @returns
|
|
149
|
+
*/
|
|
150
|
+
updateState<ValuesType = DefaultValues>(threadIdOrConfig: string | Config, options: {
|
|
151
|
+
values: ValuesType;
|
|
59
152
|
asNode?: string;
|
|
60
153
|
}): Promise<void>;
|
|
61
|
-
|
|
62
|
-
|
|
154
|
+
/**
|
|
155
|
+
* Patch the metadata of a thread.
|
|
156
|
+
*
|
|
157
|
+
* @param threadIdOrConfig Thread ID or config to patch the state of.
|
|
158
|
+
* @param metadata Metadata to patch the state with.
|
|
159
|
+
*/
|
|
160
|
+
patchState(threadIdOrConfig: string | Config, metadata: Metadata): Promise<void>;
|
|
161
|
+
/**
|
|
162
|
+
* Get all past states for a thread.
|
|
163
|
+
*
|
|
164
|
+
* @param threadId ID of the thread.
|
|
165
|
+
* @param options Additional options.
|
|
166
|
+
* @returns List of thread states.
|
|
167
|
+
*/
|
|
168
|
+
getHistory<ValuesType = DefaultValues>(threadId: string, options?: {
|
|
63
169
|
limit?: number;
|
|
64
170
|
before?: Config;
|
|
65
|
-
}): Promise<ThreadState[]>;
|
|
171
|
+
}): Promise<ThreadState<ValuesType>[]>;
|
|
66
172
|
}
|
|
67
173
|
declare class RunsClient extends BaseClient {
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
event: string;
|
|
77
|
-
data:
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
174
|
+
/**
|
|
175
|
+
* Create a run and stream the results.
|
|
176
|
+
*
|
|
177
|
+
* @param threadId The ID of the thread.
|
|
178
|
+
* @param assistantId Assistant ID to use for this run.
|
|
179
|
+
* @param payload Payload for creating a run.
|
|
180
|
+
*/
|
|
181
|
+
stream(threadId: string, assistantId: string, payload?: RunsStreamPayload): AsyncGenerator<{
|
|
182
|
+
event: "events" | "metadata" | "debug" | "updates" | "values" | "messages/partial" | "messages/metadata" | "messages/complete" | (string & {});
|
|
183
|
+
data: any;
|
|
184
|
+
}>;
|
|
185
|
+
/**
|
|
186
|
+
* Create a run.
|
|
187
|
+
*
|
|
188
|
+
* @param threadId The ID of the thread.
|
|
189
|
+
* @param assistantId Assistant ID to use for this run.
|
|
190
|
+
* @param payload Payload for creating a run.
|
|
191
|
+
* @returns The created run.
|
|
192
|
+
*/
|
|
193
|
+
create(threadId: string, assistantId: string, payload?: RunsCreatePayload): Promise<Run>;
|
|
194
|
+
/**
|
|
195
|
+
* Create a run and wait for it to complete.
|
|
196
|
+
*
|
|
197
|
+
* @param threadId The ID of the thread.
|
|
198
|
+
* @param assistantId Assistant ID to use for this run.
|
|
199
|
+
* @param payload Payload for creating a run.
|
|
200
|
+
* @returns The last values chunk of the thread.
|
|
201
|
+
*/
|
|
202
|
+
wait(threadId: string, assistantId: string, payload?: RunsWaitPayload): Promise<ThreadState["values"]>;
|
|
203
|
+
/**
|
|
204
|
+
* List all runs for a thread.
|
|
205
|
+
*
|
|
206
|
+
* @param threadId The ID of the thread.
|
|
207
|
+
* @param options Filtering and pagination options.
|
|
208
|
+
* @returns List of runs.
|
|
209
|
+
*/
|
|
95
210
|
list(threadId: string, options?: {
|
|
211
|
+
/**
|
|
212
|
+
* Maximum number of runs to return.
|
|
213
|
+
* Defaults to 10
|
|
214
|
+
*/
|
|
96
215
|
limit?: number;
|
|
216
|
+
/**
|
|
217
|
+
* Offset to start from.
|
|
218
|
+
* Defaults to 0.
|
|
219
|
+
*/
|
|
97
220
|
offset?: number;
|
|
98
221
|
}): Promise<Run[]>;
|
|
222
|
+
/**
|
|
223
|
+
* Get a run by ID.
|
|
224
|
+
*
|
|
225
|
+
* @param threadId The ID of the thread.
|
|
226
|
+
* @param runId The ID of the run.
|
|
227
|
+
* @returns The run.
|
|
228
|
+
*/
|
|
99
229
|
get(threadId: string, runId: string): Promise<Run>;
|
|
230
|
+
/**
|
|
231
|
+
* List all events for a run.
|
|
232
|
+
*
|
|
233
|
+
* @param threadId The ID of the thread.
|
|
234
|
+
* @param runId The ID of the run.
|
|
235
|
+
* @param options Filtering and pagination options.
|
|
236
|
+
* @returns List of events.
|
|
237
|
+
*/
|
|
100
238
|
listEvents(threadId: string, runId: string, options?: {
|
|
239
|
+
/**
|
|
240
|
+
* Maximum number of events to return.
|
|
241
|
+
* Defaults to 10
|
|
242
|
+
*/
|
|
101
243
|
limit?: number;
|
|
244
|
+
/**
|
|
245
|
+
* Offset to start from.
|
|
246
|
+
* Defaults to 0.
|
|
247
|
+
*/
|
|
102
248
|
offset?: number;
|
|
103
249
|
}): Promise<RunEvent[]>;
|
|
104
250
|
}
|
|
105
251
|
export declare class Client {
|
|
252
|
+
/**
|
|
253
|
+
* The client for interacting with assistants.
|
|
254
|
+
*/
|
|
106
255
|
assistants: AssistantsClient;
|
|
256
|
+
/**
|
|
257
|
+
* The client for interacting with threads.
|
|
258
|
+
*/
|
|
107
259
|
threads: ThreadsClient;
|
|
260
|
+
/**
|
|
261
|
+
* The client for interacting with runs.
|
|
262
|
+
*/
|
|
108
263
|
runs: RunsClient;
|
|
109
264
|
constructor(config?: ClientConfig);
|
|
110
265
|
}
|
package/dist/client.mjs
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { AsyncCaller } from "./utils/async_caller.mjs";
|
|
2
2
|
import { createParser } from "eventsource-parser";
|
|
3
|
-
import { IterableReadableStream
|
|
3
|
+
import { IterableReadableStream } from "./utils/stream.mjs";
|
|
4
4
|
class BaseClient {
|
|
5
5
|
constructor(config) {
|
|
6
6
|
Object.defineProperty(this, "asyncCaller", {
|
|
@@ -22,8 +22,8 @@ class BaseClient {
|
|
|
22
22
|
value: void 0
|
|
23
23
|
});
|
|
24
24
|
this.asyncCaller = new AsyncCaller({
|
|
25
|
-
maxRetries:
|
|
26
|
-
maxConcurrency:
|
|
25
|
+
maxRetries: 4,
|
|
26
|
+
maxConcurrency: 4,
|
|
27
27
|
...config?.callerOptions,
|
|
28
28
|
});
|
|
29
29
|
this.timeoutMs = config?.timeoutMs || 12_000;
|
|
@@ -59,26 +59,52 @@ class BaseClient {
|
|
|
59
59
|
}
|
|
60
60
|
}
|
|
61
61
|
class AssistantsClient extends BaseClient {
|
|
62
|
+
/**
|
|
63
|
+
* Get an assistant by ID.
|
|
64
|
+
*
|
|
65
|
+
* @param assistantId The ID of the assistant.
|
|
66
|
+
* @returns Assistant
|
|
67
|
+
*/
|
|
62
68
|
async get(assistantId) {
|
|
63
69
|
return this.fetch(`/assistants/${assistantId}`);
|
|
64
70
|
}
|
|
71
|
+
/**
|
|
72
|
+
* Get the JSON representation of the graph assigned to a runnable
|
|
73
|
+
* @param assistantId The ID of the assistant.
|
|
74
|
+
* @returns Serialized graph
|
|
75
|
+
*/
|
|
65
76
|
async getGraph(assistantId) {
|
|
66
77
|
return this.fetch(`/assistants/${assistantId}/graph`);
|
|
67
78
|
}
|
|
79
|
+
/**
|
|
80
|
+
* Get the state and config schema of the graph assigned to a runnable
|
|
81
|
+
* @param assistantId The ID of the assistant.
|
|
82
|
+
* @returns Graph schema
|
|
83
|
+
*/
|
|
68
84
|
async getSchemas(assistantId) {
|
|
69
85
|
return this.fetch(`/assistants/${assistantId}/schemas`);
|
|
70
86
|
}
|
|
87
|
+
/**
|
|
88
|
+
* Create a new assistant.
|
|
89
|
+
* @param payload Payload for creating an assistant.
|
|
90
|
+
* @returns The created assistant.
|
|
91
|
+
*/
|
|
71
92
|
async create(payload) {
|
|
72
93
|
return this.fetch("/assistants", {
|
|
73
94
|
method: "POST",
|
|
74
|
-
|
|
95
|
+
json: {
|
|
75
96
|
graph_id: payload.graphId,
|
|
76
97
|
config: payload.config,
|
|
77
98
|
metadata: payload.metadata,
|
|
78
|
-
}
|
|
79
|
-
headers: { "Content-Type": "application/json" },
|
|
99
|
+
},
|
|
80
100
|
});
|
|
81
101
|
}
|
|
102
|
+
/**
|
|
103
|
+
* Update an assistant.
|
|
104
|
+
* @param assistantId ID of the assistant.
|
|
105
|
+
* @param payload Payload for updating the assistant.
|
|
106
|
+
* @returns The updated assistant.
|
|
107
|
+
*/
|
|
82
108
|
async upsert(assistantId, payload) {
|
|
83
109
|
return this.fetch(`/assistants/${assistantId}`, {
|
|
84
110
|
method: "PUT",
|
|
@@ -89,57 +115,106 @@ class AssistantsClient extends BaseClient {
|
|
|
89
115
|
},
|
|
90
116
|
});
|
|
91
117
|
}
|
|
118
|
+
/**
|
|
119
|
+
* List assistants.
|
|
120
|
+
* @param query Query options.
|
|
121
|
+
* @returns List of assistants.
|
|
122
|
+
*/
|
|
92
123
|
async search(query) {
|
|
93
124
|
return this.fetch("/assistants/search", {
|
|
94
125
|
method: "POST",
|
|
95
126
|
json: {
|
|
96
|
-
metadata: query
|
|
97
|
-
limit: query
|
|
98
|
-
offset: query
|
|
127
|
+
metadata: query?.metadata ?? undefined,
|
|
128
|
+
limit: query?.limit ?? 10,
|
|
129
|
+
offset: query?.offset ?? 0,
|
|
99
130
|
},
|
|
100
131
|
});
|
|
101
132
|
}
|
|
102
133
|
}
|
|
103
134
|
class ThreadsClient extends BaseClient {
|
|
135
|
+
/**
|
|
136
|
+
* Get a thread by ID.
|
|
137
|
+
*
|
|
138
|
+
* @param threadId ID of the thread.
|
|
139
|
+
* @returns The thread.
|
|
140
|
+
*/
|
|
104
141
|
async get(threadId) {
|
|
105
142
|
return this.fetch(`/threads/${threadId}`);
|
|
106
143
|
}
|
|
144
|
+
/**
|
|
145
|
+
* Create a new thread.
|
|
146
|
+
*
|
|
147
|
+
* @param payload Payload for creating a thread.
|
|
148
|
+
* @returns The created thread.
|
|
149
|
+
*/
|
|
107
150
|
async create(payload) {
|
|
108
151
|
return this.fetch(`/threads`, {
|
|
109
152
|
method: "POST",
|
|
110
153
|
json: { metadata: payload },
|
|
111
154
|
});
|
|
112
155
|
}
|
|
156
|
+
/**
|
|
157
|
+
* Update a thread.
|
|
158
|
+
*
|
|
159
|
+
* @param threadId ID of the thread.
|
|
160
|
+
* @param payload Payload for updating the thread.
|
|
161
|
+
* @returns The updated thread.
|
|
162
|
+
*/
|
|
113
163
|
async upsert(threadId, payload) {
|
|
114
164
|
return this.fetch(`/threads/${threadId}`, {
|
|
115
165
|
method: "PUT",
|
|
116
166
|
json: { metadata: payload },
|
|
117
167
|
});
|
|
118
168
|
}
|
|
169
|
+
/**
|
|
170
|
+
* Delete a thread.
|
|
171
|
+
*
|
|
172
|
+
* @param threadId ID of the thread.
|
|
173
|
+
*/
|
|
119
174
|
async delete(threadId) {
|
|
120
175
|
return this.fetch(`/threads/${threadId}`, {
|
|
121
176
|
method: "DELETE",
|
|
122
177
|
});
|
|
123
178
|
}
|
|
179
|
+
/**
|
|
180
|
+
* List threads
|
|
181
|
+
*
|
|
182
|
+
* @param query Query options
|
|
183
|
+
* @returns List of threads
|
|
184
|
+
*/
|
|
124
185
|
async search(query) {
|
|
125
186
|
return this.fetch("/threads/search", {
|
|
126
187
|
method: "POST",
|
|
127
188
|
json: {
|
|
128
|
-
metadata: query
|
|
129
|
-
limit: query
|
|
130
|
-
offset: query
|
|
189
|
+
metadata: query?.metadata ?? undefined,
|
|
190
|
+
limit: query?.limit ?? 10,
|
|
191
|
+
offset: query?.offset ?? 0,
|
|
131
192
|
},
|
|
132
193
|
});
|
|
133
194
|
}
|
|
195
|
+
/**
|
|
196
|
+
* Get state for a thread.
|
|
197
|
+
*
|
|
198
|
+
* @param threadId ID of the thread.
|
|
199
|
+
* @returns Thread state.
|
|
200
|
+
*/
|
|
134
201
|
async getState(threadId) {
|
|
135
202
|
return this.fetch(`/threads/${threadId}/state`);
|
|
136
203
|
}
|
|
137
|
-
|
|
204
|
+
/**
|
|
205
|
+
* Add state to a thread.
|
|
206
|
+
*
|
|
207
|
+
* @param threadIdOrConfig Thread ID or config that identifies the state to start from.
|
|
208
|
+
* @param values The state update
|
|
209
|
+
* @param options Additional options.
|
|
210
|
+
* @returns
|
|
211
|
+
*/
|
|
212
|
+
async updateState(threadIdOrConfig, options) {
|
|
138
213
|
let config = undefined;
|
|
139
214
|
let threadId;
|
|
140
215
|
if (typeof threadIdOrConfig !== "string") {
|
|
141
216
|
config = threadIdOrConfig;
|
|
142
|
-
if (typeof config.configurable
|
|
217
|
+
if (typeof config.configurable?.thread_id !== "string") {
|
|
143
218
|
throw new Error("Thread ID is required when updating state with a config.");
|
|
144
219
|
}
|
|
145
220
|
threadId = config.configurable.thread_id;
|
|
@@ -150,9 +225,15 @@ class ThreadsClient extends BaseClient {
|
|
|
150
225
|
}
|
|
151
226
|
return this.fetch(`/threads/${threadId}/state`, {
|
|
152
227
|
method: "POST",
|
|
153
|
-
json: { values, config, as_node: options?.asNode },
|
|
228
|
+
json: { values: options.values, config, as_node: options?.asNode },
|
|
154
229
|
});
|
|
155
230
|
}
|
|
231
|
+
/**
|
|
232
|
+
* Patch the metadata of a thread.
|
|
233
|
+
*
|
|
234
|
+
* @param threadIdOrConfig Thread ID or config to patch the state of.
|
|
235
|
+
* @param metadata Metadata to patch the state with.
|
|
236
|
+
*/
|
|
156
237
|
async patchState(threadIdOrConfig, metadata) {
|
|
157
238
|
let threadId;
|
|
158
239
|
if (typeof threadIdOrConfig !== "string") {
|
|
@@ -169,6 +250,13 @@ class ThreadsClient extends BaseClient {
|
|
|
169
250
|
json: { metadata: metadata },
|
|
170
251
|
});
|
|
171
252
|
}
|
|
253
|
+
/**
|
|
254
|
+
* Get all past states for a thread.
|
|
255
|
+
*
|
|
256
|
+
* @param threadId ID of the thread.
|
|
257
|
+
* @param options Additional options.
|
|
258
|
+
* @returns List of thread states.
|
|
259
|
+
*/
|
|
172
260
|
async getHistory(threadId, options) {
|
|
173
261
|
return this.fetch(`/threads/${threadId}/history`, {
|
|
174
262
|
params: {
|
|
@@ -179,17 +267,24 @@ class ThreadsClient extends BaseClient {
|
|
|
179
267
|
}
|
|
180
268
|
}
|
|
181
269
|
class RunsClient extends BaseClient {
|
|
182
|
-
|
|
270
|
+
/**
|
|
271
|
+
* Create a run and stream the results.
|
|
272
|
+
*
|
|
273
|
+
* @param threadId The ID of the thread.
|
|
274
|
+
* @param assistantId Assistant ID to use for this run.
|
|
275
|
+
* @param payload Payload for creating a run.
|
|
276
|
+
*/
|
|
277
|
+
async *stream(threadId, assistantId, payload) {
|
|
183
278
|
const response = await this.asyncCaller.fetch(...this.prepareFetchOptions(`/threads/${threadId}/runs/stream`, {
|
|
184
279
|
method: "POST",
|
|
185
280
|
json: {
|
|
186
|
-
input:
|
|
187
|
-
config:
|
|
188
|
-
metadata:
|
|
189
|
-
stream_mode:
|
|
281
|
+
input: payload?.input,
|
|
282
|
+
config: payload?.config,
|
|
283
|
+
metadata: payload?.metadata,
|
|
284
|
+
stream_mode: payload?.streamMode,
|
|
190
285
|
assistant_id: assistantId,
|
|
191
|
-
interrupt_before:
|
|
192
|
-
interrupt_after:
|
|
286
|
+
interrupt_before: payload?.interruptBefore,
|
|
287
|
+
interrupt_after: payload?.interruptAfter,
|
|
193
288
|
},
|
|
194
289
|
}));
|
|
195
290
|
let parser;
|
|
@@ -203,7 +298,7 @@ class RunsClient extends BaseClient {
|
|
|
203
298
|
}
|
|
204
299
|
if ("data" in event) {
|
|
205
300
|
ctrl.enqueue({
|
|
206
|
-
event: event.event ?? "
|
|
301
|
+
event: event.event ?? "message",
|
|
207
302
|
data: JSON.parse(event.data),
|
|
208
303
|
});
|
|
209
304
|
}
|
|
@@ -213,35 +308,58 @@ class RunsClient extends BaseClient {
|
|
|
213
308
|
parser.feed(textDecoder.decode(chunk));
|
|
214
309
|
},
|
|
215
310
|
}));
|
|
216
|
-
|
|
311
|
+
yield* IterableReadableStream.fromReadableStream(stream);
|
|
217
312
|
}
|
|
218
|
-
|
|
313
|
+
/**
|
|
314
|
+
* Create a run.
|
|
315
|
+
*
|
|
316
|
+
* @param threadId The ID of the thread.
|
|
317
|
+
* @param assistantId Assistant ID to use for this run.
|
|
318
|
+
* @param payload Payload for creating a run.
|
|
319
|
+
* @returns The created run.
|
|
320
|
+
*/
|
|
321
|
+
async create(threadId, assistantId, payload) {
|
|
219
322
|
return this.fetch(`/threads/${threadId}/runs`, {
|
|
220
323
|
method: "POST",
|
|
221
324
|
json: {
|
|
222
|
-
input:
|
|
223
|
-
config:
|
|
224
|
-
metadata:
|
|
325
|
+
input: payload?.input,
|
|
326
|
+
config: payload?.config,
|
|
327
|
+
metadata: payload?.metadata,
|
|
225
328
|
assistant_id: assistantId,
|
|
226
|
-
interrupt_before:
|
|
227
|
-
interrupt_after:
|
|
228
|
-
webhook:
|
|
329
|
+
interrupt_before: payload?.interruptBefore,
|
|
330
|
+
interrupt_after: payload?.interruptAfter,
|
|
331
|
+
webhook: payload?.webhook,
|
|
229
332
|
},
|
|
230
333
|
});
|
|
231
334
|
}
|
|
232
|
-
|
|
335
|
+
/**
|
|
336
|
+
* Create a run and wait for it to complete.
|
|
337
|
+
*
|
|
338
|
+
* @param threadId The ID of the thread.
|
|
339
|
+
* @param assistantId Assistant ID to use for this run.
|
|
340
|
+
* @param payload Payload for creating a run.
|
|
341
|
+
* @returns The last values chunk of the thread.
|
|
342
|
+
*/
|
|
343
|
+
async wait(threadId, assistantId, payload) {
|
|
233
344
|
return this.fetch(`/threads/${threadId}/runs/wait`, {
|
|
234
345
|
method: "POST",
|
|
235
346
|
json: {
|
|
236
|
-
input:
|
|
237
|
-
config:
|
|
238
|
-
metadata:
|
|
347
|
+
input: payload?.input,
|
|
348
|
+
config: payload?.config,
|
|
349
|
+
metadata: payload?.metadata,
|
|
239
350
|
assistant_id: assistantId,
|
|
240
|
-
interrupt_before:
|
|
241
|
-
interrupt_after:
|
|
351
|
+
interrupt_before: payload?.interruptBefore,
|
|
352
|
+
interrupt_after: payload?.interruptAfter,
|
|
242
353
|
},
|
|
243
354
|
});
|
|
244
355
|
}
|
|
356
|
+
/**
|
|
357
|
+
* List all runs for a thread.
|
|
358
|
+
*
|
|
359
|
+
* @param threadId The ID of the thread.
|
|
360
|
+
* @param options Filtering and pagination options.
|
|
361
|
+
* @returns List of runs.
|
|
362
|
+
*/
|
|
245
363
|
async list(threadId, options) {
|
|
246
364
|
return this.fetch(`/threads/${threadId}/runs`, {
|
|
247
365
|
params: {
|
|
@@ -250,9 +368,24 @@ class RunsClient extends BaseClient {
|
|
|
250
368
|
},
|
|
251
369
|
});
|
|
252
370
|
}
|
|
371
|
+
/**
|
|
372
|
+
* Get a run by ID.
|
|
373
|
+
*
|
|
374
|
+
* @param threadId The ID of the thread.
|
|
375
|
+
* @param runId The ID of the run.
|
|
376
|
+
* @returns The run.
|
|
377
|
+
*/
|
|
253
378
|
async get(threadId, runId) {
|
|
254
379
|
return this.fetch(`/threads/${threadId}/runs/${runId}`);
|
|
255
380
|
}
|
|
381
|
+
/**
|
|
382
|
+
* List all events for a run.
|
|
383
|
+
*
|
|
384
|
+
* @param threadId The ID of the thread.
|
|
385
|
+
* @param runId The ID of the run.
|
|
386
|
+
* @param options Filtering and pagination options.
|
|
387
|
+
* @returns List of events.
|
|
388
|
+
*/
|
|
256
389
|
async listEvents(threadId, runId, options) {
|
|
257
390
|
return this.fetch(`/threads/${threadId}/runs/${runId}/events`, {
|
|
258
391
|
params: {
|
|
@@ -264,18 +397,27 @@ class RunsClient extends BaseClient {
|
|
|
264
397
|
}
|
|
265
398
|
export class Client {
|
|
266
399
|
constructor(config) {
|
|
400
|
+
/**
|
|
401
|
+
* The client for interacting with assistants.
|
|
402
|
+
*/
|
|
267
403
|
Object.defineProperty(this, "assistants", {
|
|
268
404
|
enumerable: true,
|
|
269
405
|
configurable: true,
|
|
270
406
|
writable: true,
|
|
271
407
|
value: void 0
|
|
272
408
|
});
|
|
409
|
+
/**
|
|
410
|
+
* The client for interacting with threads.
|
|
411
|
+
*/
|
|
273
412
|
Object.defineProperty(this, "threads", {
|
|
274
413
|
enumerable: true,
|
|
275
414
|
configurable: true,
|
|
276
415
|
writable: true,
|
|
277
416
|
value: void 0
|
|
278
417
|
});
|
|
418
|
+
/**
|
|
419
|
+
* The client for interacting with runs.
|
|
420
|
+
*/
|
|
279
421
|
Object.defineProperty(this, "runs", {
|
|
280
422
|
enumerable: true,
|
|
281
423
|
configurable: true,
|
package/dist/schema.d.ts
CHANGED
|
@@ -4,20 +4,32 @@ export interface Config {
|
|
|
4
4
|
* Tags for this call and any sub-calls (eg. a Chain calling an LLM).
|
|
5
5
|
* You can use these to filter calls.
|
|
6
6
|
*/
|
|
7
|
-
tags
|
|
7
|
+
tags?: string[];
|
|
8
8
|
/**
|
|
9
9
|
* Maximum number of times a call can recurse.
|
|
10
10
|
* If not provided, defaults to 25.
|
|
11
11
|
*/
|
|
12
|
-
recursion_limit
|
|
12
|
+
recursion_limit?: number;
|
|
13
13
|
/**
|
|
14
14
|
* Runtime values for attributes previously made configurable on this Runnable.
|
|
15
15
|
*/
|
|
16
|
-
configurable:
|
|
16
|
+
configurable: {
|
|
17
|
+
thread_id?: string;
|
|
18
|
+
[key: string]: unknown;
|
|
19
|
+
};
|
|
17
20
|
}
|
|
18
21
|
export interface GraphSchema {
|
|
22
|
+
/**
|
|
23
|
+
* The ID of the graph.
|
|
24
|
+
*/
|
|
19
25
|
graph_id: string;
|
|
26
|
+
/**
|
|
27
|
+
* The schema for the graph state
|
|
28
|
+
*/
|
|
20
29
|
state_schema: Record<string, unknown>;
|
|
30
|
+
/**
|
|
31
|
+
* The schema for the graph config
|
|
32
|
+
*/
|
|
21
33
|
config_schema: Record<string, unknown>;
|
|
22
34
|
}
|
|
23
35
|
export type Metadata = Optional<Record<string, unknown>>;
|
|
@@ -36,8 +48,9 @@ export interface Thread {
|
|
|
36
48
|
updated_at: string;
|
|
37
49
|
metadata: Metadata;
|
|
38
50
|
}
|
|
39
|
-
export
|
|
40
|
-
|
|
51
|
+
export type DefaultValues = Record<string, unknown>[] | Record<string, unknown>;
|
|
52
|
+
export interface ThreadState<ValuesType = DefaultValues> {
|
|
53
|
+
values: ValuesType;
|
|
41
54
|
next: string[];
|
|
42
55
|
config: Config;
|
|
43
56
|
metadata: Metadata;
|
package/dist/types.d.mts
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { Config, Metadata } from "./schema.js";
|
|
2
|
+
export type StreamMode = "values" | "messages" | "updates" | "events" | "debug";
|
|
3
|
+
interface RunsInvokePayload {
|
|
4
|
+
/**
|
|
5
|
+
* Input to the run. Pass `null` to resume from the current state of the thread.
|
|
6
|
+
*/
|
|
7
|
+
input?: Record<string, unknown> | null;
|
|
8
|
+
/**
|
|
9
|
+
* Metadata for the run.
|
|
10
|
+
*/
|
|
11
|
+
metadata?: Metadata;
|
|
12
|
+
/**
|
|
13
|
+
* Additional configuration for the run.
|
|
14
|
+
*/
|
|
15
|
+
config?: Config;
|
|
16
|
+
/**
|
|
17
|
+
* Interrupt execution before entering these nodes.
|
|
18
|
+
*/
|
|
19
|
+
interruptBefore?: string[];
|
|
20
|
+
/**
|
|
21
|
+
* Interrupt execution after leaving these nodes.
|
|
22
|
+
*/
|
|
23
|
+
interruptAfter?: string[];
|
|
24
|
+
}
|
|
25
|
+
export interface RunsStreamPayload extends RunsInvokePayload {
|
|
26
|
+
/**
|
|
27
|
+
* One of `"values"`, `"messages"`, `"updates"` or `"events"`.
|
|
28
|
+
* - `"values"`: Stream the thread state any time it changes.
|
|
29
|
+
* - `"messages"`: Stream chat messages from thread state and calls to chat models,
|
|
30
|
+
* token-by-token where possible.
|
|
31
|
+
* - `"updates"`: Stream the state updates returned by each node.
|
|
32
|
+
* - `"events"`: Stream all events produced by the run. You can also access these
|
|
33
|
+
* afterwards using the `client.runs.listEvents()` method.
|
|
34
|
+
*/
|
|
35
|
+
streamMode?: StreamMode | Array<StreamMode>;
|
|
36
|
+
}
|
|
37
|
+
export interface RunsCreatePayload extends RunsInvokePayload {
|
|
38
|
+
/**
|
|
39
|
+
* Webhook to call when the run is complete.
|
|
40
|
+
*/
|
|
41
|
+
webhook?: string;
|
|
42
|
+
}
|
|
43
|
+
export type RunsWaitPayload = RunsStreamPayload;
|
|
44
|
+
export {};
|
package/dist/types.mjs
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -24,8 +24,8 @@ export interface AsyncCallerCallOptions {
|
|
|
24
24
|
* Concurrent calls are limited by the `maxConcurrency` parameter, which defaults
|
|
25
25
|
* to `Infinity`. This means that by default, all calls will be made in parallel.
|
|
26
26
|
*
|
|
27
|
-
* Retries are limited by the `maxRetries` parameter, which defaults to
|
|
28
|
-
* means that by default, each call will be retried up to
|
|
27
|
+
* Retries are limited by the `maxRetries` parameter, which defaults to 5. This
|
|
28
|
+
* means that by default, each call will be retried up to 5 times, with an
|
|
29
29
|
* exponential backoff between each attempt.
|
|
30
30
|
*/
|
|
31
31
|
export declare class AsyncCaller {
|
|
@@ -9,10 +9,57 @@ const STATUS_NO_RETRY = [
|
|
|
9
9
|
406, // Not Acceptable
|
|
10
10
|
407, // Proxy Authentication Required
|
|
11
11
|
408, // Request Timeout
|
|
12
|
+
422, // Unprocessable Entity
|
|
12
13
|
];
|
|
13
14
|
const STATUS_IGNORE = [
|
|
14
15
|
409, // Conflict
|
|
15
16
|
];
|
|
17
|
+
/**
|
|
18
|
+
* Do not rely on globalThis.Response, rather just
|
|
19
|
+
* do duck typing
|
|
20
|
+
*/
|
|
21
|
+
function isResponse(x) {
|
|
22
|
+
if (x == null || typeof x !== "object")
|
|
23
|
+
return false;
|
|
24
|
+
return "status" in x && "statusText" in x && "text" in x;
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Utility error to properly handle failed requests
|
|
28
|
+
*/
|
|
29
|
+
class HTTPError extends Error {
|
|
30
|
+
constructor(status, message, response) {
|
|
31
|
+
super(`HTTP ${status}: ${message}`);
|
|
32
|
+
Object.defineProperty(this, "status", {
|
|
33
|
+
enumerable: true,
|
|
34
|
+
configurable: true,
|
|
35
|
+
writable: true,
|
|
36
|
+
value: void 0
|
|
37
|
+
});
|
|
38
|
+
Object.defineProperty(this, "text", {
|
|
39
|
+
enumerable: true,
|
|
40
|
+
configurable: true,
|
|
41
|
+
writable: true,
|
|
42
|
+
value: void 0
|
|
43
|
+
});
|
|
44
|
+
Object.defineProperty(this, "response", {
|
|
45
|
+
enumerable: true,
|
|
46
|
+
configurable: true,
|
|
47
|
+
writable: true,
|
|
48
|
+
value: void 0
|
|
49
|
+
});
|
|
50
|
+
this.status = status;
|
|
51
|
+
this.text = message;
|
|
52
|
+
this.response = response;
|
|
53
|
+
}
|
|
54
|
+
static async fromResponse(response, options) {
|
|
55
|
+
try {
|
|
56
|
+
return new HTTPError(response.status, await response.text(), options?.includeResponse ? response : undefined);
|
|
57
|
+
}
|
|
58
|
+
catch {
|
|
59
|
+
return new HTTPError(response.status, response.statusText, options?.includeResponse ? response : undefined);
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
}
|
|
16
63
|
/**
|
|
17
64
|
* A class that can be used to make async calls with concurrency and retry logic.
|
|
18
65
|
*
|
|
@@ -22,8 +69,8 @@ const STATUS_IGNORE = [
|
|
|
22
69
|
* Concurrent calls are limited by the `maxConcurrency` parameter, which defaults
|
|
23
70
|
* to `Infinity`. This means that by default, all calls will be made in parallel.
|
|
24
71
|
*
|
|
25
|
-
* Retries are limited by the `maxRetries` parameter, which defaults to
|
|
26
|
-
* means that by default, each call will be retried up to
|
|
72
|
+
* Retries are limited by the `maxRetries` parameter, which defaults to 5. This
|
|
73
|
+
* means that by default, each call will be retried up to 5 times, with an
|
|
27
74
|
* exponential backoff between each attempt.
|
|
28
75
|
*/
|
|
29
76
|
export class AsyncCaller {
|
|
@@ -53,7 +100,7 @@ export class AsyncCaller {
|
|
|
53
100
|
value: void 0
|
|
54
101
|
});
|
|
55
102
|
this.maxConcurrency = params.maxConcurrency ?? Infinity;
|
|
56
|
-
this.maxRetries = params.maxRetries ??
|
|
103
|
+
this.maxRetries = params.maxRetries ?? 4;
|
|
57
104
|
if ("default" in PQueueMod) {
|
|
58
105
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
59
106
|
this.queue = new PQueueMod.default({
|
|
@@ -69,15 +116,15 @@ export class AsyncCaller {
|
|
|
69
116
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
70
117
|
call(callable, ...args) {
|
|
71
118
|
const onFailedResponseHook = this.onFailedResponseHook;
|
|
72
|
-
return this.queue.add(() => pRetry(() => callable(...args).catch((error) => {
|
|
119
|
+
return this.queue.add(() => pRetry(() => callable(...args).catch(async (error) => {
|
|
73
120
|
// eslint-disable-next-line no-instanceof/no-instanceof
|
|
74
|
-
console.error;
|
|
75
121
|
if (error instanceof Error) {
|
|
76
122
|
throw error;
|
|
77
123
|
}
|
|
78
|
-
else if (error
|
|
79
|
-
|
|
80
|
-
|
|
124
|
+
else if (isResponse(error)) {
|
|
125
|
+
throw await HTTPError.fromResponse(error, {
|
|
126
|
+
includeResponse: !!onFailedResponseHook,
|
|
127
|
+
});
|
|
81
128
|
}
|
|
82
129
|
else {
|
|
83
130
|
throw new Error(error);
|
|
@@ -93,18 +140,15 @@ export class AsyncCaller {
|
|
|
93
140
|
if (error?.code === "ECONNABORTED") {
|
|
94
141
|
throw error;
|
|
95
142
|
}
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
const status = response?.status;
|
|
99
|
-
if (status) {
|
|
100
|
-
if (STATUS_NO_RETRY.includes(+status)) {
|
|
143
|
+
if (error instanceof HTTPError) {
|
|
144
|
+
if (STATUS_NO_RETRY.includes(error.status)) {
|
|
101
145
|
throw error;
|
|
102
146
|
}
|
|
103
|
-
else if (STATUS_IGNORE.includes(
|
|
147
|
+
else if (STATUS_IGNORE.includes(error.status)) {
|
|
104
148
|
return;
|
|
105
149
|
}
|
|
106
|
-
if (onFailedResponseHook) {
|
|
107
|
-
await onFailedResponseHook(response);
|
|
150
|
+
if (onFailedResponseHook && error.response) {
|
|
151
|
+
await onFailedResponseHook(error.response);
|
|
108
152
|
}
|
|
109
153
|
}
|
|
110
154
|
},
|
package/dist/utils/stream.d.mts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
|
|
1
|
+
type IterableReadableStreamInterface<T> = ReadableStream<T> & AsyncIterable<T>;
|
|
2
2
|
export declare class IterableReadableStream<T> extends ReadableStream<T> implements IterableReadableStreamInterface<T> {
|
|
3
3
|
reader: ReadableStreamDefaultReader<T>;
|
|
4
4
|
ensureReader(): void;
|
|
@@ -9,3 +9,4 @@ export declare class IterableReadableStream<T> extends ReadableStream<T> impleme
|
|
|
9
9
|
static fromReadableStream<T>(stream: ReadableStream<T>): IterableReadableStream<T>;
|
|
10
10
|
static fromAsyncGenerator<T>(generator: AsyncGenerator<T>): IterableReadableStream<T>;
|
|
11
11
|
}
|
|
12
|
+
export {};
|