foliko 1.0.73 → 1.0.75

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