catalog.test.ts 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. import { describe, expect } from "bun:test"
  2. import { DateTime, Effect, Layer, Option } from "effect"
  3. import { Catalog } from "@opencode-ai/core/catalog"
  4. import { ModelV2 } from "@opencode-ai/core/model"
  5. import { PluginV2 } from "@opencode-ai/core/plugin"
  6. import { ProviderV2 } from "@opencode-ai/core/provider"
  7. import { testEffect } from "../lib/effect"
  8. const it = testEffect(Catalog.layer.pipe(Layer.provideMerge(PluginV2.defaultLayer)))
  9. describe("CatalogV2", () => {
  10. it.effect("normalizes provider baseURL into endpoint url", () =>
  11. Effect.gen(function* () {
  12. const catalog = yield* Catalog.Service
  13. const providerID = ProviderV2.ID.make("test")
  14. yield* catalog.provider.update(providerID, (provider) => {
  15. provider.endpoint = {
  16. type: "aisdk",
  17. package: "@ai-sdk/openai-compatible",
  18. url: "https://default.example.com",
  19. }
  20. provider.options.aisdk.provider.baseURL = "https://override.example.com"
  21. })
  22. const provider = yield* catalog.provider.get(providerID)
  23. expect(provider.endpoint).toEqual({
  24. type: "aisdk",
  25. package: "@ai-sdk/openai-compatible",
  26. url: "https://override.example.com",
  27. })
  28. expect(provider.options.aisdk.provider.baseURL).toBeUndefined()
  29. }),
  30. )
  31. it.effect("normalizes model baseURL into endpoint url", () =>
  32. Effect.gen(function* () {
  33. const catalog = yield* Catalog.Service
  34. const providerID = ProviderV2.ID.make("test")
  35. const modelID = ModelV2.ID.make("model")
  36. yield* catalog.provider.update(providerID, (provider) => {
  37. provider.endpoint = {
  38. type: "aisdk",
  39. package: "@ai-sdk/openai-compatible",
  40. url: "https://provider.example.com",
  41. }
  42. })
  43. yield* catalog.model.update(providerID, modelID, (model) => {
  44. model.endpoint = {
  45. type: "aisdk",
  46. package: "@ai-sdk/openai-compatible",
  47. url: "https://model.example.com",
  48. }
  49. model.options.aisdk.provider.baseURL = "https://override.example.com"
  50. })
  51. const model = yield* catalog.model.get(providerID, modelID)
  52. expect(model.endpoint).toEqual({
  53. type: "aisdk",
  54. package: "@ai-sdk/openai-compatible",
  55. url: "https://override.example.com",
  56. })
  57. expect(model.options.aisdk.provider.baseURL).toBeUndefined()
  58. }),
  59. )
  60. it.effect("resolves unknown model endpoint from provider endpoint", () =>
  61. Effect.gen(function* () {
  62. const catalog = yield* Catalog.Service
  63. const providerID = ProviderV2.ID.make("test")
  64. const modelID = ModelV2.ID.make("model")
  65. yield* catalog.provider.update(providerID, (provider) => {
  66. provider.endpoint = {
  67. type: "aisdk",
  68. package: "@ai-sdk/openai-compatible",
  69. url: "https://provider.example.com",
  70. }
  71. })
  72. yield* catalog.model.update(providerID, modelID, () => {})
  73. const model = yield* catalog.model.get(providerID, modelID)
  74. expect(model.endpoint).toEqual({
  75. type: "aisdk",
  76. package: "@ai-sdk/openai-compatible",
  77. url: "https://provider.example.com",
  78. })
  79. }),
  80. )
  81. it.effect("runs provider hooks after baseURL is normalized", () =>
  82. Effect.gen(function* () {
  83. const catalog = yield* Catalog.Service
  84. const plugin = yield* PluginV2.Service
  85. const providerID = ProviderV2.ID.make("test")
  86. const seen: unknown[] = []
  87. yield* plugin.add({
  88. id: PluginV2.ID.make("test"),
  89. effect: Effect.succeed({
  90. "provider.update": (evt) =>
  91. Effect.sync(() => {
  92. seen.push(evt.provider.endpoint.type)
  93. if (evt.provider.endpoint.type === "aisdk") seen.push(evt.provider.endpoint.url)
  94. seen.push(evt.provider.options.aisdk.provider.baseURL)
  95. }),
  96. }),
  97. })
  98. yield* catalog.provider.update(providerID, (provider) => {
  99. provider.endpoint = {
  100. type: "aisdk",
  101. package: "@ai-sdk/openai-compatible",
  102. }
  103. provider.options.aisdk.provider.baseURL = "https://provider.example.com"
  104. })
  105. expect(seen).toEqual(["aisdk", "https://provider.example.com", undefined])
  106. }),
  107. )
  108. it.effect("resolves provider and model option merges", () =>
  109. Effect.gen(function* () {
  110. const catalog = yield* Catalog.Service
  111. const providerID = ProviderV2.ID.make("test")
  112. const modelID = ModelV2.ID.make("model")
  113. yield* catalog.provider.update(providerID, (provider) => {
  114. provider.options.headers.provider = "provider"
  115. provider.options.headers.shared = "provider"
  116. provider.options.body.provider = true
  117. provider.options.aisdk.provider.provider = true
  118. })
  119. yield* catalog.model.update(providerID, modelID, (model) => {
  120. model.options.headers.model = "model"
  121. model.options.headers.shared = "model"
  122. model.options.body.model = true
  123. model.options.aisdk.provider.model = true
  124. model.options.aisdk.request.request = true
  125. })
  126. const model = yield* catalog.model.get(providerID, modelID)
  127. expect(model.options.headers).toEqual({ provider: "provider", shared: "model", model: "model" })
  128. expect(model.options.body).toEqual({ provider: true, model: true })
  129. expect(model.options.aisdk.provider).toEqual({ provider: true, model: true })
  130. expect(model.options.aisdk.request).toEqual({ request: true })
  131. }),
  132. )
  133. it.effect("falls back to newest available model when no default is configured", () =>
  134. Effect.gen(function* () {
  135. const catalog = yield* Catalog.Service
  136. const providerID = ProviderV2.ID.make("test")
  137. yield* catalog.provider.update(providerID, (provider) => {
  138. provider.enabled = { via: "custom", data: {} }
  139. })
  140. yield* catalog.model.update(providerID, ModelV2.ID.make("old"), (model) => {
  141. model.time.released = DateTime.makeUnsafe(1000)
  142. })
  143. yield* catalog.model.update(providerID, ModelV2.ID.make("new"), (model) => {
  144. model.time.released = DateTime.makeUnsafe(2000)
  145. })
  146. const model = yield* catalog.model.default()
  147. expect(Option.getOrUndefined(model)?.id).toMatch("new")
  148. }),
  149. )
  150. it.effect("small model prefers small keyword candidates before cost scoring", () =>
  151. Effect.gen(function* () {
  152. const catalog = yield* Catalog.Service
  153. const providerID = ProviderV2.ID.make("test")
  154. yield* catalog.provider.update(providerID, () => {})
  155. yield* catalog.model.update(providerID, ModelV2.ID.make("cheap-large"), (model) => {
  156. model.capabilities.input = ["text"]
  157. model.capabilities.output = ["text"]
  158. model.cost = [{ input: 1, output: 1, cache: { read: 0, write: 0 } }]
  159. model.time.released = DateTime.makeUnsafe(Date.now())
  160. })
  161. yield* catalog.model.update(providerID, ModelV2.ID.make("expensive-mini"), (model) => {
  162. model.capabilities.input = ["text"]
  163. model.capabilities.output = ["text"]
  164. model.cost = [{ input: 10, output: 10, cache: { read: 0, write: 0 } }]
  165. model.time.released = DateTime.makeUnsafe(Date.now())
  166. })
  167. const model = yield* catalog.model.small(providerID)
  168. expect(Option.getOrUndefined(model)?.id).toMatch("expensive-mini")
  169. }),
  170. )
  171. })