@ai-setting/roy-agent-core 1.0.0

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 (378) hide show
  1. package/dist/index.js +99145 -0
  2. package/package.json +114 -0
  3. package/src/config/config-component.test.ts +627 -0
  4. package/src/config/config-component.ts +906 -0
  5. package/src/config/config-parser.test.ts +319 -0
  6. package/src/config/config-parser.ts +203 -0
  7. package/src/config/decentralized-config.test.ts +740 -0
  8. package/src/config/env-key.ts +210 -0
  9. package/src/config/env-source.test.ts +252 -0
  10. package/src/config/env-source.ts +301 -0
  11. package/src/config/file-source.test.ts +357 -0
  12. package/src/config/file-source.ts +421 -0
  13. package/src/config/index.ts +24 -0
  14. package/src/config/protocol-resolver.test.ts +217 -0
  15. package/src/config/protocol-resolver.ts +228 -0
  16. package/src/env/agent/agent-component.abort.test.ts +511 -0
  17. package/src/env/agent/agent-component.record-session.test.ts +349 -0
  18. package/src/env/agent/agent-component.test.ts +1389 -0
  19. package/src/env/agent/agent-component.tool-error.test.ts +327 -0
  20. package/src/env/agent/agent-component.ts +1711 -0
  21. package/src/env/agent/agent-config-registration.test.ts +226 -0
  22. package/src/env/agent/agent-config-registration.ts +46 -0
  23. package/src/env/agent/agent-reminder-plugin.integration.test.ts +243 -0
  24. package/src/env/agent/index.ts +10 -0
  25. package/src/env/agent/summary-agent.parse-hint.test.ts +360 -0
  26. package/src/env/agent/summary-agent.ts +508 -0
  27. package/src/env/agent/types.ts +536 -0
  28. package/src/env/commands/commands-component.test.ts +364 -0
  29. package/src/env/commands/commands-component.ts +604 -0
  30. package/src/env/commands/commands-config-registration.test.ts +198 -0
  31. package/src/env/commands/commands-config-registration.ts +38 -0
  32. package/src/env/commands/index.ts +21 -0
  33. package/src/env/commands/parser.test.ts +203 -0
  34. package/src/env/commands/parser.ts +115 -0
  35. package/src/env/commands/types.ts +184 -0
  36. package/src/env/commands-prompt-integration.test.ts +243 -0
  37. package/src/env/component-env.test.ts +119 -0
  38. package/src/env/component.ts +335 -0
  39. package/src/env/constants.test.ts +72 -0
  40. package/src/env/constants.ts +123 -0
  41. package/src/env/debug/debug-component.test.ts +114 -0
  42. package/src/env/debug/debug-component.ts +547 -0
  43. package/src/env/debug/formatters/index.ts +9 -0
  44. package/src/env/debug/formatters/repl-formatter.test.ts +139 -0
  45. package/src/env/debug/formatters/repl-formatter.ts +358 -0
  46. package/src/env/debug/formatters/trace-formatter.test.ts +119 -0
  47. package/src/env/debug/formatters/trace-formatter.ts +191 -0
  48. package/src/env/debug/formatters/tree-formatter.test.ts +107 -0
  49. package/src/env/debug/formatters/tree-formatter.ts +325 -0
  50. package/src/env/debug/index.ts +38 -0
  51. package/src/env/debug/parser/regex-parser.test.ts +201 -0
  52. package/src/env/debug/parser/regex-parser.ts +196 -0
  53. package/src/env/debug/parser/span-builder.test.ts +241 -0
  54. package/src/env/debug/parser/span-builder.ts +386 -0
  55. package/src/env/debug/reader/log-reader.test.ts +170 -0
  56. package/src/env/debug/reader/log-reader.ts +186 -0
  57. package/src/env/debug/reader/span-db-reader.test.ts +118 -0
  58. package/src/env/debug/reader/span-db-reader.ts +201 -0
  59. package/src/env/debug/types.test.ts +187 -0
  60. package/src/env/debug/types.ts +171 -0
  61. package/src/env/environment-init.test.ts +183 -0
  62. package/src/env/environment-lifecycle.test.ts +516 -0
  63. package/src/env/environment-service.test.ts +332 -0
  64. package/src/env/environment.handle-query.test.ts +96 -0
  65. package/src/env/environment.test.ts +232 -0
  66. package/src/env/environment.ts +708 -0
  67. package/src/env/errors.test.ts +165 -0
  68. package/src/env/errors.ts +157 -0
  69. package/src/env/event-source/event-source-agent-handler.test.ts +193 -0
  70. package/src/env/event-source/event-source-agent-handler.ts +111 -0
  71. package/src/env/event-source/event-source-component.process-cleanup.test.ts +236 -0
  72. package/src/env/event-source/event-source-component.stop.test.ts +346 -0
  73. package/src/env/event-source/event-source-component.test.ts +1207 -0
  74. package/src/env/event-source/event-source-component.ts +1379 -0
  75. package/src/env/event-source/event-source-config-registration.test.ts +242 -0
  76. package/src/env/event-source/event-source-config-registration.ts +37 -0
  77. package/src/env/event-source/event-source-integration.test.ts +320 -0
  78. package/src/env/event-source/event-source-platform.test.ts +630 -0
  79. package/src/env/event-source/types.ts +298 -0
  80. package/src/env/hook/global-hook-manager.ts +162 -0
  81. package/src/env/hook/hook-manager.test.ts +374 -0
  82. package/src/env/hook/hook-manager.ts +309 -0
  83. package/src/env/hook/index.ts +38 -0
  84. package/src/env/hook/types.ts +138 -0
  85. package/src/env/index.ts +144 -0
  86. package/src/env/interface.ts +203 -0
  87. package/src/env/llm/hooks.test.ts +293 -0
  88. package/src/env/llm/hooks.ts +316 -0
  89. package/src/env/llm/index.ts +61 -0
  90. package/src/env/llm/invoke-threshold-check.test.ts +88 -0
  91. package/src/env/llm/invoke-timeout.test.ts +54 -0
  92. package/src/env/llm/invoke.test.ts +71 -0
  93. package/src/env/llm/invoke.ts +1039 -0
  94. package/src/env/llm/llm-config.test.ts +523 -0
  95. package/src/env/llm/llm.test.ts +233 -0
  96. package/src/env/llm/llm.ts +568 -0
  97. package/src/env/llm/provider.test.ts +182 -0
  98. package/src/env/llm/provider.ts +108 -0
  99. package/src/env/llm/transform.test.ts +251 -0
  100. package/src/env/llm/transform.ts +286 -0
  101. package/src/env/llm/types.test.ts +580 -0
  102. package/src/env/llm/types.ts +424 -0
  103. package/src/env/log-trace/decorator-otel.test.ts +182 -0
  104. package/src/env/log-trace/decorator.ts +230 -0
  105. package/src/env/log-trace/index.ts +79 -0
  106. package/src/env/log-trace/log-trace-component.test.ts +242 -0
  107. package/src/env/log-trace/log-trace-component.ts +497 -0
  108. package/src/env/log-trace/log-trace-config-registration.test.ts +348 -0
  109. package/src/env/log-trace/log-trace-config-registration.ts +45 -0
  110. package/src/env/log-trace/logger.test.ts +149 -0
  111. package/src/env/log-trace/logger.ts +522 -0
  112. package/src/env/log-trace/opentelemetry/cli-propagation.test.ts +147 -0
  113. package/src/env/log-trace/opentelemetry/cli-propagation.ts +194 -0
  114. package/src/env/log-trace/opentelemetry/integration.test.ts +668 -0
  115. package/src/env/log-trace/opentelemetry/mod.ts +25 -0
  116. package/src/env/log-trace/opentelemetry/propagation-env.test.ts +181 -0
  117. package/src/env/log-trace/opentelemetry/propagation-env.ts +136 -0
  118. package/src/env/log-trace/opentelemetry/propagation.test.ts +259 -0
  119. package/src/env/log-trace/opentelemetry/propagation.ts +215 -0
  120. package/src/env/log-trace/opentelemetry/tracer-provider-context.test.ts +166 -0
  121. package/src/env/log-trace/opentelemetry/tracer-provider.test.ts +379 -0
  122. package/src/env/log-trace/opentelemetry/tracer-provider.ts +612 -0
  123. package/src/env/log-trace/span-storage.test.ts +145 -0
  124. package/src/env/log-trace/span-storage.ts +230 -0
  125. package/src/env/log-trace/trace-context.test.ts +187 -0
  126. package/src/env/log-trace/trace-context.ts +162 -0
  127. package/src/env/log-trace/types.test.ts +63 -0
  128. package/src/env/log-trace/types.ts +172 -0
  129. package/src/env/mcp/README.md +244 -0
  130. package/src/env/mcp/__integration__/mcp-component.integration.test.ts +373 -0
  131. package/src/env/mcp/config.test.ts +74 -0
  132. package/src/env/mcp/config.ts +116 -0
  133. package/src/env/mcp/index.ts +41 -0
  134. package/src/env/mcp/loader.test.ts +161 -0
  135. package/src/env/mcp/loader.ts +209 -0
  136. package/src/env/mcp/mcp-component.test.ts +111 -0
  137. package/src/env/mcp/mcp-component.ts +358 -0
  138. package/src/env/mcp/mcp-config-registration.test.ts +304 -0
  139. package/src/env/mcp/mcp-config-registration.ts +50 -0
  140. package/src/env/mcp/scanner.test.ts +170 -0
  141. package/src/env/mcp/scanner.ts +246 -0
  142. package/src/env/mcp/tool/adapter.test.ts +520 -0
  143. package/src/env/mcp/tool/adapter.ts +521 -0
  144. package/src/env/mcp/tool/index.ts +5 -0
  145. package/src/env/mcp/types.test.ts +171 -0
  146. package/src/env/mcp/types.ts +79 -0
  147. package/src/env/memory/README.md +177 -0
  148. package/src/env/memory/built-in/index.ts +59 -0
  149. package/src/env/memory/built-in/recall-memory.ts +103 -0
  150. package/src/env/memory/built-in/record-memory.ts +148 -0
  151. package/src/env/memory/index.ts +20 -0
  152. package/src/env/memory/memory-component.test.ts +239 -0
  153. package/src/env/memory/memory-component.ts +503 -0
  154. package/src/env/memory/memory-config-registration.test.ts +67 -0
  155. package/src/env/memory/memory-config-registration.ts +48 -0
  156. package/src/env/memory/memory-config.ts +45 -0
  157. package/src/env/memory/memory-file.test.ts +268 -0
  158. package/src/env/memory/plugin/index.ts +48 -0
  159. package/src/env/memory/plugin/memory-agent.test.ts +249 -0
  160. package/src/env/memory/plugin/memory-agent.ts +365 -0
  161. package/src/env/memory/plugin/memory-manager.ts +198 -0
  162. package/src/env/memory/plugin/memory-plugin-agent.test.ts +145 -0
  163. package/src/env/memory/plugin/memory-plugin.ts +210 -0
  164. package/src/env/memory/plugin/plugin-simplified.test.ts +51 -0
  165. package/src/env/memory/plugin/recall-memory.test.ts +106 -0
  166. package/src/env/memory/plugin/recall-memory.ts +53 -0
  167. package/src/env/memory/plugin/types.ts +101 -0
  168. package/src/env/memory/tools/memory-agent-tools.ts +228 -0
  169. package/src/env/memory/types.ts +85 -0
  170. package/src/env/paths.ts +118 -0
  171. package/src/env/prompt/index.ts +18 -0
  172. package/src/env/prompt/memory-prompts.test.ts +91 -0
  173. package/src/env/prompt/prompt-component.test.ts +491 -0
  174. package/src/env/prompt/prompt-component.ts +619 -0
  175. package/src/env/prompt/prompt-config-registration.test.ts +213 -0
  176. package/src/env/prompt/prompt-config-registration.ts +39 -0
  177. package/src/env/prompt/prompts-index.ts +504 -0
  178. package/src/env/prompt/renderer.ts +67 -0
  179. package/src/env/prompt/types.ts +136 -0
  180. package/src/env/session/hooks.ts +18 -0
  181. package/src/env/session/index.ts +37 -0
  182. package/src/env/session/search-query-parser.test.ts +425 -0
  183. package/src/env/session/search-query-parser.ts +171 -0
  184. package/src/env/session/session-checkpoint.test.ts +523 -0
  185. package/src/env/session/session-component.extract-recent-messages.test.ts +209 -0
  186. package/src/env/session/session-component.test.ts +132 -0
  187. package/src/env/session/session-component.ts +1249 -0
  188. package/src/env/session/session-config-registration.test.ts +138 -0
  189. package/src/env/session/session-config-registration.ts +52 -0
  190. package/src/env/session/session-message-converter.test.ts +763 -0
  191. package/src/env/session/session-message-converter.ts +415 -0
  192. package/src/env/session/session-message-e2e.test.ts +448 -0
  193. package/src/env/session/session-search.test.ts +391 -0
  194. package/src/env/session/session-store.test.ts +362 -0
  195. package/src/env/session/session-store.ts +141 -0
  196. package/src/env/session/storage/index.ts +6 -0
  197. package/src/env/session/storage/memory.ts +502 -0
  198. package/src/env/session/storage/sqlite.ts +794 -0
  199. package/src/env/session/types.ts +742 -0
  200. package/src/env/skill/config.ts +39 -0
  201. package/src/env/skill/index.ts +6 -0
  202. package/src/env/skill/parser.test.ts +116 -0
  203. package/src/env/skill/parser.ts +77 -0
  204. package/src/env/skill/scanner.test.ts +211 -0
  205. package/src/env/skill/scanner.ts +119 -0
  206. package/src/env/skill/skill-component.test.ts +234 -0
  207. package/src/env/skill/skill-component.ts +352 -0
  208. package/src/env/skill/skill-config-registration.test.ts +60 -0
  209. package/src/env/skill/skill-config-registration.ts +43 -0
  210. package/src/env/skill/tool/index.ts +1 -0
  211. package/src/env/skill/tool/skill-tool.test.ts +100 -0
  212. package/src/env/skill/tool/skill-tool.ts +72 -0
  213. package/src/env/skill/types.ts +64 -0
  214. package/src/env/task/delegate/delegate-tool.test.ts +498 -0
  215. package/src/env/task/delegate/delegate-tool.ts +1014 -0
  216. package/src/env/task/delegate/index.ts +18 -0
  217. package/src/env/task/delegate/stop-tool.test.ts +140 -0
  218. package/src/env/task/delegate/stop-tool.ts +119 -0
  219. package/src/env/task/delegate/task-events.test.ts +178 -0
  220. package/src/env/task/delegate/task-events.ts +143 -0
  221. package/src/env/task/hooks/contexts.test.ts +92 -0
  222. package/src/env/task/hooks/contexts.ts +192 -0
  223. package/src/env/task/hooks/index.ts +23 -0
  224. package/src/env/task/hooks/task-hook-points.test.ts +32 -0
  225. package/src/env/task/hooks/task-hook-points.ts +54 -0
  226. package/src/env/task/index.ts +7 -0
  227. package/src/env/task/plugins/index.ts +13 -0
  228. package/src/env/task/plugins/task-plugin.test.ts +74 -0
  229. package/src/env/task/plugins/task-plugin.ts +89 -0
  230. package/src/env/task/plugins/task-tag-plugin.test.ts +377 -0
  231. package/src/env/task/plugins/task-tag-plugin.ts +319 -0
  232. package/src/env/task/plugins/task-workflow-extractor.integration.test.ts +226 -0
  233. package/src/env/task/plugins/workflow-extractor-agent.test.ts +107 -0
  234. package/src/env/task/plugins/workflow-extractor-agent.ts +225 -0
  235. package/src/env/task/storage/index.ts +6 -0
  236. package/src/env/task/storage/sqlite-task-store.test.ts +283 -0
  237. package/src/env/task/storage/sqlite-task-store.ts +903 -0
  238. package/src/env/task/storage/task-search.test.ts +291 -0
  239. package/src/env/task/tag-service.test.ts +198 -0
  240. package/src/env/task/tag-service.ts +264 -0
  241. package/src/env/task/task-component.test.ts +193 -0
  242. package/src/env/task/task-component.ts +658 -0
  243. package/src/env/task/task-config-registration.test.ts +57 -0
  244. package/src/env/task/task-config-registration.ts +37 -0
  245. package/src/env/task/task-types.test.ts +137 -0
  246. package/src/env/task/tools/complete-tool.ts +44 -0
  247. package/src/env/task/tools/create-tool.ts +49 -0
  248. package/src/env/task/tools/delete-tool.ts +43 -0
  249. package/src/env/task/tools/get-tool.ts +59 -0
  250. package/src/env/task/tools/index.ts +10 -0
  251. package/src/env/task/tools/list-tool.ts +40 -0
  252. package/src/env/task/tools/operation/create-tool.ts +48 -0
  253. package/src/env/task/tools/operation/delete-tool.ts +43 -0
  254. package/src/env/task/tools/operation/get-tool.ts +43 -0
  255. package/src/env/task/tools/operation/index.ts +9 -0
  256. package/src/env/task/tools/operation/list-tool.ts +40 -0
  257. package/src/env/task/tools/operation/operation-tools.test.ts +274 -0
  258. package/src/env/task/tools/operation/operation-types.ts +75 -0
  259. package/src/env/task/tools/operation/update-tool.ts +47 -0
  260. package/src/env/task/tools/task-tools.test.ts +203 -0
  261. package/src/env/task/tools/task-types.test.ts +75 -0
  262. package/src/env/task/tools/task-types.ts +68 -0
  263. package/src/env/task/tools/update-tool.ts +70 -0
  264. package/src/env/task/types.ts +160 -0
  265. package/src/env/tool/built-in/bash.ts +201 -0
  266. package/src/env/tool/built-in/echo.ts +29 -0
  267. package/src/env/tool/built-in/edit-file.test.ts +136 -0
  268. package/src/env/tool/built-in/edit-file.ts +92 -0
  269. package/src/env/tool/built-in/glob.test.ts +94 -0
  270. package/src/env/tool/built-in/glob.ts +65 -0
  271. package/src/env/tool/built-in/grep.test.ts +122 -0
  272. package/src/env/tool/built-in/grep.ts +108 -0
  273. package/src/env/tool/built-in/index.ts +44 -0
  274. package/src/env/tool/built-in/read-file.test.ts +84 -0
  275. package/src/env/tool/built-in/read-file.ts +75 -0
  276. package/src/env/tool/built-in/write-file.test.ts +119 -0
  277. package/src/env/tool/built-in/write-file.ts +68 -0
  278. package/src/env/tool/index.ts +24 -0
  279. package/src/env/tool/registry.test.ts +257 -0
  280. package/src/env/tool/registry.ts +167 -0
  281. package/src/env/tool/tool-component.test.ts +559 -0
  282. package/src/env/tool/tool-component.ts +563 -0
  283. package/src/env/tool/tool-config-registration.test.ts +249 -0
  284. package/src/env/tool/tool-config-registration.ts +46 -0
  285. package/src/env/tool/types.ts +267 -0
  286. package/src/env/tool/validator.test.ts +143 -0
  287. package/src/env/tool/validator.ts +44 -0
  288. package/src/env/types.ts +180 -0
  289. package/src/env/workflow/ask-user-tool-registration.test.ts +216 -0
  290. package/src/env/workflow/complex-workflow.integration.test.ts +1900 -0
  291. package/src/env/workflow/decorators/decorator-node.ts +229 -0
  292. package/src/env/workflow/decorators/decorator.test.ts +196 -0
  293. package/src/env/workflow/decorators/edge.ts +82 -0
  294. package/src/env/workflow/decorators/index.ts +31 -0
  295. package/src/env/workflow/decorators/node-as.ts +98 -0
  296. package/src/env/workflow/decorators/workflow.ts +54 -0
  297. package/src/env/workflow/engine/dag-manager.test.ts +570 -0
  298. package/src/env/workflow/engine/dag-manager.ts +594 -0
  299. package/src/env/workflow/engine/engine.ts +1422 -0
  300. package/src/env/workflow/engine/event-bus.test.ts +359 -0
  301. package/src/env/workflow/engine/event-bus.ts +156 -0
  302. package/src/env/workflow/engine/executor-agent-session.test.ts +84 -0
  303. package/src/env/workflow/engine/executor.test.ts +619 -0
  304. package/src/env/workflow/engine/executor.ts +593 -0
  305. package/src/env/workflow/engine/index.ts +24 -0
  306. package/src/env/workflow/engine/node-registry.test.ts +560 -0
  307. package/src/env/workflow/engine/node-registry.ts +289 -0
  308. package/src/env/workflow/engine/resume-removed.test.ts +22 -0
  309. package/src/env/workflow/engine/scheduler.test.ts +715 -0
  310. package/src/env/workflow/engine/scheduler.ts +318 -0
  311. package/src/env/workflow/engine/workflow-engine.test.ts +815 -0
  312. package/src/env/workflow/extractor/workflow-converter.ts +306 -0
  313. package/src/env/workflow/fixtures.ts +380 -0
  314. package/src/env/workflow/index.ts +38 -0
  315. package/src/env/workflow/integration/run-resume-unified.test.ts +186 -0
  316. package/src/env/workflow/integration/service-integration.test.ts +267 -0
  317. package/src/env/workflow/metadata/keys.ts +12 -0
  318. package/src/env/workflow/nodes/agent-component-adapter.test.ts +318 -0
  319. package/src/env/workflow/nodes/agent-component-adapter.ts +448 -0
  320. package/src/env/workflow/nodes/agent-node.test.ts +371 -0
  321. package/src/env/workflow/nodes/agent-node.ts +598 -0
  322. package/src/env/workflow/nodes/ask-user-node.ts +113 -0
  323. package/src/env/workflow/nodes/condition-node.ts +200 -0
  324. package/src/env/workflow/nodes/index.ts +9 -0
  325. package/src/env/workflow/nodes/merge-node.ts +141 -0
  326. package/src/env/workflow/nodes/skill-node.test.ts +253 -0
  327. package/src/env/workflow/nodes/skill-node.ts +393 -0
  328. package/src/env/workflow/nodes/tool-node.test.ts +251 -0
  329. package/src/env/workflow/nodes/tool-node.ts +493 -0
  330. package/src/env/workflow/nodes/workflow-llm-history.test.ts +455 -0
  331. package/src/env/workflow/nodes/workflow-node.test.ts +315 -0
  332. package/src/env/workflow/nodes/workflow-node.ts +311 -0
  333. package/src/env/workflow/service/index.ts +27 -0
  334. package/src/env/workflow/service/registry.test.ts +133 -0
  335. package/src/env/workflow/service/registry.ts +71 -0
  336. package/src/env/workflow/service/workflow-service.test.ts +310 -0
  337. package/src/env/workflow/service/workflow-service.ts +393 -0
  338. package/src/env/workflow/storage/index.ts +28 -0
  339. package/src/env/workflow/storage/mock-repositories.ts +385 -0
  340. package/src/env/workflow/storage/sqlite.test.ts +179 -0
  341. package/src/env/workflow/storage/sqlite.ts +163 -0
  342. package/src/env/workflow/storage/workflow-repo.test.ts +780 -0
  343. package/src/env/workflow/storage/workflow-repo.ts +342 -0
  344. package/src/env/workflow/tools/ask-user-tool.ts +82 -0
  345. package/src/env/workflow/tools/index.ts +26 -0
  346. package/src/env/workflow/tools/run-workflow.test.ts +352 -0
  347. package/src/env/workflow/tools/run-workflow.ts +214 -0
  348. package/src/env/workflow/types/context.ts +18 -0
  349. package/src/env/workflow/types/decorators-types.ts +198 -0
  350. package/src/env/workflow/types/event.test.ts +515 -0
  351. package/src/env/workflow/types/event.ts +193 -0
  352. package/src/env/workflow/types/index.ts +49 -0
  353. package/src/env/workflow/types/run.test.ts +437 -0
  354. package/src/env/workflow/types/run.ts +173 -0
  355. package/src/env/workflow/types/workflow-hil.ts +114 -0
  356. package/src/env/workflow/types/workflow-message.test.ts +138 -0
  357. package/src/env/workflow/types/workflow-message.ts +196 -0
  358. package/src/env/workflow/types/workflow-session.test.ts +95 -0
  359. package/src/env/workflow/types/workflow-session.ts +59 -0
  360. package/src/env/workflow/types/workflow.test.ts +495 -0
  361. package/src/env/workflow/types/workflow.ts +195 -0
  362. package/src/env/workflow/types_compat.ts +51 -0
  363. package/src/env/workflow/utils/create-workflow.ts +47 -0
  364. package/src/env/workflow/utils/execution-state.ts +245 -0
  365. package/src/env/workflow/utils/index.ts +18 -0
  366. package/src/env/workflow/utils/node-registry-helper.ts +58 -0
  367. package/src/env/workflow/utils/recovery-validator.test.ts +460 -0
  368. package/src/env/workflow/utils/recovery-validator.ts +377 -0
  369. package/src/env/workflow/utils/session-parser.test.ts +111 -0
  370. package/src/env/workflow/utils/session-parser.ts +94 -0
  371. package/src/env/workflow/utils/session-recovery.test.ts +334 -0
  372. package/src/env/workflow/utils/session-recovery.ts +188 -0
  373. package/src/env/workflow/utils/template-resolver.test.ts +258 -0
  374. package/src/env/workflow/utils/template-resolver.ts +436 -0
  375. package/src/env/workflow/utils/validation-rules.ts +149 -0
  376. package/src/env/workflow/workflow-component.ts +544 -0
  377. package/src/index.ts +422 -0
  378. package/src/utils/id.ts +21 -0
@@ -0,0 +1,1039 @@
1
+ /**
2
+ * @fileoverview LLM invocation with AI SDK integration.
3
+ *
4
+ * This module provides LLM invocation using AI SDK for better provider support.
5
+ * Stream events are published via Environment.pushEnvEvent().
6
+ */
7
+
8
+ import { randomUUID } from "crypto";
9
+ import { streamText, type ModelMessage, type ToolSet, jsonSchema } from "ai";
10
+ import { createOpenAI } from "@ai-sdk/openai";
11
+ import { createAnthropic } from "@ai-sdk/anthropic";
12
+ import { createGoogleGenerativeAI } from "@ai-sdk/google";
13
+ import { createOpenAICompatible } from "@ai-sdk/openai-compatible";
14
+ import { z } from "zod";
15
+ import { zodToJsonSchema } from "zod-to-json-schema";
16
+ import type { ToolInfo, ToolResult, LLMOutput, LLMMessage, UsageInfo } from "./types";
17
+ import type { Environment } from "../interface";
18
+ import { createLogger } from "../log-trace/logger";
19
+
20
+ // 创建 llm:invoke 模块日志器
21
+ const logger = createLogger("llm:invoke");
22
+
23
+ // LanguageModel type alias for type checking
24
+ type AISDKLanguageModel = Parameters<ReturnType<typeof createOpenAI>["languageModel"]>[0];
25
+
26
+ /**
27
+ * GenerateText Options
28
+ *
29
+ * 定义 generateText 函数的参数类型,用于类型安全调用
30
+ */
31
+ interface GenerateTextOptions {
32
+ model: AISDKLanguageModel;
33
+ messages: ModelMessage[];
34
+ tools?: ToolSet;
35
+ temperature?: number;
36
+ maxTokens?: number;
37
+ abortSignal?: AbortSignal;
38
+ [key: string]: unknown;
39
+ }
40
+
41
+ /**
42
+ * GenerateText Result
43
+ *
44
+ * 定义 generateText 返回的结果类型
45
+ */
46
+ interface GenerateTextResult {
47
+ text: string;
48
+ finishReason: string;
49
+ usage: {
50
+ promptTokens?: number;
51
+ completionTokens?: number;
52
+ totalTokens?: number;
53
+ };
54
+ [key: string]: unknown;
55
+ }
56
+
57
+ /**
58
+ * Invoke LLM 配置
59
+ */
60
+ export interface InvokeConfig {
61
+ /** 模型标识符(格式: providerId/modelId) */
62
+ model: string;
63
+ /** Base URL */
64
+ baseURL?: string;
65
+ /** API Key */
66
+ apiKey: string;
67
+ /** 额外选项 */
68
+ options?: {
69
+ thinkingInText?: {
70
+ enabled?: boolean;
71
+ tags?: string[];
72
+ };
73
+ };
74
+ }
75
+
76
+ /**
77
+ * LLM 选项
78
+ */
79
+ export interface InvokeOptions {
80
+ /** 消息列表 */
81
+ messages: LLMMessage[];
82
+ /** 工具列表 */
83
+ tools?: ToolInfo[];
84
+ /** 模型标识符(可选) */
85
+ model?: string;
86
+ /** 温度参数 */
87
+ temperature?: number;
88
+ /** 最大 Token 数 */
89
+ maxTokens?: number;
90
+ /** Environment 实例(用于发布流式事件,可选) */
91
+ env?: Environment;
92
+ /** 上下文信息(用于事件元数据) */
93
+ context?: {
94
+ /** Session ID */
95
+ sessionId?: string;
96
+ /** Message ID */
97
+ messageId?: string;
98
+ };
99
+ }
100
+
101
+ /**
102
+ * LLM Tool Context - Context for tool calls during LLM invocation
103
+ */
104
+ export interface LLMToolContext {
105
+ /** Abort Signal */
106
+ abort?: AbortSignal | undefined;
107
+ /** 额外上下文 */
108
+ context?: Record<string, unknown>;
109
+ }
110
+
111
+ /**
112
+ * Usage 信息(AI SDK 格式)
113
+ * 参考: LanguageModelV3Usage from @ai-sdk/provider
114
+ */
115
+ interface AISDKUsage {
116
+ // 标准格式: inputTokens/outputTokens 是对象,包含 total 等属性
117
+ inputTokens?: {
118
+ total?: number;
119
+ noCache?: number;
120
+ cacheRead?: number;
121
+ cacheWrite?: number;
122
+ [key: string]: unknown;
123
+ };
124
+ outputTokens?: {
125
+ total?: number;
126
+ [key: string]: unknown;
127
+ };
128
+ totalTokens?: number;
129
+ // 兼容旧格式: 直接 number
130
+ promptTokens?: number;
131
+ completionTokens?: number;
132
+ inputTokenDetails?: {
133
+ tokens?: number;
134
+ [key: string]: unknown;
135
+ };
136
+ outputTokenDetails?: {
137
+ tokens?: number;
138
+ [key: string]: unknown;
139
+ };
140
+ raw?: {
141
+ total_tokens?: number;
142
+ prompt_tokens?: number;
143
+ completion_tokens?: number;
144
+ };
145
+ }
146
+
147
+ /**
148
+ * Usage 信息转换
149
+ */
150
+ export interface ConvertedUsageInfo {
151
+ /** Prompt Token 数 */
152
+ promptTokens: number;
153
+ /** Completion Token 数 */
154
+ completionTokens: number;
155
+ /** 总 Token 数 */
156
+ totalTokens: number;
157
+ }
158
+
159
+ /**
160
+ * AI SDK 最大重试次数
161
+ * AI SDK 内部使用指数退避策略(默认 2s → 4s → 8s)
162
+ */
163
+ const MAX_RETRIES = 3;
164
+
165
+ /**
166
+ * LLM 默认超时时间(毫秒)
167
+ */
168
+ const DEFAULT_TIMEOUT = 60000;
169
+
170
+ /**
171
+ * 超时错误
172
+ */
173
+ export class TimeoutError extends Error {
174
+ constructor(message: string, public readonly timeout: number) {
175
+ super(message);
176
+ this.name = "TimeoutError";
177
+ }
178
+ }
179
+
180
+ /**
181
+ * 带超时的 Promise 包装器
182
+ *
183
+ * @param promise 要执行的 Promise
184
+ * @param timeoutMs 超时时间(毫秒)
185
+ * @param message 自定义错误消息
186
+ * @returns Promise 结果
187
+ * @throws TimeoutError 当超时时
188
+ */
189
+ export async function withTimeout<T>(
190
+ promise: Promise<T>,
191
+ timeoutMs: number = DEFAULT_TIMEOUT,
192
+ message?: string
193
+ ): Promise<T> {
194
+ const timeoutPromise = new Promise<never>((_, reject) => {
195
+ setTimeout(() => {
196
+ reject(new TimeoutError(
197
+ message || `LLM invocation timeout after ${timeoutMs}ms`,
198
+ timeoutMs
199
+ ));
200
+ }, timeoutMs);
201
+ });
202
+
203
+ return Promise.race([promise, timeoutPromise]);
204
+ }
205
+
206
+ /**
207
+ * 解析模型字符串
208
+ * 格式: "providerId/modelId"
209
+ */
210
+ export function parseModelString(model?: string): { providerId: string; modelId: string } {
211
+ if (!model) {
212
+ return { providerId: "openai", modelId: "gpt-4o" };
213
+ }
214
+
215
+ const parts = model.split("/");
216
+ if (parts.length >= 2) {
217
+ return { providerId: parts[0], modelId: parts.slice(1).join("/") };
218
+ }
219
+
220
+ return { providerId: "openai", modelId: model };
221
+ }
222
+
223
+ /**
224
+ * 从 Usage 信息提取标准格式
225
+ */
226
+ function extractUsageInfo(usage: AISDKUsage | undefined): ConvertedUsageInfo | undefined {
227
+ if (!usage) return undefined;
228
+
229
+ // 标准格式: inputTokens/outputTokens 是对象,包含 total 属性(AI SDK LanguageModelV3Usage)
230
+ if (usage.inputTokens && typeof usage.inputTokens === "object") {
231
+ return {
232
+ promptTokens: usage.inputTokens.total ?? 0,
233
+ completionTokens: usage.outputTokens?.total ?? 0,
234
+ totalTokens: usage.totalTokens ?? 0,
235
+ };
236
+ }
237
+
238
+ // 旧格式: 直接 number 属性
239
+ if (typeof usage.promptTokens === "number") {
240
+ return {
241
+ promptTokens: usage.promptTokens,
242
+ completionTokens: usage.completionTokens ?? 0,
243
+ totalTokens: usage.totalTokens ?? 0,
244
+ };
245
+ }
246
+
247
+ // MiniMax 格式: 使用 tokenDetails
248
+ if (usage.inputTokenDetails || usage.outputTokenDetails) {
249
+ return {
250
+ promptTokens: usage.inputTokenDetails?.tokens ?? 0,
251
+ completionTokens: usage.outputTokenDetails?.tokens ?? 0,
252
+ totalTokens: usage.totalTokens ?? 0,
253
+ };
254
+ }
255
+
256
+ // MiniMax 流式格式: 只有 total_tokens
257
+ if (usage.raw && typeof usage.raw.total_tokens === "number") {
258
+ return {
259
+ promptTokens: 0,
260
+ completionTokens: 0,
261
+ totalTokens: usage.raw.total_tokens,
262
+ };
263
+ }
264
+
265
+ return undefined;
266
+ }
267
+
268
+ /**
269
+ * 将 LLMMessage 转换为 AI SDK 格式
270
+ */
271
+ function convertToSDKMessages(messages: LLMMessage[]): ModelMessage[] {
272
+ return messages.map((msg) => {
273
+ // 处理 assistant 消息中的 toolCalls
274
+ if (msg.role === "assistant" && msg.toolCalls && msg.toolCalls.length > 0) {
275
+ // AI SDK v6 期望 toolCalls 在 content 数组中作为 tool-call part
276
+ // 注意:使用 input: unknown 而不是 arguments: string
277
+ const toolCallParts = msg.toolCalls.map((tc) => {
278
+ // 解析 arguments 为对象
279
+ let input: unknown = {};
280
+ if (tc.function?.arguments) {
281
+ if (typeof tc.function.arguments === "string") {
282
+ try {
283
+ input = JSON.parse(tc.function.arguments);
284
+ } catch {
285
+ input = tc.function.arguments;
286
+ }
287
+ } else {
288
+ input = tc.function.arguments;
289
+ }
290
+ } else if ((tc as any).arguments) {
291
+ if (typeof (tc as any).arguments === "string") {
292
+ try {
293
+ input = JSON.parse((tc as any).arguments);
294
+ } catch {
295
+ input = (tc as any).arguments;
296
+ }
297
+ } else {
298
+ input = (tc as any).arguments;
299
+ }
300
+ }
301
+
302
+ return {
303
+ type: "tool-call" as const,
304
+ toolCallId: tc.id,
305
+ toolName: tc.function?.name || tc.name || "unknown",
306
+ input,
307
+ };
308
+ });
309
+
310
+ return {
311
+ role: "assistant" as const,
312
+ content: toolCallParts,
313
+ };
314
+ }
315
+
316
+ // 验证 tool 消息格式
317
+ if (msg.role === "tool") {
318
+ if (!msg.toolCallId) {
319
+ logger.warn("Tool message missing toolCallId", { msg });
320
+ }
321
+
322
+ // AI SDK v6 要求 tool 消息的 output 格式为 { type: 'text', value: string }
323
+ // 处理 content 可能是数组的情况(AI SDK v6 格式)
324
+ let outputValue = "";
325
+ const content = msg.content as string | unknown[];
326
+ if (typeof content === "string") {
327
+ outputValue = content;
328
+ } else if (Array.isArray(content)) {
329
+ // 如果是数组,可能是 [{ type: 'tool-result', output: '...' }]
330
+ const toolResultPart = (content as any[]).find((p: any) => p?.type === "tool-result");
331
+ if (toolResultPart) {
332
+ outputValue = typeof toolResultPart.output === "string"
333
+ ? toolResultPart.output
334
+ : JSON.stringify(toolResultPart.output);
335
+ } else {
336
+ // 尝试提取任何文本内容
337
+ outputValue = JSON.stringify(content);
338
+ }
339
+ } else {
340
+ outputValue = JSON.stringify(content);
341
+ }
342
+
343
+ const converted: ModelMessage = {
344
+ role: "tool" as const,
345
+ content: [{
346
+ type: "tool-result" as const,
347
+ toolCallId: msg.toolCallId || "unknown",
348
+ toolName: msg.name || "unknown",
349
+ output: {
350
+ type: "text" as const,
351
+ value: outputValue,
352
+ },
353
+ }],
354
+ };
355
+
356
+ return converted;
357
+ }
358
+ return msg as unknown as ModelMessage;
359
+ });
360
+ }
361
+
362
+ /**
363
+ * 将 ToolInfo 转换为 AI SDK ToolSet 格式
364
+ */
365
+ function convertToolsToSDK(tools: ToolInfo[]): ToolSet {
366
+ const result: ToolSet = {};
367
+
368
+ for (const tool of tools) {
369
+ const jsonSchemaObj = extractToolSchema(tool.parameters);
370
+ (result as Record<string, unknown>)[tool.name] = {
371
+ description: tool.description || "",
372
+ inputSchema: jsonSchema(jsonSchemaObj),
373
+ };
374
+ }
375
+
376
+ return result;
377
+ }
378
+
379
+ /**
380
+ * 从 Zod 类型提取 JSON Schema
381
+ */
382
+ function extractToolSchema(parameters: Record<string, unknown>): Record<string, unknown> {
383
+ // 如果是 Zod 类型
384
+ if (parameters && typeof parameters === "object" && "_def" in (parameters as { _def?: unknown })) {
385
+ const zodSchema = parameters as unknown as z.ZodType;
386
+ const schema: Record<string, unknown> = zodToJsonSchema(zodSchema, "zod") as Record<string, unknown>;
387
+ if ("$ref" in schema && schema.definitions) {
388
+ const def = (schema.definitions as Record<string, unknown>).zod as Record<string, unknown> | undefined;
389
+ if (def && def.type === "object" && def.properties) {
390
+ return {
391
+ type: "object",
392
+ properties: def.properties,
393
+ required: def.required,
394
+ additionalProperties: true,
395
+ };
396
+ }
397
+ }
398
+ return schema as Record<string, unknown>;
399
+ }
400
+
401
+ // 如果已经是 JSON Schema 格式
402
+ return parameters as Record<string, unknown>;
403
+ }
404
+
405
+ /**
406
+ * 生成 Provider 选项
407
+ */
408
+ function generateProviderOptions(
409
+ providerType: string,
410
+ options: {
411
+ temperature?: number;
412
+ maxTokens?: number;
413
+ }
414
+ ): {
415
+ temperature?: number;
416
+ maxTokens?: number;
417
+ providerOptions: Record<string, unknown>;
418
+ } {
419
+ const result: {
420
+ temperature?: number;
421
+ maxTokens?: number;
422
+ providerOptions: Record<string, unknown>;
423
+ } = {
424
+ providerOptions: {},
425
+ };
426
+
427
+ if (options.temperature !== undefined) {
428
+ result.temperature = options.temperature;
429
+ }
430
+
431
+ if (options.maxTokens !== undefined) {
432
+ result.maxTokens = options.maxTokens;
433
+ }
434
+
435
+ // Provider 特定配置
436
+ switch (providerType) {
437
+ case "anthropic":
438
+ // Anthropic 不支持 temperature,使用 top_p
439
+ break;
440
+ case "openai-compatible":
441
+ // OpenAI 兼容格式支持 includeUsage
442
+ result.providerOptions.includeUsage = true;
443
+ break;
444
+ }
445
+
446
+ return result;
447
+ }
448
+
449
+ /**
450
+ * 创建 Env 事件发布函数
451
+ * @param env Environment 实例
452
+ * @param context 上下文信息
453
+ * @returns 发布函数
454
+ */
455
+ function createEnvEventEmitter(env?: Environment, context?: InvokeOptions["context"]) {
456
+ if (!env) {
457
+ return null;
458
+ }
459
+
460
+ return (event: { type: string; [key: string]: unknown }) => {
461
+ try {
462
+ env.pushEnvEvent({
463
+ id: randomUUID(),
464
+ type: `llm.${event.type}`,
465
+ timestamp: Date.now(),
466
+ metadata: {
467
+ source: "llm.invoke",
468
+ sessionId: context?.sessionId,
469
+ messageId: context?.messageId,
470
+ },
471
+ payload: event,
472
+ });
473
+ } catch (err) {
474
+ logger.warn("Failed to push env event", { error: String(err), eventType: event.type });
475
+ }
476
+ };
477
+ }
478
+
479
+ /**
480
+ * 处理文本中的 thinking 标签
481
+ */
482
+ function processThinkingFromText(
483
+ textDelta: string,
484
+ config: {
485
+ enabled?: boolean;
486
+ tags?: string[];
487
+ removeFromOutput?: boolean;
488
+ }
489
+ ): { cleanedText: string; thinkingContent?: string } {
490
+ if (!config.enabled || !textDelta) {
491
+ return { cleanedText: textDelta };
492
+ }
493
+
494
+ const tags = config.tags || ["thinking", "think"];
495
+ let remainingText = textDelta;
496
+ let extractedThinking = "";
497
+
498
+ for (const tag of tags) {
499
+ let openTag: string;
500
+ let closeTag: string;
501
+
502
+ // 特殊处理 "think" -> 映射到标准的 <think>/</think>
503
+ if (tag === "think") {
504
+ openTag = "<think>";
505
+ closeTag = "</think>";
506
+ } else {
507
+ openTag = `<${tag}>`;
508
+ closeTag = `</${tag}>`;
509
+ }
510
+
511
+ // 匹配所有 thinking 标签
512
+ const regex = new RegExp(`${openTag}([\\s\\S]*?)${closeTag}`, "gi");
513
+ let match;
514
+
515
+ while ((match = regex.exec(remainingText)) !== null) {
516
+ const content = match[1];
517
+ extractedThinking += content;
518
+
519
+ // 从输出中移除 thinking 标签
520
+ if (config.removeFromOutput !== false) {
521
+ remainingText = remainingText.replaceAll(match[0], "");
522
+ }
523
+ }
524
+ }
525
+
526
+ return {
527
+ cleanedText: remainingText,
528
+ thinkingContent: extractedThinking || undefined,
529
+ };
530
+ }
531
+
532
+ /**
533
+ * 处理流式 thinking 标签
534
+ * 在检测到 opening/closing 标签时立即触发推理事件
535
+ */
536
+ function processThinkingStream(
537
+ textDelta: string,
538
+ config: {
539
+ enabled?: boolean;
540
+ tags?: string[];
541
+ removeFromOutput?: boolean;
542
+ },
543
+ state: {
544
+ isOpen: boolean;
545
+ content: string;
546
+ }
547
+ ): {
548
+ cleanedText: string;
549
+ isThinkingTagOpen: boolean;
550
+ currentThinkingContent: string;
551
+ reasoningEvents: string[];
552
+ } {
553
+ if (!config.enabled || !textDelta) {
554
+ return {
555
+ cleanedText: textDelta,
556
+ isThinkingTagOpen: state.isOpen,
557
+ currentThinkingContent: state.content,
558
+ reasoningEvents: [],
559
+ };
560
+ }
561
+
562
+ const tags = config.tags || ["thinking"];
563
+ let remainingText = textDelta;
564
+ const reasoningEvents: string[] = [];
565
+ let isOpen = state.isOpen;
566
+ let currentContent = state.content;
567
+
568
+ for (const tag of tags) {
569
+ const openTag = `<${tag}>`;
570
+ const closeTag = `</${tag}>`;
571
+
572
+ let text = remainingText;
573
+ let result = "";
574
+
575
+ const openIndex = text.toLowerCase().indexOf(openTag.toLowerCase());
576
+ const closeIndex = text.toLowerCase().indexOf(closeTag.toLowerCase());
577
+
578
+ if (openIndex !== -1 && (closeIndex === -1 || openIndex < closeIndex)) {
579
+ // 先找到 opening 标签
580
+ const beforeOpen = text.substring(0, openIndex);
581
+ const afterOpen = text.substring(openIndex + openTag.length);
582
+
583
+ if (!isOpen) {
584
+ isOpen = true;
585
+ currentContent = "";
586
+ reasoningEvents.push("");
587
+ }
588
+
589
+ result += beforeOpen;
590
+
591
+ const innerCloseIndex = afterOpen.toLowerCase().indexOf(closeTag.toLowerCase());
592
+
593
+ if (innerCloseIndex !== -1) {
594
+ // 同一 delta 中有 opening 和 closing
595
+ const thinkingContent = afterOpen.substring(0, innerCloseIndex);
596
+ const afterClose = afterOpen.substring(innerCloseIndex + closeTag.length);
597
+
598
+ currentContent += thinkingContent;
599
+ reasoningEvents.push(currentContent);
600
+
601
+ isOpen = false;
602
+ currentContent = "";
603
+
604
+ result += afterClose;
605
+ } else {
606
+ // 只有 opening 标签
607
+ currentContent += afterOpen;
608
+ reasoningEvents.push(currentContent);
609
+ result += "";
610
+ }
611
+
612
+ remainingText = result;
613
+ } else if (closeIndex !== -1) {
614
+ // 先找到 closing 标签
615
+ const beforeClose = text.substring(0, closeIndex);
616
+ const afterClose = text.substring(closeIndex + closeTag.length);
617
+
618
+ if (isOpen) {
619
+ currentContent += beforeClose;
620
+ reasoningEvents.push(currentContent);
621
+ isOpen = false;
622
+ currentContent = "";
623
+ result = afterClose;
624
+ } else {
625
+ result = text;
626
+ }
627
+
628
+ remainingText = result;
629
+ }
630
+ }
631
+
632
+ return {
633
+ cleanedText: remainingText,
634
+ isThinkingTagOpen: isOpen,
635
+ currentThinkingContent: currentContent,
636
+ reasoningEvents,
637
+ };
638
+ }
639
+
640
+ /**
641
+ * 核心 LLM 调用函数
642
+ * 使用 AI SDK 的 streamText
643
+ *
644
+ * 流式事件通过 env.pushEnvEvent() 发布
645
+ *
646
+ * @param config - Invoke 配置
647
+ * @param options - LLM 选项
648
+ * @param ctx - Tool Context
649
+ * @returns ToolResult
650
+ */
651
+ export async function invoke(
652
+ config: InvokeConfig,
653
+ options: InvokeOptions,
654
+ ctx: LLMToolContext
655
+ ): Promise<ToolResult> {
656
+ try {
657
+ // 0. 创建 Env 事件发布器
658
+ const emitToEnv = createEnvEventEmitter(options.env, options.context);
659
+
660
+ // 1. 解析模型字符串
661
+ const { providerId, modelId } = parseModelString(options.model || config.model);
662
+
663
+ // 2. 获取 Provider 实例
664
+ // 注意:实际使用需要 ProviderManager 已初始化
665
+ // 这里简化处理,直接创建 SDK Provider
666
+ const provider = createProviderInstance(providerId, config.apiKey, config.baseURL);
667
+
668
+ if (!provider) {
669
+ throw new Error(`Failed to create provider for ${providerId}`);
670
+ }
671
+
672
+ // 3. 转换消息为 AI SDK 格式
673
+ const messages = convertToSDKMessages(options.messages);
674
+
675
+ // DEBUG: 记录转换后消息中的 toolCallId 匹配情况
676
+ const toolCallIds = new Set<string>();
677
+ const assistantToolCalls: { id: string; name?: string }[] = [];
678
+ const toolResultIds: string[] = [];
679
+
680
+ for (const msg of messages) {
681
+ if (msg.role === "assistant") {
682
+ // 检查 assistant 消息中的 toolCalls
683
+ const assistantContent = Array.isArray(msg.content) ? msg.content : [];
684
+ for (const part of assistantContent) {
685
+ if (part && typeof part === "object" && (part as any).type === "tool-call") {
686
+ const tc = (part as any).toolCall || (part as any);
687
+ assistantToolCalls.push({ id: tc.id, name: tc.name || (tc.function && tc.function.name) });
688
+ toolCallIds.add(tc.id);
689
+ }
690
+ }
691
+ }
692
+ if (msg.role === "tool") {
693
+ const toolContent = Array.isArray(msg.content) ? msg.content : [];
694
+ for (const part of toolContent) {
695
+ if (part && typeof part === "object" && (part as any).type === "tool-result") {
696
+ toolResultIds.push((part as any).toolCallId);
697
+ }
698
+ }
699
+ }
700
+ }
701
+
702
+ // 检查 toolCallId 匹配
703
+ const missingToolResults = assistantToolCalls.filter(tc => !toolResultIds.includes(tc.id));
704
+ const orphanedToolResults = toolResultIds.filter(id => !toolCallIds.has(id));
705
+
706
+ if (assistantToolCalls.length > 0 || toolResultIds.length > 0) {
707
+ logger.debug("[invoke] ToolCallId matching analysis:", {
708
+ assistantToolCalls,
709
+ toolResultIds,
710
+ missingToolResults: missingToolResults.length > 0 ? missingToolResults : "none",
711
+ orphanedToolResults: orphanedToolResults.length > 0 ? orphanedToolResults : "none",
712
+ });
713
+ }
714
+
715
+ // 4. 转换工具
716
+ const tools = options.tools && options.tools.length > 0 ? convertToolsToSDK(options.tools) : undefined;
717
+
718
+ // 5. 生成 Provider 选项
719
+ const providerOptions = generateProviderOptions(providerId, {
720
+ temperature: options.temperature,
721
+ maxTokens: options.maxTokens,
722
+ });
723
+
724
+ // 6. 发布开始事件
725
+ const model = `${providerId}/${modelId}`;
726
+ if (emitToEnv) {
727
+ emitToEnv({ type: "start", metadata: { model } });
728
+ }
729
+
730
+ // 7. 调用 AI SDK streamText
731
+ const streamTextOptions = {
732
+ model: provider.languageModel(modelId) as Parameters<typeof streamText>[0]["model"],
733
+ messages,
734
+ tools,
735
+ temperature: providerOptions.temperature,
736
+ maxTokens: providerOptions.maxTokens,
737
+ abortSignal: ctx.abort,
738
+ maxRetries: MAX_RETRIES,
739
+ streamOptions: {
740
+ includeUsage: true,
741
+ },
742
+ };
743
+
744
+ const result = await streamText(streamTextOptions as Parameters<typeof streamText>[0]);
745
+
746
+ // 8. 处理流
747
+ let fullContent = "";
748
+ let reasoningContent = "";
749
+ const toolCalls: Array<{ id: string; name: string; args: Record<string, unknown> }> = [];
750
+ let usageInfo: ConvertedUsageInfo | undefined;
751
+
752
+ // 流式推理状态
753
+ let isThinkingTagOpen = false;
754
+ let currentThinkingContent = "";
755
+
756
+ for await (const part of result.fullStream) {
757
+ const streamPart = part as Record<string, unknown>;
758
+
759
+ switch (streamPart.type) {
760
+ case "text-delta":
761
+ const textDelta = streamPart.text as string;
762
+
763
+ // 检查是否有 thinkingInText 配置(从 config.options 中获取)
764
+ const thinkingConfig = (config.options as { thinkingInText?: { enabled?: boolean; tags?: string[] } } | undefined)?.thinkingInText;
765
+
766
+ if (thinkingConfig?.enabled) {
767
+ const thinkingResult = processThinkingStream(textDelta, thinkingConfig, {
768
+ isOpen: isThinkingTagOpen,
769
+ content: currentThinkingContent,
770
+ });
771
+
772
+ isThinkingTagOpen = thinkingResult.isThinkingTagOpen;
773
+ currentThinkingContent = thinkingResult.currentThinkingContent;
774
+ fullContent += thinkingResult.cleanedText;
775
+
776
+ for (const reasoningDelta of thinkingResult.reasoningEvents) {
777
+ reasoningContent = reasoningDelta;
778
+ if (emitToEnv) {
779
+ emitToEnv({ type: "reasoning", content: reasoningContent });
780
+ }
781
+ }
782
+
783
+ if (emitToEnv) {
784
+ emitToEnv({ type: "text", content: fullContent, delta: thinkingResult.cleanedText });
785
+ }
786
+ } else {
787
+ fullContent += textDelta;
788
+ if (emitToEnv) {
789
+ emitToEnv({ type: "text", content: fullContent, delta: textDelta });
790
+ }
791
+ }
792
+ break;
793
+
794
+ case "reasoning-delta":
795
+ const reasoningDelta = streamPart.text as string;
796
+ reasoningContent += reasoningDelta;
797
+ if (emitToEnv) {
798
+ emitToEnv({ type: "reasoning", content: reasoningContent });
799
+ }
800
+ break;
801
+
802
+ case "tool-call":
803
+ const toolInput = streamPart.input as Record<string, unknown>;
804
+ const toolCallId = streamPart.toolCallId as string;
805
+ const toolName = streamPart.toolName as string;
806
+ toolCalls.push({
807
+ id: toolCallId,
808
+ name: toolName,
809
+ args: toolInput,
810
+ });
811
+ if (emitToEnv) {
812
+ emitToEnv({
813
+ type: "tool_call",
814
+ toolCall: {
815
+ id: toolCallId,
816
+ name: toolName,
817
+ arguments: JSON.stringify(toolInput),
818
+ },
819
+ });
820
+ }
821
+ break;
822
+
823
+ case "finish-step":
824
+ const stepUsage = extractUsageInfo(streamPart.usage as AISDKUsage);
825
+ if (stepUsage) {
826
+ usageInfo = stepUsage;
827
+ }
828
+ break;
829
+
830
+ case "error":
831
+ throw streamPart.error;
832
+
833
+ case "finish":
834
+ usageInfo = extractUsageInfo(streamPart.totalUsage as AISDKUsage);
835
+ if (!usageInfo && streamPart.usage) {
836
+ usageInfo = extractUsageInfo(streamPart.usage as AISDKUsage);
837
+ }
838
+ break;
839
+ }
840
+ }
841
+
842
+ // 9. 发布完成事件
843
+ if (emitToEnv) {
844
+ emitToEnv({
845
+ type: "completed",
846
+ content: fullContent,
847
+ metadata: {
848
+ model,
849
+ usage: usageInfo,
850
+ },
851
+ });
852
+ }
853
+
854
+ // 10. 返回结果
855
+ if (toolCalls.length > 0) {
856
+ // 有工具调用时,返回第一个工具调用的结果
857
+ const firstToolCall = toolCalls[0];
858
+ return {
859
+ toolCallId: firstToolCall.id,
860
+ result: JSON.stringify({
861
+ content: fullContent,
862
+ reasoning: reasoningContent || undefined,
863
+ tool_calls: toolCalls.map((tc) => ({
864
+ id: tc.id,
865
+ function: {
866
+ name: tc.name,
867
+ arguments: JSON.stringify(tc.args),
868
+ },
869
+ })),
870
+ }),
871
+ };
872
+ }
873
+
874
+ // 无工具调用时,返回内容
875
+ return {
876
+ toolCallId: "invoke-" + Date.now(),
877
+ result: JSON.stringify({
878
+ content: fullContent,
879
+ reasoning: reasoningContent || undefined,
880
+ usage: usageInfo,
881
+ model,
882
+ }),
883
+ };
884
+ } catch (error) {
885
+ logger.error("Error during invocation", {
886
+ error: error instanceof Error ? error.message : String(error),
887
+ stack: error instanceof Error ? error.stack : undefined,
888
+ });
889
+
890
+ return {
891
+ toolCallId: "error-" + Date.now(),
892
+ result: error instanceof Error ? error.message : String(error),
893
+ isError: true,
894
+ };
895
+ }
896
+ }
897
+
898
+ /**
899
+ * 创建 Provider 实例
900
+ * 根据 providerId 创建对应的 AI SDK Provider
901
+ */
902
+ function createProviderInstance(
903
+ providerId: string,
904
+ apiKey: string,
905
+ baseURL?: string
906
+ ): any {
907
+ try {
908
+ // OpenAI
909
+ if (providerId === "openai") {
910
+ const openai = createOpenAI({
911
+ apiKey,
912
+ baseURL,
913
+ });
914
+ return openai;
915
+ }
916
+
917
+ // Anthropic
918
+ if (providerId === "anthropic") {
919
+ const anthropic = createAnthropic({
920
+ apiKey,
921
+ });
922
+ return anthropic;
923
+ }
924
+
925
+ // Google
926
+ if (providerId === "google") {
927
+ const google = createGoogleGenerativeAI({
928
+ apiKey,
929
+ });
930
+ return google;
931
+ }
932
+
933
+ // 默认: 使用 OpenAI 兼容格式(支持 minimax、deepseek 等)
934
+ const defaultProvider = createOpenAICompatible({
935
+ name: providerId,
936
+ apiKey,
937
+ baseURL: baseURL || "",
938
+ includeUsage: true, // Enable usage in stream responses for MiniMax etc.
939
+ });
940
+ return defaultProvider;
941
+ } catch (error) {
942
+ logger.error(`Failed to create provider ${providerId}`, { error: String(error) });
943
+ return null;
944
+ }
945
+ }
946
+
947
+ /**
948
+ * 非流式 LLM 调用
949
+ *
950
+ * 使用 AI SDK 的 generateText API,返回完整响应
951
+ */
952
+ export async function invokeNonStream(
953
+ config: InvokeConfig,
954
+ options: InvokeOptions,
955
+ ctx: LLMToolContext
956
+ ): Promise<LLMOutput> {
957
+ // 解析模型字符串
958
+ const { providerId, modelId } = parseModelString(options.model || config.model);
959
+
960
+ // 获取 Provider 实例
961
+ const provider = createProviderInstance(providerId, config.apiKey, config.baseURL);
962
+ if (!provider) {
963
+ throw new Error(`Failed to create provider for ${providerId}`);
964
+ }
965
+
966
+ // 转换消息和工具
967
+ const messages = convertToSDKMessages(options.messages);
968
+ const tools = options.tools && options.tools.length > 0 ? convertToolsToSDK(options.tools) : undefined;
969
+
970
+ // 生成 Provider 选项
971
+ const providerOptions = generateProviderOptions(providerId, {
972
+ temperature: options.temperature,
973
+ maxTokens: options.maxTokens,
974
+ });
975
+
976
+ // 使用 generateText API(非流式)
977
+ const { generateText } = await import("ai");
978
+ const genTextFn = generateText as unknown as (options: GenerateTextOptions) => Promise<GenerateTextResult>;
979
+ const result = await genTextFn({
980
+ model: provider.languageModel(modelId),
981
+ messages,
982
+ tools,
983
+ temperature: providerOptions.temperature,
984
+ maxTokens: providerOptions.maxTokens,
985
+ abortSignal: ctx.abort,
986
+ });
987
+
988
+ // 计算 usage
989
+ const usage = result.usage;
990
+ const usageInfo: UsageInfo | undefined = usage ? {
991
+ promptTokens: (usage as Record<string, unknown>).promptTokens as number ?? 0,
992
+ completionTokens: (usage as Record<string, unknown>).completionTokens as number ?? 0,
993
+ totalTokens: (usage as Record<string, unknown>).totalTokens as number ?? 0,
994
+ } : undefined;
995
+
996
+ // 发布完成事件(如果有 env)
997
+ if (options.env) {
998
+ options.env.pushEnvEvent({
999
+ id: randomUUID(),
1000
+ type: "llm.completed",
1001
+ timestamp: Date.now(),
1002
+ metadata: {
1003
+ source: "llm.invokeNonStream",
1004
+ sessionId: options.context?.sessionId,
1005
+ messageId: options.context?.messageId,
1006
+ },
1007
+ payload: {
1008
+ type: "completed",
1009
+ content: result.text,
1010
+ metadata: {
1011
+ model: `${providerId}/${modelId}`,
1012
+ usage: usageInfo,
1013
+ },
1014
+ },
1015
+ });
1016
+ }
1017
+
1018
+ return {
1019
+ content: result.text,
1020
+ finishReason: result.finishReason === "stop" ? "stop" : "tool-calls",
1021
+ model: `${providerId}/${modelId}`,
1022
+ usage: usageInfo,
1023
+ };
1024
+ }
1025
+
1026
+ /**
1027
+ * 创建 LLM 配置
1028
+ */
1029
+ export function createInvokeConfig(
1030
+ model: string,
1031
+ apiKey: string,
1032
+ baseURL?: string
1033
+ ): InvokeConfig {
1034
+ return {
1035
+ model,
1036
+ apiKey,
1037
+ baseURL,
1038
+ };
1039
+ }