Stop paying the multimodal tax. Your AI agent keeps every image it has seen but only sends the ones it needs: lighter chats, no endless reconnecting, no starting over. Works with Codex, Claude Code, Cline, Gemini CLI, OpenCode, WorkBuddy and more. Install once, forget it.
TypeScript ai-agentsai-imageclaude-codeclaude-code-pluginclinecodexcodex-plugincomputer-usecontext-engineeringcontext-managementdeveloper-toolsgemini-cliimage-generationmcpmultimodalmultimodal-aiopencodeqwen-codetoken-optimizationvision-language-model
feat(rewrite): 特征行说明、当轮新图编号和带特征的占位
chipfighter29d97344b3b3
diff --git a/plugin/src/claude-index.ts b/plugin/src/claude-index.tsindex 725326c..8795bd0 100644--- a/plugin/src/claude-index.ts+++ b/plugin/src/claude-index.ts@@ -16,6 +16,7 @@ import { closeSync, existsSync, openSync, readdirSync, readSync, statSync } fromimport { homedir } from "node:os";import { join } from "node:path";import { baseName, FETCHED_STATE, imageDigest, type ImageKind, type ImageRef } from "./images.ts";+import { addNotes, type Notes } from "./notes.ts";import { pixelHashOfData, type FetchedCopy, type IndexedImage, type ThreadIndex } from "./thread-index.ts";type Json = Record<string, any>;@@ -181,13 +182,17 @@ export function buildClaudeIndex(sessionId: string, records: ClaudeRecord[], tra// The assistant's text in the current turn and in the one before (Codex: requestedIds in panel-state.ts).let current: string[] = [];let previous: string[] = [];+ // v0.8: what the model noted in record lines at the end of its replies (notes.ts), by image id.+ const notes = new Map<string, Notes>();chain.forEach((record, position) => {const content = record.message?.content;if (record.type === "assistant" && Array.isArray(content)) {for (const block of content as Json[]) {if (block?.type === "tool_use" && block.id) tools.set(String(block.id), { name: String(block.name ?? ""), input: block.input ?? {} });const text = textOf(block);- if (text) current.push(text);+ if (!text) continue;+ current.push(text);+ addNotes(notes, text, turn || null);}return;}@@ -252,7 +257,7 @@ export function buildClaudeIndex(sessionId: string, records: ClaudeRecord[], traconst twin = images.find((other) => other.name && (other.contentId === entry.contentId || (entry.pixelSha256 !== null && other.pixelSha256 === entry.pixelSha256)));if (twin) entry.name = twin.name;}- return { threadId: sessionId, images, byKey, turns: turn, turnNumbers, copies, uploads, chain, title: titleOf(records, chain), replies, transcript, summary };+ return { threadId: sessionId, images, byKey, turns: turn, turnNumbers, copies, notes, uploads, chain, title: titleOf(records, chain), replies, transcript, summary };}// The session's name as Claude Code shows it: the latest custom title, else the start of the first prompt.diff --git a/plugin/src/claude-rewrite.ts b/plugin/src/claude-rewrite.tsindex 2c50ac9..aff89fe 100644--- a/plugin/src/claude-rewrite.ts+++ b/plugin/src/claude-rewrite.ts@@ -9,12 +9,14 @@// — thinking blocks included — is passed on unchanged.// Matching to the index (claude-index.ts): an image inside a tool result by its tool_use_id; pasted images by// content, a request message to the transcript record(s) holding the same images in the same order.+// v0.8: the feature line note follows the message holding the first image (a system message, as the other notes), and+// every image sent gets its id, also the ones new in this turn; placeholders carry what the model noted.// Input: the request's messages, the session's index, the keys to leave out. Output: new messages plus a report.import type { ClaudeIndex, ClaudeImage } from "./claude-index.ts";import { imageDigest, type ImageDigest, type ImageKind } from "./images.ts";import type { Lang } from "./language.ts";-import { autoPlaceholder, claudeNote, compactedNote, copyNote, duplicatePlaceholder, hereNote, includedLabel, newImageWord, plainPlaceholder, type Described, type RewriteReport } from "./rewrite.ts";+import { autoPlaceholder, claudeNote, compactedNote, copyNote, duplicatePlaceholder, featureNote, hereNote, includedLabel, newImageWord, plainPlaceholder, type Described, type RewriteReport } from "./rewrite.ts";type Json = Record<string, any>;// msg: the message; part: the block in its content; sub: for an image in a tool result, its position in that result.@@ -89,24 +91,34 @@ export type ClaudeRewriteOptions = {auto?: { currentTurn: number };// v0.5: the full note is given before these messages already (after a compaction summary holding placeholders).noted?: boolean;+ // v0.8: the feature line note and ids on this turn's new images too (rewrite.ts).+ features?: boolean;};export function rewriteMessages(messages: Json[],- index: Pick<ClaudeIndex, "uploads" | "byKey">,+ index: Pick<ClaudeIndex, "uploads" | "byKey"> & Partial<Pick<ClaudeIndex, "notes">>,leaveOut: Set<string>,pixelHash: (image: RequestImage) => string | null,options: ClaudeRewriteOptions = {},): { messages: Json[]; report: RewriteReport } {- const { lang = "zh", copies = new Map<string, string>(), auto, noted = false } = options;+ const { lang = "zh", copies = new Map<string, string>(), auto, noted = false, features = false } = options;const refs = findRequestImages(messages, index);- const describe = (ref: RequestImage): ClaudeImage | undefined => (ref.key ? index.byKey.get(ref.key) : undefined);+ const describe = (ref: RequestImage): ClaudeImage | undefined => {+ const image = ref.key ? index.byKey.get(ref.key) : undefined;+ const notes = image ? index.notes?.get(image.id) : undefined;+ return image && notes ? { ...image, notes } : image;+ };const report: RewriteReport = { images: refs.length, replaced: [], locked: [], sentContentIds: [], copies: [] };const copied = refs.filter((ref) => ref.key !== null && copies.has(ref.key));const drop = refs.filter((ref) => ref.key !== null && !copies.has(ref.key) && leaveOut.has(ref.key) && describe(ref));const sent = refs.filter((ref) => !drop.includes(ref) && !copied.includes(ref));report.sentContentIds = sent.map((ref) => ref.contentId);- if (!drop.length && !copied.length) return { messages, report };+ const unknown = refs.filter((ref) => !copied.includes(ref) && !describe(ref)).length;+ if (unknown) report.unknown = unknown;+ // v0.8: the first image the index knows; the feature line note follows its message.+ const first = features ? refs.find((ref) => describe(ref)) : undefined;+ if (!drop.length && !copied.length && !first) return { messages, report };const earlier = (ref: RequestImage) => { const turn = describe(ref)?.turn; return auto !== undefined && turn !== null && turn !== undefined && turn < auto.currentTurn; };const lasting = auto ? sent.filter(earlier) : sent;const same = (a: RequestImage, b: RequestImage) => {@@ -141,10 +153,13 @@ export function rewriteMessages(edit(ref, 1, copyNote(copies.get(ref.key!)!, lang));report.copies.push(copies.get(ref.key!)!);}- // v0.1-25: once anything is left out, every image still sent says which id it is.- for (const ref of lasting) {+ // v0.1-25: once anything is left out, every image still sent says which id it is. v0.8: with feature lines, this+ // turn's new images too.+ for (const ref of features ? sent : lasting) {const image = describe(ref);- if (image) edit(ref, 0, includedLabel(image, lang), true);+ if (!image) continue;+ edit(ref, 0, includedLabel(image, lang), true);+ report.labeled = (report.labeled ?? 0) + 1;}const apply = (blocks: Json[], list: Edit[]) => {const out = [...blocks];@@ -174,6 +189,12 @@ export function rewriteMessages(noteAfter(next, msg, `${msg === holding[0] && !noted ? `${claudeNote(lang, auto !== undefined)}\n` : ""}${hereNote(plainBy.get(msg)!, lang, auto !== undefined)}`);}if (holding.length && !noted) report.noteAt = holding[0] + 1;+ // v0.8: after the message holding the first image, at or before the others; joined with a note already there.+ if (first) {+ const added = noteAfter(next, first.msg, featureNote(lang));+ report.featureAt = first.msg + 1;+ if (added && report.noteAt !== undefined && report.noteAt > first.msg) report.noteAt += 1;+ }return { messages: next, report };}diff --git a/plugin/src/notes.ts b/plugin/src/notes.tsnew file mode 100644index 0000000..121f471--- /dev/null+++ b/plugin/src/notes.ts@@ -0,0 +1,33 @@+// Purpose: v0.8 — the record lines the model writes at the end of a reply (docs/v0.8/plan.md): a feature line for each+// image new in that turn, or a rating the user gave an image, each as a Markdown link definition, which neither Codex+// Desktop nor Claude Desktop shows (gate 1, 2026-10-19): `[//]: # (cam IMG-015 深蓝雨夜|近景,正脸)`,+// `[//]: # (cam IMG-003 评价:太暗)`; in English `… (cam IMG-003 rated: too dark)`.+// Only the model's own replies are read (thread-index.ts, claude-index.ts): the same text in a user message or a tool+// result counts for nothing, so a web page cannot plant one.+// Input: the text of an assistant reply and its turn. Output: what the model noted, per image id.++// rating: the user's words the model noted, with the turn it noted them in.+export type Rating = { turn: number | null; text: string };+// line: the feature line, written once (the first one stands); ratings: oldest first.+export type Notes = { line: string | null; ratings: Rating[] };++const RECORD = /^ {0,3}\[\/\/\]:[ \t]*#[ \t]*\(cam[ \t]+(IMG-\d{3,})[ \t]+([^()\n]+?)[ \t]*\)[ \t]*$/gm;+const RATING = /^(?:评价|rated)[ \t]*[::][ \t]*/i;++export function addNotes(notes: Map<string, Notes>, text: string, turn: number | null): void {+ for (const [, id, body] of text.matchAll(RECORD)) {+ const entry = notes.get(id) ?? { line: null, ratings: [] };+ const rating = RATING.exec(body);+ if (rating) {+ const said = body.slice(rating[0].length).trim();+ if (said) entry.ratings.push({ turn, text: said });+ } else if (entry.line === null) entry.line = body.trim();+ notes.set(id, entry);+ }+}++// The scene: the feature line's first part, before "|" (or " | " in English).+export function sceneOf(line: string | null): string | null {+ const scene = line?.split(/||\s\|\s/)[0]?.trim();+ return scene ? scene : null;+}diff --git a/plugin/src/proxy.ts b/plugin/src/proxy.tsindex 359e713..2ca374d 100644--- a/plugin/src/proxy.ts+++ b/plugin/src/proxy.ts@@ -41,7 +41,7 @@ import { recordRequest } from "./request-stats.ts";import { rewriteItems, type Described } from "./rewrite.ts";import { connectDirectly, pluginGone, usesEngine } from "./setup.ts";import { claudeSelection, effectiveSelection, ensureAutoDefault, selectionDir, sideChatSelection } from "./selection.ts";-import { loadThreadIndex, pixelHashOf, pixelHashOfData, setPixelCache, sideChatOrigin, type ThreadIndex } from "./thread-index.ts";+import { loadThreadIndex, pixelHashOf, pixelHashOfData, setPixelCache, sideChatOrigin, writesNotes, type ThreadIndex } from "./thread-index.ts";type Json = Record<string, any>;const here = dirname(fileURLToPath(import.meta.url));@@ -294,7 +294,9 @@ export function rewriteBody(original: Buffer, encoding: string | undefined, threconst selection = side ?? effectiveSelection(threadId, sessionsDir, dir);const describe = (ref: ImageRef): Described | undefined => {const known = index.byKey.get(ref.key);- if (known) return known;+ // v0.8: with what the model noted about it, for its placeholder.+ const notes = known ? index.notes.get(known.id) : undefined;+ if (known) return notes ? { ...known, notes } : known;const stored = selection.unchecked[ref.key];return stored ? { id: stored.id, name: ref.name, label: ref.label, kind: ref.kind, turn: null, width: ref.width, height: ref.height } : undefined;};@@ -306,7 +308,9 @@ export function rewriteBody(original: Buffer, encoding: string | undefined, threconst pinned = selection.pinned ?? {};leaveOut = new Set(index.images.filter((image) => image.replaceable && image.turn !== null && image.turn < current && !pinned[image.key]).map((image) => image.key));}- const { items, report } = rewriteItems(json.input, describe, leaveOut, pixels, { lang: currentLang(), copies, ...(selection.auto ? { auto: { currentTurn: current } } : {}) });+ // v0.8: feature lines in a task's own requests, not a side chat's or a subagent's.+ const features = !origin && writesNotes(sessionsDir, threadId);+ const { items, report } = rewriteItems(json.input, describe, leaveOut, pixels, { lang: currentLang(), copies, features, ...(selection.auto ? { auto: { currentTurn: current } } : {}) });const summary: Json = {images: report.images,replaced: report.replaced.map(({ key: _key, ...rest }) => rest),@@ -315,8 +319,10 @@ export function rewriteBody(original: Buffer, encoding: string | undefined, thre...(selection.auto ? { auto: true } : {}),...(report.copies.length ? { copies: report.copies } : {}),...(origin ? { sideChatOf: origin } : {}),+ ...(report.featureAt !== undefined ? { featureAt: report.featureAt, labeled: report.labeled ?? 0 } : {}),+ ...(report.unknown ? { unknown: report.unknown } : {}),};- if (!report.replaced.length && !report.copies.length) return { body: original, report: summary };+ if (!report.replaced.length && !report.copies.length && report.featureAt === undefined) return { body: original, report: summary };json.input = items;const next = Buffer.from(JSON.stringify(json), "utf8");const body = encodeBody(next, encoding);@@ -325,7 +331,8 @@ export function rewriteBody(original: Buffer, encoding: string | undefined, thre// v0.4: a Claude Code session's requests are rewritten once something is unchecked there, or automatic selection was// used (copies the model fetched may be in its history from then on). v0.5: or once it was compacted with images-// before the compaction, which the user may keep sending.+// before the compaction, which the user may keep sending. v0.8: or once it has any image, for the feature line note+// and the ids (Claude Code sends the whole history every time, so this costs no upload; docs/v0.8/plan.md).export function claudeNeedsRewrite(sessionId: string | null, dir = claudeSelectionDirOf(), home = claudeHome()): boolean {if (!sessionId) return false;let index: ClaudeIndex | null = null;@@ -334,7 +341,7 @@ export function claudeNeedsRewrite(sessionId: string | null, dir = claudeSelecticonst selection = claudeSelection(sessionId, dir, index ? claudeStartedAt(index) : null);if (Object.keys(selection.unchecked).length > 0 || selection.autoSince) return true;} catch { /* the transcript still decides */ }- return !!index?.images.some((image) => image.compacted);+ return !!index?.images.length;}// The images a Claude Code request carried, by index key, with the base64 characters each took (the panel's@@ -385,14 +392,19 @@ export function rewriteClaudeBody(original: Buffer, encoding: string | undefined// v0.5: after a compaction, the images from before it go back right after the summary (claude-rewrite.ts): the ones// the user keeps sending (checked, or pinned with automatic selection) read from the transcript, the others as// placeholders. With any of those, the full note follows the summary, and later messages get only their short notes.- const compactedImages = index.images.filter((image) => image.compacted);+ // v0.8: each with what the model noted about it, for its placeholder.+ const compactedImages = index.images.filter((image) => image.compacted).map((image) => {+ const notes = index.notes.get(image.id);+ return notes ? { ...image, notes } : image;+ });const summaryIn = index.summary && compactedImages.length ? summaryAt(json.messages, index.summary) : null;const resend: Resend[] = summaryIn === null ? [] : compactedImages.filter((image) => !leaveOut.has(image.key)).flatMap((image) => {const data = claudeImageData(index, image);return data && image.mime ? [{ image, block: { type: "image", source: { type: "base64", media_type: image.mime, data } } }] : [];});const compactedOut = summaryIn !== null && resend.length < compactedImages.length;- const rewrote = rewriteMessages(json.messages, index, leaveOut, pixels, { lang, copies, noted: compactedOut, ...(selection.auto ? { auto: { currentTurn: current } } : {}) });+ // v0.8: feature lines in every main conversation (a subagent's requests do not come here).+ const rewrote = rewriteMessages(json.messages, index, leaveOut, pixels, { lang, copies, noted: compactedOut, features: true, ...(selection.auto ? { auto: { currentTurn: current } } : {}) });const report = rewrote.report;let messages = rewrote.messages;let compacted: Json | null = null;@@ -403,6 +415,7 @@ export function rewriteClaudeBody(original: Buffer, encoding: string | undefined// The note's place: after the summary when it holds placeholders; a later one moves down with a message added.if (compactedOut) report.noteAt = added.at + 1;else if (added.inserted && report.noteAt !== undefined && report.noteAt > added.at) report.noteAt += 1;+ if (added.inserted && report.featureAt !== undefined && report.featureAt > added.at) report.featureAt += 1;compacted = { at: added.at, listed: compactedImages.length, resent: resend.map(({ image }) => image.id) };}}@@ -416,8 +429,10 @@ export function rewriteClaudeBody(original: Buffer, encoding: string | undefined...(selection.auto ? { auto: true } : {}),...(report.copies.length ? { copies: report.copies } : {}),...(compacted ? { compacted } : {}),+ ...(report.featureAt !== undefined ? { featureAt: report.featureAt, labeled: report.labeled ?? 0 } : {}),+ ...(report.unknown ? { unknown: report.unknown } : {}),};- const changed = report.replaced.length > 0 || report.copies.length > 0 || compacted !== null;+ const changed = report.replaced.length > 0 || report.copies.length > 0 || compacted !== null || report.featureAt !== undefined;// Once the engine has sent the session a changed history, thinking made after it may be in every later request, even// with nothing left out now (an image checked again).if (!changed && !thinking?.edited) return { body: original, report: summary, attempts: [] };diff --git a/plugin/src/rewrite.ts b/plugin/src/rewrite.tsindex 579705a..9fc8102 100644--- a/plugin/src/rewrite.ts+++ b/plugin/src/rewrite.ts@@ -11,17 +11,24 @@// wording, and the model fetches what it needs with cam_view_image (whose words are here too, as text for the model).// A copy it fetched is sent in that turn only. Drafts of this wording were tried on the three GPT-6 models in Chinese// and English before it was built (spike/src/auto-fetch-selftest.ts).+// v0.8 (docs/v0.8/plan.md) — the model writes a feature line for each new image and notes the user's ratings, as record+// lines in its replies (notes.ts); a placeholder carries them, so the model still tells the images apart after a+// compaction. For that a feature line note goes before the turn of the first image, and every image sent says its id,+// also the ones new in this turn. The second line of a placeholder is shorter, offsetting the added text.// Input: request items, a lookup into the thread index, the keys to leave out. Output: new items plus a metadata report.import { FETCHED_STATE, findImages, type ImageKind, type ImageRef } from "./images.ts";import type { Lang } from "./language.ts";+import type { Notes, Rating } from "./notes.ts";type Json = Record<string, any>;-export type Described = { id: string; name: string | null; label: string | null; kind: ImageKind; turn: number | null; width: number | null; height: number | null };+// notes (v0.8): what the model noted about the image, shown in its placeholder.+export type Described = { id: string; name: string | null; label: string | null; kind: ImageKind; turn: number | null; width: number | null; height: number | null; notes?: Notes };export type Replacement = { id: string; key: string; kind: ImageKind; mode: "plain" | "duplicate"; sameAs: string | null; base64Chars: number };// noteAt: where the developer message explaining omitted images was inserted, if one was. copies: the ids whose copies-// fetched in an earlier turn were left out.-export type RewriteReport = { images: number; replaced: Replacement[]; locked: string[]; sentContentIds: string[]; copies: string[]; noteAt?: number };+// fetched in an earlier turn were left out. v0.8: featureAt, where the feature line note went; labeled, how many+// images sent got their id; unknown, how many images of the request the index did not know (e.g. not recorded yet).+export type RewriteReport = { images: number; replaced: Replacement[]; locked: string[]; sentContentIds: string[]; copies: string[]; noteAt?: number; featureAt?: number; labeled?: number; unknown?: number };type FetchWords = { title: string; description: string; ids: string; missing: (id: string) => string; unavailable: (id: string) => string; off: string };type Words = {@@ -36,6 +43,8 @@ type Words = {// v0.5, Claude Code only: after a compaction summary, the images from before it, which of them are not sent this// time, and how to get one back.compacted: (list: string, missing: string | null, auto: boolean) => string;+ // v0.8: how to write feature lines and note ratings; the placeholder's line with them; how long each may be shown.+ featureNote: string; notes: (line: string | null, ratings: Rating[]) => string; caps: { line: number; rating: number };};// The image sits in the history, so the model concludes it never saw it and takes back what it said about it (user@@ -54,7 +63,9 @@ const WORDS: Record<Lang, Words> = {included: "已提供",separator: ",",heading: (id, label, state, fields) => `[图片 ${id}${label ? `(${label})` : ""} ${state}|${fields.join("|")}]`,- plain: (id) => `原图被用户省略以节省上下文,情况见前面的“上下文管理说明”;需要重新看这张图时,在回复里写出“需要 ${id}”。`,+ // v0.8: shorter; the context management note says why the image is left out (it was "原图被用户省略以节省上下文,情况+ // 见前面的“上下文管理说明”;需要重新看这张图时,在回复里写出“需要 IMG-xxx”。").+ plain: (id) => `需要重看时,在回复里写“需要 ${id}”。`,duplicate: (id, where) => `这张图和 ${id}(${where})内容完全相同,那一条在本次请求里照常提供,请直接参考那一条。`,note: ["上下文管理说明(来自用户安装的上下文素材管理工具):为了节省上下文,用户取消勾选了这个任务里的部分图片。后面的对话里,这些图片原来的位置被换成了“[图片 IMG-xxx 已省略…]”这样的占位文字;仍在提供的图片前面标着“[图片 IMG-xxx 已提供…]”,紧接着就是原图。",@@ -62,7 +73,7 @@ const WORDS: Record<Lang, Words> = {"被问到现在能不能看到某张被省略的图片时,如实说明现在看不到(被用户省略了),并说明之前的描述仍然有效。现在看不到画面,就不要补充之前没说过的细节。需要重新看某张图时,在回复里原样写出“需要 IMG-xxx”(几张就写“需要 IMG-002、IMG-003”):工具靠这几个字认出你要的图。用户重新勾选后,原图会回到它在对话里原来的位置:那里的占位文字换成“[图片 IMG-xxx 已提供…]”和原图;它不会出现在用户的新消息里,所以不要让用户重新上传或附上图片。不要自己用工具去读取这些图片。","标着“[图片 IMG-xxx 已提供…]”的图片现在就在你面前,即使它之前被省略过、或者正是你要过的图:被问到它时,直接看图回答,不要说看不到。",].join("\n"),- autoPlain: (id) => `之前各轮的图片默认省略以节省上下文(自动选图),情况见前面的“上下文管理说明”;需要看这张图的原图时,调用 cam_view_image 工具,传入“${id}”。`,+ autoPlain: (id) => `需要原图时调用 cam_view_image,传入“${id}”。`,autoNote: ["上下文管理说明(来自用户安装的上下文素材管理工具):这个任务开着“自动选图”。为了节省上下文,之前各轮的图片默认不再随请求提供,后面的对话里,它们原来的位置换成了“[图片 IMG-xxx 已省略…]”这样的占位文字;本轮新出现的图片照常提供,用户固定发送的图片也照常提供,前面标着“[图片 IMG-xxx 已提供…]”,紧接着就是原图。","这些图片在它们出现的那一轮真实存在,你当时收到并看过;你之前对它们的描述,以及据此做出的判断,都是看着原图做出的,仍然有效。不要因为现在看不到,就认为之前的回答是猜测或错误,也不要收回或道歉。",@@ -105,6 +116,22 @@ const WORDS: Record<Lang, Words> = {unavailable: (id) => `${id} 的原图读不出来。`,off: "这个任务没有开自动选图:被省略的图片要由用户勾选。需要时在回复里写“需要 IMG-xxx”。",},+ // v0.8, the wording the user approved on 2026-10-19 (docs/v0.8/plan.md).+ featureNote: [+ "特征行说明(来自用户安装的上下文素材管理工具):这个任务里的每张图都有编号,写在图前面的“[图片 IMG-xxx …]”里。为了以后(包括对话压缩之后)还能认出每张图,请在每一轮最后那条回复的末尾(不要写在中途调用工具时说的话里),给这一轮新出现的每张图写一行特征,格式是:",+ "[//]: # (cam IMG-015 场景|看得见的特征)",+ "- 先写场景,再写看得见的特征(景别、主体、角度、光线、颜色、构图),一共 30 字以内,例如“[//]: # (cam IMG-015 深蓝雨夜|近景,正脸,霓虹反光)”。",+ "- 同一个场景沿用你之前用过的叫法。",+ "- 只写看得见的东西,不写“好看”“像谁”这类判断。每张图只写一次,以后不改。用户上传的、你生成的、你查看的、工具返回的图都要写;用 cam_view_image 取回的图和没有编号的图不用写。",+ "- 用户评价某张图时(包括隔了几轮才说),先确定是哪一张,再照用户的原话记一行,15 字以内,例如“[//]: # (cam IMG-003 评价:太暗)”。拿不准是哪一张就不记。",+ "- 这些行和正文之间空一行,一张图写一行;括号里不要用半角括号。用户看不到这些行,所以正文里不要提到它们,也不要说你在记录。",+ "- 以后被省略的图,占位里会带着你写的特征和记下的评价,对话压缩之后也还在。要选图时先看这些;拿不准时,先把原图要回来看(怎么要,见“上下文管理说明”),或者问用户是哪一张。",+ ].join("\n"),+ notes: (line, ratings) => [+ line ? `特征:${line}` : null,+ ratings.length ? `评价:${ratings.map((rating) => `${rating.turn !== null ? `第 ${rating.turn} 轮` : ""}“${rating.text}”`).join(",")}` : null,+ ].filter((part) => part !== null).join(";"),+ caps: { line: 30, rating: 15 },},en: {kind: { upload: "uploaded by the user", view: "viewed with a tool", generated: "generated image", tool: "image from a tool result" },@@ -117,7 +144,7 @@ const WORDS: Record<Lang, Words> = {included: "included",separator: ", ",heading: (id, label, state, fields) => `[Image ${id}${label ? ` (${label})` : ""} ${state} | ${fields.join(" | ")}]`,- plain: (id) => `The user left out the original image to save context; see the "Context management note" above. If you need to see this image again, write "need ${id}" in your reply.`,+ plain: (id) => `To see it again, write "need ${id}" in your reply.`,duplicate: (id, where) => `This image is identical to ${id} (${where}), which is included in this request as usual; refer to that one.`,note: ["Context management note (from the context asset manager the user installed): to save context, the user unchecked some images in this task. Further on in the conversation, where those images were, there is now placeholder text such as \"[Image IMG-xxx omitted…]\"; each image still included is marked \"[Image IMG-xxx included…]\", with the original image right after it.",@@ -125,7 +152,7 @@ const WORDS: Record<Lang, Words> = {"If asked whether you can see an omitted image now, say truthfully that you cannot see it now (the user left it out), and that your earlier descriptions remain valid. Since you cannot see it now, do not add details you did not mention before. If you need to look at an image again, write exactly \"need IMG-xxx\" in your reply (for several: \"need IMG-002, IMG-003\"): the tool finds the images you want by these words. Once the user checks an image again, its original is back in its old place in the conversation: the placeholder there becomes \"[Image IMG-xxx included…]\" followed by the original image. It does not come in the user's new message, so do not ask the user to upload or attach it again. Do not read these images yourself with tools.","An image marked \"[Image IMG-xxx included…]\" is in front of you now, even if it was omitted before or is one you asked for: when asked about it, look at it and answer from it, and do not say you cannot see it.",].join("\n"),- autoPlain: (id) => `Images from earlier turns are left out by default to save context (automatic image selection); see the "Context management note" above. To see the original of this image, call the cam_view_image tool with "${id}".`,+ autoPlain: (id) => `For the original, call cam_view_image with "${id}".`,autoNote: ["Context management note (from the context asset manager the user installed): this task has automatic image selection on. To save context, images from earlier turns are no longer included in requests by default: further on in the conversation, where those images were, there is now placeholder text such as \"[Image IMG-xxx omitted…]\". Images new in this turn are included as usual, and so are the images the user pinned, marked \"[Image IMG-xxx included…]\" with the original image right after it.","These images really existed in the turn where they appeared, and you received and looked at them then; what you said about them before, and the judgments you based on it, were made looking at the original images and remain valid. Do not treat your earlier answers as guesses or mistakes because you cannot see the images now, and do not take them back or apologize.",@@ -154,6 +181,21 @@ const WORDS: Record<Lang, Words> = {unavailable: (id) => `The original of ${id} could not be read.`,off: "Automatic image selection is off in this task: omitted images come back only when the user checks them. If you need one, write \"need IMG-xxx\" in your reply.",},+ featureNote: [+ "Feature line note (from the context asset manager the user installed): every image in this task has an ID, written in the \"[Image IMG-xxx …]\" line before it. So that each image can still be told apart later, also after the conversation is compacted, end the last reply of every turn (not what you say between tool calls) with one feature line for each image that is new in this turn, in this form:",+ "[//]: # (cam IMG-015 scene | visible features)",+ "- First the scene, then what can be seen (shot size, subject, angle, light, colors, composition), about 15 words at most, for example \"[//]: # (cam IMG-015 rainy blue night | close-up, facing the camera, neon reflections)\".",+ "- For a scene you named before, reuse that name.",+ "- Only what can be seen; no judgments such as \"beautiful\" or \"looks like someone\". One line per image, written once and never changed. Write one for the images the user uploaded, the ones you generated or viewed, and those in tool results; not for images you fetched with cam_view_image or images without an ID.",+ "- When the user rates an image, even turns later, first make sure which one it is, then note the user's own words in a line of about 8 words at most, for example \"[//]: # (cam IMG-003 rated: too dark)\". If you are not sure which image it is, do not note it.",+ "- Leave a blank line between these lines and your reply, one image per line, and no parentheses inside them. The user cannot see these lines, so do not mention them in your reply or say that you are noting anything.",+ "- Images left out later carry your feature line and the ratings you noted in their placeholders, also after a compaction. Check them when you pick an image; when unsure, get the original back first (the \"Context management note\" says how) or ask the user which one.",+ ].join("\n"),+ notes: (line, ratings) => [+ line ? `Features: ${line}` : null,+ ratings.length ? `rated: ${ratings.map((rating) => `"${rating.text}"${rating.turn !== null ? ` (turn ${rating.turn})` : ""}`).join(", ")}` : null,+ ].filter((part) => part !== null).join("; "),+ caps: { line: 100, rating: 50 },},};@@ -170,9 +212,28 @@ function heading(words: Words, image: Described, state: string): string {return words.heading(image.id, image.label, state, fields);}+// v0.8: a feature line or rating longer than the note allows is cut after the last punctuation mark (or space) that+// still fits, with "…"; the model is asked to keep them short, and the panel searches the full text.+const CLIP_AT = /[,。;、!?,.;!?||::\s]/u;+export function clip(text: string, max: number): string {+ const chars = [...text.trim()];+ if (chars.length <= max) return chars.join("");+ let cut = max;+ for (let i = max - 1; i > 0; i--) if (CLIP_AT.test(chars[i])) { cut = i; break; }+ return `${chars.slice(0, cut).join("").trimEnd()}…`;+}++// v0.8: the line under a placeholder's heading: the model's feature line and the latest three ratings it noted, with+// their turns; null when it noted nothing (e.g. an image from before v0.8).+function notesLine(words: Words, image: Described): string | null {+ const { line = null, ratings = [] } = image.notes ?? {};+ if (!line && !ratings.length) return null;+ return words.notes(line ? clip(line, words.caps.line) : null, ratings.slice(-3).map((rating) => ({ ...rating, text: clip(rating.text, words.caps.rating) })));+}+export function plainPlaceholder(image: Described, lang: Lang = "zh"): string {const words = WORDS[lang];- return `${heading(words, image, words.omitted)}\n${words.plain(image.id)}`;+ return [heading(words, image, words.omitted), notesLine(words, image), words.plain(image.id)].filter((line) => line !== null).join("\n");}export function duplicatePlaceholder(image: Described, same: Described, lang: Lang = "zh"): string {@@ -216,9 +277,12 @@ export function compactedNote(images: Described[], sent: Set<string>, lang: Langexport function autoPlaceholder(image: Described, lang: Lang = "zh"): string {const words = WORDS[lang];- return `${heading(words, image, words.omitted)}\n${words.autoPlain(image.id)}`;+ return [heading(words, image, words.omitted), notesLine(words, image), words.autoPlain(image.id)].filter((line) => line !== null).join("\n");}+// v0.8: the feature line note, for a task's own requests (not a side chat's or a subagent's).+export const featureNote = (lang: Lang) => WORDS[lang].featureNote;+// The line cam_view_image puts before each image it returns. Without Codex's own label ("[Image #3]"), which numbers// images within their message and means nothing here: GPT-6 Luna once read it as the digit asked about.export function fetchedLabel(image: Described, lang: Lang = "zh"): string {@@ -232,6 +296,9 @@ export type RewriteOptions = {// Automatic selection: its wording; and a placeholder points to, and a label marks, only images of earlier turns// (the pinned ones), which stay sent through the turn, so a copy fetched mid-turn changes nothing before it.auto?: { currentTurn: number };+ // v0.8: the feature line note before the turn of the first image, and an id label on every image sent, also the ones+ // new in this turn (the model notes them by id). A duplicate placeholder still points only to earlier turns' images.+ features?: boolean;};export function rewriteItems(@@ -241,7 +308,7 @@ export function rewriteItems(pixelHash: (ref: ImageRef) => string | null,options: Lang | RewriteOptions = "zh",): { items: Json[]; report: RewriteReport } {- const { lang = "zh", copies = new Map<string, string>(), auto }: RewriteOptions = typeof options === "string" ? { lang: options } : options;+ const { lang = "zh", copies = new Map<string, string>(), auto, features = false }: RewriteOptions = typeof options === "string" ? { lang: options } : options;const refs = findImages(items);const report: RewriteReport = { images: refs.length, replaced: [], locked: [], sentContentIds: [], copies: [] };const copied = refs.filter((ref) => copies.has(ref.key) && ref.part !== null);@@ -249,7 +316,11 @@ export function rewriteItems(for (const ref of refs) if (unchecked.has(ref.key) && !ref.replaceable) report.locked.push(describe(ref)?.id ?? ref.key);const sent = refs.filter((ref) => !drop.includes(ref) && !copied.includes(ref));report.sentContentIds = sent.map((ref) => ref.contentId);- if (!drop.length && !copied.length) return { items, report };+ const unknown = refs.filter((ref) => !copied.includes(ref) && !describe(ref)).length;+ if (unknown) report.unknown = unknown;+ // v0.8: the first image the index knows; the feature line note goes before its turn.+ const first = features ? refs.find((ref) => describe(ref)) : undefined;+ if (!drop.length && !copied.length && !first) return { items, report };const earlier = (ref: ImageRef) => { const turn = describe(ref)?.turn; return auto !== undefined && turn !== null && turn !== undefined && turn < auto.currentTurn; };const lasting = auto ? sent.filter(earlier) : sent;@@ -278,9 +349,12 @@ export function rewriteItems(report.copies.push(copies.get(ref.key)!);}// v0.1-25: a checked image sits in its old place among placeholders, so every image still sent is marked with its id.- for (const ref of lasting) {+ // v0.8: with feature lines, this turn's new images too.+ for (const ref of features ? sent : lasting) {const image = ref.replaceable && ref.part !== null ? describe(ref) : undefined;- if (image) edit(ref.item, ref.openTag ?? ref.part!, 0, includedLabel(image, lang));+ if (!image) continue;+ edit(ref.item, ref.openTag ?? ref.part!, 0, includedLabel(image, lang));+ report.labeled = (report.labeled ?? 0) + 1;}const next = items.map((item, index) => {const list = edits.get(index);@@ -291,12 +365,21 @@ export function rewriteItems(return { ...item, [field]: parts };});// Only plain placeholders need the note (a duplicate's content is still in view). It goes before the user message- // that opens that turn, so it never splits a tool call from its output.- if (firstPlain !== null) {- let at = firstPlain;+ // that opens that turn, so it never splits a tool call from its output. v0.8: the feature line note likewise, before+ // the turn of the first image: at or before the other one (the first image comes no later than the first omitted).+ const turnStart = (from: number) => {+ let at = from;while (at > 0 && !(next[at].type === "message" && next[at].role === "user")) at--;- next.splice(at, 0, { type: "message", role: "developer", content: [{ type: "input_text", text: auto ? WORDS[lang].autoNote : WORDS[lang].note }] });- report.noteAt = at;+ return at;+ };+ const developer = (text: string) => ({ type: "message", role: "developer", content: [{ type: "input_text", text }] });+ const noteAt = firstPlain !== null ? turnStart(firstPlain) : null;+ const featureAt = first ? turnStart(first.item) : null;+ if (noteAt !== null) next.splice(noteAt, 0, developer(auto ? WORDS[lang].autoNote : WORDS[lang].note));+ if (featureAt !== null) {+ next.splice(featureAt, 0, developer(WORDS[lang].featureNote));+ report.featureAt = featureAt;}+ if (noteAt !== null) report.noteAt = noteAt + (featureAt !== null ? 1 : 0);return { items: next, report };}diff --git a/plugin/src/thread-index.ts b/plugin/src/thread-index.tsindex 2e10e0d..ddc4382 100644--- a/plugin/src/thread-index.ts+++ b/plugin/src/thread-index.ts@@ -10,19 +10,21 @@// a task the user has archived since. v0.1-13 — if that page is gone for good (its task deleted), the rest is read and// the gap reported, instead of failing.// v0.3 — an image the model fetched with our tool (cam_view_image) is kept apart as a copy of the original: no id.+// v0.8 — what the model noted about the images in record lines at the end of its replies (notes.ts), by image id.// Input: the sessions directory and a thread id. Output: ThreadIndex (in memory; files are parsed incrementally).import { closeSync, existsSync, mkdirSync, openSync, readdirSync, readFileSync, readSync, statSync, writeFileSync } from "node:fs";import { basename, dirname, join } from "node:path";import { zstdDecompressSync } from "node:zlib";import { findImages, type ImageRef } from "./images.ts";+import { addNotes, type Notes } from "./notes.ts";import { decodePng } from "./png.ts";type Json = Record<string, any>;export type IndexedImage = ImageRef & { id: string; turn: number | null; bytes: number; pixelSha256: string | null };// v0.3: an image the model fetched with our tool, a copy of `of` sent in its own turn only (spec: automatic selection).export type FetchedCopy = { key: string; of: string; turn: number | null };-export type ThreadIndex = { threadId: string; images: IndexedImage[]; byKey: Map<string, IndexedImage>; turns: number; turnNumbers: Map<string, number>; copies: Map<string, FetchedCopy> };+export type ThreadIndex = { threadId: string; images: IndexedImage[]; byKey: Map<string, IndexedImage>; turns: number; turnNumbers: Map<string, number>; copies: Map<string, FetchedCopy>; notes: Map<string, Notes> };export type Parsed = { offset: number; type: string; payload: Json; timestamp: string | null };type FileCache = { size: number; mtimeMs: number; parsedTo: number; records: Parsed[] };@@ -223,13 +225,22 @@ export function buildIndex(threadId: string, history: Parsed[]): ThreadIndex {const turnNumbers = new Map<string, number>();const items: Json[] = [];const activeTurn: Array<string | null> = [];+ const notes = new Map<string, Notes>();let current: string | null = null;for (const record of history) {if (record.type === "event_msg" && record.payload.type === "task_started" && record.payload.turn_id) {current = record.payload.turn_id;if (!turnNumbers.has(current!)) turnNumbers.set(current!, turnNumbers.size + 1);}- if (record.type === "response_item") { items.push(record.payload); activeTurn.push(current); }+ if (record.type !== "response_item") continue;+ items.push(record.payload);+ activeTurn.push(current);+ // v0.8: only the model's own replies.+ const { type, role, content } = record.payload;+ if (type === "message" && role === "assistant" && Array.isArray(content)) {+ const turn = current ? turnNumbers.get(current) ?? null : null;+ for (const part of content) if (part?.type === "output_text" && typeof part.text === "string") addNotes(notes, part.text, turn);+ }}const images: IndexedImage[] = [];const byKey = new Map<string, IndexedImage>();@@ -256,7 +267,7 @@ export function buildIndex(threadId: string, history: Parsed[]): ThreadIndex {const twin = images.find((other) => other.name && (other.contentId === entry.contentId || (entry.pixelSha256 !== null && other.pixelSha256 === entry.pixelSha256)));if (twin) entry.name = twin.name;}- return { threadId, images, byKey, turns: turnNumbers.size, turnNumbers, copies };+ return { threadId, images, byKey, turns: turnNumbers.size, turnNumbers, copies, notes };}// The first line alone (session_meta can be long), read in chunks without parsing the rest of the file.@@ -365,6 +376,22 @@ export function sessionMetaOf(sessionsDir: string, threadId: string): Json | nul});}+const noteTakers = new Map<string, boolean>();++// v0.8: whether a thread's model writes feature lines (docs/v0.8/plan.md): the user's own tasks and their forks, not a+// subagent (started with history or without) or one of Codex's internal tasks. Without a first line yet, yes, and not+// cached: a subagent's first request can come before its rollout is written (v0.7); a user's task's rarely does.+export function writesNotes(sessionsDir: string, threadId: string): boolean {+ const key = `${sessionsDir}\0${threadId}`;+ if (noteTakers.has(key)) return noteTakers.get(key)!;+ const meta = sessionMetaOf(sessionsDir, threadId);+ if (!meta) return true;+ const source = meta.source;+ const own = !(source && typeof source === "object" && ("subagent" in source || "internal" in source));+ noteTakers.set(key, own);+ return own;+}+// Threads whose rollout was first written at or after `since` (ms), with their session_meta, newest last. Only// today's and yesterday's folders are looked at. For a thread started by a message, the first line is written when// that message is sent (Codex writes rollouts lazily), so its timestamp tells when the thread started.diff --git a/spike/src/claude-panel.test.ts b/spike/src/claude-panel.test.tsindex c14da91..adb6480 100644--- a/spike/src/claude-panel.test.ts+++ b/spike/src/claude-panel.test.ts@@ -111,7 +111,8 @@ test("v0.6: a session started after the default began has automatic selection onassert.equal(get(`/__cam/claude/panel?session=${session}`, fresh).body.auto, false);const old = started("2026-10-12T23:00:00Z");assert.equal(get(`/__cam/claude/panel?session=${session}`, old).body.auto, false);- assert.equal(claudeNeedsRewrite(session, claudeSelectionDirOf(old.dataRoot), old.home), false);+ // v0.8: still rewritten, as every session with images is (the feature line note), though nothing is left out.+ assert.equal(claudeNeedsRewrite(session, claudeSelectionDirOf(old.dataRoot), old.home), true);});test("the panel page and API answer only requests naming the engine by a loopback name, from no other site", () => {diff --git a/spike/src/notes.test.ts b/spike/src/notes.test.tsnew file mode 100644index 0000000..afd8119--- /dev/null+++ b/spike/src/notes.test.ts@@ -0,0 +1,172 @@+// Purpose: v0.8 — feature lines and ratings (docs/v0.8/plan.md): the record lines read from the model's own replies+// in both indexes, how a placeholder shows them, the feature line note's place, and ids on this turn's new images.+// Input: synthetic records, transcripts and request items; output: Node test assertions only.++import assert from "node:assert/strict";+import { mkdirSync, mkdtempSync, writeFileSync } from "node:fs";+import { tmpdir } from "node:os";+import { join } from "node:path";+import test from "node:test";+import { buildClaudeIndex } from "../../plugin/src/claude-index.ts";+import { rewriteMessages } from "../../plugin/src/claude-rewrite.ts";+import { findImages, type ImageRef } from "../../plugin/src/images.ts";+import { addNotes, sceneOf, type Notes } from "../../plugin/src/notes.ts";+import { autoPlaceholder, clip, featureNote, plainPlaceholder, rewriteItems, type Described } from "../../plugin/src/rewrite.ts";+import { buildIndex, writesNotes, type Parsed } from "../../plugin/src/thread-index.ts";+import { image, pngA, pngB, text, transcript } from "./claude-fixtures.ts";+import { png } from "./testkit.ts";++const notesOf = (text: string, turn: number | null = 1) => {+ const notes = new Map<string, Notes>();+ addNotes(notes, text, turn);+ return notes;+};++test("record lines: a feature line per image, written once; ratings pile up with their turns", () => {+ const notes = notesOf([+ "两张都生成好了。",+ "",+ "[//]: # (cam IMG-001 深蓝雨夜|近景,正脸,霓虹反光)",+ "[//]: # (cam IMG-002 暖色灯光|中景,侧脸,逆光)",+ "[//]: # (cam IMG-001 深蓝雨夜|别的写法)",+ "[//]: # (cam IMG-002 评价:太暗)",+ ].join("\n"), 3);+ addNotes(notes, "[//]: # (cam IMG-002 评价: 脸最好)\n [//]: # (cam IMG-003 rated: too dark )", 5);+ assert.deepEqual(notes.get("IMG-001"), { line: "深蓝雨夜|近景,正脸,霓虹反光", ratings: [] });+ assert.deepEqual(notes.get("IMG-002"), { line: "暖色灯光|中景,侧脸,逆光", ratings: [{ turn: 3, text: "太暗" }, { turn: 5, text: "脸最好" }] });+ assert.deepEqual(notes.get("IMG-003"), { line: null, ratings: [{ turn: 5, text: "too dark" }] });+ // Not a record line: inside other text, ASCII parentheses inside, no id, more than three spaces before it.+ const none = notesOf("看这里 [//]: # (cam IMG-004 x)\n[//]: # (cam IMG-005 带(括号)的)\n[//]: # (cam 没有编号)\n [//]: # (cam IMG-006 缩进太多)");+ assert.equal(none.size, 0);+ assert.equal(sceneOf("深蓝雨夜|近景,正脸"), "深蓝雨夜");+ assert.equal(sceneOf("rainy blue night | close-up"), "rainy blue night");+ assert.equal(sceneOf(null), null);+});++test("a long feature line or rating is cut at the last punctuation mark that fits", () => {+ assert.equal(clip("深蓝雨夜|近景,正脸", 30), "深蓝雨夜|近景,正脸");+ assert.equal(clip("深蓝雨夜|近景,正脸,霓虹反光,撑着一把透明的伞,背后是很长很长的街道和招牌", 30), "深蓝雨夜|近景,正脸,霓虹反光,撑着一把透明的伞…");+ assert.equal(clip("一二三四五六七八九十一二三四五六", 15), "一二三四五六七八九十一二三四五…");+ assert.equal(clip("rainy blue night | close-up, facing the camera", 30), "rainy blue night | close-up,…");+});++const record = (type: string, payload: Record<string, any>): Parsed => ({ offset: 0, type, payload, timestamp: null });+const url = (bytes: Buffer) => `data:image/png;base64,${bytes.toString("base64")}`;+const red = png(4, 4, () => [255, 0, 0]);+const blue = png(4, 4, () => [0, 0, 255]);++test("the Codex index reads record lines from the model's replies only", () => {+ const history = [+ record("event_msg", { type: "task_started", turn_id: "t1" }),+ record("response_item", { type: "message", id: "m1", role: "user", content: [{ type: "input_image", image_url: url(red) }, { type: "input_text", text: "[//]: # (cam IMG-001 用户自己写的)" }] }),+ record("response_item", { type: "custom_tool_call", call_id: "c1", name: "exec", input: "x" }),+ record("response_item", { type: "custom_tool_call_output", id: "o1", call_id: "c1", output: [{ type: "input_text", text: "[//]: # (cam IMG-001 网页里藏的)" }] }),+ record("response_item", { type: "message", id: "m2", role: "assistant", content: [{ type: "output_text", text: "好的。\n\n[//]: # (cam IMG-001 深蓝雨夜|近景)" }] }),+ record("event_msg", { type: "task_started", turn_id: "t2" }),+ record("response_item", { type: "message", id: "m3", role: "assistant", content: [{ type: "output_text", text: "[//]: # (cam IMG-001 评价:太暗)" }] }),+ ];+ const index = buildIndex("thread", history);+ assert.deepEqual(index.notes.get("IMG-001"), { line: "深蓝雨夜|近景", ratings: [{ turn: 2, text: "太暗" }] });+});++test("the Claude index reads record lines from the assistant's text, with the turn", () => {+ const t = transcript();+ t.prompt([image(pngA), text("[//]: # (cam IMG-001 用户自己写的)")]);+ t.assistant(text("A red square.\n\n[//]: # (cam IMG-001 red square | flat, centered)"));+ t.prompt("Is it too dark?");+ t.assistant(text("[//]: # (cam IMG-001 rated: too dark)"));+ const index = buildClaudeIndex("s", t.records);+ assert.deepEqual(index.notes.get("IMG-001"), { line: "red square | flat, centered", ratings: [{ turn: 2, text: "too dark" }] });+});++const tagged = (name: string, n: number, bytes: Buffer) => [+ { type: "input_text", text: `<image name=[Image #${n}] path="C:\\work\\${name}">` },+ { type: "input_image", image_url: url(bytes), detail: "high" },+ { type: "input_text", text: "</image>" },+];+// Turn 1: a.png; turn 2: b.png, the request's current turn.+const turns = () => [+ { type: "message", id: "dev", role: "developer", content: [{ type: "input_text", text: "instructions" }] },+ { type: "message", id: "u1", role: "user", content: [{ type: "input_text", text: "第一张" }, ...tagged("a.png", 1, red)] },+ { type: "message", id: "a1", role: "assistant", content: [{ type: "output_text", text: "收到" }] },+ { type: "message", id: "u2", role: "user", content: [{ type: "input_text", text: "第二张" }, ...tagged("b.png", 1, blue)] },+];+const A_NOTES: Notes = { line: "深蓝雨夜|近景,正脸", ratings: [{ turn: 1, text: "一" }, { turn: 2, text: "二" }, { turn: 3, text: "三" }, { turn: 4, text: "脸最好" }] };+function describer(list: Array<Record<string, any>>, notes: Record<string, Notes> = {}) {+ const refs = findImages(list);+ const byKey = new Map(refs.map((ref, i) => {+ const id = `IMG-00${i + 1}`;+ return [ref.key, { id, name: ref.name, label: ref.label, kind: ref.kind, turn: i + 1, width: ref.width, height: ref.height, ...(notes[id] ? { notes: notes[id] } : {}) } satisfies Described];+ }));+ return { describe: (ref: ImageRef) => byKey.get(ref.key), key: (id: string) => [...byKey].find(([, v]) => v.id === id)![0] };+}+const texts = (item: Record<string, any>): string[] => item.content.map((part: Record<string, any>) => part.text ?? part.type);++test("a placeholder carries the feature line and the latest three ratings, with their turns", () => {+ const image: Described = { id: "IMG-001", name: "a.png", label: null, kind: "upload", turn: 1, width: 4, height: 4, notes: A_NOTES };+ assert.equal(autoPlaceholder(image), "[图片 IMG-001 已省略|a.png|用户上传|第 1 轮|4×4]\n特征:深蓝雨夜|近景,正脸;评价:第 2 轮“二”,第 3 轮“三”,第 4 轮“脸最好”\n需要原图时调用 cam_view_image,传入“IMG-001”。");+ assert.equal(plainPlaceholder(image, "en"), "[Image IMG-001 omitted | a.png | uploaded by the user | turn 1 | 4×4]\nFeatures: 深蓝雨夜|近景,正脸; rated: \"二\" (turn 2), \"三\" (turn 3), \"脸最好\" (turn 4)\nTo see it again, write \"need IMG-001\" in your reply.");+ assert.equal(autoPlaceholder({ ...image, notes: undefined }), "[图片 IMG-001 已省略|a.png|用户上传|第 1 轮|4×4]\n需要原图时调用 cam_view_image,传入“IMG-001”。", "nothing noted: no line");+});++test("with feature lines, the note goes before the first image's turn and this turn's new image says its id", () => {+ const input = turns();+ const { describe, key } = describer(input, { "IMG-001": A_NOTES });+ // Turn 2 with automatic selection: turn 1's a.png is left out, b.png is new.+ const { items: out, report } = rewriteItems(input, describe, new Set([key("IMG-001")]), () => null, { auto: { currentTurn: 2 }, features: true });+ assert.deepEqual(out.map((item) => item.role ?? item.type), ["developer", "developer", "developer", "user", "assistant", "user"]);+ assert.equal(out[1].content[0].text, featureNote("zh"));+ assert.match(out[2].content[0].text, /^上下文管理说明(来自用户安装的上下文素材管理工具):这个任务开着“自动选图”/);+ assert.deepEqual([report.featureAt, report.noteAt, report.labeled], [1, 2, 1]);+ assert.match(texts(out[3])[1], /^\[图片 IMG-001(\[Image #1\]) 已省略|a\.png|用户上传|第 1 轮|4×4\]\n特征:深蓝雨夜|近景,正脸;/);+ assert.deepEqual(texts(out[5]).slice(0, 3), ["第二张", "[图片 IMG-002([Image #1]) 已提供|b.png|用户上传|第 2 轮|4×4]", `<image name=[Image #1] path="C:\\work\\b.png">`]);+ // Turn 1's own request, nothing left out: the note and a.png's id only.+ const first = turns().slice(0, 2);+ const one = rewriteItems(first, describer(first).describe, new Set(), () => null, { auto: { currentTurn: 1 }, features: true });+ assert.deepEqual(one.items.map((item) => item.role), ["developer", "developer", "user"]);+ assert.deepEqual([one.report.replaced, one.report.featureAt, one.report.noteAt, one.report.labeled], [[], 1, undefined, 1]);+ // Without feature lines (a side chat, a subagent) all is as before: nothing to do, the same items back.+ assert.equal(rewriteItems(first, describer(first).describe, new Set(), () => null, { auto: { currentTurn: 1 } }).items, first);+ // An image the index does not know yet is sent as it is, and counted.+ const unknown = rewriteItems(first, () => undefined, new Set(), () => null, { features: true });+ assert.deepEqual([unknown.items === first, unknown.report.unknown], [true, 1]);+});++test("Claude: the feature line note follows the message holding the first image, joining a note already there", () => {+ const t = transcript();+ t.prompt([image(pngA), text("first")]);+ t.assistant(text("A red square."));+ const p2 = t.prompt([image(pngB), text("second")]);+ const index = buildClaudeIndex("s", t.records);+ const messages = [+ { role: "user", content: [image(pngA), text("first")] },+ { role: "assistant", content: [text("A red square.")] },+ { role: "user", content: [image(pngB), text("second")] },+ ];+ const plain = rewriteMessages(messages, index, new Set(), () => null, { features: true });+ assert.deepEqual(plain.messages.map((message) => message.role), ["user", "system", "assistant", "user"]);+ assert.equal(plain.messages[1].content, featureNote("zh"));+ assert.deepEqual([plain.report.featureAt, plain.report.labeled], [1, 2]);+ assert.match(plain.messages[3].content[0].text, /^\[图片 IMG-002 已提供|未命名|用户上传|第 2 轮|8×8\]$/);+ // B unchecked: the full note and its short note follow its message; the feature line note stays after A's.+ const some = rewriteMessages(messages, index, new Set([`${p2}#0`]), () => null, { features: true });+ assert.deepEqual(some.messages.map((message) => message.role), ["user", "system", "assistant", "user", "system"]);+ assert.deepEqual([some.report.featureAt, some.report.noteAt], [1, 4]);+});++test("a subagent's model writes no feature lines; the user's own task does, also before its rollout's first line", () => {+ const sessions = mkdtempSync(join(tmpdir(), "cam-notes-sessions-"));+ const day = join(sessions, "2026", "10", "19");+ mkdirSync(day, { recursive: true });+ const write = (id: string, meta: Record<string, any>) => writeFileSync(join(day, `rollout-2026-10-19T10-00-00-${id}.jsonl`), `${JSON.stringify({ type: "session_meta", payload: { id, ...meta } })}\n`);+ const own = "01a15100-0000-7000-8000-000000000001";+ const sub = "01a15100-0000-7000-8000-000000000002";+ const forkedSub = "01a15100-0000-7000-8000-000000000003";+ write(own, { source: "vscode" });+ write(sub, { source: { subagent: { thread_spawn: { parent_thread_id: own, depth: 1 } } }, parent_thread_id: own });+ write(forkedSub, { source: { subagent: { thread_spawn: { parent_thread_id: own } } }, forked_from_id: own });+ assert.equal(writesNotes(sessions, own), true);+ assert.equal(writesNotes(sessions, sub), false);+ assert.equal(writesNotes(sessions, forkedSub), false);+ assert.equal(writesNotes(sessions, "01a15100-0000-7000-8000-000000000009"), true, "no rollout yet");+});diff --git a/spike/src/proxy.test.ts b/spike/src/proxy.test.tsindex ba501f8..52e53e5 100644--- a/spike/src/proxy.test.ts+++ b/spike/src/proxy.test.ts@@ -129,7 +129,8 @@ test("rewriteBody swaps the unchecked image for a placeholder and re-encodes thetest("rewriteBody forwards the original bytes when nothing applies or the body is not safe to re-serialize", () => {const { threadId, sessions, selections, body } = fixture();writeSelection({ threadId, unchecked: { "msg_other#0": { id: "IMG-009", at: "2026-09-24T10:00:00Z" } } }, selections);- const original = body();+ // v0.8: a request with images gets the feature line note (next test), so nothing applies to one without any.+ const original = zlib.zstdCompressSync(Buffer.from('{"model":"m","input":[{"type":"message","role":"user","content":[{"type":"input_text","text":"hi"}]}],"stream":true}'));assert.equal(rewriteBody(original, "zstd", threadId, sessions, selections).body, original);writeSelection({ threadId, unchecked: { "msg_1#0": { id: "IMG-001", at: "2026-09-24T10:00:00Z" } } }, selections);const risky = body(',"big":12345678901234567');@@ -151,11 +152,17 @@ test("with automatic selection, rewriteBody leaves out earlier turns' images thaconst text = zlib.zstdDecompressSync(out).toString("utf8");assert.ok(!text.includes(red.slice(30)), "turn 1's IMG-001 is left out");assert.ok(text.includes(blue.slice(30)), "the pinned IMG-002 is still sent");- assert.match(text, /\[图片 IMG-001(\[Image #1\]) 已省略|a\.png|用户上传|第 1 轮|64×64\]\\n之前各轮的图片默认省略/);+ assert.match(text, /\[图片 IMG-001(\[Image #1\]) 已省略|a\.png|用户上传|第 1 轮|64×64\]\\n需要原图时调用 cam_view_image/);assert.match(text, /\[图片 IMG-002(\[Image #2\]) 已提供|b\.png/);assert.match(text, /上下文管理说明(来自用户安装的上下文素材管理工具):这个任务开着“自动选图”/);assert.deepEqual([report.auto, report.replaced.map((r: Record<string, unknown>) => [r.id, r.mode])], [true, [["IMG-001", "plain"]]]);- assert.equal(rewriteBody(original, "zstd", threadId, sessions, selections, "t1").body, original, "turn 1's own requests send its images");+ // Turn 1's own requests send its images, each with its id now (v0.8), under the feature line note.+ const own = rewriteBody(original, "zstd", threadId, sessions, selections, "t1");+ const ownText = zlib.zstdDecompressSync(own.body).toString("utf8");+ assert.ok(ownText.includes(red.slice(30)) && ownText.includes(blue.slice(30)), "turn 1's own requests send its images");+ assert.deepEqual([own.report.replaced, own.report.featureAt, own.report.labeled], [[], 0, 2]);+ assert.match(ownText, /特征行说明(来自用户安装的上下文素材管理工具)/);+ assert.match(ownText, /\[图片 IMG-001(\[Image #1\]) 已提供|a\.png/);});// v0.4: Claude Code through the same engine.@@ -212,7 +219,7 @@ test("the panel's last request is the session's conversation, not Claude Desktopassert.equal(claudeConversationRequest(describeBody("/backend-api/codex/responses", { input: [] })), false);});-test("a Claude Code request is rewritten from the session's transcript and selection; nothing to do keeps the bytes", async () => {+test("a Claude Code request is rewritten from the session's transcript and selection, with images also with nothing unchecked", async () => {const { usualSession, usualRequest } = await import("./claude-fixtures.ts");const { claudeNeedsRewrite, rewriteClaudeBody, claudeImageSizes } = await import("../../plugin/src/proxy.ts");const { buildClaudeIndex } = await import("../../plugin/src/claude-index.ts");@@ -223,8 +230,12 @@ test("a Claude Code request is rewritten from the session's transcript and selecmkdirSync(join(home, "projects", "D--work"), { recursive: true });writeFileSync(join(home, "projects", "D--work", `${session}.jsonl`), t.records.map((record) => JSON.stringify(record)).join("\n") + "\n");const body = Buffer.from(JSON.stringify({ model: "claude-haiku-5-5", thinking: { type: "adaptive" }, messages: usualRequest() }));- assert.equal(claudeNeedsRewrite(session, dir), false);- assert.equal(rewriteClaudeBody(body, undefined, session, dir, home, {}).body, body);+ // v0.8: a session with images is rewritten with nothing unchecked too: the feature line note follows the first+ // image's message, and every image says its id.+ assert.equal(claudeNeedsRewrite(session, dir, home), true);+ const plain = rewriteClaudeBody(body, undefined, session, dir, home, {});+ assert.deepEqual([plain.report.replaced, plain.report.featureAt, plain.report.labeled], [[], 1, 4]);+ assert.equal(JSON.parse(plain.body.toString("utf8")).messages[1].role, "system");writeSelection({ threadId: session, unchecked: { [`${p1}#1`]: { id: "IMG-002", at: "2026-10-10T00:00:00Z" } } }, dir);assert.equal(claudeNeedsRewrite(session, dir), true);const out = rewriteClaudeBody(body, undefined, session, dir, home, {});@@ -278,20 +289,24 @@ test("an account that refuses thinking after an edited history gets the rewriteassert.deepEqual(steps(strict), [[null, null, { kept: 0, left: 1 }], [null, "thinking-signature", null]]);const went = strict.attempts[0];recordSent(record, went.history!, went.sent!, went.made);- // The next request carries thinking made after that; with the image checked again it no longer fits, while the- // thinking made before the edit fits again. Should the record be wrong, the request goes without any thinking.+ // The next request carries thinking made after that; with the image checked again it no longer fits. v0.8: neither+ // does the thinking made before the edit, which v0.7 put back: with the feature line note and the ids the history+ // never goes back to how Claude Code built it (in a session that had them from its first image, that thinking was+ // made after them, and fits). Should the record be wrong, the request goes as built.const next = [...usualRequest(), { role: "assistant", content: [{ type: "thinking", thinking: "", signature: "sig-9" }, text("Blue.")] }, { role: "user", content: [text("More?")] }];writeSelection({ threadId: session, unchecked: {} }, dir);const again = rewriteClaudeBody(request(next), undefined, session, dir, home, {}, record);- assert.deepEqual(steps(again), [[null, null, { kept: 1, left: 1 }], ["thinking-signature", null, { kept: 0, left: 2 }], [null, null, null]]);- assert.deepEqual(again.attempts.map((attempt: any) => attempt.sure), [true, false, false]);+ assert.deepEqual(steps(again), [[null, null, { kept: 0, left: 2 }], [null, "thinking-signature", null]]);+ assert.deepEqual(again.attempts.map((attempt: any) => attempt.sure), [true, false]);const sent = JSON.parse(again.attempts[0].body.toString("utf8")).messages;- assert.deepEqual(sent[1].content.map((block: any) => block.type), ["thinking", "text"]);- assert.deepEqual(sent[9].content.map((block: any) => block.type), ["text"]);- // An account not known to check: the request as Claude Code built it first.+ assert.match(sent[1].content, /^特征行说明/);+ assert.deepEqual(sent[2].content.map((block: any) => block.type), ["text"]);+ assert.deepEqual(sent[10].content.map((block: any) => block.type), ["text"]);+ // An account not known to check: the rewrite with all its thinking first, the request as built last.const lenient = rewriteClaudeBody(request(next), undefined, session, dir, home, {}, { ...record, strict: false });- assert.deepEqual(steps(lenient), [[null, null, null], ["thinking-signature", null, { kept: 1, left: 1 }], ["thinking-signature", null, { kept: 0, left: 2 }]]);- assert.equal(lenient.attempts[0].body.equals(request(next)), true);+ assert.deepEqual(steps(lenient), [[null, null, null], ["thinking-signature", null, { kept: 0, left: 2 }], [null, "thinking-signature", null]]);+ assert.equal(lenient.attempts[0].body.equals(request(next)), false);+ assert.equal(lenient.attempts.at(-1)!.body.equals(request(next)), true);// Another image unchecked: nothing made so far fits.writeSelection({ threadId: session, unchecked: { [`${p1}#0`]: { id: "IMG-001", at: "2026-10-10T00:00:00Z" } } }, dir);const other = rewriteClaudeBody(request(next), undefined, session, dir, home, {}, record);@@ -325,8 +340,8 @@ test("v0.5: after a compaction, the images from before it go back after the summmkdirSync(join(home, "projects", "D--work"), { recursive: true });const file = join(home, "projects", "D--work", `${session}.jsonl`);writeFileSync(file, t.records.map((record) => JSON.stringify(record)).join("\n") + "\n");- // Before the compaction, with nothing unchecked: nothing to look at.- assert.equal(claudeNeedsRewrite(session, dir, home), false);+ // Before the compaction, with nothing unchecked: rewritten only for the feature line note and the ids (v0.8).+ assert.equal(claudeNeedsRewrite(session, dir, home), true);t.boundary();t.summary();const p3 = t.prompt([image(pngB), text("And now?")]);@@ -361,7 +376,9 @@ test("v0.5: after a compaction, the images from before it go back after the summconst both = sent(rewriteClaudeBody(request, undefined, session, dir, home, {}));assert.deepEqual(both.map((message: any) => message.role), ["user", "system", "assistant", "user", "system"]);assert.deepEqual(partsOf(both[3]), ["omitted:IMG-003", "And now?"]);- assert.match(both[4].content, /^上下文管理说明:上面这条消息里的 IMG-003/);+ // v0.8: IMG-003 is also the request's own first image, so the feature line note joins its short note, first.+ assert.deepEqual(both[4].content.map((block: any) => block.text.slice(0, 5)), ["特征行说明", "上下文管理"]);+ assert.match(both[4].content[1].text, /^上下文管理说明:上面这条消息里的 IMG-003/);// Automatic selection: only the pinned ones go out again; the others are placeholders in its words.writeSelection({ threadId: session, unchecked: {}, auto: true, autoSince: "2026-10-12T00:00:00Z", pinned: { "tool:toolu_1#0": { id: "IMG-002", at: "2026-10-12T00:00:00Z" } } } as any, dir);const auto = rewriteClaudeBody(request, undefined, session, dir, home, {});@@ -401,7 +418,9 @@ test("v0.5: the request writing a second summary is rewritten as usual; after it]), undefined, session, dir, home, {});assert.deepEqual(writing.report.compacted, { at: 0, listed: 1, resent: ["IMG-001"] });assert.deepEqual(partsOf(sent(writing)[0]), ["first summary", "IMG-001", "image:A"]);- assert.deepEqual(partsOf(sent(writing)[3]), ["image:B", "second"]);+ // v0.8: B, the request's own first image, says its id; the feature line note follows its message.+ assert.deepEqual(partsOf(sent(writing)[3]), ["IMG-002", "image:B", "second"]);+ assert.match(sent(writing)[4].content, /^特征行说明/);// After the second compaction both are from before it: listed, and sent again after the new summary.t.boundary();t.summary("second summary");diff --git a/spike/src/rewrite.test.ts b/spike/src/rewrite.test.tsindex 6b49c72..6438dd2 100644--- a/spike/src/rewrite.test.ts+++ b/spike/src/rewrite.test.ts@@ -56,7 +56,7 @@ test("an unchecked upload becomes one plain placeholder in place of Codex's tag,const parts = texts(out[2]);// The text, IMG-001's placeholder, then IMG-002 and IMG-003, each with its label (a later test) and Codex's 3 parts.assert.equal(parts.length, 1 + 1 + 4 + 4);- assert.match(parts[1], /^\[图片 IMG-001(\[Image #1\]) 已省略|a\.png|用户上传|第 1 轮|4×4\]\n原图被用户省略以节省上下文,情况见前面的“上下文管理说明”/);+ assert.match(parts[1], /^\[图片 IMG-001(\[Image #1\]) 已省略|a\.png|用户上传|第 1 轮|4×4\]\n需要重看时,在回复里写“需要 IMG-001”。$/);assert.match(parts[1], /需要 IMG-001/);assert.doesNotMatch(parts[1], /C:\\work/, "no local path in the placeholder");assert.match(texts(out[5])[1], /^\[图片 IMG-004 已省略|a\.png|工具查看的图片|第 1 轮|4×4\]\n.*需要 IMG-004/s);@@ -177,7 +177,7 @@ test("a PDF comment screenshot is replaced like an upload, and Codex's caption bassert.deepEqual(texts(out.find((item) => item.id === "msg_1")!), ["PDF path: C:\\docs\\report.pdf\nPDF page: 3/12\n",caption,- "[图片 IMG-001 已省略|未命名|用户上传|第 1 轮|4×4]\n原图被用户省略以节省上下文,情况见前面的“上下文管理说明”;需要重新看这张图时,在回复里写出“需要 IMG-001”。",+ "[图片 IMG-001 已省略|未命名|用户上传|第 1 轮|4×4]\n需要重看时,在回复里写“需要 IMG-001”。",]);});@@ -223,7 +223,7 @@ test("automatic selection: earlier turns become placeholders in its own wording,assert.equal(report.noteAt, 1);const parts = texts(out[2]);assert.equal(parts[1], autoPlaceholder({ id: "IMG-001", name: "a.png", label: "[Image #1]", kind: "upload", turn: 1, width: 4, height: 4 }));- assert.match(parts[1], /^\[图片 IMG-001(\[Image #1\]) 已省略|a\.png|用户上传|第 1 轮|4×4\]\n.*自动选图.*调用 cam_view_image 工具,传入“IMG-001”/s);+ assert.match(parts[1], /^\[图片 IMG-001(\[Image #1\]) 已省略|a\.png|用户上传|第 1 轮|4×4\]\n需要原图时调用 cam_view_image,传入“IMG-001”。$/);// The copy fetched this turn is sent, yet IMG-001's placeholder does not point to it: it stays as it was before the fetch.assert.deepEqual(modes(report), [["IMG-001", "plain", null], ["IMG-002", "plain", null], ["IMG-003", "plain", null]]);assert.equal(out[4], input[3], "this turn's new image goes as it is, without a label");