Claude's answers, rendered: Markdown, math, charts and diagrams drawn in the Claude Code terminal. Independent project, not affiliated with Anthropic.
TypeScript claude-code-mod
A preview of pasted images above the prompt box
razorback16bfeb0f7adfc7
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 0121e1d..2923354 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -6,6 +6,7 @@ All releases of Intelligent UI for Claude Code (`cc-intelligent-ui`). Claude Cod### New+- **A preview of pasted images above the prompt box.** When you paste an image (`[Image #N]`), a small preview of it shows above the prompt box before you send the prompt, with its number (`#N`) at its left. Typst makes a copy of the image that fills the cells of the preview. The previews show side by side, approximately 5 rows tall. When you delete the `[Image #N]` text, or send or clear the prompt, the preview goes away. Refer to [Preview of pasted images](docs/usage.md#preview-of-pasted-images).- **herdr support** (the terminal multiplexer). In a herdr pane, Claude Code needs `CLAUDE_CODE_FORCE_TERMINAL_IMAGES=1` to show pictures. The mod asks one time to add the shell line, as in iTerm2. `/iui setup` also adds it. Then close the pane and open a new herdr pane. herdr must run in a terminal that shows kitty pictures (kitty, Ghostty or iTerm2). Refer to [Terminals](docs/install.md#terminals).- **herdr in iTerm2.** iTerm2 shows each row of a herdr picture from the bottom of the image, so the rows of a page showed in the wrong order. When the herdr client in use is iTerm2, the mod stores the bands of rows of each page and picture in reverse order. Then iTerm2 shows them in the correct order. `/iui details` shows `pages and pictures stored upside down by rows` when this is on. It does not work with `herdr --remote` in iTerm2.- `/iui setup` works in herdr on Linux too.@@ -37,6 +38,7 @@ All releases of Intelligent UI for Claude Code (`cc-intelligent-ui`). Claude Cod- The README has a **Privacy and access** section: the network request, the clipboard reads and writes, the files, the processes and the Typst root.- The shell line of `/iui setup` sets the variable in iTerm2 and in herdr. `/iui setup` replaces the line of an older version. In herdr, the mod asks to update an older line. In iTerm2, the older line still works, and the mod does not ask.- In herdr, pages are shorter, so that each page is at most 16 MiB as pixels.+- **Pasted images in the transcript look the same as their preview.** After you send the prompt, the images show under it side by side, approximately 5 rows tall, with no number. They do not use `/iui height` now (sent images and Typst drawings still do). Claude Code's own `[Image #N]` rows stay under them.## 0.3.2 (2026-10-21)diff --git a/README.md b/README.mdindex a082b09..daa2dab 100644--- a/README.md+++ b/README.md@@ -55,7 +55,7 @@ In other terminals, the mod shows the normal text. Refer to [Install](docs/insta- **Rendered Markdown.** Headings, tables, task lists, block quotes and code with syntax colours, in your terminal's own font.- **LaTeX math.** Inline `$…$` and display `$$…$$` math, typeset.- **Live diagrams, charts and plots.** Claude writes Typst with CeTZ, cetz-plot and fletcher. The drawings use your theme's colours and work offline. For a complex drawing, Claude loads the bundled `intelligent-ui` skill, with the full API and a compile check.-- **Images and files.** Images in an answer, images that Claude sends, Markdown files that Claude sends, and images that you paste show in the transcript.+- **Images and files.** Images in an answer, images that Claude sends, Markdown files that Claude sends, and images that you paste show in the transcript. Images that you paste also show above the prompt box before you send. Refer to [Preview of pasted images](docs/usage.md#preview-of-pasted-images).- **Copy text from pictures.** In fullscreen mode, drag over a picture with the mouse, as you do over normal text. The mod highlights the selected text and copies it when you release the button. Refer to [Copy text from a picture](docs/usage.md#copy-text-from-a-picture).- **Links you can click.** The links, file paths and commands of an answer show in rows under its picture. A cmd+click on a link opens it. A click on a command copies it.- **Plans you can read.** In plan mode, the plan shows rendered, with an approve, edit or reject question under it.diff --git a/docs/development.md b/docs/development.mdindex e195710..0cdb05c 100644--- a/docs/development.md+++ b/docs/development.md@@ -15,6 +15,7 @@ sh tests/fonts-sh-test.sh # the copy of a font out of a terminal's programsh tests/bold-sh-test.sh # the fake bold of md.typ (term-bold=0) with the real Typst and Monaco (skips with no Monaco)sh tests/code-sh-test.sh # code blocks of md.typ (width, header, copy button mark, row grid) with the real Typst (skips with no typst)sh tests/root-sh-test.sh # the Typst root of a compile (STAGE_SH): answer code reads no other file, with the real Typst (its compiles skip with no typst)+sh tests/fill-sh-test.sh # the root of a pasted image's preview (FILL_SH): only fill.typ and the pasted file, with the real Typst (its compiles skip with no typst)sh tests/check-sh-test.sh # the skill's check.sh: a private cache folder, and a block reads no other file (skips with no Typst 0.15)sh tests/footprint-sh-test.sh # release gate: the validator's footprint is whole (no [+N chars] mark, calls line of 850 characters at most)sh tests/gap-sh-test.sh # the gap at a page break of md.typ (seams), on and off the row grid, with the real Typst (skips with no typst)@@ -41,7 +42,7 @@ Picture select writes trace lines that start with `select:`:## Module layout-The hooks module is `hooks/register.tsx`. It connects the hooks to the modules in `hooks/lib/`. Each module has one topic, for example `markdown.ts` (blocks and segments), `mode.ts` (light or dark), `typst.ts` (compile), `fonts.ts` (the lean font set: the fonts that a compile of plain text loads), `draw.tsx` (the pages of an answer and the link rows under them), `links.ts` (the links, file paths and commands of a text), `answer.ts` (one text through the compile and cache steps), `plan.tsx` (the plan approval question and the plan rows), `frame.ts` (the terminal size and the watcher process, which also reports the clipboard changes), `clipboard.ts` (how the clipboard is read and watched for the `/iui grab` toast: macOS, or the Linux tools), `copy.ts` (the text under a mouse selection of pictures), `select.ts` and `blit.ts` (picture select and its highlight), `frames.ts` (the frames of a page: the cell edges, the bands, the row pieces and the frame PNG), `pagecache.ts` (the background page cache: the build queue in slices, and the words probe), `png.ts` (PNG decode, also in steps, and the deflate pieces, in plain JavaScript), `flip.ts` (herdr in iTerm2: the pages and pictures stored with the bands of their rows moved), `probe.ts` (the drawn pages, and the words of a page that a copy reads, kept in memory) and `updates.ts` (the daily check for a new version). The values that change are in one object, `S`, in `state.ts`. The hook bodies in `register.tsx` call `$` themselves; every other `$` call is a wrapper with a short name in `api.ts` (for example `cat` for `$.fs.read`, `exec` for `$.process.run`). The `calls:` note of `claude plugin validate` names each method with the wrappers that call it (`via cat`), and a method that a hook body calls with none. The validator cuts a note at about 1,000 characters and marks the cut `[+N chars]`; the catalogue scan then cannot read the footprint. `tests/footprint-sh-test.sh` is the release gate for this: it fails on a `[+N chars]` mark in the text or the `--json` report, on a calls line (the text after `calls: `) of more than 850 characters, and on a `$` call in `hooks/` outside `api.ts` and `register.tsx`. The tests import from `hooks/register.tsx`, which exports the names that they use.+The hooks module is `hooks/register.tsx`. It connects the hooks to the modules in `hooks/lib/`. Each module has one topic, for example `markdown.ts` (blocks and segments), `mode.ts` (light or dark), `typst.ts` (compile), `fonts.ts` (the lean font set: the fonts that a compile of plain text loads), `draw.tsx` (the pages of an answer and the link rows under them), `links.ts` (the links, file paths and commands of a text), `answer.ts` (one text through the compile and cache steps), `plan.tsx` (the plan approval question and the plan rows), `composer.tsx` (the previews of the images pasted in the draft, above the prompt box), `frame.ts` (the terminal size and the watcher process, which also reports the clipboard changes), `clipboard.ts` (how the clipboard is read and watched for the `/iui grab` toast: macOS, or the Linux tools), `copy.ts` (the text under a mouse selection of pictures), `select.ts` and `blit.ts` (picture select and its highlight), `frames.ts` (the frames of a page: the cell edges, the bands, the row pieces and the frame PNG), `pagecache.ts` (the background page cache: the build queue in slices, and the words probe), `png.ts` (PNG decode, also in steps, and the deflate pieces, in plain JavaScript), `flip.ts` (herdr in iTerm2: the pages and pictures stored with the bands of their rows moved), `probe.ts` (the drawn pages, and the words of a page that a copy reads, kept in memory) and `updates.ts` (the daily check for a new version). The values that change are in one object, `S`, in `state.ts`. The hook bodies in `register.tsx` call `$` themselves; every other `$` call is a wrapper with a short name in `api.ts` (for example `cat` for `$.fs.read`, `exec` for `$.process.run`). The `calls:` note of `claude plugin validate` names each method with the wrappers that call it (`via cat`), and a method that a hook body calls with none. The validator cuts a note at about 1,000 characters and marks the cut `[+N chars]`; the catalogue scan then cannot read the footprint. `tests/footprint-sh-test.sh` is the release gate for this: it fails on a `[+N chars]` mark in the text or the `--json` report, on a calls line (the text after `calls: `) of more than 850 characters, and on a `$` call in `hooks/` outside `api.ts` and `register.tsx`. The tests import from `hooks/register.tsx`, which exports the names that they use.The skill for Claude is in `skills/intelligent-ui/`: `SKILL.md` (the contract, the workflow and an index), `references/` (one file for each topic: Typst, fletcher, CeTZ, plots and charts) and `scripts/check.sh` (the compile check, with the real template). `TYPST_PROMPT` in `hooks/lib/settings.ts` tells Claude when to load it.diff --git a/docs/usage.md b/docs/usage.mdindex 58a6a69..3eeaecf 100644--- a/docs/usage.md+++ b/docs/usage.md@@ -29,11 +29,23 @@ Intelligent UI draws Claude's answers as rendered Markdown pictures. Only the dr- Markdown files (`.md` or `.markdown`, 256 KiB or less). The mod draws the file as rendered pictures, the same as an answer. A relative image path in the file starts from the folder of the file.Other files stay as the normal row. JPEG images, WebP images, SVG images, PDF files and Markdown files need Typst.-- Images that you paste into the prompt (`[Image #N]`). The mod draws each image under your prompt.- These pictures are not larger than the image. They are not wider than the transcript. The height setting (`/iui height`) sets the most rows that they can use (Typst drawings use it too): by default, half the screen and not more than 24 rows. The mod draws PNG, JPEG, WebP and SVG images only. Other image types (for example GIF) stay as the normal row. The size of an SVG image is its width and height in CSS px.+ These images are not larger than the image file. They are not wider than the transcript. The height setting (`/iui height`) sets the most rows that they can use (Typst drawings use it too): by default, half the screen and not more than 24 rows. The mod draws PNG, JPEG, WebP and SVG images only. Other image types (for example GIF) stay as the normal row. The size of an SVG image is its width and height in CSS px.+- Images that you paste into the prompt (`[Image #N]`). Before you send the prompt, a small preview of each image shows above the prompt box. After you send it, the mod draws the images under your prompt at the same size: side by side, approximately 5 rows tall, with no number (Claude Code shows the numbers there). The height setting does not apply to them. Claude Code's own `[Image #N]` rows stay under them: a mod cannot change these rows. Refer to [Preview of pasted images](#preview-of-pasted-images).- The plan in plan mode (the ExitPlanMode tool). The mod draws the plan as a picture in the transcript, the same as an answer. Then the mod asks you to approve the plan. Refer to [Plan approval](#plan-approval).+## Preview of pasted images++When you paste an image into the prompt box, the box shows `[Image #N]`. The mod then shows a small preview of the image above the prompt box, before you send the prompt. The number of the image (`#N`) shows in dim text at the left of its preview.++- The preview fills its cells. Typst makes a copy of the image that has the size of the cells, one time for each image and size, in the cache folder. Until the copy is ready, the preview shows the image as it is. Without Typst, or if Typst cannot make the copy, the preview also shows the image as it is.+- The previews show side by side. When they do not fit in one line, they go on to the next line.+- A preview is approximately 5 rows tall, or less when the space above the prompt box is small. The image keeps its shape.+- When you delete the `[Image #N]` text, the preview of that image goes away. When you send or clear the prompt, all the previews go away.+- The mod shows PNG images only. If Claude Code wrote no PNG file for an image, that image has no preview.+- A survey above the prompt box hides the previews until it closes.+- With `/iui off`, or in a terminal that shows no pictures, there are no previews.+## The intelligent-ui skillThe mod has a skill for Claude: `intelligent-ui`. It has the full API of Typst, fletcher, CeTZ and cetz-plot, recipes for many diagram and chart types, and a compile check. Claude loads it by itself before it writes a complex drawing, or after a drawing did not compile. For normal answers and simple drawings, Claude does not load it.@@ -54,7 +66,7 @@ You can also start the skill yourself: type `/cc-intelligent-ui:intelligent-ui`| `/iui width` | Shows the width: the most columns that a picture can use || `/iui width <40–400>` | Sets the most columns that a picture can use (kept for later sessions) || `/iui width full` | Pictures use the full width of the terminal, the same as the text of Claude Code (kept for later sessions). This is the default. A picture is never wider than 255 columns. |-| `/iui height` | Shows the image height: the most rows that an image can use. This applies to images (sent or pasted) and to Typst drawings in answers. |+| `/iui height` | Shows the image height: the most rows that an image can use. This applies to images that Claude sends and to Typst drawings in answers. Images that you paste always show small. || `/iui height half` | An image uses half the terminal height, from 6 to 24 rows (kept for later sessions). This is the default. || `/iui height full` | An image uses the terminal height less 16 rows (kept for later sessions). The 16 rows are for the row of the file, the text around it and the prompt. || `/iui height <6–200>` | Sets the most rows that an image can use (kept for later sessions). An image is never taller than with `full`. |diff --git a/hooks/lib/composer.tsx b/hooks/lib/composer.tsxnew file mode 100644index 0000000..eff234a--- /dev/null+++ b/hooks/lib/composer.tsx@@ -0,0 +1,238 @@+// The images pasted in the prompt box: a small preview of each `[Image #N]`,+// side by side. The draft's are in the band above the prompt (AbovePrompt)+// before it is sent, each after its dim `#N`; a sent prompt's under its row+// (UserMessage), the same size, after the gutter (Claude Code shows the+// numbers there). The picture is a copy that Typst makes to fill+// its cells exactly (render/fill.typ: no empty cell area under it); the plain+// image shows until it is made. The draft's numbers are in the `draft` atom+// (register.tsx, which writes it at each edit that changes them); the+// pictures are the files Claude Code wrote at the paste (pasteDir).+import type { EngineInterface, ImageSource, RenderElement, RenderInput, RenderViewport } from 'claude-code'++import { COMPILE_TIMEOUT_MS, GUTTER, PICTURES_KEPT, PREVIEW_GAP, PREVIEW_RETRY_MS, PREVIEW_ROWS, PREVIEW_TRIES } from './settings'+import { keep, type Cell, type Env } from './state'+import { folderOf, pastedImagePath, pictureCells, type PixelBox } from './images'+import { pasteDir, pictureOf, sourceOf, type Picture } from './pictures'+import { shownFor } from './frame'+import { hashOf } from './draw'+import { FILL_SH, fillCall, logDebug } from './typst'+import { isFlipHere } from './terminal'+import { delay, stat, write, exec, uiResolve } from './api'++export type BandInput = RenderInput<'AbovePrompt', 'terminal'>+export type RowInput = RenderInput<'UserMessage', 'terminal'>+// One preview: the image's number, its source (the filled copy once made)+// and its picture's cells.+export type Preview = { n: number; source: ImageSource; columns: number; rows: number }+type Slot = { columns: number; rows: number }+type Size = { width: number; height: number }++// Whether two lists of numbers are the same, in the same order.+export const sameNumbers = (a: readonly number[], b: readonly number[]) => a.length === b.length && a.every((n, i) => n === b[i])++// The alt text of a preview: the draft's own mark.+export const altOf = (n: number) => `[Image #${n}]`++// A band preview's number, and its column: the number and a space.+export const labelOf = (n: number) => `#${n}`+export const labelColumns = (n: number) => labelOf(n).length + 1++// The pixels of `cells` cells: the box a filled copy fills exactly.+export const cellPixels = (cells: Slot, cell: Cell): PixelBox => ({+ width: Math.max(1, Math.round(cells.columns * cell.width)),+ height: Math.max(1, Math.round(cells.rows * cell.height)),+})++// Where the filled copy of a picture is kept: a key folder of the cache (as+// a convert's, dropped after 2 days), by the file, its stat stamp and the+// box in pixels (one copy, whatever its number).+export const filledPath = (cache: string, p: Picture, box: PixelBox) => `${cache}/${hashOf(['fill', p.file, p.stamp, box.width, box.height])}/image.png`++// The previews' layout in `columns` cells and `maxRows` rows (Infinity under+// a row: no cap): each picture at most PREVIEW_ROWS tall (less when the band+// is short), after its label's `labels[i]` columns (none when not given),+// side by side, PREVIEW_GAP apart, a new line only where one does not fit.+// With more lines than fit in all but one row of the band, the rows go down+// (to 1: then the band scrolls). Null when not one row, or no column, is+// left for a picture.+export function previewLayout(+ sizes: readonly Size[], cell: Cell, columns: number, maxRows: number, labels: readonly number[] = [],+): { boxes: Slot[]; lines: number[][] } | null {+ const most = Math.min(PREVIEW_ROWS, maxRows - 1)+ const label = (i: number) => labels[i] ?? 0+ if (most < 1 || !sizes.length || sizes.some((_, i) => columns - label(i) < 1)) return null+ let laid: { boxes: Slot[]; lines: number[][] } | null = null+ for (let rows = most; rows >= 1; rows--) {+ const boxes = sizes.map((size, i) => pictureCells(size, columns - label(i), rows, cell))+ laid = { boxes, lines: linesOf(boxes.map((b, i) => label(i) + b.columns), columns) }+ const tallest = (line: number[]) => Math.max(...line.map(i => boxes[i]!.rows))+ if (laid.lines.reduce((sum, line) => sum + tallest(line), 0) <= maxRows - 1) break+ }+ return laid+}++// The boxes of `widths` in lines of at most `columns` cells, PREVIEW_GAP+// apart, in order; a box wider than a line has one of its own.+export function linesOf(widths: readonly number[], columns: number): number[][] {+ const lines: number[][] = []+ let used = 0+ widths.forEach((w, i) => {+ const line = lines.at(-1)+ if (line && used + PREVIEW_GAP + w <= columns) {+ line.push(i)+ used += PREVIEW_GAP + w+ } else {+ lines.push([i])+ used = w+ }+ })+ return lines+}++// The previews of the images `numbers`, in lines (previewLayout) of+// `columns` cells and `maxRows` rows, after their numbers when `labelled`,+// else after the gutter: each one's filled copy+// once made, else the plain image (in flip mode none: only the filled copy+// is stored for its rows). A file that is gone or not a PNG is left out;+// `isWhole` is false then (it may not be written yet: previewRetry).+// `isFinal` is false while a copy is made (`redraw` once it is).+export async function previewsOf(+ $: EngineInterface, view: RenderViewport | undefined, ready: Env, numbers: readonly number[], o: { columns: number; maxRows: number; labelled: boolean; redraw: () => void },+): Promise<{ lines: Preview[][]; isWhole: boolean; isFinal: boolean }> {+ const dir = await pasteDir($, ready)+ if (!dir) return { lines: [], isWhole: false, isFinal: true }+ const flip = isFlipHere(ready)+ const found: { n: number; plain: ImageSource | null; p: Picture }[] = []+ for (const n of numbers) {+ const p = await pictureOf($, pastedImagePath(dir, n))+ const plain = p && !flip ? await sourceOf($, ready, p) : null+ if (p && (flip || plain)) found.push({ n, plain, p })+ }+ const isWhole = found.length === numbers.length+ const cell = shownFor(view).cell+ const laid = o.labelled+ ? previewLayout(found.map(f => f.p), cell, o.columns, o.maxRows, found.map(f => labelColumns(f.n)))+ : previewLayout(found.map(f => f.p), cell, o.columns - GUTTER, o.maxRows)+ if (!laid) return { lines: [], isWhole, isFinal: true }+ let isFinal = true+ const previews = new Map<number, Preview>()+ for (const [i, { n, plain, p }] of found.entries()) {+ const box = laid.boxes[i]!+ const made = filledOf($, ready, p, cellPixels(box, cell), o.redraw)+ if (made === undefined) isFinal = false+ const q = made ? await pictureOf($, made) : null+ const source = (q ? await sourceOf($, ready, q, box.rows) : null) ?? plain+ if (source) previews.set(i, { n, source, ...box })+ }+ const lines = laid.lines.map(line => line.flatMap(i => previews.get(i) ?? [])).filter(line => line.length)+ return { lines, isWhole, isFinal }+}++// The filled copies made in this load by key (null: it failed), and the+// ones being made.+const filled = new Map<string, string | null>()+const filling = new Set<string>()++// A picture's filled copy for `box` pixels: its path; undefined while it is+// made (`redraw` once it is: the plain image shows meanwhile); null when it+// failed (not tried again) or with no Typst.+function filledOf($: EngineInterface, ready: Env, p: Picture, box: PixelBox, redraw: () => void): string | null | undefined {+ if (!ready.typst) return null+ const key = `${p.file}@${p.stamp}@${box.width}x${box.height}`+ if (filled.has(key)) return filled.get(key) ?? null+ if (filling.has(key)) return undefined+ filling.add(key)+ void makeFilled($, ready, p, box).catch(() => null).then(png => {+ filling.delete(key)+ keep(filled, key, png, PICTURES_KEPT)+ if (png) redraw()+ })+ return undefined+}++// Makes the filled copy with Typst (a part file moved to image.png once+// whole, as a convert), in a root of its own (FILL_SH: fill.typ and a link+// to the pasted file, removed after). One made in an earlier load is used+// as is. Null when the root can not be made or Typst fails.+async function makeFilled($: EngineInterface, ready: Env, p: Picture, box: PixelBox): Promise<string | null> {+ const out = filledPath(ready.cache, p, box)+ const made = await stat($, out).catch(() => null)+ if (made?.kind === 'file' && made.size) return out+ const dir = folderOf(out)+ // The write makes the key folder; Typst does not.+ await write($, `${dir}/source.txt`, `${p.file}\n`).catch(() => undefined)+ const part = `${dir}/image.part.png`+ const run = `${dir}/r-${Math.random().toString(36).slice(2, 10)}`+ const staged = await exec($, ['sh', '-c', FILL_SH, 'sh', run, ready.root, p.file], { timeoutMs: 10000 }).catch(() => null)+ if (staged?.exitCode !== 0) {+ await logDebug($, ready.cache, `fill ${p.file}: the root ${run} could not be made: ${staged?.stderr ?? 'sh did not run'}`)+ return null+ }+ try {+ const { argv, env } = fillCall({ typst: ready.typst!, run, cache: ready.cache, box, out: part })+ const ran = await exec($, argv, { timeoutMs: COMPILE_TIMEOUT_MS, env })+ if (ran.exitCode !== 0) {+ await logDebug($, ready.cache, `fill ${p.file} failed:\n${ran.stderr}`)+ return null+ }+ } finally {+ void Promise.resolve(exec($, ['rm', '-rf', run], { timeoutMs: 10000 })).catch(() => undefined)+ }+ const moved = await exec($, ['mv', '-f', part, out])+ return moved.exitCode === 0 ? out : null+}++// A draft with an image not found: `redraw` (the band alone) a little later+// (the file may come after the edit), PREVIEW_TRIES times at most per draft.+let retried = { key: '', tries: 0 }+export function previewRetry($: EngineInterface, key: string, redraw: () => void) {+ if (retried.key !== key) retried = { key, tries: 0 }+ if (retried.tries >= PREVIEW_TRIES) return+ retried.tries+++ void delay($, PREVIEW_RETRY_MS, redraw)+}++// The previews in their lines: each after its dim number when `labelled`+// (the first one's in the gutter), else after the gutter (as underRow).+function previewLines($: EngineInterface, e: BandInput | RowInput, lines: readonly Preview[][], labelled: boolean) {+ const { Box, Text, Image } = uiResolve($, e)+ const image = (p: Preview) => <Image key={`preview-${p.n}`} source={p.source} columns={p.columns} rows={p.rows} alt={altOf(p.n)} />+ return (+ <Box flexDirection="column" marginLeft={labelled ? 0 : GUTTER}>+ {lines.map((line, i) => (+ <Box key={`previews-${i}`} flexDirection="row" columnGap={PREVIEW_GAP}>+ {line.map(p => labelled ? (+ <Box key={`preview-${p.n}`} flexDirection="row">+ <Box width={labelColumns(p.n)}>+ <Text dimColor>{labelOf(p.n)}</Text>+ </Box>+ {image(p)}+ </Box>+ ) : image(p))}+ </Box>+ ))}+ </Box>+ )+}++// The previews, then the band beneath (another plugin's or the engine's).+export function drawPreviews($: EngineInterface, e: BandInput, lines: readonly Preview[][], below: RenderElement) {+ const { Box } = uiResolve($, e)+ return (+ <Box flexDirection="column">+ {previewLines($, e, lines, true)}+ {below}+ </Box>+ )+}++// The engine's prompt row, then the previews under it.+export function previewsUnder($: EngineInterface, e: RowInput, row: RenderElement, lines: readonly Preview[][]) {+ const { Box } = uiResolve($, e)+ return (+ <Box flexDirection="column">+ {row}+ {previewLines($, e, lines, false)}+ </Box>+ )+}diff --git a/hooks/lib/pictures.tsx b/hooks/lib/pictures.tsxindex d13e7ba..890f3de 100644--- a/hooks/lib/pictures.tsx+++ b/hooks/lib/pictures.tsx@@ -1,8 +1,9 @@-// Pictures under rows: the files Claude sends (SendUserFile) and the images-// the user pastes are drawn under their row. An image is drawn at its own-// size or smaller (a JPEG, WebP or SVG as the PNG Typst makes of it); a PDF-// as one page, with a control line to turn the pages; a Markdown file as an-// answer is. The pure parts are in images.ts.+// Pictures under rows: the files Claude sends (SendUserFile) are drawn under+// their row. An image is drawn at its own size or smaller (a JPEG, WebP or+// SVG as the PNG Typst makes of it); a PDF as one page, with a control line+// to turn the pages; a Markdown file as an answer is. The images the user+// pastes are in composer.tsx (its folder, pasteDir, is here). The pure parts+// are in images.ts.import type { EngineInterface, FsStat, ImageSource, RenderElement, RenderInput } from 'claude-code'import { COMPILE_TIMEOUT_MS, GUTTER, PICTURES_KEPT, SENT_TEXT_MAX_BYTES, type Height } from './settings'@@ -105,20 +106,6 @@ export async function pictureBytes($: EngineInterface, p: Picture): Promise<strireturn base64}-// The Images for the files that are PNGs, sized to the Frame on screen.-// `at`: as statOf. In flip mode each is first a PNG Typst makes at its box-// (8-bit RGBA, no larger than drawn), which shownOne stores flipped.-export async function shownOf($: EngineInterface, e: RowInput, ready: Env, files: readonly string[], at?: StatAt): Promise<Shown[]> {- const shown: Shown[] = []- const box = isFlipHere(ready) ? boxOf(shownFor(e.viewport), ready.inline, S.shownHeight) : null- for (const file of new Set(files)) {- const png = box ? ((await convertedOf($, ready, file, box, at)) ?? file) : file- const s = png ? await shownOne($, e, ready, png, nameOf(file), at) : null- if (s) shown.push(s)- }- return shown-}-// One PNG's Image, sized to the Frame and the height setting on screen// (shownFor, S.shownHeight: a new size shows when its epoch commits, with// every other picture), and at most as wide as the room now; null when it is not a PNG. `alt` is the file's name@@ -130,17 +117,25 @@ export async function shownOne($: EngineInterface, e: RowInput, ready: Env, fileif (!p) return nullconst frame = shownFor(e.viewport)const fit = fitBox(pictureCells(p, pictureColumns(frame, false), pictureRowsFor(frame.rows, S.shownHeight), frame.cell), roomColumns(e.viewport))+ const source = await sourceOf($, ready, p, fit.rows)+ return source ? { source, ...fit, alt } : null+}++// A PNG's Image source: its bytes over ssh (null when too large), else its+// file name, with a generation by its stat. In flip mode the PNG stored for+// `rows` (flippedOf); null with no rows or when that fails.+export async function sourceOf($: EngineInterface, ready: Env, p: Picture, rows?: number): Promise<ImageSource | null> {if (ready.inline) {const png = await pictureBytes($, p)- return png ? { source: { png }, ...fit, alt } : null+ return png ? { png } : null}if (isFlipHere(ready)) {- const flipped = await flippedOf($, ready, p, fit.rows)- if (!flipped) return null- return { source: { file: flipped, format: 'png', generation: Number.parseInt(hashOf([p.stamp, fit.rows]).slice(0, 8), 16) }, ...fit, alt }+ // Flip mode: only the PNG stored for the rows it is drawn in.+ if (rows === undefined) return null+ const flipped = await flippedOf($, ready, p, rows)+ return flipped ? { file: flipped, format: 'png', generation: Number.parseInt(hashOf([p.stamp, rows]).slice(0, 8), 16) } : null}- const generation = Number.parseInt(hashOf([p.stamp]).slice(0, 8), 16)- return { source: { file, format: 'png', generation }, ...fit, alt }+ return { file: p.file, format: 'png', generation: Number.parseInt(hashOf([p.stamp]).slice(0, 8), 16) }}// Flip mode: a PNG stored for herdr in iTerm2 drawn `rows` rows talldiff --git a/hooks/lib/settings.ts b/hooks/lib/settings.tsindex 5f241a6..691e86a 100644--- a/hooks/lib/settings.ts+++ b/hooks/lib/settings.ts@@ -99,7 +99,7 @@ export const MODES = ['dark', 'light'] as const// terminal's rows.export const PICTURE_MAX_ROWS = 24export const PICTURE_MIN_ROWS = 6-// `/iui height`: the most rows an image (sent or pasted) or a Typst+// `/iui height`: the most rows an image Claude sends or a Typst// drawing (the room of a live block or a local image in Markdown; not a// whole Markdown page) takes. `half`, the default: as above. `full`: the// terminal's rows less PICTURE_ROOM_ROWS. A number: that many rows, at most@@ -123,6 +123,13 @@ export const gridText = (grid: boolean) => (grid ? 'on (each line of text on whoexport const SELECT_SINCE = '2.1.298'export const selectText = (on: boolean) => (on ? 'on (select and copy picture text with the mouse, fullscreen)' : 'off')export const PICTURES_KEPT = 64+// The previews of pasted images (composer.tsx: the band and the rows): about+// this many rows tall, this many columns apart, and a file not there yet+// looked for again after PREVIEW_RETRY_MS, PREVIEW_TRIES times at most.+export const PREVIEW_ROWS = 5+export const PREVIEW_GAP = 2+export const PREVIEW_RETRY_MS = 250+export const PREVIEW_TRIES = 8// The file paths of the link rows under a picture: whether each one is a// file, kept for this many paths (the oldest go).export const LINK_FILES_KEPT = 512diff --git a/hooks/lib/state.ts b/hooks/lib/state.tsindex b7cde2e..36e9abd 100644--- a/hooks/lib/state.ts+++ b/hooks/lib/state.ts@@ -250,6 +250,9 @@ export const S = {updatesLoading: null as Promise<void> | null,updateDone: false,updateBusy: false,+ // The `[Image #N]` numbers last written to the `draft` atom (register.tsx;+ // null: none written in this load).+ draftImages: null as number[] | null,}// The settings an /iui command set in this load (`size`, `enabled`, ...): the// start (setUp) does not read them from the store, which may answer with thediff --git a/hooks/lib/typst.ts b/hooks/lib/typst.tsindex 9daa044..7c6fe8b 100644--- a/hooks/lib/typst.ts+++ b/hooks/lib/typst.ts@@ -210,6 +210,39 @@ export function convertCall(o: {return { argv, env: noNetworkEnv(o.cache) }}+// A pasted image's preview made to fill `box` pixels (the cells it is drawn+// in) exactly (render/fill.typ), in the root `run` that FILL_SH made: the+// pasted file is /image.png there, and Typst reads no other file.+export function fillCall(o: { typst: string; run: string; cache: string; box: { width: number; height: number }; out: string }): {+ argv: string[]+ env: Record<string, string>+} {+ const argv = [+ o.typst, 'compile',+ '--root', o.run,+ '--format', 'png',+ '--diagnostic-format', 'short',+ '--ppi', '72',+ '--input', 'f=/image.png',+ '--input', `maxw=${o.box.width}`,+ '--input', `maxh=${o.box.height}`,+ `${o.run}/fill.typ`,+ o.out,+ ]+ return { argv, env: noNetworkEnv(o.cache) }+}++// The root of a fill: a new private folder ($1) with fill.typ copied from+// the plugin root ($2) (Typst wants its main file inside the root) and a+// link image.png to the pasted file ($3), which must be a .png file, not a link.+export const FILL_SH = [+ 'umask 077',+ 'r=$1 p=$2 f=$3',+ 'case $f in /*.png) ;; *) exit 1 ;; esac',+ '[ -f "$f" ] && [ ! -L "$f" ] || exit 1',+ 'mkdir "$r" && cp "$p/render/fill.typ" "$r/fill.typ" && ln -s "$f" "$r/image.png"',+].join('\n')+export function noNetworkEnv(cache: string): Record<string, string> {return {HTTPS_PROXY: NO_NETWORK,diff --git a/hooks/register.tsx b/hooks/register.tsxindex 95f2441..9baa605 100644--- a/hooks/register.tsx+++ b/hooks/register.tsx@@ -13,11 +13,12 @@// The work is in ./lib, one topic per module: settings, state (the shared// mutable state `S`), markdown, terminal, terminals, mode, themes, command,// setup, host, typst, draw, stream, answer (one text through the pipeline),-// env, images, pictures, plan (the plan approval flow), copy (the text under+// env, images, pictures, composer (the previews of the images pasted in the+// draft), plan (the plan approval flow), copy (the text under// a mouse copy of the pictures), probe (the drawn pages, and the words of a// page a copy reads) and updates (the daily check for a new version). This// file joins them to the hooks, and gives the tests the names they import.-import { atom, read, update } from 'claude-code'+import { atom, memberOf, read, update } from 'claude-code'import type { EngineInterface, Register, RenderElement, RenderSurface, RenderViewport, UiCopyArgs } from 'claude-code'import {@@ -49,8 +50,9 @@ import { clipboardNow, clipboardRead, clipWatchArgs, clipWayReady, hintText } frimport { setUp } from './lib/env'import { frameFor, noteView, stopWatch, watchFrame, watchState } from './lib/frame'import { herdrText, isFlipHere, roomColumns } from './lib/terminal'-import { onScreenOf, pastedImagePath, pastedNumbers, sentFiles } from './lib/images'-import { drawPageField, pasteDir, pdfPagesOf, rowsAfter, sentOf, shownOf, underRow, type Shown, type Under } from './lib/pictures'+import { onScreenOf, pastedNumbers, sentFiles } from './lib/images'+import { drawPageField, pdfPagesOf, rowsAfter, sentOf, underRow, type Under } from './lib/pictures'+import { drawPreviews, previewRetry, previewsOf, previewsUnder, sameNumbers, type Preview } from './lib/composer'import { checkUpdates, loadUpdates, updateLine, updateNotice, type UpdateState } from './lib/updates'import { nowMs, write, surf, save, copy, uiSelection, toast } from './lib/api'import {@@ -61,7 +63,7 @@ import {// The names this module exported before the split (the tests import them).export {BASE_THEME, DEFAULT_CELL, DEFAULT_HEIGHT, DEFAULT_SIZE, DEFAULT_THEME, DEFAULT_WIDTH, gridText, heightText, IMAGE_MAX_COLUMNS, MAX_HEIGHT, MAX_SIZE, MAX_WIDTH,- MIN_HEIGHT, MIN_SIZE, MIN_WIDTH, PICTURE_MAX_ROWS, PICTURE_MIN_ROWS, PICTURE_ROOM_ROWS, SEGMENT_ROWS, TYPST_PROMPT, widthText, type Height, type Width,+ MIN_HEIGHT, MIN_SIZE, MIN_WIDTH, PICTURE_MAX_ROWS, PICTURE_MIN_ROWS, PICTURE_ROOM_ROWS, PREVIEW_RETRY_MS, PREVIEW_ROWS, SEGMENT_ROWS, TYPST_PROMPT, widthText, type Height, type Width,COMMIT_MAX_MS, COMMIT_MIN_MS, DESIGNS_KEPT_DAYS, RESIZE_QUIET_MS, RESULTS_KEPT, TOUCH_FLUSH_MS,} from './lib/settings'export { DEFAULT_METRICS, keep, type Cell, type Design, type FontMetrics, type Frame, type Look, type Metrics, type Room } from './lib/state'@@ -84,7 +86,7 @@ export { commandSuggestions, parseCommand, type Command, type Suggestion } fromexport { hasNewSetup, hasSetup, SETUP_END, SETUP_NOT_MAC, SETUP_START, setupBlock, setupWordsOf, shellRcOf, withoutSetup, withSetup, type ShellRc } from './lib/setup'export { fcPatternOf, HERDR_CLIENT_SH, HERDR_TICK_SH, OSC11_PL, perlFor, PROGRAM_FONTS_PL, PROGRAM_FONTS_SH, WATCH_PL } from './lib/host'export {- convertCall, isSafePage, measureCall, noNetworkEnv, pageRowsOf, parseOd, pngSize, probeCall, resultOf, roomInputs, shouldRetrySafe, typstCall, typstInputs,+ convertCall, fillCall, isSafePage, measureCall, noNetworkEnv, pageRowsOf, parseOd, pngSize, probeCall, resultOf, roomInputs, shouldRetrySafe, typstCall, typstInputs,} from './lib/typst'export {alignFirstLine, fileHref, firstLineOf, fontFit, hashOf, renderKey, resolveLinks, scaledBulletRow, sizing, type FirstLine, type ShownLink,@@ -92,6 +94,7 @@ export {export { nextJob } from './lib/stream'export * from './lib/images'export { rowsAfter, type Under } from './lib/pictures'+export { altOf, cellPixels, filledPath, labelColumns, labelOf, linesOf, previewLayout, sameNumbers } from './lib/composer'export * from './lib/copy'export { targetAt, targetsOf, type PageTarget, type Target } from './lib/targets'export {@@ -111,6 +114,8 @@ export {// The atoms the hooks read and write (a copy of each in this file: see state.ts).const isOn = atom({ plugin: 'cc-intelligent-ui', key: 'isOn' } as const, true)const done = atom({ plugin: 'cc-intelligent-ui', key: 'done' } as const, 0)+const draft = atom({ plugin: 'cc-intelligent-ui', key: 'draft' } as const, [])+const filled = atom({ plugin: 'cc-intelligent-ui', key: 'filled' } as const, 0)// ---- Pictures under rows ----------------------------------------------------@@ -148,6 +153,19 @@ async function answerEnv($: EngineInterface, e: { viewport?: RenderViewport }):return ready?.env.typst ? ready : null}+// The draft's `[Image #N]` numbers to the `draft` atom, only when they+// changed (the band above the prompt draws again): a key that keeps them+// writes nothing. Never throws.+function noteDraft($: EngineInterface, text: string) {+ try {+ const now = pastedNumbers(text)+ if (S.draftImages && sameNumbers(S.draftImages, now)) return+ S.draftImages = now+ // The newest numbers at the write, should two writes cross.+ void update($, draft, () => S.draftImages ?? []).catch(() => undefined)+ } catch {}+}+// ---- Rows drawn before ----------------------------------------------------------// What a drawn row depends on besides its own inputs: `done` (every result@@ -175,7 +193,8 @@ type AnswerMemo = { tree: RenderElement; after: number }const answers = new Map<string, Memo<AnswerMemo>>()const plans = new Map<string, Memo<{ drawing: Drawing; links: ShownLink[] }>>()const unders = new Map<string, Memo<Under[]>>()-const pasted = new Map<string, Memo<Shown[]>>()+const pasted = new Map<string, Memo<Preview[][]>>()+const drafts = new Map<string, Memo<Preview[][]>>()// The kept value when the inputs and the stamp are the same. A new onScreen// is given to the row's jobs that wait or run (their order and whether they@@ -836,16 +855,22 @@ export const register: Register = on => {}).catch(($, e, next) => next(e))// Typing in the main prompt takes a picture selection down (a key that- // edits, not a bare cursor move); the edit passes on as it is.+ // edits, not a bare cursor move); the edit passes on as it is. The box+ // after it gives the draft's pasted images (noteDraft).on('prompt.edit', async ($, e, next) => {- if (picked.now && (e.inputText || e.end > e.start)) clearSelection($)- return next(e)+ const isEdit = !!e.inputText || e.end > e.start+ if (picked.now && isEdit) clearSelection($)+ const box = await next(e)+ if (isEdit && e.origin.kind === 'composer') noteDraft($, box.text)+ return box}).catch(($, e, next) => next(e))// A change of the prompt box no edit carried (a recalled entry, an undo, a- // clear): the same, but not another plugin's fill. Observe only.+ // clear): the same, but not another plugin's fill. Observe only. Any+ // change gives the draft's pasted images (a submit clears them).on('prompt.change', async ($, e, next) => {if (picked.now && e.origin.kind !== 'plugin' && e.origin.kind !== 'app') clearSelection($)+ noteDraft($, e.text)return next(e)}).catch(($, e, next) => next(e))@@ -906,7 +931,7 @@ export const register: Register = on => {await $.store.set('height', S.height)}const head = asked.height === undefined ? 'The most rows an image or a Typst drawing takes' : 'Intelligent UI images and Typst drawings now take at most'- return { text: `${head}: ${heightText(S.height)}. This applies to images (sent or pasted) and to Typst drawings in answers. Use /iui height half (the default), /iui height full, or /iui height <${MIN_HEIGHT}-${MAX_HEIGHT}> (never more than full).` }+ return { text: `${head}: ${heightText(S.height)}. This applies to images that Claude sends and to Typst drawings in answers (images that you paste show small, as their preview above the prompt box). Use /iui height half (the default), /iui height full, or /iui height <${MIN_HEIGHT}-${MAX_HEIGHT}> (never more than full).` }}if (asked.kind === 'grid') {if (asked.grid === undefined) await setUp($).catch(() => null)@@ -1179,36 +1204,77 @@ export const register: Register = on => {return drawPageField($, e)})- // The images the user pasted (`[Image #N]`), under the prompt row. Only the- // user's own prompts: a notification's or another agent's row stays as is.+ // The images the user pasted (`[Image #N]`), under the prompt row, as+ // their preview above the prompt box was (composer.tsx). Only the user's+ // own prompts: a notification's or another agent's row stays as is. The+ // row reads its member of `filled`, which goes up when a filled copy is+ // made: that row alone draws again.on('ui.render', { component: 'UserMessage' }, async ($, e, next) => {if (e.surface !== 'terminal' || e.props.origin?.kind !== 'composer' || e.props.from || e.props.task) return next(e)const numbers = pastedNumbers(e.props.text)if (!numbers.length) return next(e)- let shown: Shown[] = []+ let lines: Preview[][] = []try {const ready = await pictureEnv($, e)+ const mine = memberOf(filled, e)+ if (ready) await read($, mine)const k = { text: e.props.text, extra: '', stamp: ready ? stampOf(ready, e.viewport) : '', onScreen: undefined }const kept = ready ? memoOf(pasted, e.requestId, k, () => false) : undefined- if (kept) shown = kept+ if (kept) lines = keptelse if (ready) {- const dir = await pasteDir($, ready.env)- if (dir) shown = await shownOf($, e, ready.env, numbers.map(n => pastedImagePath(dir, n)), { done: ready.done, row: e.requestId })- if (shown.length) memoSet(pasted, e.requestId, k, shown)+ const redraw = () => void update($, mine, n => n + 1).catch(() => undefined)+ const made = await previewsOf($, e.viewport, ready.env, numbers, { columns: roomColumns(e.viewport), maxRows: Number.POSITIVE_INFINITY, labelled: false, redraw })+ lines = made.lines+ if (lines.length && made.isFinal) memoSet(pasted, e.requestId, k, lines)else pasted.delete(e.requestId)}} catch {- shown = []+ lines = []}const row = await next(e)- if (!shown.length) return row+ if (!lines.length) return rowtry {- return underRow($, e, row, shown)+ return previewsUnder($, e, row, lines)} catch {return row}})+ // The images pasted in the draft (`[Image #N]`), as small previews above+ // the prompt box until it is sent, then the band beneath. A survey, no+ // image, or no picture drawn: the band as it is.+ on('ui.render', { component: 'AbovePrompt' }, async ($, e, next) => {+ if (e.surface !== 'terminal' || e.props.hasSurvey) return next(e)+ let lines: Preview[][] = []+ try {+ const numbers = await read($, draft)+ const ready = numbers.length ? await pictureEnv($, e) : null+ if (ready) {+ const k = { text: numbers.join(','), extra: `${e.props.bodyColumns}x${e.props.maxRows}`, stamp: stampOf(ready, e.viewport), onScreen: undefined }+ const kept = memoOf(drafts, 'draft', k, () => false)+ if (kept) lines = kept+ else {+ // The same numbers written again: only the band reads them.+ const redraw = () => void update($, draft, ns => [...ns]).catch(() => undefined)+ const made = await previewsOf($, e.viewport, ready.env, numbers, { columns: e.props.bodyColumns, maxRows: e.props.maxRows, labelled: true, redraw })+ lines = made.lines+ if (made.isWhole && made.isFinal) memoSet(drafts, 'draft', k, lines)+ else drafts.delete('draft')+ if (!made.isWhole) previewRetry($, k.text, redraw)+ }+ }+ } catch {+ lines = []+ }+ const band = await next(e)+ if (!lines.length) return band+ try {+ return drawPreviews($, e, lines, band)+ } catch {+ return band+ }+ })+// ---- The plan --------------------------------------------------------------// The main loop's running turn, which "clear context" ends.diff --git a/render/fill.typ b/render/fill.typnew file mode 100644index 0000000..6342b19--- /dev/null+++ b/render/fill.typ@@ -0,0 +1,15 @@+// Intelligent UI: the preview of a pasted image (the band above the prompt+// box), made to fill the cells it is drawn in. Compiled at 72 ppi, where 1 pt+// is 1 pixel.+//+// Inputs: f = the PNG's path in the root (/image.png: FILL_SH links it, and+// the root has only it and this file); maxw, maxh = the pixel size of its+// cells. The page is exactly that, and the image covers it (the aspect kept,+// centred, under half a cell cut on one side), so no empty cell area is left+// under or beside it.+#set page(width: auto, height: auto, margin: 0pt)++#let num(key) = calc.max(1, float(sys.inputs.at(key, default: "1")))+#let (bw, bh) = (num("maxw"), num("maxh"))++#box(width: bw * 1pt, height: bh * 1pt, clip: true, image(sys.inputs.at("f", default: ""), width: bw * 1pt, height: bh * 1pt, fit: "cover"))diff --git a/tests/fill-sh-test.sh b/tests/fill-sh-test.shnew file mode 100644index 0000000..665d871--- /dev/null+++ b/tests/fill-sh-test.sh@@ -0,0 +1,82 @@+#!/bin/sh+# Makes the root of a pasted image's preview with FILL_SH (hooks/lib/typst.ts)+# in sh, dash and bash, and compiles render/fill.typ in it with the real+# Typst, as the mod does: the root is private and has only fill.typ and a+# link to the pasted file; a pasted file that is a link or not a file gets no+# root; the copy fills the box; Typst reads no file outside the root.+# Usage: sh tests/fill-sh-test.sh (exit 0 when all pass; the compiles skip with no typst)+set -u+here=$(cd "$(dirname "$0")/.." && pwd -P)+tmp=$(mktemp -d "${TMPDIR:-/tmp}/iui-fill-test.XXXXXX")+tmp=$(cd "$tmp" && pwd -P)+trap 'rm -rf "$tmp"' EXIT+pass=0+fail=0+ok() { pass=$((pass + 1)); }+bad() { fail=$((fail + 1)); echo "FAIL: $*"; }++# The TypeScript array as sh: each '...' line, the escapes undone.+fill_sh=$(perl -e 'local $/; $_ = <STDIN>; /export const FILL_SH = \[\n(.*?)\n\]\.join/s or exit 1;+ print join("\n", map { s/^\s*\x27//; s/\x27,?\s*$//; s/\\\\/\\/g; $_ } split /\n/, $1)' < "$here/hooks/lib/typst.ts")+[ -n "$fill_sh" ] && ok || bad "FILL_SH: not found in typst.ts"++# The pasted image (Claude Code's file), a link to it and a secret text file.+paste=$tmp/paste secret=$tmp/secret.txt+mkdir -p "$paste/dir.png"+echo 'SECRET-WORDS-42' > "$secret"+if command -v typst >/dev/null 2>&1; then+ printf '#set page(width: 64pt, height: 32pt, fill: blue)\n' | typst compile -f png - "$paste/5.png"+else+ printf '\211PNG\r\n\032\n' > "$paste/5.png"+fi+ln -s 5.png "$paste/6.png"++shells=+for s in sh dash bash; do command -v "$s" >/dev/null 2>&1 && shells="$shells $s"; done+n=0+for s in $shells; do+ [ "$s" = bash ] && s='bash --posix'+ n=$((n + 1)); k=$tmp/cache/k$n; r=$k/r-1+ mkdir -p "$k"+ $s -c "$fill_sh" sh "$r" "$here" "$paste/5.png" && ok || bad "$s: FILL_SH failed"+ case $(ls -ld "$r") in drwx------*) ok ;; *) bad "$s: the root is not private ($(ls -ld "$r"))" ;; esac+ [ -f "$r/fill.typ" ] && [ ! -L "$r/fill.typ" ] && cmp -s "$here/render/fill.typ" "$r/fill.typ" && ok || bad "$s: fill.typ is not a copy"+ [ -L "$r/image.png" ] && [ "$(readlink "$r/image.png")" = "$paste/5.png" ] && ok || bad "$s: image.png is not a link to the pasted file"+ [ "$(ls -A "$r" | tr '\n' ' ')" = "fill.typ image.png " ] && ok || bad "$s: the root has more: $(ls -A "$r")"+ # A folder that is there is never used again.+ $s -c "$fill_sh" sh "$r" "$here" "$paste/5.png" 2>/dev/null && bad "$s: an old folder was used again" || ok+ # A link, a folder, no file, not a .png or a relative path: no root.+ for no in "$paste/6.png" "$paste/dir.png" "$paste/missing.png" "$secret" 5.png; do+ $s -c "$fill_sh" sh "$k/r-no" "$here" "$no" 2>/dev/null && bad "$s: a root for $no" || ok+ [ ! -e "$k/r-no" ] && ok || { bad "$s: $k/r-no made for $no"; rm -rf "$k/r-no"; }+ done+done++if ! command -v typst >/dev/null 2>&1; then+ echo "fill-sh-test: $pass passed, $fail failed (compiles skipped: no typst)"+ [ "$fail" = 0 ]; exit+fi++# Compiles fill.typ as the mod does (fillCall), with input f=$2, in root $1.+fill() {+ perl -e 'alarm 20; exec @ARGV' typst compile --root "$1" --format png --diagnostic-format short --ppi 72 \+ --input "f=$2" --input maxw=40 --input maxh=30 "$1/fill.typ" "$tmp/out.png" > "$tmp/err" 2>&1+}+r=$tmp/cache/k1/r-1+fill "$r" /image.png && ok || bad "the pasted image: $(head -n 3 "$tmp/err")"+# The copy is the box in pixels: 40 x 30.+size=$(perl -e 'open(F, "<", $ARGV[0]) or exit 1; binmode F; read(F, my $h, 24); my ($w, $h2) = unpack("NN", substr($h, 16, 8)); print "$w $h2"' "$tmp/out.png")+[ "$size" = "40 30" ] && ok || bad "the copy is $size, not 40 30"+# Any other file: not found, or out of the root.+for f in /etc/hosts "$secret" "$paste/5.png" ../../../../../../../../etc/hosts /fill.typ; do+ if fill "$r" "$f"; then bad "f=$f: compiled"+ elif ! grep -q 'SECRET-WORDS\|localhost' "$tmp/err"; then ok+ else bad "f=$f: $(head -n 3 "$tmp/err")"+ fi+done+# The same input with the root at / (as before) reads the file: the test is real.+typst compile --root / --format png --ppi 72 --input "f=$paste/5.png" --input maxw=40 --input maxh=30 "$here/render/fill.typ" "$tmp/old.png" > /dev/null 2>&1 \+ && ok || bad "with the root at /, the pasted file by its path did not compile (the test shows nothing)"++echo "fill-sh-test: $pass passed, $fail failed"+[ "$fail" = 0 ]diff --git a/tests/images.test.ts b/tests/images.test.tsindex 3beeb42..a81e8b1 100644--- a/tests/images.test.ts+++ b/tests/images.test.ts@@ -4,10 +4,11 @@ import { onScreenOf } from '../hooks/lib/images'import { encodeSteps, flipRows, WRITE_PL } from '../hooks/lib/flip'import { decodePng, fromBase64, toBase64, type Rgba } from '../hooks/lib/png'import { HERDR_CLIENT_SH } from '../hooks/lib/host'+import { FILL_SH } from '../hooks/lib/typst'import {claudeTmpOf, convertBox, convertCall, convertedPath, DEFAULT_CELL, factsAfter, folderOf, imageKindOf, isMarkdownPath, isNoPageError, isPdfPath, lastKnownPage,NO_FACTS, pageControls, pageCountOf, pageOf, parsePage, pasteDirOf, pastedImagePath, pastedNumbers, COMMIT_MIN_MS, DEFAULT_HEIGHT, RESIZE_QUIET_MS, PICTURE_MAX_ROWS, PICTURE_ROOM_ROWS, pictureCells,- pictureRowsFor, projectSlug, renderKey, rowsAfter, sentFiles, sentImages, shortError, type Under,+ pictureRowsFor, projectSlug, renderKey, rowsAfter, sentFiles, sentImages, shortError, type Under, altOf, cellPixels, fillCall, filledPath, labelColumns, labelOf, linesOf, previewLayout, PREVIEW_RETRY_MS, sameNumbers,} from '../hooks/register'// ---- Pure helpers -----------------------------------------------------------@@ -371,6 +372,11 @@ function world(on: any, o: {herdr?: string// The bytes (base64) a read of a PNG in the cache gives (else PNG).bytes?: string+ // A fill compile (render/fill.typ) ends only when this settles; it fails with `failFill`.+ holdFill?: Promise<void>+ failFill?: boolean+ // The root of a fill (FILL_SH) can not be made.+ failStage?: boolean} = {}) {// Intelligent UI's store in memory (as mock.store), and its writes by key;// `redrawsAt`: by key, the `done` writes (redraws) made when its write ended.@@ -378,8 +384,11 @@ function world(on: any, o: {const kept: Record<string, unknown> = {}const redrawsAt: Record<string, number> = {}let redraws = 0+ // Every state write, by key.+ const sets: string[] = []on('state.set', async ($: any, e: any, next: any) => {if (e.key === 'done') redraws+++ sets.push(e.key)return next(e)})on('store.get', async ($: any, e: any) => ({ value: store.get(e.key) }))@@ -401,6 +410,10 @@ function world(on: any, o: {const heads: string[] = []const reads: string[] = []const converts: string[][] = []+ const fills: string[][] = []+ // The roots of the fills made (FILL_SH's arguments), and the folders removed.+ const fillRoots: string[][] = []+ const removed: string[] = []const moves: string[][] = []// The PNGs perl writes (flip mode), by path.const pngs = new Map<string, Uint8Array>()@@ -446,6 +459,15 @@ function world(on: any, o: {delete files[argv[2]!]return ok('')}+ if (argv[0] === 'sh' && argv[2] === FILL_SH) return (fillRoots.push(argv.slice(4)), ok('', o.failStage ? 1 : 0))+ if (argv[0] === 'rm' && argv[1] === '-rf') return (removed.push(argv[2]!), ok(''))+ if (argv[0] === '/usr/bin/typst' && argv[1] === 'compile' && argv.at(-2)!.endsWith('/fill.typ')) {+ fills.push(argv)+ await o.holdFill+ if (o.failFill) return ok('', 1)+ files[argv.at(-1)!] = HEAD+ return ok('')+ }if (argv[0] === '/usr/bin/typst' && argv[1] === 'compile' && argv.at(-2)!.endsWith('/render/convert.typ')) {converts.push(argv)const file = argv.find(a => a.startsWith('f='))!.slice(2)@@ -502,7 +524,7 @@ function world(on: any, o: {})on('ui.toast', async () => ({ value: undefined }))on('ui.render', () => ENGINE as any)- return { clock, heads, reads, converts, moves, pngs, docs, logs, files, texts, mtimes, written, kept, redrawsAt, redraws: () => redraws }+ return { clock, heads, reads, converts, moves, pngs, docs, logs, files, texts, mtimes, written, kept, redrawsAt, redraws: () => redraws, sets, stats, fills, fillRoots, removed }}const sent = (output: unknown, isErrored = false, id = 'tu-1') =>@@ -1226,15 +1248,62 @@ test('an errored SendUserFile call leaves the row as it is', async ($, on) => {expect(await ui.drawn()).toMatchObject(ENGINE)})-test('an image the user pastes draws under the prompt', async ($, on) => {- world(on, { files: { [`${PASTED}/5.png`]: HEAD }, dirs: [PASTED] })+// The previews as the row draws them: after the gutter, a line of pictures+// PREVIEW_GAP apart. The band's have no gutter: each is after its number.+const PREVIEWS = { type: 'Box', props: { flexDirection: 'column', marginLeft: 2 } }+const BAND_PREVIEWS = { type: 'Box', props: { flexDirection: 'column', marginLeft: 0 } }+const LINE = { type: 'Box', props: { flexDirection: 'row', columnGap: 2 } }+// A band preview: its dim number in a column of the number and a space, then+// the picture.+const labelled = (n: number) => ({+ type: 'Box',+ props: { flexDirection: 'row' },+ children: [+ { type: 'Box', props: { width: `#${n}`.length + 1 }, children: [{ type: 'Text', props: { dimColor: true } }] },+ { type: 'Image', props: { alt: `[Image #${n}]` } },+ ],+})++test('an image the user pastes draws under the prompt, as its preview did: small, after the gutter', async ($, on) => {+ world(on, { files: { [`${PASTED}/5.png`]: HEAD, [`${PASTED}/6.png`]: TALL }, dirs: [PASTED] })await ready($)const ui = await $.ui.mount(prompt('[Image #5] what is this'))const tree: any = await ui.drawn()expect(tree.children[0]).toMatchObject(ENGINE)const [image] = images(tree)expect(image.props.source).toMatchObject({ file: `${PASTED}/5.png`, format: 'png' })- expect(image.props).toMatchObject({ columns: 40, rows: 10 })+ expect(image.props).toMatchObject({ columns: 20, rows: 5, alt: '[Image #5]' })+ expect(tree.children[1]).toMatchObject(PREVIEWS)+ expect(tree.children[1].children[0]).toMatchObject(LINE)+ // The image alone: no number (Claude Code shows it in the row).+ expect(tree.children[1].children[0].children[0]).toMatchObject({ type: 'Image', props: { alt: '[Image #5]' } })+ // Two images: side by side, 2 columns apart.+ const two: any = await (await $.ui.mount(prompt('[Image #5] and [Image #6]', undefined, 'u-2'))).drawn()+ expect(two.children[1].children).toHaveLength(1)+ expect(two.children[1].children[0]).toMatchObject(LINE)+ expect(alts(two)).toEqual(['[Image #5]', '[Image #6]'])+})++test('under the prompt row: the plain image while Typst makes the filled copy, then the copy (that row alone draws again)', async ($, on) => {+ let release = () => {}+ const holdFill = new Promise<void>(r => (release = r))+ const { clock, fills, sets } = world(on, { typst: true, files: { [`${PASTED}/5.png`]: HEAD }, dirs: [PASTED], holdFill })+ await ready($)+ const ui = await $.ui.mount(prompt('[Image #5] what is this'))+ expect(images(await ui.drawn())[0].props.source.file).toBe(`${PASTED}/5.png`)+ await clock.settle()+ expect(fills.length).toBe(1)+ const before = { filled: sets.filter(k => k === 'filled').length, done: sets.filter(k => k === 'done').length }+ release()+ await clock.settle()+ const out = fills[0]!.at(-1)!.replace(/image\.part\.png$/, 'image.png')+ expect(images(await ui.drawn())[0].props).toMatchObject({ source: { file: out }, columns: 20, rows: 5 })+ expect(sets.filter(k => k === 'filled').length).toBe(before.filled + 1)+ expect(sets.filter(k => k === 'done').length).toBe(before.done)+ // Kept: a redraw makes no new copy.+ await ui.redraw()+ expect(images(await ui.drawn())[0].props.source.file).toBe(out)+ expect(fills.length).toBe(1)})test('a pasted image is found when the folder name is not the plain slug', async ($, on) => {@@ -1277,16 +1346,321 @@ test('in a terminal that draws no pictures, rows stay as they are', async ($, onexpect(await (await $.ui.mount(prompt('[Image #5] hi'))).drawn()).toMatchObject(ENGINE)})+// ---- The draft's previews -----------------------------------------------------++test('previews go side by side, 2 columns apart, and wrap only where one does not fit', () => {+ expect(linesOf([10, 10, 10], 34)).toEqual([[0, 1, 2]])+ expect(linesOf([10, 10, 10], 33)).toEqual([[0, 1], [2]])+ // A box wider than the line has one of its own.+ expect(linesOf([40, 10], 30)).toEqual([[0], [1]])+ expect(linesOf([], 30)).toEqual([])+})++test('a preview\'s alt is its mark in the draft', () => {+ expect(altOf(12)).toBe('[Image #12]')+ // The box a filled copy fills: the cells' pixels exactly.+ expect(cellPixels({ columns: 17, rows: 5 }, { width: 8, height: 16 })).toEqual({ width: 136, height: 80 })+})++test('a preview is about 5 rows tall, keeps its aspect, and leaves a row of the band free', () => {+ const wide = { width: 640, height: 320 }+ // 640 x 320 px at 16 x 32 px cells: 20 x 5 cells at 5 rows.+ expect(previewLayout([wide], DEFAULT_CELL, 95, 20)).toEqual({ boxes: [{ columns: 20, rows: 5 }], lines: [[0]] })+ // A short band: the picture and a free row.+ expect(previewLayout([wide], DEFAULT_CELL, 95, 5)?.boxes[0]).toMatchObject({ rows: 4 })+ expect(previewLayout([wide], DEFAULT_CELL, 95, 2)?.boxes[0]).toMatchObject({ rows: 1 })+ // Not one row, or no column, for a picture: nothing.+ expect(previewLayout([wide], DEFAULT_CELL, 95, 1)).toBe(null)+ expect(previewLayout([wide], DEFAULT_CELL, 0, 20)).toBe(null)+ // A tiny picture keeps its size.+ expect(previewLayout([{ width: 32, height: 32 }], DEFAULT_CELL, 95, 20)?.boxes[0]).toEqual({ columns: 2, rows: 1 })+ // A wide picture is as wide as the line.+ expect(previewLayout([{ width: 3136, height: 640 }], DEFAULT_CELL, 26, 20)?.boxes[0]).toEqual({ columns: 26, rows: 3 })+ // 20 columns each, 2 apart: 42 do not fit in 41, so two lines; in a short+ // band the rows go down until they fit (here at 4 rows, on one line).+ expect(previewLayout([wide, wide], DEFAULT_CELL, 42, 20)!.lines).toEqual([[0, 1]])+ expect(previewLayout([wide, wide], DEFAULT_CELL, 41, 20)!.lines).toEqual([[0], [1]])+ const two = previewLayout([wide, wide], DEFAULT_CELL, 41, 8)!+ expect(two.lines).toEqual([[0, 1]])+ expect(two.boxes[0]!.rows).toBe(4)+ // The band's labels take columns: 3 + 20, 2, 3 + 20 = 48 do not fit in 47.+ expect(previewLayout([wide, wide], DEFAULT_CELL, 48, 20, [3, 3])!.lines).toEqual([[0, 1]])+ expect(previewLayout([wide, wide], DEFAULT_CELL, 47, 20, [3, 3])!.lines).toEqual([[0], [1]])+ // A wide picture is as wide as the line less its label; no column left: nothing.+ expect(previewLayout([{ width: 3136, height: 640 }], DEFAULT_CELL, 26, 20, [3])?.boxes[0]).toMatchObject({ columns: 23 })+ expect(previewLayout([wide], DEFAULT_CELL, 3, 20, [3])).toBe(null)+})++test('a band preview\'s number and its column', () => {+ expect(labelOf(5)).toBe('#5')+ expect(labelColumns(5)).toBe(3)+ expect(labelColumns(12)).toBe(4)+})++test('the same numbers in the same order', () => {+ expect(sameNumbers([1, 2], [1, 2])).toBe(true)+ expect(sameNumbers([1, 2], [2, 1])).toBe(false)+ expect(sameNumbers([], [])).toBe(true)+ expect(sameNumbers([1], [])).toBe(false)+})++test('the filled copy: Typst covers the box with the image, in a root of its own (never /)', () => {+ const { argv, env } = fillCall({ typst: '/usr/bin/typst', run: '/c/k/r-1', cache: '/c', box: { width: 320, height: 160 }, out: '/c/k/image.part.png' })+ expect(argv).toEqual([+ '/usr/bin/typst', 'compile', '--root', '/c/k/r-1', '--format', 'png', '--diagnostic-format', 'short', '--ppi', '72',+ '--input', 'f=/image.png', '--input', 'maxw=320', '--input', 'maxh=160', '/c/k/r-1/fill.typ', '/c/k/image.part.png',+ ])+ expect(env.TYPST_PACKAGE_CACHE_PATH).toBe('/c/pkg-cache')+})++test('a filled copy is kept by the file, its stamp and the box', () => {+ const p = { file: '/t/5.png', stamp: '1:1000', bytes: 1000, width: 640, height: 320 }+ const box = { width: 320, height: 160 }+ const one = filledPath('/c', p, box)+ expect(one).toMatch(/^\/c\/[0-9a-f]{16}\/image\.png$/)+ expect(filledPath('/c', { ...p }, { ...box })).toBe(one)+ const others = [filledPath('/c', { ...p, stamp: '2:1000' }, box), filledPath('/c', { ...p, file: '/t/6.png' }, box), filledPath('/c', p, { width: 256, height: 128 })]+ expect(new Set([one, ...others]).size).toBe(4)+})++// The band above the prompt box, and the editor beneath the mod: an edit is+// its splice, a change the box as given.+const band = (o: Record<string, unknown> = {}) =>+ ({+ plugin: 'cc-intelligent-ui', surface: 'terminal', component: 'AbovePrompt', requestId: 'band',+ props: { hasSurvey: false, isWorking: false, maxRows: 20, bodyColumns: 95, scroll: { offset: 0, bodyRows: 19 }, view: {}, ...o },+ viewport: { columns: 100, rows: 40 },+ }) as any+function editor(on: any) {+ on('prompt.edit', async ($: any, e: any) => ({ text: e.text.slice(0, e.start) + e.inputText + e.text.slice(e.end), cursor: e.start + e.inputText.length }))+ on('prompt.change', async ($: any, e: any) => ({ text: e.text }))+}+// A paste (or a key) at the end of `text`; a deletion of `[from, to)`.+const typed = (text: string, inputText: string) => ({ origin: { kind: 'composer' }, text, cursor: text.length, start: text.length, end: text.length, inputText }) as any+const cut = (text: string, from: number, to: number) => ({ origin: { kind: 'composer' }, text, cursor: to, start: from, end: to, inputText: '' }) as any+// The alt texts of the previews drawn.+const alts = (tree: any): string[] => images(tree).map(i => i.props.alt)++test('a pasted image shows above the prompt box until the image text is deleted or the prompt is sent', async ($, on) => {+ world(on, { files: { [`${PASTED}/5.png`]: HEAD, [`${PASTED}/6.png`]: TALL }, dirs: [PASTED] })+ editor(on)+ await ready($)+ const prompt = ($ as any).prompt+ const ui = await $.ui.mount(band())+ expect(await ui.drawn()).toMatchObject(ENGINE)+ // A paste: its preview after its number (no Typst here: the plain image),+ // then the band beneath.+ await prompt.edit(typed('look ', '[Image #5]'))+ let tree: any = await ui.drawn()+ expect(images(tree).map(i => i.props)).toEqual([expect.objectContaining({ source: expect.objectContaining({ file: `${PASTED}/5.png` }), columns: 20, rows: 5, alt: '[Image #5]' })])+ expect(tree.children.at(-1)).toMatchObject(ENGINE)+ expect(tree.children[0]).toMatchObject(BAND_PREVIEWS)+ expect(tree.children[0].children[0].children[0]).toMatchObject(labelled(5))+ // A second paste: side by side, each at most 5 rows, the aspect kept.+ await prompt.edit(typed('look [Image #5] ', '[Image #6]'))+ tree = await ui.drawn()+ expect(images(tree).map(i => [i.props.columns, i.props.rows])).toEqual([[20, 5], [2, 5]])+ expect(alts(tree)).toEqual(['[Image #5]', '[Image #6]'])+ expect(tree.children[0].children[0]).toMatchObject(LINE)+ expect(tree.children[0].children[0].children[1]).toMatchObject(labelled(6))+ // The text of #5 deleted: #6 alone.+ await prompt.edit(cut('look [Image #5] [Image #6]', 5, 16))+ expect(alts(await ui.drawn())).toEqual(['[Image #6]'])+ // A submit clears the box: no preview.+ await prompt.change({ text: '', cursor: 0, origin: { kind: 'clear' } })+ expect(await ui.drawn()).toMatchObject(ENGINE)+ // A recalled entry with an image: it shows again.+ await prompt.change({ text: '[Image #5] again', cursor: 16, origin: { kind: 'history' } })+ expect(alts(await ui.drawn())).toEqual(['[Image #5]'])+})++test('the filled copy: the plain image while Typst makes it, then the copy (the band alone draws again), one per image and box', async ($, on) => {+ let release = () => {}+ const holdFill = new Promise<void>(r => (release = r))+ const { clock, fills, sets } = world(on, { typst: true, files: { [`${PASTED}/5.png`]: HEAD }, dirs: [PASTED], holdFill })+ editor(on)+ await ready($)+ const ui = await $.ui.mount(band())+ await ($ as any).prompt.edit(typed('', '[Image #5]'))+ expect(images(await ui.drawn())[0].props.source.file).toBe(`${PASTED}/5.png`)+ await clock.settle()+ // The box of 20 x 5 cells of 16 x 32 px.+ expect(fills.length).toBe(1)+ const argv = fills[0]!+ expect(argv).toEqual(expect.arrayContaining(['f=/image.png', 'maxw=320', 'maxh=160']))+ const drafts = sets.filter(k => k === 'draft').length+ const redraws = sets.filter(k => k === 'done').length+ release()+ await clock.settle()+ const out = argv.at(-1)!.replace(/image\.part\.png$/, 'image.png')+ const [image] = images(await ui.drawn())+ expect(image.props.source.file).toBe(out)+ expect(image.props).toMatchObject({ columns: 20, rows: 5, alt: '[Image #5]' })+ expect(sets.filter(k => k === 'draft').length).toBe(drafts + 1)+ expect(sets.filter(k => k === 'done').length).toBe(redraws)+ // A new draft with the same image: the copy at once, no compile.+ await ($ as any).prompt.edit(typed('[Image #5]', ' [Image #7]'))+ await ($ as any).prompt.edit(cut('[Image #5] [Image #7]', 10, 21))+ await clock.settle()+ expect(images(await ui.drawn())[0].props.source.file).toBe(out)+ expect(fills.length).toBe(1)+})++test('a fill runs in a root of its own: only fill.typ and a link to the pasted file, never /, removed after', async ($, on) => {+ const { clock, fills, fillRoots, removed } = world(on, { typst: true, files: { [`${PASTED}/5.png`]: HEAD }, dirs: [PASTED] })+ editor(on)+ await ready($)+ const ui = await $.ui.mount(band())+ await ($ as any).prompt.edit(typed('', '[Image #5]'))+ await clock.settle()+ expect(fills.length).toBe(1)+ const argv = fills[0]!+ const run = argv[argv.indexOf('--root') + 1]!+ expect(run).not.toBe('/')+ const key = argv.at(-1)!.replace(/\/image\.part\.png$/, '')+ expect(run.startsWith(`${key}/r-`)).toBe(true)+ expect(argv.at(-2)).toBe(`${run}/fill.typ`)+ expect(argv).toContain('f=/image.png')+ expect(argv.some(a => a.includes(PASTED))).toBe(false)+ // The root is made first, with the pasted file, and goes after the run.+ expect(fillRoots).toEqual([[run, expect.any(String), `${PASTED}/5.png`]])+ expect(removed).toContain(run)+ expect(images(await ui.drawn())[0].props.source.file).toBe(`${key}/image.png`)+})++test('a fill whose root can not be made runs no Typst; the plain image stays', async ($, on) => {+ const { clock, fills, fillRoots } = world(on, { typst: true, files: { [`${PASTED}/5.png`]: HEAD }, dirs: [PASTED], failStage: true })+ editor(on)+ await ready($)+ const ui = await $.ui.mount(band())+ await ($ as any).prompt.edit(typed('', '[Image #5]'))+ await clock.settle()+ expect(fillRoots.length).toBe(1)+ expect(fills.length).toBe(0)+ await ui.redraw()+ expect(images(await ui.drawn())[0].props.source.file).toBe(`${PASTED}/5.png`)+})++test('in herdr in iTerm2, a pasted preview shows only its filled copy, stored flipped by rows for its box', async ($, on) => {+ let release = () => {}+ const holdFill = new Promise<void>(r => (release = r))+ const { clock, fills, pngs } = world(on, { typst: true, herdr: '/Applications/iTerm.app/Contents/MacOS/iTerm2', files: { [`${PASTED}/5.png`]: HEAD }, dirs: [PASTED], bytes: ROWS_PNG, holdFill })+ editor(on)+ await ready($)+ const ui = await $.ui.mount(band())+ await ($ as any).prompt.edit(typed('', '[Image #5]'))+ // The plain image is not stored for flip mode: nothing while the copy is made.+ expect(images(await ui.drawn())).toEqual([])+ release()+ await clock.settle()+ expect(fills.length).toBe(1)+ await ui.redraw()+ const [image] = images(await ui.drawn())+ expect(image.props).toMatchObject({ columns: 20, rows: 5, alt: '[Image #5]' })+ expect(image.props.source.file).toMatch(/^\/c\/[0-9a-f]{16}\/flip\.png$/)+ expect([...decodePng(pngs.get(image.props.source.file)!).data]).toEqual([...flipRows(ROWS_IMG, 5).data])+})++test('in herdr in iTerm2, a sent prompt draws its pasted images from their flipped filled copies', async ($, on) => {+ let release = () => {}+ const holdFill = new Promise<void>(r => (release = r))+ const { clock, pngs } = world(on, { typst: true, herdr: '/Applications/iTerm.app/Contents/MacOS/iTerm2', files: { [`${PASTED}/5.png`]: HEAD }, dirs: [PASTED], bytes: ROWS_PNG, holdFill })+ await ready($)+ const ui = await $.ui.mount(prompt('look [Image #5]'))+ expect(images(await ui.drawn())).toEqual([])+ release()+ await clock.settle()+ await ui.redraw()+ const [image] = images(await ui.drawn())+ expect(image.props.source.file).toMatch(/^\/c\/[0-9a-f]{16}\/flip\.png$/)+ expect(pngs.has(image.props.source.file)).toBe(true)+})++test('a fill compile that fails leaves the plain image, and is not tried again', async ($, on) => {+ const { clock, fills } = world(on, { typst: true, files: { [`${PASTED}/5.png`]: HEAD }, dirs: [PASTED], failFill: true })+ editor(on)+ await ready($)+ const ui = await $.ui.mount(band())+ await ($ as any).prompt.edit(typed('', '[Image #5]'))+ await clock.settle()+ expect(fills.length).toBe(1)+ await ui.redraw()+ expect(images(await ui.drawn())[0].props.source.file).toBe(`${PASTED}/5.png`)+ await clock.settle()+ expect(fills.length).toBe(1)+})++test('a key that keeps the images reads no file and draws nothing new', async ($, on) => {+ const { heads, sets, stats } = world(on, { files: { [`${PASTED}/5.png`]: HEAD }, dirs: [PASTED] })+ editor(on)+ await ready($)+ const prompt = ($ as any).prompt+ const ui = await $.ui.mount(band())+ await prompt.edit(typed('', '[Image #5]'))+ expect(alts(await ui.drawn())).toEqual(['[Image #5]'])+ const read = heads.length+ const statted = stats.get(`${PASTED}/5.png`)+ expect(statted).toBeGreaterThan(0)+ const drafts = sets.filter(k => k === 'draft').length+ await prompt.edit(typed('[Image #5]', ' what'))+ await prompt.edit(typed('[Image #5] what', ' is this'))+ await ui.redraw()+ expect(alts(await ui.drawn())).toEqual(['[Image #5]'])+ expect(heads.length).toBe(read)+ expect(stats.get(`${PASTED}/5.png`)).toBe(statted)+ expect(sets.filter(k => k === 'draft').length).toBe(drafts)+})++test('an image file written after the paste shows a moment later', async ($, on) => {+ const { clock, files, sets } = world(on, { dirs: [PASTED] })+ editor(on)+ await ready($)+ const ui = await $.ui.mount(band())+ await ($ as any).prompt.edit(typed('', '[Image #5]'))+ expect(await ui.drawn()).toMatchObject(ENGINE)+ const drafts = sets.filter(k => k === 'draft').length+ const redraws = sets.filter(k => k === 'done').length+ files[`${PASTED}/5.png`] = HEAD+ await clock.advance(PREVIEW_RETRY_MS)+ expect(alts(await ui.drawn())).toEqual(['[Image #5]'])+ // The band alone draws again: the draft written once more, no refresh.+ expect(sets.filter(k => k === 'draft').length).toBe(drafts + 1)+ expect(sets.filter(k => k === 'done').length).toBe(redraws)+})++test('the band stays as it is with a survey, with no image, with Intelligent UI off, and for a file that is gone', async ($, on) => {+ world(on, { files: { [`${PASTED}/5.png`]: HEAD }, dirs: [PASTED] })+ editor(on)+ await ready($)+ const prompt = ($ as any).prompt+ // No image in the draft.+ await prompt.edit(typed('', 'hello'))+ expect(await (await $.ui.mount(band())).drawn()).toMatchObject(ENGINE)+ // A survey holds the band.+ await prompt.edit(typed('hello ', '[Image #5]'))+ expect(await (await $.ui.mount({ ...band({ hasSurvey: true }), requestId: 'band-2' })).drawn()).toMatchObject(ENGINE)+ // A file that is gone is left out; none left: the band as it is.+ await prompt.edit(typed('hello [Image #5] ', '[Image #9]'))+ expect(alts(await (await $.ui.mount({ ...band(), requestId: 'band-3' })).drawn())).toEqual(['[Image #5]'])+ await prompt.change({ text: '[Image #9]', cursor: 10, origin: { kind: 'undo' } })+ expect(await (await $.ui.mount({ ...band(), requestId: 'band-4' })).drawn()).toMatchObject(ENGINE)+ // Off.+ await prompt.change({ text: '[Image #5]', cursor: 10, origin: { kind: 'undo' } })+ await $.command.run({ command: 'iui', args: 'off' } as any)+ expect(await (await $.ui.mount({ ...band(), requestId: 'band-5' })).drawn()).toMatchObject(ENGINE)+})+// ---- The height setting --------------------------------------------------------test('a kept height caps sent and pasted images; height full draws them taller at the commit', async ($, on) => {+test('a kept height caps sent images (not pasted ones); height full draws them taller at the commit', async ($, on) => {const { clock, kept } = world(on, { stored: { height: 12 }, files: { '/t/tall.png': TALL, [`${PASTED}/5.png`]: TALL }, dirs: [PASTED] })const ran = await ready($)expect(ran.text).toContain('height 12 rows')const ui = await $.ui.mount(sent(attach('/t/tall.png')))expect(images(await ui.drawn())[0].props.rows).toBe(12)+ // A pasted image does not follow the setting: its preview's size.const pasted = await $.ui.mount(prompt('[Image #5] tall'))- expect(images(await pasted.drawn())[0].props.rows).toBe(12)+ expect(images(await pasted.drawn())[0].props.rows).toBe(5)const set = await $.command.run({ command: 'iui', args: 'height full' } as any)expect(set.text).toContain('full (the terminal height less 16 rows)')expect(kept.height).toBe('full')@@ -1298,7 +1672,7 @@ test('a kept height caps sent and pasted images; height full draws them taller aawait ui.redraw(sent(attach('/t/tall.png')).props)expect(images(await ui.drawn())[0].props.rows).toBe(24)await pasted.redraw(prompt('[Image #5] tall').props)- expect(images(await pasted.drawn())[0].props.rows).toBe(24)+ expect(images(await pasted.drawn())[0].props.rows).toBe(5)// A number is never more than full.await $.command.run({ command: 'iui', args: 'height 36' } as any)expect(kept.height).toBe(36)@@ -1410,7 +1784,7 @@ test('height with no value shows it; a bad value is refused; a bad kept value isconst { kept } = world(on, { stored: { height: 3 } })const shown = await $.command.run({ command: 'iui', args: 'height' } as any)expect(shown.text).toContain('The most rows an image or a Typst drawing takes: half (half the terminal, 6 to 24 rows)')- expect(shown.text).toContain('This applies to images (sent or pasted) and to Typst drawings in answers.')+ expect(shown.text).toContain('This applies to images that Claude sends and to Typst drawings in answers (images that you paste show small, as their preview above the prompt box).')const bad = await $.command.run({ command: 'iui', args: 'height 5' } as any)expect(bad.text).toContain('Not understood: "height 5"')expect(kept.height).toBe(undefined)diff --git a/types/index.d.ts b/types/index.d.tsindex 87ce264..6c6b4d3 100644--- a/types/index.d.ts+++ b/types/index.d.ts@@ -9,6 +9,12 @@ declare module 'claude-code' {// Goes up each time a render finishes or the setup changes, so the// AssistantMessage hooks that read it draw again.done: number+ // The `[Image #N]` numbers of the prompt box's draft, so the band above+ // the prompt draws their previews.+ draft: number[]+ // Per prompt row (its requestId): goes up when a filled copy of one of+ // its pasted images is made, so that row draws again.+ filled: StateFamily<number>}}}