|
| 1 | +import { test, expect } from "bun:test" |
| 2 | +import path from "path" |
| 3 | +import { tmpdir } from "../fixture/fixture" |
| 4 | +import { Instance } from "../../src/project/instance" |
| 5 | +import { Provider } from "../../src/provider/provider" |
| 6 | + |
| 7 | +test("provider with queryParams in config", async () => { |
| 8 | + await using tmp = await tmpdir({ |
| 9 | + init: async (dir) => { |
| 10 | + await Bun.write( |
| 11 | + path.join(dir, "opencode.json"), |
| 12 | + JSON.stringify({ |
| 13 | + $schema: "https://opencode.ai/config.json", |
| 14 | + provider: { |
| 15 | + "custom-openai": { |
| 16 | + name: "Custom OpenAI with Query Params", |
| 17 | + npm: "@ai-sdk/openai-compatible", |
| 18 | + env: [], |
| 19 | + models: { |
| 20 | + "gpt-4": { |
| 21 | + name: "GPT-4", |
| 22 | + tool_call: true, |
| 23 | + limit: { context: 128000, output: 4096 }, |
| 24 | + }, |
| 25 | + }, |
| 26 | + options: { |
| 27 | + apiKey: "test-key", |
| 28 | + baseURL: "https://api.example.com/v1", |
| 29 | + queryParams: { |
| 30 | + "api-version": "2024-01-01", |
| 31 | + tenant: "my-org", |
| 32 | + }, |
| 33 | + }, |
| 34 | + }, |
| 35 | + }, |
| 36 | + }), |
| 37 | + ) |
| 38 | + }, |
| 39 | + }) |
| 40 | + await Instance.provide({ |
| 41 | + directory: tmp.path, |
| 42 | + fn: async () => { |
| 43 | + const providers = await Provider.list() |
| 44 | + expect(providers["custom-openai"]).toBeDefined() |
| 45 | + expect(providers["custom-openai"].name).toBe("Custom OpenAI with Query Params") |
| 46 | + expect(providers["custom-openai"].options.queryParams).toEqual({ |
| 47 | + "api-version": "2024-01-01", |
| 48 | + tenant: "my-org", |
| 49 | + }) |
| 50 | + }, |
| 51 | + }) |
| 52 | +}) |
| 53 | + |
| 54 | +test("provider without queryParams still works", async () => { |
| 55 | + await using tmp = await tmpdir({ |
| 56 | + init: async (dir) => { |
| 57 | + await Bun.write( |
| 58 | + path.join(dir, "opencode.json"), |
| 59 | + JSON.stringify({ |
| 60 | + $schema: "https://opencode.ai/config.json", |
| 61 | + provider: { |
| 62 | + "custom-openai": { |
| 63 | + name: "Custom OpenAI", |
| 64 | + npm: "@ai-sdk/openai-compatible", |
| 65 | + env: [], |
| 66 | + models: { |
| 67 | + "gpt-4": { |
| 68 | + name: "GPT-4", |
| 69 | + tool_call: true, |
| 70 | + limit: { context: 128000, output: 4096 }, |
| 71 | + }, |
| 72 | + }, |
| 73 | + options: { |
| 74 | + apiKey: "test-key", |
| 75 | + baseURL: "https://api.example.com/v1", |
| 76 | + }, |
| 77 | + }, |
| 78 | + }, |
| 79 | + }), |
| 80 | + ) |
| 81 | + }, |
| 82 | + }) |
| 83 | + await Instance.provide({ |
| 84 | + directory: tmp.path, |
| 85 | + fn: async () => { |
| 86 | + const providers = await Provider.list() |
| 87 | + expect(providers["custom-openai"]).toBeDefined() |
| 88 | + expect(providers["custom-openai"].name).toBe("Custom OpenAI") |
| 89 | + expect(providers["custom-openai"].options.queryParams).toBeUndefined() |
| 90 | + }, |
| 91 | + }) |
| 92 | +}) |
| 93 | + |
| 94 | +test("provider with empty queryParams", async () => { |
| 95 | + await using tmp = await tmpdir({ |
| 96 | + init: async (dir) => { |
| 97 | + await Bun.write( |
| 98 | + path.join(dir, "opencode.json"), |
| 99 | + JSON.stringify({ |
| 100 | + $schema: "https://opencode.ai/config.json", |
| 101 | + provider: { |
| 102 | + "custom-openai": { |
| 103 | + name: "Custom OpenAI Empty Params", |
| 104 | + npm: "@ai-sdk/openai-compatible", |
| 105 | + env: [], |
| 106 | + models: { |
| 107 | + "gpt-4": { |
| 108 | + name: "GPT-4", |
| 109 | + tool_call: true, |
| 110 | + limit: { context: 128000, output: 4096 }, |
| 111 | + }, |
| 112 | + }, |
| 113 | + options: { |
| 114 | + apiKey: "test-key", |
| 115 | + baseURL: "https://api.example.com/v1", |
| 116 | + queryParams: {}, |
| 117 | + }, |
| 118 | + }, |
| 119 | + }, |
| 120 | + }), |
| 121 | + ) |
| 122 | + }, |
| 123 | + }) |
| 124 | + await Instance.provide({ |
| 125 | + directory: tmp.path, |
| 126 | + fn: async () => { |
| 127 | + const providers = await Provider.list() |
| 128 | + expect(providers["custom-openai"]).toBeDefined() |
| 129 | + expect(providers["custom-openai"].options.queryParams).toEqual({}) |
| 130 | + }, |
| 131 | + }) |
| 132 | +}) |
0 commit comments