@zenaigc/zenmcp 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- 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 +178 -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 +33 -4
|
@@ -0,0 +1,444 @@
|
|
|
1
|
+
import { mkdir, readFile, copyFile, lstat, rm, readdir } from 'node:fs/promises';
|
|
2
|
+
import { resolve, join } from 'node:path';
|
|
3
|
+
import { randomInt, randomUUID } from 'node:crypto';
|
|
4
|
+
import { parseJob, cutsOf, sha256File, sha256String } from '../core/index.js';
|
|
5
|
+
import { FileAttemptStore, PaidRunner, LocalBudget, scrub } from '../gateway/index.js';
|
|
6
|
+
import { probe, frameDiffs, detectCuts, locateCuts, conform } from '../timeline/index.js';
|
|
7
|
+
import { WAN3_LIMITS } from '../models/wan3/limits.js';
|
|
8
|
+
import { compileWan3 } from '../models/wan3/index.js';
|
|
9
|
+
import { inspectGenerated, inspectConformed, inspectComposited, inspectExport, buildReport } from '../inspect/index.js';
|
|
10
|
+
import { approvedAsset, ingestAsset } from '../assets/images.js';
|
|
11
|
+
import { assetSchema } from '../core/schema.js';
|
|
12
|
+
import { child } from '../assets/files.js';
|
|
13
|
+
import { generateVideo, mapRequest, validateMedia, download, refreshUnsettledRefunds, same } from './generate.js';
|
|
14
|
+
import { lockJob, readJson, writeJson } from './storage.js';
|
|
15
|
+
import { mixAudio } from './media.js';
|
|
16
|
+
import { containsSensitiveText, planCard } from './card.js';
|
|
17
|
+
export const STEP_NAMES = ['analyze', 'plan', 'prepare', 'compile', 'generate', 'inspect', 'conform', 'effects', 'mix', 'export', 'report'];
|
|
18
|
+
const out = (data, files = []) => ({ data, files });
|
|
19
|
+
const UNCONFIRMED_TAG = '待确认';
|
|
20
|
+
const EFFECT_RENDERER_UNAVAILABLE = 'effect_renderer_unavailable:任务有特效层但未配置 EffectRenderer,不能静默跳过。';
|
|
21
|
+
export function safeError(error, secret) {
|
|
22
|
+
// External exception messages may contain credentials or signed output URLs.
|
|
23
|
+
const fileError = error;
|
|
24
|
+
if (fileError && typeof fileError.code === 'string' && typeof fileError.syscall === 'string')
|
|
25
|
+
return `file_error:${fileError.code}`;
|
|
26
|
+
const message = error instanceof Error ? error.message : 'step_failed';
|
|
27
|
+
return containsSensitiveText(message) ? 'step_failed:外部错误详情未持久化,避免泄露敏感信息' : scrub(message, secret);
|
|
28
|
+
}
|
|
29
|
+
export function hash(value) {
|
|
30
|
+
const stable = (v) => Array.isArray(v) ? v.map(stable) : v && typeof v === 'object' ? Object.fromEntries(Object.entries(v).sort(([a], [b]) => a.localeCompare(b)).map(([k, v]) => [k, stable(v)])) : v;
|
|
31
|
+
return sha256String(JSON.stringify(stable(value)));
|
|
32
|
+
}
|
|
33
|
+
export async function analyzeReference(path) {
|
|
34
|
+
const source = { ...await probe(path), sha256: await sha256File(path) };
|
|
35
|
+
const cuts = detectCuts(await frameDiffs(path));
|
|
36
|
+
return { source, cuts, note: '仅软件切点检测,阈值待校准;标签、绑定、定稿属性与语义需 Agent/用户确认。' };
|
|
37
|
+
}
|
|
38
|
+
export function draftJob(analysis) {
|
|
39
|
+
const { source, cuts } = analysis;
|
|
40
|
+
const boundaries = [0, ...cuts.map(c => c.frame), source.frames];
|
|
41
|
+
const gcd = (a, b) => b ? gcd(b, a % b) : a;
|
|
42
|
+
const divisor = gcd(source.width, source.height);
|
|
43
|
+
const ratio = `${source.width / divisor}:${source.height / divisor}`;
|
|
44
|
+
const allowedRatio = WAN3_LIMITS.allowedRatios.includes(ratio);
|
|
45
|
+
const job = {
|
|
46
|
+
schema_version: 1, job_id: randomUUID(), created_at: new Date().toISOString(),
|
|
47
|
+
storyboard: { source, effects: [], segments: boundaries.slice(0, -1).map((start, i) => ({
|
|
48
|
+
id: `s${i + 1}`, start_frame: start, end_frame: boundaries[i + 1],
|
|
49
|
+
tags: { voice: UNCONFIRMED_TAG, space: UNCONFIRMED_TAG, product: UNCONFIRMED_TAG, effect: UNCONFIRMED_TAG },
|
|
50
|
+
})) }, assets: [], bindings: [], requirements: [],
|
|
51
|
+
};
|
|
52
|
+
return { job, output_hint: { duration_seconds: Math.ceil(source.frames * source.fps.den / source.fps.num), ...(allowedRatio ? { ratio } : {}) },
|
|
53
|
+
note: '切点由软件检测;标签、绑定和 slots 均需 Agent/用户确认,尚未选择分辨率。' + (allowedRatio ? '' : `源比例 ${ratio} 不在允许列表,output_hint 已省略 ratio,需 Agent/用户选择输出比例。`) };
|
|
54
|
+
}
|
|
55
|
+
function compilePlan(input) {
|
|
56
|
+
const attributes = Object.fromEntries(input.job.assets.filter(a => a.role === 'product').map(a => [a.id, a.attributes ?? '']));
|
|
57
|
+
const motion = input.job.assets.find(a => a.role === 'motion_ref');
|
|
58
|
+
const source = input.job.storyboard.source;
|
|
59
|
+
return { job: input.job, attributes, motion_ref: motion?.id ?? '', motion_ref_seconds: source.frames * source.fps.den / source.fps.num, output: input.output };
|
|
60
|
+
}
|
|
61
|
+
function compiled(input) {
|
|
62
|
+
const result = compileWan3(compilePlan(input));
|
|
63
|
+
if (!result.request)
|
|
64
|
+
throw new Error(`compile_failed:${result.issues.map(i => `${i.code}: ${i.message}`).join(';')}`);
|
|
65
|
+
return result.request;
|
|
66
|
+
}
|
|
67
|
+
/** Canonical digest of user-confirmed plan inputs (excluding output.seed so new_seed revisions remain authorized). */
|
|
68
|
+
export function computePlanDigest(input) {
|
|
69
|
+
const { seed: _seed, ...outputWithoutSeed } = input.output;
|
|
70
|
+
return hash({
|
|
71
|
+
job: input.job,
|
|
72
|
+
output: outputWithoutSeed,
|
|
73
|
+
slots: input.slots ?? {},
|
|
74
|
+
retryReserveCredits: input.retryReserveCredits,
|
|
75
|
+
hasUserAudio: Boolean(input.audio),
|
|
76
|
+
});
|
|
77
|
+
}
|
|
78
|
+
export class Engine {
|
|
79
|
+
options;
|
|
80
|
+
tokens;
|
|
81
|
+
constructor(options) {
|
|
82
|
+
this.options = options;
|
|
83
|
+
this.tokens = options.tokens ?? { token: async () => {
|
|
84
|
+
const value = process.env.ZENMCP_TOKEN;
|
|
85
|
+
if (!value)
|
|
86
|
+
throw new Error('ZENMCP_TOKEN 未配置');
|
|
87
|
+
return value;
|
|
88
|
+
} };
|
|
89
|
+
}
|
|
90
|
+
async importAssets(dir, items) {
|
|
91
|
+
if (!items.length)
|
|
92
|
+
throw new Error('assets_required');
|
|
93
|
+
if (new Set(items.map(item => item.id)).size !== items.length)
|
|
94
|
+
throw new Error('duplicate_asset_id');
|
|
95
|
+
await mkdir(dir, { recursive: true });
|
|
96
|
+
const unlock = await lockJob(dir);
|
|
97
|
+
const created = [];
|
|
98
|
+
try {
|
|
99
|
+
await this.assertNoPaidWork(dir);
|
|
100
|
+
const assets = [];
|
|
101
|
+
for (const item of items) {
|
|
102
|
+
const destination = await child(dir, 'assets', item.id);
|
|
103
|
+
let exists = false;
|
|
104
|
+
try {
|
|
105
|
+
await lstat(destination);
|
|
106
|
+
exists = true;
|
|
107
|
+
}
|
|
108
|
+
catch (error) {
|
|
109
|
+
if (error.code !== 'ENOENT')
|
|
110
|
+
throw error;
|
|
111
|
+
}
|
|
112
|
+
let asset;
|
|
113
|
+
if (exists) {
|
|
114
|
+
const digest = await sha256File(destination);
|
|
115
|
+
if (digest !== await sha256File(item.source))
|
|
116
|
+
throw new Error(`asset_exists: ${item.id}`);
|
|
117
|
+
asset = assetSchema.parse({ id: item.id, role: item.role, path: `assets/${item.id}`, sha256: digest, approved: false });
|
|
118
|
+
}
|
|
119
|
+
else {
|
|
120
|
+
asset = await ingestAsset(dir, item.source, item.role, item.id);
|
|
121
|
+
created.push(destination);
|
|
122
|
+
}
|
|
123
|
+
if (item.attributes !== undefined)
|
|
124
|
+
asset.attributes = item.attributes;
|
|
125
|
+
asset = approvedAsset(asset);
|
|
126
|
+
await validateMedia(dir, { assets: [asset] });
|
|
127
|
+
assets.push(asset);
|
|
128
|
+
}
|
|
129
|
+
return assets;
|
|
130
|
+
}
|
|
131
|
+
catch (error) {
|
|
132
|
+
await Promise.all(created.map(path => rm(path, { force: true })));
|
|
133
|
+
throw error;
|
|
134
|
+
}
|
|
135
|
+
finally {
|
|
136
|
+
await unlock();
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
async create(dir, input) {
|
|
140
|
+
parseJob(input.job);
|
|
141
|
+
if (!Number.isSafeInteger(input.retryReserveCredits) || input.retryReserveCredits < 0)
|
|
142
|
+
throw new Error('invalid retry reserve');
|
|
143
|
+
await mkdir(dir, { recursive: true });
|
|
144
|
+
const unlock = await lockJob(dir);
|
|
145
|
+
try {
|
|
146
|
+
await this.assertNoPaidWork(dir);
|
|
147
|
+
// Secrets are refused, rather than silently changing the user's inputs.
|
|
148
|
+
const secret = await this.tokens.token();
|
|
149
|
+
if (JSON.stringify(scrub(input, secret)) !== JSON.stringify(input))
|
|
150
|
+
throw new Error('token_in_input');
|
|
151
|
+
await writeJson(dir, 'input.json', input);
|
|
152
|
+
await writeJson(dir, 'state.json', { status: 'idle', steps: {}, revisions: {} });
|
|
153
|
+
}
|
|
154
|
+
finally {
|
|
155
|
+
await unlock();
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
async get(dir) {
|
|
159
|
+
const input = await readJson(dir, 'input.json');
|
|
160
|
+
const state = await readJson(dir, 'state.json');
|
|
161
|
+
const attempts = await new FileAttemptStore(await child(dir, 'attempts')).list(input.job.job_id);
|
|
162
|
+
return { state, budget: state.approvedCredits === undefined ? null : new LocalBudget(state.approvedCredits).usage(attempts), paths: { state: resolve(dir, 'state.json'), input: resolve(dir, 'input.json') } };
|
|
163
|
+
}
|
|
164
|
+
async assertNoPaidWork(dir) {
|
|
165
|
+
let exists = false;
|
|
166
|
+
try {
|
|
167
|
+
await lstat(await child(dir, 'input.json'));
|
|
168
|
+
exists = true;
|
|
169
|
+
}
|
|
170
|
+
catch (error) {
|
|
171
|
+
if (error.code !== 'ENOENT')
|
|
172
|
+
throw new Error('job_exists');
|
|
173
|
+
}
|
|
174
|
+
try {
|
|
175
|
+
const attemptsRoot = await child(dir, 'attempts');
|
|
176
|
+
const inspectAttempts = async (path) => {
|
|
177
|
+
for (const entry of await readdir(path, { withFileTypes: true })) {
|
|
178
|
+
if (!entry.isDirectory())
|
|
179
|
+
throw new Error('job_exists');
|
|
180
|
+
await inspectAttempts(join(path, entry.name));
|
|
181
|
+
}
|
|
182
|
+
};
|
|
183
|
+
let attemptsExist = true;
|
|
184
|
+
try {
|
|
185
|
+
await lstat(attemptsRoot);
|
|
186
|
+
}
|
|
187
|
+
catch (error) {
|
|
188
|
+
if (error.code === 'ENOENT')
|
|
189
|
+
attemptsExist = false;
|
|
190
|
+
else
|
|
191
|
+
throw error;
|
|
192
|
+
}
|
|
193
|
+
if (attemptsExist)
|
|
194
|
+
await inspectAttempts(attemptsRoot);
|
|
195
|
+
if (exists) {
|
|
196
|
+
const input = await readJson(dir, 'input.json');
|
|
197
|
+
parseJob(input.job);
|
|
198
|
+
}
|
|
199
|
+
let state;
|
|
200
|
+
try {
|
|
201
|
+
state = await readJson(dir, 'state.json');
|
|
202
|
+
}
|
|
203
|
+
catch (error) {
|
|
204
|
+
if (!exists && error.code === 'ENOENT')
|
|
205
|
+
return;
|
|
206
|
+
throw error;
|
|
207
|
+
}
|
|
208
|
+
if (!state || !['idle', 'running', 'completed', 'failed'].includes(state.status)
|
|
209
|
+
|| !state.steps || typeof state.steps !== 'object' || Array.isArray(state.steps)
|
|
210
|
+
|| !state.revisions || typeof state.revisions !== 'object' || Array.isArray(state.revisions))
|
|
211
|
+
throw new Error('job_exists');
|
|
212
|
+
if (Object.hasOwn(state.steps, 'generate'))
|
|
213
|
+
throw new Error('job_exists');
|
|
214
|
+
}
|
|
215
|
+
catch {
|
|
216
|
+
throw new Error('job_exists');
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
async plan(dir) {
|
|
220
|
+
return this.run(dir, undefined, 'plan');
|
|
221
|
+
}
|
|
222
|
+
async execute(dir, approvedCredits, planDigest) { return this.run(dir, approvedCredits, 'report', planDigest); }
|
|
223
|
+
async revise(dir, request) {
|
|
224
|
+
const input = await readJson(dir, 'input.json');
|
|
225
|
+
if (!input.job.storyboard.segments.some(s => s.id === request.segment))
|
|
226
|
+
return { decision: '需用户决定', reason: '未知段落', ...request };
|
|
227
|
+
if (!['conform', 'effects', 'mix', 'new_seed'].includes(request.action))
|
|
228
|
+
return { decision: '需用户决定', ...request };
|
|
229
|
+
const unlock = await lockJob(dir);
|
|
230
|
+
try {
|
|
231
|
+
const state = await readJson(dir, 'state.json');
|
|
232
|
+
if (request.action === 'new_seed') {
|
|
233
|
+
if (request.approvedCredits === undefined || request.approvedCredits !== state.approvedCredits)
|
|
234
|
+
throw new Error('需携带当前已批准预算;增加预算需用户决定');
|
|
235
|
+
input.output.seed = randomInt(0, 2147483647);
|
|
236
|
+
await writeJson(dir, 'input.json', input);
|
|
237
|
+
}
|
|
238
|
+
else {
|
|
239
|
+
const step = request.action;
|
|
240
|
+
state.revisions[step] = (state.revisions[step] ?? 0) + 1;
|
|
241
|
+
}
|
|
242
|
+
await writeJson(dir, 'state.json', state);
|
|
243
|
+
}
|
|
244
|
+
finally {
|
|
245
|
+
await unlock();
|
|
246
|
+
}
|
|
247
|
+
return { decision: 'ready', segment: request.segment, attribute: request.attribute, action: request.action, note: request.action === 'new_seed' ? '换 seed 重跑全片;不拼接旧片,不保证修好。' : '仅重跑确定性步骤及其后续步骤。' };
|
|
248
|
+
}
|
|
249
|
+
steps() {
|
|
250
|
+
const o = this.options;
|
|
251
|
+
return STEP_NAMES.map(name => ({ name, run: async (c) => {
|
|
252
|
+
const { dir, input } = c;
|
|
253
|
+
const job = input.job;
|
|
254
|
+
const file = (n) => child(dir, n);
|
|
255
|
+
const path = (s) => c.outputs[s].data.path;
|
|
256
|
+
const artifact = (s) => c.outputs[s].data;
|
|
257
|
+
switch (name) {
|
|
258
|
+
case 'analyze': {
|
|
259
|
+
const analysis = await analyzeReference(input.reference);
|
|
260
|
+
if (hash(analysis.source) !== hash(job.storyboard.source))
|
|
261
|
+
throw new Error('reference_source_mismatch');
|
|
262
|
+
return out(analysis);
|
|
263
|
+
}
|
|
264
|
+
case 'plan': {
|
|
265
|
+
const req = mapRequest(compiled(input), c.catalog, input.slots ?? {});
|
|
266
|
+
const quote = await o.gateway.price(req);
|
|
267
|
+
const data = { catalog: o.catalogLabel, currency: c.catalog.currency, lineItems: [{ operation: 'generate', credits: quote.price_credits, pricing_version: quote.pricing_version }], retryReserveCredits: input.retryReserveCredits, totalCredits: quote.price_credits + input.retryReserveCredits, note: '当前仅使用已批准定稿素材;未安排参考图合成。重试预留显式计入总额;价格变化由 PaidRunner 再报价并检查预算。' };
|
|
268
|
+
const planPath = await file('plan.json');
|
|
269
|
+
await writeJson(dir, 'plan.json', data);
|
|
270
|
+
return out(data, [planPath]);
|
|
271
|
+
}
|
|
272
|
+
case 'prepare':
|
|
273
|
+
await validateMedia(dir, job);
|
|
274
|
+
return out({ prepared: true });
|
|
275
|
+
case 'compile': {
|
|
276
|
+
const request = compiled(input);
|
|
277
|
+
await writeJson(dir, 'compiled.json', request);
|
|
278
|
+
return out(request, [await file('compiled.json')]);
|
|
279
|
+
}
|
|
280
|
+
case 'generate': return out(await generateVideo({ dir, job, compiled: artifact('compile'), slots: input.slots ?? {}, catalog: c.catalog, gateway: o.gateway, runner: c.runner, store: new FileAttemptStore(await child(dir, 'attempts')), output: await file('generated.mp4'), poll: o.poll, download: o.download ?? download }), [await file('generated.mp4')]);
|
|
281
|
+
case 'inspect': return out({ checks: await inspectGenerated(job, path('generate')) });
|
|
282
|
+
case 'conform': {
|
|
283
|
+
const target = await file('conformed.mp4');
|
|
284
|
+
await conform(path('generate'), { actual_cuts: locateCuts(await frameDiffs(path('generate')), cutsOf(job.storyboard)), target_cuts: cutsOf(job.storyboard), target_frames: job.storyboard.source.frames }, target);
|
|
285
|
+
return out({ path: target, checks: await inspectConformed(job, target) }, [target]);
|
|
286
|
+
}
|
|
287
|
+
case 'effects': {
|
|
288
|
+
if (!job.storyboard.effects.length)
|
|
289
|
+
return out({ path: path('conform'), skipped: true, note: '没有特效层' });
|
|
290
|
+
if (!c.renderer)
|
|
291
|
+
throw new Error(EFFECT_RENDERER_UNAVAILABLE);
|
|
292
|
+
const result = await c.renderer.render(path('conform'), job.storyboard.effects, await file('effects.mp4'));
|
|
293
|
+
return out({ path: result.path, checks: await inspectComposited(job, path('conform'), result.path, { effectRanges: result.ranges }) }, [result.path]);
|
|
294
|
+
}
|
|
295
|
+
case 'mix': {
|
|
296
|
+
const audio = input.audio ?? ((await probe(input.reference)).has_audio ? input.reference : undefined);
|
|
297
|
+
const result = await mixAudio(path('effects'), audio, await file('mixed.mp4'));
|
|
298
|
+
return out({ ...result, note: result.silent ? '没有音源,静音输出。' : '音源裁至成片时长,短音源补静音、尾部淡出,目标 -14 LUFS;实测见导出检查。' }, [result.path]);
|
|
299
|
+
}
|
|
300
|
+
case 'export': {
|
|
301
|
+
const target = await file('final.mp4');
|
|
302
|
+
await copyFile(path('mix'), target);
|
|
303
|
+
return out({ path: target, checks: await inspectExport(job, target) }, [target]);
|
|
304
|
+
}
|
|
305
|
+
case 'report': {
|
|
306
|
+
const checks = ['inspect', 'conform', 'effects', 'export'].flatMap(s => (artifact(s).checks ?? []));
|
|
307
|
+
checks.push({ check: 'audio_source', check_version: 'engine-v1', status: artifact('mix').silent ? 'not_applicable' : 'pass', evidence: null, note: artifact('mix').note });
|
|
308
|
+
const result = await buildReport(job, checks, path('export'), resolve(dir, 'report'), { referencePath: input.reference, assetRoot: dir });
|
|
309
|
+
return out({ ...result, markdown: await readFile(result.report_md, 'utf8'), path: path('export') }, [result.report_json, result.report_md, ...result.cards]);
|
|
310
|
+
}
|
|
311
|
+
}
|
|
312
|
+
} }));
|
|
313
|
+
}
|
|
314
|
+
async run(dir, approved, until, planDigest) {
|
|
315
|
+
if (until === 'report' && (!Number.isSafeInteger(approved) || approved < 0))
|
|
316
|
+
throw new Error('approved_budget_required:执行必须携带用户已批准积分额');
|
|
317
|
+
const unlock = await lockJob(dir);
|
|
318
|
+
let state;
|
|
319
|
+
let secret = '';
|
|
320
|
+
try {
|
|
321
|
+
const input = await readJson(dir, 'input.json');
|
|
322
|
+
parseJob(input.job);
|
|
323
|
+
state = await readJson(dir, 'state.json');
|
|
324
|
+
state.status = 'running';
|
|
325
|
+
delete state.error;
|
|
326
|
+
secret = await this.tokens.token();
|
|
327
|
+
if (until === 'report')
|
|
328
|
+
state.approvedCredits = approved;
|
|
329
|
+
await writeJson(dir, 'state.json', state);
|
|
330
|
+
let renderer;
|
|
331
|
+
try {
|
|
332
|
+
const unconfirmed = input.job.storyboard.segments.flatMap(segment => Object.entries(segment.tags).filter(([, value]) => value === UNCONFIRMED_TAG).map(([tag]) => `${segment.id}.${tag}`));
|
|
333
|
+
if (unconfirmed.length)
|
|
334
|
+
throw new Error(`unconfirmed_tags: ${unconfirmed.join(', ')}`);
|
|
335
|
+
if (until === 'report' && planDigest !== undefined && computePlanDigest(input) !== planDigest) {
|
|
336
|
+
throw new Error('plan_digest_mismatch:任务输入与已确认计划摘要不一致,请重新运行 plan_job 并由用户确认。');
|
|
337
|
+
}
|
|
338
|
+
if (input.job.storyboard.effects.length) {
|
|
339
|
+
if (!this.options.effects)
|
|
340
|
+
throw new Error(EFFECT_RENDERER_UNAVAILABLE);
|
|
341
|
+
renderer = this.options.effects(input.job);
|
|
342
|
+
await renderer.validate(input.job.storyboard.effects);
|
|
343
|
+
if (until === 'report' && state.steps.effects?.status !== 'completed')
|
|
344
|
+
await renderer.checkRuntime?.();
|
|
345
|
+
}
|
|
346
|
+
}
|
|
347
|
+
catch (error) {
|
|
348
|
+
const safe = safeError(error, secret);
|
|
349
|
+
state.error = safe;
|
|
350
|
+
if (state.steps.plan?.status !== 'completed')
|
|
351
|
+
state.steps.plan = { hash: '', status: 'failed', error: safe };
|
|
352
|
+
throw new Error(safe);
|
|
353
|
+
}
|
|
354
|
+
const catalog = await this.options.gateway.catalog();
|
|
355
|
+
if (!catalog.modified)
|
|
356
|
+
throw new Error('catalog_required');
|
|
357
|
+
const store = new FileAttemptStore(await child(dir, 'attempts'));
|
|
358
|
+
const runner = until === 'report' ? new PaidRunner({ jobId: input.job.job_id, gateway: this.options.gateway, tokens: this.tokens, budget: new LocalBudget(approved), store }) : undefined;
|
|
359
|
+
if (runner)
|
|
360
|
+
await validateMedia(dir, input.job);
|
|
361
|
+
await runner?.recover();
|
|
362
|
+
if (runner)
|
|
363
|
+
await refreshUnsettledRefunds(runner, store, input.job.job_id);
|
|
364
|
+
if ((state.steps.plan?.status === 'completed' || state.steps.compile?.status === 'completed')
|
|
365
|
+
&& (await store.list(input.job.job_id)).length === 0)
|
|
366
|
+
compiled(input);
|
|
367
|
+
const c = { dir, input, catalog: catalog.catalog, renderer, ...(runner ? { runner } : {}), outputs: {} };
|
|
368
|
+
let chain = '';
|
|
369
|
+
for (const step of this.steps()) {
|
|
370
|
+
const local = step.name === 'analyze' ? { source: input.job.storyboard.source, reference: await sha256File(input.reference) }
|
|
371
|
+
: step.name === 'plan' ? { job: input.job, output: input.output, catalog: c.catalog, slots: input.slots, reserve: input.retryReserveCredits }
|
|
372
|
+
: step.name === 'mix' ? { audio: input.audio ? await sha256File(input.audio) : null }
|
|
373
|
+
: step.name === 'effects' ? { renderer: renderer?.version } : {};
|
|
374
|
+
if (step.name === 'prepare')
|
|
375
|
+
Object.assign(local, { assets: await Promise.all(input.job.assets.map(async (a) => [a.id, await sha256File(await child(dir, ...a.path.split('/')))])) });
|
|
376
|
+
const key = hash({ chain, local, revision: state.revisions[step.name] ?? 0, version: 1 });
|
|
377
|
+
const saved = state.steps[step.name];
|
|
378
|
+
let filesValid = saved?.status === 'completed';
|
|
379
|
+
if (filesValid)
|
|
380
|
+
for (const [path, digest] of Object.entries(saved.files ?? {})) {
|
|
381
|
+
try {
|
|
382
|
+
if (await sha256File(path) !== digest)
|
|
383
|
+
filesValid = false;
|
|
384
|
+
}
|
|
385
|
+
catch {
|
|
386
|
+
filesValid = false;
|
|
387
|
+
}
|
|
388
|
+
}
|
|
389
|
+
let valid = filesValid && saved.hash === key;
|
|
390
|
+
if (!valid && step.name === 'generate' && filesValid && saved?.output) {
|
|
391
|
+
const attempt = (await store.list(input.job.job_id)).find(a => a.idempotency_key === saved.output.data.attempt);
|
|
392
|
+
const current = c.outputs.compile.data;
|
|
393
|
+
if (attempt?.state === 'completed' && same(attempt.body.params) === same({ ...current.params, prompt: current.prompt })) {
|
|
394
|
+
saved.hash = key;
|
|
395
|
+
await writeJson(dir, 'state.json', state);
|
|
396
|
+
valid = true;
|
|
397
|
+
}
|
|
398
|
+
}
|
|
399
|
+
if (valid)
|
|
400
|
+
c.outputs[step.name] = saved.output;
|
|
401
|
+
else {
|
|
402
|
+
state.steps[step.name] = { hash: key, status: 'running' };
|
|
403
|
+
await writeJson(dir, 'state.json', state);
|
|
404
|
+
try {
|
|
405
|
+
const output = scrub(await step.run(c), secret);
|
|
406
|
+
const files = {};
|
|
407
|
+
for (const path of output.files)
|
|
408
|
+
files[path] = await sha256File(path);
|
|
409
|
+
c.outputs[step.name] = output;
|
|
410
|
+
state.steps[step.name] = { hash: key, status: 'completed', output, files };
|
|
411
|
+
}
|
|
412
|
+
catch (error) {
|
|
413
|
+
const safe = safeError(error, secret);
|
|
414
|
+
state.steps[step.name] = { hash: key, status: 'failed', error: safe };
|
|
415
|
+
throw new Error(safe);
|
|
416
|
+
}
|
|
417
|
+
await writeJson(dir, 'state.json', state);
|
|
418
|
+
}
|
|
419
|
+
chain = hash({ key, output: c.outputs[step.name] });
|
|
420
|
+
if (until === 'plan' && step.name === 'plan')
|
|
421
|
+
break;
|
|
422
|
+
}
|
|
423
|
+
state.status = until === 'report' ? 'completed' : 'idle';
|
|
424
|
+
await writeJson(dir, 'state.json', state);
|
|
425
|
+
const result = { state, result: c.outputs[until], paths: c.outputs[until]?.files ?? [] };
|
|
426
|
+
if (until === 'plan') {
|
|
427
|
+
const digest = computePlanDigest(input);
|
|
428
|
+
return { ...result, planDigest: digest, markdown: scrub(planCard(input, c.outputs.plan.data, compileWan3(compilePlan(input)).issues.filter(i => i.level === 'warn'), digest), secret) };
|
|
429
|
+
}
|
|
430
|
+
return result;
|
|
431
|
+
}
|
|
432
|
+
catch (error) {
|
|
433
|
+
if (state) {
|
|
434
|
+
state.status = 'failed';
|
|
435
|
+
state.error ??= safeError(error, secret);
|
|
436
|
+
await writeJson(dir, 'state.json', state);
|
|
437
|
+
}
|
|
438
|
+
throw error;
|
|
439
|
+
}
|
|
440
|
+
finally {
|
|
441
|
+
await unlock();
|
|
442
|
+
}
|
|
443
|
+
}
|
|
444
|
+
}
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
import { lstat, open, readFile, rename, rm } from 'node:fs/promises';
|
|
2
|
+
import { randomUUID } from 'node:crypto';
|
|
3
|
+
import { hostname } from 'node:os';
|
|
4
|
+
import { child } from '../assets/files.js';
|
|
5
|
+
import { assertNoSignedUrl } from '../core/index.js';
|
|
6
|
+
export async function writeJson(dir, name, value) {
|
|
7
|
+
assertNoSignedUrl(value);
|
|
8
|
+
const destination = await child(dir, name);
|
|
9
|
+
const temp = await child(dir, `${randomUUID()}.tmp`);
|
|
10
|
+
try {
|
|
11
|
+
const f = await open(temp, 'wx', 0o600);
|
|
12
|
+
try {
|
|
13
|
+
await f.writeFile(JSON.stringify(value, null, 2));
|
|
14
|
+
await f.sync();
|
|
15
|
+
}
|
|
16
|
+
finally {
|
|
17
|
+
await f.close();
|
|
18
|
+
}
|
|
19
|
+
await rename(temp, destination);
|
|
20
|
+
}
|
|
21
|
+
finally {
|
|
22
|
+
await rm(temp, { force: true });
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
export async function readJson(dir, name) {
|
|
26
|
+
return JSON.parse(await readFile(await child(dir, name), 'utf8'));
|
|
27
|
+
}
|
|
28
|
+
/** Only this host's provably dead owner may be reclaimed; never steal by age. */
|
|
29
|
+
export async function lockJob(dir) {
|
|
30
|
+
const path = await child(dir, 'engine.lock');
|
|
31
|
+
const marker = await child(dir, 'engine.recovering');
|
|
32
|
+
const busy = () => new Error('job_locked:任务正在运行,或无法确认原锁归属;请稍后重试或联系支持。');
|
|
33
|
+
const recovering = () => new Error('job_locked:任务锁正在恢复或恢复标记残留;请稍后重试或联系支持。');
|
|
34
|
+
const deadOwner = async (target = path, onFail = busy) => {
|
|
35
|
+
let owner;
|
|
36
|
+
try {
|
|
37
|
+
owner = JSON.parse(await readFile(target, 'utf8'));
|
|
38
|
+
}
|
|
39
|
+
catch {
|
|
40
|
+
throw onFail();
|
|
41
|
+
}
|
|
42
|
+
if (owner.hostname !== hostname() || !Number.isSafeInteger(owner.pid) || owner.pid <= 0)
|
|
43
|
+
throw onFail();
|
|
44
|
+
try {
|
|
45
|
+
process.kill(owner.pid, 0);
|
|
46
|
+
}
|
|
47
|
+
catch (error) {
|
|
48
|
+
if (error.code === 'ESRCH')
|
|
49
|
+
return;
|
|
50
|
+
}
|
|
51
|
+
throw onFail();
|
|
52
|
+
};
|
|
53
|
+
try {
|
|
54
|
+
await lstat(marker);
|
|
55
|
+
await deadOwner(marker, recovering);
|
|
56
|
+
await rm(marker, { force: true });
|
|
57
|
+
}
|
|
58
|
+
catch (error) {
|
|
59
|
+
if (error.code !== 'ENOENT')
|
|
60
|
+
throw error;
|
|
61
|
+
}
|
|
62
|
+
const f = await open(path, 'wx', 0o600).catch(async (error) => {
|
|
63
|
+
if (error.code !== 'EEXIST')
|
|
64
|
+
throw error;
|
|
65
|
+
await deadOwner();
|
|
66
|
+
const guard = await open(marker, 'wx', 0o600).catch(() => { throw recovering(); });
|
|
67
|
+
try {
|
|
68
|
+
await guard.writeFile(JSON.stringify({ pid: process.pid, hostname: hostname() }));
|
|
69
|
+
await guard.sync();
|
|
70
|
+
// Re-read under the exclusive marker: a previous recoverer may have won.
|
|
71
|
+
await deadOwner();
|
|
72
|
+
await rm(path);
|
|
73
|
+
return await open(path, 'wx', 0o600).catch(() => { throw busy(); });
|
|
74
|
+
}
|
|
75
|
+
finally {
|
|
76
|
+
await guard.close();
|
|
77
|
+
await rm(marker, { force: true });
|
|
78
|
+
}
|
|
79
|
+
});
|
|
80
|
+
try {
|
|
81
|
+
await f.writeFile(JSON.stringify({ pid: process.pid, hostname: hostname() }));
|
|
82
|
+
await f.sync();
|
|
83
|
+
}
|
|
84
|
+
catch (error) {
|
|
85
|
+
await f.close();
|
|
86
|
+
await rm(path);
|
|
87
|
+
throw error;
|
|
88
|
+
}
|
|
89
|
+
return async () => { await f.close(); await rm(path); };
|
|
90
|
+
}
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
import { randomBytes } from "node:crypto";
|
|
2
|
+
import { lstat, mkdir, open, readdir, readFile, rename, rm } from "node:fs/promises";
|
|
3
|
+
import { dirname, join, resolve } from "node:path";
|
|
4
|
+
import { assertNoSignedUrl, isSafeId } from "../core/index.js";
|
|
5
|
+
import { attemptSchema } from "./types.js";
|
|
6
|
+
function validated(attempt) {
|
|
7
|
+
const parsed = attemptSchema.parse(attempt);
|
|
8
|
+
assertNoSignedUrl(parsed);
|
|
9
|
+
return parsed;
|
|
10
|
+
}
|
|
11
|
+
function byCreation(a, b) {
|
|
12
|
+
return a.created_at.localeCompare(b.created_at) || a.idempotency_key.localeCompare(b.idempotency_key);
|
|
13
|
+
}
|
|
14
|
+
export class MemoryAttemptStore {
|
|
15
|
+
jobs = new Map();
|
|
16
|
+
async put(a) {
|
|
17
|
+
const attempt = validated(a);
|
|
18
|
+
const job = this.jobs.get(attempt.job_id) ?? new Map();
|
|
19
|
+
job.set(attempt.idempotency_key, structuredClone(attempt));
|
|
20
|
+
this.jobs.set(attempt.job_id, job);
|
|
21
|
+
}
|
|
22
|
+
async list(jobId) {
|
|
23
|
+
const attempts = [...(this.jobs.get(jobId)?.values() ?? [])];
|
|
24
|
+
return structuredClone(attempts).sort(byCreation);
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
async function syncDirectory(dir) {
|
|
28
|
+
const handle = await open(dir, "r");
|
|
29
|
+
try {
|
|
30
|
+
await handle.sync();
|
|
31
|
+
}
|
|
32
|
+
finally {
|
|
33
|
+
await handle.close();
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
/** mkdir created everything from `first` down: the parents of those at or above root hold new entries. */
|
|
37
|
+
async function syncCreatedAncestors(first, root) {
|
|
38
|
+
if (first === undefined) {
|
|
39
|
+
return;
|
|
40
|
+
}
|
|
41
|
+
for (let dir = root; dir.length >= first.length; dir = dirname(dir)) {
|
|
42
|
+
await syncDirectory(dirname(dir));
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
/** job_id is one path segment, so a symlink here is the only way out of root. */
|
|
46
|
+
async function assertNotSymlink(dir) {
|
|
47
|
+
try {
|
|
48
|
+
if ((await lstat(dir)).isSymbolicLink()) {
|
|
49
|
+
throw new Error("job directory must not be a symlink");
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
catch (err) {
|
|
53
|
+
if (err.code !== "ENOENT") {
|
|
54
|
+
throw err;
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
/** One file per attempt: <root>/<job_id>/<idempotency_key>.json, replaced atomically. */
|
|
59
|
+
export class FileAttemptStore {
|
|
60
|
+
root;
|
|
61
|
+
constructor(root) {
|
|
62
|
+
this.root = resolve(root);
|
|
63
|
+
}
|
|
64
|
+
async put(a) {
|
|
65
|
+
const attempt = validated(a);
|
|
66
|
+
const dir = join(this.root, attempt.job_id);
|
|
67
|
+
await assertNotSymlink(dir);
|
|
68
|
+
const created = await mkdir(dir, { recursive: true, mode: 0o700 });
|
|
69
|
+
const tmp = join(dir, `.${randomBytes(8).toString("hex")}.tmp`);
|
|
70
|
+
try {
|
|
71
|
+
const handle = await open(tmp, "wx", 0o600);
|
|
72
|
+
try {
|
|
73
|
+
await handle.writeFile(`${JSON.stringify(attempt, null, 2)}\n`);
|
|
74
|
+
await handle.sync();
|
|
75
|
+
}
|
|
76
|
+
finally {
|
|
77
|
+
await handle.close();
|
|
78
|
+
}
|
|
79
|
+
await rename(tmp, join(dir, `${attempt.idempotency_key}.json`));
|
|
80
|
+
}
|
|
81
|
+
catch (err) {
|
|
82
|
+
await rm(tmp, { force: true });
|
|
83
|
+
throw err;
|
|
84
|
+
}
|
|
85
|
+
await syncDirectory(dir);
|
|
86
|
+
await syncDirectory(this.root);
|
|
87
|
+
await syncCreatedAncestors(created, this.root);
|
|
88
|
+
}
|
|
89
|
+
/** A corrupt record throws: losing sight of a charge must not unblock spending. */
|
|
90
|
+
async list(jobId) {
|
|
91
|
+
if (!isSafeId(jobId)) {
|
|
92
|
+
throw new Error("job id must be a single path segment");
|
|
93
|
+
}
|
|
94
|
+
const dir = join(this.root, jobId);
|
|
95
|
+
await assertNotSymlink(dir);
|
|
96
|
+
let names;
|
|
97
|
+
try {
|
|
98
|
+
names = await readdir(dir);
|
|
99
|
+
}
|
|
100
|
+
catch (err) {
|
|
101
|
+
if (err.code === "ENOENT") {
|
|
102
|
+
return [];
|
|
103
|
+
}
|
|
104
|
+
throw err;
|
|
105
|
+
}
|
|
106
|
+
const files = names.filter((name) => name.endsWith(".json") && !name.startsWith("."));
|
|
107
|
+
const attempts = await Promise.all(files.map(async (name) => attemptSchema.parse(JSON.parse(await readFile(join(dir, name), "utf8")))));
|
|
108
|
+
return attempts.sort(byCreation);
|
|
109
|
+
}
|
|
110
|
+
}
|