@kevlns/art-workshop 0.5.1 → 0.6.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/AGENTS.md +25 -25
- package/LICENSE +21 -21
- package/README.md +65 -63
- package/art-workshop.mjs +219 -174
- package/docs/channels.md +49 -0
- package/docs/commands.md +2259 -22
- package/docs/configuration.md +90 -82
- package/docs/core-guidance.md +58 -50
- package/docs/data-formats.md +89 -85
- package/docs/deployment.md +32 -32
- package/docs/local-models/qwen-image-2.1.json +73 -73
- package/docs/local-models/qwen-image-2.1.md +33 -33
- package/docs/model-architecture.md +78 -78
- package/docs/modes.md +73 -67
- package/docs/release-0.6.0.md +9 -0
- package/lib/agent.mjs +32 -32
- package/lib/analysis-worker.mjs +36 -32
- package/lib/analyze.mjs +85 -85
- package/lib/asset-profile.mjs +23 -23
- package/lib/backends.mjs +2 -0
- package/lib/candidates.mjs +104 -103
- package/lib/channel-operation.mjs +84 -0
- package/lib/comfy-recovery.mjs +17 -17
- package/lib/conversation.mjs +77 -77
- package/lib/core.mjs +104 -75
- package/lib/deployment-guide.mjs +12 -12
- package/lib/design.mjs +29 -29
- package/lib/dimensions.mjs +22 -22
- package/lib/edit.mjs +43 -41
- package/lib/environment-operations.mjs +49 -49
- package/lib/environment-runtime.mjs +99 -99
- package/lib/environments.mjs +55 -55
- package/lib/executor.mjs +107 -107
- package/lib/generate.mjs +60 -58
- package/lib/generation-policy.mjs +1 -1
- package/lib/heartbeat.mjs +16 -16
- package/lib/images.mjs +1 -1
- package/lib/lifecycle.mjs +53 -53
- package/lib/migration.mjs +154 -154
- package/lib/object-format.mjs +1 -1
- package/lib/process-identity.mjs +22 -14
- package/lib/project.mjs +39 -39
- package/lib/prompt-structure.mjs +3 -3
- package/lib/refs.mjs +26 -26
- package/lib/runtime.mjs +25 -25
- package/lib/service.mjs +155 -126
- package/lib/settings.mjs +50 -50
- package/lib/shared.mjs +67 -67
- package/lib/style-bundle.mjs +98 -85
- package/lib/style-contract.mjs +74 -74
- package/lib/style-lifecycle.mjs +124 -125
- package/lib/style-review.mjs +14 -14
- package/lib/style-samples.mjs +92 -0
- package/lib/task-contract.mjs +94 -93
- package/lib/tasks.mjs +497 -497
- package/lib/templates.mjs +80 -80
- package/lib/video/PROVENANCE.json +63 -0
- package/lib/video/atomic.mjs +32 -0
- package/lib/video/channel.mjs +70 -0
- package/lib/video/contract-local-media.mjs +42 -0
- package/lib/video/contract-story.mjs +213 -0
- package/lib/video/contract-video.mjs +117 -0
- package/lib/video/contracts.mjs +3 -0
- package/lib/video/h3.mjs +232 -0
- package/lib/video/local-runtime.mjs +20 -0
- package/lib/video/media.mjs +223 -0
- package/lib/video/models.mjs +336 -0
- package/lib/video/path.mjs +18 -0
- package/lib/video/process.mjs +15 -0
- package/lib/video/resource.mjs +15 -0
- package/lib/video/service.mjs +731 -0
- package/lib/video/workflow.mjs +571 -0
- package/lib/video.mjs +12 -0
- package/lib/vision.mjs +56 -56
- package/lib/worker-process.mjs +6 -6
- package/lib/workspace.mjs +305 -292
- package/package.json +8 -3
- package/rules/core-guidance.json +22 -22
- package/skills/art-workshop/SKILL.md +19 -17
- package/skills/art-workshop/references/conversation.md +11 -11
- package/skills/art-workshop/references/expression.md +27 -27
- package/skills/art-workshop/references/project.md +119 -109
- package/skills/art-workshop/references/style-discussion.md +7 -3
- package/skills/art-workshop/references/video.md +13 -0
- package/v-cli.plugin.json +7240 -2747
|
@@ -0,0 +1,731 @@
|
|
|
1
|
+
import {assertLocalEndpoint} from './local-runtime.mjs';
|
|
2
|
+
import { atomicJson, atomicWrite, replaceFile } from "./atomic.mjs";
|
|
3
|
+
import sharp from "sharp";
|
|
4
|
+
import { createWriteStream, fstatSync } from "node:fs";
|
|
5
|
+
import { Readable, Transform } from "node:stream";
|
|
6
|
+
import { pipeline } from "node:stream/promises";
|
|
7
|
+
import { mkdir, readFile, stat, rm, lstat } from "node:fs/promises";
|
|
8
|
+
import { join } from "node:path";
|
|
9
|
+
import { randomUUID, createHash } from "node:crypto";
|
|
10
|
+
import { z } from "zod";
|
|
11
|
+
import {
|
|
12
|
+
defaultVideoConfig,
|
|
13
|
+
validateVideo,
|
|
14
|
+
videoBusy,
|
|
15
|
+
videoComplete,
|
|
16
|
+
videoDependencyErrors
|
|
17
|
+
} from "./contracts.mjs";
|
|
18
|
+
import { projectPath } from "./path.mjs";
|
|
19
|
+
import { withResource } from "./resource.mjs";
|
|
20
|
+
import { compileVideo } from "./models.mjs";
|
|
21
|
+
import {
|
|
22
|
+
VideoWorkflow,
|
|
23
|
+
workflowActions,
|
|
24
|
+
scriptSchema
|
|
25
|
+
} from "./workflow.mjs";
|
|
26
|
+
const videoActions = [
|
|
27
|
+
"describe",
|
|
28
|
+
...workflowActions,
|
|
29
|
+
"material.list",
|
|
30
|
+
"material.import",
|
|
31
|
+
"material.import-file",
|
|
32
|
+
"material.hide",
|
|
33
|
+
"image",
|
|
34
|
+
"task.list",
|
|
35
|
+
"task.read",
|
|
36
|
+
"task.create",
|
|
37
|
+
"task.rename",
|
|
38
|
+
"task.archive",
|
|
39
|
+
"operation.create",
|
|
40
|
+
"operation.update",
|
|
41
|
+
"operation.rename",
|
|
42
|
+
"operation.derive",
|
|
43
|
+
"operation.validate",
|
|
44
|
+
"operation.run",
|
|
45
|
+
"operation.resume",
|
|
46
|
+
"operation.cancel"
|
|
47
|
+
];
|
|
48
|
+
const id = z.string().regex(/^[a-zA-Z0-9_-]{1,100}$/);
|
|
49
|
+
const name = z.string().trim().min(1).max(160);
|
|
50
|
+
const inputSchema = z.object({
|
|
51
|
+
assetId: id,
|
|
52
|
+
kind: z.enum(["first", "last", "middle", "reference"]),
|
|
53
|
+
frame: z.number().int().nonnegative().optional(),
|
|
54
|
+
role: z.enum(["subject", "style", "motion", "camera"]).optional()
|
|
55
|
+
}).strict();
|
|
56
|
+
const configSchema = z.object({
|
|
57
|
+
modelId: z.string(),
|
|
58
|
+
mode: z.enum(["text", "boundary", "reference"]),
|
|
59
|
+
prompt: z.string(),
|
|
60
|
+
parameters: z.record(
|
|
61
|
+
z.string(),
|
|
62
|
+
z.union([z.string(), z.number().finite(), z.boolean()])
|
|
63
|
+
),
|
|
64
|
+
inputs: z.array(inputSchema).max(128)
|
|
65
|
+
}).strict();
|
|
66
|
+
function conflict() {
|
|
67
|
+
return Object.assign(new Error("操作已被其他入口修改,请重新读取后再保存"), {
|
|
68
|
+
status: 409
|
|
69
|
+
});
|
|
70
|
+
}
|
|
71
|
+
class VideoService {
|
|
72
|
+
constructor(models, sources) {
|
|
73
|
+
this.models = models;
|
|
74
|
+
this.sources = sources;
|
|
75
|
+
this.workflow = new VideoWorkflow({
|
|
76
|
+
models,
|
|
77
|
+
signal: this.shutdown.signal,
|
|
78
|
+
root: (w) => this.root(w),
|
|
79
|
+
get: (w, id2) => this.get(w, id2),
|
|
80
|
+
transaction: (w, action) => this.transaction(w, action),
|
|
81
|
+
putAsset: (w, label, bytes, source, origin) => this.putAsset(w, label, bytes, source, origin),
|
|
82
|
+
media: (w, task, op, run) => this.media(w, task, op, run)
|
|
83
|
+
});
|
|
84
|
+
}
|
|
85
|
+
models;
|
|
86
|
+
sources;
|
|
87
|
+
workflow;
|
|
88
|
+
watches = /* @__PURE__ */ new Map();
|
|
89
|
+
closing = false;
|
|
90
|
+
shutdown = new AbortController();
|
|
91
|
+
polls = /* @__PURE__ */ new Map();
|
|
92
|
+
operations = /* @__PURE__ */ new Set();
|
|
93
|
+
async root(w) {
|
|
94
|
+
return projectPath(w.root, ".vant/video");
|
|
95
|
+
}
|
|
96
|
+
async read(w) {
|
|
97
|
+
const file=await projectPath(w.root,join(await this.root(w),"project.json"));
|
|
98
|
+
try{const info=await stat(file);if(!info.isFile()||info.size>64*1024*1024)throw new Error("视频状态超过容量或不是文件");const state=JSON.parse(await readFile(file,"utf8"));
|
|
99
|
+
if(state.version!==1||!Array.isArray(state.tasks)||!Array.isArray(state.assets)||!Array.isArray(state.hiddenSources))throw new Error("视频状态格式无效");
|
|
100
|
+
const seen=new Set(),runIds=new Set(),assetIds=new Set();for(const task of state.tasks){id.parse(task.id);if(seen.has(task.id)||!Array.isArray(task.operations)||!Number.isInteger(task.revision)||task.revision<1)throw new Error("视频任务索引无效");seen.add(task.id);const operations=new Set();for(const op of task.operations){id.parse(op.id);if(operations.has(op.id)||!Number.isInteger(op.revision)||op.revision<1||!Array.isArray(op.runs))throw new Error("视频操作索引无效");operations.add(op.id);configSchema.parse(op.config);for(const run of op.runs){id.parse(run.id);if(runIds.has(run.id))throw new Error("视频运行身份重复");runIds.add(run.id);id.parse(run.requestId);assertLocalEndpoint(run.endpoint);if(!["dispatching","running","unknown","succeeded","failed"].includes(run.status)||run.output&&!new RegExp("^"+run.id+"\\.(mp4|webm)$").test(run.output))throw new Error("视频运行索引无效");}if(op.accepted?.output&&!/^accepted-[a-f0-9-]{36}\.mp4$/.test(op.accepted.output))throw new Error("采纳视频路径无效");}for(const film of task.films||[])if(film.output!==`film-${id.parse(film.id)}.mp4`)throw new Error("成片路径无效");}
|
|
101
|
+
for(const asset of state.assets){id.parse(asset.id);if(assetIds.has(asset.id))throw new Error("视频素材身份重复");assetIds.add(asset.id);if(!["image/png","image/jpeg","image/webp"].includes(asset.mime))throw new Error("视频素材格式无效");}
|
|
102
|
+
return state;
|
|
103
|
+
}catch(error){if(error.code==="ENOENT")return {version:1,tasks:[],assets:[],hiddenSources:[]};throw error;}
|
|
104
|
+
}
|
|
105
|
+
async snapshot(w){return this.read(w);}
|
|
106
|
+
async transaction(w, action) {
|
|
107
|
+
return withResource(w.root, ".vant/video", async () => {
|
|
108
|
+
await mkdir(await this.root(w), { recursive: true });
|
|
109
|
+
const state = await this.read(w);
|
|
110
|
+
const before = JSON.stringify(state);
|
|
111
|
+
const result = await action(state);
|
|
112
|
+
if (JSON.stringify(state) !== before)
|
|
113
|
+
await atomicJson(join(await this.root(w), "project.json"), state);
|
|
114
|
+
return result;
|
|
115
|
+
});
|
|
116
|
+
}
|
|
117
|
+
task(state, taskId) {
|
|
118
|
+
const t = state.tasks.find((t2) => t2.id === id.parse(taskId));
|
|
119
|
+
if (!t) throw new Error("视频任务不存在");
|
|
120
|
+
return t;
|
|
121
|
+
}
|
|
122
|
+
operation(task, opId) {
|
|
123
|
+
const op = task.operations.find((o) => o.id === id.parse(opId));
|
|
124
|
+
if (!op) throw new Error("操作不存在");
|
|
125
|
+
return op;
|
|
126
|
+
}
|
|
127
|
+
editable(task, op, revision) {
|
|
128
|
+
if (task.archived) throw new Error("任务已归档");
|
|
129
|
+
if (op.revision !== revision) throw conflict();
|
|
130
|
+
if (videoBusy(op) || videoComplete(op))
|
|
131
|
+
throw new Error("当前操作配置只读,请派生新操作");
|
|
132
|
+
}
|
|
133
|
+
touch(task, op) {
|
|
134
|
+
task.revision++;
|
|
135
|
+
task.updatedAt = Date.now();
|
|
136
|
+
if (op) {
|
|
137
|
+
op.revision++;
|
|
138
|
+
op.updatedAt = task.updatedAt;
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
async list(w) {
|
|
142
|
+
const state = await this.read(w);
|
|
143
|
+
return state.tasks.map(
|
|
144
|
+
({ operations, script, plan, films, refinements, ...task }) => ({
|
|
145
|
+
...task,
|
|
146
|
+
operationCount: operations.length
|
|
147
|
+
})
|
|
148
|
+
);
|
|
149
|
+
}
|
|
150
|
+
async get(w, taskId) {
|
|
151
|
+
return this.task(await this.read(w), taskId);
|
|
152
|
+
}
|
|
153
|
+
async execute(w, action, input = {}) {
|
|
154
|
+
if (this.closing) throw new Error("视频服务正在关闭");
|
|
155
|
+
const work = this.executeInternal(w, action, input);
|
|
156
|
+
this.operations.add(work);
|
|
157
|
+
try {
|
|
158
|
+
return await work;
|
|
159
|
+
} finally {
|
|
160
|
+
this.operations.delete(work);
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
async executeInternal(w, action, input) {
|
|
164
|
+
if (workflowActions.includes(action))
|
|
165
|
+
return this.workflow.execute(w, action, input);
|
|
166
|
+
if (action === "material.list") return this.materials(w);
|
|
167
|
+
if (action === "material.import")
|
|
168
|
+
return this.importSource(w, z.string().parse(input.sourceId));
|
|
169
|
+
if (action === "material.hide")
|
|
170
|
+
return this.hide(w, z.string().parse(input.assetId));
|
|
171
|
+
if (action === "material.import-file") {
|
|
172
|
+
const path = await projectPath(w.root, z.string().parse(input.path)), info = await stat(path);
|
|
173
|
+
if (!info.isFile() || info.size > 20 * 1024 * 1024)
|
|
174
|
+
throw new Error("图片限20MB");
|
|
175
|
+
return this.putAsset(w, name.parse(input.name), await readFile(path));
|
|
176
|
+
}
|
|
177
|
+
if (action === "describe")
|
|
178
|
+
return {
|
|
179
|
+
schemaVersion: 2,
|
|
180
|
+
actions: videoActions,
|
|
181
|
+
models: await this.models.catalog(),
|
|
182
|
+
configSchema: z.toJSONSchema(configSchema),
|
|
183
|
+
scriptSchema: z.toJSONSchema(scriptSchema),
|
|
184
|
+
workflow: {
|
|
185
|
+
"script.update": "input{taskId,revision:task.revision,script:complete scriptSchema}; saves text and references only",
|
|
186
|
+
"script.plan": "input{taskId,script?:scriptSchema}; read-only model-aware grouping, constraints/errors and actual durations",
|
|
187
|
+
"script.apply": "input{taskId,revision:task.revision}; creates/reuses operations from saved script; never generates",
|
|
188
|
+
"operation.accept": "input{taskId,operationId,revision:operation.revision,runId}; accepts succeeded run, captures its planned tail, resolves dependent first frames",
|
|
189
|
+
"operation.bind": "input{taskId,operationId,revision:operation.revision}; rebind editable operation to its dependency's accepted tail",
|
|
190
|
+
"film.compose": "input{taskId,revision:task.revision,requestId}; renders accepted plan segments with cut/continue/dissolve transitions, preserves previous film",
|
|
191
|
+
"film.refine": "input{taskId,revision:task.revision,requestId,filmId,start,end,modelId,prompt,keepAudio?:true,parameters?,sections?:[{duration,prompt}]}; captures boundary frames and creates fixed-boundary operations; sections sum to selected duration and fit model",
|
|
192
|
+
"film.preview": "input{taskId,revision:task.revision,requestId,refinementId}; renders candidate from all accepted refinement operations; preserves current film and original audio by default",
|
|
193
|
+
"film.adopt": "input{taskId,revision:task.revision,filmId}; adopts candidate only when its source is the current film",
|
|
194
|
+
"film.undo": "input{taskId,revision:task.revision}; restores current film's parent",
|
|
195
|
+
"media.status": "input{}; checks FFmpeg/ffprobe, required for acceptance, frame extraction and film editing",
|
|
196
|
+
planning: "shots are semantic action units; group at model limits, split overlong shots in script instead of truncating prose; global inputs: reference/first/last; shot middle frame positions are relative to that shot"
|
|
197
|
+
},
|
|
198
|
+
inputContract: {
|
|
199
|
+
taskId: "task.read/create returned id",
|
|
200
|
+
operationId: "operation id within task",
|
|
201
|
+
revision: "current operation.revision (task actions use task.revision)",
|
|
202
|
+
config: "complete configuration matching configSchema",
|
|
203
|
+
requestId: "stable unique id per authorized generation, reuse on uncertain response"
|
|
204
|
+
},
|
|
205
|
+
constraints: {
|
|
206
|
+
revisionRequired: true,
|
|
207
|
+
runRequestIdRequired: true,
|
|
208
|
+
completedReadOnly: true,
|
|
209
|
+
resumeNeverSubmits: true
|
|
210
|
+
}
|
|
211
|
+
};
|
|
212
|
+
if (action === "task.list") return this.list(w);
|
|
213
|
+
if (action === "task.read") return this.get(w, input.taskId);
|
|
214
|
+
if (action === "operation.run") return this.start(w, input);
|
|
215
|
+
if (action === "operation.resume") {
|
|
216
|
+
if(input.runId!==undefined){const task=await this.get(w,input.taskId),op=this.operation(task,input.operationId);if(op.runs.at(-1)?.id!==input.runId)throw new Error("恢复请求不属于当前原运行身份");}
|
|
217
|
+
await this.poll(w, id.parse(input.taskId), id.parse(input.operationId));
|
|
218
|
+
const task = await this.get(w, input.taskId);
|
|
219
|
+
if (videoBusy(this.operation(task, input.operationId)))
|
|
220
|
+
this.watch(w, task.id, input.operationId);
|
|
221
|
+
return task;
|
|
222
|
+
}
|
|
223
|
+
if (action === "operation.cancel") return this.cancel(w,input);
|
|
224
|
+
if (action === "operation.validate") {
|
|
225
|
+
const task = await this.get(w, input.taskId), op = this.operation(task, input.operationId);
|
|
226
|
+
const m = (await this.models.catalog()).find(
|
|
227
|
+
(m2) => m2.id === op.config.modelId
|
|
228
|
+
);
|
|
229
|
+
return {
|
|
230
|
+
errors: m ? [
|
|
231
|
+
...validateVideo(op.config, m),
|
|
232
|
+
...videoDependencyErrors(task, op.id),
|
|
233
|
+
...m.available ? [] : [m.reason || "模型不可用"]
|
|
234
|
+
] : ["选择可用生成模型"]
|
|
235
|
+
};
|
|
236
|
+
}
|
|
237
|
+
return this.transaction(w, async (state) => {
|
|
238
|
+
if (action === "task.create") {
|
|
239
|
+
const now = Date.now(), task2 = {
|
|
240
|
+
id: randomUUID(),
|
|
241
|
+
name: name.parse(input.name),
|
|
242
|
+
revision: 1,
|
|
243
|
+
archived: false,
|
|
244
|
+
operations: [],
|
|
245
|
+
createdAt: now,
|
|
246
|
+
updatedAt: now
|
|
247
|
+
};
|
|
248
|
+
state.tasks.push(task2);
|
|
249
|
+
return task2;
|
|
250
|
+
}
|
|
251
|
+
const task = this.task(state, input.taskId);
|
|
252
|
+
if (action === "task.rename" || action === "task.archive") {
|
|
253
|
+
if (task.revision !== input.revision) throw conflict();
|
|
254
|
+
if (task.operations.some(videoBusy))
|
|
255
|
+
throw new Error("运行尚未结束,不能修改任务状态");
|
|
256
|
+
if (action === "task.rename") task.name = name.parse(input.name);
|
|
257
|
+
else task.archived = z.boolean().parse(input.archived);
|
|
258
|
+
this.touch(task);
|
|
259
|
+
return task;
|
|
260
|
+
}
|
|
261
|
+
if (task.archived) throw new Error("任务已归档");
|
|
262
|
+
if (action === "operation.create" || action === "operation.derive") {
|
|
263
|
+
let config, derivedFrom;
|
|
264
|
+
if (action === "operation.derive") {
|
|
265
|
+
const source = this.operation(task, input.operationId);
|
|
266
|
+
if (source.revision !== input.revision) throw conflict();
|
|
267
|
+
if (videoBusy(source)) throw new Error("运行中操作不能派生");
|
|
268
|
+
config = structuredClone(source.config);
|
|
269
|
+
derivedFrom = source.id;
|
|
270
|
+
} else {
|
|
271
|
+
const model = (await this.models.catalog()).find(
|
|
272
|
+
(m) => m.id === input.modelId && m.available
|
|
273
|
+
);
|
|
274
|
+
config = defaultVideoConfig(model);
|
|
275
|
+
}
|
|
276
|
+
const now = Date.now();
|
|
277
|
+
task.operations.push({
|
|
278
|
+
id: randomUUID(),
|
|
279
|
+
name: name.parse(input.name ?? "新操作"),
|
|
280
|
+
revision: 1,
|
|
281
|
+
config,
|
|
282
|
+
runs: [],
|
|
283
|
+
createdAt: now,
|
|
284
|
+
updatedAt: now,
|
|
285
|
+
...derivedFrom ? { derivedFrom } : {},
|
|
286
|
+
...derivedFrom ? (() => {
|
|
287
|
+
const source = this.operation(task, derivedFrom);
|
|
288
|
+
return {
|
|
289
|
+
scriptSegmentId: source.scriptSegmentId,
|
|
290
|
+
refinementId: source.refinementId,
|
|
291
|
+
dependency: structuredClone(source.dependency),
|
|
292
|
+
editDuration: source.editDuration
|
|
293
|
+
};
|
|
294
|
+
})() : {}
|
|
295
|
+
});
|
|
296
|
+
this.touch(task);
|
|
297
|
+
return task;
|
|
298
|
+
}
|
|
299
|
+
const op = this.operation(task, input.operationId);
|
|
300
|
+
if (op.revision !== input.revision) throw conflict();
|
|
301
|
+
if (action === "operation.rename") {
|
|
302
|
+
op.name = name.parse(input.name);
|
|
303
|
+
this.touch(task, op);
|
|
304
|
+
return task;
|
|
305
|
+
}
|
|
306
|
+
if (action === "operation.update") {
|
|
307
|
+
this.editable(task, op, input.revision);
|
|
308
|
+
const config = configSchema.parse(input.config);
|
|
309
|
+
for (const i of config.inputs)
|
|
310
|
+
if (!state.assets.some((a) => a.id === i.assetId))
|
|
311
|
+
throw new Error("引用素材不存在");
|
|
312
|
+
if (op.config.modelId !== config.modelId)
|
|
313
|
+
op.profiles = {
|
|
314
|
+
...op.profiles,
|
|
315
|
+
[op.config.modelId]: structuredClone(op.config)
|
|
316
|
+
};
|
|
317
|
+
op.config = config;
|
|
318
|
+
this.touch(task, op);
|
|
319
|
+
return task;
|
|
320
|
+
}
|
|
321
|
+
throw new Error("未知视频操作");
|
|
322
|
+
});
|
|
323
|
+
}
|
|
324
|
+
async assets(w) {
|
|
325
|
+
return (await this.read(w)).assets.filter((a) => !a.hidden);
|
|
326
|
+
}
|
|
327
|
+
async materials(w) {
|
|
328
|
+
const state = await this.read(w), external = await this.sources?.list(w) ?? [];
|
|
329
|
+
return {
|
|
330
|
+
assets: state.assets.filter((a) => !a.hidden),
|
|
331
|
+
sources: external.filter((s) => !state.hiddenSources.includes(s.id))
|
|
332
|
+
};
|
|
333
|
+
}
|
|
334
|
+
async hide(w, assetId) {
|
|
335
|
+
return this.transaction(w, (state) => {
|
|
336
|
+
const a = state.assets.find((a2) => a2.id === assetId);
|
|
337
|
+
if (a) a.hidden = true;
|
|
338
|
+
else if (!state.hiddenSources.includes(assetId))
|
|
339
|
+
state.hiddenSources.push(assetId);
|
|
340
|
+
return { ok: true };
|
|
341
|
+
});
|
|
342
|
+
}
|
|
343
|
+
async importSource(w, sourceId) {
|
|
344
|
+
const source = (await this.sources?.list(w))?.find(
|
|
345
|
+
(s) => s.id === sourceId
|
|
346
|
+
);
|
|
347
|
+
if (!source || !this.sources) throw new Error("来源图片不存在");
|
|
348
|
+
const data = await this.sources.read(w, source);
|
|
349
|
+
return this.putAsset(
|
|
350
|
+
w,
|
|
351
|
+
source.name,
|
|
352
|
+
data.bytes,
|
|
353
|
+
source.source,
|
|
354
|
+
source.origin
|
|
355
|
+
);
|
|
356
|
+
}
|
|
357
|
+
async putAsset(w, label, bytes, source = "import", origin) {
|
|
358
|
+
if (!bytes.length || bytes.length > 20 * 1024 * 1024)
|
|
359
|
+
throw new Error("图片限 20MB");
|
|
360
|
+
const mime = bytes.subarray(0, 8).equals(Buffer.from([137, 80, 78, 71, 13, 10, 26, 10])) ? "image/png" : bytes[0] === 255 && bytes[1] === 216 ? "image/jpeg" : bytes.toString("ascii", 0, 4) === "RIFF" && bytes.toString("ascii", 8, 12) === "WEBP" ? "image/webp" : "";
|
|
361
|
+
if (!mime) throw new Error("仅支持 PNG、JPEG、WebP 图片");
|
|
362
|
+
const decoder = sharp(bytes, {
|
|
363
|
+
limitInputPixels: 32 * 1024 * 1024,
|
|
364
|
+
failOn: "error"
|
|
365
|
+
});
|
|
366
|
+
const metadata=await decoder.metadata();await decoder.stats();
|
|
367
|
+
if (!metadata.width || !metadata.height) throw new Error("图片无法解码");
|
|
368
|
+
const asset = {
|
|
369
|
+
id: source === "capture" ? randomUUID() : source === 'conversation' ? createHash('sha256').update(`conversation\0${origin?.sessionId}\0${origin?.attachmentId}\0`).update(bytes).digest('hex') : createHash("sha256").update(bytes).digest("hex"),
|
|
370
|
+
name: name.parse(label),
|
|
371
|
+
mime,
|
|
372
|
+
source,
|
|
373
|
+
origin,
|
|
374
|
+
hash: createHash("sha256").update(bytes).digest("hex"),
|
|
375
|
+
createdAt: Date.now()
|
|
376
|
+
};
|
|
377
|
+
return this.transaction(w, async (state) => {
|
|
378
|
+
const existing = state.assets.find((a) => a.id === asset.id);
|
|
379
|
+
if (existing) {
|
|
380
|
+
existing.hidden = false;
|
|
381
|
+
return existing;
|
|
382
|
+
}
|
|
383
|
+
const path = await projectPath(
|
|
384
|
+
w.root,
|
|
385
|
+
join(await this.root(w), `${asset.id}.image`)
|
|
386
|
+
);
|
|
387
|
+
await atomicWrite(path, bytes);
|
|
388
|
+
state.assets.push(asset);
|
|
389
|
+
return asset;
|
|
390
|
+
});
|
|
391
|
+
}
|
|
392
|
+
async asset(w,assetId){const asset=(await this.read(w)).assets.find(a=>a.id===id.parse(assetId));if(!asset)throw new Error("素材不存在");const file=await projectPath(w.root,join(await this.root(w),`${asset.id}.image`)),info=await stat(file);if(!info.isFile()||info.size>20*1024*1024)throw new Error("视频图片素材无效或超过20MB");const bytes=await readFile(file),expected=asset.hash||(/^[a-f0-9]{64}$/.test(asset.id)?asset.id:null);if(expected&&createHash('sha256').update(bytes).digest('hex')!==expected)throw new Error("视频素材内容已变化");return {bytes,type:asset.mime};}
|
|
393
|
+
async media(w, taskId, opId, runId) {
|
|
394
|
+
const task = this.task(await this.read(w), taskId), op = this.operation(task, opId), run = runId ? op.runs.find((r) => r.id === id.parse(runId)) : op.runs.at(-1);
|
|
395
|
+
if (run?.status !== "succeeded" || !run.output)
|
|
396
|
+
throw new Error("视频尚未就绪");
|
|
397
|
+
const path = await projectPath(
|
|
398
|
+
w.root,
|
|
399
|
+
join(await this.root(w), run.output)
|
|
400
|
+
);
|
|
401
|
+
await stat(path);
|
|
402
|
+
return {
|
|
403
|
+
path,
|
|
404
|
+
name: op.name.replace(/[<>:"/\\|?*\x00-\x1f]/g, "_") + (run.output.endsWith(".webm") ? ".webm" : ".mp4")
|
|
405
|
+
};
|
|
406
|
+
}
|
|
407
|
+
async start(w, input) {
|
|
408
|
+
if (this.closing) throw new Error("服务正在关闭");
|
|
409
|
+
const requestId = id.parse(input.requestId), settings = await this.models.get(), catalog = await this.models.catalog(settings);
|
|
410
|
+
let duplicate = false;
|
|
411
|
+
const run = await this.transaction(w, async (state) => {
|
|
412
|
+
const task = this.task(state, input.taskId), op = this.operation(task, input.operationId);
|
|
413
|
+
const prior = op.runs.find((r) => r.requestId === requestId);
|
|
414
|
+
if (prior) {
|
|
415
|
+
duplicate = true;
|
|
416
|
+
return prior;
|
|
417
|
+
}
|
|
418
|
+
this.editable(task, op, input.revision);
|
|
419
|
+
const model = catalog.find((m) => m.id === op.config.modelId);
|
|
420
|
+
if (!model?.available) throw new Error(model?.reason || "生成模型不可用");
|
|
421
|
+
const errors = validateVideo(op.config, model);
|
|
422
|
+
errors.push(...videoDependencyErrors(task, op.id));
|
|
423
|
+
if (errors.length) throw new Error(errors.join(";"));
|
|
424
|
+
for (const i of op.config.inputs)
|
|
425
|
+
if (!state.assets.some((a) => a.id === i.assetId))
|
|
426
|
+
throw new Error("素材不存在");
|
|
427
|
+
const run2 = {
|
|
428
|
+
id: randomUUID(),
|
|
429
|
+
requestId,
|
|
430
|
+
status: "dispatching",
|
|
431
|
+
createdAt: Date.now(),
|
|
432
|
+
updatedAt: Date.now(),
|
|
433
|
+
config: structuredClone(op.config),
|
|
434
|
+
model: structuredClone(model),
|
|
435
|
+
endpoint: settings.localEndpoint
|
|
436
|
+
};
|
|
437
|
+
op.runs.push(run2);
|
|
438
|
+
this.touch(task, op);
|
|
439
|
+
return run2;
|
|
440
|
+
});
|
|
441
|
+
if (duplicate) return this.get(w, input.taskId);
|
|
442
|
+
let submitted = false;
|
|
443
|
+
try {
|
|
444
|
+
await this.models.ensure(
|
|
445
|
+
settings,
|
|
446
|
+
run.model.id,
|
|
447
|
+
this.shutdown.signal,
|
|
448
|
+
run.config.mode
|
|
449
|
+
);
|
|
450
|
+
const images = /* @__PURE__ */ new Map();
|
|
451
|
+
for (const i of run.config.inputs) {
|
|
452
|
+
if (images.has(i.assetId)) continue;
|
|
453
|
+
const data = await this.asset(w, i.assetId), form = new FormData();
|
|
454
|
+
form.append(
|
|
455
|
+
"image",
|
|
456
|
+
new Blob([new Uint8Array(data.bytes)], { type: data.type }),
|
|
457
|
+
`${i.assetId}.${data.type.split("/")[1]}`
|
|
458
|
+
);
|
|
459
|
+
const response2 = await this.models.fetcher(
|
|
460
|
+
new URL("upload/image", run.endpoint),
|
|
461
|
+
{
|
|
462
|
+
method: "POST",
|
|
463
|
+
body: form,
|
|
464
|
+
signal: AbortSignal.any([
|
|
465
|
+
this.shutdown.signal,
|
|
466
|
+
AbortSignal.timeout(3e4)
|
|
467
|
+
])
|
|
468
|
+
}
|
|
469
|
+
);
|
|
470
|
+
if (!response2.ok)
|
|
471
|
+
throw new Error(`素材上传失败 HTTP ${response2.status}`);
|
|
472
|
+
const uploaded = z.object({ name: z.string().min(1), subfolder: z.string().optional() }).parse(await response2.json());
|
|
473
|
+
images.set(
|
|
474
|
+
i.assetId,
|
|
475
|
+
[uploaded.subfolder, uploaded.name].filter(Boolean).join("/")
|
|
476
|
+
);
|
|
477
|
+
}
|
|
478
|
+
const prompt = compileVideo(run.config, run.model, settings, images);
|
|
479
|
+
submitted = true;
|
|
480
|
+
const response = await this.models.fetcher(
|
|
481
|
+
new URL("prompt", run.endpoint),
|
|
482
|
+
{
|
|
483
|
+
method: "POST",
|
|
484
|
+
headers: { "Content-Type": "application/json" },
|
|
485
|
+
body: JSON.stringify({
|
|
486
|
+
prompt,
|
|
487
|
+
client_id: run.id,
|
|
488
|
+
extra_data: { vantRunId: run.id }
|
|
489
|
+
}),
|
|
490
|
+
signal: AbortSignal.any([
|
|
491
|
+
this.shutdown.signal,
|
|
492
|
+
AbortSignal.timeout(3e4)
|
|
493
|
+
])
|
|
494
|
+
}
|
|
495
|
+
);
|
|
496
|
+
if (response.status === 400) {
|
|
497
|
+
submitted = false;
|
|
498
|
+
throw new Error(
|
|
499
|
+
`ComfyUI 拒绝工作流:${(await response.text()).slice(0, 1e3)}`
|
|
500
|
+
);
|
|
501
|
+
}
|
|
502
|
+
if (!response.ok) throw new Error(`提交响应异常 HTTP ${response.status}`);
|
|
503
|
+
const receipt = await response.json();
|
|
504
|
+
if (typeof receipt.prompt_id !== "string")
|
|
505
|
+
throw new Error("未收到上游运行身份");
|
|
506
|
+
await this.updateRun(w, input.taskId, input.operationId, run.id, (r) => {
|
|
507
|
+
r.upstreamId = receipt.prompt_id;
|
|
508
|
+
r.status = "running";
|
|
509
|
+
});
|
|
510
|
+
} catch (e) {
|
|
511
|
+
await this.updateRun(w, input.taskId, input.operationId, run.id, (r) => {
|
|
512
|
+
r.status = submitted ? "unknown" : "failed";
|
|
513
|
+
r.error = e.message;
|
|
514
|
+
});
|
|
515
|
+
}
|
|
516
|
+
this.watch(w, input.taskId, input.operationId);
|
|
517
|
+
return this.get(w, input.taskId);
|
|
518
|
+
}
|
|
519
|
+
updateRun(w, taskId, opId, runId, change) {
|
|
520
|
+
return this.transaction(w, (state) => {
|
|
521
|
+
const task = this.task(state, taskId), op = this.operation(task, opId), run = op.runs.find((candidate) => candidate.id === runId);
|
|
522
|
+
if (!run) throw new Error("视频运行记录不存在");
|
|
523
|
+
const before = JSON.stringify(run);
|
|
524
|
+
change(run);
|
|
525
|
+
if (JSON.stringify(run) === before) return;
|
|
526
|
+
run.updatedAt = Date.now();
|
|
527
|
+
this.touch(task, op);
|
|
528
|
+
});
|
|
529
|
+
}
|
|
530
|
+
watch(w, taskId, opId) {
|
|
531
|
+
const key = `${w.id}:${taskId}:${opId}`;
|
|
532
|
+
if (this.closing || this.watches.has(key)) return;
|
|
533
|
+
this.watches.set(
|
|
534
|
+
key,
|
|
535
|
+
setTimeout(async () => {
|
|
536
|
+
try {
|
|
537
|
+
await this.poll(w, taskId, opId);
|
|
538
|
+
} catch {
|
|
539
|
+
} finally {
|
|
540
|
+
this.watches.delete(key);
|
|
541
|
+
if (!this.closing) {
|
|
542
|
+
try {
|
|
543
|
+
const task = this.task(await this.read(w), taskId);
|
|
544
|
+
if (videoBusy(this.operation(task, opId)))
|
|
545
|
+
this.watch(w, taskId, opId);
|
|
546
|
+
} catch {
|
|
547
|
+
}
|
|
548
|
+
}
|
|
549
|
+
}
|
|
550
|
+
}, 5e3)
|
|
551
|
+
);
|
|
552
|
+
this.watches.get(key).unref();
|
|
553
|
+
}
|
|
554
|
+
async poll(w, taskId, opId) {
|
|
555
|
+
if (this.closing) return;
|
|
556
|
+
const key = `${w.id}:${taskId}:${opId}`, current = this.polls.get(key);
|
|
557
|
+
if (current) return current;
|
|
558
|
+
const work = this.pollOnce(w, taskId, opId);
|
|
559
|
+
this.polls.set(key, work);
|
|
560
|
+
try {
|
|
561
|
+
await work;
|
|
562
|
+
} finally {
|
|
563
|
+
if (this.polls.get(key) === work) this.polls.delete(key);
|
|
564
|
+
}
|
|
565
|
+
}
|
|
566
|
+
async pollOnce(w, taskId, opId) {
|
|
567
|
+
const op = this.operation(this.task(await this.read(w), taskId), opId), run = op.runs.at(-1);
|
|
568
|
+
if (!run || !videoBusy(op)) return;
|
|
569
|
+
if (!run.upstreamId) {
|
|
570
|
+
if (run.status === "dispatching" && Date.now() - run.createdAt < 12e4)
|
|
571
|
+
return;
|
|
572
|
+
const matches = /* @__PURE__ */ new Set();
|
|
573
|
+
for (const route of ["queue", "history?max_items=200"]) {
|
|
574
|
+
const result = await this.models.fetcher(new URL(route, run.endpoint), {
|
|
575
|
+
signal: AbortSignal.any([
|
|
576
|
+
this.shutdown.signal,
|
|
577
|
+
AbortSignal.timeout(15e3)
|
|
578
|
+
])
|
|
579
|
+
});
|
|
580
|
+
if (!result.ok) throw new Error("无法核对原运行");
|
|
581
|
+
const data = await result.json();
|
|
582
|
+
if (route === "queue") {
|
|
583
|
+
for (const row of [
|
|
584
|
+
...data.queue_running ?? [],
|
|
585
|
+
...data.queue_pending ?? []
|
|
586
|
+
])
|
|
587
|
+
if (row[3]?.vantRunId === run.id && typeof row[1] === "string")
|
|
588
|
+
matches.add(row[1]);
|
|
589
|
+
} else
|
|
590
|
+
for (const [key, entry2] of Object.entries(data))
|
|
591
|
+
if (entry2.prompt?.[3]?.vantRunId === run.id) matches.add(key);
|
|
592
|
+
}
|
|
593
|
+
if (matches.size === 1) {
|
|
594
|
+
await this.updateRun(w, taskId, opId, run.id, (r) => {
|
|
595
|
+
r.upstreamId = [...matches][0];
|
|
596
|
+
r.status = "running";
|
|
597
|
+
delete r.error;
|
|
598
|
+
});
|
|
599
|
+
} else
|
|
600
|
+
await this.updateRun(w, taskId, opId, run.id, (r) => {
|
|
601
|
+
r.status = "unknown";
|
|
602
|
+
r.error = matches.size > 1 ? "发现多个原运行身份,需要人工核对" : "未找到唯一上游身份;不会自动重新生成";
|
|
603
|
+
});
|
|
604
|
+
return;
|
|
605
|
+
}
|
|
606
|
+
const response = await this.models.fetcher(
|
|
607
|
+
new URL(`history/${encodeURIComponent(run.upstreamId)}`, run.endpoint),
|
|
608
|
+
{
|
|
609
|
+
signal: AbortSignal.any([
|
|
610
|
+
this.shutdown.signal,
|
|
611
|
+
AbortSignal.timeout(15e3)
|
|
612
|
+
])
|
|
613
|
+
}
|
|
614
|
+
);
|
|
615
|
+
if (!response.ok) throw new Error("无法查询生成状态");
|
|
616
|
+
const history = await response.json(), entry = history[run.upstreamId];
|
|
617
|
+
if (!entry) return;
|
|
618
|
+
if (entry.status?.status_str === "error") {
|
|
619
|
+
await this.updateRun(w, taskId, opId, run.id, (r) => {
|
|
620
|
+
r.status = "failed";
|
|
621
|
+
r.error = "ComfyUI 执行失败,请查看该运行的上游日志";
|
|
622
|
+
});
|
|
623
|
+
return;
|
|
624
|
+
}
|
|
625
|
+
if (!entry.status?.completed) return;
|
|
626
|
+
const files = Object.values(entry.outputs ?? {}).flatMap(
|
|
627
|
+
(o) => [...o.videos ?? [], ...o.gifs ?? [], ...o.images ?? []]
|
|
628
|
+
);
|
|
629
|
+
const file = files.find((f) => /\.(mp4|webm)$/i.test(f.filename));
|
|
630
|
+
if (!file) {
|
|
631
|
+
await this.updateRun(w, taskId, opId, run.id, (r) => {
|
|
632
|
+
r.status = "failed";
|
|
633
|
+
r.error = "工作流完成但没有视频输出";
|
|
634
|
+
});
|
|
635
|
+
return;
|
|
636
|
+
}
|
|
637
|
+
const url = new URL("view", run.endpoint);
|
|
638
|
+
for (const k of ["filename", "subfolder", "type"])
|
|
639
|
+
if (file[k]) url.searchParams.set(k, file[k]);
|
|
640
|
+
const media = await this.models.fetcher(url, {
|
|
641
|
+
signal: AbortSignal.any([
|
|
642
|
+
this.shutdown.signal,
|
|
643
|
+
AbortSignal.timeout(12e4)
|
|
644
|
+
])
|
|
645
|
+
});
|
|
646
|
+
if (!media.ok || !media.body) throw new Error("视频取回失败");
|
|
647
|
+
const filename = `${run.id}${/\.webm$/i.test(file.filename) ? ".webm" : ".mp4"}`;
|
|
648
|
+
const output = await projectPath(
|
|
649
|
+
w.root,
|
|
650
|
+
join(await this.root(w), filename)
|
|
651
|
+
), temporary = await projectPath(w.root,`${output}.${randomUUID()}.part`);
|
|
652
|
+
let temporaryOwner;
|
|
653
|
+
let size = 0, header = Buffer.alloc(0);
|
|
654
|
+
const limiter = new Transform({
|
|
655
|
+
transform(chunk, _encoding, callback) {
|
|
656
|
+
size += chunk.length;
|
|
657
|
+
if (size > 512 * 1024 * 1024)
|
|
658
|
+
return callback(new Error("视频超过512MB接入上限"));
|
|
659
|
+
if (header.length < 12)
|
|
660
|
+
header = Buffer.concat([
|
|
661
|
+
header,
|
|
662
|
+
chunk.subarray(0, 12 - header.length)
|
|
663
|
+
]);
|
|
664
|
+
callback(null, chunk);
|
|
665
|
+
}
|
|
666
|
+
});
|
|
667
|
+
try {
|
|
668
|
+
const writer=createWriteStream(await projectPath(w.root,temporary),{flags:'wx',mode:0o600});
|
|
669
|
+
writer.once('open',fd=>{try{temporaryOwner=fstatSync(fd);}catch(error){writer.destroy(error);}});
|
|
670
|
+
await pipeline(
|
|
671
|
+
Readable.fromWeb(media.body),
|
|
672
|
+
limiter,
|
|
673
|
+
writer,
|
|
674
|
+
{ signal: this.shutdown.signal }
|
|
675
|
+
);
|
|
676
|
+
if (size < 12 || !(header.toString("ascii", 4, 8) === "ftyp" || header.subarray(0, 4).equals(Buffer.from([26, 69, 223, 163]))))
|
|
677
|
+
throw new Error("上游产物不是有效视频容器");
|
|
678
|
+
const checkedTemporary=await projectPath(w.root,temporary),current=await lstat(checkedTemporary);
|
|
679
|
+
if(!temporaryOwner||!current.isFile()||current.dev!==temporaryOwner.dev||current.ino!==temporaryOwner.ino)throw new Error('视频临时产物身份已变化');
|
|
680
|
+
await replaceFile(checkedTemporary,await projectPath(w.root,output));
|
|
681
|
+
} catch(error) {
|
|
682
|
+
await this.updateRun(w,taskId,opId,run.id,value=>{value.status="unknown";value.error="原任务已返回,但视频产物取回/校验未完成;只查询原任务,不重新提交。";});
|
|
683
|
+
throw error;
|
|
684
|
+
} finally {
|
|
685
|
+
if(temporaryOwner){let cleanup;try{const candidate=await projectPath(w.root,temporary),current=await lstat(candidate);if(current.isFile()&¤t.dev===temporaryOwner.dev&¤t.ino===temporaryOwner.ino)cleanup=candidate;}catch{}if(cleanup)await rm(cleanup,{force:true,maxRetries:5,retryDelay:25});}
|
|
686
|
+
}
|
|
687
|
+
await this.updateRun(w, taskId, opId, run.id, (r) => {
|
|
688
|
+
r.output = filename;
|
|
689
|
+
r.status = "succeeded";
|
|
690
|
+
delete r.error;
|
|
691
|
+
});
|
|
692
|
+
}
|
|
693
|
+
async restore(workspaces) {
|
|
694
|
+
for (const w of workspaces) {
|
|
695
|
+
try {
|
|
696
|
+
const state = await this.read(w);
|
|
697
|
+
for (const t of state.tasks)
|
|
698
|
+
for (const op of t.operations)
|
|
699
|
+
if (videoBusy(op)) this.watch(w, t.id, op.id);
|
|
700
|
+
} catch (error) {
|
|
701
|
+
console.warn("视频任务恢复失败", w.id, error.message);
|
|
702
|
+
}
|
|
703
|
+
}
|
|
704
|
+
}
|
|
705
|
+
async cancel(w,input){
|
|
706
|
+
const task=await this.get(w,input.taskId),op=this.operation(task,input.operationId),run=op.runs.at(-1);
|
|
707
|
+
if(!run||input.runId!==run.id)throw new Error("取消必须绑定原运行身份");
|
|
708
|
+
if(run.cancelled&&run.stopConfirmed)return task;
|
|
709
|
+
if(op.revision!==input.revision)throw conflict();
|
|
710
|
+
if(!run.upstreamId)throw new Error("缺少上游身份;只能核对原运行,不能盲目取消");
|
|
711
|
+
assertLocalEndpoint(run.endpoint);
|
|
712
|
+
const readQueue=async()=>{const response=await this.models.fetcher(new URL('queue',run.endpoint),{signal:AbortSignal.any([this.shutdown.signal,AbortSignal.timeout(15000)])});if(!response.ok)throw new Error("无法核对上游队列");return response.json()};
|
|
713
|
+
const queue=await readQueue();
|
|
714
|
+
if((queue.queue_running||[]).some(row=>row[1]===run.upstreamId))throw Object.assign(new Error("此 ComfyUI 运行中的任务没有可核验的独立取消接口;不会中断全局共享任务"),{code:'video-running-cancel-unavailable'});
|
|
715
|
+
if(!(queue.queue_pending||[]).some(row=>row[1]===run.upstreamId)){await this.poll(w,task.id,op.id);return this.get(w,task.id);}
|
|
716
|
+
try { const response=await this.models.fetcher(new URL('queue',run.endpoint),{method:'POST',headers:{'content-type':'application/json'},body:JSON.stringify({delete:[run.upstreamId]}),signal:AbortSignal.any([this.shutdown.signal,AbortSignal.timeout(15000)])});if(!response.ok)throw Object.assign(new Error("上游未确认移除原排队任务"),{unknown:response.status!==400});const after=await readQueue();if([...(after.queue_running||[]),...(after.queue_pending||[])].some(row=>row[1]===run.upstreamId))throw Object.assign(new Error("原任务仍在上游队列中,停止未确认"),{unknown:true});const history=await this.models.fetcher(new URL('history/'+encodeURIComponent(run.upstreamId),run.endpoint),{signal:AbortSignal.any([this.shutdown.signal,AbortSignal.timeout(15000)])});if(!history.ok)throw Object.assign(new Error('无法核对取消后的原任务回执'),{unknown:true});const entry=(await history.json())[run.upstreamId];if(entry?.status?.completed||entry?.status?.status_str==='error'){await this.poll(w,task.id,op.id);return this.get(w,task.id);}
|
|
717
|
+
await this.updateRun(w,task.id,op.id,run.id,value=>{value.status='failed';value.cancelled=true;value.stopConfirmed=true;value.error='已从上游队列移除原任务';});return await this.get(w,task.id); } catch(error) { throw Object.assign(error,{unknown:error.unknown!==false}); }
|
|
718
|
+
}
|
|
719
|
+
async close() {
|
|
720
|
+
this.closing = true;
|
|
721
|
+
for (const timer of this.watches.values()) clearTimeout(timer);
|
|
722
|
+
this.watches.clear();
|
|
723
|
+
this.shutdown.abort();
|
|
724
|
+
await Promise.allSettled([...this.polls.values(), ...this.operations]);
|
|
725
|
+
}
|
|
726
|
+
}
|
|
727
|
+
export {
|
|
728
|
+
VideoService,
|
|
729
|
+
configSchema,
|
|
730
|
+
videoActions
|
|
731
|
+
};
|