@langchain/langgraph-api 0.0.10
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/LICENSE +21 -0
- package/README.md +3 -0
- package/dist/api/assistants.mjs +144 -0
- package/dist/api/runs.mjs +239 -0
- package/dist/api/store.mjs +83 -0
- package/dist/api/threads.mjs +143 -0
- package/dist/cli/entrypoint.mjs +42 -0
- package/dist/cli/spawn.d.mts +14 -0
- package/dist/cli/spawn.mjs +34 -0
- package/dist/cli/utils/ipc/client.mjs +47 -0
- package/dist/cli/utils/ipc/utils/get-pipe-path.mjs +29 -0
- package/dist/cli/utils/ipc/utils/temporary-directory.mjs +40 -0
- package/dist/graph/load.hooks.mjs +17 -0
- package/dist/graph/load.mjs +72 -0
- package/dist/graph/load.utils.mjs +50 -0
- package/dist/graph/parser/parser.mjs +309 -0
- package/dist/graph/parser/parser.worker.mjs +7 -0
- package/dist/graph/parser/schema/types.mjs +1607 -0
- package/dist/graph/parser/schema/types.template.mts +83 -0
- package/dist/logging.mjs +100 -0
- package/dist/preload.mjs +3 -0
- package/dist/queue.mjs +93 -0
- package/dist/schemas.mjs +407 -0
- package/dist/server.mjs +74 -0
- package/dist/state.mjs +32 -0
- package/dist/storage/checkpoint.mjs +127 -0
- package/dist/storage/importMap.mjs +55 -0
- package/dist/storage/ops.mjs +792 -0
- package/dist/storage/persist.mjs +78 -0
- package/dist/storage/store.mjs +41 -0
- package/dist/stream.mjs +215 -0
- package/dist/utils/abort.mjs +8 -0
- package/dist/utils/hono.mjs +27 -0
- package/dist/utils/importMap.mjs +55 -0
- package/dist/utils/runnableConfig.mjs +45 -0
- package/dist/utils/serde.mjs +20 -0
- package/package.json +56 -0
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
export declare function spawnServer(args: {
|
|
2
|
+
host: string;
|
|
3
|
+
port: string;
|
|
4
|
+
nJobsPerWorker: string;
|
|
5
|
+
}, context: {
|
|
6
|
+
config: {
|
|
7
|
+
graphs: Record<string, string>;
|
|
8
|
+
};
|
|
9
|
+
env: NodeJS.ProcessEnv;
|
|
10
|
+
hostUrl: string;
|
|
11
|
+
}, options: {
|
|
12
|
+
pid: number;
|
|
13
|
+
projectCwd: string;
|
|
14
|
+
}): Promise<import("child_process").ChildProcess>;
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { fileURLToPath } from "node:url";
|
|
2
|
+
import { spawn } from "node:child_process";
|
|
3
|
+
export async function spawnServer(args, context, options) {
|
|
4
|
+
const localUrl = `http://${args.host}:${args.port}`;
|
|
5
|
+
const studioUrl = `${context.hostUrl}/studio?baseUrl=${localUrl}`;
|
|
6
|
+
console.log(`
|
|
7
|
+
Welcome to
|
|
8
|
+
|
|
9
|
+
╦ ┌─┐┌┐┌┌─┐╔═╗┬─┐┌─┐┌─┐┬ ┬
|
|
10
|
+
║ ├─┤││││ ┬║ ╦├┬┘├─┤├─┘├─┤
|
|
11
|
+
╩═╝┴ ┴┘└┘└─┘╚═╝┴└─┴ ┴┴ ┴ ┴.js
|
|
12
|
+
|
|
13
|
+
- 🚀 API: \x1b[36m${localUrl}\x1b[0m
|
|
14
|
+
- 🎨 Studio UI: \x1b[36m${studioUrl}\x1b[0m
|
|
15
|
+
|
|
16
|
+
This in-memory server is designed for development and testing.
|
|
17
|
+
For production use, please use LangGraph Cloud.
|
|
18
|
+
|
|
19
|
+
`);
|
|
20
|
+
return spawn(process.execPath, [
|
|
21
|
+
fileURLToPath(new URL("../../cli.mjs", import.meta.resolve("tsx/esm/api"))),
|
|
22
|
+
"watch",
|
|
23
|
+
"--clear-screen=false",
|
|
24
|
+
fileURLToPath(new URL(import.meta.resolve("./entrypoint.mjs"))),
|
|
25
|
+
options.pid.toString(),
|
|
26
|
+
JSON.stringify({
|
|
27
|
+
port: Number.parseInt(args.port, 10),
|
|
28
|
+
nWorkers: Number.parseInt(args.nJobsPerWorker, 10),
|
|
29
|
+
host: args.host,
|
|
30
|
+
graphs: context.config.graphs,
|
|
31
|
+
cwd: options.projectCwd,
|
|
32
|
+
}),
|
|
33
|
+
], { stdio: ["inherit", "inherit", "inherit", "ipc"], env: context.env });
|
|
34
|
+
}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
// MIT License
|
|
2
|
+
//
|
|
3
|
+
// Copyright (c) Hiroki Osame <hiroki.osame@gmail.com>
|
|
4
|
+
//
|
|
5
|
+
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
// of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
// in the Software without restriction, including without limitation the rights
|
|
8
|
+
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
// copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
// furnished to do so, subject to the following conditions:
|
|
11
|
+
//
|
|
12
|
+
// The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
// copies or substantial portions of the Software.
|
|
14
|
+
//
|
|
15
|
+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
// SOFTWARE.
|
|
22
|
+
//
|
|
23
|
+
// https://github.com/privatenumber/tsx/tree/28a3e7d2b8fd72b683aab8a98dd1fcee4624e4cb
|
|
24
|
+
import net from "node:net";
|
|
25
|
+
import { getPipePath } from "./utils/get-pipe-path.mjs";
|
|
26
|
+
export const connectToServer = (processId = process.ppid) => new Promise((resolve) => {
|
|
27
|
+
const pipePath = getPipePath(processId);
|
|
28
|
+
const socket = net.createConnection(pipePath, () => {
|
|
29
|
+
const sendToParent = (data) => {
|
|
30
|
+
const messageBuffer = Buffer.from(JSON.stringify(data));
|
|
31
|
+
const lengthBuffer = Buffer.alloc(4);
|
|
32
|
+
lengthBuffer.writeInt32BE(messageBuffer.length, 0);
|
|
33
|
+
socket.write(Buffer.concat([lengthBuffer, messageBuffer]));
|
|
34
|
+
};
|
|
35
|
+
resolve(sendToParent);
|
|
36
|
+
});
|
|
37
|
+
/**
|
|
38
|
+
* Ignore error when:
|
|
39
|
+
* - Called as a loader and there is no server
|
|
40
|
+
* - Nested process when using --test and the ppid is incorrect
|
|
41
|
+
*/
|
|
42
|
+
socket.on("error", () => {
|
|
43
|
+
resolve(undefined);
|
|
44
|
+
});
|
|
45
|
+
// Prevent Node from waiting for this socket to close before exiting
|
|
46
|
+
socket.unref();
|
|
47
|
+
});
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
// MIT License
|
|
2
|
+
//
|
|
3
|
+
// Copyright (c) Hiroki Osame <hiroki.osame@gmail.com>
|
|
4
|
+
//
|
|
5
|
+
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
// of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
// in the Software without restriction, including without limitation the rights
|
|
8
|
+
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
// copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
// furnished to do so, subject to the following conditions:
|
|
11
|
+
//
|
|
12
|
+
// The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
// copies or substantial portions of the Software.
|
|
14
|
+
//
|
|
15
|
+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
// SOFTWARE.
|
|
22
|
+
//
|
|
23
|
+
// https://github.com/privatenumber/tsx/tree/28a3e7d2b8fd72b683aab8a98dd1fcee4624e4cb
|
|
24
|
+
import path from "node:path";
|
|
25
|
+
import { tmpdir } from "./temporary-directory.mjs";
|
|
26
|
+
export const getPipePath = (processId) => {
|
|
27
|
+
const pipePath = path.join(tmpdir, `${processId}.pipe`);
|
|
28
|
+
return process.platform === "win32" ? `\\\\?\\pipe\\${pipePath}` : pipePath;
|
|
29
|
+
};
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
// MIT License
|
|
2
|
+
//
|
|
3
|
+
// Copyright (c) Hiroki Osame <hiroki.osame@gmail.com>
|
|
4
|
+
//
|
|
5
|
+
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
// of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
// in the Software without restriction, including without limitation the rights
|
|
8
|
+
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
// copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
// furnished to do so, subject to the following conditions:
|
|
11
|
+
//
|
|
12
|
+
// The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
// copies or substantial portions of the Software.
|
|
14
|
+
//
|
|
15
|
+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
// SOFTWARE.
|
|
22
|
+
//
|
|
23
|
+
// https://github.com/privatenumber/tsx/tree/28a3e7d2b8fd72b683aab8a98dd1fcee4624e4cb
|
|
24
|
+
import path from "node:path";
|
|
25
|
+
import os from "node:os";
|
|
26
|
+
/**
|
|
27
|
+
* Cache directory is based on the user's identifier
|
|
28
|
+
* to avoid permission issues when accessed by a different user
|
|
29
|
+
*/
|
|
30
|
+
const { geteuid } = process;
|
|
31
|
+
const userId = geteuid
|
|
32
|
+
? // For Linux users with virtual users on CI (e.g. Docker)
|
|
33
|
+
geteuid()
|
|
34
|
+
: // Use username on Windows because it doesn't have id
|
|
35
|
+
os.userInfo().username;
|
|
36
|
+
/**
|
|
37
|
+
* This ensures that the cache directory is unique per user
|
|
38
|
+
* and has the appropriate permissions
|
|
39
|
+
*/
|
|
40
|
+
export const tmpdir = path.join(os.tmpdir(), `tsx-${userId}`);
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
// This hook is to ensure that @langchain/langgraph package
|
|
2
|
+
// found in /api folder has precendence compared to user-provided package
|
|
3
|
+
// found in /deps. Does not attempt to semver check for too old packages.
|
|
4
|
+
const OVERRIDE_RESOLVE = [
|
|
5
|
+
"@langchain/langgraph",
|
|
6
|
+
"@langchain/langgraph-checkpoint",
|
|
7
|
+
];
|
|
8
|
+
export async function resolve(specifier, context, nextResolve) {
|
|
9
|
+
if (OVERRIDE_RESOLVE.includes(specifier)) {
|
|
10
|
+
const parentURL = new URL("./load.mts", import.meta.url).toString();
|
|
11
|
+
return await nextResolve(specifier, {
|
|
12
|
+
...context,
|
|
13
|
+
parentURL,
|
|
14
|
+
});
|
|
15
|
+
}
|
|
16
|
+
return nextResolve(specifier, context);
|
|
17
|
+
}
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
import * as uuid from "uuid";
|
|
3
|
+
import { Assistants } from "../storage/ops.mjs";
|
|
4
|
+
import { HTTPException } from "hono/http-exception";
|
|
5
|
+
import { resolveGraph, runGraphSchemaWorker, } from "./load.utils.mjs";
|
|
6
|
+
import { checkpointer } from "../storage/checkpoint.mjs";
|
|
7
|
+
import { store } from "../storage/store.mjs";
|
|
8
|
+
import { logger } from "../logging.mjs";
|
|
9
|
+
export const GRAPHS = {};
|
|
10
|
+
export const GRAPH_SPEC = {};
|
|
11
|
+
export const GRAPH_SCHEMA = {};
|
|
12
|
+
export const NAMESPACE_GRAPH = uuid.parse("6ba7b821-9dad-11d1-80b4-00c04fd430c8");
|
|
13
|
+
const ConfigSchema = z.record(z.unknown());
|
|
14
|
+
export const getAssistantId = (graphId) => {
|
|
15
|
+
if (graphId in GRAPHS)
|
|
16
|
+
return uuid.v5(graphId, NAMESPACE_GRAPH);
|
|
17
|
+
return graphId;
|
|
18
|
+
};
|
|
19
|
+
export async function registerFromEnv(specs, options) {
|
|
20
|
+
const envConfig = process.env.LANGGRAPH_CONFIG
|
|
21
|
+
? ConfigSchema.parse(JSON.parse(process.env.LANGGRAPH_CONFIG))
|
|
22
|
+
: undefined;
|
|
23
|
+
return await Promise.all(Object.entries(specs).map(async ([graphId, rawSpec]) => {
|
|
24
|
+
logger.info(`Registering graph with id '${graphId}'`, {
|
|
25
|
+
graph_id: graphId,
|
|
26
|
+
});
|
|
27
|
+
const config = envConfig?.[graphId];
|
|
28
|
+
const { resolved, ...spec } = await resolveGraph(rawSpec, {
|
|
29
|
+
cwd: options.cwd,
|
|
30
|
+
});
|
|
31
|
+
// registering the graph runtime
|
|
32
|
+
GRAPHS[graphId] = resolved;
|
|
33
|
+
GRAPH_SPEC[graphId] = spec;
|
|
34
|
+
await Assistants.put(uuid.v5(graphId, NAMESPACE_GRAPH), {
|
|
35
|
+
graph_id: graphId,
|
|
36
|
+
metadata: { created_by: "system" },
|
|
37
|
+
config: config ?? {},
|
|
38
|
+
if_exists: "do_nothing",
|
|
39
|
+
name: graphId,
|
|
40
|
+
});
|
|
41
|
+
return resolved;
|
|
42
|
+
}));
|
|
43
|
+
}
|
|
44
|
+
export function getGraph(graphId, options) {
|
|
45
|
+
if (!GRAPHS[graphId])
|
|
46
|
+
throw new HTTPException(404, { message: `Graph "${graphId}" not found` });
|
|
47
|
+
// TODO: have a check for the type of graph
|
|
48
|
+
const compiled = GRAPHS[graphId];
|
|
49
|
+
if (typeof options?.checkpointer !== "undefined") {
|
|
50
|
+
compiled.checkpointer = options?.checkpointer ?? undefined;
|
|
51
|
+
}
|
|
52
|
+
else {
|
|
53
|
+
compiled.checkpointer = checkpointer;
|
|
54
|
+
}
|
|
55
|
+
compiled.store = options?.store ?? store;
|
|
56
|
+
return compiled;
|
|
57
|
+
}
|
|
58
|
+
export async function getGraphSchema(graphId) {
|
|
59
|
+
if (!GRAPH_SPEC[graphId])
|
|
60
|
+
throw new HTTPException(404, {
|
|
61
|
+
message: `Spec for "${graphId}" not found`,
|
|
62
|
+
});
|
|
63
|
+
if (!GRAPH_SCHEMA[graphId] || true) {
|
|
64
|
+
try {
|
|
65
|
+
GRAPH_SCHEMA[graphId] = await runGraphSchemaWorker(GRAPH_SPEC[graphId]);
|
|
66
|
+
}
|
|
67
|
+
catch (error) {
|
|
68
|
+
throw new Error(`Failed to extract schema for "${graphId}": ${error}`);
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
return GRAPH_SCHEMA[graphId];
|
|
72
|
+
}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { Worker } from "node:worker_threads";
|
|
2
|
+
import * as fs from "node:fs/promises";
|
|
3
|
+
import * as path from "node:path";
|
|
4
|
+
import { fileURLToPath, pathToFileURL } from "node:url";
|
|
5
|
+
import * as uuid from "uuid";
|
|
6
|
+
export const GRAPHS = {};
|
|
7
|
+
export const NAMESPACE_GRAPH = uuid.parse("6ba7b821-9dad-11d1-80b4-00c04fd430c8");
|
|
8
|
+
export async function resolveGraph(spec, options) {
|
|
9
|
+
const [userFile, exportSymbol] = spec.split(":", 2);
|
|
10
|
+
const sourceFile = path.resolve(options.cwd, userFile);
|
|
11
|
+
// validate file exists
|
|
12
|
+
await fs.stat(sourceFile);
|
|
13
|
+
if (options?.onlyFilePresence) {
|
|
14
|
+
return { sourceFile: userFile, exportSymbol, resolved: undefined };
|
|
15
|
+
}
|
|
16
|
+
const isGraph = (graph) => {
|
|
17
|
+
if (typeof graph !== "object" || graph == null)
|
|
18
|
+
return false;
|
|
19
|
+
return "compile" in graph && typeof graph.compile === "function";
|
|
20
|
+
};
|
|
21
|
+
const graph = await import(pathToFileURL(sourceFile).toString()).then((module) => module[exportSymbol || "default"]);
|
|
22
|
+
// obtain the graph, and if not compiled, compile it
|
|
23
|
+
const resolved = await (async () => {
|
|
24
|
+
if (!graph)
|
|
25
|
+
throw new Error("Failed to load graph: graph is nullush");
|
|
26
|
+
const graphLike = typeof graph === "function" ? await graph() : await graph;
|
|
27
|
+
if (isGraph(graphLike))
|
|
28
|
+
return graphLike.compile();
|
|
29
|
+
return graphLike;
|
|
30
|
+
})();
|
|
31
|
+
return { sourceFile, exportSymbol, resolved };
|
|
32
|
+
}
|
|
33
|
+
export async function runGraphSchemaWorker(spec) {
|
|
34
|
+
const SCHEMA_RESOLVE_TIMEOUT_MS = 30_000;
|
|
35
|
+
return await new Promise((resolve, reject) => {
|
|
36
|
+
const worker = new Worker(fileURLToPath(new URL("./parser/parser.worker.mjs", import.meta.url)));
|
|
37
|
+
// Set a timeout to reject if the worker takes too long
|
|
38
|
+
const timeoutId = setTimeout(() => {
|
|
39
|
+
worker.terminate();
|
|
40
|
+
reject(new Error("Schema extract worker timed out"));
|
|
41
|
+
}, SCHEMA_RESOLVE_TIMEOUT_MS);
|
|
42
|
+
worker.on("message", (result) => {
|
|
43
|
+
worker.terminate();
|
|
44
|
+
clearTimeout(timeoutId);
|
|
45
|
+
resolve(result);
|
|
46
|
+
});
|
|
47
|
+
worker.on("error", reject);
|
|
48
|
+
worker.postMessage(spec);
|
|
49
|
+
});
|
|
50
|
+
}
|
|
@@ -0,0 +1,309 @@
|
|
|
1
|
+
import * as ts from "typescript";
|
|
2
|
+
import * as vfs from "@typescript/vfs";
|
|
3
|
+
import * as path from "node:path";
|
|
4
|
+
import dedent from "dedent";
|
|
5
|
+
import { buildGenerator } from "./schema/types.mjs";
|
|
6
|
+
import { fileURLToPath } from "node:url";
|
|
7
|
+
const __dirname = fileURLToPath(new URL(".", import.meta.url));
|
|
8
|
+
const compilerOptions = {
|
|
9
|
+
noEmit: true,
|
|
10
|
+
strict: true,
|
|
11
|
+
allowUnusedLabels: true,
|
|
12
|
+
};
|
|
13
|
+
export class SubgraphExtractor {
|
|
14
|
+
program;
|
|
15
|
+
checker;
|
|
16
|
+
sourceFile;
|
|
17
|
+
inferFile;
|
|
18
|
+
anyPregelType;
|
|
19
|
+
anyGraphType;
|
|
20
|
+
strict;
|
|
21
|
+
constructor(program, sourceFile, inferFile, options) {
|
|
22
|
+
this.program = program;
|
|
23
|
+
this.sourceFile = sourceFile;
|
|
24
|
+
this.inferFile = inferFile;
|
|
25
|
+
this.checker = program.getTypeChecker();
|
|
26
|
+
this.strict = options?.strict ?? false;
|
|
27
|
+
this.anyPregelType = this.findTypeByName("AnyPregel");
|
|
28
|
+
this.anyGraphType = this.findTypeByName("AnyGraph");
|
|
29
|
+
}
|
|
30
|
+
findTypeByName = (needle) => {
|
|
31
|
+
let result = undefined;
|
|
32
|
+
const visit = (node) => {
|
|
33
|
+
if (ts.isTypeAliasDeclaration(node)) {
|
|
34
|
+
const symbol = node.symbol;
|
|
35
|
+
if (symbol != null) {
|
|
36
|
+
const name = this.checker
|
|
37
|
+
.getFullyQualifiedName(symbol)
|
|
38
|
+
.replace(/".*"\./, "");
|
|
39
|
+
if (name === needle)
|
|
40
|
+
result = this.checker.getTypeAtLocation(node);
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
if (result == null)
|
|
44
|
+
ts.forEachChild(node, visit);
|
|
45
|
+
};
|
|
46
|
+
ts.forEachChild(this.inferFile, visit);
|
|
47
|
+
if (!result)
|
|
48
|
+
throw new Error(`Failed to find "${needle}" type`);
|
|
49
|
+
return result;
|
|
50
|
+
};
|
|
51
|
+
find = (root, predicate) => {
|
|
52
|
+
let result = undefined;
|
|
53
|
+
const visit = (node) => {
|
|
54
|
+
if (predicate(node)) {
|
|
55
|
+
result = node;
|
|
56
|
+
}
|
|
57
|
+
else {
|
|
58
|
+
ts.forEachChild(node, visit);
|
|
59
|
+
}
|
|
60
|
+
};
|
|
61
|
+
if (predicate(root))
|
|
62
|
+
return root;
|
|
63
|
+
ts.forEachChild(root, visit);
|
|
64
|
+
return result;
|
|
65
|
+
};
|
|
66
|
+
findSubgraphs = (node, namespace = []) => {
|
|
67
|
+
const findAllAddNodeCalls = (acc, node) => {
|
|
68
|
+
if (ts.isCallExpression(node)) {
|
|
69
|
+
const firstChild = node.getChildAt(0);
|
|
70
|
+
if (ts.isPropertyAccessExpression(firstChild) &&
|
|
71
|
+
this.getText(firstChild.name) === "addNode") {
|
|
72
|
+
let nodeName = "unknown";
|
|
73
|
+
let variables = [];
|
|
74
|
+
const [subgraphNode, callArg] = node.arguments;
|
|
75
|
+
if (subgraphNode && ts.isStringLiteralLike(subgraphNode)) {
|
|
76
|
+
nodeName = this.getText(subgraphNode);
|
|
77
|
+
if ((nodeName.startsWith(`"`) && nodeName.endsWith(`"`)) ||
|
|
78
|
+
(nodeName.startsWith(`'`) && nodeName.endsWith(`'`))) {
|
|
79
|
+
nodeName = nodeName.slice(1, -1);
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
if (callArg) {
|
|
83
|
+
if (ts.isFunctionLike(callArg) ||
|
|
84
|
+
ts.isCallLikeExpression(callArg)) {
|
|
85
|
+
variables = this.reduceChildren(callArg, this.findSubgraphIdentifiers, []);
|
|
86
|
+
}
|
|
87
|
+
else if (ts.isIdentifier(callArg)) {
|
|
88
|
+
variables = this.findSubgraphIdentifiers([], callArg);
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
if (variables.length > 0) {
|
|
92
|
+
if (variables.length > 1) {
|
|
93
|
+
const targetName = [...namespace, nodeName].join("|");
|
|
94
|
+
const errMsg = `Multiple unique subgraph invocations found for "${targetName}"`;
|
|
95
|
+
if (this.strict)
|
|
96
|
+
throw new Error(errMsg);
|
|
97
|
+
console.warn(errMsg);
|
|
98
|
+
}
|
|
99
|
+
acc.push({
|
|
100
|
+
namespace: namespace,
|
|
101
|
+
node: nodeName,
|
|
102
|
+
subgraph: variables[0],
|
|
103
|
+
});
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
return acc;
|
|
108
|
+
};
|
|
109
|
+
let subgraphs = this.reduceChildren(node, findAllAddNodeCalls, []);
|
|
110
|
+
// TODO: make this more strict, only traverse the flow graph only
|
|
111
|
+
// if no `addNode` calls were found
|
|
112
|
+
if (!subgraphs.length) {
|
|
113
|
+
const candidate = this.find(node, (node) => node && "flowNode" in node && node.flowNode);
|
|
114
|
+
if (candidate?.flowNode &&
|
|
115
|
+
this.isGraphOrPregelType(this.checker.getTypeAtLocation(candidate.flowNode.node))) {
|
|
116
|
+
subgraphs = this.findSubgraphs(candidate.flowNode.node, namespace);
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
// handle recursive behaviour
|
|
120
|
+
if (subgraphs.length > 0) {
|
|
121
|
+
return [
|
|
122
|
+
...subgraphs,
|
|
123
|
+
...subgraphs.map(({ subgraph, node }) => this.findSubgraphs(subgraph.node, [...namespace, node])),
|
|
124
|
+
].flat();
|
|
125
|
+
}
|
|
126
|
+
return subgraphs;
|
|
127
|
+
};
|
|
128
|
+
getSubgraphsVariables = (name) => {
|
|
129
|
+
const sourceSymbol = this.checker.getSymbolAtLocation(this.sourceFile);
|
|
130
|
+
const exports = this.checker.getExportsOfModule(sourceSymbol);
|
|
131
|
+
const targetExport = exports.find((item) => item.name === name);
|
|
132
|
+
if (!targetExport)
|
|
133
|
+
throw new Error(`Failed to find export "${name}"`);
|
|
134
|
+
const varDecls = (targetExport.declarations ?? []).filter(ts.isVariableDeclaration);
|
|
135
|
+
return varDecls.flatMap((varDecl) => {
|
|
136
|
+
if (!varDecl.initializer)
|
|
137
|
+
return [];
|
|
138
|
+
return this.findSubgraphs(varDecl.initializer, [name]);
|
|
139
|
+
});
|
|
140
|
+
};
|
|
141
|
+
getAugmentedSourceFile = (name) => {
|
|
142
|
+
const vars = this.getSubgraphsVariables(name);
|
|
143
|
+
const typeExports = [
|
|
144
|
+
{ typeName: `__langgraph__${name}`, valueName: name, graphName: name },
|
|
145
|
+
];
|
|
146
|
+
for (const { subgraph, node, namespace } of vars) {
|
|
147
|
+
typeExports.push({
|
|
148
|
+
typeName: `__langgraph__${namespace.join("_")}_${node}`,
|
|
149
|
+
valueName: subgraph.name,
|
|
150
|
+
graphName: [...namespace, node].join("|"),
|
|
151
|
+
});
|
|
152
|
+
}
|
|
153
|
+
const sourceFilePath = "__langgraph__source.mts";
|
|
154
|
+
const sourceContents = [
|
|
155
|
+
this.getText(this.sourceFile),
|
|
156
|
+
...typeExports.map(({ typeName, valueName }) => `export type ${typeName} = typeof ${valueName}`),
|
|
157
|
+
].join("\n\n");
|
|
158
|
+
const inferFilePath = "__langraph__infer.mts";
|
|
159
|
+
const inferContents = [
|
|
160
|
+
...typeExports.map(({ typeName }) => `import type { ${typeName}} from "./__langgraph__source.mts"`),
|
|
161
|
+
this.inferFile.getText(this.inferFile),
|
|
162
|
+
...typeExports.flatMap(({ typeName }) => {
|
|
163
|
+
return [
|
|
164
|
+
dedent `
|
|
165
|
+
type ${typeName}__reflect = Reflect<${typeName}>;
|
|
166
|
+
export type ${typeName}__state = Inspect<${typeName}__reflect["state"]>;
|
|
167
|
+
export type ${typeName}__update = Inspect<${typeName}__reflect["update"]>;
|
|
168
|
+
|
|
169
|
+
type ${typeName}__builder = BuilderReflect<${typeName}>;
|
|
170
|
+
export type ${typeName}__input = Inspect<FilterAny<${typeName}__builder["input"]>>;
|
|
171
|
+
export type ${typeName}__output = Inspect<FilterAny<${typeName}__builder["output"]>>;
|
|
172
|
+
export type ${typeName}__config = Inspect<FilterAny<${typeName}__builder["config"]>>;
|
|
173
|
+
`,
|
|
174
|
+
];
|
|
175
|
+
}),
|
|
176
|
+
].join("\n\n");
|
|
177
|
+
return {
|
|
178
|
+
files: [
|
|
179
|
+
[sourceFilePath, sourceContents],
|
|
180
|
+
[inferFilePath, inferContents],
|
|
181
|
+
],
|
|
182
|
+
exports: typeExports,
|
|
183
|
+
};
|
|
184
|
+
};
|
|
185
|
+
findSubgraphIdentifiers = (acc, node) => {
|
|
186
|
+
if (ts.isIdentifier(node)) {
|
|
187
|
+
const smb = this.checker.getSymbolAtLocation(node);
|
|
188
|
+
if (smb?.valueDeclaration &&
|
|
189
|
+
ts.isVariableDeclaration(smb.valueDeclaration)) {
|
|
190
|
+
const target = smb.valueDeclaration;
|
|
191
|
+
const targetType = this.checker.getTypeAtLocation(target);
|
|
192
|
+
if (this.isGraphOrPregelType(targetType)) {
|
|
193
|
+
acc.push({ name: this.getText(target.name), node: target });
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
if (smb?.declarations) {
|
|
197
|
+
const target = smb.declarations.find(ts.isImportSpecifier);
|
|
198
|
+
if (target) {
|
|
199
|
+
const targetType = this.checker.getTypeAtLocation(target);
|
|
200
|
+
if (this.isGraphOrPregelType(targetType)) {
|
|
201
|
+
acc.push({ name: this.getText(target.name), node: target });
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
return acc;
|
|
207
|
+
};
|
|
208
|
+
isGraphOrPregelType = (type) => {
|
|
209
|
+
return (this.checker.isTypeAssignableTo(type, this.anyPregelType) ||
|
|
210
|
+
this.checker.isTypeAssignableTo(type, this.anyGraphType));
|
|
211
|
+
};
|
|
212
|
+
getText(node) {
|
|
213
|
+
return node.getText(this.sourceFile);
|
|
214
|
+
}
|
|
215
|
+
reduceChildren(node, fn, initial) {
|
|
216
|
+
let acc = initial;
|
|
217
|
+
function it(node) {
|
|
218
|
+
acc = fn(acc, node);
|
|
219
|
+
// @ts-expect-error
|
|
220
|
+
ts.forEachChild(node, it.bind(this));
|
|
221
|
+
}
|
|
222
|
+
ts.forEachChild(node, it.bind(this));
|
|
223
|
+
return acc;
|
|
224
|
+
}
|
|
225
|
+
static extractSchemas(target, name, options) {
|
|
226
|
+
const dirname = typeof target === "string" ? path.dirname(target) : __dirname;
|
|
227
|
+
// This API is not well made for Windows, ensure that the paths are UNIX slashes
|
|
228
|
+
const fsMap = new Map();
|
|
229
|
+
const system = vfs.createFSBackedSystem(fsMap, dirname, ts);
|
|
230
|
+
const vfsPath = (inputPath) => {
|
|
231
|
+
if (process.platform === "win32")
|
|
232
|
+
return inputPath.replace(/\\/g, "/");
|
|
233
|
+
return inputPath;
|
|
234
|
+
};
|
|
235
|
+
const host = vfs.createVirtualCompilerHost(system, compilerOptions, ts);
|
|
236
|
+
const targetPath = typeof target === "string"
|
|
237
|
+
? target
|
|
238
|
+
: path.resolve(dirname, "./__langgraph__target.mts");
|
|
239
|
+
const inferTemplatePath = path.resolve(__dirname, "./schema/types.template.mts");
|
|
240
|
+
if (typeof target !== "string") {
|
|
241
|
+
fsMap.set(vfsPath(targetPath), target.contents);
|
|
242
|
+
for (const [name, contents] of target.files ?? []) {
|
|
243
|
+
fsMap.set(vfsPath(path.resolve(dirname, name)), contents);
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
// TODO: this is in all instances broken, will need to address in a way
|
|
247
|
+
// that allows loading fs-backed modules from different vfs
|
|
248
|
+
// host.compilerHost.resolveModuleNames = (moduleNames, containingFile) => {
|
|
249
|
+
// const resolvedModules: (ts.ResolvedModule | undefined)[] = [];
|
|
250
|
+
// for (const moduleName of moduleNames) {
|
|
251
|
+
// let target = containingFile;
|
|
252
|
+
// const relative = path.relative(dirname, containingFile);
|
|
253
|
+
// logger.debug(`${moduleName} ${containingFile}`);
|
|
254
|
+
// if (
|
|
255
|
+
// moduleName.startsWith("@langchain/langgraph") &&
|
|
256
|
+
// relative &&
|
|
257
|
+
// !relative.startsWith("..") &&
|
|
258
|
+
// !path.isAbsolute(relative)
|
|
259
|
+
// ) {
|
|
260
|
+
// target = path.resolve(__dirname, "../", relative);
|
|
261
|
+
// process.stderr.write(`${moduleName} ${relative} -> ${target}\n`);
|
|
262
|
+
// }
|
|
263
|
+
// resolvedModules.push(
|
|
264
|
+
// ts.resolveModuleName(
|
|
265
|
+
// moduleName,
|
|
266
|
+
// target,
|
|
267
|
+
// compilerOptions,
|
|
268
|
+
// host.compilerHost
|
|
269
|
+
// ).resolvedModule
|
|
270
|
+
// );
|
|
271
|
+
// }
|
|
272
|
+
// return resolvedModules;
|
|
273
|
+
// };
|
|
274
|
+
const research = ts.createProgram({
|
|
275
|
+
rootNames: [inferTemplatePath, targetPath],
|
|
276
|
+
options: compilerOptions,
|
|
277
|
+
host: host.compilerHost,
|
|
278
|
+
});
|
|
279
|
+
const extractor = new SubgraphExtractor(research, research.getSourceFile(targetPath), research.getSourceFile(inferTemplatePath), options);
|
|
280
|
+
const { files, exports } = extractor.getAugmentedSourceFile(name);
|
|
281
|
+
for (const [name, source] of files) {
|
|
282
|
+
system.writeFile(vfsPath(path.resolve(dirname, name)), source);
|
|
283
|
+
}
|
|
284
|
+
const extract = ts.createProgram({
|
|
285
|
+
rootNames: [path.resolve(dirname, "./__langraph__infer.mts")],
|
|
286
|
+
options: compilerOptions,
|
|
287
|
+
host: host.compilerHost,
|
|
288
|
+
});
|
|
289
|
+
const schemaGenerator = buildGenerator(extract);
|
|
290
|
+
const trySymbol = (schema, symbol) => {
|
|
291
|
+
try {
|
|
292
|
+
return schema?.getSchemaForSymbol(symbol) ?? undefined;
|
|
293
|
+
}
|
|
294
|
+
catch (e) {
|
|
295
|
+
console.warn(`Failed to obtain symbol "${symbol}":`, e?.message);
|
|
296
|
+
}
|
|
297
|
+
return undefined;
|
|
298
|
+
};
|
|
299
|
+
return Object.fromEntries(exports.map(({ typeName, graphName }) => [
|
|
300
|
+
graphName,
|
|
301
|
+
{
|
|
302
|
+
input: trySymbol(schemaGenerator, `${typeName}__input`),
|
|
303
|
+
output: trySymbol(schemaGenerator, `${typeName}__output`),
|
|
304
|
+
state: trySymbol(schemaGenerator, `${typeName}__update`),
|
|
305
|
+
config: trySymbol(schemaGenerator, `${typeName}__config`),
|
|
306
|
+
},
|
|
307
|
+
]));
|
|
308
|
+
}
|
|
309
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { tsImport } from "tsx/esm/api";
|
|
2
|
+
import { parentPort } from "node:worker_threads";
|
|
3
|
+
parentPort?.on("message", async (payload) => {
|
|
4
|
+
const { SubgraphExtractor } = await tsImport("./parser.mjs", import.meta.url);
|
|
5
|
+
const result = SubgraphExtractor.extractSchemas(payload.sourceFile, payload.exportSymbol, { strict: false });
|
|
6
|
+
parentPort?.postMessage(result);
|
|
7
|
+
});
|