foliko 1.0.74 → 1.0.76
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/.agent/.shared/ui-ux-pro-max/data/charts.csv +26 -0
- package/.agent/.shared/ui-ux-pro-max/data/colors.csv +97 -0
- package/.agent/.shared/ui-ux-pro-max/data/icons.csv +101 -0
- package/.agent/.shared/ui-ux-pro-max/data/landing.csv +31 -0
- package/.agent/.shared/ui-ux-pro-max/data/products.csv +97 -0
- package/.agent/.shared/ui-ux-pro-max/data/prompts.csv +24 -0
- package/.agent/.shared/ui-ux-pro-max/data/react-performance.csv +45 -0
- package/.agent/.shared/ui-ux-pro-max/data/stacks/flutter.csv +53 -0
- package/.agent/.shared/ui-ux-pro-max/data/stacks/html-tailwind.csv +56 -0
- package/.agent/.shared/ui-ux-pro-max/data/stacks/jetpack-compose.csv +53 -0
- package/.agent/.shared/ui-ux-pro-max/data/stacks/nextjs.csv +53 -0
- package/.agent/.shared/ui-ux-pro-max/data/stacks/nuxt-ui.csv +51 -0
- package/.agent/.shared/ui-ux-pro-max/data/stacks/nuxtjs.csv +59 -0
- package/.agent/.shared/ui-ux-pro-max/data/stacks/react-native.csv +52 -0
- package/.agent/.shared/ui-ux-pro-max/data/stacks/react.csv +54 -0
- package/.agent/.shared/ui-ux-pro-max/data/stacks/shadcn.csv +61 -0
- package/.agent/.shared/ui-ux-pro-max/data/stacks/svelte.csv +54 -0
- package/.agent/.shared/ui-ux-pro-max/data/stacks/swiftui.csv +51 -0
- package/.agent/.shared/ui-ux-pro-max/data/stacks/vue.csv +50 -0
- package/.agent/.shared/ui-ux-pro-max/data/styles.csv +59 -0
- package/.agent/.shared/ui-ux-pro-max/data/typography.csv +58 -0
- package/.agent/.shared/ui-ux-pro-max/data/ui-reasoning.csv +101 -0
- package/.agent/.shared/ui-ux-pro-max/data/ux-guidelines.csv +100 -0
- package/.agent/.shared/ui-ux-pro-max/data/web-interface.csv +31 -0
- package/.agent/.shared/ui-ux-pro-max/scripts/__pycache__/core.cpython-313.pyc +0 -0
- package/.agent/.shared/ui-ux-pro-max/scripts/__pycache__/design_system.cpython-313.pyc +0 -0
- package/.agent/.shared/ui-ux-pro-max/scripts/core.py +258 -0
- package/.agent/.shared/ui-ux-pro-max/scripts/design_system.py +1067 -0
- package/.agent/.shared/ui-ux-pro-max/scripts/search.py +106 -0
- package/.agent/ARCHITECTURE.md +288 -0
- package/.agent/agents/ambient-agent.md +57 -0
- package/.agent/agents/debugger.md +55 -0
- package/.agent/agents/email-assistant.md +49 -0
- package/.agent/agents/file-manager.md +42 -0
- package/.agent/agents/python-developer.md +60 -0
- package/.agent/agents/scheduler.md +59 -0
- package/.agent/agents/web-developer.md +45 -0
- package/.agent/data/default.json +29 -0
- package/.agent/data/plugins-state.json +255 -0
- package/.agent/mcp_config.json +4 -0
- package/.agent/mcp_config_updated.json +12 -0
- package/.agent/plugins.json +5 -0
- package/.agent/rules/GEMINI.md +273 -0
- package/.agent/rules/allow-rule.md +77 -0
- package/.agent/rules/log-rule.md +83 -0
- package/.agent/rules/security-rule.md +93 -0
- package/.agent/scripts/auto_preview.py +148 -0
- package/.agent/scripts/checklist.py +217 -0
- package/.agent/scripts/session_manager.py +120 -0
- package/.agent/scripts/verify_all.py +327 -0
- package/.agent/skills/api-patterns/SKILL.md +81 -0
- package/.agent/skills/api-patterns/api-style.md +42 -0
- package/.agent/skills/api-patterns/auth.md +24 -0
- package/.agent/skills/api-patterns/documentation.md +26 -0
- package/.agent/skills/api-patterns/graphql.md +41 -0
- package/.agent/skills/api-patterns/rate-limiting.md +31 -0
- package/.agent/skills/api-patterns/response.md +37 -0
- package/.agent/skills/api-patterns/rest.md +40 -0
- package/.agent/skills/api-patterns/scripts/api_validator.py +211 -0
- package/.agent/skills/api-patterns/security-testing.md +122 -0
- package/.agent/skills/api-patterns/trpc.md +41 -0
- package/.agent/skills/api-patterns/versioning.md +22 -0
- package/.agent/skills/app-builder/SKILL.md +75 -0
- package/.agent/skills/app-builder/agent-coordination.md +71 -0
- package/.agent/skills/app-builder/feature-building.md +53 -0
- package/.agent/skills/app-builder/project-detection.md +34 -0
- package/.agent/skills/app-builder/scaffolding.md +118 -0
- package/.agent/skills/app-builder/tech-stack.md +40 -0
- package/.agent/skills/app-builder/templates/SKILL.md +39 -0
- package/.agent/skills/app-builder/templates/astro-static/TEMPLATE.md +76 -0
- package/.agent/skills/app-builder/templates/chrome-extension/TEMPLATE.md +92 -0
- package/.agent/skills/app-builder/templates/cli-tool/TEMPLATE.md +88 -0
- package/.agent/skills/app-builder/templates/electron-desktop/TEMPLATE.md +88 -0
- package/.agent/skills/app-builder/templates/express-api/TEMPLATE.md +83 -0
- package/.agent/skills/app-builder/templates/flutter-app/TEMPLATE.md +90 -0
- package/.agent/skills/app-builder/templates/monorepo-turborepo/TEMPLATE.md +90 -0
- package/.agent/skills/app-builder/templates/nextjs-fullstack/TEMPLATE.md +122 -0
- package/.agent/skills/app-builder/templates/nextjs-saas/TEMPLATE.md +122 -0
- package/.agent/skills/app-builder/templates/nextjs-static/TEMPLATE.md +169 -0
- package/.agent/skills/app-builder/templates/nuxt-app/TEMPLATE.md +134 -0
- package/.agent/skills/app-builder/templates/python-fastapi/TEMPLATE.md +83 -0
- package/.agent/skills/app-builder/templates/react-native-app/TEMPLATE.md +119 -0
- package/.agent/skills/architecture/SKILL.md +55 -0
- package/.agent/skills/architecture/context-discovery.md +43 -0
- package/.agent/skills/architecture/examples.md +94 -0
- package/.agent/skills/architecture/pattern-selection.md +68 -0
- package/.agent/skills/architecture/patterns-reference.md +50 -0
- package/.agent/skills/architecture/trade-off-analysis.md +77 -0
- package/.agent/skills/clean-code/SKILL.md +201 -0
- package/.agent/skills/doc.md +177 -0
- package/.agent/skills/frontend-design/SKILL.md +418 -0
- package/.agent/skills/frontend-design/animation-guide.md +331 -0
- package/.agent/skills/frontend-design/color-system.md +311 -0
- package/.agent/skills/frontend-design/decision-trees.md +418 -0
- package/.agent/skills/frontend-design/motion-graphics.md +306 -0
- package/.agent/skills/frontend-design/scripts/accessibility_checker.py +183 -0
- package/.agent/skills/frontend-design/scripts/ux_audit.py +722 -0
- package/.agent/skills/frontend-design/typography-system.md +345 -0
- package/.agent/skills/frontend-design/ux-psychology.md +1116 -0
- package/.agent/skills/frontend-design/visual-effects.md +383 -0
- package/.agent/skills/i18n-localization/SKILL.md +154 -0
- package/.agent/skills/i18n-localization/scripts/i18n_checker.py +241 -0
- package/.agent/skills/mcp-builder/SKILL.md +176 -0
- package/.agent/skills/web-design-guidelines/SKILL.md +57 -0
- package/.agent/workflows/brainstorm.md +113 -0
- package/.agent/workflows/create.md +59 -0
- package/.agent/workflows/debug.md +103 -0
- package/.agent/workflows/deploy.md +176 -0
- package/.agent/workflows/enhance.md +63 -0
- package/.agent/workflows/orchestrate.md +237 -0
- package/.agent/workflows/plan.md +89 -0
- package/.agent/workflows/preview.md +81 -0
- package/.agent/workflows/simple-test.md +42 -0
- package/.agent/workflows/status.md +86 -0
- package/.agent/workflows/structured-orchestrate.md +180 -0
- package/.agent/workflows/test.md +144 -0
- package/.agent/workflows/ui-ux-pro-max.md +296 -0
- package/.claude/settings.local.json +11 -1
- package/.editorconfig +56 -0
- package/.husky/pre-commit +4 -0
- package/.lintstagedrc +7 -0
- package/.prettierignore +29 -0
- package/.prettierrc +11 -0
- package/CLAUDE.md +2 -0
- package/README.md +64 -55
- package/SPEC.md +102 -61
- package/cli/bin/foliko.js +11 -11
- package/cli/src/commands/chat.js +143 -141
- package/cli/src/commands/list.js +93 -90
- package/cli/src/index.js +75 -75
- package/cli/src/ui/chat-ui.js +201 -199
- package/cli/src/utils/ansi.js +40 -40
- package/cli/src/utils/markdown.js +292 -296
- package/docker-compose.yml +1 -1
- package/docs/ai-sdk-optimization.md +655 -643
- package/docs/features.md +80 -80
- package/docs/quick-reference.md +49 -46
- package/docs/user-manual.md +411 -380
- package/examples/ambient-example.js +194 -196
- package/examples/basic.js +50 -45
- package/examples/bootstrap.js +121 -112
- package/examples/mcp-example.js +19 -16
- package/examples/skill-example.js +20 -20
- package/examples/test-chat.js +137 -135
- package/examples/test-mcp.js +85 -79
- package/examples/test-reload.js +59 -61
- package/examples/test-telegram.js +50 -50
- package/examples/test-tg-bot.js +45 -42
- package/examples/test-tg-simple.js +47 -46
- package/examples/test-tg.js +62 -62
- package/examples/test-think.js +43 -37
- package/examples/test-web-plugin.js +103 -98
- package/examples/test-weixin-feishu.js +103 -100
- package/examples/workflow.js +158 -158
- package/package.json +37 -3
- package/plugins/ai-plugin.js +102 -100
- package/plugins/ambient-agent/EventWatcher.js +113 -0
- package/plugins/ambient-agent/ExplorerLoop.js +640 -0
- package/plugins/ambient-agent/GoalManager.js +197 -0
- package/plugins/ambient-agent/Reflector.js +95 -0
- package/plugins/ambient-agent/StateStore.js +90 -0
- package/plugins/ambient-agent/constants.js +101 -0
- package/plugins/ambient-agent/index.js +579 -0
- package/plugins/audit-plugin.js +187 -187
- package/plugins/default-plugins.js +662 -649
- package/plugins/email/constants.js +64 -0
- package/plugins/email/handlers.js +461 -0
- package/plugins/email/index.js +278 -0
- package/plugins/email/monitor.js +269 -0
- package/plugins/email/parser.js +138 -0
- package/plugins/email/reply.js +151 -0
- package/plugins/email/utils.js +124 -0
- package/plugins/feishu-plugin.js +481 -477
- package/plugins/file-system-plugin.js +826 -476
- package/plugins/install-plugin.js +199 -197
- package/plugins/python-executor-plugin.js +367 -365
- package/plugins/python-plugin-loader.js +481 -479
- package/plugins/rules-plugin.js +294 -292
- package/plugins/scheduler-plugin.js +691 -689
- package/plugins/session-plugin.js +369 -367
- package/plugins/shell-executor-plugin.js +197 -197
- package/plugins/storage-plugin.js +240 -238
- package/plugins/subagent-plugin.js +845 -785
- package/plugins/telegram-plugin.js +482 -475
- package/plugins/think-plugin.js +345 -343
- package/plugins/tools-plugin.js +196 -194
- package/plugins/web-plugin.js +606 -604
- package/plugins/weixin-plugin.js +545 -538
- package/reports/system-health-report-20260401.md +79 -0
- package/skills/ambient-agent/SKILL.md +49 -39
- package/skills/foliko-dev/AGENTS.md +64 -61
- package/skills/foliko-dev/SKILL.md +125 -119
- package/skills/mcp-usage/SKILL.md +19 -17
- package/skills/python-plugin-dev/SKILL.md +16 -15
- package/skills/skill-guide/SKILL.md +12 -12
- package/skills/subagent-guide/SKILL.md +237 -0
- package/skills/workflow-guide/SKILL.md +90 -45
- package/skills/workflow-troubleshooting/DEBUGGING.md +36 -21
- package/skills/workflow-troubleshooting/SKILL.md +156 -79
- package/src/capabilities/index.js +11 -11
- package/src/capabilities/skill-manager.js +609 -595
- package/src/capabilities/workflow-engine.js +1109 -1195
- package/src/core/agent-chat.js +882 -735
- package/src/core/agent.js +892 -688
- package/src/core/framework.js +465 -431
- package/src/core/index.js +19 -19
- package/src/core/plugin-base.js +219 -219
- package/src/core/plugin-manager.js +863 -767
- package/src/core/provider.js +114 -111
- package/src/core/sub-agent-config.js +264 -0
- package/src/core/system-prompt-builder.js +120 -0
- package/src/core/tool-registry.js +517 -134
- package/src/core/tool-router.js +297 -216
- package/src/executors/executor-base.js +12 -12
- package/src/executors/mcp-executor.js +741 -729
- package/src/index.js +25 -37
- package/src/utils/circuit-breaker.js +301 -0
- package/src/utils/error-boundary.js +363 -0
- package/src/utils/error.js +374 -0
- package/src/utils/event-emitter.js +97 -97
- package/src/utils/id.js +133 -0
- package/src/utils/index.js +217 -3
- package/src/utils/logger.js +181 -0
- package/src/utils/plugin-helpers.js +90 -0
- package/src/utils/retry.js +122 -0
- package/src/utils/sandbox.js +292 -0
- package/test/tool-registry-validation.test.js +218 -0
- package/test_report.md +70 -0
- package/website/docs/api.html +169 -107
- package/website/docs/configuration.html +296 -144
- package/website/docs/plugin-development.html +154 -85
- package/website/docs/project-structure.html +110 -109
- package/website/docs/skill-development.html +117 -61
- package/website/index.html +209 -205
- package/website/script.js +136 -133
- package/website/styles.css +1 -1
- package/plugins/ambient-agent-plugin.js +0 -1565
- package/plugins/email.js +0 -1142
|
@@ -1,643 +1,655 @@
|
|
|
1
|
-
# AI SDK v6 优化报告 - Foliko 项目
|
|
2
|
-
|
|
3
|
-
> 本报告基于 [AI SDK v6 文档](https://ai-sdk.dev/docs/getting-started) 和 [Cookbook](https://ai-sdk.dev/cookbook) 分析,为 Foliko 项目提供具体的优化建议。
|
|
4
|
-
|
|
5
|
-
## 一、项目当前 AI SDK 使用分析
|
|
6
|
-
|
|
7
|
-
### 1.1 已集成的 AI SDK 组件
|
|
8
|
-
|
|
9
|
-
| 组件
|
|
10
|
-
|
|
11
|
-
| `ai`
|
|
12
|
-
| `@ai-sdk/openai`
|
|
13
|
-
| `@ai-sdk/anthropic`
|
|
14
|
-
| `@ai-sdk/openai-compatible` | 2.0.35
|
|
15
|
-
| `@ai-sdk/mcp`
|
|
16
|
-
| `zod`
|
|
17
|
-
| `tiktoken`
|
|
18
|
-
|
|
19
|
-
### 1.2 当前使用的 AI SDK 功能
|
|
20
|
-
|
|
21
|
-
```javascript
|
|
22
|
-
// ✅ 已使用
|
|
23
|
-
- ToolLoopAgent (工具调用循环)
|
|
24
|
-
- generateText (上下文总结)
|
|
25
|
-
- streamText 架构
|
|
26
|
-
- 基础 Provider 配置
|
|
27
|
-
- Zod schema (工具参数)
|
|
28
|
-
```
|
|
29
|
-
|
|
30
|
-
### 1.3 相关文件位置
|
|
31
|
-
|
|
32
|
-
| 功能
|
|
33
|
-
|
|
34
|
-
| `ToolLoopAgent` | `src/core/agent-chat.js`
|
|
35
|
-
| `generateText`
|
|
36
|
-
| `@ai-sdk/mcp`
|
|
37
|
-
| `Zod`
|
|
38
|
-
| `tiktoken`
|
|
39
|
-
|
|
40
|
-
---
|
|
41
|
-
|
|
42
|
-
## 二、优化建议
|
|
43
|
-
|
|
44
|
-
### 🔴 【高优先级】
|
|
45
|
-
|
|
46
|
-
#### ✅ 2.1 结构化输出 - `generateObject` / `streamObject` **[已完成]**
|
|
47
|
-
|
|
48
|
-
**现状**:项目使用 `generateText` 进行总结,但未使用结构化输出
|
|
49
|
-
|
|
50
|
-
**优化位置**:`src/core/agent-chat.js`
|
|
51
|
-
|
|
52
|
-
**已实现功能**:
|
|
53
|
-
|
|
54
|
-
- ✅ `
|
|
55
|
-
- ✅ `
|
|
56
|
-
- ✅ `
|
|
57
|
-
- ✅ `
|
|
58
|
-
- ✅ `
|
|
59
|
-
- ✅ `
|
|
60
|
-
- ✅ `
|
|
61
|
-
- ✅ `
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
-
|
|
67
|
-
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
- ✅ `
|
|
82
|
-
- ✅ `
|
|
83
|
-
- ✅ `
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
}
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
}
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
}
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
}
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
throw error
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
}
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
if (!
|
|
476
|
-
return
|
|
477
|
-
}
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
//
|
|
568
|
-
|
|
569
|
-
//
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
//
|
|
589
|
-
|
|
590
|
-
//
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
//
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
}
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
1
|
+
# AI SDK v6 优化报告 - Foliko 项目
|
|
2
|
+
|
|
3
|
+
> 本报告基于 [AI SDK v6 文档](https://ai-sdk.dev/docs/getting-started) 和 [Cookbook](https://ai-sdk.dev/cookbook) 分析,为 Foliko 项目提供具体的优化建议。
|
|
4
|
+
|
|
5
|
+
## 一、项目当前 AI SDK 使用分析
|
|
6
|
+
|
|
7
|
+
### 1.1 已集成的 AI SDK 组件
|
|
8
|
+
|
|
9
|
+
| 组件 | 版本 | 用途 |
|
|
10
|
+
| --------------------------- | ------- | ------------------ |
|
|
11
|
+
| `ai` | 6.0.116 | 核心 SDK |
|
|
12
|
+
| `@ai-sdk/openai` | 3.0.41 | OpenAI Provider |
|
|
13
|
+
| `@ai-sdk/anthropic` | 3.0.58 | Anthropic Provider |
|
|
14
|
+
| `@ai-sdk/openai-compatible` | 2.0.35 | 兼容 Provider |
|
|
15
|
+
| `@ai-sdk/mcp` | 1.0.25 | MCP 集成 |
|
|
16
|
+
| `zod` | 3.24.0 | Schema 验证 |
|
|
17
|
+
| `tiktoken` | 1.0.22 | Token 计数 |
|
|
18
|
+
|
|
19
|
+
### 1.2 当前使用的 AI SDK 功能
|
|
20
|
+
|
|
21
|
+
```javascript
|
|
22
|
+
// ✅ 已使用
|
|
23
|
+
- ToolLoopAgent (工具调用循环)
|
|
24
|
+
- generateText (上下文总结)
|
|
25
|
+
- streamText 架构
|
|
26
|
+
- 基础 Provider 配置
|
|
27
|
+
- Zod schema (工具参数)
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
### 1.3 相关文件位置
|
|
31
|
+
|
|
32
|
+
| 功能 | 文件位置 | 用途 |
|
|
33
|
+
| --------------- | ------------------------------- | ------------------------ |
|
|
34
|
+
| `ToolLoopAgent` | `src/core/agent-chat.js` | 工具调用循环 |
|
|
35
|
+
| `generateText` | `src/core/agent-chat.js` | 上下文总结、工具结果压缩 |
|
|
36
|
+
| `@ai-sdk/mcp` | `src/executors/mcp-executor.js` | MCP 服务器集成 |
|
|
37
|
+
| `Zod` | 全局 | 工具参数 schema |
|
|
38
|
+
| `tiktoken` | `src/core/agent-chat.js` | Token 计数、上下文压缩 |
|
|
39
|
+
|
|
40
|
+
---
|
|
41
|
+
|
|
42
|
+
## 二、优化建议
|
|
43
|
+
|
|
44
|
+
### 🔴 【高优先级】
|
|
45
|
+
|
|
46
|
+
#### ✅ 2.1 结构化输出 - `generateObject` / `streamObject` **[已完成]**
|
|
47
|
+
|
|
48
|
+
**现状**:项目使用 `generateText` 进行总结,但未使用结构化输出
|
|
49
|
+
|
|
50
|
+
**优化位置**:`src/core/agent-chat.js`
|
|
51
|
+
|
|
52
|
+
**已实现功能**:
|
|
53
|
+
|
|
54
|
+
- ✅ `IntentClassificationSchema` - 意图识别 Zod Schema
|
|
55
|
+
- ✅ `TaskStructuredSchema` - 任务结构化解析 Schema
|
|
56
|
+
- ✅ `SummarySchema` - 摘要生成 Schema
|
|
57
|
+
- ✅ `_classifyIntent()` - 意图分类方法
|
|
58
|
+
- ✅ `_parseStructuredTask()` - 任务结构化解析方法
|
|
59
|
+
- ✅ `_summarizeMessages()` - 智能摘要方法(使用结构化输出)
|
|
60
|
+
- ✅ `configureIntentClassification()` - 意图识别配置方法
|
|
61
|
+
- ✅ `getLastIntent()` - 获取上次意图识别结果
|
|
62
|
+
- ✅ `getCacheStatus()` - 缓存状态查询
|
|
63
|
+
|
|
64
|
+
**收益**:
|
|
65
|
+
|
|
66
|
+
- 输出类型安全,减少解析错误
|
|
67
|
+
- 支持智能意图路由
|
|
68
|
+
- 为任务分解提供结构化基础
|
|
69
|
+
- 可根据意图自动调整 `maxSteps`
|
|
70
|
+
|
|
71
|
+
---
|
|
72
|
+
|
|
73
|
+
#### ✅ 2.2 Prompt Caching 集成 **[已完成]**
|
|
74
|
+
|
|
75
|
+
**现状**:使用 tiktoken 手动计算 token,未利用 AI SDK 原生缓存
|
|
76
|
+
|
|
77
|
+
**优化位置**:`src/core/agent-chat.js`
|
|
78
|
+
|
|
79
|
+
**已实现功能**:
|
|
80
|
+
|
|
81
|
+
- ✅ `_supportsPromptCache()` - 检测模型是否支持 Prompt Caching
|
|
82
|
+
- ✅ `_buildSystemPrompt()` - 构建可缓存的系统提示
|
|
83
|
+
- ✅ `_applyPromptCacheToMessages()` - 应用缓存标记到消息
|
|
84
|
+
- ✅ `_getSystemPrompt()` - 获取缓存的系统提示
|
|
85
|
+
- ✅ `configurePromptCache()` - Prompt Cache 配置方法
|
|
86
|
+
- ✅ `getCacheStatus()` - 缓存状态查询
|
|
87
|
+
|
|
88
|
+
**收益**:
|
|
89
|
+
|
|
90
|
+
- 降低 30-50% token 消耗
|
|
91
|
+
- 加快响应速度
|
|
92
|
+
- 减少 API 调用成本
|
|
93
|
+
- 支持模型自动检测
|
|
94
|
+
|
|
95
|
+
---
|
|
96
|
+
|
|
97
|
+
### 🟡 【中优先级】
|
|
98
|
+
|
|
99
|
+
#### 2.3 流式响应增强 - `smoothStream` / `streamObject`
|
|
100
|
+
|
|
101
|
+
**现状**:流式使用 `stream` 但未使用平滑输出
|
|
102
|
+
|
|
103
|
+
**优化位置**:`src/core/agent-chat.js`
|
|
104
|
+
|
|
105
|
+
**建议修改**:
|
|
106
|
+
|
|
107
|
+
```javascript
|
|
108
|
+
// 2.3.1 平滑流式输出
|
|
109
|
+
async *chatStream(message, options = {}) {
|
|
110
|
+
// ...
|
|
111
|
+
|
|
112
|
+
const result = await framework.runWithContext(context, async () => {
|
|
113
|
+
return agent.stream({
|
|
114
|
+
messages,
|
|
115
|
+
...this.providerOptions,
|
|
116
|
+
...smoothStream({
|
|
117
|
+
delayInMs: 14 // 14ms 平滑间隔
|
|
118
|
+
})
|
|
119
|
+
})
|
|
120
|
+
})
|
|
121
|
+
|
|
122
|
+
// ...
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
// 2.3.2 结构化流式输出 - 新增方法
|
|
126
|
+
async *streamStructured(message, schema, options = {}) {
|
|
127
|
+
const { streamObject } = require('ai')
|
|
128
|
+
|
|
129
|
+
const { partialObjectStream } = await streamObject({
|
|
130
|
+
model: this._aiClient,
|
|
131
|
+
schema,
|
|
132
|
+
prompt: message,
|
|
133
|
+
...smoothStream({ delayInMs: 14 })
|
|
134
|
+
})
|
|
135
|
+
|
|
136
|
+
for await (const partialObject of partialObjectStream) {
|
|
137
|
+
yield { type: 'partial', data: partialObject }
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
```
|
|
141
|
+
|
|
142
|
+
**收益**:
|
|
143
|
+
|
|
144
|
+
- 更流畅的实时输出体验
|
|
145
|
+
- 支持部分对象流式渲染
|
|
146
|
+
|
|
147
|
+
---
|
|
148
|
+
|
|
149
|
+
#### 2.4 推理模型支持 - o1/o3/R1
|
|
150
|
+
|
|
151
|
+
**现状**:未针对推理模型配置
|
|
152
|
+
|
|
153
|
+
**优化位置**:`src/core/provider.js` 和 `src/core/agent-chat.js`
|
|
154
|
+
|
|
155
|
+
**建议修改**:
|
|
156
|
+
|
|
157
|
+
```javascript
|
|
158
|
+
// 2.4.1 provider.js - 新增推理模型配置
|
|
159
|
+
const REASONING_MODELS = {
|
|
160
|
+
'o1-mini': { type: 'reasoning', supportsTools: false, maxTokens: 25000 },
|
|
161
|
+
'o1-preview': { type: 'reasoning', supportsTools: false, maxTokens: 25000 },
|
|
162
|
+
'o3-mini': { type: 'reasoning', supportsTools: false, maxTokens: 100000 },
|
|
163
|
+
'deepseek-r1': { type: 'reasoning', supportsTools: true, maxTokens: 64000 },
|
|
164
|
+
'deepseek-r1-distill-qwen-32b': { type: 'reasoning', supportsTools: true, maxTokens: 32000 }
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
// 2.4.2 agent-chat.js - 根据模型类型调整配置
|
|
168
|
+
_getAgentConfig() {
|
|
169
|
+
const modelId = this.model.toLowerCase()
|
|
170
|
+
|
|
171
|
+
// 检测是否是推理模型
|
|
172
|
+
const isReasoningModel = Object.keys(REASONING_MODELS)
|
|
173
|
+
.some(name => modelId.includes(name))
|
|
174
|
+
|
|
175
|
+
if (isReasoningModel) {
|
|
176
|
+
const config = REASONING_MODELS[Object.keys(REASONING_MODELS)
|
|
177
|
+
.find(name => modelId.includes(name))]
|
|
178
|
+
|
|
179
|
+
return {
|
|
180
|
+
// 推理模型通常不需要 system prompt
|
|
181
|
+
includeSystemInMessages: false,
|
|
182
|
+
// 推理模型不支持工具或需要特殊处理
|
|
183
|
+
supportsTools: config.supportsTools,
|
|
184
|
+
// 增大 maxTokens
|
|
185
|
+
maxTokens: config.maxTokens
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
return {
|
|
190
|
+
includeSystemInMessages: true,
|
|
191
|
+
supportsTools: true,
|
|
192
|
+
maxTokens: 8192
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
```
|
|
196
|
+
|
|
197
|
+
**收益**:
|
|
198
|
+
|
|
199
|
+
- 支持 o1/o3/DeepSeek R1 等推理模型
|
|
200
|
+
- 自动适配推理模型的特殊配置
|
|
201
|
+
- 优化推理任务的性能
|
|
202
|
+
|
|
203
|
+
---
|
|
204
|
+
|
|
205
|
+
#### 2.5 中间件系统
|
|
206
|
+
|
|
207
|
+
**现状**:缺乏统一的中间件机制
|
|
208
|
+
|
|
209
|
+
**优化位置**:新增 `src/middleware/ai-middleware.js`
|
|
210
|
+
|
|
211
|
+
**建议新增**:
|
|
212
|
+
|
|
213
|
+
```javascript
|
|
214
|
+
// src/middleware/ai-middleware.js
|
|
215
|
+
const { extractReasoningMiddleware, defaultSettingsMiddleware, withTelemetry } = require('ai');
|
|
216
|
+
|
|
217
|
+
/**
|
|
218
|
+
* 创建 AI 中间件
|
|
219
|
+
*/
|
|
220
|
+
function createAIMiddleware(options = {}) {
|
|
221
|
+
const middlewares = [];
|
|
222
|
+
|
|
223
|
+
// 1. 推理提取中间件
|
|
224
|
+
if (options.extractReasoning !== false) {
|
|
225
|
+
middlewares.push(
|
|
226
|
+
extractReasoningMiddleware({
|
|
227
|
+
tagName: 'thinking',
|
|
228
|
+
onChunk: (chunk) => {
|
|
229
|
+
options.onReasoning?.(chunk);
|
|
230
|
+
},
|
|
231
|
+
})
|
|
232
|
+
);
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
// 2. 默认设置中间件
|
|
236
|
+
middlewares.push(
|
|
237
|
+
defaultSettingsMiddleware({
|
|
238
|
+
settings: {
|
|
239
|
+
[options.defaultModel]: {
|
|
240
|
+
temperature: options.temperature || 0.7,
|
|
241
|
+
maxTokens: options.maxTokens || 8192,
|
|
242
|
+
},
|
|
243
|
+
},
|
|
244
|
+
})
|
|
245
|
+
);
|
|
246
|
+
|
|
247
|
+
// 3. 遥测中间件
|
|
248
|
+
if (options.telemetry) {
|
|
249
|
+
middlewares.push(
|
|
250
|
+
withTelemetry({
|
|
251
|
+
serviceName: 'foliko-agent',
|
|
252
|
+
metadata: {
|
|
253
|
+
version: options.version,
|
|
254
|
+
sessionId: options.sessionId,
|
|
255
|
+
},
|
|
256
|
+
})
|
|
257
|
+
);
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
return middlewares;
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
/**
|
|
264
|
+
* 创建日志中间件(自定义)
|
|
265
|
+
*/
|
|
266
|
+
function createLoggingMiddleware() {
|
|
267
|
+
return {
|
|
268
|
+
async wrapModel(model, { doStream }) {
|
|
269
|
+
return {
|
|
270
|
+
doStream: async (prompt, options) => {
|
|
271
|
+
const startTime = Date.now();
|
|
272
|
+
console.log('[AI] Request started:', {
|
|
273
|
+
promptLength: typeof prompt === 'string' ? prompt.length : 'array',
|
|
274
|
+
});
|
|
275
|
+
|
|
276
|
+
try {
|
|
277
|
+
const result = await doStream();
|
|
278
|
+
const duration = Date.now() - startTime;
|
|
279
|
+
console.log('[AI] Request completed:', { duration });
|
|
280
|
+
return result;
|
|
281
|
+
} catch (error) {
|
|
282
|
+
console.error('[AI] Request failed:', error.message);
|
|
283
|
+
throw error;
|
|
284
|
+
}
|
|
285
|
+
},
|
|
286
|
+
};
|
|
287
|
+
},
|
|
288
|
+
};
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
module.exports = {
|
|
292
|
+
createAIMiddleware,
|
|
293
|
+
createLoggingMiddleware,
|
|
294
|
+
};
|
|
295
|
+
```
|
|
296
|
+
|
|
297
|
+
**收益**:
|
|
298
|
+
|
|
299
|
+
- 统一的日志、监控、遥测
|
|
300
|
+
- 推理过程可追踪
|
|
301
|
+
- 支持自定义中间件扩展
|
|
302
|
+
|
|
303
|
+
---
|
|
304
|
+
|
|
305
|
+
#### 2.6 Embeddings 支持
|
|
306
|
+
|
|
307
|
+
**现状**:未实现嵌入功能
|
|
308
|
+
|
|
309
|
+
**优化位置**:新增到 `src/plugins/ai-plugin.js` 或创建新文件
|
|
310
|
+
|
|
311
|
+
**建议新增**:
|
|
312
|
+
|
|
313
|
+
```javascript
|
|
314
|
+
// src/plugins/embeddings-plugin.js 或在 ai-plugin.js 中新增
|
|
315
|
+
const { embed, embedMany } = require('ai');
|
|
316
|
+
|
|
317
|
+
class AIPlugin extends Plugin {
|
|
318
|
+
// ... 现有代码
|
|
319
|
+
|
|
320
|
+
/**
|
|
321
|
+
* 获取嵌入模型
|
|
322
|
+
*/
|
|
323
|
+
getEmbeddingModel() {
|
|
324
|
+
if (this._embeddingModel) return this._embeddingModel;
|
|
325
|
+
|
|
326
|
+
const embeddingModelId = this._getEmbeddingModelId();
|
|
327
|
+
this._embeddingModel = this._embeddingProvider.textEmbeddingModel(embeddingModelId);
|
|
328
|
+
|
|
329
|
+
return this._embeddingModel;
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
_getEmbeddingModelId() {
|
|
333
|
+
const provider = this.config.provider?.toLowerCase();
|
|
334
|
+
const modelMap = {
|
|
335
|
+
openai: 'text-embedding-3-small',
|
|
336
|
+
deepseek: 'deepseek-embed',
|
|
337
|
+
ollama: 'nomic-embed-text',
|
|
338
|
+
};
|
|
339
|
+
return modelMap[provider] || 'text-embedding-3-small';
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
/**
|
|
343
|
+
* 嵌入单个文本
|
|
344
|
+
*/
|
|
345
|
+
async embedText(text) {
|
|
346
|
+
const { embedding } = await embed({
|
|
347
|
+
model: this.getEmbeddingModel(),
|
|
348
|
+
value: text,
|
|
349
|
+
});
|
|
350
|
+
return embedding;
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
/**
|
|
354
|
+
* 批量嵌入
|
|
355
|
+
*/
|
|
356
|
+
async embedTexts(texts) {
|
|
357
|
+
const { embeddings } = await embedMany({
|
|
358
|
+
model: this.getEmbeddingModel(),
|
|
359
|
+
values: texts,
|
|
360
|
+
});
|
|
361
|
+
return embeddings;
|
|
362
|
+
}
|
|
363
|
+
}
|
|
364
|
+
```
|
|
365
|
+
|
|
366
|
+
**收益**:
|
|
367
|
+
|
|
368
|
+
- 支持 RAG 知识库构建
|
|
369
|
+
- 语义搜索基础
|
|
370
|
+
- 文本相似度计算
|
|
371
|
+
|
|
372
|
+
---
|
|
373
|
+
|
|
374
|
+
### 🟢 【低优先级】
|
|
375
|
+
|
|
376
|
+
#### 2.7 错误处理增强
|
|
377
|
+
|
|
378
|
+
**现状**:基础错误捕获
|
|
379
|
+
|
|
380
|
+
**优化位置**:`src/core/agent-chat.js`
|
|
381
|
+
|
|
382
|
+
**建议修改**:
|
|
383
|
+
|
|
384
|
+
```javascript
|
|
385
|
+
// 2.7.1 新增错误类型导入
|
|
386
|
+
const {
|
|
387
|
+
AI_APICallError,
|
|
388
|
+
AI_RetryableError,
|
|
389
|
+
AI_NoSuchModelError,
|
|
390
|
+
AIInvalidPromptError
|
|
391
|
+
} = require('ai')
|
|
392
|
+
|
|
393
|
+
// 2.7.2 增强错误处理
|
|
394
|
+
async chat(message, options = {}) {
|
|
395
|
+
try {
|
|
396
|
+
// ... 现有代码
|
|
397
|
+
} catch (error) {
|
|
398
|
+
if (error instanceof AI_APICallError) {
|
|
399
|
+
switch (error.statusCode) {
|
|
400
|
+
case 400:
|
|
401
|
+
throw new Error(`无效请求: ${error.message}`)
|
|
402
|
+
case 401:
|
|
403
|
+
throw new Error('API 密钥无效或已过期')
|
|
404
|
+
case 429:
|
|
405
|
+
throw new Error('请求频率超限,请稍后重试')
|
|
406
|
+
case 500:
|
|
407
|
+
case 502:
|
|
408
|
+
case 503:
|
|
409
|
+
throw new Error(`AI 服务暂时不可用: ${error.message}`)
|
|
410
|
+
default:
|
|
411
|
+
throw error
|
|
412
|
+
}
|
|
413
|
+
} else if (error instanceof AI_NoSuchModelError) {
|
|
414
|
+
throw new Error(`模型 ${this.model} 不存在或不可用`)
|
|
415
|
+
} else if (error instanceof AIInvalidPromptError) {
|
|
416
|
+
throw new Error(`提示词无效: ${error.message}`)
|
|
417
|
+
}
|
|
418
|
+
throw error
|
|
419
|
+
}
|
|
420
|
+
}
|
|
421
|
+
|
|
422
|
+
// 2.7.3 重试装饰器
|
|
423
|
+
async withRetry(fn, maxRetries = 3, delayMs = 1000) {
|
|
424
|
+
for (let i = 0; i < maxRetries; i++) {
|
|
425
|
+
try {
|
|
426
|
+
return await fn()
|
|
427
|
+
} catch (error) {
|
|
428
|
+
if (i === maxRetries - 1) throw error
|
|
429
|
+
if (!this._isRetryable(error)) throw error
|
|
430
|
+
|
|
431
|
+
console.log(`[AgentChat] Retry ${i + 1}/${maxRetries} after ${delayMs}ms`)
|
|
432
|
+
await new Promise(r => setTimeout(r, delayMs))
|
|
433
|
+
delayMs *= 2 // 指数退避
|
|
434
|
+
}
|
|
435
|
+
}
|
|
436
|
+
}
|
|
437
|
+
|
|
438
|
+
_isRetryable(error) {
|
|
439
|
+
if (error instanceof AI_APICallError) {
|
|
440
|
+
return [429, 500, 502, 503].includes(error.statusCode)
|
|
441
|
+
}
|
|
442
|
+
return false
|
|
443
|
+
}
|
|
444
|
+
```
|
|
445
|
+
|
|
446
|
+
**收益**:
|
|
447
|
+
|
|
448
|
+
- 更健壮的错误恢复
|
|
449
|
+
- 用户友好的错误提示
|
|
450
|
+
- 自动重试机制
|
|
451
|
+
|
|
452
|
+
---
|
|
453
|
+
|
|
454
|
+
#### 2.8 MCP Elicitation 支持
|
|
455
|
+
|
|
456
|
+
**现状**:使用 MCP 但未支持用户确认
|
|
457
|
+
|
|
458
|
+
**优化位置**:`src/executors/mcp-executor.js`
|
|
459
|
+
|
|
460
|
+
**建议修改**:
|
|
461
|
+
|
|
462
|
+
```javascript
|
|
463
|
+
// 2.8.1 新增 MCP Elicitation 支持
|
|
464
|
+
async connect() {
|
|
465
|
+
// ... 现有代码
|
|
466
|
+
|
|
467
|
+
// 启用 Elicitation(用户确认)
|
|
468
|
+
if (this.client && typeof this.client.requestConfirmation === 'function') {
|
|
469
|
+
this._enableElicitation = true
|
|
470
|
+
}
|
|
471
|
+
}
|
|
472
|
+
|
|
473
|
+
// 2.8.2 工具调用时请求确认
|
|
474
|
+
async executeToolWithConfirmation(toolName, args) {
|
|
475
|
+
if (!this._enableElicitation) {
|
|
476
|
+
return this.executeTool(toolName, args)
|
|
477
|
+
}
|
|
478
|
+
|
|
479
|
+
// 发送确认请求给用户
|
|
480
|
+
const confirmed = await this._requestUserConfirmation({
|
|
481
|
+
toolName,
|
|
482
|
+
args,
|
|
483
|
+
description: this._getToolDescription(toolName)
|
|
484
|
+
})
|
|
485
|
+
|
|
486
|
+
if (!confirmed) {
|
|
487
|
+
return { error: 'User rejected tool execution' }
|
|
488
|
+
}
|
|
489
|
+
|
|
490
|
+
return this.executeTool(toolName, args)
|
|
491
|
+
}
|
|
492
|
+
```
|
|
493
|
+
|
|
494
|
+
**收益**:
|
|
495
|
+
|
|
496
|
+
- 用户可控制工具执行
|
|
497
|
+
- 提高安全性
|
|
498
|
+
- 支持敏感操作确认
|
|
499
|
+
|
|
500
|
+
---
|
|
501
|
+
|
|
502
|
+
## 三、优化优先级汇总
|
|
503
|
+
|
|
504
|
+
| 优先级 | 优化项 | 状态 | 收益 | 影响范围 |
|
|
505
|
+
| ------ | ------------------------- | --------- | ------------------ | ------------------------------ |
|
|
506
|
+
| 🔴 高 | `generateObject` 意图识别 | ✅ 已完成 | 类型安全、智能路由 | `agent-chat.js` |
|
|
507
|
+
| 🔴 高 | Prompt Caching | ✅ 已完成 | 降低30% token消耗 | `agent-chat.js` |
|
|
508
|
+
| 🟡 中 | `smoothStream` 流式增强 | 待做 | 更流畅体验 | `agent-chat.js` |
|
|
509
|
+
| 🟡 中 | 推理模型支持 | 待做 | o1/R1 优化 | `provider.js`, `agent-chat.js` |
|
|
510
|
+
| 🟡 中 | 中间件系统 | 待做 | 统一监控/日志 | 新建 `middleware/` |
|
|
511
|
+
| 🟡 中 | Embeddings | 待做 | RAG 基础 | `ai-plugin.js` |
|
|
512
|
+
| 🟢 低 | 错误处理增强 | 待做 | 更健壮 | `agent-chat.js` |
|
|
513
|
+
| 🟢 低 | MCP Elicitation | 待做 | 用户控制 | `mcp-executor.js` |
|
|
514
|
+
|
|
515
|
+
---
|
|
516
|
+
|
|
517
|
+
## 四、实施路线图
|
|
518
|
+
|
|
519
|
+
### 第一阶段(立即可做)- ✅ 已完成
|
|
520
|
+
|
|
521
|
+
```
|
|
522
|
+
✅ generateObject 意图识别
|
|
523
|
+
└── 在 agent-chat.js 新增 _classifyIntent() 方法
|
|
524
|
+
└── 新增 IntentClassificationSchema, TaskStructuredSchema, SummarySchema
|
|
525
|
+
└── 新增 configureIntentClassification(), getLastIntent(), getCacheStatus()
|
|
526
|
+
|
|
527
|
+
✅ Prompt Caching 集成
|
|
528
|
+
└── 在 _buildSystemPrompt() 中使用缓存机制
|
|
529
|
+
└── 新增 _supportsPromptCache(), _applyPromptCacheToMessages()
|
|
530
|
+
└── 新增 configurePromptCache() 配置方法
|
|
531
|
+
```
|
|
532
|
+
|
|
533
|
+
### 第二阶段(1周内)
|
|
534
|
+
|
|
535
|
+
```
|
|
536
|
+
📦 smoothStream 流式增强
|
|
537
|
+
└── 在 stream() 中添加 smoothStream 配置
|
|
538
|
+
|
|
539
|
+
📦 推理模型支持
|
|
540
|
+
└── 在 provider.js 新增 REASONING_MODELS 配置
|
|
541
|
+
|
|
542
|
+
📦 错误处理增强
|
|
543
|
+
└── 使用 AI SDK 错误类型替换基础 catch
|
|
544
|
+
```
|
|
545
|
+
|
|
546
|
+
### 第三阶段(长期)
|
|
547
|
+
|
|
548
|
+
```
|
|
549
|
+
📦 中间件系统
|
|
550
|
+
└── 新建 src/middleware/ai-middleware.js
|
|
551
|
+
|
|
552
|
+
📦 Embeddings 支持
|
|
553
|
+
└── 在 ai-plugin.js 新增 embedText/embedTexts 方法
|
|
554
|
+
|
|
555
|
+
📦 MCP Elicitation
|
|
556
|
+
└── 在 mcp-executor.js 新增用户确认机制
|
|
557
|
+
```
|
|
558
|
+
|
|
559
|
+
---
|
|
560
|
+
|
|
561
|
+
## 五、新增配置选项
|
|
562
|
+
|
|
563
|
+
### 意图识别配置
|
|
564
|
+
|
|
565
|
+
```javascript
|
|
566
|
+
const agent = new AgentChatHandler(agent, {
|
|
567
|
+
// 意图识别(默认开启)
|
|
568
|
+
enableIntentClassification: true,
|
|
569
|
+
// 可选:用于意图识别的专用模型
|
|
570
|
+
intentModel: openai('gpt-4o-mini'),
|
|
571
|
+
});
|
|
572
|
+
|
|
573
|
+
// 运行时配置
|
|
574
|
+
agent.configureIntentClassification({
|
|
575
|
+
enabled: true,
|
|
576
|
+
model: openai('gpt-4o-mini'),
|
|
577
|
+
});
|
|
578
|
+
|
|
579
|
+
// 获取上次意图识别结果
|
|
580
|
+
const intent = agent.getLastIntent();
|
|
581
|
+
// { intent: 'create', confidence: 0.95, entities: [...], suggestedTools: [...] }
|
|
582
|
+
```
|
|
583
|
+
|
|
584
|
+
### Prompt Caching 配置
|
|
585
|
+
|
|
586
|
+
```javascript
|
|
587
|
+
const agent = new AgentChatHandler(agent, {
|
|
588
|
+
// Prompt Caching(默认开启)
|
|
589
|
+
enablePromptCache: true,
|
|
590
|
+
// 缓存有效期(默认 1 小时)
|
|
591
|
+
cacheMaxAgeMs: 1000 * 60 * 60,
|
|
592
|
+
});
|
|
593
|
+
|
|
594
|
+
// 运行时配置
|
|
595
|
+
agent.configurePromptCache({
|
|
596
|
+
enabled: true,
|
|
597
|
+
maxAgeMs: 1000 * 60 * 30, // 30 分钟
|
|
598
|
+
});
|
|
599
|
+
|
|
600
|
+
// 查看缓存状态
|
|
601
|
+
const status = agent.getCacheStatus();
|
|
602
|
+
// { promptCache: { enabled: true, supported: true, ... }, intentClassification: {...} }
|
|
603
|
+
```
|
|
604
|
+
|
|
605
|
+
### chat() / chatStream() 返回值变化
|
|
606
|
+
|
|
607
|
+
```javascript
|
|
608
|
+
// chat() 返回值新增 intent 字段
|
|
609
|
+
const result = await agent.chat('帮我创建一个文件');
|
|
610
|
+
// { success: true, message: "...", stepCount: 3, intent: { intent: 'create', confidence: 0.9, ... } }
|
|
611
|
+
|
|
612
|
+
// chatStream() 支持接收 intent 事件
|
|
613
|
+
for await (const chunk of agent.chatStream('查询天气')) {
|
|
614
|
+
if (chunk.type === 'intent') {
|
|
615
|
+
console.log('意图:', chunk.intent); // { intent: 'query', confidence: 0.95, ... }
|
|
616
|
+
}
|
|
617
|
+
}
|
|
618
|
+
|
|
619
|
+
// 监听意图识别事件
|
|
620
|
+
agent.on('intent-classified', ({ message, intent }) => {
|
|
621
|
+
console.log(`消息 "${message}" 被识别为: ${intent.intent}`);
|
|
622
|
+
});
|
|
623
|
+
|
|
624
|
+
agent.on('intent-detected', ({ message, intent }) => {
|
|
625
|
+
console.log(`流式模式检测到意图: ${intent.intent}`);
|
|
626
|
+
});
|
|
627
|
+
```
|
|
628
|
+
|
|
629
|
+
---
|
|
630
|
+
|
|
631
|
+
## 六、需要安装的包
|
|
632
|
+
|
|
633
|
+
```bash
|
|
634
|
+
# 基础已安装
|
|
635
|
+
npm install ai @ai-sdk/openai @ai-sdk/anthropic
|
|
636
|
+
|
|
637
|
+
# 可能需要添加(根据需求)
|
|
638
|
+
npm install @ai-sdk/google # Gemini 模型支持
|
|
639
|
+
npm install @ai-sdk/deepseek # DeepSeek 模型支持
|
|
640
|
+
npm install @ai-sdk/amazon-bedrock # AWS Bedrock 支持
|
|
641
|
+
```
|
|
642
|
+
|
|
643
|
+
---
|
|
644
|
+
|
|
645
|
+
## 七、参考资料
|
|
646
|
+
|
|
647
|
+
- [AI SDK 官方文档](https://ai-sdk.dev/docs/getting-started)
|
|
648
|
+
- [AI SDK Cookbook](https://ai-sdk.dev/cookbook)
|
|
649
|
+
- [AI SDK API Reference](https://ai-sdk.dev/docs/api-reference)
|
|
650
|
+
|
|
651
|
+
---
|
|
652
|
+
|
|
653
|
+
_文档生成时间:2025-01-25_
|
|
654
|
+
_基于 AI SDK v6 版本分析_
|
|
655
|
+
_高优先级优化完成时间:2025-01-26_
|