@aaronjmars/openroutercli 0.2.2 → 0.4.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 +39 -7
- package/SKILL.md +93 -22
- package/package.json +1 -1
- package/src/api.js +24 -4
- package/src/args.js +50 -0
- package/src/cli.js +68 -11
- package/src/commands/account.js +48 -4
- package/src/commands/batches.js +358 -0
- package/src/commands/byok.js +114 -40
- package/src/commands/chat.js +277 -43
- package/src/commands/containers.js +75 -0
- package/src/commands/data.js +376 -0
- package/src/commands/decisions.js +96 -0
- package/src/commands/embed.js +35 -10
- package/src/commands/files.js +61 -10
- package/src/commands/generation.js +78 -9
- package/src/commands/guardrails.js +131 -11
- package/src/commands/image.js +43 -12
- package/src/commands/keys.js +59 -11
- package/src/commands/messages.js +231 -47
- package/src/commands/misc.js +76 -11
- package/src/commands/models.js +164 -26
- 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/config.js +3 -0
- package/src/media.js +44 -0
package/src/commands/account.js
CHANGED
|
@@ -40,6 +40,28 @@ function renderKey(label, data) {
|
|
|
40
40
|
` rate_limit: ${d.rate_limit.requests ?? "?"} req / ${d.rate_limit.interval ?? "?"}`,
|
|
41
41
|
);
|
|
42
42
|
}
|
|
43
|
+
if (d.allowed_data_regions) {
|
|
44
|
+
outln(` data_regions: ${d.allowed_data_regions.join(", ") || "-"}`);
|
|
45
|
+
}
|
|
46
|
+
if (d.effective_budget) {
|
|
47
|
+
outln(` effective_budget: ${fmtBudget(d.effective_budget)}`);
|
|
48
|
+
}
|
|
49
|
+
if (Array.isArray(d.budgets) && d.budgets.length) {
|
|
50
|
+
outln(
|
|
51
|
+
" budgets: " + c.dim("(checked in this order; any at $0 left blocks requests)"),
|
|
52
|
+
);
|
|
53
|
+
for (const b of d.budgets) outln(` ${fmtBudget(b)}`);
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
// One budget layer on a line: scope, spend/limit, remaining, window.
|
|
58
|
+
function fmtBudget(b) {
|
|
59
|
+
const money = (x) => (x == null ? "?" : `$${Number(x).toFixed(2)}`);
|
|
60
|
+
const window = b.reset_interval
|
|
61
|
+
? `${b.reset_interval}${b.resets_at ? `, resets ${b.resets_at}` : ""}`
|
|
62
|
+
: "lifetime";
|
|
63
|
+
const byok = b.include_byok ? ", incl. BYOK" : "";
|
|
64
|
+
return `${b.scope ?? "?"}: ${money(b.spend_usd)} of ${money(b.limit_usd)} used, ${money(b.remaining_usd)} left (${window}${byok})`;
|
|
43
65
|
}
|
|
44
66
|
|
|
45
67
|
export async function whoamiCommand(argv) {
|
|
@@ -113,17 +135,32 @@ export async function activityCommand(argv) {
|
|
|
113
135
|
date: { type: "string" },
|
|
114
136
|
"api-key-hash": { type: "string" },
|
|
115
137
|
"user-id": { type: "string" },
|
|
138
|
+
"group-by": { type: "string" },
|
|
139
|
+
workspace: { type: "string" },
|
|
116
140
|
});
|
|
117
141
|
if (values.help) {
|
|
118
142
|
process.stdout.write(
|
|
119
|
-
|
|
143
|
+
"Usage: openrouter activity [options]\n\n" +
|
|
144
|
+
"Get usage activity for the last 30 completed UTC days, grouped by endpoint.\n" +
|
|
145
|
+
"Requires a management key.\n\n" +
|
|
146
|
+
"Options:\n" +
|
|
147
|
+
" --date <YYYY-MM-DD> UTC date in last 30 days\n" +
|
|
148
|
+
" --api-key-hash <hash> Filter by API key SHA-256 hash\n" +
|
|
149
|
+
" --user-id <id> Filter by org member user ID\n" +
|
|
150
|
+
" --workspace <uuid> Only activity attributed to this workspace\n" +
|
|
151
|
+
" --group-by workspace Split each row per workspace (adds workspace_id)\n",
|
|
120
152
|
);
|
|
121
153
|
return 0;
|
|
122
154
|
}
|
|
155
|
+
if (values["group-by"] && values["group-by"] !== "workspace") {
|
|
156
|
+
throw new Error(`Unknown --group-by "${values["group-by"]}". Expected: workspace`);
|
|
157
|
+
}
|
|
123
158
|
const query = {};
|
|
124
159
|
if (values.date) query.date = values.date;
|
|
125
160
|
if (values["api-key-hash"]) query.api_key_hash = values["api-key-hash"];
|
|
126
161
|
if (values["user-id"]) query.user_id = values["user-id"];
|
|
162
|
+
if (values.workspace) query.workspace_id = values.workspace;
|
|
163
|
+
if (values["group-by"]) query.group_by = values["group-by"];
|
|
127
164
|
const data = await api("GET", "/activity", {
|
|
128
165
|
auth: authFromValues(values),
|
|
129
166
|
requiresManagement: true,
|
|
@@ -135,13 +172,20 @@ export async function activityCommand(argv) {
|
|
|
135
172
|
outln(c.dim("(no activity)"));
|
|
136
173
|
return;
|
|
137
174
|
}
|
|
138
|
-
|
|
175
|
+
const columns = [
|
|
139
176
|
{ label: "date", value: (r) => r.date || r.day || "" },
|
|
140
177
|
{ label: "model", value: (r) => r.model || r.model_permaslug || "" },
|
|
178
|
+
{ label: "provider", value: (r) => r.provider_name || "" },
|
|
141
179
|
{ label: "requests", value: (r) => r.requests ?? r.request_count ?? "" },
|
|
142
|
-
{ label: "
|
|
180
|
+
{ label: "prompt", value: (r) => r.prompt_tokens ?? "" },
|
|
181
|
+
{ label: "completion", value: (r) => r.completion_tokens ?? "" },
|
|
182
|
+
{ label: "cached", value: (r) => r.cached_tokens ?? "" },
|
|
143
183
|
{ label: "cost", value: (r) => r.usage ?? r.cost ?? "" },
|
|
144
|
-
]
|
|
184
|
+
];
|
|
185
|
+
if (rows.some((r) => r.workspace_id)) {
|
|
186
|
+
columns.splice(1, 0, { label: "workspace", value: (r) => r.workspace_id || "" });
|
|
187
|
+
}
|
|
188
|
+
table(rows, columns);
|
|
145
189
|
});
|
|
146
190
|
return 0;
|
|
147
191
|
}
|
|
@@ -0,0 +1,358 @@
|
|
|
1
|
+
import { writeFile } from "node:fs/promises";
|
|
2
|
+
import { parseArgs, authFromValues, numberOption, loadJson, readStdinIfPiped } from "../args.js";
|
|
3
|
+
import { api, APIError } from "../api.js";
|
|
4
|
+
import { c, info, isJsonMode, outln, printJSON, printResult, table } from "../output.js";
|
|
5
|
+
|
|
6
|
+
const HELP = `Usage: openrouter batches <subcommand> [options]
|
|
7
|
+
|
|
8
|
+
Run many requests asynchronously against one endpoint and model (Batch API).
|
|
9
|
+
|
|
10
|
+
Subcommands:
|
|
11
|
+
list [options] List batches (newest first)
|
|
12
|
+
create [options] Submit a batch
|
|
13
|
+
get <id> Status, request counts and usage
|
|
14
|
+
wait <id> [options] Poll until the batch reaches a final status
|
|
15
|
+
results <id> [-o out.jsonl] Write the per-request results as JSONL
|
|
16
|
+
delete <id> Delete a finished batch and its stored data
|
|
17
|
+
|
|
18
|
+
list options:
|
|
19
|
+
--limit <n> 1-100
|
|
20
|
+
--after <id> Next page: the previous page's last_id
|
|
21
|
+
--status <s> validating | in_progress | completed | failed |
|
|
22
|
+
expired | cancelled (repeat or comma-separate)
|
|
23
|
+
--created-after <t> Unix seconds, YYYY-MM-DD or ISO date-time
|
|
24
|
+
--created-before <t>
|
|
25
|
+
|
|
26
|
+
create options:
|
|
27
|
+
-m, --model <id> Model for every request (required)
|
|
28
|
+
--endpoint <e> chat | responses | messages | embeddings (default: chat)
|
|
29
|
+
--file <path> JSONL, one request per line: {"custom_id": "...", "body": {...}}
|
|
30
|
+
(a line without "body" is used as the body itself and
|
|
31
|
+
gets custom_id req-<n>). Reads stdin when omitted.
|
|
32
|
+
--completion-window <w> Default: 24h
|
|
33
|
+
--provider <json|@file> Routing: {"only": [...], "allow_fallbacks": false}
|
|
34
|
+
--only <csv> Shortcut for provider.only
|
|
35
|
+
--no-fallbacks Shortcut for provider.allow_fallbacks = false
|
|
36
|
+
|
|
37
|
+
wait options:
|
|
38
|
+
--interval <s> Seconds between polls (default: 10)
|
|
39
|
+
--timeout <s> Give up after this many seconds (default: 86400)
|
|
40
|
+
|
|
41
|
+
results options:
|
|
42
|
+
-o, --out <path> Output file (default: stdout)
|
|
43
|
+
|
|
44
|
+
Statuses: validating, in_progress, finalizing, completed, failed, expired,
|
|
45
|
+
cancelling, cancelled. Final ones: completed, failed, expired, cancelled.
|
|
46
|
+
There is no cancel endpoint. Results are kept for 30 days.
|
|
47
|
+
|
|
48
|
+
Examples:
|
|
49
|
+
openrouter batches create -m openai/gpt-5-nano --file reqs.jsonl
|
|
50
|
+
openrouter batches wait batch_abc && openrouter batches results batch_abc -o out.jsonl
|
|
51
|
+
`;
|
|
52
|
+
|
|
53
|
+
const ENDPOINTS = {
|
|
54
|
+
chat: "/v1/chat/completions",
|
|
55
|
+
"chat-completions": "/v1/chat/completions",
|
|
56
|
+
responses: "/v1/responses",
|
|
57
|
+
messages: "/v1/messages",
|
|
58
|
+
embeddings: "/v1/embeddings",
|
|
59
|
+
embed: "/v1/embeddings",
|
|
60
|
+
};
|
|
61
|
+
|
|
62
|
+
const TERMINAL_STATUSES = ["completed", "failed", "expired", "cancelled"];
|
|
63
|
+
const FAILED_STATUSES = ["failed", "expired", "cancelled"];
|
|
64
|
+
|
|
65
|
+
function sleep(ms) {
|
|
66
|
+
return new Promise((r) => setTimeout(r, ms));
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
function csv(values) {
|
|
70
|
+
return [values]
|
|
71
|
+
.flat()
|
|
72
|
+
.flatMap((v) => String(v).split(","))
|
|
73
|
+
.map((s) => s.trim())
|
|
74
|
+
.filter(Boolean);
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
function resolveEndpoint(value) {
|
|
78
|
+
const v = value || "chat";
|
|
79
|
+
if (v.startsWith("/")) return v;
|
|
80
|
+
const mapped = ENDPOINTS[v];
|
|
81
|
+
if (!mapped) {
|
|
82
|
+
throw new Error(`Unknown --endpoint: ${v} (use chat, responses, messages or embeddings)`);
|
|
83
|
+
}
|
|
84
|
+
return mapped;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
function parseRequests(text, source) {
|
|
88
|
+
const trimmed = text.trim();
|
|
89
|
+
if (!trimmed) throw new Error(`No requests found in ${source}`);
|
|
90
|
+
let items;
|
|
91
|
+
if (trimmed.startsWith("[")) {
|
|
92
|
+
try {
|
|
93
|
+
items = JSON.parse(trimmed);
|
|
94
|
+
} catch (err) {
|
|
95
|
+
throw new Error(`Invalid JSON in ${source}: ${err.message}`);
|
|
96
|
+
}
|
|
97
|
+
} else {
|
|
98
|
+
items = [];
|
|
99
|
+
const lines = trimmed.split(/\r?\n/);
|
|
100
|
+
for (let i = 0; i < lines.length; i++) {
|
|
101
|
+
const line = lines[i].trim();
|
|
102
|
+
if (!line) continue;
|
|
103
|
+
try {
|
|
104
|
+
items.push(JSON.parse(line));
|
|
105
|
+
} catch (err) {
|
|
106
|
+
throw new Error(`Invalid JSON on line ${i + 1} of ${source}: ${err.message}`);
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
return items.map((item, i) => {
|
|
111
|
+
if (!item || typeof item !== "object" || Array.isArray(item)) {
|
|
112
|
+
throw new Error(`Request ${i + 1} in ${source} is not a JSON object`);
|
|
113
|
+
}
|
|
114
|
+
if (item.body && typeof item.body === "object") {
|
|
115
|
+
return { custom_id: String(item.custom_id ?? `req-${i + 1}`), body: item.body };
|
|
116
|
+
}
|
|
117
|
+
return { custom_id: `req-${i + 1}`, body: item };
|
|
118
|
+
});
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
function fmtTime(t) {
|
|
122
|
+
if (t == null) return "";
|
|
123
|
+
const n = Number(t);
|
|
124
|
+
if (!Number.isFinite(n)) return String(t);
|
|
125
|
+
return new Date(n < 1e12 ? n * 1000 : n).toISOString().replace(".000Z", "Z");
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
function counts(b) {
|
|
129
|
+
const rc = b.request_counts || {};
|
|
130
|
+
return `${rc.completed ?? 0}/${rc.failed ?? 0}/${rc.total ?? 0}`;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
function printBatch(b) {
|
|
134
|
+
printResult(b, () => {
|
|
135
|
+
outln(`${c.bold("batch:")} ${b.id}`);
|
|
136
|
+
outln(` status: ${b.status}`);
|
|
137
|
+
outln(` model: ${b.model}`);
|
|
138
|
+
outln(` endpoint: ${b.endpoint}`);
|
|
139
|
+
outln(` requests (completed/failed/total): ${counts(b)}`);
|
|
140
|
+
outln(` created: ${fmtTime(b.created_at)}`);
|
|
141
|
+
if (b.finalized_at) outln(` finalized: ${fmtTime(b.finalized_at)}`);
|
|
142
|
+
if (b.usage) {
|
|
143
|
+
const u = b.usage;
|
|
144
|
+
const parts = [];
|
|
145
|
+
if (u.prompt_tokens != null) parts.push(`prompt ${u.prompt_tokens}`);
|
|
146
|
+
if (u.completion_tokens != null) parts.push(`completion ${u.completion_tokens}`);
|
|
147
|
+
if (u.cost != null)
|
|
148
|
+
parts.push(`cost $${Number(u.cost).toFixed(Number(u.cost) < 0.001 ? 7 : 4)}`);
|
|
149
|
+
if (parts.length) outln(` usage: ${parts.join(", ")}`);
|
|
150
|
+
}
|
|
151
|
+
if (b.error && b.error.message) outln(` ${c.red("error:")} ${b.error.message}`);
|
|
152
|
+
if (Array.isArray(b.results)) {
|
|
153
|
+
outln(` results: ${b.results.length} (openrouter batches results ${b.id} -o out.jsonl)`);
|
|
154
|
+
}
|
|
155
|
+
});
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
// 410 and 402 carry a useful meaning the generic API message hides.
|
|
159
|
+
async function getBatch(id, values) {
|
|
160
|
+
try {
|
|
161
|
+
return await api("GET", `/batches/${encodeURIComponent(id)}`, {
|
|
162
|
+
auth: authFromValues(values),
|
|
163
|
+
});
|
|
164
|
+
} catch (err) {
|
|
165
|
+
if (err instanceof APIError && err.status === 410) {
|
|
166
|
+
throw new APIError(`batch expired (results retained 30 days): ${id}`, err);
|
|
167
|
+
}
|
|
168
|
+
if (err instanceof APIError && err.status === 402) {
|
|
169
|
+
throw new APIError(
|
|
170
|
+
`${err.message}\nThe batch cost exceeds your balance. Top up credits, then poll again to get the results.`,
|
|
171
|
+
err,
|
|
172
|
+
);
|
|
173
|
+
}
|
|
174
|
+
throw err;
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
export async function batchesCommand(argv) {
|
|
179
|
+
const sub = argv[0];
|
|
180
|
+
const rest = argv.slice(1);
|
|
181
|
+
if (!sub || sub === "help" || sub === "-h" || sub === "--help") {
|
|
182
|
+
process.stdout.write(HELP);
|
|
183
|
+
return sub ? 0 : 1;
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
if (rest.includes("-h") || rest.includes("--help")) {
|
|
187
|
+
process.stdout.write(HELP);
|
|
188
|
+
return 0;
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
if (sub === "list") {
|
|
192
|
+
const { values } = parseArgs(rest, {
|
|
193
|
+
limit: { type: "string" },
|
|
194
|
+
after: { type: "string" },
|
|
195
|
+
status: { type: "string", multiple: true },
|
|
196
|
+
"created-after": { type: "string" },
|
|
197
|
+
"created-before": { type: "string" },
|
|
198
|
+
});
|
|
199
|
+
const query = {};
|
|
200
|
+
if (values.limit !== undefined) query.limit = numberOption(values.limit, "--limit");
|
|
201
|
+
if (values.after) query.after = values.after;
|
|
202
|
+
if (values.status) query.status = csv(values.status);
|
|
203
|
+
if (values["created-after"]) query.created_after = values["created-after"];
|
|
204
|
+
if (values["created-before"]) query.created_before = values["created-before"];
|
|
205
|
+
const data = await api("GET", "/batches", { auth: authFromValues(values), query });
|
|
206
|
+
printResult(data, () => {
|
|
207
|
+
const rows = data.data || [];
|
|
208
|
+
if (!rows.length) {
|
|
209
|
+
outln("no batches");
|
|
210
|
+
return;
|
|
211
|
+
}
|
|
212
|
+
table(rows, [
|
|
213
|
+
{ label: "id", value: (b) => b.id },
|
|
214
|
+
{ label: "status", value: (b) => b.status },
|
|
215
|
+
{ label: "model", value: (b) => b.model },
|
|
216
|
+
{ label: "endpoint", value: (b) => b.endpoint },
|
|
217
|
+
{ label: "done/failed/total", value: counts },
|
|
218
|
+
{ label: "created", value: (b) => fmtTime(b.created_at) },
|
|
219
|
+
]);
|
|
220
|
+
if (data.has_more && data.last_id) info(`more: --after ${data.last_id}`);
|
|
221
|
+
});
|
|
222
|
+
return 0;
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
if (sub === "create") {
|
|
226
|
+
const { values, positionals } = parseArgs(rest, {
|
|
227
|
+
model: { type: "string", short: "m" },
|
|
228
|
+
endpoint: { type: "string" },
|
|
229
|
+
file: { type: "string" },
|
|
230
|
+
"completion-window": { type: "string" },
|
|
231
|
+
provider: { type: "string" },
|
|
232
|
+
only: { type: "string" },
|
|
233
|
+
"no-fallbacks": { type: "boolean" },
|
|
234
|
+
});
|
|
235
|
+
if (positionals.length) throw new Error(`Unexpected argument: ${positionals[0]}`);
|
|
236
|
+
if (!values.model) throw new Error("--model is required");
|
|
237
|
+
let text;
|
|
238
|
+
let source;
|
|
239
|
+
if (values.file && values.file !== "-") {
|
|
240
|
+
const { readFile } = await import("node:fs/promises");
|
|
241
|
+
text = await readFile(values.file, "utf8");
|
|
242
|
+
source = values.file;
|
|
243
|
+
} else {
|
|
244
|
+
text = await readStdinIfPiped();
|
|
245
|
+
source = "stdin";
|
|
246
|
+
if (!text) throw new Error("No requests: pass --file reqs.jsonl or pipe JSONL on stdin");
|
|
247
|
+
}
|
|
248
|
+
const requests = parseRequests(text, source);
|
|
249
|
+
|
|
250
|
+
let provider = values.provider ? await loadJson(values.provider, "--provider") : undefined;
|
|
251
|
+
if (values.only || values["no-fallbacks"]) provider = { ...(provider || {}) };
|
|
252
|
+
if (values.only) provider.only = csv(values.only);
|
|
253
|
+
if (values["no-fallbacks"]) provider.allow_fallbacks = false;
|
|
254
|
+
|
|
255
|
+
// The API rejects bodies where `requests` comes before the other fields,
|
|
256
|
+
// so build the object in this exact key order.
|
|
257
|
+
const body = { endpoint: resolveEndpoint(values.endpoint), model: values.model };
|
|
258
|
+
if (provider) body.provider = provider;
|
|
259
|
+
if (values["completion-window"]) body.completion_window = values["completion-window"];
|
|
260
|
+
body.requests = requests;
|
|
261
|
+
|
|
262
|
+
const data = await api("POST", "/batches", { auth: authFromValues(values), body });
|
|
263
|
+
printBatch(data);
|
|
264
|
+
info(`Submitted ${requests.length} request(s). Next: openrouter batches wait ${data.id}`);
|
|
265
|
+
return 0;
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
if (sub === "get") {
|
|
269
|
+
const { values, positionals } = parseArgs(rest, {});
|
|
270
|
+
if (!positionals[0]) throw new Error("id required");
|
|
271
|
+
printBatch(await getBatch(positionals[0], values));
|
|
272
|
+
return 0;
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
if (sub === "wait") {
|
|
276
|
+
const { values, positionals } = parseArgs(rest, {
|
|
277
|
+
interval: { type: "string" },
|
|
278
|
+
timeout: { type: "string" },
|
|
279
|
+
});
|
|
280
|
+
if (!positionals[0]) throw new Error("id required");
|
|
281
|
+
const interval = (numberOption(values.interval, "--interval") ?? 10) * 1000;
|
|
282
|
+
const timeout = (numberOption(values.timeout, "--timeout") ?? 86400) * 1000;
|
|
283
|
+
if (interval <= 0) throw new Error("--interval must be greater than 0");
|
|
284
|
+
if (timeout <= 0) throw new Error("--timeout must be greater than 0");
|
|
285
|
+
const start = Date.now();
|
|
286
|
+
let last;
|
|
287
|
+
while (true) {
|
|
288
|
+
const data = await getBatch(positionals[0], values);
|
|
289
|
+
const status = data.status;
|
|
290
|
+
const line = `status: ${status} (${counts(data)} completed/failed/total)`;
|
|
291
|
+
if (line !== last) info(line);
|
|
292
|
+
last = line;
|
|
293
|
+
if (TERMINAL_STATUSES.includes(status)) {
|
|
294
|
+
printBatch(data);
|
|
295
|
+
return FAILED_STATUSES.includes(status) ? 4 : 0;
|
|
296
|
+
}
|
|
297
|
+
if (Date.now() - start > timeout) throw new Error("timeout waiting for batch");
|
|
298
|
+
await sleep(interval);
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
if (sub === "results") {
|
|
303
|
+
const { values, positionals } = parseArgs(rest, {
|
|
304
|
+
out: { type: "string", short: "o" },
|
|
305
|
+
});
|
|
306
|
+
if (!positionals[0]) throw new Error("id required");
|
|
307
|
+
const data = await getBatch(positionals[0], values);
|
|
308
|
+
if (!Array.isArray(data.results)) {
|
|
309
|
+
if (!TERMINAL_STATUSES.includes(data.status)) {
|
|
310
|
+
throw new Error(
|
|
311
|
+
`batch is not finished (status: ${data.status}). Run: openrouter batches wait ${positionals[0]}`,
|
|
312
|
+
);
|
|
313
|
+
}
|
|
314
|
+
const why = data.error && data.error.message ? `: ${data.error.message}` : "";
|
|
315
|
+
throw new Error(`batch ${data.status} with no results${why}`);
|
|
316
|
+
}
|
|
317
|
+
const results = data.results;
|
|
318
|
+
const failed = results.filter((r) => r.error).length;
|
|
319
|
+
const out = values.out;
|
|
320
|
+
if (!out || out === "-") {
|
|
321
|
+
if (isJsonMode()) printJSON(results);
|
|
322
|
+
else for (const r of results) outln(JSON.stringify(r));
|
|
323
|
+
return 0;
|
|
324
|
+
}
|
|
325
|
+
const jsonl = results.map((r) => JSON.stringify(r)).join("\n") + (results.length ? "\n" : "");
|
|
326
|
+
await writeFile(out, jsonl);
|
|
327
|
+
if (isJsonMode()) printJSON({ saved: out, results: results.length, failed });
|
|
328
|
+
else info(`Wrote ${results.length} result(s) to ${out} (${failed} failed)`);
|
|
329
|
+
return 0;
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
if (sub === "delete") {
|
|
333
|
+
const { values, positionals } = parseArgs(rest, {});
|
|
334
|
+
if (!positionals[0]) throw new Error("id required");
|
|
335
|
+
const id = positionals[0];
|
|
336
|
+
let data;
|
|
337
|
+
try {
|
|
338
|
+
data = await api("DELETE", `/batches/${encodeURIComponent(id)}`, {
|
|
339
|
+
auth: authFromValues(values),
|
|
340
|
+
});
|
|
341
|
+
} catch (err) {
|
|
342
|
+
if (err instanceof APIError && err.status === 409) {
|
|
343
|
+
throw new APIError(
|
|
344
|
+
`${err.message}\nOnly finished batches can be deleted: run \`openrouter batches wait ${id}\`, then delete again.`,
|
|
345
|
+
err,
|
|
346
|
+
);
|
|
347
|
+
}
|
|
348
|
+
throw err;
|
|
349
|
+
}
|
|
350
|
+
printResult(data, () => {
|
|
351
|
+
const up = data && data.deletion && data.deletion.upstream;
|
|
352
|
+
outln(`deleted ${id}${up ? ` (upstream ${up.provider}: ${up.status})` : ""}`);
|
|
353
|
+
});
|
|
354
|
+
return 0;
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
throw new Error(`Unknown batches subcommand: ${sub}`);
|
|
358
|
+
}
|
package/src/commands/byok.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
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
3
|
import { outln, printResult, table } from "../output.js";
|
|
4
4
|
|
|
@@ -7,36 +7,39 @@ const HELP = `Usage: openrouter byok <subcommand> [options]
|
|
|
7
7
|
Manage BYOK (bring-your-own-key) provider integrations (requires a provisioning/management key).
|
|
8
8
|
|
|
9
9
|
Subcommands:
|
|
10
|
-
list
|
|
10
|
+
list [options] List integrations
|
|
11
11
|
get <id> Get a single integration
|
|
12
12
|
create [options] Create an integration
|
|
13
13
|
update <id> [options] Update an integration
|
|
14
14
|
delete <id> Delete an integration
|
|
15
15
|
|
|
16
|
-
|
|
17
|
-
--
|
|
18
|
-
--
|
|
19
|
-
--
|
|
20
|
-
--allowed-models <csv>
|
|
21
|
-
--allowed-hashes <csv>
|
|
22
|
-
--allowed-users <csv>
|
|
23
|
-
--disabled / --enabled
|
|
24
|
-
--fallback / --no-fallback
|
|
16
|
+
list options:
|
|
17
|
+
--workspace <uuid> Workspace to list (default: the default workspace)
|
|
18
|
+
--provider <slug> Only this provider (e.g. openai, anthropic, amazon-bedrock)
|
|
19
|
+
--offset <n> / --limit <n> Pagination (limit max 100)
|
|
25
20
|
|
|
26
|
-
update options:
|
|
27
|
-
--provider <slug>
|
|
28
|
-
--api-key <key>
|
|
21
|
+
create / update options:
|
|
22
|
+
--provider <slug> Provider slug (create only, required)
|
|
23
|
+
--api-key <key> Provider secret key (required on create; on update it
|
|
24
|
+
rotates the key in place)
|
|
25
|
+
--workspace <uuid> Workspace to create it in (create only)
|
|
29
26
|
--name <text>
|
|
30
|
-
--allowed-models <csv>
|
|
31
|
-
--allowed-hashes <csv>
|
|
32
|
-
--allowed-users <csv>
|
|
27
|
+
--allowed-models <csv> ("" clears the allowlist)
|
|
28
|
+
--allowed-hashes <csv> OpenRouter key hashes ("" clears)
|
|
29
|
+
--allowed-users <csv> ("" clears)
|
|
33
30
|
--disabled / --enabled
|
|
34
|
-
--fallback / --no-fallback
|
|
31
|
+
--fallback / --no-fallback Use only after non-fallback keys (not with --byok-only)
|
|
32
|
+
--required / --no-required Drop shared endpoints for this key's models
|
|
33
|
+
--byok-only / --no-byok-only Drop shared endpoints for every model on this
|
|
34
|
+
provider, even after your keys fail
|
|
35
|
+
--region <r> Declared data region: global | europe | us | none
|
|
36
|
+
--zdr <v> Declared zero data retention: true | false | none
|
|
35
37
|
`;
|
|
36
38
|
|
|
37
39
|
const WRITE_OPTIONS = {
|
|
38
40
|
provider: { type: "string" },
|
|
39
41
|
"api-key": { type: "string" },
|
|
42
|
+
workspace: { type: "string" },
|
|
40
43
|
name: { type: "string" },
|
|
41
44
|
"allowed-models": { type: "string" },
|
|
42
45
|
"allowed-hashes": { type: "string" },
|
|
@@ -45,10 +48,70 @@ const WRITE_OPTIONS = {
|
|
|
45
48
|
enabled: { type: "boolean" },
|
|
46
49
|
fallback: { type: "boolean" },
|
|
47
50
|
"no-fallback": { type: "boolean" },
|
|
51
|
+
required: { type: "boolean" },
|
|
52
|
+
"no-required": { type: "boolean" },
|
|
53
|
+
"byok-only": { type: "boolean" },
|
|
54
|
+
"no-byok-only": { type: "boolean" },
|
|
55
|
+
region: { type: "string" },
|
|
56
|
+
zdr: { type: "string" },
|
|
48
57
|
};
|
|
49
58
|
|
|
59
|
+
const REGIONS = ["global", "europe", "us"];
|
|
60
|
+
|
|
61
|
+
// "" means null (no restriction), the API's way to clear an allowlist.
|
|
50
62
|
function csvOption(value) {
|
|
51
|
-
|
|
63
|
+
if (value === "") return null;
|
|
64
|
+
return value
|
|
65
|
+
.split(",")
|
|
66
|
+
.map((s) => s.trim())
|
|
67
|
+
.filter(Boolean);
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
function toggle(values, on, off) {
|
|
71
|
+
if (values[on] && values[off]) throw new Error(`Pass only one of --${on} or --${off}.`);
|
|
72
|
+
if (values[on]) return true;
|
|
73
|
+
if (values[off]) return false;
|
|
74
|
+
return undefined;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
// Fields shared by create and update (both bodies take the same names).
|
|
78
|
+
function buildBody(values) {
|
|
79
|
+
const body = {};
|
|
80
|
+
if (values["api-key"]) body.key = values["api-key"];
|
|
81
|
+
if (values.name) body.name = values.name;
|
|
82
|
+
if (values["allowed-models"] !== undefined)
|
|
83
|
+
body.allowed_models = csvOption(values["allowed-models"]);
|
|
84
|
+
if (values["allowed-hashes"] !== undefined)
|
|
85
|
+
body.allowed_api_key_hashes = csvOption(values["allowed-hashes"]);
|
|
86
|
+
if (values["allowed-users"] !== undefined)
|
|
87
|
+
body.allowed_user_ids = csvOption(values["allowed-users"]);
|
|
88
|
+
const pairs = [
|
|
89
|
+
["disabled", "disabled", "enabled"],
|
|
90
|
+
["is_fallback", "fallback", "no-fallback"],
|
|
91
|
+
["is_required", "required", "no-required"],
|
|
92
|
+
["is_byok_only", "byok-only", "no-byok-only"],
|
|
93
|
+
];
|
|
94
|
+
for (const [field, on, off] of pairs) {
|
|
95
|
+
const v = toggle(values, on, off);
|
|
96
|
+
if (v !== undefined) body[field] = v;
|
|
97
|
+
}
|
|
98
|
+
if (body.is_fallback && body.is_byok_only) {
|
|
99
|
+
throw new Error("--fallback cannot be combined with --byok-only.");
|
|
100
|
+
}
|
|
101
|
+
if (values.region !== undefined) {
|
|
102
|
+
if (values.region === "none") body.declared_region = null;
|
|
103
|
+
else if (REGIONS.includes(values.region)) body.declared_region = values.region;
|
|
104
|
+
else
|
|
105
|
+
throw new Error(
|
|
106
|
+
`Unknown --region "${values.region}". Expected: ${REGIONS.join(" | ")} | none`,
|
|
107
|
+
);
|
|
108
|
+
}
|
|
109
|
+
if (values.zdr !== undefined) {
|
|
110
|
+
const map = { true: true, false: false, none: null };
|
|
111
|
+
if (!(values.zdr in map)) throw new Error("--zdr must be true, false, or none");
|
|
112
|
+
body.declared_zdr = map[values.zdr];
|
|
113
|
+
}
|
|
114
|
+
return body;
|
|
52
115
|
}
|
|
53
116
|
|
|
54
117
|
export async function byokCommand(argv) {
|
|
@@ -65,10 +128,18 @@ export async function byokCommand(argv) {
|
|
|
65
128
|
}
|
|
66
129
|
|
|
67
130
|
if (sub === "list") {
|
|
68
|
-
const { values } = parseArgs(rest, {
|
|
131
|
+
const { values } = parseArgs(rest, {
|
|
132
|
+
...PAGINATION_OPTIONS,
|
|
133
|
+
workspace: { type: "string" },
|
|
134
|
+
provider: { type: "string" },
|
|
135
|
+
});
|
|
136
|
+
const query = paginationQuery(values);
|
|
137
|
+
if (values.workspace) query.workspace_id = values.workspace;
|
|
138
|
+
if (values.provider) query.provider = values.provider;
|
|
69
139
|
const data = await api("GET", "/byok", {
|
|
70
140
|
auth: authFromValues(values),
|
|
71
141
|
requiresManagement: true,
|
|
142
|
+
query,
|
|
72
143
|
});
|
|
73
144
|
printResult(data, () => {
|
|
74
145
|
const rows = data.data || [];
|
|
@@ -77,6 +148,19 @@ export async function byokCommand(argv) {
|
|
|
77
148
|
{ label: "provider", value: (b) => b.provider || "" },
|
|
78
149
|
{ label: "name", value: (b) => b.name || "" },
|
|
79
150
|
{ label: "disabled", value: (b) => (b.disabled ? "yes" : "") },
|
|
151
|
+
{
|
|
152
|
+
label: "mode",
|
|
153
|
+
value: (b) =>
|
|
154
|
+
[
|
|
155
|
+
b.is_fallback && "fallback",
|
|
156
|
+
b.is_required && "required",
|
|
157
|
+
b.is_byok_only && "byok-only",
|
|
158
|
+
]
|
|
159
|
+
.filter(Boolean)
|
|
160
|
+
.join(","),
|
|
161
|
+
},
|
|
162
|
+
{ label: "region", value: (b) => b.declared_region ?? "" },
|
|
163
|
+
{ label: "zdr", value: (b) => (b.declared_zdr == null ? "" : String(b.declared_zdr)) },
|
|
80
164
|
]);
|
|
81
165
|
});
|
|
82
166
|
return 0;
|
|
@@ -97,15 +181,8 @@ export async function byokCommand(argv) {
|
|
|
97
181
|
const { values } = parseArgs(rest, WRITE_OPTIONS);
|
|
98
182
|
if (!values.provider) throw new Error("--provider required");
|
|
99
183
|
if (!values["api-key"]) throw new Error("--api-key required");
|
|
100
|
-
const body = { provider: values.provider,
|
|
101
|
-
if (values.
|
|
102
|
-
if (values["allowed-models"]) body.allowed_models = csvOption(values["allowed-models"]);
|
|
103
|
-
if (values["allowed-hashes"]) body.allowed_api_key_hashes = csvOption(values["allowed-hashes"]);
|
|
104
|
-
if (values["allowed-users"]) body.allowed_user_ids = csvOption(values["allowed-users"]);
|
|
105
|
-
if (values.disabled) body.disabled = true;
|
|
106
|
-
if (values.enabled) body.disabled = false;
|
|
107
|
-
if (values.fallback) body.is_fallback = true;
|
|
108
|
-
if (values["no-fallback"]) body.is_fallback = false;
|
|
184
|
+
const body = { provider: values.provider, ...buildBody(values) };
|
|
185
|
+
if (values.workspace) body.workspace_id = values.workspace;
|
|
109
186
|
const data = await api("POST", "/byok", {
|
|
110
187
|
auth: authFromValues(values),
|
|
111
188
|
requiresManagement: true,
|
|
@@ -118,17 +195,14 @@ export async function byokCommand(argv) {
|
|
|
118
195
|
if (sub === "update") {
|
|
119
196
|
const { values, positionals } = parseArgs(rest, WRITE_OPTIONS);
|
|
120
197
|
if (!positionals[0]) throw new Error("id required");
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
if (values
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
if (values.enabled) body.disabled = false;
|
|
130
|
-
if (values.fallback) body.is_fallback = true;
|
|
131
|
-
if (values["no-fallback"]) body.is_fallback = false;
|
|
198
|
+
// The update body has no provider or workspace_id: a credential cannot
|
|
199
|
+
// change either after creation.
|
|
200
|
+
if (values.provider || values.workspace) {
|
|
201
|
+
throw new Error(
|
|
202
|
+
"--provider and --workspace are create-only; delete and recreate the integration to change them.",
|
|
203
|
+
);
|
|
204
|
+
}
|
|
205
|
+
const body = buildBody(values);
|
|
132
206
|
const data = await api("PATCH", `/byok/${encodeURIComponent(positionals[0])}`, {
|
|
133
207
|
auth: authFromValues(values),
|
|
134
208
|
requiresManagement: true,
|