@aaronjmars/openroutercli 0.1.6 → 0.2.1
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 +4 -0
- package/SKILL.md +79 -14
- package/bin/openrouter.js +3 -3
- package/package.json +7 -2
- package/src/api.js +36 -25
- package/src/args.js +17 -18
- package/src/cli.js +53 -35
- package/src/commands/account.js +45 -43
- package/src/commands/byok.js +153 -0
- package/src/commands/chat.js +228 -107
- package/src/commands/embed.js +31 -26
- package/src/commands/files.js +102 -0
- package/src/commands/generation.js +10 -10
- package/src/commands/guardrails.js +98 -79
- package/src/commands/image.js +149 -0
- package/src/commands/keys.js +61 -55
- package/src/commands/login.js +84 -88
- package/src/commands/messages.js +119 -55
- package/src/commands/misc.js +45 -45
- package/src/commands/models.js +215 -139
- package/src/commands/presets.js +93 -0
- package/src/commands/request.js +20 -20
- package/src/commands/rerank.js +33 -25
- package/src/commands/speech.js +16 -16
- package/src/commands/transcribe.js +62 -0
- package/src/commands/video.js +45 -42
- package/src/commands/workspaces.js +49 -45
- package/src/config.js +17 -23
- package/src/output.js +14 -27
package/src/commands/chat.js
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
import readline from
|
|
2
|
-
import { stdin, stdout } from
|
|
3
|
-
import { parseArgs, authFromValues, numberOption, shouldStream } from
|
|
4
|
-
import { api, assertNoStreamError, 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
6
|
|
|
7
|
-
const DEFAULT_MODEL =
|
|
8
|
-
const ERROR_FINISH_REASONS = [
|
|
7
|
+
const DEFAULT_MODEL = "openrouter/auto";
|
|
8
|
+
const ERROR_FINISH_REASONS = ["error", "content_filter"];
|
|
9
9
|
|
|
10
10
|
const HELP = `Usage: openrouter chat [prompt...] [options]
|
|
11
11
|
|
|
@@ -28,13 +28,30 @@ Options:
|
|
|
28
28
|
--presence-penalty <n> -2.0 to 2.0
|
|
29
29
|
--repetition-penalty <n> 0.0 to 2.0
|
|
30
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
|
|
31
35
|
--json-output Ask the model for JSON (response_format=json_object)
|
|
32
36
|
--schema <file|json> JSON schema for structured output
|
|
33
37
|
--tool <file|json> Tool definition (repeatable). JSON object or @file.json
|
|
34
38
|
--tool-choice <v> auto | none | required | <tool name>
|
|
35
|
-
--
|
|
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
|
|
36
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)
|
|
37
49
|
--image <url|path> Attach an image (repeatable). URL or local file
|
|
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)
|
|
38
55
|
--raw Print full JSON response (non-streaming; the REPL
|
|
39
56
|
always streams, so it is ignored there)
|
|
40
57
|
--usage Print usage info to stderr after completion
|
|
@@ -45,77 +62,139 @@ Options:
|
|
|
45
62
|
|
|
46
63
|
async function readStdinIfPiped() {
|
|
47
64
|
if (stdin.isTTY) return null;
|
|
48
|
-
let data =
|
|
65
|
+
let data = "";
|
|
49
66
|
for await (const chunk of stdin) data += chunk;
|
|
50
67
|
return data.trimEnd() || null;
|
|
51
68
|
}
|
|
52
69
|
|
|
53
70
|
async function loadJsonOrFile(value) {
|
|
54
|
-
if (value.startsWith(
|
|
55
|
-
const { readFile } = await import(
|
|
56
|
-
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");
|
|
57
74
|
return JSON.parse(text);
|
|
58
75
|
}
|
|
59
|
-
if (value.startsWith(
|
|
76
|
+
if (value.startsWith("{") || value.startsWith("[")) return JSON.parse(value);
|
|
60
77
|
// Bare value: try it as a file path first, then as inline JSON.
|
|
61
78
|
try {
|
|
62
|
-
const { readFile } = await import(
|
|
63
|
-
const text = await readFile(value,
|
|
79
|
+
const { readFile } = await import("node:fs/promises");
|
|
80
|
+
const text = await readFile(value, "utf8");
|
|
64
81
|
return JSON.parse(text);
|
|
65
82
|
} catch {
|
|
66
83
|
return JSON.parse(value);
|
|
67
84
|
}
|
|
68
85
|
}
|
|
69
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
|
+
|
|
70
108
|
async function imageToContent(value) {
|
|
71
|
-
if (/^https?:\/\//i.test(value) || value.startsWith(
|
|
72
|
-
return { type:
|
|
109
|
+
if (/^https?:\/\//i.test(value) || value.startsWith("data:")) {
|
|
110
|
+
return { type: "image_url", image_url: { url: value } };
|
|
73
111
|
}
|
|
74
|
-
const { readFile } = await import(
|
|
112
|
+
const { readFile } = await import("node:fs/promises");
|
|
75
113
|
const buf = await readFile(value);
|
|
76
|
-
const ext = (value.split(
|
|
114
|
+
const ext = (value.split(".").pop() || "").toLowerCase();
|
|
77
115
|
const mime =
|
|
78
|
-
ext ===
|
|
79
|
-
?
|
|
80
|
-
: ext ===
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
116
|
+
ext === "png"
|
|
117
|
+
? "image/png"
|
|
118
|
+
: ext === "webp"
|
|
119
|
+
? "image/webp"
|
|
120
|
+
: ext === "gif"
|
|
121
|
+
? "image/gif"
|
|
122
|
+
: "image/jpeg";
|
|
85
123
|
return {
|
|
86
|
-
type:
|
|
87
|
-
image_url: { url: `data:${mime};base64,${buf.toString(
|
|
124
|
+
type: "image_url",
|
|
125
|
+
image_url: { url: `data:${mime};base64,${buf.toString("base64")}` },
|
|
88
126
|
};
|
|
89
127
|
}
|
|
90
128
|
|
|
91
129
|
const NUMERIC_SAMPLING = {
|
|
92
|
-
temperature:
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
seed:
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
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",
|
|
101
141
|
};
|
|
102
142
|
|
|
103
143
|
function applySamplingOptions(body, values) {
|
|
104
|
-
if (values.models) body.models = values.models.split(
|
|
144
|
+
if (values.models) body.models = values.models.split(",").map((s) => s.trim());
|
|
105
145
|
for (const [flag, field] of Object.entries(NUMERIC_SAMPLING)) {
|
|
106
146
|
const n = numberOption(values[flag], `--${flag}`);
|
|
107
147
|
if (n !== undefined) body[field] = n;
|
|
108
148
|
}
|
|
109
|
-
if (values.stop) body.stop = values.stop.split(
|
|
110
|
-
if (values.
|
|
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;
|
|
158
|
+
}
|
|
159
|
+
|
|
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);
|
|
169
|
+
}
|
|
170
|
+
if (values["pdf-engine"]) {
|
|
171
|
+
list.push({ id: "file-parser", pdf: { engine: values["pdf-engine"] } });
|
|
172
|
+
}
|
|
173
|
+
return list;
|
|
174
|
+
}
|
|
175
|
+
|
|
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;
|
|
111
188
|
}
|
|
112
189
|
|
|
113
190
|
async function buildUserContent(values, prompt, withImages) {
|
|
114
191
|
const images = withImages ? values.image || [] : [];
|
|
115
|
-
|
|
192
|
+
const files = withImages ? values.file || [] : [];
|
|
193
|
+
if (images.length === 0 && files.length === 0) return prompt;
|
|
116
194
|
const parts = [];
|
|
117
|
-
if (prompt) parts.push({ type:
|
|
195
|
+
if (prompt) parts.push({ type: "text", text: prompt });
|
|
118
196
|
for (const img of images) parts.push(await imageToContent(img));
|
|
197
|
+
for (const f of files) parts.push(await fileToContent(f));
|
|
119
198
|
return parts;
|
|
120
199
|
}
|
|
121
200
|
|
|
@@ -124,12 +203,12 @@ async function buildUserContent(values, prompt, withImages) {
|
|
|
124
203
|
async function applyRequestOptions(body, values) {
|
|
125
204
|
applySamplingOptions(body, values);
|
|
126
205
|
|
|
127
|
-
if (values[
|
|
206
|
+
if (values["json-output"]) body.response_format = { type: "json_object" };
|
|
128
207
|
if (values.schema) {
|
|
129
208
|
const schema = await loadJsonOrFile(values.schema);
|
|
130
209
|
body.response_format = {
|
|
131
|
-
type:
|
|
132
|
-
json_schema: { name:
|
|
210
|
+
type: "json_schema",
|
|
211
|
+
json_schema: { name: "response", schema, strict: true },
|
|
133
212
|
};
|
|
134
213
|
}
|
|
135
214
|
if (values.tool && values.tool.length) {
|
|
@@ -138,23 +217,51 @@ async function applyRequestOptions(body, values) {
|
|
|
138
217
|
const def = await loadJsonOrFile(t);
|
|
139
218
|
// Accept either {type, function} or a bare {name, parameters, ...}
|
|
140
219
|
if (def.type && def.function) body.tools.push(def);
|
|
141
|
-
else body.tools.push({ type:
|
|
220
|
+
else body.tools.push({ type: "function", function: def });
|
|
142
221
|
}
|
|
143
222
|
}
|
|
144
|
-
if (values[
|
|
145
|
-
const v = values[
|
|
146
|
-
if ([
|
|
147
|
-
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 } };
|
|
148
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"]);
|
|
149
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));
|
|
150
251
|
}
|
|
151
252
|
|
|
152
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
|
+
}
|
|
153
260
|
const messages = [];
|
|
154
|
-
if (values.system) messages.push({ role:
|
|
261
|
+
if (values.system) messages.push({ role: "system", content: values.system });
|
|
155
262
|
messages.push({
|
|
156
|
-
role:
|
|
157
|
-
content: await buildUserContent(values, prompt, true)
|
|
263
|
+
role: "user",
|
|
264
|
+
content: await buildUserContent(values, prompt, true),
|
|
158
265
|
});
|
|
159
266
|
const body = { model: values.model || DEFAULT_MODEL, messages };
|
|
160
267
|
await applyRequestOptions(body, values);
|
|
@@ -163,21 +270,21 @@ async function buildBody(values, prompt) {
|
|
|
163
270
|
|
|
164
271
|
function printUsage(model, usage) {
|
|
165
272
|
info(
|
|
166
|
-
`model=${model ||
|
|
273
|
+
`model=${model || ""} prompt=${usage.prompt_tokens} completion=${usage.completion_tokens} total=${usage.total_tokens}`,
|
|
167
274
|
);
|
|
168
275
|
}
|
|
169
276
|
|
|
170
277
|
async function streamResponse(body, auth) {
|
|
171
|
-
const res = await api(
|
|
278
|
+
const res = await api("POST", "/chat/completions", {
|
|
172
279
|
auth,
|
|
173
280
|
body: { ...body, stream: true },
|
|
174
281
|
raw: true,
|
|
175
|
-
headers: { Accept:
|
|
282
|
+
headers: { Accept: "text/event-stream" },
|
|
176
283
|
});
|
|
177
284
|
let usage = null;
|
|
178
285
|
let model = null;
|
|
179
286
|
let finishReason = null;
|
|
180
|
-
let text =
|
|
287
|
+
let text = "";
|
|
181
288
|
for await (const evt of sseStream(res)) {
|
|
182
289
|
assertNoStreamError(evt);
|
|
183
290
|
if (evt.usage) usage = evt.usage;
|
|
@@ -186,12 +293,12 @@ async function streamResponse(body, auth) {
|
|
|
186
293
|
if (!choice) continue;
|
|
187
294
|
const delta = choice.delta || choice.message;
|
|
188
295
|
if (!delta) continue;
|
|
189
|
-
if (typeof delta.content ===
|
|
296
|
+
if (typeof delta.content === "string") {
|
|
190
297
|
out(delta.content);
|
|
191
298
|
text += delta.content;
|
|
192
299
|
} else if (Array.isArray(delta.content)) {
|
|
193
300
|
for (const part of delta.content) {
|
|
194
|
-
if (part.type ===
|
|
301
|
+
if (part.type === "text" && part.text) {
|
|
195
302
|
out(part.text);
|
|
196
303
|
text += part.text;
|
|
197
304
|
}
|
|
@@ -207,37 +314,54 @@ async function streamResponse(body, auth) {
|
|
|
207
314
|
}
|
|
208
315
|
if (choice.finish_reason) finishReason = choice.finish_reason;
|
|
209
316
|
}
|
|
210
|
-
if (process.stdout.isTTY) out(
|
|
317
|
+
if (process.stdout.isTTY) out("\n");
|
|
211
318
|
return { usage, model, finishReason, text };
|
|
212
319
|
}
|
|
213
320
|
|
|
214
321
|
export async function chatCommand(argv) {
|
|
215
322
|
const { values, positionals } = parseArgs(argv, {
|
|
216
|
-
model: { type:
|
|
217
|
-
models: { type:
|
|
218
|
-
system: { type:
|
|
219
|
-
stream: { type:
|
|
220
|
-
|
|
221
|
-
temperature: { type:
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
seed: { type:
|
|
226
|
-
stop: { type:
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
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" },
|
|
241
365
|
});
|
|
242
366
|
|
|
243
367
|
if (values.help) {
|
|
@@ -251,39 +375,37 @@ export async function chatCommand(argv) {
|
|
|
251
375
|
// the REPL's readline with an exhausted stream and no way to report why.
|
|
252
376
|
if (values.interactive && !stdin.isTTY) {
|
|
253
377
|
throw new Error(
|
|
254
|
-
|
|
255
|
-
'single prompt, drop -i: echo "hi" | openrouter chat'
|
|
378
|
+
"--interactive needs a terminal on stdin. To send piped input as a " +
|
|
379
|
+
'single prompt, drop -i: echo "hi" | openrouter chat',
|
|
256
380
|
);
|
|
257
381
|
}
|
|
258
382
|
|
|
259
383
|
const piped = await readStdinIfPiped();
|
|
260
|
-
let prompt = positionals.join(
|
|
384
|
+
let prompt = positionals.join(" ").trim();
|
|
261
385
|
if (piped) prompt = prompt ? `${prompt}\n\n${piped}` : piped;
|
|
262
386
|
|
|
263
|
-
const wantsInteractive =
|
|
264
|
-
values.interactive || (!prompt && stdin.isTTY && stdout.isTTY);
|
|
387
|
+
const wantsInteractive = values.interactive || (!prompt && stdin.isTTY && stdout.isTTY);
|
|
265
388
|
|
|
266
389
|
if (wantsInteractive) {
|
|
267
390
|
return repl(values, auth);
|
|
268
391
|
}
|
|
269
392
|
|
|
270
393
|
if (!prompt) {
|
|
271
|
-
throw new Error(
|
|
394
|
+
throw new Error("No prompt. Pass text, pipe via stdin, or use --interactive.");
|
|
272
395
|
}
|
|
273
396
|
|
|
274
397
|
const body = await buildBody(values, prompt);
|
|
275
398
|
|
|
276
399
|
if (!shouldStream(values)) {
|
|
277
|
-
const data = await api(
|
|
400
|
+
const data = await api("POST", "/chat/completions", { auth, body });
|
|
278
401
|
if (isJsonMode() || values.raw) {
|
|
279
402
|
printJSON(data);
|
|
280
403
|
} else {
|
|
281
404
|
const choice = data.choices && data.choices[0];
|
|
282
405
|
const msg = choice && choice.message;
|
|
283
|
-
if (msg && typeof msg.content ===
|
|
406
|
+
if (msg && typeof msg.content === "string") outln(msg.content);
|
|
284
407
|
else if (msg && Array.isArray(msg.content)) {
|
|
285
|
-
for (const part of msg.content)
|
|
286
|
-
if (part.type === 'text') outln(part.text);
|
|
408
|
+
for (const part of msg.content) if (part.type === "text") outln(part.text);
|
|
287
409
|
} else outln(JSON.stringify(data));
|
|
288
410
|
if (values.usage && data.usage) printUsage(data.model, data.usage);
|
|
289
411
|
}
|
|
@@ -307,8 +429,8 @@ export async function chatCommand(argv) {
|
|
|
307
429
|
async function repl(values, auth) {
|
|
308
430
|
const rl = readline.createInterface({ input: stdin, output: stdout });
|
|
309
431
|
const history = [];
|
|
310
|
-
if (values.system) history.push({ role:
|
|
311
|
-
outln(c.dim(
|
|
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."));
|
|
312
434
|
let model = values.model || DEFAULT_MODEL;
|
|
313
435
|
outln(c.dim(`model: ${model}`));
|
|
314
436
|
let firstTurn = true;
|
|
@@ -316,23 +438,22 @@ async function repl(values, auth) {
|
|
|
316
438
|
while (true) {
|
|
317
439
|
let line;
|
|
318
440
|
try {
|
|
319
|
-
line = await rl.question(c.cyan(
|
|
441
|
+
line = await rl.question(c.cyan("› "));
|
|
320
442
|
} catch {
|
|
321
443
|
return 0;
|
|
322
444
|
}
|
|
323
445
|
if (line == null) return 0;
|
|
324
446
|
const trimmed = line.trim();
|
|
325
447
|
if (!trimmed) continue;
|
|
326
|
-
if (trimmed ===
|
|
327
|
-
if (trimmed ===
|
|
448
|
+
if (trimmed === "/exit" || trimmed === "/quit") return 0;
|
|
449
|
+
if (trimmed === "/reset") {
|
|
328
450
|
history.length = 0;
|
|
329
|
-
if (values.system)
|
|
330
|
-
history.push({ role: 'system', content: values.system });
|
|
451
|
+
if (values.system) history.push({ role: "system", content: values.system });
|
|
331
452
|
firstTurn = true;
|
|
332
|
-
outln(c.dim(
|
|
453
|
+
outln(c.dim("(history cleared)"));
|
|
333
454
|
continue;
|
|
334
455
|
}
|
|
335
|
-
if (trimmed.startsWith(
|
|
456
|
+
if (trimmed.startsWith("/model ")) {
|
|
336
457
|
model = trimmed.slice(7).trim();
|
|
337
458
|
outln(c.dim(`model: ${model}`));
|
|
338
459
|
continue;
|
|
@@ -340,14 +461,14 @@ async function repl(values, auth) {
|
|
|
340
461
|
// --image attaches to the opening turn only; resending it every turn
|
|
341
462
|
// would re-upload the same attachment.
|
|
342
463
|
history.push({
|
|
343
|
-
role:
|
|
344
|
-
content: await buildUserContent(values, trimmed, firstTurn)
|
|
464
|
+
role: "user",
|
|
465
|
+
content: await buildUserContent(values, trimmed, firstTurn),
|
|
345
466
|
});
|
|
346
467
|
const body = { model, messages: history };
|
|
347
468
|
await applyRequestOptions(body, values);
|
|
348
469
|
try {
|
|
349
470
|
const meta = await streamResponse(body, auth);
|
|
350
|
-
history.push({ role:
|
|
471
|
+
history.push({ role: "assistant", content: meta.text });
|
|
351
472
|
firstTurn = false;
|
|
352
473
|
if (values.usage && meta.usage) printUsage(meta.model, meta.usage);
|
|
353
474
|
if (ERROR_FINISH_REASONS.includes(meta.finishReason)) {
|
package/src/commands/embed.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { parseArgs, authFromValues, numberOption, readStdinIfPiped } from
|
|
2
|
-
import { api } from
|
|
3
|
-
import { outln, pricePerMillion, printResult, table } from
|
|
1
|
+
import { parseArgs, authFromValues, numberOption, readStdinIfPiped } from "../args.js";
|
|
2
|
+
import { api } from "../api.js";
|
|
3
|
+
import { outln, pricePerMillion, printResult, table } from "../output.js";
|
|
4
4
|
|
|
5
5
|
const HELP = `Usage: openrouter embed [text...] [options]
|
|
6
6
|
openrouter embed models
|
|
@@ -17,70 +17,75 @@ Options:
|
|
|
17
17
|
`;
|
|
18
18
|
|
|
19
19
|
export async function embedCommand(argv) {
|
|
20
|
-
if (argv[0] ===
|
|
20
|
+
if (argv[0] === "models") {
|
|
21
21
|
const { values } = parseArgs(argv.slice(1), {});
|
|
22
22
|
if (values.help) {
|
|
23
23
|
process.stdout.write(HELP);
|
|
24
24
|
return 0;
|
|
25
25
|
}
|
|
26
|
-
const data = await api(
|
|
26
|
+
const data = await api("GET", "/embeddings/models", {
|
|
27
27
|
auth: authFromValues(values),
|
|
28
|
-
requireAuth: false
|
|
28
|
+
requireAuth: false,
|
|
29
29
|
});
|
|
30
30
|
printResult(data, () => {
|
|
31
31
|
const rows = data.data || [];
|
|
32
32
|
table(rows, [
|
|
33
|
-
{ label:
|
|
34
|
-
{ label:
|
|
33
|
+
{ label: "id", value: (m) => m.id },
|
|
34
|
+
{ label: "context", value: (m) => m.context_length ?? "" },
|
|
35
35
|
{
|
|
36
|
-
label:
|
|
37
|
-
value: (m) => pricePerMillion((m.pricing || {}).prompt)
|
|
36
|
+
label: "$/M tokens",
|
|
37
|
+
value: (m) => pricePerMillion((m.pricing || {}).prompt),
|
|
38
38
|
},
|
|
39
|
-
{ label:
|
|
39
|
+
{ label: "name", value: (m) => m.name || "" },
|
|
40
40
|
]);
|
|
41
41
|
});
|
|
42
42
|
return 0;
|
|
43
43
|
}
|
|
44
44
|
|
|
45
45
|
const { values, positionals } = parseArgs(argv, {
|
|
46
|
-
model: { type:
|
|
47
|
-
input: { type:
|
|
48
|
-
|
|
49
|
-
dimensions: { type:
|
|
50
|
-
encoding: { type:
|
|
51
|
-
provider: { type:
|
|
46
|
+
model: { type: "string", short: "m" },
|
|
47
|
+
input: { type: "string", short: "i", multiple: true },
|
|
48
|
+
"input-type": { type: "string" },
|
|
49
|
+
dimensions: { type: "string" },
|
|
50
|
+
encoding: { type: "string" },
|
|
51
|
+
provider: { type: "string" },
|
|
52
52
|
});
|
|
53
53
|
if (values.help) {
|
|
54
54
|
process.stdout.write(HELP);
|
|
55
55
|
return 0;
|
|
56
56
|
}
|
|
57
|
-
if (!values.model) throw new Error(
|
|
57
|
+
if (!values.model) throw new Error("--model is required");
|
|
58
58
|
|
|
59
59
|
const inputs = [...(values.input || []), ...positionals];
|
|
60
60
|
const piped = await readStdinIfPiped();
|
|
61
61
|
if (piped) inputs.push(piped);
|
|
62
|
-
if (!inputs.length) throw new Error(
|
|
62
|
+
if (!inputs.length) throw new Error("No input. Pass text, use --input, or pipe stdin.");
|
|
63
63
|
|
|
64
64
|
const body = {
|
|
65
65
|
model: values.model,
|
|
66
|
-
input: inputs.length === 1 ? inputs[0] : inputs
|
|
66
|
+
input: inputs.length === 1 ? inputs[0] : inputs,
|
|
67
67
|
};
|
|
68
|
-
if (values[
|
|
69
|
-
if (values.dimensions) body.dimensions = numberOption(values.dimensions,
|
|
68
|
+
if (values["input-type"]) body.input_type = values["input-type"];
|
|
69
|
+
if (values.dimensions) body.dimensions = numberOption(values.dimensions, "--dimensions");
|
|
70
70
|
if (values.encoding) body.encoding_format = values.encoding;
|
|
71
71
|
if (values.provider) body.provider = JSON.parse(values.provider);
|
|
72
72
|
|
|
73
|
-
const data = await api(
|
|
73
|
+
const data = await api("POST", "/embeddings", {
|
|
74
74
|
auth: authFromValues(values),
|
|
75
|
-
body
|
|
75
|
+
body,
|
|
76
76
|
});
|
|
77
77
|
printResult(data, () => {
|
|
78
78
|
const arr = data.data || [];
|
|
79
79
|
for (const e of arr) {
|
|
80
80
|
const v = e.embedding;
|
|
81
81
|
if (Array.isArray(v))
|
|
82
|
-
outln(
|
|
83
|
-
|
|
82
|
+
outln(
|
|
83
|
+
`[${v.length}d] ${v
|
|
84
|
+
.slice(0, 4)
|
|
85
|
+
.map((x) => x.toFixed(5))
|
|
86
|
+
.join(", ")}, ...`,
|
|
87
|
+
);
|
|
88
|
+
else outln(String(v).slice(0, 80) + "...");
|
|
84
89
|
}
|
|
85
90
|
});
|
|
86
91
|
return 0;
|