@aaronjmars/openroutercli 0.1.3 → 0.1.4

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 CHANGED
@@ -206,7 +206,3 @@ This is an **unofficial** community project. It is not affiliated with,
206
206
  endorsed by, or sponsored by OpenRouter. "OpenRouter" and the OpenRouter
207
207
  logo are trademarks of their respective owners; usage here is for
208
208
  identification only.
209
-
210
- ## Star History
211
-
212
- [![Star History Chart](https://api.star-history.com/svg?repos=aaronjmars/openroutercli&type=Date)](https://www.star-history.com/#aaronjmars/openroutercli&Date)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@aaronjmars/openroutercli",
3
- "version": "0.1.3",
3
+ "version": "0.1.4",
4
4
  "description": "Command-line interface for the OpenRouter API. Built for humans and LLM agents.",
5
5
  "type": "module",
6
6
  "bin": {
package/src/args.js CHANGED
@@ -20,6 +20,30 @@ export function parseArgs(argv, options = {}) {
20
20
  });
21
21
  }
22
22
 
23
+ // parseArgs yields strings; a bare Number() turns a typo into NaN, which
24
+ // JSON.stringify serializes as null -- an API request that silently means
25
+ // something else. Fail on the CLI instead.
26
+ export function numberOption(value, flag) {
27
+ if (value === undefined) return undefined;
28
+ const n = Number(value);
29
+ if (String(value).trim() === '' || !Number.isFinite(n)) {
30
+ throw new Error(`Invalid number for ${flag}: "${value}"`);
31
+ }
32
+ return n;
33
+ }
34
+
35
+ export const PAGINATION_OPTIONS = {
36
+ offset: { type: 'string' },
37
+ limit: { type: 'string' }
38
+ };
39
+
40
+ export function paginationQuery(values) {
41
+ const query = {};
42
+ if (values.offset) query.offset = values.offset;
43
+ if (values.limit) query.limit = values.limit;
44
+ return query;
45
+ }
46
+
23
47
  export function authFromValues(values) {
24
48
  const out = {};
25
49
  if (values.key) out.key = values.key;
package/src/cli.js CHANGED
@@ -14,6 +14,7 @@ import { requestCommand } from './commands/request.js';
14
14
  import { guardrailsCommand } from './commands/guardrails.js';
15
15
  import { workspacesCommand } from './commands/workspaces.js';
16
16
  import { orgCommand, zdrCommand, authCodeCommand } from './commands/misc.js';
17
+ import { DEFAULT_BASE_URL } from './config.js';
17
18
  import { readFileSync } from 'node:fs';
18
19
  import { fileURLToPath } from 'node:url';
19
20
  import { dirname, join } from 'node:path';
@@ -98,7 +99,7 @@ Management (require a management key):
98
99
 
99
100
  Global options (work with all commands):
100
101
  -k, --key <key> Use this API key instead of the stored one
101
- --base-url <url> Override API base URL (default: https://openrouter.ai/api/v1)
102
+ --base-url <url> Override API base URL (default: ${DEFAULT_BASE_URL})
102
103
  --referer <url> HTTP-Referer header (set as your app's URL)
103
104
  --title <name> X-Title header (your app name; appears on openrouter.ai)
104
105
  --json Output JSON (also disables streaming, color, and prompts)
@@ -174,7 +174,6 @@ async function streamResponse(body, auth) {
174
174
  if (process.stdout.isTTY) out(c.dim(delta.reasoning));
175
175
  }
176
176
  if (delta.tool_calls) {
177
- // Print tool calls JSON-compactly
178
177
  for (const tc of delta.tool_calls) {
179
178
  if (tc.function && tc.function.arguments) out(tc.function.arguments);
180
179
  }
@@ -1,7 +1,7 @@
1
1
  import { stdin } from 'node:process';
2
2
  import { parseArgs, authFromValues } from '../args.js';
3
3
  import { api } from '../api.js';
4
- import { c, isJsonMode, outln, printResult, table } from '../output.js';
4
+ import { outln, pricePerMillion, printResult, table } from '../output.js';
5
5
 
6
6
  const HELP = `Usage: openrouter embed [text...] [options]
7
7
  openrouter embed models
@@ -38,11 +38,7 @@ export async function embedCommand(argv) {
38
38
  { label: 'context', value: (m) => m.context_length ?? '' },
39
39
  {
40
40
  label: '$/M tokens',
41
- value: (m) => {
42
- const p = m.pricing || {};
43
- const fmt = (x) => (x == null ? '-' : (Number(x) * 1e6).toFixed(2));
44
- return fmt(p.prompt);
45
- }
41
+ value: (m) => pricePerMillion((m.pricing || {}).prompt)
46
42
  },
47
43
  { label: 'name', value: (m) => m.name || '' }
48
44
  ]);
@@ -1,6 +1,6 @@
1
- import { parseArgs, authFromValues } from '../args.js';
1
+ import { parseArgs, authFromValues, PAGINATION_OPTIONS, paginationQuery, numberOption } from '../args.js';
2
2
  import { api } from '../api.js';
3
- import { c, outln, printResult, table } from '../output.js';
3
+ import { outln, printResult, table } from '../output.js';
4
4
 
5
5
  const HELP = `Usage: openrouter guardrails <subcommand> [options]
6
6
 
@@ -41,7 +41,7 @@ function csv(v) {
41
41
  function buildBody(values) {
42
42
  const body = {};
43
43
  if (values.description !== undefined) body.description = values.description;
44
- if (values['limit-usd'] !== undefined) body.limit_usd = Number(values['limit-usd']);
44
+ if (values['limit-usd'] !== undefined) body.limit_usd = numberOption(values['limit-usd'], '--limit-usd');
45
45
  if (values['reset-interval'] !== undefined) {
46
46
  body.reset_interval = values['reset-interval'] === 'null' ? null : values['reset-interval'];
47
47
  }
@@ -77,13 +77,8 @@ export async function guardrailsCommand(argv) {
77
77
  const auth = (v) => authFromValues(v);
78
78
 
79
79
  if (sub === 'list') {
80
- const { values } = parseArgs(rest, {
81
- offset: { type: 'string' },
82
- limit: { type: 'string' }
83
- });
84
- const query = {};
85
- if (values.offset) query.offset = values.offset;
86
- if (values.limit) query.limit = values.limit;
80
+ const { values } = parseArgs(rest, PAGINATION_OPTIONS);
81
+ const query = paginationQuery(values);
87
82
  const data = await api('GET', '/guardrails', {
88
83
  auth: auth(values),
89
84
  requiresManagement: true,
@@ -109,7 +104,7 @@ export async function guardrailsCommand(argv) {
109
104
  auth: auth(values),
110
105
  requiresManagement: true
111
106
  });
112
- printResult(data, () => outln(JSON.stringify(data, null, 2)));
107
+ printResult(data);
113
108
  return 0;
114
109
  }
115
110
 
@@ -127,7 +122,7 @@ export async function guardrailsCommand(argv) {
127
122
  requiresManagement: true,
128
123
  body
129
124
  });
130
- printResult(data, () => outln(JSON.stringify(data, null, 2)));
125
+ printResult(data);
131
126
  return 0;
132
127
  }
133
128
 
@@ -144,7 +139,7 @@ export async function guardrailsCommand(argv) {
144
139
  requiresManagement: true,
145
140
  body
146
141
  });
147
- printResult(data, () => outln(JSON.stringify(data, null, 2)));
142
+ printResult(data);
148
143
  return 0;
149
144
  }
150
145
 
@@ -169,7 +164,7 @@ export async function guardrailsCommand(argv) {
169
164
  auth: auth(values),
170
165
  requiresManagement: true
171
166
  });
172
- printResult(data, () => outln(JSON.stringify(data, null, 2)));
167
+ printResult(data);
173
168
  return 0;
174
169
  }
175
170
 
@@ -184,7 +179,7 @@ export async function guardrailsCommand(argv) {
184
179
  auth: auth(values),
185
180
  requiresManagement: true
186
181
  });
187
- printResult(data, () => outln(JSON.stringify(data, null, 2)));
182
+ printResult(data);
188
183
  return 0;
189
184
  }
190
185
 
@@ -201,7 +196,7 @@ export async function guardrailsCommand(argv) {
201
196
  requiresManagement: true,
202
197
  body: { key_hashes: hashes }
203
198
  });
204
- printResult(data, () => outln(JSON.stringify(data, null, 2)));
199
+ printResult(data);
205
200
  return 0;
206
201
  }
207
202
 
@@ -218,7 +213,7 @@ export async function guardrailsCommand(argv) {
218
213
  requiresManagement: true,
219
214
  body: { user_ids: userIds }
220
215
  });
221
- printResult(data, () => outln(JSON.stringify(data, null, 2)));
216
+ printResult(data);
222
217
  return 0;
223
218
  }
224
219
 
@@ -1,4 +1,4 @@
1
- import { parseArgs, authFromValues } from '../args.js';
1
+ import { parseArgs, authFromValues, numberOption } from '../args.js';
2
2
  import { api } from '../api.js';
3
3
  import { c, outln, printResult, table } from '../output.js';
4
4
 
@@ -69,7 +69,7 @@ export async function keysCommand(argv) {
69
69
  auth: authFromValues(values),
70
70
  requiresManagement: true
71
71
  });
72
- printResult(data, () => outln(JSON.stringify(data, null, 2)));
72
+ printResult(data);
73
73
  return 0;
74
74
  }
75
75
 
@@ -84,7 +84,7 @@ export async function keysCommand(argv) {
84
84
  const name = positionals[0];
85
85
  if (!name) throw new Error('name required');
86
86
  const body = { name };
87
- if (values.limit) body.limit = Number(values.limit);
87
+ if (values.limit) body.limit = numberOption(values.limit, '--limit');
88
88
  if (values['limit-reset']) body.limit_reset = values['limit-reset'];
89
89
  if (values['expires-at']) body.expires_at = values['expires-at'];
90
90
  if (values.workspace) body.workspace_id = values.workspace;
@@ -117,7 +117,7 @@ export async function keysCommand(argv) {
117
117
  if (!positionals[0]) throw new Error('hash required');
118
118
  const body = {};
119
119
  if (values.name) body.name = values.name;
120
- if (values.limit) body.limit = Number(values.limit);
120
+ if (values.limit) body.limit = numberOption(values.limit, '--limit');
121
121
  if (values['limit-reset']) body.limit_reset = values['limit-reset'];
122
122
  if (values['include-byok']) body.include_byok_in_limit = true;
123
123
  if (values['no-include-byok']) body.include_byok_in_limit = false;
@@ -128,7 +128,7 @@ export async function keysCommand(argv) {
128
128
  requiresManagement: true,
129
129
  body
130
130
  });
131
- printResult(data, () => outln(JSON.stringify(data, null, 2)));
131
+ printResult(data);
132
132
  return 0;
133
133
  }
134
134
 
@@ -1,21 +1,16 @@
1
- import { parseArgs, authFromValues } from '../args.js';
1
+ import { parseArgs, authFromValues, PAGINATION_OPTIONS, paginationQuery } from '../args.js';
2
2
  import { api } from '../api.js';
3
- import { c, outln, printResult, table } from '../output.js';
3
+ import { printResult, table } from '../output.js';
4
4
 
5
5
  export async function orgMembersCommand(argv) {
6
- const { values } = parseArgs(argv, {
7
- offset: { type: 'string' },
8
- limit: { type: 'string' }
9
- });
6
+ const { values } = parseArgs(argv, PAGINATION_OPTIONS);
10
7
  if (values.help) {
11
8
  process.stdout.write(
12
9
  'Usage: openrouter org members [--offset N] [--limit N]\n\nList organization members. Requires a management key.\n'
13
10
  );
14
11
  return 0;
15
12
  }
16
- const query = {};
17
- if (values.offset) query.offset = values.offset;
18
- if (values.limit) query.limit = values.limit;
13
+ const query = paginationQuery(values);
19
14
  const data = await api('GET', '/organization/members', {
20
15
  auth: authFromValues(values),
21
16
  requiresManagement: true,
@@ -57,7 +52,7 @@ export async function zdrCommand(argv) {
57
52
  const data = await api('GET', '/endpoints/zdr', {
58
53
  auth: authFromValues(values)
59
54
  });
60
- printResult(data, () => outln(JSON.stringify(data, null, 2)));
55
+ printResult(data);
61
56
  return 0;
62
57
  }
63
58
 
@@ -99,6 +94,6 @@ export async function authCodeCommand(argv) {
99
94
  requiresManagement: true,
100
95
  body
101
96
  });
102
- printResult(data, () => outln(JSON.stringify(data, null, 2)));
97
+ printResult(data);
103
98
  return 0;
104
99
  }
@@ -1,6 +1,6 @@
1
1
  import { parseArgs, authFromValues } from '../args.js';
2
2
  import { api } from '../api.js';
3
- import { c, isJsonMode, outln, printResult, table } from '../output.js';
3
+ import { c, isJsonMode, outln, pricePerMillion, printResult, table } from '../output.js';
4
4
 
5
5
  const HELP = `Usage: openrouter models [subcommand] [options]
6
6
 
@@ -32,8 +32,7 @@ function listFormatter(data) {
32
32
  label: 'in/out $/M',
33
33
  value: (m) => {
34
34
  const p = m.pricing || {};
35
- const fmt = (x) => (x == null ? '-' : (Number(x) * 1e6).toFixed(2));
36
- return `${fmt(p.prompt)}/${fmt(p.completion)}`;
35
+ return `${pricePerMillion(p.prompt)}/${pricePerMillion(p.completion)}`;
37
36
  }
38
37
  },
39
38
  { label: 'name', value: (m) => m.name || '' }
@@ -227,8 +226,7 @@ export async function modelsCommand(argv) {
227
226
  label: 'in/out $/M',
228
227
  value: (e) => {
229
228
  const p = e.pricing || {};
230
- const fmt = (x) => (x == null ? '-' : (Number(x) * 1e6).toFixed(2));
231
- return `${fmt(p.prompt)}/${fmt(p.completion)}`;
229
+ return `${pricePerMillion(p.prompt)}/${pricePerMillion(p.completion)}`;
232
230
  }
233
231
  },
234
232
  {
@@ -1,8 +1,7 @@
1
- import { writeFile } from 'node:fs/promises';
2
1
  import { stdin } from 'node:process';
3
2
  import { parseArgs, authFromValues } from '../args.js';
4
3
  import { api } from '../api.js';
5
- import { info, outln, printJSON, isJsonMode } from '../output.js';
4
+ import { writeBinaryOutput } from '../output.js';
6
5
 
7
6
  const HELP = `Usage: openrouter speech [text...] [options]
8
7
 
@@ -57,12 +56,6 @@ export async function speechCommand(argv) {
57
56
  });
58
57
 
59
58
  const out = values.out || `out.${values.format || 'mp3'}`;
60
- if (out === '-') {
61
- process.stdout.write(Buffer.from(bytes));
62
- } else {
63
- await writeFile(out, Buffer.from(bytes));
64
- if (isJsonMode()) printJSON({ saved: out, bytes: bytes.length });
65
- else info(`Wrote ${bytes.length} bytes to ${out}`);
66
- }
59
+ await writeBinaryOutput(bytes, out);
67
60
  return 0;
68
61
  }
@@ -1,7 +1,6 @@
1
- import { writeFile } from 'node:fs/promises';
2
- import { parseArgs, authFromValues } from '../args.js';
1
+ import { parseArgs, authFromValues, numberOption } from '../args.js';
3
2
  import { api } from '../api.js';
4
- import { c, info, outln, printJSON, printResult, isJsonMode } from '../output.js';
3
+ import { c, info, outln, printResult, writeBinaryOutput } from '../output.js';
5
4
 
6
5
  const HELP = `Usage: openrouter video <subcommand> [options]
7
6
 
@@ -38,7 +37,7 @@ export async function videoCommand(argv) {
38
37
  if (sub === 'models') {
39
38
  const { values } = parseArgs(rest, {});
40
39
  const data = await api('GET', '/videos/models', { auth: auth(values) });
41
- printResult(data, () => outln(JSON.stringify(data, null, 2)));
40
+ printResult(data);
42
41
  return 0;
43
42
  }
44
43
 
@@ -75,7 +74,7 @@ export async function videoCommand(argv) {
75
74
  const data = await api('GET', `/videos/${encodeURIComponent(positionals[0])}`, {
76
75
  auth: auth(values)
77
76
  });
78
- printResult(data, () => outln(JSON.stringify(data, null, 2)));
77
+ printResult(data);
79
78
  return 0;
80
79
  }
81
80
 
@@ -85,8 +84,10 @@ export async function videoCommand(argv) {
85
84
  timeout: { type: 'string' }
86
85
  });
87
86
  if (!positionals[0]) throw new Error('jobId required');
88
- const interval = Number(values.interval || 5) * 1000;
89
- const timeout = Number(values.timeout || 600) * 1000;
87
+ const interval = (numberOption(values.interval, '--interval') ?? 5) * 1000;
88
+ const timeout = (numberOption(values.timeout, '--timeout') ?? 600) * 1000;
89
+ if (interval <= 0) throw new Error('--interval must be greater than 0');
90
+ if (timeout <= 0) throw new Error('--timeout must be greater than 0');
90
91
  const start = Date.now();
91
92
  while (true) {
92
93
  const data = await api('GET', `/videos/${encodeURIComponent(positionals[0])}`, {
@@ -95,7 +96,7 @@ export async function videoCommand(argv) {
95
96
  const status = data.status || (data.data && data.data.status);
96
97
  info(`status: ${status}`);
97
98
  if (['succeeded', 'completed', 'failed', 'cancelled', 'error'].includes(status)) {
98
- printResult(data, () => outln(JSON.stringify(data, null, 2)));
99
+ printResult(data);
99
100
  return ['failed', 'cancelled', 'error'].includes(status) ? 4 : 0;
100
101
  }
101
102
  if (Date.now() - start > timeout) {
@@ -115,12 +116,7 @@ export async function videoCommand(argv) {
115
116
  binary: true
116
117
  });
117
118
  const out = values.out || `${positionals[0]}.mp4`;
118
- if (out === '-') process.stdout.write(Buffer.from(bytes));
119
- else {
120
- await writeFile(out, Buffer.from(bytes));
121
- if (isJsonMode()) printJSON({ saved: out, bytes: bytes.length });
122
- else info(`Wrote ${bytes.length} bytes to ${out}`);
123
- }
119
+ await writeBinaryOutput(bytes, out);
124
120
  return 0;
125
121
  }
126
122
 
@@ -1,6 +1,6 @@
1
- import { parseArgs, authFromValues } from '../args.js';
1
+ import { parseArgs, authFromValues, PAGINATION_OPTIONS, paginationQuery } from '../args.js';
2
2
  import { api } from '../api.js';
3
- import { c, outln, printResult, table } from '../output.js';
3
+ import { outln, printResult, table } from '../output.js';
4
4
 
5
5
  const HELP = `Usage: openrouter workspaces <subcommand> [options]
6
6
 
@@ -52,13 +52,8 @@ export async function workspacesCommand(argv) {
52
52
  const auth = (v) => authFromValues(v);
53
53
 
54
54
  if (sub === 'list') {
55
- const { values } = parseArgs(rest, {
56
- offset: { type: 'string' },
57
- limit: { type: 'string' }
58
- });
59
- const query = {};
60
- if (values.offset) query.offset = values.offset;
61
- if (values.limit) query.limit = values.limit;
55
+ const { values } = parseArgs(rest, PAGINATION_OPTIONS);
56
+ const query = paginationQuery(values);
62
57
  const data = await api('GET', '/workspaces', {
63
58
  auth: auth(values),
64
59
  requiresManagement: true,
@@ -83,7 +78,7 @@ export async function workspacesCommand(argv) {
83
78
  auth: auth(values),
84
79
  requiresManagement: true
85
80
  });
86
- printResult(data, () => outln(JSON.stringify(data, null, 2)));
81
+ printResult(data);
87
82
  return 0;
88
83
  }
89
84
 
@@ -97,7 +92,7 @@ export async function workspacesCommand(argv) {
97
92
  requiresManagement: true,
98
93
  body
99
94
  });
100
- printResult(data, () => outln(JSON.stringify(data, null, 2)));
95
+ printResult(data);
101
96
  return 0;
102
97
  }
103
98
 
@@ -111,7 +106,7 @@ export async function workspacesCommand(argv) {
111
106
  requiresManagement: true,
112
107
  body
113
108
  });
114
- printResult(data, () => outln(JSON.stringify(data, null, 2)));
109
+ printResult(data);
115
110
  return 0;
116
111
  }
117
112
 
@@ -139,7 +134,7 @@ export async function workspacesCommand(argv) {
139
134
  requiresManagement: true,
140
135
  body: { user_ids: userIds }
141
136
  });
142
- printResult(data, () => outln(JSON.stringify(data, null, 2)));
137
+ printResult(data);
143
138
  return 0;
144
139
  }
145
140
 
package/src/config.js CHANGED
@@ -26,14 +26,6 @@ export async function saveConfig(cfg) {
26
26
  await fs.rename(tmp, CONFIG_FILE);
27
27
  }
28
28
 
29
- export async function clearConfig() {
30
- try {
31
- await fs.unlink(CONFIG_FILE);
32
- } catch (err) {
33
- if (err.code !== 'ENOENT') throw err;
34
- }
35
- }
36
-
37
29
  export async function resolveAuth(opts = {}) {
38
30
  const cfg = await loadConfig();
39
31
  const userKey =
@@ -45,12 +37,10 @@ export async function resolveAuth(opts = {}) {
45
37
  // the regular key); inference commands prefer the regular key (falling back
46
38
  // to the management key).
47
39
  let apiKey;
48
- let usedManagementSlot = false;
49
40
  if (opts.key) {
50
41
  apiKey = opts.key;
51
42
  } else if (opts.requiresManagement) {
52
43
  apiKey = mgmtKey || userKey;
53
- usedManagementSlot = !!mgmtKey;
54
44
  } else {
55
45
  apiKey = userKey || mgmtKey;
56
46
  }
@@ -73,7 +63,6 @@ export async function resolveAuth(opts = {}) {
73
63
  referer,
74
64
  title,
75
65
  config: cfg,
76
- usedManagementSlot,
77
66
  hasManagementKey: !!mgmtKey,
78
67
  hasUserKey: !!userKey
79
68
  };
package/src/output.js CHANGED
@@ -1,3 +1,5 @@
1
+ import { writeFile } from 'node:fs/promises';
2
+
1
3
  const isTTY = process.stdout.isTTY;
2
4
  const noColor =
3
5
  process.env.NO_COLOR != null ||
@@ -45,14 +47,26 @@ export function info(text) {
45
47
  process.stderr.write(c.dim(text) + '\n');
46
48
  }
47
49
 
48
- export function warn(text) {
49
- process.stderr.write(c.yellow('warning: ') + text + '\n');
50
- }
51
-
52
50
  export function printJSON(data) {
53
51
  process.stdout.write(JSON.stringify(data, null, 2) + '\n');
54
52
  }
55
53
 
54
+ // OpenRouter prices are per-token; tables show them per million.
55
+ export function pricePerMillion(x) {
56
+ return x == null ? '-' : (Number(x) * 1e6).toFixed(2);
57
+ }
58
+
59
+ export async function writeBinaryOutput(bytes, out) {
60
+ const buf = Buffer.from(bytes);
61
+ if (out === '-') {
62
+ process.stdout.write(buf);
63
+ return;
64
+ }
65
+ await writeFile(out, buf);
66
+ if (JSON_MODE) printJSON({ saved: out, bytes: bytes.length });
67
+ else info(`Wrote ${bytes.length} bytes to ${out}`);
68
+ }
69
+
56
70
  export function printResult(data, formatter) {
57
71
  if (JSON_MODE || !formatter) {
58
72
  printJSON(data);