@zenaigc/zenmcp 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/INSTALL.md +77 -0
- package/README.md +53 -2
- package/dist/src/acquire/download.js +152 -0
- package/dist/src/acquire/errors.js +8 -0
- package/dist/src/acquire/share.js +67 -0
- package/dist/src/acquire/social.js +98 -0
- package/dist/src/acquire/trim.js +95 -0
- package/dist/src/assets/files.js +29 -0
- package/dist/src/assets/generator.js +141 -0
- package/dist/src/assets/images.js +90 -0
- package/dist/src/assets/index.js +3 -0
- package/dist/src/assets/model-library.js +87 -0
- package/dist/src/assets/upload-cache.js +36 -0
- package/dist/src/contract/cloud-v1.js +334 -0
- package/dist/src/core/hash.js +18 -0
- package/dist/src/core/index.js +4 -0
- package/dist/src/core/job-store.js +251 -0
- package/dist/src/core/safety.js +43 -0
- package/dist/src/core/schema.js +295 -0
- package/dist/src/doctor.js +22 -0
- package/dist/src/effects/check.js +19 -0
- package/dist/src/effects/font.js +78 -0
- package/dist/src/effects/index.js +188 -0
- package/dist/src/engine/card.js +50 -0
- package/dist/src/engine/direct.js +493 -0
- package/dist/src/engine/generate.js +234 -0
- package/dist/src/engine/index.js +3 -0
- package/dist/src/engine/media.js +24 -0
- package/dist/src/engine/steps.js +444 -0
- package/dist/src/engine/storage.js +90 -0
- package/dist/src/gateway/attempt-store.js +110 -0
- package/dist/src/gateway/budget.js +39 -0
- package/dist/src/gateway/cloud-gateway.js +141 -0
- package/dist/src/gateway/config.js +10 -0
- package/dist/src/gateway/device-auth.js +53 -0
- package/dist/src/gateway/errors.js +105 -0
- package/dist/src/gateway/http.js +124 -0
- package/dist/src/gateway/index.js +8 -0
- package/dist/src/gateway/paid-runner.js +376 -0
- package/dist/src/gateway/types.js +42 -0
- package/dist/src/inspect/index.js +276 -0
- package/dist/src/inspect/media.js +43 -0
- package/dist/src/mcp/server.js +177 -0
- package/dist/src/models/wan3/index.js +173 -0
- package/dist/src/models/wan3/limits.js +25 -0
- package/dist/src/timeline/conform.js +255 -0
- package/dist/src/timeline/cuts.js +225 -0
- package/dist/src/timeline/diff.js +82 -0
- package/dist/src/timeline/errors.js +8 -0
- package/dist/src/timeline/ffmpeg.js +164 -0
- package/dist/src/timeline/index.js +6 -0
- package/dist/src/timeline/params.js +46 -0
- package/dist/src/timeline/probe.js +98 -0
- package/dist/src/timeline/types.js +1 -0
- package/dist/templates/pop-title/README.md +18 -0
- package/dist/templates/pop-title/index.html +102 -0
- package/dist/templates/pop-title/pop-title.js +97 -0
- package/dist/templates/pop-title/render.js +42 -0
- package/dist/templates/pop-title/size-1080/index.html +102 -0
- package/dist/templates/pop-title/variables.example.json +9 -0
- package/dist/templates/pop-title/vendor/FONT.md +10 -0
- package/dist/templates/pop-title/vendor/NotoSansCJKsc-Bold.otf +0 -0
- package/dist/templates/pop-title/vendor/OFL.txt +92 -0
- package/dist/templates/pop-title/vendor/gsap.min.js +11 -0
- package/package.json +35 -4
- package/skills/zenmcp/SKILL.md +27 -0
|
@@ -0,0 +1,493 @@
|
|
|
1
|
+
import { constants } from 'node:fs';
|
|
2
|
+
import { copyFile, lstat, mkdir, readFile, readdir, realpath, rename, rm } from 'node:fs/promises';
|
|
3
|
+
import { join } from 'node:path';
|
|
4
|
+
import { randomUUID } from 'node:crypto';
|
|
5
|
+
import { hostname } from 'node:os';
|
|
6
|
+
import { z } from 'zod';
|
|
7
|
+
import { sha256File, sha256String, isSafeId } from '../core/index.js';
|
|
8
|
+
import { inspectImage } from '../assets/index.js';
|
|
9
|
+
import { child, directory } from '../assets/files.js';
|
|
10
|
+
import { loadUploads, saveUploads } from '../assets/upload-cache.js';
|
|
11
|
+
import { probe } from '../timeline/index.js';
|
|
12
|
+
import { attemptSchema, FileAttemptStore, GatewayError, LocalBudget, PaidRunner, scrub, redact } from '../gateway/index.js';
|
|
13
|
+
import { chargeNotice } from '../gateway/paid-runner.js';
|
|
14
|
+
import { hash, safeError } from './steps.js';
|
|
15
|
+
import { lockJob, readJson, writeJson } from './storage.js';
|
|
16
|
+
import { download, fetchOutput, pollAttempt, refreshUnsettledRefunds, replayRequest, terminalError, uploadContentType } from './generate.js';
|
|
17
|
+
const inputSchema = z.strictObject({ slot: z.string(), type: z.enum(['image', 'video']), sha256: z.string().regex(/^[a-f0-9]{64}$/), content_type: z.string(), size_bytes: z.number().int().nonnegative() });
|
|
18
|
+
const requestSchema = z.strictObject({ schema_version: z.literal(1), generation_id: z.guid(), request: z.strictObject({ capability: z.string(), medium: z.enum(['image', 'video']), params: z.record(z.string(), z.union([z.string(), z.number(), z.boolean()])), inputs: z.array(inputSchema) }), request_digest: z.string(), quote: z.strictObject({ price_credits: z.number().int().nonnegative(), pricing_version: z.string(), quoted_at: z.string() }) });
|
|
19
|
+
const stateSchema = z.object({
|
|
20
|
+
request_digest: z.string(), status: z.enum(['quoted', 'running', 'completed', 'failed']),
|
|
21
|
+
approvedCredits: z.number().int().nonnegative().optional(), mode: z.enum(['R', 'N1', 'N2']).optional(),
|
|
22
|
+
error: z.string().optional(), attempt: z.string().optional(),
|
|
23
|
+
output: z.object({ file: z.string().regex(/^output\.(png|jpg|webp|mp4)$/), kind: z.enum(['image', 'video']), sha256: z.string().regex(/^[a-f0-9]{64}$/) }).optional(),
|
|
24
|
+
});
|
|
25
|
+
async function loadState(dir, currentDigest) {
|
|
26
|
+
const raw = await optional(dir, 'state.json');
|
|
27
|
+
if (raw === undefined)
|
|
28
|
+
return undefined;
|
|
29
|
+
if (currentDigest && raw !== null && typeof raw === 'object' && raw.request_digest !== currentDigest)
|
|
30
|
+
return undefined;
|
|
31
|
+
const parsed = stateSchema.safeParse(raw);
|
|
32
|
+
if (!parsed.success)
|
|
33
|
+
return corrupt();
|
|
34
|
+
return parsed.data;
|
|
35
|
+
}
|
|
36
|
+
const bindingsSchema = z.strictObject({ request_digest: z.string(), submissions: z.array(z.strictObject({ assets: z.record(z.string(), z.guid()), at: z.string() })) });
|
|
37
|
+
const corrupt = () => { throw new Error('generation_state_corrupt'); };
|
|
38
|
+
const live = (a) => a.state !== 'rejected' && a.refund_state !== 'refunded';
|
|
39
|
+
async function optional(dir, name) {
|
|
40
|
+
try {
|
|
41
|
+
return await readJson(dir, name);
|
|
42
|
+
}
|
|
43
|
+
catch (e) {
|
|
44
|
+
if (e.code === 'ENOENT')
|
|
45
|
+
return undefined;
|
|
46
|
+
return corrupt();
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
async function loadRequest(dir) {
|
|
50
|
+
const raw = await optional(dir, 'request.json');
|
|
51
|
+
if (raw === undefined)
|
|
52
|
+
return undefined;
|
|
53
|
+
const parsed = requestSchema.safeParse(raw);
|
|
54
|
+
if (!parsed.success || hash(parsed.data.request) !== parsed.data.request_digest)
|
|
55
|
+
return corrupt();
|
|
56
|
+
return parsed.data;
|
|
57
|
+
}
|
|
58
|
+
/** Fail closed before any recovery request or allocation of a new payment key. */
|
|
59
|
+
export async function validateEvidence(dir, req, gateway) {
|
|
60
|
+
const records = [];
|
|
61
|
+
try {
|
|
62
|
+
const root = await child(dir, 'attempts');
|
|
63
|
+
let entries;
|
|
64
|
+
try {
|
|
65
|
+
entries = await readdir(root);
|
|
66
|
+
}
|
|
67
|
+
catch (e) {
|
|
68
|
+
if (e.code === 'ENOENT')
|
|
69
|
+
entries = [];
|
|
70
|
+
else
|
|
71
|
+
throw e;
|
|
72
|
+
}
|
|
73
|
+
for (const name of entries) {
|
|
74
|
+
const path = join(root, name);
|
|
75
|
+
const info = await lstat(path);
|
|
76
|
+
if (!info.isDirectory() || info.isSymbolicLink() || name !== req?.generation_id)
|
|
77
|
+
return corrupt();
|
|
78
|
+
for (const file of await readdir(path)) {
|
|
79
|
+
const info = await lstat(join(path, file));
|
|
80
|
+
if (!info.isFile() || info.isSymbolicLink())
|
|
81
|
+
return corrupt();
|
|
82
|
+
if (/^\.[0-9a-f]{16}\.tmp$/.test(file))
|
|
83
|
+
continue;
|
|
84
|
+
if (!file.endsWith('.json') || !isSafeId(file.slice(0, -5)))
|
|
85
|
+
return corrupt();
|
|
86
|
+
const a = attemptSchema.parse(JSON.parse(await readFile(join(path, file), 'utf8')));
|
|
87
|
+
if (a.job_id !== req.generation_id || `${a.idempotency_key}.json` !== file)
|
|
88
|
+
return corrupt();
|
|
89
|
+
records.push(a);
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
const bindingsRaw = await optional(dir, 'bindings.json');
|
|
93
|
+
const bindings = bindingsRaw === undefined ? undefined : bindingsSchema.parse(bindingsRaw);
|
|
94
|
+
if (req && bindings?.request_digest === req.request_digest) {
|
|
95
|
+
const slots = req.request.inputs.map(i => i.slot).sort();
|
|
96
|
+
if (new Set(slots).size !== slots.length)
|
|
97
|
+
return corrupt();
|
|
98
|
+
for (const submission of bindings.submissions) {
|
|
99
|
+
if (hash(Object.keys(submission.assets).sort()) !== hash(slots))
|
|
100
|
+
return corrupt();
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
if (records.length) {
|
|
104
|
+
if (!req || !bindings || bindings.request_digest !== req.request_digest)
|
|
105
|
+
return corrupt();
|
|
106
|
+
const state = await loadState(dir);
|
|
107
|
+
if (!state || state.request_digest !== req.request_digest)
|
|
108
|
+
return corrupt();
|
|
109
|
+
for (const a of records) {
|
|
110
|
+
if (a.body.asset_id || a.body.capability !== req.request.capability || hash(a.body.params ?? {}) !== hash(req.request.params))
|
|
111
|
+
return corrupt();
|
|
112
|
+
if (!bindings.submissions.some(s => hash(s.assets) === hash(a.body.assets ?? {})))
|
|
113
|
+
return corrupt();
|
|
114
|
+
}
|
|
115
|
+
if (records.filter(live).length > 1)
|
|
116
|
+
return corrupt();
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
catch {
|
|
120
|
+
return corrupt();
|
|
121
|
+
}
|
|
122
|
+
if (records.some(a => a.deployment !== gateway.deployment || !a.account_id))
|
|
123
|
+
throw new Error('prior_payment_unresolved:请使用原部署及账户核实付款。');
|
|
124
|
+
return records;
|
|
125
|
+
}
|
|
126
|
+
export function assertNewAttemptAllowed(attempts, mode, retry, allowed) {
|
|
127
|
+
if (attempts.some(live))
|
|
128
|
+
return corrupt();
|
|
129
|
+
if (attempts.some(a => !allowed.has(a.idempotency_key)) && !(mode === 'N2' && retry))
|
|
130
|
+
throw new Error('retry_requires_approval');
|
|
131
|
+
}
|
|
132
|
+
async function verifyFile(path, digest) {
|
|
133
|
+
try {
|
|
134
|
+
const info = await lstat(path);
|
|
135
|
+
if (!info.isFile() || info.isSymbolicLink() || await sha256File(path) !== digest)
|
|
136
|
+
return corrupt();
|
|
137
|
+
}
|
|
138
|
+
catch {
|
|
139
|
+
return corrupt();
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
function parameters(cap, values) {
|
|
143
|
+
const params = { ...values };
|
|
144
|
+
if (Object.keys(params).some(k => !cap.params.some(p => p.key === k)))
|
|
145
|
+
throw new Error('catalog_param_unavailable');
|
|
146
|
+
for (const p of cap.params) {
|
|
147
|
+
if (params[p.key] === undefined && p.default !== undefined)
|
|
148
|
+
params[p.key] = p.default;
|
|
149
|
+
const v = params[p.key];
|
|
150
|
+
if (v === undefined) {
|
|
151
|
+
if (p.required)
|
|
152
|
+
throw new Error('catalog_param_required');
|
|
153
|
+
continue;
|
|
154
|
+
}
|
|
155
|
+
if (!['string', 'number', 'boolean'].includes(typeof v) || ((p.type === 'text' || p.type === 'select' || p.type === 'segmented') && typeof v !== 'string') || (p.type === 'toggle' && typeof v !== 'boolean') || (p.type === 'slider' && typeof v !== 'number') || (p.options?.length && !p.options.some(o => o.value === v)) || (typeof v === 'number' && (!Number.isFinite(v) || (p.min != null && v < p.min) || (p.max != null && v > p.max))) || (typeof v === 'string' && p.max_length != null && [...v].length > p.max_length))
|
|
156
|
+
throw new Error('catalog_param_invalid');
|
|
157
|
+
}
|
|
158
|
+
return params;
|
|
159
|
+
}
|
|
160
|
+
export class DirectGeneration {
|
|
161
|
+
options;
|
|
162
|
+
active = new Map();
|
|
163
|
+
constructor(options) {
|
|
164
|
+
this.options = options;
|
|
165
|
+
}
|
|
166
|
+
async listModels(medium) {
|
|
167
|
+
const c = await this.options.gateway.catalog();
|
|
168
|
+
if (!c.modified)
|
|
169
|
+
throw new Error('catalog_required');
|
|
170
|
+
return { models: c.catalog.capabilities.filter(c => c.medium !== 'audio' && (!medium || c.medium === medium)) };
|
|
171
|
+
}
|
|
172
|
+
async request(dir) {
|
|
173
|
+
let req;
|
|
174
|
+
try {
|
|
175
|
+
req = await loadRequest(dir);
|
|
176
|
+
}
|
|
177
|
+
catch {
|
|
178
|
+
return corrupt();
|
|
179
|
+
}
|
|
180
|
+
const attempts = await validateEvidence(dir, req, this.options.gateway);
|
|
181
|
+
return { req, attempts };
|
|
182
|
+
}
|
|
183
|
+
async uploads(dir, req, reset = false) {
|
|
184
|
+
const assets = {};
|
|
185
|
+
if (!req.request.inputs.length)
|
|
186
|
+
return assets;
|
|
187
|
+
const g = this.options.gateway;
|
|
188
|
+
const me = await g.me();
|
|
189
|
+
const key = sha256String(`${g.deployment}\n${me.user_id}`).slice(0, 32);
|
|
190
|
+
const cacheDir = await directory(dir, 'uploads', key);
|
|
191
|
+
const cache = await loadUploads(cacheDir);
|
|
192
|
+
if (reset) {
|
|
193
|
+
for (const i of req.request.inputs)
|
|
194
|
+
delete cache[i.sha256];
|
|
195
|
+
await saveUploads(cacheDir, cache);
|
|
196
|
+
}
|
|
197
|
+
for (const i of req.request.inputs) {
|
|
198
|
+
if (!cache[i.sha256]) {
|
|
199
|
+
const a = await g.upload(await child(dir, 'assets', i.sha256), { consent_confirmed: true, content_type: i.content_type });
|
|
200
|
+
if (a.status !== 'ready')
|
|
201
|
+
throw new Error('asset_not_ready');
|
|
202
|
+
cache[i.sha256] = a.asset_id;
|
|
203
|
+
await saveUploads(cacheDir, cache);
|
|
204
|
+
}
|
|
205
|
+
assets[i.slot] = cache[i.sha256];
|
|
206
|
+
}
|
|
207
|
+
return assets;
|
|
208
|
+
}
|
|
209
|
+
async quote(input) {
|
|
210
|
+
const secret = await this.options.tokens.token();
|
|
211
|
+
const raw = { capability: input.capability, prompt: input.prompt, params: input.params };
|
|
212
|
+
if (JSON.stringify(scrub(raw, secret)) !== JSON.stringify(raw) || [input.prompt, ...Object.values(input.params ?? {})].some(v => typeof v === 'string' && /zk_(?:live|test)_[A-Za-z0-9_-]+|Bearer\s+\S+/i.test(v)))
|
|
213
|
+
throw new Error('token_in_input');
|
|
214
|
+
await mkdir(input.directory, { recursive: true });
|
|
215
|
+
const dir = await realpath(input.directory);
|
|
216
|
+
if (this.active.has(dir))
|
|
217
|
+
throw new Error('generation_running');
|
|
218
|
+
const release = await lockJob(dir);
|
|
219
|
+
try {
|
|
220
|
+
const { req: existing, attempts } = await this.request(dir);
|
|
221
|
+
if (Object.keys(input.inputs ?? {}).length && input.consent_confirmed !== true)
|
|
222
|
+
throw new Error('upload_consent_required');
|
|
223
|
+
const catalog = await this.options.gateway.catalog();
|
|
224
|
+
if (!catalog.modified)
|
|
225
|
+
throw new Error('catalog_required');
|
|
226
|
+
const cap = catalog.catalog.capabilities.find(c => c.id === input.capability);
|
|
227
|
+
if (!cap || cap.medium === 'audio')
|
|
228
|
+
throw new Error('capability_not_available');
|
|
229
|
+
if (input.params?.prompt !== undefined && input.params.prompt !== input.prompt)
|
|
230
|
+
throw new Error('prompt_mismatch');
|
|
231
|
+
const params = parameters(cap, { ...input.params, prompt: input.prompt });
|
|
232
|
+
const supplied = Object.keys(input.inputs ?? {});
|
|
233
|
+
for (const slot of supplied) {
|
|
234
|
+
const s = cap.assets.find(s => s.key === slot);
|
|
235
|
+
if (!s)
|
|
236
|
+
throw new Error('catalog_slot_unavailable');
|
|
237
|
+
if (s.type !== 'image' && s.type !== 'video')
|
|
238
|
+
throw new Error('unsupported_slot_type');
|
|
239
|
+
}
|
|
240
|
+
if (cap.assets.some(s => s.required && s.type === 'audio'))
|
|
241
|
+
throw new Error('unsupported_slot_type');
|
|
242
|
+
if (cap.assets.some(s => s.required && !supplied.includes(s.key)))
|
|
243
|
+
throw new Error('catalog_slot_missing');
|
|
244
|
+
const assetDir = await directory(dir, 'assets');
|
|
245
|
+
if (!attempts.length)
|
|
246
|
+
for (const n of await readdir(assetDir))
|
|
247
|
+
if (n.startsWith('tmp-'))
|
|
248
|
+
await rm(join(assetDir, n), { force: true });
|
|
249
|
+
const inputs = [];
|
|
250
|
+
for (const slot of supplied.sort()) {
|
|
251
|
+
const spec = cap.assets.find(s => s.key === slot);
|
|
252
|
+
const src = await realpath(input.inputs[slot]);
|
|
253
|
+
if (!(await lstat(src)).isFile())
|
|
254
|
+
throw new Error('invalid_input');
|
|
255
|
+
const tmp = await child(dir, 'assets', `tmp-${randomUUID()}`);
|
|
256
|
+
await copyFile(src, tmp, constants.COPYFILE_EXCL);
|
|
257
|
+
try {
|
|
258
|
+
const sha256 = await sha256File(tmp);
|
|
259
|
+
const size_bytes = (await lstat(tmp)).size;
|
|
260
|
+
let content_type;
|
|
261
|
+
if (spec.type === 'image') {
|
|
262
|
+
const facts = await inspectImage(tmp);
|
|
263
|
+
if (!['jpeg', 'png', 'webp', 'heif'].includes(facts.format))
|
|
264
|
+
throw new Error('unsupported_input_format');
|
|
265
|
+
content_type = `image/${facts.format}`;
|
|
266
|
+
}
|
|
267
|
+
else {
|
|
268
|
+
const p = await probe(tmp);
|
|
269
|
+
const duration = p.frames * p.fps.den / p.fps.num;
|
|
270
|
+
if (!(duration > 0 && Number.isFinite(duration)))
|
|
271
|
+
throw new Error('invalid_input');
|
|
272
|
+
if (spec.max_duration_seconds != null && duration > spec.max_duration_seconds)
|
|
273
|
+
throw new Error('input_too_long');
|
|
274
|
+
content_type = await uploadContentType(tmp, 'video');
|
|
275
|
+
}
|
|
276
|
+
if (spec.max_size_bytes != null && size_bytes > spec.max_size_bytes)
|
|
277
|
+
throw new Error('input_too_large');
|
|
278
|
+
const dest = join(assetDir, sha256);
|
|
279
|
+
let exists = true;
|
|
280
|
+
try {
|
|
281
|
+
await lstat(dest);
|
|
282
|
+
}
|
|
283
|
+
catch (e) {
|
|
284
|
+
if (e.code === 'ENOENT')
|
|
285
|
+
exists = false;
|
|
286
|
+
else
|
|
287
|
+
throw e;
|
|
288
|
+
}
|
|
289
|
+
if (exists)
|
|
290
|
+
await verifyFile(dest, sha256);
|
|
291
|
+
else if (!attempts.length)
|
|
292
|
+
await rename(tmp, dest);
|
|
293
|
+
inputs.push({ slot, type: spec.type, sha256, content_type, size_bytes });
|
|
294
|
+
}
|
|
295
|
+
finally {
|
|
296
|
+
await rm(tmp, { force: true });
|
|
297
|
+
}
|
|
298
|
+
}
|
|
299
|
+
const request = { capability: cap.id, medium: cap.medium, params, inputs };
|
|
300
|
+
const digest = hash(request);
|
|
301
|
+
if (attempts.length && digest !== existing.request_digest)
|
|
302
|
+
throw new Error('generation_exists:请使用新目录。');
|
|
303
|
+
const req = { schema_version: 1, generation_id: existing?.generation_id ?? randomUUID(), request, request_digest: digest, quote: existing?.quote ?? { price_credits: 0, pricing_version: '', quoted_at: '' } };
|
|
304
|
+
let assets = await this.uploads(dir, req);
|
|
305
|
+
let quote;
|
|
306
|
+
try {
|
|
307
|
+
quote = await this.options.gateway.price({ capability: cap.id, params, assets });
|
|
308
|
+
}
|
|
309
|
+
catch (e) {
|
|
310
|
+
if (!(e instanceof GatewayError) || e.code !== 'asset_not_found')
|
|
311
|
+
throw e;
|
|
312
|
+
assets = await this.uploads(dir, req, true);
|
|
313
|
+
quote = await this.options.gateway.price({ capability: cap.id, params, assets });
|
|
314
|
+
}
|
|
315
|
+
req.quote = { price_credits: quote.price_credits, pricing_version: quote.pricing_version, quoted_at: new Date().toISOString() };
|
|
316
|
+
await writeJson(dir, 'request.json', req);
|
|
317
|
+
if (!attempts.length)
|
|
318
|
+
await writeJson(dir, 'state.json', { request_digest: digest, status: 'quoted' });
|
|
319
|
+
return { ...req.quote, requestDigest: digest, markdown: `报价 ${quote.price_credits} 积分。请获得用户明确批准后,携带 requestDigest 调用 run_generation。` };
|
|
320
|
+
}
|
|
321
|
+
finally {
|
|
322
|
+
await release();
|
|
323
|
+
}
|
|
324
|
+
}
|
|
325
|
+
async run(input) {
|
|
326
|
+
const dir = await realpath(input.directory);
|
|
327
|
+
if (this.active.has(dir))
|
|
328
|
+
return { status: 'running' };
|
|
329
|
+
const release = await lockJob(dir);
|
|
330
|
+
let handed = false;
|
|
331
|
+
try {
|
|
332
|
+
const req = await loadRequest(dir);
|
|
333
|
+
if (!req) {
|
|
334
|
+
await validateEvidence(dir, undefined, this.options.gateway);
|
|
335
|
+
throw new Error('quote_required');
|
|
336
|
+
}
|
|
337
|
+
if (input.requestDigest !== req.request_digest)
|
|
338
|
+
throw new Error('request_digest_mismatch');
|
|
339
|
+
const attempts = await validateEvidence(dir, req, this.options.gateway);
|
|
340
|
+
const state = await loadState(dir, attempts.length ? undefined : req.request_digest);
|
|
341
|
+
if (state?.request_digest === req.request_digest && state.status === 'completed' && state.output && /^output\.(png|jpg|webp|mp4)$/.test(state.output.file)) {
|
|
342
|
+
try {
|
|
343
|
+
await verifyFile(join(dir, state.output.file), state.output.sha256);
|
|
344
|
+
return { status: 'completed', output: { file: join(dir, state.output.file), kind: state.output.kind, sha256: state.output.sha256 } };
|
|
345
|
+
}
|
|
346
|
+
catch { /* Re-download the same completed attempt. */ }
|
|
347
|
+
}
|
|
348
|
+
if (!Number.isSafeInteger(input.approvedCredits) || input.approvedCredits < 0)
|
|
349
|
+
throw new Error('approved_budget_required');
|
|
350
|
+
const mode = attempts.some(live) ? 'R' : attempts.length ? 'N2' : 'N1';
|
|
351
|
+
if (mode !== 'R' && input.approvedCredits < req.quote.price_credits)
|
|
352
|
+
throw new Error('approval_below_quote');
|
|
353
|
+
if (mode === 'N2' && input.retryAfterRelease !== true)
|
|
354
|
+
throw new Error('retry_requires_approval');
|
|
355
|
+
const next = { request_digest: req.request_digest, status: 'running', approvedCredits: input.approvedCredits, mode };
|
|
356
|
+
await writeJson(dir, 'state.json', next);
|
|
357
|
+
const work = Promise.resolve().then(() => this.execute(dir, req, next, input.retryAfterRelease === true)).catch(async (e) => {
|
|
358
|
+
const secret = await this.options.tokens.token().catch(() => '');
|
|
359
|
+
await writeJson(dir, 'state.json', { ...next, status: 'failed', error: redact(safeError(e, secret), secret) });
|
|
360
|
+
}).catch(() => undefined).finally(async () => { this.active.delete(dir); await release(); });
|
|
361
|
+
this.active.set(dir, work);
|
|
362
|
+
handed = true;
|
|
363
|
+
return { status: 'accepted', markdown: '已接受后台生成,请用 get_generation 查询。' };
|
|
364
|
+
}
|
|
365
|
+
finally {
|
|
366
|
+
if (!handed)
|
|
367
|
+
await release();
|
|
368
|
+
}
|
|
369
|
+
}
|
|
370
|
+
async execute(dir, req, state, retry) {
|
|
371
|
+
const g = this.options.gateway;
|
|
372
|
+
const store = new FileAttemptStore(await child(dir, 'attempts'));
|
|
373
|
+
const runner = new PaidRunner({ jobId: req.generation_id, gateway: g, tokens: this.options.tokens, budget: new LocalBudget(state.approvedCredits), store });
|
|
374
|
+
let attempts = await store.list(req.generation_id);
|
|
375
|
+
if (attempts.length) {
|
|
376
|
+
if (attempts.some(a => a.deployment !== g.deployment || !a.account_id))
|
|
377
|
+
throw new Error('prior_payment_unresolved');
|
|
378
|
+
const me = await g.me().catch(() => { throw new Error('account_lookup_failed'); });
|
|
379
|
+
if (attempts.some(a => a.account_id !== me.user_id))
|
|
380
|
+
throw new Error('generation_account_mismatch');
|
|
381
|
+
await runner.recover();
|
|
382
|
+
await refreshUnsettledRefunds(runner, store, req.generation_id);
|
|
383
|
+
}
|
|
384
|
+
attempts = await validateEvidence(dir, req, g);
|
|
385
|
+
let attempt = attempts.find(live);
|
|
386
|
+
if (state.mode === 'R') {
|
|
387
|
+
if (!attempt)
|
|
388
|
+
throw new Error('retry_requires_approval:前次尝试已释放,需明确批准后带 retryAfterRelease: true 重试。');
|
|
389
|
+
if (attempt.state === 'unconfirmed') {
|
|
390
|
+
const key = attempt.idempotency_key;
|
|
391
|
+
attempt = await runner.submit(replayRequest(attempt));
|
|
392
|
+
if (attempt.idempotency_key !== key)
|
|
393
|
+
return corrupt();
|
|
394
|
+
}
|
|
395
|
+
if (attempt.state === 'mismatch')
|
|
396
|
+
throw new Error('payment_mismatch');
|
|
397
|
+
if ((attempt.state === 'failed' || attempt.state === 'cancelled') && attempt.refund_state !== 'not_refunded')
|
|
398
|
+
throw new Error('refund_pending');
|
|
399
|
+
}
|
|
400
|
+
else {
|
|
401
|
+
const allowed = new Set();
|
|
402
|
+
const c = await g.catalog();
|
|
403
|
+
if (!c.modified || !c.catalog.capabilities.some(c => c.id === req.request.capability && c.medium === req.request.medium))
|
|
404
|
+
throw new Error('capability_not_available');
|
|
405
|
+
for (const i of req.request.inputs)
|
|
406
|
+
await verifyFile(await child(dir, 'assets', i.sha256), i.sha256);
|
|
407
|
+
for (let n = 0; n < 2; n++) {
|
|
408
|
+
const assets = await this.uploads(dir, req, n > 0);
|
|
409
|
+
const old = await optional(dir, 'bindings.json');
|
|
410
|
+
const bindings = old === undefined ? { request_digest: req.request_digest, submissions: [] } : bindingsSchema.parse(old);
|
|
411
|
+
if (bindings.request_digest !== req.request_digest) {
|
|
412
|
+
if (attempts.length)
|
|
413
|
+
return corrupt();
|
|
414
|
+
bindings.request_digest = req.request_digest;
|
|
415
|
+
bindings.submissions = [];
|
|
416
|
+
}
|
|
417
|
+
bindings.submissions.push({ assets, at: new Date().toISOString() });
|
|
418
|
+
await writeJson(dir, 'bindings.json', bindings);
|
|
419
|
+
await validateEvidence(dir, req, g);
|
|
420
|
+
const before = await store.list(req.generation_id);
|
|
421
|
+
assertNewAttemptAllowed(before, state.mode, retry, allowed);
|
|
422
|
+
try {
|
|
423
|
+
attempt = await runner.submit({ capability: req.request.capability, params: req.request.params, assets });
|
|
424
|
+
if (n === 0 && attempt.state === 'rejected' && attempt.rejected_code === 'asset_not_found') {
|
|
425
|
+
allowed.add(attempt.idempotency_key);
|
|
426
|
+
continue;
|
|
427
|
+
}
|
|
428
|
+
break;
|
|
429
|
+
}
|
|
430
|
+
catch (e) {
|
|
431
|
+
if (n === 0 && e instanceof GatewayError && e.code === 'asset_not_found' && (await store.list(req.generation_id)).length === before.length)
|
|
432
|
+
continue;
|
|
433
|
+
throw e;
|
|
434
|
+
}
|
|
435
|
+
}
|
|
436
|
+
}
|
|
437
|
+
attempt = await pollAttempt(runner, attempt, this.options.poll ?? { intervalMs: 5000, timeoutMs: req.request.medium === 'image' ? 600000 : 1800000 });
|
|
438
|
+
if (attempt.state !== 'completed')
|
|
439
|
+
throw await terminalError(attempt, g);
|
|
440
|
+
const url = await fetchOutput(g, attempt, req.request.medium);
|
|
441
|
+
const tmp = await child(dir, 'output.download');
|
|
442
|
+
let file;
|
|
443
|
+
try {
|
|
444
|
+
await (this.options.download ?? download)(url, tmp, { maxBytes: (req.request.medium === 'image' ? 50 : 500) * 1024 * 1024 });
|
|
445
|
+
if (req.request.medium === 'image') {
|
|
446
|
+
const facts = await inspectImage(tmp);
|
|
447
|
+
const ext = { png: 'png', jpeg: 'jpg', webp: 'webp' }[facts.format];
|
|
448
|
+
if (!ext)
|
|
449
|
+
throw new Error('invalid_output');
|
|
450
|
+
file = `output.${ext}`;
|
|
451
|
+
}
|
|
452
|
+
else {
|
|
453
|
+
await probe(tmp);
|
|
454
|
+
file = 'output.mp4';
|
|
455
|
+
}
|
|
456
|
+
await rename(tmp, await child(dir, file));
|
|
457
|
+
}
|
|
458
|
+
catch {
|
|
459
|
+
throw new Error('invalid_output');
|
|
460
|
+
}
|
|
461
|
+
finally {
|
|
462
|
+
await rm(tmp, { force: true });
|
|
463
|
+
}
|
|
464
|
+
await writeJson(dir, 'state.json', { ...state, status: 'completed', attempt: attempt.idempotency_key, output: { file, kind: req.request.medium, sha256: await sha256File(join(dir, file)) } });
|
|
465
|
+
}
|
|
466
|
+
async get(input) {
|
|
467
|
+
const dir = await realpath(input.directory);
|
|
468
|
+
const { req, attempts } = await this.request(dir);
|
|
469
|
+
if (!req)
|
|
470
|
+
throw new Error('quote_required');
|
|
471
|
+
const state = await loadState(dir, attempts.length ? undefined : req.request_digest);
|
|
472
|
+
const background = this.active.has(dir);
|
|
473
|
+
const charge_notice = attempts.map(chargeNotice).find(notice => notice !== undefined) ?? null;
|
|
474
|
+
let locked = false;
|
|
475
|
+
try {
|
|
476
|
+
const owner = JSON.parse(await readFile(await child(dir, 'engine.lock'), 'utf8'));
|
|
477
|
+
locked = true;
|
|
478
|
+
if (owner.hostname === hostname() && Number.isSafeInteger(owner.pid) && owner.pid > 0)
|
|
479
|
+
try {
|
|
480
|
+
process.kill(owner.pid, 0);
|
|
481
|
+
}
|
|
482
|
+
catch (e) {
|
|
483
|
+
if (e.code === 'ESRCH')
|
|
484
|
+
locked = false;
|
|
485
|
+
}
|
|
486
|
+
}
|
|
487
|
+
catch (e) {
|
|
488
|
+
locked = e.code !== 'ENOENT';
|
|
489
|
+
}
|
|
490
|
+
const secret = await this.options.tokens.token().catch(() => '');
|
|
491
|
+
return scrub({ charge_notice, status: state?.status ?? 'quoted', generation_id: req.generation_id, request_digest: req.request_digest, request: req.request, quote: req.quote, approvedCredits: state?.approvedCredits, budget: new LocalBudget(state?.approvedCredits ?? 0).usage(attempts), attempts: attempts.map(a => ({ key: a.idempotency_key, state: a.state, status: a.status, charged_credits: a.charged_credits, refund_state: a.refund_state, ...(/^[a-z][a-z0-9_]*$/.test(a.rejected_code ?? '') ? { rejected_code: a.rejected_code } : {}) })), output: state?.status === 'completed' && state.output && /^output\.(png|jpg|webp|mp4)$/.test(state.output.file) ? { file: join(dir, state.output.file), kind: state.output.kind, sha256: state.output.sha256 } : undefined, background, interrupted: state?.status === 'running' && !background && !locked, error: state?.error ? redact(safeError(new Error(state.error), secret), secret) : undefined, markdown: [`生成状态:${state?.status ?? 'quoted'}`, ...(charge_notice ? [charge_notice, '预算已保留,请在同一生成目录续查。'] : [])].join('\n\n') }, secret);
|
|
492
|
+
}
|
|
493
|
+
}
|