openany-cli-dev 0.0.0-stage → 0.1.1
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 +472 -2
- package/bin/openany.js +3 -0
- package/package.json +27 -4
- package/skills/openany/SKILL.md +353 -0
- package/skills/openany/SKILL.zh-CN.md +496 -0
- package/skills/openany-provider/SKILL.md +26 -0
- package/skills/openany-provider/SKILL.zh-CN.md +26 -0
- package/skills/openany-provider/references/apis.md +60 -0
- package/skills/openany-provider/references/apis.zh-CN.md +55 -0
- package/skills/openany-provider/references/application.md +49 -0
- package/skills/openany-provider/references/application.zh-CN.md +51 -0
- package/skills/openany-provider/references/auth.md +3 -0
- package/skills/openany-provider/references/auth.zh-CN.md +3 -0
- package/skills/openany-provider/references/connections.md +36 -0
- package/skills/openany-provider/references/connections.zh-CN.md +36 -0
- package/skills/openany-provider/references/imports.md +57 -0
- package/skills/openany-provider/references/imports.zh-CN.md +57 -0
- package/skills/openany-provider/references/listing.md +29 -0
- package/skills/openany-provider/references/listing.zh-CN.md +11 -0
- package/skills/openany-provider/references/login.md +57 -0
- package/skills/openany-provider/references/login.zh-CN.md +57 -0
- package/skills/openany-provider/references/model-import.md +11 -0
- package/skills/openany-provider/references/model-import.zh-CN.md +11 -0
- package/skills/openany-provider/references/models.md +107 -0
- package/skills/openany-provider/references/models.zh-CN.md +68 -0
- package/skills/openany-provider/references/publishing.md +40 -0
- package/skills/openany-provider/references/publishing.zh-CN.md +40 -0
- package/skills/openany-provider/references/reviews.md +22 -0
- package/skills/openany-provider/references/reviews.zh-CN.md +20 -0
- package/skills/openany-provider/templates/api-metadata.json +27 -0
- package/skills/openany-provider/templates/bindings.json +1 -0
- package/skills/openany-provider/templates/models-v2.json +545 -0
- package/skills/openany-provider/templates/models.json +10 -0
- package/skills/openany-provider/templates/provider-agent-api-template.json +697 -0
- package/skills/openany-provider/templates/seller-agent-api-import-template.json +822 -0
- package/skills/openany-provider/templates/seller-registration.json +26 -0
- package/src/auth-recovery.js +179 -0
- package/src/auth.js +1001 -0
- package/src/catalog.js +306 -0
- package/src/cli.js +605 -0
- package/src/config.js +260 -0
- package/src/errors.js +53 -0
- package/src/http.js +238 -0
- package/src/model-curl.js +359 -0
- package/src/model-materials.js +58 -0
- package/src/model-request.js +402 -0
- package/src/model-validation.js +56 -0
- package/src/models.js +188 -0
- package/src/native/config-acl.ps1 +34 -0
- package/src/native/connection-macos.js +109 -0
- package/src/native/connection-windows.ps1 +98 -0
- package/src/native/setup-macos.js +72 -0
- package/src/native/setup-windows.ps1 +83 -0
- package/src/native-prompt.js +85 -0
- package/src/payment-signing.js +362 -0
- package/src/prompt.js +69 -0
- package/src/provider-api-import-v2.js +72 -0
- package/src/provider-apis.js +3 -0
- package/src/provider-client.js +153 -0
- package/src/provider-connections.js +152 -0
- package/src/provider-import-format.js +289 -0
- package/src/provider-imports.js +402 -0
- package/src/provider-json.js +49 -0
- package/src/provider-model-import-v2.js +61 -0
- package/src/provider-models.js +253 -0
- package/src/provider-profile.js +65 -0
- package/src/provider-reviews.js +61 -0
- package/src/provider-state.js +176 -0
- package/src/provider.js +75 -0
- package/src/setup.js +201 -0
- package/src/tasks.js +53 -0
- package/src/topups.js +845 -0
- package/src/vendor/provider-apply/market-taxonomy.mjs +22 -0
- package/src/vendor/provider-apply/provenance.json +8 -0
- package/src/vendor/provider-apply/registration.mjs +151 -0
- package/src/vendor/provider-import/api-categories.json +29 -0
- package/src/vendor/provider-import/api-fields.json +33 -0
- package/src/vendor/provider-import/model-fields.json +44 -0
- package/src/wallet.js +213 -0
- package/src/windows-permissions.js +22 -0
package/src/cli.js
ADDED
|
@@ -0,0 +1,605 @@
|
|
|
1
|
+
import packageInfo from "../package.json" with { type: "json" };
|
|
2
|
+
import { tmpdir } from "node:os";
|
|
3
|
+
import { join } from "node:path";
|
|
4
|
+
import { readFile, writeFile, lstat, mkdtemp } from "node:fs/promises";
|
|
5
|
+
import { ConfigStore, configurationStatus } from "./config.js";
|
|
6
|
+
import { CLIError, errorRecord, redact, requireValue } from "./errors.js";
|
|
7
|
+
import { HttpClient, unwrap } from "./http.js";
|
|
8
|
+
import { listQuery, runApi, formatMicroUsd } from "./catalog.js";
|
|
9
|
+
import { runModels } from "./models.js";
|
|
10
|
+
import { runTasks } from "./tasks.js";
|
|
11
|
+
const HELP = `OpenAny CLI ${packageInfo.version} (Node.js >=22)
|
|
12
|
+
Usage: openany-dev <command> [options]
|
|
13
|
+
Backend: https://dev.openany.si; default gateway: https://api-dev.openany.si
|
|
14
|
+
Model execution uses the OpenAny gateway with the path and query from curlExample.
|
|
15
|
+
setup [--wallet_connect wallet | --email_login email | --api_key api_key]
|
|
16
|
+
setup --api_key api_key --gui [-j] (native API key dialog)
|
|
17
|
+
setup --wallet_connect wallet --gui [-j] (native wallet dialog)
|
|
18
|
+
logout [-j] (back up and remove all local account configuration)
|
|
19
|
+
status_check wallet|email|api_key|payment [-j]
|
|
20
|
+
Legacy syntax (still supported):
|
|
21
|
+
setup [--scenario call|wallet|email] (interactive terminal setup)
|
|
22
|
+
status --scenario call|wallet|email|payment [-j]
|
|
23
|
+
setup --non-interactive | setup -j (check configuration only)
|
|
24
|
+
keys add|list|remove|activate --label LABEL [--stdin] [--activate]
|
|
25
|
+
balance
|
|
26
|
+
collections | discover -q KEYWORD -l LIMIT [--cursor CURSOR] [--public]
|
|
27
|
+
--category NAME --provider NAME --data-platform NAME --method GET
|
|
28
|
+
inspect --id API_ID
|
|
29
|
+
run --id API_ID [--query JSON] [--path JSON] [--headers JSON] [-i JSON|-f FILE]
|
|
30
|
+
tasks get --id TASK_ID
|
|
31
|
+
tasks wait --id TASK_ID [--timeout-seconds N] (default 120; 1–600)
|
|
32
|
+
calls list [--limit N --cursor C --status success|failed --api-id ID]
|
|
33
|
+
[--from UTC_ISO --to UTC_ISO]
|
|
34
|
+
Includes model and data/tool usage for the current key; review filtering is unsupported.
|
|
35
|
+
calls get --id REQUEST_ID
|
|
36
|
+
models list|prices|playgrounds|parameters
|
|
37
|
+
--model ID --protocol NAME --mode NAME --endpoint PATH
|
|
38
|
+
models examples --model ID [--provider NAME] [--public]
|
|
39
|
+
models run --model ID [--provider NAME] [--example EXAMPLE_ID]
|
|
40
|
+
-i JSON|-f FILE [--attach FIELD=FILE] [-o RESULT] [--dry-run]
|
|
41
|
+
--path '{"task_id":"TASK_ID"}' (for a published GET example)
|
|
42
|
+
Multiple providers require --provider; multiple requests require --example.
|
|
43
|
+
Supply the complete JSON body, including model; route.provider is bound from the catalog.
|
|
44
|
+
Audio transcription is not supported. Task polling is explicit, never automatic.
|
|
45
|
+
seller status [--application ID]
|
|
46
|
+
seller applications list|create --file REGISTRATION_JSON --operation NAME
|
|
47
|
+
seller connections list|get|create --application ID [--id ID]
|
|
48
|
+
create --file JSON --operation NAME [--stdin] (or OPENANY_DEV_UPSTREAM_API_KEY)
|
|
49
|
+
create --gui --operation NAME [-j] (native connection dialog; macOS/Windows)
|
|
50
|
+
seller models|apis list|get|create|update|verify|publish|unpublish --application ID
|
|
51
|
+
[--id ID] [--file PATH | --input JSON] [--operation NAME] [--verification-id ID]
|
|
52
|
+
Model verify: request.path must be {} when supplied; endpointIndex selects the saved endpoint.
|
|
53
|
+
Independent writes need distinct operations; reconcile unknown outcomes before retrying.
|
|
54
|
+
seller reviews list|get --application ID [--id ID]
|
|
55
|
+
[--type model|api --resource-id ID] (historical records only)
|
|
56
|
+
seller imports preview --type model|api --file JSON --application ID
|
|
57
|
+
[--bindings-file JSON] [--metadata-file JSON]
|
|
58
|
+
seller imports run|resume|status --batch ID --application ID
|
|
59
|
+
seller schema --type model|api|connection|model-import --application ID
|
|
60
|
+
Seller writes require a stable --operation name; resume uncertain creates with --resume.
|
|
61
|
+
auth wallet [--prepare | --challenge-id ID --signature-file FILE]
|
|
62
|
+
auth email send [--email ADDRESS]
|
|
63
|
+
auth email verify --code SIX_DIGITS [--workspace ID] [--display-name NAME]
|
|
64
|
+
auth email refresh | auth email workspace --workspace ID
|
|
65
|
+
auth recover --file RECOVERY_FILE
|
|
66
|
+
keys create|rotate --identity wallet --label LABEL
|
|
67
|
+
[--prepare | --challenge-id ID --signature-file FILE] [--expected-key-id ID]
|
|
68
|
+
keys remote list [--limit N --offset N] [--workspace ID]
|
|
69
|
+
keys remote get|revoke --id KEY_ID [--workspace ID]
|
|
70
|
+
keys remote create --label LABEL [--workspace ID]
|
|
71
|
+
topups config [--workspace ID]
|
|
72
|
+
topups quote --amount-usd AMOUNT --network CAIP2 [--workspace ID]
|
|
73
|
+
topups authorize --identity wallet|email --network CAIP2[,CAIP2]
|
|
74
|
+
--max-single-usd AMOUNT --max-total-usd AMOUNT --scope LABEL
|
|
75
|
+
--expires-at ISO_TIMESTAMP [--workspace ID]
|
|
76
|
+
topups create --scope LABEL --intent LABEL --amount-usd AMOUNT --network CAIP2
|
|
77
|
+
[--prepare] [--resume]
|
|
78
|
+
topups pay --intent LABEL [--prepare | --signature-file FILE]
|
|
79
|
+
topups get --intent LABEL | --id ORDER_ID --identity wallet|email [--workspace ID]
|
|
80
|
+
General options: --help --version -j/--json -o/--output FILE (never overwrite existing files)
|
|
81
|
+
Regular requests require OPENANY_DEV_API_KEY or the saved active key; explicit --public browsing and models run --dry-run only read the public catalog without a key.
|
|
82
|
+
setup hides API key and wallet private key input and saves it to the existing restricted credentials file; environment variables take precedence.
|
|
83
|
+
keys add reads from OPENANY_DEV_API_KEY, --stdin or hidden terminal input; keys cannot be passed as command-line arguments.
|
|
84
|
+
--public is only for catalog browsing or models run --dry-run. Dry-run is rejected for all other commands; it never calls balance or executes a model.
|
|
85
|
+
--prepare exports payloads for external wallet signing without submitting payment. Signature files contain JSON for payments or signature text for authentication.
|
|
86
|
+
topups authorize only records prior user authorization; amounts include service fees, and submitted payments are never retried automatically.
|
|
87
|
+
`;
|
|
88
|
+
const boolean = new Set([
|
|
89
|
+
"help",
|
|
90
|
+
"version",
|
|
91
|
+
"json",
|
|
92
|
+
"public",
|
|
93
|
+
"stdin",
|
|
94
|
+
"activate",
|
|
95
|
+
"non-interactive",
|
|
96
|
+
"gui",
|
|
97
|
+
"prepare",
|
|
98
|
+
"resume",
|
|
99
|
+
"dry-run",
|
|
100
|
+
]);
|
|
101
|
+
const aliases = {
|
|
102
|
+
h: "help",
|
|
103
|
+
v: "version",
|
|
104
|
+
j: "json",
|
|
105
|
+
o: "output",
|
|
106
|
+
i: "input",
|
|
107
|
+
f: "file",
|
|
108
|
+
q: "search",
|
|
109
|
+
l: "limit",
|
|
110
|
+
};
|
|
111
|
+
const setupTargets = { wallet_connect: "wallet", email_login: "email", api_key: "api_key" };
|
|
112
|
+
const valuedOptions = new Set([
|
|
113
|
+
...Object.keys(setupTargets),
|
|
114
|
+
"output",
|
|
115
|
+
"input",
|
|
116
|
+
"file",
|
|
117
|
+
"search",
|
|
118
|
+
"limit",
|
|
119
|
+
"cursor",
|
|
120
|
+
"category",
|
|
121
|
+
"provider",
|
|
122
|
+
"data-platform",
|
|
123
|
+
"method",
|
|
124
|
+
"id",
|
|
125
|
+
"path",
|
|
126
|
+
"query",
|
|
127
|
+
"headers",
|
|
128
|
+
"label",
|
|
129
|
+
"model",
|
|
130
|
+
"protocol",
|
|
131
|
+
"mode",
|
|
132
|
+
"endpoint",
|
|
133
|
+
"example",
|
|
134
|
+
"attach",
|
|
135
|
+
"scenario",
|
|
136
|
+
"identity",
|
|
137
|
+
"email",
|
|
138
|
+
"code",
|
|
139
|
+
"display-name",
|
|
140
|
+
"workspace",
|
|
141
|
+
"challenge-id",
|
|
142
|
+
"signature",
|
|
143
|
+
"signature-file",
|
|
144
|
+
"expected-key-id",
|
|
145
|
+
"offset",
|
|
146
|
+
"scope",
|
|
147
|
+
"intent",
|
|
148
|
+
"network",
|
|
149
|
+
"max-single-usd",
|
|
150
|
+
"max-total-usd",
|
|
151
|
+
"amount-usd",
|
|
152
|
+
"expires-at",
|
|
153
|
+
"status",
|
|
154
|
+
"from",
|
|
155
|
+
"to",
|
|
156
|
+
"api-id",
|
|
157
|
+
"review-required",
|
|
158
|
+
"application",
|
|
159
|
+
"operation",
|
|
160
|
+
"type",
|
|
161
|
+
"batch",
|
|
162
|
+
"bindings-file",
|
|
163
|
+
"metadata-file",
|
|
164
|
+
"resource-id",
|
|
165
|
+
"verification-id",
|
|
166
|
+
"timeout-seconds",
|
|
167
|
+
]);
|
|
168
|
+
export function parseArgs(argv) {
|
|
169
|
+
const options = {},
|
|
170
|
+
args = [];
|
|
171
|
+
for (let i = 0; i < argv.length; i++) {
|
|
172
|
+
const item = argv[i];
|
|
173
|
+
if (item === "--") {
|
|
174
|
+
args.push(...argv.slice(i + 1));
|
|
175
|
+
break;
|
|
176
|
+
}
|
|
177
|
+
if (!item.startsWith("-")) {
|
|
178
|
+
args.push(item);
|
|
179
|
+
continue;
|
|
180
|
+
}
|
|
181
|
+
const eq = item.indexOf("=");
|
|
182
|
+
let key = (eq < 0 ? item : item.slice(0, eq)).replace(/^--?/, "");
|
|
183
|
+
key = aliases[key] ?? key;
|
|
184
|
+
if (!boolean.has(key) && !valuedOptions.has(key))
|
|
185
|
+
throw new CLIError("UNKNOWN_OPTION", `Unknown option: --${key}`);
|
|
186
|
+
let value;
|
|
187
|
+
if (eq >= 0) {
|
|
188
|
+
value = item.slice(eq + 1);
|
|
189
|
+
if (boolean.has(key)) {
|
|
190
|
+
if (!["true", "false"].includes(value))
|
|
191
|
+
throw new CLIError(
|
|
192
|
+
"INVALID_INPUT",
|
|
193
|
+
`--${key} requires true or false`,
|
|
194
|
+
);
|
|
195
|
+
value = value === "true";
|
|
196
|
+
}
|
|
197
|
+
} else if (boolean.has(key)) value = true;
|
|
198
|
+
else {
|
|
199
|
+
value = argv[++i];
|
|
200
|
+
if (value === undefined || value.startsWith("--"))
|
|
201
|
+
throw new CLIError("INPUT_REQUIRED", `Missing --${key} value`);
|
|
202
|
+
}
|
|
203
|
+
if (key === "attach") options[key] = [...(options[key] ?? []), value];
|
|
204
|
+
else if (Object.hasOwn(options, key))
|
|
205
|
+
throw new CLIError("INVALID_INPUT", `Repeated option: --${key}`);
|
|
206
|
+
else options[key] = value;
|
|
207
|
+
}
|
|
208
|
+
const selectors = Object.keys(setupTargets).filter((key) => Object.hasOwn(options, key));
|
|
209
|
+
if (selectors.length) {
|
|
210
|
+
if (args.length !== 1 || args[0] !== "setup" || selectors.length !== 1 || options.scenario !== undefined)
|
|
211
|
+
throw new CLIError("INVALID_INPUT", "Use exactly one setup selector with the setup command.");
|
|
212
|
+
const selector = selectors[0];
|
|
213
|
+
const target = setupTargets[selector];
|
|
214
|
+
if (options[selector] !== target)
|
|
215
|
+
throw new CLIError("INVALID_INPUT", `Use setup --${selector} ${target}.`);
|
|
216
|
+
options.scenario = target === "api_key" ? "call" : target;
|
|
217
|
+
}
|
|
218
|
+
const connectionGui = args.length === 3 && args[0] === "seller" &&
|
|
219
|
+
args[1] === "connections" && args[2] === "create";
|
|
220
|
+
const setupGui = args.length === 1 && args[0] === "setup" &&
|
|
221
|
+
["call", "wallet"].includes(options.scenario);
|
|
222
|
+
if (options.gui && ((!setupGui && !connectionGui) || options.prepare || options["non-interactive"] ||
|
|
223
|
+
(connectionGui && (options.stdin || options.file !== undefined || options.input !== undefined)))) {
|
|
224
|
+
throw new CLIError("INVALID_INPUT", "--gui requires API key/wallet setup or seller connections create; it cannot be combined with --prepare, --non-interactive, or connection file/input/stdin options.");
|
|
225
|
+
}
|
|
226
|
+
if (args[0] === "status_check" && !options.help && !options.version) {
|
|
227
|
+
const target = args[1];
|
|
228
|
+
if (args.length !== 2 || !["wallet", "email", "api_key", "payment"].includes(target) || options.scenario !== undefined)
|
|
229
|
+
throw new CLIError("INVALID_INPUT", "Use status_check with one target: wallet, email, api_key or payment.");
|
|
230
|
+
options.scenario = target === "api_key" ? "call" : target;
|
|
231
|
+
return { args: ["status"], options };
|
|
232
|
+
}
|
|
233
|
+
return { args, options };
|
|
234
|
+
}
|
|
235
|
+
function parseJson(value, name) {
|
|
236
|
+
try {
|
|
237
|
+
return JSON.parse(value);
|
|
238
|
+
} catch {
|
|
239
|
+
throw new CLIError("INVALID_INPUT", `${name} must be valid JSON`);
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
async function readSecret(stdin, stderr) {
|
|
243
|
+
if (!stdin.isTTY) {
|
|
244
|
+
let text = "";
|
|
245
|
+
for await (const chunk of stdin) text += chunk;
|
|
246
|
+
if (text.length > 65536)
|
|
247
|
+
throw new CLIError("INVALID_INPUT", "Secret input is too long");
|
|
248
|
+
return text.trim();
|
|
249
|
+
}
|
|
250
|
+
if (!stdin.setRawMode)
|
|
251
|
+
throw new CLIError("INPUT_REQUIRED", "Use --stdin or OPENANY_DEV_API_KEY");
|
|
252
|
+
stderr.write("API key: ");
|
|
253
|
+
return new Promise((resolve, reject) => {
|
|
254
|
+
let text = "";
|
|
255
|
+
const raw = stdin.isRaw;
|
|
256
|
+
stdin.setRawMode(true);
|
|
257
|
+
stdin.resume();
|
|
258
|
+
function finish(error) {
|
|
259
|
+
stdin.off("data", onData);
|
|
260
|
+
stdin.setRawMode(raw);
|
|
261
|
+
stdin.pause();
|
|
262
|
+
stderr.write("\n");
|
|
263
|
+
error ? reject(error) : resolve(text);
|
|
264
|
+
}
|
|
265
|
+
function onData(data) {
|
|
266
|
+
for (const character of data.toString()) {
|
|
267
|
+
if (character === "\u0003") {
|
|
268
|
+
finish(new CLIError("CANCELLED", "Input cancelled"));
|
|
269
|
+
return;
|
|
270
|
+
}
|
|
271
|
+
if (character === "\r" || character === "\n") {
|
|
272
|
+
finish();
|
|
273
|
+
return;
|
|
274
|
+
}
|
|
275
|
+
if (character === "\u007f" || character === "\b")
|
|
276
|
+
text = text.slice(0, -1);
|
|
277
|
+
else if (character >= " ") text += character;
|
|
278
|
+
}
|
|
279
|
+
}
|
|
280
|
+
stdin.on("data", onData);
|
|
281
|
+
});
|
|
282
|
+
}
|
|
283
|
+
async function localKeys(args, ctx) {
|
|
284
|
+
const { store, options, env } = ctx;
|
|
285
|
+
const action = args[1] ?? "list";
|
|
286
|
+
if (action === "list") {
|
|
287
|
+
const state = await store.read();
|
|
288
|
+
return {
|
|
289
|
+
keys: state.keys.map(({ label, metadata }) => ({
|
|
290
|
+
label,
|
|
291
|
+
metadata,
|
|
292
|
+
active: state.activeKey === label,
|
|
293
|
+
})),
|
|
294
|
+
environmentOverride: Boolean(env.OPENANY_DEV_API_KEY),
|
|
295
|
+
};
|
|
296
|
+
}
|
|
297
|
+
const label = requireValue(options.label ?? args[2], "--label");
|
|
298
|
+
if (!/^[A-Za-z0-9][A-Za-z0-9._-]{0,63}$/.test(label))
|
|
299
|
+
throw new CLIError(
|
|
300
|
+
"INVALID_INPUT",
|
|
301
|
+
"Key labels must use 1–64 letters, digits, dot, underscore or hyphen",
|
|
302
|
+
);
|
|
303
|
+
if (action === "add") {
|
|
304
|
+
let key = env.OPENANY_DEV_API_KEY?.trim();
|
|
305
|
+
if (options.stdin || !key) {
|
|
306
|
+
if (!options.stdin && !ctx.stdin.isTTY)
|
|
307
|
+
throw new CLIError(
|
|
308
|
+
"CONFIG_REQUIRED",
|
|
309
|
+
"Provide OPENANY_DEV_API_KEY or pipe a key with --stdin",
|
|
310
|
+
{ missing: ["OPENANY_DEV_API_KEY"] },
|
|
311
|
+
);
|
|
312
|
+
key = await readSecret(ctx.stdin, ctx.stderr);
|
|
313
|
+
}
|
|
314
|
+
requireValue(key, "API key");
|
|
315
|
+
ctx.secrets.push(key);
|
|
316
|
+
return store.update((state) => {
|
|
317
|
+
if (state.keys.some((k) => k.label === label))
|
|
318
|
+
throw new CLIError(
|
|
319
|
+
"KEY_EXISTS",
|
|
320
|
+
"A local key with this label already exists",
|
|
321
|
+
);
|
|
322
|
+
state.keys.push({ label, key });
|
|
323
|
+
if (!state.activeKey || options.activate) state.activeKey = label;
|
|
324
|
+
return { label, active: state.activeKey === label, saved: true };
|
|
325
|
+
});
|
|
326
|
+
}
|
|
327
|
+
return store.update((state) => {
|
|
328
|
+
const index = state.keys.findIndex((k) => k.label === label);
|
|
329
|
+
if (index < 0) throw new CLIError("KEY_NOT_FOUND", "Local key not found");
|
|
330
|
+
if (action === "activate") state.activeKey = label;
|
|
331
|
+
else if (action === "remove") {
|
|
332
|
+
state.keys.splice(index, 1);
|
|
333
|
+
if (state.activeKey === label) state.activeKey = null;
|
|
334
|
+
} else throw new CLIError("UNKNOWN_COMMAND", "Unknown local keys command");
|
|
335
|
+
return { label, action };
|
|
336
|
+
});
|
|
337
|
+
}
|
|
338
|
+
function callQuery(options) {
|
|
339
|
+
const query = listQuery({ limit: options.limit, cursor: options.cursor });
|
|
340
|
+
for (const name of ["from", "to"])
|
|
341
|
+
if (options[name] !== undefined) {
|
|
342
|
+
const time = new Date(options[name]);
|
|
343
|
+
if (
|
|
344
|
+
!Number.isFinite(time.getTime()) ||
|
|
345
|
+
time.toISOString() !== options[name]
|
|
346
|
+
)
|
|
347
|
+
throw new CLIError(
|
|
348
|
+
"INVALID_INPUT",
|
|
349
|
+
`${name} must use UTC ISO format with milliseconds`,
|
|
350
|
+
);
|
|
351
|
+
query[name] = options[name];
|
|
352
|
+
}
|
|
353
|
+
if (query.from && query.to && query.from >= query.to)
|
|
354
|
+
throw new CLIError("INVALID_INPUT", "from must precede to");
|
|
355
|
+
if (options.status !== undefined) {
|
|
356
|
+
const allowed = ["success", "failed"];
|
|
357
|
+
if (!allowed.includes(options.status))
|
|
358
|
+
throw new CLIError(
|
|
359
|
+
"INVALID_INPUT",
|
|
360
|
+
"Call status must be success or failed",
|
|
361
|
+
{ allowed },
|
|
362
|
+
);
|
|
363
|
+
query.status = options.status;
|
|
364
|
+
}
|
|
365
|
+
if (options["api-id"] !== undefined) {
|
|
366
|
+
if (
|
|
367
|
+
!/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i.test(
|
|
368
|
+
options["api-id"],
|
|
369
|
+
)
|
|
370
|
+
)
|
|
371
|
+
throw new CLIError("INVALID_INPUT", "api-id must be a UUID");
|
|
372
|
+
query.apiId = options["api-id"];
|
|
373
|
+
}
|
|
374
|
+
if (options["review-required"] !== undefined)
|
|
375
|
+
throw new CLIError(
|
|
376
|
+
"UNSUPPORTED_FILTER",
|
|
377
|
+
"--review-required is not supported: usage logs do not record review state",
|
|
378
|
+
);
|
|
379
|
+
return query;
|
|
380
|
+
}
|
|
381
|
+
export async function runCommand(args, ctx) {
|
|
382
|
+
const { options, http, store } = ctx;
|
|
383
|
+
const command = args[0];
|
|
384
|
+
const modelPreview =
|
|
385
|
+
command === "models" && args[1] === "run" && options["dry-run"];
|
|
386
|
+
if (options["dry-run"] && !modelPreview)
|
|
387
|
+
throw new CLIError(
|
|
388
|
+
"INVALID_INPUT",
|
|
389
|
+
"--dry-run is only valid for models run",
|
|
390
|
+
);
|
|
391
|
+
if (command === "logout") {
|
|
392
|
+
if (args.length !== 1 || Object.keys(options).some(key => !["json", "output"].includes(key)))
|
|
393
|
+
throw new CLIError("INVALID_INPUT", "Use logout [-j] without account selectors or other options");
|
|
394
|
+
return store.backupAndLogout();
|
|
395
|
+
}
|
|
396
|
+
if (command === "setup") {
|
|
397
|
+
const { runSetup } = await import("./setup.js");
|
|
398
|
+
return runSetup(ctx);
|
|
399
|
+
}
|
|
400
|
+
if (command === "status") return configurationStatus(store, options);
|
|
401
|
+
if (
|
|
402
|
+
command === "keys" &&
|
|
403
|
+
!["remote", "create", "rotate", "revoke"].includes(args[1])
|
|
404
|
+
)
|
|
405
|
+
return localKeys(args, ctx);
|
|
406
|
+
if (command === "auth" || command === "keys") {
|
|
407
|
+
const { runAuth } = await import("./auth.js");
|
|
408
|
+
return runAuth(args, ctx);
|
|
409
|
+
}
|
|
410
|
+
if (command === "provider")
|
|
411
|
+
throw new CLIError("UNKNOWN_COMMAND", "Unknown command: provider. Use openany-dev seller instead.");
|
|
412
|
+
if (command === "seller") {
|
|
413
|
+
const { runProvider } = await import("./provider.js");
|
|
414
|
+
return runProvider(args, ctx);
|
|
415
|
+
}
|
|
416
|
+
if (command === "topups") {
|
|
417
|
+
const { runTopups } = await import("./topups.js");
|
|
418
|
+
return runTopups(args, ctx);
|
|
419
|
+
}
|
|
420
|
+
const publicAllowed =
|
|
421
|
+
options.public &&
|
|
422
|
+
(["collections", "discover", "inspect"].includes(command) ||
|
|
423
|
+
(command === "models" &&
|
|
424
|
+
(["prices", "playgrounds", "parameters", "examples"].includes(
|
|
425
|
+
args[1],
|
|
426
|
+
) ||
|
|
427
|
+
modelPreview)));
|
|
428
|
+
if (options.public && !publicAllowed)
|
|
429
|
+
throw new CLIError(
|
|
430
|
+
"INVALID_INPUT",
|
|
431
|
+
"--public is only valid for public catalog browsing",
|
|
432
|
+
);
|
|
433
|
+
if (!publicAllowed && !modelPreview && !http.apiKey)
|
|
434
|
+
throw new CLIError(
|
|
435
|
+
"CONFIG_REQUIRED",
|
|
436
|
+
"Configure OPENANY_DEV_API_KEY before making requests",
|
|
437
|
+
{ missing: ["OPENANY_DEV_API_KEY"] },
|
|
438
|
+
);
|
|
439
|
+
if (command === "balance") {
|
|
440
|
+
const account = unwrap(
|
|
441
|
+
await http.backend("/api/v1/agent-api-account", { auth: "apiKey" }),
|
|
442
|
+
);
|
|
443
|
+
const availableUsd = formatMicroUsd(account?.availableMicroUsd);
|
|
444
|
+
return {
|
|
445
|
+
...account,
|
|
446
|
+
availableUsd,
|
|
447
|
+
availableUsdFormatted: `$${availableUsd}`,
|
|
448
|
+
};
|
|
449
|
+
}
|
|
450
|
+
if (command === "collections" || command === "discover")
|
|
451
|
+
return unwrap(
|
|
452
|
+
await http.backend(
|
|
453
|
+
command === "collections"
|
|
454
|
+
? "/api/v1/agent-api-collections"
|
|
455
|
+
: "/api/v1/agent-apis",
|
|
456
|
+
{ query: listQuery(options, command === "collections") },
|
|
457
|
+
),
|
|
458
|
+
);
|
|
459
|
+
if (command === "inspect")
|
|
460
|
+
return unwrap(
|
|
461
|
+
await http.backend(
|
|
462
|
+
`/api/v1/agent-apis/${encodeURIComponent(requireValue(options.id ?? args[1], "--id"))}`,
|
|
463
|
+
),
|
|
464
|
+
);
|
|
465
|
+
if (command === "run") return runApi(options, ctx);
|
|
466
|
+
if (command === "tasks") return runTasks(args, ctx);
|
|
467
|
+
if (command === "calls") {
|
|
468
|
+
const action = args[1] ?? "list";
|
|
469
|
+
if (!["get", "list"].includes(action))
|
|
470
|
+
throw new CLIError("UNKNOWN_COMMAND", "Calls support only list and get");
|
|
471
|
+
const path =
|
|
472
|
+
action === "get"
|
|
473
|
+
? `/${encodeURIComponent(requireValue(options.id ?? args[2], "--id"))}`
|
|
474
|
+
: "";
|
|
475
|
+
return unwrap(
|
|
476
|
+
await http.backend(`/api/v1/agent-api-calls${path}`, {
|
|
477
|
+
auth: "apiKey",
|
|
478
|
+
query: action === "list" ? callQuery(options) : undefined,
|
|
479
|
+
}),
|
|
480
|
+
);
|
|
481
|
+
}
|
|
482
|
+
if (command === "models") return runModels(args, ctx);
|
|
483
|
+
throw new CLIError("UNKNOWN_COMMAND", `Unknown command: ${command}`);
|
|
484
|
+
}
|
|
485
|
+
export async function main(
|
|
486
|
+
argv,
|
|
487
|
+
{
|
|
488
|
+
env = process.env,
|
|
489
|
+
fetch = globalThis.fetch,
|
|
490
|
+
stdout = process.stdout,
|
|
491
|
+
stderr = process.stderr,
|
|
492
|
+
stdin = process.stdin,
|
|
493
|
+
} = {},
|
|
494
|
+
) {
|
|
495
|
+
const secrets = [
|
|
496
|
+
env.OPENANY_DEV_API_KEY,
|
|
497
|
+
env.OPENANY_DEV_UPSTREAM_API_KEY,
|
|
498
|
+
env.OPENANY_DEV_ACCESS_TOKEN,
|
|
499
|
+
env.OPENANY_DEV_SESSION_TOKEN,
|
|
500
|
+
env.OPENANY_DEV_WALLET_PRIVATE_KEY,
|
|
501
|
+
].filter(Boolean);
|
|
502
|
+
try {
|
|
503
|
+
const { args, options } = parseArgs(argv);
|
|
504
|
+
if (options.version || options.help || !args.length) {
|
|
505
|
+
const value = options.version ? { version: packageInfo.version } : { help: HELP };
|
|
506
|
+
const text = options.json
|
|
507
|
+
? JSON.stringify(value) + "\n"
|
|
508
|
+
: options.version
|
|
509
|
+
? packageInfo.version + "\n"
|
|
510
|
+
: HELP;
|
|
511
|
+
if (options.output) {
|
|
512
|
+
await writeFile(options.output, text, { flag: "wx", mode: 0o600 });
|
|
513
|
+
stdout.write(JSON.stringify({ output: options.output }) + "\n");
|
|
514
|
+
} else stdout.write(text);
|
|
515
|
+
return 0;
|
|
516
|
+
}
|
|
517
|
+
if (options.output) {
|
|
518
|
+
try {
|
|
519
|
+
await lstat(options.output);
|
|
520
|
+
throw new CLIError(
|
|
521
|
+
"OUTPUT_EXISTS",
|
|
522
|
+
"Output file already exists; choose a new path",
|
|
523
|
+
);
|
|
524
|
+
} catch (error) {
|
|
525
|
+
if (error.code !== "ENOENT") throw error;
|
|
526
|
+
}
|
|
527
|
+
}
|
|
528
|
+
const store = new ConfigStore(env);
|
|
529
|
+
const state = await store.read();
|
|
530
|
+
secrets.push(
|
|
531
|
+
...state.keys.map((key) => key.key),
|
|
532
|
+
state.settings?.OPENANY_DEV_WALLET_PRIVATE_KEY,
|
|
533
|
+
);
|
|
534
|
+
env = await store.getEnvironment();
|
|
535
|
+
const apiKey = await store.getApiKey();
|
|
536
|
+
if (apiKey) secrets.push(apiKey);
|
|
537
|
+
const http = new HttpClient({ fetch, apiKey });
|
|
538
|
+
const ctx = {
|
|
539
|
+
args,
|
|
540
|
+
options,
|
|
541
|
+
env,
|
|
542
|
+
store,
|
|
543
|
+
http,
|
|
544
|
+
stdin,
|
|
545
|
+
stdout,
|
|
546
|
+
stderr,
|
|
547
|
+
secrets,
|
|
548
|
+
jsonOption: (name, fallback) =>
|
|
549
|
+
options[name] === undefined
|
|
550
|
+
? fallback
|
|
551
|
+
: parseJson(options[name], `--${name}`),
|
|
552
|
+
input: async () => {
|
|
553
|
+
if (options.input !== undefined && options.file !== undefined)
|
|
554
|
+
throw new CLIError("INVALID_INPUT", "Choose --input or --file");
|
|
555
|
+
return options.file !== undefined
|
|
556
|
+
? parseJson(await readFile(options.file, "utf8"), "Input file")
|
|
557
|
+
: options.input !== undefined
|
|
558
|
+
? parseJson(options.input, "--input")
|
|
559
|
+
: undefined;
|
|
560
|
+
},
|
|
561
|
+
};
|
|
562
|
+
let result = await runCommand(args, ctx);
|
|
563
|
+
let bytes;
|
|
564
|
+
let responseMetadata = {};
|
|
565
|
+
if (result?.headers instanceof Headers) {
|
|
566
|
+
responseMetadata = {
|
|
567
|
+
status: result.status,
|
|
568
|
+
requestId: result.requestId,
|
|
569
|
+
contentType: result.contentType,
|
|
570
|
+
};
|
|
571
|
+
bytes = result.body instanceof Uint8Array ? result.body : undefined;
|
|
572
|
+
result = bytes ?? {
|
|
573
|
+
status: result.status,
|
|
574
|
+
requestId: result.requestId,
|
|
575
|
+
contentType: result.contentType,
|
|
576
|
+
body: result.body,
|
|
577
|
+
};
|
|
578
|
+
}
|
|
579
|
+
if (bytes && !options.output) {
|
|
580
|
+
const directory = await mkdtemp(join(tmpdir(), "openany-result-"));
|
|
581
|
+
const output = join(directory, "response.bin");
|
|
582
|
+
await writeFile(output, bytes, { flag: "wx", mode: 0o600 });
|
|
583
|
+
stdout.write(
|
|
584
|
+
JSON.stringify({
|
|
585
|
+
output,
|
|
586
|
+
savedAutomatically: true,
|
|
587
|
+
...responseMetadata,
|
|
588
|
+
}) + "\n",
|
|
589
|
+
);
|
|
590
|
+
return 0;
|
|
591
|
+
}
|
|
592
|
+
const content =
|
|
593
|
+
bytes ?? redact(result, secrets, options.json ? 0 : 2) + "\n";
|
|
594
|
+
if (options.output) {
|
|
595
|
+
await writeFile(options.output, content, { flag: "wx", mode: 0o600 });
|
|
596
|
+
stdout.write(
|
|
597
|
+
JSON.stringify({ output: options.output, ...responseMetadata }) + "\n",
|
|
598
|
+
);
|
|
599
|
+
} else stdout.write(content);
|
|
600
|
+
return 0;
|
|
601
|
+
} catch (error) {
|
|
602
|
+
stderr.write(redact(errorRecord(error), secrets) + "\n");
|
|
603
|
+
return 1;
|
|
604
|
+
}
|
|
605
|
+
}
|