@langchain/langgraph-api 1.5.6 → 1.5.8
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/api/runs.mjs +18 -6
- package/dist/experimental/embed/runs.mjs +5 -2
- package/dist/queue.mjs +39 -10
- package/dist/schemas.d.mts +10 -0
- package/dist/schemas.mjs +18 -0
- package/dist/storage/ops.d.mts +1 -0
- package/dist/storage/ops.mjs +44 -6
- package/dist/storage/types.d.mts +1 -1
- package/dist/stream.d.mts +1 -0
- package/dist/stream.mjs +33 -11
- package/package.json +8 -8
package/dist/api/runs.mjs
CHANGED
|
@@ -8,6 +8,7 @@ import { getAssistantId } from "../graph/load.mjs";
|
|
|
8
8
|
import { logError, logger } from "../logging.mjs";
|
|
9
9
|
import * as schemas from "../schemas.mjs";
|
|
10
10
|
import { runs, threads } from "../storage/context.mjs";
|
|
11
|
+
import { matchesStreamMode } from "../stream.mjs";
|
|
11
12
|
import { getDisconnectAbortSignal, jsonExtra, waitKeepAlive, } from "../utils/hono.mjs";
|
|
12
13
|
import { applyAuthToRunConfig, applyRequestHeadersToRunConfig, } from "../utils/run-auth.mjs";
|
|
13
14
|
import { serialiseAsDict } from "../utils/serde.mjs";
|
|
@@ -23,7 +24,7 @@ const createValidRun = async (threadId, payload, kwargs) => {
|
|
|
23
24
|
: [];
|
|
24
25
|
if (streamMode.length === 0)
|
|
25
26
|
streamMode.push("values");
|
|
26
|
-
const multitaskStrategy = payload.multitask_strategy ?? "
|
|
27
|
+
const multitaskStrategy = payload.multitask_strategy ?? "enqueue";
|
|
27
28
|
const preventInsertInInflight = multitaskStrategy === "reject";
|
|
28
29
|
const config = { ...run.config };
|
|
29
30
|
if (run.checkpoint_id) {
|
|
@@ -143,7 +144,7 @@ api.post("/runs/stream", zValidator("json", schemas.RunCreate), async (c) => {
|
|
|
143
144
|
});
|
|
144
145
|
});
|
|
145
146
|
// TODO: port to Python API
|
|
146
|
-
api.get("/runs/:run_id/stream", zValidator("param", z.object({ run_id: z.string().uuid() })), zValidator("query",
|
|
147
|
+
api.get("/runs/:run_id/stream", zValidator("param", z.object({ run_id: z.string().uuid() })), zValidator("query", schemas.JoinStreamQuery), async (c) => {
|
|
147
148
|
const { run_id } = c.req.valid("param");
|
|
148
149
|
const query = c.req.valid("query");
|
|
149
150
|
const lastEventId = c.req.header("Last-Event-ID") || undefined;
|
|
@@ -159,6 +160,8 @@ api.get("/runs/:run_id/stream", zValidator("param", z.object({ run_id: z.string(
|
|
|
159
160
|
lastEventId,
|
|
160
161
|
ignore404: true,
|
|
161
162
|
}, c.var.auth)) {
|
|
163
|
+
if (!matchesStreamMode(event, query.stream_mode))
|
|
164
|
+
continue;
|
|
162
165
|
await stream.writeSSE({ id, data: serialiseAsDict(data), event });
|
|
163
166
|
}
|
|
164
167
|
}
|
|
@@ -281,10 +284,10 @@ api.get("/threads/:thread_id/runs/:run_id/join", zValidator("param", z.object({
|
|
|
281
284
|
const { thread_id, run_id } = c.req.valid("param");
|
|
282
285
|
return jsonExtra(c, await runs().join(run_id, thread_id, c.var.auth));
|
|
283
286
|
});
|
|
284
|
-
api.get("/threads/:thread_id/runs/:run_id/stream", zValidator("param", z.object({ thread_id: z.string().uuid(), run_id: z.string().uuid() })), zValidator("query",
|
|
287
|
+
api.get("/threads/:thread_id/runs/:run_id/stream", zValidator("param", z.object({ thread_id: z.string().uuid(), run_id: z.string().uuid() })), zValidator("query", schemas.JoinStreamQuery), async (c) => {
|
|
285
288
|
// Stream Run Http
|
|
286
289
|
const { thread_id, run_id } = c.req.valid("param");
|
|
287
|
-
const { cancel_on_disconnect } = c.req.valid("query");
|
|
290
|
+
const { cancel_on_disconnect, stream_mode } = c.req.valid("query");
|
|
288
291
|
const lastEventId = c.req.header("Last-Event-ID") || undefined;
|
|
289
292
|
return streamSSE(c, async (stream) => {
|
|
290
293
|
const signal = cancel_on_disconnect
|
|
@@ -295,6 +298,8 @@ api.get("/threads/:thread_id/runs/:run_id/stream", zValidator("param", z.object(
|
|
|
295
298
|
cancelOnDisconnect: signal != null,
|
|
296
299
|
lastEventId,
|
|
297
300
|
}, c.var.auth)) {
|
|
301
|
+
if (!matchesStreamMode(event, stream_mode))
|
|
302
|
+
continue;
|
|
298
303
|
await stream.writeSSE({ id, data: serialiseAsDict(data), event });
|
|
299
304
|
}
|
|
300
305
|
});
|
|
@@ -307,8 +312,15 @@ api.post("/threads/:thread_id/runs/:run_id/cancel", zValidator("param", z.object
|
|
|
307
312
|
const { thread_id, run_id } = c.req.valid("param");
|
|
308
313
|
const { wait, action } = c.req.valid("query");
|
|
309
314
|
await runs().cancel(thread_id, [run_id], { action }, c.var.auth);
|
|
310
|
-
if (wait)
|
|
311
|
-
|
|
315
|
+
if (wait) {
|
|
316
|
+
// A cancelled pending stateless run is deleted with its thread.
|
|
317
|
+
await runs()
|
|
318
|
+
.join(run_id, thread_id, c.var.auth)
|
|
319
|
+
.catch((error) => {
|
|
320
|
+
if (!(error instanceof HTTPException) || error.status !== 404)
|
|
321
|
+
throw error;
|
|
322
|
+
});
|
|
323
|
+
}
|
|
312
324
|
return c.body(null, wait ? 204 : 202);
|
|
313
325
|
});
|
|
314
326
|
export default api;
|
|
@@ -3,7 +3,7 @@ import { streamSSE } from "hono/streaming";
|
|
|
3
3
|
import { v7 as uuidv7 } from "@langchain/core/utils/uuid";
|
|
4
4
|
import { z } from "zod/v3";
|
|
5
5
|
import * as schemas from "../../schemas.mjs";
|
|
6
|
-
import { streamState } from "../../stream.mjs";
|
|
6
|
+
import { matchesStreamMode, streamState } from "../../stream.mjs";
|
|
7
7
|
import { serialiseAsDict, serializeError } from "../../utils/serde.mjs";
|
|
8
8
|
import { getDisconnectAbortSignal, jsonExtra } from "../../utils/hono.mjs";
|
|
9
9
|
import { createStubRun } from "./utils.mjs";
|
|
@@ -59,8 +59,9 @@ export function registerRunRoutes(api, context) {
|
|
|
59
59
|
api.get("/threads/:thread_id/runs/:run_id/stream", zValidator("param", z.object({
|
|
60
60
|
thread_id: z.string().uuid(),
|
|
61
61
|
run_id: z.string().uuid(),
|
|
62
|
-
})), async (c) => {
|
|
62
|
+
})), zValidator("query", schemas.JoinStreamQuery), async (c) => {
|
|
63
63
|
const { thread_id, run_id } = c.req.valid("param");
|
|
64
|
+
const { stream_mode } = c.req.valid("query");
|
|
64
65
|
const thread = await context.threads.get(thread_id);
|
|
65
66
|
if (thread == null)
|
|
66
67
|
return c.json({ error: "Thread not found" }, 404);
|
|
@@ -87,6 +88,8 @@ export function registerRunRoutes(api, context) {
|
|
|
87
88
|
getGraph: context.getGraph,
|
|
88
89
|
signal,
|
|
89
90
|
})) {
|
|
91
|
+
if (!matchesStreamMode(event, stream_mode))
|
|
92
|
+
continue;
|
|
90
93
|
await stream.writeSSE({ data: serialiseAsDict(data), event });
|
|
91
94
|
}
|
|
92
95
|
}
|
package/dist/queue.mjs
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { HTTPException } from "hono/http-exception";
|
|
1
2
|
import { streamState, } from "./stream.mjs";
|
|
2
3
|
import { logError, logger } from "./logging.mjs";
|
|
3
4
|
import { serializeError } from "./utils/serde.mjs";
|
|
@@ -8,7 +9,11 @@ const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
|
|
|
8
9
|
export const queue = async (ops) => {
|
|
9
10
|
while (true) {
|
|
10
11
|
for await (const { run, attempt, signal } of ops.runs.next()) {
|
|
11
|
-
|
|
12
|
+
// Nothing restarts this loop, so one run's failure must not end it.
|
|
13
|
+
await worker(ops, run, attempt, signal).catch((error) => logError(error, {
|
|
14
|
+
prefix: "Background worker failed",
|
|
15
|
+
context: { run_id: run.run_id },
|
|
16
|
+
}));
|
|
12
17
|
}
|
|
13
18
|
// TODO: this is very suboptimal, we should implement subscription to the run
|
|
14
19
|
await sleep(1000 * Math.random());
|
|
@@ -41,7 +46,25 @@ const worker = async (ops, run, attempt, signal) => {
|
|
|
41
46
|
...result,
|
|
42
47
|
};
|
|
43
48
|
};
|
|
44
|
-
const
|
|
49
|
+
const isCancelled = () => signal.aborted &&
|
|
50
|
+
(signal.reason === "interrupt" || signal.reason === "rollback");
|
|
51
|
+
// The run, or its thread, can be deleted while it runs: a final write that
|
|
52
|
+
// fails because its record is gone has nothing left to update.
|
|
53
|
+
const unlessGone = async (write, isGone) => {
|
|
54
|
+
try {
|
|
55
|
+
await write;
|
|
56
|
+
}
|
|
57
|
+
catch (error) {
|
|
58
|
+
if (!(await isGone()))
|
|
59
|
+
throw error;
|
|
60
|
+
}
|
|
61
|
+
};
|
|
62
|
+
const runGone = async () => (await ops.runs.get(run.run_id, run.thread_id, undefined)) == null;
|
|
63
|
+
const threadGone = () => ops.threads.get(run.thread_id, undefined).then(() => false, (error) => {
|
|
64
|
+
if (error instanceof HTTPException && error.status === 404)
|
|
65
|
+
return true;
|
|
66
|
+
throw error;
|
|
67
|
+
});
|
|
45
68
|
try {
|
|
46
69
|
if (attempt > MAX_RETRY_ATTEMPTS) {
|
|
47
70
|
throw new Error(`Run ${run.run_id} exceeded max attempts`);
|
|
@@ -66,7 +89,7 @@ const worker = async (ops, run, attempt, signal) => {
|
|
|
66
89
|
}
|
|
67
90
|
}
|
|
68
91
|
catch (error) {
|
|
69
|
-
if (!
|
|
92
|
+
if (!isCancelled()) {
|
|
70
93
|
await ops.runs.stream.publish({
|
|
71
94
|
runId,
|
|
72
95
|
resumable,
|
|
@@ -86,14 +109,20 @@ const worker = async (ops, run, attempt, signal) => {
|
|
|
86
109
|
run_exec_ms: endedAt.valueOf() - startedAt.valueOf(),
|
|
87
110
|
});
|
|
88
111
|
status = "success";
|
|
89
|
-
await ops.runs.setStatus(run.run_id, status);
|
|
112
|
+
await unlessGone(ops.runs.setStatus(run.run_id, status), runGone);
|
|
90
113
|
}
|
|
91
114
|
catch (error) {
|
|
92
115
|
endedAt = new Date();
|
|
93
|
-
if (
|
|
116
|
+
if (isCancelled()) {
|
|
94
117
|
checkpoint = undefined;
|
|
95
|
-
|
|
96
|
-
|
|
118
|
+
if (signal.reason === "rollback") {
|
|
119
|
+
status = "rollback";
|
|
120
|
+
await unlessGone(ops.runs.delete(run.run_id, run.thread_id, undefined), runGone);
|
|
121
|
+
}
|
|
122
|
+
else {
|
|
123
|
+
status = "interrupted";
|
|
124
|
+
await unlessGone(ops.runs.setStatus(run.run_id, status), runGone);
|
|
125
|
+
}
|
|
97
126
|
}
|
|
98
127
|
else {
|
|
99
128
|
if (error instanceof Error)
|
|
@@ -110,15 +139,15 @@ const worker = async (ops, run, attempt, signal) => {
|
|
|
110
139
|
},
|
|
111
140
|
});
|
|
112
141
|
status = "error";
|
|
113
|
-
await ops.runs.setStatus(run.run_id, status);
|
|
142
|
+
await unlessGone(ops.runs.setStatus(run.run_id, status), runGone);
|
|
114
143
|
}
|
|
115
144
|
}
|
|
116
145
|
finally {
|
|
117
146
|
if (temporary) {
|
|
118
|
-
await ops.threads.delete(run.thread_id, undefined);
|
|
147
|
+
await unlessGone(ops.threads.delete(run.thread_id, undefined), threadGone);
|
|
119
148
|
}
|
|
120
149
|
else {
|
|
121
|
-
await ops.threads.setStatus(run.thread_id, { checkpoint, exception });
|
|
150
|
+
await unlessGone(ops.threads.setStatus(run.thread_id, { checkpoint, exception }), threadGone);
|
|
122
151
|
}
|
|
123
152
|
if (webhook) {
|
|
124
153
|
await callWebhook({
|
package/dist/schemas.d.mts
CHANGED
|
@@ -1550,3 +1550,13 @@ export declare const StoreGetItem: z.ZodObject<{
|
|
|
1550
1550
|
namespace?: string | undefined;
|
|
1551
1551
|
}>;
|
|
1552
1552
|
export declare const coercedBoolean: z.ZodEffects<z.ZodString, boolean, string>;
|
|
1553
|
+
export declare const JoinStreamQuery: z.ZodObject<{
|
|
1554
|
+
cancel_on_disconnect: z.ZodOptional<z.ZodEffects<z.ZodString, boolean, string>>;
|
|
1555
|
+
stream_mode: z.ZodEffects<z.ZodOptional<z.ZodUnion<[z.ZodString, z.ZodArray<z.ZodString, "many">]>>, string[], string | string[] | undefined>;
|
|
1556
|
+
}, "strip", z.ZodTypeAny, {
|
|
1557
|
+
stream_mode: string[];
|
|
1558
|
+
cancel_on_disconnect?: boolean | undefined;
|
|
1559
|
+
}, {
|
|
1560
|
+
stream_mode?: string | string[] | undefined;
|
|
1561
|
+
cancel_on_disconnect?: string | undefined;
|
|
1562
|
+
}>;
|
package/dist/schemas.mjs
CHANGED
|
@@ -492,3 +492,21 @@ export const coercedBoolean = z.string().transform((val) => {
|
|
|
492
492
|
const lower = val.toLowerCase();
|
|
493
493
|
return lower === "true" || lower === "1" || lower === "yes";
|
|
494
494
|
});
|
|
495
|
+
// JS SDKs before 1.12 send several stream modes as one JSON array.
|
|
496
|
+
const parseStreamMode = (value) => {
|
|
497
|
+
if (!value.startsWith("["))
|
|
498
|
+
return value ? [value] : [];
|
|
499
|
+
try {
|
|
500
|
+
return JSON.parse(value);
|
|
501
|
+
}
|
|
502
|
+
catch {
|
|
503
|
+
return [value];
|
|
504
|
+
}
|
|
505
|
+
};
|
|
506
|
+
export const JoinStreamQuery = z.object({
|
|
507
|
+
cancel_on_disconnect: coercedBoolean.optional(),
|
|
508
|
+
stream_mode: z
|
|
509
|
+
.union([z.string(), z.array(z.string())])
|
|
510
|
+
.optional()
|
|
511
|
+
.transform((value) => [value ?? []].flat().flatMap(parseStreamMode)),
|
|
512
|
+
});
|
package/dist/storage/ops.d.mts
CHANGED
|
@@ -131,6 +131,7 @@ export declare class FileSystemThreads implements ThreadsRepo {
|
|
|
131
131
|
export declare class FileSystemRuns implements RunsRepo {
|
|
132
132
|
private readonly conn;
|
|
133
133
|
private readonly threads;
|
|
134
|
+
private requeuedPreviousProcessRuns;
|
|
134
135
|
readonly stream: RunsStreamRepo;
|
|
135
136
|
constructor(conn: FileSystemPersistence<Store>);
|
|
136
137
|
next(): AsyncGenerator<{
|
package/dist/storage/ops.mjs
CHANGED
|
@@ -870,6 +870,7 @@ export class FileSystemThreads {
|
|
|
870
870
|
export class FileSystemRuns {
|
|
871
871
|
conn;
|
|
872
872
|
threads;
|
|
873
|
+
requeuedPreviousProcessRuns = false;
|
|
873
874
|
stream;
|
|
874
875
|
constructor(conn) {
|
|
875
876
|
this.conn = conn;
|
|
@@ -877,6 +878,24 @@ export class FileSystemRuns {
|
|
|
877
878
|
this.stream = new FileSystemRuns.Stream(conn, this);
|
|
878
879
|
}
|
|
879
880
|
async *next() {
|
|
881
|
+
if (!this.requeuedPreviousProcessRuns) {
|
|
882
|
+
this.requeuedPreviousProcessRuns = true;
|
|
883
|
+
// No run is running in this process before the first claim, so a
|
|
884
|
+
// `running` run was cut off by a restart (a hot reload) and would
|
|
885
|
+
// otherwise block its thread for good.
|
|
886
|
+
await this.conn.with((STORE) => {
|
|
887
|
+
const cutOff = Object.values(STORE.runs).filter((run) => run.status === "running");
|
|
888
|
+
for (const run of cutOff) {
|
|
889
|
+
run.status = "pending";
|
|
890
|
+
run.updated_at = new Date();
|
|
891
|
+
}
|
|
892
|
+
if (cutOff.length) {
|
|
893
|
+
logger.warn("Retrying runs cut off by a server restart", {
|
|
894
|
+
run_ids: cutOff.map((run) => run.run_id),
|
|
895
|
+
});
|
|
896
|
+
}
|
|
897
|
+
});
|
|
898
|
+
}
|
|
880
899
|
yield* this.conn.withGenerator(async function* (STORE, options) {
|
|
881
900
|
const now = new Date();
|
|
882
901
|
const pendingRunIds = Object.values(STORE.runs)
|
|
@@ -1058,6 +1077,12 @@ export class FileSystemRuns {
|
|
|
1058
1077
|
throw new HTTPException(404, { message: "Run not found" });
|
|
1059
1078
|
}
|
|
1060
1079
|
}
|
|
1080
|
+
// Ends the run's open streams, which would otherwise wait out their
|
|
1081
|
+
// timeout and report the run as not found.
|
|
1082
|
+
StreamManager.readers[run_id]?.push({
|
|
1083
|
+
topic: `run:${run_id}:control`,
|
|
1084
|
+
data: "done",
|
|
1085
|
+
});
|
|
1061
1086
|
if (thread_id != null)
|
|
1062
1087
|
checkpointer.delete(thread_id, run_id);
|
|
1063
1088
|
delete STORE.runs[run_id];
|
|
@@ -1118,7 +1143,16 @@ export class FileSystemRuns {
|
|
|
1118
1143
|
const control = StreamManager.getControl(runId);
|
|
1119
1144
|
control?.abort(options.action ?? "interrupt");
|
|
1120
1145
|
if (run.status === "pending") {
|
|
1121
|
-
if (control
|
|
1146
|
+
if (control == null && run.kwargs.temporary) {
|
|
1147
|
+
// No worker will run it now, so nothing else deletes it, or its
|
|
1148
|
+
// temporary thread once no other run is left on it.
|
|
1149
|
+
delete STORE.runs[runId];
|
|
1150
|
+
const threadHasOtherRuns = Object.values(STORE.runs).some((other) => other.thread_id === run.thread_id);
|
|
1151
|
+
if (!threadHasOtherRuns) {
|
|
1152
|
+
promises.push(this.threads.delete(run.thread_id, undefined));
|
|
1153
|
+
}
|
|
1154
|
+
}
|
|
1155
|
+
else if (control || action !== "rollback") {
|
|
1122
1156
|
run.status = "interrupted";
|
|
1123
1157
|
run.updated_at = new Date();
|
|
1124
1158
|
const thread = STORE.threads[run.thread_id];
|
|
@@ -1135,7 +1169,7 @@ export class FileSystemRuns {
|
|
|
1135
1169
|
promises.push(this.delete(runId, threadId, auth));
|
|
1136
1170
|
}
|
|
1137
1171
|
}
|
|
1138
|
-
else {
|
|
1172
|
+
else if (run.status !== "running") {
|
|
1139
1173
|
logger.warn("Attempted to cancel non-pending run.", {
|
|
1140
1174
|
run_id: runId,
|
|
1141
1175
|
status: run.status,
|
|
@@ -1259,10 +1293,14 @@ export class FileSystemRuns {
|
|
|
1259
1293
|
}
|
|
1260
1294
|
}
|
|
1261
1295
|
}
|
|
1262
|
-
if (signal?.aborted &&
|
|
1263
|
-
|
|
1264
|
-
|
|
1265
|
-
|
|
1296
|
+
if (signal?.aborted && options?.cancelOnDisconnect) {
|
|
1297
|
+
// A stateless run is deleted with its thread as soon as it ends.
|
|
1298
|
+
await runs
|
|
1299
|
+
.cancel(threadId, [runId], { action: "interrupt" }, auth)
|
|
1300
|
+
.catch((error) => {
|
|
1301
|
+
if (!(error instanceof HTTPException) || error.status !== 404)
|
|
1302
|
+
throw error;
|
|
1303
|
+
});
|
|
1266
1304
|
}
|
|
1267
1305
|
});
|
|
1268
1306
|
}
|
package/dist/storage/types.d.mts
CHANGED
|
@@ -83,7 +83,7 @@ export interface Store {
|
|
|
83
83
|
retry_counter: Record<string, number>;
|
|
84
84
|
}
|
|
85
85
|
export interface Message {
|
|
86
|
-
topic: `run:${string}:stream:${string}`;
|
|
86
|
+
topic: `run:${string}:stream:${string}` | `run:${string}:control`;
|
|
87
87
|
data: unknown;
|
|
88
88
|
/**
|
|
89
89
|
* Marks payloads that have already been converted to their protocol
|
package/dist/stream.d.mts
CHANGED
|
@@ -41,6 +41,7 @@ export declare function streamState(run: Run, options: {
|
|
|
41
41
|
onTaskResult?: (taskResult: StreamTaskResult) => void;
|
|
42
42
|
signal?: AbortSignal;
|
|
43
43
|
}): AsyncGenerator<SourceStreamEvent>;
|
|
44
|
+
export declare const matchesStreamMode: (event: string, streamMode: string[]) => boolean;
|
|
44
45
|
/**
|
|
45
46
|
* Executes a graph run using `graph.streamEvents(..., { version: "v3" })`
|
|
46
47
|
* and maps the resulting `ProtocolEvent` objects into the `{ event, data }`
|
package/dist/stream.mjs
CHANGED
|
@@ -136,12 +136,16 @@ export async function* streamState(run, options) {
|
|
|
136
136
|
...(tracer && { callbacks: [tracer] }),
|
|
137
137
|
}));
|
|
138
138
|
const messages = {};
|
|
139
|
+
const messageIdByRun = {};
|
|
139
140
|
const completedIds = new Set();
|
|
140
141
|
for await (const event of events) {
|
|
141
142
|
if (event.tags?.includes("langsmith:hidden"))
|
|
142
143
|
continue;
|
|
143
|
-
|
|
144
|
-
|
|
144
|
+
// A runnable streamed inside a node emits on_chain_stream with a plain chunk.
|
|
145
|
+
const isGraphChunk = event.event === "on_chain_stream" &&
|
|
146
|
+
((kwargs.subgraphs && Array.isArray(event.data.chunk)) ||
|
|
147
|
+
event.run_id === run.run_id);
|
|
148
|
+
if (isGraphChunk) {
|
|
145
149
|
// Pregel stream chunks are `[ns, mode, payload]`. Lightweight checkpoint
|
|
146
150
|
// envelopes arrive as a separate `checkpoints` chunk before `values`.
|
|
147
151
|
const rawTuple = (kwargs.subgraphs ? event.data.chunk : [null, ...event.data.chunk]);
|
|
@@ -215,10 +219,14 @@ export async function* streamState(run, options) {
|
|
|
215
219
|
// - handleLLMEnd should not dedupe the message
|
|
216
220
|
// - Don't think there's an utility that would convert a BaseMessageChunk to a BaseMessage?
|
|
217
221
|
if (userStreamMode.includes("messages")) {
|
|
218
|
-
if (
|
|
219
|
-
(kwargs.subgraphs || event.run_id === run.run_id)) {
|
|
222
|
+
if (isGraphChunk) {
|
|
220
223
|
const newMessages = [];
|
|
221
|
-
const
|
|
224
|
+
const tuple = event.data.chunk;
|
|
225
|
+
// With subgraphs on, chunks are [ns, mode, chunk]; a subgraph's
|
|
226
|
+
// values are not the root graph's messages.
|
|
227
|
+
if (tuple.length === 3 && tuple[0].length > 0)
|
|
228
|
+
continue;
|
|
229
|
+
const chunk = tuple.at(-1);
|
|
222
230
|
let chunkMessages = [];
|
|
223
231
|
if (typeof chunk === "object" &&
|
|
224
232
|
chunk != null &&
|
|
@@ -242,19 +250,21 @@ export async function* streamState(run, options) {
|
|
|
242
250
|
else if (event.event === "on_chat_model_stream" &&
|
|
243
251
|
!event.tags?.includes("nostream")) {
|
|
244
252
|
const message = event.data.chunk;
|
|
245
|
-
|
|
253
|
+
// Anthropic sets the id on the first chunk only; the rest get run-<runId>.
|
|
254
|
+
const id = (messageIdByRun[event.run_id] ??= message.id);
|
|
255
|
+
if (!id)
|
|
246
256
|
continue;
|
|
247
|
-
if (messages[
|
|
248
|
-
messages[
|
|
257
|
+
if (messages[id] == null) {
|
|
258
|
+
messages[id] = message;
|
|
249
259
|
yield {
|
|
250
260
|
event: "messages/metadata",
|
|
251
|
-
data: { [
|
|
261
|
+
data: { [id]: { metadata: event.metadata } },
|
|
252
262
|
};
|
|
253
263
|
}
|
|
254
264
|
else {
|
|
255
|
-
messages[
|
|
265
|
+
messages[id] = messages[id].concat(message);
|
|
256
266
|
}
|
|
257
|
-
yield { event: "messages/partial", data: [messages[
|
|
267
|
+
yield { event: "messages/partial", data: [messages[id]] };
|
|
258
268
|
}
|
|
259
269
|
}
|
|
260
270
|
}
|
|
@@ -267,6 +277,18 @@ export async function* streamState(run, options) {
|
|
|
267
277
|
yield { event: "feedback", data };
|
|
268
278
|
}
|
|
269
279
|
}
|
|
280
|
+
export const matchesStreamMode = (event, streamMode) => {
|
|
281
|
+
// A run's error is its outcome, not a mode, so no filter drops it.
|
|
282
|
+
if (streamMode.length === 0 || event === "error")
|
|
283
|
+
return true;
|
|
284
|
+
const mode = event.split("|")[0];
|
|
285
|
+
if (streamMode.includes(mode))
|
|
286
|
+
return true;
|
|
287
|
+
// The messages modes stream `messages` (messages-tuple) and `messages/*`
|
|
288
|
+
// (messages) events; as on the Agent Server, either mode keeps both.
|
|
289
|
+
return (mode.startsWith("messages") &&
|
|
290
|
+
(streamMode.includes("messages") || streamMode.includes("messages-tuple")));
|
|
291
|
+
};
|
|
270
292
|
function isUnsupportedStreamEventsV3Error(error) {
|
|
271
293
|
return (error instanceof Error &&
|
|
272
294
|
error.message.includes('Only versions "v1" and "v2" of the schema are currently supported'));
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@langchain/langgraph-api",
|
|
3
|
-
"version": "1.5.
|
|
3
|
+
"version": "1.5.8",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"engines": {
|
|
6
6
|
"node": "^18.19.0 || >=20.16.0"
|
|
@@ -52,7 +52,7 @@
|
|
|
52
52
|
},
|
|
53
53
|
"dependencies": {
|
|
54
54
|
"@babel/code-frame": "^7.26.2",
|
|
55
|
-
"@hono/node-server": "^2.1.
|
|
55
|
+
"@hono/node-server": "^2.1.3",
|
|
56
56
|
"@hono/node-ws": "^1.3.0",
|
|
57
57
|
"@hono/zod-validator": "^0.7.6",
|
|
58
58
|
"@langchain/protocol": "^0.0.20",
|
|
@@ -71,7 +71,7 @@
|
|
|
71
71
|
"winston": "^3.17.0",
|
|
72
72
|
"winston-console-format": "^1.0.8",
|
|
73
73
|
"zod": "^4.6.6",
|
|
74
|
-
"@langchain/langgraph-ui": "1.5.
|
|
74
|
+
"@langchain/langgraph-ui": "1.5.8"
|
|
75
75
|
},
|
|
76
76
|
"peerDependencies": {
|
|
77
77
|
"@langchain/core": "^1.1.48",
|
|
@@ -90,7 +90,7 @@
|
|
|
90
90
|
}
|
|
91
91
|
},
|
|
92
92
|
"devDependencies": {
|
|
93
|
-
"@langchain/core": "^1.2
|
|
93
|
+
"@langchain/core": "^1.3.2",
|
|
94
94
|
"@types/babel__code-frame": "^7.0.6",
|
|
95
95
|
"@types/node": "^18.15.11",
|
|
96
96
|
"@types/react": "^19.3.0",
|
|
@@ -98,15 +98,15 @@
|
|
|
98
98
|
"@types/semver": "^7.7.0",
|
|
99
99
|
"deepagents": "^1.10.5",
|
|
100
100
|
"jose": "^6.0.10",
|
|
101
|
-
"langchain": "^1.
|
|
101
|
+
"langchain": "^1.6.0",
|
|
102
102
|
"postgres": "^3.4.5",
|
|
103
103
|
"ts-node": "^10.9.2",
|
|
104
104
|
"typescript": "^4.9.5 || ^5.4.5",
|
|
105
105
|
"vitest": "^4.1.12",
|
|
106
106
|
"wait-port": "^1.1.0",
|
|
107
|
-
"@langchain/langgraph": "1.
|
|
108
|
-
"@langchain/langgraph-
|
|
109
|
-
"@langchain/langgraph-
|
|
107
|
+
"@langchain/langgraph": "1.6.1",
|
|
108
|
+
"@langchain/langgraph-sdk": "1.12.10",
|
|
109
|
+
"@langchain/langgraph-checkpoint": "1.2.2"
|
|
110
110
|
},
|
|
111
111
|
"scripts": {
|
|
112
112
|
"clean": "rm -rf dist/ .turbo/ ./tests/graphs/.langgraph_api/ ./tests/protocol-v2/graphs/.langgraph_api/",
|