@aaronjmars/openroutercli 0.3.0 → 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 +30 -9
- package/SKILL.md +86 -25
- package/package.json +1 -1
- package/src/api.js +12 -1
- package/src/args.js +48 -0
- package/src/cli.js +24 -8
- package/src/commands/account.js +48 -4
- package/src/commands/batches.js +360 -0
- package/src/commands/byok.js +114 -40
- package/src/commands/chat.js +277 -43
- package/src/commands/data.js +3 -3
- package/src/commands/decisions.js +46 -24
- package/src/commands/embed.js +35 -10
- package/src/commands/files.js +24 -8
- package/src/commands/generation.js +78 -9
- package/src/commands/guardrails.js +141 -11
- package/src/commands/image.js +43 -12
- package/src/commands/keys.js +59 -11
- package/src/commands/messages.js +217 -61
- package/src/commands/misc.js +76 -11
- package/src/commands/models.js +99 -16
- package/src/commands/observability.js +267 -0
- package/src/commands/presets.js +62 -3
- package/src/commands/rerank.js +28 -6
- package/src/commands/speech.js +80 -15
- package/src/commands/tools.js +102 -0
- package/src/commands/transcribe.js +113 -13
- package/src/commands/video.js +112 -14
- package/src/commands/workspaces.js +201 -6
- package/src/media.js +44 -0
package/src/commands/chat.js
CHANGED
|
@@ -1,12 +1,160 @@
|
|
|
1
1
|
import readline from "node:readline/promises";
|
|
2
2
|
import { stdin, stdout } from "node:process";
|
|
3
|
-
import {
|
|
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>
|
|
44
|
-
|
|
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
|
-
--
|
|
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
|
-
--
|
|
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
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
return
|
|
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
|
-
|
|
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
|
-
|
|
81
|
-
return JSON.parse(text);
|
|
249
|
+
text = await readFile(value, "utf8");
|
|
82
250
|
} catch {
|
|
83
|
-
return
|
|
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
|
-
|
|
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
|
-
|
|
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
|
|
219
|
-
|
|
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"])
|
|
230
|
-
|
|
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
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
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 =
|
|
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
|
-
|
|
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
|
-
|
|
407
|
-
|
|
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
|
}
|
package/src/commands/data.js
CHANGED
|
@@ -131,9 +131,9 @@ const DATASETS_HELP = `Usage: openrouter datasets <subcommand> [options]
|
|
|
131
131
|
Public OpenRouter usage datasets.
|
|
132
132
|
|
|
133
133
|
Subcommands:
|
|
134
|
-
apps [options]
|
|
135
|
-
daily [options]
|
|
136
|
-
session-cost [options]
|
|
134
|
+
apps [options] Top public apps by token usage
|
|
135
|
+
daily [options] Daily/weekly/monthly model rankings
|
|
136
|
+
session-cost [options] Cost per session by harness and model
|
|
137
137
|
|
|
138
138
|
apps options:
|
|
139
139
|
--category <name> --subcategory <name> --sort popular|trending
|
|
@@ -1,33 +1,44 @@
|
|
|
1
|
-
import { parseArgs, authFromValues } from "../args.js";
|
|
1
|
+
import { parseArgs, authFromValues, loadJson } from "../args.js";
|
|
2
2
|
import { api } from "../api.js";
|
|
3
|
+
import { mediaUrl } from "../media.js";
|
|
3
4
|
import { printResult } from "../output.js";
|
|
4
5
|
|
|
5
|
-
const HELP = `Usage: openrouter
|
|
6
|
+
const HELP = `Usage: openrouter systemone [options]
|
|
7
|
+
openrouter decisions [options] (same command)
|
|
6
8
|
|
|
7
|
-
Submit a
|
|
8
|
-
|
|
9
|
+
Submit a System One (Decisions) request: state plus typed questions (choice,
|
|
10
|
+
score, yes/no), answered by a System One model such as typesafe/jev-latest.
|
|
11
|
+
Used for routing, classification and other structured decisions.
|
|
9
12
|
|
|
10
13
|
Options:
|
|
11
|
-
-m, --model <id>
|
|
14
|
+
-m, --model <id> Model id, e.g. jev-latest (required without --body)
|
|
12
15
|
--state <json|text> State to evaluate (required without --body)
|
|
13
16
|
--questions <json> Questions object or @questions.json
|
|
17
|
+
--image <url|path> Add an image to the state (repeatable). Local files
|
|
18
|
+
are sent as data URLs. For multimodal models.
|
|
14
19
|
--body <json|@file> Fully-formed request body
|
|
15
20
|
--extra <json> Extra fields merged into the request
|
|
21
|
+
--alpha Use the older /api/alpha/decisions route
|
|
22
|
+
|
|
23
|
+
Example:
|
|
24
|
+
openrouter systemone -m jev-latest --state "Refund request for order 123" \\
|
|
25
|
+
--questions '{"route":{"type":"choice","options":["billing","support"]}}'
|
|
16
26
|
`;
|
|
17
27
|
|
|
18
|
-
async function
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
28
|
+
async function imagePart(value) {
|
|
29
|
+
return { type: "image_url", image_url: { url: await mediaUrl(value) } };
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
// State becomes an array of parts: a text state turns into a text part, an
|
|
33
|
+
// object stays one element, an array keeps its items; images go last.
|
|
34
|
+
export function withImages(state, parts) {
|
|
35
|
+
if (!parts.length) return state;
|
|
36
|
+
let base;
|
|
37
|
+
if (state === undefined || state === null || state === "") base = [];
|
|
38
|
+
else if (Array.isArray(state)) base = state;
|
|
39
|
+
else if (typeof state === "string") base = [{ type: "text", text: state }];
|
|
40
|
+
else base = [state];
|
|
41
|
+
return [...base, ...parts];
|
|
31
42
|
}
|
|
32
43
|
|
|
33
44
|
async function loadState(value) {
|
|
@@ -45,17 +56,20 @@ export async function decisionsCommand(argv) {
|
|
|
45
56
|
model: { type: "string", short: "m" },
|
|
46
57
|
state: { type: "string" },
|
|
47
58
|
questions: { type: "string" },
|
|
59
|
+
image: { type: "string", multiple: true },
|
|
48
60
|
body: { type: "string" },
|
|
49
61
|
extra: { type: "string" },
|
|
62
|
+
alpha: { type: "boolean" },
|
|
50
63
|
});
|
|
51
64
|
if (values.help) {
|
|
52
65
|
process.stdout.write(HELP);
|
|
53
66
|
return 0;
|
|
54
67
|
}
|
|
68
|
+
const images = values.image || [];
|
|
55
69
|
let body = values.body ? await loadJson(values.body, "--body") : null;
|
|
56
70
|
if (!body) {
|
|
57
71
|
if (!values.model) throw new Error("--model is required");
|
|
58
|
-
if (values.state === undefined) throw new Error("--state is required");
|
|
72
|
+
if (values.state === undefined && !images.length) throw new Error("--state is required");
|
|
59
73
|
if (!values.questions) throw new Error("--questions is required");
|
|
60
74
|
body = {
|
|
61
75
|
model: values.model,
|
|
@@ -63,12 +77,20 @@ export async function decisionsCommand(argv) {
|
|
|
63
77
|
questions: await loadJson(values.questions, "--questions"),
|
|
64
78
|
};
|
|
65
79
|
}
|
|
80
|
+
if (images.length) {
|
|
81
|
+
const parts = await Promise.all(images.map(imagePart));
|
|
82
|
+
body.state = withImages(body.state, parts);
|
|
83
|
+
}
|
|
66
84
|
if (values.extra) Object.assign(body, await loadJson(values.extra, "--extra"));
|
|
67
|
-
const data =
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
85
|
+
const data = values.alpha
|
|
86
|
+
? await api("POST", "/decisions", {
|
|
87
|
+
rootPath: "/api/alpha",
|
|
88
|
+
auth: authFromValues(values),
|
|
89
|
+
body,
|
|
90
|
+
})
|
|
91
|
+
: await api("POST", "/systemone", { auth: authFromValues(values), body });
|
|
72
92
|
printResult(data);
|
|
73
93
|
return 0;
|
|
74
94
|
}
|
|
95
|
+
|
|
96
|
+
export const systemoneCommand = decisionsCommand;
|