| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091 |
- /**
- * OpenCode v2 Plugin: Auto-discover models from OpenAI-compatible endpoints.
- *
- * Reads endpoint/key from opencode.jsonc (project or global) when available,
- * falling back to the built-in defaults. Creates the "OmniRoute" provider itself.
- */
- import { Plugin, Model } from "@opencode/plugin";
- function parseJsonc(text: string): any {
- try { return JSON.parse(text); } catch {}
- let s = text.replace(/\/\/.*$/gm, "").replace(/\/\*[\s\S]*?\*\//g, "");
- s = s.replace(/,\s*([}\]])/g, "$1");
- return JSON.parse(s);
- }
- function readConfig(): { baseURL: string; apiKey: string } {
- const fs = require("node:fs");
- const path = require("node:path");
- const os = require("node:os");
- const candidates = [
- path.join(process.cwd(), "opencode.jsonc"),
- path.join(process.cwd(), "opencode.json"),
- path.join(os.homedir(), ".config", "opencode", "opencode.jsonc"),
- ];
- for (const c of candidates) {
- try {
- if (!fs.existsSync(c)) continue;
- const cfg = parseJsonc(fs.readFileSync(c, "utf8"));
- const provs = cfg?.providers || cfg?.provider;
- if (!provs || typeof provs !== "object") continue;
- for (const info of Object.values(provs) as any[]) {
- const p = info as Record<string, unknown>;
- if (typeof p.package === "string" && p.package.includes("openai-compatible")) {
- const settings = (p.settings && typeof p.settings === "object") ? (p.settings as Record<string, unknown>) : {};
- const baseURL = typeof settings.baseURL === "string" ? settings.baseURL : undefined;
- const apiKey = typeof settings.apiKey === "string" ? settings.apiKey : "";
- if (baseURL) return { baseURL, apiKey };
- }
- }
- } catch {}
- }
- return { baseURL: "http://localhost:20128/v1", apiKey: "sk-e9ac58c0b78cd2bc-f17d3e-f6beea67" };
- }
- const { baseURL: BASE_URL, apiKey: API_KEY } = readConfig();
- const PROVIDER_ID = "OmniRoute";
- const PROVIDER_NAME = "OmniRoute";
- export default Plugin.define({
- id: "opencode.openai-compatible-model-discovery",
- async setup(ctx) {
- try {
- const headers: Record<string, string> = { "Content-Type": "application/json" };
- if (API_KEY) headers["Authorization"] = `Bearer ${API_KEY}`;
- const res = await fetch(`${BASE_URL}/models`, { headers });
- if (!res.ok) throw new Error(`HTTP ${res.status} from ${BASE_URL}/models`);
- const json = await res.json();
- const rawModels = (json.data || []) as Array<Record<string, unknown>>;
- const models = rawModels.map((m) => {
- const modelId = typeof m.id === "string" ? m.id : String(m.id);
- const contextLength = typeof m.context_length === "number" ? m.context_length : 131072;
- const maxTokens = typeof m.max_tokens === "number" ? m.max_tokens : 32768;
- const inputMods = Array.isArray(m.input_modalities) ? (m.input_modalities as string[]) : ["text"];
- const outputMods = Array.isArray(m.output_modalities) ? (m.output_modalities as string[]) : ["text"];
- return {
- id: modelId, modelID: modelId, providerID: PROVIDER_ID, name: modelId,
- capabilities: { tools: true, input: inputMods, output: outputMods },
- variants: [], cost: [{ input: 0, output: 0, cache: { read: 0, write: 0 } }],
- limit: { context: contextLength, output: Math.min(maxTokens, contextLength) },
- time: { released: Math.floor(Date.now() / 1000) },
- status: "active" as const, enabled: true,
- } as unknown as Model.Info;
- });
- ctx.provider.transform((editor) => {
- const source = editor.get(PROVIDER_ID);
- if (source) {
- const existing = [...source.models.values()];
- const existingIDs = new Set(existing.map((model) => String(model.id)));
- const merged = [...existing, ...models.filter((m) => !existingIDs.has(String(m.id)))];
- editor.models.set(PROVIDER_ID, merged);
- } else {
- editor.add({ info: { id: PROVIDER_ID, name: PROVIDER_NAME, activation: "enabled" as const, package: "aisdk:@ai-sdk/openai-compatible", settings: { baseURL: BASE_URL, apiKey: API_KEY } } as never, models });
- }
- });
- console.log(`[openai-discovery] Registered ${models.length} models in provider "${PROVIDER_ID}"`);
- } catch (error) {
- console.error(`[openai-discovery] Failed to discover models: ${error instanceof Error ? error.message : String(error)}`);
- }
- },
- });
|