variant.test.ts 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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(
  26. events,
  27. locationLayer,
  28. Policy.layer.pipe(Layer.provide(locationLayer)),
  29. connections,
  30. integrations,
  31. ),
  32. ),
  33. )
  34. const it = testEffect(
  35. Layer.mergeAll(catalog.pipe(Layer.provide(connections)), integrations, connections, events, locationLayer),
  36. )
  37. describe("VariantPlugin", () => {
  38. it.effect("adds GLM 5.2 variants after catalog sources", () =>
  39. Effect.gen(function* () {
  40. const service = yield* Catalog.Service
  41. yield* service.transform((catalog) => {
  42. catalog.provider.update(ProviderV2.ID.opencode, (provider) => {
  43. provider.api = { type: "aisdk", package: "@ai-sdk/openai-compatible" }
  44. })
  45. catalog.model.update(ProviderV2.ID.opencode, ModelV2.ID.make("glm-5.2"), (model) => {
  46. model.api = {
  47. id: ModelV2.ID.make("glm-5.2"),
  48. type: "aisdk",
  49. package: "@ai-sdk/openai-compatible",
  50. }
  51. })
  52. })
  53. yield* VariantPlugin.Plugin.effect(host({ catalog: catalogHost(service) }))
  54. expect((yield* service.model.get(ProviderV2.ID.opencode, ModelV2.ID.make("glm-5.2")))?.variants).toEqual([
  55. expect.objectContaining({ id: "high", body: { reasoning_effort: "high" } }),
  56. expect.objectContaining({ id: "max", body: { reasoning_effort: "max" } }),
  57. ])
  58. }),
  59. )
  60. it.effect("keeps explicit variants over generated defaults", () =>
  61. Effect.gen(function* () {
  62. const service = yield* Catalog.Service
  63. yield* service.transform((catalog) => {
  64. catalog.model.update(ProviderV2.ID.opencode, ModelV2.ID.make("glm-5.2"), (model) => {
  65. model.api = {
  66. id: ModelV2.ID.make("glm-5.2"),
  67. type: "aisdk",
  68. package: "@ai-sdk/openai-compatible",
  69. }
  70. model.variants = [
  71. { id: ModelV2.VariantID.make("high"), headers: { custom: "true" }, body: {} },
  72. ]
  73. })
  74. })
  75. yield* VariantPlugin.Plugin.effect(host({ catalog: catalogHost(service) }))
  76. expect((yield* service.model.get(ProviderV2.ID.opencode, ModelV2.ID.make("glm-5.2")))?.variants).toEqual([
  77. expect.objectContaining({ id: "high", headers: { custom: "true" } }),
  78. expect.objectContaining({ id: "max", body: { reasoning_effort: "max" } }),
  79. ])
  80. }),
  81. )
  82. })