Browse Source

fix: Use correct OpenCode v2 plugin structure

- Export { id, effect } object instead of function
- Matches OpenCode v2 Plugin API requirements
Paul Klumpp 1 tuần trước cách đây
mục cha
commit
f1e176d19e
1 tập tin đã thay đổi với 50 bổ sung và 47 xóa
  1. 50 47
      .opencode/plugins/opencode-openai-discovery/index.js

+ 50 - 47
.opencode/plugins/opencode-openai-discovery/index.js

@@ -1,51 +1,54 @@
-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
+module.exports = {
+  id: "opencode-openai-discovery",
+  effect: async (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.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();
+      }
+    });
   }
-  
-  ctx.commands.register({
-    id: "refresh-models",
-    description: "Refresh all discovered models",
-    async run() {
-      ctx.log.info("Refreshing models...");
-      await ctx.provider.reload();
-    }
-  });
 };