provider-perplexity.test.ts 4.6 KB

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