@langgraph-js/sdk 1.7.1 → 1.7.5
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.d.ts +1 -0
- package/dist/LangGraphClient.js +4 -8
- 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/ToolUI.d.ts +6 -5
- package/dist/tool/ToolUI.js +12 -0
- package/dist/tool/createTool.d.ts +3 -3
- package/package.json +2 -1
- package/src/LangGraphClient.ts +655 -656
- package/src/SpendTime.ts +60 -60
- package/src/ToolManager.ts +131 -131
- package/src/index.ts +5 -5
- package/src/tool/ToolUI.ts +51 -41
- package/src/tool/copilotkit-actions.ts +72 -72
- package/src/tool/createTool.ts +102 -102
- 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 +298 -298
- 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
|
+
}
|
package/dist/tool/ToolUI.d.ts
CHANGED
|
@@ -1,14 +1,15 @@
|
|
|
1
1
|
import { RenderMessage } from "../LangGraphClient.js";
|
|
2
2
|
import { LangGraphClient } from "../LangGraphClient.js";
|
|
3
|
-
export declare class ToolRenderData<D> {
|
|
3
|
+
export declare class ToolRenderData<I, D> {
|
|
4
4
|
message: RenderMessage;
|
|
5
5
|
client: LangGraphClient;
|
|
6
6
|
constructor(message: RenderMessage, client: LangGraphClient);
|
|
7
|
-
get state(): "idle" | "done";
|
|
8
|
-
get input():
|
|
7
|
+
get state(): "idle" | "done" | "loading";
|
|
8
|
+
get input(): I | null;
|
|
9
9
|
get output(): string;
|
|
10
|
-
getJSONOutput():
|
|
10
|
+
getJSONOutput(): D;
|
|
11
11
|
/** 如果解析失败,则返回 null */
|
|
12
|
-
getJSONOutputSafe():
|
|
12
|
+
getJSONOutputSafe(): D | null;
|
|
13
|
+
getInputRepaired(): I | null;
|
|
13
14
|
response(data: D): void;
|
|
14
15
|
}
|
package/dist/tool/ToolUI.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { getMessageContent } from "../ui-store/createChatStore.js";
|
|
2
|
+
import { jsonrepair } from "jsonrepair";
|
|
2
3
|
export class ToolRenderData {
|
|
3
4
|
constructor(message, client) {
|
|
4
5
|
this.message = message;
|
|
@@ -9,6 +10,9 @@ export class ToolRenderData {
|
|
|
9
10
|
if (this.message.type === "tool" && ((_b = (_a = this.message) === null || _a === void 0 ? void 0 : _a.additional_kwargs) === null || _b === void 0 ? void 0 : _b.done)) {
|
|
10
11
|
return "done";
|
|
11
12
|
}
|
|
13
|
+
if (this.message.tool_input) {
|
|
14
|
+
return "loading";
|
|
15
|
+
}
|
|
12
16
|
return "idle";
|
|
13
17
|
}
|
|
14
18
|
get input() {
|
|
@@ -34,6 +38,14 @@ export class ToolRenderData {
|
|
|
34
38
|
return null;
|
|
35
39
|
}
|
|
36
40
|
}
|
|
41
|
+
getInputRepaired() {
|
|
42
|
+
try {
|
|
43
|
+
return JSON.parse(jsonrepair(this.message.tool_input || ""));
|
|
44
|
+
}
|
|
45
|
+
catch (e) {
|
|
46
|
+
return null;
|
|
47
|
+
}
|
|
48
|
+
}
|
|
37
49
|
response(data) {
|
|
38
50
|
this.client.doneFEToolWaiting(this.message.id, JSON.stringify(data));
|
|
39
51
|
}
|
|
@@ -11,7 +11,7 @@ export interface UnionTool<Args extends ZodRawShape, Child extends Object = Obje
|
|
|
11
11
|
execute: ToolCallback<Args>;
|
|
12
12
|
/** 工具执行成功后触发的附加消息 */
|
|
13
13
|
callbackMessage?: (result: CallToolResult) => Message[];
|
|
14
|
-
render?: <D>(tool: ToolRenderData<D>) => Child;
|
|
14
|
+
render?: <D>(tool: ToolRenderData<z.objectOutputType<Args, ZodTypeAny>, D>) => Child;
|
|
15
15
|
onlyRender?: boolean;
|
|
16
16
|
/** 只允许指定的 agent 使用该工具,如果未指定,则所有 agent 都可以使用 */
|
|
17
17
|
allowAgent?: string[];
|
|
@@ -54,10 +54,10 @@ export declare const createMCPTool: <Args extends ZodRawShape>(tool: UnionTool<A
|
|
|
54
54
|
}>))[];
|
|
55
55
|
export declare const createToolUI: <Args extends Parameter[] | [] = [], Child extends Object = {}>(tool: Action<Args> & {
|
|
56
56
|
allowAgent?: string[];
|
|
57
|
-
render?: (tool: ToolRenderData<any>) => Child;
|
|
57
|
+
render?: (tool: ToolRenderData<any, any>) => Child;
|
|
58
58
|
onlyRender?: boolean;
|
|
59
59
|
}) => {
|
|
60
|
-
render: ((tool: ToolRenderData<any>) => Child) | undefined;
|
|
60
|
+
render: ((tool: ToolRenderData<any, any>) => Child) | undefined;
|
|
61
61
|
onlyRender: boolean | undefined;
|
|
62
62
|
name: string;
|
|
63
63
|
description: string;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@langgraph-js/sdk",
|
|
3
|
-
"version": "1.7.
|
|
3
|
+
"version": "1.7.5",
|
|
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",
|
|
@@ -30,6 +30,7 @@
|
|
|
30
30
|
},
|
|
31
31
|
"dependencies": {
|
|
32
32
|
"@langchain/langgraph-sdk": "^0.0.77",
|
|
33
|
+
"jsonrepair": "^3.12.0",
|
|
33
34
|
"nanostores": "^1.0.1",
|
|
34
35
|
"zod": "^3.25.17",
|
|
35
36
|
"zod-to-json-schema": "^3.24.5"
|