138 lines
9.0 KiB
JavaScript
138 lines
9.0 KiB
JavaScript
'use strict'
|
|
/* 插件文本节点 + 上游参考图:任务文本不能被丢空(2026-12-03)
|
|
*
|
|
* 守的是什么:
|
|
* · 一图一任务:相册 N 张 → N 个任务,每个只带自己那张图
|
|
* · 批量模式:按 ALBUM_BATCH_MAX(12) 分批 → ⌈N/12⌉ 个任务,每批多张参考图
|
|
* · 组合扇出:相册接在组合前面时必须变成 N 个任务(否则会被压成 1 个任务 + N 张图)
|
|
* · 零回归:没有相册时,ref/组合/中继的任务数逐字不变
|
|
*
|
|
* 用真实模块 + 最小注入实现(不切片 app.js):依赖全是纯函数,本文件里各写一份等价实现。
|
|
*/
|
|
const assert = require('assert')
|
|
const fs = require('fs')
|
|
const path = require('path')
|
|
const vm = require('vm')
|
|
|
|
const src = fs.readFileSync(path.join(__dirname, '..', 'src', 'generation-task-graph.js'), 'utf8')
|
|
const box = { console }
|
|
vm.runInNewContext(src, box)
|
|
const TG = box.IWBWTaskGraph
|
|
assert.ok(TG && typeof TG.create === 'function', '模块应导出 IWBWTaskGraph.create')
|
|
|
|
// ── 最小注入依赖(语义与 app.js 一致,但实现从简)──
|
|
const byPos = (a, b) => (Number(a.x) - Number(b.x)) || (Number(a.y) - Number(b.y)) || String(a.id).localeCompare(String(b.id))
|
|
const dedupeTaskRefs = (list) => { const seen = new Set(), out = []; for (const r of (list || [])) { if (!r || seen.has(r.id)) continue; seen.add(r.id); out.push(r) } return out }
|
|
const refArgsOf = (rn) => { const d = (rn && rn.data) || {}; if (d.localName) return [{ type: 'local', name: d.localName }]; if (d.data) return [{ type: 'data', value: d.data, name: d.name || 'ref.png' }]; if (typeof d.url === 'string') return d.url.trim() ? [{ type: 'url', value: d.url.trim() }] : []; if (d.url) return [{ type: 'data', value: d.url, name: d.name || 'ref.png' }]; return [] }
|
|
const isBatchSrc = (n) => !!n && (n.type === 'prompt' || n.type === 'textout' || n.type === 'combine' || n.type === 'batch' || (n.data && n.data.outKind === 'prompt'))
|
|
const batchJoinedText = (data, base) => String((data && data.combined) || base || '')
|
|
const batchSrcText = (n) => String((n && n.data && (n.data.combined || n.data.text)) || '')
|
|
const wDedupeTaskIds = (list) => { const seen = new Set(), out = []; for (const t of (list || [])) { if (!t || seen.has(t.id)) continue; seen.add(t.id); out.push(t) } return out }
|
|
|
|
|
|
const tg = TG.create({
|
|
sortByPosition: (arr) => arr.slice().sort(byPos),
|
|
compareCanvasImageOrder: byPos,
|
|
compareCanvasTextOrder: byPos,
|
|
dedupeTaskRefs,
|
|
isBatchSrc,
|
|
batchJoinedText,
|
|
wDedupeTaskIds,
|
|
refArgsOf,
|
|
batchSrcText,
|
|
standardSizeOf: (n) => ({ w: 1024, h: 1024 }),
|
|
})
|
|
|
|
let failed = 0
|
|
const ok = (name, cond, extra) => { if (!cond) failed++; console.log((cond ? '✓ ' : '✗ ') + name + (cond || !extra ? '' : ' · ' + extra)) }
|
|
|
|
const ref = { id: 'r1', type: 'ref', x: 0, y: 0, data: { url: 'data:image/png;base64,AAA' } }
|
|
const tpl = { id: 't1', type: 'plugin:promptlib:ptpl', x: 100, y: 0, data: { text: '我要画一只猫', outKind: 'prompt' } }
|
|
const gen = { id: 'g1', type: 'gen', x: 200, y: 0, data: { model: 'm' } }
|
|
const e1 = { id: 'e1', from: 'r1', to: 't1' }
|
|
const e2 = { id: 'e2', from: 't1', to: 'g1' }
|
|
|
|
// 1) 插件文本节点接了参考图:文本必须保留,图也要带上
|
|
let tasks = tg.buildGenerationTasksV2(gen, [ref, tpl, gen], [e1, e2])
|
|
ok('插件文本节点 + 上游参考图:任务文本保留', tasks.length === 1 && tasks[0].text === '我要画一只猫', JSON.stringify(tasks.map((t) => t.text)))
|
|
ok('插件文本节点 + 上游参考图:参考图带上', tasks.length === 1 && tasks[0].refs.length === 1)
|
|
|
|
// 2) 没有上游参考图:行为不变
|
|
tasks = tg.buildGenerationTasksV2(gen, [tpl, gen], [e2])
|
|
ok('无上游参考图:文本仍是自己的', tasks.length === 1 && tasks[0].text === '我要画一只猫' && tasks[0].refs.length === 0)
|
|
|
|
// 3) 插件节点自己没有文本、只带上游图:仍然允许「纯图任务」(不新增文本)
|
|
const tplEmpty = { id: 't1', type: 'plugin:promptlib:ptpl', x: 100, y: 0, data: { text: '', outKind: 'prompt' } }
|
|
tasks = tg.buildGenerationTasksV2(gen, [ref, tplEmpty, gen], [e1, e2])
|
|
ok('空文本 + 上游图:任务仍成立且文本为空', tasks.length === 1 && tasks[0].text === '' && tasks[0].refs.length === 1)
|
|
|
|
// 4) 上游是文本节点(非 ref):自己的文本仍然优先
|
|
const textNode = { id: 'x1', type: 'textout', x: -50, y: 0, data: { text: '上游文本' } }
|
|
const ex = { id: 'ex', from: 'x1', to: 't1' }
|
|
tasks = tg.buildGenerationTasksV2(gen, [textNode, tpl, gen], [ex, e2])
|
|
ok('上游文本 + 自己文本:自己的优先', tasks.length === 1 && tasks[0].text === '我要画一只猫', JSON.stringify(tasks.map((t) => t.text)))
|
|
|
|
// 5) 对照:内置 prompt 节点行为不变
|
|
const promptNode = { id: 'p1', type: 'prompt', x: 100, y: 0, data: { text: '内置提示词' } }
|
|
const ep = { id: 'ep', from: 'p1', to: 'g1' }
|
|
tasks = tg.buildGenerationTasksV2(gen, [ref, promptNode, gen], [{ id: 'er', from: 'r1', to: 'p1' }, ep])
|
|
ok('内置 prompt 节点 + 上游图:文本保留', tasks.length === 1 && tasks[0].text === '内置提示词' && tasks[0].refs.length === 1)
|
|
|
|
// 6) 两张参考图 → 插件文本节点:必须合并成 1 个任务(图1+图2 一起带)
|
|
{
|
|
const r2 = { id: 'r2', type: 'ref', x: 0, y: 100, data: { url: 'data:image/png;base64,BBB' } }
|
|
const edges2 = [{ id: 'e1', from: 'r1', to: 't1' }, { id: 'e1b', from: 'r2', to: 't1' }, e2]
|
|
const ts = tg.buildGenerationTasksV2(gen, [ref, r2, tpl, gen], edges2)
|
|
ok('两张参考图 → 1 个任务', ts.length === 1, JSON.stringify(ts.map((t) => t.refs.length)))
|
|
ok('两张参考图 → refs=2 且文本保留', ts.length === 1 && ts[0].refs.length === 2 && ts[0].text === '我要画一只猫')
|
|
}
|
|
|
|
// 7) 两张参考图 + 空文本的插件节点:仍是 1 个任务(纯图任务)
|
|
{
|
|
const r2 = { id: 'r2', type: 'ref', x: 0, y: 100, data: { url: 'data:image/png;base64,BBB' } }
|
|
const tplEmpty2 = { id: 't1', type: 'plugin:promptlib:ptpl', x: 100, y: 0, data: { text: '', outKind: 'prompt' } }
|
|
const edges2 = [{ id: 'e1', from: 'r1', to: 't1' }, { id: 'e1b', from: 'r2', to: 't1' }, e2]
|
|
const ts = tg.buildGenerationTasksV2(gen, [ref, r2, tplEmpty2, gen], edges2)
|
|
ok('两张参考图 + 空文本 → 1 个任务、refs=2', ts.length === 1 && ts[0].text === '' && ts[0].refs.length === 2, JSON.stringify(ts.map((t) => ({ t: t.text, r: t.refs.length }))))
|
|
}
|
|
|
|
// 7b) 一张图 + 一段文本 → 插件文本节点:合并成 1 个任务(不能多生成一张没图的)
|
|
{
|
|
const txtNode = { id: 'x9', type: 'textout', x: -50, y: 20, data: { text: '上游文案' } }
|
|
const edges2 = [{ id: 'e1', from: 'r1', to: 't1' }, { id: 'ex9', from: 'x9', to: 't1' }, e2]
|
|
const ts = tg.buildGenerationTasksV2(gen, [ref, txtNode, tpl, gen], edges2)
|
|
ok('图 + 文本 → 1 个任务', ts.length === 1, JSON.stringify(ts.map((t) => ({ t: t.text, r: t.refs.length }))))
|
|
ok('图 + 文本 → 自己的文本优先、refs=1', ts.length === 1 && ts[0].text === '我要画一只猫' && ts[0].refs.length === 1)
|
|
}
|
|
|
|
// 7c) 图 + 文本(节点自己没文本)→ 用上游文本、refs=1
|
|
{
|
|
const txtNode = { id: 'x9', type: 'textout', x: -50, y: 20, data: { text: '上游文案' } }
|
|
const tplEmpty3 = { id: 't1', type: 'plugin:promptlib:ptpl', x: 100, y: 0, data: { text: '', outKind: 'prompt' } }
|
|
const edges2 = [{ id: 'e1', from: 'r1', to: 't1' }, { id: 'ex9', from: 'x9', to: 't1' }, e2]
|
|
const ts = tg.buildGenerationTasksV2(gen, [ref, txtNode, tplEmpty3, gen], edges2)
|
|
ok('图 + 文本(节点无文本)→ 1 个任务、用上游文本', ts.length === 1 && ts[0].text === '上游文案' && ts[0].refs.length === 1, JSON.stringify(ts.map((t) => ({ t: t.text, r: t.refs.length }))))
|
|
}
|
|
|
|
// 8) 回归:相册(2 张) → 插件文本节点 → 仍是一图一任务(不能被压平)
|
|
{
|
|
const album = { id: 'a1', type: 'plugin:album:album', x: 0, y: 0, data: { outKind: 'images', items: [{ src: 'data:image/png;base64,AAA' }, { src: 'data:image/png;base64,BBB' }] } }
|
|
const ea = { id: 'ea', from: 'a1', to: 't1' }
|
|
const ts = tg.buildGenerationTasksV2(gen, [album, tpl, gen], [ea, e2])
|
|
ok('相册(2 张) → 插件文本节点:仍 2 个任务、各带 1 张', ts.length === 2 && ts.every((t) => t.refs.length === 1), JSON.stringify(ts.map((t) => t.refs.length)))
|
|
ok('相册 → 插件文本节点:每个任务都带自己的文本', ts.every((t) => t.text === '我要画一只猫'))
|
|
}
|
|
|
|
// 9) 回归:两个文本上游 → 插件节点:仍是 2 个任务
|
|
{
|
|
const x1 = { id: 'x1', type: 'textout', x: -100, y: 0, data: { text: '上游A' } }
|
|
const x2 = { id: 'x2', type: 'textout', x: -100, y: 100, data: { text: '上游B' } }
|
|
const ex1 = { id: 'ex1', from: 'x1', to: 't1' }
|
|
const ex2 = { id: 'ex2', from: 'x2', to: 't1' }
|
|
const ts = tg.buildGenerationTasksV2(gen, [x1, x2, tpl, gen], [ex1, ex2, e2])
|
|
ok('两个文本上游 → 仍 2 个任务', ts.length === 2, JSON.stringify(ts.map((t) => t.text)))
|
|
}
|
|
|
|
if (failed) { console.error('plugin-text-node-refs: ' + failed + ' 项失败'); process.exit(1) }
|
|
console.log('plugin-text-node-refs checks passed')
|