@aaronjmars/openroutercli 0.2.2 → 0.4.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.
@@ -1,12 +1,160 @@
1
1
  import readline from "node:readline/promises";
2
2
  import { stdin, stdout } from "node:process";
3
- import { parseArgs, authFromValues, numberOption, shouldStream } from "../args.js";
3
+ import {
4
+ parseArgs,
5
+ authFromValues,
6
+ numberOption,
7
+ shouldStream,
8
+ loadJson,
9
+ REQUEST_FIELD_OPTIONS,
10
+ REQUEST_FIELD_HELP,
11
+ applyRequestFields,
12
+ } from "../args.js";
4
13
  import { api, assertNoStreamError, sseStream } from "../api.js";
5
14
  import { c, info, isJsonMode, out, outln, printJSON } from "../output.js";
6
15
 
7
16
  const DEFAULT_MODEL = "openrouter/auto";
8
17
  const ERROR_FINISH_REASONS = ["error", "content_filter"];
9
18
 
19
+ // ---- Shared by chat, messages and responses (messages.js imports these) ----
20
+
21
+ export const SHARED_OPTIONS = {
22
+ "server-tool": { type: "string", multiple: true },
23
+ "defer-loading": { type: "boolean" },
24
+ "deferred-tools": { type: "string" },
25
+ cache: { type: "boolean" },
26
+ "cache-ttl": { type: "string" },
27
+ rule: { type: "string", multiple: true },
28
+ "alignment-mode": { type: "string" },
29
+ "alignment-threshold": { type: "string" },
30
+ };
31
+
32
+ export const SERVER_TOOL_HELP = ` --server-tool <name>[=<json|@file>] OpenRouter server tool (repeatable),
33
+ sent as {"type":"openrouter:<name>","parameters":{..}}.
34
+ e.g. web_search, web_fetch, datetime, tool_search,
35
+ image_generation, fusion, advisor, subagent, bash
36
+ --defer-loading Mark every --tool as defer_loading (pair with
37
+ --server-tool tool_search or --deferred-tools)
38
+ --deferred-tools <json|@file> Router-level deferred tools, e.g.
39
+ {"protocol":"v1","profile":"portable","validation":
40
+ "runtime","search":{"type":"bm25","max_results":5}}
41
+ (search.type: regex | bm25 | custom)
42
+ `;
43
+
44
+ export const CACHE_HELP = ` --cache Automatic prompt caching (top-level cache_control)
45
+ --cache-ttl <ttl> 5m | 1h (implies --cache)
46
+ `;
47
+
48
+ export const ALIGNMENT_HELP = ` --rule <text> Alignment plugin rule (repeatable, max 128, <=200 chars,
49
+ one line each). Merged into --plugins
50
+ --alignment-mode <m> enforce (withhold + retry, then error) | audit
51
+ --alignment-threshold <p> Probability that counts as broken (default 0.7)
52
+ `;
53
+
54
+ // "web_search" | "openrouter:web_search" | "web_search={...}" | "web_search=@f.json"
55
+ export async function serverTool(spec) {
56
+ const eq = spec.indexOf("=");
57
+ const name = (eq === -1 ? spec : spec.slice(0, eq)).trim();
58
+ if (!name) throw new Error(`Invalid --server-tool: "${spec}"`);
59
+ const tool = { type: name.includes(":") ? name : `openrouter:${name}` };
60
+ if (eq !== -1) tool.parameters = await loadJson(spec.slice(eq + 1), "--server-tool");
61
+ return tool;
62
+ }
63
+
64
+ // Server tools, defer_loading and router-level deferred_tools. Call after
65
+ // body.tools holds the --tool definitions.
66
+ export async function applyToolExtras(body, values) {
67
+ if (values["defer-loading"]) {
68
+ if (!body.tools || !body.tools.length) throw new Error("--defer-loading needs --tool");
69
+ body.tools = body.tools.map((t) =>
70
+ !t.type || t.type === "function" || t.type === "custom" || t.input_schema
71
+ ? { ...t, defer_loading: true }
72
+ : t,
73
+ );
74
+ }
75
+ for (const spec of values["server-tool"] || []) {
76
+ (body.tools ||= []).push(await serverTool(spec));
77
+ }
78
+ if (values["deferred-tools"]) {
79
+ body.deferred_tools = await loadJson(values["deferred-tools"], "--deferred-tools");
80
+ }
81
+ }
82
+
83
+ export function applyCache(body, values) {
84
+ if (!values.cache && !values["cache-ttl"]) return;
85
+ const ttl = values["cache-ttl"];
86
+ if (ttl && ttl !== "5m" && ttl !== "1h") throw new Error(`--cache-ttl must be 5m or 1h`);
87
+ body.cache_control = { type: "ephemeral", ...(ttl ? { ttl } : {}) };
88
+ }
89
+
90
+ // Adds the alignment plugin from --rule/--alignment-*. An alignment entry
91
+ // already in --plugins is extended, since only one is allowed per request.
92
+ export function addAlignmentPlugin(list, values) {
93
+ const rules = values.rule || [];
94
+ const mode = values["alignment-mode"];
95
+ const threshold = numberOption(values["alignment-threshold"], "--alignment-threshold");
96
+ if (!rules.length && !mode && threshold === undefined) return list;
97
+ for (const r of rules) {
98
+ if (!r.trim() || r.length > 200 || /[\r\n]/.test(r)) {
99
+ throw new Error(`--rule must be one line of 1-200 chars: ${JSON.stringify(r.slice(0, 40))}`);
100
+ }
101
+ }
102
+ if (mode && mode !== "enforce" && mode !== "audit") {
103
+ throw new Error("--alignment-mode must be enforce or audit");
104
+ }
105
+ let entry = list.find((p) => p && p.id === "alignment");
106
+ if (!entry) {
107
+ entry = { id: "alignment", rules: [] };
108
+ list.push(entry);
109
+ }
110
+ entry.rules = [...(entry.rules || []), ...rules];
111
+ if (!entry.rules.length) throw new Error("--alignment-mode/--alignment-threshold need --rule");
112
+ if (mode) entry.mode = mode;
113
+ if (threshold !== undefined) entry.threshold = threshold;
114
+ return list;
115
+ }
116
+
117
+ function alignmentSummary(alignment) {
118
+ const calls = (alignment && alignment.calls) || [];
119
+ return calls
120
+ .map((call) => {
121
+ let s = `call ${call.call} ${call.outcome}`;
122
+ if (Array.isArray(call.rules)) {
123
+ const broken = call.rules
124
+ .map((r, i) => (r.broken ? i + 1 : 0))
125
+ .filter(Boolean)
126
+ .join(",");
127
+ const maxP = Math.max(...call.rules.map((r) => r.probability || 0));
128
+ s += broken ? ` (broke rule ${broken}, max p=${maxP})` : ` (max p=${maxP})`;
129
+ }
130
+ if (call.reason) s += ` (${call.reason})`;
131
+ return s;
132
+ })
133
+ .join("; ");
134
+ }
135
+
136
+ // One stderr line for a response's alignment result (pretty mode only).
137
+ export function printAlignment(alignment) {
138
+ if (alignment && alignment.calls) info(`alignment: ${alignmentSummary(alignment)}`);
139
+ }
140
+
141
+ // An enforce-mode block arrives as an API error carrying
142
+ // error.metadata.alignment; say so before the error itself is printed.
143
+ export async function withAlignmentErrors(promise) {
144
+ try {
145
+ return await promise;
146
+ } catch (err) {
147
+ const alignment = err && err.body && err.body.error && err.body.error.metadata?.alignment;
148
+ if (alignment && !isJsonMode()) {
149
+ process.stderr.write(`alignment: blocked - ${alignmentSummary(alignment)}\n`);
150
+ const rej = alignment.rejected;
151
+ const text = rej && !Array.isArray(rej) ? rej.content : undefined;
152
+ if (text) process.stderr.write(`alignment: withheld reply: ${text}\n`);
153
+ }
154
+ throw err;
155
+ }
156
+ }
157
+
10
158
  const HELP = `Usage: openrouter chat [prompt...] [options]
11
159
 
12
160
  Send a chat completion. With no prompt and a TTY, starts an interactive REPL.
@@ -21,7 +169,8 @@ Options:
21
169
  --temperature <n>
22
170
  --top-p <n>
23
171
  --top-k <n>
24
- --max-tokens <n>
172
+ --max-tokens <n> (deprecated upstream; prefer --max-completion-tokens)
173
+ --max-completion-tokens <n>
25
174
  --seed <n>
26
175
  --stop <text> Stop sequence (repeatable via comma)
27
176
  --frequency-penalty <n> -2.0 to 2.0
@@ -34,23 +183,44 @@ Options:
34
183
  --logit-bias <json|@file> Token bias map
35
184
  --json-output Ask the model for JSON (response_format=json_object)
36
185
  --schema <file|json> JSON schema for structured output
37
- --tool <file|json> Tool definition (repeatable). JSON object or @file.json
186
+ --tool <file|json> Tool definition (repeatable). JSON object or @file.json.
187
+ A bare {name, parameters} is wrapped as a function;
188
+ server tools ({"type":"openrouter:..."}) pass through.
189
+ Add "defer_loading": true to load a tool on demand
38
190
  --tool-choice <v> auto | none | required | <tool name>
39
191
  --no-parallel-tools Disable parallel tool calls
40
- --reasoning <effort> none|minimal|low|medium|high|xhigh|max
192
+ ${SERVER_TOOL_HELP} --reasoning <effort> none|minimal|low|medium|high|xhigh|max
41
193
  --reasoning-max-tokens <n> Reasoning token budget (Anthropic-style)
42
194
  --reasoning-exclude Reason internally but omit reasoning from the output
43
- --provider <json> Provider routing options as JSON
44
- --web Enable the web-search plugin (id: web)
195
+ --provider <json|@file> Provider routing (order, only, sort, ...;
196
+ options: per-provider passthrough keyed by slug)
197
+ --modalities <csv> Output modalities: text,image,audio
198
+ --image-config <json|@file> Image output options, e.g. {"aspect_ratio":"16:9"}
199
+ --prediction <text|@file> Predicted output (prediction.content) to cut latency
200
+ --prompt-cache-key <k> Provider prompt-cache key
201
+ --metadata <json|@file> String key/value pairs (max 16)
202
+ ${CACHE_HELP} --cache-system Mark the system prompt as a cache breakpoint
203
+ --cache-user Mark the user prompt as a cache breakpoint
204
+
205
+ Plugins (ids: auto-router, moderation, web, web-fetch, file-parser,
206
+ response-healing, context-compression, pareto-router, fusion, switchyard-router,
207
+ jev-router, alignment; anything not covered below goes in --plugins):
208
+ --web Web-search plugin (id: web; deprecated upstream, the
209
+ newer path is --server-tool web_search)
45
210
  --web-max-results <n> Max web results
46
211
  --web-search-prompt <text> Custom web-search prompt
212
+ --web-engine <e> native | exa | firecrawl | parallel | perplexity
213
+ --web-domains <csv> Only search these domains (include_domains)
214
+ --web-exclude <csv> Never search these domains (exclude_domains)
215
+ --web-fetch Web-fetch plugin (id: web-fetch)
47
216
  --pdf-engine <e> PDF parser engine: mistral-ocr | pdf-text | native
48
- --plugins <json|@file> Full plugins array (merged with --web/--pdf-engine)
217
+ --response-healing Repair malformed JSON output (id: response-healing)
218
+ ${ALIGNMENT_HELP} --plugins <json|@file> Full plugins array (merged with the flags above)
219
+
220
+ Input / output:
49
221
  --image <url|path> Attach an image (repeatable). URL or local file
50
222
  --file <path> Attach a file/PDF (repeatable). Local path
51
- --cache-system Mark the system prompt as a cache breakpoint
52
- --cache-user Mark the user prompt as a cache breakpoint
53
- --extra <json|@file> Extra fields merged into the request body
223
+ ${REQUEST_FIELD_HELP} --extra <json|@file> Extra fields merged into the request body
54
224
  --body <json|@file> Fully-formed body (overrides all other flags; one-shot)
55
225
  --raw Print full JSON response (non-streaming; the REPL
56
226
  always streams, so it is ignored there)
@@ -67,21 +237,20 @@ async function readStdinIfPiped() {
67
237
  return data.trimEnd() || null;
68
238
  }
69
239
 
70
- async function loadJsonOrFile(value) {
71
- if (value.startsWith("@")) {
72
- const { readFile } = await import("node:fs/promises");
73
- const text = await readFile(value.slice(1), "utf8");
74
- return JSON.parse(text);
240
+ // loadJson plus chat's older convenience: a bare value that names a file is
241
+ // read as JSON before falling back to inline JSON.
242
+ async function loadJsonOrFile(value, flag) {
243
+ if (value.startsWith("@") || value.startsWith("{") || value.startsWith("[")) {
244
+ return loadJson(value, flag);
75
245
  }
76
- if (value.startsWith("{") || value.startsWith("[")) return JSON.parse(value);
77
- // Bare value: try it as a file path first, then as inline JSON.
246
+ let text;
78
247
  try {
79
248
  const { readFile } = await import("node:fs/promises");
80
- const text = await readFile(value, "utf8");
81
- return JSON.parse(text);
249
+ text = await readFile(value, "utf8");
82
250
  } catch {
83
- return JSON.parse(value);
251
+ return loadJson(value, flag);
84
252
  }
253
+ return loadJson(text, flag);
85
254
  }
86
255
 
87
256
  const FILE_MIME = {
@@ -131,6 +300,7 @@ const NUMERIC_SAMPLING = {
131
300
  "top-p": "top_p",
132
301
  "top-k": "top_k",
133
302
  "max-tokens": "max_tokens",
303
+ "max-completion-tokens": "max_completion_tokens",
134
304
  seed: "seed",
135
305
  "frequency-penalty": "frequency_penalty",
136
306
  "presence-penalty": "presence_penalty",
@@ -157,20 +327,32 @@ function applySamplingOptions(body, values) {
157
327
  if (Object.keys(reasoning).length) body.reasoning = reasoning;
158
328
  }
159
329
 
330
+ const csv = (v) =>
331
+ v
332
+ .split(",")
333
+ .map((s) => s.trim())
334
+ .filter(Boolean);
335
+
160
336
  async function buildPlugins(values) {
161
- const plugins = values.plugins ? await loadJsonOrFile(values.plugins) : [];
337
+ const plugins = values.plugins ? await loadJsonOrFile(values.plugins, "--plugins") : [];
162
338
  const list = Array.isArray(plugins) ? plugins : [plugins];
163
- if (values.web || values["web-max-results"] || values["web-search-prompt"]) {
339
+ const webFlags = ["web-max-results", "web-search-prompt", "web-engine", "web-domains"];
340
+ if (values.web || values["web-exclude"] || webFlags.some((f) => values[f])) {
164
341
  const web = { id: "web" };
165
342
  const n = numberOption(values["web-max-results"], "--web-max-results");
166
343
  if (n !== undefined) web.max_results = n;
167
344
  if (values["web-search-prompt"]) web.search_prompt = values["web-search-prompt"];
345
+ if (values["web-engine"]) web.engine = values["web-engine"];
346
+ if (values["web-domains"]) web.include_domains = csv(values["web-domains"]);
347
+ if (values["web-exclude"]) web.exclude_domains = csv(values["web-exclude"]);
168
348
  list.push(web);
169
349
  }
350
+ if (values["web-fetch"]) list.push({ id: "web-fetch" });
170
351
  if (values["pdf-engine"]) {
171
352
  list.push({ id: "file-parser", pdf: { engine: values["pdf-engine"] } });
172
353
  }
173
- return list;
354
+ if (values["response-healing"]) list.push({ id: "response-healing" });
355
+ return addAlignmentPlugin(list, values);
174
356
  }
175
357
 
176
358
  // Wrap a string into a single text content part so a cache_control breakpoint
@@ -205,7 +387,7 @@ async function applyRequestOptions(body, values) {
205
387
 
206
388
  if (values["json-output"]) body.response_format = { type: "json_object" };
207
389
  if (values.schema) {
208
- const schema = await loadJsonOrFile(values.schema);
390
+ const schema = await loadJsonOrFile(values.schema, "--schema");
209
391
  body.response_format = {
210
392
  type: "json_schema",
211
393
  json_schema: { name: "response", schema, strict: true },
@@ -214,20 +396,39 @@ async function applyRequestOptions(body, values) {
214
396
  if (values.tool && values.tool.length) {
215
397
  body.tools = [];
216
398
  for (const t of values.tool) {
217
- const def = await loadJsonOrFile(t);
218
- // Accept either {type, function} or a bare {name, parameters, ...}
219
- if (def.type && def.function) body.tools.push(def);
399
+ const def = await loadJsonOrFile(t, "--tool");
400
+ // Accept {type:"function", function}, a server tool such as
401
+ // {type:"openrouter:web_search"} (passed through as is), or a bare
402
+ // {name, parameters, ...} function definition.
403
+ if (def.type && (def.function || def.type !== "function")) body.tools.push(def);
220
404
  else body.tools.push({ type: "function", function: def });
221
405
  }
222
406
  }
407
+ await applyToolExtras(body, values);
223
408
  if (values["tool-choice"]) {
224
409
  const v = values["tool-choice"];
225
410
  if (["auto", "none", "required"].includes(v)) body.tool_choice = v;
226
411
  else body.tool_choice = { type: "function", function: { name: v } };
227
412
  }
228
413
  if (values["no-parallel-tools"]) body.parallel_tool_calls = false;
229
- if (values["logit-bias"]) body.logit_bias = await loadJsonOrFile(values["logit-bias"]);
230
- if (values.provider) body.provider = await loadJsonOrFile(values.provider);
414
+ if (values["logit-bias"])
415
+ body.logit_bias = await loadJsonOrFile(values["logit-bias"], "--logit-bias");
416
+ if (values.provider) body.provider = await loadJsonOrFile(values.provider, "--provider");
417
+ if (values.modalities) body.modalities = csv(values.modalities);
418
+ if (values["image-config"]) {
419
+ body.image_config = await loadJsonOrFile(values["image-config"], "--image-config");
420
+ }
421
+ if (values.prediction) {
422
+ const v = values.prediction;
423
+ const content = v.startsWith("@")
424
+ ? await (await import("node:fs/promises")).readFile(v.slice(1), "utf8")
425
+ : v;
426
+ body.prediction = { type: "content", content };
427
+ }
428
+ if (values["prompt-cache-key"]) body.prompt_cache_key = values["prompt-cache-key"];
429
+ if (values.metadata) body.metadata = await loadJsonOrFile(values.metadata, "--metadata");
430
+ applyCache(body, values);
431
+ await applyRequestFields(body, values);
231
432
 
232
433
  const plugins = await buildPlugins(values);
233
434
  if (plugins.length) body.plugins = plugins;
@@ -247,14 +448,14 @@ async function applyRequestOptions(body, values) {
247
448
  }
248
449
  }
249
450
 
250
- if (values.extra) Object.assign(body, await loadJsonOrFile(values.extra));
451
+ if (values.extra) Object.assign(body, await loadJsonOrFile(values.extra, "--extra"));
251
452
  }
252
453
 
253
454
  async function buildBody(values, prompt) {
254
455
  // --body is a full escape hatch: send it verbatim (still honors --extra).
255
456
  if (values.body) {
256
- const body = await loadJsonOrFile(values.body);
257
- if (values.extra) Object.assign(body, await loadJsonOrFile(values.extra));
457
+ const body = await loadJsonOrFile(values.body, "--body");
458
+ if (values.extra) Object.assign(body, await loadJsonOrFile(values.extra, "--extra"));
258
459
  return body;
259
460
  }
260
461
  const messages = [];
@@ -275,13 +476,17 @@ function printUsage(model, usage) {
275
476
  }
276
477
 
277
478
  async function streamResponse(body, auth) {
278
- const res = await api("POST", "/chat/completions", {
279
- auth,
280
- body: { ...body, stream: true },
281
- raw: true,
282
- headers: { Accept: "text/event-stream" },
283
- });
479
+ const res = await withAlignmentErrors(
480
+ api("POST", "/chat/completions", {
481
+ auth,
482
+ body: { ...body, stream: true },
483
+ raw: true,
484
+ headers: { Accept: "text/event-stream" },
485
+ }),
486
+ );
284
487
  let usage = null;
488
+ let alignment = null;
489
+ let images = 0;
285
490
  let model = null;
286
491
  let finishReason = null;
287
492
  let text = "";
@@ -289,6 +494,7 @@ async function streamResponse(body, auth) {
289
494
  assertNoStreamError(evt);
290
495
  if (evt.usage) usage = evt.usage;
291
496
  if (evt.model) model = evt.model;
497
+ if (evt.alignment) alignment = evt.alignment;
292
498
  const choice = evt.choices && evt.choices[0];
293
499
  if (!choice) continue;
294
500
  const delta = choice.delta || choice.message;
@@ -312,12 +518,17 @@ async function streamResponse(body, auth) {
312
518
  if (tc.function && tc.function.arguments) out(tc.function.arguments);
313
519
  }
314
520
  }
521
+ if (Array.isArray(delta.images)) images += delta.images.length;
315
522
  if (choice.finish_reason) finishReason = choice.finish_reason;
316
523
  }
317
524
  if (process.stdout.isTTY) out("\n");
525
+ if (images) info(imagesNote(images));
526
+ printAlignment(alignment);
318
527
  return { usage, model, finishReason, text };
319
528
  }
320
529
 
530
+ const imagesNote = (n) => `[${n} image(s) returned as data URLs; use --json to get them]`;
531
+
321
532
  export async function chatCommand(argv) {
322
533
  const { values, positionals } = parseArgs(argv, {
323
534
  model: { type: "string", short: "m" },
@@ -329,6 +540,7 @@ export async function chatCommand(argv) {
329
540
  "top-p": { type: "string" },
330
541
  "top-k": { type: "string" },
331
542
  "max-tokens": { type: "string" },
543
+ "max-completion-tokens": { type: "string" },
332
544
  seed: { type: "string" },
333
545
  stop: { type: "string" },
334
546
  "frequency-penalty": { type: "string" },
@@ -351,8 +563,18 @@ export async function chatCommand(argv) {
351
563
  web: { type: "boolean" },
352
564
  "web-max-results": { type: "string" },
353
565
  "web-search-prompt": { type: "string" },
566
+ "web-engine": { type: "string" },
567
+ "web-domains": { type: "string" },
568
+ "web-exclude": { type: "string" },
569
+ "web-fetch": { type: "boolean" },
354
570
  "pdf-engine": { type: "string" },
571
+ "response-healing": { type: "boolean" },
355
572
  plugins: { type: "string" },
573
+ modalities: { type: "string" },
574
+ "image-config": { type: "string" },
575
+ prediction: { type: "string" },
576
+ "prompt-cache-key": { type: "string" },
577
+ metadata: { type: "string" },
356
578
  image: { type: "string", multiple: true },
357
579
  file: { type: "string", multiple: true },
358
580
  "cache-system": { type: "boolean" },
@@ -362,6 +584,8 @@ export async function chatCommand(argv) {
362
584
  raw: { type: "boolean" },
363
585
  usage: { type: "boolean" },
364
586
  interactive: { type: "boolean", short: "i" },
587
+ ...SHARED_OPTIONS,
588
+ ...REQUEST_FIELD_OPTIONS,
365
589
  });
366
590
 
367
591
  if (values.help) {
@@ -384,28 +608,38 @@ export async function chatCommand(argv) {
384
608
  let prompt = positionals.join(" ").trim();
385
609
  if (piped) prompt = prompt ? `${prompt}\n\n${piped}` : piped;
386
610
 
387
- const wantsInteractive = values.interactive || (!prompt && stdin.isTTY && stdout.isTTY);
611
+ const wantsInteractive =
612
+ values.interactive || (!prompt && !values.body && stdin.isTTY && stdout.isTTY);
388
613
 
389
614
  if (wantsInteractive) {
390
615
  return repl(values, auth);
391
616
  }
392
617
 
393
- if (!prompt) {
618
+ // --body carries its own messages, so it needs no prompt.
619
+ if (!prompt && !values.body) {
394
620
  throw new Error("No prompt. Pass text, pipe via stdin, or use --interactive.");
395
621
  }
396
622
 
397
623
  const body = await buildBody(values, prompt);
398
624
 
399
625
  if (!shouldStream(values)) {
400
- const data = await api("POST", "/chat/completions", { auth, body });
626
+ const data = await withAlignmentErrors(api("POST", "/chat/completions", { auth, body }));
401
627
  if (isJsonMode() || values.raw) {
402
628
  printJSON(data);
403
629
  } else {
404
630
  const choice = data.choices && data.choices[0];
405
631
  const msg = choice && choice.message;
406
- if (msg && typeof msg.content === "string") outln(msg.content);
407
- else if (msg && Array.isArray(msg.content)) {
632
+ const images = msg && Array.isArray(msg.images) ? msg.images.length : 0;
633
+ if (images) info(imagesNote(images));
634
+ printAlignment(data.alignment);
635
+ if (msg && typeof msg.content === "string") {
636
+ if (msg.content || !images) outln(msg.content);
637
+ } else if (msg && Array.isArray(msg.content)) {
408
638
  for (const part of msg.content) if (part.type === "text") outln(part.text);
639
+ } else if (choice && choice.finish_reason === "length") {
640
+ process.stderr.write(
641
+ "warning: no output, hit max_tokens (reasoning models may spend the budget thinking)\n",
642
+ );
409
643
  } else outln(JSON.stringify(data));
410
644
  if (values.usage && data.usage) printUsage(data.model, data.usage);
411
645
  }
@@ -0,0 +1,75 @@
1
+ import { parseArgs, authFromValues, numberOption } from "../args.js";
2
+ import { api } from "../api.js";
3
+ import { printResult, writeBinaryOutput } from "../output.js";
4
+
5
+ const HELP = `Usage: openrouter containers <subcommand> [options]
6
+
7
+ Inspect files produced by OpenRouter's hosted shell/batch tools.
8
+
9
+ Subcommands:
10
+ list <container-id> [--limit <n>] [--after <file-id>]
11
+ get <container-id> <file-id>
12
+ download <container-id> <file-id> [-o file]
13
+ promote <container-id> <file-id> Promote into workspace Files
14
+ `;
15
+
16
+ function ids(positionals) {
17
+ if (positionals.length < 2) throw new Error("container-id and file-id required");
18
+ return positionals.slice(0, 2).map(encodeURIComponent);
19
+ }
20
+
21
+ export async function containersCommand(argv) {
22
+ const sub = argv[0];
23
+ const rest = argv.slice(1);
24
+ if (!sub || sub === "help" || sub === "-h" || sub === "--help") {
25
+ process.stdout.write(HELP);
26
+ return sub ? 0 : 1;
27
+ }
28
+ if (rest.includes("-h") || rest.includes("--help")) {
29
+ process.stdout.write(HELP);
30
+ return 0;
31
+ }
32
+
33
+ if (sub === "list") {
34
+ const { values, positionals } = parseArgs(rest, {
35
+ limit: { type: "string" },
36
+ after: { type: "string" },
37
+ });
38
+ if (!positionals[0]) throw new Error("container-id required");
39
+ const query = {};
40
+ if (values.limit !== undefined) query.limit = numberOption(values.limit, "--limit");
41
+ if (values.after) query.after = values.after;
42
+ const data = await api("GET", `/containers/${encodeURIComponent(positionals[0])}/files`, {
43
+ auth: authFromValues(values),
44
+ query,
45
+ });
46
+ printResult(data);
47
+ return 0;
48
+ }
49
+
50
+ if (sub === "get" || sub === "download" || sub === "promote") {
51
+ const { values, positionals } = parseArgs(rest, {
52
+ out: { type: "string", short: "o" },
53
+ });
54
+ const [containerId, fileId] = ids(positionals);
55
+ const path = `/containers/${containerId}/files/${fileId}`;
56
+ if (sub === "get") {
57
+ const data = await api("GET", path, { auth: authFromValues(values) });
58
+ printResult(data);
59
+ return 0;
60
+ }
61
+ if (sub === "promote") {
62
+ const data = await api("POST", `${path}/promote`, { auth: authFromValues(values) });
63
+ printResult(data);
64
+ return 0;
65
+ }
66
+ const bytes = await api("GET", `${path}/content`, {
67
+ auth: authFromValues(values),
68
+ binary: true,
69
+ });
70
+ await writeBinaryOutput(bytes, values.out || positionals[1]);
71
+ return 0;
72
+ }
73
+
74
+ throw new Error(`Unknown containers subcommand: ${sub}`);
75
+ }