rird 2.1.231 → 2.3.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 (381) hide show
  1. package/AGENTS.md +86 -0
  2. package/COMPLETED_TEST_SUITE.txt +280 -0
  3. package/Dockerfile +18 -0
  4. package/README.md +397 -6
  5. package/RIRD_ERROR_HANDLING_SUMMARY.md +307 -0
  6. package/TESTING.md +512 -0
  7. package/TEST_IMPLEMENTATION_REPORT.md +463 -0
  8. package/TEST_SUITE.md +307 -0
  9. package/TEST_SUMMARY.txt +380 -0
  10. package/bin/rird-perf.js +37 -0
  11. package/bin/rird.js +43 -8
  12. package/bunfig.toml +4 -0
  13. package/create-wrapper.ps1 +51 -0
  14. package/docs/ARCHITECTURE.md +768 -0
  15. package/docs/CLI_REFERENCE.md +681 -0
  16. package/docs/DOCUMENTATION_MANIFEST.md +392 -0
  17. package/docs/INDEX.md +295 -0
  18. package/docs/PRODUCTION_SETUP.md +633 -0
  19. package/docs/TROUBLESHOOTING.md +914 -0
  20. package/facebook_ads_library.png +0 -0
  21. package/nul +0 -0
  22. package/nul`nif +0 -0
  23. package/package.json +104 -15
  24. package/parsers-config.ts +239 -0
  25. package/rird-1.0.199.tgz +0 -0
  26. package/rird-1.0.205.tgz +0 -0
  27. package/script/build-windows.ts +56 -0
  28. package/script/build.ts +165 -0
  29. package/{postinstall.mjs → script/postinstall.mjs} +47 -68
  30. package/script/publish-registries.ts +187 -0
  31. package/script/publish.ts +85 -0
  32. package/script/schema.ts +47 -0
  33. package/src/acp/README.md +164 -0
  34. package/src/acp/agent.ts +1063 -0
  35. package/src/acp/session.ts +101 -0
  36. package/src/acp/types.ts +22 -0
  37. package/src/agent/agent.ts +367 -0
  38. package/src/agent/generate.txt +75 -0
  39. package/src/agent/prompt/compaction.txt +12 -0
  40. package/src/agent/prompt/explore.txt +18 -0
  41. package/src/agent/prompt/summary.txt +10 -0
  42. package/src/agent/prompt/title.txt +36 -0
  43. package/src/auth/index.ts +70 -0
  44. package/src/bun/index.ts +114 -0
  45. package/src/bus/bus-event.ts +43 -0
  46. package/src/bus/global.ts +10 -0
  47. package/src/bus/index.ts +105 -0
  48. package/src/cli/bootstrap.ts +17 -0
  49. package/src/cli/cmd/acp.ts +104 -0
  50. package/src/cli/cmd/activate.ts +50 -0
  51. package/src/cli/cmd/agent.ts +256 -0
  52. package/src/cli/cmd/auth.ts +412 -0
  53. package/src/cli/cmd/cmd.ts +7 -0
  54. package/src/cli/cmd/debug/config.ts +15 -0
  55. package/src/cli/cmd/debug/file.ts +91 -0
  56. package/src/cli/cmd/debug/index.ts +43 -0
  57. package/src/cli/cmd/debug/lsp.ts +48 -0
  58. package/src/cli/cmd/debug/ripgrep.ts +83 -0
  59. package/src/cli/cmd/debug/scrap.ts +15 -0
  60. package/src/cli/cmd/debug/skill.ts +15 -0
  61. package/src/cli/cmd/debug/snapshot.ts +48 -0
  62. package/src/cli/cmd/export.ts +88 -0
  63. package/src/cli/cmd/generate.ts +38 -0
  64. package/src/cli/cmd/github.ts +1400 -0
  65. package/src/cli/cmd/import.ts +98 -0
  66. package/src/cli/cmd/mcp.ts +654 -0
  67. package/src/cli/cmd/models.ts +68 -0
  68. package/src/cli/cmd/pr.ts +112 -0
  69. package/src/cli/cmd/run.ts +434 -0
  70. package/src/cli/cmd/serve.ts +31 -0
  71. package/src/cli/cmd/session.ts +106 -0
  72. package/src/cli/cmd/stats.ts +298 -0
  73. package/src/cli/cmd/tui/app.tsx +694 -0
  74. package/src/cli/cmd/tui/attach.ts +30 -0
  75. package/src/cli/cmd/tui/component/border.tsx +21 -0
  76. package/src/cli/cmd/tui/component/dialog-agent.tsx +31 -0
  77. package/src/cli/cmd/tui/component/dialog-command.tsx +124 -0
  78. package/src/cli/cmd/tui/component/dialog-mcp.tsx +86 -0
  79. package/src/cli/cmd/tui/component/dialog-model.tsx +236 -0
  80. package/src/cli/cmd/tui/component/dialog-provider.tsx +240 -0
  81. package/src/cli/cmd/tui/component/dialog-session-list.tsx +102 -0
  82. package/src/cli/cmd/tui/component/dialog-session-rename.tsx +31 -0
  83. package/src/cli/cmd/tui/component/dialog-stash.tsx +86 -0
  84. package/src/cli/cmd/tui/component/dialog-status.tsx +162 -0
  85. package/src/cli/cmd/tui/component/dialog-tag.tsx +44 -0
  86. package/src/cli/cmd/tui/component/dialog-theme-list.tsx +50 -0
  87. package/src/cli/cmd/tui/component/did-you-know.tsx +85 -0
  88. package/src/cli/cmd/tui/component/logo.tsx +48 -0
  89. package/src/cli/cmd/tui/component/prompt/autocomplete.tsx +574 -0
  90. package/src/cli/cmd/tui/component/prompt/history.tsx +108 -0
  91. package/src/cli/cmd/tui/component/prompt/index.tsx +1087 -0
  92. package/src/cli/cmd/tui/component/prompt/stash.tsx +101 -0
  93. package/src/cli/cmd/tui/component/tips.ts +27 -0
  94. package/src/cli/cmd/tui/component/todo-item.tsx +32 -0
  95. package/src/cli/cmd/tui/context/args.tsx +14 -0
  96. package/src/cli/cmd/tui/context/directory.ts +13 -0
  97. package/src/cli/cmd/tui/context/exit.tsx +23 -0
  98. package/src/cli/cmd/tui/context/helper.tsx +25 -0
  99. package/src/cli/cmd/tui/context/keybind.tsx +101 -0
  100. package/src/cli/cmd/tui/context/kv.tsx +49 -0
  101. package/src/cli/cmd/tui/context/local.tsx +345 -0
  102. package/src/cli/cmd/tui/context/prompt.tsx +18 -0
  103. package/src/cli/cmd/tui/context/route.tsx +46 -0
  104. package/src/cli/cmd/tui/context/sdk.tsx +74 -0
  105. package/src/cli/cmd/tui/context/sync.tsx +372 -0
  106. package/src/cli/cmd/tui/context/theme/aura.json +69 -0
  107. package/src/cli/cmd/tui/context/theme/ayu.json +80 -0
  108. package/src/cli/cmd/tui/context/theme/catppuccin-frappe.json +233 -0
  109. package/src/cli/cmd/tui/context/theme/catppuccin-macchiato.json +233 -0
  110. package/src/cli/cmd/tui/context/theme/catppuccin.json +112 -0
  111. package/src/cli/cmd/tui/context/theme/cobalt2.json +228 -0
  112. package/src/cli/cmd/tui/context/theme/cursor.json +249 -0
  113. package/src/cli/cmd/tui/context/theme/dracula.json +219 -0
  114. package/src/cli/cmd/tui/context/theme/everforest.json +241 -0
  115. package/src/cli/cmd/tui/context/theme/flexoki.json +237 -0
  116. package/src/cli/cmd/tui/context/theme/github.json +233 -0
  117. package/src/cli/cmd/tui/context/theme/gruvbox.json +95 -0
  118. package/src/cli/cmd/tui/context/theme/kanagawa.json +77 -0
  119. package/src/cli/cmd/tui/context/theme/lucent-orng.json +227 -0
  120. package/src/cli/cmd/tui/context/theme/material.json +235 -0
  121. package/src/cli/cmd/tui/context/theme/matrix.json +77 -0
  122. package/src/cli/cmd/tui/context/theme/mercury.json +252 -0
  123. package/src/cli/cmd/tui/context/theme/monokai.json +221 -0
  124. package/src/cli/cmd/tui/context/theme/nightowl.json +221 -0
  125. package/src/cli/cmd/tui/context/theme/nord.json +223 -0
  126. package/src/cli/cmd/tui/context/theme/one-dark.json +84 -0
  127. package/src/cli/cmd/tui/context/theme/orng.json +245 -0
  128. package/src/cli/cmd/tui/context/theme/palenight.json +222 -0
  129. package/src/cli/cmd/tui/context/theme/rird.json +245 -0
  130. package/src/cli/cmd/tui/context/theme/rosepine.json +234 -0
  131. package/src/cli/cmd/tui/context/theme/solarized.json +223 -0
  132. package/src/cli/cmd/tui/context/theme/synthwave84.json +226 -0
  133. package/src/cli/cmd/tui/context/theme/tokyonight.json +243 -0
  134. package/src/cli/cmd/tui/context/theme/vercel.json +245 -0
  135. package/src/cli/cmd/tui/context/theme/vesper.json +218 -0
  136. package/src/cli/cmd/tui/context/theme/zenburn.json +223 -0
  137. package/src/cli/cmd/tui/context/theme.tsx +1109 -0
  138. package/src/cli/cmd/tui/event.ts +40 -0
  139. package/src/cli/cmd/tui/hooks/use-safe-terminal-dimensions.ts +12 -0
  140. package/src/cli/cmd/tui/routes/home.tsx +138 -0
  141. package/src/cli/cmd/tui/routes/session/dialog-fork-from-timeline.tsx +64 -0
  142. package/src/cli/cmd/tui/routes/session/dialog-message.tsx +109 -0
  143. package/src/cli/cmd/tui/routes/session/dialog-subagent.tsx +26 -0
  144. package/src/cli/cmd/tui/routes/session/dialog-timeline.tsx +47 -0
  145. package/src/cli/cmd/tui/routes/session/footer.tsx +88 -0
  146. package/src/cli/cmd/tui/routes/session/header.tsx +125 -0
  147. package/src/cli/cmd/tui/routes/session/index.tsx +1876 -0
  148. package/src/cli/cmd/tui/routes/session/sidebar.tsx +320 -0
  149. package/src/cli/cmd/tui/spawn.ts +60 -0
  150. package/src/cli/cmd/tui/thread.ts +142 -0
  151. package/src/cli/cmd/tui/ui/dialog-alert.tsx +57 -0
  152. package/src/cli/cmd/tui/ui/dialog-confirm.tsx +83 -0
  153. package/src/cli/cmd/tui/ui/dialog-help.tsx +38 -0
  154. package/src/cli/cmd/tui/ui/dialog-prompt.tsx +77 -0
  155. package/src/cli/cmd/tui/ui/dialog-select.tsx +333 -0
  156. package/src/cli/cmd/tui/ui/dialog.tsx +171 -0
  157. package/src/cli/cmd/tui/ui/spinner.ts +368 -0
  158. package/src/cli/cmd/tui/ui/toast.tsx +100 -0
  159. package/src/cli/cmd/tui/util/clipboard.ts +127 -0
  160. package/src/cli/cmd/tui/util/editor.ts +32 -0
  161. package/src/cli/cmd/tui/util/terminal.ts +146 -0
  162. package/src/cli/cmd/tui/worker.ts +63 -0
  163. package/src/cli/cmd/uninstall.ts +344 -0
  164. package/src/cli/cmd/upgrade.ts +127 -0
  165. package/src/cli/cmd/web.ts +84 -0
  166. package/src/cli/error.ts +69 -0
  167. package/src/cli/ui.ts +101 -0
  168. package/src/cli/upgrade.ts +28 -0
  169. package/src/command/index.ts +80 -0
  170. package/src/command/template/initialize.txt +10 -0
  171. package/src/command/template/review.txt +97 -0
  172. package/src/config/config.ts +994 -0
  173. package/src/config/markdown.ts +41 -0
  174. package/src/env/index.ts +26 -0
  175. package/src/file/ignore.ts +83 -0
  176. package/src/file/index.ts +328 -0
  177. package/src/file/ripgrep.ts +393 -0
  178. package/src/file/time.ts +64 -0
  179. package/src/file/watcher.ts +103 -0
  180. package/src/flag/flag.ts +84 -0
  181. package/src/format/formatter.ts +315 -0
  182. package/src/format/index.ts +137 -0
  183. package/src/global/index.ts +101 -0
  184. package/src/id/id.ts +73 -0
  185. package/src/ide/index.ts +76 -0
  186. package/src/index.ts +297 -0
  187. package/src/index.ts.backup +271 -0
  188. package/src/installation/index.ts +258 -0
  189. package/src/lib/IMPLEMENTATION_NOTES.md +345 -0
  190. package/src/lib/error-handler.ts +225 -0
  191. package/src/lib/error-testing-guide.md +258 -0
  192. package/src/lib/errors.ts +285 -0
  193. package/src/lib/performance.ts +70 -0
  194. package/src/lib/telemetry.ts +282 -0
  195. package/src/lsp/client.ts +229 -0
  196. package/src/lsp/index.ts +485 -0
  197. package/src/lsp/language.ts +116 -0
  198. package/src/lsp/server.ts +1895 -0
  199. package/src/mcp/auth.ts +135 -0
  200. package/src/mcp/index.ts +1117 -0
  201. package/src/mcp/intent-analyzer.ts +376 -0
  202. package/src/mcp/oauth-callback.ts +200 -0
  203. package/src/mcp/oauth-provider.ts +154 -0
  204. package/src/patch/index.ts +632 -0
  205. package/src/permission/index.ts +199 -0
  206. package/src/plugin/index.ts +91 -0
  207. package/src/project/bootstrap.ts +33 -0
  208. package/src/project/instance.ts +78 -0
  209. package/src/project/project.ts +236 -0
  210. package/src/project/state.ts +65 -0
  211. package/src/project/vcs.ts +76 -0
  212. package/src/provider/auth.ts +143 -0
  213. package/src/provider/models-macro.ts +55 -0
  214. package/src/provider/models.ts +161 -0
  215. package/src/provider/provider.ts +1109 -0
  216. package/src/provider/sdk/openai-compatible/src/README.md +5 -0
  217. package/src/provider/sdk/openai-compatible/src/index.ts +2 -0
  218. package/src/provider/sdk/openai-compatible/src/openai-compatible-provider.ts +100 -0
  219. package/src/provider/sdk/openai-compatible/src/responses/convert-to-openai-responses-input.ts +303 -0
  220. package/src/provider/sdk/openai-compatible/src/responses/map-openai-responses-finish-reason.ts +22 -0
  221. package/src/provider/sdk/openai-compatible/src/responses/openai-config.ts +18 -0
  222. package/src/provider/sdk/openai-compatible/src/responses/openai-error.ts +22 -0
  223. package/src/provider/sdk/openai-compatible/src/responses/openai-responses-api-types.ts +207 -0
  224. package/src/provider/sdk/openai-compatible/src/responses/openai-responses-language-model.ts +1713 -0
  225. package/src/provider/sdk/openai-compatible/src/responses/openai-responses-prepare-tools.ts +177 -0
  226. package/src/provider/sdk/openai-compatible/src/responses/openai-responses-settings.ts +1 -0
  227. package/src/provider/sdk/openai-compatible/src/responses/tool/code-interpreter.ts +88 -0
  228. package/src/provider/sdk/openai-compatible/src/responses/tool/file-search.ts +128 -0
  229. package/src/provider/sdk/openai-compatible/src/responses/tool/image-generation.ts +115 -0
  230. package/src/provider/sdk/openai-compatible/src/responses/tool/local-shell.ts +65 -0
  231. package/src/provider/sdk/openai-compatible/src/responses/tool/web-search-preview.ts +104 -0
  232. package/src/provider/sdk/openai-compatible/src/responses/tool/web-search.ts +103 -0
  233. package/src/provider/transform.ts +455 -0
  234. package/src/pty/index.ts +231 -0
  235. package/src/security/guardrails.test.ts +341 -0
  236. package/src/security/guardrails.ts +570 -0
  237. package/src/security/index.ts +19 -0
  238. package/src/server/error.ts +36 -0
  239. package/src/server/project.ts +79 -0
  240. package/src/server/server.ts +2641 -0
  241. package/src/server/tui.ts +71 -0
  242. package/src/session/compaction.ts +228 -0
  243. package/src/session/index.ts +464 -0
  244. package/src/session/llm.ts +201 -0
  245. package/src/session/message-v2.ts +695 -0
  246. package/src/session/message.ts +189 -0
  247. package/src/session/processor.ts +409 -0
  248. package/src/session/prompt/act-switch.txt +5 -0
  249. package/src/session/prompt/anthropic-20250930.txt +166 -0
  250. package/src/session/prompt/anthropic.txt +63 -0
  251. package/src/session/prompt/anthropic_spoof.txt +1 -0
  252. package/src/session/prompt/beast.txt +76 -0
  253. package/src/session/prompt/codex.txt +304 -0
  254. package/src/session/prompt/copilot-gpt-5.txt +137 -0
  255. package/src/session/prompt/gemini.txt +62 -0
  256. package/src/session/prompt/max-steps.txt +16 -0
  257. package/src/session/prompt/plan-reminder-anthropic.txt +35 -0
  258. package/src/session/prompt/plan.txt +24 -0
  259. package/src/session/prompt/polaris.txt +88 -0
  260. package/src/session/prompt/qwen.txt +59 -0
  261. package/src/session/prompt.ts +1552 -0
  262. package/src/session/retry.ts +86 -0
  263. package/src/session/revert.ts +108 -0
  264. package/src/session/sensitive-filter.test.ts +327 -0
  265. package/src/session/sensitive-filter.ts +466 -0
  266. package/src/session/status.ts +76 -0
  267. package/src/session/summary.ts +209 -0
  268. package/src/session/system.ts +122 -0
  269. package/src/session/todo.ts +37 -0
  270. package/src/share/share-next.ts +222 -0
  271. package/src/share/share.ts +87 -0
  272. package/src/shell/shell.ts +67 -0
  273. package/src/skill/index.ts +1 -0
  274. package/src/skill/skill.ts +83 -0
  275. package/src/snapshot/index.ts +197 -0
  276. package/src/storage/storage.ts +226 -0
  277. package/src/tests/agent.test.ts +308 -0
  278. package/src/tests/build-guards.test.ts +267 -0
  279. package/src/tests/config.test.ts +664 -0
  280. package/src/tests/tool-registry.test.ts +589 -0
  281. package/src/tool/bash.ts +314 -0
  282. package/src/tool/bash.txt +158 -0
  283. package/src/tool/batch.ts +175 -0
  284. package/src/tool/batch.txt +24 -0
  285. package/src/tool/codesearch.ts +184 -0
  286. package/src/tool/codesearch.txt +12 -0
  287. package/src/tool/edit.ts +675 -0
  288. package/src/tool/edit.txt +10 -0
  289. package/src/tool/glob.ts +65 -0
  290. package/src/tool/glob.txt +6 -0
  291. package/src/tool/grep.ts +121 -0
  292. package/src/tool/grep.txt +8 -0
  293. package/src/tool/invalid.ts +17 -0
  294. package/src/tool/ls.ts +110 -0
  295. package/src/tool/ls.txt +1 -0
  296. package/src/tool/lsp-diagnostics.ts +26 -0
  297. package/src/tool/lsp-diagnostics.txt +1 -0
  298. package/src/tool/lsp-hover.ts +31 -0
  299. package/src/tool/lsp-hover.txt +1 -0
  300. package/src/tool/lsp.ts +87 -0
  301. package/src/tool/lsp.txt +19 -0
  302. package/src/tool/multiedit.ts +46 -0
  303. package/src/tool/multiedit.txt +41 -0
  304. package/src/tool/patch.ts +233 -0
  305. package/src/tool/patch.txt +1 -0
  306. package/src/tool/read.ts +219 -0
  307. package/src/tool/read.txt +12 -0
  308. package/src/tool/registry.ts +162 -0
  309. package/src/tool/skill.ts +100 -0
  310. package/src/tool/task.ts +136 -0
  311. package/src/tool/task.txt +51 -0
  312. package/src/tool/todo.ts +39 -0
  313. package/src/tool/todoread.txt +14 -0
  314. package/src/tool/todowrite.txt +167 -0
  315. package/src/tool/tool.ts +71 -0
  316. package/src/tool/webfetch.ts +198 -0
  317. package/src/tool/webfetch.txt +13 -0
  318. package/src/tool/websearch.ts +268 -0
  319. package/src/tool/websearch.txt +13 -0
  320. package/src/tool/write.ts +110 -0
  321. package/src/tool/write.txt +8 -0
  322. package/src/util/archive.ts +16 -0
  323. package/src/util/color.ts +19 -0
  324. package/src/util/context.ts +25 -0
  325. package/src/util/defer.ts +12 -0
  326. package/src/util/eventloop.ts +20 -0
  327. package/src/util/filesystem.ts +83 -0
  328. package/src/util/fn.ts +11 -0
  329. package/src/util/iife.ts +3 -0
  330. package/src/util/keybind.ts +102 -0
  331. package/src/util/lazy.ts +11 -0
  332. package/src/util/license.ts +362 -0
  333. package/src/util/locale.ts +81 -0
  334. package/src/util/lock.ts +98 -0
  335. package/src/util/log.ts +180 -0
  336. package/src/util/queue.ts +32 -0
  337. package/src/util/rpc.ts +42 -0
  338. package/src/util/scrap.ts +10 -0
  339. package/src/util/signal.ts +12 -0
  340. package/src/util/timeout.ts +14 -0
  341. package/src/util/token.ts +7 -0
  342. package/src/util/wildcard.ts +54 -0
  343. package/sst-env.d.ts +9 -0
  344. package/test/agent/agent.test.ts +146 -0
  345. package/test/bun.test.ts +53 -0
  346. package/test/cli/cmd/acp.test.ts +144 -0
  347. package/test/cli/cmd/run.test.ts +250 -0
  348. package/test/cli/github-remote.test.ts +80 -0
  349. package/test/config/agent-color.test.ts +66 -0
  350. package/test/config/config.test.ts +536 -0
  351. package/test/config/markdown.test.ts +89 -0
  352. package/test/file/ignore.test.ts +10 -0
  353. package/test/fixture/fixture.ts +37 -0
  354. package/test/fixture/lsp/fake-lsp-server.js +77 -0
  355. package/test/helpers.ts +172 -0
  356. package/test/ide/ide.test.ts +82 -0
  357. package/test/installation/installation.test.ts +143 -0
  358. package/test/keybind.test.ts +421 -0
  359. package/test/lsp/client.test.ts +95 -0
  360. package/test/mcp/headers.test.ts +153 -0
  361. package/test/patch/patch.test.ts +348 -0
  362. package/test/preload.ts +57 -0
  363. package/test/project/project.test.ts +74 -0
  364. package/test/provider/provider.test.ts +74 -0
  365. package/test/provider/transform.test.ts +411 -0
  366. package/test/session/retry.test.ts +111 -0
  367. package/test/session/session.test.ts +71 -0
  368. package/test/skill/skill.test.ts +131 -0
  369. package/test/snapshot/snapshot.test.ts +940 -0
  370. package/test/tool/__snapshots__/tool.test.ts.snap +9 -0
  371. package/test/tool/bash.test.ts +434 -0
  372. package/test/tool/grep.test.ts +108 -0
  373. package/test/tool/patch.test.ts +259 -0
  374. package/test/tool/read.test.ts +42 -0
  375. package/test/util/iife.test.ts +36 -0
  376. package/test/util/lazy.test.ts +50 -0
  377. package/test/util/license.test.ts +235 -0
  378. package/test/util/timeout.test.ts +21 -0
  379. package/test/util/wildcard.test.ts +55 -0
  380. package/tsconfig.json +16 -0
  381. package/update-versions.ps1 +65 -0
@@ -0,0 +1,464 @@
1
+ import { BusEvent } from "@/bus/bus-event"
2
+ import { Bus } from "@/bus"
3
+ import { Decimal } from "decimal.js"
4
+ import z from "zod"
5
+ import { type LanguageModelUsage, type ProviderMetadata } from "ai"
6
+ import { Config } from "../config/config"
7
+ import { Flag } from "../flag/flag"
8
+ import { Identifier } from "../id/id"
9
+ import { Installation } from "../installation"
10
+
11
+ import { Storage } from "../storage/storage"
12
+ import { Log } from "../util/log"
13
+ import { MessageV2 } from "./message-v2"
14
+ import { Instance } from "../project/instance"
15
+ import { SessionPrompt } from "./prompt"
16
+ import { fn } from "@/util/fn"
17
+ import { Command } from "../command"
18
+ import { Snapshot } from "@/snapshot"
19
+ import { SensitiveFilter } from "./sensitive-filter"
20
+
21
+ import type { Provider } from "@/provider/provider"
22
+
23
+ export namespace Session {
24
+ const log = Log.create({ service: "session" })
25
+
26
+ const parentTitlePrefix = "New session - "
27
+ const childTitlePrefix = "Child session - "
28
+
29
+ function createDefaultTitle(isChild = false) {
30
+ return (isChild ? childTitlePrefix : parentTitlePrefix) + new Date().toISOString()
31
+ }
32
+
33
+ export function isDefaultTitle(title: string) {
34
+ return new RegExp(
35
+ `^(${parentTitlePrefix}|${childTitlePrefix})\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}\\.\\d{3}Z$`,
36
+ ).test(title)
37
+ }
38
+
39
+ export const Info = z
40
+ .object({
41
+ id: Identifier.schema("session"),
42
+ projectID: z.string(),
43
+ directory: z.string(),
44
+ parentID: Identifier.schema("session").optional(),
45
+ summary: z
46
+ .object({
47
+ additions: z.number(),
48
+ deletions: z.number(),
49
+ files: z.number(),
50
+ diffs: Snapshot.FileDiff.array().optional(),
51
+ })
52
+ .optional(),
53
+ share: z
54
+ .object({
55
+ url: z.string(),
56
+ })
57
+ .optional(),
58
+ title: z.string(),
59
+ version: z.string(),
60
+ time: z.object({
61
+ created: z.number(),
62
+ updated: z.number(),
63
+ compacting: z.number().optional(),
64
+ archived: z.number().optional(),
65
+ }),
66
+ revert: z
67
+ .object({
68
+ messageID: z.string(),
69
+ partID: z.string().optional(),
70
+ snapshot: z.string().optional(),
71
+ diff: z.string().optional(),
72
+ })
73
+ .optional(),
74
+ })
75
+ .meta({
76
+ ref: "Session",
77
+ })
78
+ export type Info = z.output<typeof Info>
79
+
80
+ export const ShareInfo = z
81
+ .object({
82
+ secret: z.string(),
83
+ url: z.string(),
84
+ })
85
+ .meta({
86
+ ref: "SessionShare",
87
+ })
88
+ export type ShareInfo = z.output<typeof ShareInfo>
89
+
90
+ export const Event = {
91
+ Created: BusEvent.define(
92
+ "session.created",
93
+ z.object({
94
+ info: Info,
95
+ }),
96
+ ),
97
+ Updated: BusEvent.define(
98
+ "session.updated",
99
+ z.object({
100
+ info: Info,
101
+ }),
102
+ ),
103
+ Deleted: BusEvent.define(
104
+ "session.deleted",
105
+ z.object({
106
+ info: Info,
107
+ }),
108
+ ),
109
+ Diff: BusEvent.define(
110
+ "session.diff",
111
+ z.object({
112
+ sessionID: z.string(),
113
+ diff: Snapshot.FileDiff.array(),
114
+ }),
115
+ ),
116
+ Error: BusEvent.define(
117
+ "session.error",
118
+ z.object({
119
+ sessionID: z.string().optional(),
120
+ error: MessageV2.Assistant.shape.error,
121
+ }),
122
+ ),
123
+ }
124
+
125
+ export const create = fn(
126
+ z
127
+ .object({
128
+ parentID: Identifier.schema("session").optional(),
129
+ title: z.string().optional(),
130
+ })
131
+ .optional(),
132
+ async (input) => {
133
+ return createNext({
134
+ parentID: input?.parentID,
135
+ directory: Instance.directory,
136
+ title: input?.title,
137
+ })
138
+ },
139
+ )
140
+
141
+ export const fork = fn(
142
+ z.object({
143
+ sessionID: Identifier.schema("session"),
144
+ messageID: Identifier.schema("message").optional(),
145
+ }),
146
+ async (input) => {
147
+ const session = await createNext({
148
+ directory: Instance.directory,
149
+ })
150
+ const msgs = await messages({ sessionID: input.sessionID })
151
+ for (const msg of msgs) {
152
+ if (input.messageID && msg.info.id >= input.messageID) break
153
+ const cloned = await updateMessage({
154
+ ...msg.info,
155
+ sessionID: session.id,
156
+ id: Identifier.ascending("message"),
157
+ })
158
+
159
+ for (const part of msg.parts) {
160
+ await updatePart({
161
+ ...part,
162
+ id: Identifier.ascending("part"),
163
+ messageID: cloned.id,
164
+ sessionID: session.id,
165
+ })
166
+ }
167
+ }
168
+ return session
169
+ },
170
+ )
171
+
172
+ export const touch = fn(Identifier.schema("session"), async (sessionID) => {
173
+ await update(sessionID, (draft) => {
174
+ draft.time.updated = Date.now()
175
+ })
176
+ })
177
+
178
+ export async function createNext(input: { id?: string; title?: string; parentID?: string; directory: string }) {
179
+ const result: Info = {
180
+ id: Identifier.descending("session", input.id),
181
+ version: Installation.VERSION,
182
+ projectID: Instance.project.id,
183
+ directory: input.directory,
184
+ parentID: input.parentID,
185
+ title: input.title ?? createDefaultTitle(!!input.parentID),
186
+ time: {
187
+ created: Date.now(),
188
+ updated: Date.now(),
189
+ },
190
+ }
191
+ log.info("created", result)
192
+ await Storage.write(["session", Instance.project.id, result.id], result)
193
+ Bus.publish(Event.Created, {
194
+ info: result,
195
+ })
196
+ const cfg = await Config.get()
197
+ if (!result.parentID && (Flag.OPENCODE_AUTO_SHARE || cfg.share === "auto"))
198
+ share(result.id)
199
+ .then((share) => {
200
+ update(result.id, (draft) => {
201
+ draft.share = share
202
+ })
203
+ })
204
+ .catch(() => {
205
+ // Silently ignore sharing errors during session creation
206
+ })
207
+ Bus.publish(Event.Updated, {
208
+ info: result,
209
+ })
210
+ return result
211
+ }
212
+
213
+ export const get = fn(Identifier.schema("session"), async (id) => {
214
+ const read = await Storage.read<Info>(["session", Instance.project.id, id])
215
+ return read as Info
216
+ })
217
+
218
+ export const getShare = fn(Identifier.schema("session"), async (id) => {
219
+ return Storage.read<ShareInfo>(["share", id])
220
+ })
221
+
222
+ export const share = fn(Identifier.schema("session"), async (id) => {
223
+ const cfg = await Config.get()
224
+ if (cfg.share === "disabled") {
225
+ throw new Error("Sharing is disabled in configuration")
226
+ }
227
+ const { ShareNext } = await import("@/share/share-next")
228
+ const share = await ShareNext.create(id)
229
+ if ("error" in share) {
230
+ throw new Error(share.error)
231
+ }
232
+ await update(id, (draft) => {
233
+ draft.share = {
234
+ url: share.url,
235
+ }
236
+ })
237
+ return share
238
+ })
239
+
240
+ export const unshare = fn(Identifier.schema("session"), async (id) => {
241
+ // Use ShareNext to remove the share (same as share function uses ShareNext to create)
242
+ const { ShareNext } = await import("@/share/share-next")
243
+ await ShareNext.remove(id)
244
+ await update(id, (draft) => {
245
+ draft.share = undefined
246
+ })
247
+ })
248
+
249
+ export async function update(id: string, editor: (session: Info) => void) {
250
+ const project = Instance.project
251
+ const result = await Storage.update<Info>(["session", project.id, id], (draft) => {
252
+ editor(draft)
253
+ draft.time.updated = Date.now()
254
+ })
255
+ Bus.publish(Event.Updated, {
256
+ info: result,
257
+ })
258
+ return result
259
+ }
260
+
261
+ export const diff = fn(Identifier.schema("session"), async (sessionID) => {
262
+ const diffs = await Storage.read<Snapshot.FileDiff[]>(["session_diff", sessionID])
263
+ return diffs ?? []
264
+ })
265
+
266
+ export const messages = fn(
267
+ z.object({
268
+ sessionID: Identifier.schema("session"),
269
+ limit: z.number().optional(),
270
+ }),
271
+ async (input) => {
272
+ const result = [] as MessageV2.WithParts[]
273
+ for await (const msg of MessageV2.stream(input.sessionID)) {
274
+ if (input.limit && result.length >= input.limit) break
275
+ result.push(msg)
276
+ }
277
+ result.reverse()
278
+ return result
279
+ },
280
+ )
281
+
282
+ export async function* list() {
283
+ const project = Instance.project
284
+ for (const item of await Storage.list(["session", project.id])) {
285
+ yield Storage.read<Info>(item)
286
+ }
287
+ }
288
+
289
+ export const children = fn(Identifier.schema("session"), async (parentID) => {
290
+ const project = Instance.project
291
+ const result = [] as Session.Info[]
292
+ for (const item of await Storage.list(["session", project.id])) {
293
+ const session = await Storage.read<Info>(item)
294
+ if (session.parentID !== parentID) continue
295
+ result.push(session)
296
+ }
297
+ return result
298
+ })
299
+
300
+ export const remove = fn(Identifier.schema("session"), async (sessionID) => {
301
+ const project = Instance.project
302
+ try {
303
+ const session = await get(sessionID)
304
+ for (const child of await children(sessionID)) {
305
+ await remove(child.id)
306
+ }
307
+ await unshare(sessionID).catch(() => {})
308
+ for (const msg of await Storage.list(["message", sessionID])) {
309
+ for (const part of await Storage.list(["part", msg.at(-1)!])) {
310
+ await Storage.remove(part)
311
+ }
312
+ await Storage.remove(msg)
313
+ }
314
+ await Storage.remove(["session", project.id, sessionID])
315
+ // Clean up sensitive data mappings for this session
316
+ SensitiveFilter.clearSession(sessionID)
317
+ Bus.publish(Event.Deleted, {
318
+ info: session,
319
+ })
320
+ } catch (e) {
321
+ log.error(e)
322
+ }
323
+ })
324
+
325
+ export const updateMessage = fn(MessageV2.Info, async (msg) => {
326
+ await Storage.write(["message", msg.sessionID, msg.id], msg)
327
+ Bus.publish(MessageV2.Event.Updated, {
328
+ info: msg,
329
+ })
330
+ return msg
331
+ })
332
+
333
+ export const removeMessage = fn(
334
+ z.object({
335
+ sessionID: Identifier.schema("session"),
336
+ messageID: Identifier.schema("message"),
337
+ }),
338
+ async (input) => {
339
+ await Storage.remove(["message", input.sessionID, input.messageID])
340
+ Bus.publish(MessageV2.Event.Removed, {
341
+ sessionID: input.sessionID,
342
+ messageID: input.messageID,
343
+ })
344
+ return input.messageID
345
+ },
346
+ )
347
+
348
+ export const removePart = fn(
349
+ z.object({
350
+ sessionID: Identifier.schema("session"),
351
+ messageID: Identifier.schema("message"),
352
+ partID: Identifier.schema("part"),
353
+ }),
354
+ async (input) => {
355
+ await Storage.remove(["part", input.messageID, input.partID])
356
+ Bus.publish(MessageV2.Event.PartRemoved, {
357
+ sessionID: input.sessionID,
358
+ messageID: input.messageID,
359
+ partID: input.partID,
360
+ })
361
+ return input.partID
362
+ },
363
+ )
364
+
365
+ const UpdatePartInput = z.union([
366
+ MessageV2.Part,
367
+ z.object({
368
+ part: MessageV2.TextPart,
369
+ delta: z.string(),
370
+ }),
371
+ z.object({
372
+ part: MessageV2.ReasoningPart,
373
+ delta: z.string(),
374
+ }),
375
+ ])
376
+
377
+ export const updatePart = fn(UpdatePartInput, async (input) => {
378
+ const part = "delta" in input ? input.part : input
379
+ const delta = "delta" in input ? input.delta : undefined
380
+ await Storage.write(["part", part.messageID, part.id], part)
381
+ Bus.publish(MessageV2.Event.PartUpdated, {
382
+ part,
383
+ delta,
384
+ })
385
+ return part
386
+ })
387
+
388
+ export const getUsage = fn(
389
+ z.object({
390
+ model: z.custom<Provider.Model>(),
391
+ usage: z.custom<LanguageModelUsage>(),
392
+ metadata: z.custom<ProviderMetadata>().optional(),
393
+ }),
394
+ (input) => {
395
+ const cachedInputTokens = input.usage.cachedInputTokens ?? 0
396
+ const excludesCachedTokens = !!(input.metadata?.["anthropic"] || input.metadata?.["bedrock"])
397
+ const adjustedInputTokens = excludesCachedTokens
398
+ ? (input.usage.inputTokens ?? 0)
399
+ : (input.usage.inputTokens ?? 0) - cachedInputTokens
400
+ const safe = (value: number) => {
401
+ if (!Number.isFinite(value)) return 0
402
+ return value
403
+ }
404
+
405
+ const tokens = {
406
+ input: safe(adjustedInputTokens),
407
+ output: safe(input.usage.outputTokens ?? 0),
408
+ reasoning: safe(input.usage?.reasoningTokens ?? 0),
409
+ cache: {
410
+ write: safe(
411
+ (input.metadata?.["anthropic"]?.["cacheCreationInputTokens"] ??
412
+ // @ts-expect-error
413
+ input.metadata?.["bedrock"]?.["usage"]?.["cacheWriteInputTokens"] ??
414
+ 0) as number,
415
+ ),
416
+ read: safe(cachedInputTokens),
417
+ },
418
+ }
419
+
420
+ const costInfo =
421
+ input.model.cost?.experimentalOver200K && tokens.input + tokens.cache.read > 200_000
422
+ ? input.model.cost.experimentalOver200K
423
+ : input.model.cost
424
+ return {
425
+ cost: safe(
426
+ new Decimal(0)
427
+ .add(new Decimal(tokens.input).mul(costInfo?.input ?? 0).div(1_000_000))
428
+ .add(new Decimal(tokens.output).mul(costInfo?.output ?? 0).div(1_000_000))
429
+ .add(new Decimal(tokens.cache.read).mul(costInfo?.cache?.read ?? 0).div(1_000_000))
430
+ .add(new Decimal(tokens.cache.write).mul(costInfo?.cache?.write ?? 0).div(1_000_000))
431
+ // TODO: improve model pricing metadata; for now:
432
+ // charge reasoning tokens at the same rate as output tokens
433
+ .add(new Decimal(tokens.reasoning).mul(costInfo?.output ?? 0).div(1_000_000))
434
+ .toNumber(),
435
+ ),
436
+ tokens,
437
+ }
438
+ },
439
+ )
440
+
441
+ export class BusyError extends Error {
442
+ constructor(public readonly sessionID: string) {
443
+ super(`Session ${sessionID} is busy`)
444
+ }
445
+ }
446
+
447
+ export const initialize = fn(
448
+ z.object({
449
+ sessionID: Identifier.schema("session"),
450
+ modelID: z.string(),
451
+ providerID: z.string(),
452
+ messageID: Identifier.schema("message"),
453
+ }),
454
+ async (input) => {
455
+ await SessionPrompt.command({
456
+ sessionID: input.sessionID,
457
+ messageID: input.messageID,
458
+ model: input.providerID + "/" + input.modelID,
459
+ command: Command.Default.INIT,
460
+ arguments: "",
461
+ })
462
+ },
463
+ )
464
+ }
@@ -0,0 +1,201 @@
1
+ import { Provider } from "@/provider/provider"
2
+ import { Log } from "@/util/log"
3
+ import { streamText, wrapLanguageModel, type ModelMessage, type StreamTextResult, type Tool, type ToolSet } from "ai"
4
+ import { clone, mergeDeep, pipe } from "remeda"
5
+ import { ProviderTransform } from "@/provider/transform"
6
+ import { Config } from "@/config/config"
7
+ import { Instance } from "@/project/instance"
8
+ import type { Agent } from "@/agent/agent"
9
+ import type { MessageV2 } from "./message-v2"
10
+ import { Plugin } from "@/plugin"
11
+ import { SystemPrompt } from "./system"
12
+ import { ToolRegistry } from "@/tool/registry"
13
+ import { Flag } from "@/flag/flag"
14
+
15
+ export namespace LLM {
16
+ const log = Log.create({ service: "llm" })
17
+
18
+ export const OUTPUT_TOKEN_MAX = Flag.OPENCODE_EXPERIMENTAL_OUTPUT_TOKEN_MAX || 32_000
19
+
20
+ export type StreamInput = {
21
+ user: MessageV2.User
22
+ sessionID: string
23
+ model: Provider.Model
24
+ agent: Agent.Info
25
+ system: string[]
26
+ abort: AbortSignal
27
+ messages: ModelMessage[]
28
+ small?: boolean
29
+ tools: Record<string, Tool>
30
+ retries?: number
31
+ }
32
+
33
+ export type StreamOutput = StreamTextResult<ToolSet, unknown>
34
+
35
+ export async function stream(input: StreamInput) {
36
+ const l = log
37
+ .clone()
38
+ .tag("providerID", input.model.providerID)
39
+ .tag("modelID", input.model.id)
40
+ .tag("sessionID", input.sessionID)
41
+ .tag("small", (input.small ?? false).toString())
42
+ .tag("agent", input.agent.name)
43
+ l.info("stream", {
44
+ modelID: input.model.id,
45
+ providerID: input.model.providerID,
46
+ })
47
+ const [language, cfg] = await Promise.all([Provider.getLanguage(input.model), Config.get()])
48
+
49
+ const system = SystemPrompt.header(input.model.providerID)
50
+ system.push(
51
+ [
52
+ // use agent prompt otherwise provider prompt
53
+ ...(input.agent.prompt ? [input.agent.prompt] : SystemPrompt.provider(input.model)),
54
+ // any custom prompt passed into this call
55
+ ...input.system,
56
+ // any custom prompt from last user message
57
+ ...(input.user.system ? [input.user.system] : []),
58
+ ]
59
+ .filter((x) => x)
60
+ .join("\n"),
61
+ )
62
+
63
+ const header = system[0]
64
+ const original = clone(system)
65
+ await Plugin.trigger("experimental.chat.system.transform", {}, { system })
66
+ if (system.length === 0) {
67
+ system.push(...original)
68
+ }
69
+ // rejoin to maintain 2-part structure for caching if header unchanged
70
+ if (system.length > 2 && system[0] === header) {
71
+ const rest = system.slice(1)
72
+ system.length = 0
73
+ system.push(header, rest.join("\n"))
74
+ }
75
+
76
+ const provider = await Provider.getProvider(input.model.providerID)
77
+
78
+ const params = await Plugin.trigger(
79
+ "chat.params",
80
+ {
81
+ sessionID: input.sessionID,
82
+ agent: input.agent,
83
+ model: input.model,
84
+ provider: Provider.getProvider(input.model.providerID),
85
+ message: input.user,
86
+ },
87
+ {
88
+ temperature: input.model.capabilities.temperature
89
+ ? (input.agent.temperature ?? ProviderTransform.temperature(input.model))
90
+ : undefined,
91
+ topP: input.agent.topP ?? ProviderTransform.topP(input.model),
92
+ topK: ProviderTransform.topK(input.model),
93
+ options: pipe(
94
+ {},
95
+ mergeDeep(ProviderTransform.options(input.model, input.sessionID, provider.options)),
96
+ input.small ? mergeDeep(ProviderTransform.smallOptions(input.model)) : mergeDeep({}),
97
+ mergeDeep(input.model.options),
98
+ mergeDeep(input.agent.options),
99
+ ),
100
+ },
101
+ )
102
+
103
+ l.info("params", {
104
+ params,
105
+ })
106
+
107
+ const maxOutputTokens = ProviderTransform.maxOutputTokens(
108
+ input.model.api.npm,
109
+ params.options,
110
+ input.model.limit.output,
111
+ OUTPUT_TOKEN_MAX,
112
+ )
113
+
114
+ const tools = await resolveTools(input)
115
+
116
+ return streamText({
117
+ onError(error) {
118
+ l.error("stream error", {
119
+ error,
120
+ })
121
+ },
122
+ async experimental_repairToolCall(failed) {
123
+ const lower = failed.toolCall.toolName.toLowerCase()
124
+ if (lower !== failed.toolCall.toolName && tools[lower]) {
125
+ l.info("repairing tool call", {
126
+ tool: failed.toolCall.toolName,
127
+ repaired: lower,
128
+ })
129
+ return {
130
+ ...failed.toolCall,
131
+ toolName: lower,
132
+ }
133
+ }
134
+ return {
135
+ ...failed.toolCall,
136
+ input: JSON.stringify({
137
+ tool: failed.toolCall.toolName,
138
+ error: failed.error.message,
139
+ }),
140
+ toolName: "invalid",
141
+ }
142
+ },
143
+ temperature: params.temperature,
144
+ topP: params.topP,
145
+ topK: params.topK,
146
+ providerOptions: ProviderTransform.providerOptions(input.model, params.options),
147
+ activeTools: Object.keys(tools).filter((x) => x !== "invalid"),
148
+ tools,
149
+ maxOutputTokens,
150
+ abortSignal: input.abort,
151
+ headers: {
152
+ ...(input.model.providerID.startsWith("rird")
153
+ ? {
154
+ "x-rird-project": Instance.project.id,
155
+ "x-rird-session": input.sessionID,
156
+ "x-rird-request": input.user.id,
157
+ "x-rird-client": Flag.OPENCODE_CLIENT,
158
+ }
159
+ : undefined),
160
+ ...input.model.headers,
161
+ },
162
+ maxRetries: input.retries ?? 0,
163
+ messages: [
164
+ ...system.map(
165
+ (x): ModelMessage => ({
166
+ role: "system",
167
+ content: x,
168
+ }),
169
+ ),
170
+ ...input.messages,
171
+ ],
172
+ model: wrapLanguageModel({
173
+ model: language,
174
+ middleware: [
175
+ {
176
+ async transformParams(args) {
177
+ if (args.type === "stream") {
178
+ // @ts-expect-error
179
+ args.params.prompt = ProviderTransform.message(args.params.prompt, input.model)
180
+ }
181
+ return args.params
182
+ },
183
+ },
184
+ ],
185
+ }),
186
+ experimental_telemetry: { isEnabled: cfg.experimental?.openTelemetry },
187
+ })
188
+ }
189
+
190
+ async function resolveTools(input: Pick<StreamInput, "tools" | "agent" | "user">) {
191
+ const enabled = pipe(
192
+ input.agent.tools,
193
+ mergeDeep(await ToolRegistry.enabled(input.agent)),
194
+ mergeDeep(input.user.tools ?? {}),
195
+ )
196
+ for (const [key, value] of Object.entries(enabled)) {
197
+ if (value === false) delete input.tools[key]
198
+ }
199
+ return input.tools
200
+ }
201
+ }