Repository navigation
feat(sidecar): add provider manifest client (port #6042) #265
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,87 @@ | ||
| import type { | ||
| ProviderPluginManifest, | ||
| ProviderPluginManifestEntry, | ||
| } from "./providerPluginManifest.ts"; | ||
|
|
||
| export const PROVIDER_PLUGIN_MANIFEST_PATH = "/api/v1/provider-plugin-manifest"; | ||
| export const PROVIDER_PLUGIN_MANIFEST_ENV = "OMNIROUTE_PROVIDER_MANIFEST_URL"; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. CRITICAL: Duplicate export - Reply with |
||
|
|
||
| export interface ProviderPluginManifestClientOptions { | ||
| baseUrl?: string | null; | ||
| manifestUrl?: string | null; | ||
| fetchImpl?: typeof fetch; | ||
| signal?: AbortSignal | null; | ||
| } | ||
|
|
||
| function trimTrailingSlash(value: string): string { | ||
| return value.replace(/\/+$/, ""); | ||
|
Check warning on line 17 in open-sse/config/providerPluginManifestClient.ts
|
||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. CRITICAL: Inconsistent regex behavior - Reply with |
||
| } | ||
|
|
||
| export function resolveProviderPluginManifestUrl( | ||
| options: Pick<ProviderPluginManifestClientOptions, "baseUrl" | "manifestUrl"> = {}, | ||
| ): string { | ||
| const explicitUrl = options.manifestUrl?.trim(); | ||
| if (explicitUrl) return explicitUrl; | ||
|
|
||
| const envUrl = process.env[PROVIDER_PLUGIN_MANIFEST_ENV]?.trim(); | ||
| if (envUrl) return envUrl; | ||
|
|
||
| const baseUrl = options.baseUrl?.trim(); | ||
| if (baseUrl) { | ||
| return `${trimTrailingSlash(baseUrl)}${PROVIDER_PLUGIN_MANIFEST_PATH}`; | ||
| } | ||
|
|
||
| const host = process.env.HOST || "127.0.0.1"; | ||
| const port = process.env.PORT || process.env.DASHBOARD_PORT || process.env.API_PORT || "20128"; | ||
| const protocol = process.env.OMNIROUTE_PUBLIC_PROTOCOL || "http"; | ||
| return `${protocol}://${host}:${port}${PROVIDER_PLUGIN_MANIFEST_PATH}`; | ||
| } | ||
|
|
||
| export async function fetchProviderPluginManifest( | ||
| options: ProviderPluginManifestClientOptions = {}, | ||
| ): Promise<ProviderPluginManifest> { | ||
| const fetcher = options.fetchImpl ?? fetch; | ||
| const url = resolveProviderPluginManifestUrl(options); | ||
| const response = await fetcher(url, { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. WARNING: Missing default timeout - HTTP fetch operations in this codebase typically use Reply with |
||
| headers: { Accept: "application/json" }, | ||
| signal: options.signal ?? undefined, | ||
| }); | ||
|
|
||
| if (!response.ok) { | ||
| throw new Error(`Provider plugin manifest request failed: HTTP ${response.status}`); | ||
| } | ||
|
|
||
| const manifest = (await response.json()) as ProviderPluginManifest; | ||
| if (manifest.schemaVersion !== 1 || !Array.isArray(manifest.providers)) { | ||
| throw new Error("Provider plugin manifest response is not schemaVersion 1"); | ||
| } | ||
|
|
||
| return manifest; | ||
| } | ||
|
|
||
| export function getProviderPluginManifestEntryForModelFromManifest( | ||
| manifest: ProviderPluginManifest, | ||
| model: string | undefined, | ||
| ): ProviderPluginManifestEntry | null { | ||
| if (!model) return null; | ||
|
|
||
| const providerPrefix = model.includes("/") ? model.split("/", 1)[0] : ""; | ||
| if (providerPrefix) { | ||
| const prefixed = manifest.providers.find( | ||
| (provider) => provider.id === providerPrefix || provider.alias === providerPrefix, | ||
| ); | ||
| if (prefixed) return prefixed; | ||
| } | ||
|
|
||
| return manifest.providers.find((provider) => | ||
| provider.models.some((candidate) => candidate.id === model), | ||
| ) ?? null; | ||
| } | ||
|
|
||
| export async function fetchProviderPluginManifestEntryForModel( | ||
| model: string | undefined, | ||
| options: ProviderPluginManifestClientOptions = {}, | ||
| ): Promise<ProviderPluginManifestEntry | null> { | ||
| const manifest = await fetchProviderPluginManifest(options); | ||
| return getProviderPluginManifestEntryForModelFromManifest(manifest, model); | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,150 @@ | ||
| import assert from "node:assert/strict"; | ||
| import test from "node:test"; | ||
|
|
||
| import { | ||
| fetchProviderPluginManifest, | ||
| fetchProviderPluginManifestEntryForModel, | ||
| getProviderPluginManifestEntryForModelFromManifest, | ||
| PROVIDER_PLUGIN_MANIFEST_ENV, | ||
| PROVIDER_PLUGIN_MANIFEST_PATH, | ||
| resolveProviderPluginManifestUrl, | ||
| } from "../../open-sse/config/providerPluginManifestClient.ts"; | ||
| import type { ProviderPluginManifest } from "../../open-sse/config/providerPluginManifest.ts"; | ||
|
|
||
| const ORIGINAL_ENV = { ...process.env }; | ||
|
|
||
| function restoreEnv() { | ||
| process.env = { ...ORIGINAL_ENV }; | ||
| } | ||
|
|
||
| test.afterEach(() => { | ||
| restoreEnv(); | ||
| }); | ||
|
|
||
| test("provider plugin manifest client resolves explicit and env URLs first", () => { | ||
| process.env[PROVIDER_PLUGIN_MANIFEST_ENV] = "http://env.example/manifest"; | ||
|
|
||
| assert.equal( | ||
| resolveProviderPluginManifestUrl({ manifestUrl: "http://explicit.example/manifest" }), | ||
| "http://explicit.example/manifest", | ||
| ); | ||
| assert.equal( | ||
| resolveProviderPluginManifestUrl({ baseUrl: "http://local.example:20128/" }), | ||
| "http://env.example/manifest", | ||
| ); | ||
| }); | ||
|
|
||
| test("provider plugin manifest client resolves base and default local URLs", () => { | ||
| delete process.env[PROVIDER_PLUGIN_MANIFEST_ENV]; | ||
| process.env.HOST = "0.0.0.0"; | ||
| process.env.PORT = "20129"; | ||
|
|
||
| assert.equal( | ||
| resolveProviderPluginManifestUrl({ baseUrl: "http://127.0.0.1:20128/" }), | ||
| `http://127.0.0.1:20128${PROVIDER_PLUGIN_MANIFEST_PATH}`, | ||
| ); | ||
| assert.equal( | ||
| resolveProviderPluginManifestUrl(), | ||
| `http://0.0.0.0:20129${PROVIDER_PLUGIN_MANIFEST_PATH}`, | ||
| ); | ||
| }); | ||
|
|
||
| test("provider plugin manifest client fetches and validates schemaVersion 1", async () => { | ||
| const manifest: ProviderPluginManifest = { | ||
| schemaVersion: 1, | ||
| generatedFrom: "open-sse/config/providers", | ||
| providers: [], | ||
| }; | ||
| const calls: Array<{ url: string; init?: RequestInit }> = []; | ||
| const fetchImpl: typeof fetch = async (url, init) => { | ||
| calls.push({ url: String(url), init }); | ||
| return new Response(JSON.stringify(manifest), { | ||
| status: 200, | ||
| headers: { "Content-Type": "application/json" }, | ||
| }); | ||
| }; | ||
|
|
||
| const result = await fetchProviderPluginManifest({ | ||
| manifestUrl: "http://sidecar.local/manifest", | ||
| fetchImpl, | ||
| }); | ||
|
|
||
| assert.deepEqual(result, manifest); | ||
| assert.equal(calls[0]?.url, "http://sidecar.local/manifest"); | ||
| assert.equal((calls[0]?.init?.headers as Record<string, string>).Accept, "application/json"); | ||
| }); | ||
|
|
||
| test("provider plugin manifest client rejects failed HTTP and malformed responses", async () => { | ||
| await assert.rejects( | ||
| fetchProviderPluginManifest({ | ||
| manifestUrl: "http://sidecar.local/manifest", | ||
| fetchImpl: async () => new Response("nope", { status: 503 }), | ||
| }), | ||
| /HTTP 503/, | ||
| ); | ||
|
|
||
| await assert.rejects( | ||
| fetchProviderPluginManifest({ | ||
| manifestUrl: "http://sidecar.local/manifest", | ||
| fetchImpl: async () => new Response(JSON.stringify({ providers: [] }), { status: 200 }), | ||
| }), | ||
| /schemaVersion 1/, | ||
| ); | ||
| }); | ||
|
|
||
| test("provider plugin manifest client resolves model entries from fetched manifests", async () => { | ||
| const manifest: ProviderPluginManifest = { | ||
| schemaVersion: 1, | ||
| generatedFrom: "open-sse/config/providers", | ||
| providers: [ | ||
| { | ||
| id: "anthropic", | ||
| alias: "claude", | ||
| format: "anthropic", | ||
| executor: "default", | ||
| auth: { type: "apikey", header: "x-api-key" }, | ||
| endpoints: { baseUrl: "https://api.anthropic.com" }, | ||
| capabilities: ["apikey", "sidecar-candidate"], | ||
| passthroughModels: false, | ||
| models: [{ id: "claude-sonnet-4.6", name: "Claude Sonnet 4.6" }], | ||
| sidecar: { eligible: true, reasons: [] }, | ||
| }, | ||
| { | ||
| id: "openai", | ||
| format: "openai", | ||
| executor: "default", | ||
| auth: { type: "apikey", header: "Authorization", prefix: "Bearer" }, | ||
| endpoints: { baseUrl: "https://api.openai.com/v1" }, | ||
| capabilities: ["apikey", "sidecar-candidate"], | ||
| passthroughModels: false, | ||
| models: [{ id: "gpt-4.1", name: "GPT-4.1" }], | ||
| sidecar: { eligible: true, reasons: [] }, | ||
| }, | ||
| ], | ||
| }; | ||
| const fetchImpl: typeof fetch = async () => | ||
| new Response(JSON.stringify(manifest), { | ||
| status: 200, | ||
| headers: { "Content-Type": "application/json" }, | ||
| }); | ||
|
|
||
| assert.equal( | ||
| getProviderPluginManifestEntryForModelFromManifest(manifest, "openai/gpt-4.1")?.id, | ||
| "openai", | ||
| ); | ||
| assert.equal( | ||
| getProviderPluginManifestEntryForModelFromManifest(manifest, "claude/claude-sonnet-4.6")?.id, | ||
| "anthropic", | ||
| ); | ||
| assert.equal( | ||
| getProviderPluginManifestEntryForModelFromManifest(manifest, "gpt-4.1")?.id, | ||
| "openai", | ||
| ); | ||
| assert.equal( | ||
| await fetchProviderPluginManifestEntryForModel("missing-model", { | ||
| manifestUrl: "http://sidecar.local/manifest", | ||
| fetchImpl, | ||
| }), | ||
| null, | ||
| ); | ||
| }); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
CRITICAL: Duplicate export -
PROVIDER_PLUGIN_MANIFEST_PATHis already exported fromproviderPluginManifestUrl.tsline 2. Exporting the same constant from two modules creates ambiguity and potential for divergent values.Reply with
@kilocode-bot fix itto have Kilo Code address this issue.