codepro-ia 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (393) hide show
  1. package/AGENTS.md +27 -0
  2. package/Dockerfile +18 -0
  3. package/README.md +70 -0
  4. package/bin/opencode +84 -0
  5. package/bunfig.toml +7 -0
  6. package/package.json +144 -0
  7. package/parsers-config.ts +253 -0
  8. package/script/build.ts +167 -0
  9. package/script/postinstall.mjs +125 -0
  10. package/script/publish-registries.ts +187 -0
  11. package/script/publish.ts +70 -0
  12. package/script/schema.ts +47 -0
  13. package/src/acp/README.md +164 -0
  14. package/src/acp/agent.ts +1127 -0
  15. package/src/acp/session.ts +101 -0
  16. package/src/acp/types.ts +22 -0
  17. package/src/agent/agent.ts +311 -0
  18. package/src/agent/generate.txt +75 -0
  19. package/src/agent/prompt/compaction.txt +12 -0
  20. package/src/agent/prompt/explore.txt +18 -0
  21. package/src/agent/prompt/summary.txt +11 -0
  22. package/src/agent/prompt/title.txt +43 -0
  23. package/src/auth/index.ts +73 -0
  24. package/src/bun/index.ts +134 -0
  25. package/src/bus/bus-event.ts +43 -0
  26. package/src/bus/global.ts +10 -0
  27. package/src/bus/index.ts +105 -0
  28. package/src/cli/bootstrap.ts +17 -0
  29. package/src/cli/cmd/acp.ts +69 -0
  30. package/src/cli/cmd/agent.ts +257 -0
  31. package/src/cli/cmd/auth.ts +400 -0
  32. package/src/cli/cmd/cmd.ts +7 -0
  33. package/src/cli/cmd/debug/agent.ts +166 -0
  34. package/src/cli/cmd/debug/config.ts +16 -0
  35. package/src/cli/cmd/debug/file.ts +97 -0
  36. package/src/cli/cmd/debug/index.ts +48 -0
  37. package/src/cli/cmd/debug/lsp.ts +52 -0
  38. package/src/cli/cmd/debug/ripgrep.ts +87 -0
  39. package/src/cli/cmd/debug/scrap.ts +16 -0
  40. package/src/cli/cmd/debug/skill.ts +16 -0
  41. package/src/cli/cmd/debug/snapshot.ts +52 -0
  42. package/src/cli/cmd/export.ts +88 -0
  43. package/src/cli/cmd/generate.ts +38 -0
  44. package/src/cli/cmd/github.ts +1548 -0
  45. package/src/cli/cmd/import.ts +98 -0
  46. package/src/cli/cmd/mcp.ts +755 -0
  47. package/src/cli/cmd/models.ts +77 -0
  48. package/src/cli/cmd/pr.ts +112 -0
  49. package/src/cli/cmd/run.ts +395 -0
  50. package/src/cli/cmd/serve.ts +20 -0
  51. package/src/cli/cmd/session.ts +135 -0
  52. package/src/cli/cmd/stats.ts +402 -0
  53. package/src/cli/cmd/tui/app.tsx +724 -0
  54. package/src/cli/cmd/tui/attach.ts +31 -0
  55. package/src/cli/cmd/tui/component/border.tsx +21 -0
  56. package/src/cli/cmd/tui/component/dialog-agent.tsx +31 -0
  57. package/src/cli/cmd/tui/component/dialog-command.tsx +124 -0
  58. package/src/cli/cmd/tui/component/dialog-mcp.tsx +86 -0
  59. package/src/cli/cmd/tui/component/dialog-model.tsx +234 -0
  60. package/src/cli/cmd/tui/component/dialog-provider.tsx +256 -0
  61. package/src/cli/cmd/tui/component/dialog-session-list.tsx +114 -0
  62. package/src/cli/cmd/tui/component/dialog-session-rename.tsx +31 -0
  63. package/src/cli/cmd/tui/component/dialog-stash.tsx +87 -0
  64. package/src/cli/cmd/tui/component/dialog-status.tsx +164 -0
  65. package/src/cli/cmd/tui/component/dialog-tag.tsx +44 -0
  66. package/src/cli/cmd/tui/component/dialog-theme-list.tsx +50 -0
  67. package/src/cli/cmd/tui/component/logo.tsx +106 -0
  68. package/src/cli/cmd/tui/component/prompt/autocomplete.tsx +763 -0
  69. package/src/cli/cmd/tui/component/prompt/frecency.tsx +89 -0
  70. package/src/cli/cmd/tui/component/prompt/history.tsx +108 -0
  71. package/src/cli/cmd/tui/component/prompt/index.tsx +1090 -0
  72. package/src/cli/cmd/tui/component/prompt/stash.tsx +101 -0
  73. package/src/cli/cmd/tui/component/textarea-keybindings.ts +73 -0
  74. package/src/cli/cmd/tui/component/tips.tsx +153 -0
  75. package/src/cli/cmd/tui/component/todo-item.tsx +32 -0
  76. package/src/cli/cmd/tui/context/args.tsx +14 -0
  77. package/src/cli/cmd/tui/context/directory.ts +13 -0
  78. package/src/cli/cmd/tui/context/exit.tsx +23 -0
  79. package/src/cli/cmd/tui/context/helper.tsx +25 -0
  80. package/src/cli/cmd/tui/context/keybind.tsx +101 -0
  81. package/src/cli/cmd/tui/context/kv.tsx +52 -0
  82. package/src/cli/cmd/tui/context/local.tsx +402 -0
  83. package/src/cli/cmd/tui/context/prompt.tsx +18 -0
  84. package/src/cli/cmd/tui/context/route.tsx +46 -0
  85. package/src/cli/cmd/tui/context/sdk.tsx +94 -0
  86. package/src/cli/cmd/tui/context/sync.tsx +427 -0
  87. package/src/cli/cmd/tui/context/theme/aura.json +69 -0
  88. package/src/cli/cmd/tui/context/theme/ayu.json +80 -0
  89. package/src/cli/cmd/tui/context/theme/carbonfox.json +248 -0
  90. package/src/cli/cmd/tui/context/theme/catppuccin-frappe.json +233 -0
  91. package/src/cli/cmd/tui/context/theme/catppuccin-macchiato.json +233 -0
  92. package/src/cli/cmd/tui/context/theme/catppuccin.json +112 -0
  93. package/src/cli/cmd/tui/context/theme/cobalt2.json +228 -0
  94. package/src/cli/cmd/tui/context/theme/cursor.json +249 -0
  95. package/src/cli/cmd/tui/context/theme/dracula.json +219 -0
  96. package/src/cli/cmd/tui/context/theme/everforest.json +241 -0
  97. package/src/cli/cmd/tui/context/theme/flexoki.json +237 -0
  98. package/src/cli/cmd/tui/context/theme/github.json +233 -0
  99. package/src/cli/cmd/tui/context/theme/gruvbox.json +95 -0
  100. package/src/cli/cmd/tui/context/theme/kanagawa.json +77 -0
  101. package/src/cli/cmd/tui/context/theme/lucent-orng.json +237 -0
  102. package/src/cli/cmd/tui/context/theme/material.json +235 -0
  103. package/src/cli/cmd/tui/context/theme/matrix.json +77 -0
  104. package/src/cli/cmd/tui/context/theme/mercury.json +252 -0
  105. package/src/cli/cmd/tui/context/theme/monokai.json +221 -0
  106. package/src/cli/cmd/tui/context/theme/nightowl.json +221 -0
  107. package/src/cli/cmd/tui/context/theme/nord.json +223 -0
  108. package/src/cli/cmd/tui/context/theme/one-dark.json +84 -0
  109. package/src/cli/cmd/tui/context/theme/orng.json +249 -0
  110. package/src/cli/cmd/tui/context/theme/osaka-jade.json +93 -0
  111. package/src/cli/cmd/tui/context/theme/palenight.json +222 -0
  112. package/src/cli/cmd/tui/context/theme/rosepine.json +234 -0
  113. package/src/cli/cmd/tui/context/theme/solarized.json +223 -0
  114. package/src/cli/cmd/tui/context/theme/synthwave84.json +226 -0
  115. package/src/cli/cmd/tui/context/theme/tokyonight.json +243 -0
  116. package/src/cli/cmd/tui/context/theme/vercel.json +245 -0
  117. package/src/cli/cmd/tui/context/theme/vesper.json +218 -0
  118. package/src/cli/cmd/tui/context/theme/zenburn.json +223 -0
  119. package/src/cli/cmd/tui/context/theme.tsx +1152 -0
  120. package/src/cli/cmd/tui/event.ts +46 -0
  121. package/src/cli/cmd/tui/routes/home.tsx +141 -0
  122. package/src/cli/cmd/tui/routes/session/dialog-fork-from-timeline.tsx +64 -0
  123. package/src/cli/cmd/tui/routes/session/dialog-message.tsx +109 -0
  124. package/src/cli/cmd/tui/routes/session/dialog-subagent.tsx +26 -0
  125. package/src/cli/cmd/tui/routes/session/dialog-timeline.tsx +47 -0
  126. package/src/cli/cmd/tui/routes/session/footer.tsx +91 -0
  127. package/src/cli/cmd/tui/routes/session/header.tsx +136 -0
  128. package/src/cli/cmd/tui/routes/session/index.tsx +1936 -0
  129. package/src/cli/cmd/tui/routes/session/permission.tsx +489 -0
  130. package/src/cli/cmd/tui/routes/session/question.tsx +435 -0
  131. package/src/cli/cmd/tui/routes/session/sidebar.tsx +312 -0
  132. package/src/cli/cmd/tui/thread.ts +165 -0
  133. package/src/cli/cmd/tui/ui/dialog-alert.tsx +57 -0
  134. package/src/cli/cmd/tui/ui/dialog-confirm.tsx +83 -0
  135. package/src/cli/cmd/tui/ui/dialog-export-options.tsx +204 -0
  136. package/src/cli/cmd/tui/ui/dialog-help.tsx +38 -0
  137. package/src/cli/cmd/tui/ui/dialog-prompt.tsx +77 -0
  138. package/src/cli/cmd/tui/ui/dialog-select.tsx +354 -0
  139. package/src/cli/cmd/tui/ui/dialog.tsx +167 -0
  140. package/src/cli/cmd/tui/ui/link.tsx +28 -0
  141. package/src/cli/cmd/tui/ui/spinner.ts +368 -0
  142. package/src/cli/cmd/tui/ui/toast.tsx +100 -0
  143. package/src/cli/cmd/tui/util/clipboard.ts +144 -0
  144. package/src/cli/cmd/tui/util/editor.ts +32 -0
  145. package/src/cli/cmd/tui/util/signal.ts +7 -0
  146. package/src/cli/cmd/tui/util/terminal.ts +114 -0
  147. package/src/cli/cmd/tui/util/transcript.ts +98 -0
  148. package/src/cli/cmd/tui/worker.ts +152 -0
  149. package/src/cli/cmd/uninstall.ts +344 -0
  150. package/src/cli/cmd/upgrade.ts +73 -0
  151. package/src/cli/cmd/web.ts +81 -0
  152. package/src/cli/error.ts +57 -0
  153. package/src/cli/network.ts +53 -0
  154. package/src/cli/ui.ts +88 -0
  155. package/src/cli/upgrade.ts +25 -0
  156. package/src/command/index.ts +131 -0
  157. package/src/command/template/initialize.txt +10 -0
  158. package/src/command/template/review.txt +97 -0
  159. package/src/config/config.ts +1246 -0
  160. package/src/config/markdown.ts +93 -0
  161. package/src/env/index.ts +26 -0
  162. package/src/file/ignore.ts +83 -0
  163. package/src/file/index.ts +411 -0
  164. package/src/file/ripgrep.ts +409 -0
  165. package/src/file/time.ts +64 -0
  166. package/src/file/watcher.ts +118 -0
  167. package/src/flag/flag.ts +54 -0
  168. package/src/format/formatter.ts +359 -0
  169. package/src/format/index.ts +137 -0
  170. package/src/global/index.ts +55 -0
  171. package/src/id/id.ts +83 -0
  172. package/src/ide/index.ts +76 -0
  173. package/src/index.ts +159 -0
  174. package/src/installation/index.ts +246 -0
  175. package/src/lsp/client.ts +252 -0
  176. package/src/lsp/index.ts +485 -0
  177. package/src/lsp/language.ts +119 -0
  178. package/src/lsp/server.ts +2032 -0
  179. package/src/mcp/auth.ts +135 -0
  180. package/src/mcp/index.ts +926 -0
  181. package/src/mcp/oauth-callback.ts +200 -0
  182. package/src/mcp/oauth-provider.ts +154 -0
  183. package/src/patch/index.ts +622 -0
  184. package/src/permission/arity.ts +163 -0
  185. package/src/permission/index.ts +210 -0
  186. package/src/permission/next.ts +269 -0
  187. package/src/plugin/codex.ts +493 -0
  188. package/src/plugin/copilot.ts +269 -0
  189. package/src/plugin/index.ts +135 -0
  190. package/src/project/bootstrap.ts +31 -0
  191. package/src/project/instance.ts +91 -0
  192. package/src/project/project.ts +320 -0
  193. package/src/project/state.ts +66 -0
  194. package/src/project/vcs.ts +76 -0
  195. package/src/provider/auth.ts +147 -0
  196. package/src/provider/models-macro.ts +11 -0
  197. package/src/provider/models.ts +112 -0
  198. package/src/provider/provider.ts +1220 -0
  199. package/src/provider/sdk/openai-compatible/src/README.md +5 -0
  200. package/src/provider/sdk/openai-compatible/src/index.ts +2 -0
  201. package/src/provider/sdk/openai-compatible/src/openai-compatible-provider.ts +100 -0
  202. package/src/provider/sdk/openai-compatible/src/responses/convert-to-openai-responses-input.ts +303 -0
  203. package/src/provider/sdk/openai-compatible/src/responses/map-openai-responses-finish-reason.ts +22 -0
  204. package/src/provider/sdk/openai-compatible/src/responses/openai-config.ts +18 -0
  205. package/src/provider/sdk/openai-compatible/src/responses/openai-error.ts +22 -0
  206. package/src/provider/sdk/openai-compatible/src/responses/openai-responses-api-types.ts +207 -0
  207. package/src/provider/sdk/openai-compatible/src/responses/openai-responses-language-model.ts +1713 -0
  208. package/src/provider/sdk/openai-compatible/src/responses/openai-responses-prepare-tools.ts +177 -0
  209. package/src/provider/sdk/openai-compatible/src/responses/openai-responses-settings.ts +1 -0
  210. package/src/provider/sdk/openai-compatible/src/responses/tool/code-interpreter.ts +88 -0
  211. package/src/provider/sdk/openai-compatible/src/responses/tool/file-search.ts +128 -0
  212. package/src/provider/sdk/openai-compatible/src/responses/tool/image-generation.ts +115 -0
  213. package/src/provider/sdk/openai-compatible/src/responses/tool/local-shell.ts +65 -0
  214. package/src/provider/sdk/openai-compatible/src/responses/tool/web-search-preview.ts +104 -0
  215. package/src/provider/sdk/openai-compatible/src/responses/tool/web-search.ts +103 -0
  216. package/src/provider/transform.ts +705 -0
  217. package/src/pty/index.ts +229 -0
  218. package/src/question/index.ts +171 -0
  219. package/src/server/error.ts +36 -0
  220. package/src/server/mdns.ts +59 -0
  221. package/src/server/routes/config.ts +92 -0
  222. package/src/server/routes/experimental.ts +157 -0
  223. package/src/server/routes/file.ts +197 -0
  224. package/src/server/routes/global.ts +135 -0
  225. package/src/server/routes/mcp.ts +225 -0
  226. package/src/server/routes/permission.ts +68 -0
  227. package/src/server/routes/project.ts +82 -0
  228. package/src/server/routes/provider.ts +165 -0
  229. package/src/server/routes/pty.ts +169 -0
  230. package/src/server/routes/question.ts +98 -0
  231. package/src/server/routes/session.ts +935 -0
  232. package/src/server/routes/tui.ts +377 -0
  233. package/src/server/server.ts +578 -0
  234. package/src/session/compaction.ts +225 -0
  235. package/src/session/index.ts +488 -0
  236. package/src/session/llm.ts +279 -0
  237. package/src/session/message-v2.ts +702 -0
  238. package/src/session/message.ts +189 -0
  239. package/src/session/processor.ts +406 -0
  240. package/src/session/prompt/anthropic-20250930.txt +166 -0
  241. package/src/session/prompt/anthropic.txt +105 -0
  242. package/src/session/prompt/anthropic_spoof.txt +1 -0
  243. package/src/session/prompt/beast.txt +147 -0
  244. package/src/session/prompt/build-switch.txt +5 -0
  245. package/src/session/prompt/codex.txt +72 -0
  246. package/src/session/prompt/codex_header.txt +72 -0
  247. package/src/session/prompt/copilot-gpt-5.txt +143 -0
  248. package/src/session/prompt/gemini.txt +155 -0
  249. package/src/session/prompt/max-steps.txt +16 -0
  250. package/src/session/prompt/plan-reminder-anthropic.txt +67 -0
  251. package/src/session/prompt/plan.txt +26 -0
  252. package/src/session/prompt/qwen.txt +109 -0
  253. package/src/session/prompt.ts +1792 -0
  254. package/src/session/retry.ts +90 -0
  255. package/src/session/revert.ts +108 -0
  256. package/src/session/status.ts +76 -0
  257. package/src/session/summary.ts +150 -0
  258. package/src/session/system.ts +138 -0
  259. package/src/session/todo.ts +37 -0
  260. package/src/share/share-next.ts +194 -0
  261. package/src/share/share.ts +87 -0
  262. package/src/shell/shell.ts +67 -0
  263. package/src/skill/index.ts +1 -0
  264. package/src/skill/skill.ts +136 -0
  265. package/src/snapshot/index.ts +199 -0
  266. package/src/storage/storage.ts +227 -0
  267. package/src/tool/bash.ts +258 -0
  268. package/src/tool/bash.txt +115 -0
  269. package/src/tool/batch.ts +175 -0
  270. package/src/tool/batch.txt +24 -0
  271. package/src/tool/codesearch.ts +132 -0
  272. package/src/tool/codesearch.txt +12 -0
  273. package/src/tool/edit.ts +645 -0
  274. package/src/tool/edit.txt +10 -0
  275. package/src/tool/external-directory.ts +32 -0
  276. package/src/tool/glob.ts +77 -0
  277. package/src/tool/glob.txt +6 -0
  278. package/src/tool/grep.ts +154 -0
  279. package/src/tool/grep.txt +8 -0
  280. package/src/tool/invalid.ts +17 -0
  281. package/src/tool/ls.ts +121 -0
  282. package/src/tool/ls.txt +1 -0
  283. package/src/tool/lsp.ts +96 -0
  284. package/src/tool/lsp.txt +19 -0
  285. package/src/tool/multiedit.ts +46 -0
  286. package/src/tool/multiedit.txt +41 -0
  287. package/src/tool/patch.ts +201 -0
  288. package/src/tool/patch.txt +1 -0
  289. package/src/tool/plan-enter.txt +14 -0
  290. package/src/tool/plan-exit.txt +13 -0
  291. package/src/tool/plan.ts +130 -0
  292. package/src/tool/question.ts +33 -0
  293. package/src/tool/question.txt +10 -0
  294. package/src/tool/read.ts +200 -0
  295. package/src/tool/read.txt +12 -0
  296. package/src/tool/registry.ts +143 -0
  297. package/src/tool/skill.ts +75 -0
  298. package/src/tool/task.ts +188 -0
  299. package/src/tool/task.txt +60 -0
  300. package/src/tool/todo.ts +53 -0
  301. package/src/tool/todoread.txt +14 -0
  302. package/src/tool/todowrite.txt +167 -0
  303. package/src/tool/tool.ts +88 -0
  304. package/src/tool/truncation.ts +99 -0
  305. package/src/tool/webfetch.ts +182 -0
  306. package/src/tool/webfetch.txt +13 -0
  307. package/src/tool/websearch.ts +150 -0
  308. package/src/tool/websearch.txt +14 -0
  309. package/src/tool/write.ts +80 -0
  310. package/src/tool/write.txt +8 -0
  311. package/src/util/archive.ts +16 -0
  312. package/src/util/color.ts +19 -0
  313. package/src/util/context.ts +25 -0
  314. package/src/util/defer.ts +12 -0
  315. package/src/util/eventloop.ts +20 -0
  316. package/src/util/filesystem.ts +93 -0
  317. package/src/util/fn.ts +11 -0
  318. package/src/util/format.ts +20 -0
  319. package/src/util/iife.ts +3 -0
  320. package/src/util/keybind.ts +103 -0
  321. package/src/util/lazy.ts +18 -0
  322. package/src/util/locale.ts +81 -0
  323. package/src/util/lock.ts +98 -0
  324. package/src/util/log.ts +180 -0
  325. package/src/util/queue.ts +32 -0
  326. package/src/util/rpc.ts +66 -0
  327. package/src/util/scrap.ts +10 -0
  328. package/src/util/signal.ts +12 -0
  329. package/src/util/timeout.ts +14 -0
  330. package/src/util/token.ts +7 -0
  331. package/src/util/wildcard.ts +56 -0
  332. package/src/worktree/index.ts +217 -0
  333. package/sst-env.d.ts +9 -0
  334. package/test/agent/agent.test.ts +638 -0
  335. package/test/bun.test.ts +53 -0
  336. package/test/cli/github-action.test.ts +129 -0
  337. package/test/cli/github-remote.test.ts +80 -0
  338. package/test/cli/tui/transcript.test.ts +297 -0
  339. package/test/config/agent-color.test.ts +66 -0
  340. package/test/config/config.test.ts +1376 -0
  341. package/test/config/fixtures/empty-frontmatter.md +4 -0
  342. package/test/config/fixtures/frontmatter.md +28 -0
  343. package/test/config/fixtures/no-frontmatter.md +1 -0
  344. package/test/config/markdown.test.ts +192 -0
  345. package/test/file/ignore.test.ts +10 -0
  346. package/test/file/path-traversal.test.ts +198 -0
  347. package/test/fixture/fixture.ts +45 -0
  348. package/test/fixture/lsp/fake-lsp-server.js +77 -0
  349. package/test/ide/ide.test.ts +82 -0
  350. package/test/keybind.test.ts +421 -0
  351. package/test/lsp/client.test.ts +95 -0
  352. package/test/mcp/headers.test.ts +153 -0
  353. package/test/mcp/oauth-browser.test.ts +261 -0
  354. package/test/patch/patch.test.ts +348 -0
  355. package/test/permission/arity.test.ts +33 -0
  356. package/test/permission/next.test.ts +652 -0
  357. package/test/permission-task.test.ts +319 -0
  358. package/test/plugin/codex.test.ts +123 -0
  359. package/test/preload.ts +65 -0
  360. package/test/project/project.test.ts +120 -0
  361. package/test/provider/amazon-bedrock.test.ts +268 -0
  362. package/test/provider/gitlab-duo.test.ts +286 -0
  363. package/test/provider/provider.test.ts +2149 -0
  364. package/test/provider/transform.test.ts +1566 -0
  365. package/test/question/question.test.ts +300 -0
  366. package/test/server/session-list.test.ts +39 -0
  367. package/test/server/session-select.test.ts +78 -0
  368. package/test/session/compaction.test.ts +293 -0
  369. package/test/session/llm.test.ts +90 -0
  370. package/test/session/message-v2.test.ts +662 -0
  371. package/test/session/retry.test.ts +131 -0
  372. package/test/session/revert-compact.test.ts +285 -0
  373. package/test/session/session.test.ts +71 -0
  374. package/test/skill/skill.test.ts +185 -0
  375. package/test/snapshot/snapshot.test.ts +939 -0
  376. package/test/tool/__snapshots__/tool.test.ts.snap +9 -0
  377. package/test/tool/bash.test.ts +320 -0
  378. package/test/tool/external-directory.test.ts +126 -0
  379. package/test/tool/fixtures/large-image.png +0 -0
  380. package/test/tool/fixtures/models-api.json +33453 -0
  381. package/test/tool/grep.test.ts +109 -0
  382. package/test/tool/patch.test.ts +261 -0
  383. package/test/tool/read.test.ts +303 -0
  384. package/test/tool/registry.test.ts +76 -0
  385. package/test/tool/truncation.test.ts +159 -0
  386. package/test/util/filesystem.test.ts +39 -0
  387. package/test/util/format.test.ts +59 -0
  388. package/test/util/iife.test.ts +36 -0
  389. package/test/util/lazy.test.ts +50 -0
  390. package/test/util/lock.test.ts +72 -0
  391. package/test/util/timeout.test.ts +21 -0
  392. package/test/util/wildcard.test.ts +75 -0
  393. package/tsconfig.json +16 -0
@@ -0,0 +1,1376 @@
1
+ import { test, expect, describe, mock } from "bun:test"
2
+ import { Config } from "../../src/config/config"
3
+ import { Instance } from "../../src/project/instance"
4
+ import { Auth } from "../../src/auth"
5
+ import { tmpdir } from "../fixture/fixture"
6
+ import path from "path"
7
+ import fs from "fs/promises"
8
+ import { pathToFileURL } from "url"
9
+
10
+ test("loads config with defaults when no files exist", async () => {
11
+ await using tmp = await tmpdir()
12
+ await Instance.provide({
13
+ directory: tmp.path,
14
+ fn: async () => {
15
+ const config = await Config.get()
16
+ expect(config.username).toBeDefined()
17
+ },
18
+ })
19
+ })
20
+
21
+ test("loads JSON config file", async () => {
22
+ await using tmp = await tmpdir({
23
+ init: async (dir) => {
24
+ await Bun.write(
25
+ path.join(dir, "opencode.json"),
26
+ JSON.stringify({
27
+ $schema: "https://opencode.ai/config.json",
28
+ model: "test/model",
29
+ username: "testuser",
30
+ }),
31
+ )
32
+ },
33
+ })
34
+ await Instance.provide({
35
+ directory: tmp.path,
36
+ fn: async () => {
37
+ const config = await Config.get()
38
+ expect(config.model).toBe("test/model")
39
+ expect(config.username).toBe("testuser")
40
+ },
41
+ })
42
+ })
43
+
44
+ test("loads JSONC config file", async () => {
45
+ await using tmp = await tmpdir({
46
+ init: async (dir) => {
47
+ await Bun.write(
48
+ path.join(dir, "opencode.jsonc"),
49
+ `{
50
+ // This is a comment
51
+ "$schema": "https://opencode.ai/config.json",
52
+ "model": "test/model",
53
+ "username": "testuser"
54
+ }`,
55
+ )
56
+ },
57
+ })
58
+ await Instance.provide({
59
+ directory: tmp.path,
60
+ fn: async () => {
61
+ const config = await Config.get()
62
+ expect(config.model).toBe("test/model")
63
+ expect(config.username).toBe("testuser")
64
+ },
65
+ })
66
+ })
67
+
68
+ test("merges multiple config files with correct precedence", async () => {
69
+ await using tmp = await tmpdir({
70
+ init: async (dir) => {
71
+ await Bun.write(
72
+ path.join(dir, "opencode.jsonc"),
73
+ JSON.stringify({
74
+ $schema: "https://opencode.ai/config.json",
75
+ model: "base",
76
+ username: "base",
77
+ }),
78
+ )
79
+ await Bun.write(
80
+ path.join(dir, "opencode.json"),
81
+ JSON.stringify({
82
+ $schema: "https://opencode.ai/config.json",
83
+ model: "override",
84
+ }),
85
+ )
86
+ },
87
+ })
88
+ await Instance.provide({
89
+ directory: tmp.path,
90
+ fn: async () => {
91
+ const config = await Config.get()
92
+ expect(config.model).toBe("override")
93
+ expect(config.username).toBe("base")
94
+ },
95
+ })
96
+ })
97
+
98
+ test("handles environment variable substitution", async () => {
99
+ const originalEnv = process.env["TEST_VAR"]
100
+ process.env["TEST_VAR"] = "test_theme"
101
+
102
+ try {
103
+ await using tmp = await tmpdir({
104
+ init: async (dir) => {
105
+ await Bun.write(
106
+ path.join(dir, "opencode.json"),
107
+ JSON.stringify({
108
+ $schema: "https://opencode.ai/config.json",
109
+ theme: "{env:TEST_VAR}",
110
+ }),
111
+ )
112
+ },
113
+ })
114
+ await Instance.provide({
115
+ directory: tmp.path,
116
+ fn: async () => {
117
+ const config = await Config.get()
118
+ expect(config.theme).toBe("test_theme")
119
+ },
120
+ })
121
+ } finally {
122
+ if (originalEnv !== undefined) {
123
+ process.env["TEST_VAR"] = originalEnv
124
+ } else {
125
+ delete process.env["TEST_VAR"]
126
+ }
127
+ }
128
+ })
129
+
130
+ test("handles file inclusion substitution", async () => {
131
+ await using tmp = await tmpdir({
132
+ init: async (dir) => {
133
+ await Bun.write(path.join(dir, "included.txt"), "test_theme")
134
+ await Bun.write(
135
+ path.join(dir, "opencode.json"),
136
+ JSON.stringify({
137
+ $schema: "https://opencode.ai/config.json",
138
+ theme: "{file:included.txt}",
139
+ }),
140
+ )
141
+ },
142
+ })
143
+ await Instance.provide({
144
+ directory: tmp.path,
145
+ fn: async () => {
146
+ const config = await Config.get()
147
+ expect(config.theme).toBe("test_theme")
148
+ },
149
+ })
150
+ })
151
+
152
+ test("validates config schema and throws on invalid fields", async () => {
153
+ await using tmp = await tmpdir({
154
+ init: async (dir) => {
155
+ await Bun.write(
156
+ path.join(dir, "opencode.json"),
157
+ JSON.stringify({
158
+ $schema: "https://opencode.ai/config.json",
159
+ invalid_field: "should cause error",
160
+ }),
161
+ )
162
+ },
163
+ })
164
+ await Instance.provide({
165
+ directory: tmp.path,
166
+ fn: async () => {
167
+ // Strict schema should throw an error for invalid fields
168
+ await expect(Config.get()).rejects.toThrow()
169
+ },
170
+ })
171
+ })
172
+
173
+ test("throws error for invalid JSON", async () => {
174
+ await using tmp = await tmpdir({
175
+ init: async (dir) => {
176
+ await Bun.write(path.join(dir, "opencode.json"), "{ invalid json }")
177
+ },
178
+ })
179
+ await Instance.provide({
180
+ directory: tmp.path,
181
+ fn: async () => {
182
+ await expect(Config.get()).rejects.toThrow()
183
+ },
184
+ })
185
+ })
186
+
187
+ test("handles agent configuration", async () => {
188
+ await using tmp = await tmpdir({
189
+ init: async (dir) => {
190
+ await Bun.write(
191
+ path.join(dir, "opencode.json"),
192
+ JSON.stringify({
193
+ $schema: "https://opencode.ai/config.json",
194
+ agent: {
195
+ test_agent: {
196
+ model: "test/model",
197
+ temperature: 0.7,
198
+ description: "test agent",
199
+ },
200
+ },
201
+ }),
202
+ )
203
+ },
204
+ })
205
+ await Instance.provide({
206
+ directory: tmp.path,
207
+ fn: async () => {
208
+ const config = await Config.get()
209
+ expect(config.agent?.["test_agent"]).toEqual(
210
+ expect.objectContaining({
211
+ model: "test/model",
212
+ temperature: 0.7,
213
+ description: "test agent",
214
+ }),
215
+ )
216
+ },
217
+ })
218
+ })
219
+
220
+ test("handles command configuration", async () => {
221
+ await using tmp = await tmpdir({
222
+ init: async (dir) => {
223
+ await Bun.write(
224
+ path.join(dir, "opencode.json"),
225
+ JSON.stringify({
226
+ $schema: "https://opencode.ai/config.json",
227
+ command: {
228
+ test_command: {
229
+ template: "test template",
230
+ description: "test command",
231
+ agent: "test_agent",
232
+ },
233
+ },
234
+ }),
235
+ )
236
+ },
237
+ })
238
+ await Instance.provide({
239
+ directory: tmp.path,
240
+ fn: async () => {
241
+ const config = await Config.get()
242
+ expect(config.command?.["test_command"]).toEqual({
243
+ template: "test template",
244
+ description: "test command",
245
+ agent: "test_agent",
246
+ })
247
+ },
248
+ })
249
+ })
250
+
251
+ test("migrates autoshare to share field", async () => {
252
+ await using tmp = await tmpdir({
253
+ init: async (dir) => {
254
+ await Bun.write(
255
+ path.join(dir, "opencode.json"),
256
+ JSON.stringify({
257
+ $schema: "https://opencode.ai/config.json",
258
+ autoshare: true,
259
+ }),
260
+ )
261
+ },
262
+ })
263
+ await Instance.provide({
264
+ directory: tmp.path,
265
+ fn: async () => {
266
+ const config = await Config.get()
267
+ expect(config.share).toBe("auto")
268
+ expect(config.autoshare).toBe(true)
269
+ },
270
+ })
271
+ })
272
+
273
+ test("migrates mode field to agent field", async () => {
274
+ await using tmp = await tmpdir({
275
+ init: async (dir) => {
276
+ await Bun.write(
277
+ path.join(dir, "opencode.json"),
278
+ JSON.stringify({
279
+ $schema: "https://opencode.ai/config.json",
280
+ mode: {
281
+ test_mode: {
282
+ model: "test/model",
283
+ temperature: 0.5,
284
+ },
285
+ },
286
+ }),
287
+ )
288
+ },
289
+ })
290
+ await Instance.provide({
291
+ directory: tmp.path,
292
+ fn: async () => {
293
+ const config = await Config.get()
294
+ expect(config.agent?.["test_mode"]).toEqual({
295
+ model: "test/model",
296
+ temperature: 0.5,
297
+ mode: "primary",
298
+ options: {},
299
+ permission: {},
300
+ })
301
+ },
302
+ })
303
+ })
304
+
305
+ test("loads config from .opencode directory", async () => {
306
+ await using tmp = await tmpdir({
307
+ init: async (dir) => {
308
+ const opencodeDir = path.join(dir, ".opencode")
309
+ await fs.mkdir(opencodeDir, { recursive: true })
310
+ const agentDir = path.join(opencodeDir, "agent")
311
+ await fs.mkdir(agentDir, { recursive: true })
312
+
313
+ await Bun.write(
314
+ path.join(agentDir, "test.md"),
315
+ `---
316
+ model: test/model
317
+ ---
318
+ Test agent prompt`,
319
+ )
320
+ },
321
+ })
322
+ await Instance.provide({
323
+ directory: tmp.path,
324
+ fn: async () => {
325
+ const config = await Config.get()
326
+ expect(config.agent?.["test"]).toEqual(
327
+ expect.objectContaining({
328
+ name: "test",
329
+ model: "test/model",
330
+ prompt: "Test agent prompt",
331
+ }),
332
+ )
333
+ },
334
+ })
335
+ })
336
+
337
+ test("loads agents from .opencode/agents (plural)", async () => {
338
+ await using tmp = await tmpdir({
339
+ init: async (dir) => {
340
+ const opencodeDir = path.join(dir, ".opencode")
341
+ await fs.mkdir(opencodeDir, { recursive: true })
342
+
343
+ const agentsDir = path.join(opencodeDir, "agents")
344
+ await fs.mkdir(path.join(agentsDir, "nested"), { recursive: true })
345
+
346
+ await Bun.write(
347
+ path.join(agentsDir, "helper.md"),
348
+ `---
349
+ model: test/model
350
+ mode: subagent
351
+ ---
352
+ Helper agent prompt`,
353
+ )
354
+
355
+ await Bun.write(
356
+ path.join(agentsDir, "nested", "child.md"),
357
+ `---
358
+ model: test/model
359
+ mode: subagent
360
+ ---
361
+ Nested agent prompt`,
362
+ )
363
+ },
364
+ })
365
+
366
+ await Instance.provide({
367
+ directory: tmp.path,
368
+ fn: async () => {
369
+ const config = await Config.get()
370
+
371
+ expect(config.agent?.["helper"]).toMatchObject({
372
+ name: "helper",
373
+ model: "test/model",
374
+ mode: "subagent",
375
+ prompt: "Helper agent prompt",
376
+ })
377
+
378
+ expect(config.agent?.["nested/child"]).toMatchObject({
379
+ name: "nested/child",
380
+ model: "test/model",
381
+ mode: "subagent",
382
+ prompt: "Nested agent prompt",
383
+ })
384
+ },
385
+ })
386
+ })
387
+
388
+ test("loads commands from .opencode/command (singular)", async () => {
389
+ await using tmp = await tmpdir({
390
+ init: async (dir) => {
391
+ const opencodeDir = path.join(dir, ".opencode")
392
+ await fs.mkdir(opencodeDir, { recursive: true })
393
+
394
+ const commandDir = path.join(opencodeDir, "command")
395
+ await fs.mkdir(path.join(commandDir, "nested"), { recursive: true })
396
+
397
+ await Bun.write(
398
+ path.join(commandDir, "hello.md"),
399
+ `---
400
+ description: Test command
401
+ ---
402
+ Hello from singular command`,
403
+ )
404
+
405
+ await Bun.write(
406
+ path.join(commandDir, "nested", "child.md"),
407
+ `---
408
+ description: Nested command
409
+ ---
410
+ Nested command template`,
411
+ )
412
+ },
413
+ })
414
+
415
+ await Instance.provide({
416
+ directory: tmp.path,
417
+ fn: async () => {
418
+ const config = await Config.get()
419
+
420
+ expect(config.command?.["hello"]).toEqual({
421
+ description: "Test command",
422
+ template: "Hello from singular command",
423
+ })
424
+
425
+ expect(config.command?.["nested/child"]).toEqual({
426
+ description: "Nested command",
427
+ template: "Nested command template",
428
+ })
429
+ },
430
+ })
431
+ })
432
+
433
+ test("loads commands from .opencode/commands (plural)", async () => {
434
+ await using tmp = await tmpdir({
435
+ init: async (dir) => {
436
+ const opencodeDir = path.join(dir, ".opencode")
437
+ await fs.mkdir(opencodeDir, { recursive: true })
438
+
439
+ const commandsDir = path.join(opencodeDir, "commands")
440
+ await fs.mkdir(path.join(commandsDir, "nested"), { recursive: true })
441
+
442
+ await Bun.write(
443
+ path.join(commandsDir, "hello.md"),
444
+ `---
445
+ description: Test command
446
+ ---
447
+ Hello from plural commands`,
448
+ )
449
+
450
+ await Bun.write(
451
+ path.join(commandsDir, "nested", "child.md"),
452
+ `---
453
+ description: Nested command
454
+ ---
455
+ Nested command template`,
456
+ )
457
+ },
458
+ })
459
+
460
+ await Instance.provide({
461
+ directory: tmp.path,
462
+ fn: async () => {
463
+ const config = await Config.get()
464
+
465
+ expect(config.command?.["hello"]).toEqual({
466
+ description: "Test command",
467
+ template: "Hello from plural commands",
468
+ })
469
+
470
+ expect(config.command?.["nested/child"]).toEqual({
471
+ description: "Nested command",
472
+ template: "Nested command template",
473
+ })
474
+ },
475
+ })
476
+ })
477
+
478
+ test("updates config and writes to file", async () => {
479
+ await using tmp = await tmpdir()
480
+ await Instance.provide({
481
+ directory: tmp.path,
482
+ fn: async () => {
483
+ const newConfig = { model: "updated/model" }
484
+ await Config.update(newConfig as any)
485
+
486
+ const writtenConfig = JSON.parse(await Bun.file(path.join(tmp.path, "config.json")).text())
487
+ expect(writtenConfig.model).toBe("updated/model")
488
+ },
489
+ })
490
+ })
491
+
492
+ test("gets config directories", async () => {
493
+ await using tmp = await tmpdir()
494
+ await Instance.provide({
495
+ directory: tmp.path,
496
+ fn: async () => {
497
+ const dirs = await Config.directories()
498
+ expect(dirs.length).toBeGreaterThanOrEqual(1)
499
+ },
500
+ })
501
+ })
502
+
503
+ test("resolves scoped npm plugins in config", async () => {
504
+ await using tmp = await tmpdir({
505
+ init: async (dir) => {
506
+ const pluginDir = path.join(dir, "node_modules", "@scope", "plugin")
507
+ await fs.mkdir(pluginDir, { recursive: true })
508
+
509
+ await Bun.write(
510
+ path.join(dir, "package.json"),
511
+ JSON.stringify({ name: "config-fixture", version: "1.0.0", type: "module" }, null, 2),
512
+ )
513
+
514
+ await Bun.write(
515
+ path.join(pluginDir, "package.json"),
516
+ JSON.stringify(
517
+ {
518
+ name: "@scope/plugin",
519
+ version: "1.0.0",
520
+ type: "module",
521
+ main: "./index.js",
522
+ },
523
+ null,
524
+ 2,
525
+ ),
526
+ )
527
+
528
+ await Bun.write(path.join(pluginDir, "index.js"), "export default {}\n")
529
+
530
+ await Bun.write(
531
+ path.join(dir, "opencode.json"),
532
+ JSON.stringify({ $schema: "https://opencode.ai/config.json", plugin: ["@scope/plugin"] }, null, 2),
533
+ )
534
+ },
535
+ })
536
+
537
+ await Instance.provide({
538
+ directory: tmp.path,
539
+ fn: async () => {
540
+ const config = await Config.get()
541
+ const pluginEntries = config.plugin ?? []
542
+
543
+ const baseUrl = pathToFileURL(path.join(tmp.path, "opencode.json")).href
544
+ const expected = import.meta.resolve("@scope/plugin", baseUrl)
545
+
546
+ expect(pluginEntries.includes(expected)).toBe(true)
547
+
548
+ const scopedEntry = pluginEntries.find((entry) => entry === expected)
549
+ expect(scopedEntry).toBeDefined()
550
+ expect(scopedEntry?.includes("/node_modules/@scope/plugin/")).toBe(true)
551
+ },
552
+ })
553
+ })
554
+
555
+ test("merges plugin arrays from global and local configs", async () => {
556
+ await using tmp = await tmpdir({
557
+ init: async (dir) => {
558
+ // Create a nested project structure with local .opencode config
559
+ const projectDir = path.join(dir, "project")
560
+ const opencodeDir = path.join(projectDir, ".opencode")
561
+ await fs.mkdir(opencodeDir, { recursive: true })
562
+
563
+ // Global config with plugins
564
+ await Bun.write(
565
+ path.join(dir, "opencode.json"),
566
+ JSON.stringify({
567
+ $schema: "https://opencode.ai/config.json",
568
+ plugin: ["global-plugin-1", "global-plugin-2"],
569
+ }),
570
+ )
571
+
572
+ // Local .opencode config with different plugins
573
+ await Bun.write(
574
+ path.join(opencodeDir, "opencode.json"),
575
+ JSON.stringify({
576
+ $schema: "https://opencode.ai/config.json",
577
+ plugin: ["local-plugin-1"],
578
+ }),
579
+ )
580
+ },
581
+ })
582
+
583
+ await Instance.provide({
584
+ directory: path.join(tmp.path, "project"),
585
+ fn: async () => {
586
+ const config = await Config.get()
587
+ const plugins = config.plugin ?? []
588
+
589
+ // Should contain both global and local plugins
590
+ expect(plugins.some((p) => p.includes("global-plugin-1"))).toBe(true)
591
+ expect(plugins.some((p) => p.includes("global-plugin-2"))).toBe(true)
592
+ expect(plugins.some((p) => p.includes("local-plugin-1"))).toBe(true)
593
+
594
+ // Should have all 3 plugins (not replaced, but merged)
595
+ const pluginNames = plugins.filter((p) => p.includes("global-plugin") || p.includes("local-plugin"))
596
+ expect(pluginNames.length).toBeGreaterThanOrEqual(3)
597
+ },
598
+ })
599
+ })
600
+
601
+ test("does not error when only custom agent is a subagent", async () => {
602
+ await using tmp = await tmpdir({
603
+ init: async (dir) => {
604
+ const opencodeDir = path.join(dir, ".opencode")
605
+ await fs.mkdir(opencodeDir, { recursive: true })
606
+ const agentDir = path.join(opencodeDir, "agent")
607
+ await fs.mkdir(agentDir, { recursive: true })
608
+
609
+ await Bun.write(
610
+ path.join(agentDir, "helper.md"),
611
+ `---
612
+ model: test/model
613
+ mode: subagent
614
+ ---
615
+ Helper subagent prompt`,
616
+ )
617
+ },
618
+ })
619
+ await Instance.provide({
620
+ directory: tmp.path,
621
+ fn: async () => {
622
+ const config = await Config.get()
623
+ expect(config.agent?.["helper"]).toMatchObject({
624
+ name: "helper",
625
+ model: "test/model",
626
+ mode: "subagent",
627
+ prompt: "Helper subagent prompt",
628
+ })
629
+ },
630
+ })
631
+ })
632
+
633
+ test("merges instructions arrays from global and local configs", async () => {
634
+ await using tmp = await tmpdir({
635
+ init: async (dir) => {
636
+ const projectDir = path.join(dir, "project")
637
+ const opencodeDir = path.join(projectDir, ".opencode")
638
+ await fs.mkdir(opencodeDir, { recursive: true })
639
+
640
+ await Bun.write(
641
+ path.join(dir, "opencode.json"),
642
+ JSON.stringify({
643
+ $schema: "https://opencode.ai/config.json",
644
+ instructions: ["global-instructions.md", "shared-rules.md"],
645
+ }),
646
+ )
647
+
648
+ await Bun.write(
649
+ path.join(opencodeDir, "opencode.json"),
650
+ JSON.stringify({
651
+ $schema: "https://opencode.ai/config.json",
652
+ instructions: ["local-instructions.md"],
653
+ }),
654
+ )
655
+ },
656
+ })
657
+
658
+ await Instance.provide({
659
+ directory: path.join(tmp.path, "project"),
660
+ fn: async () => {
661
+ const config = await Config.get()
662
+ const instructions = config.instructions ?? []
663
+
664
+ expect(instructions).toContain("global-instructions.md")
665
+ expect(instructions).toContain("shared-rules.md")
666
+ expect(instructions).toContain("local-instructions.md")
667
+ expect(instructions.length).toBe(3)
668
+ },
669
+ })
670
+ })
671
+
672
+ test("deduplicates duplicate instructions from global and local configs", async () => {
673
+ await using tmp = await tmpdir({
674
+ init: async (dir) => {
675
+ const projectDir = path.join(dir, "project")
676
+ const opencodeDir = path.join(projectDir, ".opencode")
677
+ await fs.mkdir(opencodeDir, { recursive: true })
678
+
679
+ await Bun.write(
680
+ path.join(dir, "opencode.json"),
681
+ JSON.stringify({
682
+ $schema: "https://opencode.ai/config.json",
683
+ instructions: ["duplicate.md", "global-only.md"],
684
+ }),
685
+ )
686
+
687
+ await Bun.write(
688
+ path.join(opencodeDir, "opencode.json"),
689
+ JSON.stringify({
690
+ $schema: "https://opencode.ai/config.json",
691
+ instructions: ["duplicate.md", "local-only.md"],
692
+ }),
693
+ )
694
+ },
695
+ })
696
+
697
+ await Instance.provide({
698
+ directory: path.join(tmp.path, "project"),
699
+ fn: async () => {
700
+ const config = await Config.get()
701
+ const instructions = config.instructions ?? []
702
+
703
+ expect(instructions).toContain("global-only.md")
704
+ expect(instructions).toContain("local-only.md")
705
+ expect(instructions).toContain("duplicate.md")
706
+
707
+ const duplicates = instructions.filter((i) => i === "duplicate.md")
708
+ expect(duplicates.length).toBe(1)
709
+ expect(instructions.length).toBe(3)
710
+ },
711
+ })
712
+ })
713
+
714
+ test("deduplicates duplicate plugins from global and local configs", async () => {
715
+ await using tmp = await tmpdir({
716
+ init: async (dir) => {
717
+ // Create a nested project structure with local .opencode config
718
+ const projectDir = path.join(dir, "project")
719
+ const opencodeDir = path.join(projectDir, ".opencode")
720
+ await fs.mkdir(opencodeDir, { recursive: true })
721
+
722
+ // Global config with plugins
723
+ await Bun.write(
724
+ path.join(dir, "opencode.json"),
725
+ JSON.stringify({
726
+ $schema: "https://opencode.ai/config.json",
727
+ plugin: ["duplicate-plugin", "global-plugin-1"],
728
+ }),
729
+ )
730
+
731
+ // Local .opencode config with some overlapping plugins
732
+ await Bun.write(
733
+ path.join(opencodeDir, "opencode.json"),
734
+ JSON.stringify({
735
+ $schema: "https://opencode.ai/config.json",
736
+ plugin: ["duplicate-plugin", "local-plugin-1"],
737
+ }),
738
+ )
739
+ },
740
+ })
741
+
742
+ await Instance.provide({
743
+ directory: path.join(tmp.path, "project"),
744
+ fn: async () => {
745
+ const config = await Config.get()
746
+ const plugins = config.plugin ?? []
747
+
748
+ // Should contain all unique plugins
749
+ expect(plugins.some((p) => p.includes("global-plugin-1"))).toBe(true)
750
+ expect(plugins.some((p) => p.includes("local-plugin-1"))).toBe(true)
751
+ expect(plugins.some((p) => p.includes("duplicate-plugin"))).toBe(true)
752
+
753
+ // Should deduplicate the duplicate plugin
754
+ const duplicatePlugins = plugins.filter((p) => p.includes("duplicate-plugin"))
755
+ expect(duplicatePlugins.length).toBe(1)
756
+
757
+ // Should have exactly 3 unique plugins
758
+ const pluginNames = plugins.filter(
759
+ (p) => p.includes("global-plugin") || p.includes("local-plugin") || p.includes("duplicate-plugin"),
760
+ )
761
+ expect(pluginNames.length).toBe(3)
762
+ },
763
+ })
764
+ })
765
+
766
+ // Legacy tools migration tests
767
+
768
+ test("migrates legacy tools config to permissions - allow", async () => {
769
+ await using tmp = await tmpdir({
770
+ init: async (dir) => {
771
+ await Bun.write(
772
+ path.join(dir, "opencode.json"),
773
+ JSON.stringify({
774
+ $schema: "https://opencode.ai/config.json",
775
+ agent: {
776
+ test: {
777
+ tools: {
778
+ bash: true,
779
+ read: true,
780
+ },
781
+ },
782
+ },
783
+ }),
784
+ )
785
+ },
786
+ })
787
+ await Instance.provide({
788
+ directory: tmp.path,
789
+ fn: async () => {
790
+ const config = await Config.get()
791
+ expect(config.agent?.["test"]?.permission).toEqual({
792
+ bash: "allow",
793
+ read: "allow",
794
+ })
795
+ },
796
+ })
797
+ })
798
+
799
+ test("migrates legacy tools config to permissions - deny", async () => {
800
+ await using tmp = await tmpdir({
801
+ init: async (dir) => {
802
+ await Bun.write(
803
+ path.join(dir, "opencode.json"),
804
+ JSON.stringify({
805
+ $schema: "https://opencode.ai/config.json",
806
+ agent: {
807
+ test: {
808
+ tools: {
809
+ bash: false,
810
+ webfetch: false,
811
+ },
812
+ },
813
+ },
814
+ }),
815
+ )
816
+ },
817
+ })
818
+ await Instance.provide({
819
+ directory: tmp.path,
820
+ fn: async () => {
821
+ const config = await Config.get()
822
+ expect(config.agent?.["test"]?.permission).toEqual({
823
+ bash: "deny",
824
+ webfetch: "deny",
825
+ })
826
+ },
827
+ })
828
+ })
829
+
830
+ test("migrates legacy write tool to edit permission", async () => {
831
+ await using tmp = await tmpdir({
832
+ init: async (dir) => {
833
+ await Bun.write(
834
+ path.join(dir, "opencode.json"),
835
+ JSON.stringify({
836
+ $schema: "https://opencode.ai/config.json",
837
+ agent: {
838
+ test: {
839
+ tools: {
840
+ write: true,
841
+ },
842
+ },
843
+ },
844
+ }),
845
+ )
846
+ },
847
+ })
848
+ await Instance.provide({
849
+ directory: tmp.path,
850
+ fn: async () => {
851
+ const config = await Config.get()
852
+ expect(config.agent?.["test"]?.permission).toEqual({
853
+ edit: "allow",
854
+ })
855
+ },
856
+ })
857
+ })
858
+
859
+ test("migrates legacy edit tool to edit permission", async () => {
860
+ await using tmp = await tmpdir({
861
+ init: async (dir) => {
862
+ await Bun.write(
863
+ path.join(dir, "opencode.json"),
864
+ JSON.stringify({
865
+ $schema: "https://opencode.ai/config.json",
866
+ agent: {
867
+ test: {
868
+ tools: {
869
+ edit: false,
870
+ },
871
+ },
872
+ },
873
+ }),
874
+ )
875
+ },
876
+ })
877
+ await Instance.provide({
878
+ directory: tmp.path,
879
+ fn: async () => {
880
+ const config = await Config.get()
881
+ expect(config.agent?.["test"]?.permission).toEqual({
882
+ edit: "deny",
883
+ })
884
+ },
885
+ })
886
+ })
887
+
888
+ test("migrates legacy patch tool to edit permission", async () => {
889
+ await using tmp = await tmpdir({
890
+ init: async (dir) => {
891
+ await Bun.write(
892
+ path.join(dir, "opencode.json"),
893
+ JSON.stringify({
894
+ $schema: "https://opencode.ai/config.json",
895
+ agent: {
896
+ test: {
897
+ tools: {
898
+ patch: true,
899
+ },
900
+ },
901
+ },
902
+ }),
903
+ )
904
+ },
905
+ })
906
+ await Instance.provide({
907
+ directory: tmp.path,
908
+ fn: async () => {
909
+ const config = await Config.get()
910
+ expect(config.agent?.["test"]?.permission).toEqual({
911
+ edit: "allow",
912
+ })
913
+ },
914
+ })
915
+ })
916
+
917
+ test("migrates legacy multiedit tool to edit permission", async () => {
918
+ await using tmp = await tmpdir({
919
+ init: async (dir) => {
920
+ await Bun.write(
921
+ path.join(dir, "opencode.json"),
922
+ JSON.stringify({
923
+ $schema: "https://opencode.ai/config.json",
924
+ agent: {
925
+ test: {
926
+ tools: {
927
+ multiedit: false,
928
+ },
929
+ },
930
+ },
931
+ }),
932
+ )
933
+ },
934
+ })
935
+ await Instance.provide({
936
+ directory: tmp.path,
937
+ fn: async () => {
938
+ const config = await Config.get()
939
+ expect(config.agent?.["test"]?.permission).toEqual({
940
+ edit: "deny",
941
+ })
942
+ },
943
+ })
944
+ })
945
+
946
+ test("migrates mixed legacy tools config", async () => {
947
+ await using tmp = await tmpdir({
948
+ init: async (dir) => {
949
+ await Bun.write(
950
+ path.join(dir, "opencode.json"),
951
+ JSON.stringify({
952
+ $schema: "https://opencode.ai/config.json",
953
+ agent: {
954
+ test: {
955
+ tools: {
956
+ bash: true,
957
+ write: true,
958
+ read: false,
959
+ webfetch: true,
960
+ },
961
+ },
962
+ },
963
+ }),
964
+ )
965
+ },
966
+ })
967
+ await Instance.provide({
968
+ directory: tmp.path,
969
+ fn: async () => {
970
+ const config = await Config.get()
971
+ expect(config.agent?.["test"]?.permission).toEqual({
972
+ bash: "allow",
973
+ edit: "allow",
974
+ read: "deny",
975
+ webfetch: "allow",
976
+ })
977
+ },
978
+ })
979
+ })
980
+
981
+ test("merges legacy tools with existing permission config", async () => {
982
+ await using tmp = await tmpdir({
983
+ init: async (dir) => {
984
+ await Bun.write(
985
+ path.join(dir, "opencode.json"),
986
+ JSON.stringify({
987
+ $schema: "https://opencode.ai/config.json",
988
+ agent: {
989
+ test: {
990
+ permission: {
991
+ glob: "allow",
992
+ },
993
+ tools: {
994
+ bash: true,
995
+ },
996
+ },
997
+ },
998
+ }),
999
+ )
1000
+ },
1001
+ })
1002
+ await Instance.provide({
1003
+ directory: tmp.path,
1004
+ fn: async () => {
1005
+ const config = await Config.get()
1006
+ expect(config.agent?.["test"]?.permission).toEqual({
1007
+ glob: "allow",
1008
+ bash: "allow",
1009
+ })
1010
+ },
1011
+ })
1012
+ })
1013
+
1014
+ test("permission config preserves key order", async () => {
1015
+ await using tmp = await tmpdir({
1016
+ init: async (dir) => {
1017
+ await Bun.write(
1018
+ path.join(dir, "opencode.json"),
1019
+ JSON.stringify({
1020
+ $schema: "https://opencode.ai/config.json",
1021
+ permission: {
1022
+ "*": "deny",
1023
+ edit: "ask",
1024
+ write: "ask",
1025
+ external_directory: "ask",
1026
+ read: "allow",
1027
+ todowrite: "allow",
1028
+ todoread: "allow",
1029
+ "thoughts_*": "allow",
1030
+ "reasoning_model_*": "allow",
1031
+ "tools_*": "allow",
1032
+ "pr_comments_*": "allow",
1033
+ },
1034
+ }),
1035
+ )
1036
+ },
1037
+ })
1038
+ await Instance.provide({
1039
+ directory: tmp.path,
1040
+ fn: async () => {
1041
+ const config = await Config.get()
1042
+ expect(Object.keys(config.permission!)).toEqual([
1043
+ "*",
1044
+ "edit",
1045
+ "write",
1046
+ "external_directory",
1047
+ "read",
1048
+ "todowrite",
1049
+ "todoread",
1050
+ "thoughts_*",
1051
+ "reasoning_model_*",
1052
+ "tools_*",
1053
+ "pr_comments_*",
1054
+ ])
1055
+ },
1056
+ })
1057
+ })
1058
+
1059
+ // MCP config merging tests
1060
+
1061
+ test("project config can override MCP server enabled status", async () => {
1062
+ await using tmp = await tmpdir({
1063
+ init: async (dir) => {
1064
+ // Simulates a base config (like from remote .well-known) with disabled MCP
1065
+ await Bun.write(
1066
+ path.join(dir, "opencode.jsonc"),
1067
+ JSON.stringify({
1068
+ $schema: "https://opencode.ai/config.json",
1069
+ mcp: {
1070
+ jira: {
1071
+ type: "remote",
1072
+ url: "https://jira.example.com/mcp",
1073
+ enabled: false,
1074
+ },
1075
+ wiki: {
1076
+ type: "remote",
1077
+ url: "https://wiki.example.com/mcp",
1078
+ enabled: false,
1079
+ },
1080
+ },
1081
+ }),
1082
+ )
1083
+ // Project config enables just jira
1084
+ await Bun.write(
1085
+ path.join(dir, "opencode.json"),
1086
+ JSON.stringify({
1087
+ $schema: "https://opencode.ai/config.json",
1088
+ mcp: {
1089
+ jira: {
1090
+ type: "remote",
1091
+ url: "https://jira.example.com/mcp",
1092
+ enabled: true,
1093
+ },
1094
+ },
1095
+ }),
1096
+ )
1097
+ },
1098
+ })
1099
+ await Instance.provide({
1100
+ directory: tmp.path,
1101
+ fn: async () => {
1102
+ const config = await Config.get()
1103
+ // jira should be enabled (overridden by project config)
1104
+ expect(config.mcp?.jira).toEqual({
1105
+ type: "remote",
1106
+ url: "https://jira.example.com/mcp",
1107
+ enabled: true,
1108
+ })
1109
+ // wiki should still be disabled (not overridden)
1110
+ expect(config.mcp?.wiki).toEqual({
1111
+ type: "remote",
1112
+ url: "https://wiki.example.com/mcp",
1113
+ enabled: false,
1114
+ })
1115
+ },
1116
+ })
1117
+ })
1118
+
1119
+ test("MCP config deep merges preserving base config properties", async () => {
1120
+ await using tmp = await tmpdir({
1121
+ init: async (dir) => {
1122
+ // Base config with full MCP definition
1123
+ await Bun.write(
1124
+ path.join(dir, "opencode.jsonc"),
1125
+ JSON.stringify({
1126
+ $schema: "https://opencode.ai/config.json",
1127
+ mcp: {
1128
+ myserver: {
1129
+ type: "remote",
1130
+ url: "https://myserver.example.com/mcp",
1131
+ enabled: false,
1132
+ headers: {
1133
+ "X-Custom-Header": "value",
1134
+ },
1135
+ },
1136
+ },
1137
+ }),
1138
+ )
1139
+ // Override just enables it, should preserve other properties
1140
+ await Bun.write(
1141
+ path.join(dir, "opencode.json"),
1142
+ JSON.stringify({
1143
+ $schema: "https://opencode.ai/config.json",
1144
+ mcp: {
1145
+ myserver: {
1146
+ type: "remote",
1147
+ url: "https://myserver.example.com/mcp",
1148
+ enabled: true,
1149
+ },
1150
+ },
1151
+ }),
1152
+ )
1153
+ },
1154
+ })
1155
+ await Instance.provide({
1156
+ directory: tmp.path,
1157
+ fn: async () => {
1158
+ const config = await Config.get()
1159
+ expect(config.mcp?.myserver).toEqual({
1160
+ type: "remote",
1161
+ url: "https://myserver.example.com/mcp",
1162
+ enabled: true,
1163
+ headers: {
1164
+ "X-Custom-Header": "value",
1165
+ },
1166
+ })
1167
+ },
1168
+ })
1169
+ })
1170
+
1171
+ test("local .opencode config can override MCP from project config", async () => {
1172
+ await using tmp = await tmpdir({
1173
+ init: async (dir) => {
1174
+ // Project config with disabled MCP
1175
+ await Bun.write(
1176
+ path.join(dir, "opencode.json"),
1177
+ JSON.stringify({
1178
+ $schema: "https://opencode.ai/config.json",
1179
+ mcp: {
1180
+ docs: {
1181
+ type: "remote",
1182
+ url: "https://docs.example.com/mcp",
1183
+ enabled: false,
1184
+ },
1185
+ },
1186
+ }),
1187
+ )
1188
+ // Local .opencode directory config enables it
1189
+ const opencodeDir = path.join(dir, ".opencode")
1190
+ await fs.mkdir(opencodeDir, { recursive: true })
1191
+ await Bun.write(
1192
+ path.join(opencodeDir, "opencode.json"),
1193
+ JSON.stringify({
1194
+ $schema: "https://opencode.ai/config.json",
1195
+ mcp: {
1196
+ docs: {
1197
+ type: "remote",
1198
+ url: "https://docs.example.com/mcp",
1199
+ enabled: true,
1200
+ },
1201
+ },
1202
+ }),
1203
+ )
1204
+ },
1205
+ })
1206
+ await Instance.provide({
1207
+ directory: tmp.path,
1208
+ fn: async () => {
1209
+ const config = await Config.get()
1210
+ expect(config.mcp?.docs?.enabled).toBe(true)
1211
+ },
1212
+ })
1213
+ })
1214
+
1215
+ test("project config overrides remote well-known config", async () => {
1216
+ const originalFetch = globalThis.fetch
1217
+ let fetchedUrl: string | undefined
1218
+ const mockFetch = mock((url: string | URL | Request) => {
1219
+ const urlStr = url.toString()
1220
+ if (urlStr.includes(".well-known/opencode")) {
1221
+ fetchedUrl = urlStr
1222
+ return Promise.resolve(
1223
+ new Response(
1224
+ JSON.stringify({
1225
+ config: {
1226
+ mcp: {
1227
+ jira: {
1228
+ type: "remote",
1229
+ url: "https://jira.example.com/mcp",
1230
+ enabled: false,
1231
+ },
1232
+ },
1233
+ },
1234
+ }),
1235
+ { status: 200 },
1236
+ ),
1237
+ )
1238
+ }
1239
+ return originalFetch(url)
1240
+ })
1241
+ globalThis.fetch = mockFetch as unknown as typeof fetch
1242
+
1243
+ const originalAuthAll = Auth.all
1244
+ Auth.all = mock(() =>
1245
+ Promise.resolve({
1246
+ "https://example.com": {
1247
+ type: "wellknown" as const,
1248
+ key: "TEST_TOKEN",
1249
+ token: "test-token",
1250
+ },
1251
+ }),
1252
+ )
1253
+
1254
+ try {
1255
+ await using tmp = await tmpdir({
1256
+ git: true,
1257
+ init: async (dir) => {
1258
+ // Project config enables jira (overriding remote default)
1259
+ await Bun.write(
1260
+ path.join(dir, "opencode.json"),
1261
+ JSON.stringify({
1262
+ $schema: "https://opencode.ai/config.json",
1263
+ mcp: {
1264
+ jira: {
1265
+ type: "remote",
1266
+ url: "https://jira.example.com/mcp",
1267
+ enabled: true,
1268
+ },
1269
+ },
1270
+ }),
1271
+ )
1272
+ },
1273
+ })
1274
+ await Instance.provide({
1275
+ directory: tmp.path,
1276
+ fn: async () => {
1277
+ const config = await Config.get()
1278
+ // Verify fetch was called for wellknown config
1279
+ expect(fetchedUrl).toBe("https://example.com/.well-known/opencode")
1280
+ // Project config (enabled: true) should override remote (enabled: false)
1281
+ expect(config.mcp?.jira?.enabled).toBe(true)
1282
+ },
1283
+ })
1284
+ } finally {
1285
+ globalThis.fetch = originalFetch
1286
+ Auth.all = originalAuthAll
1287
+ }
1288
+ })
1289
+
1290
+ describe("getPluginName", () => {
1291
+ test("extracts name from file:// URL", () => {
1292
+ expect(Config.getPluginName("file:///path/to/plugin/foo.js")).toBe("foo")
1293
+ expect(Config.getPluginName("file:///path/to/plugin/bar.ts")).toBe("bar")
1294
+ expect(Config.getPluginName("file:///some/path/my-plugin.js")).toBe("my-plugin")
1295
+ })
1296
+
1297
+ test("extracts name from npm package with version", () => {
1298
+ expect(Config.getPluginName("oh-my-opencode@2.4.3")).toBe("oh-my-opencode")
1299
+ expect(Config.getPluginName("some-plugin@1.0.0")).toBe("some-plugin")
1300
+ expect(Config.getPluginName("plugin@latest")).toBe("plugin")
1301
+ })
1302
+
1303
+ test("extracts name from scoped npm package", () => {
1304
+ expect(Config.getPluginName("@scope/pkg@1.0.0")).toBe("@scope/pkg")
1305
+ expect(Config.getPluginName("@opencode/plugin@2.0.0")).toBe("@opencode/plugin")
1306
+ })
1307
+
1308
+ test("returns full string for package without version", () => {
1309
+ expect(Config.getPluginName("some-plugin")).toBe("some-plugin")
1310
+ expect(Config.getPluginName("@scope/pkg")).toBe("@scope/pkg")
1311
+ })
1312
+ })
1313
+
1314
+ describe("deduplicatePlugins", () => {
1315
+ test("removes duplicates keeping higher priority (later entries)", () => {
1316
+ const plugins = ["global-plugin@1.0.0", "shared-plugin@1.0.0", "local-plugin@2.0.0", "shared-plugin@2.0.0"]
1317
+
1318
+ const result = Config.deduplicatePlugins(plugins)
1319
+
1320
+ expect(result).toContain("global-plugin@1.0.0")
1321
+ expect(result).toContain("local-plugin@2.0.0")
1322
+ expect(result).toContain("shared-plugin@2.0.0")
1323
+ expect(result).not.toContain("shared-plugin@1.0.0")
1324
+ expect(result.length).toBe(3)
1325
+ })
1326
+
1327
+ test("prefers local file over npm package with same name", () => {
1328
+ const plugins = ["oh-my-opencode@2.4.3", "file:///project/.opencode/plugin/oh-my-opencode.js"]
1329
+
1330
+ const result = Config.deduplicatePlugins(plugins)
1331
+
1332
+ expect(result.length).toBe(1)
1333
+ expect(result[0]).toBe("file:///project/.opencode/plugin/oh-my-opencode.js")
1334
+ })
1335
+
1336
+ test("preserves order of remaining plugins", () => {
1337
+ const plugins = ["a-plugin@1.0.0", "b-plugin@1.0.0", "c-plugin@1.0.0"]
1338
+
1339
+ const result = Config.deduplicatePlugins(plugins)
1340
+
1341
+ expect(result).toEqual(["a-plugin@1.0.0", "b-plugin@1.0.0", "c-plugin@1.0.0"])
1342
+ })
1343
+
1344
+ test("local plugin directory overrides global opencode.json plugin", async () => {
1345
+ await using tmp = await tmpdir({
1346
+ init: async (dir) => {
1347
+ const projectDir = path.join(dir, "project")
1348
+ const opencodeDir = path.join(projectDir, ".opencode")
1349
+ const pluginDir = path.join(opencodeDir, "plugin")
1350
+ await fs.mkdir(pluginDir, { recursive: true })
1351
+
1352
+ await Bun.write(
1353
+ path.join(dir, "opencode.json"),
1354
+ JSON.stringify({
1355
+ $schema: "https://opencode.ai/config.json",
1356
+ plugin: ["my-plugin@1.0.0"],
1357
+ }),
1358
+ )
1359
+
1360
+ await Bun.write(path.join(pluginDir, "my-plugin.js"), "export default {}")
1361
+ },
1362
+ })
1363
+
1364
+ await Instance.provide({
1365
+ directory: path.join(tmp.path, "project"),
1366
+ fn: async () => {
1367
+ const config = await Config.get()
1368
+ const plugins = config.plugin ?? []
1369
+
1370
+ const myPlugins = plugins.filter((p) => Config.getPluginName(p) === "my-plugin")
1371
+ expect(myPlugins.length).toBe(1)
1372
+ expect(myPlugins[0].startsWith("file://")).toBe(true)
1373
+ },
1374
+ })
1375
+ })
1376
+ })