@renxqoo/renx-code 0.0.3 → 0.0.5

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 (505) hide show
  1. package/README.md +58 -223
  2. package/bin/renx.cjs +50 -0
  3. package/package.json +12 -111
  4. package/src/App.tsx +297 -0
  5. package/src/agent/runtime/event-format.ts +258 -0
  6. package/src/agent/runtime/model-types.ts +13 -0
  7. package/src/agent/runtime/runtime.context-usage.test.ts +192 -0
  8. package/src/agent/runtime/runtime.error-handling.test.ts +235 -0
  9. package/src/agent/runtime/runtime.simple.test.ts +16 -0
  10. package/src/agent/runtime/runtime.test.ts +296 -0
  11. package/src/agent/runtime/runtime.ts +875 -0
  12. package/src/agent/runtime/runtime.usage-forwarding.test.ts +228 -0
  13. package/src/agent/runtime/source-modules.test.ts +38 -0
  14. package/src/agent/runtime/source-modules.ts +370 -0
  15. package/src/agent/runtime/tool-call-buffer.test.ts +65 -0
  16. package/src/agent/runtime/tool-call-buffer.ts +60 -0
  17. package/src/agent/runtime/tool-confirmation.test.ts +56 -0
  18. package/src/agent/runtime/tool-confirmation.ts +15 -0
  19. package/src/agent/runtime/types.ts +99 -0
  20. package/src/commands/slash-commands.test.ts +216 -0
  21. package/src/commands/slash-commands.ts +64 -0
  22. package/src/components/chat/assistant-reply.test.tsx +47 -0
  23. package/src/components/chat/assistant-reply.tsx +136 -0
  24. package/src/components/chat/assistant-segment.test.ts +99 -0
  25. package/src/components/chat/assistant-segment.tsx +125 -0
  26. package/src/components/chat/assistant-tool-group.tsx +900 -0
  27. package/src/components/chat/code-block.test.tsx +206 -0
  28. package/src/components/chat/code-block.tsx +313 -0
  29. package/src/components/chat/prompt-card.tsx +81 -0
  30. package/src/components/chat/segment-groups.test.ts +52 -0
  31. package/src/components/chat/segment-groups.ts +106 -0
  32. package/src/components/chat/turn-item.tsx +39 -0
  33. package/src/components/conversation-panel.tsx +43 -0
  34. package/src/components/file-mention-menu.tsx +77 -0
  35. package/src/components/file-picker-dialog.tsx +206 -0
  36. package/src/components/footer-hints.tsx +75 -0
  37. package/src/components/model-picker-dialog.tsx +248 -0
  38. package/src/components/prompt.tsx +233 -0
  39. package/src/components/slash-command-menu.tsx +65 -0
  40. package/src/components/tool-confirm-dialog-content.test.ts +103 -0
  41. package/src/components/tool-confirm-dialog-content.ts +186 -0
  42. package/src/components/tool-confirm-dialog.tsx +187 -0
  43. package/src/components/tool-display-config.ts +119 -0
  44. package/src/context-usage-regressions.test.ts +26 -0
  45. package/src/files/attachment-capabilities.test.ts +30 -0
  46. package/src/files/attachment-capabilities.ts +50 -0
  47. package/src/files/attachment-content.ts +153 -0
  48. package/src/files/file-mention-query.test.ts +34 -0
  49. package/src/files/file-mention-query.ts +32 -0
  50. package/src/files/prompt-display.ts +13 -0
  51. package/src/files/types.ts +5 -0
  52. package/src/files/workspace-files.ts +63 -0
  53. package/src/hooks/agent-event-handlers.test.ts +207 -0
  54. package/src/hooks/agent-event-handlers.ts +196 -0
  55. package/src/hooks/chat-local-replies.fixed.test.ts +119 -0
  56. package/src/hooks/chat-local-replies.test.ts +153 -0
  57. package/src/hooks/chat-local-replies.ts +63 -0
  58. package/src/hooks/turn-updater.test.ts +70 -0
  59. package/src/hooks/turn-updater.ts +166 -0
  60. package/src/hooks/use-agent-chat.context.test.ts +10 -0
  61. package/src/hooks/use-agent-chat.status.test.ts +14 -0
  62. package/src/hooks/use-agent-chat.test.ts +80 -0
  63. package/src/hooks/use-agent-chat.ts +621 -0
  64. package/src/hooks/use-file-mention-menu.ts +196 -0
  65. package/src/hooks/use-file-picker.ts +185 -0
  66. package/src/hooks/use-model-picker.ts +196 -0
  67. package/src/hooks/use-slash-command-menu.ts +154 -0
  68. package/src/index.tsx +55 -0
  69. package/src/runtime/clipboard.test.ts +43 -0
  70. package/src/runtime/clipboard.ts +89 -0
  71. package/src/runtime/exit.test.ts +177 -0
  72. package/src/runtime/exit.ts +98 -0
  73. package/src/runtime/runtime-support.test.ts +31 -0
  74. package/src/runtime/terminal-theme.test.ts +55 -0
  75. package/src/runtime/terminal-theme.ts +196 -0
  76. package/src/types/chat.ts +32 -0
  77. package/src/types/message-content.ts +48 -0
  78. package/src/ui/open-code-theme.ts +176 -0
  79. package/src/ui/opencode-markdown.ts +211 -0
  80. package/src/ui/theme.simple.test.ts +52 -0
  81. package/src/ui/theme.test.ts +151 -0
  82. package/src/ui/theme.ts +152 -0
  83. package/src/utils/time.test.ts +144 -0
  84. package/src/utils/time.ts +7 -0
  85. package/tsconfig.json +30 -0
  86. package/vendor/agent-root/src/agent/ENTERPRISE_ACCEPTANCE_CHECKLIST.md +95 -0
  87. package/vendor/agent-root/src/agent/ENTERPRISE_REALTIME.html +1345 -0
  88. package/vendor/agent-root/src/agent/ENTERPRISE_REALTIME.md +1353 -0
  89. package/vendor/agent-root/src/agent/ERROR_CONTRACT.md +60 -0
  90. package/vendor/agent-root/src/agent/TEST_COVERAGE_ANALYSIS.md +278 -0
  91. package/vendor/agent-root/src/agent/__test__/error-contract.test.ts +72 -0
  92. package/vendor/agent-root/src/agent/__test__/types.test.ts +137 -0
  93. package/vendor/agent-root/src/agent/agent/__test__/abort-runtime.test.ts +83 -0
  94. package/vendor/agent-root/src/agent/agent/__test__/callback-safety.test.ts +34 -0
  95. package/vendor/agent-root/src/agent/agent/__test__/compaction.test.ts +323 -0
  96. package/vendor/agent-root/src/agent/agent/__test__/concurrency.test.ts +290 -0
  97. package/vendor/agent-root/src/agent/agent/__test__/error-normalizer.test.ts +377 -0
  98. package/vendor/agent-root/src/agent/agent/__test__/error.test.ts +212 -0
  99. package/vendor/agent-root/src/agent/agent/__test__/fault-injection.test.ts +295 -0
  100. package/vendor/agent-root/src/agent/agent/__test__/index.test.ts +3607 -0
  101. package/vendor/agent-root/src/agent/agent/__test__/logger.test.ts +35 -0
  102. package/vendor/agent-root/src/agent/agent/__test__/message-utils.test.ts +517 -0
  103. package/vendor/agent-root/src/agent/agent/__test__/telemetry.test.ts +97 -0
  104. package/vendor/agent-root/src/agent/agent/__test__/timeout-budget.test.ts +479 -0
  105. package/vendor/agent-root/src/agent/agent/__test__/tool-call-merge.test.ts +80 -0
  106. package/vendor/agent-root/src/agent/agent/__test__/tool-execution-ledger.test.ts +76 -0
  107. package/vendor/agent-root/src/agent/agent/__test__/write-buffer.test.ts +173 -0
  108. package/vendor/agent-root/src/agent/agent/__test__/write-file-session.test.ts +109 -0
  109. package/vendor/agent-root/src/agent/agent/abort-runtime.ts +71 -0
  110. package/vendor/agent-root/src/agent/agent/callback-safety.ts +33 -0
  111. package/vendor/agent-root/src/agent/agent/compaction.ts +291 -0
  112. package/vendor/agent-root/src/agent/agent/concurrency.ts +103 -0
  113. package/vendor/agent-root/src/agent/agent/error-normalizer.ts +190 -0
  114. package/vendor/agent-root/src/agent/agent/error.ts +198 -0
  115. package/vendor/agent-root/src/agent/agent/index.ts +1772 -0
  116. package/vendor/agent-root/src/agent/agent/logger.ts +65 -0
  117. package/vendor/agent-root/src/agent/agent/message-utils.ts +101 -0
  118. package/vendor/agent-root/src/agent/agent/stream-events.ts +61 -0
  119. package/vendor/agent-root/src/agent/agent/telemetry.ts +123 -0
  120. package/vendor/agent-root/src/agent/agent/timeout-budget.ts +227 -0
  121. package/vendor/agent-root/src/agent/agent/tool-call-merge.ts +111 -0
  122. package/vendor/agent-root/src/agent/agent/tool-execution-ledger.ts +164 -0
  123. package/vendor/agent-root/src/agent/agent/write-buffer.ts +188 -0
  124. package/vendor/agent-root/src/agent/agent/write-file-session.ts +238 -0
  125. package/vendor/agent-root/src/agent/app/__test__/agent-app-service.test.ts +1053 -0
  126. package/vendor/agent-root/src/agent/app/__test__/minimal-agent-application.test.ts +158 -0
  127. package/vendor/agent-root/src/agent/app/__test__/sqlite-agent-app-store.test.ts +437 -0
  128. package/vendor/agent-root/src/agent/app/agent-app-service.ts +748 -0
  129. package/vendor/agent-root/src/agent/app/contracts.ts +109 -0
  130. package/vendor/agent-root/src/agent/app/index.ts +5 -0
  131. package/vendor/agent-root/src/agent/app/minimal-agent-application.ts +151 -0
  132. package/vendor/agent-root/src/agent/app/ports.ts +72 -0
  133. package/vendor/agent-root/src/agent/app/sqlite-agent-app-store.ts +1182 -0
  134. package/vendor/agent-root/src/agent/app/sqlite-client.ts +177 -0
  135. package/vendor/agent-root/src/agent/docs/cli-app-layer/00-README.md +36 -0
  136. package/vendor/agent-root/src/agent/docs/cli-app-layer/01-scope-and-goals.md +33 -0
  137. package/vendor/agent-root/src/agent/docs/cli-app-layer/02-architecture-overview.md +40 -0
  138. package/vendor/agent-root/src/agent/docs/cli-app-layer/03-domain-model-and-contracts.md +91 -0
  139. package/vendor/agent-root/src/agent/docs/cli-app-layer/04-ports-and-interfaces.md +116 -0
  140. package/vendor/agent-root/src/agent/docs/cli-app-layer/05-run-orchestration-and-state-machine.md +52 -0
  141. package/vendor/agent-root/src/agent/docs/cli-app-layer/06-cli-commands-and-ux.md +53 -0
  142. package/vendor/agent-root/src/agent/docs/cli-app-layer/07-storage-design-local.md +52 -0
  143. package/vendor/agent-root/src/agent/docs/cli-app-layer/08-error-and-observability.md +40 -0
  144. package/vendor/agent-root/src/agent/docs/cli-app-layer/09-security-and-policy-boundary.md +19 -0
  145. package/vendor/agent-root/src/agent/docs/cli-app-layer/10-test-plan-and-acceptance.md +28 -0
  146. package/vendor/agent-root/src/agent/docs/cli-app-layer/11-implementation-phases.md +26 -0
  147. package/vendor/agent-root/src/agent/docs/cli-app-layer/12-open-questions-and-risks.md +30 -0
  148. package/vendor/agent-root/src/agent/docs/cli-app-layer/13-sqlite-schema-fields-and-rationale.md +567 -0
  149. package/vendor/agent-root/src/agent/docs/cli-app-layer/14-project-flow-mermaid.md +583 -0
  150. package/vendor/agent-root/src/agent/docs/cli-app-layer/15-openclaw-style-project-blueprint.md +972 -0
  151. package/vendor/agent-root/src/agent/error-contract.ts +154 -0
  152. package/{dist/agent/prompts/system.js → vendor/agent-root/src/agent/prompts/system.ts} +47 -23
  153. package/vendor/agent-root/src/agent/prompts/system1.ts +208 -0
  154. package/vendor/agent-root/src/agent/storage/__test__/file-history-store.test.ts +98 -0
  155. package/vendor/agent-root/src/agent/storage/file-history-store.ts +313 -0
  156. package/vendor/agent-root/src/agent/storage/file-storage-config.ts +94 -0
  157. package/vendor/agent-root/src/agent/storage/file-system.ts +31 -0
  158. package/vendor/agent-root/src/agent/storage/file-write-service.ts +21 -0
  159. package/vendor/agent-root/src/agent/tool/__test__/base-tool.test.ts +413 -0
  160. package/vendor/agent-root/src/agent/tool/__test__/bash-policy.test.ts +356 -0
  161. package/vendor/agent-root/src/agent/tool/__test__/bash.mocked-coverage.test.ts +375 -0
  162. package/vendor/agent-root/src/agent/tool/__test__/bash.test.ts +372 -0
  163. package/vendor/agent-root/src/agent/tool/__test__/error.test.ts +108 -0
  164. package/vendor/agent-root/src/agent/tool/__test__/file-edit-tool.test.ts +258 -0
  165. package/vendor/agent-root/src/agent/tool/__test__/file-history-tools.test.ts +121 -0
  166. package/vendor/agent-root/src/agent/tool/__test__/file-read-tool.test.ts +210 -0
  167. package/vendor/agent-root/src/agent/tool/__test__/glob.test.ts +139 -0
  168. package/vendor/agent-root/src/agent/tool/__test__/grep.mocked-coverage.test.ts +456 -0
  169. package/vendor/agent-root/src/agent/tool/__test__/grep.test.ts +192 -0
  170. package/vendor/agent-root/src/agent/tool/__test__/lsp.test.ts +300 -0
  171. package/vendor/agent-root/src/agent/tool/__test__/outside-workspace-confirmation.test.ts +214 -0
  172. package/vendor/agent-root/src/agent/tool/__test__/path-security.test.ts +336 -0
  173. package/vendor/agent-root/src/agent/tool/__test__/skill-loader.test.ts +494 -0
  174. package/vendor/agent-root/src/agent/tool/__test__/skill-parser.test.ts +543 -0
  175. package/vendor/agent-root/src/agent/tool/__test__/skill-tool.test.ts +172 -0
  176. package/vendor/agent-root/src/agent/tool/__test__/task-concurrency-and-version.test.ts +116 -0
  177. package/vendor/agent-root/src/agent/tool/__test__/task-create-get-list-update.test.ts +267 -0
  178. package/vendor/agent-root/src/agent/tool/__test__/task-create.test.ts +519 -0
  179. package/vendor/agent-root/src/agent/tool/__test__/task-errors.test.ts +225 -0
  180. package/vendor/agent-root/src/agent/tool/__test__/task-output-blocking.test.ts +223 -0
  181. package/vendor/agent-root/src/agent/tool/__test__/task-output.test.ts +184 -0
  182. package/vendor/agent-root/src/agent/tool/__test__/task-parent-abort.test.ts +287 -0
  183. package/vendor/agent-root/src/agent/tool/__test__/task-real-runner-adapter.test.ts +190 -0
  184. package/vendor/agent-root/src/agent/tool/__test__/task-run-lifecycle.test.ts +352 -0
  185. package/vendor/agent-root/src/agent/tool/__test__/task-store-runner-branches.test.ts +395 -0
  186. package/vendor/agent-root/src/agent/tool/__test__/task-store.test.ts +391 -0
  187. package/vendor/agent-root/src/agent/tool/__test__/task-subagent-config-integration.test.ts +176 -0
  188. package/vendor/agent-root/src/agent/tool/__test__/task-subagent-config.test.ts +68 -0
  189. package/vendor/agent-root/src/agent/tool/__test__/task-tools-core-edges.test.ts +630 -0
  190. package/vendor/agent-root/src/agent/tool/__test__/task-tools-runtime-edges.test.ts +732 -0
  191. package/vendor/agent-root/src/agent/tool/__test__/task-types.test.ts +494 -0
  192. package/vendor/agent-root/src/agent/tool/__test__/task-utils-branches.test.ts +175 -0
  193. package/vendor/agent-root/src/agent/tool/__test__/tool-manager.test.ts +505 -0
  194. package/vendor/agent-root/src/agent/tool/__test__/types.test.ts +55 -0
  195. package/vendor/agent-root/src/agent/tool/__test__/web-fetch.test.ts +244 -0
  196. package/vendor/agent-root/src/agent/tool/__test__/web-search.test.ts +290 -0
  197. package/vendor/agent-root/src/agent/tool/__test__/write-file.test.ts +368 -0
  198. package/vendor/agent-root/src/agent/tool/base-tool.ts +345 -0
  199. package/vendor/agent-root/src/agent/tool/bash-policy.ts +636 -0
  200. package/vendor/agent-root/src/agent/tool/bash.ts +688 -0
  201. package/vendor/agent-root/src/agent/tool/error.ts +131 -0
  202. package/vendor/agent-root/src/agent/tool/file-edit-tool.ts +264 -0
  203. package/vendor/agent-root/src/agent/tool/file-history-list.ts +103 -0
  204. package/vendor/agent-root/src/agent/tool/file-history-restore.ts +149 -0
  205. package/vendor/agent-root/src/agent/tool/file-read-tool.ts +211 -0
  206. package/vendor/agent-root/src/agent/tool/glob.ts +171 -0
  207. package/vendor/agent-root/src/agent/tool/grep.ts +496 -0
  208. package/vendor/agent-root/src/agent/tool/lsp.ts +481 -0
  209. package/vendor/agent-root/src/agent/tool/path-security.ts +117 -0
  210. package/vendor/agent-root/src/agent/tool/search/common.ts +153 -0
  211. package/vendor/agent-root/src/agent/tool/skill/index.ts +13 -0
  212. package/vendor/agent-root/src/agent/tool/skill/loader.ts +229 -0
  213. package/vendor/agent-root/src/agent/tool/skill/parser.ts +124 -0
  214. package/vendor/agent-root/src/agent/tool/skill/types.ts +27 -0
  215. package/vendor/agent-root/src/agent/tool/skill-tool.ts +143 -0
  216. package/vendor/agent-root/src/agent/tool/task-create.ts +186 -0
  217. package/vendor/agent-root/src/agent/tool/task-errors.ts +42 -0
  218. package/vendor/agent-root/src/agent/tool/task-get.ts +116 -0
  219. package/vendor/agent-root/src/agent/tool/task-graph.ts +78 -0
  220. package/vendor/agent-root/src/agent/tool/task-list.ts +141 -0
  221. package/vendor/agent-root/src/agent/tool/task-mock-runner-adapter.ts +232 -0
  222. package/vendor/agent-root/src/agent/tool/task-output.ts +223 -0
  223. package/vendor/agent-root/src/agent/tool/task-parent-abort.ts +115 -0
  224. package/vendor/agent-root/src/agent/tool/task-real-runner-adapter.ts +336 -0
  225. package/vendor/agent-root/src/agent/tool/task-runner-adapter.ts +55 -0
  226. package/vendor/agent-root/src/agent/tool/task-stop.ts +187 -0
  227. package/vendor/agent-root/src/agent/tool/task-store.ts +217 -0
  228. package/vendor/agent-root/src/agent/tool/task-subagent-config.ts +149 -0
  229. package/vendor/agent-root/src/agent/tool/task-types.ts +264 -0
  230. package/vendor/agent-root/src/agent/tool/task-update.ts +315 -0
  231. package/vendor/agent-root/src/agent/tool/task.ts +209 -0
  232. package/vendor/agent-root/src/agent/tool/tool-manager.ts +362 -0
  233. package/vendor/agent-root/src/agent/tool/tool-prompts.ts +242 -0
  234. package/vendor/agent-root/src/agent/tool/types.ts +116 -0
  235. package/vendor/agent-root/src/agent/tool/web-fetch.ts +227 -0
  236. package/vendor/agent-root/src/agent/tool/web-search.ts +208 -0
  237. package/vendor/agent-root/src/agent/tool/write-file.ts +497 -0
  238. package/vendor/agent-root/src/agent/types.ts +232 -0
  239. package/vendor/agent-root/src/agent/utils/__tests__/index.test.ts +18 -0
  240. package/vendor/agent-root/src/agent/utils/__tests__/message-utils.test.ts +610 -0
  241. package/vendor/agent-root/src/agent/utils/__tests__/message.test.ts +223 -0
  242. package/vendor/agent-root/src/agent/utils/__tests__/token.test.ts +42 -0
  243. package/vendor/agent-root/src/agent/utils/index.ts +16 -0
  244. package/vendor/agent-root/src/agent/utils/message.ts +171 -0
  245. package/vendor/agent-root/src/agent/utils/token.ts +28 -0
  246. package/vendor/agent-root/src/config/__tests__/load-config-to-env.test.ts +129 -0
  247. package/vendor/agent-root/src/config/__tests__/loader.test.ts +247 -0
  248. package/vendor/agent-root/src/config/__tests__/runtime.test.ts +88 -0
  249. package/vendor/agent-root/src/config/index.ts +54 -0
  250. package/vendor/agent-root/src/config/loader.ts +431 -0
  251. package/vendor/agent-root/src/config/paths.ts +30 -0
  252. package/vendor/agent-root/src/config/runtime.ts +163 -0
  253. package/vendor/agent-root/src/config/types.ts +70 -0
  254. package/vendor/agent-root/src/logger/index.ts +57 -0
  255. package/vendor/agent-root/src/logger/logger.ts +819 -0
  256. package/vendor/agent-root/src/logger/types.ts +150 -0
  257. package/vendor/agent-root/src/providers/__tests__/errors.test.ts +441 -0
  258. package/vendor/agent-root/src/providers/__tests__/index.test.ts +16 -0
  259. package/vendor/agent-root/src/providers/__tests__/openai-compatible.options.test.ts +318 -0
  260. package/vendor/agent-root/src/providers/__tests__/openai-compatible.test.ts +600 -0
  261. package/vendor/agent-root/src/providers/__tests__/registry.test.ts +449 -0
  262. package/vendor/agent-root/src/providers/__tests__/responses-adapter.test.ts +298 -0
  263. package/vendor/agent-root/src/providers/adapters/__tests__/anthropic.test.ts +354 -0
  264. package/vendor/agent-root/src/providers/adapters/__tests__/kimi.test.ts +58 -0
  265. package/vendor/agent-root/src/providers/adapters/__tests__/standard.test.ts +261 -0
  266. package/vendor/agent-root/src/providers/adapters/anthropic.ts +572 -0
  267. package/vendor/agent-root/src/providers/adapters/base.ts +131 -0
  268. package/vendor/agent-root/src/providers/adapters/kimi.ts +48 -0
  269. package/vendor/agent-root/src/providers/adapters/responses.ts +732 -0
  270. package/vendor/agent-root/src/providers/adapters/standard.ts +120 -0
  271. package/vendor/agent-root/src/providers/http/__tests__/client.timeout.test.ts +313 -0
  272. package/vendor/agent-root/src/providers/http/client.ts +289 -0
  273. package/vendor/agent-root/src/providers/http/stream-parser.ts +109 -0
  274. package/vendor/agent-root/src/providers/index.ts +76 -0
  275. package/vendor/agent-root/src/providers/kimi-headers.ts +177 -0
  276. package/vendor/agent-root/src/providers/openai-compatible.ts +387 -0
  277. package/vendor/agent-root/src/providers/registry/model-config.ts +230 -0
  278. package/vendor/agent-root/src/providers/registry/provider-factory.ts +123 -0
  279. package/vendor/agent-root/src/providers/registry.ts +135 -0
  280. package/vendor/agent-root/src/providers/types/api.ts +284 -0
  281. package/vendor/agent-root/src/providers/types/config.ts +58 -0
  282. package/vendor/agent-root/src/providers/types/errors.ts +323 -0
  283. package/vendor/agent-root/src/providers/types/index.ts +72 -0
  284. package/vendor/agent-root/src/providers/types/provider.ts +45 -0
  285. package/vendor/agent-root/src/providers/types/registry.ts +88 -0
  286. package/LICENSE +0 -21
  287. package/dist/App.d.ts +0 -2
  288. package/dist/App.d.ts.map +0 -1
  289. package/dist/App.js +0 -170
  290. package/dist/App.js.map +0 -1
  291. package/dist/agent/prompts/system.d.ts +0 -24
  292. package/dist/agent/prompts/system.d.ts.map +0 -1
  293. package/dist/agent/prompts/system.js.map +0 -1
  294. package/dist/agent/runtime/event-format.d.ts +0 -17
  295. package/dist/agent/runtime/event-format.d.ts.map +0 -1
  296. package/dist/agent/runtime/event-format.js +0 -194
  297. package/dist/agent/runtime/event-format.js.map +0 -1
  298. package/dist/agent/runtime/model-types.d.ts +0 -13
  299. package/dist/agent/runtime/model-types.d.ts.map +0 -1
  300. package/dist/agent/runtime/model-types.js +0 -1
  301. package/dist/agent/runtime/model-types.js.map +0 -1
  302. package/dist/agent/runtime/runtime.d.ts +0 -16
  303. package/dist/agent/runtime/runtime.d.ts.map +0 -1
  304. package/dist/agent/runtime/runtime.js +0 -691
  305. package/dist/agent/runtime/runtime.js.map +0 -1
  306. package/dist/agent/runtime/source-modules.d.ts +0 -176
  307. package/dist/agent/runtime/source-modules.d.ts.map +0 -1
  308. package/dist/agent/runtime/source-modules.js +0 -110
  309. package/dist/agent/runtime/source-modules.js.map +0 -1
  310. package/dist/agent/runtime/tool-call-buffer.d.ts +0 -12
  311. package/dist/agent/runtime/tool-call-buffer.d.ts.map +0 -1
  312. package/dist/agent/runtime/tool-call-buffer.js +0 -48
  313. package/dist/agent/runtime/tool-call-buffer.js.map +0 -1
  314. package/dist/agent/runtime/tool-confirmation.d.ts +0 -3
  315. package/dist/agent/runtime/tool-confirmation.d.ts.map +0 -1
  316. package/dist/agent/runtime/tool-confirmation.js +0 -9
  317. package/dist/agent/runtime/tool-confirmation.js.map +0 -1
  318. package/dist/agent/runtime/types.d.ts +0 -86
  319. package/dist/agent/runtime/types.d.ts.map +0 -1
  320. package/dist/agent/runtime/types.js +0 -1
  321. package/dist/agent/runtime/types.js.map +0 -1
  322. package/dist/cli.d.ts +0 -3
  323. package/dist/cli.d.ts.map +0 -1
  324. package/dist/cli.js +0 -12
  325. package/dist/cli.js.map +0 -1
  326. package/dist/commands/slash-commands.d.ts +0 -11
  327. package/dist/commands/slash-commands.d.ts.map +0 -1
  328. package/dist/commands/slash-commands.js +0 -48
  329. package/dist/commands/slash-commands.js.map +0 -1
  330. package/dist/components/chat/assistant-reply.d.ts +0 -13
  331. package/dist/components/chat/assistant-reply.d.ts.map +0 -1
  332. package/dist/components/chat/assistant-reply.js +0 -78
  333. package/dist/components/chat/assistant-reply.js.map +0 -1
  334. package/dist/components/chat/assistant-segment.d.ts +0 -8
  335. package/dist/components/chat/assistant-segment.d.ts.map +0 -1
  336. package/dist/components/chat/assistant-segment.js +0 -54
  337. package/dist/components/chat/assistant-segment.js.map +0 -1
  338. package/dist/components/chat/assistant-tool-group.d.ts +0 -7
  339. package/dist/components/chat/assistant-tool-group.d.ts.map +0 -1
  340. package/dist/components/chat/assistant-tool-group.js +0 -695
  341. package/dist/components/chat/assistant-tool-group.js.map +0 -1
  342. package/dist/components/chat/code-block.d.ts +0 -16
  343. package/dist/components/chat/code-block.d.ts.map +0 -1
  344. package/dist/components/chat/code-block.js +0 -194
  345. package/dist/components/chat/code-block.js.map +0 -1
  346. package/dist/components/chat/prompt-card.d.ts +0 -9
  347. package/dist/components/chat/prompt-card.d.ts.map +0 -1
  348. package/dist/components/chat/prompt-card.js +0 -18
  349. package/dist/components/chat/prompt-card.js.map +0 -1
  350. package/dist/components/chat/segment-groups.d.ts +0 -24
  351. package/dist/components/chat/segment-groups.d.ts.map +0 -1
  352. package/dist/components/chat/segment-groups.js +0 -69
  353. package/dist/components/chat/segment-groups.js.map +0 -1
  354. package/dist/components/chat/turn-item.d.ts +0 -9
  355. package/dist/components/chat/turn-item.d.ts.map +0 -1
  356. package/dist/components/chat/turn-item.js +0 -11
  357. package/dist/components/chat/turn-item.js.map +0 -1
  358. package/dist/components/conversation-panel.d.ts +0 -8
  359. package/dist/components/conversation-panel.d.ts.map +0 -1
  360. package/dist/components/conversation-panel.js +0 -8
  361. package/dist/components/conversation-panel.js.map +0 -1
  362. package/dist/components/file-mention-menu.d.ts +0 -11
  363. package/dist/components/file-mention-menu.d.ts.map +0 -1
  364. package/dist/components/file-mention-menu.js +0 -15
  365. package/dist/components/file-mention-menu.js.map +0 -1
  366. package/dist/components/file-picker-dialog.d.ts +0 -21
  367. package/dist/components/file-picker-dialog.d.ts.map +0 -1
  368. package/dist/components/file-picker-dialog.js +0 -48
  369. package/dist/components/file-picker-dialog.js.map +0 -1
  370. package/dist/components/footer-hints.d.ts +0 -7
  371. package/dist/components/footer-hints.d.ts.map +0 -1
  372. package/dist/components/footer-hints.js +0 -29
  373. package/dist/components/footer-hints.js.map +0 -1
  374. package/dist/components/model-picker-dialog.d.ts +0 -20
  375. package/dist/components/model-picker-dialog.d.ts.map +0 -1
  376. package/dist/components/model-picker-dialog.js +0 -72
  377. package/dist/components/model-picker-dialog.js.map +0 -1
  378. package/dist/components/prompt.d.ts +0 -18
  379. package/dist/components/prompt.d.ts.map +0 -1
  380. package/dist/components/prompt.js +0 -96
  381. package/dist/components/prompt.js.map +0 -1
  382. package/dist/components/slash-command-menu.d.ts +0 -9
  383. package/dist/components/slash-command-menu.d.ts.map +0 -1
  384. package/dist/components/slash-command-menu.js +0 -20
  385. package/dist/components/slash-command-menu.js.map +0 -1
  386. package/dist/components/tool-confirm-dialog-content.d.ts +0 -15
  387. package/dist/components/tool-confirm-dialog-content.d.ts.map +0 -1
  388. package/dist/components/tool-confirm-dialog-content.js +0 -143
  389. package/dist/components/tool-confirm-dialog-content.js.map +0 -1
  390. package/dist/components/tool-confirm-dialog.d.ts +0 -12
  391. package/dist/components/tool-confirm-dialog.d.ts.map +0 -1
  392. package/dist/components/tool-confirm-dialog.js +0 -21
  393. package/dist/components/tool-confirm-dialog.js.map +0 -1
  394. package/dist/components/tool-display-config.d.ts +0 -11
  395. package/dist/components/tool-display-config.d.ts.map +0 -1
  396. package/dist/components/tool-display-config.js +0 -94
  397. package/dist/components/tool-display-config.js.map +0 -1
  398. package/dist/config/paths.d.ts +0 -7
  399. package/dist/config/paths.d.ts.map +0 -1
  400. package/dist/config/paths.js +0 -24
  401. package/dist/config/paths.js.map +0 -1
  402. package/dist/files/attachment-capabilities.d.ts +0 -19
  403. package/dist/files/attachment-capabilities.d.ts.map +0 -1
  404. package/dist/files/attachment-capabilities.js +0 -26
  405. package/dist/files/attachment-capabilities.js.map +0 -1
  406. package/dist/files/attachment-content.d.ts +0 -5
  407. package/dist/files/attachment-content.d.ts.map +0 -1
  408. package/dist/files/attachment-content.js +0 -117
  409. package/dist/files/attachment-content.js.map +0 -1
  410. package/dist/files/file-mention-query.d.ts +0 -9
  411. package/dist/files/file-mention-query.d.ts.map +0 -1
  412. package/dist/files/file-mention-query.js +0 -23
  413. package/dist/files/file-mention-query.js.map +0 -1
  414. package/dist/files/prompt-display.d.ts +0 -3
  415. package/dist/files/prompt-display.d.ts.map +0 -1
  416. package/dist/files/prompt-display.js +0 -11
  417. package/dist/files/prompt-display.js.map +0 -1
  418. package/dist/files/types.d.ts +0 -6
  419. package/dist/files/types.d.ts.map +0 -1
  420. package/dist/files/types.js +0 -1
  421. package/dist/files/types.js.map +0 -1
  422. package/dist/files/workspace-files.d.ts +0 -3
  423. package/dist/files/workspace-files.d.ts.map +0 -1
  424. package/dist/files/workspace-files.js +0 -48
  425. package/dist/files/workspace-files.js.map +0 -1
  426. package/dist/hooks/agent-event-handlers.d.ts +0 -11
  427. package/dist/hooks/agent-event-handlers.d.ts.map +0 -1
  428. package/dist/hooks/agent-event-handlers.js +0 -137
  429. package/dist/hooks/agent-event-handlers.js.map +0 -1
  430. package/dist/hooks/chat-local-replies.d.ts +0 -9
  431. package/dist/hooks/chat-local-replies.d.ts.map +0 -1
  432. package/dist/hooks/chat-local-replies.js +0 -54
  433. package/dist/hooks/chat-local-replies.js.map +0 -1
  434. package/dist/hooks/turn-updater.d.ts +0 -9
  435. package/dist/hooks/turn-updater.d.ts.map +0 -1
  436. package/dist/hooks/turn-updater.js +0 -103
  437. package/dist/hooks/turn-updater.js.map +0 -1
  438. package/dist/hooks/use-agent-chat.d.ts +0 -29
  439. package/dist/hooks/use-agent-chat.d.ts.map +0 -1
  440. package/dist/hooks/use-agent-chat.js +0 -455
  441. package/dist/hooks/use-agent-chat.js.map +0 -1
  442. package/dist/hooks/use-file-mention-menu.d.ts +0 -22
  443. package/dist/hooks/use-file-mention-menu.d.ts.map +0 -1
  444. package/dist/hooks/use-file-mention-menu.js +0 -137
  445. package/dist/hooks/use-file-mention-menu.js.map +0 -1
  446. package/dist/hooks/use-file-picker.d.ts +0 -21
  447. package/dist/hooks/use-file-picker.d.ts.map +0 -1
  448. package/dist/hooks/use-file-picker.js +0 -145
  449. package/dist/hooks/use-file-picker.js.map +0 -1
  450. package/dist/hooks/use-model-picker.d.ts +0 -23
  451. package/dist/hooks/use-model-picker.d.ts.map +0 -1
  452. package/dist/hooks/use-model-picker.js +0 -151
  453. package/dist/hooks/use-model-picker.js.map +0 -1
  454. package/dist/hooks/use-slash-command-menu.d.ts +0 -19
  455. package/dist/hooks/use-slash-command-menu.d.ts.map +0 -1
  456. package/dist/hooks/use-slash-command-menu.js +0 -101
  457. package/dist/hooks/use-slash-command-menu.js.map +0 -1
  458. package/dist/index.d.ts +0 -2
  459. package/dist/index.d.ts.map +0 -1
  460. package/dist/index.js +0 -6
  461. package/dist/index.js.map +0 -1
  462. package/dist/run-cli-app.d.ts +0 -2
  463. package/dist/run-cli-app.d.ts.map +0 -1
  464. package/dist/run-cli-app.js +0 -41
  465. package/dist/run-cli-app.js.map +0 -1
  466. package/dist/runtime/clipboard.d.ts +0 -10
  467. package/dist/runtime/clipboard.d.ts.map +0 -1
  468. package/dist/runtime/clipboard.js +0 -64
  469. package/dist/runtime/clipboard.js.map +0 -1
  470. package/dist/runtime/exit.d.ts +0 -7
  471. package/dist/runtime/exit.d.ts.map +0 -1
  472. package/dist/runtime/exit.js +0 -85
  473. package/dist/runtime/exit.js.map +0 -1
  474. package/dist/runtime/runtime-support.d.ts +0 -4
  475. package/dist/runtime/runtime-support.d.ts.map +0 -1
  476. package/dist/runtime/runtime-support.js +0 -19
  477. package/dist/runtime/runtime-support.js.map +0 -1
  478. package/dist/runtime/terminal-theme.d.ts +0 -25
  479. package/dist/runtime/terminal-theme.d.ts.map +0 -1
  480. package/dist/runtime/terminal-theme.js +0 -148
  481. package/dist/runtime/terminal-theme.js.map +0 -1
  482. package/dist/types/chat.d.ts +0 -29
  483. package/dist/types/chat.d.ts.map +0 -1
  484. package/dist/types/chat.js +0 -1
  485. package/dist/types/chat.js.map +0 -1
  486. package/dist/types/message-content.d.ts +0 -38
  487. package/dist/types/message-content.d.ts.map +0 -1
  488. package/dist/types/message-content.js +0 -1
  489. package/dist/types/message-content.js.map +0 -1
  490. package/dist/ui/open-code-theme.d.ts +0 -58
  491. package/dist/ui/open-code-theme.d.ts.map +0 -1
  492. package/dist/ui/open-code-theme.js +0 -113
  493. package/dist/ui/open-code-theme.js.map +0 -1
  494. package/dist/ui/opencode-markdown.d.ts +0 -7
  495. package/dist/ui/opencode-markdown.d.ts.map +0 -1
  496. package/dist/ui/opencode-markdown.js +0 -169
  497. package/dist/ui/opencode-markdown.js.map +0 -1
  498. package/dist/ui/theme.d.ts +0 -68
  499. package/dist/ui/theme.d.ts.map +0 -1
  500. package/dist/ui/theme.js +0 -80
  501. package/dist/ui/theme.js.map +0 -1
  502. package/dist/utils/time.d.ts +0 -2
  503. package/dist/utils/time.d.ts.map +0 -1
  504. package/dist/utils/time.js +0 -7
  505. package/dist/utils/time.js.map +0 -1
@@ -0,0 +1,1772 @@
1
+ import { createHash } from 'node:crypto';
2
+ import {
3
+ Message,
4
+ AgentInput,
5
+ AgentCallbacks,
6
+ AgentContextUsage,
7
+ CompactionInfo,
8
+ StreamEvent,
9
+ ErrorDecision,
10
+ ToolDecision,
11
+ } from '../types';
12
+ import { ToolManager } from '../tool/tool-manager';
13
+ import { LLMProvider, LLMRequestMessage, Tool, ToolCall } from '../../providers';
14
+ import { EventEmitter } from 'events';
15
+ import {
16
+ AgentAbortedError,
17
+ AgentError,
18
+ AgentUpstreamRetryableError,
19
+ MaxRetriesError,
20
+ TimeoutBudgetExceededError,
21
+ UnknownError,
22
+ } from './error';
23
+ import { mergeAgentLoggers, type AgentLogger } from './logger';
24
+ import { compact, estimateMessagesTokens } from './compaction';
25
+ import { LLMTool, ToolConcurrencyPolicy } from '../tool/types';
26
+ import type { BackoffConfig } from '../../providers';
27
+ import {
28
+ convertMessageToLLMMessage as toLLMMessage,
29
+ mergeLLMConfig as mergeLLMRequestConfig,
30
+ shouldSendMessageToLLM,
31
+ } from './message-utils';
32
+ import { processToolCallPairs } from '../utils/message';
33
+
34
+ import {
35
+ createCheckpoint,
36
+ createDoneEvent,
37
+ createErrorEvent,
38
+ createProgressEvent,
39
+ } from './stream-events';
40
+ import {
41
+ buildExecutionWaves as buildToolExecutionWaves,
42
+ runWithConcurrencyAndLock as runTasksWithConcurrencyAndLock,
43
+ } from './concurrency';
44
+ import {
45
+ calculateRetryDelay as calculateRetryDelayWithBackoff,
46
+ isAbortError as isAbortErrorByMessage,
47
+ normalizeError as normalizeAgentError,
48
+ } from './error-normalizer';
49
+ import {
50
+ createExecutionAbortScope as createExecutionBudgetScope,
51
+ createStageAbortScope as createStageBudgetScope,
52
+ createTimeoutBudgetState as createBudgetState,
53
+ type AbortScope,
54
+ type TimeoutBudgetState,
55
+ type TimeoutStage,
56
+ } from './timeout-budget';
57
+ import {
58
+ buildWriteFileSessionKey as createWriteFileSessionKey,
59
+ bufferWriteFileToolCallChunk as bufferWriteFileChunk,
60
+ cleanupWriteFileBufferIfNeeded as cleanupWriteFileBufferSession,
61
+ enrichWriteFileToolError as buildWriteFileToolErrorPayload,
62
+ isWriteFileProtocolOutput as isWriteFileProtocolResponse,
63
+ isWriteFileToolCall as isWriteFileTool,
64
+ shouldEnrichWriteFileFailure as needEnrichWriteFileFailure,
65
+ type WriteBufferRuntime,
66
+ } from './write-file-session';
67
+ import {
68
+ createToolResultMessage as buildToolResultMessage,
69
+ NoopToolExecutionLedger,
70
+ executeToolCallWithLedger as executeWithToolLedger,
71
+ type ToolExecutionLedger,
72
+ type ToolExecutionLedgerRecord,
73
+ } from './tool-execution-ledger';
74
+ import {
75
+ emitMetric as pushMetric,
76
+ emitTrace as pushTrace,
77
+ endSpan as finishSpan,
78
+ extractErrorCode as parseErrorCode,
79
+ logError as writeErrorLog,
80
+ logInfo as writeInfoLog,
81
+ logWarn as writeWarnLog,
82
+ startSpan as beginSpan,
83
+ type SpanRuntime,
84
+ } from './telemetry';
85
+ import {
86
+ safeCallback as invokeSafeCallback,
87
+ safeErrorCallback as invokeSafeErrorCallback,
88
+ } from './callback-safety';
89
+ import { mergeToolCalls as mergeToolCallsWithBuffer } from './tool-call-merge';
90
+ import type { ToolResult } from '../tool/base-tool';
91
+ import {
92
+ normalizeTimeoutBudgetError as normalizeAbortTimeoutBudgetError,
93
+ sleepWithAbort,
94
+ throwIfAborted as assertNotAborted,
95
+ timeoutBudgetErrorFromSignal as timeoutErrorFromAbortSignal,
96
+ } from './abort-runtime';
97
+
98
+ function generateId(prefix: string): string {
99
+ return `${prefix}${Date.now()}_${Math.random().toString(36).substring(2, 11)}`;
100
+ }
101
+
102
+ function hasNonEmptyText(value: unknown): value is string {
103
+ return typeof value === 'string' && value.length > 0;
104
+ }
105
+
106
+ type ContinuationMetadata = {
107
+ responseId?: string;
108
+ llmRequestConfigHash?: string;
109
+ llmRequestInputHash?: string;
110
+ llmRequestInputMessageCount?: number;
111
+ llmResponseMessageHash?: string;
112
+ continuationMode?: 'full' | 'incremental';
113
+ previousResponseIdUsed?: string;
114
+ continuationBaselineMessageCount?: number;
115
+ continuationDeltaMessageCount?: number;
116
+ };
117
+
118
+ type LLMRequestPlan = {
119
+ llmMessages: LLMRequestMessage[];
120
+ requestMessages: LLMRequestMessage[];
121
+ requestConfig: AgentInput['config'];
122
+ requestConfigHash: string;
123
+ requestInputHash: string;
124
+ requestInputMessageCount: number;
125
+ continuationMode: 'full' | 'incremental';
126
+ previousResponseIdUsed?: string;
127
+ continuationBaselineMessageCount?: number;
128
+ continuationDeltaMessageCount: number;
129
+ };
130
+
131
+ function isPlainRecord(value: unknown): value is Record<string, unknown> {
132
+ return typeof value === 'object' && value !== null && !Array.isArray(value);
133
+ }
134
+
135
+ function normalizeValueForHash(value: unknown): unknown {
136
+ if (value === undefined) {
137
+ return null;
138
+ }
139
+ if (
140
+ value === null ||
141
+ typeof value === 'string' ||
142
+ typeof value === 'number' ||
143
+ typeof value === 'boolean'
144
+ ) {
145
+ return value;
146
+ }
147
+ if (Array.isArray(value)) {
148
+ return value.map((item) => normalizeValueForHash(item));
149
+ }
150
+ if (typeof value === 'object') {
151
+ return Object.keys(value as Record<string, unknown>)
152
+ .sort()
153
+ .reduce<Record<string, unknown>>((acc, key) => {
154
+ const normalized = normalizeValueForHash((value as Record<string, unknown>)[key]);
155
+ if (normalized !== undefined) {
156
+ acc[key] = normalized;
157
+ }
158
+ return acc;
159
+ }, {});
160
+ }
161
+ return String(value);
162
+ }
163
+
164
+ function hashValueForContinuation(value: unknown): string {
165
+ return createHash('sha256')
166
+ .update(JSON.stringify(normalizeValueForHash(value)))
167
+ .digest('hex');
168
+ }
169
+
170
+ export interface AgentConfig {
171
+ maxRetryCount?: number;
172
+ enableCompaction?: boolean;
173
+ compactionTriggerRatio?: number;
174
+ compactionKeepMessagesNum?: number;
175
+ enableServerSideContinuation?: boolean;
176
+ backoffConfig?: BackoffConfig;
177
+ maxConcurrentToolCalls?: number;
178
+ toolConcurrencyPolicyResolver?: (toolCall: ToolCall) => ToolConcurrencyPolicy;
179
+ logger?: AgentLogger;
180
+ /**
181
+ * Optional external idempotency ledger.
182
+ * Defaults to Noop to keep the agent stateless across process restarts and scale-out replicas.
183
+ */
184
+ toolExecutionLedger?: ToolExecutionLedger;
185
+ timeoutBudgetMs?: number;
186
+ llmTimeoutRatio?: number;
187
+ }
188
+
189
+ export type { AgentLogger } from './logger';
190
+
191
+ interface InternalAgentConfig {
192
+ maxRetryCount: number;
193
+ enableCompaction: boolean;
194
+ compactionTriggerRatio: number;
195
+ compactionKeepMessagesNum: number;
196
+ enableServerSideContinuation: boolean;
197
+ backoffConfig: BackoffConfig;
198
+ maxConcurrentToolCalls: number;
199
+ toolConcurrencyPolicyResolver?: (toolCall: ToolCall) => ToolConcurrencyPolicy;
200
+ logger: AgentLogger;
201
+ timeoutBudgetMs?: number;
202
+ llmTimeoutRatio: number;
203
+ }
204
+
205
+ const DEFAULT_MAX_RETRY_COUNT = 20;
206
+ const DEFAULT_COMPACTION_TRIGGER_RATIO = 0.8;
207
+ const DEFAULT_COMPACTION_KEEP_MESSAGES = 20;
208
+ const DEFAULT_MAX_CONCURRENT_TOOL_CALLS = 1;
209
+ const DEFAULT_LLM_TIMEOUT_RATIO = 0.7;
210
+ const ABORTED_MESSAGE = 'Operation aborted';
211
+
212
+ export type { ToolExecutionLedger, ToolExecutionLedgerRecord } from './tool-execution-ledger';
213
+
214
+ export class StatelessAgent extends EventEmitter {
215
+ private llmProvider: LLMProvider;
216
+ private toolExecutor: ToolManager;
217
+ private config: InternalAgentConfig;
218
+ private logger: AgentLogger;
219
+ private toolExecutionLedger: ToolExecutionLedger;
220
+ constructor(llmProvider: LLMProvider, toolExecutor: ToolManager, config: AgentConfig) {
221
+ super();
222
+ this.llmProvider = llmProvider;
223
+ this.toolExecutor = toolExecutor;
224
+ this.logger = config.logger ?? {};
225
+ this.toolExecutionLedger = config.toolExecutionLedger ?? new NoopToolExecutionLedger();
226
+ const llmTimeoutRatio = Number.isFinite(config.llmTimeoutRatio)
227
+ ? Number(config.llmTimeoutRatio)
228
+ : DEFAULT_LLM_TIMEOUT_RATIO;
229
+ const clampedLlmTimeoutRatio = Math.min(0.95, Math.max(0.05, llmTimeoutRatio));
230
+ this.config = {
231
+ maxRetryCount: config.maxRetryCount ?? DEFAULT_MAX_RETRY_COUNT,
232
+ enableCompaction: config.enableCompaction ?? false,
233
+ compactionTriggerRatio: config.compactionTriggerRatio ?? DEFAULT_COMPACTION_TRIGGER_RATIO,
234
+ compactionKeepMessagesNum:
235
+ config.compactionKeepMessagesNum ?? DEFAULT_COMPACTION_KEEP_MESSAGES,
236
+ enableServerSideContinuation: config.enableServerSideContinuation ?? false,
237
+ backoffConfig: config.backoffConfig ?? {},
238
+ maxConcurrentToolCalls: Math.max(
239
+ 1,
240
+ Math.floor(config.maxConcurrentToolCalls ?? DEFAULT_MAX_CONCURRENT_TOOL_CALLS)
241
+ ),
242
+ toolConcurrencyPolicyResolver: config.toolConcurrencyPolicyResolver,
243
+ logger: this.logger,
244
+ timeoutBudgetMs:
245
+ config.timeoutBudgetMs &&
246
+ Number.isFinite(config.timeoutBudgetMs) &&
247
+ config.timeoutBudgetMs > 0
248
+ ? Math.floor(config.timeoutBudgetMs)
249
+ : undefined,
250
+ llmTimeoutRatio: clampedLlmTimeoutRatio,
251
+ };
252
+ }
253
+
254
+ getContextLimitTokens(contextLimitTokens?: number): number {
255
+ if (
256
+ typeof contextLimitTokens === 'number' &&
257
+ Number.isFinite(contextLimitTokens) &&
258
+ contextLimitTokens > 0
259
+ ) {
260
+ return Math.max(1, Math.floor(contextLimitTokens));
261
+ }
262
+ const maxTokens = this.llmProvider.getLLMMaxTokens();
263
+ const maxOutputTokens = this.llmProvider.getMaxOutputTokens();
264
+ return Math.max(1, maxTokens - maxOutputTokens);
265
+ }
266
+
267
+ estimateContextUsage(
268
+ messages: Message[],
269
+ tools?: Tool[],
270
+ contextLimitTokens?: number
271
+ ): Pick<AgentContextUsage, 'contextTokens' | 'contextLimitTokens' | 'contextUsagePercent'> {
272
+ const llmTools = tools as unknown as LLMTool[] | undefined;
273
+ const contextTokens = estimateMessagesTokens(messages, llmTools);
274
+ const resolvedContextLimitTokens = this.getContextLimitTokens(contextLimitTokens);
275
+ return {
276
+ contextTokens,
277
+ contextLimitTokens: resolvedContextLimitTokens,
278
+ contextUsagePercent: (contextTokens / resolvedContextLimitTokens) * 100,
279
+ };
280
+ }
281
+
282
+ attachLogger(logger: AgentLogger): void {
283
+ this.logger = mergeAgentLoggers(this.logger, logger);
284
+ this.config.logger = this.logger;
285
+ }
286
+
287
+ private convertMessageToLLMMessage(message: Message) {
288
+ return toLLMMessage(message);
289
+ }
290
+
291
+ private normalizeContinuationConfig(config: AgentInput['config']): Record<string, unknown> {
292
+ if (!config) {
293
+ return {};
294
+ }
295
+
296
+ const { abortSignal, previous_response_id, ...rest } = config as AgentInput['config'] & {
297
+ abortSignal?: AbortSignal;
298
+ previous_response_id?: string;
299
+ };
300
+ void abortSignal;
301
+ void previous_response_id;
302
+
303
+ return normalizeValueForHash(rest) as Record<string, unknown>;
304
+ }
305
+
306
+ private readContinuationMetadata(message: Message): ContinuationMetadata | undefined {
307
+ if (!isPlainRecord(message.metadata)) {
308
+ return undefined;
309
+ }
310
+
311
+ const metadata = message.metadata as Record<string, unknown>;
312
+ const responseId =
313
+ typeof metadata.responseId === 'string' && metadata.responseId.trim().length > 0
314
+ ? metadata.responseId
315
+ : undefined;
316
+ const llmRequestConfigHash =
317
+ typeof metadata.llmRequestConfigHash === 'string' ? metadata.llmRequestConfigHash : undefined;
318
+ const llmRequestInputHash =
319
+ typeof metadata.llmRequestInputHash === 'string' ? metadata.llmRequestInputHash : undefined;
320
+ const llmRequestInputMessageCount =
321
+ typeof metadata.llmRequestInputMessageCount === 'number'
322
+ ? metadata.llmRequestInputMessageCount
323
+ : undefined;
324
+ const llmResponseMessageHash =
325
+ typeof metadata.llmResponseMessageHash === 'string'
326
+ ? metadata.llmResponseMessageHash
327
+ : undefined;
328
+
329
+ if (
330
+ !responseId ||
331
+ !llmRequestConfigHash ||
332
+ !llmRequestInputHash ||
333
+ typeof llmRequestInputMessageCount !== 'number' ||
334
+ !Number.isInteger(llmRequestInputMessageCount) ||
335
+ llmRequestInputMessageCount < 0 ||
336
+ !llmResponseMessageHash
337
+ ) {
338
+ return undefined;
339
+ }
340
+
341
+ const safeInputMessageCount = llmRequestInputMessageCount;
342
+
343
+ return {
344
+ responseId,
345
+ llmRequestConfigHash,
346
+ llmRequestInputHash,
347
+ llmRequestInputMessageCount: safeInputMessageCount,
348
+ llmResponseMessageHash,
349
+ };
350
+ }
351
+
352
+ private buildLLMRequestPlan(messages: Message[], config: AgentInput['config']): LLMRequestPlan {
353
+ const llmSourceMessages = messages.filter((msg) => shouldSendMessageToLLM(msg));
354
+ const llmMessages = llmSourceMessages.map((msg) => this.convertMessageToLLMMessage(msg));
355
+ const requestConfigHash = hashValueForContinuation(this.normalizeContinuationConfig(config));
356
+ const requestInputHash = hashValueForContinuation(llmMessages);
357
+ const requestInputMessageCount = llmMessages.length;
358
+
359
+ const explicitPreviousResponseId =
360
+ typeof config?.previous_response_id === 'string' &&
361
+ config.previous_response_id.trim().length > 0
362
+ ? config.previous_response_id
363
+ : undefined;
364
+
365
+ if (explicitPreviousResponseId) {
366
+ return {
367
+ llmMessages,
368
+ requestMessages: llmMessages,
369
+ requestConfig: config,
370
+ requestConfigHash,
371
+ requestInputHash,
372
+ requestInputMessageCount,
373
+ continuationMode: 'full',
374
+ continuationDeltaMessageCount: llmMessages.length,
375
+ };
376
+ }
377
+
378
+ // Keep server-side continuation opt-in. In the current gateway environment
379
+ // full replay + prompt_cache_key is more stable than automatic previous_response_id chaining.
380
+ if (!this.config.enableServerSideContinuation) {
381
+ return {
382
+ llmMessages,
383
+ requestMessages: llmMessages,
384
+ requestConfig: config,
385
+ requestConfigHash,
386
+ requestInputHash,
387
+ requestInputMessageCount,
388
+ continuationMode: 'full',
389
+ continuationDeltaMessageCount: llmMessages.length,
390
+ };
391
+ }
392
+
393
+ for (let index = llmSourceMessages.length - 1; index >= 0; index -= 1) {
394
+ const candidate = llmSourceMessages[index];
395
+ if (candidate.role !== 'assistant') {
396
+ continue;
397
+ }
398
+
399
+ const metadata = this.readContinuationMetadata(candidate);
400
+ if (!metadata) {
401
+ continue;
402
+ }
403
+
404
+ if (metadata.llmRequestConfigHash !== requestConfigHash) {
405
+ break;
406
+ }
407
+
408
+ const prefixMessages = llmMessages.slice(0, index);
409
+ const currentAssistantMessage = llmMessages[index];
410
+ if (!currentAssistantMessage) {
411
+ break;
412
+ }
413
+
414
+ if (prefixMessages.length !== metadata.llmRequestInputMessageCount) {
415
+ break;
416
+ }
417
+
418
+ if (hashValueForContinuation(prefixMessages) !== metadata.llmRequestInputHash) {
419
+ break;
420
+ }
421
+
422
+ if (hashValueForContinuation(currentAssistantMessage) !== metadata.llmResponseMessageHash) {
423
+ break;
424
+ }
425
+
426
+ // If the delta contains a tool result, the paired assistant tool_call must
427
+ // be included as well, otherwise the Responses gateway rejects the request shape.
428
+ const continuationWindow = processToolCallPairs(
429
+ llmSourceMessages.slice(0, index + 1),
430
+ llmSourceMessages.slice(index + 1)
431
+ );
432
+ const deltaSourceMessages = continuationWindow.active;
433
+ if (deltaSourceMessages.length === 0) {
434
+ break;
435
+ }
436
+ const deltaMessages = deltaSourceMessages.map((msg) => this.convertMessageToLLMMessage(msg));
437
+
438
+ return {
439
+ llmMessages,
440
+ requestMessages: deltaMessages,
441
+ requestConfig: {
442
+ ...(config || {}),
443
+ previous_response_id: metadata.responseId,
444
+ },
445
+ requestConfigHash,
446
+ requestInputHash,
447
+ requestInputMessageCount,
448
+ continuationMode: 'incremental',
449
+ previousResponseIdUsed: metadata.responseId,
450
+ continuationBaselineMessageCount: continuationWindow.pending.length,
451
+ continuationDeltaMessageCount: deltaMessages.length,
452
+ };
453
+ }
454
+
455
+ return {
456
+ llmMessages,
457
+ requestMessages: llmMessages,
458
+ requestConfig: config,
459
+ requestConfigHash,
460
+ requestInputHash,
461
+ requestInputMessageCount,
462
+ continuationMode: 'full',
463
+ continuationDeltaMessageCount: llmMessages.length,
464
+ };
465
+ }
466
+
467
+ private needsCompaction(
468
+ messages: Message[],
469
+ tools?: Tool[],
470
+ contextLimitTokens?: number
471
+ ): boolean {
472
+ if (!this.config.enableCompaction) {
473
+ return false;
474
+ }
475
+
476
+ const usableLimit = this.getContextLimitTokens(contextLimitTokens);
477
+ const threshold = usableLimit * this.config.compactionTriggerRatio;
478
+
479
+ const llmTools = tools as unknown as LLMTool[] | undefined;
480
+ const currentTokens = estimateMessagesTokens(messages, llmTools);
481
+
482
+ return currentTokens >= threshold;
483
+ }
484
+
485
+ private async compactMessagesIfNeeded(
486
+ messages: Message[],
487
+ tools?: Tool[],
488
+ contextLimitTokens?: number
489
+ ): Promise<string[]> {
490
+ if (!this.needsCompaction(messages, tools, contextLimitTokens)) {
491
+ return [];
492
+ }
493
+
494
+ try {
495
+ const result = await compact(messages, {
496
+ provider: this.llmProvider,
497
+ keepMessagesNum: this.config.compactionKeepMessagesNum,
498
+ });
499
+ messages.splice(0, messages.length, ...result.messages);
500
+
501
+ return result.removedMessageIds ?? [];
502
+ } catch (error) {
503
+ this.logError('[Agent] Compaction failed:', error);
504
+ return [];
505
+ }
506
+ }
507
+
508
+ async *runStream(
509
+ input: AgentInput,
510
+ callbacks?: AgentCallbacks
511
+ ): AsyncGenerator<StreamEvent, void, unknown> {
512
+ const { messages: inputMessages, maxSteps = 100, abortSignal: inputAbortSignal } = input;
513
+ const effectiveTools = this.resolveLLMTools(input.tools);
514
+ const messages = [...inputMessages];
515
+ if (typeof input.systemPrompt === 'string' && input.systemPrompt.trim().length > 0) {
516
+ const hasSystemMessage = messages.some((message) => message.role === 'system');
517
+ if (!hasSystemMessage) {
518
+ messages.unshift({
519
+ messageId: generateId('msg_sys_'),
520
+ type: 'system',
521
+ role: 'system',
522
+ content: input.systemPrompt,
523
+ timestamp: Date.now(),
524
+ });
525
+ }
526
+ }
527
+ const writeBufferSessions = new Map<string, WriteBufferRuntime>();
528
+ const timeoutBudget = this.createTimeoutBudgetState(input);
529
+ const executionScope = this.createExecutionAbortScope(inputAbortSignal, timeoutBudget);
530
+ const abortSignal = executionScope.signal;
531
+ const traceId = input.executionId || generateId('trace_');
532
+ const runSpan = await this.startSpan(callbacks, traceId, 'agent.run', undefined, {
533
+ executionId: input.executionId,
534
+ conversationId: input.conversationId,
535
+ maxSteps,
536
+ timeoutBudgetMs: timeoutBudget?.totalMs,
537
+ });
538
+ this.logInfo('[Agent] run.start', {
539
+ executionId: input.executionId,
540
+ traceId,
541
+ spanId: runSpan.spanId,
542
+ messageCount: messages.length,
543
+ });
544
+
545
+ let stepIndex = 0;
546
+ let retryCount = 0;
547
+ let runOutcome: 'done' | 'error' | 'aborted' | 'timeout' | 'max_retries' | 'max_steps' = 'done';
548
+ let runErrorCode: string | undefined;
549
+ let terminalDoneEmitted = false;
550
+
551
+ try {
552
+ while (stepIndex < maxSteps) {
553
+ if (abortSignal?.aborted) {
554
+ const timeoutError = this.timeoutBudgetErrorFromSignal(abortSignal);
555
+ if (timeoutError) {
556
+ runOutcome = 'timeout';
557
+ runErrorCode = timeoutError.errorCode;
558
+ yield* this.yieldErrorEvent(timeoutError);
559
+ } else {
560
+ runOutcome = 'aborted';
561
+ runErrorCode = 'AGENT_ABORTED';
562
+ yield* this.yieldErrorEvent(new AgentAbortedError(ABORTED_MESSAGE));
563
+ }
564
+ break;
565
+ }
566
+
567
+ if (retryCount >= this.config.maxRetryCount) {
568
+ runOutcome = 'max_retries';
569
+ runErrorCode = 'AGENT_MAX_RETRIES_REACHED';
570
+ yield* this.yieldMaxRetriesError();
571
+ break;
572
+ }
573
+
574
+ stepIndex++;
575
+
576
+ try {
577
+ this.throwIfAborted(abortSignal);
578
+ const messageCountBeforeCompaction = messages.length;
579
+ const removedMessageIds = await this.compactMessagesIfNeeded(
580
+ messages,
581
+ effectiveTools,
582
+ input.contextLimitTokens
583
+ );
584
+ if (removedMessageIds.length > 0) {
585
+ const compactionInfo: CompactionInfo = {
586
+ executionId: input.executionId,
587
+ stepIndex,
588
+ removedMessageIds,
589
+ messageCountBefore: messageCountBeforeCompaction,
590
+ messageCountAfter: messages.length,
591
+ };
592
+ await this.safeCallback(callbacks?.onCompaction, compactionInfo);
593
+ yield {
594
+ type: 'compaction',
595
+ data: compactionInfo,
596
+ };
597
+ }
598
+
599
+ this.throwIfAborted(abortSignal);
600
+
601
+ const contextUsage = this.estimateContextUsage(
602
+ messages,
603
+ effectiveTools,
604
+ input.contextLimitTokens
605
+ );
606
+ await this.safeCallback(callbacks?.onContextUsage, {
607
+ stepIndex,
608
+ messageCount: messages.length,
609
+ ...contextUsage,
610
+ });
611
+
612
+ yield* this.emitProgress(input.executionId, stepIndex, 'llm', messages.length);
613
+ const llmSpan = await this.startSpan(
614
+ callbacks,
615
+ traceId,
616
+ 'agent.llm.step',
617
+ runSpan.spanId,
618
+ {
619
+ executionId: input.executionId,
620
+ stepIndex,
621
+ messageCount: messages.length,
622
+ }
623
+ );
624
+ const llmScope = this.createStageAbortScope(abortSignal, timeoutBudget, 'llm');
625
+ let llmResult:
626
+ | {
627
+ assistantMessage: Message;
628
+ toolCalls: ToolCall[];
629
+ }
630
+ | undefined;
631
+ let llmErrorCode: string | undefined;
632
+ let llmSucceeded = false;
633
+ try {
634
+ const llmGen = this.callLLMAndProcessStream(
635
+ messages,
636
+ this.mergeLLMConfig(
637
+ input.config,
638
+ effectiveTools,
639
+ llmScope.signal,
640
+ input.conversationId
641
+ ),
642
+ llmScope.signal,
643
+ input.executionId,
644
+ stepIndex,
645
+ writeBufferSessions
646
+ );
647
+ for (;;) {
648
+ const next = await llmGen.next();
649
+ if (next.done) {
650
+ llmResult = next.value;
651
+ break;
652
+ }
653
+ yield next.value as StreamEvent;
654
+ }
655
+ this.throwIfAborted(llmScope.signal);
656
+ llmSucceeded = true;
657
+ } catch (error) {
658
+ llmErrorCode = this.extractErrorCode(error) || 'AGENT_LLM_STAGE_FAILED';
659
+ throw error;
660
+ } finally {
661
+ llmScope.release();
662
+ const llmLatencyMs = Date.now() - llmSpan.startedAt;
663
+ await this.emitMetric(callbacks, {
664
+ name: 'agent.llm.duration_ms',
665
+ value: llmLatencyMs,
666
+ unit: 'ms',
667
+ timestamp: Date.now(),
668
+ tags: {
669
+ executionId: input.executionId,
670
+ stepIndex,
671
+ success: llmSucceeded ? 'true' : 'false',
672
+ },
673
+ });
674
+ await this.endSpan(callbacks, llmSpan, {
675
+ executionId: input.executionId,
676
+ stepIndex,
677
+ latencyMs: llmLatencyMs,
678
+ errorCode: llmErrorCode,
679
+ });
680
+ this.logInfo('[Agent] llm.step', {
681
+ executionId: input.executionId,
682
+ traceId,
683
+ spanId: llmSpan.spanId,
684
+ stepIndex,
685
+ latencyMs: llmLatencyMs,
686
+ errorCode: llmErrorCode,
687
+ messageCount: messages.length,
688
+ });
689
+ }
690
+
691
+ if (!llmResult) {
692
+ throw new UnknownError('LLM stream completed without result');
693
+ }
694
+ const assistantMessage = llmResult.assistantMessage;
695
+ const toolCalls = llmResult.toolCalls;
696
+
697
+ messages.push(assistantMessage);
698
+ await this.safeCallback(callbacks?.onMessage, assistantMessage);
699
+
700
+ if (toolCalls.length > 0) {
701
+ yield* this.emitProgress(input.executionId, stepIndex, 'tool', messages.length);
702
+ const toolStageSpan = await this.startSpan(
703
+ callbacks,
704
+ traceId,
705
+ 'agent.tool.stage',
706
+ runSpan.spanId,
707
+ {
708
+ executionId: input.executionId,
709
+ stepIndex,
710
+ toolCalls: toolCalls.length,
711
+ }
712
+ );
713
+ const toolScope = this.createStageAbortScope(abortSignal, timeoutBudget, 'tool');
714
+ let toolResultMessage: Message | undefined;
715
+ let toolStageErrorCode: string | undefined;
716
+ let toolStageSucceeded = false;
717
+ try {
718
+ const toolGen = this.processToolCalls(
719
+ toolCalls,
720
+ messages,
721
+ stepIndex,
722
+ callbacks,
723
+ toolScope.signal,
724
+ input.executionId,
725
+ traceId,
726
+ toolStageSpan.spanId,
727
+ writeBufferSessions
728
+ );
729
+ for (;;) {
730
+ const next = await toolGen.next();
731
+ if (next.done) {
732
+ toolResultMessage = next.value;
733
+ break;
734
+ }
735
+ yield next.value as StreamEvent;
736
+ }
737
+ toolStageSucceeded = true;
738
+ } catch (error) {
739
+ toolStageErrorCode = this.extractErrorCode(error) || 'AGENT_TOOL_STAGE_FAILED';
740
+ throw error;
741
+ } finally {
742
+ toolScope.release();
743
+ const toolStageLatencyMs = Date.now() - toolStageSpan.startedAt;
744
+ await this.emitMetric(callbacks, {
745
+ name: 'agent.tool.stage.duration_ms',
746
+ value: toolStageLatencyMs,
747
+ unit: 'ms',
748
+ timestamp: Date.now(),
749
+ tags: {
750
+ executionId: input.executionId,
751
+ stepIndex,
752
+ success: toolStageSucceeded ? 'true' : 'false',
753
+ },
754
+ });
755
+ await this.endSpan(callbacks, toolStageSpan, {
756
+ executionId: input.executionId,
757
+ stepIndex,
758
+ latencyMs: toolStageLatencyMs,
759
+ errorCode: toolStageErrorCode,
760
+ toolCalls: toolCalls.length,
761
+ });
762
+ this.logInfo('[Agent] tool.stage', {
763
+ executionId: input.executionId,
764
+ traceId,
765
+ spanId: toolStageSpan.spanId,
766
+ stepIndex,
767
+ latencyMs: toolStageLatencyMs,
768
+ errorCode: toolStageErrorCode,
769
+ toolCalls: toolCalls.length,
770
+ });
771
+ }
772
+
773
+ const lastMessage = toolResultMessage;
774
+ yield* this.yieldCheckpoint(input.executionId, stepIndex, lastMessage, callbacks);
775
+ continue;
776
+ }
777
+
778
+ retryCount = 0;
779
+ runOutcome = 'done';
780
+ terminalDoneEmitted = true;
781
+ yield* this.yieldDoneEvent(stepIndex, 'stop');
782
+ break;
783
+ } catch (error) {
784
+ const timeoutError = this.normalizeTimeoutBudgetError(error, abortSignal);
785
+ if (timeoutError) {
786
+ runOutcome = 'timeout';
787
+ runErrorCode = timeoutError.errorCode;
788
+ yield* this.yieldErrorEvent(timeoutError);
789
+ break;
790
+ }
791
+
792
+ if (this.isAbortError(error) || inputAbortSignal?.aborted) {
793
+ runOutcome = 'aborted';
794
+ runErrorCode = 'AGENT_ABORTED';
795
+ yield* this.yieldErrorEvent(new AgentAbortedError(ABORTED_MESSAGE));
796
+ break;
797
+ }
798
+
799
+ const normalizedError = this.normalizeError(error);
800
+ this.logError('[Agent] run.error', normalizedError, {
801
+ executionId: input.executionId,
802
+ traceId,
803
+ stepIndex,
804
+ retryCount,
805
+ errorCode: normalizedError.errorCode,
806
+ category: normalizedError.category,
807
+ });
808
+ runOutcome = 'error';
809
+ runErrorCode = normalizedError.errorCode;
810
+ const decision = await this.safeErrorCallback(callbacks?.onError, normalizedError);
811
+ yield* this.yieldErrorEvent(normalizedError);
812
+
813
+ const shouldRetry = decision?.retry ?? normalizedError.retryable;
814
+ if (!shouldRetry) {
815
+ break;
816
+ }
817
+
818
+ retryCount++;
819
+ this.logWarn('[Agent] retry.scheduled', {
820
+ executionId: input.executionId,
821
+ traceId,
822
+ stepIndex,
823
+ retryCount,
824
+ errorCode: normalizedError.errorCode,
825
+ });
826
+ if (retryCount < this.config.maxRetryCount) {
827
+ const retryDelay = this.calculateRetryDelay(retryCount, error as Error);
828
+ try {
829
+ await this.sleep(retryDelay, abortSignal);
830
+ } catch (sleepError) {
831
+ const sleepTimeoutError = this.normalizeTimeoutBudgetError(sleepError, abortSignal);
832
+ if (sleepTimeoutError) {
833
+ runOutcome = 'timeout';
834
+ runErrorCode = sleepTimeoutError.errorCode;
835
+ yield* this.yieldErrorEvent(sleepTimeoutError);
836
+ break;
837
+ }
838
+ if (this.isAbortError(sleepError) || inputAbortSignal?.aborted) {
839
+ runOutcome = 'aborted';
840
+ runErrorCode = 'AGENT_ABORTED';
841
+ yield* this.yieldErrorEvent(new AgentAbortedError(ABORTED_MESSAGE));
842
+ break;
843
+ }
844
+ throw sleepError;
845
+ }
846
+ }
847
+ }
848
+ }
849
+
850
+ if (!terminalDoneEmitted && runOutcome === 'done' && stepIndex >= maxSteps) {
851
+ runOutcome = 'max_steps';
852
+ terminalDoneEmitted = true;
853
+ yield* this.yieldDoneEvent(stepIndex, 'max_steps');
854
+ }
855
+ } finally {
856
+ const runLatencyMs = Date.now() - runSpan.startedAt;
857
+ await this.emitMetric(callbacks, {
858
+ name: 'agent.run.duration_ms',
859
+ value: runLatencyMs,
860
+ unit: 'ms',
861
+ timestamp: Date.now(),
862
+ tags: {
863
+ executionId: input.executionId,
864
+ outcome: runOutcome,
865
+ },
866
+ });
867
+ await this.emitMetric(callbacks, {
868
+ name: 'agent.retry.count',
869
+ value: retryCount,
870
+ unit: 'count',
871
+ timestamp: Date.now(),
872
+ tags: {
873
+ executionId: input.executionId,
874
+ },
875
+ });
876
+ await this.endSpan(callbacks, runSpan, {
877
+ executionId: input.executionId,
878
+ stepIndex,
879
+ latencyMs: runLatencyMs,
880
+ outcome: runOutcome,
881
+ errorCode: runErrorCode,
882
+ retryCount,
883
+ });
884
+ this.logInfo('[Agent] run.finish', {
885
+ executionId: input.executionId,
886
+ traceId,
887
+ spanId: runSpan.spanId,
888
+ stepIndex,
889
+ latencyMs: runLatencyMs,
890
+ outcome: runOutcome,
891
+ errorCode: runErrorCode,
892
+ retryCount,
893
+ });
894
+ executionScope.release();
895
+ }
896
+ }
897
+
898
+ private async safeCallback<T>(
899
+ callback: ((arg: T) => void | Promise<void>) | undefined,
900
+ arg: T
901
+ ): Promise<void> {
902
+ await invokeSafeCallback(callback, arg, (error) =>
903
+ this.logError('[Agent] Callback error:', error)
904
+ );
905
+ }
906
+
907
+ private async safeErrorCallback(
908
+ callback: ((error: Error) => ErrorDecision | void | Promise<ErrorDecision | void>) | undefined,
909
+ error: Error
910
+ ): Promise<ErrorDecision | undefined> {
911
+ return invokeSafeErrorCallback(callback, error, (err) =>
912
+ this.logError('[Agent] Error callback error:', err)
913
+ );
914
+ }
915
+
916
+ private async mergeToolCalls(
917
+ existing: ToolCall[],
918
+ newCalls: ToolCall[],
919
+ messageId: string,
920
+ executionId: string | undefined,
921
+ stepIndex: number,
922
+ writeBufferSessions: Map<string, WriteBufferRuntime>
923
+ ): Promise<ToolCall[]> {
924
+ return mergeToolCallsWithBuffer({
925
+ existing,
926
+ incoming: newCalls,
927
+ messageId,
928
+ onArgumentsChunk: async (toolCall, argumentsChunk, chunkMessageId) => {
929
+ const sessionKey = createWriteFileSessionKey({
930
+ executionId,
931
+ stepIndex,
932
+ toolCallId: toolCall.id,
933
+ });
934
+ await bufferWriteFileChunk({
935
+ toolCall,
936
+ argumentsChunk,
937
+ messageId: chunkMessageId,
938
+ sessionKey,
939
+ sessions: writeBufferSessions,
940
+ onError: (error) =>
941
+ this.logError('[Agent] Failed to buffer write_file tool chunk:', error),
942
+ });
943
+ },
944
+ });
945
+ }
946
+
947
+ private async *callLLMAndProcessStream(
948
+ messages: Message[],
949
+ config: AgentInput['config'],
950
+ abortSignal?: AbortSignal,
951
+ executionId?: string,
952
+ stepIndex = 0,
953
+ writeBufferSessions: Map<string, WriteBufferRuntime> = new Map()
954
+ ): AsyncGenerator<StreamEvent, { assistantMessage: Message; toolCalls: ToolCall[] }, unknown> {
955
+ const requestPlan = this.buildLLMRequestPlan(messages, config);
956
+ const stream = this.llmProvider.generateStream(
957
+ requestPlan.requestMessages,
958
+ requestPlan.requestConfig
959
+ );
960
+
961
+ const assistantMessage: Message = {
962
+ messageId: generateId('msg_'),
963
+ type: 'assistant-text',
964
+ role: 'assistant',
965
+ content: '',
966
+ reasoning_content: '',
967
+ timestamp: Date.now(),
968
+ };
969
+
970
+ let toolCalls: ToolCall[] = [];
971
+ let finished = false;
972
+
973
+ for await (const chunk of stream) {
974
+ this.throwIfAborted(abortSignal);
975
+ const choices = chunk.choices;
976
+ const delta = choices?.[0]?.delta;
977
+
978
+ if (typeof chunk.id === 'string' && chunk.id.trim().length > 0) {
979
+ assistantMessage.metadata = {
980
+ ...assistantMessage.metadata,
981
+ responseId: chunk.id,
982
+ };
983
+ }
984
+
985
+ if (chunk.usage) {
986
+ assistantMessage.usage = chunk.usage;
987
+ }
988
+
989
+ if (finished) {
990
+ continue;
991
+ }
992
+
993
+ if (typeof delta?.content === 'string') {
994
+ assistantMessage.content = `${assistantMessage.content}${delta.content}`;
995
+
996
+ yield {
997
+ type: 'chunk',
998
+ data: {
999
+ messageId: assistantMessage.messageId,
1000
+ content: delta.content,
1001
+ delta: true,
1002
+ },
1003
+ };
1004
+ }
1005
+
1006
+ if (typeof delta?.reasoning_content === 'string') {
1007
+ const currentReasoning = assistantMessage.reasoning_content;
1008
+ assistantMessage.reasoning_content = `${currentReasoning || ''}${delta.reasoning_content}`;
1009
+
1010
+ yield {
1011
+ type: 'reasoning_chunk',
1012
+ data: {
1013
+ messageId: assistantMessage.messageId,
1014
+ reasoningContent: delta.reasoning_content,
1015
+ delta: true,
1016
+ },
1017
+ };
1018
+ }
1019
+
1020
+ if (delta?.tool_calls) {
1021
+ toolCalls = await this.mergeToolCalls(
1022
+ toolCalls,
1023
+ delta.tool_calls,
1024
+ assistantMessage.messageId,
1025
+ executionId,
1026
+ stepIndex,
1027
+ writeBufferSessions
1028
+ );
1029
+
1030
+ yield {
1031
+ type: 'tool_call',
1032
+ data: {
1033
+ messageId: assistantMessage.messageId,
1034
+ toolCalls,
1035
+ },
1036
+ };
1037
+ }
1038
+
1039
+ const finishReason =
1040
+ choices?.[0]?.finish_reason ||
1041
+ (delta as { finish_reason?: string } | undefined)?.finish_reason;
1042
+ if (finishReason) {
1043
+ finished = true;
1044
+ }
1045
+ }
1046
+
1047
+ assistantMessage.tool_calls = toolCalls.length > 0 ? toolCalls : undefined;
1048
+ assistantMessage.type = toolCalls.length > 0 ? 'tool-call' : 'assistant-text';
1049
+ assistantMessage.metadata = {
1050
+ ...assistantMessage.metadata,
1051
+ llmRequestConfigHash: requestPlan.requestConfigHash,
1052
+ llmRequestInputHash: requestPlan.requestInputHash,
1053
+ llmRequestInputMessageCount: requestPlan.requestInputMessageCount,
1054
+ continuationMode: requestPlan.continuationMode,
1055
+ continuationDeltaMessageCount: requestPlan.continuationDeltaMessageCount,
1056
+ ...(requestPlan.previousResponseIdUsed
1057
+ ? {
1058
+ previousResponseIdUsed: requestPlan.previousResponseIdUsed,
1059
+ continuationBaselineMessageCount: requestPlan.continuationBaselineMessageCount,
1060
+ }
1061
+ : {}),
1062
+ };
1063
+ assistantMessage.metadata = {
1064
+ ...assistantMessage.metadata,
1065
+ llmResponseMessageHash: hashValueForContinuation(
1066
+ this.convertMessageToLLMMessage(assistantMessage)
1067
+ ),
1068
+ };
1069
+
1070
+ const hasAssistantText = hasNonEmptyText(assistantMessage.content);
1071
+ const hasReasoningText = hasNonEmptyText(assistantMessage.reasoning_content);
1072
+ if (toolCalls.length === 0 && !hasAssistantText && !hasReasoningText) {
1073
+ throw new AgentUpstreamRetryableError('LLM returned an empty assistant response');
1074
+ }
1075
+
1076
+ return { assistantMessage, toolCalls };
1077
+ }
1078
+
1079
+ private async *executeTool(
1080
+ toolCall: ToolCall,
1081
+ stepIndex: number,
1082
+ callbacks?: AgentCallbacks,
1083
+ abortSignal?: AbortSignal,
1084
+ executionId?: string,
1085
+ traceId?: string,
1086
+ parentSpanId?: string,
1087
+ writeBufferSessions: Map<string, WriteBufferRuntime> = new Map()
1088
+ ): AsyncGenerator<StreamEvent, Message, unknown> {
1089
+ this.throwIfAborted(abortSignal);
1090
+ const effectiveTraceId = traceId || executionId || generateId('trace_');
1091
+ const toolSpan = await this.startSpan(
1092
+ callbacks,
1093
+ effectiveTraceId,
1094
+ 'agent.tool.execute',
1095
+ parentSpanId,
1096
+ {
1097
+ executionId,
1098
+ stepIndex,
1099
+ toolCallId: toolCall.id,
1100
+ toolName: toolCall.function.name,
1101
+ }
1102
+ );
1103
+ let toolErrorCode: string | undefined;
1104
+ let cachedHit = false;
1105
+ let toolSucceeded = false;
1106
+ try {
1107
+ const writeFileSessionKey = createWriteFileSessionKey({
1108
+ executionId,
1109
+ stepIndex,
1110
+ toolCallId: toolCall.id,
1111
+ });
1112
+
1113
+ const ledgerResult = await executeWithToolLedger({
1114
+ ledger: this.toolExecutionLedger,
1115
+ executionId,
1116
+ toolCallId: toolCall.id,
1117
+ execute: async () => {
1118
+ const toolExecResult = await this.toolExecutor.execute(toolCall, {
1119
+ onChunk: (chunk) => {
1120
+ this.emit('tool_chunk', {
1121
+ toolCallId: toolCall.id,
1122
+ toolName: toolCall.function.name,
1123
+ arguments: toolCall.function.arguments,
1124
+ chunk: chunk.data,
1125
+ chunkType: chunk.type,
1126
+ });
1127
+ },
1128
+ onConfirm: async (info) => {
1129
+ return new Promise((resolve) => {
1130
+ let settled = false;
1131
+ const abortHandler = () => {
1132
+ if (!settled) {
1133
+ settled = true;
1134
+ resolve({ approved: false, message: ABORTED_MESSAGE });
1135
+ }
1136
+ };
1137
+
1138
+ const cleanup = () => {
1139
+ if (!settled) {
1140
+ settled = true;
1141
+ }
1142
+ abortSignal?.removeEventListener('abort', abortHandler);
1143
+ };
1144
+
1145
+ if (abortSignal?.aborted) {
1146
+ abortHandler();
1147
+ return;
1148
+ }
1149
+ abortSignal?.addEventListener('abort', abortHandler, { once: true });
1150
+
1151
+ this.emit('tool_confirm', {
1152
+ ...info,
1153
+ resolve: (decision: ToolDecision) => {
1154
+ cleanup();
1155
+ resolve(decision);
1156
+ },
1157
+ });
1158
+ });
1159
+ },
1160
+ onPolicyCheck: callbacks?.onToolPolicy
1161
+ ? async (info) => {
1162
+ const decision = await callbacks.onToolPolicy?.(info);
1163
+ return decision || { allowed: true };
1164
+ }
1165
+ : undefined,
1166
+ toolCallId: toolCall.id,
1167
+ loopIndex: stepIndex,
1168
+ agent: this,
1169
+ toolAbortSignal: abortSignal,
1170
+ });
1171
+
1172
+ let toolOutput = '';
1173
+ let toolSummary = '';
1174
+ let errorCode: string | undefined;
1175
+
1176
+ if (toolExecResult.success) {
1177
+ toolOutput = toolExecResult.output || '';
1178
+ await cleanupWriteFileBufferSession(toolCall, writeBufferSessions, writeFileSessionKey);
1179
+ } else {
1180
+ if (isWriteFileTool(toolCall)) {
1181
+ if (isWriteFileProtocolResponse(toolExecResult.output)) {
1182
+ toolOutput = toolExecResult.output;
1183
+ } else if (needEnrichWriteFileFailure(toolExecResult.error, toolExecResult.output)) {
1184
+ const errorContent =
1185
+ toolExecResult.error?.message ||
1186
+ toolExecResult.output ||
1187
+ new UnknownError().message;
1188
+ toolOutput = await buildWriteFileToolErrorPayload(
1189
+ toolCall,
1190
+ errorContent,
1191
+ writeBufferSessions,
1192
+ writeFileSessionKey
1193
+ );
1194
+ } else {
1195
+ toolOutput =
1196
+ toolExecResult.error?.message ||
1197
+ toolExecResult.output ||
1198
+ new UnknownError().message;
1199
+ }
1200
+ } else {
1201
+ toolOutput =
1202
+ toolExecResult.error?.message ||
1203
+ toolExecResult.output ||
1204
+ new UnknownError().message;
1205
+ }
1206
+ errorCode = this.extractErrorCode(toolExecResult.error) || 'TOOL_EXECUTION_FAILED';
1207
+ }
1208
+
1209
+ toolSummary = this.resolveToolResultSummary(toolCall, toolExecResult, toolOutput);
1210
+
1211
+ return {
1212
+ success: toolExecResult.success,
1213
+ output: toolOutput,
1214
+ summary: toolSummary,
1215
+ payload: toolExecResult.payload,
1216
+ metadata: toolExecResult.metadata,
1217
+ errorName: toolExecResult.error?.name,
1218
+ errorMessage: toolExecResult.error?.message,
1219
+ errorCode,
1220
+ recordedAt: Date.now(),
1221
+ };
1222
+ },
1223
+ onError: (error) => {
1224
+ this.logError('[Agent] Failed to execute tool with ledger:', error, {
1225
+ executionId,
1226
+ stepIndex,
1227
+ toolCallId: toolCall.id,
1228
+ });
1229
+ },
1230
+ });
1231
+
1232
+ cachedHit = ledgerResult.fromCache;
1233
+ toolSucceeded = ledgerResult.record.success;
1234
+ toolErrorCode = ledgerResult.record.errorCode;
1235
+
1236
+ const replayResult = this.createToolResultMessageFromLedger(toolCall.id, ledgerResult.record);
1237
+ await this.safeCallback(callbacks?.onMessage, replayResult);
1238
+
1239
+ yield {
1240
+ type: 'tool_result',
1241
+ data: replayResult,
1242
+ };
1243
+
1244
+ return replayResult;
1245
+ } catch (error) {
1246
+ toolErrorCode = this.extractErrorCode(error) || 'TOOL_EXECUTION_FAILED';
1247
+ throw error;
1248
+ } finally {
1249
+ const toolLatencyMs = Date.now() - toolSpan.startedAt;
1250
+ await this.emitMetric(callbacks, {
1251
+ name: 'agent.tool.duration_ms',
1252
+ value: toolLatencyMs,
1253
+ unit: 'ms',
1254
+ timestamp: Date.now(),
1255
+ tags: {
1256
+ executionId: executionId || '',
1257
+ stepIndex,
1258
+ toolCallId: toolCall.id,
1259
+ cached: cachedHit ? 'true' : 'false',
1260
+ success: toolSucceeded ? 'true' : 'false',
1261
+ },
1262
+ });
1263
+ await this.endSpan(callbacks, toolSpan, {
1264
+ executionId,
1265
+ stepIndex,
1266
+ toolCallId: toolCall.id,
1267
+ toolName: toolCall.function.name,
1268
+ latencyMs: toolLatencyMs,
1269
+ cached: cachedHit,
1270
+ errorCode: toolErrorCode,
1271
+ });
1272
+ this.logInfo('[Agent] tool.execute', {
1273
+ executionId,
1274
+ traceId: effectiveTraceId,
1275
+ spanId: toolSpan.spanId,
1276
+ parentSpanId: toolSpan.parentSpanId,
1277
+ stepIndex,
1278
+ toolCallId: toolCall.id,
1279
+ toolName: toolCall.function.name,
1280
+ latencyMs: toolLatencyMs,
1281
+ cached: cachedHit,
1282
+ errorCode: toolErrorCode,
1283
+ });
1284
+ }
1285
+ }
1286
+
1287
+ private async *processToolCalls(
1288
+ toolCalls: ToolCall[],
1289
+ messages: Message[],
1290
+ stepIndex: number,
1291
+ callbacks?: AgentCallbacks,
1292
+ abortSignal?: AbortSignal,
1293
+ executionId?: string,
1294
+ traceId?: string,
1295
+ parentSpanId?: string,
1296
+ writeBufferSessions: Map<string, WriteBufferRuntime> = new Map()
1297
+ ): AsyncGenerator<StreamEvent, Message, unknown> {
1298
+ if (this.config.maxConcurrentToolCalls <= 1 || toolCalls.length <= 1) {
1299
+ for (const toolCall of toolCalls) {
1300
+ this.throwIfAborted(abortSignal);
1301
+ yield* this.emitProgress(executionId, stepIndex, 'tool', messages.length);
1302
+
1303
+ const toolGen = this.executeTool(
1304
+ toolCall,
1305
+ stepIndex,
1306
+ callbacks,
1307
+ abortSignal,
1308
+ executionId,
1309
+ traceId,
1310
+ parentSpanId,
1311
+ writeBufferSessions
1312
+ );
1313
+ let resultMessage: Message | undefined;
1314
+ for (;;) {
1315
+ const next = await toolGen.next();
1316
+ if (next.done) {
1317
+ resultMessage = next.value;
1318
+ break;
1319
+ }
1320
+ yield next.value as StreamEvent;
1321
+ }
1322
+
1323
+ if (resultMessage) {
1324
+ messages.push(resultMessage);
1325
+ }
1326
+ }
1327
+
1328
+ const lastMessage = messages[messages.length - 1];
1329
+ return lastMessage;
1330
+ }
1331
+
1332
+ const plans = toolCalls.map((toolCall) => ({
1333
+ toolCall,
1334
+ policy: this.resolveToolConcurrencyPolicy(toolCall),
1335
+ }));
1336
+
1337
+ for (let i = 0; i < plans.length; i++) {
1338
+ this.throwIfAborted(abortSignal);
1339
+ yield* this.emitProgress(executionId, stepIndex, 'tool', messages.length);
1340
+ }
1341
+
1342
+ const waves = this.buildExecutionWaves(plans);
1343
+ const allResults: Array<{ events: StreamEvent[]; message?: Message }> = [];
1344
+ for (const wave of waves) {
1345
+ this.throwIfAborted(abortSignal);
1346
+ if (wave.type === 'exclusive') {
1347
+ allResults.push(
1348
+ await this.executeToolTask(
1349
+ wave.plans[0].toolCall,
1350
+ stepIndex,
1351
+ callbacks,
1352
+ abortSignal,
1353
+ executionId,
1354
+ traceId,
1355
+ parentSpanId,
1356
+ writeBufferSessions
1357
+ )
1358
+ );
1359
+ continue;
1360
+ }
1361
+
1362
+ const parallelResults = await this.runParallelWave(
1363
+ wave.plans,
1364
+ stepIndex,
1365
+ callbacks,
1366
+ abortSignal,
1367
+ executionId,
1368
+ traceId,
1369
+ parentSpanId,
1370
+ writeBufferSessions
1371
+ );
1372
+ allResults.push(...parallelResults);
1373
+ }
1374
+
1375
+ for (const taskResult of allResults) {
1376
+ for (const event of taskResult.events) {
1377
+ yield event;
1378
+ }
1379
+ if (taskResult.message) {
1380
+ messages.push(taskResult.message);
1381
+ }
1382
+ this.throwIfAborted(abortSignal);
1383
+ }
1384
+
1385
+ const lastMessage = messages[messages.length - 1];
1386
+ return lastMessage;
1387
+ }
1388
+
1389
+ private resolveToolConcurrencyPolicy(toolCall: ToolCall): ToolConcurrencyPolicy {
1390
+ if (this.config.toolConcurrencyPolicyResolver) {
1391
+ return this.config.toolConcurrencyPolicyResolver(toolCall);
1392
+ }
1393
+
1394
+ const manager = this.toolExecutor as ToolManager & {
1395
+ getConcurrencyPolicy?: (call: ToolCall) => ToolConcurrencyPolicy;
1396
+ };
1397
+ if (typeof manager.getConcurrencyPolicy === 'function') {
1398
+ return manager.getConcurrencyPolicy(toolCall);
1399
+ }
1400
+
1401
+ return { mode: 'exclusive' };
1402
+ }
1403
+
1404
+ private buildExecutionWaves(
1405
+ plans: Array<{ toolCall: ToolCall; policy: ToolConcurrencyPolicy }>
1406
+ ): Array<{
1407
+ type: 'exclusive' | 'parallel';
1408
+ plans: Array<{ toolCall: ToolCall; policy: ToolConcurrencyPolicy }>;
1409
+ }> {
1410
+ return buildToolExecutionWaves(plans);
1411
+ }
1412
+
1413
+ private async runParallelWave(
1414
+ plans: Array<{ toolCall: ToolCall; policy: ToolConcurrencyPolicy }>,
1415
+ stepIndex: number,
1416
+ callbacks?: AgentCallbacks,
1417
+ abortSignal?: AbortSignal,
1418
+ executionId?: string,
1419
+ traceId?: string,
1420
+ parentSpanId?: string,
1421
+ writeBufferSessions: Map<string, WriteBufferRuntime> = new Map()
1422
+ ): Promise<Array<{ events: StreamEvent[]; message?: Message }>> {
1423
+ const tasks = plans.map((plan) => ({
1424
+ lockKey: plan.policy.lockKey,
1425
+ run: async () =>
1426
+ this.executeToolTask(
1427
+ plan.toolCall,
1428
+ stepIndex,
1429
+ callbacks,
1430
+ abortSignal,
1431
+ executionId,
1432
+ traceId,
1433
+ parentSpanId,
1434
+ writeBufferSessions
1435
+ ),
1436
+ }));
1437
+ return this.runWithConcurrencyAndLock(tasks, this.config.maxConcurrentToolCalls);
1438
+ }
1439
+
1440
+ private async executeToolTask(
1441
+ toolCall: ToolCall,
1442
+ stepIndex: number,
1443
+ callbacks?: AgentCallbacks,
1444
+ abortSignal?: AbortSignal,
1445
+ executionId?: string,
1446
+ traceId?: string,
1447
+ parentSpanId?: string,
1448
+ writeBufferSessions: Map<string, WriteBufferRuntime> = new Map()
1449
+ ): Promise<{ events: StreamEvent[]; message?: Message }> {
1450
+ const events: StreamEvent[] = [];
1451
+ const toolGen = this.executeTool(
1452
+ toolCall,
1453
+ stepIndex,
1454
+ callbacks,
1455
+ abortSignal,
1456
+ executionId,
1457
+ traceId,
1458
+ parentSpanId,
1459
+ writeBufferSessions
1460
+ );
1461
+ let resultMessage: Message | undefined;
1462
+ for (;;) {
1463
+ const next = await toolGen.next();
1464
+ if (next.done) {
1465
+ resultMessage = next.value;
1466
+ break;
1467
+ }
1468
+ events.push(next.value as StreamEvent);
1469
+ }
1470
+ return {
1471
+ events,
1472
+ message: resultMessage,
1473
+ };
1474
+ }
1475
+
1476
+ private async runWithConcurrencyAndLock<T>(
1477
+ tasks: Array<{ lockKey?: string; run: () => Promise<T> }>,
1478
+ limit: number
1479
+ ): Promise<T[]> {
1480
+ return runTasksWithConcurrencyAndLock(tasks, limit);
1481
+ }
1482
+
1483
+ private async *yieldCheckpoint(
1484
+ executionId: string | undefined,
1485
+ stepIndex: number,
1486
+ lastMessage: Message | undefined,
1487
+ callbacks?: AgentCallbacks
1488
+ ): AsyncGenerator<StreamEvent, void, unknown> {
1489
+ const checkpoint = createCheckpoint(executionId, stepIndex, lastMessage?.messageId);
1490
+ await this.safeCallback(callbacks?.onCheckpoint, checkpoint);
1491
+
1492
+ yield {
1493
+ type: 'checkpoint',
1494
+ data: checkpoint,
1495
+ };
1496
+ }
1497
+
1498
+ private *yieldMaxRetriesError(): Generator<StreamEvent> {
1499
+ yield* this.yieldErrorEvent(new MaxRetriesError());
1500
+ }
1501
+
1502
+ private *emitProgress(
1503
+ executionId: string | undefined,
1504
+ stepIndex: number,
1505
+ currentAction: 'llm' | 'tool',
1506
+ messageCount: number
1507
+ ): Generator<StreamEvent> {
1508
+ yield createProgressEvent(executionId, stepIndex, currentAction, messageCount);
1509
+ }
1510
+
1511
+ private *yieldErrorEvent(error: AgentError): Generator<StreamEvent> {
1512
+ yield createErrorEvent(error);
1513
+ }
1514
+
1515
+ private *yieldDoneEvent(
1516
+ stepIndex: number,
1517
+ finishReason: 'stop' | 'max_steps' = 'stop'
1518
+ ): Generator<StreamEvent> {
1519
+ yield createDoneEvent(stepIndex, finishReason);
1520
+ }
1521
+
1522
+ private mergeLLMConfig(
1523
+ config: AgentInput['config'],
1524
+ tools?: AgentInput['tools'],
1525
+ abortSignal?: AbortSignal,
1526
+ conversationId?: string
1527
+ ): AgentInput['config'] {
1528
+ const merged = mergeLLMRequestConfig(config, tools, abortSignal);
1529
+ if (
1530
+ typeof conversationId !== 'string' ||
1531
+ conversationId.trim().length === 0 ||
1532
+ merged?.prompt_cache_key
1533
+ ) {
1534
+ return merged;
1535
+ }
1536
+
1537
+ // Use the conversation id as the default sticky cache routing key so
1538
+ // repeated full replays can still hit provider-side prefix caching.
1539
+ return {
1540
+ ...(merged || {}),
1541
+ prompt_cache_key: conversationId,
1542
+ };
1543
+ }
1544
+
1545
+ private resolveLLMTools(inputTools?: Tool[]): Tool[] | undefined {
1546
+ if (typeof inputTools !== 'undefined') {
1547
+ return inputTools;
1548
+ }
1549
+
1550
+ const manager = this.toolExecutor as ToolManager & {
1551
+ getTools?: () => Array<{ toToolSchema?: () => unknown }>;
1552
+ };
1553
+ if (typeof manager.getTools !== 'function') {
1554
+ return undefined;
1555
+ }
1556
+
1557
+ const schemas: Tool[] = [];
1558
+ for (const tool of manager.getTools()) {
1559
+ if (typeof tool.toToolSchema !== 'function') {
1560
+ continue;
1561
+ }
1562
+ const schema = tool.toToolSchema();
1563
+ schemas.push({
1564
+ type: schema.type,
1565
+ function: {
1566
+ name: schema.function.name,
1567
+ description: schema.function.description,
1568
+ parameters: (schema.function.parameters as Record<string, unknown> | undefined) || {},
1569
+ },
1570
+ });
1571
+ }
1572
+
1573
+ return schemas.length > 0 ? schemas : undefined;
1574
+ }
1575
+
1576
+ private async emitMetric(
1577
+ callbacks: AgentCallbacks | undefined,
1578
+ metric: Parameters<typeof pushMetric>[1]
1579
+ ): Promise<void> {
1580
+ await pushMetric(callbacks, metric, this.safeCallback.bind(this));
1581
+ }
1582
+
1583
+ private async emitTrace(
1584
+ callbacks: AgentCallbacks | undefined,
1585
+ event: Parameters<typeof pushTrace>[1]
1586
+ ): Promise<void> {
1587
+ await pushTrace(callbacks, event, this.safeCallback.bind(this));
1588
+ }
1589
+
1590
+ private async startSpan(
1591
+ callbacks: AgentCallbacks | undefined,
1592
+ traceId: string,
1593
+ name: string,
1594
+ parentSpanId?: string,
1595
+ attributes?: Record<string, unknown>
1596
+ ): Promise<SpanRuntime> {
1597
+ return beginSpan({
1598
+ callbacks,
1599
+ traceId,
1600
+ name,
1601
+ parentSpanId,
1602
+ attributes,
1603
+ createSpanId: () => generateId('span_'),
1604
+ emitTrace: async (cbs, event) => {
1605
+ await this.emitTrace(cbs, event);
1606
+ },
1607
+ });
1608
+ }
1609
+
1610
+ private async endSpan(
1611
+ callbacks: AgentCallbacks | undefined,
1612
+ span: SpanRuntime,
1613
+ attributes?: Record<string, unknown>
1614
+ ): Promise<void> {
1615
+ await finishSpan({
1616
+ callbacks,
1617
+ span,
1618
+ attributes,
1619
+ emitTrace: async (cbs, event) => {
1620
+ await this.emitTrace(cbs, event);
1621
+ },
1622
+ });
1623
+ }
1624
+
1625
+ private extractErrorCode(error: unknown): string | undefined {
1626
+ return parseErrorCode(error);
1627
+ }
1628
+
1629
+ private createTimeoutBudgetState(input: AgentInput): TimeoutBudgetState | undefined {
1630
+ return createBudgetState({
1631
+ inputTimeoutBudgetMs: input.timeoutBudgetMs,
1632
+ configTimeoutBudgetMs: this.config.timeoutBudgetMs,
1633
+ inputLlmTimeoutRatio: input.llmTimeoutRatio,
1634
+ configLlmTimeoutRatio: this.config.llmTimeoutRatio,
1635
+ });
1636
+ }
1637
+
1638
+ private createExecutionAbortScope(
1639
+ inputAbortSignal: AbortSignal | undefined,
1640
+ timeoutBudget: TimeoutBudgetState | undefined
1641
+ ): AbortScope {
1642
+ return createExecutionBudgetScope(inputAbortSignal, timeoutBudget);
1643
+ }
1644
+
1645
+ private createStageAbortScope(
1646
+ baseSignal: AbortSignal | undefined,
1647
+ timeoutBudget: TimeoutBudgetState | undefined,
1648
+ stage: TimeoutStage
1649
+ ): AbortScope {
1650
+ return createStageBudgetScope(baseSignal, timeoutBudget, stage);
1651
+ }
1652
+
1653
+ private timeoutBudgetErrorFromSignal(
1654
+ signal: AbortSignal | undefined
1655
+ ): TimeoutBudgetExceededError | undefined {
1656
+ return timeoutErrorFromAbortSignal(signal);
1657
+ }
1658
+
1659
+ private normalizeTimeoutBudgetError(
1660
+ error: unknown,
1661
+ signal: AbortSignal | undefined
1662
+ ): TimeoutBudgetExceededError | undefined {
1663
+ return normalizeAbortTimeoutBudgetError(error, signal);
1664
+ }
1665
+
1666
+ private throwIfAborted(signal?: AbortSignal): void {
1667
+ assertNotAborted(signal, ABORTED_MESSAGE);
1668
+ }
1669
+
1670
+ private isAbortError(error: unknown): boolean {
1671
+ return isAbortErrorByMessage(error, ABORTED_MESSAGE);
1672
+ }
1673
+
1674
+ private normalizeError(error: unknown): AgentError {
1675
+ return normalizeAgentError(error, ABORTED_MESSAGE);
1676
+ }
1677
+
1678
+ private calculateRetryDelay(retryCount: number, error: Error): number {
1679
+ return calculateRetryDelayWithBackoff(retryCount, error, this.config.backoffConfig);
1680
+ }
1681
+
1682
+ private async sleep(ms: number, signal?: AbortSignal): Promise<void> {
1683
+ await sleepWithAbort(ms, signal, ABORTED_MESSAGE);
1684
+ }
1685
+
1686
+ private logError(message: string, error: unknown, context?: Record<string, unknown>): void {
1687
+ writeErrorLog(this.logger, message, error, context);
1688
+ }
1689
+
1690
+ private logInfo(message: string, context?: Record<string, unknown>, data?: unknown): void {
1691
+ writeInfoLog(this.logger, message, context, data);
1692
+ }
1693
+
1694
+ private logWarn(message: string, context?: Record<string, unknown>, data?: unknown): void {
1695
+ writeWarnLog(this.logger, message, context, data);
1696
+ }
1697
+
1698
+ private resolveToolResultSummary(
1699
+ toolCall: ToolCall,
1700
+ toolResult: ToolResult,
1701
+ toolOutput: string
1702
+ ): string {
1703
+ if (hasNonEmptyText(toolResult.summary)) {
1704
+ return toolResult.summary;
1705
+ }
1706
+
1707
+ const toolName = toolCall.function.name;
1708
+ const subject = toolName === 'bash' ? 'Command' : toolName;
1709
+
1710
+ if (toolResult.success) {
1711
+ if (hasNonEmptyText(toolOutput)) {
1712
+ return `${subject} completed successfully.`;
1713
+ }
1714
+ return `${subject} completed successfully with no output.`;
1715
+ }
1716
+
1717
+ const errorMessage =
1718
+ toolResult.error?.message || (hasNonEmptyText(toolOutput) ? toolOutput : undefined);
1719
+ if (errorMessage) {
1720
+ return `${subject} failed: ${errorMessage}`;
1721
+ }
1722
+ return `${subject} failed.`;
1723
+ }
1724
+
1725
+ private buildToolResultMetadata(
1726
+ record: ToolExecutionLedgerRecord
1727
+ ): Record<string, unknown> | undefined {
1728
+ const error: Record<string, unknown> = {};
1729
+ if (record.errorName) {
1730
+ error.name = record.errorName;
1731
+ }
1732
+ if (record.errorMessage) {
1733
+ error.message = record.errorMessage;
1734
+ }
1735
+ if (record.errorCode) {
1736
+ error.code = record.errorCode;
1737
+ }
1738
+
1739
+ const toolResult: Record<string, unknown> = {
1740
+ success: record.success,
1741
+ summary: record.summary,
1742
+ };
1743
+ if (hasNonEmptyText(record.output)) {
1744
+ toolResult.output = record.output;
1745
+ }
1746
+ if (record.payload !== undefined) {
1747
+ toolResult.payload = record.payload;
1748
+ }
1749
+ if (record.metadata && Object.keys(record.metadata).length > 0) {
1750
+ toolResult.metadata = record.metadata;
1751
+ }
1752
+ if (Object.keys(error).length > 0) {
1753
+ toolResult.error = error;
1754
+ }
1755
+
1756
+ return {
1757
+ toolResult,
1758
+ };
1759
+ }
1760
+
1761
+ private createToolResultMessageFromLedger(
1762
+ toolCallId: string,
1763
+ record: ToolExecutionLedgerRecord
1764
+ ): Message {
1765
+ return buildToolResultMessage({
1766
+ toolCallId,
1767
+ content: hasNonEmptyText(record.output) ? record.output : record.summary,
1768
+ metadata: this.buildToolResultMetadata(record),
1769
+ createMessageId: () => generateId('msg_'),
1770
+ });
1771
+ }
1772
+ }