@langchain/langgraph-api 1.2.1 → 1.2.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/semver/index.mjs +1 -19
- package/dist/semver/satisfiesPeerRange.d.mts +1 -0
- package/dist/semver/satisfiesPeerRange.mjs +19 -0
- package/dist/src/api/assistants.d.mts +3 -0
- package/dist/src/api/assistants.mjs +194 -0
- package/dist/src/api/meta.d.mts +3 -0
- package/dist/src/api/meta.mjs +65 -0
- package/dist/src/api/protocol.d.mts +7 -0
- package/dist/src/api/protocol.mjs +157 -0
- package/dist/src/api/runs.d.mts +3 -0
- package/dist/src/api/runs.mjs +335 -0
- package/dist/src/api/store.d.mts +3 -0
- package/dist/src/api/store.mjs +111 -0
- package/dist/src/api/threads.d.mts +3 -0
- package/dist/src/api/threads.mjs +143 -0
- package/dist/src/graph/load.utils.d.mts +22 -0
- package/dist/src/graph/load.utils.mjs +59 -0
- package/dist/src/protocol/service.d.mts +101 -0
- package/dist/src/protocol/service.mjs +568 -0
- package/dist/src/protocol/session/event-normalizers.d.mts +52 -0
- package/dist/src/protocol/session/index.d.mts +261 -0
- package/dist/src/protocol/session/index.mjs +826 -0
- package/dist/src/protocol/session/namespace.d.mts +47 -0
- package/dist/src/protocol/session/namespace.mjs +62 -0
- package/dist/src/protocol/types.d.mts +121 -0
- package/dist/src/protocol/types.mjs +1 -0
- package/dist/src/schemas.d.mts +1552 -0
- package/dist/src/semver/index.d.mts +15 -0
- package/dist/src/semver/index.mjs +46 -0
- package/dist/src/semver/satisfiesPeerRange.d.mts +1 -0
- package/dist/src/semver/satisfiesPeerRange.mjs +19 -0
- package/dist/src/state.d.mts +3 -0
- package/dist/src/state.mjs +30 -0
- package/dist/src/storage/context.d.mts +3 -0
- package/dist/src/storage/context.mjs +11 -0
- package/dist/src/storage/ops.mjs +1281 -0
- package/dist/src/stream.d.mts +64 -0
- package/dist/src/stream.mjs +427 -0
- package/dist/src/utils/hono.d.mts +5 -0
- package/dist/src/utils/hono.mjs +24 -0
- package/dist/src/utils/runnableConfig.d.mts +3 -0
- package/dist/src/utils/runnableConfig.mjs +45 -0
- package/dist/src/webhook.d.mts +11 -0
- package/dist/src/webhook.mjs +30 -0
- package/package.json +7 -7
|
@@ -0,0 +1,335 @@
|
|
|
1
|
+
import { zValidator } from "@hono/zod-validator";
|
|
2
|
+
import { Hono } from "hono";
|
|
3
|
+
import { HTTPException } from "hono/http-exception";
|
|
4
|
+
import { streamSSE } from "hono/streaming";
|
|
5
|
+
import { v7 as uuid7 } from "uuid";
|
|
6
|
+
import { z } from "zod/v3";
|
|
7
|
+
import { getAssistantId } from "../graph/load.mjs";
|
|
8
|
+
import { logError, logger } from "../logging.mjs";
|
|
9
|
+
import * as schemas from "../schemas.mjs";
|
|
10
|
+
import { runs, threads } from "../storage/context.mjs";
|
|
11
|
+
import { getDisconnectAbortSignal, jsonExtra, waitKeepAlive, } from "../utils/hono.mjs";
|
|
12
|
+
import { serialiseAsDict } from "../utils/serde.mjs";
|
|
13
|
+
const api = new Hono();
|
|
14
|
+
const createValidRun = async (threadId, payload, kwargs) => {
|
|
15
|
+
const { assistant_id: assistantId, ...run } = payload;
|
|
16
|
+
const { auth, headers } = kwargs ?? {};
|
|
17
|
+
const runId = uuid7();
|
|
18
|
+
const streamMode = Array.isArray(payload.stream_mode)
|
|
19
|
+
? payload.stream_mode
|
|
20
|
+
: payload.stream_mode != null
|
|
21
|
+
? [payload.stream_mode]
|
|
22
|
+
: [];
|
|
23
|
+
if (streamMode.length === 0)
|
|
24
|
+
streamMode.push("values");
|
|
25
|
+
const multitaskStrategy = payload.multitask_strategy ?? "reject";
|
|
26
|
+
const preventInsertInInflight = multitaskStrategy === "reject";
|
|
27
|
+
const config = { ...run.config };
|
|
28
|
+
if (run.checkpoint_id) {
|
|
29
|
+
config.configurable ??= {};
|
|
30
|
+
config.configurable.checkpoint_id = run.checkpoint_id;
|
|
31
|
+
}
|
|
32
|
+
if (run.checkpoint) {
|
|
33
|
+
config.configurable ??= {};
|
|
34
|
+
Object.assign(config.configurable, run.checkpoint);
|
|
35
|
+
}
|
|
36
|
+
if (run.langsmith_tracer) {
|
|
37
|
+
config.configurable ??= {};
|
|
38
|
+
Object.assign(config.configurable, {
|
|
39
|
+
langsmith_project: run.langsmith_tracer.project_name,
|
|
40
|
+
langsmith_example_id: run.langsmith_tracer.example_id,
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
if (headers) {
|
|
44
|
+
for (const [rawKey, value] of headers.entries()) {
|
|
45
|
+
const key = rawKey.toLowerCase();
|
|
46
|
+
if (key.startsWith("x-")) {
|
|
47
|
+
if (["x-api-key", "x-tenant-id", "x-service-key"].includes(key)) {
|
|
48
|
+
continue;
|
|
49
|
+
}
|
|
50
|
+
config.configurable ??= {};
|
|
51
|
+
config.configurable[key] = value;
|
|
52
|
+
}
|
|
53
|
+
else if (key === "user-agent") {
|
|
54
|
+
config.configurable ??= {};
|
|
55
|
+
config.configurable[key] = value;
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
let userId;
|
|
60
|
+
if (auth) {
|
|
61
|
+
userId = auth.user.identity ?? auth.user.id;
|
|
62
|
+
config.configurable ??= {};
|
|
63
|
+
config.configurable["langgraph_auth_user"] = auth.user;
|
|
64
|
+
config.configurable["langgraph_auth_user_id"] = userId;
|
|
65
|
+
config.configurable["langgraph_auth_permissions"] = auth.scopes;
|
|
66
|
+
}
|
|
67
|
+
let feedbackKeys = run.feedback_keys != null
|
|
68
|
+
? Array.isArray(run.feedback_keys)
|
|
69
|
+
? run.feedback_keys
|
|
70
|
+
: [run.feedback_keys]
|
|
71
|
+
: undefined;
|
|
72
|
+
if (!feedbackKeys?.length)
|
|
73
|
+
feedbackKeys = undefined;
|
|
74
|
+
const [first, ...inflight] = await runs().put(runId, getAssistantId(assistantId), {
|
|
75
|
+
input: run.input,
|
|
76
|
+
command: run.command,
|
|
77
|
+
config,
|
|
78
|
+
context: run.context,
|
|
79
|
+
stream_mode: streamMode,
|
|
80
|
+
interrupt_before: run.interrupt_before,
|
|
81
|
+
interrupt_after: run.interrupt_after,
|
|
82
|
+
webhook: run.webhook,
|
|
83
|
+
feedback_keys: feedbackKeys,
|
|
84
|
+
temporary: threadId == null && (run.on_completion ?? "delete") === "delete",
|
|
85
|
+
subgraphs: run.stream_subgraphs ?? false,
|
|
86
|
+
resumable: run.stream_resumable ?? false,
|
|
87
|
+
}, {
|
|
88
|
+
threadId,
|
|
89
|
+
userId,
|
|
90
|
+
metadata: run.metadata,
|
|
91
|
+
status: "pending",
|
|
92
|
+
multitaskStrategy,
|
|
93
|
+
preventInsertInInflight,
|
|
94
|
+
afterSeconds: payload.after_seconds,
|
|
95
|
+
ifNotExists: payload.if_not_exists,
|
|
96
|
+
}, auth);
|
|
97
|
+
if (first?.run_id === runId) {
|
|
98
|
+
logger.info("Created run", { run_id: runId, thread_id: threadId });
|
|
99
|
+
if ((multitaskStrategy === "interrupt" || multitaskStrategy === "rollback") &&
|
|
100
|
+
inflight.length > 0) {
|
|
101
|
+
try {
|
|
102
|
+
await runs().cancel(threadId, inflight.map((run) => run.run_id), { action: multitaskStrategy }, auth);
|
|
103
|
+
}
|
|
104
|
+
catch (error) {
|
|
105
|
+
logger.warn("Failed to cancel inflight runs, might be already cancelled", {
|
|
106
|
+
error,
|
|
107
|
+
run_ids: inflight.map((run) => run.run_id),
|
|
108
|
+
thread_id: threadId,
|
|
109
|
+
});
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
return first;
|
|
113
|
+
}
|
|
114
|
+
else if (multitaskStrategy === "reject") {
|
|
115
|
+
throw new HTTPException(422, {
|
|
116
|
+
message: "Thread is already running a task. Wait for it to finish or choose a different multitask strategy.",
|
|
117
|
+
});
|
|
118
|
+
}
|
|
119
|
+
throw new HTTPException(500, {
|
|
120
|
+
message: "Unreachable state when creating run",
|
|
121
|
+
});
|
|
122
|
+
};
|
|
123
|
+
api.post("/runs/crons", zValidator("json", schemas.CronCreate), async () => {
|
|
124
|
+
// Create Thread Cron
|
|
125
|
+
throw new HTTPException(500, { message: "Not implemented" });
|
|
126
|
+
});
|
|
127
|
+
api.post("/runs/crons/search", zValidator("json", schemas.CronSearch), async () => {
|
|
128
|
+
// Search Crons
|
|
129
|
+
throw new HTTPException(500, { message: "Not implemented" });
|
|
130
|
+
});
|
|
131
|
+
api.delete("/runs/crons/:cron_id", zValidator("param", z.object({ cron_id: z.string().uuid() })), async () => {
|
|
132
|
+
// Delete Cron
|
|
133
|
+
throw new HTTPException(500, { message: "Not implemented" });
|
|
134
|
+
});
|
|
135
|
+
api.post("/threads/:thread_id/runs/crons", zValidator("param", z.object({ thread_id: z.string().uuid() })), zValidator("json", schemas.CronCreate), async () => {
|
|
136
|
+
// Create Thread Cron
|
|
137
|
+
throw new HTTPException(500, { message: "Not implemented" });
|
|
138
|
+
});
|
|
139
|
+
api.post("/runs/stream", zValidator("json", schemas.RunCreate), async (c) => {
|
|
140
|
+
// Stream Stateless Run
|
|
141
|
+
const payload = c.req.valid("json");
|
|
142
|
+
const run = await createValidRun(undefined, payload, {
|
|
143
|
+
auth: c.var.auth,
|
|
144
|
+
headers: c.req.raw.headers,
|
|
145
|
+
});
|
|
146
|
+
c.header("Content-Location", `/runs/${run.run_id}`);
|
|
147
|
+
return streamSSE(c, async (stream) => {
|
|
148
|
+
const cancelOnDisconnect = payload.on_disconnect === "cancel"
|
|
149
|
+
? getDisconnectAbortSignal(c, stream)
|
|
150
|
+
: undefined;
|
|
151
|
+
try {
|
|
152
|
+
for await (const { event, data } of runs().stream.join(run.run_id, undefined, {
|
|
153
|
+
signal: cancelOnDisconnect,
|
|
154
|
+
cancelOnDisconnect: cancelOnDisconnect != null,
|
|
155
|
+
lastEventId: run.kwargs.resumable ? "-1" : undefined,
|
|
156
|
+
ignore404: true,
|
|
157
|
+
}, c.var.auth)) {
|
|
158
|
+
await stream.writeSSE({ data: serialiseAsDict(data), event });
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
catch (error) {
|
|
162
|
+
logError(error, { prefix: "Error streaming run" });
|
|
163
|
+
}
|
|
164
|
+
});
|
|
165
|
+
});
|
|
166
|
+
// TODO: port to Python API
|
|
167
|
+
api.get("/runs/:run_id/stream", zValidator("param", z.object({ run_id: z.string().uuid() })), zValidator("query", z.object({ cancel_on_disconnect: schemas.coercedBoolean.optional() })), async (c) => {
|
|
168
|
+
const { run_id } = c.req.valid("param");
|
|
169
|
+
const query = c.req.valid("query");
|
|
170
|
+
const lastEventId = c.req.header("Last-Event-ID") || undefined;
|
|
171
|
+
c.header("Content-Location", `/runs/${run_id}`);
|
|
172
|
+
return streamSSE(c, async (stream) => {
|
|
173
|
+
const cancelOnDisconnect = query.cancel_on_disconnect
|
|
174
|
+
? getDisconnectAbortSignal(c, stream)
|
|
175
|
+
: undefined;
|
|
176
|
+
try {
|
|
177
|
+
for await (const { id, event, data } of runs().stream.join(run_id, undefined, {
|
|
178
|
+
signal: cancelOnDisconnect,
|
|
179
|
+
cancelOnDisconnect: cancelOnDisconnect != null,
|
|
180
|
+
lastEventId,
|
|
181
|
+
ignore404: true,
|
|
182
|
+
}, c.var.auth)) {
|
|
183
|
+
await stream.writeSSE({ id, data: serialiseAsDict(data), event });
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
catch (error) {
|
|
187
|
+
logError(error, { prefix: "Error streaming run" });
|
|
188
|
+
}
|
|
189
|
+
});
|
|
190
|
+
});
|
|
191
|
+
api.post("/runs/wait", zValidator("json", schemas.RunCreate), async (c) => {
|
|
192
|
+
// Wait Stateless Run
|
|
193
|
+
const payload = c.req.valid("json");
|
|
194
|
+
const run = await createValidRun(undefined, payload, {
|
|
195
|
+
auth: c.var.auth,
|
|
196
|
+
headers: c.req.raw.headers,
|
|
197
|
+
});
|
|
198
|
+
c.header("Content-Location", `/runs/${run.run_id}`);
|
|
199
|
+
return waitKeepAlive(c, runs().wait(run.run_id, undefined, c.var.auth));
|
|
200
|
+
});
|
|
201
|
+
api.post("/runs", zValidator("json", schemas.RunCreate), async (c) => {
|
|
202
|
+
// Create Stateless Run
|
|
203
|
+
const payload = c.req.valid("json");
|
|
204
|
+
const run = await createValidRun(undefined, payload, {
|
|
205
|
+
auth: c.var.auth,
|
|
206
|
+
headers: c.req.raw.headers,
|
|
207
|
+
});
|
|
208
|
+
c.header("Content-Location", `/runs/${run.run_id}`);
|
|
209
|
+
return jsonExtra(c, run);
|
|
210
|
+
});
|
|
211
|
+
api.post("/runs/batch", zValidator("json", schemas.RunBatchCreate), async (c) => {
|
|
212
|
+
// Batch Runs
|
|
213
|
+
const payload = c.req.valid("json");
|
|
214
|
+
const runs = await Promise.all(payload.map((run) => createValidRun(undefined, run, {
|
|
215
|
+
auth: c.var.auth,
|
|
216
|
+
headers: c.req.raw.headers,
|
|
217
|
+
})));
|
|
218
|
+
return jsonExtra(c, runs);
|
|
219
|
+
});
|
|
220
|
+
api.get("/threads/:thread_id/runs", zValidator("param", z.object({ thread_id: z.string().uuid() })), zValidator("query", z.object({
|
|
221
|
+
limit: z.coerce.number().nullish(),
|
|
222
|
+
offset: z.coerce.number().nullish(),
|
|
223
|
+
status: z.string().nullish(),
|
|
224
|
+
metadata: z.record(z.string(), z.unknown()).nullish(),
|
|
225
|
+
})), async (c) => {
|
|
226
|
+
// List runs
|
|
227
|
+
const { thread_id } = c.req.valid("param");
|
|
228
|
+
const { limit, offset, status, metadata } = c.req.valid("query");
|
|
229
|
+
const [runsResponse] = await Promise.all([
|
|
230
|
+
runs().search(thread_id, { limit, offset, status, metadata }, c.var.auth),
|
|
231
|
+
threads().get(thread_id, c.var.auth),
|
|
232
|
+
]);
|
|
233
|
+
return jsonExtra(c, runsResponse);
|
|
234
|
+
});
|
|
235
|
+
api.post("/threads/:thread_id/runs", zValidator("param", z.object({ thread_id: z.string().uuid() })), zValidator("json", schemas.RunCreate), async (c) => {
|
|
236
|
+
// Create Run
|
|
237
|
+
const { thread_id } = c.req.valid("param");
|
|
238
|
+
const payload = c.req.valid("json");
|
|
239
|
+
const run = await createValidRun(thread_id, payload, {
|
|
240
|
+
auth: c.var.auth,
|
|
241
|
+
headers: c.req.raw.headers,
|
|
242
|
+
});
|
|
243
|
+
c.header("Content-Location", `/threads/${thread_id}/runs/${run.run_id}`);
|
|
244
|
+
return jsonExtra(c, run);
|
|
245
|
+
});
|
|
246
|
+
api.post("/threads/:thread_id/runs/stream", zValidator("param", z.object({ thread_id: z.string().uuid() })), zValidator("json", schemas.RunCreate), async (c) => {
|
|
247
|
+
// Stream Run
|
|
248
|
+
const { thread_id } = c.req.valid("param");
|
|
249
|
+
const payload = c.req.valid("json");
|
|
250
|
+
const run = await createValidRun(thread_id, payload, {
|
|
251
|
+
auth: c.var.auth,
|
|
252
|
+
headers: c.req.raw.headers,
|
|
253
|
+
});
|
|
254
|
+
c.header("Content-Location", `/threads/${thread_id}/runs/${run.run_id}`);
|
|
255
|
+
return streamSSE(c, async (stream) => {
|
|
256
|
+
const cancelOnDisconnect = payload.on_disconnect === "cancel"
|
|
257
|
+
? getDisconnectAbortSignal(c, stream)
|
|
258
|
+
: undefined;
|
|
259
|
+
try {
|
|
260
|
+
for await (const { id, event, data } of runs().stream.join(run.run_id, thread_id, {
|
|
261
|
+
signal: cancelOnDisconnect,
|
|
262
|
+
cancelOnDisconnect: cancelOnDisconnect != null,
|
|
263
|
+
lastEventId: run.kwargs.resumable ? "-1" : undefined,
|
|
264
|
+
}, c.var.auth)) {
|
|
265
|
+
await stream.writeSSE({ id, data: serialiseAsDict(data), event });
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
catch (error) {
|
|
269
|
+
logError(error, { prefix: "Error streaming run" });
|
|
270
|
+
}
|
|
271
|
+
});
|
|
272
|
+
});
|
|
273
|
+
api.post("/threads/:thread_id/runs/wait", zValidator("param", z.object({ thread_id: z.string().uuid() })), zValidator("json", schemas.RunCreate), async (c) => {
|
|
274
|
+
// Wait Run
|
|
275
|
+
const { thread_id } = c.req.valid("param");
|
|
276
|
+
const payload = c.req.valid("json");
|
|
277
|
+
const run = await createValidRun(thread_id, payload, {
|
|
278
|
+
auth: c.var.auth,
|
|
279
|
+
headers: c.req.raw.headers,
|
|
280
|
+
});
|
|
281
|
+
c.header("Content-Location", `/threads/${thread_id}/runs/${run.run_id}`);
|
|
282
|
+
return waitKeepAlive(c, runs().join(run.run_id, thread_id, c.var.auth));
|
|
283
|
+
});
|
|
284
|
+
api.get("/threads/:thread_id/runs/:run_id", zValidator("param", z.object({ thread_id: z.string().uuid(), run_id: z.string().uuid() })), async (c) => {
|
|
285
|
+
const { thread_id, run_id } = c.req.valid("param");
|
|
286
|
+
const [run] = await Promise.all([
|
|
287
|
+
runs().get(run_id, thread_id, c.var.auth),
|
|
288
|
+
threads().get(thread_id, c.var.auth),
|
|
289
|
+
]);
|
|
290
|
+
if (run == null)
|
|
291
|
+
throw new HTTPException(404, { message: "Run not found" });
|
|
292
|
+
return jsonExtra(c, run);
|
|
293
|
+
});
|
|
294
|
+
api.delete("/threads/:thread_id/runs/:run_id", zValidator("param", z.object({ thread_id: z.string().uuid(), run_id: z.string().uuid() })), async (c) => {
|
|
295
|
+
// Delete Run
|
|
296
|
+
const { thread_id, run_id } = c.req.valid("param");
|
|
297
|
+
await runs().delete(run_id, thread_id, c.var.auth);
|
|
298
|
+
return c.body(null, 204);
|
|
299
|
+
});
|
|
300
|
+
api.get("/threads/:thread_id/runs/:run_id/join", zValidator("param", z.object({ thread_id: z.string().uuid(), run_id: z.string().uuid() })), async (c) => {
|
|
301
|
+
// Join Run Http
|
|
302
|
+
const { thread_id, run_id } = c.req.valid("param");
|
|
303
|
+
return jsonExtra(c, await runs().join(run_id, thread_id, c.var.auth));
|
|
304
|
+
});
|
|
305
|
+
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", z.object({ cancel_on_disconnect: schemas.coercedBoolean.optional() })), async (c) => {
|
|
306
|
+
// Stream Run Http
|
|
307
|
+
const { thread_id, run_id } = c.req.valid("param");
|
|
308
|
+
const { cancel_on_disconnect } = c.req.valid("query");
|
|
309
|
+
const lastEventId = c.req.header("Last-Event-ID") || undefined;
|
|
310
|
+
return streamSSE(c, async (stream) => {
|
|
311
|
+
const signal = cancel_on_disconnect
|
|
312
|
+
? getDisconnectAbortSignal(c, stream)
|
|
313
|
+
: undefined;
|
|
314
|
+
for await (const { id, event, data } of runs().stream.join(run_id, thread_id, {
|
|
315
|
+
signal,
|
|
316
|
+
cancelOnDisconnect: signal != null,
|
|
317
|
+
lastEventId,
|
|
318
|
+
}, c.var.auth)) {
|
|
319
|
+
await stream.writeSSE({ id, data: serialiseAsDict(data), event });
|
|
320
|
+
}
|
|
321
|
+
});
|
|
322
|
+
});
|
|
323
|
+
api.post("/threads/:thread_id/runs/:run_id/cancel", zValidator("param", z.object({ thread_id: z.string().uuid(), run_id: z.string().uuid() })), zValidator("query", z.object({
|
|
324
|
+
wait: z.coerce.boolean().optional().default(false),
|
|
325
|
+
action: z.enum(["interrupt", "rollback"]).optional().default("interrupt"),
|
|
326
|
+
})), async (c) => {
|
|
327
|
+
// Cancel Run Http
|
|
328
|
+
const { thread_id, run_id } = c.req.valid("param");
|
|
329
|
+
const { wait, action } = c.req.valid("query");
|
|
330
|
+
await runs().cancel(thread_id, [run_id], { action }, c.var.auth);
|
|
331
|
+
if (wait)
|
|
332
|
+
await runs().join(run_id, thread_id, c.var.auth);
|
|
333
|
+
return c.body(null, wait ? 204 : 202);
|
|
334
|
+
});
|
|
335
|
+
export default api;
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
import { Hono } from "hono";
|
|
2
|
+
import { zValidator } from "@hono/zod-validator";
|
|
3
|
+
import * as schemas from "../schemas.mjs";
|
|
4
|
+
import { HTTPException } from "hono/http-exception";
|
|
5
|
+
import { store as storageStore } from "../storage/store.mjs";
|
|
6
|
+
import { handleAuthEvent } from "../auth/index.mjs";
|
|
7
|
+
const api = new Hono();
|
|
8
|
+
const validateNamespace = (namespace) => {
|
|
9
|
+
if (!namespace || namespace.length === 0) {
|
|
10
|
+
throw new HTTPException(400, { message: "Namespace is required" });
|
|
11
|
+
}
|
|
12
|
+
for (const label of namespace) {
|
|
13
|
+
if (!label || label.includes(".")) {
|
|
14
|
+
throw new HTTPException(422, {
|
|
15
|
+
message: "Namespace labels cannot be empty or contain periods. Received: " +
|
|
16
|
+
namespace.join("."),
|
|
17
|
+
});
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
};
|
|
21
|
+
const mapItemsToApi = (item) => {
|
|
22
|
+
if (item == null)
|
|
23
|
+
return null;
|
|
24
|
+
const clonedItem = { ...item };
|
|
25
|
+
delete clonedItem.createdAt;
|
|
26
|
+
delete clonedItem.updatedAt;
|
|
27
|
+
clonedItem.created_at = item.createdAt;
|
|
28
|
+
clonedItem.updated_at = item.updatedAt;
|
|
29
|
+
return clonedItem;
|
|
30
|
+
};
|
|
31
|
+
api.post("/store/namespaces", zValidator("json", schemas.StoreListNamespaces), async (c) => {
|
|
32
|
+
// List Namespaces
|
|
33
|
+
const payload = c.req.valid("json");
|
|
34
|
+
if (payload.prefix)
|
|
35
|
+
validateNamespace(payload.prefix);
|
|
36
|
+
if (payload.suffix)
|
|
37
|
+
validateNamespace(payload.suffix);
|
|
38
|
+
await handleAuthEvent(c.var.auth, "store:list_namespaces", {
|
|
39
|
+
namespace: payload.prefix,
|
|
40
|
+
suffix: payload.suffix,
|
|
41
|
+
max_depth: payload.max_depth,
|
|
42
|
+
limit: payload.limit,
|
|
43
|
+
offset: payload.offset,
|
|
44
|
+
});
|
|
45
|
+
return c.json({
|
|
46
|
+
namespaces: await storageStore.listNamespaces({
|
|
47
|
+
limit: payload.limit ?? 100,
|
|
48
|
+
offset: payload.offset ?? 0,
|
|
49
|
+
prefix: payload.prefix,
|
|
50
|
+
suffix: payload.suffix,
|
|
51
|
+
maxDepth: payload.max_depth,
|
|
52
|
+
}),
|
|
53
|
+
});
|
|
54
|
+
});
|
|
55
|
+
api.post("/store/items/search", zValidator("json", schemas.StoreSearchItems), async (c) => {
|
|
56
|
+
// Search Items
|
|
57
|
+
const payload = c.req.valid("json");
|
|
58
|
+
if (payload.namespace_prefix)
|
|
59
|
+
validateNamespace(payload.namespace_prefix);
|
|
60
|
+
await handleAuthEvent(c.var.auth, "store:search", {
|
|
61
|
+
namespace: payload.namespace_prefix,
|
|
62
|
+
filter: payload.filter,
|
|
63
|
+
limit: payload.limit,
|
|
64
|
+
offset: payload.offset,
|
|
65
|
+
query: payload.query,
|
|
66
|
+
});
|
|
67
|
+
const items = await storageStore.search(payload.namespace_prefix, {
|
|
68
|
+
filter: payload.filter,
|
|
69
|
+
limit: payload.limit ?? 10,
|
|
70
|
+
offset: payload.offset ?? 0,
|
|
71
|
+
query: payload.query,
|
|
72
|
+
});
|
|
73
|
+
return c.json({ items: items.map(mapItemsToApi) });
|
|
74
|
+
});
|
|
75
|
+
api.put("/store/items", zValidator("json", schemas.StorePutItem), async (c) => {
|
|
76
|
+
// Put Item
|
|
77
|
+
const payload = c.req.valid("json");
|
|
78
|
+
if (payload.namespace)
|
|
79
|
+
validateNamespace(payload.namespace);
|
|
80
|
+
await handleAuthEvent(c.var.auth, "store:put", {
|
|
81
|
+
namespace: payload.namespace,
|
|
82
|
+
key: payload.key,
|
|
83
|
+
value: payload.value,
|
|
84
|
+
});
|
|
85
|
+
await storageStore.put(payload.namespace, payload.key, payload.value);
|
|
86
|
+
return c.body(null, 204);
|
|
87
|
+
});
|
|
88
|
+
api.delete("/store/items", zValidator("json", schemas.StoreDeleteItem), async (c) => {
|
|
89
|
+
// Delete Item
|
|
90
|
+
const payload = c.req.valid("json");
|
|
91
|
+
if (payload.namespace)
|
|
92
|
+
validateNamespace(payload.namespace);
|
|
93
|
+
await handleAuthEvent(c.var.auth, "store:delete", {
|
|
94
|
+
namespace: payload.namespace,
|
|
95
|
+
key: payload.key,
|
|
96
|
+
});
|
|
97
|
+
await storageStore.delete(payload.namespace ?? [], payload.key);
|
|
98
|
+
return c.body(null, 204);
|
|
99
|
+
});
|
|
100
|
+
api.get("/store/items", zValidator("query", schemas.StoreGetItem), async (c) => {
|
|
101
|
+
// Get Item
|
|
102
|
+
const payload = c.req.valid("query");
|
|
103
|
+
await handleAuthEvent(c.var.auth, "store:get", {
|
|
104
|
+
namespace: payload.namespace,
|
|
105
|
+
key: payload.key,
|
|
106
|
+
});
|
|
107
|
+
const key = payload.key;
|
|
108
|
+
const namespace = payload.namespace;
|
|
109
|
+
return c.json(mapItemsToApi(await storageStore.get(namespace, key)));
|
|
110
|
+
});
|
|
111
|
+
export default api;
|
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
import { zValidator } from "@hono/zod-validator";
|
|
2
|
+
import { Hono } from "hono";
|
|
3
|
+
import { v7 as uuid7 } from "uuid";
|
|
4
|
+
import { z } from "zod/v3";
|
|
5
|
+
import * as schemas from "../schemas.mjs";
|
|
6
|
+
import { stateSnapshotToThreadState } from "../state.mjs";
|
|
7
|
+
import { threads } from "../storage/context.mjs";
|
|
8
|
+
import { jsonExtra } from "../utils/hono.mjs";
|
|
9
|
+
const api = new Hono();
|
|
10
|
+
// Threads Routes
|
|
11
|
+
api.post("/threads", zValidator("json", schemas.ThreadCreate), async (c) => {
|
|
12
|
+
// Create Thread
|
|
13
|
+
const payload = c.req.valid("json");
|
|
14
|
+
const thread = await threads().put(payload.thread_id || uuid7(), { metadata: payload.metadata, if_exists: payload.if_exists ?? "raise" }, c.var.auth);
|
|
15
|
+
if (payload.supersteps?.length) {
|
|
16
|
+
await threads().state.bulk({ configurable: { thread_id: thread.thread_id } }, payload.supersteps, c.var.auth);
|
|
17
|
+
}
|
|
18
|
+
return jsonExtra(c, thread);
|
|
19
|
+
});
|
|
20
|
+
api.post("/threads/search", zValidator("json", schemas.ThreadSearchRequest), async (c) => {
|
|
21
|
+
// Search Threads
|
|
22
|
+
const payload = c.req.valid("json");
|
|
23
|
+
const result = [];
|
|
24
|
+
let total = 0;
|
|
25
|
+
for await (const item of threads().search({
|
|
26
|
+
status: payload.status,
|
|
27
|
+
values: payload.values,
|
|
28
|
+
metadata: payload.metadata,
|
|
29
|
+
ids: payload.ids,
|
|
30
|
+
limit: payload.limit ?? 10,
|
|
31
|
+
offset: payload.offset ?? 0,
|
|
32
|
+
sort_by: payload.sort_by ?? "created_at",
|
|
33
|
+
sort_order: payload.sort_order ?? "desc",
|
|
34
|
+
select: payload.select,
|
|
35
|
+
}, c.var.auth)) {
|
|
36
|
+
result.push(Object.fromEntries(Object.entries(item.thread).filter(([k]) => !payload.select || payload.select.includes(k))));
|
|
37
|
+
// Only set total if it's the first item
|
|
38
|
+
if (total === 0) {
|
|
39
|
+
total = item.total;
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
const nextOffset = (payload.offset ?? 0) + total;
|
|
43
|
+
if (total === payload.limit) {
|
|
44
|
+
c.res.headers.set("X-Pagination-Next", nextOffset.toString());
|
|
45
|
+
c.res.headers.set("X-Pagination-Total", (nextOffset + 1).toString());
|
|
46
|
+
}
|
|
47
|
+
else {
|
|
48
|
+
c.res.headers.set("X-Pagination-Total", nextOffset.toString());
|
|
49
|
+
}
|
|
50
|
+
return jsonExtra(c, result);
|
|
51
|
+
});
|
|
52
|
+
api.post("/threads/count", zValidator("json", schemas.ThreadCountRequest), async (c) => {
|
|
53
|
+
const payload = c.req.valid("json");
|
|
54
|
+
const total = await threads().count(payload, c.var.auth);
|
|
55
|
+
return c.json(total);
|
|
56
|
+
});
|
|
57
|
+
api.get("/threads/:thread_id/state", zValidator("param", z.object({ thread_id: z.string().uuid() })), zValidator("query", z.object({ subgraphs: schemas.coercedBoolean.optional() })), async (c) => {
|
|
58
|
+
// Get Latest Thread State
|
|
59
|
+
const { thread_id } = c.req.valid("param");
|
|
60
|
+
const { subgraphs } = c.req.valid("query");
|
|
61
|
+
const state = stateSnapshotToThreadState(await threads().state.get({ configurable: { thread_id } }, { subgraphs }, c.var.auth));
|
|
62
|
+
return jsonExtra(c, state);
|
|
63
|
+
});
|
|
64
|
+
api.post("/threads/:thread_id/state", zValidator("param", z.object({ thread_id: z.string().uuid() })), zValidator("json", schemas.ThreadStateUpdate), async (c) => {
|
|
65
|
+
// Update Thread State
|
|
66
|
+
const { thread_id } = c.req.valid("param");
|
|
67
|
+
const payload = c.req.valid("json");
|
|
68
|
+
const config = { configurable: { thread_id } };
|
|
69
|
+
if (payload.checkpoint_id) {
|
|
70
|
+
config.configurable ??= {};
|
|
71
|
+
config.configurable.checkpoint_id = payload.checkpoint_id;
|
|
72
|
+
}
|
|
73
|
+
if (payload.checkpoint) {
|
|
74
|
+
config.configurable ??= {};
|
|
75
|
+
Object.assign(config.configurable, payload.checkpoint);
|
|
76
|
+
}
|
|
77
|
+
const inserted = await threads().state.post(config, payload.values, payload.as_node, c.var.auth);
|
|
78
|
+
return jsonExtra(c, inserted);
|
|
79
|
+
});
|
|
80
|
+
api.get("/threads/:thread_id/state/:checkpoint_id", zValidator("param", z.object({
|
|
81
|
+
thread_id: z.string().uuid(),
|
|
82
|
+
checkpoint_id: z.string().uuid(),
|
|
83
|
+
})), zValidator("query", z.object({ subgraphs: schemas.coercedBoolean.optional() })), async (c) => {
|
|
84
|
+
// Get Thread State At Checkpoint
|
|
85
|
+
const { thread_id, checkpoint_id } = c.req.valid("param");
|
|
86
|
+
const { subgraphs } = c.req.valid("query");
|
|
87
|
+
const state = stateSnapshotToThreadState(await threads().state.get({ configurable: { thread_id, checkpoint_id } }, { subgraphs }, c.var.auth));
|
|
88
|
+
return jsonExtra(c, state);
|
|
89
|
+
});
|
|
90
|
+
api.post("/threads/:thread_id/state/checkpoint", zValidator("param", z.object({ thread_id: z.string().uuid() })), zValidator("json", z.object({
|
|
91
|
+
subgraphs: schemas.coercedBoolean.optional(),
|
|
92
|
+
checkpoint: schemas.CheckpointSchema.nullish(),
|
|
93
|
+
})), async (c) => {
|
|
94
|
+
// Get Thread State At Checkpoint Post
|
|
95
|
+
const { thread_id } = c.req.valid("param");
|
|
96
|
+
const { checkpoint, subgraphs } = c.req.valid("json");
|
|
97
|
+
const state = stateSnapshotToThreadState(await threads().state.get({ configurable: { thread_id, ...checkpoint } }, { subgraphs }, c.var.auth));
|
|
98
|
+
return jsonExtra(c, state);
|
|
99
|
+
});
|
|
100
|
+
api.get("/threads/:thread_id/history", zValidator("param", z.object({ thread_id: z.string().uuid() })), zValidator("query", z.object({
|
|
101
|
+
limit: z
|
|
102
|
+
.string()
|
|
103
|
+
.optional()
|
|
104
|
+
.default("10")
|
|
105
|
+
.transform((value) => parseInt(value, 10)),
|
|
106
|
+
before: z.string().optional(),
|
|
107
|
+
})), async (c) => {
|
|
108
|
+
// Get Thread History
|
|
109
|
+
const { thread_id } = c.req.valid("param");
|
|
110
|
+
const { limit, before } = c.req.valid("query");
|
|
111
|
+
const states = await threads().state.list({ configurable: { thread_id, checkpoint_ns: "" } }, { limit, before }, c.var.auth);
|
|
112
|
+
return jsonExtra(c, states.map(stateSnapshotToThreadState));
|
|
113
|
+
});
|
|
114
|
+
api.post("/threads/:thread_id/history", zValidator("param", z.object({ thread_id: z.string().uuid() })), zValidator("json", schemas.ThreadHistoryRequest), async (c) => {
|
|
115
|
+
// Get Thread History Post
|
|
116
|
+
const { thread_id } = c.req.valid("param");
|
|
117
|
+
const { limit, before, metadata, checkpoint } = c.req.valid("json");
|
|
118
|
+
const states = await threads().state.list({ configurable: { thread_id, checkpoint_ns: "", ...checkpoint } }, { limit, before, metadata }, c.var.auth);
|
|
119
|
+
return jsonExtra(c, states.map(stateSnapshotToThreadState));
|
|
120
|
+
});
|
|
121
|
+
api.get("/threads/:thread_id", zValidator("param", z.object({ thread_id: z.string().uuid() })), async (c) => {
|
|
122
|
+
// Get Thread
|
|
123
|
+
const { thread_id } = c.req.valid("param");
|
|
124
|
+
return jsonExtra(c, await threads().get(thread_id, c.var.auth));
|
|
125
|
+
});
|
|
126
|
+
api.delete("/threads/:thread_id", zValidator("param", z.object({ thread_id: z.string().uuid() })), async (c) => {
|
|
127
|
+
// Delete Thread
|
|
128
|
+
const { thread_id } = c.req.valid("param");
|
|
129
|
+
await threads().delete(thread_id, c.var.auth);
|
|
130
|
+
return new Response(null, { status: 204 });
|
|
131
|
+
});
|
|
132
|
+
api.patch("/threads/:thread_id", zValidator("param", z.object({ thread_id: z.string().uuid() })), zValidator("json", schemas.ThreadPatchRequest), async (c) => {
|
|
133
|
+
// Patch Thread
|
|
134
|
+
const { thread_id } = c.req.valid("param");
|
|
135
|
+
const { metadata } = c.req.valid("json");
|
|
136
|
+
return jsonExtra(c, await threads().patch(thread_id, { metadata }, c.var.auth));
|
|
137
|
+
});
|
|
138
|
+
api.post("/threads/:thread_id/copy", zValidator("param", z.object({ thread_id: z.string().uuid() })), async (c) => {
|
|
139
|
+
// Copy Thread
|
|
140
|
+
const { thread_id } = c.req.valid("param");
|
|
141
|
+
return jsonExtra(c, await threads().copy(thread_id, c.var.auth));
|
|
142
|
+
});
|
|
143
|
+
export default api;
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import type { CompiledGraph } from "@langchain/langgraph";
|
|
2
|
+
export declare const GRAPHS: Record<string, CompiledGraph<string>>;
|
|
3
|
+
export declare const NAMESPACE_GRAPH: Uint8Array<ArrayBufferLike>;
|
|
4
|
+
export type CompiledGraphFactory<T extends string> = (config: {
|
|
5
|
+
configurable?: Record<string, unknown>;
|
|
6
|
+
}) => Promise<CompiledGraph<T>>;
|
|
7
|
+
export declare function resolveGraph(spec: string, options: {
|
|
8
|
+
cwd: string;
|
|
9
|
+
onlyFilePresence?: false;
|
|
10
|
+
}): Promise<{
|
|
11
|
+
sourceFile: string;
|
|
12
|
+
exportSymbol: string;
|
|
13
|
+
resolved: CompiledGraph<string> | CompiledGraphFactory<string>;
|
|
14
|
+
}>;
|
|
15
|
+
export declare function resolveGraph(spec: string, options: {
|
|
16
|
+
cwd: string;
|
|
17
|
+
onlyFilePresence: true;
|
|
18
|
+
}): Promise<{
|
|
19
|
+
sourceFile: string;
|
|
20
|
+
exportSymbol: string;
|
|
21
|
+
resolved: undefined;
|
|
22
|
+
}>;
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import * as uuid from "uuid";
|
|
2
|
+
import * as path from "node:path";
|
|
3
|
+
import * as fs from "node:fs/promises";
|
|
4
|
+
import { pathToFileURL } from "node:url";
|
|
5
|
+
export const GRAPHS = {};
|
|
6
|
+
export const NAMESPACE_GRAPH = uuid.parse("6ba7b821-9dad-11d1-80b4-00c04fd430c8");
|
|
7
|
+
export async function resolveGraph(spec, options) {
|
|
8
|
+
const [userFile, exportSymbol] = spec.split(":", 2);
|
|
9
|
+
const sourceFile = path.resolve(options.cwd, userFile);
|
|
10
|
+
// validate file exists
|
|
11
|
+
await fs.stat(sourceFile);
|
|
12
|
+
if (options?.onlyFilePresence) {
|
|
13
|
+
return { sourceFile, exportSymbol, resolved: undefined };
|
|
14
|
+
}
|
|
15
|
+
const isGraph = (graph) => {
|
|
16
|
+
if (typeof graph !== "object" || graph == null)
|
|
17
|
+
return false;
|
|
18
|
+
return "compile" in graph && typeof graph.compile === "function";
|
|
19
|
+
};
|
|
20
|
+
const isCompiledGraph = (graph) => {
|
|
21
|
+
if (typeof graph !== "object" || graph == null)
|
|
22
|
+
return false;
|
|
23
|
+
return ("builder" in graph &&
|
|
24
|
+
typeof graph.builder === "object" &&
|
|
25
|
+
graph.builder != null);
|
|
26
|
+
};
|
|
27
|
+
const graph = await import(pathToFileURL(sourceFile).toString()).then((module) => module[exportSymbol || "default"]);
|
|
28
|
+
// obtain the graph, and if not compiled, compile it
|
|
29
|
+
const resolved = await (async () => {
|
|
30
|
+
if (!graph)
|
|
31
|
+
throw new Error("Failed to load graph: graph is nullush");
|
|
32
|
+
const afterResolve = (graphLike) => {
|
|
33
|
+
const graph = isGraph(graphLike) ? graphLike.compile() : graphLike;
|
|
34
|
+
// TODO: hack, remove once LangChain 1.x createAgent is fixed.
|
|
35
|
+
// `createAgent` returns a ReactAgent wrapper that itself looks
|
|
36
|
+
// like a CompiledGraph (it has a `builder` — the outer
|
|
37
|
+
// StateGraph) *and* exposes the real compiled pregel under
|
|
38
|
+
// `.graph`. Unwrap to the inner graph whenever both are
|
|
39
|
+
// present so downstream code (e.g. the v2 streaming path that
|
|
40
|
+
// keys off `graph.streamTransformers`) sees the actual pregel
|
|
41
|
+
// rather than the wrapper.
|
|
42
|
+
const inner = graph.graph;
|
|
43
|
+
if (inner != null &&
|
|
44
|
+
typeof inner === "object" &&
|
|
45
|
+
isCompiledGraph(inner)) {
|
|
46
|
+
return inner;
|
|
47
|
+
}
|
|
48
|
+
return graph;
|
|
49
|
+
};
|
|
50
|
+
if (typeof graph === "function") {
|
|
51
|
+
return async (config) => {
|
|
52
|
+
const graphLike = await graph(config);
|
|
53
|
+
return afterResolve(graphLike);
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
return afterResolve(await graph);
|
|
57
|
+
})();
|
|
58
|
+
return { sourceFile, exportSymbol, resolved };
|
|
59
|
+
}
|