Files
image-workbench-desktop/src/generation-results-store.js
T

164 lines
7.0 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/* 生成结果(生图节点 data.result)的**精简落盘**与**跨端合并**
*
* 背景(2026-11-03 用户实测缺陷):输出节点只是「生图节点 result 的只读投影」,
* 而 result 此前被当成「会话态」——保存前一概置 null(withoutGenerationResults),
* 于是刷新 / 新开标签 / 切画布 / 跨设备同步 / 别的标签保存一下,输出节点的图全掉。
*
* 本模块只做两件纯事(读写入参的副本,绝不改原对象):
* 1. slim:把 result 压到「显示所需的最小字段」——只留 url/宽高/idb + 任务标识,
* 丢掉每条结果上挂的 job(含任务提示词全文与参考图引用数组)与 snapshot。
* ⇒ 落盘体积从「每条 1~3KB」降到「每条约 200B」,也让撤销历史/memory 统计不被撑大。
* 2. merge:按 (promptId, vi) 合并,同键取 at 更新者。
* ⇒ 别的标签/设备保存了一份「没有这条结果」的存档时,不会把我内存里的新结果洗掉。
*
* 上限:每个节点最多保留 MAX_ENTRIES 条(按 at 取最新),避免长期堆积无上限。
* 只经 window.IWBGenerationResults 使用(与其它 src 模块一致,无裸标识符跨文件引用)。
*/
;(function (global) {
'use strict'
const MAX_ENTRIES = 60
const ITEM_KEYS = ['ok', 'url', 'width', 'height', 'idb', 'error', 'errorType']
const ENTRY_KEYS = ['promptId', 'vi', 'label', 'taskTitle', 'at']
const pick = (src, keys) => {
const out = {}
for (const k of keys) if (src && src[k] !== undefined) out[k] = src[k]
return out
}
const atOf = (g) => {
const n = Number(g && g.at)
return Number.isFinite(n) && n > 0 ? n : 0
}
// 单条结果的精简:**只留显示与「重试/下载」真正用到的字段**
const slimItem = (r) => (r && typeof r === 'object') ? pick(r, ITEM_KEYS) : r
const slimEntry = (g) => {
if (!g || typeof g !== 'object') return g
const out = pick(g, ENTRY_KEYS)
if (Array.isArray(g.items)) out.items = g.items.map(slimItem)
return out
}
// 裁剪到上限:at 最大的 N 条(无 at 的老数据排在后面,先被裁)
const capEntries = (list) => {
const arr = Array.isArray(list) ? list : []
if (arr.length <= MAX_ENTRIES) return arr
return arr.slice()
.sort((a, b) => atOf(b) - atOf(a))
.slice(0, MAX_ENTRIES)
.sort((a, b) => atOf(a) - atOf(b))
}
// 单节点:result 不是数组时原样返回(引用相等 ⇒ 调用方可用 !== 判断有没有改动)
const slimNode = (n) => {
if (!n || !n.data || !Array.isArray(n.data.result)) return n
return Object.assign({}, n, { data: Object.assign({}, n.data, { result: capEntries(n.data.result.map(slimEntry)) }) })
}
const slimWs = (w) => {
if (!w || !Array.isArray(w.nodes)) return w
let touched = false
const nodes = w.nodes.map((n) => { const s = slimNode(n); if (s !== n) touched = true; return s })
return touched ? Object.assign({}, w, { nodes }) : w
}
const slimList = (list) => (Array.isArray(list) ? list.map(slimWs) : list)
const keyOf = (g) => String((g && g.promptId) || '') + '#' + String(Number((g && g.vi) || 0))
// 按 (promptId, vi) 合并:**同键取 at 更新者**,不同键并集;结果按 at 升序(稳定,便于比较)
const mergeEntries = (incoming, existing) => {
const a = capEntries(incoming)
const b = capEntries(existing)
if (!a.length) return b
if (!b.length) return a
const map = new Map()
for (const g of b) { const k = keyOf(g); const prev = map.get(k); if (!prev || atOf(g) >= atOf(prev)) map.set(k, g) }
for (const g of a) { const k = keyOf(g); const prev = map.get(k); if (!prev || atOf(g) >= atOf(prev)) map.set(k, g) }
return capEntries(Array.from(map.values()).sort((x, y) => atOf(x) - atOf(y)))
}
const mergeNode = (inc, cur) => {
if (!inc || !cur) return inc || cur
const a = inc.data && Array.isArray(inc.data.result) ? inc.data.result : null
const b = cur.data && Array.isArray(cur.data.result) ? cur.data.result : null
// 两边都没结果:直接用对方节点(画布结构以 incoming 为准)
if (!a && !b) return inc
const merged = mergeEntries(a || [], b || [])
if (!merged.length) return inc
return Object.assign({}, inc, { data: Object.assign({}, inc.data || {}, { result: merged }) })
}
// 整份画布列表合并:结构/边/分组一律用 incoming(对端最新),只有 result 做合并
const mergeWs = (inc, cur) => {
if (!inc || !cur || !Array.isArray(inc.nodes) || !Array.isArray(cur.nodes)) return inc
const curMap = new Map(cur.nodes.map((n) => [n && n.id, n]))
let touched = false
const nodes = inc.nodes.map((n) => {
if (!n || !n.id) return n
const prev = curMap.get(n.id)
const hasInc = n.data && Array.isArray(n.data.result) && n.data.result.length
const hasCur = prev && prev.data && Array.isArray(prev.data.result) && prev.data.result.length
if (!hasInc && !hasCur) return n
const m = mergeNode(n, prev)
if (m !== n) touched = true
return m
})
return touched ? Object.assign({}, inc, { nodes }) : inc
}
const mergeWsList = (incoming, current) => {
if (!Array.isArray(incoming)) return incoming
if (!Array.isArray(current) || !current.length) return incoming
const curMap = new Map(current.map((w) => [w && w.id, w]))
return incoming.map((w) => {
const cur = curMap.get(w && w.id)
return cur ? mergeWs(w, cur) : w
})
}
// 落盘后 `items[].data`(缩略图 base64)被剥掉,只剩 `idb` 引用 ⇒ 装载时按需把缩略图读回内存缓存,
// 否则输出节点会退化成加载远端原图(实测 4K 原图解码是画布内存尖峰的主因)。
const resultThumbRefs = (nodes) => {
const out = []
const seen = new Set()
for (const n of (Array.isArray(nodes) ? nodes : [])) {
const r = n && n.data && n.data.result
if (!Array.isArray(r)) continue
for (const g of r) {
const items = (g && Array.isArray(g.items)) ? g.items : []
for (const it of items) {
if (!it || !it.idb || !it.url || it.data) continue
const url = String(it.url)
if (seen.has(url)) continue
seen.add(url)
out.push({ idb: String(it.idb), url })
}
}
}
return out
}
// has(url):已缓存则跳过;fetchOne(idb, url):调用方负责真的读字节并回填缓存
const warmThumbs = (nodes, has, fetchOne) => {
const refs = resultThumbRefs(nodes)
for (const x of refs) {
try {
if (typeof has === 'function' && has(x.url)) continue
if (typeof fetchOne === 'function') fetchOne(x.idb, x.url)
} catch (e) { /* 预热失败不影响装载 */ }
}
return refs.length
}
global.IWBGenerationResults = Object.freeze({
MAX_ENTRIES, ITEM_KEYS, ENTRY_KEYS,
slimItem, slimEntry, slimNode, slimWs, slimList,
mergeEntries, mergeNode, mergeWs, mergeWsList,
resultThumbRefs, warmThumbs,
})
})(typeof window !== 'undefined' ? window : globalThis)