foliko 2.0.2 → 2.0.3
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 +6 -6
- package/docs/public-api.md +91 -0
- package/docs/system-prompt.md +219 -0
- package/docs/usage.md +6 -6
- package/package.json +1 -1
- package/plugins/ambient/ExplorerLoop.js +1 -0
- package/plugins/ambient/index.js +1 -0
- package/plugins/core/coordinator/index.js +10 -5
- package/plugins/core/default/bootstrap.js +21 -3
- package/plugins/core/default/config.js +12 -3
- package/plugins/core/python-loader/index.js +3 -3
- package/plugins/core/scheduler/index.js +26 -2
- package/plugins/core/skill-manager/index.js +198 -151
- package/plugins/core/sub-agent/index.js +34 -15
- package/plugins/core/think/index.js +1 -0
- package/plugins/core/workflow/index.js +5 -4
- package/plugins/executors/data-splitter/index.js +14 -1
- package/plugins/executors/extension/index.js +51 -37
- package/plugins/executors/python/index.js +3 -3
- package/plugins/executors/shell/index.js +6 -4
- package/plugins/io/web/index.js +2 -1
- package/plugins/memory/index.js +29 -74
- package/plugins/messaging/email/handlers.js +1 -19
- package/plugins/messaging/email/monitor.js +2 -17
- package/plugins/messaging/email/reply.js +2 -1
- package/plugins/messaging/email/utils.js +20 -1
- package/plugins/messaging/feishu/index.js +1 -1
- package/plugins/messaging/qq/index.js +1 -1
- package/plugins/messaging/telegram/index.js +1 -1
- package/plugins/messaging/weixin/index.js +1 -1
- package/plugins/plugin-manager/index.js +1 -33
- package/plugins/tools/index.js +14 -1
- package/skills/plugins/SKILL.md +316 -0
- package/skills/skill-guide/SKILL.md +5 -5
- package/skills/{subagent-guide → subagents}/SKILL.md +1 -1
- package/skills/{workflow-guide → workflows}/SKILL.md +3 -3
- package/skills/{workflow-troubleshooting → workflows/workflow-troubleshooting}/DEBUGGING.md +197 -197
- package/skills/{workflow-troubleshooting → workflows/workflow-troubleshooting}/SKILL.md +391 -391
- package/src/agent/base.js +5 -20
- package/src/agent/index.js +20 -8
- package/src/agent/main.js +100 -23
- package/src/agent/prompt-registry.js +296 -0
- package/src/agent/sub-config.js +1 -78
- package/src/agent/sub.js +19 -24
- package/src/cli/commands/plugin.js +1 -60
- package/src/cli/ui/chat-ui-old.js +1 -1
- package/src/cli/ui/chat-ui.js +1 -1
- package/src/cli/ui/components/agent-mention-provider.js +1 -1
- package/src/common/constants.js +42 -0
- package/src/common/id.js +13 -0
- package/src/common/queue.js +2 -2
- package/src/context/compressor.js +17 -9
- package/src/framework/framework.js +185 -0
- package/src/framework/index.js +1 -2
- package/src/framework/lifecycle.js +102 -1
- package/src/framework/loader.js +1 -55
- package/src/index.js +11 -2
- package/src/plugin/base.js +69 -55
- package/src/plugin/loader.js +1 -57
- package/src/session/entry.js +1 -11
- package/src/storage/manager.js +1 -12
- package/src/tool/executor.js +2 -1
- package/src/tool/router.js +5 -5
- package/src/utils/data-splitter.js +2 -1
- package/src/utils/index.js +150 -0
- package/src/utils/message-validator.js +21 -17
- package/src/utils/plugin-helpers.js +19 -5
- package/subagent.md +2 -2
- package/tests/core/plugin-prompts.test.js +219 -0
- package/tests/core/prompt-registry.test.js +209 -0
- package/src/cli/utils/plugin-config.js +0 -50
- package/src/config/plugin-config.js +0 -50
- /package/skills/{ambient-agent → ambient}/SKILL.md +0 -0
- /package/skills/{foliko-dev → foliko}/AGENTS.md +0 -0
- /package/skills/{foliko-dev → foliko}/SKILL.md +0 -0
- /package/skills/{mcp-usage → mcp}/SKILL.md +0 -0
- /package/skills/{plugin-guide → plugins-guide}/SKILL.md +0 -0
- /package/skills/{python-plugin-dev → python}/SKILL.md +0 -0
|
@@ -1,391 +1,391 @@
|
|
|
1
|
-
---
|
|
2
|
-
name: workflow-troubleshooting
|
|
3
|
-
description: 工作流故障排除与修复指南。当工作流执行失败、fetch 工具不可用、模板变量问题时调用此技能。
|
|
4
|
-
allowed-tools: python-executor, shell, read_file, write_file, execute_workflow, fetch
|
|
5
|
-
---
|
|
6
|
-
|
|
7
|
-
# 工作流故障排除指南
|
|
8
|
-
|
|
9
|
-
## 常见问题与解决方案
|
|
10
|
-
|
|
11
|
-
### 1. fetch 工具可用 ✅
|
|
12
|
-
|
|
13
|
-
**好消息**: 工作流引擎**支持** `fetch` 工具!可以直接在 steps 中使用:
|
|
14
|
-
|
|
15
|
-
```yaml
|
|
16
|
-
steps:
|
|
17
|
-
- type: tool
|
|
18
|
-
name: fetch
|
|
19
|
-
params:
|
|
20
|
-
url: 'https://news.baidu.com'
|
|
21
|
-
timeout: 10000
|
|
22
|
-
output: 'baidu_news'
|
|
23
|
-
```
|
|
24
|
-
|
|
25
|
-
**优势**:
|
|
26
|
-
|
|
27
|
-
- 原生支持,无需额外 sandbox 配置
|
|
28
|
-
- 语法简洁
|
|
29
|
-
- 自动处理超时和错误
|
|
30
|
-
|
|
31
|
-
### 2. 多个 fetch 步骤串联
|
|
32
|
-
|
|
33
|
-
**已验证可用**:
|
|
34
|
-
|
|
35
|
-
```yaml
|
|
36
|
-
steps:
|
|
37
|
-
- type: tool
|
|
38
|
-
name: fetch
|
|
39
|
-
params:
|
|
40
|
-
url: 'https://news.baidu.com'
|
|
41
|
-
output: 'baidu_news'
|
|
42
|
-
|
|
43
|
-
- type: tool
|
|
44
|
-
name: fetch
|
|
45
|
-
params:
|
|
46
|
-
url: 'https://feeds.bbci.co.uk/news/world/rss.xml'
|
|
47
|
-
output: 'bbc_news'
|
|
48
|
-
|
|
49
|
-
- type: tool
|
|
50
|
-
name: fetch
|
|
51
|
-
params:
|
|
52
|
-
url: 'https://www.reddit.com/r/popular/hot.json?limit=20'
|
|
53
|
-
output: 'reddit_news'
|
|
54
|
-
```
|
|
55
|
-
|
|
56
|
-
### 3. Python 脚本获取结果
|
|
57
|
-
|
|
58
|
-
**正确方式**: Python 脚本返回 JSON 格式字符串
|
|
59
|
-
|
|
60
|
-
```python
|
|
61
|
-
import json
|
|
62
|
-
import requests
|
|
63
|
-
|
|
64
|
-
result = {"news": ["标题1", "标题2"]}
|
|
65
|
-
print(json.dumps(result)) # 输出 JSON 字符串
|
|
66
|
-
```
|
|
67
|
-
|
|
68
|
-
### 4. webhook 类型步骤
|
|
69
|
-
|
|
70
|
-
**警告**: `webhook` 类型需要 HTTP 服务支持,否则会失败。
|
|
71
|
-
|
|
72
|
-
- 如需接收外部数据,使用 `web_register_webhook` 注册路由
|
|
73
|
-
- 测试时改用 `tool` + `fetch` 组合
|
|
74
|
-
|
|
75
|
-
### 5. 变量传递问题
|
|
76
|
-
|
|
77
|
-
- **原因**: sandbox 环境无法访问主环境变量
|
|
78
|
-
- **解决方案**:
|
|
79
|
-
- 使用上下文变量 `$.context.xxx`
|
|
80
|
-
- 或直接在脚本中硬编码(测试用)
|
|
81
|
-
|
|
82
|
-
## 调试技巧
|
|
83
|
-
|
|
84
|
-
1. **先单独测试每个步骤** - 用简单输入验证
|
|
85
|
-
2. **检查工作流定义语法** - YAML/JSON 格式正确性
|
|
86
|
-
3. **使用简单输入测试变量传递**
|
|
87
|
-
4. **fetch 失败时**: 加 `timeout` 参数,加 `proxy: true` 重试
|
|
88
|
-
|
|
89
|
-
## 成功案例
|
|
90
|
-
|
|
91
|
-
### 真实新闻聚合工作流
|
|
92
|
-
|
|
93
|
-
```yaml
|
|
94
|
-
name: 'news-fetcher'
|
|
95
|
-
steps:
|
|
96
|
-
- type: 'context'
|
|
97
|
-
key: 'current_time'
|
|
98
|
-
value: '$.time.current'
|
|
99
|
-
|
|
100
|
-
- type: 'tool'
|
|
101
|
-
name: 'fetch'
|
|
102
|
-
params:
|
|
103
|
-
url: 'https://news.baidu.com'
|
|
104
|
-
timeout: 15000
|
|
105
|
-
output: 'baidu_news'
|
|
106
|
-
|
|
107
|
-
- type: 'tool'
|
|
108
|
-
name: 'fetch'
|
|
109
|
-
params:
|
|
110
|
-
url: 'https://feeds.bbci.co.uk/news/world/rss.xml'
|
|
111
|
-
timeout: 15000
|
|
112
|
-
output: 'bbc_news'
|
|
113
|
-
|
|
114
|
-
- type: 'tool'
|
|
115
|
-
name: 'fetch'
|
|
116
|
-
params:
|
|
117
|
-
url: 'https://www.reddit.com/r/popular/hot.json?limit=20'
|
|
118
|
-
timeout: 15000
|
|
119
|
-
output: 'reddit_news'
|
|
120
|
-
|
|
121
|
-
- type: 'python'
|
|
122
|
-
script: |
|
|
123
|
-
import json
|
|
124
|
-
# 处理新闻数据...
|
|
125
|
-
result = {"status": "success", "sources": ["baidu", "bbc", "reddit"]}
|
|
126
|
-
print(json.dumps(result))
|
|
127
|
-
```
|
|
128
|
-
|
|
129
|
-
### 网络不稳定时加代理
|
|
130
|
-
|
|
131
|
-
```yaml
|
|
132
|
-
- type: 'tool'
|
|
133
|
-
name: 'fetch'
|
|
134
|
-
params:
|
|
135
|
-
url: 'https://feeds.bbci.co.uk/news/world/rss.xml'
|
|
136
|
-
timeout: 15000
|
|
137
|
-
proxy: true # 失败时自动启用
|
|
138
|
-
output: 'bbc_news'
|
|
139
|
-
```
|
|
140
|
-
|
|
141
|
-
## 复杂工作流步骤类型测试 ✅
|
|
142
|
-
|
|
143
|
-
以下步骤类型已通过完整测试验证:
|
|
144
|
-
|
|
145
|
-
### 1. parallel (并行执行) ✅
|
|
146
|
-
|
|
147
|
-
多个任务同时执行,提高效率:
|
|
148
|
-
|
|
149
|
-
```json
|
|
150
|
-
{
|
|
151
|
-
"type": "parallel",
|
|
152
|
-
"steps": [
|
|
153
|
-
{ "type": "tool", "name": "get_time", "output": "current_time" },
|
|
154
|
-
{
|
|
155
|
-
"type": "tool",
|
|
156
|
-
"name": "storage_set",
|
|
157
|
-
"params": { "key": "test", "value": "ok" },
|
|
158
|
-
"output": "storage_result"
|
|
159
|
-
},
|
|
160
|
-
{
|
|
161
|
-
"type": "tool",
|
|
162
|
-
"name": "fetch",
|
|
163
|
-
"params": { "url": "https://news.baidu.com" },
|
|
164
|
-
"output": "news"
|
|
165
|
-
}
|
|
166
|
-
]
|
|
167
|
-
}
|
|
168
|
-
```
|
|
169
|
-
|
|
170
|
-
### 2. condition (条件分支) ✅
|
|
171
|
-
|
|
172
|
-
支持 if/else 逻辑判断:
|
|
173
|
-
|
|
174
|
-
```json
|
|
175
|
-
{
|
|
176
|
-
"type": "condition",
|
|
177
|
-
"if": { "key": "$.context.storage_result", "equals": "success" },
|
|
178
|
-
"then": [
|
|
179
|
-
{
|
|
180
|
-
"type": "tool",
|
|
181
|
-
"name": "notification_send",
|
|
182
|
-
"params": { "title": "成功", "message": "条件成立" }
|
|
183
|
-
}
|
|
184
|
-
],
|
|
185
|
-
"else": [
|
|
186
|
-
{
|
|
187
|
-
"type": "tool",
|
|
188
|
-
"name": "notification_send",
|
|
189
|
-
"params": { "title": "失败", "message": "条件不成立" }
|
|
190
|
-
}
|
|
191
|
-
]
|
|
192
|
-
}
|
|
193
|
-
```
|
|
194
|
-
|
|
195
|
-
**支持的操作符**:`equals`, `not_equals`, `contains`, `greater_than`, `less_than`, `exists`, `not_exists`
|
|
196
|
-
|
|
197
|
-
### 3. loop (循环执行) ✅
|
|
198
|
-
|
|
199
|
-
支持指定次数的循环:
|
|
200
|
-
|
|
201
|
-
```json
|
|
202
|
-
{
|
|
203
|
-
"type": "loop",
|
|
204
|
-
"count": 3,
|
|
205
|
-
"steps": [
|
|
206
|
-
{
|
|
207
|
-
"type": "tool",
|
|
208
|
-
"name": "notification_send",
|
|
209
|
-
"params": { "title": "循环", "message": "第 {{index}} 次" }
|
|
210
|
-
}
|
|
211
|
-
]
|
|
212
|
-
}
|
|
213
|
-
```
|
|
214
|
-
|
|
215
|
-
### 4. switch (多分支) ✅
|
|
216
|
-
|
|
217
|
-
类似 switch-case 的多条件分支:
|
|
218
|
-
|
|
219
|
-
```json
|
|
220
|
-
{
|
|
221
|
-
"type": "switch",
|
|
222
|
-
"value": "$.context.status",
|
|
223
|
-
"cases": [
|
|
224
|
-
{
|
|
225
|
-
"value": "success",
|
|
226
|
-
"steps": [{ "type": "tool", "name": "notification_send", "params": { "title": "成功" } }]
|
|
227
|
-
},
|
|
228
|
-
{
|
|
229
|
-
"value": "error",
|
|
230
|
-
"steps": [{ "type": "tool", "name": "notification_send", "params": { "title": "错误" } }]
|
|
231
|
-
}
|
|
232
|
-
],
|
|
233
|
-
"default": [{ "type": "tool", "name": "notification_send", "params": { "title": "默认" } }]
|
|
234
|
-
}
|
|
235
|
-
```
|
|
236
|
-
|
|
237
|
-
### 5. try-catch (错误处理) ✅
|
|
238
|
-
|
|
239
|
-
捕获步骤执行中的错误,防止中断:
|
|
240
|
-
|
|
241
|
-
```json
|
|
242
|
-
{
|
|
243
|
-
"type": "try",
|
|
244
|
-
"steps": [{ "type": "tool", "name": "email_unread_count" }],
|
|
245
|
-
"catch": [
|
|
246
|
-
{
|
|
247
|
-
"type": "tool",
|
|
248
|
-
"name": "notification_send",
|
|
249
|
-
"params": { "title": "捕获错误", "message": "邮件服务超时,使用备用方案" }
|
|
250
|
-
}
|
|
251
|
-
]
|
|
252
|
-
}
|
|
253
|
-
```
|
|
254
|
-
|
|
255
|
-
### 6. delay (延迟执行) ✅
|
|
256
|
-
|
|
257
|
-
在步骤之间添加延迟:
|
|
258
|
-
|
|
259
|
-
```json
|
|
260
|
-
{
|
|
261
|
-
"type": "delay",
|
|
262
|
-
"ms": 500
|
|
263
|
-
}
|
|
264
|
-
```
|
|
265
|
-
|
|
266
|
-
### 7. nested workflow (嵌套工作流) ✅
|
|
267
|
-
|
|
268
|
-
在主工作流中调用子工作流:
|
|
269
|
-
|
|
270
|
-
```json
|
|
271
|
-
{
|
|
272
|
-
"type": "workflow",
|
|
273
|
-
"name": "workflow_file-backup",
|
|
274
|
-
"input": {}
|
|
275
|
-
}
|
|
276
|
-
```
|
|
277
|
-
|
|
278
|
-
### 8. context (上下文变量) ✅
|
|
279
|
-
|
|
280
|
-
在步骤间传递数据:
|
|
281
|
-
|
|
282
|
-
```json
|
|
283
|
-
{
|
|
284
|
-
"type": "context",
|
|
285
|
-
"key": "workflow_status",
|
|
286
|
-
"value": "completed"
|
|
287
|
-
}
|
|
288
|
-
```
|
|
289
|
-
|
|
290
|
-
## 复杂工作流完整示例
|
|
291
|
-
|
|
292
|
-
```json
|
|
293
|
-
{
|
|
294
|
-
"name": "complex-workflow-demo",
|
|
295
|
-
"input": {},
|
|
296
|
-
"steps": [
|
|
297
|
-
{
|
|
298
|
-
"type": "parallel",
|
|
299
|
-
"steps": [
|
|
300
|
-
{ "type": "tool", "name": "get_time", "output": "time" },
|
|
301
|
-
{
|
|
302
|
-
"type": "tool",
|
|
303
|
-
"name": "storage_set",
|
|
304
|
-
"params": { "key": "status", "value": "started" },
|
|
305
|
-
"output": "storage"
|
|
306
|
-
},
|
|
307
|
-
{ "type": "tool", "name": "storage_list", "output": "namespaces" }
|
|
308
|
-
]
|
|
309
|
-
},
|
|
310
|
-
{
|
|
311
|
-
"type": "condition",
|
|
312
|
-
"if": { "key": "$.context.storage", "exists": true },
|
|
313
|
-
"then": [
|
|
314
|
-
{
|
|
315
|
-
"type": "tool",
|
|
316
|
-
"name": "notification_send",
|
|
317
|
-
"params": { "title": "开始", "message": "工作流启动" }
|
|
318
|
-
}
|
|
319
|
-
]
|
|
320
|
-
},
|
|
321
|
-
{ "type": "delay", "ms": 300 },
|
|
322
|
-
{
|
|
323
|
-
"type": "loop",
|
|
324
|
-
"count": 2,
|
|
325
|
-
"steps": [
|
|
326
|
-
{
|
|
327
|
-
"type": "tool",
|
|
328
|
-
"name": "notification_send",
|
|
329
|
-
"params": { "title": "循环", "message": "第 {{index}} 次" }
|
|
330
|
-
}
|
|
331
|
-
]
|
|
332
|
-
},
|
|
333
|
-
{
|
|
334
|
-
"type": "switch",
|
|
335
|
-
"value": "$.context.storage",
|
|
336
|
-
"cases": [
|
|
337
|
-
{
|
|
338
|
-
"value": "started",
|
|
339
|
-
"steps": [
|
|
340
|
-
{
|
|
341
|
-
"type": "tool",
|
|
342
|
-
"name": "notification_send",
|
|
343
|
-
"params": { "title": "状态", "message": "已开始" }
|
|
344
|
-
}
|
|
345
|
-
]
|
|
346
|
-
}
|
|
347
|
-
],
|
|
348
|
-
"default": [
|
|
349
|
-
{
|
|
350
|
-
"type": "tool",
|
|
351
|
-
"name": "notification_send",
|
|
352
|
-
"params": { "title": "状态", "message": "默认状态" }
|
|
353
|
-
}
|
|
354
|
-
]
|
|
355
|
-
},
|
|
356
|
-
{
|
|
357
|
-
"type": "try",
|
|
358
|
-
"steps": [{ "type": "tool", "name": "email_unread_count" }],
|
|
359
|
-
"catch": [
|
|
360
|
-
{
|
|
361
|
-
"type": "tool",
|
|
362
|
-
"name": "notification_send",
|
|
363
|
-
"params": { "title": "错误处理", "message": "捕获异常完成" }
|
|
364
|
-
}
|
|
365
|
-
]
|
|
366
|
-
},
|
|
367
|
-
{ "type": "workflow", "name": "workflow_file-backup", "input": {} },
|
|
368
|
-
{
|
|
369
|
-
"type": "tool",
|
|
370
|
-
"name": "notification_send",
|
|
371
|
-
"params": { "title": "完成", "message": "复杂工作流执行成功" }
|
|
372
|
-
}
|
|
373
|
-
]
|
|
374
|
-
}
|
|
375
|
-
```
|
|
376
|
-
|
|
377
|
-
## 关键结论
|
|
378
|
-
|
|
379
|
-
| 场景 | 推荐方案 |
|
|
380
|
-
| ------------ | ---------------------------- |
|
|
381
|
-
| 获取网络数据 | ✅ `fetch` 工具 |
|
|
382
|
-
| 本地脚本处理 | ✅ `python` 类型 + JSON 输出 |
|
|
383
|
-
| 接收外部请求 | `webhook` 类型(需服务运行) |
|
|
384
|
-
| 串行任务 | ✅ 多个 `tool`/`python` 步骤 |
|
|
385
|
-
| 并行任务 | ✅ `parallel` 类型 |
|
|
386
|
-
| 条件分支 | ✅ `condition` 类型 |
|
|
387
|
-
| 循环执行 | ✅ `loop` 类型 |
|
|
388
|
-
| 多分支选择 | ✅ `switch` 类型 |
|
|
389
|
-
| 错误处理 | ✅ `try-catch` 类型 |
|
|
390
|
-
| 延迟等待 | ✅ `delay` 类型 |
|
|
391
|
-
| 子工作流调用 | ✅ `workflow` 类型 |
|
|
1
|
+
---
|
|
2
|
+
name: workflow-troubleshooting
|
|
3
|
+
description: 工作流故障排除与修复指南。当工作流执行失败、fetch 工具不可用、模板变量问题时调用此技能。
|
|
4
|
+
allowed-tools: python-executor, shell, read_file, write_file, execute_workflow, fetch
|
|
5
|
+
---
|
|
6
|
+
|
|
7
|
+
# 工作流故障排除指南
|
|
8
|
+
|
|
9
|
+
## 常见问题与解决方案
|
|
10
|
+
|
|
11
|
+
### 1. fetch 工具可用 ✅
|
|
12
|
+
|
|
13
|
+
**好消息**: 工作流引擎**支持** `fetch` 工具!可以直接在 steps 中使用:
|
|
14
|
+
|
|
15
|
+
```yaml
|
|
16
|
+
steps:
|
|
17
|
+
- type: tool
|
|
18
|
+
name: fetch
|
|
19
|
+
params:
|
|
20
|
+
url: 'https://news.baidu.com'
|
|
21
|
+
timeout: 10000
|
|
22
|
+
output: 'baidu_news'
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
**优势**:
|
|
26
|
+
|
|
27
|
+
- 原生支持,无需额外 sandbox 配置
|
|
28
|
+
- 语法简洁
|
|
29
|
+
- 自动处理超时和错误
|
|
30
|
+
|
|
31
|
+
### 2. 多个 fetch 步骤串联
|
|
32
|
+
|
|
33
|
+
**已验证可用**:
|
|
34
|
+
|
|
35
|
+
```yaml
|
|
36
|
+
steps:
|
|
37
|
+
- type: tool
|
|
38
|
+
name: fetch
|
|
39
|
+
params:
|
|
40
|
+
url: 'https://news.baidu.com'
|
|
41
|
+
output: 'baidu_news'
|
|
42
|
+
|
|
43
|
+
- type: tool
|
|
44
|
+
name: fetch
|
|
45
|
+
params:
|
|
46
|
+
url: 'https://feeds.bbci.co.uk/news/world/rss.xml'
|
|
47
|
+
output: 'bbc_news'
|
|
48
|
+
|
|
49
|
+
- type: tool
|
|
50
|
+
name: fetch
|
|
51
|
+
params:
|
|
52
|
+
url: 'https://www.reddit.com/r/popular/hot.json?limit=20'
|
|
53
|
+
output: 'reddit_news'
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
### 3. Python 脚本获取结果
|
|
57
|
+
|
|
58
|
+
**正确方式**: Python 脚本返回 JSON 格式字符串
|
|
59
|
+
|
|
60
|
+
```python
|
|
61
|
+
import json
|
|
62
|
+
import requests
|
|
63
|
+
|
|
64
|
+
result = {"news": ["标题1", "标题2"]}
|
|
65
|
+
print(json.dumps(result)) # 输出 JSON 字符串
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
### 4. webhook 类型步骤
|
|
69
|
+
|
|
70
|
+
**警告**: `webhook` 类型需要 HTTP 服务支持,否则会失败。
|
|
71
|
+
|
|
72
|
+
- 如需接收外部数据,使用 `web_register_webhook` 注册路由
|
|
73
|
+
- 测试时改用 `tool` + `fetch` 组合
|
|
74
|
+
|
|
75
|
+
### 5. 变量传递问题
|
|
76
|
+
|
|
77
|
+
- **原因**: sandbox 环境无法访问主环境变量
|
|
78
|
+
- **解决方案**:
|
|
79
|
+
- 使用上下文变量 `$.context.xxx`
|
|
80
|
+
- 或直接在脚本中硬编码(测试用)
|
|
81
|
+
|
|
82
|
+
## 调试技巧
|
|
83
|
+
|
|
84
|
+
1. **先单独测试每个步骤** - 用简单输入验证
|
|
85
|
+
2. **检查工作流定义语法** - YAML/JSON 格式正确性
|
|
86
|
+
3. **使用简单输入测试变量传递**
|
|
87
|
+
4. **fetch 失败时**: 加 `timeout` 参数,加 `proxy: true` 重试
|
|
88
|
+
|
|
89
|
+
## 成功案例
|
|
90
|
+
|
|
91
|
+
### 真实新闻聚合工作流
|
|
92
|
+
|
|
93
|
+
```yaml
|
|
94
|
+
name: 'news-fetcher'
|
|
95
|
+
steps:
|
|
96
|
+
- type: 'context'
|
|
97
|
+
key: 'current_time'
|
|
98
|
+
value: '$.time.current'
|
|
99
|
+
|
|
100
|
+
- type: 'tool'
|
|
101
|
+
name: 'fetch'
|
|
102
|
+
params:
|
|
103
|
+
url: 'https://news.baidu.com'
|
|
104
|
+
timeout: 15000
|
|
105
|
+
output: 'baidu_news'
|
|
106
|
+
|
|
107
|
+
- type: 'tool'
|
|
108
|
+
name: 'fetch'
|
|
109
|
+
params:
|
|
110
|
+
url: 'https://feeds.bbci.co.uk/news/world/rss.xml'
|
|
111
|
+
timeout: 15000
|
|
112
|
+
output: 'bbc_news'
|
|
113
|
+
|
|
114
|
+
- type: 'tool'
|
|
115
|
+
name: 'fetch'
|
|
116
|
+
params:
|
|
117
|
+
url: 'https://www.reddit.com/r/popular/hot.json?limit=20'
|
|
118
|
+
timeout: 15000
|
|
119
|
+
output: 'reddit_news'
|
|
120
|
+
|
|
121
|
+
- type: 'python'
|
|
122
|
+
script: |
|
|
123
|
+
import json
|
|
124
|
+
# 处理新闻数据...
|
|
125
|
+
result = {"status": "success", "sources": ["baidu", "bbc", "reddit"]}
|
|
126
|
+
print(json.dumps(result))
|
|
127
|
+
```
|
|
128
|
+
|
|
129
|
+
### 网络不稳定时加代理
|
|
130
|
+
|
|
131
|
+
```yaml
|
|
132
|
+
- type: 'tool'
|
|
133
|
+
name: 'fetch'
|
|
134
|
+
params:
|
|
135
|
+
url: 'https://feeds.bbci.co.uk/news/world/rss.xml'
|
|
136
|
+
timeout: 15000
|
|
137
|
+
proxy: true # 失败时自动启用
|
|
138
|
+
output: 'bbc_news'
|
|
139
|
+
```
|
|
140
|
+
|
|
141
|
+
## 复杂工作流步骤类型测试 ✅
|
|
142
|
+
|
|
143
|
+
以下步骤类型已通过完整测试验证:
|
|
144
|
+
|
|
145
|
+
### 1. parallel (并行执行) ✅
|
|
146
|
+
|
|
147
|
+
多个任务同时执行,提高效率:
|
|
148
|
+
|
|
149
|
+
```json
|
|
150
|
+
{
|
|
151
|
+
"type": "parallel",
|
|
152
|
+
"steps": [
|
|
153
|
+
{ "type": "tool", "name": "get_time", "output": "current_time" },
|
|
154
|
+
{
|
|
155
|
+
"type": "tool",
|
|
156
|
+
"name": "storage_set",
|
|
157
|
+
"params": { "key": "test", "value": "ok" },
|
|
158
|
+
"output": "storage_result"
|
|
159
|
+
},
|
|
160
|
+
{
|
|
161
|
+
"type": "tool",
|
|
162
|
+
"name": "fetch",
|
|
163
|
+
"params": { "url": "https://news.baidu.com" },
|
|
164
|
+
"output": "news"
|
|
165
|
+
}
|
|
166
|
+
]
|
|
167
|
+
}
|
|
168
|
+
```
|
|
169
|
+
|
|
170
|
+
### 2. condition (条件分支) ✅
|
|
171
|
+
|
|
172
|
+
支持 if/else 逻辑判断:
|
|
173
|
+
|
|
174
|
+
```json
|
|
175
|
+
{
|
|
176
|
+
"type": "condition",
|
|
177
|
+
"if": { "key": "$.context.storage_result", "equals": "success" },
|
|
178
|
+
"then": [
|
|
179
|
+
{
|
|
180
|
+
"type": "tool",
|
|
181
|
+
"name": "notification_send",
|
|
182
|
+
"params": { "title": "成功", "message": "条件成立" }
|
|
183
|
+
}
|
|
184
|
+
],
|
|
185
|
+
"else": [
|
|
186
|
+
{
|
|
187
|
+
"type": "tool",
|
|
188
|
+
"name": "notification_send",
|
|
189
|
+
"params": { "title": "失败", "message": "条件不成立" }
|
|
190
|
+
}
|
|
191
|
+
]
|
|
192
|
+
}
|
|
193
|
+
```
|
|
194
|
+
|
|
195
|
+
**支持的操作符**:`equals`, `not_equals`, `contains`, `greater_than`, `less_than`, `exists`, `not_exists`
|
|
196
|
+
|
|
197
|
+
### 3. loop (循环执行) ✅
|
|
198
|
+
|
|
199
|
+
支持指定次数的循环:
|
|
200
|
+
|
|
201
|
+
```json
|
|
202
|
+
{
|
|
203
|
+
"type": "loop",
|
|
204
|
+
"count": 3,
|
|
205
|
+
"steps": [
|
|
206
|
+
{
|
|
207
|
+
"type": "tool",
|
|
208
|
+
"name": "notification_send",
|
|
209
|
+
"params": { "title": "循环", "message": "第 {{index}} 次" }
|
|
210
|
+
}
|
|
211
|
+
]
|
|
212
|
+
}
|
|
213
|
+
```
|
|
214
|
+
|
|
215
|
+
### 4. switch (多分支) ✅
|
|
216
|
+
|
|
217
|
+
类似 switch-case 的多条件分支:
|
|
218
|
+
|
|
219
|
+
```json
|
|
220
|
+
{
|
|
221
|
+
"type": "switch",
|
|
222
|
+
"value": "$.context.status",
|
|
223
|
+
"cases": [
|
|
224
|
+
{
|
|
225
|
+
"value": "success",
|
|
226
|
+
"steps": [{ "type": "tool", "name": "notification_send", "params": { "title": "成功" } }]
|
|
227
|
+
},
|
|
228
|
+
{
|
|
229
|
+
"value": "error",
|
|
230
|
+
"steps": [{ "type": "tool", "name": "notification_send", "params": { "title": "错误" } }]
|
|
231
|
+
}
|
|
232
|
+
],
|
|
233
|
+
"default": [{ "type": "tool", "name": "notification_send", "params": { "title": "默认" } }]
|
|
234
|
+
}
|
|
235
|
+
```
|
|
236
|
+
|
|
237
|
+
### 5. try-catch (错误处理) ✅
|
|
238
|
+
|
|
239
|
+
捕获步骤执行中的错误,防止中断:
|
|
240
|
+
|
|
241
|
+
```json
|
|
242
|
+
{
|
|
243
|
+
"type": "try",
|
|
244
|
+
"steps": [{ "type": "tool", "name": "email_unread_count" }],
|
|
245
|
+
"catch": [
|
|
246
|
+
{
|
|
247
|
+
"type": "tool",
|
|
248
|
+
"name": "notification_send",
|
|
249
|
+
"params": { "title": "捕获错误", "message": "邮件服务超时,使用备用方案" }
|
|
250
|
+
}
|
|
251
|
+
]
|
|
252
|
+
}
|
|
253
|
+
```
|
|
254
|
+
|
|
255
|
+
### 6. delay (延迟执行) ✅
|
|
256
|
+
|
|
257
|
+
在步骤之间添加延迟:
|
|
258
|
+
|
|
259
|
+
```json
|
|
260
|
+
{
|
|
261
|
+
"type": "delay",
|
|
262
|
+
"ms": 500
|
|
263
|
+
}
|
|
264
|
+
```
|
|
265
|
+
|
|
266
|
+
### 7. nested workflow (嵌套工作流) ✅
|
|
267
|
+
|
|
268
|
+
在主工作流中调用子工作流:
|
|
269
|
+
|
|
270
|
+
```json
|
|
271
|
+
{
|
|
272
|
+
"type": "workflow",
|
|
273
|
+
"name": "workflow_file-backup",
|
|
274
|
+
"input": {}
|
|
275
|
+
}
|
|
276
|
+
```
|
|
277
|
+
|
|
278
|
+
### 8. context (上下文变量) ✅
|
|
279
|
+
|
|
280
|
+
在步骤间传递数据:
|
|
281
|
+
|
|
282
|
+
```json
|
|
283
|
+
{
|
|
284
|
+
"type": "context",
|
|
285
|
+
"key": "workflow_status",
|
|
286
|
+
"value": "completed"
|
|
287
|
+
}
|
|
288
|
+
```
|
|
289
|
+
|
|
290
|
+
## 复杂工作流完整示例
|
|
291
|
+
|
|
292
|
+
```json
|
|
293
|
+
{
|
|
294
|
+
"name": "complex-workflow-demo",
|
|
295
|
+
"input": {},
|
|
296
|
+
"steps": [
|
|
297
|
+
{
|
|
298
|
+
"type": "parallel",
|
|
299
|
+
"steps": [
|
|
300
|
+
{ "type": "tool", "name": "get_time", "output": "time" },
|
|
301
|
+
{
|
|
302
|
+
"type": "tool",
|
|
303
|
+
"name": "storage_set",
|
|
304
|
+
"params": { "key": "status", "value": "started" },
|
|
305
|
+
"output": "storage"
|
|
306
|
+
},
|
|
307
|
+
{ "type": "tool", "name": "storage_list", "output": "namespaces" }
|
|
308
|
+
]
|
|
309
|
+
},
|
|
310
|
+
{
|
|
311
|
+
"type": "condition",
|
|
312
|
+
"if": { "key": "$.context.storage", "exists": true },
|
|
313
|
+
"then": [
|
|
314
|
+
{
|
|
315
|
+
"type": "tool",
|
|
316
|
+
"name": "notification_send",
|
|
317
|
+
"params": { "title": "开始", "message": "工作流启动" }
|
|
318
|
+
}
|
|
319
|
+
]
|
|
320
|
+
},
|
|
321
|
+
{ "type": "delay", "ms": 300 },
|
|
322
|
+
{
|
|
323
|
+
"type": "loop",
|
|
324
|
+
"count": 2,
|
|
325
|
+
"steps": [
|
|
326
|
+
{
|
|
327
|
+
"type": "tool",
|
|
328
|
+
"name": "notification_send",
|
|
329
|
+
"params": { "title": "循环", "message": "第 {{index}} 次" }
|
|
330
|
+
}
|
|
331
|
+
]
|
|
332
|
+
},
|
|
333
|
+
{
|
|
334
|
+
"type": "switch",
|
|
335
|
+
"value": "$.context.storage",
|
|
336
|
+
"cases": [
|
|
337
|
+
{
|
|
338
|
+
"value": "started",
|
|
339
|
+
"steps": [
|
|
340
|
+
{
|
|
341
|
+
"type": "tool",
|
|
342
|
+
"name": "notification_send",
|
|
343
|
+
"params": { "title": "状态", "message": "已开始" }
|
|
344
|
+
}
|
|
345
|
+
]
|
|
346
|
+
}
|
|
347
|
+
],
|
|
348
|
+
"default": [
|
|
349
|
+
{
|
|
350
|
+
"type": "tool",
|
|
351
|
+
"name": "notification_send",
|
|
352
|
+
"params": { "title": "状态", "message": "默认状态" }
|
|
353
|
+
}
|
|
354
|
+
]
|
|
355
|
+
},
|
|
356
|
+
{
|
|
357
|
+
"type": "try",
|
|
358
|
+
"steps": [{ "type": "tool", "name": "email_unread_count" }],
|
|
359
|
+
"catch": [
|
|
360
|
+
{
|
|
361
|
+
"type": "tool",
|
|
362
|
+
"name": "notification_send",
|
|
363
|
+
"params": { "title": "错误处理", "message": "捕获异常完成" }
|
|
364
|
+
}
|
|
365
|
+
]
|
|
366
|
+
},
|
|
367
|
+
{ "type": "workflow", "name": "workflow_file-backup", "input": {} },
|
|
368
|
+
{
|
|
369
|
+
"type": "tool",
|
|
370
|
+
"name": "notification_send",
|
|
371
|
+
"params": { "title": "完成", "message": "复杂工作流执行成功" }
|
|
372
|
+
}
|
|
373
|
+
]
|
|
374
|
+
}
|
|
375
|
+
```
|
|
376
|
+
|
|
377
|
+
## 关键结论
|
|
378
|
+
|
|
379
|
+
| 场景 | 推荐方案 |
|
|
380
|
+
| ------------ | ---------------------------- |
|
|
381
|
+
| 获取网络数据 | ✅ `fetch` 工具 |
|
|
382
|
+
| 本地脚本处理 | ✅ `python` 类型 + JSON 输出 |
|
|
383
|
+
| 接收外部请求 | `webhook` 类型(需服务运行) |
|
|
384
|
+
| 串行任务 | ✅ 多个 `tool`/`python` 步骤 |
|
|
385
|
+
| 并行任务 | ✅ `parallel` 类型 |
|
|
386
|
+
| 条件分支 | ✅ `condition` 类型 |
|
|
387
|
+
| 循环执行 | ✅ `loop` 类型 |
|
|
388
|
+
| 多分支选择 | ✅ `switch` 类型 |
|
|
389
|
+
| 错误处理 | ✅ `try-catch` 类型 |
|
|
390
|
+
| 延迟等待 | ✅ `delay` 类型 |
|
|
391
|
+
| 子工作流调用 | ✅ `workflow` 类型 |
|