warden-agent 0.4.1 → 0.5.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.
package/README.md CHANGED
@@ -84,6 +84,8 @@ Inside the app:
84
84
  | `/add` | add a model from any source (see above) |
85
85
  | `/model` | pick from your saved models by number; `/model <name>` or `/model provider/model` switches directly (becomes your default) |
86
86
  | `/models openrouter nemotron --free` | browse and search a source's models |
87
+ | `/image <description>` | make a picture with your image model, saved in `images/`; `--wide`, `--tall` or `--square` (see "Making images") |
88
+ | `/image-model` | pick the image model `/image` uses |
87
89
  | `/providers` | which sources have a key |
88
90
  | `/remove <name>` | remove a saved model; `/remove <source>` forgets that source's saved key |
89
91
  | `/mode` | `ask` (approve edits and commands), `auto-edit` (edits in this folder run without asking), `yolo` (everything runs without asking). Shift+Tab cycles. |
@@ -106,6 +108,28 @@ warden -p "summarise this repo" # answer once and exit (scripts); --j
106
108
 
107
109
  If a `WARDEN.md`, `AGENTS.md` or `CLAUDE.md` exists in the folder, its contents are given to the model as project instructions.
108
110
 
111
+ ## Making images
112
+
113
+ Warden can make pictures for your project with a separate **image model**. Your coding model stays as it is.
114
+
115
+ 1. Type `/add`, pick **OpenRouter**, and press Enter to keep your key.
116
+ 2. At Search, type `recraft` and pick `recraft/recraft-v4.1-flash-v2:free`. Warden sees it is an image model, can make one small test image, and saves it as your image model.
117
+ 3. Make images:
118
+
119
+ ```
120
+ /image a black sport bike on a wet road at night, red light --wide
121
+ /image round logo with a red helmet on black --square
122
+ /image tall poster of a mountain road at sunrise --tall
123
+ ```
124
+
125
+ Each image is saved in `images/` in your project folder, named after the description. Nothing is overwritten. Then tell the model what to do with it, for example `use images/black-sport-bike-on-wet-road.webp as the hero image`. The extension is whatever the model sends: Recraft sends WebP (`.webp`), others may send `.png` or `.jpg`; every browser shows all of them. `/image-model` switches between saved image models. Esc cancels a running `/image`.
126
+
127
+ Good to know:
128
+
129
+ - Free image models are rare and come and go. `recraft-v4.1-flash-v2:free` is scheduled to go away on 28 November 2026. When a model disappears, `/image` says so; add another with `/add`.
130
+ - Google's image models (Nano Banana) work with `/image` too, but need billing turned on. A free Gemini key cannot make images.
131
+ - The coding model never makes images by itself. Only `/image` does, so your image quota is only used when you ask.
132
+
109
133
  ## Tools the model can use
110
134
 
111
135
  `read_file`, `write_file` (with `append` for writing big files in parts), `edit_file` (exact text replacement), `list_dir`, `glob`, `grep`, `bash`, and `job`. On Windows, `bash` uses Git Bash if installed, otherwise PowerShell.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "warden-agent",
3
- "version": "0.4.1",
3
+ "version": "0.5.0",
4
4
  "description": "A terminal coding agent for any model (Gemini, OpenRouter, Groq, Ollama, OpenAI, Anthropic), plus tools to review and compare coding agents.",
5
5
  "keywords": [
6
6
  "ai",
@@ -36,7 +36,7 @@
36
36
  "scripts": {
37
37
  "daemon": "node src/cli.mjs daemon",
38
38
  "watch": "node src/cli.mjs watch",
39
- "test": "node test/meta.mock.mjs && node test/agent.mock.mjs && node test/tui.mock.mjs && node test/models.mock.mjs && node test/providers.mock.mjs && node test/arena.mock.mjs && node test/harnesses.mock.mjs",
39
+ "test": "node test/meta.mock.mjs && node test/agent.mock.mjs && node test/tui.mock.mjs && node test/images.mock.mjs && node test/models.mock.mjs && node test/providers.mock.mjs && node test/arena.mock.mjs && node test/harnesses.mock.mjs",
40
40
  "build:standalone": "node scripts/build-standalone.mjs"
41
41
  },
42
42
  "engines": {
@@ -6,16 +6,29 @@
6
6
 
7
7
  const NON_CHAT = /embed|rerank|safety|guard|reward|whisper|tts|speech|transcri|moderation|ocr|parse|clip|retriever|detector|image-gen|flux|stable-diffusion/i;
8
8
 
9
+ // Ids that are image generators even when a source's list does not say so (used for sources without that metadata).
10
+ export const looksLikeImageModel = (id) => /(^|[/._:-])(image|images|imagen|nano-banana|flux|recraft|seedream|stable-diffusion|sdxl|dall-e|gpt-image|riverflow|krea)([/._:-]|\d|$)/i.test(String(id));
11
+
12
+ // OpenRouter: a model is free when nothing it charges for costs anything (image models also bill per image).
13
+ const orFree = (pr) => !!pr && Object.entries(pr).filter(([k]) => /^(prompt|completion|image_token|image_output)$/.test(k)).every(([, v]) => Number(v) === 0);
14
+ const orImage = (m) => (m.architecture?.output_modalities || []).includes("image");
15
+
9
16
  export const CATALOG = {
10
17
  openrouter: {
11
18
  label: "OpenRouter", api: "openai", baseUrl: "https://openrouter.ai/api/v1", keyEnv: ["OPENROUTER_API_KEY"],
12
19
  keyUrl: "https://openrouter.ai/keys", blurb: "hundreds of models, some free (Nemotron, Qwen, Llama…)", publicList: true,
13
20
  async list(fetchJson) {
14
- const j = await fetchJson("https://openrouter.ai/api/v1/models");
15
- return (j.data || []).map((m) => ({
16
- id: m.id, ctx: m.context_length ?? null,
17
- free: m.pricing ? Number(m.pricing.prompt) === 0 && Number(m.pricing.completion) === 0 : null,
18
- tools: Array.isArray(m.supported_parameters) ? m.supported_parameters.includes("tools") : null,
21
+ // the default list only has models that write text; image-only models (Recraft, Flux…) are asked for separately
22
+ const [j, img] = await Promise.all([
23
+ fetchJson("https://openrouter.ai/api/v1/models"),
24
+ fetchJson("https://openrouter.ai/api/v1/models?output_modalities=image").catch(() => ({ data: [] })),
25
+ ]);
26
+ const seen = new Set();
27
+ return [...(j.data || []), ...(img.data || [])].filter((m) => m?.id && !seen.has(m.id) && seen.add(m.id)).map((m) => ({
28
+ id: m.id, ctx: orImage(m) ? null : m.context_length ?? null,
29
+ free: m.pricing ? orFree(m.pricing) : null,
30
+ tools: orImage(m) ? null : Array.isArray(m.supported_parameters) ? m.supported_parameters.includes("tools") : null,
31
+ image: orImage(m),
19
32
  }));
20
33
  },
21
34
  },
@@ -0,0 +1,165 @@
1
+ // Image generation for /image. A separate "image model" (saved with /add, switched with /image-model) makes the
2
+ // picture; your coding model is not involved and never makes images on its own.
3
+ //
4
+ // Wire formats (checked against the providers' docs on 2026-11-21):
5
+ // OpenRouter POST {baseUrl}/images { model, prompt, aspect_ratio, n } -> { data: [{ b64_json, media_type }] }
6
+ // Google POST .../v1beta/interactions { model, input, response_format } -> base64 image in an "image" content block
7
+ // (header Api-Revision: 2026-05-20). Google's image models are not on the free tier: they need billing.
8
+ // others POST {baseUrl}/images/generations (OpenAI style) { model, prompt, n, size } -> { data: [{ b64_json | url }] }
9
+ // The response reader below accepts every shape above (and a few older ones), so a provider tweaking its format does
10
+ // not break saving.
11
+ import fs from "node:fs";
12
+ import path from "node:path";
13
+ import { parseModelSpec, resolveProvider, keyFor, postWithRetry } from "./providers.mjs";
14
+
15
+ export const ASPECTS = { square: "1:1", wide: "16:9", tall: "9:16" };
16
+ const OPENAI_SIZES = { "1:1": "1024x1024", "16:9": "1536x1024", "9:16": "1024x1536" };
17
+ export const GOOGLE_API_REVISION = "2026-05-20";
18
+
19
+ export { looksLikeImageModel } from "./catalog.mjs";
20
+
21
+ export class ImageError extends Error {
22
+ constructor(message, { status, kind } = {}) { super(message); this.name = "ImageError"; this.status = status; this.kind = kind; }
23
+ }
24
+
25
+ /**
26
+ * Makes one image. Returns { bytes: Buffer, mime, ext }.
27
+ * spec: "openrouter/recraft/recraft-v4.1-flash-v2:free" or "gemini/gemini-nano-banana-2.1"
28
+ */
29
+ export async function generateImage({ spec, prompt, aspect = "1:1", cfg = {}, signal, onRetry, env = process.env }) {
30
+ if (!String(prompt || "").trim()) throw new ImageError("Describe the image, e.g. /image a black sport bike at night");
31
+ const { provider, model } = parseModelSpec(spec);
32
+ const p = resolveProvider(provider, cfg);
33
+ const key = keyFor(provider, p, env);
34
+ const auth = key ? { authorization: `Bearer ${key}` } : {};
35
+ const timeoutMs = Math.max(60, Number(cfg.requestTimeoutSec) || 180) * 1000;
36
+ const opts = { signal, onRetry, retries: 2, timeoutMs, label: "The image model" };
37
+
38
+ let r;
39
+ try {
40
+ if (isGoogle(provider, p)) {
41
+ const url = p.imagesUrl || String(p.baseUrl).replace(/\/openai\/?$/, "") + "/interactions";
42
+ r = await postWithRetry(url, { "x-goog-api-key": key, "api-revision": GOOGLE_API_REVISION }, {
43
+ model, input: [{ type: "text", text: prompt }],
44
+ response_format: { type: "image", mime_type: "image/png", aspect_ratio: aspect },
45
+ }, opts);
46
+ } else if (provider === "openrouter" || /openrouter\.ai/.test(p.baseUrl)) {
47
+ r = await postWithRetry(p.imagesUrl || `${p.baseUrl}/images`, auth, { model, prompt, aspect_ratio: aspect, n: 1 }, opts);
48
+ } else {
49
+ r = await postWithRetry(p.imagesUrl || `${p.baseUrl}/images/generations`, auth, { model, prompt, n: 1, size: OPENAI_SIZES[aspect] || "1024x1024" }, opts);
50
+ }
51
+ } catch (e) {
52
+ if (signal?.aborted || e?.name === "AbortError") throw e;
53
+ throw explainImageError(e, provider);
54
+ }
55
+
56
+ const text = await r.text();
57
+ let j;
58
+ try { j = JSON.parse(text); } catch { throw new ImageError(`The image model sent something that is not an image response: ${text.replace(/\s+/g, " ").slice(0, 160)}`, { kind: "bad" }); }
59
+ const found = findImage(j);
60
+ if (!found) {
61
+ const said = typeof j?.output_text === "string" ? j.output_text : findText(j);
62
+ throw new ImageError(`The image model answered without an image${said ? `: "${said.replace(/\s+/g, " ").slice(0, 200)}"` : ""}. Try rewording the description.`, { kind: "noimage" });
63
+ }
64
+ let bytes;
65
+ if (found.url) {
66
+ const g = await fetch(found.url, { signal: signal ? AbortSignal.any([signal, AbortSignal.timeout(60000)]) : AbortSignal.timeout(60000) });
67
+ if (!g.ok) throw new ImageError(`Could not download the image (${g.status}).`, { kind: "bad" });
68
+ bytes = Buffer.from(await g.arrayBuffer());
69
+ } else bytes = Buffer.from(found.b64.replace(/\s+/g, ""), "base64");
70
+ const kind = sniff(bytes);
71
+ if (!kind) throw new ImageError("The image model sent data that is not a valid image, so nothing was saved. Try again.", { kind: "bad" });
72
+ return { bytes, ...kind };
73
+ }
74
+
75
+ const isGoogle = (provider, p) => provider === "gemini" || /generativelanguage\.googleapis\.com/.test(p.baseUrl || "");
76
+
77
+ // Plain-language reasons for the usual failures.
78
+ export function explainImageError(e, provider) {
79
+ const msg = String(e?.message || e);
80
+ const st = e?.status;
81
+ const google = provider === "gemini";
82
+ if (e?.timeout) return new ImageError(`${msg}`, { status: st, kind: "timeout" });
83
+ // (a free key asking for a paid-only model gets "limit: 0" quota errors, or a billing / free-tier message)
84
+ if (google && /billing|free.?tier|limit:\s*0\b|FAILED_PRECONDITION|not available|paid/i.test(msg))
85
+ return new ImageError("Google's image models need billing turned on; a free Gemini key cannot make images. Turn on billing at https://aistudio.google.com, or switch to a free image model with /image-model.", { status: st, kind: "billing" });
86
+ if (st === 401 || st === 403) return new ImageError(`The provider rejected the key (${st}). Check it with /add, or pick another image model with /image-model.`, { status: st, kind: "auth" });
87
+ if (st === 402 || /insufficient credits|payment required/i.test(msg)) return new ImageError("This image model is not free and the account has no credits. Pick a free one with /image-model or /add.", { status: st, kind: "paid" });
88
+ if (st === 404 || /no endpoints|not found|deprecated|no longer available|removed/i.test(msg))
89
+ return new ImageError(`This image model is not available any more (${msg.slice(0, 120)}). Free models come and go: add another with /add, then pick it with /image-model.`, { status: st, kind: "gone" });
90
+ if (st === 429 || e?.quota) return new ImageError("The image model's free quota or rate limit is used up for now. Wait a bit and try again, or pick another image model with /image-model.", { status: st, kind: "quota" });
91
+ if (st === 400 && /safety|policy|blocked|moderat|content/i.test(msg)) return new ImageError(`The image model refused this description (${msg.slice(0, 160)}). Try describing it differently.`, { status: st, kind: "refused" });
92
+ return new ImageError(`The image model failed: ${msg.slice(0, 300)}`, { status: st, kind: "other" });
93
+ }
94
+
95
+ // Finds the first image in a response, whatever the provider's shape. Returns { b64 } or { url } or null.
96
+ export function findImage(j) {
97
+ const seen = new Set();
98
+ const stack = [j];
99
+ while (stack.length) {
100
+ const o = stack.shift();
101
+ if (!o || typeof o !== "object" || seen.has(o)) continue;
102
+ seen.add(o);
103
+ if (typeof o.b64_json === "string" && o.b64_json) return { b64: o.b64_json };
104
+ if (typeof o.bytesBase64Encoded === "string") return { b64: o.bytesBase64Encoded };
105
+ for (const k of ["inlineData", "inline_data", "output_image"]) if (typeof o[k]?.data === "string" && o[k].data) return { b64: o[k].data };
106
+ if (o.type === "image" && typeof o.data === "string" && o.data.length > 64) return fromMaybeDataUrl(o.data);
107
+ const iu = typeof o.image_url === "string" ? o.image_url : o.image_url?.url;
108
+ if (typeof iu === "string" && iu) return fromMaybeDataUrl(iu);
109
+ if (Array.isArray(j?.data) && j.data.includes(o) && typeof o.url === "string" && /^https?:\/\//.test(o.url)) return { url: o.url };
110
+ for (const [k, v] of Object.entries(o)) if (k !== "input" && v && typeof v === "object") stack.push(v);
111
+ }
112
+ return null;
113
+ }
114
+ function fromMaybeDataUrl(s) {
115
+ const m = /^data:[^;,]+;base64,(.*)$/s.exec(s);
116
+ if (m) return { b64: m[1] };
117
+ if (/^https?:\/\//.test(s)) return { url: s };
118
+ return { b64: s };
119
+ }
120
+ function findText(j) {
121
+ const stack = [j];
122
+ while (stack.length) {
123
+ const o = stack.shift();
124
+ if (!o || typeof o !== "object") continue;
125
+ if (o.type === "text" && typeof o.text === "string") return o.text;
126
+ if (typeof o.content === "string" && o.content) return o.content;
127
+ for (const [k, v] of Object.entries(o)) if (k !== "input" && v && typeof v === "object") stack.push(v);
128
+ }
129
+ return "";
130
+ }
131
+
132
+ // What kind of image the bytes are (by their first bytes, not by what the provider claims). null = not an image.
133
+ export function sniff(b) {
134
+ if (!b || b.length < 32) return null;
135
+ if (b[0] === 0x89 && b.toString("latin1", 1, 4) === "PNG") return { mime: "image/png", ext: "png" };
136
+ if (b[0] === 0xff && b[1] === 0xd8 && b[2] === 0xff) return { mime: "image/jpeg", ext: "jpg" };
137
+ if (b.toString("latin1", 0, 4) === "RIFF" && b.toString("latin1", 8, 12) === "WEBP") return { mime: "image/webp", ext: "webp" };
138
+ if (b.toString("latin1", 0, 4) === "GIF8") return { mime: "image/gif", ext: "gif" };
139
+ const head = b.toString("utf8", 0, Math.min(b.length, 600)).trimStart();
140
+ if (/^(<\?xml[^>]*>\s*)?(<!--[\s\S]*?-->\s*)*(<!DOCTYPE svg[^>]*>\s*)?<svg[\s>]/i.test(head)) return { mime: "image/svg+xml", ext: "svg" };
141
+ return null;
142
+ }
143
+
144
+ // "A black sport bike on a wet road!" -> "black-sport-bike-on-wet-road"
145
+ export function slugFor(prompt) {
146
+ const stop = new Set(["a", "an", "the", "of", "with", "and", "in", "at"]);
147
+ const words = String(prompt).toLowerCase().match(/[a-z0-9]+/g) || [];
148
+ const s = words.filter((w) => !stop.has(w)).slice(0, 6).join("-").slice(0, 50).replace(/-+$/, "");
149
+ return s || "image";
150
+ }
151
+
152
+ // Saves into <cwd>/images/<slug>.<ext>, adding -2, -3… instead of overwriting. Returns the path relative to cwd.
153
+ export function saveImage(cwd, prompt, { bytes, ext }) {
154
+ const dir = path.join(cwd, "images");
155
+ fs.mkdirSync(dir, { recursive: true });
156
+ const base = slugFor(prompt);
157
+ for (let i = 1; i < 1000; i++) {
158
+ const file = path.join(dir, `${base}${i > 1 ? "-" + i : ""}.${ext}`);
159
+ try { fs.writeFileSync(file, bytes, { flag: "wx" }); return path.relative(cwd, file).split(path.sep).join("/"); }
160
+ catch (e) { if (e.code !== "EEXIST") throw e; }
161
+ }
162
+ throw new ImageError("Too many images with this name in images/. Rename or delete some.");
163
+ }
164
+
165
+ export const fmtBytes = (n) => (n >= 1048576 ? `${(n / 1048576).toFixed(1)} MB` : `${Math.max(1, Math.round(n / 1024))} KB`);
@@ -1,10 +1,20 @@
1
1
  // Models you added with /add, saved in ~/.warden/config.json under agent.models:
2
2
  // [{ name: "nemotron-free", spec: "openrouter/nvidia/nemotron-3.5-lightning:free", ctx: 262144, tools: "native" | "text" | "none" | null }]
3
3
  // tools = how this model gets to use Warden's tools: its own tool calling, the text fallback, or chat only.
4
+ // kind: "image" marks an image model (used only by /image; agent.imageModel holds the spec of the one in use).
4
5
  import { saveAgentConfig } from "./config.mjs";
5
6
  import { chat, toolsUnsupported } from "./providers.mjs";
6
7
 
7
8
  export const savedModels = (cfg) => (Array.isArray(cfg.models) ? cfg.models : []);
9
+ export const chatModels = (cfg) => savedModels(cfg).filter((m) => m.kind !== "image");
10
+ export const imageModels = (cfg) => savedModels(cfg).filter((m) => m.kind === "image");
11
+
12
+ // The image model /image uses: { name, spec } (name may be null for one set by spec only), or null.
13
+ export function currentImageModel(cfg) {
14
+ if (!cfg.imageModel) return null;
15
+ return findModel(cfg, cfg.imageModel) || { name: null, spec: cfg.imageModel };
16
+ }
17
+ export function setImageModel(cfg, spec) { cfg.imageModel = spec || null; saveAgentConfig({ imageModel: cfg.imageModel }); }
8
18
 
9
19
  export function findModel(cfg, nameOrSpec) {
10
20
  return savedModels(cfg).find((m) => m.name === nameOrSpec) || savedModels(cfg).find((m) => m.spec === nameOrSpec) || null;
@@ -14,6 +24,7 @@ export function findModel(cfg, nameOrSpec) {
14
24
  export function resolveModelInput(cfg, input) {
15
25
  const s = String(input || "").trim();
16
26
  const hit = savedModels(cfg).find((m) => m.name === s);
27
+ if (hit?.kind === "image") throw new Error(`"${s}" is an image model: it makes pictures with /image and cannot chat or code. Use it with /image-model ${s}`);
17
28
  if (hit) return hit.spec;
18
29
  if (s.includes("/")) return s;
19
30
  throw new Error(`no saved model called "${s}". /model lists them; /add adds one; or use provider/model`);
@@ -29,9 +40,10 @@ export function addModel(cfg, entry) {
29
40
 
30
41
  export function removeModel(cfg, name) {
31
42
  const before = savedModels(cfg);
32
- const after = before.filter((m) => m.name !== name);
33
- if (after.length === before.length) return false;
34
- persist(cfg, after);
43
+ const gone = before.find((m) => m.name === name);
44
+ if (!gone) return false;
45
+ persist(cfg, before.filter((m) => m !== gone));
46
+ if (gone.kind === "image" && cfg.imageModel === gone.spec) setImageModel(cfg, null);
35
47
  return true;
36
48
  }
37
49
 
@@ -39,6 +51,7 @@ export function removeModelsOf(cfg, provider) {
39
51
  const after = savedModels(cfg).filter((m) => !m.spec.startsWith(provider + "/"));
40
52
  const n = savedModels(cfg).length - after.length;
41
53
  if (n) persist(cfg, after);
54
+ if (String(cfg.imageModel || "").startsWith(provider + "/")) setImageModel(cfg, null);
42
55
  return n;
43
56
  }
44
57
 
@@ -8,7 +8,7 @@
8
8
  // { role: "assistant", content: string, toolCalls?: [{ id, name, args }] }
9
9
  // { role: "tool", toolCallId, name, content: string }
10
10
 
11
- import { CATALOG } from "./catalog.mjs";
11
+ import { CATALOG, looksLikeImageModel } from "./catalog.mjs";
12
12
  import { savedKey } from "./credentials.mjs";
13
13
  import { textToolsPrompt, toTextProtocol, parseTextToolCalls, ToolTagFilter } from "./texttools.mjs";
14
14
 
@@ -149,7 +149,7 @@ async function readStream(r, { signal, idleMs, stallMs, onLine, label }) {
149
149
  if (fired === "stall") throw new ModelTimeoutError(`${label} kept the connection open for ${secs(stallMs)}s without sending any reply (it may be overloaded), so Warden stopped waiting. Try again, or switch models with /model.`);
150
150
  }
151
151
 
152
- async function postWithRetry(url, headers, body, { signal, onRetry, retries = 4, timeoutMs = TIMEOUT_DEFAULTS.requestTimeoutSec * 1000, label = "The model" }) {
152
+ export async function postWithRetry(url, headers, body, { signal, onRetry, retries = 4, timeoutMs = TIMEOUT_DEFAULTS.requestTimeoutSec * 1000, label = "The model" }) {
153
153
  let timeoutRetries = 1;
154
154
  for (let attempt = 0; ; attempt++) {
155
155
  // each attempt gets its own deadline for the response to start; the user's interrupt still applies throughout
@@ -185,7 +185,7 @@ async function postWithRetry(url, headers, body, { signal, onRetry, retries = 4,
185
185
  const retryable = r.status === 429 || r.status >= 500;
186
186
  if (!retryable || attempt >= retries) throw fail();
187
187
  // A long or daily quota is not worth waiting for: say so now instead of retrying.
188
- if (r.status === 429 && (hintedDelay > 60_000 || (/per ?day|daily|PerDay/i.test(msg) && !hintedDelay))) {
188
+ if (r.status === 429 && (hintedDelay > 60_000 || (/per[ -]?day|daily|PerDay/i.test(msg) && !hintedDelay))) {
189
189
  const e = fail(" [quota used up for now; switch model with /model or try again later]");
190
190
  e.quota = true;
191
191
  throw e;
@@ -299,7 +299,7 @@ async function anthropicChat({ p, key, model, system, messages, tools, signal, o
299
299
  return { text, toolCalls, usage: j.usage ? { input: j.usage.input_tokens ?? 0, output: j.usage.output_tokens ?? 0 } : null, stopReason: j.stop_reason };
300
300
  }
301
301
 
302
- // Models a provider offers: [{ id, ctx, free, tools }] (null = unknown). Uses the source's public list when it has one.
302
+ // Models a provider offers: [{ id, ctx, free, tools, image }] (null = unknown). Uses the source's public list when it has one.
303
303
  export async function listModels(name, cfg = {}, env = process.env) {
304
304
  const p = resolveProvider(name, cfg);
305
305
  let key = null;
@@ -315,5 +315,5 @@ export async function listModels(name, cfg = {}, env = process.env) {
315
315
  const j = await fetchJson(`${p.baseUrl}/models`);
316
316
  list = (j.data || j.models || []).map((m) => ({ id: String(m.id || m.name || "").replace(/^models\//, ""), ctx: m.context_length ?? m.context_window ?? null, free: null, tools: null }));
317
317
  } else throw new Error(`listing models is not supported for ${name}; type the model id instead`);
318
- return list.filter((m) => m.id).sort((a, b) => a.id.localeCompare(b.id));
318
+ return list.filter((m) => m.id).map((m) => ({ ...m, image: m.image ?? looksLikeImageModel(m.id) })).sort((a, b) => a.id.localeCompare(b.id));
319
319
  }
package/src/tui/app.mjs CHANGED
@@ -10,7 +10,8 @@ import { parseModelSpec, keyStatus, listModels, resolveProvider } from "../agent
10
10
  import { MODES, detectShell, listJobs, stopJob, jobRunning, describeJob } from "../agent/tools.mjs";
11
11
  import { CATALOG, ADD_ORDER } from "../agent/catalog.mjs";
12
12
  import { saveKey, removeKey, mask, CREDENTIALS_FILE } from "../agent/credentials.mjs";
13
- import { savedModels, findModel, resolveModelInput, addModel, removeModel, removeModelsOf, suggestName, probeToolSupport } from "../agent/models.mjs";
13
+ import { savedModels, chatModels, imageModels, currentImageModel, setImageModel, findModel, resolveModelInput, addModel, removeModel, removeModelsOf, suggestName, probeToolSupport } from "../agent/models.mjs";
14
+ import { generateImage, saveImage, looksLikeImageModel, ASPECTS, fmtBytes } from "../agent/images.mjs";
14
15
 
15
16
  import { VERSION } from "../version.mjs";
16
17
  import { updateNotice } from "../agent/update.mjs";
@@ -19,6 +20,8 @@ const COMMANDS = {
19
20
  "/add": "add a model: pick a source (OpenRouter, Hugging Face, NVIDIA, DeepSeek, Ollama…), paste a key, pick a model",
20
21
  "/model": "pick from your saved models, or /model <name> or /model provider/model",
21
22
  "/models": "browse a source's models: /models openrouter nemotron --free",
23
+ "/image": "make a picture with your image model: /image a red bike at night (--wide, --tall, --square)",
24
+ "/image-model": "pick the image model /image uses (add one with /add)",
22
25
  "/providers": "which sources are connected",
23
26
  "/remove": "remove a saved model, or a source's saved key: /remove <name>",
24
27
  "/mode": "permission mode: ask | auto-edit | yolo (also shift+tab)",
@@ -50,8 +53,8 @@ export async function startApp({ cwd = process.cwd(), model, mode, resume, initi
50
53
  input: process.stdin, output: out, terminal: tty, historySize: 200,
51
54
  completer: (line) => {
52
55
  if (!line.startsWith("/")) return [[], line];
53
- const m = line.match(/^\/(model|remove) (.*)$/);
54
- if (m) { const opts = savedModels(cfg).map((x) => `/${m[1]} ${x.name}`).filter((o) => o.startsWith(line)); return [opts, line]; }
56
+ const m = line.match(/^\/(model|remove|image-model) (.*)$/);
57
+ if (m) { const pool = m[1] === "model" ? chatModels(cfg) : m[1] === "image-model" ? imageModels(cfg) : savedModels(cfg); const opts = pool.map((x) => `/${m[1]} ${x.name}`).filter((o) => o.startsWith(line)); return [opts, line]; }
55
58
  const hits = Object.keys(COMMANDS).filter((c) => c.startsWith(line));
56
59
  return [hits.length ? hits : Object.keys(COMMANDS), line];
57
60
  },
@@ -378,8 +381,8 @@ export async function startApp({ cwd = process.cwd(), model, mode, resume, initi
378
381
  switch (cmd) {
379
382
  case "/help": case "/?":
380
383
  console.log();
381
- for (const [k, v] of Object.entries(COMMANDS)) console.log(` ${C.accent(k.padEnd(9))} ${C.dim(v)}`);
382
- console.log(` ${C.accent("!cmd".padEnd(9))} ${C.dim("run a shell command yourself; the model sees the output")}`);
384
+ for (const [k, v] of Object.entries(COMMANDS)) console.log(` ${C.accent(k.padEnd(12))} ${C.dim(v)}`);
385
+ console.log(` ${C.accent("!cmd".padEnd(12))} ${C.dim("run a shell command yourself; the model sees the output")}`);
383
386
  console.log(C.dim(`\n While the model works: keep typing and press enter to queue a message (it gets it at its next step) · esc or ctrl+c interrupts`));
384
387
  console.log(C.dim(` Keys: ctrl+c twice at an empty prompt quits · shift+tab cycles the mode · end a line with \\ for multi-line\n Settings: ${CONFIG_FILE}\n`));
385
388
  break;
@@ -413,13 +416,13 @@ export async function startApp({ cwd = process.cwd(), model, mode, resume, initi
413
416
  console.log(C.dim(` mode: ${modeLabel(arg)}${arg === "yolo" ? C.yellow(" (edits and shell commands run without asking)") : ""}\n`)); break;
414
417
  case "/model": {
415
418
  if (!arg) {
416
- const list = savedModels(cfg);
419
+ const list = chatModels(cfg);
417
420
  console.log(`\n current: ${C.bold(session.model)}${findModel(cfg, session.model) ? C.gray(` (${findModel(cfg, session.model).name})`) : ""}\n`);
418
421
  if (!list.length) { console.log(C.dim(" No saved models yet. /add connects a source and picks a model.\n")); break; }
419
422
  list.forEach((m, i) => console.log(` ${C.accent(String(i + 1).padStart(2))} ${(m.spec === session.model ? C.mint("● ") : " ") + m.name.padEnd(32)} ${C.gray(m.spec)}${toolTag(m.tools)}`));
420
423
  const pick = (await answer(`\n Number to switch to (enter to keep the current one) ${C.accent("›")} `, "")).trim();
421
424
  if (!pick) { console.log(); break; }
422
- const m = /^\d+$/.test(pick) ? list[Number(pick) - 1] : findModel(cfg, pick);
425
+ const m = /^\d+$/.test(pick) ? list[Number(pick) - 1] : list.find((x) => x.name === pick || x.spec === pick);
423
426
  if (!m) { console.log(C.red(" no such model\n")); break; }
424
427
  switchModel(m.spec);
425
428
  break;
@@ -445,6 +448,8 @@ export async function startApp({ cwd = process.cwd(), model, mode, resume, initi
445
448
  break;
446
449
  }
447
450
  case "/add": await addWizard(); break;
451
+ case "/image": await imageCommand(arg); break;
452
+ case "/image-model": await imageModelCommand(arg); break;
448
453
  case "/providers": {
449
454
  console.log();
450
455
  for (const name of [...ADD_ORDER, ...Object.keys(cfg.providers || {}).filter((n) => !CATALOG[n])]) {
@@ -496,6 +501,8 @@ export async function startApp({ cwd = process.cwd(), model, mode, resume, initi
496
501
  }
497
502
 
498
503
  function switchModel(spec) {
504
+ const im = findModel(cfg, spec);
505
+ if (im?.kind === "image") { console.log(C.red(` ${im.name} is an image model; it cannot chat or code. Use it with /image-model.\n`)); return; }
499
506
  const { provider } = parseModelSpec(spec);
500
507
  resolveProvider(provider, cfg);
501
508
  session.model = spec;
@@ -555,8 +562,9 @@ export async function startApp({ cwd = process.cwd(), model, mode, resume, initi
555
562
  console.log(C.dim(` ${list.length} models${free.length ? `, ${free.length} free` : ""}.`));
556
563
  const q = (await answer(` Search (enter for ${free.length ? "free ones" : "all"}, "all" for everything) ${C.accent("›")} `, "")).trim().toLowerCase();
557
564
  let shown = q === "all" ? list : q ? list.filter((m) => m.id.toLowerCase().includes(q)) : free.length ? free : list;
558
- shown = [...shown].sort((a, b) => (b.free === true) - (a.free === true) || (b.tools === true) - (a.tools === true) || a.id.localeCompare(b.id));
565
+ shown = [...shown].sort((a, b) => (!!a.image - !!b.image) || (b.free === true) - (a.free === true) || (b.tools === true) - (a.tools === true) || a.id.localeCompare(b.id));
559
566
  if (!shown.length) console.log(C.yellow(" Nothing matched."));
567
+ if (shown.length && shown.every((m) => m.image)) console.log(C.dim(" These are image models: they make pictures with /image (your coding model stays as it is)."));
560
568
  shown.slice(0, 30).forEach((m, i) => console.log(` ${C.accent(String(i + 1).padStart(2))} ${m.id}${modelFlags(m)}`));
561
569
  if (shown.length > 30) console.log(C.gray(` … ${shown.length - 30} more; search to narrow down`));
562
570
  const sel = (await answer(` Number, or type a model id ${C.accent("›")} `, "")).trim();
@@ -569,6 +577,7 @@ export async function startApp({ cwd = process.cwd(), model, mode, resume, initi
569
577
  }
570
578
  const spec = `${name}/${id}`;
571
579
  const meta = list.find((m) => m.id === id) || {};
580
+ if (meta.image ?? looksLikeImageModel(id)) return addImageModel(name, id, spec);
572
581
 
573
582
  // test it once: proves the key and model work, and finds out how it can use tools
574
583
  const spin2 = new Spinner(out, C); spin2.start(`Testing ${spec}`);
@@ -591,6 +600,84 @@ export async function startApp({ cwd = process.cwd(), model, mode, resume, initi
591
600
  switchModel(spec);
592
601
  }
593
602
 
603
+ // /add picked an image model: save it as the image model (the coding model stays), optionally test it.
604
+ async function addImageModel(name, id, spec) {
605
+ console.log(` ${C.accent("🖼")} ${C.bold(id)} is an ${C.bold("image model")}: it makes pictures with /image and cannot chat or write code.`);
606
+ console.log(C.dim(` Your coding model stays ${findModel(cfg, session.model)?.name || session.model}.`));
607
+ if (name === "gemini") console.log(C.yellow(" Note: Google's image models need billing turned on; a free Gemini key cannot make images."));
608
+ const t = (await answer(` Test it by making one small image now? (uses one image from its quota) [Y/n] `, "y")).trim();
609
+ if (!/^n/i.test(t)) {
610
+ const spin = new Spinner(out, C); spin.start(`Making a test image with ${spec}`);
611
+ try {
612
+ const img = await generateImage({ spec, prompt: "a simple red circle on a plain white background", cfg, onRetry: ({ status: st, waitMs }) => spin.setLabel(`Rate limited (${st}), retrying in ${Math.round(waitMs / 1000)}s`) });
613
+ spin.stop(); console.log(` ${C.mint("✓")} Works. It sent a ${img.ext.toUpperCase()} image (${fmtBytes(img.bytes.length)}).`);
614
+ } catch (e) {
615
+ spin.stop(); console.log(C.red(` Test failed: ${e.message}`));
616
+ const keep = (await answer(` Save it anyway? [y/N] `, "n")).trim();
617
+ if (!/^y/i.test(keep)) return console.log(C.dim(" Not saved.\n"));
618
+ }
619
+ }
620
+ let short = suggestName(id);
621
+ const typed = (await answer(` Name it (enter for "${short}") ${C.accent("›")} `, "")).trim();
622
+ if (typed) short = typed.replace(/\s+/g, "-");
623
+ addModel(cfg, { name: short, spec, kind: "image" });
624
+ setImageModel(cfg, spec);
625
+ console.log(C.mint(` ✓ Added ${short} as your image model.`) + C.dim(` Try: /image a black sport bike on a wet road at night --wide\n`));
626
+ }
627
+
628
+ // /image-model: show the image models, switch to one by number or name.
629
+ async function imageModelCommand(arg) {
630
+ const list = imageModels(cfg);
631
+ const cur = currentImageModel(cfg);
632
+ if (arg) {
633
+ const m = list.find((x) => x.name === arg || x.spec === arg) || (arg.includes("/") ? { name: null, spec: arg } : null);
634
+ if (!m) return console.log(C.red(` No saved image model called "${arg}". /image-model lists them; /add adds one.\n`));
635
+ setImageModel(cfg, m.spec);
636
+ return console.log(C.dim(` image model: ${m.name ? m.name + " = " : ""}${m.spec}. Saved.\n`));
637
+ }
638
+ console.log(`\n image model: ${cur ? C.bold(cur.name || cur.spec) + C.gray(cur.name ? ` (${cur.spec})` : "") : C.gray("none yet")}\n`);
639
+ if (!list.length) return console.log(C.dim(" No image models saved. Add one with /add (on OpenRouter, search \"image\" or \"recraft\"; free ones end in :free).\n"));
640
+ list.forEach((m, i) => console.log(` ${C.accent(String(i + 1).padStart(2))} ${(m.spec === cur?.spec ? C.mint("● ") : " ") + m.name.padEnd(32)} ${C.gray(m.spec)}`));
641
+ const pick = (await answer(`\n Number to use (enter to keep the current one) ${C.accent("›")} `, "")).trim();
642
+ if (!pick) return console.log();
643
+ const m = /^\d+$/.test(pick) ? list[Number(pick) - 1] : list.find((x) => x.name === pick);
644
+ if (!m) return console.log(C.red(" no such image model\n"));
645
+ setImageModel(cfg, m.spec);
646
+ console.log(C.dim(` image model: ${m.name} = ${m.spec}. Saved.\n`));
647
+ }
648
+
649
+ // /image <description> [--wide|--tall|--square]: makes one picture and saves it in images/. Esc cancels.
650
+ async function imageCommand(arg) {
651
+ const words = arg.split(/\s+/).filter(Boolean);
652
+ const isFlag = (w) => /^--(wide|tall|square)$/.test(w);
653
+ const flag = words.filter(isFlag).pop();
654
+ const prompt = words.filter((w) => !isFlag(w)).join(" ").trim();
655
+ if (!prompt) return console.log(C.dim(" usage: /image <what to draw> [--wide | --tall | --square]\n e.g. /image a black sport bike on a wet road at night, red light --wide\n"));
656
+ const cur = currentImageModel(cfg);
657
+ if (!cur) return console.log(C.yellow(" No image model yet.") + C.dim(" Add one with /add: pick OpenRouter, search \"image\" or \"recraft\" (free ones end in :free).\n"));
658
+ const aspect = ASPECTS[flag ? flag.slice(2) : "square"];
659
+ busy = true; controller = new AbortController();
660
+ echo(false);
661
+ status.start(`Drawing with ${cur.name || cur.spec} (${aspect})`);
662
+ try {
663
+ const img = await generateImage({ spec: cur.spec, prompt, aspect, cfg, signal: controller.signal, onRetry: ({ status: st, waitMs }) => status.spin.setLabel(`Rate limited (${st}), retrying in ${Math.round(waitMs / 1000)}s`) });
664
+ status.stop();
665
+ const rel = saveImage(cwd, prompt, img);
666
+ console.log(` ${C.mint("✓")} Saved ${C.bold(rel)} ${C.gray(`(${img.ext.toUpperCase()}, ${fmtBytes(img.bytes.length)}, ${aspect})`)}`);
667
+ console.log(C.dim(` To use it, tell the model, e.g. "use ${rel} as the hero image".\n`));
668
+ // the coding model learns the file exists, like a shared terminal
669
+ session.push({ role: "user", content: `I made an image with /image and saved it as ${rel} (${img.ext}, ${aspect}). Description: ${prompt}\n(No reply needed unless I ask.)` });
670
+ session.push({ role: "assistant", content: "Noted." });
671
+ } catch (e) {
672
+ status.stop();
673
+ if (controller.signal.aborted) console.log(C.yellow(" Cancelled.") + C.dim(" No image was saved.\n"));
674
+ else console.log(C.red(` ${e.message}\n`));
675
+ } finally {
676
+ busy = false; controller = null;
677
+ echo(true); rl.prevRows = 0;
678
+ }
679
+ }
680
+
594
681
  function replayTail() {
595
682
  const tail = session.messages.filter((m) => (m.role === "user" || (m.role === "assistant" && m.content))).slice(-4);
596
683
  for (const m of tail) console.log(m.role === "user" ? `${C.accent("›")} ${m.content.slice(0, 300)}` : C.dim(` ${m.content.slice(0, 400)}`));
@@ -603,6 +690,8 @@ export async function startApp({ cwd = process.cwd(), model, mode, resume, initi
603
690
  console.log(` ${C.mint("◉")} ${C.bold("Warden")} ${C.gray("v" + VERSION)}`);
604
691
  console.log(` ${C.gray("model")} ${session.model} ${/missing/.test(st) ? C.red(`(${st})`) : C.gray(`(${st})`)}`);
605
692
  console.log(` ${C.gray("mode ")} ${modeLabel(perm.mode)} ${C.gray("(shift+tab or /mode)")}`);
693
+ const im = currentImageModel(cfg);
694
+ if (im) console.log(` ${C.gray("image")} ${im.name || im.spec} ${C.gray("(/image to draw, /image-model to switch)")}`);
606
695
  console.log(` ${C.gray("dir ")} ${cwd}`);
607
696
  console.log(C.gray(` /help for commands · /model to switch models · esc interrupts · type while it works to queue a message`));
608
697
  const upd = updateNotice(cfg);
package/src/version.mjs CHANGED
@@ -1,4 +1,4 @@
1
1
  // Keep in sync with package.json (test/meta.mock.mjs checks this).
2
- export const VERSION = "0.4.1";
2
+ export const VERSION = "0.5.0";
3
3
  export const PACKAGE = "warden-agent";
4
4
  export const REPO = "yashhonap/warden";