@aaronjmars/openroutercli 0.1.4 → 0.2.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 +40 -35
- package/SKILL.md +18 -17
- package/bin/openrouter.js +3 -3
- package/package.json +7 -2
- package/src/api.js +44 -22
- package/src/args.js +29 -14
- package/src/cli.js +60 -46
- package/src/commands/account.js +54 -46
- package/src/commands/byok.js +153 -0
- package/src/commands/chat.js +304 -154
- package/src/commands/embed.js +35 -34
- package/src/commands/files.js +102 -0
- package/src/commands/generation.js +10 -10
- package/src/commands/guardrails.js +115 -87
- package/src/commands/image.js +149 -0
- package/src/commands/keys.js +70 -55
- package/src/commands/login.js +101 -100
- package/src/commands/messages.js +143 -77
- package/src/commands/misc.js +51 -45
- package/src/commands/models.js +246 -149
- package/src/commands/presets.js +93 -0
- package/src/commands/request.js +21 -21
- package/src/commands/rerank.js +33 -25
- package/src/commands/speech.js +16 -24
- package/src/commands/transcribe.js +62 -0
- package/src/commands/video.js +58 -44
- package/src/commands/workspaces.js +60 -52
- package/src/config.js +17 -22
- package/src/output.js +13 -30
package/src/commands/chat.js
CHANGED
|
@@ -1,8 +1,11 @@
|
|
|
1
|
-
import readline from
|
|
2
|
-
import { stdin, stdout } from
|
|
3
|
-
import { parseArgs, authFromValues } from
|
|
4
|
-
import { api, sseStream } from
|
|
5
|
-
import { c, info, isJsonMode, out, outln, printJSON } from
|
|
1
|
+
import readline from "node:readline/promises";
|
|
2
|
+
import { stdin, stdout } from "node:process";
|
|
3
|
+
import { parseArgs, authFromValues, numberOption, shouldStream } from "../args.js";
|
|
4
|
+
import { api, assertNoStreamError, sseStream } from "../api.js";
|
|
5
|
+
import { c, info, isJsonMode, out, outln, printJSON } from "../output.js";
|
|
6
|
+
|
|
7
|
+
const DEFAULT_MODEL = "openrouter/auto";
|
|
8
|
+
const ERROR_FINISH_REASONS = ["error", "content_filter"];
|
|
6
9
|
|
|
7
10
|
const HELP = `Usage: openrouter chat [prompt...] [options]
|
|
8
11
|
|
|
@@ -10,7 +13,7 @@ Send a chat completion. With no prompt and a TTY, starts an interactive REPL.
|
|
|
10
13
|
With no prompt and stdin piped, reads the prompt from stdin.
|
|
11
14
|
|
|
12
15
|
Options:
|
|
13
|
-
-m, --model <id> Model id (default:
|
|
16
|
+
-m, --model <id> Model id (default: ${DEFAULT_MODEL})
|
|
14
17
|
--models <csv> Fallback list (comma-separated)
|
|
15
18
|
-s, --system <text> System message
|
|
16
19
|
--stream Stream tokens (default when TTY)
|
|
@@ -25,107 +28,187 @@ Options:
|
|
|
25
28
|
--presence-penalty <n> -2.0 to 2.0
|
|
26
29
|
--repetition-penalty <n> 0.0 to 2.0
|
|
27
30
|
--min-p <n> 0.0 to 1.0
|
|
31
|
+
--top-a <n> 0.0 to 1.0
|
|
32
|
+
--logprobs Return log probabilities
|
|
33
|
+
--top-logprobs <n> Return top-N logprobs per token (0-20)
|
|
34
|
+
--logit-bias <json|@file> Token bias map
|
|
28
35
|
--json-output Ask the model for JSON (response_format=json_object)
|
|
29
36
|
--schema <file|json> JSON schema for structured output
|
|
30
37
|
--tool <file|json> Tool definition (repeatable). JSON object or @file.json
|
|
31
38
|
--tool-choice <v> auto | none | required | <tool name>
|
|
32
|
-
--
|
|
39
|
+
--no-parallel-tools Disable parallel tool calls
|
|
40
|
+
--reasoning <effort> none|minimal|low|medium|high|xhigh|max
|
|
41
|
+
--reasoning-max-tokens <n> Reasoning token budget (Anthropic-style)
|
|
42
|
+
--reasoning-exclude Reason internally but omit reasoning from the output
|
|
33
43
|
--provider <json> Provider routing options as JSON
|
|
44
|
+
--web Enable the web-search plugin (id: web)
|
|
45
|
+
--web-max-results <n> Max web results
|
|
46
|
+
--web-search-prompt <text> Custom web-search prompt
|
|
47
|
+
--pdf-engine <e> PDF parser engine: mistral-ocr | pdf-text | native
|
|
48
|
+
--plugins <json|@file> Full plugins array (merged with --web/--pdf-engine)
|
|
34
49
|
--image <url|path> Attach an image (repeatable). URL or local file
|
|
35
|
-
--
|
|
50
|
+
--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
|
|
54
|
+
--body <json|@file> Fully-formed body (overrides all other flags; one-shot)
|
|
55
|
+
--raw Print full JSON response (non-streaming; the REPL
|
|
56
|
+
always streams, so it is ignored there)
|
|
36
57
|
--usage Print usage info to stderr after completion
|
|
37
|
-
--interactive, -i Force interactive REPL
|
|
58
|
+
--interactive, -i Force interactive REPL (needs a terminal).
|
|
59
|
+
--image applies to the opening turn only.
|
|
38
60
|
-h, --help
|
|
39
61
|
`;
|
|
40
62
|
|
|
41
63
|
async function readStdinIfPiped() {
|
|
42
64
|
if (stdin.isTTY) return null;
|
|
43
|
-
let data =
|
|
65
|
+
let data = "";
|
|
44
66
|
for await (const chunk of stdin) data += chunk;
|
|
45
67
|
return data.trimEnd() || null;
|
|
46
68
|
}
|
|
47
69
|
|
|
48
70
|
async function loadJsonOrFile(value) {
|
|
49
|
-
if (value.startsWith(
|
|
50
|
-
const { readFile } = await import(
|
|
51
|
-
const text = await readFile(value.slice(1),
|
|
71
|
+
if (value.startsWith("@")) {
|
|
72
|
+
const { readFile } = await import("node:fs/promises");
|
|
73
|
+
const text = await readFile(value.slice(1), "utf8");
|
|
52
74
|
return JSON.parse(text);
|
|
53
75
|
}
|
|
54
|
-
if (value.startsWith(
|
|
55
|
-
//
|
|
76
|
+
if (value.startsWith("{") || value.startsWith("[")) return JSON.parse(value);
|
|
77
|
+
// Bare value: try it as a file path first, then as inline JSON.
|
|
56
78
|
try {
|
|
57
|
-
const { readFile } = await import(
|
|
58
|
-
const text = await readFile(value,
|
|
79
|
+
const { readFile } = await import("node:fs/promises");
|
|
80
|
+
const text = await readFile(value, "utf8");
|
|
59
81
|
return JSON.parse(text);
|
|
60
82
|
} catch {
|
|
61
83
|
return JSON.parse(value);
|
|
62
84
|
}
|
|
63
85
|
}
|
|
64
86
|
|
|
87
|
+
const FILE_MIME = {
|
|
88
|
+
pdf: "application/pdf",
|
|
89
|
+
txt: "text/plain",
|
|
90
|
+
md: "text/markdown",
|
|
91
|
+
csv: "text/csv",
|
|
92
|
+
json: "application/json",
|
|
93
|
+
html: "text/html",
|
|
94
|
+
};
|
|
95
|
+
|
|
96
|
+
async function fileToContent(value) {
|
|
97
|
+
const { readFile } = await import("node:fs/promises");
|
|
98
|
+
const buf = await readFile(value);
|
|
99
|
+
const ext = (value.split(".").pop() || "").toLowerCase();
|
|
100
|
+
const mime = FILE_MIME[ext] || "application/octet-stream";
|
|
101
|
+
const filename = value.split("/").pop() || value;
|
|
102
|
+
return {
|
|
103
|
+
type: "file",
|
|
104
|
+
file: { filename, file_data: `data:${mime};base64,${buf.toString("base64")}` },
|
|
105
|
+
};
|
|
106
|
+
}
|
|
107
|
+
|
|
65
108
|
async function imageToContent(value) {
|
|
66
|
-
if (/^https?:\/\//i.test(value) || value.startsWith(
|
|
67
|
-
return { type:
|
|
109
|
+
if (/^https?:\/\//i.test(value) || value.startsWith("data:")) {
|
|
110
|
+
return { type: "image_url", image_url: { url: value } };
|
|
68
111
|
}
|
|
69
|
-
const { readFile } = await import(
|
|
112
|
+
const { readFile } = await import("node:fs/promises");
|
|
70
113
|
const buf = await readFile(value);
|
|
71
|
-
const ext = (value.split(
|
|
114
|
+
const ext = (value.split(".").pop() || "").toLowerCase();
|
|
72
115
|
const mime =
|
|
73
|
-
ext ===
|
|
74
|
-
?
|
|
75
|
-
: ext ===
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
116
|
+
ext === "png"
|
|
117
|
+
? "image/png"
|
|
118
|
+
: ext === "webp"
|
|
119
|
+
? "image/webp"
|
|
120
|
+
: ext === "gif"
|
|
121
|
+
? "image/gif"
|
|
122
|
+
: "image/jpeg";
|
|
80
123
|
return {
|
|
81
|
-
type:
|
|
82
|
-
image_url: { url: `data:${mime};base64,${buf.toString(
|
|
124
|
+
type: "image_url",
|
|
125
|
+
image_url: { url: `data:${mime};base64,${buf.toString("base64")}` },
|
|
83
126
|
};
|
|
84
127
|
}
|
|
85
128
|
|
|
129
|
+
const NUMERIC_SAMPLING = {
|
|
130
|
+
temperature: "temperature",
|
|
131
|
+
"top-p": "top_p",
|
|
132
|
+
"top-k": "top_k",
|
|
133
|
+
"max-tokens": "max_tokens",
|
|
134
|
+
seed: "seed",
|
|
135
|
+
"frequency-penalty": "frequency_penalty",
|
|
136
|
+
"presence-penalty": "presence_penalty",
|
|
137
|
+
"repetition-penalty": "repetition_penalty",
|
|
138
|
+
"min-p": "min_p",
|
|
139
|
+
"top-a": "top_a",
|
|
140
|
+
"top-logprobs": "top_logprobs",
|
|
141
|
+
};
|
|
142
|
+
|
|
86
143
|
function applySamplingOptions(body, values) {
|
|
87
|
-
if (values.models) body.models = values.models.split(
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
if (values.
|
|
93
|
-
if (values.
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
if (values
|
|
97
|
-
|
|
98
|
-
if (
|
|
144
|
+
if (values.models) body.models = values.models.split(",").map((s) => s.trim());
|
|
145
|
+
for (const [flag, field] of Object.entries(NUMERIC_SAMPLING)) {
|
|
146
|
+
const n = numberOption(values[flag], `--${flag}`);
|
|
147
|
+
if (n !== undefined) body[field] = n;
|
|
148
|
+
}
|
|
149
|
+
if (values.stop) body.stop = values.stop.split(",");
|
|
150
|
+
if (values.logprobs) body.logprobs = true;
|
|
151
|
+
|
|
152
|
+
const reasoning = {};
|
|
153
|
+
if (values.reasoning) reasoning.effort = values.reasoning;
|
|
154
|
+
const rmax = numberOption(values["reasoning-max-tokens"], "--reasoning-max-tokens");
|
|
155
|
+
if (rmax !== undefined) reasoning.max_tokens = rmax;
|
|
156
|
+
if (values["reasoning-exclude"]) reasoning.exclude = true;
|
|
157
|
+
if (Object.keys(reasoning).length) body.reasoning = reasoning;
|
|
99
158
|
}
|
|
100
159
|
|
|
101
|
-
async function
|
|
102
|
-
const
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
for (const img of images) parts.push(await imageToContent(img));
|
|
111
|
-
userContent = parts;
|
|
112
|
-
} else {
|
|
113
|
-
userContent = prompt;
|
|
160
|
+
async function buildPlugins(values) {
|
|
161
|
+
const plugins = values.plugins ? await loadJsonOrFile(values.plugins) : [];
|
|
162
|
+
const list = Array.isArray(plugins) ? plugins : [plugins];
|
|
163
|
+
if (values.web || values["web-max-results"] || values["web-search-prompt"]) {
|
|
164
|
+
const web = { id: "web" };
|
|
165
|
+
const n = numberOption(values["web-max-results"], "--web-max-results");
|
|
166
|
+
if (n !== undefined) web.max_results = n;
|
|
167
|
+
if (values["web-search-prompt"]) web.search_prompt = values["web-search-prompt"];
|
|
168
|
+
list.push(web);
|
|
114
169
|
}
|
|
115
|
-
|
|
170
|
+
if (values["pdf-engine"]) {
|
|
171
|
+
list.push({ id: "file-parser", pdf: { engine: values["pdf-engine"] } });
|
|
172
|
+
}
|
|
173
|
+
return list;
|
|
174
|
+
}
|
|
116
175
|
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
176
|
+
// Wrap a string into a single text content part so a cache_control breakpoint
|
|
177
|
+
// can be attached; content already in part form is returned untouched.
|
|
178
|
+
function markCache(content) {
|
|
179
|
+
if (typeof content === "string") {
|
|
180
|
+
return [{ type: "text", text: content, cache_control: { type: "ephemeral" } }];
|
|
181
|
+
}
|
|
182
|
+
if (Array.isArray(content) && content.length) {
|
|
183
|
+
const parts = content.map((p) => ({ ...p }));
|
|
184
|
+
parts[parts.length - 1].cache_control = { type: "ephemeral" };
|
|
185
|
+
return parts;
|
|
186
|
+
}
|
|
187
|
+
return content;
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
async function buildUserContent(values, prompt, withImages) {
|
|
191
|
+
const images = withImages ? values.image || [] : [];
|
|
192
|
+
const files = withImages ? values.file || [] : [];
|
|
193
|
+
if (images.length === 0 && files.length === 0) return prompt;
|
|
194
|
+
const parts = [];
|
|
195
|
+
if (prompt) parts.push({ type: "text", text: prompt });
|
|
196
|
+
for (const img of images) parts.push(await imageToContent(img));
|
|
197
|
+
for (const f of files) parts.push(await fileToContent(f));
|
|
198
|
+
return parts;
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
// Options that apply to the request whichever turn it is, so an interactive
|
|
202
|
+
// session is configured the same way a one-shot run is.
|
|
203
|
+
async function applyRequestOptions(body, values) {
|
|
121
204
|
applySamplingOptions(body, values);
|
|
122
205
|
|
|
123
|
-
if (values[
|
|
206
|
+
if (values["json-output"]) body.response_format = { type: "json_object" };
|
|
124
207
|
if (values.schema) {
|
|
125
208
|
const schema = await loadJsonOrFile(values.schema);
|
|
126
209
|
body.response_format = {
|
|
127
|
-
type:
|
|
128
|
-
json_schema: { name:
|
|
210
|
+
type: "json_schema",
|
|
211
|
+
json_schema: { name: "response", schema, strict: true },
|
|
129
212
|
};
|
|
130
213
|
}
|
|
131
214
|
if (values.tool && values.tool.length) {
|
|
@@ -134,43 +217,94 @@ async function buildBody(values, prompt) {
|
|
|
134
217
|
const def = await loadJsonOrFile(t);
|
|
135
218
|
// Accept either {type, function} or a bare {name, parameters, ...}
|
|
136
219
|
if (def.type && def.function) body.tools.push(def);
|
|
137
|
-
else body.tools.push({ type:
|
|
220
|
+
else body.tools.push({ type: "function", function: def });
|
|
138
221
|
}
|
|
139
222
|
}
|
|
140
|
-
if (values[
|
|
141
|
-
const v = values[
|
|
142
|
-
if ([
|
|
143
|
-
else body.tool_choice = { type:
|
|
223
|
+
if (values["tool-choice"]) {
|
|
224
|
+
const v = values["tool-choice"];
|
|
225
|
+
if (["auto", "none", "required"].includes(v)) body.tool_choice = v;
|
|
226
|
+
else body.tool_choice = { type: "function", function: { name: v } };
|
|
144
227
|
}
|
|
228
|
+
if (values["no-parallel-tools"]) body.parallel_tool_calls = false;
|
|
229
|
+
if (values["logit-bias"]) body.logit_bias = await loadJsonOrFile(values["logit-bias"]);
|
|
145
230
|
if (values.provider) body.provider = await loadJsonOrFile(values.provider);
|
|
231
|
+
|
|
232
|
+
const plugins = await buildPlugins(values);
|
|
233
|
+
if (plugins.length) body.plugins = plugins;
|
|
234
|
+
|
|
235
|
+
// cache_control breakpoints are attached to message content, so this must
|
|
236
|
+
// run after the messages are assembled.
|
|
237
|
+
if (values["cache-system"]) {
|
|
238
|
+
const sys = body.messages.find((m) => m.role === "system");
|
|
239
|
+
if (sys) sys.content = markCache(sys.content);
|
|
240
|
+
}
|
|
241
|
+
if (values["cache-user"]) {
|
|
242
|
+
for (let i = body.messages.length - 1; i >= 0; i--) {
|
|
243
|
+
if (body.messages[i].role === "user") {
|
|
244
|
+
body.messages[i].content = markCache(body.messages[i].content);
|
|
245
|
+
break;
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
if (values.extra) Object.assign(body, await loadJsonOrFile(values.extra));
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
async function buildBody(values, prompt) {
|
|
254
|
+
// --body is a full escape hatch: send it verbatim (still honors --extra).
|
|
255
|
+
if (values.body) {
|
|
256
|
+
const body = await loadJsonOrFile(values.body);
|
|
257
|
+
if (values.extra) Object.assign(body, await loadJsonOrFile(values.extra));
|
|
258
|
+
return body;
|
|
259
|
+
}
|
|
260
|
+
const messages = [];
|
|
261
|
+
if (values.system) messages.push({ role: "system", content: values.system });
|
|
262
|
+
messages.push({
|
|
263
|
+
role: "user",
|
|
264
|
+
content: await buildUserContent(values, prompt, true),
|
|
265
|
+
});
|
|
266
|
+
const body = { model: values.model || DEFAULT_MODEL, messages };
|
|
267
|
+
await applyRequestOptions(body, values);
|
|
146
268
|
return body;
|
|
147
269
|
}
|
|
148
270
|
|
|
271
|
+
function printUsage(model, usage) {
|
|
272
|
+
info(
|
|
273
|
+
`model=${model || ""} prompt=${usage.prompt_tokens} completion=${usage.completion_tokens} total=${usage.total_tokens}`,
|
|
274
|
+
);
|
|
275
|
+
}
|
|
276
|
+
|
|
149
277
|
async function streamResponse(body, auth) {
|
|
150
|
-
const res = await api(
|
|
278
|
+
const res = await api("POST", "/chat/completions", {
|
|
151
279
|
auth,
|
|
152
280
|
body: { ...body, stream: true },
|
|
153
281
|
raw: true,
|
|
154
|
-
headers: { Accept:
|
|
282
|
+
headers: { Accept: "text/event-stream" },
|
|
155
283
|
});
|
|
156
284
|
let usage = null;
|
|
157
285
|
let model = null;
|
|
158
286
|
let finishReason = null;
|
|
287
|
+
let text = "";
|
|
159
288
|
for await (const evt of sseStream(res)) {
|
|
289
|
+
assertNoStreamError(evt);
|
|
160
290
|
if (evt.usage) usage = evt.usage;
|
|
161
291
|
if (evt.model) model = evt.model;
|
|
162
292
|
const choice = evt.choices && evt.choices[0];
|
|
163
293
|
if (!choice) continue;
|
|
164
294
|
const delta = choice.delta || choice.message;
|
|
165
295
|
if (!delta) continue;
|
|
166
|
-
if (typeof delta.content ===
|
|
167
|
-
|
|
296
|
+
if (typeof delta.content === "string") {
|
|
297
|
+
out(delta.content);
|
|
298
|
+
text += delta.content;
|
|
299
|
+
} else if (Array.isArray(delta.content)) {
|
|
168
300
|
for (const part of delta.content) {
|
|
169
|
-
if (part.type ===
|
|
301
|
+
if (part.type === "text" && part.text) {
|
|
302
|
+
out(part.text);
|
|
303
|
+
text += part.text;
|
|
304
|
+
}
|
|
170
305
|
}
|
|
171
306
|
}
|
|
172
307
|
if (delta.reasoning) {
|
|
173
|
-
// reasoning trace; show in dim if TTY
|
|
174
308
|
if (process.stdout.isTTY) out(c.dim(delta.reasoning));
|
|
175
309
|
}
|
|
176
310
|
if (delta.tool_calls) {
|
|
@@ -180,37 +314,54 @@ async function streamResponse(body, auth) {
|
|
|
180
314
|
}
|
|
181
315
|
if (choice.finish_reason) finishReason = choice.finish_reason;
|
|
182
316
|
}
|
|
183
|
-
if (process.stdout.isTTY) out(
|
|
184
|
-
return { usage, model, finishReason };
|
|
317
|
+
if (process.stdout.isTTY) out("\n");
|
|
318
|
+
return { usage, model, finishReason, text };
|
|
185
319
|
}
|
|
186
320
|
|
|
187
321
|
export async function chatCommand(argv) {
|
|
188
322
|
const { values, positionals } = parseArgs(argv, {
|
|
189
|
-
model: { type:
|
|
190
|
-
models: { type:
|
|
191
|
-
system: { type:
|
|
192
|
-
stream: { type:
|
|
193
|
-
|
|
194
|
-
temperature: { type:
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
seed: { type:
|
|
199
|
-
stop: { type:
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
323
|
+
model: { type: "string", short: "m" },
|
|
324
|
+
models: { type: "string" },
|
|
325
|
+
system: { type: "string", short: "s" },
|
|
326
|
+
stream: { type: "boolean" },
|
|
327
|
+
"no-stream": { type: "boolean" },
|
|
328
|
+
temperature: { type: "string" },
|
|
329
|
+
"top-p": { type: "string" },
|
|
330
|
+
"top-k": { type: "string" },
|
|
331
|
+
"max-tokens": { type: "string" },
|
|
332
|
+
seed: { type: "string" },
|
|
333
|
+
stop: { type: "string" },
|
|
334
|
+
"frequency-penalty": { type: "string" },
|
|
335
|
+
"presence-penalty": { type: "string" },
|
|
336
|
+
"repetition-penalty": { type: "string" },
|
|
337
|
+
"min-p": { type: "string" },
|
|
338
|
+
"top-a": { type: "string" },
|
|
339
|
+
logprobs: { type: "boolean" },
|
|
340
|
+
"top-logprobs": { type: "string" },
|
|
341
|
+
"logit-bias": { type: "string" },
|
|
342
|
+
"json-output": { type: "boolean" },
|
|
343
|
+
schema: { type: "string" },
|
|
344
|
+
tool: { type: "string", multiple: true },
|
|
345
|
+
"tool-choice": { type: "string" },
|
|
346
|
+
"no-parallel-tools": { type: "boolean" },
|
|
347
|
+
reasoning: { type: "string" },
|
|
348
|
+
"reasoning-max-tokens": { type: "string" },
|
|
349
|
+
"reasoning-exclude": { type: "boolean" },
|
|
350
|
+
provider: { type: "string" },
|
|
351
|
+
web: { type: "boolean" },
|
|
352
|
+
"web-max-results": { type: "string" },
|
|
353
|
+
"web-search-prompt": { type: "string" },
|
|
354
|
+
"pdf-engine": { type: "string" },
|
|
355
|
+
plugins: { type: "string" },
|
|
356
|
+
image: { type: "string", multiple: true },
|
|
357
|
+
file: { type: "string", multiple: true },
|
|
358
|
+
"cache-system": { type: "boolean" },
|
|
359
|
+
"cache-user": { type: "boolean" },
|
|
360
|
+
extra: { type: "string" },
|
|
361
|
+
body: { type: "string" },
|
|
362
|
+
raw: { type: "boolean" },
|
|
363
|
+
usage: { type: "boolean" },
|
|
364
|
+
interactive: { type: "boolean", short: "i" },
|
|
214
365
|
});
|
|
215
366
|
|
|
216
367
|
if (values.help) {
|
|
@@ -219,54 +370,58 @@ export async function chatCommand(argv) {
|
|
|
219
370
|
}
|
|
220
371
|
|
|
221
372
|
const auth = authFromValues(values);
|
|
373
|
+
|
|
374
|
+
// Must precede readStdinIfPiped: that call drains stdin, which would leave
|
|
375
|
+
// the REPL's readline with an exhausted stream and no way to report why.
|
|
376
|
+
if (values.interactive && !stdin.isTTY) {
|
|
377
|
+
throw new Error(
|
|
378
|
+
"--interactive needs a terminal on stdin. To send piped input as a " +
|
|
379
|
+
'single prompt, drop -i: echo "hi" | openrouter chat',
|
|
380
|
+
);
|
|
381
|
+
}
|
|
382
|
+
|
|
222
383
|
const piped = await readStdinIfPiped();
|
|
223
|
-
let prompt = positionals.join(
|
|
384
|
+
let prompt = positionals.join(" ").trim();
|
|
224
385
|
if (piped) prompt = prompt ? `${prompt}\n\n${piped}` : piped;
|
|
225
386
|
|
|
226
|
-
const wantsInteractive =
|
|
227
|
-
values.interactive || (!prompt && stdin.isTTY && stdout.isTTY);
|
|
387
|
+
const wantsInteractive = values.interactive || (!prompt && stdin.isTTY && stdout.isTTY);
|
|
228
388
|
|
|
229
389
|
if (wantsInteractive) {
|
|
230
390
|
return repl(values, auth);
|
|
231
391
|
}
|
|
232
392
|
|
|
233
393
|
if (!prompt) {
|
|
234
|
-
throw new Error(
|
|
394
|
+
throw new Error("No prompt. Pass text, pipe via stdin, or use --interactive.");
|
|
235
395
|
}
|
|
236
396
|
|
|
237
397
|
const body = await buildBody(values, prompt);
|
|
238
398
|
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
values.stream ||
|
|
242
|
-
(!values['no-stream'] && !values.raw && !isJsonMode() && stdout.isTTY);
|
|
243
|
-
|
|
244
|
-
if (!shouldStream) {
|
|
245
|
-
const data = await api('POST', '/chat/completions', { auth, body });
|
|
399
|
+
if (!shouldStream(values)) {
|
|
400
|
+
const data = await api("POST", "/chat/completions", { auth, body });
|
|
246
401
|
if (isJsonMode() || values.raw) {
|
|
247
402
|
printJSON(data);
|
|
248
403
|
} else {
|
|
249
404
|
const choice = data.choices && data.choices[0];
|
|
250
405
|
const msg = choice && choice.message;
|
|
251
|
-
if (msg && typeof msg.content ===
|
|
406
|
+
if (msg && typeof msg.content === "string") outln(msg.content);
|
|
252
407
|
else if (msg && Array.isArray(msg.content)) {
|
|
253
|
-
for (const part of msg.content)
|
|
254
|
-
if (part.type === 'text') outln(part.text);
|
|
408
|
+
for (const part of msg.content) if (part.type === "text") outln(part.text);
|
|
255
409
|
} else outln(JSON.stringify(data));
|
|
256
|
-
if (values.usage && data.usage)
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
}
|
|
410
|
+
if (values.usage && data.usage) printUsage(data.model, data.usage);
|
|
411
|
+
}
|
|
412
|
+
const finish = data.choices && data.choices[0] && data.choices[0].finish_reason;
|
|
413
|
+
if (ERROR_FINISH_REASONS.includes(finish)) {
|
|
414
|
+
process.stderr.write(`error: generation stopped (${finish})\n`);
|
|
415
|
+
return 2;
|
|
261
416
|
}
|
|
262
417
|
return 0;
|
|
263
418
|
}
|
|
264
419
|
|
|
265
420
|
const meta = await streamResponse(body, auth);
|
|
266
|
-
if (values.usage && meta.usage)
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
421
|
+
if (values.usage && meta.usage) printUsage(meta.model, meta.usage);
|
|
422
|
+
if (ERROR_FINISH_REASONS.includes(meta.finishReason)) {
|
|
423
|
+
process.stderr.write(`error: generation stopped (${meta.finishReason})\n`);
|
|
424
|
+
return 2;
|
|
270
425
|
}
|
|
271
426
|
return 0;
|
|
272
427
|
}
|
|
@@ -274,55 +429,51 @@ export async function chatCommand(argv) {
|
|
|
274
429
|
async function repl(values, auth) {
|
|
275
430
|
const rl = readline.createInterface({ input: stdin, output: stdout });
|
|
276
431
|
const history = [];
|
|
277
|
-
if (values.system) history.push({ role:
|
|
278
|
-
outln(c.dim(
|
|
279
|
-
let model = values.model ||
|
|
432
|
+
if (values.system) history.push({ role: "system", content: values.system });
|
|
433
|
+
outln(c.dim("OpenRouter chat. /exit to quit, /reset to clear history, /model <id> to switch."));
|
|
434
|
+
let model = values.model || DEFAULT_MODEL;
|
|
280
435
|
outln(c.dim(`model: ${model}`));
|
|
436
|
+
let firstTurn = true;
|
|
281
437
|
try {
|
|
282
438
|
while (true) {
|
|
283
439
|
let line;
|
|
284
440
|
try {
|
|
285
|
-
line = await rl.question(c.cyan(
|
|
441
|
+
line = await rl.question(c.cyan("› "));
|
|
286
442
|
} catch {
|
|
287
443
|
return 0;
|
|
288
444
|
}
|
|
289
445
|
if (line == null) return 0;
|
|
290
446
|
const trimmed = line.trim();
|
|
291
447
|
if (!trimmed) continue;
|
|
292
|
-
if (trimmed ===
|
|
293
|
-
if (trimmed ===
|
|
448
|
+
if (trimmed === "/exit" || trimmed === "/quit") return 0;
|
|
449
|
+
if (trimmed === "/reset") {
|
|
294
450
|
history.length = 0;
|
|
295
|
-
if (values.system)
|
|
296
|
-
|
|
297
|
-
outln(c.dim(
|
|
451
|
+
if (values.system) history.push({ role: "system", content: values.system });
|
|
452
|
+
firstTurn = true;
|
|
453
|
+
outln(c.dim("(history cleared)"));
|
|
298
454
|
continue;
|
|
299
455
|
}
|
|
300
|
-
if (trimmed.startsWith(
|
|
456
|
+
if (trimmed.startsWith("/model ")) {
|
|
301
457
|
model = trimmed.slice(7).trim();
|
|
302
458
|
outln(c.dim(`model: ${model}`));
|
|
303
459
|
continue;
|
|
304
460
|
}
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
461
|
+
// --image attaches to the opening turn only; resending it every turn
|
|
462
|
+
// would re-upload the same attachment.
|
|
463
|
+
history.push({
|
|
464
|
+
role: "user",
|
|
465
|
+
content: await buildUserContent(values, trimmed, firstTurn),
|
|
466
|
+
});
|
|
467
|
+
const body = { model, messages: history };
|
|
468
|
+
await applyRequestOptions(body, values);
|
|
308
469
|
try {
|
|
309
|
-
const
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
let assistant = '';
|
|
316
|
-
for await (const evt of sseStream(res)) {
|
|
317
|
-
const delta = evt.choices?.[0]?.delta;
|
|
318
|
-
if (delta?.content) {
|
|
319
|
-
out(delta.content);
|
|
320
|
-
assistant += delta.content;
|
|
321
|
-
}
|
|
322
|
-
if (delta?.reasoning && process.stdout.isTTY) out(c.dim(delta.reasoning));
|
|
470
|
+
const meta = await streamResponse(body, auth);
|
|
471
|
+
history.push({ role: "assistant", content: meta.text });
|
|
472
|
+
firstTurn = false;
|
|
473
|
+
if (values.usage && meta.usage) printUsage(meta.model, meta.usage);
|
|
474
|
+
if (ERROR_FINISH_REASONS.includes(meta.finishReason)) {
|
|
475
|
+
outln(c.red(`generation stopped (${meta.finishReason})`));
|
|
323
476
|
}
|
|
324
|
-
out('\n');
|
|
325
|
-
history.push({ role: 'assistant', content: assistant });
|
|
326
477
|
} catch (err) {
|
|
327
478
|
outln(c.red(err.message));
|
|
328
479
|
// Drop the orphaned user message so the next turn doesn't send a
|
|
@@ -335,5 +486,4 @@ async function repl(values, auth) {
|
|
|
335
486
|
}
|
|
336
487
|
}
|
|
337
488
|
|
|
338
|
-
// Alias used by `complete`
|
|
339
489
|
export const completeCommand = chatCommand;
|