@aaronjmars/openroutercli 0.1.6 → 0.2.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 +4 -0
- package/bin/openrouter.js +3 -3
- package/package.json +7 -2
- package/src/api.js +36 -25
- package/src/args.js +17 -18
- package/src/cli.js +53 -35
- package/src/commands/account.js +45 -43
- package/src/commands/byok.js +153 -0
- package/src/commands/chat.js +228 -107
- package/src/commands/embed.js +31 -26
- package/src/commands/files.js +102 -0
- package/src/commands/generation.js +10 -10
- package/src/commands/guardrails.js +98 -79
- package/src/commands/image.js +149 -0
- package/src/commands/keys.js +61 -55
- package/src/commands/login.js +84 -88
- package/src/commands/messages.js +119 -55
- package/src/commands/misc.js +45 -45
- package/src/commands/models.js +215 -139
- package/src/commands/presets.js +93 -0
- package/src/commands/request.js +20 -20
- package/src/commands/rerank.js +33 -25
- package/src/commands/speech.js +16 -16
- package/src/commands/transcribe.js +62 -0
- package/src/commands/video.js +45 -42
- package/src/commands/workspaces.js +49 -45
- package/src/config.js +17 -23
- package/src/output.js +14 -27
package/README.md
CHANGED
|
@@ -207,3 +207,7 @@ This is an **unofficial** community project. It is not affiliated with,
|
|
|
207
207
|
endorsed by, or sponsored by OpenRouter. "OpenRouter" and the OpenRouter
|
|
208
208
|
logo are trademarks of their respective owners; usage here is for
|
|
209
209
|
identification only.
|
|
210
|
+
|
|
211
|
+
---
|
|
212
|
+
|
|
213
|
+
Built by [Aaron Elijah Mars](https://aaronjmars.com), founder of Aeon and MiroShark · [@aaronjmars](https://github.com/aaronjmars)
|
package/bin/openrouter.js
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
-
import { run } from
|
|
2
|
+
import { run } from "../src/cli.js";
|
|
3
3
|
|
|
4
4
|
run(process.argv.slice(2)).catch((err) => {
|
|
5
5
|
const msg = err && err.message ? err.message : String(err);
|
|
6
6
|
process.stderr.write(`error: ${msg}\n`);
|
|
7
7
|
if (process.env.OPENROUTER_DEBUG && err && err.stack) {
|
|
8
|
-
process.stderr.write(err.stack +
|
|
8
|
+
process.stderr.write(err.stack + "\n");
|
|
9
9
|
}
|
|
10
|
-
process.exit(err && typeof err.exitCode ===
|
|
10
|
+
process.exit(err && typeof err.exitCode === "number" ? err.exitCode : 1);
|
|
11
11
|
});
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@aaronjmars/openroutercli",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.2.0",
|
|
4
4
|
"description": "Command-line interface for the OpenRouter API. Built for humans and LLM agents.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"bin": {
|
|
@@ -50,6 +50,11 @@
|
|
|
50
50
|
"access": "public"
|
|
51
51
|
},
|
|
52
52
|
"scripts": {
|
|
53
|
-
"start": "node bin/openrouter.js"
|
|
53
|
+
"start": "node bin/openrouter.js",
|
|
54
|
+
"format": "biome format --write .",
|
|
55
|
+
"format:check": "biome ci ."
|
|
56
|
+
},
|
|
57
|
+
"devDependencies": {
|
|
58
|
+
"@biomejs/biome": "^2.5.12"
|
|
54
59
|
}
|
|
55
60
|
}
|
package/src/api.js
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
import { resolveAuth } from
|
|
1
|
+
import { resolveAuth } from "./config.js";
|
|
2
2
|
|
|
3
3
|
export class APIError extends Error {
|
|
4
4
|
constructor(message, { status, body } = {}) {
|
|
5
5
|
super(message);
|
|
6
|
-
this.name =
|
|
6
|
+
this.name = "APIError";
|
|
7
7
|
this.status = status;
|
|
8
8
|
this.body = body;
|
|
9
9
|
this.exitCode = 2;
|
|
@@ -12,9 +12,9 @@ export class APIError extends Error {
|
|
|
12
12
|
|
|
13
13
|
function buildHeaders({ apiKey, referer, title, extra }) {
|
|
14
14
|
const headers = {
|
|
15
|
-
Accept:
|
|
16
|
-
|
|
17
|
-
|
|
15
|
+
Accept: "application/json",
|
|
16
|
+
"HTTP-Referer": referer,
|
|
17
|
+
"X-Title": title,
|
|
18
18
|
};
|
|
19
19
|
if (apiKey) headers.Authorization = `Bearer ${apiKey}`;
|
|
20
20
|
return { ...headers, ...(extra || {}) };
|
|
@@ -22,7 +22,7 @@ function buildHeaders({ apiKey, referer, title, extra }) {
|
|
|
22
22
|
|
|
23
23
|
function joinUrl(base, path) {
|
|
24
24
|
if (/^https?:\/\//i.test(path)) return path;
|
|
25
|
-
return base.replace(/\/$/,
|
|
25
|
+
return base.replace(/\/$/, "") + (path.startsWith("/") ? path : "/" + path);
|
|
26
26
|
}
|
|
27
27
|
|
|
28
28
|
async function parseError(res) {
|
|
@@ -33,10 +33,10 @@ async function parseError(res) {
|
|
|
33
33
|
} catch {}
|
|
34
34
|
const message =
|
|
35
35
|
(body && body.error && body.error.message) ||
|
|
36
|
-
(typeof body ===
|
|
36
|
+
(typeof body === "string" ? body : `HTTP ${res.status}`);
|
|
37
37
|
throw new APIError(`${res.status} ${res.statusText}: ${message}`, {
|
|
38
38
|
status: res.status,
|
|
39
|
-
body
|
|
39
|
+
body,
|
|
40
40
|
});
|
|
41
41
|
}
|
|
42
42
|
|
|
@@ -46,28 +46,40 @@ export async function api(method, path, opts = {}) {
|
|
|
46
46
|
const auth = await resolveAuth(authOpts);
|
|
47
47
|
if (opts.requireAuth !== false && !auth.apiKey) {
|
|
48
48
|
const msg = opts.requiresManagement
|
|
49
|
-
?
|
|
50
|
-
:
|
|
49
|
+
? "No API key set. This command needs a management key. Run `openrouter login --management` or set OPENROUTER_MANAGEMENT_KEY."
|
|
50
|
+
: "No API key set. Run `openrouter login` or `openrouter login --key sk-or-...`, or set OPENROUTER_API_KEY.";
|
|
51
51
|
const e = new Error(msg);
|
|
52
52
|
e.exitCode = 3;
|
|
53
53
|
throw e;
|
|
54
54
|
}
|
|
55
55
|
|
|
56
|
-
const url =
|
|
56
|
+
const url =
|
|
57
|
+
joinUrl(auth.baseUrl, path) +
|
|
58
|
+
(opts.query ? "?" + new URLSearchParams(opts.query).toString() : "");
|
|
57
59
|
const headers = buildHeaders({
|
|
58
60
|
apiKey: auth.apiKey,
|
|
59
61
|
referer: auth.referer,
|
|
60
62
|
title: auth.title,
|
|
61
|
-
extra: opts.headers
|
|
63
|
+
extra: opts.headers,
|
|
62
64
|
});
|
|
63
65
|
|
|
64
66
|
let body;
|
|
65
67
|
if (opts.body !== undefined) {
|
|
66
|
-
|
|
68
|
+
// Pass raw byte/string/streaming bodies straight through. FormData in
|
|
69
|
+
// particular must reach fetch untouched so it can set the multipart
|
|
70
|
+
// Content-Type (with boundary) itself.
|
|
71
|
+
const raw =
|
|
72
|
+
opts.body instanceof Uint8Array ||
|
|
73
|
+
typeof opts.body === "string" ||
|
|
74
|
+
(typeof FormData !== "undefined" && opts.body instanceof FormData) ||
|
|
75
|
+
(typeof Blob !== "undefined" && opts.body instanceof Blob) ||
|
|
76
|
+
opts.body instanceof ArrayBuffer ||
|
|
77
|
+
opts.body instanceof URLSearchParams;
|
|
78
|
+
if (raw) {
|
|
67
79
|
body = opts.body;
|
|
68
80
|
} else {
|
|
69
81
|
body = JSON.stringify(opts.body);
|
|
70
|
-
headers[
|
|
82
|
+
headers["Content-Type"] = "application/json";
|
|
71
83
|
}
|
|
72
84
|
}
|
|
73
85
|
|
|
@@ -75,7 +87,7 @@ export async function api(method, path, opts = {}) {
|
|
|
75
87
|
method: method.toUpperCase(),
|
|
76
88
|
headers,
|
|
77
89
|
body,
|
|
78
|
-
signal: opts.signal
|
|
90
|
+
signal: opts.signal,
|
|
79
91
|
});
|
|
80
92
|
|
|
81
93
|
if (!res.ok) await parseError(res);
|
|
@@ -96,31 +108,30 @@ export async function api(method, path, opts = {}) {
|
|
|
96
108
|
// non-2xx status, so nothing above this catches them.
|
|
97
109
|
export function assertNoStreamError(evt) {
|
|
98
110
|
if (!evt) return;
|
|
99
|
-
const err = evt.error ?? (evt.type ===
|
|
111
|
+
const err = evt.error ?? (evt.type === "error" ? evt : null);
|
|
100
112
|
if (!err) return;
|
|
101
|
-
const message =
|
|
102
|
-
(typeof err === 'string' ? err : err.message) || JSON.stringify(err);
|
|
113
|
+
const message = (typeof err === "string" ? err : err.message) || JSON.stringify(err);
|
|
103
114
|
throw new APIError(`stream error: ${message}`);
|
|
104
115
|
}
|
|
105
116
|
|
|
106
117
|
export async function* sseStream(res) {
|
|
107
118
|
if (!res.body) return;
|
|
108
119
|
const reader = res.body.getReader();
|
|
109
|
-
const decoder = new TextDecoder(
|
|
110
|
-
let buffer =
|
|
120
|
+
const decoder = new TextDecoder("utf8");
|
|
121
|
+
let buffer = "";
|
|
111
122
|
while (true) {
|
|
112
123
|
const { value, done } = await reader.read();
|
|
113
124
|
if (done) break;
|
|
114
125
|
buffer += decoder.decode(value, { stream: true });
|
|
115
126
|
let idx;
|
|
116
|
-
while ((idx = buffer.indexOf(
|
|
117
|
-
const line = buffer.slice(0, idx).replace(/\r$/,
|
|
127
|
+
while ((idx = buffer.indexOf("\n")) !== -1) {
|
|
128
|
+
const line = buffer.slice(0, idx).replace(/\r$/, "");
|
|
118
129
|
buffer = buffer.slice(idx + 1);
|
|
119
130
|
if (!line) continue;
|
|
120
|
-
if (line.startsWith(
|
|
121
|
-
if (line.startsWith(
|
|
131
|
+
if (line.startsWith(":")) continue; // SSE comment / keepalive
|
|
132
|
+
if (line.startsWith("data:")) {
|
|
122
133
|
const data = line.slice(5).trim();
|
|
123
|
-
if (data ===
|
|
134
|
+
if (data === "[DONE]") return;
|
|
124
135
|
try {
|
|
125
136
|
yield JSON.parse(data);
|
|
126
137
|
} catch {
|
package/src/args.js
CHANGED
|
@@ -1,15 +1,15 @@
|
|
|
1
|
-
import { parseArgs as nodeParseArgs } from
|
|
2
|
-
import { stdin } from
|
|
3
|
-
import { isJsonMode } from
|
|
1
|
+
import { parseArgs as nodeParseArgs } from "node:util";
|
|
2
|
+
import { stdin } from "node:process";
|
|
3
|
+
import { isJsonMode } from "./output.js";
|
|
4
4
|
|
|
5
5
|
const GLOBAL_OPTIONS = {
|
|
6
|
-
key: { type:
|
|
7
|
-
|
|
8
|
-
referer: { type:
|
|
9
|
-
title: { type:
|
|
10
|
-
json: { type:
|
|
11
|
-
quiet: { type:
|
|
12
|
-
help: { type:
|
|
6
|
+
key: { type: "string", short: "k" },
|
|
7
|
+
"base-url": { type: "string" },
|
|
8
|
+
referer: { type: "string" },
|
|
9
|
+
title: { type: "string" },
|
|
10
|
+
json: { type: "boolean" },
|
|
11
|
+
quiet: { type: "boolean", short: "q" },
|
|
12
|
+
help: { type: "boolean", short: "h" },
|
|
13
13
|
};
|
|
14
14
|
|
|
15
15
|
export function parseArgs(argv, options = {}) {
|
|
@@ -18,7 +18,7 @@ export function parseArgs(argv, options = {}) {
|
|
|
18
18
|
args: argv,
|
|
19
19
|
options: merged,
|
|
20
20
|
allowPositionals: true,
|
|
21
|
-
strict: true
|
|
21
|
+
strict: true,
|
|
22
22
|
});
|
|
23
23
|
}
|
|
24
24
|
|
|
@@ -28,15 +28,15 @@ export function parseArgs(argv, options = {}) {
|
|
|
28
28
|
export function numberOption(value, flag) {
|
|
29
29
|
if (value === undefined) return undefined;
|
|
30
30
|
const n = Number(value);
|
|
31
|
-
if (String(value).trim() ===
|
|
31
|
+
if (String(value).trim() === "" || !Number.isFinite(n)) {
|
|
32
32
|
throw new Error(`Invalid number for ${flag}: "${value}"`);
|
|
33
33
|
}
|
|
34
34
|
return n;
|
|
35
35
|
}
|
|
36
36
|
|
|
37
37
|
export const PAGINATION_OPTIONS = {
|
|
38
|
-
offset: { type:
|
|
39
|
-
limit: { type:
|
|
38
|
+
offset: { type: "string" },
|
|
39
|
+
limit: { type: "string" },
|
|
40
40
|
};
|
|
41
41
|
|
|
42
42
|
export function paginationQuery(values) {
|
|
@@ -49,7 +49,7 @@ export function paginationQuery(values) {
|
|
|
49
49
|
export function authFromValues(values) {
|
|
50
50
|
const out = {};
|
|
51
51
|
if (values.key) out.key = values.key;
|
|
52
|
-
if (values[
|
|
52
|
+
if (values["base-url"]) out.baseUrl = values["base-url"];
|
|
53
53
|
if (values.referer) out.referer = values.referer;
|
|
54
54
|
if (values.title) out.title = values.title;
|
|
55
55
|
return out;
|
|
@@ -57,14 +57,13 @@ export function authFromValues(values) {
|
|
|
57
57
|
|
|
58
58
|
export async function readStdinIfPiped() {
|
|
59
59
|
if (stdin.isTTY) return null;
|
|
60
|
-
let data =
|
|
60
|
+
let data = "";
|
|
61
61
|
for await (const chunk of stdin) data += chunk;
|
|
62
62
|
return data.trim() || null;
|
|
63
63
|
}
|
|
64
64
|
|
|
65
65
|
export function shouldStream(values) {
|
|
66
66
|
return (
|
|
67
|
-
values.stream ||
|
|
68
|
-
(!values['no-stream'] && !values.raw && !isJsonMode() && process.stdout.isTTY)
|
|
67
|
+
values.stream || (!values["no-stream"] && !values.raw && !isJsonMode() && process.stdout.isTTY)
|
|
69
68
|
);
|
|
70
69
|
}
|
package/src/cli.js
CHANGED
|
@@ -1,26 +1,31 @@
|
|
|
1
|
-
import { setJsonMode, setQuiet, outln } from
|
|
2
|
-
import { loginCommand, logoutCommand } from
|
|
3
|
-
import { whoamiCommand, creditsCommand, activityCommand } from
|
|
4
|
-
import { modelsCommand, providersCommand } from
|
|
5
|
-
import { chatCommand, completeCommand } from
|
|
6
|
-
import { embedCommand } from
|
|
7
|
-
import { rerankCommand } from
|
|
8
|
-
import { speechCommand } from
|
|
9
|
-
import {
|
|
10
|
-
import {
|
|
11
|
-
import {
|
|
12
|
-
import {
|
|
13
|
-
import {
|
|
14
|
-
import {
|
|
15
|
-
import {
|
|
16
|
-
import {
|
|
17
|
-
import {
|
|
18
|
-
import {
|
|
19
|
-
import {
|
|
20
|
-
import {
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
1
|
+
import { setJsonMode, setQuiet, outln } from "./output.js";
|
|
2
|
+
import { loginCommand, logoutCommand } from "./commands/login.js";
|
|
3
|
+
import { whoamiCommand, creditsCommand, activityCommand } from "./commands/account.js";
|
|
4
|
+
import { modelsCommand, providersCommand } from "./commands/models.js";
|
|
5
|
+
import { chatCommand, completeCommand } from "./commands/chat.js";
|
|
6
|
+
import { embedCommand } from "./commands/embed.js";
|
|
7
|
+
import { rerankCommand } from "./commands/rerank.js";
|
|
8
|
+
import { speechCommand } from "./commands/speech.js";
|
|
9
|
+
import { transcribeCommand } from "./commands/transcribe.js";
|
|
10
|
+
import { videoCommand } from "./commands/video.js";
|
|
11
|
+
import { imageCommand } from "./commands/image.js";
|
|
12
|
+
import { messagesCommand, responsesCommand } from "./commands/messages.js";
|
|
13
|
+
import { generationCommand } from "./commands/generation.js";
|
|
14
|
+
import { keysCommand } from "./commands/keys.js";
|
|
15
|
+
import { byokCommand } from "./commands/byok.js";
|
|
16
|
+
import { filesCommand } from "./commands/files.js";
|
|
17
|
+
import { presetsCommand } from "./commands/presets.js";
|
|
18
|
+
import { requestCommand } from "./commands/request.js";
|
|
19
|
+
import { guardrailsCommand } from "./commands/guardrails.js";
|
|
20
|
+
import { workspacesCommand } from "./commands/workspaces.js";
|
|
21
|
+
import { orgCommand, zdrCommand, authCodeCommand } from "./commands/misc.js";
|
|
22
|
+
import { DEFAULT_BASE_URL } from "./config.js";
|
|
23
|
+
import { readFileSync } from "node:fs";
|
|
24
|
+
import { fileURLToPath } from "node:url";
|
|
25
|
+
import { dirname, join } from "node:path";
|
|
26
|
+
|
|
27
|
+
const pkgPath = join(dirname(fileURLToPath(import.meta.url)), "..", "package.json");
|
|
28
|
+
const VERSION = JSON.parse(readFileSync(pkgPath, "utf8")).version;
|
|
24
29
|
|
|
25
30
|
const COMMANDS = {
|
|
26
31
|
login: loginCommand,
|
|
@@ -36,17 +41,24 @@ const COMMANDS = {
|
|
|
36
41
|
rerank: rerankCommand,
|
|
37
42
|
speech: speechCommand,
|
|
38
43
|
tts: speechCommand,
|
|
44
|
+
transcribe: transcribeCommand,
|
|
45
|
+
stt: transcribeCommand,
|
|
39
46
|
video: videoCommand,
|
|
47
|
+
image: imageCommand,
|
|
48
|
+
images: imageCommand,
|
|
40
49
|
messages: messagesCommand,
|
|
41
50
|
responses: responsesCommand,
|
|
42
51
|
generation: generationCommand,
|
|
52
|
+
presets: presetsCommand,
|
|
43
53
|
keys: keysCommand,
|
|
54
|
+
byok: byokCommand,
|
|
55
|
+
files: filesCommand,
|
|
44
56
|
guardrails: guardrailsCommand,
|
|
45
57
|
workspaces: workspacesCommand,
|
|
46
58
|
org: orgCommand,
|
|
47
59
|
zdr: zdrCommand,
|
|
48
|
-
|
|
49
|
-
request: requestCommand
|
|
60
|
+
"auth-code": authCodeCommand,
|
|
61
|
+
request: requestCommand,
|
|
50
62
|
};
|
|
51
63
|
|
|
52
64
|
const HELP = `openrouter - CLI for the OpenRouter API
|
|
@@ -69,15 +81,19 @@ Inference:
|
|
|
69
81
|
embed [text...] Embeddings
|
|
70
82
|
rerank --query --doc Reranking
|
|
71
83
|
speech [text...] Text to speech (audio out)
|
|
84
|
+
transcribe <file> Speech to text (audio in). Alias: stt
|
|
72
85
|
video <sub> Video generation jobs (create / get / wait / download / models)
|
|
86
|
+
image <sub> Image generation (create / models / endpoints). Alias: images
|
|
73
87
|
|
|
74
88
|
Discovery:
|
|
75
|
-
models [list|show|endpoints|count|user] List models, full model
|
|
76
|
-
provider variants,
|
|
77
|
-
|
|
89
|
+
models [list|show|endpoints|parameters|count|user] List models, full model
|
|
90
|
+
detail, provider variants, param
|
|
91
|
+
analytics, count, or your set
|
|
78
92
|
embed models List embedding models
|
|
79
93
|
video models List video generation models
|
|
94
|
+
image models List image generation models
|
|
80
95
|
providers List providers (with HQ + datacenters)
|
|
96
|
+
presets <sub> list / get / versions (saved config profiles)
|
|
81
97
|
generation <id> [--content] Generation metadata or content
|
|
82
98
|
credits Remaining credits
|
|
83
99
|
activity Usage activity (management key)
|
|
@@ -85,8 +101,10 @@ Discovery:
|
|
|
85
101
|
|
|
86
102
|
Management (require a management key):
|
|
87
103
|
keys <sub> list / get / create / update / delete
|
|
104
|
+
byok <sub> list / get / create / update / delete (provider integrations)
|
|
88
105
|
guardrails <sub> list / get / create / update / delete / assignments
|
|
89
106
|
workspaces <sub> list / get / create / update / delete / add-members / remove-members
|
|
107
|
+
files <sub> list / get / upload / download / delete
|
|
90
108
|
org members List organization members
|
|
91
109
|
auth-code Mint a PKCE authorization code so a user can claim a key
|
|
92
110
|
request <METHOD> <path> Raw authenticated request to any endpoint
|
|
@@ -112,7 +130,7 @@ Environment:
|
|
|
112
130
|
|
|
113
131
|
Examples:
|
|
114
132
|
openrouter login
|
|
115
|
-
openrouter chat "Hello, world" -m anthropic/claude-
|
|
133
|
+
openrouter chat "Hello, world" -m anthropic/claude-sonnet-4.5
|
|
116
134
|
echo "summarize this" | openrouter chat -m openai/gpt-4o-mini --no-stream
|
|
117
135
|
openrouter models --filter claude --json
|
|
118
136
|
openrouter embed -m openai/text-embedding-3-small "hello" "world"
|
|
@@ -131,10 +149,10 @@ function preParseGlobals(argv) {
|
|
|
131
149
|
let version = false;
|
|
132
150
|
for (let i = 0; i < argv.length; i++) {
|
|
133
151
|
const a = argv[i];
|
|
134
|
-
if (a ===
|
|
135
|
-
else if (a ===
|
|
136
|
-
else if (a ===
|
|
137
|
-
else if (a ===
|
|
152
|
+
if (a === "--json") json = true;
|
|
153
|
+
else if (a === "-q" || a === "--quiet") quiet = true;
|
|
154
|
+
else if (a === "-V" || a === "--version") version = true;
|
|
155
|
+
else if (a === "-h" || a === "--help") {
|
|
138
156
|
help = true;
|
|
139
157
|
out.push(a); // forward so subcommand help can also pick it up
|
|
140
158
|
} else out.push(a);
|
|
@@ -166,7 +184,7 @@ export async function run(argv) {
|
|
|
166
184
|
return safeExit(1);
|
|
167
185
|
}
|
|
168
186
|
const code = await handler(rest);
|
|
169
|
-
if (typeof code ===
|
|
187
|
+
if (typeof code === "number") await safeExit(code);
|
|
170
188
|
}
|
|
171
189
|
|
|
172
190
|
function safeExit(code) {
|
|
@@ -183,7 +201,7 @@ function safeExit(code) {
|
|
|
183
201
|
for (const stream of [process.stdout, process.stderr]) {
|
|
184
202
|
if (stream && stream.writableLength > 0) {
|
|
185
203
|
pending++;
|
|
186
|
-
stream.write(
|
|
204
|
+
stream.write("", done);
|
|
187
205
|
}
|
|
188
206
|
}
|
|
189
207
|
if (pending === 0) {
|
package/src/commands/account.js
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
import { parseArgs, authFromValues } from
|
|
2
|
-
import { api, APIError } from
|
|
3
|
-
import { resolveAuth } from
|
|
4
|
-
import { c, isJsonMode, outln, printJSON, printResult, table } from
|
|
1
|
+
import { parseArgs, authFromValues } from "../args.js";
|
|
2
|
+
import { api, APIError } from "../api.js";
|
|
3
|
+
import { resolveAuth } from "../config.js";
|
|
4
|
+
import { c, isJsonMode, outln, printJSON, printResult, table } from "../output.js";
|
|
5
5
|
|
|
6
6
|
async function fetchKey(authOpts, requiresManagement = false) {
|
|
7
7
|
try {
|
|
8
|
-
return await api(
|
|
8
|
+
return await api("GET", "/key", { auth: authOpts, requiresManagement });
|
|
9
9
|
} catch (err) {
|
|
10
10
|
if (err instanceof APIError) return { error: err.message };
|
|
11
11
|
throw err;
|
|
@@ -15,41 +15,43 @@ async function fetchKey(authOpts, requiresManagement = false) {
|
|
|
15
15
|
function renderKey(label, data) {
|
|
16
16
|
outln(c.bold(label));
|
|
17
17
|
if (!data) {
|
|
18
|
-
outln(c.dim(
|
|
18
|
+
outln(c.dim(" (not configured)"));
|
|
19
19
|
return;
|
|
20
20
|
}
|
|
21
21
|
if (data.error) {
|
|
22
|
-
outln(
|
|
22
|
+
outln(" " + c.red(data.error));
|
|
23
23
|
return;
|
|
24
24
|
}
|
|
25
25
|
const d = data.data || data;
|
|
26
|
-
outln(` label: ${d.label ??
|
|
26
|
+
outln(` label: ${d.label ?? ""}`);
|
|
27
27
|
outln(` is_management_key: ${d.is_management_key ?? false}`);
|
|
28
28
|
outln(` is_free_tier: ${d.is_free_tier}`);
|
|
29
|
-
outln(` limit: ${d.limit ??
|
|
30
|
-
outln(` limit_remaining: ${d.limit_remaining ??
|
|
31
|
-
outln(` limit_reset: ${d.limit_reset ??
|
|
32
|
-
outln(` usage (total): ${d.usage ??
|
|
29
|
+
outln(` limit: ${d.limit ?? "unlimited"}`);
|
|
30
|
+
outln(` limit_remaining: ${d.limit_remaining ?? "-"}`);
|
|
31
|
+
outln(` limit_reset: ${d.limit_reset ?? "-"}`);
|
|
32
|
+
outln(` usage (total): ${d.usage ?? "-"}`);
|
|
33
33
|
if (d.usage_daily != null) outln(` usage_daily: ${d.usage_daily}`);
|
|
34
34
|
if (d.usage_weekly != null) outln(` usage_weekly: ${d.usage_weekly}`);
|
|
35
35
|
if (d.usage_monthly != null) outln(` usage_monthly: ${d.usage_monthly}`);
|
|
36
36
|
if (d.byok_usage != null) outln(` byok_usage: ${d.byok_usage}`);
|
|
37
|
-
outln(` expires_at: ${d.expires_at ??
|
|
37
|
+
outln(` expires_at: ${d.expires_at ?? "-"}`);
|
|
38
38
|
if (d.rate_limit) {
|
|
39
|
-
outln(
|
|
39
|
+
outln(
|
|
40
|
+
` rate_limit: ${d.rate_limit.requests ?? "?"} req / ${d.rate_limit.interval ?? "?"}`,
|
|
41
|
+
);
|
|
40
42
|
}
|
|
41
43
|
}
|
|
42
44
|
|
|
43
45
|
export async function whoamiCommand(argv) {
|
|
44
46
|
const { values } = parseArgs(argv, {
|
|
45
|
-
management: { type:
|
|
47
|
+
management: { type: "boolean" },
|
|
46
48
|
});
|
|
47
49
|
if (values.help) {
|
|
48
50
|
process.stdout.write(
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
51
|
+
"Usage: openrouter whoami [--management]\n\n" +
|
|
52
|
+
"Show the API key(s) currently configured. By default shows both the user\n" +
|
|
53
|
+
"key and the management key (if set). With --management, shows only the\n" +
|
|
54
|
+
"management key.\n",
|
|
53
55
|
);
|
|
54
56
|
return 0;
|
|
55
57
|
}
|
|
@@ -59,17 +61,17 @@ export async function whoamiCommand(argv) {
|
|
|
59
61
|
|
|
60
62
|
// If --key is supplied, query just that key.
|
|
61
63
|
if (authOpts.key) {
|
|
62
|
-
const data = await api(
|
|
63
|
-
printResult(data, () => renderKey(
|
|
64
|
+
const data = await api("GET", "/key", { auth: authOpts });
|
|
65
|
+
printResult(data, () => renderKey("API key (--key override)", data));
|
|
64
66
|
return 0;
|
|
65
67
|
}
|
|
66
68
|
|
|
67
69
|
if (values.management) {
|
|
68
70
|
const data = resolved.hasManagementKey
|
|
69
|
-
? await api(
|
|
71
|
+
? await api("GET", "/key", { auth: authOpts, requiresManagement: true })
|
|
70
72
|
: null;
|
|
71
73
|
if (isJsonMode()) printJSON({ management: data });
|
|
72
|
-
else renderKey(
|
|
74
|
+
else renderKey("Management key", data);
|
|
73
75
|
return 0;
|
|
74
76
|
}
|
|
75
77
|
|
|
@@ -85,22 +87,22 @@ export async function whoamiCommand(argv) {
|
|
|
85
87
|
printJSON({ user: userData, management: mgmtData });
|
|
86
88
|
return code;
|
|
87
89
|
}
|
|
88
|
-
renderKey(
|
|
89
|
-
outln(
|
|
90
|
-
renderKey(
|
|
90
|
+
renderKey("User key", userData);
|
|
91
|
+
outln("");
|
|
92
|
+
renderKey("Management key", mgmtData);
|
|
91
93
|
return code;
|
|
92
94
|
}
|
|
93
95
|
|
|
94
96
|
export async function creditsCommand(argv) {
|
|
95
97
|
const { values } = parseArgs(argv, {});
|
|
96
98
|
if (values.help) {
|
|
97
|
-
process.stdout.write(
|
|
99
|
+
process.stdout.write("Usage: openrouter credits\n\nShow remaining credits.\n");
|
|
98
100
|
return 0;
|
|
99
101
|
}
|
|
100
|
-
const data = await api(
|
|
102
|
+
const data = await api("GET", "/credits", { auth: authFromValues(values) });
|
|
101
103
|
printResult(data, () => {
|
|
102
104
|
const d = data.data || data;
|
|
103
|
-
outln(c.bold(
|
|
105
|
+
outln(c.bold("Credits"));
|
|
104
106
|
for (const [k, v] of Object.entries(d)) outln(` ${k}: ${v}`);
|
|
105
107
|
});
|
|
106
108
|
return 0;
|
|
@@ -108,37 +110,37 @@ export async function creditsCommand(argv) {
|
|
|
108
110
|
|
|
109
111
|
export async function activityCommand(argv) {
|
|
110
112
|
const { values } = parseArgs(argv, {
|
|
111
|
-
date: { type:
|
|
112
|
-
|
|
113
|
-
|
|
113
|
+
date: { type: "string" },
|
|
114
|
+
"api-key-hash": { type: "string" },
|
|
115
|
+
"user-id": { type: "string" },
|
|
114
116
|
});
|
|
115
117
|
if (values.help) {
|
|
116
118
|
process.stdout.write(
|
|
117
|
-
`Usage: openrouter activity [options]\n\nGet usage activity grouped by endpoint.\n\nOptions:\n --date <YYYY-MM-DD> UTC date in last 30 days\n --api-key-hash <hash> Filter by API key SHA-256 hash\n --user-id <id> Filter by org member user ID\n
|
|
119
|
+
`Usage: openrouter activity [options]\n\nGet usage activity grouped by endpoint.\n\nOptions:\n --date <YYYY-MM-DD> UTC date in last 30 days\n --api-key-hash <hash> Filter by API key SHA-256 hash\n --user-id <id> Filter by org member user ID\n`,
|
|
118
120
|
);
|
|
119
121
|
return 0;
|
|
120
122
|
}
|
|
121
123
|
const query = {};
|
|
122
124
|
if (values.date) query.date = values.date;
|
|
123
|
-
if (values[
|
|
124
|
-
if (values[
|
|
125
|
-
const data = await api(
|
|
125
|
+
if (values["api-key-hash"]) query.api_key_hash = values["api-key-hash"];
|
|
126
|
+
if (values["user-id"]) query.user_id = values["user-id"];
|
|
127
|
+
const data = await api("GET", "/activity", {
|
|
126
128
|
auth: authFromValues(values),
|
|
127
129
|
requiresManagement: true,
|
|
128
|
-
query
|
|
130
|
+
query,
|
|
129
131
|
});
|
|
130
132
|
printResult(data, () => {
|
|
131
133
|
const rows = data.data || [];
|
|
132
134
|
if (!rows.length) {
|
|
133
|
-
outln(c.dim(
|
|
135
|
+
outln(c.dim("(no activity)"));
|
|
134
136
|
return;
|
|
135
137
|
}
|
|
136
138
|
table(rows, [
|
|
137
|
-
{ label:
|
|
138
|
-
{ label:
|
|
139
|
-
{ label:
|
|
140
|
-
{ label:
|
|
141
|
-
{ label:
|
|
139
|
+
{ label: "date", value: (r) => r.date || r.day || "" },
|
|
140
|
+
{ label: "model", value: (r) => r.model || r.model_permaslug || "" },
|
|
141
|
+
{ label: "requests", value: (r) => r.requests ?? r.request_count ?? "" },
|
|
142
|
+
{ label: "tokens", value: (r) => r.tokens ?? r.total_tokens ?? "" },
|
|
143
|
+
{ label: "cost", value: (r) => r.usage ?? r.cost ?? "" },
|
|
142
144
|
]);
|
|
143
145
|
});
|
|
144
146
|
return 0;
|