@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,276 @@
|
|
|
1
|
+
import { mkdir, writeFile, rm } from "node:fs/promises";
|
|
2
|
+
import { resolve, join } from "node:path";
|
|
3
|
+
import { cutsOf } from "../core/schema.js";
|
|
4
|
+
import { probe, frameDiffs, locateCuts, detectCuts, timelineParams } from "../timeline/index.js";
|
|
5
|
+
import { command, frames, still } from "./media.js";
|
|
6
|
+
const VERSION = "inspect-v1-pending-calibration";
|
|
7
|
+
function result(check, status, evidence, note, segment) {
|
|
8
|
+
return { check, check_version: VERSION, status, evidence, ...(note ? { note } : {}), ...(segment ? { segment } : {}) };
|
|
9
|
+
}
|
|
10
|
+
function error(check, e) { return result(check, "error", String(e)); }
|
|
11
|
+
function nonnegative(n) {
|
|
12
|
+
if (!Number.isFinite(n) || n < 0)
|
|
13
|
+
throw new Error("threshold must be finite and nonnegative");
|
|
14
|
+
return n;
|
|
15
|
+
}
|
|
16
|
+
async function cutChecks(job, path, exact, opts) {
|
|
17
|
+
try {
|
|
18
|
+
const diffs = await frameDiffs(path);
|
|
19
|
+
const expected = cutsOf(job.storyboard);
|
|
20
|
+
const p = opts.timeline ?? timelineParams;
|
|
21
|
+
// Missing tail segments must not prevent checking all other boundaries.
|
|
22
|
+
const valid = expected.filter(c => c < diffs.length + 1);
|
|
23
|
+
const located = locateCuts(diffs, valid, p);
|
|
24
|
+
const checks = expected.map((cut, i) => {
|
|
25
|
+
const found = located.find(c => c.expected === cut);
|
|
26
|
+
const status = !found || found.status === "not_found" ? "fail" : found.status === "ambiguous" ? "suspect" : exact && (found.frame !== cut || found.start !== cut) ? "fail" : "pass";
|
|
27
|
+
return result("cut", status, found ?? { expected: cut, status: "not_found" }, "仅检查切点,不判断段内容或顺序", job.storyboard.segments[i + 1]?.id);
|
|
28
|
+
});
|
|
29
|
+
if (exact) {
|
|
30
|
+
const actual = detectCuts(diffs, p).map(c => c.frame);
|
|
31
|
+
checks.push(result("all_cuts", JSON.stringify(actual) === JSON.stringify(expected) ? "pass" : "fail", { expected, actual }, "阈值待校准;不能据此确认服装或段落语义"));
|
|
32
|
+
}
|
|
33
|
+
checks.push(result("segment_semantics", "unsupported", null, "删段、换序、重复若保留切点,软件不能确认;服装、身份、局部细节需人工看"));
|
|
34
|
+
return checks;
|
|
35
|
+
}
|
|
36
|
+
catch (e) {
|
|
37
|
+
return [error("cuts", e)];
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
async function basic(job, path, opts) {
|
|
41
|
+
try {
|
|
42
|
+
const actual = await probe(path);
|
|
43
|
+
const expected = job.storyboard.source;
|
|
44
|
+
const duration = actual.frames * actual.fps.den / actual.fps.num;
|
|
45
|
+
const target = expected.frames * expected.fps.den / expected.fps.num;
|
|
46
|
+
const dimensions = opts.expectedDimensions;
|
|
47
|
+
if (dimensions && (!Number.isInteger(dimensions.width) || dimensions.width <= 0 || !Number.isInteger(dimensions.height) || dimensions.height <= 0))
|
|
48
|
+
throw new Error("expected dimensions must be positive integers");
|
|
49
|
+
return [result("readable", "pass", actual),
|
|
50
|
+
result("fps", actual.fps.num * expected.fps.den === expected.fps.num * actual.fps.den ? "pass" : "fail", { expected: expected.fps, actual: actual.fps }),
|
|
51
|
+
result("resolution", !dimensions ? "not_applicable" : actual.width === dimensions.width && actual.height === dimensions.height ? "pass" : "fail", { expected: dimensions ? [dimensions.width, dimensions.height] : null, actual: [actual.width, actual.height] }, dimensions ? "与当前输出目标尺寸比较" : "未提供当前输出目标尺寸;原始参考尺寸不是生成目标"),
|
|
52
|
+
result("duration", Math.abs(duration - target) <= nonnegative(opts.durationToleranceSeconds ?? 0.05) ? "pass" : "fail", { expected: target, actual: duration, tolerance: opts.durationToleranceSeconds ?? 0.05 })];
|
|
53
|
+
}
|
|
54
|
+
catch (e) {
|
|
55
|
+
return [error("readable", e)];
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
function targetFor(job, segment, opts) {
|
|
59
|
+
if (opts.targets?.[segment])
|
|
60
|
+
return opts.targets[segment];
|
|
61
|
+
const binding = job.bindings.find(b => b.segment === segment && job.assets.some(a => a.id === b.asset && a.role === "product"));
|
|
62
|
+
const asset = job.assets.find(a => a.id === binding?.asset);
|
|
63
|
+
return asset && opts.assetRoot ? resolve(opts.assetRoot, asset.path) : undefined;
|
|
64
|
+
}
|
|
65
|
+
function foreground(image, plate, delta, minimum) {
|
|
66
|
+
const sum = [0, 0, 0];
|
|
67
|
+
let count = 0;
|
|
68
|
+
for (let i = 0; i < image.length; i += 3) {
|
|
69
|
+
if ((Math.abs(image[i] - plate[i]) + Math.abs(image[i + 1] - plate[i + 1]) + Math.abs(image[i + 2] - plate[i + 2])) / 3 <= delta)
|
|
70
|
+
continue;
|
|
71
|
+
count++;
|
|
72
|
+
for (let k = 0; k < 3; k++)
|
|
73
|
+
sum[k] = sum[k] + image[i + k];
|
|
74
|
+
}
|
|
75
|
+
return count / (image.length / 3) < minimum || !count ? null : sum.map(v => v / count);
|
|
76
|
+
}
|
|
77
|
+
async function colors(job, path, opts) {
|
|
78
|
+
const checks = [];
|
|
79
|
+
for (const s of job.storyboard.segments) {
|
|
80
|
+
const target = targetFor(job, s.id, opts);
|
|
81
|
+
if (!opts.cleanPlate || opts.fixedCamera !== true || !target) {
|
|
82
|
+
checks.push(result("foreground_color", "not_applicable", { clean_plate: !!opts.cleanPlate, fixed_camera: opts.fixedCamera === true, target: !!target }, "需空景帧、固定机位和目标素材;服装需人工看", s.id));
|
|
83
|
+
continue;
|
|
84
|
+
}
|
|
85
|
+
try {
|
|
86
|
+
const [image, plate, material] = await Promise.all([still(path, Math.floor((s.start_frame + s.end_frame - 1) / 2)), still(opts.cleanPlate), still(target)]);
|
|
87
|
+
const delta = nonnegative(opts.foregroundDelta ?? 20);
|
|
88
|
+
const minimum = nonnegative(opts.minimumForegroundFraction ?? 0.02);
|
|
89
|
+
const a = foreground(image, plate, delta, minimum), b = foreground(material, plate, delta, minimum);
|
|
90
|
+
const distance = a && b ? Math.hypot(...a.map((v, i) => v - b[i])) : null;
|
|
91
|
+
const threshold = nonnegative(opts.colorDistance ?? 100);
|
|
92
|
+
checks.push(result("foreground_color", distance !== null && distance > threshold ? "suspect" : "unsupported", { distance, threshold, foreground: a, target: b, frame: Math.floor((s.start_frame + s.end_frame - 1) / 2), pending_calibration: true }, "粗略颜色提示;相近或前景不足为 inconclusive,不代表衣服正确;需人工看。目标与空景背景差异也可能误报", s.id));
|
|
93
|
+
}
|
|
94
|
+
catch (e) {
|
|
95
|
+
checks.push({ ...error("foreground_color", e), segment: s.id });
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
return checks;
|
|
99
|
+
}
|
|
100
|
+
export async function inspectGenerated(job, artifactPath, opts = {}) {
|
|
101
|
+
return [...await basic(job, artifactPath, opts), ...await cutChecks(job, artifactPath, false, opts), ...await colors(job, artifactPath, opts)];
|
|
102
|
+
}
|
|
103
|
+
export async function inspectConformed(job, artifactPath, opts = {}) {
|
|
104
|
+
const checks = await basic(job, artifactPath, opts);
|
|
105
|
+
try {
|
|
106
|
+
const m = await probe(artifactPath);
|
|
107
|
+
checks.push(result("frame_count", m.frames === job.storyboard.source.frames ? "pass" : "fail", { expected: job.storyboard.source.frames, actual: m.frames }));
|
|
108
|
+
}
|
|
109
|
+
catch (e) {
|
|
110
|
+
checks.push(error("frame_count", e));
|
|
111
|
+
}
|
|
112
|
+
return [...checks, ...await cutChecks(job, artifactPath, true, opts)];
|
|
113
|
+
}
|
|
114
|
+
export async function inspectComposited(job, basePath, compositePath, opts = {}) {
|
|
115
|
+
const checks = [];
|
|
116
|
+
const boundaries = [0, ...cutsOf(job.storyboard), job.storyboard.source.frames];
|
|
117
|
+
const ranges = job.storyboard.effects.map(e => [boundaries[e.start_cut], boundaries[e.end_cut]]);
|
|
118
|
+
if (!job.storyboard.effects.length)
|
|
119
|
+
checks.push(result("effect_timing", "not_applicable", [], "没有特效"));
|
|
120
|
+
job.storyboard.effects.forEach((_, i) => {
|
|
121
|
+
const actual = opts.effectRanges?.filter(r => r.effect === i);
|
|
122
|
+
const expected = ranges[i];
|
|
123
|
+
checks.push(result("effect_timing", actual?.length === 1 ? Number.isInteger(actual[0].start_frame) && Number.isInteger(actual[0].end_frame) && actual[0].start_frame === expected[0] && actual[0].end_frame === expected[1] ? "pass" : "fail" : "unsupported", { effect: i, expected, actual }, "基于调用方提供的实测特效层范围;无法从成片推断不可见层的时间"));
|
|
124
|
+
});
|
|
125
|
+
try {
|
|
126
|
+
const [base, composite] = await Promise.all([probe(basePath), probe(compositePath)]);
|
|
127
|
+
checks.push(result("base_frame_count", base.frames === composite.frames ? "pass" : "fail", { base: base.frames, composite: composite.frames }));
|
|
128
|
+
const [bd, cd] = await Promise.all([frameDiffs(basePath), frameDiffs(compositePath)]);
|
|
129
|
+
const params = opts.timeline ?? timelineParams;
|
|
130
|
+
const expected = cutsOf(job.storyboard);
|
|
131
|
+
const baseLocated = locateCuts(bd, expected.filter(c => c < base.frames), params);
|
|
132
|
+
const compositeLocated = locateCuts(cd, expected.filter(c => c < composite.frames), params);
|
|
133
|
+
for (const [index, cut] of expected.entries()) {
|
|
134
|
+
const b = baseLocated.find(c => c.expected === cut), c = compositeLocated.find(c => c.expected === cut);
|
|
135
|
+
const status = !b || !c || b.status === "not_found" || c.status === "not_found" ? "fail" : b.status === "ambiguous" || c.status === "ambiguous" ? "suspect" : b.start === c.start && b.frame === c.frame ? "pass" : "fail";
|
|
136
|
+
checks.push(result("base_cut", status, { expected: cut, base: b, composite: c }, "比较全部预期切点在基础片与合成片中的位置", job.storyboard.segments[index + 1]?.id));
|
|
137
|
+
}
|
|
138
|
+
const bc = detectCuts(bd, params), cc = detectCuts(cd, params);
|
|
139
|
+
checks.push(result("base_cuts", JSON.stringify(bc.map(c => c.frame)) === JSON.stringify(cc.map(c => c.frame)) ? "pass" : "fail", { base: bc, composite: cc }, "切点阈值待校准"));
|
|
140
|
+
if (base.width !== composite.width || base.height !== composite.height || base.frames !== composite.frames || base.fps.num * composite.fps.den !== composite.fps.num * base.fps.den) {
|
|
141
|
+
checks.push(result("outside_effects", "fail", { base, composite }, "无法逐帧同尺寸对比"));
|
|
142
|
+
}
|
|
143
|
+
else {
|
|
144
|
+
const meanTolerance = nonnegative(opts.outsideMeanTolerance ?? 2);
|
|
145
|
+
const pixelTolerance = nonnegative(opts.outsidePixelTolerance ?? 12);
|
|
146
|
+
const fractionTolerance = nonnegative(opts.outsideChangedFraction ?? 0.01);
|
|
147
|
+
const bytes = base.width * base.height * 3;
|
|
148
|
+
const left = frames(basePath, bytes), right = frames(compositePath, bytes);
|
|
149
|
+
const changed = [];
|
|
150
|
+
let tested = 0;
|
|
151
|
+
try {
|
|
152
|
+
for (let f = 0; f < base.frames; f++) {
|
|
153
|
+
const [a, b] = await Promise.all([left.next(), right.next()]);
|
|
154
|
+
if (a.done || b.done)
|
|
155
|
+
throw new Error("decoder ended before probed frame count");
|
|
156
|
+
if (ranges.some(([start, end]) => f >= start && f < end))
|
|
157
|
+
continue;
|
|
158
|
+
tested++;
|
|
159
|
+
let sum = 0, pixels = 0;
|
|
160
|
+
for (let j = 0; j < bytes; j += 3) {
|
|
161
|
+
let max = 0;
|
|
162
|
+
for (let k = 0; k < 3; k++) {
|
|
163
|
+
const d = Math.abs(a.value[j + k] - b.value[j + k]);
|
|
164
|
+
sum += d;
|
|
165
|
+
max = Math.max(max, d);
|
|
166
|
+
}
|
|
167
|
+
if (max > pixelTolerance)
|
|
168
|
+
pixels++;
|
|
169
|
+
}
|
|
170
|
+
const mean = sum / bytes, fraction = pixels / (bytes / 3);
|
|
171
|
+
if (mean > meanTolerance || fraction > fractionTolerance)
|
|
172
|
+
changed.push({ frame: f, mean, fraction });
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
finally {
|
|
176
|
+
await Promise.all([left.return(undefined), right.return(undefined)]);
|
|
177
|
+
}
|
|
178
|
+
checks.push(result("outside_effects", tested === 0 ? "not_applicable" : changed.length ? "fail" : "pass", { tested, changed, meanTolerance, pixelTolerance, fractionTolerance, pending_calibration: true }));
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
catch (e) {
|
|
182
|
+
checks.push(error("composite_comparison", e));
|
|
183
|
+
}
|
|
184
|
+
return checks;
|
|
185
|
+
}
|
|
186
|
+
export async function inspectExport(_job, artifactPath, opts = {}) {
|
|
187
|
+
const checks = [];
|
|
188
|
+
try {
|
|
189
|
+
const { stdout } = await command("ffprobe", ["-v", "error", "-show_streams", "-of", "json", artifactPath]);
|
|
190
|
+
const streams = JSON.parse(stdout).streams;
|
|
191
|
+
const v = streams.find(s => s.codec_type === "video"), a = streams.find(s => s.codec_type === "audio");
|
|
192
|
+
checks.push(result("codec", !v ? "fail" : (opts.codecs ?? ["h264"]).includes(v.codec_name ?? "") ? "pass" : "fail", { actual: v?.codec_name, allowed: opts.codecs ?? ["h264"] }));
|
|
193
|
+
checks.push(result("pixel_format", !v ? "fail" : (opts.pixelFormats ?? ["yuv420p"]).includes(v.pix_fmt ?? "") ? "pass" : "fail", { actual: v?.pix_fmt, allowed: opts.pixelFormats ?? ["yuv420p"] }));
|
|
194
|
+
const delta = v?.duration !== undefined && a?.duration !== undefined ? Math.abs(Number(v.duration) - Number(a.duration)) : NaN;
|
|
195
|
+
const tolerance = nonnegative(opts.avToleranceSeconds ?? 0.1);
|
|
196
|
+
checks.push(result("av_duration", !a ? "not_applicable" : !Number.isFinite(delta) ? "unsupported" : delta <= tolerance ? "pass" : "fail", { delta: Number.isFinite(delta) ? delta : null, tolerance, pending_calibration: true }, "音画时长差;阈值待校准"));
|
|
197
|
+
if (!a) {
|
|
198
|
+
checks.push(result("loudness", "not_applicable", null, "没有音轨"), result("true_peak", "not_applicable", null, "没有音轨"));
|
|
199
|
+
}
|
|
200
|
+
else {
|
|
201
|
+
const { stderr } = await command("ffmpeg", ["-hide_banner", "-nostats", "-i", artifactPath, "-map", "0:a:0", "-af", "ebur128=peak=true", "-f", "null", "-"]);
|
|
202
|
+
const summary = stderr.slice(stderr.lastIndexOf("Summary:"));
|
|
203
|
+
const integrated = /I:\s*(-?(?:\d+(?:\.\d+)?|inf))\s*LUFS/.exec(summary);
|
|
204
|
+
const peak = /Peak:\s*(-?(?:\d+(?:\.\d+)?|inf))\s*dBFS/.exec(summary);
|
|
205
|
+
const target = opts.loudnessTarget ?? -14, lt = nonnegative(opts.loudnessTolerance ?? 2), max = opts.truePeakMax ?? -1;
|
|
206
|
+
if (!Number.isFinite(target) || !Number.isFinite(max))
|
|
207
|
+
throw new Error("invalid audio threshold");
|
|
208
|
+
const i = integrated ? Number(integrated[1]) : NaN, p = peak ? Number(peak[1]) : NaN;
|
|
209
|
+
checks.push(result("loudness", Number.isNaN(i) ? "unsupported" : Math.abs(i - target) <= lt ? "pass" : "fail", { integrated_lufs: Number.isFinite(i) ? i : String(i), target, tolerance: lt, pending_calibration: true }, "ebur128 实测;阈值待校准"));
|
|
210
|
+
checks.push(result("true_peak", Number.isNaN(p) ? "unsupported" : p <= max ? "pass" : "fail", { peak_dbfs: Number.isFinite(p) ? p : String(p), max, pending_calibration: true }, "ebur128 实测;阈值待校准"));
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
catch (e) {
|
|
214
|
+
checks.push(error("export", e));
|
|
215
|
+
}
|
|
216
|
+
return checks;
|
|
217
|
+
}
|
|
218
|
+
export function coverage(job, results) {
|
|
219
|
+
return job.requirements.map(r => {
|
|
220
|
+
// No software check in v1 establishes semantic requirements.
|
|
221
|
+
if (r.subject !== "structure" || !["frame_count", "cut", "all_cuts", "fps", "resolution", "duration"].includes(r.attribute))
|
|
222
|
+
return { requirement: r.id, state: "needs_human" };
|
|
223
|
+
const checks = results.filter(c => c.check === r.attribute && (c.requirement === r.id || (!c.requirement && (!c.segment || c.segment === r.segment))));
|
|
224
|
+
const worst = checks.find(c => c.status === "fail") ?? checks.find(c => c.status === "suspect") ?? (checks.length && checks.every(c => c.status === "pass") ? checks[0] : undefined);
|
|
225
|
+
return worst ? { requirement: r.id, state: "checked", status: worst.status } : { requirement: r.id, state: "unchecked", ...(checks[0] ? { status: checks[0].status } : {}) };
|
|
226
|
+
});
|
|
227
|
+
}
|
|
228
|
+
export async function buildReport(job, results, artifactPath, outDir, opts = {}) {
|
|
229
|
+
await mkdir(outDir, { recursive: true });
|
|
230
|
+
const cards = [], cardChecks = [];
|
|
231
|
+
for (const [index, s] of job.storyboard.segments.entries()) {
|
|
232
|
+
const target = targetFor(job, s.id, opts);
|
|
233
|
+
if (!opts.referencePath || !target) {
|
|
234
|
+
cardChecks.push(result("comparison_card", "not_applicable", { reference: !!opts.referencePath, target: !!target }, "缺少原始参考或目标素材", s.id));
|
|
235
|
+
continue;
|
|
236
|
+
}
|
|
237
|
+
const path = resolve(outDir, `segment-${index + 1}.png`);
|
|
238
|
+
const frame = Math.floor((s.start_frame + s.end_frame - 1) / 2);
|
|
239
|
+
try {
|
|
240
|
+
// An empty ffmpeg selection can exit successfully without writing a file.
|
|
241
|
+
// Remove prior evidence so only this invocation can produce a valid card.
|
|
242
|
+
await rm(path, { force: true });
|
|
243
|
+
await command("ffmpeg", ["-y", "-v", "error", "-i", opts.referencePath, "-i", target, "-i", artifactPath, "-filter_complex",
|
|
244
|
+
`[0:v]select=eq(n\\,${frame}),scale=240:320:force_original_aspect_ratio=decrease,pad=240:320:(ow-iw)/2:(oh-ih)/2,setsar=1[a];[1:v]select=eq(n\\,0),scale=240:320:force_original_aspect_ratio=decrease,pad=240:320:(ow-iw)/2:(oh-ih)/2,setsar=1[b];[2:v]select=eq(n\\,${frame}),scale=240:320:force_original_aspect_ratio=decrease,pad=240:320:(ow-iw)/2:(oh-ih)/2,setsar=1[c];[a][b][c]hstack=inputs=3[out]`, "-map", "[out]", "-frames:v", "1", path]);
|
|
245
|
+
// Verify ffmpeg actually produced the image (empty selections can exit zero).
|
|
246
|
+
await still(path);
|
|
247
|
+
cards.push(path);
|
|
248
|
+
cardChecks.push(result("comparison_card", "pass", { path, frame, columns: ["原始参考", "目标素材", "生成帧"] }, "仅对照卡生成成功,服装需人工看", s.id));
|
|
249
|
+
}
|
|
250
|
+
catch (e) {
|
|
251
|
+
cardChecks.push({ ...error("comparison_card", e), segment: s.id });
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
const all = [...results, ...cardChecks], cov = coverage(job, all);
|
|
255
|
+
const counts = Object.fromEntries(["pass", "fail", "suspect", "not_applicable", "unsupported", "error"].map(s => [s, all.filter(c => c.status === s).length]));
|
|
256
|
+
const lines = ["# 结论摘要", `软件检查统计:${JSON.stringify(counts)}。pass 仅代表对应软件检查已执行;报告不阻止导出。`, "服装、身份、局部语义需人工看。所有启发式阈值待校准。", "", "## 问题(段落与证据)"];
|
|
257
|
+
for (const c of all.filter(c => c.status === "fail" || c.status === "suspect"))
|
|
258
|
+
lines.push(`- 段 ${c.segment ?? "全片"} / ${c.check}: ${c.status};${c.note ?? ""};证据 ${JSON.stringify(c.evidence)}`);
|
|
259
|
+
for (const level of ["minimum", "detail"]) {
|
|
260
|
+
lines.push("", `## ${level === "minimum" ? "最低要求" : "细节要求"}:需人工看与覆盖情况`);
|
|
261
|
+
for (const r of job.requirements.filter(r => r.level === level)) {
|
|
262
|
+
const c = cov.find(c => c.requirement === r.id);
|
|
263
|
+
lines.push(`- 段 ${r.segment} / ${r.id} / ${r.attribute}: ${c.state === "needs_human" ? "需人工看" : c.state === "unchecked" ? "未检查" : `已检查 ${c.status}`} `);
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
lines.push("", "## 未检查项");
|
|
267
|
+
for (const c of all.filter(c => ["not_applicable", "unsupported", "error"].includes(c.status)))
|
|
268
|
+
lines.push(`- 段 ${c.segment ?? "全片"} / ${c.check}: ${c.status};${c.note ?? ""};${JSON.stringify(c.evidence)}`);
|
|
269
|
+
lines.push("", "## 逐段对照卡", "列顺序:原始参考、目标素材、生成帧。代表帧不能覆盖整段语义。");
|
|
270
|
+
for (const card of cards)
|
|
271
|
+
lines.push(``);
|
|
272
|
+
const report_json = resolve(outDir, "report.json"), report_md = join(resolve(outDir), "report.md");
|
|
273
|
+
await writeFile(report_json, JSON.stringify({ job_id: job.job_id, artifactPath, conclusion: counts, results: all, coverage: cov, cards, blocks_export: false, pending_calibration: true }, null, 2));
|
|
274
|
+
await writeFile(report_md, lines.join("\n") + "\n");
|
|
275
|
+
return { report_json, report_md, cards };
|
|
276
|
+
}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { execFile, spawn } from "node:child_process";
|
|
2
|
+
import { promisify } from "node:util";
|
|
3
|
+
const exec = promisify(execFile);
|
|
4
|
+
export async function command(bin, args) {
|
|
5
|
+
return exec(bin, args, { maxBuffer: 16 * 1024 * 1024 });
|
|
6
|
+
}
|
|
7
|
+
export async function still(path, frame = 0, size = 64) {
|
|
8
|
+
const { stdout } = await exec("ffmpeg", ["-v", "error", "-i", path, "-vf",
|
|
9
|
+
`select=eq(n\\,${frame}),scale=${size}:${size}:flags=area`, "-frames:v", "1",
|
|
10
|
+
"-f", "rawvideo", "-pix_fmt", "rgb24", "pipe:1"], { encoding: "buffer", maxBuffer: size * size * 6 });
|
|
11
|
+
if (stdout.length !== size * size * 3)
|
|
12
|
+
throw new Error("requested frame unavailable");
|
|
13
|
+
return stdout;
|
|
14
|
+
}
|
|
15
|
+
/** Stream full-resolution RGB frames; memory is bounded by decoder chunks. */
|
|
16
|
+
export async function* frames(path, bytes) {
|
|
17
|
+
const child = spawn("ffmpeg", ["-v", "error", "-i", path, "-map", "0:v:0", "-fps_mode", "passthrough", "-f", "rawvideo", "-pix_fmt", "rgb24", "pipe:1"]);
|
|
18
|
+
let stderr = "";
|
|
19
|
+
child.stderr.on("data", (b) => { stderr = (stderr + b.toString()).slice(-4096); });
|
|
20
|
+
const done = new Promise((resolve, reject) => {
|
|
21
|
+
child.on("error", reject);
|
|
22
|
+
child.on("close", code => code === 0 ? resolve() : reject(new Error(`decode failed: ${stderr}`)));
|
|
23
|
+
});
|
|
24
|
+
void done.catch(() => undefined);
|
|
25
|
+
let pending = Buffer.alloc(0);
|
|
26
|
+
try {
|
|
27
|
+
for await (const chunk of child.stdout) {
|
|
28
|
+
pending = Buffer.concat([pending, chunk]);
|
|
29
|
+
while (pending.length >= bytes) {
|
|
30
|
+
yield pending.subarray(0, bytes);
|
|
31
|
+
pending = pending.subarray(bytes);
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
await done;
|
|
35
|
+
if (pending.length)
|
|
36
|
+
throw new Error("partial frame");
|
|
37
|
+
}
|
|
38
|
+
finally {
|
|
39
|
+
if (child.exitCode === null)
|
|
40
|
+
child.kill();
|
|
41
|
+
await done.catch(() => undefined);
|
|
42
|
+
}
|
|
43
|
+
}
|
|
@@ -0,0 +1,178 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import { fetchSocialPost } from '../acquire/social.js';
|
|
3
|
+
import { AcquireError } from '../acquire/share.js';
|
|
4
|
+
import { GatewayError, redact } from '../gateway/errors.js';
|
|
5
|
+
import { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js';
|
|
6
|
+
import { StdioServerTransport } from '@modelcontextprotocol/sdk/server/stdio.js';
|
|
7
|
+
import { z } from 'zod';
|
|
8
|
+
import { jobSchema, assetSchema } from '../core/index.js';
|
|
9
|
+
import { CloudGateway, scrub } from '../gateway/index.js';
|
|
10
|
+
import { PopTitleRenderer } from '../effects/index.js';
|
|
11
|
+
import { Engine, analyzeReference, draftJob, safeError } from '../engine/steps.js';
|
|
12
|
+
import { DirectGeneration } from '../engine/direct.js';
|
|
13
|
+
import { trimMedia } from '../acquire/trim.js';
|
|
14
|
+
const inputSchema = z.object({
|
|
15
|
+
job: jobSchema, reference: z.string().min(1),
|
|
16
|
+
output: z.object({ duration_seconds: z.number().int(), resolution: z.enum(['480P', '720P', '1080P']), ratio: z.string(), seed: z.number().int().optional() }),
|
|
17
|
+
slots: z.record(z.string(), z.string()).optional(), retryReserveCredits: z.number().int().nonnegative(), audio: z.string().optional(),
|
|
18
|
+
});
|
|
19
|
+
function response(data) {
|
|
20
|
+
const result = data;
|
|
21
|
+
const markdown = typeof result.markdown === 'string' ? result.markdown : `\`\`\`json\n${JSON.stringify(data, null, 2)}\n\`\`\``;
|
|
22
|
+
return { content: [{ type: 'text', text: markdown }], structuredContent: { ...data, markdown } };
|
|
23
|
+
}
|
|
24
|
+
function formatGenerationText(result) {
|
|
25
|
+
if (!result.charge_notice || result.markdown.includes(result.charge_notice))
|
|
26
|
+
return result.markdown;
|
|
27
|
+
return `${result.markdown}\n\n${result.charge_notice}:预算已保留,请在同一生成目录续查。`;
|
|
28
|
+
}
|
|
29
|
+
export async function failure(error, tokens) {
|
|
30
|
+
const secret = await tokens.token().catch(() => '');
|
|
31
|
+
return { isError: true, content: [{ type: 'text', text: redact(safeError(error, secret), secret) }] };
|
|
32
|
+
}
|
|
33
|
+
const SERVER_INSTRUCTIONS = `Zenmcp tools are atomic. Combine them as follows.
|
|
34
|
+
0. Direct image/video generation
|
|
35
|
+
- list_models → quote_generation → show the quote and obtain explicit credit approval → run_generation → get_generation. Use a new empty directory for each generation. Local uploads require user consent and consent_confirmed: true. Retrying a released attempt requires explicit approval and retryAfterRelease: true. Only use analyze_reference when replicating reference video shots or rhythm.
|
|
36
|
+
1. Input source
|
|
37
|
+
- Social share text or link (Douyin, Xiaohongshu, Bilibili, TikTok, Instagram, X): call fetch_social_post first. Never scrape pages yourself. Use a new empty directory per post and do not fetch the same link twice in one task; the service has a daily limit.
|
|
38
|
+
- Local video file from the user: skip fetching and go straight to step 2 with that path.
|
|
39
|
+
2. Video
|
|
40
|
+
- Call analyze_reference on the local video path right away; no user confirmation is needed for fetching or analysis.
|
|
41
|
+
- If the video is longer than about 30 seconds, or plan_job reports a duration limit, ask the user once which segment to keep, call trim_media, then analyze the trimmed file.
|
|
42
|
+
3. Images (kind = images)
|
|
43
|
+
- Show the downloaded images and ask which ones are product or model assets. Call import_assets with confirmed: true only after the user confirms.
|
|
44
|
+
4. Text
|
|
45
|
+
- title, text and hashtags from fetch_social_post are the post caption, not a transcript of speech in the video. Do not claim to know what is said in the video.
|
|
46
|
+
5. Paid steps
|
|
47
|
+
- plan_job only quotes; call execute_job only with credits the user approved. Report results with get_job. quote_generation only quotes; run_generation requires explicitly approved credits and requestDigest. Query with get_generation; never automatically approve retryAfterRelease.
|
|
48
|
+
6. Errors from fetch_social_post
|
|
49
|
+
- share_link_unresolved: ask the user to copy the full link.
|
|
50
|
+
- unsupported_platform: list the six supported platforms.
|
|
51
|
+
- rate_limited: tell the user the daily limit is reached and to try again tomorrow.
|
|
52
|
+
- post_unavailable: the post was deleted, is private, or is region-locked.`;
|
|
53
|
+
/** Background tasks live in this MCP process; state and attempts survive its restart. */
|
|
54
|
+
export function createServer(engine, socialOptions = {}) {
|
|
55
|
+
const server = new McpServer({ name: 'zenmcp', version: '0.1.0' }, { instructions: SERVER_INSTRUCTIONS });
|
|
56
|
+
const active = new Map();
|
|
57
|
+
server.registerTool('fetch_social_post', {
|
|
58
|
+
description: '解析抖音/小红书/B站/TikTok/Instagram/X 分享口令或链接,下载视频或图集到本地目录,返回本地路径与标题/正文/话题。免费、限次;不分析内容。视频可直接交给 analyze_reference。',
|
|
59
|
+
inputSchema: { text: z.string().min(1).max(4000), directory: z.string(), download: z.boolean().default(true) },
|
|
60
|
+
}, async (input) => {
|
|
61
|
+
try {
|
|
62
|
+
return response(await fetchSocialPost(engine.options.gateway, input, socialOptions));
|
|
63
|
+
}
|
|
64
|
+
catch (error) {
|
|
65
|
+
const code = error instanceof AcquireError || error instanceof GatewayError ? error.code ?? 'upstream_unavailable' : 'download_failed';
|
|
66
|
+
return { isError: true, content: [{ type: 'text', text: code === 'share_link_unresolved' ? 'share_link_unresolved:请复制完整链接' : redact(code) }], structuredContent: { code: redact(code) } };
|
|
67
|
+
}
|
|
68
|
+
});
|
|
69
|
+
server.registerTool('analyze_reference', { description: '本地分析参考视频;仅软件切点与媒体事实,语义需人工确认。', inputSchema: { path: z.string() } }, async ({ path }) => { const analysis = await analyzeReference(path); return response({ ...analysis, draft: draftJob(analysis) }); });
|
|
70
|
+
server.registerTool('import_assets', {
|
|
71
|
+
description: '导入用户已确认的最终图片、动作参考及属性,返回供 job.assets 使用的已批准素材和任务相对路径。confirmed:true 表示用户已确认这些素材和属性为最终版本;在用户完成确认前不得调用。不会读取 token 或调用网关。',
|
|
72
|
+
inputSchema: { directory: z.string(), assets: z.array(z.object({ id: assetSchema.shape.id, role: assetSchema.shape.role, source: z.string().min(1), attributes: z.string().optional() })).min(1), confirmed: z.literal(true) },
|
|
73
|
+
}, async ({ directory, assets }) => response({ assets: await engine.importAssets(directory, assets) }));
|
|
74
|
+
server.registerTool('plan_job', { description: '保存任务并给出分项积分报价及明示重试预留;不提交生成。', inputSchema: { directory: z.string(), input: inputSchema.optional() } }, async ({ directory, input }) => {
|
|
75
|
+
if (input)
|
|
76
|
+
await engine.create(directory, input);
|
|
77
|
+
const plan = await engine.plan(directory);
|
|
78
|
+
return response(plan);
|
|
79
|
+
});
|
|
80
|
+
server.registerTool('execute_job', { description: '携带用户批准的积分上限(及可选的已确认计划摘要 planDigest),后台执行;立即返回,用 get_job 查询。', inputSchema: { directory: z.string(), approvedCredits: z.number().int().nonnegative(), planDigest: z.string().min(1).optional() } }, async ({ directory, approvedCredits, planDigest }) => {
|
|
81
|
+
if (active.has(directory))
|
|
82
|
+
return response({ status: 'running', directory });
|
|
83
|
+
// Resolve locking and persisted status inside Engine; never swallow an unhandled rejection.
|
|
84
|
+
const work = engine.execute(directory, approvedCredits, planDigest).catch(() => undefined).finally(() => active.delete(directory));
|
|
85
|
+
active.set(directory, work);
|
|
86
|
+
return response({ status: 'accepted', directory, markdown: '已接受后台执行。请用 get_job 查看步骤状态、错误及报告路径。' });
|
|
87
|
+
});
|
|
88
|
+
server.registerTool('get_job', { description: '查询落盘步骤、预算占用、报告 Markdown 与文件路径。', inputSchema: { directory: z.string() } }, async ({ directory }) => {
|
|
89
|
+
const job = await engine.get(directory);
|
|
90
|
+
const background = active.has(directory);
|
|
91
|
+
const interrupted = job.state.status === 'running' && !background;
|
|
92
|
+
const statusLine = interrupted
|
|
93
|
+
? '任务状态:running(后台任务已中断,请使用 execute_job 在原目录续跑恢复)'
|
|
94
|
+
: `任务状态:${job.state.status}`;
|
|
95
|
+
return response({ ...job, background, interrupted, markdown: job.state.steps.report?.output?.data.markdown ?? `${statusLine}\n\n${JSON.stringify(job, null, 2)}` });
|
|
96
|
+
});
|
|
97
|
+
server.registerTool('revise_job', { description: '按段与属性归类:conform/effects/mix 确定性重跑,new_seed 重跑全片;其他修改需用户决定。准备修改后用 execute_job 执行。', inputSchema: { directory: z.string(), segment: z.string(), attribute: z.string(), action: z.string(), approvedCredits: z.number().int().nonnegative().optional() } }, async ({ directory, ...request }) => response(await engine.revise(directory, request)));
|
|
98
|
+
server.registerTool('trim_media', {
|
|
99
|
+
description: '本地截取视频片段(精确重编码,保留音轨),输出新文件供 analyze_reference 使用;不联网、不收费。',
|
|
100
|
+
inputSchema: {
|
|
101
|
+
path: z.string().min(1),
|
|
102
|
+
start_seconds: z.number().nonnegative(),
|
|
103
|
+
end_seconds: z.number().positive(),
|
|
104
|
+
output: z.string().min(1),
|
|
105
|
+
},
|
|
106
|
+
}, async ({ path, start_seconds, end_seconds, output }) => response(await trimMedia({ path, start_seconds, end_seconds, output })));
|
|
107
|
+
server.registerTool('account_status', {
|
|
108
|
+
description: '查询当前账户可用积分、日限额与总限额占用情况(只读,不扣费)。',
|
|
109
|
+
inputSchema: {},
|
|
110
|
+
}, async () => {
|
|
111
|
+
const secret = await engine.tokens.token().catch(() => '');
|
|
112
|
+
const me = scrub(await engine.options.gateway.me(), secret);
|
|
113
|
+
const remainingDaily = me.daily_credit_limit === null ? null : Math.max(0, me.daily_credit_limit - me.used_today);
|
|
114
|
+
const remainingTotal = me.total_credit_limit === null ? null : Math.max(0, me.total_credit_limit - me.used_total);
|
|
115
|
+
const effectiveAvailable = Math.min(me.credits_available, ...(remainingDaily === null ? [] : [remainingDaily]), ...(remainingTotal === null ? [] : [remainingTotal]));
|
|
116
|
+
const note = me.daily_credit_limit === null
|
|
117
|
+
? '当前平台 Key 未设置日积分限额,建议在网站账户页为 Key 配置日限额以防超支。'
|
|
118
|
+
: '当前平台 Key 已启用日积分限额保护。';
|
|
119
|
+
const markdown = [
|
|
120
|
+
'# 账户额度状态',
|
|
121
|
+
'',
|
|
122
|
+
`- 可用积分:${me.credits_available}`,
|
|
123
|
+
`- 今日已用 / 日限额:${me.used_today} / ${me.daily_credit_limit ?? '未设置'}`,
|
|
124
|
+
`- 累计已用 / 总限额:${me.used_total} / ${me.total_credit_limit ?? '未设置'}`,
|
|
125
|
+
`- 当前单次可支配上限:${effectiveAvailable}`,
|
|
126
|
+
`- 说明:${note}`,
|
|
127
|
+
].join('\n');
|
|
128
|
+
return response({
|
|
129
|
+
credits_available: me.credits_available,
|
|
130
|
+
daily_credit_limit: me.daily_credit_limit,
|
|
131
|
+
used_today: me.used_today,
|
|
132
|
+
total_credit_limit: me.total_credit_limit,
|
|
133
|
+
used_total: me.used_total,
|
|
134
|
+
effective_available_credits: effectiveAvailable,
|
|
135
|
+
credential_kind: me.credential_kind,
|
|
136
|
+
note,
|
|
137
|
+
markdown,
|
|
138
|
+
});
|
|
139
|
+
});
|
|
140
|
+
const direct = new DirectGeneration({ gateway: engine.options.gateway, tokens: engine.tokens, poll: engine.options.poll, download: engine.options.download });
|
|
141
|
+
const guarded = (fn) => async (input) => {
|
|
142
|
+
try {
|
|
143
|
+
return response(await fn(input));
|
|
144
|
+
}
|
|
145
|
+
catch (error) {
|
|
146
|
+
return failure(error, engine.tokens);
|
|
147
|
+
}
|
|
148
|
+
};
|
|
149
|
+
server.registerTool('list_models', { description: '免费列出当前可用图片与视频能力。', inputSchema: { medium: z.enum(['image', 'video']).optional() } }, guarded((i) => direct.listModels(i.medium)));
|
|
150
|
+
server.registerTool('quote_generation', { description: '校验并上传已获用户同意的本地素材,报价;不提交生成。', inputSchema: { directory: z.string(), capability: z.string(), prompt: z.string(), params: z.record(z.string(), z.union([z.string(), z.number(), z.boolean()])).optional(), inputs: z.record(z.string(), z.string()).optional(), consent_confirmed: z.boolean().optional() } }, guarded((i) => direct.quote(i)));
|
|
151
|
+
server.registerTool('run_generation', { description: '携带明确批准积分与请求摘要,后台执行或恢复同一付款尝试。释放后重试需用户重新批准。', inputSchema: { directory: z.string(), approvedCredits: z.number().int().nonnegative(), requestDigest: z.string(), retryAfterRelease: z.boolean().optional() } }, guarded((i) => direct.run(i)));
|
|
152
|
+
server.registerTool('get_generation', { description: '只读查询直通生成状态、账单摘要与本地输出。', inputSchema: { directory: z.string() } }, guarded(async (i) => { const result = await direct.get(i); return { ...result, markdown: formatGenerationText(result) }; }));
|
|
153
|
+
return server;
|
|
154
|
+
}
|
|
155
|
+
export function createEngine(baseUrl, tokens) {
|
|
156
|
+
const capturedToken = process.env.ZENMCP_TOKEN;
|
|
157
|
+
delete process.env.ZENMCP_TOKEN;
|
|
158
|
+
const provider = tokens ?? { token: async () => {
|
|
159
|
+
const value = capturedToken ?? process.env.ZENMCP_TOKEN;
|
|
160
|
+
if (!value)
|
|
161
|
+
throw new Error('ZENMCP_TOKEN 未配置');
|
|
162
|
+
return value;
|
|
163
|
+
} };
|
|
164
|
+
return new Engine({ effects: job => new PopTitleRenderer(job), gateway: new CloudGateway({ baseUrl, tokens: provider }), tokens: provider, catalogLabel: '云端 catalog(仅运行工具时读取;Wan 可用性以返回目录为准)' });
|
|
165
|
+
}
|
|
166
|
+
export async function startServer(baseUrl, tokens) {
|
|
167
|
+
const server = createServer(createEngine(baseUrl, tokens));
|
|
168
|
+
await server.connect(new StdioServerTransport());
|
|
169
|
+
return server;
|
|
170
|
+
}
|
|
171
|
+
// Importing this module in an embedding host never starts network or stdio work.
|
|
172
|
+
const entry = process.argv[1] ? await (await import('node:fs/promises')).realpath(process.argv[1]).catch(() => process.argv[1]) : '';
|
|
173
|
+
if (entry && import.meta.url === (await import('node:url')).pathToFileURL(entry).href) {
|
|
174
|
+
const baseUrl = process.env.ZENMCP_BASE_URL;
|
|
175
|
+
if (!baseUrl)
|
|
176
|
+
throw new Error('ZENMCP_BASE_URL 未配置');
|
|
177
|
+
await startServer(baseUrl);
|
|
178
|
+
}
|