provider-perplexity.test.ts 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. import { AISDK } from "@opencode-ai/core/aisdk"
  2. import { describe, expect } from "bun:test"
  3. import type { LanguageModelV3 } from "@ai-sdk/provider"
  4. import { Effect } from "effect"
  5. import { ModelV2 } from "@opencode-ai/core/model"
  6. import { PluginV2 } from "@opencode-ai/core/plugin"
  7. import { PluginHost } from "@opencode-ai/core/plugin/host"
  8. import { PerplexityPlugin } from "@opencode-ai/core/plugin/provider/perplexity"
  9. import { ProviderV2 } from "@opencode-ai/core/provider"
  10. import { testEffect } from "../lib/effect"
  11. import { PluginTestLayer } from "./fixture"
  12. const it = testEffect(PluginTestLayer)
  13. const addPlugin = Effect.fn(function* () {
  14. const plugin = yield* PluginV2.Service
  15. const aisdk = yield* AISDK.Service
  16. const host = yield* PluginHost.make(plugin)
  17. yield* PerplexityPlugin.effect(host)
  18. })
  19. function fakeSelectorSdk(calls: string[]) {
  20. const make = (method: string) => (id: string) => {
  21. calls.push(`${method}:${id}`)
  22. return { modelId: id, provider: method, specificationVersion: "v3" } as unknown as LanguageModelV3
  23. }
  24. return {
  25. responses: make("responses"),
  26. messages: make("messages"),
  27. chat: make("chat"),
  28. languageModel: make("languageModel"),
  29. }
  30. }
  31. describe("PerplexityPlugin", () => {
  32. it.effect("creates a Perplexity SDK for the exact @ai-sdk/perplexity package", () =>
  33. Effect.gen(function* () {
  34. const plugin = yield* PluginV2.Service
  35. const aisdk = yield* AISDK.Service
  36. yield* addPlugin()
  37. const result = yield* aisdk.runSDK({
  38. model: ModelV2.Info.make({
  39. ...ModelV2.Info.empty(ProviderV2.ID.make("perplexity"), ModelV2.ID.make("sonar")),
  40. api: { id: ModelV2.ID.make("sonar"), type: "aisdk", package: "test-provider" },
  41. }),
  42. package: "@ai-sdk/perplexity",
  43. options: { name: "perplexity" },
  44. })
  45. expect(result.sdk).toBeDefined()
  46. }),
  47. )
  48. it.effect("ignores packages that are not the bundled Perplexity package", () =>
  49. Effect.gen(function* () {
  50. const plugin = yield* PluginV2.Service
  51. const aisdk = yield* AISDK.Service
  52. yield* addPlugin()
  53. const result = yield* aisdk.runSDK({
  54. model: ModelV2.Info.make({
  55. ...ModelV2.Info.empty(ProviderV2.ID.make("perplexity"), ModelV2.ID.make("sonar")),
  56. api: { id: ModelV2.ID.make("sonar"), type: "aisdk", package: "test-provider" },
  57. }),
  58. package: "@ai-sdk/perplexity-compatible",
  59. options: { name: "perplexity" },
  60. })
  61. expect(result.sdk).toBeUndefined()
  62. }),
  63. )
  64. it.effect("uses the Perplexity provider ID as the SDK name for the bundled provider", () =>
  65. Effect.gen(function* () {
  66. const plugin = yield* PluginV2.Service
  67. const aisdk = yield* AISDK.Service
  68. yield* addPlugin()
  69. const result = yield* aisdk.runSDK({
  70. model: ModelV2.Info.make({
  71. ...ModelV2.Info.empty(ProviderV2.ID.make("perplexity"), ModelV2.ID.make("sonar")),
  72. api: { id: ModelV2.ID.make("sonar"), type: "aisdk", package: "test-provider" },
  73. }),
  74. package: "@ai-sdk/perplexity",
  75. options: { name: "perplexity" },
  76. })
  77. expect(result.sdk.languageModel("sonar").provider).toBe("perplexity")
  78. }),
  79. )
  80. it.effect("creates bundled Perplexity SDKs for custom provider IDs", () =>
  81. Effect.gen(function* () {
  82. const plugin = yield* PluginV2.Service
  83. const aisdk = yield* AISDK.Service
  84. yield* addPlugin()
  85. const result = yield* aisdk.runSDK({
  86. model: ModelV2.Info.make({
  87. ...ModelV2.Info.empty(ProviderV2.ID.make("custom-perplexity"), ModelV2.ID.make("sonar")),
  88. api: { id: ModelV2.ID.make("sonar"), type: "aisdk", package: "test-provider" },
  89. }),
  90. package: "@ai-sdk/perplexity",
  91. options: { name: "custom-perplexity" },
  92. })
  93. expect(result.sdk.languageModel("sonar").provider).toBe("perplexity")
  94. }),
  95. )
  96. it.effect("leaves Perplexity language selection to the default languageModel fallback", () =>
  97. Effect.gen(function* () {
  98. const plugin = yield* PluginV2.Service
  99. const aisdk = yield* AISDK.Service
  100. const calls: string[] = []
  101. yield* addPlugin()
  102. const result = yield* aisdk.runLanguage({
  103. model: ModelV2.Info.make({
  104. ...ModelV2.Info.empty(ProviderV2.ID.make("perplexity"), ModelV2.ID.make("alias")),
  105. api: { id: ModelV2.ID.make("sonar"), type: "aisdk", package: "test-provider" },
  106. }),
  107. sdk: fakeSelectorSdk(calls),
  108. options: {},
  109. })
  110. expect(calls).toEqual([])
  111. expect(result.language).toBeUndefined()
  112. }),
  113. )
  114. })