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/catalog.js
ADDED
|
@@ -0,0 +1,306 @@
|
|
|
1
|
+
import { CLIError, requireValue } from "./errors.js";
|
|
2
|
+
import { safePath, unwrap } from "./http.js";
|
|
3
|
+
export const isObject = (value) =>
|
|
4
|
+
value !== null && typeof value === "object" && !Array.isArray(value);
|
|
5
|
+
export function microUsd(value) {
|
|
6
|
+
if (typeof value !== "string" || !/^\d+$/.test(value))
|
|
7
|
+
throw new CLIError(
|
|
8
|
+
"INVALID_RESPONSE",
|
|
9
|
+
"Expected a decimal integer microUSD amount",
|
|
10
|
+
);
|
|
11
|
+
return BigInt(value);
|
|
12
|
+
}
|
|
13
|
+
export function formatMicroUsd(value) {
|
|
14
|
+
const amount = microUsd(value);
|
|
15
|
+
const fraction = (amount % 1000000n)
|
|
16
|
+
.toString()
|
|
17
|
+
.padStart(6, "0")
|
|
18
|
+
.replace(/0+$/, "")
|
|
19
|
+
.padEnd(2, "0");
|
|
20
|
+
return `${amount / 1000000n}.${fraction}`;
|
|
21
|
+
}
|
|
22
|
+
export function validateValue(value, field, name = field.name) {
|
|
23
|
+
if (value === undefined) {
|
|
24
|
+
if (field.required)
|
|
25
|
+
throw new CLIError("INVALID_INPUT", `Missing required input: ${name}`);
|
|
26
|
+
return;
|
|
27
|
+
}
|
|
28
|
+
if (value === null) {
|
|
29
|
+
if (!field.nullable)
|
|
30
|
+
throw new CLIError("INVALID_INPUT", `${name} cannot be null`);
|
|
31
|
+
return;
|
|
32
|
+
}
|
|
33
|
+
const types = Array.isArray(field.type) ? field.type : [field.type];
|
|
34
|
+
const matches = (type) =>
|
|
35
|
+
type === "integer"
|
|
36
|
+
? Number.isSafeInteger(value)
|
|
37
|
+
: type === "number"
|
|
38
|
+
? typeof value === "number" && Number.isFinite(value)
|
|
39
|
+
: type === "array"
|
|
40
|
+
? Array.isArray(value)
|
|
41
|
+
: type === "object"
|
|
42
|
+
? isObject(value)
|
|
43
|
+
: type === "file"
|
|
44
|
+
? value instanceof Blob
|
|
45
|
+
: typeof value === type;
|
|
46
|
+
if (!types.some(matches))
|
|
47
|
+
throw new CLIError("INVALID_INPUT", `Invalid type for ${name}`);
|
|
48
|
+
const minLength = field.min_length ?? field.minLength,
|
|
49
|
+
maxLength = field.max_length ?? field.maxLength;
|
|
50
|
+
if (
|
|
51
|
+
typeof value === "string" &&
|
|
52
|
+
((minLength !== undefined && [...value].length < minLength) ||
|
|
53
|
+
(maxLength !== undefined && [...value].length > maxLength))
|
|
54
|
+
)
|
|
55
|
+
throw new CLIError("INVALID_INPUT", `Invalid length for ${name}`);
|
|
56
|
+
if (
|
|
57
|
+
typeof value === "number" &&
|
|
58
|
+
((field.minimum != null && value < Number(field.minimum)) ||
|
|
59
|
+
(field.maximum != null && value > Number(field.maximum)))
|
|
60
|
+
)
|
|
61
|
+
throw new CLIError("INVALID_INPUT", `Out of range: ${name}`);
|
|
62
|
+
const choices = field.enum_values ?? field.enumValues;
|
|
63
|
+
const sameKind = (item) =>
|
|
64
|
+
Array.isArray(item)
|
|
65
|
+
? Array.isArray(value)
|
|
66
|
+
: item === null
|
|
67
|
+
? value === null
|
|
68
|
+
: typeof item === typeof value && !Array.isArray(value);
|
|
69
|
+
if (
|
|
70
|
+
Array.isArray(choices) &&
|
|
71
|
+
choices.length &&
|
|
72
|
+
(!Array.isArray(field.type) || choices.some(sameKind)) &&
|
|
73
|
+
!choices.some((v) => JSON.stringify(v) === JSON.stringify(value))
|
|
74
|
+
)
|
|
75
|
+
throw new CLIError("INVALID_INPUT", `Invalid enum value for ${name}`);
|
|
76
|
+
const itemType = field.item_type ?? field.itemType;
|
|
77
|
+
if (Array.isArray(value) && itemType)
|
|
78
|
+
for (const item of value)
|
|
79
|
+
validateValue(item, { type: itemType, nullable: false }, `${name}[]`);
|
|
80
|
+
}
|
|
81
|
+
function pointerTokens(pointer) {
|
|
82
|
+
if (pointer === "") return [];
|
|
83
|
+
if (
|
|
84
|
+
typeof pointer !== "string" ||
|
|
85
|
+
!pointer.startsWith("/") ||
|
|
86
|
+
/~[^01]/.test(pointer)
|
|
87
|
+
)
|
|
88
|
+
throw new CLIError("INVALID_CONTRACT", "Invalid body JSON pointer");
|
|
89
|
+
return pointer
|
|
90
|
+
.slice(1)
|
|
91
|
+
.split("/")
|
|
92
|
+
.map((v) => v.replaceAll("~1", "/").replaceAll("~0", "~"));
|
|
93
|
+
}
|
|
94
|
+
function readPointer(body, tokens) {
|
|
95
|
+
let value = body;
|
|
96
|
+
for (const token of tokens) {
|
|
97
|
+
if (
|
|
98
|
+
value === null ||
|
|
99
|
+
typeof value !== "object" ||
|
|
100
|
+
!Object.hasOwn(value, token)
|
|
101
|
+
)
|
|
102
|
+
return undefined;
|
|
103
|
+
value = value[token];
|
|
104
|
+
}
|
|
105
|
+
return value;
|
|
106
|
+
}
|
|
107
|
+
const scalar = (v) =>
|
|
108
|
+
v !== null && typeof v === "object" ? JSON.stringify(v) : String(v ?? "");
|
|
109
|
+
export function prepareApiRequest(
|
|
110
|
+
definition,
|
|
111
|
+
{ path = {}, query = {}, headers = {}, body } = {},
|
|
112
|
+
) {
|
|
113
|
+
safePath(definition.publicPath, "/v1/agent-apis/");
|
|
114
|
+
if (
|
|
115
|
+
!["GET", "POST", "PUT", "PATCH", "DELETE", "HEAD"].includes(
|
|
116
|
+
definition.method,
|
|
117
|
+
) ||
|
|
118
|
+
!Array.isArray(definition.inputs)
|
|
119
|
+
)
|
|
120
|
+
throw new CLIError("INVALID_CONTRACT", "Invalid API request contract");
|
|
121
|
+
if (
|
|
122
|
+
!["none", "application/json", "application/x-www-form-urlencoded"].includes(
|
|
123
|
+
definition.requestContentType,
|
|
124
|
+
)
|
|
125
|
+
)
|
|
126
|
+
throw new CLIError("INVALID_CONTRACT", "Unsupported request content type");
|
|
127
|
+
for (const [name, value] of Object.entries({ path, query, headers }))
|
|
128
|
+
if (!isObject(value))
|
|
129
|
+
throw new CLIError("INVALID_INPUT", `${name} must be a JSON object`);
|
|
130
|
+
if (
|
|
131
|
+
definition.method === "GET" &&
|
|
132
|
+
definition.inputs.some((f) => f.location === "body" && f.required)
|
|
133
|
+
)
|
|
134
|
+
throw new CLIError(
|
|
135
|
+
"INVALID_CONTRACT",
|
|
136
|
+
"GET contract requires a body that the gateway cannot execute",
|
|
137
|
+
);
|
|
138
|
+
const blocked =
|
|
139
|
+
/^(authorization|proxy-authorization|cookie|host|content-type|content-length|x-workspace-id|connection|transfer-encoding|upgrade|accept-encoding)$/i;
|
|
140
|
+
for (const name of Object.keys(headers))
|
|
141
|
+
if (blocked.test(name))
|
|
142
|
+
throw new CLIError("INVALID_INPUT", `Reserved header: ${name}`);
|
|
143
|
+
const groups = { path, query, header: headers };
|
|
144
|
+
for (const [location, values] of Object.entries(groups))
|
|
145
|
+
for (const name of Object.keys(values))
|
|
146
|
+
if (
|
|
147
|
+
!(location === "header" && /^(accept|accept-language)$/i.test(name)) &&
|
|
148
|
+
!definition.inputs.some(
|
|
149
|
+
(f) =>
|
|
150
|
+
f.location === location &&
|
|
151
|
+
(location === "header"
|
|
152
|
+
? f.name.toLowerCase() === name.toLowerCase()
|
|
153
|
+
: f.name === name),
|
|
154
|
+
)
|
|
155
|
+
)
|
|
156
|
+
throw new CLIError(
|
|
157
|
+
"INVALID_INPUT",
|
|
158
|
+
`Unknown ${location} input: ${name}`,
|
|
159
|
+
);
|
|
160
|
+
const bodyFields = [];
|
|
161
|
+
for (const field of definition.inputs) {
|
|
162
|
+
let value;
|
|
163
|
+
if (field.location === "body") {
|
|
164
|
+
const tokens = pointerTokens(
|
|
165
|
+
field.pointer ??
|
|
166
|
+
`/${field.name.replaceAll("~", "~0").replaceAll("/", "~1")}`,
|
|
167
|
+
);
|
|
168
|
+
bodyFields.push({ tokens, field });
|
|
169
|
+
value = readPointer(body, tokens);
|
|
170
|
+
} else {
|
|
171
|
+
if (!Object.hasOwn(groups, field.location))
|
|
172
|
+
throw new CLIError("INVALID_CONTRACT", "Unsupported input location");
|
|
173
|
+
const group = groups[field.location];
|
|
174
|
+
const name =
|
|
175
|
+
field.location === "header"
|
|
176
|
+
? Object.keys(group).find(
|
|
177
|
+
(n) => n.toLowerCase() === field.name.toLowerCase(),
|
|
178
|
+
)
|
|
179
|
+
: field.name;
|
|
180
|
+
value = group[name];
|
|
181
|
+
}
|
|
182
|
+
validateValue(value, field);
|
|
183
|
+
}
|
|
184
|
+
// A declared object/array owns its subtree; otherwise reject fields absent from the published contract.
|
|
185
|
+
function checkBody(value, tokens = []) {
|
|
186
|
+
if (
|
|
187
|
+
bodyFields.some(
|
|
188
|
+
(f) =>
|
|
189
|
+
f.tokens.length === tokens.length &&
|
|
190
|
+
f.tokens.every((t, i) => t === tokens[i]),
|
|
191
|
+
)
|
|
192
|
+
)
|
|
193
|
+
return;
|
|
194
|
+
if (value !== null && typeof value === "object") {
|
|
195
|
+
for (const key of Object.keys(value)) {
|
|
196
|
+
const next = [...tokens, key];
|
|
197
|
+
if (!bodyFields.some((f) => next.every((t, i) => f.tokens[i] === t)))
|
|
198
|
+
throw new CLIError(
|
|
199
|
+
"INVALID_INPUT",
|
|
200
|
+
`Unknown body input: /${next.join("/")}`,
|
|
201
|
+
);
|
|
202
|
+
checkBody(value[key], next);
|
|
203
|
+
}
|
|
204
|
+
} else if (value !== undefined)
|
|
205
|
+
throw new CLIError(
|
|
206
|
+
"INVALID_INPUT",
|
|
207
|
+
"Body is absent from the published contract",
|
|
208
|
+
);
|
|
209
|
+
}
|
|
210
|
+
checkBody(body);
|
|
211
|
+
let gatewayPath = definition.publicPath.replace(/\{([^}]+)\}/g, (_, name) => {
|
|
212
|
+
const value = requireValue(path[name], `path.${name}`);
|
|
213
|
+
const raw = scalar(value);
|
|
214
|
+
if (raw === "." || raw === ".." || /[\\/%\u0000-\u001f]/.test(raw))
|
|
215
|
+
throw new CLIError("UNSAFE_PATH", `Unsafe path input: ${name}`);
|
|
216
|
+
return encodeURIComponent(raw);
|
|
217
|
+
});
|
|
218
|
+
if (/[{}]/.test(gatewayPath))
|
|
219
|
+
throw new CLIError("INVALID_INPUT", "Unresolved path parameter");
|
|
220
|
+
safePath(gatewayPath, "/v1/agent-apis/");
|
|
221
|
+
const result = {
|
|
222
|
+
path: gatewayPath,
|
|
223
|
+
method: definition.method,
|
|
224
|
+
query,
|
|
225
|
+
headers: { ...headers },
|
|
226
|
+
auth: "apiKey",
|
|
227
|
+
};
|
|
228
|
+
if (
|
|
229
|
+
body !== undefined &&
|
|
230
|
+
(definition.method === "GET" ||
|
|
231
|
+
definition.method === "HEAD" ||
|
|
232
|
+
definition.requestContentType === "none")
|
|
233
|
+
)
|
|
234
|
+
throw new CLIError("INVALID_INPUT", "This contract does not accept a body");
|
|
235
|
+
if (body !== undefined) {
|
|
236
|
+
if (definition.requestContentType === "application/json")
|
|
237
|
+
result.json = body;
|
|
238
|
+
else {
|
|
239
|
+
if (!isObject(body))
|
|
240
|
+
throw new CLIError("INVALID_INPUT", "Form body must be an object");
|
|
241
|
+
result.headers["Content-Type"] = "application/x-www-form-urlencoded";
|
|
242
|
+
const form = new URLSearchParams();
|
|
243
|
+
for (const [key, value] of Object.entries(body))
|
|
244
|
+
for (const item of Array.isArray(value) ? value : [value])
|
|
245
|
+
form.append(key, scalar(item));
|
|
246
|
+
result.body = form.toString();
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
return result;
|
|
250
|
+
}
|
|
251
|
+
export function listQuery(options, collection = false) {
|
|
252
|
+
const query = {};
|
|
253
|
+
for (const key of [
|
|
254
|
+
"search",
|
|
255
|
+
"limit",
|
|
256
|
+
"cursor",
|
|
257
|
+
"category",
|
|
258
|
+
...(collection ? [] : ["dataPlatform", "provider", "method"]),
|
|
259
|
+
]) {
|
|
260
|
+
const value =
|
|
261
|
+
options[key] ??
|
|
262
|
+
(key === "dataPlatform" ? options["data-platform"] : undefined);
|
|
263
|
+
if (value !== undefined) query[key] = value;
|
|
264
|
+
}
|
|
265
|
+
if (
|
|
266
|
+
query.limit !== undefined &&
|
|
267
|
+
(!/^\d+$/.test(query.limit) ||
|
|
268
|
+
Number(query.limit) < 1 ||
|
|
269
|
+
Number(query.limit) > 100)
|
|
270
|
+
)
|
|
271
|
+
throw new CLIError("INVALID_INPUT", "limit must be between 1 and 100");
|
|
272
|
+
return query;
|
|
273
|
+
}
|
|
274
|
+
export async function runApi(options, ctx) {
|
|
275
|
+
const id = requireValue(options.id, "--id");
|
|
276
|
+
const definition = unwrap(
|
|
277
|
+
await ctx.http.backend(`/api/v1/agent-apis/${encodeURIComponent(id)}`),
|
|
278
|
+
);
|
|
279
|
+
const prepared = prepareApiRequest(definition, {
|
|
280
|
+
path: ctx.jsonOption("path", {}),
|
|
281
|
+
query: ctx.jsonOption("query", {}),
|
|
282
|
+
headers: ctx.jsonOption("headers", {}),
|
|
283
|
+
body: await ctx.input(),
|
|
284
|
+
});
|
|
285
|
+
const account = unwrap(
|
|
286
|
+
await ctx.http.backend("/api/v1/agent-api-account", { auth: "apiKey" }),
|
|
287
|
+
);
|
|
288
|
+
const available = microUsd(account.availableMicroUsd);
|
|
289
|
+
if (definition.billing?.mode === "fixed") {
|
|
290
|
+
if (available < microUsd(definition.billing.priceMicroUsd))
|
|
291
|
+
throw new CLIError(
|
|
292
|
+
"INSUFFICIENT_BALANCE",
|
|
293
|
+
"Available balance is below the fixed call price",
|
|
294
|
+
{
|
|
295
|
+
availableMicroUsd: account.availableMicroUsd,
|
|
296
|
+
priceMicroUsd: definition.billing.priceMicroUsd,
|
|
297
|
+
},
|
|
298
|
+
);
|
|
299
|
+
} else if (available <= 0n)
|
|
300
|
+
throw new CLIError(
|
|
301
|
+
"INSUFFICIENT_BALANCE",
|
|
302
|
+
"Available balance must be positive",
|
|
303
|
+
);
|
|
304
|
+
const { path, ...request } = prepared;
|
|
305
|
+
return ctx.http.gateway(path, request);
|
|
306
|
+
}
|