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
refactor(panel): the panel computes from the core only
chipfighter68d05e526fab
diff --git a/docs/design.md b/docs/design.mdindex dae88bb..2b348b7 100644--- a/docs/design.md+++ b/docs/design.md@@ -22,7 +22,7 @@ v0.11 起代码分成三层([v0.11/plan.md](v0.11/plan.md)):`core/` 是不|---|---|---|| 引擎 | `proxy.ts`(入口)、`core/engine/transport.ts`、`adapters/codex/engine.ts`、`adapters/claude/engine.ts`、`engine.ts`、`core/routes.ts` | 本机代理:按绑定线路转发、改写请求、记录每个任务最近一次请求的统计;全机只有一个 || 插件服务 | `plugin-server.ts` | Codex 为每个会话启动的 MCP 服务:拉起并看守引擎,给面板提供数据;v0.3 起还给模型提供取图工具 |-| 面板 | `panel/panel.html`、`panel/page.ts`、`panel/panel-state.ts` | 侧边面板里的 MCP 应用,以及它要显示的数据 |+| 面板 | `panel/panel.html`、`panel/page.ts`、`panel/panel-state.ts`;Codex、Claude 各自的读取在 `adapters/codex/panel.ts`、`adapters/claude/panel.ts` | 侧边面板里的 MCP 应用,以及它要显示的数据 || 任务索引 | `adapters/codex/thread-index.ts`、`adapters/codex/children.ts`、`adapters/protocols/openai-responses/images.ts`、`core/images.ts`、`core/session.ts`、`core/pixels.ts`、`core/thumbnail.ts`、`core/png.ts`、`adapters/codex/thread-names.ts` | 从 Codex 的任务记录里只读地找出图片和它的来源、编号、生成缩略图、读任务名 || 改写 | `core/plan.ts`、`core/words.ts`、`adapters/protocols/openai-responses/rewrite.ts`、`core/selection.ts`、`adapters/codex/selection.ts`、`adapters/claude/selection.ts`、`core/request-stats.ts`、`core/notes.ts` | 每张图发不发、换成什么字(两个平台共用);发给模型的全部文字;把决定落进 Responses 请求;勾选状态;请求统计;特征行 || 接入和安装 | `adapters/codex/`:`codexconfig.ts`、`setup.ts`、`install.ts`、`codexcli.ts`、`plugin-cache.ts`、`binding.ts`;`cam.ts` | 管理 Codex 配置里带标记的块;用 Codex 的命令行装卸插件;保护升级中的旧运行文件;新任务绑定;`cam` 命令行 |@@ -167,7 +167,7 @@ v0.11 起代码分成三层([v0.11/plan.md](v0.11/plan.md)):`core/` 是不- 切换前,先把还没保存的勾选按原来的模式存完;切换进行中不接受勾选,免得记错地方。- 手动模式下不显示“最近一轮模型取回”,提示条照旧是“模型需要”。- 预计请求只算每轮都发的图,数字后面加“起”(英文是“+”),悬停说明模型取回的图不算在内。-- **子代理和旁聊**(v0.7,`panel-state.ts`):面板数据里的 `children` 是引擎登记的子代理和旁聊,按第一次请求的时间排,每个带名字(子代理的 `agent_nickname`,没有时用 `agent_path` 的最后一段)、最近一次完整请求带了几张图,以及和主任务一样算出的上次请求(替换几张、前后大小)。v0.8 起没有 `childrenAuto` 开关,每个也不再带“管不管”。+- **子代理和旁聊**(v0.7,`adapters/codex/panel.ts`):面板数据里的 `children` 是引擎登记的子代理和旁聊,按第一次请求的时间排,每个带名字(子代理的 `agent_nickname`,没有时用 `agent_path` 的最后一段)、最近一次完整请求带了几张图,以及和主任务一样算出的上次请求(替换几张、前后大小)。v0.8 起没有 `childrenAuto` 开关,每个也不再带“管不管”。- **任务名**:读 `<CODEX_HOME>/session_index.jsonl`,同一个任务以最后一行为准。- **新任务绑定**:在新任务页打开的面板,绑在 Codex 预先准备的任务上,发第一条消息后可能换成另一个任务。- 页面记下自己哪些时间段显示在屏幕上,读数据时带给插件服务。diff --git a/plugin/src/adapters/claude/panel-web.ts b/plugin/src/adapters/claude/panel-web.tsindex 6a04a4c..a9ef151 100644--- a/plugin/src/adapters/claude/panel-web.ts+++ b/plugin/src/adapters/claude/panel-web.ts@@ -6,7 +6,7 @@ import type { IncomingHttpHeaders } from "node:http";import { ownRequest } from "../../core/engine/transport.ts";import { webPanelPage } from "../../panel/page.ts";import { claudePanelLanguage } from "../../panel/panel-language.ts";-import { applyClaudeSelection, claudeImageFor, loadClaudePanelState, type ClaudePanelOptions } from "../../panel/panel-state.ts";+import { applyClaudeSelection, claudeImageFor, loadClaudePanelState, type ClaudePanelOptions } from "./panel.ts";type Json = Record<string, any>;diff --git a/plugin/src/adapters/claude/panel.ts b/plugin/src/adapters/claude/panel.tsnew file mode 100644index 0000000..59eccb2--- /dev/null+++ b/plugin/src/adapters/claude/panel.ts@@ -0,0 +1,61 @@+// Purpose: v0.4 — the panel's data for a Claude Code session (v0.11: split from panel/panel-state.ts,+// docs/v0.11/plan.md): from its transcript (claude-index.ts) and the selection and statistics kept for Claude sessions+// (paths.ts).+// Input: session id, the tool's data directory. Output: PanelState; selection changes are written.++import { claudeRequestStatsDirOf, claudeSelectionDirOf, dataDir } from "../../core/paths.ts";+import { readRequestStats, type RequestStats } from "../../core/request-stats.ts";+import { stableIdsSince, writeSelection, type Selection } from "../../core/selection.ts";+import { askedIn, changedSelection, imageUrl, joinTurns, offKeys, panelStateOf, piece, type PanelState, type SelectionChange } from "../../panel/panel-state.ts";+import { claudeHome, claudeImageData, claudeStartedAt, isPrompt, loadClaudeIndex, type ClaudeIndex, type ClaudeRecord } from "./claude-index.ts";+import { claudeSelection } from "./selection.ts";++export function claudeTurnTexts(chain: ClaudeRecord[]): Record<string, string> {+ const texts = new Map<number, string[]>();+ let turn = 0;+ for (const record of chain) {+ if (record.type === "user" && isPrompt(record)) turn++;+ const content = record.message?.content;+ if (!turn || (record.type !== "user" && record.type !== "assistant") || record.isMeta || record.isCompactSummary) continue;+ if (typeof content === "string") { piece(texts, turn, content); continue; }+ if (!Array.isArray(content)) continue;+ for (const block of content) {+ if (block?.type === "text") piece(texts, turn, block.text);+ else if (block?.type === "tool_use" && record.type === "assistant") piece(texts, turn, JSON.stringify(block.input ?? {}));+ }+ }+ return joinTurns(texts);+}++// kept for Claude sessions (paths.ts).+export type ClaudePanelOptions = { dataRoot?: string; home?: string };++function loadClaude(sessionId: string, options: ClaudePanelOptions) {+ const root = options.dataRoot ?? dataDir();+ const index = loadClaudeIndex(sessionId, options.home ?? claudeHome(), stableIdsSince(claudeSelectionDirOf(root)));+ const selection = claudeSelection(sessionId, claudeSelectionDirOf(root), claudeStartedAt(index), index.previous);+ const stats = readRequestStats(sessionId, claudeRequestStatsDirOf(root));+ return { root, index, selection, stats };+}++const claudeState = (sessionId: string, index: ClaudeIndex, selection: Selection, stats: RequestStats | null): PanelState =>+ panelStateOf(sessionId, index, offKeys(index, selection), stats, index.title, !!selection.auto, [...new Set(index.replies.flatMap(askedIn))], index.chain.length > 0, claudeTurnTexts(index.chain));++export function loadClaudePanelState(sessionId: string, options: ClaudePanelOptions = {}): PanelState {+ const { index, selection, stats } = loadClaude(sessionId, options);+ return claudeState(sessionId, index, selection, stats);+}++export function applyClaudeSelection(sessionId: string, change: SelectionChange, options: ClaudePanelOptions = {}): PanelState {+ const { root, index, selection, stats } = loadClaude(sessionId, options);+ const next = changedSelection(sessionId, index, selection, change);+ writeSelection(next, claudeSelectionDirOf(root));+ return claudeState(sessionId, index, next, stats);+}++export function claudeImageFor(sessionId: string, id: string, maxSide: number, options: ClaudePanelOptions = {}): { id: string; dataUrl: string | null } {+ const index = loadClaudeIndex(sessionId, options.home ?? claudeHome(), stableIdsSince(claudeSelectionDirOf(options.dataRoot ?? dataDir())));+ const image = index.images.find((candidate) => candidate.id === id);+ if (!image) throw new Error(`${id} is not an image of this session`);+ return { id, dataUrl: imageUrl(image, claudeImageData(index, image), maxSide) };+}diff --git a/plugin/src/adapters/codex/panel.ts b/plugin/src/adapters/codex/panel.tsnew file mode 100644index 0000000..011bc1a--- /dev/null+++ b/plugin/src/adapters/codex/panel.ts@@ -0,0 +1,115 @@+// Purpose: the panel's data for a Codex task (v0.11: split from panel/panel-state.ts, docs/v0.11/plan.md): the task's+// rollout, selection, statistics, name, subagents and side chats, and the ids the model's latest reply asked for.+// v0.1-9 — the task's name (thread-names.ts). v0.1-13 — whether part of the history could not be found. v0.7 — the+// task's subagents and side chats (children.ts), each with its last request.+// Input: thread id, the sessions directory (rollouts are only read) and the tool's data directory.+// Output: PanelState; selection changes are written to <data dir>/selection/.++import { childrenDirOf, dataDir, requestStatsDirOf, selectionDirOf } from "../../core/paths.ts";+import { readRequestStats, type RequestStats } from "../../core/request-stats.ts";+import { writeSelection } from "../../core/selection.ts";+import { askedIn, changedSelection, imageUrl, joinTurns, offKeys, panelStateOf, piece, sendInfo, type ChildInfo, type PanelState, type SelectionChange } from "../../panel/panel-state.ts";+import { readChildren } from "./children.ts";+import { effectiveSelection } from "./selection.ts";+import { buildIndex, imageData, readThreadHistory, sessionMetaOf, type ThreadIndex } from "./thread-index.ts";+import { threadTitle } from "./thread-names.ts";++type Json = Record<string, any>;+type Record_ = { type: string; payload: Json };+export type PanelOptions = { sessionsDir: string; dataRoot?: string };++// Ids the model asked for ("需要 IMG-003") in its latest reply; a turn still running falls back to the one before.+export function requestedIds(history: Record_[]): string[] {+ let current: string[] = [];+ let previous: string[] = [];+ for (const record of history) {+ if (record.type === "event_msg" && record.payload.type === "task_started") {+ if (current.length) previous = current;+ current = [];+ }+ if (record.type === "response_item" && record.payload.type === "message" && record.payload.role === "assistant") {+ for (const part of record.payload.content ?? []) if (typeof part?.text === "string") current.push(part.text);+ }+ }+ return [...new Set((current.length ? current : previous).flatMap(askedIn))];+}++export function codexTurnTexts(history: Record_[]): Record<string, string> {+ const numbers = new Map<string, number>();+ const texts = new Map<number, string[]>();+ let turn: number | null = null;+ for (const record of history) {+ if (record.type === "event_msg" && record.payload.type === "task_started" && record.payload.turn_id) {+ if (!numbers.has(record.payload.turn_id)) numbers.set(record.payload.turn_id, numbers.size + 1);+ turn = numbers.get(record.payload.turn_id)!;+ }+ if (turn === null || record.type !== "response_item") continue;+ const item = record.payload;+ if (item.type === "message" && (item.role === "user" || item.role === "assistant") && Array.isArray(item.content)) for (const part of item.content) piece(texts, turn, part?.text);+ else if (item.type === "function_call") piece(texts, turn, item.arguments);+ else if (item.type === "custom_tool_call") piece(texts, turn, item.input);+ else if (item.type === "image_generation_call") piece(texts, turn, item.revised_prompt);+ }+ return joinTurns(texts);+}++// `unchecked`: the keys whose box is not ticked (v0.3: with automatic selection on, every image not pinned).+export function panelState(threadId: string, history: Record_[], index: ThreadIndex, unchecked: Set<string>, stats: RequestStats | null, title: string | null = null, auto = false): PanelState {+ return panelStateOf(threadId, index, unchecked, stats, title, auto, requestedIds(history), history.length > 0, codexTurnTexts(history));+}++// v0.7: oldest first, as the engine first saw each one.+function childrenOf(taskId: string, options: PanelOptions): ChildInfo[] {+ const root = options.dataRoot ?? dataDir();+ return Object.entries(readChildren(taskId, childrenDirOf(root)))+ .sort(([, a], [, b]) => a.at.localeCompare(b.at))+ .map(([threadId, child]): ChildInfo => {+ const stats = readRequestStats(threadId, requestStatsDirOf(root));+ const spawn = child.kind === "subagent" ? sessionMetaOf(options.sessionsDir, threadId)?.source?.subagent?.thread_spawn : null;+ const images = stats?.lastHttp?.images;+ return { threadId, kind: child.kind, name: spawn?.agent_nickname ?? spawn?.agent_path?.split("/").pop() ?? null, startedAt: child.at, images: typeof images === "number" ? images : null, last: sendInfo(stats).last };+ });+}++// A thread with no rollout yet (new, or the panel opened outside a thread) simply has no images. An earlier part of+// the history that is gone for good is left out and reported.+function historyOf(threadId: string, sessionsDir: string): { history: Record_[]; missing: string[] } {+ const missing: string[] = [];+ try {+ return { history: readThreadHistory(sessionsDir, threadId, missing), missing };+ } catch (error) {+ if (error instanceof Error && error.message.startsWith("no rollout found")) return { history: [], missing };+ throw error;+ }+}++function load(threadId: string, options: PanelOptions) {+ const root = options.dataRoot ?? dataDir();+ const { history, missing } = historyOf(threadId, options.sessionsDir);+ const index = buildIndex(threadId, history);+ const selection = effectiveSelection(threadId, options.sessionsDir, selectionDirOf(root));+ const stats = readRequestStats(threadId, requestStatsDirOf(root));+ const title = threadTitle(options.sessionsDir, threadId, history);+ return { root, history, index, selection, stats, title, historyMissing: missing.length > 0 };+}++export function loadPanelState(threadId: string, options: PanelOptions): PanelState {+ const { history, index, selection, stats, title, historyMissing } = load(threadId, options);+ return { ...panelState(threadId, history, index, offKeys(index, selection), stats, title, !!selection.auto), historyMissing, children: childrenOf(threadId, options) };+}++export function applySelection(threadId: string, change: SelectionChange, options: PanelOptions): PanelState {+ const { root, history, index, selection, stats, title, historyMissing } = load(threadId, options);+ const next = changedSelection(threadId, index, selection, change);+ writeSelection(next, selectionDirOf(root));+ return { ...panelState(threadId, history, index, offKeys(index, next), stats, title, !!next.auto), historyMissing, children: childrenOf(threadId, options) };+}++export function imageFor(threadId: string, id: string, maxSide: number, options: PanelOptions): { id: string; dataUrl: string | null } {+ const { history } = historyOf(threadId, options.sessionsDir);+ const index = buildIndex(threadId, history);+ const image = index.images.find((candidate) => candidate.id === id);+ if (!image) throw new Error(`${id} is not an image of this thread`);+ const items = history.filter((record) => record.type === "response_item").map((record) => record.payload);+ return { id, dataUrl: imageUrl(image, imageData(items, image), maxSide) };+}diff --git a/plugin/src/cam.ts b/plugin/src/cam.tsindex 1a28163..aa4a169 100644--- a/plugin/src/cam.ts+++ b/plugin/src/cam.ts@@ -24,7 +24,8 @@ import { codexUiLocale, currentLang, storedLang, systemLang } from "./language.timport { say, type MessageKey } from "./messages.ts";import { migrateDataOnce } from "./core/migrate-data.ts";import { MARKETPLACE, PLUGIN, PLUGIN_ID, pluginStatus } from "./adapters/codex/install.ts";-import { applySelection, loadPanelState, type PanelState } from "./panel/panel-state.ts";+import { applySelection, loadPanelState } from "./adapters/codex/panel.ts";+import type { PanelState } from "./panel/panel-state.ts";import { dataDir, selectionDirOf } from "./core/paths.ts";import { preservingPluginCache } from "./adapters/codex/plugin-cache.ts";import { backupConfig, connectDirectly, planEngine, readConfig, readEnv, useEngine } from "./adapters/codex/setup.ts";diff --git a/plugin/src/core/session.ts b/plugin/src/core/session.tsindex 6155d56..528339a 100644--- a/plugin/src/core/session.ts+++ b/plugin/src/core/session.ts@@ -14,6 +14,8 @@ type Json = Record<string, any>;export type IndexedImage = ImageDigest & {key: string; kind: ImageKind; name: string | null; label: string | null; source: ImageSource; replaceable: boolean;id: string; turn: number | null; bytes: number; pixelSha256: string | null; compacted?: boolean;+ // v0.2: a PDF comment screenshot's page and file (Codex desktop), for the panel.+ pdfPage?: number | null; pdfName?: string | null;};// v0.3: an image the model fetched with our tool, a copy of `of` sent in its own turn only (spec: automatic selection).export type FetchedCopy = { key: string; of: string; turn: number | null };diff --git a/plugin/src/panel/panel-state.ts b/plugin/src/panel/panel-state.tsindex 023a8d5..e3d0ce8 100644--- a/plugin/src/panel/panel-state.ts+++ b/plugin/src/panel/panel-state.ts@@ -10,24 +10,17 @@// automatic selection; the task's switch for them is gone.// v0.8 — what the panel searches and groups by, and never shows (docs/v0.8/plan.md): each image's scene (its feature// line's first part) and the feature line and ratings the model noted; and per turn, what was said in 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/.+// v0.11: platform-free (docs/v0.11/plan.md): what the panel shows is computed here from the core's index, selection+// and statistics; reading a platform's records is its adapter's (adapters/<platform>/panel.ts).+// Input: a session's index, selection and statistics. Output: PanelState as plain JSON, selection changes.-import { readChildren } from "../adapters/codex/children.ts";-import { claudeHome, claudeImageData, claudeStartedAt, isPrompt, loadClaudeIndex, type ClaudeIndex, type ClaudeRecord } from "../adapters/claude/claude-index.ts";import type { ImageSource } from "../core/images.ts";import { sceneOf, withoutRecords } from "../core/notes.ts";-import { childrenDirOf, claudeRequestStatsDirOf, claudeSelectionDirOf, dataDir, requestStatsDirOf, selectionDirOf } from "../core/paths.ts";-import { readRequestStats, type RequestStats } from "../core/request-stats.ts";-import { claudeSelection } from "../adapters/claude/selection.ts";-import { effectiveSelection } from "../adapters/codex/selection.ts";-import { stableIdsSince, writeSelection, type Mark, type Selection } from "../core/selection.ts";-import { buildIndex, imageData, readThreadHistory, sessionMetaOf, type IndexedImage, type ThreadIndex } from "../adapters/codex/thread-index.ts";-import { threadTitle } from "../adapters/codex/thread-names.ts";+import type { RequestStats } from "../core/request-stats.ts";+import type { Mark, Selection } from "../core/selection.ts";+import type { IndexedImage, SessionIndex } from "../core/session.ts";import { pngThumbnail } from "../core/thumbnail.ts";-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;// v0.2 — what the panel filters by; a PDF comment screenshot also says its page and the PDF's name when known.@@ -106,7 +99,6 @@ export function sendInfo(stats: RequestStats | null): SendInfo {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. The English// wording (v0.1-14) asks for "need IMG-004", which the model may quote.@@ -135,23 +127,7 @@ export function askedIn(text: string): string[] {return [...ids];}-// Ids the model asked for ("需要 IMG-003") in its latest reply; a turn still running falls back to the one before.-export function requestedIds(history: Record_[]): string[] {- let current: string[] = [];- let previous: string[] = [];- for (const record of history) {- if (record.type === "event_msg" && record.payload.type === "task_started") {- if (current.length) previous = current;- current = [];- }- if (record.type === "response_item" && record.payload.type === "message" && record.payload.role === "assistant") {- for (const part of record.payload.content ?? []) if (typeof part?.text === "string") current.push(part.text);- }- }- return [...new Set((current.length ? current : previous).flatMap(askedIn))];-}--export function sameContent(index: ThreadIndex, image: IndexedImage): string[] {+export function sameContent(index: SessionIndex, image: IndexedImage): string[] {return index.images.filter((other) => other !== image && (other.contentId === image.contentId || (image.pixelSha256 !== null && other.pixelSha256 === image.pixelSha256))).map((other) => other.id);@@ -162,57 +138,18 @@ export function sameContent(index: ThreadIndex, image: IndexedImage): string[] {// so a long task does not make every read of the panel large. The turns are numbered as in the index.const PIECE = 200;const TURN_TEXT = 600;-const joinTurns = (texts: Map<number, string[]>): Record<string, string> =>+export const joinTurns = (texts: Map<number, string[]>): Record<string, string> =>Object.fromEntries([...texts].map(([turn, pieces]) => [String(turn), pieces.join(" ").slice(0, TURN_TEXT)]));-const piece = (texts: Map<number, string[]>, turn: number, text: unknown) => {+export const piece = (texts: Map<number, string[]>, turn: number, text: unknown) => {// Text the client adds (instructions, the environment, an image's tag) starts with a tag or the AGENTS.md heading.if (typeof text !== "string" || /^\s*(?:<|# AGENTS\.md)/.test(text)) return;const said = withoutRecords(text).replace(/\s+/g, " ").trim().slice(0, PIECE);if (said) texts.set(turn, [...(texts.get(turn) ?? []), said]);};-export function codexTurnTexts(history: Record_[]): Record<string, string> {- const numbers = new Map<string, number>();- const texts = new Map<number, string[]>();- let turn: number | null = null;- for (const record of history) {- if (record.type === "event_msg" && record.payload.type === "task_started" && record.payload.turn_id) {- if (!numbers.has(record.payload.turn_id)) numbers.set(record.payload.turn_id, numbers.size + 1);- turn = numbers.get(record.payload.turn_id)!;- }- if (turn === null || record.type !== "response_item") continue;- const item = record.payload;- if (item.type === "message" && (item.role === "user" || item.role === "assistant") && Array.isArray(item.content)) for (const part of item.content) piece(texts, turn, part?.text);- else if (item.type === "function_call") piece(texts, turn, item.arguments);- else if (item.type === "custom_tool_call") piece(texts, turn, item.input);- else if (item.type === "image_generation_call") piece(texts, turn, item.revised_prompt);- }- return joinTurns(texts);-}-export function claudeTurnTexts(chain: ClaudeRecord[]): Record<string, string> {- const texts = new Map<number, string[]>();- let turn = 0;- for (const record of chain) {- if (record.type === "user" && isPrompt(record)) turn++;- const content = record.message?.content;- if (!turn || (record.type !== "user" && record.type !== "assistant") || record.isMeta || record.isCompactSummary) continue;- if (typeof content === "string") { piece(texts, turn, content); continue; }- if (!Array.isArray(content)) continue;- for (const block of content) {- if (block?.type === "text") piece(texts, turn, block.text);- else if (block?.type === "tool_use" && record.type === "assistant") piece(texts, turn, JSON.stringify(block.input ?? {}));- }- }- return joinTurns(texts);-}--// `unchecked`: the keys whose box is not ticked (v0.3: with automatic selection on, every image not pinned).-export function panelState(threadId: string, history: Record_[], index: ThreadIndex, unchecked: Set<string>, stats: RequestStats | null, title: string | null = null, auto = false): PanelState {- return panelStateOf(threadId, index, unchecked, stats, title, auto, requestedIds(history), history.length > 0, codexTurnTexts(history));-}// v0.4: the same for any index: Codex's (from a rollout) or Claude Code's (claude-index.ts), with the ids the model's// latest reply asked for and whether the conversation has started.-export function panelStateOf(threadId: string, index: ThreadIndex, unchecked: Set<string>, stats: RequestStats | null, title: string | null, auto: boolean, requested: string[], started: boolean, turnTexts: Record<string, string> = {}): PanelState {+export function panelStateOf(threadId: string, index: SessionIndex, unchecked: Set<string>, stats: RequestStats | null, title: string | null, auto: boolean, requested: string[], started: boolean, turnTexts: Record<string, string> = {}): PanelState {const fetched = [...new Set([...index.copies.values()].filter((copy) => copy.turn !== null && copy.turn === index.turns).map((copy) => copy.of))];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.@@ -253,66 +190,20 @@ export function panelStateOf(threadId: string, index: ThreadIndex, unchecked: Se};}-// v0.7: oldest first, as the engine first saw each one.-function childrenOf(taskId: string, options: PanelOptions): ChildInfo[] {- const root = options.dataRoot ?? dataDir();- return Object.entries(readChildren(taskId, childrenDirOf(root)))- .sort(([, a], [, b]) => a.at.localeCompare(b.at))- .map(([threadId, child]): ChildInfo => {- const stats = readRequestStats(threadId, requestStatsDirOf(root));- const spawn = child.kind === "subagent" ? sessionMetaOf(options.sessionsDir, threadId)?.source?.subagent?.thread_spawn : null;- const images = stats?.lastHttp?.images;- return { threadId, kind: child.kind, name: spawn?.agent_nickname ?? spawn?.agent_path?.split("/").pop() ?? null, startedAt: child.at, images: typeof images === "number" ? images : null, last: sendInfo(stats).last };- });-}--// A thread with no rollout yet (new, or the panel opened outside a thread) simply has no images. An earlier part of-// the history that is gone for good is left out and reported.-function historyOf(threadId: string, sessionsDir: string): { history: Record_[]; missing: string[] } {- const missing: string[] = [];- try {- return { history: readThreadHistory(sessionsDir, threadId, missing), missing };- } catch (error) {- if (error instanceof Error && error.message.startsWith("no rollout found")) return { history: [], missing };- throw error;- }-}--function load(threadId: string, options: PanelOptions) {- const root = options.dataRoot ?? dataDir();- const { history, missing } = historyOf(threadId, options.sessionsDir);- const index = buildIndex(threadId, history);- const selection = effectiveSelection(threadId, options.sessionsDir, selectionDirOf(root));- const stats = readRequestStats(threadId, requestStatsDirOf(root));- const title = threadTitle(options.sessionsDir, threadId, history);- return { root, history, index, selection, stats, title, historyMissing: missing.length > 0 };-}-// The keys whose box is not ticked: the unchecked ones; v0.3: with automatic selection on, every image that can be left// out and is not pinned.-function offKeys(index: ThreadIndex, selection: Selection): Set<string> {+export function offKeys(index: SessionIndex, selection: Selection): Set<string> {if (!selection.auto) return new Set(Object.keys(selection.unchecked));const pinned = selection.pinned ?? {};return new Set(index.images.filter((image) => image.replaceable && !pinned[image.key]).map((image) => image.key));}-export function loadPanelState(threadId: string, options: PanelOptions): PanelState {- const { history, index, selection, stats, title, historyMissing } = load(threadId, options);- return { ...panelState(threadId, history, index, offKeys(index, selection), stats, title, !!selection.auto), historyMissing, children: childrenOf(threadId, options) };-}-// v0.3: `auto` switches automatic selection; with `mode: "auto"` a tick pins an image instead of sending it, each kept// in its own record, so switching back finds the manual checks as they were.export type SelectionChange = { uncheck?: string[]; check?: string[]; checkAll?: boolean; auto?: boolean; mode?: "manual" | "auto" };-export function applySelection(threadId: string, change: SelectionChange, options: PanelOptions): PanelState {- const { root, history, index, selection, stats, title, historyMissing } = load(threadId, options);- const next = changedSelection(threadId, index, selection, change);- writeSelection(next, selectionDirOf(root));- return { ...panelState(threadId, history, index, offKeys(index, next), stats, title, !!next.auto), historyMissing, children: childrenOf(threadId, options) };-}// The selection after a change; nothing is written.-export function changedSelection(threadId: string, index: ThreadIndex, selection: Selection, change: SelectionChange): Selection {+export function changedSelection(threadId: string, index: SessionIndex, selection: Selection, change: SelectionChange): Selection {const now = new Date().toISOString();let next: Selection = { ...selection, threadId };// Switched on for the first time, the thread is marked: from then on its history may hold copies the model fetched.@@ -338,55 +229,11 @@ export function changedSelection(threadId: string, index: ThreadIndex, selection}// One image for the panel: a PNG larger than maxSide is scaled down; smaller PNGs and other formats (which the-// browser scales itself) are passed through as they are.-export function imageFor(threadId: string, id: string, maxSide: number, options: PanelOptions): { id: string; dataUrl: string | null } {- const { history } = historyOf(threadId, options.sessionsDir);- const index = buildIndex(threadId, history);- const image = index.images.find((candidate) => candidate.id === id);- if (!image) throw new Error(`${id} is not an image of this thread`);- const items = history.filter((record) => record.type === "response_item").map((record) => record.payload);- const data = imageData(items, image);- if (!data || !image.mime?.startsWith("image/")) return { id, dataUrl: null };- const original = `data:${image.mime};base64,${data}`;- const large = image.mime === "image/png" && Math.max(image.width ?? 0, image.height ?? 0) > maxSide;- // A PNG our decoder cannot read (e.g. interlaced) is still shown, just not scaled down first.- return { id, dataUrl: large ? pngThumbnail(data, maxSide) ?? original : original };-}--// v0.4: the same for a Claude Code session, from its transcript (claude-index.ts) and the selection and statistics-// kept for Claude sessions (paths.ts).-export type ClaudePanelOptions = { dataRoot?: string; home?: string };--function loadClaude(sessionId: string, options: ClaudePanelOptions) {- const root = options.dataRoot ?? dataDir();- const index = loadClaudeIndex(sessionId, options.home ?? claudeHome(), stableIdsSince(claudeSelectionDirOf(root)));- const selection = claudeSelection(sessionId, claudeSelectionDirOf(root), claudeStartedAt(index), index.previous);- const stats = readRequestStats(sessionId, claudeRequestStatsDirOf(root));- return { root, index, selection, stats };-}--const claudeState = (sessionId: string, index: ClaudeIndex, selection: Selection, stats: RequestStats | null): PanelState =>- panelStateOf(sessionId, index, offKeys(index, selection), stats, index.title, !!selection.auto, [...new Set(index.replies.flatMap(askedIn))], index.chain.length > 0, claudeTurnTexts(index.chain));--export function loadClaudePanelState(sessionId: string, options: ClaudePanelOptions = {}): PanelState {- const { index, selection, stats } = loadClaude(sessionId, options);- return claudeState(sessionId, index, selection, stats);-}--export function applyClaudeSelection(sessionId: string, change: SelectionChange, options: ClaudePanelOptions = {}): PanelState {- const { root, index, selection, stats } = loadClaude(sessionId, options);- const next = changedSelection(sessionId, index, selection, change);- writeSelection(next, claudeSelectionDirOf(root));- return claudeState(sessionId, index, next, stats);-}--export function claudeImageFor(sessionId: string, id: string, maxSide: number, options: ClaudePanelOptions = {}): { id: string; dataUrl: string | null } {- const index = loadClaudeIndex(sessionId, options.home ?? claudeHome(), stableIdsSince(claudeSelectionDirOf(options.dataRoot ?? dataDir())));- const image = index.images.find((candidate) => candidate.id === id);- if (!image) throw new Error(`${id} is not an image of this session`);- const data = claudeImageData(index, image);- if (!data || !image.mime?.startsWith("image/")) return { id, dataUrl: null };+// browser scales itself) are passed through as they are. A PNG our decoder cannot read (e.g. interlaced) is still+// shown, just not scaled down first.+export function imageUrl(image: Pick<IndexedImage, "mime" | "width" | "height">, data: string | null, maxSide: number): string | null {+ if (!data || !image.mime?.startsWith("image/")) return null;const original = `data:${image.mime};base64,${data}`;const large = image.mime === "image/png" && Math.max(image.width ?? 0, image.height ?? 0) > maxSide;- return { id, dataUrl: large ? pngThumbnail(data, maxSide) ?? original : original };+ return large ? pngThumbnail(data, maxSide) ?? original : original;}diff --git a/plugin/src/plugin-server.ts b/plugin/src/plugin-server.tsindex 9186116..5e0437c 100644--- a/plugin/src/plugin-server.ts+++ b/plugin/src/plugin-server.ts@@ -35,7 +35,9 @@ import { currentLang, langOf, rememberLang, type Lang } from "./language.ts";import { say } from "./messages.ts";import { migrateDataOnce } from "./core/migrate-data.ts";import { panelHtml } from "./panel/page.ts";-import { applySelection, claudeImageFor, imageFor, loadPanelState, type ClaudePanelOptions, type PanelState } from "./panel/panel-state.ts";+import { applySelection, imageFor, loadPanelState } from "./adapters/codex/panel.ts";+import { claudeImageFor, type ClaudePanelOptions } from "./adapters/claude/panel.ts";+import type { PanelState } from "./panel/panel-state.ts";import { claudeSelectionDirOf, dataDir, pixelCacheDirOf, selectionDirOf } from "./core/paths.ts";import { fetchedLabel, fetchWords } from "./core/words.ts";import { claudeSelection } from "./adapters/claude/selection.ts";diff --git a/spike/src/layers.test.ts b/spike/src/layers.test.tsindex 28e767a..8d047d1 100644--- a/spike/src/layers.test.ts+++ b/spike/src/layers.test.ts@@ -19,12 +19,6 @@ const PENDING = new Set<string>(["adapters/codex/engine.ts -> language.ts","adapters/codex/install.ts -> language.ts","adapters/codex/install.ts -> messages.ts",- "panel/panel-state.ts -> adapters/claude/claude-index.ts",- "panel/panel-state.ts -> adapters/claude/selection.ts",- "panel/panel-state.ts -> adapters/codex/children.ts",- "panel/panel-state.ts -> adapters/codex/selection.ts",- "panel/panel-state.ts -> adapters/codex/thread-index.ts",- "panel/panel-state.ts -> adapters/codex/thread-names.ts",]);type Layer = "core" | "panel" | "protocols" | `adapter:${string}` | "top";diff --git a/spike/src/notes.test.ts b/spike/src/notes.test.tsindex dd64287..741905d 100644--- a/spike/src/notes.test.ts+++ b/spike/src/notes.test.ts@@ -14,7 +14,9 @@ import { addNotes, notesBy, sceneOf, type Notes } from "../../plugin/src/core/noimport { autoPlaceholder, clip, featureNote, plainPlaceholder, type Described } from "../../plugin/src/core/words.ts";import { rewriteItems } from "../../plugin/src/adapters/protocols/openai-responses/rewrite.ts";import { buildIndex, writesNotes, type Parsed } from "../../plugin/src/adapters/codex/thread-index.ts";-import { claudeTurnTexts, loadPanelState, panelStateOf } from "../../plugin/src/panel/panel-state.ts";+import { claudeTurnTexts } from "../../plugin/src/adapters/claude/panel.ts";+import { loadPanelState } from "../../plugin/src/adapters/codex/panel.ts";+import { panelStateOf } from "../../plugin/src/panel/panel-state.ts";import { image, pngA, pngB, text, transcript } from "./claude-fixtures.ts";import { assistant, line, sampleSessions, THREAD, turn } from "./testfixtures.ts";import { png } from "./testkit.ts";diff --git a/spike/src/panel-state.test.ts b/spike/src/panel-state.test.tsindex cbfbd90..1e537ee 100644--- a/spike/src/panel-state.test.ts+++ b/spike/src/panel-state.test.ts@@ -8,7 +8,8 @@ import { readFileSync, writeFileSync } from "node:fs";import { dirname, join } from "node:path";import test from "node:test";import { decodePng } from "../../plugin/src/core/png.ts";-import { applySelection, imageFor, loadPanelState, panelStateOf, requestedIds, sendInfo } from "../../plugin/src/panel/panel-state.ts";+import { applySelection, imageFor, loadPanelState, requestedIds } from "../../plugin/src/adapters/codex/panel.ts";+import { panelStateOf, sendInfo } from "../../plugin/src/panel/panel-state.ts";import { childrenDirOf, requestStatsDirOf } from "../../plugin/src/core/paths.ts";import { recordChild } from "../../plugin/src/adapters/codex/children.ts";import { recordRequest } from "../../plugin/src/core/request-stats.ts";diff --git a/spike/src/v07-children-selftest.ts b/spike/src/v07-children-selftest.tsindex 9e0c5e8..a10ddf9 100644--- a/spike/src/v07-children-selftest.ts+++ b/spike/src/v07-children-selftest.ts@@ -22,7 +22,7 @@ import { AppServer, findBundledCodex } from "./appserver.ts";import { codexHome } from "../../plugin/src/adapters/codex/codexconfig.ts";import { engineHealth } from "../../plugin/src/engine.ts";import { languageFileOf, rememberLang, type Lang } from "../../plugin/src/language.ts";-import { loadPanelState } from "../../plugin/src/panel/panel-state.ts";+import { loadPanelState } from "../../plugin/src/adapters/codex/panel.ts";import { requestStatsDirOf } from "../../plugin/src/core/paths.ts";import { readRequestStats } from "../../plugin/src/core/request-stats.ts";import { loadThreadIndex, threadsStartedSince } from "../../plugin/src/adapters/codex/thread-index.ts";