@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.
@@ -0,0 +1,153 @@
1
+ import { parseArgs, authFromValues } from "../args.js";
2
+ import { api } from "../api.js";
3
+ import { outln, printResult, table } from "../output.js";
4
+
5
+ const HELP = `Usage: openrouter byok <subcommand> [options]
6
+
7
+ Manage BYOK (bring-your-own-key) provider integrations (requires a provisioning/management key).
8
+
9
+ Subcommands:
10
+ list List integrations
11
+ get <id> Get a single integration
12
+ create [options] Create an integration
13
+ update <id> [options] Update an integration
14
+ delete <id> Delete an integration
15
+
16
+ create options:
17
+ --provider <slug> Provider slug (required)
18
+ --api-key <key> Provider secret key (required)
19
+ --name <text>
20
+ --allowed-models <csv>
21
+ --allowed-hashes <csv>
22
+ --allowed-users <csv>
23
+ --disabled / --enabled
24
+ --fallback / --no-fallback
25
+
26
+ update options:
27
+ --provider <slug>
28
+ --api-key <key>
29
+ --name <text>
30
+ --allowed-models <csv>
31
+ --allowed-hashes <csv>
32
+ --allowed-users <csv>
33
+ --disabled / --enabled
34
+ --fallback / --no-fallback
35
+ `;
36
+
37
+ const WRITE_OPTIONS = {
38
+ provider: { type: "string" },
39
+ "api-key": { type: "string" },
40
+ name: { type: "string" },
41
+ "allowed-models": { type: "string" },
42
+ "allowed-hashes": { type: "string" },
43
+ "allowed-users": { type: "string" },
44
+ disabled: { type: "boolean" },
45
+ enabled: { type: "boolean" },
46
+ fallback: { type: "boolean" },
47
+ "no-fallback": { type: "boolean" },
48
+ };
49
+
50
+ function csvOption(value) {
51
+ return value.split(",").map((s) => s.trim());
52
+ }
53
+
54
+ export async function byokCommand(argv) {
55
+ const sub = argv[0];
56
+ const rest = argv.slice(1);
57
+ if (!sub || sub === "-h" || sub === "--help" || sub === "help") {
58
+ process.stdout.write(HELP);
59
+ return sub ? 0 : 1;
60
+ }
61
+
62
+ if (rest.includes("-h") || rest.includes("--help")) {
63
+ process.stdout.write(HELP);
64
+ return 0;
65
+ }
66
+
67
+ if (sub === "list") {
68
+ const { values } = parseArgs(rest, {});
69
+ const data = await api("GET", "/byok", {
70
+ auth: authFromValues(values),
71
+ requiresManagement: true,
72
+ });
73
+ printResult(data, () => {
74
+ const rows = data.data || [];
75
+ table(rows, [
76
+ { label: "id", value: (b) => b.id || "" },
77
+ { label: "provider", value: (b) => b.provider || "" },
78
+ { label: "name", value: (b) => b.name || "" },
79
+ { label: "disabled", value: (b) => (b.disabled ? "yes" : "") },
80
+ ]);
81
+ });
82
+ return 0;
83
+ }
84
+
85
+ if (sub === "get") {
86
+ const { values, positionals } = parseArgs(rest, {});
87
+ if (!positionals[0]) throw new Error("id required");
88
+ const data = await api("GET", `/byok/${encodeURIComponent(positionals[0])}`, {
89
+ auth: authFromValues(values),
90
+ requiresManagement: true,
91
+ });
92
+ printResult(data);
93
+ return 0;
94
+ }
95
+
96
+ if (sub === "create") {
97
+ const { values } = parseArgs(rest, WRITE_OPTIONS);
98
+ if (!values.provider) throw new Error("--provider required");
99
+ if (!values["api-key"]) throw new Error("--api-key required");
100
+ const body = { provider: values.provider, key: values["api-key"] };
101
+ if (values.name) body.name = values.name;
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;
109
+ const data = await api("POST", "/byok", {
110
+ auth: authFromValues(values),
111
+ requiresManagement: true,
112
+ body,
113
+ });
114
+ printResult(data);
115
+ return 0;
116
+ }
117
+
118
+ if (sub === "update") {
119
+ const { values, positionals } = parseArgs(rest, WRITE_OPTIONS);
120
+ if (!positionals[0]) throw new Error("id required");
121
+ const body = {};
122
+ if (values.provider) body.provider = values.provider;
123
+ if (values["api-key"]) body.key = values["api-key"];
124
+ if (values.name) body.name = values.name;
125
+ if (values["allowed-models"]) body.allowed_models = csvOption(values["allowed-models"]);
126
+ if (values["allowed-hashes"]) body.allowed_api_key_hashes = csvOption(values["allowed-hashes"]);
127
+ if (values["allowed-users"]) body.allowed_user_ids = csvOption(values["allowed-users"]);
128
+ if (values.disabled) body.disabled = true;
129
+ if (values.enabled) body.disabled = false;
130
+ if (values.fallback) body.is_fallback = true;
131
+ if (values["no-fallback"]) body.is_fallback = false;
132
+ const data = await api("PATCH", `/byok/${encodeURIComponent(positionals[0])}`, {
133
+ auth: authFromValues(values),
134
+ requiresManagement: true,
135
+ body,
136
+ });
137
+ printResult(data);
138
+ return 0;
139
+ }
140
+
141
+ if (sub === "delete") {
142
+ const { values, positionals } = parseArgs(rest, {});
143
+ if (!positionals[0]) throw new Error("id required");
144
+ const data = await api("DELETE", `/byok/${encodeURIComponent(positionals[0])}`, {
145
+ auth: authFromValues(values),
146
+ requiresManagement: true,
147
+ });
148
+ printResult(data, () => outln("deleted"));
149
+ return 0;
150
+ }
151
+
152
+ throw new Error(`Unknown byok subcommand: ${sub}`);
153
+ }