@cloudbase/agent-adapter-langgraph 0.0.11 → 0.0.13
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/CHANGELOG.md +12 -0
- package/README.md +85 -74
- package/dist/index.d.mts +9 -1
- package/dist/index.d.ts +9 -1
- package/dist/index.js +359 -159
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +356 -158
- package/dist/index.mjs.map +1 -1
- package/package.json +4 -3
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,17 @@
|
|
|
1
1
|
# @cloudbase/agent-adapter-langgraph
|
|
2
2
|
|
|
3
|
+
## 1.0.1-alpha.6
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- alpha release 0.1.2-alpha.1
|
|
8
|
+
- Update all public packages to version 0.1.2-alpha.1
|
|
9
|
+
- Trigger automated alpha release workflow
|
|
10
|
+
- Includes latest features and improvements
|
|
11
|
+
|
|
12
|
+
- Updated dependencies
|
|
13
|
+
- @cloudbase/agent-agents@1.0.1-alpha.6
|
|
14
|
+
|
|
3
15
|
## 1.0.1-alpha.5
|
|
4
16
|
|
|
5
17
|
### Patch Changes
|
package/README.md
CHANGED
|
@@ -1,125 +1,136 @@
|
|
|
1
1
|
# @cloudbase/agent-adapter-langgraph
|
|
2
2
|
|
|
3
|
-
LangGraph
|
|
3
|
+
将 LangGraph 工作流转换为符合 [AG-UI 协议](https://docs.ag-ui.com) 的 Agent。
|
|
4
4
|
|
|
5
|
-
##
|
|
5
|
+
## 安装
|
|
6
6
|
|
|
7
7
|
```bash
|
|
8
|
-
npm install @cloudbase/agent-
|
|
8
|
+
npm install @cloudbase/agent-adapter-langgraph
|
|
9
9
|
```
|
|
10
10
|
|
|
11
|
-
##
|
|
11
|
+
## 功能
|
|
12
12
|
|
|
13
|
-
- **
|
|
14
|
-
- **
|
|
15
|
-
- **TDAIStore**: Store implementation for LangGraph using TDAI Memory
|
|
16
|
-
- **AGKitStateAnnotation**: Pre-configured state annotation for AG-Kit integration
|
|
13
|
+
- **LanggraphAgent**:将编译后的 LangGraph 工作流包装为 AG-UI 兼容的 Agent
|
|
14
|
+
- **ClientStateAnnotation**:工作流状态定义,包含 messages 和 client tools
|
|
17
15
|
|
|
18
|
-
##
|
|
16
|
+
## 快速开始
|
|
19
17
|
|
|
20
|
-
###
|
|
18
|
+
### 配合 @cloudbase/agent-server 使用
|
|
21
19
|
|
|
22
20
|
```typescript
|
|
21
|
+
import { run } from "@cloudbase/agent-server";
|
|
23
22
|
import { StateGraph, START, END } from "@langchain/langgraph";
|
|
24
|
-
import {
|
|
25
|
-
import { AgentConfig } from "@cloudbase/agent-agents/abstract";
|
|
23
|
+
import { ClientStateAnnotation, LanggraphAgent } from "@cloudbase/agent-adapter-langgraph";
|
|
26
24
|
|
|
27
|
-
//
|
|
28
|
-
const workflow = new StateGraph(
|
|
25
|
+
// 创建 LangGraph 工作流
|
|
26
|
+
const workflow = new StateGraph(ClientStateAnnotation)
|
|
29
27
|
.addNode("chat_node", chatNode)
|
|
30
28
|
.addEdge(START, "chat_node")
|
|
31
29
|
.addEdge("chat_node", END);
|
|
32
30
|
|
|
33
31
|
const compiledWorkflow = workflow.compile();
|
|
34
32
|
|
|
35
|
-
//
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
33
|
+
// 部署为 HTTP 服务
|
|
34
|
+
run({
|
|
35
|
+
createAgent: () => ({
|
|
36
|
+
agent: new LanggraphAgent({ compiledWorkflow }),
|
|
37
|
+
}),
|
|
38
|
+
port: 9000,
|
|
40
39
|
});
|
|
41
40
|
```
|
|
42
41
|
|
|
43
|
-
|
|
42
|
+
## API 参考
|
|
44
43
|
|
|
45
|
-
|
|
46
|
-
import { TDAISaver } from "@cloudbase/agent-adapter-langgraph";
|
|
47
|
-
import { MemoryClient } from "@cloudbase/agent-agents";
|
|
44
|
+
### LanggraphAgent
|
|
48
45
|
|
|
49
|
-
|
|
50
|
-
endpoint: "https://api.tdai.com",
|
|
51
|
-
apiKey: "your-api-key",
|
|
52
|
-
memoryId: "your-memory-id",
|
|
53
|
-
});
|
|
46
|
+
将编译后的 LangGraph 工作流转换为 AG-UI 兼容的 Agent。
|
|
54
47
|
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
}
|
|
48
|
+
```typescript
|
|
49
|
+
type LanggraphAgentConfig = AgentConfig & {
|
|
50
|
+
compiledWorkflow: CompiledStateGraph; // 编译后的 LangGraph 工作流
|
|
51
|
+
logger?: Logger; // 可选,日志实例
|
|
52
|
+
};
|
|
60
53
|
|
|
61
|
-
const
|
|
62
|
-
checkpointer,
|
|
63
|
-
});
|
|
54
|
+
const agent = new LanggraphAgent(config);
|
|
64
55
|
```
|
|
65
56
|
|
|
66
|
-
|
|
57
|
+
- `AgentConfig`:来自 [AG-UI 协议](https://docs.ag-ui.com)
|
|
58
|
+
- `Logger`:日志接口,详见 [@cloudbase/agent-server 文档](https://www.npmjs.com/package/@cloudbase/agent-server)
|
|
67
59
|
|
|
68
|
-
|
|
69
|
-
import { TDAIStore } from "@cloudbase/agent-adapter-langgraph";
|
|
70
|
-
import { MemoryClient } from "@cloudbase/agent-agents";
|
|
60
|
+
### ClientStateAnnotation
|
|
71
61
|
|
|
72
|
-
|
|
73
|
-
endpoint: "https://api.tdai.com",
|
|
74
|
-
apiKey: "your-api-key",
|
|
75
|
-
memoryId: "your-memory-id",
|
|
76
|
-
});
|
|
62
|
+
创建 LangGraph 工作流时使用的状态定义,已包含 AG-UI 需要的字段:
|
|
77
63
|
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
64
|
+
- `messages`:消息历史
|
|
65
|
+
- `client.tools`:客户端传来的工具列表
|
|
66
|
+
|
|
67
|
+
```typescript
|
|
68
|
+
const workflow = new StateGraph(ClientStateAnnotation)
|
|
69
|
+
.addNode("chat_node", chatNode)
|
|
70
|
+
// ...
|
|
82
71
|
```
|
|
83
72
|
|
|
84
|
-
##
|
|
73
|
+
## 使用客户端工具
|
|
85
74
|
|
|
86
|
-
|
|
75
|
+
AG-UI 支持**客户端工具**(Client Tools):客户端定义工具,Agent 调用后由客户端执行并返回结果。
|
|
76
|
+
|
|
77
|
+
适用场景:
|
|
78
|
+
- 需要访问客户端 API(如获取地理位置、访问剪贴板)
|
|
79
|
+
- 需要用户确认的操作(如发送邮件前确认)
|
|
80
|
+
- 需要展示 UI 交互(如让用户选择文件)
|
|
87
81
|
|
|
88
|
-
|
|
82
|
+
### 在 chatNode 中使用客户端工具
|
|
89
83
|
|
|
90
|
-
**Constructor:**
|
|
91
84
|
```typescript
|
|
92
|
-
|
|
93
|
-
```
|
|
85
|
+
import { ClientState } from "@cloudbase/agent-adapter-langgraph";
|
|
94
86
|
|
|
95
|
-
|
|
87
|
+
async function chatNode(state: ClientState) {
|
|
88
|
+
const model = new ChatOpenAI({ model: "gpt-4o" });
|
|
96
89
|
|
|
97
|
-
|
|
90
|
+
// 合并服务端工具和客户端工具
|
|
91
|
+
const modelWithTools = model.bindTools([
|
|
92
|
+
...serverTools, // 服务端定义的工具
|
|
93
|
+
...(state.client?.tools || []) // 客户端传来的工具
|
|
94
|
+
]);
|
|
98
95
|
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
96
|
+
const response = await modelWithTools.invoke([...state.messages]);
|
|
97
|
+
return { messages: [response] };
|
|
98
|
+
}
|
|
102
99
|
```
|
|
103
100
|
|
|
104
|
-
###
|
|
101
|
+
### 区分服务端工具和客户端工具
|
|
105
102
|
|
|
106
|
-
|
|
103
|
+
当 Agent 调用工具时,需要判断是服务端执行还是交给客户端:
|
|
107
104
|
|
|
108
|
-
**Constructor:**
|
|
109
105
|
```typescript
|
|
110
|
-
|
|
111
|
-
|
|
106
|
+
const serverToolNames = new Set(serverTools.map(t => t.name));
|
|
107
|
+
|
|
108
|
+
function shouldContinue(state: ClientState): "tools" | "end" {
|
|
109
|
+
const lastMessage = state.messages.at(-1) as AIMessage;
|
|
110
|
+
|
|
111
|
+
if (lastMessage.tool_calls?.length) {
|
|
112
|
+
// 如果是服务端工具,继续执行
|
|
113
|
+
const hasServerTool = lastMessage.tool_calls.some(
|
|
114
|
+
tc => serverToolNames.has(tc.name)
|
|
115
|
+
);
|
|
116
|
+
if (hasServerTool) return "tools";
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
// 客户端工具或无工具调用,结束并返回给客户端
|
|
120
|
+
return "end";
|
|
121
|
+
}
|
|
122
|
+
```
|
|
123
|
+
|
|
124
|
+
## 依赖
|
|
112
125
|
|
|
113
|
-
|
|
126
|
+
- `@langchain/langgraph`:LangGraph 框架
|
|
127
|
+
- `@langchain/core`:LangChain 核心工具
|
|
114
128
|
|
|
115
|
-
|
|
116
|
-
- `@langchain/langgraph`: LangGraph framework
|
|
117
|
-
- `@langchain/core`: LangChain core utilities
|
|
118
|
-
- `rxjs`: Reactive extensions for JavaScript
|
|
129
|
+
## 文档
|
|
119
130
|
|
|
120
|
-
|
|
131
|
+
📚 完整文档请参阅 [云开发 Agent 开发指南](https://docs.cloudbase.net/ai/agent-development/)
|
|
121
132
|
|
|
122
|
-
|
|
123
|
-
- [LangGraph Documentation](https://langchain-ai.github.io/langgraph/)
|
|
124
|
-
- [AG-Kit Examples](https://github.com/agkit/agkit/tree/main/typescript-sdk/packages/examples)
|
|
133
|
+
## 相关资源
|
|
125
134
|
|
|
135
|
+
- [AG-UI 协议](https://docs.ag-ui.com)
|
|
136
|
+
- [LangGraph 文档](https://langchain-ai.github.io/langgraph/)
|
package/dist/index.d.mts
CHANGED
|
@@ -4,6 +4,8 @@ import { AnnotationRoot, StateDefinition, StateGraph, BaseCheckpointSaver, Check
|
|
|
4
4
|
import { AbstractAgent, AgentConfig, RunAgentInput, EventType, BaseEvent } from '@ag-ui/client';
|
|
5
5
|
import { Observable, Subscriber } from 'rxjs';
|
|
6
6
|
import { InteropZodObject } from '@langchain/core/utils/types';
|
|
7
|
+
import { Logger } from '@cloudbase/agent-shared';
|
|
8
|
+
export { Logger, createConsoleLogger, noopLogger } from '@cloudbase/agent-shared';
|
|
7
9
|
import { RunnableConfig } from '@langchain/core/runnables';
|
|
8
10
|
import { IMemoryClientOptions, MemoryClient } from '@cloudbase/agent-agents';
|
|
9
11
|
|
|
@@ -59,8 +61,14 @@ declare const ClientStateAnnotation: AnnotationRoot<{
|
|
|
59
61
|
type ClientState = typeof ClientStateAnnotation.State;
|
|
60
62
|
declare class LanggraphAgent extends AbstractAgent {
|
|
61
63
|
compiledWorkflow?: CompiledStateGraph<ClientStateDefinition>;
|
|
64
|
+
private logger;
|
|
62
65
|
constructor(agentConfig: AgentConfig & {
|
|
63
66
|
compiledWorkflow: any;
|
|
67
|
+
/**
|
|
68
|
+
* Logger instance for structured logging.
|
|
69
|
+
* @default noopLogger (silent)
|
|
70
|
+
*/
|
|
71
|
+
logger?: Logger;
|
|
64
72
|
});
|
|
65
73
|
run(input: RunAgentInput): Observable<{
|
|
66
74
|
type: EventType;
|
|
@@ -68,7 +76,7 @@ declare class LanggraphAgent extends AbstractAgent {
|
|
|
68
76
|
rawEvent?: any;
|
|
69
77
|
}>;
|
|
70
78
|
_run(subscriber: Subscriber<BaseEvent>, input: RunAgentInput): Promise<void>;
|
|
71
|
-
clone():
|
|
79
|
+
clone(): LanggraphAgent;
|
|
72
80
|
}
|
|
73
81
|
|
|
74
82
|
type PendingWrite = [string, any];
|
package/dist/index.d.ts
CHANGED
|
@@ -4,6 +4,8 @@ import { AnnotationRoot, StateDefinition, StateGraph, BaseCheckpointSaver, Check
|
|
|
4
4
|
import { AbstractAgent, AgentConfig, RunAgentInput, EventType, BaseEvent } from '@ag-ui/client';
|
|
5
5
|
import { Observable, Subscriber } from 'rxjs';
|
|
6
6
|
import { InteropZodObject } from '@langchain/core/utils/types';
|
|
7
|
+
import { Logger } from '@cloudbase/agent-shared';
|
|
8
|
+
export { Logger, createConsoleLogger, noopLogger } from '@cloudbase/agent-shared';
|
|
7
9
|
import { RunnableConfig } from '@langchain/core/runnables';
|
|
8
10
|
import { IMemoryClientOptions, MemoryClient } from '@cloudbase/agent-agents';
|
|
9
11
|
|
|
@@ -59,8 +61,14 @@ declare const ClientStateAnnotation: AnnotationRoot<{
|
|
|
59
61
|
type ClientState = typeof ClientStateAnnotation.State;
|
|
60
62
|
declare class LanggraphAgent extends AbstractAgent {
|
|
61
63
|
compiledWorkflow?: CompiledStateGraph<ClientStateDefinition>;
|
|
64
|
+
private logger;
|
|
62
65
|
constructor(agentConfig: AgentConfig & {
|
|
63
66
|
compiledWorkflow: any;
|
|
67
|
+
/**
|
|
68
|
+
* Logger instance for structured logging.
|
|
69
|
+
* @default noopLogger (silent)
|
|
70
|
+
*/
|
|
71
|
+
logger?: Logger;
|
|
64
72
|
});
|
|
65
73
|
run(input: RunAgentInput): Observable<{
|
|
66
74
|
type: EventType;
|
|
@@ -68,7 +76,7 @@ declare class LanggraphAgent extends AbstractAgent {
|
|
|
68
76
|
rawEvent?: any;
|
|
69
77
|
}>;
|
|
70
78
|
_run(subscriber: Subscriber<BaseEvent>, input: RunAgentInput): Promise<void>;
|
|
71
|
-
clone():
|
|
79
|
+
clone(): LanggraphAgent;
|
|
72
80
|
}
|
|
73
81
|
|
|
74
82
|
type PendingWrite = [string, any];
|