soraacode 0.1.7 → 0.2.1

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