@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
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { parseArgs, authFromValues, PAGINATION_OPTIONS, paginationQuery } from
|
|
2
|
-
import { api } from
|
|
3
|
-
import { outln, printResult, table } from
|
|
1
|
+
import { parseArgs, authFromValues, PAGINATION_OPTIONS, paginationQuery } from "../args.js";
|
|
2
|
+
import { api } from "../api.js";
|
|
3
|
+
import { outln, printResult, table } from "../output.js";
|
|
4
4
|
|
|
5
5
|
const HELP = `Usage: openrouter workspaces <subcommand> [options]
|
|
6
6
|
|
|
@@ -25,118 +25,122 @@ create / update options:
|
|
|
25
25
|
`;
|
|
26
26
|
|
|
27
27
|
const COMMON = {
|
|
28
|
-
slug: { type:
|
|
29
|
-
description: { type:
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
28
|
+
slug: { type: "string" },
|
|
29
|
+
description: { type: "string" },
|
|
30
|
+
"default-text-model": { type: "string" },
|
|
31
|
+
"default-image-model": { type: "string" },
|
|
32
|
+
"default-provider-sort": { type: "string" },
|
|
33
33
|
};
|
|
34
34
|
|
|
35
35
|
function buildBody(values) {
|
|
36
36
|
const body = {};
|
|
37
37
|
if (values.slug !== undefined) body.slug = values.slug;
|
|
38
38
|
if (values.description !== undefined) body.description = values.description;
|
|
39
|
-
if (values[
|
|
40
|
-
|
|
41
|
-
if (values[
|
|
39
|
+
if (values["default-text-model"] !== undefined)
|
|
40
|
+
body.default_text_model = values["default-text-model"];
|
|
41
|
+
if (values["default-image-model"] !== undefined)
|
|
42
|
+
body.default_image_model = values["default-image-model"];
|
|
43
|
+
if (values["default-provider-sort"] !== undefined)
|
|
44
|
+
body.default_provider_sort = values["default-provider-sort"];
|
|
42
45
|
return body;
|
|
43
46
|
}
|
|
44
47
|
|
|
45
48
|
export async function workspacesCommand(argv) {
|
|
46
49
|
const sub = argv[0];
|
|
47
50
|
const rest = argv.slice(1);
|
|
48
|
-
if (!sub || sub ===
|
|
51
|
+
if (!sub || sub === "help" || sub === "-h" || sub === "--help") {
|
|
49
52
|
process.stdout.write(HELP);
|
|
50
53
|
return sub ? 0 : 1;
|
|
51
54
|
}
|
|
52
55
|
|
|
53
|
-
if (rest.includes(
|
|
56
|
+
if (rest.includes("-h") || rest.includes("--help")) {
|
|
54
57
|
process.stdout.write(HELP);
|
|
55
58
|
return 0;
|
|
56
59
|
}
|
|
57
60
|
|
|
58
|
-
if (sub ===
|
|
61
|
+
if (sub === "list") {
|
|
59
62
|
const { values } = parseArgs(rest, PAGINATION_OPTIONS);
|
|
60
63
|
const query = paginationQuery(values);
|
|
61
|
-
const data = await api(
|
|
64
|
+
const data = await api("GET", "/workspaces", {
|
|
62
65
|
auth: authFromValues(values),
|
|
63
66
|
requiresManagement: true,
|
|
64
|
-
query
|
|
67
|
+
query,
|
|
65
68
|
});
|
|
66
69
|
printResult(data, () => {
|
|
67
70
|
const rows = data.data || [];
|
|
68
71
|
table(rows, [
|
|
69
|
-
{ label:
|
|
70
|
-
{ label:
|
|
71
|
-
{ label:
|
|
72
|
-
{ label:
|
|
72
|
+
{ label: "slug", value: (w) => w.slug || "" },
|
|
73
|
+
{ label: "name", value: (w) => w.name || "" },
|
|
74
|
+
{ label: "id", value: (w) => w.id || "" },
|
|
75
|
+
{ label: "is_default", value: (w) => (w.is_default ? "yes" : "") },
|
|
73
76
|
]);
|
|
74
77
|
});
|
|
75
78
|
return 0;
|
|
76
79
|
}
|
|
77
80
|
|
|
78
|
-
if (sub ===
|
|
81
|
+
if (sub === "get") {
|
|
79
82
|
const { values, positionals } = parseArgs(rest, {});
|
|
80
|
-
if (!positionals[0]) throw new Error(
|
|
81
|
-
const data = await api(
|
|
83
|
+
if (!positionals[0]) throw new Error("id|slug required");
|
|
84
|
+
const data = await api("GET", `/workspaces/${encodeURIComponent(positionals[0])}`, {
|
|
82
85
|
auth: authFromValues(values),
|
|
83
|
-
requiresManagement: true
|
|
86
|
+
requiresManagement: true,
|
|
84
87
|
});
|
|
85
88
|
printResult(data);
|
|
86
89
|
return 0;
|
|
87
90
|
}
|
|
88
91
|
|
|
89
|
-
if (sub ===
|
|
92
|
+
if (sub === "create") {
|
|
90
93
|
const { values, positionals } = parseArgs(rest, COMMON);
|
|
91
94
|
const name = positionals[0];
|
|
92
|
-
if (!name) throw new Error(
|
|
95
|
+
if (!name) throw new Error("name required");
|
|
93
96
|
const body = { name, ...buildBody(values) };
|
|
94
|
-
const data = await api(
|
|
97
|
+
const data = await api("POST", "/workspaces", {
|
|
95
98
|
auth: authFromValues(values),
|
|
96
99
|
requiresManagement: true,
|
|
97
|
-
body
|
|
100
|
+
body,
|
|
98
101
|
});
|
|
99
102
|
printResult(data);
|
|
100
103
|
return 0;
|
|
101
104
|
}
|
|
102
105
|
|
|
103
|
-
if (sub ===
|
|
104
|
-
const { values, positionals } = parseArgs(rest, { ...COMMON, name: { type:
|
|
105
|
-
if (!positionals[0]) throw new Error(
|
|
106
|
+
if (sub === "update") {
|
|
107
|
+
const { values, positionals } = parseArgs(rest, { ...COMMON, name: { type: "string" } });
|
|
108
|
+
if (!positionals[0]) throw new Error("id|slug required");
|
|
106
109
|
const body = buildBody(values);
|
|
107
110
|
if (values.name) body.name = values.name;
|
|
108
|
-
const data = await api(
|
|
111
|
+
const data = await api("PATCH", `/workspaces/${encodeURIComponent(positionals[0])}`, {
|
|
109
112
|
auth: authFromValues(values),
|
|
110
113
|
requiresManagement: true,
|
|
111
|
-
body
|
|
114
|
+
body,
|
|
112
115
|
});
|
|
113
116
|
printResult(data);
|
|
114
117
|
return 0;
|
|
115
118
|
}
|
|
116
119
|
|
|
117
|
-
if (sub ===
|
|
120
|
+
if (sub === "delete") {
|
|
118
121
|
const { values, positionals } = parseArgs(rest, {});
|
|
119
|
-
if (!positionals[0]) throw new Error(
|
|
120
|
-
const data = await api(
|
|
122
|
+
if (!positionals[0]) throw new Error("id|slug required");
|
|
123
|
+
const data = await api("DELETE", `/workspaces/${encodeURIComponent(positionals[0])}`, {
|
|
121
124
|
auth: authFromValues(values),
|
|
122
|
-
requiresManagement: true
|
|
125
|
+
requiresManagement: true,
|
|
123
126
|
});
|
|
124
|
-
printResult(data, () => outln(
|
|
127
|
+
printResult(data, () => outln("deleted"));
|
|
125
128
|
return 0;
|
|
126
129
|
}
|
|
127
130
|
|
|
128
|
-
if (sub ===
|
|
131
|
+
if (sub === "add-members" || sub === "remove-members") {
|
|
129
132
|
const { values, positionals } = parseArgs(rest, {});
|
|
130
|
-
if (positionals.length < 2) throw new Error(
|
|
133
|
+
if (positionals.length < 2) throw new Error("Usage: <id|slug> <userId>...");
|
|
131
134
|
const id = positionals[0];
|
|
132
135
|
const userIds = positionals.slice(1);
|
|
133
|
-
const path =
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
136
|
+
const path =
|
|
137
|
+
sub === "add-members"
|
|
138
|
+
? `/workspaces/${encodeURIComponent(id)}/members/add`
|
|
139
|
+
: `/workspaces/${encodeURIComponent(id)}/members/remove`;
|
|
140
|
+
const data = await api("POST", path, {
|
|
137
141
|
auth: authFromValues(values),
|
|
138
142
|
requiresManagement: true,
|
|
139
|
-
body: { user_ids: userIds }
|
|
143
|
+
body: { user_ids: userIds },
|
|
140
144
|
});
|
|
141
145
|
printResult(data);
|
|
142
146
|
return 0;
|
package/src/config.js
CHANGED
|
@@ -1,37 +1,35 @@
|
|
|
1
|
-
import { promises as fs } from
|
|
2
|
-
import { homedir } from
|
|
3
|
-
import { join } from
|
|
1
|
+
import { promises as fs } from "node:fs";
|
|
2
|
+
import { homedir } from "node:os";
|
|
3
|
+
import { join } from "node:path";
|
|
4
4
|
|
|
5
|
-
const XDG = process.env.XDG_CONFIG_HOME || join(homedir(),
|
|
6
|
-
const CONFIG_DIR = join(XDG,
|
|
7
|
-
const CONFIG_FILE = join(CONFIG_DIR,
|
|
5
|
+
const XDG = process.env.XDG_CONFIG_HOME || join(homedir(), ".config");
|
|
6
|
+
const CONFIG_DIR = join(XDG, "openrouter");
|
|
7
|
+
const CONFIG_FILE = join(CONFIG_DIR, "config.json");
|
|
8
8
|
|
|
9
|
-
export const DEFAULT_BASE_URL =
|
|
10
|
-
export const DEFAULT_AUTH_URL =
|
|
11
|
-
export const PROVISIONING_KEYS_URL =
|
|
12
|
-
'https://openrouter.ai/settings/provisioning-keys';
|
|
9
|
+
export const DEFAULT_BASE_URL = "https://openrouter.ai/api/v1";
|
|
10
|
+
export const DEFAULT_AUTH_URL = "https://openrouter.ai/auth";
|
|
11
|
+
export const PROVISIONING_KEYS_URL = "https://openrouter.ai/settings/provisioning-keys";
|
|
13
12
|
|
|
14
13
|
export async function loadConfig() {
|
|
15
14
|
try {
|
|
16
|
-
const raw = await fs.readFile(CONFIG_FILE,
|
|
15
|
+
const raw = await fs.readFile(CONFIG_FILE, "utf8");
|
|
17
16
|
return JSON.parse(raw);
|
|
18
17
|
} catch (err) {
|
|
19
|
-
if (err.code ===
|
|
18
|
+
if (err.code === "ENOENT") return {};
|
|
20
19
|
throw err;
|
|
21
20
|
}
|
|
22
21
|
}
|
|
23
22
|
|
|
24
23
|
export async function saveConfig(cfg) {
|
|
25
24
|
await fs.mkdir(CONFIG_DIR, { recursive: true, mode: 0o700 });
|
|
26
|
-
const tmp = CONFIG_FILE +
|
|
25
|
+
const tmp = CONFIG_FILE + ".tmp";
|
|
27
26
|
await fs.writeFile(tmp, JSON.stringify(cfg, null, 2), { mode: 0o600 });
|
|
28
27
|
await fs.rename(tmp, CONFIG_FILE);
|
|
29
28
|
}
|
|
30
29
|
|
|
31
30
|
export async function resolveAuth(opts = {}) {
|
|
32
31
|
const cfg = await loadConfig();
|
|
33
|
-
const userKey =
|
|
34
|
-
process.env.OPENROUTER_API_KEY || process.env.OPENROUTER_KEY || cfg.apiKey;
|
|
32
|
+
const userKey = process.env.OPENROUTER_API_KEY || process.env.OPENROUTER_KEY || cfg.apiKey;
|
|
35
33
|
const mgmtKey = process.env.OPENROUTER_MANAGEMENT_KEY || cfg.managementKey;
|
|
36
34
|
|
|
37
35
|
// If the caller explicitly passed --key, that always wins.
|
|
@@ -48,23 +46,19 @@ export async function resolveAuth(opts = {}) {
|
|
|
48
46
|
}
|
|
49
47
|
|
|
50
48
|
const baseUrl =
|
|
51
|
-
opts.baseUrl ||
|
|
52
|
-
process.env.OPENROUTER_BASE_URL ||
|
|
53
|
-
cfg.baseUrl ||
|
|
54
|
-
DEFAULT_BASE_URL;
|
|
49
|
+
opts.baseUrl || process.env.OPENROUTER_BASE_URL || cfg.baseUrl || DEFAULT_BASE_URL;
|
|
55
50
|
const referer =
|
|
56
51
|
opts.referer ||
|
|
57
52
|
process.env.OPENROUTER_REFERER ||
|
|
58
53
|
cfg.referer ||
|
|
59
|
-
|
|
60
|
-
const title =
|
|
61
|
-
opts.title || process.env.OPENROUTER_TITLE || cfg.title || 'openrouter-cli';
|
|
54
|
+
"https://github.com/aaronjmars/openroutercli";
|
|
55
|
+
const title = opts.title || process.env.OPENROUTER_TITLE || cfg.title || "openrouter-cli";
|
|
62
56
|
return {
|
|
63
57
|
apiKey,
|
|
64
58
|
baseUrl,
|
|
65
59
|
referer,
|
|
66
60
|
title,
|
|
67
61
|
hasManagementKey: !!mgmtKey,
|
|
68
|
-
hasUserKey: !!userKey
|
|
62
|
+
hasUserKey: !!userKey,
|
|
69
63
|
};
|
|
70
64
|
}
|
package/src/output.js
CHANGED
|
@@ -1,20 +1,16 @@
|
|
|
1
|
-
import { writeFile } from
|
|
1
|
+
import { writeFile } from "node:fs/promises";
|
|
2
2
|
|
|
3
3
|
const isTTY = process.stdout.isTTY;
|
|
4
|
-
const noColor =
|
|
5
|
-
process.env.NO_COLOR != null ||
|
|
6
|
-
process.env.OPENROUTER_NO_COLOR != null ||
|
|
7
|
-
!isTTY;
|
|
4
|
+
const noColor = process.env.NO_COLOR != null || process.env.OPENROUTER_NO_COLOR != null || !isTTY;
|
|
8
5
|
|
|
9
|
-
const wrap = (open, close) => (s) =>
|
|
10
|
-
noColor ? String(s) : `\x1b[${open}m${s}\x1b[${close}m`;
|
|
6
|
+
const wrap = (open, close) => (s) => (noColor ? String(s) : `\x1b[${open}m${s}\x1b[${close}m`);
|
|
11
7
|
|
|
12
8
|
export const c = {
|
|
13
9
|
bold: wrap(1, 22),
|
|
14
10
|
dim: wrap(2, 22),
|
|
15
11
|
red: wrap(31, 39),
|
|
16
12
|
green: wrap(32, 39),
|
|
17
|
-
cyan: wrap(36, 39)
|
|
13
|
+
cyan: wrap(36, 39),
|
|
18
14
|
};
|
|
19
15
|
|
|
20
16
|
let JSON_MODE = false;
|
|
@@ -34,27 +30,27 @@ export function out(text) {
|
|
|
34
30
|
process.stdout.write(text);
|
|
35
31
|
}
|
|
36
32
|
|
|
37
|
-
export function outln(text =
|
|
38
|
-
process.stdout.write(text +
|
|
33
|
+
export function outln(text = "") {
|
|
34
|
+
process.stdout.write(text + "\n");
|
|
39
35
|
}
|
|
40
36
|
|
|
41
37
|
export function info(text) {
|
|
42
38
|
if (QUIET || JSON_MODE) return;
|
|
43
|
-
process.stderr.write(c.dim(text) +
|
|
39
|
+
process.stderr.write(c.dim(text) + "\n");
|
|
44
40
|
}
|
|
45
41
|
|
|
46
42
|
export function printJSON(data) {
|
|
47
|
-
process.stdout.write(JSON.stringify(data, null, 2) +
|
|
43
|
+
process.stdout.write(JSON.stringify(data, null, 2) + "\n");
|
|
48
44
|
}
|
|
49
45
|
|
|
50
46
|
// OpenRouter prices are per-token; tables show them per million.
|
|
51
47
|
export function pricePerMillion(x) {
|
|
52
|
-
return x == null ?
|
|
48
|
+
return x == null ? "-" : (Number(x) * 1e6).toFixed(2);
|
|
53
49
|
}
|
|
54
50
|
|
|
55
51
|
export async function writeBinaryOutput(bytes, out) {
|
|
56
52
|
const buf = Buffer.from(bytes);
|
|
57
|
-
if (out ===
|
|
53
|
+
if (out === "-") {
|
|
58
54
|
process.stdout.write(buf);
|
|
59
55
|
return;
|
|
60
56
|
}
|
|
@@ -74,21 +70,12 @@ export function printResult(data, formatter) {
|
|
|
74
70
|
export function table(rows, columns) {
|
|
75
71
|
if (rows.length === 0) return;
|
|
76
72
|
const widths = columns.map((col) =>
|
|
77
|
-
Math.max(
|
|
78
|
-
col.label.length,
|
|
79
|
-
...rows.map((r) => String(col.value(r) ?? '').length)
|
|
80
|
-
)
|
|
73
|
+
Math.max(col.label.length, ...rows.map((r) => String(col.value(r) ?? "").length)),
|
|
81
74
|
);
|
|
82
|
-
const header = columns
|
|
83
|
-
.map((col, i) => c.bold(col.label.padEnd(widths[i])))
|
|
84
|
-
.join(' ');
|
|
75
|
+
const header = columns.map((col, i) => c.bold(col.label.padEnd(widths[i]))).join(" ");
|
|
85
76
|
outln(header);
|
|
86
|
-
outln(columns.map((_, i) => c.dim(
|
|
77
|
+
outln(columns.map((_, i) => c.dim("-".repeat(widths[i]))).join(" "));
|
|
87
78
|
for (const row of rows) {
|
|
88
|
-
outln(
|
|
89
|
-
columns
|
|
90
|
-
.map((col, i) => String(col.value(row) ?? '').padEnd(widths[i]))
|
|
91
|
-
.join(' ')
|
|
92
|
-
);
|
|
79
|
+
outln(columns.map((col, i) => String(col.value(row) ?? "").padEnd(widths[i])).join(" "));
|
|
93
80
|
}
|
|
94
81
|
}
|