provider-helper.ts 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. import { Npm } from "@opencode-ai/core/npm"
  2. import type { LanguageModelV3 } from "@ai-sdk/provider"
  3. import { expect } from "bun:test"
  4. import { Effect, Layer, Option } from "effect"
  5. import { Catalog } from "@opencode-ai/core/catalog"
  6. import { EventV2 } from "@opencode-ai/core/event"
  7. import { Location } from "@opencode-ai/core/location"
  8. import { ModelV2 } from "@opencode-ai/core/model"
  9. import { PluginV2 } from "@opencode-ai/core/plugin"
  10. import { ProviderV2 } from "@opencode-ai/core/provider"
  11. import { AbsolutePath } from "@opencode-ai/core/schema"
  12. import { location } from "../fixture/location"
  13. import { testEffect } from "../lib/effect"
  14. export const fixtureProvider = new URL("./fixtures/provider-factory.ts", import.meta.url).href
  15. const locationLayer = Layer.succeed(
  16. Location.Service,
  17. Location.Service.of(location({ directory: AbsolutePath.make("test") })),
  18. )
  19. export const npmLayer = Layer.succeed(
  20. Npm.Service,
  21. Npm.Service.of({
  22. add: () => Effect.succeed({ directory: "", entrypoint: Option.none<string>() }),
  23. install: () => Effect.void,
  24. which: () => Effect.succeed(Option.none<string>()),
  25. }),
  26. )
  27. export const catalogLayer = Layer.succeed(
  28. Catalog.Service,
  29. Catalog.Service.of({
  30. transform: () => Effect.die("unexpected catalog.transform"),
  31. provider: {
  32. get: () => Effect.die("unexpected provider.get"),
  33. all: () => Effect.succeed([]),
  34. available: () => Effect.succeed([]),
  35. },
  36. model: {
  37. get: () => Effect.die("unexpected model.get"),
  38. all: () => Effect.succeed([]),
  39. available: () => Effect.succeed([]),
  40. default: () => Effect.succeed(Option.none<ModelV2.Info>()),
  41. small: () => Effect.succeed(Option.none<ModelV2.Info>()),
  42. },
  43. }),
  44. )
  45. export const it = testEffect(
  46. Catalog.locationLayer.pipe(
  47. Layer.provideMerge(EventV2.defaultLayer),
  48. Layer.provideMerge(locationLayer),
  49. Layer.provideMerge(npmLayer),
  50. ),
  51. )
  52. type ProviderInput = Partial<Omit<ProviderV2.Info, "api" | "request">> & {
  53. api?: ProviderV2.Api
  54. request?: ProviderV2.Request
  55. }
  56. type ModelInput = Partial<Omit<ModelV2.Info, "api" | "request">> & {
  57. api?: (ProviderV2.Api & { id?: ModelV2.ID }) | { id: ModelV2.ID }
  58. request?: ModelV2.Info["request"]
  59. }
  60. export function provider(providerID: string, options?: ProviderInput) {
  61. return new ProviderV2.Info({
  62. ...ProviderV2.Info.empty(ProviderV2.ID.make(providerID)),
  63. api: options?.api ?? {
  64. type: "aisdk",
  65. package: "test-provider",
  66. },
  67. ...options,
  68. request: {
  69. headers: {},
  70. body: {},
  71. ...options?.request,
  72. },
  73. })
  74. }
  75. export function model(providerID: string, modelID: string, options?: ModelInput) {
  76. return new ModelV2.Info({
  77. ...ModelV2.Info.empty(ProviderV2.ID.make(providerID), ModelV2.ID.make(modelID)),
  78. ...options,
  79. api:
  80. options?.api && "type" in options.api
  81. ? { id: ModelV2.ID.make(modelID), ...options.api }
  82. : {
  83. id: ModelV2.ID.make(modelID),
  84. ...options?.api,
  85. type: "aisdk",
  86. package: "test-provider",
  87. },
  88. request: {
  89. headers: {},
  90. body: {},
  91. ...options?.request,
  92. },
  93. })
  94. }
  95. export function withEnv<A, E, R>(vars: Record<string, string | undefined>, fx: () => Effect.Effect<A, E, R>) {
  96. return Effect.acquireUseRelease(
  97. Effect.sync(() => {
  98. const previous = Object.fromEntries(Object.keys(vars).map((key) => [key, process.env[key]]))
  99. for (const [key, value] of Object.entries(vars)) {
  100. if (value === undefined) delete process.env[key]
  101. else process.env[key] = value
  102. }
  103. return previous
  104. }),
  105. () => fx(),
  106. (previous) =>
  107. Effect.sync(() => {
  108. for (const [key, value] of Object.entries(previous)) {
  109. if (value === undefined) delete process.env[key]
  110. else process.env[key] = value
  111. }
  112. }),
  113. )
  114. }
  115. export function fakeSelectorSdk(calls: string[]) {
  116. const make = (method: string) => (id: string) => {
  117. calls.push(`${method}:${id}`)
  118. return { modelId: id, provider: method, specificationVersion: "v3" } as unknown as LanguageModelV3
  119. }
  120. return {
  121. responses: make("responses"),
  122. messages: make("messages"),
  123. chat: make("chat"),
  124. languageModel: make("languageModel"),
  125. }
  126. }
  127. export function expectPluginRegistered(ids: string[], id: string) {
  128. expect(ids).toContain(PluginV2.ID.make(id))
  129. }