@langchain/langgraph-sdk 0.0.1-rc.1 → 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 CHANGED
@@ -1,4 +1,4 @@
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
3
  import { RunsCreatePayload, RunsStreamPayload, RunsWaitPayload } from "./types.mjs";
4
4
  interface ClientConfig {
@@ -21,19 +21,51 @@ 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
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
60
  graphId: string;
34
61
  config?: Config;
35
62
  metadata?: Metadata;
36
63
  }): Promise<Assistant>;
64
+ /**
65
+ * List assistants.
66
+ * @param query Query options.
67
+ * @returns List of assistants.
68
+ */
37
69
  search(query?: {
38
70
  metadata?: Metadata;
39
71
  limit?: number;
@@ -54,7 +86,7 @@ declare class ThreadsClient extends BaseClient {
54
86
  * @param payload Payload for creating a thread.
55
87
  * @returns The created thread.
56
88
  */
57
- create(payload: {
89
+ create(payload?: {
58
90
  /**
59
91
  * Metadata for the thread.
60
92
  */
@@ -106,7 +138,7 @@ declare class ThreadsClient extends BaseClient {
106
138
  * @param threadId ID of the thread.
107
139
  * @returns Thread state.
108
140
  */
109
- getState(threadId: string): Promise<ThreadState>;
141
+ getState<ValuesType = DefaultValues>(threadId: string): Promise<ThreadState<ValuesType>>;
110
142
  /**
111
143
  * Add state to a thread.
112
144
  *
@@ -115,11 +147,12 @@ declare class ThreadsClient extends BaseClient {
115
147
  * @param options Additional options.
116
148
  * @returns
117
149
  */
118
- updateState(threadIdOrConfig: string | Config, values: Record<string, unknown>, options?: {
150
+ updateState<ValuesType = DefaultValues>(threadIdOrConfig: string | Config, options: {
151
+ values: ValuesType;
119
152
  asNode?: string;
120
153
  }): Promise<void>;
121
154
  /**
122
- * Patch the state of a thread.
155
+ * Patch the metadata of a thread.
123
156
  *
124
157
  * @param threadIdOrConfig Thread ID or config to patch the state of.
125
158
  * @param metadata Metadata to patch the state with.
@@ -132,10 +165,10 @@ declare class ThreadsClient extends BaseClient {
132
165
  * @param options Additional options.
133
166
  * @returns List of thread states.
134
167
  */
135
- getHistory(threadId: string, options?: {
168
+ getHistory<ValuesType = DefaultValues>(threadId: string, options?: {
136
169
  limit?: number;
137
170
  before?: Config;
138
- }): Promise<ThreadState[]>;
171
+ }): Promise<ThreadState<ValuesType>[]>;
139
172
  }
140
173
  declare class RunsClient extends BaseClient {
141
174
  /**
@@ -146,8 +179,8 @@ declare class RunsClient extends BaseClient {
146
179
  * @param payload Payload for creating a run.
147
180
  */
148
181
  stream(threadId: string, assistantId: string, payload?: RunsStreamPayload): AsyncGenerator<{
149
- event: string;
150
- data: unknown;
182
+ event: "events" | "metadata" | "debug" | "updates" | "values" | "messages/partial" | "messages/metadata" | "messages/complete" | (string & {});
183
+ data: any;
151
184
  }>;
152
185
  /**
153
186
  * Create a run.
@@ -216,8 +249,17 @@ declare class RunsClient extends BaseClient {
216
249
  }): Promise<RunEvent[]>;
217
250
  }
218
251
  export declare class Client {
252
+ /**
253
+ * The client for interacting with assistants.
254
+ */
219
255
  assistants: AssistantsClient;
256
+ /**
257
+ * The client for interacting with threads.
258
+ */
220
259
  threads: ThreadsClient;
260
+ /**
261
+ * The client for interacting with runs.
262
+ */
221
263
  runs: RunsClient;
222
264
  constructor(config?: ClientConfig);
223
265
  }
package/dist/client.mjs CHANGED
@@ -22,8 +22,8 @@ class BaseClient {
22
22
  value: void 0
23
23
  });
24
24
  this.asyncCaller = new AsyncCaller({
25
- maxRetries: 5,
26
- maxConcurrency: 5,
25
+ maxRetries: 4,
26
+ maxConcurrency: 4,
27
27
  ...config?.callerOptions,
28
28
  });
29
29
  this.timeoutMs = config?.timeoutMs || 12_000;
@@ -59,15 +59,36 @@ 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",
@@ -78,6 +99,12 @@ class AssistantsClient extends BaseClient {
78
99
  },
79
100
  });
80
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
+ */
81
108
  async upsert(assistantId, payload) {
82
109
  return this.fetch(`/assistants/${assistantId}`, {
83
110
  method: "PUT",
@@ -88,6 +115,11 @@ class AssistantsClient extends BaseClient {
88
115
  },
89
116
  });
90
117
  }
118
+ /**
119
+ * List assistants.
120
+ * @param query Query options.
121
+ * @returns List of assistants.
122
+ */
91
123
  async search(query) {
92
124
  return this.fetch("/assistants/search", {
93
125
  method: "POST",
@@ -177,7 +209,7 @@ class ThreadsClient extends BaseClient {
177
209
  * @param options Additional options.
178
210
  * @returns
179
211
  */
180
- async updateState(threadIdOrConfig, values, options) {
212
+ async updateState(threadIdOrConfig, options) {
181
213
  let config = undefined;
182
214
  let threadId;
183
215
  if (typeof threadIdOrConfig !== "string") {
@@ -193,11 +225,11 @@ class ThreadsClient extends BaseClient {
193
225
  }
194
226
  return this.fetch(`/threads/${threadId}/state`, {
195
227
  method: "POST",
196
- json: { values, config, as_node: options?.asNode },
228
+ json: { values: options.values, config, as_node: options?.asNode },
197
229
  });
198
230
  }
199
231
  /**
200
- * Patch the state of a thread.
232
+ * Patch the metadata of a thread.
201
233
  *
202
234
  * @param threadIdOrConfig Thread ID or config to patch the state of.
203
235
  * @param metadata Metadata to patch the state with.
@@ -365,18 +397,27 @@ class RunsClient extends BaseClient {
365
397
  }
366
398
  export class Client {
367
399
  constructor(config) {
400
+ /**
401
+ * The client for interacting with assistants.
402
+ */
368
403
  Object.defineProperty(this, "assistants", {
369
404
  enumerable: true,
370
405
  configurable: true,
371
406
  writable: true,
372
407
  value: void 0
373
408
  });
409
+ /**
410
+ * The client for interacting with threads.
411
+ */
374
412
  Object.defineProperty(this, "threads", {
375
413
  enumerable: true,
376
414
  configurable: true,
377
415
  writable: true,
378
416
  value: void 0
379
417
  });
418
+ /**
419
+ * The client for interacting with runs.
420
+ */
380
421
  Object.defineProperty(this, "runs", {
381
422
  enumerable: true,
382
423
  configurable: true,
package/dist/schema.d.ts CHANGED
@@ -19,8 +19,17 @@ export interface Config {
19
19
  };
20
20
  }
21
21
  export interface GraphSchema {
22
+ /**
23
+ * The ID of the graph.
24
+ */
22
25
  graph_id: string;
26
+ /**
27
+ * The schema for the graph state
28
+ */
23
29
  state_schema: Record<string, unknown>;
30
+ /**
31
+ * The schema for the graph config
32
+ */
24
33
  config_schema: Record<string, unknown>;
25
34
  }
26
35
  export type Metadata = Optional<Record<string, unknown>>;
@@ -39,8 +48,9 @@ export interface Thread {
39
48
  updated_at: string;
40
49
  metadata: Metadata;
41
50
  }
42
- export interface ThreadState {
43
- values: Record<string, unknown>[] | Record<string, unknown>;
51
+ export type DefaultValues = Record<string, unknown>[] | Record<string, unknown>;
52
+ export interface ThreadState<ValuesType = DefaultValues> {
53
+ values: ValuesType;
44
54
  next: string[];
45
55
  config: Config;
46
56
  metadata: Metadata;
@@ -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.2",
4
4
  "description": "Client library for interacting with the LangGraph API",
5
5
  "type": "module",
6
6
  "packageManager": "yarn@1.22.19",