@langgraph-js/sdk 1.7.11 → 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.
- package/.env +0 -0
- package/.turbo/turbo-build.log +5 -0
- package/LICENSE +201 -201
- package/README.md +163 -163
- package/dist/LangGraphClient.js +3 -0
- package/dist/ToolManager.d.ts +1 -1
- package/dist/ToolManager.js +2 -1
- package/dist/server/createState.d.ts +13 -0
- package/dist/server/createState.js +20 -0
- package/dist/server/feTools.d.ts +16 -0
- package/dist/server/feTools.js +37 -0
- package/dist/server/index.d.ts +3 -0
- package/dist/server/index.js +3 -0
- package/dist/server/interrupt/index.d.ts +23 -0
- package/dist/server/interrupt/index.js +36 -0
- package/dist/server/swarm/handoff.d.ts +11 -0
- package/dist/server/swarm/handoff.js +84 -0
- package/dist/server/swarm/keepState.d.ts +6 -0
- package/dist/server/swarm/keepState.js +21 -0
- package/dist/server/tools/index.d.ts +1 -0
- package/dist/server/tools/index.js +1 -0
- package/dist/server/tools/sequential-thinking.d.ts +52 -0
- package/dist/server/tools/sequential-thinking.js +69 -0
- package/dist/server/utils.d.ts +3 -0
- package/dist/server/utils.js +24 -0
- package/dist/tool/createTool.d.ts +25 -23
- package/dist/tool/createTool.js +31 -9
- package/package.json +1 -1
- package/src/LangGraphClient.ts +658 -655
- package/src/SpendTime.ts +60 -60
- package/src/ToolManager.ts +132 -132
- package/src/index.ts +5 -5
- package/src/tool/ToolUI.ts +55 -55
- package/src/tool/copilotkit-actions.ts +72 -72
- package/src/tool/createTool.ts +123 -107
- package/src/tool/index.ts +3 -3
- package/src/tool/utils.ts +158 -158
- package/src/ui-store/UnionStore.ts +29 -29
- package/src/ui-store/createChatStore.ts +295 -295
- package/src/ui-store/index.ts +2 -2
- package/src/ui-store/rafDebounce.ts +29 -29
- package/test/testResponse.json +5418 -5418
- 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,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
|
|
11
|
+
execute?: ToolCallback<Args>;
|
|
12
12
|
/** 工具执行成功后触发的附加消息 */
|
|
13
13
|
callbackMessage?: (result: CallToolResult) => Message[];
|
|
14
|
-
|
|
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,13 +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
|
-
/**
|
|
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[];
|
|
33
39
|
allowGraph?: string[];
|
|
40
|
+
render?: (tool: ToolRenderData<any, any>) => Child;
|
|
41
|
+
onlyRender?: boolean;
|
|
34
42
|
}) => UnionTool<Args>;
|
|
35
43
|
export declare const createJSONDefineTool: <Args extends ZodRawShape>(tool: UnionTool<Args>) => {
|
|
36
44
|
name: string;
|
|
@@ -48,6 +56,12 @@ export declare const createMCPTool: <Args extends ZodRawShape>(tool: UnionTool<A
|
|
|
48
56
|
text: string;
|
|
49
57
|
}[];
|
|
50
58
|
isError?: undefined;
|
|
59
|
+
} | {
|
|
60
|
+
content: {
|
|
61
|
+
type: "text";
|
|
62
|
+
text: string;
|
|
63
|
+
}[] | undefined;
|
|
64
|
+
isError?: undefined;
|
|
51
65
|
} | {
|
|
52
66
|
content: {
|
|
53
67
|
type: string;
|
|
@@ -55,24 +69,12 @@ export declare const createMCPTool: <Args extends ZodRawShape>(tool: UnionTool<A
|
|
|
55
69
|
}[];
|
|
56
70
|
isError: boolean;
|
|
57
71
|
}>))[];
|
|
58
|
-
|
|
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> & {
|
|
59
76
|
allowAgent?: string[];
|
|
60
77
|
allowGraph?: string[];
|
|
61
78
|
render?: (tool: ToolRenderData<any, any>) => Child;
|
|
62
79
|
onlyRender?: boolean;
|
|
63
|
-
}) =>
|
|
64
|
-
render: ((tool: ToolRenderData<any, any>) => Child) | undefined;
|
|
65
|
-
onlyRender: boolean | undefined;
|
|
66
|
-
name: string;
|
|
67
|
-
description: string;
|
|
68
|
-
parameters: z.ZodRawShape;
|
|
69
|
-
/** 是否直接返回工具结果,而不是通过消息返回 */
|
|
70
|
-
returnDirect?: boolean;
|
|
71
|
-
execute: ToolCallback<z.ZodRawShape>;
|
|
72
|
-
/** 工具执行成功后触发的附加消息 */
|
|
73
|
-
callbackMessage?: (result: CallToolResult) => Message[];
|
|
74
|
-
/** 只允许指定的 agent 使用该工具,如果未指定,则所有 agent 都可以使用 */
|
|
75
|
-
allowAgent?: string[];
|
|
76
|
-
/** 只允许指定的 Graph 使用该工具 */
|
|
77
|
-
allowGraph?: string[];
|
|
78
|
-
};
|
|
80
|
+
}) => UnionTool<Args>;
|
package/dist/tool/createTool.js
CHANGED
|
@@ -5,11 +5,35 @@ import { zodToJsonSchema } from "zod-to-json-schema";
|
|
|
5
5
|
export const createTool = (tool) => {
|
|
6
6
|
return tool;
|
|
7
7
|
};
|
|
8
|
-
/**
|
|
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 || [])),
|
|
@@ -46,8 +70,9 @@ export const createMCPTool = (tool) => {
|
|
|
46
70
|
tool.description,
|
|
47
71
|
tool.parameters,
|
|
48
72
|
async (args) => {
|
|
73
|
+
var _a;
|
|
49
74
|
try {
|
|
50
|
-
const result = await tool.execute(args);
|
|
75
|
+
const result = await ((_a = tool.execute) === null || _a === void 0 ? void 0 : _a.call(tool, args));
|
|
51
76
|
if (typeof result === "string") {
|
|
52
77
|
return { content: [{ type: "text", text: result }] };
|
|
53
78
|
}
|
|
@@ -61,10 +86,7 @@ export const createMCPTool = (tool) => {
|
|
|
61
86
|
},
|
|
62
87
|
];
|
|
63
88
|
};
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
onlyRender: tool.onlyRender,
|
|
69
|
-
};
|
|
70
|
-
};
|
|
89
|
+
/**
|
|
90
|
+
* @deprecated Use createUITool instead
|
|
91
|
+
*/
|
|
92
|
+
export const createToolUI = createFETool;
|