provider-perplexity.test.ts 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. modelID: ModelV2.ID.make("sonar"),
  41. package: "aisdk:test-provider",
  42. }),
  43. package: "@ai-sdk/perplexity",
  44. options: { name: "perplexity" },
  45. })
  46. expect(result.sdk).toBeDefined()
  47. }),
  48. )
  49. it.effect("ignores packages that are not the bundled Perplexity package", () =>
  50. Effect.gen(function* () {
  51. const plugin = yield* PluginV2.Service
  52. const aisdk = yield* AISDK.Service
  53. yield* addPlugin()
  54. const result = yield* aisdk.runSDK({
  55. model: ModelV2.Info.make({
  56. ...ModelV2.Info.empty(ProviderV2.ID.make("perplexity"), ModelV2.ID.make("sonar")),
  57. modelID: ModelV2.ID.make("sonar"),
  58. package: "aisdk:test-provider",
  59. }),
  60. package: "@ai-sdk/perplexity-compatible",
  61. options: { name: "perplexity" },
  62. })
  63. expect(result.sdk).toBeUndefined()
  64. }),
  65. )
  66. it.effect("uses the Perplexity provider ID as the SDK name for the bundled provider", () =>
  67. Effect.gen(function* () {
  68. const plugin = yield* PluginV2.Service
  69. const aisdk = yield* AISDK.Service
  70. yield* addPlugin()
  71. const result = yield* aisdk.runSDK({
  72. model: ModelV2.Info.make({
  73. ...ModelV2.Info.empty(ProviderV2.ID.make("perplexity"), ModelV2.ID.make("sonar")),
  74. modelID: ModelV2.ID.make("sonar"),
  75. package: "aisdk:test-provider",
  76. }),
  77. package: "@ai-sdk/perplexity",
  78. options: { name: "perplexity" },
  79. })
  80. expect(result.sdk.languageModel("sonar").provider).toBe("perplexity")
  81. }),
  82. )
  83. it.effect("creates bundled Perplexity SDKs for custom provider IDs", () =>
  84. Effect.gen(function* () {
  85. const plugin = yield* PluginV2.Service
  86. const aisdk = yield* AISDK.Service
  87. yield* addPlugin()
  88. const result = yield* aisdk.runSDK({
  89. model: ModelV2.Info.make({
  90. ...ModelV2.Info.empty(ProviderV2.ID.make("custom-perplexity"), ModelV2.ID.make("sonar")),
  91. modelID: ModelV2.ID.make("sonar"),
  92. package: "aisdk:test-provider",
  93. }),
  94. package: "@ai-sdk/perplexity",
  95. options: { name: "custom-perplexity" },
  96. })
  97. expect(result.sdk.languageModel("sonar").provider).toBe("perplexity")
  98. }),
  99. )
  100. it.effect("leaves Perplexity language selection to the default languageModel fallback", () =>
  101. Effect.gen(function* () {
  102. const plugin = yield* PluginV2.Service
  103. const aisdk = yield* AISDK.Service
  104. const calls: string[] = []
  105. yield* addPlugin()
  106. const result = yield* aisdk.runLanguage({
  107. model: ModelV2.Info.make({
  108. ...ModelV2.Info.empty(ProviderV2.ID.make("perplexity"), ModelV2.ID.make("alias")),
  109. modelID: ModelV2.ID.make("sonar"),
  110. package: "aisdk:test-provider",
  111. }),
  112. sdk: fakeSelectorSdk(calls),
  113. options: {},
  114. })
  115. expect(calls).toEqual([])
  116. expect(result.language).toBeUndefined()
  117. }),
  118. )
  119. })