@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.
@@ -1,11 +1,11 @@
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';
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 = 'openrouter/auto';
8
- const ERROR_FINISH_REASONS = ['error', 'content_filter'];
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
- --reasoning <effort> low | medium | high
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('node:fs/promises');
56
- const text = await readFile(value.slice(1), 'utf8');
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('{') || value.startsWith('[')) return JSON.parse(value);
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('node:fs/promises');
63
- const text = await readFile(value, 'utf8');
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('data:')) {
72
- return { type: 'image_url', image_url: { url: value } };
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('node:fs/promises');
112
+ const { readFile } = await import("node:fs/promises");
75
113
  const buf = await readFile(value);
76
- const ext = (value.split('.').pop() || '').toLowerCase();
114
+ const ext = (value.split(".").pop() || "").toLowerCase();
77
115
  const mime =
78
- ext === 'png'
79
- ? 'image/png'
80
- : ext === 'webp'
81
- ? 'image/webp'
82
- : ext === 'gif'
83
- ? 'image/gif'
84
- : 'image/jpeg';
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: 'image_url',
87
- image_url: { url: `data:${mime};base64,${buf.toString('base64')}` }
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: 'temperature',
93
- 'top-p': 'top_p',
94
- 'top-k': 'top_k',
95
- 'max-tokens': 'max_tokens',
96
- seed: 'seed',
97
- 'frequency-penalty': 'frequency_penalty',
98
- 'presence-penalty': 'presence_penalty',
99
- 'repetition-penalty': 'repetition_penalty',
100
- 'min-p': 'min_p'
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(',').map((s) => s.trim());
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.reasoning) body.reasoning = { effort: values.reasoning };
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
- if (images.length === 0) return prompt;
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: 'text', text: prompt });
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['json-output']) body.response_format = { type: 'json_object' };
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: 'json_schema',
132
- json_schema: { name: 'response', schema, strict: true }
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: 'function', function: def });
220
+ else body.tools.push({ type: "function", function: def });
142
221
  }
143
222
  }
144
- if (values['tool-choice']) {
145
- const v = values['tool-choice'];
146
- if (['auto', 'none', 'required'].includes(v)) body.tool_choice = v;
147
- else body.tool_choice = { type: 'function', function: { name: v } };
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: 'system', content: values.system });
261
+ if (values.system) messages.push({ role: "system", content: values.system });
155
262
  messages.push({
156
- role: 'user',
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 || ''} prompt=${usage.prompt_tokens} completion=${usage.completion_tokens} total=${usage.total_tokens}`
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('POST', '/chat/completions', {
278
+ const res = await api("POST", "/chat/completions", {
172
279
  auth,
173
280
  body: { ...body, stream: true },
174
281
  raw: true,
175
- headers: { Accept: 'text/event-stream' }
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 === 'string') {
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 === 'text' && part.text) {
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('\n');
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: 'string', short: 'm' },
217
- models: { type: 'string' },
218
- system: { type: 'string', short: 's' },
219
- stream: { type: 'boolean' },
220
- 'no-stream': { type: 'boolean' },
221
- temperature: { type: 'string' },
222
- 'top-p': { type: 'string' },
223
- 'top-k': { type: 'string' },
224
- 'max-tokens': { type: 'string' },
225
- seed: { type: 'string' },
226
- stop: { type: 'string' },
227
- 'frequency-penalty': { type: 'string' },
228
- 'presence-penalty': { type: 'string' },
229
- 'repetition-penalty': { type: 'string' },
230
- 'min-p': { type: 'string' },
231
- 'json-output': { type: 'boolean' },
232
- schema: { type: 'string' },
233
- tool: { type: 'string', multiple: true },
234
- 'tool-choice': { type: 'string' },
235
- reasoning: { type: 'string' },
236
- provider: { type: 'string' },
237
- image: { type: 'string', multiple: true },
238
- raw: { type: 'boolean' },
239
- usage: { type: 'boolean' },
240
- interactive: { type: 'boolean', short: 'i' }
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
- '--interactive needs a terminal on stdin. To send piped input as a ' +
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(' ').trim();
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('No prompt. Pass text, pipe via stdin, or use --interactive.');
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('POST', '/chat/completions', { auth, body });
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 === 'string') outln(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: 'system', content: values.system });
311
- outln(c.dim('OpenRouter chat. /exit to quit, /reset to clear history, /model <id> to switch.'));
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 === '/exit' || trimmed === '/quit') return 0;
327
- if (trimmed === '/reset') {
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('(history cleared)'));
453
+ outln(c.dim("(history cleared)"));
333
454
  continue;
334
455
  }
335
- if (trimmed.startsWith('/model ')) {
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: 'user',
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: 'assistant', content: meta.text });
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)) {
@@ -1,6 +1,6 @@
1
- import { parseArgs, authFromValues, numberOption, readStdinIfPiped } from '../args.js';
2
- import { api } from '../api.js';
3
- import { outln, pricePerMillion, printResult, table } from '../output.js';
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] === 'models') {
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('GET', '/embeddings/models', {
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: 'id', value: (m) => m.id },
34
- { label: 'context', value: (m) => m.context_length ?? '' },
33
+ { label: "id", value: (m) => m.id },
34
+ { label: "context", value: (m) => m.context_length ?? "" },
35
35
  {
36
- label: '$/M tokens',
37
- value: (m) => pricePerMillion((m.pricing || {}).prompt)
36
+ label: "$/M tokens",
37
+ value: (m) => pricePerMillion((m.pricing || {}).prompt),
38
38
  },
39
- { label: 'name', value: (m) => m.name || '' }
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: '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' }
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('--model is required');
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('No input. Pass text, use --input, or pipe stdin.');
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['input-type']) body.input_type = values['input-type'];
69
- if (values.dimensions) body.dimensions = numberOption(values.dimensions, '--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('POST', '/embeddings', {
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(`[${v.length}d] ${v.slice(0, 4).map((x) => x.toFixed(5)).join(', ')}, ...`);
83
- else outln(String(v).slice(0, 80) + '...');
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;