@langgraph-js/sdk 1.7.10 → 1.8.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (43) hide show
  1. package/.env +0 -0
  2. package/.turbo/turbo-build.log +5 -0
  3. package/LICENSE +201 -201
  4. package/README.md +163 -163
  5. package/dist/LangGraphClient.js +3 -0
  6. package/dist/ToolManager.d.ts +1 -1
  7. package/dist/ToolManager.js +2 -1
  8. package/dist/server/createState.d.ts +13 -0
  9. package/dist/server/createState.js +20 -0
  10. package/dist/server/feTools.d.ts +16 -0
  11. package/dist/server/feTools.js +37 -0
  12. package/dist/server/index.d.ts +3 -0
  13. package/dist/server/index.js +3 -0
  14. package/dist/server/interrupt/index.d.ts +23 -0
  15. package/dist/server/interrupt/index.js +36 -0
  16. package/dist/server/swarm/handoff.d.ts +11 -0
  17. package/dist/server/swarm/handoff.js +84 -0
  18. package/dist/server/swarm/keepState.d.ts +6 -0
  19. package/dist/server/swarm/keepState.js +21 -0
  20. package/dist/server/tools/index.d.ts +1 -0
  21. package/dist/server/tools/index.js +1 -0
  22. package/dist/server/tools/sequential-thinking.d.ts +52 -0
  23. package/dist/server/tools/sequential-thinking.js +69 -0
  24. package/dist/server/utils.d.ts +3 -0
  25. package/dist/server/utils.js +24 -0
  26. package/dist/tool/createTool.d.ts +27 -23
  27. package/dist/tool/createTool.js +32 -9
  28. package/package.json +1 -1
  29. package/src/LangGraphClient.ts +658 -655
  30. package/src/SpendTime.ts +60 -60
  31. package/src/ToolManager.ts +132 -132
  32. package/src/index.ts +5 -5
  33. package/src/tool/ToolUI.ts +55 -55
  34. package/src/tool/copilotkit-actions.ts +72 -72
  35. package/src/tool/createTool.ts +123 -104
  36. package/src/tool/index.ts +3 -3
  37. package/src/tool/utils.ts +158 -158
  38. package/src/ui-store/UnionStore.ts +29 -29
  39. package/src/ui-store/createChatStore.ts +295 -295
  40. package/src/ui-store/index.ts +2 -2
  41. package/src/ui-store/rafDebounce.ts +29 -29
  42. package/test/testResponse.json +5418 -5418
  43. package/tsconfig.json +112 -112
@@ -0,0 +1,52 @@
1
+ import { z } from "zod";
2
+ export declare const SequentialThinkingTool: import("@langchain/core/tools").DynamicStructuredTool<z.ZodObject<{
3
+ thought: z.ZodString;
4
+ nextThoughtNeeded: z.ZodBoolean;
5
+ thoughtNumber: z.ZodNumber;
6
+ totalThoughts: z.ZodNumber;
7
+ isRevision: z.ZodOptional<z.ZodBoolean>;
8
+ revisesThought: z.ZodOptional<z.ZodNumber>;
9
+ branchFromThought: z.ZodOptional<z.ZodNumber>;
10
+ branchId: z.ZodOptional<z.ZodString>;
11
+ needsMoreThoughts: z.ZodOptional<z.ZodBoolean>;
12
+ }, "strip", z.ZodTypeAny, {
13
+ thought: string;
14
+ nextThoughtNeeded: boolean;
15
+ thoughtNumber: number;
16
+ totalThoughts: number;
17
+ isRevision?: boolean | undefined;
18
+ revisesThought?: number | undefined;
19
+ branchFromThought?: number | undefined;
20
+ branchId?: string | undefined;
21
+ needsMoreThoughts?: boolean | undefined;
22
+ }, {
23
+ thought: string;
24
+ nextThoughtNeeded: boolean;
25
+ thoughtNumber: number;
26
+ totalThoughts: number;
27
+ isRevision?: boolean | undefined;
28
+ revisesThought?: number | undefined;
29
+ branchFromThought?: number | undefined;
30
+ branchId?: string | undefined;
31
+ needsMoreThoughts?: boolean | undefined;
32
+ }>, {
33
+ thought: string;
34
+ nextThoughtNeeded: boolean;
35
+ thoughtNumber: number;
36
+ totalThoughts: number;
37
+ isRevision?: boolean | undefined;
38
+ revisesThought?: number | undefined;
39
+ branchFromThought?: number | undefined;
40
+ branchId?: string | undefined;
41
+ needsMoreThoughts?: boolean | undefined;
42
+ }, {
43
+ thought: string;
44
+ nextThoughtNeeded: boolean;
45
+ thoughtNumber: number;
46
+ totalThoughts: number;
47
+ isRevision?: boolean | undefined;
48
+ revisesThought?: number | undefined;
49
+ branchFromThought?: number | undefined;
50
+ branchId?: string | undefined;
51
+ needsMoreThoughts?: boolean | undefined;
52
+ }, string>;
@@ -0,0 +1,69 @@
1
+ import { tool } from "@langchain/core/tools";
2
+ import { z } from "zod";
3
+ const schema = z.object({
4
+ thought: z.string().describe("Your current thinking step"),
5
+ nextThoughtNeeded: z.boolean().describe("Whether another thought step is needed"),
6
+ thoughtNumber: z.number().min(1).describe("Current thought number"),
7
+ totalThoughts: z.number().min(1).describe("Estimated total thoughts needed"),
8
+ isRevision: z.boolean().optional().describe("Whether this revises previous thinking"),
9
+ revisesThought: z.number().min(1).optional().describe("Which thought is being reconsidered"),
10
+ branchFromThought: z.number().min(1).optional().describe("Branching point thought number"),
11
+ branchId: z.string().optional().describe("Branch identifier"),
12
+ needsMoreThoughts: z.boolean().optional().describe("If more thoughts are needed"),
13
+ });
14
+ // 存储思考历史
15
+ const thoughtHistory = [];
16
+ const branches = {};
17
+ export const SequentialThinkingTool = tool(async (args) => {
18
+ try {
19
+ if (args.thoughtNumber > args.totalThoughts) {
20
+ args.totalThoughts = args.thoughtNumber;
21
+ }
22
+ thoughtHistory.push(args);
23
+ if (args.branchFromThought && args.branchId) {
24
+ if (!branches[args.branchId]) {
25
+ branches[args.branchId] = [];
26
+ }
27
+ branches[args.branchId].push(args);
28
+ }
29
+ return JSON.stringify({
30
+ thoughtNumber: args.thoughtNumber,
31
+ totalThoughts: args.totalThoughts,
32
+ nextThoughtNeeded: args.nextThoughtNeeded,
33
+ branches: Object.keys(branches),
34
+ thoughtHistoryLength: thoughtHistory.length,
35
+ }, null, 2);
36
+ }
37
+ catch (error) {
38
+ return JSON.stringify({
39
+ error: error instanceof Error ? error.message : String(error),
40
+ status: "failed",
41
+ }, null, 2);
42
+ }
43
+ }, {
44
+ name: "sequential-thinking",
45
+ description: `A detailed tool for dynamic and reflective problem-solving through thoughts.
46
+ This tool helps analyze problems through a flexible thinking process that can adapt and evolve.
47
+ Each thought can build on, question, or revise previous insights as understanding deepens.
48
+
49
+ When to use this tool:
50
+ - Breaking down complex problems into steps
51
+ - Planning and design with room for revision
52
+ - Analysis that might need course correction
53
+ - Problems where the full scope might not be clear initially
54
+ - Problems that require a multi-step solution
55
+ - Tasks that need to maintain context over multiple steps
56
+ - Situations where irrelevant information needs to be filtered out
57
+
58
+ Key features:
59
+ - You can adjust total_thoughts up or down as you progress
60
+ - You can question or revise previous thoughts
61
+ - You can add more thoughts even after reaching what seemed like the end
62
+ - You can express uncertainty and explore alternative approaches
63
+ - Not every thought needs to build linearly - you can branch or backtrack
64
+ - Generates a solution hypothesis
65
+ - Verifies the hypothesis based on the Chain of Thought steps
66
+ - Repeats the process until satisfied
67
+ - Provides a correct answer`,
68
+ schema,
69
+ });
@@ -0,0 +1,3 @@
1
+ import { BaseMessage, HumanMessage } from "@langchain/core/messages";
2
+ export declare const getTextMessageContent: (message: BaseMessage) => string;
3
+ export declare function getLastHumanMessage(messages: BaseMessage[]): HumanMessage | undefined;
@@ -0,0 +1,24 @@
1
+ import { isHumanMessage } from "@langchain/core/messages";
2
+ export const getTextMessageContent = (message) => {
3
+ if (typeof message.content === "string") {
4
+ return message.content;
5
+ }
6
+ else {
7
+ return message.content
8
+ .filter((i) => i.type === "text")
9
+ .map((i) => i.text)
10
+ .join("\n");
11
+ }
12
+ };
13
+ export function getLastHumanMessage(messages) {
14
+ // 从后往前遍历消息列表
15
+ for (let i = messages.length - 1; i >= 0; i--) {
16
+ const message = messages[i];
17
+ // 检查消息是否是 HumanMessage 的实例
18
+ if (isHumanMessage(message)) {
19
+ return message;
20
+ }
21
+ }
22
+ // 如果没有找到 HumanMessage,则返回 undefined
23
+ return undefined;
24
+ }
@@ -2,16 +2,17 @@ import { z, ZodRawShape, ZodTypeAny } from "zod";
2
2
  import { Action, Parameter } from "./copilotkit-actions.js";
3
3
  import { Message } from "@langchain/langgraph-sdk";
4
4
  import { ToolRenderData } from "./ToolUI.js";
5
- export interface UnionTool<Args extends ZodRawShape, Child extends Object = Object> {
5
+ export interface UnionTool<Args extends ZodRawShape, Child extends Object = Object, D = any> {
6
6
  name: string;
7
7
  description: string;
8
8
  parameters: Args;
9
9
  /** 是否直接返回工具结果,而不是通过消息返回 */
10
10
  returnDirect?: boolean;
11
- execute: ToolCallback<Args>;
11
+ execute?: ToolCallback<Args>;
12
12
  /** 工具执行成功后触发的附加消息 */
13
13
  callbackMessage?: (result: CallToolResult) => Message[];
14
- render?: <D>(tool: ToolRenderData<z.objectOutputType<Args, ZodTypeAny>, D>) => Child;
14
+ handler?: (args: z.objectOutputType<Args, ZodTypeAny>, context?: any) => D | Promise<D>;
15
+ render?: (tool: ToolRenderData<z.objectOutputType<Args, ZodTypeAny>, D>) => Child;
15
16
  onlyRender?: boolean;
16
17
  /** 只允许指定的 agent 使用该工具,如果未指定,则所有 agent 都可以使用 */
17
18
  allowAgent?: string[];
@@ -24,12 +25,20 @@ export type CallToolResult = string | {
24
25
  text: string;
25
26
  }[];
26
27
  /** 用于格式校验 */
27
- export declare const createTool: <Args extends ZodRawShape>(tool: UnionTool<Args>) => UnionTool<Args, Object>;
28
- /** 提供一种兼容 copilotkit 的定义方式,简化定义形式
28
+ export declare const createTool: <Args extends ZodRawShape>(tool: UnionTool<Args>) => UnionTool<Args, Object, any>;
29
+ /**
30
+ * create Type Safe Tool with zod and UI Render Feature
31
+ */
32
+ export declare const createUITool: <Args extends ZodRawShape, Child extends Object = {}>(tool: UnionTool<Args, Child>) => UnionTool<Args, Child>;
33
+ /**
34
+ * 提供一种兼容 copilotkit 的定义方式,简化定义形式
29
35
  * 来自 copilotkit 的 frontend action
30
36
  */
31
- export declare const createFETool: <const T extends Parameter[], Args extends ZodRawShape>(tool: Action<T> & {
37
+ export declare const createFETool: <const T extends Parameter[], Args extends ZodRawShape, Child extends Object = {}>(tool: Action<T> & {
32
38
  allowAgent?: string[];
39
+ allowGraph?: string[];
40
+ render?: (tool: ToolRenderData<any, any>) => Child;
41
+ onlyRender?: boolean;
33
42
  }) => UnionTool<Args>;
34
43
  export declare const createJSONDefineTool: <Args extends ZodRawShape>(tool: UnionTool<Args>) => {
35
44
  name: string;
@@ -47,6 +56,12 @@ export declare const createMCPTool: <Args extends ZodRawShape>(tool: UnionTool<A
47
56
  text: string;
48
57
  }[];
49
58
  isError?: undefined;
59
+ } | {
60
+ content: {
61
+ type: "text";
62
+ text: string;
63
+ }[] | undefined;
64
+ isError?: undefined;
50
65
  } | {
51
66
  content: {
52
67
  type: string;
@@ -54,23 +69,12 @@ export declare const createMCPTool: <Args extends ZodRawShape>(tool: UnionTool<A
54
69
  }[];
55
70
  isError: boolean;
56
71
  }>))[];
57
- export declare const createToolUI: <Args extends Parameter[] | [] = [], Child extends Object = {}>(tool: Action<Args> & {
72
+ /**
73
+ * @deprecated Use createUITool instead
74
+ */
75
+ export declare const createToolUI: <const T extends Parameter[], Args extends ZodRawShape, Child extends Object = {}>(tool: Action<T> & {
58
76
  allowAgent?: string[];
77
+ allowGraph?: string[];
59
78
  render?: (tool: ToolRenderData<any, any>) => Child;
60
79
  onlyRender?: boolean;
61
- }) => {
62
- render: ((tool: ToolRenderData<any, any>) => Child) | undefined;
63
- onlyRender: boolean | undefined;
64
- name: string;
65
- description: string;
66
- parameters: z.ZodRawShape;
67
- /** 是否直接返回工具结果,而不是通过消息返回 */
68
- returnDirect?: boolean;
69
- execute: ToolCallback<z.ZodRawShape>;
70
- /** 工具执行成功后触发的附加消息 */
71
- callbackMessage?: (result: CallToolResult) => Message[];
72
- /** 只允许指定的 agent 使用该工具,如果未指定,则所有 agent 都可以使用 */
73
- allowAgent?: string[];
74
- /** 只允许指定的 Graph 使用该工具 */
75
- allowGraph?: string[];
76
- };
80
+ }) => UnionTool<Args>;
@@ -5,17 +5,42 @@ import { zodToJsonSchema } from "zod-to-json-schema";
5
5
  export const createTool = (tool) => {
6
6
  return tool;
7
7
  };
8
- /** 提供一种兼容 copilotkit 的定义方式,简化定义形式
8
+ /**
9
+ * create Type Safe Tool with zod and UI Render Feature
10
+ */
11
+ export const createUITool = (tool) => {
12
+ return {
13
+ ...tool,
14
+ async execute(args, context) {
15
+ var _a;
16
+ try {
17
+ const result = await ((_a = tool.handler) === null || _a === void 0 ? void 0 : _a.call(tool, args, context));
18
+ if (typeof result === "string") {
19
+ return [{ type: "text", text: result }];
20
+ }
21
+ return [{ type: "text", text: JSON.stringify(result) }];
22
+ }
23
+ catch (error) {
24
+ return [{ type: "text", text: `Error: ${error}` }];
25
+ }
26
+ },
27
+ };
28
+ };
29
+ /**
30
+ * 提供一种兼容 copilotkit 的定义方式,简化定义形式
9
31
  * 来自 copilotkit 的 frontend action
10
32
  */
11
33
  export const createFETool = (tool) => {
12
34
  return {
35
+ render: tool.render,
36
+ onlyRender: tool.onlyRender,
13
37
  name: tool.name,
14
38
  description: tool.description || "",
15
39
  parameters: convertJsonSchemaToZodRawShape(actionParametersToJsonSchema(tool.parameters || [])),
16
40
  returnDirect: tool.returnDirect,
17
41
  callbackMessage: tool.callbackMessage,
18
42
  allowAgent: tool.allowAgent,
43
+ allowGraph: tool.allowGraph,
19
44
  async execute(args, context) {
20
45
  var _a;
21
46
  try {
@@ -45,8 +70,9 @@ export const createMCPTool = (tool) => {
45
70
  tool.description,
46
71
  tool.parameters,
47
72
  async (args) => {
73
+ var _a;
48
74
  try {
49
- const result = await tool.execute(args);
75
+ const result = await ((_a = tool.execute) === null || _a === void 0 ? void 0 : _a.call(tool, args));
50
76
  if (typeof result === "string") {
51
77
  return { content: [{ type: "text", text: result }] };
52
78
  }
@@ -60,10 +86,7 @@ export const createMCPTool = (tool) => {
60
86
  },
61
87
  ];
62
88
  };
63
- export const createToolUI = (tool) => {
64
- return {
65
- ...createFETool(tool),
66
- render: tool.render,
67
- onlyRender: tool.onlyRender,
68
- };
69
- };
89
+ /**
90
+ * @deprecated Use createUITool instead
91
+ */
92
+ export const createToolUI = createFETool;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@langgraph-js/sdk",
3
- "version": "1.7.10",
3
+ "version": "1.8.0",
4
4
  "description": "The UI SDK for LangGraph - seamlessly integrate your AI agents with frontend interfaces",
5
5
  "main": "dist/index.js",
6
6
  "type": "module",