@langgraph-js/sdk 1.1.8 → 1.1.9
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/LICENSE +201 -201
- package/README.md +163 -163
- package/dist/LangGraphClient.d.ts +3 -1
- package/dist/LangGraphClient.js +11 -3
- package/dist/ui-store/createChatStore.js +9 -3
- package/dist/ui-store/rafDebounce.d.ts +6 -0
- package/dist/ui-store/rafDebounce.js +26 -0
- package/package.json +2 -2
- package/src/LangGraphClient.ts +563 -554
- package/src/SpendTime.ts +60 -60
- package/src/ToolManager.ts +123 -123
- package/src/index.ts +5 -5
- package/src/tool/copilotkit-actions.ts +72 -72
- package/src/tool/createTool.ts +78 -78
- package/src/tool/index.ts +2 -2
- package/src/tool/utils.ts +158 -158
- package/src/ui-store/UnionStore.ts +29 -29
- package/src/ui-store/createChatStore.ts +249 -241
- package/src/ui-store/index.ts +2 -2
- package/src/ui-store/rafDebounce.ts +29 -0
- package/test/testResponse.json +5418 -5418
- package/tsconfig.json +112 -112
package/README.md
CHANGED
|
@@ -1,163 +1,163 @@
|
|
|
1
|
-
# @langgraph-js/sdk
|
|
2
|
-
|
|
3
|
-

|
|
4
|
-

|
|
5
|
-
|
|
6
|
-
> The missing UI SDK for LangGraph - seamlessly integrate your AI agents with frontend interfaces
|
|
7
|
-
|
|
8
|
-
## Why @langgraph-js/sdk?
|
|
9
|
-
|
|
10
|
-
Building AI agent applications is complex, especially when you need to bridge the gap between LangGraph agents and interactive user interfaces. This SDK solves the critical challenges of frontend integration:
|
|
11
|
-
|
|
12
|
-
- **Provides a complete UI integration layer** - no more complex custom code to handle tools, streaming, and state management
|
|
13
|
-
- **Simplifies human-in-the-loop interactions** - easily incorporate user feedback within agent workflows
|
|
14
|
-
- **Handles edge cases automatically** - interruptions, errors, token management and more
|
|
15
|
-
- **Offers a rich set of UI components** - ready-to-use elements to display agent interactions
|
|
16
|
-
|
|
17
|
-
[DOCS](https://langgraph-js.netlify.app)
|
|
18
|
-
|
|
19
|
-
## Installation
|
|
20
|
-
|
|
21
|
-
```bash
|
|
22
|
-
# Using npm
|
|
23
|
-
npm install @langgraph-js/sdk
|
|
24
|
-
|
|
25
|
-
# Using yarn
|
|
26
|
-
yarn add @langgraph-js/sdk
|
|
27
|
-
|
|
28
|
-
# Using pnpm
|
|
29
|
-
pnpm add @langgraph-js/sdk
|
|
30
|
-
```
|
|
31
|
-
|
|
32
|
-
## Key Features
|
|
33
|
-
|
|
34
|
-
### Generative UI
|
|
35
|
-
|
|
36
|
-
- ✅ Custom Tool Messages
|
|
37
|
-
- ✅ Token Counter
|
|
38
|
-
- ✅ Stop Graph Progress
|
|
39
|
-
- ✅ Interrupt Handling
|
|
40
|
-
- ✅ Error Handling
|
|
41
|
-
- ✅ Spend Time Tracking
|
|
42
|
-
- ✅ Time Persistence
|
|
43
|
-
|
|
44
|
-
### Frontend Actions
|
|
45
|
-
|
|
46
|
-
- ✅ Definition of Union Tools
|
|
47
|
-
- ✅ Frontend Functions As Tools
|
|
48
|
-
- ✅ Human-in-the-Loop Interaction
|
|
49
|
-
- ✅ Interrupt Mode
|
|
50
|
-
|
|
51
|
-
### Authorization
|
|
52
|
-
|
|
53
|
-
- ✅ Cookie-Based Authentication
|
|
54
|
-
- ✅ Custom Token Authentication
|
|
55
|
-
|
|
56
|
-
### Persistence
|
|
57
|
-
|
|
58
|
-
- ✅ Read History from LangGraph
|
|
59
|
-
|
|
60
|
-
## Advanced Usage
|
|
61
|
-
|
|
62
|
-
### Creating a Chat Store
|
|
63
|
-
|
|
64
|
-
You can easily create a reactive store for your LangGraph client:
|
|
65
|
-
|
|
66
|
-
```typescript
|
|
67
|
-
import { createChatStore } from "@langgraph-js/sdk";
|
|
68
|
-
|
|
69
|
-
export const globalChatStore = createChatStore(
|
|
70
|
-
"agent",
|
|
71
|
-
{
|
|
72
|
-
// Custom LangGraph backend interaction
|
|
73
|
-
apiUrl: "http://localhost:8123",
|
|
74
|
-
// Custom headers for authentication
|
|
75
|
-
defaultHeaders: JSON.parse(localStorage.getItem("code") || "{}"),
|
|
76
|
-
callerOptions: {
|
|
77
|
-
// Example for including cookies
|
|
78
|
-
// fetch(url: string, options: RequestInit) {
|
|
79
|
-
// options.credentials = "include";
|
|
80
|
-
// return fetch(url, options);
|
|
81
|
-
// },
|
|
82
|
-
},
|
|
83
|
-
},
|
|
84
|
-
{
|
|
85
|
-
onInit(client) {
|
|
86
|
-
client.tools.bindTools([]);
|
|
87
|
-
},
|
|
88
|
-
}
|
|
89
|
-
);
|
|
90
|
-
```
|
|
91
|
-
|
|
92
|
-
### React Integration
|
|
93
|
-
|
|
94
|
-
First, install the nanostores React integration:
|
|
95
|
-
|
|
96
|
-
```bash
|
|
97
|
-
pnpm i @nanostores/react
|
|
98
|
-
```
|
|
99
|
-
|
|
100
|
-
Then create a context provider for your chat:
|
|
101
|
-
|
|
102
|
-
```tsx
|
|
103
|
-
import React, { createContext, useContext, useEffect } from "react";
|
|
104
|
-
import { globalChatStore } from "../store"; // Import your store
|
|
105
|
-
import { UnionStore, useUnionStore } from "@langgraph-js/sdk";
|
|
106
|
-
import { useStore } from "@nanostores/react";
|
|
107
|
-
|
|
108
|
-
type ChatContextType = UnionStore<typeof globalChatStore>;
|
|
109
|
-
|
|
110
|
-
const ChatContext = createContext<ChatContextType | undefined>(undefined);
|
|
111
|
-
|
|
112
|
-
export const useChat = () => {
|
|
113
|
-
const context = useContext(ChatContext);
|
|
114
|
-
if (!context) {
|
|
115
|
-
throw new Error("useChat must be used within a ChatProvider");
|
|
116
|
-
}
|
|
117
|
-
return context;
|
|
118
|
-
};
|
|
119
|
-
|
|
120
|
-
export const ChatProvider = ({ children }) => {
|
|
121
|
-
// Use store to ensure React gets reactive state updates
|
|
122
|
-
const store = useUnionStore(globalChatStore, useStore);
|
|
123
|
-
|
|
124
|
-
useEffect(() => {
|
|
125
|
-
// Initialize client
|
|
126
|
-
store.initClient().then(() => {
|
|
127
|
-
// Initialize conversation history
|
|
128
|
-
store.refreshHistoryList();
|
|
129
|
-
});
|
|
130
|
-
}, [store.currentAgent]);
|
|
131
|
-
|
|
132
|
-
return <ChatContext.Provider value={store}>{children}</ChatContext.Provider>;
|
|
133
|
-
};
|
|
134
|
-
```
|
|
135
|
-
|
|
136
|
-
Use it in your components:
|
|
137
|
-
|
|
138
|
-
```tsx
|
|
139
|
-
export const MyChat = () => {
|
|
140
|
-
return (
|
|
141
|
-
<ChatProvider>
|
|
142
|
-
<ChatComp></ChatComp>
|
|
143
|
-
</ChatProvider>
|
|
144
|
-
);
|
|
145
|
-
};
|
|
146
|
-
|
|
147
|
-
function ChatComp() {
|
|
148
|
-
const chat = useChat();
|
|
149
|
-
// Use chat store methods and state here
|
|
150
|
-
}
|
|
151
|
-
```
|
|
152
|
-
|
|
153
|
-
## Documentation
|
|
154
|
-
|
|
155
|
-
For complete documentation, visit our [official docs](https://langgraph-js.netlify.app).
|
|
156
|
-
|
|
157
|
-
## Contributing
|
|
158
|
-
|
|
159
|
-
Contributions are welcome! Please feel free to submit a Pull Request.
|
|
160
|
-
|
|
161
|
-
## License
|
|
162
|
-
|
|
163
|
-
This project is licensed under the Apache-2.0 License.
|
|
1
|
+
# @langgraph-js/sdk
|
|
2
|
+
|
|
3
|
+

|
|
4
|
+

|
|
5
|
+
|
|
6
|
+
> The missing UI SDK for LangGraph - seamlessly integrate your AI agents with frontend interfaces
|
|
7
|
+
|
|
8
|
+
## Why @langgraph-js/sdk?
|
|
9
|
+
|
|
10
|
+
Building AI agent applications is complex, especially when you need to bridge the gap between LangGraph agents and interactive user interfaces. This SDK solves the critical challenges of frontend integration:
|
|
11
|
+
|
|
12
|
+
- **Provides a complete UI integration layer** - no more complex custom code to handle tools, streaming, and state management
|
|
13
|
+
- **Simplifies human-in-the-loop interactions** - easily incorporate user feedback within agent workflows
|
|
14
|
+
- **Handles edge cases automatically** - interruptions, errors, token management and more
|
|
15
|
+
- **Offers a rich set of UI components** - ready-to-use elements to display agent interactions
|
|
16
|
+
|
|
17
|
+
[DOCS](https://langgraph-js.netlify.app)
|
|
18
|
+
|
|
19
|
+
## Installation
|
|
20
|
+
|
|
21
|
+
```bash
|
|
22
|
+
# Using npm
|
|
23
|
+
npm install @langgraph-js/sdk
|
|
24
|
+
|
|
25
|
+
# Using yarn
|
|
26
|
+
yarn add @langgraph-js/sdk
|
|
27
|
+
|
|
28
|
+
# Using pnpm
|
|
29
|
+
pnpm add @langgraph-js/sdk
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
## Key Features
|
|
33
|
+
|
|
34
|
+
### Generative UI
|
|
35
|
+
|
|
36
|
+
- ✅ Custom Tool Messages
|
|
37
|
+
- ✅ Token Counter
|
|
38
|
+
- ✅ Stop Graph Progress
|
|
39
|
+
- ✅ Interrupt Handling
|
|
40
|
+
- ✅ Error Handling
|
|
41
|
+
- ✅ Spend Time Tracking
|
|
42
|
+
- ✅ Time Persistence
|
|
43
|
+
|
|
44
|
+
### Frontend Actions
|
|
45
|
+
|
|
46
|
+
- ✅ Definition of Union Tools
|
|
47
|
+
- ✅ Frontend Functions As Tools
|
|
48
|
+
- ✅ Human-in-the-Loop Interaction
|
|
49
|
+
- ✅ Interrupt Mode
|
|
50
|
+
|
|
51
|
+
### Authorization
|
|
52
|
+
|
|
53
|
+
- ✅ Cookie-Based Authentication
|
|
54
|
+
- ✅ Custom Token Authentication
|
|
55
|
+
|
|
56
|
+
### Persistence
|
|
57
|
+
|
|
58
|
+
- ✅ Read History from LangGraph
|
|
59
|
+
|
|
60
|
+
## Advanced Usage
|
|
61
|
+
|
|
62
|
+
### Creating a Chat Store
|
|
63
|
+
|
|
64
|
+
You can easily create a reactive store for your LangGraph client:
|
|
65
|
+
|
|
66
|
+
```typescript
|
|
67
|
+
import { createChatStore } from "@langgraph-js/sdk";
|
|
68
|
+
|
|
69
|
+
export const globalChatStore = createChatStore(
|
|
70
|
+
"agent",
|
|
71
|
+
{
|
|
72
|
+
// Custom LangGraph backend interaction
|
|
73
|
+
apiUrl: "http://localhost:8123",
|
|
74
|
+
// Custom headers for authentication
|
|
75
|
+
defaultHeaders: JSON.parse(localStorage.getItem("code") || "{}"),
|
|
76
|
+
callerOptions: {
|
|
77
|
+
// Example for including cookies
|
|
78
|
+
// fetch(url: string, options: RequestInit) {
|
|
79
|
+
// options.credentials = "include";
|
|
80
|
+
// return fetch(url, options);
|
|
81
|
+
// },
|
|
82
|
+
},
|
|
83
|
+
},
|
|
84
|
+
{
|
|
85
|
+
onInit(client) {
|
|
86
|
+
client.tools.bindTools([]);
|
|
87
|
+
},
|
|
88
|
+
}
|
|
89
|
+
);
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
### React Integration
|
|
93
|
+
|
|
94
|
+
First, install the nanostores React integration:
|
|
95
|
+
|
|
96
|
+
```bash
|
|
97
|
+
pnpm i @nanostores/react
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
Then create a context provider for your chat:
|
|
101
|
+
|
|
102
|
+
```tsx
|
|
103
|
+
import React, { createContext, useContext, useEffect } from "react";
|
|
104
|
+
import { globalChatStore } from "../store"; // Import your store
|
|
105
|
+
import { UnionStore, useUnionStore } from "@langgraph-js/sdk";
|
|
106
|
+
import { useStore } from "@nanostores/react";
|
|
107
|
+
|
|
108
|
+
type ChatContextType = UnionStore<typeof globalChatStore>;
|
|
109
|
+
|
|
110
|
+
const ChatContext = createContext<ChatContextType | undefined>(undefined);
|
|
111
|
+
|
|
112
|
+
export const useChat = () => {
|
|
113
|
+
const context = useContext(ChatContext);
|
|
114
|
+
if (!context) {
|
|
115
|
+
throw new Error("useChat must be used within a ChatProvider");
|
|
116
|
+
}
|
|
117
|
+
return context;
|
|
118
|
+
};
|
|
119
|
+
|
|
120
|
+
export const ChatProvider = ({ children }) => {
|
|
121
|
+
// Use store to ensure React gets reactive state updates
|
|
122
|
+
const store = useUnionStore(globalChatStore, useStore);
|
|
123
|
+
|
|
124
|
+
useEffect(() => {
|
|
125
|
+
// Initialize client
|
|
126
|
+
store.initClient().then(() => {
|
|
127
|
+
// Initialize conversation history
|
|
128
|
+
store.refreshHistoryList();
|
|
129
|
+
});
|
|
130
|
+
}, [store.currentAgent]);
|
|
131
|
+
|
|
132
|
+
return <ChatContext.Provider value={store}>{children}</ChatContext.Provider>;
|
|
133
|
+
};
|
|
134
|
+
```
|
|
135
|
+
|
|
136
|
+
Use it in your components:
|
|
137
|
+
|
|
138
|
+
```tsx
|
|
139
|
+
export const MyChat = () => {
|
|
140
|
+
return (
|
|
141
|
+
<ChatProvider>
|
|
142
|
+
<ChatComp></ChatComp>
|
|
143
|
+
</ChatProvider>
|
|
144
|
+
);
|
|
145
|
+
};
|
|
146
|
+
|
|
147
|
+
function ChatComp() {
|
|
148
|
+
const chat = useChat();
|
|
149
|
+
// Use chat store methods and state here
|
|
150
|
+
}
|
|
151
|
+
```
|
|
152
|
+
|
|
153
|
+
## Documentation
|
|
154
|
+
|
|
155
|
+
For complete documentation, visit our [official docs](https://langgraph-js.netlify.app).
|
|
156
|
+
|
|
157
|
+
## Contributing
|
|
158
|
+
|
|
159
|
+
Contributions are welcome! Please feel free to submit a Pull Request.
|
|
160
|
+
|
|
161
|
+
## License
|
|
162
|
+
|
|
163
|
+
This project is licensed under the Apache-2.0 License.
|
|
@@ -43,6 +43,8 @@ export type RenderMessage = Message & {
|
|
|
43
43
|
spend_time?: number;
|
|
44
44
|
/** 渲染时的唯一 id,聚合而来*/
|
|
45
45
|
unique_id?: string;
|
|
46
|
+
/** 工具调用是否完成 */
|
|
47
|
+
done?: boolean;
|
|
46
48
|
};
|
|
47
49
|
export type SendMessageOptions = {
|
|
48
50
|
extraParams?: Record<string, any>;
|
|
@@ -90,7 +92,7 @@ export declare class LangGraphClient extends Client {
|
|
|
90
92
|
* @zh 初始化 Assistant。
|
|
91
93
|
* @en Initializes the Assistant.
|
|
92
94
|
*/
|
|
93
|
-
initAssistant(agentName
|
|
95
|
+
initAssistant(agentName?: string): Promise<void>;
|
|
94
96
|
/**
|
|
95
97
|
* @zh 创建一个新的 Thread。
|
|
96
98
|
* @en Creates a new Thread.
|
package/dist/LangGraphClient.js
CHANGED
|
@@ -54,9 +54,15 @@ export class LangGraphClient extends Client {
|
|
|
54
54
|
const assistants = await this.listAssistants();
|
|
55
55
|
this.availableAssistants = assistants;
|
|
56
56
|
if (assistants.length > 0) {
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
57
|
+
if (agentName) {
|
|
58
|
+
console.log("agentName", agentName);
|
|
59
|
+
this.currentAssistant = assistants.find((assistant) => assistant.graph_id === agentName) || null;
|
|
60
|
+
if (!this.currentAssistant) {
|
|
61
|
+
throw new Error("Agent not found: " + agentName);
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
else {
|
|
65
|
+
this.currentAssistant = assistants[0];
|
|
60
66
|
}
|
|
61
67
|
}
|
|
62
68
|
else {
|
|
@@ -238,8 +244,10 @@ export class LangGraphClient extends Client {
|
|
|
238
244
|
message.tool_input = typeof assistantToolMessage.args !== "string" ? JSON.stringify(assistantToolMessage.args) : assistantToolMessage.args;
|
|
239
245
|
if (message.additional_kwargs) {
|
|
240
246
|
message.additional_kwargs.done = true;
|
|
247
|
+
message.done = true;
|
|
241
248
|
}
|
|
242
249
|
else {
|
|
250
|
+
message.done = true;
|
|
243
251
|
message.additional_kwargs = {
|
|
244
252
|
done: true,
|
|
245
253
|
};
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { atom } from "nanostores";
|
|
2
2
|
import { LangGraphClient } from "../LangGraphClient.js";
|
|
3
|
+
import { rafDebounce } from "./rafDebounce.js";
|
|
3
4
|
/**
|
|
4
5
|
* @zh 格式化日期对象为时间字符串。
|
|
5
6
|
* @en Formats a Date object into a time string.
|
|
@@ -71,6 +72,9 @@ export const createChatStore = (initClientName, config, context = {}) => {
|
|
|
71
72
|
const showHistory = atom(true);
|
|
72
73
|
const currentAgent = atom(initClientName);
|
|
73
74
|
const currentChatId = atom(null);
|
|
75
|
+
const updateUI = rafDebounce((newClient) => {
|
|
76
|
+
renderMessages.set(newClient.renderMessage);
|
|
77
|
+
});
|
|
74
78
|
/**
|
|
75
79
|
* @zh 初始化 LangGraph 客户端。
|
|
76
80
|
* @en Initializes the LangGraph client.
|
|
@@ -79,6 +83,7 @@ export const createChatStore = (initClientName, config, context = {}) => {
|
|
|
79
83
|
var _a;
|
|
80
84
|
const newClient = new LangGraphClient(config);
|
|
81
85
|
await newClient.initAssistant(currentAgent.get());
|
|
86
|
+
currentAgent.set(newClient.getCurrentAssistant().graph_id);
|
|
82
87
|
// 不再需要创建,sendMessage 会自动创建
|
|
83
88
|
// await newClient.createThread();
|
|
84
89
|
inChatError.set(null);
|
|
@@ -93,13 +98,12 @@ export const createChatStore = (initClientName, config, context = {}) => {
|
|
|
93
98
|
inChatError.set(event.data);
|
|
94
99
|
}
|
|
95
100
|
// console.log(newClient.renderMessage);
|
|
96
|
-
|
|
101
|
+
updateUI(newClient);
|
|
97
102
|
});
|
|
98
103
|
(_a = context.onInit) === null || _a === void 0 ? void 0 : _a.call(context, newClient);
|
|
99
104
|
newClient.graphState = {};
|
|
100
105
|
client.set(newClient);
|
|
101
106
|
}
|
|
102
|
-
;
|
|
103
107
|
/**
|
|
104
108
|
* @zh 发送消息。
|
|
105
109
|
* @en Sends a message.
|
|
@@ -197,7 +201,9 @@ export const createChatStore = (initClientName, config, context = {}) => {
|
|
|
197
201
|
setCurrentAgent(agent) {
|
|
198
202
|
currentAgent.set(agent);
|
|
199
203
|
return initClient().then(() => {
|
|
200
|
-
|
|
204
|
+
if (showHistory.get()) {
|
|
205
|
+
refreshHistoryList();
|
|
206
|
+
}
|
|
201
207
|
});
|
|
202
208
|
},
|
|
203
209
|
/**
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Creates a debounced function that executes once per animation frame
|
|
3
|
+
* @param callback - The function to debounce
|
|
4
|
+
* @returns A function that executes the callback on the next animation frame
|
|
5
|
+
*/
|
|
6
|
+
export declare function rafDebounce<T extends (...args: any[]) => any>(callback: T): (...args: Parameters<T>) => void;
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Creates a debounced function that executes once per animation frame
|
|
3
|
+
* @param callback - The function to debounce
|
|
4
|
+
* @returns A function that executes the callback on the next animation frame
|
|
5
|
+
*/
|
|
6
|
+
export function rafDebounce(callback) {
|
|
7
|
+
let rafId = null;
|
|
8
|
+
let lastArgs = null;
|
|
9
|
+
// Return the debounced function
|
|
10
|
+
return function (...args) {
|
|
11
|
+
// Store the most recent arguments
|
|
12
|
+
lastArgs = args;
|
|
13
|
+
// Cancel any pending animation frame
|
|
14
|
+
if (rafId !== null) {
|
|
15
|
+
cancelAnimationFrame(rafId);
|
|
16
|
+
}
|
|
17
|
+
// Schedule execution on the next animation frame
|
|
18
|
+
rafId = requestAnimationFrame(() => {
|
|
19
|
+
if (lastArgs !== null) {
|
|
20
|
+
callback(...lastArgs);
|
|
21
|
+
lastArgs = null;
|
|
22
|
+
}
|
|
23
|
+
rafId = null;
|
|
24
|
+
});
|
|
25
|
+
};
|
|
26
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@langgraph-js/sdk",
|
|
3
|
-
"version": "1.1.
|
|
3
|
+
"version": "1.1.9",
|
|
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",
|
|
@@ -29,7 +29,7 @@
|
|
|
29
29
|
"url": "https://github.com/KonghaYao/YaoAgent/issues"
|
|
30
30
|
},
|
|
31
31
|
"dependencies": {
|
|
32
|
-
"@langchain/langgraph-sdk": "^0.0.
|
|
32
|
+
"@langchain/langgraph-sdk": "^0.0.76",
|
|
33
33
|
"nanostores": "^1.0.1",
|
|
34
34
|
"zod": "^3.24.3",
|
|
35
35
|
"zod-to-json-schema": "^3.24.3"
|