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