@kevlns/art-workshop 0.2.1 → 0.3.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.
Files changed (58) hide show
  1. package/AGENTS.md +24 -51
  2. package/README.md +31 -93
  3. package/art-workshop.mjs +69 -25
  4. package/docs/commands.md +913 -80
  5. package/docs/configuration.md +33 -139
  6. package/docs/core-guidance.md +7 -7
  7. package/docs/data-formats.md +14 -152
  8. package/docs/deployment.md +40 -0
  9. package/docs/model-architecture.md +52 -0
  10. package/docs/modes.md +15 -116
  11. package/docs/remote-models.md +42 -0
  12. package/docs/troubleshooting.md +30 -68
  13. package/lib/adapters/dashscope.mjs +234 -0
  14. package/lib/adapters/index.mjs +28 -0
  15. package/lib/agent.mjs +2 -2
  16. package/lib/analysis-worker.mjs +25 -0
  17. package/lib/analyze.mjs +8 -8
  18. package/lib/backends.mjs +350 -74
  19. package/lib/bl.mjs +3 -18
  20. package/lib/candidates.mjs +82 -0
  21. package/lib/comfyui.mjs +10 -8
  22. package/lib/contracts.d.ts +67 -0
  23. package/lib/deployment-worker.mjs +97 -0
  24. package/lib/deployment.mjs +72 -0
  25. package/lib/design.mjs +5 -5
  26. package/lib/dimensions.mjs +22 -0
  27. package/lib/edit.mjs +6 -3
  28. package/lib/environments.mjs +67 -0
  29. package/lib/execution/remote.mjs +156 -0
  30. package/lib/executor.mjs +10 -24
  31. package/lib/generate.mjs +9 -5
  32. package/lib/generation-policy.mjs +198 -0
  33. package/lib/images.mjs +10 -0
  34. package/lib/lifecycle.mjs +40 -0
  35. package/lib/migration.mjs +154 -0
  36. package/lib/model-catalog.mjs +288 -0
  37. package/lib/model-registry.mjs +234 -0
  38. package/lib/plan.mjs +98 -28
  39. package/lib/project.mjs +13 -4
  40. package/lib/providers.mjs +27 -22
  41. package/lib/refs.mjs +12 -16
  42. package/lib/runtime.mjs +6 -4
  43. package/lib/service.mjs +45 -4
  44. package/lib/settings.mjs +2 -4
  45. package/lib/style-lifecycle.mjs +72 -0
  46. package/lib/task-references.mjs +35 -0
  47. package/lib/task-worker.mjs +225 -50
  48. package/lib/tasks.mjs +440 -63
  49. package/lib/templates.mjs +69 -0
  50. package/lib/transport/cli.mjs +69 -0
  51. package/lib/transport/http.mjs +82 -0
  52. package/lib/vision.mjs +46 -0
  53. package/lib/workflows.mjs +10 -1
  54. package/lib/workspace.mjs +190 -0
  55. package/package.json +9 -3
  56. package/skills/art-workshop/SKILL.md +27 -54
  57. package/v-cli.plugin.json +1257 -143
  58. package/docs//351/227/256/351/242/230/346/212/245/345/221/212-2026-10-08.md +0 -29
package/lib/tasks.mjs CHANGED
@@ -1,81 +1,458 @@
1
- import {spawn} from 'node:child_process';
2
- import {mkdirSync,openSync,closeSync,existsSync,rmSync} from 'node:fs';
3
- import {join} from 'node:path';
4
- import {fileURLToPath} from 'node:url';
5
- import {randomUUID} from 'node:crypto';
6
- import {writeJson,hash} from './shared.mjs';
7
- import {runtimeRoot,taskFile,tasks,stateRead,terminal,sleep,locked,alive,http} from './runtime.mjs';
8
- import {closeService,healthy} from './service.mjs';
9
- import {cancelPrompt} from './comfyui.mjs';
10
- import {validatePlan} from './plan.mjs';
1
+ import { spawn } from "node:child_process";
2
+ import { mkdirSync, openSync, closeSync, existsSync, rmSync } from "node:fs";
3
+ import { join } from "node:path";
4
+ import { fileURLToPath } from "node:url";
5
+ import { randomUUID } from "node:crypto";
6
+ import { writeJson, hash } from "./shared.mjs";
7
+ import {
8
+ runtimeRoot,
9
+ taskFile,
10
+ tasks,
11
+ stateRead,
12
+ terminal,
13
+ sleep,
14
+ locked,
15
+ alive,
16
+ http,
17
+ } from "./runtime.mjs";
18
+ import { closeService, healthy } from "./service.mjs";
19
+ import { cancelPrompt } from "./comfyui.mjs";
20
+ import { validatePlan } from "./plan.mjs";
21
+ import { executionSettled, executionLifecycle } from "./lifecycle.mjs";
22
+ import { bindBackend } from "./backends.mjs";
23
+ import { assertPlanCurrent } from "./workspace.mjs";
24
+ import {
25
+ validateRemotePlan,
26
+ assertRemoteContract,
27
+ } from "./generation-policy.mjs";
11
28
 
12
29
  export function taskView(task) {
13
- const {schema,taskId,status,createdAt,heartbeat,finishedAt,workerPid,endpoint,promptId,manifestPath,error,diagnostic,remoteCancellation,unsettled,currentItem,logPath}=task;
14
- return {schema,taskId,status,createdAt,heartbeat,finishedAt,workerPid,endpoint,promptId,manifestPath,error,diagnostic,remoteCancellation,unsettled,currentItem,logPath,backend:task.plan?.backend?.id??'bailian'};
30
+ const {
31
+ schema,
32
+ taskId,
33
+ status,
34
+ createdAt,
35
+ heartbeat,
36
+ finishedAt,
37
+ workerPid,
38
+ endpoint,
39
+ promptId,
40
+ manifestPath,
41
+ error,
42
+ diagnostic,
43
+ remoteCancellation,
44
+ unsettled,
45
+ currentItem,
46
+ logPath,
47
+ remoteExecutions,
48
+ } = task;
49
+ return {
50
+ canResume:
51
+ terminal(task.status) &&
52
+ task.status !== "succeeded" &&
53
+ !alive(task.workerPid) &&
54
+ Object.values(task.remoteExecutions ?? {}).some((receipt) =>
55
+ ["accepted", "completed"].includes(receipt.state),
56
+ ) &&
57
+ Object.values(task.remoteExecutions ?? {}).every(
58
+ (receipt) => receipt.state !== "dispatching",
59
+ ),
60
+ operationId: task.operationId ?? null,
61
+ project: task.plan?.project ?? task.project,
62
+ job: task.plan?.job ?? (task.objectKind === "task" ? task.objectId : null),
63
+ styleId: task.styleId ?? null,
64
+ lifecycle: executionLifecycle(task),
65
+ stopConfirmed: executionSettled(task),
66
+ schema,
67
+ taskId,
68
+ status,
69
+ createdAt,
70
+ heartbeat,
71
+ finishedAt,
72
+ workerPid,
73
+ endpoint,
74
+ promptId,
75
+ manifestPath,
76
+ error,
77
+ diagnostic,
78
+ remoteCancellation,
79
+ remoteExecutions,
80
+ unsettled,
81
+ currentItem,
82
+ logPath,
83
+ backend:
84
+ task.plan?.backend?.id ?? task.analysisProvider?.providerId ?? "bailian",
85
+ configuration: task.plan?.snapshots?.job ?? null,
86
+ };
15
87
  }
16
88
 
17
- export function taskStatus(root,taskId) {
18
- const file=taskFile(root,taskId),task=stateRead(file);if(!task)throw new Error('任务不存在 '+taskId);
19
- if(!terminal(task.status)&&Date.now()-Date.parse(task.heartbeat??task.createdAt)>10000&&!alive(task.workerPid)) {
20
- task.status='lost';task.error='工作进程退出;后端任务可能仍在运行,请 task cancel 或 shutdown 清理';writeJson(file,task);
89
+ export function taskStatus(root, taskId) {
90
+ const file = taskFile(root, taskId),
91
+ task = stateRead(file);
92
+ if (!task) throw new Error("任务不存在 " + taskId);
93
+ if (
94
+ !terminal(task.status) &&
95
+ Date.now() - Date.parse(task.heartbeat ?? task.createdAt) > 10000 &&
96
+ !alive(task.workerPid)
97
+ ) {
98
+ task.status = "unknown";
99
+ task.stopConfirmed = false;
100
+ task.unsettled = true;
101
+ task.error =
102
+ "工作进程退出;后端任务可能仍在运行,请 task cancel 或 shutdown 清理";
103
+ writeJson(file, task);
21
104
  }
22
105
  return task;
23
106
  }
24
- export async function startTask(plan,items,settings) {
25
- validatePlan(plan);if(!items.length)throw new Error('没有选中任务');
26
- for(const item of items)if(!plan.items.some(x=>hash(x)===hash(item)))throw new Error('执行项不属于计划');
27
- const root=runtimeRoot(settings);
28
- return locked(root,'control',async()=>{
29
- const gate=stateRead(join(root,'shutdown.json'));
30
- if(gate&&alive(gate.pid))throw new Error('shutdown 正在执行,不能启动任务');
31
- if(gate)rmSync(join(root,'shutdown.json'),{force:true});
32
- const taskId='task-'+randomUUID(),file=taskFile(root,taskId);
33
- const logPath=join(root,'tasks',taskId+'.log');
34
- const task={schema:'art-workshop/task',taskId,status:'queued',createdAt:new Date().toISOString(),plan,items,outputRoot:settings.paths.output,root,config:settings.file,logPath};
35
- writeJson(file,task);
36
- const fd=openSync(logPath,'a');
37
- const child=spawn(process.execPath,[fileURLToPath(new URL('./task-worker.mjs',import.meta.url)),file],{detached:true,windowsHide:true,stdio:['ignore',fd,fd]});closeSync(fd);
38
- await new Promise((resolve,reject)=>{child.once('spawn',resolve);child.once('error',reject);});
107
+ export function failedItems(root, runId, plan) {
108
+ const original = taskStatus(root, runId);
109
+ if (!executionSettled(original))
110
+ throw new Error("原批次停止尚未确认,不能重试");
111
+ if (original.plan?.planHash !== plan.planHash)
112
+ throw new Error("重试必须使用原批次预案,配置已变化请创建新批次");
113
+ if (!original.manifestPath)
114
+ throw new Error("原批次没有逐项结果,不能猜测失败项");
115
+ const manifest = stateRead(original.manifestPath);
116
+ if (manifest?.taskId !== runId || manifest.planHash !== plan.planHash)
117
+ throw new Error("原批次结果身份不匹配");
118
+ const recovered = new Set();
119
+ for (const retry of tasks(root).filter((task) => task.retryOf === runId)) {
120
+ const state = taskStatus(root, retry.taskId);
121
+ if (!executionSettled(state)) throw new Error("已有重试批次停止尚未确认");
122
+ if (state.plan?.planHash !== plan.planHash || !state.manifestPath)
123
+ throw new Error("已有重试批次证据不完整");
124
+ const result = stateRead(state.manifestPath);
125
+ if (result?.taskId !== state.taskId || result.planHash !== plan.planHash)
126
+ throw new Error("重试批次结果身份不匹配");
127
+ for (const row of result.items)
128
+ if (row.ok && row.status === "generated") recovered.add(row.inputHash);
129
+ }
130
+ const failed = manifest.items.filter(
131
+ (row) =>
132
+ row.status === "failed" && !row.ok && !recovered.has(row.inputHash),
133
+ );
134
+ if (failed.some((row) => row.remote?.state === "completed"))
135
+ throw new Error("上游已完成,请恢复原运行取回产物,不能重新提交生成");
136
+ const items = failed.map((row) => {
137
+ const item = plan.items.find((item) => item.inputHash === row.inputHash);
138
+ if (!item) throw new Error("失败项不属于原预案");
139
+ return item;
140
+ });
141
+ if (
142
+ !items.length ||
143
+ new Set(items.map((item) => item.inputHash)).size !== items.length
144
+ )
145
+ throw new Error("没有唯一可重试的失败项");
146
+ return items;
147
+ }
148
+ export async function startTask(
149
+ plan,
150
+ items,
151
+ settings,
152
+ { operationId = null, retryRun = null } = {},
153
+ ) {
154
+ validatePlan(plan);
155
+ if (!items.length) throw new Error("没有选中任务");
156
+ if (plan.backend?.provider !== "comfyui") {
157
+ assertRemoteContract(plan);
158
+ validateRemotePlan({ ...plan, items });
159
+ }
160
+ for (const item of items)
161
+ if (!plan.items.some((x) => hash(x) === hash(item)))
162
+ throw new Error("执行项不属于计划");
163
+ const root = runtimeRoot(settings);
164
+ return locked(root, "control", async () => {
165
+ if (retryRun) items = failedItems(root, retryRun, plan);
166
+ if (operationId) {
167
+ if (!/^[a-z0-9-]{1,128}$/.test(operationId))
168
+ throw new Error("操作身份非法");
169
+ const existing = tasks(root).filter((t) => t.operationId === operationId);
170
+ if (existing.length) {
171
+ if (
172
+ existing.length !== 1 ||
173
+ existing[0].plan?.planHash !== plan.planHash ||
174
+ hash(existing[0].items) !== hash(items)
175
+ )
176
+ throw new Error("操作身份已用于其他请求");
177
+ return taskView(taskStatus(root, existing[0].taskId));
178
+ }
179
+ }
180
+ assertPlanCurrent(plan, settings);
181
+ if (
182
+ plan.backend &&
183
+ bindBackend(plan, settings, plan.backend.id).planHash !== plan.planHash
184
+ )
185
+ throw new Error("后端能力或工作流已变化,请重新编译");
186
+ const blocked = tasks(root).filter(
187
+ (t) =>
188
+ (t.plan?.project ?? t.project) === plan.project &&
189
+ (t.plan?.job === plan.job ||
190
+ (t.objectKind === "task" && t.objectId === plan.job)) &&
191
+ !executionSettled(taskStatus(root, t.taskId)),
192
+ );
193
+ if (blocked.length) throw new Error("同一任务已有运行中或停止未确认的执行");
194
+ const gate = stateRead(join(root, "shutdown.json"));
195
+ if (gate && alive(gate.pid))
196
+ throw new Error("shutdown 正在执行,不能启动任务");
197
+ if (gate) rmSync(join(root, "shutdown.json"), { force: true });
198
+ const taskId = "task-" + randomUUID(),
199
+ file = taskFile(root, taskId);
200
+ const logPath = join(root, "tasks", taskId + ".log");
201
+ const task = {
202
+ schema: "art-workshop/task",
203
+ operationId,
204
+ retryOf: retryRun,
205
+ taskId,
206
+ status: "queued",
207
+ stopConfirmed: false,
208
+ createdAt: new Date().toISOString(),
209
+ plan,
210
+ items,
211
+ outputRoot: settings.paths.output,
212
+ root,
213
+ config: settings.file,
214
+ logPath,
215
+ };
216
+ writeJson(file, task);
217
+ const fd = openSync(logPath, "a");
218
+ const child = spawn(
219
+ process.execPath,
220
+ [fileURLToPath(new URL("./task-worker.mjs", import.meta.url)), file],
221
+ { detached: true, windowsHide: true, stdio: ["ignore", fd, fd] },
222
+ );
223
+ closeSync(fd);
224
+ try {
225
+ await new Promise((resolve, reject) => {
226
+ child.once("spawn", resolve);
227
+ child.once("error", reject);
228
+ });
229
+ } catch (error) {
230
+ task.status = "failed";
231
+ task.error = "工作进程启动失败:" + error.message;
232
+ task.stopConfirmed = true;
233
+ task.finishedAt = new Date().toISOString();
234
+ writeJson(file, task);
235
+ throw error;
236
+ }
39
237
  child.unref();
40
- return {taskId,status:'queued',logPath};
238
+ return {
239
+ taskId,
240
+ status: "queued",
241
+ project: plan.project,
242
+ job: plan.job,
243
+ logPath,
244
+ };
41
245
  });
42
246
  }
43
- export async function waitTask(root,taskId,{timeoutSeconds=3600}={}) {
44
- const deadline=Date.now()+timeoutSeconds*1000;
45
- while(true){const state=taskStatus(root,taskId);if(terminal(state.status))return state;if(Date.now()>deadline)throw new Error('等待超时;任务仍在后台运行');await sleep(250);}
247
+ export async function waitTask(root, taskId, { timeoutSeconds = 3600 } = {}) {
248
+ const deadline = Date.now() + timeoutSeconds * 1000;
249
+ while (true) {
250
+ const state = taskStatus(root, taskId);
251
+ if (terminal(state.status)) return state;
252
+ if (Date.now() > deadline) throw new Error("等待超时;任务仍在后台运行");
253
+ await sleep(250);
254
+ }
46
255
  }
47
- export async function cancelTask(root,taskId,{wait=true}={}) {
48
- const task=taskStatus(root,taskId);
49
- if(terminal(task.status)) {
50
- if(task.endpoint&&task.promptId){await cancelPrompt(task.endpoint,task.promptId);task.promptId=null;task.unsettled=false;if(task.status!=='succeeded')task.status='cancelled';writeJson(taskFile(root,taskId),task);}
51
- return taskStatus(root,taskId);
256
+ export async function cancelTask(root, taskId, { wait = true } = {}) {
257
+ const task = taskStatus(root, taskId),
258
+ file = taskFile(root, taskId);
259
+ if (executionSettled(task)) return task;
260
+ // Only the worker writes a live run document. Cancellation is a separate request;
261
+ // parent read-modify-write would race its heartbeat and lose prompt identities.
262
+ writeJson(file + ".cancel", { requestedAt: new Date().toISOString() });
263
+ if (terminal(task.status)) {
264
+ if (alive(task.workerPid))
265
+ throw new Error("工作进程尚未退出,停止结果待确认");
266
+ if (!task.endpoint || !task.promptId)
267
+ throw new Error("没有可核验的后端任务身份,停止结果未知;保留资源");
268
+ await cancelPrompt(task.endpoint, task.promptId);
269
+ const latest = taskStatus(root, taskId);
270
+ if (alive(latest.workerPid) || latest.promptId !== task.promptId)
271
+ throw new Error("执行身份已变化,请重新查询");
272
+ latest.promptId = null;
273
+ latest.unsettled = false;
274
+ latest.stopConfirmed = true;
275
+ latest.status = "cancelled";
276
+ latest.finishedAt = new Date().toISOString();
277
+ writeJson(file, latest);
278
+ return latest;
52
279
  }
53
- writeJson(taskFile(root,taskId)+'.cancel',{requestedAt:new Date().toISOString()});
54
- return wait?waitTask(root,taskId,{timeoutSeconds:60}):{taskId,status:'cancelling'};
280
+ return wait
281
+ ? waitTask(root, taskId, { timeoutSeconds: 60 })
282
+ : { taskId, status: "cancelling", stopConfirmed: false };
55
283
  }
56
- export async function shutdown(settings) {
57
- const root=runtimeRoot(settings);
58
- return locked(root,'control',async()=>{
59
- const gate=join(root,'shutdown.json');writeJson(gate,{pid:process.pid,startedAt:new Date().toISOString()});
60
- const failures=[],stopped=[];
284
+
285
+ export async function shutdown(settings, { closeServices = true } = {}) {
286
+ const root = runtimeRoot(settings);
287
+ return locked(root, "control", async () => {
288
+ const gate = join(root, "shutdown.json");
289
+ writeJson(gate, { pid: process.pid, startedAt: new Date().toISOString() });
290
+ const failures = [],
291
+ stopped = [];
61
292
  try {
62
- const active=tasks(root).filter(t=>!terminal(taskStatus(root,t.taskId).status));
63
- for(const task of active)await cancelTask(root,task.taskId,{wait:false});
64
- const backends=Object.entries(settings.generation?.backends??{}).filter(([,b])=>b.provider==='comfyui').map(([id,b])=>({id,...b}));
293
+ const active = tasks(root).filter(
294
+ (t) => !executionSettled(taskStatus(root, t.taskId)),
295
+ );
296
+ for (const task of active)
297
+ try {
298
+ await cancelTask(root, task.taskId, { wait: false });
299
+ } catch (e) {
300
+ failures.push(task.taskId + ": " + e.message);
301
+ }
302
+ const backends = Object.entries(settings.generation?.backends ?? {})
303
+ .filter(([, b]) => b.provider === "comfyui")
304
+ .map(([id, b]) => ({ id, ...b }));
65
305
  // Frozen tasks may refer to backends removed from the current configuration.
66
- for(const task of tasks(root))if(task.plan?.backend?.provider==='comfyui')backends.push(task.plan.backend);
67
- const endpoints=new Map();for(const b of backends)for(const endpoint of b.endpoints)endpoints.set(endpoint,b);
68
- for(const [endpoint,backend] of endpoints) {
69
- try {if(await healthy(endpoint,backend))await cancelPrompt(endpoint,null,{all:true});}
70
- catch(e){failures.push(endpoint+': '+e.message);}
71
- }
72
- for(const task of active)try{await waitTask(root,task.taskId,{timeoutSeconds:60});}catch(e){failures.push(task.taskId+': '+e.message);}
306
+ for (const task of tasks(root))
307
+ if (task.plan?.backend?.provider === "comfyui")
308
+ backends.push(task.plan.backend);
309
+ const endpoints = new Map();
310
+ for (const b of backends)
311
+ for (const endpoint of b.endpoints) endpoints.set(endpoint, b);
312
+ for (const task of active)
313
+ try {
314
+ await waitTask(root, task.taskId, { timeoutSeconds: 60 });
315
+ } catch (e) {
316
+ failures.push(task.taskId + ": " + e.message);
317
+ }
318
+ // A final heartbeat may precede worker exit by a few milliseconds.
319
+ const exitDeadline = Date.now() + 5000;
320
+ while (
321
+ tasks(root).some((t) => alive(t.workerPid)) &&
322
+ Date.now() < exitDeadline
323
+ )
324
+ await sleep(50);
73
325
  // Never close the service while a live art worker is still using it.
74
- if(tasks(root).some(t=>!terminal(taskStatus(root,t.taskId).status)))throw new Error('任务尚未停止,保留服务:'+failures.join('; '));
75
- if(failures.length)throw new Error('任务停止尚未确认,保留服务:'+failures.join('; '));
76
- for(const [endpoint,backend] of endpoints)try{stopped.push(await closeService(root,endpoint,backend));}catch(e){failures.push(endpoint+': '+e.message);}
77
- if(failures.length)throw new Error('shutdown 未完整完成:'+failures.join('; '));
78
- return {ok:true,cancelledTasks:active.map(t=>t.taskId),remoteCancellationUnconfirmed:active.filter(t=>taskStatus(root,t.taskId).remoteCancellation).map(t=>t.taskId),services:stopped};
79
- }finally{rmSync(gate,{force:true});}
326
+ if (
327
+ tasks(root).some(
328
+ (t) =>
329
+ !executionSettled(taskStatus(root, t.taskId)) || alive(t.workerPid),
330
+ )
331
+ )
332
+ throw new Error("任务尚未停止,保留服务:" + failures.join("; "));
333
+ if (failures.length)
334
+ throw new Error("任务停止尚未确认,保留服务:" + failures.join("; "));
335
+ if (!closeServices)
336
+ return {
337
+ ok: true,
338
+ cancelledTasks: active.map((t) => t.taskId),
339
+ services: [],
340
+ };
341
+ // A shared ComfyUI can contain jobs outside this workspace. Never clear its
342
+ // queue or interrupt them; owned workers cancel only their verified prompt.
343
+ for (const [endpoint, backend] of endpoints)
344
+ if (await healthy(endpoint, backend)) {
345
+ const queue = await http(endpoint, "/queue");
346
+ if (
347
+ (queue.queue_running ?? []).length ||
348
+ (queue.queue_pending ?? []).length
349
+ )
350
+ throw new Error("服务仍有其他任务,保留服务:" + endpoint);
351
+ }
352
+ for (const [endpoint, backend] of endpoints)
353
+ try {
354
+ stopped.push(await closeService(root, endpoint, backend));
355
+ } catch (e) {
356
+ failures.push(endpoint + ": " + e.message);
357
+ }
358
+ if (failures.length)
359
+ throw new Error("shutdown 未完整完成:" + failures.join("; "));
360
+ return {
361
+ ok: true,
362
+ cancelledTasks: active.map((t) => t.taskId),
363
+ remoteCancellationUnconfirmed: active
364
+ .filter((t) => taskStatus(root, t.taskId).remoteCancellation)
365
+ .map((t) => t.taskId),
366
+ services: stopped,
367
+ };
368
+ } finally {
369
+ rmSync(gate, { force: true });
370
+ }
371
+ });
372
+ }
373
+
374
+ export function recoverTask(root, projectId, operationId) {
375
+ if (
376
+ typeof operationId !== "string" ||
377
+ !/^[a-z0-9-]{1,128}$/.test(operationId)
378
+ )
379
+ throw new Error("恢复操作身份非法");
380
+ const matches = tasks(root).filter(
381
+ (t) =>
382
+ t.operationId === operationId &&
383
+ (t.plan?.project ?? t.project) === projectId,
384
+ );
385
+ if (matches.length !== 1)
386
+ throw new Error("没有唯一的原运行证据,停止状态未知;禁止重新启动");
387
+ return taskView(taskStatus(root, matches[0].taskId));
388
+ }
389
+
390
+ /** Resume only durable original remote identities. This path never submits a new request. */
391
+ export async function resumeTask(root, taskId) {
392
+ return locked(root, "control", async () => {
393
+ const task = taskStatus(root, taskId),
394
+ file = taskFile(root, taskId);
395
+ if (alive(task.workerPid) || !terminal(task.status))
396
+ throw new Error("原工作进程仍在执行,不能恢复");
397
+ validatePlan(task.plan);
398
+ if (executionSettled(task) && task.status === "succeeded")
399
+ return taskView(task);
400
+ const identities = Object.entries(task.remoteExecutions ?? {});
401
+ if (
402
+ !identities.length ||
403
+ identities.some(
404
+ ([key, receipt]) =>
405
+ !["accepted", "completed", "failed", "cancelled"].includes(
406
+ receipt.state,
407
+ ) ||
408
+ !task.items.some(
409
+ (item) =>
410
+ item.inputHash === key &&
411
+ item.request &&
412
+ hash(item.request) === receipt.requestHash,
413
+ ),
414
+ )
415
+ )
416
+ throw new Error("缺少唯一可恢复的上游任务身份;禁止重新提交");
417
+ if (
418
+ !identities.some(([, receipt]) =>
419
+ ["accepted", "completed"].includes(receipt.state),
420
+ )
421
+ )
422
+ throw new Error("没有可继续查询或取回的原任务");
423
+ const gate = stateRead(join(root, "shutdown.json"));
424
+ if (gate && alive(gate.pid)) throw new Error("shutdown 正在执行,不能恢复");
425
+ task.error = null;
426
+ task.diagnostic = null;
427
+ task.resumeOnly = true;
428
+ task.status = "queued";
429
+ task.stopConfirmed = false;
430
+ task.unsettled = true;
431
+ task.finishedAt = null;
432
+ task.heartbeat = new Date().toISOString();
433
+ task.workerPid = null;
434
+ rmSync(file + ".cancel", { force: true });
435
+ if (gate) rmSync(join(root, "shutdown.json"), { force: true });
436
+ writeJson(file, task);
437
+ const fd = openSync(task.logPath, "a"),
438
+ child = spawn(
439
+ process.execPath,
440
+ [fileURLToPath(new URL("./task-worker.mjs", import.meta.url)), file],
441
+ { detached: true, windowsHide: true, stdio: ["ignore", fd, fd] },
442
+ );
443
+ closeSync(fd);
444
+ try {
445
+ await new Promise((resolve, reject) => {
446
+ child.once("spawn", resolve);
447
+ child.once("error", reject);
448
+ });
449
+ } catch (error) {
450
+ task.status = "unknown";
451
+ task.error = error.message;
452
+ writeJson(file, task);
453
+ throw error;
454
+ }
455
+ child.unref();
456
+ return taskView(task);
80
457
  });
81
458
  }
@@ -0,0 +1,69 @@
1
+ import {loadCore} from './core.mjs';
2
+ import {loadProject} from './project.mjs';
3
+ function generateTemplate(projectId,core,kind) {
4
+ return {
5
+ project: projectId,
6
+ kind: "generate",
7
+ core,
8
+ asset: {
9
+ project: projectId,
10
+ id: "custom",
11
+ name: "自定义画布",
12
+ constraints: {
13
+ text: false,
14
+ frame: false,
15
+ scene: false,
16
+ crop: false,
17
+ shadow: false,
18
+ },
19
+ defaults: {
20
+ preset: "centered",
21
+ view: "正面",
22
+ direction: "无方向",
23
+ occupancy: 0.7,
24
+ effectIntensity: "中",
25
+ background: { type: "solid", color: "#FFFFFF" },
26
+ },
27
+ allowedOverrides: {
28
+ preset: ["centered"],
29
+ view: ["正面", "等距视角", "俯视"],
30
+ direction: ["无方向"],
31
+ occupancy: { min: 0.1, max: 0.95 },
32
+ effectIntensity: ["弱", "中", "强"],
33
+ },
34
+ presets: { centered: "主体居中,完整展示,四周保留安全空间" },
35
+ checks: ["主体完整", "需求明确"],
36
+ design: { resolution: { width: 512, height: 512 }, outputScale: 1 },
37
+ },
38
+ items: [
39
+ {
40
+ id: "asset-1",
41
+ intent: "",
42
+ entities: [
43
+ {
44
+ id: "subject",
45
+ kind,
46
+ appearance: "",
47
+ color: "保持需求所述身份色",
48
+ role: "primary",
49
+ count: 1,
50
+ },
51
+ ],
52
+ effects: [],
53
+ composition: { focalTarget: "subject" },
54
+ mustRead: [],
55
+ variants: [{ id: "base" }],
56
+ },
57
+ ],
58
+ };
59
+ }
60
+
61
+
62
+ export function jobTemplate(root,projectId,{kind='generate',core}={}){
63
+ loadProject(projectId,root);
64
+ if(kind==='edit')return {project:projectId,kind:'edit',items:[{id:'asset-1',changes:[],preserve:[],mustRead:[]}]};
65
+ if(kind!=='generate'||!core)throw new Error('生成草稿需要选择已冻结核心');
66
+ const frozen=loadCore(projectId,core,root),domain=Object.keys(frozen.config.domains)[0];
67
+ if(!domain)throw new Error('冻结核心缺少题材配置');
68
+ return generateTemplate(projectId,core,domain);
69
+ }
@@ -0,0 +1,69 @@
1
+ import { spawn } from "node:child_process";
2
+ import { resolveBlEntry } from "../bl.mjs";
3
+ /** Shared subprocess transport; a malformed success response is still a failure. */
4
+ export function callBailian(
5
+ args,
6
+ { cancelled = () => false, onRemoteCancel = () => {} } = {},
7
+ ) {
8
+ return new Promise((resolve, reject) => {
9
+ const child = spawn(process.execPath, [resolveBlEntry(), ...args], {
10
+ windowsHide: true,
11
+ stdio: ["ignore", "pipe", "pipe"],
12
+ });
13
+ let out = "",
14
+ err = "",
15
+ overflow = false,
16
+ stopping = false;
17
+ const uncertain = () => {
18
+ if (!stopping) {
19
+ stopping = true;
20
+ onRemoteCancel();
21
+ }
22
+ };
23
+ child.stdout.on("data", (chunk) => {
24
+ out += chunk;
25
+ if (out.length > 64 * 1024 * 1024) {
26
+ overflow = true;
27
+ uncertain();
28
+ child.kill();
29
+ }
30
+ });
31
+ child.stderr.on("data", (chunk) => {
32
+ err = (err + chunk).slice(-4000);
33
+ });
34
+ const timer = setInterval(() => {
35
+ if (cancelled()) {
36
+ uncertain();
37
+ child.kill();
38
+ }
39
+ }, 100);
40
+ child.once("error", (error) => {
41
+ clearInterval(timer);
42
+ reject(error);
43
+ });
44
+ child.once("close", (code) => {
45
+ clearInterval(timer);
46
+ if (cancelled() || overflow || code !== 0) {
47
+ uncertain();
48
+ return reject(
49
+ new Error(
50
+ cancelled()
51
+ ? "任务已取消"
52
+ : overflow
53
+ ? "远程响应过大"
54
+ : "bl 调用失败(退出码 " + code + ")",
55
+ ),
56
+ );
57
+ }
58
+ try {
59
+ const value = JSON.parse(out);
60
+ if (!value || typeof value !== "object" || Array.isArray(value))
61
+ throw new Error();
62
+ resolve(value);
63
+ } catch {
64
+ uncertain();
65
+ reject(new Error("远程客户端返回无效 JSON;执行结果未确认"));
66
+ }
67
+ });
68
+ });
69
+ }