provider-helper.ts 4.8 KB

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