@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
|
@@ -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");
|
package/src/commands/rerank.js
CHANGED
|
@@ -1,6 +1,15 @@
|
|
|
1
1
|
import { stdin } from "node:process";
|
|
2
|
-
import {
|
|
2
|
+
import {
|
|
3
|
+
parseArgs,
|
|
4
|
+
authFromValues,
|
|
5
|
+
numberOption,
|
|
6
|
+
loadJson,
|
|
7
|
+
applyRequestFields,
|
|
8
|
+
MEDIA_FIELD_HELP,
|
|
9
|
+
MEDIA_FIELD_OPTIONS,
|
|
10
|
+
} from "../args.js";
|
|
3
11
|
import { api } from "../api.js";
|
|
12
|
+
import { mediaUrl } from "../media.js";
|
|
4
13
|
import { c, outln, printResult } from "../output.js";
|
|
5
14
|
|
|
6
15
|
const HELP = `Usage: openrouter rerank --model <id> --query <q> [doc...] [options]
|
|
@@ -12,9 +21,12 @@ Options:
|
|
|
12
21
|
--query <text> Query string (required)
|
|
13
22
|
-d, --doc <text> Document (repeatable). Or pass docs as positionals.
|
|
14
23
|
--docs-file <p> Read docs from file (one per line). - for stdin.
|
|
24
|
+
--image-doc <url|path> Image document (repeatable, multimodal models);
|
|
25
|
+
local files are sent as data URLs
|
|
15
26
|
--top-n <n> Return only the top N
|
|
16
|
-
--provider <json>
|
|
17
|
-
|
|
27
|
+
--provider <json|@file>
|
|
28
|
+
--extra <json|@file> Extra fields merged into the body (last)
|
|
29
|
+
${MEDIA_FIELD_HELP}`;
|
|
18
30
|
|
|
19
31
|
async function readDocsFromStdin() {
|
|
20
32
|
let data = "";
|
|
@@ -31,8 +43,11 @@ export async function rerankCommand(argv) {
|
|
|
31
43
|
query: { type: "string" },
|
|
32
44
|
doc: { type: "string", short: "d", multiple: true },
|
|
33
45
|
"docs-file": { type: "string" },
|
|
46
|
+
"image-doc": { type: "string", multiple: true },
|
|
34
47
|
"top-n": { type: "string" },
|
|
35
48
|
provider: { type: "string" },
|
|
49
|
+
extra: { type: "string" },
|
|
50
|
+
...MEDIA_FIELD_OPTIONS,
|
|
36
51
|
});
|
|
37
52
|
if (values.help) {
|
|
38
53
|
process.stdout.write(HELP);
|
|
@@ -55,7 +70,9 @@ export async function rerankCommand(argv) {
|
|
|
55
70
|
);
|
|
56
71
|
}
|
|
57
72
|
}
|
|
58
|
-
|
|
73
|
+
for (const img of values["image-doc"] || []) docs.push({ image: await mediaUrl(img) });
|
|
74
|
+
if (!docs.length)
|
|
75
|
+
throw new Error("No documents. Pass via positionals, --doc, --docs-file or --image-doc.");
|
|
59
76
|
|
|
60
77
|
const body = {
|
|
61
78
|
model: values.model,
|
|
@@ -63,7 +80,9 @@ export async function rerankCommand(argv) {
|
|
|
63
80
|
documents: docs,
|
|
64
81
|
};
|
|
65
82
|
if (values["top-n"]) body.top_n = numberOption(values["top-n"], "--top-n");
|
|
66
|
-
if (values.provider) body.provider =
|
|
83
|
+
if (values.provider) body.provider = await loadJson(values.provider, "--provider");
|
|
84
|
+
await applyRequestFields(body, values);
|
|
85
|
+
if (values.extra) Object.assign(body, await loadJson(values.extra, "--extra"));
|
|
67
86
|
|
|
68
87
|
const data = await api("POST", "/rerank", {
|
|
69
88
|
auth: authFromValues(values),
|
|
@@ -74,7 +93,10 @@ export async function rerankCommand(argv) {
|
|
|
74
93
|
for (const r of results) {
|
|
75
94
|
const idx = r.index;
|
|
76
95
|
const score = r.relevance_score ?? r.score;
|
|
77
|
-
const
|
|
96
|
+
const doc = docs[idx];
|
|
97
|
+
const text =
|
|
98
|
+
(r.document && r.document.text) ||
|
|
99
|
+
(typeof doc === "string" ? doc : doc?.text || (doc?.image ? "[image]" : ""));
|
|
78
100
|
outln(`${c.bold(score?.toFixed?.(4) ?? score)} ${c.dim(`#${idx}`)} ${text.slice(0, 120)}`);
|
|
79
101
|
}
|
|
80
102
|
});
|
package/src/commands/speech.js
CHANGED
|
@@ -1,6 +1,16 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import {
|
|
2
|
+
parseArgs,
|
|
3
|
+
authFromValues,
|
|
4
|
+
numberOption,
|
|
5
|
+
readStdinIfPiped,
|
|
6
|
+
loadJson,
|
|
7
|
+
applyRequestFields,
|
|
8
|
+
MEDIA_FIELD_HELP,
|
|
9
|
+
MEDIA_FIELD_OPTIONS,
|
|
10
|
+
} from "../args.js";
|
|
2
11
|
import { api } from "../api.js";
|
|
3
|
-
import {
|
|
12
|
+
import { mediaKind, mediaUrl } from "../media.js";
|
|
13
|
+
import { info, writeBinaryOutput } from "../output.js";
|
|
4
14
|
|
|
5
15
|
const HELP = `Usage: openrouter speech [text...] [options]
|
|
6
16
|
|
|
@@ -8,21 +18,52 @@ Generate speech audio from text. Reads text from arguments or stdin.
|
|
|
8
18
|
|
|
9
19
|
Options:
|
|
10
20
|
-m, --model <id> TTS model id (required)
|
|
11
|
-
--voice <name> Voice id
|
|
12
|
-
--
|
|
21
|
+
--voice <name> Voice id (provider-specific)
|
|
22
|
+
--instructions <text> Tone, pacing or emotion (OpenAI gpt-4o-mini-tts, Gemini TTS)
|
|
23
|
+
--turns <json|@file> Multi-speaker input instead of text (Gemini TTS):
|
|
24
|
+
[{"text":"Hi Jane.","voice":"Kore","instructions":"whispering"}, ...]
|
|
25
|
+
--ref <url|path> Voice-cloning reference (repeatable, up to 3 audio clips),
|
|
26
|
+
or one image (.png/.jpg/.webp) for voice design.
|
|
27
|
+
Local files are sent as data URLs.
|
|
28
|
+
--ref-text <t> Transcript of a --ref clip (repeatable, in --ref order)
|
|
29
|
+
--format <fmt> mp3 | pcm (default: mp3; pcm is raw 16-bit audio)
|
|
13
30
|
--speed <n> Speech speed (e.g. 1.0)
|
|
14
|
-
-o, --out <file> Output audio file (default: out.mp3, or stdout if -)
|
|
15
|
-
--provider <json>
|
|
16
|
-
|
|
31
|
+
-o, --out <file> Output audio file (default: out.mp3 / out.pcm, or stdout if -)
|
|
32
|
+
--provider <json|@file> Routing and provider options, e.g.
|
|
33
|
+
'{"zdr":true,"options":{"elevenlabs":{"stability":0.5}}}'
|
|
34
|
+
--extra <json|@file> Extra fields merged into the body (last)
|
|
35
|
+
${MEDIA_FIELD_HELP}`;
|
|
36
|
+
|
|
37
|
+
async function referenceParts(refs, texts) {
|
|
38
|
+
if (texts.length > refs.length) throw new Error("More --ref-text values than --ref clips");
|
|
39
|
+
const parts = [];
|
|
40
|
+
for (const [i, ref] of refs.entries()) {
|
|
41
|
+
const isUrl = /^https?:\/\//i.test(ref);
|
|
42
|
+
if (mediaKind(ref) === "image") {
|
|
43
|
+
parts.push({ type: "image_url", image_url: { url: await mediaUrl(ref) } });
|
|
44
|
+
} else {
|
|
45
|
+
const input_audio = isUrl ? { url: ref } : { data: await mediaUrl(ref) };
|
|
46
|
+
parts.push({ type: "input_audio", input_audio });
|
|
47
|
+
}
|
|
48
|
+
if (texts[i]) parts.push({ type: "text", text: texts[i] });
|
|
49
|
+
}
|
|
50
|
+
return parts;
|
|
51
|
+
}
|
|
17
52
|
|
|
18
53
|
export async function speechCommand(argv) {
|
|
19
54
|
const { values, positionals } = parseArgs(argv, {
|
|
20
55
|
model: { type: "string", short: "m" },
|
|
21
56
|
voice: { type: "string" },
|
|
57
|
+
instructions: { type: "string" },
|
|
58
|
+
turns: { type: "string" },
|
|
59
|
+
ref: { type: "string", multiple: true },
|
|
60
|
+
"ref-text": { type: "string", multiple: true },
|
|
22
61
|
format: { type: "string" },
|
|
23
62
|
speed: { type: "string" },
|
|
24
63
|
out: { type: "string", short: "o" },
|
|
25
64
|
provider: { type: "string" },
|
|
65
|
+
extra: { type: "string" },
|
|
66
|
+
...MEDIA_FIELD_OPTIONS,
|
|
26
67
|
});
|
|
27
68
|
if (values.help) {
|
|
28
69
|
process.stdout.write(HELP);
|
|
@@ -30,16 +71,36 @@ export async function speechCommand(argv) {
|
|
|
30
71
|
}
|
|
31
72
|
if (!values.model) throw new Error("--model is required");
|
|
32
73
|
|
|
33
|
-
let
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
74
|
+
let input;
|
|
75
|
+
if (values.turns) {
|
|
76
|
+
if (positionals.length) throw new Error("Pass text or --turns, not both");
|
|
77
|
+
input = await loadJson(values.turns, "--turns");
|
|
78
|
+
if (!Array.isArray(input) || !input.length) {
|
|
79
|
+
throw new Error("--turns must be a non-empty JSON array of {text, voice?, instructions?}");
|
|
80
|
+
}
|
|
81
|
+
} else {
|
|
82
|
+
let text = positionals.join(" ").trim();
|
|
83
|
+
const piped = await readStdinIfPiped();
|
|
84
|
+
if (piped) text = text ? `${text}\n${piped}` : piped;
|
|
85
|
+
if (!text) throw new Error("No input text.");
|
|
86
|
+
input = text;
|
|
87
|
+
}
|
|
37
88
|
|
|
38
|
-
|
|
89
|
+
// The API defaults to raw PCM; ask for mp3 so the default out.mp3 plays.
|
|
90
|
+
const format = values.format || "mp3";
|
|
91
|
+
const body = { model: values.model, input, response_format: format };
|
|
39
92
|
if (values.voice) body.voice = values.voice;
|
|
40
|
-
if (values.
|
|
93
|
+
if (values.instructions) body.instructions = values.instructions;
|
|
94
|
+
if (values["ref-text"]?.length && !values.ref?.length) {
|
|
95
|
+
throw new Error("--ref-text needs a --ref clip");
|
|
96
|
+
}
|
|
97
|
+
if (values.ref?.length) {
|
|
98
|
+
body.input_references = await referenceParts(values.ref, values["ref-text"] || []);
|
|
99
|
+
}
|
|
41
100
|
if (values.speed) body.speed = numberOption(values.speed, "--speed");
|
|
42
|
-
if (values.provider) body.provider =
|
|
101
|
+
if (values.provider) body.provider = await loadJson(values.provider, "--provider");
|
|
102
|
+
await applyRequestFields(body, values);
|
|
103
|
+
if (values.extra) Object.assign(body, await loadJson(values.extra, "--extra"));
|
|
43
104
|
|
|
44
105
|
const bytes = await api("POST", "/audio/speech", {
|
|
45
106
|
auth: authFromValues(values),
|
|
@@ -47,7 +108,11 @@ export async function speechCommand(argv) {
|
|
|
47
108
|
binary: true,
|
|
48
109
|
});
|
|
49
110
|
|
|
50
|
-
const
|
|
111
|
+
const outFormat = body.response_format;
|
|
112
|
+
const out = values.out || `out.${outFormat}`;
|
|
113
|
+
if (outFormat === "pcm" && /\.mp3$/i.test(out)) {
|
|
114
|
+
info(`warning: ${out} holds raw PCM audio, not mp3 (use --format mp3 or a .pcm name)`);
|
|
115
|
+
}
|
|
51
116
|
await writeBinaryOutput(bytes, out);
|
|
52
117
|
return 0;
|
|
53
118
|
}
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
import { parseArgs, authFromValues, PAGINATION_OPTIONS, paginationQuery } from "../args.js";
|
|
2
|
+
import { api } from "../api.js";
|
|
3
|
+
import { c, outln, printResult, table } from "../output.js";
|
|
4
|
+
|
|
5
|
+
const HELP = `Usage: openrouter tools <subcommand> [options]
|
|
6
|
+
|
|
7
|
+
Server tools OpenRouter runs for a model (web search, web fetch, shell, ...):
|
|
8
|
+
accepted tools[].type spellings, engines with pricing, native support.
|
|
9
|
+
|
|
10
|
+
Subcommands:
|
|
11
|
+
list [options] List server tools (default)
|
|
12
|
+
get <name> One tool by canonical name or alias (web_search -> openrouter:web_search)
|
|
13
|
+
|
|
14
|
+
list options:
|
|
15
|
+
--offset <n>
|
|
16
|
+
--limit <n> 1-100 (all tools when both are omitted)
|
|
17
|
+
--api-format <f> responses | chat-completions | anthropic-messages
|
|
18
|
+
`;
|
|
19
|
+
|
|
20
|
+
function engineNames(t) {
|
|
21
|
+
return (t.engines || []).map((e) => e.id || e.name).filter(Boolean);
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
function short(s, n = 60) {
|
|
25
|
+
const v = s || "";
|
|
26
|
+
return v.length > n ? v.slice(0, n - 3) + "..." : v;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export async function toolsCommand(argv) {
|
|
30
|
+
if (argv[0] === "help" || argv[0] === "-h" || argv[0] === "--help") {
|
|
31
|
+
process.stdout.write(HELP);
|
|
32
|
+
return 0;
|
|
33
|
+
}
|
|
34
|
+
const sub = argv[0] && !argv[0].startsWith("-") ? argv[0] : "list";
|
|
35
|
+
const rest = sub === argv[0] ? argv.slice(1) : argv;
|
|
36
|
+
|
|
37
|
+
if (rest.includes("-h") || rest.includes("--help")) {
|
|
38
|
+
process.stdout.write(HELP);
|
|
39
|
+
return 0;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
if (sub === "list") {
|
|
43
|
+
const { values } = parseArgs(rest, {
|
|
44
|
+
...PAGINATION_OPTIONS,
|
|
45
|
+
"api-format": { type: "string" },
|
|
46
|
+
});
|
|
47
|
+
const query = paginationQuery(values);
|
|
48
|
+
if (values["api-format"]) query.api_format = values["api-format"];
|
|
49
|
+
const data = await api("GET", "/tools", { auth: authFromValues(values), query });
|
|
50
|
+
printResult(data, () => {
|
|
51
|
+
table(data.data || [], [
|
|
52
|
+
{ label: "id", value: (t) => t.id },
|
|
53
|
+
{ label: "name", value: (t) => t.name },
|
|
54
|
+
{ label: "status", value: (t) => t.status },
|
|
55
|
+
{ label: "engines", value: (t) => engineNames(t).join(",") },
|
|
56
|
+
{ label: "summary", value: (t) => short(t.summary) },
|
|
57
|
+
]);
|
|
58
|
+
});
|
|
59
|
+
return 0;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
if (sub === "get") {
|
|
63
|
+
const { values, positionals } = parseArgs(rest, {});
|
|
64
|
+
if (!positionals[0]) throw new Error("name required");
|
|
65
|
+
// Aliases answer 308 to the canonical URL; fetch follows it.
|
|
66
|
+
const data = await api("GET", `/tools/${encodeURIComponent(positionals[0])}`, {
|
|
67
|
+
auth: authFromValues(values),
|
|
68
|
+
});
|
|
69
|
+
printResult(data, () => {
|
|
70
|
+
const t = data.data || data;
|
|
71
|
+
outln(`${c.bold(t.id)} ${t.name || ""} [${t.status || ""}]`);
|
|
72
|
+
if (t.summary) outln(` ${t.summary}`);
|
|
73
|
+
if (t.docs_url) outln(` docs: ${t.docs_url}`);
|
|
74
|
+
if (t.supported_api_formats) outln(` api formats: ${t.supported_api_formats.join(", ")}`);
|
|
75
|
+
const aliases = (t.aliases || []).map((a) => a.type).filter(Boolean);
|
|
76
|
+
if (aliases.length) outln(` aliases: ${aliases.join(", ")}`);
|
|
77
|
+
if (t.default_engine) {
|
|
78
|
+
outln(
|
|
79
|
+
` default engine: ${t.default_engine}${t.fallback_engine ? ` (fallback: ${t.fallback_engine})` : ""}`,
|
|
80
|
+
);
|
|
81
|
+
}
|
|
82
|
+
if (t.native_support && t.native_support.endpoint_count != null) {
|
|
83
|
+
outln(` native endpoints: ${t.native_support.endpoint_count}`);
|
|
84
|
+
}
|
|
85
|
+
const engines = t.engines || [];
|
|
86
|
+
if (engines.length) {
|
|
87
|
+
outln(c.bold("engines:"));
|
|
88
|
+
table(engines, [
|
|
89
|
+
{ label: "id", value: (e) => e.id || "" },
|
|
90
|
+
{ label: "name", value: (e) => e.name || "" },
|
|
91
|
+
{ label: "executed_by", value: (e) => e.executed_by || "" },
|
|
92
|
+
{ label: "byok", value: (e) => e.byok || "" },
|
|
93
|
+
{ label: "regions", value: (e) => (e.data_regions || []).join(",") },
|
|
94
|
+
]);
|
|
95
|
+
}
|
|
96
|
+
outln(c.dim("full detail (schemas, pricing): add --json"));
|
|
97
|
+
});
|
|
98
|
+
return 0;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
throw new Error(`Unknown tools subcommand: ${sub}`);
|
|
102
|
+
}
|