@cloudbase/agent-adapter-langgraph 1.0.1-alpha.21 → 1.0.1-alpha.24
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 +121 -0
- package/dist/index.d.mts +69 -1
- package/dist/index.d.ts +69 -1
- package/dist/index.js +406 -78
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +386 -65
- package/dist/index.mjs.map +1 -1
- package/dist/{langchain-5EZUSOX3.mjs → langchain-YGANX565.mjs} +23 -15
- package/dist/langchain-YGANX565.mjs.map +1 -0
- package/package.json +14 -8
- package/dist/langchain-5EZUSOX3.mjs.map +0 -1
package/README.md
CHANGED
|
@@ -121,10 +121,130 @@ function shouldContinue(state: ClientState): "tools" | "end" {
|
|
|
121
121
|
}
|
|
122
122
|
```
|
|
123
123
|
|
|
124
|
+
## CloudBaseSaver
|
|
125
|
+
|
|
126
|
+
`CloudBaseSaver` 是基于腾讯云开发(CloudBase)的 LangGraph 检查点存储实现,支持多租户隔离。
|
|
127
|
+
|
|
128
|
+
### 安装依赖
|
|
129
|
+
|
|
130
|
+
```bash
|
|
131
|
+
npm install @cloudbase/node-sdk
|
|
132
|
+
```
|
|
133
|
+
|
|
134
|
+
### 基本用法
|
|
135
|
+
|
|
136
|
+
```typescript
|
|
137
|
+
import { CloudBaseSaver } from "@cloudbase/agent-adapter-langgraph";
|
|
138
|
+
import cloudbase from "@cloudbase/node-sdk";
|
|
139
|
+
|
|
140
|
+
// 初始化 CloudBase
|
|
141
|
+
const app = cloudbase.init({
|
|
142
|
+
env: process.env.CLOUDBASE_ENV_ID,
|
|
143
|
+
secretId: process.env.CLOUDBASE_SECRET_ID,
|
|
144
|
+
secretKey: process.env.CLOUDBASE_SECRET_KEY,
|
|
145
|
+
});
|
|
146
|
+
|
|
147
|
+
const db = app.database();
|
|
148
|
+
|
|
149
|
+
// 创建检查点存储
|
|
150
|
+
const checkpointer = new CloudBaseSaver({
|
|
151
|
+
db,
|
|
152
|
+
userId: "user-123", // 用于多租户隔离
|
|
153
|
+
agentId: "my-agent", // 可选,用于区分不同 Agent,默认 "default"
|
|
154
|
+
checkpointsCollection: "checkpoints", // 可选,默认 "checkpoints"
|
|
155
|
+
writesCollection: "checkpoint_writes", // 可选,默认 "checkpoint_writes"
|
|
156
|
+
});
|
|
157
|
+
|
|
158
|
+
// 首次使用前,调用 setup() 创建数据库集合(可选,也可手动在控制台创建)
|
|
159
|
+
await checkpointer.setup();
|
|
160
|
+
|
|
161
|
+
// 在 LangGraph 工作流中使用
|
|
162
|
+
const compiledWorkflow = workflow.compile({ checkpointer });
|
|
163
|
+
```
|
|
164
|
+
|
|
165
|
+
### 配置选项
|
|
166
|
+
|
|
167
|
+
| 参数 | 类型 | 必填 | 默认值 | 说明 |
|
|
168
|
+
|------|------|------|--------|------|
|
|
169
|
+
| `db` | `CloudBaseDb` | ✅ | - | CloudBase 数据库实例,通过 `app.database()` 获取 |
|
|
170
|
+
| `userId` | `string` | ✅ | - | 用户 ID,用于多租户数据隔离 |
|
|
171
|
+
| `agentId` | `string` | ❌ | `"default"` | Agent ID,用于区分同一用户的不同 Agent/工作流 |
|
|
172
|
+
| `checkpointsCollection` | `string` | ❌ | `"checkpoints"` | 存储检查点的集合名称 |
|
|
173
|
+
| `writesCollection` | `string` | ❌ | `"checkpoint_writes"` | 存储待处理写入的集合名称 |
|
|
174
|
+
|
|
175
|
+
### setup() 方法
|
|
176
|
+
|
|
177
|
+
`setup()` 方法用于自动创建所需的数据库集合。该方法是幂等的,可以安全地多次调用:
|
|
178
|
+
|
|
179
|
+
```typescript
|
|
180
|
+
// 在应用启动时调用一次即可
|
|
181
|
+
await checkpointer.setup();
|
|
182
|
+
```
|
|
183
|
+
|
|
184
|
+
如果集合已存在,`setup()` 会静默跳过,不会抛出错误。你也可以选择在 CloudBase 控制台手动创建集合。
|
|
185
|
+
|
|
186
|
+
### 获取 userId
|
|
187
|
+
|
|
188
|
+
在实际应用中,`userId` 通常从请求的认证信息中获取。一种常见方式是解析 JWT token 的 `sub` claim:
|
|
189
|
+
|
|
190
|
+
```typescript
|
|
191
|
+
function getUserIdFromRequest(request: Request): string {
|
|
192
|
+
const authHeader = request.headers.get("Authorization");
|
|
193
|
+
if (!authHeader?.startsWith("Bearer ")) {
|
|
194
|
+
throw new Error("Missing Authorization header");
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
const token = authHeader.slice(7);
|
|
198
|
+
const parts = token.split(".");
|
|
199
|
+
if (parts.length !== 3) {
|
|
200
|
+
throw new Error("Invalid JWT format");
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
// 解码 payload(base64url)
|
|
204
|
+
const payload = parts[1]!;
|
|
205
|
+
const decoded = atob(payload.replace(/-/g, "+").replace(/_/g, "/"));
|
|
206
|
+
const claims = JSON.parse(decoded);
|
|
207
|
+
|
|
208
|
+
if (typeof claims.sub !== "string" || !claims.sub) {
|
|
209
|
+
throw new Error("Invalid JWT: missing 'sub' claim");
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
return claims.sub;
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
// 在 createAgent 中使用
|
|
216
|
+
createExpressRoutes({
|
|
217
|
+
createAgent: async (context) => {
|
|
218
|
+
const userId = getUserIdFromRequest(context.request);
|
|
219
|
+
const checkpointer = new CloudBaseSaver({ db, userId });
|
|
220
|
+
// ...
|
|
221
|
+
},
|
|
222
|
+
express: app,
|
|
223
|
+
});
|
|
224
|
+
```
|
|
225
|
+
|
|
226
|
+
> **注意**:上述 JWT 解析仅做 base64 解码,未验证签名。生产环境建议使用 `jose` 等库进行完整的 JWT 验证。
|
|
227
|
+
|
|
228
|
+
### 创建数据库集合
|
|
229
|
+
|
|
230
|
+
在 CloudBase 控制台创建以下集合:
|
|
231
|
+
|
|
232
|
+
- `checkpoints`(或自定义名称)
|
|
233
|
+
- `checkpoint_writes`(或自定义名称)
|
|
234
|
+
|
|
235
|
+
### 环境变量
|
|
236
|
+
|
|
237
|
+
```bash
|
|
238
|
+
CLOUDBASE_ENV_ID=your-env-id
|
|
239
|
+
CLOUDBASE_SECRET_ID=your-secret-id
|
|
240
|
+
CLOUDBASE_SECRET_KEY=your-secret-key
|
|
241
|
+
```
|
|
242
|
+
|
|
124
243
|
## 依赖
|
|
125
244
|
|
|
126
245
|
- `@langchain/langgraph`:LangGraph 框架
|
|
127
246
|
- `@langchain/core`:LangChain 核心工具
|
|
247
|
+
- `@cloudbase/node-sdk`:腾讯云开发 SDK(peerDependency,使用 CloudBaseSaver 时需安装)
|
|
128
248
|
|
|
129
249
|
## 文档
|
|
130
250
|
|
|
@@ -134,3 +254,4 @@ function shouldContinue(state: ClientState): "tools" | "end" {
|
|
|
134
254
|
|
|
135
255
|
- [AG-UI 协议](https://docs.ag-ui.com)
|
|
136
256
|
- [LangGraph 文档](https://langchain-ai.github.io/langgraph/)
|
|
257
|
+
- [LangGraph Checkpointer 概念](https://langchain-ai.github.io/langgraph/concepts/persistence/) - 了解 checkpointer 的更多用法(如 `getTuple`、`list`、`deleteThread` 等)
|
package/dist/index.d.mts
CHANGED
|
@@ -8,6 +8,8 @@ import { Logger } from '@cloudbase/agent-shared';
|
|
|
8
8
|
export { Logger, createConsoleLogger, noopLogger } from '@cloudbase/agent-shared';
|
|
9
9
|
import { RunnableConfig } from '@langchain/core/runnables';
|
|
10
10
|
import { IMemoryClientOptions, MemoryClient } from '@cloudbase/agent-agents';
|
|
11
|
+
import { CheckpointListOptions as CheckpointListOptions$1, PendingWrite as PendingWrite$1 } from '@langchain/langgraph-checkpoint';
|
|
12
|
+
import { CloudBase } from '@cloudbase/node-sdk';
|
|
11
13
|
|
|
12
14
|
type SDZod = StateDefinition | InteropZodObject;
|
|
13
15
|
type CompiledStateGraph<SD extends SDZod> = ReturnType<StateGraph<SD>["compile"]>;
|
|
@@ -135,6 +137,72 @@ declare class TDAISaver extends BaseCheckpointSaver {
|
|
|
135
137
|
close(): void;
|
|
136
138
|
}
|
|
137
139
|
|
|
140
|
+
/** CloudBase database instance type (from app.database()) */
|
|
141
|
+
type CloudBaseDb = ReturnType<CloudBase["database"]>;
|
|
142
|
+
interface CloudBaseSaverConfig {
|
|
143
|
+
/** CloudBase database instance from app.database() */
|
|
144
|
+
db: CloudBaseDb;
|
|
145
|
+
/** User ID for multi-tenant isolation */
|
|
146
|
+
userId: string;
|
|
147
|
+
/** Agent ID to distinguish different agents/graphs (default: "default") */
|
|
148
|
+
agentId?: string;
|
|
149
|
+
/** Collection name for checkpoints (default: "checkpoints") */
|
|
150
|
+
checkpointsCollection?: string;
|
|
151
|
+
/** Collection name for writes (default: "checkpoint_writes") */
|
|
152
|
+
writesCollection?: string;
|
|
153
|
+
}
|
|
154
|
+
/**
|
|
155
|
+
* CloudBaseSaver - LangGraph checkpoint saver implementation using Tencent CloudBase
|
|
156
|
+
*
|
|
157
|
+
* Storage Strategy:
|
|
158
|
+
* - Uses 2 collections: checkpoints and checkpoint_writes
|
|
159
|
+
* - Data is serialized using BaseCheckpointSaver's serde (base64 encoded)
|
|
160
|
+
* - Multi-tenant isolation via userId field
|
|
161
|
+
*/
|
|
162
|
+
declare class CloudBaseSaver extends BaseCheckpointSaver {
|
|
163
|
+
private db;
|
|
164
|
+
private userId;
|
|
165
|
+
private agentId;
|
|
166
|
+
private checkpointsCollection;
|
|
167
|
+
private writesCollection;
|
|
168
|
+
constructor(config: CloudBaseSaverConfig);
|
|
169
|
+
/**
|
|
170
|
+
* Setup the required collections in CloudBase.
|
|
171
|
+
* Call this once before using the saver. Safe to call multiple times.
|
|
172
|
+
*/
|
|
173
|
+
setup(): Promise<void>;
|
|
174
|
+
/**
|
|
175
|
+
* Serialize data to JSON object using serde.
|
|
176
|
+
* CloudBase is a document database, so we store JSON directly instead of Base64.
|
|
177
|
+
* Binary data (Uint8Array) is not supported - fail early if encountered.
|
|
178
|
+
*/
|
|
179
|
+
private serialize;
|
|
180
|
+
/**
|
|
181
|
+
* Deserialize JSON object back to data using serde.
|
|
182
|
+
*/
|
|
183
|
+
private deserialize;
|
|
184
|
+
/**
|
|
185
|
+
* Retrieves a checkpoint from CloudBase based on the provided config.
|
|
186
|
+
*/
|
|
187
|
+
getTuple(config: RunnableConfig): Promise<CheckpointTuple | undefined>;
|
|
188
|
+
/**
|
|
189
|
+
* List checkpoints from CloudBase, ordered by checkpoint_id descending.
|
|
190
|
+
*/
|
|
191
|
+
list(config: RunnableConfig, options?: CheckpointListOptions$1): AsyncGenerator<CheckpointTuple>;
|
|
192
|
+
/**
|
|
193
|
+
* Save a checkpoint to CloudBase.
|
|
194
|
+
*/
|
|
195
|
+
put(config: RunnableConfig, checkpoint: Checkpoint, metadata: CheckpointMetadata): Promise<RunnableConfig>;
|
|
196
|
+
/**
|
|
197
|
+
* Save intermediate writes to CloudBase.
|
|
198
|
+
*/
|
|
199
|
+
putWrites(config: RunnableConfig, writes: PendingWrite$1[], taskId: string): Promise<void>;
|
|
200
|
+
/**
|
|
201
|
+
* Delete all checkpoints and writes for a thread.
|
|
202
|
+
*/
|
|
203
|
+
deleteThread(threadId: string): Promise<void>;
|
|
204
|
+
}
|
|
205
|
+
|
|
138
206
|
/**
|
|
139
207
|
* TDAI Store configuration
|
|
140
208
|
*/
|
|
@@ -296,4 +364,4 @@ declare class TDAIStore extends BaseStore {
|
|
|
296
364
|
}): Promise<Item[]>;
|
|
297
365
|
}
|
|
298
366
|
|
|
299
|
-
export { ClientPropertiesAnnotation, type ClientState, ClientStateAnnotation, LanggraphAgent, TDAISaver, type TDAISaverConfig, TDAIStore, type TDAIStoreConfig };
|
|
367
|
+
export { ClientPropertiesAnnotation, type ClientState, ClientStateAnnotation, CloudBaseSaver, type CloudBaseSaverConfig, LanggraphAgent, TDAISaver, type TDAISaverConfig, TDAIStore, type TDAIStoreConfig };
|
package/dist/index.d.ts
CHANGED
|
@@ -8,6 +8,8 @@ import { Logger } from '@cloudbase/agent-shared';
|
|
|
8
8
|
export { Logger, createConsoleLogger, noopLogger } from '@cloudbase/agent-shared';
|
|
9
9
|
import { RunnableConfig } from '@langchain/core/runnables';
|
|
10
10
|
import { IMemoryClientOptions, MemoryClient } from '@cloudbase/agent-agents';
|
|
11
|
+
import { CheckpointListOptions as CheckpointListOptions$1, PendingWrite as PendingWrite$1 } from '@langchain/langgraph-checkpoint';
|
|
12
|
+
import { CloudBase } from '@cloudbase/node-sdk';
|
|
11
13
|
|
|
12
14
|
type SDZod = StateDefinition | InteropZodObject;
|
|
13
15
|
type CompiledStateGraph<SD extends SDZod> = ReturnType<StateGraph<SD>["compile"]>;
|
|
@@ -135,6 +137,72 @@ declare class TDAISaver extends BaseCheckpointSaver {
|
|
|
135
137
|
close(): void;
|
|
136
138
|
}
|
|
137
139
|
|
|
140
|
+
/** CloudBase database instance type (from app.database()) */
|
|
141
|
+
type CloudBaseDb = ReturnType<CloudBase["database"]>;
|
|
142
|
+
interface CloudBaseSaverConfig {
|
|
143
|
+
/** CloudBase database instance from app.database() */
|
|
144
|
+
db: CloudBaseDb;
|
|
145
|
+
/** User ID for multi-tenant isolation */
|
|
146
|
+
userId: string;
|
|
147
|
+
/** Agent ID to distinguish different agents/graphs (default: "default") */
|
|
148
|
+
agentId?: string;
|
|
149
|
+
/** Collection name for checkpoints (default: "checkpoints") */
|
|
150
|
+
checkpointsCollection?: string;
|
|
151
|
+
/** Collection name for writes (default: "checkpoint_writes") */
|
|
152
|
+
writesCollection?: string;
|
|
153
|
+
}
|
|
154
|
+
/**
|
|
155
|
+
* CloudBaseSaver - LangGraph checkpoint saver implementation using Tencent CloudBase
|
|
156
|
+
*
|
|
157
|
+
* Storage Strategy:
|
|
158
|
+
* - Uses 2 collections: checkpoints and checkpoint_writes
|
|
159
|
+
* - Data is serialized using BaseCheckpointSaver's serde (base64 encoded)
|
|
160
|
+
* - Multi-tenant isolation via userId field
|
|
161
|
+
*/
|
|
162
|
+
declare class CloudBaseSaver extends BaseCheckpointSaver {
|
|
163
|
+
private db;
|
|
164
|
+
private userId;
|
|
165
|
+
private agentId;
|
|
166
|
+
private checkpointsCollection;
|
|
167
|
+
private writesCollection;
|
|
168
|
+
constructor(config: CloudBaseSaverConfig);
|
|
169
|
+
/**
|
|
170
|
+
* Setup the required collections in CloudBase.
|
|
171
|
+
* Call this once before using the saver. Safe to call multiple times.
|
|
172
|
+
*/
|
|
173
|
+
setup(): Promise<void>;
|
|
174
|
+
/**
|
|
175
|
+
* Serialize data to JSON object using serde.
|
|
176
|
+
* CloudBase is a document database, so we store JSON directly instead of Base64.
|
|
177
|
+
* Binary data (Uint8Array) is not supported - fail early if encountered.
|
|
178
|
+
*/
|
|
179
|
+
private serialize;
|
|
180
|
+
/**
|
|
181
|
+
* Deserialize JSON object back to data using serde.
|
|
182
|
+
*/
|
|
183
|
+
private deserialize;
|
|
184
|
+
/**
|
|
185
|
+
* Retrieves a checkpoint from CloudBase based on the provided config.
|
|
186
|
+
*/
|
|
187
|
+
getTuple(config: RunnableConfig): Promise<CheckpointTuple | undefined>;
|
|
188
|
+
/**
|
|
189
|
+
* List checkpoints from CloudBase, ordered by checkpoint_id descending.
|
|
190
|
+
*/
|
|
191
|
+
list(config: RunnableConfig, options?: CheckpointListOptions$1): AsyncGenerator<CheckpointTuple>;
|
|
192
|
+
/**
|
|
193
|
+
* Save a checkpoint to CloudBase.
|
|
194
|
+
*/
|
|
195
|
+
put(config: RunnableConfig, checkpoint: Checkpoint, metadata: CheckpointMetadata): Promise<RunnableConfig>;
|
|
196
|
+
/**
|
|
197
|
+
* Save intermediate writes to CloudBase.
|
|
198
|
+
*/
|
|
199
|
+
putWrites(config: RunnableConfig, writes: PendingWrite$1[], taskId: string): Promise<void>;
|
|
200
|
+
/**
|
|
201
|
+
* Delete all checkpoints and writes for a thread.
|
|
202
|
+
*/
|
|
203
|
+
deleteThread(threadId: string): Promise<void>;
|
|
204
|
+
}
|
|
205
|
+
|
|
138
206
|
/**
|
|
139
207
|
* TDAI Store configuration
|
|
140
208
|
*/
|
|
@@ -296,4 +364,4 @@ declare class TDAIStore extends BaseStore {
|
|
|
296
364
|
}): Promise<Item[]>;
|
|
297
365
|
}
|
|
298
366
|
|
|
299
|
-
export { ClientPropertiesAnnotation, type ClientState, ClientStateAnnotation, LanggraphAgent, TDAISaver, type TDAISaverConfig, TDAIStore, type TDAIStoreConfig };
|
|
367
|
+
export { ClientPropertiesAnnotation, type ClientState, ClientStateAnnotation, CloudBaseSaver, type CloudBaseSaverConfig, LanggraphAgent, TDAISaver, type TDAISaverConfig, TDAIStore, type TDAIStoreConfig };
|