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
|
@@ -0,0 +1,402 @@
|
|
|
1
|
+
import { createHash } from "node:crypto";
|
|
2
|
+
import { readFile } from "node:fs/promises";
|
|
3
|
+
import { basename, extname } from "node:path";
|
|
4
|
+
import { CLIError } from "./errors.js";
|
|
5
|
+
import { isObject } from "./catalog.js";
|
|
6
|
+
import { parseCurlExamples } from "./model-curl.js";
|
|
7
|
+
import { modelUrl } from "./http.js";
|
|
8
|
+
import { resolveModelExamples } from "./model-materials.js";
|
|
9
|
+
import { validateModelParameters } from "./model-validation.js";
|
|
10
|
+
|
|
11
|
+
const placeholders = (name) => new Set([
|
|
12
|
+
`{${name}}`,
|
|
13
|
+
`<${name}>`,
|
|
14
|
+
`YOUR_${name.toUpperCase()}`,
|
|
15
|
+
`<${name}-for-this-model>`,
|
|
16
|
+
]);
|
|
17
|
+
const MODEL_PLACEHOLDERS = placeholders("model");
|
|
18
|
+
const PROVIDER_PLACEHOLDERS = placeholders("provider");
|
|
19
|
+
const hash = (value) =>
|
|
20
|
+
createHash("sha256").update(JSON.stringify(value)).digest("hex").slice(0, 16);
|
|
21
|
+
const fail = (code, message, details) => {
|
|
22
|
+
throw new CLIError(code, message, details);
|
|
23
|
+
};
|
|
24
|
+
function bindProvider(payload, provider, source = false, explicit = false) {
|
|
25
|
+
if (payload.route !== undefined && !isObject(payload.route))
|
|
26
|
+
fail("INVALID_INPUT", "route must be an object");
|
|
27
|
+
const value = payload.route?.provider;
|
|
28
|
+
if (value !== undefined && !PROVIDER_PLACEHOLDERS.has(value) && value !== provider)
|
|
29
|
+
fail(
|
|
30
|
+
source ? "EXAMPLE_PROVIDER_MISMATCH" : "INVALID_INPUT",
|
|
31
|
+
"Request provider does not match the selected catalog provider",
|
|
32
|
+
{ provider, requestProvider: value },
|
|
33
|
+
);
|
|
34
|
+
if (explicit) payload.route = { ...payload.route, provider };
|
|
35
|
+
else if (PROVIDER_PLACEHOLDERS.has(value)) {
|
|
36
|
+
delete payload.route.provider;
|
|
37
|
+
if (!Object.keys(payload.route).length) delete payload.route;
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
function checkModel(payload, model, pathModel, source = false) {
|
|
41
|
+
if (MODEL_PLACEHOLDERS.has(payload.model)) payload.model = model;
|
|
42
|
+
if (payload.model !== undefined && payload.model !== model)
|
|
43
|
+
fail(
|
|
44
|
+
source ? "EXAMPLE_MODEL_MISMATCH" : "INVALID_INPUT",
|
|
45
|
+
"Request model does not match --model",
|
|
46
|
+
{ model, requestModel: payload.model },
|
|
47
|
+
);
|
|
48
|
+
if (!pathModel && payload.model === undefined)
|
|
49
|
+
fail(
|
|
50
|
+
source ? "EXAMPLE_MODEL_REQUIRED" : "INPUT_REQUIRED",
|
|
51
|
+
"Request body must explicitly include model",
|
|
52
|
+
);
|
|
53
|
+
}
|
|
54
|
+
function checkExample(request, row, explicitProvider) {
|
|
55
|
+
const { path } = modelUrl(request.url);
|
|
56
|
+
if (/\/audio(?:s)?\/transcriptions\/?$/.test(path))
|
|
57
|
+
fail(
|
|
58
|
+
"MODEL_OPERATION_UNSUPPORTED",
|
|
59
|
+
"Audio transcription is not supported by models run",
|
|
60
|
+
);
|
|
61
|
+
const pathParameters = [
|
|
62
|
+
...new Set(
|
|
63
|
+
[...path.matchAll(/\{([A-Za-z_][A-Za-z_0-9]*)\}/g)].map((m) => m[1]),
|
|
64
|
+
),
|
|
65
|
+
];
|
|
66
|
+
if (
|
|
67
|
+
/[{}]/.test(path.replace(/\{[A-Za-z_][A-Za-z_0-9]*\}/g, "")) ||
|
|
68
|
+
path.includes("YOUR_TASK_ID") ||
|
|
69
|
+
/[<>]/.test(path)
|
|
70
|
+
)
|
|
71
|
+
fail(
|
|
72
|
+
"UNRESOLVED_PLACEHOLDER",
|
|
73
|
+
"Use explicit path placeholders such as {task_id}",
|
|
74
|
+
);
|
|
75
|
+
if (request.json) {
|
|
76
|
+
checkModel(request.json, row.model, pathParameters.includes("model"), true);
|
|
77
|
+
bindProvider(request.json, row.provider, true, Boolean(explicitProvider));
|
|
78
|
+
}
|
|
79
|
+
if (request.form) {
|
|
80
|
+
if (!/^\/v1\/images\/edits?$/.test(path))
|
|
81
|
+
fail(
|
|
82
|
+
"MODEL_OPERATION_UNSUPPORTED",
|
|
83
|
+
"Multipart execution currently supports image edits only",
|
|
84
|
+
);
|
|
85
|
+
const routes = request.form.filter((field) => field.name === "route");
|
|
86
|
+
if (routes.length > 1 || routes.some((field) => field.file))
|
|
87
|
+
fail(
|
|
88
|
+
"EXAMPLE_PROVIDER_MISMATCH",
|
|
89
|
+
"Multipart route must be one JSON object",
|
|
90
|
+
);
|
|
91
|
+
const payload = {};
|
|
92
|
+
if (routes.length) {
|
|
93
|
+
try {
|
|
94
|
+
payload.route = JSON.parse(routes[0].value);
|
|
95
|
+
} catch {
|
|
96
|
+
fail("INVALID_CURL_JSON", "Multipart route must contain valid JSON");
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
bindProvider(payload, row.provider, true, Boolean(explicitProvider));
|
|
100
|
+
if (routes.length && payload.route === undefined)
|
|
101
|
+
request.form = request.form.filter((field) => field.name !== "route");
|
|
102
|
+
else if (routes.length) routes[0].value = JSON.stringify(payload.route);
|
|
103
|
+
else if (payload.route !== undefined)
|
|
104
|
+
request.form.push({
|
|
105
|
+
name: "route",
|
|
106
|
+
value: JSON.stringify(payload.route),
|
|
107
|
+
});
|
|
108
|
+
const models = request.form.filter((field) => field.name === "model");
|
|
109
|
+
if (models.length > 1 || models.some((field) => field.file))
|
|
110
|
+
fail(
|
|
111
|
+
"EXAMPLE_MODEL_MISMATCH",
|
|
112
|
+
"Multipart example must have one scalar model",
|
|
113
|
+
);
|
|
114
|
+
if (models.length && MODEL_PLACEHOLDERS.has(models[0].value))
|
|
115
|
+
models[0].value = row.model;
|
|
116
|
+
checkModel(
|
|
117
|
+
{ ...(models.length ? { model: models[0].value } : {}) },
|
|
118
|
+
row.model,
|
|
119
|
+
pathParameters.includes("model"),
|
|
120
|
+
true,
|
|
121
|
+
);
|
|
122
|
+
}
|
|
123
|
+
return pathParameters;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
export function modelExamples(rows, model, provider, playgrounds = []) {
|
|
127
|
+
if (!Array.isArray(playgrounds))
|
|
128
|
+
fail("INVALID_RESPONSE", "Model playgrounds response must be an array");
|
|
129
|
+
if (!Array.isArray(rows))
|
|
130
|
+
fail("INVALID_RESPONSE", "Model prices response must be an array");
|
|
131
|
+
const matching = rows.filter(
|
|
132
|
+
(row) => row.model === model && row.active !== false,
|
|
133
|
+
);
|
|
134
|
+
if (!matching.length)
|
|
135
|
+
fail("MODEL_NOT_FOUND", "No active catalog records match --model", {
|
|
136
|
+
model,
|
|
137
|
+
});
|
|
138
|
+
const providers = [...new Set(matching.map((row) => row.provider))];
|
|
139
|
+
if (providers.some((value) => typeof value !== "string" || !value.trim()))
|
|
140
|
+
fail("INVALID_RESPONSE", "Catalog record is missing its provider");
|
|
141
|
+
if (provider !== undefined && !providers.includes(provider))
|
|
142
|
+
fail("PROVIDER_NOT_FOUND", "No catalog record matches --provider", {
|
|
143
|
+
providers,
|
|
144
|
+
});
|
|
145
|
+
const examples = [];
|
|
146
|
+
for (const row of matching.filter(
|
|
147
|
+
(row) => provider === undefined || row.provider === provider,
|
|
148
|
+
)) {
|
|
149
|
+
const platforms = playgrounds.filter((item) =>
|
|
150
|
+
item.model === model && item.active !== false &&
|
|
151
|
+
(!item.provider || item.provider === row.provider) && item.curlExample?.trim(),
|
|
152
|
+
);
|
|
153
|
+
const resolved = resolveModelExamples(row, playgrounds);
|
|
154
|
+
const parsed = resolved ?? platforms.flatMap((item) => {
|
|
155
|
+
const examples = parseCurlExamples(item.curlExample);
|
|
156
|
+
if (!examples.length) examples.push({
|
|
157
|
+
index: 1,
|
|
158
|
+
line: 1,
|
|
159
|
+
error: {
|
|
160
|
+
code: "EXAMPLE_NOT_FOUND",
|
|
161
|
+
message: "Playground curlExample contains no request",
|
|
162
|
+
},
|
|
163
|
+
});
|
|
164
|
+
return examples.map((example) => ({
|
|
165
|
+
...example,
|
|
166
|
+
source: "playground",
|
|
167
|
+
playgroundId: item.id,
|
|
168
|
+
}));
|
|
169
|
+
});
|
|
170
|
+
const endpointKey = (example) => {
|
|
171
|
+
const { path } = modelUrl(example.request.url);
|
|
172
|
+
return `${example.request.method}:${path
|
|
173
|
+
.replaceAll("{model}", encodeURIComponent(row.model))
|
|
174
|
+
.replaceAll("{provider}", encodeURIComponent(row.provider))
|
|
175
|
+
.replace(/\/+$/, "")
|
|
176
|
+
.replace(/\/images\/generations$/, "/images")
|
|
177
|
+
.replace(/\/images\/edits$/, "/images/edit")}`;
|
|
178
|
+
};
|
|
179
|
+
// A broken primary template must remain visible, never trigger fallback.
|
|
180
|
+
const hasPlayground = parsed.length > 0;
|
|
181
|
+
if (resolved === null && !parsed.some((example) => example.error)) {
|
|
182
|
+
const covered = new Set(parsed.map(endpointKey));
|
|
183
|
+
for (const example of parseCurlExamples(row.curlExample)) {
|
|
184
|
+
if (!hasPlayground || (!example.error && !covered.has(endpointKey(example))))
|
|
185
|
+
parsed.push({ ...example, source: "price" });
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
if (!parsed.length)
|
|
189
|
+
parsed.push({
|
|
190
|
+
index: 1,
|
|
191
|
+
line: 1,
|
|
192
|
+
source: "price",
|
|
193
|
+
error: {
|
|
194
|
+
code: "EXAMPLE_NOT_FOUND",
|
|
195
|
+
message: "Catalog record has no curlExample",
|
|
196
|
+
},
|
|
197
|
+
});
|
|
198
|
+
for (const example of parsed) {
|
|
199
|
+
const id = `${row.id ?? row.routeId ?? hash([row.model, row.provider])}:${hash([example.source, example.playgroundId, example.index, example.request ?? platforms.find((item) => item.id === example.playgroundId)?.curlExample ?? row.curlExample])}`;
|
|
200
|
+
const result = {
|
|
201
|
+
id,
|
|
202
|
+
priceId: row.id,
|
|
203
|
+
routeId: row.routeId,
|
|
204
|
+
model,
|
|
205
|
+
provider: row.provider,
|
|
206
|
+
...example,
|
|
207
|
+
};
|
|
208
|
+
try {
|
|
209
|
+
if (!result.error)
|
|
210
|
+
result.pathParameters = checkExample(result.request, row, provider);
|
|
211
|
+
} catch (error) {
|
|
212
|
+
if (!(error instanceof CLIError)) throw error;
|
|
213
|
+
result.error = {
|
|
214
|
+
code: error.code,
|
|
215
|
+
message: error.message,
|
|
216
|
+
line: example.line,
|
|
217
|
+
...error.details,
|
|
218
|
+
};
|
|
219
|
+
}
|
|
220
|
+
examples.push(result);
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
return { model, providers, examples };
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
export function selectExample(catalog, options) {
|
|
227
|
+
let choices = catalog.examples;
|
|
228
|
+
if (options.example)
|
|
229
|
+
choices = choices.filter((example) => example.id === options.example);
|
|
230
|
+
if (options.endpoint)
|
|
231
|
+
choices = choices.filter(
|
|
232
|
+
(example) =>
|
|
233
|
+
example.request &&
|
|
234
|
+
modelUrl(example.request.url).path === options.endpoint,
|
|
235
|
+
);
|
|
236
|
+
if (choices.length !== 1)
|
|
237
|
+
fail(
|
|
238
|
+
choices.length ? "EXAMPLE_AMBIGUOUS" : "EXAMPLE_NOT_FOUND",
|
|
239
|
+
choices.length
|
|
240
|
+
? "Choose one --example from models examples; requests are never executed as a batch"
|
|
241
|
+
: "No matching curl example; refresh models examples",
|
|
242
|
+
{
|
|
243
|
+
examples: choices.map(({ id, provider, index, error }) => ({
|
|
244
|
+
id,
|
|
245
|
+
provider,
|
|
246
|
+
index,
|
|
247
|
+
error,
|
|
248
|
+
})),
|
|
249
|
+
},
|
|
250
|
+
);
|
|
251
|
+
const selected = choices[0];
|
|
252
|
+
if (selected.error) {
|
|
253
|
+
const { code, message, ...details } = selected.error;
|
|
254
|
+
fail(code, message, {
|
|
255
|
+
example: selected.id,
|
|
256
|
+
provider: selected.provider,
|
|
257
|
+
...details,
|
|
258
|
+
});
|
|
259
|
+
}
|
|
260
|
+
return selected;
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
const mime = (path) =>
|
|
264
|
+
({
|
|
265
|
+
".png": "image/png",
|
|
266
|
+
".jpg": "image/jpeg",
|
|
267
|
+
".jpeg": "image/jpeg",
|
|
268
|
+
".webp": "image/webp",
|
|
269
|
+
".wav": "audio/wav",
|
|
270
|
+
".mp3": "audio/mpeg",
|
|
271
|
+
})[extname(path).toLowerCase()] ?? "application/octet-stream";
|
|
272
|
+
|
|
273
|
+
export async function prepareModelRequest(example, ctx) {
|
|
274
|
+
const { options } = ctx;
|
|
275
|
+
const template = example.request;
|
|
276
|
+
const pathValues = ctx.jsonOption("path", {});
|
|
277
|
+
if (!isObject(pathValues))
|
|
278
|
+
fail("INVALID_INPUT", "--path must be a JSON object");
|
|
279
|
+
for (const name of Object.keys(pathValues))
|
|
280
|
+
if (!example.pathParameters.includes(name) || ["model", "provider"].includes(name))
|
|
281
|
+
fail("INVALID_INPUT", `Unknown or reserved path parameter: ${name}`);
|
|
282
|
+
let { origin, path, search } = modelUrl(template.url);
|
|
283
|
+
for (const name of example.pathParameters) {
|
|
284
|
+
const value = name === "model"
|
|
285
|
+
? example.model
|
|
286
|
+
: name === "provider" ? example.provider : pathValues[name];
|
|
287
|
+
if (typeof value !== "string" || !value)
|
|
288
|
+
fail(
|
|
289
|
+
"PATH_PARAMETER_REQUIRED",
|
|
290
|
+
`Provide --path with a nonempty ${name}`,
|
|
291
|
+
{ missing: [name] },
|
|
292
|
+
);
|
|
293
|
+
// Reject percent escapes so a caller cannot smuggle another decoding layer.
|
|
294
|
+
if (
|
|
295
|
+
/[\\%\u0000-\u0020]/.test(value) ||
|
|
296
|
+
value.split("/").some((part) => part === "." || part === "..")
|
|
297
|
+
)
|
|
298
|
+
fail("UNSAFE_PATH", `Unsafe path parameter: ${name}`);
|
|
299
|
+
path = path.replaceAll(`{${name}}`, encodeURIComponent(value));
|
|
300
|
+
}
|
|
301
|
+
const url = origin + path + search;
|
|
302
|
+
modelUrl(url);
|
|
303
|
+
const headers = { ...template.headers };
|
|
304
|
+
delete headers.authorization;
|
|
305
|
+
const request = { method: template.method, headers, auth: "apiKey" };
|
|
306
|
+
const preview = {
|
|
307
|
+
example: example.id,
|
|
308
|
+
provider: example.provider,
|
|
309
|
+
source: example.source,
|
|
310
|
+
method: template.method,
|
|
311
|
+
url,
|
|
312
|
+
headers: { ...headers, Authorization: "Bearer [REDACTED]" },
|
|
313
|
+
notices: template.notices,
|
|
314
|
+
};
|
|
315
|
+
const input = await ctx.input();
|
|
316
|
+
const payload = input === undefined ? undefined : structuredClone(input);
|
|
317
|
+
if (template.method === "GET") {
|
|
318
|
+
if (payload !== undefined || options.attach?.length)
|
|
319
|
+
fail(
|
|
320
|
+
"INVALID_INPUT",
|
|
321
|
+
"GET examples accept --path, not a body or attachments",
|
|
322
|
+
);
|
|
323
|
+
preview.validation = validateModelParameters(example, {});
|
|
324
|
+
return { url, request, preview, endpoint: path };
|
|
325
|
+
}
|
|
326
|
+
if (!isObject(payload))
|
|
327
|
+
fail(
|
|
328
|
+
"INPUT_REQUIRED",
|
|
329
|
+
"Supply the complete request object with --input JSON or --file FILE",
|
|
330
|
+
);
|
|
331
|
+
checkModel(payload, example.model, example.pathParameters.includes("model"));
|
|
332
|
+
if (template.json) {
|
|
333
|
+
if (options.attach?.length)
|
|
334
|
+
fail(
|
|
335
|
+
"INVALID_INPUT",
|
|
336
|
+
"This JSON example does not define multipart attachments",
|
|
337
|
+
);
|
|
338
|
+
bindProvider(payload, example.provider, false, Boolean(options.provider));
|
|
339
|
+
preview.validation = validateModelParameters(example, payload);
|
|
340
|
+
request.json = payload;
|
|
341
|
+
preview.json = payload;
|
|
342
|
+
} else {
|
|
343
|
+
bindProvider(payload, example.provider, false, Boolean(options.provider));
|
|
344
|
+
const fileNames = new Set(
|
|
345
|
+
template.form.filter((field) => field.file).map((field) => field.name),
|
|
346
|
+
);
|
|
347
|
+
const attachments = (options.attach ?? []).map((spec) => {
|
|
348
|
+
const split = spec.indexOf("=");
|
|
349
|
+
if (split < 1 || split === spec.length - 1)
|
|
350
|
+
fail("INVALID_INPUT", "Attachments use --attach FIELD=FILE");
|
|
351
|
+
const name = spec.slice(0, split),
|
|
352
|
+
path = spec.slice(split + 1);
|
|
353
|
+
if (!fileNames.has(name))
|
|
354
|
+
fail(
|
|
355
|
+
"INVALID_INPUT",
|
|
356
|
+
`Attachment field is absent from the example: ${name}`,
|
|
357
|
+
);
|
|
358
|
+
return { name, path };
|
|
359
|
+
});
|
|
360
|
+
for (const name of fileNames)
|
|
361
|
+
if ((example.jsonSchema?.required?.includes(name) || example.parameterSchema?.some(f => f.path === `/${name}` && f.required)) && !attachments.some((file) => file.name === name))
|
|
362
|
+
fail(
|
|
363
|
+
"ATTACHMENT_REQUIRED",
|
|
364
|
+
`Provide --attach ${name}=FILE; example paths are never read`,
|
|
365
|
+
);
|
|
366
|
+
preview.validation = validateModelParameters(example, { ...payload, ...Object.fromEntries(attachments.map(a => [a.name, a.path])) });
|
|
367
|
+
const form = new FormData();
|
|
368
|
+
const fields = [];
|
|
369
|
+
for (const [name, value] of Object.entries(payload)) {
|
|
370
|
+
if (fileNames.has(name))
|
|
371
|
+
fail("INVALID_INPUT", `Unknown scalar multipart field: ${name}`);
|
|
372
|
+
if (name === "route") {
|
|
373
|
+
const serialized = JSON.stringify(value);
|
|
374
|
+
form.append(name, serialized);
|
|
375
|
+
fields.push({ name, value: serialized });
|
|
376
|
+
continue;
|
|
377
|
+
}
|
|
378
|
+
for (const item of Array.isArray(value) ? value : [value]) {
|
|
379
|
+
if (!["string", "number", "boolean"].includes(typeof item))
|
|
380
|
+
fail("INVALID_INPUT", `Multipart ${name} requires scalar values`);
|
|
381
|
+
form.append(name, String(item));
|
|
382
|
+
fields.push({ name, value: String(item) });
|
|
383
|
+
}
|
|
384
|
+
}
|
|
385
|
+
for (const attachment of attachments) {
|
|
386
|
+
const bytes = await readFile(attachment.path);
|
|
387
|
+
form.append(
|
|
388
|
+
attachment.name,
|
|
389
|
+
new Blob([bytes], { type: mime(attachment.path) }),
|
|
390
|
+
basename(attachment.path),
|
|
391
|
+
);
|
|
392
|
+
fields.push({
|
|
393
|
+
name: attachment.name,
|
|
394
|
+
file: attachment.path,
|
|
395
|
+
size: bytes.length,
|
|
396
|
+
});
|
|
397
|
+
}
|
|
398
|
+
request.body = form;
|
|
399
|
+
preview.form = fields;
|
|
400
|
+
}
|
|
401
|
+
return { url, request, preview, endpoint: path };
|
|
402
|
+
}
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import Ajv2020 from 'ajv/dist/2020.js';
|
|
2
|
+
import Ajv from 'ajv';
|
|
3
|
+
import {CLIError} from './errors.js';
|
|
4
|
+
const gateway = ['model','route','route_mode'];
|
|
5
|
+
const engines = [new Ajv({strict:false,strictSchema:true,allErrors:true,validateFormats:false}),new Ajv2020({strict:false,strictSchema:true,allErrors:true,validateFormats:false})];
|
|
6
|
+
// Gateway-owned fields are checked separately. Preserve all business constraints.
|
|
7
|
+
function businessSchema(schema) {
|
|
8
|
+
const s=structuredClone(schema);
|
|
9
|
+
const visited=new Set();
|
|
10
|
+
function visit(node) {
|
|
11
|
+
if(!node || typeof node!=='object' || visited.has(node)) return;
|
|
12
|
+
visited.add(node);
|
|
13
|
+
if(node.$ref?.startsWith('#/')) {
|
|
14
|
+
const ref=node.$ref.slice(2).split('/').map(p=>p.replace(/~1/g,'/').replace(/~0/g,'~'));
|
|
15
|
+
visit(ref.reduce((v,k)=>v && Object.hasOwn(v,k)?v[k]:undefined,s));
|
|
16
|
+
}
|
|
17
|
+
if(node.properties) for(const name of gateway) delete node.properties[name];
|
|
18
|
+
if(node.required) node.required=node.required.filter(n=>!gateway.includes(n));
|
|
19
|
+
for(const key of ['allOf','anyOf','oneOf']) node[key]?.forEach(visit);
|
|
20
|
+
for(const key of ['if','then','else','not']) if(node[key]) visit(node[key]);
|
|
21
|
+
}
|
|
22
|
+
visit(s);return s;
|
|
23
|
+
}
|
|
24
|
+
function check(schema, value) {
|
|
25
|
+
try {
|
|
26
|
+
const engine=schema?.$schema?.includes('2020-12') ? engines[1] : engines[0];
|
|
27
|
+
const validate=engine.compile(schema);
|
|
28
|
+
return validate(value)?[]:validate.errors.map(e=>({path:e.instancePath || '/',keyword:e.keyword,message:e.message,params:e.params}));
|
|
29
|
+
} catch(error) {
|
|
30
|
+
throw new CLIError('INVALID_PARAMETER_SCHEMA','Configured parameter schema could not be compiled',{reason:error.message});
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
export function validateModelParameters(example,payload) {
|
|
34
|
+
if(example.request.method==='GET') return {level:'path-only',notices:[]};
|
|
35
|
+
const body=Object.fromEntries(Object.entries(payload).filter(([k])=>!gateway.includes(k)));
|
|
36
|
+
let errors=[],level='basic';
|
|
37
|
+
if(example.jsonSchema!==undefined && example.jsonSchema!==null) {
|
|
38
|
+
level='json-schema';errors=check(businessSchema(example.jsonSchema),body);
|
|
39
|
+
} else if(example.parameterSchema?.length) {
|
|
40
|
+
level='field-constraints';
|
|
41
|
+
for(const field of example.parameterSchema) {
|
|
42
|
+
const parts=(field.path ?? '').split('/').slice(1).map(p=>p.replace(/~1/g,'/').replace(/~0/g,'~'));
|
|
43
|
+
if(!parts.length || gateway.includes(parts[0])) continue;
|
|
44
|
+
const value=parts.reduce((v,k)=>v && Object.hasOwn(v,k)?v[k]:undefined,body);
|
|
45
|
+
if(value===undefined) {if(field.required) errors.push({path:field.path,keyword:'required',message:'is required'});continue;}
|
|
46
|
+
const schema={};
|
|
47
|
+
const type=field.valueType || (['json','textarea','select'].includes(field.type)?undefined:field.type);
|
|
48
|
+
if(type) schema.type=type.split('|').map(t=>t.trim());
|
|
49
|
+
for(const [from,to] of [['min','minimum'],['max','maximum'],['minItems','minItems'],['maxItems','maxItems'],['minLength','minLength'],['maxLength','maxLength']]) if(field[from]!==undefined) schema[to]=field[from];
|
|
50
|
+
if(field.allowedValues?.length || field.options?.length) schema.enum=field.allowedValues?.length?field.allowedValues:field.options;
|
|
51
|
+
errors.push(...check(schema,value).map(e=>({...e,path:field.path+ (e.path==='/'?'':e.path)})));
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
if(errors.length) throw new CLIError('PARAMETER_VALIDATION_FAILED','Request parameters do not match the configured contract',{errors});
|
|
55
|
+
return {level,notices:level==='basic'?['Only basic request checks were performed; example values do not define parameter constraints.']:['JSON Schema format annotations are not validated.']};
|
|
56
|
+
}
|
package/src/models.js
ADDED
|
@@ -0,0 +1,188 @@
|
|
|
1
|
+
import { CLIError, requireValue } from "./errors.js";
|
|
2
|
+
import { unwrap } from "./http.js";
|
|
3
|
+
import { microUsd } from "./catalog.js";
|
|
4
|
+
import {
|
|
5
|
+
modelExamples,
|
|
6
|
+
selectExample,
|
|
7
|
+
prepareModelRequest,
|
|
8
|
+
} from "./model-request.js";
|
|
9
|
+
const routeQuery = (options) => ({ modelid: options.model });
|
|
10
|
+
// The public catalog also serves partner, upstream and reference prices.
|
|
11
|
+
function salePricing(row) {
|
|
12
|
+
const {
|
|
13
|
+
officialPricing, catalogPricing, promptSource, completionSource, imageCompletionSource,
|
|
14
|
+
actualPrompt, actualCompletion, partnerPriceMultiplierBps, partnerPrompt,
|
|
15
|
+
partnerCompletion, partnerCacheCreationPrompt, partnerCacheReadPrompt, ...sale
|
|
16
|
+
} = row;
|
|
17
|
+
if (Array.isArray(sale.pricingTiers)) sale.pricingTiers = sale.pricingTiers.map(salePricing);
|
|
18
|
+
if (Array.isArray(sale.timePricingRules)) sale.timePricingRules = sale.timePricingRules.map(salePricing);
|
|
19
|
+
if (sale.displayPricing) sale.displayPricing = salePricing(sale.displayPricing);
|
|
20
|
+
return sale;
|
|
21
|
+
}
|
|
22
|
+
function operationQuery(options) {
|
|
23
|
+
return {
|
|
24
|
+
modelid: requireValue(options.model, "--model"),
|
|
25
|
+
protocol: requireValue(options.protocol, "--protocol"),
|
|
26
|
+
mode: requireValue(options.mode, "--mode"),
|
|
27
|
+
...(options.endpoint ? { endpoint: options.endpoint } : {}),
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
function verifyStream(response, operation) {
|
|
31
|
+
if (!response.contentType.includes("text/event-stream")) return response;
|
|
32
|
+
const text = response.body;
|
|
33
|
+
if (typeof text !== "string")
|
|
34
|
+
throw new CLIError("INVALID_RESPONSE", "Expected text event stream");
|
|
35
|
+
let responseCompleted = false,
|
|
36
|
+
chatCompleted = false,
|
|
37
|
+
messageStopped = false;
|
|
38
|
+
for (const block of text.split(/\r?\n\r?\n/)) {
|
|
39
|
+
const lines = block.split(/\r?\n/);
|
|
40
|
+
const data = lines
|
|
41
|
+
.filter((line) => line.startsWith("data:"))
|
|
42
|
+
.map((line) => line.slice(5).trimStart())
|
|
43
|
+
.join("\n");
|
|
44
|
+
if (data === "[DONE]") {
|
|
45
|
+
chatCompleted = true;
|
|
46
|
+
continue;
|
|
47
|
+
}
|
|
48
|
+
if (!data) continue;
|
|
49
|
+
let parsed;
|
|
50
|
+
try {
|
|
51
|
+
parsed = JSON.parse(data);
|
|
52
|
+
} catch {
|
|
53
|
+
throw new CLIError(
|
|
54
|
+
"INVALID_RESPONSE",
|
|
55
|
+
"Invalid JSON event in model stream",
|
|
56
|
+
{ requestId: response.requestId },
|
|
57
|
+
);
|
|
58
|
+
}
|
|
59
|
+
if (
|
|
60
|
+
parsed.type === "response.completed" &&
|
|
61
|
+
parsed.response?.status === "completed"
|
|
62
|
+
)
|
|
63
|
+
responseCompleted = true;
|
|
64
|
+
if (parsed.type === "message_stop") messageStopped = true;
|
|
65
|
+
const nested = parsed.response;
|
|
66
|
+
const failed =
|
|
67
|
+
["response.failed", "response.incomplete", "response.cancelled"].includes(
|
|
68
|
+
parsed.type,
|
|
69
|
+
) || ["failed", "incomplete", "cancelled"].includes(nested?.status);
|
|
70
|
+
const remoteError = parsed.error ?? nested?.error;
|
|
71
|
+
if (
|
|
72
|
+
remoteError ||
|
|
73
|
+
failed ||
|
|
74
|
+
parsed.type === "error" ||
|
|
75
|
+
lines.includes("event: error")
|
|
76
|
+
)
|
|
77
|
+
throw new CLIError(
|
|
78
|
+
remoteError?.code ??
|
|
79
|
+
(parsed.type === "response.incomplete"
|
|
80
|
+
? "MODEL_RESPONSE_INCOMPLETE"
|
|
81
|
+
: "MODEL_STREAM_ERROR"),
|
|
82
|
+
remoteError?.message ?? "Model stream did not complete successfully",
|
|
83
|
+
{
|
|
84
|
+
requestId: response.requestId,
|
|
85
|
+
responseId: nested?.id,
|
|
86
|
+
responseStatus: nested?.status,
|
|
87
|
+
incompleteDetails: nested?.incomplete_details,
|
|
88
|
+
},
|
|
89
|
+
);
|
|
90
|
+
}
|
|
91
|
+
if (
|
|
92
|
+
(operation.protocol === "openai-chat-completions" ||
|
|
93
|
+
operation.endpoint === "/v1/chat/completions") &&
|
|
94
|
+
!chatCompleted
|
|
95
|
+
)
|
|
96
|
+
throw new CLIError(
|
|
97
|
+
"INCOMPLETE_STREAM",
|
|
98
|
+
"Model stream ended without its completion marker; do not retry automatically",
|
|
99
|
+
{ requestId: response.requestId },
|
|
100
|
+
);
|
|
101
|
+
if (
|
|
102
|
+
(operation.protocol === "anthropic-messages" ||
|
|
103
|
+
operation.endpoint === "/v1/messages") &&
|
|
104
|
+
!messageStopped
|
|
105
|
+
)
|
|
106
|
+
throw new CLIError(
|
|
107
|
+
"INCOMPLETE_STREAM",
|
|
108
|
+
"Model stream ended without message_stop; do not retry automatically",
|
|
109
|
+
{ requestId: response.requestId },
|
|
110
|
+
);
|
|
111
|
+
if (
|
|
112
|
+
(operation.endpoint === "/v1/responses" ||
|
|
113
|
+
operation.protocol === "openai-responses") &&
|
|
114
|
+
!responseCompleted
|
|
115
|
+
)
|
|
116
|
+
throw new CLIError(
|
|
117
|
+
"INCOMPLETE_STREAM",
|
|
118
|
+
"Responses stream ended without response.completed; do not retry automatically",
|
|
119
|
+
{ requestId: response.requestId },
|
|
120
|
+
);
|
|
121
|
+
return response;
|
|
122
|
+
}
|
|
123
|
+
export async function runModels(args, ctx) {
|
|
124
|
+
const { http, options } = ctx;
|
|
125
|
+
const action = args[1] ?? "list";
|
|
126
|
+
if (action === "list")
|
|
127
|
+
return unwrap(await http.gateway("/v1/models", { auth: "apiKey" }));
|
|
128
|
+
if (action === "prices")
|
|
129
|
+
return unwrap(
|
|
130
|
+
await http.backend("/api/v1/key-market/model-prices", {
|
|
131
|
+
query: routeQuery(options),
|
|
132
|
+
}),
|
|
133
|
+
).map(salePricing);
|
|
134
|
+
if (action === "playgrounds")
|
|
135
|
+
return unwrap(
|
|
136
|
+
await http.backend("/api/v1/key-market/model-playgrounds", {
|
|
137
|
+
query: { modelid: requireValue(options.model, "--model") },
|
|
138
|
+
}),
|
|
139
|
+
);
|
|
140
|
+
if (action === "parameters")
|
|
141
|
+
return unwrap(
|
|
142
|
+
await http.backend("/api/v1/key-market/model-parameters", {
|
|
143
|
+
query: operationQuery(options),
|
|
144
|
+
}),
|
|
145
|
+
);
|
|
146
|
+
if (!["run", "examples"].includes(action))
|
|
147
|
+
throw new CLIError("UNKNOWN_COMMAND", "Unknown models command");
|
|
148
|
+
for (const name of ["protocol", "mode", "method", "query", "headers"])
|
|
149
|
+
if (options[name] !== undefined)
|
|
150
|
+
throw new CLIError(
|
|
151
|
+
"EXAMPLE_SELECTOR_UNSUPPORTED",
|
|
152
|
+
`--${name} is not supported for curl examples; use --provider and --example`,
|
|
153
|
+
);
|
|
154
|
+
const model = requireValue(options.model, "--model");
|
|
155
|
+
const rows = unwrap(await http.backend("/api/v1/key-market/model-prices", {
|
|
156
|
+
query: { modelid: model },
|
|
157
|
+
}));
|
|
158
|
+
const needsLegacyPlaygrounds = Array.isArray(rows) && rows.some(row =>
|
|
159
|
+
row.model === model && row.active !== false && (!options.provider || row.provider === options.provider) &&
|
|
160
|
+
!Array.isArray(row.playgrounds) && !(row.importedInterface?.status === "available" &&
|
|
161
|
+
row.providerKind !== "community" && row.modelSource !== "p2p"));
|
|
162
|
+
const playgrounds = needsLegacyPlaygrounds ? unwrap(await http.backend("/api/v1/key-market/model-playgrounds", {
|
|
163
|
+
query: { modelid: model },
|
|
164
|
+
})) : [];
|
|
165
|
+
const catalog = modelExamples(rows, model, options.provider, playgrounds);
|
|
166
|
+
if (action === "examples") return catalog;
|
|
167
|
+
const example = selectExample(catalog, options);
|
|
168
|
+
const prepared = await prepareModelRequest(example, ctx);
|
|
169
|
+
if (options["dry-run"]) return prepared.preview;
|
|
170
|
+
if (prepared.endpoint === "/v1/audio/speech" && !options.output)
|
|
171
|
+
throw new CLIError(
|
|
172
|
+
"OUTPUT_REQUIRED",
|
|
173
|
+
"Audio speech requires --output FILE before execution",
|
|
174
|
+
);
|
|
175
|
+
if (prepared.request.method !== "GET") {
|
|
176
|
+
const account = unwrap(
|
|
177
|
+
await http.backend("/api/v1/agent-api-account", { auth: "apiKey" }),
|
|
178
|
+
);
|
|
179
|
+
if (microUsd(account.availableMicroUsd) <= 0n)
|
|
180
|
+
throw new CLIError(
|
|
181
|
+
"INSUFFICIENT_BALANCE",
|
|
182
|
+
"Available balance must be positive",
|
|
183
|
+
);
|
|
184
|
+
}
|
|
185
|
+
return verifyStream(await http.model(prepared.url, prepared.request), {
|
|
186
|
+
endpoint: prepared.endpoint,
|
|
187
|
+
});
|
|
188
|
+
}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
$TargetPath = $env:OPENANY_ACL_TARGET
|
|
2
|
+
$Protect = $env:OPENANY_ACL_PROTECT -eq 'true'
|
|
3
|
+
$ErrorActionPreference = 'Stop'
|
|
4
|
+
try {
|
|
5
|
+
$identity = [System.Security.Principal.WindowsIdentity]::GetCurrent()
|
|
6
|
+
$sid = $identity.User
|
|
7
|
+
if ($Protect) {
|
|
8
|
+
$acl = New-Object System.Security.AccessControl.DirectorySecurity
|
|
9
|
+
$acl.SetOwner($sid)
|
|
10
|
+
$acl.SetAccessRuleProtection($true, $false)
|
|
11
|
+
$rule = New-Object System.Security.AccessControl.FileSystemAccessRule(
|
|
12
|
+
$sid, 'FullControl', 'ContainerInherit,ObjectInherit', 'None', 'Allow'
|
|
13
|
+
)
|
|
14
|
+
$acl.AddAccessRule($rule)
|
|
15
|
+
Set-Acl -LiteralPath $TargetPath -AclObject $acl
|
|
16
|
+
}
|
|
17
|
+
$acl = Get-Acl -LiteralPath $TargetPath
|
|
18
|
+
$descriptor = New-Object System.Security.AccessControl.RawSecurityDescriptor($acl.GetSecurityDescriptorBinaryForm(), 0)
|
|
19
|
+
if ($null -eq $descriptor.DiscretionaryAcl -or
|
|
20
|
+
($descriptor.ControlFlags -band [System.Security.AccessControl.ControlFlags]::DiscretionaryAclPresent) -eq 0) {
|
|
21
|
+
throw 'Missing discretionary ACL'
|
|
22
|
+
}
|
|
23
|
+
$owner = $acl.GetOwner([System.Security.Principal.SecurityIdentifier]).Value
|
|
24
|
+
if ($owner -ne $sid.Value) { throw 'Unexpected owner' }
|
|
25
|
+
$allowed = @($sid.Value, 'S-1-5-18', 'S-1-5-32-544')
|
|
26
|
+
foreach ($rule in $acl.GetAccessRules($true, $true, [System.Security.Principal.SecurityIdentifier])) {
|
|
27
|
+
if ($rule.AccessControlType -eq 'Allow' -and $rule.IdentityReference.Value -notin $allowed) {
|
|
28
|
+
throw 'Unexpected access'
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
} catch {
|
|
32
|
+
[Console]::Error.WriteLine('Credential ACL verification failed.')
|
|
33
|
+
exit 1
|
|
34
|
+
}
|