@zoraxylia/dsh-codex-adapter 0.0.0-stage → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (87) hide show
  1. package/.claude-plugin/marketplace.json +15 -0
  2. package/.claude-plugin/plugin.json +8 -0
  3. package/.mcp.json +8 -0
  4. package/LICENSE +21 -0
  5. package/README.de.md +480 -0
  6. package/README.es.md +480 -0
  7. package/README.fr.md +480 -0
  8. package/README.hi.md +480 -0
  9. package/README.id.md +480 -0
  10. package/README.ja.md +478 -0
  11. package/README.ko.md +480 -0
  12. package/README.md +483 -2
  13. package/README.pt.md +480 -0
  14. package/README.ru.md +480 -0
  15. package/README.th.md +480 -0
  16. package/README.tr.md +480 -0
  17. package/README.vi.md +480 -0
  18. package/README.zh-TW.md +478 -0
  19. package/README.zh.md +482 -0
  20. package/agents/ds-flash.md +17 -0
  21. package/agents/ds-pro.md +17 -0
  22. package/agy/agents/ds-flash.md +18 -0
  23. package/agy/agents/ds-pro.md +18 -0
  24. package/agy/skills/dsh-config/SKILL.md +6 -0
  25. package/agy/skills/dsh-playbook/SKILL.md +65 -0
  26. package/agy/skills/dsh-status/SKILL.md +6 -0
  27. package/codex/agents/ds-flash.toml +23 -0
  28. package/codex/agents/ds-pro.toml +23 -0
  29. package/codex/prompts/dsh-config.md +3 -0
  30. package/codex/prompts/dsh-playbook.md +60 -0
  31. package/codex/prompts/dsh-status.md +1 -0
  32. package/commands/config.md +9 -0
  33. package/commands/off.md +5 -0
  34. package/commands/on.md +5 -0
  35. package/commands/playbook.md +64 -0
  36. package/commands/status.md +5 -0
  37. package/cordis.patch.yml +4 -0
  38. package/docs/images/dsh-crew-host.png +0 -0
  39. package/docs/images/dsh-crew-jobs.png +0 -0
  40. package/docs/images/dsh-crew-logo.png +0 -0
  41. package/docs/images/dsh-crew-overview.png +0 -0
  42. package/grok/agents/ds-flash.md +19 -0
  43. package/grok/agents/ds-pro.md +19 -0
  44. package/grok/commands/dsh-config.md +9 -0
  45. package/grok/commands/dsh-playbook.md +66 -0
  46. package/grok/commands/dsh-status.md +5 -0
  47. package/lib/client.js +2684 -0
  48. package/lib/server.mjs +15417 -0
  49. package/package.json +137 -4
  50. package/scripts/build-client.mjs +23 -0
  51. package/scripts/build-mcp.mjs +43 -0
  52. package/scripts/fake-agy.sh +23 -0
  53. package/scripts/release-local.mjs +87 -0
  54. package/scripts/smoke-bundle.mjs +166 -0
  55. package/scripts/smoke-cwd.mjs +41 -0
  56. package/scripts/smoke-pack.mjs +285 -0
  57. package/scripts/smoke.mjs +79 -0
  58. package/scripts/verify-cli-workers.mjs +133 -0
  59. package/scripts/verify-wpc10-client.mjs +237 -0
  60. package/scripts/verify-wpc14-proc-endpoints.mjs +356 -0
  61. package/scripts/verify-wpc9-guardrails.mjs +432 -0
  62. package/src/cli-workers.mjs +396 -0
  63. package/src/client/index.tsx +1133 -0
  64. package/src/client/jobs-view.ts +374 -0
  65. package/src/cwd-lock.mjs +91 -0
  66. package/src/hub/index.mjs +643 -0
  67. package/src/hub-client.mjs +72 -0
  68. package/src/i18n.mjs +19 -0
  69. package/src/install/cli.mjs +34 -0
  70. package/src/install/install.mjs +577 -0
  71. package/src/jobs.mjs +289 -0
  72. package/src/multimodal.mjs +744 -0
  73. package/src/origin-guard.mjs +133 -0
  74. package/src/paths.mjs +43 -0
  75. package/src/plugin-env.mjs +65 -0
  76. package/src/proc-kill.mjs +208 -0
  77. package/src/process-info.mjs +60 -0
  78. package/src/provider-probe.mjs +35 -0
  79. package/src/server.mjs +438 -0
  80. package/src/status-shard.mjs +95 -0
  81. package/src/tier-binding.mjs +63 -0
  82. package/src/vision-route.mjs +139 -0
  83. package/src/worker-profiles.mjs +95 -0
  84. package/src/worker-question.mjs +22 -0
  85. package/statusline/statusline.sh +14 -0
  86. package/statusline/worker-segment.sh +35 -0
  87. package/worker.cordis.yml +77 -0
@@ -0,0 +1,744 @@
1
+ // Multimodal bridge: gives the (text-only) DeepSeek model eyes and a brush by
2
+ // borrowing the vision / image-generation abilities of locally installed
3
+ // subscription CLIs (claude -p, codex exec, agy).
4
+ //
5
+ // Vision ladder (WPC11): native DeepSeek VL model deepseek-v4-flash-vision-exp
6
+ // (direct API call, key from DEEPSEEK_API_KEY or ~/.config/dsh-crew/.env — the
7
+ // same source the standalone dispatch uses) is preferred whenever a key is
8
+ // available; every failure degrades to the pre-existing CLI provider chain,
9
+ // which is kept byte-for-byte as the fallback. Image generation is untouched:
10
+ // the native model only ever looks at pictures.
11
+ //
12
+ // Tools are plain ToolDefinition-shaped objects (see @deepseek-ai/dsh-tools
13
+ // defineTool output) so the hub keeps its zero-@deepseek-imports realm
14
+ // discipline.
15
+
16
+ import { spawn } from 'node:child_process';
17
+ import { createHash } from 'node:crypto';
18
+ import { readFileSync, writeFileSync, mkdirSync, existsSync, statSync } from 'node:fs';
19
+ import { join, isAbsolute, dirname } from 'node:path';
20
+ import { homedir } from 'node:os';
21
+ import { tr, setLang } from './i18n.mjs';
22
+
23
+ const CONFIG_DIR = join(homedir(), '.config', 'dsh-crew');
24
+ const CACHE_FILE = join(CONFIG_DIR, 'vision-cache.json');
25
+ const CACHE_CAP = 200;
26
+
27
+ // --- native DeepSeek vision backend ---------------------------------------
28
+ // Wire shape mirrors the DSH llm-deepseek adapter (dsh-v0.1.0-rc.8,
29
+ // packages/llm/llm-deepseek/src/serialize.ts): POST {base}/chat/completions
30
+ // with an OpenAI-style image_url content part carrying a base64 data URL.
31
+ const NATIVE_VISION_MODEL = 'deepseek-v4-flash-vision-exp';
32
+ const PUBLIC_DEEPSEEK_BASE = 'https://api.deepseek.com'; // $DEEPSEEK_BASE_URL overrides
33
+ const NATIVE_TIMEOUT_MS = 240_000;
34
+ // Provider ids a config file may name to pin the native path explicitly.
35
+ const NATIVE_VISION_PROVIDER_IDS = new Set(['native', 'deepseek-native']);
36
+
37
+ /**
38
+ * Light .env loader — a copy of jobs.mjs loadDotEnv (duplicated on purpose so
39
+ * the bridge keeps its import-clean, standalone discipline). Plain KEY=VALUE
40
+ * lines only; a missing file yields {}.
41
+ */
42
+ let dotEnv = null;
43
+ function loadDotEnv() {
44
+ if (dotEnv) return dotEnv;
45
+ dotEnv = {};
46
+ try {
47
+ for (const line of readFileSync(join(CONFIG_DIR, '.env'), 'utf8').split('\n')) {
48
+ const m = line.match(/^\s*([A-Z0-9_]+)\s*=\s*(.*)\s*$/);
49
+ if (m && m[2]) dotEnv[m[1]] = m[2];
50
+ }
51
+ } catch {}
52
+ return dotEnv;
53
+ }
54
+
55
+ /** Key for the native call: env wins, then ~/.config/dsh-crew/.env (same
56
+ * precedence the standalone dispatch applies). */
57
+ function deepseekApiKey() {
58
+ return String(process.env.DEEPSEEK_API_KEY ?? '').trim() || String(loadDotEnv().DEEPSEEK_API_KEY ?? '').trim();
59
+ }
60
+
61
+ function deepseekBaseUrl() {
62
+ const base = String(process.env.DEEPSEEK_BASE_URL ?? '').trim().replace(/\/+$/, '');
63
+ return base || PUBLIC_DEEPSEEK_BASE;
64
+ }
65
+
66
+ function run(cmd, args, { timeoutMs, cwd } = {}) {
67
+ // spawn with stdin ignored: several agent CLIs (codex in particular) behave
68
+ // differently when handed an open interactive stdin pipe.
69
+ return new Promise((resolve, reject) => {
70
+ const child = spawn(cmd, args, { cwd, env: process.env, stdio: ['ignore', 'pipe', 'pipe'] });
71
+ let out = '', err = '';
72
+ const timer = setTimeout(() => { child.kill('SIGKILL'); }, timeoutMs ?? 180_000);
73
+ child.stdout.on('data', (c) => { out += c; });
74
+ child.stderr.on('data', (c) => { err += c; });
75
+ child.on('error', (e) => { clearTimeout(timer); reject(new Error(`${cmd} spawn failed: ${e.message}`)); });
76
+ child.on('close', (code, signal) => {
77
+ clearTimeout(timer);
78
+ if (signal) reject(new Error(`${cmd} killed by ${signal} (timeout ${timeoutMs ?? 180_000}ms)\n${err.slice(-400)}`));
79
+ else if (code !== 0) reject(new Error(`${cmd} exited ${code}\n${(err || out).slice(-400)}`));
80
+ else resolve(out + (out === '' ? err : ''));
81
+ });
82
+ });
83
+ }
84
+
85
+ function cacheKey(parts) { return createHash('sha256').update(parts.join('\u0000')).digest('hex'); }
86
+ function readCache() { try { return JSON.parse(readFileSync(CACHE_FILE, 'utf8')); } catch { return {}; } }
87
+ function writeCache(cache) {
88
+ try {
89
+ const keys = Object.keys(cache);
90
+ if (keys.length > CACHE_CAP) for (const k of keys.slice(0, keys.length - CACHE_CAP)) delete cache[k];
91
+ mkdirSync(CONFIG_DIR, { recursive: true });
92
+ writeFileSync(CACHE_FILE, JSON.stringify(cache));
93
+ } catch {}
94
+ }
95
+
96
+ const RESULT_SCHEMA = {
97
+ type: 'object',
98
+ properties: {
99
+ ok: { type: 'boolean' },
100
+ tool: { type: 'string' },
101
+ text: { type: 'string' },
102
+ // Which backend actually answered, plus any native→CLI degradation reason
103
+ // (e.g. "claude-code (native failed: HTTP 404 ...)").
104
+ provider: { type: 'string' },
105
+ },
106
+ required: ['ok', 'tool', 'text'],
107
+ additionalProperties: false,
108
+ };
109
+
110
+ async function describeViaClaude(model, imagePath, question) {
111
+ const prompt = `Use your Read tool to view the image file at ${imagePath}, then answer concisely: ${question}. Reply with the answer only — no preamble.`;
112
+ const { dirname } = await import('node:path');
113
+ // NOTE: --allowedTools is variadic and would swallow a trailing prompt, so
114
+ // the prompt rides directly on -p.
115
+ return (await run('claude', ['-p', prompt, '--model', model, '--allowedTools', 'Read'], {
116
+ timeoutMs: 180_000, cwd: dirname(imagePath),
117
+ })).trim();
118
+ }
119
+
120
+ async function describeViaCodex(imagePath, question, model) {
121
+ const prompt = `Look at the attached image and answer concisely: ${question}. Reply with the answer only.`;
122
+ const modelArgs = model && model !== 'default' ? ['-c', `model="${model}"`] : [];
123
+ const out = await run('codex', ['exec', '--skip-git-repo-check', ...modelArgs, '-i', imagePath, prompt], { timeoutMs: 180_000 });
124
+ const lines = out.trim().split('\n').filter((l) => l.trim() !== '' && !l.startsWith('[20') && !/^(tokens used|codex$|user$|mcp:)/.test(l.trim()));
125
+ return lines.slice(-12).join('\n').trim();
126
+ }
127
+
128
+ async function describeViaGrok(imagePath, question, model) {
129
+ const { dirname, basename } = await import('node:path');
130
+ const prompt = `View the image file at ./${basename(imagePath)} and answer concisely: ${question}. Reply with the answer only — no preamble.`;
131
+ const modelArgs = model && model !== 'default' ? ['-m', model] : [];
132
+ const out = await run('grok', [...modelArgs, '-p', prompt], { timeoutMs: 240_000, cwd: dirname(imagePath) });
133
+ return out.trim();
134
+ }
135
+
136
+ async function describeViaAgy(imagePath, question, model) {
137
+ const prompt = `View the image file at ${imagePath} and answer concisely: ${question}. Reply with the answer only — no preamble.`;
138
+ const modelArgs = model && model !== 'default' ? ['--model', model] : [];
139
+ try {
140
+ return (await run('agy', [...modelArgs, '-p', prompt], { timeoutMs: 240_000 })).trim();
141
+ } catch (err) {
142
+ const msg = String(err?.message ?? err);
143
+ if (/permission/i.test(msg)) {
144
+ throw new Error(tr(
145
+ 'agy 需要 read_file 权限:在 Antigravity 的 settings.json 的 permissions.allow 里加入 read_file 规则,或改用其他视觉 provider。原始错误: ',
146
+ 'agy needs read_file permission: add a read_file rule to permissions.allow in Antigravity\'s settings.json, or pick another vision provider. Original error: ',
147
+ ) + msg.slice(0, 160));
148
+ }
149
+ throw err;
150
+ }
151
+ }
152
+
153
+ /** Shell-quote a value for interpolation into a custom command template. */
154
+ function shq(s) { return "'" + String(s).replace(/'/g, "'\\''") + "'"; }
155
+
156
+ const BUILTIN_VISION = ['claude-code', 'codex', 'grok', 'agy'];
157
+ const BUILTIN_IMAGEGEN = ['codex', 'agy', 'grok'];
158
+
159
+ /** Look up a user-defined provider entry by id. */
160
+ export function findCustomProvider(config, id) {
161
+ return (config?.custom_providers ?? []).find((p) => p && p.id === id);
162
+ }
163
+
164
+ /** 'api' (OpenAI-compatible endpoint) or 'cli' (command template). Entries
165
+ * created before the type field existed carry commands → cli. */
166
+ function entryType(entry) {
167
+ return entry.type ?? ((entry.vision_command || entry.imagegen_command) ? 'cli' : 'api');
168
+ }
169
+
170
+ function extMime(p) {
171
+ const e = p.toLowerCase().split('.').pop();
172
+ return e === 'jpg' || e === 'jpeg' ? 'image/jpeg' : e === 'webp' ? 'image/webp' : e === 'gif' ? 'image/gif' : 'image/png';
173
+ }
174
+
175
+ async function fetchJson(url, { apiKey, body, timeoutMs }) {
176
+ const ctl = new AbortController();
177
+ const t = setTimeout(() => ctl.abort(), timeoutMs);
178
+ try {
179
+ const res = await fetch(url, {
180
+ method: 'POST',
181
+ headers: { 'content-type': 'application/json', ...(apiKey ? { authorization: `Bearer ${apiKey}` } : {}) },
182
+ body: JSON.stringify(body),
183
+ signal: ctl.signal,
184
+ });
185
+ const text = await res.text();
186
+ if (!res.ok) throw new Error(`${url} → HTTP ${res.status}: ${text.slice(0, 300)}`);
187
+ return JSON.parse(text);
188
+ } catch (err) {
189
+ if (err?.name === 'AbortError') throw new Error(tr(`${url} 请求超时 (${timeoutMs}ms)`, `${url} timed out (${timeoutMs}ms)`));
190
+ throw err;
191
+ } finally { clearTimeout(t); }
192
+ }
193
+
194
+ function apiBase(entry) {
195
+ const base = String(entry.base_url ?? '').trim().replace(/\/+$/, '');
196
+ if (!base) throw new Error(tr(`自定义 provider "${entry.name ?? entry.id}" 未配置 Base URL`, `Custom provider "${entry.name ?? entry.id}" has no Base URL`));
197
+ return base;
198
+ }
199
+
200
+ async function describeViaApi(entry, imagePath, question, model) {
201
+ const base = apiBase(entry);
202
+ const m = model && model !== 'default' ? model : entry.models?.[0];
203
+ if (!m) throw new Error(tr(`自定义 provider "${entry.name ?? entry.id}" 未配置模型列表,无法确定视觉模型`, `Custom provider "${entry.name ?? entry.id}" has no models configured`));
204
+ const b64 = readFileSync(imagePath).toString('base64');
205
+ const data = await fetchJson(`${base}/chat/completions`, {
206
+ apiKey: entry.api_key,
207
+ timeoutMs: 240_000,
208
+ body: {
209
+ model: m,
210
+ messages: [{
211
+ role: 'user',
212
+ content: [
213
+ { type: 'text', text: `${question}\nReply with the answer only — no preamble.` },
214
+ { type: 'image_url', image_url: { url: `data:${extMime(imagePath)};base64,${b64}` } },
215
+ ],
216
+ }],
217
+ },
218
+ });
219
+ const text = data?.choices?.[0]?.message?.content;
220
+ if (typeof text !== 'string' || text.trim() === '') throw new Error(tr(`${entry.name ?? entry.id} API 返回为空`, `${entry.name ?? entry.id} API returned an empty answer`));
221
+ return text.trim();
222
+ }
223
+
224
+ async function describeViaNative(imagePath, question) {
225
+ const b64 = readFileSync(imagePath).toString('base64');
226
+ const data = await fetchJson(`${deepseekBaseUrl()}/chat/completions`, {
227
+ apiKey: deepseekApiKey(),
228
+ timeoutMs: NATIVE_TIMEOUT_MS,
229
+ body: {
230
+ model: NATIVE_VISION_MODEL,
231
+ messages: [{
232
+ role: 'user',
233
+ content: [
234
+ { type: 'text', text: `${question}\nReply with the answer only — no preamble.` },
235
+ { type: 'image_url', image_url: { url: `data:${extMime(imagePath)};base64,${b64}` } },
236
+ ],
237
+ }],
238
+ },
239
+ });
240
+ const text = data?.choices?.[0]?.message?.content;
241
+ if (typeof text !== 'string' || text.trim() === '') {
242
+ throw new Error(tr('DeepSeek native 视觉 API 返回为空', 'DeepSeek native vision API returned an empty answer'));
243
+ }
244
+ return text.trim();
245
+ }
246
+
247
+ async function generateViaApi(entry, prompt, outputPath, size) {
248
+ const base = apiBase(entry);
249
+ // No fallback to the vision model list: a VL model would accept the call and
250
+ // fail late (or bill for nothing). Image generation is opt-in per provider.
251
+ const m = String(entry.imagegen_model ?? '').trim();
252
+ if (!m) throw new Error(tr(
253
+ `自定义 provider "${entry.name ?? entry.id}" 未配置生图模型(在设置 → DSH Crew → 自定义 Provider 的"生图模型"里填写)`,
254
+ `Custom provider "${entry.name ?? entry.id}" has no image-gen model (fill in "Image-gen model" under Settings → DSH Crew → Custom providers)`));
255
+ const body = { model: m, prompt, n: 1 };
256
+ if (size && /^\d+x\d+$/.test(size)) body.size = size;
257
+ const data = await fetchJson(`${base}/images/generations`, { apiKey: entry.api_key, timeoutMs: 480_000, body });
258
+ const item = data?.data?.[0];
259
+ let buf;
260
+ if (item?.b64_json) buf = Buffer.from(item.b64_json, 'base64');
261
+ else if (item?.url) {
262
+ const r = await fetch(item.url);
263
+ if (!r.ok) throw new Error(tr(`下载生成图失败: HTTP ${r.status}`, `Failed to download the generated image: HTTP ${r.status}`));
264
+ buf = Buffer.from(await r.arrayBuffer());
265
+ } else {
266
+ throw new Error(tr(
267
+ `${entry.name ?? entry.id} API 未返回图像数据: ${JSON.stringify(data).slice(0, 200)}`,
268
+ `${entry.name ?? entry.id} API returned no image data: ${JSON.stringify(data).slice(0, 200)}`));
269
+ }
270
+ writeFileSync(outputPath, buf);
271
+ return outputPath;
272
+ }
273
+
274
+ async function describeViaCustom(entry, imagePath, question, model) {
275
+ const template = entry.vision_command;
276
+ if (!template || !template.includes('{image}')) {
277
+ throw new Error(tr(
278
+ `自定义 provider "${entry.name ?? entry.id}" 的视觉命令未配置或缺少 {image} 占位符(可用 {image} {question} {model},stdout 即答案)`,
279
+ `Custom provider "${entry.name ?? entry.id}" has no vision command, or it lacks the {image} placeholder (available: {image} {question} {model}; stdout is the answer)`));
280
+ }
281
+ const cmd = template
282
+ .replaceAll('{image}', shq(imagePath))
283
+ .replaceAll('{question}', shq(question))
284
+ .replaceAll('{model}', shq(model ?? 'default'));
285
+ return (await run('/bin/bash', ['-lc', cmd], { timeoutMs: 240_000, cwd: dirname(imagePath) })).trim();
286
+ }
287
+
288
+ async function generateViaCustom(entry, prompt, outputPath, size) {
289
+ const template = entry.imagegen_command;
290
+ if (!template || !template.includes('{prompt}') || !template.includes('{output}')) {
291
+ throw new Error(tr(
292
+ `自定义 provider "${entry.name ?? entry.id}" 的生图命令未配置或缺少 {prompt}/{output} 占位符(可用 {prompt} {output} {size},命令须把图片写到 {output})`,
293
+ `Custom provider "${entry.name ?? entry.id}" has no image-gen command, or it lacks the {prompt}/{output} placeholders (available: {prompt} {output} {size}; the command must write the image to {output})`));
294
+ }
295
+ const cmd = template
296
+ .replaceAll('{prompt}', shq(prompt))
297
+ .replaceAll('{output}', shq(outputPath))
298
+ .replaceAll('{size}', shq(size ?? ''));
299
+ const out = await run('/bin/bash', ['-lc', cmd], { timeoutMs: 480_000, cwd: dirname(outputPath) });
300
+ if (!existsSync(outputPath)) {
301
+ throw new Error(`custom command finished but ${outputPath} was not created. Tail:\n${out.trim().slice(-300)}`);
302
+ }
303
+ return outputPath;
304
+ }
305
+
306
+ async function generateViaCodex(prompt, outputPath, size) {
307
+ // Run codex with cwd = the output directory and a relative save path, so the
308
+ // write lands inside its sandbox's writable root regardless of policy.
309
+ const { dirname, basename } = await import('node:path');
310
+ const task = `$imagegen ${prompt}. Save exactly one image to ./${basename(outputPath)} in the current directory.${size ? ` Target size ${size}.` : ''}`;
311
+ const out = await run('codex', ['exec', '--skip-git-repo-check', task], { timeoutMs: 480_000, cwd: dirname(outputPath) });
312
+ if (!existsSync(outputPath)) {
313
+ throw new Error(`codex finished but ${outputPath} was not created. Tail of output:\n${out.trim().slice(-400)}`);
314
+ }
315
+ return outputPath;
316
+ }
317
+
318
+ async function generateViaGrok(prompt, outputPath) {
319
+ // Grok Build CLI: native generate_image tool, saves straight into cwd.
320
+ const { dirname, basename } = await import('node:path');
321
+ const p = `Use your generate_image tool to create: ${prompt}. Save it to ./${basename(outputPath)} in the current directory (convert to the requested format if needed). Reply with the saved path.`;
322
+ const out = await run('grok', ['-p', p], { timeoutMs: 480_000, cwd: dirname(outputPath) });
323
+ if (!existsSync(outputPath)) {
324
+ throw new Error(`grok finished but ${outputPath} was not created. Tail:\n${out.trim().slice(-300)}`);
325
+ }
326
+ return outputPath;
327
+ }
328
+
329
+ async function generateViaAgy(prompt, outputPath) {
330
+ // Antigravity CLI: native generate_image (Nano Banana) needs no permission
331
+ // bypass, but saves into ~/.gemini/antigravity-cli/{brain,scratch}/... with a
332
+ // sanitized name. A nonce in the requested filename lets us find the output.
333
+ const { randomBytes } = await import('node:crypto');
334
+ const { readdirSync, copyFileSync } = await import('node:fs');
335
+ const nonce = randomBytes(4).toString('hex');
336
+ const p = `Use ONLY your native generate_image tool — do not run shell commands or any other tools. Generate: ${prompt}. Save the file with a name containing ${nonce}. Reply with the saved path.`;
337
+ await run('agy', ['-p', p], { timeoutMs: 480_000 });
338
+ const root = join(homedir(), '.gemini', 'antigravity-cli');
339
+ let fresh = 0;
340
+ let found;
341
+ try {
342
+ for (const f of readdirSync(root, { recursive: true })) {
343
+ const name = String(f);
344
+ if (!/\.(png|jpe?g|webp)$/i.test(name)) continue;
345
+ const p2 = join(root, name);
346
+ const t = statSync(p2).mtimeMs;
347
+ if (Date.now() - t > 8 * 60_000) continue;
348
+ fresh += 1;
349
+ // Only a nonce match is evidence the file answers THIS prompt. Falling
350
+ // back to "the freshest image around" once shipped an unrelated file as
351
+ // a successful generation (PATTERN-AUDIT D1) — an unverified match must
352
+ // not be reported as success.
353
+ if (name.includes(nonce)) { found = p2; break; }
354
+ }
355
+ } catch {}
356
+ if (!found) {
357
+ throw new Error(
358
+ `agy finished but no saved image carries the nonce (${nonce}) under ~/.gemini/antigravity-cli` +
359
+ (fresh > 0 ? ` — ${fresh} fresh image(s) were found but none verifiably answers this prompt; inspect them manually before trusting any` : ''));
360
+ }
361
+ copyFileSync(found, outputPath);
362
+ return outputPath;
363
+ }
364
+
365
+ // --- connectivity test ------------------------------------------------------
366
+
367
+ const CRC_TABLE = (() => {
368
+ const t = new Int32Array(256);
369
+ for (let n = 0; n < 256; n++) {
370
+ let c = n;
371
+ for (let k = 0; k < 8; k++) c = c & 1 ? 0xedb88320 ^ (c >>> 1) : c >>> 1;
372
+ t[n] = c;
373
+ }
374
+ return t;
375
+ })();
376
+ function crc32(buf) {
377
+ let c = -1;
378
+ for (const b of buf) c = CRC_TABLE[(c ^ b) & 0xff] ^ (c >>> 8);
379
+ return (c ^ -1) >>> 0;
380
+ }
381
+ function pngChunk(type, data) {
382
+ const len = Buffer.alloc(4);
383
+ len.writeUInt32BE(data.length);
384
+ const body = Buffer.concat([Buffer.from(type, 'ascii'), data]);
385
+ const crc = Buffer.alloc(4);
386
+ crc.writeUInt32BE(crc32(body));
387
+ return Buffer.concat([len, body, crc]);
388
+ }
389
+ /** A small solid-colour PNG, built in memory so the test needs no fixture file. */
390
+ async function testPng(size = 64, rgb = [220, 40, 40]) {
391
+ const { deflateSync } = await import('node:zlib');
392
+ const ihdr = Buffer.alloc(13);
393
+ ihdr.writeUInt32BE(size, 0);
394
+ ihdr.writeUInt32BE(size, 4);
395
+ ihdr[8] = 8; ihdr[9] = 2; // 8-bit RGB
396
+ const row = Buffer.concat([Buffer.from([0]), Buffer.concat(Array.from({ length: size }, () => Buffer.from(rgb)))]);
397
+ const raw = Buffer.concat(Array.from({ length: size }, () => row));
398
+ return Buffer.concat([
399
+ Buffer.from([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a]),
400
+ pngChunk('IHDR', ihdr),
401
+ pngChunk('IDAT', deflateSync(raw)),
402
+ pngChunk('IEND', Buffer.alloc(0)),
403
+ ]);
404
+ }
405
+
406
+ const TEST_QUESTION = 'What single colour fills this image? Answer with one word.';
407
+
408
+
409
+ /**
410
+ * Probe a custom provider entry (as edited in the panel, saved or not) and
411
+ * report per-step results. Image generation is never actually run — it would
412
+ * cost money and minutes; only reachability and configuration are checked.
413
+ * @returns {Promise<{ok: boolean, steps: Array<{name: string, ok: boolean, detail: string}>}>}
414
+ */
415
+ export async function testProvider(entry, lang) {
416
+ if (lang) setLang(lang);
417
+ const steps = [];
418
+ const push = (name, ok, detail) => { steps.push({ name, ok, detail: String(detail).slice(0, 400) }); return ok; };
419
+ const type = entryType(entry ?? {});
420
+ const tmp = join(CONFIG_DIR, 'test-image.png');
421
+
422
+ if (type === 'api') {
423
+ let base;
424
+ try { base = apiBase(entry); } catch (err) { push(tr('配置', 'Configuration'), false, err.message); return { ok: false, steps }; }
425
+ push(tr('配置', 'Configuration'), true, `${base}${entry.api_key ? tr(' · 带 API Key', ' · with API key') : tr(' · 无 Key(不发送 Authorization)', ' · no key (no Authorization header)')}`);
426
+
427
+ // 1. Reachability + auth, via the optional /models endpoint.
428
+ try {
429
+ const ctl = new AbortController();
430
+ const t = setTimeout(() => ctl.abort(), 20_000);
431
+ const res = await fetch(`${base}/models`, {
432
+ headers: entry.api_key ? { authorization: `Bearer ${entry.api_key}` } : {},
433
+ signal: ctl.signal,
434
+ }).finally(() => clearTimeout(t));
435
+ const text = await res.text();
436
+ if (res.ok) {
437
+ let ids = [];
438
+ try { ids = (JSON.parse(text).data ?? []).map((m) => m.id).filter(Boolean); } catch {}
439
+ const missing = (entry.models ?? []).filter((m) => ids.length > 0 && !ids.includes(m));
440
+ push(tr('连接 /models', 'Reach /models'), true, ids.length
441
+ ? tr(`可达,返回 ${ids.length} 个模型${missing.length ? `;未列出: ${missing.join(', ')}` : ''}`,
442
+ `reachable, ${ids.length} models returned${missing.length ? `; not listed: ${missing.join(', ')}` : ''}`)
443
+ : tr('可达', 'reachable'));
444
+ } else if (res.status === 401 || res.status === 403) {
445
+ push(tr('连接 /models', 'Reach /models'), false, tr(`HTTP ${res.status}:鉴权失败,检查 API Key`, `HTTP ${res.status}: authentication failed, check the API key`));
446
+ return { ok: false, steps };
447
+ } else {
448
+ push(tr('连接 /models', 'Reach /models'), true, tr(`HTTP ${res.status}(该端点可能未实现,继续实测视觉调用)`, `HTTP ${res.status} (endpoint may be unimplemented; continuing with a real vision call)`));
449
+ }
450
+ } catch (err) {
451
+ push(tr('连接 /models', 'Reach /models'), false, err?.name === 'AbortError' ? tr('请求超时 (20s)', 'request timed out (20s)') : String(err.message ?? err));
452
+ return { ok: false, steps };
453
+ }
454
+
455
+ // 2. Real vision round-trip on a tiny generated image.
456
+ const model = entry.models?.[0];
457
+ if (!model) { push(tr('视觉调用', 'Vision call'), false, tr('未配置模型列表', 'no models configured')); return { ok: false, steps }; }
458
+ try {
459
+ mkdirSync(CONFIG_DIR, { recursive: true });
460
+ writeFileSync(tmp, await testPng());
461
+ const answer = await describeViaApi(entry, tmp, TEST_QUESTION, model);
462
+ push(tr('视觉调用', 'Vision call'), true, `${model} → “${answer.slice(0, 60)}”`);
463
+ } catch (err) {
464
+ push(tr('视觉调用', 'Vision call'), false, String(err.message ?? err));
465
+ return { ok: false, steps };
466
+ }
467
+
468
+ if (String(entry.imagegen_model ?? '').trim()) {
469
+ push(tr('生图配置', 'Image-gen setup'), true, tr(
470
+ `模型 ${entry.imagegen_model.trim()}(未实际出图,避免产生费用与等待)`,
471
+ `model ${entry.imagegen_model.trim()} (not actually run — that would cost money and time)`));
472
+ } else {
473
+ push(tr('生图配置', 'Image-gen setup'), true, tr(
474
+ '未配置生图模型 → 该 provider 只提供视觉能力',
475
+ 'no image-gen model → this provider offers vision only'));
476
+ }
477
+ return { ok: true, steps };
478
+ }
479
+
480
+ // CLI: check the executables exist, then actually run the vision command.
481
+ const bin = (cmd) => String(cmd).trim().split(/\s+/)[0];
482
+ if (entry.vision_command) {
483
+ try {
484
+ const which = (await run('/bin/bash', ['-lc', `command -v ${shq(bin(entry.vision_command))}`], { timeoutMs: 15_000 })).trim();
485
+ push(tr('视觉命令可执行', 'Vision command found'), true, which);
486
+ } catch {
487
+ push(tr('视觉命令可执行', 'Vision command found'), false, tr(`找不到可执行文件: ${bin(entry.vision_command)}`, `executable not found: ${bin(entry.vision_command)}`));
488
+ return { ok: false, steps };
489
+ }
490
+ try {
491
+ mkdirSync(CONFIG_DIR, { recursive: true });
492
+ writeFileSync(tmp, await testPng());
493
+ const answer = await describeViaCustom(entry, tmp, TEST_QUESTION, entry.models?.[0] ?? 'default');
494
+ if (answer === '') { push(tr('视觉调用', 'Vision call'), false, tr('命令成功但没有输出', 'the command succeeded but printed nothing')); return { ok: false, steps }; }
495
+ push(tr('视觉调用', 'Vision call'), true, `“${answer.slice(0, 60)}”`);
496
+ } catch (err) {
497
+ push(tr('视觉调用', 'Vision call'), false, String(err.message ?? err));
498
+ return { ok: false, steps };
499
+ }
500
+ }
501
+ if (entry.imagegen_command) {
502
+ try {
503
+ const which = (await run('/bin/bash', ['-lc', `command -v ${shq(bin(entry.imagegen_command))}`], { timeoutMs: 15_000 })).trim();
504
+ push(tr('生图命令可执行', 'Image-gen command found'), true, tr(`${which}(未实际出图,避免长时间等待)`, `${which} (not actually run — that would take minutes)`));
505
+ } catch {
506
+ push(tr('生图命令可执行', 'Image-gen command found'), false, tr(`找不到可执行文件: ${bin(entry.imagegen_command)}`, `executable not found: ${bin(entry.imagegen_command)}`));
507
+ return { ok: false, steps };
508
+ }
509
+ }
510
+ if (steps.length === 0) push(tr('配置', 'Configuration'), false, tr('未填写任何命令', 'no command configured'));
511
+ return { ok: steps.every((s) => s.ok), steps };
512
+ }
513
+
514
+ const modelListCache = new Map(); // provider -> { at, models }
515
+
516
+ /** List selectable models for CLIs that expose a `models` subcommand. */
517
+ export async function listVisionModels(provider, force = false, getConfig = null, lang = null) {
518
+ if (lang) setLang(lang);
519
+ if (!BUILTIN_VISION.includes(provider)) {
520
+ const entry = getConfig ? findCustomProvider(getConfig(), provider) : null;
521
+ const models = (entry?.models ?? []).map((m) => ({ value: String(m) }));
522
+ // API providers must name a real model; only command-based ones have a
523
+ // meaningful "let the CLI decide" option.
524
+ return entryType(entry ?? {}) === 'api' ? models : [{ value: 'default', label: tr('default(CLI 默认)', 'default (CLI default)') }, ...models];
525
+ }
526
+ const cached = modelListCache.get(provider);
527
+ if (!force && cached && Date.now() - cached.at < 60 * 60 * 1000) return cached.models;
528
+ let models = [];
529
+ if (provider === 'agy') {
530
+ const out = await run('agy', ['models'], { timeoutMs: 60_000 });
531
+ models = out.split('\n')
532
+ .map((l) => l.split('\t'))
533
+ .filter((parts) => parts.length >= 2 && /^[a-z0-9.-]+$/i.test(parts[0].trim()))
534
+ .map((parts) => ({ value: parts[0].trim(), label: parts[1].trim() }));
535
+ } else if (provider === 'grok') {
536
+ const out = await run('grok', ['models'], { timeoutMs: 60_000 });
537
+ models = out.split('\n')
538
+ .map((l) => l.match(/^\s*[*-]\s+([a-z0-9.-]+)(\s+\(default\))?/i))
539
+ .filter(Boolean)
540
+ .map((m) => ({ value: m[1], label: m[1] + (m[2] ? tr('(默认)', ' (default)') : '') }));
541
+ }
542
+ const result = [{ value: 'default', label: tr('default(CLI 默认)', 'default (CLI default)') }, ...models];
543
+ modelListCache.set(provider, { at: Date.now(), models: result });
544
+ return result;
545
+ }
546
+
547
+ function envelope(tool, text) { return { ok: true, tool, text }; }
548
+
549
+ const VISION_PROVIDER_HINT = tr(
550
+ `未知视觉 provider: PROVIDER(内置: ${BUILTIN_VISION.join(' / ')} / native,或在设置 → DSH Crew → 自定义 Provider 里添加)`,
551
+ `Unknown vision provider: PROVIDER (built-in: ${BUILTIN_VISION.join(' / ')} / native, or add one under Settings → DSH Crew → Custom providers)`);
552
+
553
+ /**
554
+ * Cache key for one vision answer. The leading 'v3' version tag plus the
555
+ * backend discriminator make native results and CLI results different keys,
556
+ * so a CLI-era entry ('v2' or the CLI backend) can never be served as a
557
+ * native answer (and vice versa).
558
+ */
559
+ function visionCacheKey({ backend, provider, model, imagePath, stat, question, custom, baseUrl }) {
560
+ const parts = ['v3', backend, provider, model, imagePath, String(stat.size), String(stat.mtimeMs), question];
561
+ if (baseUrl) parts.push(baseUrl);
562
+ if (custom) parts.push(entryType(custom), custom.vision_command ?? '', custom.base_url ?? '');
563
+ return cacheKey(parts);
564
+ }
565
+
566
+ /** Pre-existing CLI dispatch, byte-for-byte the old selection ladder. */
567
+ async function describeViaCli(provider, model, custom, imagePath, question) {
568
+ if (custom) {
569
+ return entryType(custom) === 'cli'
570
+ ? describeViaCustom(custom, imagePath, question, model)
571
+ : describeViaApi(custom, imagePath, question, model);
572
+ }
573
+ switch (provider) {
574
+ case 'codex': return describeViaCodex(imagePath, question, model);
575
+ case 'grok': return describeViaGrok(imagePath, question, model);
576
+ case 'agy': return describeViaAgy(imagePath, question, model);
577
+ default: return describeViaClaude(model, imagePath, question);
578
+ }
579
+ }
580
+
581
+ /**
582
+ * Core vision call with provenance, shared by the describe_image tool and the
583
+ * pre-step transcriber.
584
+ *
585
+ * Ladder: native DeepSeek VL model (when DEEPSEEK_API_KEY is available) → the
586
+ * existing CLI chain. The ladder covers the built-in CLI providers; explicit
587
+ * custom providers keep their own configured path untouched. Every degradation
588
+ * (no key, HTTP error, timeout, empty answer) is surfaced in the returned
589
+ * `provider` string and on the console.
590
+ *
591
+ * @returns {Promise<{text: string, provider: string, model: string, cached: boolean}>}
592
+ */
593
+ export async function describeFileWithProvenance(getConfig, imagePath, question) {
594
+ const config = getConfig();
595
+ const configured = config.vision_provider ?? 'claude-code';
596
+ if (configured === 'off') throw new Error('vision bridge is disabled');
597
+ const model = config.vision_model ?? 'haiku';
598
+ const stat = statSync(imagePath);
599
+
600
+ const isNativeId = NATIVE_VISION_PROVIDER_IDS.has(configured);
601
+ const custom = BUILTIN_VISION.includes(configured) ? null : findCustomProvider(config, configured);
602
+ if (!BUILTIN_VISION.includes(configured) && !isNativeId && !custom) {
603
+ throw new Error(VISION_PROVIDER_HINT.replace('PROVIDER', configured));
604
+ }
605
+ // For an explicit 'native' id the CLI degradation target is the default
606
+ // chain entry; for built-in CLI providers it is that provider itself.
607
+ const cliProvider = isNativeId ? 'claude-code' : configured;
608
+ const useNativeLadder = BUILTIN_VISION.includes(configured) || isNativeId;
609
+ const cache = readCache();
610
+
611
+ let nativeIssue = null;
612
+ if (useNativeLadder) {
613
+ const apiKey = deepseekApiKey();
614
+ if (!apiKey) {
615
+ nativeIssue = { skipped: true, reason: tr(
616
+ '缺少 DEEPSEEK_API_KEY(环境变量或 ~/.config/dsh-crew/.env)',
617
+ 'no DEEPSEEK_API_KEY (env or ~/.config/dsh-crew/.env)') };
618
+ } else {
619
+ const key = visionCacheKey({ backend: 'native', provider: 'deepseek', model: NATIVE_VISION_MODEL, imagePath, stat, question, baseUrl: deepseekBaseUrl() });
620
+ if (cache[key]) {
621
+ return { text: cache[key], provider: `native · ${NATIVE_VISION_MODEL} (cached)`, model: NATIVE_VISION_MODEL, cached: true };
622
+ }
623
+ try {
624
+ const started = Date.now();
625
+ const text = await describeViaNative(imagePath, question);
626
+ if (text === '') throw new Error('native returned an empty answer');
627
+ cache[key] = text;
628
+ writeCache(cache);
629
+ console.warn(`dsh-crew: vision answered by native ${NATIVE_VISION_MODEL} in ${Date.now() - started}ms`);
630
+ return { text, provider: `native · ${NATIVE_VISION_MODEL}`, model: NATIVE_VISION_MODEL, cached: false };
631
+ } catch (err) {
632
+ nativeIssue = { skipped: false, reason: String(err?.message ?? err) };
633
+ console.warn(`dsh-crew: native vision failed, falling back to "${cliProvider}": ${nativeIssue.reason}`);
634
+ }
635
+ }
636
+ }
637
+
638
+ // Degradation target: the pre-existing CLI chain, exactly as before.
639
+ const key = visionCacheKey({ backend: 'cli', provider: cliProvider, model, imagePath, stat, question, custom });
640
+ let text = cache[key];
641
+ const cached = Boolean(text);
642
+ if (!text) {
643
+ text = await describeViaCli(cliProvider, model, custom, imagePath, question);
644
+ if (text === '') throw new Error(`${cliProvider} returned an empty answer`);
645
+ cache[key] = text;
646
+ writeCache(cache);
647
+ }
648
+ const provider = nativeIssue
649
+ ? `${cliProvider} (native ${nativeIssue.skipped ? 'skipped' : 'failed'}: ${nativeIssue.reason.slice(0, 200)})`
650
+ : cliProvider;
651
+ return { text, provider, model, cached };
652
+ }
653
+
654
+ /** String-only view of the core call (kept for external callers). */
655
+ export async function describeFile(getConfig, imagePath, question) {
656
+ const { text } = await describeFileWithProvenance(getConfig, imagePath, question);
657
+ return text;
658
+ }
659
+
660
+ /**
661
+ * @param getConfig () => global config (vision_provider, vision_model, imagegen_provider)
662
+ */
663
+ export function createMultimodalTools(getConfig) {
664
+ const describeImage = {
665
+ name: 'describe_image',
666
+ description: 'See an image through a vision-capable model (Claude / GPT / Grok / Gemini / custom CLI) and get an answer about it. Use whenever you need to understand an image file: screenshots, design renders, photos, diagrams. Ask a specific question for the most useful answer. Results are cached per image+question.',
667
+ parameters: {
668
+ type: 'object',
669
+ properties: {
670
+ image_path: { type: 'string', description: 'Absolute path to the image file (png/jpg/webp/gif)' },
671
+ question: { type: 'string', description: 'What you want to know about the image. Default: a detailed description.' },
672
+ },
673
+ required: ['image_path'],
674
+ additionalProperties: false,
675
+ },
676
+ output: {
677
+ schema: RESULT_SCHEMA,
678
+ render: (_args, value) => [{ type: 'text', text: value.text }],
679
+ },
680
+ async execute(args) {
681
+ const config = getConfig();
682
+ const provider = config.vision_provider ?? 'claude-code';
683
+ if (provider === 'off') throw new Error(tr('视觉桥已关闭(设置 → DSH Crew → 视觉 provider)', 'the vision bridge is disabled (Settings → DSH Crew → Vision provider)'));
684
+ const imagePath = String(args.image_path ?? '');
685
+ if (!isAbsolute(imagePath) || !existsSync(imagePath)) throw new Error(`image_path must be an existing absolute path, got: ${imagePath}`);
686
+ const question = typeof args.question === 'string' && args.question !== ''
687
+ ? args.question
688
+ : 'Describe this image in detail: layout, text content, notable elements.';
689
+ const answer = await describeFileWithProvenance(getConfig, imagePath, question);
690
+ // Same convention as generate_image: the provider (and any native→CLI
691
+ // degradation reason) rides the text, plus a dedicated provider field.
692
+ const text = answer.provider ? `${answer.text}\n(provider: ${answer.provider})` : answer.text;
693
+ return { ok: true, tool: 'describe_image', text, provider: answer.provider };
694
+ },
695
+ };
696
+
697
+ const generateImage = {
698
+ name: 'generate_image',
699
+ description: 'Generate a raster image from a text prompt via a subscription image model (codex → gpt-image-2, agy → Nano Banana, grok → Grok Imagine, custom → user-configured CLI). Provide a full visual description and an absolute output path. Note: output is a flat raster (no layers); for layered, editable design output use the OpenPencil pipeline instead.',
700
+ parameters: {
701
+ type: 'object',
702
+ properties: {
703
+ prompt: { type: 'string', description: 'Full visual description: subject, style, colors, composition, constraints' },
704
+ output_path: { type: 'string', description: 'Absolute path to save the image (e.g. /path/to/hero.png)' },
705
+ size: { type: 'string', description: 'Optional size hint, e.g. "1024x1024", "16:9"' },
706
+ provider: { type: 'string', description: 'Override the configured image provider: codex | agy | grok | a custom provider id' },
707
+ },
708
+ required: ['prompt', 'output_path'],
709
+ additionalProperties: false,
710
+ },
711
+ output: {
712
+ schema: RESULT_SCHEMA,
713
+ render: (_args, value) => [{ type: 'text', text: value.text }],
714
+ },
715
+ async execute(args) {
716
+ const config = getConfig();
717
+ const provider = args.provider ?? config.imagegen_provider ?? 'codex';
718
+ if (provider === 'off') throw new Error(tr('生图已关闭(设置 → DSH Crew → 生图 provider)', 'image generation is disabled (Settings → DSH Crew → Image-gen provider)'));
719
+ const outputPath = String(args.output_path ?? '');
720
+ if (!isAbsolute(outputPath)) throw new Error(`output_path must be absolute, got: ${outputPath}`);
721
+ const prompt = String(args.prompt ?? '').trim();
722
+ if (prompt === '') throw new Error('prompt is required');
723
+ const custom = BUILTIN_IMAGEGEN.includes(provider) ? null : findCustomProvider(config, provider);
724
+ if (!BUILTIN_IMAGEGEN.includes(provider) && !custom) {
725
+ throw new Error(tr(
726
+ `未知生图 provider: ${provider}(内置: ${BUILTIN_IMAGEGEN.join(' / ')},或在设置 → DSH Crew → 自定义 Provider 里添加)`,
727
+ `Unknown image-gen provider: ${provider} (built-in: ${BUILTIN_IMAGEGEN.join(' / ')}, or add one under Settings → DSH Crew → Custom providers)`));
728
+ }
729
+ const saved = custom
730
+ ? (entryType(custom) === 'cli'
731
+ ? await generateViaCustom(custom, prompt, outputPath, args.size)
732
+ : await generateViaApi(custom, prompt, outputPath, args.size))
733
+ : provider === 'agy'
734
+ ? await generateViaAgy(prompt, outputPath)
735
+ : provider === 'grok'
736
+ ? await generateViaGrok(prompt, outputPath)
737
+ : await generateViaCodex(prompt, outputPath, args.size);
738
+ const bytes = statSync(saved).size;
739
+ return envelope('generate_image', `image saved: ${saved} (${Math.round(bytes / 1024)} KB, provider: ${provider})`);
740
+ },
741
+ };
742
+
743
+ return [describeImage, generateImage];
744
+ }