agent-scene-toolkit 0.1.0 → 0.1.1
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 +95 -40
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,17 +1,14 @@
|
|
|
1
1
|
# agent-scene-toolkit
|
|
2
2
|
|
|
3
|
-
Lightweight Agent orchestration library built on LangChain
|
|
3
|
+
Lightweight Agent orchestration library built on LangChain.
|
|
4
4
|
|
|
5
5
|
> **3 分钟上手**:定义 Profile → 定义 ToolKit → 创建 Agent → 对话。
|
|
6
6
|
|
|
7
|
-
##
|
|
7
|
+
## 核心概念
|
|
8
8
|
|
|
9
|
-
- **ToolKit
|
|
10
|
-
- **
|
|
11
|
-
- **Scene
|
|
12
|
-
- **单/多 Agent 自动切换**:配置 `supervisor` 即启用多 Agent 协作
|
|
13
|
-
- **标准化 SSE 事件流**:`text` / `tool_start` / `tool_end` / `handoff` / `agent` / `error` / `done`
|
|
14
|
-
- **Express 一行集成**:`app.post('/chat', agent.handleRequest())`
|
|
9
|
+
- **ToolKit**:静态能力包,按领域分组的工具集 + 使用策略 Prompt
|
|
10
|
+
- **AgentProfile**:角色身份,只需定义 name + systemPrompt + model
|
|
11
|
+
- **Scene**:运行时场景,注入动态上下文 + 决定当前可用的 ToolKit
|
|
15
12
|
|
|
16
13
|
## 📦 Install
|
|
17
14
|
|
|
@@ -19,7 +16,7 @@ Lightweight Agent orchestration library built on LangChain, with unified SSE str
|
|
|
19
16
|
npm install agent-scene-toolkit @langchain/core @langchain/langgraph @langchain/openai langchain express
|
|
20
17
|
```
|
|
21
18
|
|
|
22
|
-
##
|
|
19
|
+
## 快速开始 — 最小示例
|
|
23
20
|
|
|
24
21
|
```typescript
|
|
25
22
|
import { createAgent, defineProfile, defineToolKit, defineScene } from 'agent-scene-toolkit'
|
|
@@ -50,7 +47,7 @@ const agent = createAgent({
|
|
|
50
47
|
toolkits: [canvasToolKit],
|
|
51
48
|
agents: [director],
|
|
52
49
|
scene: timelineScene,
|
|
53
|
-
llm: { baseURL: 'https://api.
|
|
50
|
+
llm: { baseURL: 'https://api.bltcy.ai', apiKey: 'sk-xxx' },
|
|
54
51
|
})
|
|
55
52
|
|
|
56
53
|
// 5. 发起对话
|
|
@@ -59,58 +56,116 @@ for await (const event of agent.chat({ message: '你好', threadId: 'thread-001'
|
|
|
59
56
|
}
|
|
60
57
|
```
|
|
61
58
|
|
|
62
|
-
##
|
|
59
|
+
## 完整配置
|
|
63
60
|
|
|
64
|
-
|
|
61
|
+
展示所有可选字段:记忆持久化、滑动窗口、LangFuse 观测、Scene 生命周期回调、动态运行时上下文。
|
|
65
62
|
|
|
66
63
|
```typescript
|
|
64
|
+
import { createAgent, defineProfile, defineToolKit, defineScene } from 'agent-scene-toolkit'
|
|
65
|
+
import { MemorySaver } from '@langchain/langgraph'
|
|
66
|
+
import { CallbackHandler } from 'langfuse-langchain'
|
|
67
|
+
|
|
68
|
+
// LangFuse 观测回调(也可通过环境变量 LANGFUSE_SECRET_KEY / LANGFUSE_PUBLIC_KEY / LANGFUSE_HOST 配置)
|
|
69
|
+
const langfuseHandler = new CallbackHandler({
|
|
70
|
+
secretKey: 'sk-lf-xxx',
|
|
71
|
+
publicKey: 'pk-lf-xxx',
|
|
72
|
+
baseUrl: 'https://langfuse.your-domain.com', // 自部署地址
|
|
73
|
+
})
|
|
74
|
+
|
|
75
|
+
const agent = createAgent({
|
|
76
|
+
toolkits: [canvasToolKit, aiToolKit],
|
|
77
|
+
agents: [director],
|
|
78
|
+
scene: defineScene({
|
|
79
|
+
name: 'timeline-editing',
|
|
80
|
+
toolkits: ['canvas', 'ai'],
|
|
81
|
+
prompt: (ctx) => `用户在时间线编辑器,视频时长: ${ctx.duration}秒`,
|
|
82
|
+
onToolEnd: (toolName, result) => { // 工具调用完成回调
|
|
83
|
+
if (toolName === 'bindTrack') refreshTimeline()
|
|
84
|
+
},
|
|
85
|
+
}),
|
|
86
|
+
checkpointer: new MemorySaver(), // 记忆持久化(生产环境用 PostgresSaver)
|
|
87
|
+
maxMessages: 50, // 滑动窗口大小(默认 50)
|
|
88
|
+
callbacks: [langfuseHandler], // 透传给 LLM + LangGraph,追踪完整执行链路
|
|
89
|
+
llm: { baseURL: 'https://api.bltcy.ai', apiKey: 'sk-xxx' },
|
|
90
|
+
})
|
|
91
|
+
|
|
92
|
+
// 传入 sceneContext 注入动态运行时数据 → scene.prompt(ctx)
|
|
93
|
+
for await (const event of agent.chat({
|
|
94
|
+
message: '帮我调整第3秒的转场',
|
|
95
|
+
threadId: 'thread-001',
|
|
96
|
+
sceneContext: { duration: 30, currentTime: 3 },
|
|
97
|
+
})) {
|
|
98
|
+
switch (event.type) {
|
|
99
|
+
case 'text': process.stdout.write(event.content); break
|
|
100
|
+
case 'tool_start': console.log(`🔧 调用 ${event.toolName}`); break
|
|
101
|
+
case 'tool_end': console.log(`✅ ${event.toolName}`, event.output); break
|
|
102
|
+
case 'error': console.error(`❌ ${event.message}`); break
|
|
103
|
+
case 'done': console.log('\n--- 结束 ---'); break
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
```
|
|
107
|
+
|
|
108
|
+
## 多 Agent 模式
|
|
109
|
+
|
|
110
|
+
配置 `supervisor` 后自动启用 Supervisor 策略,Supervisor 根据任务自动 handoff 给合适的 Worker。
|
|
111
|
+
|
|
112
|
+
```typescript
|
|
113
|
+
const director = defineProfile({
|
|
114
|
+
name: '导演',
|
|
115
|
+
systemPrompt: '你是一位视频导演,负责统筹任务分派...',
|
|
116
|
+
model: 'gpt-4o',
|
|
117
|
+
})
|
|
118
|
+
const screenwriter = defineProfile({
|
|
119
|
+
name: '编剧',
|
|
120
|
+
systemPrompt: '你是一位编剧,擅长剧本创作...',
|
|
121
|
+
model: 'gpt-4o-mini',
|
|
122
|
+
})
|
|
123
|
+
|
|
67
124
|
const agent = createAgent({
|
|
68
125
|
toolkits: [canvasToolKit, aiToolKit],
|
|
69
126
|
agents: [director, screenwriter],
|
|
70
|
-
supervisor: '导演',
|
|
71
|
-
llm: { baseURL: 'https://api.
|
|
127
|
+
supervisor: '导演', // ← 指定 Supervisor,自动启用多 Agent
|
|
128
|
+
llm: { baseURL: 'https://api.bltcy.ai', apiKey: 'sk-xxx' },
|
|
72
129
|
})
|
|
130
|
+
|
|
131
|
+
for await (const event of agent.chat({ message: '写一个30秒的广告脚本', threadId: 'thread-002' })) {
|
|
132
|
+
if (event.type === 'agent') console.log(`🎭 ${event.name} 正在回答`)
|
|
133
|
+
if (event.type === 'handoff') console.log(`🔀 ${event.from} → ${event.to}`)
|
|
134
|
+
if (event.type === 'text') process.stdout.write(event.content)
|
|
135
|
+
}
|
|
73
136
|
```
|
|
74
137
|
|
|
75
|
-
##
|
|
138
|
+
## Express 集成
|
|
76
139
|
|
|
77
140
|
```typescript
|
|
78
141
|
import express from 'express'
|
|
79
142
|
|
|
80
143
|
const app = express()
|
|
81
144
|
app.use(express.json())
|
|
145
|
+
|
|
146
|
+
// 一行挂载 SSE 路由
|
|
82
147
|
app.post('/chat', agent.handleRequest())
|
|
148
|
+
|
|
149
|
+
// 请求体:{ message: string, threadId: string, sceneContext?: Record<string, any> }
|
|
150
|
+
// 响应:SSE 事件流(text/event-stream)
|
|
151
|
+
// data: {"type":"agent","name":"导演"}
|
|
152
|
+
// data: {"type":"text","content":"我来帮你调整"}
|
|
153
|
+
// data: {"type":"tool_start","toolName":"bindTrack","input":{"trackId":"t-01"}}
|
|
154
|
+
// data: {"type":"tool_end","toolName":"bindTrack","output":{"success":true}}
|
|
155
|
+
// data: {"type":"done"}
|
|
83
156
|
```
|
|
84
157
|
|
|
85
158
|
## 📡 SSE Event Protocol
|
|
86
159
|
|
|
87
160
|
| Event | Trigger | Payload |
|
|
88
161
|
|-------|---------|---------|
|
|
89
|
-
| `text` | LLM
|
|
90
|
-
| `tool_start` |
|
|
91
|
-
| `tool_end` |
|
|
92
|
-
| `handoff` | Agent
|
|
93
|
-
| `agent` |
|
|
94
|
-
| `error` |
|
|
95
|
-
| `done` |
|
|
96
|
-
|
|
97
|
-
## ⚙️ Full Configuration
|
|
98
|
-
|
|
99
|
-
```typescript
|
|
100
|
-
const agent = createAgent({
|
|
101
|
-
toolkits: [canvasToolKit, aiToolKit], // Global toolkit pool
|
|
102
|
-
agents: [director], // Agent profiles
|
|
103
|
-
supervisor: '导演', // Optional: enables multi-agent
|
|
104
|
-
scene: timelineScene, // Optional: runtime context + tool filtering
|
|
105
|
-
checkpointer: new MemorySaver(), // Optional: memory persistence
|
|
106
|
-
maxMessages: 50, // Optional: sliding window size
|
|
107
|
-
callbacks: [langfuseHandler], // Optional: LangChain callbacks (e.g. LangFuse)
|
|
108
|
-
llm: { // Optional: OpenAI-compatible gateway
|
|
109
|
-
baseURL: 'https://api.openai.com/v1',
|
|
110
|
-
apiKey: 'sk-xxx',
|
|
111
|
-
},
|
|
112
|
-
})
|
|
113
|
-
```
|
|
162
|
+
| `text` | LLM 输出文本 token | `{ content: string }` |
|
|
163
|
+
| `tool_start` | 工具调用开始 | `{ toolName: string, input: Record<string, any> }` |
|
|
164
|
+
| `tool_end` | 工具调用结束 | `{ toolName: string, output: any }` |
|
|
165
|
+
| `handoff` | Agent 切换(多 Agent) | `{ from: string, to: string }` |
|
|
166
|
+
| `agent` | 当前回答的 Agent 身份 | `{ name: string }` |
|
|
167
|
+
| `error` | 执行出错 | `{ message: string }` |
|
|
168
|
+
| `done` | 流结束 | `{}` |
|
|
114
169
|
|
|
115
170
|
## 📖 API Documentation
|
|
116
171
|
|