@cloudbase/agent-adapter-langgraph 0.0.12 → 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/README.md CHANGED
@@ -1,125 +1,136 @@
1
1
  # @cloudbase/agent-adapter-langgraph
2
2
 
3
- LangGraph adapter for AG-Kit agents. This package provides integration between AG-Kit and LangGraph framework, enabling you to use LangGraph workflows with AG-Kit's agent infrastructure.
3
+ LangGraph 工作流转换为符合 [AG-UI 协议](https://docs.ag-ui.com) Agent。
4
4
 
5
- ## Installation
5
+ ## 安装
6
6
 
7
7
  ```bash
8
- npm install @cloudbase/agent-agents @cloudbase/agent-adapter-langgraph
8
+ npm install @cloudbase/agent-adapter-langgraph
9
9
  ```
10
10
 
11
- ## Features
11
+ ## 功能
12
12
 
13
- - **LangGraphAgent**: Agent implementation that works with compiled LangGraph workflows
14
- - **TDAISaver**: Checkpoint saver implementation using TDAI Memory
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
- ## Usage
16
+ ## 快速开始
19
17
 
20
- ### Basic Agent Setup
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 { AGKitStateAnnotation, LanggraphAgent } from "@cloudbase/agent-adapter-langgraph";
25
- import { AgentConfig } from "@cloudbase/agent-agents/abstract";
23
+ import { ClientStateAnnotation, LanggraphAgent } from "@cloudbase/agent-adapter-langgraph";
26
24
 
27
- // Create your LangGraph workflow
28
- const workflow = new StateGraph(AGKitStateAnnotation)
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
- // Create the agent
36
- const agent = new LanggraphAgent({
37
- name: "my-langgraph-agent",
38
- description: "A LangGraph agent",
39
- compiledWorkflow,
33
+ // 部署为 HTTP 服务
34
+ run({
35
+ createAgent: () => ({
36
+ agent: new LanggraphAgent({ compiledWorkflow }),
37
+ }),
38
+ port: 9000,
40
39
  });
41
40
  ```
42
41
 
43
- ### Using TDAISaver for Checkpoints
42
+ ## API 参考
44
43
 
45
- ```typescript
46
- import { TDAISaver } from "@cloudbase/agent-adapter-langgraph";
47
- import { MemoryClient } from "@cloudbase/agent-agents";
44
+ ### LanggraphAgent
48
45
 
49
- const memoryClient = new MemoryClient({
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
- const checkpointer = new TDAISaver({
56
- endpoint: "https://api.tdai.com",
57
- apiKey: "your-api-key",
58
- memoryId: "your-memory-id",
59
- });
48
+ ```typescript
49
+ type LanggraphAgentConfig = AgentConfig & {
50
+ compiledWorkflow: CompiledStateGraph; // 编译后的 LangGraph 工作流
51
+ logger?: Logger; // 可选,日志实例
52
+ };
60
53
 
61
- const compiledWorkflow = workflow.compile({
62
- checkpointer,
63
- });
54
+ const agent = new LanggraphAgent(config);
64
55
  ```
65
56
 
66
- ### Using TDAIStore
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
- ```typescript
69
- import { TDAIStore } from "@cloudbase/agent-adapter-langgraph";
70
- import { MemoryClient } from "@cloudbase/agent-agents";
60
+ ### ClientStateAnnotation
71
61
 
72
- const memoryClient = new MemoryClient({
73
- endpoint: "https://api.tdai.com",
74
- apiKey: "your-api-key",
75
- memoryId: "your-memory-id",
76
- });
62
+ 创建 LangGraph 工作流时使用的状态定义,已包含 AG-UI 需要的字段:
77
63
 
78
- const store = new TDAIStore({
79
- memoryClient,
80
- sessionId: "session-123",
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
- ## API Reference
73
+ ## 使用客户端工具
85
74
 
86
- ### LanggraphAgent
75
+ AG-UI 支持**客户端工具**(Client Tools):客户端定义工具,Agent 调用后由客户端执行并返回结果。
76
+
77
+ 适用场景:
78
+ - 需要访问客户端 API(如获取地理位置、访问剪贴板)
79
+ - 需要用户确认的操作(如发送邮件前确认)
80
+ - 需要展示 UI 交互(如让用户选择文件)
87
81
 
88
- Agent class that extends `AbstractAgent` and works with compiled LangGraph workflows.
82
+ ### chatNode 中使用客户端工具
89
83
 
90
- **Constructor:**
91
84
  ```typescript
92
- constructor(config: AgentConfig & { compiledWorkflow: CompiledStateGraph })
93
- ```
85
+ import { ClientState } from "@cloudbase/agent-adapter-langgraph";
94
86
 
95
- ### TDAISaver
87
+ async function chatNode(state: ClientState) {
88
+ const model = new ChatOpenAI({ model: "gpt-4o" });
96
89
 
97
- Checkpoint saver implementation for LangGraph using TDAI Memory.
90
+ // 合并服务端工具和客户端工具
91
+ const modelWithTools = model.bindTools([
92
+ ...serverTools, // 服务端定义的工具
93
+ ...(state.client?.tools || []) // 客户端传来的工具
94
+ ]);
98
95
 
99
- **Constructor:**
100
- ```typescript
101
- constructor(config: TDAISaverConfig)
96
+ const response = await modelWithTools.invoke([...state.messages]);
97
+ return { messages: [response] };
98
+ }
102
99
  ```
103
100
 
104
- ### TDAIStore
101
+ ### 区分服务端工具和客户端工具
105
102
 
106
- Store implementation for LangGraph using TDAI Memory.
103
+ Agent 调用工具时,需要判断是服务端执行还是交给客户端:
107
104
 
108
- **Constructor:**
109
105
  ```typescript
110
- constructor(config: TDAIStoreConfig)
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
- ## Requirements
126
+ - `@langchain/langgraph`:LangGraph 框架
127
+ - `@langchain/core`:LangChain 核心工具
114
128
 
115
- - `@cloudbase/agent-agents`: Core agent functionality
116
- - `@langchain/langgraph`: LangGraph framework
117
- - `@langchain/core`: LangChain core utilities
118
- - `rxjs`: Reactive extensions for JavaScript
129
+ ## 文档
119
130
 
120
- ## Related Resources
131
+ 📚 完整文档请参阅 [云开发 Agent 开发指南](https://docs.cloudbase.net/ai/agent-development/)
121
132
 
122
- - [AG-Kit Documentation](https://docs.agkit.dev)
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(): any;
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(): any;
79
+ clone(): LanggraphAgent;
72
80
  }
73
81
 
74
82
  type PendingWrite = [string, any];