@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,102 @@
1
+ import { parseArgs, authFromValues } from "../args.js";
2
+ import { api } from "../api.js";
3
+ import { outln, printResult, table, writeBinaryOutput } from "../output.js";
4
+
5
+ const HELP = `Usage: openrouter files <subcommand> [options]
6
+
7
+ Manage files stored on OpenRouter (backing store for file/PDF inputs).
8
+
9
+ Subcommands:
10
+ list List uploaded files
11
+ get <id> Get one file's metadata
12
+ upload <path> [options] Upload a file (multipart)
13
+ download <id> [-o file] Download file content
14
+ delete <id> Delete a file
15
+
16
+ upload options:
17
+ --purpose <p> Purpose tag for the file (e.g. assistants, user_data)
18
+ `;
19
+
20
+ export async function filesCommand(argv) {
21
+ const sub = argv[0];
22
+ const rest = argv.slice(1);
23
+ if (!sub || sub === "help" || sub === "-h" || sub === "--help") {
24
+ process.stdout.write(HELP);
25
+ return sub ? 0 : 1;
26
+ }
27
+ if (rest.includes("-h") || rest.includes("--help")) {
28
+ process.stdout.write(HELP);
29
+ return 0;
30
+ }
31
+
32
+ if (sub === "list") {
33
+ const { values } = parseArgs(rest, {});
34
+ const data = await api("GET", "/files", { auth: authFromValues(values) });
35
+ printResult(data, () => {
36
+ const rows = data.data || [];
37
+ table(rows, [
38
+ { label: "id", value: (f) => f.id || "" },
39
+ { label: "filename", value: (f) => f.filename || f.name || "" },
40
+ { label: "bytes", value: (f) => f.bytes ?? f.size ?? "" },
41
+ { label: "purpose", value: (f) => f.purpose || "" },
42
+ ]);
43
+ });
44
+ return 0;
45
+ }
46
+
47
+ if (sub === "get") {
48
+ const { values, positionals } = parseArgs(rest, {});
49
+ if (!positionals[0]) throw new Error("id required");
50
+ const data = await api("GET", `/files/${encodeURIComponent(positionals[0])}`, {
51
+ auth: authFromValues(values),
52
+ });
53
+ printResult(data);
54
+ return 0;
55
+ }
56
+
57
+ if (sub === "upload") {
58
+ const { values, positionals } = parseArgs(rest, {
59
+ purpose: { type: "string" },
60
+ });
61
+ const path = positionals[0];
62
+ if (!path) throw new Error("path required");
63
+ const { readFile } = await import("node:fs/promises");
64
+ const buf = await readFile(path);
65
+ const filename = path.split("/").pop() || "file";
66
+ const form = new FormData();
67
+ form.append("file", new Blob([buf]), filename);
68
+ if (values.purpose) form.append("purpose", values.purpose);
69
+ const data = await api("POST", "/files", {
70
+ auth: authFromValues(values),
71
+ body: form,
72
+ });
73
+ printResult(data);
74
+ return 0;
75
+ }
76
+
77
+ if (sub === "download") {
78
+ const { values, positionals } = parseArgs(rest, {
79
+ out: { type: "string", short: "o" },
80
+ });
81
+ if (!positionals[0]) throw new Error("id required");
82
+ const bytes = await api("GET", `/files/${encodeURIComponent(positionals[0])}/content`, {
83
+ auth: authFromValues(values),
84
+ binary: true,
85
+ });
86
+ const out = values.out || positionals[0];
87
+ await writeBinaryOutput(bytes, out);
88
+ return 0;
89
+ }
90
+
91
+ if (sub === "delete") {
92
+ const { values, positionals } = parseArgs(rest, {});
93
+ if (!positionals[0]) throw new Error("id required");
94
+ const data = await api("DELETE", `/files/${encodeURIComponent(positionals[0])}`, {
95
+ auth: authFromValues(values),
96
+ });
97
+ printResult(data, () => outln("deleted"));
98
+ return 0;
99
+ }
100
+
101
+ throw new Error(`Unknown files subcommand: ${sub}`);
102
+ }
@@ -1,29 +1,29 @@
1
- import { parseArgs, authFromValues } from '../args.js';
2
- import { api } from '../api.js';
3
- import { c, outln, printResult } from '../output.js';
1
+ import { parseArgs, authFromValues } from "../args.js";
2
+ import { api } from "../api.js";
3
+ import { c, outln, printResult } from "../output.js";
4
4
 
5
5
  export async function generationCommand(argv) {
6
6
  const { values, positionals } = parseArgs(argv, {
7
- content: { type: 'boolean' }
7
+ content: { type: "boolean" },
8
8
  });
9
9
  if (values.help || !positionals[0]) {
10
10
  process.stdout.write(
11
- 'Usage: openrouter generation <id> [--content]\n\nFetch metadata for a generation. With --content, fetch the input/output content.\n'
11
+ "Usage: openrouter generation <id> [--content]\n\nFetch metadata for a generation. With --content, fetch the input/output content.\n",
12
12
  );
13
13
  return values.help ? 0 : 1;
14
14
  }
15
15
  const id = positionals[0];
16
- const path = values.content ? '/generation/content' : '/generation';
17
- const data = await api('GET', path, {
16
+ const path = values.content ? "/generation/content" : "/generation";
17
+ const data = await api("GET", path, {
18
18
  auth: authFromValues(values),
19
- query: { id }
19
+ query: { id },
20
20
  });
21
21
  printResult(data, () => {
22
22
  const d = data.data || data;
23
23
  outln(c.bold(`generation ${id}`));
24
24
  for (const [k, v] of Object.entries(d)) {
25
- const display = typeof v === 'string' && v.length > 200 ? v.slice(0, 200) + '...' : v;
26
- outln(` ${k}: ${typeof display === 'object' ? JSON.stringify(display) : display}`);
25
+ const display = typeof v === "string" && v.length > 200 ? v.slice(0, 200) + "..." : v;
26
+ outln(` ${k}: ${typeof display === "object" ? JSON.stringify(display) : display}`);
27
27
  }
28
28
  });
29
29
  return 0;
@@ -1,6 +1,12 @@
1
- import { parseArgs, authFromValues, PAGINATION_OPTIONS, paginationQuery, numberOption } from '../args.js';
2
- import { api } from '../api.js';
3
- import { outln, printResult, table } from '../output.js';
1
+ import {
2
+ parseArgs,
3
+ authFromValues,
4
+ PAGINATION_OPTIONS,
5
+ paginationQuery,
6
+ numberOption,
7
+ } from "../args.js";
8
+ import { api } from "../api.js";
9
+ import { outln, printResult, table } from "../output.js";
4
10
 
5
11
  const HELP = `Usage: openrouter guardrails <subcommand> [options]
6
12
 
@@ -30,197 +36,210 @@ create / update options:
30
36
  --ignored-models <csv>
31
37
  --allowed-providers <csv>
32
38
  --ignored-providers <csv>
39
+ --include-byok / --no-include-byok Count BYOK spend toward the budget
33
40
  --workspace <uuid> (create only)
34
41
  `;
35
42
 
36
43
  function csv(v) {
37
44
  if (v == null) return undefined;
38
- if (v === '') return null;
39
- return v.split(',').map((s) => s.trim()).filter(Boolean);
45
+ if (v === "") return null;
46
+ return v
47
+ .split(",")
48
+ .map((s) => s.trim())
49
+ .filter(Boolean);
40
50
  }
41
51
 
42
52
  function buildBody(values) {
43
53
  const body = {};
44
54
  if (values.description !== undefined) body.description = values.description;
45
- if (values['limit-usd'] !== undefined) body.limit_usd = numberOption(values['limit-usd'], '--limit-usd');
46
- if (values['reset-interval'] !== undefined) {
47
- body.reset_interval = values['reset-interval'] === 'null' ? null : values['reset-interval'];
55
+ if (values["limit-usd"] !== undefined)
56
+ body.limit_usd = numberOption(values["limit-usd"], "--limit-usd");
57
+ if (values["reset-interval"] !== undefined) {
58
+ body.reset_interval = values["reset-interval"] === "null" ? null : values["reset-interval"];
48
59
  }
49
- if (values['enforce-zdr']) body.enforce_zdr = true;
50
- if (values['no-zdr']) body.enforce_zdr = false;
51
- if (values['allowed-models'] !== undefined) body.allowed_models = csv(values['allowed-models']);
52
- if (values['ignored-models'] !== undefined) body.ignored_models = csv(values['ignored-models']);
53
- if (values['allowed-providers'] !== undefined) body.allowed_providers = csv(values['allowed-providers']);
54
- if (values['ignored-providers'] !== undefined) body.ignored_providers = csv(values['ignored-providers']);
60
+ if (values["enforce-zdr"]) body.enforce_zdr = true;
61
+ if (values["no-zdr"]) body.enforce_zdr = false;
62
+ if (values["allowed-models"] !== undefined) body.allowed_models = csv(values["allowed-models"]);
63
+ if (values["ignored-models"] !== undefined) body.ignored_models = csv(values["ignored-models"]);
64
+ if (values["allowed-providers"] !== undefined)
65
+ body.allowed_providers = csv(values["allowed-providers"]);
66
+ if (values["ignored-providers"] !== undefined)
67
+ body.ignored_providers = csv(values["ignored-providers"]);
68
+ if (values["include-byok"]) body.include_byok_in_budgets = true;
69
+ if (values["no-include-byok"]) body.include_byok_in_budgets = false;
55
70
  return body;
56
71
  }
57
72
 
58
73
  const COMMON_FIELDS = {
59
- description: { type: 'string' },
60
- 'limit-usd': { type: 'string' },
61
- 'reset-interval': { type: 'string' },
62
- 'enforce-zdr': { type: 'boolean' },
63
- 'no-zdr': { type: 'boolean' },
64
- 'allowed-models': { type: 'string' },
65
- 'ignored-models': { type: 'string' },
66
- 'allowed-providers': { type: 'string' },
67
- 'ignored-providers': { type: 'string' }
74
+ description: { type: "string" },
75
+ "limit-usd": { type: "string" },
76
+ "reset-interval": { type: "string" },
77
+ "enforce-zdr": { type: "boolean" },
78
+ "no-zdr": { type: "boolean" },
79
+ "allowed-models": { type: "string" },
80
+ "ignored-models": { type: "string" },
81
+ "allowed-providers": { type: "string" },
82
+ "ignored-providers": { type: "string" },
83
+ "include-byok": { type: "boolean" },
84
+ "no-include-byok": { type: "boolean" },
68
85
  };
69
86
 
70
87
  export async function guardrailsCommand(argv) {
71
88
  const sub = argv[0];
72
89
  const rest = argv.slice(1);
73
- if (!sub || sub === 'help' || sub === '-h' || sub === '--help') {
90
+ if (!sub || sub === "help" || sub === "-h" || sub === "--help") {
74
91
  process.stdout.write(HELP);
75
92
  return sub ? 0 : 1;
76
93
  }
77
94
 
78
- if (rest.includes('-h') || rest.includes('--help')) {
95
+ if (rest.includes("-h") || rest.includes("--help")) {
79
96
  process.stdout.write(HELP);
80
97
  return 0;
81
98
  }
82
99
 
83
- if (sub === 'list') {
100
+ if (sub === "list") {
84
101
  const { values } = parseArgs(rest, PAGINATION_OPTIONS);
85
102
  const query = paginationQuery(values);
86
- const data = await api('GET', '/guardrails', {
103
+ const data = await api("GET", "/guardrails", {
87
104
  auth: authFromValues(values),
88
105
  requiresManagement: true,
89
- query
106
+ query,
90
107
  });
91
108
  printResult(data, () => {
92
109
  const rows = data.data || [];
93
110
  table(rows, [
94
- { label: 'id', value: (g) => g.id },
95
- { label: 'name', value: (g) => g.name },
96
- { label: 'limit_usd', value: (g) => g.limit_usd ?? '' },
97
- { label: 'reset', value: (g) => g.reset_interval ?? '' },
98
- { label: 'zdr', value: (g) => (g.enforce_zdr ? 'yes' : '') }
111
+ { label: "id", value: (g) => g.id },
112
+ { label: "name", value: (g) => g.name },
113
+ { label: "limit_usd", value: (g) => g.limit_usd ?? "" },
114
+ { label: "reset", value: (g) => g.reset_interval ?? "" },
115
+ { label: "zdr", value: (g) => (g.enforce_zdr ? "yes" : "") },
99
116
  ]);
100
117
  });
101
118
  return 0;
102
119
  }
103
120
 
104
- if (sub === 'get') {
121
+ if (sub === "get") {
105
122
  const { values, positionals } = parseArgs(rest, {});
106
- if (!positionals[0]) throw new Error('id required');
107
- const data = await api('GET', `/guardrails/${encodeURIComponent(positionals[0])}`, {
123
+ if (!positionals[0]) throw new Error("id required");
124
+ const data = await api("GET", `/guardrails/${encodeURIComponent(positionals[0])}`, {
108
125
  auth: authFromValues(values),
109
- requiresManagement: true
126
+ requiresManagement: true,
110
127
  });
111
128
  printResult(data);
112
129
  return 0;
113
130
  }
114
131
 
115
- if (sub === 'create') {
132
+ if (sub === "create") {
116
133
  const { values, positionals } = parseArgs(rest, {
117
134
  ...COMMON_FIELDS,
118
- workspace: { type: 'string' }
135
+ workspace: { type: "string" },
119
136
  });
120
137
  const name = positionals[0];
121
- if (!name) throw new Error('name required');
138
+ if (!name) throw new Error("name required");
122
139
  const body = { name, ...buildBody(values) };
123
140
  if (values.workspace) body.workspace_id = values.workspace;
124
- const data = await api('POST', '/guardrails', {
141
+ const data = await api("POST", "/guardrails", {
125
142
  auth: authFromValues(values),
126
143
  requiresManagement: true,
127
- body
144
+ body,
128
145
  });
129
146
  printResult(data);
130
147
  return 0;
131
148
  }
132
149
 
133
- if (sub === 'update') {
150
+ if (sub === "update") {
134
151
  const { values, positionals } = parseArgs(rest, {
135
152
  ...COMMON_FIELDS,
136
- name: { type: 'string' }
153
+ name: { type: "string" },
137
154
  });
138
- if (!positionals[0]) throw new Error('id required');
155
+ if (!positionals[0]) throw new Error("id required");
139
156
  const body = buildBody(values);
140
157
  if (values.name) body.name = values.name;
141
- const data = await api('PATCH', `/guardrails/${encodeURIComponent(positionals[0])}`, {
158
+ const data = await api("PATCH", `/guardrails/${encodeURIComponent(positionals[0])}`, {
142
159
  auth: authFromValues(values),
143
160
  requiresManagement: true,
144
- body
161
+ body,
145
162
  });
146
163
  printResult(data);
147
164
  return 0;
148
165
  }
149
166
 
150
- if (sub === 'delete') {
167
+ if (sub === "delete") {
151
168
  const { values, positionals } = parseArgs(rest, {});
152
- if (!positionals[0]) throw new Error('id required');
153
- const data = await api('DELETE', `/guardrails/${encodeURIComponent(positionals[0])}`, {
169
+ if (!positionals[0]) throw new Error("id required");
170
+ const data = await api("DELETE", `/guardrails/${encodeURIComponent(positionals[0])}`, {
154
171
  auth: authFromValues(values),
155
- requiresManagement: true
172
+ requiresManagement: true,
156
173
  });
157
- printResult(data, () => outln('deleted'));
174
+ printResult(data, () => outln("deleted"));
158
175
  return 0;
159
176
  }
160
177
 
161
- if (sub === 'assignments') {
178
+ if (sub === "assignments") {
162
179
  const { values } = parseArgs(rest, {
163
- keys: { type: 'boolean' },
164
- members: { type: 'boolean' }
180
+ keys: { type: "boolean" },
181
+ members: { type: "boolean" },
165
182
  });
166
183
  if (values.keys && values.members) {
167
- throw new Error('Pass only one of --keys or --members.');
184
+ throw new Error("Pass only one of --keys or --members.");
168
185
  }
169
186
  const path = values.members
170
- ? '/guardrails/assignments/members'
171
- : '/guardrails/assignments/keys';
172
- const data = await api('GET', path, {
187
+ ? "/guardrails/assignments/members"
188
+ : "/guardrails/assignments/keys";
189
+ const data = await api("GET", path, {
173
190
  auth: authFromValues(values),
174
- requiresManagement: true
191
+ requiresManagement: true,
175
192
  });
176
193
  printResult(data);
177
194
  return 0;
178
195
  }
179
196
 
180
- if (sub === 'key-assignments' || sub === 'member-assignments') {
197
+ if (sub === "key-assignments" || sub === "member-assignments") {
181
198
  const { values, positionals } = parseArgs(rest, {});
182
- if (!positionals[0]) throw new Error('id required');
199
+ if (!positionals[0]) throw new Error("id required");
183
200
  const path =
184
- sub === 'key-assignments'
201
+ sub === "key-assignments"
185
202
  ? `/guardrails/${encodeURIComponent(positionals[0])}/assignments/keys`
186
203
  : `/guardrails/${encodeURIComponent(positionals[0])}/assignments/members`;
187
- const data = await api('GET', path, {
204
+ const data = await api("GET", path, {
188
205
  auth: authFromValues(values),
189
- requiresManagement: true
206
+ requiresManagement: true,
190
207
  });
191
208
  printResult(data);
192
209
  return 0;
193
210
  }
194
211
 
195
- if (sub === 'assign-key' || sub === 'unassign-key') {
212
+ if (sub === "assign-key" || sub === "unassign-key") {
196
213
  const { values, positionals } = parseArgs(rest, {});
197
- if (positionals.length < 2) throw new Error('Usage: <id> <hash>...');
214
+ if (positionals.length < 2) throw new Error("Usage: <id> <hash>...");
198
215
  const id = positionals[0];
199
216
  const hashes = positionals.slice(1);
200
- const path = sub === 'assign-key'
201
- ? `/guardrails/${encodeURIComponent(id)}/assignments/keys`
202
- : `/guardrails/${encodeURIComponent(id)}/assignments/keys/remove`;
203
- const data = await api('POST', path, {
217
+ const path =
218
+ sub === "assign-key"
219
+ ? `/guardrails/${encodeURIComponent(id)}/assignments/keys`
220
+ : `/guardrails/${encodeURIComponent(id)}/assignments/keys/remove`;
221
+ const data = await api("POST", path, {
204
222
  auth: authFromValues(values),
205
223
  requiresManagement: true,
206
- body: { key_hashes: hashes }
224
+ body: { key_hashes: hashes },
207
225
  });
208
226
  printResult(data);
209
227
  return 0;
210
228
  }
211
229
 
212
- if (sub === 'assign-member' || sub === 'unassign-member') {
230
+ if (sub === "assign-member" || sub === "unassign-member") {
213
231
  const { values, positionals } = parseArgs(rest, {});
214
- if (positionals.length < 2) throw new Error('Usage: <id> <user_id>...');
232
+ if (positionals.length < 2) throw new Error("Usage: <id> <user_id>...");
215
233
  const id = positionals[0];
216
234
  const userIds = positionals.slice(1);
217
- const path = sub === 'assign-member'
218
- ? `/guardrails/${encodeURIComponent(id)}/assignments/members`
219
- : `/guardrails/${encodeURIComponent(id)}/assignments/members/remove`;
220
- const data = await api('POST', path, {
235
+ const path =
236
+ sub === "assign-member"
237
+ ? `/guardrails/${encodeURIComponent(id)}/assignments/members`
238
+ : `/guardrails/${encodeURIComponent(id)}/assignments/members/remove`;
239
+ const data = await api("POST", path, {
221
240
  auth: authFromValues(values),
222
241
  requiresManagement: true,
223
- body: { user_ids: userIds }
242
+ body: { user_ids: userIds },
224
243
  });
225
244
  printResult(data);
226
245
  return 0;
@@ -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
+ }