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(engine): 自动选图——之前各轮的图默认省略,模型用 cam_view_image 按编号取回
chipfighterf72fcab34aca
diff --git a/plugin/src/images.ts b/plugin/src/images.tsindex 130b130..eb48792 100644--- a/plugin/src/images.ts+++ b/plugin/src/images.ts@@ -2,6 +2,7 @@// and describe it: a key that names the same occurrence in both places, its kind and name, and a content digest.// v0.2 — the source the panel filters by: the kind, except for the screenshots Codex desktop attaches to a comment on a// PDF page or a web page, told apart by the caption Codex writes before them. The text for the model does not use it.+// v0.3 — an image our fetch tool returned carries, in the line before it, the id it is a copy of (thread-index.ts).// Input: items as parsed JSON. Output: ImageRef[] in item order (pure, no I/O).import { createHash } from "node:crypto";@@ -30,8 +31,15 @@ export type ImageRef = ImageDigest & {// name only one PDF.pdfPage: number | null;pdfName: string | null;+ // v0.3: in a tool output, the id named by our fetch tool's line right before the image ("[图片 IMG-001 取回的原图|…]").+ fetchedId: string | null;};+// v0.3 — the state our fetch tool (cam_view_image) writes in the line before each image it returns, in either+// language, so the image is known as a copy of that id. The rest of the line is the usual heading (rewrite.ts).+export const FETCHED_STATE = { zh: "取回的原图", en: "fetched original" } as const;+const FETCHED_LINE = new RegExp(`^\\[(?:图片|Image) (IMG-\\d{3,}) (?:${FETCHED_STATE.zh}|${FETCHED_STATE.en})(?:|| \\|)`);+const OPEN_TAG = /^<image name=(\[Image #\d+\]) path="(.*)">$/s;// Codex desktop 26.924's own wording: the caption right before a comment's screenshot, and the line naming the PDF in// the comment's text. Worded otherwise, a screenshot counts as an upload, as it did before v0.2.@@ -133,7 +141,7 @@ export function findImages(items: Json[]): ImageRef[] {if (item.type === "image_generation_call" && typeof item.result === "string" && item.result) {const found = digest(item, item.result, "image/png");- refs.push({ ...found, key: keyFor(0, found.contentId), item: index, part: null, openTag: null, closeTag: null, kind: "generated", replaceable: false, name: null, label: null, turnId, source: "generated", pdfPage: null, pdfName: null });+ refs.push({ ...found, key: keyFor(0, found.contentId), item: index, part: null, openTag: null, closeTag: null, kind: "generated", replaceable: false, name: null, label: null, turnId, source: "generated", pdfPage: null, pdfName: null, fetchedId: null });return;}const field = item.type === "message" ? item.content : item.type === "function_call_output" || item.type === "custom_tool_call_output" ? item.output : null;@@ -170,6 +178,7 @@ export function findImages(items: Json[]): ImageRef[] {source,pdfPage: pdf?.[1] ? Number(pdf[1]) : null,pdfName: pdf ? pdfName ?? null : null,+ fetchedId: context && before !== null ? FETCHED_LINE.exec(before)?.[1] ?? null : null,});n++;});diff --git a/plugin/src/panel-state.ts b/plugin/src/panel-state.tsindex 9e739a2..dffe6f1 100644--- a/plugin/src/panel-state.ts+++ b/plugin/src/panel-state.ts@@ -195,7 +195,8 @@ export function applySelection(threadId: string, change: { uncheck?: string[]; cif (!image) throw new Error(`${id} is not an image of this thread`);delete unchecked[image.key];}- writeSelection({ threadId, unchecked }, selectionDirOf(root));+ // The rest (v0.3: automatic selection's switch and pins) stays as it was.+ writeSelection({ ...selection, threadId, unchecked }, selectionDirOf(root));return { ...panelState(threadId, history, index, new Set(Object.keys(unchecked)), stats, title), historyMissing };}diff --git a/plugin/src/plugin-server.ts b/plugin/src/plugin-server.tsindex 6dacf99..642ae5e 100644--- a/plugin/src/plugin-server.ts+++ b/plugin/src/plugin-server.ts@@ -11,6 +11,9 @@// are logged with their duration; pixel fingerprints are shared with the engine through the data directory.// v0.1-14 — the panel reports Codex's interface language with its calls; it is remembered (language.ts), and the tool// list (the tab's title), results and refusals speak it (messages.ts). v0.1-20 — Codex's language setting counts too.+// v0.3 — cam_view_image, the one tool for the model: in a thread with automatic selection on, it returns the originals+// of images by id, each after a line naming it (rewrite.ts), so the index knows them as copies. Codex's code mode does+// not list plugin tools to the model; the model learns its name from the context note and looks it up.// Input: MCP JSON-RPC over stdio. Env: CAM_ENGINE_PORT (default 17891), CAM_NO_ENGINE=1 (tests), CAM_DATA_DIR.// Output: tool results; <data dir>/plugin-server.jsonl (events and counts only, no conversation content).@@ -26,9 +29,11 @@ import { currentLang, langOf, rememberLang, type Lang } from "./language.ts";import { say } from "./messages.ts";import { migrateDataOnce } from "./migrate-data.ts";import { applySelection, imageFor, loadPanelState, type PanelState } from "./panel-state.ts";-import { dataDir, pixelCacheDirOf } from "./paths.ts";+import { dataDir, pixelCacheDirOf, selectionDirOf } from "./paths.ts";+import { fetchedLabel, fetchWords } from "./rewrite.ts";+import { effectiveSelection } from "./selection.ts";import { connectDirectly, useEngine, usesEngine } from "./setup.ts";-import { hasRollout, readThreadHistory, sessionMetaOf, setPixelCache } from "./thread-index.ts";+import { hasRollout, loadThreadIndex, readThreadHistory, sessionMetaOf, setPixelCache } from "./thread-index.ts";import { threadTitle } from "./thread-names.ts";type Json = Record<string, any>;@@ -80,6 +85,14 @@ export function toolsFor(lang: Lang) {inputSchema: { type: "object", properties: { ...shared, target: { type: "string" } }, required: ["target"] },_meta: APP_ONLY,},+ // For the model (no app-only visibility); its words are text for the model, so they live in rewrite.ts.+ {+ name: "cam_view_image",+ title: fetchWords(lang).title,+ description: fetchWords(lang).description,+ inputSchema: { type: "object", properties: { ids: { type: "array", items: { type: "string" }, description: fetchWords(lang).ids } }, required: ["ids"] },+ annotations: { readOnlyHint: true },+ },];}export const TOOLS = toolsFor("zh");@@ -184,6 +197,30 @@ export function callTool(name: string, args: Json, meta: Json | undefined, sessiconst state = loadPanelState(threadId, options);return { content: [{ type: "text", text: say(lang, args.enable === true ? "call.enabled" : "call.disabled") }], structuredContent: panel(state, { notes: plan.notes }) };}+ if (name === "cam_view_image") {+ const words = fetchWords(lang);+ const ids: string[] = Array.isArray(args.ids) ? args.ids.map(String) : typeof args.id === "string" ? [args.id] : [];+ // Only where the user switched automatic selection on; otherwise images come back only when the user checks them.+ if (!effectiveSelection(threadId, sessionsDir, selectionDirOf(dataDir())).auto) {+ log({ event: "fetch", threadId, ids, refused: "automatic selection is off" });+ return { content: [{ type: "text", text: words.off }], isError: true };+ }+ const index = loadThreadIndex(sessionsDir, threadId);+ const content: Json[] = [];+ const found: string[] = [];+ for (const asked of ids) {+ const id = asked.trim().toUpperCase();+ const image = index.images.find((candidate) => candidate.id === id);+ if (!image) { content.push({ type: "text", text: words.missing(asked) }); continue; }+ const { dataUrl } = imageFor(threadId, id, Number.MAX_SAFE_INTEGER, options);+ const parts = dataUrl ? /^data:([^;,]+);base64,(.*)$/s.exec(dataUrl) : null;+ if (!parts) { content.push({ type: "text", text: words.unavailable(id) }); continue; }+ content.push({ type: "text", text: fetchedLabel(image, lang) }, { type: "image", data: parts[2], mimeType: parts[1] });+ found.push(id);+ }+ log({ event: "fetch", threadId, ids, found });+ return { content, isError: ids.length > 0 && !found.length };+ }if (name === "cam_image") {const image = imageFor(threadId, String(args.id), Number(args.maxSide ?? 160), options);// The image data goes in _meta, which only the panel sees.diff --git a/plugin/src/proxy.ts b/plugin/src/proxy.tsindex 0691aa2..4ac59c8 100644--- a/plugin/src/proxy.ts+++ b/plugin/src/proxy.ts@@ -6,6 +6,8 @@// v0.1-3: reports its build and version; POST /__cam/retire hands the port to a newer engine (see engine.ts).// v0.1-11: pixel fingerprints go to the shared cache in the data directory, for the panels to reuse.// v0.1-14: the text for the model is in the language the panel last reported (Codex's interface language).+// v0.3: automatic selection — a thread with it on leaves out the images of earlier turns that are not pinned, and a+// copy the model fetched is sent in its own turn only (spec v0.3).// Only metadata is logged (never auth headers or conversation content).// Input: [--port 17891] [--stay (no auto-exit)] [--force-http] [--dump-requests (synthetic test threads only)];// the outbound proxy is taken from HTTPS_PROXY/HTTP_PROXY or the system proxy settings (Windows, macOS).@@ -191,9 +193,28 @@ export function hasUnchecked(threadId: string | null, dir = selectionDir, sessiotry { return Object.keys(effectiveSelection(threadId, sessionsDir, dir).unchecked).length > 0; } catch { return false; }}+// Whether a thread's requests go through the rewrite (and so over HTTP): something unchecked, or automatic selection+// used at some point (v0.3), since from then on the history may hold copies the model fetched, left out of later turns.+export function needsRewrite(threadId: string | null, dir = selectionDir, sessionsDir = join(codexHome(), "sessions")): boolean {+ if (!threadId) return false;+ try {+ const selection = effectiveSelection(threadId, sessionsDir, dir);+ return Object.keys(selection.unchecked).length > 0 || !!selection.autoSince;+ } catch { return false; }+}++// The turn a request belongs to, as numbered in the index; a turn not written to the rollout yet comes after the last.+// Without a turn id the last one counts as current, so its images are sent rather than left out.+export function currentTurnOf(index: ThreadIndex, turnId: string | null): number {+ if (!turnId) return index.turns;+ return index.turnNumbers.get(turnId) ?? index.turns + 1;+}+// P2: replace the thread's unchecked images with placeholders. Whenever the body cannot be handled safely it is// forwarded unchanged and the reason is logged; the report never contains conversation content.-export function rewriteBody(original: Buffer, encoding: string | undefined, threadId: string, sessionsDir: string, dir = selectionDir): { body: Buffer; report: Json } {+// v0.3: with automatic selection on, the images of earlier turns that are not pinned instead; in either mode, copies+// the model fetched in an earlier turn.+export function rewriteBody(original: Buffer, encoding: string | undefined, threadId: string, sessionsDir: string, dir = selectionDir, turnId: string | null = null): { body: Buffer; report: Json } {const decoded = decodeBody(original, encoding);if (!decoded) return { body: original, report: { skipped: "undecodable body" } };const text = decoded.toString("utf8");@@ -211,14 +232,23 @@ export function rewriteBody(original: Buffer, encoding: string | undefined, threreturn stored ? { id: stored.id, name: ref.name, label: ref.label, kind: ref.kind, turn: null, width: ref.width, height: ref.height } : undefined;};const pixels = (ref: ImageRef) => index.byKey.get(ref.key)?.pixelSha256 ?? pixelHashOf(json.input, ref);- const { items, report } = rewriteItems(json.input, describe, new Set(Object.keys(selection.unchecked)), pixels, currentLang());+ const current = currentTurnOf(index, turnId);+ const copies = new Map([...index.copies.values()].filter((copy) => copy.turn === null || copy.turn < current).map((copy) => [copy.key, copy.of]));+ let leaveOut = new Set(Object.keys(selection.unchecked));+ if (selection.auto) {+ const 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 } } : {}) });const summary: Json = {images: report.images,replaced: report.replaced.map(({ key: _key, ...rest }) => rest),locked: report.locked,sentImageHashes: report.sentContentIds.map((id) => id.slice(0, 16)),+ ...(selection.auto ? { auto: true } : {}),+ ...(report.copies.length ? { copies: report.copies } : {}),};- if (!report.replaced.length) return { body: original, report: summary };+ if (!report.replaced.length && !report.copies.length) return { body: original, report: summary };json.input = items;const next = Buffer.from(JSON.stringify(json), "utf8");const body = encodeBody(next, encoding);@@ -301,7 +331,7 @@ async function main(): Promise<void> {};setInterval(() => {for (const [threadId, connections] of live) {- if (!hasUnchecked(threadId)) continue;+ if (!needsRewrite(threadId)) continue;for (const connection of connections) {if (Date.now() - connection.last < 1500) {// A turn still running here keeps the images the server already holds; noted once so the panel can say so.@@ -338,7 +368,7 @@ async function main(): Promise<void> {const started = Date.now();const path = (req.url ?? "/").split("?")[0];const identity = requestIdentity(req.headers);- const rewrite = req.method === "POST" && /\/responses(\/compact)?$/.test(path) && hasUnchecked(identity.threadId);+ const rewrite = req.method === "POST" && /\/responses(\/compact)?$/.test(path) && needsRewrite(identity.threadId);const chunks: Buffer[] = [];let requestBytes = 0;let responseBytes = 0;@@ -399,7 +429,7 @@ async function main(): Promise<void> {req.on("end", () => {const original = Buffer.concat(chunks);let out: { body: Buffer; report: Json };- try { out = rewriteBody(original, req.headers["content-encoding"] as string | undefined, identity.threadId!, sessionsDir); }+ try { out = rewriteBody(original, req.headers["content-encoding"] as string | undefined, identity.threadId!, sessionsDir, selectionDir, identity.turnId); }catch (error) { out = { body: original, report: { skipped: `rewrite failed: ${String(error)}` } }; }extra = { rewrite: out.report };const headers = forwardHeaders(req.headers);@@ -418,9 +448,9 @@ async function main(): Promise<void> {client.end("HTTP/1.1 503 Service Unavailable\r\nConnection: close\r\nContent-Length: 0\r\n\r\n");return;}- if (/\/responses$/.test(path) && (forceHttp || hasUnchecked(identity.threadId))) {+ if (/\/responses$/.test(path) && (forceHttp || needsRewrite(identity.threadId))) {client.end("HTTP/1.1 426 Upgrade Required\r\nConnection: close\r\nContent-Length: 0\r\n\r\n");- log({ at: new Date(started).toISOString(), id, transport: "websocket", path, declined: 426, reason: forceHttp ? "--force-http" : "thread has unchecked images", ...identity });+ log({ at: new Date(started).toISOString(), id, transport: "websocket", path, declined: 426, reason: forceHttp ? "--force-http" : "thread needs its requests rewritten", ...identity });return;}let up = head.length;diff --git a/plugin/src/rewrite.ts b/plugin/src/rewrite.tsindex 863275a..908be68 100644--- a/plugin/src/rewrite.ts+++ b/plugin/src/rewrite.ts@@ -7,22 +7,29 @@// v0.1-24 — how to ask for an image back is spelled out, for every GPT-6 model (self-tests with CAM_TEST_MODEL).// v0.1-25 — once anything is left out, each image still sent carries its id, and the note says a checked image is back// in its old place: GPT-6 Luna looked for it in the newest message and said it could not see it (user 2026-09-28).-// Input: request items, a lookup into the thread index, the unchecked keys. Output: new items plus a metadata report.+// v0.3 — automatic selection (user 2026-10-06): images from earlier turns that are not pinned are left out, in their own+// 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).+// Input: request items, a lookup into the thread index, the keys to leave out. Output: new items plus a metadata report.-import { findImages, type ImageKind, type ImageRef } from "./images.ts";+import { FETCHED_STATE, findImages, type ImageKind, type ImageRef } from "./images.ts";import type { Lang } from "./language.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 };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.-export type RewriteReport = { images: number; replaced: Replacement[]; locked: string[]; sentContentIds: string[]; noteAt?: 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 };+type FetchWords = { title: string; description: string; ids: string; missing: (id: string) => string; unavailable: (id: string) => string; off: string };type Words = {kind: Record<ImageKind, string>; unnamed: string; turn: (n: number) => string; thisTurn: string; newImage: string;omitted: string; notRepeated: string; included: string; separator: string;heading: (id: string, label: string | null, state: string, fields: string[]) => string;plain: (id: string) => string; duplicate: (id: string, where: string) => string; note: string;+ autoPlain: (id: string) => string; autoNote: string; copy: (id: string) => string; fetch: FetchWords;};// The image sits in the history, so the model concludes it never saw it and takes back what it said about it (user@@ -49,6 +56,22 @@ const WORDS: Record<Lang, Words> = {"被问到现在能不能看到某张被省略的图片时,如实说明现在看不到(被用户省略了),并说明之前的描述仍然有效。现在看不到画面,就不要补充之前没说过的细节。需要重新看某张图时,在回复里原样写出“需要 IMG-xxx”(几张就写“需要 IMG-002、IMG-003”):工具靠这几个字认出你要的图。用户重新勾选后,原图会回到它在对话里原来的位置:那里的占位文字换成“[图片 IMG-xxx 已提供…]”和原图;它不会出现在用户的新消息里,所以不要让用户重新上传或附上图片。不要自己用工具去读取这些图片。","标着“[图片 IMG-xxx 已提供…]”的图片现在就在你面前,即使它之前被省略过、或者正是你要过的图:被问到它时,直接看图回答,不要说看不到。",].join("\n"),+ autoPlain: (id) => `之前各轮的图片默认省略以节省上下文(自动选图),情况见前面的“上下文管理说明”;需要看这张图的原图时,调用 cam_view_image 工具,传入“${id}”。`,+ autoNote: [+ "上下文管理说明(来自用户安装的上下文素材管理工具):这个任务开着“自动选图”。为了节省上下文,之前各轮的图片默认不再随请求提供,后面的对话里,它们原来的位置换成了“[图片 IMG-xxx 已省略…]”这样的占位文字;本轮新出现的图片照常提供,用户固定发送的图片也照常提供,前面标着“[图片 IMG-xxx 已提供…]”,紧接着就是原图。",+ "这些图片在它们出现的那一轮真实存在,你当时收到并看过;你之前对它们的描述,以及据此做出的判断,都是看着原图做出的,仍然有效。不要因为现在看不到,就认为之前的回答是猜测或错误,也不要收回或道歉。",+ "回答需要看某张被省略的图片、而你之前说过的内容不够用时,调用 cam_view_image 工具,传入它的编号(几张就一次传几个),原图会出现在这次的工具结果里,前面标着“[图片 IMG-xxx 取回的原图…]”,拿到后看图回答。之前说过的内容够用时,不要调用。现在看不到的画面细节,不要猜测或补充;不要让用户重新上传图片,也不要用其他工具去读取图片文件。",+ "用 cam_view_image 取回的图片只在本轮提供,下一轮又会换回占位文字;之后还需要时,再调用一次。标着“已提供”或“取回的原图”的图片现在就在你面前:被问到它时,直接看图回答,不要说看不到。",+ ].join("\n"),+ copy: (id) => `[${id} 的原图:模型当时用 cam_view_image 取回,只在那一轮提供,现在已省略]`,+ fetch: {+ title: "查看被省略的图片",+ description: "查看这个任务里一张或几张被省略的图片的原图。被省略的图片在对话里显示为“[图片 IMG-xxx 已省略…]”。只在回答需要看图、而你之前对它说过的内容不够用时调用;取回的原图只在本轮提供。",+ ids: "要查看的图片编号,例如 [\"IMG-004\"];几张就一次传几个。",+ missing: (id) => `这个任务里没有 ${id}。`,+ unavailable: (id) => `${id} 的原图读不出来。`,+ off: "这个任务没有开自动选图:被省略的图片要由用户勾选。需要时在回复里写“需要 IMG-xxx”。",+ },},en: {kind: { upload: "uploaded by the user", view: "viewed with a tool", generated: "generated image", tool: "image from a tool result" },@@ -69,6 +92,22 @@ 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}".`,+ 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.",+ "When an answer needs an omitted image and what you said about it before is not enough, call the cam_view_image tool with its id (several ids at once if you need several); the original comes back in that tool result, marked \"[Image IMG-xxx fetched original…]\", and you answer from it. When what you said before is enough, do not call it. Do not guess or add visual details you cannot see now; do not ask the user to upload images again, and do not read image files with other tools.",+ "An image you get with cam_view_image is included only in this turn; from the next turn on it is a placeholder again. Call the tool again if you need it later. An image marked \"included\" or \"fetched original\" is in front of you now: when asked about it, look at it and answer from it, and do not say you cannot see it.",+ ].join("\n"),+ copy: (id) => `[Original of ${id}: fetched by the model with cam_view_image, included only in that turn, now omitted]`,+ fetch: {+ title: "View omitted images",+ description: "View the originals of one or more omitted images in this task. Omitted images appear in the conversation as \"[Image IMG-xxx omitted…]\". Call it only when an answer needs to look at an image and what you said about it before is not enough; the originals are included only in this turn.",+ ids: "Ids of the images to view, e.g. [\"IMG-004\"]; pass several at once if you need several.",+ missing: (id) => `This task has no ${id}.`,+ 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.",+ },},};@@ -102,20 +141,48 @@ export function includedLabel(image: Described, lang: Lang = "zh"): string {return heading(WORDS[lang], image, WORDS[lang].included);}+// v0.3: automatic selection.+export const autoNote = (lang: Lang) => WORDS[lang].autoNote;+export const fetchWords = (lang: Lang): FetchWords => WORDS[lang].fetch;++export function autoPlaceholder(image: Described, lang: Lang = "zh"): string {+ const words = WORDS[lang];+ return `${heading(words, image, words.omitted)}\n${words.autoPlain(image.id)}`;+}++// 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 {+ return heading(WORDS[lang], { ...image, label: null }, FETCHED_STATE[lang]);+}++export type RewriteOptions = {+ lang?: Lang;+ // Copies the model fetched in an earlier turn (key → the id they copy): left out in either mode.+ copies?: Map<string, string>;+ // 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 };+};+export function rewriteItems(items: Json[],describe: (ref: ImageRef) => Described | undefined,unchecked: Set<string>,pixelHash: (ref: ImageRef) => string | null,- lang: Lang = "zh",+ 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 refs = findImages(items);- const report: RewriteReport = { images: refs.length, replaced: [], locked: [], sentContentIds: [] };- const drop = refs.filter((ref) => unchecked.has(ref.key) && ref.replaceable && describe(ref));+ const report: RewriteReport = { images: refs.length, replaced: [], locked: [], sentContentIds: [], copies: [] };+ const copied = refs.filter((ref) => copies.has(ref.key) && ref.part !== null);+ const drop = refs.filter((ref) => !copies.has(ref.key) && unchecked.has(ref.key) && ref.replaceable && describe(ref));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));+ const sent = refs.filter((ref) => !drop.includes(ref) && !copied.includes(ref));report.sentContentIds = sent.map((ref) => ref.contentId);- if (!drop.length) return { items, report };+ if (!drop.length && !copied.length) 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;const same = (a: ImageRef, b: ImageRef) => {if (a.contentId === b.contentId) return true;@@ -129,16 +196,20 @@ export function rewriteItems(let firstPlain: number | null = null;for (const ref of drop) {const image = describe(ref)!;- const copy = sent.find((other) => same(other, ref));+ const copy = lasting.find((other) => same(other, ref));const copyImage = copy ? describe(copy) ?? { id: WORDS[lang].newImage, name: copy.name, label: copy.label, kind: copy.kind, turn: null, width: copy.width, height: copy.height } : null;- const text = copyImage ? duplicatePlaceholder(image, copyImage, lang) : plainPlaceholder(image, lang);+ const text = copyImage ? duplicatePlaceholder(image, copyImage, lang) : auto ? autoPlaceholder(image, lang) : plainPlaceholder(image, lang);if (!copyImage && firstPlain === null) firstPlain = ref.item;report.replaced.push({ id: image.id, key: ref.key, kind: ref.kind, mode: copyImage ? "duplicate" : "plain", sameAs: copyImage?.id ?? null, base64Chars: ref.base64Chars });const at = ref.openTag ?? ref.part!;edit(ref.item, at, (ref.closeTag ?? ref.part!) - at + 1, text);}+ for (const ref of copied) {+ edit(ref.item, ref.part!, 1, WORDS[lang].copy(copies.get(ref.key)!));+ 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 sent) {+ for (const ref of lasting) {const image = ref.replaceable && ref.part !== null ? describe(ref) : undefined;if (image) edit(ref.item, ref.openTag ?? ref.part!, 0, includedLabel(image, lang));}@@ -155,7 +226,7 @@ export function rewriteItems(if (firstPlain !== null) {let at = firstPlain;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: WORDS[lang].note }] });+ next.splice(at, 0, { type: "message", role: "developer", content: [{ type: "input_text", text: auto ? WORDS[lang].autoNote : WORDS[lang].note }] });report.noteAt = at;}return { items: next, report };diff --git a/plugin/src/selection.ts b/plugin/src/selection.tsindex f64f092..ee829e7 100644--- a/plugin/src/selection.ts+++ b/plugin/src/selection.ts@@ -2,14 +2,20 @@// the panel), read by the proxy on every request of that thread.// P3 — a forked thread starts with what its parent had unchecked when the fork was made (user decision 2026-09-24);// from then on the two are independent.-// Input/Output: <data dir>/selection/<thread id>.json, e.g. { "threadId": "…", "unchecked": { "<key>": { "id": "IMG-003", "at": "…" } } }.+// v0.3 — automatic selection: a switch per thread and the images pinned while it is on ("固定发送"), kept apart from+// the manual checks, which come back as they were when it is switched off. `autoAt` is when it was last switched,+// `autoSince` when it was first switched on: from then on the history may hold copies the model fetched, which the+// engine leaves out of later turns even with the switch off.+// Input/Output: <data dir>/selection/<thread id>.json, e.g. { "threadId": "…", "unchecked": { "<key>": { "id": "IMG-003", "at": "…" } } },+// plus "auto", "autoAt", "autoSince" and "pinned" (same shape as "unchecked") once automatic selection has been used.import { existsSync, mkdirSync, readFileSync, renameSync, statSync, writeFileSync } from "node:fs";import { join } from "node:path";import { selectionDirOf } from "./paths.ts";import { forkOrigin } from "./thread-index.ts";-export type Selection = { threadId: string; unchecked: Record<string, { id: string; at: string }>; inheritedFrom?: string };+export type Mark = { id: string; at: string };+export type Selection = { threadId: string; unchecked: Record<string, Mark>; auto?: boolean; autoAt?: string; autoSince?: string; pinned?: Record<string, Mark>; inheritedFrom?: string };export const selectionDir = selectionDirOf();const THREAD_ID = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/;@@ -40,15 +46,22 @@ export function writeSelection(selection: Selection, dir = selectionDir): void {}// The thread's own selection; a fork without one takes the images its parent had unchecked by the time of the fork-// (a fork of a fork asks its own parent in turn) and keeps them as its own file from then on.+// (a fork of a fork asks its own parent in turn) and keeps them as its own file from then on. v0.3: likewise the+// pinned images and the switch, when it was turned on before the fork.export function effectiveSelection(threadId: string, sessionsDir: string, dir = selectionDir, depth = 0): Selection {if (existsSync(fileFor(threadId, dir)) || depth > 16) return readSelection(threadId, dir);const origin = forkOrigin(sessionsDir, threadId);if (!origin) return { threadId, unchecked: {} };const parent = effectiveSelection(origin.parent, sessionsDir, dir, depth + 1);- const unchecked = Object.fromEntries(Object.entries(parent.unchecked).filter(([, entry]) => !origin.at || entry.at <= origin.at));- if (!Object.keys(unchecked).length) return { threadId, unchecked: {} };- const inherited: Selection = { threadId, unchecked, inheritedFrom: origin.parent };+ const before = (entries: Record<string, Mark> = {}) => Object.fromEntries(Object.entries(entries).filter(([, entry]) => !origin.at || entry.at <= origin.at));+ const unchecked = before(parent.unchecked);+ const pinned = before(parent.pinned);+ const used = !!parent.autoSince && (!origin.at || parent.autoSince <= origin.at);+ // On at the fork if it is on now and was last switched before the fork.+ const auto = used && parent.auto === true && (!origin.at || !parent.autoAt || parent.autoAt <= origin.at);+ if (!Object.keys(unchecked).length && !Object.keys(pinned).length && !used) return { threadId, unchecked: {} };+ const switches = used ? { auto, autoSince: parent.autoSince, ...(auto ? { autoAt: parent.autoAt } : {}) } : {};+ const inherited: Selection = { threadId, unchecked, ...switches, ...(Object.keys(pinned).length ? { pinned } : {}), inheritedFrom: origin.parent };writeSelection(inherited, dir);return inherited;}diff --git a/plugin/src/thread-index.ts b/plugin/src/thread-index.tsindex 7ed37b1..a93179a 100644--- a/plugin/src/thread-index.ts+++ b/plugin/src/thread-index.ts@@ -9,6 +9,7 @@// v0.1-12 — archived tasks (<CODEX_HOME>/archived_sessions) are looked up too: a fork's history may start in a page of// 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.// 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";@@ -19,7 +20,9 @@ import { decodePng } from "./png.ts";type Json = Record<string, any>;export type IndexedImage = ImageRef & { id: string; turn: number | null; bytes: number; pixelSha256: string | null };-export type ThreadIndex = { threadId: string; images: IndexedImage[]; byKey: Map<string, IndexedImage>; turns: number; turnNumbers: Map<string, number> };+// 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 Parsed = { offset: number; type: string; payload: Json; timestamp: string | null };type FileCache = { size: number; mtimeMs: number; parsedTo: number; records: Parsed[] };@@ -225,16 +228,20 @@ export function buildIndex(threadId: string, history: Parsed[]): ThreadIndex {}const images: IndexedImage[] = [];const byKey = new Map<string, IndexedImage>();+ const copies = new Map<string, FetchedCopy>();for (const ref of findImages(items)) {- if (byKey.has(ref.key)) continue;+ if (byKey.has(ref.key) || copies.has(ref.key)) continue;const turnId = ref.turnId ?? activeTurn[ref.item];- const entry: IndexedImage = {- ...ref,- id: `IMG-${String(images.length + 1).padStart(3, "0")}`,- turn: turnId ? turnNumbers.get(turnId) ?? null : null,- bytes: Math.floor((ref.base64Chars * 3) / 4),- pixelSha256: pixelHashOf(items, ref),- };+ const turn = turnId ? turnNumbers.get(turnId) ?? null : null;+ const pixelSha256 = pixelHashOf(items, ref);+ // v0.3: what our fetch tool brought back is the same image again, not a new occurrence, so it takes no id (the ids+ // after it stay as they were) and the panel lists it on the original's row. Its content must match that id's.+ const original = ref.fetchedId ? images.find((image) => image.id === ref.fetchedId) : undefined;+ if (original && (original.contentId === ref.contentId || (pixelSha256 !== null && original.pixelSha256 === pixelSha256))) {+ copies.set(ref.key, { key: ref.key, of: original.id, turn });+ continue;+ }+ const entry: IndexedImage = { ...ref, id: `IMG-${String(images.length + 1).padStart(3, "0")}`, turn, bytes: Math.floor((ref.base64Chars * 3) / 4), pixelSha256 };images.push(entry);byKey.set(ref.key, entry);}@@ -244,7 +251,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 };+ return { threadId, images, byKey, turns: turnNumbers.size, turnNumbers, copies };}// The first line alone (session_meta can be long), read in chunks without parsing the rest of the file.diff --git a/spike/src/panel-state.test.ts b/spike/src/panel-state.test.tsindex a55157c..320e9d2 100644--- a/spike/src/panel-state.test.ts+++ b/spike/src/panel-state.test.ts@@ -11,6 +11,7 @@ import { decodePng } from "../../plugin/src/png.ts";import { applySelection, imageFor, loadPanelState, requestedIds, sendInfo } from "../../plugin/src/panel-state.ts";import { requestStatsDirOf } from "../../plugin/src/paths.ts";import { recordRequest } from "../../plugin/src/request-stats.ts";+import { readSelection, writeSelection } from "../../plugin/src/selection.ts";import { assistant, line, red, sampleSessions, THREAD, turn, upload } from "./testfixtures.ts";import { shrink } from "../../plugin/src/thumbnail.ts";import { png } from "./testkit.ts";@@ -129,6 +130,17 @@ test("check and uncheck are kept per thread and validated", () => {assert.throws(() => applySelection(THREAD, { uncheck: ["IMG-099"] }, options), /not an image of this thread/);});+test("checking and unchecking keep automatic selection's switch and pins (v0.3)", () => {+ const options = sampleSessions();+ const dir = join(options.dataRoot, "selection");+ const auto = { auto: true, autoAt: "2026-10-06T10:00:00Z", autoSince: "2026-10-06T10:00:00Z", pinned: { "msg_1#1": { id: "IMG-002", at: "2026-10-06T10:00:00Z" } } };+ writeSelection({ threadId: THREAD, unchecked: {}, ...auto }, dir);+ applySelection(THREAD, { uncheck: ["IMG-001"] }, options);+ const { unchecked, ...rest } = readSelection(THREAD, dir);+ assert.deepEqual(Object.values(unchecked).map((entry) => entry.id), ["IMG-001"]);+ assert.deepEqual(rest, { threadId: THREAD, ...auto });+});+test("thumbnails keep the aspect ratio and fit the requested size", () => {const options = sampleSessions();const thumb = imageFor(THREAD, "IMG-001", 32, options);diff --git a/spike/src/plugin-server.test.ts b/spike/src/plugin-server.test.tsindex 62762eb..9e2d842 100644--- a/spike/src/plugin-server.test.ts+++ b/spike/src/plugin-server.test.ts@@ -1,7 +1,7 @@// Purpose: P3-3 — the plugin's MCP server: thread resolution, the rule that only the user may check or uncheck,// thumbnails kept out of model-visible content, and a full stdio session (engine supervision switched off).// P4 — the panel's entrypoint and page resource. v0.1-10 — a panel on a new chat is offered the thread the user just-// started and follows it once bound.+// started and follows it once bound. v0.3 — cam_view_image, the model's one tool, works only with automatic selection.// Input: synthetic rollouts in temporary folders; output: Node test assertions only.import assert from "node:assert/strict";@@ -10,6 +10,7 @@ import { dirname, join } from "node:path";import test from "node:test";import { fileURLToPath } from "node:url";import { callTool, fromModel, PANEL_MIME, PANEL_URI, readResource, threadOf, TOOLS } from "../../plugin/src/plugin-server.ts";+import { writeSelection } from "../../plugin/src/selection.ts";import { line, red, sampleSessions, startThread, THREAD, turn, upload } from "./testfixtures.ts";// These tests check the Chinese wording: the language is fixed, so neither the system's language nor one a panel@@ -32,7 +33,11 @@ test("the thread comes from the arguments, the panel's meta, or the model's turntest("the panel is a thread entrypoint with a fixed page; only the entry tool renders it", () => {const panel = TOOLS.find((tool) => tool.name === "cam_panel")!;assert.deepEqual(panel._meta, { ui: { resourceUri: PANEL_URI, visibility: ["app"] }, "openai/ui": { entrypoints: [{ type: "thread" }] } });- for (const tool of TOOLS) assert.deepEqual(tool._meta.ui.visibility, ["app"], `${tool.name} is for the panel, not the model`);+ for (const tool of TOOLS.filter((tool) => tool.name !== "cam_view_image")) assert.deepEqual(tool._meta?.ui.visibility, ["app"], `${tool.name} is for the panel, not the model`);+ // v0.3: the one tool for the model, which only reads.+ const fetch = TOOLS.find((tool) => tool.name === "cam_view_image")!;+ assert.equal(fetch._meta, undefined);+ assert.deepEqual(fetch.annotations, { readOnlyHint: true });const [page] = readResource(PANEL_URI).contents;assert.equal(page.mimeType, PANEL_MIME);assert.match(page.text, /<title>上下文素材<\/title>/);@@ -65,6 +70,28 @@ test("image data stays in _meta, out of the model-visible content", () => {}});+test("the model fetches originals by id only where automatic selection is on (v0.3)", () => {+ const { sessionsDir, dataRoot } = sampleSessions();+ process.env.CAM_DATA_DIR = dataRoot;+ try {+ const off = callTool("cam_view_image", { ids: ["IMG-001"] }, modelMeta, sessionsDir);+ assert.equal(off.isError, true);+ assert.match(off.content[0].text, /没有开自动选图.*需要 IMG-xxx/);+ writeSelection({ threadId: THREAD, unchecked: {}, auto: true, autoAt: "2026-10-06T10:00:00Z", autoSince: "2026-10-06T10:00:00Z" }, join(dataRoot, "selection"));+ const on = callTool("cam_view_image", { ids: ["img-001", "IMG-009"] }, modelMeta, sessionsDir);+ assert.equal(on.isError, false);+ assert.deepEqual(on.content.map((part: any) => part.type), ["text", "image", "text"]);+ // The line before the image names it (no "[Image #1]"), so the index knows the image as a copy of IMG-001.+ assert.equal(on.content[0].text, "[图片 IMG-001 取回的原图|a.png|用户上传|第 1 轮|64×32]");+ assert.equal(on.content[1].mimeType, "image/png");+ assert.equal(Buffer.from(on.content[1].data, "base64").equals(red), true, "the original bytes, not a thumbnail");+ assert.match(on.content[2].text, /没有 IMG-009/);+ assert.equal(callTool("cam_view_image", { ids: ["IMG-009"] }, modelMeta, sessionsDir).isError, true);+ } finally {+ delete process.env.CAM_DATA_DIR;+ }+});+test("a stdio session lists the tools and answers calls", async () => {const { sessionsDir, dataRoot } = sampleSessions();const child = spawn(process.execPath, [join(here, "..", "..", "plugin", "src", "plugin-server.ts")], { env: { ...process.env, CAM_NO_ENGINE: "1", CAM_DATA_DIR: dataRoot, CODEX_HOME: dirname(sessionsDir) }, stdio: ["pipe", "pipe", "inherit"] });@@ -93,7 +120,7 @@ test("a stdio session lists the tools and answers calls", async () => {child.stdin.end();assert.deepEqual(replies[1].result.capabilities, { tools: {}, resources: {} });assert.deepEqual(replies[1].result.serverInfo.icons.map((icon: any) => [icon.theme, icon.src.slice(0, 26)]), [["light", "data:image/svg+xml;base64,"], ["dark", "data:image/svg+xml;base64,"]]);- assert.deepEqual(replies[2].result.tools.map((tool: any) => tool.name), ["cam_panel", "cam_set_selection", "cam_image", "cam_setup", "cam_bind"]);+ assert.deepEqual(replies[2].result.tools.map((tool: any) => tool.name), ["cam_panel", "cam_set_selection", "cam_image", "cam_setup", "cam_bind", "cam_view_image"]);assert.equal(replies[3].result.structuredContent.images.length, 3);assert.equal(replies[3].result.structuredContent.engineRunning, null, "engine supervision is off in this test");assert.equal(replies[4].result.isError, true);diff --git a/spike/src/proxy.test.ts b/spike/src/proxy.test.tsindex 6e10658..d737f30 100644--- a/spike/src/proxy.test.ts+++ b/spike/src/proxy.test.ts@@ -8,7 +8,7 @@ import { tmpdir } from "node:os";import { join } from "node:path";import test from "node:test";import zlib from "node:zlib";-import { describeBody, hasUnchecked, hasUnsafeInteger, imageSizesOf, macProxy, outboundProxy, rewriteBody, requestIdentity } from "../../plugin/src/proxy.ts";+import { describeBody, hasUnchecked, hasUnsafeInteger, imageSizesOf, macProxy, needsRewrite, outboundProxy, rewriteBody, requestIdentity } from "../../plugin/src/proxy.ts";import { writeSelection } from "../../plugin/src/selection.ts";import { png } from "./testkit.ts";@@ -127,3 +127,23 @@ test("rewriteBody forwards the original bytes when nothing applies or the body iassert.equal(out, risky);assert.match(report.skipped, /2\^53/);});++// v0.3: automatic selection. The request belongs to turn 2, which the rollout has not recorded yet.+test("with automatic selection, rewriteBody leaves out earlier turns' images that are not pinned", () => {+ const { threadId, sessions, selections, red, blue, body } = fixture();+ assert.equal(needsRewrite(threadId, selections, sessions), false);+ writeSelection({ threadId, unchecked: {}, auto: true, autoAt: "2026-10-06T10:00:00Z", autoSince: "2026-10-06T10:00:00Z", pinned: { "msg_1#1": { id: "IMG-002", at: "2026-10-06T10:00:00Z" } } }, selections);+ assert.equal(needsRewrite(threadId, selections, sessions), true);+ const json = JSON.parse(zlib.zstdDecompressSync(body()).toString("utf8"));+ json.input.push({ type: "message", role: "user", content: [{ type: "input_text", text: "再看看" }] });+ const original = zlib.zstdCompressSync(Buffer.from(JSON.stringify(json)));+ const { body: out, report } = rewriteBody(original, "zstd", threadId, sessions, selections, "t2");+ const 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-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");+});diff --git a/spike/src/rewrite.test.ts b/spike/src/rewrite.test.tsindex 33b883e..6b49c72 100644--- a/spike/src/rewrite.test.ts+++ b/spike/src/rewrite.test.ts@@ -1,12 +1,13 @@// Purpose: P2 — the rewrite rules: only unchecked images change; duplicate vs plain placeholders; locked results.// v0.1 — one developer message explains omitted images, so the model does not take back earlier answers.// v0.1-25 — once anything is left out, each image still sent is marked with its id.+// v0.3 — automatic selection: its wording, placeholders that stay put while the model fetches, pins, and copies.// Input: synthetic request items; output: Node test assertions only.import assert from "node:assert/strict";import test from "node:test";import { findImages, type ImageRef } from "../../plugin/src/images.ts";-import { OMISSION_NOTE, omissionNote, rewriteItems, type Described } from "../../plugin/src/rewrite.ts";+import { autoNote, autoPlaceholder, fetchedLabel, OMISSION_NOTE, omissionNote, rewriteItems, type Described } from "../../plugin/src/rewrite.ts";import { png } from "./testkit.ts";const url = (bytes: Buffer) => `data:image/png;base64,${bytes.toString("base64")}`;@@ -179,3 +180,90 @@ test("a PDF comment screenshot is replaced like an upload, and Codex's caption b"[图片 IMG-001 已省略|未命名|用户上传|第 1 轮|4×4]\n原图被用户省略以节省上下文,情况见前面的“上下文管理说明”;需要重新看这张图时,在回复里写出“需要 IMG-001”。",]);});++// v0.3 (user 2026-10-06): automatic selection. Turn 1 brings IMG-001 a.png (red), IMG-002 b.png and IMG-003 b copy.png+// (both blue); turn 2 brings IMG-004 c.png (green), then the model fetches IMG-001 with cam_view_image: a copy, which+// the thread index keeps apart without an id of its own.+const FETCHED = "[图片 IMG-001 取回的原图|a.png|用户上传|第 1 轮|4×4]";+const COPY_GONE = "[IMG-001 的原图:模型当时用 cam_view_image 取回,只在那一轮提供,现在已省略]";+const fetchCall = { type: "custom_tool_call", call_id: "c2", name: "exec", input: 'const r = await tools.mcp__codex_attachment_manager__cam_view_image({ids:["IMG-001"]}); for (const c of r.content) c.type === "image" ? image(c) : text(c.text);' };+const fetchOutput = (label = FETCHED) => ({ type: "custom_tool_call_output", id: "ctco_2", call_id: "c2", output: [{ type: "input_text", text: "Script completed" }, { type: "input_text", text: label }, { type: "input_image", image_url: url(red) }] });+const autoItems = (fetched = true) => [+ { type: "message", id: "msg_dev", role: "developer", content: [{ type: "input_text", text: "instructions" }] },+ { type: "message", id: "msg_1", role: "user", content: [{ type: "input_text", text: "三张图" }, ...tagged("a.png", 1, red), ...tagged("b.png", 2, blue), ...tagged("b copy.png", 3, blue)] },+ { type: "message", id: "msg_2", role: "assistant", content: [{ type: "output_text", text: "收到" }] },+ { type: "message", id: "msg_3", role: "user", content: [{ type: "input_text", text: "这张呢" }, ...tagged("c.png", 1, green)] },+ ...(fetched ? [fetchCall, fetchOutput()] : []),+];+function autoIndex(list: Array<Record<string, any>>) {+ const refs = findImages(list);+ const byKey = new Map<string, Described>();+ for (const ref of refs.filter((candidate) => !candidate.fetchedId)) byKey.set(ref.key, { id: `IMG-00${byKey.size + 1}`, name: ref.name, label: ref.label, kind: ref.kind, turn: ref.item <= 2 ? 1 : 2, width: ref.width, height: ref.height });+ return {+ describe: (ref: ImageRef) => byKey.get(ref.key),+ keys: (...ids: string[]) => new Set(ids.map((id) => [...byKey].find(([, v]) => v.id === id)![0])),+ copyKey: refs.find((ref) => ref.fetchedId)?.key ?? null,+ };+}++test("the line cam_view_image writes before an image names the id it copies, in a tool output only", () => {+ assert.deepEqual(findImages(autoItems()).map((ref) => ref.fetchedId), [null, null, null, null, "IMG-001"]);+ const english = [fetchCall, fetchOutput("[Image IMG-001 fetched original | a.png | uploaded by the user | turn 1 | 4×4]")];+ assert.deepEqual(findImages(english).map((ref) => ref.fetchedId), ["IMG-001"]);+ const typed = [{ type: "message", id: "msg_x", role: "user", content: [{ type: "input_text", text: FETCHED }, { type: "input_image", image_url: url(red) }] }];+ assert.deepEqual(findImages(typed).map((ref) => ref.fetchedId), [null], "the same words typed by the user do not count");+ assert.equal(fetchedLabel({ id: "IMG-001", name: "a.png", label: "[Image #1]", kind: "upload", turn: 1, width: 4, height: 4 }), FETCHED, "without Codex's own label");+});++test("automatic selection: earlier turns become placeholders in its own wording, under its own note", () => {+ const input = autoItems();+ const { describe, keys } = autoIndex(input);+ const { items: out, report } = rewriteItems(input, describe, keys("IMG-001", "IMG-002", "IMG-003"), noPixels, { auto: { currentTurn: 2 } });+ assert.deepEqual(out[1], { type: "message", role: "developer", content: [{ type: "input_text", text: autoNote("zh") }] });+ 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);+ // 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");+ assert.equal(out[6], input[5], "and so does the copy");+});++test("automatic selection: the requests of a turn keep everything before the fetched copy the same", () => {+ const { describe, keys } = autoIndex(autoItems());+ const left = keys("IMG-001", "IMG-002", "IMG-003");+ const before = rewriteItems(autoItems(false), describe, left, noPixels, { auto: { currentTurn: 2 } }).items;+ const after = rewriteItems(autoItems(), describe, left, noPixels, { auto: { currentTurn: 2 } }).items;+ assert.equal(JSON.stringify(after.slice(0, before.length)), JSON.stringify(before));+});++test("automatic selection: a pinned image stays with its id, and an omitted identical one points to it", () => {+ const input = autoItems();+ const { describe, keys } = autoIndex(input);+ const { items: out, report } = rewriteItems(input, describe, keys("IMG-001", "IMG-003"), noPixels, { auto: { currentTurn: 2 } });+ assert.equal(texts(out[2])[2], "[图片 IMG-002([Image #2]) 已提供|b.png|用户上传|第 1 轮|4×4]");+ assert.deepEqual(modes(report), [["IMG-001", "plain", null], ["IMG-003", "duplicate", "IMG-002"]]);+});++test("a copy fetched in an earlier turn is left out, with automatic selection on or off", () => {+ const input = autoItems();+ const { describe, keys, copyKey } = autoIndex(input);+ const copies = new Map([[copyKey!, "IMG-001"]]);+ const manual = rewriteItems(input, describe, new Set(), noPixels, { copies });+ assert.deepEqual(texts(manual.items[5]), ["Script completed", FETCHED, COPY_GONE]);+ assert.deepEqual([manual.report.copies, manual.report.replaced.length, manual.report.noteAt], [["IMG-001"], 0, undefined]);+ const auto = rewriteItems(input, describe, keys("IMG-001", "IMG-002", "IMG-003", "IMG-004"), noPixels, { copies, auto: { currentTurn: 3 } });+ assert.equal(texts(auto.items[6])[2], COPY_GONE);+ assert.deepEqual(modes(auto.report).map(([id]) => id), ["IMG-001", "IMG-002", "IMG-003", "IMG-004"]);+ const english = rewriteItems(input, describe, new Set(), noPixels, { lang: "en", copies });+ assert.equal(texts(english.items[5])[2], "[Original of IMG-001: fetched by the model with cam_view_image, included only in that turn, now omitted]");+});++test("the note for automatic selection keeps what stops retractions and guesses, and says how to fetch", () => {+ const rules = {+ zh: [/开着“自动选图”/, /真实存在,你当时收到并看过/, /不要因为现在看不到,就认为之前的回答是猜测或错误/, /调用 cam_view_image 工具,传入它的编号/, /之前说过的内容够用时,不要调用/, /只在本轮提供/, /不要说看不到/],+ en: [/automatic image selection on/, /really existed in the turn where they appeared/, /Do not treat your earlier answers as guesses or mistakes/, /call the cam_view_image tool with its id/, /When what you said before is enough, do not call it/, /included only in this turn/, /do not say you cannot see it/],+ };+ for (const lang of ["zh", "en"] as const) for (const rule of rules[lang]) assert.match(autoNote(lang), rule, `${lang}: ${rule}`);+});diff --git a/spike/src/selection.test.ts b/spike/src/selection.test.tsindex 772582c..f9d2e1f 100644--- a/spike/src/selection.test.ts+++ b/spike/src/selection.test.ts@@ -7,7 +7,7 @@ import { existsSync, mkdirSync, mkdtempSync, writeFileSync } from "node:fs";import { tmpdir } from "node:os";import { join } from "node:path";import test from "node:test";-import { hasUnchecked } from "../../plugin/src/proxy.ts";+import { hasUnchecked, needsRewrite } from "../../plugin/src/proxy.ts";import { effectiveSelection, readSelection, writeSelection } from "../../plugin/src/selection.ts";import { startThread } from "./testfixtures.ts";@@ -66,3 +66,26 @@ test("a thread whose rollout is not written yet is looked up again later", () =>startThread(sessionsDir, LATE, { forked_from_id: PARENT, timestamp: "2026-09-24T12:00:00Z" });assert.deepEqual(Object.values(effectiveSelection(LATE, sessionsDir, dir).unchecked).map((entry) => entry.id), ["IMG-001"]);});++// v0.3: automatic selection's switch and pins are inherited the same way. FORK was made at 12:00.+test("a fork takes automatic selection when it was on at the fork, and the pins made before it", () => {+ const { sessionsDir, dir } = setup();+ const pinned = { "msg_1#0": { id: "IMG-001", at: "2026-09-24T11:40:00Z" }, "msg_2#0": { id: "IMG-002", at: "2026-09-24T13:00:00Z" } };+ writeSelection({ threadId: PARENT, unchecked: {}, auto: true, autoAt: "2026-09-24T11:30:00Z", autoSince: "2026-09-24T11:30:00Z", pinned }, dir);+ const fork = effectiveSelection(FORK, sessionsDir, dir);+ assert.equal(fork.auto, true);+ assert.deepEqual(Object.values(fork.pinned ?? {}).map((entry) => entry.id), ["IMG-001"]);+ assert.equal(needsRewrite(FORK, dir, sessionsDir), true);+});++test("switched on again after the fork, the switch stays the parent's; first used after it, nothing comes along", () => {+ const again = setup();+ writeSelection({ threadId: PARENT, unchecked: {}, auto: true, autoAt: "2026-09-24T12:30:00Z", autoSince: "2026-09-24T11:00:00Z" }, again.dir);+ const fork = effectiveSelection(FORK, again.sessionsDir, again.dir);+ assert.equal(fork.auto, false);+ assert.equal(needsRewrite(FORK, again.dir, again.sessionsDir), true, "copies fetched before the fork are still left out of its requests");+ const later = setup();+ writeSelection({ threadId: PARENT, unchecked: {}, auto: true, autoAt: "2026-09-24T12:30:00Z", autoSince: "2026-09-24T12:30:00Z" }, later.dir);+ assert.equal(effectiveSelection(FORK, later.sessionsDir, later.dir).auto, undefined);+ assert.equal(needsRewrite(FORK, later.dir, later.sessionsDir), false);+});diff --git a/spike/src/thread-index.test.ts b/spike/src/thread-index.test.tsindex d5ceee5..f456986 100644--- a/spike/src/thread-index.test.ts+++ b/spike/src/thread-index.test.ts@@ -172,3 +172,22 @@ test("when a page the history starts in is gone for good (its task deleted), theassert.deepEqual(loadThreadIndex(dir, THREAD).images.map((i) => i.name), ["b.png"], "the engine reads what is left too");assert.throws(() => readThreadHistory(dir, "01a0d301-0000-7000-8000-0000000000ee"), /no rollout found/, "a thread without a page of its own still has no history");});++// v0.3: what the model fetched with cam_view_image is the same image again, kept apart as a copy of the original.+test("an image the model fetched with cam_view_image is a copy without an id, and the ids after it stay", () => {+ const { dir, day } = sessions();+ const fetched = (callId: string, label: string, bytes: Buffer) =>+ line("response_item", { type: "custom_tool_call", call_id: callId, name: "exec", input: 'await tools.mcp__codex_attachment_manager__cam_view_image({ids:["IMG-001"]})' }) ++ line("response_item", { type: "custom_tool_call_output", call_id: callId, output: [{ type: "input_text", text: "Script completed" }, { type: "input_text", text: label }, { type: "input_image", image_url: url(bytes) }] });+ writeFileSync(join(day, `rollout-2026-09-24T10-00-00-${THREAD}.jsonl`),+ line("session_meta", { id: THREAD }) ++ turn("t1") + upload("msg_1", "t1", "a.png", red) ++ turn("t2") + fetched("c1", "[图片 IMG-001 取回的原图|a.png|用户上传|第 1 轮|4×4]", red) ++ // A line naming IMG-001 before an image that is not IMG-001 makes no copy.+ fetched("c2", "[Image IMG-001 fetched original | a.png | uploaded by the user | turn 1 | 4×4]", blue) ++ turn("t3") + upload("msg_3", "t3", "b.png", blue));+ const index = loadThreadIndex(dir, THREAD);+ assert.deepEqual(index.images.map((image) => [image.id, image.turn]), [["IMG-001", 1], ["IMG-002", 2], ["IMG-003", 3]]);+ assert.deepEqual([...index.copies.values()], [{ key: "custom_tool_call_output:c1#0", of: "IMG-001", turn: 2 }]);+ assert.equal(index.byKey.has("custom_tool_call_output:c1#0"), false);+});