| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051 |
- module.exports = async function(ctx) {
- const endpoints = (ctx.config || {}).endpoints || [];
-
- if (!Array.isArray(endpoints)) {
- throw new Error("Plugin config requires 'endpoints' array");
- }
-
- for (const ep of endpoints) {
- try {
- const headers = { "Content-Type": "application/json" };
- if (ep.apiKey) headers["Authorization"] = `Bearer ${ep.apiKey}`;
-
- const res = await fetch(`${ep.baseURL}/models`, { headers });
- if (!res.ok) throw new Error(`HTTP ${res.status}`);
-
- const json = await res.json();
- const models = (json.data || []).map(m => ({
- id: m.id,
- name: m.id,
- capabilities: { input: ["text"], output: "text" },
- limits: { context: m.context_length || 128000, output: m.max_tokens || 8192 }
- }));
-
- await ctx.provider.transform(editor => {
- editor.add({
- info: {
- id: ep.name,
- name: ep.name,
- activation: "enabled",
- package: "@opencode/ai/providers/openai-compatible",
- settings: { baseURL: ep.baseURL }
- },
- models
- });
- });
-
- ctx.log.info(`Discovered ${models.length} models from ${ep.name}`);
- } catch(e) {
- ctx.log.error(`Failed to discover from ${ep.name}: ${e.message}`);
- }
- }
-
- ctx.commands.register({
- id: "refresh-models",
- description: "Refresh all discovered models",
- async run() {
- ctx.log.info("Refreshing models...");
- await ctx.provider.reload();
- }
- });
- };
|