A local plugin for image-heavy chats in Codex, Claude Code and other agents: earlier images stop going out with every message, and the model fetches the one it needs by its ID, also after compaction.
TypeScript ai-agentsclaude-codeclaude-code-plugincodexcodex-plugincontext-managementdeveloper-toolsimage-generationmcpmultimodal
fix(compaction): keep ratings whole; list a repeated picture as "same as"
chipfighter7a200d56f3a1
diff --git a/plugin/src/rewrite.ts b/plugin/src/rewrite.tsindex 9cbb267..6844c53 100644--- a/plugin/src/rewrite.ts+++ b/plugin/src/rewrite.ts@@ -35,7 +35,7 @@ type FetchWords = { title: string; description: string; ids: string; missing: (itype Words = {kind: Record<ImageKind, string>; unnamed: string; turn: (n: number) => string; thisTurn: string; newImage: string;// v0.10: the source in a compact line (compactLine), shorter.- kindShort: Record<ImageKind, string>;+ kindShort: Record<ImageKind, string>; sameShort: (id: string) => string;omitted: string; notRepeated: string; included: string; separator: string;// v0.10: the label of an image sent once more for its feature line (Described.awaiting).includedAwaiting: string;@@ -64,6 +64,7 @@ const WORDS: Record<Lang, Words> = {zh: {kind: { upload: "用户上传", view: "工具查看的图片", generated: "生成的图片", tool: "工具结果里的图片" },kindShort: { upload: "上传", view: "查看", generated: "生成", tool: "工具" },+ sameShort: (id) => `同 ${id}`,unnamed: "未命名",turn: (n) => `第 ${n} 轮`,thisTurn: "本轮",@@ -169,6 +170,7 @@ const WORDS: Record<Lang, Words> = {en: {kind: { upload: "uploaded by the user", view: "viewed with a tool", generated: "generated image", tool: "image from a tool result" },kindShort: { upload: "upload", view: "viewed", generated: "generated", tool: "tool" },+ sameShort: (id) => `same as ${id}`,unnamed: "unnamed",turn: (n) => `turn ${n}`,thisTurn: "this turn",@@ -269,7 +271,9 @@ function heading(words: Words, image: Described, state: string): string {// differed only after it read the same).const CLIP_AT = /[,。;、!?,.;!?||::\s]/u;const WIDE = /[\p{Script=Han}\p{Script=Hiragana}\p{Script=Katakana}\p{Script=Hangul} -〿-]/u;-const CAPS = { line: 30, rating: 15 };+// A rating keeps as much as a feature line: they are the user's own words, and only a few per session (a long replay+// lost half of one at 15, 2026-10-31).+const CAPS = { line: 30, rating: 30 };export function clip(text: string, max: number): string {const chars = [...text.trim()];let width = 0;@@ -336,19 +340,25 @@ export function compactedNote(images: Described[], sent: Set<string>, lang: Lang// v0.10: an image put back after a compaction without its original, in one line: id, name, source, turn, and what// the model noted (as in a placeholder). A placeholder apiece repeated the same words for every image; a session with-// 130 images before its compaction took 1.3–1.9万 tokens per request that way, about 0.4–0.5万 as lines-// (docs/v0.10/plan.md).-export function compactLine(image: Described, lang: Lang = "zh"): string {+// 127 images before its compaction took about 1.7–2.0万 tokens per request that way, about 0.7–0.9万 as lines+// (docs/v0.10/plan.md). `same`: the id of an earlier line with the same picture (sent again), named instead of its+// features.+export function compactLine(image: Described, lang: Lang = "zh", same: string | null = null): string {const words = WORDS[lang];const fields = [image.id, image.name ?? "-", words.kindShort[image.kind], image.turn !== null ? words.turn(image.turn) : "-"];const { line = null, ratings = [] } = image.notes ?? {};- if (line) fields.push(clip(line, CAPS.line));+ if (same) fields.push(words.sameShort(same));+ else if (line) fields.push(clip(line, CAPS.line));if (ratings.length) fields.push(words.notes(null, ratings.slice(-3).map((rating) => ({ ...rating, text: clip(rating.text, CAPS.rating) }))));return fields.join(lang === "zh" ? "|" : " | ");}-export function compactList(images: Described[], lang: Lang = "zh"): string {- return [WORDS[lang].compactHeader(images.length), ...images.map((image) => compactLine(image, lang))].join("\n");+// The images carry what the indexes know of their content (thread-index.ts, IndexedImage): the same digest or pixels+// is the same picture.+type Listed = Described & { contentId?: string; pixelSha256?: string | null };+export function compactList(images: Listed[], lang: Lang = "zh"): string {+ const sameAs = (image: Listed, at: number) => images.slice(0, at).find((other) => (image.contentId !== undefined && other.contentId === image.contentId) || (image.pixelSha256 && other.pixelSha256 === image.pixelSha256))?.id ?? null;+ return [WORDS[lang].compactHeader(images.length), ...images.map((image, at) => compactLine(image, lang, sameAs(image, at)))].join("\n");}// v0.10, Codex: right after its compaction item, the images that compaction did not keep (thread-index.ts,diff --git a/spike/src/claude-rewrite.test.ts b/spike/src/claude-rewrite.test.tsindex 42677e4..0f71f05 100644--- a/spike/src/claude-rewrite.test.ts+++ b/spike/src/claude-rewrite.test.ts@@ -226,6 +226,12 @@ test("v0.5: after a compaction, the images from before it go back right after thassert.deepEqual(auto.messages[0].content.map((b: any) => b.text), ["summary", compactList([a], "zh"), "<system-reminder>x</system-reminder>"]);assert.equal(compactLine(a, "zh"), "IMG-001|-|上传|第 1 轮");assert.equal(compactLine({ ...a, notes: { line: "书房|红色方块", ratings: [{ turn: 3, text: "太暗" }] } }, "zh"), "IMG-001|-|上传|第 1 轮|书房|红色方块|评价:第 3 轮“太暗”");+ // A rating keeps up to 30 Han characters, as a feature line does (the long test lost the second half of a rating at 15).+ assert.equal(compactLine({ ...a, notes: { line: null, ratings: [{ turn: 9, text: "左边那格光线太平、表情也僵;右边那格手指多了一根" }] } }, "zh"), "IMG-001|-|上传|第 1 轮|评价:第 9 轮“左边那格光线太平、表情也僵;右边那格手指多了一根”");+ // The same picture listed again (sent once more) points at the first one instead of repeating its features.+ const again = { ...a, id: "IMG-002", name: "again.png", kind: "view" as const, turn: 4, contentId: "same", notes: { line: "书房|红色方块", ratings: [] } };+ assert.equal(compactList([{ ...a, contentId: "same" }, again], "zh").split("\n")[2], "IMG-002|again.png|查看|第 4 轮|同 IMG-001");+ assert.equal(compactList([{ ...a, contentId: "same" }, again], "en").split("\n")[2], "IMG-002 | again.png | viewed | turn 4 | same as IMG-001");assert.ok(auto.messages[1].content.startsWith(claudeNote("zh", true)));assert.match(auto.messages[1].content, /摘要之前的对话中有 1 张图片(IMG-001)/);// None sent as an original: "这 1 张", not "其余".