@aaronjmars/openroutercli 0.1.2 → 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 +0 -4
- package/SKILL.md +1 -1
- package/package.json +1 -1
- package/src/args.js +24 -0
- package/src/cli.js +14 -3
- package/src/commands/chat.js +31 -8
- package/src/commands/embed.js +2 -6
- package/src/commands/guardrails.js +12 -17
- package/src/commands/keys.js +5 -5
- package/src/commands/messages.js +23 -2
- package/src/commands/misc.js +6 -11
- package/src/commands/models.js +3 -5
- package/src/commands/speech.js +2 -9
- package/src/commands/video.js +10 -14
- package/src/commands/workspaces.js +8 -13
- package/src/config.js +1 -12
- package/src/output.js +18 -4
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
|
-
[](https://www.star-history.com/#aaronjmars/openroutercli&Date)
|
package/SKILL.md
CHANGED
|
@@ -318,7 +318,7 @@ openrouter --json keys get <hash> | jq '.data | {usage, usage_daily, limit_remai
|
|
|
318
318
|
| --- | --- | --- |
|
|
319
319
|
| `-k, --key` | `OPENROUTER_API_KEY` / `OPENROUTER_MANAGEMENT_KEY` | from config |
|
|
320
320
|
| `--base-url` | `OPENROUTER_BASE_URL` | `https://openrouter.ai/api/v1` |
|
|
321
|
-
| `--referer` | `OPENROUTER_REFERER` | `https://github.com/
|
|
321
|
+
| `--referer` | `OPENROUTER_REFERER` | `https://github.com/aaronjmars/openroutercli` |
|
|
322
322
|
| `--title` | `OPENROUTER_TITLE` | `openrouter-cli` |
|
|
323
323
|
| `--json` | — | off |
|
|
324
324
|
| `-q, --quiet` | — | off |
|
package/package.json
CHANGED
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,8 +14,19 @@ 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
|
-
|
|
18
|
-
|
|
17
|
+
import { DEFAULT_BASE_URL } from './config.js';
|
|
18
|
+
import { readFileSync } from 'node:fs';
|
|
19
|
+
import { fileURLToPath } from 'node:url';
|
|
20
|
+
import { dirname, join } from 'node:path';
|
|
21
|
+
|
|
22
|
+
const VERSION = (() => {
|
|
23
|
+
try {
|
|
24
|
+
const pkgPath = join(dirname(fileURLToPath(import.meta.url)), '..', 'package.json');
|
|
25
|
+
return JSON.parse(readFileSync(pkgPath, 'utf8')).version;
|
|
26
|
+
} catch {
|
|
27
|
+
return 'unknown';
|
|
28
|
+
}
|
|
29
|
+
})();
|
|
19
30
|
|
|
20
31
|
const COMMANDS = {
|
|
21
32
|
login: loginCommand,
|
|
@@ -88,7 +99,7 @@ Management (require a management key):
|
|
|
88
99
|
|
|
89
100
|
Global options (work with all commands):
|
|
90
101
|
-k, --key <key> Use this API key instead of the stored one
|
|
91
|
-
--base-url <url> Override API base URL (default:
|
|
102
|
+
--base-url <url> Override API base URL (default: ${DEFAULT_BASE_URL})
|
|
92
103
|
--referer <url> HTTP-Referer header (set as your app's URL)
|
|
93
104
|
--title <name> X-Title header (your app name; appears on openrouter.ai)
|
|
94
105
|
--json Output JSON (also disables streaming, color, and prompts)
|
package/src/commands/chat.js
CHANGED
|
@@ -17,9 +17,14 @@ Options:
|
|
|
17
17
|
--no-stream Disable streaming
|
|
18
18
|
--temperature <n>
|
|
19
19
|
--top-p <n>
|
|
20
|
+
--top-k <n>
|
|
20
21
|
--max-tokens <n>
|
|
21
22
|
--seed <n>
|
|
22
23
|
--stop <text> Stop sequence (repeatable via comma)
|
|
24
|
+
--frequency-penalty <n> -2.0 to 2.0
|
|
25
|
+
--presence-penalty <n> -2.0 to 2.0
|
|
26
|
+
--repetition-penalty <n> 0.0 to 2.0
|
|
27
|
+
--min-p <n> 0.0 to 1.0
|
|
23
28
|
--json-output Ask the model for JSON (response_format=json_object)
|
|
24
29
|
--schema <file|json> JSON schema for structured output
|
|
25
30
|
--tool <file|json> Tool definition (repeatable). JSON object or @file.json
|
|
@@ -78,6 +83,21 @@ async function imageToContent(value) {
|
|
|
78
83
|
};
|
|
79
84
|
}
|
|
80
85
|
|
|
86
|
+
function applySamplingOptions(body, values) {
|
|
87
|
+
if (values.models) body.models = values.models.split(',').map((s) => s.trim());
|
|
88
|
+
if (values.temperature != null) body.temperature = Number(values.temperature);
|
|
89
|
+
if (values['top-p'] != null) body.top_p = Number(values['top-p']);
|
|
90
|
+
if (values['top-k'] != null) body.top_k = Number(values['top-k']);
|
|
91
|
+
if (values['max-tokens'] != null) body.max_tokens = Number(values['max-tokens']);
|
|
92
|
+
if (values.seed != null) body.seed = Number(values.seed);
|
|
93
|
+
if (values.stop) body.stop = values.stop.split(',');
|
|
94
|
+
if (values['frequency-penalty'] != null) body.frequency_penalty = Number(values['frequency-penalty']);
|
|
95
|
+
if (values['presence-penalty'] != null) body.presence_penalty = Number(values['presence-penalty']);
|
|
96
|
+
if (values['repetition-penalty'] != null) body.repetition_penalty = Number(values['repetition-penalty']);
|
|
97
|
+
if (values['min-p'] != null) body.min_p = Number(values['min-p']);
|
|
98
|
+
if (values.reasoning) body.reasoning = { effort: values.reasoning };
|
|
99
|
+
}
|
|
100
|
+
|
|
81
101
|
async function buildBody(values, prompt) {
|
|
82
102
|
const messages = [];
|
|
83
103
|
if (values.system) messages.push({ role: 'system', content: values.system });
|
|
@@ -98,12 +118,7 @@ async function buildBody(values, prompt) {
|
|
|
98
118
|
model: values.model || 'openrouter/auto',
|
|
99
119
|
messages
|
|
100
120
|
};
|
|
101
|
-
|
|
102
|
-
if (values.temperature != null) body.temperature = Number(values.temperature);
|
|
103
|
-
if (values['top-p'] != null) body.top_p = Number(values['top-p']);
|
|
104
|
-
if (values['max-tokens'] != null) body.max_tokens = Number(values['max-tokens']);
|
|
105
|
-
if (values.seed != null) body.seed = Number(values.seed);
|
|
106
|
-
if (values.stop) body.stop = values.stop.split(',');
|
|
121
|
+
applySamplingOptions(body, values);
|
|
107
122
|
|
|
108
123
|
if (values['json-output']) body.response_format = { type: 'json_object' };
|
|
109
124
|
if (values.schema) {
|
|
@@ -127,7 +142,6 @@ async function buildBody(values, prompt) {
|
|
|
127
142
|
if (['auto', 'none', 'required'].includes(v)) body.tool_choice = v;
|
|
128
143
|
else body.tool_choice = { type: 'function', function: { name: v } };
|
|
129
144
|
}
|
|
130
|
-
if (values.reasoning) body.reasoning = { effort: values.reasoning };
|
|
131
145
|
if (values.provider) body.provider = await loadJsonOrFile(values.provider);
|
|
132
146
|
return body;
|
|
133
147
|
}
|
|
@@ -160,7 +174,6 @@ async function streamResponse(body, auth) {
|
|
|
160
174
|
if (process.stdout.isTTY) out(c.dim(delta.reasoning));
|
|
161
175
|
}
|
|
162
176
|
if (delta.tool_calls) {
|
|
163
|
-
// Print tool calls JSON-compactly
|
|
164
177
|
for (const tc of delta.tool_calls) {
|
|
165
178
|
if (tc.function && tc.function.arguments) out(tc.function.arguments);
|
|
166
179
|
}
|
|
@@ -180,9 +193,14 @@ export async function chatCommand(argv) {
|
|
|
180
193
|
'no-stream': { type: 'boolean' },
|
|
181
194
|
temperature: { type: 'string' },
|
|
182
195
|
'top-p': { type: 'string' },
|
|
196
|
+
'top-k': { type: 'string' },
|
|
183
197
|
'max-tokens': { type: 'string' },
|
|
184
198
|
seed: { type: 'string' },
|
|
185
199
|
stop: { type: 'string' },
|
|
200
|
+
'frequency-penalty': { type: 'string' },
|
|
201
|
+
'presence-penalty': { type: 'string' },
|
|
202
|
+
'repetition-penalty': { type: 'string' },
|
|
203
|
+
'min-p': { type: 'string' },
|
|
186
204
|
'json-output': { type: 'boolean' },
|
|
187
205
|
schema: { type: 'string' },
|
|
188
206
|
tool: { type: 'string', multiple: true },
|
|
@@ -286,6 +304,7 @@ async function repl(values, auth) {
|
|
|
286
304
|
}
|
|
287
305
|
history.push({ role: 'user', content: trimmed });
|
|
288
306
|
const body = { model, messages: history, stream: true };
|
|
307
|
+
applySamplingOptions(body, values);
|
|
289
308
|
try {
|
|
290
309
|
const res = await api('POST', '/chat/completions', {
|
|
291
310
|
auth,
|
|
@@ -300,11 +319,15 @@ async function repl(values, auth) {
|
|
|
300
319
|
out(delta.content);
|
|
301
320
|
assistant += delta.content;
|
|
302
321
|
}
|
|
322
|
+
if (delta?.reasoning && process.stdout.isTTY) out(c.dim(delta.reasoning));
|
|
303
323
|
}
|
|
304
324
|
out('\n');
|
|
305
325
|
history.push({ role: 'assistant', content: assistant });
|
|
306
326
|
} catch (err) {
|
|
307
327
|
outln(c.red(err.message));
|
|
328
|
+
// Drop the orphaned user message so the next turn doesn't send a
|
|
329
|
+
// [..., user, user] sequence the API will reject.
|
|
330
|
+
history.pop();
|
|
308
331
|
}
|
|
309
332
|
}
|
|
310
333
|
} finally {
|
package/src/commands/embed.js
CHANGED
|
@@ -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 {
|
|
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 {
|
|
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 =
|
|
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
|
-
|
|
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
|
|
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
|
|
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
|
|
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
|
|
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
|
|
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
|
|
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
|
|
216
|
+
printResult(data);
|
|
222
217
|
return 0;
|
|
223
218
|
}
|
|
224
219
|
|
package/src/commands/keys.js
CHANGED
|
@@ -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
|
|
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 =
|
|
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 =
|
|
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
|
|
131
|
+
printResult(data);
|
|
132
132
|
return 0;
|
|
133
133
|
}
|
|
134
134
|
|
package/src/commands/messages.js
CHANGED
|
@@ -25,7 +25,13 @@ OpenAI-compatible /responses endpoint.
|
|
|
25
25
|
Options:
|
|
26
26
|
-m, --model <id> Model id (required)
|
|
27
27
|
-s, --instructions <t> System / instructions
|
|
28
|
-
--
|
|
28
|
+
--max-tokens <n> Sent as max_output_tokens
|
|
29
|
+
--temperature <n>
|
|
30
|
+
--top-p <n>
|
|
31
|
+
--seed <n>
|
|
32
|
+
--reasoning <effort> low | medium | high
|
|
33
|
+
--stream Stream tokens (default when TTY)
|
|
34
|
+
--no-stream Disable streaming
|
|
29
35
|
--raw Print full JSON response
|
|
30
36
|
--body <json|@file> Provide a fully-formed body (overrides flags)
|
|
31
37
|
`;
|
|
@@ -117,7 +123,13 @@ export async function responsesCommand(argv) {
|
|
|
117
123
|
const { values, positionals } = parseArgs(argv, {
|
|
118
124
|
model: { type: 'string', short: 'm' },
|
|
119
125
|
instructions: { type: 'string', short: 's' },
|
|
126
|
+
'max-tokens': { type: 'string' },
|
|
127
|
+
temperature: { type: 'string' },
|
|
128
|
+
'top-p': { type: 'string' },
|
|
129
|
+
seed: { type: 'string' },
|
|
130
|
+
reasoning: { type: 'string' },
|
|
120
131
|
stream: { type: 'boolean' },
|
|
132
|
+
'no-stream': { type: 'boolean' },
|
|
121
133
|
raw: { type: 'boolean' },
|
|
122
134
|
body: { type: 'string' }
|
|
123
135
|
});
|
|
@@ -135,9 +147,18 @@ export async function responsesCommand(argv) {
|
|
|
135
147
|
if (!prompt) throw new Error('No prompt.');
|
|
136
148
|
body = { model: values.model, input: prompt };
|
|
137
149
|
if (values.instructions) body.instructions = values.instructions;
|
|
150
|
+
if (values['max-tokens'] != null) body.max_output_tokens = Number(values['max-tokens']);
|
|
151
|
+
if (values.temperature != null) body.temperature = Number(values.temperature);
|
|
152
|
+
if (values['top-p'] != null) body.top_p = Number(values['top-p']);
|
|
153
|
+
if (values.seed != null) body.seed = Number(values.seed);
|
|
154
|
+
if (values.reasoning) body.reasoning = { effort: values.reasoning };
|
|
138
155
|
}
|
|
139
156
|
|
|
140
|
-
|
|
157
|
+
const shouldStream =
|
|
158
|
+
values.stream ||
|
|
159
|
+
(!values['no-stream'] && !values.raw && !isJsonMode() && process.stdout.isTTY);
|
|
160
|
+
|
|
161
|
+
if (shouldStream) {
|
|
141
162
|
body.stream = true;
|
|
142
163
|
const res = await api('POST', '/responses', {
|
|
143
164
|
auth: authFromValues(values),
|
package/src/commands/misc.js
CHANGED
|
@@ -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 {
|
|
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
|
|
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
|
|
97
|
+
printResult(data);
|
|
103
98
|
return 0;
|
|
104
99
|
}
|
package/src/commands/models.js
CHANGED
|
@@ -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
|
-
|
|
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
|
-
|
|
231
|
-
return `${fmt(p.prompt)}/${fmt(p.completion)}`;
|
|
229
|
+
return `${pricePerMillion(p.prompt)}/${pricePerMillion(p.completion)}`;
|
|
232
230
|
}
|
|
233
231
|
},
|
|
234
232
|
{
|
package/src/commands/speech.js
CHANGED
|
@@ -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 {
|
|
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
|
-
|
|
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
|
}
|
package/src/commands/video.js
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
|
-
import {
|
|
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,
|
|
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
|
|
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
|
|
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 =
|
|
89
|
-
const timeout =
|
|
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
|
|
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
|
-
|
|
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 {
|
|
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
|
-
|
|
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
|
|
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
|
|
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
|
|
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
|
|
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
|
}
|
|
@@ -64,7 +54,7 @@ export async function resolveAuth(opts = {}) {
|
|
|
64
54
|
opts.referer ||
|
|
65
55
|
process.env.OPENROUTER_REFERER ||
|
|
66
56
|
cfg.referer ||
|
|
67
|
-
'https://github.com/
|
|
57
|
+
'https://github.com/aaronjmars/openroutercli';
|
|
68
58
|
const title =
|
|
69
59
|
opts.title || process.env.OPENROUTER_TITLE || cfg.title || 'openrouter-cli';
|
|
70
60
|
return {
|
|
@@ -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);
|