@langchain/langgraph-sdk 0.0.1-rc.1 → 0.0.1-rc.11

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 CHANGED
@@ -1,15 +1,17 @@
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 { RunsCreatePayload, RunsStreamPayload, RunsWaitPayload } from "./types.mjs";
3
+ import { RunsCreatePayload, RunsStreamPayload, RunsWaitPayload, StreamEvent } 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;
@@ -21,19 +23,57 @@ declare class BaseClient {
21
23
  }): Promise<T>;
22
24
  }
23
25
  declare class AssistantsClient extends BaseClient {
26
+ /**
27
+ * Get an assistant by ID.
28
+ *
29
+ * @param assistantId The ID of the assistant.
30
+ * @returns Assistant
31
+ */
24
32
  get(assistantId: string): Promise<Assistant>;
33
+ /**
34
+ * Get the JSON representation of the graph assigned to a runnable
35
+ * @param assistantId The ID of the assistant.
36
+ * @returns Serialized graph
37
+ */
25
38
  getGraph(assistantId: string): Promise<AssistantGraph>;
39
+ /**
40
+ * Get the state and config schema of the graph assigned to a runnable
41
+ * @param assistantId The ID of the assistant.
42
+ * @returns Graph schema
43
+ */
26
44
  getSchemas(assistantId: string): Promise<GraphSchema>;
45
+ /**
46
+ * Create a new assistant.
47
+ * @param payload Payload for creating an assistant.
48
+ * @returns The created assistant.
49
+ */
27
50
  create(payload: {
28
51
  graphId: string;
29
52
  config?: Config;
30
53
  metadata?: Metadata;
31
54
  }): Promise<Assistant>;
32
- upsert(assistantId: string, payload: {
55
+ /**
56
+ * Update an assistant.
57
+ * @param assistantId ID of the assistant.
58
+ * @param payload Payload for updating the assistant.
59
+ * @returns The updated assistant.
60
+ */
61
+ update(assistantId: string, payload: {
33
62
  graphId: string;
34
63
  config?: Config;
35
64
  metadata?: Metadata;
36
65
  }): Promise<Assistant>;
66
+ /**
67
+ * Delete an assistant.
68
+ *
69
+ * @param assistantId ID of the assistant.
70
+ */
71
+ delete(assistantId: string): Promise<void>;
72
+ /**
73
+ * List assistants.
74
+ * @param query Query options.
75
+ * @returns List of assistants.
76
+ */
37
77
  search(query?: {
38
78
  metadata?: Metadata;
39
79
  limit?: number;
@@ -54,7 +94,7 @@ declare class ThreadsClient extends BaseClient {
54
94
  * @param payload Payload for creating a thread.
55
95
  * @returns The created thread.
56
96
  */
57
- create(payload: {
97
+ create(payload?: {
58
98
  /**
59
99
  * Metadata for the thread.
60
100
  */
@@ -67,7 +107,7 @@ declare class ThreadsClient extends BaseClient {
67
107
  * @param payload Payload for updating the thread.
68
108
  * @returns The updated thread.
69
109
  */
70
- upsert(threadId: string, payload?: {
110
+ update(threadId: string, payload?: {
71
111
  /**
72
112
  * Metadata for the thread.
73
113
  */
@@ -106,20 +146,20 @@ declare class ThreadsClient extends BaseClient {
106
146
  * @param threadId ID of the thread.
107
147
  * @returns Thread state.
108
148
  */
109
- getState(threadId: string): Promise<ThreadState>;
149
+ getState<ValuesType = DefaultValues>(threadId: string, checkpointId?: string): Promise<ThreadState<ValuesType>>;
110
150
  /**
111
151
  * Add state to a thread.
112
152
  *
113
- * @param threadIdOrConfig Thread ID or config that identifies the state to start from.
114
- * @param values The state update
115
- * @param options Additional options.
153
+ * @param threadId The ID of the thread.
116
154
  * @returns
117
155
  */
118
- updateState(threadIdOrConfig: string | Config, values: Record<string, unknown>, options?: {
156
+ updateState<ValuesType = DefaultValues>(threadId: string, options: {
157
+ values: ValuesType;
158
+ checkpointId?: string;
119
159
  asNode?: string;
120
160
  }): Promise<void>;
121
161
  /**
122
- * Patch the state of a thread.
162
+ * Patch the metadata of a thread.
123
163
  *
124
164
  * @param threadIdOrConfig Thread ID or config to patch the state of.
125
165
  * @param metadata Metadata to patch the state with.
@@ -132,22 +172,20 @@ declare class ThreadsClient extends BaseClient {
132
172
  * @param options Additional options.
133
173
  * @returns List of thread states.
134
174
  */
135
- getHistory(threadId: string, options?: {
175
+ getHistory<ValuesType = DefaultValues>(threadId: string, options?: {
136
176
  limit?: number;
137
177
  before?: Config;
138
- }): Promise<ThreadState[]>;
178
+ metadata?: Metadata;
179
+ }): Promise<ThreadState<ValuesType>[]>;
139
180
  }
140
181
  declare class RunsClient extends BaseClient {
141
- /**
142
- * Create a run and stream the results.
143
- *
144
- * @param threadId The ID of the thread.
145
- * @param assistantId Assistant ID to use for this run.
146
- * @param payload Payload for creating a run.
147
- */
182
+ stream(threadId: null, assistantId: string, payload?: Omit<RunsStreamPayload, "multitaskStrategy">): AsyncGenerator<{
183
+ event: StreamEvent;
184
+ data: any;
185
+ }>;
148
186
  stream(threadId: string, assistantId: string, payload?: RunsStreamPayload): AsyncGenerator<{
149
- event: string;
150
- data: unknown;
187
+ event: StreamEvent;
188
+ data: any;
151
189
  }>;
152
190
  /**
153
191
  * Create a run.
@@ -158,14 +196,7 @@ declare class RunsClient extends BaseClient {
158
196
  * @returns The created run.
159
197
  */
160
198
  create(threadId: string, assistantId: string, payload?: RunsCreatePayload): Promise<Run>;
161
- /**
162
- * Create a run and wait for it to complete.
163
- *
164
- * @param threadId The ID of the thread.
165
- * @param assistantId Assistant ID to use for this run.
166
- * @param payload Payload for creating a run.
167
- * @returns The last values chunk of the thread.
168
- */
199
+ wait(threadId: null, assistantId: string, payload?: Omit<RunsWaitPayload, "multitaskStrategy">): Promise<ThreadState["values"]>;
169
200
  wait(threadId: string, assistantId: string, payload?: RunsWaitPayload): Promise<ThreadState["values"]>;
170
201
  /**
171
202
  * List all runs for a thread.
@@ -194,6 +225,23 @@ declare class RunsClient extends BaseClient {
194
225
  * @returns The run.
195
226
  */
196
227
  get(threadId: string, runId: string): Promise<Run>;
228
+ /**
229
+ * Cancel a run.
230
+ *
231
+ * @param threadId The ID of the thread.
232
+ * @param runId The ID of the run.
233
+ * @param wait Whether to block when canceling
234
+ * @returns
235
+ */
236
+ cancel(threadId: string, runId: string, wait?: boolean): Promise<void>;
237
+ /**
238
+ * Block until a run is done.
239
+ *
240
+ * @param threadId The ID of the thread.
241
+ * @param runId The ID of the run.
242
+ * @returns
243
+ */
244
+ join(threadId: string, runId: string): Promise<void>;
197
245
  /**
198
246
  * List all events for a run.
199
247
  *
@@ -214,10 +262,27 @@ declare class RunsClient extends BaseClient {
214
262
  */
215
263
  offset?: number;
216
264
  }): Promise<RunEvent[]>;
265
+ /**
266
+ * Delete a run.
267
+ *
268
+ * @param threadId The ID of the thread.
269
+ * @param runId The ID of the run.
270
+ * @returns
271
+ */
272
+ delete(threadId: string, runId: string): Promise<void>;
217
273
  }
218
274
  export declare class Client {
275
+ /**
276
+ * The client for interacting with assistants.
277
+ */
219
278
  assistants: AssistantsClient;
279
+ /**
280
+ * The client for interacting with threads.
281
+ */
220
282
  threads: ThreadsClient;
283
+ /**
284
+ * The client for interacting with runs.
285
+ */
221
286
  runs: RunsClient;
222
287
  constructor(config?: ClientConfig);
223
288
  }
package/dist/client.mjs CHANGED
@@ -21,16 +21,26 @@ 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
- maxRetries: 5,
26
- maxConcurrency: 5,
31
+ maxRetries: 4,
32
+ maxConcurrency: 4,
27
33
  ...config?.callerOptions,
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 = { ...options };
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,19 +65,43 @@ 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) {
69
+ return undefined;
70
+ }
58
71
  return response.json();
59
72
  }
60
73
  }
61
74
  class AssistantsClient extends BaseClient {
75
+ /**
76
+ * Get an assistant by ID.
77
+ *
78
+ * @param assistantId The ID of the assistant.
79
+ * @returns Assistant
80
+ */
62
81
  async get(assistantId) {
63
82
  return this.fetch(`/assistants/${assistantId}`);
64
83
  }
84
+ /**
85
+ * Get the JSON representation of the graph assigned to a runnable
86
+ * @param assistantId The ID of the assistant.
87
+ * @returns Serialized graph
88
+ */
65
89
  async getGraph(assistantId) {
66
90
  return this.fetch(`/assistants/${assistantId}/graph`);
67
91
  }
92
+ /**
93
+ * Get the state and config schema of the graph assigned to a runnable
94
+ * @param assistantId The ID of the assistant.
95
+ * @returns Graph schema
96
+ */
68
97
  async getSchemas(assistantId) {
69
98
  return this.fetch(`/assistants/${assistantId}/schemas`);
70
99
  }
100
+ /**
101
+ * Create a new assistant.
102
+ * @param payload Payload for creating an assistant.
103
+ * @returns The created assistant.
104
+ */
71
105
  async create(payload) {
72
106
  return this.fetch("/assistants", {
73
107
  method: "POST",
@@ -78,9 +112,15 @@ class AssistantsClient extends BaseClient {
78
112
  },
79
113
  });
80
114
  }
81
- async upsert(assistantId, payload) {
115
+ /**
116
+ * Update an assistant.
117
+ * @param assistantId ID of the assistant.
118
+ * @param payload Payload for updating the assistant.
119
+ * @returns The updated assistant.
120
+ */
121
+ async update(assistantId, payload) {
82
122
  return this.fetch(`/assistants/${assistantId}`, {
83
- method: "PUT",
123
+ method: "PATCH",
84
124
  json: {
85
125
  graph_id: payload.graphId,
86
126
  config: payload.config,
@@ -88,6 +128,21 @@ class AssistantsClient extends BaseClient {
88
128
  },
89
129
  });
90
130
  }
131
+ /**
132
+ * Delete an assistant.
133
+ *
134
+ * @param assistantId ID of the assistant.
135
+ */
136
+ async delete(assistantId) {
137
+ return this.fetch(`/assistants/${assistantId}`, {
138
+ method: "DELETE",
139
+ });
140
+ }
141
+ /**
142
+ * List assistants.
143
+ * @param query Query options.
144
+ * @returns List of assistants.
145
+ */
91
146
  async search(query) {
92
147
  return this.fetch("/assistants/search", {
93
148
  method: "POST",
@@ -118,7 +173,7 @@ class ThreadsClient extends BaseClient {
118
173
  async create(payload) {
119
174
  return this.fetch(`/threads`, {
120
175
  method: "POST",
121
- json: { metadata: payload },
176
+ json: { metadata: payload?.metadata },
122
177
  });
123
178
  }
124
179
  /**
@@ -128,10 +183,10 @@ class ThreadsClient extends BaseClient {
128
183
  * @param payload Payload for updating the thread.
129
184
  * @returns The updated thread.
130
185
  */
131
- async upsert(threadId, payload) {
186
+ async update(threadId, payload) {
132
187
  return this.fetch(`/threads/${threadId}`, {
133
- method: "PUT",
134
- json: { metadata: payload },
188
+ method: "PATCH",
189
+ json: { metadata: payload?.metadata },
135
190
  });
136
191
  }
137
192
  /**
@@ -166,38 +221,29 @@ class ThreadsClient extends BaseClient {
166
221
  * @param threadId ID of the thread.
167
222
  * @returns Thread state.
168
223
  */
169
- async getState(threadId) {
170
- return this.fetch(`/threads/${threadId}/state`);
224
+ async getState(threadId, checkpointId) {
225
+ return this.fetch(checkpointId != null
226
+ ? `/threads/${threadId}/state/${checkpointId}`
227
+ : `/threads/${threadId}/state`);
171
228
  }
172
229
  /**
173
230
  * Add state to a thread.
174
231
  *
175
- * @param threadIdOrConfig Thread ID or config that identifies the state to start from.
176
- * @param values The state update
177
- * @param options Additional options.
232
+ * @param threadId The ID of the thread.
178
233
  * @returns
179
234
  */
180
- async updateState(threadIdOrConfig, values, options) {
181
- let config = undefined;
182
- let threadId;
183
- if (typeof threadIdOrConfig !== "string") {
184
- config = threadIdOrConfig;
185
- if (typeof config.configurable?.thread_id !== "string") {
186
- throw new Error("Thread ID is required when updating state with a config.");
187
- }
188
- threadId = config.configurable.thread_id;
189
- }
190
- else {
191
- config = undefined;
192
- threadId = threadIdOrConfig;
193
- }
235
+ async updateState(threadId, options) {
194
236
  return this.fetch(`/threads/${threadId}/state`, {
195
237
  method: "POST",
196
- json: { values, config, as_node: options?.asNode },
238
+ json: {
239
+ values: options.values,
240
+ checkpoint_id: options.checkpointId,
241
+ as_node: options?.asNode,
242
+ },
197
243
  });
198
244
  }
199
245
  /**
200
- * Patch the state of a thread.
246
+ * Patch the metadata of a thread.
201
247
  *
202
248
  * @param threadIdOrConfig Thread ID or config to patch the state of.
203
249
  * @param metadata Metadata to patch the state with.
@@ -227,9 +273,11 @@ class ThreadsClient extends BaseClient {
227
273
  */
228
274
  async getHistory(threadId, options) {
229
275
  return this.fetch(`/threads/${threadId}/history`, {
230
- params: {
276
+ method: "POST",
277
+ json: {
231
278
  limit: options?.limit ?? 10,
232
279
  before: options?.before,
280
+ metadata: options?.metadata,
233
281
  },
234
282
  });
235
283
  }
@@ -243,24 +291,32 @@ class RunsClient extends BaseClient {
243
291
  * @param payload Payload for creating a run.
244
292
  */
245
293
  async *stream(threadId, assistantId, payload) {
246
- const response = await this.asyncCaller.fetch(...this.prepareFetchOptions(`/threads/${threadId}/runs/stream`, {
294
+ const json = {
295
+ input: payload?.input,
296
+ config: payload?.config,
297
+ metadata: payload?.metadata,
298
+ stream_mode: payload?.streamMode,
299
+ feedback_keys: payload?.feedbackKeys,
300
+ assistant_id: assistantId,
301
+ interrupt_before: payload?.interruptBefore,
302
+ interrupt_after: payload?.interruptAfter,
303
+ };
304
+ if (payload?.multitaskStrategy != null) {
305
+ json["multitask_strategy"] = payload?.multitaskStrategy;
306
+ }
307
+ const endpoint = threadId == null ? `/runs/stream` : `/threads/${threadId}/runs/stream`;
308
+ const response = await this.asyncCaller.fetch(...this.prepareFetchOptions(endpoint, {
247
309
  method: "POST",
248
- json: {
249
- input: payload?.input,
250
- config: payload?.config,
251
- metadata: payload?.metadata,
252
- stream_mode: payload?.streamMode,
253
- assistant_id: assistantId,
254
- interrupt_before: payload?.interruptBefore,
255
- interrupt_after: payload?.interruptAfter,
256
- },
310
+ json,
311
+ signal: payload?.signal,
257
312
  }));
258
313
  let parser;
259
314
  const textDecoder = new TextDecoder();
260
315
  const stream = (response.body || new ReadableStream({ start: (ctrl) => ctrl.close() })).pipeThrough(new TransformStream({
261
316
  async start(ctrl) {
262
317
  parser = createParser((event) => {
263
- if (event.type === "event" && event.data === "[DONE]") {
318
+ if ((payload?.signal && payload.signal.aborted) ||
319
+ (event.type === "event" && event.data === "[DONE]")) {
264
320
  ctrl.terminate();
265
321
  return;
266
322
  }
@@ -287,17 +343,22 @@ class RunsClient extends BaseClient {
287
343
  * @returns The created run.
288
344
  */
289
345
  async create(threadId, assistantId, payload) {
346
+ const json = {
347
+ input: payload?.input,
348
+ config: payload?.config,
349
+ metadata: payload?.metadata,
350
+ assistant_id: assistantId,
351
+ interrupt_before: payload?.interruptBefore,
352
+ interrupt_after: payload?.interruptAfter,
353
+ webhook: payload?.webhook,
354
+ };
355
+ if (payload?.multitaskStrategy != null) {
356
+ json["multitask_strategy"] = payload?.multitaskStrategy;
357
+ }
290
358
  return this.fetch(`/threads/${threadId}/runs`, {
291
359
  method: "POST",
292
- json: {
293
- input: payload?.input,
294
- config: payload?.config,
295
- metadata: payload?.metadata,
296
- assistant_id: assistantId,
297
- interrupt_before: payload?.interruptBefore,
298
- interrupt_after: payload?.interruptAfter,
299
- webhook: payload?.webhook,
300
- },
360
+ json,
361
+ signal: payload?.signal,
301
362
  });
302
363
  }
303
364
  /**
@@ -309,16 +370,22 @@ class RunsClient extends BaseClient {
309
370
  * @returns The last values chunk of the thread.
310
371
  */
311
372
  async wait(threadId, assistantId, payload) {
312
- return this.fetch(`/threads/${threadId}/runs/wait`, {
373
+ const json = {
374
+ input: payload?.input,
375
+ config: payload?.config,
376
+ metadata: payload?.metadata,
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/wait` : `/threads/${threadId}/runs/wait`;
385
+ return this.fetch(endpoint, {
313
386
  method: "POST",
314
- json: {
315
- input: payload?.input,
316
- config: payload?.config,
317
- metadata: payload?.metadata,
318
- assistant_id: assistantId,
319
- interrupt_before: payload?.interruptBefore,
320
- interrupt_after: payload?.interruptAfter,
321
- },
387
+ json,
388
+ signal: payload?.signal,
322
389
  });
323
390
  }
324
391
  /**
@@ -346,6 +413,32 @@ class RunsClient extends BaseClient {
346
413
  async get(threadId, runId) {
347
414
  return this.fetch(`/threads/${threadId}/runs/${runId}`);
348
415
  }
416
+ /**
417
+ * Cancel a run.
418
+ *
419
+ * @param threadId The ID of the thread.
420
+ * @param runId The ID of the run.
421
+ * @param wait Whether to block when canceling
422
+ * @returns
423
+ */
424
+ async cancel(threadId, runId, wait = false) {
425
+ return this.fetch(`/threads/${threadId}/runs/${runId}/cancel`, {
426
+ method: "POST",
427
+ params: {
428
+ wait: wait ? "1" : "0",
429
+ },
430
+ });
431
+ }
432
+ /**
433
+ * Block until a run is done.
434
+ *
435
+ * @param threadId The ID of the thread.
436
+ * @param runId The ID of the run.
437
+ * @returns
438
+ */
439
+ async join(threadId, runId) {
440
+ return this.fetch(`/threads/${threadId}/runs/${runId}/join`);
441
+ }
349
442
  /**
350
443
  * List all events for a run.
351
444
  *
@@ -362,21 +455,42 @@ class RunsClient extends BaseClient {
362
455
  },
363
456
  });
364
457
  }
458
+ /**
459
+ * Delete a run.
460
+ *
461
+ * @param threadId The ID of the thread.
462
+ * @param runId The ID of the run.
463
+ * @returns
464
+ */
465
+ async delete(threadId, runId) {
466
+ return this.fetch(`/threads/${threadId}/runs/${runId}`, {
467
+ method: "DELETE",
468
+ });
469
+ }
365
470
  }
366
471
  export class Client {
367
472
  constructor(config) {
473
+ /**
474
+ * The client for interacting with assistants.
475
+ */
368
476
  Object.defineProperty(this, "assistants", {
369
477
  enumerable: true,
370
478
  configurable: true,
371
479
  writable: true,
372
480
  value: void 0
373
481
  });
482
+ /**
483
+ * The client for interacting with threads.
484
+ */
374
485
  Object.defineProperty(this, "threads", {
375
486
  enumerable: true,
376
487
  configurable: true,
377
488
  writable: true,
378
489
  value: void 0
379
490
  });
491
+ /**
492
+ * The client for interacting with runs.
493
+ */
380
494
  Object.defineProperty(this, "runs", {
381
495
  enumerable: true,
382
496
  configurable: true,
package/dist/index.d.mts CHANGED
@@ -1 +1,2 @@
1
1
  export { Client } from "./client.mjs";
2
+ export type { Assistant, AssistantGraph, Config, DefaultValues, GraphSchema, Metadata, Run, RunEvent, Thread, ThreadState, } from "./schema.js";
package/dist/schema.d.ts CHANGED
@@ -1,3 +1,4 @@
1
+ import type { JSONSchema7 } from "json-schema";
1
2
  type Optional<T> = T | null | undefined;
2
3
  export interface Config {
3
4
  /**
@@ -14,14 +15,30 @@ export interface Config {
14
15
  * Runtime values for attributes previously made configurable on this Runnable.
15
16
  */
16
17
  configurable: {
18
+ /**
19
+ * ID of the thread
20
+ */
17
21
  thread_id?: string;
22
+ /**
23
+ * Timestamp of the state checkpoint
24
+ */
25
+ thread_ts?: string;
18
26
  [key: string]: unknown;
19
27
  };
20
28
  }
21
29
  export interface GraphSchema {
30
+ /**
31
+ * The ID of the graph.
32
+ */
22
33
  graph_id: string;
23
- state_schema: Record<string, unknown>;
24
- config_schema: Record<string, unknown>;
34
+ /**
35
+ * The schema for the graph state
36
+ */
37
+ state_schema: JSONSchema7;
38
+ /**
39
+ * The schema for the graph config
40
+ */
41
+ config_schema: JSONSchema7;
25
42
  }
26
43
  export type Metadata = Optional<Record<string, unknown>>;
27
44
  export interface Assistant {
@@ -39,13 +56,14 @@ export interface Thread {
39
56
  updated_at: string;
40
57
  metadata: Metadata;
41
58
  }
42
- export interface ThreadState {
43
- values: Record<string, unknown>[] | Record<string, unknown>;
59
+ export type DefaultValues = Record<string, unknown>[] | Record<string, unknown>;
60
+ export interface ThreadState<ValuesType = DefaultValues> {
61
+ values: ValuesType;
44
62
  next: string[];
45
- config: Config;
63
+ checkpoint_id: string;
46
64
  metadata: Metadata;
47
65
  created_at: Optional<string>;
48
- parent_config: Optional<Config>;
66
+ parent_checkpoint_id: Optional<string>;
49
67
  }
50
68
  export interface Run {
51
69
  run_id: string;
package/dist/types.d.mts CHANGED
@@ -1,5 +1,7 @@
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 StreamEvent = "events" | "metadata" | "debug" | "updates" | "values" | "messages/partial" | "messages/metadata" | "messages/complete" | (string & {});
3
5
  interface RunsInvokePayload {
4
6
  /**
5
7
  * Input to the run. Pass `null` to resume from the current state of the thread.
@@ -21,6 +23,21 @@ interface RunsInvokePayload {
21
23
  * Interrupt execution after leaving these nodes.
22
24
  */
23
25
  interruptAfter?: string[];
26
+ /**
27
+ * Strategy to handle concurrent runs on the same thread. Only relevant if
28
+ * there is a pending/inflight run on the same thread. One of:
29
+ * - "reject": Reject the new run.
30
+ * - "interrupt": Interrupt the current run, keeping steps completed until now,
31
+ and start a new one.
32
+ * - "rollback": Cancel and delete the existing run, rolling back the thread to
33
+ the state before it had started, then start the new run.
34
+ * - "enqueue": Queue up the new run to start after the current run finishes.
35
+ */
36
+ multitaskStrategy?: MultitaskStrategy;
37
+ /**
38
+ * Abort controller signal to cancel the run.
39
+ */
40
+ signal?: AbortController["signal"];
24
41
  }
25
42
  export interface RunsStreamPayload extends RunsInvokePayload {
26
43
  /**
@@ -33,6 +50,11 @@ export interface RunsStreamPayload extends RunsInvokePayload {
33
50
  * afterwards using the `client.runs.listEvents()` method.
34
51
  */
35
52
  streamMode?: StreamMode | Array<StreamMode>;
53
+ /**
54
+ * Pass one or more feedbackKeys if you want to request short-lived signed URLs
55
+ * for submitting feedback to LangSmith with this key for this run.
56
+ */
57
+ feedbackKeys?: string[];
36
58
  }
37
59
  export interface RunsCreatePayload extends RunsInvokePayload {
38
60
  /**
@@ -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 6. This
28
- * means that by default, each call will be retried up to 6 times, with an
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 6. This
26
- * means that by default, each call will be retried up to 6 times, with an
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 ?? 6;
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 instanceof Response) {
79
- // TODO: try to parse the response body and throw a more informative error
80
- throw new Error(`HTTP ${error.status}: ${error.statusText}`);
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
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
97
- const response = error?.response;
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(+status)) {
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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@langchain/langgraph-sdk",
3
- "version": "0.0.1-rc.1",
3
+ "version": "0.0.1-rc.11",
4
4
  "description": "Client library for interacting with the LangGraph API",
5
5
  "type": "module",
6
6
  "packageManager": "yarn@1.22.19",
@@ -9,11 +9,14 @@
9
9
  "build": "yarn clean && yarn build:esm && yarn build:cjs && node scripts/create-entrypoints.js",
10
10
  "build:esm": "rm -f src/package.json && tsc --outDir dist/ && rm -rf dist/tests dist/**/tests",
11
11
  "build:cjs": "echo '{}' > src/package.json && tsc --outDir dist-cjs/ -p tsconfig.cjs.json && node scripts/move-cjs-to-dist.js && rm -r dist-cjs src/package.json",
12
- "prepublish": "yarn run build"
12
+ "prepublish": "yarn run build",
13
+ "format": "prettier --write src",
14
+ "lint": "prettier --check src && tsc --noEmit"
13
15
  },
14
16
  "main": "index.js",
15
17
  "license": "MIT",
16
18
  "dependencies": {
19
+ "@types/json-schema": "^7.0.15",
17
20
  "eventsource-parser": "^1.1.2",
18
21
  "p-queue": "^6.6.2",
19
22
  "p-retry": "4",
@@ -23,6 +26,7 @@
23
26
  "@tsconfig/recommended": "^1.0.2",
24
27
  "@types/node": "^20.12.12",
25
28
  "@types/uuid": "^9.0.1",
29
+ "prettier": "^3.2.5",
26
30
  "typescript": "^5.4.5"
27
31
  },
28
32
  "exports": {