@langchain/langgraph-sdk 0.0.1-rc.9 → 0.0.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/LICENSE +6 -6
- package/README.md +64 -0
- package/dist/client.d.mts +76 -33
- package/dist/client.mjs +184 -38
- package/dist/index.d.mts +1 -1
- package/dist/schema.d.ts +9 -11
- package/dist/types.d.mts +20 -0
- package/dist/utils/async_caller.d.mts +7 -0
- package/dist/utils/async_caller.mjs +9 -1
- package/package.json +4 -1
package/LICENSE
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
|
|
1
|
+
MIT License
|
|
2
2
|
|
|
3
|
-
Copyright (c) 2024 LangChain
|
|
3
|
+
Copyright (c) 2024 LangChain, Inc.
|
|
4
4
|
|
|
5
5
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
6
|
of this software and associated documentation files (the "Software"), to deal
|
|
@@ -9,13 +9,13 @@ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
9
9
|
copies of the Software, and to permit persons to whom the Software is
|
|
10
10
|
furnished to do so, subject to the following conditions:
|
|
11
11
|
|
|
12
|
-
The above copyright notice and this permission notice shall be included in
|
|
13
|
-
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
14
|
|
|
15
15
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
16
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
17
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
18
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
19
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
-
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
21
|
-
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
# LangGraph JS/TS SDK
|
|
2
|
+
|
|
3
|
+
This repository contains the JS/TS SDK for interacting with the LangGraph REST API.
|
|
4
|
+
|
|
5
|
+
## Quick Start
|
|
6
|
+
|
|
7
|
+
To get started with the JS/TS SDK, [install the package](https://www.npmjs.com/package/@langchain/langgraph-sdk)
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
yarn add @langchain/langgraph-sdk
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
You will need a running LangGraph API server. If you're running a server locally using `langgraph-cli`, SDK will automatically point at `http://localhost:8123`, otherwise
|
|
14
|
+
you would need to specify the server URL when creating a client.
|
|
15
|
+
|
|
16
|
+
```js
|
|
17
|
+
import { Client } from "@langchain/langgraph-sdk";
|
|
18
|
+
|
|
19
|
+
const client = new Client();
|
|
20
|
+
|
|
21
|
+
// List all assistants
|
|
22
|
+
const assistants = await client.assistants.search({
|
|
23
|
+
metadata: null,
|
|
24
|
+
offset: 0,
|
|
25
|
+
limit: 10,
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
// We auto-create an assistant for each graph you register in config.
|
|
29
|
+
const agent = assistants[0];
|
|
30
|
+
|
|
31
|
+
// Start a new thread
|
|
32
|
+
const thread = await client.threads.create();
|
|
33
|
+
|
|
34
|
+
// Start a streaming run
|
|
35
|
+
const messages = [{ role: "human", content: "what's the weather in la" }];
|
|
36
|
+
|
|
37
|
+
const streamResponse = client.runs.stream(
|
|
38
|
+
thread["thread_id"],
|
|
39
|
+
agent["assistant_id"],
|
|
40
|
+
{
|
|
41
|
+
input: { messages },
|
|
42
|
+
}
|
|
43
|
+
);
|
|
44
|
+
|
|
45
|
+
for await (const chunk of streamResponse) {
|
|
46
|
+
console.log(chunk);
|
|
47
|
+
}
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
## Documentation
|
|
51
|
+
|
|
52
|
+
To generate documentation, run the following commands:
|
|
53
|
+
|
|
54
|
+
1. Generate docs.
|
|
55
|
+
|
|
56
|
+
yarn typedoc
|
|
57
|
+
|
|
58
|
+
1. Consolidate doc files into one markdown file.
|
|
59
|
+
|
|
60
|
+
npx concat-md --decrease-title-levels --ignore=js_ts_sdk_ref.md --start-title-level-at 2 docs > docs/js_ts_sdk_ref.md
|
|
61
|
+
|
|
62
|
+
1. Copy `js_ts_sdk_ref.md` to MkDocs directory.
|
|
63
|
+
|
|
64
|
+
cp docs/js_ts_sdk_ref.md ../../docs/docs/cloud/reference/sdk/js_ts_sdk_ref.md
|
package/dist/client.d.mts
CHANGED
|
@@ -1,15 +1,17 @@
|
|
|
1
|
-
import { Assistant, AssistantGraph, Config, DefaultValues, GraphSchema, Metadata, Run,
|
|
1
|
+
import { Assistant, AssistantGraph, Config, DefaultValues, GraphSchema, Metadata, Run, Thread, ThreadState, Cron } from "./schema.js";
|
|
2
2
|
import { AsyncCaller, AsyncCallerParams } from "./utils/async_caller.mjs";
|
|
3
|
-
import { RunsCreatePayload, RunsStreamPayload, RunsWaitPayload } from "./types.mjs";
|
|
3
|
+
import { RunsCreatePayload, RunsStreamPayload, RunsWaitPayload, StreamEvent, CronsCreatePayload, OnConflictBehavior } from "./types.mjs";
|
|
4
4
|
interface ClientConfig {
|
|
5
5
|
apiUrl?: string;
|
|
6
6
|
callerOptions?: AsyncCallerParams;
|
|
7
7
|
timeoutMs?: number;
|
|
8
|
+
defaultHeaders?: Record<string, string | null | undefined>;
|
|
8
9
|
}
|
|
9
10
|
declare class BaseClient {
|
|
10
11
|
protected asyncCaller: AsyncCaller;
|
|
11
12
|
protected timeoutMs: number;
|
|
12
13
|
protected apiUrl: string;
|
|
14
|
+
protected defaultHeaders: Record<string, string | null | undefined>;
|
|
13
15
|
constructor(config?: ClientConfig);
|
|
14
16
|
protected prepareFetchOptions(path: string, options?: RequestInit & {
|
|
15
17
|
json?: unknown;
|
|
@@ -20,6 +22,39 @@ declare class BaseClient {
|
|
|
20
22
|
params?: Record<string, unknown>;
|
|
21
23
|
}): Promise<T>;
|
|
22
24
|
}
|
|
25
|
+
declare class CronsClient extends BaseClient {
|
|
26
|
+
/**
|
|
27
|
+
*
|
|
28
|
+
* @param threadId The ID of the thread.
|
|
29
|
+
* @param assistantId Assistant ID to use for this cron job.
|
|
30
|
+
* @param payload Payload for creating a cron job.
|
|
31
|
+
* @returns The created background run.
|
|
32
|
+
*/
|
|
33
|
+
createForThread(threadId: string, assistantId: string, payload?: CronsCreatePayload): Promise<Run>;
|
|
34
|
+
/**
|
|
35
|
+
*
|
|
36
|
+
* @param assistantId Assistant ID to use for this cron job.
|
|
37
|
+
* @param payload Payload for creating a cron job.
|
|
38
|
+
* @returns
|
|
39
|
+
*/
|
|
40
|
+
create(assistantId: string, payload?: CronsCreatePayload): Promise<Run>;
|
|
41
|
+
/**
|
|
42
|
+
*
|
|
43
|
+
* @param cronId Cron ID of Cron job to delete.
|
|
44
|
+
*/
|
|
45
|
+
delete(cronId: string): Promise<void>;
|
|
46
|
+
/**
|
|
47
|
+
*
|
|
48
|
+
* @param query Query options.
|
|
49
|
+
* @returns List of crons.
|
|
50
|
+
*/
|
|
51
|
+
search(query?: {
|
|
52
|
+
assistantId?: string;
|
|
53
|
+
threadId?: string;
|
|
54
|
+
limit?: number;
|
|
55
|
+
offset?: number;
|
|
56
|
+
}): Promise<Cron[]>;
|
|
57
|
+
}
|
|
23
58
|
declare class AssistantsClient extends BaseClient {
|
|
24
59
|
/**
|
|
25
60
|
* Get an assistant by ID.
|
|
@@ -61,12 +96,19 @@ declare class AssistantsClient extends BaseClient {
|
|
|
61
96
|
config?: Config;
|
|
62
97
|
metadata?: Metadata;
|
|
63
98
|
}): Promise<Assistant>;
|
|
99
|
+
/**
|
|
100
|
+
* Delete an assistant.
|
|
101
|
+
*
|
|
102
|
+
* @param assistantId ID of the assistant.
|
|
103
|
+
*/
|
|
104
|
+
delete(assistantId: string): Promise<void>;
|
|
64
105
|
/**
|
|
65
106
|
* List assistants.
|
|
66
107
|
* @param query Query options.
|
|
67
108
|
* @returns List of assistants.
|
|
68
109
|
*/
|
|
69
110
|
search(query?: {
|
|
111
|
+
graphId?: string;
|
|
70
112
|
metadata?: Metadata;
|
|
71
113
|
limit?: number;
|
|
72
114
|
offset?: number;
|
|
@@ -91,6 +133,8 @@ declare class ThreadsClient extends BaseClient {
|
|
|
91
133
|
* Metadata for the thread.
|
|
92
134
|
*/
|
|
93
135
|
metadata?: Metadata;
|
|
136
|
+
threadId?: string;
|
|
137
|
+
ifExists?: OnConflictBehavior;
|
|
94
138
|
}): Promise<Thread>;
|
|
95
139
|
/**
|
|
96
140
|
* Update a thread.
|
|
@@ -171,15 +215,12 @@ declare class ThreadsClient extends BaseClient {
|
|
|
171
215
|
}): Promise<ThreadState<ValuesType>[]>;
|
|
172
216
|
}
|
|
173
217
|
declare class RunsClient extends BaseClient {
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
* @param assistantId Assistant ID to use for this run.
|
|
179
|
-
* @param payload Payload for creating a run.
|
|
180
|
-
*/
|
|
218
|
+
stream(threadId: null, assistantId: string, payload?: Omit<RunsStreamPayload, "multitaskStrategy">): AsyncGenerator<{
|
|
219
|
+
event: StreamEvent;
|
|
220
|
+
data: any;
|
|
221
|
+
}>;
|
|
181
222
|
stream(threadId: string, assistantId: string, payload?: RunsStreamPayload): AsyncGenerator<{
|
|
182
|
-
event:
|
|
223
|
+
event: StreamEvent;
|
|
183
224
|
data: any;
|
|
184
225
|
}>;
|
|
185
226
|
/**
|
|
@@ -191,14 +232,7 @@ declare class RunsClient extends BaseClient {
|
|
|
191
232
|
* @returns The created run.
|
|
192
233
|
*/
|
|
193
234
|
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
|
-
*/
|
|
235
|
+
wait(threadId: null, assistantId: string, payload?: Omit<RunsWaitPayload, "multitaskStrategy">): Promise<ThreadState["values"]>;
|
|
202
236
|
wait(threadId: string, assistantId: string, payload?: RunsWaitPayload): Promise<ThreadState["values"]>;
|
|
203
237
|
/**
|
|
204
238
|
* List all runs for a thread.
|
|
@@ -228,25 +262,30 @@ declare class RunsClient extends BaseClient {
|
|
|
228
262
|
*/
|
|
229
263
|
get(threadId: string, runId: string): Promise<Run>;
|
|
230
264
|
/**
|
|
231
|
-
*
|
|
265
|
+
* Cancel a run.
|
|
232
266
|
*
|
|
233
267
|
* @param threadId The ID of the thread.
|
|
234
268
|
* @param runId The ID of the run.
|
|
235
|
-
* @param
|
|
236
|
-
* @returns
|
|
269
|
+
* @param wait Whether to block when canceling
|
|
270
|
+
* @returns
|
|
237
271
|
*/
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
272
|
+
cancel(threadId: string, runId: string, wait?: boolean): Promise<void>;
|
|
273
|
+
/**
|
|
274
|
+
* Block until a run is done.
|
|
275
|
+
*
|
|
276
|
+
* @param threadId The ID of the thread.
|
|
277
|
+
* @param runId The ID of the run.
|
|
278
|
+
* @returns
|
|
279
|
+
*/
|
|
280
|
+
join(threadId: string, runId: string): Promise<void>;
|
|
281
|
+
/**
|
|
282
|
+
* Delete a run.
|
|
283
|
+
*
|
|
284
|
+
* @param threadId The ID of the thread.
|
|
285
|
+
* @param runId The ID of the run.
|
|
286
|
+
* @returns
|
|
287
|
+
*/
|
|
288
|
+
delete(threadId: string, runId: string): Promise<void>;
|
|
250
289
|
}
|
|
251
290
|
export declare class Client {
|
|
252
291
|
/**
|
|
@@ -261,6 +300,10 @@ export declare class Client {
|
|
|
261
300
|
* The client for interacting with runs.
|
|
262
301
|
*/
|
|
263
302
|
runs: RunsClient;
|
|
303
|
+
/**
|
|
304
|
+
* The client for interacting with cron runs.
|
|
305
|
+
*/
|
|
306
|
+
crons: CronsClient;
|
|
264
307
|
constructor(config?: ClientConfig);
|
|
265
308
|
}
|
|
266
309
|
export {};
|
package/dist/client.mjs
CHANGED
|
@@ -21,6 +21,12 @@ class BaseClient {
|
|
|
21
21
|
writable: true,
|
|
22
22
|
value: void 0
|
|
23
23
|
});
|
|
24
|
+
Object.defineProperty(this, "defaultHeaders", {
|
|
25
|
+
enumerable: true,
|
|
26
|
+
configurable: true,
|
|
27
|
+
writable: true,
|
|
28
|
+
value: void 0
|
|
29
|
+
});
|
|
24
30
|
this.asyncCaller = new AsyncCaller({
|
|
25
31
|
maxRetries: 4,
|
|
26
32
|
maxConcurrency: 4,
|
|
@@ -28,9 +34,13 @@ class BaseClient {
|
|
|
28
34
|
});
|
|
29
35
|
this.timeoutMs = config?.timeoutMs || 12_000;
|
|
30
36
|
this.apiUrl = config?.apiUrl || "http://localhost:8123";
|
|
37
|
+
this.defaultHeaders = config?.defaultHeaders || {};
|
|
31
38
|
}
|
|
32
39
|
prepareFetchOptions(path, options) {
|
|
33
|
-
const mutatedOptions = {
|
|
40
|
+
const mutatedOptions = {
|
|
41
|
+
...options,
|
|
42
|
+
headers: { ...this.defaultHeaders, ...options?.headers },
|
|
43
|
+
};
|
|
34
44
|
if (mutatedOptions.json) {
|
|
35
45
|
mutatedOptions.body = JSON.stringify(mutatedOptions.json);
|
|
36
46
|
mutatedOptions.headers = {
|
|
@@ -55,9 +65,84 @@ class BaseClient {
|
|
|
55
65
|
}
|
|
56
66
|
async fetch(path, options) {
|
|
57
67
|
const response = await this.asyncCaller.fetch(...this.prepareFetchOptions(path, options));
|
|
68
|
+
if (response.status === 202 || response.status === 204) {
|
|
69
|
+
return undefined;
|
|
70
|
+
}
|
|
58
71
|
return response.json();
|
|
59
72
|
}
|
|
60
73
|
}
|
|
74
|
+
class CronsClient extends BaseClient {
|
|
75
|
+
/**
|
|
76
|
+
*
|
|
77
|
+
* @param threadId The ID of the thread.
|
|
78
|
+
* @param assistantId Assistant ID to use for this cron job.
|
|
79
|
+
* @param payload Payload for creating a cron job.
|
|
80
|
+
* @returns The created background run.
|
|
81
|
+
*/
|
|
82
|
+
async createForThread(threadId, assistantId, payload) {
|
|
83
|
+
const json = {
|
|
84
|
+
schedule: payload?.schedule,
|
|
85
|
+
input: payload?.input,
|
|
86
|
+
config: payload?.config,
|
|
87
|
+
metadata: payload?.metadata,
|
|
88
|
+
assistant_id: assistantId,
|
|
89
|
+
interrupt_before: payload?.interruptBefore,
|
|
90
|
+
interrupt_after: payload?.interruptAfter,
|
|
91
|
+
webhook: payload?.webhook,
|
|
92
|
+
};
|
|
93
|
+
return this.fetch(`/threads/${threadId}/runs/crons`, {
|
|
94
|
+
method: "POST",
|
|
95
|
+
json,
|
|
96
|
+
});
|
|
97
|
+
}
|
|
98
|
+
/**
|
|
99
|
+
*
|
|
100
|
+
* @param assistantId Assistant ID to use for this cron job.
|
|
101
|
+
* @param payload Payload for creating a cron job.
|
|
102
|
+
* @returns
|
|
103
|
+
*/
|
|
104
|
+
async create(assistantId, payload) {
|
|
105
|
+
const json = {
|
|
106
|
+
schedule: payload?.schedule,
|
|
107
|
+
input: payload?.input,
|
|
108
|
+
config: payload?.config,
|
|
109
|
+
metadata: payload?.metadata,
|
|
110
|
+
assistant_id: assistantId,
|
|
111
|
+
interrupt_before: payload?.interruptBefore,
|
|
112
|
+
interrupt_after: payload?.interruptAfter,
|
|
113
|
+
webhook: payload?.webhook,
|
|
114
|
+
};
|
|
115
|
+
return this.fetch(`/runs/crons`, {
|
|
116
|
+
method: "POST",
|
|
117
|
+
json,
|
|
118
|
+
});
|
|
119
|
+
}
|
|
120
|
+
/**
|
|
121
|
+
*
|
|
122
|
+
* @param cronId Cron ID of Cron job to delete.
|
|
123
|
+
*/
|
|
124
|
+
async delete(cronId) {
|
|
125
|
+
await this.fetch(`/runs/crons/${cronId}`, {
|
|
126
|
+
method: "DELETE",
|
|
127
|
+
});
|
|
128
|
+
}
|
|
129
|
+
/**
|
|
130
|
+
*
|
|
131
|
+
* @param query Query options.
|
|
132
|
+
* @returns List of crons.
|
|
133
|
+
*/
|
|
134
|
+
async search(query) {
|
|
135
|
+
return this.fetch("/runs/crons/search", {
|
|
136
|
+
method: "POST",
|
|
137
|
+
json: {
|
|
138
|
+
assistant_id: query?.assistantId ?? undefined,
|
|
139
|
+
thread_id: query?.threadId ?? undefined,
|
|
140
|
+
limit: query?.limit ?? 10,
|
|
141
|
+
offset: query?.offset ?? 0,
|
|
142
|
+
},
|
|
143
|
+
});
|
|
144
|
+
}
|
|
145
|
+
}
|
|
61
146
|
class AssistantsClient extends BaseClient {
|
|
62
147
|
/**
|
|
63
148
|
* Get an assistant by ID.
|
|
@@ -115,6 +200,16 @@ class AssistantsClient extends BaseClient {
|
|
|
115
200
|
},
|
|
116
201
|
});
|
|
117
202
|
}
|
|
203
|
+
/**
|
|
204
|
+
* Delete an assistant.
|
|
205
|
+
*
|
|
206
|
+
* @param assistantId ID of the assistant.
|
|
207
|
+
*/
|
|
208
|
+
async delete(assistantId) {
|
|
209
|
+
return this.fetch(`/assistants/${assistantId}`, {
|
|
210
|
+
method: "DELETE",
|
|
211
|
+
});
|
|
212
|
+
}
|
|
118
213
|
/**
|
|
119
214
|
* List assistants.
|
|
120
215
|
* @param query Query options.
|
|
@@ -124,6 +219,7 @@ class AssistantsClient extends BaseClient {
|
|
|
124
219
|
return this.fetch("/assistants/search", {
|
|
125
220
|
method: "POST",
|
|
126
221
|
json: {
|
|
222
|
+
graph_id: query?.graphId ?? undefined,
|
|
127
223
|
metadata: query?.metadata ?? undefined,
|
|
128
224
|
limit: query?.limit ?? 10,
|
|
129
225
|
offset: query?.offset ?? 0,
|
|
@@ -150,7 +246,11 @@ class ThreadsClient extends BaseClient {
|
|
|
150
246
|
async create(payload) {
|
|
151
247
|
return this.fetch(`/threads`, {
|
|
152
248
|
method: "POST",
|
|
153
|
-
json: {
|
|
249
|
+
json: {
|
|
250
|
+
metadata: payload?.metadata,
|
|
251
|
+
thread_id: payload?.threadId,
|
|
252
|
+
if_exists: payload?.ifExists,
|
|
253
|
+
},
|
|
154
254
|
});
|
|
155
255
|
}
|
|
156
256
|
/**
|
|
@@ -268,18 +368,23 @@ class RunsClient extends BaseClient {
|
|
|
268
368
|
* @param payload Payload for creating a run.
|
|
269
369
|
*/
|
|
270
370
|
async *stream(threadId, assistantId, payload) {
|
|
271
|
-
const
|
|
371
|
+
const json = {
|
|
372
|
+
input: payload?.input,
|
|
373
|
+
config: payload?.config,
|
|
374
|
+
metadata: payload?.metadata,
|
|
375
|
+
stream_mode: payload?.streamMode,
|
|
376
|
+
feedback_keys: payload?.feedbackKeys,
|
|
377
|
+
assistant_id: assistantId,
|
|
378
|
+
interrupt_before: payload?.interruptBefore,
|
|
379
|
+
interrupt_after: payload?.interruptAfter,
|
|
380
|
+
};
|
|
381
|
+
if (payload?.multitaskStrategy != null) {
|
|
382
|
+
json["multitask_strategy"] = payload?.multitaskStrategy;
|
|
383
|
+
}
|
|
384
|
+
const endpoint = threadId == null ? `/runs/stream` : `/threads/${threadId}/runs/stream`;
|
|
385
|
+
const response = await this.asyncCaller.fetch(...this.prepareFetchOptions(endpoint, {
|
|
272
386
|
method: "POST",
|
|
273
|
-
json
|
|
274
|
-
input: payload?.input,
|
|
275
|
-
config: payload?.config,
|
|
276
|
-
metadata: payload?.metadata,
|
|
277
|
-
stream_mode: payload?.streamMode,
|
|
278
|
-
feedback_keys: payload?.feedbackKeys,
|
|
279
|
-
assistant_id: assistantId,
|
|
280
|
-
interrupt_before: payload?.interruptBefore,
|
|
281
|
-
interrupt_after: payload?.interruptAfter,
|
|
282
|
-
},
|
|
387
|
+
json,
|
|
283
388
|
signal: payload?.signal,
|
|
284
389
|
}));
|
|
285
390
|
let parser;
|
|
@@ -315,17 +420,21 @@ class RunsClient extends BaseClient {
|
|
|
315
420
|
* @returns The created run.
|
|
316
421
|
*/
|
|
317
422
|
async create(threadId, assistantId, payload) {
|
|
423
|
+
const json = {
|
|
424
|
+
input: payload?.input,
|
|
425
|
+
config: payload?.config,
|
|
426
|
+
metadata: payload?.metadata,
|
|
427
|
+
assistant_id: assistantId,
|
|
428
|
+
interrupt_before: payload?.interruptBefore,
|
|
429
|
+
interrupt_after: payload?.interruptAfter,
|
|
430
|
+
webhook: payload?.webhook,
|
|
431
|
+
};
|
|
432
|
+
if (payload?.multitaskStrategy != null) {
|
|
433
|
+
json["multitask_strategy"] = payload?.multitaskStrategy;
|
|
434
|
+
}
|
|
318
435
|
return this.fetch(`/threads/${threadId}/runs`, {
|
|
319
436
|
method: "POST",
|
|
320
|
-
json
|
|
321
|
-
input: payload?.input,
|
|
322
|
-
config: payload?.config,
|
|
323
|
-
metadata: payload?.metadata,
|
|
324
|
-
assistant_id: assistantId,
|
|
325
|
-
interrupt_before: payload?.interruptBefore,
|
|
326
|
-
interrupt_after: payload?.interruptAfter,
|
|
327
|
-
webhook: payload?.webhook,
|
|
328
|
-
},
|
|
437
|
+
json,
|
|
329
438
|
signal: payload?.signal,
|
|
330
439
|
});
|
|
331
440
|
}
|
|
@@ -338,16 +447,21 @@ class RunsClient extends BaseClient {
|
|
|
338
447
|
* @returns The last values chunk of the thread.
|
|
339
448
|
*/
|
|
340
449
|
async wait(threadId, assistantId, payload) {
|
|
341
|
-
|
|
450
|
+
const json = {
|
|
451
|
+
input: payload?.input,
|
|
452
|
+
config: payload?.config,
|
|
453
|
+
metadata: payload?.metadata,
|
|
454
|
+
assistant_id: assistantId,
|
|
455
|
+
interrupt_before: payload?.interruptBefore,
|
|
456
|
+
interrupt_after: payload?.interruptAfter,
|
|
457
|
+
};
|
|
458
|
+
if (payload?.multitaskStrategy != null) {
|
|
459
|
+
json["multitask_strategy"] = payload?.multitaskStrategy;
|
|
460
|
+
}
|
|
461
|
+
const endpoint = threadId == null ? `/runs/wait` : `/threads/${threadId}/runs/wait`;
|
|
462
|
+
return this.fetch(endpoint, {
|
|
342
463
|
method: "POST",
|
|
343
|
-
json
|
|
344
|
-
input: payload?.input,
|
|
345
|
-
config: payload?.config,
|
|
346
|
-
metadata: payload?.metadata,
|
|
347
|
-
assistant_id: assistantId,
|
|
348
|
-
interrupt_before: payload?.interruptBefore,
|
|
349
|
-
interrupt_after: payload?.interruptAfter,
|
|
350
|
-
},
|
|
464
|
+
json,
|
|
351
465
|
signal: payload?.signal,
|
|
352
466
|
});
|
|
353
467
|
}
|
|
@@ -377,21 +491,43 @@ class RunsClient extends BaseClient {
|
|
|
377
491
|
return this.fetch(`/threads/${threadId}/runs/${runId}`);
|
|
378
492
|
}
|
|
379
493
|
/**
|
|
380
|
-
*
|
|
494
|
+
* Cancel a run.
|
|
381
495
|
*
|
|
382
496
|
* @param threadId The ID of the thread.
|
|
383
497
|
* @param runId The ID of the run.
|
|
384
|
-
* @param
|
|
385
|
-
* @returns
|
|
498
|
+
* @param wait Whether to block when canceling
|
|
499
|
+
* @returns
|
|
386
500
|
*/
|
|
387
|
-
async
|
|
388
|
-
return this.fetch(`/threads/${threadId}/runs/${runId}/
|
|
501
|
+
async cancel(threadId, runId, wait = false) {
|
|
502
|
+
return this.fetch(`/threads/${threadId}/runs/${runId}/cancel`, {
|
|
503
|
+
method: "POST",
|
|
389
504
|
params: {
|
|
390
|
-
|
|
391
|
-
offset: options?.offset ?? 0,
|
|
505
|
+
wait: wait ? "1" : "0",
|
|
392
506
|
},
|
|
393
507
|
});
|
|
394
508
|
}
|
|
509
|
+
/**
|
|
510
|
+
* Block until a run is done.
|
|
511
|
+
*
|
|
512
|
+
* @param threadId The ID of the thread.
|
|
513
|
+
* @param runId The ID of the run.
|
|
514
|
+
* @returns
|
|
515
|
+
*/
|
|
516
|
+
async join(threadId, runId) {
|
|
517
|
+
return this.fetch(`/threads/${threadId}/runs/${runId}/join`);
|
|
518
|
+
}
|
|
519
|
+
/**
|
|
520
|
+
* Delete a run.
|
|
521
|
+
*
|
|
522
|
+
* @param threadId The ID of the thread.
|
|
523
|
+
* @param runId The ID of the run.
|
|
524
|
+
* @returns
|
|
525
|
+
*/
|
|
526
|
+
async delete(threadId, runId) {
|
|
527
|
+
return this.fetch(`/threads/${threadId}/runs/${runId}`, {
|
|
528
|
+
method: "DELETE",
|
|
529
|
+
});
|
|
530
|
+
}
|
|
395
531
|
}
|
|
396
532
|
export class Client {
|
|
397
533
|
constructor(config) {
|
|
@@ -422,8 +558,18 @@ export class Client {
|
|
|
422
558
|
writable: true,
|
|
423
559
|
value: void 0
|
|
424
560
|
});
|
|
561
|
+
/**
|
|
562
|
+
* The client for interacting with cron runs.
|
|
563
|
+
*/
|
|
564
|
+
Object.defineProperty(this, "crons", {
|
|
565
|
+
enumerable: true,
|
|
566
|
+
configurable: true,
|
|
567
|
+
writable: true,
|
|
568
|
+
value: void 0
|
|
569
|
+
});
|
|
425
570
|
this.assistants = new AssistantsClient(config);
|
|
426
571
|
this.threads = new ThreadsClient(config);
|
|
427
572
|
this.runs = new RunsClient(config);
|
|
573
|
+
this.crons = new CronsClient(config);
|
|
428
574
|
}
|
|
429
575
|
}
|
package/dist/index.d.mts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
export { Client } from "./client.mjs";
|
|
2
|
-
export type { Assistant, AssistantGraph, Config, DefaultValues, GraphSchema, Metadata, Run,
|
|
2
|
+
export type { Assistant, AssistantGraph, Config, DefaultValues, GraphSchema, Metadata, Run, Thread, ThreadState, Cron, } from "./schema.js";
|
package/dist/schema.d.ts
CHANGED
|
@@ -56,6 +56,15 @@ export interface Thread {
|
|
|
56
56
|
updated_at: string;
|
|
57
57
|
metadata: Metadata;
|
|
58
58
|
}
|
|
59
|
+
export interface Cron {
|
|
60
|
+
cron_id: string;
|
|
61
|
+
thread_id: Optional<string>;
|
|
62
|
+
end_time: Optional<string>;
|
|
63
|
+
schedule: string;
|
|
64
|
+
created_at: string;
|
|
65
|
+
updated_at: string;
|
|
66
|
+
payload: Record<string, unknown>;
|
|
67
|
+
}
|
|
59
68
|
export type DefaultValues = Record<string, unknown>[] | Record<string, unknown>;
|
|
60
69
|
export interface ThreadState<ValuesType = DefaultValues> {
|
|
61
70
|
values: ValuesType;
|
|
@@ -74,15 +83,4 @@ export interface Run {
|
|
|
74
83
|
status: "pending" | "running" | "error" | "success" | "timeout" | "interrupted";
|
|
75
84
|
metadata: Metadata;
|
|
76
85
|
}
|
|
77
|
-
export interface RunEvent {
|
|
78
|
-
event_id: string;
|
|
79
|
-
run_id: string;
|
|
80
|
-
received_at: string;
|
|
81
|
-
span_id: string;
|
|
82
|
-
event: string;
|
|
83
|
-
name: string;
|
|
84
|
-
data: Record<string, unknown>;
|
|
85
|
-
metadata: Record<string, unknown>;
|
|
86
|
-
tags: string[];
|
|
87
|
-
}
|
|
88
86
|
export {};
|
package/dist/types.d.mts
CHANGED
|
@@ -1,5 +1,8 @@
|
|
|
1
1
|
import { Config, Metadata } from "./schema.js";
|
|
2
2
|
export type StreamMode = "values" | "messages" | "updates" | "events" | "debug";
|
|
3
|
+
export type MultitaskStrategy = "reject" | "interrupt" | "rollback" | "enqueue";
|
|
4
|
+
export type OnConflictBehavior = "raise" | "do_nothing";
|
|
5
|
+
export type StreamEvent = "events" | "metadata" | "debug" | "updates" | "values" | "messages/partial" | "messages/metadata" | "messages/complete" | (string & {});
|
|
3
6
|
interface RunsInvokePayload {
|
|
4
7
|
/**
|
|
5
8
|
* Input to the run. Pass `null` to resume from the current state of the thread.
|
|
@@ -21,6 +24,17 @@ interface RunsInvokePayload {
|
|
|
21
24
|
* Interrupt execution after leaving these nodes.
|
|
22
25
|
*/
|
|
23
26
|
interruptAfter?: string[];
|
|
27
|
+
/**
|
|
28
|
+
* Strategy to handle concurrent runs on the same thread. Only relevant if
|
|
29
|
+
* there is a pending/inflight run on the same thread. One of:
|
|
30
|
+
* - "reject": Reject the new run.
|
|
31
|
+
* - "interrupt": Interrupt the current run, keeping steps completed until now,
|
|
32
|
+
and start a new one.
|
|
33
|
+
* - "rollback": Cancel and delete the existing run, rolling back the thread to
|
|
34
|
+
the state before it had started, then start the new run.
|
|
35
|
+
* - "enqueue": Queue up the new run to start after the current run finishes.
|
|
36
|
+
*/
|
|
37
|
+
multitaskStrategy?: MultitaskStrategy;
|
|
24
38
|
/**
|
|
25
39
|
* Abort controller signal to cancel the run.
|
|
26
40
|
*/
|
|
@@ -49,5 +63,11 @@ export interface RunsCreatePayload extends RunsInvokePayload {
|
|
|
49
63
|
*/
|
|
50
64
|
webhook?: string;
|
|
51
65
|
}
|
|
66
|
+
export interface CronsCreatePayload extends RunsCreatePayload {
|
|
67
|
+
/**
|
|
68
|
+
* Schedule for running the Cron Job
|
|
69
|
+
*/
|
|
70
|
+
schedule: string;
|
|
71
|
+
}
|
|
52
72
|
export type RunsWaitPayload = RunsStreamPayload;
|
|
53
73
|
export {};
|
|
@@ -11,6 +11,12 @@ export interface AsyncCallerParams {
|
|
|
11
11
|
*/
|
|
12
12
|
maxRetries?: number;
|
|
13
13
|
onFailedResponseHook?: ResponseCallback;
|
|
14
|
+
/**
|
|
15
|
+
* Specify a custom fetch implementation.
|
|
16
|
+
*
|
|
17
|
+
* By default we expect the `fetch` is available in the global scope.
|
|
18
|
+
*/
|
|
19
|
+
fetch?: typeof fetch;
|
|
14
20
|
}
|
|
15
21
|
export interface AsyncCallerCallOptions {
|
|
16
22
|
signal?: AbortSignal;
|
|
@@ -33,6 +39,7 @@ export declare class AsyncCaller {
|
|
|
33
39
|
protected maxRetries: AsyncCallerParams["maxRetries"];
|
|
34
40
|
private queue;
|
|
35
41
|
private onFailedResponseHook?;
|
|
42
|
+
private customFetch?;
|
|
36
43
|
constructor(params: AsyncCallerParams);
|
|
37
44
|
call<A extends any[], T extends (...args: A) => Promise<any>>(callable: T, ...args: Parameters<T>): Promise<Awaited<ReturnType<T>>>;
|
|
38
45
|
callWithOptions<A extends any[], T extends (...args: A) => Promise<any>>(options: AsyncCallerCallOptions, callable: T, ...args: Parameters<T>): Promise<Awaited<ReturnType<T>>>;
|
|
@@ -99,6 +99,12 @@ export class AsyncCaller {
|
|
|
99
99
|
writable: true,
|
|
100
100
|
value: void 0
|
|
101
101
|
});
|
|
102
|
+
Object.defineProperty(this, "customFetch", {
|
|
103
|
+
enumerable: true,
|
|
104
|
+
configurable: true,
|
|
105
|
+
writable: true,
|
|
106
|
+
value: void 0
|
|
107
|
+
});
|
|
102
108
|
this.maxConcurrency = params.maxConcurrency ?? Infinity;
|
|
103
109
|
this.maxRetries = params.maxRetries ?? 4;
|
|
104
110
|
if ("default" in PQueueMod) {
|
|
@@ -112,6 +118,7 @@ export class AsyncCaller {
|
|
|
112
118
|
this.queue = new PQueueMod({ concurrency: this.maxConcurrency });
|
|
113
119
|
}
|
|
114
120
|
this.onFailedResponseHook = params?.onFailedResponseHook;
|
|
121
|
+
this.customFetch = params.fetch;
|
|
115
122
|
}
|
|
116
123
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
117
124
|
call(callable, ...args) {
|
|
@@ -175,6 +182,7 @@ export class AsyncCaller {
|
|
|
175
182
|
return this.call(callable, ...args);
|
|
176
183
|
}
|
|
177
184
|
fetch(...args) {
|
|
178
|
-
|
|
185
|
+
const fetchFn = this.customFetch ?? fetch;
|
|
186
|
+
return this.call(() => fetchFn(...args).then((res) => (res.ok ? res : Promise.reject(res))));
|
|
179
187
|
}
|
|
180
188
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@langchain/langgraph-sdk",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.2",
|
|
4
4
|
"description": "Client library for interacting with the LangGraph API",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"packageManager": "yarn@1.22.19",
|
|
@@ -26,7 +26,10 @@
|
|
|
26
26
|
"@tsconfig/recommended": "^1.0.2",
|
|
27
27
|
"@types/node": "^20.12.12",
|
|
28
28
|
"@types/uuid": "^9.0.1",
|
|
29
|
+
"concat-md": "^0.5.1",
|
|
29
30
|
"prettier": "^3.2.5",
|
|
31
|
+
"typedoc": "^0.26.1",
|
|
32
|
+
"typedoc-plugin-markdown": "^4.1.0",
|
|
30
33
|
"typescript": "^5.4.5"
|
|
31
34
|
},
|
|
32
35
|
"exports": {
|