@nhtio/adk 0.1.0-master-91c13c80 → 0.1.0-master-a4cc5f78

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 (54) hide show
  1. package/CHANGELOG.md +6 -0
  2. package/batteries/generation/local_diffusion/adapter.cjs +517 -0
  3. package/batteries/generation/local_diffusion/adapter.cjs.map +1 -0
  4. package/batteries/generation/local_diffusion/adapter.d.ts +25 -0
  5. package/batteries/generation/local_diffusion/adapter.mjs +515 -0
  6. package/batteries/generation/local_diffusion/adapter.mjs.map +1 -0
  7. package/batteries/generation/local_diffusion/exceptions.cjs +35 -0
  8. package/batteries/generation/local_diffusion/exceptions.cjs.map +1 -0
  9. package/batteries/generation/local_diffusion/exceptions.d.ts +31 -0
  10. package/batteries/generation/local_diffusion/exceptions.mjs +26 -0
  11. package/batteries/generation/local_diffusion/exceptions.mjs.map +1 -0
  12. package/batteries/generation/local_diffusion/index.d.ts +10 -0
  13. package/batteries/generation/local_diffusion/protocol.cjs +321 -0
  14. package/batteries/generation/local_diffusion/protocol.cjs.map +1 -0
  15. package/batteries/generation/local_diffusion/protocol.d.ts +170 -0
  16. package/batteries/generation/local_diffusion/protocol.mjs +313 -0
  17. package/batteries/generation/local_diffusion/protocol.mjs.map +1 -0
  18. package/batteries/generation/local_diffusion/types.cjs +2 -0
  19. package/batteries/generation/local_diffusion/types.d.ts +129 -0
  20. package/batteries/generation/local_diffusion/types.mjs +0 -0
  21. package/batteries/generation/local_diffusion/validation.cjs +62 -0
  22. package/batteries/generation/local_diffusion/validation.cjs.map +1 -0
  23. package/batteries/generation/local_diffusion/validation.d.ts +5 -0
  24. package/batteries/generation/local_diffusion/validation.mjs +59 -0
  25. package/batteries/generation/local_diffusion/validation.mjs.map +1 -0
  26. package/batteries/generation/local_diffusion.cjs +16 -0
  27. package/batteries/generation/local_diffusion.mjs +4 -0
  28. package/batteries/llm/chat_common/lifecycle.d.ts +12 -4
  29. package/batteries/media/forge.cjs +1 -1
  30. package/batteries/media/forge.mjs +1 -1
  31. package/batteries/media/lint.cjs +1 -1
  32. package/batteries/media/lint.mjs +1 -1
  33. package/batteries/media.cjs +1 -1
  34. package/batteries/media.mjs +1 -1
  35. package/batteries/tts/native/adapter.cjs +1 -1
  36. package/batteries/tts/native/adapter.mjs +1 -1
  37. package/batteries/tts/native.cjs +1 -1
  38. package/batteries/tts/native.mjs +1 -1
  39. package/eslint.cjs +1 -1
  40. package/eslint.mjs +1 -1
  41. package/index.cjs +1 -1
  42. package/index.mjs +1 -1
  43. package/lifecycle-C4kwYJ-N.mjs.map +1 -1
  44. package/lifecycle-vYQJ2rmf.js.map +1 -1
  45. package/mcp/adk-docs-corpus.json +1 -1
  46. package/package.json +331 -301
  47. package/server.json +2 -2
  48. package/skills/adk-assembly/SKILL.md +2 -2
  49. package/{validate-BM00UtKB.mjs → validate-Dnp_0cFt.mjs} +150 -150
  50. package/validate-Dnp_0cFt.mjs.map +1 -0
  51. package/{validate-DnFwZdPY.js → validate-VZXWZ5p4.js} +149 -149
  52. package/validate-VZXWZ5p4.js.map +1 -0
  53. package/validate-BM00UtKB.mjs.map +0 -1
  54. package/validate-DnFwZdPY.js.map +0 -1
@@ -0,0 +1,515 @@
1
+ import { B as isError } from "../../../tokenizable-CNdPvlaY.mjs";
2
+ import { o as decodeBase64, s as encodeBase64 } from "../../../tool_call-COcpJD2x.mjs";
3
+ import "../../../guards.mjs";
4
+ import { n as emitLifecycle } from "../../../lifecycle-C4kwYJ-N.mjs";
5
+ import { toBytes } from "../_shared.mjs";
6
+ import { E_LOCAL_DIFFUSION_ABORTED, E_LOCAL_DIFFUSION_BACKEND_ERROR, E_LOCAL_DIFFUSION_BUSY, E_LOCAL_DIFFUSION_DISPOSED, E_LOCAL_DIFFUSION_MALFORMED_FRAME, E_LOCAL_DIFFUSION_REQUEST_TIMEOUT, E_LOCAL_DIFFUSION_STARTUP_TIMEOUT } from "./exceptions.mjs";
7
+ import { validateOptions } from "./validation.mjs";
8
+ import { DEFAULT_PROTOCOL, buildEditCommand, buildGenerateCommand, buildShutdownCommand, buildStopCommand, createFrameReader } from "./protocol.mjs";
9
+ //#region src/batteries/generation/local_diffusion/adapter.ts
10
+ /** Node-only local diffusion generation adapter.
11
+ * @module @nhtio/adk/batteries/generation/local_diffusion/adapter
12
+ */
13
+ var BATTERY = "local_diffusion_generation";
14
+ var DEFAULT_MAX_DECODED_BYTES = 50 * 1024 * 1024;
15
+ /** The rejection error for a non-success outcome (an aborted outcome maps to the typed abort error). */
16
+ var outcomeError = (outcome) => outcome.kind === "aborted" ? new E_LOCAL_DIFFUSION_ABORTED() : outcome.error;
17
+ var deferred = () => {
18
+ let resolve;
19
+ let reject;
20
+ return {
21
+ promise: new Promise((res, rej) => {
22
+ resolve = res;
23
+ reject = rej;
24
+ }),
25
+ resolve,
26
+ reject
27
+ };
28
+ };
29
+ /**
30
+ * Whether `candidate` resolves to a location strictly inside `dir` — real containment (resolves `..`
31
+ * and rejects sibling-prefix escapes like `/output2`), not a raw string `startsWith`.
32
+ */
33
+ var isContained = (dir, candidate, sep) => {
34
+ const base = dir.endsWith(sep) ? dir.slice(0, -sep.length) : dir;
35
+ return candidate === base || candidate.startsWith(base + sep);
36
+ };
37
+ /** Local diffusion adapter backed by a line-protocol subprocess. */
38
+ var LocalDiffusionGenerationAdapter = class LocalDiffusionGenerationAdapter {
39
+ #options;
40
+ #process;
41
+ #epoch = 0;
42
+ #state = "idle";
43
+ #loadPromise;
44
+ #loadReject;
45
+ #request;
46
+ #rid = 0;
47
+ #disposePromise;
48
+ #listeners = [];
49
+ /** Whether the current host exposes Node's process runtime. */
50
+ static isAvailable() {
51
+ return typeof process !== "undefined" && Boolean(process.versions?.node);
52
+ }
53
+ /** Construct and validate an adapter. */
54
+ constructor(options) {
55
+ this.#options = validateOptions(options);
56
+ }
57
+ /** Whether this instance can run, honoring its injected probe. */
58
+ isAvailable() {
59
+ return (this.#options.isAvailable ?? LocalDiffusionGenerationAdapter.isAvailable)();
60
+ }
61
+ /** Start the backend and wait for its ready event. */
62
+ async preload() {
63
+ this.#assertLive();
64
+ if (this.#state === "ready" || this.#state === "running" || this.#state === "stopping") return;
65
+ if (this.#loadPromise) return this.#loadPromise;
66
+ const loadPromise = this.#start();
67
+ this.#loadPromise = loadPromise;
68
+ loadPromise.catch(() => void 0).finally(() => {
69
+ if (this.#loadPromise === loadPromise) {
70
+ this.#loadPromise = void 0;
71
+ this.#loadReject = void 0;
72
+ }
73
+ });
74
+ return loadPromise;
75
+ }
76
+ /** Synchronously terminate the current process and invalidate all continuations. */
77
+ reset() {
78
+ if (this.#state === "disposed") return;
79
+ this.#retire(new E_LOCAL_DIFFUSION_BACKEND_ERROR(["adapter reset"]));
80
+ this.#state = "idle";
81
+ }
82
+ /** Gracefully stop the backend, then force terminate it. Terminal and idempotent. */
83
+ dispose() {
84
+ if (this.#disposePromise) return this.#disposePromise;
85
+ this.#disposePromise = this.#dispose();
86
+ return this.#disposePromise;
87
+ }
88
+ async #dispose() {
89
+ const process = this.#process;
90
+ this.#retire(new E_LOCAL_DIFFUSION_DISPOSED(), false);
91
+ this.#state = "disposed";
92
+ if (process && process.stdin) {
93
+ try {
94
+ process.stdin.write(buildShutdownCommand(this.#protocol()));
95
+ } catch {}
96
+ await new Promise((resolve) => {
97
+ const timer = setTimeout(resolve, this.#options.disposeGraceMs ?? 5e3);
98
+ const finish = () => {
99
+ clearTimeout(timer);
100
+ process.off("close", finish);
101
+ resolve();
102
+ };
103
+ process.on("close", finish);
104
+ });
105
+ }
106
+ process?.kill();
107
+ }
108
+ /** Async-disposal protocol alias. */
109
+ async [Symbol.asyncDispose]() {
110
+ await this.dispose();
111
+ }
112
+ /** Generate images from text. */
113
+ async generate(prompt, opts) {
114
+ return this.#call("generate", prompt, void 0, opts);
115
+ }
116
+ /** Edit images with text guidance. */
117
+ async edit(inputs, prompt, opts) {
118
+ const normalized = await Promise.all((Array.isArray(inputs) ? inputs : [inputs]).map(toBytes));
119
+ return this.#call("edit", prompt, normalized, opts);
120
+ }
121
+ #assertLive() {
122
+ if (this.#state === "disposed" || this.#disposePromise) throw new E_LOCAL_DIFFUSION_DISPOSED();
123
+ }
124
+ #protocol() {
125
+ const o = this.#options;
126
+ return {
127
+ ...DEFAULT_PROTOCOL,
128
+ ...o.protocol ?? {},
129
+ commandPrefix: o.commandPrefix ?? o.protocol?.commandPrefix ?? DEFAULT_PROTOCOL.commandPrefix,
130
+ eventPrefix: o.eventPrefix ?? o.protocol?.eventPrefix ?? DEFAULT_PROTOCOL.eventPrefix,
131
+ ops: {
132
+ ...DEFAULT_PROTOCOL.ops,
133
+ ...o.protocol?.ops,
134
+ ...o.ops
135
+ },
136
+ control: {
137
+ ...DEFAULT_PROTOCOL.control,
138
+ ...o.protocol?.control,
139
+ ...o.control
140
+ },
141
+ events: {
142
+ ...DEFAULT_PROTOCOL.events,
143
+ ...o.protocol?.events,
144
+ ...o.events
145
+ }
146
+ };
147
+ }
148
+ /**
149
+ * Tear down the current process + request atomically: bump the epoch (fencing every outstanding
150
+ * continuation), detach listeners, reject an in-flight preload, record the request's terminal
151
+ * outcome (which settles once its pending reads drain), and drop cached handles. Kills the child
152
+ * unless `kill` is false — `dispose()` defers the kill until after its graceful-shutdown wait.
153
+ * Leaves `#state` for the caller to set (`idle` for reset, `disposed` for dispose).
154
+ */
155
+ #retire(error, kill = true) {
156
+ this.#epoch += 1;
157
+ this.#detach();
158
+ this.#loadReject?.(error);
159
+ this.#loadReject = void 0;
160
+ this.#loadPromise = void 0;
161
+ const request = this.#request;
162
+ if (request) this.#settle(request, {
163
+ kind: "error",
164
+ error
165
+ });
166
+ if (kill) this.#process?.kill();
167
+ this.#process = void 0;
168
+ }
169
+ async #start() {
170
+ this.#assertLive();
171
+ if (!this.isAvailable()) throw new E_LOCAL_DIFFUSION_BACKEND_ERROR(["Node process is unavailable"]);
172
+ const epoch = ++this.#epoch;
173
+ const startupTimeoutMs = this.#options.startupTimeoutMs ?? 3e4;
174
+ this.#state = "starting";
175
+ const spawner = this.#options.spawn ?? (async (ctx) => {
176
+ const { spawn } = await import("node:child_process");
177
+ const child = spawn(ctx.command, ctx.args, { stdio: [
178
+ "pipe",
179
+ "pipe",
180
+ "pipe"
181
+ ] });
182
+ if (!child.stdin || !child.stdout) throw new E_LOCAL_DIFFUSION_BACKEND_ERROR(["backend stdin/stdout unavailable"]);
183
+ return child;
184
+ });
185
+ let process;
186
+ try {
187
+ process = await spawner({
188
+ command: this.#options.command,
189
+ args: this.#options.args ?? [],
190
+ model: this.#options.model
191
+ });
192
+ } catch (error) {
193
+ throw new E_LOCAL_DIFFUSION_BACKEND_ERROR([isError(error) ? error.message : String(error)]);
194
+ }
195
+ if (epoch !== this.#epoch || this.#state === "disposed") {
196
+ process.kill();
197
+ throw new E_LOCAL_DIFFUSION_BACKEND_ERROR(["startup superseded"]);
198
+ }
199
+ if (!process.stdin || !process.stdout) throw new E_LOCAL_DIFFUSION_BACKEND_ERROR(["backend stdin/stdout unavailable"]);
200
+ this.#process = process;
201
+ const wait = deferred();
202
+ this.#loadReject = wait.reject;
203
+ const reader = createFrameReader({
204
+ maxLineBytes: this.#options.maxLineBytes,
205
+ config: this.#protocol(),
206
+ onFrame: (frame) => this.#onFrame(frame, epoch, wait)
207
+ });
208
+ const onData = (chunk) => {
209
+ if (epoch === this.#epoch) reader.push(chunk);
210
+ };
211
+ const onStreamEnd = () => {
212
+ if (epoch !== this.#epoch) return;
213
+ reader.end();
214
+ failure("backend stdout closed", null, null);
215
+ };
216
+ const failure = (detail, code, signal) => {
217
+ if (epoch !== this.#epoch) return;
218
+ const error = this.#state === "starting" ? new E_LOCAL_DIFFUSION_STARTUP_TIMEOUT([startupTimeoutMs]) : new E_LOCAL_DIFFUSION_BACKEND_ERROR([
219
+ detail,
220
+ code ?? void 0,
221
+ signal ?? void 0
222
+ ]);
223
+ wait.reject(error);
224
+ this.#detach();
225
+ this.#loadReject = void 0;
226
+ this.#loadPromise = void 0;
227
+ const request = this.#request;
228
+ if (request) this.#settle(request, {
229
+ kind: "error",
230
+ error
231
+ });
232
+ this.#process?.kill();
233
+ this.#process = void 0;
234
+ this.#epoch += 1;
235
+ this.#state = "idle";
236
+ };
237
+ const onError = () => failure("backend error", null, null);
238
+ const onExit = (code, signal) => failure("backend exited", code, signal);
239
+ const onStdoutError = () => failure("backend stdout error", null, null);
240
+ process.stdout.on("data", onData);
241
+ process.stdout.on("end", onStreamEnd);
242
+ process.stdout.on("error", onStdoutError);
243
+ process.on("error", onError);
244
+ process.on("exit", onExit);
245
+ process.on("close", onExit);
246
+ this.#listeners.push({
247
+ target: process.stdout,
248
+ event: "data",
249
+ fn: onData
250
+ }, {
251
+ target: process.stdout,
252
+ event: "end",
253
+ fn: onStreamEnd
254
+ }, {
255
+ target: process.stdout,
256
+ event: "error",
257
+ fn: onStdoutError
258
+ }, {
259
+ target: process,
260
+ event: "error",
261
+ fn: onError
262
+ }, {
263
+ target: process,
264
+ event: "exit",
265
+ fn: onExit
266
+ }, {
267
+ target: process,
268
+ event: "close",
269
+ fn: onExit
270
+ });
271
+ await new Promise((resolve, reject) => {
272
+ const timer = setTimeout(() => {
273
+ failure("startup timed out", null, null);
274
+ }, startupTimeoutMs);
275
+ wait.promise.then(() => {
276
+ clearTimeout(timer);
277
+ resolve();
278
+ }, (error) => {
279
+ clearTimeout(timer);
280
+ reject(error);
281
+ });
282
+ });
283
+ if (epoch !== this.#epoch) throw new E_LOCAL_DIFFUSION_BACKEND_ERROR(["startup superseded"]);
284
+ }
285
+ #onFrame(frame, epoch, load) {
286
+ if (epoch !== this.#epoch) return;
287
+ if (frame.kind === "modelLoad") {
288
+ emitLifecycle(this.#options, BATTERY, this.#options.model, "loading", { progress: frame.progress });
289
+ return;
290
+ }
291
+ if (frame.kind === "ready") {
292
+ if (this.#state !== "starting") return;
293
+ this.#state = "ready";
294
+ emitLifecycle(this.#options, BATTERY, this.#options.model, "ready");
295
+ load.resolve(void 0);
296
+ return;
297
+ }
298
+ const request = this.#request;
299
+ if (frame.kind === "malformed" || frame.kind === "protocolError") {
300
+ if (request) {
301
+ if (!request.outcome) request.outcome = {
302
+ kind: "error",
303
+ error: new E_LOCAL_DIFFUSION_MALFORMED_FRAME([frame.detail])
304
+ };
305
+ this.#maybeSettle(request);
306
+ }
307
+ return;
308
+ }
309
+ if (!request || !("rid" in frame) || frame.rid !== request.rid) return;
310
+ if (frame.kind === "progress") {
311
+ emitLifecycle(this.#options, BATTERY, this.#options.model, "generating", { progress: frame.progress });
312
+ return;
313
+ }
314
+ if (frame.kind === "image") {
315
+ request.pending += 1;
316
+ this.#readImage(frame.payload).then((output) => {
317
+ if (this.#request === request) request.outputs.push(output);
318
+ }).catch((error) => {
319
+ if (this.#request === request && !request.outcome) request.outcome = {
320
+ kind: "error",
321
+ error
322
+ };
323
+ }).finally(() => {
324
+ request.pending -= 1;
325
+ this.#maybeSettle(request);
326
+ });
327
+ return;
328
+ }
329
+ if (frame.kind === "error") {
330
+ if (!request.outcome) request.outcome = {
331
+ kind: "error",
332
+ error: new E_LOCAL_DIFFUSION_BACKEND_ERROR([frame.message])
333
+ };
334
+ this.#maybeSettle(request);
335
+ return;
336
+ }
337
+ if (frame.kind === "done") {
338
+ if (!request.outcome) request.outcome = { kind: "success" };
339
+ this.#maybeSettle(request);
340
+ }
341
+ }
342
+ /** Settle `request` once its recorded terminal outcome is set AND all image reads have drained. */
343
+ #maybeSettle(request) {
344
+ if (request.outcome && request.pending === 0) this.#settle(request, request.outcome);
345
+ }
346
+ async #readImage(payload) {
347
+ const maxDecoded = this.#options.maxDecodedBytes ?? DEFAULT_MAX_DECODED_BYTES;
348
+ let bytes;
349
+ let filename;
350
+ if (payload.b64 !== void 0) {
351
+ if (!/^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?$/.test(payload.b64)) throw new E_LOCAL_DIFFUSION_MALFORMED_FRAME(["invalid base64"]);
352
+ if (Math.floor(payload.b64.length * 3 / 4) > maxDecoded) throw new E_LOCAL_DIFFUSION_MALFORMED_FRAME(["inline image exceeds maxDecodedBytes"]);
353
+ bytes = decodeBase64(payload.b64);
354
+ } else if (payload.path !== void 0) {
355
+ const requestPath = payload.path;
356
+ const fs = this.#options.fs ?? await this.#defaultFs();
357
+ try {
358
+ bytes = await fs.readFile(requestPath);
359
+ } finally {
360
+ await this.#cleanupPath(fs, requestPath);
361
+ }
362
+ filename = requestPath.split(/[\\/]/).pop();
363
+ } else throw new E_LOCAL_DIFFUSION_MALFORMED_FRAME(["image has no data"]);
364
+ if (bytes.length === 0 || bytes.length > maxDecoded || !/^image\/(png|jpeg|jpg|webp|gif|avif)$/i.test(payload.mimeType)) throw new E_LOCAL_DIFFUSION_MALFORMED_FRAME(["invalid image output"]);
365
+ return {
366
+ kind: "image",
367
+ mimeType: payload.mimeType,
368
+ bytes,
369
+ ...filename ? { filename } : {}
370
+ };
371
+ }
372
+ /**
373
+ * Delete a backend-written result file ONLY when its LEXICALLY-resolved path is strictly inside
374
+ * `outputDir` (resolves `..`, rejects sibling-prefix escapes). Containment is lexical, not
375
+ * realpath-based: a symlink placed inside `outputDir` that points elsewhere is NOT followed-and-checked
376
+ * — this cleanup trusts that the consumer's own `outputDir` does not contain adversarial symlinks.
377
+ * Cleanup failure is diagnostic-only and never fails the generation.
378
+ */
379
+ async #cleanupPath(fs, filePath) {
380
+ const outputDir = this.#options.outputDir;
381
+ if (!outputDir) return;
382
+ try {
383
+ const path = await import("node:path");
384
+ if (!isContained(path.resolve(outputDir), path.resolve(filePath), path.sep)) return;
385
+ await fs.unlink(filePath);
386
+ } catch {}
387
+ }
388
+ async #defaultFs() {
389
+ const fs = await import("node:fs/promises");
390
+ return {
391
+ readFile: (path) => fs.readFile(path),
392
+ unlink: (path) => fs.unlink(path).then(() => void 0)
393
+ };
394
+ }
395
+ async #call(kind, prompt, inputs, opts) {
396
+ this.#assertLive();
397
+ if (this.#state === "running" || this.#state === "stopping") throw new E_LOCAL_DIFFUSION_BUSY();
398
+ await this.preload();
399
+ this.#assertLive();
400
+ if (this.#state !== "ready") throw new E_LOCAL_DIFFUSION_BUSY();
401
+ const o = this.#options;
402
+ const rid = this.#rid++;
403
+ const request = {
404
+ rid,
405
+ done: deferred(),
406
+ outputs: [],
407
+ pending: 0,
408
+ outcome: void 0,
409
+ settled: false,
410
+ terminated: false,
411
+ stopTimer: void 0,
412
+ requestTimer: void 0,
413
+ detachSignal: void 0
414
+ };
415
+ this.#request = request;
416
+ this.#state = "running";
417
+ emitLifecycle(o, BATTERY, o.model, "generating");
418
+ const args = {
419
+ prompt,
420
+ ...kind === "edit" ? { images: (inputs ?? []).map((input) => ({
421
+ b64: encodeBase64(input.bytes),
422
+ ...input.mimeType ? { mimeType: input.mimeType } : {}
423
+ })) } : {},
424
+ ...Object.fromEntries(Object.entries({
425
+ negativePrompt: opts?.negativePrompt ?? o.negativePrompt,
426
+ steps: opts?.steps ?? o.steps,
427
+ cfgScale: opts?.cfgScale ?? o.cfgScale,
428
+ sampler: opts?.sampler ?? o.sampler,
429
+ seed: opts?.seed ?? o.seed,
430
+ width: opts?.width ?? o.width,
431
+ height: opts?.height ?? o.height
432
+ }).filter(([, value]) => value !== void 0))
433
+ };
434
+ try {
435
+ this.#process?.stdin?.write(kind === "edit" ? buildEditCommand(rid, args, this.#protocol()) : buildGenerateCommand(rid, args, this.#protocol()));
436
+ } catch (error) {
437
+ this.#settle(request, {
438
+ kind: "error",
439
+ error: new E_LOCAL_DIFFUSION_BACKEND_ERROR([isError(error) ? error.message : String(error)])
440
+ });
441
+ return request.done.promise;
442
+ }
443
+ if (opts?.signal) {
444
+ const signal = opts.signal;
445
+ const abort = () => this.#beginStop(request, { kind: "aborted" });
446
+ if (signal.aborted) abort();
447
+ else {
448
+ signal.addEventListener("abort", abort, { once: true });
449
+ request.detachSignal = () => signal.removeEventListener("abort", abort);
450
+ }
451
+ }
452
+ if (o.requestTimeoutMs) request.requestTimer = setTimeout(() => this.#beginStop(request, {
453
+ kind: "error",
454
+ error: new E_LOCAL_DIFFUSION_REQUEST_TIMEOUT([o.requestTimeoutMs])
455
+ }), o.requestTimeoutMs);
456
+ return request.done.promise;
457
+ }
458
+ /**
459
+ * Begin a best-effort stop of `request` (abort or request-timeout): reject the caller now with the
460
+ * terminal `outcome`, but HOLD the single-flight slot in `stopping` (writing advisory `__stop__`)
461
+ * until the cancelled rid's terminal frame arrives OR `abortGraceMs` elapses → kill + respawn.
462
+ */
463
+ #beginStop(request, outcome) {
464
+ if (this.#request !== request || this.#state !== "running") return;
465
+ this.#state = "stopping";
466
+ try {
467
+ this.#process?.stdin?.write(buildStopCommand(request.rid, this.#protocol()));
468
+ } catch {}
469
+ if (!request.outcome) request.outcome = outcome;
470
+ if (!request.settled) request.done.reject(outcomeError(outcome));
471
+ request.settled = true;
472
+ request.detachSignal?.();
473
+ request.detachSignal = void 0;
474
+ if (request.requestTimer) clearTimeout(request.requestTimer);
475
+ request.requestTimer = void 0;
476
+ request.stopTimer = setTimeout(() => {
477
+ if (this.#request === request) this.reset();
478
+ }, this.#options.abortGraceMs ?? 5e3);
479
+ }
480
+ /**
481
+ * Fully settle `request` exactly once: clear its timers, release the single-flight slot (only if it
482
+ * is still the current request), settle the caller's promise if not already done, and emit exactly
483
+ * ONE terminal lifecycle event. Idempotent — a second call (e.g. a drained pending read after an
484
+ * earlier retirement) is a no-op, so no duplicate lifecycle/promise settlement can occur.
485
+ */
486
+ #settle(request, outcome) {
487
+ if (request.terminated) return;
488
+ request.terminated = true;
489
+ if (request.stopTimer) clearTimeout(request.stopTimer);
490
+ request.stopTimer = void 0;
491
+ if (request.requestTimer) clearTimeout(request.requestTimer);
492
+ request.requestTimer = void 0;
493
+ request.detachSignal?.();
494
+ request.detachSignal = void 0;
495
+ if (this.#request === request) {
496
+ this.#request = void 0;
497
+ if (this.#state === "running" || this.#state === "stopping") this.#state = "ready";
498
+ }
499
+ if (!request.settled) {
500
+ request.settled = true;
501
+ if (outcome.kind === "success") request.done.resolve(request.outputs);
502
+ else request.done.reject(outcomeError(outcome));
503
+ }
504
+ if (outcome.kind === "success") emitLifecycle(this.#options, BATTERY, this.#options.model, "complete");
505
+ else emitLifecycle(this.#options, BATTERY, this.#options.model, "error", { error: outcomeError(outcome) });
506
+ }
507
+ #detach() {
508
+ for (const listener of this.#listeners) listener.target.off(listener.event, listener.fn);
509
+ this.#listeners = [];
510
+ }
511
+ };
512
+ //#endregion
513
+ export { LocalDiffusionGenerationAdapter };
514
+
515
+ //# sourceMappingURL=adapter.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"adapter.mjs","names":["#options","#assertLive","#state","#loadPromise","#start","#loadReject","#retire","#disposePromise","#dispose","#process","#protocol","#call","#epoch","#detach","#request","#settle","#onFrame","#listeners","#maybeSettle","#readImage","#defaultFs","#cleanupPath","#rid","#beginStop"],"sources":["../../../../src/batteries/generation/local_diffusion/adapter.ts"],"sourcesContent":["/** Node-only local diffusion generation adapter.\n * @module @nhtio/adk/batteries/generation/local_diffusion/adapter\n */\nimport { toBytes } from '../_shared'\nimport { isError } from '@nhtio/adk/guards'\nimport { validateOptions } from './validation'\nimport { emitLifecycle } from '../../llm/chat_common/lifecycle'\nimport { decodeBase64, encodeBase64 } from '../../../lib/helpers/base64'\nimport {\n DEFAULT_PROTOCOL,\n buildEditCommand,\n buildGenerateCommand,\n buildShutdownCommand,\n buildStopCommand,\n createFrameReader,\n} from './protocol'\nimport {\n E_LOCAL_DIFFUSION_ABORTED,\n E_LOCAL_DIFFUSION_BACKEND_ERROR,\n E_LOCAL_DIFFUSION_BUSY,\n E_LOCAL_DIFFUSION_DISPOSED,\n E_LOCAL_DIFFUSION_MALFORMED_FRAME,\n E_LOCAL_DIFFUSION_REQUEST_TIMEOUT,\n E_LOCAL_DIFFUSION_STARTUP_TIMEOUT,\n} from './exceptions'\nimport type { GenerationImageInput } from '../_shared'\nimport type { GeneratedMediaOutput } from '../openai/types'\nimport type { ParsedFrame, ProtocolConfig } from './protocol'\nimport type {\n DiffusionBackendProcess,\n DiffusionFsLike,\n LocalDiffusionCallOptions,\n LocalDiffusionGenerationAdapterOptions,\n} from './types'\n\nconst BATTERY = 'local_diffusion_generation' as const\nconst DEFAULT_MAX_DECODED_BYTES = 50 * 1024 * 1024\n\ntype Deferred<T> = { promise: Promise<T>; resolve(value: T): void; reject(error: unknown): void }\n\n/** How a request ended, so {@link LocalDiffusionGenerationAdapter} settles it exactly once. */\ntype Outcome = { kind: 'success' } | { kind: 'error'; error: unknown } | { kind: 'aborted' }\n\n/** The rejection error for a non-success outcome (an aborted outcome maps to the typed abort error). */\nconst outcomeError = (outcome: Exclude<Outcome, { kind: 'success' }>): unknown =>\n outcome.kind === 'aborted' ? new E_LOCAL_DIFFUSION_ABORTED() : outcome.error\n\n/** Mutable state for the single in-flight request. */\ntype RequestState = {\n /** The monotonic request id fenced against backend frames. */\n rid: number\n /** The caller-facing settlement. */\n done: Deferred<GeneratedMediaOutput[]>\n /** Accumulated image outputs. */\n outputs: GeneratedMediaOutput[]\n /** Count of in-flight image reads not yet settled. */\n pending: number\n /** The recorded terminal outcome, set once; the request settles when `pending` reaches 0. */\n outcome: Outcome | undefined\n /** Whether the caller's promise has been settled (may precede full termination on abort/timeout). */\n settled: boolean\n /** Whether full terminal settlement (slot release + one lifecycle event) has already run. */\n terminated: boolean\n /** The abort/timeout grace timer holding the slot in `stopping`, if any. */\n stopTimer: ReturnType<typeof setTimeout> | undefined\n /** The request-timeout timer, if any. */\n requestTimer: ReturnType<typeof setTimeout> | undefined\n /** Detaches the caller's abort listener, if any. */\n detachSignal: (() => void) | undefined\n}\n\nconst deferred = <T>(): Deferred<T> => {\n let resolve!: (value: T) => void\n let reject!: (error: unknown) => void\n const promise = new Promise<T>((res, rej) => {\n resolve = res\n reject = rej\n })\n return { promise, resolve, reject }\n}\n\n/**\n * Whether `candidate` resolves to a location strictly inside `dir` — real containment (resolves `..`\n * and rejects sibling-prefix escapes like `/output2`), not a raw string `startsWith`.\n */\nconst isContained = (dir: string, candidate: string, sep: string): boolean => {\n const base = dir.endsWith(sep) ? dir.slice(0, -sep.length) : dir\n return candidate === base || candidate.startsWith(base + sep)\n}\n\n/** Local diffusion adapter backed by a line-protocol subprocess. */\nexport class LocalDiffusionGenerationAdapter {\n readonly #options: LocalDiffusionGenerationAdapterOptions\n #process: DiffusionBackendProcess | undefined\n #epoch = 0\n #state: 'idle' | 'starting' | 'ready' | 'running' | 'stopping' | 'disposed' = 'idle'\n #loadPromise: Promise<void> | undefined\n #loadReject: ((error: unknown) => void) | undefined\n #request: RequestState | undefined\n #rid = 0\n #disposePromise: Promise<void> | undefined\n #listeners: Array<{\n target: { off(event: string, listener: (...args: any[]) => void): void }\n event: string\n fn: (...args: any[]) => void\n }> = []\n\n /** Whether the current host exposes Node's process runtime. */\n public static isAvailable(): boolean {\n return typeof process !== 'undefined' && Boolean(process.versions?.node)\n }\n\n /** Construct and validate an adapter. */\n constructor(options: unknown) {\n this.#options = validateOptions(options)\n }\n\n /** Whether this instance can run, honoring its injected probe. */\n public isAvailable(): boolean {\n return (this.#options.isAvailable ?? LocalDiffusionGenerationAdapter.isAvailable)()\n }\n\n /** Start the backend and wait for its ready event. */\n public async preload(): Promise<void> {\n this.#assertLive()\n if (this.#state === 'ready' || this.#state === 'running' || this.#state === 'stopping') return\n if (this.#loadPromise) return this.#loadPromise\n const loadPromise = this.#start()\n this.#loadPromise = loadPromise\n loadPromise\n .catch(() => undefined)\n .finally(() => {\n // Only clear if this exact load is still the current one (a reset/dispose may have replaced it).\n if (this.#loadPromise === loadPromise) {\n this.#loadPromise = undefined\n this.#loadReject = undefined\n }\n })\n return loadPromise\n }\n\n /** Synchronously terminate the current process and invalidate all continuations. */\n public reset(): void {\n if (this.#state === 'disposed') return\n this.#retire(new E_LOCAL_DIFFUSION_BACKEND_ERROR(['adapter reset']))\n this.#state = 'idle'\n }\n\n /** Gracefully stop the backend, then force terminate it. Terminal and idempotent. */\n public dispose(): Promise<void> {\n if (this.#disposePromise) return this.#disposePromise\n this.#disposePromise = this.#dispose()\n return this.#disposePromise\n }\n\n async #dispose(): Promise<void> {\n const process = this.#process\n // Move to the terminal state SYNCHRONOUSLY so any concurrent call sees `disposed` (not `ready`)\n // and every in-flight continuation is fenced by the epoch bump inside #retire. Defer the kill:\n // the two-phase teardown sends `__shutdown__`, waits `disposeGraceMs`, then force-kills once below.\n this.#retire(new E_LOCAL_DIFFUSION_DISPOSED(), false)\n this.#state = 'disposed'\n if (process && process.stdin) {\n try {\n process.stdin.write(buildShutdownCommand(this.#protocol()))\n } catch {\n /* teardown — the process may already be gone */\n }\n await new Promise<void>((resolve) => {\n const timer = setTimeout(resolve, this.#options.disposeGraceMs ?? 5000)\n const finish = (): void => {\n clearTimeout(timer)\n process.off('close', finish)\n resolve()\n }\n process.on('close', finish)\n })\n }\n process?.kill()\n }\n\n /** Async-disposal protocol alias. */\n public async [Symbol.asyncDispose](): Promise<void> {\n await this.dispose()\n }\n\n /** Generate images from text. */\n public async generate(\n prompt: string,\n opts?: LocalDiffusionCallOptions\n ): Promise<GeneratedMediaOutput[]> {\n return this.#call('generate', prompt, undefined, opts)\n }\n\n /** Edit images with text guidance. */\n public async edit(\n inputs: GenerationImageInput | GenerationImageInput[],\n prompt: string,\n opts?: LocalDiffusionCallOptions\n ): Promise<GeneratedMediaOutput[]> {\n const normalized = await Promise.all((Array.isArray(inputs) ? inputs : [inputs]).map(toBytes))\n return this.#call('edit', prompt, normalized, opts)\n }\n\n #assertLive(): void {\n if (this.#state === 'disposed' || this.#disposePromise) throw new E_LOCAL_DIFFUSION_DISPOSED()\n }\n\n #protocol(): ProtocolConfig {\n const o = this.#options\n return {\n ...DEFAULT_PROTOCOL,\n ...(o.protocol ?? {}),\n commandPrefix: o.commandPrefix ?? o.protocol?.commandPrefix ?? DEFAULT_PROTOCOL.commandPrefix,\n eventPrefix: o.eventPrefix ?? o.protocol?.eventPrefix ?? DEFAULT_PROTOCOL.eventPrefix,\n ops: { ...DEFAULT_PROTOCOL.ops, ...o.protocol?.ops, ...o.ops },\n control: { ...DEFAULT_PROTOCOL.control, ...o.protocol?.control, ...o.control },\n events: { ...DEFAULT_PROTOCOL.events, ...o.protocol?.events, ...o.events },\n }\n }\n\n /**\n * Tear down the current process + request atomically: bump the epoch (fencing every outstanding\n * continuation), detach listeners, reject an in-flight preload, record the request's terminal\n * outcome (which settles once its pending reads drain), and drop cached handles. Kills the child\n * unless `kill` is false — `dispose()` defers the kill until after its graceful-shutdown wait.\n * Leaves `#state` for the caller to set (`idle` for reset, `disposed` for dispose).\n */\n #retire(error: unknown, kill = true): void {\n this.#epoch += 1\n this.#detach()\n this.#loadReject?.(error)\n this.#loadReject = undefined\n this.#loadPromise = undefined\n const request = this.#request\n if (request) this.#settle(request, { kind: 'error', error })\n if (kill) this.#process?.kill()\n this.#process = undefined\n }\n\n async #start(): Promise<void> {\n this.#assertLive()\n if (!this.isAvailable())\n throw new E_LOCAL_DIFFUSION_BACKEND_ERROR(['Node process is unavailable'])\n const epoch = ++this.#epoch\n const startupTimeoutMs = this.#options.startupTimeoutMs ?? 30_000\n this.#state = 'starting'\n const spawner =\n this.#options.spawn ??\n (async (ctx): Promise<DiffusionBackendProcess> => {\n const { spawn } = await import('node:child_process')\n const child = spawn(ctx.command, ctx.args, {\n stdio: ['pipe', 'pipe', 'pipe'],\n }) as unknown as DiffusionBackendProcess\n if (!child.stdin || !child.stdout)\n throw new E_LOCAL_DIFFUSION_BACKEND_ERROR(['backend stdin/stdout unavailable'])\n return child\n })\n let process: DiffusionBackendProcess\n try {\n process = await spawner({\n command: this.#options.command,\n args: this.#options.args ?? [],\n model: this.#options.model,\n })\n } catch (error) {\n throw new E_LOCAL_DIFFUSION_BACKEND_ERROR([isError(error) ? error.message : String(error)])\n }\n if (epoch !== this.#epoch || (this.#state as string) === 'disposed') {\n process.kill()\n throw new E_LOCAL_DIFFUSION_BACKEND_ERROR(['startup superseded'])\n }\n if (!process.stdin || !process.stdout)\n throw new E_LOCAL_DIFFUSION_BACKEND_ERROR(['backend stdin/stdout unavailable'])\n this.#process = process\n const wait = deferred<void>()\n this.#loadReject = wait.reject\n const reader = createFrameReader({\n maxLineBytes: this.#options.maxLineBytes,\n config: this.#protocol(),\n onFrame: (frame) => this.#onFrame(frame, epoch, wait),\n })\n const onData = (chunk: Uint8Array): void => {\n if (epoch === this.#epoch) reader.push(chunk)\n }\n const onStreamEnd = (): void => {\n if (epoch !== this.#epoch) return\n // Flush any trailing partial line, THEN treat the closed stdout as a terminal transport failure:\n // a backend that closes its stdout can produce no further frames, so a startup or in-flight\n // request would otherwise hang forever (permanent BUSY). failure() retires + settles it.\n reader.end()\n failure('backend stdout closed', null, null)\n }\n const failure = (detail: string, code: number | null, signal: NodeJS.Signals | null): void => {\n if (epoch !== this.#epoch) return\n const error =\n this.#state === 'starting'\n ? new E_LOCAL_DIFFUSION_STARTUP_TIMEOUT([startupTimeoutMs])\n : new E_LOCAL_DIFFUSION_BACKEND_ERROR([detail, code ?? undefined, signal ?? undefined])\n // Fully retire the process so no stale `#request`/`#process`/`ready` survives. Detach + kill the\n // child (a startup-timeout child may still be alive) and bump the epoch so any late frame from\n // it — including `ready` — is fenced out.\n wait.reject(error)\n this.#detach()\n this.#loadReject = undefined\n this.#loadPromise = undefined\n const request = this.#request\n if (request) this.#settle(request, { kind: 'error', error })\n this.#process?.kill()\n this.#process = undefined\n this.#epoch += 1\n this.#state = 'idle'\n }\n const onError = (): void => failure('backend error', null, null)\n const onExit = (code: number | null, signal: NodeJS.Signals | null): void =>\n failure('backend exited', code, signal)\n const onStdoutError = (): void => failure('backend stdout error', null, null)\n process.stdout.on('data', onData)\n process.stdout.on('end', onStreamEnd)\n process.stdout.on('error', onStdoutError)\n process.on('error', onError)\n process.on('exit', onExit)\n process.on('close', onExit)\n this.#listeners.push(\n { target: process.stdout, event: 'data', fn: onData },\n { target: process.stdout, event: 'end', fn: onStreamEnd },\n { target: process.stdout, event: 'error', fn: onStdoutError },\n { target: process, event: 'error', fn: onError },\n { target: process, event: 'exit', fn: onExit },\n { target: process, event: 'close', fn: onExit }\n )\n await new Promise<void>((resolve, reject) => {\n const timer = setTimeout(() => {\n // Route the timeout through failure() so the still-running child is killed + detached + fenced\n // (state → idle, epoch bumped) — a late `ready` from the timed-out child cannot revive it.\n failure('startup timed out', null, null)\n }, startupTimeoutMs)\n wait.promise.then(\n () => {\n clearTimeout(timer)\n resolve()\n },\n (error) => {\n clearTimeout(timer)\n reject(error)\n }\n )\n })\n if (epoch !== this.#epoch) throw new E_LOCAL_DIFFUSION_BACKEND_ERROR(['startup superseded'])\n }\n\n #onFrame(frame: ParsedFrame, epoch: number, load: Deferred<void>): void {\n if (epoch !== this.#epoch) return\n if (frame.kind === 'modelLoad') {\n emitLifecycle(this.#options, BATTERY, this.#options.model, 'loading', {\n progress: frame.progress,\n })\n return\n }\n if (frame.kind === 'ready') {\n // Only a pending startup transitions to ready; a duplicate/unsolicited `ready` during\n // running/stopping must not reopen the slot or overwrite the active request.\n if (this.#state !== 'starting') return\n this.#state = 'ready'\n emitLifecycle(this.#options, BATTERY, this.#options.model, 'ready')\n load.resolve(undefined)\n return\n }\n const request = this.#request\n if (frame.kind === 'malformed' || frame.kind === 'protocolError') {\n // Record the terminal outcome but hold the slot until pending image reads drain (like `done`).\n if (request) {\n if (!request.outcome)\n request.outcome = {\n kind: 'error',\n error: new E_LOCAL_DIFFUSION_MALFORMED_FRAME([frame.detail]),\n }\n this.#maybeSettle(request)\n }\n return\n }\n // Every remaining frame kind carries a rid; fence it against the current request.\n if (!request || !('rid' in frame) || frame.rid !== request.rid) return\n if (frame.kind === 'progress') {\n emitLifecycle(this.#options, BATTERY, this.#options.model, 'generating', {\n progress: frame.progress,\n })\n return\n }\n if (frame.kind === 'image') {\n request.pending += 1\n void this.#readImage(frame.payload)\n .then((output) => {\n if (this.#request === request) request.outputs.push(output)\n })\n .catch((error) => {\n // Fence against a retired/superseded request: only the current request records an outcome.\n if (this.#request === request && !request.outcome)\n request.outcome = { kind: 'error', error }\n })\n .finally(() => {\n request.pending -= 1\n this.#maybeSettle(request)\n })\n return\n }\n if (frame.kind === 'error') {\n if (!request.outcome)\n request.outcome = {\n kind: 'error',\n error: new E_LOCAL_DIFFUSION_BACKEND_ERROR([frame.message]),\n }\n this.#maybeSettle(request)\n return\n }\n if (frame.kind === 'done') {\n if (!request.outcome) request.outcome = { kind: 'success' }\n this.#maybeSettle(request)\n }\n }\n\n /** Settle `request` once its recorded terminal outcome is set AND all image reads have drained. */\n #maybeSettle(request: RequestState): void {\n if (request.outcome && request.pending === 0) this.#settle(request, request.outcome)\n }\n\n async #readImage(payload: {\n path?: string\n b64?: string\n mimeType: string\n }): Promise<GeneratedMediaOutput> {\n const maxDecoded = this.#options.maxDecodedBytes ?? DEFAULT_MAX_DECODED_BYTES\n let bytes: Uint8Array\n let filename: string | undefined\n if (payload.b64 !== undefined) {\n if (!/^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?$/.test(payload.b64))\n throw new E_LOCAL_DIFFUSION_MALFORMED_FRAME(['invalid base64'])\n // Bound memory BEFORE allocating: decoded length ≈ 3/4 of the base64 char count.\n const approxDecoded = Math.floor((payload.b64.length * 3) / 4)\n if (approxDecoded > maxDecoded)\n throw new E_LOCAL_DIFFUSION_MALFORMED_FRAME(['inline image exceeds maxDecodedBytes'])\n bytes = decodeBase64(payload.b64)\n } else if (payload.path !== undefined) {\n const requestPath = payload.path\n const fs = this.#options.fs ?? (await this.#defaultFs())\n try {\n bytes = await fs.readFile(requestPath)\n } finally {\n await this.#cleanupPath(fs, requestPath)\n }\n filename = requestPath.split(/[\\\\/]/).pop()\n } else throw new E_LOCAL_DIFFUSION_MALFORMED_FRAME(['image has no data'])\n if (\n bytes.length === 0 ||\n bytes.length > maxDecoded ||\n !/^image\\/(png|jpeg|jpg|webp|gif|avif)$/i.test(payload.mimeType)\n )\n throw new E_LOCAL_DIFFUSION_MALFORMED_FRAME(['invalid image output'])\n return { kind: 'image', mimeType: payload.mimeType, bytes, ...(filename ? { filename } : {}) }\n }\n\n /**\n * Delete a backend-written result file ONLY when its LEXICALLY-resolved path is strictly inside\n * `outputDir` (resolves `..`, rejects sibling-prefix escapes). Containment is lexical, not\n * realpath-based: a symlink placed inside `outputDir` that points elsewhere is NOT followed-and-checked\n * — this cleanup trusts that the consumer's own `outputDir` does not contain adversarial symlinks.\n * Cleanup failure is diagnostic-only and never fails the generation.\n */\n async #cleanupPath(fs: DiffusionFsLike, filePath: string): Promise<void> {\n const outputDir = this.#options.outputDir\n if (!outputDir) return\n try {\n const path = await import('node:path')\n const resolvedDir = path.resolve(outputDir)\n const resolvedPath = path.resolve(filePath)\n if (!isContained(resolvedDir, resolvedPath, path.sep)) return\n await fs.unlink(filePath)\n } catch {\n /* diagnostic-only cleanup — a failed unlink never fails the generation */\n }\n }\n\n async #defaultFs(): Promise<DiffusionFsLike> {\n const fs = await import('node:fs/promises')\n return {\n readFile: (path) => fs.readFile(path),\n unlink: (path) => fs.unlink(path).then(() => undefined),\n }\n }\n\n async #call(\n kind: 'generate' | 'edit',\n prompt: string,\n inputs?: Array<{ bytes: Uint8Array; mimeType?: string }>,\n opts?: LocalDiffusionCallOptions\n ): Promise<GeneratedMediaOutput[]> {\n this.#assertLive()\n if (this.#state === 'running' || this.#state === 'stopping') throw new E_LOCAL_DIFFUSION_BUSY()\n await this.preload()\n this.#assertLive()\n if (this.#state !== 'ready') throw new E_LOCAL_DIFFUSION_BUSY()\n const o = this.#options\n const rid = this.#rid++\n const request: RequestState = {\n rid,\n done: deferred<GeneratedMediaOutput[]>(),\n outputs: [],\n pending: 0,\n outcome: undefined,\n settled: false,\n terminated: false,\n stopTimer: undefined,\n requestTimer: undefined,\n detachSignal: undefined,\n }\n this.#request = request\n this.#state = 'running'\n emitLifecycle(o, BATTERY, o.model, 'generating')\n const args: Record<string, unknown> = {\n prompt,\n ...(kind === 'edit'\n ? {\n images: (inputs ?? []).map((input) => ({\n b64: encodeBase64(input.bytes),\n ...(input.mimeType ? { mimeType: input.mimeType } : {}),\n })),\n }\n : {}),\n ...Object.fromEntries(\n Object.entries({\n negativePrompt: opts?.negativePrompt ?? o.negativePrompt,\n steps: opts?.steps ?? o.steps,\n cfgScale: opts?.cfgScale ?? o.cfgScale,\n sampler: opts?.sampler ?? o.sampler,\n seed: opts?.seed ?? o.seed,\n width: opts?.width ?? o.width,\n height: opts?.height ?? o.height,\n }).filter(([, value]) => value !== undefined)\n ),\n }\n try {\n this.#process?.stdin?.write(\n kind === 'edit'\n ? buildEditCommand(rid, args, this.#protocol())\n : buildGenerateCommand(rid, args, this.#protocol())\n )\n } catch (error) {\n this.#settle(request, {\n kind: 'error',\n error: new E_LOCAL_DIFFUSION_BACKEND_ERROR([\n isError(error) ? error.message : String(error),\n ]),\n })\n return request.done.promise\n }\n if (opts?.signal) {\n const signal = opts.signal\n const abort = (): void => this.#beginStop(request, { kind: 'aborted' })\n if (signal.aborted) abort()\n else {\n signal.addEventListener('abort', abort, { once: true })\n request.detachSignal = () => signal.removeEventListener('abort', abort)\n }\n }\n if (o.requestTimeoutMs)\n request.requestTimer = setTimeout(\n () =>\n this.#beginStop(request, {\n kind: 'error',\n error: new E_LOCAL_DIFFUSION_REQUEST_TIMEOUT([o.requestTimeoutMs as number]),\n }),\n o.requestTimeoutMs\n )\n return request.done.promise\n }\n\n /**\n * Begin a best-effort stop of `request` (abort or request-timeout): reject the caller now with the\n * terminal `outcome`, but HOLD the single-flight slot in `stopping` (writing advisory `__stop__`)\n * until the cancelled rid's terminal frame arrives OR `abortGraceMs` elapses → kill + respawn.\n */\n #beginStop(request: RequestState, outcome: Exclude<Outcome, { kind: 'success' }>): void {\n if (this.#request !== request || this.#state !== 'running') return\n this.#state = 'stopping'\n try {\n this.#process?.stdin?.write(buildStopCommand(request.rid, this.#protocol()))\n } catch {\n /* advisory — the backend may ignore or never receive __stop__ */\n }\n // Record the terminal outcome + reject the caller immediately, but keep the slot held: the request\n // is not cleared until its reads drain (settle) or the grace timer fires.\n if (!request.outcome) request.outcome = outcome\n if (!request.settled) request.done.reject(outcomeError(outcome))\n request.settled = true\n request.detachSignal?.()\n request.detachSignal = undefined\n if (request.requestTimer) clearTimeout(request.requestTimer)\n request.requestTimer = undefined\n request.stopTimer = setTimeout(() => {\n if (this.#request === request) this.reset()\n }, this.#options.abortGraceMs ?? 5000)\n }\n\n /**\n * Fully settle `request` exactly once: clear its timers, release the single-flight slot (only if it\n * is still the current request), settle the caller's promise if not already done, and emit exactly\n * ONE terminal lifecycle event. Idempotent — a second call (e.g. a drained pending read after an\n * earlier retirement) is a no-op, so no duplicate lifecycle/promise settlement can occur.\n */\n #settle(request: RequestState, outcome: Outcome): void {\n if (request.terminated) return\n request.terminated = true\n if (request.stopTimer) clearTimeout(request.stopTimer)\n request.stopTimer = undefined\n if (request.requestTimer) clearTimeout(request.requestTimer)\n request.requestTimer = undefined\n request.detachSignal?.()\n request.detachSignal = undefined\n if (this.#request === request) {\n this.#request = undefined\n if (this.#state === 'running' || this.#state === 'stopping') this.#state = 'ready'\n }\n if (!request.settled) {\n request.settled = true\n if (outcome.kind === 'success') request.done.resolve(request.outputs)\n else request.done.reject(outcomeError(outcome))\n }\n // The lifecycle event mirrors the true outcome, never a spurious `complete` after a failure/abort.\n if (outcome.kind === 'success')\n emitLifecycle(this.#options, BATTERY, this.#options.model, 'complete')\n else\n emitLifecycle(this.#options, BATTERY, this.#options.model, 'error', {\n error: outcomeError(outcome),\n })\n }\n\n #detach(): void {\n for (const listener of this.#listeners) listener.target.off(listener.event, listener.fn)\n this.#listeners = []\n }\n}\n"],"mappings":";;;;;;;;;;;;AAmCA,IAAM,UAAU;AAChB,IAAM,4BAA4B,KAAK,OAAO;;AAQ9C,IAAM,gBAAgB,YACpB,QAAQ,SAAS,YAAY,IAAI,0BAA0B,IAAI,QAAQ;AA0BzE,IAAM,iBAAiC;CACrC,IAAI;CACJ,IAAI;CAKJ,OAAO;EAAE,SAAA,IAJW,SAAY,KAAK,QAAQ;GAC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;EACvB,QAAQ,YAAY,iBAAiB;GACnC,IAAI,KAAKI,aAAa,SAAS,KAAK,MAAM;EAC5C,GAAG,KAAKd,SAAS,gBAAgB,GAAI;CACvC;;;;;;;CAQA,QAAQ,SAAuB,SAAwB;EACrD,IAAI,QAAQ,YAAY;EACxB,QAAQ,aAAa;EACrB,IAAI,QAAQ,WAAW,aAAa,QAAQ,SAAS;EACrD,QAAQ,YAAY,KAAA;EACpB,IAAI,QAAQ,cAAc,aAAa,QAAQ,YAAY;EAC3D,QAAQ,eAAe,KAAA;EACvB,QAAQ,eAAe;EACvB,QAAQ,eAAe,KAAA;EACvB,IAAI,KAAKc,aAAa,SAAS;GAC7B,KAAKA,WAAW,KAAA;GAChB,IAAI,KAAKZ,WAAW,aAAa,KAAKA,WAAW,YAAY,KAAKA,SAAS;EAC7E;EACA,IAAI,CAAC,QAAQ,SAAS;GACpB,QAAQ,UAAU;GAClB,IAAI,QAAQ,SAAS,WAAW,QAAQ,KAAK,QAAQ,QAAQ,OAAO;QAC/D,QAAQ,KAAK,OAAO,aAAa,OAAO,CAAC;EAChD;EAEA,IAAI,QAAQ,SAAS,WACnB,cAAc,KAAKF,UAAU,SAAS,KAAKA,SAAS,OAAO,UAAU;OAErE,cAAc,KAAKA,UAAU,SAAS,KAAKA,SAAS,OAAO,SAAS,EAClE,OAAO,aAAa,OAAO,EAC7B,CAAC;CACL;CAEA,UAAgB;EACd,KAAK,MAAM,YAAY,KAAKiB,YAAY,SAAS,OAAO,IAAI,SAAS,OAAO,SAAS,EAAE;EACvF,KAAKA,aAAa,CAAC;CACrB;AACF"}
@@ -0,0 +1,35 @@
1
+ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
2
+ require("../../../chunk-Cek0wNdY.js");
3
+ const require_exceptions = require("../../../exceptions-BfOYct3J.js");
4
+ require("../../../factories.cjs");
5
+ //#region src/batteries/generation/local_diffusion/exceptions.ts
6
+ /** Local diffusion battery exceptions.
7
+ * @module @nhtio/adk/batteries/generation/local_diffusion/exceptions
8
+ */
9
+ /** Invalid adapter configuration. */
10
+ var E_INVALID_LOCAL_DIFFUSION_OPTIONS = require_exceptions.createException("E_INVALID_LOCAL_DIFFUSION_OPTIONS", "Invalid local diffusion options: %s", "E_INVALID_LOCAL_DIFFUSION_OPTIONS", 529, true);
11
+ /** Backend process failure. Optional printf args: `[detail, exitCode?, signal?]`. */
12
+ var E_LOCAL_DIFFUSION_BACKEND_ERROR = require_exceptions.createException("E_LOCAL_DIFFUSION_BACKEND_ERROR", "Local diffusion backend error: %s (exit %s, signal %s)", "E_LOCAL_DIFFUSION_BACKEND_ERROR", 502, false);
13
+ /** Startup exceeded its deadline. */
14
+ var E_LOCAL_DIFFUSION_STARTUP_TIMEOUT = require_exceptions.createException("E_LOCAL_DIFFUSION_STARTUP_TIMEOUT", "Local diffusion startup timed out after %dms", "E_LOCAL_DIFFUSION_STARTUP_TIMEOUT", 504, false);
15
+ /** Request exceeded its deadline. */
16
+ var E_LOCAL_DIFFUSION_REQUEST_TIMEOUT = require_exceptions.createException("E_LOCAL_DIFFUSION_REQUEST_TIMEOUT", "Local diffusion request timed out after %dms", "E_LOCAL_DIFFUSION_REQUEST_TIMEOUT", 504, false);
17
+ /** Request was cancelled. */
18
+ var E_LOCAL_DIFFUSION_ABORTED = require_exceptions.createException("E_LOCAL_DIFFUSION_ABORTED", "Local diffusion request aborted", "E_LOCAL_DIFFUSION_ABORTED", 499, false);
19
+ /** Backend emitted malformed protocol data. */
20
+ var E_LOCAL_DIFFUSION_MALFORMED_FRAME = require_exceptions.createException("E_LOCAL_DIFFUSION_MALFORMED_FRAME", "Malformed local diffusion frame: %s", "E_LOCAL_DIFFUSION_MALFORMED_FRAME", 502, false);
21
+ /** Another request is active. */
22
+ var E_LOCAL_DIFFUSION_BUSY = require_exceptions.createException("E_LOCAL_DIFFUSION_BUSY", "Local diffusion backend is busy", "E_LOCAL_DIFFUSION_BUSY", 409, false);
23
+ /** Adapter has been disposed. */
24
+ var E_LOCAL_DIFFUSION_DISPOSED = require_exceptions.createException("E_LOCAL_DIFFUSION_DISPOSED", "Local diffusion adapter is disposed", "E_LOCAL_DIFFUSION_DISPOSED", 409, false);
25
+ //#endregion
26
+ exports.E_INVALID_LOCAL_DIFFUSION_OPTIONS = E_INVALID_LOCAL_DIFFUSION_OPTIONS;
27
+ exports.E_LOCAL_DIFFUSION_ABORTED = E_LOCAL_DIFFUSION_ABORTED;
28
+ exports.E_LOCAL_DIFFUSION_BACKEND_ERROR = E_LOCAL_DIFFUSION_BACKEND_ERROR;
29
+ exports.E_LOCAL_DIFFUSION_BUSY = E_LOCAL_DIFFUSION_BUSY;
30
+ exports.E_LOCAL_DIFFUSION_DISPOSED = E_LOCAL_DIFFUSION_DISPOSED;
31
+ exports.E_LOCAL_DIFFUSION_MALFORMED_FRAME = E_LOCAL_DIFFUSION_MALFORMED_FRAME;
32
+ exports.E_LOCAL_DIFFUSION_REQUEST_TIMEOUT = E_LOCAL_DIFFUSION_REQUEST_TIMEOUT;
33
+ exports.E_LOCAL_DIFFUSION_STARTUP_TIMEOUT = E_LOCAL_DIFFUSION_STARTUP_TIMEOUT;
34
+
35
+ //# sourceMappingURL=exceptions.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"exceptions.cjs","names":[],"sources":["../../../../src/batteries/generation/local_diffusion/exceptions.ts"],"sourcesContent":["/** Local diffusion battery exceptions.\n * @module @nhtio/adk/batteries/generation/local_diffusion/exceptions\n */\nimport { createException } from '@nhtio/adk/factories'\n\n/** Invalid adapter configuration. */\nexport const E_INVALID_LOCAL_DIFFUSION_OPTIONS = createException<[string]>(\n 'E_INVALID_LOCAL_DIFFUSION_OPTIONS',\n 'Invalid local diffusion options: %s',\n 'E_INVALID_LOCAL_DIFFUSION_OPTIONS',\n 529,\n true\n)\n/** Backend process failure. Optional printf args: `[detail, exitCode?, signal?]`. */\nexport const E_LOCAL_DIFFUSION_BACKEND_ERROR = createException<[string, number?, string?]>(\n 'E_LOCAL_DIFFUSION_BACKEND_ERROR',\n 'Local diffusion backend error: %s (exit %s, signal %s)',\n 'E_LOCAL_DIFFUSION_BACKEND_ERROR',\n 502,\n false\n)\n/** Startup exceeded its deadline. */\nexport const E_LOCAL_DIFFUSION_STARTUP_TIMEOUT = createException<[number]>(\n 'E_LOCAL_DIFFUSION_STARTUP_TIMEOUT',\n 'Local diffusion startup timed out after %dms',\n 'E_LOCAL_DIFFUSION_STARTUP_TIMEOUT',\n 504,\n false\n)\n/** Request exceeded its deadline. */\nexport const E_LOCAL_DIFFUSION_REQUEST_TIMEOUT = createException<[number]>(\n 'E_LOCAL_DIFFUSION_REQUEST_TIMEOUT',\n 'Local diffusion request timed out after %dms',\n 'E_LOCAL_DIFFUSION_REQUEST_TIMEOUT',\n 504,\n false\n)\n/** Request was cancelled. */\nexport const E_LOCAL_DIFFUSION_ABORTED = createException<[]>(\n 'E_LOCAL_DIFFUSION_ABORTED',\n 'Local diffusion request aborted',\n 'E_LOCAL_DIFFUSION_ABORTED',\n 499,\n false\n)\n/** Backend emitted malformed protocol data. */\nexport const E_LOCAL_DIFFUSION_MALFORMED_FRAME = createException<[string]>(\n 'E_LOCAL_DIFFUSION_MALFORMED_FRAME',\n 'Malformed local diffusion frame: %s',\n 'E_LOCAL_DIFFUSION_MALFORMED_FRAME',\n 502,\n false\n)\n/** Another request is active. */\nexport const E_LOCAL_DIFFUSION_BUSY = createException<[]>(\n 'E_LOCAL_DIFFUSION_BUSY',\n 'Local diffusion backend is busy',\n 'E_LOCAL_DIFFUSION_BUSY',\n 409,\n false\n)\n/** Adapter has been disposed. */\nexport const E_LOCAL_DIFFUSION_DISPOSED = createException<[]>(\n 'E_LOCAL_DIFFUSION_DISPOSED',\n 'Local diffusion adapter is disposed',\n 'E_LOCAL_DIFFUSION_DISPOSED',\n 409,\n false\n)\n"],"mappings":";;;;;;;;;AAMA,IAAa,oCAAoC,mBAAA,gBAC/C,qCACA,uCACA,qCACA,KACA,IACF;;AAEA,IAAa,kCAAkC,mBAAA,gBAC7C,mCACA,0DACA,mCACA,KACA,KACF;;AAEA,IAAa,oCAAoC,mBAAA,gBAC/C,qCACA,gDACA,qCACA,KACA,KACF;;AAEA,IAAa,oCAAoC,mBAAA,gBAC/C,qCACA,gDACA,qCACA,KACA,KACF;;AAEA,IAAa,4BAA4B,mBAAA,gBACvC,6BACA,mCACA,6BACA,KACA,KACF;;AAEA,IAAa,oCAAoC,mBAAA,gBAC/C,qCACA,uCACA,qCACA,KACA,KACF;;AAEA,IAAa,yBAAyB,mBAAA,gBACpC,0BACA,mCACA,0BACA,KACA,KACF;;AAEA,IAAa,6BAA6B,mBAAA,gBACxC,8BACA,uCACA,8BACA,KACA,KACF"}