A local plugin for image-heavy chats in Codex, Claude Code and other agents: earlier images stop going out with every message, and the model fetches the one it needs by its ID, also after compaction.
TypeScript ai-agentsclaude-codeclaude-code-plugincodexcodex-plugincontext-managementdeveloper-toolsimage-generationmcpmultimodal
fix(claude): 比较历史时按 Anthropic 的方式看值,不看写法
chipfightere5808206529b
diff --git a/plugin/src/claude-thinking.ts b/plugin/src/claude-thinking.tsindex 6bfcdc4..55d42f8 100644--- a/plugin/src/claude-thinking.ts+++ b/plugin/src/claude-thinking.ts@@ -19,10 +19,22 @@ const SESSION_ID = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12// Hashes of the latest histories only: a block is made after the history of the request just before it.const KEEP = 2000;-// Cache breakpoints move from request to request; thinking is not bound to them.-const withoutBreakpoints = (key: string, value: unknown) => (key === "cache_control" ? undefined : value);+// What Anthropic compares: values, not JSON formatting or key order; content given as a string is the text block it+// stands for (Claude Code sends its trailing system message as a block with a cache breakpoint, and as a string in the+// requests after); cache breakpoints move from request to request.+function canonical(value: unknown): unknown {+ if (Array.isArray(value)) return value.map(canonical);+ if (!value || typeof value !== "object") return value;+ const out: Json = {};+ for (const key of Object.keys(value).sort()) {+ const field = (value as Json)[key];+ if (key === "cache_control") continue;+ out[key] = key === "content" && typeof field === "string" ? [{ text: field, type: "text" }] : canonical(field);+ }+ return out;+}export const link = (previous: string, message: Json): string =>- createHash("sha256").update(previous).update("\n").update(JSON.stringify(message, withoutBreakpoints) ?? "").digest("hex");+ createHash("sha256").update(previous).update("\n").update(JSON.stringify(canonical(message)) ?? "").digest("hex");export const chainOf = (messages: Json[]): string => messages.reduce((chain: string, message) => link(chain, message), "");const thinkingIn = (message: Json): number =>diff --git a/spike/src/claude-thinking.test.ts b/spike/src/claude-thinking.test.tsindex 1a3b278..2b1f7ce 100644--- a/spike/src/claude-thinking.test.ts+++ b/spike/src/claude-thinking.test.ts@@ -71,10 +71,17 @@ test("an edit after a thinking block leaves that block alone", () => {assert.deepEqual(fit.messages[1], s.a1);});-test("cache breakpoints are not part of the history; a message of thinking alone is not emptied", () => {+test("cache breakpoints, key order and string content are not part of the history; a message of thinking alone is not emptied", () => {const s = session();const moved = [s.m0, s.a1, s.m2, s.a3, user(text("Next.", { cache_control: { type: "ephemeral" } }))];assert.equal(chainOf(moved), chainOf(s.original));+ // Claude Code 2.1.295: its trailing system message is a block with a breakpoint while it is last, a string after.+ const last = { role: "system", content: [text("<total_tokens>1 tokens left</total_tokens>", { cache_control: { type: "ephemeral" } })] };+ const later = { content: "<total_tokens>1 tokens left</total_tokens>", role: "system" };+ assert.equal(chainOf([s.m0, last]), chainOf([s.m0, later]));+ const result = (content: unknown) => user({ type: "tool_result", tool_use_id: "toolu_1", content });+ assert.equal(chainOf([result("done")]), chainOf([result([{ type: "text", text: "done" }])]));+ assert.notEqual(chainOf([s.m0, later]), chainOf([s.m0, { ...later, content: "<total_tokens>2 tokens left</total_tokens>" }]));const alone = assistant(think("s9"));const fit = fitThinking([s.m0, alone], [...omitted(s.m0), alone], new Map());assert.deepEqual([fit.kept, fit.left], [1, 0]);