session-runner-model.test.ts 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. import { describe, expect } from "bun:test"
  2. import { LLM } from "@opencode-ai/llm"
  3. import { LLMClient } from "@opencode-ai/llm/route"
  4. import { ConfigProvider, DateTime, Effect } from "effect"
  5. import { Headers } from "effect/unstable/http"
  6. import { Credential } from "@opencode-ai/core/credential"
  7. import { Integration } from "@opencode-ai/core/integration"
  8. import { ModelV2 } from "@opencode-ai/core/model"
  9. import { ProviderV2 } from "@opencode-ai/core/provider"
  10. import { ProjectV2 } from "@opencode-ai/core/project"
  11. import { SessionRunnerModel } from "@opencode-ai/core/session/runner/model"
  12. import { SessionV2 } from "@opencode-ai/core/session"
  13. import { AbsolutePath } from "@opencode-ai/core/schema"
  14. import { it } from "./lib/effect"
  15. type Api =
  16. | {
  17. readonly type: "aisdk"
  18. readonly package: string
  19. readonly url?: string
  20. readonly settings?: Record<string, unknown>
  21. }
  22. | { readonly type: "native"; readonly url?: string; readonly settings: Record<string, unknown> }
  23. const model = (api: Api, variants: ModelV2.Info["variants"] = []) =>
  24. new ModelV2.Info({
  25. id: ModelV2.ID.make("test-model"),
  26. providerID: ProviderV2.ID.make("test-provider"),
  27. name: "Test model",
  28. api: { id: ModelV2.ID.make("api-test-model"), ...api },
  29. capabilities: { tools: true, input: ["text"], output: ["text"] },
  30. request: {
  31. headers: { "x-test": "header" },
  32. body: { apiKey: "secret", custom_extension: { enabled: true } },
  33. generation: { temperature: 0.7 },
  34. options: { store: false, serviceTier: "priority" },
  35. },
  36. variants,
  37. time: { released: 0 },
  38. cost: [],
  39. status: "active",
  40. enabled: true,
  41. limit: { context: 100, output: 20 },
  42. })
  43. describe("SessionRunnerModel", () => {
  44. it.effect("maps catalog OpenAI AI SDK models into native Responses routes", () =>
  45. Effect.gen(function* () {
  46. const resolved = yield* SessionRunnerModel.fromCatalogModel(
  47. model({ type: "aisdk", package: "@ai-sdk/openai", url: "https://openai.example/v1" }),
  48. )
  49. expect(resolved).toMatchObject({ id: "api-test-model", provider: "test-provider" })
  50. expect(resolved.route).toMatchObject({
  51. id: "openai-responses",
  52. endpoint: { baseURL: "https://openai.example/v1" },
  53. defaults: {
  54. headers: { "x-test": "header" },
  55. limits: { context: 100, output: 20 },
  56. generation: { temperature: 0.7 },
  57. providerOptions: { openai: { store: false, serviceTier: "priority" } },
  58. http: { body: { custom_extension: { enabled: true } } },
  59. },
  60. })
  61. }),
  62. )
  63. it.effect("keeps catalog apiKey credentials out of provider JSON", () =>
  64. Effect.gen(function* () {
  65. const resolved = yield* SessionRunnerModel.fromCatalogModel(
  66. model({ type: "aisdk", package: "@ai-sdk/openai", url: "https://openai.example/v1" }),
  67. )
  68. const prepared = yield* LLMClient.prepare(LLM.request({ model: resolved, prompt: "Hello" }))
  69. expect(JSON.stringify(prepared.body)).not.toContain("apiKey")
  70. expect(JSON.stringify(prepared.body)).not.toContain("secret")
  71. }),
  72. )
  73. it.effect("uses merged API settings for OpenAI-compatible auth and request defaults", () =>
  74. Effect.gen(function* () {
  75. const resolved = yield* SessionRunnerModel.fromCatalogModel(
  76. new ModelV2.Info({
  77. ...model({
  78. type: "aisdk",
  79. package: "@ai-sdk/openai-compatible",
  80. url: "https://compatible.example/v1",
  81. settings: { apiKey: "settings-secret", compatibility: "strict" },
  82. }),
  83. request: { headers: {}, body: {}, generation: {}, options: {} },
  84. }),
  85. )
  86. const request = LLM.request({ model: resolved, prompt: "Hello" })
  87. const headers = yield* resolved.route.auth.apply({
  88. request,
  89. method: "POST",
  90. url: "https://compatible.example/v1/chat/completions",
  91. body: "{}",
  92. headers: Headers.empty,
  93. })
  94. expect(headers.authorization).toBe("Bearer settings-secret")
  95. expect(resolved.route.defaults.http?.body).toEqual({})
  96. }),
  97. )
  98. it.effect("lowers selected OpenAI Session variants into Responses options", () =>
  99. Effect.gen(function* () {
  100. const base = model({ type: "aisdk", package: "@ai-sdk/openai", url: "https://openai.example/v1" }, [
  101. {
  102. id: ModelV2.VariantID.make("high"),
  103. headers: { "x-variant": "high" },
  104. body: {},
  105. generation: { temperature: 0.2 },
  106. options: { reasoningEffort: "high" },
  107. },
  108. ])
  109. const catalog = new ModelV2.Info({
  110. ...base,
  111. request: { ...base.request, options: { ...base.request.options, reasoningEffort: "medium" } },
  112. })
  113. const session = SessionV2.Info.make({
  114. id: SessionV2.ID.make("ses_model_variant"),
  115. projectID: ProjectV2.ID.global,
  116. title: "test",
  117. model: {
  118. id: catalog.id,
  119. providerID: catalog.providerID,
  120. variant: ModelV2.VariantID.make("high"),
  121. },
  122. cost: 0,
  123. tokens: { input: 0, output: 0, reasoning: 0, cache: { read: 0, write: 0 } },
  124. time: { created: DateTime.makeUnsafe(0), updated: DateTime.makeUnsafe(0) },
  125. location: { directory: AbsolutePath.make("/project") },
  126. })
  127. const resolved = yield* SessionRunnerModel.resolve(session, catalog)
  128. const prepared = yield* LLMClient.prepare(LLM.request({ model: resolved, prompt: "Hello" }))
  129. expect(resolved.route.defaults.headers).toMatchObject({ "x-test": "header", "x-variant": "high" })
  130. expect(resolved.route.defaults.http?.body).toEqual({ custom_extension: { enabled: true } })
  131. expect(prepared.body).toMatchObject({
  132. store: false,
  133. service_tier: "priority",
  134. temperature: 0.2,
  135. reasoning: { effort: "high" },
  136. })
  137. expect(prepared.body).not.toHaveProperty("reasoningEffort")
  138. }),
  139. )
  140. it.effect("lowers selected OpenAI-compatible Session variants into Chat options", () =>
  141. Effect.gen(function* () {
  142. const catalog = model(
  143. { type: "aisdk", package: "@ai-sdk/openai-compatible", url: "https://compatible.example/v1" },
  144. [
  145. {
  146. id: ModelV2.VariantID.make("high"),
  147. headers: {},
  148. body: {},
  149. generation: {},
  150. options: { reasoningEffort: "high" },
  151. },
  152. ],
  153. )
  154. const session = SessionV2.Info.make({
  155. id: SessionV2.ID.make("ses_compatible_variant"),
  156. projectID: ProjectV2.ID.global,
  157. title: "test",
  158. model: { id: catalog.id, providerID: catalog.providerID, variant: ModelV2.VariantID.make("high") },
  159. cost: 0,
  160. tokens: { input: 0, output: 0, reasoning: 0, cache: { read: 0, write: 0 } },
  161. time: { created: DateTime.makeUnsafe(0), updated: DateTime.makeUnsafe(0) },
  162. location: { directory: AbsolutePath.make("/project") },
  163. })
  164. const resolved = yield* SessionRunnerModel.resolve(session, catalog)
  165. const prepared = yield* LLMClient.prepare(LLM.request({ model: resolved, prompt: "Hello" }))
  166. expect(resolved.route.defaults.http?.body).toEqual({ custom_extension: { enabled: true } })
  167. expect(prepared.body).toMatchObject({
  168. store: false,
  169. reasoning_effort: "high",
  170. })
  171. expect(prepared.body).not.toHaveProperty("reasoningEffort")
  172. }),
  173. )
  174. it.effect("rejects an explicit unavailable Session variant during model resolution", () =>
  175. Effect.gen(function* () {
  176. const catalog = model({ type: "aisdk", package: "@ai-sdk/openai", url: "https://openai.example/v1" })
  177. const session = SessionV2.Info.make({
  178. id: SessionV2.ID.make("ses_model_variant_unavailable"),
  179. projectID: ProjectV2.ID.global,
  180. title: "test",
  181. model: {
  182. id: catalog.id,
  183. providerID: catalog.providerID,
  184. variant: ModelV2.VariantID.make("unknown"),
  185. },
  186. cost: 0,
  187. tokens: { input: 0, output: 0, reasoning: 0, cache: { read: 0, write: 0 } },
  188. time: { created: DateTime.makeUnsafe(0), updated: DateTime.makeUnsafe(0) },
  189. location: { directory: AbsolutePath.make("/project") },
  190. })
  191. const failure = yield* SessionRunnerModel.resolve(session, catalog).pipe(Effect.flip)
  192. expect(failure).toMatchObject({
  193. _tag: "SessionRunnerModel.VariantUnavailableError",
  194. providerID: "test-provider",
  195. modelID: "test-model",
  196. variant: "unknown",
  197. })
  198. }),
  199. )
  200. it.effect("lowers selected Anthropic Session variants into Messages options", () =>
  201. Effect.gen(function* () {
  202. const catalog = model({ type: "aisdk", package: "@ai-sdk/anthropic", url: "https://anthropic.example/v1" }, [
  203. {
  204. id: ModelV2.VariantID.make("high"),
  205. headers: {},
  206. body: {},
  207. generation: {},
  208. options: { thinking: { type: "enabled", budgetTokens: 12000 } },
  209. },
  210. ])
  211. const session = SessionV2.Info.make({
  212. id: SessionV2.ID.make("ses_anthropic_variant"),
  213. projectID: ProjectV2.ID.global,
  214. title: "test",
  215. model: { id: catalog.id, providerID: catalog.providerID, variant: ModelV2.VariantID.make("high") },
  216. cost: 0,
  217. tokens: { input: 0, output: 0, reasoning: 0, cache: { read: 0, write: 0 } },
  218. time: { created: DateTime.makeUnsafe(0), updated: DateTime.makeUnsafe(0) },
  219. location: { directory: AbsolutePath.make("/project") },
  220. })
  221. const resolved = yield* SessionRunnerModel.resolve(session, catalog)
  222. const prepared = yield* LLMClient.prepare(LLM.request({ model: resolved, prompt: "Hello" }))
  223. expect(resolved.route.defaults.http?.body).toEqual({ custom_extension: { enabled: true } })
  224. expect(prepared.body).toMatchObject({
  225. thinking: { type: "enabled", budget_tokens: 12000 },
  226. })
  227. expect(JSON.stringify(prepared.body)).not.toContain("budgetTokens")
  228. }),
  229. )
  230. it.effect("maps catalog Anthropic AI SDK models into native routes", () =>
  231. Effect.gen(function* () {
  232. const resolved = yield* SessionRunnerModel.fromCatalogModel(
  233. model({ type: "aisdk", package: "@ai-sdk/anthropic", url: "https://anthropic.example/v1" }),
  234. )
  235. expect(resolved.route).toMatchObject({
  236. id: "anthropic-messages",
  237. endpoint: { baseURL: "https://anthropic.example/v1" },
  238. })
  239. }),
  240. )
  241. it.effect("preserves environment-backed bearer auth", () =>
  242. Effect.gen(function* () {
  243. const resolved = yield* SessionRunnerModel.fromCatalogModel(
  244. new ModelV2.Info({
  245. ...model({ type: "aisdk", package: "@ai-sdk/openai", url: "https://openai.example/v1" }),
  246. request: { headers: {}, body: {}, generation: {}, options: {} },
  247. }),
  248. { type: "env", name: "TEST_PROVIDER_API_KEY" },
  249. )
  250. const request = LLM.request({ model: resolved, prompt: "Hello" })
  251. const headers = yield* resolved.route.auth
  252. .apply({
  253. request,
  254. method: "POST",
  255. url: "https://openai.example/v1/responses",
  256. body: "{}",
  257. headers: Headers.empty,
  258. })
  259. .pipe(
  260. Effect.provide(ConfigProvider.layer(ConfigProvider.fromEnv({ env: { TEST_PROVIDER_API_KEY: "secret" } }))),
  261. )
  262. expect(headers.authorization).toBe("Bearer secret")
  263. }),
  264. )
  265. it.effect("prefers stored credentials over configured auth", () =>
  266. Effect.gen(function* () {
  267. const credential = new Credential.Info({
  268. id: Credential.ID.create(),
  269. integrationID: Integration.ID.make("test-provider"),
  270. label: "Work",
  271. value: new Credential.Key({ type: "key", key: "stored-secret", metadata: { tenant: "work" } }),
  272. })
  273. const resolved = yield* SessionRunnerModel.fromCatalogModel(
  274. new ModelV2.Info({
  275. ...model({ type: "aisdk", package: "@ai-sdk/openai", url: "https://openai.example/v1" }),
  276. request: { headers: {}, body: { apiKey: "configured-secret" }, generation: {}, options: {} },
  277. }),
  278. { type: "credential", id: credential.id, label: credential.label },
  279. credential,
  280. )
  281. const headers = yield* resolved.route.auth.apply({
  282. request: LLM.request({ model: resolved, prompt: "Hello" }),
  283. method: "POST",
  284. url: "https://openai.example/v1/responses",
  285. body: "{}",
  286. headers: Headers.empty,
  287. })
  288. expect(headers.authorization).toBe("Bearer stored-secret")
  289. expect(resolved.route.defaults.http?.body).toEqual({ tenant: "work" })
  290. }),
  291. )
  292. it.effect("rejects catalog APIs without a native route", () =>
  293. Effect.gen(function* () {
  294. const failure = yield* SessionRunnerModel.fromCatalogModel(
  295. model({ type: "aisdk", package: "@ai-sdk/google", url: "https://google.example/v1" }),
  296. ).pipe(Effect.flip)
  297. expect(failure).toMatchObject({
  298. _tag: "SessionRunnerModel.UnsupportedApiError",
  299. providerID: "test-provider",
  300. modelID: "test-model",
  301. api: "aisdk:@ai-sdk/google",
  302. })
  303. }),
  304. )
  305. it.effect("reports whether a catalog model has a supported native route", () =>
  306. Effect.sync(() => {
  307. expect(
  308. SessionRunnerModel.supported(
  309. model({ type: "aisdk", package: "@ai-sdk/openai", url: "https://openai.example/v1" }),
  310. ),
  311. ).toBe(true)
  312. expect(
  313. SessionRunnerModel.supported(
  314. model({ type: "aisdk", package: "@ai-sdk/google", url: "https://google.example/v1" }),
  315. ),
  316. ).toBe(false)
  317. expect(SessionRunnerModel.supported(model({ type: "native", settings: {} }))).toBe(false)
  318. }),
  319. )
  320. })