@langchain/langgraph-api 1.1.8 → 1.1.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.
Files changed (100) hide show
  1. package/README.md +3 -3
  2. package/dist/api/assistants.d.mts +3 -0
  3. package/dist/api/assistants.mjs +193 -0
  4. package/dist/api/meta.d.mts +3 -0
  5. package/dist/api/meta.mjs +65 -0
  6. package/dist/api/runs.d.mts +3 -0
  7. package/dist/api/runs.mjs +324 -0
  8. package/dist/api/store.d.mts +3 -0
  9. package/dist/api/store.mjs +111 -0
  10. package/dist/api/threads.d.mts +3 -0
  11. package/dist/api/threads.mjs +143 -0
  12. package/dist/auth/custom.d.mts +9 -0
  13. package/dist/auth/custom.mjs +32 -0
  14. package/dist/auth/index.d.mts +43 -0
  15. package/dist/auth/index.mjs +163 -0
  16. package/dist/cli/entrypoint.d.mts +1 -0
  17. package/dist/cli/entrypoint.mjs +41 -0
  18. package/dist/cli/spawn.d.mts +42 -0
  19. package/dist/cli/spawn.mjs +47 -0
  20. package/dist/cli/utils/ipc/client.d.mts +5 -0
  21. package/dist/cli/utils/ipc/client.mjs +47 -0
  22. package/dist/cli/utils/ipc/utils/get-pipe-path.d.mts +1 -0
  23. package/dist/cli/utils/ipc/utils/get-pipe-path.mjs +29 -0
  24. package/dist/cli/utils/ipc/utils/temporary-directory.d.mts +5 -0
  25. package/dist/cli/utils/ipc/utils/temporary-directory.mjs +40 -0
  26. package/dist/command.d.mts +11 -0
  27. package/dist/command.mjs +15 -0
  28. package/dist/experimental/embed.d.mts +42 -0
  29. package/dist/experimental/embed.mjs +299 -0
  30. package/dist/graph/api.d.mts +1 -0
  31. package/dist/graph/api.mjs +2 -0
  32. package/dist/graph/load.d.mts +19 -0
  33. package/dist/graph/load.hooks.d.mts +2 -0
  34. package/dist/graph/load.hooks.mjs +52 -0
  35. package/dist/graph/load.mjs +96 -0
  36. package/dist/graph/load.utils.d.mts +22 -0
  37. package/dist/graph/load.utils.mjs +49 -0
  38. package/dist/graph/parser/index.d.mts +23 -0
  39. package/dist/graph/parser/index.mjs +58 -0
  40. package/dist/graph/parser/parser.d.mts +77 -0
  41. package/dist/graph/parser/parser.mjs +429 -0
  42. package/dist/graph/parser/parser.worker.d.mts +1 -0
  43. package/dist/graph/parser/parser.worker.mjs +7 -0
  44. package/dist/graph/parser/schema/types.d.mts +154 -0
  45. package/dist/graph/parser/schema/types.mjs +1496 -0
  46. package/dist/graph/parser/schema/types.template.d.mts +1 -0
  47. package/dist/graph/parser/schema/types.template.mts +92 -0
  48. package/dist/http/custom.d.mts +6 -0
  49. package/dist/http/custom.mjs +10 -0
  50. package/dist/http/middleware.d.mts +11 -0
  51. package/dist/http/middleware.mjs +57 -0
  52. package/dist/logging.d.mts +10 -0
  53. package/dist/logging.mjs +115 -0
  54. package/dist/loopback.d.mts +4 -0
  55. package/dist/loopback.mjs +10 -0
  56. package/dist/preload.d.mts +1 -0
  57. package/dist/preload.mjs +29 -0
  58. package/dist/queue.d.mts +2 -0
  59. package/dist/queue.mjs +119 -0
  60. package/dist/schemas.d.mts +1552 -0
  61. package/dist/schemas.mjs +492 -0
  62. package/dist/semver/index.d.mts +15 -0
  63. package/dist/semver/index.mjs +46 -0
  64. package/dist/server.d.mts +175 -0
  65. package/dist/server.mjs +181 -0
  66. package/dist/state.d.mts +3 -0
  67. package/dist/state.mjs +30 -0
  68. package/dist/storage/checkpoint.d.mts +19 -0
  69. package/dist/storage/checkpoint.mjs +127 -0
  70. package/dist/storage/context.d.mts +3 -0
  71. package/dist/storage/context.mjs +11 -0
  72. package/dist/storage/importMap.d.mts +55 -0
  73. package/dist/storage/importMap.mjs +55 -0
  74. package/dist/storage/ops.d.mts +169 -0
  75. package/dist/storage/ops.mjs +1262 -0
  76. package/dist/storage/persist.d.mts +18 -0
  77. package/dist/storage/persist.mjs +81 -0
  78. package/dist/storage/store.d.mts +17 -0
  79. package/dist/storage/store.mjs +41 -0
  80. package/dist/storage/types.d.mts +301 -0
  81. package/dist/storage/types.mjs +1 -0
  82. package/dist/stream.d.mts +43 -0
  83. package/dist/stream.mjs +235 -0
  84. package/dist/ui/load.d.mts +8 -0
  85. package/dist/ui/load.mjs +53 -0
  86. package/dist/utils/abort.d.mts +1 -0
  87. package/dist/utils/abort.mjs +8 -0
  88. package/dist/utils/hono.d.mts +5 -0
  89. package/dist/utils/hono.mjs +24 -0
  90. package/dist/utils/importMap.d.mts +55 -0
  91. package/dist/utils/importMap.mjs +55 -0
  92. package/dist/utils/runnableConfig.d.mts +3 -0
  93. package/dist/utils/runnableConfig.mjs +45 -0
  94. package/dist/utils/serde.d.mts +5 -0
  95. package/dist/utils/serde.mjs +20 -0
  96. package/dist/vitest.config.d.ts +2 -0
  97. package/dist/vitest.config.js +11 -0
  98. package/dist/webhook.d.mts +11 -0
  99. package/dist/webhook.mjs +30 -0
  100. package/package.json +19 -19
@@ -0,0 +1,96 @@
1
+ import { z } from "zod/v3";
2
+ import * as uuid from "uuid";
3
+ import { HTTPException } from "hono/http-exception";
4
+ import { resolveGraph } from "./load.utils.mjs";
5
+ import { getStaticGraphSchema } from "./parser/index.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.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(assistants, 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 { context, ...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,
38
+ context,
39
+ if_exists: "do_nothing",
40
+ name: graphId,
41
+ }, undefined);
42
+ return resolved;
43
+ }));
44
+ }
45
+ export async function getGraph(graphId, config, options) {
46
+ assertGraphExists(graphId);
47
+ const compiled = typeof GRAPHS[graphId] === "function"
48
+ ? await GRAPHS[graphId](config ?? { configurable: {} })
49
+ : GRAPHS[graphId];
50
+ if (typeof options?.checkpointer !== "undefined") {
51
+ compiled.checkpointer = options?.checkpointer ?? undefined;
52
+ }
53
+ else {
54
+ compiled.checkpointer = checkpointer;
55
+ }
56
+ compiled.store = options?.store ?? store;
57
+ return compiled;
58
+ }
59
+ export function assertGraphExists(graphId) {
60
+ if (!GRAPHS[graphId])
61
+ throw new HTTPException(404, {
62
+ message: `Graph "${graphId}" not found`,
63
+ });
64
+ }
65
+ export function getGraphKeys() {
66
+ return Object.keys(GRAPHS);
67
+ }
68
+ export async function getCachedStaticGraphSchema(graphId) {
69
+ if (!GRAPH_SPEC[graphId])
70
+ throw new HTTPException(404, {
71
+ message: `Spec for "${graphId}" not found`,
72
+ });
73
+ if (!GRAPH_SCHEMA[graphId]) {
74
+ let timeoutMs = 30_000;
75
+ try {
76
+ const envTimeout = Number.parseInt(process.env.LANGGRAPH_SCHEMA_RESOLVE_TIMEOUT_MS ?? "0", 10);
77
+ if (!Number.isNaN(envTimeout) && envTimeout > 0) {
78
+ timeoutMs = envTimeout;
79
+ }
80
+ }
81
+ catch {
82
+ // ignore
83
+ }
84
+ try {
85
+ GRAPH_SCHEMA[graphId] = await getStaticGraphSchema(GRAPH_SPEC[graphId], {
86
+ timeoutMs,
87
+ });
88
+ }
89
+ catch (error) {
90
+ throw new Error(`Failed to extract schema for "${graphId}"`, {
91
+ cause: error,
92
+ });
93
+ }
94
+ }
95
+ return GRAPH_SCHEMA[graphId];
96
+ }
@@ -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,49 @@
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
+ if (!isCompiledGraph(graph) && "graph" in graph) {
36
+ return graph.graph;
37
+ }
38
+ return graph;
39
+ };
40
+ if (typeof graph === "function") {
41
+ return async (config) => {
42
+ const graphLike = await graph(config);
43
+ return afterResolve(graphLike);
44
+ };
45
+ }
46
+ return afterResolve(await graph);
47
+ })();
48
+ return { sourceFile, exportSymbol, resolved };
49
+ }
@@ -0,0 +1,23 @@
1
+ import type { JSONSchema7 } from "json-schema";
2
+ import type { Pregel } from "@langchain/langgraph";
3
+ export interface GraphSchema {
4
+ state: JSONSchema7 | undefined;
5
+ input: JSONSchema7 | undefined;
6
+ output: JSONSchema7 | undefined;
7
+ config: JSONSchema7 | undefined;
8
+ }
9
+ export interface GraphSpec {
10
+ sourceFile: string;
11
+ exportSymbol: string;
12
+ }
13
+ type GraphSchemaWithSubgraphs = Record<string, GraphSchema>;
14
+ export declare function getStaticGraphSchema(spec: GraphSpec, options?: {
15
+ mainThread?: boolean;
16
+ timeoutMs?: number;
17
+ }): Promise<GraphSchemaWithSubgraphs>;
18
+ export declare function getStaticGraphSchema(specMap: Record<string, GraphSpec>, options?: {
19
+ mainThread?: boolean;
20
+ timeoutMs?: number;
21
+ }): Promise<Record<string, GraphSchemaWithSubgraphs>>;
22
+ export declare function getRuntimeGraphSchema(graph: Pregel<any, any, any, any, any>): Promise<GraphSchema | undefined>;
23
+ export {};
@@ -0,0 +1,58 @@
1
+ import { fileURLToPath } from "node:url";
2
+ import { Worker } from "node:worker_threads";
3
+ const isGraphSpec = (spec) => {
4
+ if (typeof spec !== "object" || spec == null)
5
+ return false;
6
+ if (!("sourceFile" in spec) || typeof spec.sourceFile !== "string")
7
+ return false;
8
+ if (!("exportSymbol" in spec) || typeof spec.exportSymbol !== "string")
9
+ return false;
10
+ return true;
11
+ };
12
+ export async function getStaticGraphSchema(input, options) {
13
+ async function execute(specs) {
14
+ if (options?.mainThread) {
15
+ const { SubgraphExtractor } = await import("./parser.mjs");
16
+ return SubgraphExtractor.extractSchemas(specs, { strict: false });
17
+ }
18
+ return await new Promise((resolve, reject) => {
19
+ const worker = new Worker(fileURLToPath(new URL("./parser.worker.mjs", import.meta.url)), { argv: process.argv.slice(-1) });
20
+ // Set a timeout to reject if the worker takes too long
21
+ const timeoutId = setTimeout(() => {
22
+ worker.terminate();
23
+ reject(new Error("Schema extract worker timed out"));
24
+ }, options?.timeoutMs ?? 30000);
25
+ worker.on("message", (result) => {
26
+ worker.terminate();
27
+ clearTimeout(timeoutId);
28
+ resolve(result);
29
+ });
30
+ worker.on("error", reject);
31
+ worker.postMessage(specs);
32
+ });
33
+ }
34
+ const specs = isGraphSpec(input) ? [input] : Object.values(input);
35
+ const results = await execute(specs);
36
+ if (isGraphSpec(input)) {
37
+ return results[0];
38
+ }
39
+ return Object.fromEntries(Object.keys(input).map((graphId, idx) => [graphId, results[idx]]));
40
+ }
41
+ export async function getRuntimeGraphSchema(graph) {
42
+ try {
43
+ const { getInputTypeSchema, getOutputTypeSchema, getUpdateTypeSchema, getConfigTypeSchema, } = await import("@langchain/langgraph/zod/schema");
44
+ const result = {
45
+ state: getUpdateTypeSchema(graph),
46
+ input: getInputTypeSchema(graph),
47
+ output: getOutputTypeSchema(graph),
48
+ config: getConfigTypeSchema(graph),
49
+ };
50
+ if (Object.values(result).every((i) => i == null))
51
+ return undefined;
52
+ return result;
53
+ }
54
+ catch {
55
+ // ignore
56
+ }
57
+ return undefined;
58
+ }
@@ -0,0 +1,77 @@
1
+ import * as ts from "typescript";
2
+ import type { JSONSchema7 } from "json-schema";
3
+ interface GraphSchema {
4
+ state: JSONSchema7 | undefined;
5
+ input: JSONSchema7 | undefined;
6
+ output: JSONSchema7 | undefined;
7
+ config: JSONSchema7 | undefined;
8
+ }
9
+ export declare class SubgraphExtractor {
10
+ protected program: ts.Program;
11
+ protected checker: ts.TypeChecker;
12
+ protected sourceFile: ts.SourceFile;
13
+ protected inferFile: ts.SourceFile;
14
+ protected anyPregelType: ts.Type;
15
+ protected anyGraphType: ts.Type;
16
+ protected strict: boolean;
17
+ constructor(program: ts.Program, sourceFile: ts.SourceFile, inferFile: ts.SourceFile, options?: {
18
+ strict?: boolean;
19
+ });
20
+ private findTypeByName;
21
+ private find;
22
+ protected findSubgraphs: (node: ts.Node, namespace?: string[]) => {
23
+ node: string;
24
+ namespace: string[];
25
+ subgraph: {
26
+ name: string;
27
+ node: ts.Node;
28
+ };
29
+ }[];
30
+ protected getSubgraphsVariables: (name: string) => {
31
+ node: string;
32
+ namespace: string[];
33
+ subgraph: {
34
+ name: string;
35
+ node: ts.Node;
36
+ };
37
+ }[];
38
+ getAugmentedSourceFile: (sourcePath: string, name: string, options: {
39
+ allowImportingTsExtensions: boolean;
40
+ }) => {
41
+ inferFile: {
42
+ fileName: string;
43
+ contents: string;
44
+ };
45
+ sourceFile: {
46
+ fileName: string;
47
+ contents: string;
48
+ };
49
+ exports: {
50
+ typeName: string;
51
+ valueName: string;
52
+ graphName: string;
53
+ }[];
54
+ };
55
+ protected findSubgraphIdentifiers: (acc: {
56
+ node: ts.Node;
57
+ name: string;
58
+ }[], node: ts.Node) => {
59
+ node: ts.Node;
60
+ name: string;
61
+ }[];
62
+ protected isGraphOrPregelType: (type: ts.Type) => boolean;
63
+ protected getText(node: ts.Node): string;
64
+ protected reduceChildren<Acc>(node: ts.Node, fn: (acc: Acc, node: ts.Node) => Acc, initial: Acc): Acc;
65
+ static extractSchemas(target: {
66
+ sourceFile: string | {
67
+ path: string;
68
+ contents: string;
69
+ main?: boolean;
70
+ }[];
71
+ exportSymbol: string;
72
+ }[], options?: {
73
+ strict?: boolean;
74
+ tsConfigOptions?: Record<string, unknown>;
75
+ }): Record<string, GraphSchema>[];
76
+ }
77
+ export {};