| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108 |
- /**
- * OpenCode v2 Plugin: Auto-discover models from OpenAI-compatible endpoints.
- *
- * Queries GET /v1/models and registers all discovered models under the
- * configured provider. If the provider does not exist yet, it is created.
- */
- import { Plugin, Model } from "@opencode/plugin";
- // Configuration
- const PROVIDER_ID = "OmniRoute";
- const PROVIDER_NAME = "OmniRoute";
- const BASE_URL = "http://localhost:20128/v1";
- const API_KEY = "sk-e9ac58c0b78cd2bc-f17d3e-f6beea67";
- export default Plugin.define({
- id: "opencode.openai-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) {
- // Merge discovered models into the existing provider
- 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 {
- // Create the provider with all discovered models
- 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)}`
- );
- }
- },
- });
|