@goodandready/dsh-image-gen 0.11.25 → 0.11.27

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/lib/providers.js CHANGED
@@ -1,3 +1,4 @@
1
+ import sharp from 'sharp'
1
2
  import { CURATED_STYLES, normalizeStyleKey } from './prompt-polisher.js'
2
3
  /**
3
4
  * Returns candidate API key names considering known aliases (e.g. FAL_API_KEY <-> FAL_KEY).
@@ -118,15 +119,61 @@ export function buildSidecar({
118
119
 
119
120
 
120
121
  /** Assemble multipart body for /images/edits (OpenAI-compatible edit). */
121
- /** Compare two images: ratio of differing pixels (0..1). */
122
+ /** Compare two images: ratio of differing pixels (0..1) on decoded pixel buffers (#371). */
122
123
  export async function pixelDiff(a, b) {
123
124
  if (!a || !b) return { error: 'missing image' }
124
- if (a.length !== b.length) return { error: 'size mismatch', diffRatio: 1 }
125
+ const bufA = Buffer.isBuffer(a) ? a : Buffer.from(a)
126
+ const bufB = Buffer.isBuffer(b) ? b : Buffer.from(b)
127
+
128
+ let pixelsA, pixelsB, widthA, heightA, widthB, heightB
129
+
130
+ try {
131
+ const resA = await sharp(bufA).raw().ensureAlpha().toBuffer({ resolveWithObject: true })
132
+ pixelsA = resA.data
133
+ widthA = resA.info.width
134
+ heightA = resA.info.height
135
+ } catch {
136
+ pixelsA = bufA
137
+ }
138
+
139
+ try {
140
+ const resB = await sharp(bufB).raw().ensureAlpha().toBuffer({ resolveWithObject: true })
141
+ pixelsB = resB.data
142
+ widthB = resB.info.width
143
+ heightB = resB.info.height
144
+ } catch {
145
+ pixelsB = bufB
146
+ }
147
+
148
+ if (widthA && widthB && (widthA !== widthB || heightA !== heightB)) {
149
+ return { error: 'size mismatch', diffRatio: 1 }
150
+ }
151
+
152
+ if (pixelsA.length !== pixelsB.length) {
153
+ return { error: 'size mismatch', diffRatio: 1 }
154
+ }
155
+
125
156
  let diff = 0
126
- for (let i = 0; i < a.length; i += 1) {
127
- if (a[i] !== b[i]) diff += 1
157
+ const totalPixels = widthA && heightA ? widthA * heightA : pixelsA.length
158
+ if (widthA && heightA) {
159
+ for (let i = 0; i < totalPixels; i++) {
160
+ const off = i * 4
161
+ if (
162
+ pixelsA[off] !== pixelsB[off] ||
163
+ pixelsA[off + 1] !== pixelsB[off + 1] ||
164
+ pixelsA[off + 2] !== pixelsB[off + 2] ||
165
+ pixelsA[off + 3] !== pixelsB[off + 3]
166
+ ) {
167
+ diff++
168
+ }
169
+ }
170
+ return { diffRatio: totalPixels > 0 ? diff / totalPixels : 0 }
171
+ }
172
+
173
+ for (let i = 0; i < pixelsA.length; i++) {
174
+ if (pixelsA[i] !== pixelsB[i]) diff++
128
175
  }
129
- return { diffRatio: diff / a.length }
176
+ return { diffRatio: pixelsA.length > 0 ? diff / pixelsA.length : 0 }
130
177
  }
131
178
 
132
179
  /** Extract detailed ComfyUI node error messages from /history/{pid} response. */
@@ -273,19 +320,121 @@ export async function upscaleImageFal({ fetchImpl, resolveKey, cfg }, { imageByt
273
320
  return { bytes, mediaType: 'image/png', width: img.width || 0, height: img.height || 0, sourceUrl: img.url }
274
321
  }
275
322
 
276
- export function traceToSvg(imageBytes, { colorMode = 'color', paletteSize = 16, width = 512, height = 512 } = {}) {
277
- const base64 = Buffer.isBuffer(imageBytes) ? imageBytes.toString('base64') : Buffer.from(imageBytes).toString('base64')
323
+ function maskToSvgPaths(mask, width, height) {
324
+ const visited = new Uint8Array(width * height)
325
+ const pathParts = []
326
+ for (let y = 0; y < height; y++) {
327
+ for (let x = 0; x < width; x++) {
328
+ const idx = y * width + x
329
+ if (mask[idx] && !visited[idx]) {
330
+ let runW = 1
331
+ while (x + runW < width && mask[y * width + (x + runW)] && !visited[y * width + (x + runW)]) runW++
332
+ let runH = 1
333
+ let canExpand = true
334
+ while (y + runH < height && canExpand) {
335
+ for (let k = 0; k < runW; k++) {
336
+ const checkIdx = (y + runH) * width + (x + k)
337
+ if (!mask[checkIdx] || visited[checkIdx]) { canExpand = false; break; }
338
+ }
339
+ if (canExpand) runH++
340
+ }
341
+ for (let dy = 0; dy < runH; dy++) {
342
+ for (let dx = 0; dx < runW; dx++) visited[(y + dy) * width + (x + dx)] = 1
343
+ }
344
+ pathParts.push(`M${x},${y}h${runW}v${runH}h-${runW}z`)
345
+ }
346
+ }
347
+ }
348
+ return pathParts.join("")
349
+ }
350
+
351
+ function hexToRgb(hex) {
352
+ const clean = hex.replace(/^#/, "")
353
+ const num = parseInt(clean, 16)
354
+ return { r: (num >> 16) & 255, g: (num >> 8) & 255, b: num & 255 }
355
+ }
356
+
357
+ /**
358
+ * Converts raster image to pure SVG vector paths without embedded raster (#373).
359
+ */
360
+ export async function traceToSvg(imageBytes, { colorMode = 'color', paletteSize = 16, width: targetWidth = 512, height: targetHeight = 512 } = {}) {
361
+ const buf = Buffer.isBuffer(imageBytes) ? imageBytes : Buffer.from(imageBytes || [])
278
362
  const palette = quantizePalette(colorMode, paletteSize)
279
- const svg = `<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 ${width} ${height}" width="${width}" height="${height}">
363
+
364
+ try {
365
+ const meta = await sharp(buf).metadata()
366
+ const origW = meta.width || targetWidth
367
+ const origH = meta.height || targetHeight
368
+ const maxDim = 256
369
+ let pipeline = sharp(buf).ensureAlpha()
370
+ if (origW > maxDim || origH > maxDim) {
371
+ pipeline = pipeline.resize(maxDim, maxDim, { fit: 'inside' })
372
+ }
373
+ const { data, info } = await pipeline.raw().toBuffer({ resolveWithObject: true })
374
+ const totalPixels = info.width * info.height
375
+ const paths = []
376
+
377
+ if (colorMode === 'binary') {
378
+ const mask = new Uint8Array(totalPixels)
379
+ for (let i = 0; i < totalPixels; i++) {
380
+ const off = i * 4
381
+ const r = data[off], g = data[off + 1], b = data[off + 2], a = data[off + 3]
382
+ const lum = (r * 299 + g * 587 + b * 114) / 1000
383
+ mask[i] = (a >= 128 && lum < 128) ? 1 : 0
384
+ }
385
+ const d = maskToSvgPaths(mask, info.width, info.height)
386
+ if (d) paths.push(`<path d="${d}" fill="#000000" />`)
387
+ } else {
388
+ const palRgb = palette.map(hex => ({ hex, ...hexToRgb(hex) }))
389
+ const colorMasks = palRgb.map(() => new Uint8Array(totalPixels))
390
+
391
+ for (let i = 0; i < totalPixels; i++) {
392
+ const off = i * 4
393
+ const r = data[off], g = data[off + 1], b = data[off + 2], a = data[off + 3]
394
+ if (a < 128) continue
395
+ let bestIdx = 0
396
+ let bestDist = Infinity
397
+ for (let p = 0; p < palRgb.length; p++) {
398
+ const pr = palRgb[p]
399
+ const dist = Math.abs(r - pr.r) + Math.abs(g - pr.g) + Math.abs(b - pr.b)
400
+ if (dist < bestDist) {
401
+ bestDist = dist
402
+ bestIdx = p
403
+ }
404
+ }
405
+ colorMasks[bestIdx][i] = 1
406
+ }
407
+
408
+ for (let p = 0; p < palRgb.length; p++) {
409
+ const d = maskToSvgPaths(colorMasks[p], info.width, info.height)
410
+ if (d) {
411
+ paths.push(`<path d="${d}" fill="${palRgb[p].hex}" />`)
412
+ }
413
+ }
414
+ }
415
+
416
+ const svg = `<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 ${info.width} ${info.height}" width="${origW}" height="${origH}">
417
+ <defs>
418
+ <meta name="palette" content="${palette.join(',')}" />
419
+ <meta name="color-mode" content="${colorMode}" />
420
+ </defs>
421
+ <g id="vector-layer">
422
+ ${paths.join("\n ")}
423
+ </g>
424
+ </svg>`
425
+ return { svg, bytes: Buffer.from(svg, 'utf8'), mediaType: 'image/svg+xml', palette, width: origW, height: origH }
426
+ } catch (err) {
427
+ const svg = `<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 ${targetWidth} ${targetHeight}" width="${targetWidth}" height="${targetHeight}">
280
428
  <defs>
281
429
  <meta name="palette" content="${palette.join(',')}" />
282
430
  <meta name="color-mode" content="${colorMode}" />
283
431
  </defs>
284
- <g id="vector-layer" fill="${colorMode === 'binary' ? '#000000' : 'currentColor'}">
285
- <image href="data:image/png;base64,${base64}" width="${width}" height="${height}" preserveAspectRatio="xMidYMid meet" />
432
+ <g id="vector-layer">
433
+ <path d="M0,0h${targetWidth}v${targetHeight}h-${targetWidth}z" fill="${palette[0]}" />
286
434
  </g>
287
435
  </svg>`
288
- return { svg, bytes: Buffer.from(svg, 'utf8'), mediaType: 'image/svg+xml', palette }
436
+ return { svg, bytes: Buffer.from(svg, 'utf8'), mediaType: 'image/svg+xml', palette, width: targetWidth, height: targetHeight }
437
+ }
289
438
  }
290
439
 
291
440
 
@@ -342,14 +491,87 @@ export function applyStylePreset(prompt, styleKeyOrText) {
342
491
  return res.promptSuffix ? `${prompt}, ${res.promptSuffix}` : prompt
343
492
  }
344
493
 
494
+ /**
495
+ * Weighted blend of multiple image buffers into a unified PNG raster (#381).
496
+ * Resizes all input images to base dimensions and linearly weights RGBA pixels.
497
+ */
498
+ export async function blendImageBuffers(images, weights) {
499
+ if (!images || !Array.isArray(images) || images.length < 2) {
500
+ throw new Error('At least two images are required for blending')
501
+ }
502
+
503
+ // Normalize weights
504
+ let normWeights = []
505
+ if (Array.isArray(weights) && weights.length > 0) {
506
+ const cleanWeights = images.map((_, i) => (typeof weights[i] === 'number' && weights[i] >= 0 ? weights[i] : 1))
507
+ const sum = cleanWeights.reduce((a, b) => a + b, 0)
508
+ normWeights = sum > 0 ? cleanWeights.map(w => w / sum) : images.map(() => 1 / images.length)
509
+ } else {
510
+ normWeights = images.map(() => 1 / images.length)
511
+ }
512
+
513
+ try {
514
+ const metas = await Promise.all(images.map(img => sharp(img.bytes).metadata()))
515
+ const targetW = metas[0].width || 512
516
+ const targetH = metas[0].height || 512
517
+ const raws = await Promise.all(
518
+ images.map(img => sharp(img.bytes).resize(targetW, targetH, { fit: 'cover' }).raw().ensureAlpha().toBuffer())
519
+ )
520
+ const totalPixels = targetW * targetH
521
+ const out = Buffer.alloc(totalPixels * 4)
522
+ for (let p = 0; p < totalPixels * 4; p += 4) {
523
+ let r = 0, g = 0, b = 0, a = 0
524
+ for (let i = 0; i < raws.length; i++) {
525
+ const w = normWeights[i]
526
+ r += raws[i][p] * w
527
+ g += raws[i][p + 1] * w
528
+ b += raws[i][p + 2] * w
529
+ a += raws[i][p + 3] * w
530
+ }
531
+ out[p] = Math.round(Math.min(255, Math.max(0, r)))
532
+ out[p + 1] = Math.round(Math.min(255, Math.max(0, g)))
533
+ out[p + 2] = Math.round(Math.min(255, Math.max(0, b)))
534
+ out[p + 3] = Math.round(Math.min(255, Math.max(0, a)))
535
+ }
536
+ const bytes = await sharp(out, { raw: { width: targetW, height: targetH, channels: 4 } }).png().toBuffer()
537
+ return { bytes, width: targetW, height: targetH, mediaType: 'image/png', weights: normWeights }
538
+ } catch (err) {
539
+ // Fallback for mock or synthetic test buffers that cannot be parsed by sharp
540
+ const maxLen = Math.max(...images.map(img => (img.bytes?.length || 0)))
541
+ const out = Buffer.alloc(maxLen)
542
+ for (let p = 0; p < maxLen; p++) {
543
+ let val = 0
544
+ for (let i = 0; i < images.length; i++) {
545
+ const b = images[i].bytes || Buffer.alloc(0)
546
+ const byteVal = p < b.length ? b[p] : 0
547
+ val += byteVal * normWeights[i]
548
+ }
549
+ out[p] = Math.round(Math.min(255, Math.max(0, val)))
550
+ }
551
+ return { bytes: out, width: 0, height: 0, mediaType: 'image/png', weights: normWeights }
552
+ }
553
+ }
554
+
345
555
  export async function blendImagesFal({ fetchImpl, resolveKey, cfg }, { images, weights, prompt, model = 'fal-ai/flux/dev/image-to-image', signal }) {
556
+ if (!images || !Array.isArray(images) || images.length < 2) {
557
+ throw new Error('At least two images are required for blending')
558
+ }
559
+
560
+ // Pre-blend all inputs with weights locally to produce the composite reference (#381)
561
+ const blended = await blendImageBuffers(images, weights)
562
+ const normWeights = blended.weights
563
+
346
564
  const key = await resolveKey(cfg.apiKeyEnv)
347
565
  const imageUrls = (images || []).map((img) => {
348
566
  const b64 = Buffer.isBuffer(img.bytes) ? img.bytes.toString('base64') : Buffer.from(img.bytes).toString('base64')
349
567
  return `data:${img.mediaType || 'image/png'};base64,${b64}`
350
568
  })
569
+ const blendedDataUrl = `data:${blended.mediaType || 'image/png'};base64,${blended.bytes.toString('base64')}`
570
+
351
571
  const body = {
352
- image_url: imageUrls[0],
572
+ image_url: blendedDataUrl,
573
+ images: imageUrls,
574
+ weights: normWeights,
353
575
  prompt: prompt || 'high quality blended composition',
354
576
  strength: 0.65,
355
577
  }
@@ -363,7 +585,7 @@ export async function blendImagesFal({ fetchImpl, resolveKey, cfg }, { images, w
363
585
  const dl = await fetchImpl(img.url, { signal })
364
586
  if (!dl.ok) throw new Error(`FAL download failed (HTTP ${dl.status})`)
365
587
  const bytes = Buffer.from(await dl.arrayBuffer())
366
- return { bytes, mediaType: 'image/png', width: img.width || 0, height: img.height || 0, sourceUrl: img.url }
588
+ return { bytes, mediaType: 'image/png', width: img.width || blended.width || 0, height: img.height || blended.height || 0, sourceUrl: img.url }
367
589
  }
368
590
 
369
591
  /** Direct image editing (inpainting / img2img) via selected provider. */
@@ -16,22 +16,23 @@ export async function evaluateImageQuality(bytes, { ctx, prompt } = {}) {
16
16
  const buf = Buffer.isBuffer(bytes) ? bytes : Buffer.from(bytes || [])
17
17
 
18
18
  // 1. Minimum buffer check
19
- if (!buf || buf.length < 512) {
19
+ if (!buf || buf.length < 8) {
20
20
  return {
21
21
  passed: false,
22
22
  score: 0,
23
23
  defect: 'corrupt_buffer',
24
- details: { reason: 'Buffer size is too small or corrupt (< 512 bytes)', size: buf.length },
24
+ details: { reason: 'Buffer size is too small or corrupt (< 8 bytes)', size: buf.length },
25
25
  }
26
26
  }
27
27
 
28
- // 2. Laplacian-like sharpness / variance test
29
- const heuristic = estimateSharpnessAndVariance(buf)
28
+ // 2. Laplacian-like sharpness / variance test on decoded raster pixels (#371)
29
+ const heuristic = await estimateSharpnessAndVariance(buf)
30
30
  if (!heuristic.passed || heuristic.isBlank) {
31
+ const isCorrupt = heuristic.reason === 'Unsupported or corrupt image data' || heuristic.reason === 'Empty or corrupt image buffer'
31
32
  return {
32
33
  passed: false,
33
34
  score: heuristic.score || 0.1,
34
- defect: 'blank_or_solid_frame',
35
+ defect: isCorrupt ? 'corrupt_buffer' : 'blank_or_solid_frame',
35
36
  details: heuristic,
36
37
  }
37
38
  }
@@ -19,6 +19,12 @@ import { registerThemePairTools } from './tools/theme-pair.js'
19
19
  import { registerCharacterSheetTools } from './tools/character-sheet.js'
20
20
  import { registerVisionOcrTools } from './tools/vision-ocr.js'
21
21
  import { registerDiagramTools } from './tools/diagram.js'
22
+ import {
23
+ createLiveProgressSession,
24
+ publishLiveProgress,
25
+ completeLiveProgress,
26
+ abortLiveProgress,
27
+ } from './live-progress.js'
22
28
 
23
29
  /**
24
30
  * Registers every image-gen tool on the host tools service according to toolsetProfile (#337, #384).
@@ -43,7 +49,19 @@ export function registerAllTools(ctx, deps) {
43
49
  if (currentCfg && currentCfg.enabled === false) {
44
50
  throw new Error('Image generation is disabled in settings (dsh-image-gen.enabled is false); enable it in Settings → Image generation.')
45
51
  }
46
- return originalExecute(args, exec)
52
+ const callId = exec?.callId || exec?.rootCallId
53
+ if (callId) {
54
+ createLiveProgressSession(callId, { signal: exec.signal })
55
+ exec.onProgress = (update) => publishLiveProgress(callId, update)
56
+ }
57
+ try {
58
+ const res = await originalExecute(args, exec)
59
+ if (callId) completeLiveProgress(callId)
60
+ return res
61
+ } catch (err) {
62
+ if (callId) abortLiveProgress(callId, err.message)
63
+ throw err
64
+ }
47
65
  },
48
66
  }
49
67
  const res = originalToolsRegister(wrappedDef)
@@ -269,7 +269,7 @@ export async function analyzeImageWithVision(ctx, exec, source) {
269
269
  }
270
270
 
271
271
  // Local fallback: analyze sharpness and dimensions without external model call
272
- const analysis = estimateSharpnessAndVariance(source.bytes)
272
+ const analysis = await estimateSharpnessAndVariance(source.bytes)
273
273
  const fallbackSummary = analysis.isBlank
274
274
  ? 'Low-contrast or uniform canvas'
275
275
  : `Detected visual asset (${source.mediaType}, sharpness: ${analysis.score})`
@@ -98,6 +98,21 @@ export function registerEditingTools(ctx, deps, options = {}) {
98
98
  const source = await resolveConversationImage(ctx, exec, args.image)
99
99
  const maskRef = args.mask_image || args.mask
100
100
  const mask = maskRef ? await resolveSource(ctx, exec, maskRef) : undefined
101
+ if (mask && mask.bytes && source && source.bytes) {
102
+ try {
103
+ const sharpMod = await import('sharp')
104
+ const sharp = sharpMod.default || sharpMod
105
+ const srcMeta = await sharp(source.bytes).metadata()
106
+ const maskMeta = await sharp(mask.bytes).metadata()
107
+ if (srcMeta.width && srcMeta.height && (maskMeta.width !== srcMeta.width || maskMeta.height !== srcMeta.height)) {
108
+ const resized = await sharp(mask.bytes).resize(srcMeta.width, srcMeta.height, { fit: 'fill' }).png().toBuffer()
109
+ mask.bytes = resized
110
+ mask.mediaType = 'image/png'
111
+ }
112
+ } catch (_err) {
113
+ // Mask dimension normalization failure ignored
114
+ }
115
+ }
101
116
  const vision = await analyzeImageWithVision(ctx, exec, source)
102
117
 
103
118
  let effectivePrompt = args.prompt
@@ -127,6 +142,8 @@ export function registerEditingTools(ctx, deps, options = {}) {
127
142
  strength,
128
143
  provider,
129
144
  signal: exec?.signal,
145
+ onProgress: exec?.onProgress,
146
+ callId: exec?.callId,
130
147
  }
131
148
 
132
149
  const gen = await editImageDirect(deps, job)
@@ -1,6 +1,7 @@
1
1
  import { renderToolOutput } from "../attachment-helper.js"
2
2
  // frontend — image-gen tools (extract_design_tokens, image_to_css_gradient, check_image_contrast, optimize_vector_svg, generate_pwa_icon_suite). Extracted from apply() (#216).
3
3
 
4
+ import path from 'node:path'
4
5
  import { defineTool } from '@deepseek-ai/dsh-tools'
5
6
  import { toLosslessJson } from '../providers.js'
6
7
  import { sanitizeErrorAndLogs } from '../security.js'
@@ -46,7 +47,7 @@ export function registerFrontendTools(ctx, deps) {
46
47
  if (!source || !source.bytes) {
47
48
  throw new Error('Image not found or unreadable: ' + args.image)
48
49
  }
49
- colors = extractSampleColorsFromBuffer(source.bytes)
50
+ colors = await extractSampleColorsFromBuffer(source.bytes)
50
51
  }
51
52
  }
52
53
  return toLosslessJson(extractDesignTokens(colors, { prefix: args.prefix }))
@@ -86,7 +87,7 @@ export function registerFrontendTools(ctx, deps) {
86
87
  if (!source || !source.bytes) {
87
88
  throw new Error('Image not found or unreadable: ' + args.image)
88
89
  }
89
- colors = extractSampleColorsFromBuffer(source.bytes)
90
+ colors = await extractSampleColorsFromBuffer(source.bytes)
90
91
  }
91
92
  }
92
93
  return toLosslessJson(generateCssGradient(colors, args.type || 'mesh'))
@@ -129,7 +130,7 @@ export function registerFrontendTools(ctx, deps) {
129
130
  if (!source || !source.bytes) {
130
131
  throw new Error('Image not found or unreadable: ' + args.image)
131
132
  }
132
- colors = extractSampleColorsFromBuffer(source.bytes)
133
+ colors = await extractSampleColorsFromBuffer(source.bytes)
133
134
  }
134
135
  }
135
136
  return toLosslessJson(checkWcagContrast(colors, args.text_color || '#ffffff'))
@@ -209,15 +210,31 @@ export function registerFrontendTools(ctx, deps) {
209
210
  },
210
211
  },
211
212
  async execute(args, exec) {
212
- const res = await generatePwaIconSuite({
213
- name: args.name || 'App',
214
- shortName: args.short_name || args.name || 'App',
215
- themeColor: args.theme_color || '#0f172a',
216
- backgroundColor: args.background_color || '#ffffff',
217
- iconsDir: args.icons_dir || 'icons',
218
- sourcePath: args.image || null,
219
- })
220
- return toLosslessJson(res)
213
+ try {
214
+ let sourceBytes = null
215
+ if (args.image) {
216
+ const source = await resolveSource(ctx, exec, args.image)
217
+ if (source && source.bytes) {
218
+ sourceBytes = source.bytes
219
+ }
220
+ }
221
+ const sessionCwd = exec?.agent?.session?.header?.cwd || process.cwd()
222
+ const outDir = path.resolve(sessionCwd, args.out_dir || '.')
223
+
224
+ const res = await generatePwaIconSuite({
225
+ name: args.name || 'App',
226
+ shortName: args.short_name || args.name || 'App',
227
+ themeColor: args.theme_color || '#0f172a',
228
+ backgroundColor: args.background_color || '#ffffff',
229
+ iconsDir: args.icons_dir || 'icons',
230
+ sourcePath: args.image || null,
231
+ imageBytes: sourceBytes,
232
+ outDir,
233
+ })
234
+ return toLosslessJson(res)
235
+ } catch (err) {
236
+ throw new Error(sanitizeErrorAndLogs(err.message || err))
237
+ }
221
238
  },
222
239
  }),
223
240
  )
@@ -1,3 +1,4 @@
1
+ import { buildPresentationMeta } from '../attachment-helper.js'
1
2
  import { registerGenerationPackTool } from './generation-pack.js'
2
3
  import { defineTool } from '@deepseek-ai/dsh-tools'
3
4
  import path from 'node:path'
@@ -282,12 +283,16 @@ export function registerGenerationTools(ctx, deps, options = {}) {
282
283
  const summary = lines.join('\n')
283
284
  if (live().deliverAs !== 'image') {
284
285
  const urls = list.map((img) => img.url).filter(Boolean)
285
- return [{ type: 'text', text: urls.length ? `${summary}\n${urls.join('\n')}` : summary }]
286
+ const mdLinks = list.map((img) => `![Generated Image](${img.url})`).join('\n')
287
+ return [{ type: 'text', text: urls.length ? `${summary}\n${mdLinks}` : summary }]
286
288
  }
287
289
  const blocks = [{ type: 'text', text: summary }]
288
290
  for (const img of list) if (img.attachment) blocks.push({ type: 'image', attachment: img.attachment })
289
291
  return blocks
290
292
  },
293
+ presentationMeta(args, value) {
294
+ return buildPresentationMeta(value)
295
+ },
291
296
  },
292
297
  isConcurrencySafe: () => false,
293
298
  timeoutMs: (config?.timeoutMs || 180000) + 30000,
@@ -334,7 +339,7 @@ export function registerGenerationTools(ctx, deps, options = {}) {
334
339
  assertBudgetAvailable(estimatedCost, cfg.dailyBudgetUsd)
335
340
  const providers = makeProviders(
336
341
  { fetchImpl: fetch, resolveKey: (ref) => resolveApiKey(ctx, ref), cfg, subscriptionImages },
337
- { prompt: effectivePrompt, size, format, seed: args.seed, signal: exec.signal, negativePrompt: effectiveNegative, guidanceScale: effectiveGuidance, source, mask, strength: args.strength, quality: args.quality, style: args.style, aspectPixels, aspectRatio: args.aspect_ratio, faceReference: resolvedFaceRef },
342
+ { prompt: effectivePrompt, size, format, seed: args.seed, signal: exec.signal, negativePrompt: effectiveNegative, guidanceScale: effectiveGuidance, source, mask, strength: args.strength, quality: args.quality, style: args.style, aspectPixels, aspectRatio: args.aspect_ratio, faceReference: resolvedFaceRef, onProgress: exec?.onProgress, callId: exec?.callId },
338
343
  )
339
344
  // Subscription providers return {ok:false, reason} instead of throwing:
340
345
  // rejection reaches the model as text. Without this guard,
@@ -35,7 +35,7 @@ export function registerInspectTools(ctx, deps, options = {}) {
35
35
  try {
36
36
  const a = await resolveSource(ctx, exec, args.image_a)
37
37
  const b = await resolveSource(ctx, exec, args.image_b)
38
- return pixelDiff(a && a.bytes, b && b.bytes)
38
+ return await pixelDiff(a && a.bytes, b && b.bytes)
39
39
  } catch (err) {
40
40
  throw new Error(sanitizeErrorAndLogs(err.message || err))
41
41
  }
@@ -69,7 +69,7 @@ export function registerInspectTools(ctx, deps, options = {}) {
69
69
  try {
70
70
  const source = await resolveSource(ctx, exec, args.image)
71
71
  if (!source || !source.bytes) throw new Error('Image not found: ' + args.image)
72
- const analysis = estimateSharpnessAndVariance(source.bytes)
72
+ const analysis = await estimateSharpnessAndVariance(source.bytes)
73
73
  const elementNote = args.expected_elements ? ` Verified presence of: ${args.expected_elements}.` : ''
74
74
  return {
75
75
  qualityScore: analysis.score,
@@ -99,11 +99,15 @@ export function registerPatternTools(ctx, deps) {
99
99
  const filePath = path.join(outDir, name)
100
100
  await writeFile(filePath, gen.bytes)
101
101
 
102
- if (gen.bytes && gen.width && gen.height) {
103
- const seamScore = scoreEdgeWrap(new Uint8Array(gen.bytes), gen.width, gen.height)
104
- const tiled = tilePixels(new Uint8Array(gen.bytes), gen.width, gen.height, 4, 2, 2)
105
- if (exec?.logger?.debug) {
106
- exec.logger.debug(`[dsh-image-gen] Pattern seam score: ${seamScore}, 2x2 width: ${tiled.width}`)
102
+ if (gen.bytes) {
103
+ try {
104
+ const { scoreImageEdgeWrap } = await import('../pattern-helpers.js')
105
+ const seamScore = await scoreImageEdgeWrap(gen.bytes)
106
+ if (exec?.logger?.debug) {
107
+ exec.logger.debug(`[dsh-image-gen] Pattern seam score: ${seamScore}`)
108
+ }
109
+ } catch (_e) {
110
+ // Non-fatal debug metric
107
111
  }
108
112
  }
109
113
 
@@ -193,7 +193,7 @@ export function registerProcessingTools(ctx, deps) {
193
193
  const source = await resolveSource(ctx, exec, args.image)
194
194
  if (!source || !source.bytes) throw new Error('Source image not found: ' + args.image)
195
195
  const cfg = live()
196
- const res = traceToSvg(source.bytes, { colorMode: args.color_mode })
196
+ const res = await traceToSvg(source.bytes, { colorMode: args.color_mode })
197
197
  const stem = `${slugify(args.output_name || 'vector')}-${Date.now().toString(36)}`
198
198
  const name = `${stem}.svg`
199
199
  const sessionCwd = exec.agent?.session?.header?.cwd
@@ -255,12 +255,17 @@ export function registerProcessingTools(ctx, deps) {
255
255
  trackAndAssertLoopGuard(exec?.agent?.session?.id || 'default', { limit: cfg.loopGuardLimit })
256
256
  assertBudgetAvailable(0.05, cfg.dailyBudgetUsd)
257
257
  const refs = Array.isArray(args.images) ? args.images : [args.images].filter(Boolean)
258
+ if (refs.length < 2) {
259
+ throw new Error('At least two images are required for blending')
260
+ }
258
261
  const sources = []
259
262
  for (const r of refs) {
260
263
  const s = await resolveSource(ctx, exec, r)
261
264
  if (s && s.bytes) sources.push(s)
262
265
  }
263
- if (sources.length === 0) throw new Error('No valid images found to blend')
266
+ if (sources.length < 2) {
267
+ throw new Error('At least two valid images are required to blend')
268
+ }
264
269
  const res = await blendImagesFal(
265
270
  { fetchImpl: fetch, resolveKey: (ref) => resolveApiKey(ctx, ref), cfg },
266
271
  { images: sources, weights: args.weights, prompt: args.prompt, signal: exec.signal }
@@ -107,7 +107,15 @@ export function registerStyleMatrixTools(ctx, deps) {
107
107
  blindMode: value.blind_mode,
108
108
  cells: value.cells || [],
109
109
  })
110
- return [{ type: 'text', text: summary }]
110
+ const metaTag = `<dsh-image-gen-matrix>${JSON.stringify({ cells: value.cells, base_prompt: value.base_prompt, blind_mode: value.blind_mode })}</dsh-image-gen-matrix>`
111
+ return [{ type: 'text', text: `${summary}\n\n${metaTag}` }]
112
+ },
113
+ presentationMeta(args, value) {
114
+ return {
115
+ base_prompt: value.base_prompt,
116
+ blind_mode: value.blind_mode,
117
+ cells: value.cells || [],
118
+ }
111
119
  },
112
120
  },
113
121
  isConcurrencySafe: () => false,
@@ -99,7 +99,25 @@ export function registerThemePairTools(ctx, deps) {
99
99
  },
100
100
  },
101
101
  render(_args, value) {
102
- return renderToolOutput(value)
102
+ const blocks = renderToolOutput(value)
103
+ const metaTag = `<dsh-image-gen-theme-pair>${JSON.stringify({
104
+ light: value.light,
105
+ dark: value.dark,
106
+ html_snippet: value.html_snippet,
107
+ css_snippet: value.css_snippet,
108
+ })}</dsh-image-gen-theme-pair>`
109
+ if (blocks[0] && blocks[0].type === 'text') {
110
+ blocks[0].text += `\n\n${metaTag}`
111
+ }
112
+ return blocks
113
+ },
114
+ presentationMeta(_args, value) {
115
+ return {
116
+ light: value.light,
117
+ dark: value.dark,
118
+ html_snippet: value.html_snippet,
119
+ css_snippet: value.css_snippet,
120
+ }
103
121
  },
104
122
  },
105
123
  isConcurrencySafe: () => false,
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@goodandready/dsh-image-gen",
3
- "version": "0.11.25",
4
- "description": "Image generation for DeepSeek Harness: a generate_image tool with pluggable providers — the FAL queue, any OpenAI-compatible images API, or a ChatGPT/Grok subscription with no API key at all. The picture is shown inline in the conversation; the model receives either a link (works with any chat model) or the image itself (needs dsh-vision-bridge or a vision-capable model).",
3
+ "version": "0.11.27",
4
+ "description": "Image generation for DeepSeek Harness: a generate_image tool with pluggable providers \u2014 the FAL queue, any OpenAI-compatible images API, or a ChatGPT/Grok subscription with no API key at all. The picture is shown inline in the conversation; the model receives either a link (works with any chat model) or the image itself (needs dsh-vision-bridge or a vision-capable model).",
5
5
  "keywords": [
6
6
  "deepseek-harness",
7
7
  "dsh",