@zmskco/dsh-notebook 0.1.0 → 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.
- package/LICENSE +23 -23
- package/README.md +29 -75
- package/assets/material-icons/LICENSE.txt +8 -0
- package/assets/material-icons/NOTICE.md +13 -0
- package/cordis.patch.yml +4 -2
- package/icon.png +0 -0
- package/lib/client.js +6803 -261
- package/lib/http/api.js +69 -14
- package/lib/index.js +19 -8
- package/lib/story/bootstrap.js +4 -9
- package/lib/story/manuscript.js +137 -14
- package/lib/story/project.js +263 -30
- package/lib/story/readme.js +9 -9
- package/lib/story/root.js +12 -9
- package/lib/story/sandbox.js +33 -106
- package/lib/story/workspace.js +1 -15
- package/locale/en.json +6 -6
- package/locale/zh.json +6 -6
- package/package.json +94 -89
- package/icon.svg +0 -9
- package/lib/config.js +0 -43
- package/lib/http/media.js +0 -187
- package/lib/render/backend.js +0 -247
- package/lib/render/jobs.js +0 -267
- package/lib/render/png.js +0 -125
- package/lib/tools.js +0 -341
package/lib/render/backend.js
DELETED
|
@@ -1,247 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* The image backend seam.
|
|
3
|
-
*
|
|
4
|
-
* Novel-to-comic needs pixels, but where they come from is a deployment
|
|
5
|
-
* decision — and during plugin development there is usually no image stack on
|
|
6
|
-
* the machine at all. So the renderer talks to this interface, and the profile
|
|
7
|
-
* picks an implementation: `stub` (deterministic placeholders, no service
|
|
8
|
-
* required) or `comfy` (a ComfyUI HTTP API). Everything above the seam —
|
|
9
|
-
* prompt assembly, jobs, progress, panel files, the media route, previews — is
|
|
10
|
-
* identical for both, which is what makes the stub a real test rather than a
|
|
11
|
-
* mock.
|
|
12
|
-
*
|
|
13
|
-
* ComfyUI is addressed over HTTP with a configurable base URL, so it may be a
|
|
14
|
-
* process on this machine or one on the LAN; the code path is the same and the
|
|
15
|
-
* failure modes are surfaced as text rather than assumed away.
|
|
16
|
-
*/
|
|
17
|
-
import { readFile } from 'node:fs/promises';
|
|
18
|
-
import { NotebookError, asNotebookError } from '../errors.js';
|
|
19
|
-
import { encodePlaceholderPng } from './png.js';
|
|
20
|
-
const COMFY_CLIENT_ID = 'dsh-notebook';
|
|
21
|
-
function delay(ms, signal) {
|
|
22
|
-
return new Promise((resolve, reject) => {
|
|
23
|
-
if (signal.aborted) {
|
|
24
|
-
reject(new NotebookError('internal', 'cancelled'));
|
|
25
|
-
return;
|
|
26
|
-
}
|
|
27
|
-
const timer = setTimeout(() => {
|
|
28
|
-
signal.removeEventListener('abort', onAbort);
|
|
29
|
-
resolve();
|
|
30
|
-
}, ms);
|
|
31
|
-
const onAbort = () => {
|
|
32
|
-
clearTimeout(timer);
|
|
33
|
-
reject(new NotebookError('internal', 'cancelled'));
|
|
34
|
-
};
|
|
35
|
-
signal.addEventListener('abort', onAbort, { once: true });
|
|
36
|
-
});
|
|
37
|
-
}
|
|
38
|
-
/** The development backend: real PNG bytes, no image service. */
|
|
39
|
-
export class StubBackend {
|
|
40
|
-
id = 'stub';
|
|
41
|
-
label = 'Placeholder renderer';
|
|
42
|
-
async probe() {
|
|
43
|
-
return {
|
|
44
|
-
ready: true,
|
|
45
|
-
detail: 'Renders deterministic placeholder panels. Configure imageBackend "comfy" to use a real model.',
|
|
46
|
-
};
|
|
47
|
-
}
|
|
48
|
-
async generate(request, signal) {
|
|
49
|
-
// A short pause and an abort check keep the job pipeline honest: progress,
|
|
50
|
-
// cancellation and partial-batch behaviour are exercised, not skipped.
|
|
51
|
-
await delay(120, signal);
|
|
52
|
-
return encodePlaceholderPng(request.prompt, request.seed, request.width, request.height);
|
|
53
|
-
}
|
|
54
|
-
}
|
|
55
|
-
/**
|
|
56
|
-
* Write one `"<nodeId>.<inputName>"` mapping into a workflow graph.
|
|
57
|
-
* @returns `true` when the node and input both existed.
|
|
58
|
-
*/
|
|
59
|
-
function applyNodeInput(graph, mapping, value) {
|
|
60
|
-
if (mapping === undefined)
|
|
61
|
-
return false;
|
|
62
|
-
const separator = mapping.lastIndexOf('.');
|
|
63
|
-
if (separator <= 0)
|
|
64
|
-
return false;
|
|
65
|
-
const node = graph[mapping.slice(0, separator)];
|
|
66
|
-
if (node === undefined)
|
|
67
|
-
return false;
|
|
68
|
-
const inputs = node.inputs ?? (node.inputs = {});
|
|
69
|
-
inputs[mapping.slice(separator + 1)] = value;
|
|
70
|
-
return true;
|
|
71
|
-
}
|
|
72
|
-
/** Extract the first produced image descriptor from a `/history` entry. */
|
|
73
|
-
function firstImage(outputs) {
|
|
74
|
-
if (typeof outputs !== 'object' || outputs === null)
|
|
75
|
-
return undefined;
|
|
76
|
-
for (const node of Object.values(outputs)) {
|
|
77
|
-
const images = node.images;
|
|
78
|
-
if (!Array.isArray(images))
|
|
79
|
-
continue;
|
|
80
|
-
for (const image of images) {
|
|
81
|
-
const candidate = image;
|
|
82
|
-
if (typeof candidate.filename !== 'string')
|
|
83
|
-
continue;
|
|
84
|
-
return {
|
|
85
|
-
filename: candidate.filename,
|
|
86
|
-
subfolder: typeof candidate.subfolder === 'string' ? candidate.subfolder : '',
|
|
87
|
-
type: typeof candidate.type === 'string' ? candidate.type : 'output',
|
|
88
|
-
};
|
|
89
|
-
}
|
|
90
|
-
}
|
|
91
|
-
return undefined;
|
|
92
|
-
}
|
|
93
|
-
/** The ComfyUI HTTP backend. */
|
|
94
|
-
export class ComfyUiBackend {
|
|
95
|
-
baseUrl;
|
|
96
|
-
workflowPath;
|
|
97
|
-
nodes;
|
|
98
|
-
timeoutMs;
|
|
99
|
-
log;
|
|
100
|
-
id = 'comfy';
|
|
101
|
-
label = 'ComfyUI';
|
|
102
|
-
constructor(baseUrl, workflowPath, nodes, timeoutMs, log) {
|
|
103
|
-
this.baseUrl = baseUrl;
|
|
104
|
-
this.workflowPath = workflowPath;
|
|
105
|
-
this.nodes = nodes;
|
|
106
|
-
this.timeoutMs = timeoutMs;
|
|
107
|
-
this.log = log;
|
|
108
|
-
}
|
|
109
|
-
async fetchWithTimeout(path, init, signal) {
|
|
110
|
-
const timeout = AbortSignal.timeout(this.timeoutMs);
|
|
111
|
-
const combined = AbortSignal.any([signal, timeout]);
|
|
112
|
-
try {
|
|
113
|
-
return await fetch(`${this.baseUrl}${path}`, { ...init, signal: combined });
|
|
114
|
-
}
|
|
115
|
-
catch (error) {
|
|
116
|
-
if (signal.aborted)
|
|
117
|
-
throw new NotebookError('internal', 'cancelled');
|
|
118
|
-
if (timeout.aborted) {
|
|
119
|
-
throw new NotebookError('backend-unavailable', `${this.baseUrl}${path} timed out after ${String(this.timeoutMs)} ms`);
|
|
120
|
-
}
|
|
121
|
-
throw new NotebookError('backend-unavailable', `cannot reach ComfyUI at ${this.baseUrl}${path}: ${asNotebookError(error).message}`);
|
|
122
|
-
}
|
|
123
|
-
}
|
|
124
|
-
async probe(signal) {
|
|
125
|
-
if (this.workflowPath === null) {
|
|
126
|
-
return { ready: false, detail: 'comfyWorkflow is unset; export a workflow with "Save (API Format)" and point the config at it.' };
|
|
127
|
-
}
|
|
128
|
-
if (this.nodes.prompt === undefined) {
|
|
129
|
-
return { ready: false, detail: 'comfyNodes.prompt is unset; map it to the positive-prompt input, for example "6.text".' };
|
|
130
|
-
}
|
|
131
|
-
try {
|
|
132
|
-
const response = await this.fetchWithTimeout('/system_stats', { method: 'GET' }, signal);
|
|
133
|
-
if (!response.ok) {
|
|
134
|
-
return { ready: false, detail: `ComfyUI answered ${String(response.status)} for /system_stats` };
|
|
135
|
-
}
|
|
136
|
-
return { ready: true, detail: `ComfyUI reachable at ${this.baseUrl}; workflow ${this.workflowPath}` };
|
|
137
|
-
}
|
|
138
|
-
catch (error) {
|
|
139
|
-
return { ready: false, detail: asNotebookError(error).message };
|
|
140
|
-
}
|
|
141
|
-
}
|
|
142
|
-
async loadGraph() {
|
|
143
|
-
if (this.workflowPath === null) {
|
|
144
|
-
throw new NotebookError('bad-request', 'comfyWorkflow is unset');
|
|
145
|
-
}
|
|
146
|
-
let text;
|
|
147
|
-
try {
|
|
148
|
-
text = await readFile(this.workflowPath, 'utf8');
|
|
149
|
-
}
|
|
150
|
-
catch (error) {
|
|
151
|
-
throw new NotebookError('bad-request', `cannot read the ComfyUI workflow at ${this.workflowPath}: ${asNotebookError(error).message}`);
|
|
152
|
-
}
|
|
153
|
-
let parsed;
|
|
154
|
-
try {
|
|
155
|
-
parsed = JSON.parse(text);
|
|
156
|
-
}
|
|
157
|
-
catch (error) {
|
|
158
|
-
throw new NotebookError('bad-request', `${this.workflowPath} is not valid JSON: ${asNotebookError(error).message}`);
|
|
159
|
-
}
|
|
160
|
-
// "Save (API Format)" emits either the bare map or a wrapper carrying it.
|
|
161
|
-
const graph = parsed.prompt ?? parsed;
|
|
162
|
-
if (typeof graph !== 'object' || graph === null || Array.isArray(graph)) {
|
|
163
|
-
throw new NotebookError('bad-request', `${this.workflowPath} is not a ComfyUI API-format graph (expected an object of nodes)`);
|
|
164
|
-
}
|
|
165
|
-
return structuredClone(graph);
|
|
166
|
-
}
|
|
167
|
-
async generate(request, signal) {
|
|
168
|
-
const graph = await this.loadGraph();
|
|
169
|
-
const missing = [];
|
|
170
|
-
const set = (mapping, value, name) => {
|
|
171
|
-
if (!applyNodeInput(graph, mapping, value))
|
|
172
|
-
missing.push(name);
|
|
173
|
-
};
|
|
174
|
-
set(this.nodes.prompt, request.prompt, 'prompt');
|
|
175
|
-
if (request.negativePrompt !== undefined)
|
|
176
|
-
set(this.nodes.negativePrompt, request.negativePrompt, 'negativePrompt');
|
|
177
|
-
set(this.nodes.seed, request.seed, 'seed');
|
|
178
|
-
set(this.nodes.width, request.width, 'width');
|
|
179
|
-
set(this.nodes.height, request.height, 'height');
|
|
180
|
-
if (request.checkpoint !== undefined)
|
|
181
|
-
set(this.nodes.checkpoint, request.checkpoint, 'checkpoint');
|
|
182
|
-
if (missing.length > 0) {
|
|
183
|
-
throw new NotebookError('bad-request', `the workflow has no node matching ${missing.join(', ')}; check comfyNodes against the exported graph`);
|
|
184
|
-
}
|
|
185
|
-
const submitted = await this.fetchWithTimeout('/prompt', {
|
|
186
|
-
method: 'POST',
|
|
187
|
-
headers: { 'content-type': 'application/json' },
|
|
188
|
-
body: JSON.stringify({ prompt: graph, client_id: COMFY_CLIENT_ID }),
|
|
189
|
-
}, signal);
|
|
190
|
-
if (!submitted.ok) {
|
|
191
|
-
const detail = await submitted.text().catch(() => '');
|
|
192
|
-
throw new NotebookError('backend-failed', `ComfyUI rejected the prompt (${String(submitted.status)}): ${detail.slice(0, 500)}`);
|
|
193
|
-
}
|
|
194
|
-
const queued = await submitted.json();
|
|
195
|
-
if (typeof queued.prompt_id !== 'string') {
|
|
196
|
-
throw new NotebookError('backend-failed', 'ComfyUI accepted the request but returned no prompt_id');
|
|
197
|
-
}
|
|
198
|
-
const promptId = queued.prompt_id;
|
|
199
|
-
const deadline = Date.now() + this.timeoutMs;
|
|
200
|
-
for (;;) {
|
|
201
|
-
if (signal.aborted)
|
|
202
|
-
throw new NotebookError('internal', 'cancelled');
|
|
203
|
-
if (Date.now() > deadline) {
|
|
204
|
-
throw new NotebookError('backend-unavailable', `ComfyUI did not finish ${promptId} within ${String(this.timeoutMs)} ms`);
|
|
205
|
-
}
|
|
206
|
-
await delay(700, signal);
|
|
207
|
-
const historyResponse = await this.fetchWithTimeout(`/history/${encodeURIComponent(promptId)}`, { method: 'GET' }, signal);
|
|
208
|
-
if (!historyResponse.ok)
|
|
209
|
-
continue;
|
|
210
|
-
const history = await historyResponse.json();
|
|
211
|
-
const record = history[promptId];
|
|
212
|
-
if (record === undefined)
|
|
213
|
-
continue;
|
|
214
|
-
const status = record.status?.status_str;
|
|
215
|
-
if (status === 'error') {
|
|
216
|
-
const messages = JSON.stringify(record.status?.messages ?? []).slice(0, 500);
|
|
217
|
-
throw new NotebookError('backend-failed', `ComfyUI reported an error for ${promptId}: ${messages}`);
|
|
218
|
-
}
|
|
219
|
-
const image = firstImage(record.outputs);
|
|
220
|
-
if (image === undefined) {
|
|
221
|
-
// Completed without an image node output: nothing to fetch.
|
|
222
|
-
if (status === 'success') {
|
|
223
|
-
throw new NotebookError('backend-failed', `ComfyUI finished ${promptId} without producing an image; check the workflow's output node`);
|
|
224
|
-
}
|
|
225
|
-
continue;
|
|
226
|
-
}
|
|
227
|
-
const query = new URLSearchParams({ filename: image.filename, subfolder: image.subfolder, type: image.type });
|
|
228
|
-
const imageResponse = await this.fetchWithTimeout(`/view?${query.toString()}`, { method: 'GET' }, signal);
|
|
229
|
-
if (!imageResponse.ok) {
|
|
230
|
-
throw new NotebookError('backend-failed', `ComfyUI could not serve ${image.filename} (${String(imageResponse.status)})`);
|
|
231
|
-
}
|
|
232
|
-
return new Uint8Array(await imageResponse.arrayBuffer());
|
|
233
|
-
}
|
|
234
|
-
}
|
|
235
|
-
}
|
|
236
|
-
/**
|
|
237
|
-
* Build the backend the configuration selects.
|
|
238
|
-
* @param config - resolved plugin configuration.
|
|
239
|
-
* @param log - plugin logger, used for backend warnings.
|
|
240
|
-
* @returns the backend instance.
|
|
241
|
-
*/
|
|
242
|
-
export function createBackend(config, log) {
|
|
243
|
-
if (config.imageBackend === 'comfy') {
|
|
244
|
-
return new ComfyUiBackend(config.comfyUrl, config.comfyWorkflow, config.comfyNodes, config.requestTimeoutMs, log);
|
|
245
|
-
}
|
|
246
|
-
return new StubBackend();
|
|
247
|
-
}
|
package/lib/render/jobs.js
DELETED
|
@@ -1,267 +0,0 @@
|
|
|
1
|
-
import { asNotebookError } from '../errors.js';
|
|
2
|
-
import { assertId, assertStoryName } from '../story/paths.js';
|
|
3
|
-
/** How many finished jobs to keep for the page's history list. */
|
|
4
|
-
const HISTORY_LIMIT = 40;
|
|
5
|
-
export class JobRegistry {
|
|
6
|
-
records = new Map();
|
|
7
|
-
listeners = new Set();
|
|
8
|
-
counter = 0;
|
|
9
|
-
/**
|
|
10
|
-
* Create a job in `queued` state.
|
|
11
|
-
* @param label - human-readable summary shown in the UI.
|
|
12
|
-
* @param total - number of work items.
|
|
13
|
-
* @returns the created job's view.
|
|
14
|
-
*/
|
|
15
|
-
create(label, total) {
|
|
16
|
-
this.counter += 1;
|
|
17
|
-
const id = `job-${String(Date.now())}-${String(this.counter)}`;
|
|
18
|
-
const view = {
|
|
19
|
-
id,
|
|
20
|
-
kind: 'render',
|
|
21
|
-
label,
|
|
22
|
-
status: 'queued',
|
|
23
|
-
total,
|
|
24
|
-
completed: 0,
|
|
25
|
-
startedAt: new Date().toISOString(),
|
|
26
|
-
};
|
|
27
|
-
this.records.set(id, { view, controller: new AbortController() });
|
|
28
|
-
this.prune();
|
|
29
|
-
this.publish(view);
|
|
30
|
-
return view;
|
|
31
|
-
}
|
|
32
|
-
/** The cancellation signal for a running job. */
|
|
33
|
-
signal(id) {
|
|
34
|
-
const record = this.records.get(id);
|
|
35
|
-
if (record === undefined)
|
|
36
|
-
throw new Error(`unknown job ${id}`);
|
|
37
|
-
return record.controller.signal;
|
|
38
|
-
}
|
|
39
|
-
/** Replace the mutable fields of a job and notify subscribers. */
|
|
40
|
-
update(id, patch) {
|
|
41
|
-
const record = this.records.get(id);
|
|
42
|
-
if (record === undefined)
|
|
43
|
-
throw new Error(`unknown job ${id}`);
|
|
44
|
-
Object.assign(record.view, patch);
|
|
45
|
-
if (patch.status === 'done' || patch.status === 'failed' || patch.status === 'cancelled') {
|
|
46
|
-
record.view.finishedAt = new Date().toISOString();
|
|
47
|
-
}
|
|
48
|
-
this.publish(record.view);
|
|
49
|
-
return record.view;
|
|
50
|
-
}
|
|
51
|
-
/** Abort a running job; the runner settles it as `cancelled`. */
|
|
52
|
-
cancel(id) {
|
|
53
|
-
const record = this.records.get(id);
|
|
54
|
-
if (record === undefined)
|
|
55
|
-
return false;
|
|
56
|
-
if (record.view.status !== 'queued' && record.view.status !== 'running')
|
|
57
|
-
return false;
|
|
58
|
-
record.controller.abort();
|
|
59
|
-
return true;
|
|
60
|
-
}
|
|
61
|
-
get(id) {
|
|
62
|
-
return this.records.get(id)?.view;
|
|
63
|
-
}
|
|
64
|
-
/** Every job, newest first. */
|
|
65
|
-
list() {
|
|
66
|
-
return [...this.records.values()]
|
|
67
|
-
.map(record => record.view)
|
|
68
|
-
.sort((left, right) => right.startedAt.localeCompare(left.startedAt));
|
|
69
|
-
}
|
|
70
|
-
/** Abort every running job; called when the plugin unloads. */
|
|
71
|
-
cancelAll() {
|
|
72
|
-
for (const record of this.records.values())
|
|
73
|
-
record.controller.abort();
|
|
74
|
-
}
|
|
75
|
-
subscribe(listener) {
|
|
76
|
-
this.listeners.add(listener);
|
|
77
|
-
return () => { this.listeners.delete(listener); };
|
|
78
|
-
}
|
|
79
|
-
publish(view) {
|
|
80
|
-
for (const listener of this.listeners) {
|
|
81
|
-
try {
|
|
82
|
-
listener({ ...view });
|
|
83
|
-
}
|
|
84
|
-
catch {
|
|
85
|
-
// A broken subscriber must not take down the runner.
|
|
86
|
-
}
|
|
87
|
-
}
|
|
88
|
-
}
|
|
89
|
-
prune() {
|
|
90
|
-
if (this.records.size <= HISTORY_LIMIT)
|
|
91
|
-
return;
|
|
92
|
-
const settled = [...this.records.values()]
|
|
93
|
-
.filter(record => record.view.finishedAt !== undefined)
|
|
94
|
-
.sort((left, right) => (left.view.finishedAt ?? '').localeCompare(right.view.finishedAt ?? ''));
|
|
95
|
-
while (this.records.size > HISTORY_LIMIT && settled.length > 0) {
|
|
96
|
-
const oldest = settled.shift();
|
|
97
|
-
if (oldest !== undefined)
|
|
98
|
-
this.records.delete(oldest.view.id);
|
|
99
|
-
}
|
|
100
|
-
}
|
|
101
|
-
}
|
|
102
|
-
/**
|
|
103
|
-
* Build the positive and negative prompts for one shot.
|
|
104
|
-
*
|
|
105
|
-
* The character segment is the concatenation of each card's frozen `prompt`,
|
|
106
|
-
* used verbatim: paraphrasing it per shot is precisely what makes a pure
|
|
107
|
-
* prompt+seed pipeline drift, so nothing here rewrites or summarizes it.
|
|
108
|
-
* @param bible - the project bible.
|
|
109
|
-
* @param shot - the panel.
|
|
110
|
-
* @param style - the episode's settings, which may carry a template.
|
|
111
|
-
* @returns the assembled prompt pair.
|
|
112
|
-
*/
|
|
113
|
-
export function assemblePrompt(bible, shot, style) {
|
|
114
|
-
// `?? []` guards a storyboard that was hand-edited into a shape the store's
|
|
115
|
-
// normalizer did not see. Losing one character from a prompt is a bad panel;
|
|
116
|
-
// throwing here is a failed batch with an unreadable error.
|
|
117
|
-
const characterIds = shot.characterIds ?? [];
|
|
118
|
-
const characters = characterIds
|
|
119
|
-
.map(id => bible.characters.find(card => card.id === id)?.prompt)
|
|
120
|
-
.filter((text) => typeof text === 'string' && text.length > 0);
|
|
121
|
-
const scene = shot.sceneId === undefined
|
|
122
|
-
? undefined
|
|
123
|
-
: bible.scenes.find(card => card.id === shot.sceneId)?.prompt;
|
|
124
|
-
const template = style.promptTemplate;
|
|
125
|
-
let prompt;
|
|
126
|
-
if (typeof template === 'string' && template.trim().length > 0) {
|
|
127
|
-
prompt = template
|
|
128
|
-
.replaceAll('{{character}}', characters.join(', '))
|
|
129
|
-
.replaceAll('{{scene}}', scene ?? '')
|
|
130
|
-
.replaceAll('{{shot}}', shot.description)
|
|
131
|
-
.split('\n').map(line => line.replace(/,\s*,/g, ',').replace(/\s{2,}/g, ' ').trim()).join('\n')
|
|
132
|
-
.replace(/^[,\s]+|[,\s]+$/g, '');
|
|
133
|
-
}
|
|
134
|
-
else {
|
|
135
|
-
prompt = [characters.join(', '), shot.description, scene ?? ''].filter(part => part.length > 0).join(', ');
|
|
136
|
-
}
|
|
137
|
-
const negatives = [
|
|
138
|
-
style.negativePrompt,
|
|
139
|
-
...characterIds.map(id => bible.characters.find(card => card.id === id)?.negativePrompt),
|
|
140
|
-
].filter((text) => typeof text === 'string' && text.trim().length > 0);
|
|
141
|
-
return { prompt, negativePrompt: [...new Set(negatives)].join(', ') };
|
|
142
|
-
}
|
|
143
|
-
/** A seed the backend will accept as concrete. */
|
|
144
|
-
function resolveSeed(seed) {
|
|
145
|
-
if (Number.isFinite(seed) && seed >= 0)
|
|
146
|
-
return Math.floor(seed);
|
|
147
|
-
return Math.floor(Math.random() * 2_147_483_647);
|
|
148
|
-
}
|
|
149
|
-
/**
|
|
150
|
-
* Start a render batch and return its job immediately.
|
|
151
|
-
*
|
|
152
|
-
* The job exists before any I/O happens, so the caller always has an id to poll
|
|
153
|
-
* and a synchronous validation failure (a malformed project or episode id)
|
|
154
|
-
* still throws at the call site rather than becoming a failed job.
|
|
155
|
-
* @param deps - store, backend, registry and logger.
|
|
156
|
-
* @param params - the target project, episode and optional shot selection.
|
|
157
|
-
* @returns the queued job view.
|
|
158
|
-
*/
|
|
159
|
-
export function startRender(deps, params) {
|
|
160
|
-
const projectId = assertStoryName(params.project);
|
|
161
|
-
const episode = assertId('episode', params.episode);
|
|
162
|
-
const job = deps.jobs.create(`渲染 ${projectId}/${episode}`, 0);
|
|
163
|
-
const selection = params.shotIds === undefined ? {} : { shotIds: params.shotIds };
|
|
164
|
-
void execute(deps, { project: projectId, episode, ...selection }, job.id).catch((error) => {
|
|
165
|
-
deps.log.warn('notebook: render job crashed', error);
|
|
166
|
-
try {
|
|
167
|
-
deps.jobs.update(job.id, { status: 'failed', error: asNotebookError(error).message });
|
|
168
|
-
}
|
|
169
|
-
catch {
|
|
170
|
-
// The registry was disposed mid-flight; nothing left to settle.
|
|
171
|
-
}
|
|
172
|
-
});
|
|
173
|
-
return job;
|
|
174
|
-
}
|
|
175
|
-
/**
|
|
176
|
-
* Render the requested shots of one episode, one panel at a time.
|
|
177
|
-
*
|
|
178
|
-
* Panels are written and the storyboard is saved after each result, so a batch
|
|
179
|
-
* that fails or is cancelled half way leaves every finished panel on disk and
|
|
180
|
-
* recorded — re-running then resumes rather than repeating.
|
|
181
|
-
*/
|
|
182
|
-
async function execute(deps, params, jobId) {
|
|
183
|
-
const projectId = params.project;
|
|
184
|
-
const episode = params.episode;
|
|
185
|
-
const project = await deps.store.open(projectId);
|
|
186
|
-
const bible = await deps.store.readBible(project);
|
|
187
|
-
let board = await deps.store.readStoryboard(project, episode);
|
|
188
|
-
const selected = params.shotIds === undefined || params.shotIds.length === 0
|
|
189
|
-
? board.shots
|
|
190
|
-
: board.shots.filter(shot => params.shotIds?.includes(shot.id));
|
|
191
|
-
if (selected.length === 0) {
|
|
192
|
-
deps.jobs.update(jobId, { status: 'failed', error: `episode ${episode} has no shots to render`, total: 0 });
|
|
193
|
-
return;
|
|
194
|
-
}
|
|
195
|
-
deps.jobs.update(jobId, {
|
|
196
|
-
status: 'running',
|
|
197
|
-
total: selected.length,
|
|
198
|
-
message: `后端:${deps.backend.label}`,
|
|
199
|
-
});
|
|
200
|
-
const signal = deps.jobs.signal(jobId);
|
|
201
|
-
let completed = 0;
|
|
202
|
-
let failures = 0;
|
|
203
|
-
try {
|
|
204
|
-
for (const shot of selected) {
|
|
205
|
-
if (signal.aborted)
|
|
206
|
-
break;
|
|
207
|
-
shot.status = 'rendering';
|
|
208
|
-
shot.error = undefined;
|
|
209
|
-
await deps.store.writeStoryboard(project, episode, board);
|
|
210
|
-
deps.jobs.update(jobId, { message: `${shot.id}: 生成中` });
|
|
211
|
-
const { prompt, negativePrompt } = assemblePrompt(bible, shot, board.style);
|
|
212
|
-
const seed = resolveSeed(shot.seed);
|
|
213
|
-
try {
|
|
214
|
-
// Guarded because a scene-only shot has no character: `characterIds` may
|
|
215
|
-
// be empty, and the first card is what a reference image hangs off.
|
|
216
|
-
const lead = shot.characterIds[0];
|
|
217
|
-
const reference = lead === undefined
|
|
218
|
-
? undefined
|
|
219
|
-
: bible.characters.find(card => card.id === lead)?.refImage;
|
|
220
|
-
const bytes = await deps.backend.generate({
|
|
221
|
-
prompt,
|
|
222
|
-
...negativePrompt.length > 0 ? { negativePrompt } : {},
|
|
223
|
-
seed,
|
|
224
|
-
width: board.style.width,
|
|
225
|
-
height: board.style.height,
|
|
226
|
-
...board.style.checkpoint !== undefined ? { checkpoint: board.style.checkpoint } : {},
|
|
227
|
-
...reference !== undefined ? { referenceImage: reference } : {},
|
|
228
|
-
}, signal);
|
|
229
|
-
const rel = await deps.store.writeBinary(project, deps.store.panelPath(episode, shot.id), bytes);
|
|
230
|
-
shot.image = rel;
|
|
231
|
-
shot.seed = seed;
|
|
232
|
-
shot.status = 'done';
|
|
233
|
-
shot.error = undefined;
|
|
234
|
-
completed += 1;
|
|
235
|
-
}
|
|
236
|
-
catch (error) {
|
|
237
|
-
if (signal.aborted) {
|
|
238
|
-
shot.status = 'idle';
|
|
239
|
-
await deps.store.writeStoryboard(project, episode, board).catch(() => undefined);
|
|
240
|
-
break;
|
|
241
|
-
}
|
|
242
|
-
failures += 1;
|
|
243
|
-
shot.status = 'failed';
|
|
244
|
-
shot.error = asNotebookError(error).message;
|
|
245
|
-
deps.log.warn(`notebook: shot ${shot.id} failed`, error);
|
|
246
|
-
}
|
|
247
|
-
board = await deps.store.writeStoryboard(project, episode, board);
|
|
248
|
-
deps.jobs.update(jobId, {
|
|
249
|
-
completed,
|
|
250
|
-
message: `${shot.id} ${shot.status === 'done' ? '完成' : '失败'} (${String(completed)}/${String(selected.length)})`,
|
|
251
|
-
});
|
|
252
|
-
}
|
|
253
|
-
if (signal.aborted) {
|
|
254
|
-
deps.jobs.update(jobId, { status: 'cancelled', message: `已取消,完成 ${String(completed)} 格` });
|
|
255
|
-
return;
|
|
256
|
-
}
|
|
257
|
-
deps.jobs.update(jobId, {
|
|
258
|
-
status: failures > 0 && completed === 0 ? 'failed' : 'done',
|
|
259
|
-
completed,
|
|
260
|
-
...failures > 0 ? { error: `${String(failures)} 格渲染失败` } : {},
|
|
261
|
-
message: `完成 ${String(completed)}/${String(selected.length)} 格`,
|
|
262
|
-
});
|
|
263
|
-
}
|
|
264
|
-
catch (error) {
|
|
265
|
-
deps.jobs.update(jobId, { status: 'failed', error: asNotebookError(error).message });
|
|
266
|
-
}
|
|
267
|
-
}
|
package/lib/render/png.js
DELETED
|
@@ -1,125 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* A minimal PNG encoder for the development backend.
|
|
3
|
-
*
|
|
4
|
-
* The stub image backend must produce a real, decodable image so the whole
|
|
5
|
-
* chain it exists to exercise — byte write, media route, `<img>` decode, range
|
|
6
|
-
* read — is genuinely tested rather than mocked. Pulling an image library in
|
|
7
|
-
* for that would put a runtime dependency in a bundle whose whole point is
|
|
8
|
-
* having none, and PNG's baseline encoding is small enough to own: signature,
|
|
9
|
-
* `IHDR`, one deflated `IDAT`, `IEND`.
|
|
10
|
-
*/
|
|
11
|
-
import { deflateSync } from 'node:zlib';
|
|
12
|
-
const CRC_TABLE = (() => {
|
|
13
|
-
const table = new Uint32Array(256);
|
|
14
|
-
for (let index = 0; index < 256; index += 1) {
|
|
15
|
-
let value = index;
|
|
16
|
-
for (let bit = 0; bit < 8; bit += 1) {
|
|
17
|
-
value = (value & 1) !== 0 ? 0xedb88320 ^ (value >>> 1) : value >>> 1;
|
|
18
|
-
}
|
|
19
|
-
table[index] = value >>> 0;
|
|
20
|
-
}
|
|
21
|
-
return table;
|
|
22
|
-
})();
|
|
23
|
-
function crc32(bytes) {
|
|
24
|
-
let value = 0xffffffff;
|
|
25
|
-
for (const byte of bytes)
|
|
26
|
-
value = (CRC_TABLE[(value ^ byte) & 0xff] ?? 0) ^ (value >>> 8);
|
|
27
|
-
return (value ^ 0xffffffff) >>> 0;
|
|
28
|
-
}
|
|
29
|
-
function chunk(type, data) {
|
|
30
|
-
const length = Buffer.alloc(4);
|
|
31
|
-
length.writeUInt32BE(data.length, 0);
|
|
32
|
-
const typeBytes = Buffer.from(type, 'ascii');
|
|
33
|
-
const crc = Buffer.alloc(4);
|
|
34
|
-
crc.writeUInt32BE(crc32(Buffer.concat([typeBytes, data])), 0);
|
|
35
|
-
return Buffer.concat([length, typeBytes, Buffer.from(data), crc]);
|
|
36
|
-
}
|
|
37
|
-
/** A deterministic 32-bit hash, so one seed always yields one image. */
|
|
38
|
-
function hash32(value) {
|
|
39
|
-
let hash = 0x811c9dc5;
|
|
40
|
-
for (let index = 0; index < value.length; index += 1) {
|
|
41
|
-
hash ^= value.charCodeAt(index);
|
|
42
|
-
hash = Math.imul(hash, 0x01000193) >>> 0;
|
|
43
|
-
}
|
|
44
|
-
return hash >>> 0;
|
|
45
|
-
}
|
|
46
|
-
/** Convert HSL (h in degrees, s/l in 0..1) to 8-bit RGB. */
|
|
47
|
-
function hslToRgb(hue, saturation, lightness) {
|
|
48
|
-
const chroma = (1 - Math.abs(2 * lightness - 1)) * saturation;
|
|
49
|
-
const sector = ((hue % 360) + 360) % 360 / 60;
|
|
50
|
-
const second = chroma * (1 - Math.abs((sector % 2) - 1));
|
|
51
|
-
const base = lightness - chroma / 2;
|
|
52
|
-
const table = sector < 1 ? [chroma, second, 0]
|
|
53
|
-
: sector < 2 ? [second, chroma, 0]
|
|
54
|
-
: sector < 3 ? [0, chroma, second]
|
|
55
|
-
: sector < 4 ? [0, second, chroma]
|
|
56
|
-
: sector < 5 ? [second, 0, chroma]
|
|
57
|
-
: [chroma, 0, second];
|
|
58
|
-
return [
|
|
59
|
-
Math.round((table[0] + base) * 255),
|
|
60
|
-
Math.round((table[1] + base) * 255),
|
|
61
|
-
Math.round((table[2] + base) * 255),
|
|
62
|
-
];
|
|
63
|
-
}
|
|
64
|
-
/**
|
|
65
|
-
* Render a deterministic placeholder panel.
|
|
66
|
-
*
|
|
67
|
-
* The seed selects the hue and the direction of the gradient, so two shots
|
|
68
|
-
* with different seeds are visibly different and re-rendering one seed
|
|
69
|
-
* reproduces the same bytes — which is exactly the property the real backend
|
|
70
|
-
* is expected to have.
|
|
71
|
-
* @param label - the prompt text whose hash seeds the palette.
|
|
72
|
-
* @param seed - the recorded shot seed.
|
|
73
|
-
* @param width - pixel width.
|
|
74
|
-
* @param height - pixel height.
|
|
75
|
-
* @returns encoded PNG bytes.
|
|
76
|
-
*/
|
|
77
|
-
export function encodePlaceholderPng(label, seed, width, height) {
|
|
78
|
-
const hash = hash32(`${label}#${String(seed)}`);
|
|
79
|
-
const hue = hash % 360;
|
|
80
|
-
const [topR, topG, topB] = hslToRgb(hue, 0.45, 0.32);
|
|
81
|
-
const [bottomR, bottomG, bottomB] = hslToRgb(hue + 28, 0.55, 0.68);
|
|
82
|
-
const stride = width * 3;
|
|
83
|
-
const raw = Buffer.alloc((stride + 1) * height);
|
|
84
|
-
for (let y = 0; y < height; y += 1) {
|
|
85
|
-
const rowStart = y * (stride + 1);
|
|
86
|
-
raw[rowStart] = 0; // filter type 0 (None)
|
|
87
|
-
const t = height <= 1 ? 0 : y / (height - 1);
|
|
88
|
-
const r = Math.round(topR + (bottomR - topR) * t);
|
|
89
|
-
const g = Math.round(topG + (bottomG - topG) * t);
|
|
90
|
-
const b = Math.round(topB + (bottomB - topB) * t);
|
|
91
|
-
for (let x = 0; x < width; x += 1) {
|
|
92
|
-
const at = rowStart + 1 + x * 3;
|
|
93
|
-
raw[at] = r;
|
|
94
|
-
raw[at + 1] = g;
|
|
95
|
-
raw[at + 2] = b;
|
|
96
|
-
}
|
|
97
|
-
}
|
|
98
|
-
// A caption band whose thickness encodes the seed, so a glance distinguishes
|
|
99
|
-
// two renders that share a palette.
|
|
100
|
-
const bandHeight = 6 + (hash % 24);
|
|
101
|
-
const bandTop = height - bandHeight - 8;
|
|
102
|
-
for (let y = Math.max(0, bandTop); y < Math.min(height, bandTop + bandHeight); y += 1) {
|
|
103
|
-
const rowStart = y * (stride + 1);
|
|
104
|
-
for (let x = 0; x < width; x += 1) {
|
|
105
|
-
const at = rowStart + 1 + x * 3;
|
|
106
|
-
raw[at] = 24;
|
|
107
|
-
raw[at + 1] = 24;
|
|
108
|
-
raw[at + 2] = 28;
|
|
109
|
-
}
|
|
110
|
-
}
|
|
111
|
-
const header = Buffer.alloc(13);
|
|
112
|
-
header.writeUInt32BE(width, 0);
|
|
113
|
-
header.writeUInt32BE(height, 4);
|
|
114
|
-
header[8] = 8; // bit depth
|
|
115
|
-
header[9] = 2; // colour type: truecolour RGB
|
|
116
|
-
header[10] = 0; // deflate
|
|
117
|
-
header[11] = 0; // adaptive filtering
|
|
118
|
-
header[12] = 0; // no interlace
|
|
119
|
-
return Buffer.concat([
|
|
120
|
-
Buffer.from([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a]),
|
|
121
|
-
chunk('IHDR', header),
|
|
122
|
-
chunk('IDAT', deflateSync(raw, { level: 6 })),
|
|
123
|
-
chunk('IEND', new Uint8Array(0)),
|
|
124
|
-
]);
|
|
125
|
-
}
|