provider-opencode.test.ts 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  1. import { describe, expect } from "bun:test"
  2. import { Effect } from "effect"
  3. import { Catalog } from "@opencode-ai/core/catalog"
  4. import { Credential } from "@opencode-ai/core/credential"
  5. import { EventV2 } from "@opencode-ai/core/event"
  6. import { Integration } from "@opencode-ai/core/integration"
  7. import { ModelV2 } from "@opencode-ai/core/model"
  8. import { PluginV2 } from "@opencode-ai/core/plugin"
  9. import { PluginHost } from "@opencode-ai/core/plugin/host"
  10. import { OpencodePlugin } from "@opencode-ai/core/plugin/provider/opencode"
  11. import { ProviderV2 } from "@opencode-ai/core/provider"
  12. import { testEffect } from "../lib/effect"
  13. import { PluginTestLayer } from "./fixture"
  14. const it = testEffect(PluginTestLayer)
  15. const addPlugin = Effect.fn(function* () {
  16. const plugin = yield* PluginV2.Service
  17. const host = yield* PluginHost.make(plugin)
  18. const events = yield* EventV2.Service
  19. const integration = yield* Integration.Service
  20. yield* OpencodePlugin.effect(host).pipe(
  21. Effect.provideService(EventV2.Service, events),
  22. Effect.provideService(Integration.Service, integration),
  23. )
  24. })
  25. function required<T>(value: T | undefined): T {
  26. if (value === undefined) throw new Error("Expected value")
  27. return value
  28. }
  29. function eventually<A>(
  30. effect: Effect.Effect<A>,
  31. predicate: (value: A) => boolean,
  32. remaining = 1000,
  33. ): Effect.Effect<A, Error> {
  34. return Effect.gen(function* () {
  35. const value = yield* effect
  36. if (predicate(value)) return value
  37. if (remaining === 0) return yield* Effect.fail(new Error("Timed out waiting for value"))
  38. yield* Effect.promise(() => Bun.sleep(1))
  39. return yield* eventually(effect, predicate, remaining - 1)
  40. })
  41. }
  42. function withEnv<A, E, R>(vars: Record<string, string | undefined>, effect: () => Effect.Effect<A, E, R>) {
  43. return Effect.acquireUseRelease(
  44. Effect.sync(() => {
  45. const previous = Object.fromEntries(Object.keys(vars).map((key) => [key, process.env[key]]))
  46. Object.entries(vars).forEach(([key, value]) => {
  47. if (value === undefined) delete process.env[key]
  48. else process.env[key] = value
  49. })
  50. return previous
  51. }),
  52. effect,
  53. (previous) =>
  54. Effect.sync(() =>
  55. Object.entries(previous).forEach(([key, value]) => {
  56. if (value === undefined) delete process.env[key]
  57. else process.env[key] = value
  58. }),
  59. ),
  60. )
  61. }
  62. const cost = (input: number, output = 0) => [{ input, output, cache: { read: 0, write: 0 } }]
  63. describe("OpencodePlugin", () => {
  64. it.effect("registers account and service account methods", () =>
  65. Effect.gen(function* () {
  66. yield* addPlugin()
  67. expect((yield* (yield* Integration.Service).get(Integration.ID.make("opencode")))?.methods).toEqual([
  68. {
  69. id: Integration.MethodID.make("device"),
  70. type: "oauth",
  71. label: "OpenCode Console account",
  72. },
  73. { type: "key", label: "API key (service account)" },
  74. ])
  75. }),
  76. )
  77. it.live("loads providers and models from the connected OpenCode server", () =>
  78. Effect.acquireUseRelease(
  79. Effect.sync(() => {
  80. const authorization: Array<string | null> = []
  81. const gate = Promise.withResolvers<void>()
  82. return {
  83. authorization,
  84. release: gate.resolve,
  85. server: Bun.serve({
  86. port: 0,
  87. fetch: async (request) => {
  88. await gate.promise
  89. authorization.push(request.headers.get("authorization"))
  90. const origin = new URL(request.url).origin
  91. return Response.json({
  92. config: {
  93. enterprise: { url: origin },
  94. provider: {
  95. remote: {
  96. name: "Remote",
  97. npm: "@ai-sdk/openai-compatible",
  98. api: `${origin}/v1`,
  99. env: ["REMOTE_API_KEY"],
  100. options: {
  101. apiKey: "{env:REMOTE_API_KEY}",
  102. headers: { "x-org-id": "org" },
  103. custom: "value",
  104. },
  105. models: {
  106. model: {
  107. name: "Remote Model",
  108. family: "remote",
  109. release_date: "2026-01-02",
  110. tool_call: true,
  111. modalities: { input: ["text", "image"], output: ["text"] },
  112. options: { apiKey: "model-secret", temperature: 0.5 },
  113. variants: { high: { apiKey: "variant-secret", temperature: 0.2 } },
  114. cost: { input: 1, output: 2, cache_read: 0.1 },
  115. limit: { context: 1000, output: 100 },
  116. },
  117. disabled: { name: "Disabled", status: "deprecated" },
  118. },
  119. },
  120. },
  121. },
  122. })
  123. },
  124. }),
  125. }
  126. }),
  127. ({ authorization, release, server }) =>
  128. Effect.gen(function* () {
  129. const credentials = yield* Credential.Service
  130. const catalog = yield* Catalog.Service
  131. yield* catalog.transform((draft) => {
  132. draft.provider.update(ProviderV2.ID.make("remote"), () => {})
  133. draft.model.update(ProviderV2.ID.make("remote"), ModelV2.ID.make("model"), (model) => {
  134. model.variants = [
  135. {
  136. id: ModelV2.VariantID.make("custom"),
  137. settings: {},
  138. headers: { "x-custom": "true" },
  139. body: { custom: true },
  140. },
  141. ]
  142. })
  143. draft.model.update(ProviderV2.ID.make("remote"), ModelV2.ID.make("stale"), () => {})
  144. })
  145. yield* credentials.create({
  146. integrationID: Integration.ID.make("opencode"),
  147. value: Credential.Key.make({
  148. type: "key",
  149. key: "secret",
  150. metadata: { server: server.url.origin },
  151. }),
  152. })
  153. yield* addPlugin()
  154. expect(authorization).toEqual([])
  155. release()
  156. const provider = required(
  157. yield* eventually(
  158. catalog.provider.get(ProviderV2.ID.make("remote")),
  159. (item) => item?.integrationID === Integration.ID.make("opencode"),
  160. ),
  161. )
  162. expect(provider).toMatchObject({
  163. name: "Remote",
  164. integrationID: "opencode",
  165. api: {
  166. type: "aisdk",
  167. package: "@ai-sdk/openai-compatible",
  168. url: `${server.url.origin}/v1`,
  169. },
  170. })
  171. expect(provider.request).toEqual({ settings: {}, headers: { "x-org-id": "org" }, body: { custom: "value" } })
  172. expect(yield* (yield* Integration.Service).get(Integration.ID.make("remote"))).toBeUndefined()
  173. const model = required(yield* catalog.model.get(ProviderV2.ID.make("remote"), ModelV2.ID.make("model")))
  174. expect(model).toMatchObject({
  175. name: "Remote Model",
  176. family: "remote",
  177. capabilities: { tools: true, input: ["text", "image"], output: ["text"] },
  178. cost: [{ input: 1, output: 2, cache: { read: 0.1, write: 0 } }],
  179. limit: { context: 1000, output: 100 },
  180. })
  181. expect(model.request.body).toEqual({ custom: "value", temperature: 0.5 })
  182. expect(model.variants).toEqual([
  183. {
  184. id: ModelV2.VariantID.make("custom"),
  185. settings: {},
  186. headers: { "x-custom": "true" },
  187. body: { custom: true },
  188. },
  189. {
  190. id: ModelV2.VariantID.make("high"),
  191. settings: {},
  192. headers: {},
  193. body: { temperature: 0.2 },
  194. },
  195. ])
  196. expect(
  197. required(yield* catalog.model.get(ProviderV2.ID.make("remote"), ModelV2.ID.make("disabled"))).enabled,
  198. ).toBe(false)
  199. expect(yield* catalog.model.get(ProviderV2.ID.make("remote"), ModelV2.ID.make("stale"))).toBeDefined()
  200. expect(authorization).toContain("Bearer secret")
  201. }),
  202. ({ server }) => Effect.promise(() => server.stop(true)),
  203. ),
  204. )
  205. it.effect("uses a public key and disables paid models without credentials", () =>
  206. withEnv({ OPENCODE_API_KEY: undefined }, () =>
  207. Effect.gen(function* () {
  208. const catalog = yield* Catalog.Service
  209. yield* catalog.transform((catalog) => {
  210. const provider = ProviderV2.Info.make({
  211. ...ProviderV2.Info.empty(ProviderV2.ID.opencode),
  212. api: { type: "aisdk", package: "test-provider" },
  213. })
  214. const model = ModelV2.Info.make({
  215. ...ModelV2.Info.empty(provider.id, ModelV2.ID.make("paid")),
  216. api: { id: ModelV2.ID.make("paid"), type: "aisdk", package: "test-provider" },
  217. cost: cost(1),
  218. })
  219. catalog.provider.update(provider.id, () => {})
  220. catalog.model.update(provider.id, model.id, (draft) => {
  221. draft.cost = [...model.cost]
  222. })
  223. })
  224. yield* addPlugin()
  225. expect(required(yield* catalog.provider.get(ProviderV2.ID.opencode)).request.body.apiKey).toBe("public")
  226. expect(required(yield* catalog.model.get(ProviderV2.ID.opencode, ModelV2.ID.make("paid"))).enabled).toBe(false)
  227. }),
  228. ),
  229. )
  230. it.effect("keeps free models without credentials", () =>
  231. withEnv({ OPENCODE_API_KEY: undefined }, () =>
  232. Effect.gen(function* () {
  233. const catalog = yield* Catalog.Service
  234. yield* catalog.transform((catalog) => {
  235. const provider = ProviderV2.Info.make({
  236. ...ProviderV2.Info.empty(ProviderV2.ID.opencode),
  237. api: { type: "aisdk", package: "test-provider" },
  238. })
  239. const model = ModelV2.Info.make({
  240. ...ModelV2.Info.empty(provider.id, ModelV2.ID.make("free")),
  241. api: { id: ModelV2.ID.make("free"), type: "aisdk", package: "test-provider" },
  242. cost: cost(0),
  243. })
  244. catalog.provider.update(provider.id, () => {})
  245. catalog.model.update(provider.id, model.id, (draft) => {
  246. draft.cost = [...model.cost]
  247. })
  248. })
  249. yield* addPlugin()
  250. expect(required(yield* catalog.provider.get(ProviderV2.ID.opencode)).request.body.apiKey).toBe("public")
  251. expect(required(yield* catalog.model.get(ProviderV2.ID.opencode, ModelV2.ID.make("free"))).enabled).toBe(true)
  252. }),
  253. ),
  254. )
  255. it.effect("treats output-only cost as free without credentials", () =>
  256. withEnv({ OPENCODE_API_KEY: undefined }, () =>
  257. Effect.gen(function* () {
  258. const catalog = yield* Catalog.Service
  259. yield* catalog.transform((catalog) => {
  260. const provider = ProviderV2.Info.make({
  261. ...ProviderV2.Info.empty(ProviderV2.ID.opencode),
  262. api: { type: "aisdk", package: "test-provider" },
  263. })
  264. const model = ModelV2.Info.make({
  265. ...ModelV2.Info.empty(provider.id, ModelV2.ID.make("output-only")),
  266. api: { id: ModelV2.ID.make("output-only"), type: "aisdk", package: "test-provider" },
  267. cost: cost(0, 1),
  268. })
  269. catalog.provider.update(provider.id, () => {})
  270. catalog.model.update(provider.id, model.id, (draft) => {
  271. draft.cost = [...model.cost]
  272. })
  273. })
  274. yield* addPlugin()
  275. expect(required(yield* catalog.provider.get(ProviderV2.ID.opencode)).request.body.apiKey).toBe("public")
  276. expect(required(yield* catalog.model.get(ProviderV2.ID.opencode, ModelV2.ID.make("output-only"))).enabled).toBe(
  277. true,
  278. )
  279. }),
  280. ),
  281. )
  282. it.effect("uses OPENCODE_API_KEY as credentials", () =>
  283. withEnv({ OPENCODE_API_KEY: "secret" }, () =>
  284. Effect.gen(function* () {
  285. const catalog = yield* Catalog.Service
  286. yield* catalog.transform((catalog) => {
  287. const provider = ProviderV2.Info.make({
  288. ...ProviderV2.Info.empty(ProviderV2.ID.opencode),
  289. api: { type: "aisdk", package: "test-provider" },
  290. })
  291. const model = ModelV2.Info.make({
  292. ...ModelV2.Info.empty(provider.id, ModelV2.ID.make("paid")),
  293. api: { id: ModelV2.ID.make("paid"), type: "aisdk", package: "test-provider" },
  294. cost: cost(1),
  295. })
  296. catalog.provider.update(provider.id, () => {})
  297. catalog.model.update(provider.id, model.id, (draft) => {
  298. draft.cost = [...model.cost]
  299. })
  300. })
  301. yield* addPlugin()
  302. expect(required(yield* catalog.provider.get(ProviderV2.ID.opencode)).request.body.apiKey).toBeUndefined()
  303. expect(required(yield* catalog.model.get(ProviderV2.ID.opencode, ModelV2.ID.make("paid"))).enabled).toBe(true)
  304. }),
  305. ),
  306. )
  307. it.effect("uses configured provider env vars as credentials", () =>
  308. withEnv({ OPENCODE_API_KEY: undefined, CUSTOM_OPENCODE_API_KEY: "secret" }, () =>
  309. Effect.gen(function* () {
  310. const catalog = yield* Catalog.Service
  311. const integrations = yield* Integration.Service
  312. yield* integrations.transform((editor) => {
  313. editor.method.update({
  314. integrationID: Integration.ID.make("opencode"),
  315. method: { type: "env", names: ["CUSTOM_OPENCODE_API_KEY"] },
  316. })
  317. })
  318. yield* catalog.transform((catalog) => {
  319. const provider = ProviderV2.Info.make({
  320. ...ProviderV2.Info.empty(ProviderV2.ID.opencode),
  321. api: { type: "aisdk", package: "test-provider" },
  322. })
  323. const model = ModelV2.Info.make({
  324. ...ModelV2.Info.empty(provider.id, ModelV2.ID.make("paid")),
  325. api: { id: ModelV2.ID.make("paid"), type: "aisdk", package: "test-provider" },
  326. cost: cost(1),
  327. })
  328. catalog.provider.update(provider.id, () => {})
  329. catalog.model.update(provider.id, model.id, (draft) => {
  330. draft.cost = [...model.cost]
  331. })
  332. })
  333. yield* addPlugin()
  334. expect(required(yield* catalog.provider.get(ProviderV2.ID.opencode)).request.body.apiKey).toBeUndefined()
  335. expect(required(yield* catalog.model.get(ProviderV2.ID.opencode, ModelV2.ID.make("paid"))).enabled).toBe(true)
  336. }),
  337. ),
  338. )
  339. it.effect("uses configured apiKey as credentials", () =>
  340. withEnv({ OPENCODE_API_KEY: undefined }, () =>
  341. Effect.gen(function* () {
  342. const catalog = yield* Catalog.Service
  343. yield* catalog.transform((catalog) => {
  344. const provider = ProviderV2.Info.make({
  345. ...ProviderV2.Info.empty(ProviderV2.ID.opencode),
  346. api: { type: "aisdk", package: "test-provider" },
  347. request: {
  348. settings: {},
  349. headers: {},
  350. body: { apiKey: "configured" },
  351. },
  352. })
  353. const model = ModelV2.Info.make({
  354. ...ModelV2.Info.empty(provider.id, ModelV2.ID.make("paid")),
  355. api: { id: ModelV2.ID.make("paid"), type: "aisdk", package: "test-provider" },
  356. cost: cost(1),
  357. })
  358. catalog.provider.update(provider.id, (draft) => {
  359. draft.request = provider.request
  360. })
  361. catalog.model.update(provider.id, model.id, (draft) => {
  362. draft.cost = [...model.cost]
  363. })
  364. })
  365. yield* addPlugin()
  366. expect(required(yield* catalog.provider.get(ProviderV2.ID.opencode)).request.body.apiKey).toBe("configured")
  367. expect(required(yield* catalog.model.get(ProviderV2.ID.opencode, ModelV2.ID.make("paid"))).enabled).toBe(true)
  368. }),
  369. ),
  370. )
  371. it.effect("ignores non-opencode providers and models", () =>
  372. withEnv({ OPENCODE_API_KEY: undefined }, () =>
  373. Effect.gen(function* () {
  374. const catalog = yield* Catalog.Service
  375. yield* catalog.transform((catalog) => {
  376. const provider = ProviderV2.Info.make({
  377. ...ProviderV2.Info.empty(ProviderV2.ID.openai),
  378. api: { type: "aisdk", package: "test-provider" },
  379. })
  380. const model = ModelV2.Info.make({
  381. ...ModelV2.Info.empty(provider.id, ModelV2.ID.make("paid")),
  382. api: { id: ModelV2.ID.make("paid"), type: "aisdk", package: "test-provider" },
  383. cost: cost(1),
  384. })
  385. catalog.provider.update(provider.id, () => {})
  386. catalog.model.update(provider.id, model.id, (draft) => {
  387. draft.cost = [...model.cost]
  388. })
  389. })
  390. yield* addPlugin()
  391. expect(required(yield* catalog.provider.get(ProviderV2.ID.openai)).request.body.apiKey).toBeUndefined()
  392. expect(required(yield* catalog.model.get(ProviderV2.ID.openai, ModelV2.ID.make("paid"))).enabled).toBe(true)
  393. }),
  394. ),
  395. )
  396. it.effect("prefers gpt-5-nano as the opencode small model", () =>
  397. Effect.gen(function* () {
  398. const catalog = yield* Catalog.Service
  399. const providerID = ProviderV2.ID.opencode
  400. yield* catalog.transform((catalog) => {
  401. catalog.provider.update(providerID, () => {})
  402. catalog.model.update(providerID, ModelV2.ID.make("cheap-mini"), (model) => {
  403. model.capabilities.input = ["text"]
  404. model.capabilities.output = ["text"]
  405. model.cost = [...cost(1, 1)]
  406. model.time.released = Date.now()
  407. })
  408. catalog.model.update(providerID, ModelV2.ID.make("gpt-5-nano"), (model) => {
  409. model.capabilities.input = ["text"]
  410. model.capabilities.output = ["text"]
  411. model.cost = [...cost(10, 10)]
  412. model.time.released = Date.now()
  413. })
  414. })
  415. const selected = yield* catalog.model.small(providerID)
  416. expect(selected?.id).toBe(ModelV2.ID.make("gpt-5-nano"))
  417. }),
  418. )
  419. })