Draw diagrams, LaTeX math, and tool-result images inline in kitty-graphics terminals

A Claude Code mod that draws diagrams, LaTeX math, and tool-result images inline in the terminal transcript, using the kitty graphics protocol.

mermaid, dot (or graphviz), d2, or svg in Claude's reply is rendered and drawn under the reply. The code stays visible above the picture.$$ … $$, \[ … \], or a math, latex, or tex code block, is typeset with MathJax. Inline $…$ stays text, since a picture cannot sit inside a line, and a latex block holding a whole document (\documentclass) stays code.Read on a PNG, JPEG, GIF, WebP, or SVG file, or a screenshot an MCP tool returns, is drawn under the tool's row, where Claude Code otherwise shows only a size line; an SVG read only in part is left alone. In a collapsed tool group they are thumbnails; ctrl+o unfolds the group and draws them full size. A PNG, JPEG, GIF, WebP, or SVG Claude sends to another device with SendUserFile is drawn under its attachment line too, so it is on screen when you come back to the terminal. It is read from disk when the row is first drawn; overwriting the file later leaves that picture as it was.A picture the size caps would shrink below 60% of its natural size grows up to the terminal's height, and one still smaller says so under it, such as shown at 43% · /figures to enlarge.
/figures opens a pane with the session's pictures, starting on the newest one drawn too small to read, or else the newest. In fullscreen mode, select part of a diagram's source or a tool row with the mouse before running it to start on that picture instead. p and n step through them, o opens the current one with open (macOS) or xdg-open, and Escape or your next prompt closes the pane.

The plugin also ships a diagrams skill, which tells Claude which languages are drawn and how to write them so they render.
Each rendered PNG is cached under $TMPDIR/claude-figures/, named by a hash of the source and the theme, so a resumed session draws its pictures again without re-rendering. Files older than seven days are removed when a session starts.
Claude Code 2.1.288 or later, with mods (function-hook plugins) enabled, in a terminal it treats as kitty-graphics capable; see Terminal support. Each kind of picture also needs programs on PATH:
| Picture | Needs |
|---|---|
mermaid | mmdr or mmdc; resvg recommended |
dot, graphviz | Graphviz's dot and resvg |
d2 | d2 and resvg |
svg, math | resvg |
| Tool images other than PNG | magick (ImageMagick) or sips (built into macOS) |
scale option.claude plugin marketplace add natsukium/claude-code-figures-plugin
claude plugin install figures@figures
Install mmdr with cargo install mermaid-rs-renderer.
Set these with /config or claude plugin configure figures.
| Option | Default | Meaning |
|---|---|---|
theme | auto | auto follows Claude Code's theme; or dark, default, forest, neutral, modern (mmdr only; mmdc draws it as default). Math is drawn light on dark and dark otherwise |
background | transparent | Diagram background; transparent lets the terminal's background show through |
scale | 2 | Pixel density of the picture; above 1 needs resvg |
cell_width_px | 8 | Assumed width of one terminal cell, in the pixels diagrams are laid out in |
cell_aspect | 2.1 | Assumed height-to-width ratio of one terminal cell |
max_columns | 120 | Widest a picture may be, in cells; it is also kept inside the terminal |
max_rows | 30 | Tallest a picture may be before it is grown for legibility, in cells |
renderers | (empty) | Renderer ids in preference order, comma-separated; see below |
tool_images | true | Draw images found in tool results |
tool_image_max_rows | 12 | Tallest a tool-result thumbnail may be, in cells |
renderers picks and orders the programs that draw each language. The ids are mmdr and mmdc (mermaid), dot, d2, svg, and mathjax. A language with any of its renderers listed uses only those, in the listed order; the others keep the default. So mmdc draws mermaid with mmdc alone, and mmdc,mmdr tries mmdc first. An id no renderer has is reported when a session starts.
Claude Code enables pictures only when the terminal's XTVERSION reply names kitty (0.28.0 or later) or Ghostty. A terminal that answers the kitty graphics query correctly under any other name still gets the alt text, such as [mermaid diagram 1]. Setting TERM or TERM_PROGRAM has no effect.
To use the plugin on such a terminal, set the override Claude Code reads:
CLAUDE_CODE_FORCE_TERMINAL_IMAGES=1 claude
This also skips Claude Code's tmux check. Inside tmux, the pictures additionally need set -g allow-passthrough on.
To see what Claude Code decided, start it with --debug and look for the Terminal capabilities: line in the debug log.
cell_width_px and cell_aspect, since mods are not told the terminal's cell size in pixels. Adjust them if pictures look stretched.theme=auto follows Claude Code's own theme (/config): a light variant draws light pictures, the others dark. When Claude Code's theme is itself auto, its resolved value is not exposed, so COLORFGBG decides where the terminal exports it, and dark otherwise.A --> B -->|ok| C) is misparsed; write one edge per line. Such a block renders without an error, so it is not handed to mmdc; set renderers to mmdc to avoid mmdr.\text{日本語}, is drawn by resvg with a system font./figures pane shares the screen with the prompt, so in a short terminal it has only a few rows; o opens the picture at full size outside the terminal.tsconfig.json extends the engine's typings in .claude-plugin/types/, which Vite and Vitest read as well as tsc. Claude Code writes them only when it loads the plugin from this folder, and no command writes them alone, so pnpm types starts claude -p --plugin-dir . with a dummy API key and ignores the failed request once the typings exist. build, test, and typecheck run it first. Claude Code is a dev dependency, so the scripts and pnpm exec claude use the version pinned in pnpm-lock.yaml, whatever claude is on PATH.
pnpm install
pnpm build # regenerate hooks/vendor/
pnpm test # unit tests (Vitest, tests/unit/*.spec.ts)
pnpm typecheck
pnpm exec claude plugin validate .
pnpm exec claude plugin test . # hooks through the engine (tests/engine/*.test.ts)
Claude Code loads hooks/ as TypeScript source, so only third-party code is built. pnpm build bundles MathJax and @noble/hashes with Vite into hooks/vendor/, which is committed so the repository stays installable as is. A hooks module imports no file over 1 MiB and cannot import modules at run time, so the build splits the bundle into chunks and writes each of the font's glyph ranges as JSON, which the plugin reads the first time a formula uses it.
The engine refuses a module that passes $ across an import, so hooks/register.tsx wraps what the plugin needs from $ in an Io object (hooks/io.ts), and every other module takes that.
Apache-2.0. hooks/vendor/ is built from MathJax and its New Computer Modern font, both Apache-2.0, and @noble/hashes, MIT.
hooks/register.tsx 123 lines1import { atom, read, update } from 'claude-code'
2import type { EngineInterface, Register, RenderChildren } from 'claude-code'
3
4import { parseConfig } from './config.ts'
5import type { Figures } from './figures.ts'
6import type { Io } from './io.ts'
7import { Memo, cacheDir, expire } from './pipeline/cache.ts'
8import { Pipeline } from './pipeline/index.ts'
9import { createRegistry } from './renderers/index.ts'
10import { replyPictures, toolGroupPictures, toolUsePictures } from './ui/inline.tsx'
11import { PANE, drawPane, gallery, selectedIn } from './ui/pane.tsx'
12import { type TerminalElements, type pictures, viewportOf } from './ui/pictures.tsx'
13
14const selected = atom({ plugin: 'figures', key: 'selected' } as const, null)
15// Read by the pane so that each /figures redraws it: the engine does not track
16// the transcript the gallery comes from, and $.ui.invalidate("ui.render")
17// would redraw every transcript row this plugin draws as well.
18const opened = atom({ plugin: 'figures', key: 'opened' } as const, 0)
19
20function ioOf($: EngineInterface): Io {
21 return {
22 run: (argv, init) => $.process.run(argv, init),
23 exists: (path) => $.fs.exists(path),
24 readText: (path) => $.fs.read(path),
25 readBase64: async (path) => (await $.fs.read(path, { as: 'bytes' })).base64,
26 write: (path, text) => $.fs.write(path, text),
27 tmpdir: () => $.env.get('TMPDIR'),
28 colorfgbg: () => $.env.get('COLORFGBG'),
29 claudeTheme: async () => (await $.config.list()).find((row) => row.key === 'theme')?.value,
30 messages: () => $.session.messages(),
31 toast: (text) => $.ui.toast(text),
32 pluginRoot: $.plugin.root,
33 }
34}
35
36export const register: Register = (on, options) => {
37 const config = parseConfig(options)
38 const registry = createRegistry(config.renderers)
39 const figures: Figures = { config, registry, pipeline: new Pipeline(registry), sent: new Memo(), groups: new Map() }
40
41 on('session.start', async ($, e, next) => {
42 const io = ioOf($)
43 await expire(io, await cacheDir(io))
44 await $.command.register({ name: PANE, description: 'Browse the diagrams and images drawn in this session' })
45 if (registry.unknown.length > 0) {
46 $.ui.toast(`figures: unknown renderer ${registry.unknown.join(', ')} in the renderers option`)
47 }
48 return next(e)
49 })
50
51 on('command.run', { command: PANE }, async ($) => {
52 const selection = await $.ui.selection()
53 const index = selection && selectedIn(await gallery(ioOf($), figures), selection, figures.groups)
54 if (index !== undefined) await update($, selected, () => index)
55 await update($, opened, (n) => n + 1)
56 // The pane is where a picture too small inline is enlarged, so it asks for
57 // all the height the layout can spare rather than max_rows.
58 await $.ui.open({ id: PANE, title: 'Figures', focus: true, closeOnEscape: true, rows: 255 })
59 return { text: 'Figures pane opened: p/n move, o opens in the system viewer.' }
60 })
61
62 // Only the person's own prompt dismisses it; a notification or peer
63 // delivery is not the person moving on.
64 on('prompt.submit', async ($, e, next) => {
65 if (e.origin.kind === 'composer' || e.origin.kind === 'bridge') await $.ui.close({ id: PANE })
66 return next(e)
67 })
68
69 on('ui.render', { component: 'Pane', requestId: PANE }, async ($, e, next) => {
70 if (e.surface !== 'terminal') return next(e)
71 await read($, opened)
72 const selection = { index: await read($, selected), set: (index: number) => update($, selected, () => index) }
73 return drawPane(ioOf($), figures, $.ui.resolve(e), e.props, viewportOf(e), selection)
74 })
75
76 on('ui.render', { component: 'AssistantMessage' }, async ($, e, next) => {
77 if (e.surface !== 'terminal') return next(e)
78 const el = $.ui.resolve(e)
79 return under(el, () => next(e), await replyPictures(ioOf($), figures, el, e.props.text, viewportOf(e)))
80 })
81
82 // SendUserFile's ToolUse row draws nothing of its own, so a picture hung
83 // under it lands above the attachment line its ToolResult draws.
84 on('ui.render', { component: 'ToolResult' }, async ($, e, next) => {
85 if (e.surface !== 'terminal' || e.props.tool !== 'SendUserFile' || e.props.isErrored) return next(e)
86 const el = $.ui.resolve(e)
87 return under(el, () => next(e), await toolUsePictures(ioOf($), figures, el, e.props, viewportOf(e)))
88 })
89
90 on('ui.render', { component: 'ToolUse' }, async ($, e, next) => {
91 if (e.surface !== 'terminal' || e.props.isRunning || e.props.output === undefined) return next(e)
92 if (e.props.tool === 'SendUserFile') return next(e)
93 const el = $.ui.resolve(e)
94 return under(el, () => next(e), await toolUsePictures(ioOf($), figures, el, e.props, viewportOf(e)))
95 })
96
97 on('ui.render', { component: 'ToolGroup' }, async ($, e, next) => {
98 if (e.surface !== 'terminal' || e.props.isExpanded) return next(e)
99 if (e.requestId !== undefined) {
100 figures.groups.set(
101 e.requestId,
102 e.props.calls.flatMap((call) => (call.tool_use_id === undefined ? [] : [call.tool_use_id])),
103 )
104 }
105 const el = $.ui.resolve(e)
106 return under(el, () => next(e), await toolGroupPictures(ioOf($), figures, el, e.props.calls, viewportOf(e)))
107 })
108}
109
110async function under<Row extends RenderChildren>(
111 { Box }: TerminalElements,
112 row: () => Promise<Row>,
113 drawn: ReturnType<typeof pictures> | undefined,
114) {
115 if (drawn === undefined) return row()
116 return (
117 <Box flexDirection="column">
118 {await row()}
119 {drawn}
120 </Box>
121 )
122}
123hooks/config.ts 33 lines1export type Config = {
2 /** `auto` follows Claude Code's theme; anything else is a fixed renderer theme. */
3 theme: string
4 background: string
5 scale: number
6 cellWidthPx: number
7 cellAspect: number
8 maxColumns: number
9 maxRows: number
10 toolImages: boolean
11 toolImageMaxRows: number
12 /** Renderer ids in preference order; see renderers/index.ts. */
13 renderers: string[]
14}
15
16export function parseConfig(options: Record<string, unknown>): Config {
17 return {
18 theme: String(options.theme ?? 'auto'),
19 background: String(options.background ?? 'transparent'),
20 scale: Number(options.scale ?? 2),
21 cellWidthPx: Number(options.cell_width_px ?? 8),
22 cellAspect: Number(options.cell_aspect ?? 2.1),
23 maxColumns: Number(options.max_columns ?? 120),
24 maxRows: Number(options.max_rows ?? 30),
25 toolImages: options.tool_images !== false,
26 toolImageMaxRows: Number(options.tool_image_max_rows ?? 12),
27 renderers: String(options.renderers ?? '')
28 .split(',')
29 .map((id) => id.trim())
30 .filter((id) => id !== ''),
31 }
32}
33hooks/figures.ts 86 lines1import type { Config } from './config.ts'
2import { type Block, blocksIn } from './detect/markdown.ts'
3import { type FoundImage, SVG_MIME, imagesIn, readSvgIn, sentFilesIn } from './detect/tool-output.ts'
4import type { Io } from './io.ts'
5import type { Memo } from './pipeline/cache.ts'
6import type { Outcome, Pipeline } from './pipeline/index.ts'
7import type { Registry } from './renderers/index.ts'
8import { resolveTheme } from './theme.ts'
9
10/** What every hook shares: the options, the renderers they chose, the pipeline, and the files sent so far. */
11export type Figures = {
12 config: Config
13 registry: Registry
14 pipeline: Pipeline
15 sent: Memo<ToolPicture[]>
16 /**
17 * The tool_use_ids under each collapsed tool group row drawn so far. A
18 * selection in such a row names it by the group's own requestId, which
19 * the transcript does not carry.
20 */
21 groups: Map<string, string[]>
22}
23
24export const blocksOf = (figures: Figures, text: string): Block[] => blocksIn(text, figures.registry)
25
26export async function renderBlocks(io: Io, figures: Figures, blocks: Block[]): Promise<Outcome[]> {
27 const { config, pipeline } = figures
28 const theme = await resolveTheme(io, config.theme)
29 return Promise.all(
30 blocks.map((block) =>
31 pipeline.code(io, {
32 lang: block.lang,
33 source: block.source,
34 theme,
35 background: config.background,
36 scale: config.scale,
37 cellWidthPx: config.cellWidthPx,
38 }),
39 ),
40 )
41}
42
43export type ToolCall = { tool_use_id: string; tool: string; output?: unknown }
44
45/** A picture a tool returned or sent: raster bytes, or an SVG's source for the svg renderer. */
46export type ToolPicture = { image: FoundImage } | { svg: string }
47
48export async function renderImages(io: Io, figures: Figures, call: ToolCall): Promise<Outcome[]> {
49 const read = readSvgIn(call.tool, call.output)
50 const files: ToolPicture[] = [
51 ...imagesIn(call.output).map((image) => ({ image })),
52 ...(read === undefined ? [] : [{ svg: read }]),
53 ...(await sentFiles(io, figures, call)),
54 ]
55 return Promise.all(
56 files.map(async (file) =>
57 'image' in file
58 ? figures.pipeline.image(io, file.image)
59 : (await renderBlocks(io, figures, [{ lang: 'svg', source: file.svg }]))[0]!,
60 ),
61 )
62}
63
64// Held per call, not per path: a path sent again after being overwritten
65// must not repaint the earlier rows with the new picture.
66function sentFiles(io: Io, figures: Figures, call: ToolCall): Promise<ToolPicture[]> {
67 const files = sentFilesIn(call.tool, call.output)
68 if (files.length === 0) return Promise.resolve([])
69 return figures.sent.get(call.tool_use_id, async () => {
70 const read = await Promise.all(
71 files.map(async ({ path, mime }): Promise<ToolPicture[]> => {
72 // Moved, deleted, or over the engine's read limit: the send itself
73 // succeeded, so the row stays as the engine draws it.
74 try {
75 return mime === SVG_MIME
76 ? [{ svg: await io.readText(path) }]
77 : [{ image: { base64: await io.readBase64(path), mime } }]
78 } catch {
79 return []
80 }
81 }),
82 )
83 return read.flat()
84 })
85}
86hooks/io.ts 25 lines1import type { EngineInterface, SessionMessage } from 'claude-code'
2
3export type RunResult = Awaited<ReturnType<EngineInterface['process']['run']>>
4
5/**
6 * Everything the plugin reaches outside itself. The engine refuses a module
7 * that passes `$` across an import, so register.tsx builds this from `$` and
8 * the rest of the plugin sees only it.
9 */
10export type Io = {
11 run(argv: string[], init: { stdin: string; timeoutMs: number }): Promise<RunResult>
12 exists(path: string): Promise<boolean>
13 readText(path: string): Promise<string>
14 readBase64(path: string): Promise<string>
15 write(path: string, text: string): Promise<void>
16 tmpdir(): Promise<string | undefined>
17 /** The terminal's `fg;bg` colors, where it exports them. */
18 colorfgbg(): Promise<string | undefined>
19 /** Claude Code's own `theme` setting. */
20 claudeTheme(): Promise<unknown>
21 messages(): Promise<readonly SessionMessage[]>
22 toast(text: string): unknown
23 pluginRoot: string
24}
25hooks/pipeline/cache.ts 39 lines1import type { Io } from '../io.ts'
2import { sha256Hex } from '../vendor/hash.js'
3
4// JSON.stringify keeps insertion order, so keys are sorted to make two equal
5// requests hash alike however they were built.
6const canonical = (value: unknown): string =>
7 JSON.stringify(value, (_, v: unknown) =>
8 v !== null && typeof v === 'object' && !Array.isArray(v)
9 ? Object.fromEntries(Object.entries(v).sort(([a], [b]) => (a < b ? -1 : a > b ? 1 : 0)))
10 : v,
11 )
12
13export const cacheKey = (request: unknown): string => sha256Hex(canonical(request))
14
15export async function cacheDir(io: Io): Promise<string> {
16 const tmp = ((await io.tmpdir()) ?? '/tmp').replace(/\/$/, '')
17 return `${tmp}/claude-figures`
18}
19
20export async function expire(io: Io, dir: string, days = 7): Promise<void> {
21 await io
22 .run(['find', dir, '-name', '*.png', '-mtime', `+${days}`, '-delete'], { stdin: '', timeoutMs: 10_000 })
23 .catch(() => undefined)
24}
25
26/** One promise per key, so a picture drawn again while rendering joins the first render. */
27export class Memo<T> {
28 readonly #entries = new Map<string, Promise<T>>()
29
30 get(key: string, make: () => Promise<T>): Promise<T> {
31 let entry = this.#entries.get(key)
32 if (entry === undefined) {
33 entry = make()
34 this.#entries.set(key, entry)
35 }
36 return entry
37 }
38}
39hooks/pipeline/index.ts 125 lines1import type { FoundImage } from '../detect/tool-output.ts'
2import type { Io } from '../io.ts'
3import type { Registry } from '../renderers/index.ts'
4import type { Artifact, CodeRequest, Renderer } from '../renderers/types.ts'
5import { Memo, cacheDir, cacheKey } from './cache.ts'
6import { type Exec, MissingCommand, execWith, failure } from './exec.ts'
7import { readPngHeader, readPngSize } from './png.ts'
8import { convertToPng, rasterizeSvg, sansSerifFamily } from './raster.ts'
9
10/** A PNG on disk, with its size at 1x. */
11export type Rendered = { path: string; width: number; height: number }
12export type Outcome = Rendered | { error: string } | { skipped: true }
13
14const settle = (pending: Promise<Outcome>): Promise<Outcome> =>
15 pending.catch((error: unknown) => ({ error: error instanceof Error ? error.message : String(error) }))
16
17export class Pipeline {
18 readonly #registry: Registry
19 readonly #code = new Memo<Outcome>()
20 readonly #images = new Memo<Outcome>()
21 #canRasterizeSvg = true
22 #sansSerif: Promise<string | undefined> | undefined
23 #dir: Promise<string> | undefined
24
25 constructor(registry: Registry) {
26 this.#registry = registry
27 }
28
29 code(io: Io, request: CodeRequest): Promise<Outcome> {
30 const key = cacheKey(request)
31 return this.#code.get(key, () => settle(this.#renderCode(io, request, key)))
32 }
33
34 image(io: Io, image: FoundImage): Promise<Outcome> {
35 return this.#images.get(image.base64, () => settle(this.#materialize(io, image)))
36 }
37
38 async #renderCode(io: Io, request: CodeRequest, key: string): Promise<Outcome> {
39 const dir = await this.#cacheDir(io)
40 const pathAt = (zoom: number) => `${dir}/${key}@${zoom}x.png`
41 for (const zoom of new Set([Math.max(1, request.scale), 1])) {
42 if (await io.exists(pathAt(zoom))) return this.#rendered(io, pathAt(zoom), zoom)
43 }
44
45 // A missing renderer hands the block to the next, and so does a failing
46 // one, since mmdc runs mermaid.js itself and accepts what mmdr rejects.
47 // The error shown is the first, from the renderer preferred. With none
48 // installed the block stays code, as it reads well enough as text.
49 const exec = execWith(io)
50 let firstError: { error: string } | undefined
51 for (const renderer of this.#registry.chain(request.lang)) {
52 try {
53 const outcome = await this.#tryRenderer(io, exec, renderer, request, dir, pathAt)
54 if (!('error' in outcome)) return outcome
55 firstError ??= outcome
56 } catch (error) {
57 if (!(error instanceof MissingCommand)) throw error
58 }
59 }
60 return firstError ?? { skipped: true }
61 }
62
63 async #tryRenderer(
64 io: Io,
65 exec: Exec,
66 renderer: Renderer,
67 request: CodeRequest,
68 dir: string,
69 pathAt: (zoom: number) => string,
70 ): Promise<Outcome> {
71 const ctx = { io, exec, dir, outPath: pathAt, canRasterizeSvg: this.#canRasterizeSvg }
72 const artifact: Artifact | { error: string } = await renderer.render(request, ctx)
73 if ('error' in artifact) return artifact
74 if (artifact.kind === 'png') return this.#rendered(io, artifact.path, artifact.zoom)
75
76 const zoom = Math.max(1, request.scale)
77 try {
78 this.#sansSerif ??= sansSerifFamily(exec)
79 const failed = await rasterizeSvg(exec, artifact.svg, pathAt(zoom), zoom, await this.#sansSerif)
80 return failed ?? this.#rendered(io, pathAt(zoom), zoom)
81 } catch (error) {
82 if (!(error instanceof MissingCommand) || !this.#canRasterizeSvg) throw error
83 // The renderer may have a way without resvg (mmdr writes PNG itself).
84 this.#canRasterizeSvg = false
85 return this.#tryRenderer(io, exec, renderer, request, dir, pathAt)
86 }
87 }
88
89 // resvg, mmdr and base64 -d do not create the directory they write to;
90 // io.write does.
91 #cacheDir(io: Io): Promise<string> {
92 this.#dir ??= cacheDir(io).then(async (dir) => {
93 await io.write(`${dir}/.keep`, '')
94 return dir
95 })
96 return this.#dir
97 }
98
99 async #rendered(io: Io, path: string, zoom: number): Promise<Rendered> {
100 const size = await readPngSize(io, path)
101 return { path, width: size.width / zoom, height: size.height / zoom }
102 }
103
104 // io.write takes text only, so the picture's bytes reach disk through
105 // base64 -d.
106 async #materialize(io: Io, image: FoundImage): Promise<Outcome> {
107 const exec = execWith(io)
108 const dir = await this.#cacheDir(io)
109 const key = cacheKey(image.base64)
110 const path = `${dir}/img-${key}.png`
111 if (!(await io.exists(path))) {
112 const isPng = image.mime === 'image/png'
113 const raw = isPng ? path : `${dir}/img-${key}.${image.mime.slice('image/'.length)}`
114 const decoded = await exec(['sh', '-c', 'base64 -d > "$1"', 'sh', raw], image.base64)
115 if (decoded.exitCode !== 0) return failure('base64', decoded)
116 if (!isPng) {
117 const failed = await convertToPng(exec, raw, path)
118 await exec(['rm', '-f', raw]).catch(() => undefined)
119 if (failed) return failed
120 }
121 }
122 return { path, ...(await readPngHeader(exec, path)) }
123 }
124}
125hooks/renderers/index.ts 42 lines1import type { Catalog } from '../detect/markdown.ts'
2import { d2 } from './d2.ts'
3import { graphviz } from './graphviz.ts'
4import { mathjax } from './mathjax.ts'
5import { mmdc } from './mmdc.ts'
6import { mmdr } from './mmdr.ts'
7import { svg } from './svg.ts'
8import type { Renderer } from './types.ts'
9
10/** Every renderer, each lang's in default preference order. */
11export const RENDERERS: readonly Renderer[] = [mmdr, mmdc, graphviz, d2, svg, mathjax]
12
13export type Registry = Catalog & {
14 chain(lang: string): readonly Renderer[]
15 /** Ids in the `renderers` option that no renderer has. */
16 unknown: readonly string[]
17}
18
19// A lang with any of its renderers in `order` uses only those, in that order;
20// the others keep the default order. Ids are unique across langs, so the
21// option needs no lang prefix.
22export function createRegistry(order: readonly string[], all: readonly Renderer[] = RENDERERS): Registry {
23 const langOfTag = new Map<string, string>()
24 const chains = new Map<string, Renderer[]>()
25 for (const renderer of all) {
26 const [lang] = renderer.langs
27 for (const tag of renderer.langs) langOfTag.set(tag, lang)
28 chains.set(lang, [...(chains.get(lang) ?? []), renderer])
29 }
30 for (const [lang, chain] of chains) {
31 const chosen = order.flatMap((id) => chain.filter((r) => r.id === id))
32 if (chosen.length > 0) chains.set(lang, chosen)
33 }
34 const ids = new Set(all.map((r) => r.id))
35 return {
36 langOf: (tag) => langOfTag.get(tag),
37 accepts: (lang, source) => (chains.get(lang) ?? []).some((r) => r.accepts?.(source) ?? true),
38 chain: (lang) => chains.get(lang) ?? [],
39 unknown: order.filter((id) => !ids.has(id)),
40 }
41}
42hooks/ui/inline.tsx 58 lines1import { type Figures, type ToolCall, blocksOf, renderBlocks, renderImages } from '../figures.ts'
2import type { Io } from '../io.ts'
3import type { Viewport } from '../layout.ts'
4import type { Outcome } from '../pipeline/index.ts'
5import { type TerminalElements, pictures } from './pictures.tsx'
6import { fullSize, thumbnailSize } from './placements.ts'
7
8// Each returns the pictures to draw under the engine's own row, or undefined
9// to leave the row alone.
10
11export async function replyPictures(io: Io, figures: Figures, el: TerminalElements, text: string, viewport: Viewport) {
12 const blocks = blocksOf(figures, text)
13 if (blocks.length === 0) return undefined
14 const outcomes = await renderBlocks(io, figures, blocks)
15 return pictures(
16 el,
17 outcomes,
18 fullSize(figures.config),
19 viewport,
20 blocks.map((b) => b.lang),
21 )
22}
23
24export async function toolUsePictures(
25 io: Io,
26 figures: Figures,
27 el: TerminalElements,
28 call: ToolCall,
29 viewport: Viewport,
30) {
31 if (!figures.config.toolImages) return undefined
32 const outcomes = await renderImages(io, figures, call)
33 if (outcomes.length === 0) return undefined
34 return pictures(el, outcomes, fullSize(figures.config), viewport, call.tool)
35}
36
37// A collapsed group draws one count line and no ToolUse rows, so its
38// pictures are drawn under that line; an expanded one reaches ToolUse.
39export async function toolGroupPictures(
40 io: Io,
41 figures: Figures,
42 el: TerminalElements,
43 calls: readonly { tool_use_id?: string; tool: string; isRunning: boolean; output?: unknown }[],
44 viewport: Viewport,
45) {
46 if (!figures.config.toolImages) return undefined
47 const perCall = await Promise.all(
48 calls.map((call) =>
49 call.tool_use_id === undefined || call.isRunning || call.output === undefined
50 ? Promise.resolve([] as Outcome[])
51 : renderImages(io, figures, { tool_use_id: call.tool_use_id, tool: call.tool, output: call.output }),
52 ),
53 )
54 const outcomes = perCall.flat()
55 if (outcomes.length === 0) return undefined
56 return pictures(el, outcomes, thumbnailSize(figures.config), viewport, 'image')
57}
58hooks/ui/pane.tsx 165 lines1import type { Elements } from 'claude-code'
2
3import type { GalleryItem } from '../../types'
4import { type Figures, blocksOf, renderBlocks, renderImages } from '../figures.ts'
5import type { Io } from '../io.ts'
6import { LEGIBLE_SCALE, pane, type Viewport } from '../layout.ts'
7import { MissingCommand, execWith } from '../pipeline/exec.ts'
8import type { Outcome } from '../pipeline/index.ts'
9import { fullSize, thumbnailSize } from './placements.ts'
10
11export const PANE = 'figures'
12const GALLERY_SIZE = 30
13
14/** The pane's chosen picture, held in $.state by register.tsx. */
15export type Selection = { index: number | null; set(index: number): void }
16
17export type PaneProps = {
18 bodyColumns: number
19 placement: string
20 scroll: { bodyRows: number }
21}
22
23// Render hooks may not write state, so the gallery is rebuilt from the
24// transcript; the memos make that a lookup for anything drawn.
25export async function gallery(io: Io, figures: Figures): Promise<GalleryItem[]> {
26 const items = (
27 outcomes: Outcome[],
28 kind: GalleryItem['kind'],
29 label: (i: number) => string,
30 origin: (i: number) => GalleryItem['origin'],
31 ): GalleryItem[] =>
32 outcomes.flatMap((outcome, i) =>
33 'path' in outcome
34 ? [
35 {
36 path: outcome.path,
37 width: outcome.width,
38 height: outcome.height,
39 label: label(i),
40 kind,
41 origin: origin(i),
42 },
43 ]
44 : [],
45 )
46 const perMessage = await Promise.all(
47 (await io.messages()).map(async (message) => {
48 if (message.role !== 'assistant') return []
49 const blocks = blocksOf(figures, message.text)
50 const diagrams =
51 blocks.length > 0
52 ? items(
53 await renderBlocks(io, figures, blocks),
54 'diagram',
55 (i) => blocks[i]?.lang ?? 'diagram',
56 (i) => ({ source: blocks[i]?.source ?? '' }),
57 )
58 : []
59 if (!figures.config.toolImages) return diagrams
60 const tools = await Promise.all(
61 message.toolUses
62 .filter((use) => use.result !== undefined && !use.isError)
63 .map(async (use) =>
64 items(
65 await renderImages(io, figures, { tool_use_id: use.tool_use_id, tool: use.tool, output: use.result }),
66 'tool',
67 () => use.tool,
68 () => ({ toolUseId: use.tool_use_id }),
69 ),
70 ),
71 )
72 return [...diagrams, ...tools.flat()]
73 }),
74 )
75 const seen = new Set<string>()
76 return perMessage
77 .flat()
78 .filter((item) => !seen.has(item.path) && seen.add(item.path))
79 .slice(-GALLERY_SIZE)
80}
81
82const squeeze = (text: string) => text.replace(/\s+/g, ' ').trim()
83
84/**
85 * The newest picture a mouse selection points at: a tool image by the row the
86 * selection lies in, a diagram by source text selected within its block.
87 */
88export function selectedIn(
89 list: GalleryItem[],
90 selection: { text: string; requestId?: string },
91 groups: ReadonlyMap<string, readonly string[]>,
92): number | undefined {
93 const text = squeeze(selection.text)
94 const row = selection.requestId
95 const calls = row === undefined ? [] : (groups.get(row) ?? [row])
96 const index = list.findLastIndex(({ origin }) =>
97 'toolUseId' in origin ? calls.includes(origin.toolUseId) : text !== '' && squeeze(origin.source).includes(text),
98 )
99 return index === -1 ? undefined : index
100}
101
102async function openExternally(io: Io, path: string): Promise<void> {
103 const exec = execWith(io)
104 for (const opener of ['open', 'xdg-open']) {
105 try {
106 await exec([opener, path])
107 return
108 } catch (error) {
109 if (!(error instanceof MissingCommand)) throw error
110 }
111 }
112 io.toast('Neither open nor xdg-open is on PATH')
113}
114
115type PaneElements = Pick<Elements['terminal'], 'Box' | 'Button' | 'Image' | 'Text'>
116
117export async function drawPane(
118 io: Io,
119 figures: Figures,
120 { Box, Button, Image, Text }: PaneElements,
121 props: PaneProps,
122 viewport: Viewport,
123 selection: Selection,
124) {
125 const { config } = figures
126 const list = await gallery(io, figures)
127 if (list.length === 0) return <Text dimColor>No diagrams or images in this session yet.</Text>
128
129 // Opened without a choice, the pane shows the newest picture the
130 // transcript could not draw legibly, which is what /figures is for.
131 const illegible = list.findLastIndex(
132 (item) =>
133 (item.kind === 'tool' ? thumbnailSize(config) : fullSize(config)).fit(item, viewport).scale < LEGIBLE_SCALE,
134 )
135 const fallback = illegible === -1 ? list.length - 1 : illegible
136 const index = selection.index === null ? fallback : Math.min(Math.max(selection.index, 0), list.length - 1)
137 const item = list[index]!
138
139 // An inline pane grows to its content, so its bodyRows is the height drawn
140 // last rather than room to fill, and the layout clips what does not fit.
141 // Only the dock's is fixed; inline, the prompt and its chrome (about 14
142 // rows with the frame) stay below. One row goes to the header.
143 const bodyRows = props.placement === 'dock' ? Math.max(1, props.scroll.bodyRows - 1) : Math.max(4, viewport.rows - 14)
144 const { columns, rows } = pane(item, config, { columns: props.bodyColumns, rows: bodyRows })
145 return (
146 <Box flexDirection="column">
147 <Box>
148 <Button key="prev" label="Prev" hotkey="p" onPress={() => selection.set(Math.max(0, index - 1))} />
149 <Button
150 key="next"
151 label="Next"
152 hotkey="n"
153 onPress={() => selection.set(Math.min(list.length - 1, index + 1))}
154 />
155 <Button key="open" label="Open" hotkey="o" onPress={() => openExternally(io, item.path)} />
156 <Text dimColor>
157 {' '}
158 {index + 1}/{list.length} {item.label}
159 </Text>
160 </Box>
161 <Image source={{ file: item.path, format: 'png' }} columns={columns} rows={rows} alt={`[${item.label}]`} />
162 </Box>
163 )
164}
165hooks/ui/pictures.tsx 59 lines1import type { Elements } from 'claude-code'
2
3import { type Fit, LEGIBLE_SCALE, type Size, type Viewport } from '../layout.ts'
4import type { Outcome } from '../pipeline/index.ts'
5
6export type TerminalElements = Pick<Elements['terminal'], 'Box' | 'Image' | 'Text'>
7
8export type Placement = {
9 fit(natural: Size, viewport: Viewport): Fit
10 /** Shown under a picture drawn below LEGIBLE_SCALE. */
11 enlarge: string
12}
13
14export const INDENT = 2
15
16export function pictures(
17 { Box, Image, Text }: TerminalElements,
18 outcomes: Outcome[],
19 placement: Placement,
20 viewport: Viewport,
21 labels: string | readonly string[],
22) {
23 const room = { columns: viewport.columns - INDENT, rows: viewport.rows }
24 return outcomes.map((outcome, i) => {
25 const label = typeof labels === 'string' ? labels : (labels[i] ?? 'image')
26 if ('skipped' in outcome) return null
27 if ('error' in outcome) {
28 return (
29 <Box key={`${label}-${i}`} marginLeft={INDENT}>
30 <Text color="error">
31 {label}: {outcome.error.split('\n')[0]}
32 </Text>
33 </Box>
34 )
35 }
36 const { columns, rows, scale } = placement.fit(outcome, room)
37 return (
38 <Box key={`${label}-${i}`} marginLeft={INDENT} marginTop={1} flexDirection="column">
39 <Image
40 source={{ file: outcome.path, format: 'png' }}
41 columns={columns}
42 rows={rows}
43 alt={`[${label} ${i + 1}]`}
44 />
45 {scale < LEGIBLE_SCALE ? (
46 <Text dimColor>
47 shown at {Math.round(scale * 100)}% · {placement.enlarge}
48 </Text>
49 ) : null}
50 </Box>
51 )
52 })
53}
54
55export const viewportOf = (e: { viewport?: { columns: number; rows: number } }): Viewport => ({
56 columns: e.viewport?.columns ?? 80,
57 rows: e.viewport?.rows ?? 40,
58})
59hooks/detect/markdown.ts 35 lines1export type Block = { lang: string; source: string }
2
3/** What the renderers know: which fence tags they draw, and which sources they take. */
4export type Catalog = {
5 langOf(tag: string): string | undefined
6 accepts(lang: string, source: string): boolean
7}
8
9const FENCE = /^```([^\n`]*)\n([\s\S]*?)^```[ \t]*$/gm
10const DISPLAY_MATH = /\$\$([\s\S]+?)\$\$|\\\[([\s\S]+?)\\\]/g
11
12// Display math is looked for only between fences, so a `$$` in a shell
13// snippet is left alone; inline `$x$` is not drawn, since a picture cannot
14// sit inside a line of text.
15export function blocksIn(text: string, catalog: Catalog): Block[] {
16 const found: Block[] = []
17 const add = (lang: string | undefined, source: string) => {
18 if (lang !== undefined && catalog.accepts(lang, source)) found.push({ lang, source })
19 }
20 const prose = (from: number, to: number) => {
21 for (const m of text.slice(from, to).matchAll(DISPLAY_MATH)) {
22 const source = (m[1] ?? m[2] ?? '').trim()
23 if (source) add(catalog.langOf('math'), source)
24 }
25 }
26 let end = 0
27 for (const m of text.matchAll(FENCE)) {
28 prose(end, m.index)
29 end = m.index + m[0].length
30 add(catalog.langOf(m[1]?.trim().toLowerCase() ?? ''), m[2] ?? '')
31 }
32 prose(end, text.length)
33 return found
34}
35hooks/detect/tool-output.ts 75 lines1export type FoundImage = { base64: string; mime: string }
2
3const IMAGE_MIME = /^image\/(png|jpeg|gif|webp)$/
4export const SVG_MIME = 'image/svg+xml'
5
6// Tool results carry pictures in a few shapes: Read's `{ type: 'image', file:
7// { base64, type } }`, MCP content blocks `{ type: 'image', data, mimeType }`,
8// and API blocks `{ type: 'image', source: { data, media_type } }`.
9export function imagesIn(output: unknown, limit = 4): FoundImage[] {
10 const found: FoundImage[] = []
11 const visit = (value: unknown, depth: number) => {
12 if (found.length >= limit || depth > 6 || value === null || typeof value !== 'object') return
13 if (Array.isArray(value)) {
14 for (const item of value) visit(item, depth + 1)
15 return
16 }
17 const record = value as Record<string, unknown>
18 if (record.type === 'image') {
19 const file = record.file as Record<string, unknown> | undefined
20 const source = record.source as Record<string, unknown> | undefined
21 const base64 = file?.base64 ?? record.data ?? source?.data
22 const mime = file?.type ?? record.mimeType ?? source?.media_type
23 if (typeof base64 === 'string' && typeof mime === 'string' && IMAGE_MIME.test(mime)) {
24 found.push({ base64, mime })
25 return
26 }
27 }
28 for (const child of Object.values(record)) visit(child, depth + 1)
29 }
30 visit(output, 0)
31 return found
32}
33
34export type SentPath = { path: string; mime: string }
35
36const MIME_BY_EXTENSION: Record<string, string> = {
37 png: 'image/png',
38 jpg: 'image/jpeg',
39 jpeg: 'image/jpeg',
40 gif: 'image/gif',
41 webp: 'image/webp',
42 svg: SVG_MIME,
43}
44
45// SendUserFile returns where its files are, `{ attachments: [{ path, isImage,
46// media_type }] }`, not their bytes. `isImage` is false for an SVG, which the
47// svg renderer still draws, so the media type decides.
48export function sentFilesIn(tool: string, output: unknown, limit = 4): SentPath[] {
49 if (tool !== 'SendUserFile' || output === null || typeof output !== 'object') return []
50 const attachments = (output as { attachments?: unknown }).attachments
51 if (!Array.isArray(attachments)) return []
52 const found: SentPath[] = []
53 for (const attachment of attachments) {
54 if (found.length >= limit) break
55 if (attachment === null || typeof attachment !== 'object') continue
56 const { path, media_type } = attachment as Record<string, unknown>
57 if (typeof path !== 'string') continue
58 const mime =
59 typeof media_type === 'string' ? media_type : MIME_BY_EXTENSION[path.split('.').pop()?.toLowerCase() ?? '']
60 if (mime !== undefined && (IMAGE_MIME.test(mime) || mime === SVG_MIME)) found.push({ path, mime })
61 }
62 return found
63}
64
65// Read returns an SVG as text, `{ type: 'text', file: { filePath, content,
66// startLine, numLines, totalLines } }`. A partial read is not a picture.
67export function readSvgIn(tool: string, output: unknown): string | undefined {
68 if (tool !== 'Read' || output === null || typeof output !== 'object') return undefined
69 const { type, file } = output as { type?: unknown; file?: Record<string, unknown> }
70 if (type !== 'text' || file === undefined || typeof file.filePath !== 'string') return undefined
71 if (!file.filePath.toLowerCase().endsWith('.svg') || typeof file.content !== 'string') return undefined
72 const isWhole = file.startLine === 1 && file.numLines === file.totalLines && file.truncatedByTokenCap !== true
73 return isWhole ? file.content : undefined
74}
75