@aaronjmars/openroutercli 0.1.3 → 0.1.6
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 +36 -39
- package/SKILL.md +18 -17
- package/package.json +1 -1
- package/src/api.js +11 -0
- package/src/args.js +40 -0
- package/src/cli.js +12 -15
- package/src/commands/account.js +11 -5
- package/src/commands/chat.js +104 -76
- package/src/commands/embed.js +8 -16
- package/src/commands/guardrails.js +33 -29
- package/src/commands/keys.js +16 -7
- package/src/commands/login.js +25 -20
- package/src/commands/messages.js +36 -34
- package/src/commands/misc.js +14 -13
- package/src/commands/models.js +44 -25
- package/src/commands/request.js +1 -1
- package/src/commands/rerank.js +2 -2
- package/src/commands/speech.js +4 -19
- package/src/commands/video.js +31 -24
- package/src/commands/workspaces.js +20 -21
- package/src/config.js +5 -15
- package/src/output.js +19 -9
package/src/commands/chat.js
CHANGED
|
@@ -1,16 +1,19 @@
|
|
|
1
1
|
import readline from 'node:readline/promises';
|
|
2
2
|
import { stdin, stdout } from 'node:process';
|
|
3
|
-
import { parseArgs, authFromValues } from '../args.js';
|
|
4
|
-
import { api, sseStream } from '../api.js';
|
|
3
|
+
import { parseArgs, authFromValues, numberOption, shouldStream } from '../args.js';
|
|
4
|
+
import { api, assertNoStreamError, sseStream } from '../api.js';
|
|
5
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'];
|
|
9
|
+
|
|
7
10
|
const HELP = `Usage: openrouter chat [prompt...] [options]
|
|
8
11
|
|
|
9
12
|
Send a chat completion. With no prompt and a TTY, starts an interactive REPL.
|
|
10
13
|
With no prompt and stdin piped, reads the prompt from stdin.
|
|
11
14
|
|
|
12
15
|
Options:
|
|
13
|
-
-m, --model <id> Model id (default:
|
|
16
|
+
-m, --model <id> Model id (default: ${DEFAULT_MODEL})
|
|
14
17
|
--models <csv> Fallback list (comma-separated)
|
|
15
18
|
-s, --system <text> System message
|
|
16
19
|
--stream Stream tokens (default when TTY)
|
|
@@ -32,9 +35,11 @@ Options:
|
|
|
32
35
|
--reasoning <effort> low | medium | high
|
|
33
36
|
--provider <json> Provider routing options as JSON
|
|
34
37
|
--image <url|path> Attach an image (repeatable). URL or local file
|
|
35
|
-
--raw Print full JSON response (non-streaming
|
|
38
|
+
--raw Print full JSON response (non-streaming; the REPL
|
|
39
|
+
always streams, so it is ignored there)
|
|
36
40
|
--usage Print usage info to stderr after completion
|
|
37
|
-
--interactive, -i Force interactive REPL
|
|
41
|
+
--interactive, -i Force interactive REPL (needs a terminal).
|
|
42
|
+
--image applies to the opening turn only.
|
|
38
43
|
-h, --help
|
|
39
44
|
`;
|
|
40
45
|
|
|
@@ -52,7 +57,7 @@ async function loadJsonOrFile(value) {
|
|
|
52
57
|
return JSON.parse(text);
|
|
53
58
|
}
|
|
54
59
|
if (value.startsWith('{') || value.startsWith('[')) return JSON.parse(value);
|
|
55
|
-
//
|
|
60
|
+
// Bare value: try it as a file path first, then as inline JSON.
|
|
56
61
|
try {
|
|
57
62
|
const { readFile } = await import('node:fs/promises');
|
|
58
63
|
const text = await readFile(value, 'utf8');
|
|
@@ -83,41 +88,40 @@ async function imageToContent(value) {
|
|
|
83
88
|
};
|
|
84
89
|
}
|
|
85
90
|
|
|
91
|
+
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'
|
|
101
|
+
};
|
|
102
|
+
|
|
86
103
|
function applySamplingOptions(body, values) {
|
|
87
104
|
if (values.models) body.models = values.models.split(',').map((s) => s.trim());
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
if (values.seed != null) body.seed = Number(values.seed);
|
|
105
|
+
for (const [flag, field] of Object.entries(NUMERIC_SAMPLING)) {
|
|
106
|
+
const n = numberOption(values[flag], `--${flag}`);
|
|
107
|
+
if (n !== undefined) body[field] = n;
|
|
108
|
+
}
|
|
93
109
|
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
110
|
if (values.reasoning) body.reasoning = { effort: values.reasoning };
|
|
99
111
|
}
|
|
100
112
|
|
|
101
|
-
async function
|
|
102
|
-
const
|
|
103
|
-
if (
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
const images
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
if (prompt) parts.push({ type: 'text', text: prompt });
|
|
110
|
-
for (const img of images) parts.push(await imageToContent(img));
|
|
111
|
-
userContent = parts;
|
|
112
|
-
} else {
|
|
113
|
-
userContent = prompt;
|
|
114
|
-
}
|
|
115
|
-
messages.push({ role: 'user', content: userContent });
|
|
113
|
+
async function buildUserContent(values, prompt, withImages) {
|
|
114
|
+
const images = withImages ? values.image || [] : [];
|
|
115
|
+
if (images.length === 0) return prompt;
|
|
116
|
+
const parts = [];
|
|
117
|
+
if (prompt) parts.push({ type: 'text', text: prompt });
|
|
118
|
+
for (const img of images) parts.push(await imageToContent(img));
|
|
119
|
+
return parts;
|
|
120
|
+
}
|
|
116
121
|
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
};
|
|
122
|
+
// Options that apply to the request whichever turn it is, so an interactive
|
|
123
|
+
// session is configured the same way a one-shot run is.
|
|
124
|
+
async function applyRequestOptions(body, values) {
|
|
121
125
|
applySamplingOptions(body, values);
|
|
122
126
|
|
|
123
127
|
if (values['json-output']) body.response_format = { type: 'json_object' };
|
|
@@ -143,9 +147,26 @@ async function buildBody(values, prompt) {
|
|
|
143
147
|
else body.tool_choice = { type: 'function', function: { name: v } };
|
|
144
148
|
}
|
|
145
149
|
if (values.provider) body.provider = await loadJsonOrFile(values.provider);
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
async function buildBody(values, prompt) {
|
|
153
|
+
const messages = [];
|
|
154
|
+
if (values.system) messages.push({ role: 'system', content: values.system });
|
|
155
|
+
messages.push({
|
|
156
|
+
role: 'user',
|
|
157
|
+
content: await buildUserContent(values, prompt, true)
|
|
158
|
+
});
|
|
159
|
+
const body = { model: values.model || DEFAULT_MODEL, messages };
|
|
160
|
+
await applyRequestOptions(body, values);
|
|
146
161
|
return body;
|
|
147
162
|
}
|
|
148
163
|
|
|
164
|
+
function printUsage(model, usage) {
|
|
165
|
+
info(
|
|
166
|
+
`model=${model || ''} prompt=${usage.prompt_tokens} completion=${usage.completion_tokens} total=${usage.total_tokens}`
|
|
167
|
+
);
|
|
168
|
+
}
|
|
169
|
+
|
|
149
170
|
async function streamResponse(body, auth) {
|
|
150
171
|
const res = await api('POST', '/chat/completions', {
|
|
151
172
|
auth,
|
|
@@ -156,25 +177,30 @@ async function streamResponse(body, auth) {
|
|
|
156
177
|
let usage = null;
|
|
157
178
|
let model = null;
|
|
158
179
|
let finishReason = null;
|
|
180
|
+
let text = '';
|
|
159
181
|
for await (const evt of sseStream(res)) {
|
|
182
|
+
assertNoStreamError(evt);
|
|
160
183
|
if (evt.usage) usage = evt.usage;
|
|
161
184
|
if (evt.model) model = evt.model;
|
|
162
185
|
const choice = evt.choices && evt.choices[0];
|
|
163
186
|
if (!choice) continue;
|
|
164
187
|
const delta = choice.delta || choice.message;
|
|
165
188
|
if (!delta) continue;
|
|
166
|
-
if (typeof delta.content === 'string')
|
|
167
|
-
|
|
189
|
+
if (typeof delta.content === 'string') {
|
|
190
|
+
out(delta.content);
|
|
191
|
+
text += delta.content;
|
|
192
|
+
} else if (Array.isArray(delta.content)) {
|
|
168
193
|
for (const part of delta.content) {
|
|
169
|
-
if (part.type === 'text' && part.text)
|
|
194
|
+
if (part.type === 'text' && part.text) {
|
|
195
|
+
out(part.text);
|
|
196
|
+
text += part.text;
|
|
197
|
+
}
|
|
170
198
|
}
|
|
171
199
|
}
|
|
172
200
|
if (delta.reasoning) {
|
|
173
|
-
// reasoning trace; show in dim if TTY
|
|
174
201
|
if (process.stdout.isTTY) out(c.dim(delta.reasoning));
|
|
175
202
|
}
|
|
176
203
|
if (delta.tool_calls) {
|
|
177
|
-
// Print tool calls JSON-compactly
|
|
178
204
|
for (const tc of delta.tool_calls) {
|
|
179
205
|
if (tc.function && tc.function.arguments) out(tc.function.arguments);
|
|
180
206
|
}
|
|
@@ -182,7 +208,7 @@ async function streamResponse(body, auth) {
|
|
|
182
208
|
if (choice.finish_reason) finishReason = choice.finish_reason;
|
|
183
209
|
}
|
|
184
210
|
if (process.stdout.isTTY) out('\n');
|
|
185
|
-
return { usage, model, finishReason };
|
|
211
|
+
return { usage, model, finishReason, text };
|
|
186
212
|
}
|
|
187
213
|
|
|
188
214
|
export async function chatCommand(argv) {
|
|
@@ -220,6 +246,16 @@ export async function chatCommand(argv) {
|
|
|
220
246
|
}
|
|
221
247
|
|
|
222
248
|
const auth = authFromValues(values);
|
|
249
|
+
|
|
250
|
+
// Must precede readStdinIfPiped: that call drains stdin, which would leave
|
|
251
|
+
// the REPL's readline with an exhausted stream and no way to report why.
|
|
252
|
+
if (values.interactive && !stdin.isTTY) {
|
|
253
|
+
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'
|
|
256
|
+
);
|
|
257
|
+
}
|
|
258
|
+
|
|
223
259
|
const piped = await readStdinIfPiped();
|
|
224
260
|
let prompt = positionals.join(' ').trim();
|
|
225
261
|
if (piped) prompt = prompt ? `${prompt}\n\n${piped}` : piped;
|
|
@@ -237,12 +273,7 @@ export async function chatCommand(argv) {
|
|
|
237
273
|
|
|
238
274
|
const body = await buildBody(values, prompt);
|
|
239
275
|
|
|
240
|
-
|
|
241
|
-
const shouldStream =
|
|
242
|
-
values.stream ||
|
|
243
|
-
(!values['no-stream'] && !values.raw && !isJsonMode() && stdout.isTTY);
|
|
244
|
-
|
|
245
|
-
if (!shouldStream) {
|
|
276
|
+
if (!shouldStream(values)) {
|
|
246
277
|
const data = await api('POST', '/chat/completions', { auth, body });
|
|
247
278
|
if (isJsonMode() || values.raw) {
|
|
248
279
|
printJSON(data);
|
|
@@ -254,20 +285,21 @@ export async function chatCommand(argv) {
|
|
|
254
285
|
for (const part of msg.content)
|
|
255
286
|
if (part.type === 'text') outln(part.text);
|
|
256
287
|
} else outln(JSON.stringify(data));
|
|
257
|
-
if (values.usage && data.usage)
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
}
|
|
288
|
+
if (values.usage && data.usage) printUsage(data.model, data.usage);
|
|
289
|
+
}
|
|
290
|
+
const finish = data.choices && data.choices[0] && data.choices[0].finish_reason;
|
|
291
|
+
if (ERROR_FINISH_REASONS.includes(finish)) {
|
|
292
|
+
process.stderr.write(`error: generation stopped (${finish})\n`);
|
|
293
|
+
return 2;
|
|
262
294
|
}
|
|
263
295
|
return 0;
|
|
264
296
|
}
|
|
265
297
|
|
|
266
298
|
const meta = await streamResponse(body, auth);
|
|
267
|
-
if (values.usage && meta.usage)
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
299
|
+
if (values.usage && meta.usage) printUsage(meta.model, meta.usage);
|
|
300
|
+
if (ERROR_FINISH_REASONS.includes(meta.finishReason)) {
|
|
301
|
+
process.stderr.write(`error: generation stopped (${meta.finishReason})\n`);
|
|
302
|
+
return 2;
|
|
271
303
|
}
|
|
272
304
|
return 0;
|
|
273
305
|
}
|
|
@@ -277,8 +309,9 @@ async function repl(values, auth) {
|
|
|
277
309
|
const history = [];
|
|
278
310
|
if (values.system) history.push({ role: 'system', content: values.system });
|
|
279
311
|
outln(c.dim('OpenRouter chat. /exit to quit, /reset to clear history, /model <id> to switch.'));
|
|
280
|
-
let model = values.model ||
|
|
312
|
+
let model = values.model || DEFAULT_MODEL;
|
|
281
313
|
outln(c.dim(`model: ${model}`));
|
|
314
|
+
let firstTurn = true;
|
|
282
315
|
try {
|
|
283
316
|
while (true) {
|
|
284
317
|
let line;
|
|
@@ -295,6 +328,7 @@ async function repl(values, auth) {
|
|
|
295
328
|
history.length = 0;
|
|
296
329
|
if (values.system)
|
|
297
330
|
history.push({ role: 'system', content: values.system });
|
|
331
|
+
firstTurn = true;
|
|
298
332
|
outln(c.dim('(history cleared)'));
|
|
299
333
|
continue;
|
|
300
334
|
}
|
|
@@ -303,27 +337,22 @@ async function repl(values, auth) {
|
|
|
303
337
|
outln(c.dim(`model: ${model}`));
|
|
304
338
|
continue;
|
|
305
339
|
}
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
340
|
+
// --image attaches to the opening turn only; resending it every turn
|
|
341
|
+
// would re-upload the same attachment.
|
|
342
|
+
history.push({
|
|
343
|
+
role: 'user',
|
|
344
|
+
content: await buildUserContent(values, trimmed, firstTurn)
|
|
345
|
+
});
|
|
346
|
+
const body = { model, messages: history };
|
|
347
|
+
await applyRequestOptions(body, values);
|
|
309
348
|
try {
|
|
310
|
-
const
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
let assistant = '';
|
|
317
|
-
for await (const evt of sseStream(res)) {
|
|
318
|
-
const delta = evt.choices?.[0]?.delta;
|
|
319
|
-
if (delta?.content) {
|
|
320
|
-
out(delta.content);
|
|
321
|
-
assistant += delta.content;
|
|
322
|
-
}
|
|
323
|
-
if (delta?.reasoning && process.stdout.isTTY) out(c.dim(delta.reasoning));
|
|
349
|
+
const meta = await streamResponse(body, auth);
|
|
350
|
+
history.push({ role: 'assistant', content: meta.text });
|
|
351
|
+
firstTurn = false;
|
|
352
|
+
if (values.usage && meta.usage) printUsage(meta.model, meta.usage);
|
|
353
|
+
if (ERROR_FINISH_REASONS.includes(meta.finishReason)) {
|
|
354
|
+
outln(c.red(`generation stopped (${meta.finishReason})`));
|
|
324
355
|
}
|
|
325
|
-
out('\n');
|
|
326
|
-
history.push({ role: 'assistant', content: assistant });
|
|
327
356
|
} catch (err) {
|
|
328
357
|
outln(c.red(err.message));
|
|
329
358
|
// Drop the orphaned user message so the next turn doesn't send a
|
|
@@ -336,5 +365,4 @@ async function repl(values, auth) {
|
|
|
336
365
|
}
|
|
337
366
|
}
|
|
338
367
|
|
|
339
|
-
// Alias used by `complete`
|
|
340
368
|
export const completeCommand = chatCommand;
|
package/src/commands/embed.js
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { parseArgs, authFromValues } from '../args.js';
|
|
1
|
+
import { parseArgs, authFromValues, numberOption, readStdinIfPiped } from '../args.js';
|
|
3
2
|
import { api } from '../api.js';
|
|
4
|
-
import {
|
|
3
|
+
import { outln, pricePerMillion, printResult, table } from '../output.js';
|
|
5
4
|
|
|
6
5
|
const HELP = `Usage: openrouter embed [text...] [options]
|
|
7
6
|
openrouter embed models
|
|
@@ -17,16 +16,13 @@ Options:
|
|
|
17
16
|
--provider <json> Provider routing as JSON
|
|
18
17
|
`;
|
|
19
18
|
|
|
20
|
-
async function readStdinIfPiped() {
|
|
21
|
-
if (stdin.isTTY) return null;
|
|
22
|
-
let data = '';
|
|
23
|
-
for await (const chunk of stdin) data += chunk;
|
|
24
|
-
return data.trim() || null;
|
|
25
|
-
}
|
|
26
|
-
|
|
27
19
|
export async function embedCommand(argv) {
|
|
28
20
|
if (argv[0] === 'models') {
|
|
29
21
|
const { values } = parseArgs(argv.slice(1), {});
|
|
22
|
+
if (values.help) {
|
|
23
|
+
process.stdout.write(HELP);
|
|
24
|
+
return 0;
|
|
25
|
+
}
|
|
30
26
|
const data = await api('GET', '/embeddings/models', {
|
|
31
27
|
auth: authFromValues(values),
|
|
32
28
|
requireAuth: false
|
|
@@ -38,11 +34,7 @@ export async function embedCommand(argv) {
|
|
|
38
34
|
{ label: 'context', value: (m) => m.context_length ?? '' },
|
|
39
35
|
{
|
|
40
36
|
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
|
-
}
|
|
37
|
+
value: (m) => pricePerMillion((m.pricing || {}).prompt)
|
|
46
38
|
},
|
|
47
39
|
{ label: 'name', value: (m) => m.name || '' }
|
|
48
40
|
]);
|
|
@@ -74,7 +66,7 @@ export async function embedCommand(argv) {
|
|
|
74
66
|
input: inputs.length === 1 ? inputs[0] : inputs
|
|
75
67
|
};
|
|
76
68
|
if (values['input-type']) body.input_type = values['input-type'];
|
|
77
|
-
if (values.dimensions) body.dimensions =
|
|
69
|
+
if (values.dimensions) body.dimensions = numberOption(values.dimensions, '--dimensions');
|
|
78
70
|
if (values.encoding) body.encoding_format = values.encoding;
|
|
79
71
|
if (values.provider) body.provider = JSON.parse(values.provider);
|
|
80
72
|
|
|
@@ -1,13 +1,13 @@
|
|
|
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
|
|
|
7
7
|
Manage guardrails (requires a management key).
|
|
8
8
|
|
|
9
9
|
Subcommands:
|
|
10
|
-
list
|
|
10
|
+
list [--offset <n>] [--limit <n>] List guardrails
|
|
11
11
|
get <id> Get one
|
|
12
12
|
create <name> [options] Create
|
|
13
13
|
update <id> [options] Update
|
|
@@ -21,6 +21,7 @@ Subcommands:
|
|
|
21
21
|
member-assignments <id> List member assignments for a guardrail
|
|
22
22
|
|
|
23
23
|
create / update options:
|
|
24
|
+
--name <name> (update only)
|
|
24
25
|
--description <text>
|
|
25
26
|
--limit-usd <n>
|
|
26
27
|
--reset-interval <p> daily | weekly | monthly | null
|
|
@@ -41,7 +42,7 @@ function csv(v) {
|
|
|
41
42
|
function buildBody(values) {
|
|
42
43
|
const body = {};
|
|
43
44
|
if (values.description !== undefined) body.description = values.description;
|
|
44
|
-
if (values['limit-usd'] !== undefined) body.limit_usd =
|
|
45
|
+
if (values['limit-usd'] !== undefined) body.limit_usd = numberOption(values['limit-usd'], '--limit-usd');
|
|
45
46
|
if (values['reset-interval'] !== undefined) {
|
|
46
47
|
body.reset_interval = values['reset-interval'] === 'null' ? null : values['reset-interval'];
|
|
47
48
|
}
|
|
@@ -74,18 +75,16 @@ export async function guardrailsCommand(argv) {
|
|
|
74
75
|
return sub ? 0 : 1;
|
|
75
76
|
}
|
|
76
77
|
|
|
77
|
-
|
|
78
|
+
if (rest.includes('-h') || rest.includes('--help')) {
|
|
79
|
+
process.stdout.write(HELP);
|
|
80
|
+
return 0;
|
|
81
|
+
}
|
|
78
82
|
|
|
79
83
|
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;
|
|
84
|
+
const { values } = parseArgs(rest, PAGINATION_OPTIONS);
|
|
85
|
+
const query = paginationQuery(values);
|
|
87
86
|
const data = await api('GET', '/guardrails', {
|
|
88
|
-
auth:
|
|
87
|
+
auth: authFromValues(values),
|
|
89
88
|
requiresManagement: true,
|
|
90
89
|
query
|
|
91
90
|
});
|
|
@@ -106,10 +105,10 @@ export async function guardrailsCommand(argv) {
|
|
|
106
105
|
const { values, positionals } = parseArgs(rest, {});
|
|
107
106
|
if (!positionals[0]) throw new Error('id required');
|
|
108
107
|
const data = await api('GET', `/guardrails/${encodeURIComponent(positionals[0])}`, {
|
|
109
|
-
auth:
|
|
108
|
+
auth: authFromValues(values),
|
|
110
109
|
requiresManagement: true
|
|
111
110
|
});
|
|
112
|
-
printResult(data
|
|
111
|
+
printResult(data);
|
|
113
112
|
return 0;
|
|
114
113
|
}
|
|
115
114
|
|
|
@@ -123,11 +122,11 @@ export async function guardrailsCommand(argv) {
|
|
|
123
122
|
const body = { name, ...buildBody(values) };
|
|
124
123
|
if (values.workspace) body.workspace_id = values.workspace;
|
|
125
124
|
const data = await api('POST', '/guardrails', {
|
|
126
|
-
auth:
|
|
125
|
+
auth: authFromValues(values),
|
|
127
126
|
requiresManagement: true,
|
|
128
127
|
body
|
|
129
128
|
});
|
|
130
|
-
printResult(data
|
|
129
|
+
printResult(data);
|
|
131
130
|
return 0;
|
|
132
131
|
}
|
|
133
132
|
|
|
@@ -140,11 +139,11 @@ export async function guardrailsCommand(argv) {
|
|
|
140
139
|
const body = buildBody(values);
|
|
141
140
|
if (values.name) body.name = values.name;
|
|
142
141
|
const data = await api('PATCH', `/guardrails/${encodeURIComponent(positionals[0])}`, {
|
|
143
|
-
auth:
|
|
142
|
+
auth: authFromValues(values),
|
|
144
143
|
requiresManagement: true,
|
|
145
144
|
body
|
|
146
145
|
});
|
|
147
|
-
printResult(data
|
|
146
|
+
printResult(data);
|
|
148
147
|
return 0;
|
|
149
148
|
}
|
|
150
149
|
|
|
@@ -152,7 +151,7 @@ export async function guardrailsCommand(argv) {
|
|
|
152
151
|
const { values, positionals } = parseArgs(rest, {});
|
|
153
152
|
if (!positionals[0]) throw new Error('id required');
|
|
154
153
|
const data = await api('DELETE', `/guardrails/${encodeURIComponent(positionals[0])}`, {
|
|
155
|
-
auth:
|
|
154
|
+
auth: authFromValues(values),
|
|
156
155
|
requiresManagement: true
|
|
157
156
|
});
|
|
158
157
|
printResult(data, () => outln('deleted'));
|
|
@@ -164,12 +163,17 @@ export async function guardrailsCommand(argv) {
|
|
|
164
163
|
keys: { type: 'boolean' },
|
|
165
164
|
members: { type: 'boolean' }
|
|
166
165
|
});
|
|
167
|
-
|
|
166
|
+
if (values.keys && values.members) {
|
|
167
|
+
throw new Error('Pass only one of --keys or --members.');
|
|
168
|
+
}
|
|
169
|
+
const path = values.members
|
|
170
|
+
? '/guardrails/assignments/members'
|
|
171
|
+
: '/guardrails/assignments/keys';
|
|
168
172
|
const data = await api('GET', path, {
|
|
169
|
-
auth:
|
|
173
|
+
auth: authFromValues(values),
|
|
170
174
|
requiresManagement: true
|
|
171
175
|
});
|
|
172
|
-
printResult(data
|
|
176
|
+
printResult(data);
|
|
173
177
|
return 0;
|
|
174
178
|
}
|
|
175
179
|
|
|
@@ -181,10 +185,10 @@ export async function guardrailsCommand(argv) {
|
|
|
181
185
|
? `/guardrails/${encodeURIComponent(positionals[0])}/assignments/keys`
|
|
182
186
|
: `/guardrails/${encodeURIComponent(positionals[0])}/assignments/members`;
|
|
183
187
|
const data = await api('GET', path, {
|
|
184
|
-
auth:
|
|
188
|
+
auth: authFromValues(values),
|
|
185
189
|
requiresManagement: true
|
|
186
190
|
});
|
|
187
|
-
printResult(data
|
|
191
|
+
printResult(data);
|
|
188
192
|
return 0;
|
|
189
193
|
}
|
|
190
194
|
|
|
@@ -197,11 +201,11 @@ export async function guardrailsCommand(argv) {
|
|
|
197
201
|
? `/guardrails/${encodeURIComponent(id)}/assignments/keys`
|
|
198
202
|
: `/guardrails/${encodeURIComponent(id)}/assignments/keys/remove`;
|
|
199
203
|
const data = await api('POST', path, {
|
|
200
|
-
auth:
|
|
204
|
+
auth: authFromValues(values),
|
|
201
205
|
requiresManagement: true,
|
|
202
206
|
body: { key_hashes: hashes }
|
|
203
207
|
});
|
|
204
|
-
printResult(data
|
|
208
|
+
printResult(data);
|
|
205
209
|
return 0;
|
|
206
210
|
}
|
|
207
211
|
|
|
@@ -214,11 +218,11 @@ export async function guardrailsCommand(argv) {
|
|
|
214
218
|
? `/guardrails/${encodeURIComponent(id)}/assignments/members`
|
|
215
219
|
: `/guardrails/${encodeURIComponent(id)}/assignments/members/remove`;
|
|
216
220
|
const data = await api('POST', path, {
|
|
217
|
-
auth:
|
|
221
|
+
auth: authFromValues(values),
|
|
218
222
|
requiresManagement: true,
|
|
219
223
|
body: { user_ids: userIds }
|
|
220
224
|
});
|
|
221
|
-
printResult(data
|
|
225
|
+
printResult(data);
|
|
222
226
|
return 0;
|
|
223
227
|
}
|
|
224
228
|
|
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
|
|
|
@@ -7,7 +7,11 @@ const HELP = `Usage: openrouter keys <subcommand> [options]
|
|
|
7
7
|
Manage API keys (requires a provisioning/management key).
|
|
8
8
|
|
|
9
9
|
Subcommands:
|
|
10
|
-
list
|
|
10
|
+
list [options] List API keys
|
|
11
|
+
|
|
12
|
+
list options:
|
|
13
|
+
--offset <n> Pagination offset
|
|
14
|
+
--include-disabled Include disabled keys
|
|
11
15
|
get <hash> Get a single key
|
|
12
16
|
create <name> [options] Create a key
|
|
13
17
|
update <hash> [options] Update a key
|
|
@@ -36,6 +40,11 @@ export async function keysCommand(argv) {
|
|
|
36
40
|
return sub ? 0 : 1;
|
|
37
41
|
}
|
|
38
42
|
|
|
43
|
+
if (rest.includes('-h') || rest.includes('--help')) {
|
|
44
|
+
process.stdout.write(HELP);
|
|
45
|
+
return 0;
|
|
46
|
+
}
|
|
47
|
+
|
|
39
48
|
if (sub === 'list') {
|
|
40
49
|
const { values } = parseArgs(rest, {
|
|
41
50
|
offset: { type: 'string' },
|
|
@@ -69,7 +78,7 @@ export async function keysCommand(argv) {
|
|
|
69
78
|
auth: authFromValues(values),
|
|
70
79
|
requiresManagement: true
|
|
71
80
|
});
|
|
72
|
-
printResult(data
|
|
81
|
+
printResult(data);
|
|
73
82
|
return 0;
|
|
74
83
|
}
|
|
75
84
|
|
|
@@ -84,7 +93,7 @@ export async function keysCommand(argv) {
|
|
|
84
93
|
const name = positionals[0];
|
|
85
94
|
if (!name) throw new Error('name required');
|
|
86
95
|
const body = { name };
|
|
87
|
-
if (values.limit) body.limit =
|
|
96
|
+
if (values.limit) body.limit = numberOption(values.limit, '--limit');
|
|
88
97
|
if (values['limit-reset']) body.limit_reset = values['limit-reset'];
|
|
89
98
|
if (values['expires-at']) body.expires_at = values['expires-at'];
|
|
90
99
|
if (values.workspace) body.workspace_id = values.workspace;
|
|
@@ -97,7 +106,7 @@ export async function keysCommand(argv) {
|
|
|
97
106
|
printResult(data, () => {
|
|
98
107
|
const k = data.key || (data.data && data.data.key);
|
|
99
108
|
const meta = data.data || data;
|
|
100
|
-
outln(c.bold('key (save this
|
|
109
|
+
outln(c.bold('key (save this - shown only once):'));
|
|
101
110
|
outln(' ' + (k || '(see JSON)'));
|
|
102
111
|
outln(c.dim(JSON.stringify(meta, null, 2)));
|
|
103
112
|
});
|
|
@@ -117,7 +126,7 @@ export async function keysCommand(argv) {
|
|
|
117
126
|
if (!positionals[0]) throw new Error('hash required');
|
|
118
127
|
const body = {};
|
|
119
128
|
if (values.name) body.name = values.name;
|
|
120
|
-
if (values.limit) body.limit =
|
|
129
|
+
if (values.limit) body.limit = numberOption(values.limit, '--limit');
|
|
121
130
|
if (values['limit-reset']) body.limit_reset = values['limit-reset'];
|
|
122
131
|
if (values['include-byok']) body.include_byok_in_limit = true;
|
|
123
132
|
if (values['no-include-byok']) body.include_byok_in_limit = false;
|
|
@@ -128,7 +137,7 @@ export async function keysCommand(argv) {
|
|
|
128
137
|
requiresManagement: true,
|
|
129
138
|
body
|
|
130
139
|
});
|
|
131
|
-
printResult(data
|
|
140
|
+
printResult(data);
|
|
132
141
|
return 0;
|
|
133
142
|
}
|
|
134
143
|
|