@aaronjmars/openroutercli 0.1.4 → 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,149 @@
1
+ import { parseArgs, authFromValues, numberOption } from "../args.js";
2
+ import { api } from "../api.js";
3
+ import {
4
+ isJsonMode,
5
+ outln,
6
+ pricePerMillion,
7
+ printResult,
8
+ table,
9
+ writeBinaryOutput,
10
+ } from "../output.js";
11
+
12
+ const HELP = `Usage: openrouter image <subcommand> [options]
13
+
14
+ Subcommands:
15
+ create [prompt...] Generate image(s) (default)
16
+ models List image-generation models
17
+ endpoints <author>/<slug> List provider variants for a model
18
+
19
+ create options:
20
+ -m, --model <id> Image model id (required)
21
+ -p, --prompt <text> Prompt (or pass as positional)
22
+ --n <count> Number of images to generate
23
+ --resolution <r> e.g. 1024x1024
24
+ --aspect <ratio> e.g. 16:9
25
+ --quality <q>
26
+ --format <fmt> png|jpeg|webp
27
+ --background <b>
28
+ --seed <n>
29
+ --provider <json>
30
+ --extra <json> Extra fields merged into the body
31
+ -o, --out <file> Output file (prefix when generating multiple)
32
+ `;
33
+
34
+ const KNOWN_SUBCOMMANDS = new Set(["create", "generate", "models", "endpoints"]);
35
+
36
+ export async function imageCommand(argv) {
37
+ const sub = argv[0] && KNOWN_SUBCOMMANDS.has(argv[0]) ? argv[0] : null;
38
+ const rest = sub ? argv.slice(1) : argv;
39
+
40
+ if (argv[0] === "-h" || argv[0] === "--help" || argv[0] === "help") {
41
+ process.stdout.write(HELP);
42
+ return 0;
43
+ }
44
+ if (rest.includes("-h") || rest.includes("--help")) {
45
+ process.stdout.write(HELP);
46
+ return 0;
47
+ }
48
+
49
+ if (sub === "models") {
50
+ const { values } = parseArgs(rest, {});
51
+ const data = await api("GET", "/images/models", {
52
+ auth: authFromValues(values),
53
+ requireAuth: false,
54
+ });
55
+ printResult(data, () => {
56
+ const rows = data.data || [];
57
+ table(rows, [
58
+ { label: "id", value: (m) => m.id },
59
+ { label: "context", value: (m) => m.context_length ?? "" },
60
+ { label: "$/M", value: (m) => pricePerMillion((m.pricing || {}).prompt) },
61
+ { label: "name", value: (m) => m.name || "" },
62
+ ]);
63
+ });
64
+ return 0;
65
+ }
66
+
67
+ if (sub === "endpoints") {
68
+ const { values, positionals } = parseArgs(rest, {});
69
+ if (!positionals[0]) throw new Error("Expected <author>/<slug>");
70
+ const target = positionals[0];
71
+ const slash = target.indexOf("/");
72
+ if (slash === -1) throw new Error("Expected <author>/<slug>");
73
+ const author = target.slice(0, slash);
74
+ const slug = target.slice(slash + 1);
75
+ const data = await api("GET", `/images/models/${author}/${slug}/endpoints`, {
76
+ auth: authFromValues(values),
77
+ requireAuth: false,
78
+ });
79
+ const eps = (data.data && data.data.endpoints) || data.endpoints || [];
80
+ printResult(data, () => {
81
+ table(eps, [
82
+ { label: "provider", value: (e) => e.provider_name || e.name || "" },
83
+ {
84
+ label: "in/out $/M",
85
+ value: (e) => {
86
+ const p = e.pricing || {};
87
+ return `${pricePerMillion(p.prompt)}/${pricePerMillion(p.completion)}`;
88
+ },
89
+ },
90
+ { label: "quant", value: (e) => e.quantization || "" },
91
+ ]);
92
+ });
93
+ return 0;
94
+ }
95
+
96
+ // default (no subcommand) or create/generate: POST /images
97
+ const { values, positionals } = parseArgs(rest, {
98
+ model: { type: "string", short: "m" },
99
+ prompt: { type: "string", short: "p" },
100
+ n: { type: "string" },
101
+ resolution: { type: "string" },
102
+ aspect: { type: "string" },
103
+ quality: { type: "string" },
104
+ format: { type: "string" },
105
+ background: { type: "string" },
106
+ seed: { type: "string" },
107
+ provider: { type: "string" },
108
+ extra: { type: "string" },
109
+ out: { type: "string", short: "o" },
110
+ });
111
+ if (!values.model) throw new Error("--model is required");
112
+ const prompt = values.prompt || positionals.join(" ").trim();
113
+ if (!prompt) throw new Error("Prompt is required");
114
+
115
+ const body = { model: values.model, prompt };
116
+ if (values.n) body.n = numberOption(values.n, "--n");
117
+ if (values.resolution) body.resolution = values.resolution;
118
+ if (values.aspect) body.aspect_ratio = values.aspect;
119
+ if (values.quality) body.quality = values.quality;
120
+ if (values.format) body.output_format = values.format;
121
+ if (values.background) body.background = values.background;
122
+ if (values.seed) body.seed = numberOption(values.seed, "--seed");
123
+ if (values.provider) body.provider = JSON.parse(values.provider);
124
+ if (values.extra) Object.assign(body, JSON.parse(values.extra));
125
+
126
+ const data = await api("POST", "/images", { auth: authFromValues(values), body });
127
+
128
+ if (isJsonMode()) {
129
+ printResult(data);
130
+ return 0;
131
+ }
132
+
133
+ const images = data.data || [];
134
+ const ext = values.format || "png";
135
+ const single = images.length === 1;
136
+ for (let i = 0; i < images.length; i++) {
137
+ const img = images[i];
138
+ if (img.b64_json) {
139
+ const bytes = Buffer.from(img.b64_json, "base64");
140
+ let out;
141
+ if (values.out) out = single ? values.out : `${values.out}-${i}.${ext}`;
142
+ else out = `image-${i}.${ext}`;
143
+ await writeBinaryOutput(bytes, out);
144
+ } else if (img.url) {
145
+ outln(img.url);
146
+ }
147
+ }
148
+ return 0;
149
+ }
@@ -1,13 +1,17 @@
1
- import { parseArgs, authFromValues, numberOption } from '../args.js';
2
- import { api } from '../api.js';
3
- import { c, outln, printResult, table } from '../output.js';
1
+ import { parseArgs, authFromValues, numberOption } from "../args.js";
2
+ import { api } from "../api.js";
3
+ import { c, outln, printResult, table } from "../output.js";
4
4
 
5
5
  const HELP = `Usage: openrouter keys <subcommand> [options]
6
6
 
7
7
  Manage API keys (requires a provisioning/management key).
8
8
 
9
9
  Subcommands:
10
- list List API keys
10
+ list [options] List API keys
11
+
12
+ list options:
13
+ --offset <n> Pagination offset
14
+ --include-disabled Include disabled keys
11
15
  get <hash> Get a single key
12
16
  create <name> [options] Create a key
13
17
  update <hash> [options] Update a key
@@ -19,11 +23,13 @@ create options:
19
23
  --expires-at <iso> ISO 8601 UTC expiry
20
24
  --workspace <uuid> Workspace id
21
25
  --include-byok Include BYOK in limit
26
+ --disabled Create the key already disabled
22
27
 
23
28
  update options:
24
29
  --name <name>
25
30
  --limit <usd>
26
31
  --limit-reset <p>
32
+ --expires-at <iso> ISO 8601 UTC expiry
27
33
  --include-byok / --no-include-byok
28
34
  --disabled / --enabled
29
35
  `;
@@ -31,115 +37,124 @@ update options:
31
37
  export async function keysCommand(argv) {
32
38
  const sub = argv[0];
33
39
  const rest = argv.slice(1);
34
- if (!sub || sub === '-h' || sub === '--help' || sub === 'help') {
40
+ if (!sub || sub === "-h" || sub === "--help" || sub === "help") {
35
41
  process.stdout.write(HELP);
36
42
  return sub ? 0 : 1;
37
43
  }
38
44
 
39
- if (sub === 'list') {
45
+ if (rest.includes("-h") || rest.includes("--help")) {
46
+ process.stdout.write(HELP);
47
+ return 0;
48
+ }
49
+
50
+ if (sub === "list") {
40
51
  const { values } = parseArgs(rest, {
41
- offset: { type: 'string' },
42
- 'include-disabled': { type: 'boolean' }
52
+ offset: { type: "string" },
53
+ "include-disabled": { type: "boolean" },
43
54
  });
44
55
  const query = {};
45
56
  if (values.offset) query.offset = values.offset;
46
- if (values['include-disabled']) query.include_disabled = 'true';
47
- const data = await api('GET', '/keys', {
57
+ if (values["include-disabled"]) query.include_disabled = "true";
58
+ const data = await api("GET", "/keys", {
48
59
  auth: authFromValues(values),
49
60
  requiresManagement: true,
50
- query
61
+ query,
51
62
  });
52
63
  printResult(data, () => {
53
64
  const rows = data.data || [];
54
65
  table(rows, [
55
- { label: 'name', value: (k) => k.name || '' },
56
- { label: 'hash', value: (k) => k.hash || k.id || '' },
57
- { label: 'usage', value: (k) => k.usage ?? '' },
58
- { label: 'limit', value: (k) => k.limit ?? '' },
59
- { label: 'disabled', value: (k) => (k.disabled ? 'yes' : '') }
66
+ { label: "name", value: (k) => k.name || "" },
67
+ { label: "hash", value: (k) => k.hash || k.id || "" },
68
+ { label: "usage", value: (k) => k.usage ?? "" },
69
+ { label: "limit", value: (k) => k.limit ?? "" },
70
+ { label: "disabled", value: (k) => (k.disabled ? "yes" : "") },
60
71
  ]);
61
72
  });
62
73
  return 0;
63
74
  }
64
75
 
65
- if (sub === 'get') {
76
+ if (sub === "get") {
66
77
  const { values, positionals } = parseArgs(rest, {});
67
- if (!positionals[0]) throw new Error('hash required');
68
- const data = await api('GET', `/keys/${encodeURIComponent(positionals[0])}`, {
78
+ if (!positionals[0]) throw new Error("hash required");
79
+ const data = await api("GET", `/keys/${encodeURIComponent(positionals[0])}`, {
69
80
  auth: authFromValues(values),
70
- requiresManagement: true
81
+ requiresManagement: true,
71
82
  });
72
83
  printResult(data);
73
84
  return 0;
74
85
  }
75
86
 
76
- if (sub === 'create') {
87
+ if (sub === "create") {
77
88
  const { values, positionals } = parseArgs(rest, {
78
- limit: { type: 'string' },
79
- 'limit-reset': { type: 'string' },
80
- 'expires-at': { type: 'string' },
81
- workspace: { type: 'string' },
82
- 'include-byok': { type: 'boolean' }
89
+ limit: { type: "string" },
90
+ "limit-reset": { type: "string" },
91
+ "expires-at": { type: "string" },
92
+ workspace: { type: "string" },
93
+ "include-byok": { type: "boolean" },
94
+ disabled: { type: "boolean" },
83
95
  });
84
96
  const name = positionals[0];
85
- if (!name) throw new Error('name required');
97
+ if (!name) throw new Error("name required");
86
98
  const body = { name };
87
- if (values.limit) body.limit = numberOption(values.limit, '--limit');
88
- if (values['limit-reset']) body.limit_reset = values['limit-reset'];
89
- if (values['expires-at']) body.expires_at = values['expires-at'];
99
+ if (values.limit) body.limit = numberOption(values.limit, "--limit");
100
+ if (values["limit-reset"]) body.limit_reset = values["limit-reset"];
101
+ if (values["expires-at"]) body.expires_at = values["expires-at"];
90
102
  if (values.workspace) body.workspace_id = values.workspace;
91
- if (values['include-byok']) body.include_byok_in_limit = true;
92
- const data = await api('POST', '/keys', {
103
+ if (values["include-byok"]) body.include_byok_in_limit = true;
104
+ if (values.disabled) body.disabled = true;
105
+ const data = await api("POST", "/keys", {
93
106
  auth: authFromValues(values),
94
107
  requiresManagement: true,
95
- body
108
+ body,
96
109
  });
97
110
  printResult(data, () => {
98
111
  const k = data.key || (data.data && data.data.key);
99
112
  const meta = data.data || data;
100
- outln(c.bold('key (save this — shown only once):'));
101
- outln(' ' + (k || '(see JSON)'));
113
+ outln(c.bold("key (save this - shown only once):"));
114
+ outln(" " + (k || "(see JSON)"));
102
115
  outln(c.dim(JSON.stringify(meta, null, 2)));
103
116
  });
104
117
  return 0;
105
118
  }
106
119
 
107
- if (sub === 'update') {
120
+ if (sub === "update") {
108
121
  const { values, positionals } = parseArgs(rest, {
109
- name: { type: 'string' },
110
- limit: { type: 'string' },
111
- 'limit-reset': { type: 'string' },
112
- 'include-byok': { type: 'boolean' },
113
- 'no-include-byok': { type: 'boolean' },
114
- disabled: { type: 'boolean' },
115
- enabled: { type: 'boolean' }
122
+ name: { type: "string" },
123
+ limit: { type: "string" },
124
+ "limit-reset": { type: "string" },
125
+ "expires-at": { type: "string" },
126
+ "include-byok": { type: "boolean" },
127
+ "no-include-byok": { type: "boolean" },
128
+ disabled: { type: "boolean" },
129
+ enabled: { type: "boolean" },
116
130
  });
117
- if (!positionals[0]) throw new Error('hash required');
131
+ if (!positionals[0]) throw new Error("hash required");
118
132
  const body = {};
119
133
  if (values.name) body.name = values.name;
120
- if (values.limit) body.limit = numberOption(values.limit, '--limit');
121
- if (values['limit-reset']) body.limit_reset = values['limit-reset'];
122
- if (values['include-byok']) body.include_byok_in_limit = true;
123
- if (values['no-include-byok']) body.include_byok_in_limit = false;
134
+ if (values.limit) body.limit = numberOption(values.limit, "--limit");
135
+ if (values["limit-reset"]) body.limit_reset = values["limit-reset"];
136
+ if (values["expires-at"]) body.expires_at = values["expires-at"];
137
+ if (values["include-byok"]) body.include_byok_in_limit = true;
138
+ if (values["no-include-byok"]) body.include_byok_in_limit = false;
124
139
  if (values.disabled) body.disabled = true;
125
140
  if (values.enabled) body.disabled = false;
126
- const data = await api('PATCH', `/keys/${encodeURIComponent(positionals[0])}`, {
141
+ const data = await api("PATCH", `/keys/${encodeURIComponent(positionals[0])}`, {
127
142
  auth: authFromValues(values),
128
143
  requiresManagement: true,
129
- body
144
+ body,
130
145
  });
131
146
  printResult(data);
132
147
  return 0;
133
148
  }
134
149
 
135
- if (sub === 'delete') {
150
+ if (sub === "delete") {
136
151
  const { values, positionals } = parseArgs(rest, {});
137
- if (!positionals[0]) throw new Error('hash required');
138
- const data = await api('DELETE', `/keys/${encodeURIComponent(positionals[0])}`, {
152
+ if (!positionals[0]) throw new Error("hash required");
153
+ const data = await api("DELETE", `/keys/${encodeURIComponent(positionals[0])}`, {
139
154
  auth: authFromValues(values),
140
- requiresManagement: true
155
+ requiresManagement: true,
141
156
  });
142
- printResult(data, () => outln('deleted'));
157
+ printResult(data, () => outln("deleted"));
143
158
  return 0;
144
159
  }
145
160