variant.test.ts 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. import { describe, expect } from "bun:test"
  2. import { Catalog } from "@opencode-ai/core/catalog"
  3. import { Credential } from "@opencode-ai/core/credential"
  4. import { EventV2 } from "@opencode-ai/core/event"
  5. import { Integration } from "@opencode-ai/core/integration"
  6. import { Location } from "@opencode-ai/core/location"
  7. import { ModelV2 } from "@opencode-ai/core/model"
  8. import { Policy } from "@opencode-ai/core/policy"
  9. import { VariantPlugin } from "@opencode-ai/core/plugin/variant"
  10. import { ProviderV2 } from "@opencode-ai/core/provider"
  11. import { AbsolutePath } from "@opencode-ai/core/schema"
  12. import { Effect, Layer } from "effect"
  13. import { location } from "../fixture/location"
  14. import { testEffect } from "../lib/effect"
  15. import { catalogHost, host } from "./host"
  16. const events = EventV2.defaultLayer
  17. const locationLayer = Layer.succeed(
  18. Location.Service,
  19. Location.Service.of(location({ directory: AbsolutePath.make(import.meta.dir) })),
  20. )
  21. const connections = Credential.defaultLayer.pipe(Layer.fresh)
  22. const integrations = Integration.locationLayer.pipe(Layer.provide(events), Layer.provide(connections))
  23. const catalog = Catalog.layer.pipe(
  24. Layer.provide(
  25. Layer.mergeAll(events, locationLayer, Policy.layer.pipe(Layer.provide(locationLayer)), connections, integrations),
  26. ),
  27. )
  28. const it = testEffect(
  29. Layer.mergeAll(catalog.pipe(Layer.provide(connections)), integrations, connections, events, locationLayer),
  30. )
  31. describe("VariantPlugin", () => {
  32. it.effect("adds GLM 5.2 variants after catalog sources", () =>
  33. Effect.gen(function* () {
  34. const service = yield* Catalog.Service
  35. yield* service.transform((catalog) => {
  36. catalog.provider.update(ProviderV2.ID.opencode, (provider) => {
  37. provider.api = { type: "aisdk", package: "@ai-sdk/openai-compatible" }
  38. })
  39. catalog.model.update(ProviderV2.ID.opencode, ModelV2.ID.make("glm-5.2"), (model) => {
  40. model.api = {
  41. id: ModelV2.ID.make("glm-5.2"),
  42. type: "aisdk",
  43. package: "@ai-sdk/openai-compatible",
  44. }
  45. })
  46. })
  47. yield* VariantPlugin.Plugin.effect(host({ catalog: catalogHost(service) }))
  48. expect((yield* service.model.get(ProviderV2.ID.opencode, ModelV2.ID.make("glm-5.2")))?.variants).toEqual([
  49. expect.objectContaining({ id: "high", body: { reasoning_effort: "high" } }),
  50. expect.objectContaining({ id: "max", body: { reasoning_effort: "max" } }),
  51. ])
  52. }),
  53. )
  54. it.effect("keeps explicit variants over generated defaults", () =>
  55. Effect.gen(function* () {
  56. const service = yield* Catalog.Service
  57. yield* service.transform((catalog) => {
  58. catalog.model.update(ProviderV2.ID.opencode, ModelV2.ID.make("glm-5.2"), (model) => {
  59. model.api = {
  60. id: ModelV2.ID.make("glm-5.2"),
  61. type: "aisdk",
  62. package: "@ai-sdk/openai-compatible",
  63. }
  64. model.variants = [{ id: ModelV2.VariantID.make("high"), headers: { custom: "true" }, body: {} }]
  65. })
  66. })
  67. yield* VariantPlugin.Plugin.effect(host({ catalog: catalogHost(service) }))
  68. expect((yield* service.model.get(ProviderV2.ID.opencode, ModelV2.ID.make("glm-5.2")))?.variants).toEqual([
  69. expect.objectContaining({ id: "high", headers: { custom: "true" } }),
  70. expect.objectContaining({ id: "max", body: { reasoning_effort: "max" } }),
  71. ])
  72. }),
  73. )
  74. })