@vegintech/langchain-react-agent 0.0.1 → 0.0.2
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/README.md +48 -53
- package/dist/index.d.mts +5 -5
- package/dist/index.mjs +3 -3
- package/package.json +16 -16
package/README.md
CHANGED
|
@@ -32,8 +32,8 @@ npm install react react-dom @langchain/react
|
|
|
32
32
|
## 快速开始
|
|
33
33
|
|
|
34
34
|
```tsx
|
|
35
|
-
import { AgentChat } from
|
|
36
|
-
import
|
|
35
|
+
import { AgentChat } from "@vegintech/agentkit";
|
|
36
|
+
import "@vegintech/agentkit/style.css";
|
|
37
37
|
|
|
38
38
|
function App() {
|
|
39
39
|
return (
|
|
@@ -41,7 +41,7 @@ function App() {
|
|
|
41
41
|
apiUrl="http://localhost:8000/api"
|
|
42
42
|
assistantId="my-assistant"
|
|
43
43
|
onThreadIdChange={(threadId) => {
|
|
44
|
-
console.log(
|
|
44
|
+
console.log("New thread:", threadId);
|
|
45
45
|
}}
|
|
46
46
|
/>
|
|
47
47
|
);
|
|
@@ -55,39 +55,36 @@ function App() {
|
|
|
55
55
|
核心聊天组件,封装了消息列表、输入框和工具管理功能。
|
|
56
56
|
|
|
57
57
|
```tsx
|
|
58
|
-
import { AgentChat } from
|
|
58
|
+
import { AgentChat } from "@vegintech/agentkit";
|
|
59
59
|
|
|
60
60
|
<AgentChat
|
|
61
61
|
// 连接配置
|
|
62
62
|
apiUrl="https://api.example.com"
|
|
63
63
|
assistantId="your-assistant-id"
|
|
64
|
-
threadId={threadId}
|
|
65
|
-
headers={{ Authorization:
|
|
66
|
-
onThreadIdChange={(id) => setThreadId(id)}
|
|
67
|
-
|
|
64
|
+
threadId={threadId} // 可选:会话 ID
|
|
65
|
+
headers={{ Authorization: "Bearer token" }} // 可选:自定义请求头
|
|
66
|
+
onThreadIdChange={(id) => setThreadId(id)} // 可选:会话 ID 变化回调
|
|
68
67
|
// 工具配置
|
|
69
|
-
tools={[frontendTool, backendTool]}
|
|
70
|
-
|
|
68
|
+
tools={[frontendTool, backendTool]} // 可选:工具定义数组
|
|
71
69
|
// 上下文
|
|
72
|
-
contexts={[{ description:
|
|
73
|
-
|
|
70
|
+
contexts={[{ description: "用户信息", value: "用户ID: 123" }]} // 可选
|
|
74
71
|
// 消息渲染配置
|
|
75
72
|
messageConfig={{
|
|
76
|
-
components: {
|
|
77
|
-
|
|
73
|
+
components: {
|
|
74
|
+
/* 自定义 Markdown 组件 */
|
|
75
|
+
},
|
|
76
|
+
allowedTags: { div: ["class"] },
|
|
78
77
|
}}
|
|
79
|
-
|
|
80
78
|
// 输入框配置
|
|
81
79
|
inputConfig={{
|
|
82
|
-
placeholder:
|
|
80
|
+
placeholder: "请输入消息...",
|
|
83
81
|
onPreSend: (params) => ({
|
|
84
|
-
messages: [{ type:
|
|
82
|
+
messages: [{ type: "human", content: params.message }],
|
|
85
83
|
}),
|
|
86
84
|
}}
|
|
87
|
-
|
|
88
85
|
// 错误处理
|
|
89
86
|
onError={(error) => console.error(error)}
|
|
90
|
-
|
|
87
|
+
/>;
|
|
91
88
|
```
|
|
92
89
|
|
|
93
90
|
## 工具定义
|
|
@@ -97,27 +94,25 @@ import { AgentChat } from '@vegintech/agentkit';
|
|
|
97
94
|
前端工具在浏览器端执行,适用于需要访问浏览器 API 或不需要后端处理的场景。
|
|
98
95
|
|
|
99
96
|
```tsx
|
|
100
|
-
import type { FrontendTool } from
|
|
97
|
+
import type { FrontendTool } from "@vegintech/agentkit";
|
|
101
98
|
|
|
102
99
|
const alertTool: FrontendTool = {
|
|
103
|
-
type:
|
|
104
|
-
name:
|
|
105
|
-
description:
|
|
100
|
+
type: "frontend",
|
|
101
|
+
name: "alert",
|
|
102
|
+
description: "显示一个警告弹窗",
|
|
106
103
|
parameters: {
|
|
107
|
-
type:
|
|
104
|
+
type: "object",
|
|
108
105
|
properties: {
|
|
109
|
-
message: { type:
|
|
106
|
+
message: { type: "string", description: "警告消息内容" },
|
|
110
107
|
},
|
|
111
|
-
required: [
|
|
108
|
+
required: ["message"],
|
|
112
109
|
},
|
|
113
110
|
execute: async (args) => {
|
|
114
111
|
alert(args.message);
|
|
115
112
|
return { success: true };
|
|
116
113
|
},
|
|
117
114
|
render: (props) => (
|
|
118
|
-
<div className="tool-alert">
|
|
119
|
-
{props.status === 'running' ? '显示弹窗中...' : '弹窗已显示'}
|
|
120
|
-
</div>
|
|
115
|
+
<div className="tool-alert">{props.status === "running" ? "显示弹窗中..." : "弹窗已显示"}</div>
|
|
121
116
|
),
|
|
122
117
|
};
|
|
123
118
|
```
|
|
@@ -127,24 +122,24 @@ const alertTool: FrontendTool = {
|
|
|
127
122
|
后端工具由 LangChain Agent 在后端执行,适用于需要访问数据库、API 等后端资源的场景。
|
|
128
123
|
|
|
129
124
|
```tsx
|
|
130
|
-
import type { BackendTool } from
|
|
125
|
+
import type { BackendTool } from "@vegintech/agentkit";
|
|
131
126
|
|
|
132
127
|
const searchTool: BackendTool = {
|
|
133
|
-
type:
|
|
134
|
-
name:
|
|
135
|
-
description:
|
|
128
|
+
type: "backend",
|
|
129
|
+
name: "search",
|
|
130
|
+
description: "搜索知识库",
|
|
136
131
|
parameters: {
|
|
137
|
-
type:
|
|
132
|
+
type: "object",
|
|
138
133
|
properties: {
|
|
139
|
-
query: { type:
|
|
134
|
+
query: { type: "string", description: "搜索关键词" },
|
|
140
135
|
},
|
|
141
|
-
required: [
|
|
136
|
+
required: ["query"],
|
|
142
137
|
},
|
|
143
138
|
render: (props) => (
|
|
144
139
|
<div className="tool-search">
|
|
145
|
-
{props.status ===
|
|
146
|
-
{props.status ===
|
|
147
|
-
{props.status ===
|
|
140
|
+
{props.status === "pending" && "等待执行..."}
|
|
141
|
+
{props.status === "running" && `搜索: ${props.arguments.query}`}
|
|
142
|
+
{props.status === "success" && `找到 ${props.result?.count || 0} 条结果`}
|
|
148
143
|
</div>
|
|
149
144
|
),
|
|
150
145
|
};
|
|
@@ -154,19 +149,19 @@ const searchTool: BackendTool = {
|
|
|
154
149
|
|
|
155
150
|
### AgentChat Props
|
|
156
151
|
|
|
157
|
-
| 属性
|
|
158
|
-
|
|
159
|
-
| apiUrl
|
|
160
|
-
| assistantId
|
|
161
|
-
| threadId
|
|
162
|
-
| headers
|
|
163
|
-
| onThreadIdChange | (threadId: string) => void
|
|
164
|
-
| className
|
|
165
|
-
| tools
|
|
166
|
-
| contexts
|
|
167
|
-
| messageConfig
|
|
168
|
-
| inputConfig
|
|
169
|
-
| onError
|
|
152
|
+
| 属性 | 类型 | 必填 | 说明 |
|
|
153
|
+
| ---------------- | ------------------------------------------- | ---- | ------------------------- |
|
|
154
|
+
| apiUrl | string | 是 | LangGraph API 地址 |
|
|
155
|
+
| assistantId | string | 是 | 助手 ID |
|
|
156
|
+
| threadId | string | 否 | 会话 ID,不传则创建新会话 |
|
|
157
|
+
| headers | Record<string, string \| undefined \| null> | 否 | 自定义请求头 |
|
|
158
|
+
| onThreadIdChange | (threadId: string) => void | 否 | 会话 ID 变化回调 |
|
|
159
|
+
| className | string | 否 | 自定义 CSS 类名 |
|
|
160
|
+
| tools | ToolDefinition[] | 否 | 工具定义数组 |
|
|
161
|
+
| contexts | ContextItem[] | 否 | 上下文信息数组 |
|
|
162
|
+
| messageConfig | MessageConfig | 否 | 消息渲染配置 |
|
|
163
|
+
| inputConfig | InputConfig | 否 | 输入框配置 |
|
|
164
|
+
| onError | (error: Error) => void | 否 | 错误处理回调 |
|
|
170
165
|
|
|
171
166
|
### 类型定义
|
|
172
167
|
|
|
@@ -181,7 +176,7 @@ import type {
|
|
|
181
176
|
ContextItem,
|
|
182
177
|
MessageConfig,
|
|
183
178
|
InputConfig,
|
|
184
|
-
} from
|
|
179
|
+
} from "@vegintech/agentkit";
|
|
185
180
|
```
|
|
186
181
|
|
|
187
182
|
## 开发
|
package/dist/index.d.mts
CHANGED
|
@@ -13,7 +13,7 @@ interface ContextItem {
|
|
|
13
13
|
value: string;
|
|
14
14
|
}
|
|
15
15
|
/** Sender onSubmit 插槽配置类型 */
|
|
16
|
-
type SenderSlotConfig = SenderProps[
|
|
16
|
+
type SenderSlotConfig = SenderProps["slotConfig"];
|
|
17
17
|
/** Sender onSubmit 完整参数 */
|
|
18
18
|
interface SenderSubmitParams {
|
|
19
19
|
message: string;
|
|
@@ -66,7 +66,7 @@ interface ToolParameterSchema {
|
|
|
66
66
|
[key: string]: unknown;
|
|
67
67
|
}
|
|
68
68
|
/** 消息类型,用于区分用户和助手消息 */
|
|
69
|
-
type MessageType =
|
|
69
|
+
type MessageType = "human" | "ai" | "system" | "tool" | "function";
|
|
70
70
|
/** 扩展的消息类型,确保包含 id 和必要的字段 */
|
|
71
71
|
interface ChatMessage {
|
|
72
72
|
id: string;
|
|
@@ -92,7 +92,7 @@ interface ToolCallInput {
|
|
|
92
92
|
result?: unknown;
|
|
93
93
|
}
|
|
94
94
|
/** 工具执行状态 */
|
|
95
|
-
type ToolExecutionStatus =
|
|
95
|
+
type ToolExecutionStatus = "pending" | "running" | "success" | "error";
|
|
96
96
|
/** 工具执行结果 */
|
|
97
97
|
interface ToolExecutionRecord {
|
|
98
98
|
callId: string;
|
|
@@ -121,13 +121,13 @@ interface ToolBase<TArgs = Record<string, unknown>> {
|
|
|
121
121
|
/** 前端工具:必须有 execute */
|
|
122
122
|
interface FrontendTool<TArgs = Record<string, unknown>> extends ToolBase<TArgs> {
|
|
123
123
|
/** 前端工具必须有 execute */
|
|
124
|
-
type:
|
|
124
|
+
type: "frontend";
|
|
125
125
|
execute: (args: TArgs) => Promise<unknown> | unknown;
|
|
126
126
|
}
|
|
127
127
|
/** 后端工具:不能有 execute */
|
|
128
128
|
interface BackendTool<TArgs = Record<string, unknown>> extends ToolBase<TArgs> {
|
|
129
129
|
/** 后端工具由 Agent 执行 */
|
|
130
|
-
type:
|
|
130
|
+
type: "backend";
|
|
131
131
|
execute?: never;
|
|
132
132
|
}
|
|
133
133
|
/** 工具定义联合类型 */
|
package/dist/index.mjs
CHANGED
|
@@ -162,7 +162,7 @@ ChatInput.displayName = "ChatInput";
|
|
|
162
162
|
//#region src/ToolCallRenderer.tsx
|
|
163
163
|
/**
|
|
164
164
|
* 工具调用渲染组件
|
|
165
|
-
*
|
|
165
|
+
*
|
|
166
166
|
* 职责:
|
|
167
167
|
* 1. 渲染单个工具调用的状态和结果
|
|
168
168
|
* 2. 支持自定义 render 函数
|
|
@@ -212,12 +212,12 @@ const DefaultToolCallRenderer = ({ record }) => {
|
|
|
212
212
|
//#region src/ToolManager.tsx
|
|
213
213
|
/**
|
|
214
214
|
* 工具管理器组件
|
|
215
|
-
*
|
|
215
|
+
*
|
|
216
216
|
* 职责:
|
|
217
217
|
* 1. 管理工具执行状态
|
|
218
218
|
* 2. 自动执行前端工具(避免重复执行)
|
|
219
219
|
* 3. 通知外部执行状态变化
|
|
220
|
-
*
|
|
220
|
+
*
|
|
221
221
|
* 这是一个无 UI 组件,只负责逻辑处理
|
|
222
222
|
*/
|
|
223
223
|
const ToolManager = ({ tools, toolCalls, isLoading = false, onExecutionChange, onToolResult, completedToolResults }) => {
|
package/package.json
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@vegintech/langchain-react-agent",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.2",
|
|
4
4
|
"description": "LangChain Agent UI component library for React",
|
|
5
5
|
"license": "MIT",
|
|
6
|
-
"sideEffects": false,
|
|
7
6
|
"files": [
|
|
8
7
|
"dist"
|
|
9
8
|
],
|
|
10
9
|
"type": "module",
|
|
10
|
+
"sideEffects": false,
|
|
11
11
|
"exports": {
|
|
12
12
|
".": "./dist/index.mjs",
|
|
13
13
|
"./package.json": "./package.json"
|
|
@@ -22,26 +22,26 @@
|
|
|
22
22
|
"check": "vp check",
|
|
23
23
|
"prepublishOnly": "vp run build"
|
|
24
24
|
},
|
|
25
|
-
"
|
|
26
|
-
"
|
|
27
|
-
"
|
|
25
|
+
"dependencies": {
|
|
26
|
+
"@ant-design/x": "^2.4.0",
|
|
27
|
+
"@langchain/core": "^1.1.36",
|
|
28
|
+
"@langchain/react": "^0.2.3",
|
|
29
|
+
"streamdown": "^2.5.0"
|
|
28
30
|
},
|
|
29
31
|
"devDependencies": {
|
|
30
32
|
"@types/node": "^25.5.0",
|
|
31
|
-
"@types/react": "^19.
|
|
32
|
-
"@types/react-dom": "^19.
|
|
33
|
+
"@types/react": "^19.2.14",
|
|
34
|
+
"@types/react-dom": "^19.2.3",
|
|
33
35
|
"@typescript/native-preview": "7.0.0-dev.20260316.1",
|
|
34
|
-
"@vitejs/plugin-react": "
|
|
36
|
+
"@vitejs/plugin-react": "^6.0.1",
|
|
35
37
|
"bumpp": "^11.0.1",
|
|
36
|
-
"react": "^19.
|
|
37
|
-
"react-dom": "^19.
|
|
38
|
+
"react": "^19.2.4",
|
|
39
|
+
"react-dom": "^19.2.4",
|
|
38
40
|
"typescript": "^5.9.3",
|
|
39
|
-
"vite-plus": "^0.1.
|
|
41
|
+
"vite-plus": "^0.1.14"
|
|
40
42
|
},
|
|
41
|
-
"
|
|
42
|
-
"
|
|
43
|
-
"
|
|
44
|
-
"@langchain/react": "catalog:",
|
|
45
|
-
"streamdown": "catalog:"
|
|
43
|
+
"peerDependencies": {
|
|
44
|
+
"react": ">=18.0.0",
|
|
45
|
+
"react-dom": ">=18.0.0"
|
|
46
46
|
}
|
|
47
47
|
}
|