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(panel): 下一条消息的发送预览和请求大小对比;改写不了时在面板上提示
chipfighter2db9f123344a
diff --git a/spike/scripts/panel-dev.ts b/spike/scripts/panel-dev.tsindex 5e53c57..1fa0fbb 100644--- a/spike/scripts/panel-dev.ts+++ b/spike/scripts/panel-dev.ts@@ -2,10 +2,11 @@// MCP Apps host side of the bridge) around the real panel.html, and answers its tool calls with the real plugin// server code over a synthetic thread of test images (no user material).// Input: none; `node spike/scripts/panel-dev.ts [--port 17895]`. Output: http://127.0.0.1:<port>/-// (?theme=dark, ?solo=1 for the panel alone, ?demo=pending|preview for a state to screenshot, ?slow=1 for slow calls).+// (?theme=dark, ?solo=1 for the panel alone, ?demo=pending|preview for a state to screenshot, ?slow=1 for slow calls,+// ?stats=none|skipped|websocket for other engine statistics than a normal rewritten request).// Everything is written to a temporary folder; nothing in the user's Codex home is read or changed.-import { mkdirSync, mkdtempSync, readFileSync, writeFileSync } from "node:fs";+import { mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs";import { createServer } from "node:http";import { tmpdir } from "node:os";import { dirname, join } from "node:path";@@ -51,10 +52,31 @@ writeFileSync(join(day, `rollout-2026-09-24T10-00-00-${THREAD}.jsonl`),line("response_item", { type: "image_generation_call", id: "ig_1", status: "completed", result: png(256, 256, (x, y) => [x, y, 180]).toString("base64") }) + say("需要 IMG-001 才能回答。", "t4"));const { callTool } = await import("../src/plugin-server.ts");+const { imageSizesOf } = await import("../src/proxy.ts");+const { recordRequest } = await import("../src/request-stats.ts");+const { requestStatsDirOf } = await import("../src/paths.ts");const sessionsDir = join(root, "sessions");const meta = { threadId: THREAD, thread_id: THREAD };-// Start with the red square and the copy of the blue circle unchecked, so both placeholder kinds show.-callTool("cam_set_selection", { uncheck: ["IMG-001", "IMG-005"] }, meta, sessionsDir);+// Start with the red square, the copy of the blue circle and the large noise image unchecked, so both placeholder+// kinds and a visible saving show.+callTool("cam_set_selection", { uncheck: ["IMG-001", "IMG-005", "IMG-006"] }, meta, sessionsDir);++// What the engine would have recorded: the last full request came in turn 3 (turn 4's images are new since) and had+// IMG-001, IMG-005 and IMG-006 replaced. ?stats=skipped|websocket|none shows the other cases.+function writeStats(kind: string): void {+ const dir = requestStatsDirOf(process.env.CAM_DATA_DIR);+ rmSync(join(dir, `${THREAD}.json`), { force: true });+ if (kind === "none") return;+ const items = readFileSync(join(day, `rollout-2026-09-24T10-00-00-${THREAD}.jsonl`), "utf8").trim().split("\n").map((raw) => JSON.parse(raw)).filter((record) => record.type === "response_item").map((record) => record.payload);+ const sizes = Object.fromEntries(Object.entries(imageSizesOf(items)).slice(0, 6));+ const imageChars = Object.values(sizes).reduce((sum, chars) => sum + chars, 0);+ const before = imageChars + 180_000;+ const replaced = [sizes["msg_1#0"], sizes["msg_3#0"], sizes["msg_3#1"]];+ const rewrite = kind === "skipped" ? { skipped: "thread index: Error: demo" } : { replaced: [{ id: "IMG-001" }, { id: "IMG-005" }, { id: "IMG-006" }], decodedBefore: before, decodedAfter: before - replaced.reduce((sum, chars) => sum + chars, 0) + 2100 };+ const at = new Date(Date.now() - 6 * 60_000).toISOString();+ recordRequest(THREAD, { at, transport: "http", turnId: "t3", decodedBytes: before, imageSizes: sizes, rewrite }, dir);+ if (kind === "websocket") recordRequest(THREAD, { at: new Date().toISOString(), transport: "websocket", event: "active-while-unchecked" }, dir);+}// ---- the host page: a stand-in for Codex's side panel ----const HOST = `<!doctype html><html lang="zh-CN"><head><meta charset="utf-8"><title>面板开发页</title><style>@@ -70,7 +92,7 @@ const HOST = `<!doctype html><html lang="zh-CN"><head><meta charset="utf-8"><tit.tools { position: fixed; left: 10px; top: 10px; display: flex; gap: 6px; } .tools button { font: inherit; }body.solo .chat, body.solo .tools, body.solo .tabs { display: none; } body.solo .panel { width: 100%; border: 0; }</style></head><body><div class="tools"><button id="theme">切换明暗</button><button id="grow">加一张图</button></div>-<div class="chat">(对话区)</div><div class="panel"><div class="tabs"><span class="tab">上下文素材</span></div><iframe id="app" src="/panel.html"></iframe></div>+<div class="chat">(对话区)</div><div class="panel"><div class="tabs"><span class="tab">上下文素材</span></div><iframe id="app"></iframe></div><script>const params = new URLSearchParams(location.search);let theme = params.get("theme") === "dark" ? "dark" : "light";@@ -98,6 +120,8 @@ const HOST = `<!doctype html><html lang="zh-CN"><head><meta charset="utf-8"><tit}document.getElementById("theme").onclick = () => { theme = theme === "dark" ? "light" : "dark"; document.body.classList.toggle("dark", theme === "dark"); send({ method: "ui/notifications/host-context-changed", params: { theme } }); };document.getElementById("grow").onclick = () => fetch("/grow", { method: "POST" });+ // The engine statistics for this page load, then the panel.+ fetch("/stats?kind=" + (params.get("stats") || "normal"), { method: "POST" }).then(() => { frame.src = "/panel.html"; });</script></body></html>`;let extra = 0;@@ -105,6 +129,10 @@ createServer((request, response) => {const reply = (status: number, type: string, body: string) => { response.writeHead(status, { "content-type": type, "cache-control": "no-store" }); response.end(body); };if (request.method === "GET" && (request.url === "/" || request.url?.startsWith("/?"))) return reply(200, "text/html; charset=utf-8", HOST);if (request.method === "GET" && request.url === "/panel.html") return reply(200, "text/html; charset=utf-8", readFileSync(join(here, "..", "src", "panel.html"), "utf8"));+ if (request.method === "POST" && request.url?.startsWith("/stats")) {+ writeStats(new URL(request.url, "http://x").searchParams.get("kind") ?? "normal");+ return reply(200, "application/json", "{}");+ }if (request.method === "POST" && request.url === "/grow") {// A new turn with one more upload, to watch the panel pick it up by polling.extra++;diff --git a/spike/src/engine.test.ts b/spike/src/engine.test.tsindex 89a8229..c41dfd2 100644--- a/spike/src/engine.test.ts+++ b/spike/src/engine.test.ts@@ -11,7 +11,7 @@ import { dirname, join } from "node:path";import test from "node:test";import { fileURLToPath } from "node:url";import { countCodexProcesses, engineHealth, ensureEngine, isEngineHealth, watchForCodex } from "./engine.ts";-import { recordRequest } from "./proxy.ts";+import { readRequestStats, recordRequest } from "./request-stats.ts";const here = dirname(fileURLToPath(import.meta.url));@@ -58,9 +58,19 @@ test("ensure starts one engine; a second start on the same port leaves it alone"test("request statistics are written per thread, and only for real thread ids", () => {const dir = mkdtempSync(join(tmpdir(), "cam-stats-"));const thread = "01a0d301-0000-7000-8000-000000000001";- recordRequest(thread, { at: "2026-09-24T12:00:00Z", images: 3 }, dir);- assert.equal(JSON.parse(readFileSync(join(dir, `${thread}.json`), "utf8")).images, 3);+ recordRequest(thread, { at: "2026-09-24T12:00:00Z", transport: "http", images: 3 }, dir);+ assert.equal(JSON.parse(readFileSync(join(dir, `${thread}.json`), "utf8")).latest.images, 3);recordRequest("../escape", { images: 1 }, dir);recordRequest(null, { images: 1 }, dir);assert.equal(existsSync(join(dir, "..", "escape.json")), false);});++test("a WebSocket event becomes the latest without erasing the last full HTTP request", () => {+ const dir = mkdtempSync(join(tmpdir(), "cam-stats-"));+ const thread = "01a0d301-0000-7000-8000-000000000002";+ recordRequest(thread, { at: "t1", transport: "http", decodedBytes: 100 }, dir);+ recordRequest(thread, { at: "t2", transport: "websocket", event: "open" }, dir);+ assert.deepEqual(readRequestStats(thread, dir), { latest: { at: "t2", transport: "websocket", event: "open" }, lastHttp: { at: "t1", transport: "http", decodedBytes: 100 } });+ recordRequest(thread, { at: "t3", transport: "http", decodedBytes: 50 }, dir);+ assert.equal(readRequestStats(thread, dir)!.lastHttp!.decodedBytes, 50);+});diff --git a/spike/src/panel-state.test.ts b/spike/src/panel-state.test.tsindex e7d6af2..6a758b6 100644--- a/spike/src/panel-state.test.ts+++ b/spike/src/panel-state.test.ts@@ -5,7 +5,9 @@import assert from "node:assert/strict";import test from "node:test";import { decodePng } from "./png.ts";-import { applySelection, imageFor, loadPanelState, requestedIds } from "./panel-state.ts";+import { applySelection, imageFor, loadPanelState, requestedIds, sendInfo } from "./panel-state.ts";+import { requestStatsDirOf } from "./paths.ts";+import { recordRequest } from "./request-stats.ts";import { assistant, line, red, sampleSessions, THREAD, turn } from "./testfixtures.ts";import { shrink } from "./thumbnail.ts";@@ -27,7 +29,37 @@ test("panel state lists every image with checked, same-content and requested fla]);assert.deepEqual(state.requested, ["IMG-001"]);assert.equal(state.totals.checkedBytes, state.totals.allBytes);- assert.equal(state.lastRequest, null);+ assert.deepEqual(state.send, { baseline: null, last: null, notice: null });+ assert.deepEqual(state.images.map((image) => [image.inLastRequest, image.inNextRequest]), [[null, true], [null, true], [null, true]], "without a recorded request every image counts");+});++test("the size baseline: images the last request carried, images added since, and images compacted away", () => {+ const options = sampleSessions(turn("t3") + line("response_item", {+ type: "message", id: "msg_3", role: "user",+ content: [{ type: "input_image", image_url: `data:image/png;base64,${red.toString("base64")}` }],+ internal_chat_message_metadata_passthrough: { turn_id: "t3" },+ }));+ // The last request (turn 2) carried IMG-002 and IMG-003; IMG-001 had been compacted away; IMG-004 came later.+ recordRequest(THREAD, { at: "2026-09-24T12:00:00Z", transport: "http", turnId: "t2", decodedBytes: 5000, imageSizes: { "msg_1#1": 100, "msg_1#2": 100 }, rewrite: { replaced: [{ id: "IMG-003" }], decodedBefore: 5000, decodedAfter: 4500 } }, requestStatsDirOf(options.dataRoot));+ const state = loadPanelState(THREAD, options);+ assert.deepEqual(state.images.map((image) => [image.id, image.inLastRequest, image.inNextRequest]), [+ ["IMG-001", false, false], ["IMG-002", true, true], ["IMG-003", true, true], ["IMG-004", false, true],+ ]);+ assert.deepEqual(state.send, {+ baseline: { at: "2026-09-24T12:00:00Z", bytes: 5000 },+ last: { at: "2026-09-24T12:00:00Z", bytesBefore: 5000, bytesAfter: 4500, replaced: 1, skipped: false },+ notice: null,+ });+});++test("the panel is told when the engine could not rewrite, or a WebSocket turn kept the unchecked images", () => {+ const http = { at: "t1", transport: "http", decodedBytes: 900, rewrite: { skipped: "thread index: Error: boom" } };+ assert.deepEqual(sendInfo({ latest: http, lastHttp: http }).notice, { kind: "skipped", at: "t1", reason: "读取这个任务的记录失败" });+ assert.deepEqual(sendInfo({ latest: { ...http, rewrite: { skipped: "rewrite failed: x" } }, lastHttp: { ...http, rewrite: { skipped: "rewrite failed: x" } } }).notice?.kind, "skipped");+ const busy = { at: "t2", transport: "websocket", event: "active-while-unchecked" };+ assert.deepEqual(sendInfo({ latest: busy, lastHttp: http }).notice, { kind: "websocket", at: "t2" });+ assert.equal(sendInfo({ latest: { at: "t3", transport: "http", decodedBytes: 800, rewrite: { replaced: [] } }, lastHttp: null }).notice, null);+ assert.equal(sendInfo({ latest: http, lastHttp: http }).baseline, null, "a request without image sizes is no baseline");});test("check and uncheck are kept per thread and validated", () => {diff --git a/spike/src/panel-state.ts b/spike/src/panel-state.tsindex fd3898e..c93c488 100644--- a/spike/src/panel-state.ts+++ b/spike/src/panel-state.ts@@ -1,12 +1,12 @@// Purpose: P3-3 — what the panel shows for one thread: every image occurrence with its state, the ids the model asked// for in its latest reply, and the engine's statistics of the latest request. Also applies check/uncheck actions.-// P4 — serves each image for the panel's thumbnails and previews.+// P4 — serves each image for the panel's thumbnails and previews, and the inputs of its "next message" estimate:+// the last full request as a size baseline, which images it carried, and whether the engine could not rewrite it.// Input: thread id, the sessions directory (rollouts are only read) and the tool's data directory.// Output: PanelState as plain JSON; selection changes are written to <data dir>/selection/.-import { existsSync, readFileSync } from "node:fs";-import { join } from "node:path";import { dataDir, requestStatsDirOf, selectionDirOf } from "./paths.ts";+import { readRequestStats, type RequestStats } from "./request-stats.ts";import { effectiveSelection, writeSelection } from "./selection.ts";import { buildIndex, imageData, readThreadHistory, type IndexedImage, type ThreadIndex } from "./thread-index.ts";import { pngThumbnail } from "./thumbnail.ts";@@ -15,14 +15,50 @@ type Json = Record<string, any>;type Record_ = { type: string; payload: Json };export type PanelImage = {id: string; kind: string; name: string | null; label: string | null; turn: number | null;- width: number | null; height: number | null; bytes: number;+ width: number | null; height: number | null; bytes: number; base64Chars: number;sameAs: string[]; checked: boolean; replaceable: boolean; requested: boolean;+ // Whether the last full request carried it (null: no such request recorded yet), and whether the next one will+ // (carried last time, or added since; images compacted out of the history will not come back).+ inLastRequest: boolean | null; inNextRequest: boolean;+};+export type SendInfo = {+ // The last full HTTP request as Codex built it, before any rewrite: the baseline of the estimate.+ baseline: { at: string; bytes: number } | null;+ // What the engine sent for that request.+ last: { at: string; bytesBefore: number; bytesAfter: number; replaced: number; skipped: boolean } | null;+ notice: { kind: "skipped"; at: string; reason: string } | { kind: "websocket"; at: string } | null;};export type PanelState = {threadId: string; turns: number; images: PanelImage[]; requested: string[];totals: { images: number; unchecked: number; checkedBytes: number; allBytes: number };- lastRequest: Json | null;+ send: SendInfo;};++// The engine's reasons for forwarding a request unchanged, in the user's words.+const SKIP_REASONS: Array<[RegExp, string]> = [+ [/^undecodable body/, "请求内容无法解压"],+ [/^unparsable body/, "请求内容无法解析"],+ [/^no input array/, "请求的格式和预期不同"],+ [/^integer beyond/, "请求里有超大整数,改写会改变它的值"],+ [/^thread index/, "读取这个任务的记录失败"],+];++export function sendInfo(stats: RequestStats | null): SendInfo {+ const latest = stats?.latest ?? null;+ const http = stats?.lastHttp ?? null;+ const bytes = typeof http?.decodedBytes === "number" ? http.decodedBytes : null;+ const rewrite = http?.rewrite ?? null;+ let notice: SendInfo["notice"] = null;+ if (latest?.transport === "websocket" && (latest.event === "active-while-unchecked" || latest.activeWhileUnchecked)) notice = { kind: "websocket", at: latest.at };+ else if (latest?.transport === "http" && rewrite?.skipped) {+ notice = { kind: "skipped", at: http!.at, reason: SKIP_REASONS.find(([pattern]) => pattern.test(rewrite.skipped))?.[1] ?? "改写时出错" };+ }+ return {+ baseline: http && bytes !== null && http.imageSizes ? { at: http.at, bytes } : null,+ last: http && bytes !== null ? { at: http.at, bytesBefore: bytes, bytesAfter: rewrite?.decodedAfter ?? bytes, replaced: rewrite?.replaced?.length ?? 0, skipped: !!rewrite?.skipped } : null,+ notice,+ };+}export type PanelOptions = { sessionsDir: string; dataRoot?: string };// "需要 IMG-004" or a list right after it ("需要 IMG-004、IMG-002 和 IMG-007"); a full stop ends the list.@@ -57,16 +93,25 @@ export function sameContent(index: ThreadIndex, image: IndexedImage): string[] {.map((other) => other.id);}-export function panelState(threadId: string, history: Record_[], index: ThreadIndex, unchecked: Set<string>, lastRequest: Json | null): PanelState {+export function panelState(threadId: string, history: Record_[], index: ThreadIndex, unchecked: Set<string>, stats: RequestStats | null): PanelState {const requested = requestedIds(history);- const images = index.images.map((image): PanelImage => ({- id: image.id, kind: image.kind, name: image.name, label: image.label, turn: image.turn,- width: image.width, height: image.height, bytes: image.bytes,- sameAs: sameContent(index, image),- checked: !unchecked.has(image.key),- replaceable: image.replaceable,- requested: requested.includes(image.id),- }));+ const carried: Record<string, number> | null = stats?.lastHttp?.imageSizes ?? null;+ // Images after the last one that request carried, or from a later turn, were added since and go out next time.+ const lastCarried = carried ? index.images.reduce((last, image, position) => (image.key in carried ? position : last), -1) : -1;+ const lastTurn = stats?.lastHttp?.turnId ? index.turnNumbers.get(stats.lastHttp.turnId) ?? null : null;+ const images = index.images.map((image, position): PanelImage => {+ const inLast = carried ? image.key in carried : null;+ return {+ id: image.id, kind: image.kind, name: image.name, label: image.label, turn: image.turn,+ width: image.width, height: image.height, bytes: image.bytes, base64Chars: image.base64Chars,+ sameAs: sameContent(index, image),+ checked: !unchecked.has(image.key),+ replaceable: image.replaceable,+ requested: requested.includes(image.id),+ inLastRequest: inLast,+ inNextRequest: inLast !== false || position > lastCarried || (lastTurn !== null && image.turn !== null && image.turn > lastTurn),+ };+ });return {threadId, turns: index.turns, images, requested,totals: {@@ -75,7 +120,7 @@ export function panelState(threadId: string, history: Record_[], index: ThreadIncheckedBytes: images.filter((image) => image.checked).reduce((sum, image) => sum + image.bytes, 0),allBytes: images.reduce((sum, image) => sum + image.bytes, 0),},- lastRequest,+ send: sendInfo(stats),};}@@ -94,18 +139,17 @@ function load(threadId: string, options: PanelOptions) {const history = historyOf(threadId, options.sessionsDir);const index = buildIndex(threadId, history);const selection = effectiveSelection(threadId, options.sessionsDir, selectionDirOf(root));- const statsFile = join(requestStatsDirOf(root), `${threadId}.json`);- const lastRequest = existsSync(statsFile) ? JSON.parse(readFileSync(statsFile, "utf8")) : null;- return { root, history, index, selection, lastRequest };+ const stats = readRequestStats(threadId, requestStatsDirOf(root));+ return { root, history, index, selection, stats };}export function loadPanelState(threadId: string, options: PanelOptions): PanelState {- const { history, index, selection, lastRequest } = load(threadId, options);- return panelState(threadId, history, index, new Set(Object.keys(selection.unchecked)), lastRequest);+ const { history, index, selection, stats } = load(threadId, options);+ return panelState(threadId, history, index, new Set(Object.keys(selection.unchecked)), stats);}export function applySelection(threadId: string, change: { uncheck?: string[]; check?: string[]; checkAll?: boolean }, options: PanelOptions): PanelState {- const { root, history, index, selection, lastRequest } = load(threadId, options);+ const { root, history, index, selection, stats } = load(threadId, options);const unchecked = change.checkAll ? {} : { ...selection.unchecked };const byId = new Map(index.images.map((image) => [image.id, image]));for (const id of change.uncheck ?? []) {@@ -120,7 +164,7 @@ export function applySelection(threadId: string, change: { uncheck?: string[]; cdelete unchecked[image.key];}writeSelection({ threadId, unchecked }, selectionDirOf(root));- return panelState(threadId, history, index, new Set(Object.keys(unchecked)), lastRequest);+ return panelState(threadId, history, index, new Set(Object.keys(unchecked)), stats);}// One image for the panel: a PNG larger than maxSide is scaled down; smaller PNGs and other formats (which thediff --git a/spike/src/panel.html b/spike/src/panel.htmlindex 060d4cb..8036241 100644--- a/spike/src/panel.html+++ b/spike/src/panel.html@@ -56,8 +56,17 @@.title h1 { margin: 0; font-size: 15px; font-weight: 600; letter-spacing: .01em; }.count { padding: 0 7px; border-radius: 999px; background: var(--bg-soft); color: var(--text-2); font-size: 12px; font-weight: 500; line-height: 20px; font-variant-numeric: tabular-nums; }.count:empty { display: none; }- .summary { margin-top: 4px; color: var(--text-3); font-size: 12.5px; font-variant-numeric: tabular-nums; }- .summary b { font-weight: 600; color: var(--text); }+ .plan { margin-top: 4px; color: var(--text-2); font-size: 12.5px; font-variant-numeric: tabular-nums; }+ .plan b { font-weight: 600; color: var(--text); }+ .meter { margin-top: 12px; padding: 10px 12px 11px; border-radius: 12px; background: var(--bg-soft); }+ .meter-head { display: flex; align-items: baseline; gap: 8px; color: var(--text-2); font-size: 12.5px; }+ .meter-head b { color: var(--text); font-size: 15px; font-weight: 600; font-variant-numeric: tabular-nums; }+ .meter-full { margin-left: auto; color: var(--text-3); white-space: nowrap; font-variant-numeric: tabular-nums; }+ .meter-full.saved { color: var(--ok); }+ .bar { margin-top: 8px; height: 6px; border-radius: 999px; background: color-mix(in srgb, var(--text) 9%, transparent); overflow: hidden; }+ .bar i { display: block; height: 100%; border-radius: inherit; background: var(--info); transition: width .25s ease; }+ .last { margin-top: 8px; color: var(--text-3); font-size: 12px; font-variant-numeric: tabular-nums; }+ .last:empty { display: none; }main { flex: 1; min-height: 0; overflow-y: auto; padding: 0 10px 18px; border-top: 1px solid var(--border); scrollbar-width: thin; scrollbar-color: var(--border-strong) transparent; }.turn { display: flex; align-items: center; gap: 10px; margin: 18px 8px 6px; color: var(--text-3); font-size: 12px; font-weight: 500; }@@ -157,7 +166,12 @@<body><header><div class="title"><h1>图片</h1><span class="count" id="count"></span></div>- <div class="summary" id="summary">正在读取…</div>+ <div class="plan" id="plan">正在读取…</div>+ <div class="meter" id="meter" hidden>+ <div class="meter-head"><span id="meterLabel"></span><b id="meterValue"></b><span class="meter-full" id="meterFull"></span></div>+ <div class="bar"><i id="meterBar"></i></div>+ </div>+ <div class="last" id="last"></div></header><main id="main"></main><footer>@@ -334,17 +348,44 @@const formatBytes = (bytes) => bytes < 1024 ? `${bytes} B` : bytes < 1024 * 1024 ? `${Math.round(bytes / 1024)} KB` : `${(bytes / 1024 / 1024).toFixed(1)} MB`;const describe = (image) => [KIND[image.kind] ?? image.kind, image.width && image.height ? `${image.width}×${image.height}` : null, formatBytes(image.bytes)].filter(Boolean).join(" · ");+ // An unchecked image becomes a duplicate placeholder when a copy of the same content is still sent in that request.+ const sentCopyOf = (image) => image.sameAs.find((id) => { const other = byId(id); return other && other.inNextRequest && isChecked(other); });+function tagsOf(image) {const tags = [];- const checked = isChecked(image);- const sentCopy = image.sameAs.find((id) => { const other = byId(id); return other && isChecked(other); });if (!image.replaceable) tags.push(["无法取消", ""]);+ // Compacted out of the history: it is not sent either way.+ if (!image.inNextRequest) { tags.push(["已不在上下文", ""]); return tags; }+ const checked = isChecked(image);+ const sentCopy = sentCopyOf(image);if (!checked && image.requested) tags.push(["模型需要这张", "warn"]);if (!checked) tags.push([sentCopy ? `由 ${sentCopy} 代替` : "不发送", ""]);else if (image.sameAs.length) tags.push([`与 ${image.sameAs[0]} 相同`, ""]);return tags;}+ // ---- the next message: what goes out, and the request size against sending everything (an estimate) ----+ const PLACEHOLDER_CHARS = 700;+ function forecast() {+ const list = images();+ const counted = list.filter((image) => image.inNextRequest);+ const dropped = counted.filter((image) => image.replaceable && !isChecked(image));+ const saved = dropped.reduce((sum, image) => sum + Math.max(0, image.base64Chars - PLACEHOLDER_CHARS), 0);+ const baseline = S.data?.send?.baseline ?? null;+ // With a recorded request: its size plus the images added since. Without one: the images alone.+ const full = baseline+ ? baseline.bytes + counted.filter((image) => image.inLastRequest === false).reduce((sum, image) => sum + image.base64Chars, 0)+ : counted.reduce((sum, image) => sum + image.base64Chars, 0);+ return { whole: !!baseline, full, after: Math.max(0, full - saved), sent: counted.length - dropped.length, dropped: dropped.length, duplicates: dropped.filter(sentCopyOf).length, gone: list.length - counted.length };+ }+ function timeOf(at) {+ const date = new Date(at);+ if (Number.isNaN(date.getTime())) return "";+ const pad = (n) => String(n).padStart(2, "0");+ const time = `${pad(date.getHours())}:${pad(date.getMinutes())}`;+ return date.toDateString() === new Date().toDateString() ? time : `${date.getMonth() + 1}月${date.getDate()}日 ${time}`;+ }+function makeRow(id) {const row = document.createElement("div");row.className = "row";@@ -394,20 +435,43 @@function renderHeader() {const list = images();- if (!S.data) { $("count").textContent = ""; $("summary").textContent = S.error ? "读取失败" : "正在读取…"; return; }- const sent = list.filter(isChecked);+ const plan = $("plan");$("count").textContent = list.length ? String(list.length) : "";- const summary = $("summary");- if (!list.length) { summary.textContent = "还没有图片"; return; }- summary.replaceChildren(- "发送 ", Object.assign(document.createElement("b"), { textContent: String(sent.length) }), " 张",- list.length > sent.length ? ` · 不发送 ${list.length - sent.length} 张` : "",- sent.length ? ` · 约 ${formatBytes(sent.reduce((sum, image) => sum + image.bytes, 0))}` : "",+ if (!S.data || !list.length) {+ plan.textContent = !S.data ? (S.error ? "读取失败" : "正在读取…") : "还没有图片";+ $("meter").hidden = true;+ $("last").textContent = "";+ return;+ }+ const f = forecast();+ const bold = (text) => Object.assign(document.createElement("b"), { textContent: String(text) });+ plan.replaceChildren(+ "下一条消息:发送 ", bold(f.sent), " 张",+ f.dropped ? ` · 占位 ${f.dropped} 张${f.duplicates ? `(其中重复 ${f.duplicates} 张)` : ""}` : "",+ f.gone ? ` · ${f.gone} 张已不在上下文` : "",);+ $("meter").hidden = false;+ $("meterLabel").textContent = f.whole ? "预计请求" : "预计图片数据";+ $("meterValue").textContent = formatBytes(f.after);+ const saving = f.after < f.full;+ const percent = (1 - f.after / f.full) * 100;+ $("meterFull").textContent = saving ? `全部发送 ${formatBytes(f.full)} · 省 ${percent < 1 ? "<1" : Math.round(percent)}%` : "未取消任何图";+ $("meterFull").classList.toggle("saved", saving);+ $("meterBar").style.width = `${f.full > 0 ? Math.max(2, Math.round((f.after / f.full) * 100)) : 100}%`;+ const last = S.data.send?.last;+ $("last").textContent = !last ? (f.whole ? "" : "还没有记录到经过代理的完整请求,先按图片大小估算")+ : last.skipped ? `上一次请求 ${timeOf(last.at)}:没能替换,原样发出 ${formatBytes(last.bytesBefore)}`+ : last.replaced ? `上一次请求 ${timeOf(last.at)}:替换了 ${last.replaced} 张,少发 ${formatBytes(last.bytesBefore - last.bytesAfter)},实际发出 ${formatBytes(last.bytesAfter)}`+ : `上一次请求 ${timeOf(last.at)}:没有替换,发出 ${formatBytes(last.bytesBefore)}`;}+ // Notices are reused while their text stays the same, so re-rendering does not move the list around.+ const notices = new Map();function notice(tone, text, action) {+ const key = `${tone}|${text}`;+ if (notices.has(key)) return notices.get(key);const box = document.createElement("div");+ notices.set(key, box);box.className = `notice ${tone}`;const span = document.createElement("span");span.className = "grow";@@ -426,6 +490,9 @@const nodes = [];if (S.error) nodes.push(notice("error", S.error, { label: "重试", run: () => { S.error = null; render(); refresh(); } }));if (S.data?.engineRunning === false) nodes.push(notice("warn", "代理没有在运行,取消勾选暂时不会生效。关掉 Codex 再打开,一般就能恢复。"));+ const sendNotice = S.data?.send?.notice;+ if (sendNotice?.kind === "skipped") nodes.push(notice("error", `上一次请求(${timeOf(sendNotice.at)})没能按勾选替换,图片原样发给了模型。原因:${sendNotice.reason}。`));+ if (sendNotice?.kind === "websocket") nodes.push(notice("warn", "取消勾选时这一轮对话还在进行,这一轮剩下的请求仍带着原图;从下一条消息起生效。"));const list = images();if (S.data && !list.length) nodes.push(emptyState);let turn;@@ -446,6 +513,7 @@nodes.push(row);}for (const [id, row] of rows) if (!byId(id)) { observer.unobserve(row); rows.delete(id); }+ for (const [key, box] of notices) if (!nodes.includes(box)) notices.delete(key);// Only touch the list when its items changed, so scrolling and keyboard focus are kept.const current = main.children;if (nodes.length !== current.length || nodes.some((node, index) => node !== current[index])) main.replaceChildren(...nodes);diff --git a/spike/src/proxy.test.ts b/spike/src/proxy.test.tsindex 1361319..72a9013 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, outboundProxy, rewriteBody, requestIdentity } from "./proxy.ts";+import { describeBody, hasUnchecked, hasUnsafeInteger, imageSizesOf, outboundProxy, rewriteBody, requestIdentity } from "./proxy.ts";import { writeSelection } from "./selection.ts";import { png } from "./testkit.ts";@@ -49,6 +49,14 @@ test("request metadata counts images in messages and in tool outputs, without coassert.equal(describeBody("/backend-api/codex/images/edits", { model: "gpt-image", images: [{}, {}], size: "1024x1024" }).kind, "image_edit");});+test("the size baseline names each image by its index key, with its base64 length and nothing else", () => {+ const input = [+ { type: "message", id: "msg_1", role: "user", content: [{ type: "input_text", text: "secret words" }, { type: "input_image", image_url: "data:image/png;base64,AAAA" }] },+ { type: "custom_tool_call_output", call_id: "c1", output: [{ type: "input_image", image_url: "data:image/png;base64,BBBBBBBB" }] },+ ];+ assert.deepEqual(imageSizesOf(input), { "msg_1#0": 4, "custom_tool_call_output:c1#0": 8 });+});+test("integers JavaScript cannot hold exactly are detected outside strings only", () => {assert.equal(hasUnsafeInteger('{"a":12345678901234567}'), true);assert.equal(hasUnsafeInteger('{"a":"order 12345678901234567","b":1790246521.5642291234567,"c":[1,-2,3e25]}'), false);diff --git a/spike/src/proxy.ts b/spike/src/proxy.tsindex 8acbea1..930486c 100644--- a/spike/src/proxy.ts+++ b/spike/src/proxy.ts@@ -1,6 +1,8 @@// Purpose: the engine — local proxy between Codex and the ChatGPT backend. P1: HTTP requests and WebSocket upgrades// are forwarded byte for byte. P2: requests of threads with unchecked images are rewritten. P3: one instance per// machine, exits once no Codex process is left, and keeps per-thread statistics of the latest request.+// P4: the statistics also name the images each full request carried (the panel's size baseline), and note a+// WebSocket turn that kept running after images were unchecked (it cannot be rewritten).// 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 Windows proxy settings.@@ -8,7 +10,7 @@import { execFileSync } from "node:child_process";import { createHash } from "node:crypto";-import { appendFileSync, mkdirSync, renameSync, writeFileSync } from "node:fs";+import { appendFileSync, mkdirSync, writeFileSync } from "node:fs";import http, { type IncomingHttpHeaders } from "node:http";import net from "node:net";import { join } from "node:path";@@ -18,8 +20,9 @@ import { pathToFileURL } from "node:url";import zlib from "node:zlib";import { codexHome } from "./codexconfig.ts";import { DEFAULT_PORT, ENGINE_SERVICE, engineHealth, watchForCodex } from "./engine.ts";-import type { ImageRef } from "./images.ts";-import { dataDir, proxyLogDirOf, requestStatsDirOf } from "./paths.ts";+import { findImages, type ImageRef } from "./images.ts";+import { dataDir, proxyLogDirOf } from "./paths.ts";+import { recordRequest } from "./request-stats.ts";import { rewriteItems, type Described } from "./rewrite.ts";import { effectiveSelection, selectionDir } from "./selection.ts";import { loadThreadIndex, pixelHashOf, type ThreadIndex } from "./thread-index.ts";@@ -27,7 +30,6 @@ import { loadThreadIndex, pixelHashOf, type ThreadIndex } from "./thread-index.ttype Json = Record<string, any>;const UPSTREAM_HOST = "chatgpt.com";const HOP_BY_HOP = new Set(["connection", "keep-alive", "proxy-connection", "transfer-encoding", "te", "trailer", "upgrade", "host"]);-const THREAD_ID = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/;// Outbound proxy: environment first, then the Windows per-user setting; NO_PROXY is honoured for the upstream host.export function outboundProxy(env = process.env, windowsSetting = readWindowsProxy): { host: string; port: number } | null {@@ -130,13 +132,9 @@ function log(entry: Json): void {appendFileSync(join(dir, `${new Date().toISOString().slice(0, 10)}.jsonl`), `${JSON.stringify(entry)}\n`);}-// P3: the latest /responses request of each thread, for the panel's "what was sent" view. Metadata only.-export function recordRequest(threadId: string | null, stats: Json, dir = requestStatsDirOf()): void {- if (!threadId || !THREAD_ID.test(threadId)) return;- mkdirSync(dir, { recursive: true });- const file = join(dir, `${threadId}.json`);- writeFileSync(`${file}.tmp`, JSON.stringify(stats, null, 2));- renameSync(`${file}.tmp`, file);+// The images a request carried and the base64 characters each took, under the same keys as the thread index.+export function imageSizesOf(input: Json[]): Record<string, number> {+ return Object.fromEntries(findImages(input).map((ref) => [ref.key, ref.base64Chars]));}// P2-1 debugging aid for synthetic test threads only: the request with every inline image reduced to its hash and size.@@ -232,12 +230,20 @@ async function main(): Promise<void> {}// Open WebSocket connections per thread. Over WebSocket Codex only sends new items and the server keeps the rest,// so a thread with unchecked images must move to HTTP: its idle connections are closed, and its next upgrade gets 426.- const live = new Map<string, Set<{ client: Duplex; socket: tls.TLSSocket; last: number; closedForSelection: boolean }>>();+ type Live = { client: Duplex; socket: tls.TLSSocket; last: number; closedForSelection: boolean; activeWhileUnchecked: boolean; path: string; identity: Json };+ const live = new Map<string, Set<Live>>();setInterval(() => {for (const [threadId, connections] of live) {if (!hasUnchecked(threadId)) continue;for (const connection of connections) {- if (Date.now() - connection.last < 1500) continue;+ 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.+ if (!connection.activeWhileUnchecked) {+ connection.activeWhileUnchecked = true;+ recordRequest(threadId, { at: new Date().toISOString(), transport: "websocket", event: "active-while-unchecked", path: connection.path, ...connection.identity });+ }+ continue;+ }connection.closedForSelection = true;connection.socket.destroy();connection.client.destroy();@@ -269,10 +275,13 @@ async function main(): Promise<void> {const body = Buffer.concat(chunks);const decoded = req.method === "POST" ? decodeBody(body, req.headers["content-encoding"] as string | undefined) : null;let details: Json = {};+ let imageSizes: Record<string, number> | null = null;if (decoded) {try {const json = JSON.parse(decoded.toString("utf8"));details = describeBody(path, json);+ // Measured on the body as Codex sent it, before any rewrite: the panel's "everything sent" baseline.+ if (/\/responses$/.test(path) && Array.isArray(json.input)) imageSizes = imageSizesOf(json.input);if (dumpDir && /\/responses$/.test(path)) {mkdirSync(dumpDir, { recursive: true });const headers = Object.fromEntries(Object.entries(req.headers).filter(([name]) => /^(x-codex|session_id|conversation_id|openai-beta|content-)/.test(name)));@@ -282,7 +291,8 @@ async function main(): Promise<void> {}const entry = { at: new Date(started).toISOString(), id, transport: "http", method: req.method, path, status, requestBytes, decodedBytes: decoded?.length ?? null, contentEncoding: req.headers["content-encoding"] ?? null, responseBytes, ms: Date.now() - started, ...identity, ...details, ...extra, error: error ?? null };log(entry);- if (/\/responses$/.test(path)) recordRequest(identity.threadId, entry);+ // Image keys go to the per-thread statistics only, not to the log.+ if (/\/responses$/.test(path)) recordRequest(identity.threadId, imageSizes ? { ...entry, imageSizes } : entry);};// body === null streams the request through unchanged.const send = (headers: Record<string, string | string[]>, body: Buffer | null) => connectUpstream(via).then((socket) => {@@ -335,7 +345,7 @@ async function main(): Promise<void> {let down = 0;let upstreamStatus: string | null = null;connectUpstream(via).then((socket) => {- const connection = { client, socket, last: Date.now(), closedForSelection: false };+ const connection: Live = { client, socket, last: Date.now(), closedForSelection: false, activeWhileUnchecked: false, path, identity };if (identity.threadId) {if (!live.has(identity.threadId)) live.set(identity.threadId, new Set());live.get(identity.threadId)!.add(connection);@@ -363,7 +373,7 @@ async function main(): Promise<void> {socket.destroy();client.destroy();if (identity.threadId) live.get(identity.threadId)?.delete(connection);- const entry = { at: new Date(started).toISOString(), id, transport: "websocket", path, upstreamStatus, upBytes: up, downBytes: down, ms: Date.now() - started, closedForSelection: connection.closedForSelection, ...identity };+ const entry = { at: new Date(started).toISOString(), id, transport: "websocket", path, upstreamStatus, upBytes: up, downBytes: down, ms: Date.now() - started, closedForSelection: connection.closedForSelection, activeWhileUnchecked: connection.activeWhileUnchecked, ...identity };log(entry);recordRequest(identity.threadId, entry);};diff --git a/spike/src/request-stats.ts b/spike/src/request-stats.tsnew file mode 100644index 0000000..77dd740--- /dev/null+++ b/spike/src/request-stats.ts@@ -0,0 +1,36 @@+// Purpose: P4 — per thread, what the engine last saw: the latest event (an HTTP request or a WebSocket connection)+// and the latest full HTTP /responses request, which the panel uses as the baseline of its size estimate. Kept apart,+// so a WebSocket connection does not erase the baseline. Metadata only: sizes, counts, image keys, never content.+// Input/Output: <data dir>/state/requests/<thread id>.json, e.g. { "latest": {…}, "lastHttp": {…, "imageSizes": {…}} }.++import { existsSync, mkdirSync, readFileSync, renameSync, writeFileSync } from "node:fs";+import { join } from "node:path";+import { requestStatsDirOf } from "./paths.ts";++type Json = Record<string, any>;+export type RequestStats = { latest: Json; lastHttp: Json | null };++const THREAD_ID = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/;++export function readRequestStats(threadId: string, dir = requestStatsDirOf()): RequestStats | null {+ if (!THREAD_ID.test(threadId)) return null;+ const file = join(dir, `${threadId}.json`);+ if (!existsSync(file)) return null;+ try {+ const json = JSON.parse(readFileSync(file, "utf8"));+ // Files written before P4 held the latest entry alone.+ return "latest" in json ? json : { latest: json, lastHttp: json.transport === "http" ? json : null };+ } catch {+ return null;+ }+}++export function recordRequest(threadId: string | null, stats: Json, dir = requestStatsDirOf()): void {+ if (!threadId || !THREAD_ID.test(threadId)) return;+ mkdirSync(dir, { recursive: true });+ const file = join(dir, `${threadId}.json`);+ const next: RequestStats = { latest: stats, lastHttp: stats.transport === "http" ? stats : readRequestStats(threadId, dir)?.lastHttp ?? null };+ // Written to a temporary file first, so the panel never reads a half-written file.+ writeFileSync(`${file}.tmp`, JSON.stringify(next, null, 2));+ renameSync(`${file}.tmp`, file);+}diff --git a/spike/src/thread-index.ts b/spike/src/thread-index.tsindex 59f8640..384122a 100644--- a/spike/src/thread-index.ts+++ b/spike/src/thread-index.ts@@ -10,7 +10,7 @@ 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 };+export type ThreadIndex = { threadId: string; images: IndexedImage[]; byKey: Map<string, IndexedImage>; turns: number; turnNumbers: Map<string, number> };type Parsed = { offset: number; type: string; payload: Json };type FileCache = { size: number; mtimeMs: number; parsedTo: number; records: Parsed[] };@@ -129,7 +129,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 };+ return { threadId, images, byKey, turns: turnNumbers.size, turnNumbers };}// The first line alone (session_meta can be long), read in chunks without parsing the rest of the file.