neurocode-ai 1.18.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (756) hide show
  1. package/AGENTS.md +131 -0
  2. package/Dockerfile +18 -0
  3. package/README.md +15 -0
  4. package/bin/neurocode +199 -0
  5. package/bunfig.toml +7 -0
  6. package/git +0 -0
  7. package/migration/20260511173437_session-metadata/migration.sql +1 -0
  8. package/migration/20260511173437_session-metadata/snapshot.json +1500 -0
  9. package/package.json +157 -0
  10. package/parsers-config.ts +1 -0
  11. package/script/bench-search.ts +94 -0
  12. package/script/bench-test-suite.ts +52 -0
  13. package/script/build.ts +246 -0
  14. package/script/generate.ts +14 -0
  15. package/script/httpapi-exercise.ts +1 -0
  16. package/script/postinstall.mjs +189 -0
  17. package/script/profile-test-files.ts +42 -0
  18. package/script/publish.ts +213 -0
  19. package/script/run-workspace-server +106 -0
  20. package/script/schema.ts +77 -0
  21. package/script/time.ts +6 -0
  22. package/script/trace-imports.ts +153 -0
  23. package/specs/effect/error-boundaries-plan.md +235 -0
  24. package/specs/effect/errors.md +207 -0
  25. package/specs/effect/facades.md +218 -0
  26. package/specs/effect/guide.md +247 -0
  27. package/specs/effect/instance-context.md +13 -0
  28. package/specs/effect/loose-ends.md +30 -0
  29. package/specs/effect/migration.md +62 -0
  30. package/specs/effect/routes.md +61 -0
  31. package/specs/effect/schema.md +88 -0
  32. package/specs/effect/server-package.md +58 -0
  33. package/specs/effect/todo.md +241 -0
  34. package/specs/effect/tools.md +88 -0
  35. package/specs/openapi-translation-cleanup.md +204 -0
  36. package/specs/tui-plugins.md +544 -0
  37. package/specs/v2/api.ts +67 -0
  38. package/specs/v2/message-shape.md +136 -0
  39. package/specs/v2/notifications.md +13 -0
  40. package/specs/v2/tui-command-shim.md +67 -0
  41. package/src/account/account.ts +473 -0
  42. package/src/account/repo.ts +171 -0
  43. package/src/account/schema.ts +99 -0
  44. package/src/account/url.ts +8 -0
  45. package/src/acp/agent.ts +95 -0
  46. package/src/acp/config-option.ts +203 -0
  47. package/src/acp/content.ts +269 -0
  48. package/src/acp/directory.ts +212 -0
  49. package/src/acp/error.ts +97 -0
  50. package/src/acp/event.ts +342 -0
  51. package/src/acp/permission.ts +254 -0
  52. package/src/acp/profile.ts +42 -0
  53. package/src/acp/service.ts +1097 -0
  54. package/src/acp/session.ts +232 -0
  55. package/src/acp/tool.ts +364 -0
  56. package/src/acp/usage.ts +239 -0
  57. package/src/agent/agent.ts +501 -0
  58. package/src/agent/generate.txt +75 -0
  59. package/src/agent/prompt/compaction.txt +9 -0
  60. package/src/agent/prompt/explore.txt +18 -0
  61. package/src/agent/prompt/summary.txt +11 -0
  62. package/src/agent/prompt/title.txt +44 -0
  63. package/src/agent/subagent-permissions.ts +27 -0
  64. package/src/audio.d.ts +14 -0
  65. package/src/auth/index.ts +97 -0
  66. package/src/background/job.ts +37 -0
  67. package/src/bus/global.ts +22 -0
  68. package/src/cli/bootstrap.ts +11 -0
  69. package/src/cli/cmd/account.ts +264 -0
  70. package/src/cli/cmd/acp.ts +73 -0
  71. package/src/cli/cmd/agent.ts +259 -0
  72. package/src/cli/cmd/attach.ts +148 -0
  73. package/src/cli/cmd/cmd.ts +7 -0
  74. package/src/cli/cmd/db.ts +62 -0
  75. package/src/cli/cmd/debug/agent.handler.ts +193 -0
  76. package/src/cli/cmd/debug/agent.ts +27 -0
  77. package/src/cli/cmd/debug/config.ts +14 -0
  78. package/src/cli/cmd/debug/file.ts +73 -0
  79. package/src/cli/cmd/debug/index.ts +87 -0
  80. package/src/cli/cmd/debug/lsp.ts +50 -0
  81. package/src/cli/cmd/debug/ripgrep.ts +79 -0
  82. package/src/cli/cmd/debug/scrap.ts +16 -0
  83. package/src/cli/cmd/debug/skill.ts +15 -0
  84. package/src/cli/cmd/debug/snapshot.ts +50 -0
  85. package/src/cli/cmd/debug/startup.ts +11 -0
  86. package/src/cli/cmd/debug/v2.ts +42 -0
  87. package/src/cli/cmd/export.ts +292 -0
  88. package/src/cli/cmd/generate.ts +54 -0
  89. package/src/cli/cmd/github.handler.ts +1593 -0
  90. package/src/cli/cmd/github.shared.ts +30 -0
  91. package/src/cli/cmd/github.ts +42 -0
  92. package/src/cli/cmd/import.ts +230 -0
  93. package/src/cli/cmd/mcp.ts +840 -0
  94. package/src/cli/cmd/models.ts +66 -0
  95. package/src/cli/cmd/plug.ts +230 -0
  96. package/src/cli/cmd/pr.ts +115 -0
  97. package/src/cli/cmd/prompt-display.ts +1 -0
  98. package/src/cli/cmd/providers.ts +534 -0
  99. package/src/cli/cmd/run/demo.ts +1274 -0
  100. package/src/cli/cmd/run/entry.body.ts +205 -0
  101. package/src/cli/cmd/run/footer.command.tsx +1070 -0
  102. package/src/cli/cmd/run/footer.menu.tsx +351 -0
  103. package/src/cli/cmd/run/footer.permission.tsx +472 -0
  104. package/src/cli/cmd/run/footer.prompt.tsx +1306 -0
  105. package/src/cli/cmd/run/footer.question.tsx +573 -0
  106. package/src/cli/cmd/run/footer.subagent.tsx +175 -0
  107. package/src/cli/cmd/run/footer.ts +1129 -0
  108. package/src/cli/cmd/run/footer.view.tsx +945 -0
  109. package/src/cli/cmd/run/footer.width.ts +27 -0
  110. package/src/cli/cmd/run/permission.shared.ts +256 -0
  111. package/src/cli/cmd/run/prompt.editor.ts +157 -0
  112. package/src/cli/cmd/run/prompt.shared.ts +153 -0
  113. package/src/cli/cmd/run/question.shared.ts +340 -0
  114. package/src/cli/cmd/run/runtime.boot.ts +202 -0
  115. package/src/cli/cmd/run/runtime.lifecycle.ts +406 -0
  116. package/src/cli/cmd/run/runtime.queue.ts +349 -0
  117. package/src/cli/cmd/run/runtime.shared.ts +17 -0
  118. package/src/cli/cmd/run/runtime.stdin.ts +37 -0
  119. package/src/cli/cmd/run/runtime.ts +814 -0
  120. package/src/cli/cmd/run/scrollback.shared.ts +92 -0
  121. package/src/cli/cmd/run/scrollback.surface.ts +431 -0
  122. package/src/cli/cmd/run/scrollback.writer.tsx +352 -0
  123. package/src/cli/cmd/run/session-data.ts +1113 -0
  124. package/src/cli/cmd/run/session-replay.ts +374 -0
  125. package/src/cli/cmd/run/session.shared.ts +196 -0
  126. package/src/cli/cmd/run/splash.ts +280 -0
  127. package/src/cli/cmd/run/stream.transport.ts +1462 -0
  128. package/src/cli/cmd/run/stream.ts +175 -0
  129. package/src/cli/cmd/run/subagent-data.ts +876 -0
  130. package/src/cli/cmd/run/theme.ts +690 -0
  131. package/src/cli/cmd/run/tool.ts +1486 -0
  132. package/src/cli/cmd/run/trace.ts +94 -0
  133. package/src/cli/cmd/run/turn-summary.ts +47 -0
  134. package/src/cli/cmd/run/types.ts +350 -0
  135. package/src/cli/cmd/run/variant.shared.ts +216 -0
  136. package/src/cli/cmd/run.ts +1011 -0
  137. package/src/cli/cmd/serve.ts +24 -0
  138. package/src/cli/cmd/session.ts +147 -0
  139. package/src/cli/cmd/stats.ts +393 -0
  140. package/src/cli/cmd/tui.ts +309 -0
  141. package/src/cli/cmd/uninstall.ts +353 -0
  142. package/src/cli/cmd/upgrade.ts +74 -0
  143. package/src/cli/cmd/web.ts +84 -0
  144. package/src/cli/effect/prompt.ts +37 -0
  145. package/src/cli/effect-cmd.ts +96 -0
  146. package/src/cli/error.ts +130 -0
  147. package/src/cli/heap.ts +45 -0
  148. package/src/cli/logo.ts +1 -0
  149. package/src/cli/network.ts +80 -0
  150. package/src/cli/tui/layer.ts +8 -0
  151. package/src/cli/tui/validate-session.ts +29 -0
  152. package/src/cli/tui/worker.ts +80 -0
  153. package/src/cli/ui.ts +132 -0
  154. package/src/cli/upgrade.ts +53 -0
  155. package/src/command/index.ts +177 -0
  156. package/src/command/template/initialize.txt +66 -0
  157. package/src/command/template/review.txt +101 -0
  158. package/src/config/agent.ts +59 -0
  159. package/src/config/command.ts +39 -0
  160. package/src/config/config.ts +681 -0
  161. package/src/config/entry-name.ts +19 -0
  162. package/src/config/managed.ts +69 -0
  163. package/src/config/markdown.ts +36 -0
  164. package/src/config/parse.ts +79 -0
  165. package/src/config/paths.ts +45 -0
  166. package/src/config/plugin.ts +79 -0
  167. package/src/config/tui-cwd.ts +5 -0
  168. package/src/config/tui-host-attention.ts +21 -0
  169. package/src/config/tui-migrate.ts +132 -0
  170. package/src/config/tui.ts +276 -0
  171. package/src/config/variable.ts +91 -0
  172. package/src/control-plane/adapters/index.ts +41 -0
  173. package/src/control-plane/adapters/worktree.ts +96 -0
  174. package/src/control-plane/dev/README.md +19 -0
  175. package/src/control-plane/dev/debug-workspace-plugin.ts +73 -0
  176. package/src/control-plane/types.ts +59 -0
  177. package/src/control-plane/util.ts +39 -0
  178. package/src/control-plane/workspace-adapter-runtime.ts +51 -0
  179. package/src/control-plane/workspace-context.ts +26 -0
  180. package/src/control-plane/workspace.ts +964 -0
  181. package/src/effect/app-node-builder-v1.ts +12 -0
  182. package/src/effect/app-runtime.ts +135 -0
  183. package/src/effect/bootstrap-runtime.ts +19 -0
  184. package/src/effect/bridge.ts +84 -0
  185. package/src/effect/config-service.ts +67 -0
  186. package/src/effect/instance-ref.ts +11 -0
  187. package/src/effect/instance-registry.ts +12 -0
  188. package/src/effect/instance-state.ts +69 -0
  189. package/src/effect/promise.ts +17 -0
  190. package/src/effect/run-service.ts +47 -0
  191. package/src/effect/runner.ts +217 -0
  192. package/src/effect/runtime-flags.ts +78 -0
  193. package/src/env/index.ts +41 -0
  194. package/src/event-manifest.ts +3 -0
  195. package/src/event-v2-bridge.ts +71 -0
  196. package/src/format/formatter.ts +404 -0
  197. package/src/format/index.ts +203 -0
  198. package/src/git/index.ts +348 -0
  199. package/src/id/id.ts +80 -0
  200. package/src/ide/index.ts +54 -0
  201. package/src/image/image.ts +172 -0
  202. package/src/index.ts +142 -0
  203. package/src/installation/index.ts +336 -0
  204. package/src/lsp/client.ts +650 -0
  205. package/src/lsp/diagnostic.ts +29 -0
  206. package/src/lsp/language.ts +121 -0
  207. package/src/lsp/launch.ts +21 -0
  208. package/src/lsp/lsp.ts +507 -0
  209. package/src/lsp/server.ts +1983 -0
  210. package/src/markdown.d.ts +4 -0
  211. package/src/mcp/auth.ts +163 -0
  212. package/src/mcp/browser.ts +37 -0
  213. package/src/mcp/catalog.ts +170 -0
  214. package/src/mcp/index.ts +1004 -0
  215. package/src/mcp/oauth-callback.ts +194 -0
  216. package/src/mcp/oauth-provider.ts +259 -0
  217. package/src/node.ts +4 -0
  218. package/src/patch/index.ts +686 -0
  219. package/src/permission/arity.ts +163 -0
  220. package/src/permission/evaluate.ts +1 -0
  221. package/src/permission/index.ts +223 -0
  222. package/src/plugin/azure.ts +26 -0
  223. package/src/plugin/cloudflare.ts +76 -0
  224. package/src/plugin/digitalocean.ts +325 -0
  225. package/src/plugin/github-copilot/copilot.ts +414 -0
  226. package/src/plugin/github-copilot/models.ts +258 -0
  227. package/src/plugin/index.ts +314 -0
  228. package/src/plugin/install.ts +439 -0
  229. package/src/plugin/loader.ts +237 -0
  230. package/src/plugin/meta.ts +188 -0
  231. package/src/plugin/openai/README.md +31 -0
  232. package/src/plugin/openai/codex.ts +565 -0
  233. package/src/plugin/openai/ws-pool.ts +270 -0
  234. package/src/plugin/openai/ws.ts +381 -0
  235. package/src/plugin/pty-environment.ts +24 -0
  236. package/src/plugin/shared.ts +323 -0
  237. package/src/plugin/snowflake-cortex.ts +507 -0
  238. package/src/plugin/tui/internal.ts +10 -0
  239. package/src/plugin/tui/runtime.ts +1131 -0
  240. package/src/plugin/xai.ts +626 -0
  241. package/src/project/bootstrap-service.ts +9 -0
  242. package/src/project/bootstrap.ts +58 -0
  243. package/src/project/instance-context.ts +24 -0
  244. package/src/project/instance-runtime.ts +16 -0
  245. package/src/project/instance-store.ts +213 -0
  246. package/src/project/project.ts +483 -0
  247. package/src/project/vcs.ts +423 -0
  248. package/src/provider/auth.ts +229 -0
  249. package/src/provider/error.ts +188 -0
  250. package/src/provider/model-status.ts +8 -0
  251. package/src/provider/provider.ts +2006 -0
  252. package/src/provider/transform.ts +1832 -0
  253. package/src/question/index.ts +161 -0
  254. package/src/question/schema.ts +4 -0
  255. package/src/server/auth.ts +48 -0
  256. package/src/server/event.ts +10 -0
  257. package/src/server/global-lifecycle.ts +28 -0
  258. package/src/server/init-projectors.ts +3 -0
  259. package/src/server/mdns.ts +47 -0
  260. package/src/server/projectors.ts +1 -0
  261. package/src/server/proxy-util.ts +48 -0
  262. package/src/server/routes/instance/httpapi/AGENTS.md +39 -0
  263. package/src/server/routes/instance/httpapi/api.ts +97 -0
  264. package/src/server/routes/instance/httpapi/errors.ts +193 -0
  265. package/src/server/routes/instance/httpapi/groups/config.ts +65 -0
  266. package/src/server/routes/instance/httpapi/groups/control-plane.ts +35 -0
  267. package/src/server/routes/instance/httpapi/groups/control.ts +76 -0
  268. package/src/server/routes/instance/httpapi/groups/event.ts +29 -0
  269. package/src/server/routes/instance/httpapi/groups/experimental.ts +275 -0
  270. package/src/server/routes/instance/httpapi/groups/file.ts +185 -0
  271. package/src/server/routes/instance/httpapi/groups/global.ts +136 -0
  272. package/src/server/routes/instance/httpapi/groups/instance.ts +206 -0
  273. package/src/server/routes/instance/httpapi/groups/mcp.ts +156 -0
  274. package/src/server/routes/instance/httpapi/groups/metadata.ts +18 -0
  275. package/src/server/routes/instance/httpapi/groups/permission.ts +61 -0
  276. package/src/server/routes/instance/httpapi/groups/project-copy.ts +32 -0
  277. package/src/server/routes/instance/httpapi/groups/project.ts +93 -0
  278. package/src/server/routes/instance/httpapi/groups/provider.ts +101 -0
  279. package/src/server/routes/instance/httpapi/groups/pty.ts +172 -0
  280. package/src/server/routes/instance/httpapi/groups/query.ts +12 -0
  281. package/src/server/routes/instance/httpapi/groups/question.ts +74 -0
  282. package/src/server/routes/instance/httpapi/groups/session.ts +462 -0
  283. package/src/server/routes/instance/httpapi/groups/sync.ts +113 -0
  284. package/src/server/routes/instance/httpapi/groups/tui.ts +208 -0
  285. package/src/server/routes/instance/httpapi/groups/workspace.ts +141 -0
  286. package/src/server/routes/instance/httpapi/handlers/config.ts +34 -0
  287. package/src/server/routes/instance/httpapi/handlers/control-plane.ts +37 -0
  288. package/src/server/routes/instance/httpapi/handlers/control.ts +43 -0
  289. package/src/server/routes/instance/httpapi/handlers/event.ts +99 -0
  290. package/src/server/routes/instance/httpapi/handlers/experimental.ts +193 -0
  291. package/src/server/routes/instance/httpapi/handlers/file.ts +139 -0
  292. package/src/server/routes/instance/httpapi/handlers/global.ts +156 -0
  293. package/src/server/routes/instance/httpapi/handlers/instance.ts +110 -0
  294. package/src/server/routes/instance/httpapi/handlers/mcp.ts +111 -0
  295. package/src/server/routes/instance/httpapi/handlers/permission.ts +41 -0
  296. package/src/server/routes/instance/httpapi/handlers/project-copy.ts +83 -0
  297. package/src/server/routes/instance/httpapi/handlers/project.ts +63 -0
  298. package/src/server/routes/instance/httpapi/handlers/provider.ts +113 -0
  299. package/src/server/routes/instance/httpapi/handlers/pty.ts +273 -0
  300. package/src/server/routes/instance/httpapi/handlers/question.ts +54 -0
  301. package/src/server/routes/instance/httpapi/handlers/session-errors.ts +21 -0
  302. package/src/server/routes/instance/httpapi/handlers/session.ts +442 -0
  303. package/src/server/routes/instance/httpapi/handlers/sync.ts +89 -0
  304. package/src/server/routes/instance/httpapi/handlers/tui.ts +131 -0
  305. package/src/server/routes/instance/httpapi/handlers/workspace.ts +102 -0
  306. package/src/server/routes/instance/httpapi/lifecycle.ts +54 -0
  307. package/src/server/routes/instance/httpapi/middleware/authorization.ts +150 -0
  308. package/src/server/routes/instance/httpapi/middleware/compression.ts +64 -0
  309. package/src/server/routes/instance/httpapi/middleware/cors-vary.ts +29 -0
  310. package/src/server/routes/instance/httpapi/middleware/error.ts +43 -0
  311. package/src/server/routes/instance/httpapi/middleware/fence.ts +25 -0
  312. package/src/server/routes/instance/httpapi/middleware/instance-context.ts +43 -0
  313. package/src/server/routes/instance/httpapi/middleware/proxy.ts +108 -0
  314. package/src/server/routes/instance/httpapi/middleware/schema-error.ts +41 -0
  315. package/src/server/routes/instance/httpapi/middleware/workspace-routing.ts +250 -0
  316. package/src/server/routes/instance/httpapi/public.ts +537 -0
  317. package/src/server/routes/instance/httpapi/server.ts +325 -0
  318. package/src/server/routes/instance/httpapi/websocket-tracker.ts +60 -0
  319. package/src/server/server.ts +226 -0
  320. package/src/server/shared/fence.ts +60 -0
  321. package/src/server/shared/pty-ticket.ts +15 -0
  322. package/src/server/shared/public-ui.ts +12 -0
  323. package/src/server/shared/tui-control.ts +28 -0
  324. package/src/server/shared/ui.ts +108 -0
  325. package/src/server/shared/workspace-routing.ts +38 -0
  326. package/src/server/tui-event.ts +1 -0
  327. package/src/session/compaction.ts +562 -0
  328. package/src/session/instruction.ts +237 -0
  329. package/src/session/llm/AGENTS.md +90 -0
  330. package/src/session/llm/ai-sdk.ts +288 -0
  331. package/src/session/llm/native-request.ts +196 -0
  332. package/src/session/llm/native-runtime.ts +195 -0
  333. package/src/session/llm/request.ts +226 -0
  334. package/src/session/llm.ts +404 -0
  335. package/src/session/message-error.ts +14 -0
  336. package/src/session/message-v2.ts +734 -0
  337. package/src/session/message.ts +148 -0
  338. package/src/session/overflow.ts +34 -0
  339. package/src/session/processor.ts +718 -0
  340. package/src/session/prompt/anthropic.txt +105 -0
  341. package/src/session/prompt/beast.txt +147 -0
  342. package/src/session/prompt/build-switch.txt +5 -0
  343. package/src/session/prompt/codex.txt +79 -0
  344. package/src/session/prompt/copilot-gpt-5.txt +143 -0
  345. package/src/session/prompt/default.txt +95 -0
  346. package/src/session/prompt/gemini.txt +155 -0
  347. package/src/session/prompt/gpt.txt +107 -0
  348. package/src/session/prompt/kimi.txt +95 -0
  349. package/src/session/prompt/meta.txt +65 -0
  350. package/src/session/prompt/plan-mode.txt +70 -0
  351. package/src/session/prompt/plan-reminder-anthropic.txt +67 -0
  352. package/src/session/prompt/plan.txt +26 -0
  353. package/src/session/prompt/trinity.txt +97 -0
  354. package/src/session/prompt.ts +1631 -0
  355. package/src/session/reminders.ts +92 -0
  356. package/src/session/retry.ts +201 -0
  357. package/src/session/revert.ts +146 -0
  358. package/src/session/run-state.ts +151 -0
  359. package/src/session/schema.ts +26 -0
  360. package/src/session/session.ts +1018 -0
  361. package/src/session/status.ts +56 -0
  362. package/src/session/summary.ts +160 -0
  363. package/src/session/system.ts +145 -0
  364. package/src/session/todo.ts +74 -0
  365. package/src/session/tools.ts +590 -0
  366. package/src/share/session.ts +58 -0
  367. package/src/share/share-next.ts +371 -0
  368. package/src/skill/discovery.ts +140 -0
  369. package/src/skill/index.ts +354 -0
  370. package/src/snapshot/index.ts +807 -0
  371. package/src/sql.d.ts +4 -0
  372. package/src/storage/schema.ts +5 -0
  373. package/src/storage/storage.ts +327 -0
  374. package/src/sync/README.md +179 -0
  375. package/src/sync/schema.ts +11 -0
  376. package/src/temporary.ts +31 -0
  377. package/src/tool/apply_patch.ts +313 -0
  378. package/src/tool/apply_patch.txt +33 -0
  379. package/src/tool/code-mode.ts +310 -0
  380. package/src/tool/edit.ts +737 -0
  381. package/src/tool/edit.txt +10 -0
  382. package/src/tool/external-directory.ts +49 -0
  383. package/src/tool/glob.ts +76 -0
  384. package/src/tool/glob.txt +6 -0
  385. package/src/tool/grep.ts +115 -0
  386. package/src/tool/grep.txt +8 -0
  387. package/src/tool/invalid.ts +21 -0
  388. package/src/tool/json-schema.ts +164 -0
  389. package/src/tool/lsp.ts +113 -0
  390. package/src/tool/lsp.txt +24 -0
  391. package/src/tool/mcp-websearch.ts +96 -0
  392. package/src/tool/plan-enter.txt +14 -0
  393. package/src/tool/plan-exit.txt +13 -0
  394. package/src/tool/plan.ts +79 -0
  395. package/src/tool/question.ts +44 -0
  396. package/src/tool/question.txt +10 -0
  397. package/src/tool/read.ts +386 -0
  398. package/src/tool/read.txt +14 -0
  399. package/src/tool/registry.ts +450 -0
  400. package/src/tool/schema.ts +14 -0
  401. package/src/tool/shell/id.ts +19 -0
  402. package/src/tool/shell/prompt.ts +293 -0
  403. package/src/tool/shell/shell.txt +21 -0
  404. package/src/tool/shell.ts +645 -0
  405. package/src/tool/skill.ts +70 -0
  406. package/src/tool/skill.txt +5 -0
  407. package/src/tool/task.ts +360 -0
  408. package/src/tool/task.txt +19 -0
  409. package/src/tool/todo.ts +46 -0
  410. package/src/tool/todowrite.txt +44 -0
  411. package/src/tool/tool.ts +183 -0
  412. package/src/tool/truncate.ts +156 -0
  413. package/src/tool/truncation-dir.ts +4 -0
  414. package/src/tool/webfetch.ts +192 -0
  415. package/src/tool/webfetch.txt +13 -0
  416. package/src/tool/websearch.ts +143 -0
  417. package/src/tool/websearch.txt +14 -0
  418. package/src/tool/write.ts +104 -0
  419. package/src/tool/write.txt +8 -0
  420. package/src/util/archive.ts +17 -0
  421. package/src/util/bom.ts +27 -0
  422. package/src/util/data-url.ts +9 -0
  423. package/src/util/defer.ts +10 -0
  424. package/src/util/effect-http-client.ts +11 -0
  425. package/src/util/error.ts +1 -0
  426. package/src/util/filesystem.ts +251 -0
  427. package/src/util/html.ts +8 -0
  428. package/src/util/iife.ts +3 -0
  429. package/src/util/lazy.ts +20 -0
  430. package/src/util/local-context.ts +25 -0
  431. package/src/util/locale.ts +2 -0
  432. package/src/util/media.ts +26 -0
  433. package/src/util/process.ts +177 -0
  434. package/src/util/proxy-env.ts +72 -0
  435. package/src/util/queue.ts +32 -0
  436. package/src/util/record.ts +1 -0
  437. package/src/util/repository.ts +232 -0
  438. package/src/util/rpc.ts +66 -0
  439. package/src/util/signal.ts +12 -0
  440. package/src/util/timeout.ts +13 -0
  441. package/src/util/token.ts +1 -0
  442. package/src/util/wildcard.ts +59 -0
  443. package/src/worktree/index.ts +623 -0
  444. package/sst-env.d.ts +10 -0
  445. package/test/AGENTS.md +204 -0
  446. package/test/EFFECT_TEST_MIGRATION.md +169 -0
  447. package/test/account/repo.test.ts +355 -0
  448. package/test/account/service.test.ts +503 -0
  449. package/test/acp/config-option.test.ts +229 -0
  450. package/test/acp/content.test.ts +235 -0
  451. package/test/acp/directory.test.ts +188 -0
  452. package/test/acp/error.test.ts +67 -0
  453. package/test/acp/event.test.ts +751 -0
  454. package/test/acp/permission.test.ts +401 -0
  455. package/test/acp/service-session.test.ts +1248 -0
  456. package/test/acp/session.test.ts +201 -0
  457. package/test/acp/tool.test.ts +298 -0
  458. package/test/acp/usage.test.ts +318 -0
  459. package/test/agent/agent.test.ts +755 -0
  460. package/test/agent/plan-mode-subagent-bypass.test.ts +160 -0
  461. package/test/agent/plugin-agent-regression.test.ts +51 -0
  462. package/test/auth/auth.test.ts +75 -0
  463. package/test/background/job.test.ts +244 -0
  464. package/test/cli/account.test.ts +30 -0
  465. package/test/cli/acp/acp-test-client.ts +97 -0
  466. package/test/cli/acp/config-options.test.ts +103 -0
  467. package/test/cli/acp/helpers.ts +96 -0
  468. package/test/cli/acp/initialize-auth.test.ts +61 -0
  469. package/test/cli/acp/lifecycle.test.ts +118 -0
  470. package/test/cli/acp/prompt-content.test.ts +97 -0
  471. package/test/cli/acp/skills.test.ts +38 -0
  472. package/test/cli/cmd/tui/attention.test.ts +484 -0
  473. package/test/cli/effect-cmd-instance-als.test.ts +40 -0
  474. package/test/cli/error.test.ts +95 -0
  475. package/test/cli/github-action.test.ts +199 -0
  476. package/test/cli/github-remote.test.ts +90 -0
  477. package/test/cli/help/__snapshots__/help-snapshots.test.ts.snap +623 -0
  478. package/test/cli/help/help-snapshots.test.ts +140 -0
  479. package/test/cli/import.test.ts +90 -0
  480. package/test/cli/mcp-add.test.ts +74 -0
  481. package/test/cli/plugin-auth-picker.test.ts +120 -0
  482. package/test/cli/run/entry.body.test.ts +536 -0
  483. package/test/cli/run/footer.menu.test.ts +43 -0
  484. package/test/cli/run/footer.view.test.tsx +1411 -0
  485. package/test/cli/run/footer.width.test.ts +35 -0
  486. package/test/cli/run/permission.shared.test.ts +144 -0
  487. package/test/cli/run/prompt.editor.test.ts +101 -0
  488. package/test/cli/run/prompt.shared.test.ts +101 -0
  489. package/test/cli/run/question.shared.test.ts +115 -0
  490. package/test/cli/run/run-process.test.ts +331 -0
  491. package/test/cli/run/runtime.boot.test.ts +283 -0
  492. package/test/cli/run/runtime.queue.test.ts +481 -0
  493. package/test/cli/run/runtime.stdin.test.ts +71 -0
  494. package/test/cli/run/runtime.test.ts +238 -0
  495. package/test/cli/run/scrollback.surface.test.ts +1092 -0
  496. package/test/cli/run/session-data.test.ts +593 -0
  497. package/test/cli/run/session-replay.test.ts +691 -0
  498. package/test/cli/run/session.shared.test.ts +247 -0
  499. package/test/cli/run/stream.test.ts +56 -0
  500. package/test/cli/run/stream.transport.test.ts +2363 -0
  501. package/test/cli/run/subagent-data.test.ts +547 -0
  502. package/test/cli/run/theme.test.ts +177 -0
  503. package/test/cli/run/variant.shared.test.ts +218 -0
  504. package/test/cli/serve/serve-process.test.ts +61 -0
  505. package/test/cli/smokes/read-only.test.ts +115 -0
  506. package/test/cli/tui/attach.test.ts +11 -0
  507. package/test/cli/tui/editor-context-zed.test.ts +379 -0
  508. package/test/cli/tui/editor-context.test.tsx +297 -0
  509. package/test/cli/tui/plugin-add.test.ts +110 -0
  510. package/test/cli/tui/plugin-install.test.ts +87 -0
  511. package/test/cli/tui/plugin-lifecycle.test.ts +224 -0
  512. package/test/cli/tui/plugin-loader-entrypoint.test.ts +485 -0
  513. package/test/cli/tui/plugin-loader-pure.test.ts +72 -0
  514. package/test/cli/tui/plugin-loader.test.ts +1332 -0
  515. package/test/cli/tui/plugin-toggle.test.ts +264 -0
  516. package/test/cli/tui/thread.test.ts +101 -0
  517. package/test/config/agent-color.test.ts +47 -0
  518. package/test/config/config.test.ts +2052 -0
  519. package/test/config/entry-name.test.ts +57 -0
  520. package/test/config/fixtures/empty-frontmatter.md +4 -0
  521. package/test/config/fixtures/frontmatter.md +28 -0
  522. package/test/config/fixtures/markdown-header.md +11 -0
  523. package/test/config/fixtures/no-frontmatter.md +1 -0
  524. package/test/config/fixtures/weird-model-id.md +13 -0
  525. package/test/config/lsp.test.ts +69 -0
  526. package/test/config/markdown.test.ts +228 -0
  527. package/test/config/plugin.test.ts +0 -0
  528. package/test/config/tui.test.ts +894 -0
  529. package/test/control-plane/adapters.test.ts +71 -0
  530. package/test/control-plane/workspace.test.ts +1701 -0
  531. package/test/effect/app-runtime-logger.test.ts +101 -0
  532. package/test/effect/config-service.test.ts +65 -0
  533. package/test/effect/instance-state.test.ts +392 -0
  534. package/test/effect/run-service.test.ts +89 -0
  535. package/test/effect/runner.test.ts +514 -0
  536. package/test/effect/runtime-flags.test.ts +374 -0
  537. package/test/event-manifest.test.ts +24 -0
  538. package/test/fake/account.ts +9 -0
  539. package/test/fake/auth.ts +8 -0
  540. package/test/fake/npm.ts +8 -0
  541. package/test/fake/provider.ts +82 -0
  542. package/test/fake/skill.ts +8 -0
  543. package/test/filesystem/filesystem.test.ts +318 -0
  544. package/test/fixture/agent-plugin.constants.ts +6 -0
  545. package/test/fixture/agent-plugin.ts +12 -0
  546. package/test/fixture/config.ts +23 -0
  547. package/test/fixture/db.ts +11 -0
  548. package/test/fixture/fixture.test.ts +26 -0
  549. package/test/fixture/fixture.ts +228 -0
  550. package/test/fixture/flag.ts +20 -0
  551. package/test/fixture/flock-worker.ts +72 -0
  552. package/test/fixture/lsp/fake-lsp-server.js +249 -0
  553. package/test/fixture/mcp-lifecycle-stdio.ts +26 -0
  554. package/test/fixture/mcp-session-recovery.ts +50 -0
  555. package/test/fixture/plug-worker.ts +93 -0
  556. package/test/fixture/plugin-meta-worker.ts +19 -0
  557. package/test/fixture/plugin.ts +10 -0
  558. package/test/fixture/skills/agents-sdk/SKILL.md +152 -0
  559. package/test/fixture/skills/agents-sdk/references/callable.md +92 -0
  560. package/test/fixture/skills/cloudflare/SKILL.md +211 -0
  561. package/test/fixture/skills/index.json +6 -0
  562. package/test/fixture/tui-environment.tsx +32 -0
  563. package/test/fixture/tui-plugin.ts +355 -0
  564. package/test/fixture/tui-runtime.ts +56 -0
  565. package/test/fixture/tui-sdk.ts +82 -0
  566. package/test/fixture/workspace.ts +34 -0
  567. package/test/fixtures/recordings/session/native-anthropic-tool-loop.json +49 -0
  568. package/test/fixtures/recordings/session/native-openai-oauth-tool-loop.json +45 -0
  569. package/test/fixtures/recordings/session/native-zen-tool-loop.json +49 -0
  570. package/test/format/format.test.ts +235 -0
  571. package/test/git/git.test.ts +179 -0
  572. package/test/ide/ide.test.ts +82 -0
  573. package/test/image/fixtures/picture-5mb-base64.png +0 -0
  574. package/test/image/image.test.ts +121 -0
  575. package/test/installation/installation.test.ts +240 -0
  576. package/test/lib/cli-process.ts +535 -0
  577. package/test/lib/effect.ts +177 -0
  578. package/test/lib/filesystem.ts +10 -0
  579. package/test/lib/llm-server.ts +779 -0
  580. package/test/lib/snapshot.ts +73 -0
  581. package/test/lib/test-provider.ts +37 -0
  582. package/test/lib/websocket.ts +46 -0
  583. package/test/lsp/client.test.ts +488 -0
  584. package/test/lsp/index.test.ts +231 -0
  585. package/test/lsp/jdtls-root.test.ts +459 -0
  586. package/test/lsp/launch.test.ts +22 -0
  587. package/test/lsp/lifecycle.test.ts +160 -0
  588. package/test/mcp/auth.test.ts +78 -0
  589. package/test/mcp/catalog.test.ts +107 -0
  590. package/test/mcp/headers.test.ts +102 -0
  591. package/test/mcp/lifecycle.test.ts +560 -0
  592. package/test/mcp/oauth-auto-connect.test.ts +300 -0
  593. package/test/mcp/oauth-browser.test.ts +225 -0
  594. package/test/mcp/oauth-callback.test.ts +114 -0
  595. package/test/mcp/oauth-provider.test.ts +102 -0
  596. package/test/mcp/session-recovery.test.ts +27 -0
  597. package/test/patch/patch.test.ts +384 -0
  598. package/test/permission/arity.test.ts +33 -0
  599. package/test/permission/next.test.ts +1174 -0
  600. package/test/permission-task.test.ts +319 -0
  601. package/test/plugin/auth-override.test.ts +101 -0
  602. package/test/plugin/cloudflare.test.ts +68 -0
  603. package/test/plugin/codex.test.ts +299 -0
  604. package/test/plugin/github-copilot-models.test.ts +424 -0
  605. package/test/plugin/install-concurrency.test.ts +140 -0
  606. package/test/plugin/install.test.ts +570 -0
  607. package/test/plugin/loader-shared.test.ts +1306 -0
  608. package/test/plugin/meta.test.ts +137 -0
  609. package/test/plugin/openai-rollout.test.ts +17 -0
  610. package/test/plugin/openai-ws.test.ts +884 -0
  611. package/test/plugin/shared.test.ts +88 -0
  612. package/test/plugin/snowflake-cortex.test.ts +278 -0
  613. package/test/plugin/trigger.test.ts +108 -0
  614. package/test/plugin/workspace-adapter.test.ts +111 -0
  615. package/test/plugin/xai.test.ts +620 -0
  616. package/test/preload.ts +92 -0
  617. package/test/project/instance-bootstrap.test.ts +115 -0
  618. package/test/project/instance.test.ts +248 -0
  619. package/test/project/migrate-global.test.ts +168 -0
  620. package/test/project/project-directory.test.ts +202 -0
  621. package/test/project/project.test.ts +808 -0
  622. package/test/project/vcs.test.ts +335 -0
  623. package/test/project/worktree-remove.test.ts +128 -0
  624. package/test/project/worktree.test.ts +324 -0
  625. package/test/provider/amazon-bedrock.test.ts +361 -0
  626. package/test/provider/cf-ai-gateway-e2e.test.ts +132 -0
  627. package/test/provider/digitalocean.test.ts +124 -0
  628. package/test/provider/gitlab-duo.test.ts +412 -0
  629. package/test/provider/header-timeout.test.ts +234 -0
  630. package/test/provider/model-status.test.ts +61 -0
  631. package/test/provider/provider.test.ts +2059 -0
  632. package/test/provider/transform.test.ts +5535 -0
  633. package/test/question/question.test.ts +459 -0
  634. package/test/server/AGENTS.md +15 -0
  635. package/test/server/auth.test.ts +59 -0
  636. package/test/server/global-bus.ts +31 -0
  637. package/test/server/global-session-list.test.ts +108 -0
  638. package/test/server/httpapi-authorization.test.ts +174 -0
  639. package/test/server/httpapi-compression.test.ts +151 -0
  640. package/test/server/httpapi-config.test.ts +110 -0
  641. package/test/server/httpapi-control-plane.test.ts +63 -0
  642. package/test/server/httpapi-cors-vary.test.ts +63 -0
  643. package/test/server/httpapi-cors.test.ts +122 -0
  644. package/test/server/httpapi-error-middleware.test.ts +101 -0
  645. package/test/server/httpapi-event.test.ts +94 -0
  646. package/test/server/httpapi-exercise/assertions.ts +64 -0
  647. package/test/server/httpapi-exercise/backend.ts +144 -0
  648. package/test/server/httpapi-exercise/dsl.ts +210 -0
  649. package/test/server/httpapi-exercise/environment.ts +40 -0
  650. package/test/server/httpapi-exercise/index.ts +1802 -0
  651. package/test/server/httpapi-exercise/report.ts +66 -0
  652. package/test/server/httpapi-exercise/routing.ts +96 -0
  653. package/test/server/httpapi-exercise/runner.ts +267 -0
  654. package/test/server/httpapi-exercise/runtime.ts +52 -0
  655. package/test/server/httpapi-exercise/types.ts +127 -0
  656. package/test/server/httpapi-experimental.test.ts +298 -0
  657. package/test/server/httpapi-file.test.ts +83 -0
  658. package/test/server/httpapi-global.test.ts +66 -0
  659. package/test/server/httpapi-instance-context.test.ts +337 -0
  660. package/test/server/httpapi-instance-route-auth.test.ts +81 -0
  661. package/test/server/httpapi-instance.test.ts +265 -0
  662. package/test/server/httpapi-layer.ts +33 -0
  663. package/test/server/httpapi-listen.test.ts +465 -0
  664. package/test/server/httpapi-mcp-oauth.test.ts +73 -0
  665. package/test/server/httpapi-mcp.test.ts +223 -0
  666. package/test/server/httpapi-mdns.test.ts +79 -0
  667. package/test/server/httpapi-promptasync-context.test.ts +212 -0
  668. package/test/server/httpapi-provider.test.ts +401 -0
  669. package/test/server/httpapi-pty.test.ts +299 -0
  670. package/test/server/httpapi-public-openapi.test.ts +350 -0
  671. package/test/server/httpapi-query-schema-drift.test.ts +330 -0
  672. package/test/server/httpapi-reference.test.ts +63 -0
  673. package/test/server/httpapi-schema-error-body.test.ts +166 -0
  674. package/test/server/httpapi-sdk.test.ts +913 -0
  675. package/test/server/httpapi-session.test.ts +1090 -0
  676. package/test/server/httpapi-sync.test.ts +149 -0
  677. package/test/server/httpapi-ui.test.ts +456 -0
  678. package/test/server/httpapi-v2-location.test.ts +126 -0
  679. package/test/server/httpapi-v2-pty.test.ts +250 -0
  680. package/test/server/httpapi-workspace-routing.test.ts +552 -0
  681. package/test/server/httpapi-workspace.test.ts +506 -0
  682. package/test/server/negative-tokens-regression.test.ts +84 -0
  683. package/test/server/project-copy.test.ts +127 -0
  684. package/test/server/project-init-git.test.ts +118 -0
  685. package/test/server/proxy-util.test.ts +113 -0
  686. package/test/server/sdk-error-shape.test.ts +81 -0
  687. package/test/server/sdk-v1-smoke.test.ts +57 -0
  688. package/test/server/session-actions.test.ts +110 -0
  689. package/test/server/session-diff-missing-patch.test.ts +97 -0
  690. package/test/server/session-list.test.ts +302 -0
  691. package/test/server/session-messages.test.ts +180 -0
  692. package/test/server/session-select.test.ts +67 -0
  693. package/test/server/workspace-proxy.test.ts +181 -0
  694. package/test/server/workspace-routing.test.ts +94 -0
  695. package/test/server/worktree-endpoint-repro.test.ts +307 -0
  696. package/test/session/compaction.test.ts +1819 -0
  697. package/test/session/instruction.test.ts +264 -0
  698. package/test/session/llm-native-recorded.test.ts +413 -0
  699. package/test/session/llm-native.test.ts +761 -0
  700. package/test/session/llm.test.ts +2115 -0
  701. package/test/session/message-v2.test.ts +1662 -0
  702. package/test/session/messages-pagination.test.ts +1057 -0
  703. package/test/session/processor-effect.test.ts +1067 -0
  704. package/test/session/prompt.test.ts +2402 -0
  705. package/test/session/retry.test.ts +440 -0
  706. package/test/session/revert-compact.test.ts +638 -0
  707. package/test/session/schema-decoding.test.ts +313 -0
  708. package/test/session/session-schema.test.ts +78 -0
  709. package/test/session/session.test.ts +253 -0
  710. package/test/session/snapshot-tool-race.test.ts +189 -0
  711. package/test/session/structured-output-integration.test.ts +235 -0
  712. package/test/session/structured-output.test.ts +387 -0
  713. package/test/session/system.test.ts +149 -0
  714. package/test/share/share-next.test.ts +324 -0
  715. package/test/skill/discovery.test.ts +186 -0
  716. package/test/skill/skill.test.ts +585 -0
  717. package/test/snapshot/snapshot.test.ts +1218 -0
  718. package/test/storage/storage.test.ts +297 -0
  719. package/test/tool/__snapshots__/parameters.test.ts.snap +466 -0
  720. package/test/tool/__snapshots__/tool.test.ts.snap +9 -0
  721. package/test/tool/apply_patch.test.ts +529 -0
  722. package/test/tool/code-mode-integration.test.ts +331 -0
  723. package/test/tool/code-mode.test.ts +730 -0
  724. package/test/tool/edit.test.ts +574 -0
  725. package/test/tool/external-directory.test.ts +156 -0
  726. package/test/tool/fixtures/large-image.png +0 -0
  727. package/test/tool/fixtures/models-api.json +172692 -0
  728. package/test/tool/glob.test.ts +132 -0
  729. package/test/tool/grep.test.ts +223 -0
  730. package/test/tool/lsp.test.ts +184 -0
  731. package/test/tool/parameters.test.ts +290 -0
  732. package/test/tool/question.test.ts +133 -0
  733. package/test/tool/read.test.ts +608 -0
  734. package/test/tool/registry.test.ts +572 -0
  735. package/test/tool/shell.test.ts +1199 -0
  736. package/test/tool/skill.test.ts +134 -0
  737. package/test/tool/task.test.ts +985 -0
  738. package/test/tool/tool-define.test.ts +154 -0
  739. package/test/tool/truncation.test.ts +264 -0
  740. package/test/tool/webfetch.test.ts +119 -0
  741. package/test/tool/websearch.test.ts +99 -0
  742. package/test/tool/write.test.ts +279 -0
  743. package/test/util/data-url.test.ts +14 -0
  744. package/test/util/error.test.ts +16 -0
  745. package/test/util/filesystem.test.ts +656 -0
  746. package/test/util/glob.test.ts +164 -0
  747. package/test/util/html.test.ts +15 -0
  748. package/test/util/iife.test.ts +36 -0
  749. package/test/util/lazy.test.ts +50 -0
  750. package/test/util/module.test.ts +59 -0
  751. package/test/util/process.test.ts +128 -0
  752. package/test/util/repository.test.ts +93 -0
  753. package/test/util/timeout.test.ts +21 -0
  754. package/test/util/wildcard.test.ts +90 -0
  755. package/test/v2/session-message-updater.test.ts +269 -0
  756. package/tsconfig.json +16 -0
@@ -0,0 +1,2006 @@
1
+ import { LayerNode } from "@neurocode-ai/core/effect/layer-node"
2
+ import os from "os"
3
+ import { ConfigV1 } from "@neurocode-ai/core/v1/config/config"
4
+ import fuzzysort from "fuzzysort"
5
+ import { Config } from "@/config/config"
6
+ import { mapValues, mergeDeep, omit, pickBy, sortBy } from "remeda"
7
+ import { NoSuchModelError, type Provider as SDK } from "ai"
8
+ import { Npm } from "@neurocode-ai/core/npm"
9
+ import { Hash } from "@neurocode-ai/core/util/hash"
10
+ import { Plugin } from "../plugin"
11
+ import { serviceUse } from "@neurocode-ai/core/effect/service-use"
12
+ import { type LanguageModelV3 } from "@ai-sdk/provider"
13
+ import { ModelsDev } from "@neurocode-ai/core/models-dev"
14
+ import { Auth } from "../auth"
15
+ import { Env } from "../env"
16
+ import { InstallationVersion } from "@neurocode-ai/core/installation/version"
17
+ import { iife } from "@/util/iife"
18
+ import { Global } from "@neurocode-ai/core/global"
19
+ import path from "path"
20
+ import { pathToFileURL } from "url"
21
+ import { Effect, Layer, Context, Schema, Types } from "effect"
22
+ import { EffectBridge } from "@/effect/bridge"
23
+ import { InstanceState } from "@/effect/instance-state"
24
+ import { EffectPromise } from "@/effect/promise"
25
+ import { FSUtil } from "@neurocode-ai/core/fs-util"
26
+ import { isRecord } from "@/util/record"
27
+ import { optional } from "@neurocode-ai/core/schema"
28
+ import { ProviderTransform } from "./transform"
29
+ import { ProviderV2 } from "@neurocode-ai/core/provider"
30
+ import { ModelV2 } from "@neurocode-ai/core/model"
31
+ import { ModelStatus } from "./model-status"
32
+ import { RuntimeFlags } from "@/effect/runtime-flags"
33
+ import { ProviderError } from "./error"
34
+
35
+ const OPENAI_HEADER_TIMEOUT_DEFAULT = 300_000
36
+
37
+ function wrapSSE(res: Response, ms: number, ctl: AbortController) {
38
+ if (typeof ms !== "number" || ms <= 0) return res
39
+ if (!res.body) return res
40
+ if (!res.headers.get("content-type")?.includes("text/event-stream")) return res
41
+
42
+ const reader = res.body.getReader()
43
+ const body = new ReadableStream<Uint8Array>({
44
+ async pull(ctrl) {
45
+ const part = await new Promise<Awaited<ReturnType<typeof reader.read>>>((resolve, reject) => {
46
+ const id = setTimeout(() => {
47
+ const err = new ProviderError.ResponseStreamError("SSE read timed out")
48
+ ctl.abort(err)
49
+ void reader.cancel(err)
50
+ reject(err)
51
+ }, ms)
52
+
53
+ reader.read().then(
54
+ (part) => {
55
+ clearTimeout(id)
56
+ resolve(part)
57
+ },
58
+ (err) => {
59
+ clearTimeout(id)
60
+ reject(err)
61
+ },
62
+ )
63
+ })
64
+
65
+ if (part.done) {
66
+ ctrl.close()
67
+ return
68
+ }
69
+
70
+ ctrl.enqueue(part.value)
71
+ },
72
+ async cancel(reason) {
73
+ ctl.abort(reason)
74
+ await reader.cancel(reason)
75
+ },
76
+ })
77
+
78
+ return new Response(body, {
79
+ headers: new Headers(res.headers),
80
+ status: res.status,
81
+ statusText: res.statusText,
82
+ })
83
+ }
84
+
85
+ function timeoutController(ms: number) {
86
+ const ctl = new AbortController()
87
+ const id = setTimeout(() => ctl.abort(new ProviderError.HeaderTimeoutError(ms)), ms)
88
+ return {
89
+ signal: ctl.signal,
90
+ clear: () => clearTimeout(id),
91
+ }
92
+ }
93
+
94
+ function googleVertexAnthropicBaseURL(project: string | undefined, location: string | undefined) {
95
+ if (!project) return
96
+ if (location !== "eu" && location !== "us") return
97
+ // Continental multi-regions require Regional Endpoint Platform domains.
98
+ return `https://aiplatform.${location}.rep.googleapis.com/v1/projects/${project}/locations/${location}/publishers/anthropic/models`
99
+ }
100
+
101
+ type BundledSDK = {
102
+ languageModel(modelId: string): LanguageModelV3
103
+ chat?: (modelId: string) => LanguageModelV3
104
+ responses?: (modelId: string) => LanguageModelV3
105
+ }
106
+
107
+ const BUNDLED_PROVIDERS: Record<string, () => Promise<(opts: any) => BundledSDK>> = {
108
+ "@ai-sdk/amazon-bedrock": () => import("@ai-sdk/amazon-bedrock").then((m) => m.createAmazonBedrock),
109
+ "@ai-sdk/amazon-bedrock/mantle": () => import("@ai-sdk/amazon-bedrock/mantle").then((m) => m.createBedrockMantle),
110
+ "@ai-sdk/anthropic": () => import("@ai-sdk/anthropic").then((m) => m.createAnthropic),
111
+ "@ai-sdk/azure": () => import("@ai-sdk/azure").then((m) => m.createAzure),
112
+ "@ai-sdk/google": () => import("@ai-sdk/google").then((m) => m.createGoogleGenerativeAI),
113
+ "@ai-sdk/google-vertex": () => import("@ai-sdk/google-vertex").then((m) => m.createVertex),
114
+ "@ai-sdk/google-vertex/anthropic": () =>
115
+ import("@ai-sdk/google-vertex/anthropic").then((m) => m.createVertexAnthropic),
116
+ "@ai-sdk/openai": () => import("@ai-sdk/openai").then((m) => m.createOpenAI),
117
+ "@ai-sdk/openai-compatible": () => import("@ai-sdk/openai-compatible").then((m) => m.createOpenAICompatible),
118
+ "@openrouter/ai-sdk-provider": () => import("@openrouter/ai-sdk-provider").then((m) => m.createOpenRouter),
119
+ "@ai-sdk/xai": () => import("@ai-sdk/xai").then((m) => m.createXai),
120
+ "@ai-sdk/mistral": () => import("@ai-sdk/mistral").then((m) => m.createMistral),
121
+ "@ai-sdk/groq": () => import("@ai-sdk/groq").then((m) => m.createGroq),
122
+ "@ai-sdk/deepinfra": () => import("@ai-sdk/deepinfra").then((m) => m.createDeepInfra),
123
+ "@ai-sdk/cerebras": () => import("@ai-sdk/cerebras").then((m) => m.createCerebras),
124
+ "@ai-sdk/cohere": () => import("@ai-sdk/cohere").then((m) => m.createCohere),
125
+ "@ai-sdk/gateway": () => import("@ai-sdk/gateway").then((m) => m.createGateway),
126
+ "@ai-sdk/togetherai": () => import("@ai-sdk/togetherai").then((m) => m.createTogetherAI),
127
+ "@ai-sdk/perplexity": () => import("@ai-sdk/perplexity").then((m) => m.createPerplexity),
128
+ "@ai-sdk/vercel": () => import("@ai-sdk/vercel").then((m) => m.createVercel),
129
+ "@ai-sdk/alibaba": () => import("@ai-sdk/alibaba").then((m) => m.createAlibaba),
130
+ "gitlab-ai-provider": () => import("gitlab-ai-provider").then((m) => m.createGitLab),
131
+ "@ai-sdk/github-copilot": () =>
132
+ import("@neurocode-ai/core/github-copilot/copilot-provider").then((m) => m.createOpenaiCompatible),
133
+ "venice-ai-sdk-provider": () => import("venice-ai-sdk-provider").then((m) => m.createVenice),
134
+ }
135
+
136
+ type CustomModelLoader = (sdk: any, modelID: string, options?: Record<string, any>, model?: Model) => Promise<any>
137
+ type CustomVarsLoader = (options: Record<string, any>) => Record<string, string>
138
+ type CustomDiscoverModels = () => Promise<Record<string, Model>>
139
+ type CustomLoader = (provider: Info) => Effect.Effect<{
140
+ autoload: boolean
141
+ getModel?: CustomModelLoader
142
+ vars?: CustomVarsLoader
143
+ options?: Record<string, any>
144
+ discoverModels?: CustomDiscoverModels
145
+ }>
146
+
147
+ type CustomDep = {
148
+ auth: (id: string) => Effect.Effect<Auth.Info | undefined>
149
+ config: () => Effect.Effect<ConfigV1.Info>
150
+ env: () => Effect.Effect<Record<string, string | undefined>>
151
+ get: (key: string) => Effect.Effect<string | undefined>
152
+ }
153
+
154
+ function selectAzureLanguageModel(sdk: any, modelID: string, useChat: boolean) {
155
+ if (useChat && sdk.chat) return sdk.chat(modelID)
156
+ if (sdk.responses) return sdk.responses(modelID)
157
+ if (sdk.messages) return sdk.messages(modelID)
158
+ if (sdk.chat) return sdk.chat(modelID)
159
+ return sdk.languageModel(modelID)
160
+ }
161
+
162
+ function selectBedrockMantleLanguageModel(sdk: BundledSDK, modelID: string) {
163
+ if (modelID === "openai.gpt-oss-safeguard-20b" || modelID === "openai.gpt-oss-safeguard-120b")
164
+ return sdk.chat?.(modelID) ?? sdk.languageModel(modelID)
165
+ return sdk.responses?.(modelID) ?? sdk.languageModel(modelID)
166
+ }
167
+
168
+ function custom(dep: CustomDep): Record<string, CustomLoader> {
169
+ return {
170
+ anthropic: () =>
171
+ Effect.succeed({
172
+ autoload: false,
173
+ options: {
174
+ headers: {
175
+ "anthropic-beta": "interleaved-thinking-2025-05-14,fine-grained-tool-streaming-2025-05-14",
176
+ },
177
+ },
178
+ }),
179
+ opencode: Effect.fnUntraced(function* (input: Info) {
180
+ const env = yield* dep.env()
181
+ const hasKey = iife(() => {
182
+ if (input.env.some((item) => env[item])) return true
183
+ return false
184
+ })
185
+ const ok =
186
+ hasKey ||
187
+ Boolean(yield* dep.auth(input.id)) ||
188
+ Boolean((yield* dep.config()).provider?.["opencode"]?.options?.apiKey)
189
+
190
+ if (!ok) {
191
+ for (const [key, value] of Object.entries(input.models)) {
192
+ if (value.cost.input === 0) continue
193
+ delete input.models[key]
194
+ }
195
+ }
196
+
197
+ return {
198
+ autoload: Object.keys(input.models).length > 0,
199
+ options: ok ? {} : { apiKey: "public" },
200
+ }
201
+ }),
202
+ openai: () =>
203
+ Effect.succeed({
204
+ autoload: false,
205
+ async getModel(sdk: any, modelID: string, _options?: Record<string, any>) {
206
+ return sdk.responses(modelID)
207
+ },
208
+ options: { headerTimeout: OPENAI_HEADER_TIMEOUT_DEFAULT },
209
+ }),
210
+ meta: () =>
211
+ Effect.succeed({
212
+ autoload: false,
213
+ async getModel(sdk: any, modelID: string, _options?: Record<string, any>) {
214
+ return sdk.responses(modelID)
215
+ },
216
+ }),
217
+ xai: () =>
218
+ Effect.succeed({
219
+ autoload: false,
220
+ async getModel(sdk: any, modelID: string, _options?: Record<string, any>) {
221
+ return sdk.responses(modelID)
222
+ },
223
+ options: {},
224
+ }),
225
+ "github-copilot": () =>
226
+ Effect.succeed({
227
+ autoload: false,
228
+ async getModel(sdk: any, modelID: string, _options?: Record<string, any>, model?: Model) {
229
+ if (sdk.responses === undefined && sdk.chat === undefined) return sdk.languageModel(modelID)
230
+ if (model && "endpoint" in model.api) {
231
+ if (model.api.endpoint === "responses" && sdk.responses) return sdk.responses(modelID)
232
+ if (model.api.endpoint === "chat" && sdk.chat) return sdk.chat(modelID)
233
+ }
234
+ const match = /^gpt-(\d+)/.exec(modelID)
235
+ if (match && Number(match[1]) >= 5 && !modelID.startsWith("gpt-5-mini")) return sdk.responses(modelID)
236
+ return sdk.chat(modelID)
237
+ },
238
+ options: {},
239
+ }),
240
+ azure: Effect.fnUntraced(function* (provider: Info) {
241
+ const env = yield* dep.env()
242
+ const auth = yield* dep.auth(provider.id)
243
+ const resource = iife(() => {
244
+ return [
245
+ provider.options?.resourceName,
246
+ auth?.type === "api" ? auth.metadata?.resourceName : undefined,
247
+ env["AZURE_RESOURCE_NAME"],
248
+ ].find((name) => typeof name === "string" && name.trim() !== "")
249
+ })
250
+
251
+ if (!resource && !provider.options?.baseURL) {
252
+ return {
253
+ autoload: false,
254
+ async getModel() {
255
+ throw new Error(
256
+ "AZURE_RESOURCE_NAME is missing, set it using env var or reconnecting the azure provider and setting it",
257
+ )
258
+ },
259
+ }
260
+ }
261
+
262
+ return {
263
+ autoload: false,
264
+ async getModel(sdk: any, modelID: string, options?: Record<string, any>) {
265
+ return selectAzureLanguageModel(sdk, modelID, Boolean(options?.["useCompletionUrls"]))
266
+ },
267
+ options: {
268
+ resourceName: resource,
269
+ },
270
+ vars(_options): Record<string, string> {
271
+ if (resource) {
272
+ return {
273
+ AZURE_RESOURCE_NAME: resource,
274
+ }
275
+ }
276
+ return {}
277
+ },
278
+ }
279
+ }),
280
+ "azure-cognitive-services": Effect.fnUntraced(function* (provider: Info) {
281
+ const resourceName = yield* dep.get("AZURE_COGNITIVE_SERVICES_RESOURCE_NAME")
282
+ return {
283
+ autoload: false,
284
+ async getModel(sdk: any, modelID: string, options?: Record<string, any>) {
285
+ return selectAzureLanguageModel(sdk, modelID, Boolean(options?.["useCompletionUrls"]))
286
+ },
287
+ options: {
288
+ baseURL: resourceName
289
+ ? `https://${resourceName}.cognitiveservices.azure.com/openai${provider.options?.useDeploymentBasedUrls ? "" : "/v1"}`
290
+ : undefined,
291
+ },
292
+ }
293
+ }),
294
+ "amazon-bedrock": Effect.fnUntraced(function* () {
295
+ const providerConfig = (yield* dep.config()).provider?.["amazon-bedrock"]
296
+ const auth = yield* dep.auth("amazon-bedrock")
297
+ const env = yield* dep.env()
298
+
299
+ // Region precedence: 1) config file, 2) env var, 3) default
300
+ const configRegion = providerConfig?.options?.region
301
+ const envRegion = env["AWS_REGION"]
302
+ const defaultRegion = configRegion ?? envRegion ?? "us-east-1"
303
+
304
+ // Profile: config file takes precedence over env var
305
+ const configProfile = providerConfig?.options?.profile
306
+ const envProfile = env["AWS_PROFILE"]
307
+ const profile = configProfile ?? envProfile
308
+
309
+ const awsAccessKeyId = env["AWS_ACCESS_KEY_ID"]
310
+ const configApiKey = providerConfig?.options?.apiKey
311
+
312
+ // TODO: Using process.env directly because Env.set only updates a process.env shallow copy,
313
+ // until the scope of the Env API is clarified (test only or runtime?)
314
+ const awsBearerToken = iife(() => {
315
+ const envToken = process.env.AWS_BEARER_TOKEN_BEDROCK
316
+ if (envToken) return envToken
317
+ if (auth?.type === "api") {
318
+ process.env.AWS_BEARER_TOKEN_BEDROCK = auth.key
319
+ return auth.key
320
+ }
321
+ return undefined
322
+ })
323
+
324
+ const awsWebIdentityTokenFile = env["AWS_WEB_IDENTITY_TOKEN_FILE"]
325
+
326
+ const containerCreds = Boolean(
327
+ process.env.AWS_CONTAINER_CREDENTIALS_RELATIVE_URI || process.env.AWS_CONTAINER_CREDENTIALS_FULL_URI,
328
+ )
329
+
330
+ if (
331
+ !profile &&
332
+ !awsAccessKeyId &&
333
+ !awsBearerToken &&
334
+ !configApiKey &&
335
+ !awsWebIdentityTokenFile &&
336
+ !containerCreds
337
+ )
338
+ return { autoload: false }
339
+
340
+ const { fromNodeProviderChain } = yield* Effect.promise(() => import("@aws-sdk/credential-providers"))
341
+
342
+ const providerOptions: Record<string, any> = {
343
+ region: defaultRegion,
344
+ }
345
+
346
+ // Only use credential chain if no bearer token exists
347
+ // Bearer token takes precedence over credential chain (profiles, access keys, IAM roles, web identity tokens)
348
+ if (!awsBearerToken && !configApiKey) {
349
+ // Build credential provider options (only pass profile if specified)
350
+ const credentialProviderOptions = profile ? { profile } : {}
351
+
352
+ providerOptions.credentialProvider = fromNodeProviderChain(credentialProviderOptions)
353
+ }
354
+
355
+ // Add custom endpoint if specified (endpoint takes precedence over baseURL)
356
+ const endpoint = providerConfig?.options?.endpoint ?? providerConfig?.options?.baseURL
357
+ if (endpoint) {
358
+ providerOptions.baseURL = endpoint
359
+ }
360
+
361
+ return {
362
+ autoload: true,
363
+ options: providerOptions,
364
+ vars(options: Record<string, any>) {
365
+ return { AWS_REGION: options.region ?? defaultRegion }
366
+ },
367
+ async getModel(sdk: any, modelID: string, options?: Record<string, any>, model?: Model) {
368
+ if (model?.api.npm === "@ai-sdk/amazon-bedrock/mantle") return selectBedrockMantleLanguageModel(sdk, modelID)
369
+
370
+ // Skip region prefixing if model already has a cross-region inference profile prefix
371
+ // Models from models.dev may already include prefixes like us., eu., global., etc.
372
+ const crossRegionPrefixes = ["global.", "us.", "eu.", "jp.", "apac.", "au."]
373
+ if (crossRegionPrefixes.some((prefix) => modelID.startsWith(prefix))) {
374
+ return sdk.languageModel(modelID)
375
+ }
376
+
377
+ // Region resolution precedence (highest to lowest):
378
+ // 1. options.region from opencode.json provider config
379
+ // 2. defaultRegion from AWS_REGION environment variable
380
+ // 3. Default "us-east-1" (baked into defaultRegion)
381
+ const region = options?.region ?? defaultRegion
382
+
383
+ let regionPrefix = region.split("-")[0]
384
+
385
+ switch (regionPrefix) {
386
+ case "us": {
387
+ const modelRequiresPrefix = [
388
+ "nova-micro",
389
+ "nova-lite",
390
+ "nova-pro",
391
+ "nova-premier",
392
+ "nova-2",
393
+ "claude",
394
+ "deepseek",
395
+ ].some((m) => modelID.includes(m))
396
+ const isGovCloud = region.startsWith("us-gov")
397
+ if (modelRequiresPrefix && !isGovCloud) {
398
+ modelID = `${regionPrefix}.${modelID}`
399
+ }
400
+ break
401
+ }
402
+ case "eu": {
403
+ const regionRequiresPrefix = [
404
+ "eu-west-1",
405
+ "eu-west-2",
406
+ "eu-west-3",
407
+ "eu-north-1",
408
+ "eu-central-1",
409
+ "eu-south-1",
410
+ "eu-south-2",
411
+ ].some((r) => region.includes(r))
412
+ const modelRequiresPrefix = ["claude", "nova-lite", "nova-micro", "llama3", "pixtral"].some((m) =>
413
+ modelID.includes(m),
414
+ )
415
+ if (regionRequiresPrefix && modelRequiresPrefix) {
416
+ modelID = `${regionPrefix}.${modelID}`
417
+ }
418
+ break
419
+ }
420
+ case "ap": {
421
+ const isAustraliaRegion = ["ap-southeast-2", "ap-southeast-4"].includes(region)
422
+ const isTokyoRegion = region === "ap-northeast-1"
423
+ if (
424
+ isAustraliaRegion &&
425
+ ["anthropic.claude-sonnet-4-5", "anthropic.claude-haiku"].some((m) => modelID.includes(m))
426
+ ) {
427
+ regionPrefix = "au"
428
+ modelID = `${regionPrefix}.${modelID}`
429
+ } else if (isTokyoRegion) {
430
+ // Tokyo region uses jp. prefix for cross-region inference
431
+ const modelRequiresPrefix = ["claude", "nova-lite", "nova-micro", "nova-pro"].some((m) =>
432
+ modelID.includes(m),
433
+ )
434
+ if (modelRequiresPrefix) {
435
+ regionPrefix = "jp"
436
+ modelID = `${regionPrefix}.${modelID}`
437
+ }
438
+ } else {
439
+ // Other APAC regions use apac. prefix
440
+ const modelRequiresPrefix = ["claude", "nova-lite", "nova-micro", "nova-pro"].some((m) =>
441
+ modelID.includes(m),
442
+ )
443
+ if (modelRequiresPrefix) {
444
+ regionPrefix = "apac"
445
+ modelID = `${regionPrefix}.${modelID}`
446
+ }
447
+ }
448
+ break
449
+ }
450
+ }
451
+
452
+ return sdk.languageModel(modelID)
453
+ },
454
+ }
455
+ }),
456
+ llmgateway: () =>
457
+ Effect.succeed({
458
+ autoload: false,
459
+ options: {
460
+ headers: {
461
+ "HTTP-Referer": "https://opencode.ai/",
462
+ "X-Title": "opencode",
463
+ "X-Source": "opencode",
464
+ },
465
+ },
466
+ }),
467
+ openrouter: () =>
468
+ Effect.succeed({
469
+ autoload: false,
470
+ options: {
471
+ headers: {
472
+ "HTTP-Referer": "https://opencode.ai/",
473
+ "X-Title": "opencode",
474
+ },
475
+ },
476
+ }),
477
+ nvidia: (provider) =>
478
+ Effect.succeed({
479
+ autoload: provider.source === "config",
480
+ options: {
481
+ headers: {
482
+ "HTTP-Referer": "https://opencode.ai/",
483
+ "X-Title": "opencode",
484
+ "X-BILLING-INVOKE-ORIGIN": "OpenCode",
485
+ },
486
+ },
487
+ }),
488
+ vercel: () =>
489
+ Effect.succeed({
490
+ autoload: false,
491
+ options: {
492
+ headers: {
493
+ "http-referer": "https://opencode.ai/",
494
+ "x-title": "opencode",
495
+ },
496
+ },
497
+ }),
498
+ "google-vertex": Effect.fnUntraced(function* (provider: Info) {
499
+ const env = yield* dep.env()
500
+ // models.dev advertises GOOGLE_VERTEX_PROJECT for Vertex; keep the wider
501
+ // Google Cloud project env names as fallbacks for existing ADC setups.
502
+ const project =
503
+ provider.options?.project ??
504
+ env["GOOGLE_VERTEX_PROJECT"] ??
505
+ env["GOOGLE_CLOUD_PROJECT"] ??
506
+ env["GCP_PROJECT"] ??
507
+ env["GCLOUD_PROJECT"]
508
+
509
+ const location = String(
510
+ provider.options?.location ??
511
+ env["GOOGLE_VERTEX_LOCATION"] ??
512
+ env["GOOGLE_CLOUD_LOCATION"] ??
513
+ env["VERTEX_LOCATION"] ??
514
+ "us-central1",
515
+ )
516
+
517
+ const autoload = Boolean(project)
518
+ if (!autoload) return { autoload: false }
519
+ return {
520
+ autoload: true,
521
+ vars(_options: Record<string, any>) {
522
+ const endpoint = location === "global" ? "aiplatform.googleapis.com" : `${location}-aiplatform.googleapis.com`
523
+ return {
524
+ ...(project && { GOOGLE_VERTEX_PROJECT: project }),
525
+ GOOGLE_VERTEX_LOCATION: location,
526
+ GOOGLE_VERTEX_ENDPOINT: endpoint,
527
+ }
528
+ },
529
+ options: {
530
+ project,
531
+ location,
532
+ fetch: async (input: RequestInfo | URL, init?: RequestInit) => {
533
+ const { GoogleAuth } = await import("google-auth-library")
534
+ const auth = new GoogleAuth({ scopes: ["https://www.googleapis.com/auth/cloud-platform"] })
535
+ const client = await auth.getClient()
536
+ const token = await client.getAccessToken()
537
+
538
+ const headers = new Headers(init?.headers)
539
+ headers.set("Authorization", `Bearer ${token.token}`)
540
+
541
+ return fetch(input, { ...init, headers })
542
+ },
543
+ },
544
+ async getModel(sdk: any, modelID: string) {
545
+ const id = String(modelID).trim()
546
+ return sdk.languageModel(id)
547
+ },
548
+ }
549
+ }),
550
+ "google-vertex-anthropic": Effect.fnUntraced(function* () {
551
+ const env = yield* dep.env()
552
+ const project = env["GOOGLE_CLOUD_PROJECT"] ?? env["GCP_PROJECT"] ?? env["GCLOUD_PROJECT"]
553
+ const location = env["GOOGLE_CLOUD_LOCATION"] ?? env["VERTEX_LOCATION"] ?? "global"
554
+ const autoload = Boolean(project)
555
+ if (!autoload) return { autoload: false }
556
+ const baseURL = googleVertexAnthropicBaseURL(project, location)
557
+ return {
558
+ autoload: true,
559
+ options: {
560
+ project,
561
+ location,
562
+ ...(baseURL && { baseURL }),
563
+ },
564
+ async getModel(sdk: any, modelID) {
565
+ const id = String(modelID).trim()
566
+ return sdk.languageModel(id)
567
+ },
568
+ }
569
+ }),
570
+ "sap-ai-core": Effect.fnUntraced(function* () {
571
+ const auth = yield* dep.auth("sap-ai-core")
572
+ // TODO: Using process.env directly because Env.set only updates a shallow copy (not process.env),
573
+ // until the scope of the Env API is clarified (test only or runtime?)
574
+ const envServiceKey = iife(() => {
575
+ const envAICoreServiceKey = process.env.AICORE_SERVICE_KEY
576
+ if (envAICoreServiceKey) return envAICoreServiceKey
577
+ if (auth?.type === "api") {
578
+ process.env.AICORE_SERVICE_KEY = auth.key
579
+ return auth.key
580
+ }
581
+ return undefined
582
+ })
583
+ const deploymentId = process.env.AICORE_DEPLOYMENT_ID
584
+ const resourceGroup = process.env.AICORE_RESOURCE_GROUP
585
+
586
+ return {
587
+ autoload: !!envServiceKey,
588
+ options: envServiceKey ? { deploymentId, resourceGroup } : {},
589
+ async getModel(sdk: any, modelID: string) {
590
+ return sdk(modelID)
591
+ },
592
+ }
593
+ }),
594
+ zenmux: () =>
595
+ Effect.succeed({
596
+ autoload: false,
597
+ options: {
598
+ headers: {
599
+ "HTTP-Referer": "https://opencode.ai/",
600
+ "X-Title": "opencode",
601
+ },
602
+ },
603
+ }),
604
+ gitlab: Effect.fnUntraced(function* (input: Info) {
605
+ const {
606
+ VERSION: GITLAB_PROVIDER_VERSION,
607
+ isWorkflowModel,
608
+ discoverWorkflowModels,
609
+ } = yield* Effect.promise(() => import("gitlab-ai-provider"))
610
+
611
+ const instanceUrl = (yield* dep.get("GITLAB_INSTANCE_URL")) || "https://gitlab.com"
612
+
613
+ const auth = yield* dep.auth(input.id)
614
+ const apiKey = auth?.type === "oauth" ? auth.access : auth?.type === "api" ? auth.key : undefined
615
+ const token = apiKey ?? (yield* dep.get("GITLAB_TOKEN"))
616
+
617
+ const providerConfig = (yield* dep.config()).provider?.["gitlab"]
618
+ const directory = yield* InstanceState.directory
619
+
620
+ const aiGatewayHeaders = {
621
+ "User-Agent": `opencode/${InstallationVersion} gitlab-ai-provider/${GITLAB_PROVIDER_VERSION} (${os.platform()} ${os.release()}; ${os.arch()})`,
622
+ "anthropic-beta": "context-1m-2025-08-07",
623
+ ...providerConfig?.options?.aiGatewayHeaders,
624
+ }
625
+
626
+ const featureFlags = {
627
+ duo_agent_platform_agentic_chat: true,
628
+ duo_agent_platform: true,
629
+ ...providerConfig?.options?.featureFlags,
630
+ }
631
+
632
+ return {
633
+ autoload: !!token,
634
+ options: {
635
+ instanceUrl,
636
+ apiKey: token,
637
+ aiGatewayHeaders,
638
+ featureFlags,
639
+ },
640
+ async getModel(sdk: any, modelID: string, options?: Record<string, any>) {
641
+ if (modelID.startsWith("duo-workflow-")) {
642
+ const workflowRef = typeof options?.workflowRef === "string" ? options.workflowRef : undefined
643
+ // Use the static mapping if it exists, otherwise use duo-workflow with selectedModelRef
644
+ const sdkModelID = isWorkflowModel(modelID) ? modelID : "duo-workflow"
645
+ const workflowDefinition =
646
+ typeof options?.workflowDefinition === "string" ? options.workflowDefinition : undefined
647
+ const model = sdk.workflowChat(sdkModelID, {
648
+ featureFlags,
649
+ workflowDefinition,
650
+ })
651
+ if (workflowRef) {
652
+ model.selectedModelRef = workflowRef
653
+ }
654
+ return model
655
+ }
656
+ return sdk.agenticChat(modelID, {
657
+ aiGatewayHeaders,
658
+ featureFlags,
659
+ })
660
+ },
661
+ async discoverModels(): Promise<Record<string, Model>> {
662
+ if (!apiKey) {
663
+ return {}
664
+ }
665
+
666
+ try {
667
+ const token = apiKey
668
+ const getHeaders = (): Record<string, string> =>
669
+ auth?.type === "api" ? { "PRIVATE-TOKEN": token } : { Authorization: `Bearer ${token}` }
670
+
671
+ const result = await discoverWorkflowModels({ instanceUrl, getHeaders }, { workingDirectory: directory })
672
+
673
+ if (!result.models.length) {
674
+ return {}
675
+ }
676
+
677
+ const models: Record<string, Model> = {}
678
+ for (const m of result.models) {
679
+ if (!input.models[m.id]) {
680
+ models[m.id] = {
681
+ id: ModelV2.ID.make(m.id),
682
+ providerID: ProviderV2.ID.make("gitlab"),
683
+ name: `Agent Platform (${m.name})`,
684
+ family: "",
685
+ api: {
686
+ id: m.id,
687
+ url: instanceUrl,
688
+ npm: "gitlab-ai-provider",
689
+ },
690
+ status: "active",
691
+ headers: {},
692
+ options: { workflowRef: m.ref },
693
+ cost: { input: 0, output: 0, cache: { read: 0, write: 0 } },
694
+ limit: { context: m.context, output: m.output },
695
+ capabilities: {
696
+ temperature: false,
697
+ reasoning: true,
698
+ attachment: true,
699
+ toolcall: true,
700
+ input: {
701
+ text: true,
702
+ audio: false,
703
+ image: true,
704
+ video: false,
705
+ pdf: true,
706
+ },
707
+ output: {
708
+ text: true,
709
+ audio: false,
710
+ image: false,
711
+ video: false,
712
+ pdf: false,
713
+ },
714
+ interleaved: false,
715
+ },
716
+ release_date: "",
717
+ variants: {},
718
+ }
719
+ }
720
+ }
721
+
722
+ return models
723
+ } catch (e) {
724
+ return {}
725
+ }
726
+ },
727
+ }
728
+ }),
729
+ "cloudflare-workers-ai": Effect.fnUntraced(function* (input: Info) {
730
+ // When baseURL is already configured (e.g. corporate config routing through a proxy/gateway),
731
+ // skip the account ID check because the URL is already fully specified.
732
+ if (input.options?.baseURL) return { autoload: false }
733
+
734
+ const auth = yield* dep.auth(input.id)
735
+ const env = yield* dep.env()
736
+ const accountId = env["CLOUDFLARE_ACCOUNT_ID"] || (auth?.type === "api" ? auth.metadata?.accountId : undefined)
737
+ if (!accountId)
738
+ return {
739
+ autoload: false,
740
+ async getModel() {
741
+ throw new Error(
742
+ "CLOUDFLARE_ACCOUNT_ID is missing. Set it with: export CLOUDFLARE_ACCOUNT_ID=<your-account-id>",
743
+ )
744
+ },
745
+ }
746
+
747
+ const apiKey = env["CLOUDFLARE_API_KEY"] || (auth?.type === "api" ? auth.key : undefined)
748
+
749
+ return {
750
+ autoload: !!apiKey,
751
+ options: {
752
+ apiKey,
753
+ headers: {
754
+ "User-Agent": `opencode/${InstallationVersion} cloudflare-workers-ai (${os.platform()} ${os.release()}; ${os.arch()})`,
755
+ },
756
+ },
757
+ async getModel(sdk: any, modelID: string) {
758
+ return sdk.languageModel(modelID)
759
+ },
760
+ vars(_options) {
761
+ return {
762
+ CLOUDFLARE_ACCOUNT_ID: accountId,
763
+ }
764
+ },
765
+ }
766
+ }),
767
+ "cloudflare-ai-gateway": Effect.fnUntraced(function* (input: Info) {
768
+ // When baseURL is already configured (e.g. corporate config), skip the ID checks.
769
+ if (input.options?.baseURL) return { autoload: false }
770
+
771
+ const auth = yield* dep.auth(input.id)
772
+ const env = yield* dep.env()
773
+ const accountId = env["CLOUDFLARE_ACCOUNT_ID"] || (auth?.type === "api" ? auth.metadata?.accountId : undefined)
774
+ // The Cloudflare auth prompt stores this value as gatewayId metadata.
775
+ const gateway = env["CLOUDFLARE_GATEWAY_ID"] || (auth?.type === "api" ? auth.metadata?.gatewayId : undefined)
776
+
777
+ if (!accountId || !gateway) {
778
+ const missing = [
779
+ !accountId ? "CLOUDFLARE_ACCOUNT_ID" : undefined,
780
+ !gateway ? "CLOUDFLARE_GATEWAY_ID" : undefined,
781
+ ].filter((x): x is string => Boolean(x))
782
+ return {
783
+ autoload: false,
784
+ async getModel() {
785
+ throw new Error(
786
+ `${missing.join(" and ")} missing. Set with: ${missing.map((x) => `export ${x}=<value>`).join(" && ")}`,
787
+ )
788
+ },
789
+ }
790
+ }
791
+
792
+ // Get API token from env or auth - required for authenticated gateways
793
+ const apiToken =
794
+ env["CLOUDFLARE_API_TOKEN"] || env["CF_AIG_TOKEN"] || (auth?.type === "api" ? auth.key : undefined)
795
+
796
+ if (!apiToken) {
797
+ throw new Error(
798
+ "CLOUDFLARE_API_TOKEN (or CF_AIG_TOKEN) is required for Cloudflare AI Gateway. " +
799
+ "Set it via environment variable or run `opencode auth cloudflare-ai-gateway`.",
800
+ )
801
+ }
802
+
803
+ // Use official ai-gateway-provider package (v2.x for AI SDK v5 compatibility)
804
+ const { createAiGateway } = yield* Effect.promise(() => import("ai-gateway-provider"))
805
+ const { createUnified } = yield* Effect.promise(() => import("ai-gateway-provider/providers/unified"))
806
+
807
+ const metadata = iife(() => {
808
+ if (input.options?.metadata) return input.options.metadata
809
+ try {
810
+ return JSON.parse(input.options?.headers?.["cf-aig-metadata"])
811
+ } catch {
812
+ return undefined
813
+ }
814
+ })
815
+ const opts = {
816
+ metadata,
817
+ cacheTtl: input.options?.cacheTtl,
818
+ cacheKey: input.options?.cacheKey,
819
+ skipCache: input.options?.skipCache,
820
+ collectLog: input.options?.collectLog,
821
+ headers: {
822
+ "User-Agent": `opencode/${InstallationVersion} cloudflare-ai-gateway (${os.platform()} ${os.release()}; ${os.arch()})`,
823
+ },
824
+ }
825
+
826
+ const aigateway = createAiGateway({
827
+ accountId,
828
+ gateway,
829
+ apiKey: apiToken,
830
+ ...(Object.values(opts).some((v) => v !== undefined) ? { options: opts } : {}),
831
+ })
832
+ const unified = createUnified({ apiKey: apiToken })
833
+
834
+ return {
835
+ autoload: true,
836
+ async getModel(_sdk: any, modelID: string, _options?: Record<string, any>) {
837
+ // Model IDs use Unified API format: provider/model (e.g., "anthropic/claude-sonnet-4-5")
838
+ return aigateway(unified(modelID))
839
+ },
840
+ options: {},
841
+ }
842
+ }),
843
+ cerebras: () =>
844
+ Effect.succeed({
845
+ autoload: false,
846
+ options: {
847
+ headers: {
848
+ "X-Cerebras-3rd-Party-Integration": "opencode",
849
+ },
850
+ },
851
+ }),
852
+ kilo: () =>
853
+ Effect.succeed({
854
+ autoload: false,
855
+ options: {
856
+ headers: {
857
+ "HTTP-Referer": "https://opencode.ai/",
858
+ "X-Title": "opencode",
859
+ },
860
+ },
861
+ }),
862
+ "snowflake-cortex": Effect.fnUntraced(function* (input: Info) {
863
+ const env = yield* dep.env()
864
+ const auth = yield* dep.auth(input.id)
865
+
866
+ const account =
867
+ env["SNOWFLAKE_ACCOUNT"] ??
868
+ (auth?.type === "api" ? auth.metadata?.account : undefined) ??
869
+ (auth?.type === "oauth" ? auth.accountId : undefined) ??
870
+ input.options?.account
871
+
872
+ const envToken = env["SNOWFLAKE_CORTEX_TOKEN"] ?? env["SNOWFLAKE_CORTEX_PAT"]
873
+ const apiKeyToken = auth?.type === "api" ? auth.key : undefined
874
+ const oauthToken = auth?.type === "oauth" ? auth.access : undefined
875
+ const configToken = input.options?.token ?? input.options?.apiKey
876
+
877
+ const token = envToken ?? apiKeyToken ?? oauthToken ?? configToken
878
+
879
+ if (!account || !token) {
880
+ const missing = [!account && "SNOWFLAKE_ACCOUNT", !token && "SNOWFLAKE_CORTEX_TOKEN"].filter(Boolean).join(", ")
881
+ return {
882
+ autoload: false,
883
+ async getModel() {
884
+ throw new Error(
885
+ `Snowflake Cortex: missing credentials (${missing}). Provide a bearer token (OAuth, JWT, or PAT) via env var, opencode auth, or provider options.`,
886
+ )
887
+ },
888
+ }
889
+ }
890
+
891
+ const baseURL = `https://${account}.snowflakecomputing.com/api/v2/cortex/v1`
892
+
893
+ const options: Record<string, any> = { baseURL, apiKey: token }
894
+
895
+ // Only skip provider-level fetch when the token is from OAuth with no override.
896
+ // For OAuth tokens, the plugin auth loader's combined fetch handles
897
+ // OAuth refresh + snowflake transformations in one place.
898
+ // For env/config/API-key tokens, the provider fetch applies snowflake
899
+ // transformations directly.
900
+ const useOAuthHandler =
901
+ oauthToken !== undefined && envToken === undefined && apiKeyToken === undefined && configToken === undefined
902
+ if (!useOAuthHandler) {
903
+ options.fetch = async (url: RequestInfo | URL, init?: RequestInit) => {
904
+ if (init?.body && typeof init.body === "string") {
905
+ try {
906
+ const body = JSON.parse(init.body)
907
+ if ("max_tokens" in body) {
908
+ body.max_completion_tokens = body.max_tokens
909
+ delete body.max_tokens
910
+ init = { ...init, body: JSON.stringify(body) }
911
+ }
912
+ } catch {}
913
+ }
914
+
915
+ const response = await fetch(url, init)
916
+
917
+ if (!response.ok && response.status === 400) {
918
+ try {
919
+ const errorData = await response.clone().json()
920
+ const errorMessage = String(errorData.message || errorData.error || "")
921
+ if (errorMessage.toLowerCase().includes("conversation complete")) {
922
+ return new Response(
923
+ JSON.stringify({
924
+ choices: [{ finish_reason: "stop", message: { content: "", role: "assistant" } }],
925
+ }),
926
+ { status: 200, headers: new Headers({ "content-type": "application/json" }) },
927
+ )
928
+ }
929
+ } catch {}
930
+ }
931
+
932
+ if (response.body && response.headers.get("content-type")?.includes("text/event-stream")) {
933
+ const reader = response.body.getReader()
934
+ const encoder = new TextEncoder()
935
+ const decoder = new TextDecoder()
936
+ const stream = new ReadableStream({
937
+ async pull(ctrl) {
938
+ const { done, value } = await reader.read()
939
+ if (done) {
940
+ ctrl.close()
941
+ return
942
+ }
943
+ const text = decoder.decode(value, { stream: true })
944
+ ctrl.enqueue(encoder.encode(text.replace(/"role"\s*:\s*""/g, '"role":"assistant"')))
945
+ },
946
+ cancel() {
947
+ reader.cancel()
948
+ },
949
+ })
950
+ return new Response(stream, { headers: response.headers, status: response.status })
951
+ }
952
+
953
+ return response
954
+ }
955
+ }
956
+
957
+ return {
958
+ autoload: input.source === "config",
959
+ options,
960
+ }
961
+ }),
962
+ }
963
+ }
964
+
965
+ const ProviderApiInfo = Schema.Struct({
966
+ id: Schema.String,
967
+ url: Schema.String,
968
+ npm: Schema.String,
969
+ })
970
+
971
+ const ProviderModalities = Schema.Struct({
972
+ text: Schema.Boolean,
973
+ audio: Schema.Boolean,
974
+ image: Schema.Boolean,
975
+ video: Schema.Boolean,
976
+ pdf: Schema.Boolean,
977
+ })
978
+
979
+ const ProviderInterleaved = Schema.Union([
980
+ Schema.Boolean,
981
+ Schema.Struct({
982
+ field: Schema.Literals(["reasoning", "reasoning_content", "reasoning_details"]),
983
+ }),
984
+ ])
985
+
986
+ const ProviderCapabilities = Schema.Struct({
987
+ temperature: Schema.Boolean,
988
+ reasoning: Schema.Boolean,
989
+ attachment: Schema.Boolean,
990
+ toolcall: Schema.Boolean,
991
+ input: ProviderModalities,
992
+ output: ProviderModalities,
993
+ interleaved: ProviderInterleaved,
994
+ })
995
+
996
+ const ProviderCacheCost = Schema.Struct({
997
+ read: Schema.Finite,
998
+ write: Schema.Finite,
999
+ })
1000
+
1001
+ const ProviderCostTier = Schema.Struct({
1002
+ input: Schema.Finite,
1003
+ output: Schema.Finite,
1004
+ cache: ProviderCacheCost,
1005
+ tier: Schema.Struct({
1006
+ type: Schema.Literal("context"),
1007
+ size: Schema.Finite,
1008
+ }),
1009
+ })
1010
+
1011
+ const ProviderCost = Schema.Struct({
1012
+ input: Schema.Finite,
1013
+ output: Schema.Finite,
1014
+ cache: ProviderCacheCost,
1015
+ tiers: optional(Schema.Array(ProviderCostTier)),
1016
+ experimentalOver200K: optional(
1017
+ Schema.Struct({
1018
+ input: Schema.Finite,
1019
+ output: Schema.Finite,
1020
+ cache: ProviderCacheCost,
1021
+ }),
1022
+ ),
1023
+ })
1024
+
1025
+ const ProviderLimit = Schema.Struct({
1026
+ context: Schema.Finite,
1027
+ input: optional(Schema.Finite),
1028
+ output: Schema.Finite,
1029
+ })
1030
+
1031
+ export const Model = Schema.Struct({
1032
+ id: ModelV2.ID,
1033
+ providerID: ProviderV2.ID,
1034
+ api: ProviderApiInfo,
1035
+ name: Schema.String,
1036
+ family: optional(Schema.String),
1037
+ capabilities: ProviderCapabilities,
1038
+ cost: ProviderCost,
1039
+ limit: ProviderLimit,
1040
+ status: ModelStatus,
1041
+ options: Schema.Record(Schema.String, Schema.Any),
1042
+ headers: Schema.Record(Schema.String, Schema.String),
1043
+ release_date: Schema.String,
1044
+ variants: optional(Schema.Record(Schema.String, Schema.Record(Schema.String, Schema.Any))),
1045
+ }).annotate({ identifier: "Model" })
1046
+ export type Model = Types.DeepMutable<Schema.Schema.Type<typeof Model>>
1047
+
1048
+ export const Info = Schema.Struct({
1049
+ id: ProviderV2.ID,
1050
+ name: Schema.String,
1051
+ source: Schema.Literals(["env", "config", "custom", "api"]),
1052
+ env: Schema.Array(Schema.String),
1053
+ key: optional(Schema.String),
1054
+ options: Schema.Record(Schema.String, Schema.Any),
1055
+ models: Schema.Record(Schema.String, Model),
1056
+ }).annotate({ identifier: "Provider" })
1057
+ export type Info = Types.DeepMutable<Schema.Schema.Type<typeof Info>>
1058
+
1059
+ const DefaultModelIDs = Schema.Record(Schema.String, Schema.String)
1060
+
1061
+ export const ListResult = Schema.Struct({
1062
+ all: Schema.Array(Info),
1063
+ default: DefaultModelIDs,
1064
+ connected: Schema.Array(Schema.String),
1065
+ })
1066
+ export type ListResult = Types.DeepMutable<Schema.Schema.Type<typeof ListResult>>
1067
+
1068
+ export const ConfigProvidersResult = Schema.Struct({
1069
+ providers: Schema.Array(Info),
1070
+ default: DefaultModelIDs,
1071
+ })
1072
+ export type ConfigProvidersResult = Types.DeepMutable<Schema.Schema.Type<typeof ConfigProvidersResult>>
1073
+
1074
+ export function toPublicInfo(provider: Info): Info {
1075
+ return JSON.parse(
1076
+ JSON.stringify(
1077
+ {
1078
+ ...provider,
1079
+ models: Object.fromEntries(Object.entries(provider.models).filter(([, model]) => Schema.is(Model)(model))),
1080
+ },
1081
+ (_, value) => {
1082
+ if (typeof value === "function" || typeof value === "symbol" || value === undefined) return undefined
1083
+ if (typeof value === "bigint") return value.toString()
1084
+ return value
1085
+ },
1086
+ ),
1087
+ )
1088
+ }
1089
+
1090
+ export function defaultModelIDs<T extends { models: Record<string, { id: string }> }>(providers: Record<string, T>) {
1091
+ return mapValues(providers, (item) => sort(Object.values(item.models))[0].id)
1092
+ }
1093
+
1094
+ export class ModelNotFoundError extends Schema.TaggedErrorClass<ModelNotFoundError>()("ProviderModelNotFoundError", {
1095
+ providerID: ProviderV2.ID,
1096
+ modelID: ModelV2.ID,
1097
+ suggestions: Schema.optional(Schema.Array(Schema.String)),
1098
+ cause: Schema.optional(Schema.Defect()),
1099
+ }) {
1100
+ override get message() {
1101
+ const suggestions = this.suggestions?.length ? ` Did you mean: ${this.suggestions.join(", ")}?` : ""
1102
+ return `Model not found: ${this.providerID}/${this.modelID}.${suggestions}`
1103
+ }
1104
+
1105
+ static isInstance(input: unknown): input is ModelNotFoundError {
1106
+ return input instanceof ModelNotFoundError
1107
+ }
1108
+ }
1109
+
1110
+ export class InitError extends Schema.TaggedErrorClass<InitError>()("ProviderInitError", {
1111
+ providerID: ProviderV2.ID,
1112
+ cause: Schema.optional(Schema.Defect()),
1113
+ }) {
1114
+ override get message() {
1115
+ return `Failed to initialize provider: ${this.providerID}`
1116
+ }
1117
+
1118
+ static isInstance(input: unknown): input is InitError {
1119
+ return input instanceof InitError
1120
+ }
1121
+ }
1122
+
1123
+ export class NoProvidersError extends Schema.TaggedErrorClass<NoProvidersError>()("ProviderNoProvidersError", {}) {
1124
+ override get message() {
1125
+ return "No providers are available"
1126
+ }
1127
+
1128
+ static isInstance(input: unknown): input is NoProvidersError {
1129
+ return input instanceof NoProvidersError
1130
+ }
1131
+ }
1132
+
1133
+ export class NoModelsError extends Schema.TaggedErrorClass<NoModelsError>()("ProviderNoModelsError", {
1134
+ providerID: ProviderV2.ID,
1135
+ }) {
1136
+ override get message() {
1137
+ return `No models are available for provider: ${this.providerID}`
1138
+ }
1139
+
1140
+ static isInstance(input: unknown): input is NoModelsError {
1141
+ return input instanceof NoModelsError
1142
+ }
1143
+ }
1144
+
1145
+ export type DefaultModelError = ModelNotFoundError | NoProvidersError | NoModelsError
1146
+ export type Error = ModelNotFoundError | InitError | NoProvidersError | NoModelsError
1147
+
1148
+ export interface Interface {
1149
+ readonly list: () => Effect.Effect<Record<ProviderV2.ID, Info>>
1150
+ readonly getProvider: (providerID: ProviderV2.ID) => Effect.Effect<Info>
1151
+ readonly getModel: (providerID: ProviderV2.ID, modelID: ModelV2.ID) => Effect.Effect<Model, ModelNotFoundError>
1152
+ readonly getLanguage: (model: Model) => Effect.Effect<LanguageModelV3, ModelNotFoundError>
1153
+ readonly closest: (
1154
+ providerID: ProviderV2.ID,
1155
+ query: string[],
1156
+ ) => Effect.Effect<{ providerID: ProviderV2.ID; modelID: string } | undefined>
1157
+ readonly getSmallModel: (providerID: ProviderV2.ID) => Effect.Effect<Model | undefined>
1158
+ readonly defaultModel: () => Effect.Effect<{ providerID: ProviderV2.ID; modelID: ModelV2.ID }, DefaultModelError>
1159
+ }
1160
+
1161
+ interface State {
1162
+ models: Map<string, LanguageModelV3>
1163
+ providers: Record<ProviderV2.ID, Info>
1164
+ catalog: Record<ProviderV2.ID, Info>
1165
+ sdk: Map<string, BundledSDK>
1166
+ modelLoaders: Record<string, CustomModelLoader>
1167
+ varsLoaders: Record<string, CustomVarsLoader>
1168
+ }
1169
+
1170
+ export class Service extends Context.Service<Service, Interface>()("@neurocode/Provider") {}
1171
+
1172
+ export const use = serviceUse(Service)
1173
+
1174
+ function cost(c: ModelsDev.Model["cost"]): Model["cost"] {
1175
+ const result: Model["cost"] = {
1176
+ input: c?.input ?? 0,
1177
+ output: c?.output ?? 0,
1178
+ cache: {
1179
+ read: c?.cache_read ?? 0,
1180
+ write: c?.cache_write ?? 0,
1181
+ },
1182
+ }
1183
+ if (c?.tiers) {
1184
+ result.tiers = c.tiers.map((item) => ({
1185
+ input: item.input,
1186
+ output: item.output,
1187
+ cache: {
1188
+ read: item.cache_read ?? 0,
1189
+ write: item.cache_write ?? 0,
1190
+ },
1191
+ tier: item.tier,
1192
+ }))
1193
+ }
1194
+ if (c?.context_over_200k) {
1195
+ result.experimentalOver200K = {
1196
+ cache: {
1197
+ read: c.context_over_200k.cache_read ?? 0,
1198
+ write: c.context_over_200k.cache_write ?? 0,
1199
+ },
1200
+ input: c.context_over_200k.input,
1201
+ output: c.context_over_200k.output,
1202
+ }
1203
+ }
1204
+ return result
1205
+ }
1206
+
1207
+ function fromModelsDevModel(provider: ModelsDev.Provider, model: ModelsDev.Model): Model {
1208
+ const base: Model = {
1209
+ id: ModelV2.ID.make(model.id),
1210
+ providerID: ProviderV2.ID.make(provider.id),
1211
+ name: model.name,
1212
+ family: model.family,
1213
+ api: {
1214
+ id: model.id,
1215
+ url: model.provider?.api ?? provider.api ?? "",
1216
+ npm: model.provider?.npm ?? provider.npm ?? "@ai-sdk/openai-compatible",
1217
+ },
1218
+ status: model.status ?? "active",
1219
+ headers: {},
1220
+ options: {},
1221
+ cost: cost(model.cost),
1222
+ limit: {
1223
+ context: model.limit.context,
1224
+ input: model.limit.input,
1225
+ output: model.limit.output,
1226
+ },
1227
+ capabilities: {
1228
+ temperature: model.temperature ?? false,
1229
+ reasoning: model.reasoning ?? false,
1230
+ attachment: model.attachment ?? false,
1231
+ toolcall: model.tool_call ?? true,
1232
+ input: {
1233
+ text: model.modalities?.input?.includes("text") ?? false,
1234
+ audio: model.modalities?.input?.includes("audio") ?? false,
1235
+ image: model.modalities?.input?.includes("image") ?? false,
1236
+ video: model.modalities?.input?.includes("video") ?? false,
1237
+ pdf: model.modalities?.input?.includes("pdf") ?? false,
1238
+ },
1239
+ output: {
1240
+ text: model.modalities?.output?.includes("text") ?? false,
1241
+ audio: model.modalities?.output?.includes("audio") ?? false,
1242
+ image: model.modalities?.output?.includes("image") ?? false,
1243
+ video: model.modalities?.output?.includes("video") ?? false,
1244
+ pdf: model.modalities?.output?.includes("pdf") ?? false,
1245
+ },
1246
+ interleaved: model.interleaved ?? false,
1247
+ },
1248
+ release_date: model.release_date ?? "",
1249
+ variants: {},
1250
+ }
1251
+
1252
+ const variants = ProviderTransform.reasoningVariants(model, base) ?? ProviderTransform.variants(base)
1253
+
1254
+ return {
1255
+ ...base,
1256
+ variants: mapValues(variants, (v) => v),
1257
+ }
1258
+ }
1259
+
1260
+ export function fromModelsDevProvider(provider: ModelsDev.Provider): Info {
1261
+ const models: Record<string, Model> = {}
1262
+ for (const [key, model] of Object.entries(provider.models)) {
1263
+ models[key] = fromModelsDevModel(provider, model)
1264
+ for (const [mode, opts] of Object.entries(model.experimental?.modes ?? {})) {
1265
+ const id = `${model.id}-${mode}`
1266
+ const base = fromModelsDevModel(provider, model)
1267
+ models[id] = {
1268
+ ...base,
1269
+ id: ModelV2.ID.make(id),
1270
+ name: `${model.name} ${mode[0].toUpperCase()}${mode.slice(1)}`,
1271
+ cost: opts.cost ? mergeDeep(base.cost, cost(opts.cost)) : base.cost,
1272
+ options: modeOptions(base, opts.provider?.body),
1273
+ headers: opts.provider?.headers ?? base.headers,
1274
+ }
1275
+ }
1276
+ }
1277
+ return {
1278
+ id: ProviderV2.ID.make(provider.id),
1279
+ source: "custom",
1280
+ name: provider.name,
1281
+ env: [...(provider.env ?? [])],
1282
+ options: {},
1283
+ models,
1284
+ }
1285
+ }
1286
+
1287
+ function modeOptions(model: Model, body: Record<string, unknown> | undefined) {
1288
+ if (!body) return model.options
1289
+ const options = Object.fromEntries(
1290
+ Object.entries(body).map(([key, value]) => [key.replace(/_([a-z])/g, (_, char) => char.toUpperCase()), value]),
1291
+ )
1292
+ const reasoning = body.reasoning
1293
+ if (model.api.npm !== "@ai-sdk/openai" || !isRecord(reasoning) || typeof reasoning.mode !== "string") return options
1294
+ const { reasoning: _, ...rest } = options
1295
+ return { ...rest, reasoningMode: reasoning.mode }
1296
+ }
1297
+
1298
+ function modelSuggestions(provider: Info | undefined, modelID: ModelV2.ID, enableExperimentalModels: boolean) {
1299
+ const available = provider
1300
+ ? Object.keys(provider.models).filter((id) => {
1301
+ const model = provider.models[id]
1302
+ if (model.status === "deprecated") return false
1303
+ if (model.status === "alpha" && !enableExperimentalModels) return false
1304
+ return true
1305
+ })
1306
+ : []
1307
+ const fuzzy = fuzzysort.go(modelID, available, { limit: 3, threshold: -10000 }).map((m) => m.target)
1308
+ if (fuzzy.length) return fuzzy
1309
+ const query = modelID
1310
+ .toLowerCase()
1311
+ .split(/[^a-z0-9]+/)
1312
+ .filter((part) => part.length > 1)
1313
+ return sortBy(
1314
+ available
1315
+ .map((id) => ({
1316
+ id,
1317
+ score: query.filter((part) => id.toLowerCase().includes(part)).length,
1318
+ }))
1319
+ .filter((item) => item.score > 0),
1320
+ [(item) => item.score, "desc"],
1321
+ [(item) => item.id, "asc"],
1322
+ )
1323
+ .slice(0, 3)
1324
+ .map((item) => item.id)
1325
+ }
1326
+
1327
+ const layer = Layer.effect(
1328
+ Service,
1329
+ Effect.gen(function* () {
1330
+ const fs = yield* FSUtil.Service
1331
+ const config = yield* Config.Service
1332
+ const auth = yield* Auth.Service
1333
+ const env = yield* Env.Service
1334
+ const plugin = yield* Plugin.Service
1335
+ const modelsDevSvc = yield* ModelsDev.Service
1336
+ const runtimeFlags = yield* RuntimeFlags.Service
1337
+
1338
+ const state = yield* InstanceState.make<State>(() =>
1339
+ Effect.gen(function* () {
1340
+ const bridge = yield* EffectBridge.make()
1341
+ const cfg = yield* config.get()
1342
+ const modelsDev = yield* modelsDevSvc.get()
1343
+ const catalog = mapValues(modelsDev, fromModelsDevProvider)
1344
+ const database = mapValues(catalog, toPublicInfo)
1345
+
1346
+ const providers: Record<ProviderV2.ID, Info> = {} as Record<ProviderV2.ID, Info>
1347
+ const languages = new Map<string, LanguageModelV3>()
1348
+ const modelLoaders: {
1349
+ [providerID: string]: CustomModelLoader
1350
+ } = {}
1351
+ const varsLoaders: {
1352
+ [providerID: string]: CustomVarsLoader
1353
+ } = {}
1354
+ const sdk = new Map<string, BundledSDK>()
1355
+ const discoveryLoaders: {
1356
+ [providerID: string]: CustomDiscoverModels
1357
+ } = {}
1358
+ const dep = {
1359
+ auth: (id: string) => auth.get(id).pipe(Effect.orDie),
1360
+ config: () => config.get(),
1361
+ env: () => env.all(),
1362
+ get: (key: string) => env.get(key),
1363
+ }
1364
+
1365
+ function mergeProvider(providerID: ProviderV2.ID, provider: Partial<Info>) {
1366
+ const existing = providers[providerID]
1367
+ if (existing) {
1368
+ // @ts-expect-error
1369
+ providers[providerID] = mergeDeep(existing, provider)
1370
+ return
1371
+ }
1372
+ const match = database[providerID]
1373
+ if (!match) return
1374
+ // @ts-expect-error
1375
+ providers[providerID] = mergeDeep(match, provider)
1376
+ }
1377
+
1378
+ // load plugins first so config() hook runs before reading cfg.provider
1379
+ const plugins = yield* plugin.list()
1380
+
1381
+ // now read config providers - includes any modifications from plugin config() hook
1382
+ const configProviders = Object.entries(cfg.provider ?? {})
1383
+ const disabled = new Set(cfg.disabled_providers ?? [])
1384
+ const enabled = cfg.enabled_providers ? new Set(cfg.enabled_providers) : null
1385
+
1386
+ function isProviderAllowed(providerID: ProviderV2.ID): boolean {
1387
+ if (enabled && !enabled.has(providerID)) return false
1388
+ if (disabled.has(providerID)) return false
1389
+ return true
1390
+ }
1391
+
1392
+ for (const hook of plugins) {
1393
+ const p = hook.provider
1394
+ const models = p?.models
1395
+ if (!p || !models) continue
1396
+
1397
+ const providerID = ProviderV2.ID.make(p.id)
1398
+ if (disabled.has(providerID)) continue
1399
+
1400
+ const provider = database[providerID]
1401
+ if (!provider) continue
1402
+ const pluginAuth = yield* auth.get(providerID).pipe(Effect.orDie)
1403
+
1404
+ provider.models = yield* Effect.promise(async () => {
1405
+ const next = await models(toPublicInfo(provider), { auth: pluginAuth })
1406
+ return Object.fromEntries(
1407
+ Object.entries(next).map(([id, model]) => [
1408
+ id,
1409
+ {
1410
+ ...model,
1411
+ id: ModelV2.ID.make(id),
1412
+ providerID,
1413
+ },
1414
+ ]),
1415
+ )
1416
+ })
1417
+ }
1418
+
1419
+ // extend database from config
1420
+ for (const [providerID, provider] of configProviders) {
1421
+ const existing = database[providerID]
1422
+ const parsed: Info = {
1423
+ id: ProviderV2.ID.make(providerID),
1424
+ name: provider.name ?? existing?.name ?? providerID,
1425
+ env: provider.env ?? existing?.env ?? [],
1426
+ options: mergeDeep(existing?.options ?? {}, provider.options ?? {}),
1427
+ source: "config",
1428
+ models: existing?.models ?? {},
1429
+ }
1430
+
1431
+ for (const [modelID, model] of Object.entries(provider.models ?? {})) {
1432
+ const existingModel = parsed.models[model.id ?? modelID]
1433
+ const apiID = model.id ?? existingModel?.api.id ?? modelID
1434
+ const apiNpm =
1435
+ model.provider?.npm ??
1436
+ provider.npm ??
1437
+ existingModel?.api.npm ??
1438
+ modelsDev[providerID]?.npm ??
1439
+ "@ai-sdk/openai-compatible"
1440
+ const name = iife(() => {
1441
+ if (model.name) return model.name
1442
+ if (model.id && model.id !== modelID) return modelID
1443
+ return existingModel?.name ?? modelID
1444
+ })
1445
+ const parsedModel: Model = {
1446
+ id: ModelV2.ID.make(modelID),
1447
+ api: {
1448
+ id: apiID,
1449
+ npm: apiNpm,
1450
+ url: model.provider?.api ?? provider?.api ?? existingModel?.api.url ?? modelsDev[providerID]?.api ?? "",
1451
+ },
1452
+ status: model.status ?? existingModel?.status ?? "active",
1453
+ name,
1454
+ providerID: ProviderV2.ID.make(providerID),
1455
+ capabilities: {
1456
+ temperature: model.temperature ?? existingModel?.capabilities.temperature ?? false,
1457
+ reasoning: model.reasoning ?? existingModel?.capabilities.reasoning ?? false,
1458
+ attachment: model.attachment ?? existingModel?.capabilities.attachment ?? false,
1459
+ toolcall: model.tool_call ?? existingModel?.capabilities.toolcall ?? true,
1460
+ input: {
1461
+ text: model.modalities?.input?.includes("text") ?? existingModel?.capabilities.input.text ?? true,
1462
+ audio: model.modalities?.input?.includes("audio") ?? existingModel?.capabilities.input.audio ?? false,
1463
+ image: model.modalities?.input?.includes("image") ?? existingModel?.capabilities.input.image ?? false,
1464
+ video: model.modalities?.input?.includes("video") ?? existingModel?.capabilities.input.video ?? false,
1465
+ pdf: model.modalities?.input?.includes("pdf") ?? existingModel?.capabilities.input.pdf ?? false,
1466
+ },
1467
+ output: {
1468
+ text: model.modalities?.output?.includes("text") ?? existingModel?.capabilities.output.text ?? true,
1469
+ audio:
1470
+ model.modalities?.output?.includes("audio") ?? existingModel?.capabilities.output.audio ?? false,
1471
+ image:
1472
+ model.modalities?.output?.includes("image") ?? existingModel?.capabilities.output.image ?? false,
1473
+ video:
1474
+ model.modalities?.output?.includes("video") ?? existingModel?.capabilities.output.video ?? false,
1475
+ pdf: model.modalities?.output?.includes("pdf") ?? existingModel?.capabilities.output.pdf ?? false,
1476
+ },
1477
+ interleaved:
1478
+ model.interleaved ??
1479
+ existingModel?.capabilities.interleaved ??
1480
+ (!existingModel && apiNpm === "@ai-sdk/openai-compatible" && apiID.includes("deepseek")
1481
+ ? { field: "reasoning_content" }
1482
+ : false),
1483
+ },
1484
+ cost: {
1485
+ input: model?.cost?.input ?? existingModel?.cost?.input ?? 0,
1486
+ output: model?.cost?.output ?? existingModel?.cost?.output ?? 0,
1487
+ cache: {
1488
+ read: model?.cost?.cache_read ?? existingModel?.cost?.cache.read ?? 0,
1489
+ write: model?.cost?.cache_write ?? existingModel?.cost?.cache.write ?? 0,
1490
+ },
1491
+ },
1492
+ options: mergeDeep(existingModel?.options ?? {}, model.options ?? {}),
1493
+ limit: {
1494
+ context: model.limit?.context ?? existingModel?.limit?.context ?? 0,
1495
+ input: model.limit?.input ?? existingModel?.limit?.input,
1496
+ output: model.limit?.output ?? existingModel?.limit?.output ?? 0,
1497
+ },
1498
+ headers: mergeDeep(existingModel?.headers ?? {}, model.headers ?? {}),
1499
+ family: model.family ?? existingModel?.family ?? "",
1500
+ release_date: model.release_date ?? existingModel?.release_date ?? "",
1501
+ variants: {},
1502
+ }
1503
+ const variants =
1504
+ existingModel?.api.npm === parsedModel.api.npm
1505
+ ? (existingModel.variants ?? ProviderTransform.variants(parsedModel))
1506
+ : ProviderTransform.variants(parsedModel)
1507
+ const merged = mergeDeep(variants, model.variants ?? {})
1508
+ parsedModel.variants = mapValues(
1509
+ pickBy(merged, (v) => !v.disabled),
1510
+ (v) => omit(v, ["disabled"]),
1511
+ )
1512
+ parsed.models[modelID] = parsedModel
1513
+ }
1514
+ database[providerID] = parsed
1515
+ }
1516
+
1517
+ // load env
1518
+ const envs = yield* env.all()
1519
+ for (const [id, provider] of Object.entries(database)) {
1520
+ const providerID = ProviderV2.ID.make(id)
1521
+ if (disabled.has(providerID)) continue
1522
+ const apiKey = provider.env.map((item) => envs[item]).find(Boolean)
1523
+ if (!apiKey) continue
1524
+ mergeProvider(providerID, {
1525
+ source: "env",
1526
+ key: provider.env.length === 1 ? apiKey : undefined,
1527
+ })
1528
+ }
1529
+
1530
+ // load apikeys
1531
+ const auths = yield* auth.all().pipe(Effect.orDie)
1532
+ for (const [id, provider] of Object.entries(auths)) {
1533
+ const providerID = ProviderV2.ID.make(id)
1534
+ if (disabled.has(providerID)) continue
1535
+ if (provider.type === "api") {
1536
+ mergeProvider(providerID, {
1537
+ source: "api",
1538
+ key: provider.key,
1539
+ })
1540
+ }
1541
+ }
1542
+
1543
+ // plugin auth loader - database now has entries for config providers
1544
+ for (const plugin of plugins) {
1545
+ if (!plugin.auth) continue
1546
+ const providerID = ProviderV2.ID.make(plugin.auth.provider)
1547
+ if (disabled.has(providerID)) continue
1548
+
1549
+ const stored = yield* auth.get(providerID).pipe(Effect.orDie)
1550
+ if (!stored) continue
1551
+ if (!plugin.auth.loader) continue
1552
+
1553
+ const options = yield* Effect.promise(() =>
1554
+ plugin.auth!.loader!(
1555
+ () => bridge.promise(auth.get(providerID).pipe(Effect.orDie)) as any,
1556
+ toPublicInfo(database[plugin.auth!.provider]),
1557
+ ),
1558
+ )
1559
+ const opts = options ?? {}
1560
+ const patch: Partial<Info> = providers[providerID] ? { options: opts } : { source: "custom", options: opts }
1561
+ mergeProvider(providerID, patch)
1562
+ }
1563
+
1564
+ for (const [id, fn] of Object.entries(custom(dep))) {
1565
+ const providerID = ProviderV2.ID.make(id)
1566
+ if (disabled.has(providerID)) continue
1567
+ const data = database[providerID]
1568
+ if (!data) {
1569
+ continue
1570
+ }
1571
+ const result = yield* fn(data)
1572
+ if (result && (result.autoload || providers[providerID])) {
1573
+ if (result.getModel) modelLoaders[providerID] = result.getModel
1574
+ if (result.vars) varsLoaders[providerID] = result.vars
1575
+ if (result.discoverModels) discoveryLoaders[providerID] = result.discoverModels
1576
+ const opts = result.options ?? {}
1577
+ const patch: Partial<Info> = providers[providerID] ? { options: opts } : { source: "custom", options: opts }
1578
+ mergeProvider(providerID, patch)
1579
+ }
1580
+ }
1581
+
1582
+ // load config - re-apply with updated data
1583
+ for (const [id, provider] of configProviders) {
1584
+ const providerID = ProviderV2.ID.make(id)
1585
+ const partial: Partial<Info> = { source: "config" }
1586
+ if (provider.env) partial.env = provider.env
1587
+ if (provider.name) partial.name = provider.name
1588
+ if (provider.options) partial.options = provider.options
1589
+ mergeProvider(providerID, partial)
1590
+ }
1591
+
1592
+ const gitlab = ProviderV2.ID.make("gitlab")
1593
+ if (discoveryLoaders[gitlab] && providers[gitlab] && isProviderAllowed(gitlab)) {
1594
+ yield* Effect.promise(async () => {
1595
+ try {
1596
+ const discovered = await discoveryLoaders[gitlab]()
1597
+ for (const [modelID, model] of Object.entries(discovered)) {
1598
+ if (!providers[gitlab].models[modelID]) {
1599
+ providers[gitlab].models[modelID] = model
1600
+ }
1601
+ }
1602
+ } catch (e) {}
1603
+ })
1604
+ }
1605
+
1606
+ for (const [id, provider] of Object.entries(providers)) {
1607
+ const providerID = ProviderV2.ID.make(id)
1608
+ if (!isProviderAllowed(providerID)) {
1609
+ delete providers[providerID]
1610
+ continue
1611
+ }
1612
+
1613
+ const configProvider = cfg.provider?.[providerID]
1614
+
1615
+ for (const [modelID, model] of Object.entries(provider.models)) {
1616
+ model.api.id = model.api.id ?? model.id ?? modelID
1617
+ if (
1618
+ // These chat aliases are invalid for the special handling in the
1619
+ // built-in providers below, but custom providers may support them.
1620
+ (modelID === "gpt-5-chat-latest" &&
1621
+ (providerID === ProviderV2.ID.openai ||
1622
+ providerID === ProviderV2.ID.githubCopilot ||
1623
+ providerID === ProviderV2.ID.openrouter)) ||
1624
+ (providerID === ProviderV2.ID.openrouter && modelID === "openai/gpt-5-chat")
1625
+ )
1626
+ delete provider.models[modelID]
1627
+ if (model.status === "alpha" && !runtimeFlags.enableExperimentalModels) delete provider.models[modelID]
1628
+ if (model.status === "deprecated") delete provider.models[modelID]
1629
+ if (
1630
+ (configProvider?.blacklist && configProvider.blacklist.includes(modelID)) ||
1631
+ (configProvider?.whitelist && !configProvider.whitelist.includes(modelID))
1632
+ )
1633
+ delete provider.models[modelID]
1634
+
1635
+ if (model.variants === undefined) {
1636
+ model.variants = mapValues(ProviderTransform.variants(model), (v) => v)
1637
+ }
1638
+
1639
+ const configVariants = configProvider?.models?.[modelID]?.variants
1640
+ if (configVariants && model.variants) {
1641
+ const merged = mergeDeep(model.variants, configVariants)
1642
+ model.variants = mapValues(
1643
+ pickBy(merged, (v) => !v.disabled),
1644
+ (v) => omit(v, ["disabled"]),
1645
+ )
1646
+ }
1647
+ }
1648
+
1649
+ if (Object.keys(provider.models).length === 0) {
1650
+ delete providers[providerID]
1651
+ continue
1652
+ }
1653
+ }
1654
+
1655
+ return {
1656
+ models: languages,
1657
+ providers,
1658
+ catalog,
1659
+ sdk,
1660
+ modelLoaders,
1661
+ varsLoaders,
1662
+ }
1663
+ }),
1664
+ )
1665
+
1666
+ const list = Effect.fn("Provider.list")(() => InstanceState.use(state, (s) => s.providers))
1667
+
1668
+ async function resolveSDK(model: Model, s: State, envs: Record<string, string | undefined>) {
1669
+ try {
1670
+ const provider = s.providers[model.providerID]
1671
+ const options = { ...provider.options }
1672
+
1673
+ if (
1674
+ model.providerID === "google-vertex" &&
1675
+ model.api.npm === "@ai-sdk/google-vertex/anthropic" &&
1676
+ !options.baseURL
1677
+ ) {
1678
+ const baseURL = googleVertexAnthropicBaseURL(
1679
+ typeof options.project === "string" ? options.project : undefined,
1680
+ typeof options.location === "string" ? options.location : undefined,
1681
+ )
1682
+ if (baseURL) options.baseURL = baseURL
1683
+ }
1684
+
1685
+ if (model.providerID === "google-vertex" && !model.api.npm.includes("@ai-sdk/openai-compatible")) {
1686
+ delete options.fetch
1687
+ }
1688
+
1689
+ if (model.api.npm.includes("@ai-sdk/openai-compatible") && options["includeUsage"] !== false) {
1690
+ options["includeUsage"] = true
1691
+ }
1692
+
1693
+ const baseURL = iife(() => {
1694
+ let url =
1695
+ typeof options["baseURL"] === "string" && options["baseURL"] !== "" ? options["baseURL"] : model.api.url
1696
+ if (!url) return
1697
+
1698
+ const loader = s.varsLoaders[model.providerID]
1699
+ if (loader) {
1700
+ const vars = loader(options)
1701
+ for (const [key, value] of Object.entries(vars)) {
1702
+ const field = "${" + key + "}"
1703
+ url = url.replaceAll(field, value)
1704
+ }
1705
+ }
1706
+
1707
+ url = url.replace(/\$\{([^}]+)\}/g, (item, key) => {
1708
+ const val = envs[String(key)]
1709
+ return val ?? item
1710
+ })
1711
+ return url
1712
+ })
1713
+
1714
+ if (baseURL !== undefined) options["baseURL"] = baseURL
1715
+ if (options["apiKey"] === undefined && provider.key) options["apiKey"] = provider.key
1716
+ if (model.headers)
1717
+ options["headers"] = {
1718
+ ...options["headers"],
1719
+ ...model.headers,
1720
+ }
1721
+
1722
+ const key = Hash.fast(
1723
+ JSON.stringify({
1724
+ providerID: model.providerID,
1725
+ npm: model.api.npm,
1726
+ options,
1727
+ }),
1728
+ )
1729
+ const existing = s.sdk.get(key)
1730
+ if (existing) return existing
1731
+
1732
+ const customFetch = options["fetch"]
1733
+ const chunkTimeout = options["chunkTimeout"]
1734
+ const headerTimeout = options["headerTimeout"]
1735
+ delete options["chunkTimeout"]
1736
+ delete options["headerTimeout"]
1737
+
1738
+ options["fetch"] = async (input: any, init?: BunFetchRequestInit) => {
1739
+ const fetchFn = customFetch ?? fetch
1740
+ const opts = init ?? {}
1741
+ const chunkAbortCtl = typeof chunkTimeout === "number" && chunkTimeout > 0 ? new AbortController() : undefined
1742
+ const headerTimeoutMs = headerTimeout === false ? undefined : headerTimeout
1743
+ const headerTimeoutCtl = typeof headerTimeoutMs === "number" ? timeoutController(headerTimeoutMs) : undefined
1744
+ const signals: AbortSignal[] = []
1745
+
1746
+ if (opts.signal) signals.push(opts.signal)
1747
+ if (chunkAbortCtl) signals.push(chunkAbortCtl.signal)
1748
+ if (headerTimeoutCtl) signals.push(headerTimeoutCtl.signal)
1749
+ if (options["timeout"] !== undefined && options["timeout"] !== null && options["timeout"] !== false)
1750
+ signals.push(AbortSignal.timeout(options["timeout"]))
1751
+
1752
+ const combined = signals.length === 0 ? null : signals.length === 1 ? signals[0] : AbortSignal.any(signals)
1753
+ if (combined) opts.signal = combined
1754
+
1755
+ const res = await fetchFn(input, {
1756
+ ...opts,
1757
+ // @ts-ignore see here: https://github.com/oven-sh/bun/issues/16682
1758
+ timeout: false,
1759
+ }).finally(() => headerTimeoutCtl?.clear())
1760
+
1761
+ if (!chunkAbortCtl) return res
1762
+ return wrapSSE(res, chunkTimeout, chunkAbortCtl)
1763
+ }
1764
+
1765
+ const bundledLoader = BUNDLED_PROVIDERS[model.api.npm]
1766
+ if (bundledLoader) {
1767
+ const factory = await bundledLoader()
1768
+ const loaded = factory({
1769
+ name: model.providerID,
1770
+ ...options,
1771
+ })
1772
+ s.sdk.set(key, loaded)
1773
+ return loaded as SDK
1774
+ }
1775
+
1776
+ const installedPath = await (async () => {
1777
+ if (model.api.npm.startsWith("file://")) {
1778
+ return model.api.npm
1779
+ }
1780
+ const item = await Npm.add(model.api.npm)
1781
+ if (!item.entrypoint) throw new Error(`Package ${model.api.npm} has no import entrypoint`)
1782
+ return item.entrypoint
1783
+ })()
1784
+
1785
+ // `installedPath` is a local entry path or an existing `file://` URL. Normalize
1786
+ // only path inputs so Node on Windows accepts the dynamic import.
1787
+ const importSpec = installedPath.startsWith("file://") ? installedPath : pathToFileURL(installedPath).href
1788
+ const mod = await import(importSpec)
1789
+
1790
+ const fn = mod[Object.keys(mod).find((key) => key.startsWith("create"))!]
1791
+ const loaded = fn({
1792
+ name: model.providerID,
1793
+ ...options,
1794
+ })
1795
+ s.sdk.set(key, loaded)
1796
+ return loaded as SDK
1797
+ } catch (e) {
1798
+ throw new InitError({ providerID: model.providerID, cause: e })
1799
+ }
1800
+ }
1801
+
1802
+ const getProvider = Effect.fn("Provider.getProvider")((providerID: ProviderV2.ID) =>
1803
+ InstanceState.use(state, (s) => s.providers[providerID]),
1804
+ )
1805
+
1806
+ const getModel = Effect.fn("Provider.getModel")(function* (providerID: ProviderV2.ID, modelID: ModelV2.ID) {
1807
+ const s = yield* InstanceState.get(state)
1808
+ const provider = s.providers[providerID]
1809
+ if (!provider) {
1810
+ const catalogProvider = s.catalog[providerID]
1811
+ const suggestions = catalogProvider
1812
+ ? modelSuggestions(catalogProvider, modelID, runtimeFlags.enableExperimentalModels)
1813
+ : fuzzysort
1814
+ .go(providerID, Object.keys({ ...s.catalog, ...s.providers }), { limit: 3, threshold: -10000 })
1815
+ .map((m) => m.target)
1816
+ return yield* new ModelNotFoundError({ providerID, modelID, suggestions })
1817
+ }
1818
+
1819
+ const info = provider.models[modelID]
1820
+ if (!info) {
1821
+ const current = modelSuggestions(provider, modelID, runtimeFlags.enableExperimentalModels)
1822
+ const suggestions = current.length
1823
+ ? current
1824
+ : modelSuggestions(s.catalog[providerID], modelID, runtimeFlags.enableExperimentalModels)
1825
+ return yield* new ModelNotFoundError({ providerID, modelID, suggestions })
1826
+ }
1827
+ return info
1828
+ })
1829
+
1830
+ const getLanguage = Effect.fn("Provider.getLanguage")(function* (model: Model) {
1831
+ const s = yield* InstanceState.get(state)
1832
+ const envs = yield* env.all()
1833
+ const key = `${model.providerID}/${model.id}`
1834
+ if (s.models.has(key)) return s.models.get(key)!
1835
+
1836
+ const provider = s.providers[model.providerID]
1837
+ return yield* EffectPromise.refineRejection(
1838
+ async () => {
1839
+ const sdk = await resolveSDK(model, s, envs)
1840
+ const language = s.modelLoaders[model.providerID]
1841
+ ? await s.modelLoaders[model.providerID](
1842
+ sdk,
1843
+ model.api.id,
1844
+ {
1845
+ ...provider.options,
1846
+ ...model.options,
1847
+ },
1848
+ model,
1849
+ )
1850
+ : sdk.languageModel(model.api.id)
1851
+ s.models.set(key, language)
1852
+ return language
1853
+ },
1854
+ (cause) =>
1855
+ cause instanceof NoSuchModelError
1856
+ ? new ModelNotFoundError({ modelID: model.id, providerID: model.providerID, cause })
1857
+ : undefined,
1858
+ )
1859
+ })
1860
+
1861
+ const closest = Effect.fn("Provider.closest")(function* (providerID: ProviderV2.ID, query: string[]) {
1862
+ const s = yield* InstanceState.get(state)
1863
+ const provider = s.providers[providerID]
1864
+ if (!provider) return undefined
1865
+ for (const item of query) {
1866
+ for (const modelID of Object.keys(provider.models)) {
1867
+ if (modelID.includes(item)) return { providerID, modelID }
1868
+ }
1869
+ }
1870
+ return undefined
1871
+ })
1872
+
1873
+ const getSmallModel = Effect.fn("Provider.getSmallModel")(function* (providerID: ProviderV2.ID) {
1874
+ const cfg = yield* config.get()
1875
+
1876
+ if (cfg.small_model) {
1877
+ const parsed = parseModel(cfg.small_model)
1878
+ return yield* getModel(parsed.providerID, parsed.modelID).pipe(
1879
+ Effect.catchTag("ProviderModelNotFoundError", () => Effect.succeed(undefined)),
1880
+ )
1881
+ }
1882
+
1883
+ const s = yield* InstanceState.get(state)
1884
+ const provider = s.providers[providerID]
1885
+ if (!provider) return undefined
1886
+
1887
+ const experimental = yield* plugin.trigger<"experimental.provider.small_model">(
1888
+ "experimental.provider.small_model",
1889
+ { provider: toPublicInfo(provider) },
1890
+ { model: undefined },
1891
+ )
1892
+ if (experimental.model) {
1893
+ return {
1894
+ ...experimental.model,
1895
+ id: ModelV2.ID.make(experimental.model.id),
1896
+ providerID: ProviderV2.ID.make(experimental.model.providerID),
1897
+ }
1898
+ }
1899
+
1900
+ // TODO: Remove these provider-specific assumptions once model syncing reliably reports available deployments.
1901
+ if (providerID === ProviderV2.ID.azure || providerID === ProviderV2.ID.make("azure-cognitive-services")) {
1902
+ return undefined
1903
+ }
1904
+
1905
+ const priority = providerID.startsWith("opencode")
1906
+ ? ["gpt-nano"]
1907
+ : providerID.startsWith("github-copilot")
1908
+ ? ["gpt-mini", ...smallModelFamilyPriority]
1909
+ : smallModelFamilyPriority
1910
+ const models = sortBy(
1911
+ Object.values(provider.models),
1912
+ [(model) => model.release_date, "desc"],
1913
+ [(model) => model.id, "desc"],
1914
+ )
1915
+ for (const family of priority) {
1916
+ const candidates = models.filter((model) => model.family === family)
1917
+ if (providerID === ProviderV2.ID.amazonBedrock) {
1918
+ const crossRegionPrefixes = ["global.", "us.", "eu."]
1919
+
1920
+ const globalMatch = candidates.find((model) => model.id.startsWith("global."))
1921
+ if (globalMatch) return globalMatch
1922
+
1923
+ const region = provider.options?.region
1924
+ if (region) {
1925
+ const regionPrefix = region.split("-")[0]
1926
+ if (regionPrefix === "us" || regionPrefix === "eu") {
1927
+ const regionalMatch = candidates.find((model) => model.id.startsWith(`${regionPrefix}.`))
1928
+ if (regionalMatch) return regionalMatch
1929
+ }
1930
+ }
1931
+
1932
+ const unprefixed = candidates.find((model) => !crossRegionPrefixes.some((p) => model.id.startsWith(p)))
1933
+ if (unprefixed) return unprefixed
1934
+ continue
1935
+ }
1936
+ if (candidates[0]) return candidates[0]
1937
+ }
1938
+
1939
+ return undefined
1940
+ })
1941
+
1942
+ const defaultModel = Effect.fn("Provider.defaultModel")(function* () {
1943
+ const cfg = yield* config.get()
1944
+ if (cfg.model) return parseModel(cfg.model)
1945
+
1946
+ const s = yield* InstanceState.get(state)
1947
+ const recent = yield* fs.readJson(path.join(Global.Path.state, "model.json")).pipe(
1948
+ Effect.map((x): { providerID: ProviderV2.ID; modelID: ModelV2.ID }[] => {
1949
+ if (!isRecord(x) || !Array.isArray(x.recent)) return []
1950
+ return x.recent.flatMap((item) => {
1951
+ if (!isRecord(item)) return []
1952
+ if (typeof item.providerID !== "string") return []
1953
+ if (typeof item.modelID !== "string") return []
1954
+ return [{ providerID: ProviderV2.ID.make(item.providerID), modelID: ModelV2.ID.make(item.modelID) }]
1955
+ })
1956
+ }),
1957
+ Effect.catch(() => Effect.succeed([] as { providerID: ProviderV2.ID; modelID: ModelV2.ID }[])),
1958
+ )
1959
+ for (const entry of recent) {
1960
+ const provider = s.providers[entry.providerID]
1961
+ if (!provider) continue
1962
+ if (!provider.models[entry.modelID]) continue
1963
+ return { providerID: entry.providerID, modelID: entry.modelID }
1964
+ }
1965
+
1966
+ const configured = Object.keys(cfg.provider ?? {})
1967
+ const provider = Object.values(s.providers).find((p) => configured.length === 0 || configured.includes(p.id))
1968
+ if (!provider) return yield* new NoProvidersError()
1969
+ const [model] = sort(Object.values(provider.models))
1970
+ if (!model) return yield* new NoModelsError({ providerID: provider.id })
1971
+ return {
1972
+ providerID: provider.id,
1973
+ modelID: model.id,
1974
+ }
1975
+ })
1976
+
1977
+ return Service.of({ list, getProvider, getModel, getLanguage, closest, getSmallModel, defaultModel })
1978
+ }),
1979
+ )
1980
+
1981
+ const priority = ["gpt-5", "claude-sonnet-4", "big-pickle", "gemini-3-pro"]
1982
+ const smallModelFamilyPriority = ["gemini-flash", "gpt-nano", "claude-haiku"]
1983
+ export function sort<T extends { id: string }>(models: T[]) {
1984
+ return sortBy(
1985
+ models,
1986
+ [(model) => priority.findIndex((filter) => model.id.includes(filter)), "desc"],
1987
+ [(model) => (model.id.includes("latest") ? 0 : 1), "asc"],
1988
+ [(model) => model.id, "desc"],
1989
+ )
1990
+ }
1991
+
1992
+ export function parseModel(model: string) {
1993
+ const [providerID, ...rest] = model.split("/")
1994
+ return {
1995
+ providerID: ProviderV2.ID.make(providerID),
1996
+ modelID: ModelV2.ID.make(rest.join("/")),
1997
+ }
1998
+ }
1999
+
2000
+ export const node = LayerNode.make({
2001
+ service: Service,
2002
+ layer: layer,
2003
+ deps: [FSUtil.node, Config.node, Auth.node, Env.node, Plugin.node, ModelsDev.node, RuntimeFlags.node],
2004
+ })
2005
+
2006
+ export * as Provider from "./provider"