-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathopencode-vision-plugin.js
More file actions
333 lines (325 loc) · 16.4 KB
/
Copy pathopencode-vision-plugin.js
File metadata and controls
333 lines (325 loc) · 16.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
// vision.ts
var vision_exports = {};
__export(vision_exports, {
default: () => vision_default
});
module.exports = __toCommonJS(vision_exports);
var import_plugin = require("@opencode-ai/plugin");
var import_promises = require("node:fs/promises");
var import_node_path = require("node:path");
var isOk = (r) => r.ok;
var DEFAULT_MODELS = {
gemini: "gemini-3.6-flash",
nim: "meta/llama-3.2-90b-vision-instruct"
};
var DUAL_MODELS = [
["meta/llama-3.2-90b-vision-instruct", "structured overall description"],
["nvidia/nemotron-nano-12b-v2-vl", "sharp low-level visual detail"]
];
function defaultConfig() {
return {
provider: "chain",
chain: ["gemini", "nim"],
models: { ...DEFAULT_MODELS },
keys: {},
baseUrls: {
nim: "https://integrate.api.nvidia.com/v1",
gemini: "https://generativelanguage.googleapis.com/v1beta"
},
timeoutMs: 6e4
};
}
var ENV_RE = /\{env:([A-Za-z_][A-Za-z0-9_]*)\}/g;
function interp(s) {
return s.replace(ENV_RE, (_match, name) => process.env[name] ?? "");
}
function deepInterp(v) {
if (typeof v === "string") return interp(v);
if (Array.isArray(v)) return v.map(deepInterp);
if (v && typeof v === "object") {
const out = {};
for (const [key, value] of Object.entries(v)) out[key] = deepInterp(value);
return out;
}
return v;
}
function entries(v) {
return v && typeof v === "object" ? Object.entries(v) : [];
}
function loadConfig(raw) {
const cfg = defaultConfig();
const user = deepInterp(raw ?? {});
if (typeof user.provider === "string") cfg.provider = user.provider;
if (Array.isArray(user.chain)) cfg.chain = user.chain.filter((x) => x === "gemini" || x === "nim");
for (const [key, value] of entries(user.models)) if (typeof value === "string") cfg.models[key] = value;
for (const [key, value] of entries(user.keys)) if (typeof value === "string") cfg.keys[key] = value;
for (const [key, value] of entries(user.baseUrls)) if (typeof value === "string") cfg.baseUrls[key] = value;
if (typeof user.timeout_ms === "number") cfg.timeoutMs = user.timeout_ms;
if (typeof user.timeout === "number") cfg.timeoutMs = user.timeout;
if (!cfg.keys.gemini && process.env.GEMINI_API_KEY) cfg.keys.gemini = process.env.GEMINI_API_KEY;
if (!cfg.keys.gemini && process.env.GOOGLE_API_KEY) cfg.keys.gemini = process.env.GOOGLE_API_KEY;
if (!cfg.keys.nim && process.env.NVIDIA_API_KEY) cfg.keys.nim = process.env.NVIDIA_API_KEY;
return cfg;
}
async function resolveImage(ref) {
if (ref.startsWith("data:image/") && ref.includes(";base64,")) {
const [head, b64] = ref.split(",", 2);
const mime = /image\/[a-z0-9.+-]+/i.exec(head)?.[0] ?? "image/png";
return { data: b64, mime };
}
if (/^https?:\/\//i.test(ref)) {
const response = await fetch(ref);
if (!response.ok) throw new Error(`Failed to fetch image URL: HTTP ${response.status}`);
const buf2 = new Uint8Array(await response.arrayBuffer());
const mime = response.headers.get("content-type")?.split(";")[0] || "image/png";
return { data: Buffer.from(buf2).toString("base64"), mime };
}
const buf = await (0, import_promises.readFile)((0, import_node_path.resolve)(ref));
const MIME = {
".png": "image/png",
".jpg": "image/jpeg",
".jpeg": "image/jpeg",
".gif": "image/gif",
".webp": "image/webp",
".bmp": "image/bmp",
".tiff": "image/tiff",
".tif": "image/tiff",
".avif": "image/avif",
".heic": "image/heic",
".heif": "image/heif"
};
const ext = (0, import_node_path.extname)(ref).toLowerCase();
return { data: Buffer.from(buf).toString("base64"), mime: MIME[ext] ?? "image/png" };
}
var DESCRIBE_PROMPT = `Describe this image comprehensively and objectively. Include:
1. Main subject and composition
2. Colors, lighting, visual style
3. ALL visible text (transcribed exactly, preserve original language)
4. People, objects, environment
5. Context and purpose (UI screenshot, photo, diagram, document, etc.)
6. Technical quality and notable visual elements
Be precise. Do not speculate beyond what is visible.`;
var BLIND_READER_CLAUSE = `
CRITICAL: the reader cannot see this image at all. Treat this description as the
reader's ONLY source of visual truth, so be exhaustive and explicitly spatial:
- Describe layout, arrangement, and relative position (top/bottom/left/right/center,
foreground/background, above/below/next to).
- Name every distinct element, its approximate size, and its function.
- For screenshots/UI/diagrams: describe structure, grouping, alignment behavior,
what is interactive, and read off all visible text verbatim.
- Use precise, concrete words. Avoid "you can see" / "as shown" assumptions; the
reader cannot see \u2014 leave nothing implied.`;
var OCR_PROMPT = `Extract ALL text from this image EXACTLY as it appears.
Preserve the original language, capitalization, line breaks, and formatting.
Return ONLY the extracted text with no commentary.
If there is no readable text, say "[No text detected]".`;
var BLIND_CALIBRATION = `
CALIBRATION \u2014 follow the framework sequence for the matching category. The good
example is the target style (precise, measured, spatial); the bad example is the
style to avoid.
1. Data & Technical Visuals (graphs, charts, diagrams)
Framework: Identity \u2192 Axis/Scale \u2192 Trend Line \u2192 Key Anomalies
Bad: "This is a line graph showing sales going up and down over the last year, with a huge drop in the middle."
Good: "A 2D line graph tracking monthly revenue across 12 months. The horizontal X-axis plots time from January to December. The vertical Y-axis measures revenue from zero to one hundred thousand dollars. The data line rises steadily from forty thousand in January to a peak of ninety thousand in June, plunges sharply to ten thousand in July, and recovers to eighty thousand by December."
2. Physical Spatial Environments (real-world scenes, landscapes, labs)
Framework: Z-Axis Depth (Foreground to Background) \u2192 Clock-Face Anchors \u2192 Textures/Lighting
Bad: "A crowded, messy workshop filled with dangerous tools and electronics scattered all over the place."
Good: "A brightly lit, five-meter-wide electronics laboratory. In the foreground at 6 o'clock sits a wooden workbench littered with copper wires, a silver soldering iron, and green circuit boards. In the midground at 3 o'clock, a tall steel rack houses stacked digital oscilloscopes with glowing green grid screens. In the background, a concrete wall features a wide glass window revealing an outdoor courtyard."
3. Abstract Concepts & Human Elements (art, infographics, behavior)
Framework: Core Subject \u2192 Micro-Details/Geometry \u2192 Objective Action \u2192 Color/Composition
Bad: "An inspirational infographic about success showing a businessman looking proud after finally reaching his goal."
Good: "A minimalist vector infographic on a solid white background. The central graphic is a black, stepped pyramid with five distinct levels. A stylized human silhouette clad in a sharp charcoal suit stands atop the highest peak. The silhouette holds a bright yellow, triangular flag pointing upward to the right. Crisp, black sans-serif text labels sit horizontally beneath each step of the pyramid."`;
function describePrompt(blind, userPrompt) {
if (userPrompt) return userPrompt;
return blind ? `${DESCRIBE_PROMPT}
${BLIND_READER_CLAUSE}
${BLIND_CALIBRATION}` : DESCRIBE_PROMPT;
}
function ownerOf(model) {
return model.startsWith("gemini") ? "gemini" : "nim";
}
async function providerCall(cfg, provider, model, prompt, img, timeoutMs) {
return provider === "gemini" ? geminiCall(cfg, model, prompt, img, timeoutMs) : nimCall(cfg, model, prompt, img, timeoutMs);
}
async function runWithChain(cfg, model, prompt, img) {
const providers = model ? [ownerOf(model)] : cfg.chain;
const perProvider = Math.max(2e3, Math.floor(cfg.timeoutMs / providers.length));
let last = { ok: false, error: "no provider available" };
for (const provider of providers) {
const chosen = model ?? cfg.models[provider] ?? DEFAULT_MODELS[provider];
const r = await providerCall(cfg, provider, chosen, prompt, img, perProvider);
if (isOk(r)) return r;
last = r;
}
return last;
}
async function dualDescribe(cfg, img, blind, userPrompt) {
const prompt = describePrompt(blind, userPrompt);
const sections = [];
let anyOk = false;
for (const [model, label] of DUAL_MODELS) {
const r = await nimCall(cfg, model, prompt, img, cfg.timeoutMs);
if (isOk(r)) anyOk = true;
sections.push(`\u2550\u2550\u2550 ${label} \u2014 ${model} \u2550\u2550\u2550
${isOk(r) ? r.text : "\u26A0\uFE0F FAILED: " + r.error}`);
}
return anyOk ? { ok: true, text: sections.join("\n\n").trim() } : { ok: false, error: "Dual NIM describe failed" };
}
async function nimCall(cfg, model, prompt, img, timeoutMs) {
const key = cfg.keys.nim;
if (!key) return { ok: false, error: "NVIDIA_API_KEY missing (config keys.nim)" };
const body = {
model,
messages: [{
role: "user",
content: [
{ type: "text", text: prompt },
{ type: "image_url", image_url: { url: `data:${img.mime};base64,${img.data}` } }
]
}],
max_tokens: 1024,
temperature: 0.2
};
try {
const response = await fetch(`${cfg.baseUrls.nim}/chat/completions`, {
method: "POST",
headers: { "Content-Type": "application/json", Authorization: `Bearer ${key}` },
body: JSON.stringify(body),
signal: AbortSignal.timeout(timeoutMs)
});
if (!response.ok) return { ok: false, error: `NIM HTTP ${response.status}: ${(await response.text()).slice(0, 300)}` };
const json = await response.json();
const text = json.choices?.[0]?.message?.content;
return text ? { ok: true, text: text.trim() } : { ok: false, error: "[EMPTY] no content from NIM" };
} catch (e) {
return { ok: false, error: `NIM fetch failed: ${e instanceof Error ? e.message : String(e)}` };
}
}
async function geminiCall(cfg, model, prompt, img, timeoutMs) {
const key = cfg.keys.gemini;
if (!key) return { ok: false, error: "Gemini key missing (config keys.gemini)" };
const body = {
contents: [{
parts: [
{ text: prompt },
{ inline_data: { mime_type: img.mime, data: img.data } }
]
}],
generationConfig: { temperature: 0.2, maxOutputTokens: 8192 }
};
try {
const response = await fetch(`${cfg.baseUrls.gemini}/models/${model}:generateContent?key=${key}`, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(body),
signal: AbortSignal.timeout(timeoutMs)
});
if (!response.ok) return { ok: false, error: `Gemini HTTP ${response.status}: ${(await response.text()).slice(0, 300)}` };
const json = await response.json();
const parts = json.candidates?.[0]?.content?.parts;
const text = Array.isArray(parts) ? parts.map((p) => p?.text ?? "").join("") : "";
return text ? { ok: true, text: text.trim() } : { ok: false, error: "[Gemini] no text in response" };
} catch (e) {
return { ok: false, error: `Gemini fetch failed: ${e instanceof Error ? e.message : String(e)}` };
}
}
var plugin = async (_input, pluginOptions) => {
const cfg = loadConfig(pluginOptions);
return {
tool: {
describe: (0, import_plugin.tool)({
description: `Vision describe: composition, layout, colors, objects, ALL visible text, context. blind (REQUIRED): true = reader cannot see the image \u2014 exhaustive spatial description; false = normal. DEFAULT (omit model) = DUAL mode: two NIM VLMs \u2014 structured overview + fine detail \u2014 labelled sections, best fidelity; prefer it. Single view ONLY for one-offs: "meta/llama-3.2-90b-vision-instruct" (overview), "nvidia/nemotron-nano-12b-v2-vl" (sharp detail), "meta/llama-3.2-11b-vision-instruct" (light), "gemini-3.6-flash". Flaky models \u2014 call independently.`,
args: {
image_path: import_plugin.tool.schema.string(),
blind: import_plugin.tool.schema.boolean(),
prompt: import_plugin.tool.schema.optional(import_plugin.tool.schema.string()),
model: import_plugin.tool.schema.optional(import_plugin.tool.schema.string())
},
async execute(args) {
if (!args.image_path || typeof args.image_path !== "string") return "Missing required: image_path";
if (typeof args.blind !== "boolean") return "Missing required: blind (true/false)";
const prompt = args.prompt ?? "";
const model = args.model ?? void 0;
try {
const img = await resolveImage(args.image_path);
if (model) {
const r = await runWithChain(cfg, model, describePrompt(args.blind, prompt), img);
return isOk(r) ? r.text : r.error;
}
const d = await dualDescribe(cfg, img, args.blind, prompt);
return isOk(d) ? d.text : d.error;
} catch (e) {
return `Error: ${e instanceof Error ? e.message : String(e)}`;
}
}
}),
ocr: (0, import_plugin.tool)({
description: `OCR: extract ALL visible text verbatim \u2014 screenshots, UI, documents, receipts, diagrams, photos, memes, whiteboards. Returns only the extracted text. blind (REQUIRED): true = reader cannot see the image. Use for anything text-bearing; never guess text from context.`,
args: {
image_path: import_plugin.tool.schema.string(),
blind: import_plugin.tool.schema.boolean(),
model: import_plugin.tool.schema.optional(import_plugin.tool.schema.string())
},
async execute(args) {
if (!args.image_path || typeof args.image_path !== "string") return "Missing required: image_path";
if (typeof args.blind !== "boolean") return "Missing required: blind (true/false)";
try {
const img = await resolveImage(args.image_path);
const r = await runWithChain(cfg, args.model ?? void 0, OCR_PROMPT, img);
return isOk(r) ? r.text : r.error;
} catch (e) {
return `Error: ${e instanceof Error ? e.message : String(e)}`;
}
}
}),
analyze: (0, import_plugin.tool)({
description: `Full vision analysis: source metadata + DUAL description + OCR in one call \u2014 screenshot/UI/document forensics, audits, evidence extraction, comprehensive understanding. Slower (multi-call); prefer describe for quick questions. blind (REQUIRED): true = reader cannot see the image \u2014 exhaustive spatial description. Omit model = DUAL mode.`,
args: {
image_path: import_plugin.tool.schema.string(),
blind: import_plugin.tool.schema.boolean(),
model: import_plugin.tool.schema.optional(import_plugin.tool.schema.string())
},
async execute(args) {
if (!args.image_path || typeof args.image_path !== "string") return "Missing required: image_path";
if (typeof args.blind !== "boolean") return "Missing required: blind (true/false)";
try {
const img = await resolveImage(args.image_path);
const desc = args.model ? await runWithChain(cfg, args.model, describePrompt(args.blind, ""), img) : await dualDescribe(cfg, img, args.blind, "");
const ocr = await runWithChain(cfg, void 0, OCR_PROMPT, img);
const meta = args.image_path.startsWith("data:") ? "data: URL" : `path: ${args.image_path}`;
return [
`\u{1F4D0} SOURCE`,
meta,
`
\u{1F5BC}\uFE0F VISUAL DESCRIPTION
${isOk(desc) ? desc.text : "\u26A0\uFE0F " + desc.error}`,
`
\u{1F4C4} TEXT CONTENT
${isOk(ocr) ? ocr.text : "\u26A0\uFE0F " + ocr.error}`
].join("\n");
} catch (e) {
return `Error: ${e instanceof Error ? e.message : String(e)}`;
}
}
})
}
};
};
var vision_default = plugin;