alchemylab-opencode 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 (379) hide show
  1. package/LICENSE +21 -0
  2. package/NODE_COMPAT.md +184 -0
  3. package/package.json +112 -0
  4. package/packages/opencode/migration/20260127222353_familiar_lady_ursula/migration.sql +90 -0
  5. package/packages/opencode/migration/20260127222353_familiar_lady_ursula/snapshot.json +796 -0
  6. package/packages/opencode/migration/20260211171708_add_project_commands/migration.sql +1 -0
  7. package/packages/opencode/migration/20260211171708_add_project_commands/snapshot.json +806 -0
  8. package/packages/opencode/migration/20260213144116_wakeful_the_professor/migration.sql +11 -0
  9. package/packages/opencode/migration/20260213144116_wakeful_the_professor/snapshot.json +897 -0
  10. package/packages/opencode/migration/20260225215848_workspace/migration.sql +7 -0
  11. package/packages/opencode/migration/20260225215848_workspace/snapshot.json +959 -0
  12. package/packages/opencode/migration/20260227213759_add_session_workspace_id/migration.sql +2 -0
  13. package/packages/opencode/migration/20260227213759_add_session_workspace_id/snapshot.json +983 -0
  14. package/packages/opencode/migration/20260228203230_blue_harpoon/migration.sql +17 -0
  15. package/packages/opencode/migration/20260228203230_blue_harpoon/snapshot.json +1102 -0
  16. package/packages/opencode/migration/20260303231226_add_workspace_fields/migration.sql +5 -0
  17. package/packages/opencode/migration/20260303231226_add_workspace_fields/snapshot.json +1013 -0
  18. package/packages/opencode/migration/20260309230000_move_org_to_state/migration.sql +3 -0
  19. package/packages/opencode/migration/20260309230000_move_org_to_state/snapshot.json +1156 -0
  20. package/packages/opencode/migration/20260312043431_session_message_cursor/migration.sql +4 -0
  21. package/packages/opencode/migration/20260312043431_session_message_cursor/snapshot.json +1168 -0
  22. package/packages/opencode/migration/20260323234822_events/migration.sql +13 -0
  23. package/packages/opencode/migration/20260323234822_events/snapshot.json +1271 -0
  24. package/packages/opencode/package.json +140 -0
  25. package/packages/opencode/src/account/account.sql.ts +39 -0
  26. package/packages/opencode/src/account/index.ts +463 -0
  27. package/packages/opencode/src/account/repo.ts +163 -0
  28. package/packages/opencode/src/account/schema.ts +91 -0
  29. package/packages/opencode/src/acp/README.md +174 -0
  30. package/packages/opencode/src/acp/agent.ts +1847 -0
  31. package/packages/opencode/src/acp/session.ts +116 -0
  32. package/packages/opencode/src/acp/types.ts +24 -0
  33. package/packages/opencode/src/agent/agent.ts +420 -0
  34. package/packages/opencode/src/agent/generate.txt +75 -0
  35. package/packages/opencode/src/agent/prompt/compaction.txt +15 -0
  36. package/packages/opencode/src/agent/prompt/explore.txt +18 -0
  37. package/packages/opencode/src/agent/prompt/summary.txt +11 -0
  38. package/packages/opencode/src/agent/prompt/title.txt +44 -0
  39. package/packages/opencode/src/auth/index.ts +110 -0
  40. package/packages/opencode/src/bus/bus-event.ts +40 -0
  41. package/packages/opencode/src/bus/global.ts +10 -0
  42. package/packages/opencode/src/bus/index.ts +185 -0
  43. package/packages/opencode/src/cli/bootstrap.ts +17 -0
  44. package/packages/opencode/src/cli/cmd/cmd.ts +7 -0
  45. package/packages/opencode/src/cli/cmd/run.ts +689 -0
  46. package/packages/opencode/src/cli/cmd/serve.ts +24 -0
  47. package/packages/opencode/src/cli/cmd/tui/event.ts +49 -0
  48. package/packages/opencode/src/cli/effect/prompt.ts +25 -0
  49. package/packages/opencode/src/cli/error.ts +46 -0
  50. package/packages/opencode/src/cli/heap.ts +59 -0
  51. package/packages/opencode/src/cli/logo.ts +6 -0
  52. package/packages/opencode/src/cli/network.ts +60 -0
  53. package/packages/opencode/src/cli/ui.ts +133 -0
  54. package/packages/opencode/src/cli/upgrade.ts +31 -0
  55. package/packages/opencode/src/command/index.ts +195 -0
  56. package/packages/opencode/src/command/template/initialize.txt +66 -0
  57. package/packages/opencode/src/command/template/review.txt +101 -0
  58. package/packages/opencode/src/config/config.ts +1599 -0
  59. package/packages/opencode/src/config/console-state.ts +15 -0
  60. package/packages/opencode/src/config/markdown.ts +99 -0
  61. package/packages/opencode/src/config/paths.ts +167 -0
  62. package/packages/opencode/src/config/tui-migrate.ts +155 -0
  63. package/packages/opencode/src/config/tui-schema.ts +37 -0
  64. package/packages/opencode/src/config/tui.ts +179 -0
  65. package/packages/opencode/src/control-plane/adaptors/index.ts +20 -0
  66. package/packages/opencode/src/control-plane/adaptors/worktree.ts +42 -0
  67. package/packages/opencode/src/control-plane/schema.ts +17 -0
  68. package/packages/opencode/src/control-plane/sse.ts +66 -0
  69. package/packages/opencode/src/control-plane/types.ts +32 -0
  70. package/packages/opencode/src/control-plane/workspace.sql.ts +17 -0
  71. package/packages/opencode/src/control-plane/workspace.ts +168 -0
  72. package/packages/opencode/src/effect/cross-spawn-spawner.ts +502 -0
  73. package/packages/opencode/src/effect/instance-ref.ts +6 -0
  74. package/packages/opencode/src/effect/instance-registry.ts +12 -0
  75. package/packages/opencode/src/effect/instance-state.ts +82 -0
  76. package/packages/opencode/src/effect/run-service.ts +33 -0
  77. package/packages/opencode/src/effect/runner.ts +216 -0
  78. package/packages/opencode/src/env/index.ts +28 -0
  79. package/packages/opencode/src/file/ignore.ts +82 -0
  80. package/packages/opencode/src/file/index.ts +686 -0
  81. package/packages/opencode/src/file/protected.ts +59 -0
  82. package/packages/opencode/src/file/ripgrep.ts +376 -0
  83. package/packages/opencode/src/file/time.ts +133 -0
  84. package/packages/opencode/src/file/watcher.ts +173 -0
  85. package/packages/opencode/src/filesystem/index.ts +236 -0
  86. package/packages/opencode/src/flag/flag.ts +156 -0
  87. package/packages/opencode/src/format/formatter.ts +413 -0
  88. package/packages/opencode/src/format/index.ts +203 -0
  89. package/packages/opencode/src/git/index.ts +303 -0
  90. package/packages/opencode/src/global/index.ts +56 -0
  91. package/packages/opencode/src/id/id.ts +85 -0
  92. package/packages/opencode/src/ide/index.ts +74 -0
  93. package/packages/opencode/src/installation/index.ts +355 -0
  94. package/packages/opencode/src/installation/meta.ts +7 -0
  95. package/packages/opencode/src/lsp/client.ts +252 -0
  96. package/packages/opencode/src/lsp/index.ts +558 -0
  97. package/packages/opencode/src/lsp/language.ts +120 -0
  98. package/packages/opencode/src/lsp/launch.ts +21 -0
  99. package/packages/opencode/src/lsp/server.ts +1968 -0
  100. package/packages/opencode/src/mcp/auth.ts +173 -0
  101. package/packages/opencode/src/mcp/index.ts +931 -0
  102. package/packages/opencode/src/mcp/oauth-callback.ts +216 -0
  103. package/packages/opencode/src/mcp/oauth-provider.ts +185 -0
  104. package/packages/opencode/src/node-loader.mjs +104 -0
  105. package/packages/opencode/src/node-preload.mjs +39 -0
  106. package/packages/opencode/src/node.ts +1 -0
  107. package/packages/opencode/src/npm/index.ts +188 -0
  108. package/packages/opencode/src/patch/index.ts +680 -0
  109. package/packages/opencode/src/permission/arity.ts +163 -0
  110. package/packages/opencode/src/permission/evaluate.ts +15 -0
  111. package/packages/opencode/src/permission/index.ts +325 -0
  112. package/packages/opencode/src/permission/schema.ts +17 -0
  113. package/packages/opencode/src/plugin/cloudflare.ts +67 -0
  114. package/packages/opencode/src/plugin/codex.ts +608 -0
  115. package/packages/opencode/src/plugin/github-copilot/copilot.ts +361 -0
  116. package/packages/opencode/src/plugin/github-copilot/models.ts +144 -0
  117. package/packages/opencode/src/plugin/index.ts +299 -0
  118. package/packages/opencode/src/plugin/install.ts +439 -0
  119. package/packages/opencode/src/plugin/loader.ts +174 -0
  120. package/packages/opencode/src/plugin/meta.ts +188 -0
  121. package/packages/opencode/src/plugin/shared.ts +323 -0
  122. package/packages/opencode/src/project/bootstrap.ts +31 -0
  123. package/packages/opencode/src/project/instance.ts +175 -0
  124. package/packages/opencode/src/project/project.sql.ts +16 -0
  125. package/packages/opencode/src/project/project.ts +519 -0
  126. package/packages/opencode/src/project/schema.ts +16 -0
  127. package/packages/opencode/src/project/state.ts +70 -0
  128. package/packages/opencode/src/project/vcs.ts +240 -0
  129. package/packages/opencode/src/provider/auth.ts +253 -0
  130. package/packages/opencode/src/provider/error.ts +197 -0
  131. package/packages/opencode/src/provider/models.ts +162 -0
  132. package/packages/opencode/src/provider/provider.ts +1709 -0
  133. package/packages/opencode/src/provider/schema.ts +38 -0
  134. package/packages/opencode/src/provider/sdk/copilot/README.md +5 -0
  135. package/packages/opencode/src/provider/sdk/copilot/chat/convert-to-openai-compatible-chat-messages.ts +170 -0
  136. package/packages/opencode/src/provider/sdk/copilot/chat/get-response-metadata.ts +15 -0
  137. package/packages/opencode/src/provider/sdk/copilot/chat/map-openai-compatible-finish-reason.ts +19 -0
  138. package/packages/opencode/src/provider/sdk/copilot/chat/openai-compatible-api-types.ts +64 -0
  139. package/packages/opencode/src/provider/sdk/copilot/chat/openai-compatible-chat-language-model.ts +815 -0
  140. package/packages/opencode/src/provider/sdk/copilot/chat/openai-compatible-chat-options.ts +28 -0
  141. package/packages/opencode/src/provider/sdk/copilot/chat/openai-compatible-metadata-extractor.ts +44 -0
  142. package/packages/opencode/src/provider/sdk/copilot/chat/openai-compatible-prepare-tools.ts +83 -0
  143. package/packages/opencode/src/provider/sdk/copilot/copilot-provider.ts +100 -0
  144. package/packages/opencode/src/provider/sdk/copilot/index.ts +2 -0
  145. package/packages/opencode/src/provider/sdk/copilot/openai-compatible-error.ts +27 -0
  146. package/packages/opencode/src/provider/sdk/copilot/responses/convert-to-openai-responses-input.ts +335 -0
  147. package/packages/opencode/src/provider/sdk/copilot/responses/map-openai-responses-finish-reason.ts +22 -0
  148. package/packages/opencode/src/provider/sdk/copilot/responses/openai-config.ts +18 -0
  149. package/packages/opencode/src/provider/sdk/copilot/responses/openai-error.ts +22 -0
  150. package/packages/opencode/src/provider/sdk/copilot/responses/openai-responses-api-types.ts +214 -0
  151. package/packages/opencode/src/provider/sdk/copilot/responses/openai-responses-language-model.ts +1769 -0
  152. package/packages/opencode/src/provider/sdk/copilot/responses/openai-responses-prepare-tools.ts +173 -0
  153. package/packages/opencode/src/provider/sdk/copilot/responses/openai-responses-settings.ts +1 -0
  154. package/packages/opencode/src/provider/sdk/copilot/responses/tool/code-interpreter.ts +87 -0
  155. package/packages/opencode/src/provider/sdk/copilot/responses/tool/file-search.ts +127 -0
  156. package/packages/opencode/src/provider/sdk/copilot/responses/tool/image-generation.ts +114 -0
  157. package/packages/opencode/src/provider/sdk/copilot/responses/tool/local-shell.ts +64 -0
  158. package/packages/opencode/src/provider/sdk/copilot/responses/tool/web-search-preview.ts +103 -0
  159. package/packages/opencode/src/provider/sdk/copilot/responses/tool/web-search.ts +102 -0
  160. package/packages/opencode/src/provider/transform.ts +1050 -0
  161. package/packages/opencode/src/pty/index.ts +397 -0
  162. package/packages/opencode/src/pty/pty.node.ts +27 -0
  163. package/packages/opencode/src/pty/pty.ts +25 -0
  164. package/packages/opencode/src/pty/schema.ts +17 -0
  165. package/packages/opencode/src/question/index.ts +224 -0
  166. package/packages/opencode/src/question/schema.ts +17 -0
  167. package/packages/opencode/src/run.ts +28 -0
  168. package/packages/opencode/src/serve.ts +45 -0
  169. package/packages/opencode/src/server/error.ts +36 -0
  170. package/packages/opencode/src/server/event.ts +7 -0
  171. package/packages/opencode/src/server/instance.ts +324 -0
  172. package/packages/opencode/src/server/mdns.ts +60 -0
  173. package/packages/opencode/src/server/middleware.ts +33 -0
  174. package/packages/opencode/src/server/projectors.ts +28 -0
  175. package/packages/opencode/src/server/proxy.ts +131 -0
  176. package/packages/opencode/src/server/router.ts +105 -0
  177. package/packages/opencode/src/server/routes/config.ts +92 -0
  178. package/packages/opencode/src/server/routes/event.ts +83 -0
  179. package/packages/opencode/src/server/routes/experimental.ts +374 -0
  180. package/packages/opencode/src/server/routes/file.ts +197 -0
  181. package/packages/opencode/src/server/routes/global.ts +312 -0
  182. package/packages/opencode/src/server/routes/mcp.ts +225 -0
  183. package/packages/opencode/src/server/routes/permission.ts +69 -0
  184. package/packages/opencode/src/server/routes/project.ts +118 -0
  185. package/packages/opencode/src/server/routes/provider.ts +171 -0
  186. package/packages/opencode/src/server/routes/pty.ts +210 -0
  187. package/packages/opencode/src/server/routes/question.ts +99 -0
  188. package/packages/opencode/src/server/routes/session.ts +1030 -0
  189. package/packages/opencode/src/server/routes/tui.ts +379 -0
  190. package/packages/opencode/src/server/routes/workspace.ts +94 -0
  191. package/packages/opencode/src/server/server.ts +353 -0
  192. package/packages/opencode/src/session/compaction.ts +428 -0
  193. package/packages/opencode/src/session/index.ts +887 -0
  194. package/packages/opencode/src/session/instruction.ts +258 -0
  195. package/packages/opencode/src/session/llm.ts +354 -0
  196. package/packages/opencode/src/session/message-v2.ts +1031 -0
  197. package/packages/opencode/src/session/message.ts +191 -0
  198. package/packages/opencode/src/session/overflow.ts +22 -0
  199. package/packages/opencode/src/session/processor.ts +523 -0
  200. package/packages/opencode/src/session/projectors.ts +135 -0
  201. package/packages/opencode/src/session/prompt/anthropic.txt +105 -0
  202. package/packages/opencode/src/session/prompt/beast.txt +147 -0
  203. package/packages/opencode/src/session/prompt/build-switch.txt +5 -0
  204. package/packages/opencode/src/session/prompt/codex.txt +79 -0
  205. package/packages/opencode/src/session/prompt/copilot-gpt-5.txt +143 -0
  206. package/packages/opencode/src/session/prompt/default.txt +105 -0
  207. package/packages/opencode/src/session/prompt/gemini.txt +155 -0
  208. package/packages/opencode/src/session/prompt/gpt.txt +107 -0
  209. package/packages/opencode/src/session/prompt/kimi.txt +95 -0
  210. package/packages/opencode/src/session/prompt/max-steps.txt +16 -0
  211. package/packages/opencode/src/session/prompt/plan-reminder-anthropic.txt +67 -0
  212. package/packages/opencode/src/session/prompt/plan.txt +26 -0
  213. package/packages/opencode/src/session/prompt/trinity.txt +97 -0
  214. package/packages/opencode/src/session/prompt.ts +1911 -0
  215. package/packages/opencode/src/session/retry.ts +106 -0
  216. package/packages/opencode/src/session/revert.ts +176 -0
  217. package/packages/opencode/src/session/schema.ts +38 -0
  218. package/packages/opencode/src/session/session.sql.ts +103 -0
  219. package/packages/opencode/src/session/status.ts +102 -0
  220. package/packages/opencode/src/session/summary.ts +177 -0
  221. package/packages/opencode/src/session/system.ts +76 -0
  222. package/packages/opencode/src/session/todo.ts +95 -0
  223. package/packages/opencode/src/share/share-next.ts +369 -0
  224. package/packages/opencode/src/share/share.sql.ts +13 -0
  225. package/packages/opencode/src/shell/shell.ts +110 -0
  226. package/packages/opencode/src/skill/discovery.ts +116 -0
  227. package/packages/opencode/src/skill/index.ts +277 -0
  228. package/packages/opencode/src/snapshot/index.ts +726 -0
  229. package/packages/opencode/src/sql.d.ts +4 -0
  230. package/packages/opencode/src/storage/db.node.ts +8 -0
  231. package/packages/opencode/src/storage/db.ts +177 -0
  232. package/packages/opencode/src/storage/json-migration.ts +425 -0
  233. package/packages/opencode/src/storage/schema.sql.ts +10 -0
  234. package/packages/opencode/src/storage/schema.ts +5 -0
  235. package/packages/opencode/src/storage/storage.ts +353 -0
  236. package/packages/opencode/src/sync/README.md +179 -0
  237. package/packages/opencode/src/sync/event.sql.ts +16 -0
  238. package/packages/opencode/src/sync/index.ts +263 -0
  239. package/packages/opencode/src/sync/schema.ts +14 -0
  240. package/packages/opencode/src/tool/apply_patch.ts +281 -0
  241. package/packages/opencode/src/tool/apply_patch.txt +33 -0
  242. package/packages/opencode/src/tool/bash.ts +496 -0
  243. package/packages/opencode/src/tool/bash.txt +117 -0
  244. package/packages/opencode/src/tool/batch.ts +183 -0
  245. package/packages/opencode/src/tool/batch.txt +24 -0
  246. package/packages/opencode/src/tool/codesearch.ts +132 -0
  247. package/packages/opencode/src/tool/codesearch.txt +12 -0
  248. package/packages/opencode/src/tool/edit.ts +667 -0
  249. package/packages/opencode/src/tool/edit.txt +10 -0
  250. package/packages/opencode/src/tool/external-directory.ts +46 -0
  251. package/packages/opencode/src/tool/glob.ts +78 -0
  252. package/packages/opencode/src/tool/glob.txt +6 -0
  253. package/packages/opencode/src/tool/grep.ts +156 -0
  254. package/packages/opencode/src/tool/grep.txt +8 -0
  255. package/packages/opencode/src/tool/invalid.ts +17 -0
  256. package/packages/opencode/src/tool/ls.ts +121 -0
  257. package/packages/opencode/src/tool/ls.txt +1 -0
  258. package/packages/opencode/src/tool/lsp.ts +97 -0
  259. package/packages/opencode/src/tool/lsp.txt +19 -0
  260. package/packages/opencode/src/tool/multiedit.ts +46 -0
  261. package/packages/opencode/src/tool/multiedit.txt +41 -0
  262. package/packages/opencode/src/tool/plan-enter.txt +14 -0
  263. package/packages/opencode/src/tool/plan-exit.txt +13 -0
  264. package/packages/opencode/src/tool/plan.ts +131 -0
  265. package/packages/opencode/src/tool/question.ts +46 -0
  266. package/packages/opencode/src/tool/question.txt +10 -0
  267. package/packages/opencode/src/tool/read.ts +332 -0
  268. package/packages/opencode/src/tool/read.txt +14 -0
  269. package/packages/opencode/src/tool/registry.ts +265 -0
  270. package/packages/opencode/src/tool/schema.ts +17 -0
  271. package/packages/opencode/src/tool/skill.ts +105 -0
  272. package/packages/opencode/src/tool/task.ts +166 -0
  273. package/packages/opencode/src/tool/task.txt +60 -0
  274. package/packages/opencode/src/tool/todo.ts +48 -0
  275. package/packages/opencode/src/tool/todowrite.txt +167 -0
  276. package/packages/opencode/src/tool/tool.ts +112 -0
  277. package/packages/opencode/src/tool/truncate.ts +144 -0
  278. package/packages/opencode/src/tool/truncation-dir.ts +4 -0
  279. package/packages/opencode/src/tool/webfetch.ts +206 -0
  280. package/packages/opencode/src/tool/webfetch.txt +13 -0
  281. package/packages/opencode/src/tool/websearch.ts +150 -0
  282. package/packages/opencode/src/tool/websearch.txt +14 -0
  283. package/packages/opencode/src/tool/write.ts +84 -0
  284. package/packages/opencode/src/tool/write.txt +8 -0
  285. package/packages/opencode/src/util/abort.ts +35 -0
  286. package/packages/opencode/src/util/archive.ts +17 -0
  287. package/packages/opencode/src/util/color.ts +19 -0
  288. package/packages/opencode/src/util/context.ts +25 -0
  289. package/packages/opencode/src/util/data-url.ts +9 -0
  290. package/packages/opencode/src/util/defer.ts +12 -0
  291. package/packages/opencode/src/util/effect-http-client.ts +11 -0
  292. package/packages/opencode/src/util/effect-zod.ts +98 -0
  293. package/packages/opencode/src/util/error.ts +77 -0
  294. package/packages/opencode/src/util/filesystem.ts +245 -0
  295. package/packages/opencode/src/util/flock.ts +333 -0
  296. package/packages/opencode/src/util/fn.ts +21 -0
  297. package/packages/opencode/src/util/format.ts +20 -0
  298. package/packages/opencode/src/util/glob.ts +34 -0
  299. package/packages/opencode/src/util/hash.ts +7 -0
  300. package/packages/opencode/src/util/iife.ts +3 -0
  301. package/packages/opencode/src/util/keybind.ts +103 -0
  302. package/packages/opencode/src/util/lazy.ts +23 -0
  303. package/packages/opencode/src/util/locale.ts +81 -0
  304. package/packages/opencode/src/util/lock.ts +98 -0
  305. package/packages/opencode/src/util/log.ts +182 -0
  306. package/packages/opencode/src/util/network.ts +9 -0
  307. package/packages/opencode/src/util/process.ts +176 -0
  308. package/packages/opencode/src/util/queue.ts +32 -0
  309. package/packages/opencode/src/util/record.ts +3 -0
  310. package/packages/opencode/src/util/rpc.ts +66 -0
  311. package/packages/opencode/src/util/schema.ts +53 -0
  312. package/packages/opencode/src/util/scrap.ts +10 -0
  313. package/packages/opencode/src/util/signal.ts +12 -0
  314. package/packages/opencode/src/util/timeout.ts +14 -0
  315. package/packages/opencode/src/util/token.ts +7 -0
  316. package/packages/opencode/src/util/update-schema.ts +13 -0
  317. package/packages/opencode/src/util/which.ts +14 -0
  318. package/packages/opencode/src/util/wildcard.ts +59 -0
  319. package/packages/opencode/src/worktree/index.ts +612 -0
  320. package/packages/plugin/package.json +38 -0
  321. package/packages/plugin/src/example.ts +18 -0
  322. package/packages/plugin/src/index.ts +282 -0
  323. package/packages/plugin/src/shell.ts +136 -0
  324. package/packages/plugin/src/tool.ts +38 -0
  325. package/packages/plugin/src/tui.ts +508 -0
  326. package/packages/script/package.json +16 -0
  327. package/packages/script/src/index.ts +77 -0
  328. package/packages/sdk/package.json +33 -0
  329. package/packages/sdk/src/client.ts +55 -0
  330. package/packages/sdk/src/gen/client/client.gen.ts +212 -0
  331. package/packages/sdk/src/gen/client/index.ts +25 -0
  332. package/packages/sdk/src/gen/client/types.gen.ts +222 -0
  333. package/packages/sdk/src/gen/client/utils.gen.ts +287 -0
  334. package/packages/sdk/src/gen/client.gen.ts +22 -0
  335. package/packages/sdk/src/gen/core/auth.gen.ts +41 -0
  336. package/packages/sdk/src/gen/core/bodySerializer.gen.ts +74 -0
  337. package/packages/sdk/src/gen/core/params.gen.ts +144 -0
  338. package/packages/sdk/src/gen/core/pathSerializer.gen.ts +167 -0
  339. package/packages/sdk/src/gen/core/queryKeySerializer.gen.ts +111 -0
  340. package/packages/sdk/src/gen/core/serverSentEvents.gen.ts +210 -0
  341. package/packages/sdk/src/gen/core/types.gen.ts +91 -0
  342. package/packages/sdk/src/gen/core/utils.gen.ts +109 -0
  343. package/packages/sdk/src/gen/sdk.gen.ts +1197 -0
  344. package/packages/sdk/src/gen/types.gen.ts +3904 -0
  345. package/packages/sdk/src/index.ts +21 -0
  346. package/packages/sdk/src/process.ts +31 -0
  347. package/packages/sdk/src/server.ts +134 -0
  348. package/packages/sdk/src/v2/client.ts +81 -0
  349. package/packages/sdk/src/v2/gen/client/client.gen.ts +285 -0
  350. package/packages/sdk/src/v2/gen/client/index.ts +25 -0
  351. package/packages/sdk/src/v2/gen/client/types.gen.ts +202 -0
  352. package/packages/sdk/src/v2/gen/client/utils.gen.ts +289 -0
  353. package/packages/sdk/src/v2/gen/client.gen.ts +18 -0
  354. package/packages/sdk/src/v2/gen/core/auth.gen.ts +41 -0
  355. package/packages/sdk/src/v2/gen/core/bodySerializer.gen.ts +82 -0
  356. package/packages/sdk/src/v2/gen/core/params.gen.ts +169 -0
  357. package/packages/sdk/src/v2/gen/core/pathSerializer.gen.ts +167 -0
  358. package/packages/sdk/src/v2/gen/core/queryKeySerializer.gen.ts +111 -0
  359. package/packages/sdk/src/v2/gen/core/serverSentEvents.gen.ts +239 -0
  360. package/packages/sdk/src/v2/gen/core/types.gen.ts +86 -0
  361. package/packages/sdk/src/v2/gen/core/utils.gen.ts +137 -0
  362. package/packages/sdk/src/v2/gen/sdk.gen.ts +4223 -0
  363. package/packages/sdk/src/v2/gen/types.gen.ts +5265 -0
  364. package/packages/sdk/src/v2/index.ts +21 -0
  365. package/packages/sdk/src/v2/server.ts +134 -0
  366. package/packages/util/package.json +20 -0
  367. package/packages/util/src/array.ts +10 -0
  368. package/packages/util/src/binary.ts +41 -0
  369. package/packages/util/src/encode.ts +51 -0
  370. package/packages/util/src/error.ts +54 -0
  371. package/packages/util/src/fn.ts +11 -0
  372. package/packages/util/src/identifier.ts +48 -0
  373. package/packages/util/src/iife.ts +3 -0
  374. package/packages/util/src/lazy.ts +11 -0
  375. package/packages/util/src/module.ts +10 -0
  376. package/packages/util/src/path.ts +37 -0
  377. package/packages/util/src/retry.ts +41 -0
  378. package/packages/util/src/slug.ts +74 -0
  379. package/postinstall.cjs +55 -0
@@ -0,0 +1,689 @@
1
+ import type { Argv } from "yargs"
2
+ import path from "path"
3
+ import { pathToFileURL } from "url"
4
+ import { UI } from "../ui"
5
+ import { cmd } from "./cmd"
6
+ import { Flag } from "../../flag/flag"
7
+ import { bootstrap } from "../bootstrap"
8
+ import { EOL } from "os"
9
+ import { Filesystem } from "../../util/filesystem"
10
+ import { createOpencodeClient, type Message, type OpencodeClient, type ToolPart } from "@opencode-ai/sdk/v2"
11
+ import { Server } from "../../server/server"
12
+ import { Provider } from "../../provider/provider"
13
+ import { Agent } from "../../agent/agent"
14
+ import { Permission } from "../../permission"
15
+ import { Tool } from "../../tool/tool"
16
+ import { GlobTool } from "../../tool/glob"
17
+ import { GrepTool } from "../../tool/grep"
18
+ import { ListTool } from "../../tool/ls"
19
+ import { ReadTool } from "../../tool/read"
20
+ import { WebFetchTool } from "../../tool/webfetch"
21
+ import { EditTool } from "../../tool/edit"
22
+ import { WriteTool } from "../../tool/write"
23
+ import { CodeSearchTool } from "../../tool/codesearch"
24
+ import { WebSearchTool } from "../../tool/websearch"
25
+ import { TaskTool } from "../../tool/task"
26
+ import { SkillTool } from "../../tool/skill"
27
+ import { BashTool } from "../../tool/bash"
28
+ import { TodoWriteTool } from "../../tool/todo"
29
+ import { Locale } from "../../util/locale"
30
+
31
+ type ToolProps<T> = {
32
+ input: Tool.InferParameters<T>
33
+ metadata: Tool.InferMetadata<T>
34
+ part: ToolPart
35
+ }
36
+
37
+ function props<T>(part: ToolPart): ToolProps<T> {
38
+ const state = part.state
39
+ return {
40
+ input: state.input as Tool.InferParameters<T>,
41
+ metadata: ("metadata" in state ? state.metadata : {}) as Tool.InferMetadata<T>,
42
+ part,
43
+ }
44
+ }
45
+
46
+ type Inline = {
47
+ icon: string
48
+ title: string
49
+ description?: string
50
+ }
51
+
52
+ function inline(info: Inline) {
53
+ const suffix = info.description ? UI.Style.TEXT_DIM + ` ${info.description}` + UI.Style.TEXT_NORMAL : ""
54
+ UI.println(UI.Style.TEXT_NORMAL + info.icon, UI.Style.TEXT_NORMAL + info.title + suffix)
55
+ }
56
+
57
+ function block(info: Inline, output?: string) {
58
+ UI.empty()
59
+ inline(info)
60
+ if (!output?.trim()) return
61
+ UI.println(output)
62
+ UI.empty()
63
+ }
64
+
65
+ function fallback(part: ToolPart) {
66
+ const state = part.state
67
+ const input = "input" in state ? state.input : undefined
68
+ const title =
69
+ ("title" in state && state.title ? state.title : undefined) ||
70
+ (input && typeof input === "object" && Object.keys(input).length > 0 ? JSON.stringify(input) : "Unknown")
71
+ inline({
72
+ icon: "⚙",
73
+ title: `${part.tool} ${title}`,
74
+ })
75
+ }
76
+
77
+ function glob(info: ToolProps<typeof GlobTool>) {
78
+ const root = info.input.path ?? ""
79
+ const title = `Glob "${info.input.pattern}"`
80
+ const suffix = root ? `in ${normalizePath(root)}` : ""
81
+ const num = info.metadata.count
82
+ const description =
83
+ num === undefined ? suffix : `${suffix}${suffix ? " · " : ""}${num} ${num === 1 ? "match" : "matches"}`
84
+ inline({
85
+ icon: "✱",
86
+ title,
87
+ ...(description && { description }),
88
+ })
89
+ }
90
+
91
+ function grep(info: ToolProps<typeof GrepTool>) {
92
+ const root = info.input.path ?? ""
93
+ const title = `Grep "${info.input.pattern}"`
94
+ const suffix = root ? `in ${normalizePath(root)}` : ""
95
+ const num = info.metadata.matches
96
+ const description =
97
+ num === undefined ? suffix : `${suffix}${suffix ? " · " : ""}${num} ${num === 1 ? "match" : "matches"}`
98
+ inline({
99
+ icon: "✱",
100
+ title,
101
+ ...(description && { description }),
102
+ })
103
+ }
104
+
105
+ function list(info: ToolProps<typeof ListTool>) {
106
+ const dir = info.input.path ? normalizePath(info.input.path) : ""
107
+ inline({
108
+ icon: "→",
109
+ title: dir ? `List ${dir}` : "List",
110
+ })
111
+ }
112
+
113
+ function read(info: ToolProps<typeof ReadTool>) {
114
+ const file = normalizePath(info.input.filePath)
115
+ const pairs = Object.entries(info.input).filter(([key, value]) => {
116
+ if (key === "filePath") return false
117
+ return typeof value === "string" || typeof value === "number" || typeof value === "boolean"
118
+ })
119
+ const description = pairs.length ? `[${pairs.map(([key, value]) => `${key}=${value}`).join(", ")}]` : undefined
120
+ inline({
121
+ icon: "→",
122
+ title: `Read ${file}`,
123
+ ...(description && { description }),
124
+ })
125
+ }
126
+
127
+ function write(info: ToolProps<typeof WriteTool>) {
128
+ block(
129
+ {
130
+ icon: "←",
131
+ title: `Write ${normalizePath(info.input.filePath)}`,
132
+ },
133
+ info.part.state.status === "completed" ? info.part.state.output : undefined,
134
+ )
135
+ }
136
+
137
+ function webfetch(info: ToolProps<typeof WebFetchTool>) {
138
+ inline({
139
+ icon: "%",
140
+ title: `WebFetch ${info.input.url}`,
141
+ })
142
+ }
143
+
144
+ function edit(info: ToolProps<typeof EditTool>) {
145
+ const title = normalizePath(info.input.filePath)
146
+ const diff = info.metadata.diff
147
+ block(
148
+ {
149
+ icon: "←",
150
+ title: `Edit ${title}`,
151
+ },
152
+ diff,
153
+ )
154
+ }
155
+
156
+ function codesearch(info: ToolProps<typeof CodeSearchTool>) {
157
+ inline({
158
+ icon: "◇",
159
+ title: `Exa Code Search "${info.input.query}"`,
160
+ })
161
+ }
162
+
163
+ function websearch(info: ToolProps<typeof WebSearchTool>) {
164
+ inline({
165
+ icon: "◈",
166
+ title: `Exa Web Search "${info.input.query}"`,
167
+ })
168
+ }
169
+
170
+ function task(info: ToolProps<typeof TaskTool>) {
171
+ const input = info.part.state.input
172
+ const status = info.part.state.status
173
+ const subagent =
174
+ typeof input.subagent_type === "string" && input.subagent_type.trim().length > 0 ? input.subagent_type : "unknown"
175
+ const agent = Locale.titlecase(subagent)
176
+ const desc =
177
+ typeof input.description === "string" && input.description.trim().length > 0 ? input.description : undefined
178
+ const icon = status === "error" ? "✗" : status === "running" ? "•" : "✓"
179
+ const name = desc ?? `${agent} Task`
180
+ inline({
181
+ icon,
182
+ title: name,
183
+ description: desc ? `${agent} Agent` : undefined,
184
+ })
185
+ }
186
+
187
+ function skill(info: ToolProps<typeof SkillTool>) {
188
+ inline({
189
+ icon: "→",
190
+ title: `Skill "${info.input.name}"`,
191
+ })
192
+ }
193
+
194
+ function bash(info: ToolProps<typeof BashTool>) {
195
+ const output = info.part.state.status === "completed" ? info.part.state.output?.trim() : undefined
196
+ block(
197
+ {
198
+ icon: "$",
199
+ title: `${info.input.command}`,
200
+ },
201
+ output,
202
+ )
203
+ }
204
+
205
+ function todo(info: ToolProps<typeof TodoWriteTool>) {
206
+ block(
207
+ {
208
+ icon: "#",
209
+ title: "Todos",
210
+ },
211
+ info.input.todos.map((item) => `${item.status === "completed" ? "[x]" : "[ ]"} ${item.content}`).join("\n"),
212
+ )
213
+ }
214
+
215
+ function normalizePath(input?: string) {
216
+ if (!input) return ""
217
+ if (path.isAbsolute(input)) return path.relative(process.cwd(), input) || "."
218
+ return input
219
+ }
220
+
221
+ export const RunCommand = cmd({
222
+ command: "run [message..]",
223
+ describe: "run opencode with a message",
224
+ builder: (yargs: Argv) => {
225
+ return yargs
226
+ .positional("message", {
227
+ describe: "message to send",
228
+ type: "string",
229
+ array: true,
230
+ default: [],
231
+ })
232
+ .option("command", {
233
+ describe: "the command to run, use message for args",
234
+ type: "string",
235
+ })
236
+ .option("continue", {
237
+ alias: ["c"],
238
+ describe: "continue the last session",
239
+ type: "boolean",
240
+ })
241
+ .option("session", {
242
+ alias: ["s"],
243
+ describe: "session id to continue",
244
+ type: "string",
245
+ })
246
+ .option("fork", {
247
+ describe: "fork the session before continuing (requires --continue or --session)",
248
+ type: "boolean",
249
+ })
250
+ .option("share", {
251
+ type: "boolean",
252
+ describe: "share the session",
253
+ })
254
+ .option("model", {
255
+ type: "string",
256
+ alias: ["m"],
257
+ describe: "model to use in the format of provider/model",
258
+ })
259
+ .option("agent", {
260
+ type: "string",
261
+ describe: "agent to use",
262
+ })
263
+ .option("format", {
264
+ type: "string",
265
+ choices: ["default", "json"],
266
+ default: "default",
267
+ describe: "format: default (formatted) or json (raw JSON events)",
268
+ })
269
+ .option("file", {
270
+ alias: ["f"],
271
+ type: "string",
272
+ array: true,
273
+ describe: "file(s) to attach to message",
274
+ })
275
+ .option("title", {
276
+ type: "string",
277
+ describe: "title for the session (uses truncated prompt if no value provided)",
278
+ })
279
+ .option("attach", {
280
+ type: "string",
281
+ describe: "attach to a running opencode server (e.g., http://localhost:4096)",
282
+ })
283
+ .option("password", {
284
+ alias: ["p"],
285
+ type: "string",
286
+ describe: "basic auth password (defaults to OPENCODE_SERVER_PASSWORD)",
287
+ })
288
+ .option("dir", {
289
+ type: "string",
290
+ describe: "directory to run in, path on remote server if attaching",
291
+ })
292
+ .option("port", {
293
+ type: "number",
294
+ describe: "port for the local server (defaults to random port if no value provided)",
295
+ })
296
+ .option("variant", {
297
+ type: "string",
298
+ describe: "model variant (provider-specific reasoning effort, e.g., high, max, minimal)",
299
+ })
300
+ .option("thinking", {
301
+ type: "boolean",
302
+ describe: "show thinking blocks",
303
+ default: false,
304
+ })
305
+ .option("dangerously-skip-permissions", {
306
+ type: "boolean",
307
+ describe: "auto-approve permissions that are not explicitly denied (dangerous!)",
308
+ default: false,
309
+ })
310
+ },
311
+ handler: async (args) => {
312
+ let message = [...args.message, ...(args["--"] || [])]
313
+ .map((arg) => (arg.includes(" ") ? `"${arg.replace(/"/g, '\\"')}"` : arg))
314
+ .join(" ")
315
+
316
+ const directory = (() => {
317
+ if (!args.dir) return undefined
318
+ if (args.attach) return args.dir
319
+ try {
320
+ process.chdir(args.dir)
321
+ return process.cwd()
322
+ } catch {
323
+ UI.error("Failed to change directory to " + args.dir)
324
+ process.exit(1)
325
+ }
326
+ })()
327
+
328
+ const files: { type: "file"; url: string; filename: string; mime: string }[] = []
329
+ if (args.file) {
330
+ const list = Array.isArray(args.file) ? args.file : [args.file]
331
+
332
+ for (const filePath of list) {
333
+ const resolvedPath = path.resolve(process.cwd(), filePath)
334
+ if (!(await Filesystem.exists(resolvedPath))) {
335
+ UI.error(`File not found: ${filePath}`)
336
+ process.exit(1)
337
+ }
338
+
339
+ const mime = (await Filesystem.isDir(resolvedPath)) ? "application/x-directory" : "text/plain"
340
+
341
+ files.push({
342
+ type: "file",
343
+ url: pathToFileURL(resolvedPath).href,
344
+ filename: path.basename(resolvedPath),
345
+ mime,
346
+ })
347
+ }
348
+ }
349
+
350
+ if (!process.stdin.isTTY) message += "\n" + (await Bun.stdin.text())
351
+
352
+ if (message.trim().length === 0 && !args.command) {
353
+ UI.error("You must provide a message or a command")
354
+ process.exit(1)
355
+ }
356
+
357
+ if (args.fork && !args.continue && !args.session) {
358
+ UI.error("--fork requires --continue or --session")
359
+ process.exit(1)
360
+ }
361
+
362
+ const rules: Permission.Ruleset = [
363
+ {
364
+ permission: "question",
365
+ action: "deny",
366
+ pattern: "*",
367
+ },
368
+ {
369
+ permission: "plan_enter",
370
+ action: "deny",
371
+ pattern: "*",
372
+ },
373
+ {
374
+ permission: "plan_exit",
375
+ action: "deny",
376
+ pattern: "*",
377
+ },
378
+ ]
379
+
380
+ function title() {
381
+ if (args.title === undefined) return
382
+ if (args.title !== "") return args.title
383
+ return message.slice(0, 50) + (message.length > 50 ? "..." : "")
384
+ }
385
+
386
+ async function session(sdk: OpencodeClient) {
387
+ const baseID = args.continue ? (await sdk.session.list()).data?.find((s) => !s.parentID)?.id : args.session
388
+
389
+ if (baseID && args.fork) {
390
+ const forked = await sdk.session.fork({ sessionID: baseID })
391
+ return forked.data?.id
392
+ }
393
+
394
+ if (baseID) return baseID
395
+
396
+ const name = title()
397
+ const result = await sdk.session.create({ title: name, permission: rules })
398
+ return result.data?.id
399
+ }
400
+
401
+ async function share(sdk: OpencodeClient, sessionID: string) {
402
+ const cfg = await sdk.config.get()
403
+ if (!cfg.data) return
404
+ if (cfg.data.share !== "auto" && !Flag.OPENCODE_AUTO_SHARE && !args.share) return
405
+ const res = await sdk.session.share({ sessionID }).catch((error) => {
406
+ if (error instanceof Error && error.message.includes("disabled")) {
407
+ UI.println(UI.Style.TEXT_DANGER_BOLD + "! " + error.message)
408
+ }
409
+ return { error }
410
+ })
411
+ if (!res.error && "data" in res && res.data?.share?.url) {
412
+ UI.println(UI.Style.TEXT_INFO_BOLD + "~ " + res.data.share.url)
413
+ }
414
+ }
415
+
416
+ async function execute(sdk: OpencodeClient) {
417
+ function tool(part: ToolPart) {
418
+ try {
419
+ if (part.tool === "bash") return bash(props<typeof BashTool>(part))
420
+ if (part.tool === "glob") return glob(props<typeof GlobTool>(part))
421
+ if (part.tool === "grep") return grep(props<typeof GrepTool>(part))
422
+ if (part.tool === "list") return list(props<typeof ListTool>(part))
423
+ if (part.tool === "read") return read(props<typeof ReadTool>(part))
424
+ if (part.tool === "write") return write(props<typeof WriteTool>(part))
425
+ if (part.tool === "webfetch") return webfetch(props<typeof WebFetchTool>(part))
426
+ if (part.tool === "edit") return edit(props<typeof EditTool>(part))
427
+ if (part.tool === "codesearch") return codesearch(props<typeof CodeSearchTool>(part))
428
+ if (part.tool === "websearch") return websearch(props<typeof WebSearchTool>(part))
429
+ if (part.tool === "task") return task(props<typeof TaskTool>(part))
430
+ if (part.tool === "todowrite") return todo(props<typeof TodoWriteTool>(part))
431
+ if (part.tool === "skill") return skill(props<typeof SkillTool>(part))
432
+ return fallback(part)
433
+ } catch {
434
+ return fallback(part)
435
+ }
436
+ }
437
+
438
+ function emit(type: string, data: Record<string, unknown>) {
439
+ if (args.format === "json") {
440
+ process.stdout.write(JSON.stringify({ type, timestamp: Date.now(), sessionID, ...data }) + EOL)
441
+ return true
442
+ }
443
+ return false
444
+ }
445
+
446
+ const events = await sdk.event.subscribe()
447
+ let error: string | undefined
448
+
449
+ async function loop() {
450
+ const toggles = new Map<string, boolean>()
451
+
452
+ for await (const event of events.stream) {
453
+ if (
454
+ event.type === "message.updated" &&
455
+ event.properties.info.role === "assistant" &&
456
+ args.format !== "json" &&
457
+ toggles.get("start") !== true
458
+ ) {
459
+ UI.empty()
460
+ UI.println(`> ${event.properties.info.agent} · ${event.properties.info.modelID}`)
461
+ UI.empty()
462
+ toggles.set("start", true)
463
+ }
464
+
465
+ if (event.type === "message.part.updated") {
466
+ const part = event.properties.part
467
+ if (part.sessionID !== sessionID) continue
468
+
469
+ if (part.type === "tool" && (part.state.status === "completed" || part.state.status === "error")) {
470
+ if (emit("tool_use", { part })) continue
471
+ if (part.state.status === "completed") {
472
+ tool(part)
473
+ continue
474
+ }
475
+ inline({
476
+ icon: "✗",
477
+ title: `${part.tool} failed`,
478
+ })
479
+ UI.error(part.state.error)
480
+ }
481
+
482
+ if (
483
+ part.type === "tool" &&
484
+ part.tool === "task" &&
485
+ part.state.status === "running" &&
486
+ args.format !== "json"
487
+ ) {
488
+ if (toggles.get(part.id) === true) continue
489
+ task(props<typeof TaskTool>(part))
490
+ toggles.set(part.id, true)
491
+ }
492
+
493
+ if (part.type === "step-start") {
494
+ if (emit("step_start", { part })) continue
495
+ }
496
+
497
+ if (part.type === "step-finish") {
498
+ if (emit("step_finish", { part })) continue
499
+ }
500
+
501
+ if (part.type === "text" && part.time?.end) {
502
+ if (emit("text", { part })) continue
503
+ const text = part.text.trim()
504
+ if (!text) continue
505
+ if (!process.stdout.isTTY) {
506
+ process.stdout.write(text + EOL)
507
+ continue
508
+ }
509
+ UI.empty()
510
+ UI.println(text)
511
+ UI.empty()
512
+ }
513
+
514
+ if (part.type === "reasoning" && part.time?.end && args.thinking) {
515
+ if (emit("reasoning", { part })) continue
516
+ const text = part.text.trim()
517
+ if (!text) continue
518
+ const line = `Thinking: ${text}`
519
+ if (process.stdout.isTTY) {
520
+ UI.empty()
521
+ UI.println(`${UI.Style.TEXT_DIM}\u001b[3m${line}\u001b[0m${UI.Style.TEXT_NORMAL}`)
522
+ UI.empty()
523
+ continue
524
+ }
525
+ process.stdout.write(line + EOL)
526
+ }
527
+ }
528
+
529
+ if (event.type === "session.error") {
530
+ const props = event.properties
531
+ if (props.sessionID !== sessionID || !props.error) continue
532
+ let err = String(props.error.name)
533
+ if ("data" in props.error && props.error.data && "message" in props.error.data) {
534
+ err = String(props.error.data.message)
535
+ }
536
+ error = error ? error + EOL + err : err
537
+ if (emit("error", { error: props.error })) continue
538
+ UI.error(err)
539
+ }
540
+
541
+ if (
542
+ event.type === "session.status" &&
543
+ event.properties.sessionID === sessionID &&
544
+ event.properties.status.type === "idle"
545
+ ) {
546
+ break
547
+ }
548
+
549
+ if (event.type === "permission.asked") {
550
+ const permission = event.properties
551
+ if (permission.sessionID !== sessionID) continue
552
+
553
+ if (args["dangerously-skip-permissions"]) {
554
+ await sdk.permission.reply({
555
+ requestID: permission.id,
556
+ reply: "once",
557
+ })
558
+ } else {
559
+ UI.println(
560
+ UI.Style.TEXT_WARNING_BOLD + "!",
561
+ UI.Style.TEXT_NORMAL +
562
+ `permission requested: ${permission.permission} (${permission.patterns.join(", ")}); auto-rejecting`,
563
+ )
564
+ await sdk.permission.reply({
565
+ requestID: permission.id,
566
+ reply: "reject",
567
+ })
568
+ }
569
+ }
570
+ }
571
+ }
572
+
573
+ // Validate agent if specified
574
+ const agent = await (async () => {
575
+ if (!args.agent) return undefined
576
+
577
+ // When attaching, validate against the running server instead of local Instance state.
578
+ if (args.attach) {
579
+ const modes = await sdk.app
580
+ .agents(undefined, { throwOnError: true })
581
+ .then((x) => x.data ?? [])
582
+ .catch(() => undefined)
583
+
584
+ if (!modes) {
585
+ UI.println(
586
+ UI.Style.TEXT_WARNING_BOLD + "!",
587
+ UI.Style.TEXT_NORMAL,
588
+ `failed to list agents from ${args.attach}. Falling back to default agent`,
589
+ )
590
+ return undefined
591
+ }
592
+
593
+ const agent = modes.find((a) => a.name === args.agent)
594
+ if (!agent) {
595
+ UI.println(
596
+ UI.Style.TEXT_WARNING_BOLD + "!",
597
+ UI.Style.TEXT_NORMAL,
598
+ `agent "${args.agent}" not found. Falling back to default agent`,
599
+ )
600
+ return undefined
601
+ }
602
+
603
+ if (agent.mode === "subagent") {
604
+ UI.println(
605
+ UI.Style.TEXT_WARNING_BOLD + "!",
606
+ UI.Style.TEXT_NORMAL,
607
+ `agent "${args.agent}" is a subagent, not a primary agent. Falling back to default agent`,
608
+ )
609
+ return undefined
610
+ }
611
+
612
+ return args.agent
613
+ }
614
+
615
+ const entry = await Agent.get(args.agent)
616
+ if (!entry) {
617
+ UI.println(
618
+ UI.Style.TEXT_WARNING_BOLD + "!",
619
+ UI.Style.TEXT_NORMAL,
620
+ `agent "${args.agent}" not found. Falling back to default agent`,
621
+ )
622
+ return undefined
623
+ }
624
+ if (entry.mode === "subagent") {
625
+ UI.println(
626
+ UI.Style.TEXT_WARNING_BOLD + "!",
627
+ UI.Style.TEXT_NORMAL,
628
+ `agent "${args.agent}" is a subagent, not a primary agent. Falling back to default agent`,
629
+ )
630
+ return undefined
631
+ }
632
+ return args.agent
633
+ })()
634
+
635
+ const sessionID = await session(sdk)
636
+ if (!sessionID) {
637
+ UI.error("Session not found")
638
+ process.exit(1)
639
+ }
640
+ await share(sdk, sessionID)
641
+
642
+ loop().catch((e) => {
643
+ console.error(e)
644
+ process.exit(1)
645
+ })
646
+
647
+ if (args.command) {
648
+ await sdk.session.command({
649
+ sessionID,
650
+ agent,
651
+ model: args.model,
652
+ command: args.command,
653
+ arguments: message,
654
+ variant: args.variant,
655
+ })
656
+ } else {
657
+ const model = args.model ? Provider.parseModel(args.model) : undefined
658
+ await sdk.session.prompt({
659
+ sessionID,
660
+ agent,
661
+ model,
662
+ variant: args.variant,
663
+ parts: [...files, { type: "text", text: message }],
664
+ })
665
+ }
666
+ }
667
+
668
+ if (args.attach) {
669
+ const headers = (() => {
670
+ const password = args.password ?? process.env.OPENCODE_SERVER_PASSWORD
671
+ if (!password) return undefined
672
+ const username = process.env.OPENCODE_SERVER_USERNAME ?? "opencode"
673
+ const auth = `Basic ${Buffer.from(`${username}:${password}`).toString("base64")}`
674
+ return { Authorization: auth }
675
+ })()
676
+ const sdk = createOpencodeClient({ baseUrl: args.attach, directory, headers })
677
+ return await execute(sdk)
678
+ }
679
+
680
+ await bootstrap(process.cwd(), async () => {
681
+ const fetchFn = (async (input: RequestInfo | URL, init?: RequestInit) => {
682
+ const request = new Request(input, init)
683
+ return Server.Default().fetch(request)
684
+ }) as typeof globalThis.fetch
685
+ const sdk = createOpencodeClient({ baseUrl: "http://opencode.internal", fetch: fetchFn })
686
+ await execute(sdk)
687
+ })
688
+ },
689
+ })
@@ -0,0 +1,24 @@
1
+ import { Server } from "../../server/server"
2
+ import { cmd } from "./cmd"
3
+ import { withNetworkOptions, resolveNetworkOptions } from "../network"
4
+ import { Flag } from "../../flag/flag"
5
+ import { Workspace } from "../../control-plane/workspace"
6
+ import { Project } from "../../project/project"
7
+ import { Installation } from "../../installation"
8
+
9
+ export const ServeCommand = cmd({
10
+ command: "serve",
11
+ builder: (yargs) => withNetworkOptions(yargs),
12
+ describe: "starts a headless opencode server",
13
+ handler: async (args) => {
14
+ if (!Flag.OPENCODE_SERVER_PASSWORD) {
15
+ console.log("Warning: OPENCODE_SERVER_PASSWORD is not set; server is unsecured.")
16
+ }
17
+ const opts = await resolveNetworkOptions(args)
18
+ const server = await Server.listen(opts)
19
+ console.log(`opencode server listening on http://${server.hostname}:${server.port}`)
20
+
21
+ await new Promise(() => {})
22
+ await server.stop()
23
+ },
24
+ })