@langchain/langgraph-api 1.1.16 → 1.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (52) hide show
  1. package/dist/api/protocol.d.mts +7 -0
  2. package/dist/api/protocol.mjs +157 -0
  3. package/dist/api/runs.mjs +15 -4
  4. package/dist/command.mjs +1 -1
  5. package/dist/experimental/embed/constants.d.mts +3 -0
  6. package/dist/experimental/embed/constants.mjs +11 -0
  7. package/dist/experimental/embed/protocol.d.mts +9 -0
  8. package/dist/experimental/embed/protocol.mjs +453 -0
  9. package/dist/experimental/embed/runs.d.mts +8 -0
  10. package/dist/experimental/embed/runs.mjs +202 -0
  11. package/dist/experimental/embed/threads.d.mts +8 -0
  12. package/dist/experimental/embed/threads.mjs +151 -0
  13. package/dist/experimental/embed/types.d.mts +77 -0
  14. package/dist/experimental/embed/types.mjs +1 -0
  15. package/dist/experimental/embed/utils.d.mts +29 -0
  16. package/dist/experimental/embed/utils.mjs +62 -0
  17. package/dist/experimental/embed.d.mts +11 -31
  18. package/dist/experimental/embed.mjs +19 -399
  19. package/dist/graph/load.d.mts +2 -2
  20. package/dist/graph/load.utils.mjs +13 -3
  21. package/dist/protocol/constants.d.mts +7 -0
  22. package/dist/protocol/constants.mjs +7 -0
  23. package/dist/protocol/service.d.mts +101 -0
  24. package/dist/protocol/service.mjs +568 -0
  25. package/dist/protocol/session/event-normalizers.d.mts +52 -0
  26. package/dist/protocol/session/event-normalizers.mjs +162 -0
  27. package/dist/protocol/session/index.d.mts +261 -0
  28. package/dist/protocol/session/index.mjs +826 -0
  29. package/dist/protocol/session/internal-types.d.mts +67 -0
  30. package/dist/protocol/session/internal-types.mjs +28 -0
  31. package/dist/protocol/session/metadata.d.mts +24 -0
  32. package/dist/protocol/session/metadata.mjs +95 -0
  33. package/dist/protocol/session/namespace.d.mts +47 -0
  34. package/dist/protocol/session/namespace.mjs +62 -0
  35. package/dist/protocol/session/state-normalizers.d.mts +57 -0
  36. package/dist/protocol/session/state-normalizers.mjs +430 -0
  37. package/dist/protocol/session/tool-calls.d.mts +27 -0
  38. package/dist/protocol/session/tool-calls.mjs +59 -0
  39. package/dist/protocol/types.d.mts +121 -0
  40. package/dist/protocol/types.mjs +1 -0
  41. package/dist/queue.mjs +8 -2
  42. package/dist/schemas.d.mts +58 -58
  43. package/dist/semver/index.mjs +19 -1
  44. package/dist/server.mjs +22 -3
  45. package/dist/state.mjs +1 -1
  46. package/dist/storage/ops.mjs +17 -5
  47. package/dist/storage/persist.mjs +1 -1
  48. package/dist/storage/types.d.mts +10 -1
  49. package/dist/stream.d.mts +25 -4
  50. package/dist/stream.mjs +203 -11
  51. package/dist/utils/serde.mjs +1 -1
  52. package/package.json +16 -14
package/dist/stream.d.mts CHANGED
@@ -1,5 +1,6 @@
1
1
  import type { BaseCheckpointSaver, LangGraphRunnableConfig, CheckpointMetadata, Interrupt, StateSnapshot } from "@langchain/langgraph";
2
2
  import type { Pregel } from "@langchain/langgraph/pregel";
3
+ import type { SourceStreamEvent } from "./protocol/types.mjs";
3
4
  import type { Checkpoint, Run, RunnableConfig } from "./storage/types.mjs";
4
5
  interface DebugTask {
5
6
  id: string;
@@ -36,8 +37,28 @@ export declare function streamState(run: Run, options: {
36
37
  onCheckpoint?: (checkpoint: StreamCheckpoint) => void;
37
38
  onTaskResult?: (taskResult: StreamTaskResult) => void;
38
39
  signal?: AbortSignal;
39
- }): AsyncGenerator<{
40
- event: string;
41
- data: unknown;
42
- }>;
40
+ }): AsyncGenerator<SourceStreamEvent>;
41
+ /**
42
+ * Executes a graph run using `graph.streamEvents(..., { version: "v3" })`
43
+ * and maps the resulting `ProtocolEvent` objects into the `{ event, data }`
44
+ * shape consumed by both the legacy SSE path and the protocol v2 session.
45
+ *
46
+ * This path activates graph-level `streamTransformers` (registered via
47
+ * `.compile({ transformers })`) so that custom transformer output flows to
48
+ * clients automatically.
49
+ *
50
+ * @param run - The queued run to execute.
51
+ * @param options - Callbacks and graph-loading infrastructure.
52
+ * @returns Async generator of `{ event, data }` pairs.
53
+ */
54
+ export declare function streamStateV2(run: Run, options: {
55
+ attempt: number;
56
+ graph: Pregel<any, any, any, any, any>;
57
+ getGraph: (graphId: string, config: LangGraphRunnableConfig | undefined, options?: {
58
+ checkpointer?: BaseCheckpointSaver | null;
59
+ }) => Promise<Pregel<any, any, any, any, any>>;
60
+ onCheckpoint?: (checkpoint: StreamCheckpoint) => void;
61
+ onTaskResult?: (taskResult: StreamTaskResult) => void;
62
+ signal?: AbortSignal;
63
+ }): AsyncGenerator<SourceStreamEvent>;
43
64
  export {};
package/dist/stream.mjs CHANGED
@@ -1,7 +1,9 @@
1
1
  import { isBaseMessage } from "@langchain/core/messages";
2
2
  import { LangChainTracer } from "@langchain/core/tracers/tracer_langchain";
3
+ import { convertToProtocolEvent, STREAM_EVENTS_V3_MODES, } from "@langchain/langgraph/web";
3
4
  import { Client as LangSmithClient, getDefaultProjectName } from "langsmith";
4
5
  import { getLangGraphCommand } from "./command.mjs";
6
+ import { PROTOCOL_STREAM_RUN_KEY } from "./protocol/constants.mjs";
5
7
  import { checkLangGraphSemver } from "./semver/index.mjs";
6
8
  import { runnableConfigToCheckpoint, taskRunnableConfigToCheckpoint, } from "./utils/runnableConfig.mjs";
7
9
  const isRunnableConfig = (config) => {
@@ -62,6 +64,15 @@ export async function* streamState(run, options) {
62
64
  const graph = await options.getGraph(graphId, kwargs.config, {
63
65
  checkpointer: kwargs.temporary ? null : undefined,
64
66
  });
67
+ // Only v2 protocol entrypoints opt into `streamStateV2`.
68
+ // Legacy run/stream endpoints stay on the existing `streamEvents`
69
+ // path even if a graph defines stream transformers, so they do not
70
+ // emit protocol-framed events on non-protocol transports.
71
+ const isProtocolV2Run = kwargs[PROTOCOL_STREAM_RUN_KEY] === true;
72
+ if (isProtocolV2Run) {
73
+ yield* streamStateV2(run, { ...options, graph });
74
+ return;
75
+ }
65
76
  const userStreamMode = kwargs.stream_mode ?? [];
66
77
  const libStreamMode = new Set(userStreamMode.filter((mode) => mode !== "events" && mode !== "messages-tuple") ?? []);
67
78
  if (userStreamMode.includes("messages-tuple")) {
@@ -103,7 +114,7 @@ export async function* streamState(run, options) {
103
114
  : undefined;
104
115
  const events = graph.streamEvents(kwargs.command != null
105
116
  ? getLangGraphCommand(kwargs.command)
106
- : kwargs.input ?? null, {
117
+ : (kwargs.input ?? null), {
107
118
  version: "v2",
108
119
  interruptAfter: kwargs.interrupt_after,
109
120
  interruptBefore: kwargs.interrupt_before,
@@ -123,9 +134,16 @@ export async function* streamState(run, options) {
123
134
  for await (const event of events) {
124
135
  if (event.tags?.includes("langsmith:hidden"))
125
136
  continue;
126
- if (event.event === "on_chain_stream" && event.run_id === run.run_id) {
127
- const [ns, mode, chunk] = (kwargs.subgraphs ? event.data.chunk : [null, ...event.data.chunk]);
128
- // Listen for debug events and capture checkpoint
137
+ if (event.event === "on_chain_stream" &&
138
+ (kwargs.subgraphs || event.run_id === run.run_id)) {
139
+ // Pregel's stream tuple is `[ns, mode, payload, meta?]` (4th element
140
+ // is the optional `StreamChunkMeta`, preserved when streaming with
141
+ // `subgraphs: true`). The meta carries the lightweight checkpoint
142
+ // envelope attached by `_emitValuesWithCheckpointMeta`, which we
143
+ // forward as a companion `checkpoints` source event below.
144
+ const rawTuple = (kwargs.subgraphs ? event.data.chunk : [null, ...event.data.chunk]);
145
+ const [ns, mode, chunk] = rawTuple;
146
+ const chunkMeta = rawTuple[3];
129
147
  let data = chunk;
130
148
  if (mode === "debug") {
131
149
  const debugChunk = chunk;
@@ -152,6 +170,20 @@ export async function* streamState(run, options) {
152
170
  }
153
171
  data = debugTask;
154
172
  }
173
+ // Emit the lightweight checkpoint envelope as a dedicated
174
+ // `checkpoints` source event immediately BEFORE the companion
175
+ // `values` event so clients subscribed to both channels have the
176
+ // envelope buffered by the time the values payload arrives
177
+ // (`useMessageMetadata(msg.id).parentCheckpointId` for fork /
178
+ // edit flows). Clients that only want fork / time-travel metadata
179
+ // subscribe to `checkpoints` alone and avoid the full-state
180
+ // payload.
181
+ if (mode === "values" && chunkMeta?.checkpoint != null) {
182
+ const sseEvent = kwargs.subgraphs && ns?.length
183
+ ? `checkpoints|${ns.join("|")}`
184
+ : "checkpoints";
185
+ yield { event: sseEvent, data: chunkMeta.checkpoint };
186
+ }
155
187
  if (mode === "messages") {
156
188
  if (userStreamMode.includes("messages-tuple")) {
157
189
  if (kwargs.subgraphs && ns?.length) {
@@ -163,12 +195,8 @@ export async function* streamState(run, options) {
163
195
  }
164
196
  }
165
197
  else if (userStreamMode.includes(mode)) {
166
- if (kwargs.subgraphs && ns?.length) {
167
- yield { event: `${mode}|${ns.join("|")}`, data };
168
- }
169
- else {
170
- yield { event: mode, data };
171
- }
198
+ const sseEvent = kwargs.subgraphs && ns?.length ? `${mode}|${ns.join("|")}` : mode;
199
+ yield { event: sseEvent, data };
172
200
  }
173
201
  }
174
202
  else if (userStreamMode.includes("events")) {
@@ -182,7 +210,8 @@ export async function* streamState(run, options) {
182
210
  // - handleLLMEnd should not dedupe the message
183
211
  // - Don't think there's an utility that would convert a BaseMessageChunk to a BaseMessage?
184
212
  if (userStreamMode.includes("messages")) {
185
- if (event.event === "on_chain_stream" && event.run_id === run.run_id) {
213
+ if (event.event === "on_chain_stream" &&
214
+ (kwargs.subgraphs || event.run_id === run.run_id)) {
186
215
  const newMessages = [];
187
216
  const [_, chunk] = event.data.chunk;
188
217
  let chunkMessages = [];
@@ -233,3 +262,166 @@ export async function* streamState(run, options) {
233
262
  yield { event: "feedback", data };
234
263
  }
235
264
  }
265
+ function isUnsupportedStreamEventsV3Error(error) {
266
+ return (error instanceof Error &&
267
+ error.message.includes('Only versions "v1" and "v2" of the schema are currently supported'));
268
+ }
269
+ async function* fallbackProtocolStreamFromGraphStream(graph, input, options) {
270
+ let seq = 0;
271
+ const stream = await graph.stream(input, options);
272
+ for await (const tuple of stream) {
273
+ const [namespace, mode, payload, meta] = tuple;
274
+ const events = convertToProtocolEvent({
275
+ namespace: namespace ?? [],
276
+ mode,
277
+ payload,
278
+ seq,
279
+ meta: meta,
280
+ });
281
+ seq += events.length;
282
+ for (const event of events) {
283
+ yield event;
284
+ }
285
+ }
286
+ }
287
+ /**
288
+ * Executes a graph run using `graph.streamEvents(..., { version: "v3" })`
289
+ * and maps the resulting `ProtocolEvent` objects into the `{ event, data }`
290
+ * shape consumed by both the legacy SSE path and the protocol v2 session.
291
+ *
292
+ * This path activates graph-level `streamTransformers` (registered via
293
+ * `.compile({ transformers })`) so that custom transformer output flows to
294
+ * clients automatically.
295
+ *
296
+ * @param run - The queued run to execute.
297
+ * @param options - Callbacks and graph-loading infrastructure.
298
+ * @returns Async generator of `{ event, data }` pairs.
299
+ */
300
+ export async function* streamStateV2(run, options) {
301
+ const kwargs = run.kwargs;
302
+ const graph = options.graph;
303
+ yield {
304
+ event: "metadata",
305
+ data: { run_id: run.run_id, attempt: options.attempt },
306
+ };
307
+ if (!LANGGRAPH_VERSION) {
308
+ const version = await checkLangGraphSemver();
309
+ LANGGRAPH_VERSION = version.find((v) => v.name === "@langchain/langgraph");
310
+ }
311
+ const metadata = {
312
+ ...kwargs.config?.metadata,
313
+ run_attempt: options.attempt,
314
+ langgraph_version: LANGGRAPH_VERSION?.version ?? "0.0.0",
315
+ langgraph_plan: "developer",
316
+ langgraph_host: "self-hosted",
317
+ langgraph_api_url: process.env.LANGGRAPH_API_URL ?? undefined,
318
+ };
319
+ const tracer = run.kwargs?.config?.configurable?.langsmith_project
320
+ ? new LangChainTracer({
321
+ replicas: [
322
+ [
323
+ run.kwargs?.config?.configurable?.langsmith_project,
324
+ {
325
+ reference_example_id: run.kwargs?.config?.configurable?.langsmith_example_id,
326
+ },
327
+ ],
328
+ [getDefaultProjectName(), undefined],
329
+ ],
330
+ })
331
+ : undefined;
332
+ const graphInput = kwargs.command != null
333
+ ? getLangGraphCommand(kwargs.command)
334
+ : (kwargs.input ?? null);
335
+ const graphOptions = {
336
+ version: "v3",
337
+ interruptAfter: kwargs.interrupt_after,
338
+ interruptBefore: kwargs.interrupt_before,
339
+ tags: kwargs.config?.tags,
340
+ context: kwargs.context,
341
+ configurable: kwargs.config?.configurable,
342
+ recursionLimit: kwargs.config?.recursion_limit,
343
+ metadata: { ls_integration: "langgraph", ...metadata },
344
+ runId: run.run_id,
345
+ signal: options?.signal,
346
+ ...(tracer && { callbacks: [tracer] }),
347
+ };
348
+ let graphRun;
349
+ try {
350
+ graphRun = await graph.streamEvents(graphInput, graphOptions);
351
+ }
352
+ catch (error) {
353
+ if (!isUnsupportedStreamEventsV3Error(error)) {
354
+ throw error;
355
+ }
356
+ graphRun = fallbackProtocolStreamFromGraphStream(graph, graphInput, {
357
+ ...graphOptions,
358
+ streamMode: STREAM_EVENTS_V3_MODES,
359
+ subgraphs: true,
360
+ });
361
+ }
362
+ for await (const event of graphRun) {
363
+ const ns = event.params.namespace;
364
+ const mode = event.method;
365
+ const data = event.params.data;
366
+ if (mode === "debug") {
367
+ const debugChunk = data;
368
+ if (debugChunk.type === "checkpoint") {
369
+ const debugCheckpoint = preprocessDebugCheckpoint(debugChunk.payload);
370
+ options?.onCheckpoint?.(debugCheckpoint);
371
+ const sseEvent = ns.length > 0 ? `${mode}|${ns.join("|")}` : mode;
372
+ yield {
373
+ event: sseEvent,
374
+ data: { ...debugChunk, payload: debugCheckpoint },
375
+ };
376
+ continue;
377
+ }
378
+ else if (debugChunk.type === "task_result") {
379
+ const debugResult = preprocessDebugCheckpointTask(debugChunk.payload);
380
+ options?.onTaskResult?.(debugResult);
381
+ const sseEvent = ns.length > 0 ? `${mode}|${ns.join("|")}` : mode;
382
+ yield {
383
+ event: sseEvent,
384
+ data: { ...debugChunk, payload: debugResult },
385
+ };
386
+ continue;
387
+ }
388
+ }
389
+ else if (mode === "tasks") {
390
+ const debugTask = preprocessDebugCheckpointTask(data);
391
+ if ("result" in debugTask || "error" in debugTask) {
392
+ options?.onTaskResult?.(debugTask);
393
+ }
394
+ const sseEvent = ns.length > 0 ? `${mode}|${ns.join("|")}` : mode;
395
+ yield { event: sseEvent, data: debugTask };
396
+ continue;
397
+ }
398
+ const sseEvent = ns.length > 0 ? `${mode}|${ns.join("|")}` : mode;
399
+ /**
400
+ * These modes have already been converted to their protocol shape by
401
+ * core's `convertToProtocolEvent`, so the session can skip
402
+ * re-normalization. Other modes (values, debug, tasks) still
403
+ * require API-specific processing (interrupt stripping, state
404
+ * message normalization, checkpoint preprocessing). `checkpoints`
405
+ * is emitted by core as a standalone protocol event whose `data` is
406
+ * already the lightweight envelope; the session frames it on the
407
+ * dedicated `checkpoints` channel. `lifecycle` events are
408
+ * synthesized by core's `LifecycleTransformer` and forwarded
409
+ * verbatim by the session.
410
+ */
411
+ const normalized = mode === "tools" ||
412
+ mode === "updates" ||
413
+ mode === "custom" ||
414
+ mode === "messages" ||
415
+ mode === "checkpoints" ||
416
+ mode === "lifecycle";
417
+ yield { event: sseEvent, data, normalized };
418
+ }
419
+ if (kwargs.feedback_keys) {
420
+ const client = new LangSmithClient();
421
+ const feedbackData = Object.fromEntries(await Promise.all(kwargs.feedback_keys.map(async (feedback) => {
422
+ const { url } = await client.createPresignedFeedbackToken(run.run_id, feedback);
423
+ return [feedback, url];
424
+ })));
425
+ yield { event: "feedback", data: feedbackData };
426
+ }
427
+ }
@@ -10,7 +10,7 @@ export const serialiseAsDict = (obj) => {
10
10
  return { ...data, type };
11
11
  }
12
12
  return value;
13
- }, 2);
13
+ });
14
14
  };
15
15
  export const serializeError = (error) => {
16
16
  if (error instanceof Error) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@langchain/langgraph-api",
3
- "version": "1.1.16",
3
+ "version": "1.2.0",
4
4
  "type": "module",
5
5
  "engines": {
6
6
  "node": "^18.19.0 || >=20.16.0"
@@ -52,15 +52,17 @@
52
52
  },
53
53
  "dependencies": {
54
54
  "@babel/code-frame": "^7.26.2",
55
- "@hono/node-server": "^1.12.0",
55
+ "@hono/node-server": "^1.19.13",
56
+ "@hono/node-ws": "^1.3.0",
56
57
  "@hono/zod-validator": "^0.7.6",
58
+ "@langchain/protocol": "^0.0.15",
57
59
  "@types/json-schema": "^7.0.15",
58
60
  "@typescript/vfs": "^1.6.0",
59
61
  "dedent": "^1.5.3",
60
62
  "dotenv": "^16.4.7",
61
63
  "exit-hook": "^4.0.0",
62
- "hono": "^4.5.4",
63
- "langsmith": ">=0.3.33 <1.0.0",
64
+ "hono": "^4.12.14",
65
+ "langsmith": ">=0.5.19 <1.0.0",
64
66
  "open": "^10.1.0",
65
67
  "semver": "^7.7.1",
66
68
  "stacktrace-parser": "^0.1.10",
@@ -70,10 +72,10 @@
70
72
  "winston": "^3.17.0",
71
73
  "winston-console-format": "^1.0.8",
72
74
  "zod": "^3.25.76 || ^4",
73
- "@langchain/langgraph-ui": "1.1.16"
75
+ "@langchain/langgraph-ui": "1.2.0"
74
76
  },
75
77
  "peerDependencies": {
76
- "@langchain/core": "^0.3.59 || ^1.0.1",
78
+ "@langchain/core": "^1.1.44",
77
79
  "@langchain/langgraph": "^0.2.57 || ^0.3.0 || ^0.4.0 || ^1.0.0-alpha || ^1.0.0",
78
80
  "@langchain/langgraph-checkpoint": "~0.0.16 || ^0.1.0 || ~1.0.0",
79
81
  "@langchain/langgraph-sdk": "^1.6.5",
@@ -85,32 +87,32 @@
85
87
  }
86
88
  },
87
89
  "devDependencies": {
88
- "@langchain/core": "^1.1.28",
90
+ "@langchain/core": "^1.1.44",
89
91
  "@types/babel__code-frame": "^7.0.6",
90
92
  "@types/node": "^18.15.11",
91
93
  "@types/react": "^19.0.8",
92
94
  "@types/react-dom": "^19.0.3",
93
95
  "@types/semver": "^7.7.0",
94
96
  "@types/uuid": "^10.0.0",
97
+ "deepagents": "^1.9.0",
95
98
  "jose": "^6.0.10",
99
+ "langchain": "^1.2.30",
96
100
  "postgres": "^3.4.5",
97
- "prettier": "^2.8.3",
98
101
  "typescript": "^4.9.5 || ^5.4.5",
99
102
  "vitest": "^3.2.4",
100
103
  "wait-port": "^1.1.0",
101
- "@langchain/langgraph": "1.2.3",
102
- "@langchain/langgraph-checkpoint": "1.0.1",
103
- "@langchain/langgraph-sdk": "1.7.3"
104
+ "@langchain/langgraph-checkpoint": "1.0.2",
105
+ "@langchain/langgraph-sdk": "1.9.0",
106
+ "@langchain/langgraph": "1.3.0"
104
107
  },
105
108
  "scripts": {
106
- "clean": "rm -rf dist/ .turbo/ ./tests/graphs/.langgraph_api/",
109
+ "clean": "rm -rf dist/ .turbo/ ./tests/graphs/.langgraph_api/ ./tests/protocol-v2/graphs/.langgraph_api/",
107
110
  "build": "pnpm turbo build:internal --filter=@langchain/langgraph-api",
108
111
  "build:internal": "pnpm clean && node scripts/build.mjs",
109
112
  "dev": "tsx ./tests/utils.server.mts --dev",
110
113
  "prepublish": "pnpm build",
111
114
  "typecheck": "tsc --noEmit",
112
115
  "test": "vitest run",
113
- "format": "prettier --write .",
114
- "format:check": "prettier --check ."
116
+ "test:protocol-v2": "vitest run tests/protocol-v2"
115
117
  }
116
118
  }