@aaronjmars/openroutercli 0.3.0 → 0.5.0
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 +30 -9
- package/SKILL.md +86 -25
- package/package.json +1 -1
- package/src/api.js +12 -1
- package/src/args.js +48 -0
- package/src/cli.js +24 -8
- package/src/commands/account.js +48 -4
- package/src/commands/batches.js +360 -0
- package/src/commands/byok.js +114 -40
- package/src/commands/chat.js +277 -43
- package/src/commands/data.js +3 -3
- package/src/commands/decisions.js +46 -24
- package/src/commands/embed.js +35 -10
- package/src/commands/files.js +24 -8
- package/src/commands/generation.js +78 -9
- package/src/commands/guardrails.js +141 -11
- package/src/commands/image.js +43 -12
- package/src/commands/keys.js +59 -11
- package/src/commands/messages.js +217 -61
- package/src/commands/misc.js +76 -11
- package/src/commands/models.js +99 -16
- package/src/commands/observability.js +267 -0
- package/src/commands/presets.js +62 -3
- package/src/commands/rerank.js +28 -6
- package/src/commands/speech.js +80 -15
- package/src/commands/tools.js +102 -0
- package/src/commands/transcribe.js +113 -13
- package/src/commands/video.js +112 -14
- package/src/commands/workspaces.js +201 -6
- package/src/media.js +44 -0
package/src/commands/misc.js
CHANGED
|
@@ -5,8 +5,63 @@ import {
|
|
|
5
5
|
PAGINATION_OPTIONS,
|
|
6
6
|
paginationQuery,
|
|
7
7
|
} from "../args.js";
|
|
8
|
-
import { api } from "../api.js";
|
|
9
|
-
import { printResult, table } from "../output.js";
|
|
8
|
+
import { api, APIError } from "../api.js";
|
|
9
|
+
import { outln, printResult, table } from "../output.js";
|
|
10
|
+
|
|
11
|
+
// Org-only endpoints answer a personal account with an error (404 or a
|
|
12
|
+
// message naming organization accounts). Add a plain hint to it.
|
|
13
|
+
async function orgOnly(promise) {
|
|
14
|
+
try {
|
|
15
|
+
return await promise;
|
|
16
|
+
} catch (err) {
|
|
17
|
+
if (
|
|
18
|
+
err instanceof APIError &&
|
|
19
|
+
(err.status === 404 || /organi[sz]ation/i.test(err.message || ""))
|
|
20
|
+
) {
|
|
21
|
+
err.message +=
|
|
22
|
+
"\nHint: your management key belongs to a personal account. Switch to an" +
|
|
23
|
+
" organization in the OpenRouter dashboard and create the management key there.";
|
|
24
|
+
}
|
|
25
|
+
throw err;
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
async function orgSettingsCommand(argv) {
|
|
30
|
+
const { values } = parseArgs(argv, {
|
|
31
|
+
"filtered-catalog": { type: "string" },
|
|
32
|
+
});
|
|
33
|
+
if (values.help) {
|
|
34
|
+
process.stdout.write(
|
|
35
|
+
"Usage: openrouter org settings [--filtered-catalog true|false]\n\n" +
|
|
36
|
+
"Show organization settings, or change them. Organization accounts only;\n" +
|
|
37
|
+
"requires a management key.\n\n" +
|
|
38
|
+
"Options:\n" +
|
|
39
|
+
" --filtered-catalog <bool> When true, GET /models with an org API key returns\n" +
|
|
40
|
+
" only the models that key can use (the models user\n" +
|
|
41
|
+
" catalog), and the dashboard shows the same list.\n",
|
|
42
|
+
);
|
|
43
|
+
return 0;
|
|
44
|
+
}
|
|
45
|
+
const opts = { auth: authFromValues(values), requiresManagement: true };
|
|
46
|
+
let data;
|
|
47
|
+
if (values["filtered-catalog"] !== undefined) {
|
|
48
|
+
const v = values["filtered-catalog"].toLowerCase();
|
|
49
|
+
if (v !== "true" && v !== "false") throw new Error("--filtered-catalog must be true or false");
|
|
50
|
+
data = await orgOnly(
|
|
51
|
+
api("PATCH", "/organization/settings", {
|
|
52
|
+
...opts,
|
|
53
|
+
body: { is_filtered_model_catalog_enabled: v === "true" },
|
|
54
|
+
}),
|
|
55
|
+
);
|
|
56
|
+
} else {
|
|
57
|
+
data = await orgOnly(api("GET", "/organization/settings", opts));
|
|
58
|
+
}
|
|
59
|
+
printResult(data, () => {
|
|
60
|
+
const d = data.data || data;
|
|
61
|
+
for (const [k, v] of Object.entries(d)) outln(`${k}: ${v}`);
|
|
62
|
+
});
|
|
63
|
+
return 0;
|
|
64
|
+
}
|
|
10
65
|
|
|
11
66
|
async function orgMembersCommand(argv) {
|
|
12
67
|
const { values } = parseArgs(argv, PAGINATION_OPTIONS);
|
|
@@ -17,11 +72,13 @@ async function orgMembersCommand(argv) {
|
|
|
17
72
|
return 0;
|
|
18
73
|
}
|
|
19
74
|
const query = paginationQuery(values);
|
|
20
|
-
const data = await
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
75
|
+
const data = await orgOnly(
|
|
76
|
+
api("GET", "/organization/members", {
|
|
77
|
+
auth: authFromValues(values),
|
|
78
|
+
requiresManagement: true,
|
|
79
|
+
query,
|
|
80
|
+
}),
|
|
81
|
+
);
|
|
25
82
|
printResult(data, () => {
|
|
26
83
|
const rows = data.data || [];
|
|
27
84
|
table(rows, [
|
|
@@ -38,9 +95,14 @@ export async function orgCommand(argv) {
|
|
|
38
95
|
const sub = argv[0];
|
|
39
96
|
const rest = argv.slice(1);
|
|
40
97
|
if (sub === "members") return orgMembersCommand(rest);
|
|
98
|
+
if (sub === "settings") return orgSettingsCommand(rest);
|
|
41
99
|
if (!sub || sub === "-h" || sub === "--help" || sub === "help") {
|
|
42
100
|
process.stdout.write(
|
|
43
|
-
"Usage: openrouter org <subcommand>\n\
|
|
101
|
+
"Usage: openrouter org <subcommand>\n\n" +
|
|
102
|
+
"Organization accounts only; requires a management key.\n\n" +
|
|
103
|
+
"Subcommands:\n" +
|
|
104
|
+
" members [--offset N] [--limit N] List organization members\n" +
|
|
105
|
+
" settings [--filtered-catalog true|false] Show or change organization settings\n",
|
|
44
106
|
);
|
|
45
107
|
return sub ? 0 : 1;
|
|
46
108
|
}
|
|
@@ -71,6 +133,7 @@ export async function authCodeCommand(argv) {
|
|
|
71
133
|
"key-label": { type: "string" },
|
|
72
134
|
limit: { type: "string" },
|
|
73
135
|
"usage-limit-type": { type: "string" },
|
|
136
|
+
workspace: { type: "string" },
|
|
74
137
|
});
|
|
75
138
|
if (values.help || !values["callback-url"]) {
|
|
76
139
|
process.stdout.write(
|
|
@@ -78,13 +141,14 @@ export async function authCodeCommand(argv) {
|
|
|
78
141
|
"Mint a PKCE authorization code so a user can claim a key for your app.\n" +
|
|
79
142
|
"Requires a management key.\n\n" +
|
|
80
143
|
"Options:\n" +
|
|
81
|
-
" --callback-url <url>
|
|
144
|
+
" --callback-url <url> https URL, or localhost/127.0.0.1 on any port (required)\n" +
|
|
82
145
|
" --code-challenge <s> PKCE challenge\n" +
|
|
83
146
|
" --code-challenge-method <m> S256 | plain\n" +
|
|
84
|
-
" --expires-at <iso>
|
|
147
|
+
" --expires-at <iso> ISO 8601 UTC expiry with seconds (2027-12-31T23:59:59Z)\n" +
|
|
85
148
|
" --key-label <text> Custom label for the resulting key\n" +
|
|
86
149
|
" --limit <usd> Credit limit\n" +
|
|
87
|
-
" --usage-limit-type <p> daily | weekly | monthly\n"
|
|
150
|
+
" --usage-limit-type <p> daily | weekly | monthly\n" +
|
|
151
|
+
" --workspace <uuid> Workspace the resulting key belongs to\n",
|
|
88
152
|
);
|
|
89
153
|
return values.help ? 0 : 1;
|
|
90
154
|
}
|
|
@@ -95,6 +159,7 @@ export async function authCodeCommand(argv) {
|
|
|
95
159
|
if (values["key-label"]) body.key_label = values["key-label"];
|
|
96
160
|
if (values.limit) body.limit = numberOption(values.limit, "--limit");
|
|
97
161
|
if (values["usage-limit-type"]) body.usage_limit_type = values["usage-limit-type"];
|
|
162
|
+
if (values.workspace) body.workspace_id = values.workspace;
|
|
98
163
|
const data = await api("POST", "/auth/keys/code", {
|
|
99
164
|
auth: authFromValues(values),
|
|
100
165
|
requiresManagement: true,
|
package/src/commands/models.js
CHANGED
|
@@ -1,5 +1,11 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
1
|
+
import {
|
|
2
|
+
parseArgs,
|
|
3
|
+
authFromValues,
|
|
4
|
+
numberOption,
|
|
5
|
+
PAGINATION_OPTIONS,
|
|
6
|
+
paginationQuery,
|
|
7
|
+
} from "../args.js";
|
|
8
|
+
import { api, APIError } from "../api.js";
|
|
3
9
|
import { c, isJsonMode, outln, pricePerMillion, printResult, table } from "../output.js";
|
|
4
10
|
|
|
5
11
|
const ENDPOINT_SORT_FIELDS = ["throughput", "latency", "prompt", "completion", "uptime", "context"];
|
|
@@ -21,8 +27,10 @@ Subcommands:
|
|
|
21
27
|
architecture, supported params, top provider).
|
|
22
28
|
Aliases: info, detail
|
|
23
29
|
endpoints <author>/<slug> List endpoints (provider variants) for a model
|
|
24
|
-
parameters <author>/<slug> Supported
|
|
25
|
-
|
|
30
|
+
parameters <author>/<slug> Supported request parameters for a model
|
|
31
|
+
(undocumented endpoint, may change)
|
|
32
|
+
user Models filtered by your provider preferences,
|
|
33
|
+
privacy settings and guardrails (API key)
|
|
26
34
|
count Get the total model count
|
|
27
35
|
|
|
28
36
|
Options for list:
|
|
@@ -42,7 +50,7 @@ Options for list:
|
|
|
42
50
|
--providers <csv> Hosting provider names
|
|
43
51
|
--distillable <true|false> Filter by distillation capability
|
|
44
52
|
--zdr Only models with ZDR endpoints
|
|
45
|
-
--region <region>
|
|
53
|
+
--region <region> Only models with endpoints in a data region: eu | us
|
|
46
54
|
--order <sort> Server-side sort: most-popular|newest|top-weekly|
|
|
47
55
|
pricing-low-to-high|pricing-high-to-low|
|
|
48
56
|
context-high-to-low|throughput-high-to-low|
|
|
@@ -229,6 +237,26 @@ function renderModelDetail(m) {
|
|
|
229
237
|
outln(c.dim(`per_request_limits: ${JSON.stringify(m.per_request_limits)}`));
|
|
230
238
|
}
|
|
231
239
|
|
|
240
|
+
// GET /model/{author}/{slug}: one model by id, no key needed. Returns null
|
|
241
|
+
// when the id is unknown so the caller can fall back to a fuzzy match.
|
|
242
|
+
async function fetchModel(id, auth) {
|
|
243
|
+
const slash = id.indexOf("/");
|
|
244
|
+
const author = encodeURIComponent(id.slice(0, slash));
|
|
245
|
+
const slug = encodeURIComponent(id.slice(slash + 1)).replace(/%3A/gi, ":");
|
|
246
|
+
try {
|
|
247
|
+
const res = await api("GET", `/model/${author}/${slug}`, { auth, requireAuth: false });
|
|
248
|
+
return (res && res.data) || null;
|
|
249
|
+
} catch (err) {
|
|
250
|
+
if (err instanceof APIError && (err.status === 404 || err.status === 400)) return null;
|
|
251
|
+
throw err;
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
function nativeTools(e) {
|
|
256
|
+
const keys = Object.keys(e.native_tools || {});
|
|
257
|
+
return keys.map((k) => k.replace(/^openrouter:/, "")).join(",");
|
|
258
|
+
}
|
|
259
|
+
|
|
232
260
|
export async function modelsCommand(argv) {
|
|
233
261
|
// A leading non-flag token is the subcommand; otherwise default to list
|
|
234
262
|
// and keep argv intact so the list branch still sees its flags.
|
|
@@ -239,13 +267,30 @@ export async function modelsCommand(argv) {
|
|
|
239
267
|
const { values, positionals } = parseArgs(rest, {});
|
|
240
268
|
if (values.help || positionals.length === 0) {
|
|
241
269
|
process.stdout.write(
|
|
242
|
-
"Usage: openrouter models show <model-id>\n\
|
|
270
|
+
"Usage: openrouter models show <model-id>\n\n" +
|
|
271
|
+
"Show full details (pricing, architecture, capabilities) for one model.\n" +
|
|
272
|
+
"An <author>/<slug> id (variants like :free too) is looked up directly, which\n" +
|
|
273
|
+
"also finds image/video models the default list hides. Anything else is\n" +
|
|
274
|
+
"matched against model ids and names. No API key needed.\n",
|
|
243
275
|
);
|
|
244
276
|
return values.help ? 0 : 1;
|
|
245
277
|
}
|
|
278
|
+
// An <author>/<slug> id resolves directly (aliases, :variants, and image
|
|
279
|
+
// or video models the default list hides). A miss falls back to the
|
|
280
|
+
// fuzzy match over the full list below.
|
|
281
|
+
const direct = positionals[0].includes("/")
|
|
282
|
+
? await fetchModel(positionals[0], authFromValues(values))
|
|
283
|
+
: null;
|
|
284
|
+
if (direct) {
|
|
285
|
+
if (isJsonMode()) printResult(direct);
|
|
286
|
+
else renderModelDetail(direct);
|
|
287
|
+
return 0;
|
|
288
|
+
}
|
|
246
289
|
const target = positionals[0].toLowerCase();
|
|
290
|
+
// output_modalities=all so image/video/audio models are matchable too.
|
|
247
291
|
const data = await api("GET", "/models", {
|
|
248
292
|
auth: authFromValues(values),
|
|
293
|
+
query: { output_modalities: "all" },
|
|
249
294
|
requireAuth: false,
|
|
250
295
|
});
|
|
251
296
|
const rows = data.data || [];
|
|
@@ -367,24 +412,56 @@ export async function modelsCommand(argv) {
|
|
|
367
412
|
e.uptime_last_30m != null ? Number(e.uptime_last_30m).toFixed(1) + "%" : "",
|
|
368
413
|
},
|
|
369
414
|
{ label: "quant", value: (e) => e.quantization || "" },
|
|
415
|
+
{ label: "native tools", value: nativeTools },
|
|
370
416
|
]);
|
|
371
417
|
});
|
|
372
418
|
return 0;
|
|
373
419
|
}
|
|
374
420
|
|
|
375
421
|
if (sub === "user") {
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
422
|
+
let parsed;
|
|
423
|
+
try {
|
|
424
|
+
parsed = parseArgs(rest, {
|
|
425
|
+
...PAGINATION_OPTIONS,
|
|
426
|
+
"output-modalities": { type: "string" },
|
|
427
|
+
order: { type: "string" },
|
|
428
|
+
sort: { type: "string" },
|
|
429
|
+
});
|
|
430
|
+
} catch (err) {
|
|
431
|
+
// `models user` used to forward every list filter; the endpoint only
|
|
432
|
+
// takes these, so say so instead of a bare "Unknown option".
|
|
433
|
+
if (err.code === "ERR_PARSE_ARGS_UNKNOWN_OPTION") {
|
|
434
|
+
throw new Error(
|
|
435
|
+
`${err.message.split(".")[0]}. models user only supports --output-modalities, --order/--sort, --limit and --offset; use \`openrouter models list\` for search filters.`,
|
|
436
|
+
);
|
|
437
|
+
}
|
|
438
|
+
throw err;
|
|
439
|
+
}
|
|
440
|
+
const { values } = parsed;
|
|
380
441
|
if (values.help) {
|
|
381
442
|
process.stdout.write(
|
|
382
|
-
"Usage: openrouter models user [
|
|
443
|
+
"Usage: openrouter models user [options]\n\n" +
|
|
444
|
+
"List models filtered by your provider preferences, privacy settings, and\n" +
|
|
445
|
+
"guardrails. Uses your API key; on a regional host (eu/us) only models that\n" +
|
|
446
|
+
"can route in-region are returned.\n\n" +
|
|
447
|
+
"Options:\n" +
|
|
448
|
+
" --output-modalities <csv> text,image,embeddings,audio,video,rerank,decisions,\n" +
|
|
449
|
+
' speech,transcription (or "all"). Default: text\n' +
|
|
450
|
+
" --order <sort> Server-side sort: most-popular|newest|top-weekly|\n" +
|
|
451
|
+
" pricing-low-to-high|pricing-high-to-low|\n" +
|
|
452
|
+
" context-high-to-low|throughput-high-to-low|\n" +
|
|
453
|
+
" latency-low-to-high|intelligence-high-to-low|\n" +
|
|
454
|
+
" coding-high-to-low|agentic-high-to-low|\n" +
|
|
455
|
+
" design-arena-elo-high-to-low\n" +
|
|
456
|
+
" --sort <sort> Alias for --order\n" +
|
|
457
|
+
" --limit <n> Max rows (1-1000). Omit limit and offset for all\n" +
|
|
458
|
+
" --offset <n> Rows to skip\n",
|
|
383
459
|
);
|
|
384
460
|
return 0;
|
|
385
461
|
}
|
|
386
|
-
const query =
|
|
387
|
-
if (values
|
|
462
|
+
const query = paginationQuery(values);
|
|
463
|
+
if (values["output-modalities"]) query.output_modalities = values["output-modalities"];
|
|
464
|
+
if (values.order || values.sort) query.sort = values.order || values.sort;
|
|
388
465
|
const data = await api("GET", "/models/user", {
|
|
389
466
|
auth: authFromValues(values),
|
|
390
467
|
query,
|
|
@@ -400,8 +477,9 @@ export async function modelsCommand(argv) {
|
|
|
400
477
|
if (values.help || positionals.length === 0) {
|
|
401
478
|
process.stdout.write(
|
|
402
479
|
"Usage: openrouter models parameters <author>/<slug> [--provider <slug>]\n\n" +
|
|
403
|
-
"Show a model
|
|
404
|
-
"
|
|
480
|
+
"Show the request parameters a model supports. Optionally scope to one provider.\n" +
|
|
481
|
+
"Note: /parameters is not in the public API spec; it works today but may change.\n" +
|
|
482
|
+
"For the documented list see `openrouter models show <id>` (supported_parameters).\n",
|
|
405
483
|
);
|
|
406
484
|
return values.help ? 0 : 1;
|
|
407
485
|
}
|
|
@@ -416,7 +494,11 @@ export async function modelsCommand(argv) {
|
|
|
416
494
|
auth: authFromValues(values),
|
|
417
495
|
query,
|
|
418
496
|
});
|
|
419
|
-
printResult(data)
|
|
497
|
+
printResult(data, () => {
|
|
498
|
+
const d = data.data || data;
|
|
499
|
+
outln(d.model || target);
|
|
500
|
+
for (const p of d.supported_parameters || []) outln(` ${p}`);
|
|
501
|
+
});
|
|
420
502
|
return 0;
|
|
421
503
|
}
|
|
422
504
|
|
|
@@ -503,6 +585,7 @@ export async function modelsCommand(argv) {
|
|
|
503
585
|
}
|
|
504
586
|
|
|
505
587
|
const filtered = { ...data, data: rows };
|
|
588
|
+
if (values.filter || values.free) filtered.total_count = rows.length;
|
|
506
589
|
printResult(filtered, () => listFormatter(filtered));
|
|
507
590
|
return 0;
|
|
508
591
|
}
|
|
@@ -0,0 +1,267 @@
|
|
|
1
|
+
import {
|
|
2
|
+
parseArgs,
|
|
3
|
+
authFromValues,
|
|
4
|
+
PAGINATION_OPTIONS,
|
|
5
|
+
paginationQuery,
|
|
6
|
+
numberOption,
|
|
7
|
+
loadJson,
|
|
8
|
+
} from "../args.js";
|
|
9
|
+
import { api } from "../api.js";
|
|
10
|
+
import { c, outln, printResult, table } from "../output.js";
|
|
11
|
+
|
|
12
|
+
const HELP = `Usage: openrouter observability <subcommand> [options]
|
|
13
|
+
|
|
14
|
+
Manage observability destinations: where OpenRouter forwards traces of your
|
|
15
|
+
requests (requires a management key). Alias: obs
|
|
16
|
+
|
|
17
|
+
Subcommands:
|
|
18
|
+
list [options] List destinations
|
|
19
|
+
get <id> Get one
|
|
20
|
+
create <name> --type <t> --config <json|@file> [options]
|
|
21
|
+
update <id> [options] Update (only the fields you pass change)
|
|
22
|
+
delete <id> Delete
|
|
23
|
+
|
|
24
|
+
list options:
|
|
25
|
+
--offset <n>
|
|
26
|
+
--limit <n> 1-100
|
|
27
|
+
--workspace <id> Another workspace (default: the key's default)
|
|
28
|
+
|
|
29
|
+
create / update options:
|
|
30
|
+
--type <t> (create only) arize | braintrust | clickhouse |
|
|
31
|
+
datadog | grafana | langfuse | langsmith | newrelic |
|
|
32
|
+
opik | otel-collector | posthog | ramp | s3 | sentry |
|
|
33
|
+
snowflake | weave | webhook
|
|
34
|
+
--config <json|@file> Provider settings, e.g. langfuse:
|
|
35
|
+
{"publicKey":"pk-lf-...","secretKey":"sk-lf-...",
|
|
36
|
+
"baseUrl":"https://cloud.langfuse.com"}. Clickhouse and
|
|
37
|
+
Snowflake accept "shouldIncludeCacheWriteTokens": true.
|
|
38
|
+
On update, masked values are ignored and unset fields
|
|
39
|
+
keep their value.
|
|
40
|
+
--name <name> (update only)
|
|
41
|
+
--enabled / --disabled Turn forwarding on or off
|
|
42
|
+
--privacy-mode / --no-privacy-mode Send metadata only, no request/response bodies
|
|
43
|
+
--sampling-rate <n> 0.0001-1 (1 = 100%)
|
|
44
|
+
--filter-rules <json|@file> Structured rules for which events are forwarded
|
|
45
|
+
("null" clears them on update)
|
|
46
|
+
--regions <csv> global | europe (eu) | us (default: global)
|
|
47
|
+
--api-keys <csv> Only forward traffic from these API key hashes
|
|
48
|
+
("all" forwards every key on update)
|
|
49
|
+
--broadcast-cost / --no-broadcast-cost Cost and billing metadata
|
|
50
|
+
--broadcast-cost-allocation / --no-broadcast-cost-allocation
|
|
51
|
+
Cost allocation metadata (beta)
|
|
52
|
+
--broadcast-identity / --no-broadcast-identity Identity metadata
|
|
53
|
+
--broadcast-request-context / --no-broadcast-request-context
|
|
54
|
+
Request-context metadata
|
|
55
|
+
--workspace <id> (create only) Target workspace
|
|
56
|
+
|
|
57
|
+
Pretty output never shows config values (only their keys); use --json for the
|
|
58
|
+
full object (the API masks secrets there too).
|
|
59
|
+
`;
|
|
60
|
+
|
|
61
|
+
const BOOL_PAIRS = [
|
|
62
|
+
["privacy-mode", "no-privacy-mode", "privacy_mode"],
|
|
63
|
+
["broadcast-cost", "no-broadcast-cost", "broadcast_generation_cost"],
|
|
64
|
+
[
|
|
65
|
+
"broadcast-cost-allocation",
|
|
66
|
+
"no-broadcast-cost-allocation",
|
|
67
|
+
"broadcast_generation_cost_allocation",
|
|
68
|
+
],
|
|
69
|
+
["broadcast-identity", "no-broadcast-identity", "broadcast_generation_identity"],
|
|
70
|
+
[
|
|
71
|
+
"broadcast-request-context",
|
|
72
|
+
"no-broadcast-request-context",
|
|
73
|
+
"broadcast_generation_request_context",
|
|
74
|
+
],
|
|
75
|
+
];
|
|
76
|
+
|
|
77
|
+
const COMMON_FIELDS = {
|
|
78
|
+
config: { type: "string" },
|
|
79
|
+
enabled: { type: "boolean" },
|
|
80
|
+
disabled: { type: "boolean" },
|
|
81
|
+
"sampling-rate": { type: "string" },
|
|
82
|
+
"filter-rules": { type: "string" },
|
|
83
|
+
regions: { type: "string" },
|
|
84
|
+
"api-keys": { type: "string" },
|
|
85
|
+
};
|
|
86
|
+
for (const [on, off] of BOOL_PAIRS) {
|
|
87
|
+
COMMON_FIELDS[on] = { type: "boolean" };
|
|
88
|
+
COMMON_FIELDS[off] = { type: "boolean" };
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
function csv(v) {
|
|
92
|
+
return v
|
|
93
|
+
.split(",")
|
|
94
|
+
.map((s) => s.trim())
|
|
95
|
+
.filter(Boolean);
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
async function buildBody(values) {
|
|
99
|
+
const body = {};
|
|
100
|
+
if (values.config !== undefined) body.config = await loadJson(values.config, "--config");
|
|
101
|
+
if (values.enabled && values.disabled)
|
|
102
|
+
throw new Error("Pass only one of --enabled or --disabled.");
|
|
103
|
+
if (values.enabled) body.enabled = true;
|
|
104
|
+
if (values.disabled) body.enabled = false;
|
|
105
|
+
for (const [on, off, field] of BOOL_PAIRS) {
|
|
106
|
+
if (values[on] && values[off]) throw new Error(`Pass only one of --${on} or --${off}.`);
|
|
107
|
+
if (values[on]) body[field] = true;
|
|
108
|
+
if (values[off]) body[field] = false;
|
|
109
|
+
}
|
|
110
|
+
if (values["sampling-rate"] !== undefined) {
|
|
111
|
+
const rate = numberOption(values["sampling-rate"], "--sampling-rate");
|
|
112
|
+
if (rate < 0.0001 || rate > 1) throw new Error("--sampling-rate must be between 0.0001 and 1");
|
|
113
|
+
body.sampling_rate = rate;
|
|
114
|
+
}
|
|
115
|
+
if (values["filter-rules"] !== undefined) {
|
|
116
|
+
body.filter_rules =
|
|
117
|
+
values["filter-rules"] === "null"
|
|
118
|
+
? null
|
|
119
|
+
: await loadJson(values["filter-rules"], "--filter-rules");
|
|
120
|
+
}
|
|
121
|
+
if (values.regions !== undefined) body.regions = csv(values.regions);
|
|
122
|
+
if (values["api-keys"] !== undefined) {
|
|
123
|
+
body.api_key_hashes = values["api-keys"] === "all" ? null : csv(values["api-keys"]);
|
|
124
|
+
}
|
|
125
|
+
return body;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
function keysOf(obj) {
|
|
129
|
+
return obj && typeof obj === "object" ? Object.keys(obj).join(",") : "";
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
function printDestination(data) {
|
|
133
|
+
printResult(data, () => {
|
|
134
|
+
const d = data.data || data;
|
|
135
|
+
outln(`${c.bold(d.name || "(unnamed)")} ${d.type || ""} ${d.id || ""}`);
|
|
136
|
+
outln(` enabled: ${d.enabled ? "yes" : "no"}`);
|
|
137
|
+
if (d.workspace_id) outln(` workspace: ${d.workspace_id}`);
|
|
138
|
+
outln(` config keys: ${keysOf(d.config) || "-"}`);
|
|
139
|
+
if (d.sampling_rate != null) outln(` sampling_rate: ${d.sampling_rate}`);
|
|
140
|
+
outln(` privacy_mode: ${d.privacy_mode ? "yes" : "no"}`);
|
|
141
|
+
if (d.regions) outln(` regions: ${d.regions.join(",")}`);
|
|
142
|
+
outln(` api keys: ${Array.isArray(d.api_key_hashes) ? d.api_key_hashes.join(",") : "all"}`);
|
|
143
|
+
const broadcast = [
|
|
144
|
+
["cost", d.broadcast_generation_cost],
|
|
145
|
+
["cost_allocation", d.broadcast_generation_cost_allocation],
|
|
146
|
+
["identity", d.broadcast_generation_identity],
|
|
147
|
+
["request_context", d.broadcast_generation_request_context],
|
|
148
|
+
]
|
|
149
|
+
.filter(([, v]) => v)
|
|
150
|
+
.map(([k]) => k);
|
|
151
|
+
outln(` broadcast: ${broadcast.join(",") || "-"}`);
|
|
152
|
+
if (d.filter_rules) outln(` filter_rules: ${JSON.stringify(d.filter_rules)}`);
|
|
153
|
+
if (d.created_at) outln(` created: ${d.created_at}`);
|
|
154
|
+
if (d.updated_at) outln(` updated: ${d.updated_at}`);
|
|
155
|
+
});
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
export async function observabilityCommand(argv) {
|
|
159
|
+
const sub = argv[0];
|
|
160
|
+
const rest = argv.slice(1);
|
|
161
|
+
if (!sub || sub === "help" || sub === "-h" || sub === "--help") {
|
|
162
|
+
process.stdout.write(HELP);
|
|
163
|
+
return sub ? 0 : 1;
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
if (rest.includes("-h") || rest.includes("--help")) {
|
|
167
|
+
process.stdout.write(HELP);
|
|
168
|
+
return 0;
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
if (sub === "list") {
|
|
172
|
+
const { values } = parseArgs(rest, {
|
|
173
|
+
...PAGINATION_OPTIONS,
|
|
174
|
+
workspace: { type: "string" },
|
|
175
|
+
});
|
|
176
|
+
const query = paginationQuery(values);
|
|
177
|
+
if (values.workspace) query.workspace_id = values.workspace;
|
|
178
|
+
const data = await api("GET", "/observability/destinations", {
|
|
179
|
+
auth: authFromValues(values),
|
|
180
|
+
requiresManagement: true,
|
|
181
|
+
query,
|
|
182
|
+
});
|
|
183
|
+
printResult(data, () => {
|
|
184
|
+
const rows = data.data || [];
|
|
185
|
+
if (!rows.length) {
|
|
186
|
+
outln("no destinations");
|
|
187
|
+
return;
|
|
188
|
+
}
|
|
189
|
+
table(rows, [
|
|
190
|
+
{ label: "id", value: (d) => d.id },
|
|
191
|
+
{ label: "type", value: (d) => d.type },
|
|
192
|
+
{ label: "name", value: (d) => d.name || "" },
|
|
193
|
+
{ label: "enabled", value: (d) => (d.enabled ? "yes" : "no") },
|
|
194
|
+
{ label: "sampling", value: (d) => d.sampling_rate ?? "" },
|
|
195
|
+
{ label: "privacy", value: (d) => (d.privacy_mode ? "yes" : "") },
|
|
196
|
+
{ label: "regions", value: (d) => (d.regions || []).join(",") },
|
|
197
|
+
{ label: "config_keys", value: (d) => keysOf(d.config) },
|
|
198
|
+
]);
|
|
199
|
+
});
|
|
200
|
+
return 0;
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
if (sub === "get") {
|
|
204
|
+
const { values, positionals } = parseArgs(rest, {});
|
|
205
|
+
if (!positionals[0]) throw new Error("id required");
|
|
206
|
+
const data = await api(
|
|
207
|
+
"GET",
|
|
208
|
+
`/observability/destinations/${encodeURIComponent(positionals[0])}`,
|
|
209
|
+
{ auth: authFromValues(values), requiresManagement: true },
|
|
210
|
+
);
|
|
211
|
+
printDestination(data);
|
|
212
|
+
return 0;
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
if (sub === "create") {
|
|
216
|
+
const { values, positionals } = parseArgs(rest, {
|
|
217
|
+
...COMMON_FIELDS,
|
|
218
|
+
type: { type: "string" },
|
|
219
|
+
workspace: { type: "string" },
|
|
220
|
+
});
|
|
221
|
+
const name = positionals[0];
|
|
222
|
+
if (!name) throw new Error("name required");
|
|
223
|
+
if (!values.type) throw new Error("--type is required");
|
|
224
|
+
if (values.config === undefined) throw new Error("--config is required");
|
|
225
|
+
const body = { type: values.type, name, ...(await buildBody(values)) };
|
|
226
|
+
if (values.workspace) body.workspace_id = values.workspace;
|
|
227
|
+
const data = await api("POST", "/observability/destinations", {
|
|
228
|
+
auth: authFromValues(values),
|
|
229
|
+
requiresManagement: true,
|
|
230
|
+
body,
|
|
231
|
+
});
|
|
232
|
+
printDestination(data);
|
|
233
|
+
return 0;
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
if (sub === "update") {
|
|
237
|
+
const { values, positionals } = parseArgs(rest, {
|
|
238
|
+
...COMMON_FIELDS,
|
|
239
|
+
name: { type: "string" },
|
|
240
|
+
});
|
|
241
|
+
if (!positionals[0]) throw new Error("id required");
|
|
242
|
+
const body = await buildBody(values);
|
|
243
|
+
if (values.name !== undefined) body.name = values.name;
|
|
244
|
+
if (!Object.keys(body).length) throw new Error("Nothing to update: pass at least one field.");
|
|
245
|
+
const data = await api(
|
|
246
|
+
"PATCH",
|
|
247
|
+
`/observability/destinations/${encodeURIComponent(positionals[0])}`,
|
|
248
|
+
{ auth: authFromValues(values), requiresManagement: true, body },
|
|
249
|
+
);
|
|
250
|
+
printDestination(data);
|
|
251
|
+
return 0;
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
if (sub === "delete") {
|
|
255
|
+
const { values, positionals } = parseArgs(rest, {});
|
|
256
|
+
if (!positionals[0]) throw new Error("id required");
|
|
257
|
+
const data = await api(
|
|
258
|
+
"DELETE",
|
|
259
|
+
`/observability/destinations/${encodeURIComponent(positionals[0])}`,
|
|
260
|
+
{ auth: authFromValues(values), requiresManagement: true },
|
|
261
|
+
);
|
|
262
|
+
printResult(data, () => outln("deleted"));
|
|
263
|
+
return 0;
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
throw new Error(`Unknown observability subcommand: ${sub}`);
|
|
267
|
+
}
|
package/src/commands/presets.js
CHANGED
|
@@ -1,18 +1,50 @@
|
|
|
1
|
-
import { parseArgs, authFromValues } from "../args.js";
|
|
1
|
+
import { parseArgs, authFromValues, loadJson } from "../args.js";
|
|
2
2
|
import { api } from "../api.js";
|
|
3
|
-
import { printResult, table } from "../output.js";
|
|
3
|
+
import { outln, printResult, table } from "../output.js";
|
|
4
4
|
|
|
5
5
|
const HELP = `Usage: openrouter presets <subcommand> [options]
|
|
6
6
|
|
|
7
|
-
View server-saved config presets
|
|
7
|
+
View and create server-saved config presets.
|
|
8
8
|
|
|
9
9
|
Subcommands:
|
|
10
10
|
list List presets (default)
|
|
11
11
|
get <slug> Get a single preset
|
|
12
12
|
versions <slug> List versions for a preset
|
|
13
13
|
version <slug> <version> Get a specific preset version
|
|
14
|
+
create <slug> --body <json|@file> [--api chat|messages|responses]
|
|
15
|
+
Create a preset from an inference request body.
|
|
16
|
+
An existing slug gets a new version.
|
|
17
|
+
|
|
18
|
+
create options:
|
|
19
|
+
--body <json|@file> A normal request body for that API. Only preset fields
|
|
20
|
+
(model, provider, sampling params, ...) are kept;
|
|
21
|
+
messages, input and stream are ignored.
|
|
22
|
+
--api <a> chat (default) | messages | responses
|
|
23
|
+
|
|
24
|
+
Example:
|
|
25
|
+
openrouter presets create fast-cheap --body '{"model":"openai/gpt-5-nano","temperature":0.2,"provider":{"sort":"price"}}'
|
|
14
26
|
`;
|
|
15
27
|
|
|
28
|
+
const PRESET_APIS = {
|
|
29
|
+
chat: "chat/completions",
|
|
30
|
+
"chat-completions": "chat/completions",
|
|
31
|
+
messages: "messages",
|
|
32
|
+
responses: "responses",
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
// The inference schemas require messages even though preset creation ignores
|
|
36
|
+
// them, so a config-only body gets a placeholder.
|
|
37
|
+
export function buildPresetBody(apiName, body) {
|
|
38
|
+
if (!body || typeof body !== "object" || Array.isArray(body)) {
|
|
39
|
+
throw new Error("--body must be a JSON object");
|
|
40
|
+
}
|
|
41
|
+
const out = { ...body };
|
|
42
|
+
if (apiName !== "responses" && !out.messages) {
|
|
43
|
+
out.messages = [{ role: "user", content: "." }];
|
|
44
|
+
}
|
|
45
|
+
return out;
|
|
46
|
+
}
|
|
47
|
+
|
|
16
48
|
export async function presetsCommand(argv) {
|
|
17
49
|
const sub = argv[0] && !argv[0].startsWith("-") ? argv[0] : "list";
|
|
18
50
|
const rest = sub === argv[0] ? argv.slice(1) : argv;
|
|
@@ -37,6 +69,33 @@ export async function presetsCommand(argv) {
|
|
|
37
69
|
return 0;
|
|
38
70
|
}
|
|
39
71
|
|
|
72
|
+
if (sub === "create") {
|
|
73
|
+
const { values, positionals } = parseArgs(rest, {
|
|
74
|
+
body: { type: "string" },
|
|
75
|
+
api: { type: "string" },
|
|
76
|
+
});
|
|
77
|
+
const slug = positionals[0];
|
|
78
|
+
if (!slug) throw new Error("slug required");
|
|
79
|
+
if (!values.body) throw new Error("--body is required");
|
|
80
|
+
const apiName = values.api || "chat";
|
|
81
|
+
const route = PRESET_APIS[apiName];
|
|
82
|
+
if (!route) throw new Error(`Unknown --api: ${apiName} (use chat, messages or responses)`);
|
|
83
|
+
const body = buildPresetBody(apiName, await loadJson(values.body, "--body"));
|
|
84
|
+
const data = await api("POST", `/presets/${encodeURIComponent(slug)}/${route}`, {
|
|
85
|
+
auth: authFromValues(values),
|
|
86
|
+
body,
|
|
87
|
+
});
|
|
88
|
+
printResult(data, () => {
|
|
89
|
+
const p = data && (data.data || data);
|
|
90
|
+
const dv = (p && p.designated_version) || {};
|
|
91
|
+
const version = dv.version ?? dv.version_number ?? dv.id;
|
|
92
|
+
outln(
|
|
93
|
+
`saved preset ${(p && p.slug) || slug}${version != null ? ` (version ${version})` : ""}`,
|
|
94
|
+
);
|
|
95
|
+
});
|
|
96
|
+
return 0;
|
|
97
|
+
}
|
|
98
|
+
|
|
40
99
|
if (sub === "versions") {
|
|
41
100
|
const { values, positionals } = parseArgs(rest, {});
|
|
42
101
|
if (!positionals[0]) throw new Error("slug required");
|