|
| 1 | +// 图块抽取:会话消息里的 `type:"image"` 块到纯文本占位(Issue #611)。 |
| 2 | +// |
| 3 | +// 为什么单独一个模块:在本次改动之前,`textOf` 的实现在 summarize / continuity / |
| 4 | +// hot-memory 三处各有一份副本(各写一份的理由不同,注释里都写了)。三份都是 |
| 5 | +// `p?.type && p.type !== "text"` → 空串,于是图片一律被丢。给三份各加一遍 image |
| 6 | +// 分支,等于给同一件事写三份实现——而这三份本来就该同一个口径。所以为 image |
| 7 | +// 单独抽一个**只做一件事**的模块:给定一个 content 块,回答它的可读文本。 |
| 8 | +// |
| 9 | +// 口径(维护者 2026-11-08 定,先只做 off 档): |
| 10 | +// - 不调 vision、不读字节、不落原图。宿主有 `attachments.readImage()`,但记忆库 |
| 11 | +// 已 273MB,原图档明确不做。 |
| 12 | +// - 占位形如 `[图: 屏幕截图 2026-10-19.png]`,带文件名与宽高,够人回看时定位; |
| 13 | +// 拿不到名字时退化成 `[图: image/png]`、再退化成 `[图]`,**绝不返回空串**—— |
| 14 | +// 返回空串正是本次要修的那个 bug(图-only 消息整条被丢,静默)。 |
| 15 | +// - 这里只回答「这一块读出什么文本」,不回答「这个模型能不能看图」。后者要看 |
| 16 | +// `llm.resolveModelInfo(...).inputModalities`,属于描述档的准入判断,本批不做。 |
| 17 | + |
| 18 | +/** 图片块的占位前缀:审计与测试都按它计数。 */ |
| 19 | +export const IMAGE_PLACEHOLDER_PREFIX = "[图"; |
| 20 | + |
| 21 | +/** 一个图片块读出的人类可读标签(不含方括号)。什么元数据都没有时返回空串。 */ |
| 22 | +export function imageLabel(block) { |
| 23 | + const attachment = block?.attachment ?? {}; |
| 24 | + const name = typeof attachment.name === "string" ? attachment.name.trim() : ""; |
| 25 | + const mediaType = typeof attachment.mediaType === "string" ? attachment.mediaType : ""; |
| 26 | + const width = Number.isFinite(attachment.width) ? attachment.width : undefined; |
| 27 | + const height = Number.isFinite(attachment.height) ? attachment.height : undefined; |
| 28 | + const size = width !== undefined && height !== undefined ? `${width}x${height}` : ""; |
| 29 | + // 没有名字也没有 mediaType 时返回空串,由调用方退化成裸 `[图]`——宁可写「有一张图」, |
| 30 | + // 也不要写「[图: image]」这种把类型名当名字的假标签。 |
| 31 | + const head = name || mediaType; |
| 32 | + if (!head) return ""; |
| 33 | + const tail = [mediaType && name ? mediaType : "", size].filter(Boolean).join(" "); |
| 34 | + return tail ? `${head} ${tail}` : head; |
| 35 | +} |
| 36 | + |
| 37 | +/** |
| 38 | + * 把一个 content 块读成文本。 |
| 39 | + * |
| 40 | + * @param {unknown} block content 数组里的一项 |
| 41 | + * @param {object} [options] |
| 42 | + * @param {boolean} [options.imagePlaceholder=true] false 时图片块返回空串(旧行为, |
| 43 | + * 留给需要严格「只要文字」的调用方,例如把同一批内容喂给不支持图的模型)。 |
| 44 | + * @returns {string} |
| 45 | + */ |
| 46 | +export function blockToText(block, options = {}) { |
| 47 | + if (typeof block === "string") return block; |
| 48 | + if (!block || typeof block !== "object") return ""; |
| 49 | + if (block.type === "image") { |
| 50 | + if (options.imagePlaceholder === false) return ""; |
| 51 | + const label = imageLabel(block); |
| 52 | + return label ? `${IMAGE_PLACEHOLDER_PREFIX}: ${label}]` : `${IMAGE_PLACEHOLDER_PREFIX}]`; |
| 53 | + } |
| 54 | + if (block.type === undefined || block.type === "text") { |
| 55 | + return typeof block.text === "string" ? block.text : ""; |
| 56 | + } |
| 57 | + // reasoning 等私有块:刻意不采集(各调用方的注释都写了理由,这里不重复)。 |
| 58 | + return ""; |
| 59 | +} |
| 60 | + |
| 61 | +/** |
| 62 | + * 把一个 content 数组读成多行文本。字符串数组(宽松形状)与严格块数组都认。 |
| 63 | + * |
| 64 | + * @param {unknown} content |
| 65 | + * @param {object} [options] 透传给 blockToText |
| 66 | + * @returns {string} |
| 67 | + */ |
| 68 | +export function contentToText(content, options = {}) { |
| 69 | + if (typeof content === "string") return content; |
| 70 | + if (!Array.isArray(content)) return ""; |
| 71 | + return content |
| 72 | + .map((block) => blockToText(block, options)) |
| 73 | + .filter((s) => s) |
| 74 | + .join("\n"); |
| 75 | +} |
| 76 | + |
| 77 | +/** |
| 78 | + * 数一段 content 里有几个图片块(审计用)。 |
| 79 | + * |
| 80 | + * 为什么单独一个函数而不是在 contentToText 里顺带返回:调用方要的是「这一窗有多少 |
| 81 | + * 张图」这个数,而 contentToText 的返回值是字符串,签名不想为一个计量改成对象。 |
| 82 | + * |
| 83 | + * @param {unknown} content |
| 84 | + * @returns {number} |
| 85 | + */ |
| 86 | +export function countImages(content) { |
| 87 | + if (!Array.isArray(content)) return 0; |
| 88 | + let n = 0; |
| 89 | + for (const block of content) { |
| 90 | + if (block && typeof block === "object" && block.type === "image") n += 1; |
| 91 | + } |
| 92 | + return n; |
| 93 | +} |
0 commit comments