provider-snowflake-cortex.test.ts 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. import { AISDK } from "@opencode-ai/core/aisdk"
  2. import { describe, expect, it as bun_it } from "bun:test"
  3. import { Effect } from "effect"
  4. import { Model } from "@opencode-ai/core/model"
  5. import { Plugin } from "@opencode-ai/core/plugin"
  6. import { PluginHost } from "@opencode-ai/core/plugin/host"
  7. import { SnowflakeCortexPlugin, cortexFetch } from "@opencode-ai/core/plugin/provider/snowflake-cortex"
  8. import { ProviderPlugins } from "@opencode-ai/core/plugin/provider"
  9. import { Provider } 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* Plugin.Service
  15. const aisdk = yield* AISDK.Service
  16. const host = yield* PluginHost.make(plugin)
  17. yield* SnowflakeCortexPlugin.effect(host)
  18. })
  19. function withEnv<A, E, R>(vars: Record<string, string | undefined>, effect: () => Effect.Effect<A, E, R>) {
  20. return Effect.acquireUseRelease(
  21. Effect.sync(() => {
  22. const previous = Object.fromEntries(Object.keys(vars).map((key) => [key, process.env[key]]))
  23. Object.entries(vars).forEach(([key, value]) => {
  24. if (value === undefined) delete process.env[key]
  25. else process.env[key] = value
  26. })
  27. return previous
  28. }),
  29. effect,
  30. (previous) =>
  31. Effect.sync(() => {
  32. Object.entries(previous).forEach(([key, value]) => {
  33. if (value === undefined) delete process.env[key]
  34. else process.env[key] = value
  35. })
  36. }),
  37. )
  38. }
  39. describe("SnowflakeCortexPlugin", () => {
  40. it.effect("is registered in ProviderPlugins before OpenAICompatiblePlugin", () =>
  41. Effect.sync(() => {
  42. expect(ProviderPlugins.map((item) => item.id)).toContain("opencode.provider.snowflake-cortex")
  43. const ids = ProviderPlugins.map((p) => p.id)
  44. expect(ids.indexOf("opencode.provider.snowflake-cortex")).toBeLessThan(
  45. ids.indexOf("opencode.provider.openai-compatible"),
  46. )
  47. }),
  48. )
  49. it.effect("ignores non-snowflake-cortex providers", () =>
  50. Effect.gen(function* () {
  51. const plugin = yield* Plugin.Service
  52. const aisdk = yield* AISDK.Service
  53. yield* addPlugin()
  54. const result = yield* aisdk.runSDK({
  55. model: Model.Info.make({
  56. ...Model.Info.default(Provider.ID.make("openai"), Model.ID.make("gpt-4")),
  57. modelID: Model.ID.make("gpt-4"),
  58. package: "aisdk:test-provider",
  59. }),
  60. package: "@ai-sdk/openai",
  61. options: { name: "openai" },
  62. })
  63. expect(result.sdk).toBeUndefined()
  64. }),
  65. )
  66. it.effect("creates SDK for snowflake-cortex using SNOWFLAKE_CORTEX_PAT env var", () =>
  67. withEnv({ SNOWFLAKE_CORTEX_PAT: "test-pat" }, () =>
  68. Effect.gen(function* () {
  69. const plugin = yield* Plugin.Service
  70. const aisdk = yield* AISDK.Service
  71. yield* addPlugin()
  72. const result = yield* aisdk.runSDK({
  73. model: Model.Info.make({
  74. ...Model.Info.default(Provider.ID.make("snowflake-cortex"), Model.ID.make("claude-sonnet-4-6")),
  75. modelID: Model.ID.make("claude-sonnet-4-6"),
  76. package: "aisdk:test-provider",
  77. }),
  78. package: "@ai-sdk/openai-compatible",
  79. options: { name: "snowflake-cortex", baseURL: "https://test.snowflakecomputing.com/api/v2/cortex/v1" },
  80. })
  81. expect(result.sdk).toBeDefined()
  82. }),
  83. ),
  84. )
  85. it.effect("falls back to options.apiKey when SNOWFLAKE_CORTEX_PAT env var is absent", () =>
  86. withEnv({ SNOWFLAKE_CORTEX_PAT: undefined }, () =>
  87. Effect.gen(function* () {
  88. const plugin = yield* Plugin.Service
  89. const aisdk = yield* AISDK.Service
  90. yield* addPlugin()
  91. const result = yield* aisdk.runSDK({
  92. model: Model.Info.make({
  93. ...Model.Info.default(Provider.ID.make("snowflake-cortex"), Model.ID.make("claude-sonnet-4-6")),
  94. modelID: Model.ID.make("claude-sonnet-4-6"),
  95. package: "aisdk:test-provider",
  96. }),
  97. package: "@ai-sdk/openai-compatible",
  98. options: {
  99. name: "snowflake-cortex",
  100. baseURL: "https://test.snowflakecomputing.com/api/v2/cortex/v1",
  101. apiKey: "options-pat",
  102. },
  103. })
  104. expect(result.sdk).toBeDefined()
  105. }),
  106. ),
  107. )
  108. it.effect("uses SNOWFLAKE_CORTEX_TOKEN env var", () =>
  109. withEnv({ SNOWFLAKE_CORTEX_TOKEN: "oauth-token", SNOWFLAKE_CORTEX_PAT: undefined }, () =>
  110. Effect.gen(function* () {
  111. const plugin = yield* Plugin.Service
  112. const aisdk = yield* AISDK.Service
  113. yield* addPlugin()
  114. const result = yield* aisdk.runSDK({
  115. model: Model.Info.make({
  116. ...Model.Info.default(Provider.ID.make("snowflake-cortex"), Model.ID.make("claude-sonnet-4-6")),
  117. modelID: Model.ID.make("claude-sonnet-4-6"),
  118. package: "aisdk:test-provider",
  119. }),
  120. package: "@ai-sdk/openai-compatible",
  121. options: { name: "snowflake-cortex", baseURL: "https://test.snowflakecomputing.com/api/v2/cortex/v1" },
  122. })
  123. expect(result.sdk).toBeDefined()
  124. }),
  125. ),
  126. )
  127. it.effect("falls back to options.token when no Snowflake env token is set", () =>
  128. withEnv({ SNOWFLAKE_CORTEX_TOKEN: undefined, SNOWFLAKE_CORTEX_PAT: undefined }, () =>
  129. Effect.gen(function* () {
  130. const plugin = yield* Plugin.Service
  131. const aisdk = yield* AISDK.Service
  132. yield* addPlugin()
  133. const result = yield* aisdk.runSDK({
  134. model: Model.Info.make({
  135. ...Model.Info.default(Provider.ID.make("snowflake-cortex"), Model.ID.make("claude-sonnet-4-6")),
  136. modelID: Model.ID.make("claude-sonnet-4-6"),
  137. package: "aisdk:test-provider",
  138. }),
  139. package: "@ai-sdk/openai-compatible",
  140. options: {
  141. name: "snowflake-cortex",
  142. baseURL: "https://test.snowflakecomputing.com/api/v2/cortex/v1",
  143. token: "options-token",
  144. },
  145. })
  146. expect(result.sdk).toBeDefined()
  147. }),
  148. ),
  149. )
  150. it.effect("sets includeUsage on the SDK options", () =>
  151. withEnv({ SNOWFLAKE_CORTEX_PAT: "test-pat" }, () =>
  152. Effect.gen(function* () {
  153. const plugin = yield* Plugin.Service
  154. const aisdk = yield* AISDK.Service
  155. yield* addPlugin()
  156. const result = yield* aisdk.runSDK({
  157. model: Model.Info.make({
  158. ...Model.Info.default(Provider.ID.make("snowflake-cortex"), Model.ID.make("claude-sonnet-4-6")),
  159. modelID: Model.ID.make("claude-sonnet-4-6"),
  160. package: "aisdk:test-provider",
  161. }),
  162. package: "@ai-sdk/openai-compatible",
  163. options: { name: "snowflake-cortex", baseURL: "https://test.snowflakecomputing.com/api/v2/cortex/v1" },
  164. })
  165. expect(result.options.includeUsage).toBe(true)
  166. }),
  167. ),
  168. )
  169. })
  170. type FetchLike = (url: string | URL | Request, init?: RequestInit) => Promise<Response>
  171. describe("cortexFetch", () => {
  172. bun_it("rewrites max_tokens to max_completion_tokens", async () => {
  173. const captured: RequestInit[] = []
  174. const upstream: FetchLike = async (_url, init) => {
  175. captured.push(init ?? {})
  176. return new Response("{}", { status: 200 })
  177. }
  178. await cortexFetch(upstream)("https://test", {
  179. method: "POST",
  180. body: JSON.stringify({ model: "claude-sonnet-4-6", max_tokens: 1024 }),
  181. })
  182. const body = JSON.parse(captured[0].body as string)
  183. expect(body.max_completion_tokens).toBe(1024)
  184. expect(body.max_tokens).toBeUndefined()
  185. })
  186. bun_it("preserves body when max_tokens is absent", async () => {
  187. const captured: RequestInit[] = []
  188. const upstream: FetchLike = async (_url, init) => {
  189. captured.push(init ?? {})
  190. return new Response("{}", { status: 200 })
  191. }
  192. const original = JSON.stringify({ model: "claude-sonnet-4-6", temperature: 0.7 })
  193. await cortexFetch(upstream)("https://test", { method: "POST", body: original })
  194. expect(captured[0].body).toBe(original)
  195. })
  196. bun_it("treats 400 'conversation complete' as a stop response", async () => {
  197. const upstream: FetchLike = async () =>
  198. new Response(JSON.stringify({ message: "Conversation complete" }), {
  199. status: 400,
  200. headers: { "content-type": "application/json" },
  201. })
  202. const response = await cortexFetch(upstream)("https://test", {})
  203. expect(response.status).toBe(200)
  204. const data = (await response.json()) as { choices: { finish_reason: string }[] }
  205. expect(data.choices[0].finish_reason).toBe("stop")
  206. })
  207. bun_it("passes through other 400 errors unchanged", async () => {
  208. const upstream: FetchLike = async () =>
  209. new Response(JSON.stringify({ message: "Invalid model" }), {
  210. status: 400,
  211. headers: { "content-type": "application/json" },
  212. })
  213. const response = await cortexFetch(upstream)("https://test", {})
  214. expect(response.status).toBe(400)
  215. })
  216. bun_it("passes through non-400 errors unchanged", async () => {
  217. const upstream: FetchLike = async () => new Response("Unauthorized", { status: 401 })
  218. const response = await cortexFetch(upstream)("https://test", {})
  219. expect(response.status).toBe(401)
  220. })
  221. bun_it("handles invalid JSON body gracefully without throwing", async () => {
  222. const captured: RequestInit[] = []
  223. const upstream: FetchLike = async (_url, init) => {
  224. captured.push(init ?? {})
  225. return new Response("{}", { status: 200 })
  226. }
  227. const invalidBody = "{ not json }"
  228. await cortexFetch(upstream)("https://test", { method: "POST", body: invalidBody })
  229. expect(captured[0].body).toBe(invalidBody)
  230. })
  231. bun_it("rewrites role:'' to role:'assistant' in streaming SSE chunks", async () => {
  232. const chunk = `data: {"choices":[{"delta":{"role":"","content":"Hi"},"index":0}]}\n\n`
  233. const upstream: FetchLike = async () =>
  234. new Response(
  235. new ReadableStream({
  236. start: (ctrl) => {
  237. ctrl.enqueue(new TextEncoder().encode(chunk))
  238. ctrl.close()
  239. },
  240. }),
  241. {
  242. status: 200,
  243. headers: { "content-type": "text/event-stream" },
  244. },
  245. )
  246. const response = await cortexFetch(upstream)("https://test", {})
  247. const text = await response.text()
  248. expect(text).toContain('"role":"assistant"')
  249. expect(text).not.toContain('"role":""')
  250. })
  251. })