@aaronjmars/openroutercli 0.1.2
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/LICENSE +21 -0
- package/README.md +212 -0
- package/SKILL.md +345 -0
- package/bin/openrouter.js +11 -0
- package/keys.png +0 -0
- package/openrouter-logo.jpeg +0 -0
- package/package.json +55 -0
- package/src/api.js +121 -0
- package/src/args.js +30 -0
- package/src/cli.js +187 -0
- package/src/commands/account.js +139 -0
- package/src/commands/chat.js +316 -0
- package/src/commands/embed.js +95 -0
- package/src/commands/generation.js +30 -0
- package/src/commands/guardrails.js +226 -0
- package/src/commands/keys.js +147 -0
- package/src/commands/login.js +307 -0
- package/src/commands/messages.js +172 -0
- package/src/commands/misc.js +104 -0
- package/src/commands/models.js +373 -0
- package/src/commands/request.js +100 -0
- package/src/commands/rerank.js +74 -0
- package/src/commands/speech.js +68 -0
- package/src/commands/video.js +128 -0
- package/src/commands/workspaces.js +147 -0
- package/src/config.js +80 -0
- package/src/output.js +84 -0
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
import { parseArgs, authFromValues } from '../args.js';
|
|
2
|
+
import { api } from '../api.js';
|
|
3
|
+
import { c, outln, printResult, table } from '../output.js';
|
|
4
|
+
|
|
5
|
+
export async function orgMembersCommand(argv) {
|
|
6
|
+
const { values } = parseArgs(argv, {
|
|
7
|
+
offset: { type: 'string' },
|
|
8
|
+
limit: { type: 'string' }
|
|
9
|
+
});
|
|
10
|
+
if (values.help) {
|
|
11
|
+
process.stdout.write(
|
|
12
|
+
'Usage: openrouter org members [--offset N] [--limit N]\n\nList organization members. Requires a management key.\n'
|
|
13
|
+
);
|
|
14
|
+
return 0;
|
|
15
|
+
}
|
|
16
|
+
const query = {};
|
|
17
|
+
if (values.offset) query.offset = values.offset;
|
|
18
|
+
if (values.limit) query.limit = values.limit;
|
|
19
|
+
const data = await api('GET', '/organization/members', {
|
|
20
|
+
auth: authFromValues(values),
|
|
21
|
+
requiresManagement: true,
|
|
22
|
+
query
|
|
23
|
+
});
|
|
24
|
+
printResult(data, () => {
|
|
25
|
+
const rows = data.data || [];
|
|
26
|
+
table(rows, [
|
|
27
|
+
{ label: 'user_id', value: (m) => m.user_id || m.id || '' },
|
|
28
|
+
{ label: 'email', value: (m) => m.email || '' },
|
|
29
|
+
{ label: 'role', value: (m) => m.role || '' },
|
|
30
|
+
{ label: 'name', value: (m) => m.name || '' }
|
|
31
|
+
]);
|
|
32
|
+
});
|
|
33
|
+
return 0;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export async function orgCommand(argv) {
|
|
37
|
+
const sub = argv[0];
|
|
38
|
+
const rest = argv.slice(1);
|
|
39
|
+
if (sub === 'members') return orgMembersCommand(rest);
|
|
40
|
+
if (!sub || sub === '-h' || sub === '--help' || sub === 'help') {
|
|
41
|
+
process.stdout.write(
|
|
42
|
+
'Usage: openrouter org <subcommand>\n\nSubcommands:\n members List organization members (management key)\n'
|
|
43
|
+
);
|
|
44
|
+
return sub ? 0 : 1;
|
|
45
|
+
}
|
|
46
|
+
throw new Error(`Unknown org subcommand: ${sub}`);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
export async function zdrCommand(argv) {
|
|
50
|
+
const { values } = parseArgs(argv, {});
|
|
51
|
+
if (values.help) {
|
|
52
|
+
process.stdout.write(
|
|
53
|
+
'Usage: openrouter zdr\n\nPreview the impact of Zero Data Retention on the available endpoints.\n'
|
|
54
|
+
);
|
|
55
|
+
return 0;
|
|
56
|
+
}
|
|
57
|
+
const data = await api('GET', '/endpoints/zdr', {
|
|
58
|
+
auth: authFromValues(values)
|
|
59
|
+
});
|
|
60
|
+
printResult(data, () => outln(JSON.stringify(data, null, 2)));
|
|
61
|
+
return 0;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
export async function authCodeCommand(argv) {
|
|
65
|
+
const { values } = parseArgs(argv, {
|
|
66
|
+
'callback-url': { type: 'string' },
|
|
67
|
+
'code-challenge': { type: 'string' },
|
|
68
|
+
'code-challenge-method': { type: 'string' },
|
|
69
|
+
'expires-at': { type: 'string' },
|
|
70
|
+
'key-label': { type: 'string' },
|
|
71
|
+
limit: { type: 'string' },
|
|
72
|
+
'usage-limit-type': { type: 'string' }
|
|
73
|
+
});
|
|
74
|
+
if (values.help || !values['callback-url']) {
|
|
75
|
+
process.stdout.write(
|
|
76
|
+
'Usage: openrouter auth-code --callback-url <url> [options]\n\n' +
|
|
77
|
+
'Mint a PKCE authorization code so a user can claim a key for your app.\n' +
|
|
78
|
+
'Requires a management key.\n\n' +
|
|
79
|
+
'Options:\n' +
|
|
80
|
+
' --callback-url <url> HTTPS URL on port 443 or 3000 (required)\n' +
|
|
81
|
+
' --code-challenge <s> PKCE challenge\n' +
|
|
82
|
+
' --code-challenge-method <m> S256 | plain\n' +
|
|
83
|
+
' --expires-at <iso> Optional ISO 8601 UTC expiry\n' +
|
|
84
|
+
' --key-label <text> Custom label for the resulting key\n' +
|
|
85
|
+
' --limit <usd> Credit limit\n' +
|
|
86
|
+
' --usage-limit-type <p> daily | weekly | monthly\n'
|
|
87
|
+
);
|
|
88
|
+
return values.help ? 0 : 1;
|
|
89
|
+
}
|
|
90
|
+
const body = { callback_url: values['callback-url'] };
|
|
91
|
+
if (values['code-challenge']) body.code_challenge = values['code-challenge'];
|
|
92
|
+
if (values['code-challenge-method']) body.code_challenge_method = values['code-challenge-method'];
|
|
93
|
+
if (values['expires-at']) body.expires_at = values['expires-at'];
|
|
94
|
+
if (values['key-label']) body.key_label = values['key-label'];
|
|
95
|
+
if (values.limit) body.limit = Number(values.limit);
|
|
96
|
+
if (values['usage-limit-type']) body.usage_limit_type = values['usage-limit-type'];
|
|
97
|
+
const data = await api('POST', '/auth/keys/code', {
|
|
98
|
+
auth: authFromValues(values),
|
|
99
|
+
requiresManagement: true,
|
|
100
|
+
body
|
|
101
|
+
});
|
|
102
|
+
printResult(data, () => outln(JSON.stringify(data, null, 2)));
|
|
103
|
+
return 0;
|
|
104
|
+
}
|
|
@@ -0,0 +1,373 @@
|
|
|
1
|
+
import { parseArgs, authFromValues } from '../args.js';
|
|
2
|
+
import { api } from '../api.js';
|
|
3
|
+
import { c, isJsonMode, outln, printResult, table } from '../output.js';
|
|
4
|
+
|
|
5
|
+
const HELP = `Usage: openrouter models [subcommand] [options]
|
|
6
|
+
|
|
7
|
+
Subcommands:
|
|
8
|
+
list List all models (default)
|
|
9
|
+
show <id> Full detail for a single model (pricing breakdown,
|
|
10
|
+
architecture, supported params, top provider)
|
|
11
|
+
endpoints <author>/<slug> List endpoints (provider variants) for a model
|
|
12
|
+
count Get the total model count
|
|
13
|
+
|
|
14
|
+
Options for list:
|
|
15
|
+
--category <name> programming|roleplay|marketing|technology|...
|
|
16
|
+
--supported <param> Filter by supported parameter (e.g. tools)
|
|
17
|
+
--output-modalities <csv> text,image,audio,embeddings (or "all")
|
|
18
|
+
--filter <substr> Local substring filter on id/name
|
|
19
|
+
--free Only :free models
|
|
20
|
+
--sort <field> id|name|context|prompt|completion (default: as-returned)
|
|
21
|
+
`;
|
|
22
|
+
|
|
23
|
+
function listFormatter(data) {
|
|
24
|
+
const rows = data.data || [];
|
|
25
|
+
table(rows.slice(0, 200), [
|
|
26
|
+
{ label: 'id', value: (m) => m.id },
|
|
27
|
+
{
|
|
28
|
+
label: 'context',
|
|
29
|
+
value: (m) => (m.context_length || '').toLocaleString?.() ?? m.context_length
|
|
30
|
+
},
|
|
31
|
+
{
|
|
32
|
+
label: 'in/out $/M',
|
|
33
|
+
value: (m) => {
|
|
34
|
+
const p = m.pricing || {};
|
|
35
|
+
const fmt = (x) => (x == null ? '-' : (Number(x) * 1e6).toFixed(2));
|
|
36
|
+
return `${fmt(p.prompt)}/${fmt(p.completion)}`;
|
|
37
|
+
}
|
|
38
|
+
},
|
|
39
|
+
{ label: 'name', value: (m) => m.name || '' }
|
|
40
|
+
]);
|
|
41
|
+
if (rows.length > 200) outln(c.dim(`... ${rows.length - 200} more (use --json for the full list)`));
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
function fmtPerMillion(x) {
|
|
45
|
+
if (x == null || x === '') return '-';
|
|
46
|
+
const n = Number(x);
|
|
47
|
+
if (!Number.isFinite(n)) return String(x);
|
|
48
|
+
return '$' + (n * 1e6).toFixed(n * 1e6 < 0.1 ? 4 : 2) + '/M';
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
function fmtFlat(x) {
|
|
52
|
+
if (x == null || x === '') return '-';
|
|
53
|
+
const n = Number(x);
|
|
54
|
+
if (!Number.isFinite(n)) return String(x);
|
|
55
|
+
return '$' + n.toFixed(n < 0.001 ? 6 : 4);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
function renderModelDetail(m) {
|
|
59
|
+
outln(c.bold(m.name || m.id));
|
|
60
|
+
outln(c.dim(m.id));
|
|
61
|
+
if (m.canonical_slug && m.canonical_slug !== m.id) outln(c.dim(`canonical: ${m.canonical_slug}`));
|
|
62
|
+
if (m.hugging_face_id) outln(c.dim(`hugging_face: ${m.hugging_face_id}`));
|
|
63
|
+
outln('');
|
|
64
|
+
if (m.description) {
|
|
65
|
+
outln(m.description);
|
|
66
|
+
outln('');
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
const a = m.architecture || {};
|
|
70
|
+
outln(c.bold('architecture'));
|
|
71
|
+
outln(` modality: ${a.modality ?? '-'}`);
|
|
72
|
+
outln(` input_modalities: ${(a.input_modalities || []).join(', ') || '-'}`);
|
|
73
|
+
outln(` output_modalities: ${(a.output_modalities || []).join(', ') || '-'}`);
|
|
74
|
+
outln(` tokenizer: ${a.tokenizer ?? '-'}`);
|
|
75
|
+
outln(` instruct_type: ${a.instruct_type ?? '-'}`);
|
|
76
|
+
outln('');
|
|
77
|
+
|
|
78
|
+
const tp = m.top_provider || {};
|
|
79
|
+
outln(c.bold('top provider'));
|
|
80
|
+
outln(` context_length: ${tp.context_length ?? m.context_length ?? '-'}`);
|
|
81
|
+
outln(` max_completion_tokens: ${tp.max_completion_tokens ?? '-'}`);
|
|
82
|
+
outln(` is_moderated: ${tp.is_moderated ?? '-'}`);
|
|
83
|
+
outln('');
|
|
84
|
+
|
|
85
|
+
const p = m.pricing || {};
|
|
86
|
+
outln(c.bold('pricing ') + c.dim('(per token unless noted)'));
|
|
87
|
+
const priceRows = [
|
|
88
|
+
['prompt (input)', fmtPerMillion(p.prompt)],
|
|
89
|
+
['completion (output)', fmtPerMillion(p.completion)],
|
|
90
|
+
['input cache read', fmtPerMillion(p.input_cache_read)],
|
|
91
|
+
['input cache write', fmtPerMillion(p.input_cache_write)],
|
|
92
|
+
['internal reasoning', fmtPerMillion(p.internal_reasoning)],
|
|
93
|
+
['image input', p.image != null ? fmtFlat(p.image) + ' /image' : '-'],
|
|
94
|
+
['request', p.request != null ? fmtFlat(p.request) + ' /request' : '-'],
|
|
95
|
+
['web search', p.web_search != null ? fmtFlat(p.web_search) + ' /search' : '-']
|
|
96
|
+
];
|
|
97
|
+
for (const [k, v] of priceRows) {
|
|
98
|
+
if (v === '-') continue;
|
|
99
|
+
outln(` ${k.padEnd(22)} ${v}`);
|
|
100
|
+
}
|
|
101
|
+
outln('');
|
|
102
|
+
|
|
103
|
+
outln(c.bold('supported_parameters'));
|
|
104
|
+
outln(' ' + ((m.supported_parameters || []).join(', ') || '-'));
|
|
105
|
+
outln('');
|
|
106
|
+
|
|
107
|
+
if (m.default_parameters && Object.keys(m.default_parameters).length) {
|
|
108
|
+
outln(c.bold('default_parameters'));
|
|
109
|
+
for (const [k, v] of Object.entries(m.default_parameters)) {
|
|
110
|
+
if (v == null) continue;
|
|
111
|
+
outln(` ${k}: ${v}`);
|
|
112
|
+
}
|
|
113
|
+
outln('');
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
if (m.knowledge_cutoff) outln(c.dim(`knowledge_cutoff: ${m.knowledge_cutoff}`));
|
|
117
|
+
if (m.expiration_date) outln(c.dim(`expiration_date: ${m.expiration_date}`));
|
|
118
|
+
if (m.created) outln(c.dim(`created: ${new Date(m.created * 1000).toISOString()}`));
|
|
119
|
+
if (m.per_request_limits) outln(c.dim(`per_request_limits: ${JSON.stringify(m.per_request_limits)}`));
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
export async function modelsCommand(argv) {
|
|
123
|
+
// Subcommand: show / endpoints / count / list
|
|
124
|
+
const sub = argv[0] && !argv[0].startsWith('-') ? argv[0] : 'list';
|
|
125
|
+
const rest = sub === argv[0] ? argv.slice(1) : argv;
|
|
126
|
+
|
|
127
|
+
if (sub === 'show' || sub === 'info' || sub === 'detail') {
|
|
128
|
+
const { values, positionals } = parseArgs(rest, {});
|
|
129
|
+
if (values.help || positionals.length === 0) {
|
|
130
|
+
process.stdout.write(
|
|
131
|
+
'Usage: openrouter models show <model-id>\n\nShow full details (pricing, architecture, capabilities) for one model.\n'
|
|
132
|
+
);
|
|
133
|
+
return values.help ? 0 : 1;
|
|
134
|
+
}
|
|
135
|
+
const target = positionals[0].toLowerCase();
|
|
136
|
+
const data = await api('GET', '/models', {
|
|
137
|
+
auth: authFromValues(values),
|
|
138
|
+
requireAuth: false
|
|
139
|
+
});
|
|
140
|
+
const rows = data.data || [];
|
|
141
|
+
let m = rows.find((r) => (r.id || '').toLowerCase() === target);
|
|
142
|
+
if (!m) m = rows.find((r) => (r.canonical_slug || '').toLowerCase() === target);
|
|
143
|
+
if (!m) {
|
|
144
|
+
const matches = rows.filter(
|
|
145
|
+
(r) =>
|
|
146
|
+
(r.id || '').toLowerCase().includes(target) ||
|
|
147
|
+
(r.name || '').toLowerCase().includes(target)
|
|
148
|
+
);
|
|
149
|
+
if (matches.length === 1) m = matches[0];
|
|
150
|
+
else if (matches.length > 1) {
|
|
151
|
+
throw new Error(
|
|
152
|
+
`Multiple matches for "${positionals[0]}":\n ${matches
|
|
153
|
+
.slice(0, 10)
|
|
154
|
+
.map((r) => r.id)
|
|
155
|
+
.join('\n ')}${matches.length > 10 ? `\n ...and ${matches.length - 10} more` : ''}`
|
|
156
|
+
);
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
if (!m) throw new Error(`No model found matching "${positionals[0]}".`);
|
|
160
|
+
|
|
161
|
+
if (isJsonMode()) {
|
|
162
|
+
printResult(m);
|
|
163
|
+
return 0;
|
|
164
|
+
}
|
|
165
|
+
renderModelDetail(m);
|
|
166
|
+
return 0;
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
if (sub === 'endpoints') {
|
|
170
|
+
const { values, positionals } = parseArgs(rest, {
|
|
171
|
+
sort: { type: 'string' },
|
|
172
|
+
best: { type: 'boolean' }
|
|
173
|
+
});
|
|
174
|
+
if (values.help || positionals.length === 0) {
|
|
175
|
+
process.stdout.write(
|
|
176
|
+
'Usage: openrouter models endpoints <author>/<slug> [options]\n\n' +
|
|
177
|
+
'List provider endpoints (variants) for a model, with throughput,\n' +
|
|
178
|
+
'latency, uptime, and pricing.\n\n' +
|
|
179
|
+
'Options:\n' +
|
|
180
|
+
' --sort <field> throughput | latency | prompt | completion | uptime | context\n' +
|
|
181
|
+
' Sort the list before display (best first).\n' +
|
|
182
|
+
' --best Show only the top row after sorting.\n'
|
|
183
|
+
);
|
|
184
|
+
return values.help ? 0 : 1;
|
|
185
|
+
}
|
|
186
|
+
const target = positionals[0];
|
|
187
|
+
const slash = target.indexOf('/');
|
|
188
|
+
if (slash === -1) throw new Error('Expected <author>/<slug>');
|
|
189
|
+
const author = target.slice(0, slash);
|
|
190
|
+
const slug = target.slice(slash + 1);
|
|
191
|
+
const data = await api('GET', `/models/${author}/${slug}/endpoints`, {
|
|
192
|
+
auth: authFromValues(values),
|
|
193
|
+
requireAuth: false
|
|
194
|
+
});
|
|
195
|
+
let eps = (data.data && data.data.endpoints) || data.endpoints || [];
|
|
196
|
+
if (values.sort) {
|
|
197
|
+
const key = values.sort;
|
|
198
|
+
const score = (e) => {
|
|
199
|
+
const p = e.pricing || {};
|
|
200
|
+
const t = e.throughput_last_30m || {};
|
|
201
|
+
const l = e.latency_last_30m || {};
|
|
202
|
+
switch (key) {
|
|
203
|
+
case 'throughput': return -(t.p50 ?? -Infinity);
|
|
204
|
+
case 'latency': return l.p50 ?? Infinity;
|
|
205
|
+
case 'prompt': return Number(p.prompt ?? Infinity);
|
|
206
|
+
case 'completion': return Number(p.completion ?? Infinity);
|
|
207
|
+
case 'uptime': return -(e.uptime_last_30m ?? -Infinity);
|
|
208
|
+
case 'context': return -(e.context_length ?? -Infinity);
|
|
209
|
+
default: return 0;
|
|
210
|
+
}
|
|
211
|
+
};
|
|
212
|
+
eps = [...eps].sort((a, b) => {
|
|
213
|
+
const va = score(a), vb = score(b);
|
|
214
|
+
if (Number.isNaN(va)) return 1;
|
|
215
|
+
if (Number.isNaN(vb)) return -1;
|
|
216
|
+
return va - vb;
|
|
217
|
+
});
|
|
218
|
+
}
|
|
219
|
+
if (values.best) eps = eps.slice(0, 1);
|
|
220
|
+
|
|
221
|
+
printResult({ ...data, data: { ...(data.data || {}), endpoints: eps } }, () => {
|
|
222
|
+
table(eps, [
|
|
223
|
+
{ label: 'provider', value: (e) => e.provider_name || e.name || '' },
|
|
224
|
+
{ label: 'context', value: (e) => e.context_length ?? '' },
|
|
225
|
+
{ label: 'max_out', value: (e) => e.max_completion_tokens ?? '' },
|
|
226
|
+
{
|
|
227
|
+
label: 'in/out $/M',
|
|
228
|
+
value: (e) => {
|
|
229
|
+
const p = e.pricing || {};
|
|
230
|
+
const fmt = (x) => (x == null ? '-' : (Number(x) * 1e6).toFixed(2));
|
|
231
|
+
return `${fmt(p.prompt)}/${fmt(p.completion)}`;
|
|
232
|
+
}
|
|
233
|
+
},
|
|
234
|
+
{
|
|
235
|
+
label: 'tput p50',
|
|
236
|
+
value: (e) =>
|
|
237
|
+
e.throughput_last_30m?.p50 != null
|
|
238
|
+
? Number(e.throughput_last_30m.p50).toFixed(0) + ' tok/s'
|
|
239
|
+
: ''
|
|
240
|
+
},
|
|
241
|
+
{
|
|
242
|
+
label: 'lat p50',
|
|
243
|
+
value: (e) =>
|
|
244
|
+
e.latency_last_30m?.p50 != null
|
|
245
|
+
? Number(e.latency_last_30m.p50).toFixed(0) + ' ms'
|
|
246
|
+
: ''
|
|
247
|
+
},
|
|
248
|
+
{
|
|
249
|
+
label: 'uptime 30m',
|
|
250
|
+
value: (e) =>
|
|
251
|
+
e.uptime_last_30m != null ? Number(e.uptime_last_30m).toFixed(1) + '%' : ''
|
|
252
|
+
},
|
|
253
|
+
{ label: 'quant', value: (e) => e.quantization || '' }
|
|
254
|
+
]);
|
|
255
|
+
});
|
|
256
|
+
return 0;
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
if (sub === 'user') {
|
|
260
|
+
const { values } = parseArgs(rest, {
|
|
261
|
+
workspace: { type: 'string' }
|
|
262
|
+
});
|
|
263
|
+
if (values.help) {
|
|
264
|
+
process.stdout.write(
|
|
265
|
+
'Usage: openrouter models user [--workspace <id|slug>]\n\nList models filtered by your workspace provider preferences, privacy, and guardrails.\n'
|
|
266
|
+
);
|
|
267
|
+
return 0;
|
|
268
|
+
}
|
|
269
|
+
const query = {};
|
|
270
|
+
if (values.workspace) query.workspace_id = values.workspace;
|
|
271
|
+
const data = await api('GET', '/models/user', {
|
|
272
|
+
auth: authFromValues(values),
|
|
273
|
+
query
|
|
274
|
+
});
|
|
275
|
+
printResult(data, () => listFormatter(data));
|
|
276
|
+
return 0;
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
if (sub === 'count') {
|
|
280
|
+
const { values } = parseArgs(rest, {});
|
|
281
|
+
const data = await api('GET', '/models/count', {
|
|
282
|
+
auth: authFromValues(values),
|
|
283
|
+
requireAuth: false
|
|
284
|
+
});
|
|
285
|
+
printResult(data, () => outln(JSON.stringify(data)));
|
|
286
|
+
return 0;
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
// list
|
|
290
|
+
const { values } = parseArgs(rest, {
|
|
291
|
+
category: { type: 'string' },
|
|
292
|
+
supported: { type: 'string' },
|
|
293
|
+
'output-modalities': { type: 'string' },
|
|
294
|
+
filter: { type: 'string' },
|
|
295
|
+
free: { type: 'boolean' },
|
|
296
|
+
sort: { type: 'string' }
|
|
297
|
+
});
|
|
298
|
+
if (values.help) {
|
|
299
|
+
process.stdout.write(HELP);
|
|
300
|
+
return 0;
|
|
301
|
+
}
|
|
302
|
+
const query = {};
|
|
303
|
+
if (values.category) query.category = values.category;
|
|
304
|
+
if (values.supported) query.supported_parameters = values.supported;
|
|
305
|
+
if (values['output-modalities'])
|
|
306
|
+
query.output_modalities = values['output-modalities'];
|
|
307
|
+
|
|
308
|
+
const data = await api('GET', '/models', {
|
|
309
|
+
auth: authFromValues(values),
|
|
310
|
+
query,
|
|
311
|
+
requireAuth: false
|
|
312
|
+
});
|
|
313
|
+
|
|
314
|
+
let rows = data.data || [];
|
|
315
|
+
if (values.filter) {
|
|
316
|
+
const f = values.filter.toLowerCase();
|
|
317
|
+
rows = rows.filter(
|
|
318
|
+
(m) =>
|
|
319
|
+
(m.id || '').toLowerCase().includes(f) ||
|
|
320
|
+
(m.name || '').toLowerCase().includes(f)
|
|
321
|
+
);
|
|
322
|
+
}
|
|
323
|
+
if (values.free) rows = rows.filter((m) => (m.id || '').endsWith(':free'));
|
|
324
|
+
|
|
325
|
+
if (values.sort) {
|
|
326
|
+
const key = values.sort;
|
|
327
|
+
const get = (m) => {
|
|
328
|
+
switch (key) {
|
|
329
|
+
case 'id': return m.id || '';
|
|
330
|
+
case 'name': return m.name || '';
|
|
331
|
+
case 'context': return -(m.context_length || 0);
|
|
332
|
+
case 'prompt': return Number(m.pricing?.prompt ?? Infinity);
|
|
333
|
+
case 'completion': return Number(m.pricing?.completion ?? Infinity);
|
|
334
|
+
default: return 0;
|
|
335
|
+
}
|
|
336
|
+
};
|
|
337
|
+
rows = [...rows].sort((a, b) => {
|
|
338
|
+
const va = get(a), vb = get(b);
|
|
339
|
+
if (va < vb) return -1;
|
|
340
|
+
if (va > vb) return 1;
|
|
341
|
+
return 0;
|
|
342
|
+
});
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
const filtered = { ...data, data: rows };
|
|
346
|
+
printResult(filtered, () => listFormatter(filtered));
|
|
347
|
+
return 0;
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
export async function providersCommand(argv) {
|
|
351
|
+
const { values } = parseArgs(argv, {});
|
|
352
|
+
if (values.help) {
|
|
353
|
+
process.stdout.write('Usage: openrouter providers\n\nList all providers.\n');
|
|
354
|
+
return 0;
|
|
355
|
+
}
|
|
356
|
+
const data = await api('GET', '/providers', {
|
|
357
|
+
auth: authFromValues(values),
|
|
358
|
+
requireAuth: false
|
|
359
|
+
});
|
|
360
|
+
printResult(data, () => {
|
|
361
|
+
const rows = data.data || [];
|
|
362
|
+
table(rows, [
|
|
363
|
+
{ label: 'slug', value: (p) => p.slug || p.id || '' },
|
|
364
|
+
{ label: 'name', value: (p) => p.name || '' },
|
|
365
|
+
{ label: 'hq', value: (p) => p.headquarters || '' },
|
|
366
|
+
{ label: 'datacenters', value: (p) => (p.datacenters || []).join(',') || '' },
|
|
367
|
+
{ label: 'privacy', value: (p) => (p.privacy_policy_url ? 'yes' : '') },
|
|
368
|
+
{ label: 'tos', value: (p) => (p.terms_of_service_url ? 'yes' : '') },
|
|
369
|
+
{ label: 'status', value: (p) => (p.status_page_url ? 'yes' : '') }
|
|
370
|
+
]);
|
|
371
|
+
});
|
|
372
|
+
return 0;
|
|
373
|
+
}
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
import { stdin } from 'node:process';
|
|
2
|
+
import { parseArgs, authFromValues } from '../args.js';
|
|
3
|
+
import { api } from '../api.js';
|
|
4
|
+
import { printJSON, out } from '../output.js';
|
|
5
|
+
|
|
6
|
+
const HELP = `Usage: openrouter request <METHOD> <path> [options]
|
|
7
|
+
|
|
8
|
+
Make a raw authenticated request to the OpenRouter API.
|
|
9
|
+
|
|
10
|
+
Examples:
|
|
11
|
+
openrouter request GET /credits
|
|
12
|
+
openrouter request POST /chat/completions --body @body.json
|
|
13
|
+
openrouter request GET /models --query category=programming
|
|
14
|
+
|
|
15
|
+
Options:
|
|
16
|
+
--body <json|@file> JSON body. @file reads from disk. - reads stdin.
|
|
17
|
+
--query <k=v> Query parameter (repeatable)
|
|
18
|
+
--header <k:v> Extra request header (repeatable)
|
|
19
|
+
--binary <out|-> Treat response as binary; write to file or stdout
|
|
20
|
+
--raw Print raw text response (no JSON parsing)
|
|
21
|
+
`;
|
|
22
|
+
|
|
23
|
+
async function readStdin() {
|
|
24
|
+
let data = '';
|
|
25
|
+
for await (const chunk of stdin) data += chunk;
|
|
26
|
+
return data;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
async function loadBody(value) {
|
|
30
|
+
if (value === '-') return JSON.parse(await readStdin());
|
|
31
|
+
if (value.startsWith('@')) {
|
|
32
|
+
const { readFile } = await import('node:fs/promises');
|
|
33
|
+
return JSON.parse(await readFile(value.slice(1), 'utf8'));
|
|
34
|
+
}
|
|
35
|
+
return JSON.parse(value);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export async function requestCommand(argv) {
|
|
39
|
+
const { values, positionals } = parseArgs(argv, {
|
|
40
|
+
body: { type: 'string' },
|
|
41
|
+
query: { type: 'string', multiple: true },
|
|
42
|
+
header: { type: 'string', multiple: true },
|
|
43
|
+
binary: { type: 'string' },
|
|
44
|
+
raw: { type: 'boolean' }
|
|
45
|
+
});
|
|
46
|
+
if (values.help) {
|
|
47
|
+
process.stdout.write(HELP);
|
|
48
|
+
return 0;
|
|
49
|
+
}
|
|
50
|
+
if (positionals.length < 2) {
|
|
51
|
+
process.stdout.write(HELP);
|
|
52
|
+
return 1;
|
|
53
|
+
}
|
|
54
|
+
const method = positionals[0];
|
|
55
|
+
const path = positionals[1];
|
|
56
|
+
|
|
57
|
+
const query = {};
|
|
58
|
+
for (const q of values.query || []) {
|
|
59
|
+
const i = q.indexOf('=');
|
|
60
|
+
if (i === -1) throw new Error(`bad --query (need k=v): ${q}`);
|
|
61
|
+
query[q.slice(0, i)] = q.slice(i + 1);
|
|
62
|
+
}
|
|
63
|
+
const headers = {};
|
|
64
|
+
for (const h of values.header || []) {
|
|
65
|
+
const i = h.indexOf(':');
|
|
66
|
+
if (i === -1) throw new Error(`bad --header (need k:v): ${h}`);
|
|
67
|
+
headers[h.slice(0, i).trim()] = h.slice(i + 1).trim();
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
const opts = {
|
|
71
|
+
auth: authFromValues(values),
|
|
72
|
+
headers,
|
|
73
|
+
query: Object.keys(query).length ? query : undefined
|
|
74
|
+
};
|
|
75
|
+
if (values.body !== undefined) opts.body = await loadBody(values.body);
|
|
76
|
+
|
|
77
|
+
if (values.binary) {
|
|
78
|
+
opts.binary = true;
|
|
79
|
+
const bytes = await api(method, path, opts);
|
|
80
|
+
if (values.binary === '-') process.stdout.write(Buffer.from(bytes));
|
|
81
|
+
else {
|
|
82
|
+
const { writeFile } = await import('node:fs/promises');
|
|
83
|
+
await writeFile(values.binary, Buffer.from(bytes));
|
|
84
|
+
}
|
|
85
|
+
return 0;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
if (values.raw) {
|
|
89
|
+
opts.raw = true;
|
|
90
|
+
const res = await api(method, path, opts);
|
|
91
|
+
process.stdout.write(await res.text());
|
|
92
|
+
return res.ok ? 0 : 2;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
const data = await api(method, path, opts);
|
|
96
|
+
if (data == null) return 0;
|
|
97
|
+
if (typeof data === 'string') out(data);
|
|
98
|
+
else printJSON(data);
|
|
99
|
+
return 0;
|
|
100
|
+
}
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import { stdin } from 'node:process';
|
|
2
|
+
import { parseArgs, authFromValues } from '../args.js';
|
|
3
|
+
import { api } from '../api.js';
|
|
4
|
+
import { c, outln, printResult } from '../output.js';
|
|
5
|
+
|
|
6
|
+
const HELP = `Usage: openrouter rerank --model <id> --query <q> [doc...] [options]
|
|
7
|
+
|
|
8
|
+
Rerank documents against a query.
|
|
9
|
+
|
|
10
|
+
Options:
|
|
11
|
+
-m, --model <id> Reranking model (required)
|
|
12
|
+
--query <text> Query string (required)
|
|
13
|
+
-d, --doc <text> Document (repeatable). Or pass docs as positionals.
|
|
14
|
+
--docs-file <p> Read docs from file (one per line). - for stdin.
|
|
15
|
+
--top-n <n> Return only the top N
|
|
16
|
+
--provider <json>
|
|
17
|
+
`;
|
|
18
|
+
|
|
19
|
+
async function readDocsFromStdin() {
|
|
20
|
+
let data = '';
|
|
21
|
+
for await (const chunk of stdin) data += chunk;
|
|
22
|
+
return data.split(/\r?\n/).map((l) => l.trim()).filter(Boolean);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export async function rerankCommand(argv) {
|
|
26
|
+
const { values, positionals } = parseArgs(argv, {
|
|
27
|
+
model: { type: 'string', short: 'm' },
|
|
28
|
+
query: { type: 'string' },
|
|
29
|
+
doc: { type: 'string', short: 'd', multiple: true },
|
|
30
|
+
'docs-file': { type: 'string' },
|
|
31
|
+
'top-n': { type: 'string' },
|
|
32
|
+
provider: { type: 'string' }
|
|
33
|
+
});
|
|
34
|
+
if (values.help) {
|
|
35
|
+
process.stdout.write(HELP);
|
|
36
|
+
return 0;
|
|
37
|
+
}
|
|
38
|
+
if (!values.model) throw new Error('--model is required');
|
|
39
|
+
if (!values.query) throw new Error('--query is required');
|
|
40
|
+
|
|
41
|
+
let docs = [...(values.doc || []), ...positionals];
|
|
42
|
+
if (values['docs-file']) {
|
|
43
|
+
if (values['docs-file'] === '-') docs = docs.concat(await readDocsFromStdin());
|
|
44
|
+
else {
|
|
45
|
+
const { readFile } = await import('node:fs/promises');
|
|
46
|
+
const text = await readFile(values['docs-file'], 'utf8');
|
|
47
|
+
docs = docs.concat(text.split(/\r?\n/).map((l) => l.trim()).filter(Boolean));
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
if (!docs.length) throw new Error('No documents. Pass via positionals, --doc, or --docs-file.');
|
|
51
|
+
|
|
52
|
+
const body = {
|
|
53
|
+
model: values.model,
|
|
54
|
+
query: values.query,
|
|
55
|
+
documents: docs
|
|
56
|
+
};
|
|
57
|
+
if (values['top-n']) body.top_n = Number(values['top-n']);
|
|
58
|
+
if (values.provider) body.provider = JSON.parse(values.provider);
|
|
59
|
+
|
|
60
|
+
const data = await api('POST', '/rerank', {
|
|
61
|
+
auth: authFromValues(values),
|
|
62
|
+
body
|
|
63
|
+
});
|
|
64
|
+
printResult(data, () => {
|
|
65
|
+
const results = data.results || data.data || [];
|
|
66
|
+
for (const r of results) {
|
|
67
|
+
const idx = r.index;
|
|
68
|
+
const score = r.relevance_score ?? r.score;
|
|
69
|
+
const text = (r.document && r.document.text) || docs[idx] || '';
|
|
70
|
+
outln(`${c.bold(score?.toFixed?.(4) ?? score)} ${c.dim(`#${idx}`)} ${text.slice(0, 120)}`);
|
|
71
|
+
}
|
|
72
|
+
});
|
|
73
|
+
return 0;
|
|
74
|
+
}
|