@stonerzju/opencode 1.2.17 → 1.2.18

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 (261) hide show
  1. package/bin/opencode +29 -157
  2. package/package.json +29 -29
  3. package/src/acp/agent.ts +4 -4
  4. package/src/acp/session.ts +1 -1
  5. package/src/agent/agent.ts +3 -3
  6. package/src/bun/index.ts +2 -2
  7. package/src/cli/cmd/acp.ts +3 -3
  8. package/src/cli/cmd/debug/file.ts +1 -1
  9. package/src/cli/cmd/github.ts +2 -2
  10. package/src/cli/cmd/pr.ts +1 -1
  11. package/src/cli/cmd/tui/app.tsx +24 -24
  12. package/src/cli/cmd/tui/attach.ts +3 -3
  13. package/src/cli/cmd/tui/component/dialog-agent.tsx +3 -3
  14. package/src/cli/cmd/tui/component/dialog-command.tsx +3 -3
  15. package/src/cli/cmd/tui/component/dialog-mcp.tsx +5 -5
  16. package/src/cli/cmd/tui/component/dialog-model.tsx +4 -4
  17. package/src/cli/cmd/tui/component/dialog-provider.tsx +4 -4
  18. package/src/cli/cmd/tui/component/dialog-session-list.tsx +5 -5
  19. package/src/cli/cmd/tui/component/dialog-session-rename.tsx +3 -3
  20. package/src/cli/cmd/tui/component/dialog-skill.tsx +3 -3
  21. package/src/cli/cmd/tui/component/dialog-stash.tsx +3 -3
  22. package/src/cli/cmd/tui/component/dialog-status.tsx +2 -2
  23. package/src/cli/cmd/tui/component/dialog-tag.tsx +3 -3
  24. package/src/cli/cmd/tui/component/logo.tsx +2 -2
  25. package/src/cli/cmd/tui/component/prompt/autocomplete.tsx +6 -6
  26. package/src/cli/cmd/tui/component/prompt/frecency.tsx +2 -2
  27. package/src/cli/cmd/tui/component/prompt/history.tsx +2 -2
  28. package/src/cli/cmd/tui/component/prompt/index.tsx +14 -14
  29. package/src/cli/cmd/tui/component/prompt/stash.tsx +2 -2
  30. package/src/cli/cmd/tui/component/textarea-keybindings.ts +1 -1
  31. package/src/cli/cmd/tui/component/tips.tsx +1 -1
  32. package/src/cli/cmd/tui/context/directory.ts +1 -1
  33. package/src/cli/cmd/tui/context/exit.tsx +1 -1
  34. package/src/cli/cmd/tui/context/keybind.tsx +2 -2
  35. package/src/cli/cmd/tui/context/kv.tsx +2 -2
  36. package/src/cli/cmd/tui/context/local.tsx +6 -6
  37. package/src/cli/cmd/tui/context/sync.tsx +4 -4
  38. package/src/cli/cmd/tui/context/theme/opencode.json +245 -0
  39. package/src/cli/cmd/tui/context/theme.tsx +2 -2
  40. package/src/cli/cmd/tui/context/tui-config.tsx +1 -1
  41. package/src/cli/cmd/tui/event.ts +2 -2
  42. package/src/cli/cmd/tui/routes/home.tsx +6 -6
  43. package/src/cli/cmd/tui/routes/session/dialog-fork-from-timeline.tsx +6 -6
  44. package/src/cli/cmd/tui/routes/session/dialog-message.tsx +6 -6
  45. package/src/cli/cmd/tui/routes/session/dialog-subagent.tsx +2 -2
  46. package/src/cli/cmd/tui/routes/session/dialog-timeline.tsx +3 -3
  47. package/src/cli/cmd/tui/routes/session/header.tsx +5 -5
  48. package/src/cli/cmd/tui/routes/session/index.tsx +32 -32
  49. package/src/cli/cmd/tui/routes/session/permission.tsx +4 -4
  50. package/src/cli/cmd/tui/routes/session/sidebar.tsx +4 -4
  51. package/src/cli/cmd/tui/thread.ts +9 -9
  52. package/src/cli/cmd/tui/ui/dialog-confirm.tsx +1 -1
  53. package/src/cli/cmd/tui/ui/dialog-help.tsx +2 -2
  54. package/src/cli/cmd/tui/ui/dialog-select.tsx +5 -5
  55. package/src/cli/cmd/tui/ui/dialog.tsx +3 -3
  56. package/src/cli/cmd/tui/ui/toast.tsx +1 -1
  57. package/src/cli/cmd/tui/util/editor.ts +3 -3
  58. package/src/cli/cmd/tui/util/transcript.ts +1 -1
  59. package/src/cli/cmd/tui/worker.ts +10 -10
  60. package/src/cli/error.ts +1 -1
  61. package/src/cli/ui.ts +1 -1
  62. package/src/cli/upgrade.ts +4 -4
  63. package/src/command/index.ts +1 -1
  64. package/src/config/config.ts +10 -10
  65. package/src/config/markdown.ts +1 -1
  66. package/src/config/migrate-tui-config.ts +5 -5
  67. package/src/config/paths.ts +4 -4
  68. package/src/config/tui.ts +4 -4
  69. package/src/control/control.sql.ts +1 -1
  70. package/src/control/index.ts +1 -1
  71. package/src/control-plane/adaptors/worktree.ts +1 -1
  72. package/src/control-plane/session-proxy-middleware.ts +1 -1
  73. package/src/control-plane/workspace.sql.ts +1 -1
  74. package/src/control-plane/workspace.ts +7 -7
  75. package/src/file/index.ts +1 -1
  76. package/src/file/ripgrep.ts +2 -2
  77. package/src/file/watcher.ts +5 -5
  78. package/src/format/formatter.ts +1 -1
  79. package/src/ide/index.ts +3 -3
  80. package/src/index.ts +1 -1
  81. package/src/installation/index.ts +3 -3
  82. package/src/lsp/client.ts +3 -3
  83. package/src/lsp/index.ts +3 -3
  84. package/src/mcp/index.ts +4 -4
  85. package/src/permission/index.ts +2 -2
  86. package/src/permission/next.ts +10 -10
  87. package/src/plugin/codex.ts +1 -1
  88. package/src/plugin/copilot.ts +2 -2
  89. package/src/plugin/index.ts +1 -1
  90. package/src/project/bootstrap.ts +2 -2
  91. package/src/project/instance.ts +4 -4
  92. package/src/project/project.sql.ts +1 -1
  93. package/src/project/project.ts +5 -5
  94. package/src/project/state.ts +1 -1
  95. package/src/project/vcs.ts +4 -4
  96. package/src/provider/auth.ts +4 -4
  97. package/src/provider/error.ts +1 -1
  98. package/src/provider/models-snapshot.ts +2 -0
  99. package/src/provider/models.ts +1 -1
  100. package/src/provider/provider.ts +2 -2
  101. package/src/provider/transform.ts +2 -2
  102. package/src/pty/index.ts +5 -5
  103. package/src/question/index.ts +5 -5
  104. package/src/server/event.ts +1 -1
  105. package/src/server/mdns.ts +1 -1
  106. package/src/server/routes/global.ts +3 -3
  107. package/src/server/routes/permission.ts +1 -1
  108. package/src/server/routes/pty.ts +1 -1
  109. package/src/server/routes/session.ts +4 -4
  110. package/src/server/routes/tui.ts +1 -1
  111. package/src/server/server.ts +3 -3
  112. package/src/session/compaction.ts +7 -7
  113. package/src/session/index.ts +10 -10
  114. package/src/session/instruction.ts +1 -1
  115. package/src/session/llm.ts +11 -11
  116. package/src/session/message-v2.ts +10 -10
  117. package/src/session/message.ts +1 -1
  118. package/src/session/processor.ts +10 -10
  119. package/src/session/prompt.ts +8 -8
  120. package/src/session/retry.ts +2 -2
  121. package/src/session/revert.ts +1 -1
  122. package/src/session/session.sql.ts +3 -3
  123. package/src/session/status.ts +3 -3
  124. package/src/session/summary.ts +5 -5
  125. package/src/session/system.ts +1 -1
  126. package/src/session/todo.ts +2 -2
  127. package/src/share/share-next.ts +7 -7
  128. package/src/share/share.sql.ts +1 -1
  129. package/src/shell/shell.ts +3 -3
  130. package/src/skill/skill.ts +6 -6
  131. package/src/storage/db.ts +1 -1
  132. package/src/storage/storage.ts +1 -1
  133. package/src/tool/bash.ts +6 -6
  134. package/src/tool/edit.ts +1 -1
  135. package/src/tool/registry.ts +2 -2
  136. package/src/tool/skill.ts +1 -1
  137. package/src/tool/task.ts +3 -3
  138. package/src/util/array.ts +10 -0
  139. package/src/util/binary.ts +41 -0
  140. package/src/util/encode.ts +51 -0
  141. package/src/util/error.ts +54 -0
  142. package/src/util/identifier.ts +48 -0
  143. package/src/util/lazy.ts +4 -16
  144. package/src/util/path.ts +37 -0
  145. package/src/util/retry.ts +41 -0
  146. package/src/util/slug.ts +74 -0
  147. package/src/worktree/index.ts +3 -3
  148. package/AGENTS.md +0 -10
  149. package/BUN_SHELL_MIGRATION_PLAN.md +0 -136
  150. package/Dockerfile +0 -18
  151. package/README.md +0 -15
  152. package/bunfig.toml +0 -7
  153. package/drizzle.config.ts +0 -10
  154. package/script/build.ts +0 -224
  155. package/script/check-migrations.ts +0 -16
  156. package/script/postinstall.mjs +0 -131
  157. package/script/publish.ts +0 -181
  158. package/script/schema.ts +0 -63
  159. package/script/seed-e2e.ts +0 -50
  160. package/sst-env.d.ts +0 -10
  161. package/test/AGENTS.md +0 -81
  162. package/test/acp/agent-interface.test.ts +0 -51
  163. package/test/acp/event-subscription.test.ts +0 -683
  164. package/test/agent/agent.test.ts +0 -689
  165. package/test/bun.test.ts +0 -53
  166. package/test/cli/github-action.test.ts +0 -197
  167. package/test/cli/github-remote.test.ts +0 -80
  168. package/test/cli/import.test.ts +0 -38
  169. package/test/cli/plugin-auth-picker.test.ts +0 -120
  170. package/test/cli/tui/transcript.test.ts +0 -322
  171. package/test/config/agent-color.test.ts +0 -71
  172. package/test/config/config.test.ts +0 -1886
  173. package/test/config/fixtures/empty-frontmatter.md +0 -4
  174. package/test/config/fixtures/frontmatter.md +0 -28
  175. package/test/config/fixtures/markdown-header.md +0 -11
  176. package/test/config/fixtures/no-frontmatter.md +0 -1
  177. package/test/config/fixtures/weird-model-id.md +0 -13
  178. package/test/config/markdown.test.ts +0 -228
  179. package/test/config/tui.test.ts +0 -510
  180. package/test/control-plane/session-proxy-middleware.test.ts +0 -147
  181. package/test/control-plane/sse.test.ts +0 -56
  182. package/test/control-plane/workspace-server-sse.test.ts +0 -65
  183. package/test/control-plane/workspace-sync.test.ts +0 -97
  184. package/test/file/ignore.test.ts +0 -10
  185. package/test/file/index.test.ts +0 -394
  186. package/test/file/path-traversal.test.ts +0 -198
  187. package/test/file/ripgrep.test.ts +0 -39
  188. package/test/file/time.test.ts +0 -361
  189. package/test/fixture/db.ts +0 -11
  190. package/test/fixture/fixture.ts +0 -45
  191. package/test/fixture/lsp/fake-lsp-server.js +0 -77
  192. package/test/fixture/skills/agents-sdk/SKILL.md +0 -152
  193. package/test/fixture/skills/agents-sdk/references/callable.md +0 -92
  194. package/test/fixture/skills/cloudflare/SKILL.md +0 -211
  195. package/test/fixture/skills/index.json +0 -6
  196. package/test/ide/ide.test.ts +0 -82
  197. package/test/keybind.test.ts +0 -421
  198. package/test/lsp/client.test.ts +0 -95
  199. package/test/mcp/headers.test.ts +0 -153
  200. package/test/mcp/oauth-browser.test.ts +0 -249
  201. package/test/memory/abort-leak.test.ts +0 -136
  202. package/test/patch/patch.test.ts +0 -348
  203. package/test/permission/arity.test.ts +0 -33
  204. package/test/permission/next.test.ts +0 -689
  205. package/test/permission-task.test.ts +0 -319
  206. package/test/plugin/auth-override.test.ts +0 -44
  207. package/test/plugin/codex.test.ts +0 -123
  208. package/test/preload.ts +0 -80
  209. package/test/project/project.test.ts +0 -348
  210. package/test/project/worktree-remove.test.ts +0 -65
  211. package/test/provider/amazon-bedrock.test.ts +0 -446
  212. package/test/provider/copilot/convert-to-copilot-messages.test.ts +0 -523
  213. package/test/provider/copilot/copilot-chat-model.test.ts +0 -592
  214. package/test/provider/gitlab-duo.test.ts +0 -262
  215. package/test/provider/provider.test.ts +0 -2220
  216. package/test/provider/transform.test.ts +0 -2353
  217. package/test/pty/pty-output-isolation.test.ts +0 -140
  218. package/test/question/question.test.ts +0 -300
  219. package/test/scheduler.test.ts +0 -73
  220. package/test/server/global-session-list.test.ts +0 -89
  221. package/test/server/session-list.test.ts +0 -90
  222. package/test/server/session-select.test.ts +0 -78
  223. package/test/session/compaction.test.ts +0 -423
  224. package/test/session/instruction.test.ts +0 -170
  225. package/test/session/llm.test.ts +0 -667
  226. package/test/session/message-v2.test.ts +0 -924
  227. package/test/session/prompt.test.ts +0 -211
  228. package/test/session/retry.test.ts +0 -188
  229. package/test/session/revert-compact.test.ts +0 -285
  230. package/test/session/session.test.ts +0 -71
  231. package/test/session/structured-output-integration.test.ts +0 -233
  232. package/test/session/structured-output.test.ts +0 -385
  233. package/test/skill/discovery.test.ts +0 -110
  234. package/test/skill/skill.test.ts +0 -388
  235. package/test/snapshot/snapshot.test.ts +0 -1180
  236. package/test/storage/json-migration.test.ts +0 -846
  237. package/test/tool/__snapshots__/tool.test.ts.snap +0 -9
  238. package/test/tool/apply_patch.test.ts +0 -566
  239. package/test/tool/bash.test.ts +0 -402
  240. package/test/tool/edit.test.ts +0 -496
  241. package/test/tool/external-directory.test.ts +0 -127
  242. package/test/tool/fixtures/large-image.png +0 -0
  243. package/test/tool/fixtures/models-api.json +0 -38413
  244. package/test/tool/grep.test.ts +0 -110
  245. package/test/tool/question.test.ts +0 -107
  246. package/test/tool/read.test.ts +0 -504
  247. package/test/tool/registry.test.ts +0 -122
  248. package/test/tool/skill.test.ts +0 -112
  249. package/test/tool/truncation.test.ts +0 -160
  250. package/test/tool/webfetch.test.ts +0 -100
  251. package/test/tool/write.test.ts +0 -348
  252. package/test/util/filesystem.test.ts +0 -443
  253. package/test/util/format.test.ts +0 -59
  254. package/test/util/glob.test.ts +0 -164
  255. package/test/util/iife.test.ts +0 -36
  256. package/test/util/lazy.test.ts +0 -50
  257. package/test/util/lock.test.ts +0 -72
  258. package/test/util/process.test.ts +0 -59
  259. package/test/util/timeout.test.ts +0 -21
  260. package/test/util/wildcard.test.ts +0 -90
  261. package/tsconfig.json +0 -16
@@ -1,2220 +0,0 @@
1
- import { test, expect } from "bun:test"
2
- import path from "path"
3
-
4
- import { tmpdir } from "../fixture/fixture"
5
- import { Instance } from "../../src/project/instance"
6
- import { Provider } from "../../src/provider/provider"
7
- import { Env } from "../../src/env"
8
-
9
- test("provider loaded from env variable", async () => {
10
- await using tmp = await tmpdir({
11
- init: async (dir) => {
12
- await Bun.write(
13
- path.join(dir, "opencode.json"),
14
- JSON.stringify({
15
- $schema: "https://opencode.ai/config.json",
16
- }),
17
- )
18
- },
19
- })
20
- await Instance.provide({
21
- directory: tmp.path,
22
- init: async () => {
23
- Env.set("ANTHROPIC_API_KEY", "test-api-key")
24
- },
25
- fn: async () => {
26
- const providers = await Provider.list()
27
- expect(providers["anthropic"]).toBeDefined()
28
- // Provider should retain its connection source even if custom loaders
29
- // merge additional options.
30
- expect(providers["anthropic"].source).toBe("env")
31
- expect(providers["anthropic"].options.headers["anthropic-beta"]).toBeDefined()
32
- },
33
- })
34
- })
35
-
36
- test("provider loaded from config with apiKey option", async () => {
37
- await using tmp = await tmpdir({
38
- init: async (dir) => {
39
- await Bun.write(
40
- path.join(dir, "opencode.json"),
41
- JSON.stringify({
42
- $schema: "https://opencode.ai/config.json",
43
- provider: {
44
- anthropic: {
45
- options: {
46
- apiKey: "config-api-key",
47
- },
48
- },
49
- },
50
- }),
51
- )
52
- },
53
- })
54
- await Instance.provide({
55
- directory: tmp.path,
56
- fn: async () => {
57
- const providers = await Provider.list()
58
- expect(providers["anthropic"]).toBeDefined()
59
- },
60
- })
61
- })
62
-
63
- test("disabled_providers excludes provider", async () => {
64
- await using tmp = await tmpdir({
65
- init: async (dir) => {
66
- await Bun.write(
67
- path.join(dir, "opencode.json"),
68
- JSON.stringify({
69
- $schema: "https://opencode.ai/config.json",
70
- disabled_providers: ["anthropic"],
71
- }),
72
- )
73
- },
74
- })
75
- await Instance.provide({
76
- directory: tmp.path,
77
- init: async () => {
78
- Env.set("ANTHROPIC_API_KEY", "test-api-key")
79
- },
80
- fn: async () => {
81
- const providers = await Provider.list()
82
- expect(providers["anthropic"]).toBeUndefined()
83
- },
84
- })
85
- })
86
-
87
- test("enabled_providers restricts to only listed providers", async () => {
88
- await using tmp = await tmpdir({
89
- init: async (dir) => {
90
- await Bun.write(
91
- path.join(dir, "opencode.json"),
92
- JSON.stringify({
93
- $schema: "https://opencode.ai/config.json",
94
- enabled_providers: ["anthropic"],
95
- }),
96
- )
97
- },
98
- })
99
- await Instance.provide({
100
- directory: tmp.path,
101
- init: async () => {
102
- Env.set("ANTHROPIC_API_KEY", "test-api-key")
103
- Env.set("OPENAI_API_KEY", "test-openai-key")
104
- },
105
- fn: async () => {
106
- const providers = await Provider.list()
107
- expect(providers["anthropic"]).toBeDefined()
108
- expect(providers["openai"]).toBeUndefined()
109
- },
110
- })
111
- })
112
-
113
- test("model whitelist filters models for provider", async () => {
114
- await using tmp = await tmpdir({
115
- init: async (dir) => {
116
- await Bun.write(
117
- path.join(dir, "opencode.json"),
118
- JSON.stringify({
119
- $schema: "https://opencode.ai/config.json",
120
- provider: {
121
- anthropic: {
122
- whitelist: ["claude-sonnet-4-20250514"],
123
- },
124
- },
125
- }),
126
- )
127
- },
128
- })
129
- await Instance.provide({
130
- directory: tmp.path,
131
- init: async () => {
132
- Env.set("ANTHROPIC_API_KEY", "test-api-key")
133
- },
134
- fn: async () => {
135
- const providers = await Provider.list()
136
- expect(providers["anthropic"]).toBeDefined()
137
- const models = Object.keys(providers["anthropic"].models)
138
- expect(models).toContain("claude-sonnet-4-20250514")
139
- expect(models.length).toBe(1)
140
- },
141
- })
142
- })
143
-
144
- test("model blacklist excludes specific models", async () => {
145
- await using tmp = await tmpdir({
146
- init: async (dir) => {
147
- await Bun.write(
148
- path.join(dir, "opencode.json"),
149
- JSON.stringify({
150
- $schema: "https://opencode.ai/config.json",
151
- provider: {
152
- anthropic: {
153
- blacklist: ["claude-sonnet-4-20250514"],
154
- },
155
- },
156
- }),
157
- )
158
- },
159
- })
160
- await Instance.provide({
161
- directory: tmp.path,
162
- init: async () => {
163
- Env.set("ANTHROPIC_API_KEY", "test-api-key")
164
- },
165
- fn: async () => {
166
- const providers = await Provider.list()
167
- expect(providers["anthropic"]).toBeDefined()
168
- const models = Object.keys(providers["anthropic"].models)
169
- expect(models).not.toContain("claude-sonnet-4-20250514")
170
- },
171
- })
172
- })
173
-
174
- test("custom model alias via config", async () => {
175
- await using tmp = await tmpdir({
176
- init: async (dir) => {
177
- await Bun.write(
178
- path.join(dir, "opencode.json"),
179
- JSON.stringify({
180
- $schema: "https://opencode.ai/config.json",
181
- provider: {
182
- anthropic: {
183
- models: {
184
- "my-alias": {
185
- id: "claude-sonnet-4-20250514",
186
- name: "My Custom Alias",
187
- },
188
- },
189
- },
190
- },
191
- }),
192
- )
193
- },
194
- })
195
- await Instance.provide({
196
- directory: tmp.path,
197
- init: async () => {
198
- Env.set("ANTHROPIC_API_KEY", "test-api-key")
199
- },
200
- fn: async () => {
201
- const providers = await Provider.list()
202
- expect(providers["anthropic"]).toBeDefined()
203
- expect(providers["anthropic"].models["my-alias"]).toBeDefined()
204
- expect(providers["anthropic"].models["my-alias"].name).toBe("My Custom Alias")
205
- },
206
- })
207
- })
208
-
209
- test("custom provider with npm package", async () => {
210
- await using tmp = await tmpdir({
211
- init: async (dir) => {
212
- await Bun.write(
213
- path.join(dir, "opencode.json"),
214
- JSON.stringify({
215
- $schema: "https://opencode.ai/config.json",
216
- provider: {
217
- "custom-provider": {
218
- name: "Custom Provider",
219
- npm: "@ai-sdk/openai-compatible",
220
- api: "https://api.custom.com/v1",
221
- env: ["CUSTOM_API_KEY"],
222
- models: {
223
- "custom-model": {
224
- name: "Custom Model",
225
- tool_call: true,
226
- limit: {
227
- context: 128000,
228
- output: 4096,
229
- },
230
- },
231
- },
232
- options: {
233
- apiKey: "custom-key",
234
- },
235
- },
236
- },
237
- }),
238
- )
239
- },
240
- })
241
- await Instance.provide({
242
- directory: tmp.path,
243
- fn: async () => {
244
- const providers = await Provider.list()
245
- expect(providers["custom-provider"]).toBeDefined()
246
- expect(providers["custom-provider"].name).toBe("Custom Provider")
247
- expect(providers["custom-provider"].models["custom-model"]).toBeDefined()
248
- },
249
- })
250
- })
251
-
252
- test("env variable takes precedence, config merges options", async () => {
253
- await using tmp = await tmpdir({
254
- init: async (dir) => {
255
- await Bun.write(
256
- path.join(dir, "opencode.json"),
257
- JSON.stringify({
258
- $schema: "https://opencode.ai/config.json",
259
- provider: {
260
- anthropic: {
261
- options: {
262
- timeout: 60000,
263
- },
264
- },
265
- },
266
- }),
267
- )
268
- },
269
- })
270
- await Instance.provide({
271
- directory: tmp.path,
272
- init: async () => {
273
- Env.set("ANTHROPIC_API_KEY", "env-api-key")
274
- },
275
- fn: async () => {
276
- const providers = await Provider.list()
277
- expect(providers["anthropic"]).toBeDefined()
278
- // Config options should be merged
279
- expect(providers["anthropic"].options.timeout).toBe(60000)
280
- },
281
- })
282
- })
283
-
284
- test("getModel returns model for valid provider/model", async () => {
285
- await using tmp = await tmpdir({
286
- init: async (dir) => {
287
- await Bun.write(
288
- path.join(dir, "opencode.json"),
289
- JSON.stringify({
290
- $schema: "https://opencode.ai/config.json",
291
- }),
292
- )
293
- },
294
- })
295
- await Instance.provide({
296
- directory: tmp.path,
297
- init: async () => {
298
- Env.set("ANTHROPIC_API_KEY", "test-api-key")
299
- },
300
- fn: async () => {
301
- const model = await Provider.getModel("anthropic", "claude-sonnet-4-20250514")
302
- expect(model).toBeDefined()
303
- expect(model.providerID).toBe("anthropic")
304
- expect(model.id).toBe("claude-sonnet-4-20250514")
305
- const language = await Provider.getLanguage(model)
306
- expect(language).toBeDefined()
307
- },
308
- })
309
- })
310
-
311
- test("getModel throws ModelNotFoundError for invalid model", async () => {
312
- await using tmp = await tmpdir({
313
- init: async (dir) => {
314
- await Bun.write(
315
- path.join(dir, "opencode.json"),
316
- JSON.stringify({
317
- $schema: "https://opencode.ai/config.json",
318
- }),
319
- )
320
- },
321
- })
322
- await Instance.provide({
323
- directory: tmp.path,
324
- init: async () => {
325
- Env.set("ANTHROPIC_API_KEY", "test-api-key")
326
- },
327
- fn: async () => {
328
- expect(Provider.getModel("anthropic", "nonexistent-model")).rejects.toThrow()
329
- },
330
- })
331
- })
332
-
333
- test("getModel throws ModelNotFoundError for invalid provider", async () => {
334
- await using tmp = await tmpdir({
335
- init: async (dir) => {
336
- await Bun.write(
337
- path.join(dir, "opencode.json"),
338
- JSON.stringify({
339
- $schema: "https://opencode.ai/config.json",
340
- }),
341
- )
342
- },
343
- })
344
- await Instance.provide({
345
- directory: tmp.path,
346
- fn: async () => {
347
- expect(Provider.getModel("nonexistent-provider", "some-model")).rejects.toThrow()
348
- },
349
- })
350
- })
351
-
352
- test("parseModel correctly parses provider/model string", () => {
353
- const result = Provider.parseModel("anthropic/claude-sonnet-4")
354
- expect(result.providerID).toBe("anthropic")
355
- expect(result.modelID).toBe("claude-sonnet-4")
356
- })
357
-
358
- test("parseModel handles model IDs with slashes", () => {
359
- const result = Provider.parseModel("openrouter/anthropic/claude-3-opus")
360
- expect(result.providerID).toBe("openrouter")
361
- expect(result.modelID).toBe("anthropic/claude-3-opus")
362
- })
363
-
364
- test("defaultModel returns first available model when no config set", async () => {
365
- await using tmp = await tmpdir({
366
- init: async (dir) => {
367
- await Bun.write(
368
- path.join(dir, "opencode.json"),
369
- JSON.stringify({
370
- $schema: "https://opencode.ai/config.json",
371
- }),
372
- )
373
- },
374
- })
375
- await Instance.provide({
376
- directory: tmp.path,
377
- init: async () => {
378
- Env.set("ANTHROPIC_API_KEY", "test-api-key")
379
- },
380
- fn: async () => {
381
- const model = await Provider.defaultModel()
382
- expect(model.providerID).toBeDefined()
383
- expect(model.modelID).toBeDefined()
384
- },
385
- })
386
- })
387
-
388
- test("defaultModel respects config model setting", async () => {
389
- await using tmp = await tmpdir({
390
- init: async (dir) => {
391
- await Bun.write(
392
- path.join(dir, "opencode.json"),
393
- JSON.stringify({
394
- $schema: "https://opencode.ai/config.json",
395
- model: "anthropic/claude-sonnet-4-20250514",
396
- }),
397
- )
398
- },
399
- })
400
- await Instance.provide({
401
- directory: tmp.path,
402
- init: async () => {
403
- Env.set("ANTHROPIC_API_KEY", "test-api-key")
404
- },
405
- fn: async () => {
406
- const model = await Provider.defaultModel()
407
- expect(model.providerID).toBe("anthropic")
408
- expect(model.modelID).toBe("claude-sonnet-4-20250514")
409
- },
410
- })
411
- })
412
-
413
- test("provider with baseURL from config", async () => {
414
- await using tmp = await tmpdir({
415
- init: async (dir) => {
416
- await Bun.write(
417
- path.join(dir, "opencode.json"),
418
- JSON.stringify({
419
- $schema: "https://opencode.ai/config.json",
420
- provider: {
421
- "custom-openai": {
422
- name: "Custom OpenAI",
423
- npm: "@ai-sdk/openai-compatible",
424
- env: [],
425
- models: {
426
- "gpt-4": {
427
- name: "GPT-4",
428
- tool_call: true,
429
- limit: { context: 128000, output: 4096 },
430
- },
431
- },
432
- options: {
433
- apiKey: "test-key",
434
- baseURL: "https://custom.openai.com/v1",
435
- },
436
- },
437
- },
438
- }),
439
- )
440
- },
441
- })
442
- await Instance.provide({
443
- directory: tmp.path,
444
- fn: async () => {
445
- const providers = await Provider.list()
446
- expect(providers["custom-openai"]).toBeDefined()
447
- expect(providers["custom-openai"].options.baseURL).toBe("https://custom.openai.com/v1")
448
- },
449
- })
450
- })
451
-
452
- test("model cost defaults to zero when not specified", async () => {
453
- await using tmp = await tmpdir({
454
- init: async (dir) => {
455
- await Bun.write(
456
- path.join(dir, "opencode.json"),
457
- JSON.stringify({
458
- $schema: "https://opencode.ai/config.json",
459
- provider: {
460
- "test-provider": {
461
- name: "Test Provider",
462
- npm: "@ai-sdk/openai-compatible",
463
- env: [],
464
- models: {
465
- "test-model": {
466
- name: "Test Model",
467
- tool_call: true,
468
- limit: { context: 128000, output: 4096 },
469
- },
470
- },
471
- options: {
472
- apiKey: "test-key",
473
- },
474
- },
475
- },
476
- }),
477
- )
478
- },
479
- })
480
- await Instance.provide({
481
- directory: tmp.path,
482
- fn: async () => {
483
- const providers = await Provider.list()
484
- const model = providers["test-provider"].models["test-model"]
485
- expect(model.cost.input).toBe(0)
486
- expect(model.cost.output).toBe(0)
487
- expect(model.cost.cache.read).toBe(0)
488
- expect(model.cost.cache.write).toBe(0)
489
- },
490
- })
491
- })
492
-
493
- test("model options are merged from existing model", async () => {
494
- await using tmp = await tmpdir({
495
- init: async (dir) => {
496
- await Bun.write(
497
- path.join(dir, "opencode.json"),
498
- JSON.stringify({
499
- $schema: "https://opencode.ai/config.json",
500
- provider: {
501
- anthropic: {
502
- models: {
503
- "claude-sonnet-4-20250514": {
504
- options: {
505
- customOption: "custom-value",
506
- },
507
- },
508
- },
509
- },
510
- },
511
- }),
512
- )
513
- },
514
- })
515
- await Instance.provide({
516
- directory: tmp.path,
517
- init: async () => {
518
- Env.set("ANTHROPIC_API_KEY", "test-api-key")
519
- },
520
- fn: async () => {
521
- const providers = await Provider.list()
522
- const model = providers["anthropic"].models["claude-sonnet-4-20250514"]
523
- expect(model.options.customOption).toBe("custom-value")
524
- },
525
- })
526
- })
527
-
528
- test("provider removed when all models filtered out", async () => {
529
- await using tmp = await tmpdir({
530
- init: async (dir) => {
531
- await Bun.write(
532
- path.join(dir, "opencode.json"),
533
- JSON.stringify({
534
- $schema: "https://opencode.ai/config.json",
535
- provider: {
536
- anthropic: {
537
- whitelist: ["nonexistent-model"],
538
- },
539
- },
540
- }),
541
- )
542
- },
543
- })
544
- await Instance.provide({
545
- directory: tmp.path,
546
- init: async () => {
547
- Env.set("ANTHROPIC_API_KEY", "test-api-key")
548
- },
549
- fn: async () => {
550
- const providers = await Provider.list()
551
- expect(providers["anthropic"]).toBeUndefined()
552
- },
553
- })
554
- })
555
-
556
- test("closest finds model by partial match", async () => {
557
- await using tmp = await tmpdir({
558
- init: async (dir) => {
559
- await Bun.write(
560
- path.join(dir, "opencode.json"),
561
- JSON.stringify({
562
- $schema: "https://opencode.ai/config.json",
563
- }),
564
- )
565
- },
566
- })
567
- await Instance.provide({
568
- directory: tmp.path,
569
- init: async () => {
570
- Env.set("ANTHROPIC_API_KEY", "test-api-key")
571
- },
572
- fn: async () => {
573
- const result = await Provider.closest("anthropic", ["sonnet-4"])
574
- expect(result).toBeDefined()
575
- expect(result?.providerID).toBe("anthropic")
576
- expect(result?.modelID).toContain("sonnet-4")
577
- },
578
- })
579
- })
580
-
581
- test("closest returns undefined for nonexistent provider", async () => {
582
- await using tmp = await tmpdir({
583
- init: async (dir) => {
584
- await Bun.write(
585
- path.join(dir, "opencode.json"),
586
- JSON.stringify({
587
- $schema: "https://opencode.ai/config.json",
588
- }),
589
- )
590
- },
591
- })
592
- await Instance.provide({
593
- directory: tmp.path,
594
- fn: async () => {
595
- const result = await Provider.closest("nonexistent", ["model"])
596
- expect(result).toBeUndefined()
597
- },
598
- })
599
- })
600
-
601
- test("getModel uses realIdByKey for aliased models", async () => {
602
- await using tmp = await tmpdir({
603
- init: async (dir) => {
604
- await Bun.write(
605
- path.join(dir, "opencode.json"),
606
- JSON.stringify({
607
- $schema: "https://opencode.ai/config.json",
608
- provider: {
609
- anthropic: {
610
- models: {
611
- "my-sonnet": {
612
- id: "claude-sonnet-4-20250514",
613
- name: "My Sonnet Alias",
614
- },
615
- },
616
- },
617
- },
618
- }),
619
- )
620
- },
621
- })
622
- await Instance.provide({
623
- directory: tmp.path,
624
- init: async () => {
625
- Env.set("ANTHROPIC_API_KEY", "test-api-key")
626
- },
627
- fn: async () => {
628
- const providers = await Provider.list()
629
- expect(providers["anthropic"].models["my-sonnet"]).toBeDefined()
630
-
631
- const model = await Provider.getModel("anthropic", "my-sonnet")
632
- expect(model).toBeDefined()
633
- expect(model.id).toBe("my-sonnet")
634
- expect(model.name).toBe("My Sonnet Alias")
635
- },
636
- })
637
- })
638
-
639
- test("provider api field sets model api.url", async () => {
640
- await using tmp = await tmpdir({
641
- init: async (dir) => {
642
- await Bun.write(
643
- path.join(dir, "opencode.json"),
644
- JSON.stringify({
645
- $schema: "https://opencode.ai/config.json",
646
- provider: {
647
- "custom-api": {
648
- name: "Custom API",
649
- npm: "@ai-sdk/openai-compatible",
650
- api: "https://api.example.com/v1",
651
- env: [],
652
- models: {
653
- "model-1": {
654
- name: "Model 1",
655
- tool_call: true,
656
- limit: { context: 8000, output: 2000 },
657
- },
658
- },
659
- options: {
660
- apiKey: "test-key",
661
- },
662
- },
663
- },
664
- }),
665
- )
666
- },
667
- })
668
- await Instance.provide({
669
- directory: tmp.path,
670
- fn: async () => {
671
- const providers = await Provider.list()
672
- // api field is stored on model.api.url, used by getSDK to set baseURL
673
- expect(providers["custom-api"].models["model-1"].api.url).toBe("https://api.example.com/v1")
674
- },
675
- })
676
- })
677
-
678
- test("explicit baseURL overrides api field", async () => {
679
- await using tmp = await tmpdir({
680
- init: async (dir) => {
681
- await Bun.write(
682
- path.join(dir, "opencode.json"),
683
- JSON.stringify({
684
- $schema: "https://opencode.ai/config.json",
685
- provider: {
686
- "custom-api": {
687
- name: "Custom API",
688
- npm: "@ai-sdk/openai-compatible",
689
- api: "https://api.example.com/v1",
690
- env: [],
691
- models: {
692
- "model-1": {
693
- name: "Model 1",
694
- tool_call: true,
695
- limit: { context: 8000, output: 2000 },
696
- },
697
- },
698
- options: {
699
- apiKey: "test-key",
700
- baseURL: "https://custom.override.com/v1",
701
- },
702
- },
703
- },
704
- }),
705
- )
706
- },
707
- })
708
- await Instance.provide({
709
- directory: tmp.path,
710
- fn: async () => {
711
- const providers = await Provider.list()
712
- expect(providers["custom-api"].options.baseURL).toBe("https://custom.override.com/v1")
713
- },
714
- })
715
- })
716
-
717
- test("model inherits properties from existing database model", async () => {
718
- await using tmp = await tmpdir({
719
- init: async (dir) => {
720
- await Bun.write(
721
- path.join(dir, "opencode.json"),
722
- JSON.stringify({
723
- $schema: "https://opencode.ai/config.json",
724
- provider: {
725
- anthropic: {
726
- models: {
727
- "claude-sonnet-4-20250514": {
728
- name: "Custom Name for Sonnet",
729
- },
730
- },
731
- },
732
- },
733
- }),
734
- )
735
- },
736
- })
737
- await Instance.provide({
738
- directory: tmp.path,
739
- init: async () => {
740
- Env.set("ANTHROPIC_API_KEY", "test-api-key")
741
- },
742
- fn: async () => {
743
- const providers = await Provider.list()
744
- const model = providers["anthropic"].models["claude-sonnet-4-20250514"]
745
- expect(model.name).toBe("Custom Name for Sonnet")
746
- expect(model.capabilities.toolcall).toBe(true)
747
- expect(model.capabilities.attachment).toBe(true)
748
- expect(model.limit.context).toBeGreaterThan(0)
749
- },
750
- })
751
- })
752
-
753
- test("disabled_providers prevents loading even with env var", async () => {
754
- await using tmp = await tmpdir({
755
- init: async (dir) => {
756
- await Bun.write(
757
- path.join(dir, "opencode.json"),
758
- JSON.stringify({
759
- $schema: "https://opencode.ai/config.json",
760
- disabled_providers: ["openai"],
761
- }),
762
- )
763
- },
764
- })
765
- await Instance.provide({
766
- directory: tmp.path,
767
- init: async () => {
768
- Env.set("OPENAI_API_KEY", "test-openai-key")
769
- },
770
- fn: async () => {
771
- const providers = await Provider.list()
772
- expect(providers["openai"]).toBeUndefined()
773
- },
774
- })
775
- })
776
-
777
- test("enabled_providers with empty array allows no providers", async () => {
778
- await using tmp = await tmpdir({
779
- init: async (dir) => {
780
- await Bun.write(
781
- path.join(dir, "opencode.json"),
782
- JSON.stringify({
783
- $schema: "https://opencode.ai/config.json",
784
- enabled_providers: [],
785
- }),
786
- )
787
- },
788
- })
789
- await Instance.provide({
790
- directory: tmp.path,
791
- init: async () => {
792
- Env.set("ANTHROPIC_API_KEY", "test-api-key")
793
- Env.set("OPENAI_API_KEY", "test-openai-key")
794
- },
795
- fn: async () => {
796
- const providers = await Provider.list()
797
- expect(Object.keys(providers).length).toBe(0)
798
- },
799
- })
800
- })
801
-
802
- test("whitelist and blacklist can be combined", async () => {
803
- await using tmp = await tmpdir({
804
- init: async (dir) => {
805
- await Bun.write(
806
- path.join(dir, "opencode.json"),
807
- JSON.stringify({
808
- $schema: "https://opencode.ai/config.json",
809
- provider: {
810
- anthropic: {
811
- whitelist: ["claude-sonnet-4-20250514", "claude-opus-4-20250514"],
812
- blacklist: ["claude-opus-4-20250514"],
813
- },
814
- },
815
- }),
816
- )
817
- },
818
- })
819
- await Instance.provide({
820
- directory: tmp.path,
821
- init: async () => {
822
- Env.set("ANTHROPIC_API_KEY", "test-api-key")
823
- },
824
- fn: async () => {
825
- const providers = await Provider.list()
826
- expect(providers["anthropic"]).toBeDefined()
827
- const models = Object.keys(providers["anthropic"].models)
828
- expect(models).toContain("claude-sonnet-4-20250514")
829
- expect(models).not.toContain("claude-opus-4-20250514")
830
- expect(models.length).toBe(1)
831
- },
832
- })
833
- })
834
-
835
- test("model modalities default correctly", async () => {
836
- await using tmp = await tmpdir({
837
- init: async (dir) => {
838
- await Bun.write(
839
- path.join(dir, "opencode.json"),
840
- JSON.stringify({
841
- $schema: "https://opencode.ai/config.json",
842
- provider: {
843
- "test-provider": {
844
- name: "Test",
845
- npm: "@ai-sdk/openai-compatible",
846
- env: [],
847
- models: {
848
- "test-model": {
849
- name: "Test Model",
850
- tool_call: true,
851
- limit: { context: 8000, output: 2000 },
852
- },
853
- },
854
- options: { apiKey: "test" },
855
- },
856
- },
857
- }),
858
- )
859
- },
860
- })
861
- await Instance.provide({
862
- directory: tmp.path,
863
- fn: async () => {
864
- const providers = await Provider.list()
865
- const model = providers["test-provider"].models["test-model"]
866
- expect(model.capabilities.input.text).toBe(true)
867
- expect(model.capabilities.output.text).toBe(true)
868
- },
869
- })
870
- })
871
-
872
- test("model with custom cost values", async () => {
873
- await using tmp = await tmpdir({
874
- init: async (dir) => {
875
- await Bun.write(
876
- path.join(dir, "opencode.json"),
877
- JSON.stringify({
878
- $schema: "https://opencode.ai/config.json",
879
- provider: {
880
- "test-provider": {
881
- name: "Test",
882
- npm: "@ai-sdk/openai-compatible",
883
- env: [],
884
- models: {
885
- "test-model": {
886
- name: "Test Model",
887
- tool_call: true,
888
- limit: { context: 8000, output: 2000 },
889
- cost: {
890
- input: 5,
891
- output: 15,
892
- cache_read: 2.5,
893
- cache_write: 7.5,
894
- },
895
- },
896
- },
897
- options: { apiKey: "test" },
898
- },
899
- },
900
- }),
901
- )
902
- },
903
- })
904
- await Instance.provide({
905
- directory: tmp.path,
906
- fn: async () => {
907
- const providers = await Provider.list()
908
- const model = providers["test-provider"].models["test-model"]
909
- expect(model.cost.input).toBe(5)
910
- expect(model.cost.output).toBe(15)
911
- expect(model.cost.cache.read).toBe(2.5)
912
- expect(model.cost.cache.write).toBe(7.5)
913
- },
914
- })
915
- })
916
-
917
- test("getSmallModel returns appropriate small model", async () => {
918
- await using tmp = await tmpdir({
919
- init: async (dir) => {
920
- await Bun.write(
921
- path.join(dir, "opencode.json"),
922
- JSON.stringify({
923
- $schema: "https://opencode.ai/config.json",
924
- }),
925
- )
926
- },
927
- })
928
- await Instance.provide({
929
- directory: tmp.path,
930
- init: async () => {
931
- Env.set("ANTHROPIC_API_KEY", "test-api-key")
932
- },
933
- fn: async () => {
934
- const model = await Provider.getSmallModel("anthropic")
935
- expect(model).toBeDefined()
936
- expect(model?.id).toContain("haiku")
937
- },
938
- })
939
- })
940
-
941
- test("getSmallModel respects config small_model override", async () => {
942
- await using tmp = await tmpdir({
943
- init: async (dir) => {
944
- await Bun.write(
945
- path.join(dir, "opencode.json"),
946
- JSON.stringify({
947
- $schema: "https://opencode.ai/config.json",
948
- small_model: "anthropic/claude-sonnet-4-20250514",
949
- }),
950
- )
951
- },
952
- })
953
- await Instance.provide({
954
- directory: tmp.path,
955
- init: async () => {
956
- Env.set("ANTHROPIC_API_KEY", "test-api-key")
957
- },
958
- fn: async () => {
959
- const model = await Provider.getSmallModel("anthropic")
960
- expect(model).toBeDefined()
961
- expect(model?.providerID).toBe("anthropic")
962
- expect(model?.id).toBe("claude-sonnet-4-20250514")
963
- },
964
- })
965
- })
966
-
967
- test("provider.sort prioritizes preferred models", () => {
968
- const models = [
969
- { id: "random-model", name: "Random" },
970
- { id: "claude-sonnet-4-latest", name: "Claude Sonnet 4" },
971
- { id: "gpt-5-turbo", name: "GPT-5 Turbo" },
972
- { id: "other-model", name: "Other" },
973
- ] as any[]
974
-
975
- const sorted = Provider.sort(models)
976
- expect(sorted[0].id).toContain("sonnet-4")
977
- expect(sorted[0].id).toContain("latest")
978
- expect(sorted[sorted.length - 1].id).not.toContain("gpt-5")
979
- expect(sorted[sorted.length - 1].id).not.toContain("sonnet-4")
980
- })
981
-
982
- test("multiple providers can be configured simultaneously", async () => {
983
- await using tmp = await tmpdir({
984
- init: async (dir) => {
985
- await Bun.write(
986
- path.join(dir, "opencode.json"),
987
- JSON.stringify({
988
- $schema: "https://opencode.ai/config.json",
989
- provider: {
990
- anthropic: {
991
- options: { timeout: 30000 },
992
- },
993
- openai: {
994
- options: { timeout: 60000 },
995
- },
996
- },
997
- }),
998
- )
999
- },
1000
- })
1001
- await Instance.provide({
1002
- directory: tmp.path,
1003
- init: async () => {
1004
- Env.set("ANTHROPIC_API_KEY", "test-anthropic-key")
1005
- Env.set("OPENAI_API_KEY", "test-openai-key")
1006
- },
1007
- fn: async () => {
1008
- const providers = await Provider.list()
1009
- expect(providers["anthropic"]).toBeDefined()
1010
- expect(providers["openai"]).toBeDefined()
1011
- expect(providers["anthropic"].options.timeout).toBe(30000)
1012
- expect(providers["openai"].options.timeout).toBe(60000)
1013
- },
1014
- })
1015
- })
1016
-
1017
- test("provider with custom npm package", async () => {
1018
- await using tmp = await tmpdir({
1019
- init: async (dir) => {
1020
- await Bun.write(
1021
- path.join(dir, "opencode.json"),
1022
- JSON.stringify({
1023
- $schema: "https://opencode.ai/config.json",
1024
- provider: {
1025
- "local-llm": {
1026
- name: "Local LLM",
1027
- npm: "@ai-sdk/openai-compatible",
1028
- env: [],
1029
- models: {
1030
- "llama-3": {
1031
- name: "Llama 3",
1032
- tool_call: true,
1033
- limit: { context: 8192, output: 2048 },
1034
- },
1035
- },
1036
- options: {
1037
- apiKey: "not-needed",
1038
- baseURL: "http://localhost:11434/v1",
1039
- },
1040
- },
1041
- },
1042
- }),
1043
- )
1044
- },
1045
- })
1046
- await Instance.provide({
1047
- directory: tmp.path,
1048
- fn: async () => {
1049
- const providers = await Provider.list()
1050
- expect(providers["local-llm"]).toBeDefined()
1051
- expect(providers["local-llm"].models["llama-3"].api.npm).toBe("@ai-sdk/openai-compatible")
1052
- expect(providers["local-llm"].options.baseURL).toBe("http://localhost:11434/v1")
1053
- },
1054
- })
1055
- })
1056
-
1057
- // Edge cases for model configuration
1058
-
1059
- test("model alias name defaults to alias key when id differs", async () => {
1060
- await using tmp = await tmpdir({
1061
- init: async (dir) => {
1062
- await Bun.write(
1063
- path.join(dir, "opencode.json"),
1064
- JSON.stringify({
1065
- $schema: "https://opencode.ai/config.json",
1066
- provider: {
1067
- anthropic: {
1068
- models: {
1069
- sonnet: {
1070
- id: "claude-sonnet-4-20250514",
1071
- // no name specified - should default to "sonnet" (the key)
1072
- },
1073
- },
1074
- },
1075
- },
1076
- }),
1077
- )
1078
- },
1079
- })
1080
- await Instance.provide({
1081
- directory: tmp.path,
1082
- init: async () => {
1083
- Env.set("ANTHROPIC_API_KEY", "test-api-key")
1084
- },
1085
- fn: async () => {
1086
- const providers = await Provider.list()
1087
- expect(providers["anthropic"].models["sonnet"].name).toBe("sonnet")
1088
- },
1089
- })
1090
- })
1091
-
1092
- test("provider with multiple env var options only includes apiKey when single env", async () => {
1093
- await using tmp = await tmpdir({
1094
- init: async (dir) => {
1095
- await Bun.write(
1096
- path.join(dir, "opencode.json"),
1097
- JSON.stringify({
1098
- $schema: "https://opencode.ai/config.json",
1099
- provider: {
1100
- "multi-env": {
1101
- name: "Multi Env Provider",
1102
- npm: "@ai-sdk/openai-compatible",
1103
- env: ["MULTI_ENV_KEY_1", "MULTI_ENV_KEY_2"],
1104
- models: {
1105
- "model-1": {
1106
- name: "Model 1",
1107
- tool_call: true,
1108
- limit: { context: 8000, output: 2000 },
1109
- },
1110
- },
1111
- options: {
1112
- baseURL: "https://api.example.com/v1",
1113
- },
1114
- },
1115
- },
1116
- }),
1117
- )
1118
- },
1119
- })
1120
- await Instance.provide({
1121
- directory: tmp.path,
1122
- init: async () => {
1123
- Env.set("MULTI_ENV_KEY_1", "test-key")
1124
- },
1125
- fn: async () => {
1126
- const providers = await Provider.list()
1127
- expect(providers["multi-env"]).toBeDefined()
1128
- // When multiple env options exist, key should NOT be auto-set
1129
- expect(providers["multi-env"].key).toBeUndefined()
1130
- },
1131
- })
1132
- })
1133
-
1134
- test("provider with single env var includes apiKey automatically", async () => {
1135
- await using tmp = await tmpdir({
1136
- init: async (dir) => {
1137
- await Bun.write(
1138
- path.join(dir, "opencode.json"),
1139
- JSON.stringify({
1140
- $schema: "https://opencode.ai/config.json",
1141
- provider: {
1142
- "single-env": {
1143
- name: "Single Env Provider",
1144
- npm: "@ai-sdk/openai-compatible",
1145
- env: ["SINGLE_ENV_KEY"],
1146
- models: {
1147
- "model-1": {
1148
- name: "Model 1",
1149
- tool_call: true,
1150
- limit: { context: 8000, output: 2000 },
1151
- },
1152
- },
1153
- options: {
1154
- baseURL: "https://api.example.com/v1",
1155
- },
1156
- },
1157
- },
1158
- }),
1159
- )
1160
- },
1161
- })
1162
- await Instance.provide({
1163
- directory: tmp.path,
1164
- init: async () => {
1165
- Env.set("SINGLE_ENV_KEY", "my-api-key")
1166
- },
1167
- fn: async () => {
1168
- const providers = await Provider.list()
1169
- expect(providers["single-env"]).toBeDefined()
1170
- // Single env option should auto-set key
1171
- expect(providers["single-env"].key).toBe("my-api-key")
1172
- },
1173
- })
1174
- })
1175
-
1176
- test("model cost overrides existing cost values", async () => {
1177
- await using tmp = await tmpdir({
1178
- init: async (dir) => {
1179
- await Bun.write(
1180
- path.join(dir, "opencode.json"),
1181
- JSON.stringify({
1182
- $schema: "https://opencode.ai/config.json",
1183
- provider: {
1184
- anthropic: {
1185
- models: {
1186
- "claude-sonnet-4-20250514": {
1187
- cost: {
1188
- input: 999,
1189
- output: 888,
1190
- },
1191
- },
1192
- },
1193
- },
1194
- },
1195
- }),
1196
- )
1197
- },
1198
- })
1199
- await Instance.provide({
1200
- directory: tmp.path,
1201
- init: async () => {
1202
- Env.set("ANTHROPIC_API_KEY", "test-api-key")
1203
- },
1204
- fn: async () => {
1205
- const providers = await Provider.list()
1206
- const model = providers["anthropic"].models["claude-sonnet-4-20250514"]
1207
- expect(model.cost.input).toBe(999)
1208
- expect(model.cost.output).toBe(888)
1209
- },
1210
- })
1211
- })
1212
-
1213
- test("completely new provider not in database can be configured", async () => {
1214
- await using tmp = await tmpdir({
1215
- init: async (dir) => {
1216
- await Bun.write(
1217
- path.join(dir, "opencode.json"),
1218
- JSON.stringify({
1219
- $schema: "https://opencode.ai/config.json",
1220
- provider: {
1221
- "brand-new-provider": {
1222
- name: "Brand New",
1223
- npm: "@ai-sdk/openai-compatible",
1224
- env: [],
1225
- api: "https://new-api.com/v1",
1226
- models: {
1227
- "new-model": {
1228
- name: "New Model",
1229
- tool_call: true,
1230
- reasoning: true,
1231
- attachment: true,
1232
- temperature: true,
1233
- limit: { context: 32000, output: 8000 },
1234
- modalities: {
1235
- input: ["text", "image"],
1236
- output: ["text"],
1237
- },
1238
- },
1239
- },
1240
- options: {
1241
- apiKey: "new-key",
1242
- },
1243
- },
1244
- },
1245
- }),
1246
- )
1247
- },
1248
- })
1249
- await Instance.provide({
1250
- directory: tmp.path,
1251
- fn: async () => {
1252
- const providers = await Provider.list()
1253
- expect(providers["brand-new-provider"]).toBeDefined()
1254
- expect(providers["brand-new-provider"].name).toBe("Brand New")
1255
- const model = providers["brand-new-provider"].models["new-model"]
1256
- expect(model.capabilities.reasoning).toBe(true)
1257
- expect(model.capabilities.attachment).toBe(true)
1258
- expect(model.capabilities.input.image).toBe(true)
1259
- },
1260
- })
1261
- })
1262
-
1263
- test("disabled_providers and enabled_providers interaction", async () => {
1264
- await using tmp = await tmpdir({
1265
- init: async (dir) => {
1266
- await Bun.write(
1267
- path.join(dir, "opencode.json"),
1268
- JSON.stringify({
1269
- $schema: "https://opencode.ai/config.json",
1270
- // enabled_providers takes precedence - only these are considered
1271
- enabled_providers: ["anthropic", "openai"],
1272
- // Then disabled_providers filters from the enabled set
1273
- disabled_providers: ["openai"],
1274
- }),
1275
- )
1276
- },
1277
- })
1278
- await Instance.provide({
1279
- directory: tmp.path,
1280
- init: async () => {
1281
- Env.set("ANTHROPIC_API_KEY", "test-anthropic")
1282
- Env.set("OPENAI_API_KEY", "test-openai")
1283
- Env.set("GOOGLE_GENERATIVE_AI_API_KEY", "test-google")
1284
- },
1285
- fn: async () => {
1286
- const providers = await Provider.list()
1287
- // anthropic: in enabled, not in disabled = allowed
1288
- expect(providers["anthropic"]).toBeDefined()
1289
- // openai: in enabled, but also in disabled = NOT allowed
1290
- expect(providers["openai"]).toBeUndefined()
1291
- // google: not in enabled = NOT allowed (even though not disabled)
1292
- expect(providers["google"]).toBeUndefined()
1293
- },
1294
- })
1295
- })
1296
-
1297
- test("model with tool_call false", async () => {
1298
- await using tmp = await tmpdir({
1299
- init: async (dir) => {
1300
- await Bun.write(
1301
- path.join(dir, "opencode.json"),
1302
- JSON.stringify({
1303
- $schema: "https://opencode.ai/config.json",
1304
- provider: {
1305
- "no-tools": {
1306
- name: "No Tools Provider",
1307
- npm: "@ai-sdk/openai-compatible",
1308
- env: [],
1309
- models: {
1310
- "basic-model": {
1311
- name: "Basic Model",
1312
- tool_call: false,
1313
- limit: { context: 4000, output: 1000 },
1314
- },
1315
- },
1316
- options: { apiKey: "test" },
1317
- },
1318
- },
1319
- }),
1320
- )
1321
- },
1322
- })
1323
- await Instance.provide({
1324
- directory: tmp.path,
1325
- fn: async () => {
1326
- const providers = await Provider.list()
1327
- expect(providers["no-tools"].models["basic-model"].capabilities.toolcall).toBe(false)
1328
- },
1329
- })
1330
- })
1331
-
1332
- test("model defaults tool_call to true when not specified", async () => {
1333
- await using tmp = await tmpdir({
1334
- init: async (dir) => {
1335
- await Bun.write(
1336
- path.join(dir, "opencode.json"),
1337
- JSON.stringify({
1338
- $schema: "https://opencode.ai/config.json",
1339
- provider: {
1340
- "default-tools": {
1341
- name: "Default Tools Provider",
1342
- npm: "@ai-sdk/openai-compatible",
1343
- env: [],
1344
- models: {
1345
- model: {
1346
- name: "Model",
1347
- // tool_call not specified
1348
- limit: { context: 4000, output: 1000 },
1349
- },
1350
- },
1351
- options: { apiKey: "test" },
1352
- },
1353
- },
1354
- }),
1355
- )
1356
- },
1357
- })
1358
- await Instance.provide({
1359
- directory: tmp.path,
1360
- fn: async () => {
1361
- const providers = await Provider.list()
1362
- expect(providers["default-tools"].models["model"].capabilities.toolcall).toBe(true)
1363
- },
1364
- })
1365
- })
1366
-
1367
- test("model headers are preserved", async () => {
1368
- await using tmp = await tmpdir({
1369
- init: async (dir) => {
1370
- await Bun.write(
1371
- path.join(dir, "opencode.json"),
1372
- JSON.stringify({
1373
- $schema: "https://opencode.ai/config.json",
1374
- provider: {
1375
- "headers-provider": {
1376
- name: "Headers Provider",
1377
- npm: "@ai-sdk/openai-compatible",
1378
- env: [],
1379
- models: {
1380
- model: {
1381
- name: "Model",
1382
- tool_call: true,
1383
- limit: { context: 4000, output: 1000 },
1384
- headers: {
1385
- "X-Custom-Header": "custom-value",
1386
- Authorization: "Bearer special-token",
1387
- },
1388
- },
1389
- },
1390
- options: { apiKey: "test" },
1391
- },
1392
- },
1393
- }),
1394
- )
1395
- },
1396
- })
1397
- await Instance.provide({
1398
- directory: tmp.path,
1399
- fn: async () => {
1400
- const providers = await Provider.list()
1401
- const model = providers["headers-provider"].models["model"]
1402
- expect(model.headers).toEqual({
1403
- "X-Custom-Header": "custom-value",
1404
- Authorization: "Bearer special-token",
1405
- })
1406
- },
1407
- })
1408
- })
1409
-
1410
- test("provider env fallback - second env var used if first missing", async () => {
1411
- await using tmp = await tmpdir({
1412
- init: async (dir) => {
1413
- await Bun.write(
1414
- path.join(dir, "opencode.json"),
1415
- JSON.stringify({
1416
- $schema: "https://opencode.ai/config.json",
1417
- provider: {
1418
- "fallback-env": {
1419
- name: "Fallback Env Provider",
1420
- npm: "@ai-sdk/openai-compatible",
1421
- env: ["PRIMARY_KEY", "FALLBACK_KEY"],
1422
- models: {
1423
- model: {
1424
- name: "Model",
1425
- tool_call: true,
1426
- limit: { context: 4000, output: 1000 },
1427
- },
1428
- },
1429
- options: { baseURL: "https://api.example.com" },
1430
- },
1431
- },
1432
- }),
1433
- )
1434
- },
1435
- })
1436
- await Instance.provide({
1437
- directory: tmp.path,
1438
- init: async () => {
1439
- // Only set fallback, not primary
1440
- Env.set("FALLBACK_KEY", "fallback-api-key")
1441
- },
1442
- fn: async () => {
1443
- const providers = await Provider.list()
1444
- // Provider should load because fallback env var is set
1445
- expect(providers["fallback-env"]).toBeDefined()
1446
- },
1447
- })
1448
- })
1449
-
1450
- test("getModel returns consistent results", async () => {
1451
- await using tmp = await tmpdir({
1452
- init: async (dir) => {
1453
- await Bun.write(
1454
- path.join(dir, "opencode.json"),
1455
- JSON.stringify({
1456
- $schema: "https://opencode.ai/config.json",
1457
- }),
1458
- )
1459
- },
1460
- })
1461
- await Instance.provide({
1462
- directory: tmp.path,
1463
- init: async () => {
1464
- Env.set("ANTHROPIC_API_KEY", "test-api-key")
1465
- },
1466
- fn: async () => {
1467
- const model1 = await Provider.getModel("anthropic", "claude-sonnet-4-20250514")
1468
- const model2 = await Provider.getModel("anthropic", "claude-sonnet-4-20250514")
1469
- expect(model1.providerID).toEqual(model2.providerID)
1470
- expect(model1.id).toEqual(model2.id)
1471
- expect(model1).toEqual(model2)
1472
- },
1473
- })
1474
- })
1475
-
1476
- test("provider name defaults to id when not in database", async () => {
1477
- await using tmp = await tmpdir({
1478
- init: async (dir) => {
1479
- await Bun.write(
1480
- path.join(dir, "opencode.json"),
1481
- JSON.stringify({
1482
- $schema: "https://opencode.ai/config.json",
1483
- provider: {
1484
- "my-custom-id": {
1485
- // no name specified
1486
- npm: "@ai-sdk/openai-compatible",
1487
- env: [],
1488
- models: {
1489
- model: {
1490
- name: "Model",
1491
- tool_call: true,
1492
- limit: { context: 4000, output: 1000 },
1493
- },
1494
- },
1495
- options: { apiKey: "test" },
1496
- },
1497
- },
1498
- }),
1499
- )
1500
- },
1501
- })
1502
- await Instance.provide({
1503
- directory: tmp.path,
1504
- fn: async () => {
1505
- const providers = await Provider.list()
1506
- expect(providers["my-custom-id"].name).toBe("my-custom-id")
1507
- },
1508
- })
1509
- })
1510
-
1511
- test("ModelNotFoundError includes suggestions for typos", async () => {
1512
- await using tmp = await tmpdir({
1513
- init: async (dir) => {
1514
- await Bun.write(
1515
- path.join(dir, "opencode.json"),
1516
- JSON.stringify({
1517
- $schema: "https://opencode.ai/config.json",
1518
- }),
1519
- )
1520
- },
1521
- })
1522
- await Instance.provide({
1523
- directory: tmp.path,
1524
- init: async () => {
1525
- Env.set("ANTHROPIC_API_KEY", "test-api-key")
1526
- },
1527
- fn: async () => {
1528
- try {
1529
- await Provider.getModel("anthropic", "claude-sonet-4") // typo: sonet instead of sonnet
1530
- expect(true).toBe(false) // Should not reach here
1531
- } catch (e: any) {
1532
- expect(e.data.suggestions).toBeDefined()
1533
- expect(e.data.suggestions.length).toBeGreaterThan(0)
1534
- }
1535
- },
1536
- })
1537
- })
1538
-
1539
- test("ModelNotFoundError for provider includes suggestions", async () => {
1540
- await using tmp = await tmpdir({
1541
- init: async (dir) => {
1542
- await Bun.write(
1543
- path.join(dir, "opencode.json"),
1544
- JSON.stringify({
1545
- $schema: "https://opencode.ai/config.json",
1546
- }),
1547
- )
1548
- },
1549
- })
1550
- await Instance.provide({
1551
- directory: tmp.path,
1552
- init: async () => {
1553
- Env.set("ANTHROPIC_API_KEY", "test-api-key")
1554
- },
1555
- fn: async () => {
1556
- try {
1557
- await Provider.getModel("antropic", "claude-sonnet-4") // typo: antropic
1558
- expect(true).toBe(false) // Should not reach here
1559
- } catch (e: any) {
1560
- expect(e.data.suggestions).toBeDefined()
1561
- expect(e.data.suggestions).toContain("anthropic")
1562
- }
1563
- },
1564
- })
1565
- })
1566
-
1567
- test("getProvider returns undefined for nonexistent provider", async () => {
1568
- await using tmp = await tmpdir({
1569
- init: async (dir) => {
1570
- await Bun.write(
1571
- path.join(dir, "opencode.json"),
1572
- JSON.stringify({
1573
- $schema: "https://opencode.ai/config.json",
1574
- }),
1575
- )
1576
- },
1577
- })
1578
- await Instance.provide({
1579
- directory: tmp.path,
1580
- fn: async () => {
1581
- const provider = await Provider.getProvider("nonexistent")
1582
- expect(provider).toBeUndefined()
1583
- },
1584
- })
1585
- })
1586
-
1587
- test("getProvider returns provider info", async () => {
1588
- await using tmp = await tmpdir({
1589
- init: async (dir) => {
1590
- await Bun.write(
1591
- path.join(dir, "opencode.json"),
1592
- JSON.stringify({
1593
- $schema: "https://opencode.ai/config.json",
1594
- }),
1595
- )
1596
- },
1597
- })
1598
- await Instance.provide({
1599
- directory: tmp.path,
1600
- init: async () => {
1601
- Env.set("ANTHROPIC_API_KEY", "test-api-key")
1602
- },
1603
- fn: async () => {
1604
- const provider = await Provider.getProvider("anthropic")
1605
- expect(provider).toBeDefined()
1606
- expect(provider?.id).toBe("anthropic")
1607
- },
1608
- })
1609
- })
1610
-
1611
- test("closest returns undefined when no partial match found", async () => {
1612
- await using tmp = await tmpdir({
1613
- init: async (dir) => {
1614
- await Bun.write(
1615
- path.join(dir, "opencode.json"),
1616
- JSON.stringify({
1617
- $schema: "https://opencode.ai/config.json",
1618
- }),
1619
- )
1620
- },
1621
- })
1622
- await Instance.provide({
1623
- directory: tmp.path,
1624
- init: async () => {
1625
- Env.set("ANTHROPIC_API_KEY", "test-api-key")
1626
- },
1627
- fn: async () => {
1628
- const result = await Provider.closest("anthropic", ["nonexistent-xyz-model"])
1629
- expect(result).toBeUndefined()
1630
- },
1631
- })
1632
- })
1633
-
1634
- test("closest checks multiple query terms in order", async () => {
1635
- await using tmp = await tmpdir({
1636
- init: async (dir) => {
1637
- await Bun.write(
1638
- path.join(dir, "opencode.json"),
1639
- JSON.stringify({
1640
- $schema: "https://opencode.ai/config.json",
1641
- }),
1642
- )
1643
- },
1644
- })
1645
- await Instance.provide({
1646
- directory: tmp.path,
1647
- init: async () => {
1648
- Env.set("ANTHROPIC_API_KEY", "test-api-key")
1649
- },
1650
- fn: async () => {
1651
- // First term won't match, second will
1652
- const result = await Provider.closest("anthropic", ["nonexistent", "haiku"])
1653
- expect(result).toBeDefined()
1654
- expect(result?.modelID).toContain("haiku")
1655
- },
1656
- })
1657
- })
1658
-
1659
- test("model limit defaults to zero when not specified", async () => {
1660
- await using tmp = await tmpdir({
1661
- init: async (dir) => {
1662
- await Bun.write(
1663
- path.join(dir, "opencode.json"),
1664
- JSON.stringify({
1665
- $schema: "https://opencode.ai/config.json",
1666
- provider: {
1667
- "no-limit": {
1668
- name: "No Limit Provider",
1669
- npm: "@ai-sdk/openai-compatible",
1670
- env: [],
1671
- models: {
1672
- model: {
1673
- name: "Model",
1674
- tool_call: true,
1675
- // no limit specified
1676
- },
1677
- },
1678
- options: { apiKey: "test" },
1679
- },
1680
- },
1681
- }),
1682
- )
1683
- },
1684
- })
1685
- await Instance.provide({
1686
- directory: tmp.path,
1687
- fn: async () => {
1688
- const providers = await Provider.list()
1689
- const model = providers["no-limit"].models["model"]
1690
- expect(model.limit.context).toBe(0)
1691
- expect(model.limit.output).toBe(0)
1692
- },
1693
- })
1694
- })
1695
-
1696
- test("provider options are deeply merged", async () => {
1697
- await using tmp = await tmpdir({
1698
- init: async (dir) => {
1699
- await Bun.write(
1700
- path.join(dir, "opencode.json"),
1701
- JSON.stringify({
1702
- $schema: "https://opencode.ai/config.json",
1703
- provider: {
1704
- anthropic: {
1705
- options: {
1706
- headers: {
1707
- "X-Custom": "custom-value",
1708
- },
1709
- timeout: 30000,
1710
- },
1711
- },
1712
- },
1713
- }),
1714
- )
1715
- },
1716
- })
1717
- await Instance.provide({
1718
- directory: tmp.path,
1719
- init: async () => {
1720
- Env.set("ANTHROPIC_API_KEY", "test-api-key")
1721
- },
1722
- fn: async () => {
1723
- const providers = await Provider.list()
1724
- // Custom options should be merged
1725
- expect(providers["anthropic"].options.timeout).toBe(30000)
1726
- expect(providers["anthropic"].options.headers["X-Custom"]).toBe("custom-value")
1727
- // anthropic custom loader adds its own headers, they should coexist
1728
- expect(providers["anthropic"].options.headers["anthropic-beta"]).toBeDefined()
1729
- },
1730
- })
1731
- })
1732
-
1733
- test("custom model inherits npm package from models.dev provider config", async () => {
1734
- await using tmp = await tmpdir({
1735
- init: async (dir) => {
1736
- await Bun.write(
1737
- path.join(dir, "opencode.json"),
1738
- JSON.stringify({
1739
- $schema: "https://opencode.ai/config.json",
1740
- provider: {
1741
- openai: {
1742
- models: {
1743
- "my-custom-model": {
1744
- name: "My Custom Model",
1745
- tool_call: true,
1746
- limit: { context: 8000, output: 2000 },
1747
- },
1748
- },
1749
- },
1750
- },
1751
- }),
1752
- )
1753
- },
1754
- })
1755
- await Instance.provide({
1756
- directory: tmp.path,
1757
- init: async () => {
1758
- Env.set("OPENAI_API_KEY", "test-api-key")
1759
- },
1760
- fn: async () => {
1761
- const providers = await Provider.list()
1762
- const model = providers["openai"].models["my-custom-model"]
1763
- expect(model).toBeDefined()
1764
- expect(model.api.npm).toBe("@ai-sdk/openai")
1765
- },
1766
- })
1767
- })
1768
-
1769
- test("custom model inherits api.url from models.dev provider", async () => {
1770
- await using tmp = await tmpdir({
1771
- init: async (dir) => {
1772
- await Bun.write(
1773
- path.join(dir, "opencode.json"),
1774
- JSON.stringify({
1775
- $schema: "https://opencode.ai/config.json",
1776
- provider: {
1777
- openrouter: {
1778
- models: {
1779
- "prime-intellect/intellect-3": {},
1780
- "deepseek/deepseek-r1-0528": {
1781
- name: "DeepSeek R1",
1782
- },
1783
- },
1784
- },
1785
- },
1786
- }),
1787
- )
1788
- },
1789
- })
1790
- await Instance.provide({
1791
- directory: tmp.path,
1792
- init: async () => {
1793
- Env.set("OPENROUTER_API_KEY", "test-api-key")
1794
- },
1795
- fn: async () => {
1796
- const providers = await Provider.list()
1797
- expect(providers["openrouter"]).toBeDefined()
1798
-
1799
- // New model not in database should inherit api.url from provider
1800
- const intellect = providers["openrouter"].models["prime-intellect/intellect-3"]
1801
- expect(intellect).toBeDefined()
1802
- expect(intellect.api.url).toBe("https://openrouter.ai/api/v1")
1803
-
1804
- // Another new model should also inherit api.url
1805
- const deepseek = providers["openrouter"].models["deepseek/deepseek-r1-0528"]
1806
- expect(deepseek).toBeDefined()
1807
- expect(deepseek.api.url).toBe("https://openrouter.ai/api/v1")
1808
- expect(deepseek.name).toBe("DeepSeek R1")
1809
- },
1810
- })
1811
- })
1812
-
1813
- test("model variants are generated for reasoning models", async () => {
1814
- await using tmp = await tmpdir({
1815
- init: async (dir) => {
1816
- await Bun.write(
1817
- path.join(dir, "opencode.json"),
1818
- JSON.stringify({
1819
- $schema: "https://opencode.ai/config.json",
1820
- }),
1821
- )
1822
- },
1823
- })
1824
- await Instance.provide({
1825
- directory: tmp.path,
1826
- init: async () => {
1827
- Env.set("ANTHROPIC_API_KEY", "test-api-key")
1828
- },
1829
- fn: async () => {
1830
- const providers = await Provider.list()
1831
- // Claude sonnet 4 has reasoning capability
1832
- const model = providers["anthropic"].models["claude-sonnet-4-20250514"]
1833
- expect(model.capabilities.reasoning).toBe(true)
1834
- expect(model.variants).toBeDefined()
1835
- expect(Object.keys(model.variants!).length).toBeGreaterThan(0)
1836
- },
1837
- })
1838
- })
1839
-
1840
- test("model variants can be disabled via config", async () => {
1841
- await using tmp = await tmpdir({
1842
- init: async (dir) => {
1843
- await Bun.write(
1844
- path.join(dir, "opencode.json"),
1845
- JSON.stringify({
1846
- $schema: "https://opencode.ai/config.json",
1847
- provider: {
1848
- anthropic: {
1849
- models: {
1850
- "claude-sonnet-4-20250514": {
1851
- variants: {
1852
- high: { disabled: true },
1853
- },
1854
- },
1855
- },
1856
- },
1857
- },
1858
- }),
1859
- )
1860
- },
1861
- })
1862
- await Instance.provide({
1863
- directory: tmp.path,
1864
- init: async () => {
1865
- Env.set("ANTHROPIC_API_KEY", "test-api-key")
1866
- },
1867
- fn: async () => {
1868
- const providers = await Provider.list()
1869
- const model = providers["anthropic"].models["claude-sonnet-4-20250514"]
1870
- expect(model.variants).toBeDefined()
1871
- expect(model.variants!["high"]).toBeUndefined()
1872
- // max variant should still exist
1873
- expect(model.variants!["max"]).toBeDefined()
1874
- },
1875
- })
1876
- })
1877
-
1878
- test("model variants can be customized via config", async () => {
1879
- await using tmp = await tmpdir({
1880
- init: async (dir) => {
1881
- await Bun.write(
1882
- path.join(dir, "opencode.json"),
1883
- JSON.stringify({
1884
- $schema: "https://opencode.ai/config.json",
1885
- provider: {
1886
- anthropic: {
1887
- models: {
1888
- "claude-sonnet-4-20250514": {
1889
- variants: {
1890
- high: {
1891
- thinking: {
1892
- type: "enabled",
1893
- budgetTokens: 20000,
1894
- },
1895
- },
1896
- },
1897
- },
1898
- },
1899
- },
1900
- },
1901
- }),
1902
- )
1903
- },
1904
- })
1905
- await Instance.provide({
1906
- directory: tmp.path,
1907
- init: async () => {
1908
- Env.set("ANTHROPIC_API_KEY", "test-api-key")
1909
- },
1910
- fn: async () => {
1911
- const providers = await Provider.list()
1912
- const model = providers["anthropic"].models["claude-sonnet-4-20250514"]
1913
- expect(model.variants!["high"]).toBeDefined()
1914
- expect(model.variants!["high"].thinking.budgetTokens).toBe(20000)
1915
- },
1916
- })
1917
- })
1918
-
1919
- test("disabled key is stripped from variant config", async () => {
1920
- await using tmp = await tmpdir({
1921
- init: async (dir) => {
1922
- await Bun.write(
1923
- path.join(dir, "opencode.json"),
1924
- JSON.stringify({
1925
- $schema: "https://opencode.ai/config.json",
1926
- provider: {
1927
- anthropic: {
1928
- models: {
1929
- "claude-sonnet-4-20250514": {
1930
- variants: {
1931
- max: {
1932
- disabled: false,
1933
- customField: "test",
1934
- },
1935
- },
1936
- },
1937
- },
1938
- },
1939
- },
1940
- }),
1941
- )
1942
- },
1943
- })
1944
- await Instance.provide({
1945
- directory: tmp.path,
1946
- init: async () => {
1947
- Env.set("ANTHROPIC_API_KEY", "test-api-key")
1948
- },
1949
- fn: async () => {
1950
- const providers = await Provider.list()
1951
- const model = providers["anthropic"].models["claude-sonnet-4-20250514"]
1952
- expect(model.variants!["max"]).toBeDefined()
1953
- expect(model.variants!["max"].disabled).toBeUndefined()
1954
- expect(model.variants!["max"].customField).toBe("test")
1955
- },
1956
- })
1957
- })
1958
-
1959
- test("all variants can be disabled via config", async () => {
1960
- await using tmp = await tmpdir({
1961
- init: async (dir) => {
1962
- await Bun.write(
1963
- path.join(dir, "opencode.json"),
1964
- JSON.stringify({
1965
- $schema: "https://opencode.ai/config.json",
1966
- provider: {
1967
- anthropic: {
1968
- models: {
1969
- "claude-sonnet-4-20250514": {
1970
- variants: {
1971
- high: { disabled: true },
1972
- max: { disabled: true },
1973
- },
1974
- },
1975
- },
1976
- },
1977
- },
1978
- }),
1979
- )
1980
- },
1981
- })
1982
- await Instance.provide({
1983
- directory: tmp.path,
1984
- init: async () => {
1985
- Env.set("ANTHROPIC_API_KEY", "test-api-key")
1986
- },
1987
- fn: async () => {
1988
- const providers = await Provider.list()
1989
- const model = providers["anthropic"].models["claude-sonnet-4-20250514"]
1990
- expect(model.variants).toBeDefined()
1991
- expect(Object.keys(model.variants!).length).toBe(0)
1992
- },
1993
- })
1994
- })
1995
-
1996
- test("variant config merges with generated variants", async () => {
1997
- await using tmp = await tmpdir({
1998
- init: async (dir) => {
1999
- await Bun.write(
2000
- path.join(dir, "opencode.json"),
2001
- JSON.stringify({
2002
- $schema: "https://opencode.ai/config.json",
2003
- provider: {
2004
- anthropic: {
2005
- models: {
2006
- "claude-sonnet-4-20250514": {
2007
- variants: {
2008
- high: {
2009
- extraOption: "custom-value",
2010
- },
2011
- },
2012
- },
2013
- },
2014
- },
2015
- },
2016
- }),
2017
- )
2018
- },
2019
- })
2020
- await Instance.provide({
2021
- directory: tmp.path,
2022
- init: async () => {
2023
- Env.set("ANTHROPIC_API_KEY", "test-api-key")
2024
- },
2025
- fn: async () => {
2026
- const providers = await Provider.list()
2027
- const model = providers["anthropic"].models["claude-sonnet-4-20250514"]
2028
- expect(model.variants!["high"]).toBeDefined()
2029
- // Should have both the generated thinking config and the custom option
2030
- expect(model.variants!["high"].thinking).toBeDefined()
2031
- expect(model.variants!["high"].extraOption).toBe("custom-value")
2032
- },
2033
- })
2034
- })
2035
-
2036
- test("variants filtered in second pass for database models", async () => {
2037
- await using tmp = await tmpdir({
2038
- init: async (dir) => {
2039
- await Bun.write(
2040
- path.join(dir, "opencode.json"),
2041
- JSON.stringify({
2042
- $schema: "https://opencode.ai/config.json",
2043
- provider: {
2044
- openai: {
2045
- models: {
2046
- "gpt-5": {
2047
- variants: {
2048
- high: { disabled: true },
2049
- },
2050
- },
2051
- },
2052
- },
2053
- },
2054
- }),
2055
- )
2056
- },
2057
- })
2058
- await Instance.provide({
2059
- directory: tmp.path,
2060
- init: async () => {
2061
- Env.set("OPENAI_API_KEY", "test-api-key")
2062
- },
2063
- fn: async () => {
2064
- const providers = await Provider.list()
2065
- const model = providers["openai"].models["gpt-5"]
2066
- expect(model.variants).toBeDefined()
2067
- expect(model.variants!["high"]).toBeUndefined()
2068
- // Other variants should still exist
2069
- expect(model.variants!["medium"]).toBeDefined()
2070
- },
2071
- })
2072
- })
2073
-
2074
- test("custom model with variants enabled and disabled", async () => {
2075
- await using tmp = await tmpdir({
2076
- init: async (dir) => {
2077
- await Bun.write(
2078
- path.join(dir, "opencode.json"),
2079
- JSON.stringify({
2080
- $schema: "https://opencode.ai/config.json",
2081
- provider: {
2082
- "custom-reasoning": {
2083
- name: "Custom Reasoning Provider",
2084
- npm: "@ai-sdk/openai-compatible",
2085
- env: [],
2086
- models: {
2087
- "reasoning-model": {
2088
- name: "Reasoning Model",
2089
- tool_call: true,
2090
- reasoning: true,
2091
- limit: { context: 128000, output: 16000 },
2092
- variants: {
2093
- low: { reasoningEffort: "low" },
2094
- medium: { reasoningEffort: "medium" },
2095
- high: { reasoningEffort: "high", disabled: true },
2096
- custom: { reasoningEffort: "custom", budgetTokens: 5000 },
2097
- },
2098
- },
2099
- },
2100
- options: { apiKey: "test-key" },
2101
- },
2102
- },
2103
- }),
2104
- )
2105
- },
2106
- })
2107
- await Instance.provide({
2108
- directory: tmp.path,
2109
- fn: async () => {
2110
- const providers = await Provider.list()
2111
- const model = providers["custom-reasoning"].models["reasoning-model"]
2112
- expect(model.variants).toBeDefined()
2113
- // Enabled variants should exist
2114
- expect(model.variants!["low"]).toBeDefined()
2115
- expect(model.variants!["low"].reasoningEffort).toBe("low")
2116
- expect(model.variants!["medium"]).toBeDefined()
2117
- expect(model.variants!["medium"].reasoningEffort).toBe("medium")
2118
- expect(model.variants!["custom"]).toBeDefined()
2119
- expect(model.variants!["custom"].reasoningEffort).toBe("custom")
2120
- expect(model.variants!["custom"].budgetTokens).toBe(5000)
2121
- // Disabled variant should not exist
2122
- expect(model.variants!["high"]).toBeUndefined()
2123
- // disabled key should be stripped from all variants
2124
- expect(model.variants!["low"].disabled).toBeUndefined()
2125
- expect(model.variants!["medium"].disabled).toBeUndefined()
2126
- expect(model.variants!["custom"].disabled).toBeUndefined()
2127
- },
2128
- })
2129
- })
2130
-
2131
- test("Google Vertex: retains baseURL for custom proxy", async () => {
2132
- await using tmp = await tmpdir({
2133
- init: async (dir) => {
2134
- await Bun.write(
2135
- path.join(dir, "opencode.json"),
2136
- JSON.stringify({
2137
- $schema: "https://opencode.ai/config.json",
2138
- provider: {
2139
- "vertex-proxy": {
2140
- name: "Vertex Proxy",
2141
- npm: "@ai-sdk/google-vertex",
2142
- api: "https://my-proxy.com/v1",
2143
- env: ["GOOGLE_APPLICATION_CREDENTIALS"], // Mock env var requirement
2144
- models: {
2145
- "gemini-pro": {
2146
- name: "Gemini Pro",
2147
- tool_call: true,
2148
- },
2149
- },
2150
- options: {
2151
- project: "test-project",
2152
- location: "us-central1",
2153
- baseURL: "https://my-proxy.com/v1", // Should be retained
2154
- },
2155
- },
2156
- },
2157
- }),
2158
- )
2159
- },
2160
- })
2161
-
2162
- await Instance.provide({
2163
- directory: tmp.path,
2164
- init: async () => {
2165
- Env.set("GOOGLE_APPLICATION_CREDENTIALS", "test-creds")
2166
- },
2167
- fn: async () => {
2168
- const providers = await Provider.list()
2169
- expect(providers["vertex-proxy"]).toBeDefined()
2170
- expect(providers["vertex-proxy"].options.baseURL).toBe("https://my-proxy.com/v1")
2171
- },
2172
- })
2173
- })
2174
-
2175
- test("Google Vertex: supports OpenAI compatible models", async () => {
2176
- await using tmp = await tmpdir({
2177
- init: async (dir) => {
2178
- await Bun.write(
2179
- path.join(dir, "opencode.json"),
2180
- JSON.stringify({
2181
- $schema: "https://opencode.ai/config.json",
2182
- provider: {
2183
- "vertex-openai": {
2184
- name: "Vertex OpenAI",
2185
- npm: "@ai-sdk/google-vertex",
2186
- env: ["GOOGLE_APPLICATION_CREDENTIALS"],
2187
- models: {
2188
- "gpt-4": {
2189
- name: "GPT-4",
2190
- provider: {
2191
- npm: "@ai-sdk/openai-compatible",
2192
- api: "https://api.openai.com/v1",
2193
- },
2194
- },
2195
- },
2196
- options: {
2197
- project: "test-project",
2198
- location: "us-central1",
2199
- },
2200
- },
2201
- },
2202
- }),
2203
- )
2204
- },
2205
- })
2206
-
2207
- await Instance.provide({
2208
- directory: tmp.path,
2209
- init: async () => {
2210
- Env.set("GOOGLE_APPLICATION_CREDENTIALS", "test-creds")
2211
- },
2212
- fn: async () => {
2213
- const providers = await Provider.list()
2214
- const model = providers["vertex-openai"].models["gpt-4"]
2215
-
2216
- expect(model).toBeDefined()
2217
- expect(model.api.npm).toBe("@ai-sdk/openai-compatible")
2218
- },
2219
- })
2220
- })