diff --git a/packages/agent/src/harness/tools/write.ts b/packages/agent/src/harness/tools/write.ts new file mode 100644 index 0000000000000000000000000000000000000000..9e0ce1ddde41a538f0211e87ab76d9be5180d451 --- /dev/null +++ b/packages/agent/src/harness/tools/write.ts @@ -0,0 +1,44 @@ +import { type Static, Type } from "typebox"; +import type { AgentHarnessTool } from "../types.ts"; +import { getOrThrow } from "../types.ts"; +import { withFileMutationQueue } from "./file-mutation-queue.ts"; +import { resolveToolPath } from "./path-utils.ts"; +import type { ExecutionToolContext } from "./tool-context.ts"; + +const writeSchema = Type.Object({ + path: Type.String({ description: "Path to the file to write (relative or absolute)" }), + content: Type.String({ description: "Content to write to the file" }), +}); + +export type WriteToolInput = Static; + +export function createWriteTool(): AgentHarnessTool< + TContext, + typeof writeSchema, + undefined +> { + return { + name: "write", + label: "write", + description: + "Write content to a file. Creates the file if it doesn't exist, overwrites if it does. Automatically creates parent directories.", + parameters: writeSchema, + async execute(_toolCallId, { path, content }, _onUpdate, { env }, _invocation, context) { + const absolutePath = await resolveToolPath(env, path, context); + return withFileMutationQueue( + env, + absolutePath, + async () => { + if (context.abortSignal?.aborted) throw new Error("Operation aborted"); + getOrThrow(await env.writeFile(absolutePath, content, context)); + if (context.abortSignal?.aborted) throw new Error("Operation aborted"); + return { + content: [{ type: "text", text: `Successfully wrote to ${path}` }], + details: undefined, + }; + }, + context, + ); + }, + }; +} diff --git a/packages/coding-agent/docs/tui.md b/packages/coding-agent/docs/tui.md new file mode 100644 index 0000000000000000000000000000000000000000..b1e737325480b309988470b26eb333dfa952ed96 --- /dev/null +++ b/packages/coding-agent/docs/tui.md @@ -0,0 +1,961 @@ +> pi can create TUI components. Ask it to build one for your use case. + +# TUI Components + +Extensions and custom tools can render custom TUI components for interactive user interfaces. This page covers the component system and available building blocks. + +**Source:** [`@earendil-works/pi-tui`](https://github.com/earendil-works/pi/tree/main/packages/tui) + +## Component Interface + +All components implement: + +```typescript +interface Component { + render(width: number): string[]; + handleInput?(data: string): void; + handleMouse?(event: TuiMouseEvent): TuiMouseEventResult | undefined; + wantsKeyRelease?: boolean; + invalidate(): void; +} +``` + +| Method | Description | +|--------|-------------| +| `render(width)` | Return array of strings (one per line). Each line **must not exceed `width`**. | +| `handleInput?(data)` | Receive keyboard input when component has focus. | +| `handleMouse?(event)` | Receive normalized pointer input in fullscreen mode. | +| `wantsKeyRelease?` | If true, component receives key release events (Kitty protocol). Default: false. | +| `invalidate()` | Clear cached render state. Called on theme changes. | + +The TUI appends a full SGR reset and OSC 8 reset at the end of each rendered line. Styles do not carry across lines. If you emit multi-line text with styling, reapply styles per line or use `wrapTextWithAnsi()` so styles are preserved for each wrapped line. + +## Focusable Interface (IME Support) + +Components that display a text cursor and need IME (Input Method Editor) support should implement the `Focusable` interface: + +```typescript +import { CURSOR_MARKER, type Component, type Focusable } from "@earendil-works/pi-tui"; + +class MyInput implements Component, Focusable { + focused: boolean = false; // Set by TUI when focus changes + + render(width: number): string[] { + const marker = this.focused ? CURSOR_MARKER : ""; + // Emit marker right before the fake cursor + return [`> ${beforeCursor}${marker}\x1b[7m${atCursor}\x1b[27m${afterCursor}`]; + } +} +``` + +When a `Focusable` component has focus, TUI: +1. Sets `focused = true` on the component +2. Scans rendered output for `CURSOR_MARKER` (a zero-width APC escape sequence) +3. Positions the hardware terminal cursor at that location +4. Shows the hardware cursor only when `showHardwareCursor` is enabled + +The cursor remains hidden by default. This keeps the fake cursor rendering, while still positioning the hardware cursor for terminals that track IME candidate windows with hidden cursors. Some terminals require a visible hardware cursor for IME positioning; enable it with the renderer's `showHardwareCursor` constructor argument or `setShowHardwareCursor(true)`. Pi also maps `PI_HARDWARE_CURSOR=1` to this setting before it creates its renderer. The `Editor` and `Input` built-in components already implement this interface. + +### Container Components with Embedded Inputs + +When a container component (dialog, selector, etc.) contains an `Input` or `Editor` child, the container must implement `Focusable` and propagate the focus state to the child. Otherwise, the hardware cursor won't be positioned correctly for IME input. + +```typescript +import { Container, type Focusable, Input } from "@earendil-works/pi-tui"; + +class SearchDialog extends Container implements Focusable { + private searchInput: Input; + + // Focusable implementation - propagate to child input for IME cursor positioning + private _focused = false; + get focused(): boolean { + return this._focused; + } + set focused(value: boolean) { + this._focused = value; + this.searchInput.focused = value; + } + + constructor() { + super(); + this.searchInput = new Input(); + this.addChild(this.searchInput); + } +} +``` + +Without this propagation, typing with an IME (Chinese, Japanese, Korean, etc.) will show the candidate window in the wrong position on screen. + +## Using Components + +**In extensions** via `ctx.ui.custom()`: + +```typescript +pi.on("session_start", async (_event, ctx) => { + const result = await ctx.ui.custom((tui, theme, keybindings, done) => + new MyComponent({ + theme, + keybindings, + onChange: () => tui.requestRender(), + onSelect: (value) => done(value), + onCancel: () => done(null), + }) + ); +}); +``` + +**In custom tools** via `ctx.ui.custom()`: + +```typescript +async execute(toolCallId, params, signal, onUpdate, ctx) { + const result = await ctx.ui.custom((tui, theme, keybindings, done) => + new MyComponent({ + theme, + keybindings, + onChange: () => tui.requestRender(), + onSelect: (value) => done(value), + onCancel: () => done(null), + }) + ); + // Use result... +} +``` + +## Overlays + +Overlays render components on top of existing content without clearing the screen. Pass `{ overlay: true }` to `ctx.ui.custom()`: + +```typescript +const result = await ctx.ui.custom( + (tui, theme, keybindings, done) => new MyDialog({ onClose: done }), + { overlay: true } +); +``` + +For positioning and sizing, use `overlayOptions`: + +```typescript +const result = await ctx.ui.custom( + (tui, theme, keybindings, done) => new SidePanel({ onClose: done }), + { + overlay: true, + overlayOptions: { + // Size: number or percentage string + width: "50%", // 50% of terminal width + minWidth: 40, // minimum 40 columns + maxHeight: "80%", // max 80% of terminal height + + // Position: anchor-based (default: "center") + anchor: "right-center", // 9 positions: center, top-left, top-center, etc. + offsetX: -2, // offset from anchor + offsetY: 0, + + // Or percentage/absolute positioning + row: "25%", // 25% from top + col: 10, // column 10 + + // Margins + margin: 2, // all sides, or { top, right, bottom, left } + + // Responsive: hide on narrow terminals + visible: (termWidth, termHeight) => termWidth >= 80, + }, + // Get handle for programmatic focus and visibility control + onHandle: (handle) => { + // handle.focus() - focus this overlay and bring it to the visual front + // handle.unfocus() - release input to normal fallback + // handle.unfocus({ target }) - release input to a specific component or null + // handle.setHidden(true/false) - toggle visibility + // handle.hide() - permanently remove + }, + } +); +``` + +### Overlay Focus + +A focused visible overlay keeps input ownership across temporary non-overlay UI. If an overlay opens another `ctx.ui.custom()` component without `{ overlay: true }`, that replacement UI receives input while it is active; when it closes, the focused overlay can reclaim input. + +Use `handle.unfocus()` when a visible overlay should stop owning input and let TUI fall back to another visible capturing overlay or the previous focus target. Use `handle.unfocus({ target })` when a specific component should receive input while the overlay stays visible. Passing `{ target: null }` intentionally leaves no focused component until focus is set again. + +### Overlay Lifecycle + +Overlay components are disposed when closed. Don't reuse references - create fresh instances: + +```typescript +// Wrong - stale reference +let menu: MenuComponent; +await ctx.ui.custom((_, __, ___, done) => { + menu = new MenuComponent(done); + return menu; +}, { overlay: true }); +setActiveComponent(menu); // Disposed + +// Correct - re-call to re-show +const showMenu = () => ctx.ui.custom((_, __, ___, done) => + new MenuComponent(done), { overlay: true }); + +await showMenu(); // First show +await showMenu(); // "Back" = just call again +``` + +See [overlay-qa-tests.ts](../examples/extensions/overlay-qa-tests.ts) for comprehensive examples covering anchors, margins, stacking, responsive visibility, and animation. + +## Built-in Components + +Import from `@earendil-works/pi-tui`: + +```typescript +import { Text, Box, Container, Spacer, Markdown } from "@earendil-works/pi-tui"; +``` + +### Text + +Multi-line text with word wrapping. + +```typescript +const text = new Text( + "Hello World", // content + 1, // paddingX (default: 1) + 1, // paddingY (default: 1) + (s) => bgGray(s) // optional background function +); +text.setText("Updated"); +``` + +### Box + +Container with padding and background color. + +```typescript +const box = new Box( + 1, // paddingX + 1, // paddingY + (s) => bgGray(s) // background function +); +box.addChild(new Text("Content", 0, 0)); +box.setBgFn((s) => bgBlue(s)); +``` + +### Container + +Groups child components vertically. + +```typescript +const container = new Container(); +container.addChild(component1); +container.addChild(component2); +container.removeChild(component1); +``` + +### Spacer + +Empty vertical space. + +```typescript +const spacer = new Spacer(2); // 2 empty lines +``` + +### Markdown + +Renders markdown with syntax highlighting. + +```typescript +const md = new Markdown( + "# Title\n\nSome **bold** text", + 1, // paddingX + 1, // paddingY + theme // MarkdownTheme (see below) +); +md.setText("Updated markdown"); +``` + +### Image + +Renders images in supported terminals (Kitty, iTerm2, Ghostty, WezTerm, Warp). + +```typescript +const image = new Image( + base64Data, // base64-encoded image + "image/png", // MIME type + theme, // ImageTheme + { maxWidthCells: 80, maxHeightCells: 24 } +); +``` + +## Keyboard Input + +Use `matchesKey()` for key detection: + +```typescript +import { matchesKey, Key } from "@earendil-works/pi-tui"; + +handleInput(data: string) { + if (matchesKey(data, Key.up)) { + this.selectedIndex--; + } else if (matchesKey(data, Key.enter)) { + this.onSelect?.(this.selectedIndex); + } else if (matchesKey(data, Key.escape)) { + this.onCancel?.(); + } else if (matchesKey(data, Key.ctrl("c"))) { + // Ctrl+C + } +} +``` + +**Key identifiers** (use `Key.*` for autocomplete, or string literals): +- Basic keys: `Key.enter`, `Key.escape`, `Key.tab`, `Key.space`, `Key.backspace`, `Key.delete`, `Key.home`, `Key.end` +- Arrow keys: `Key.up`, `Key.down`, `Key.left`, `Key.right` +- With modifiers: `Key.ctrl("c")`, `Key.shift("tab")`, `Key.alt("left")`, `Key.ctrlShift("p")` +- String format also works: `"enter"`, `"ctrl+c"`, `"shift+tab"`, `"ctrl+shift+p"` + +## Mouse Input + +Fullscreen mode routes normalized press, release, click, move, drag, and wheel events to components and overlays. Return `{ handled: true }` to suppress default behavior, `capture: true` to retain drag/release ownership, `focus: true` to request keyboard focus, and `render: true` when a hover or release visibly changes the component. Press, click, drag, and wheel render by default; no-op move/release events do not. + +```typescript +import { MouseRegion } from "@earendil-works/pi-tui"; + +const clickable = new MouseRegion(content, (event) => { + if (event.type !== "click" || event.button !== "left") return undefined; + expanded = !expanded; + return { handled: true }; +}); +``` + +Unhandled wheel input scrolls the nearest `ScrollView`; unhandled primary-button drags retain transcript selection. OSC 8 links take precedence over parent click regions. `Input`, `Editor`, `SelectList`, and `SettingsList` include fullscreen mouse behavior. Regular mode does not capture mouse input because the terminal owns its scrollback. + +## Line Width + +**Critical:** Each line from `render()` must not exceed the `width` parameter. + +```typescript +import { visibleWidth, truncateToWidth } from "@earendil-works/pi-tui"; + +render(width: number): string[] { + // Truncate long lines + return [truncateToWidth(this.text, width)]; +} +``` + +Utilities: +- `visibleWidth(str)` - Get display width (ignores ANSI codes) +- `truncateToWidth(str, width, ellipsis?)` - Truncate with optional ellipsis +- `wrapTextWithAnsi(str, width)` - Word wrap preserving ANSI codes + +## Creating Custom Components + +Example: Interactive selector + +```typescript +import { + matchesKey, Key, + truncateToWidth, visibleWidth +} from "@earendil-works/pi-tui"; + +class MySelector { + private items: string[]; + private selected = 0; + private cachedWidth?: number; + private cachedLines?: string[]; + + public onSelect?: (item: string) => void; + public onCancel?: () => void; + + constructor(items: string[]) { + this.items = items; + } + + handleInput(data: string): void { + if (matchesKey(data, Key.up) && this.selected > 0) { + this.selected--; + this.invalidate(); + } else if (matchesKey(data, Key.down) && this.selected < this.items.length - 1) { + this.selected++; + this.invalidate(); + } else if (matchesKey(data, Key.enter)) { + this.onSelect?.(this.items[this.selected]); + } else if (matchesKey(data, Key.escape)) { + this.onCancel?.(); + } + } + + render(width: number): string[] { + if (this.cachedLines && this.cachedWidth === width) { + return this.cachedLines; + } + + this.cachedLines = this.items.map((item, i) => { + const prefix = i === this.selected ? "> " : " "; + return truncateToWidth(prefix + item, width); + }); + this.cachedWidth = width; + return this.cachedLines; + } + + invalidate(): void { + this.cachedWidth = undefined; + this.cachedLines = undefined; + } +} +``` + +Usage in an extension: + +```typescript +pi.registerCommand("pick", { + description: "Pick an item", + handler: async (_args, ctx) => { + const items = ["Option A", "Option B", "Option C"]; + const selected = await ctx.ui.custom((tui, _theme, _keybindings, done) => { + const selector = new MySelector(items); + selector.onSelect = done; + selector.onCancel = () => done(null); + + return { + render: (width) => selector.render(width), + handleInput: (data) => { + selector.handleInput(data); + tui.requestRender(); + }, + invalidate: () => selector.invalidate(), + }; + }); + + if (selected !== null) { + ctx.ui.notify(`Selected: ${selected}`, "info"); + } + } +}); +``` + +## Theming + +Components accept theme objects for styling. + +**In `renderCall`/`renderResult`**, use the `theme` parameter: + +```typescript +renderResult(result, options, theme, context) { + // Use theme.fg() for foreground colors + return new Text(theme.fg("success", "Done!"), 0, 0); + + // Use theme.bg() for background colors + const styled = theme.bg("toolPendingBg", theme.fg("accent", "text")); +} +``` + +**Foreground colors** (`theme.fg(color, text)`): + +| Category | Colors | +|----------|--------| +| General | `text`, `accent`, `muted`, `dim`, `searchMatchText` | +| Status | `success`, `error`, `warning` | +| Borders | `border`, `borderAccent`, `borderMuted` | +| Messages | `userMessageText`, `customMessageText`, `customMessageLabel` | +| Tools | `toolTitle`, `toolOutput` | +| Diffs | `toolDiffAdded`, `toolDiffRemoved`, `toolDiffContext` | +| Markdown | `mdHeading`, `mdLink`, `mdLinkUrl`, `mdCode`, `mdCodeBlock`, `mdCodeBlockBorder`, `mdQuote`, `mdQuoteBorder`, `mdHr`, `mdListBullet` | +| Syntax | `syntaxComment`, `syntaxKeyword`, `syntaxFunction`, `syntaxVariable`, `syntaxString`, `syntaxNumber`, `syntaxType`, `syntaxOperator`, `syntaxPunctuation` | +| Thinking | `thinkingOff`, `thinkingMinimal`, `thinkingLow`, `thinkingMedium`, `thinkingHigh`, `thinkingXhigh`, `thinkingMax` | +| Modes | `bashMode` | + +**Background colors** (`theme.bg(color, text)`): + +`selectedBg`, `searchMatchBg`, `userMessageBg`, `customMessageBg`, `toolPendingBg`, `toolSuccessBg`, `toolErrorBg` + +**For Markdown**, use `getMarkdownTheme()`: + +```typescript +import { getMarkdownTheme } from "@earendil-works/pi-coding-agent"; +import { Markdown } from "@earendil-works/pi-tui"; + +renderResult(result, options, theme, context) { + const mdTheme = getMarkdownTheme(); + return new Markdown(result.details.markdown, 0, 0, mdTheme); +} +``` + +**For custom components**, define your own theme interface: + +```typescript +interface MyTheme { + selected: (s: string) => string; + normal: (s: string) => string; +} +``` + +## Debug logging + +Set `PI_TUI_WRITE_LOG` to capture the raw ANSI stream written to stdout. + +```bash +PI_TUI_WRITE_LOG=/tmp/tui-ansi.log npx tsx packages/tui/test/chat-simple.ts +``` + +## Performance + +Cache rendered output when possible: + +```typescript +class CachedComponent { + private cachedWidth?: number; + private cachedLines?: string[]; + + render(width: number): string[] { + if (this.cachedLines && this.cachedWidth === width) { + return this.cachedLines; + } + // ... compute lines ... + this.cachedWidth = width; + this.cachedLines = lines; + return lines; + } + + invalidate(): void { + this.cachedWidth = undefined; + this.cachedLines = undefined; + } +} +``` + +Call `invalidate()` when state changes, then use the injected `tui.requestRender()` to trigger re-render. + +## Invalidation and Theme Changes + +When the theme changes, the TUI calls `invalidate()` on all components to clear their caches. Components must properly implement `invalidate()` to ensure theme changes take effect. + +### The Problem + +If a component pre-bakes theme colors into strings (via `theme.fg()`, `theme.bg()`, etc.) and caches them, the cached strings contain ANSI escape codes from the old theme. Simply clearing the render cache isn't enough if the component stores the themed content separately. + +**Wrong approach** (theme colors won't update): + +```typescript +class BadComponent extends Container { + private content: Text; + + constructor(message: string, theme: Theme) { + super(); + // Pre-baked theme colors stored in Text component + this.content = new Text(theme.fg("accent", message), 1, 0); + this.addChild(this.content); + } + // No invalidate override - parent's invalidate only clears + // child render caches, not the pre-baked content +} +``` + +### The Solution + +Components that build content with theme colors must rebuild that content when `invalidate()` is called: + +```typescript +class GoodComponent extends Container { + private message: string; + private content: Text; + + constructor(message: string) { + super(); + this.message = message; + this.content = new Text("", 1, 0); + this.addChild(this.content); + this.updateDisplay(); + } + + private updateDisplay(): void { + // Rebuild content with current theme + this.content.setText(theme.fg("accent", this.message)); + } + + override invalidate(): void { + super.invalidate(); // Clear child caches + this.updateDisplay(); // Rebuild with new theme + } +} +``` + +### Pattern: Rebuild on Invalidate + +For components with complex content: + +```typescript +class ComplexComponent extends Container { + private data: SomeData; + + constructor(data: SomeData) { + super(); + this.data = data; + this.rebuild(); + } + + private rebuild(): void { + this.clear(); // Remove all children + + // Build UI with current theme + this.addChild(new Text(theme.fg("accent", theme.bold("Title")), 1, 0)); + this.addChild(new Spacer(1)); + + for (const item of this.data.items) { + const color = item.active ? "success" : "muted"; + this.addChild(new Text(theme.fg(color, item.label), 1, 0)); + } + } + + override invalidate(): void { + super.invalidate(); + this.rebuild(); + } +} +``` + +### When This Matters + +This pattern is needed when: + +1. **Pre-baking theme colors** - Using `theme.fg()` or `theme.bg()` to create styled strings stored in child components +2. **Syntax highlighting** - Using `highlightCode()` which applies theme-based syntax colors +3. **Complex layouts** - Building child component trees that embed theme colors + +This pattern is NOT needed when: + +1. **Using theme callbacks** - Passing functions like `(text) => theme.fg("accent", text)` that are called during render +2. **Simple containers** - Just grouping other components without adding themed content +3. **Stateless render** - Computing themed output fresh in every `render()` call (no caching) + +## Common Patterns + +These patterns cover the most common UI needs in extensions. **Copy these patterns instead of building from scratch.** + +### Pattern 1: Selection Dialog (SelectList) + +For letting users pick from a list of options. Use `SelectList` from `@earendil-works/pi-tui` with `DynamicBorder` for framing. + +```typescript +import type { ExtensionAPI } from "@earendil-works/pi-coding-agent"; +import { DynamicBorder } from "@earendil-works/pi-coding-agent"; +import { Container, type SelectItem, SelectList, Text } from "@earendil-works/pi-tui"; + +pi.registerCommand("pick", { + handler: async (_args, ctx) => { + const items: SelectItem[] = [ + { value: "opt1", label: "Option 1", description: "First option" }, + { value: "opt2", label: "Option 2", description: "Second option" }, + { value: "opt3", label: "Option 3" }, // description is optional + ]; + + const result = await ctx.ui.custom((tui, theme, _kb, done) => { + const container = new Container(); + + // Top border + container.addChild(new DynamicBorder((s: string) => theme.fg("accent", s))); + + // Title + container.addChild(new Text(theme.fg("accent", theme.bold("Pick an Option")), 1, 0)); + + // SelectList with theme + const selectList = new SelectList(items, Math.min(items.length, 10), { + selectedPrefix: (t) => theme.fg("accent", t), + selectedText: (t) => theme.fg("accent", t), + description: (t) => theme.fg("muted", t), + scrollInfo: (t) => theme.fg("dim", t), + noMatch: (t) => theme.fg("warning", t), + }); + selectList.onSelect = (item) => done(item.value); + selectList.onCancel = () => done(null); + container.addChild(selectList); + + // Help text + container.addChild(new Text(theme.fg("dim", "↑↓ navigate • enter select • esc cancel"), 1, 0)); + + // Bottom border + container.addChild(new DynamicBorder((s: string) => theme.fg("accent", s))); + + return { + render: (w) => container.render(w), + invalidate: () => container.invalidate(), + handleInput: (data) => { selectList.handleInput(data); tui.requestRender(); }, + }; + }); + + if (result) { + ctx.ui.notify(`Selected: ${result}`, "info"); + } + }, +}); +``` + +**Examples:** [preset.ts](../examples/extensions/preset.ts), [tools.ts](../examples/extensions/tools.ts) + +### Pattern 2: Async Operation with Cancel (BorderedLoader) + +For operations that take time and should be cancellable. `BorderedLoader` shows a spinner and handles escape to cancel. + +```typescript +import { BorderedLoader } from "@earendil-works/pi-coding-agent"; + +pi.registerCommand("fetch", { + handler: async (_args, ctx) => { + const result = await ctx.ui.custom((tui, theme, _kb, done) => { + const loader = new BorderedLoader(tui, theme, "Fetching data..."); + loader.onAbort = () => done(null); + + // Do async work + fetchData(loader.signal) + .then((data) => done(data)) + .catch(() => done(null)); + + return loader; + }); + + if (result === null) { + ctx.ui.notify("Cancelled", "info"); + } else { + ctx.ui.setEditorText(result); + } + }, +}); +``` + +**Examples:** [qna.ts](../examples/extensions/qna.ts), [handoff.ts](../examples/extensions/handoff.ts) + +### Pattern 3: Settings/Toggles (SettingsList) + +For toggling multiple settings. Use `SettingsList` from `@earendil-works/pi-tui` with `getSettingsListTheme()`. + +```typescript +import { getSettingsListTheme } from "@earendil-works/pi-coding-agent"; +import { Container, type SettingItem, SettingsList, Text } from "@earendil-works/pi-tui"; + +pi.registerCommand("settings", { + handler: async (_args, ctx) => { + const items: SettingItem[] = [ + { id: "verbose", label: "Verbose mode", currentValue: "off", values: ["on", "off"] }, + { id: "color", label: "Color output", currentValue: "on", values: ["on", "off"] }, + ]; + + await ctx.ui.custom((_tui, theme, _kb, done) => { + const container = new Container(); + container.addChild(new Text(theme.fg("accent", theme.bold("Settings")), 1, 1)); + + const settingsList = new SettingsList( + items, + Math.min(items.length + 2, 15), + getSettingsListTheme(), + (id, newValue) => { + // Handle value change + ctx.ui.notify(`${id} = ${newValue}`, "info"); + }, + () => done(undefined), // On close + { enableSearch: true }, // Optional: enable fuzzy search by label + ); + container.addChild(settingsList); + + return { + render: (w) => container.render(w), + invalidate: () => container.invalidate(), + handleInput: (data) => settingsList.handleInput?.(data), + }; + }); + }, +}); +``` + +**Examples:** [tools.ts](../examples/extensions/tools.ts) + +### Pattern 4: Persistent Status Indicator + +Show status in the footer that persists across renders. Good for mode indicators. + +```typescript +// Set status (shown in footer) +ctx.ui.setStatus("my-ext", ctx.ui.theme.fg("accent", "● active")); + +// Clear status +ctx.ui.setStatus("my-ext", undefined); +``` + +**Examples:** [status-line.ts](../examples/extensions/status-line.ts), [plan-mode/index.ts](../examples/extensions/plan-mode/index.ts), [preset.ts](../examples/extensions/preset.ts) + +### Pattern 4b: Working Indicator Customization + +Customize the inline working indicator shown while pi is streaming a response. + +```typescript +// Static indicator +ctx.ui.setWorkingIndicator({ frames: [ctx.ui.theme.fg("accent", "●")] }); + +// Custom animated indicator +ctx.ui.setWorkingIndicator({ + frames: [ + ctx.ui.theme.fg("dim", "·"), + ctx.ui.theme.fg("muted", "•"), + ctx.ui.theme.fg("accent", "●"), + ctx.ui.theme.fg("muted", "•"), + ], + intervalMs: 120, +}); + +// Hide the indicator entirely +ctx.ui.setWorkingIndicator({ frames: [] }); + +// Restore pi's default spinner +ctx.ui.setWorkingIndicator(); +``` + +This only affects the normal streaming working indicator. Compaction and retry loaders keep their built-in styling. Custom frames are rendered verbatim, so extensions must add their own colors when needed. + +**Examples:** [working-indicator.ts](../examples/extensions/working-indicator.ts) + +### Pattern 5: Widgets Above/Below Editor + +Show persistent content above or below the input editor. Good for todo lists, progress. + +```typescript +// Simple string array (above editor by default) +ctx.ui.setWidget("my-widget", ["Line 1", "Line 2"]); + +// Render below the editor +ctx.ui.setWidget("my-widget", ["Line 1", "Line 2"], { placement: "belowEditor" }); + +// Or with theme +ctx.ui.setWidget("my-widget", (_tui, theme) => { + const lines = items.map((item, i) => + item.done + ? theme.fg("success", "✓ ") + theme.fg("muted", item.text) + : theme.fg("dim", "○ ") + item.text + ); + return { + render: () => lines, + invalidate: () => {}, + }; +}); + +// Clear +ctx.ui.setWidget("my-widget", undefined); +``` + +**Examples:** [plan-mode/index.ts](../examples/extensions/plan-mode/index.ts) + +### Pattern 6: Custom Footer + +Replace the footer. `footerData` exposes data not otherwise accessible to extensions. + +```typescript +ctx.ui.setFooter((tui, theme, footerData) => ({ + invalidate() {}, + render(width: number): string[] { + // footerData.getGitBranch(): string | null + // footerData.getExtensionStatuses(): ReadonlyMap + return [`${ctx.model?.id} (${footerData.getGitBranch() || "no git"})`]; + }, + dispose: footerData.onBranchChange(() => tui.requestRender()), // reactive +})); + +ctx.ui.setFooter(undefined); // restore default +``` + +Token stats available via `ctx.sessionManager.getBranch()` and `ctx.model`. + +**Examples:** [custom-footer.ts](../examples/extensions/custom-footer.ts) + +### Pattern 7: Custom Editor (vim mode, etc.) + +Replace the main input editor with a custom implementation. Useful for modal editing (vim), different keybindings (emacs), or specialized input handling. + +```typescript +import { CustomEditor, type ExtensionAPI } from "@earendil-works/pi-coding-agent"; +import { matchesKey, truncateToWidth } from "@earendil-works/pi-tui"; + +type Mode = "normal" | "insert"; + +class VimEditor extends CustomEditor { + private mode: Mode = "insert"; + + handleInput(data: string): void { + // Escape: switch to normal mode, or pass through for app handling + if (matchesKey(data, "escape")) { + if (this.mode === "insert") { + this.mode = "normal"; + return; + } + // In normal mode, escape aborts agent (handled by CustomEditor) + super.handleInput(data); + return; + } + + // Insert mode: pass everything to CustomEditor + if (this.mode === "insert") { + super.handleInput(data); + return; + } + + // Normal mode: vim-style navigation + switch (data) { + case "i": this.mode = "insert"; return; + case "h": super.handleInput("\x1b[D"); return; // Left + case "j": super.handleInput("\x1b[B"); return; // Down + case "k": super.handleInput("\x1b[A"); return; // Up + case "l": super.handleInput("\x1b[C"); return; // Right + } + // Pass unhandled keys to super (ctrl+c, etc.), but filter printable chars + if (data.length === 1 && data.charCodeAt(0) >= 32) return; + super.handleInput(data); + } + + render(width: number): string[] { + const lines = super.render(width); + // Add mode indicator to bottom border (use truncateToWidth for ANSI-safe truncation) + if (lines.length > 0) { + const label = this.mode === "normal" ? " NORMAL " : " INSERT "; + const lastLine = lines[lines.length - 1]!; + // Pass "" as ellipsis to avoid adding "..." when truncating + lines[lines.length - 1] = truncateToWidth(lastLine, width - label.length, "") + label; + } + return lines; + } +} + +export default function (pi: ExtensionAPI) { + pi.on("session_start", (_event, ctx) => { + // Factory receives the TUI, theme, and keybindings from the app + ctx.ui.setEditorComponent((tui, theme, keybindings) => + new VimEditor(tui, theme, keybindings) + ); + }); +} +``` + +**Key points:** + +- **Extend `CustomEditor`** (not base `Editor`) to get app keybindings (escape to abort, ctrl+d to exit, model switching, etc.) +- **Call `super.handleInput(data)`** for keys you don't handle +- **Status spinners**: custom editors keep standalone status rows by default. Pass `{ embedWorkingStatus: true }` as the fourth `CustomEditor` constructor argument to embed working, compaction, branch summarization, and retry spinners in the editor border instead. +- **Factory pattern**: `setEditorComponent` receives a factory function that gets `tui`, `theme`, and `keybindings` +- **Pass `undefined`** to restore the default editor: `ctx.ui.setEditorComponent(undefined)` + +**Examples:** [modal-editor.ts](../examples/extensions/modal-editor.ts) + +## Key Rules + +1. **Always use theme from callback** - Don't import theme directly. Use `theme` from the `ctx.ui.custom((tui, theme, keybindings, done) => ...)` callback. + +2. **Always type DynamicBorder color param** - Write `(s: string) => theme.fg("accent", s)`, not `(s) => theme.fg("accent", s)`. + +3. **Call tui.requestRender() after state changes** - In `handleInput`, call `tui.requestRender()` after updating state. + +4. **Return the three-method object** - Custom components need `{ render, invalidate, handleInput }`. + +5. **Use existing components** - `SelectList`, `SettingsList`, `BorderedLoader` cover 90% of cases. Don't rebuild them. + +## Examples + +- **Selection UI**: [examples/extensions/preset.ts](../examples/extensions/preset.ts) - SelectList with DynamicBorder framing +- **Async with cancel**: [examples/extensions/qna.ts](../examples/extensions/qna.ts) - BorderedLoader for LLM calls +- **Settings toggles**: [examples/extensions/tools.ts](../examples/extensions/tools.ts) - SettingsList for tool enable/disable +- **Status indicators**: [examples/extensions/plan-mode/index.ts](../examples/extensions/plan-mode/index.ts) - setStatus and setWidget +- **Working indicator**: [examples/extensions/working-indicator.ts](../examples/extensions/working-indicator.ts) - setWorkingIndicator +- **Custom footer**: [examples/extensions/custom-footer.ts](../examples/extensions/custom-footer.ts) - setFooter with stats +- **Custom editor**: [examples/extensions/modal-editor.ts](../examples/extensions/modal-editor.ts) - Vim-like modal editing +- **Snake game**: [examples/extensions/snake.ts](../examples/extensions/snake.ts) - Full game with keyboard input, game loop +- **Custom tool rendering**: [examples/extensions/todo.ts](../examples/extensions/todo.ts) - renderCall and renderResult diff --git a/packages/coding-agent/install-lock/package-lock.json b/packages/coding-agent/install-lock/package-lock.json new file mode 100644 index 0000000000000000000000000000000000000000..7d0a6eace2d8322d5d2e7bfcd8062ad62b3d213e --- /dev/null +++ b/packages/coding-agent/install-lock/package-lock.json @@ -0,0 +1,1917 @@ +{ + "name": "@earendil-works/pi-coding-agent-install", + "version": "0.85.1", + "lockfileVersion": 3, + "requires": true, + "packages": { + "": { + "name": "@earendil-works/pi-coding-agent-install", + "version": "0.85.1", + "dependencies": { + "@earendil-works/pi-coding-agent": "0.85.1" + }, + "engines": { + "node": ">=22.19.0" + } + }, + "node_modules/@anthropic-ai/sdk": { + "version": "0.124.0", + "resolved": "https://registry.npmjs.org/@anthropic-ai/sdk/-/sdk-0.124.0.tgz", + "integrity": "sha512-cN5O8i9UVxHeOQAzj/XjshWXG8KiibJDw9OGpH2Z/eR3n/RBxdoLxDJOcfqAJWvjaMDFfHTBADU04hWRJVkDyA==", + "license": "MIT", + "dependencies": { + "json-schema-to-ts": "^3.1.1", + "standardwebhooks": "^1.0.0" + }, + "peerDependencies": { + "zod": "^3.25.0 || ^4.0.0" + }, + "peerDependenciesMeta": { + "zod": { + "optional": true + } + }, + "bin": { + "anthropic-ai-sdk": "bin/cli" + } + }, + "node_modules/@aws-sdk/client-bedrock-runtime": { + "version": "3.1127.0", + "resolved": "https://registry.npmjs.org/@aws-sdk/client-bedrock-runtime/-/client-bedrock-runtime-3.1127.0.tgz", + "integrity": "sha512-IDl/lrPb90aH+pZFHGNDmgH9nAUQj5PlZH1sJ3w7RikctyjHSnY3oNjZhrLoaBoQn/rNK0zsP6OHEqEhj2tdLA==", + "license": "Apache-2.0", + "dependencies": { + "@aws-sdk/core": "^3.977.9", + "@aws-sdk/credential-provider-node": "^3.972.82", + "@aws-sdk/eventstream-handler-node": "^3.972.34", + "@aws-sdk/middleware-eventstream": "^3.972.29", + "@aws-sdk/middleware-websocket": "^3.972.52", + "@aws-sdk/token-providers": "3.1127.0", + "@aws-sdk/types": "^3.974.5", + "@smithy/core": "^3.33.3", + "@smithy/fetch-http-handler": "^5.7.2", + "@smithy/node-http-handler": "^4.11.3", + "@smithy/types": "^4.17.2", + "tslib": "^2.6.2" + }, + "engines": { + "node": ">=20.0.0" + } + }, + "node_modules/@aws-sdk/core": { + "version": "3.977.9", + "resolved": "https://registry.npmjs.org/@aws-sdk/core/-/core-3.977.9.tgz", + "integrity": "sha512-reqPFEQrZxDZpeGj4PFMepBeR5LGYHRqq/L0motTzgFkCRBA4rFdaVXDSLYyGHhxVz7sT2PDnPN9CluGSfgyJA==", + "license": "Apache-2.0", + "dependencies": { + "@aws-sdk/types": "^3.974.5", + "@aws-sdk/xml-builder": "^3.972.40", + "@aws/lambda-invoke-store": "^0.3.0", + "@smithy/core": "^3.33.3", + "@smithy/signature-v4": "^5.6.12", + "@smithy/types": "^4.17.2", + "bowser": "^2.11.0", + "tslib": "^2.6.2" + }, + "engines": { + "node": ">=20.0.0" + } + }, + "node_modules/@aws-sdk/credential-provider-env": { + "version": "3.972.70", + "resolved": "https://registry.npmjs.org/@aws-sdk/credential-provider-env/-/credential-provider-env-3.972.70.tgz", + "integrity": "sha512-H404B7dJl2mCrBqahDEYsanB0xhdDp6tXnXcTUnXmmpy2Q3J0Ho0bUajZ2jr/RdwzCyS59Gi8xXIFwPLGBl6Uw==", + "license": "Apache-2.0", + "dependencies": { + "@aws-sdk/core": "^3.977.9", + "@aws-sdk/types": "^3.974.5", + "@smithy/core": "^3.33.3", + "@smithy/types": "^4.17.2", + "tslib": "^2.6.2" + }, + "engines": { + "node": ">=20.0.0" + } + }, + "node_modules/@aws-sdk/credential-provider-http": { + "version": "3.972.72", + "resolved": "https://registry.npmjs.org/@aws-sdk/credential-provider-http/-/credential-provider-http-3.972.72.tgz", + "integrity": "sha512-X98zYOrVOeuosCX+6ktf29FC2N2GHPLia7qv6mzPzTc+RPAuHWCDS++Z6JK7eGYqb/v6uaW7bAXaOvDBfol+0w==", + "license": "Apache-2.0", + "dependencies": { + "@aws-sdk/core": "^3.977.9", + "@aws-sdk/types": "^3.974.5", + "@smithy/core": "^3.33.3", + "@smithy/fetch-http-handler": "^5.7.2", + "@smithy/node-http-handler": "^4.11.3", + "@smithy/types": "^4.17.2", + "tslib": "^2.6.2" + }, + "engines": { + "node": ">=20.0.0" + } + }, + "node_modules/@aws-sdk/credential-provider-ini": { + "version": "3.973.15", + "resolved": "https://registry.npmjs.org/@aws-sdk/credential-provider-ini/-/credential-provider-ini-3.973.15.tgz", + "integrity": "sha512-Rykg6s5ceBuynMOGWgoowO4N+27JfnqXAnVaSunZl0hOO1XodSrxGNz6sCEbnmS0lAfQZDKyb3fbr46gSuv6Sg==", + "license": "Apache-2.0", + "dependencies": { + "@aws-sdk/core": "^3.977.9", + "@aws-sdk/credential-provider-env": "^3.972.70", + "@aws-sdk/credential-provider-http": "^3.972.72", + "@aws-sdk/credential-provider-login": "^3.972.77", + "@aws-sdk/credential-provider-process": "^3.972.70", + "@aws-sdk/credential-provider-sso": "^3.973.14", + "@aws-sdk/credential-provider-web-identity": "^3.972.76", + "@aws-sdk/nested-clients": "^3.997.44", + "@aws-sdk/types": "^3.974.5", + "@smithy/core": "^3.33.3", + "@smithy/credential-provider-imds": "^4.4.16", + "@smithy/types": "^4.17.2", + "tslib": "^2.6.2" + }, + "engines": { + "node": ">=20.0.0" + } + }, + "node_modules/@aws-sdk/credential-provider-login": { + "version": "3.972.77", + "resolved": "https://registry.npmjs.org/@aws-sdk/credential-provider-login/-/credential-provider-login-3.972.77.tgz", + "integrity": "sha512-Jb59xfEISoN5mmbnA+HYqdtrSX3CgCtJoof+V5D8/TgUI56W63GEEd5Y58WijU3Ou6+WEgaLD1feVzaRXV5IDQ==", + "license": "Apache-2.0", + "dependencies": { + "@aws-sdk/core": "^3.977.9", + "@aws-sdk/nested-clients": "^3.997.44", + "@aws-sdk/types": "^3.974.5", + "@smithy/core": "^3.33.3", + "@smithy/types": "^4.17.2", + "tslib": "^2.6.2" + }, + "engines": { + "node": ">=20.0.0" + } + }, + "node_modules/@aws-sdk/credential-provider-node": { + "version": "3.972.82", + "resolved": "https://registry.npmjs.org/@aws-sdk/credential-provider-node/-/credential-provider-node-3.972.82.tgz", + "integrity": "sha512-znDkEOGXB8W3kG1LJUKP3foBZY/9qLM0eil/DxWXSp37XsdsRLQHE/d/OaCGGVgKpA6znR38h/+INk8do1FjiA==", + "license": "Apache-2.0", + "dependencies": { + "@aws-sdk/credential-provider-env": "^3.972.70", + "@aws-sdk/credential-provider-http": "^3.972.72", + "@aws-sdk/credential-provider-ini": "^3.973.15", + "@aws-sdk/credential-provider-process": "^3.972.70", + "@aws-sdk/credential-provider-sso": "^3.973.14", + "@aws-sdk/credential-provider-web-identity": "^3.972.76", + "@aws-sdk/types": "^3.974.5", + "@smithy/core": "^3.33.3", + "@smithy/credential-provider-imds": "^4.4.16", + "@smithy/types": "^4.17.2", + "tslib": "^2.6.2" + }, + "engines": { + "node": ">=20.0.0" + } + }, + "node_modules/@aws-sdk/credential-provider-process": { + "version": "3.972.70", + "resolved": "https://registry.npmjs.org/@aws-sdk/credential-provider-process/-/credential-provider-process-3.972.70.tgz", + "integrity": "sha512-2ry03fGRJr4sV3jI+ocjj5JqALnFD6ymM5KiNCDZMvq8bX2GSbE0vji4aM43TVCl2nXqqLRZaUxdq/KeWRAY4Q==", + "license": "Apache-2.0", + "dependencies": { + "@aws-sdk/core": "^3.977.9", + "@aws-sdk/types": "^3.974.5", + "@smithy/core": "^3.33.3", + "@smithy/types": "^4.17.2", + "tslib": "^2.6.2" + }, + "engines": { + "node": ">=20.0.0" + } + }, + "node_modules/@aws-sdk/credential-provider-sso": { + "version": "3.973.14", + "resolved": "https://registry.npmjs.org/@aws-sdk/credential-provider-sso/-/credential-provider-sso-3.973.14.tgz", + "integrity": "sha512-jkhg/8ocAAoc0RFyLMhCw+/zZh7gystQgd4F4hznNa8P4Cc501PQmxd+jGLiMHodPJ+7Zv/3znM62gZojyasmA==", + "license": "Apache-2.0", + "dependencies": { + "@aws-sdk/core": "^3.977.9", + "@aws-sdk/nested-clients": "^3.997.44", + "@aws-sdk/token-providers": "3.1116.0", + "@aws-sdk/types": "^3.974.5", + "@smithy/core": "^3.33.3", + "@smithy/types": "^4.17.2", + "tslib": "^2.6.2" + }, + "engines": { + "node": ">=20.0.0" + } + }, + "node_modules/@aws-sdk/credential-provider-sso/node_modules/@aws-sdk/token-providers": { + "version": "3.1116.0", + "resolved": "https://registry.npmjs.org/@aws-sdk/token-providers/-/token-providers-3.1116.0.tgz", + "integrity": "sha512-ygIivKqh8aHzNkucOCXHyIBgBpLPfrSI0mCqXF+vLBsPTUKqj0VSqAY0GFPe7lQl4HntjOcQ+KSyS7oUV2C54Q==", + "license": "Apache-2.0", + "dependencies": { + "@aws-sdk/core": "^3.977.9", + "@aws-sdk/nested-clients": "^3.997.44", + "@aws-sdk/types": "^3.974.5", + "@smithy/core": "^3.33.3", + "@smithy/types": "^4.17.2", + "tslib": "^2.6.2" + }, + "engines": { + "node": ">=20.0.0" + } + }, + "node_modules/@aws-sdk/credential-provider-web-identity": { + "version": "3.972.76", + "resolved": "https://registry.npmjs.org/@aws-sdk/credential-provider-web-identity/-/credential-provider-web-identity-3.972.76.tgz", + "integrity": "sha512-d3AGyVu759PGr35mEB2s22xxlNEA5rpdxtSPJthfPFJvoQ8dt357iVPECqWfUxXp1toJAvKmbtcIYVGigaGsCA==", + "license": "Apache-2.0", + "dependencies": { + "@aws-sdk/core": "^3.977.9", + "@aws-sdk/nested-clients": "^3.997.44", + "@aws-sdk/types": "^3.974.5", + "@smithy/core": "^3.33.3", + "@smithy/types": "^4.17.2", + "tslib": "^2.6.2" + }, + "engines": { + "node": ">=20.0.0" + } + }, + "node_modules/@aws-sdk/eventstream-handler-node": { + "version": "3.972.34", + "resolved": "https://registry.npmjs.org/@aws-sdk/eventstream-handler-node/-/eventstream-handler-node-3.972.34.tgz", + "integrity": "sha512-cTeVzpu1xEAkryTZBYhGwnQ6gOGyp8ZYZvmn0Sg/nI/ABmy/CRHHxPDJDUi9PxwxUtGGaatvfRUB3FCgT/rSWw==", + "license": "Apache-2.0", + "dependencies": { + "@aws-sdk/types": "^3.974.5", + "@smithy/core": "^3.33.3", + "@smithy/types": "^4.17.2", + "tslib": "^2.6.2" + }, + "engines": { + "node": ">=20.0.0" + } + }, + "node_modules/@aws-sdk/middleware-eventstream": { + "version": "3.972.29", + "resolved": "https://registry.npmjs.org/@aws-sdk/middleware-eventstream/-/middleware-eventstream-3.972.29.tgz", + "integrity": "sha512-dlRzHCgyB8W6hLuDC5pcT5q+ziPt00n4QGgGBE17ucLVU4zMa6lsbuUdQ2Pm75Z5VA8GF+R/+SgrRcaTdIzSIQ==", + "license": "Apache-2.0", + "dependencies": { + "@aws-sdk/types": "^3.974.5", + "@smithy/core": "^3.33.3", + "@smithy/types": "^4.17.2", + "tslib": "^2.6.2" + }, + "engines": { + "node": ">=20.0.0" + } + }, + "node_modules/@aws-sdk/middleware-websocket": { + "version": "3.972.52", + "resolved": "https://registry.npmjs.org/@aws-sdk/middleware-websocket/-/middleware-websocket-3.972.52.tgz", + "integrity": "sha512-vsPPM+nMbKJlUCFU+eoGZbdxdxDIAX9LbpjSXaR5Ufpmqgp8TdYQnoExhLu4T3umW/JIIPny1ydbhWidZZYokQ==", + "license": "Apache-2.0", + "dependencies": { + "@aws-sdk/core": "^3.977.9", + "@aws-sdk/types": "^3.974.5", + "@smithy/core": "^3.33.3", + "@smithy/fetch-http-handler": "^5.7.2", + "@smithy/signature-v4": "^5.6.12", + "@smithy/types": "^4.17.2", + "tslib": "^2.6.2" + }, + "engines": { + "node": ">= 14.0.0" + } + }, + "node_modules/@aws-sdk/nested-clients": { + "version": "3.997.44", + "resolved": "https://registry.npmjs.org/@aws-sdk/nested-clients/-/nested-clients-3.997.44.tgz", + "integrity": "sha512-NhEgryjlBF9w38ZXqGymQV28IhkYa1mKhlbYnqIis57AYwWGVYfUPgg/qC2rLRqOUfblxx++irvju10kVTa8Vw==", + "license": "Apache-2.0", + "dependencies": { + "@aws-sdk/core": "^3.977.9", + "@aws-sdk/signature-v4-multi-region": "^3.996.46", + "@aws-sdk/types": "^3.974.5", + "@smithy/core": "^3.33.3", + "@smithy/fetch-http-handler": "^5.7.2", + "@smithy/node-http-handler": "^4.11.3", + "@smithy/types": "^4.17.2", + "tslib": "^2.6.2" + }, + "engines": { + "node": ">=20.0.0" + } + }, + "node_modules/@aws-sdk/signature-v4-multi-region": { + "version": "3.996.46", + "resolved": "https://registry.npmjs.org/@aws-sdk/signature-v4-multi-region/-/signature-v4-multi-region-3.996.46.tgz", + "integrity": "sha512-L+2xZTye/2T96f3lwCws0Zw6GG2JHZW9e8FpVgGBeeExSKyeoZ6CWRpBml/7DNiK/O26jrgPM9F+Ay8VkgzUWQ==", + "license": "Apache-2.0", + "dependencies": { + "@aws-sdk/types": "^3.974.5", + "@smithy/signature-v4": "^5.6.12", + "@smithy/types": "^4.17.2", + "tslib": "^2.6.2" + }, + "engines": { + "node": ">=20.0.0" + } + }, + "node_modules/@aws-sdk/token-providers": { + "version": "3.1127.0", + "resolved": "https://registry.npmjs.org/@aws-sdk/token-providers/-/token-providers-3.1127.0.tgz", + "integrity": "sha512-Dv2TMWBshJ+tF6ahs2Sy5bh4Iabsd4GAQqVvE9XZmYmnoaVbpS2QKIKE/HRacc7bTtbjEEvP+laGzHvHlf1CiQ==", + "license": "Apache-2.0", + "dependencies": { + "@aws-sdk/core": "^3.977.9", + "@aws-sdk/nested-clients": "^3.997.44", + "@aws-sdk/types": "^3.974.5", + "@smithy/core": "^3.33.3", + "@smithy/types": "^4.17.2", + "tslib": "^2.6.2" + }, + "engines": { + "node": ">=20.0.0" + } + }, + "node_modules/@aws-sdk/types": { + "version": "3.974.5", + "resolved": "https://registry.npmjs.org/@aws-sdk/types/-/types-3.974.5.tgz", + "integrity": "sha512-LkwLL2BLbC6wNNm4JaH9mbEqBMdOZCct6VAYqhdN4U1xrWM+fUJQEfbHwQgDypapOWTRtlk25akb5afM0P8CIQ==", + "license": "Apache-2.0", + "dependencies": { + "@smithy/types": "^4.17.2", + "tslib": "^2.6.2" + }, + "engines": { + "node": ">=20.0.0" + } + }, + "node_modules/@aws-sdk/xml-builder": { + "version": "3.972.40", + "resolved": "https://registry.npmjs.org/@aws-sdk/xml-builder/-/xml-builder-3.972.40.tgz", + "integrity": "sha512-wlFmCIGUlwF4zx/kncw+bmxTQh1HeSJq4mYV/V5cZUSJadDP3kXvGW8Rn21cimj/7y9ju+47oYWXi97vF7czaA==", + "license": "Apache-2.0", + "dependencies": { + "@smithy/types": "^4.17.2", + "tslib": "^2.6.2" + }, + "engines": { + "node": ">=20.0.0" + } + }, + "node_modules/@aws/lambda-invoke-store": { + "version": "0.3.0", + "resolved": "https://registry.npmjs.org/@aws/lambda-invoke-store/-/lambda-invoke-store-0.3.0.tgz", + "integrity": "sha512-sl4Bm6yiMNYrZKkqqDFWN0UfnWhlS8ivKxrYl+6t0gCLrqr8y3B2IqZZbFRkfaVVp7C/baApyh71P+LeE1A2sQ==", + "license": "Apache-2.0", + "engines": { + "node": ">=18.0.0" + } + }, + "node_modules/@babel/runtime": { + "version": "7.29.2", + "resolved": "https://registry.npmjs.org/@babel/runtime/-/runtime-7.29.2.tgz", + "integrity": "sha512-JiDShH45zKHWyGe4ZNVRrCjBz8Nh9TMmZG1kh4QTK8hCBTWBi8Da+i7s1fJw7/lYpM4ccepSNfqzZ/QvABBi5g==", + "license": "MIT", + "engines": { + "node": ">=6.9.0" + } + }, + "node_modules/@earendil-works/chord": { + "version": "0.85.1", + "resolved": "https://registry.npmjs.org/@earendil-works/chord/-/chord-0.85.1.tgz", + "license": "MIT", + "dependencies": { + "esbuild": "0.28.2" + }, + "engines": { + "node": ">=22.19.0" + } + }, + "node_modules/@earendil-works/pi-agent-core": { + "version": "0.85.1", + "resolved": "https://registry.npmjs.org/@earendil-works/pi-agent-core/-/pi-agent-core-0.85.1.tgz", + "license": "MIT", + "dependencies": { + "@earendil-works/chord": "^0.85.1", + "@earendil-works/pi-ai": "^0.85.1", + "@earendil-works/pi-telemetry": "^0.85.1", + "diff": "8.0.4", + "ignore": "7.0.8", + "typebox": "1.3.27", + "yaml": "2.9.0" + }, + "engines": { + "node": ">=22.19.0" + } + }, + "node_modules/@earendil-works/pi-ai": { + "version": "0.85.1", + "resolved": "https://registry.npmjs.org/@earendil-works/pi-ai/-/pi-ai-0.85.1.tgz", + "license": "MIT", + "dependencies": { + "@anthropic-ai/sdk": "0.124.0", + "@aws-sdk/client-bedrock-runtime": "3.1127.0", + "@earendil-works/pi-telemetry": "^0.85.1", + "@google/genai": "2.21.0", + "@smithy/node-http-handler": "4.12.1", + "http-proxy-agent": "9.1.0", + "https-proxy-agent": "9.1.0", + "openai": "6.40.0", + "partial-json": "0.1.7", + "typebox": "1.3.27" + }, + "bin": { + "pi-ai": "dist/cli.js" + }, + "engines": { + "node": ">=22.19.0" + } + }, + "node_modules/@earendil-works/pi-ai/node_modules/agent-base": { + "version": "9.0.0", + "resolved": "https://registry.npmjs.org/agent-base/-/agent-base-9.0.0.tgz", + "integrity": "sha512-TQf59BsZnytt8GdJKLPfUZ54g/iaUL2OWDSFCCvMOhsHduDQxO8xC4PNeyIkVcA5KwL2phPSv0douC0fgWzmnA==", + "license": "MIT", + "engines": { + "node": ">= 20" + } + }, + "node_modules/@earendil-works/pi-ai/node_modules/https-proxy-agent": { + "version": "9.1.0", + "resolved": "https://registry.npmjs.org/https-proxy-agent/-/https-proxy-agent-9.1.0.tgz", + "integrity": "sha512-ag87y7cJJ9/3+GxFr8Oy4O5faDsGRGnBGsJj/YjOSsSx/5eadKLYTMPlzuR6obgoCDDm0abAAZitXXQkMOPSpA==", + "license": "MIT", + "dependencies": { + "agent-base": "9.0.0", + "debug": "^4.3.4", + "proxy-agent-negotiate": "1.1.0" + }, + "engines": { + "node": ">= 20" + } + }, + "node_modules/@earendil-works/pi-coding-agent": { + "version": "0.85.1", + "resolved": "https://registry.npmjs.org/@earendil-works/pi-coding-agent/-/pi-coding-agent-0.85.1.tgz", + "license": "MIT", + "dependencies": { + "@earendil-works/chord": "^0.85.1", + "@earendil-works/pi-agent-core": "^0.85.1", + "@earendil-works/pi-ai": "^0.85.1", + "@earendil-works/pi-tui": "^0.85.1", + "@silvia-odwyer/photon-node": "0.3.4", + "chalk": "6.0.0", + "cross-spawn": "7.0.6", + "diff": "8.0.4", + "grok-mermaid": "0.2.3", + "highlight.js": "10.7.3", + "hosted-git-info": "9.0.3", + "ignore": "7.0.8", + "jiti": "2.7.0", + "minimatch": "10.2.6", + "proper-lockfile": "4.1.2", + "semver": "7.8.5", + "typebox": "1.3.27", + "undici": "8.10.2", + "yaml": "2.9.0" + }, + "bin": { + "pi": "dist/bundle/cli.js" + }, + "engines": { + "node": ">=22.19.0" + } + }, + "node_modules/@earendil-works/pi-telemetry": { + "version": "0.85.1", + "resolved": "https://registry.npmjs.org/@earendil-works/pi-telemetry/-/pi-telemetry-0.85.1.tgz", + "license": "MIT", + "engines": { + "node": ">=22.19.0" + } + }, + "node_modules/@earendil-works/pi-tui": { + "version": "0.85.1", + "resolved": "https://registry.npmjs.org/@earendil-works/pi-tui/-/pi-tui-0.85.1.tgz", + "license": "MIT", + "dependencies": { + "get-east-asian-width": "1.6.0", + "marked": "18.0.11" + }, + "engines": { + "node": ">=22.19.0" + } + }, + "node_modules/@esbuild/aix-ppc64": { + "version": "0.28.2", + "resolved": "https://registry.npmjs.org/@esbuild/aix-ppc64/-/aix-ppc64-0.28.2.tgz", + "integrity": "sha512-XExcO+dvLKvVtNTibSTBej1NCAbaGhWn9Ww1ZPx80qsahhPFe/8jgWP0IchNe0F3HwkU7n8ejhH8bjonqht8mQ==", + "license": "MIT", + "engines": { + "node": ">=18" + }, + "os": [ + "aix" + ], + "cpu": [ + "ppc64" + ], + "optional": true + }, + "node_modules/@esbuild/android-arm": { + "version": "0.28.2", + "resolved": "https://registry.npmjs.org/@esbuild/android-arm/-/android-arm-0.28.2.tgz", + "integrity": "sha512-kXXoiPVVGQcnIYGOeaovwOURpniDBpSq4A03qkQ+BMQqtGG6HYap3xne9C1O1yo4TR3qxlCX5IqqmX6fFo2Lqg==", + "license": "MIT", + "engines": { + "node": ">=18" + }, + "os": [ + "android" + ], + "cpu": [ + "arm" + ], + "optional": true + }, + "node_modules/@esbuild/android-arm64": { + "version": "0.28.2", + "resolved": "https://registry.npmjs.org/@esbuild/android-arm64/-/android-arm64-0.28.2.tgz", + "integrity": "sha512-5YfKeeI8qWfBZIX+u2xZC3Zlb3Os/gLS2sbEKM+I4ZOcsWmHS2WLysCcQZDAFRslDUU5Oiq44gf6PYN1vGwG5A==", + "license": "MIT", + "engines": { + "node": ">=18" + }, + "os": [ + "android" + ], + "cpu": [ + "arm64" + ], + "optional": true + }, + "node_modules/@esbuild/android-x64": { + "version": "0.28.2", + "resolved": "https://registry.npmjs.org/@esbuild/android-x64/-/android-x64-0.28.2.tgz", + "integrity": "sha512-O387ite7SzUyCcy3JQX4P4bLtEA7bLLkx+esve5JHnyYfNTxcVpXZo9jhdB0lTKN44gztELTdU7nS8Nr16Fs1Q==", + "license": "MIT", + "engines": { + "node": ">=18" + }, + "os": [ + "android" + ], + "cpu": [ + "x64" + ], + "optional": true + }, + "node_modules/@esbuild/darwin-arm64": { + "version": "0.28.2", + "resolved": "https://registry.npmjs.org/@esbuild/darwin-arm64/-/darwin-arm64-0.28.2.tgz", + "integrity": "sha512-n4KqkOQrraxHJcgjM1RvwbigfQKIKJVpM7xp+KsxiyUSrRdIXnt73VhrPAx0fV44hgfmIVKjxMN9J1t5jySVkw==", + "license": "MIT", + "engines": { + "node": ">=18" + }, + "os": [ + "darwin" + ], + "cpu": [ + "arm64" + ], + "optional": true + }, + "node_modules/@esbuild/darwin-x64": { + "version": "0.28.2", + "resolved": "https://registry.npmjs.org/@esbuild/darwin-x64/-/darwin-x64-0.28.2.tgz", + "integrity": "sha512-uq6suIWYP37qzGddBKPw5QEQPi6HiLGsO7UmkpfyaYNQ3D+rN6w6WfwH+nuqcGXWvawGwxOEroO4YGnFh95azw==", + "license": "MIT", + "engines": { + "node": ">=18" + }, + "os": [ + "darwin" + ], + "cpu": [ + "x64" + ], + "optional": true + }, + "node_modules/@esbuild/freebsd-arm64": { + "version": "0.28.2", + "resolved": "https://registry.npmjs.org/@esbuild/freebsd-arm64/-/freebsd-arm64-0.28.2.tgz", + "integrity": "sha512-n+I0BTSRIoy+d6RPKnEVwql5UwBJolytvY4mAOIEJorKlqgPII8ix6slVVrfZ5Tnj7glIZvloylbB/EJPMWEXw==", + "license": "MIT", + "engines": { + "node": ">=18" + }, + "os": [ + "freebsd" + ], + "cpu": [ + "arm64" + ], + "optional": true + }, + "node_modules/@esbuild/freebsd-x64": { + "version": "0.28.2", + "resolved": "https://registry.npmjs.org/@esbuild/freebsd-x64/-/freebsd-x64-0.28.2.tgz", + "integrity": "sha512-78XJTJkvPs0kz2w61301PJjXl4g7q3JqiYMZ/M/yVI73EHBrCRTgkhu9oqG7vPqq+a/yadEW8aD+agKlk5xrmg==", + "license": "MIT", + "engines": { + "node": ">=18" + }, + "os": [ + "freebsd" + ], + "cpu": [ + "x64" + ], + "optional": true + }, + "node_modules/@esbuild/linux-arm": { + "version": "0.28.2", + "resolved": "https://registry.npmjs.org/@esbuild/linux-arm/-/linux-arm-0.28.2.tgz", + "integrity": "sha512-XlDnu2q5yoqems+xay6wSAcg9DDD7K9RLKZEBOMZm3ckNpJBvOX20tSfby8KfrrhINDyv9V2YVZKY/SpoGJI8w==", + "license": "MIT", + "engines": { + "node": ">=18" + }, + "os": [ + "linux" + ], + "cpu": [ + "arm" + ], + "optional": true + }, + "node_modules/@esbuild/linux-arm64": { + "version": "0.28.2", + "resolved": "https://registry.npmjs.org/@esbuild/linux-arm64/-/linux-arm64-0.28.2.tgz", + "integrity": "sha512-pW4AC0P3it8c7do9MVM4p51FzHzdM/TZrerurgRcHJ2WTa1VQ1CIq18xncfpBJw4ojkiZZrKW2yIBWBP92j6Ug==", + "license": "MIT", + "engines": { + "node": ">=18" + }, + "os": [ + "linux" + ], + "cpu": [ + "arm64" + ], + "optional": true + }, + "node_modules/@esbuild/linux-ia32": { + "version": "0.28.2", + "resolved": "https://registry.npmjs.org/@esbuild/linux-ia32/-/linux-ia32-0.28.2.tgz", + "integrity": "sha512-CYbnj78HsIeA+DhgUKgFCfvNsTHFhMMrinUrMZpDXJXKN8T3XViTZ/+wtHeVxEWY8ewSzTFN+nRmSwO2tZaLUQ==", + "license": "MIT", + "engines": { + "node": ">=18" + }, + "os": [ + "linux" + ], + "cpu": [ + "ia32" + ], + "optional": true + }, + "node_modules/@esbuild/linux-loong64": { + "version": "0.28.2", + "resolved": "https://registry.npmjs.org/@esbuild/linux-loong64/-/linux-loong64-0.28.2.tgz", + "integrity": "sha512-buwkd8nsph4R+ajRvw0qM5Hja/TXQow3ptzWO2EbG/cqcIkHloRrdlBtQlshyYGTNFvfkfJ5tpPLVkY4DtsPfQ==", + "license": "MIT", + "engines": { + "node": ">=18" + }, + "os": [ + "linux" + ], + "cpu": [ + "loong64" + ], + "optional": true + }, + "node_modules/@esbuild/linux-mips64el": { + "version": "0.28.2", + "resolved": "https://registry.npmjs.org/@esbuild/linux-mips64el/-/linux-mips64el-0.28.2.tgz", + "integrity": "sha512-ZVykbDyk7519VwiNb9Lcj9m8XM6v5V9uKPvrEMkkEedVewf+0itkhahp4HDpgERXhwLRpWFypsGbG/J8s0QjJA==", + "license": "MIT", + "engines": { + "node": ">=18" + }, + "os": [ + "linux" + ], + "cpu": [ + "mips64el" + ], + "optional": true + }, + "node_modules/@esbuild/linux-ppc64": { + "version": "0.28.2", + "resolved": "https://registry.npmjs.org/@esbuild/linux-ppc64/-/linux-ppc64-0.28.2.tgz", + "integrity": "sha512-CAXl+Dtd9UUuJd8pKKdwh6MLm3MUMiqMPmhZ3tTSXPqfyQ3vDl6R5hZdZ/kYojK4ofXtdfSv1tFq8XzWx3heNQ==", + "license": "MIT", + "engines": { + "node": ">=18" + }, + "os": [ + "linux" + ], + "cpu": [ + "ppc64" + ], + "optional": true + }, + "node_modules/@esbuild/linux-riscv64": { + "version": "0.28.2", + "resolved": "https://registry.npmjs.org/@esbuild/linux-riscv64/-/linux-riscv64-0.28.2.tgz", + "integrity": "sha512-GeXCej4IQtU1B+QlDV8W/RRvbzI3O/Stss+/bCXv4lZls5WGRtu2a+3JkA3i4qIUlMXpcHebWpF8AkJhATowuA==", + "license": "MIT", + "engines": { + "node": ">=18" + }, + "os": [ + "linux" + ], + "cpu": [ + "riscv64" + ], + "optional": true + }, + "node_modules/@esbuild/linux-s390x": { + "version": "0.28.2", + "resolved": "https://registry.npmjs.org/@esbuild/linux-s390x/-/linux-s390x-0.28.2.tgz", + "integrity": "sha512-3H1weTYZPxt/WOhByszQZybS9w5lKzUn1FDMsgEChbHWQwHYQQRfBxgCcZvPhjHfKyJjIievvMmEUawJrdY9Dg==", + "license": "MIT", + "engines": { + "node": ">=18" + }, + "os": [ + "linux" + ], + "cpu": [ + "s390x" + ], + "optional": true + }, + "node_modules/@esbuild/linux-x64": { + "version": "0.28.2", + "resolved": "https://registry.npmjs.org/@esbuild/linux-x64/-/linux-x64-0.28.2.tgz", + "integrity": "sha512-4xTZr1FUmSoQW4XIWmit3tzQrUTZM+N3P0XV8xROKYF50XfI7xeO90+1bZvNwxIufQ9hDQVRJH5YhgPVF8A/HQ==", + "license": "MIT", + "engines": { + "node": ">=18" + }, + "os": [ + "linux" + ], + "cpu": [ + "x64" + ], + "optional": true + }, + "node_modules/@esbuild/netbsd-arm64": { + "version": "0.28.2", + "resolved": "https://registry.npmjs.org/@esbuild/netbsd-arm64/-/netbsd-arm64-0.28.2.tgz", + "integrity": "sha512-sSATRjPeDBg3pdgHoQfoYBob11Kk1FGa9lui5RIHZCoCkJa9QKlvl3/vKz2usCmYYjs7ymJR/2Nnsqe+Hjt5nw==", + "license": "MIT", + "engines": { + "node": ">=18" + }, + "os": [ + "netbsd" + ], + "cpu": [ + "arm64" + ], + "optional": true + }, + "node_modules/@esbuild/netbsd-x64": { + "version": "0.28.2", + "resolved": "https://registry.npmjs.org/@esbuild/netbsd-x64/-/netbsd-x64-0.28.2.tgz", + "integrity": "sha512-lqnzCV+mM0gIADaKihiCg6ifgfU2L3h5E33rNQBN1Y4MaVGnzryzmvvf7UHxprpQdE8hpqLolJ9Rl+SkIRDpyw==", + "license": "MIT", + "engines": { + "node": ">=18" + }, + "os": [ + "netbsd" + ], + "cpu": [ + "x64" + ], + "optional": true + }, + "node_modules/@esbuild/openbsd-arm64": { + "version": "0.28.2", + "resolved": "https://registry.npmjs.org/@esbuild/openbsd-arm64/-/openbsd-arm64-0.28.2.tgz", + "integrity": "sha512-AL2qJILH7lNjrDmCQDvdxMfAUIv8KMNZOvrwAQ8i8//ntL9FflhOyMJ8OZSMBb8/AWXe3/5v5S20y3zCoZWKoQ==", + "license": "MIT", + "engines": { + "node": ">=18" + }, + "os": [ + "openbsd" + ], + "cpu": [ + "arm64" + ], + "optional": true + }, + "node_modules/@esbuild/openbsd-x64": { + "version": "0.28.2", + "resolved": "https://registry.npmjs.org/@esbuild/openbsd-x64/-/openbsd-x64-0.28.2.tgz", + "integrity": "sha512-QtiuPytchRyC4rwUKhexJdQKvDuZ6hWloi3igqPQNUJCS1/v9EiO3UTOXR6A3FoMo4fnAKbWJdqaIwhOzh8qEw==", + "license": "MIT", + "engines": { + "node": ">=18" + }, + "os": [ + "openbsd" + ], + "cpu": [ + "x64" + ], + "optional": true + }, + "node_modules/@esbuild/openharmony-arm64": { + "version": "0.28.2", + "resolved": "https://registry.npmjs.org/@esbuild/openharmony-arm64/-/openharmony-arm64-0.28.2.tgz", + "integrity": "sha512-WkhYDmpTjLvGlScA1rwjRUmhl4k8oXR3cIbtqWmELgU/dFeHHlEllxDvdWcNJV9rbzCexB5vz8gtNewWLgCT7Q==", + "license": "MIT", + "engines": { + "node": ">=18" + }, + "os": [ + "openharmony" + ], + "cpu": [ + "arm64" + ], + "optional": true + }, + "node_modules/@esbuild/sunos-x64": { + "version": "0.28.2", + "resolved": "https://registry.npmjs.org/@esbuild/sunos-x64/-/sunos-x64-0.28.2.tgz", + "integrity": "sha512-GPMSkTOtMnv2U2F8gxe4Io6qmVs+YKyp832Etqqxr0hFngmXQ3rzwytelm3GIn7T4VviRUlf3sOgBOiTdvaf7g==", + "license": "MIT", + "engines": { + "node": ">=18" + }, + "os": [ + "sunos" + ], + "cpu": [ + "x64" + ], + "optional": true + }, + "node_modules/@esbuild/win32-arm64": { + "version": "0.28.2", + "resolved": "https://registry.npmjs.org/@esbuild/win32-arm64/-/win32-arm64-0.28.2.tgz", + "integrity": "sha512-PIhhEkE9uPBleRBrQEJpUn7MBnibZzbGzYWPmY3x+YoVg/95zbjB4CxPPOQ8l5tYYM4mMaCthF8/1DIfBQQyWQ==", + "license": "MIT", + "engines": { + "node": ">=18" + }, + "os": [ + "win32" + ], + "cpu": [ + "arm64" + ], + "optional": true + }, + "node_modules/@esbuild/win32-ia32": { + "version": "0.28.2", + "resolved": "https://registry.npmjs.org/@esbuild/win32-ia32/-/win32-ia32-0.28.2.tgz", + "integrity": "sha512-YmJbfTlvU7Sdn9BB+4PRES4oB6pxgS37MAONj+hBr/cpXS1aBPKXxNnDbu+QCWPj0o9dgyxeq79g6c5P8KeuYA==", + "license": "MIT", + "engines": { + "node": ">=18" + }, + "os": [ + "win32" + ], + "cpu": [ + "ia32" + ], + "optional": true + }, + "node_modules/@esbuild/win32-x64": { + "version": "0.28.2", + "resolved": "https://registry.npmjs.org/@esbuild/win32-x64/-/win32-x64-0.28.2.tgz", + "integrity": "sha512-5ebpxr3nWMzrL/rnUI755Jkuee0bHL/Gq0WTF9lvcpv73wAp5eu8MfBUgWK9bhWvZjj7yX8etf/8tI8Ney695g==", + "license": "MIT", + "engines": { + "node": ">=18" + }, + "os": [ + "win32" + ], + "cpu": [ + "x64" + ], + "optional": true + }, + "node_modules/@google/genai": { + "version": "2.21.0", + "resolved": "https://registry.npmjs.org/@google/genai/-/genai-2.21.0.tgz", + "integrity": "sha512-+PDtco2/Z0ONdzCGekCoCT+O1VJS9xJQNN4XzQpXG/t3El/SWWMkCWlFRO1KmivOHPa4Q0VjUYu1HBKCZ/v33Q==", + "license": "Apache-2.0", + "dependencies": { + "google-auth-library": "^10.3.0", + "p-retry": "^4.6.2", + "protobufjs": "^7.5.4", + "ws": "^8.18.0" + }, + "peerDependencies": { + "@modelcontextprotocol/sdk": "^1.25.2" + }, + "peerDependenciesMeta": { + "@modelcontextprotocol/sdk": { + "optional": true + } + }, + "engines": { + "node": ">=20.0.0" + }, + "hasInstallScript": true + }, + "node_modules/@protobufjs/aspromise": { + "version": "1.1.2", + "resolved": "https://registry.npmjs.org/@protobufjs/aspromise/-/aspromise-1.1.2.tgz", + "integrity": "sha512-j+gKExEuLmKwvz3OgROXtrJ2UG2x8Ch2YZUxahh+s1F2HZ+wAceUNLkvy6zKCPVRkU++ZWQrdxsUeQXmcg4uoQ==", + "license": "BSD-3-Clause" + }, + "node_modules/@protobufjs/base64": { + "version": "1.1.2", + "resolved": "https://registry.npmjs.org/@protobufjs/base64/-/base64-1.1.2.tgz", + "integrity": "sha512-AZkcAA5vnN/v4PDqKyMR5lx7hZttPDgClv83E//FMNhR2TMcLUhfRUBHCmSl0oi9zMgDDqRUJkSxO3wm85+XLg==", + "license": "BSD-3-Clause" + }, + "node_modules/@protobufjs/codegen": { + "version": "2.0.5", + "resolved": "https://registry.npmjs.org/@protobufjs/codegen/-/codegen-2.0.5.tgz", + "integrity": "sha512-zgXFLzW3Ap33e6d0Wlj4MGIm6Ce8O89n/apUaGNB/jx+hw+ruWEp7EwGUshdLKVRCxZW12fp9r40E1mQrf/34g==", + "license": "BSD-3-Clause" + }, + "node_modules/@protobufjs/eventemitter": { + "version": "1.1.1", + "resolved": "https://registry.npmjs.org/@protobufjs/eventemitter/-/eventemitter-1.1.1.tgz", + "integrity": "sha512-vW1GmwMZNnL+gMRaovlh9yZX74kc+TTU3FObkkurpMaRtBfLP3ldjS9KQWlwZgraRE0+dheEEoAxdzcJQ8eXZg==", + "license": "BSD-3-Clause" + }, + "node_modules/@protobufjs/fetch": { + "version": "1.1.1", + "resolved": "https://registry.npmjs.org/@protobufjs/fetch/-/fetch-1.1.1.tgz", + "integrity": "sha512-GpptLrs57adMSuHi3VNj0mAF8dwh36LMaYF6XyJ6JMWlVsc+t42tm1HSEDmOs3A8fC9yyeisgLhsTVQokOZ0zw==", + "license": "BSD-3-Clause", + "dependencies": { + "@protobufjs/aspromise": "^1.1.1" + } + }, + "node_modules/@protobufjs/float": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/@protobufjs/float/-/float-1.0.2.tgz", + "integrity": "sha512-Ddb+kVXlXst9d+R9PfTIxh1EdNkgoRe5tOX6t01f1lYWOvJnSPDBlG241QLzcyPdoNTsblLUdujGSE4RzrTZGQ==", + "license": "BSD-3-Clause" + }, + "node_modules/@protobufjs/path": { + "version": "1.1.2", + "resolved": "https://registry.npmjs.org/@protobufjs/path/-/path-1.1.2.tgz", + "integrity": "sha512-6JOcJ5Tm08dOHAbdR3GrvP+yUUfkjG5ePsHYczMFLq3ZmMkAD98cDgcT2iA1lJ9NVwFd4tH/iSSoe44YWkltEA==", + "license": "BSD-3-Clause" + }, + "node_modules/@protobufjs/pool": { + "version": "1.1.0", + "resolved": "https://registry.npmjs.org/@protobufjs/pool/-/pool-1.1.0.tgz", + "integrity": "sha512-0kELaGSIDBKvcgS4zkjz1PeddatrjYcmMWOlAuAPwAeccUrPHdUqo/J6LiymHHEiJT5NrF1UVwxY14f+fy4WQw==", + "license": "BSD-3-Clause" + }, + "node_modules/@protobufjs/utf8": { + "version": "1.1.1", + "resolved": "https://registry.npmjs.org/@protobufjs/utf8/-/utf8-1.1.1.tgz", + "integrity": "sha512-oOAWABowe8EAbMyWKM0tYDKi8Yaox52D+HWZhAIJqQXbqe0xI/GV7FhLWqlEKreMkfDjshR5FKgi3mnle0h6Eg==", + "license": "BSD-3-Clause" + }, + "node_modules/@silvia-odwyer/photon-node": { + "version": "0.3.4", + "resolved": "https://registry.npmjs.org/@silvia-odwyer/photon-node/-/photon-node-0.3.4.tgz", + "integrity": "sha512-bnly4BKB3KDTFxrUIcgCLbaeVVS8lrAkri1pEzskpmxu9MdfGQTy8b8EgcD83ywD3RPMsIulY8xJH5Awa+t9fA==", + "license": "Apache-2.0" + }, + "node_modules/@smithy/core": { + "version": "3.33.3", + "resolved": "https://registry.npmjs.org/@smithy/core/-/core-3.33.3.tgz", + "integrity": "sha512-CsOeKq/9kA3y6VJHt+/+VTCtBaxJ4OTFpgrjIUhPpDIKxBci1k2bJaQASF2h/ELWrulGp+t97DZ0mevfAD8idg==", + "license": "Apache-2.0", + "dependencies": { + "@smithy/types": "^4.17.2", + "tslib": "^2.6.2" + }, + "engines": { + "node": ">=18.0.0" + } + }, + "node_modules/@smithy/credential-provider-imds": { + "version": "4.5.2", + "resolved": "https://registry.npmjs.org/@smithy/credential-provider-imds/-/credential-provider-imds-4.5.2.tgz", + "integrity": "sha512-A9uSdn72ozbRUSit0eib0TW7nXuNPlaeM0zcGkJ+nE6tFcSDbnmtwoxbTCFBukVQcszDAyvsd7+rTduPTXpygg==", + "license": "Apache-2.0", + "dependencies": { + "@smithy/core": "^3.33.2", + "@smithy/types": "^4.17.2", + "tslib": "^2.6.2" + }, + "engines": { + "node": ">=18.0.0" + } + }, + "node_modules/@smithy/fetch-http-handler": { + "version": "5.8.0", + "resolved": "https://registry.npmjs.org/@smithy/fetch-http-handler/-/fetch-http-handler-5.8.0.tgz", + "integrity": "sha512-ycSJu3tFAQ4v04CBB0agqFMVsSQ1iG3yw+SpgxRqKfaURpQD4CZ8Wn0zPMmSnOuTpTh65Vz+EA0rMrw089wvkA==", + "license": "Apache-2.0", + "dependencies": { + "@smithy/core": "^3.33.3", + "@smithy/types": "^4.18.0", + "tslib": "^2.6.2" + }, + "engines": { + "node": ">=18.0.0" + } + }, + "node_modules/@smithy/node-http-handler": { + "version": "4.12.1", + "resolved": "https://registry.npmjs.org/@smithy/node-http-handler/-/node-http-handler-4.12.1.tgz", + "integrity": "sha512-ThMkboGeONWXAelq9FvGsuJC4rOi+qyC4/zhUF58xYpxUg5sQKx2VXZYJmtNjr4dSuBJ1HeJXETQILCz3wOHvw==", + "license": "Apache-2.0", + "dependencies": { + "@smithy/core": "^3.33.3", + "@smithy/types": "^4.18.0", + "tslib": "^2.6.2" + }, + "engines": { + "node": ">=18.0.0" + } + }, + "node_modules/@smithy/signature-v4": { + "version": "5.7.3", + "resolved": "https://registry.npmjs.org/@smithy/signature-v4/-/signature-v4-5.7.3.tgz", + "integrity": "sha512-7ImGm+FkHRLcBaRttIAMZ6bzJZWb2cJGoYjq46F2UjycujWzrL9GEN9h4w7eQyXJYnltrUhxbbieBAIRrdqpow==", + "license": "Apache-2.0", + "dependencies": { + "@smithy/core": "^3.33.3", + "@smithy/types": "^4.17.2", + "tslib": "^2.6.2" + }, + "engines": { + "node": ">=18.0.0" + } + }, + "node_modules/@smithy/types": { + "version": "4.18.0", + "resolved": "https://registry.npmjs.org/@smithy/types/-/types-4.18.0.tgz", + "integrity": "sha512-CgB6HHWer/vrKps24ulRIbpcpb7K4xAU7SkZ7YHzBPlwHsvsrCJFEXK421s+cJzX+ZrqtA/TuU5w1HzI7k9N8A==", + "license": "Apache-2.0", + "dependencies": { + "tslib": "^2.6.2" + }, + "engines": { + "node": ">=18.0.0" + } + }, + "node_modules/@stablelib/base64": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/@stablelib/base64/-/base64-1.0.1.tgz", + "integrity": "sha512-1bnPQqSxSuc3Ii6MhBysoWCg58j97aUjuCSZrGSmDxNqtytIi0k8utUenAwTZN4V5mXXYGsVUI9zeBqy+jBOSQ==", + "license": "MIT" + }, + "node_modules/@types/node": { + "version": "22.19.19", + "resolved": "https://registry.npmjs.org/@types/node/-/node-22.19.19.tgz", + "integrity": "sha512-dyh/xO2Fh5bYrfWaaqGrRQQGkNdmYw6AmaAUvYeUMNTWQtvb796ikLdmTchRmOlOiIJ1TDXfWgVx1QkUlQ6Hew==", + "license": "MIT", + "dependencies": { + "undici-types": "~6.21.0" + } + }, + "node_modules/agent-base": { + "version": "7.1.4", + "resolved": "https://registry.npmjs.org/agent-base/-/agent-base-7.1.4.tgz", + "integrity": "sha512-MnA+YT8fwfJPgBx3m60MNqakm30XOkyIoH1y6huTQvC0PwZG7ki8NacLBcrPbNoo8vEZy7Jpuk7+jMO+CUovTQ==", + "license": "MIT", + "engines": { + "node": ">= 14" + } + }, + "node_modules/balanced-match": { + "version": "4.0.4", + "resolved": "https://registry.npmjs.org/balanced-match/-/balanced-match-4.0.4.tgz", + "integrity": "sha512-BLrgEcRTwX2o6gGxGOCNyMvGSp35YofuYzw9h1IMTRmKqttAZZVU67bdb9Pr2vUHA8+j3i2tJfjO6C6+4myGTA==", + "license": "MIT", + "engines": { + "node": "18 || 20 || >=22" + } + }, + "node_modules/base64-js": { + "version": "1.5.1", + "resolved": "https://registry.npmjs.org/base64-js/-/base64-js-1.5.1.tgz", + "integrity": "sha512-AKpaYlHn8t4SVbOHCy+b5+KKgvR4vrsD8vbvrbiQJps7fKDTkjkDry6ji0rUJjC0kzbNePLwzxq8iypo41qeWA==", + "license": "MIT", + "funding": [ + { + "type": "github", + "url": "https://github.com/sponsors/feross" + }, + { + "type": "patreon", + "url": "https://www.patreon.com/feross" + }, + { + "type": "consulting", + "url": "https://feross.org/support" + } + ] + }, + "node_modules/bignumber.js": { + "version": "9.3.1", + "resolved": "https://registry.npmjs.org/bignumber.js/-/bignumber.js-9.3.1.tgz", + "integrity": "sha512-Ko0uX15oIUS7wJ3Rb30Fs6SkVbLmPBAKdlm7q9+ak9bbIeFf0MwuBsQV6z7+X768/cHsfg+WlysDWJcmthjsjQ==", + "license": "MIT", + "engines": { + "node": "*" + } + }, + "node_modules/bowser": { + "version": "2.14.1", + "resolved": "https://registry.npmjs.org/bowser/-/bowser-2.14.1.tgz", + "integrity": "sha512-tzPjzCxygAKWFOJP011oxFHs57HzIhOEracIgAePE4pqB3LikALKnSzUyU4MGs9/iCEUuHlAJTjTc5M+u7YEGg==", + "license": "MIT" + }, + "node_modules/brace-expansion": { + "version": "5.0.9", + "resolved": "https://registry.npmjs.org/brace-expansion/-/brace-expansion-5.0.9.tgz", + "integrity": "sha512-ScQ4IuvIEF1TMlP7Zt+vjJ//9zlPb2SDcxWxM3bk8s6t6GGdJ7KO1dCcTidOPJKePW30LE/2cT7wCyPho9/Wxg==", + "license": "MIT", + "dependencies": { + "balanced-match": "^4.0.2" + }, + "engines": { + "node": "20 || >=22" + } + }, + "node_modules/buffer-equal-constant-time": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/buffer-equal-constant-time/-/buffer-equal-constant-time-1.0.1.tgz", + "integrity": "sha512-zRpUiDwd/xk6ADqPMATG8vc9VPrkck7T07OIx0gnjmJAnHnTVXNQG3vfvWNuiZIkwu9KrKdA1iJKfsfTVxE6NA==", + "license": "BSD-3-Clause" + }, + "node_modules/chalk": { + "version": "6.0.0", + "resolved": "https://registry.npmjs.org/chalk/-/chalk-6.0.0.tgz", + "integrity": "sha512-2uNTXIuTTxk7ciZgAU1BQcgnchcG0xXnrs6jzkQfj9SsRa9M2s5zE8WT96hS6KmG4MzWHSrvH43DF1m4XRkrFg==", + "license": "MIT", + "engines": { + "node": ">=22" + }, + "funding": { + "url": "https://github.com/chalk/chalk?sponsor=1" + } + }, + "node_modules/cross-spawn": { + "version": "7.0.6", + "resolved": "https://registry.npmjs.org/cross-spawn/-/cross-spawn-7.0.6.tgz", + "integrity": "sha512-uV2QOWP2nWzsy2aMp8aRibhi9dlzF5Hgh5SHaB9OiTGEyDTiJJyx0uy51QXdyWbtAHNua4XJzUKca3OzKUd3vA==", + "license": "MIT", + "dependencies": { + "path-key": "^3.1.0", + "shebang-command": "^2.0.0", + "which": "^2.0.1" + }, + "engines": { + "node": ">= 8" + } + }, + "node_modules/data-uri-to-buffer": { + "version": "4.0.1", + "resolved": "https://registry.npmjs.org/data-uri-to-buffer/-/data-uri-to-buffer-4.0.1.tgz", + "integrity": "sha512-0R9ikRb668HB7QDxT1vkpuUBtqc53YyAwMwGeUFKRojY/NWKvdZ+9UYtRfGmhqNbRkTSVpMbmyhXipFFv2cb/A==", + "license": "MIT", + "engines": { + "node": ">= 12" + } + }, + "node_modules/debug": { + "version": "4.4.3", + "resolved": "https://registry.npmjs.org/debug/-/debug-4.4.3.tgz", + "integrity": "sha512-RGwwWnwQvkVfavKVt22FGLw+xYSdzARwm0ru6DhTVA3umU5hZc28V3kO4stgYryrTlLpuvgI9GiijltAjNbcqA==", + "license": "MIT", + "dependencies": { + "ms": "^2.1.3" + }, + "peerDependenciesMeta": { + "supports-color": { + "optional": true + } + }, + "engines": { + "node": ">=6.0" + } + }, + "node_modules/diff": { + "version": "8.0.4", + "resolved": "https://registry.npmjs.org/diff/-/diff-8.0.4.tgz", + "integrity": "sha512-DPi0FmjiSU5EvQV0++GFDOJ9ASQUVFh5kD+OzOnYdi7n3Wpm9hWWGfB/O2blfHcMVTL5WkQXSnRiK9makhrcnw==", + "license": "BSD-3-Clause", + "engines": { + "node": ">=0.3.1" + } + }, + "node_modules/ecdsa-sig-formatter": { + "version": "1.0.11", + "resolved": "https://registry.npmjs.org/ecdsa-sig-formatter/-/ecdsa-sig-formatter-1.0.11.tgz", + "integrity": "sha512-nagl3RYrbNv6kQkeJIpt6NJZy8twLB/2vtz6yN9Z4vRKHN4/QZJIEbqohALSgwKdnksuY3k5Addp5lg8sVoVcQ==", + "license": "Apache-2.0", + "dependencies": { + "safe-buffer": "^5.0.1" + } + }, + "node_modules/esbuild": { + "version": "0.28.2", + "resolved": "https://registry.npmjs.org/esbuild/-/esbuild-0.28.2.tgz", + "integrity": "sha512-HKVLS8dvII+xoKW9kmqxbRKrnWEXfJJr/FZhhJmiqIB0e053QNYFqOBouTMO/k5sID4MvCiUCvv8b9M4h32wIA==", + "license": "MIT", + "optionalDependencies": { + "@esbuild/aix-ppc64": "0.28.2", + "@esbuild/android-arm": "0.28.2", + "@esbuild/android-arm64": "0.28.2", + "@esbuild/android-x64": "0.28.2", + "@esbuild/darwin-arm64": "0.28.2", + "@esbuild/darwin-x64": "0.28.2", + "@esbuild/freebsd-arm64": "0.28.2", + "@esbuild/freebsd-x64": "0.28.2", + "@esbuild/linux-arm": "0.28.2", + "@esbuild/linux-arm64": "0.28.2", + "@esbuild/linux-ia32": "0.28.2", + "@esbuild/linux-loong64": "0.28.2", + "@esbuild/linux-mips64el": "0.28.2", + "@esbuild/linux-ppc64": "0.28.2", + "@esbuild/linux-riscv64": "0.28.2", + "@esbuild/linux-s390x": "0.28.2", + "@esbuild/linux-x64": "0.28.2", + "@esbuild/netbsd-arm64": "0.28.2", + "@esbuild/netbsd-x64": "0.28.2", + "@esbuild/openbsd-arm64": "0.28.2", + "@esbuild/openbsd-x64": "0.28.2", + "@esbuild/openharmony-arm64": "0.28.2", + "@esbuild/sunos-x64": "0.28.2", + "@esbuild/win32-arm64": "0.28.2", + "@esbuild/win32-ia32": "0.28.2", + "@esbuild/win32-x64": "0.28.2" + }, + "bin": { + "esbuild": "bin/esbuild" + }, + "engines": { + "node": ">=18" + }, + "hasInstallScript": true + }, + "node_modules/extend": { + "version": "3.0.2", + "resolved": "https://registry.npmjs.org/extend/-/extend-3.0.2.tgz", + "integrity": "sha512-fjquC59cD7CyW6urNXK0FBufkZcoiGG80wTuPujX590cB5Ttln20E2UB4S/WARVqhXffZl2LNgS+gQdPIIim/g==", + "license": "MIT" + }, + "node_modules/fast-sha256": { + "version": "1.3.0", + "resolved": "https://registry.npmjs.org/fast-sha256/-/fast-sha256-1.3.0.tgz", + "integrity": "sha512-n11RGP/lrWEFI/bWdygLxhI+pVeo1ZYIVwvvPkW7azl/rOy+F3HYRZ2K5zeE9mmkhQppyv9sQFx0JM9UabnpPQ==", + "license": "Unlicense" + }, + "node_modules/fetch-blob": { + "version": "3.2.0", + "resolved": "https://registry.npmjs.org/fetch-blob/-/fetch-blob-3.2.0.tgz", + "integrity": "sha512-7yAQpD2UMJzLi1Dqv7qFYnPbaPx7ZfFK6PiIxQ4PfkGPyNyl2Ugx+a/umUonmKqjhM4DnfbMvdX6otXq83soQQ==", + "license": "MIT", + "dependencies": { + "node-domexception": "^1.0.0", + "web-streams-polyfill": "^3.0.3" + }, + "engines": { + "node": "^12.20 || >= 14.13" + }, + "funding": [ + { + "type": "github", + "url": "https://github.com/sponsors/jimmywarting" + }, + { + "type": "paypal", + "url": "https://paypal.me/jimmywarting" + } + ] + }, + "node_modules/formdata-polyfill": { + "version": "4.0.10", + "resolved": "https://registry.npmjs.org/formdata-polyfill/-/formdata-polyfill-4.0.10.tgz", + "integrity": "sha512-buewHzMvYL29jdeQTVILecSaZKnt/RJWjoZCF5OW60Z67/GmSLBkOFM7qh1PI3zFNtJbaZL5eQu1vLfazOwj4g==", + "license": "MIT", + "dependencies": { + "fetch-blob": "^3.1.2" + }, + "engines": { + "node": ">=12.20.0" + } + }, + "node_modules/gaxios": { + "version": "7.1.4", + "resolved": "https://registry.npmjs.org/gaxios/-/gaxios-7.1.4.tgz", + "integrity": "sha512-bTIgTsM2bWn3XklZISBTQX7ZSddGW+IO3bMdGaemHZ3tbqExMENHLx6kKZ/KlejgrMtj8q7wBItt51yegqalrA==", + "license": "Apache-2.0", + "dependencies": { + "extend": "^3.0.2", + "https-proxy-agent": "^7.0.1", + "node-fetch": "^3.3.2" + }, + "engines": { + "node": ">=18" + } + }, + "node_modules/gcp-metadata": { + "version": "8.1.2", + "resolved": "https://registry.npmjs.org/gcp-metadata/-/gcp-metadata-8.1.2.tgz", + "integrity": "sha512-zV/5HKTfCeKWnxG0Dmrw51hEWFGfcF2xiXqcA3+J90WDuP0SvoiSO5ORvcBsifmx/FoIjgQN3oNOGaQ5PhLFkg==", + "license": "Apache-2.0", + "dependencies": { + "gaxios": "^7.0.0", + "google-logging-utils": "^1.0.0", + "json-bigint": "^1.0.0" + }, + "engines": { + "node": ">=18" + } + }, + "node_modules/get-east-asian-width": { + "version": "1.6.0", + "resolved": "https://registry.npmjs.org/get-east-asian-width/-/get-east-asian-width-1.6.0.tgz", + "integrity": "sha512-QRbvDIbx6YklUe6RxeTeleMR0yv3cYH6PsPZHcnVn7xv7zO1BHN8r0XETu8n6Ye3Q+ahtSarc3WgtNWmehIBfA==", + "license": "MIT", + "engines": { + "node": ">=18" + }, + "funding": { + "url": "https://github.com/sponsors/sindresorhus" + } + }, + "node_modules/google-auth-library": { + "version": "10.6.2", + "resolved": "https://registry.npmjs.org/google-auth-library/-/google-auth-library-10.6.2.tgz", + "integrity": "sha512-e27Z6EThmVNNvtYASwQxose/G57rkRuaRbQyxM2bvYLLX/GqWZ5chWq2EBoUchJbCc57eC9ArzO5wMsEmWftCw==", + "license": "Apache-2.0", + "dependencies": { + "base64-js": "^1.3.0", + "ecdsa-sig-formatter": "^1.0.11", + "gaxios": "^7.1.4", + "gcp-metadata": "8.1.2", + "google-logging-utils": "1.1.3", + "jws": "^4.0.0" + }, + "engines": { + "node": ">=18" + } + }, + "node_modules/google-logging-utils": { + "version": "1.1.3", + "resolved": "https://registry.npmjs.org/google-logging-utils/-/google-logging-utils-1.1.3.tgz", + "integrity": "sha512-eAmLkjDjAFCVXg7A1unxHsLf961m6y17QFqXqAXGj/gVkKFrEICfStRfwUlGNfeCEjNRa32JEWOUTlYXPyyKvA==", + "license": "Apache-2.0", + "engines": { + "node": ">=14" + } + }, + "node_modules/graceful-fs": { + "version": "4.2.11", + "resolved": "https://registry.npmjs.org/graceful-fs/-/graceful-fs-4.2.11.tgz", + "integrity": "sha512-RbJ5/jmFcNNCcDV5o9eTnBLJ/HszWV0P73bc+Ff4nS/rJj+YaS6IGyiOL0VoBYX+l1Wrl3k63h/KrH+nhJ0XvQ==", + "license": "ISC" + }, + "node_modules/grok-mermaid": { + "version": "0.2.3", + "resolved": "https://registry.npmjs.org/grok-mermaid/-/grok-mermaid-0.2.3.tgz", + "integrity": "sha512-/4KopAbsjvuRP9MdPtlDjOHUmUVEohOX73JNcsWpzAtFxh+bq5+Dhb6gzvRieLDwIPQIR3/vy8V1NNTuz4Zsmg==", + "license": "Apache-2.0", + "engines": { + "node": ">=18" + } + }, + "node_modules/highlight.js": { + "version": "10.7.3", + "resolved": "https://registry.npmjs.org/highlight.js/-/highlight.js-10.7.3.tgz", + "integrity": "sha512-tzcUFauisWKNHaRkN4Wjl/ZA07gENAjFl3J/c480dprkGTg5EQstgaNFqBfUqCq54kZRIEcreTsAgF/m2quD7A==", + "license": "BSD-3-Clause", + "engines": { + "node": "*" + } + }, + "node_modules/hosted-git-info": { + "version": "9.0.3", + "resolved": "https://registry.npmjs.org/hosted-git-info/-/hosted-git-info-9.0.3.tgz", + "integrity": "sha512-Hc+ghLoSt6QaYZUv0WBiIvmMDZuZZ7oaDvdH8MbfOO4lOsxdXLEvuC6ePoGs9H1X9oCLyq6+NVN0MKqD+ydxyg==", + "license": "ISC", + "dependencies": { + "lru-cache": "^11.1.0" + }, + "engines": { + "node": "^20.17.0 || >=22.9.0" + } + }, + "node_modules/http-proxy-agent": { + "version": "9.1.0", + "resolved": "https://registry.npmjs.org/http-proxy-agent/-/http-proxy-agent-9.1.0.tgz", + "integrity": "sha512-2NxoveTT58mjYT4n3RPTEfCZGLMbidoO8XEieXfpSYxu+PQJ1qpx4ypwH6N+uF9twBPIvRRgvkvW5HUTYWENig==", + "license": "MIT", + "dependencies": { + "agent-base": "9.0.0", + "debug": "^4.3.4", + "proxy-agent-negotiate": "1.1.0" + }, + "engines": { + "node": ">= 20" + } + }, + "node_modules/http-proxy-agent/node_modules/agent-base": { + "version": "9.0.0", + "resolved": "https://registry.npmjs.org/agent-base/-/agent-base-9.0.0.tgz", + "integrity": "sha512-TQf59BsZnytt8GdJKLPfUZ54g/iaUL2OWDSFCCvMOhsHduDQxO8xC4PNeyIkVcA5KwL2phPSv0douC0fgWzmnA==", + "license": "MIT", + "engines": { + "node": ">= 20" + } + }, + "node_modules/https-proxy-agent": { + "version": "7.0.6", + "resolved": "https://registry.npmjs.org/https-proxy-agent/-/https-proxy-agent-7.0.6.tgz", + "integrity": "sha512-vK9P5/iUfdl95AI+JVyUuIcVtd4ofvtrOr3HNtM2yxC9bnMbEdp3x01OhQNnjb8IJYi38VlTE3mBXwcfvywuSw==", + "license": "MIT", + "dependencies": { + "agent-base": "^7.1.2", + "debug": "4" + }, + "engines": { + "node": ">= 14" + } + }, + "node_modules/ignore": { + "version": "7.0.8", + "resolved": "https://registry.npmjs.org/ignore/-/ignore-7.0.8.tgz", + "integrity": "sha512-YYNsSlXBjMk92SKnkwvB5LOVSa6OznlFUGcsvrFgNJbJCd0M1XKeFVRc8ZByeCqz32FivYNHJVooLmdqrmvp/Q==", + "license": "MIT", + "engines": { + "node": ">= 4" + } + }, + "node_modules/isexe": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/isexe/-/isexe-2.0.0.tgz", + "integrity": "sha512-RHxMLp9lnKHGHRng9QFhRCMbYAcVpn69smSGcq3f36xjgVVWThj4qqLbTLlq7Ssj8B+fIQ1EuCEGI2lKsyQeIw==", + "license": "ISC" + }, + "node_modules/jiti": { + "version": "2.7.0", + "resolved": "https://registry.npmjs.org/jiti/-/jiti-2.7.0.tgz", + "integrity": "sha512-AC/7JofJvZGrrneWNaEnJeOLUx+JlGt7tNa0wZiRPT4MY1wmfKjt2+6O2p2uz2+skll8OZZmJMNqeke7kKbNgQ==", + "license": "MIT", + "bin": { + "jiti": "lib/jiti-cli.mjs" + } + }, + "node_modules/json-bigint": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/json-bigint/-/json-bigint-1.0.0.tgz", + "integrity": "sha512-SiPv/8VpZuWbvLSMtTDU8hEfrZWg/mH/nV/b4o0CYbSxu1UIQPLdwKOCIyLQX+VIPO5vrLX3i8qtqFyhdPSUSQ==", + "license": "MIT", + "dependencies": { + "bignumber.js": "^9.0.0" + } + }, + "node_modules/json-schema-to-ts": { + "version": "3.1.1", + "resolved": "https://registry.npmjs.org/json-schema-to-ts/-/json-schema-to-ts-3.1.1.tgz", + "integrity": "sha512-+DWg8jCJG2TEnpy7kOm/7/AxaYoaRbjVB4LFZLySZlWn8exGs3A4OLJR966cVvU26N7X9TWxl+Jsw7dzAqKT6g==", + "license": "MIT", + "dependencies": { + "@babel/runtime": "^7.18.3", + "ts-algebra": "^2.0.0" + }, + "engines": { + "node": ">=16" + } + }, + "node_modules/jwa": { + "version": "2.0.1", + "resolved": "https://registry.npmjs.org/jwa/-/jwa-2.0.1.tgz", + "integrity": "sha512-hRF04fqJIP8Abbkq5NKGN0Bbr3JxlQ+qhZufXVr0DvujKy93ZCbXZMHDL4EOtodSbCWxOqR8MS1tXA5hwqCXDg==", + "license": "MIT", + "dependencies": { + "buffer-equal-constant-time": "^1.0.1", + "ecdsa-sig-formatter": "1.0.11", + "safe-buffer": "^5.0.1" + } + }, + "node_modules/jws": { + "version": "4.0.1", + "resolved": "https://registry.npmjs.org/jws/-/jws-4.0.1.tgz", + "integrity": "sha512-EKI/M/yqPncGUUh44xz0PxSidXFr/+r0pA70+gIYhjv+et7yxM+s29Y+VGDkovRofQem0fs7Uvf4+YmAdyRduA==", + "license": "MIT", + "dependencies": { + "jwa": "^2.0.1", + "safe-buffer": "^5.0.1" + } + }, + "node_modules/long": { + "version": "5.3.2", + "resolved": "https://registry.npmjs.org/long/-/long-5.3.2.tgz", + "integrity": "sha512-mNAgZ1GmyNhD7AuqnTG3/VQ26o760+ZYBPKjPvugO8+nLbYfX6TVpJPseBvopbdY+qpZ/lKUnmEc1LeZYS3QAA==", + "license": "Apache-2.0" + }, + "node_modules/lru-cache": { + "version": "11.4.0", + "resolved": "https://registry.npmjs.org/lru-cache/-/lru-cache-11.4.0.tgz", + "integrity": "sha512-W+R+kFL4HgVxONq2bhXPi3bGpzGe/yEhVOp233qw9wCRtgncJ15P3bC+e4zZMu4Cq7d+WAJjXGW0uUkifhcatA==", + "license": "BlueOak-1.0.0", + "engines": { + "node": "20 || >=22" + } + }, + "node_modules/marked": { + "version": "18.0.11", + "resolved": "https://registry.npmjs.org/marked/-/marked-18.0.11.tgz", + "integrity": "sha512-HnslJfsZkRPBDJRHvVtAaWlZHEpSu7u8LgQuJCELjRKuWR+hpq4A7sLq3p8HaI9ypVoXDXxV34CsQJEe1+J5Aw==", + "license": "MIT", + "bin": { + "marked": "bin/marked.js" + }, + "engines": { + "node": ">= 20" + } + }, + "node_modules/minimatch": { + "version": "10.2.6", + "resolved": "https://registry.npmjs.org/minimatch/-/minimatch-10.2.6.tgz", + "integrity": "sha512-vpLQEs+VLCr1nU0BXS07maYoFwlDAH0gngQuuttxIwutDFEMHq2blX+8vpgxDdK3J1PwjCJiep77OitTZ4Ll1A==", + "license": "BlueOak-1.0.0", + "dependencies": { + "brace-expansion": "^5.0.8" + }, + "engines": { + "node": "18 || 20 || >=22" + }, + "funding": { + "url": "https://github.com/sponsors/isaacs" + } + }, + "node_modules/ms": { + "version": "2.1.3", + "resolved": "https://registry.npmjs.org/ms/-/ms-2.1.3.tgz", + "integrity": "sha512-6FlzubTLZG3J2a/NVCAleEhjzq5oxgHyaCU9yYXvcLsvoVaHJq/s5xXI6/XXP6tz7R9xAOtHnSO/tXtF3WRTlA==", + "license": "MIT" + }, + "node_modules/node-domexception": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/node-domexception/-/node-domexception-1.0.0.tgz", + "integrity": "sha512-/jKZoMpw0F8GRwl4/eLROPA3cfcXtLApP0QzLmUT/HuPCZWyB7IY9ZrMeKw2O/nFIqPQB3PVM9aYm0F312AXDQ==", + "license": "MIT", + "engines": { + "node": ">=10.5.0" + }, + "deprecated": "Use your platform's native DOMException instead", + "funding": [ + { + "type": "github", + "url": "https://github.com/sponsors/jimmywarting" + }, + { + "type": "github", + "url": "https://paypal.me/jimmywarting" + } + ] + }, + "node_modules/node-fetch": { + "version": "3.3.2", + "resolved": "https://registry.npmjs.org/node-fetch/-/node-fetch-3.3.2.tgz", + "integrity": "sha512-dRB78srN/l6gqWulah9SrxeYnxeddIG30+GOqK/9OlLVyLg3HPnr6SqOWTWOXKRwC2eGYCkZ59NNuSgvSrpgOA==", + "license": "MIT", + "dependencies": { + "data-uri-to-buffer": "^4.0.0", + "fetch-blob": "^3.1.4", + "formdata-polyfill": "^4.0.10" + }, + "engines": { + "node": "^12.20.0 || ^14.13.1 || >=16.0.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/node-fetch" + } + }, + "node_modules/openai": { + "version": "6.40.0", + "resolved": "https://registry.npmjs.org/openai/-/openai-6.40.0.tgz", + "integrity": "sha512-MWtTjd/gQt4jpbji61NTgFWJLoY/PdRJ6wG9/ZDRMYNMlBKrCrSlkLI+KgHP1vR1qT6LKSAyAqIxno6lcK9JiA==", + "license": "Apache-2.0", + "peerDependencies": { + "ws": "^8.18.0", + "zod": "^3.25 || ^4.0" + }, + "peerDependenciesMeta": { + "ws": { + "optional": true + }, + "zod": { + "optional": true + } + } + }, + "node_modules/p-retry": { + "version": "4.6.2", + "resolved": "https://registry.npmjs.org/p-retry/-/p-retry-4.6.2.tgz", + "integrity": "sha512-312Id396EbJdvRONlngUx0NydfrIQ5lsYu0znKVUzVvArzEIt08V1qhtyESbGVd1FGX7UKtiFp5uwKZdM8wIuQ==", + "license": "MIT", + "dependencies": { + "@types/retry": "0.12.0", + "retry": "^0.13.1" + }, + "engines": { + "node": ">=8" + } + }, + "node_modules/p-retry/node_modules/@types/retry": { + "version": "0.12.0", + "resolved": "https://registry.npmjs.org/@types/retry/-/retry-0.12.0.tgz", + "integrity": "sha512-wWKOClTTiizcZhXnPY4wikVAwmdYHp8q6DmC+EJUzAMsycb7HB32Kh9RN4+0gExjmPmZSAQjgURXIGATPegAvA==", + "license": "MIT" + }, + "node_modules/partial-json": { + "version": "0.1.7", + "resolved": "https://registry.npmjs.org/partial-json/-/partial-json-0.1.7.tgz", + "integrity": "sha512-Njv/59hHaokb/hRUjce3Hdv12wd60MtM9Z5Olmn+nehe0QDAsRtRbJPvJ0Z91TusF0SuZRIvnM+S4l6EIP8leA==", + "license": "MIT" + }, + "node_modules/path-key": { + "version": "3.1.1", + "resolved": "https://registry.npmjs.org/path-key/-/path-key-3.1.1.tgz", + "integrity": "sha512-ojmeN0qd+y0jszEtoY48r0Peq5dwMEkIlCOu6Q5f41lfkswXuKtYrhgoTpLnyIcHm24Uhqx+5Tqm2InSwLhE6Q==", + "license": "MIT", + "engines": { + "node": ">=8" + } + }, + "node_modules/proper-lockfile": { + "version": "4.1.2", + "resolved": "https://registry.npmjs.org/proper-lockfile/-/proper-lockfile-4.1.2.tgz", + "integrity": "sha512-TjNPblN4BwAWMXU8s9AEz4JmQxnD1NNL7bNOY/AKUzyamc379FWASUhc/K1pL2noVb+XmZKLL68cjzLsiOAMaA==", + "license": "MIT", + "dependencies": { + "graceful-fs": "^4.2.4", + "retry": "^0.12.0", + "signal-exit": "^3.0.2" + } + }, + "node_modules/proper-lockfile/node_modules/retry": { + "version": "0.12.0", + "resolved": "https://registry.npmjs.org/retry/-/retry-0.12.0.tgz", + "integrity": "sha512-9LkiTwjUh6rT555DtE9rTX+BKByPfrMzEAtnlEtdEwr3Nkffwiihqe2bWADg+OQRjt9gl6ICdmB/ZFDCGAtSow==", + "license": "MIT", + "engines": { + "node": ">= 4" + } + }, + "node_modules/protobufjs": { + "version": "7.6.6", + "resolved": "https://registry.npmjs.org/protobufjs/-/protobufjs-7.6.6.tgz", + "integrity": "sha512-dYDWdjSl5RNb7SgPxGQcRU+GtvP7s2fpkrY0r432PcOIaZ0/rBcxEZnQN67iJhFuQiVw754JDoPruPCNdGsbjg==", + "license": "BSD-3-Clause", + "dependencies": { + "@protobufjs/aspromise": "^1.1.2", + "@protobufjs/base64": "^1.1.2", + "@protobufjs/codegen": "^2.0.5", + "@protobufjs/eventemitter": "^1.1.1", + "@protobufjs/fetch": "^1.1.1", + "@protobufjs/float": "^1.0.2", + "@protobufjs/path": "^1.1.2", + "@protobufjs/pool": "^1.1.0", + "@protobufjs/utf8": "^1.1.1", + "@types/node": ">=13.7.0", + "long": "^5.3.2" + }, + "engines": { + "node": ">=12.0.0" + }, + "hasInstallScript": true + }, + "node_modules/proxy-agent-negotiate": { + "version": "1.1.0", + "resolved": "https://registry.npmjs.org/proxy-agent-negotiate/-/proxy-agent-negotiate-1.1.0.tgz", + "integrity": "sha512-N8IBcM3UgCVzz2L2Lqv8DVntDnnC8/hiV4nEDUPkqq72TPUgYWjQc+bdZlBPZK9LzPAvOY//gAt0S0DApoOXWQ==", + "license": "MIT", + "peerDependencies": { + "kerberos": "^2.0.0" + }, + "peerDependenciesMeta": { + "kerberos": { + "optional": true + } + }, + "engines": { + "node": ">= 20" + } + }, + "node_modules/retry": { + "version": "0.13.1", + "resolved": "https://registry.npmjs.org/retry/-/retry-0.13.1.tgz", + "integrity": "sha512-XQBQ3I8W1Cge0Seh+6gjj03LbmRFWuoszgK9ooCpwYIrhhoO80pfq4cUkU5DkknwfOfFteRwlZ56PYOGYyFWdg==", + "license": "MIT", + "engines": { + "node": ">= 4" + } + }, + "node_modules/safe-buffer": { + "version": "5.2.1", + "resolved": "https://registry.npmjs.org/safe-buffer/-/safe-buffer-5.2.1.tgz", + "integrity": "sha512-rp3So07KcdmmKbGvgaNxQSJr7bGVSVk5S9Eq1F+ppbRo70+YeaDxkw5Dd8NPN+GD6bjnYm2VuPuCXmpuYvmCXQ==", + "license": "MIT", + "funding": [ + { + "type": "github", + "url": "https://github.com/sponsors/feross" + }, + { + "type": "patreon", + "url": "https://www.patreon.com/feross" + }, + { + "type": "consulting", + "url": "https://feross.org/support" + } + ] + }, + "node_modules/semver": { + "version": "7.8.5", + "resolved": "https://registry.npmjs.org/semver/-/semver-7.8.5.tgz", + "integrity": "sha512-Y7/KDsb8LjooZpwaqGyulO6DQlksgCncchHGk+sZIY4SBvUocMBEFH5Ur1fI4dV+Jvl0w6cjvucaIi40puRioA==", + "license": "ISC", + "bin": { + "semver": "bin/semver.js" + }, + "engines": { + "node": ">=10" + } + }, + "node_modules/shebang-command": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/shebang-command/-/shebang-command-2.0.0.tgz", + "integrity": "sha512-kHxr2zZpYtdmrN1qDjrrX/Z1rR1kG8Dx+gkpK1G4eXmvXswmcE1hTWBWYUzlraYw1/yZp6YuDY77YtvbN0dmDA==", + "license": "MIT", + "dependencies": { + "shebang-regex": "^3.0.0" + }, + "engines": { + "node": ">=8" + } + }, + "node_modules/shebang-regex": { + "version": "3.0.0", + "resolved": "https://registry.npmjs.org/shebang-regex/-/shebang-regex-3.0.0.tgz", + "integrity": "sha512-7++dFhtcx3353uBaq8DDR4NuxBetBzC7ZQOhmTQInHEd6bSrXdiEyzCvG07Z44UYdLShWUyXt5M/yhz8ekcb1A==", + "license": "MIT", + "engines": { + "node": ">=8" + } + }, + "node_modules/signal-exit": { + "version": "3.0.7", + "resolved": "https://registry.npmjs.org/signal-exit/-/signal-exit-3.0.7.tgz", + "integrity": "sha512-wnD2ZE+l+SPC/uoS0vXeE9L1+0wuaMqKlfz9AMUo38JsyLSBWSFcHR1Rri62LZc12vLr1gb3jl7iwQhgwpAbGQ==", + "license": "ISC" + }, + "node_modules/standardwebhooks": { + "version": "1.1.1", + "resolved": "https://registry.npmjs.org/standardwebhooks/-/standardwebhooks-1.1.1.tgz", + "integrity": "sha512-bCbX9ZEyFkWPsRz7Bl3NuQUJohmwGSev/yhr7vhaGPlc4AfIrspIRa6cPTBuI1ItmrTDJ4d/S2hCsfe4+vQGnQ==", + "license": "MIT", + "dependencies": { + "@stablelib/base64": "^1.0.0", + "fast-sha256": "^1.3.0" + } + }, + "node_modules/ts-algebra": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/ts-algebra/-/ts-algebra-2.0.0.tgz", + "integrity": "sha512-FPAhNPFMrkwz76P7cdjdmiShwMynZYN6SgOujD1urY4oNm80Ou9oMdmbR45LotcKOXoy7wSmHkRFE6Mxbrhefw==", + "license": "MIT" + }, + "node_modules/tslib": { + "version": "2.8.1", + "resolved": "https://registry.npmjs.org/tslib/-/tslib-2.8.1.tgz", + "integrity": "sha512-oJFu94HQb+KVduSUQL7wnpmqnfmLsOA/nAh6b6EH0wCEoK0/mPeXU6c3wKDV83MkOuHPRHtSXKKU99IBazS/2w==", + "license": "0BSD" + }, + "node_modules/typebox": { + "version": "1.3.27", + "resolved": "https://registry.npmjs.org/typebox/-/typebox-1.3.27.tgz", + "integrity": "sha512-zu+jc1pcy4UiNThxikUr36f0Rybk9PEeCg/NE6adeWr/SKsdNO4EzZHYRDlv2YCVAfj3Odq3dESSo/jNyoBXzA==", + "license": "MIT" + }, + "node_modules/undici": { + "version": "8.10.2", + "resolved": "https://registry.npmjs.org/undici/-/undici-8.10.2.tgz", + "integrity": "sha512-/y4/bH9YNU5hi9NIrpOuvGXFcxrj3CMrV+/AYpowAYTpHn8gX/XPFjNy766FPoYY0miQhdW977JFWKGNhBdwyQ==", + "license": "MIT", + "engines": { + "node": ">=22.19.0" + } + }, + "node_modules/undici-types": { + "version": "6.21.0", + "resolved": "https://registry.npmjs.org/undici-types/-/undici-types-6.21.0.tgz", + "integrity": "sha512-iwDZqg0QAGrg9Rav5H4n0M64c3mkR59cJ6wQp+7C4nI0gsmExaedaYLNO44eT4AtBBwjbTiGPMlt2Md0T9H9JQ==", + "license": "MIT" + }, + "node_modules/web-streams-polyfill": { + "version": "3.3.3", + "resolved": "https://registry.npmjs.org/web-streams-polyfill/-/web-streams-polyfill-3.3.3.tgz", + "integrity": "sha512-d2JWLCivmZYTSIoge9MsgFCZrt571BikcWGYkjC1khllbTeDlGqZ2D8vD8E/lJa8WGWbb7Plm8/XJYV7IJHZZw==", + "license": "MIT", + "engines": { + "node": ">= 8" + } + }, + "node_modules/which": { + "version": "2.0.2", + "resolved": "https://registry.npmjs.org/which/-/which-2.0.2.tgz", + "integrity": "sha512-BLI3Tl1TW3Pvl70l3yq3Y64i+awpwXqsGBYWkkqMtnbXgrMD+yj7rhW0kuEDxzJaYXGjEW5ogapKNMEKNMjibA==", + "license": "ISC", + "dependencies": { + "isexe": "^2.0.0" + }, + "bin": { + "node-which": "bin/node-which" + }, + "engines": { + "node": ">= 8" + } + }, + "node_modules/ws": { + "version": "8.21.0", + "resolved": "https://registry.npmjs.org/ws/-/ws-8.21.0.tgz", + "integrity": "sha512-Vsp28b7DRcimFQvrqu2Wek3z1iYxDCWqHYB8Qsnk/S4RfaCQzPGPyBNuVjJV3cd6UiKtUtp6sNM77gWvzcCH+g==", + "license": "MIT", + "peerDependencies": { + "bufferutil": "^4.0.1", + "utf-8-validate": ">=5.0.2" + }, + "peerDependenciesMeta": { + "bufferutil": { + "optional": true + }, + "utf-8-validate": { + "optional": true + } + }, + "engines": { + "node": ">=10.0.0" + } + }, + "node_modules/yaml": { + "version": "2.9.0", + "resolved": "https://registry.npmjs.org/yaml/-/yaml-2.9.0.tgz", + "integrity": "sha512-2AvhNX3mb8zd6Zy7INTtSpl1F15HW6Wnqj0srWlkKLcpYl/gMIMJiyuGq2KeI2YFxUPjdlB+3Lc10seMLtL4cA==", + "license": "ISC", + "bin": { + "yaml": "bin.mjs" + }, + "engines": { + "node": ">= 14.6" + }, + "funding": { + "url": "https://github.com/sponsors/eemeli" + } + } + } +} diff --git a/packages/coding-agent/install-lock/package.json b/packages/coding-agent/install-lock/package.json new file mode 100644 index 0000000000000000000000000000000000000000..76dec54a15fecd1e15593cf27a2971a4e771a4f4 --- /dev/null +++ b/packages/coding-agent/install-lock/package.json @@ -0,0 +1,15 @@ +{ + "name": "@earendil-works/pi-coding-agent-install", + "version": "0.85.1", + "private": true, + "description": "Lockfile root used by the Pi installer and updater.", + "dependencies": { + "@earendil-works/pi-coding-agent": "0.85.1" + }, + "overrides": { + "protobufjs": "7.6.6" + }, + "engines": { + "node": ">=22.19.0" + } +} diff --git a/packages/coding-agent/src/core/compaction/branch-summarization.ts b/packages/coding-agent/src/core/compaction/branch-summarization.ts new file mode 100644 index 0000000000000000000000000000000000000000..1e027c65c4b06a07242654a50674abc7490c1e65 --- /dev/null +++ b/packages/coding-agent/src/core/compaction/branch-summarization.ts @@ -0,0 +1,382 @@ +/** + * Branch summarization for tree navigation. + * + * When navigating to a different point in the session tree, this generates + * a summary of the branch being left so context isn't lost. + */ + +import type { AgentMessage, StreamFn } from "@earendil-works/pi-agent-core"; +import type { RetryCallbacks, RetryPolicy } from "@earendil-works/pi-ai"; +import { contentText } from "@earendil-works/pi-ai"; +import type { Model, SimpleStreamOptions, Usage } from "@earendil-works/pi-ai/compat"; +import { + convertToLlm, + createBranchSummaryMessage, + createCompactionSummaryMessage, + createCustomMessage, +} from "../messages.ts"; +import type { ReadonlySessionManager, SessionEntry } from "../session-manager.ts"; +import { completeSummarization, estimateTokens, getSummarizationFailure } from "./compaction.ts"; +import { + computeFileLists, + createFileOps, + extractFileOpsFromMessage, + type FileOperations, + formatFileOperations, + SUMMARIZATION_SYSTEM_PROMPT, + serializeConversation, +} from "./utils.ts"; + +// ============================================================================ +// Types +// ============================================================================ + +export interface BranchSummaryResult { + summary?: string; + usage?: Usage; + readFiles?: string[]; + modifiedFiles?: string[]; + aborted?: boolean; + error?: string; +} + +/** Details stored in BranchSummaryEntry.details for file tracking */ +export interface BranchSummaryDetails { + readFiles: string[]; + modifiedFiles: string[]; +} + +export type { FileOperations } from "./utils.ts"; + +export interface BranchPreparation { + /** Messages extracted for summarization, in chronological order */ + messages: AgentMessage[]; + /** File operations extracted from tool calls */ + fileOps: FileOperations; + /** Total estimated tokens in messages */ + totalTokens: number; +} + +export interface CollectEntriesResult { + /** Entries to summarize, in chronological order */ + entries: SessionEntry[]; + /** Common ancestor between old and new position, if any */ + commonAncestorId: string | null; +} + +export interface GenerateBranchSummaryOptions { + /** Model to use for summarization */ + model: Model; + /** API key for the model */ + apiKey?: string; + /** Request headers for the model */ + headers?: Record; + /** Provider-scoped environment values for the model */ + env?: Record; + /** Abort signal for cancellation */ + signal: AbortSignal; + /** Optional custom instructions for summarization */ + customInstructions?: string; + /** If true, customInstructions replaces the default prompt instead of being appended */ + replaceInstructions?: boolean; + /** Tokens reserved when selecting branch history (default 16384) */ + reserveTokens?: number; + /** Optional session stream function. Used to preserve SDK request behavior without mutating agent state. */ + streamFn?: StreamFn; + /** Retry policy for transient summarization errors. Reuses coding-agent's `settings.retry`. */ + retry?: RetryPolicy; + /** Optional callbacks for retry reporting (e.g. TUI retry indicators). */ + callbacks?: RetryCallbacks; +} + +// ============================================================================ +// Entry Collection +// ============================================================================ + +/** + * Collect entries that should be summarized when navigating from one position to another. + * + * Walks from oldLeafId back to the common ancestor with targetId, collecting entries + * along the way. Does NOT stop at compaction boundaries - those are included and their + * summaries become context. + * + * @param session - Session manager (read-only access) + * @param oldLeafId - Current position (where we're navigating from) + * @param targetId - Target position (where we're navigating to) + * @returns Entries to summarize and the common ancestor + */ +export function collectEntriesForBranchSummary( + session: ReadonlySessionManager, + oldLeafId: string | null, + targetId: string, +): CollectEntriesResult { + // If no old position, nothing to summarize + if (!oldLeafId) { + return { entries: [], commonAncestorId: null }; + } + + // Find common ancestor (deepest node that's on both paths) + const oldPath = new Set(session.getBranch(oldLeafId).map((e) => e.id)); + const targetPath = session.getBranch(targetId); + + // targetPath is root-first, so iterate backwards to find deepest common ancestor + let commonAncestorId: string | null = null; + for (let i = targetPath.length - 1; i >= 0; i--) { + if (oldPath.has(targetPath[i].id)) { + commonAncestorId = targetPath[i].id; + break; + } + } + + // Collect entries from old leaf back to common ancestor + const entries: SessionEntry[] = []; + let current: string | null = oldLeafId; + + while (current && current !== commonAncestorId) { + const entry = session.getEntry(current); + if (!entry) break; + entries.push(entry); + current = entry.parentId; + } + + // Reverse to get chronological order + entries.reverse(); + + return { entries, commonAncestorId }; +} + +// ============================================================================ +// Entry to Message Conversion +// ============================================================================ + +/** + * Extract AgentMessage from a session entry. + * Similar to getMessageFromEntry in compaction.ts but also handles compaction entries. + */ +function getMessageFromEntry(entry: SessionEntry): AgentMessage | undefined { + switch (entry.type) { + case "message": + // Skip tool results - context is in assistant's tool call + if (entry.message.role === "toolResult") return undefined; + return entry.message; + + case "custom_message": + return createCustomMessage(entry.customType, entry.content, entry.display, entry.details, entry.timestamp); + + case "branch_summary": + return createBranchSummaryMessage(entry.summary, entry.fromId, entry.timestamp); + + case "compaction": + return createCompactionSummaryMessage(entry.summary, entry.tokensBefore, entry.timestamp); + + // These don't contribute to conversation content + case "thinking_level_change": + case "model_change": + case "custom": + case "label": + case "session_info": + return undefined; + } +} + +/** + * Prepare entries for summarization with token budget. + * + * Walks entries from NEWEST to OLDEST, adding messages until we hit the token budget. + * This ensures we keep the most recent context when the branch is too long. + * + * Also collects file operations from: + * - Tool calls in assistant messages + * - Existing branch_summary entries' details (for cumulative tracking) + * + * @param entries - Entries in chronological order + * @param tokenBudget - Maximum tokens to include (0 = no limit) + */ +export function prepareBranchEntries(entries: SessionEntry[], tokenBudget: number = 0): BranchPreparation { + const messages: AgentMessage[] = []; + const fileOps = createFileOps(); + let totalTokens = 0; + + // First pass: collect file ops from ALL entries (even if they don't fit in token budget) + // This ensures we capture cumulative file tracking from nested branch summaries + // Only extract from pi-generated summaries (fromHook !== true), not extension-generated ones + for (const entry of entries) { + if (entry.type === "branch_summary" && !entry.fromHook && entry.details) { + const details = entry.details as BranchSummaryDetails; + if (Array.isArray(details.readFiles)) { + for (const f of details.readFiles) fileOps.read.add(f); + } + if (Array.isArray(details.modifiedFiles)) { + // Modified files go into both edited and written for proper deduplication + for (const f of details.modifiedFiles) { + fileOps.edited.add(f); + } + } + } + } + + // Second pass: walk from newest to oldest, adding messages until token budget + for (let i = entries.length - 1; i >= 0; i--) { + const entry = entries[i]; + const message = getMessageFromEntry(entry); + if (!message) continue; + + // Extract file ops from assistant messages (tool calls) + extractFileOpsFromMessage(message, fileOps); + + const tokens = estimateTokens(message); + + // Check budget before adding + if (tokenBudget > 0 && totalTokens + tokens > tokenBudget) { + // If this is a summary entry, try to fit it anyway as it's important context + if (entry.type === "compaction" || entry.type === "branch_summary") { + if (totalTokens < tokenBudget * 0.9) { + messages.unshift(message); + totalTokens += tokens; + } + } + // Stop - we've hit the budget + break; + } + + messages.unshift(message); + totalTokens += tokens; + } + + return { messages, fileOps, totalTokens }; +} + +// ============================================================================ +// Summary Generation +// ============================================================================ + +const BRANCH_SUMMARY_PREAMBLE = `The user explored a different conversation branch before returning here. +Summary of that exploration: + +`; + +const BRANCH_SUMMARY_PROMPT = `Create a structured summary of this conversation branch for context when returning later. + +Use this EXACT format: + +## Goal +[What was the user trying to accomplish in this branch?] + +## Constraints & Preferences +- [Any constraints, preferences, or requirements mentioned] +- [Or "(none)" if none were mentioned] + +## Progress +### Done +- [x] [Completed tasks/changes] + +### In Progress +- [ ] [Work that was started but not finished] + +### Blocked +- [Issues preventing progress, if any] + +## Key Decisions +- **[Decision]**: [Brief rationale] + +## Next Steps +1. [What should happen next to continue this work] + +Keep each section concise. Preserve exact file paths, function names, and error messages.`; + +/** + * Generate a summary of abandoned branch entries. + * + * @param entries - Session entries to summarize (chronological order) + * @param options - Generation options + */ +export async function generateBranchSummary( + entries: SessionEntry[], + options: GenerateBranchSummaryOptions, +): Promise { + const { + model, + apiKey, + headers, + env, + signal, + customInstructions, + replaceInstructions, + reserveTokens = 16384, + streamFn, + retry, + callbacks, + } = options; + + // Token budget = context window minus reserved space for prompt + response + const contextWindow = model.contextWindow || 128000; + const tokenBudget = contextWindow - reserveTokens; + + const { messages, fileOps } = prepareBranchEntries(entries, tokenBudget); + + if (messages.length === 0) { + return { summary: "No content to summarize" }; + } + + // Transform to LLM-compatible messages, then serialize to text + // Serialization prevents the model from treating it as a conversation to continue + const llmMessages = convertToLlm(messages); + const conversationText = serializeConversation(llmMessages); + + // Build prompt + let instructions: string; + if (replaceInstructions && customInstructions) { + instructions = customInstructions; + } else if (customInstructions) { + instructions = `${BRANCH_SUMMARY_PROMPT}\n\nAdditional focus: ${customInstructions}`; + } else { + instructions = BRANCH_SUMMARY_PROMPT; + } + const promptText = `\n${conversationText}\n\n\n${instructions}`; + + const summarizationMessages = [ + { + role: "user" as const, + content: [{ type: "text" as const, text: promptText }], + timestamp: Date.now(), + }, + ]; + + const maxTokens = Math.min(4096, model.maxTokens > 0 ? model.maxTokens : Number.POSITIVE_INFINITY); + + // Call LLM for summarization. Prefer the session stream function so SDK + // request behavior (timeouts, retries, attribution headers) stays consistent + // without running through agent state/events. Retried via completeSummarization + // so transient stream drops reuse the configured retry policy. + const context = { systemPrompt: SUMMARIZATION_SYSTEM_PROMPT, messages: summarizationMessages }; + const requestOptions: SimpleStreamOptions = { apiKey, headers, env, signal, maxTokens }; + const response = await completeSummarization(model, context, requestOptions, streamFn, retry, callbacks); + + // Check if aborted or errored + if (response.stopReason === "aborted") { + return { aborted: true }; + } + const failure = getSummarizationFailure(response, "Branch summarization"); + if (failure) { + return { error: failure }; + } + if (response.content.some((block) => block.type === "toolCall")) { + return { error: "Branch summarization attempted to call a tool" }; + } + + let summary = contentText(response.content); + + // Prepend preamble to provide context about the branch summary + summary = BRANCH_SUMMARY_PREAMBLE + summary; + + // Compute file lists and append to summary + const { readFiles, modifiedFiles } = computeFileLists(fileOps); + summary += formatFileOperations(readFiles, modifiedFiles); + + return { + summary: summary || "No summary generated", + usage: response.usage, + readFiles, + modifiedFiles, + }; +} diff --git a/packages/coding-agent/src/core/compaction/compaction.ts b/packages/coding-agent/src/core/compaction/compaction.ts new file mode 100644 index 0000000000000000000000000000000000000000..6ced927cab8ece071e022a35e22dd7e6fa160f05 --- /dev/null +++ b/packages/coding-agent/src/core/compaction/compaction.ts @@ -0,0 +1,1012 @@ +/** + * Context compaction for long sessions. + * + * Pure functions for compaction logic. The session manager handles I/O, + * and after compaction the session is reloaded. + */ + +import type { AgentMessage, StreamFn, ThinkingLevel } from "@earendil-works/pi-agent-core"; +import { contentText, type RetryCallbacks, type RetryPolicy, retryAssistantCall, uuidv7 } from "@earendil-works/pi-ai"; +import type { AssistantMessage, Context, Model, SimpleStreamOptions, Usage } from "@earendil-works/pi-ai/compat"; +import { completeSimple } from "@earendil-works/pi-ai/compat"; +import { convertToLlm } from "../messages.ts"; +import { + buildSessionContext, + type CompactionEntry, + type SessionEntry, + sessionEntryToContextMessages, +} from "../session-manager.ts"; +import { + computeFileLists, + createFileOps, + extractFileOpsFromMessage, + type FileOperations, + formatFileOperations, + SUMMARIZATION_SYSTEM_PROMPT, + serializeConversation, +} from "./utils.ts"; + +// ============================================================================ +// File Operation Tracking +// ============================================================================ + +/** Details stored in CompactionEntry.details for file tracking */ +export interface CompactionDetails { + readFiles: string[]; + modifiedFiles: string[]; +} + +/** + * Extract file operations from messages and previous compaction entries. + */ +function extractFileOperations( + messages: AgentMessage[], + entries: SessionEntry[], + prevCompactionIndex: number, +): FileOperations { + const fileOps = createFileOps(); + + // Collect from previous compaction's details (if pi-generated) + if (prevCompactionIndex >= 0) { + const prevCompaction = entries[prevCompactionIndex] as CompactionEntry; + if (!prevCompaction.fromHook && prevCompaction.details) { + // fromHook field kept for session file compatibility + const details = prevCompaction.details as CompactionDetails; + if (Array.isArray(details.readFiles)) { + for (const f of details.readFiles) fileOps.read.add(f); + } + if (Array.isArray(details.modifiedFiles)) { + for (const f of details.modifiedFiles) fileOps.edited.add(f); + } + } + } + + // Extract from tool calls in messages + for (const msg of messages) { + extractFileOpsFromMessage(msg, fileOps); + } + + return fileOps; +} + +// ============================================================================ +// Message Extraction +// ============================================================================ + +/** + * Extract AgentMessage from an entry if it produces one. + * Returns undefined for entries that don't contribute to LLM context. + */ +function getMessageFromEntryForCompaction(entry: SessionEntry): AgentMessage | undefined { + if (entry.type === "compaction") { + return undefined; + } + return sessionEntryToContextMessages(entry)[0]; +} + +/** Result from compact() - SessionManager adds uuid/parentUuid when saving */ +export interface CompactionResult { + summary: string; + firstKeptEntryId: string; + tokensBefore: number; + estimatedTokensAfter?: number; + /** Usage from the LLM call(s) that generated this summary, if available */ + usage?: Usage; + /** Extension-specific data (e.g., ArtifactIndex, version markers for structured compaction) */ + details?: T; +} + +function combineUsage(first: Usage, second: Usage): Usage { + return { + input: first.input + second.input, + output: first.output + second.output, + cacheRead: first.cacheRead + second.cacheRead, + cacheWrite: first.cacheWrite + second.cacheWrite, + ...(first.cacheWrite1h !== undefined || second.cacheWrite1h !== undefined + ? { cacheWrite1h: (first.cacheWrite1h ?? 0) + (second.cacheWrite1h ?? 0) } + : {}), + ...(first.reasoning !== undefined || second.reasoning !== undefined + ? { reasoning: (first.reasoning ?? 0) + (second.reasoning ?? 0) } + : {}), + totalTokens: first.totalTokens + second.totalTokens, + cost: { + input: first.cost.input + second.cost.input, + output: first.cost.output + second.cost.output, + cacheRead: first.cost.cacheRead + second.cost.cacheRead, + cacheWrite: first.cost.cacheWrite + second.cost.cacheWrite, + total: first.cost.total + second.cost.total, + }, + }; +} + +// ============================================================================ +// Types +// ============================================================================ + +export interface CompactionSettings { + enabled: boolean; + reserveTokens: number; + keepRecentTokens: number; +} + +export const DEFAULT_COMPACTION_SETTINGS: CompactionSettings = { + enabled: true, + reserveTokens: 16384, + keepRecentTokens: 20000, +}; + +// ============================================================================ +// Token calculation +// ============================================================================ + +/** + * Calculate total context tokens from usage. + * Uses the native totalTokens field when available, falls back to computing from components. + */ +export function calculateContextTokens(usage: Usage): number { + return usage.totalTokens || usage.input + usage.output + usage.cacheRead + usage.cacheWrite; +} + +/** + * Get usage from an assistant message if available. + * Skips aborted, error, and all-zero usage messages as they don't have valid usage data. + */ +function getAssistantUsage(msg: AgentMessage): Usage | undefined { + if (msg.role === "assistant" && "usage" in msg) { + const assistantMsg = msg as AssistantMessage; + if ( + assistantMsg.stopReason !== "aborted" && + assistantMsg.stopReason !== "error" && + assistantMsg.usage && + calculateContextTokens(assistantMsg.usage) > 0 + ) { + return assistantMsg.usage; + } + } + return undefined; +} + +/** + * Find the last valid assistant message usage from session entries. + */ +export function getLastAssistantUsage(entries: SessionEntry[]): Usage | undefined { + for (let i = entries.length - 1; i >= 0; i--) { + const entry = entries[i]; + if (entry.type === "message") { + const usage = getAssistantUsage(entry.message); + if (usage) return usage; + } + } + return undefined; +} + +export interface ContextUsageEstimate { + tokens: number; + usageTokens: number; + trailingTokens: number; + lastUsageIndex: number | null; +} + +function getLastAssistantUsageInfo(messages: AgentMessage[]): { usage: Usage; index: number } | undefined { + for (let i = messages.length - 1; i >= 0; i--) { + const usage = getAssistantUsage(messages[i]); + if (usage) return { usage, index: i }; + } + return undefined; +} + +/** + * Estimate context tokens from messages, using the last assistant usage when available. + * If there are messages after the last usage, estimate their tokens with estimateTokens. + */ +export function estimateContextTokens(messages: AgentMessage[]): ContextUsageEstimate { + const usageInfo = getLastAssistantUsageInfo(messages); + + if (!usageInfo) { + let estimated = 0; + for (const message of messages) { + estimated += estimateTokens(message); + } + return { + tokens: estimated, + usageTokens: 0, + trailingTokens: estimated, + lastUsageIndex: null, + }; + } + + const usageTokens = calculateContextTokens(usageInfo.usage); + let trailingTokens = 0; + for (let i = usageInfo.index + 1; i < messages.length; i++) { + trailingTokens += estimateTokens(messages[i]); + } + + return { + tokens: usageTokens + trailingTokens, + usageTokens, + trailingTokens, + lastUsageIndex: usageInfo.index, + }; +} + +/** + * Check if compaction should trigger based on context usage. + */ +export function shouldCompact(contextTokens: number, contextWindow: number, settings: CompactionSettings): boolean { + if (!settings.enabled) return false; + return contextTokens > contextWindow - settings.reserveTokens; +} + +// ============================================================================ +// Cut point detection +// ============================================================================ + +const ESTIMATED_IMAGE_CHARS = 4800; + +function estimateTextAndImageContentChars(content: string | Array<{ type: string; text?: string }>): number { + if (typeof content === "string") { + return content.length; + } + + let chars = 0; + for (const block of content) { + if (block.type === "text" && block.text) { + chars += block.text.length; + } else if (block.type === "image") { + chars += ESTIMATED_IMAGE_CHARS; + } + } + return chars; +} + +/** + * Estimate token count for a message using chars/4 heuristic. + * This is conservative (overestimates tokens). + */ +export function estimateTokens(message: AgentMessage): number { + let chars = 0; + + switch (message.role) { + case "user": { + chars = estimateTextAndImageContentChars( + (message as { content: string | Array<{ type: string; text?: string }> }).content, + ); + return Math.ceil(chars / 4); + } + case "assistant": { + const assistant = message as AssistantMessage; + for (const block of assistant.content) { + if (block.type === "text") { + chars += block.text.length; + } else if (block.type === "thinking") { + chars += block.thinking.length; + } else if (block.type === "toolCall") { + chars += block.name.length + JSON.stringify(block.arguments).length; + } + } + return Math.ceil(chars / 4); + } + case "custom": + case "toolResult": { + chars = estimateTextAndImageContentChars(message.content); + return Math.ceil(chars / 4); + } + case "bashExecution": { + chars = message.command.length + message.output.length; + return Math.ceil(chars / 4); + } + case "branchSummary": + case "compactionSummary": { + chars = message.summary.length; + return Math.ceil(chars / 4); + } + } + + return 0; +} + +function isCutPointMessage(message: AgentMessage): boolean { + switch (message.role) { + case "user": + case "assistant": + case "bashExecution": + case "custom": + case "branchSummary": + case "compactionSummary": + return true; + case "toolResult": + return false; + } + return false; +} + +function isTurnStartMessage(message: AgentMessage): boolean { + switch (message.role) { + case "user": + case "bashExecution": + case "custom": + case "branchSummary": + case "compactionSummary": + return true; + case "assistant": + case "toolResult": + return false; + } + return false; +} + +function isTurnStartEntry(entry: SessionEntry): boolean { + if (entry.type === "compaction") { + return false; + } + return sessionEntryToContextMessages(entry).some(isTurnStartMessage); +} + +/** + * Find valid cut points: indices of context-visible user-like or assistant messages. + * Never cut at tool results (they must follow their tool call). + * When we cut at an assistant message with tool calls, its tool results follow it + * and will be kept. + */ +function findValidCutPoints(entries: SessionEntry[], startIndex: number, endIndex: number): number[] { + const cutPoints: number[] = []; + for (let i = startIndex; i < endIndex; i++) { + const entry = entries[i]; + if (entry.type === "compaction") { + continue; + } + if (sessionEntryToContextMessages(entry).some(isCutPointMessage)) { + cutPoints.push(i); + } + } + return cutPoints; +} + +/** + * Find the context-visible user-role message that starts the turn containing the given entry index. + * Returns -1 if no turn start found before the index. + */ +export function findTurnStartIndex(entries: SessionEntry[], entryIndex: number, startIndex: number): number { + for (let i = entryIndex; i >= startIndex; i--) { + if (isTurnStartEntry(entries[i])) { + return i; + } + } + return -1; +} + +export interface CutPointResult { + /** Index of first entry to keep */ + firstKeptEntryIndex: number; + /** Index of user message that starts the turn being split, or -1 if not splitting */ + turnStartIndex: number; + /** Whether this cut splits a turn (cut point is not a user message) */ + isSplitTurn: boolean; +} + +/** + * Find the cut point in session entries that keeps approximately `keepRecentTokens`. + * + * Algorithm: Walk backwards from newest, accumulating estimated message sizes. + * Stop when we've accumulated >= keepRecentTokens. Cut at that point. + * + * Can cut at user OR assistant messages (never tool results). When cutting at an + * assistant message with tool calls, its tool results come after and will be kept. + * + * Returns CutPointResult with: + * - firstKeptEntryIndex: the entry index to start keeping from + * - turnStartIndex: if cutting mid-turn, the user message that started that turn + * - isSplitTurn: whether we're cutting in the middle of a turn + * + * Only considers entries between `startIndex` and `endIndex` (exclusive). + */ +export function findCutPoint( + entries: SessionEntry[], + startIndex: number, + endIndex: number, + keepRecentTokens: number, +): CutPointResult { + const cutPoints = findValidCutPoints(entries, startIndex, endIndex); + + if (cutPoints.length === 0) { + return { firstKeptEntryIndex: startIndex, turnStartIndex: -1, isSplitTurn: false }; + } + + // Walk backwards from newest, accumulating estimated message sizes + let accumulatedTokens = 0; + let cutIndex = cutPoints[0]; // Default: keep from first message (not header) + + for (let i = endIndex - 1; i >= startIndex; i--) { + const entry = entries[i]; + const messageTokens = sessionEntryToContextMessages(entry).reduce( + (sum, message) => sum + estimateTokens(message), + 0, + ); + if (messageTokens === 0) continue; + accumulatedTokens += messageTokens; + + // Check if we've exceeded the budget + if (accumulatedTokens >= keepRecentTokens) { + // Find the closest valid cut point at or after this entry + for (let c = 0; c < cutPoints.length; c++) { + if (cutPoints[c] >= i) { + cutIndex = cutPoints[c]; + break; + } + } + break; + } + } + + // Scan backwards from cutIndex to include adjacent metadata entries that do not affect context. + while (cutIndex > startIndex) { + const prevEntry = entries[cutIndex - 1]; + // Stop at compaction boundaries or context-visible entries. + if (prevEntry.type === "compaction" || sessionEntryToContextMessages(prevEntry).length > 0) { + break; + } + cutIndex--; + } + + // Determine if this is a split turn + const cutEntry = entries[cutIndex]; + const startsTurn = isTurnStartEntry(cutEntry); + const turnStartIndex = startsTurn ? -1 : findTurnStartIndex(entries, cutIndex, startIndex); + + return { + firstKeptEntryIndex: cutIndex, + turnStartIndex, + isSplitTurn: !startsTurn && turnStartIndex !== -1, + }; +} + +// ============================================================================ +// Summarization +// ============================================================================ + +const SUMMARIZATION_PROMPT = `The messages above are a conversation to summarize. Create a structured context checkpoint summary that another LLM will use to continue the work. + +Use this EXACT format: + +## Goal +[What is the user trying to accomplish? Can be multiple items if the session covers different tasks.] + +## Constraints & Preferences +- [Any constraints, preferences, or requirements mentioned by user] +- [Or "(none)" if none were mentioned] + +## Progress +### Done +- [x] [Completed tasks/changes] + +### In Progress +- [ ] [Current work] + +### Blocked +- [Issues preventing progress, if any] + +## Key Decisions +- **[Decision]**: [Brief rationale] + +## Next Steps +1. [Ordered list of what should happen next] + +## Critical Context +- [Any data, examples, or references needed to continue] +- [Or "(none)" if not applicable] + +Keep each section concise. Preserve exact file paths, function names, and error messages.`; + +const UPDATE_SUMMARIZATION_INSTRUCTIONS = `Update the existing structured summary with new information. RULES: +- PRESERVE all existing information from the previous summary +- ADD new progress, decisions, and context from the new messages +- UPDATE the Progress section: move items from "In Progress" to "Done" when completed +- UPDATE "Next Steps" based on what was accomplished +- PRESERVE exact file paths, function names, and error messages +- If something is no longer relevant, you may remove it + +Use this EXACT format: + +## Goal +[Preserve existing goals, add new ones if the task expanded] + +## Constraints & Preferences +- [Preserve existing, add new ones discovered] + +## Progress +### Done +- [x] [Include previously done items AND newly completed items] + +### In Progress +- [ ] [Current work - update based on progress] + +### Blocked +- [Current blockers - remove if resolved] + +## Key Decisions +- **[Decision]**: [Brief rationale] (preserve all previous, add new) + +## Next Steps +1. [Update based on current state] + +## Critical Context +- [Preserve important context, add new if needed] + +Keep each section concise. Preserve exact file paths, function names, and error messages.`; + +const UPDATE_SUMMARIZATION_PROMPT = `The messages above are NEW conversation messages to incorporate into the existing summary provided in tags. + +${UPDATE_SUMMARIZATION_INSTRUCTIONS}`; + +/** + * Returns an error message when a summarization response cannot safely be persisted. + * A length stop contains partial text and must not become a session checkpoint. + */ +export function getSummarizationFailure(response: AssistantMessage, label: string): string | undefined { + if (response.stopReason === "error") { + return `${label} failed: ${response.errorMessage || "Unknown error"}`; + } + if (response.stopReason === "length") { + return `${label} failed: generation hit the token cap and the summary is incomplete`; + } + return undefined; +} + +function createSummarizationOptions( + model: Model, + maxTokens: number, + apiKey: string | undefined, + headers: Record | undefined, + env: Record | undefined, + signal: AbortSignal | undefined, + thinkingLevel: ThinkingLevel | undefined, + sessionId: string | undefined, +): SimpleStreamOptions { + const options: SimpleStreamOptions = { maxTokens, signal, apiKey, headers, env, sessionId }; + if (model.reasoning && thinkingLevel && thinkingLevel !== "off") { + options.reasoning = thinkingLevel; + } + return options; +} + +/** + * Shared choke point for every compaction/branch-summary summarization call. Wraps the + * single LLM call in {@link retryAssistantCall} so transient stream drops (e.g. + * `terminated`, socket close) honor the configured retry policy instead of failing + * the whole compaction on the first attempt. Deterministic errors and aborts return + * immediately (see {@link retryAssistantCall}). + */ +export async function completeSummarization( + model: Model, + context: Context, + options: SimpleStreamOptions, + streamFn?: StreamFn, + retry?: RetryPolicy, + callbacks?: RetryCallbacks, +): Promise { + // Avoid cache writes for one-off summaries. Reuse caller-supplied routing when available; + // callers without a session ID, including branch summaries, receive a fresh routing ID. + const requestOptions: SimpleStreamOptions = { + ...options, + cacheRetention: "none", + sessionId: options.sessionId ?? uuidv7(), + }; + const produce = async (): Promise => + streamFn + ? (await streamFn(model, context, requestOptions)).result() + : completeSimple(model, context, requestOptions); + return retryAssistantCall(produce, retry, requestOptions.signal, callbacks); +} + +/** + * Generate a summary of the conversation using the LLM. + * If previousSummary is provided, uses the update prompt to merge. + */ +export async function generateSummary( + currentMessages: AgentMessage[], + model: Model, + reserveTokens: number, + apiKey: string | undefined, + headers?: Record, + signal?: AbortSignal, + customInstructions?: string, + previousSummary?: string, + thinkingLevel?: ThinkingLevel, + streamFn?: StreamFn, + env?: Record, + retry?: RetryPolicy, + callbacks?: RetryCallbacks, + sessionId?: string, +): Promise { + return ( + await generateSummaryWithUsage( + currentMessages, + model, + reserveTokens, + apiKey, + headers, + signal, + customInstructions, + previousSummary, + thinkingLevel, + streamFn, + env, + retry, + callbacks, + sessionId, + ) + ).text; +} + +/** Build the provider context for a standalone summary request. */ +function buildSummarizationContext(promptText: string): Context { + return { + systemPrompt: SUMMARIZATION_SYSTEM_PROMPT, + messages: [ + { + role: "user", + content: [{ type: "text", text: promptText }], + timestamp: Date.now(), + }, + ], + }; +} + +/** Generate or update a conversation summary and return its provider usage. */ +export async function generateSummaryWithUsage( + currentMessages: AgentMessage[], + model: Model, + reserveTokens: number, + apiKey: string | undefined, + headers?: Record, + signal?: AbortSignal, + customInstructions?: string, + previousSummary?: string, + thinkingLevel?: ThinkingLevel, + streamFn?: StreamFn, + env?: Record, + retry?: RetryPolicy, + callbacks?: RetryCallbacks, + sessionId?: string, +): Promise<{ text: string; usage: Usage }> { + const maxTokens = Math.min( + Math.floor(0.8 * reserveTokens), + model.maxTokens > 0 ? model.maxTokens : Number.POSITIVE_INFINITY, + ); + + // Use update prompt if we have a previous summary, otherwise initial prompt + let basePrompt = previousSummary ? UPDATE_SUMMARIZATION_PROMPT : SUMMARIZATION_PROMPT; + if (customInstructions) { + basePrompt = `${basePrompt}\n\nAdditional focus: ${customInstructions}`; + } + + // Serialize conversation to text so model doesn't try to continue it + // Convert to LLM messages first (handles custom types like bashExecution, custom, etc.) + const llmMessages = convertToLlm(currentMessages); + const conversationText = serializeConversation(llmMessages); + + // Build the prompt with conversation wrapped in tags + let promptText = `\n${conversationText}\n\n\n`; + if (previousSummary) { + promptText += `\n${previousSummary}\n\n\n`; + } + promptText += basePrompt; + + const completionOptions = createSummarizationOptions( + model, + maxTokens, + apiKey, + headers, + env, + signal, + thinkingLevel, + sessionId, + ); + + const response = await completeSummarization( + model, + buildSummarizationContext(promptText), + completionOptions, + streamFn, + retry, + callbacks, + ); + + const failure = getSummarizationFailure(response, "Summarization"); + if (failure) { + throw new Error(failure); + } + if (response.content.some((block) => block.type === "toolCall")) { + throw new Error("Summarization attempted to call a tool"); + } + + const textContent = contentText(response.content); + + return { text: textContent, usage: response.usage }; +} + +// ============================================================================ +// Compaction Preparation (for extensions) +// ============================================================================ + +export interface CompactionPreparation { + /** UUID of first entry to keep */ + firstKeptEntryId: string; + /** Messages that will be summarized and discarded */ + messagesToSummarize: AgentMessage[]; + /** Messages that will be turned into turn prefix summary (if splitting) */ + turnPrefixMessages: AgentMessage[]; + /** Whether this is a split turn (cut point in middle of turn) */ + isSplitTurn: boolean; + tokensBefore: number; + /** Summary from previous compaction, for iterative update */ + previousSummary?: string; + /** File operations extracted from messagesToSummarize */ + fileOps: FileOperations; + /** Compaction settions from settings.jsonl */ + settings: CompactionSettings; +} + +export function prepareCompaction( + pathEntries: SessionEntry[], + settings: CompactionSettings, +): CompactionPreparation | undefined { + if (pathEntries.length > 0 && pathEntries[pathEntries.length - 1].type === "compaction") { + return undefined; + } + + let prevCompactionIndex = -1; + for (let i = pathEntries.length - 1; i >= 0; i--) { + if (pathEntries[i].type === "compaction") { + prevCompactionIndex = i; + break; + } + } + + let previousSummary: string | undefined; + let boundaryStart = 0; + if (prevCompactionIndex >= 0) { + const prevCompaction = pathEntries[prevCompactionIndex] as CompactionEntry; + previousSummary = prevCompaction.summary; + const firstKeptEntryIndex = pathEntries.findIndex((entry) => entry.id === prevCompaction.firstKeptEntryId); + boundaryStart = firstKeptEntryIndex >= 0 ? firstKeptEntryIndex : prevCompactionIndex + 1; + } + const boundaryEnd = pathEntries.length; + + const tokensBefore = estimateContextTokens(buildSessionContext(pathEntries).messages).tokens; + + const cutPoint = findCutPoint(pathEntries, boundaryStart, boundaryEnd, settings.keepRecentTokens); + + // Get UUID of first kept entry + const firstKeptEntry = pathEntries[cutPoint.firstKeptEntryIndex]; + if (!firstKeptEntry?.id) { + return undefined; // Session needs migration + } + const firstKeptEntryId = firstKeptEntry.id; + + const historyEnd = cutPoint.isSplitTurn ? cutPoint.turnStartIndex : cutPoint.firstKeptEntryIndex; + + // Messages to summarize (will be discarded after summary) + const messagesToSummarize: AgentMessage[] = []; + for (let i = boundaryStart; i < historyEnd; i++) { + const msg = getMessageFromEntryForCompaction(pathEntries[i]); + if (msg) messagesToSummarize.push(msg); + } + + // Messages for turn prefix summary (if splitting a turn) + const turnPrefixMessages: AgentMessage[] = []; + if (cutPoint.isSplitTurn) { + for (let i = cutPoint.turnStartIndex; i < cutPoint.firstKeptEntryIndex; i++) { + const msg = getMessageFromEntryForCompaction(pathEntries[i]); + if (msg) turnPrefixMessages.push(msg); + } + } + + if (messagesToSummarize.length === 0 && turnPrefixMessages.length === 0) { + return undefined; + } + + // Extract file operations from messages and previous compaction + const fileOps = extractFileOperations(messagesToSummarize, pathEntries, prevCompactionIndex); + + // Also extract file ops from turn prefix if splitting + if (cutPoint.isSplitTurn) { + for (const msg of turnPrefixMessages) { + extractFileOpsFromMessage(msg, fileOps); + } + } + + return { + firstKeptEntryId, + messagesToSummarize, + turnPrefixMessages, + isSplitTurn: cutPoint.isSplitTurn, + tokensBefore, + previousSummary, + fileOps, + settings, + }; +} + +// ============================================================================ +// Main compaction function +// ============================================================================ + +const TURN_PREFIX_SUMMARIZATION_PROMPT = `This is the PREFIX of a turn that was too large to keep. The SUFFIX (recent work) is retained. + +Summarize the prefix to provide context for the retained suffix: + +## Original Request +[What did the user ask for in this turn?] + +## Early Progress +- [Key decisions and work done in the prefix] + +## Context for Suffix +- [Information needed to understand the retained recent work] + +Be concise. Focus on what's needed to understand the kept suffix.`; + +/** + * Generate summaries for compaction using prepared data. + * Returns CompactionResult - SessionManager adds uuid/parentUuid when saving. + * + * @param preparation - Pre-calculated preparation from prepareCompaction() + * @param customInstructions - Optional custom focus for the summary + * @param sessionId - Optional routing session ID forwarded without enabling prompt caching + */ +export async function compact( + preparation: CompactionPreparation, + model: Model, + apiKey: string | undefined, + headers?: Record, + customInstructions?: string, + signal?: AbortSignal, + thinkingLevel?: ThinkingLevel, + streamFn?: StreamFn, + env?: Record, + retry?: RetryPolicy, + callbacks?: RetryCallbacks, + sessionId?: string, +): Promise { + const { + firstKeptEntryId, + messagesToSummarize, + turnPrefixMessages, + isSplitTurn, + tokensBefore, + previousSummary, + fileOps, + settings, + } = preparation; + + // Generate summaries and merge into one + let summary: string; + let summaryUsage: Usage; + + if (isSplitTurn && turnPrefixMessages.length > 0) { + let historyText = "No prior history."; + let historyUsage: Usage | undefined; + if (messagesToSummarize.length > 0) { + const historyResult = await generateSummaryWithUsage( + messagesToSummarize, + model, + settings.reserveTokens, + apiKey, + headers, + signal, + customInstructions, + previousSummary, + thinkingLevel, + streamFn, + env, + retry, + callbacks, + sessionId, + ); + historyText = historyResult.text; + historyUsage = historyResult.usage; + } + const turnPrefixResult = await generateTurnPrefixSummary( + turnPrefixMessages, + model, + settings.reserveTokens, + apiKey, + headers, + env, + signal, + thinkingLevel, + streamFn, + retry, + callbacks, + sessionId, + ); + // Merge into single summary + summary = `${historyText}\n\n---\n\n**Turn Context (split turn):**\n\n${turnPrefixResult.text}`; + summaryUsage = historyUsage ? combineUsage(historyUsage, turnPrefixResult.usage) : turnPrefixResult.usage; + } else { + // Just generate history summary + const result = await generateSummaryWithUsage( + messagesToSummarize, + model, + settings.reserveTokens, + apiKey, + headers, + signal, + customInstructions, + previousSummary, + thinkingLevel, + streamFn, + env, + retry, + callbacks, + sessionId, + ); + summary = result.text; + summaryUsage = result.usage; + } + + // Compute file lists and append to summary + const { readFiles, modifiedFiles } = computeFileLists(fileOps); + summary += formatFileOperations(readFiles, modifiedFiles); + + if (!firstKeptEntryId) { + throw new Error("First kept entry has no UUID - session may need migration"); + } + + return { + summary, + firstKeptEntryId, + tokensBefore, + usage: summaryUsage, + details: { readFiles, modifiedFiles } as CompactionDetails, + }; +} + +/** + * Generate a summary for a turn prefix (when splitting a turn). + */ +async function generateTurnPrefixSummary( + messages: AgentMessage[], + model: Model, + reserveTokens: number, + apiKey: string | undefined, + headers?: Record, + env?: Record, + signal?: AbortSignal, + thinkingLevel?: ThinkingLevel, + streamFn?: StreamFn, + retry?: RetryPolicy, + callbacks?: RetryCallbacks, + sessionId?: string, +): Promise<{ text: string; usage: Usage }> { + const maxTokens = Math.min( + Math.floor(0.5 * reserveTokens), + model.maxTokens > 0 ? model.maxTokens : Number.POSITIVE_INFINITY, + ); // Smaller budget for turn prefix + const llmMessages = convertToLlm(messages); + const conversationText = serializeConversation(llmMessages); + const promptText = `\n${conversationText}\n\n\n${TURN_PREFIX_SUMMARIZATION_PROMPT}`; + + const response = await completeSummarization( + model, + buildSummarizationContext(promptText), + createSummarizationOptions(model, maxTokens, apiKey, headers, env, signal, thinkingLevel, sessionId), + streamFn, + retry, + callbacks, + ); + + const failure = getSummarizationFailure(response, "Turn prefix summarization"); + if (failure) { + throw new Error(failure); + } + if (response.content.some((block) => block.type === "toolCall")) { + throw new Error("Turn prefix summarization attempted to call a tool"); + } + + return { + text: contentText(response.content), + usage: response.usage, + }; +} diff --git a/packages/coding-agent/src/core/compaction/index.ts b/packages/coding-agent/src/core/compaction/index.ts new file mode 100644 index 0000000000000000000000000000000000000000..7fae5f2c3f95189a2e2cc199d72f43fd16a209b3 --- /dev/null +++ b/packages/coding-agent/src/core/compaction/index.ts @@ -0,0 +1,7 @@ +/** + * Compaction and summarization utilities. + */ + +export * from "./branch-summarization.ts"; +export * from "./compaction.ts"; +export * from "./utils.ts"; diff --git a/packages/coding-agent/src/core/compaction/utils.ts b/packages/coding-agent/src/core/compaction/utils.ts new file mode 100644 index 0000000000000000000000000000000000000000..35d1a29bb0a752cd02d89ae47adf12ac66339e81 --- /dev/null +++ b/packages/coding-agent/src/core/compaction/utils.ts @@ -0,0 +1,158 @@ +/** + * Shared utilities for compaction and branch summarization. + */ + +import type { AgentMessage } from "@earendil-works/pi-agent-core"; +import { contentText, type Message } from "@earendil-works/pi-ai"; + +// ============================================================================ +// File Operation Tracking +// ============================================================================ + +export interface FileOperations { + read: Set; + written: Set; + edited: Set; +} + +export function createFileOps(): FileOperations { + return { + read: new Set(), + written: new Set(), + edited: new Set(), + }; +} + +/** + * Extract file operations from tool calls in an assistant message. + */ +export function extractFileOpsFromMessage(message: AgentMessage, fileOps: FileOperations): void { + if (message.role !== "assistant") return; + if (!("content" in message) || !Array.isArray(message.content)) return; + + for (const block of message.content) { + if (typeof block !== "object" || block === null) continue; + if (!("type" in block) || block.type !== "toolCall") continue; + if (!("arguments" in block) || !("name" in block)) continue; + + const args = block.arguments as Record | undefined; + if (!args) continue; + + const path = typeof args.path === "string" ? args.path : undefined; + if (!path) continue; + + switch (block.name) { + case "read": + fileOps.read.add(path); + break; + case "write": + fileOps.written.add(path); + break; + case "edit": + fileOps.edited.add(path); + break; + } + } +} + +/** + * Compute final file lists from file operations. + * Returns readFiles (files only read, not modified) and modifiedFiles. + */ +export function computeFileLists(fileOps: FileOperations): { readFiles: string[]; modifiedFiles: string[] } { + const modified = new Set([...fileOps.edited, ...fileOps.written]); + const readOnly = [...fileOps.read].filter((f) => !modified.has(f)).sort(); + const modifiedFiles = [...modified].sort(); + return { readFiles: readOnly, modifiedFiles }; +} + +/** + * Format file operations as XML tags for summary. + */ +export function formatFileOperations(readFiles: string[], modifiedFiles: string[]): string { + const sections: string[] = []; + if (readFiles.length > 0) { + sections.push(`\n${readFiles.join("\n")}\n`); + } + if (modifiedFiles.length > 0) { + sections.push(`\n${modifiedFiles.join("\n")}\n`); + } + if (sections.length === 0) return ""; + return `\n\n${sections.join("\n\n")}`; +} + +// ============================================================================ +// Message Serialization +// ============================================================================ + +/** Maximum characters for a tool result in serialized summaries. */ +const TOOL_RESULT_MAX_CHARS = 2000; + +/** + * Truncate text to a maximum character length for summarization. + * Keeps the beginning and appends a truncation marker. + */ +function truncateForSummary(text: string, maxChars: number): string { + if (text.length <= maxChars) return text; + const truncatedChars = text.length - maxChars; + return `${text.slice(0, maxChars)}\n\n[... ${truncatedChars} more characters truncated]`; +} + +/** + * Serialize LLM messages to text for summarization. + * This prevents the model from treating it as a conversation to continue. + * Call convertToLlm() first to handle custom message types. + * + * Tool results are truncated to keep the summarization request within + * reasonable token budgets. Full content is not needed for summarization. + */ +export function serializeConversation(messages: Message[]): string { + const parts: string[] = []; + + for (const msg of messages) { + if (msg.role === "user") { + const content = contentText(msg.content, ""); + if (content) parts.push(`[User]: ${content}`); + } else if (msg.role === "assistant") { + const thinkingParts: string[] = []; + const toolCalls: string[] = []; + + for (const block of msg.content) { + if (block.type === "thinking") { + thinkingParts.push(block.thinking); + } else if (block.type === "toolCall") { + const args = block.arguments as Record; + const argsStr = Object.entries(args) + .map(([k, v]) => `${k}=${JSON.stringify(v)}`) + .join(", "); + toolCalls.push(`${block.name}(${argsStr})`); + } + } + + if (thinkingParts.length > 0) { + parts.push(`[Assistant thinking]: ${thinkingParts.join("\n")}`); + } + if (msg.content.some((block) => block.type === "text")) { + parts.push(`[Assistant]: ${contentText(msg.content)}`); + } + if (toolCalls.length > 0) { + parts.push(`[Assistant tool calls]: ${toolCalls.join("; ")}`); + } + } else if (msg.role === "toolResult") { + const content = contentText(msg.content, ""); + if (content) { + parts.push(`[Tool result]: ${truncateForSummary(content, TOOL_RESULT_MAX_CHARS)}`); + } + } + } + + return parts.join("\n\n"); +} + +// ============================================================================ +// Summarization System Prompt +// ============================================================================ + +export const SUMMARIZATION_SYSTEM_PROMPT = `You are a context summarization assistant. Your task is to read a conversation between a user and an AI assistant, then produce a structured summary following the exact format specified. + +Do NOT continue the conversation. Do NOT respond to any questions in the conversation. ONLY output the structured summary.`; diff --git a/packages/coding-agent/src/core/export-html/ansi-to-html.ts b/packages/coding-agent/src/core/export-html/ansi-to-html.ts new file mode 100644 index 0000000000000000000000000000000000000000..d675fd55b3d6db040ebfa9b4f124b24ab01f4c89 --- /dev/null +++ b/packages/coding-agent/src/core/export-html/ansi-to-html.ts @@ -0,0 +1,258 @@ +/** + * ANSI escape code to HTML converter. + * + * Converts terminal ANSI color/style codes to HTML with inline styles. + * Supports: + * - Standard foreground colors (30-37) and bright variants (90-97) + * - Standard background colors (40-47) and bright variants (100-107) + * - 256-color palette (38;5;N and 48;5;N) + * - RGB true color (38;2;R;G;B and 48;2;R;G;B) + * - Text styles: bold (1), dim (2), italic (3), underline (4) + * - Reset (0) + */ + +// Standard ANSI color palette (0-15) +const ANSI_COLORS = [ + "#000000", // 0: black + "#800000", // 1: red + "#008000", // 2: green + "#808000", // 3: yellow + "#000080", // 4: blue + "#800080", // 5: magenta + "#008080", // 6: cyan + "#c0c0c0", // 7: white + "#808080", // 8: bright black + "#ff0000", // 9: bright red + "#00ff00", // 10: bright green + "#ffff00", // 11: bright yellow + "#0000ff", // 12: bright blue + "#ff00ff", // 13: bright magenta + "#00ffff", // 14: bright cyan + "#ffffff", // 15: bright white +]; + +/** + * Convert 256-color index to hex. + */ +function color256ToHex(index: number): string { + // Standard colors (0-15) + if (index < 16) { + return ANSI_COLORS[index]; + } + + // Color cube (16-231): 6x6x6 = 216 colors + if (index < 232) { + const cubeIndex = index - 16; + const r = Math.floor(cubeIndex / 36); + const g = Math.floor((cubeIndex % 36) / 6); + const b = cubeIndex % 6; + const toComponent = (n: number) => (n === 0 ? 0 : 55 + n * 40); + const toHex = (n: number) => toComponent(n).toString(16).padStart(2, "0"); + return `#${toHex(r)}${toHex(g)}${toHex(b)}`; + } + + // Grayscale (232-255): 24 shades + const gray = 8 + (index - 232) * 10; + const grayHex = gray.toString(16).padStart(2, "0"); + return `#${grayHex}${grayHex}${grayHex}`; +} + +/** + * Escape HTML special characters. + */ +function escapeHtml(text: string): string { + return text + .replace(/&/g, "&") + .replace(//g, ">") + .replace(/"/g, """) + .replace(/'/g, "'"); +} + +interface TextStyle { + fg: string | null; + bg: string | null; + bold: boolean; + dim: boolean; + italic: boolean; + underline: boolean; +} + +function createEmptyStyle(): TextStyle { + return { + fg: null, + bg: null, + bold: false, + dim: false, + italic: false, + underline: false, + }; +} + +function styleToInlineCSS(style: TextStyle): string { + const parts: string[] = []; + if (style.fg) parts.push(`color:${style.fg}`); + if (style.bg) parts.push(`background-color:${style.bg}`); + if (style.bold) parts.push("font-weight:bold"); + if (style.dim) parts.push("opacity:0.6"); + if (style.italic) parts.push("font-style:italic"); + if (style.underline) parts.push("text-decoration:underline"); + return parts.join(";"); +} + +function hasStyle(style: TextStyle): boolean { + return style.fg !== null || style.bg !== null || style.bold || style.dim || style.italic || style.underline; +} + +/** + * Parse ANSI SGR (Select Graphic Rendition) codes and update style. + */ +function applySgrCode(params: number[], style: TextStyle): void { + let i = 0; + while (i < params.length) { + const code = params[i]; + + if (code === 0) { + // Reset all + style.fg = null; + style.bg = null; + style.bold = false; + style.dim = false; + style.italic = false; + style.underline = false; + } else if (code === 1) { + style.bold = true; + } else if (code === 2) { + style.dim = true; + } else if (code === 3) { + style.italic = true; + } else if (code === 4) { + style.underline = true; + } else if (code === 22) { + // Reset bold/dim + style.bold = false; + style.dim = false; + } else if (code === 23) { + style.italic = false; + } else if (code === 24) { + style.underline = false; + } else if (code >= 30 && code <= 37) { + // Standard foreground colors + style.fg = ANSI_COLORS[code - 30]; + } else if (code === 38) { + // Extended foreground color + if (params[i + 1] === 5 && params.length > i + 2) { + // 256-color: 38;5;N + style.fg = color256ToHex(params[i + 2]); + i += 2; + } else if (params[i + 1] === 2 && params.length > i + 4) { + // RGB: 38;2;R;G;B + const r = params[i + 2]; + const g = params[i + 3]; + const b = params[i + 4]; + style.fg = `rgb(${r},${g},${b})`; + i += 4; + } + } else if (code === 39) { + // Default foreground + style.fg = null; + } else if (code >= 40 && code <= 47) { + // Standard background colors + style.bg = ANSI_COLORS[code - 40]; + } else if (code === 48) { + // Extended background color + if (params[i + 1] === 5 && params.length > i + 2) { + // 256-color: 48;5;N + style.bg = color256ToHex(params[i + 2]); + i += 2; + } else if (params[i + 1] === 2 && params.length > i + 4) { + // RGB: 48;2;R;G;B + const r = params[i + 2]; + const g = params[i + 3]; + const b = params[i + 4]; + style.bg = `rgb(${r},${g},${b})`; + i += 4; + } + } else if (code === 49) { + // Default background + style.bg = null; + } else if (code >= 90 && code <= 97) { + // Bright foreground colors + style.fg = ANSI_COLORS[code - 90 + 8]; + } else if (code >= 100 && code <= 107) { + // Bright background colors + style.bg = ANSI_COLORS[code - 100 + 8]; + } + // Ignore unrecognized codes + + i++; + } +} + +// Match ANSI escape sequences: ESC[ followed by params and ending with 'm' +const ANSI_REGEX = /\x1b\[([\d;]*)m/g; + +/** + * Convert ANSI-escaped text to HTML with inline styles. + */ +export function ansiToHtml(text: string): string { + const style = createEmptyStyle(); + let result = ""; + let lastIndex = 0; + let inSpan = false; + + // Reset regex state + ANSI_REGEX.lastIndex = 0; + + let match = ANSI_REGEX.exec(text); + while (match !== null) { + // Add text before this escape sequence + const beforeText = text.slice(lastIndex, match.index); + if (beforeText) { + result += escapeHtml(beforeText); + } + + // Parse SGR parameters + const paramStr = match[1]; + const params = paramStr ? paramStr.split(";").map((p) => parseInt(p, 10) || 0) : [0]; + + // Close existing span if we have one + if (inSpan) { + result += ""; + inSpan = false; + } + + // Apply the codes + applySgrCode(params, style); + + // Open new span if we have any styling + if (hasStyle(style)) { + result += ``; + inSpan = true; + } + + lastIndex = match.index + match[0].length; + match = ANSI_REGEX.exec(text); + } + + // Add remaining text + const remainingText = text.slice(lastIndex); + if (remainingText) { + result += escapeHtml(remainingText); + } + + // Close any open span + if (inSpan) { + result += ""; + } + + return result; +} + +/** + * Convert array of ANSI-escaped lines to HTML. + * Each line is wrapped in a div element. + */ +export function ansiLinesToHtml(lines: string[]): string { + return lines.map((line) => `
${ansiToHtml(line) || " "}
`).join(""); +} diff --git a/packages/coding-agent/src/core/export-html/index.ts b/packages/coding-agent/src/core/export-html/index.ts new file mode 100644 index 0000000000000000000000000000000000000000..3762c2640fad839f683fcfba0de10262eaa98509 --- /dev/null +++ b/packages/coding-agent/src/core/export-html/index.ts @@ -0,0 +1,316 @@ +import type { AgentState } from "@earendil-works/pi-agent-core"; +import { existsSync, readFileSync, writeFileSync } from "fs"; +import { basename, join } from "path"; +import { APP_NAME, getExportTemplateDir } from "../../config.ts"; +import { getResolvedThemeColors, getThemeExportColors } from "../../modes/interactive/theme/theme.ts"; +import { normalizePath, resolvePath } from "../../utils/paths.ts"; +import type { ToolDefinition } from "../extensions/types.ts"; +import type { SessionEntry } from "../session-manager.ts"; +import { SessionManager } from "../session-manager.ts"; + +/** + * Interface for rendering custom tools to HTML. + * Used by agent-session to pre-render extension tool output. + */ +export interface ToolHtmlRenderer { + /** Render a tool call to HTML. Returns undefined if tool has no custom renderer. */ + renderCall(toolCallId: string, toolName: string, args: unknown): string | undefined; + /** Render a tool result to HTML. Returns collapsed/expanded or undefined if tool has no custom renderer. */ + renderResult( + toolCallId: string, + toolName: string, + result: Array<{ type: string; text?: string; data?: string; mimeType?: string }>, + details: unknown, + isError: boolean, + ): { collapsed?: string; expanded?: string } | undefined; +} + +/** Pre-rendered HTML for a custom tool call and result */ +interface RenderedToolHtml { + callHtml?: string; + resultHtmlCollapsed?: string; + resultHtmlExpanded?: string; +} + +export interface ExportOptions { + outputPath?: string; + themeName?: string; + /** Optional tool renderer for custom tools */ + toolRenderer?: ToolHtmlRenderer; +} + +/** Parse a color string to RGB values. Supports hex (#RRGGBB) and rgb(r,g,b) formats. */ +function parseColor(color: string): { r: number; g: number; b: number } | undefined { + const hexMatch = color.match(/^#([0-9a-fA-F]{2})([0-9a-fA-F]{2})([0-9a-fA-F]{2})$/); + if (hexMatch) { + return { + r: Number.parseInt(hexMatch[1], 16), + g: Number.parseInt(hexMatch[2], 16), + b: Number.parseInt(hexMatch[3], 16), + }; + } + const rgbMatch = color.match(/^rgb\s*\(\s*(\d+)\s*,\s*(\d+)\s*,\s*(\d+)\s*\)$/); + if (rgbMatch) { + return { + r: Number.parseInt(rgbMatch[1], 10), + g: Number.parseInt(rgbMatch[2], 10), + b: Number.parseInt(rgbMatch[3], 10), + }; + } + return undefined; +} + +/** Calculate relative luminance of a color (0-1, higher = lighter). */ +function getLuminance(r: number, g: number, b: number): number { + const toLinear = (c: number) => { + const s = c / 255; + return s <= 0.03928 ? s / 12.92 : ((s + 0.055) / 1.055) ** 2.4; + }; + return 0.2126 * toLinear(r) + 0.7152 * toLinear(g) + 0.0722 * toLinear(b); +} + +/** Adjust color brightness. Factor > 1 lightens, < 1 darkens. */ +function adjustBrightness(color: string, factor: number): string { + const parsed = parseColor(color); + if (!parsed) return color; + const adjust = (c: number) => Math.min(255, Math.max(0, Math.round(c * factor))); + return `rgb(${adjust(parsed.r)}, ${adjust(parsed.g)}, ${adjust(parsed.b)})`; +} + +/** Derive export background colors from a base color (e.g., userMessageBg). */ +function deriveExportColors(baseColor: string): { pageBg: string; cardBg: string; infoBg: string } { + const parsed = parseColor(baseColor); + if (!parsed) { + return { + pageBg: "rgb(24, 24, 30)", + cardBg: "rgb(30, 30, 36)", + infoBg: "rgb(60, 55, 40)", + }; + } + + const luminance = getLuminance(parsed.r, parsed.g, parsed.b); + const isLight = luminance > 0.5; + + if (isLight) { + return { + pageBg: adjustBrightness(baseColor, 0.96), + cardBg: baseColor, + infoBg: `rgb(${Math.min(255, parsed.r + 10)}, ${Math.min(255, parsed.g + 5)}, ${Math.max(0, parsed.b - 20)})`, + }; + } + return { + pageBg: adjustBrightness(baseColor, 0.7), + cardBg: adjustBrightness(baseColor, 0.85), + infoBg: `rgb(${Math.min(255, parsed.r + 20)}, ${Math.min(255, parsed.g + 15)}, ${parsed.b})`, + }; +} + +/** + * Generate CSS custom property declarations from theme colors. + */ +function generateThemeVars(themeName?: string): string { + const colors = getResolvedThemeColors(themeName); + const lines: string[] = []; + for (const [key, value] of Object.entries(colors)) { + lines.push(`--${key}: ${value};`); + } + + // Use explicit theme export colors if available, otherwise derive from userMessageBg + const themeExport = getThemeExportColors(themeName); + const userMessageBg = colors.userMessageBg || "#343541"; + const derivedColors = deriveExportColors(userMessageBg); + + lines.push(`--exportPageBg: ${themeExport.pageBg ?? derivedColors.pageBg};`); + lines.push(`--exportCardBg: ${themeExport.cardBg ?? derivedColors.cardBg};`); + lines.push(`--exportInfoBg: ${themeExport.infoBg ?? derivedColors.infoBg};`); + + return lines.join("\n "); +} + +interface SessionData { + header: ReturnType; + entries: ReturnType; + leafId: string | null; + systemPrompt?: string; + tools?: Array>; + /** Pre-rendered HTML for custom tool calls/results, keyed by tool call ID */ + renderedTools?: Record; +} + +/** + * Core HTML generation logic shared by both export functions. + */ +function generateHtml(sessionData: SessionData, themeName?: string): string { + const templateDir = getExportTemplateDir(); + const template = readFileSync(join(templateDir, "template.html"), "utf-8"); + const templateCss = readFileSync(join(templateDir, "template.css"), "utf-8"); + const templateJs = readFileSync(join(templateDir, "template.js"), "utf-8"); + const markedJs = readFileSync(join(templateDir, "vendor", "marked.min.js"), "utf-8"); + const hljsJs = readFileSync(join(templateDir, "vendor", "highlight.min.js"), "utf-8"); + + const themeVars = generateThemeVars(themeName); + const colors = getResolvedThemeColors(themeName); + const themeExport = getThemeExportColors(themeName); + const derivedExportColors = deriveExportColors(colors.userMessageBg || "#343541"); + const bodyBg = themeExport.pageBg ?? derivedExportColors.pageBg; + const containerBg = themeExport.cardBg ?? derivedExportColors.cardBg; + const infoBg = themeExport.infoBg ?? derivedExportColors.infoBg; + + // Base64 encode session data to avoid escaping issues + const sessionDataBase64 = Buffer.from(JSON.stringify(sessionData)).toString("base64"); + + // Build the CSS with theme variables injected + const css = templateCss + .replace("{{THEME_VARS}}", themeVars) + .replace("{{BODY_BG}}", bodyBg) + .replace("{{CONTAINER_BG}}", containerBg) + .replace("{{INFO_BG}}", infoBg); + + return template + .replace("{{CSS}}", css) + .replace("{{JS}}", templateJs) + .replace("{{SESSION_DATA}}", sessionDataBase64) + .replace("{{MARKED_JS}}", markedJs) + .replace("{{HIGHLIGHT_JS}}", hljsJs); +} + +/** Tools rendered directly by the HTML template (not pre-rendered via TUI→ANSI→HTML pipeline) */ +const TEMPLATE_RENDERED_TOOLS = new Set(["bash", "read", "write", "edit", "ls"]); + +/** + * Pre-render custom tools to HTML using their TUI renderers. + */ +function preRenderCustomTools( + entries: SessionEntry[], + toolRenderer: ToolHtmlRenderer, +): Record { + const renderedTools: Record = {}; + + for (const entry of entries) { + if (entry.type !== "message") continue; + const msg = entry.message; + + // Find tool calls in assistant messages + if (msg.role === "assistant" && Array.isArray(msg.content)) { + for (const block of msg.content) { + if (block.type === "toolCall" && !TEMPLATE_RENDERED_TOOLS.has(block.name)) { + const callHtml = toolRenderer.renderCall(block.id, block.name, block.arguments); + if (callHtml) { + renderedTools[block.id] = { callHtml }; + } + } + } + } + + // Find tool results + if (msg.role === "toolResult" && msg.toolCallId) { + const toolName = msg.toolName || ""; + // Only render if we have a pre-rendered call OR it's not template-rendered + const existing = renderedTools[msg.toolCallId]; + if (existing || !TEMPLATE_RENDERED_TOOLS.has(toolName)) { + const rendered = toolRenderer.renderResult( + msg.toolCallId, + toolName, + msg.content, + msg.details, + msg.isError || false, + ); + if (rendered) { + renderedTools[msg.toolCallId] = { + ...existing, + resultHtmlCollapsed: rendered.collapsed, + resultHtmlExpanded: rendered.expanded, + }; + } + } + } + } + + return renderedTools; +} + +/** + * Export session to HTML using SessionManager and AgentState. + * Used by TUI's /export command. + */ +export async function exportSessionToHtml( + sm: SessionManager, + state?: AgentState, + options?: ExportOptions | string, +): Promise { + const opts: ExportOptions = typeof options === "string" ? { outputPath: options } : options || {}; + + const sessionFile = sm.getSessionFile(); + if (!sessionFile) { + throw new Error("Cannot export in-memory session to HTML"); + } + if (!existsSync(sessionFile)) { + throw new Error("Nothing to export yet - start a conversation first"); + } + + const entries = sm.getEntries(); + + // Pre-render custom tools if a tool renderer is provided + let renderedTools: Record | undefined; + if (opts.toolRenderer) { + renderedTools = preRenderCustomTools(entries, opts.toolRenderer); + // Only include if we actually rendered something + if (Object.keys(renderedTools).length === 0) { + renderedTools = undefined; + } + } + + const sessionData: SessionData = { + header: sm.getHeader(), + entries, + leafId: sm.getLeafId(), + systemPrompt: state?.systemPrompt, + tools: state?.tools?.map((t) => ({ name: t.name, description: t.description, parameters: t.parameters })), + renderedTools, + }; + + const html = generateHtml(sessionData, opts.themeName); + + let outputPath = opts.outputPath ? normalizePath(opts.outputPath) : undefined; + if (!outputPath) { + const sessionBasename = basename(sessionFile, ".jsonl"); + outputPath = `${APP_NAME}-session-${sessionBasename}.html`; + } + + writeFileSync(outputPath, html, "utf8"); + return outputPath; +} + +/** + * Export session file to HTML (standalone, without AgentState). + * Used by CLI for exporting arbitrary session files. + */ +export async function exportFromFile(inputPath: string, options?: ExportOptions | string): Promise { + const opts: ExportOptions = typeof options === "string" ? { outputPath: options } : options || {}; + const resolvedInputPath = resolvePath(inputPath); + + if (!existsSync(resolvedInputPath)) { + throw new Error(`File not found: ${resolvedInputPath}`); + } + + const sm = SessionManager.open(resolvedInputPath); + + const sessionData: SessionData = { + header: sm.getHeader(), + entries: sm.getEntries(), + leafId: sm.getLeafId(), + systemPrompt: undefined, + tools: undefined, + }; + + const html = generateHtml(sessionData, opts.themeName); + + let outputPath = opts.outputPath ? normalizePath(opts.outputPath) : undefined; + if (!outputPath) { + const inputBasename = basename(resolvedInputPath, ".jsonl"); + outputPath = `${APP_NAME}-session-${inputBasename}.html`; + } + + writeFileSync(outputPath, html, "utf8"); + return outputPath; +} diff --git a/packages/coding-agent/src/core/export-html/template.css b/packages/coding-agent/src/core/export-html/template.css new file mode 100644 index 0000000000000000000000000000000000000000..c807161ee832d8b593f19bc7b37e92c3383206c4 --- /dev/null +++ b/packages/coding-agent/src/core/export-html/template.css @@ -0,0 +1,1066 @@ + :root { + {{THEME_VARS}} + --body-bg: {{BODY_BG}}; + --container-bg: {{CONTAINER_BG}}; + --info-bg: {{INFO_BG}}; + } + + * { margin: 0; padding: 0; box-sizing: border-box; } + + :root { + --line-height: 18px; /* 12px font * 1.5 */ + --sidebar-width: 400px; + --sidebar-min-width: 240px; + --sidebar-max-width: 840px; + --sidebar-resizer-width: 6px; + } + + body { + font-family: ui-monospace, 'Cascadia Code', 'Source Code Pro', Menlo, Consolas, 'DejaVu Sans Mono', monospace; + font-size: 12px; + line-height: var(--line-height); + color: var(--text); + background: var(--body-bg); + } + + body.sidebar-resizing { + cursor: col-resize; + user-select: none; + } + + #app { + display: flex; + min-height: 100vh; + } + + /* Sidebar */ + #sidebar { + width: var(--sidebar-width); + min-width: var(--sidebar-width); + max-width: var(--sidebar-width); + background: var(--container-bg); + flex-shrink: 0; + display: flex; + flex-direction: column; + position: sticky; + top: 0; + height: 100vh; + border-right: 1px solid var(--dim); + } + + #sidebar-resizer { + width: var(--sidebar-resizer-width); + flex-shrink: 0; + position: sticky; + top: 0; + height: 100vh; + cursor: col-resize; + touch-action: none; + background: transparent; + border-right: 1px solid transparent; + } + + #sidebar-resizer:hover, + body.sidebar-resizing #sidebar-resizer { + background: var(--selectedBg); + border-right-color: var(--dim); + } + + .sidebar-header { + padding: 8px 12px; + flex-shrink: 0; + } + + .sidebar-controls { + padding: 8px 8px 4px 8px; + } + + .sidebar-search { + width: 100%; + box-sizing: border-box; + padding: 4px 8px; + font-size: 11px; + font-family: inherit; + background: var(--body-bg); + color: var(--text); + border: 1px solid var(--dim); + border-radius: 3px; + } + + .sidebar-filters { + display: flex; + padding: 4px 8px 8px 8px; + gap: 4px; + align-items: center; + flex-wrap: wrap; + } + + .sidebar-search:focus { + outline: none; + border-color: var(--accent); + } + + .sidebar-search::placeholder { + color: var(--muted); + } + + .filter-btn { + padding: 3px 8px; + font-size: 10px; + font-family: inherit; + background: transparent; + color: var(--muted); + border: 1px solid var(--dim); + border-radius: 3px; + cursor: pointer; + } + + .filter-btn:hover { + color: var(--text); + border-color: var(--text); + } + + .filter-btn.active { + background: var(--accent); + color: var(--body-bg); + border-color: var(--accent); + } + + .sidebar-close { + display: none; + padding: 3px 8px; + font-size: 12px; + font-family: inherit; + background: transparent; + color: var(--muted); + border: 1px solid var(--dim); + border-radius: 3px; + cursor: pointer; + margin-left: auto; + } + + .sidebar-close:hover { + color: var(--text); + border-color: var(--text); + } + + .tree-container { + flex: 1; + overflow: auto; + padding: 4px 0; + } + + .tree-node { + padding: 0 8px; + cursor: pointer; + display: flex; + align-items: baseline; + font-size: 11px; + line-height: 13px; + white-space: nowrap; + } + + .tree-node:hover { + background: var(--selectedBg); + } + + .tree-node.active { + background: var(--selectedBg); + } + + .tree-node.active .tree-content { + font-weight: bold; + } + + .tree-node.in-path { + background: color-mix(in srgb, var(--accent) 10%, transparent); + } + + .tree-node:not(.in-path) { + opacity: 0.5; + } + + .tree-node:not(.in-path):hover { + opacity: 1; + } + + .tree-prefix { + color: var(--muted); + flex-shrink: 0; + font-family: monospace; + white-space: pre; + } + + .tree-marker { + color: var(--accent); + flex-shrink: 0; + } + + .tree-content { + color: var(--text); + } + + .tree-role-user { + color: var(--accent); + } + + .tree-role-skill { + color: var(--customMessageLabel); + } + + .tree-role-assistant { + color: var(--success); + } + + .tree-role-tool { + color: var(--muted); + } + + .tree-muted { + color: var(--muted); + } + + .tree-error { + color: var(--error); + } + + .tree-compaction { + color: var(--borderAccent); + } + + .tree-branch-summary { + color: var(--warning); + } + + .tree-custom-message { + color: var(--customMessageLabel); + } + + .tree-status { + padding: 4px 12px; + font-size: 10px; + color: var(--muted); + flex-shrink: 0; + } + + /* Main content */ + #content { + flex: 1; + min-width: 0; + overflow-y: auto; + padding: var(--line-height) calc(var(--line-height) * 2); + display: flex; + flex-direction: column; + align-items: center; + } + + #content > * { + width: 100%; + max-width: 800px; + } + + /* Help bar */ + .help-bar { + font-size: 11px; + color: var(--warning); + margin-bottom: var(--line-height); + display: flex; + align-items: center; + justify-content: space-between; + flex-wrap: wrap; + gap: 12px; + } + + .help-hint { + flex: 1 1 240px; + } + + .help-actions { + display: flex; + align-items: center; + flex-wrap: wrap; + gap: 8px; + } + + .header-toggle-btn, + .download-json-btn { + font-size: 10px; + padding: 2px 8px; + background: var(--container-bg); + border: 1px solid var(--border); + border-radius: 3px; + color: var(--text); + cursor: pointer; + font-family: inherit; + } + + .header-toggle-btn:hover, + .download-json-btn:hover { + background: var(--hover); + border-color: var(--borderAccent); + } + + /* Header */ + .header { + background: var(--container-bg); + border-radius: 4px; + padding: var(--line-height); + margin-bottom: var(--line-height); + } + + .header h1 { + font-size: 12px; + font-weight: bold; + color: var(--borderAccent); + margin-bottom: var(--line-height); + } + + .header-info { + display: flex; + flex-direction: column; + gap: 0; + font-size: 11px; + } + + .info-item { + color: var(--dim); + display: flex; + align-items: baseline; + } + + .info-label { + font-weight: 600; + margin-right: 8px; + min-width: 100px; + } + + .info-value { + color: var(--text); + flex: 1; + } + + /* Messages */ + #messages { + display: flex; + flex-direction: column; + gap: var(--line-height); + } + + .message-timestamp { + font-size: 10px; + color: var(--dim); + opacity: 0.8; + } + + .user-message { + background: var(--userMessageBg); + color: var(--userMessageText); + padding: var(--line-height); + border-radius: 4px; + position: relative; + } + + .assistant-message { + padding: 0; + position: relative; + } + + /* Copy link button - appears on hover */ + .copy-link-btn { + position: absolute; + top: 8px; + right: 8px; + width: 28px; + height: 28px; + padding: 6px; + background: var(--container-bg); + border: 1px solid var(--dim); + border-radius: 4px; + color: var(--muted); + cursor: pointer; + opacity: 0; + transition: opacity 0.15s, background 0.15s, color 0.15s; + display: flex; + align-items: center; + justify-content: center; + z-index: 10; + } + + .user-message:hover .copy-link-btn, + .assistant-message:hover .copy-link-btn, + .skill-user-entry:hover .copy-link-btn { + opacity: 1; + } + + .copy-link-btn:hover { + background: var(--accent); + color: var(--body-bg); + border-color: var(--accent); + } + + .copy-link-btn.copied { + background: var(--success, #22c55e); + color: white; + border-color: var(--success, #22c55e); + } + + /* Highlight effect for deep-linked messages */ + .user-message.highlight, + .assistant-message.highlight { + animation: highlight-pulse 2s ease-out; + } + + @keyframes highlight-pulse { + 0% { + box-shadow: 0 0 0 3px var(--accent); + } + 100% { + box-shadow: 0 0 0 0 transparent; + } + } + + .assistant-message > .message-timestamp { + padding-left: var(--line-height); + } + + .assistant-text { + padding: var(--line-height); + padding-bottom: 0; + } + + .message-timestamp + .assistant-text, + .message-timestamp + .thinking-block { + padding-top: 0; + } + + .thinking-block + .assistant-text { + padding-top: 0; + } + + .thinking-text { + padding: var(--line-height); + color: var(--thinkingText); + font-style: italic; + white-space: pre-wrap; + } + + .message-timestamp + .thinking-block .thinking-text, + .message-timestamp + .thinking-block .thinking-collapsed { + padding-top: 0; + } + + .thinking-collapsed { + display: none; + padding: var(--line-height); + color: var(--thinkingText); + font-style: italic; + } + + /* Tool execution */ + .tool-execution { + padding: var(--line-height); + border-radius: 4px; + } + + .tool-execution + .tool-execution { + margin-top: var(--line-height); + } + + .assistant-text + .tool-execution { + margin-top: var(--line-height); + } + + .tool-execution.pending { background: var(--toolPendingBg); } + .tool-execution.success { background: var(--toolSuccessBg); } + .tool-execution.error { background: var(--toolErrorBg); } + + .tool-header, .tool-name { + font-weight: bold; + } + + .tool-path { + color: var(--accent); + word-break: break-all; + } + + .line-numbers { + color: var(--warning); + } + + .line-count { + color: var(--dim); + } + + .tool-command { + font-weight: bold; + white-space: pre-wrap; + word-wrap: break-word; + overflow-wrap: break-word; + word-break: break-word; + } + + .tool-output { + margin-top: var(--line-height); + color: var(--toolOutput); + word-wrap: break-word; + overflow-wrap: break-word; + word-break: break-word; + font-family: inherit; + overflow-x: auto; + } + + .tool-output > div, + .output-preview > div, + .output-full > div { + margin: 0; + padding: 0; + line-height: var(--line-height); + } + + .tool-output > div:not(.output-preview):not(.output-full), + .output-preview > div:not(.expand-hint), + .output-full > div:not(.expand-hint) { + white-space: pre-wrap; + } + + .tool-output pre { + margin: 0; + padding: 0; + font-family: inherit; + color: inherit; + white-space: pre-wrap; + word-wrap: break-word; + overflow-wrap: break-word; + } + + .tool-output code { + padding: 0; + background: none; + color: var(--text); + } + + .tool-output.expandable { + cursor: pointer; + } + + .tool-output.expandable:hover { + opacity: 0.9; + } + + .tool-output.expandable .output-full { + display: none; + } + + .tool-output.expandable.expanded .output-preview { + display: none; + } + + .tool-output.expandable.expanded .output-full { + display: block; + } + + .ansi-line { + white-space: pre; + } + + .tool-images { + } + + .tool-image { + max-width: 100%; + max-height: 500px; + border-radius: 4px; + margin: var(--line-height) 0; + } + + .expand-hint { + color: var(--toolOutput); + } + + /* Diff */ + .tool-diff { + font-size: 11px; + overflow-x: auto; + white-space: pre; + } + + .diff-added { color: var(--toolDiffAdded); } + .diff-removed { color: var(--toolDiffRemoved); } + .diff-context { color: var(--toolDiffContext); } + + /* Model change */ + .model-change { + padding: 0 var(--line-height); + color: var(--dim); + font-size: 11px; + } + + .model-name { + color: var(--borderAccent); + font-weight: bold; + } + + /* Compaction / Branch Summary - matches customMessage colors from TUI */ + .compaction { + background: var(--customMessageBg); + border-radius: 4px; + padding: var(--line-height); + cursor: pointer; + } + + .compaction-label { + color: var(--customMessageLabel); + font-weight: bold; + } + + .compaction-collapsed { + color: var(--customMessageText); + } + + .compaction-content { + display: none; + color: var(--customMessageText); + white-space: pre-wrap; + margin-top: var(--line-height); + } + + .compaction.expanded .compaction-collapsed { + display: none; + } + + .compaction.expanded .compaction-content { + display: block; + } + + /* System prompt */ + .system-prompt { + background: var(--customMessageBg); + padding: var(--line-height); + border-radius: 4px; + margin-bottom: var(--line-height); + } + + .system-prompt.expandable { + cursor: pointer; + } + + .system-prompt-header { + font-weight: bold; + color: var(--customMessageLabel); + } + + .system-prompt-preview { + color: var(--customMessageText); + white-space: pre-wrap; + word-wrap: break-word; + font-size: 11px; + margin-top: var(--line-height); + } + + .system-prompt-expand-hint { + color: var(--muted); + font-style: italic; + margin-top: 4px; + } + + .system-prompt-full { + display: none; + color: var(--customMessageText); + white-space: pre-wrap; + word-wrap: break-word; + font-size: 11px; + margin-top: var(--line-height); + } + + .system-prompt.expanded .system-prompt-preview, + .system-prompt.expanded .system-prompt-expand-hint { + display: none; + } + + .system-prompt.expanded .system-prompt-full { + display: block; + } + + .system-prompt.provider-prompt { + border-left: 3px solid var(--warning); + } + + .system-prompt-note { + font-size: 10px; + font-style: italic; + color: var(--muted); + margin-top: 4px; + } + + /* Tools list */ + .tools-list { + background: var(--customMessageBg); + padding: var(--line-height); + border-radius: 4px; + margin-bottom: var(--line-height); + } + + .tools-header { + font-weight: bold; + color: var(--customMessageLabel); + margin-bottom: var(--line-height); + } + + .tool-item { + font-size: 11px; + } + + .tool-item-name { + font-weight: bold; + color: var(--text); + } + + .tool-item-desc { + color: var(--dim); + } + + .tool-params-hint { + color: var(--muted); + font-style: italic; + } + + .tool-item:has(.tool-params-hint) { + cursor: pointer; + } + + .tool-params-hint::after { + content: '[click to show parameters]'; + } + + .tool-item.params-expanded .tool-params-hint::after { + content: '[hide parameters]'; + } + + .tool-params-content { + display: none; + margin-top: 4px; + margin-left: 12px; + padding-left: 8px; + border-left: 1px solid var(--dim); + } + + .tool-item.params-expanded .tool-params-content { + display: block; + } + + .tool-param { + margin-bottom: 4px; + font-size: 11px; + } + + .tool-param-name { + font-weight: bold; + color: var(--text); + } + + .tool-param-type { + color: var(--dim); + font-style: italic; + } + + .tool-param-required { + color: var(--warning, #e8a838); + font-size: 10px; + } + + .tool-param-optional { + color: var(--dim); + font-size: 10px; + } + + .tool-param-desc { + color: var(--dim); + margin-left: 8px; + } + + /* Hook/custom messages */ + .hook-message { + background: var(--customMessageBg); + color: var(--customMessageText); + padding: var(--line-height); + border-radius: 4px; + } + + .hook-type { + color: var(--customMessageLabel); + font-weight: bold; + } + + /* Skill invocation - matches compaction style (clickable, collapsed by default) */ + .skill-invocation { + background: var(--customMessageBg); + border-radius: 4px; + padding: var(--line-height); + cursor: pointer; + } + + .skill-invocation-label { + color: var(--customMessageLabel); + font-weight: bold; + } + + .skill-invocation-collapsed { + color: var(--customMessageText); + } + + .skill-invocation-content { + display: none; + color: var(--customMessageText); + margin-top: var(--line-height); + } + + .skill-invocation.expanded .skill-invocation-collapsed { + display: none; + } + + .skill-invocation.expanded .skill-invocation-content { + display: block; + } + + .skill-invocation + .user-message { + margin-top: var(--line-height); + } + + .skill-user-entry { + position: relative; + } + + /* Branch summary */ + .branch-summary { + background: var(--customMessageBg); + padding: var(--line-height); + border-radius: 4px; + } + + .branch-summary-header { + font-weight: bold; + color: var(--borderAccent); + } + + /* Error */ + .error-text { + color: var(--error); + padding: 0 var(--line-height); + } + .tool-error { + color: var(--error); + } + + /* Images */ + .message-images { + margin-bottom: 12px; + } + + .message-image { + max-width: 100%; + max-height: 400px; + border-radius: 4px; + margin: var(--line-height) 0; + } + + /* Markdown content */ + .markdown-content h1, + .markdown-content h2, + .markdown-content h3, + .markdown-content h4, + .markdown-content h5, + .markdown-content h6 { + color: var(--mdHeading); + margin: var(--line-height) 0 0 0; + font-weight: bold; + } + + .markdown-content h1 { font-size: 1em; } + .markdown-content h2 { font-size: 1em; } + .markdown-content h3 { font-size: 1em; } + .markdown-content h4 { font-size: 1em; } + .markdown-content h5 { font-size: 1em; } + .markdown-content h6 { font-size: 1em; } + .markdown-content p { margin: 0; } + .markdown-content p + p { margin-top: var(--line-height); } + + .markdown-content a { + color: var(--mdLink); + text-decoration: underline; + } + + .markdown-content code { + background: rgba(128, 128, 128, 0.2); + color: var(--mdCode); + padding: 0 4px; + border-radius: 3px; + font-family: inherit; + } + + .markdown-content pre { + background: transparent; + margin: var(--line-height) 0; + overflow-x: auto; + } + + .markdown-content pre code { + display: block; + background: none; + color: var(--text); + } + + .markdown-content blockquote { + border-left: 3px solid var(--mdQuoteBorder); + padding-left: var(--line-height); + margin: var(--line-height) 0; + color: var(--mdQuote); + font-style: italic; + } + + .markdown-content ul, + .markdown-content ol { + margin: var(--line-height) 0; + padding-left: calc(var(--line-height) * 2); + } + + .markdown-content li { margin: 0; } + .markdown-content li::marker { color: var(--mdListBullet); } + + .markdown-content hr { + border: none; + border-top: 1px solid var(--mdHr); + margin: var(--line-height) 0; + } + + .markdown-content table { + border-collapse: collapse; + margin: 0.5em 0; + width: 100%; + } + + .markdown-content th, + .markdown-content td { + border: 1px solid var(--mdCodeBlockBorder); + padding: 6px 10px; + text-align: left; + } + + .markdown-content th { + background: rgba(128, 128, 128, 0.1); + font-weight: bold; + } + + .markdown-content img { + max-width: 100%; + border-radius: 4px; + } + + /* Syntax highlighting */ + .hljs { background: transparent; color: var(--text); } + .hljs-comment, .hljs-quote { color: var(--syntaxComment); } + .hljs-keyword, .hljs-selector-tag { color: var(--syntaxKeyword); } + .hljs-number, .hljs-literal { color: var(--syntaxNumber); } + .hljs-string, .hljs-doctag { color: var(--syntaxString); } + /* Function names: hljs v11 uses .hljs-title.function_ compound class */ + .hljs-function, .hljs-title, .hljs-title.function_, .hljs-section, .hljs-name { color: var(--syntaxFunction); } + /* Types: hljs v11 uses .hljs-title.class_ for class names */ + .hljs-type, .hljs-class, .hljs-title.class_, .hljs-built_in { color: var(--syntaxType); } + .hljs-attr, .hljs-variable, .hljs-variable.language_, .hljs-params, .hljs-property { color: var(--syntaxVariable); } + .hljs-meta, .hljs-meta .hljs-keyword, .hljs-meta .hljs-string { color: var(--syntaxKeyword); } + .hljs-operator { color: var(--syntaxOperator); } + .hljs-punctuation { color: var(--syntaxPunctuation); } + .hljs-subst { color: var(--text); } + + /* Footer */ + .footer { + margin-top: 48px; + padding: 20px; + text-align: center; + color: var(--dim); + font-size: 10px; + } + + /* Mobile */ + #hamburger { + display: none; + position: fixed; + top: 10px; + left: 10px; + z-index: 100; + padding: 3px 8px; + font-size: 12px; + font-family: inherit; + background: transparent; + color: var(--muted); + border: 1px solid var(--dim); + border-radius: 3px; + cursor: pointer; + } + + #hamburger:hover { + color: var(--text); + border-color: var(--text); + } + + + + #sidebar-overlay { + display: none; + position: fixed; + top: 0; + left: 0; + right: 0; + bottom: 0; + background: rgba(0, 0, 0, 0.5); + z-index: 98; + } + + @media (max-width: 900px) { + #sidebar { + position: fixed; + left: 0; + width: min(var(--sidebar-width), 100vw); + min-width: min(var(--sidebar-width), 100vw); + max-width: min(var(--sidebar-width), 100vw); + top: 0; + bottom: 0; + height: 100vh; + z-index: 99; + transform: translateX(-100%); + transition: transform 0.3s; + } + + #sidebar.open { + transform: translateX(0); + } + + #sidebar-resizer { + display: none; + } + + #sidebar-overlay.open { + display: block; + } + + #hamburger { + display: block; + } + + .sidebar-close { + display: block; + } + + #content { + padding: var(--line-height) 16px; + } + + #content > * { + max-width: 100%; + } + } + + @media print { + #sidebar, #sidebar-resizer, #sidebar-toggle { display: none !important; } + body { background: white; color: black; } + #content { max-width: none; } + } diff --git a/packages/coding-agent/src/core/export-html/template.html b/packages/coding-agent/src/core/export-html/template.html new file mode 100644 index 0000000000000000000000000000000000000000..c1d678a0a2fc7df8a37dc952e270dde4b9ecba6a --- /dev/null +++ b/packages/coding-agent/src/core/export-html/template.html @@ -0,0 +1,55 @@ + + + + + + Session Export + + + + + +
+ + +
+
+
+
+
+ +
+
+ + + + + + + + + + + + + diff --git a/packages/coding-agent/src/core/export-html/template.js b/packages/coding-agent/src/core/export-html/template.js new file mode 100644 index 0000000000000000000000000000000000000000..cfdd16120371d53076be7612471ababc29a4455a --- /dev/null +++ b/packages/coding-agent/src/core/export-html/template.js @@ -0,0 +1,1864 @@ + (function() { + 'use strict'; + + // ============================================================ + // DATA LOADING + // ============================================================ + + const base64 = document.getElementById('session-data').textContent; + const binary = atob(base64); + const bytes = new Uint8Array(binary.length); + for (let i = 0; i < binary.length; i++) { + bytes[i] = binary.charCodeAt(i); + } + const data = JSON.parse(new TextDecoder('utf-8').decode(bytes)); + const { header, entries, leafId: defaultLeafId, systemPrompt, tools, renderedTools } = data; + + // ============================================================ + // URL PARAMETER HANDLING + // ============================================================ + + // Parse URL parameters for deep linking: leafId and targetId + // Check for injected params (when loaded in iframe via srcdoc) or use window.location + const injectedParams = document.querySelector('meta[name="pi-url-params"]'); + const searchString = injectedParams ? injectedParams.content : window.location.search.substring(1); + const urlParams = new URLSearchParams(searchString); + const urlLeafId = urlParams.get('leafId'); + const urlTargetId = urlParams.get('targetId'); + // Use URL leafId if provided, otherwise fall back to session default + const leafId = urlLeafId || defaultLeafId; + + // ============================================================ + // DATA STRUCTURES + // ============================================================ + + // Entry lookup by ID + const byId = new Map(); + for (const entry of entries) { + byId.set(entry.id, entry); + } + + // Tool call lookup (toolCallId -> {name, arguments}) + const toolCallMap = new Map(); + for (const entry of entries) { + if (entry.type === 'message' && entry.message.role === 'assistant') { + const content = entry.message.content; + if (Array.isArray(content)) { + for (const block of content) { + if (block.type === 'toolCall') { + toolCallMap.set(block.id, { name: block.name, arguments: block.arguments }); + } + } + } + } + } + + // Label lookup (entryId -> label string) + // Labels are stored in 'label' entries that reference their target via targetId + const labelMap = new Map(); + for (const entry of entries) { + if (entry.type === 'label' && entry.targetId && entry.label) { + labelMap.set(entry.targetId, entry.label); + } + } + + // ============================================================ + // TREE DATA PREPARATION (no DOM, pure data) + // ============================================================ + + /** + * Build tree structure from flat entries. + * Returns array of root nodes, each with { entry, children, label }. + */ + function buildTree() { + const nodeMap = new Map(); + const roots = []; + + // Create nodes + for (const entry of entries) { + nodeMap.set(entry.id, { + entry, + children: [], + label: labelMap.get(entry.id) + }); + } + + // Build parent-child relationships + for (const entry of entries) { + const node = nodeMap.get(entry.id); + if (entry.parentId === null || entry.parentId === undefined || entry.parentId === entry.id) { + roots.push(node); + } else { + const parent = nodeMap.get(entry.parentId); + if (parent) { + parent.children.push(node); + } else { + roots.push(node); + } + } + } + + // Sort children by timestamp + function sortChildren(node) { + node.children.sort((a, b) => + new Date(a.entry.timestamp).getTime() - new Date(b.entry.timestamp).getTime() + ); + node.children.forEach(sortChildren); + } + roots.forEach(sortChildren); + + return roots; + } + + /** + * Build set of entry IDs on path from root to target. + */ + function buildActivePathIds(targetId) { + const ids = new Set(); + let current = byId.get(targetId); + while (current) { + ids.add(current.id); + // Stop if no parent or self-referencing (root) + if (!current.parentId || current.parentId === current.id) { + break; + } + current = byId.get(current.parentId); + } + return ids; + } + + /** + * Get array of entries from root to target (the conversation path). + */ + function getPath(targetId) { + const path = []; + let current = byId.get(targetId); + while (current) { + path.unshift(current); + // Stop if no parent or self-referencing (root) + if (!current.parentId || current.parentId === current.id) { + break; + } + current = byId.get(current.parentId); + } + return path; + } + + // Tree node lookup for finding leaves + let treeNodeMap = null; + + /** + * Find the newest leaf node reachable from a given node. + * This allows clicking any node in a branch to show the full branch. + * Children are sorted by timestamp, so the newest is always last. + */ + function findNewestLeaf(nodeId) { + // Build tree node map lazily + if (!treeNodeMap) { + treeNodeMap = new Map(); + const tree = buildTree(); + function mapNodes(node) { + treeNodeMap.set(node.entry.id, node); + node.children.forEach(mapNodes); + } + tree.forEach(mapNodes); + } + + const node = treeNodeMap.get(nodeId); + if (!node) return nodeId; + + // Follow the newest (last) child at each level + let current = node; + while (current.children.length > 0) { + current = current.children[current.children.length - 1]; + } + return current.entry.id; + } + + /** + * Flatten tree into list with indentation and connector info. + * Returns array of { node, indent, showConnector, isLast, gutters, isVirtualRootChild, multipleRoots }. + * Matches tree-selector.ts logic exactly. + */ + function flattenTree(roots, activePathIds) { + const result = []; + const multipleRoots = roots.length > 1; + + // Mark which subtrees contain the active leaf + const containsActive = new Map(); + function markActive(node) { + let has = activePathIds.has(node.entry.id); + for (const child of node.children) { + if (markActive(child)) has = true; + } + containsActive.set(node, has); + return has; + } + roots.forEach(markActive); + + // Stack: [node, indent, justBranched, showConnector, isLast, gutters, isVirtualRootChild] + const stack = []; + + // Add roots (prioritize branch containing active leaf) + const orderedRoots = [...roots].sort((a, b) => + Number(containsActive.get(b)) - Number(containsActive.get(a)) + ); + for (let i = orderedRoots.length - 1; i >= 0; i--) { + const isLast = i === orderedRoots.length - 1; + stack.push([orderedRoots[i], multipleRoots ? 1 : 0, multipleRoots, multipleRoots, isLast, [], multipleRoots]); + } + + while (stack.length > 0) { + const [node, indent, justBranched, showConnector, isLast, gutters, isVirtualRootChild] = stack.pop(); + + result.push({ node, indent, showConnector, isLast, gutters, isVirtualRootChild, multipleRoots }); + + const children = node.children; + const multipleChildren = children.length > 1; + + // Order children (active branch first) + const orderedChildren = [...children].sort((a, b) => + Number(containsActive.get(b)) - Number(containsActive.get(a)) + ); + + // Calculate child indent (matches tree-selector.ts) + let childIndent; + if (multipleChildren) { + // Parent branches: children get +1 + childIndent = indent + 1; + } else if (justBranched && indent > 0) { + // First generation after a branch: +1 for visual grouping + childIndent = indent + 1; + } else { + // Single-child chain: stay flat + childIndent = indent; + } + + // Build gutters for children + const connectorDisplayed = showConnector && !isVirtualRootChild; + const currentDisplayIndent = multipleRoots ? Math.max(0, indent - 1) : indent; + const connectorPosition = Math.max(0, currentDisplayIndent - 1); + const childGutters = connectorDisplayed + ? [...gutters, { position: connectorPosition, show: !isLast }] + : gutters; + + // Add children in reverse order for stack + for (let i = orderedChildren.length - 1; i >= 0; i--) { + const childIsLast = i === orderedChildren.length - 1; + stack.push([orderedChildren[i], childIndent, multipleChildren, multipleChildren, childIsLast, childGutters, false]); + } + } + + return result; + } + + /** + * Build ASCII prefix string for tree node. + */ + function buildTreePrefix(flatNode) { + const { indent, showConnector, isLast, gutters, isVirtualRootChild, multipleRoots } = flatNode; + const displayIndent = multipleRoots ? Math.max(0, indent - 1) : indent; + const connector = showConnector && !isVirtualRootChild ? (isLast ? '└─ ' : '├─ ') : ''; + const connectorPosition = connector ? displayIndent - 1 : -1; + + const totalChars = displayIndent * 3; + const prefixChars = []; + for (let i = 0; i < totalChars; i++) { + const level = Math.floor(i / 3); + const posInLevel = i % 3; + + const gutter = gutters.find(g => g.position === level); + if (gutter) { + prefixChars.push(posInLevel === 0 ? (gutter.show ? '│' : ' ') : ' '); + } else if (connector && level === connectorPosition) { + if (posInLevel === 0) { + prefixChars.push(isLast ? '└' : '├'); + } else if (posInLevel === 1) { + prefixChars.push('─'); + } else { + prefixChars.push(' '); + } + } else { + prefixChars.push(' '); + } + } + return prefixChars.join(''); + } + + // ============================================================ + // FILTERING (pure data) + // ============================================================ + + let filterMode = 'default'; + let searchQuery = ''; + + function hasTextContent(content) { + if (typeof content === 'string') return content.trim().length > 0; + if (Array.isArray(content)) { + for (const c of content) { + if (c.type === 'text' && c.text && c.text.trim().length > 0) return true; + } + } + return false; + } + + function extractContent(content) { + if (typeof content === 'string') return content; + if (Array.isArray(content)) { + return content + .filter(c => c.type === 'text' && c.text) + .map(c => c.text) + .join(''); + } + return ''; + } + + /** + * Parse a skill block from message text. + * Returns null if the text doesn't contain a skill block. + * Matches the format: \n...\n\n\nuser message + */ + function parseSkillBlock(text) { + const match = text.match(/^\n([\s\S]*?)\n<\/skill>(?:\n\n([\s\S]+))?$/); + if (!match) return null; + return { + name: match[1], + location: match[2], + content: match[3], + userMessage: match[4]?.trim() || undefined, + }; + } + + function getSearchableText(entry, label) { + const parts = []; + if (label) parts.push(label); + + switch (entry.type) { + case 'message': { + const msg = entry.message; + parts.push(msg.role); + if (msg.content) parts.push(extractContent(msg.content)); + if (msg.role === 'bashExecution' && msg.command) parts.push(msg.command); + break; + } + case 'custom_message': + parts.push(entry.customType); + parts.push(typeof entry.content === 'string' ? entry.content : extractContent(entry.content)); + break; + case 'compaction': + parts.push('compaction'); + break; + case 'branch_summary': + parts.push('branch summary', entry.summary); + break; + case 'model_change': + parts.push('model', entry.modelId); + break; + case 'thinking_level_change': + parts.push('thinking', entry.thinkingLevel); + break; + } + + return parts.join(' ').toLowerCase(); + } + + /** + * Filter flat nodes based on current filterMode and searchQuery. + */ + function filterNodes(flatNodes, currentLeafId) { + const searchTokens = searchQuery.toLowerCase().split(/\s+/).filter(Boolean); + + const filtered = flatNodes.filter(flatNode => { + const entry = flatNode.node.entry; + const label = flatNode.node.label; + const isCurrentLeaf = entry.id === currentLeafId; + + // Always show current leaf + if (isCurrentLeaf) return true; + + // Hide assistant messages with only tool calls (no text) unless error/aborted + if (entry.type === 'message' && entry.message.role === 'assistant') { + const msg = entry.message; + const hasText = hasTextContent(msg.content); + const isErrorOrAborted = msg.stopReason && msg.stopReason !== 'stop' && msg.stopReason !== 'toolUse'; + if (!hasText && !isErrorOrAborted) return false; + } + + // Apply filter mode + const isSettingsEntry = ['label', 'custom', 'model_change', 'thinking_level_change'].includes(entry.type); + let passesFilter = true; + + switch (filterMode) { + case 'user-only': + passesFilter = entry.type === 'message' && entry.message.role === 'user'; + break; + case 'no-tools': + passesFilter = !isSettingsEntry && !(entry.type === 'message' && entry.message.role === 'toolResult'); + break; + case 'labeled-only': + passesFilter = label !== undefined; + break; + case 'all': + passesFilter = true; + break; + default: // 'default' + passesFilter = !isSettingsEntry; + break; + } + + if (!passesFilter) return false; + + // Apply search filter + if (searchTokens.length > 0) { + const nodeText = getSearchableText(entry, label); + if (!searchTokens.every(t => nodeText.includes(t))) return false; + } + + return true; + }); + + // Recalculate visual structure based on visible tree + recalculateVisualStructure(filtered, flatNodes); + + return filtered; + } + + /** + * Recompute indentation/connectors for the filtered view + * + * Filtering can hide intermediate entries; descendants attach to the nearest visible ancestor. + * Keep indentation semantics aligned with flattenTree() so single-child chains don't drift right. + */ + function recalculateVisualStructure(filteredNodes, allFlatNodes) { + if (filteredNodes.length === 0) return; + + const visibleIds = new Set(filteredNodes.map(n => n.node.entry.id)); + + // Build entry map for parent lookup (using full tree) + const entryMap = new Map(); + for (const flatNode of allFlatNodes) { + entryMap.set(flatNode.node.entry.id, flatNode); + } + + // Find nearest visible ancestor for a node + function findVisibleAncestor(nodeId) { + let currentId = entryMap.get(nodeId)?.node.entry.parentId; + while (currentId != null) { + if (visibleIds.has(currentId)) { + return currentId; + } + currentId = entryMap.get(currentId)?.node.entry.parentId; + } + return null; + } + + // Build visible tree structure + const visibleParent = new Map(); + const visibleChildren = new Map(); + visibleChildren.set(null, []); // root-level nodes + + for (const flatNode of filteredNodes) { + const nodeId = flatNode.node.entry.id; + const ancestorId = findVisibleAncestor(nodeId); + visibleParent.set(nodeId, ancestorId); + + if (!visibleChildren.has(ancestorId)) { + visibleChildren.set(ancestorId, []); + } + visibleChildren.get(ancestorId).push(nodeId); + } + + // Update multipleRoots based on visible roots + const visibleRootIds = visibleChildren.get(null); + const multipleRoots = visibleRootIds.length > 1; + + // Build a map for quick lookup: nodeId → FlatNode + const filteredNodeMap = new Map(); + for (const flatNode of filteredNodes) { + filteredNodeMap.set(flatNode.node.entry.id, flatNode); + } + + // DFS traversal of visible tree, applying same indentation rules as flattenTree() + // Stack items: [nodeId, indent, justBranched, showConnector, isLast, gutters, isVirtualRootChild] + const stack = []; + + // Add visible roots in reverse order (to process in forward order via stack) + for (let i = visibleRootIds.length - 1; i >= 0; i--) { + const isLast = i === visibleRootIds.length - 1; + stack.push([ + visibleRootIds[i], + multipleRoots ? 1 : 0, + multipleRoots, + multipleRoots, + isLast, + [], + multipleRoots + ]); + } + + while (stack.length > 0) { + const [nodeId, indent, justBranched, showConnector, isLast, gutters, isVirtualRootChild] = stack.pop(); + + const flatNode = filteredNodeMap.get(nodeId); + if (!flatNode) continue; + + // Update this node's visual properties + flatNode.indent = indent; + flatNode.showConnector = showConnector; + flatNode.isLast = isLast; + flatNode.gutters = gutters; + flatNode.isVirtualRootChild = isVirtualRootChild; + flatNode.multipleRoots = multipleRoots; + + // Get visible children of this node + const children = visibleChildren.get(nodeId) || []; + const multipleChildren = children.length > 1; + + // Calculate child indent using same rules as flattenTree(): + // - Parent branches (multiple children): children get +1 + // - Just branched and indent > 0: children get +1 for visual grouping + // - Single-child chain: stay flat + let childIndent; + if (multipleChildren) { + childIndent = indent + 1; + } else if (justBranched && indent > 0) { + childIndent = indent + 1; + } else { + childIndent = indent; + } + + // Build gutters for children (same logic as flattenTree) + const connectorDisplayed = showConnector && !isVirtualRootChild; + const currentDisplayIndent = multipleRoots ? Math.max(0, indent - 1) : indent; + const connectorPosition = Math.max(0, currentDisplayIndent - 1); + const childGutters = connectorDisplayed + ? [...gutters, { position: connectorPosition, show: !isLast }] + : gutters; + + // Add children in reverse order (to process in forward order via stack) + for (let i = children.length - 1; i >= 0; i--) { + const childIsLast = i === children.length - 1; + stack.push([ + children[i], + childIndent, + multipleChildren, + multipleChildren, + childIsLast, + childGutters, + false + ]); + } + } + } + + // ============================================================ + // TREE DISPLAY TEXT (pure data -> string) + // ============================================================ + + function shortenPath(p) { + if (typeof p !== 'string') return ''; + if (p.startsWith('/Users/')) { + const parts = p.split('/'); + if (parts.length > 2) return '~' + p.slice(('/Users/' + parts[2]).length); + } + if (p.startsWith('/home/')) { + const parts = p.split('/'); + if (parts.length > 2) return '~' + p.slice(('/home/' + parts[2]).length); + } + return p; + } + + function formatToolCall(name, args) { + switch (name) { + case 'read': { + const path = shortenPath(String(args.path || args.file_path || '')); + const offset = args.offset; + const limit = args.limit; + let display = path; + if (offset !== undefined || limit !== undefined) { + const start = offset ?? 1; + const end = limit !== undefined ? start + limit - 1 : ''; + display += `:${start}${end ? `-${end}` : ''}`; + } + return `[read: ${display}]`; + } + case 'write': + return `[write: ${shortenPath(String(args.path || args.file_path || ''))}]`; + case 'edit': + return `[edit: ${shortenPath(String(args.path || args.file_path || ''))}]`; + case 'bash': { + const rawCmd = String(args.command || ''); + const cmd = rawCmd.replace(/[\n\t]/g, ' ').trim().slice(0, 50); + return `[bash: ${cmd}${rawCmd.length > 50 ? '...' : ''}]`; + } + case 'grep': + return `[grep: /${args.pattern || ''}/ in ${shortenPath(String(args.path || '.'))}]`; + case 'find': + return `[find: ${args.pattern || ''} in ${shortenPath(String(args.path || '.'))}]`; + case 'ls': + return `[ls: ${shortenPath(String(args.path || '.'))}]`; + default: { + const argsStr = JSON.stringify(args).slice(0, 40); + return `[${name}: ${argsStr}${JSON.stringify(args).length > 40 ? '...' : ''}]`; + } + } + } + + function escapeHtml(text) { + return String(text) + .replace(/&/g, '&') + .replace(//g, '>') + .replace(/"/g, '"') + .replace(/'/g, '''); + } + + function sanitizeMarkdownUrl(value) { + const href = String(value || '').trim().replace(/[\x00-\x1f\x7f]/g, ''); + if (!href) return href; + + const scheme = href.match(/^([A-Za-z][A-Za-z0-9+.-]*):/); + if (scheme && !/^(https?|mailto|tel|ftp)$/i.test(scheme[1])) { + return null; + } + + return href; + } + + /** + * Truncate string to maxLen chars, append "..." if truncated. + */ + function truncate(s, maxLen = 100) { + if (s.length <= maxLen) return s; + return s.slice(0, maxLen) + '...'; + } + + /** + * Get display text for tree node (returns HTML string). + */ + function getTreeNodeDisplayHtml(entry, label) { + const normalize = s => s.replace(/[\n\t]/g, ' ').trim(); + const labelHtml = label ? `[${escapeHtml(label)}] ` : ''; + + switch (entry.type) { + case 'message': { + const msg = entry.message; + if (msg.role === 'user') { + const rawContent = extractContent(msg.content); + const skillBlock = parseSkillBlock(rawContent); + if (skillBlock) { + let treeHtml = labelHtml + `skill: ${escapeHtml(skillBlock.name)}`; + if (skillBlock.userMessage) { + treeHtml += ` · user: ${escapeHtml(truncate(normalize(skillBlock.userMessage)))}`; + } + return treeHtml; + } + const content = truncate(normalize(rawContent)); + return labelHtml + `user: ${escapeHtml(content)}`; + } + if (msg.role === 'assistant') { + const textContent = truncate(normalize(extractContent(msg.content))); + if (textContent) { + return labelHtml + `assistant: ${escapeHtml(textContent)}`; + } + if (msg.stopReason === 'aborted') { + return labelHtml + `assistant: (aborted)`; + } + if (msg.errorMessage) { + return labelHtml + `assistant: ${escapeHtml(truncate(msg.errorMessage))}`; + } + return labelHtml + `assistant: (no text)`; + } + if (msg.role === 'toolResult') { + const toolCall = msg.toolCallId ? toolCallMap.get(msg.toolCallId) : null; + if (toolCall) { + return labelHtml + `${escapeHtml(formatToolCall(toolCall.name, toolCall.arguments))}`; + } + return labelHtml + `[${escapeHtml(msg.toolName || 'tool')}]`; + } + if (msg.role === 'bashExecution') { + const cmd = truncate(normalize(msg.command || '')); + return labelHtml + `[bash]: ${escapeHtml(cmd)}`; + } + return labelHtml + `[${escapeHtml(msg.role)}]`; + } + case 'compaction': + return labelHtml + `[compaction: ${Math.round(entry.tokensBefore/1000)}k tokens]`; + case 'branch_summary': { + const summary = truncate(normalize(entry.summary || '')); + return labelHtml + `[branch summary]: ${escapeHtml(summary)}`; + } + case 'custom_message': { + const content = typeof entry.content === 'string' ? entry.content : extractContent(entry.content); + return labelHtml + `[${escapeHtml(entry.customType)}]: ${escapeHtml(truncate(normalize(content)))}`; + } + case 'model_change': + return labelHtml + `[model: ${escapeHtml(entry.modelId)}]`; + case 'thinking_level_change': + return labelHtml + `[thinking: ${escapeHtml(entry.thinkingLevel)}]`; + default: + return labelHtml + `[${escapeHtml(entry.type)}]`; + } + } + + // ============================================================ + // TREE RENDERING (DOM manipulation) + // ============================================================ + + let currentLeafId = leafId; + let currentTargetId = urlTargetId || leafId; + let treeRendered = false; + + function renderTree() { + const tree = buildTree(); + const activePathIds = buildActivePathIds(currentLeafId); + const flatNodes = flattenTree(tree, activePathIds); + const filtered = filterNodes(flatNodes, currentLeafId); + const container = document.getElementById('tree-container'); + + // Full render only on first call or when filter/search changes + if (!treeRendered) { + container.innerHTML = ''; + + for (const flatNode of filtered) { + const entry = flatNode.node.entry; + const isOnPath = activePathIds.has(entry.id); + const isTarget = entry.id === currentTargetId; + + const div = document.createElement('div'); + div.className = 'tree-node'; + if (isOnPath) div.classList.add('in-path'); + if (isTarget) div.classList.add('active'); + div.dataset.id = entry.id; + + const prefix = buildTreePrefix(flatNode); + const prefixSpan = document.createElement('span'); + prefixSpan.className = 'tree-prefix'; + prefixSpan.textContent = prefix; + + const marker = document.createElement('span'); + marker.className = 'tree-marker'; + marker.textContent = isOnPath ? '•' : ' '; + + const content = document.createElement('span'); + content.className = 'tree-content'; + content.innerHTML = getTreeNodeDisplayHtml(entry, flatNode.node.label); + + div.appendChild(prefixSpan); + div.appendChild(marker); + div.appendChild(content); + // Navigate to the newest leaf through this node, but scroll to the clicked node + div.addEventListener('click', () => { + if (window.getSelection().toString()) return; + const leafId = findNewestLeaf(entry.id); + navigateTo(leafId, 'target', entry.id); + }); + + container.appendChild(div); + } + + treeRendered = true; + } else { + // Just update markers and classes + const nodes = container.querySelectorAll('.tree-node'); + for (const node of nodes) { + const id = node.dataset.id; + const isOnPath = activePathIds.has(id); + const isTarget = id === currentTargetId; + + node.classList.toggle('in-path', isOnPath); + node.classList.toggle('active', isTarget); + + const marker = node.querySelector('.tree-marker'); + if (marker) { + marker.textContent = isOnPath ? '•' : ' '; + } + } + } + + document.getElementById('tree-status').textContent = `${filtered.length} / ${flatNodes.length} entries`; + + // Scroll active node into view after layout + setTimeout(() => { + const activeNode = container.querySelector('.tree-node.active'); + if (activeNode) { + activeNode.scrollIntoView({ block: 'nearest' }); + } + }, 0); + } + + function forceTreeRerender() { + treeRendered = false; + renderTree(); + } + + // ============================================================ + // MESSAGE RENDERING + // ============================================================ + + function formatTokens(count) { + if (count < 1000) return count.toString(); + if (count < 10000) return (count / 1000).toFixed(1) + 'k'; + if (count < 1000000) return Math.round(count / 1000) + 'k'; + return (count / 1000000).toFixed(1) + 'M'; + } + + function formatTimestamp(ts) { + if (!ts) return ''; + const date = new Date(ts); + return date.toLocaleTimeString(undefined, { hour: '2-digit', minute: '2-digit', second: '2-digit' }); + } + + function replaceTabs(text) { + return text.replace(/\t/g, ' '); + } + + /** Safely coerce value to string for display. Returns null if invalid type. */ + function str(value) { + if (typeof value === 'string') return value; + if (value == null) return ''; + return null; + } + + function getLanguageFromPath(filePath) { + const ext = filePath.split('.').pop()?.toLowerCase(); + const extToLang = { + ts: 'typescript', tsx: 'typescript', js: 'javascript', jsx: 'javascript', + py: 'python', rb: 'ruby', rs: 'rust', go: 'go', java: 'java', + c: 'c', cpp: 'cpp', h: 'c', hpp: 'cpp', cs: 'csharp', + php: 'php', sh: 'bash', bash: 'bash', zsh: 'bash', + sql: 'sql', html: 'html', css: 'css', scss: 'scss', + json: 'json', yaml: 'yaml', yml: 'yaml', xml: 'xml', + md: 'markdown', dockerfile: 'dockerfile' + }; + return extToLang[ext]; + } + + function findToolResult(toolCallId) { + for (const entry of entries) { + if (entry.type === 'message' && entry.message.role === 'toolResult') { + if (entry.message.toolCallId === toolCallId) { + return entry.message; + } + } + } + return null; + } + + function formatExpandableOutput(text, maxLines, lang) { + text = replaceTabs(text); + const lines = text.split('\n'); + const displayLines = lines.slice(0, maxLines); + const remaining = lines.length - maxLines; + + if (lang) { + let highlighted; + try { + highlighted = hljs.highlight(text, { language: lang }).value; + } catch { + highlighted = escapeHtml(text); + } + + if (remaining > 0) { + const previewCode = displayLines.join('\n'); + let previewHighlighted; + try { + previewHighlighted = hljs.highlight(previewCode, { language: lang }).value; + } catch { + previewHighlighted = escapeHtml(previewCode); + } + + return ``; + } + + return `
${highlighted}
`; + } + + // Plain text output + if (remaining > 0) { + let out = ''; + return out; + } + + let out = '
'; + for (const line of displayLines) { + out += `
${escapeHtml(replaceTabs(line))}
`; + } + out += '
'; + return out; + } + + function renderToolCall(call) { + const result = findToolResult(call.id); + const isError = result?.isError || false; + const statusClass = result ? (isError ? 'error' : 'success') : 'pending'; + + const getResultText = () => { + if (!result) return ''; + const textBlocks = result.content.filter(c => c.type === 'text'); + return textBlocks.map(c => c.text).join('\n'); + }; + + const getResultImages = () => { + if (!result) return []; + return result.content.filter(c => c.type === 'image'); + }; + + const renderResultImages = () => { + const images = getResultImages(); + if (images.length === 0) return ''; + return '
' + + images.map(img => ``).join('') + + '
'; + }; + + const toolDomId = `tool-call-${escapeHtml(call.id)}`; + let html = `
`; + const args = call.arguments || {}; + const name = call.name; + + const invalidArg = '[invalid arg]'; + + switch (name) { + case 'bash': { + const command = str(args.command); + const cmdDisplay = command === null ? invalidArg : escapeHtml(command || '...'); + html += `
$ ${cmdDisplay}
`; + if (result) { + const output = getResultText().trim(); + if (output) html += formatExpandableOutput(output, 5); + } + break; + } + case 'read': { + const filePath = str(args.file_path ?? args.path); + const offset = args.offset; + const limit = args.limit; + + let pathHtml = filePath === null ? invalidArg : escapeHtml(shortenPath(filePath || '')); + if (filePath !== null && (offset !== undefined || limit !== undefined)) { + const startLine = offset ?? 1; + const endLine = limit !== undefined ? startLine + limit - 1 : ''; + pathHtml += `:${startLine}${endLine ? '-' + endLine : ''}`; + } + + html += `
read ${pathHtml}
`; + if (result) { + html += renderResultImages(); + const output = getResultText(); + const lang = filePath ? getLanguageFromPath(filePath) : null; + if (output) html += formatExpandableOutput(output, 10, lang); + } + break; + } + case 'write': { + const filePath = str(args.file_path ?? args.path); + const content = str(args.content); + + html += `
write ${filePath === null ? invalidArg : escapeHtml(shortenPath(filePath || ''))}`; + if (content !== null && content) { + const lines = content.split('\n'); + if (lines.length > 10) html += ` (${lines.length} lines)`; + } + html += '
'; + + if (content === null) { + html += `
[invalid content arg - expected string]
`; + } else if (content) { + const lang = filePath ? getLanguageFromPath(filePath) : null; + html += formatExpandableOutput(content, 10, lang); + } + if (result) { + const output = getResultText().trim(); + if (output) html += `
${escapeHtml(output)}
`; + } + break; + } + case 'edit': { + const filePath = str(args.file_path ?? args.path); + html += `
edit ${filePath === null ? invalidArg : escapeHtml(shortenPath(filePath || ''))}
`; + + if (result?.details?.diff) { + const diffLines = result.details.diff.split('\n'); + html += '
'; + for (const line of diffLines) { + const cls = line.match(/^\+/) ? 'diff-added' : line.match(/^-/) ? 'diff-removed' : 'diff-context'; + html += `
${escapeHtml(replaceTabs(line))}
`; + } + html += '
'; + } else if (result) { + const output = getResultText().trim(); + if (output) html += `
${escapeHtml(output)}
`; + } + break; + } + case 'ls': { + const dirPath = str(args.path); + const limit = args.limit; + + let pathHtml = dirPath === null ? invalidArg : escapeHtml(shortenPath(dirPath || '.')); + if (limit !== undefined) { + pathHtml += ` (limit ${escapeHtml(String(limit))})`; + } + + html += `
ls ${pathHtml}
`; + if (result) { + const output = getResultText().trim(); + if (output) html += formatExpandableOutput(output, 20); + } + break; + } + default: { + // Check for pre-rendered custom tool HTML + const rendered = renderedTools?.[call.id]; + if (rendered?.callHtml || rendered?.resultHtmlCollapsed || rendered?.resultHtmlExpanded) { + // Custom tool with pre-rendered HTML from TUI renderer + if (rendered.callHtml) { + html += `
${rendered.callHtml}
`; + } else { + html += `
${escapeHtml(name)}
`; + } + + if (rendered.resultHtmlCollapsed && rendered.resultHtmlExpanded && rendered.resultHtmlCollapsed !== rendered.resultHtmlExpanded) { + // Both collapsed and expanded differ - render expandable section + html += ``; + } else if (rendered.resultHtmlExpanded) { + // Only expanded exists (or collapsed is identical) - show directly + html += `
${rendered.resultHtmlExpanded}
`; + } else if (result) { + // No pre-rendered result HTML - fallback to JSON + const output = getResultText(); + if (output) html += formatExpandableOutput(output, 10); + } + } else { + // Fallback to JSON display (existing behavior) + html += `
${escapeHtml(name)}
`; + html += `
${escapeHtml(JSON.stringify(args, null, 2))}
`; + if (result) { + const output = getResultText(); + if (output) html += formatExpandableOutput(output, 10); + } + } + } + } + + html += '
'; + return html; + } + + /** + * Download the session data as a JSONL file. + * Reconstructs the original format: header line + entry lines. + */ + window.downloadSessionJson = function() { + // Build JSONL content: header first, then all entries + const lines = []; + if (header) { + lines.push(JSON.stringify({ type: 'header', ...header })); + } + for (const entry of entries) { + lines.push(JSON.stringify(entry)); + } + const jsonlContent = lines.join('\n'); + + // Create download + const blob = new Blob([jsonlContent], { type: 'application/x-ndjson' }); + const url = URL.createObjectURL(blob); + const a = document.createElement('a'); + a.href = url; + a.download = `${header?.id || 'session'}.jsonl`; + document.body.appendChild(a); + a.click(); + document.body.removeChild(a); + URL.revokeObjectURL(url); + } + + /** + * Build a shareable URL for a specific message. + * URL format: base?gistId&leafId=&targetId= + */ + function buildShareUrl(entryId) { + // Check for injected base URL (used when loaded in iframe via srcdoc) + const baseUrlMeta = document.querySelector('meta[name="pi-share-base-url"]'); + const baseUrl = baseUrlMeta ? baseUrlMeta.content : window.location.href.split('?')[0]; + + const url = new URL(window.location.href); + // Find the gist ID (first query param without value, e.g., ?abc123) + const gistId = Array.from(url.searchParams.keys()).find(k => !url.searchParams.get(k)); + + // Build the share URL + const params = new URLSearchParams(); + params.set('leafId', currentLeafId); + params.set('targetId', entryId); + + // If we have an injected base URL (iframe context), use it directly + if (baseUrlMeta) { + return `${baseUrl}&${params.toString()}`; + } + + // Otherwise build from current location (direct file access) + url.search = gistId ? `?${gistId}&${params.toString()}` : `?${params.toString()}`; + return url.toString(); + } + + /** + * Copy text to clipboard with visual feedback. + * Uses navigator.clipboard with fallback to execCommand for HTTP contexts. + */ + async function copyToClipboard(text, button) { + let success = false; + try { + if (navigator.clipboard && navigator.clipboard.writeText) { + await navigator.clipboard.writeText(text); + success = true; + } + } catch (err) { + // Clipboard API failed, try fallback + } + + // Fallback for HTTP or when Clipboard API is unavailable + if (!success) { + try { + const textarea = document.createElement('textarea'); + textarea.value = text; + textarea.style.position = 'fixed'; + textarea.style.opacity = '0'; + document.body.appendChild(textarea); + textarea.select(); + success = document.execCommand('copy'); + document.body.removeChild(textarea); + } catch (err) { + console.error('Failed to copy:', err); + } + } + + if (success && button) { + const originalHtml = button.innerHTML; + button.innerHTML = '✓'; + button.classList.add('copied'); + setTimeout(() => { + button.innerHTML = originalHtml; + button.classList.remove('copied'); + }, 1500); + } + } + + /** + * Render the copy-link button HTML for a message. + */ + function renderCopyLinkButton(entryId) { + return ``; + } + + function renderEntry(entry) { + const ts = formatTimestamp(entry.timestamp); + const tsHtml = ts ? `
${ts}
` : ''; + const entryDomId = `entry-${escapeHtml(entry.id)}`; + const copyBtnHtml = renderCopyLinkButton(entry.id); + + if (entry.type === 'message') { + const msg = entry.message; + + if (msg.role === 'user') { + const content = msg.content; + const text = typeof content === 'string' ? content : + content.filter(c => c.type === 'text').map(c => c.text).join('\n'); + const skillBlock = parseSkillBlock(text); + + if (skillBlock) { + // Collect images from content array + const images = Array.isArray(content) ? content.filter(c => c.type === 'image') : []; + const hasUserContent = skillBlock.userMessage || images.length > 0; + let html = `
${copyBtnHtml}${tsHtml}`; + + // Skill invocation (collapsed by default, click to expand) + html += `
+
[skill] ${escapeHtml(skillBlock.name)}
+
${escapeHtml(skillBlock.name)} (click to expand)
+
${safeMarkedParse(skillBlock.content)}
+
`; + + // User message (separate block if present) + if (hasUserContent) { + html += '
'; + if (images.length > 0) { + html += '
'; + for (const img of images) { + html += ``; + } + html += '
'; + } + if (skillBlock.userMessage) { + html += `
${safeMarkedParse(skillBlock.userMessage)}
`; + } + html += '
'; + } + + html += '
'; + return html; + } + + // No skill block - normal user message + let html = `
${copyBtnHtml}${tsHtml}`; + + if (Array.isArray(content)) { + const images = content.filter(c => c.type === 'image'); + if (images.length > 0) { + html += '
'; + for (const img of images) { + html += ``; + } + html += '
'; + } + } + + if (text.trim()) { + html += `
${safeMarkedParse(text)}
`; + } + html += '
'; + return html; + } + + if (msg.role === 'assistant') { + let html = `
${copyBtnHtml}${tsHtml}`; + + for (const block of msg.content) { + if (block.type === 'text' && block.text.trim()) { + html += `
${safeMarkedParse(block.text)}
`; + } else if (block.type === 'thinking' && block.thinking.trim()) { + html += `
+
${escapeHtml(block.thinking)}
+
Thinking ...
+
`; + } + } + + for (const block of msg.content) { + if (block.type === 'toolCall') { + html += renderToolCall(block); + } + } + + if (msg.stopReason === 'aborted') { + html += '
Aborted
'; + } else if (msg.stopReason === 'error') { + html += `
Error: ${escapeHtml(msg.errorMessage || 'Unknown error')}
`; + } + + html += '
'; + return html; + } + + if (msg.role === 'bashExecution') { + const isError = msg.cancelled || (msg.exitCode !== 0 && msg.exitCode !== null); + let html = `
${tsHtml}`; + html += `
$ ${escapeHtml(msg.command)}
`; + if (msg.output) html += formatExpandableOutput(msg.output, 10); + if (msg.cancelled) { + html += '
(cancelled)
'; + } else if (msg.exitCode !== 0 && msg.exitCode !== null) { + html += `
(exit ${msg.exitCode})
`; + } + html += '
'; + return html; + } + + if (msg.role === 'toolResult') return ''; + } + + if (entry.type === 'model_change') { + return `
${tsHtml}Switched to model: ${escapeHtml(entry.provider)}/${escapeHtml(entry.modelId)}
`; + } + + if (entry.type === 'compaction') { + return `
+
[compaction]
+
Compacted from ${entry.tokensBefore.toLocaleString()} tokens
+
Compacted from ${entry.tokensBefore.toLocaleString()} tokens\n\n${escapeHtml(entry.summary)}
+
`; + } + + if (entry.type === 'branch_summary') { + return `
${tsHtml} +
Branch Summary
+
${safeMarkedParse(entry.summary)}
+
`; + } + + if (entry.type === 'custom_message' && entry.display) { + return `
${tsHtml} +
[${escapeHtml(entry.customType)}]
+
${safeMarkedParse(typeof entry.content === 'string' ? entry.content : JSON.stringify(entry.content))}
+
`; + } + + return ''; + } + + // ============================================================ + // HEADER / STATS + // ============================================================ + + function computeStats(entryList) { + let userMessages = 0, assistantMessages = 0, toolResults = 0; + let customMessages = 0, compactions = 0, branchSummaries = 0, toolCalls = 0; + const tokens = { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 }; + const cost = { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 }; + const models = new Set(); + + for (const entry of entryList) { + if (entry.type === 'message') { + const msg = entry.message; + if (msg.role === 'user') userMessages++; + if (msg.role === 'assistant') { + assistantMessages++; + if (msg.model) models.add(msg.provider ? `${msg.provider}/${msg.model}` : msg.model); + if (msg.usage) { + tokens.input += msg.usage.input || 0; + tokens.output += msg.usage.output || 0; + tokens.cacheRead += msg.usage.cacheRead || 0; + tokens.cacheWrite += msg.usage.cacheWrite || 0; + if (msg.usage.cost) { + cost.input += msg.usage.cost.input || 0; + cost.output += msg.usage.cost.output || 0; + cost.cacheRead += msg.usage.cost.cacheRead || 0; + cost.cacheWrite += msg.usage.cost.cacheWrite || 0; + } + } + toolCalls += msg.content.filter(c => c.type === 'toolCall').length; + } + if (msg.role === 'toolResult') toolResults++; + } else if (entry.type === 'compaction') { + compactions++; + } else if (entry.type === 'branch_summary') { + branchSummaries++; + } else if (entry.type === 'custom_message') { + customMessages++; + } + } + + return { userMessages, assistantMessages, toolResults, customMessages, compactions, branchSummaries, toolCalls, tokens, cost, models: Array.from(models) }; + } + + const globalStats = computeStats(entries); + + function renderHeader() { + const totalCost = globalStats.cost.input + globalStats.cost.output + globalStats.cost.cacheRead + globalStats.cost.cacheWrite; + + const tokenParts = []; + if (globalStats.tokens.input) tokenParts.push(`↑${formatTokens(globalStats.tokens.input)}`); + if (globalStats.tokens.output) tokenParts.push(`↓${formatTokens(globalStats.tokens.output)}`); + if (globalStats.tokens.cacheRead) tokenParts.push(`R${formatTokens(globalStats.tokens.cacheRead)}`); + if (globalStats.tokens.cacheWrite) tokenParts.push(`W${formatTokens(globalStats.tokens.cacheWrite)}`); + + const msgParts = []; + if (globalStats.userMessages) msgParts.push(`${globalStats.userMessages} user`); + if (globalStats.assistantMessages) msgParts.push(`${globalStats.assistantMessages} assistant`); + if (globalStats.toolResults) msgParts.push(`${globalStats.toolResults} tool results`); + if (globalStats.customMessages) msgParts.push(`${globalStats.customMessages} custom`); + if (globalStats.compactions) msgParts.push(`${globalStats.compactions} compactions`); + if (globalStats.branchSummaries) msgParts.push(`${globalStats.branchSummaries} branch summaries`); + + let html = ` +
+

Session: ${escapeHtml(header?.id || 'unknown')}

+
+ T toggle thinking · O toggle tools +
+ + + +
+
+
+
Date:${header?.timestamp ? new Date(header.timestamp).toLocaleString() : 'unknown'}
+
Models:${escapeHtml(globalStats.models.join(', ') || 'unknown')}
+
Messages:${msgParts.join(', ') || '0'}
+
Tool Calls:${globalStats.toolCalls}
+
Tokens:${tokenParts.join(' ') || '0'}
+
Cost:$${totalCost.toFixed(3)}
+
+
`; + + // Render system prompt (user's base prompt, applies to all providers) + if (systemPrompt) { + const lines = systemPrompt.split('\n'); + const previewLines = 10; + if (lines.length > previewLines) { + const preview = lines.slice(0, previewLines).join('\n'); + const remaining = lines.length - previewLines; + html += ``; + } else { + html += `
+
System Prompt
+
${escapeHtml(systemPrompt)}
+
`; + } + } + + if (tools && tools.length > 0) { + html += `
+
Available Tools
+
+ ${tools.map(t => { + const hasParams = t.parameters && typeof t.parameters === 'object' && t.parameters.properties && Object.keys(t.parameters.properties).length > 0; + if (!hasParams) { + return `
${escapeHtml(t.name)} - ${escapeHtml(t.description)}
`; + } + const params = t.parameters; + const properties = params.properties; + const required = params.required || []; + let paramsHtml = ''; + for (const [name, prop] of Object.entries(properties)) { + const isRequired = required.includes(name); + const typeStr = prop.type || 'any'; + const reqLabel = isRequired ? 'required' : 'optional'; + paramsHtml += `
${escapeHtml(name)} ${escapeHtml(typeStr)} ${reqLabel}`; + if (prop.description) { + paramsHtml += `
${escapeHtml(prop.description)}
`; + } + paramsHtml += `
`; + } + return `
${escapeHtml(t.name)} - ${escapeHtml(t.description)}
${paramsHtml}
`; + }).join('')} +
+
`; + } + + return html; + } + + // ============================================================ + // NAVIGATION + // ============================================================ + + // Cache for rendered entry DOM nodes + const entryCache = new Map(); + + function getScrollTargetElementId(entryId) { + const entry = byId.get(entryId); + if (entry?.type === 'message' && entry.message.role === 'toolResult' && entry.message.toolCallId) { + // getElementById() matches the parsed DOM id attribute, whose HTML entities + // were already resolved from the escaped id rendered by renderToolCall(). + return `tool-call-${entry.message.toolCallId}`; + } + return `entry-${entryId}`; + } + + function renderEntryToNode(entry) { + // Check cache first + if (entryCache.has(entry.id)) { + return entryCache.get(entry.id).cloneNode(true); + } + + // Render to HTML string, then parse to node + const html = renderEntry(entry); + if (!html) return null; + + const template = document.createElement('template'); + template.innerHTML = html; + const node = template.content.firstElementChild; + + // Cache the node + if (node) { + entryCache.set(entry.id, node.cloneNode(true)); + } + return node; + } + + function navigateTo(targetId, scrollMode = 'target', scrollToEntryId = null) { + currentLeafId = targetId; + currentTargetId = scrollToEntryId || targetId; + const path = getPath(targetId); + + renderTree(); + + document.getElementById('header-container').innerHTML = renderHeader(); + attachHeaderHandlers(); + + // Build messages using cached DOM nodes + const messagesEl = document.getElementById('messages'); + const fragment = document.createDocumentFragment(); + + for (const entry of path) { + const node = renderEntryToNode(entry); + if (node) { + fragment.appendChild(node); + } + } + + messagesEl.innerHTML = ''; + messagesEl.appendChild(fragment); + + // Attach click handlers for copy-link buttons + messagesEl.querySelectorAll('.copy-link-btn').forEach(btn => { + btn.addEventListener('click', (e) => { + e.stopPropagation(); + const entryId = btn.dataset.entryId; + const shareUrl = buildShareUrl(entryId); + copyToClipboard(shareUrl, btn); + }); + }); + + // Use setTimeout(0) to ensure DOM is fully laid out before scrolling + setTimeout(() => { + const content = document.getElementById('content'); + if (scrollMode === 'bottom') { + content.scrollTop = content.scrollHeight; + } else if (scrollMode === 'target') { + // If scrollToEntryId is provided, scroll to that specific entry. + // Tool result entries are rendered inside their assistant tool-call block, + // so route them to the visible tool-call element instead. + const scrollTargetId = scrollToEntryId || targetId; + const targetEl = document.getElementById(getScrollTargetElementId(scrollTargetId)) || + document.getElementById(`entry-${scrollTargetId}`); + if (targetEl) { + targetEl.scrollIntoView({ block: 'center' }); + // Briefly highlight the target message + if (scrollToEntryId) { + targetEl.classList.add('highlight'); + setTimeout(() => targetEl.classList.remove('highlight'), 2000); + } + } + } + }, 0); + } + + // ============================================================ + // INITIALIZATION + // ============================================================ + + // Configure marked with syntax highlighting and TUI-compatible HTML handling + const strictStrikethroughRegex = /^(~~)(?=[^\s~])((?:\\.|[^\\])*?(?:\\.|[^\s~\\]))\1(?=[^~]|$)/; + + marked.use({ + breaks: true, + gfm: true, + tokenizer: { + // Treat HTML-like input as plain text so tags are shown verbatim, + // matching the TUI markdown renderer. + html() { + return undefined; + }, + tag() { + return undefined; + }, + del(src) { + const match = strictStrikethroughRegex.exec(src); + if (!match) return undefined; + return { + type: 'del', + raw: match[0], + text: match[2], + tokens: this.lexer.inlineTokens(match[2]) + }; + } + }, + renderer: { + // Sanitize link URLs with a scheme allow-list. Browsers strip C0 + // controls from schemes, so strip them before checking and emitting. + link(token) { + const href = sanitizeMarkdownUrl(token.href); + if (href === null) { + return this.parser.parseInline(token.tokens); + } + let out = ''; + return out; + }, + // Sanitize image src URLs with the same scheme allow-list. + image(token) { + const href = sanitizeMarkdownUrl(token.href); + if (href === null) { + return escapeHtml(token.text || ''); + } + let out = '' + escapeHtml(token.text || '') + '${highlighted}`; + }, + // Inline code: escape HTML + codespan(token) { + return `${escapeHtml(token.text)}`; + } + } + }); + + // Simple marked parse (escaping handled in renderers) + function safeMarkedParse(text) { + return marked.parse(text); + } + + // Search input + const searchInput = document.getElementById('tree-search'); + searchInput.addEventListener('input', (e) => { + searchQuery = e.target.value; + forceTreeRerender(); + }); + + // Filter buttons + document.querySelectorAll('.filter-btn').forEach(btn => { + btn.addEventListener('click', () => { + document.querySelectorAll('.filter-btn').forEach(b => b.classList.remove('active')); + btn.classList.add('active'); + filterMode = btn.dataset.filter; + forceTreeRerender(); + }); + }); + + // Sidebar toggle + const sidebar = document.getElementById('sidebar'); + const overlay = document.getElementById('sidebar-overlay'); + const hamburger = document.getElementById('hamburger'); + const sidebarResizer = document.getElementById('sidebar-resizer'); + const SIDEBAR_WIDTH_STORAGE_KEY = 'pi-share:v1:sidebar-width'; + const MIN_CONTENT_WIDTH = 320; + + function isMobileLayout() { + return window.matchMedia('(max-width: 900px)').matches; + } + + function getSidebarBounds() { + const rootStyles = getComputedStyle(document.documentElement); + const minWidth = parseFloat(rootStyles.getPropertyValue('--sidebar-min-width')) || 240; + const maxWidth = parseFloat(rootStyles.getPropertyValue('--sidebar-max-width')) || 720; + const viewportMaxWidth = window.innerWidth - MIN_CONTENT_WIDTH; + return { + minWidth, + maxWidth: Math.max(minWidth, Math.min(maxWidth, viewportMaxWidth)) + }; + } + + function clampSidebarWidth(width) { + const { minWidth, maxWidth } = getSidebarBounds(); + return Math.max(minWidth, Math.min(maxWidth, width)); + } + + function applySidebarWidth(width) { + document.documentElement.style.setProperty('--sidebar-width', `${Math.round(clampSidebarWidth(width))}px`); + } + + function loadSidebarWidth() { + try { + const raw = localStorage.getItem(SIDEBAR_WIDTH_STORAGE_KEY); + if (raw === null) return null; + const width = Number(raw); + return Number.isFinite(width) ? width : null; + } catch { + return null; + } + } + + function saveSidebarWidth(width) { + try { + localStorage.setItem(SIDEBAR_WIDTH_STORAGE_KEY, String(Math.round(clampSidebarWidth(width)))); + } catch { + // Ignore storage failures (e.g. private browsing restrictions) + } + } + + function setupSidebarResize() { + const savedWidth = loadSidebarWidth(); + if (savedWidth !== null) { + applySidebarWidth(savedWidth); + } + + if (!sidebarResizer) return; + + let cleanupDrag = null; + + const stopDrag = (pointerId) => { + if (cleanupDrag) { + cleanupDrag(pointerId); + cleanupDrag = null; + } + }; + + sidebarResizer.addEventListener('pointerdown', (e) => { + if (isMobileLayout()) return; + + e.preventDefault(); + const startX = e.clientX; + const startWidth = sidebar.getBoundingClientRect().width; + document.body.classList.add('sidebar-resizing'); + sidebarResizer.setPointerCapture?.(e.pointerId); + + const onPointerMove = (event) => { + applySidebarWidth(startWidth + (event.clientX - startX)); + }; + + cleanupDrag = (pointerIdToRelease) => { + document.body.classList.remove('sidebar-resizing'); + sidebarResizer.releasePointerCapture?.(pointerIdToRelease); + window.removeEventListener('pointermove', onPointerMove); + window.removeEventListener('pointerup', onPointerUp); + window.removeEventListener('pointercancel', onPointerCancel); + saveSidebarWidth(sidebar.getBoundingClientRect().width); + }; + + const onPointerUp = (event) => stopDrag(event.pointerId); + const onPointerCancel = (event) => stopDrag(event.pointerId); + + window.addEventListener('pointermove', onPointerMove); + window.addEventListener('pointerup', onPointerUp); + window.addEventListener('pointercancel', onPointerCancel); + }); + + sidebarResizer.addEventListener('dblclick', () => { + if (isMobileLayout()) return; + applySidebarWidth(400); + saveSidebarWidth(400); + }); + + window.addEventListener('resize', () => { + if (isMobileLayout()) return; + applySidebarWidth(sidebar.getBoundingClientRect().width); + }); + } + + setupSidebarResize(); + + hamburger.addEventListener('click', () => { + sidebar.classList.add('open'); + overlay.classList.add('open'); + hamburger.style.display = 'none'; + }); + + const closeSidebar = () => { + sidebar.classList.remove('open'); + overlay.classList.remove('open'); + hamburger.style.display = ''; + }; + + overlay.addEventListener('click', closeSidebar); + document.getElementById('sidebar-close').addEventListener('click', closeSidebar); + + // Toggle states + let thinkingExpanded = true; + let toolOutputsExpanded = false; + + const toggleThinking = () => { + thinkingExpanded = !thinkingExpanded; + document.querySelectorAll('.thinking-text').forEach(el => { + el.style.display = thinkingExpanded ? '' : 'none'; + }); + document.querySelectorAll('.thinking-collapsed').forEach(el => { + el.style.display = thinkingExpanded ? 'none' : 'block'; + }); + }; + + const toggleToolOutputs = () => { + toolOutputsExpanded = !toolOutputsExpanded; + document.querySelectorAll('.tool-output.expandable').forEach(el => { + el.classList.toggle('expanded', toolOutputsExpanded); + }); + document.querySelectorAll('.compaction').forEach(el => { + el.classList.toggle('expanded', toolOutputsExpanded); + }); + document.querySelectorAll('.skill-invocation').forEach(el => { + el.classList.toggle('expanded', toolOutputsExpanded); + }); + }; + + const attachHeaderHandlers = () => { + document.querySelector('[data-action="toggle-thinking"]')?.addEventListener('click', toggleThinking); + document.querySelector('[data-action="toggle-tools"]')?.addEventListener('click', toggleToolOutputs); + }; + + const isEditableTarget = (element) => { + if (!element) return false; + const tagName = element.tagName; + if (tagName === 'INPUT' || tagName === 'TEXTAREA' || tagName === 'SELECT' || tagName === 'BUTTON') { + return true; + } + return element.isContentEditable || Boolean(element.closest?.('[contenteditable="true"]')); + }; + + // Keyboard shortcuts + document.addEventListener('keydown', (e) => { + if (e.key === 'Escape') { + searchInput.value = ''; + searchQuery = ''; + navigateTo(leafId, 'bottom'); + } + + if (isEditableTarget(document.activeElement)) { + return; + } + + const key = e.key.toLowerCase(); + if (key === 't') { + e.preventDefault(); + toggleThinking(); + } else if (key === 'o') { + e.preventDefault(); + toggleToolOutputs(); + } + }); + + // Initial render + // If URL has targetId, scroll to that specific message; otherwise stay at top + if (leafId) { + if (urlTargetId && byId.has(urlTargetId)) { + // Deep link: navigate to leaf and scroll to target message + navigateTo(leafId, 'target', urlTargetId); + } else { + navigateTo(leafId, 'none'); + } + } else if (entries.length > 0) { + // Fallback: use last entry if no leafId + navigateTo(entries[entries.length - 1].id, 'none'); + } + })(); diff --git a/packages/coding-agent/src/core/export-html/tool-renderer.ts b/packages/coding-agent/src/core/export-html/tool-renderer.ts new file mode 100644 index 0000000000000000000000000000000000000000..c2030a27149176a38747c193d422e341ee48e4cc --- /dev/null +++ b/packages/coding-agent/src/core/export-html/tool-renderer.ts @@ -0,0 +1,172 @@ +/** + * Tool HTML renderer for custom tools in HTML export. + * + * Renders custom tool calls and results to HTML by invoking their TUI renderers + * and converting the ANSI output to HTML. + */ + +import type { ImageContent, TextContent } from "@earendil-works/pi-ai"; +import type { Component } from "@earendil-works/pi-tui"; +import type { Theme } from "../../modes/interactive/theme/theme.ts"; +import type { ToolDefinition, ToolRenderContext } from "../extensions/types.ts"; +import { ansiLinesToHtml } from "./ansi-to-html.ts"; + +export interface ToolHtmlRendererDeps { + /** Function to look up tool definition by name */ + getToolDefinition: (name: string) => ToolDefinition | undefined; + /** Theme for styling */ + theme: Theme; + /** Working directory for render context */ + cwd: string; + /** Terminal width for rendering (default: 100) */ + width?: number; +} + +export interface ToolHtmlRenderer { + /** Render a tool call to HTML. Returns undefined if tool has no custom renderer. */ + renderCall(toolCallId: string, toolName: string, args: unknown): string | undefined; + /** Render a tool result to collapsed/expanded HTML. Returns undefined if tool has no custom renderer. */ + renderResult( + toolCallId: string, + toolName: string, + result: Array<{ type: string; text?: string; data?: string; mimeType?: string }>, + details: unknown, + isError: boolean, + ): { collapsed?: string; expanded?: string } | undefined; +} + +/** + * Create a tool HTML renderer. + * + * The renderer looks up tool definitions and invokes their renderCall/renderResult + * methods, converting the resulting TUI Component output (ANSI) to HTML. + */ +const ANSI_ESCAPE_REGEX = /\x1b\[[\d;]*m/g; + +function isBlankRenderedLine(line: string): boolean { + return line.replace(ANSI_ESCAPE_REGEX, "").trim().length === 0; +} + +function trimRenderedResultLines(lines: string[]): string[] { + let start = 0; + let end = lines.length; + while (start < end && isBlankRenderedLine(lines[start])) start++; + while (end > start && isBlankRenderedLine(lines[end - 1])) end--; + return lines.slice(start, end); +} + +export function createToolHtmlRenderer(deps: ToolHtmlRendererDeps): ToolHtmlRenderer { + const { getToolDefinition, theme, cwd, width = 100 } = deps; + + const renderedCallComponents = new Map(); + const renderedResultComponents = new Map(); + const renderedStates = new Map(); + const renderedArgs = new Map(); + + const getState = (toolCallId: string): any => { + let state = renderedStates.get(toolCallId); + if (!state) { + state = {}; + renderedStates.set(toolCallId, state); + } + return state; + }; + + const createRenderContext = ( + toolCallId: string, + lastComponent: Component | undefined, + expanded: boolean, + isPartial: boolean, + isError: boolean, + ): ToolRenderContext => { + return { + args: renderedArgs.get(toolCallId), + toolCallId, + invalidate: () => {}, + lastComponent, + state: getState(toolCallId), + cwd, + executionStarted: true, + argsComplete: true, + isPartial, + expanded, + showImages: false, + isError, + }; + }; + + return { + renderCall(toolCallId: string, toolName: string, args: unknown): string | undefined { + try { + renderedArgs.set(toolCallId, args); + const toolDef = getToolDefinition(toolName); + if (!toolDef?.renderCall) { + return undefined; + } + + const component = toolDef.renderCall( + args, + theme, + createRenderContext(toolCallId, renderedCallComponents.get(toolCallId), false, true, false), + ); + renderedCallComponents.set(toolCallId, component); + const lines = component.render(width); + return ansiLinesToHtml(lines); + } catch { + // On error, return undefined so HTML export can fall back to structured result rendering + return undefined; + } + }, + + renderResult( + toolCallId: string, + toolName: string, + result: Array<{ type: string; text?: string; data?: string; mimeType?: string }>, + details: unknown, + isError: boolean, + ): { collapsed?: string; expanded?: string } | undefined { + try { + const toolDef = getToolDefinition(toolName); + if (!toolDef?.renderResult) { + return undefined; + } + + // Build AgentToolResult from content array + // Cast content since session storage uses generic object types + const agentToolResult = { + content: result as (TextContent | ImageContent)[], + details, + isError, + }; + + // Render collapsed + const collapsedComponent = toolDef.renderResult( + agentToolResult, + { expanded: false, isPartial: false }, + theme, + createRenderContext(toolCallId, renderedResultComponents.get(toolCallId), false, false, isError), + ); + renderedResultComponents.set(toolCallId, collapsedComponent); + const collapsed = ansiLinesToHtml(trimRenderedResultLines(collapsedComponent.render(width))); + + // Render expanded + const expandedComponent = toolDef.renderResult( + agentToolResult, + { expanded: true, isPartial: false }, + theme, + createRenderContext(toolCallId, renderedResultComponents.get(toolCallId), true, false, isError), + ); + renderedResultComponents.set(toolCallId, expandedComponent); + const expanded = ansiLinesToHtml(trimRenderedResultLines(expandedComponent.render(width))); + + return { + ...(collapsed && collapsed !== expanded ? { collapsed } : {}), + expanded, + }; + } catch { + // On error, return undefined so HTML export can fall back to structured result rendering + return undefined; + } + }, + }; +} diff --git a/packages/coding-agent/src/core/export-html/vendor/highlight.min.js b/packages/coding-agent/src/core/export-html/vendor/highlight.min.js new file mode 100644 index 0000000000000000000000000000000000000000..5d699ae6a4cfc9544a22a9ac394b01db2e5abd4f --- /dev/null +++ b/packages/coding-agent/src/core/export-html/vendor/highlight.min.js @@ -0,0 +1,1213 @@ +/*! + Highlight.js v11.9.0 (git: f47103d4f1) + (c) 2006-2023 undefined and other contributors + License: BSD-3-Clause + */ +var hljs=function(){"use strict";function e(n){ +return n instanceof Map?n.clear=n.delete=n.set=()=>{ +throw Error("map is read-only")}:n instanceof Set&&(n.add=n.clear=n.delete=()=>{ +throw Error("set is read-only") +}),Object.freeze(n),Object.getOwnPropertyNames(n).forEach((t=>{ +const a=n[t],i=typeof a;"object"!==i&&"function"!==i||Object.isFrozen(a)||e(a) +})),n}class n{constructor(e){ +void 0===e.data&&(e.data={}),this.data=e.data,this.isMatchIgnored=!1} +ignoreMatch(){this.isMatchIgnored=!0}}function t(e){ +return e.replace(/&/g,"&").replace(//g,">").replace(/"/g,""").replace(/'/g,"'") +}function a(e,...n){const t=Object.create(null);for(const n in e)t[n]=e[n] +;return n.forEach((e=>{for(const n in e)t[n]=e[n]})),t}const i=e=>!!e.scope +;class r{constructor(e,n){ +this.buffer="",this.classPrefix=n.classPrefix,e.walk(this)}addText(e){ +this.buffer+=t(e)}openNode(e){if(!i(e))return;const n=((e,{prefix:n})=>{ +if(e.startsWith("language:"))return e.replace("language:","language-") +;if(e.includes(".")){const t=e.split(".") +;return[`${n}${t.shift()}`,...t.map(((e,n)=>`${e}${"_".repeat(n+1)}`))].join(" ") +}return`${n}${e}`})(e.scope,{prefix:this.classPrefix});this.span(n)} +closeNode(e){i(e)&&(this.buffer+="")}value(){return this.buffer}span(e){ +this.buffer+=``}}const s=(e={})=>{const n={children:[]} +;return Object.assign(n,e),n};class o{constructor(){ +this.rootNode=s(),this.stack=[this.rootNode]}get top(){ +return this.stack[this.stack.length-1]}get root(){return this.rootNode}add(e){ +this.top.children.push(e)}openNode(e){const n=s({scope:e}) +;this.add(n),this.stack.push(n)}closeNode(){ +if(this.stack.length>1)return this.stack.pop()}closeAllNodes(){ +for(;this.closeNode(););}toJSON(){return JSON.stringify(this.rootNode,null,4)} +walk(e){return this.constructor._walk(e,this.rootNode)}static _walk(e,n){ +return"string"==typeof n?e.addText(n):n.children&&(e.openNode(n), +n.children.forEach((n=>this._walk(e,n))),e.closeNode(n)),e}static _collapse(e){ +"string"!=typeof e&&e.children&&(e.children.every((e=>"string"==typeof e))?e.children=[e.children.join("")]:e.children.forEach((e=>{ +o._collapse(e)})))}}class l extends o{constructor(e){super(),this.options=e} +addText(e){""!==e&&this.add(e)}startScope(e){this.openNode(e)}endScope(){ +this.closeNode()}__addSublanguage(e,n){const t=e.root +;n&&(t.scope="language:"+n),this.add(t)}toHTML(){ +return new r(this,this.options).value()}finalize(){ +return this.closeAllNodes(),!0}}function c(e){ +return e?"string"==typeof e?e:e.source:null}function d(e){return b("(?=",e,")")} +function g(e){return b("(?:",e,")*")}function u(e){return b("(?:",e,")?")} +function b(...e){return e.map((e=>c(e))).join("")}function m(...e){const n=(e=>{ +const n=e[e.length-1] +;return"object"==typeof n&&n.constructor===Object?(e.splice(e.length-1,1),n):{} +})(e);return"("+(n.capture?"":"?:")+e.map((e=>c(e))).join("|")+")"} +function p(e){return RegExp(e.toString()+"|").exec("").length-1} +const _=/\[(?:[^\\\]]|\\.)*\]|\(\??|\\([1-9][0-9]*)|\\./ +;function h(e,{joinWith:n}){let t=0;return e.map((e=>{t+=1;const n=t +;let a=c(e),i="";for(;a.length>0;){const e=_.exec(a);if(!e){i+=a;break} +i+=a.substring(0,e.index), +a=a.substring(e.index+e[0].length),"\\"===e[0][0]&&e[1]?i+="\\"+(Number(e[1])+n):(i+=e[0], +"("===e[0]&&t++)}return i})).map((e=>`(${e})`)).join(n)} +const f="[a-zA-Z]\\w*",E="[a-zA-Z_]\\w*",y="\\b\\d+(\\.\\d+)?",N="(-?)(\\b0[xX][a-fA-F0-9]+|(\\b\\d+(\\.\\d*)?|\\.\\d+)([eE][-+]?\\d+)?)",w="\\b(0b[01]+)",v={ +begin:"\\\\[\\s\\S]",relevance:0},O={scope:"string",begin:"'",end:"'", +illegal:"\\n",contains:[v]},k={scope:"string",begin:'"',end:'"',illegal:"\\n", +contains:[v]},x=(e,n,t={})=>{const i=a({scope:"comment",begin:e,end:n, +contains:[]},t);i.contains.push({scope:"doctag", +begin:"[ ]*(?=(TODO|FIXME|NOTE|BUG|OPTIMIZE|HACK|XXX):)", +end:/(TODO|FIXME|NOTE|BUG|OPTIMIZE|HACK|XXX):/,excludeBegin:!0,relevance:0}) +;const r=m("I","a","is","so","us","to","at","if","in","it","on",/[A-Za-z]+['](d|ve|re|ll|t|s|n)/,/[A-Za-z]+[-][a-z]+/,/[A-Za-z][a-z]{2,}/) +;return i.contains.push({begin:b(/[ ]+/,"(",r,/[.]?[:]?([.][ ]|[ ])/,"){3}")}),i +},M=x("//","$"),S=x("/\\*","\\*/"),A=x("#","$");var C=Object.freeze({ +__proto__:null,APOS_STRING_MODE:O,BACKSLASH_ESCAPE:v,BINARY_NUMBER_MODE:{ +scope:"number",begin:w,relevance:0},BINARY_NUMBER_RE:w,COMMENT:x, +C_BLOCK_COMMENT_MODE:S,C_LINE_COMMENT_MODE:M,C_NUMBER_MODE:{scope:"number", +begin:N,relevance:0},C_NUMBER_RE:N,END_SAME_AS_BEGIN:e=>Object.assign(e,{ +"on:begin":(e,n)=>{n.data._beginMatch=e[1]},"on:end":(e,n)=>{ +n.data._beginMatch!==e[1]&&n.ignoreMatch()}}),HASH_COMMENT_MODE:A,IDENT_RE:f, +MATCH_NOTHING_RE:/\b\B/,METHOD_GUARD:{begin:"\\.\\s*"+E,relevance:0}, +NUMBER_MODE:{scope:"number",begin:y,relevance:0},NUMBER_RE:y, +PHRASAL_WORDS_MODE:{ +begin:/\b(a|an|the|are|I'm|isn't|don't|doesn't|won't|but|just|should|pretty|simply|enough|gonna|going|wtf|so|such|will|you|your|they|like|more)\b/ +},QUOTE_STRING_MODE:k,REGEXP_MODE:{scope:"regexp",begin:/\/(?=[^/\n]*\/)/, +end:/\/[gimuy]*/,contains:[v,{begin:/\[/,end:/\]/,relevance:0,contains:[v]}]}, +RE_STARTERS_RE:"!|!=|!==|%|%=|&|&&|&=|\\*|\\*=|\\+|\\+=|,|-|-=|/=|/|:|;|<<|<<=|<=|<|===|==|=|>>>=|>>=|>=|>>>|>>|>|\\?|\\[|\\{|\\(|\\^|\\^=|\\||\\|=|\\|\\||~", +SHEBANG:(e={})=>{const n=/^#![ ]*\// +;return e.binary&&(e.begin=b(n,/.*\b/,e.binary,/\b.*/)),a({scope:"meta",begin:n, +end:/$/,relevance:0,"on:begin":(e,n)=>{0!==e.index&&n.ignoreMatch()}},e)}, +TITLE_MODE:{scope:"title",begin:f,relevance:0},UNDERSCORE_IDENT_RE:E, +UNDERSCORE_TITLE_MODE:{scope:"title",begin:E,relevance:0}});function T(e,n){ +"."===e.input[e.index-1]&&n.ignoreMatch()}function R(e,n){ +void 0!==e.className&&(e.scope=e.className,delete e.className)}function D(e,n){ +n&&e.beginKeywords&&(e.begin="\\b("+e.beginKeywords.split(" ").join("|")+")(?!\\.)(?=\\b|\\s)", +e.__beforeBegin=T,e.keywords=e.keywords||e.beginKeywords,delete e.beginKeywords, +void 0===e.relevance&&(e.relevance=0))}function I(e,n){ +Array.isArray(e.illegal)&&(e.illegal=m(...e.illegal))}function L(e,n){ +if(e.match){ +if(e.begin||e.end)throw Error("begin & end are not supported with match") +;e.begin=e.match,delete e.match}}function B(e,n){ +void 0===e.relevance&&(e.relevance=1)}const $=(e,n)=>{if(!e.beforeMatch)return +;if(e.starts)throw Error("beforeMatch cannot be used with starts") +;const t=Object.assign({},e);Object.keys(e).forEach((n=>{delete e[n] +})),e.keywords=t.keywords,e.begin=b(t.beforeMatch,d(t.begin)),e.starts={ +relevance:0,contains:[Object.assign(t,{endsParent:!0})] +},e.relevance=0,delete t.beforeMatch +},z=["of","and","for","in","not","or","if","then","parent","list","value"],F="keyword" +;function U(e,n,t=F){const a=Object.create(null) +;return"string"==typeof e?i(t,e.split(" ")):Array.isArray(e)?i(t,e):Object.keys(e).forEach((t=>{ +Object.assign(a,U(e[t],n,t))})),a;function i(e,t){ +n&&(t=t.map((e=>e.toLowerCase()))),t.forEach((n=>{const t=n.split("|") +;a[t[0]]=[e,j(t[0],t[1])]}))}}function j(e,n){ +return n?Number(n):(e=>z.includes(e.toLowerCase()))(e)?0:1}const P={},K=e=>{ +console.error(e)},H=(e,...n)=>{console.log("WARN: "+e,...n)},q=(e,n)=>{ +P[`${e}/${n}`]||(console.log(`Deprecated as of ${e}. ${n}`),P[`${e}/${n}`]=!0) +},G=Error();function Z(e,n,{key:t}){let a=0;const i=e[t],r={},s={} +;for(let e=1;e<=n.length;e++)s[e+a]=i[e],r[e+a]=!0,a+=p(n[e-1]) +;e[t]=s,e[t]._emit=r,e[t]._multi=!0}function W(e){(e=>{ +e.scope&&"object"==typeof e.scope&&null!==e.scope&&(e.beginScope=e.scope, +delete e.scope)})(e),"string"==typeof e.beginScope&&(e.beginScope={ +_wrap:e.beginScope}),"string"==typeof e.endScope&&(e.endScope={_wrap:e.endScope +}),(e=>{if(Array.isArray(e.begin)){ +if(e.skip||e.excludeBegin||e.returnBegin)throw K("skip, excludeBegin, returnBegin not compatible with beginScope: {}"), +G +;if("object"!=typeof e.beginScope||null===e.beginScope)throw K("beginScope must be object"), +G;Z(e,e.begin,{key:"beginScope"}),e.begin=h(e.begin,{joinWith:""})}})(e),(e=>{ +if(Array.isArray(e.end)){ +if(e.skip||e.excludeEnd||e.returnEnd)throw K("skip, excludeEnd, returnEnd not compatible with endScope: {}"), +G +;if("object"!=typeof e.endScope||null===e.endScope)throw K("endScope must be object"), +G;Z(e,e.end,{key:"endScope"}),e.end=h(e.end,{joinWith:""})}})(e)}function Q(e){ +function n(n,t){ +return RegExp(c(n),"m"+(e.case_insensitive?"i":"")+(e.unicodeRegex?"u":"")+(t?"g":"")) +}class t{constructor(){ +this.matchIndexes={},this.regexes=[],this.matchAt=1,this.position=0} +addRule(e,n){ +n.position=this.position++,this.matchIndexes[this.matchAt]=n,this.regexes.push([n,e]), +this.matchAt+=p(e)+1}compile(){0===this.regexes.length&&(this.exec=()=>null) +;const e=this.regexes.map((e=>e[1]));this.matcherRe=n(h(e,{joinWith:"|" +}),!0),this.lastIndex=0}exec(e){this.matcherRe.lastIndex=this.lastIndex +;const n=this.matcherRe.exec(e);if(!n)return null +;const t=n.findIndex(((e,n)=>n>0&&void 0!==e)),a=this.matchIndexes[t] +;return n.splice(0,t),Object.assign(n,a)}}class i{constructor(){ +this.rules=[],this.multiRegexes=[], +this.count=0,this.lastIndex=0,this.regexIndex=0}getMatcher(e){ +if(this.multiRegexes[e])return this.multiRegexes[e];const n=new t +;return this.rules.slice(e).forEach((([e,t])=>n.addRule(e,t))), +n.compile(),this.multiRegexes[e]=n,n}resumingScanAtSamePosition(){ +return 0!==this.regexIndex}considerAll(){this.regexIndex=0}addRule(e,n){ +this.rules.push([e,n]),"begin"===n.type&&this.count++}exec(e){ +const n=this.getMatcher(this.regexIndex);n.lastIndex=this.lastIndex +;let t=n.exec(e) +;if(this.resumingScanAtSamePosition())if(t&&t.index===this.lastIndex);else{ +const n=this.getMatcher(0);n.lastIndex=this.lastIndex+1,t=n.exec(e)} +return t&&(this.regexIndex+=t.position+1, +this.regexIndex===this.count&&this.considerAll()),t}} +if(e.compilerExtensions||(e.compilerExtensions=[]), +e.contains&&e.contains.includes("self"))throw Error("ERR: contains `self` is not supported at the top-level of a language. See documentation.") +;return e.classNameAliases=a(e.classNameAliases||{}),function t(r,s){const o=r +;if(r.isCompiled)return o +;[R,L,W,$].forEach((e=>e(r,s))),e.compilerExtensions.forEach((e=>e(r,s))), +r.__beforeBegin=null,[D,I,B].forEach((e=>e(r,s))),r.isCompiled=!0;let l=null +;return"object"==typeof r.keywords&&r.keywords.$pattern&&(r.keywords=Object.assign({},r.keywords), +l=r.keywords.$pattern, +delete r.keywords.$pattern),l=l||/\w+/,r.keywords&&(r.keywords=U(r.keywords,e.case_insensitive)), +o.keywordPatternRe=n(l,!0), +s&&(r.begin||(r.begin=/\B|\b/),o.beginRe=n(o.begin),r.end||r.endsWithParent||(r.end=/\B|\b/), +r.end&&(o.endRe=n(o.end)), +o.terminatorEnd=c(o.end)||"",r.endsWithParent&&s.terminatorEnd&&(o.terminatorEnd+=(r.end?"|":"")+s.terminatorEnd)), +r.illegal&&(o.illegalRe=n(r.illegal)), +r.contains||(r.contains=[]),r.contains=[].concat(...r.contains.map((e=>(e=>(e.variants&&!e.cachedVariants&&(e.cachedVariants=e.variants.map((n=>a(e,{ +variants:null},n)))),e.cachedVariants?e.cachedVariants:X(e)?a(e,{ +starts:e.starts?a(e.starts):null +}):Object.isFrozen(e)?a(e):e))("self"===e?r:e)))),r.contains.forEach((e=>{t(e,o) +})),r.starts&&t(r.starts,s),o.matcher=(e=>{const n=new i +;return e.contains.forEach((e=>n.addRule(e.begin,{rule:e,type:"begin" +}))),e.terminatorEnd&&n.addRule(e.terminatorEnd,{type:"end" +}),e.illegal&&n.addRule(e.illegal,{type:"illegal"}),n})(o),o}(e)}function X(e){ +return!!e&&(e.endsWithParent||X(e.starts))}class V extends Error{ +constructor(e,n){super(e),this.name="HTMLInjectionError",this.html=n}} +const J=t,Y=a,ee=Symbol("nomatch"),ne=t=>{ +const a=Object.create(null),i=Object.create(null),r=[];let s=!0 +;const o="Could not find the language '{}', did you forget to load/include a language module?",c={ +disableAutodetect:!0,name:"Plain text",contains:[]};let p={ +ignoreUnescapedHTML:!1,throwUnescapedHTML:!1,noHighlightRe:/^(no-?highlight)$/i, +languageDetectRe:/\blang(?:uage)?-([\w-]+)\b/i,classPrefix:"hljs-", +cssSelector:"pre code",languages:null,__emitter:l};function _(e){ +return p.noHighlightRe.test(e)}function h(e,n,t){let a="",i="" +;"object"==typeof n?(a=e, +t=n.ignoreIllegals,i=n.language):(q("10.7.0","highlight(lang, code, ...args) has been deprecated."), +q("10.7.0","Please use highlight(code, options) instead.\nhttps://github.com/highlightjs/highlight.js/issues/2277"), +i=e,a=n),void 0===t&&(t=!0);const r={code:a,language:i};x("before:highlight",r) +;const s=r.result?r.result:f(r.language,r.code,t) +;return s.code=r.code,x("after:highlight",s),s}function f(e,t,i,r){ +const l=Object.create(null);function c(){if(!x.keywords)return void S.addText(A) +;let e=0;x.keywordPatternRe.lastIndex=0;let n=x.keywordPatternRe.exec(A),t="" +;for(;n;){t+=A.substring(e,n.index) +;const i=w.case_insensitive?n[0].toLowerCase():n[0],r=(a=i,x.keywords[a]);if(r){ +const[e,a]=r +;if(S.addText(t),t="",l[i]=(l[i]||0)+1,l[i]<=7&&(C+=a),e.startsWith("_"))t+=n[0];else{ +const t=w.classNameAliases[e]||e;g(n[0],t)}}else t+=n[0] +;e=x.keywordPatternRe.lastIndex,n=x.keywordPatternRe.exec(A)}var a +;t+=A.substring(e),S.addText(t)}function d(){null!=x.subLanguage?(()=>{ +if(""===A)return;let e=null;if("string"==typeof x.subLanguage){ +if(!a[x.subLanguage])return void S.addText(A) +;e=f(x.subLanguage,A,!0,M[x.subLanguage]),M[x.subLanguage]=e._top +}else e=E(A,x.subLanguage.length?x.subLanguage:null) +;x.relevance>0&&(C+=e.relevance),S.__addSublanguage(e._emitter,e.language) +})():c(),A=""}function g(e,n){ +""!==e&&(S.startScope(n),S.addText(e),S.endScope())}function u(e,n){let t=1 +;const a=n.length-1;for(;t<=a;){if(!e._emit[t]){t++;continue} +const a=w.classNameAliases[e[t]]||e[t],i=n[t];a?g(i,a):(A=i,c(),A=""),t++}} +function b(e,n){ +return e.scope&&"string"==typeof e.scope&&S.openNode(w.classNameAliases[e.scope]||e.scope), +e.beginScope&&(e.beginScope._wrap?(g(A,w.classNameAliases[e.beginScope._wrap]||e.beginScope._wrap), +A=""):e.beginScope._multi&&(u(e.beginScope,n),A="")),x=Object.create(e,{parent:{ +value:x}}),x}function m(e,t,a){let i=((e,n)=>{const t=e&&e.exec(n) +;return t&&0===t.index})(e.endRe,a);if(i){if(e["on:end"]){const a=new n(e) +;e["on:end"](t,a),a.isMatchIgnored&&(i=!1)}if(i){ +for(;e.endsParent&&e.parent;)e=e.parent;return e}} +if(e.endsWithParent)return m(e.parent,t,a)}function _(e){ +return 0===x.matcher.regexIndex?(A+=e[0],1):(D=!0,0)}function h(e){ +const n=e[0],a=t.substring(e.index),i=m(x,e,a);if(!i)return ee;const r=x +;x.endScope&&x.endScope._wrap?(d(), +g(n,x.endScope._wrap)):x.endScope&&x.endScope._multi?(d(), +u(x.endScope,e)):r.skip?A+=n:(r.returnEnd||r.excludeEnd||(A+=n), +d(),r.excludeEnd&&(A=n));do{ +x.scope&&S.closeNode(),x.skip||x.subLanguage||(C+=x.relevance),x=x.parent +}while(x!==i.parent);return i.starts&&b(i.starts,e),r.returnEnd?0:n.length} +let y={};function N(a,r){const o=r&&r[0];if(A+=a,null==o)return d(),0 +;if("begin"===y.type&&"end"===r.type&&y.index===r.index&&""===o){ +if(A+=t.slice(r.index,r.index+1),!s){const n=Error(`0 width match regex (${e})`) +;throw n.languageName=e,n.badRule=y.rule,n}return 1} +if(y=r,"begin"===r.type)return(e=>{ +const t=e[0],a=e.rule,i=new n(a),r=[a.__beforeBegin,a["on:begin"]] +;for(const n of r)if(n&&(n(e,i),i.isMatchIgnored))return _(t) +;return a.skip?A+=t:(a.excludeBegin&&(A+=t), +d(),a.returnBegin||a.excludeBegin||(A=t)),b(a,e),a.returnBegin?0:t.length})(r) +;if("illegal"===r.type&&!i){ +const e=Error('Illegal lexeme "'+o+'" for mode "'+(x.scope||"")+'"') +;throw e.mode=x,e}if("end"===r.type){const e=h(r);if(e!==ee)return e} +if("illegal"===r.type&&""===o)return 1 +;if(R>1e5&&R>3*r.index)throw Error("potential infinite loop, way more iterations than matches") +;return A+=o,o.length}const w=v(e) +;if(!w)throw K(o.replace("{}",e)),Error('Unknown language: "'+e+'"') +;const O=Q(w);let k="",x=r||O;const M={},S=new p.__emitter(p);(()=>{const e=[] +;for(let n=x;n!==w;n=n.parent)n.scope&&e.unshift(n.scope) +;e.forEach((e=>S.openNode(e)))})();let A="",C=0,T=0,R=0,D=!1;try{ +if(w.__emitTokens)w.__emitTokens(t,S);else{for(x.matcher.considerAll();;){ +R++,D?D=!1:x.matcher.considerAll(),x.matcher.lastIndex=T +;const e=x.matcher.exec(t);if(!e)break;const n=N(t.substring(T,e.index),e) +;T=e.index+n}N(t.substring(T))}return S.finalize(),k=S.toHTML(),{language:e, +value:k,relevance:C,illegal:!1,_emitter:S,_top:x}}catch(n){ +if(n.message&&n.message.includes("Illegal"))return{language:e,value:J(t), +illegal:!0,relevance:0,_illegalBy:{message:n.message,index:T, +context:t.slice(T-100,T+100),mode:n.mode,resultSoFar:k},_emitter:S};if(s)return{ +language:e,value:J(t),illegal:!1,relevance:0,errorRaised:n,_emitter:S,_top:x} +;throw n}}function E(e,n){n=n||p.languages||Object.keys(a);const t=(e=>{ +const n={value:J(e),illegal:!1,relevance:0,_top:c,_emitter:new p.__emitter(p)} +;return n._emitter.addText(e),n})(e),i=n.filter(v).filter(k).map((n=>f(n,e,!1))) +;i.unshift(t);const r=i.sort(((e,n)=>{ +if(e.relevance!==n.relevance)return n.relevance-e.relevance +;if(e.language&&n.language){if(v(e.language).supersetOf===n.language)return 1 +;if(v(n.language).supersetOf===e.language)return-1}return 0})),[s,o]=r,l=s +;return l.secondBest=o,l}function y(e){let n=null;const t=(e=>{ +let n=e.className+" ";n+=e.parentNode?e.parentNode.className:"" +;const t=p.languageDetectRe.exec(n);if(t){const n=v(t[1]) +;return n||(H(o.replace("{}",t[1])), +H("Falling back to no-highlight mode for this block.",e)),n?t[1]:"no-highlight"} +return n.split(/\s+/).find((e=>_(e)||v(e)))})(e);if(_(t))return +;if(x("before:highlightElement",{el:e,language:t +}),e.dataset.highlighted)return void console.log("Element previously highlighted. To highlight again, first unset `dataset.highlighted`.",e) +;if(e.children.length>0&&(p.ignoreUnescapedHTML||(console.warn("One of your code blocks includes unescaped HTML. This is a potentially serious security risk."), +console.warn("https://github.com/highlightjs/highlight.js/wiki/security"), +console.warn("The element with unescaped HTML:"), +console.warn(e)),p.throwUnescapedHTML))throw new V("One of your code blocks includes unescaped HTML.",e.innerHTML) +;n=e;const a=n.textContent,r=t?h(a,{language:t,ignoreIllegals:!0}):E(a) +;e.innerHTML=r.value,e.dataset.highlighted="yes",((e,n,t)=>{const a=n&&i[n]||t +;e.classList.add("hljs"),e.classList.add("language-"+a) +})(e,t,r.language),e.result={language:r.language,re:r.relevance, +relevance:r.relevance},r.secondBest&&(e.secondBest={ +language:r.secondBest.language,relevance:r.secondBest.relevance +}),x("after:highlightElement",{el:e,result:r,text:a})}let N=!1;function w(){ +"loading"!==document.readyState?document.querySelectorAll(p.cssSelector).forEach(y):N=!0 +}function v(e){return e=(e||"").toLowerCase(),a[e]||a[i[e]]} +function O(e,{languageName:n}){"string"==typeof e&&(e=[e]),e.forEach((e=>{ +i[e.toLowerCase()]=n}))}function k(e){const n=v(e) +;return n&&!n.disableAutodetect}function x(e,n){const t=e;r.forEach((e=>{ +e[t]&&e[t](n)}))} +"undefined"!=typeof window&&window.addEventListener&&window.addEventListener("DOMContentLoaded",(()=>{ +N&&w()}),!1),Object.assign(t,{highlight:h,highlightAuto:E,highlightAll:w, +highlightElement:y, +highlightBlock:e=>(q("10.7.0","highlightBlock will be removed entirely in v12.0"), +q("10.7.0","Please use highlightElement now."),y(e)),configure:e=>{p=Y(p,e)}, +initHighlighting:()=>{ +w(),q("10.6.0","initHighlighting() deprecated. Use highlightAll() now.")}, +initHighlightingOnLoad:()=>{ +w(),q("10.6.0","initHighlightingOnLoad() deprecated. Use highlightAll() now.") +},registerLanguage:(e,n)=>{let i=null;try{i=n(t)}catch(n){ +if(K("Language definition for '{}' could not be registered.".replace("{}",e)), +!s)throw n;K(n),i=c} +i.name||(i.name=e),a[e]=i,i.rawDefinition=n.bind(null,t),i.aliases&&O(i.aliases,{ +languageName:e})},unregisterLanguage:e=>{delete a[e] +;for(const n of Object.keys(i))i[n]===e&&delete i[n]}, +listLanguages:()=>Object.keys(a),getLanguage:v,registerAliases:O, +autoDetection:k,inherit:Y,addPlugin:e=>{(e=>{ +e["before:highlightBlock"]&&!e["before:highlightElement"]&&(e["before:highlightElement"]=n=>{ +e["before:highlightBlock"](Object.assign({block:n.el},n)) +}),e["after:highlightBlock"]&&!e["after:highlightElement"]&&(e["after:highlightElement"]=n=>{ +e["after:highlightBlock"](Object.assign({block:n.el},n))})})(e),r.push(e)}, +removePlugin:e=>{const n=r.indexOf(e);-1!==n&&r.splice(n,1)}}),t.debugMode=()=>{ +s=!1},t.safeMode=()=>{s=!0},t.versionString="11.9.0",t.regex={concat:b, +lookahead:d,either:m,optional:u,anyNumberOfTimes:g} +;for(const n in C)"object"==typeof C[n]&&e(C[n]);return Object.assign(t,C),t +},te=ne({});te.newInstance=()=>ne({});var ae=te;const ie=e=>({IMPORTANT:{ +scope:"meta",begin:"!important"},BLOCK_COMMENT:e.C_BLOCK_COMMENT_MODE,HEXCOLOR:{ +scope:"number",begin:/#(([0-9a-fA-F]{3,4})|(([0-9a-fA-F]{2}){3,4}))\b/}, +FUNCTION_DISPATCH:{className:"built_in",begin:/[\w-]+(?=\()/}, +ATTRIBUTE_SELECTOR_MODE:{scope:"selector-attr",begin:/\[/,end:/\]/,illegal:"$", +contains:[e.APOS_STRING_MODE,e.QUOTE_STRING_MODE]},CSS_NUMBER_MODE:{ +scope:"number", +begin:e.NUMBER_RE+"(%|em|ex|ch|rem|vw|vh|vmin|vmax|cm|mm|in|pt|pc|px|deg|grad|rad|turn|s|ms|Hz|kHz|dpi|dpcm|dppx)?", +relevance:0},CSS_VARIABLE:{className:"attr",begin:/--[A-Za-z_][A-Za-z0-9_-]*/} +}),re=["a","abbr","address","article","aside","audio","b","blockquote","body","button","canvas","caption","cite","code","dd","del","details","dfn","div","dl","dt","em","fieldset","figcaption","figure","footer","form","h1","h2","h3","h4","h5","h6","header","hgroup","html","i","iframe","img","input","ins","kbd","label","legend","li","main","mark","menu","nav","object","ol","p","q","quote","samp","section","span","strong","summary","sup","table","tbody","td","textarea","tfoot","th","thead","time","tr","ul","var","video"],se=["any-hover","any-pointer","aspect-ratio","color","color-gamut","color-index","device-aspect-ratio","device-height","device-width","display-mode","forced-colors","grid","height","hover","inverted-colors","monochrome","orientation","overflow-block","overflow-inline","pointer","prefers-color-scheme","prefers-contrast","prefers-reduced-motion","prefers-reduced-transparency","resolution","scan","scripting","update","width","min-width","max-width","min-height","max-height"],oe=["active","any-link","blank","checked","current","default","defined","dir","disabled","drop","empty","enabled","first","first-child","first-of-type","fullscreen","future","focus","focus-visible","focus-within","has","host","host-context","hover","indeterminate","in-range","invalid","is","lang","last-child","last-of-type","left","link","local-link","not","nth-child","nth-col","nth-last-child","nth-last-col","nth-last-of-type","nth-of-type","only-child","only-of-type","optional","out-of-range","past","placeholder-shown","read-only","read-write","required","right","root","scope","target","target-within","user-invalid","valid","visited","where"],le=["after","backdrop","before","cue","cue-region","first-letter","first-line","grammar-error","marker","part","placeholder","selection","slotted","spelling-error"],ce=["align-content","align-items","align-self","all","animation","animation-delay","animation-direction","animation-duration","animation-fill-mode","animation-iteration-count","animation-name","animation-play-state","animation-timing-function","backface-visibility","background","background-attachment","background-blend-mode","background-clip","background-color","background-image","background-origin","background-position","background-repeat","background-size","block-size","border","border-block","border-block-color","border-block-end","border-block-end-color","border-block-end-style","border-block-end-width","border-block-start","border-block-start-color","border-block-start-style","border-block-start-width","border-block-style","border-block-width","border-bottom","border-bottom-color","border-bottom-left-radius","border-bottom-right-radius","border-bottom-style","border-bottom-width","border-collapse","border-color","border-image","border-image-outset","border-image-repeat","border-image-slice","border-image-source","border-image-width","border-inline","border-inline-color","border-inline-end","border-inline-end-color","border-inline-end-style","border-inline-end-width","border-inline-start","border-inline-start-color","border-inline-start-style","border-inline-start-width","border-inline-style","border-inline-width","border-left","border-left-color","border-left-style","border-left-width","border-radius","border-right","border-right-color","border-right-style","border-right-width","border-spacing","border-style","border-top","border-top-color","border-top-left-radius","border-top-right-radius","border-top-style","border-top-width","border-width","bottom","box-decoration-break","box-shadow","box-sizing","break-after","break-before","break-inside","caption-side","caret-color","clear","clip","clip-path","clip-rule","color","column-count","column-fill","column-gap","column-rule","column-rule-color","column-rule-style","column-rule-width","column-span","column-width","columns","contain","content","content-visibility","counter-increment","counter-reset","cue","cue-after","cue-before","cursor","direction","display","empty-cells","filter","flex","flex-basis","flex-direction","flex-flow","flex-grow","flex-shrink","flex-wrap","float","flow","font","font-display","font-family","font-feature-settings","font-kerning","font-language-override","font-size","font-size-adjust","font-smoothing","font-stretch","font-style","font-synthesis","font-variant","font-variant-caps","font-variant-east-asian","font-variant-ligatures","font-variant-numeric","font-variant-position","font-variation-settings","font-weight","gap","glyph-orientation-vertical","grid","grid-area","grid-auto-columns","grid-auto-flow","grid-auto-rows","grid-column","grid-column-end","grid-column-start","grid-gap","grid-row","grid-row-end","grid-row-start","grid-template","grid-template-areas","grid-template-columns","grid-template-rows","hanging-punctuation","height","hyphens","icon","image-orientation","image-rendering","image-resolution","ime-mode","inline-size","isolation","justify-content","left","letter-spacing","line-break","line-height","list-style","list-style-image","list-style-position","list-style-type","margin","margin-block","margin-block-end","margin-block-start","margin-bottom","margin-inline","margin-inline-end","margin-inline-start","margin-left","margin-right","margin-top","marks","mask","mask-border","mask-border-mode","mask-border-outset","mask-border-repeat","mask-border-slice","mask-border-source","mask-border-width","mask-clip","mask-composite","mask-image","mask-mode","mask-origin","mask-position","mask-repeat","mask-size","mask-type","max-block-size","max-height","max-inline-size","max-width","min-block-size","min-height","min-inline-size","min-width","mix-blend-mode","nav-down","nav-index","nav-left","nav-right","nav-up","none","normal","object-fit","object-position","opacity","order","orphans","outline","outline-color","outline-offset","outline-style","outline-width","overflow","overflow-wrap","overflow-x","overflow-y","padding","padding-block","padding-block-end","padding-block-start","padding-bottom","padding-inline","padding-inline-end","padding-inline-start","padding-left","padding-right","padding-top","page-break-after","page-break-before","page-break-inside","pause","pause-after","pause-before","perspective","perspective-origin","pointer-events","position","quotes","resize","rest","rest-after","rest-before","right","row-gap","scroll-margin","scroll-margin-block","scroll-margin-block-end","scroll-margin-block-start","scroll-margin-bottom","scroll-margin-inline","scroll-margin-inline-end","scroll-margin-inline-start","scroll-margin-left","scroll-margin-right","scroll-margin-top","scroll-padding","scroll-padding-block","scroll-padding-block-end","scroll-padding-block-start","scroll-padding-bottom","scroll-padding-inline","scroll-padding-inline-end","scroll-padding-inline-start","scroll-padding-left","scroll-padding-right","scroll-padding-top","scroll-snap-align","scroll-snap-stop","scroll-snap-type","scrollbar-color","scrollbar-gutter","scrollbar-width","shape-image-threshold","shape-margin","shape-outside","speak","speak-as","src","tab-size","table-layout","text-align","text-align-all","text-align-last","text-combine-upright","text-decoration","text-decoration-color","text-decoration-line","text-decoration-style","text-emphasis","text-emphasis-color","text-emphasis-position","text-emphasis-style","text-indent","text-justify","text-orientation","text-overflow","text-rendering","text-shadow","text-transform","text-underline-position","top","transform","transform-box","transform-origin","transform-style","transition","transition-delay","transition-duration","transition-property","transition-timing-function","unicode-bidi","vertical-align","visibility","voice-balance","voice-duration","voice-family","voice-pitch","voice-range","voice-rate","voice-stress","voice-volume","white-space","widows","width","will-change","word-break","word-spacing","word-wrap","writing-mode","z-index"].reverse(),de=oe.concat(le) +;var ge="[0-9](_*[0-9])*",ue=`\\.(${ge})`,be="[0-9a-fA-F](_*[0-9a-fA-F])*",me={ +className:"number",variants:[{ +begin:`(\\b(${ge})((${ue})|\\.)?|(${ue}))[eE][+-]?(${ge})[fFdD]?\\b`},{ +begin:`\\b(${ge})((${ue})[fFdD]?\\b|\\.([fFdD]\\b)?)`},{ +begin:`(${ue})[fFdD]?\\b`},{begin:`\\b(${ge})[fFdD]\\b`},{ +begin:`\\b0[xX]((${be})\\.?|(${be})?\\.(${be}))[pP][+-]?(${ge})[fFdD]?\\b`},{ +begin:"\\b(0|[1-9](_*[0-9])*)[lL]?\\b"},{begin:`\\b0[xX](${be})[lL]?\\b`},{ +begin:"\\b0(_*[0-7])*[lL]?\\b"},{begin:"\\b0[bB][01](_*[01])*[lL]?\\b"}], +relevance:0};function pe(e,n,t){return-1===t?"":e.replace(n,(a=>pe(e,n,t-1)))} +const _e="[A-Za-z$_][0-9A-Za-z$_]*",he=["as","in","of","if","for","while","finally","var","new","function","do","return","void","else","break","catch","instanceof","with","throw","case","default","try","switch","continue","typeof","delete","let","yield","const","class","debugger","async","await","static","import","from","export","extends"],fe=["true","false","null","undefined","NaN","Infinity"],Ee=["Object","Function","Boolean","Symbol","Math","Date","Number","BigInt","String","RegExp","Array","Float32Array","Float64Array","Int8Array","Uint8Array","Uint8ClampedArray","Int16Array","Int32Array","Uint16Array","Uint32Array","BigInt64Array","BigUint64Array","Set","Map","WeakSet","WeakMap","ArrayBuffer","SharedArrayBuffer","Atomics","DataView","JSON","Promise","Generator","GeneratorFunction","AsyncFunction","Reflect","Proxy","Intl","WebAssembly"],ye=["Error","EvalError","InternalError","RangeError","ReferenceError","SyntaxError","TypeError","URIError"],Ne=["setInterval","setTimeout","clearInterval","clearTimeout","require","exports","eval","isFinite","isNaN","parseFloat","parseInt","decodeURI","decodeURIComponent","encodeURI","encodeURIComponent","escape","unescape"],we=["arguments","this","super","console","window","document","localStorage","sessionStorage","module","global"],ve=[].concat(Ne,Ee,ye) +;function Oe(e){const n=e.regex,t=_e,a={begin:/<[A-Za-z0-9\\._:-]+/, +end:/\/[A-Za-z0-9\\._:-]+>|\/>/,isTrulyOpeningTag:(e,n)=>{ +const t=e[0].length+e.index,a=e.input[t] +;if("<"===a||","===a)return void n.ignoreMatch();let i +;">"===a&&(((e,{after:n})=>{const t="",M={ +match:[/const|var|let/,/\s+/,t,/\s*/,/=\s*/,/(async\s*)?/,n.lookahead(x)], +keywords:"async",className:{1:"keyword",3:"title.function"},contains:[f]} +;return{name:"JavaScript",aliases:["js","jsx","mjs","cjs"],keywords:i,exports:{ +PARAMS_CONTAINS:h,CLASS_REFERENCE:y},illegal:/#(?![$_A-z])/, +contains:[e.SHEBANG({label:"shebang",binary:"node",relevance:5}),{ +label:"use_strict",className:"meta",relevance:10, +begin:/^\s*['"]use (strict|asm)['"]/ +},e.APOS_STRING_MODE,e.QUOTE_STRING_MODE,d,g,u,b,m,{match:/\$\d+/},l,y,{ +className:"attr",begin:t+n.lookahead(":"),relevance:0},M,{ +begin:"("+e.RE_STARTERS_RE+"|\\b(case|return|throw)\\b)\\s*", +keywords:"return throw case",relevance:0,contains:[m,e.REGEXP_MODE,{ +className:"function",begin:x,returnBegin:!0,end:"\\s*=>",contains:[{ +className:"params",variants:[{begin:e.UNDERSCORE_IDENT_RE,relevance:0},{ +className:null,begin:/\(\s*\)/,skip:!0},{begin:/\(/,end:/\)/,excludeBegin:!0, +excludeEnd:!0,keywords:i,contains:h}]}]},{begin:/,/,relevance:0},{match:/\s+/, +relevance:0},{variants:[{begin:"<>",end:""},{ +match:/<[A-Za-z0-9\\._:-]+\s*\/>/},{begin:a.begin, +"on:begin":a.isTrulyOpeningTag,end:a.end}],subLanguage:"xml",contains:[{ +begin:a.begin,end:a.end,skip:!0,contains:["self"]}]}]},N,{ +beginKeywords:"while if switch catch for"},{ +begin:"\\b(?!function)"+e.UNDERSCORE_IDENT_RE+"\\([^()]*(\\([^()]*(\\([^()]*\\)[^()]*)*\\)[^()]*)*\\)\\s*\\{", +returnBegin:!0,label:"func.def",contains:[f,e.inherit(e.TITLE_MODE,{begin:t, +className:"title.function"})]},{match:/\.\.\./,relevance:0},O,{match:"\\$"+t, +relevance:0},{match:[/\bconstructor(?=\s*\()/],className:{1:"title.function"}, +contains:[f]},w,{relevance:0,match:/\b[A-Z][A-Z_0-9]+\b/, +className:"variable.constant"},E,k,{match:/\$[(.]/}]}} +const ke=e=>b(/\b/,e,/\w$/.test(e)?/\b/:/\B/),xe=["Protocol","Type"].map(ke),Me=["init","self"].map(ke),Se=["Any","Self"],Ae=["actor","any","associatedtype","async","await",/as\?/,/as!/,"as","borrowing","break","case","catch","class","consume","consuming","continue","convenience","copy","default","defer","deinit","didSet","distributed","do","dynamic","each","else","enum","extension","fallthrough",/fileprivate\(set\)/,"fileprivate","final","for","func","get","guard","if","import","indirect","infix",/init\?/,/init!/,"inout",/internal\(set\)/,"internal","in","is","isolated","nonisolated","lazy","let","macro","mutating","nonmutating",/open\(set\)/,"open","operator","optional","override","postfix","precedencegroup","prefix",/private\(set\)/,"private","protocol",/public\(set\)/,"public","repeat","required","rethrows","return","set","some","static","struct","subscript","super","switch","throws","throw",/try\?/,/try!/,"try","typealias",/unowned\(safe\)/,/unowned\(unsafe\)/,"unowned","var","weak","where","while","willSet"],Ce=["false","nil","true"],Te=["assignment","associativity","higherThan","left","lowerThan","none","right"],Re=["#colorLiteral","#column","#dsohandle","#else","#elseif","#endif","#error","#file","#fileID","#fileLiteral","#filePath","#function","#if","#imageLiteral","#keyPath","#line","#selector","#sourceLocation","#warning"],De=["abs","all","any","assert","assertionFailure","debugPrint","dump","fatalError","getVaList","isKnownUniquelyReferenced","max","min","numericCast","pointwiseMax","pointwiseMin","precondition","preconditionFailure","print","readLine","repeatElement","sequence","stride","swap","swift_unboxFromSwiftValueWithType","transcode","type","unsafeBitCast","unsafeDowncast","withExtendedLifetime","withUnsafeMutablePointer","withUnsafePointer","withVaList","withoutActuallyEscaping","zip"],Ie=m(/[/=\-+!*%<>&|^~?]/,/[\u00A1-\u00A7]/,/[\u00A9\u00AB]/,/[\u00AC\u00AE]/,/[\u00B0\u00B1]/,/[\u00B6\u00BB\u00BF\u00D7\u00F7]/,/[\u2016-\u2017]/,/[\u2020-\u2027]/,/[\u2030-\u203E]/,/[\u2041-\u2053]/,/[\u2055-\u205E]/,/[\u2190-\u23FF]/,/[\u2500-\u2775]/,/[\u2794-\u2BFF]/,/[\u2E00-\u2E7F]/,/[\u3001-\u3003]/,/[\u3008-\u3020]/,/[\u3030]/),Le=m(Ie,/[\u0300-\u036F]/,/[\u1DC0-\u1DFF]/,/[\u20D0-\u20FF]/,/[\uFE00-\uFE0F]/,/[\uFE20-\uFE2F]/),Be=b(Ie,Le,"*"),$e=m(/[a-zA-Z_]/,/[\u00A8\u00AA\u00AD\u00AF\u00B2-\u00B5\u00B7-\u00BA]/,/[\u00BC-\u00BE\u00C0-\u00D6\u00D8-\u00F6\u00F8-\u00FF]/,/[\u0100-\u02FF\u0370-\u167F\u1681-\u180D\u180F-\u1DBF]/,/[\u1E00-\u1FFF]/,/[\u200B-\u200D\u202A-\u202E\u203F-\u2040\u2054\u2060-\u206F]/,/[\u2070-\u20CF\u2100-\u218F\u2460-\u24FF\u2776-\u2793]/,/[\u2C00-\u2DFF\u2E80-\u2FFF]/,/[\u3004-\u3007\u3021-\u302F\u3031-\u303F\u3040-\uD7FF]/,/[\uF900-\uFD3D\uFD40-\uFDCF\uFDF0-\uFE1F\uFE30-\uFE44]/,/[\uFE47-\uFEFE\uFF00-\uFFFD]/),ze=m($e,/\d/,/[\u0300-\u036F\u1DC0-\u1DFF\u20D0-\u20FF\uFE20-\uFE2F]/),Fe=b($e,ze,"*"),Ue=b(/[A-Z]/,ze,"*"),je=["attached","autoclosure",b(/convention\(/,m("swift","block","c"),/\)/),"discardableResult","dynamicCallable","dynamicMemberLookup","escaping","freestanding","frozen","GKInspectable","IBAction","IBDesignable","IBInspectable","IBOutlet","IBSegueAction","inlinable","main","nonobjc","NSApplicationMain","NSCopying","NSManaged",b(/objc\(/,Fe,/\)/),"objc","objcMembers","propertyWrapper","requires_stored_property_inits","resultBuilder","Sendable","testable","UIApplicationMain","unchecked","unknown","usableFromInline","warn_unqualified_access"],Pe=["iOS","iOSApplicationExtension","macOS","macOSApplicationExtension","macCatalyst","macCatalystApplicationExtension","watchOS","watchOSApplicationExtension","tvOS","tvOSApplicationExtension","swift"] +;var Ke=Object.freeze({__proto__:null,grmr_bash:e=>{const n=e.regex,t={},a={ +begin:/\$\{/,end:/\}/,contains:["self",{begin:/:-/,contains:[t]}]} +;Object.assign(t,{className:"variable",variants:[{ +begin:n.concat(/\$[\w\d#@][\w\d_]*/,"(?![\\w\\d])(?![$])")},a]});const i={ +className:"subst",begin:/\$\(/,end:/\)/,contains:[e.BACKSLASH_ESCAPE]},r={ +begin:/<<-?\s*(?=\w+)/,starts:{contains:[e.END_SAME_AS_BEGIN({begin:/(\w+)/, +end:/(\w+)/,className:"string"})]}},s={className:"string",begin:/"/,end:/"/, +contains:[e.BACKSLASH_ESCAPE,t,i]};i.contains.push(s);const o={begin:/\$?\(\(/, +end:/\)\)/,contains:[{begin:/\d+#[0-9a-f]+/,className:"number"},e.NUMBER_MODE,t] +},l=e.SHEBANG({binary:"(fish|bash|zsh|sh|csh|ksh|tcsh|dash|scsh)",relevance:10 +}),c={className:"function",begin:/\w[\w\d_]*\s*\(\s*\)\s*\{/,returnBegin:!0, +contains:[e.inherit(e.TITLE_MODE,{begin:/\w[\w\d_]*/})],relevance:0};return{ +name:"Bash",aliases:["sh"],keywords:{$pattern:/\b[a-z][a-z0-9._-]+\b/, +keyword:["if","then","else","elif","fi","for","while","until","in","do","done","case","esac","function","select"], +literal:["true","false"], +built_in:["break","cd","continue","eval","exec","exit","export","getopts","hash","pwd","readonly","return","shift","test","times","trap","umask","unset","alias","bind","builtin","caller","command","declare","echo","enable","help","let","local","logout","mapfile","printf","read","readarray","source","type","typeset","ulimit","unalias","set","shopt","autoload","bg","bindkey","bye","cap","chdir","clone","comparguments","compcall","compctl","compdescribe","compfiles","compgroups","compquote","comptags","comptry","compvalues","dirs","disable","disown","echotc","echoti","emulate","fc","fg","float","functions","getcap","getln","history","integer","jobs","kill","limit","log","noglob","popd","print","pushd","pushln","rehash","sched","setcap","setopt","stat","suspend","ttyctl","unfunction","unhash","unlimit","unsetopt","vared","wait","whence","where","which","zcompile","zformat","zftp","zle","zmodload","zparseopts","zprof","zpty","zregexparse","zsocket","zstyle","ztcp","chcon","chgrp","chown","chmod","cp","dd","df","dir","dircolors","ln","ls","mkdir","mkfifo","mknod","mktemp","mv","realpath","rm","rmdir","shred","sync","touch","truncate","vdir","b2sum","base32","base64","cat","cksum","comm","csplit","cut","expand","fmt","fold","head","join","md5sum","nl","numfmt","od","paste","ptx","pr","sha1sum","sha224sum","sha256sum","sha384sum","sha512sum","shuf","sort","split","sum","tac","tail","tr","tsort","unexpand","uniq","wc","arch","basename","chroot","date","dirname","du","echo","env","expr","factor","groups","hostid","id","link","logname","nice","nohup","nproc","pathchk","pinky","printenv","printf","pwd","readlink","runcon","seq","sleep","stat","stdbuf","stty","tee","test","timeout","tty","uname","unlink","uptime","users","who","whoami","yes"] +},contains:[l,e.SHEBANG(),c,o,e.HASH_COMMENT_MODE,r,{match:/(\/[a-z._-]+)+/},s,{ +match:/\\"/},{className:"string",begin:/'/,end:/'/},{match:/\\'/},t]}}, +grmr_c:e=>{const n=e.regex,t=e.COMMENT("//","$",{contains:[{begin:/\\\n/}] +}),a="decltype\\(auto\\)",i="[a-zA-Z_]\\w*::",r="("+a+"|"+n.optional(i)+"[a-zA-Z_]\\w*"+n.optional("<[^<>]+>")+")",s={ +className:"type",variants:[{begin:"\\b[a-z\\d_]*_t\\b"},{ +match:/\batomic_[a-z]{3,6}\b/}]},o={className:"string",variants:[{ +begin:'(u8?|U|L)?"',end:'"',illegal:"\\n",contains:[e.BACKSLASH_ESCAPE]},{ +begin:"(u8?|U|L)?'(\\\\(x[0-9A-Fa-f]{2}|u[0-9A-Fa-f]{4,8}|[0-7]{3}|\\S)|.)", +end:"'",illegal:"."},e.END_SAME_AS_BEGIN({ +begin:/(?:u8?|U|L)?R"([^()\\ ]{0,16})\(/,end:/\)([^()\\ ]{0,16})"/})]},l={ +className:"number",variants:[{begin:"\\b(0b[01']+)"},{ +begin:"(-?)\\b([\\d']+(\\.[\\d']*)?|\\.[\\d']+)((ll|LL|l|L)(u|U)?|(u|U)(ll|LL|l|L)?|f|F|b|B)" +},{ +begin:"(-?)(\\b0[xX][a-fA-F0-9']+|(\\b[\\d']+(\\.[\\d']*)?|\\.[\\d']+)([eE][-+]?[\\d']+)?)" +}],relevance:0},c={className:"meta",begin:/#\s*[a-z]+\b/,end:/$/,keywords:{ +keyword:"if else elif endif define undef warning error line pragma _Pragma ifdef ifndef include" +},contains:[{begin:/\\\n/,relevance:0},e.inherit(o,{className:"string"}),{ +className:"string",begin:/<.*?>/},t,e.C_BLOCK_COMMENT_MODE]},d={ +className:"title",begin:n.optional(i)+e.IDENT_RE,relevance:0 +},g=n.optional(i)+e.IDENT_RE+"\\s*\\(",u={ +keyword:["asm","auto","break","case","continue","default","do","else","enum","extern","for","fortran","goto","if","inline","register","restrict","return","sizeof","struct","switch","typedef","union","volatile","while","_Alignas","_Alignof","_Atomic","_Generic","_Noreturn","_Static_assert","_Thread_local","alignas","alignof","noreturn","static_assert","thread_local","_Pragma"], +type:["float","double","signed","unsigned","int","short","long","char","void","_Bool","_Complex","_Imaginary","_Decimal32","_Decimal64","_Decimal128","const","static","complex","bool","imaginary"], +literal:"true false NULL", +built_in:"std string wstring cin cout cerr clog stdin stdout stderr stringstream istringstream ostringstream auto_ptr deque list queue stack vector map set pair bitset multiset multimap unordered_set unordered_map unordered_multiset unordered_multimap priority_queue make_pair array shared_ptr abort terminate abs acos asin atan2 atan calloc ceil cosh cos exit exp fabs floor fmod fprintf fputs free frexp fscanf future isalnum isalpha iscntrl isdigit isgraph islower isprint ispunct isspace isupper isxdigit tolower toupper labs ldexp log10 log malloc realloc memchr memcmp memcpy memset modf pow printf putchar puts scanf sinh sin snprintf sprintf sqrt sscanf strcat strchr strcmp strcpy strcspn strlen strncat strncmp strncpy strpbrk strrchr strspn strstr tanh tan vfprintf vprintf vsprintf endl initializer_list unique_ptr" +},b=[c,s,t,e.C_BLOCK_COMMENT_MODE,l,o],m={variants:[{begin:/=/,end:/;/},{ +begin:/\(/,end:/\)/},{beginKeywords:"new throw return else",end:/;/}], +keywords:u,contains:b.concat([{begin:/\(/,end:/\)/,keywords:u, +contains:b.concat(["self"]),relevance:0}]),relevance:0},p={ +begin:"("+r+"[\\*&\\s]+)+"+g,returnBegin:!0,end:/[{;=]/,excludeEnd:!0, +keywords:u,illegal:/[^\w\s\*&:<>.]/,contains:[{begin:a,keywords:u,relevance:0},{ +begin:g,returnBegin:!0,contains:[e.inherit(d,{className:"title.function"})], +relevance:0},{relevance:0,match:/,/},{className:"params",begin:/\(/,end:/\)/, +keywords:u,relevance:0,contains:[t,e.C_BLOCK_COMMENT_MODE,o,l,s,{begin:/\(/, +end:/\)/,keywords:u,relevance:0,contains:["self",t,e.C_BLOCK_COMMENT_MODE,o,l,s] +}]},s,t,e.C_BLOCK_COMMENT_MODE,c]};return{name:"C",aliases:["h"],keywords:u, +disableAutodetect:!0,illegal:"=]/,contains:[{ +beginKeywords:"final class struct"},e.TITLE_MODE]}]),exports:{preprocessor:c, +strings:o,keywords:u}}},grmr_cpp:e=>{const n=e.regex,t=e.COMMENT("//","$",{ +contains:[{begin:/\\\n/}] +}),a="decltype\\(auto\\)",i="[a-zA-Z_]\\w*::",r="(?!struct)("+a+"|"+n.optional(i)+"[a-zA-Z_]\\w*"+n.optional("<[^<>]+>")+")",s={ +className:"type",begin:"\\b[a-z\\d_]*_t\\b"},o={className:"string",variants:[{ +begin:'(u8?|U|L)?"',end:'"',illegal:"\\n",contains:[e.BACKSLASH_ESCAPE]},{ +begin:"(u8?|U|L)?'(\\\\(x[0-9A-Fa-f]{2}|u[0-9A-Fa-f]{4,8}|[0-7]{3}|\\S)|.)", +end:"'",illegal:"."},e.END_SAME_AS_BEGIN({ +begin:/(?:u8?|U|L)?R"([^()\\ ]{0,16})\(/,end:/\)([^()\\ ]{0,16})"/})]},l={ +className:"number",variants:[{begin:"\\b(0b[01']+)"},{ +begin:"(-?)\\b([\\d']+(\\.[\\d']*)?|\\.[\\d']+)((ll|LL|l|L)(u|U)?|(u|U)(ll|LL|l|L)?|f|F|b|B)" +},{ +begin:"(-?)(\\b0[xX][a-fA-F0-9']+|(\\b[\\d']+(\\.[\\d']*)?|\\.[\\d']+)([eE][-+]?[\\d']+)?)" +}],relevance:0},c={className:"meta",begin:/#\s*[a-z]+\b/,end:/$/,keywords:{ +keyword:"if else elif endif define undef warning error line pragma _Pragma ifdef ifndef include" +},contains:[{begin:/\\\n/,relevance:0},e.inherit(o,{className:"string"}),{ +className:"string",begin:/<.*?>/},t,e.C_BLOCK_COMMENT_MODE]},d={ +className:"title",begin:n.optional(i)+e.IDENT_RE,relevance:0 +},g=n.optional(i)+e.IDENT_RE+"\\s*\\(",u={ +type:["bool","char","char16_t","char32_t","char8_t","double","float","int","long","short","void","wchar_t","unsigned","signed","const","static"], +keyword:["alignas","alignof","and","and_eq","asm","atomic_cancel","atomic_commit","atomic_noexcept","auto","bitand","bitor","break","case","catch","class","co_await","co_return","co_yield","compl","concept","const_cast|10","consteval","constexpr","constinit","continue","decltype","default","delete","do","dynamic_cast|10","else","enum","explicit","export","extern","false","final","for","friend","goto","if","import","inline","module","mutable","namespace","new","noexcept","not","not_eq","nullptr","operator","or","or_eq","override","private","protected","public","reflexpr","register","reinterpret_cast|10","requires","return","sizeof","static_assert","static_cast|10","struct","switch","synchronized","template","this","thread_local","throw","transaction_safe","transaction_safe_dynamic","true","try","typedef","typeid","typename","union","using","virtual","volatile","while","xor","xor_eq"], +literal:["NULL","false","nullopt","nullptr","true"],built_in:["_Pragma"], +_type_hints:["any","auto_ptr","barrier","binary_semaphore","bitset","complex","condition_variable","condition_variable_any","counting_semaphore","deque","false_type","future","imaginary","initializer_list","istringstream","jthread","latch","lock_guard","multimap","multiset","mutex","optional","ostringstream","packaged_task","pair","promise","priority_queue","queue","recursive_mutex","recursive_timed_mutex","scoped_lock","set","shared_future","shared_lock","shared_mutex","shared_timed_mutex","shared_ptr","stack","string_view","stringstream","timed_mutex","thread","true_type","tuple","unique_lock","unique_ptr","unordered_map","unordered_multimap","unordered_multiset","unordered_set","variant","vector","weak_ptr","wstring","wstring_view"] +},b={className:"function.dispatch",relevance:0,keywords:{ +_hint:["abort","abs","acos","apply","as_const","asin","atan","atan2","calloc","ceil","cerr","cin","clog","cos","cosh","cout","declval","endl","exchange","exit","exp","fabs","floor","fmod","forward","fprintf","fputs","free","frexp","fscanf","future","invoke","isalnum","isalpha","iscntrl","isdigit","isgraph","islower","isprint","ispunct","isspace","isupper","isxdigit","labs","launder","ldexp","log","log10","make_pair","make_shared","make_shared_for_overwrite","make_tuple","make_unique","malloc","memchr","memcmp","memcpy","memset","modf","move","pow","printf","putchar","puts","realloc","scanf","sin","sinh","snprintf","sprintf","sqrt","sscanf","std","stderr","stdin","stdout","strcat","strchr","strcmp","strcpy","strcspn","strlen","strncat","strncmp","strncpy","strpbrk","strrchr","strspn","strstr","swap","tan","tanh","terminate","to_underlying","tolower","toupper","vfprintf","visit","vprintf","vsprintf"] +}, +begin:n.concat(/\b/,/(?!decltype)/,/(?!if)/,/(?!for)/,/(?!switch)/,/(?!while)/,e.IDENT_RE,n.lookahead(/(<[^<>]+>|)\s*\(/)) +},m=[b,c,s,t,e.C_BLOCK_COMMENT_MODE,l,o],p={variants:[{begin:/=/,end:/;/},{ +begin:/\(/,end:/\)/},{beginKeywords:"new throw return else",end:/;/}], +keywords:u,contains:m.concat([{begin:/\(/,end:/\)/,keywords:u, +contains:m.concat(["self"]),relevance:0}]),relevance:0},_={className:"function", +begin:"("+r+"[\\*&\\s]+)+"+g,returnBegin:!0,end:/[{;=]/,excludeEnd:!0, +keywords:u,illegal:/[^\w\s\*&:<>.]/,contains:[{begin:a,keywords:u,relevance:0},{ +begin:g,returnBegin:!0,contains:[d],relevance:0},{begin:/::/,relevance:0},{ +begin:/:/,endsWithParent:!0,contains:[o,l]},{relevance:0,match:/,/},{ +className:"params",begin:/\(/,end:/\)/,keywords:u,relevance:0, +contains:[t,e.C_BLOCK_COMMENT_MODE,o,l,s,{begin:/\(/,end:/\)/,keywords:u, +relevance:0,contains:["self",t,e.C_BLOCK_COMMENT_MODE,o,l,s]}] +},s,t,e.C_BLOCK_COMMENT_MODE,c]};return{name:"C++", +aliases:["cc","c++","h++","hpp","hh","hxx","cxx"],keywords:u,illegal:"",keywords:u,contains:["self",s]},{begin:e.IDENT_RE+"::",keywords:u},{ +match:[/\b(?:enum(?:\s+(?:class|struct))?|class|struct|union)/,/\s+/,/\w+/], +className:{1:"keyword",3:"title.class"}}])}},grmr_csharp:e=>{const n={ +keyword:["abstract","as","base","break","case","catch","class","const","continue","do","else","event","explicit","extern","finally","fixed","for","foreach","goto","if","implicit","in","interface","internal","is","lock","namespace","new","operator","out","override","params","private","protected","public","readonly","record","ref","return","scoped","sealed","sizeof","stackalloc","static","struct","switch","this","throw","try","typeof","unchecked","unsafe","using","virtual","void","volatile","while"].concat(["add","alias","and","ascending","async","await","by","descending","equals","from","get","global","group","init","into","join","let","nameof","not","notnull","on","or","orderby","partial","remove","select","set","unmanaged","value|0","var","when","where","with","yield"]), +built_in:["bool","byte","char","decimal","delegate","double","dynamic","enum","float","int","long","nint","nuint","object","sbyte","short","string","ulong","uint","ushort"], +literal:["default","false","null","true"]},t=e.inherit(e.TITLE_MODE,{ +begin:"[a-zA-Z](\\.?\\w)*"}),a={className:"number",variants:[{ +begin:"\\b(0b[01']+)"},{ +begin:"(-?)\\b([\\d']+(\\.[\\d']*)?|\\.[\\d']+)(u|U|l|L|ul|UL|f|F|b|B)"},{ +begin:"(-?)(\\b0[xX][a-fA-F0-9']+|(\\b[\\d']+(\\.[\\d']*)?|\\.[\\d']+)([eE][-+]?[\\d']+)?)" +}],relevance:0},i={className:"string",begin:'@"',end:'"',contains:[{begin:'""'}] +},r=e.inherit(i,{illegal:/\n/}),s={className:"subst",begin:/\{/,end:/\}/, +keywords:n},o=e.inherit(s,{illegal:/\n/}),l={className:"string",begin:/\$"/, +end:'"',illegal:/\n/,contains:[{begin:/\{\{/},{begin:/\}\}/ +},e.BACKSLASH_ESCAPE,o]},c={className:"string",begin:/\$@"/,end:'"',contains:[{ +begin:/\{\{/},{begin:/\}\}/},{begin:'""'},s]},d=e.inherit(c,{illegal:/\n/, +contains:[{begin:/\{\{/},{begin:/\}\}/},{begin:'""'},o]}) +;s.contains=[c,l,i,e.APOS_STRING_MODE,e.QUOTE_STRING_MODE,a,e.C_BLOCK_COMMENT_MODE], +o.contains=[d,l,r,e.APOS_STRING_MODE,e.QUOTE_STRING_MODE,a,e.inherit(e.C_BLOCK_COMMENT_MODE,{ +illegal:/\n/})];const g={variants:[c,l,i,e.APOS_STRING_MODE,e.QUOTE_STRING_MODE] +},u={begin:"<",end:">",contains:[{beginKeywords:"in out"},t] +},b=e.IDENT_RE+"(<"+e.IDENT_RE+"(\\s*,\\s*"+e.IDENT_RE+")*>)?(\\[\\])?",m={ +begin:"@"+e.IDENT_RE,relevance:0};return{name:"C#",aliases:["cs","c#"], +keywords:n,illegal:/::/,contains:[e.COMMENT("///","$",{returnBegin:!0, +contains:[{className:"doctag",variants:[{begin:"///",relevance:0},{ +begin:"\x3c!--|--\x3e"},{begin:""}]}] +}),e.C_LINE_COMMENT_MODE,e.C_BLOCK_COMMENT_MODE,{className:"meta",begin:"#", +end:"$",keywords:{ +keyword:"if else elif endif define undef warning error line region endregion pragma checksum" +}},g,a,{beginKeywords:"class interface",relevance:0,end:/[{;=]/, +illegal:/[^\s:,]/,contains:[{beginKeywords:"where class" +},t,u,e.C_LINE_COMMENT_MODE,e.C_BLOCK_COMMENT_MODE]},{beginKeywords:"namespace", +relevance:0,end:/[{;=]/,illegal:/[^\s:]/, +contains:[t,e.C_LINE_COMMENT_MODE,e.C_BLOCK_COMMENT_MODE]},{ +beginKeywords:"record",relevance:0,end:/[{;=]/,illegal:/[^\s:]/, +contains:[t,u,e.C_LINE_COMMENT_MODE,e.C_BLOCK_COMMENT_MODE]},{className:"meta", +begin:"^\\s*\\[(?=[\\w])",excludeBegin:!0,end:"\\]",excludeEnd:!0,contains:[{ +className:"string",begin:/"/,end:/"/}]},{ +beginKeywords:"new return throw await else",relevance:0},{className:"function", +begin:"("+b+"\\s+)+"+e.IDENT_RE+"\\s*(<[^=]+>\\s*)?\\(",returnBegin:!0, +end:/\s*[{;=]/,excludeEnd:!0,keywords:n,contains:[{ +beginKeywords:"public private protected static internal protected abstract async extern override unsafe virtual new sealed partial", +relevance:0},{begin:e.IDENT_RE+"\\s*(<[^=]+>\\s*)?\\(",returnBegin:!0, +contains:[e.TITLE_MODE,u],relevance:0},{match:/\(\)/},{className:"params", +begin:/\(/,end:/\)/,excludeBegin:!0,excludeEnd:!0,keywords:n,relevance:0, +contains:[g,a,e.C_BLOCK_COMMENT_MODE] +},e.C_LINE_COMMENT_MODE,e.C_BLOCK_COMMENT_MODE]},m]}},grmr_css:e=>{ +const n=e.regex,t=ie(e),a=[e.APOS_STRING_MODE,e.QUOTE_STRING_MODE];return{ +name:"CSS",case_insensitive:!0,illegal:/[=|'\$]/,keywords:{ +keyframePosition:"from to"},classNameAliases:{keyframePosition:"selector-tag"}, +contains:[t.BLOCK_COMMENT,{begin:/-(webkit|moz|ms|o)-(?=[a-z])/ +},t.CSS_NUMBER_MODE,{className:"selector-id",begin:/#[A-Za-z0-9_-]+/,relevance:0 +},{className:"selector-class",begin:"\\.[a-zA-Z-][a-zA-Z0-9_-]*",relevance:0 +},t.ATTRIBUTE_SELECTOR_MODE,{className:"selector-pseudo",variants:[{ +begin:":("+oe.join("|")+")"},{begin:":(:)?("+le.join("|")+")"}] +},t.CSS_VARIABLE,{className:"attribute",begin:"\\b("+ce.join("|")+")\\b"},{ +begin:/:/,end:/[;}{]/, +contains:[t.BLOCK_COMMENT,t.HEXCOLOR,t.IMPORTANT,t.CSS_NUMBER_MODE,...a,{ +begin:/(url|data-uri)\(/,end:/\)/,relevance:0,keywords:{built_in:"url data-uri" +},contains:[...a,{className:"string",begin:/[^)]/,endsWithParent:!0, +excludeEnd:!0}]},t.FUNCTION_DISPATCH]},{begin:n.lookahead(/@/),end:"[{;]", +relevance:0,illegal:/:/,contains:[{className:"keyword",begin:/@-?\w[\w]*(-\w+)*/ +},{begin:/\s/,endsWithParent:!0,excludeEnd:!0,relevance:0,keywords:{ +$pattern:/[a-z-]+/,keyword:"and or not only",attribute:se.join(" ")},contains:[{ +begin:/[a-z-]+(?=:)/,className:"attribute"},...a,t.CSS_NUMBER_MODE]}]},{ +className:"selector-tag",begin:"\\b("+re.join("|")+")\\b"}]}},grmr_diff:e=>{ +const n=e.regex;return{name:"Diff",aliases:["patch"],contains:[{ +className:"meta",relevance:10, +match:n.either(/^@@ +-\d+,\d+ +\+\d+,\d+ +@@/,/^\*\*\* +\d+,\d+ +\*\*\*\*$/,/^--- +\d+,\d+ +----$/) +},{className:"comment",variants:[{ +begin:n.either(/Index: /,/^index/,/={3,}/,/^-{3}/,/^\*{3} /,/^\+{3}/,/^diff --git/), +end:/$/},{match:/^\*{15}$/}]},{className:"addition",begin:/^\+/,end:/$/},{ +className:"deletion",begin:/^-/,end:/$/},{className:"addition",begin:/^!/, +end:/$/}]}},grmr_go:e=>{const n={ +keyword:["break","case","chan","const","continue","default","defer","else","fallthrough","for","func","go","goto","if","import","interface","map","package","range","return","select","struct","switch","type","var"], +type:["bool","byte","complex64","complex128","error","float32","float64","int8","int16","int32","int64","string","uint8","uint16","uint32","uint64","int","uint","uintptr","rune"], +literal:["true","false","iota","nil"], +built_in:["append","cap","close","complex","copy","imag","len","make","new","panic","print","println","real","recover","delete"] +};return{name:"Go",aliases:["golang"],keywords:n,illegal:"{const n=e.regex;return{name:"GraphQL",aliases:["gql"], +case_insensitive:!0,disableAutodetect:!1,keywords:{ +keyword:["query","mutation","subscription","type","input","schema","directive","interface","union","scalar","fragment","enum","on"], +literal:["true","false","null"]}, +contains:[e.HASH_COMMENT_MODE,e.QUOTE_STRING_MODE,e.NUMBER_MODE,{ +scope:"punctuation",match:/[.]{3}/,relevance:0},{scope:"punctuation", +begin:/[\!\(\)\:\=\[\]\{\|\}]{1}/,relevance:0},{scope:"variable",begin:/\$/, +end:/\W/,excludeEnd:!0,relevance:0},{scope:"meta",match:/@\w+/,excludeEnd:!0},{ +scope:"symbol",begin:n.concat(/[_A-Za-z][_0-9A-Za-z]*/,n.lookahead(/\s*:/)), +relevance:0}],illegal:[/[;<']/,/BEGIN/]}},grmr_ini:e=>{const n=e.regex,t={ +className:"number",relevance:0,variants:[{begin:/([+-]+)?[\d]+_[\d_]+/},{ +begin:e.NUMBER_RE}]},a=e.COMMENT();a.variants=[{begin:/;/,end:/$/},{begin:/#/, +end:/$/}];const i={className:"variable",variants:[{begin:/\$[\w\d"][\w\d_]*/},{ +begin:/\$\{(.*?)\}/}]},r={className:"literal", +begin:/\bon|off|true|false|yes|no\b/},s={className:"string", +contains:[e.BACKSLASH_ESCAPE],variants:[{begin:"'''",end:"'''",relevance:10},{ +begin:'"""',end:'"""',relevance:10},{begin:'"',end:'"'},{begin:"'",end:"'"}] +},o={begin:/\[/,end:/\]/,contains:[a,r,i,s,t,"self"],relevance:0 +},l=n.either(/[A-Za-z0-9_-]+/,/"(\\"|[^"])*"/,/'[^']*'/);return{ +name:"TOML, also INI",aliases:["toml"],case_insensitive:!0,illegal:/\S/, +contains:[a,{className:"section",begin:/\[+/,end:/\]+/},{ +begin:n.concat(l,"(\\s*\\.\\s*",l,")*",n.lookahead(/\s*=\s*[^#\s]/)), +className:"attr",starts:{end:/$/,contains:[a,o,r,i,s,t]}}]}},grmr_java:e=>{ +const n=e.regex,t="[\xc0-\u02b8a-zA-Z_$][\xc0-\u02b8a-zA-Z_$0-9]*",a=t+pe("(?:<"+t+"~~~(?:\\s*,\\s*"+t+"~~~)*>)?",/~~~/g,2),i={ +keyword:["synchronized","abstract","private","var","static","if","const ","for","while","strictfp","finally","protected","import","native","final","void","enum","else","break","transient","catch","instanceof","volatile","case","assert","package","default","public","try","switch","continue","throws","protected","public","private","module","requires","exports","do","sealed","yield","permits"], +literal:["false","true","null"], +type:["char","boolean","long","float","int","byte","short","double"], +built_in:["super","this"]},r={className:"meta",begin:"@"+t,contains:[{ +begin:/\(/,end:/\)/,contains:["self"]}]},s={className:"params",begin:/\(/, +end:/\)/,keywords:i,relevance:0,contains:[e.C_BLOCK_COMMENT_MODE],endsParent:!0} +;return{name:"Java",aliases:["jsp"],keywords:i,illegal:/<\/|#/, +contains:[e.COMMENT("/\\*\\*","\\*/",{relevance:0,contains:[{begin:/\w+@/, +relevance:0},{className:"doctag",begin:"@[A-Za-z]+"}]}),{ +begin:/import java\.[a-z]+\./,keywords:"import",relevance:2 +},e.C_LINE_COMMENT_MODE,e.C_BLOCK_COMMENT_MODE,{begin:/"""/,end:/"""/, +className:"string",contains:[e.BACKSLASH_ESCAPE] +},e.APOS_STRING_MODE,e.QUOTE_STRING_MODE,{ +match:[/\b(?:class|interface|enum|extends|implements|new)/,/\s+/,t],className:{ +1:"keyword",3:"title.class"}},{match:/non-sealed/,scope:"keyword"},{ +begin:[n.concat(/(?!else)/,t),/\s+/,t,/\s+/,/=(?!=)/],className:{1:"type", +3:"variable",5:"operator"}},{begin:[/record/,/\s+/,t],className:{1:"keyword", +3:"title.class"},contains:[s,e.C_LINE_COMMENT_MODE,e.C_BLOCK_COMMENT_MODE]},{ +beginKeywords:"new throw return else",relevance:0},{ +begin:["(?:"+a+"\\s+)",e.UNDERSCORE_IDENT_RE,/\s*(?=\()/],className:{ +2:"title.function"},keywords:i,contains:[{className:"params",begin:/\(/, +end:/\)/,keywords:i,relevance:0, +contains:[r,e.APOS_STRING_MODE,e.QUOTE_STRING_MODE,me,e.C_BLOCK_COMMENT_MODE] +},e.C_LINE_COMMENT_MODE,e.C_BLOCK_COMMENT_MODE]},me,r]}},grmr_javascript:Oe, +grmr_json:e=>{const n=["true","false","null"],t={scope:"literal", +beginKeywords:n.join(" ")};return{name:"JSON",keywords:{literal:n},contains:[{ +className:"attr",begin:/"(\\.|[^\\"\r\n])*"(?=\s*:)/,relevance:1.01},{ +match:/[{}[\],:]/,className:"punctuation",relevance:0 +},e.QUOTE_STRING_MODE,t,e.C_NUMBER_MODE,e.C_LINE_COMMENT_MODE,e.C_BLOCK_COMMENT_MODE], +illegal:"\\S"}},grmr_kotlin:e=>{const n={ +keyword:"abstract as val var vararg get set class object open private protected public noinline crossinline dynamic final enum if else do while for when throw try catch finally import package is in fun override companion reified inline lateinit init interface annotation data sealed internal infix operator out by constructor super tailrec where const inner suspend typealias external expect actual", +built_in:"Byte Short Char Int Long Boolean Float Double Void Unit Nothing", +literal:"true false null"},t={className:"symbol",begin:e.UNDERSCORE_IDENT_RE+"@" +},a={className:"subst",begin:/\$\{/,end:/\}/,contains:[e.C_NUMBER_MODE]},i={ +className:"variable",begin:"\\$"+e.UNDERSCORE_IDENT_RE},r={className:"string", +variants:[{begin:'"""',end:'"""(?=[^"])',contains:[i,a]},{begin:"'",end:"'", +illegal:/\n/,contains:[e.BACKSLASH_ESCAPE]},{begin:'"',end:'"',illegal:/\n/, +contains:[e.BACKSLASH_ESCAPE,i,a]}]};a.contains.push(r);const s={ +className:"meta", +begin:"@(?:file|property|field|get|set|receiver|param|setparam|delegate)\\s*:(?:\\s*"+e.UNDERSCORE_IDENT_RE+")?" +},o={className:"meta",begin:"@"+e.UNDERSCORE_IDENT_RE,contains:[{begin:/\(/, +end:/\)/,contains:[e.inherit(r,{className:"string"}),"self"]}] +},l=me,c=e.COMMENT("/\\*","\\*/",{contains:[e.C_BLOCK_COMMENT_MODE]}),d={ +variants:[{className:"type",begin:e.UNDERSCORE_IDENT_RE},{begin:/\(/,end:/\)/, +contains:[]}]},g=d;return g.variants[1].contains=[d],d.variants[1].contains=[g], +{name:"Kotlin",aliases:["kt","kts"],keywords:n, +contains:[e.COMMENT("/\\*\\*","\\*/",{relevance:0,contains:[{className:"doctag", +begin:"@[A-Za-z]+"}]}),e.C_LINE_COMMENT_MODE,c,{className:"keyword", +begin:/\b(break|continue|return|this)\b/,starts:{contains:[{className:"symbol", +begin:/@\w+/}]}},t,s,o,{className:"function",beginKeywords:"fun",end:"[(]|$", +returnBegin:!0,excludeEnd:!0,keywords:n,relevance:5,contains:[{ +begin:e.UNDERSCORE_IDENT_RE+"\\s*\\(",returnBegin:!0,relevance:0, +contains:[e.UNDERSCORE_TITLE_MODE]},{className:"type",begin://, +keywords:"reified",relevance:0},{className:"params",begin:/\(/,end:/\)/, +endsParent:!0,keywords:n,relevance:0,contains:[{begin:/:/,end:/[=,\/]/, +endsWithParent:!0,contains:[d,e.C_LINE_COMMENT_MODE,c],relevance:0 +},e.C_LINE_COMMENT_MODE,c,s,o,r,e.C_NUMBER_MODE]},c]},{ +begin:[/class|interface|trait/,/\s+/,e.UNDERSCORE_IDENT_RE],beginScope:{ +3:"title.class"},keywords:"class interface trait",end:/[:\{(]|$/,excludeEnd:!0, +illegal:"extends implements",contains:[{ +beginKeywords:"public protected internal private constructor" +},e.UNDERSCORE_TITLE_MODE,{className:"type",begin://,excludeBegin:!0, +excludeEnd:!0,relevance:0},{className:"type",begin:/[,:]\s*/,end:/[<\(,){\s]|$/, +excludeBegin:!0,returnEnd:!0},s,o]},r,{className:"meta",begin:"^#!/usr/bin/env", +end:"$",illegal:"\n"},l]}},grmr_less:e=>{ +const n=ie(e),t=de,a="[\\w-]+",i="("+a+"|@\\{"+a+"\\})",r=[],s=[],o=e=>({ +className:"string",begin:"~?"+e+".*?"+e}),l=(e,n,t)=>({className:e,begin:n, +relevance:t}),c={$pattern:/[a-z-]+/,keyword:"and or not only", +attribute:se.join(" ")},d={begin:"\\(",end:"\\)",contains:s,keywords:c, +relevance:0} +;s.push(e.C_LINE_COMMENT_MODE,e.C_BLOCK_COMMENT_MODE,o("'"),o('"'),n.CSS_NUMBER_MODE,{ +begin:"(url|data-uri)\\(",starts:{className:"string",end:"[\\)\\n]", +excludeEnd:!0} +},n.HEXCOLOR,d,l("variable","@@?"+a,10),l("variable","@\\{"+a+"\\}"),l("built_in","~?`[^`]*?`"),{ +className:"attribute",begin:a+"\\s*:",end:":",returnBegin:!0,excludeEnd:!0 +},n.IMPORTANT,{beginKeywords:"and not"},n.FUNCTION_DISPATCH);const g=s.concat({ +begin:/\{/,end:/\}/,contains:r}),u={beginKeywords:"when",endsWithParent:!0, +contains:[{beginKeywords:"and not"}].concat(s)},b={begin:i+"\\s*:", +returnBegin:!0,end:/[;}]/,relevance:0,contains:[{begin:/-(webkit|moz|ms|o)-/ +},n.CSS_VARIABLE,{className:"attribute",begin:"\\b("+ce.join("|")+")\\b", +end:/(?=:)/,starts:{endsWithParent:!0,illegal:"[<=$]",relevance:0,contains:s}}] +},m={className:"keyword", +begin:"@(import|media|charset|font-face|(-[a-z]+-)?keyframes|supports|document|namespace|page|viewport|host)\\b", +starts:{end:"[;{}]",keywords:c,returnEnd:!0,contains:s,relevance:0}},p={ +className:"variable",variants:[{begin:"@"+a+"\\s*:",relevance:15},{begin:"@"+a +}],starts:{end:"[;}]",returnEnd:!0,contains:g}},_={variants:[{ +begin:"[\\.#:&\\[>]",end:"[;{}]"},{begin:i,end:/\{/}],returnBegin:!0, +returnEnd:!0,illegal:"[<='$\"]",relevance:0, +contains:[e.C_LINE_COMMENT_MODE,e.C_BLOCK_COMMENT_MODE,u,l("keyword","all\\b"),l("variable","@\\{"+a+"\\}"),{ +begin:"\\b("+re.join("|")+")\\b",className:"selector-tag" +},n.CSS_NUMBER_MODE,l("selector-tag",i,0),l("selector-id","#"+i),l("selector-class","\\."+i,0),l("selector-tag","&",0),n.ATTRIBUTE_SELECTOR_MODE,{ +className:"selector-pseudo",begin:":("+oe.join("|")+")"},{ +className:"selector-pseudo",begin:":(:)?("+le.join("|")+")"},{begin:/\(/, +end:/\)/,relevance:0,contains:g},{begin:"!important"},n.FUNCTION_DISPATCH]},h={ +begin:a+":(:)?"+`(${t.join("|")})`,returnBegin:!0,contains:[_]} +;return r.push(e.C_LINE_COMMENT_MODE,e.C_BLOCK_COMMENT_MODE,m,p,h,b,_,u,n.FUNCTION_DISPATCH), +{name:"Less",case_insensitive:!0,illegal:"[=>'/<($\"]",contains:r}}, +grmr_lua:e=>{const n="\\[=*\\[",t="\\]=*\\]",a={begin:n,end:t,contains:["self"] +},i=[e.COMMENT("--(?!"+n+")","$"),e.COMMENT("--"+n,t,{contains:[a],relevance:10 +})];return{name:"Lua",keywords:{$pattern:e.UNDERSCORE_IDENT_RE, +literal:"true false nil", +keyword:"and break do else elseif end for goto if in local not or repeat return then until while", +built_in:"_G _ENV _VERSION __index __newindex __mode __call __metatable __tostring __len __gc __add __sub __mul __div __mod __pow __concat __unm __eq __lt __le assert collectgarbage dofile error getfenv getmetatable ipairs load loadfile loadstring module next pairs pcall print rawequal rawget rawset require select setfenv setmetatable tonumber tostring type unpack xpcall arg self coroutine resume yield status wrap create running debug getupvalue debug sethook getmetatable gethook setmetatable setlocal traceback setfenv getinfo setupvalue getlocal getregistry getfenv io lines write close flush open output type read stderr stdin input stdout popen tmpfile math log max acos huge ldexp pi cos tanh pow deg tan cosh sinh random randomseed frexp ceil floor rad abs sqrt modf asin min mod fmod log10 atan2 exp sin atan os exit setlocale date getenv difftime remove time clock tmpname rename execute package preload loadlib loaded loaders cpath config path seeall string sub upper len gfind rep find match char dump gmatch reverse byte format gsub lower table setn insert getn foreachi maxn foreach concat sort remove" +},contains:i.concat([{className:"function",beginKeywords:"function",end:"\\)", +contains:[e.inherit(e.TITLE_MODE,{ +begin:"([_a-zA-Z]\\w*\\.)*([_a-zA-Z]\\w*:)?[_a-zA-Z]\\w*"}),{className:"params", +begin:"\\(",endsWithParent:!0,contains:i}].concat(i) +},e.C_NUMBER_MODE,e.APOS_STRING_MODE,e.QUOTE_STRING_MODE,{className:"string", +begin:n,end:t,contains:[a],relevance:5}])}},grmr_makefile:e=>{const n={ +className:"variable",variants:[{begin:"\\$\\("+e.UNDERSCORE_IDENT_RE+"\\)", +contains:[e.BACKSLASH_ESCAPE]},{begin:/\$[@%{ +const n={begin:/<\/?[A-Za-z_]/,end:">",subLanguage:"xml",relevance:0},t={ +variants:[{begin:/\[.+?\]\[.*?\]/,relevance:0},{ +begin:/\[.+?\]\(((data|javascript|mailto):|(?:http|ftp)s?:\/\/).*?\)/, +relevance:2},{ +begin:e.regex.concat(/\[.+?\]\(/,/[A-Za-z][A-Za-z0-9+.-]*/,/:\/\/.*?\)/), +relevance:2},{begin:/\[.+?\]\([./?&#].*?\)/,relevance:1},{ +begin:/\[.*?\]\(.*?\)/,relevance:0}],returnBegin:!0,contains:[{match:/\[(?=\])/ +},{className:"string",relevance:0,begin:"\\[",end:"\\]",excludeBegin:!0, +returnEnd:!0},{className:"link",relevance:0,begin:"\\]\\(",end:"\\)", +excludeBegin:!0,excludeEnd:!0},{className:"symbol",relevance:0,begin:"\\]\\[", +end:"\\]",excludeBegin:!0,excludeEnd:!0}]},a={className:"strong",contains:[], +variants:[{begin:/_{2}(?!\s)/,end:/_{2}/},{begin:/\*{2}(?!\s)/,end:/\*{2}/}] +},i={className:"emphasis",contains:[],variants:[{begin:/\*(?![*\s])/,end:/\*/},{ +begin:/_(?![_\s])/,end:/_/,relevance:0}]},r=e.inherit(a,{contains:[] +}),s=e.inherit(i,{contains:[]});a.contains.push(s),i.contains.push(r) +;let o=[n,t];return[a,i,r,s].forEach((e=>{e.contains=e.contains.concat(o) +})),o=o.concat(a,i),{name:"Markdown",aliases:["md","mkdown","mkd"],contains:[{ +className:"section",variants:[{begin:"^#{1,6}",end:"$",contains:o},{ +begin:"(?=^.+?\\n[=-]{2,}$)",contains:[{begin:"^[=-]*$"},{begin:"^",end:"\\n", +contains:o}]}]},n,{className:"bullet",begin:"^[ \t]*([*+-]|(\\d+\\.))(?=\\s+)", +end:"\\s+",excludeEnd:!0},a,i,{className:"quote",begin:"^>\\s+",contains:o, +end:"$"},{className:"code",variants:[{begin:"(`{3,})[^`](.|\\n)*?\\1`*[ ]*"},{ +begin:"(~{3,})[^~](.|\\n)*?\\1~*[ ]*"},{begin:"```",end:"```+[ ]*$"},{ +begin:"~~~",end:"~~~+[ ]*$"},{begin:"`.+?`"},{begin:"(?=^( {4}|\\t))", +contains:[{begin:"^( {4}|\\t)",end:"(\\n)$"}],relevance:0}]},{ +begin:"^[-\\*]{3,}",end:"$"},t,{begin:/^\[[^\n]+\]:/,returnBegin:!0,contains:[{ +className:"symbol",begin:/\[/,end:/\]/,excludeBegin:!0,excludeEnd:!0},{ +className:"link",begin:/:\s*/,end:/$/,excludeBegin:!0}]}]}},grmr_objectivec:e=>{ +const n=/[a-zA-Z@][a-zA-Z0-9_]*/,t={$pattern:n, +keyword:["@interface","@class","@protocol","@implementation"]};return{ +name:"Objective-C",aliases:["mm","objc","obj-c","obj-c++","objective-c++"], +keywords:{"variable.language":["this","super"],$pattern:n, +keyword:["while","export","sizeof","typedef","const","struct","for","union","volatile","static","mutable","if","do","return","goto","enum","else","break","extern","asm","case","default","register","explicit","typename","switch","continue","inline","readonly","assign","readwrite","self","@synchronized","id","typeof","nonatomic","IBOutlet","IBAction","strong","weak","copy","in","out","inout","bycopy","byref","oneway","__strong","__weak","__block","__autoreleasing","@private","@protected","@public","@try","@property","@end","@throw","@catch","@finally","@autoreleasepool","@synthesize","@dynamic","@selector","@optional","@required","@encode","@package","@import","@defs","@compatibility_alias","__bridge","__bridge_transfer","__bridge_retained","__bridge_retain","__covariant","__contravariant","__kindof","_Nonnull","_Nullable","_Null_unspecified","__FUNCTION__","__PRETTY_FUNCTION__","__attribute__","getter","setter","retain","unsafe_unretained","nonnull","nullable","null_unspecified","null_resettable","class","instancetype","NS_DESIGNATED_INITIALIZER","NS_UNAVAILABLE","NS_REQUIRES_SUPER","NS_RETURNS_INNER_POINTER","NS_INLINE","NS_AVAILABLE","NS_DEPRECATED","NS_ENUM","NS_OPTIONS","NS_SWIFT_UNAVAILABLE","NS_ASSUME_NONNULL_BEGIN","NS_ASSUME_NONNULL_END","NS_REFINED_FOR_SWIFT","NS_SWIFT_NAME","NS_SWIFT_NOTHROW","NS_DURING","NS_HANDLER","NS_ENDHANDLER","NS_VALUERETURN","NS_VOIDRETURN"], +literal:["false","true","FALSE","TRUE","nil","YES","NO","NULL"], +built_in:["dispatch_once_t","dispatch_queue_t","dispatch_sync","dispatch_async","dispatch_once"], +type:["int","float","char","unsigned","signed","short","long","double","wchar_t","unichar","void","bool","BOOL","id|0","_Bool"] +},illegal:"/,end:/$/,illegal:"\\n" +},e.C_LINE_COMMENT_MODE,e.C_BLOCK_COMMENT_MODE]},{className:"class", +begin:"("+t.keyword.join("|")+")\\b",end:/(\{|$)/,excludeEnd:!0,keywords:t, +contains:[e.UNDERSCORE_TITLE_MODE]},{begin:"\\."+e.UNDERSCORE_IDENT_RE, +relevance:0}]}},grmr_perl:e=>{const n=e.regex,t=/[dualxmsipngr]{0,12}/,a={ +$pattern:/[\w.]+/, +keyword:"abs accept alarm and atan2 bind binmode bless break caller chdir chmod chomp chop chown chr chroot close closedir connect continue cos crypt dbmclose dbmopen defined delete die do dump each else elsif endgrent endhostent endnetent endprotoent endpwent endservent eof eval exec exists exit exp fcntl fileno flock for foreach fork format formline getc getgrent getgrgid getgrnam gethostbyaddr gethostbyname gethostent getlogin getnetbyaddr getnetbyname getnetent getpeername getpgrp getpriority getprotobyname getprotobynumber getprotoent getpwent getpwnam getpwuid getservbyname getservbyport getservent getsockname getsockopt given glob gmtime goto grep gt hex if index int ioctl join keys kill last lc lcfirst length link listen local localtime log lstat lt ma map mkdir msgctl msgget msgrcv msgsnd my ne next no not oct open opendir or ord our pack package pipe pop pos print printf prototype push q|0 qq quotemeta qw qx rand read readdir readline readlink readpipe recv redo ref rename require reset return reverse rewinddir rindex rmdir say scalar seek seekdir select semctl semget semop send setgrent sethostent setnetent setpgrp setpriority setprotoent setpwent setservent setsockopt shift shmctl shmget shmread shmwrite shutdown sin sleep socket socketpair sort splice split sprintf sqrt srand stat state study sub substr symlink syscall sysopen sysread sysseek system syswrite tell telldir tie tied time times tr truncate uc ucfirst umask undef unless unlink unpack unshift untie until use utime values vec wait waitpid wantarray warn when while write x|0 xor y|0" +},i={className:"subst",begin:"[$@]\\{",end:"\\}",keywords:a},r={begin:/->\{/, +end:/\}/},s={variants:[{begin:/\$\d/},{ +begin:n.concat(/[$%@](\^\w\b|#\w+(::\w+)*|\{\w+\}|\w+(::\w*)*)/,"(?![A-Za-z])(?![@$%])") +},{begin:/[$%@][^\s\w{]/,relevance:0}] +},o=[e.BACKSLASH_ESCAPE,i,s],l=[/!/,/\//,/\|/,/\?/,/'/,/"/,/#/],c=(e,a,i="\\1")=>{ +const r="\\1"===i?i:n.concat(i,a) +;return n.concat(n.concat("(?:",e,")"),a,/(?:\\.|[^\\\/])*?/,r,/(?:\\.|[^\\\/])*?/,i,t) +},d=(e,a,i)=>n.concat(n.concat("(?:",e,")"),a,/(?:\\.|[^\\\/])*?/,i,t),g=[s,e.HASH_COMMENT_MODE,e.COMMENT(/^=\w/,/=cut/,{ +endsWithParent:!0}),r,{className:"string",contains:o,variants:[{ +begin:"q[qwxr]?\\s*\\(",end:"\\)",relevance:5},{begin:"q[qwxr]?\\s*\\[", +end:"\\]",relevance:5},{begin:"q[qwxr]?\\s*\\{",end:"\\}",relevance:5},{ +begin:"q[qwxr]?\\s*\\|",end:"\\|",relevance:5},{begin:"q[qwxr]?\\s*<",end:">", +relevance:5},{begin:"qw\\s+q",end:"q",relevance:5},{begin:"'",end:"'", +contains:[e.BACKSLASH_ESCAPE]},{begin:'"',end:'"'},{begin:"`",end:"`", +contains:[e.BACKSLASH_ESCAPE]},{begin:/\{\w+\}/,relevance:0},{ +begin:"-?\\w+\\s*=>",relevance:0}]},{className:"number", +begin:"(\\b0[0-7_]+)|(\\b0x[0-9a-fA-F_]+)|(\\b[1-9][0-9_]*(\\.[0-9_]+)?)|[0_]\\b", +relevance:0},{ +begin:"(\\/\\/|"+e.RE_STARTERS_RE+"|\\b(split|return|print|reverse|grep)\\b)\\s*", +keywords:"split return print reverse grep",relevance:0, +contains:[e.HASH_COMMENT_MODE,{className:"regexp",variants:[{ +begin:c("s|tr|y",n.either(...l,{capture:!0}))},{begin:c("s|tr|y","\\(","\\)")},{ +begin:c("s|tr|y","\\[","\\]")},{begin:c("s|tr|y","\\{","\\}")}],relevance:2},{ +className:"regexp",variants:[{begin:/(m|qr)\/\//,relevance:0},{ +begin:d("(?:m|qr)?",/\//,/\//)},{begin:d("m|qr",n.either(...l,{capture:!0 +}),/\1/)},{begin:d("m|qr",/\(/,/\)/)},{begin:d("m|qr",/\[/,/\]/)},{ +begin:d("m|qr",/\{/,/\}/)}]}]},{className:"function",beginKeywords:"sub", +end:"(\\s*\\(.*?\\))?[;{]",excludeEnd:!0,relevance:5,contains:[e.TITLE_MODE]},{ +begin:"-\\w\\b",relevance:0},{begin:"^__DATA__$",end:"^__END__$", +subLanguage:"mojolicious",contains:[{begin:"^@@.*",end:"$",className:"comment"}] +}];return i.contains=g,r.contains=g,{name:"Perl",aliases:["pl","pm"],keywords:a, +contains:g}},grmr_php:e=>{ +const n=e.regex,t=/(?![A-Za-z0-9])(?![$])/,a=n.concat(/[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*/,t),i=n.concat(/(\\?[A-Z][a-z0-9_\x7f-\xff]+|\\?[A-Z]+(?=[A-Z][a-z0-9_\x7f-\xff])){1,}/,t),r={ +scope:"variable",match:"\\$+"+a},s={scope:"subst",variants:[{begin:/\$\w+/},{ +begin:/\{\$/,end:/\}/}]},o=e.inherit(e.APOS_STRING_MODE,{illegal:null +}),l="[ \t\n]",c={scope:"string",variants:[e.inherit(e.QUOTE_STRING_MODE,{ +illegal:null,contains:e.QUOTE_STRING_MODE.contains.concat(s)}),o,{ +begin:/<<<[ \t]*(?:(\w+)|"(\w+)")\n/,end:/[ \t]*(\w+)\b/, +contains:e.QUOTE_STRING_MODE.contains.concat(s),"on:begin":(e,n)=>{ +n.data._beginMatch=e[1]||e[2]},"on:end":(e,n)=>{ +n.data._beginMatch!==e[1]&&n.ignoreMatch()}},e.END_SAME_AS_BEGIN({ +begin:/<<<[ \t]*'(\w+)'\n/,end:/[ \t]*(\w+)\b/})]},d={scope:"number",variants:[{ +begin:"\\b0[bB][01]+(?:_[01]+)*\\b"},{begin:"\\b0[oO][0-7]+(?:_[0-7]+)*\\b"},{ +begin:"\\b0[xX][\\da-fA-F]+(?:_[\\da-fA-F]+)*\\b"},{ +begin:"(?:\\b\\d+(?:_\\d+)*(\\.(?:\\d+(?:_\\d+)*))?|\\B\\.\\d+)(?:[eE][+-]?\\d+)?" +}],relevance:0 +},g=["false","null","true"],u=["__CLASS__","__DIR__","__FILE__","__FUNCTION__","__COMPILER_HALT_OFFSET__","__LINE__","__METHOD__","__NAMESPACE__","__TRAIT__","die","echo","exit","include","include_once","print","require","require_once","array","abstract","and","as","binary","bool","boolean","break","callable","case","catch","class","clone","const","continue","declare","default","do","double","else","elseif","empty","enddeclare","endfor","endforeach","endif","endswitch","endwhile","enum","eval","extends","final","finally","float","for","foreach","from","global","goto","if","implements","instanceof","insteadof","int","integer","interface","isset","iterable","list","match|0","mixed","new","never","object","or","private","protected","public","readonly","real","return","string","switch","throw","trait","try","unset","use","var","void","while","xor","yield"],b=["Error|0","AppendIterator","ArgumentCountError","ArithmeticError","ArrayIterator","ArrayObject","AssertionError","BadFunctionCallException","BadMethodCallException","CachingIterator","CallbackFilterIterator","CompileError","Countable","DirectoryIterator","DivisionByZeroError","DomainException","EmptyIterator","ErrorException","Exception","FilesystemIterator","FilterIterator","GlobIterator","InfiniteIterator","InvalidArgumentException","IteratorIterator","LengthException","LimitIterator","LogicException","MultipleIterator","NoRewindIterator","OutOfBoundsException","OutOfRangeException","OuterIterator","OverflowException","ParentIterator","ParseError","RangeException","RecursiveArrayIterator","RecursiveCachingIterator","RecursiveCallbackFilterIterator","RecursiveDirectoryIterator","RecursiveFilterIterator","RecursiveIterator","RecursiveIteratorIterator","RecursiveRegexIterator","RecursiveTreeIterator","RegexIterator","RuntimeException","SeekableIterator","SplDoublyLinkedList","SplFileInfo","SplFileObject","SplFixedArray","SplHeap","SplMaxHeap","SplMinHeap","SplObjectStorage","SplObserver","SplPriorityQueue","SplQueue","SplStack","SplSubject","SplTempFileObject","TypeError","UnderflowException","UnexpectedValueException","UnhandledMatchError","ArrayAccess","BackedEnum","Closure","Fiber","Generator","Iterator","IteratorAggregate","Serializable","Stringable","Throwable","Traversable","UnitEnum","WeakReference","WeakMap","Directory","__PHP_Incomplete_Class","parent","php_user_filter","self","static","stdClass"],m={ +keyword:u,literal:(e=>{const n=[];return e.forEach((e=>{ +n.push(e),e.toLowerCase()===e?n.push(e.toUpperCase()):n.push(e.toLowerCase()) +})),n})(g),built_in:b},p=e=>e.map((e=>e.replace(/\|\d+$/,""))),_={variants:[{ +match:[/new/,n.concat(l,"+"),n.concat("(?!",p(b).join("\\b|"),"\\b)"),i],scope:{ +1:"keyword",4:"title.class"}}]},h=n.concat(a,"\\b(?!\\()"),f={variants:[{ +match:[n.concat(/::/,n.lookahead(/(?!class\b)/)),h],scope:{2:"variable.constant" +}},{match:[/::/,/class/],scope:{2:"variable.language"}},{ +match:[i,n.concat(/::/,n.lookahead(/(?!class\b)/)),h],scope:{1:"title.class", +3:"variable.constant"}},{match:[i,n.concat("::",n.lookahead(/(?!class\b)/))], +scope:{1:"title.class"}},{match:[i,/::/,/class/],scope:{1:"title.class", +3:"variable.language"}}]},E={scope:"attr", +match:n.concat(a,n.lookahead(":"),n.lookahead(/(?!::)/))},y={relevance:0, +begin:/\(/,end:/\)/,keywords:m,contains:[E,r,f,e.C_BLOCK_COMMENT_MODE,c,d,_] +},N={relevance:0, +match:[/\b/,n.concat("(?!fn\\b|function\\b|",p(u).join("\\b|"),"|",p(b).join("\\b|"),"\\b)"),a,n.concat(l,"*"),n.lookahead(/(?=\()/)], +scope:{3:"title.function.invoke"},contains:[y]};y.contains.push(N) +;const w=[E,f,e.C_BLOCK_COMMENT_MODE,c,d,_];return{case_insensitive:!1, +keywords:m,contains:[{begin:n.concat(/#\[\s*/,i),beginScope:"meta",end:/]/, +endScope:"meta",keywords:{literal:g,keyword:["new","array"]},contains:[{ +begin:/\[/,end:/]/,keywords:{literal:g,keyword:["new","array"]}, +contains:["self",...w]},...w,{scope:"meta",match:i}] +},e.HASH_COMMENT_MODE,e.COMMENT("//","$"),e.COMMENT("/\\*","\\*/",{contains:[{ +scope:"doctag",match:"@[A-Za-z]+"}]}),{match:/__halt_compiler\(\);/, +keywords:"__halt_compiler",starts:{scope:"comment",end:e.MATCH_NOTHING_RE, +contains:[{match:/\?>/,scope:"meta",endsParent:!0}]}},{scope:"meta",variants:[{ +begin:/<\?php/,relevance:10},{begin:/<\?=/},{begin:/<\?/,relevance:.1},{ +begin:/\?>/}]},{scope:"variable.language",match:/\$this\b/},r,N,f,{ +match:[/const/,/\s/,a],scope:{1:"keyword",3:"variable.constant"}},_,{ +scope:"function",relevance:0,beginKeywords:"fn function",end:/[;{]/, +excludeEnd:!0,illegal:"[$%\\[]",contains:[{beginKeywords:"use" +},e.UNDERSCORE_TITLE_MODE,{begin:"=>",endsParent:!0},{scope:"params", +begin:"\\(",end:"\\)",excludeBegin:!0,excludeEnd:!0,keywords:m, +contains:["self",r,f,e.C_BLOCK_COMMENT_MODE,c,d]}]},{scope:"class",variants:[{ +beginKeywords:"enum",illegal:/[($"]/},{beginKeywords:"class interface trait", +illegal:/[:($"]/}],relevance:0,end:/\{/,excludeEnd:!0,contains:[{ +beginKeywords:"extends implements"},e.UNDERSCORE_TITLE_MODE]},{ +beginKeywords:"namespace",relevance:0,end:";",illegal:/[.']/, +contains:[e.inherit(e.UNDERSCORE_TITLE_MODE,{scope:"title.class"})]},{ +beginKeywords:"use",relevance:0,end:";",contains:[{ +match:/\b(as|const|function)\b/,scope:"keyword"},e.UNDERSCORE_TITLE_MODE]},c,d]} +},grmr_php_template:e=>({name:"PHP template",subLanguage:"xml",contains:[{ +begin:/<\?(php|=)?/,end:/\?>/,subLanguage:"php",contains:[{begin:"/\\*", +end:"\\*/",skip:!0},{begin:'b"',end:'"',skip:!0},{begin:"b'",end:"'",skip:!0 +},e.inherit(e.APOS_STRING_MODE,{illegal:null,className:null,contains:null, +skip:!0}),e.inherit(e.QUOTE_STRING_MODE,{illegal:null,className:null, +contains:null,skip:!0})]}]}),grmr_plaintext:e=>({name:"Plain text", +aliases:["text","txt"],disableAutodetect:!0}),grmr_python:e=>{ +const n=e.regex,t=/[\p{XID_Start}_]\p{XID_Continue}*/u,a=["and","as","assert","async","await","break","case","class","continue","def","del","elif","else","except","finally","for","from","global","if","import","in","is","lambda","match","nonlocal|10","not","or","pass","raise","return","try","while","with","yield"],i={ +$pattern:/[A-Za-z]\w+|__\w+__/,keyword:a, +built_in:["__import__","abs","all","any","ascii","bin","bool","breakpoint","bytearray","bytes","callable","chr","classmethod","compile","complex","delattr","dict","dir","divmod","enumerate","eval","exec","filter","float","format","frozenset","getattr","globals","hasattr","hash","help","hex","id","input","int","isinstance","issubclass","iter","len","list","locals","map","max","memoryview","min","next","object","oct","open","ord","pow","print","property","range","repr","reversed","round","set","setattr","slice","sorted","staticmethod","str","sum","super","tuple","type","vars","zip"], +literal:["__debug__","Ellipsis","False","None","NotImplemented","True"], +type:["Any","Callable","Coroutine","Dict","List","Literal","Generic","Optional","Sequence","Set","Tuple","Type","Union"] +},r={className:"meta",begin:/^(>>>|\.\.\.) /},s={className:"subst",begin:/\{/, +end:/\}/,keywords:i,illegal:/#/},o={begin:/\{\{/,relevance:0},l={ +className:"string",contains:[e.BACKSLASH_ESCAPE],variants:[{ +begin:/([uU]|[bB]|[rR]|[bB][rR]|[rR][bB])?'''/,end:/'''/, +contains:[e.BACKSLASH_ESCAPE,r],relevance:10},{ +begin:/([uU]|[bB]|[rR]|[bB][rR]|[rR][bB])?"""/,end:/"""/, +contains:[e.BACKSLASH_ESCAPE,r],relevance:10},{ +begin:/([fF][rR]|[rR][fF]|[fF])'''/,end:/'''/, +contains:[e.BACKSLASH_ESCAPE,r,o,s]},{begin:/([fF][rR]|[rR][fF]|[fF])"""/, +end:/"""/,contains:[e.BACKSLASH_ESCAPE,r,o,s]},{begin:/([uU]|[rR])'/,end:/'/, +relevance:10},{begin:/([uU]|[rR])"/,end:/"/,relevance:10},{ +begin:/([bB]|[bB][rR]|[rR][bB])'/,end:/'/},{begin:/([bB]|[bB][rR]|[rR][bB])"/, +end:/"/},{begin:/([fF][rR]|[rR][fF]|[fF])'/,end:/'/, +contains:[e.BACKSLASH_ESCAPE,o,s]},{begin:/([fF][rR]|[rR][fF]|[fF])"/,end:/"/, +contains:[e.BACKSLASH_ESCAPE,o,s]},e.APOS_STRING_MODE,e.QUOTE_STRING_MODE] +},c="[0-9](_?[0-9])*",d=`(\\b(${c}))?\\.(${c})|\\b(${c})\\.`,g="\\b|"+a.join("|"),u={ +className:"number",relevance:0,variants:[{ +begin:`(\\b(${c})|(${d}))[eE][+-]?(${c})[jJ]?(?=${g})`},{begin:`(${d})[jJ]?`},{ +begin:`\\b([1-9](_?[0-9])*|0+(_?0)*)[lLjJ]?(?=${g})`},{ +begin:`\\b0[bB](_?[01])+[lL]?(?=${g})`},{begin:`\\b0[oO](_?[0-7])+[lL]?(?=${g})` +},{begin:`\\b0[xX](_?[0-9a-fA-F])+[lL]?(?=${g})`},{begin:`\\b(${c})[jJ](?=${g})` +}]},b={className:"comment",begin:n.lookahead(/# type:/),end:/$/,keywords:i, +contains:[{begin:/# type:/},{begin:/#/,end:/\b\B/,endsWithParent:!0}]},m={ +className:"params",variants:[{className:"",begin:/\(\s*\)/,skip:!0},{begin:/\(/, +end:/\)/,excludeBegin:!0,excludeEnd:!0,keywords:i, +contains:["self",r,u,l,e.HASH_COMMENT_MODE]}]};return s.contains=[l,u,r],{ +name:"Python",aliases:["py","gyp","ipython"],unicodeRegex:!0,keywords:i, +illegal:/(<\/|\?)|=>/,contains:[r,u,{begin:/\bself\b/},{beginKeywords:"if", +relevance:0},l,b,e.HASH_COMMENT_MODE,{match:[/\bdef/,/\s+/,t],scope:{ +1:"keyword",3:"title.function"},contains:[m]},{variants:[{ +match:[/\bclass/,/\s+/,t,/\s*/,/\(\s*/,t,/\s*\)/]},{match:[/\bclass/,/\s+/,t]}], +scope:{1:"keyword",3:"title.class",6:"title.class.inherited"}},{ +className:"meta",begin:/^[\t ]*@/,end:/(?=#)|$/,contains:[u,m,l]}]}}, +grmr_python_repl:e=>({aliases:["pycon"],contains:[{className:"meta.prompt", +starts:{end:/ |$/,starts:{end:"$",subLanguage:"python"}},variants:[{ +begin:/^>>>(?=[ ]|$)/},{begin:/^\.\.\.(?=[ ]|$)/}]}]}),grmr_r:e=>{ +const n=e.regex,t=/(?:(?:[a-zA-Z]|\.[._a-zA-Z])[._a-zA-Z0-9]*)|\.(?!\d)/,a=n.either(/0[xX][0-9a-fA-F]+\.[0-9a-fA-F]*[pP][+-]?\d+i?/,/0[xX][0-9a-fA-F]+(?:[pP][+-]?\d+)?[Li]?/,/(?:\d+(?:\.\d*)?|\.\d+)(?:[eE][+-]?\d+)?[Li]?/),i=/[=!<>:]=|\|\||&&|:::?|<-|<<-|->>|->|\|>|[-+*\/?!$&|:<=>@^~]|\*\*/,r=n.either(/[()]/,/[{}]/,/\[\[/,/[[\]]/,/\\/,/,/) +;return{name:"R",keywords:{$pattern:t, +keyword:"function if in break next repeat else for while", +literal:"NULL NA TRUE FALSE Inf NaN NA_integer_|10 NA_real_|10 NA_character_|10 NA_complex_|10", +built_in:"LETTERS letters month.abb month.name pi T F abs acos acosh all any anyNA Arg as.call as.character as.complex as.double as.environment as.integer as.logical as.null.default as.numeric as.raw asin asinh atan atanh attr attributes baseenv browser c call ceiling class Conj cos cosh cospi cummax cummin cumprod cumsum digamma dim dimnames emptyenv exp expression floor forceAndCall gamma gc.time globalenv Im interactive invisible is.array is.atomic is.call is.character is.complex is.double is.environment is.expression is.finite is.function is.infinite is.integer is.language is.list is.logical is.matrix is.na is.name is.nan is.null is.numeric is.object is.pairlist is.raw is.recursive is.single is.symbol lazyLoadDBfetch length lgamma list log max min missing Mod names nargs nzchar oldClass on.exit pos.to.env proc.time prod quote range Re rep retracemem return round seq_along seq_len seq.int sign signif sin sinh sinpi sqrt standardGeneric substitute sum switch tan tanh tanpi tracemem trigamma trunc unclass untracemem UseMethod xtfrm" +},contains:[e.COMMENT(/#'/,/$/,{contains:[{scope:"doctag",match:/@examples/, +starts:{end:n.lookahead(n.either(/\n^#'\s*(?=@[a-zA-Z]+)/,/\n^(?!#')/)), +endsParent:!0}},{scope:"doctag",begin:"@param",end:/$/,contains:[{ +scope:"variable",variants:[{match:t},{match:/`(?:\\.|[^`\\])+`/}],endsParent:!0 +}]},{scope:"doctag",match:/@[a-zA-Z]+/},{scope:"keyword",match:/\\[a-zA-Z]+/}] +}),e.HASH_COMMENT_MODE,{scope:"string",contains:[e.BACKSLASH_ESCAPE], +variants:[e.END_SAME_AS_BEGIN({begin:/[rR]"(-*)\(/,end:/\)(-*)"/ +}),e.END_SAME_AS_BEGIN({begin:/[rR]"(-*)\{/,end:/\}(-*)"/ +}),e.END_SAME_AS_BEGIN({begin:/[rR]"(-*)\[/,end:/\](-*)"/ +}),e.END_SAME_AS_BEGIN({begin:/[rR]'(-*)\(/,end:/\)(-*)'/ +}),e.END_SAME_AS_BEGIN({begin:/[rR]'(-*)\{/,end:/\}(-*)'/ +}),e.END_SAME_AS_BEGIN({begin:/[rR]'(-*)\[/,end:/\](-*)'/}),{begin:'"',end:'"', +relevance:0},{begin:"'",end:"'",relevance:0}]},{relevance:0,variants:[{scope:{ +1:"operator",2:"number"},match:[i,a]},{scope:{1:"operator",2:"number"}, +match:[/%[^%]*%/,a]},{scope:{1:"punctuation",2:"number"},match:[r,a]},{scope:{ +2:"number"},match:[/[^a-zA-Z0-9._]|^/,a]}]},{scope:{3:"operator"}, +match:[t,/\s+/,/<-/,/\s+/]},{scope:"operator",relevance:0,variants:[{match:i},{ +match:/%[^%]*%/}]},{scope:"punctuation",relevance:0,match:r},{begin:"`",end:"`", +contains:[{begin:/\\./}]}]}},grmr_ruby:e=>{ +const n=e.regex,t="([a-zA-Z_]\\w*[!?=]?|[-+~]@|<<|>>|=~|===?|<=>|[<>]=?|\\*\\*|[-/+%^&*~`|]|\\[\\]=?)",a=n.either(/\b([A-Z]+[a-z0-9]+)+/,/\b([A-Z]+[a-z0-9]+)+[A-Z]+/),i=n.concat(a,/(::\w+)*/),r={ +"variable.constant":["__FILE__","__LINE__","__ENCODING__"], +"variable.language":["self","super"], +keyword:["alias","and","begin","BEGIN","break","case","class","defined","do","else","elsif","end","END","ensure","for","if","in","module","next","not","or","redo","require","rescue","retry","return","then","undef","unless","until","when","while","yield","include","extend","prepend","public","private","protected","raise","throw"], +built_in:["proc","lambda","attr_accessor","attr_reader","attr_writer","define_method","private_constant","module_function"], +literal:["true","false","nil"]},s={className:"doctag",begin:"@[A-Za-z]+"},o={ +begin:"#<",end:">"},l=[e.COMMENT("#","$",{contains:[s] +}),e.COMMENT("^=begin","^=end",{contains:[s],relevance:10 +}),e.COMMENT("^__END__",e.MATCH_NOTHING_RE)],c={className:"subst",begin:/#\{/, +end:/\}/,keywords:r},d={className:"string",contains:[e.BACKSLASH_ESCAPE,c], +variants:[{begin:/'/,end:/'/},{begin:/"/,end:/"/},{begin:/`/,end:/`/},{ +begin:/%[qQwWx]?\(/,end:/\)/},{begin:/%[qQwWx]?\[/,end:/\]/},{ +begin:/%[qQwWx]?\{/,end:/\}/},{begin:/%[qQwWx]?/},{begin:/%[qQwWx]?\//, +end:/\//},{begin:/%[qQwWx]?%/,end:/%/},{begin:/%[qQwWx]?-/,end:/-/},{ +begin:/%[qQwWx]?\|/,end:/\|/},{begin:/\B\?(\\\d{1,3})/},{ +begin:/\B\?(\\x[A-Fa-f0-9]{1,2})/},{begin:/\B\?(\\u\{?[A-Fa-f0-9]{1,6}\}?)/},{ +begin:/\B\?(\\M-\\C-|\\M-\\c|\\c\\M-|\\M-|\\C-\\M-)[\x20-\x7e]/},{ +begin:/\B\?\\(c|C-)[\x20-\x7e]/},{begin:/\B\?\\?\S/},{ +begin:n.concat(/<<[-~]?'?/,n.lookahead(/(\w+)(?=\W)[^\n]*\n(?:[^\n]*\n)*?\s*\1\b/)), +contains:[e.END_SAME_AS_BEGIN({begin:/(\w+)/,end:/(\w+)/, +contains:[e.BACKSLASH_ESCAPE,c]})]}]},g="[0-9](_?[0-9])*",u={className:"number", +relevance:0,variants:[{ +begin:`\\b([1-9](_?[0-9])*|0)(\\.(${g}))?([eE][+-]?(${g})|r)?i?\\b`},{ +begin:"\\b0[dD][0-9](_?[0-9])*r?i?\\b"},{begin:"\\b0[bB][0-1](_?[0-1])*r?i?\\b" +},{begin:"\\b0[oO][0-7](_?[0-7])*r?i?\\b"},{ +begin:"\\b0[xX][0-9a-fA-F](_?[0-9a-fA-F])*r?i?\\b"},{ +begin:"\\b0(_?[0-7])+r?i?\\b"}]},b={variants:[{match:/\(\)/},{ +className:"params",begin:/\(/,end:/(?=\))/,excludeBegin:!0,endsParent:!0, +keywords:r}]},m=[d,{variants:[{match:[/class\s+/,i,/\s+<\s+/,i]},{ +match:[/\b(class|module)\s+/,i]}],scope:{2:"title.class", +4:"title.class.inherited"},keywords:r},{match:[/(include|extend)\s+/,i],scope:{ +2:"title.class"},keywords:r},{relevance:0,match:[i,/\.new[. (]/],scope:{ +1:"title.class"}},{relevance:0,match:/\b[A-Z][A-Z_0-9]+\b/, +className:"variable.constant"},{relevance:0,match:a,scope:"title.class"},{ +match:[/def/,/\s+/,t],scope:{1:"keyword",3:"title.function"},contains:[b]},{ +begin:e.IDENT_RE+"::"},{className:"symbol", +begin:e.UNDERSCORE_IDENT_RE+"(!|\\?)?:",relevance:0},{className:"symbol", +begin:":(?!\\s)",contains:[d,{begin:t}],relevance:0},u,{className:"variable", +begin:"(\\$\\W)|((\\$|@@?)(\\w+))(?=[^@$?])(?![A-Za-z])(?![@$?'])"},{ +className:"params",begin:/\|/,end:/\|/,excludeBegin:!0,excludeEnd:!0, +relevance:0,keywords:r},{begin:"("+e.RE_STARTERS_RE+"|unless)\\s*", +keywords:"unless",contains:[{className:"regexp",contains:[e.BACKSLASH_ESCAPE,c], +illegal:/\n/,variants:[{begin:"/",end:"/[a-z]*"},{begin:/%r\{/,end:/\}[a-z]*/},{ +begin:"%r\\(",end:"\\)[a-z]*"},{begin:"%r!",end:"![a-z]*"},{begin:"%r\\[", +end:"\\][a-z]*"}]}].concat(o,l),relevance:0}].concat(o,l) +;c.contains=m,b.contains=m;const p=[{begin:/^\s*=>/,starts:{end:"$",contains:m} +},{className:"meta.prompt", +begin:"^([>?]>|[\\w#]+\\(\\w+\\):\\d+:\\d+[>*]|(\\w+-)?\\d+\\.\\d+\\.\\d+(p\\d+)?[^\\d][^>]+>)(?=[ ])", +starts:{end:"$",keywords:r,contains:m}}];return l.unshift(o),{name:"Ruby", +aliases:["rb","gemspec","podspec","thor","irb"],keywords:r,illegal:/\/\*/, +contains:[e.SHEBANG({binary:"ruby"})].concat(p).concat(l).concat(m)}}, +grmr_rust:e=>{const n=e.regex,t={className:"title.function.invoke",relevance:0, +begin:n.concat(/\b/,/(?!let|for|while|if|else|match\b)/,e.IDENT_RE,n.lookahead(/\s*\(/)) +},a="([ui](8|16|32|64|128|size)|f(32|64))?",i=["drop ","Copy","Send","Sized","Sync","Drop","Fn","FnMut","FnOnce","ToOwned","Clone","Debug","PartialEq","PartialOrd","Eq","Ord","AsRef","AsMut","Into","From","Default","Iterator","Extend","IntoIterator","DoubleEndedIterator","ExactSizeIterator","SliceConcatExt","ToString","assert!","assert_eq!","bitflags!","bytes!","cfg!","col!","concat!","concat_idents!","debug_assert!","debug_assert_eq!","env!","eprintln!","panic!","file!","format!","format_args!","include_bytes!","include_str!","line!","local_data_key!","module_path!","option_env!","print!","println!","select!","stringify!","try!","unimplemented!","unreachable!","vec!","write!","writeln!","macro_rules!","assert_ne!","debug_assert_ne!"],r=["i8","i16","i32","i64","i128","isize","u8","u16","u32","u64","u128","usize","f32","f64","str","char","bool","Box","Option","Result","String","Vec"] +;return{name:"Rust",aliases:["rs"],keywords:{$pattern:e.IDENT_RE+"!?",type:r, +keyword:["abstract","as","async","await","become","box","break","const","continue","crate","do","dyn","else","enum","extern","false","final","fn","for","if","impl","in","let","loop","macro","match","mod","move","mut","override","priv","pub","ref","return","self","Self","static","struct","super","trait","true","try","type","typeof","unsafe","unsized","use","virtual","where","while","yield"], +literal:["true","false","Some","None","Ok","Err"],built_in:i},illegal:""},t]}}, +grmr_scss:e=>{const n=ie(e),t=le,a=oe,i="@[a-z-]+",r={className:"variable", +begin:"(\\$[a-zA-Z-][a-zA-Z0-9_-]*)\\b",relevance:0};return{name:"SCSS", +case_insensitive:!0,illegal:"[=/|']", +contains:[e.C_LINE_COMMENT_MODE,e.C_BLOCK_COMMENT_MODE,n.CSS_NUMBER_MODE,{ +className:"selector-id",begin:"#[A-Za-z0-9_-]+",relevance:0},{ +className:"selector-class",begin:"\\.[A-Za-z0-9_-]+",relevance:0 +},n.ATTRIBUTE_SELECTOR_MODE,{className:"selector-tag", +begin:"\\b("+re.join("|")+")\\b",relevance:0},{className:"selector-pseudo", +begin:":("+a.join("|")+")"},{className:"selector-pseudo", +begin:":(:)?("+t.join("|")+")"},r,{begin:/\(/,end:/\)/, +contains:[n.CSS_NUMBER_MODE]},n.CSS_VARIABLE,{className:"attribute", +begin:"\\b("+ce.join("|")+")\\b"},{ +begin:"\\b(whitespace|wait|w-resize|visible|vertical-text|vertical-ideographic|uppercase|upper-roman|upper-alpha|underline|transparent|top|thin|thick|text|text-top|text-bottom|tb-rl|table-header-group|table-footer-group|sw-resize|super|strict|static|square|solid|small-caps|separate|se-resize|scroll|s-resize|rtl|row-resize|ridge|right|repeat|repeat-y|repeat-x|relative|progress|pointer|overline|outside|outset|oblique|nowrap|not-allowed|normal|none|nw-resize|no-repeat|no-drop|newspaper|ne-resize|n-resize|move|middle|medium|ltr|lr-tb|lowercase|lower-roman|lower-alpha|loose|list-item|line|line-through|line-edge|lighter|left|keep-all|justify|italic|inter-word|inter-ideograph|inside|inset|inline|inline-block|inherit|inactive|ideograph-space|ideograph-parenthesis|ideograph-numeric|ideograph-alpha|horizontal|hidden|help|hand|groove|fixed|ellipsis|e-resize|double|dotted|distribute|distribute-space|distribute-letter|distribute-all-lines|disc|disabled|default|decimal|dashed|crosshair|collapse|col-resize|circle|char|center|capitalize|break-word|break-all|bottom|both|bolder|bold|block|bidi-override|below|baseline|auto|always|all-scroll|absolute|table|table-cell)\\b" +},{begin:/:/,end:/[;}{]/,relevance:0, +contains:[n.BLOCK_COMMENT,r,n.HEXCOLOR,n.CSS_NUMBER_MODE,e.QUOTE_STRING_MODE,e.APOS_STRING_MODE,n.IMPORTANT,n.FUNCTION_DISPATCH] +},{begin:"@(page|font-face)",keywords:{$pattern:i,keyword:"@page @font-face"}},{ +begin:"@",end:"[{;]",returnBegin:!0,keywords:{$pattern:/[a-z-]+/, +keyword:"and or not only",attribute:se.join(" ")},contains:[{begin:i, +className:"keyword"},{begin:/[a-z-]+(?=:)/,className:"attribute" +},r,e.QUOTE_STRING_MODE,e.APOS_STRING_MODE,n.HEXCOLOR,n.CSS_NUMBER_MODE] +},n.FUNCTION_DISPATCH]}},grmr_shell:e=>({name:"Shell Session", +aliases:["console","shellsession"],contains:[{className:"meta.prompt", +begin:/^\s{0,3}[/~\w\d[\]()@-]*[>%$#][ ]?/,starts:{end:/[^\\](?=\s*$)/, +subLanguage:"bash"}}]}),grmr_sql:e=>{ +const n=e.regex,t=e.COMMENT("--","$"),a=["true","false","unknown"],i=["bigint","binary","blob","boolean","char","character","clob","date","dec","decfloat","decimal","float","int","integer","interval","nchar","nclob","national","numeric","real","row","smallint","time","timestamp","varchar","varying","varbinary"],r=["abs","acos","array_agg","asin","atan","avg","cast","ceil","ceiling","coalesce","corr","cos","cosh","count","covar_pop","covar_samp","cume_dist","dense_rank","deref","element","exp","extract","first_value","floor","json_array","json_arrayagg","json_exists","json_object","json_objectagg","json_query","json_table","json_table_primitive","json_value","lag","last_value","lead","listagg","ln","log","log10","lower","max","min","mod","nth_value","ntile","nullif","percent_rank","percentile_cont","percentile_disc","position","position_regex","power","rank","regr_avgx","regr_avgy","regr_count","regr_intercept","regr_r2","regr_slope","regr_sxx","regr_sxy","regr_syy","row_number","sin","sinh","sqrt","stddev_pop","stddev_samp","substring","substring_regex","sum","tan","tanh","translate","translate_regex","treat","trim","trim_array","unnest","upper","value_of","var_pop","var_samp","width_bucket"],s=["create table","insert into","primary key","foreign key","not null","alter table","add constraint","grouping sets","on overflow","character set","respect nulls","ignore nulls","nulls first","nulls last","depth first","breadth first"],o=r,l=["abs","acos","all","allocate","alter","and","any","are","array","array_agg","array_max_cardinality","as","asensitive","asin","asymmetric","at","atan","atomic","authorization","avg","begin","begin_frame","begin_partition","between","bigint","binary","blob","boolean","both","by","call","called","cardinality","cascaded","case","cast","ceil","ceiling","char","char_length","character","character_length","check","classifier","clob","close","coalesce","collate","collect","column","commit","condition","connect","constraint","contains","convert","copy","corr","corresponding","cos","cosh","count","covar_pop","covar_samp","create","cross","cube","cume_dist","current","current_catalog","current_date","current_default_transform_group","current_path","current_role","current_row","current_schema","current_time","current_timestamp","current_path","current_role","current_transform_group_for_type","current_user","cursor","cycle","date","day","deallocate","dec","decimal","decfloat","declare","default","define","delete","dense_rank","deref","describe","deterministic","disconnect","distinct","double","drop","dynamic","each","element","else","empty","end","end_frame","end_partition","end-exec","equals","escape","every","except","exec","execute","exists","exp","external","extract","false","fetch","filter","first_value","float","floor","for","foreign","frame_row","free","from","full","function","fusion","get","global","grant","group","grouping","groups","having","hold","hour","identity","in","indicator","initial","inner","inout","insensitive","insert","int","integer","intersect","intersection","interval","into","is","join","json_array","json_arrayagg","json_exists","json_object","json_objectagg","json_query","json_table","json_table_primitive","json_value","lag","language","large","last_value","lateral","lead","leading","left","like","like_regex","listagg","ln","local","localtime","localtimestamp","log","log10","lower","match","match_number","match_recognize","matches","max","member","merge","method","min","minute","mod","modifies","module","month","multiset","national","natural","nchar","nclob","new","no","none","normalize","not","nth_value","ntile","null","nullif","numeric","octet_length","occurrences_regex","of","offset","old","omit","on","one","only","open","or","order","out","outer","over","overlaps","overlay","parameter","partition","pattern","per","percent","percent_rank","percentile_cont","percentile_disc","period","portion","position","position_regex","power","precedes","precision","prepare","primary","procedure","ptf","range","rank","reads","real","recursive","ref","references","referencing","regr_avgx","regr_avgy","regr_count","regr_intercept","regr_r2","regr_slope","regr_sxx","regr_sxy","regr_syy","release","result","return","returns","revoke","right","rollback","rollup","row","row_number","rows","running","savepoint","scope","scroll","search","second","seek","select","sensitive","session_user","set","show","similar","sin","sinh","skip","smallint","some","specific","specifictype","sql","sqlexception","sqlstate","sqlwarning","sqrt","start","static","stddev_pop","stddev_samp","submultiset","subset","substring","substring_regex","succeeds","sum","symmetric","system","system_time","system_user","table","tablesample","tan","tanh","then","time","timestamp","timezone_hour","timezone_minute","to","trailing","translate","translate_regex","translation","treat","trigger","trim","trim_array","true","truncate","uescape","union","unique","unknown","unnest","update","upper","user","using","value","values","value_of","var_pop","var_samp","varbinary","varchar","varying","versioning","when","whenever","where","width_bucket","window","with","within","without","year","add","asc","collation","desc","final","first","last","view"].filter((e=>!r.includes(e))),c={ +begin:n.concat(/\b/,n.either(...o),/\s*\(/),relevance:0,keywords:{built_in:o}} +;return{name:"SQL",case_insensitive:!0,illegal:/[{}]|<\//,keywords:{ +$pattern:/\b[\w\.]+/,keyword:((e,{exceptions:n,when:t}={})=>{const a=t +;return n=n||[],e.map((e=>e.match(/\|\d+$/)||n.includes(e)?e:a(e)?e+"|0":e)) +})(l,{when:e=>e.length<3}),literal:a,type:i, +built_in:["current_catalog","current_date","current_default_transform_group","current_path","current_role","current_schema","current_transform_group_for_type","current_user","session_user","system_time","system_user","current_time","localtime","current_timestamp","localtimestamp"] +},contains:[{begin:n.either(...s),relevance:0,keywords:{$pattern:/[\w\.]+/, +keyword:l.concat(s),literal:a,type:i}},{className:"type", +begin:n.either("double precision","large object","with timezone","without timezone") +},c,{className:"variable",begin:/@[a-z0-9][a-z0-9_]*/},{className:"string", +variants:[{begin:/'/,end:/'/,contains:[{begin:/''/}]}]},{begin:/"/,end:/"/, +contains:[{begin:/""/}]},e.C_NUMBER_MODE,e.C_BLOCK_COMMENT_MODE,t,{ +className:"operator",begin:/[-+*/=%^~]|&&?|\|\|?|!=?|<(?:=>?|<|>)?|>[>=]?/, +relevance:0}]}},grmr_swift:e=>{const n={match:/\s+/,relevance:0 +},t=e.COMMENT("/\\*","\\*/",{contains:["self"]}),a=[e.C_LINE_COMMENT_MODE,t],i={ +match:[/\./,m(...xe,...Me)],className:{2:"keyword"}},r={match:b(/\./,m(...Ae)), +relevance:0},s=Ae.filter((e=>"string"==typeof e)).concat(["_|0"]),o={variants:[{ +className:"keyword", +match:m(...Ae.filter((e=>"string"!=typeof e)).concat(Se).map(ke),...Me)}]},l={ +$pattern:m(/\b\w+/,/#\w+/),keyword:s.concat(Re),literal:Ce},c=[i,r,o],g=[{ +match:b(/\./,m(...De)),relevance:0},{className:"built_in", +match:b(/\b/,m(...De),/(?=\()/)}],u={match:/->/,relevance:0},p=[u,{ +className:"operator",relevance:0,variants:[{match:Be},{match:`\\.(\\.|${Le})+`}] +}],_="([0-9]_*)+",h="([0-9a-fA-F]_*)+",f={className:"number",relevance:0, +variants:[{match:`\\b(${_})(\\.(${_}))?([eE][+-]?(${_}))?\\b`},{ +match:`\\b0x(${h})(\\.(${h}))?([pP][+-]?(${_}))?\\b`},{match:/\b0o([0-7]_*)+\b/ +},{match:/\b0b([01]_*)+\b/}]},E=(e="")=>({className:"subst",variants:[{ +match:b(/\\/,e,/[0\\tnr"']/)},{match:b(/\\/,e,/u\{[0-9a-fA-F]{1,8}\}/)}] +}),y=(e="")=>({className:"subst",match:b(/\\/,e,/[\t ]*(?:[\r\n]|\r\n)/) +}),N=(e="")=>({className:"subst",label:"interpol",begin:b(/\\/,e,/\(/),end:/\)/ +}),w=(e="")=>({begin:b(e,/"""/),end:b(/"""/,e),contains:[E(e),y(e),N(e)] +}),v=(e="")=>({begin:b(e,/"/),end:b(/"/,e),contains:[E(e),N(e)]}),O={ +className:"string", +variants:[w(),w("#"),w("##"),w("###"),v(),v("#"),v("##"),v("###")] +},k=[e.BACKSLASH_ESCAPE,{begin:/\[/,end:/\]/,relevance:0, +contains:[e.BACKSLASH_ESCAPE]}],x={begin:/\/[^\s](?=[^/\n]*\/)/,end:/\//, +contains:k},M=e=>{const n=b(e,/\//),t=b(/\//,e);return{begin:n,end:t, +contains:[...k,{scope:"comment",begin:`#(?!.*${t})`,end:/$/}]}},S={ +scope:"regexp",variants:[M("###"),M("##"),M("#"),x]},A={match:b(/`/,Fe,/`/) +},C=[A,{className:"variable",match:/\$\d+/},{className:"variable", +match:`\\$${ze}+`}],T=[{match:/(@|#(un)?)available/,scope:"keyword",starts:{ +contains:[{begin:/\(/,end:/\)/,keywords:Pe,contains:[...p,f,O]}]}},{ +scope:"keyword",match:b(/@/,m(...je))},{scope:"meta",match:b(/@/,Fe)}],R={ +match:d(/\b[A-Z]/),relevance:0,contains:[{className:"type", +match:b(/(AV|CA|CF|CG|CI|CL|CM|CN|CT|MK|MP|MTK|MTL|NS|SCN|SK|UI|WK|XC)/,ze,"+") +},{className:"type",match:Ue,relevance:0},{match:/[?!]+/,relevance:0},{ +match:/\.\.\./,relevance:0},{match:b(/\s+&\s+/,d(Ue)),relevance:0}]},D={ +begin://,keywords:l,contains:[...a,...c,...T,u,R]};R.contains.push(D) +;const I={begin:/\(/,end:/\)/,relevance:0,keywords:l,contains:["self",{ +match:b(Fe,/\s*:/),keywords:"_|0",relevance:0 +},...a,S,...c,...g,...p,f,O,...C,...T,R]},L={begin://, +keywords:"repeat each",contains:[...a,R]},B={begin:/\(/,end:/\)/,keywords:l, +contains:[{begin:m(d(b(Fe,/\s*:/)),d(b(Fe,/\s+/,Fe,/\s*:/))),end:/:/, +relevance:0,contains:[{className:"keyword",match:/\b_\b/},{className:"params", +match:Fe}]},...a,...c,...p,f,O,...T,R,I],endsParent:!0,illegal:/["']/},$={ +match:[/(func|macro)/,/\s+/,m(A.match,Fe,Be)],className:{1:"keyword", +3:"title.function"},contains:[L,B,n],illegal:[/\[/,/%/]},z={ +match:[/\b(?:subscript|init[?!]?)/,/\s*(?=[<(])/],className:{1:"keyword"}, +contains:[L,B,n],illegal:/\[|%/},F={match:[/operator/,/\s+/,Be],className:{ +1:"keyword",3:"title"}},U={begin:[/precedencegroup/,/\s+/,Ue],className:{ +1:"keyword",3:"title"},contains:[R],keywords:[...Te,...Ce],end:/}/} +;for(const e of O.variants){const n=e.contains.find((e=>"interpol"===e.label)) +;n.keywords=l;const t=[...c,...g,...p,f,O,...C];n.contains=[...t,{begin:/\(/, +end:/\)/,contains:["self",...t]}]}return{name:"Swift",keywords:l, +contains:[...a,$,z,{beginKeywords:"struct protocol class extension enum actor", +end:"\\{",excludeEnd:!0,keywords:l,contains:[e.inherit(e.TITLE_MODE,{ +className:"title.class",begin:/[A-Za-z$_][\u00C0-\u02B80-9A-Za-z$_]*/}),...c] +},F,U,{beginKeywords:"import",end:/$/,contains:[...a],relevance:0 +},S,...c,...g,...p,f,O,...C,...T,R,I]}},grmr_typescript:e=>{ +const n=Oe(e),t=_e,a=["any","void","number","boolean","string","object","never","symbol","bigint","unknown"],i={ +beginKeywords:"namespace",end:/\{/,excludeEnd:!0, +contains:[n.exports.CLASS_REFERENCE]},r={beginKeywords:"interface",end:/\{/, +excludeEnd:!0,keywords:{keyword:"interface extends",built_in:a}, +contains:[n.exports.CLASS_REFERENCE]},s={$pattern:_e, +keyword:he.concat(["type","namespace","interface","public","private","protected","implements","declare","abstract","readonly","enum","override"]), +literal:fe,built_in:ve.concat(a),"variable.language":we},o={className:"meta", +begin:"@"+t},l=(e,n,t)=>{const a=e.contains.findIndex((e=>e.label===n)) +;if(-1===a)throw Error("can not find mode to replace");e.contains.splice(a,1,t)} +;return Object.assign(n.keywords,s), +n.exports.PARAMS_CONTAINS.push(o),n.contains=n.contains.concat([o,i,r]), +l(n,"shebang",e.SHEBANG()),l(n,"use_strict",{className:"meta",relevance:10, +begin:/^\s*['"]use strict['"]/ +}),n.contains.find((e=>"func.def"===e.label)).relevance=0,Object.assign(n,{ +name:"TypeScript",aliases:["ts","tsx","mts","cts"]}),n},grmr_vbnet:e=>{ +const n=e.regex,t=/\d{1,2}\/\d{1,2}\/\d{4}/,a=/\d{4}-\d{1,2}-\d{1,2}/,i=/(\d|1[012])(:\d+){0,2} *(AM|PM)/,r=/\d{1,2}(:\d{1,2}){1,2}/,s={ +className:"literal",variants:[{begin:n.concat(/# */,n.either(a,t),/ *#/)},{ +begin:n.concat(/# */,r,/ *#/)},{begin:n.concat(/# */,i,/ *#/)},{ +begin:n.concat(/# */,n.either(a,t),/ +/,n.either(i,r),/ *#/)}] +},o=e.COMMENT(/'''/,/$/,{contains:[{className:"doctag",begin:/<\/?/,end:/>/}] +}),l=e.COMMENT(null,/$/,{variants:[{begin:/'/},{begin:/([\t ]|^)REM(?=\s)/}]}) +;return{name:"Visual Basic .NET",aliases:["vb"],case_insensitive:!0, +classNameAliases:{label:"symbol"},keywords:{ +keyword:"addhandler alias aggregate ansi as async assembly auto binary by byref byval call case catch class compare const continue custom declare default delegate dim distinct do each equals else elseif end enum erase error event exit explicit finally for friend from function get global goto group handles if implements imports in inherits interface into iterator join key let lib loop me mid module mustinherit mustoverride mybase myclass namespace narrowing new next notinheritable notoverridable of off on operator option optional order overloads overridable overrides paramarray partial preserve private property protected public raiseevent readonly redim removehandler resume return select set shadows shared skip static step stop structure strict sub synclock take text then throw to try unicode until using when where while widening with withevents writeonly yield", +built_in:"addressof and andalso await directcast gettype getxmlnamespace is isfalse isnot istrue like mod nameof new not or orelse trycast typeof xor cbool cbyte cchar cdate cdbl cdec cint clng cobj csbyte cshort csng cstr cuint culng cushort", +type:"boolean byte char date decimal double integer long object sbyte short single string uinteger ulong ushort", +literal:"true false nothing"}, +illegal:"//|\\{|\\}|endif|gosub|variant|wend|^\\$ ",contains:[{ +className:"string",begin:/"(""|[^/n])"C\b/},{className:"string",begin:/"/, +end:/"/,illegal:/\n/,contains:[{begin:/""/}]},s,{className:"number",relevance:0, +variants:[{begin:/\b\d[\d_]*((\.[\d_]+(E[+-]?[\d_]+)?)|(E[+-]?[\d_]+))[RFD@!#]?/ +},{begin:/\b\d[\d_]*((U?[SIL])|[%&])?/},{begin:/&H[\dA-F_]+((U?[SIL])|[%&])?/},{ +begin:/&O[0-7_]+((U?[SIL])|[%&])?/},{begin:/&B[01_]+((U?[SIL])|[%&])?/}]},{ +className:"label",begin:/^\w+:/},o,l,{className:"meta", +begin:/[\t ]*#(const|disable|else|elseif|enable|end|externalsource|if|region)\b/, +end:/$/,keywords:{ +keyword:"const disable else elseif enable end externalsource if region then"}, +contains:[l]}]}},grmr_wasm:e=>{e.regex;const n=e.COMMENT(/\(;/,/;\)/) +;return n.contains.push("self"),{name:"WebAssembly",keywords:{$pattern:/[\w.]+/, +keyword:["anyfunc","block","br","br_if","br_table","call","call_indirect","data","drop","elem","else","end","export","func","global.get","global.set","local.get","local.set","local.tee","get_global","get_local","global","if","import","local","loop","memory","memory.grow","memory.size","module","mut","nop","offset","param","result","return","select","set_global","set_local","start","table","tee_local","then","type","unreachable"] +},contains:[e.COMMENT(/;;/,/$/),n,{match:[/(?:offset|align)/,/\s*/,/=/], +className:{1:"keyword",3:"operator"}},{className:"variable",begin:/\$[\w_]+/},{ +match:/(\((?!;)|\))+/,className:"punctuation",relevance:0},{ +begin:[/(?:func|call|call_indirect)/,/\s+/,/\$[^\s)]+/],className:{1:"keyword", +3:"title.function"}},e.QUOTE_STRING_MODE,{match:/(i32|i64|f32|f64)(?!\.)/, +className:"type"},{className:"keyword", +match:/\b(f32|f64|i32|i64)(?:\.(?:abs|add|and|ceil|clz|const|convert_[su]\/i(?:32|64)|copysign|ctz|demote\/f64|div(?:_[su])?|eqz?|extend_[su]\/i32|floor|ge(?:_[su])?|gt(?:_[su])?|le(?:_[su])?|load(?:(?:8|16|32)_[su])?|lt(?:_[su])?|max|min|mul|nearest|neg?|or|popcnt|promote\/f32|reinterpret\/[fi](?:32|64)|rem_[su]|rot[lr]|shl|shr_[su]|store(?:8|16|32)?|sqrt|sub|trunc(?:_[su]\/f(?:32|64))?|wrap\/i64|xor))\b/ +},{className:"number",relevance:0, +match:/[+-]?\b(?:\d(?:_?\d)*(?:\.\d(?:_?\d)*)?(?:[eE][+-]?\d(?:_?\d)*)?|0x[\da-fA-F](?:_?[\da-fA-F])*(?:\.[\da-fA-F](?:_?[\da-fA-D])*)?(?:[pP][+-]?\d(?:_?\d)*)?)\b|\binf\b|\bnan(?::0x[\da-fA-F](?:_?[\da-fA-D])*)?\b/ +}]}},grmr_xml:e=>{ +const n=e.regex,t=n.concat(/[\p{L}_]/u,n.optional(/[\p{L}0-9_.-]*:/u),/[\p{L}0-9_.-]*/u),a={ +className:"symbol",begin:/&[a-z]+;|&#[0-9]+;|&#x[a-f0-9]+;/},i={begin:/\s/, +contains:[{className:"keyword",begin:/#?[a-z_][a-z1-9_-]+/,illegal:/\n/}] +},r=e.inherit(i,{begin:/\(/,end:/\)/}),s=e.inherit(e.APOS_STRING_MODE,{ +className:"string"}),o=e.inherit(e.QUOTE_STRING_MODE,{className:"string"}),l={ +endsWithParent:!0,illegal:/`]+/}]}]}]};return{ +name:"HTML, XML", +aliases:["html","xhtml","rss","atom","xjb","xsd","xsl","plist","wsf","svg"], +case_insensitive:!0,unicodeRegex:!0,contains:[{className:"meta",begin://,relevance:10,contains:[i,o,s,r,{begin:/\[/,end:/\]/,contains:[{ +className:"meta",begin://,contains:[i,r,o,s]}]}] +},e.COMMENT(//,{relevance:10}),{begin://, +relevance:10},a,{className:"meta",end:/\?>/,variants:[{begin:/<\?xml/, +relevance:10,contains:[o]},{begin:/<\?[a-z][a-z0-9]+/}]},{className:"tag", +begin:/)/,end:/>/,keywords:{name:"style"},contains:[l],starts:{ +end:/<\/style>/,returnEnd:!0,subLanguage:["css","xml"]}},{className:"tag", +begin:/)/,end:/>/,keywords:{name:"script"},contains:[l],starts:{ +end:/<\/script>/,returnEnd:!0,subLanguage:["javascript","handlebars","xml"]}},{ +className:"tag",begin:/<>|<\/>/},{className:"tag", +begin:n.concat(//,/>/,/\s/)))), +end:/\/?>/,contains:[{className:"name",begin:t,relevance:0,starts:l}]},{ +className:"tag",begin:n.concat(/<\//,n.lookahead(n.concat(t,/>/))),contains:[{ +className:"name",begin:t,relevance:0},{begin:/>/,relevance:0,endsParent:!0}]}]} +},grmr_yaml:e=>{ +const n="true false yes no null",t="[\\w#;/?:@&=+$,.~*'()[\\]]+",a={ +className:"string",relevance:0,variants:[{begin:/'/,end:/'/},{begin:/"/,end:/"/ +},{begin:/\S+/}],contains:[e.BACKSLASH_ESCAPE,{className:"template-variable", +variants:[{begin:/\{\{/,end:/\}\}/},{begin:/%\{/,end:/\}/}]}]},i=e.inherit(a,{ +variants:[{begin:/'/,end:/'/},{begin:/"/,end:/"/},{begin:/[^\s,{}[\]]+/}]}),r={ +end:",",endsWithParent:!0,excludeEnd:!0,keywords:n,relevance:0},s={begin:/\{/, +end:/\}/,contains:[r],illegal:"\\n",relevance:0},o={begin:"\\[",end:"\\]", +contains:[r],illegal:"\\n",relevance:0},l=[{className:"attr",variants:[{ +begin:"\\w[\\w :\\/.-]*:(?=[ \t]|$)"},{begin:'"\\w[\\w :\\/.-]*":(?=[ \t]|$)'},{ +begin:"'\\w[\\w :\\/.-]*':(?=[ \t]|$)"}]},{className:"meta",begin:"^---\\s*$", +relevance:10},{className:"string", +begin:"[\\|>]([1-9]?[+-])?[ ]*\\n( +)[^ ][^\\n]*\\n(\\2[^\\n]+\\n?)*"},{ +begin:"<%[%=-]?",end:"[%-]?%>",subLanguage:"ruby",excludeBegin:!0,excludeEnd:!0, +relevance:0},{className:"type",begin:"!\\w+!"+t},{className:"type", +begin:"!<"+t+">"},{className:"type",begin:"!"+t},{className:"type",begin:"!!"+t +},{className:"meta",begin:"&"+e.UNDERSCORE_IDENT_RE+"$"},{className:"meta", +begin:"\\*"+e.UNDERSCORE_IDENT_RE+"$"},{className:"bullet",begin:"-(?=[ ]|$)", +relevance:0},e.HASH_COMMENT_MODE,{beginKeywords:n,keywords:{literal:n}},{ +className:"number", +begin:"\\b[0-9]{4}(-[0-9][0-9]){0,2}([Tt \\t][0-9][0-9]?(:[0-9][0-9]){2})?(\\.[0-9]*)?([ \\t])*(Z|[-+][0-9][0-9]?(:[0-9][0-9])?)?\\b" +},{className:"number",begin:e.C_NUMBER_RE+"\\b",relevance:0},s,o,a],c=[...l] +;return c.pop(),c.push(i),r.contains=c,{name:"YAML",case_insensitive:!0, +aliases:["yml"],contains:l}}});const He=ae;for(const e of Object.keys(Ke)){ +const n=e.replace("grmr_","").replace("_","-");He.registerLanguage(n,Ke[e])} +return He}() +;"object"==typeof exports&&"undefined"!=typeof module&&(module.exports=hljs); \ No newline at end of file diff --git a/packages/coding-agent/src/core/export-html/vendor/marked.min.js b/packages/coding-agent/src/core/export-html/vendor/marked.min.js new file mode 100644 index 0000000000000000000000000000000000000000..9d79575e45d0f6b88158097f5f4622ab938d76d9 --- /dev/null +++ b/packages/coding-agent/src/core/export-html/vendor/marked.min.js @@ -0,0 +1,78 @@ +/** + * marked v18.0.5 - a markdown parser + * Copyright (c) 2018-2026, MarkedJS. (MIT License) + * Copyright (c) 2011-2018, Christopher Jeffrey. (MIT License) + * https://github.com/markedjs/marked + */ + +/** + * DO NOT EDIT THIS FILE + * The code in this file is generated from files in ./src/ + */ +(function(g,f){if(typeof exports=="object"&&typeof module<"u"){module.exports=f()}else if("function"==typeof define && define.amd){define("marked",f)}else {g["marked"]=f()}}(typeof globalThis < "u" ? globalThis : typeof self < "u" ? self : this,function(){var exports={};var __exports=exports;var module={exports}; +"use strict";var N=Object.defineProperty;var Oe=Object.getOwnPropertyDescriptor;var we=Object.getOwnPropertyNames;var ye=Object.prototype.hasOwnProperty;var Pe=(l,e)=>{for(var t in e)N(l,t,{get:e[t],enumerable:!0})},Se=(l,e,t,n)=>{if(e&&typeof e=="object"||typeof e=="function")for(let s of we(e))!ye.call(l,s)&&s!==t&&N(l,s,{get:()=>e[s],enumerable:!(n=Oe(e,s))||n.enumerable});return l};var $e=l=>Se(N({},"__esModule",{value:!0}),l);var Rt={};Pe(Rt,{Hooks:()=>P,Lexer:()=>x,Marked:()=>C,Parser:()=>b,Renderer:()=>y,TextRenderer:()=>S,Tokenizer:()=>w,defaults:()=>T,getDefaults:()=>_,lexer:()=>bt,marked:()=>g,options:()=>ht,parse:()=>mt,parseInline:()=>ft,parser:()=>xt,setOptions:()=>kt,use:()=>dt,walkTokens:()=>gt});module.exports=$e(Rt);function _(){return{async:!1,breaks:!1,extensions:null,gfm:!0,hooks:null,pedantic:!1,renderer:null,silent:!1,tokenizer:null,walkTokens:null}}var T=_();function Q(l){T=l}var z={exec:()=>null};function E(l){let e=[];return t=>{let n=Math.max(0,Math.min(3,t-1)),s=e[n];return s||(s=l(n),e[n]=s),s}}function d(l,e=""){let t=typeof l=="string"?l:l.source,n={replace:(s,r)=>{let i=typeof r=="string"?r:r.source;return i=i.replace(m.caret,"$1"),t=t.replace(s,i),n},getRegex:()=>new RegExp(t,e)};return n}var Le=((l="")=>{try{return!!new RegExp("(?<=1)(?/,blockquoteSetextReplace:/\n {0,3}((?:=+|-+) *)(?=\n|$)/g,blockquoteSetextReplace2:/^ {0,3}>[ \t]?/gm,listReplaceNesting:/^ {1,4}(?=( {4})*[^ ])/g,listIsTask:/^\[[ xX]\] +\S/,listReplaceTask:/^\[[ xX]\] +/,listTaskCheckbox:/\[[ xX]\]/,anyLine:/\n.*\n/,hrefBrackets:/^<(.*)>$/,tableDelimiter:/[:|]/,tableAlignChars:/^\||\| *$/g,tableRowBlankLine:/\n[ \t]*$/,tableAlignRight:/^ *-+: *$/,tableAlignCenter:/^ *:-+: *$/,tableAlignLeft:/^ *:-+ *$/,startATag:/^/i,startPreScriptTag:/^<(pre|code|kbd|script)(\s|>)/i,endPreScriptTag:/^<\/(pre|code|kbd|script)(\s|>)/i,startAngleBracket:/^$/,pedanticHrefTitle:/^([^'"]*[^\s])\s+(['"])(.*)\2/,unicodeAlphaNumeric:/[\p{L}\p{N}]/u,escapeTest:/[&<>"']/,escapeReplace:/[&<>"']/g,escapeTestNoEncode:/[<>"']|&(?!(#\d{1,7}|#[Xx][a-fA-F0-9]{1,6}|\w+);)/,escapeReplaceNoEncode:/[<>"']|&(?!(#\d{1,7}|#[Xx][a-fA-F0-9]{1,6}|\w+);)/g,caret:/(^|[^\[])\^/g,percentDecode:/%25/g,findPipe:/\|/g,splitPipe:/ \|/,slashPipe:/\\\|/g,carriageReturn:/\r\n|\r/g,spaceLine:/^ +$/gm,notSpaceStart:/^\S*/,endingNewline:/\n$/,listItemRegex:l=>new RegExp(`^( {0,3}${l})((?:[ ][^\\n]*)?(?:\\n|$))`),nextBulletRegex:E(l=>new RegExp(`^ {0,${l}}(?:[*+-]|\\d{1,9}[.)])((?:[ ][^\\n]*)?(?:\\n|$))`)),hrRegex:E(l=>new RegExp(`^ {0,${l}}((?:- *){3,}|(?:_ *){3,}|(?:\\* *){3,})(?:\\n+|$)`)),fencesBeginRegex:E(l=>new RegExp(`^ {0,${l}}(?:\`\`\`|~~~)`)),headingBeginRegex:E(l=>new RegExp(`^ {0,${l}}#`)),htmlBeginRegex:E(l=>new RegExp(`^ {0,${l}}<(?:[a-z].*>|!--)`,"i")),blockquoteBeginRegex:E(l=>new RegExp(`^ {0,${l}}>`))},_e=/^(?:[ \t]*(?:\n|$))+/,ze=/^((?: {4}| {0,3}\t)[^\n]+(?:\n(?:[ \t]*(?:\n|$))*)?)+/,Me=/^ {0,3}(`{3,}(?=[^`\n]*(?:\n|$))|~{3,})([^\n]*)(?:\n|$)(?:|([\s\S]*?)(?:\n|$))(?: {0,3}\1[~`]* *(?=\n|$)|$)/,D=/^ {0,3}((?:-[\t ]*){3,}|(?:_[ \t]*){3,}|(?:\*[ \t]*){3,})(?:\n+|$)/,Ee=/^ {0,3}(#{1,6})(?=\s|$)(.*)(?:\n+|$)/,F=/ {0,3}(?:[*+-]|\d{1,9}[.)])/,ae=/^(?!bull |blockCode|fences|blockquote|heading|html|table)((?:.|\n(?!\s*?\n|bull |blockCode|fences|blockquote|heading|html|table))+?)\n {0,3}(=+|-+) *(?:\n+|$)/,le=d(ae).replace(/bull/g,F).replace(/blockCode/g,/(?: {4}| {0,3}\t)/).replace(/fences/g,/ {0,3}(?:`{3,}|~{3,})/).replace(/blockquote/g,/ {0,3}>/).replace(/heading/g,/ {0,3}#{1,6}/).replace(/html/g,/ {0,3}<[^\n>]+>\n/).replace(/\|table/g,"").getRegex(),Ie=d(ae).replace(/bull/g,F).replace(/blockCode/g,/(?: {4}| {0,3}\t)/).replace(/fences/g,/ {0,3}(?:`{3,}|~{3,})/).replace(/blockquote/g,/ {0,3}>/).replace(/heading/g,/ {0,3}#{1,6}/).replace(/html/g,/ {0,3}<[^\n>]+>\n/).replace(/table/g,/ {0,3}\|?(?:[:\- ]*\|)+[\:\- ]*\n/).getRegex(),U=/^([^\n]+(?:\n(?!hr|heading|lheading|blockquote|fences|list|html|table| +\n)[^\n]+)*)/,Ae=/^[^\n]+/,K=/(?!\s*\])(?:\\[\s\S]|[^\[\]\\])+/,Ce=d(/^ {0,3}\[(label)\]: *(?:\n[ \t]*)?([^<\s][^\s]*|<.*?>)(?:(?: +(?:\n[ \t]*)?| *\n[ \t]*)(title))? *(?:\n+|$)/).replace("label",K).replace("title",/(?:"(?:\\"?|[^"\\])*"|'[^'\n]*(?:\n[^'\n]+)*\n?'|\([^()]*\))/).getRegex(),Be=d(/^(bull)([ \t][^\n]*?)?(?:\n|$)/).replace(/bull/g,F).getRegex(),H="address|article|aside|base|basefont|blockquote|body|caption|center|col|colgroup|dd|details|dialog|dir|div|dl|dt|fieldset|figcaption|figure|footer|form|frame|frameset|h[1-6]|head|header|hr|html|iframe|legend|li|link|main|menu|menuitem|meta|nav|noframes|ol|optgroup|option|p|param|search|section|summary|table|tbody|td|tfoot|th|thead|title|tr|track|ul",W=/|$))/,De=d("^ {0,3}(?:<(script|pre|style|textarea)[\\s>][\\s\\S]*?(?:[^\\n]*\\n+|$)|comment[^\\n]*(\\n+|$)|<\\?[\\s\\S]*?(?:\\?>\\n*|$)|\\n*|$)|\\n*|$)|)[\\s\\S]*?(?:(?:\\n[ ]*)+\\n|$)|<(?!script|pre|style|textarea)([a-z][\\w-]*)(?:attribute)*? */?>(?=[ \\t]*(?:\\n|$))[\\s\\S]*?(?:(?:\\n[ ]*)+\\n|$)|(?=[ \\t]*(?:\\n|$))[\\s\\S]*?(?:(?:\\n[ ]*)+\\n|$))","i").replace("comment",W).replace("tag",H).replace("attribute",/ +[a-zA-Z:_][\w.:-]*(?: *= *"[^"\n]*"| *= *'[^'\n]*'| *= *[^\s"'=<>`]+)?/).getRegex(),ue=d(U).replace("hr",D).replace("heading"," {0,3}#{1,6}(?:\\s|$)").replace("|lheading","").replace("|table","").replace("blockquote"," {0,3}>").replace("fences"," {0,3}(?:`{3,}(?=[^`\\n]*\\n)|~{3,})[^\\n]*\\n").replace("list"," {0,3}(?:[*+-]|1[.)])[ \\t]+[^ \\t\\n]").replace("html",")|<(?:script|pre|style|textarea|!--)").replace("tag",H).getRegex(),qe=d(/^( {0,3}> ?(paragraph|[^\n]*)(?:\n|$))+/).replace("paragraph",ue).getRegex(),X={blockquote:qe,code:ze,def:Ce,fences:Me,heading:Ee,hr:D,html:De,lheading:le,list:Be,newline:_e,paragraph:ue,table:z,text:Ae},ie=d("^ *([^\\n ].*)\\n {0,3}((?:\\| *)?:?-+:? *(?:\\| *:?-+:? *)*(?:\\| *)?)(?:\\n((?:(?! *\\n|hr|heading|blockquote|code|fences|list|html).*(?:\\n|$))*)\\n*|$)").replace("hr",D).replace("heading"," {0,3}#{1,6}(?:\\s|$)").replace("blockquote"," {0,3}>").replace("code","(?: {4}| {0,3} )[^\\n]").replace("fences"," {0,3}(?:`{3,}(?=[^`\\n]*\\n)|~{3,})[^\\n]*\\n").replace("list"," {0,3}(?:[*+-]|1[.)])[ \\t]").replace("html",")|<(?:script|pre|style|textarea|!--)").replace("tag",H).getRegex(),ve={...X,lheading:Ie,table:ie,paragraph:d(U).replace("hr",D).replace("heading"," {0,3}#{1,6}(?:\\s|$)").replace("|lheading","").replace("table",ie).replace("blockquote"," {0,3}>").replace("fences"," {0,3}(?:`{3,}(?=[^`\\n]*\\n)|~{3,})[^\\n]*\\n").replace("list"," {0,3}(?:[*+-]|1[.)])[ \\t]+[^ \\t\\n]").replace("html",")|<(?:script|pre|style|textarea|!--)").replace("tag",H).getRegex()},He={...X,html:d(`^ *(?:comment *(?:\\n|\\s*$)|<(tag)[\\s\\S]+? *(?:\\n{2,}|\\s*$)|\\s]*)*?/?> *(?:\\n{2,}|\\s*$))`).replace("comment",W).replace(/tag/g,"(?!(?:a|em|strong|small|s|cite|q|dfn|abbr|data|time|code|var|samp|kbd|sub|sup|i|b|u|mark|ruby|rt|rp|bdi|bdo|span|br|wbr|ins|del|img)\\b)\\w+(?!:|[^\\w\\s@]*@)\\b").getRegex(),def:/^ *\[([^\]]+)\]: *]+)>?(?: +(["(][^\n]+[")]))? *(?:\n+|$)/,heading:/^(#{1,6})(.*)(?:\n+|$)/,fences:z,lheading:/^(.+?)\n {0,3}(=+|-+) *(?:\n+|$)/,paragraph:d(U).replace("hr",D).replace("heading",` *#{1,6} *[^ +]`).replace("lheading",le).replace("|table","").replace("blockquote"," {0,3}>").replace("|fences","").replace("|list","").replace("|html","").replace("|tag","").getRegex()},Ze=/^\\([!"#$%&'()*+,\-./:;<=>?@\[\]\\^_`{|}~])/,Ge=/^(`+)([^`]|[^`][\s\S]*?[^`])\1(?!`)/,pe=/^( {2,}|\\)\n(?!\s*$)/,Ne=/^(`+|[^`])(?:(?= {2,}\n)|[\s\S]*?(?:(?=[\\`+)[^`]+\k(?!`))*?\]\((?:\\[\s\S]|[^\\\(\)]|\((?:\\[\s\S]|[^\\\(\)])*\))*\)/).replace("precode-",Le?"(?`+)[^`]+\k(?!`)/).replace("html",/<(?! )[^<>]*?>/).getRegex(),he=/^(?:\*+(?:((?!\*)punct)|([^\s*]))?)|^_+(?:((?!_)punct)|([^\s_]))?/,Ke=d(he,"u").replace(/punct/g,I).getRegex(),We=d(he,"u").replace(/punct/g,ce).getRegex(),ke="^[^_*]*?__[^_*]*?\\*[^_*]*?(?=__)|[^*]+(?=[^*])|(?!\\*)punct(\\*+)(?=[\\s]|$)|notPunctSpace(\\*+)(?!\\*)(?=punctSpace|$)|(?!\\*)punctSpace(\\*+)(?=notPunctSpace)|[\\s](\\*+)(?!\\*)(?=punct)|(?!\\*)punct(\\*+)(?!\\*)(?=punct)|notPunctSpace(\\*+)(?=notPunctSpace)",Xe=d(ke,"gu").replace(/notPunctSpace/g,J).replace(/punctSpace/g,Z).replace(/punct/g,I).getRegex(),Je=d(ke,"gu").replace(/notPunctSpace/g,Fe).replace(/punctSpace/g,je).replace(/punct/g,ce).getRegex(),Ve=d("^[^_*]*?\\*\\*[^_*]*?_[^_*]*?(?=\\*\\*)|[^_]+(?=[^_])|(?!_)punct(_+)(?=[\\s]|$)|notPunctSpace(_+)(?!_)(?=punctSpace|$)|(?!_)punctSpace(_+)(?=notPunctSpace)|[\\s](_+)(?!_)(?=punct)|(?!_)punct(_+)(?!_)(?=punct)","gu").replace(/notPunctSpace/g,J).replace(/punctSpace/g,Z).replace(/punct/g,I).getRegex(),Ye=d(/^~~?(?:((?!~)punct)|[^\s~])/,"u").replace(/punct/g,I).getRegex(),et="^[^~]+(?=[^~])|(?!~)punct(~~?)(?=[\\s]|$)|notPunctSpace(~~?)(?!~)(?=punctSpace|$)|(?!~)punctSpace(~~?)(?=notPunctSpace)|[\\s](~~?)(?!~)(?=punct)|(?!~)punct(~~?)(?!~)(?=punct)|notPunctSpace(~~?)(?=notPunctSpace)",tt=d(et,"gu").replace(/notPunctSpace/g,J).replace(/punctSpace/g,Z).replace(/punct/g,I).getRegex(),nt=d(/\\(punct)/,"gu").replace(/punct/g,I).getRegex(),rt=d(/^<(scheme:[^\s\x00-\x1f<>]*|email)>/).replace("scheme",/[a-zA-Z][a-zA-Z0-9+.-]{1,31}/).replace("email",/[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+(@)[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)+(?![-_])/).getRegex(),st=d(W).replace("(?:-->|$)","-->").getRegex(),it=d("^comment|^|^<[a-zA-Z][\\w-]*(?:attribute)*?\\s*/?>|^<\\?[\\s\\S]*?\\?>|^|^").replace("comment",st).replace("attribute",/\s+[a-zA-Z:_][\w.:-]*(?:\s*=\s*"[^"]*"|\s*=\s*'[^']*'|\s*=\s*[^\s"'=<>`]+)?/).getRegex(),v=/(?:\[(?:\\[\s\S]|[^\[\]\\])*\]|\\[\s\S]|`+(?!`)[^`]*?`+(?!`)|``+(?=\])|[^\[\]\\`])*?/,ot=d(/^!?\[(label)\]\(\s*(href)(?:(?:[ \t]+(?:\n[ \t]*)?|\n[ \t]*)(title))?\s*\)/).replace("label",v).replace("href",/<(?:\\.|[^\n<>\\])+>|[^ \t\n\x00-\x1f]*/).replace("title",/"(?:\\"?|[^"\\])*"|'(?:\\'?|[^'\\])*'|\((?:\\\)?|[^)\\])*\)/).getRegex(),de=d(/^!?\[(label)\]\[(ref)\]/).replace("label",v).replace("ref",K).getRegex(),ge=d(/^!?\[(ref)\](?:\[\])?/).replace("ref",K).getRegex(),at=d("reflink|nolink(?!\\()","g").replace("reflink",de).replace("nolink",ge).getRegex(),oe=/[hH][tT][tT][pP][sS]?|[fF][tT][pP]/,V={_backpedal:z,anyPunctuation:nt,autolink:rt,blockSkip:Ue,br:pe,code:Ge,del:z,delLDelim:z,delRDelim:z,emStrongLDelim:Ke,emStrongRDelimAst:Xe,emStrongRDelimUnd:Ve,escape:Ze,link:ot,nolink:ge,punctuation:Qe,reflink:de,reflinkSearch:at,tag:it,text:Ne,url:z},lt={...V,link:d(/^!?\[(label)\]\((.*?)\)/).replace("label",v).getRegex(),reflink:d(/^!?\[(label)\]\s*\[([^\]]*)\]/).replace("label",v).getRegex()},j={...V,emStrongRDelimAst:Je,emStrongLDelim:We,delLDelim:Ye,delRDelim:tt,url:d(/^((?:protocol):\/\/|www\.)(?:[a-zA-Z0-9\-]+\.?)+[^\s<]*|^email/).replace("protocol",oe).replace("email",/[A-Za-z0-9._+-]+(@)[a-zA-Z0-9-_]+(?:\.[a-zA-Z0-9-_]*[a-zA-Z0-9])+(?![-_])/).getRegex(),_backpedal:/(?:[^?!.,:;*_'"~()&]+|\([^)]*\)|&(?![a-zA-Z0-9]+;$)|[?!.,:;*_'"~)]+(?!$))+/,del:/^(~~?)(?=[^\s~])((?:\\[\s\S]|[^\\])*?(?:\\[\s\S]|[^\s~\\]))\1(?=[^~]|$)/,text:d(/^([`~]+|[^`~])(?:(?= {2,}\n)|(?=[a-zA-Z0-9.!#$%&'*+\/=?_`{\|}~-]+@)|[\s\S]*?(?:(?=[\\":">",'"':""","'":"'"},fe=l=>pt[l];function O(l,e){if(e){if(m.escapeTest.test(l))return l.replace(m.escapeReplace,fe)}else if(m.escapeTestNoEncode.test(l))return l.replace(m.escapeReplaceNoEncode,fe);return l}function Y(l){try{l=encodeURI(l).replace(m.percentDecode,"%")}catch{return null}return l}function ee(l,e){let t=l.replace(m.findPipe,(r,i,o)=>{let u=!1,a=i;for(;--a>=0&&o[a]==="\\";)u=!u;return u?"|":" |"}),n=t.split(m.splitPipe),s=0;if(n[0].trim()||n.shift(),n.length>0&&!n.at(-1)?.trim()&&n.pop(),e)if(n.length>e)n.splice(e);else for(;n.length=0&&m.blankLine.test(e[t]);)t--;return e.length-t<=2?l:e.slice(0,t+1).join(` +`)}function me(l,e){if(l.indexOf(e[1])===-1)return-1;let t=0;for(let n=0;n0?-2:-1}function xe(l,e=0){let t=e,n="";for(let s of l)if(s===" "){let r=4-t%4;n+=" ".repeat(r),t+=r}else n+=s,t++;return n}function be(l,e,t,n,s){let r=e.href,i=e.title||null,o=l[1].replace(s.other.outputLinkReplace,"$1");n.state.inLink=!0;let u={type:l[0].charAt(0)==="!"?"image":"link",raw:t,href:r,title:i,text:o,tokens:n.inlineTokens(o)};return n.state.inLink=!1,u}function ct(l,e,t){let n=l.match(t.other.indentCodeCompensation);if(n===null)return e;let s=n[1];return e.split(` +`).map(r=>{let i=r.match(t.other.beginningSpace);if(i===null)return r;let[o]=i;return o.length>=s.length?r.slice(s.length):r}).join(` +`)}var w=class{options;rules;lexer;constructor(e){this.options=e||T}space(e){let t=this.rules.block.newline.exec(e);if(t&&t[0].length>0)return{type:"space",raw:t[0]}}code(e){let t=this.rules.block.code.exec(e);if(t){let n=this.options.pedantic?t[0]:te(t[0]),s=n.replace(this.rules.other.codeRemoveIndent,"");return{type:"code",raw:n,codeBlockStyle:"indented",text:s}}}fences(e){let t=this.rules.block.fences.exec(e);if(t){let n=t[0],s=ct(n,t[3]||"",this.rules);return{type:"code",raw:n,lang:t[2]?t[2].trim().replace(this.rules.inline.anyPunctuation,"$1"):t[2],text:s}}}heading(e){let t=this.rules.block.heading.exec(e);if(t){let n=t[2].trim();if(this.rules.other.endingHash.test(n)){let s=L(n,"#");(this.options.pedantic||!s||this.rules.other.endingSpaceChar.test(s))&&(n=s.trim())}return{type:"heading",raw:L(t[0],` +`),depth:t[1].length,text:n,tokens:this.lexer.inline(n)}}}hr(e){let t=this.rules.block.hr.exec(e);if(t)return{type:"hr",raw:L(t[0],` +`)}}blockquote(e){let t=this.rules.block.blockquote.exec(e);if(t){let n=L(t[0],` +`).split(` +`),s="",r="",i=[];for(;n.length>0;){let o=!1,u=[],a;for(a=0;a1,r={type:"list",raw:"",ordered:s,start:s?+n.slice(0,-1):"",loose:!1,items:[]};n=s?`\\d{1,9}\\${n.slice(-1)}`:`\\${n}`,this.options.pedantic&&(n=s?n:"[*+-]");let i=this.rules.other.listItemRegex(n),o=!1;for(;e;){let a=!1,c="",p="";if(!(t=i.exec(e))||this.rules.block.hr.test(e))break;c=t[0],e=e.substring(c.length);let k=xe(t[2].split(` +`,1)[0],t[1].length),h=e.split(` +`,1)[0],R=!k.trim(),f=0;if(this.options.pedantic?(f=2,p=k.trimStart()):R?f=t[1].length+1:(f=k.search(this.rules.other.nonSpaceChar),f=f>4?1:f,p=k.slice(f),f+=t[1].length),R&&this.rules.other.blankLine.test(h)&&(c+=h+` +`,e=e.substring(h.length+1),a=!0),!a){let $=this.rules.other.nextBulletRegex(f),ne=this.rules.other.hrRegex(f),re=this.rules.other.fencesBeginRegex(f),se=this.rules.other.headingBeginRegex(f),Re=this.rules.other.htmlBeginRegex(f),Te=this.rules.other.blockquoteBeginRegex(f);for(;e;){let G=e.split(` +`,1)[0],B;if(h=G,this.options.pedantic?(h=h.replace(this.rules.other.listReplaceNesting," "),B=h):B=h.replace(this.rules.other.tabCharGlobal," "),re.test(h)||se.test(h)||Re.test(h)||Te.test(h)||$.test(h)||ne.test(h))break;if(B.search(this.rules.other.nonSpaceChar)>=f||!h.trim())p+=` +`+B.slice(f);else{if(R||k.replace(this.rules.other.tabCharGlobal," ").search(this.rules.other.nonSpaceChar)>=4||re.test(k)||se.test(k)||ne.test(k))break;p+=` +`+h}R=!h.trim(),c+=G+` +`,e=e.substring(G.length+1),k=B.slice(f)}}r.loose||(o?r.loose=!0:this.rules.other.doubleBlankLine.test(c)&&(o=!0)),r.items.push({type:"list_item",raw:c,task:!!this.options.gfm&&this.rules.other.listIsTask.test(p),loose:!1,text:p,tokens:[]}),r.raw+=c}let u=r.items.at(-1);if(u)u.raw=u.raw.trimEnd(),u.text=u.text.trimEnd();else return;r.raw=r.raw.trimEnd();for(let a of r.items){this.lexer.state.top=!1,a.tokens=this.lexer.blockTokens(a.text,[]);let c=a.tokens[0];if(a.task&&(c?.type==="text"||c?.type==="paragraph")){a.text=a.text.replace(this.rules.other.listReplaceTask,""),c.raw=c.raw.replace(this.rules.other.listReplaceTask,""),c.text=c.text.replace(this.rules.other.listReplaceTask,"");for(let k=this.lexer.inlineQueue.length-1;k>=0;k--)if(this.rules.other.listIsTask.test(this.lexer.inlineQueue[k].src)){this.lexer.inlineQueue[k].src=this.lexer.inlineQueue[k].src.replace(this.rules.other.listReplaceTask,"");break}let p=this.rules.other.listTaskCheckbox.exec(a.raw);if(p){let k={type:"checkbox",raw:p[0]+" ",checked:p[0]!=="[ ]"};a.checked=k.checked,r.loose?a.tokens[0]&&["paragraph","text"].includes(a.tokens[0].type)&&"tokens"in a.tokens[0]&&a.tokens[0].tokens?(a.tokens[0].raw=k.raw+a.tokens[0].raw,a.tokens[0].text=k.raw+a.tokens[0].text,a.tokens[0].tokens.unshift(k)):a.tokens.unshift({type:"paragraph",raw:k.raw,text:k.raw,tokens:[k]}):a.tokens.unshift(k)}}else a.task&&(a.task=!1);if(!r.loose){let p=a.tokens.filter(h=>h.type==="space"),k=p.length>0&&p.some(h=>this.rules.other.anyLine.test(h.raw));r.loose=k}}if(r.loose)for(let a of r.items){a.loose=!0;for(let c of a.tokens)c.type==="text"&&(c.type="paragraph")}return r}}html(e){let t=this.rules.block.html.exec(e);if(t){let n=te(t[0]);return{type:"html",block:!0,raw:n,pre:t[1]==="pre"||t[1]==="script"||t[1]==="style",text:n}}}def(e){let t=this.rules.block.def.exec(e);if(t){let n=t[1].toLowerCase().replace(this.rules.other.multipleSpaceGlobal," "),s=t[2]?t[2].replace(this.rules.other.hrefBrackets,"$1").replace(this.rules.inline.anyPunctuation,"$1"):"",r=t[3]?t[3].substring(1,t[3].length-1).replace(this.rules.inline.anyPunctuation,"$1"):t[3];return{type:"def",tag:n,raw:L(t[0],` +`),href:s,title:r}}}table(e){let t=this.rules.block.table.exec(e);if(!t||!this.rules.other.tableDelimiter.test(t[2]))return;let n=ee(t[1]),s=t[2].replace(this.rules.other.tableAlignChars,"").split("|"),r=t[3]?.trim()?t[3].replace(this.rules.other.tableRowBlankLine,"").split(` +`):[],i={type:"table",raw:L(t[0],` +`),header:[],align:[],rows:[]};if(n.length===s.length){for(let o of s)this.rules.other.tableAlignRight.test(o)?i.align.push("right"):this.rules.other.tableAlignCenter.test(o)?i.align.push("center"):this.rules.other.tableAlignLeft.test(o)?i.align.push("left"):i.align.push(null);for(let o=0;o({text:u,tokens:this.lexer.inline(u),header:!1,align:i.align[a]})));return i}}lheading(e){let t=this.rules.block.lheading.exec(e);if(t){let n=t[1].trim();return{type:"heading",raw:L(t[0],` +`),depth:t[2].charAt(0)==="="?1:2,text:n,tokens:this.lexer.inline(n)}}}paragraph(e){let t=this.rules.block.paragraph.exec(e);if(t){let n=t[1].charAt(t[1].length-1)===` +`?t[1].slice(0,-1):t[1];return{type:"paragraph",raw:t[0],text:n,tokens:this.lexer.inline(n)}}}text(e){let t=this.rules.block.text.exec(e);if(t)return{type:"text",raw:t[0],text:t[0],tokens:this.lexer.inline(t[0])}}escape(e){let t=this.rules.inline.escape.exec(e);if(t)return{type:"escape",raw:t[0],text:t[1]}}tag(e){let t=this.rules.inline.tag.exec(e);if(t)return!this.lexer.state.inLink&&this.rules.other.startATag.test(t[0])?this.lexer.state.inLink=!0:this.lexer.state.inLink&&this.rules.other.endATag.test(t[0])&&(this.lexer.state.inLink=!1),!this.lexer.state.inRawBlock&&this.rules.other.startPreScriptTag.test(t[0])?this.lexer.state.inRawBlock=!0:this.lexer.state.inRawBlock&&this.rules.other.endPreScriptTag.test(t[0])&&(this.lexer.state.inRawBlock=!1),{type:"html",raw:t[0],inLink:this.lexer.state.inLink,inRawBlock:this.lexer.state.inRawBlock,block:!1,text:t[0]}}link(e){let t=this.rules.inline.link.exec(e);if(t){let n=t[2].trim();if(!this.options.pedantic&&this.rules.other.startAngleBracket.test(n)){if(!this.rules.other.endAngleBracket.test(n))return;let i=L(n.slice(0,-1),"\\");if((n.length-i.length)%2===0)return}else{let i=me(t[2],"()");if(i===-2)return;if(i>-1){let u=(t[0].indexOf("!")===0?5:4)+t[1].length+i;t[2]=t[2].substring(0,i),t[0]=t[0].substring(0,u).trim(),t[3]=""}}let s=t[2],r="";if(this.options.pedantic){let i=this.rules.other.pedanticHrefTitle.exec(s);i&&(s=i[1],r=i[3])}else r=t[3]?t[3].slice(1,-1):"";return s=s.trim(),this.rules.other.startAngleBracket.test(s)&&(this.options.pedantic&&!this.rules.other.endAngleBracket.test(n)?s=s.slice(1):s=s.slice(1,-1)),be(t,{href:s&&s.replace(this.rules.inline.anyPunctuation,"$1"),title:r&&r.replace(this.rules.inline.anyPunctuation,"$1")},t[0],this.lexer,this.rules)}}reflink(e,t){let n;if((n=this.rules.inline.reflink.exec(e))||(n=this.rules.inline.nolink.exec(e))){let s=(n[2]||n[1]).replace(this.rules.other.multipleSpaceGlobal," "),r=t[s.toLowerCase()];if(!r){let i=n[0].charAt(0);return{type:"text",raw:i,text:i}}return be(n,r,n[0],this.lexer,this.rules)}}emStrong(e,t,n=""){let s=this.rules.inline.emStrongLDelim.exec(e);if(!s||!s[1]&&!s[2]&&!s[3]&&!s[4]||s[4]&&n.match(this.rules.other.unicodeAlphaNumeric))return;if(!(s[1]||s[3]||"")||!n||this.rules.inline.punctuation.exec(n)){let i=[...s[0]].length-1,o,u,a=i,c=0,p=s[0][0]==="*"?this.rules.inline.emStrongRDelimAst:this.rules.inline.emStrongRDelimUnd;for(p.lastIndex=0,t=t.slice(-1*e.length+i);(s=p.exec(t))!==null;){if(o=s[1]||s[2]||s[3]||s[4]||s[5]||s[6],!o)continue;if(u=[...o].length,s[3]||s[4]){a+=u;continue}else if((s[5]||s[6])&&i%3&&!((i+u)%3)){c+=u;continue}if(a-=u,a>0)continue;u=Math.min(u,u+a+c);let k=[...s[0]][0].length,h=e.slice(0,i+s.index+k+u);if(Math.min(i,u)%2){let f=h.slice(1,-1);return{type:"em",raw:h,text:f,tokens:this.lexer.inlineTokens(f)}}let R=h.slice(2,-2);return{type:"strong",raw:h,text:R,tokens:this.lexer.inlineTokens(R)}}}}codespan(e){let t=this.rules.inline.code.exec(e);if(t){let n=t[2].replace(this.rules.other.newLineCharGlobal," "),s=this.rules.other.nonSpaceChar.test(n),r=this.rules.other.startingSpaceChar.test(n)&&this.rules.other.endingSpaceChar.test(n);return s&&r&&(n=n.substring(1,n.length-1)),{type:"codespan",raw:t[0],text:n}}}br(e){let t=this.rules.inline.br.exec(e);if(t)return{type:"br",raw:t[0]}}del(e,t,n=""){let s=this.rules.inline.delLDelim.exec(e);if(!s)return;if(!(s[1]||"")||!n||this.rules.inline.punctuation.exec(n)){let i=[...s[0]].length-1,o,u,a=i,c=this.rules.inline.delRDelim;for(c.lastIndex=0,t=t.slice(-1*e.length+i);(s=c.exec(t))!==null;){if(o=s[1]||s[2]||s[3]||s[4]||s[5]||s[6],!o||(u=[...o].length,u!==i))continue;if(s[3]||s[4]){a+=u;continue}if(a-=u,a>0)continue;u=Math.min(u,u+a);let p=[...s[0]][0].length,k=e.slice(0,i+s.index+p+u),h=k.slice(i,-i);return{type:"del",raw:k,text:h,tokens:this.lexer.inlineTokens(h)}}}}autolink(e){let t=this.rules.inline.autolink.exec(e);if(t){let n,s;return t[2]==="@"?(n=t[1],s="mailto:"+n):(n=t[1],s=n),{type:"link",raw:t[0],text:n,href:s,tokens:[{type:"text",raw:n,text:n}]}}}url(e){let t;if(t=this.rules.inline.url.exec(e)){let n,s;if(t[2]==="@")n=t[0],s="mailto:"+n;else{let r;do r=t[0],t[0]=this.rules.inline._backpedal.exec(t[0])?.[0]??"";while(r!==t[0]);n=t[0],t[1]==="www."?s="http://"+t[0]:s=t[0]}return{type:"link",raw:t[0],text:n,href:s,tokens:[{type:"text",raw:n,text:n}]}}}inlineText(e){let t=this.rules.inline.text.exec(e);if(t){let n=this.lexer.state.inRawBlock;return{type:"text",raw:t[0],text:t[0],escaped:n}}}};var x=class l{tokens;options;state;inlineQueue;tokenizer;constructor(e){this.tokens=[],this.tokens.links=Object.create(null),this.options=e||T,this.options.tokenizer=this.options.tokenizer||new w,this.tokenizer=this.options.tokenizer,this.tokenizer.options=this.options,this.tokenizer.lexer=this,this.inlineQueue=[],this.state={inLink:!1,inRawBlock:!1,top:!0};let t={other:m,block:q.normal,inline:A.normal};this.options.pedantic?(t.block=q.pedantic,t.inline=A.pedantic):this.options.gfm&&(t.block=q.gfm,this.options.breaks?t.inline=A.breaks:t.inline=A.gfm),this.tokenizer.rules=t}static get rules(){return{block:q,inline:A}}static lex(e,t){return new l(t).lex(e)}static lexInline(e,t){return new l(t).inlineTokens(e)}lex(e){e=e.replace(m.carriageReturn,` +`),this.blockTokens(e,this.tokens);for(let t=0;t(r=o.call({lexer:this},e,t))?(e=e.substring(r.raw.length),t.push(r),!0):!1))continue;if(r=this.tokenizer.space(e)){e=e.substring(r.raw.length);let o=t.at(-1);r.raw.length===1&&o!==void 0?o.raw+=` +`:t.push(r);continue}if(r=this.tokenizer.code(e)){e=e.substring(r.raw.length);let o=t.at(-1);o?.type==="paragraph"||o?.type==="text"?(o.raw+=(o.raw.endsWith(` +`)?"":` +`)+r.raw,o.text+=` +`+r.text,this.inlineQueue.at(-1).src=o.text):t.push(r);continue}if(r=this.tokenizer.fences(e)){e=e.substring(r.raw.length),t.push(r);continue}if(r=this.tokenizer.heading(e)){e=e.substring(r.raw.length),t.push(r);continue}if(r=this.tokenizer.hr(e)){e=e.substring(r.raw.length),t.push(r);continue}if(r=this.tokenizer.blockquote(e)){e=e.substring(r.raw.length),t.push(r);continue}if(r=this.tokenizer.list(e)){e=e.substring(r.raw.length),t.push(r);continue}if(r=this.tokenizer.html(e)){e=e.substring(r.raw.length),t.push(r);continue}if(r=this.tokenizer.def(e)){e=e.substring(r.raw.length);let o=t.at(-1);o?.type==="paragraph"||o?.type==="text"?(o.raw+=(o.raw.endsWith(` +`)?"":` +`)+r.raw,o.text+=` +`+r.raw,this.inlineQueue.at(-1).src=o.text):this.tokens.links[r.tag]||(this.tokens.links[r.tag]={href:r.href,title:r.title},t.push(r));continue}if(r=this.tokenizer.table(e)){e=e.substring(r.raw.length),t.push(r);continue}if(r=this.tokenizer.lheading(e)){e=e.substring(r.raw.length),t.push(r);continue}let i=e;if(this.options.extensions?.startBlock){let o=1/0,u=e.slice(1),a;this.options.extensions.startBlock.forEach(c=>{a=c.call({lexer:this},u),typeof a=="number"&&a>=0&&(o=Math.min(o,a))}),o<1/0&&o>=0&&(i=e.substring(0,o+1))}if(this.state.top&&(r=this.tokenizer.paragraph(i))){let o=t.at(-1);n&&o?.type==="paragraph"?(o.raw+=(o.raw.endsWith(` +`)?"":` +`)+r.raw,o.text+=` +`+r.text,this.inlineQueue.pop(),this.inlineQueue.at(-1).src=o.text):t.push(r),n=i.length!==e.length,e=e.substring(r.raw.length);continue}if(r=this.tokenizer.text(e)){e=e.substring(r.raw.length);let o=t.at(-1);o?.type==="text"?(o.raw+=(o.raw.endsWith(` +`)?"":` +`)+r.raw,o.text+=` +`+r.text,this.inlineQueue.pop(),this.inlineQueue.at(-1).src=o.text):t.push(r);continue}if(e){this.infiniteLoopError(e.charCodeAt(0));break}}return this.state.top=!0,t}inline(e,t=[]){return this.inlineQueue.push({src:e,tokens:t}),t}inlineTokens(e,t=[]){this.tokenizer.lexer=this;let n=e,s=null;if(this.tokens.links){let a=Object.keys(this.tokens.links);if(a.length>0)for(;(s=this.tokenizer.rules.inline.reflinkSearch.exec(n))!==null;)a.includes(s[0].slice(s[0].lastIndexOf("[")+1,-1))&&(n=n.slice(0,s.index)+"["+"a".repeat(s[0].length-2)+"]"+n.slice(this.tokenizer.rules.inline.reflinkSearch.lastIndex))}for(;(s=this.tokenizer.rules.inline.anyPunctuation.exec(n))!==null;)n=n.slice(0,s.index)+"++"+n.slice(this.tokenizer.rules.inline.anyPunctuation.lastIndex);let r;for(;(s=this.tokenizer.rules.inline.blockSkip.exec(n))!==null;)r=s[2]?s[2].length:0,n=n.slice(0,s.index+r)+"["+"a".repeat(s[0].length-r-2)+"]"+n.slice(this.tokenizer.rules.inline.blockSkip.lastIndex);n=this.options.hooks?.emStrongMask?.call({lexer:this},n)??n;let i=!1,o="",u=1/0;for(;e;){if(e.length(a=p.call({lexer:this},e,t))?(e=e.substring(a.raw.length),t.push(a),!0):!1))continue;if(a=this.tokenizer.escape(e)){e=e.substring(a.raw.length),t.push(a);continue}if(a=this.tokenizer.tag(e)){e=e.substring(a.raw.length),t.push(a);continue}if(a=this.tokenizer.link(e)){e=e.substring(a.raw.length),t.push(a);continue}if(a=this.tokenizer.reflink(e,this.tokens.links)){e=e.substring(a.raw.length);let p=t.at(-1);a.type==="text"&&p?.type==="text"?(p.raw+=a.raw,p.text+=a.text):t.push(a);continue}if(a=this.tokenizer.emStrong(e,n,o)){e=e.substring(a.raw.length),t.push(a);continue}if(a=this.tokenizer.codespan(e)){e=e.substring(a.raw.length),t.push(a);continue}if(a=this.tokenizer.br(e)){e=e.substring(a.raw.length),t.push(a);continue}if(a=this.tokenizer.del(e,n,o)){e=e.substring(a.raw.length),t.push(a);continue}if(a=this.tokenizer.autolink(e)){e=e.substring(a.raw.length),t.push(a);continue}if(!this.state.inLink&&(a=this.tokenizer.url(e))){e=e.substring(a.raw.length),t.push(a);continue}let c=e;if(this.options.extensions?.startInline){let p=1/0,k=e.slice(1),h;this.options.extensions.startInline.forEach(R=>{h=R.call({lexer:this},k),typeof h=="number"&&h>=0&&(p=Math.min(p,h))}),p<1/0&&p>=0&&(c=e.substring(0,p+1))}if(a=this.tokenizer.inlineText(c)){e=e.substring(a.raw.length),a.raw.slice(-1)!=="_"&&(o=a.raw.slice(-1)),i=!0;let p=t.at(-1);p?.type==="text"?(p.raw+=a.raw,p.text+=a.text):t.push(a);continue}if(e){this.infiniteLoopError(e.charCodeAt(0));break}}return t}infiniteLoopError(e){let t="Infinite loop on byte: "+e;if(this.options.silent)console.error(t);else throw new Error(t)}};var y=class{options;parser;constructor(e){this.options=e||T}space(e){return""}code({text:e,lang:t,escaped:n}){let s=(t||"").match(m.notSpaceStart)?.[0],r=e.replace(m.endingNewline,"")+` +`;return s?'
'+(n?r:O(r,!0))+`
+`:"
"+(n?r:O(r,!0))+`
+`}blockquote({tokens:e}){return`
+${this.parser.parse(e)}
+`}html({text:e}){return e}def(e){return""}heading({tokens:e,depth:t}){return`${this.parser.parseInline(e)} +`}hr(e){return`
+`}list(e){let t=e.ordered,n=e.start,s="";for(let o=0;o +`+s+" +`}listitem(e){return`
  • ${this.parser.parse(e.tokens)}
  • +`}checkbox({checked:e}){return" '}paragraph({tokens:e}){return`

    ${this.parser.parseInline(e)}

    +`}table(e){let t="",n="";for(let r=0;r${s}`),` + +`+t+` +`+s+`
    +`}tablerow({text:e}){return` +${e} +`}tablecell(e){let t=this.parser.parseInline(e.tokens),n=e.header?"th":"td";return(e.align?`<${n} align="${e.align}">`:`<${n}>`)+t+` +`}strong({tokens:e}){return`${this.parser.parseInline(e)}`}em({tokens:e}){return`${this.parser.parseInline(e)}`}codespan({text:e}){return`${O(e,!0)}`}br(e){return"
    "}del({tokens:e}){return`${this.parser.parseInline(e)}`}link({href:e,title:t,tokens:n}){let s=this.parser.parseInline(n),r=Y(e);if(r===null)return s;e=r;let i='
    ",i}image({href:e,title:t,text:n,tokens:s}){s&&(n=this.parser.parseInline(s,this.parser.textRenderer));let r=Y(e);if(r===null)return O(n);e=r;let i=`${O(n)}{let o=r[i].flat(1/0);n=n.concat(this.walkTokens(o,t))}):r.tokens&&(n=n.concat(this.walkTokens(r.tokens,t)))}}return n}use(...e){let t=this.defaults.extensions||{renderers:{},childTokens:{}};return e.forEach(n=>{let s={...n};if(s.async=this.defaults.async||s.async||!1,n.extensions&&(n.extensions.forEach(r=>{if(!r.name)throw new Error("extension name required");if("renderer"in r){let i=t.renderers[r.name];i?t.renderers[r.name]=function(...o){let u=r.renderer.apply(this,o);return u===!1&&(u=i.apply(this,o)),u}:t.renderers[r.name]=r.renderer}if("tokenizer"in r){if(!r.level||r.level!=="block"&&r.level!=="inline")throw new Error("extension level must be 'block' or 'inline'");let i=t[r.level];i?i.unshift(r.tokenizer):t[r.level]=[r.tokenizer],r.start&&(r.level==="block"?t.startBlock?t.startBlock.push(r.start):t.startBlock=[r.start]:r.level==="inline"&&(t.startInline?t.startInline.push(r.start):t.startInline=[r.start]))}"childTokens"in r&&r.childTokens&&(t.childTokens[r.name]=r.childTokens)}),s.extensions=t),n.renderer){let r=this.defaults.renderer||new y(this.defaults);for(let i in n.renderer){if(!(i in r))throw new Error(`renderer '${i}' does not exist`);if(["options","parser"].includes(i))continue;let o=i,u=n.renderer[o],a=r[o];r[o]=(...c)=>{let p=u.apply(r,c);return p===!1&&(p=a.apply(r,c)),p||""}}s.renderer=r}if(n.tokenizer){let r=this.defaults.tokenizer||new w(this.defaults);for(let i in n.tokenizer){if(!(i in r))throw new Error(`tokenizer '${i}' does not exist`);if(["options","rules","lexer"].includes(i))continue;let o=i,u=n.tokenizer[o],a=r[o];r[o]=(...c)=>{let p=u.apply(r,c);return p===!1&&(p=a.apply(r,c)),p}}s.tokenizer=r}if(n.hooks){let r=this.defaults.hooks||new P;for(let i in n.hooks){if(!(i in r))throw new Error(`hook '${i}' does not exist`);if(["options","block"].includes(i))continue;let o=i,u=n.hooks[o],a=r[o];P.passThroughHooks.has(i)?r[o]=c=>{if(this.defaults.async&&P.passThroughHooksRespectAsync.has(i))return(async()=>{let k=await u.call(r,c);return a.call(r,k)})();let p=u.call(r,c);return a.call(r,p)}:r[o]=(...c)=>{if(this.defaults.async)return(async()=>{let k=await u.apply(r,c);return k===!1&&(k=await a.apply(r,c)),k})();let p=u.apply(r,c);return p===!1&&(p=a.apply(r,c)),p}}s.hooks=r}if(n.walkTokens){let r=this.defaults.walkTokens,i=n.walkTokens;s.walkTokens=function(o){let u=[];return u.push(i.call(this,o)),r&&(u=u.concat(r.call(this,o))),u}}this.defaults={...this.defaults,...s}}),this}setOptions(e){return this.defaults={...this.defaults,...e},this}lexer(e,t){return x.lex(e,t??this.defaults)}parser(e,t){return b.parse(e,t??this.defaults)}parseMarkdown(e){return(n,s)=>{let r={...s},i={...this.defaults,...r},o=this.onError(!!i.silent,!!i.async);if(this.defaults.async===!0&&r.async===!1)return o(new Error("marked(): The async option was set to true by an extension. Remove async: false from the parse options object to return a Promise."));if(typeof n>"u"||n===null)return o(new Error("marked(): input parameter is undefined or null"));if(typeof n!="string")return o(new Error("marked(): input parameter is of type "+Object.prototype.toString.call(n)+", string expected"));if(i.hooks&&(i.hooks.options=i,i.hooks.block=e),i.async)return(async()=>{let u=i.hooks?await i.hooks.preprocess(n):n,c=await(i.hooks?await i.hooks.provideLexer(e):e?x.lex:x.lexInline)(u,i),p=i.hooks?await i.hooks.processAllTokens(c):c;i.walkTokens&&await Promise.all(this.walkTokens(p,i.walkTokens));let h=await(i.hooks?await i.hooks.provideParser(e):e?b.parse:b.parseInline)(p,i);return i.hooks?await i.hooks.postprocess(h):h})().catch(o);try{i.hooks&&(n=i.hooks.preprocess(n));let a=(i.hooks?i.hooks.provideLexer(e):e?x.lex:x.lexInline)(n,i);i.hooks&&(a=i.hooks.processAllTokens(a)),i.walkTokens&&this.walkTokens(a,i.walkTokens);let p=(i.hooks?i.hooks.provideParser(e):e?b.parse:b.parseInline)(a,i);return i.hooks&&(p=i.hooks.postprocess(p)),p}catch(u){return o(u)}}}onError(e,t){return n=>{if(n.message+=` +Please report this to https://github.com/markedjs/marked.`,e){let s="

    An error occurred:

    "+O(n.message+"",!0)+"
    ";return t?Promise.resolve(s):s}if(t)return Promise.reject(n);throw n}}};var M=new C;function g(l,e){return M.parse(l,e)}g.options=g.setOptions=function(l){return M.setOptions(l),g.defaults=M.defaults,Q(g.defaults),g};g.getDefaults=_;g.defaults=T;g.use=function(...l){return M.use(...l),g.defaults=M.defaults,Q(g.defaults),g};g.walkTokens=function(l,e){return M.walkTokens(l,e)};g.parseInline=M.parseInline;g.Parser=b;g.parser=b.parse;g.Renderer=y;g.TextRenderer=S;g.Lexer=x;g.lexer=x.lex;g.Tokenizer=w;g.Hooks=P;g.parse=g;var ht=g.options,kt=g.setOptions,dt=g.use,gt=g.walkTokens,ft=g.parseInline,mt=g,xt=b.parse,bt=x.lex; + +if(__exports != exports)module.exports = exports;return module.exports})); diff --git a/packages/coding-agent/src/core/extensions/index.ts b/packages/coding-agent/src/core/extensions/index.ts new file mode 100644 index 0000000000000000000000000000000000000000..f19816a9a634d6dcc385500d88ea3514bb06d93c --- /dev/null +++ b/packages/coding-agent/src/core/extensions/index.ts @@ -0,0 +1,193 @@ +/** + * Extension system for lifecycle events and custom tools. + */ + +export type { SlashCommandInfo, SlashCommandSource } from "../slash-commands.ts"; +export type { SourceInfo } from "../source-info.ts"; +export { + createExtensionRuntime, + discoverAndLoadExtensions, + loadExtensionFromFactory, + loadExtensions, +} from "./loader.ts"; +export type { + ExtensionErrorListener, + ForkHandler, + NavigateTreeHandler, + NewSessionHandler, + ShutdownHandler, + SwitchSessionHandler, +} from "./runner.ts"; +export { ExtensionRunner } from "./runner.ts"; +export type { + AfterProviderResponseEvent, + AgentEndEvent, + AgentSettledEvent, + AgentStartEvent, + // Re-exports + AgentToolResult, + AgentToolUpdateCallback, + AppendEntryHandler, + // App keybindings (for custom editors) + AppKeybinding, + AutocompleteProviderFactory, + // Events - Tool (ToolCallEvent types) + BashToolCallEvent, + BashToolResultEvent, + BeforeAgentStartEvent, + BeforeAgentStartEventResult, + BeforeProviderHeadersEvent, + BeforeProviderRequestEvent, + BeforeProviderRequestEventResult, + BuildSystemPromptOptions, + // Context + CompactOptions, + // Events - Agent + ContextEvent, + // Event Results + ContextEventResult, + ContextUsage, + CustomToolCallEvent, + CustomToolResultEvent, + EditorFactory, + EditToolCallEvent, + EditToolResultEvent, + // Message and Entry Rendering + EntryRenderer, + EntryRenderOptions, + ExecOptions, + ExecResult, + Extension, + ExtensionActions, + // API + ExtensionAPI, + ExtensionCommandContext, + ExtensionCommandContextActions, + ExtensionContext, + ExtensionContextActions, + // Errors + ExtensionError, + ExtensionEvent, + ExtensionFactory, + ExtensionFlag, + ExtensionHandler, + ExtensionMode, + // Runtime + ExtensionRuntime, + ExtensionShortcut, + ExtensionUIContext, + ExtensionUIDialogOptions, + ExtensionWidgetOptions, + FindToolCallEvent, + FindToolResultEvent, + GetActiveToolsHandler, + GetAllToolsHandler, + GetCommandsHandler, + GetThinkingLevelHandler, + GrepToolCallEvent, + GrepToolResultEvent, + InlineExtension, + // Events - Input + InputEvent, + InputEventResult, + InputSource, + KeybindingsManager, + LoadExtensionsResult, + LsToolCallEvent, + LsToolResultEvent, + MarkdownTransformContext, + MarkdownTransformer, + // Events - Message + MessageEndEvent, + MessageRenderer, + MessageRenderOptions, + MessageStartEvent, + MessageUpdateEvent, + ModelSelectEvent, + ModelSelectSource, + PowerShellToolCallEvent, + PowerShellToolResultEvent, + ProjectTrustContext, + ProjectTrustEvent, + ProjectTrustEventDecision, + ProjectTrustEventResult, + ProjectTrustHandler, + // Provider Registration + ProviderConfig, + ProviderModelConfig, + ReadToolCallEvent, + ReadToolResultEvent, + // Commands + RegisteredCommand, + RegisteredTool, + ReplacedSessionContext, + ResolvedCommand, + // Events - Resources + ResourcesDiscoverEvent, + ResourcesDiscoverResult, + SendMessageHandler, + SendUserMessageHandler, + SessionBeforeCompactEvent, + SessionBeforeCompactResult, + SessionBeforeForkEvent, + SessionBeforeForkResult, + SessionBeforeSwitchEvent, + SessionBeforeSwitchResult, + SessionBeforeTreeEvent, + SessionBeforeTreeResult, + SessionCompactEvent, + SessionCompactFailedEvent, + SessionEvent, + SessionInfoChangedEvent, + SessionShutdownEvent, + // Events - Session + SessionStartEvent, + SessionTreeEvent, + SetActiveToolsHandler, + SetLabelHandler, + SetModelHandler, + SetThinkingLevelHandler, + TerminalInputHandler, + // Events - Tool + ToolCallEvent, + ToolCallEventResult, + // Tools + ToolDefinition, + // Events - Tool Execution + ToolExecutionEndEvent, + // Tool execution mode + ToolExecutionMode, + ToolExecutionStartEvent, + ToolExecutionUpdateEvent, + ToolInfo, + ToolRenderResultOptions, + ToolResultEvent, + ToolResultEventResult, + TreePreparation, + TurnEndEvent, + TurnStartEvent, + UIPromptEndEvent, + UIPromptKind, + UIPromptStartEvent, + // Events - User Bash + UserBashEvent, + UserBashEventResult, + WidgetPlacement, + WorkingIndicatorOptions, + WriteToolCallEvent, + WriteToolResultEvent, +} from "./types.ts"; +// Type guards +export { + defineTool, + isBashToolResult, + isEditToolResult, + isFindToolResult, + isGrepToolResult, + isLsToolResult, + isPowerShellToolResult, + isReadToolResult, + isToolCallEventType, + isWriteToolResult, +} from "./types.ts"; +export { wrapRegisteredTool, wrapRegisteredTools } from "./wrapper.ts"; diff --git a/packages/coding-agent/src/core/extensions/loader.ts b/packages/coding-agent/src/core/extensions/loader.ts new file mode 100644 index 0000000000000000000000000000000000000000..d06f801a4e76027d3373d112f03cfc0a9e3e747b --- /dev/null +++ b/packages/coding-agent/src/core/extensions/loader.ts @@ -0,0 +1,809 @@ +/** + * Extension loader - loads TypeScript extension modules using jiti. + * + */ + +import * as fs from "node:fs"; +import { createRequire } from "node:module"; +import * as path from "node:path"; +import { fileURLToPath } from "node:url"; +import * as _bundledPiAgentCore from "@earendil-works/pi-agent-core"; +import type { Provider } from "@earendil-works/pi-ai"; +import * as _bundledPiAiCompat from "@earendil-works/pi-ai/compat"; +import * as _bundledPiAiOauth from "@earendil-works/pi-ai/oauth"; +import * as _bundledPiAiProviders from "@earendil-works/pi-ai/providers/all"; +import type { KeyId } from "@earendil-works/pi-tui"; +import * as _bundledPiTui from "@earendil-works/pi-tui"; +import { createJiti } from "jiti/static"; +// Static imports of packages that extensions may use. +// These MUST be static so Bun bundles them into the compiled binary. +// The virtualModules option then makes them available to extensions. +import * as _bundledTypebox from "typebox"; +import * as _bundledTypeboxCompile from "typebox/compile"; +import * as _bundledTypeboxValue from "typebox/value"; +import { CONFIG_DIR_NAME, getAgentDir, isBunBinary, isBundledNode } from "../../config.ts"; +// NOTE: This import works because loader.ts exports are NOT re-exported from index.ts, +// avoiding a circular dependency. Extensions can import from @earendil-works/pi-coding-agent. +import * as _bundledPiCodingAgent from "../../index.ts"; +import { resolvePath } from "../../utils/paths.ts"; +import { createEventBus, type EventBus } from "../event-bus.ts"; +import type { ExecOptions } from "../exec.ts"; +import { execCommand } from "../exec.ts"; +import { readPiManifest } from "../pi-manifest.ts"; +import { createSyntheticSourceInfo } from "../source-info.ts"; +import { time } from "../timings.ts"; +import type { + EntryRenderer, + Extension, + ExtensionAPI, + ExtensionFactory, + ExtensionRuntime, + LoadExtensionsResult, + MarkdownTransformer, + MessageRenderer, + ProviderConfig, + RegisteredCommand, + ToolDefinition, +} from "./types.ts"; + +/** Modules available to extensions via virtualModules (for compiled binaries) */ +const VIRTUAL_MODULES: Record = { + typebox: _bundledTypebox, + "typebox/compile": _bundledTypeboxCompile, + "typebox/value": _bundledTypeboxValue, + "@sinclair/typebox": _bundledTypebox, + "@sinclair/typebox/compile": _bundledTypeboxCompile, + "@sinclair/typebox/value": _bundledTypeboxValue, + "@earendil-works/pi-agent-core": _bundledPiAgentCore, + "@earendil-works/pi-tui": _bundledPiTui, + // Extensions resolve the pi-ai root to the compat entrypoint (a strict + // superset of the core entrypoint): existing extensions using the old + // global API keep working at runtime until compat is removed. + "@earendil-works/pi-ai": _bundledPiAiCompat, + "@earendil-works/pi-ai/compat": _bundledPiAiCompat, + "@earendil-works/pi-ai/oauth": _bundledPiAiOauth, + "@earendil-works/pi-ai/providers/all": _bundledPiAiProviders, + "@earendil-works/pi-coding-agent": _bundledPiCodingAgent, + "@mariozechner/pi-agent-core": _bundledPiAgentCore, + "@mariozechner/pi-tui": _bundledPiTui, + "@mariozechner/pi-ai": _bundledPiAiCompat, + "@mariozechner/pi-ai/compat": _bundledPiAiCompat, + "@mariozechner/pi-ai/oauth": _bundledPiAiOauth, + "@mariozechner/pi-ai/providers/all": _bundledPiAiProviders, + "@mariozechner/pi-coding-agent": _bundledPiCodingAgent, +}; + +const require = createRequire(import.meta.url); + +const isNodeSeaBinary = + ("sea" in process.features && process.features.sea === true) || + process.getBuiltinModule("node:sea")?.isSea() === true; +const isTypeScriptSourceRuntime = !isBunBinary && path.extname(fileURLToPath(import.meta.url)) === ".ts"; + +/** + * Get aliases for jiti (used in built Node.js mode). + * In compiled binary mode, virtualModules is used instead. + */ +let _aliases: Record | null = null; + +function getAliases(): Record { + if (_aliases) return _aliases; + + const __dirname = path.dirname(fileURLToPath(import.meta.url)); + const packageIndex = path.resolve(__dirname, "../..", "index.js"); + + const typeboxEntry = require.resolve("typebox"); + const typeboxCompileEntry = require.resolve("typebox/compile"); + const typeboxValueEntry = require.resolve("typebox/value"); + + const packagesRoot = path.resolve(__dirname, "../../../../"); + const resolveWorkspaceOrImport = (workspaceRelativePath: string, specifier: string): string => { + const workspacePath = path.join(packagesRoot, workspaceRelativePath); + if (fs.existsSync(workspacePath)) { + return workspacePath; + } + return fileURLToPath(import.meta.resolve(specifier)); + }; + + const piCodingAgentEntry = packageIndex; + const piAgentCoreEntry = resolveWorkspaceOrImport("agent/dist/index.js", "@earendil-works/pi-agent-core"); + const piTuiEntry = resolveWorkspaceOrImport("tui/dist/index.js", "@earendil-works/pi-tui"); + // Extensions resolve the pi-ai root to the compat entrypoint (a strict + // superset of the core entrypoint): existing extensions using the old + // global API keep working at runtime until compat is removed. + const piAiCompatEntry = resolveWorkspaceOrImport("ai/dist/compat.js", "@earendil-works/pi-ai/compat"); + const piAiOauthEntry = resolveWorkspaceOrImport("ai/dist/oauth.js", "@earendil-works/pi-ai/oauth"); + const piAiProvidersEntry = resolveWorkspaceOrImport( + "ai/dist/providers/all.js", + "@earendil-works/pi-ai/providers/all", + ); + + _aliases = { + "@earendil-works/pi-coding-agent": piCodingAgentEntry, + "@earendil-works/pi-agent-core": piAgentCoreEntry, + "@earendil-works/pi-tui": piTuiEntry, + "@earendil-works/pi-ai/providers/all": piAiProvidersEntry, + "@earendil-works/pi-ai/compat": piAiCompatEntry, + "@earendil-works/pi-ai/oauth": piAiOauthEntry, + "@earendil-works/pi-ai": piAiCompatEntry, + "@mariozechner/pi-coding-agent": piCodingAgentEntry, + "@mariozechner/pi-agent-core": piAgentCoreEntry, + "@mariozechner/pi-tui": piTuiEntry, + "@mariozechner/pi-ai/providers/all": piAiProvidersEntry, + "@mariozechner/pi-ai/compat": piAiCompatEntry, + "@mariozechner/pi-ai/oauth": piAiOauthEntry, + "@mariozechner/pi-ai": piAiCompatEntry, + typebox: typeboxEntry, + "typebox/compile": typeboxCompileEntry, + "typebox/value": typeboxValueEntry, + "@sinclair/typebox": typeboxEntry, + "@sinclair/typebox/compile": typeboxCompileEntry, + "@sinclair/typebox/value": typeboxValueEntry, + }; + + return _aliases; +} + +type HandlerFn = (...args: unknown[]) => Promise; + +let extensionCacheCwd: string | undefined; +let extensionCacheGeneration = 0; +const extensionCache = new Map(); + +interface ExtensionCacheToken { + cwd: string; + generation: number; +} + +export function clearExtensionCache(): void { + extensionCache.clear(); + extensionCacheCwd = undefined; + extensionCacheGeneration++; +} + +function useExtensionCacheCwd(cwd: string): ExtensionCacheToken { + const resolvedCwd = resolvePath(cwd); + if (extensionCacheCwd !== undefined && extensionCacheCwd !== resolvedCwd) { + clearExtensionCache(); + } + extensionCacheCwd = resolvedCwd; + return { cwd: resolvedCwd, generation: extensionCacheGeneration }; +} + +/** + * Create a runtime with throwing stubs for action methods. + * Runner.bindCore() replaces these with real implementations. + */ +export function createExtensionRuntime(): ExtensionRuntime { + const notInitialized = () => { + throw new Error("Extension runtime not initialized. Action methods cannot be called during extension loading."); + }; + const state: { staleMessage?: string } = {}; + const eventBusUnsubscribers = new Set<() => void>(); + const assertActive = () => { + if (state.staleMessage) { + throw new Error(state.staleMessage); + } + }; + + const runtime: ExtensionRuntime = { + sendMessage: notInitialized, + sendUserMessage: notInitialized, + appendEntry: notInitialized, + setSessionName: notInitialized, + getSessionName: notInitialized, + setLabel: notInitialized, + getActiveTools: notInitialized, + getAllTools: notInitialized, + setActiveTools: notInitialized, + // registerTool() is valid during extension load; refresh is only needed post-bind. + refreshTools: () => {}, + getCommands: notInitialized, + setModel: () => Promise.reject(new Error("Extension runtime not initialized")), + getThinkingLevel: notInitialized, + setThinkingLevel: notInitialized, + flagValues: new Map(), + pendingProviderRegistrations: [], + pendingNativeProviderRegistrations: [], + assertActive, + invalidate: (message) => { + if (state.staleMessage) return; + state.staleMessage = + message ?? + "This extension ctx is stale after session replacement or reload. Do not use a captured pi or command ctx after ctx.newSession(), ctx.fork(), ctx.switchSession(), or ctx.reload(). For newSession, fork, and switchSession, move post-replacement work into withSession and use the ctx passed to withSession. For reload, do not use the old ctx after await ctx.reload()."; + for (const unsubscribe of eventBusUnsubscribers) unsubscribe(); + eventBusUnsubscribers.clear(); + }, + trackEventBusSubscription: (unsubscribe) => { + let active = true; + const trackedUnsubscribe = () => { + if (!active) return; + active = false; + eventBusUnsubscribers.delete(trackedUnsubscribe); + unsubscribe(); + }; + eventBusUnsubscribers.add(trackedUnsubscribe); + return trackedUnsubscribe; + }, + // Pre-bind: queue registrations so bindCore() can flush them once the + // model registry is available. bindCore() replaces both with direct calls. + registerProvider: (name, config, extensionPath = "") => { + runtime.pendingProviderRegistrations.push({ name, config, extensionPath }); + }, + registerNativeProvider: (provider, extensionPath = "") => { + runtime.pendingNativeProviderRegistrations.push({ provider, extensionPath }); + }, + unregisterProvider: (name) => { + runtime.pendingProviderRegistrations = runtime.pendingProviderRegistrations.filter((r) => r.name !== name); + runtime.pendingNativeProviderRegistrations = runtime.pendingNativeProviderRegistrations.filter( + (r) => r.provider.id !== name, + ); + }, + }; + + return runtime; +} + +/** + * Create the ExtensionAPI for an extension. + * Registration methods write to the extension object. + * Action methods delegate to the shared runtime. + */ +function createExtensionAPI( + extension: Extension, + runtime: ExtensionRuntime, + cwd: string, + eventBus: EventBus, +): { api: ExtensionAPI; commit: () => void; discard: () => void } { + const pendingFlagValues = new Map(); + const pendingRuntimeChanges: Array<() => void> = []; + const loadingUnsubscribers: Array<() => void> = []; + let state: "loading" | "active" | "failed" = "loading"; + const assertActive = () => { + if (state === "failed") { + throw new Error(`Extension "${extension.path}" failed to load and its API is no longer active.`); + } + runtime.assertActive(); + }; + const applyRuntimeChange = (change: () => void) => { + if (state === "loading") pendingRuntimeChanges.push(change); + else change(); + }; + const clearPending = () => { + pendingFlagValues.clear(); + pendingRuntimeChanges.length = 0; + loadingUnsubscribers.length = 0; + }; + + const api = { + // Registration methods - write to extension + on(event: string, handler: HandlerFn): void { + assertActive(); + const list = extension.handlers.get(event) ?? []; + list.push(handler); + extension.handlers.set(event, list); + }, + + registerTool(tool: ToolDefinition): void { + assertActive(); + if (typeof tool.parameters !== "object" || tool.parameters === null || Array.isArray(tool.parameters)) { + throw new Error( + `Tool "${tool.name}" registered by extension "${extension.path}" must define an object parameter schema.`, + ); + } + extension.tools.set(tool.name, { + definition: tool, + sourceInfo: extension.sourceInfo, + }); + runtime.refreshTools(); + }, + + registerCommand(name: string, options: Omit): void { + assertActive(); + extension.commands.set(name, { + name, + sourceInfo: extension.sourceInfo, + ...options, + }); + }, + + registerShortcut( + shortcut: KeyId, + options: { + description?: string; + handler: (ctx: import("./types.ts").ExtensionContext) => Promise | void; + }, + ): void { + assertActive(); + extension.shortcuts.set(shortcut, { shortcut, extensionPath: extension.path, ...options }); + }, + + registerFlag( + name: string, + options: { description?: string; type: "boolean" | "string"; default?: boolean | string }, + ): void { + assertActive(); + if (options.default !== undefined && typeof options.default !== options.type) { + throw new Error( + `Invalid default for flag "${name}": expected ${options.type}, got ${typeof options.default}`, + ); + } + extension.flags.set(name, { name, extensionPath: extension.path, ...options }); + if (options.default !== undefined && !runtime.flagValues.has(name)) { + if (state === "loading") { + if (!pendingFlagValues.has(name)) pendingFlagValues.set(name, options.default); + } else { + runtime.flagValues.set(name, options.default); + } + } + }, + + registerMessageRenderer(customType: string, renderer: MessageRenderer): void { + assertActive(); + extension.messageRenderers.set(customType, renderer as MessageRenderer); + }, + + registerMarkdownTransformer(transformer: MarkdownTransformer): void { + assertActive(); + extension.markdownTransformer = transformer; + }, + + registerEntryRenderer(customType: string, renderer: EntryRenderer): void { + assertActive(); + extension.entryRenderers ??= new Map(); + extension.entryRenderers.set(customType, renderer as EntryRenderer); + }, + + // Flag access - checks extension registered it, reads from runtime + getFlag(name: string): boolean | string | undefined { + assertActive(); + if (!extension.flags.has(name)) return undefined; + return runtime.flagValues.has(name) ? runtime.flagValues.get(name) : pendingFlagValues.get(name); + }, + + // Action methods - delegate to shared runtime + sendMessage(message, options): void { + assertActive(); + runtime.sendMessage(message, options); + }, + + sendUserMessage(content, options): void { + assertActive(); + runtime.sendUserMessage(content, options); + }, + + appendEntry(customType: string, data?: unknown): void { + assertActive(); + runtime.appendEntry(customType, data); + }, + + setSessionName(name: string): void { + assertActive(); + runtime.setSessionName(name); + }, + + getSessionName(): string | undefined { + assertActive(); + return runtime.getSessionName(); + }, + + setLabel(entryId: string, label: string | undefined): void { + assertActive(); + runtime.setLabel(entryId, label); + }, + + exec(command: string, args: string[], options?: ExecOptions) { + assertActive(); + return execCommand(command, args, options?.cwd ?? cwd, options); + }, + + getActiveTools(): string[] { + assertActive(); + return runtime.getActiveTools(); + }, + + getAllTools() { + assertActive(); + return runtime.getAllTools(); + }, + + setActiveTools(toolNames: string[]): void { + assertActive(); + runtime.setActiveTools(toolNames); + }, + + getCommands() { + assertActive(); + return runtime.getCommands(); + }, + + setModel(model) { + assertActive(); + return runtime.setModel(model); + }, + + getThinkingLevel() { + assertActive(); + return runtime.getThinkingLevel(); + }, + + setThinkingLevel(level) { + assertActive(); + runtime.setThinkingLevel(level); + }, + + registerProvider(providerOrName: Provider | string, config?: ProviderConfig) { + assertActive(); + if (typeof providerOrName === "string") { + if (!config) throw new Error("Provider config is required when registering by name"); + applyRuntimeChange(() => runtime.registerProvider(providerOrName, config, extension.path)); + return; + } + applyRuntimeChange(() => runtime.registerNativeProvider(providerOrName, extension.path)); + }, + + unregisterProvider(name: string) { + assertActive(); + applyRuntimeChange(() => runtime.unregisterProvider(name, extension.path)); + }, + + events: { + emit(channel, data) { + assertActive(); + eventBus.emit(channel, data); + }, + on(channel, handler) { + assertActive(); + const unsubscribe = runtime.trackEventBusSubscription(eventBus.on(channel, handler)); + if (state === "loading") loadingUnsubscribers.push(unsubscribe); + return unsubscribe; + }, + }, + } as ExtensionAPI; + + return { + api, + commit: () => { + if (state !== "loading") return; + runtime.assertActive(); + for (const [name, value] of pendingFlagValues) { + if (!runtime.flagValues.has(name)) runtime.flagValues.set(name, value); + } + for (const apply of pendingRuntimeChanges) apply(); + state = "active"; + clearPending(); + }, + discard: () => { + if (state !== "loading") return; + state = "failed"; + for (const unsubscribe of loadingUnsubscribers) unsubscribe(); + clearPending(); + }, + }; +} + +function isCurrentCacheToken(cacheToken: ExtensionCacheToken | undefined): cacheToken is ExtensionCacheToken { + return ( + cacheToken !== undefined && + extensionCacheCwd === cacheToken.cwd && + extensionCacheGeneration === cacheToken.generation + ); +} + +async function loadExtensionModule(extensionPath: string, cacheToken?: ExtensionCacheToken) { + if (isCurrentCacheToken(cacheToken)) { + const cachedFactory = extensionCache.get(extensionPath); + if (cachedFactory) { + return cachedFactory; + } + } + + const jiti = createJiti(import.meta.url, { + moduleCache: false, + // Compiled binaries and the bundled Node distribution use embedded modules. + // Source TypeScript reuses host modules and root tsconfig paths. Unbundled + // Node builds use dist aliases. + ...(isBunBinary || isNodeSeaBinary || isBundledNode + ? { virtualModules: VIRTUAL_MODULES, tryNative: false } + : isTypeScriptSourceRuntime + ? { virtualModules: VIRTUAL_MODULES, tsconfigPaths: true } + : { alias: getAliases() }), + }); + + const module = await jiti.import(extensionPath, { default: true }); + const factory = module as ExtensionFactory; + if (typeof factory !== "function") { + return undefined; + } + if (isCurrentCacheToken(cacheToken)) { + extensionCache.set(extensionPath, factory); + } + return factory; +} + +/** + * Create an Extension object with empty collections. + */ +function createExtension(extensionPath: string, resolvedPath: string): Extension { + const source = + extensionPath.startsWith("<") && extensionPath.endsWith(">") + ? extensionPath.slice(1, -1).split(":")[0] || "temporary" + : "local"; + const baseDir = extensionPath.startsWith("<") ? undefined : path.dirname(resolvedPath); + + return { + path: extensionPath, + resolvedPath, + sourceInfo: createSyntheticSourceInfo(extensionPath, { source, baseDir }), + handlers: new Map(), + tools: new Map(), + messageRenderers: new Map(), + entryRenderers: new Map(), + commands: new Map(), + flags: new Map(), + shortcuts: new Map(), + }; +} + +async function initializeExtension( + factory: ExtensionFactory, + extensionPath: string, + resolvedPath: string, + cwd: string, + eventBus: EventBus, + runtime: ExtensionRuntime, +): Promise { + const extension = createExtension(extensionPath, resolvedPath); + const load = createExtensionAPI(extension, runtime, cwd, eventBus); + try { + await factory(load.api); + load.commit(); + } catch (error) { + load.discard(); + throw error; + } + time(`${extensionPath} factory`, "extensions"); + return extension; +} + +async function loadExtension( + extensionPath: string, + cwd: string, + eventBus: EventBus, + runtime: ExtensionRuntime, + cacheToken?: ExtensionCacheToken, +): Promise<{ extension: Extension | null; error: string | null }> { + const resolvedPath = resolvePath(extensionPath, cwd, { normalizeUnicodeSpaces: true }); + + try { + const factory = await loadExtensionModule(resolvedPath, cacheToken); + time(`${extensionPath} module import`, "extensions"); + if (!factory) { + return { extension: null, error: `Extension does not export a valid factory function: ${extensionPath}` }; + } + + const extension = await initializeExtension(factory, extensionPath, resolvedPath, cwd, eventBus, runtime); + + return { extension, error: null }; + } catch (err) { + const message = err instanceof Error ? err.message : String(err); + return { extension: null, error: `Failed to load extension: ${message}` }; + } +} + +/** + * Create an Extension from an inline factory function. + */ +export async function loadExtensionFromFactory( + factory: ExtensionFactory, + cwd: string, + eventBus: EventBus, + runtime: ExtensionRuntime, + extensionPath = "", +): Promise { + const resolvedCwd = resolvePath(cwd); + return initializeExtension(factory, extensionPath, extensionPath, resolvedCwd, eventBus, runtime); +} + +/** + * Load extensions from paths. + */ +async function loadExtensionsInternal( + paths: string[], + cwd: string, + eventBus?: EventBus, + runtime?: ExtensionRuntime, + useCache = false, +): Promise { + const extensions: Extension[] = []; + const errors: Array<{ path: string; error: string }> = []; + const cacheToken = useCache ? useExtensionCacheCwd(cwd) : undefined; + const resolvedCwd = cacheToken?.cwd ?? resolvePath(cwd); + const resolvedEventBus = eventBus ?? createEventBus(); + const resolvedRuntime = runtime ?? createExtensionRuntime(); + + for (const extPath of paths) { + const { extension, error } = await loadExtension( + extPath, + resolvedCwd, + resolvedEventBus, + resolvedRuntime, + cacheToken, + ); + + if (error) { + errors.push({ path: extPath, error }); + continue; + } + + if (extension) { + extensions.push(extension); + } + } + + return { + extensions, + errors, + runtime: resolvedRuntime, + }; +} + +export async function loadExtensions( + paths: string[], + cwd: string, + eventBus?: EventBus, + runtime?: ExtensionRuntime, +): Promise { + return loadExtensionsInternal(paths, cwd, eventBus, runtime); +} + +export async function loadExtensionsCached( + paths: string[], + cwd: string, + eventBus?: EventBus, + runtime?: ExtensionRuntime, +): Promise { + return loadExtensionsInternal(paths, cwd, eventBus, runtime, true); +} + +function isExtensionFile(name: string): boolean { + return name.endsWith(".ts") || name.endsWith(".js"); +} + +/** + * Resolve extension entry points from a directory. + * + * Checks for: + * 1. package.json with "pi.extensions" field -> returns declared paths + * 2. index.ts or index.js -> returns the index file + * + * Returns resolved paths or null if no entry points found. + */ +function resolveExtensionEntries(dir: string): string[] | null { + // Check for package.json with "pi" field first + const packageJsonPath = path.join(dir, "package.json"); + if (fs.existsSync(packageJsonPath)) { + const manifest = readPiManifest(packageJsonPath); + if (manifest?.extensions?.length) { + const entries: string[] = []; + for (const extPath of manifest.extensions) { + const resolvedExtPath = path.resolve(dir, extPath); + if (fs.existsSync(resolvedExtPath)) { + entries.push(resolvedExtPath); + } + } + if (entries.length > 0) { + return entries; + } + } + } + + // Check for index.ts or index.js + const indexTs = path.join(dir, "index.ts"); + const indexJs = path.join(dir, "index.js"); + if (fs.existsSync(indexTs)) { + return [indexTs]; + } + if (fs.existsSync(indexJs)) { + return [indexJs]; + } + + return null; +} + +/** + * Discover extensions in a directory. + * + * Discovery rules: + * 1. Direct files: `extensions/*.ts` or `*.js` → load + * 2. Subdirectory with index: `extensions/* /index.ts` or `index.js` → load + * 3. Subdirectory with package.json: `extensions/* /package.json` with "pi" field → load what it declares + * + * No recursion beyond one level. Complex packages must use package.json manifest. + */ +function discoverExtensionsInDir(dir: string): string[] { + if (!fs.existsSync(dir)) { + return []; + } + + const discovered: string[] = []; + + try { + const entries = fs.readdirSync(dir, { withFileTypes: true }); + + for (const entry of entries) { + const entryPath = path.join(dir, entry.name); + + // 1. Direct files: *.ts or *.js + if ((entry.isFile() || entry.isSymbolicLink()) && isExtensionFile(entry.name)) { + discovered.push(entryPath); + continue; + } + + // 2 & 3. Subdirectories + if (entry.isDirectory() || entry.isSymbolicLink()) { + const entries = resolveExtensionEntries(entryPath); + if (entries) { + discovered.push(...entries); + } + } + } + } catch { + return []; + } + + return discovered; +} + +/** + * Discover and load extensions from standard locations. + */ +export async function discoverAndLoadExtensions( + configuredPaths: string[], + cwd: string, + agentDir: string = getAgentDir(), + eventBus?: EventBus, +): Promise { + const resolvedCwd = resolvePath(cwd); + const resolvedAgentDir = resolvePath(agentDir); + const allPaths: string[] = []; + const seen = new Set(); + + const addPaths = (paths: string[]) => { + for (const p of paths) { + const resolved = path.resolve(p); + if (!seen.has(resolved)) { + seen.add(resolved); + allPaths.push(p); + } + } + }; + + // 1. Project-local extensions: cwd/${CONFIG_DIR_NAME}/extensions/ + const localExtDir = path.join(resolvedCwd, CONFIG_DIR_NAME, "extensions"); + addPaths(discoverExtensionsInDir(localExtDir)); + + // 2. Global extensions: agentDir/extensions/ + const globalExtDir = path.join(resolvedAgentDir, "extensions"); + addPaths(discoverExtensionsInDir(globalExtDir)); + + // 3. Explicitly configured paths + for (const p of configuredPaths) { + const resolved = resolvePath(p, resolvedCwd, { normalizeUnicodeSpaces: true }); + if (fs.existsSync(resolved) && fs.statSync(resolved).isDirectory()) { + // Check for package.json with pi manifest or index.ts + const entries = resolveExtensionEntries(resolved); + if (entries) { + addPaths(entries); + continue; + } + // No explicit entries - discover individual files in directory + addPaths(discoverExtensionsInDir(resolved)); + continue; + } + + addPaths([resolved]); + } + + return loadExtensions(allPaths, resolvedCwd, eventBus); +} diff --git a/packages/coding-agent/src/core/extensions/runner.ts b/packages/coding-agent/src/core/extensions/runner.ts new file mode 100644 index 0000000000000000000000000000000000000000..3346645a10456c36f5fc3051fda1540c4e7d1f8b --- /dev/null +++ b/packages/coding-agent/src/core/extensions/runner.ts @@ -0,0 +1,1286 @@ +/** + * Extension runner - executes extensions and manages their lifecycle. + */ + +import type { AgentMessage } from "@earendil-works/pi-agent-core"; +import type { ImageContent, Model, Provider, ProviderHeaders } from "@earendil-works/pi-ai"; +import type { KeyId } from "@earendil-works/pi-tui"; +import { type Theme, theme } from "../../modes/interactive/theme/theme.ts"; +import type { ResourceDiagnostic } from "../diagnostics.ts"; +import type { KeybindingsConfig } from "../keybindings.ts"; +import type { ModelRegistry } from "../model-registry.ts"; +import type { ScopedModel } from "../model-resolver.ts"; +import type { SessionManager } from "../session-manager.ts"; +import type { BuildSystemPromptOptions } from "../system-prompt.ts"; +import type { + BeforeAgentStartEvent, + BeforeAgentStartEventResult, + BeforeProviderHeadersEvent, + BeforeProviderRequestEvent, + CompactOptions, + ContextEvent, + ContextEventResult, + ContextUsage, + EntryRenderer, + Extension, + ExtensionActions, + ExtensionCommandContext, + ExtensionCommandContextActions, + ExtensionContext, + ExtensionContextActions, + ExtensionError, + ExtensionEvent, + ExtensionFlag, + ExtensionMode, + ExtensionRuntime, + ExtensionShortcut, + ExtensionUIContext, + InputEvent, + InputEventResult, + InputSource, + LoadExtensionsResult, + MarkdownTransformer, + MessageEndEvent, + MessageEndEventResult, + MessageRenderer, + ProjectTrustContext, + ProjectTrustEvent, + ProjectTrustEventResult, + ProviderConfig, + RegisteredCommand, + RegisteredTool, + ReplacedSessionContext, + ResolvedCommand, + ResourcesDiscoverEvent, + ResourcesDiscoverResult, + SessionBeforeCompactResult, + SessionBeforeForkResult, + SessionBeforeSwitchResult, + SessionBeforeTreeResult, + SessionShutdownEvent, + ToolCallEvent, + ToolCallEventResult, + ToolResultEvent, + ToolResultEventResult, + UIPromptKind, + UserBashEvent, + UserBashEventResult, +} from "./types.ts"; + +// Extension shortcuts compete with canonical keybinding ids from keybindings.json. +// Only editor-global shortcuts are reserved here. Picker-specific bindings are not. +const RESERVED_KEYBINDINGS_FOR_EXTENSION_CONFLICTS = [ + "app.interrupt", + "app.clear", + "app.exit", + "app.suspend", + "app.thinking.cycle", + "app.model.cycleForward", + "app.model.cycleBackward", + "app.model.select", + "app.tools.expand", + "app.thinking.toggle", + "app.editor.external", + "app.message.copy", + "app.message.followUp", + "tui.input.submit", + "tui.select.confirm", + "tui.select.cancel", + "tui.input.copy", + "tui.editor.deleteToLineEnd", +] as const; + +type BuiltInKeyBindings = Partial>; + +const buildBuiltinKeybindings = (resolvedKeybindings: KeybindingsConfig): BuiltInKeyBindings => { + const builtinKeybindings = {} as BuiltInKeyBindings; + for (const [keybinding, keys] of Object.entries(resolvedKeybindings)) { + if (keys === undefined) continue; + const keyList = Array.isArray(keys) ? keys : [keys]; + const restrictOverride = (RESERVED_KEYBINDINGS_FOR_EXTENSION_CONFLICTS as readonly string[]).includes(keybinding); + for (const key of keyList) { + const normalizedKey = key.toLowerCase() as KeyId; + // If multiple actions bind the same key, the reserved action wins so extensions + // remain blocked by reserved shortcuts regardless of iteration order. + const existing = builtinKeybindings[normalizedKey]; + if (existing?.restrictOverride && !restrictOverride) continue; + builtinKeybindings[normalizedKey] = { + keybinding, + restrictOverride, + }; + } + } + return builtinKeybindings; +}; + +/** Combined result from all before_agent_start handlers */ +interface BeforeAgentStartCombinedResult { + messages?: NonNullable[]; + systemPrompt?: string; +} + +/** + * Events handled by the generic emit() method. + * Events with dedicated emitXxx() methods are excluded for stronger type safety. + */ +type RunnerEmitEvent = Exclude< + ExtensionEvent, + | ToolCallEvent + | ProjectTrustEvent + | ToolResultEvent + | UserBashEvent + | ContextEvent + | BeforeProviderRequestEvent + | BeforeProviderHeadersEvent + | BeforeAgentStartEvent + | MessageEndEvent + | ResourcesDiscoverEvent + | InputEvent +>; + +type SessionBeforeEvent = Extract< + RunnerEmitEvent, + { type: "session_before_switch" | "session_before_fork" | "session_before_compact" | "session_before_tree" } +>; + +type SessionBeforeEventResult = + | SessionBeforeSwitchResult + | SessionBeforeForkResult + | SessionBeforeCompactResult + | SessionBeforeTreeResult; + +type RunnerEmitResult = TEvent extends { type: "session_before_switch" } + ? SessionBeforeSwitchResult | undefined + : TEvent extends { type: "session_before_fork" } + ? SessionBeforeForkResult | undefined + : TEvent extends { type: "session_before_compact" } + ? SessionBeforeCompactResult | undefined + : TEvent extends { type: "session_before_tree" } + ? SessionBeforeTreeResult | undefined + : undefined; + +export type ExtensionErrorListener = (error: ExtensionError) => void; + +export type NewSessionHandler = (options?: { + parentSession?: string; + setup?: (sessionManager: SessionManager) => Promise; + withSession?: (ctx: ReplacedSessionContext) => Promise; +}) => Promise<{ cancelled: boolean }>; + +export type ForkHandler = ( + entryId: string, + options?: { position?: "before" | "at"; withSession?: (ctx: ReplacedSessionContext) => Promise }, +) => Promise<{ cancelled: boolean }>; + +export type NavigateTreeHandler = ( + targetId: string, + options?: { summarize?: boolean; customInstructions?: string; replaceInstructions?: boolean; label?: string }, +) => Promise<{ cancelled: boolean }>; + +export type SwitchSessionHandler = ( + sessionPath: string, + options?: { withSession?: (ctx: ReplacedSessionContext) => Promise }, +) => Promise<{ cancelled: boolean }>; + +export type ReloadHandler = () => Promise; + +export type ShutdownHandler = () => void; + +/** + * Helper function to emit session_shutdown event to extensions. + * Returns true if the event was emitted, false if there were no handlers. + */ +export async function emitSessionShutdownEvent( + extensionRunner: ExtensionRunner, + event: SessionShutdownEvent, +): Promise { + if (extensionRunner.hasHandlers("session_shutdown")) { + await extensionRunner.emit(event); + return true; + } + return false; +} + +export async function emitProjectTrustEvent( + extensionsResult: LoadExtensionsResult, + event: ProjectTrustEvent, + ctx: ProjectTrustContext, +): Promise<{ result?: ProjectTrustEventResult; errors: ExtensionError[] }> { + const errors: ExtensionError[] = []; + for (const ext of extensionsResult.extensions) { + // A single extension may register multiple handlers for the same event. + // The first project_trust handler that returns yes/no wins; undecided falls through. + const handlers = ext.handlers.get("project_trust"); + if (!handlers || handlers.length === 0) continue; + + for (const handler of handlers) { + try { + const handlerResult = (await handler(event, ctx)) as ProjectTrustEventResult; + if (handlerResult.trusted === "undecided") { + continue; + } + return { result: handlerResult, errors }; + } catch (error) { + errors.push({ + extensionPath: ext.path, + event: event.type, + error: error instanceof Error ? error.message : String(error), + stack: error instanceof Error ? error.stack : undefined, + }); + } + } + } + return { errors }; +} + +const noOpUIContext: ExtensionUIContext = { + select: async () => undefined, + confirm: async () => false, + input: async () => undefined, + notify: () => {}, + onTerminalInput: () => () => {}, + setStatus: () => {}, + setWorkingMessage: () => {}, + setWorkingVisible: () => {}, + setWorkingIndicator: () => {}, + setHiddenThinkingLabel: () => {}, + setWidget: () => {}, + setFooter: () => {}, + setHeader: () => {}, + setTitle: () => {}, + custom: async () => undefined as never, + pasteToEditor: () => {}, + setEditorText: () => {}, + getEditorText: () => "", + editor: async () => undefined, + addAutocompleteProvider: () => {}, + setEditorComponent: () => {}, + getEditorComponent: () => undefined, + get theme() { + return theme; + }, + getAllThemes: () => [], + getTheme: () => undefined, + setTheme: (_theme: string | Theme) => ({ success: false, error: "UI not available" }), + getToolsExpanded: () => false, + setToolsExpanded: () => {}, +}; + +export class ExtensionRunner { + private extensions: Extension[]; + private runtime: ExtensionRuntime; + private uiContext: ExtensionUIContext; + private mode: ExtensionMode = "print"; + private cwd: string; + private sessionManager: SessionManager; + private modelRegistry: ModelRegistry; + private errorListeners: Set = new Set(); + private getModel: () => Model | undefined = () => undefined; + private getScopedModels: () => readonly ScopedModel[] = () => []; + private isIdleFn: () => boolean = () => true; + private isProjectTrustedFn: () => boolean = () => true; + private getSignalFn: () => AbortSignal | undefined = () => undefined; + private waitForIdleFn: () => Promise = async () => {}; + private abortFn: () => void = () => {}; + private hasPendingMessagesFn: () => boolean = () => false; + private getContextUsageFn: () => ContextUsage | undefined = () => undefined; + private compactFn: (options?: CompactOptions) => void = () => {}; + private getSystemPromptFn: () => string = () => ""; + private getSystemPromptOptionsFn: () => BuildSystemPromptOptions = () => ({ cwd: this.cwd }); + private newSessionHandler: NewSessionHandler = async () => ({ cancelled: false }); + private forkHandler: ForkHandler = async () => ({ cancelled: false }); + private navigateTreeHandler: NavigateTreeHandler = async () => ({ cancelled: false }); + private switchSessionHandler: SwitchSessionHandler = async () => ({ cancelled: false }); + private reloadHandler: ReloadHandler = async () => {}; + private shutdownHandler: ShutdownHandler = () => {}; + private shortcutDiagnostics: ResourceDiagnostic[] = []; + private commandDiagnostics: ResourceDiagnostic[] = []; + private staleMessage: string | undefined; + private uiPromptDepth = 0; + private activeUIPrompt: { kind: UIPromptKind; title?: string } | undefined; + + constructor( + extensions: Extension[], + runtime: ExtensionRuntime, + cwd: string, + sessionManager: SessionManager, + modelRegistry: ModelRegistry, + ) { + this.extensions = extensions; + this.runtime = runtime; + this.uiContext = noOpUIContext; + this.cwd = cwd; + this.sessionManager = sessionManager; + this.modelRegistry = modelRegistry; + } + + bindCore( + actions: ExtensionActions, + contextActions: ExtensionContextActions, + providerActions?: { + registerProvider?: (name: string, config: ProviderConfig) => void; + registerNativeProvider?: (provider: Provider) => void; + unregisterProvider?: (name: string) => void; + }, + ): void { + // Copy actions into the shared runtime (all extension APIs reference this) + this.runtime.sendMessage = actions.sendMessage; + this.runtime.sendUserMessage = actions.sendUserMessage; + this.runtime.appendEntry = actions.appendEntry; + this.runtime.setSessionName = actions.setSessionName; + this.runtime.getSessionName = actions.getSessionName; + this.runtime.setLabel = actions.setLabel; + this.runtime.getActiveTools = actions.getActiveTools; + this.runtime.getAllTools = actions.getAllTools; + this.runtime.setActiveTools = actions.setActiveTools; + this.runtime.refreshTools = actions.refreshTools; + this.runtime.getCommands = actions.getCommands; + this.runtime.setModel = actions.setModel; + this.runtime.getThinkingLevel = actions.getThinkingLevel; + this.runtime.setThinkingLevel = actions.setThinkingLevel; + + // Context actions (required) + this.getModel = contextActions.getModel; + this.getScopedModels = contextActions.getScopedModels; + this.isIdleFn = contextActions.isIdle; + this.isProjectTrustedFn = contextActions.isProjectTrusted; + this.getSignalFn = contextActions.getSignal; + this.abortFn = contextActions.abort; + this.hasPendingMessagesFn = contextActions.hasPendingMessages; + this.shutdownHandler = contextActions.shutdown; + this.getContextUsageFn = contextActions.getContextUsage; + this.compactFn = contextActions.compact; + this.getSystemPromptFn = contextActions.getSystemPrompt; + this.getSystemPromptOptionsFn = contextActions.getSystemPromptOptions ?? (() => ({ cwd: this.cwd })); + + // Flush provider registrations queued during extension loading + for (const { name, config, extensionPath } of this.runtime.pendingProviderRegistrations) { + try { + if (providerActions?.registerProvider) { + providerActions.registerProvider(name, config); + } else { + this.modelRegistry.registerProvider(name, config); + } + } catch (err) { + this.emitError({ + extensionPath, + event: "register_provider", + error: err instanceof Error ? err.message : String(err), + stack: err instanceof Error ? err.stack : undefined, + }); + } + } + this.runtime.pendingProviderRegistrations = []; + for (const { provider, extensionPath } of this.runtime.pendingNativeProviderRegistrations) { + try { + if (providerActions?.registerNativeProvider) { + providerActions.registerNativeProvider(provider); + } else { + this.modelRegistry.registerProvider(provider); + } + } catch (err) { + this.emitError({ + extensionPath, + event: "register_provider", + error: err instanceof Error ? err.message : String(err), + stack: err instanceof Error ? err.stack : undefined, + }); + } + } + this.runtime.pendingNativeProviderRegistrations = []; + + // From this point on, provider registration/unregistration takes effect immediately + // without requiring a /reload. + this.runtime.registerProvider = (name, config) => { + if (providerActions?.registerProvider) { + providerActions.registerProvider(name, config); + return; + } + this.modelRegistry.registerProvider(name, config); + }; + this.runtime.registerNativeProvider = (provider) => { + if (providerActions?.registerNativeProvider) { + providerActions.registerNativeProvider(provider); + return; + } + this.modelRegistry.registerProvider(provider); + }; + this.runtime.unregisterProvider = (name) => { + if (providerActions?.unregisterProvider) { + providerActions.unregisterProvider(name); + return; + } + this.modelRegistry.unregisterProvider(name); + }; + } + + bindCommandContext(actions?: ExtensionCommandContextActions): void { + if (actions) { + this.waitForIdleFn = actions.waitForIdle; + this.newSessionHandler = actions.newSession; + this.forkHandler = actions.fork; + this.navigateTreeHandler = actions.navigateTree; + this.switchSessionHandler = actions.switchSession; + this.reloadHandler = actions.reload; + return; + } + + this.waitForIdleFn = async () => {}; + this.newSessionHandler = async () => ({ cancelled: false }); + this.forkHandler = async () => ({ cancelled: false }); + this.navigateTreeHandler = async () => ({ cancelled: false }); + this.switchSessionHandler = async () => ({ cancelled: false }); + this.reloadHandler = async () => {}; + } + + setUIContext(uiContext?: ExtensionUIContext, mode: ExtensionMode = "print"): void { + this.uiContext = uiContext ? this.wrapUIPromptContext(uiContext) : noOpUIContext; + this.mode = mode; + } + + private wrapUIPromptContext(ui: ExtensionUIContext): ExtensionUIContext { + return { + ...ui, + select: (title, options, opts) => this.withUIPrompt("select", title, () => ui.select(title, options, opts)), + confirm: (title, message, opts) => this.withUIPrompt("confirm", title, () => ui.confirm(title, message, opts)), + input: (title, placeholder, opts) => + this.withUIPrompt("input", title, () => ui.input(title, placeholder, opts)), + editor: (title, prefill) => this.withUIPrompt("editor", title, () => ui.editor(title, prefill)), + custom: (factory, options) => this.withUIPrompt("custom", undefined, () => ui.custom(factory, options)), + }; + } + + private withUIPrompt(kind: UIPromptKind, title: string | undefined, run: () => Promise): Promise { + const outerPrompt = this.uiPromptDepth++ === 0; + if (outerPrompt) { + this.activeUIPrompt = { kind, title }; + this.emitUIPromptEvent({ type: "ui_prompt_start", reason: "ui_prompt", kind, ...(title ? { title } : {}) }); + } + + const finish = () => { + if (--this.uiPromptDepth > 0) return; + this.uiPromptDepth = 0; + + const prompt = this.activeUIPrompt ?? { kind, title }; + this.activeUIPrompt = undefined; + this.emitUIPromptEvent({ + type: "ui_prompt_end", + reason: "ui_prompt", + kind: prompt.kind, + ...(prompt.title ? { title: prompt.title } : {}), + }); + }; + + try { + return run().finally(finish); + } catch (err) { + finish(); + throw err; + } + } + + private emitUIPromptEvent(event: Extract): void { + queueMicrotask(() => { + void this.emit(event); + }); + } + + getUIContext(): ExtensionUIContext { + return this.uiContext; + } + + hasUI(): boolean { + return this.uiContext !== noOpUIContext; + } + + getExtensionPaths(): string[] { + return this.extensions.map((e) => e.path); + } + + /** Get all registered tools from all extensions (first registration per name wins). */ + getAllRegisteredTools(): RegisteredTool[] { + const toolsByName = new Map(); + for (const ext of this.extensions) { + for (const tool of ext.tools.values()) { + if (!toolsByName.has(tool.definition.name)) { + toolsByName.set(tool.definition.name, tool); + } + } + } + return Array.from(toolsByName.values()); + } + + /** Get a tool definition by name. Returns undefined if not found. */ + getToolDefinition(toolName: string): RegisteredTool["definition"] | undefined { + for (const ext of this.extensions) { + const tool = ext.tools.get(toolName); + if (tool) { + return tool.definition; + } + } + return undefined; + } + + getFlags(): Map { + const allFlags = new Map(); + for (const ext of this.extensions) { + for (const [name, flag] of ext.flags) { + if (!allFlags.has(name)) { + allFlags.set(name, flag); + } + } + } + return allFlags; + } + + setFlagValue(name: string, value: boolean | string): void { + this.runtime.flagValues.set(name, value); + } + + getFlagValues(): Map { + return new Map(this.runtime.flagValues); + } + + getShortcuts(resolvedKeybindings: KeybindingsConfig): Map { + this.shortcutDiagnostics = []; + const builtinKeybindings = buildBuiltinKeybindings(resolvedKeybindings); + const extensionShortcuts = new Map(); + + const addDiagnostic = (message: string, extensionPath: string) => { + this.shortcutDiagnostics.push({ type: "warning", message, path: extensionPath }); + if (!this.hasUI()) { + console.warn(message); + } + }; + + for (const ext of this.extensions) { + for (const [key, shortcut] of ext.shortcuts) { + const normalizedKey = key.toLowerCase() as KeyId; + + const builtInKeybinding = builtinKeybindings[normalizedKey]; + if (builtInKeybinding?.restrictOverride === true) { + addDiagnostic( + `Extension shortcut '${key}' from ${shortcut.extensionPath} conflicts with built-in shortcut. Skipping.`, + shortcut.extensionPath, + ); + continue; + } + + if (builtInKeybinding?.restrictOverride === false) { + addDiagnostic( + `Extension shortcut conflict: '${key}' is built-in shortcut for ${builtInKeybinding.keybinding} and ${shortcut.extensionPath}. Using ${shortcut.extensionPath}.`, + shortcut.extensionPath, + ); + } + + const existingExtensionShortcut = extensionShortcuts.get(normalizedKey); + if (existingExtensionShortcut) { + addDiagnostic( + `Extension shortcut conflict: '${key}' registered by both ${existingExtensionShortcut.extensionPath} and ${shortcut.extensionPath}. Using ${shortcut.extensionPath}.`, + shortcut.extensionPath, + ); + } + extensionShortcuts.set(normalizedKey, shortcut); + } + } + return extensionShortcuts; + } + + getShortcutDiagnostics(): ResourceDiagnostic[] { + return this.shortcutDiagnostics; + } + + invalidate( + message = "This extension ctx is stale after session replacement or reload. Do not use a captured pi or command ctx after ctx.newSession(), ctx.fork(), ctx.switchSession(), or ctx.reload(). For newSession, fork, and switchSession, move post-replacement work into withSession and use the ctx passed to withSession. For reload, do not use the old ctx after await ctx.reload().", + ): void { + if (!this.staleMessage) { + this.staleMessage = message; + this.runtime.invalidate(message); + } + } + + private assertActive(): void { + if (this.staleMessage) { + throw new Error(this.staleMessage); + } + } + + onError(listener: ExtensionErrorListener): () => void { + this.errorListeners.add(listener); + return () => this.errorListeners.delete(listener); + } + + emitError(error: ExtensionError): void { + for (const listener of this.errorListeners) { + listener(error); + } + } + + hasHandlers(eventType: string): boolean { + for (const ext of this.extensions) { + const handlers = ext.handlers.get(eventType); + if (handlers && handlers.length > 0) { + return true; + } + } + return false; + } + + getMessageRenderer(customType: string): MessageRenderer | undefined { + for (const ext of this.extensions) { + const renderer = ext.messageRenderers.get(customType); + if (renderer) { + return renderer; + } + } + return undefined; + } + + getMarkdownTransformers(): MarkdownTransformer[] { + return this.extensions.flatMap((ext) => (ext.markdownTransformer ? [ext.markdownTransformer] : [])); + } + + getEntryRenderer(customType: string): EntryRenderer | undefined { + for (const ext of this.extensions) { + const renderer = ext.entryRenderers?.get(customType); + if (renderer) { + return renderer; + } + } + return undefined; + } + + private resolveRegisteredCommands(): ResolvedCommand[] { + const commands: RegisteredCommand[] = []; + const counts = new Map(); + + for (const ext of this.extensions) { + for (const command of ext.commands.values()) { + commands.push(command); + counts.set(command.name, (counts.get(command.name) ?? 0) + 1); + } + } + + const seen = new Map(); + const takenInvocationNames = new Set(); + + return commands.map((command) => { + const occurrence = (seen.get(command.name) ?? 0) + 1; + seen.set(command.name, occurrence); + + let invocationName = (counts.get(command.name) ?? 0) > 1 ? `${command.name}:${occurrence}` : command.name; + + if (takenInvocationNames.has(invocationName)) { + let suffix = occurrence; + do { + suffix++; + invocationName = `${command.name}:${suffix}`; + } while (takenInvocationNames.has(invocationName)); + } + + takenInvocationNames.add(invocationName); + return { + ...command, + invocationName, + }; + }); + } + + getModelRegistry(): ModelRegistry { + return this.modelRegistry; + } + + getRegisteredCommands(): ResolvedCommand[] { + this.commandDiagnostics = []; + return this.resolveRegisteredCommands(); + } + + getCommandDiagnostics(): ResourceDiagnostic[] { + return this.commandDiagnostics; + } + + getCommand(name: string): ResolvedCommand | undefined { + return this.resolveRegisteredCommands().find((command) => command.invocationName === name); + } + + /** + * Request a graceful shutdown. Called by extension tools and event handlers. + * The actual shutdown behavior is provided by the mode via bindExtensions(). + */ + shutdown(): void { + this.shutdownHandler(); + } + + getActiveTools(): string[] { + this.assertActive(); + return this.runtime.getActiveTools(); + } + + /** + * Create an ExtensionContext for use in event handlers and tool execution. + * Context values are resolved at call time, so changes via bindCore/bindUI are reflected. + */ + createContext(): ExtensionContext { + const runner = this; + const getModel = this.getModel; + const getScopedModels = this.getScopedModels; + return { + get ui() { + runner.assertActive(); + return runner.uiContext; + }, + get mode() { + runner.assertActive(); + return runner.mode; + }, + get hasUI() { + runner.assertActive(); + return runner.hasUI(); + }, + get cwd() { + runner.assertActive(); + return runner.cwd; + }, + get sessionManager() { + runner.assertActive(); + return runner.sessionManager; + }, + get modelRegistry() { + runner.assertActive(); + return runner.modelRegistry; + }, + get model() { + runner.assertActive(); + return getModel(); + }, + get scopedModels() { + runner.assertActive(); + return getScopedModels(); + }, + get thinkingLevel() { + runner.assertActive(); + return runner.runtime.getThinkingLevel(); + }, + isIdle: () => { + runner.assertActive(); + return runner.isIdleFn(); + }, + isProjectTrusted: () => { + runner.assertActive(); + return runner.isProjectTrustedFn(); + }, + get signal() { + runner.assertActive(); + return runner.getSignalFn(); + }, + abort: () => { + runner.assertActive(); + runner.abortFn(); + }, + hasPendingMessages: () => { + runner.assertActive(); + return runner.hasPendingMessagesFn(); + }, + shutdown: () => { + runner.assertActive(); + runner.shutdownHandler(); + }, + getContextUsage: () => { + runner.assertActive(); + return runner.getContextUsageFn(); + }, + compact: (options) => { + runner.assertActive(); + runner.compactFn(options); + }, + getSystemPrompt: () => { + runner.assertActive(); + return runner.getSystemPromptFn(); + }, + }; + } + + createCommandContext(): ExtensionCommandContext { + // Use property descriptors instead of object spread so the guarded getters from + // createContext() stay lazy. A spread would eagerly read them once and freeze the + // old values into the returned object, bypassing stale-instance checks. + const context = Object.defineProperties( + {}, + Object.getOwnPropertyDescriptors(this.createContext()), + ) as ExtensionCommandContext; + context.getSystemPromptOptions = () => { + this.assertActive(); + return this.getSystemPromptOptionsFn(); + }; + context.waitForIdle = () => { + this.assertActive(); + return this.waitForIdleFn(); + }; + context.newSession = (options) => { + this.assertActive(); + return this.newSessionHandler(options); + }; + context.fork = (entryId, options) => { + this.assertActive(); + return this.forkHandler(entryId, options); + }; + context.navigateTree = (targetId, options) => { + this.assertActive(); + return this.navigateTreeHandler(targetId, options); + }; + context.switchSession = (sessionPath, options) => { + this.assertActive(); + return this.switchSessionHandler(sessionPath, options); + }; + context.reload = () => { + this.assertActive(); + return this.reloadHandler(); + }; + return context; + } + + private isSessionBeforeEvent(event: RunnerEmitEvent): event is SessionBeforeEvent { + return ( + event.type === "session_before_switch" || + event.type === "session_before_fork" || + event.type === "session_before_compact" || + event.type === "session_before_tree" + ); + } + + async emit(event: TEvent): Promise> { + const ctx = this.createContext(); + let result: SessionBeforeEventResult | undefined; + + for (const ext of this.extensions) { + const handlers = ext.handlers.get(event.type); + if (!handlers || handlers.length === 0) continue; + + for (const handler of handlers) { + try { + const handlerResult = await handler(event, ctx); + + if (this.isSessionBeforeEvent(event) && handlerResult) { + result = handlerResult as SessionBeforeEventResult; + if (result.cancel) { + return result as RunnerEmitResult; + } + } + } catch (err) { + const message = err instanceof Error ? err.message : String(err); + const stack = err instanceof Error ? err.stack : undefined; + this.emitError({ + extensionPath: ext.path, + event: event.type, + error: message, + stack, + }); + } + } + } + + return result as RunnerEmitResult; + } + + async emitMessageEnd(event: MessageEndEvent): Promise { + const ctx = this.createContext(); + let currentMessage = event.message; + let modified = false; + + for (const ext of this.extensions) { + const handlers = ext.handlers.get("message_end"); + if (!handlers || handlers.length === 0) continue; + + for (const handler of handlers) { + try { + const currentEvent: MessageEndEvent = { ...event, message: currentMessage }; + const handlerResult = (await handler(currentEvent, ctx)) as MessageEndEventResult | undefined; + if (!handlerResult?.message) continue; + + if (handlerResult.message.role !== currentMessage.role) { + this.emitError({ + extensionPath: ext.path, + event: "message_end", + error: "message_end handlers must return a message with the same role", + }); + continue; + } + + currentMessage = handlerResult.message; + modified = true; + } catch (err) { + const message = err instanceof Error ? err.message : String(err); + const stack = err instanceof Error ? err.stack : undefined; + this.emitError({ + extensionPath: ext.path, + event: "message_end", + error: message, + stack, + }); + } + } + } + + return modified ? currentMessage : undefined; + } + + async emitToolResult(event: ToolResultEvent): Promise { + const ctx = this.createContext(); + const currentEvent: ToolResultEvent = { ...event }; + let modified = false; + + for (const ext of this.extensions) { + const handlers = ext.handlers.get("tool_result"); + if (!handlers || handlers.length === 0) continue; + + for (const handler of handlers) { + try { + const handlerResult = (await handler(currentEvent, ctx)) as ToolResultEventResult | undefined; + if (!handlerResult) continue; + + if (handlerResult.content !== undefined) { + currentEvent.content = handlerResult.content; + modified = true; + } + if (handlerResult.details !== undefined) { + currentEvent.details = handlerResult.details; + modified = true; + } + if (handlerResult.isError !== undefined) { + currentEvent.isError = handlerResult.isError; + modified = true; + } + if (handlerResult.usage !== undefined) { + currentEvent.usage = handlerResult.usage; + modified = true; + } + } catch (err) { + const message = err instanceof Error ? err.message : String(err); + const stack = err instanceof Error ? err.stack : undefined; + this.emitError({ + extensionPath: ext.path, + event: "tool_result", + error: message, + stack, + }); + } + } + } + + if (!modified) { + return undefined; + } + + return { + content: currentEvent.content, + details: currentEvent.details, + isError: currentEvent.isError, + usage: currentEvent.usage, + }; + } + + async emitToolCall(event: ToolCallEvent): Promise { + const ctx = this.createContext(); + let result: ToolCallEventResult | undefined; + + for (const ext of this.extensions) { + const handlers = ext.handlers.get("tool_call"); + if (!handlers || handlers.length === 0) continue; + + for (const handler of handlers) { + const handlerResult = await handler(event, ctx); + + if (handlerResult) { + result = handlerResult as ToolCallEventResult; + if (result.block) { + return result; + } + } + } + } + + return result; + } + + async emitUserBash(event: UserBashEvent): Promise { + const ctx = this.createContext(); + + for (const ext of this.extensions) { + const handlers = ext.handlers.get("user_bash"); + if (!handlers || handlers.length === 0) continue; + + for (const handler of handlers) { + try { + const handlerResult = await handler(event, ctx); + if (handlerResult) { + return handlerResult as UserBashEventResult; + } + } catch (err) { + const message = err instanceof Error ? err.message : String(err); + const stack = err instanceof Error ? err.stack : undefined; + this.emitError({ + extensionPath: ext.path, + event: "user_bash", + error: message, + stack, + }); + } + } + } + + return undefined; + } + + async emitContext(messages: AgentMessage[]): Promise { + const ctx = this.createContext(); + let currentMessages = structuredClone(messages); + + for (const ext of this.extensions) { + const handlers = ext.handlers.get("context"); + if (!handlers || handlers.length === 0) continue; + + for (const handler of handlers) { + try { + const event: ContextEvent = { type: "context", messages: currentMessages }; + const handlerResult = await handler(event, ctx); + + if (handlerResult && (handlerResult as ContextEventResult).messages) { + currentMessages = (handlerResult as ContextEventResult).messages!; + } + } catch (err) { + const message = err instanceof Error ? err.message : String(err); + const stack = err instanceof Error ? err.stack : undefined; + this.emitError({ + extensionPath: ext.path, + event: "context", + error: message, + stack, + }); + } + } + } + + return currentMessages; + } + + async emitBeforeProviderRequest(payload: unknown): Promise { + const ctx = this.createContext(); + let currentPayload = payload; + + for (const ext of this.extensions) { + const handlers = ext.handlers.get("before_provider_request"); + if (!handlers || handlers.length === 0) continue; + + for (const handler of handlers) { + try { + const event: BeforeProviderRequestEvent = { + type: "before_provider_request", + payload: currentPayload, + }; + const handlerResult = await handler(event, ctx); + if (handlerResult !== undefined) { + currentPayload = handlerResult; + } + } catch (err) { + const message = err instanceof Error ? err.message : String(err); + const stack = err instanceof Error ? err.stack : undefined; + this.emitError({ + extensionPath: ext.path, + event: "before_provider_request", + error: message, + stack, + }); + } + } + } + + return currentPayload; + } + + async emitBeforeProviderHeaders(headers: ProviderHeaders): Promise { + const ctx = this.createContext(); + + for (const ext of this.extensions) { + const handlers = ext.handlers.get("before_provider_headers"); + if (!handlers || handlers.length === 0) continue; + + for (const handler of handlers) { + try { + // Handlers mutate `headers` in place; the return value is ignored. + const event: BeforeProviderHeadersEvent = { + type: "before_provider_headers", + headers, + }; + await handler(event, ctx); + } catch (err) { + const message = err instanceof Error ? err.message : String(err); + const stack = err instanceof Error ? err.stack : undefined; + this.emitError({ + extensionPath: ext.path, + event: "before_provider_headers", + error: message, + stack, + }); + } + } + } + + return headers; + } + + async emitBeforeAgentStart( + prompt: string, + images: ImageContent[] | undefined, + systemPrompt: string, + systemPromptOptions: BuildSystemPromptOptions, + ): Promise { + let currentSystemPrompt = systemPrompt; + const ctx = Object.defineProperties( + {}, + Object.getOwnPropertyDescriptors(this.createContext()), + ) as ExtensionContext; + ctx.getSystemPrompt = () => { + this.assertActive(); + return currentSystemPrompt; + }; + const messages: NonNullable[] = []; + let systemPromptModified = false; + + for (const ext of this.extensions) { + const handlers = ext.handlers.get("before_agent_start"); + if (!handlers || handlers.length === 0) continue; + + for (const handler of handlers) { + try { + const event: BeforeAgentStartEvent = { + type: "before_agent_start", + prompt, + images, + systemPrompt: currentSystemPrompt, + systemPromptOptions, + }; + const handlerResult = await handler(event, ctx); + + if (handlerResult) { + const result = handlerResult as BeforeAgentStartEventResult; + if (result.message) { + messages.push(result.message); + } + if (result.systemPrompt !== undefined) { + currentSystemPrompt = result.systemPrompt; + systemPromptModified = true; + } + } + } catch (err) { + const message = err instanceof Error ? err.message : String(err); + const stack = err instanceof Error ? err.stack : undefined; + this.emitError({ + extensionPath: ext.path, + event: "before_agent_start", + error: message, + stack, + }); + } + } + } + + if (messages.length > 0 || systemPromptModified) { + return { + messages: messages.length > 0 ? messages : undefined, + systemPrompt: systemPromptModified ? currentSystemPrompt : undefined, + }; + } + + return undefined; + } + + async emitResourcesDiscover( + cwd: string, + reason: ResourcesDiscoverEvent["reason"], + ): Promise<{ + skillPaths: Array<{ path: string; extensionPath: string }>; + promptPaths: Array<{ path: string; extensionPath: string }>; + themePaths: Array<{ path: string; extensionPath: string }>; + }> { + const ctx = this.createContext(); + const skillPaths: Array<{ path: string; extensionPath: string }> = []; + const promptPaths: Array<{ path: string; extensionPath: string }> = []; + const themePaths: Array<{ path: string; extensionPath: string }> = []; + + for (const ext of this.extensions) { + const handlers = ext.handlers.get("resources_discover"); + if (!handlers || handlers.length === 0) continue; + + for (const handler of handlers) { + try { + const event: ResourcesDiscoverEvent = { type: "resources_discover", cwd, reason }; + const handlerResult = await handler(event, ctx); + const result = handlerResult as ResourcesDiscoverResult | undefined; + + if (result?.skillPaths?.length) { + skillPaths.push(...result.skillPaths.map((path) => ({ path, extensionPath: ext.path }))); + } + if (result?.promptPaths?.length) { + promptPaths.push(...result.promptPaths.map((path) => ({ path, extensionPath: ext.path }))); + } + if (result?.themePaths?.length) { + themePaths.push(...result.themePaths.map((path) => ({ path, extensionPath: ext.path }))); + } + } catch (err) { + const message = err instanceof Error ? err.message : String(err); + const stack = err instanceof Error ? err.stack : undefined; + this.emitError({ + extensionPath: ext.path, + event: "resources_discover", + error: message, + stack, + }); + } + } + } + + return { skillPaths, promptPaths, themePaths }; + } + + /** Emit input event. Transforms chain, "handled" short-circuits. */ + async emitInput( + text: string, + images: ImageContent[] | undefined, + source: InputSource, + streamingBehavior?: "steer" | "followUp", + ): Promise { + const ctx = this.createContext(); + let currentText = text; + let currentImages = images; + + for (const ext of this.extensions) { + for (const handler of ext.handlers.get("input") ?? []) { + try { + const event: InputEvent = { + type: "input", + text: currentText, + images: currentImages, + source, + streamingBehavior, + }; + const result = (await handler(event, ctx)) as InputEventResult | undefined; + if (result?.action === "handled") return result; + if (result?.action === "transform") { + currentText = result.text; + currentImages = result.images ?? currentImages; + } + } catch (err) { + this.emitError({ + extensionPath: ext.path, + event: "input", + error: err instanceof Error ? err.message : String(err), + stack: err instanceof Error ? err.stack : undefined, + }); + } + } + } + return currentText !== text || currentImages !== images + ? { action: "transform", text: currentText, images: currentImages } + : { action: "continue" }; + } +} diff --git a/packages/coding-agent/src/core/extensions/types.ts b/packages/coding-agent/src/core/extensions/types.ts new file mode 100644 index 0000000000000000000000000000000000000000..eaff3a86ebf50ec543faf936699f5e6c84dfdf65 --- /dev/null +++ b/packages/coding-agent/src/core/extensions/types.ts @@ -0,0 +1,1797 @@ +/** + * Extension system types. + * + * Extensions are TypeScript modules that can: + * - Subscribe to agent lifecycle events + * - Register LLM-callable tools + * - Register commands, keyboard shortcuts, and CLI flags + * - Interact with the user via UI primitives + */ + +import type { + AgentMessage, + AgentToolResult, + AgentToolUpdateCallback, + ThinkingLevel, + ToolExecutionMode, +} from "@earendil-works/pi-agent-core"; +import type { + Api, + AssistantMessageEvent, + AssistantMessageEventStream, + ConstrainedSamplingConfig, + Context, + ImageContent, + Model, + OAuthCredentials, + OAuthLoginCallbacks, + Provider, + ProviderHeaders, + RefreshModelsContext, + SimpleStreamOptions, + TextContent, + ToolResultMessage, + Usage, +} from "@earendil-works/pi-ai"; +import type { + AutocompleteItem, + AutocompleteProvider, + Component, + EditorComponent, + EditorTheme, + KeyId, + OverlayHandle, + OverlayOptions, + TUI, +} from "@earendil-works/pi-tui"; +import type { Static, TSchema } from "typebox"; +import type { Theme } from "../../modes/interactive/theme/theme.ts"; +import type { BashResult } from "../bash-executor.ts"; +import type { CompactionPreparation, CompactionResult } from "../compaction/index.ts"; +import type { EventBus } from "../event-bus.ts"; +import type { ExecOptions, ExecResult } from "../exec.ts"; +import type { ReadonlyFooterDataProvider } from "../footer-data-provider.ts"; +import type { KeybindingsManager } from "../keybindings.ts"; +import type { CustomMessage } from "../messages.ts"; +import type { ModelRegistry } from "../model-registry.ts"; +import type { ScopedModel } from "../model-resolver.ts"; +import type { + BranchSummaryEntry, + CompactionEntry, + CustomEntry, + ReadonlySessionManager, + SessionEntry, + SessionManager, +} from "../session-manager.ts"; +import type { SlashCommandInfo } from "../slash-commands.ts"; +import type { SourceInfo } from "../source-info.ts"; +import type { BuildSystemPromptOptions } from "../system-prompt.ts"; +import type { BashOperations } from "../tools/bash.ts"; +import type { EditToolDetails } from "../tools/edit.ts"; +import type { + BashToolDetails, + BashToolInput, + EditToolInput, + FindToolDetails, + FindToolInput, + GrepToolDetails, + GrepToolInput, + LsToolDetails, + LsToolInput, + PowerShellToolDetails, + PowerShellToolInput, + ReadToolDetails, + ReadToolInput, + WriteToolInput, +} from "../tools/index.ts"; + +export type { ExecOptions, ExecResult } from "../exec.ts"; +export type { BuildSystemPromptOptions } from "../system-prompt.ts"; +export type { AgentToolResult, AgentToolUpdateCallback, ToolExecutionMode }; +export type { AppKeybinding, KeybindingsManager } from "../keybindings.ts"; + +// ============================================================================ +// UI Context +// ============================================================================ + +/** Options for extension UI dialogs. */ +export interface ExtensionUIDialogOptions { + /** AbortSignal to programmatically dismiss the dialog. */ + signal?: AbortSignal; + /** Timeout in milliseconds. Dialog auto-dismisses with live countdown display. */ + timeout?: number; +} + +/** Placement for extension widgets. */ +export type WidgetPlacement = "aboveEditor" | "belowEditor"; + +/** Options for extension widgets. */ +export interface ExtensionWidgetOptions { + /** Where the widget is rendered. Defaults to "aboveEditor". */ + placement?: WidgetPlacement; +} + +/** Raw terminal input listener for extensions. */ +export type TerminalInputHandler = (data: string) => { consume?: boolean; data?: string } | undefined; + +/** Working indicator configuration for the interactive streaming loader. */ +export interface WorkingIndicatorOptions { + /** Animation frames. Use an empty array to hide the indicator entirely. Custom frames are rendered verbatim. */ + frames?: string[]; + /** Frame interval in milliseconds for animated indicators. */ + intervalMs?: number; +} + +/** Wrap the current autocomplete provider with additional behavior. */ +export type AutocompleteProviderFactory = (current: AutocompleteProvider) => AutocompleteProvider; +export type EditorFactory = (tui: TUI, theme: EditorTheme, keybindings: KeybindingsManager) => EditorComponent; + +/** + * UI context for extensions to request interactive UI. + * Each mode (interactive, RPC, print) provides its own implementation. + */ +export interface ExtensionUIContext { + /** Show a selector and return the user's choice. */ + select(title: string, options: string[], opts?: ExtensionUIDialogOptions): Promise; + + /** Show a confirmation dialog. */ + confirm(title: string, message: string, opts?: ExtensionUIDialogOptions): Promise; + + /** Show a text input dialog. */ + input(title: string, placeholder?: string, opts?: ExtensionUIDialogOptions): Promise; + + /** Show a notification to the user. */ + notify(message: string, type?: "info" | "warning" | "error"): void; + + /** Listen to raw terminal input (interactive mode only). Returns an unsubscribe function. */ + onTerminalInput(handler: TerminalInputHandler): () => void; + + /** Set status text in the footer/status bar. Pass undefined to clear. */ + setStatus(key: string, text: string | undefined): void; + + /** Set the working/loading message shown during streaming. Call with no argument to restore default. */ + setWorkingMessage(message?: string): void; + + /** Show or hide the built-in interactive working loader row during streaming. */ + setWorkingVisible(visible: boolean): void; + + /** + * Configure the interactive working indicator shown during streaming. + * + * - Omit the argument to restore the default animated spinner. + * - Use `frames: ["●"]` for a static indicator. + * - Use `frames: []` to hide the indicator entirely. + * - Custom frames are rendered as provided, so extensions must add their own colors. + */ + setWorkingIndicator(options?: WorkingIndicatorOptions): void; + + /** Set the label shown for hidden thinking blocks. Call with no argument to restore default. */ + setHiddenThinkingLabel(label?: string): void; + + /** Set a widget to display above or below the editor. Accepts string array or component factory. */ + setWidget(key: string, content: string[] | undefined, options?: ExtensionWidgetOptions): void; + setWidget( + key: string, + content: ((tui: TUI, theme: Theme) => Component & { dispose?(): void }) | undefined, + options?: ExtensionWidgetOptions, + ): void; + + /** Set a custom footer component, or undefined to restore the built-in footer. + * + * The factory receives a FooterDataProvider for data not otherwise accessible: + * git branch and extension statuses from setStatus(). Context usage is on + * ctx.getContextUsage(), token stats on ctx.sessionManager.getEntries(), model info on ctx.model. + */ + setFooter( + factory: + | ((tui: TUI, theme: Theme, footerData: ReadonlyFooterDataProvider) => Component & { dispose?(): void }) + | undefined, + ): void; + + /** Set a custom header component (shown at startup, above chat), or undefined to restore the built-in header. */ + setHeader(factory: ((tui: TUI, theme: Theme) => Component & { dispose?(): void }) | undefined): void; + + /** Set the terminal window/tab title. */ + setTitle(title: string): void; + + /** Show a custom component with keyboard focus. */ + custom( + factory: ( + tui: TUI, + theme: Theme, + keybindings: KeybindingsManager, + done: (result: T) => void, + ) => (Component & { dispose?(): void }) | Promise, + options?: { + overlay?: boolean; + /** Overlay positioning/sizing options. Can be static or a function for dynamic updates. */ + overlayOptions?: OverlayOptions | (() => OverlayOptions); + /** Called with the overlay handle after the overlay is shown. Use to control visibility. */ + onHandle?: (handle: OverlayHandle) => void; + }, + ): Promise; + + /** Paste text into the editor, triggering paste handling (collapse for large content). */ + pasteToEditor(text: string): void; + + /** Set the text in the core input editor. */ + setEditorText(text: string): void; + + /** Get the current text from the core input editor. */ + getEditorText(): string; + + /** Show a multi-line editor for text editing. */ + editor(title: string, prefill?: string): Promise; + + /** Stack additional autocomplete behavior on top of the built-in provider. */ + addAutocompleteProvider(factory: AutocompleteProviderFactory): void; + + /** + * Set a custom editor component via factory function. + * Pass undefined to restore the default editor. + * + * The factory receives: + * - `theme`: EditorTheme for styling borders and autocomplete + * - `keybindings`: KeybindingsManager for app-level keybindings + * + * For full app keybinding support (escape, ctrl+d, model switching, etc.), + * extend `CustomEditor` from `@earendil-works/pi-coding-agent` and call + * `super.handleInput(data)` for keys you don't handle. + * + * @example + * ```ts + * import { CustomEditor } from "@earendil-works/pi-coding-agent"; + * + * class VimEditor extends CustomEditor { + * private mode: "normal" | "insert" = "insert"; + * + * handleInput(data: string): void { + * if (this.mode === "normal") { + * // Handle vim normal mode keys... + * if (data === "i") { this.mode = "insert"; return; } + * } + * super.handleInput(data); // App keybindings + text editing + * } + * } + * + * ctx.ui.setEditorComponent((tui, theme, keybindings) => + * new VimEditor(tui, theme, keybindings) + * ); + * ``` + */ + setEditorComponent(factory: EditorFactory | undefined): void; + + /** Get the currently configured custom editor factory, or undefined when using the default editor. */ + getEditorComponent(): EditorFactory | undefined; + + /** Get the current theme for styling. */ + readonly theme: Theme; + + /** Get all available themes with their names and file paths. */ + getAllThemes(): { name: string; path: string | undefined }[]; + + /** Load a theme by name without switching to it. Returns undefined if not found. */ + getTheme(name: string): Theme | undefined; + + /** Set the current theme by name or Theme object. */ + setTheme(theme: string | Theme): { success: boolean; error?: string }; + + /** Get current tool output expansion state. */ + getToolsExpanded(): boolean; + + /** Set tool output expansion state. */ + setToolsExpanded(expanded: boolean): void; +} + +// ============================================================================ +// Extension Context +// ============================================================================ + +export interface ContextUsage { + /** Estimated context tokens, or null if unknown (e.g. right after compaction, before next LLM response). */ + tokens: number | null; + contextWindow: number; + /** Context usage as percentage of context window, or null if tokens is unknown. */ + percent: number | null; +} + +export interface CompactOptions { + customInstructions?: string; + onComplete?: (result: CompactionResult) => void; + onError?: (error: Error) => void; +} + +/** + * Context passed to extension event handlers. + */ +export type ExtensionMode = "tui" | "rpc" | "json" | "print"; + +export interface ExtensionContext { + /** UI methods for user interaction */ + ui: ExtensionUIContext; + /** Current run mode. Use "tui" to guard terminal-only UI such as custom components. */ + mode: ExtensionMode; + /** Whether dialog-capable UI is available (true in TUI and RPC modes) */ + hasUI: boolean; + /** Current working directory */ + cwd: string; + /** Session manager (read-only) */ + sessionManager: ReadonlySessionManager; + /** Model registry for API key resolution */ + modelRegistry: ModelRegistry; + /** Current model (may be undefined) */ + model: Model | undefined; + /** Models scoped to this session (resolved from `--models` / + * `enabledModels` settings against the available catalogue). Same set + * the `/scoped-models` command shows. Empty when no scoping is + * configured (all available models are usable). Read-only snapshot. */ + scopedModels: readonly ScopedModel[]; + /** Current thinking level, when provided by the session runtime. */ + thinkingLevel?: ThinkingLevel; + /** Whether the agent is idle (not streaming) */ + isIdle(): boolean; + /** Whether project-local trust is active for this context. */ + isProjectTrusted(): boolean; + /** The current abort signal, or undefined when the agent is not streaming. */ + signal: AbortSignal | undefined; + /** Abort the current agent operation */ + abort(): void; + /** Whether there are queued messages waiting */ + hasPendingMessages(): boolean; + /** Gracefully shutdown pi and exit. Available in all contexts. */ + shutdown(): void; + /** Get current context usage for the active model. */ + getContextUsage(): ContextUsage | undefined; + /** Trigger compaction without awaiting completion. */ + compact(options?: CompactOptions): void; + /** Get the current effective system prompt. */ + getSystemPrompt(): string; +} + +/** + * Extended context for command handlers. + * Includes session control methods only safe in user-initiated commands. + */ +export interface ExtensionCommandContext extends ExtensionContext { + /** Get the current base system-prompt construction options. */ + getSystemPromptOptions(): BuildSystemPromptOptions; + + /** Wait for the agent to finish streaming */ + waitForIdle(): Promise; + + /** Start a new session, optionally with initialization. */ + newSession(options?: { + parentSession?: string; + setup?: (sessionManager: SessionManager) => Promise; + withSession?: (ctx: ReplacedSessionContext) => Promise; + }): Promise<{ cancelled: boolean }>; + + /** Fork from a specific entry, creating a new session file. */ + fork( + entryId: string, + options?: { position?: "before" | "at"; withSession?: (ctx: ReplacedSessionContext) => Promise }, + ): Promise<{ cancelled: boolean }>; + + /** Navigate to a different point in the session tree. */ + navigateTree( + targetId: string, + options?: { summarize?: boolean; customInstructions?: string; replaceInstructions?: boolean; label?: string }, + ): Promise<{ cancelled: boolean }>; + + /** Switch to a different session file. */ + switchSession( + sessionPath: string, + options?: { withSession?: (ctx: ReplacedSessionContext) => Promise }, + ): Promise<{ cancelled: boolean }>; + + /** Reload extensions, skills, prompts, themes, and context files. */ + reload(): Promise; +} + +/** + * Fresh command-capable context bound to the replacement session after a session switch. + * + * This is passed to `withSession()` callbacks on `newSession()`, `fork()`, and `switchSession()`. + */ +export interface ReplacedSessionContext extends ExtensionCommandContext { + sendMessage( + message: Pick, "customType" | "content" | "display" | "details">, + options?: { triggerTurn?: boolean; deliverAs?: "steer" | "followUp" | "nextTurn" }, + ): Promise; + + sendUserMessage( + content: string | (TextContent | ImageContent)[], + options?: { deliverAs?: "steer" | "followUp"; expandPromptTemplates?: boolean }, + ): Promise; +} + +// ============================================================================ +// Tool Types +// ============================================================================ + +/** Rendering options for tool results */ +export interface ToolRenderResultOptions { + /** Whether the result view is expanded */ + expanded: boolean; + /** Whether this is a partial/streaming result */ + isPartial: boolean; +} + +/** Context passed to tool renderers. */ +export interface ToolRenderContext { + /** Current tool call arguments. Shared across call/result renders for the same tool call. */ + args: TArgs; + /** Unique id for this tool execution. Stable across call/result renders for the same tool call. */ + toolCallId: string; + /** Invalidate just this tool execution component for redraw. */ + invalidate: () => void; + /** Previously returned component for this render slot, if any. */ + lastComponent: Component | undefined; + /** Shared renderer state for this tool row. Initialized by tool-execution.ts. */ + state: TState; + /** Working directory for this tool execution. */ + cwd: string; + /** Whether the tool execution has started. */ + executionStarted: boolean; + /** Whether the tool call arguments are complete. */ + argsComplete: boolean; + /** Whether the tool result is partial/streaming. */ + isPartial: boolean; + /** Whether the result view is expanded. */ + expanded: boolean; + /** Whether inline images are currently shown in the TUI. */ + showImages: boolean; + /** Whether the current result is an error. */ + isError: boolean; +} + +/** + * Tool definition for registerTool(). + */ +export interface ToolDefinition { + /** Tool name (used in LLM tool calls) */ + name: string; + /** Human-readable label for UI */ + label: string; + /** Description for LLM */ + description: string; + /** Optional one-line snippet for the Available tools section in the default system prompt. Custom tools are omitted from that section when this is not provided. */ + promptSnippet?: string; + /** Optional guideline bullets appended to the default system prompt Guidelines section when this tool is active. */ + promptGuidelines?: string[]; + /** Parameter schema (TypeBox) */ + parameters: TParams; + /** Optional provider-side constrained sampling request for this tool. Set false to explicitly disable it, equivalent to leaving it undefined. */ + constrainedSampling?: false | ConstrainedSamplingConfig; + /** Controls whether ToolExecutionComponent renders the standard colored shell or the tool renders its own framing. */ + renderShell?: "default" | "self"; + + /** Optional compatibility shim to prepare raw tool call arguments before schema validation. Must return an object conforming to TParams. */ + prepareArguments?: (args: unknown) => Static; + + /** + * Per-tool execution mode override. + * - "sequential": this tool must execute one at a time with other tool calls. + * - "parallel": this tool can execute concurrently with other tool calls. + * + * If omitted, the default execution mode applies. + */ + executionMode?: ToolExecutionMode; + + /** Execute the tool. */ + execute( + toolCallId: string, + params: Static, + signal: AbortSignal | undefined, + onUpdate: AgentToolUpdateCallback | undefined, + ctx: ExtensionContext, + ): Promise>; + + /** Custom rendering for tool call display */ + renderCall?: (args: Static, theme: Theme, context: ToolRenderContext>) => Component; + + /** Custom rendering for tool result display */ + renderResult?: ( + result: AgentToolResult, + options: ToolRenderResultOptions, + theme: Theme, + context: ToolRenderContext>, + ) => Component; +} + +type AnyToolDefinition = ToolDefinition; + +/** + * Preserve parameter inference for standalone tool definitions. + * + * Use this when assigning a tool to a variable or passing it through arrays such + * as `customTools`, where contextual typing would otherwise widen params to + * `unknown`. + */ +export function defineTool( + tool: ToolDefinition, +): ToolDefinition & AnyToolDefinition { + return tool as ToolDefinition & AnyToolDefinition; +} + +// ============================================================================ +// Startup/Resource Events +// ============================================================================ + +export interface ProjectTrustEvent { + type: "project_trust"; + cwd: string; +} + +export type ProjectTrustEventDecision = "yes" | "no" | "undecided"; + +export interface ProjectTrustEventResult { + trusted: ProjectTrustEventDecision; + remember?: boolean; +} + +export interface ProjectTrustContext { + cwd: string; + mode: ExtensionMode; + hasUI: boolean; + ui: Pick; +} + +export type ProjectTrustHandler = ( + event: ProjectTrustEvent, + ctx: ProjectTrustContext, +) => Promise | ProjectTrustEventResult; + +/** Fired after session_start to allow extensions to provide additional resource paths. */ +export interface ResourcesDiscoverEvent { + type: "resources_discover"; + cwd: string; + reason: "startup" | "reload"; +} + +/** Result from resources_discover event handler */ +export interface ResourcesDiscoverResult { + skillPaths?: string[]; + promptPaths?: string[]; + themePaths?: string[]; +} + +// ============================================================================ +// Session Events +// ============================================================================ + +/** Fired when a session is started, loaded, or reloaded */ +export interface SessionStartEvent { + type: "session_start"; + /** Why this session start happened. */ + reason: "startup" | "reload" | "new" | "resume" | "fork"; + /** Previously active session file. Present for "new", "resume", and "fork". */ + previousSessionFile?: string; +} + +/** Fired when the current session metadata changes. */ +export interface SessionInfoChangedEvent { + type: "session_info_changed"; + /** Current normalized session name. Undefined when the name is cleared. */ + name: string | undefined; +} + +/** Fired before switching to another session (can be cancelled) */ +export interface SessionBeforeSwitchEvent { + type: "session_before_switch"; + reason: "new" | "resume"; + targetSessionFile?: string; +} + +/** Fired before forking a session (can be cancelled) */ +export interface SessionBeforeForkEvent { + type: "session_before_fork"; + entryId: string; + position: "before" | "at"; +} + +/** Fired before context compaction (can be cancelled or customized) */ +export interface SessionBeforeCompactEvent { + type: "session_before_compact"; + preparation: CompactionPreparation; + branchEntries: SessionEntry[]; + customInstructions?: string; + /** What triggered the compaction: manual /compact, the context threshold, or context overflow recovery */ + reason: "manual" | "threshold" | "overflow"; + /** True when the aborted turn is retried after this compaction (overflow recovery) */ + willRetry: boolean; + signal: AbortSignal; +} + +/** Fired after context compaction succeeds */ +export interface SessionCompactEvent { + type: "session_compact"; + compactionEntry: CompactionEntry; + fromExtension: boolean; + /** What triggered the compaction: manual /compact, the context threshold, or context overflow recovery */ + reason: "manual" | "threshold" | "overflow"; + /** True when the aborted turn is retried after this compaction (overflow recovery) */ + willRetry: boolean; +} + +/** Fired after context compaction fails or is aborted */ +export interface SessionCompactFailedEvent { + type: "session_compact_failed"; + /** What triggered the compaction: manual /compact, the context threshold, or context overflow recovery */ + reason: "manual" | "threshold" | "overflow"; + /** Error text when compaction failed for a non-abort reason. */ + errorMessage?: string; + /** True when compaction was cancelled or aborted. */ + aborted: boolean; + /** True when the aborted turn would have been retried after this compaction (overflow recovery) */ + willRetry: boolean; + /** True when the failing compaction content came from a session_before_compact handler. */ + fromExtension: boolean; +} + +/** Fired before an extension runtime is torn down due to quit, reload, or session replacement. */ +export interface SessionShutdownEvent { + type: "session_shutdown"; + reason: "quit" | "reload" | "new" | "resume" | "fork"; + /** Destination session file when shutting down due to session replacement. */ + targetSessionFile?: string; +} + +/** Preparation data for tree navigation */ +export interface TreePreparation { + targetId: string; + oldLeafId: string | null; + commonAncestorId: string | null; + entriesToSummarize: SessionEntry[]; + userWantsSummary: boolean; + /** Custom instructions for summarization */ + customInstructions?: string; + /** If true, customInstructions replaces the default prompt instead of being appended */ + replaceInstructions?: boolean; + /** Label to attach to the branch summary entry */ + label?: string; +} + +/** Fired before navigating in the session tree (can be cancelled) */ +export interface SessionBeforeTreeEvent { + type: "session_before_tree"; + preparation: TreePreparation; + signal: AbortSignal; +} + +/** Fired after navigating in the session tree */ +export interface SessionTreeEvent { + type: "session_tree"; + newLeafId: string | null; + oldLeafId: string | null; + summaryEntry?: BranchSummaryEntry; + fromExtension?: boolean; +} + +export type SessionEvent = + | SessionStartEvent + | SessionInfoChangedEvent + | SessionBeforeSwitchEvent + | SessionBeforeForkEvent + | SessionBeforeCompactEvent + | SessionCompactEvent + | SessionCompactFailedEvent + | SessionShutdownEvent + | SessionBeforeTreeEvent + | SessionTreeEvent; + +// ============================================================================ +// Agent Events +// ============================================================================ + +/** Fired before each LLM call. Can modify messages. */ +export interface ContextEvent { + type: "context"; + messages: AgentMessage[]; +} + +/** Fired before a provider request is sent. Can replace the payload. */ +export interface BeforeProviderRequestEvent { + type: "before_provider_request"; + payload: unknown; +} + +/** + * Fired after request headers are assembled, before the provider HTTP call. + * Handlers mutate `headers` in place (e.g. to inject tracing/session headers); + * the return value is ignored. A `null` value deletes that header. + */ +export interface BeforeProviderHeadersEvent { + type: "before_provider_headers"; + headers: ProviderHeaders; +} + +/** Fired after a provider response is received and before the response stream is consumed. */ +export interface AfterProviderResponseEvent { + type: "after_provider_response"; + status: number; + headers: Record; +} + +/** Fired after user submits prompt but before agent loop. */ +export interface BeforeAgentStartEvent { + type: "before_agent_start"; + /** The raw user prompt text (after expansion). */ + prompt: string; + /** Images attached to the user prompt, if any. */ + images?: ImageContent[]; + /** The fully assembled system prompt string. */ + systemPrompt: string; + /** Structured options used to build the system prompt. Extensions can inspect this to understand what Pi loaded without re-discovering resources. */ + systemPromptOptions: BuildSystemPromptOptions; +} + +/** Fired when an agent loop starts */ +export interface AgentStartEvent { + type: "agent_start"; +} + +/** Fired when an agent loop ends */ +export interface AgentEndEvent { + type: "agent_end"; + messages: AgentMessage[]; +} + +/** Fired after an agent run has fully settled and no automatic retry, compaction, or queued continuation will run. */ +export interface AgentSettledEvent { + type: "agent_settled"; +} + +export type UIPromptKind = "select" | "confirm" | "input" | "editor" | "custom"; + +/** Fired when Pi starts waiting on a blocking user-facing extension UI prompt. */ +export interface UIPromptStartEvent { + type: "ui_prompt_start"; + reason: "ui_prompt"; + kind: UIPromptKind; + title?: string; +} + +/** Fired when Pi is no longer waiting on a blocking user-facing extension UI prompt. */ +export interface UIPromptEndEvent { + type: "ui_prompt_end"; + reason: "ui_prompt"; + kind: UIPromptKind; + title?: string; +} + +/** Fired at the start of each turn */ +export interface TurnStartEvent { + type: "turn_start"; + turnIndex: number; + timestamp: number; +} + +/** Fired at the end of each turn */ +export interface TurnEndEvent { + type: "turn_end"; + turnIndex: number; + message: AgentMessage; + toolResults: ToolResultMessage[]; +} + +/** Fired when a message starts (user, assistant, or toolResult) */ +export interface MessageStartEvent { + type: "message_start"; + message: AgentMessage; +} + +/** Fired during assistant message streaming with token-by-token updates */ +export interface MessageUpdateEvent { + type: "message_update"; + message: AgentMessage; + assistantMessageEvent: AssistantMessageEvent; +} + +/** Fired when a message ends */ +export interface MessageEndEvent { + type: "message_end"; + message: AgentMessage; +} + +/** Fired when a tool starts executing */ +export interface ToolExecutionStartEvent { + type: "tool_execution_start"; + toolCallId: string; + toolName: string; + args: any; +} + +/** Fired during tool execution with partial/streaming output */ +export interface ToolExecutionUpdateEvent { + type: "tool_execution_update"; + toolCallId: string; + toolName: string; + args: any; + partialResult: any; +} + +/** Fired when a tool finishes executing */ +export interface ToolExecutionEndEvent { + type: "tool_execution_end"; + toolCallId: string; + toolName: string; + result: any; + isError: boolean; +} + +// ============================================================================ +// Model Events +// ============================================================================ + +export type ModelSelectSource = "set" | "cycle" | "restore"; + +/** Fired when a new model is selected */ +export interface ModelSelectEvent { + type: "model_select"; + model: Model; + previousModel: Model | undefined; + source: ModelSelectSource; +} + +/** Fired when a new thinking level is selected */ +export interface ThinkingLevelSelectEvent { + type: "thinking_level_select"; + level: ThinkingLevel; + previousLevel: ThinkingLevel; +} + +// ============================================================================ +// User Bash Events +// ============================================================================ + +/** Fired when user executes a bash command via ! or !! prefix */ +export interface UserBashEvent { + type: "user_bash"; + /** The command to execute */ + command: string; + /** True if !! prefix was used (excluded from LLM context) */ + excludeFromContext: boolean; + /** Current working directory */ + cwd: string; +} + +// ============================================================================ +// Input Events +// ============================================================================ + +/** Source of user input */ +export type InputSource = "interactive" | "rpc" | "extension"; + +/** Fired when user input is received, before agent processing */ +export interface InputEvent { + type: "input"; + /** The input text */ + text: string; + /** Attached images, if any */ + images?: ImageContent[]; + /** Where the input came from */ + source: InputSource; + /** How the input will be delivered during streaming, or undefined when idle */ + streamingBehavior?: "steer" | "followUp"; +} + +/** Result from input event handler */ +export type InputEventResult = + | { action: "continue" } + | { action: "transform"; text: string; images?: ImageContent[] } + | { action: "handled" }; + +// ============================================================================ +// Tool Events +// ============================================================================ + +interface ToolCallEventBase { + type: "tool_call"; + toolCallId: string; +} + +export interface BashToolCallEvent extends ToolCallEventBase { + toolName: "bash"; + input: BashToolInput; +} + +export interface PowerShellToolCallEvent extends ToolCallEventBase { + toolName: "powershell"; + input: PowerShellToolInput; +} + +export interface ReadToolCallEvent extends ToolCallEventBase { + toolName: "read"; + input: ReadToolInput; +} + +export interface EditToolCallEvent extends ToolCallEventBase { + toolName: "edit"; + input: EditToolInput; +} + +export interface WriteToolCallEvent extends ToolCallEventBase { + toolName: "write"; + input: WriteToolInput; +} + +export interface GrepToolCallEvent extends ToolCallEventBase { + toolName: "grep"; + input: GrepToolInput; +} + +export interface FindToolCallEvent extends ToolCallEventBase { + toolName: "find"; + input: FindToolInput; +} + +export interface LsToolCallEvent extends ToolCallEventBase { + toolName: "ls"; + input: LsToolInput; +} + +export interface CustomToolCallEvent extends ToolCallEventBase { + toolName: string; + input: Record; +} + +/** + * Fired before a tool executes. Can block. + * + * `event.input` is mutable. Mutate it in place to patch tool arguments before execution. + * Later `tool_call` handlers see earlier mutations. No re-validation is performed after mutation. + */ +export type ToolCallEvent = + | BashToolCallEvent + | PowerShellToolCallEvent + | ReadToolCallEvent + | EditToolCallEvent + | WriteToolCallEvent + | GrepToolCallEvent + | FindToolCallEvent + | LsToolCallEvent + | CustomToolCallEvent; + +interface ToolResultEventBase { + type: "tool_result"; + toolCallId: string; + input: Record; + content: (TextContent | ImageContent)[]; + isError: boolean; + /** Usage from the tool execution itself, if available. */ + usage?: Usage; +} + +export interface BashToolResultEvent extends ToolResultEventBase { + toolName: "bash"; + details: BashToolDetails | undefined; +} + +export interface PowerShellToolResultEvent extends ToolResultEventBase { + toolName: "powershell"; + details: PowerShellToolDetails | undefined; +} + +export interface ReadToolResultEvent extends ToolResultEventBase { + toolName: "read"; + details: ReadToolDetails | undefined; +} + +export interface EditToolResultEvent extends ToolResultEventBase { + toolName: "edit"; + details: EditToolDetails | undefined; +} + +export interface WriteToolResultEvent extends ToolResultEventBase { + toolName: "write"; + details: undefined; +} + +export interface GrepToolResultEvent extends ToolResultEventBase { + toolName: "grep"; + details: GrepToolDetails | undefined; +} + +export interface FindToolResultEvent extends ToolResultEventBase { + toolName: "find"; + details: FindToolDetails | undefined; +} + +export interface LsToolResultEvent extends ToolResultEventBase { + toolName: "ls"; + details: LsToolDetails | undefined; +} + +export interface CustomToolResultEvent extends ToolResultEventBase { + toolName: string; + details: unknown; +} + +/** Fired after a tool executes. Can modify result. */ +export type ToolResultEvent = + | BashToolResultEvent + | PowerShellToolResultEvent + | ReadToolResultEvent + | EditToolResultEvent + | WriteToolResultEvent + | GrepToolResultEvent + | FindToolResultEvent + | LsToolResultEvent + | CustomToolResultEvent; + +// Type guards for ToolResultEvent +export function isBashToolResult(e: ToolResultEvent): e is BashToolResultEvent { + return e.toolName === "bash"; +} +export function isPowerShellToolResult(e: ToolResultEvent): e is PowerShellToolResultEvent { + return e.toolName === "powershell"; +} +export function isReadToolResult(e: ToolResultEvent): e is ReadToolResultEvent { + return e.toolName === "read"; +} +export function isEditToolResult(e: ToolResultEvent): e is EditToolResultEvent { + return e.toolName === "edit"; +} +export function isWriteToolResult(e: ToolResultEvent): e is WriteToolResultEvent { + return e.toolName === "write"; +} +export function isGrepToolResult(e: ToolResultEvent): e is GrepToolResultEvent { + return e.toolName === "grep"; +} +export function isFindToolResult(e: ToolResultEvent): e is FindToolResultEvent { + return e.toolName === "find"; +} +export function isLsToolResult(e: ToolResultEvent): e is LsToolResultEvent { + return e.toolName === "ls"; +} + +/** + * Type guard for narrowing ToolCallEvent by tool name. + * + * Built-in tools narrow automatically (no type params needed): + * ```ts + * if (isToolCallEventType("bash", event)) { + * event.input.command; // string + * } + * ``` + * + * Custom tools require explicit type parameters: + * ```ts + * if (isToolCallEventType<"my_tool", MyToolInput>("my_tool", event)) { + * event.input.action; // typed + * } + * ``` + * + * Note: Direct narrowing via `event.toolName === "bash"` doesn't work because + * CustomToolCallEvent.toolName is `string` which overlaps with all literals. + */ +export function isToolCallEventType(toolName: "bash", event: ToolCallEvent): event is BashToolCallEvent; +export function isToolCallEventType(toolName: "powershell", event: ToolCallEvent): event is PowerShellToolCallEvent; +export function isToolCallEventType(toolName: "read", event: ToolCallEvent): event is ReadToolCallEvent; +export function isToolCallEventType(toolName: "edit", event: ToolCallEvent): event is EditToolCallEvent; +export function isToolCallEventType(toolName: "write", event: ToolCallEvent): event is WriteToolCallEvent; +export function isToolCallEventType(toolName: "grep", event: ToolCallEvent): event is GrepToolCallEvent; +export function isToolCallEventType(toolName: "find", event: ToolCallEvent): event is FindToolCallEvent; +export function isToolCallEventType(toolName: "ls", event: ToolCallEvent): event is LsToolCallEvent; +export function isToolCallEventType>( + toolName: TName, + event: ToolCallEvent, +): event is ToolCallEvent & { toolName: TName; input: TInput }; +export function isToolCallEventType(toolName: string, event: ToolCallEvent): boolean { + return event.toolName === toolName; +} + +/** Union of all event types */ +export type ExtensionEvent = + | ProjectTrustEvent + | ResourcesDiscoverEvent + | SessionEvent + | ContextEvent + | BeforeProviderRequestEvent + | BeforeProviderHeadersEvent + | AfterProviderResponseEvent + | BeforeAgentStartEvent + | AgentStartEvent + | AgentEndEvent + | AgentSettledEvent + | UIPromptStartEvent + | UIPromptEndEvent + | TurnStartEvent + | TurnEndEvent + | MessageStartEvent + | MessageUpdateEvent + | MessageEndEvent + | ToolExecutionStartEvent + | ToolExecutionUpdateEvent + | ToolExecutionEndEvent + | ModelSelectEvent + | ThinkingLevelSelectEvent + | UserBashEvent + | InputEvent + | ToolCallEvent + | ToolResultEvent; + +// ============================================================================ +// Event Results +// ============================================================================ + +export interface ContextEventResult { + messages?: AgentMessage[]; +} + +export type BeforeProviderRequestEventResult = unknown; + +export interface ToolCallEventResult { + /** Block tool execution. To modify arguments, mutate `event.input` in place instead. */ + block?: boolean; + reason?: string; + /** + * Hint that the agent should stop after the current tool batch when this call is blocked. + * Early termination only happens when every finalized tool result in the batch sets this to true. + */ + terminate?: boolean; +} + +/** Result from user_bash event handler */ +export interface UserBashEventResult { + /** Custom operations to use for execution */ + operations?: BashOperations; + /** Full replacement: extension handled execution, use this result */ + result?: BashResult; +} + +export interface ToolResultEventResult { + content?: (TextContent | ImageContent)[]; + details?: unknown; + isError?: boolean; + usage?: Usage; +} + +export interface MessageEndEventResult { + /** Replace the finalized message. The replacement must keep the original message role. */ + message?: AgentMessage; +} + +export interface BeforeAgentStartEventResult { + message?: Pick; + /** Replace the system prompt for this turn. If multiple extensions return this, they are chained. */ + systemPrompt?: string; +} + +export interface SessionBeforeSwitchResult { + cancel?: boolean; +} + +export interface SessionBeforeForkResult { + cancel?: boolean; + skipConversationRestore?: boolean; +} + +export interface SessionBeforeCompactResult { + cancel?: boolean; + compaction?: CompactionResult; +} + +export interface SessionBeforeTreeResult { + cancel?: boolean; + summary?: { + summary: string; + details?: unknown; + usage?: Usage; + }; + /** Override custom instructions for summarization */ + customInstructions?: string; + /** Override whether customInstructions replaces the default prompt */ + replaceInstructions?: boolean; + /** Override label to attach to the branch summary entry */ + label?: string; +} + +// ============================================================================ +// Message and Entry Rendering +// ============================================================================ + +export interface MessageRenderOptions { + expanded: boolean; + /** Horizontal padding configured by the outputPad setting. */ + outputPad: number; +} + +export interface MarkdownTransformContext { + messageType: "user" | "assistant" | "assistant-thinking"; + isStreaming: boolean; + availableWidth: number; +} + +export type MarkdownTransformer = (markdown: string, context: MarkdownTransformContext) => string; + +export interface EntryRenderOptions { + expanded: boolean; +} + +export type MessageRenderer = ( + message: CustomMessage, + options: MessageRenderOptions, + theme: Theme, +) => Component | undefined; + +export type EntryRenderer = ( + entry: CustomEntry, + options: EntryRenderOptions, + theme: Theme, +) => Component | undefined; + +// ============================================================================ +// Command Registration +// ============================================================================ + +export interface RegisteredCommand { + name: string; + sourceInfo: SourceInfo; + description?: string; + getArgumentCompletions?: (argumentPrefix: string) => AutocompleteItem[] | null | Promise; + handler: (args: string, ctx: ExtensionCommandContext) => Promise; +} + +export interface ResolvedCommand extends RegisteredCommand { + invocationName: string; +} + +// ============================================================================ +// Extension API +// ============================================================================ + +/** Handler function type for events */ +// biome-ignore lint/suspicious/noConfusingVoidType: void allows bare return statements +export type ExtensionHandler = (event: E, ctx: ExtensionContext) => Promise | R | void; + +/** + * ExtensionAPI passed to extension factory functions. + */ +export interface ExtensionAPI { + // ========================================================================= + // Event Subscription + // ========================================================================= + + on(event: "project_trust", handler: ProjectTrustHandler): void; + on(event: "resources_discover", handler: ExtensionHandler): void; + on(event: "session_start", handler: ExtensionHandler): void; + on(event: "session_info_changed", handler: ExtensionHandler): void; + on( + event: "session_before_switch", + handler: ExtensionHandler, + ): void; + on(event: "session_before_fork", handler: ExtensionHandler): void; + on( + event: "session_before_compact", + handler: ExtensionHandler, + ): void; + on(event: "session_compact", handler: ExtensionHandler): void; + on(event: "session_compact_failed", handler: ExtensionHandler): void; + on(event: "session_shutdown", handler: ExtensionHandler): void; + on(event: "session_before_tree", handler: ExtensionHandler): void; + on(event: "session_tree", handler: ExtensionHandler): void; + on(event: "context", handler: ExtensionHandler): void; + on( + event: "before_provider_request", + handler: ExtensionHandler, + ): void; + on(event: "before_provider_headers", handler: ExtensionHandler): void; + on(event: "after_provider_response", handler: ExtensionHandler): void; + on(event: "before_agent_start", handler: ExtensionHandler): void; + on(event: "agent_start", handler: ExtensionHandler): void; + on(event: "agent_end", handler: ExtensionHandler): void; + on(event: "agent_settled", handler: ExtensionHandler): void; + on(event: "ui_prompt_start", handler: ExtensionHandler): void; + on(event: "ui_prompt_end", handler: ExtensionHandler): void; + on(event: "turn_start", handler: ExtensionHandler): void; + on(event: "turn_end", handler: ExtensionHandler): void; + on(event: "message_start", handler: ExtensionHandler): void; + on(event: "message_update", handler: ExtensionHandler): void; + on(event: "message_end", handler: ExtensionHandler): void; + on(event: "tool_execution_start", handler: ExtensionHandler): void; + on(event: "tool_execution_update", handler: ExtensionHandler): void; + on(event: "tool_execution_end", handler: ExtensionHandler): void; + on(event: "model_select", handler: ExtensionHandler): void; + on(event: "thinking_level_select", handler: ExtensionHandler): void; + on(event: "tool_call", handler: ExtensionHandler): void; + on(event: "tool_result", handler: ExtensionHandler): void; + on(event: "user_bash", handler: ExtensionHandler): void; + on(event: "input", handler: ExtensionHandler): void; + + // ========================================================================= + // Tool Registration + // ========================================================================= + + /** Register a tool that the LLM can call. */ + registerTool( + tool: ToolDefinition, + ): void; + + // ========================================================================= + // Command, Shortcut, Flag Registration + // ========================================================================= + + /** Register a custom command. */ + registerCommand(name: string, options: Omit): void; + + /** Register a keyboard shortcut. */ + registerShortcut( + shortcut: KeyId, + options: { + description?: string; + handler: (ctx: ExtensionContext) => Promise | void; + }, + ): void; + + /** Register a CLI flag. */ + registerFlag( + name: string, + options: + | { + description?: string; + type: "boolean"; + default?: boolean; + } + | { + description?: string; + type: "string"; + default?: string; + }, + ): void; + + /** Get the value of a registered CLI flag. */ + getFlag(name: string): boolean | string | undefined; + + // ========================================================================= + // Message Rendering + // ========================================================================= + + /** Register a custom renderer for CustomMessageEntry. */ + registerMessageRenderer(customType: string, renderer: MessageRenderer): void; + + /** Register a transformer for user and assistant Markdown before Pi renders it in the interactive transcript. */ + registerMarkdownTransformer(transformer: MarkdownTransformer): void; + + /** Register a custom renderer for CustomEntry. Custom entries do not participate in LLM context. */ + registerEntryRenderer(customType: string, renderer: EntryRenderer): void; + + // ========================================================================= + // Actions + // ========================================================================= + + /** Send a custom message to the session. */ + sendMessage( + message: Pick, "customType" | "content" | "display" | "details">, + options?: { triggerTurn?: boolean; deliverAs?: "steer" | "followUp" | "nextTurn" }, + ): void; + + /** + * Send a user message to the agent. Always triggers a turn. + * When the agent is streaming, use deliverAs to specify how to queue the message. + * Set expandPromptTemplates to dispatch extension commands and expand skill commands and prompt templates. + */ + sendUserMessage( + content: string | (TextContent | ImageContent)[], + options?: { deliverAs?: "steer" | "followUp"; expandPromptTemplates?: boolean }, + ): void; + + /** Append a custom entry to the session for state persistence (not sent to LLM). */ + appendEntry(customType: string, data?: T): void; + + // ========================================================================= + // Session Metadata + // ========================================================================= + + /** Set the session display name (shown in session selector). */ + setSessionName(name: string): void; + + /** Get the current session name, if set. */ + getSessionName(): string | undefined; + + /** Set or clear a label on an entry. Labels are user-defined markers for bookmarking/navigation. */ + setLabel(entryId: string, label: string | undefined): void; + + /** Execute a shell command. */ + exec(command: string, args: string[], options?: ExecOptions): Promise; + + /** Get the list of currently active tool names. */ + getActiveTools(): string[]; + + /** Get all configured tools with parameter schema, prompt guidelines, and source metadata. */ + getAllTools(): ToolInfo[]; + + /** Set the active tools by name. */ + setActiveTools(toolNames: string[]): void; + + /** Get available slash commands in the current session. */ + getCommands(): SlashCommandInfo[]; + + // ========================================================================= + // Model and Thinking Level + // ========================================================================= + + /** + * Set the model for the current session without changing the configured default for new sessions. + * Returns false if authentication is not configured for the model's provider. + */ + setModel(model: Model): Promise; + + /** Get current thinking level. */ + getThinkingLevel(): ThinkingLevel; + + /** + * Set the thinking level (clamped to model capabilities) for the current session without changing the configured default + * for new sessions. + */ + setThinkingLevel(level: ThinkingLevel): void; + + // ========================================================================= + // Provider Registration + // ========================================================================= + + /** + * Register or override a model provider. + * + * If `models` is provided: replaces all existing models for this provider. + * If only `baseUrl` is provided: overrides the URL for existing models. + * If `oauth` is provided: registers OAuth provider for /login support. + * If `streamSimple` is provided: registers a custom API stream handler. + * + * During initial extension load this call is queued and applied once the + * runner has bound its context. After that it takes effect immediately, so + * it is safe to call from command handlers or event callbacks without + * requiring a `/reload`. + * + * @example + * // Register a new provider with custom models + * pi.registerProvider("my-proxy", { + * baseUrl: "https://proxy.example.com", + * apiKey: "$PROXY_API_KEY", + * api: "anthropic-messages", + * models: [ + * { + * id: "claude-sonnet-4-20250514", + * name: "Claude 4 Sonnet (proxy)", + * reasoning: false, + * input: ["text", "image"], + * cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 }, + * contextWindow: 200000, + * maxTokens: 16384 + * } + * ] + * }); + * + * @example + * // Override baseUrl for an existing provider + * pi.registerProvider("anthropic", { + * baseUrl: "https://proxy.example.com" + * }); + * + * @example + * // Register provider with OAuth support + * pi.registerProvider("corporate-ai", { + * baseUrl: "https://ai.corp.com", + * api: "openai-responses", + * models: [...], + * oauth: { + * name: "Corporate AI (SSO)", + * async login(callbacks) { ... }, + * async refreshToken(credentials) { ... }, + * getApiKey(credentials) { return credentials.access; } + * } + * }); + */ + registerProvider(provider: Provider): void; + registerProvider(name: string, config: ProviderConfig): void; + + /** + * Unregister a previously registered provider. + * + * Removes all models belonging to the named provider and restores any + * built-in models that were overridden by it. Has no effect if the provider + * is not currently registered. + * + * Like `registerProvider`, this takes effect immediately when called after + * the initial load phase. + * + * @example + * pi.unregisterProvider("my-proxy"); + */ + unregisterProvider(name: string): void; + + /** Shared event bus for extension communication. */ + events: EventBus; +} + +// ============================================================================ +// Provider Registration Types +// ============================================================================ + +/** Configuration for registering a provider via pi.registerProvider(). */ +export interface ProviderConfig { + /** Display name for the provider in UI. */ + name?: string; + /** Base URL for the API endpoint. Required when defining models. */ + baseUrl?: string; + /** API key literal, env interpolation ($ENV_VAR or ${ENV_VAR}), or leading !command. Required when defining models (unless oauth provided). */ + apiKey?: string; + /** API type. Required at provider or model level when defining models. */ + api?: Api; + /** + * Optional streamSimple handler for custom APIs. + * Implementations must invoke `options.onPayload` before sending the provider request and use any + * returned replacement payload. They must invoke `options.onResponse` after receiving the response + * and before consuming its body, matching built-in providers. + */ + streamSimple?: (model: Model, context: Context, options?: SimpleStreamOptions) => AssistantMessageEventStream; + /** Custom headers to include in requests. */ + headers?: Record; + /** If true, adds Authorization: Bearer header with the resolved API key. */ + authHeader?: boolean; + /** Models to register. If provided, replaces all existing models for this provider. */ + models?: ProviderModelConfig[]; + /** + * Refresh this provider's model list. The returned list replaces extension-provided models. + * Use context.publish({ persist: entry }) when the catalog should persist across sessions. + */ + refreshModels?(context: RefreshModelsContext): Promise; + /** OAuth provider for /login support. The `id` is set automatically from the provider name. */ + oauth?: { + /** Display name for the provider in login UI. */ + name: string; + /** Whether access through this auth method is backed by a provider subscription. */ + isSubscription?: boolean; + /** @deprecated Retained for source compatibility; canonical auth flows ignore it. */ + usesCallbackServer?: boolean; + /** Run the login flow, return credentials to persist. */ + login(callbacks: OAuthLoginCallbacks): Promise; + /** Refresh expired credentials, return updated credentials to persist. */ + refreshToken(credentials: OAuthCredentials, signal: AbortSignal): Promise; + /** Convert credentials to API key string for the provider. */ + getApiKey(credentials: OAuthCredentials): string; + /** Legacy synchronous credential-dependent model projection. */ + modifyModels?(models: Model[], credentials: OAuthCredentials): Model[]; + }; +} + +/** Configuration for a model within a provider. */ +export interface ProviderModelConfig { + /** Model ID (e.g., "claude-sonnet-4-20250514"). */ + id: string; + /** Display name (e.g., "Claude 4 Sonnet"). */ + name: string; + /** API type override for this model. */ + api?: Api; + /** API endpoint URL override for this model. */ + baseUrl?: string; + /** Whether the model supports extended thinking. */ + reasoning: boolean; + /** Maps pi thinking levels to provider/model-specific values; null marks a level unsupported. */ + thinkingLevelMap?: Model["thinkingLevelMap"]; + /** Supported input types. */ + input: ("text" | "image")[]; + /** Per-million-token cost rates and optional request-wide input pricing tiers. */ + cost: Model["cost"]; + /** Maximum context window size in tokens. */ + contextWindow: number; + /** Maximum output tokens. */ + maxTokens: number; + /** Custom headers for this model. */ + headers?: Record; + /** OpenAI compatibility settings. */ + compat?: Model["compat"]; +} + +/** Extension factory function type. Supports both sync and async initialization. */ +export type ExtensionFactory = (pi: ExtensionAPI) => void | Promise; + +export type InlineExtension = + | ExtensionFactory + | { + /** Display name shown as `` in the startup Extensions list. */ + name: string; + factory: ExtensionFactory; + /** Omit this extension from the startup Extensions list. */ + hidden?: boolean; + }; + +// ============================================================================ +// Loaded Extension Types +// ============================================================================ + +export interface RegisteredTool { + definition: ToolDefinition; + sourceInfo: SourceInfo; +} + +export interface ExtensionFlag { + name: string; + description?: string; + type: "boolean" | "string"; + default?: boolean | string; + extensionPath: string; +} + +export interface ExtensionShortcut { + shortcut: KeyId; + description?: string; + handler: (ctx: ExtensionContext) => Promise | void; + extensionPath: string; +} + +type HandlerFn = (...args: unknown[]) => Promise; + +export type SendMessageHandler = ( + message: Pick, "customType" | "content" | "display" | "details">, + options?: { triggerTurn?: boolean; deliverAs?: "steer" | "followUp" | "nextTurn" }, +) => void; + +export type SendUserMessageHandler = ( + content: string | (TextContent | ImageContent)[], + options?: { deliverAs?: "steer" | "followUp"; expandPromptTemplates?: boolean }, +) => void; + +export type AppendEntryHandler = (customType: string, data?: T) => void; + +export type SetSessionNameHandler = (name: string) => void; + +export type GetSessionNameHandler = () => string | undefined; + +export type GetActiveToolsHandler = () => string[]; + +/** Tool info with name, description, parameter schema, prompt guidelines, and source metadata. */ +export type ToolInfo = Pick & { + sourceInfo: SourceInfo; +}; + +export type GetAllToolsHandler = () => ToolInfo[]; + +export type GetCommandsHandler = () => SlashCommandInfo[]; + +export type SetActiveToolsHandler = (toolNames: string[]) => void; + +export type RefreshToolsHandler = () => void; + +export type SetModelHandler = (model: Model) => Promise; + +export type GetThinkingLevelHandler = () => ThinkingLevel; + +export type SetThinkingLevelHandler = (level: ThinkingLevel) => void; + +export type SetLabelHandler = (entryId: string, label: string | undefined) => void; + +/** + * Shared state created by loader, used during registration and runtime. + * Contains flag values (defaults set during registration, CLI values set after). + */ +export interface ExtensionRuntimeState { + flagValues: Map; + /** Legacy provider-config registrations queued during extension loading, processed when runner binds. */ + pendingProviderRegistrations: Array<{ name: string; config: ProviderConfig; extensionPath: string }>; + /** Native pi-ai provider registrations queued during extension loading, processed when runner binds. */ + pendingNativeProviderRegistrations: Array<{ provider: Provider; extensionPath: string }>; + /** Throws when this extension instance is stale after runtime replacement. */ + assertActive: () => void; + /** Marks this extension instance as stale after runtime replacement or reload. */ + invalidate: (message?: string) => void; + /** Retain an event-bus subscription until this runtime is invalidated. */ + trackEventBusSubscription: (unsubscribe: () => void) => () => void; + /** + * Register or unregister a provider. + * + * Before bindCore(): queues registrations / removes from queue. + * After bindCore(): calls ModelRegistry directly for immediate effect. + */ + registerProvider: (name: string, config: ProviderConfig, extensionPath?: string) => void; + registerNativeProvider: (provider: Provider, extensionPath?: string) => void; + unregisterProvider: (name: string, extensionPath?: string) => void; +} + +/** + * Action implementations for pi.* API methods. + * Provided to runner.initialize(), copied into the shared runtime. + */ +export interface ExtensionActions { + sendMessage: SendMessageHandler; + sendUserMessage: SendUserMessageHandler; + appendEntry: AppendEntryHandler; + setSessionName: SetSessionNameHandler; + getSessionName: GetSessionNameHandler; + setLabel: SetLabelHandler; + getActiveTools: GetActiveToolsHandler; + getAllTools: GetAllToolsHandler; + setActiveTools: SetActiveToolsHandler; + refreshTools: RefreshToolsHandler; + getCommands: GetCommandsHandler; + setModel: SetModelHandler; + getThinkingLevel: GetThinkingLevelHandler; + setThinkingLevel: SetThinkingLevelHandler; +} + +/** + * Actions for ExtensionContext (ctx.* in event handlers). + * Required by all modes. + */ +export interface ExtensionContextActions { + getModel: () => Model | undefined; + getScopedModels: () => readonly ScopedModel[]; + isIdle: () => boolean; + isProjectTrusted: () => boolean; + getSignal: () => AbortSignal | undefined; + abort: () => void; + hasPendingMessages: () => boolean; + shutdown: () => void; + getContextUsage: () => ContextUsage | undefined; + compact: (options?: CompactOptions) => void; + getSystemPrompt: () => string; + getSystemPromptOptions?: () => BuildSystemPromptOptions; +} + +/** + * Actions for ExtensionCommandContext (ctx.* in command handlers). + * Only needed for interactive mode where extension commands are invokable. + */ +export interface ExtensionCommandContextActions { + waitForIdle: () => Promise; + newSession: (options?: { + parentSession?: string; + setup?: (sessionManager: SessionManager) => Promise; + withSession?: (ctx: ReplacedSessionContext) => Promise; + }) => Promise<{ cancelled: boolean }>; + fork: ( + entryId: string, + options?: { position?: "before" | "at"; withSession?: (ctx: ReplacedSessionContext) => Promise }, + ) => Promise<{ cancelled: boolean }>; + navigateTree: ( + targetId: string, + options?: { summarize?: boolean; customInstructions?: string; replaceInstructions?: boolean; label?: string }, + ) => Promise<{ cancelled: boolean }>; + switchSession: ( + sessionPath: string, + options?: { withSession?: (ctx: ReplacedSessionContext) => Promise }, + ) => Promise<{ cancelled: boolean }>; + reload: () => Promise; +} + +/** + * Full runtime = state + actions. + * Created by loader with throwing action stubs, completed by runner.initialize(). + */ +export interface ExtensionRuntime extends ExtensionRuntimeState, ExtensionActions {} + +/** Loaded extension with all registered items. */ +export interface Extension { + path: string; + resolvedPath: string; + hidden?: boolean; + sourceInfo: SourceInfo; + handlers: Map; + tools: Map; + messageRenderers: Map; + markdownTransformer?: MarkdownTransformer; + entryRenderers?: Map; + commands: Map; + flags: Map; + shortcuts: Map; +} + +/** Result of loading extensions. */ +export interface LoadExtensionsResult { + extensions: Extension[]; + errors: Array<{ path: string; error: string }>; + /** Shared runtime - actions are throwing stubs until runner.initialize() */ + runtime: ExtensionRuntime; +} + +// ============================================================================ +// Extension Error +// ============================================================================ + +export interface ExtensionError { + extensionPath: string; + event: string; + error: string; + stack?: string; +} diff --git a/packages/coding-agent/src/core/extensions/wrapper.ts b/packages/coding-agent/src/core/extensions/wrapper.ts new file mode 100644 index 0000000000000000000000000000000000000000..9084a1daabaa3287ef9d7429d4be846cf62c79c4 --- /dev/null +++ b/packages/coding-agent/src/core/extensions/wrapper.ts @@ -0,0 +1,45 @@ +/** + * Tool wrappers for extension-registered tools. + * + * These wrappers only adapt tool execution so extension tools receive the runner context. + * Tool call and tool result interception is handled by AgentSession via agent-core hooks. + */ + +import type { AgentTool } from "@earendil-works/pi-agent-core"; +import { wrapToolDefinition } from "../tools/tool-definition-wrapper.ts"; +import type { ExtensionRunner } from "./runner.ts"; +import type { RegisteredTool } from "./types.ts"; + +/** + * Wrap a RegisteredTool into an AgentTool. + * Uses the runner's createContext() for consistent context across tools and event handlers. + */ +export function wrapRegisteredTool(registeredTool: RegisteredTool, runner: ExtensionRunner): AgentTool { + const tool = wrapToolDefinition(registeredTool.definition, () => runner.createContext()); + const execute = tool.execute; + return { + ...tool, + execute: async (toolCallId, params, signal, onUpdate) => { + const activeBefore = runner.getActiveTools(); + const result = await execute(toolCallId, params, signal, onUpdate); + const activeAfter = runner.getActiveTools(); + if (!activeBefore.every((name) => activeAfter.includes(name))) return result; + + const beforeNames = new Set(activeBefore); + const addedToolNames = activeAfter.filter((name) => !beforeNames.has(name)); + if (addedToolNames.length === 0) return result; + return { + ...result, + addedToolNames: [...new Set([...(result.addedToolNames ?? []), ...addedToolNames])], + }; + }, + }; +} + +/** + * Wrap all registered tools into AgentTools. + * Uses the runner's createContext() for consistent context across tools and event handlers. + */ +export function wrapRegisteredTools(registeredTools: RegisteredTool[], runner: ExtensionRunner): AgentTool[] { + return registeredTools.map((tool) => wrapRegisteredTool(tool, runner)); +} diff --git a/packages/coding-agent/src/core/tools/bash.ts b/packages/coding-agent/src/core/tools/bash.ts new file mode 100644 index 0000000000000000000000000000000000000000..b7c9973a457e07e900a0dcc81e41a22b4b4971c5 --- /dev/null +++ b/packages/coding-agent/src/core/tools/bash.ts @@ -0,0 +1,400 @@ +import { constants } from "node:fs"; +import { access as fsAccess } from "node:fs/promises"; +import type { AgentTool } from "@earendil-works/pi-agent-core"; +import { spawn } from "child_process"; +import { type Static, Type } from "typebox"; +import { waitForChildProcess } from "../../utils/child-process.ts"; +import { + getShellConfig, + getShellEnv, + killProcessTree, + type ShellConfig, + trackDetachedChildPid, + untrackDetachedChildPid, +} from "../../utils/shell.ts"; +import type { ExtensionContext, ToolDefinition } from "../extensions/types.ts"; +import { OutputAccumulator } from "./output-accumulator.ts"; +import { BASH_UPDATE_THROTTLE_MS, createShellRenderers } from "./renderers/bash.ts"; +import { wrapToolDefinition } from "./tool-definition-wrapper.ts"; +import { DEFAULT_MAX_BYTES, DEFAULT_MAX_LINES, formatSize, type TruncationResult } from "./truncate.ts"; + +const MAX_TIMEOUT_MS = 2_147_483_647; +const MAX_TIMEOUT_SECONDS = MAX_TIMEOUT_MS / 1000; + +function resolveTimeoutMs(timeout: number | undefined): number | undefined { + if (timeout === undefined) return undefined; + if (!Number.isFinite(timeout) || timeout <= 0) { + throw new Error("Invalid timeout: must be a finite number of seconds"); + } + + const timeoutMs = timeout * 1000; + if (timeoutMs > MAX_TIMEOUT_MS) { + throw new Error(`Invalid timeout: maximum is ${MAX_TIMEOUT_SECONDS} seconds`); + } + return timeoutMs; +} + +const bashSchema = Type.Object({ + command: Type.String({ description: "Shell command to execute" }), + timeout: Type.Optional(Type.Number({ description: "Timeout in seconds (optional, no default timeout)" })), +}); + +export const bashToolSystemPromptContribution = { + snippet: "Execute bash commands (ls, grep, find, etc.)", + guidelines: ["You can inspect PI_* environment variables for current model and session details."], +} as const; + +export type BashToolInput = Static; + +export interface BashToolDetails { + truncation?: TruncationResult; + fullOutputPath?: string; +} + +/** + * Pluggable operations for the bash tool. + * Override these to delegate command execution to remote systems (for example SSH). + */ +export interface BashOperations { + /** + * Execute a command and stream output. + * @param command The command to execute + * @param cwd Working directory + * @param options Execution options + * @returns Promise resolving to exit code (null if killed) + */ + exec: ( + command: string, + cwd: string, + options: { + onData: (data: Buffer) => void; + signal?: AbortSignal; + timeout?: number; + env?: NodeJS.ProcessEnv; + }, + ) => Promise<{ exitCode: number | null }>; +} + +/** Shared process execution used by the built-in shell tools. */ +export function createLocalShellOperations(shellName: string, resolveShellConfig: () => ShellConfig): BashOperations { + return { + exec: async (command, cwd, { onData, signal, timeout, env }) => { + const timeoutMs = resolveTimeoutMs(timeout); + if (signal?.aborted) { + throw new Error("aborted"); + } + const shellConfig = resolveShellConfig(); + try { + await fsAccess(cwd, constants.F_OK); + } catch { + throw new Error(`Working directory does not exist: ${cwd}\nCannot execute ${shellName} commands.`); + } + + const commandFromStdin = shellConfig.commandTransport === "stdin"; + const child = spawn(shellConfig.shell, commandFromStdin ? shellConfig.args : [...shellConfig.args, command], { + cwd, + detached: process.platform !== "win32", + env: env ?? getShellEnv(), + stdio: [commandFromStdin ? "pipe" : "ignore", "pipe", "pipe"], + windowsHide: true, + }); + if (commandFromStdin) { + child.stdin?.on("error", () => {}); + child.stdin?.end(command); + } + if (child.pid) trackDetachedChildPid(child.pid); + let timedOut = false; + let timeoutHandle: NodeJS.Timeout | undefined; + const onAbort = () => { + if (child.pid) killProcessTree(child.pid); + }; + + try { + // Set timeout if provided. + if (timeoutMs !== undefined) { + timeoutHandle = setTimeout(() => { + timedOut = true; + if (child.pid) killProcessTree(child.pid); + }, timeoutMs); + } + // Stream stdout and stderr. + child.stdout?.on("data", onData); + child.stderr?.on("data", onData); + // Handle abort signal by killing the entire process tree. + if (signal) { + if (signal.aborted) onAbort(); + else signal.addEventListener("abort", onAbort, { once: true }); + } + // Handle shell spawn errors and wait for the process to terminate without hanging + // on inherited stdio handles held by detached descendants. + const exitCode = await waitForChildProcess(child); + if (signal?.aborted) { + throw new Error("aborted"); + } + if (timedOut) { + throw new Error(`timeout:${timeout}`); + } + return { exitCode }; + } finally { + if (child.pid) untrackDetachedChildPid(child.pid); + if (timeoutHandle) clearTimeout(timeoutHandle); + if (signal) signal.removeEventListener("abort", onAbort); + } + }, + }; +} + +/** + * Create bash operations using pi's built-in local shell execution backend. + * + * This is useful for extensions that intercept user_bash and still want pi's + * standard local shell behavior while wrapping or rewriting commands. + */ +export function createLocalBashOperations(options?: { shellPath?: string }): BashOperations { + return createLocalShellOperations("bash", () => getShellConfig(options?.shellPath)); +} + +export interface BashSpawnContext { + command: string; + cwd: string; + env: NodeJS.ProcessEnv; +} + +export type BashSpawnHook = (context: BashSpawnContext) => BashSpawnContext; + +function resolveSpawnContext( + command: string, + cwd: string, + spawnHook: BashSpawnHook | undefined, + exposeSessionEnvironment: boolean, + ctx: ExtensionContext | undefined, +): BashSpawnContext { + const env = { ...getShellEnv() }; + delete env.PI_SESSION_ID; + delete env.PI_SESSION_FILE; + delete env.PI_PROVIDER; + delete env.PI_MODEL; + delete env.PI_REASONING_LEVEL; + if (exposeSessionEnvironment && ctx) { + const model = ctx.model; + env.PI_SESSION_ID = ctx.sessionManager.getSessionId(); + const sessionFile = ctx.sessionManager.getSessionFile(); + if (sessionFile) env.PI_SESSION_FILE = sessionFile; + if (model) { + env.PI_PROVIDER = model.provider; + env.PI_MODEL = model.id; + } + if (ctx.thinkingLevel) env.PI_REASONING_LEVEL = ctx.thinkingLevel; + } + const baseContext: BashSpawnContext = { command, cwd, env }; + return spawnHook ? spawnHook(baseContext) : baseContext; +} + +export interface BashToolOptions { + /** Custom operations for command execution. Default: local shell */ + operations?: BashOperations; + /** Command prefix prepended to every command (for example shell setup commands) */ + commandPrefix?: string; + /** Optional explicit shell path from settings */ + shellPath?: string; + /** Expose current Pi session metadata as PI_* environment variables. Default: true */ + exposeSessionEnvironment?: boolean; + /** Hook to adjust command, cwd, or env before execution */ + spawnHook?: BashSpawnHook; +} + +export type BashRenderState = { + startedAt: number | undefined; + endedAt: number | undefined; + interval: NodeJS.Timeout | undefined; +}; + +export interface ShellToolConfig { + name: string; + label: string; + shellName: string; + prompt: string; + promptSnippet: string; + promptGuidelines?: readonly string[]; + tempFilePrefix: string; +} + +export function createShellToolDefinition( + cwd: string, + config: ShellToolConfig, + options?: BashToolOptions, +): ToolDefinition { + const ops = options?.operations ?? createLocalBashOperations({ shellPath: options?.shellPath }); + const commandPrefix = options?.commandPrefix; + const exposeSessionEnvironment = options?.exposeSessionEnvironment ?? true; + const spawnHook = options?.spawnHook; + return { + name: config.name, + label: config.label, + description: `Execute a ${config.shellName} command in the current working directory. Returns stdout and stderr. Output is truncated to last ${DEFAULT_MAX_LINES} lines or ${DEFAULT_MAX_BYTES / 1024}KB (whichever is hit first). If truncated, full output is saved to a temp file. Optionally provide a timeout in seconds.`, + promptSnippet: config.promptSnippet, + promptGuidelines: exposeSessionEnvironment && config.promptGuidelines ? [...config.promptGuidelines] : undefined, + parameters: bashSchema, + constrainedSampling: { type: "json_schema", strict: "prefer" }, + async execute( + _toolCallId, + { command, timeout }: { command: string; timeout?: number }, + signal?: AbortSignal, + onUpdate?, + ctx?: ExtensionContext, + ) { + const resolvedCommand = commandPrefix ? `${commandPrefix}\n${command}` : command; + const spawnContext = resolveSpawnContext( + resolvedCommand, + ctx?.cwd || cwd, + spawnHook, + exposeSessionEnvironment, + ctx, + ); + const output = new OutputAccumulator({ tempFilePrefix: config.tempFilePrefix }); + let acceptingOutput = true; + let updateTimer: NodeJS.Timeout | undefined; + let updateDirty = false; + let lastUpdateAt = 0; + + const emitOutputUpdate = () => { + if (!onUpdate || !updateDirty) return; + updateDirty = false; + lastUpdateAt = Date.now(); + const snapshot = output.snapshot({ persistIfTruncated: true }); + onUpdate({ + content: [{ type: "text", text: snapshot.content || "" }], + details: { + truncation: snapshot.truncation.truncated ? snapshot.truncation : undefined, + fullOutputPath: snapshot.fullOutputPath, + }, + }); + }; + + const clearUpdateTimer = () => { + if (updateTimer) { + clearTimeout(updateTimer); + updateTimer = undefined; + } + }; + + const scheduleOutputUpdate = () => { + if (!onUpdate) return; + updateDirty = true; + const delay = BASH_UPDATE_THROTTLE_MS - (Date.now() - lastUpdateAt); + if (delay <= 0) { + clearUpdateTimer(); + emitOutputUpdate(); + return; + } + updateTimer ??= setTimeout(() => { + updateTimer = undefined; + emitOutputUpdate(); + }, delay); + }; + + if (onUpdate) { + onUpdate({ content: [], details: undefined }); + } + + const handleData = (data: Buffer) => { + if (!acceptingOutput) return; + output.append(data); + scheduleOutputUpdate(); + }; + + const finishOutput = async () => { + acceptingOutput = false; + output.finish(); + clearUpdateTimer(); + emitOutputUpdate(); + const snapshot = output.snapshot({ persistIfTruncated: true }); + await output.closeTempFile(); + return snapshot; + }; + + const formatOutput = (snapshot: Awaited>, emptyText = "(no output)") => { + const truncation = snapshot.truncation; + let text = snapshot.content || emptyText; + let details: BashToolDetails | undefined; + if (truncation.truncated) { + details = { truncation, fullOutputPath: snapshot.fullOutputPath }; + const startLine = truncation.totalLines - truncation.outputLines + 1; + const endLine = truncation.totalLines; + if (truncation.lastLinePartial) { + const lastLineSize = formatSize(output.getLastLineBytes()); + text += `\n\n[Showing last ${formatSize(truncation.outputBytes)} of line ${endLine} (line is ${lastLineSize}). Full output: ${snapshot.fullOutputPath}]`; + } else if (truncation.truncatedBy === "lines") { + text += `\n\n[Showing lines ${startLine}-${endLine} of ${truncation.totalLines}. Full output: ${snapshot.fullOutputPath}]`; + } else { + text += `\n\n[Showing lines ${startLine}-${endLine} of ${truncation.totalLines} (${formatSize(DEFAULT_MAX_BYTES)} limit). Full output: ${snapshot.fullOutputPath}]`; + } + } + return { text, details }; + }; + + const appendStatus = (text: string, status: string) => `${text ? `${text}\n\n` : ""}${status}`; + + try { + let exitCode: number | null; + try { + const result = await ops.exec(spawnContext.command, spawnContext.cwd, { + onData: handleData, + signal, + timeout, + env: spawnContext.env, + }); + exitCode = result.exitCode; + } catch (err) { + const snapshot = await finishOutput(); + const { text } = formatOutput(snapshot, ""); + if (err instanceof Error && err.message === "aborted") { + throw new Error(appendStatus(text, "Command aborted")); + } + if (err instanceof Error && err.message.startsWith("timeout:")) { + const timeoutSecs = err.message.split(":")[1]; + throw new Error(appendStatus(text, `Command timed out after ${timeoutSecs} seconds`)); + } + throw err; + } + + const snapshot = await finishOutput(); + const { text: outputText, details } = formatOutput(snapshot); + if (exitCode !== 0 && exitCode !== null) { + throw new Error(appendStatus(outputText, `Command exited with code ${exitCode}`)); + } + return { content: [{ type: "text", text: outputText }], details }; + } finally { + clearUpdateTimer(); + } + }, + ...createShellRenderers(config.prompt), + }; +} + +const bashToolConfig: ShellToolConfig = { + name: "bash", + label: "bash", + shellName: "bash", + prompt: "$", + promptSnippet: bashToolSystemPromptContribution.snippet, + promptGuidelines: bashToolSystemPromptContribution.guidelines, + tempFilePrefix: "pi-bash", +}; + +export function createBashToolDefinition( + cwd: string, + options?: BashToolOptions, +): ToolDefinition { + return createShellToolDefinition(cwd, bashToolConfig, options); +} + +export function createBashTool(cwd: string, options?: BashToolOptions): AgentTool { + const definition = createBashToolDefinition(cwd, options); + const tool = wrapToolDefinition(definition); + Object.assign(tool, { + promptSnippet: definition.promptSnippet, + promptGuidelines: definition.promptGuidelines, + }); + return tool; +} diff --git a/packages/coding-agent/src/core/tools/edit-diff.ts b/packages/coding-agent/src/core/tools/edit-diff.ts new file mode 100644 index 0000000000000000000000000000000000000000..c79f5eb7b360c0f32c7ed1bb09d8483494b61ade --- /dev/null +++ b/packages/coding-agent/src/core/tools/edit-diff.ts @@ -0,0 +1,556 @@ +/** + * Shared diff computation utilities for the edit and similar tools. + */ + +import * as Diff from "diff"; +import { constants } from "fs"; +import { access, readFile } from "fs/promises"; +import { splitBom } from "../../utils/text.ts"; +import { resolveToCwd } from "./path-utils.ts"; + +export function detectLineEnding(content: string): "\r\n" | "\n" { + const crlfIdx = content.indexOf("\r\n"); + const lfIdx = content.indexOf("\n"); + if (lfIdx === -1) return "\n"; + if (crlfIdx === -1) return "\n"; + return crlfIdx < lfIdx ? "\r\n" : "\n"; +} + +export function normalizeToLF(text: string): string { + return text.replace(/\r\n/g, "\n").replace(/\r/g, "\n"); +} + +export function restoreLineEndings(text: string, ending: "\r\n" | "\n"): string { + return ending === "\r\n" ? text.replace(/\n/g, "\r\n") : text; +} + +/** + * Normalize text for fuzzy matching. Applies progressive transformations: + * - Strip trailing whitespace from each line + * - Normalize smart quotes to ASCII equivalents + * - Normalize Unicode dashes/hyphens to ASCII hyphen + * - Normalize special Unicode spaces to regular space + */ +export function normalizeForFuzzyMatch(text: string): string { + return ( + text + .normalize("NFKC") + // Strip trailing whitespace per line + .split("\n") + .map((line) => line.trimEnd()) + .join("\n") + // Smart single quotes → ' + .replace(/[\u2018\u2019\u201A\u201B]/g, "'") + // Smart double quotes → " + .replace(/[\u201C\u201D\u201E\u201F]/g, '"') + // Various dashes/hyphens → - + // U+2010 hyphen, U+2011 non-breaking hyphen, U+2012 figure dash, + // U+2013 en-dash, U+2014 em-dash, U+2015 horizontal bar, U+2212 minus + .replace(/[\u2010\u2011\u2012\u2013\u2014\u2015\u2212]/g, "-") + // Special spaces → regular space + // U+00A0 NBSP, U+2002-U+200A various spaces, U+202F narrow NBSP, + // U+205F medium math space, U+3000 ideographic space + .replace(/[\u00A0\u2002-\u200A\u202F\u205F\u3000]/g, " ") + ); +} + +function splitLinesWithEndings(content: string): string[] { + return content.match(/[^\n]*\n|[^\n]+/g) ?? []; +} + +interface LineSpan { + start: number; + end: number; +} + +interface MatchedEdit { + editIndex: number; + matchIndex: number; + matchLength: number; + newText: string; +} + +type TextReplacement = Pick; + +function getLineSpans(content: string): LineSpan[] { + let offset = 0; + return splitLinesWithEndings(content).map((line) => { + const span = { start: offset, end: offset + line.length }; + offset = span.end; + return span; + }); +} + +function getReplacementLineRange(lines: LineSpan[], replacement: TextReplacement) { + const replacementStart = replacement.matchIndex; + const replacementEnd = replacement.matchIndex + replacement.matchLength; + + let startLine = -1; + for (let i = 0; i < lines.length; i++) { + const line = lines[i]; + if (replacementStart >= line.start && replacementStart < line.end) { + startLine = i; + break; + } + } + if (startLine === -1) { + throw new Error("Replacement range is outside the base content."); + } + + let endLine = startLine; + while (endLine < lines.length && lines[endLine].end < replacementEnd) { + endLine++; + } + if (endLine >= lines.length) { + throw new Error("Replacement range is outside the base content."); + } + + return { startLine, endLine: endLine + 1 }; +} + +function applyReplacements(content: string, replacements: TextReplacement[], offset = 0): string { + let result = content; + for (let i = replacements.length - 1; i >= 0; i--) { + const replacement = replacements[i]; + const matchIndex = replacement.matchIndex - offset; + result = + result.substring(0, matchIndex) + replacement.newText + result.substring(matchIndex + replacement.matchLength); + } + return result; +} + +/** + * Apply replacements matched against `baseContent` to `originalContent` while + * preserving unchanged line blocks from the original. + * + * This is useful when `baseContent` is a normalized view of the original. Each + * replacement is widened to the lines it actually touches, those touched lines + * are rewritten from the normalized base, and all other lines are copied back + * from `originalContent`. The actual replacement ranges drive preservation so + * duplicate normalized lines cannot be aligned to the wrong occurrence. + */ +export function applyReplacementsPreservingUnchangedLines( + originalContent: string, + baseContent: string, + replacements: TextReplacement[], +): string { + const originalLines = splitLinesWithEndings(originalContent); + const baseLines = getLineSpans(baseContent); + if (originalLines.length !== baseLines.length) { + throw new Error("Cannot preserve unchanged lines because the base content has a different line count."); + } + + const groups: Array<{ startLine: number; endLine: number; replacements: TextReplacement[] }> = []; + const sortedReplacements = [...replacements].sort((a, b) => a.matchIndex - b.matchIndex); + for (const replacement of sortedReplacements) { + const range = getReplacementLineRange(baseLines, replacement); + const current = groups[groups.length - 1]; + if (current && range.startLine < current.endLine) { + current.endLine = Math.max(current.endLine, range.endLine); + current.replacements.push(replacement); + continue; + } + groups.push({ ...range, replacements: [replacement] }); + } + + let originalLineIndex = 0; + let result = ""; + for (const group of groups) { + result += originalLines.slice(originalLineIndex, group.startLine).join(""); + + const groupStartOffset = baseLines[group.startLine].start; + const groupEndOffset = baseLines[group.endLine - 1].end; + result += applyReplacements( + baseContent.slice(groupStartOffset, groupEndOffset), + group.replacements, + groupStartOffset, + ); + originalLineIndex = group.endLine; + } + result += originalLines.slice(originalLineIndex).join(""); + + return result; +} + +export interface FuzzyMatchResult { + /** Whether a match was found */ + found: boolean; + /** The index where the match starts (in the content that should be used for replacement) */ + index: number; + /** Length of the matched text */ + matchLength: number; + /** Whether fuzzy matching was used (false = exact match) */ + usedFuzzyMatch: boolean; + /** + * The content to use for replacement operations. + * When exact match: original content. When fuzzy match: normalized content. + */ + contentForReplacement: string; +} + +export interface Edit { + oldText: string; + newText: string; +} + +export interface AppliedEditsResult { + baseContent: string; + newContent: string; +} + +/** + * Find oldText in content, trying exact match first, then fuzzy match. + * When fuzzy matching is used, the returned contentForReplacement is the + * fuzzy-normalized version of the content (trailing whitespace stripped, + * Unicode quotes/dashes normalized to ASCII). + */ +export function fuzzyFindText(content: string, oldText: string): FuzzyMatchResult { + // Try exact match first + const exactIndex = content.indexOf(oldText); + if (exactIndex !== -1) { + return { + found: true, + index: exactIndex, + matchLength: oldText.length, + usedFuzzyMatch: false, + contentForReplacement: content, + }; + } + + // Try fuzzy match - work entirely in normalized space + const fuzzyContent = normalizeForFuzzyMatch(content); + const fuzzyOldText = normalizeForFuzzyMatch(oldText); + const fuzzyIndex = fuzzyContent.indexOf(fuzzyOldText); + + if (fuzzyIndex === -1) { + return { + found: false, + index: -1, + matchLength: 0, + usedFuzzyMatch: false, + contentForReplacement: content, + }; + } + + // When fuzzy matching, return offsets in normalized space. Callers can use + // the normalized content to compute replacements, then decide how much of + // that normalized output should be written back. + return { + found: true, + index: fuzzyIndex, + matchLength: fuzzyOldText.length, + usedFuzzyMatch: true, + contentForReplacement: fuzzyContent, + }; +} + +function countOccurrences(content: string, oldText: string): number { + const fuzzyContent = normalizeForFuzzyMatch(content); + const fuzzyOldText = normalizeForFuzzyMatch(oldText); + return fuzzyContent.split(fuzzyOldText).length - 1; +} + +function getNotFoundError(path: string, editIndex: number, totalEdits: number): Error { + if (totalEdits === 1) { + return new Error( + `Could not find the exact text in ${path}. The old text must match exactly including all whitespace and newlines.`, + ); + } + return new Error( + `Could not find edits[${editIndex}] in ${path}. The oldText must match exactly including all whitespace and newlines.`, + ); +} + +function getDuplicateError(path: string, editIndex: number, totalEdits: number, occurrences: number): Error { + if (totalEdits === 1) { + return new Error( + `Found ${occurrences} occurrences of the text in ${path}. The text must be unique. Please provide more context to make it unique.`, + ); + } + return new Error( + `Found ${occurrences} occurrences of edits[${editIndex}] in ${path}. Each oldText must be unique. Please provide more context to make it unique.`, + ); +} + +function getEmptyOldTextError(path: string, editIndex: number, totalEdits: number): Error { + if (totalEdits === 1) { + return new Error(`oldText must not be empty in ${path}.`); + } + return new Error(`edits[${editIndex}].oldText must not be empty in ${path}.`); +} + +function getNoChangeError(path: string, totalEdits: number): Error { + if (totalEdits === 1) { + return new Error( + `No changes made to ${path}. The replacement produced identical content. This might indicate an issue with special characters or the text not existing as expected.`, + ); + } + return new Error(`No changes made to ${path}. The replacements produced identical content.`); +} + +/** + * Apply one or more exact-text replacements to LF-normalized content. + * + * All edits are matched against the same original content. Replacements are + * then applied in reverse order so offsets remain stable. If any edit needs + * fuzzy matching, the operation runs in fuzzy-normalized content space and then + * overlays those line-level changes onto the original content so unchanged line + * blocks keep their original bytes. + */ +export function applyEditsToNormalizedContent( + normalizedContent: string, + edits: Edit[], + path: string, +): AppliedEditsResult { + const normalizedEdits = edits.map((edit) => ({ + oldText: normalizeToLF(edit.oldText), + newText: normalizeToLF(edit.newText), + })); + + for (let i = 0; i < normalizedEdits.length; i++) { + if (normalizedEdits[i].oldText.length === 0) { + throw getEmptyOldTextError(path, i, normalizedEdits.length); + } + } + + const initialMatches = normalizedEdits.map((edit) => fuzzyFindText(normalizedContent, edit.oldText)); + const usedFuzzyMatch = initialMatches.some((match) => match.usedFuzzyMatch); + const replacementBaseContent = usedFuzzyMatch ? normalizeForFuzzyMatch(normalizedContent) : normalizedContent; + + const matchedEdits: MatchedEdit[] = []; + for (let i = 0; i < normalizedEdits.length; i++) { + const edit = normalizedEdits[i]; + const matchResult = fuzzyFindText(replacementBaseContent, edit.oldText); + if (!matchResult.found) { + throw getNotFoundError(path, i, normalizedEdits.length); + } + + const occurrences = countOccurrences(replacementBaseContent, edit.oldText); + if (occurrences > 1) { + throw getDuplicateError(path, i, normalizedEdits.length, occurrences); + } + + matchedEdits.push({ + editIndex: i, + matchIndex: matchResult.index, + matchLength: matchResult.matchLength, + newText: edit.newText, + }); + } + + matchedEdits.sort((a, b) => a.matchIndex - b.matchIndex); + for (let i = 1; i < matchedEdits.length; i++) { + const previous = matchedEdits[i - 1]; + const current = matchedEdits[i]; + if (previous.matchIndex + previous.matchLength > current.matchIndex) { + throw new Error( + `edits[${previous.editIndex}] and edits[${current.editIndex}] overlap in ${path}. Merge them into one edit or target disjoint regions.`, + ); + } + } + + const baseContent = normalizedContent; + const newContent = usedFuzzyMatch + ? applyReplacementsPreservingUnchangedLines(normalizedContent, replacementBaseContent, matchedEdits) + : applyReplacements(replacementBaseContent, matchedEdits); + + if (baseContent === newContent) { + throw getNoChangeError(path, normalizedEdits.length); + } + + return { baseContent, newContent }; +} + +/** Generate a standard unified patch. */ +export function generateUnifiedPatch(path: string, oldContent: string, newContent: string, contextLines = 4): string { + return Diff.createTwoFilesPatch(path, path, oldContent, newContent, undefined, undefined, { + context: contextLines, + headerOptions: Diff.FILE_HEADERS_ONLY, + }); +} + +/** + * Generate a display-oriented diff string with line numbers and context. + * Returns both the diff string and the first changed line number (in the new file). + */ +export function generateDiffString( + oldContent: string, + newContent: string, + contextLines = 4, +): { diff: string; firstChangedLine: number | undefined } { + const parts = Diff.diffLines(oldContent, newContent); + const output: string[] = []; + + const oldLines = oldContent.split("\n"); + const newLines = newContent.split("\n"); + const maxLineNum = Math.max(oldLines.length, newLines.length); + const lineNumWidth = String(maxLineNum).length; + + let oldLineNum = 1; + let newLineNum = 1; + let lastWasChange = false; + let firstChangedLine: number | undefined; + + for (let i = 0; i < parts.length; i++) { + const part = parts[i]; + const raw = part.value.split("\n"); + if (raw[raw.length - 1] === "") { + raw.pop(); + } + + if (part.added || part.removed) { + // Capture the first changed line (in the new file) + if (firstChangedLine === undefined) { + firstChangedLine = newLineNum; + } + + // Show the change + for (const line of raw) { + if (part.added) { + const lineNum = String(newLineNum).padStart(lineNumWidth, " "); + output.push(`+${lineNum} ${line}`); + newLineNum++; + } else { + // removed + const lineNum = String(oldLineNum).padStart(lineNumWidth, " "); + output.push(`-${lineNum} ${line}`); + oldLineNum++; + } + } + lastWasChange = true; + } else { + // Context lines - only show a few before/after changes + const nextPartIsChange = i < parts.length - 1 && (parts[i + 1].added || parts[i + 1].removed); + const hasLeadingChange = lastWasChange; + const hasTrailingChange = nextPartIsChange; + + if (hasLeadingChange && hasTrailingChange) { + if (raw.length <= contextLines * 2) { + for (const line of raw) { + const lineNum = String(oldLineNum).padStart(lineNumWidth, " "); + output.push(` ${lineNum} ${line}`); + oldLineNum++; + newLineNum++; + } + } else { + const leadingLines = raw.slice(0, contextLines); + const trailingLines = raw.slice(raw.length - contextLines); + const skippedLines = raw.length - leadingLines.length - trailingLines.length; + + for (const line of leadingLines) { + const lineNum = String(oldLineNum).padStart(lineNumWidth, " "); + output.push(` ${lineNum} ${line}`); + oldLineNum++; + newLineNum++; + } + + output.push(` ${"".padStart(lineNumWidth, " ")} ...`); + oldLineNum += skippedLines; + newLineNum += skippedLines; + + for (const line of trailingLines) { + const lineNum = String(oldLineNum).padStart(lineNumWidth, " "); + output.push(` ${lineNum} ${line}`); + oldLineNum++; + newLineNum++; + } + } + } else if (hasLeadingChange) { + const shownLines = raw.slice(0, contextLines); + const skippedLines = raw.length - shownLines.length; + + for (const line of shownLines) { + const lineNum = String(oldLineNum).padStart(lineNumWidth, " "); + output.push(` ${lineNum} ${line}`); + oldLineNum++; + newLineNum++; + } + + if (skippedLines > 0) { + output.push(` ${"".padStart(lineNumWidth, " ")} ...`); + oldLineNum += skippedLines; + newLineNum += skippedLines; + } + } else if (hasTrailingChange) { + const skippedLines = Math.max(0, raw.length - contextLines); + if (skippedLines > 0) { + output.push(` ${"".padStart(lineNumWidth, " ")} ...`); + oldLineNum += skippedLines; + newLineNum += skippedLines; + } + + for (const line of raw.slice(skippedLines)) { + const lineNum = String(oldLineNum).padStart(lineNumWidth, " "); + output.push(` ${lineNum} ${line}`); + oldLineNum++; + newLineNum++; + } + } else { + // Skip these context lines entirely + oldLineNum += raw.length; + newLineNum += raw.length; + } + + lastWasChange = false; + } + } + + return { diff: output.join("\n"), firstChangedLine }; +} + +export interface EditDiffResult { + diff: string; + firstChangedLine: number | undefined; +} + +export interface EditDiffError { + error: string; +} + +/** + * Compute the diff for one or more edit operations without applying them. + * Used for preview rendering in the TUI before the tool executes. + */ +export async function computeEditsDiff( + path: string, + edits: Edit[], + cwd: string, +): Promise { + const absolutePath = resolveToCwd(path, cwd); + + try { + // Check if file exists and is readable + try { + await access(absolutePath, constants.R_OK); + } catch (error: unknown) { + const errorMessage = error instanceof Error && "code" in error ? `Error code: ${error.code}` : String(error); + return { error: `Could not edit file: ${path}. ${errorMessage}.` }; + } + + // Read the file + const rawContent = await readFile(absolutePath, "utf-8"); + + // Strip BOM before matching (LLM won't include invisible BOM in oldText) + const { text: content } = splitBom(rawContent); + const normalizedContent = normalizeToLF(content); + const { baseContent, newContent } = applyEditsToNormalizedContent(normalizedContent, edits, path); + + // Generate the diff + return generateDiffString(baseContent, newContent); + } catch (err) { + return { error: err instanceof Error ? err.message : String(err) }; + } +} + +/** + * Compute the diff for a single edit operation without applying it. + * Kept as a convenience wrapper for single-edit callers. + */ +export async function computeEditDiff( + path: string, + oldText: string, + newText: string, + cwd: string, +): Promise { + return computeEditsDiff(path, [{ oldText, newText }], cwd); +} diff --git a/packages/coding-agent/src/core/tools/edit.ts b/packages/coding-agent/src/core/tools/edit.ts new file mode 100644 index 0000000000000000000000000000000000000000..62e6fdee033df26d8a9454bded9a8a5aca0a3da3 --- /dev/null +++ b/packages/coding-agent/src/core/tools/edit.ts @@ -0,0 +1,220 @@ +import type { AgentTool } from "@earendil-works/pi-agent-core"; +import { constants } from "fs"; +import { access as fsAccess, readFile as fsReadFile, writeFile as fsWriteFile } from "fs/promises"; +import { type Static, Type } from "typebox"; +import { splitBom } from "../../utils/text.ts"; +import type { ExtensionContext, ToolDefinition } from "../extensions/types.ts"; +import { + applyEditsToNormalizedContent, + detectLineEnding, + type Edit, + generateDiffString, + generateUnifiedPatch, + normalizeToLF, + restoreLineEndings, +} from "./edit-diff.ts"; +import { withFileMutationQueue } from "./file-mutation-queue.ts"; +import { resolveToCwd } from "./path-utils.ts"; +import { type EditRenderState, editRenderers } from "./renderers/edit.ts"; +import { wrapToolDefinition } from "./tool-definition-wrapper.ts"; + +const replaceEditSchema = Type.Object( + { + oldText: Type.String({ + description: + "Exact text for one targeted replacement. It must be unique in the original file and must not overlap with any other edits[].oldText in the same call.", + }), + newText: Type.String({ description: "Replacement text for this targeted edit." }), + }, + {}, +); + +const editSchema = Type.Object( + { + path: Type.String({ description: "Path to the file to edit (relative or absolute)" }), + edits: Type.Array(replaceEditSchema, { + description: + "One or more targeted replacements. Each edit is matched against the original file, not incrementally. Do not include overlapping or nested edits. If two changes touch the same block or nearby lines, merge them into one edit instead.", + }), + }, + {}, +); + +export const editToolSystemPromptContribution = { + snippet: "Make precise file edits with exact text replacement, including multiple disjoint edits in one call", + guidelines: [ + "Use edit for precise changes (edits[].oldText must match exactly)", + "When changing multiple separate locations in one file, use one edit call with multiple entries in edits[] instead of multiple edit calls", + "Each edits[].oldText is matched against the original file, not after earlier edits are applied. Do not emit overlapping or nested edits. Merge nearby changes into one edit.", + "Keep edits[].oldText as small as possible while still being unique in the file. Do not pad with large unchanged regions.", + ], +} as const; + +export type EditToolInput = Static; +type LegacyEditToolInput = EditToolInput & { + oldText?: unknown; + newText?: unknown; +}; + +type SingleEditInput = { oldText: string; newText: string }; + +function isSingleEditInput(value: unknown): value is SingleEditInput { + if (!value || typeof value !== "object" || Array.isArray(value)) { + return false; + } + + const edit = value as Record; + return typeof edit.oldText === "string" && typeof edit.newText === "string"; +} + +export interface EditToolDetails { + /** Display-oriented diff of the changes made */ + diff: string; + /** Standard unified patch of the changes made */ + patch: string; + /** Line number of the first change in the new file (for editor navigation) */ + firstChangedLine?: number; +} + +/** + * Pluggable operations for the edit tool. + * Override these to delegate file editing to remote systems (for example SSH). + */ +export interface EditOperations { + /** Read file contents as a Buffer */ + readFile: (absolutePath: string) => Promise; + /** Write content to a file */ + writeFile: (absolutePath: string, content: string) => Promise; + /** Check if file is readable and writable (throw if not) */ + access: (absolutePath: string) => Promise; +} + +const defaultEditOperations: EditOperations = { + readFile: (path) => fsReadFile(path), + writeFile: (path, content) => fsWriteFile(path, content, "utf-8"), + access: (path) => fsAccess(path, constants.R_OK | constants.W_OK), +}; + +export interface EditToolOptions { + /** Custom operations for file editing. Default: local filesystem */ + operations?: EditOperations; +} + +function prepareEditArguments(input: unknown): EditToolInput { + if (!input || typeof input !== "object") { + return input as EditToolInput; + } + + const args = input as Record; + + // Some models (Opus 4.6, GLM-5.1) send edits as a JSON string instead of an array. + // Others send a single edit object instead of a one-element edits array. + if (typeof args.edits === "string") { + try { + const parsed = JSON.parse(args.edits); + if (Array.isArray(parsed)) { + args.edits = parsed; + } else if (isSingleEditInput(parsed)) { + args.edits = [parsed]; + } + } catch {} + } else if (isSingleEditInput(args.edits)) { + args.edits = [args.edits]; + } + + const legacy = args as LegacyEditToolInput; + if (typeof legacy.oldText !== "string" || typeof legacy.newText !== "string") { + return args as EditToolInput; + } + + const edits = Array.isArray(legacy.edits) ? [...legacy.edits] : []; + edits.push({ oldText: legacy.oldText, newText: legacy.newText }); + const { oldText: _oldText, newText: _newText, ...rest } = legacy; + return { ...rest, edits } as EditToolInput; +} + +function validateEditInput(input: EditToolInput): { path: string; edits: Edit[] } { + if (!Array.isArray(input.edits) || input.edits.length === 0) { + throw new Error("Edit tool input is invalid. edits must contain at least one replacement."); + } + return { path: input.path, edits: input.edits }; +} + +export function createEditToolDefinition( + cwd: string, + options?: EditToolOptions, +): ToolDefinition { + const ops = options?.operations ?? defaultEditOperations; + return { + name: "edit", + label: "edit", + description: + "Edit a single file using exact text replacement. Every edits[].oldText must match a unique, non-overlapping region of the original file. If two changes affect the same block or nearby lines, merge them into one edit instead of emitting overlapping edits. Do not include large unchanged regions just to connect distant changes.", + promptSnippet: editToolSystemPromptContribution.snippet, + promptGuidelines: [...editToolSystemPromptContribution.guidelines], + parameters: editSchema, + constrainedSampling: { type: "json_schema", strict: "prefer" }, + renderShell: "self", + prepareArguments: prepareEditArguments, + async execute(_toolCallId, input: EditToolInput, signal?: AbortSignal, _onUpdate?, ctx?: ExtensionContext) { + const { path, edits } = validateEditInput(input); + const absolutePath = resolveToCwd(path, ctx?.cwd || cwd); + + return withFileMutationQueue(absolutePath, async () => { + // Do not reject from an abort event listener here: that would release the + // mutation queue while an in-flight filesystem operation may still finish. + // Checking signal.aborted after each await observes the same aborts while + // keeping the queue locked until the current operation has settled. + const throwIfAborted = (): void => { + if (signal?.aborted) throw new Error("Operation aborted"); + }; + + throwIfAborted(); + + // Check if file exists. + try { + await ops.access(absolutePath); + } catch (error: unknown) { + throwIfAborted(); + const errorMessage = + error instanceof Error && "code" in error ? `Error code: ${error.code}` : String(error); + throw new Error(`Could not edit file: ${path}. ${errorMessage}.`); + } + throwIfAborted(); + + // Read the file. + const buffer = await ops.readFile(absolutePath); + const rawContent = buffer.toString("utf-8"); + throwIfAborted(); + + // Strip BOM before matching. The model will not include an invisible BOM in oldText. + const { bom, text: content } = splitBom(rawContent); + const originalEnding = detectLineEnding(content); + const normalizedContent = normalizeToLF(content); + const { baseContent, newContent } = applyEditsToNormalizedContent(normalizedContent, edits, path); + throwIfAborted(); + + const finalContent = bom + restoreLineEndings(newContent, originalEnding); + await ops.writeFile(absolutePath, finalContent); + throwIfAborted(); + + const diffResult = generateDiffString(baseContent, newContent); + const patch = generateUnifiedPatch(path, baseContent, newContent); + return { + content: [ + { + type: "text", + text: `Successfully replaced ${edits.length} block(s) in ${path}.`, + }, + ], + details: { diff: diffResult.diff, patch, firstChangedLine: diffResult.firstChangedLine }, + }; + }); + }, + ...editRenderers, + }; +} + +export function createEditTool(cwd: string, options?: EditToolOptions): AgentTool { + return wrapToolDefinition(createEditToolDefinition(cwd, options)); +} diff --git a/packages/coding-agent/src/core/tools/file-mutation-queue.ts b/packages/coding-agent/src/core/tools/file-mutation-queue.ts new file mode 100644 index 0000000000000000000000000000000000000000..5505a7a27c12745de49e0ad8a0ef1259d47b676c --- /dev/null +++ b/packages/coding-agent/src/core/tools/file-mutation-queue.ts @@ -0,0 +1,61 @@ +import { realpath } from "node:fs/promises"; +import { resolve } from "node:path"; + +const fileMutationQueues = new Map>(); +let registrationQueue = Promise.resolve(); + +function isMissingPathError(error: unknown): boolean { + return ( + typeof error === "object" && + error !== null && + "code" in error && + (error.code === "ENOENT" || error.code === "ENOTDIR") + ); +} + +async function getMutationQueueKey(filePath: string): Promise { + const resolvedPath = resolve(filePath); + try { + return await realpath(resolvedPath); + } catch (error) { + if (isMissingPathError(error)) { + return resolvedPath; + } + throw error; + } +} + +/** + * Serialize file mutation operations targeting the same file. + * Operations for different files still run in parallel. + */ +export async function withFileMutationQueue(filePath: string, fn: () => Promise): Promise { + const registration = registrationQueue.then(async () => { + const key = await getMutationQueueKey(filePath); + const currentQueue = fileMutationQueues.get(key) ?? Promise.resolve(); + + let releaseNext!: () => void; + const nextQueue = new Promise((resolveQueue) => { + releaseNext = resolveQueue; + }); + const chainedQueue = currentQueue.then(() => nextQueue); + fileMutationQueues.set(key, chainedQueue); + + return { key, currentQueue, chainedQueue, releaseNext }; + }); + registrationQueue = registration.then( + () => undefined, + () => undefined, + ); + + const { key, currentQueue, chainedQueue, releaseNext } = await registration; + await currentQueue; + try { + return await fn(); + } finally { + releaseNext(); + if (fileMutationQueues.get(key) === chainedQueue) { + fileMutationQueues.delete(key); + } + } +} diff --git a/packages/coding-agent/src/core/tools/find.ts b/packages/coding-agent/src/core/tools/find.ts new file mode 100644 index 0000000000000000000000000000000000000000..8d44c491ee8c4903a6a92005587fdee0407d3d90 --- /dev/null +++ b/packages/coding-agent/src/core/tools/find.ts @@ -0,0 +1,318 @@ +import { createInterface } from "node:readline"; +import type { AgentTool } from "@earendil-works/pi-agent-core"; +import { spawn } from "child_process"; +import path from "path"; +import { type Static, Type } from "typebox"; +import { ensureTool } from "../../utils/tools-manager.ts"; +import type { ExtensionContext, ToolDefinition } from "../extensions/types.ts"; +import { pathExists, resolveToCwd } from "./path-utils.ts"; +import { findRenderers } from "./renderers/find.ts"; +import { wrapToolDefinition } from "./tool-definition-wrapper.ts"; +import { DEFAULT_MAX_BYTES, formatSize, type TruncationResult, truncateHead } from "./truncate.ts"; + +/** Relativize a find result against the search root and normalize it to posix separators. */ +export function relativizeFindResultPath( + resultPath: string, + searchPath: string, + pathModule: path.PlatformPath = path, +): string { + const hadTrailingSeparator = + resultPath.endsWith(pathModule.sep) || (pathModule.sep === "\\" && resultPath.endsWith("/")); + const relativePath = pathModule.isAbsolute(resultPath) ? pathModule.relative(searchPath, resultPath) : resultPath; + const posixPath = relativePath.split(pathModule.sep).join("/"); + return hadTrailingSeparator && !posixPath.endsWith("/") ? `${posixPath}/` : posixPath; +} + +const findSchema = Type.Object({ + pattern: Type.String({ + description: "Glob pattern to match files, e.g. '*.ts', '**/*.json', or 'src/**/*.spec.ts'", + }), + path: Type.Optional(Type.String({ description: "Directory to search in (default: current directory)" })), + limit: Type.Optional(Type.Number({ description: "Maximum number of results (default: 1000)" })), +}); + +export const findToolSystemPromptContribution = { + snippet: "Find files by glob pattern (respects .gitignore)", + guidelines: [], +} as const; + +export type FindToolInput = Static; + +const DEFAULT_LIMIT = 1000; + +export interface FindToolDetails { + truncation?: TruncationResult; + resultLimitReached?: number; +} + +/** + * Pluggable operations for the find tool. + * Override these to delegate file search to remote systems (for example SSH). + */ +export interface FindOperations { + /** Check if path exists */ + exists: (absolutePath: string) => Promise | boolean; + /** Find files matching glob pattern. Returns relative or absolute paths. */ + glob: (pattern: string, cwd: string, options: { ignore: string[]; limit: number }) => Promise | string[]; +} + +const defaultFindOperations: FindOperations = { + exists: pathExists, + // This is a placeholder. Actual fd execution happens in execute() when no custom glob is provided. + glob: () => [], +}; + +export interface FindToolOptions { + /** Custom operations for find. Default: local filesystem plus fd */ + operations?: FindOperations; +} + +export function createFindToolDefinition( + cwd: string, + options?: FindToolOptions, +): ToolDefinition { + const customOps = options?.operations; + return { + name: "find", + label: "find", + description: `Search for files by glob pattern. Returns matching file paths relative to the search directory. Respects .gitignore. Output is truncated to ${DEFAULT_LIMIT} results or ${DEFAULT_MAX_BYTES / 1024}KB (whichever is hit first).`, + promptSnippet: findToolSystemPromptContribution.snippet, + parameters: findSchema, + async execute( + _toolCallId, + { pattern, path: searchDir, limit }: { pattern: string; path?: string; limit?: number }, + signal?: AbortSignal, + _onUpdate?, + ctx?: ExtensionContext, + ) { + return new Promise((resolve, reject) => { + if (signal?.aborted) { + reject(new Error("Operation aborted")); + return; + } + + let settled = false; + let stopChild: (() => void) | undefined; + const settle = (fn: () => void) => { + if (settled) return; + settled = true; + signal?.removeEventListener("abort", onAbort); + stopChild = undefined; + fn(); + }; + const onAbort = () => { + stopChild?.(); + settle(() => reject(new Error("Operation aborted"))); + }; + signal?.addEventListener("abort", onAbort, { once: true }); + + (async () => { + try { + const searchPath = resolveToCwd(searchDir || ".", ctx?.cwd || cwd); + const effectiveLimit = limit ?? DEFAULT_LIMIT; + const ops = customOps ?? defaultFindOperations; + + // If custom operations provide glob(), use that instead of fd. + if (customOps?.glob) { + if (!(await ops.exists(searchPath))) { + settle(() => reject(new Error(`Path not found: ${searchPath}`))); + return; + } + if (signal?.aborted) { + settle(() => reject(new Error("Operation aborted"))); + return; + } + const results = await ops.glob(pattern, searchPath, { + ignore: ["**/node_modules/**", "**/.git/**"], + limit: effectiveLimit, + }); + if (signal?.aborted) { + settle(() => reject(new Error("Operation aborted"))); + return; + } + if (results.length === 0) { + settle(() => + resolve({ + content: [{ type: "text", text: "No files found matching pattern" }], + details: undefined, + }), + ); + return; + } + + // Relativize paths against the search root for stable output. + const relativized = results.map((p) => relativizeFindResultPath(p, searchPath)); + const resultLimitReached = relativized.length >= effectiveLimit; + const rawOutput = relativized.join("\n"); + const truncation = truncateHead(rawOutput, { maxLines: Number.MAX_SAFE_INTEGER }); + let resultOutput = truncation.content; + const details: FindToolDetails = {}; + const notices: string[] = []; + if (resultLimitReached) { + notices.push(`${effectiveLimit} results limit reached`); + details.resultLimitReached = effectiveLimit; + } + if (truncation.truncated) { + notices.push(`${formatSize(DEFAULT_MAX_BYTES)} limit reached`); + details.truncation = truncation; + } + if (notices.length > 0) { + resultOutput += `\n\n[${notices.join(". ")}]`; + } + settle(() => + resolve({ + content: [{ type: "text", text: resultOutput }], + details: Object.keys(details).length > 0 ? details : undefined, + }), + ); + return; + } + + // Default implementation uses fd. + const fdPath = await ensureTool("fd"); + if (signal?.aborted) { + settle(() => reject(new Error("Operation aborted"))); + return; + } + if (!fdPath) { + settle(() => reject(new Error("fd is not available and could not be downloaded"))); + return; + } + + const args: string[] = ["--glob", "--color=never", "--hidden"]; + + // fd normally ignores .gitignore outside git repos, so keep --no-require-git + // there. Inside repos, use fd's default git-aware behavior so parent + // .gitignore rules stop at nested repo boundaries: + // https://github.com/earendil-works/pi/issues/5960 + let insideGitRepo = false; + for (let current = searchPath; ; ) { + if (await pathExists(path.join(current, ".git"))) { + insideGitRepo = true; + break; + } + const parent = path.dirname(current); + if (parent === current) break; + current = parent; + } + if (!insideGitRepo) args.push("--no-require-git"); + args.push("--max-results", String(effectiveLimit)); + + // fd --glob matches against the basename unless --full-path is set; in --full-path + // mode it matches against the absolute candidate path, so a path-containing + // pattern like 'src/**/*.spec.ts' needs a leading '**/' to match anything. + let effectivePattern = pattern; + if (pattern.includes("/")) { + args.push("--full-path"); + if (!pattern.startsWith("/") && !pattern.startsWith("**/") && pattern !== "**") { + effectivePattern = `**/${pattern}`; + } + // fd matches full paths using native separators on Windows. + if (process.platform === "win32") + effectivePattern = effectivePattern.replaceAll("/", String.raw`[/\\]`); + } + args.push("--", effectivePattern, searchPath); + + const child = spawn(fdPath, args, { stdio: ["ignore", "pipe", "pipe"] }); + const rl = createInterface({ input: child.stdout }); + let stderr = ""; + const lines: string[] = []; + + stopChild = () => { + if (!child.killed) { + child.kill(); + } + }; + + const cleanup = () => { + rl.close(); + }; + + child.stderr?.on("data", (chunk) => { + stderr += chunk.toString(); + }); + + rl.on("line", (line) => { + lines.push(line); + }); + + child.on("error", (error) => { + cleanup(); + settle(() => reject(new Error(`Failed to run fd: ${error.message}`))); + }); + + child.on("close", (code) => { + cleanup(); + if (signal?.aborted) { + settle(() => reject(new Error("Operation aborted"))); + return; + } + const output = lines.join("\n"); + if (code !== 0) { + const errorMsg = stderr.trim() || `fd exited with code ${code}`; + if (!output) { + settle(() => reject(new Error(errorMsg))); + return; + } + } + if (!output) { + settle(() => + resolve({ + content: [{ type: "text", text: "No files found matching pattern" }], + details: undefined, + }), + ); + return; + } + + const relativized: string[] = []; + for (const rawLine of lines) { + const line = rawLine.replace(/\r$/, "").trim(); + if (!line) continue; + relativized.push(relativizeFindResultPath(line, searchPath)); + } + + const resultLimitReached = relativized.length >= effectiveLimit; + const rawOutput = relativized.join("\n"); + const truncation = truncateHead(rawOutput, { maxLines: Number.MAX_SAFE_INTEGER }); + let resultOutput = truncation.content; + const details: FindToolDetails = {}; + const notices: string[] = []; + if (resultLimitReached) { + notices.push( + `${effectiveLimit} results limit reached. Use limit=${effectiveLimit * 2} for more, or refine pattern`, + ); + details.resultLimitReached = effectiveLimit; + } + if (truncation.truncated) { + notices.push(`${formatSize(DEFAULT_MAX_BYTES)} limit reached`); + details.truncation = truncation; + } + if (notices.length > 0) { + resultOutput += `\n\n[${notices.join(". ")}]`; + } + settle(() => + resolve({ + content: [{ type: "text", text: resultOutput }], + details: Object.keys(details).length > 0 ? details : undefined, + }), + ); + }); + } catch (e) { + if (signal?.aborted) { + settle(() => reject(new Error("Operation aborted"))); + return; + } + const error = e instanceof Error ? e : new Error(String(e)); + settle(() => reject(error)); + } + })(); + }); + }, + ...findRenderers, + }; +} + +export function createFindTool(cwd: string, options?: FindToolOptions): AgentTool { + return wrapToolDefinition(createFindToolDefinition(cwd, options)); +} diff --git a/packages/coding-agent/src/core/tools/grep.ts b/packages/coding-agent/src/core/tools/grep.ts new file mode 100644 index 0000000000000000000000000000000000000000..44f7e212be79a390d49b1d5c50d37fa86a012e81 --- /dev/null +++ b/packages/coding-agent/src/core/tools/grep.ts @@ -0,0 +1,323 @@ +import { readFile as fsReadFile, stat as fsStat } from "node:fs/promises"; +import { createInterface } from "node:readline"; +import type { AgentTool } from "@earendil-works/pi-agent-core"; +import { spawn } from "child_process"; +import path from "path"; +import { type Static, Type } from "typebox"; +import { ensureTool } from "../../utils/tools-manager.ts"; +import type { ExtensionContext, ToolDefinition } from "../extensions/types.ts"; +import { resolveToCwd } from "./path-utils.ts"; +import { grepRenderers } from "./renderers/grep.ts"; +import { wrapToolDefinition } from "./tool-definition-wrapper.ts"; +import { + DEFAULT_MAX_BYTES, + formatSize, + GREP_MAX_LINE_LENGTH, + type TruncationResult, + truncateHead, + truncateLine, +} from "./truncate.ts"; + +const grepSchema = Type.Object({ + pattern: Type.String({ description: "Search pattern (regex or literal string)" }), + path: Type.Optional(Type.String({ description: "Directory or file to search (default: current directory)" })), + glob: Type.Optional(Type.String({ description: "Filter files by glob pattern, e.g. '*.ts' or '**/*.spec.ts'" })), + ignoreCase: Type.Optional(Type.Boolean({ description: "Case-insensitive search (default: false)" })), + literal: Type.Optional( + Type.Boolean({ description: "Treat pattern as literal string instead of regex (default: false)" }), + ), + context: Type.Optional( + Type.Number({ description: "Number of lines to show before and after each match (default: 0)" }), + ), + limit: Type.Optional(Type.Number({ description: "Maximum number of matches to return (default: 100)" })), +}); + +export const grepToolSystemPromptContribution = { + snippet: "Search file contents for patterns (respects .gitignore)", + guidelines: [], +} as const; + +export type GrepToolInput = Static; +const DEFAULT_LIMIT = 100; + +export interface GrepToolDetails { + truncation?: TruncationResult; + matchLimitReached?: number; + linesTruncated?: boolean; +} + +/** + * Pluggable operations for the grep tool. + * Override these to delegate search to remote systems (for example SSH). + */ +export interface GrepOperations { + /** Check if path is a directory. Throws if path does not exist. */ + isDirectory: (absolutePath: string) => Promise | boolean; + /** Read file contents for context lines */ + readFile: (absolutePath: string) => Promise | string; +} + +const defaultGrepOperations: GrepOperations = { + isDirectory: async (p) => (await fsStat(p)).isDirectory(), + readFile: (p) => fsReadFile(p, "utf-8"), +}; + +export interface GrepToolOptions { + /** Custom operations for grep. Default: local filesystem plus ripgrep */ + operations?: GrepOperations; +} + +export function createGrepToolDefinition( + cwd: string, + options?: GrepToolOptions, +): ToolDefinition { + const customOps = options?.operations; + return { + name: "grep", + label: "grep", + description: `Search file contents for a pattern. Returns matching lines with file paths and line numbers. Respects .gitignore. Output is truncated to ${DEFAULT_LIMIT} matches or ${DEFAULT_MAX_BYTES / 1024}KB (whichever is hit first). Long lines are truncated to ${GREP_MAX_LINE_LENGTH} chars.`, + promptSnippet: grepToolSystemPromptContribution.snippet, + parameters: grepSchema, + async execute( + _toolCallId, + { + pattern, + path: searchDir, + glob, + ignoreCase, + literal, + context, + limit, + }: { + pattern: string; + path?: string; + glob?: string; + ignoreCase?: boolean; + literal?: boolean; + context?: number; + limit?: number; + }, + signal?: AbortSignal, + _onUpdate?, + ctx?: ExtensionContext, + ) { + return new Promise((resolve, reject) => { + if (signal?.aborted) { + reject(new Error("Operation aborted")); + return; + } + let settled = false; + const settle = (fn: () => void) => { + if (!settled) { + settled = true; + fn(); + } + }; + + (async () => { + try { + const rgPath = await ensureTool("rg"); + if (!rgPath) { + settle(() => reject(new Error("ripgrep (rg) is not available and could not be downloaded"))); + return; + } + + const searchPath = resolveToCwd(searchDir || ".", ctx?.cwd || cwd); + const ops = customOps ?? defaultGrepOperations; + let isDirectory: boolean; + try { + isDirectory = await ops.isDirectory(searchPath); + } catch { + settle(() => reject(new Error(`Path not found: ${searchPath}`))); + return; + } + + const contextValue = context && context > 0 ? context : 0; + const effectiveLimit = Math.max(1, limit ?? DEFAULT_LIMIT); + const formatPath = (filePath: string): string => { + if (isDirectory) { + const relative = path.relative(searchPath, filePath); + if (relative && !relative.startsWith("..")) { + return relative.replace(/\\/g, "/"); + } + } + return path.basename(filePath); + }; + + const fileCache = new Map(); + const getFileLines = async (filePath: string): Promise => { + let lines = fileCache.get(filePath); + if (!lines) { + try { + const content = await ops.readFile(filePath); + lines = content.replace(/\r\n/g, "\n").replace(/\r/g, "\n").split("\n"); + } catch { + lines = []; + } + fileCache.set(filePath, lines); + } + return lines; + }; + + const args: string[] = ["--json", "--line-number", "--color=never", "--hidden"]; + if (ignoreCase) args.push("--ignore-case"); + if (literal) args.push("--fixed-strings"); + if (glob) args.push("--glob", glob); + args.push("--", pattern, searchPath); + + const child = spawn(rgPath, args, { stdio: ["ignore", "pipe", "pipe"] }); + const rl = createInterface({ input: child.stdout }); + let stderr = ""; + let matchCount = 0; + let matchLimitReached = false; + let linesTruncated = false; + let aborted = false; + let killedDueToLimit = false; + const outputLines: string[] = []; + + const cleanup = () => { + rl.close(); + signal?.removeEventListener("abort", onAbort); + }; + const stopChild = (dueToLimit = false) => { + if (!child.killed) { + killedDueToLimit = dueToLimit; + child.kill(); + } + }; + const onAbort = () => { + aborted = true; + stopChild(); + }; + signal?.addEventListener("abort", onAbort, { once: true }); + child.stderr?.on("data", (chunk) => { + stderr += chunk.toString(); + }); + + const formatBlock = async (filePath: string, lineNumber: number): Promise => { + const relativePath = formatPath(filePath); + const lines = await getFileLines(filePath); + if (!lines.length) return [`${relativePath}:${lineNumber}: (unable to read file)`]; + const block: string[] = []; + const start = contextValue > 0 ? Math.max(1, lineNumber - contextValue) : lineNumber; + const end = contextValue > 0 ? Math.min(lines.length, lineNumber + contextValue) : lineNumber; + for (let current = start; current <= end; current++) { + const lineText = lines[current - 1] ?? ""; + const sanitized = lineText.replace(/\r/g, ""); + const isMatchLine = current === lineNumber; + // Truncate long lines so grep output stays compact. + const { text: truncatedText, wasTruncated } = truncateLine(sanitized); + if (wasTruncated) linesTruncated = true; + if (isMatchLine) block.push(`${relativePath}:${current}: ${truncatedText}`); + else block.push(`${relativePath}-${current}- ${truncatedText}`); + } + return block; + }; + + // Collect matches during streaming, then format them after rg exits. + const matches: Array<{ filePath: string; lineNumber: number; lineText?: string }> = []; + rl.on("line", (line) => { + if (!line.trim() || matchCount >= effectiveLimit) return; + let event: any; + try { + event = JSON.parse(line); + } catch { + return; + } + if (event.type === "match") { + matchCount++; + const filePath = event.data?.path?.text; + const lineNumber = event.data?.line_number; + const lineText = event.data?.lines?.text; + if (filePath && typeof lineNumber === "number") + matches.push({ filePath, lineNumber, lineText }); + if (matchCount >= effectiveLimit) { + matchLimitReached = true; + stopChild(true); + } + } + }); + + child.on("error", (error) => { + cleanup(); + settle(() => reject(new Error(`Failed to run ripgrep: ${error.message}`))); + }); + child.on("close", async (code) => { + cleanup(); + if (aborted) { + settle(() => reject(new Error("Operation aborted"))); + return; + } + if (!killedDueToLimit && code !== 0 && code !== 1) { + const errorMsg = stderr.trim() || `ripgrep exited with code ${code}`; + settle(() => reject(new Error(errorMsg))); + return; + } + if (matchCount === 0) { + settle(() => + resolve({ content: [{ type: "text", text: "No matches found" }], details: undefined }), + ); + return; + } + + // Format matches after streaming finishes so custom readFile() backends can be async. + for (const match of matches) { + if (contextValue === 0 && match.lineText !== undefined) { + const relativePath = formatPath(match.filePath); + const sanitized = match.lineText + .replace(/\r\n/g, "\n") + .replace(/\r/g, "") + .replace(/\n$/, ""); + const { text: truncatedText, wasTruncated } = truncateLine(sanitized); + if (wasTruncated) linesTruncated = true; + outputLines.push(`${relativePath}:${match.lineNumber}: ${truncatedText}`); + } else { + const block = await formatBlock(match.filePath, match.lineNumber); + outputLines.push(...block); + } + } + + const rawOutput = outputLines.join("\n"); + // Apply byte truncation. There is no line limit here because the match limit already capped rows. + const truncation = truncateHead(rawOutput, { maxLines: Number.MAX_SAFE_INTEGER }); + let output = truncation.content; + const details: GrepToolDetails = {}; + // Build actionable notices for truncation and match limits. + const notices: string[] = []; + if (matchLimitReached) { + notices.push( + `${effectiveLimit} matches limit reached. Use limit=${effectiveLimit * 2} for more, or refine pattern`, + ); + details.matchLimitReached = effectiveLimit; + } + if (truncation.truncated) { + notices.push(`${formatSize(DEFAULT_MAX_BYTES)} limit reached`); + details.truncation = truncation; + } + if (linesTruncated) { + notices.push( + `Some lines truncated to ${GREP_MAX_LINE_LENGTH} chars. Use read tool to see full lines`, + ); + details.linesTruncated = true; + } + if (notices.length > 0) output += `\n\n[${notices.join(". ")}]`; + settle(() => + resolve({ + content: [{ type: "text", text: output }], + details: Object.keys(details).length > 0 ? details : undefined, + }), + ); + }); + } catch (err) { + settle(() => reject(err as Error)); + } + })(); + }); + }, + ...grepRenderers, + }; +} + +export function createGrepTool(cwd: string, options?: GrepToolOptions): AgentTool { + return wrapToolDefinition(createGrepToolDefinition(cwd, options)); +} diff --git a/packages/coding-agent/src/core/tools/index.ts b/packages/coding-agent/src/core/tools/index.ts new file mode 100644 index 0000000000000000000000000000000000000000..5e8fd5b36912371c1e54878356bf40356d619aeb --- /dev/null +++ b/packages/coding-agent/src/core/tools/index.ts @@ -0,0 +1,224 @@ +export { + type BashOperations, + type BashSpawnContext, + type BashSpawnHook, + type BashToolDetails, + type BashToolInput, + type BashToolOptions, + createBashTool, + createBashToolDefinition, + createLocalBashOperations, +} from "./bash.ts"; +export { + createEditTool, + createEditToolDefinition, + type EditOperations, + type EditToolDetails, + type EditToolInput, + type EditToolOptions, +} from "./edit.ts"; +export { withFileMutationQueue } from "./file-mutation-queue.ts"; +export { + createFindTool, + createFindToolDefinition, + type FindOperations, + type FindToolDetails, + type FindToolInput, + type FindToolOptions, +} from "./find.ts"; +export { + createGrepTool, + createGrepToolDefinition, + type GrepOperations, + type GrepToolDetails, + type GrepToolInput, + type GrepToolOptions, +} from "./grep.ts"; +export { + createLsTool, + createLsToolDefinition, + type LsOperations, + type LsToolDetails, + type LsToolInput, + type LsToolOptions, +} from "./ls.ts"; +export { + createLocalPowerShellOperations, + createPowerShellTool, + createPowerShellToolDefinition, + type PowerShellOperations, + type PowerShellSpawnContext, + type PowerShellSpawnHook, + type PowerShellToolDetails, + type PowerShellToolInput, + type PowerShellToolOptions, +} from "./powershell.ts"; +export { + createReadTool, + createReadToolDefinition, + type ReadOperations, + type ReadToolDetails, + type ReadToolInput, + type ReadToolOptions, +} from "./read.ts"; +export { + DEFAULT_MAX_BYTES, + DEFAULT_MAX_LINES, + formatSize, + type TruncationOptions, + type TruncationResult, + truncateHead, + truncateLine, + truncateTail, +} from "./truncate.ts"; +export { + createWriteTool, + createWriteToolDefinition, + type WriteOperations, + type WriteToolInput, + type WriteToolOptions, +} from "./write.ts"; + +import type { AgentTool } from "@earendil-works/pi-agent-core"; +import type { ToolDefinition } from "../extensions/types.ts"; +import { type BashToolOptions, createBashTool, createBashToolDefinition } from "./bash.ts"; +import { createEditTool, createEditToolDefinition, type EditToolOptions } from "./edit.ts"; +import { createFindTool, createFindToolDefinition, type FindToolOptions } from "./find.ts"; +import { createGrepTool, createGrepToolDefinition, type GrepToolOptions } from "./grep.ts"; +import { createLsTool, createLsToolDefinition, type LsToolOptions } from "./ls.ts"; +import { createPowerShellTool, createPowerShellToolDefinition, type PowerShellToolOptions } from "./powershell.ts"; +import { createReadTool, createReadToolDefinition, type ReadToolOptions } from "./read.ts"; +import { createWriteTool, createWriteToolDefinition, type WriteToolOptions } from "./write.ts"; + +export type Tool = AgentTool; +export type ToolDef = ToolDefinition; +export type ToolName = "read" | "bash" | "powershell" | "edit" | "write" | "grep" | "find" | "ls"; +export const allToolNames: Set = new Set([ + "read", + "bash", + "powershell", + "edit", + "write", + "grep", + "find", + "ls", +]); + +export interface ToolsOptions { + read?: ReadToolOptions; + bash?: BashToolOptions; + powershell?: PowerShellToolOptions; + write?: WriteToolOptions; + edit?: EditToolOptions; + grep?: GrepToolOptions; + find?: FindToolOptions; + ls?: LsToolOptions; +} + +export function createToolDefinition(toolName: ToolName, cwd: string, options?: ToolsOptions): ToolDef { + switch (toolName) { + case "read": + return createReadToolDefinition(cwd, options?.read); + case "bash": + return createBashToolDefinition(cwd, options?.bash); + case "powershell": + return createPowerShellToolDefinition(cwd, options?.powershell); + case "edit": + return createEditToolDefinition(cwd, options?.edit); + case "write": + return createWriteToolDefinition(cwd, options?.write); + case "grep": + return createGrepToolDefinition(cwd, options?.grep); + case "find": + return createFindToolDefinition(cwd, options?.find); + case "ls": + return createLsToolDefinition(cwd, options?.ls); + default: + throw new Error(`Unknown tool name: ${toolName}`); + } +} + +export function createTool(toolName: ToolName, cwd: string, options?: ToolsOptions): Tool { + switch (toolName) { + case "read": + return createReadTool(cwd, options?.read); + case "bash": + return createBashTool(cwd, options?.bash); + case "powershell": + return createPowerShellTool(cwd, options?.powershell); + case "edit": + return createEditTool(cwd, options?.edit); + case "write": + return createWriteTool(cwd, options?.write); + case "grep": + return createGrepTool(cwd, options?.grep); + case "find": + return createFindTool(cwd, options?.find); + case "ls": + return createLsTool(cwd, options?.ls); + default: + throw new Error(`Unknown tool name: ${toolName}`); + } +} + +export function createCodingToolDefinitions(cwd: string, options?: ToolsOptions): ToolDef[] { + return [ + createReadToolDefinition(cwd, options?.read), + createBashToolDefinition(cwd, options?.bash), + createEditToolDefinition(cwd, options?.edit), + createWriteToolDefinition(cwd, options?.write), + ]; +} + +export function createReadOnlyToolDefinitions(cwd: string, options?: ToolsOptions): ToolDef[] { + return [ + createReadToolDefinition(cwd, options?.read), + createGrepToolDefinition(cwd, options?.grep), + createFindToolDefinition(cwd, options?.find), + createLsToolDefinition(cwd, options?.ls), + ]; +} + +export function createAllToolDefinitions(cwd: string, options?: ToolsOptions): Record { + return { + read: createReadToolDefinition(cwd, options?.read), + bash: createBashToolDefinition(cwd, options?.bash), + powershell: createPowerShellToolDefinition(cwd, options?.powershell), + edit: createEditToolDefinition(cwd, options?.edit), + write: createWriteToolDefinition(cwd, options?.write), + grep: createGrepToolDefinition(cwd, options?.grep), + find: createFindToolDefinition(cwd, options?.find), + ls: createLsToolDefinition(cwd, options?.ls), + }; +} + +export function createCodingTools(cwd: string, options?: ToolsOptions): Tool[] { + return [ + createReadTool(cwd, options?.read), + createBashTool(cwd, options?.bash), + createEditTool(cwd, options?.edit), + createWriteTool(cwd, options?.write), + ]; +} + +export function createReadOnlyTools(cwd: string, options?: ToolsOptions): Tool[] { + return [ + createReadTool(cwd, options?.read), + createGrepTool(cwd, options?.grep), + createFindTool(cwd, options?.find), + createLsTool(cwd, options?.ls), + ]; +} + +export function createAllTools(cwd: string, options?: ToolsOptions): Record { + return { + read: createReadTool(cwd, options?.read), + bash: createBashTool(cwd, options?.bash), + powershell: createPowerShellTool(cwd, options?.powershell), + edit: createEditTool(cwd, options?.edit), + write: createWriteTool(cwd, options?.write), + grep: createGrepTool(cwd, options?.grep), + find: createFindTool(cwd, options?.find), + ls: createLsTool(cwd, options?.ls), + }; +} diff --git a/packages/coding-agent/src/core/tools/ls.ts b/packages/coding-agent/src/core/tools/ls.ts new file mode 100644 index 0000000000000000000000000000000000000000..558ff9f9f99c64a68ceb4be13baa0cdda58a3ab2 --- /dev/null +++ b/packages/coding-agent/src/core/tools/ls.ts @@ -0,0 +1,175 @@ +import { readdir as fsReaddir, stat as fsStat } from "node:fs/promises"; +import type { AgentTool } from "@earendil-works/pi-agent-core"; +import nodePath from "path"; +import { type Static, Type } from "typebox"; +import type { ExtensionContext, ToolDefinition } from "../extensions/types.ts"; +import { pathExists, resolveToCwd } from "./path-utils.ts"; +import { lsRenderers } from "./renderers/ls.ts"; +import { wrapToolDefinition } from "./tool-definition-wrapper.ts"; +import { DEFAULT_MAX_BYTES, formatSize, type TruncationResult, truncateHead } from "./truncate.ts"; + +const lsSchema = Type.Object({ + path: Type.Optional(Type.String({ description: "Directory to list (default: current directory)" })), + limit: Type.Optional(Type.Number({ description: "Maximum number of entries to return (default: 500)" })), +}); + +export const lsToolSystemPromptContribution = { + snippet: "List directory contents", + guidelines: [], +} as const; + +export type LsToolInput = Static; + +const DEFAULT_LIMIT = 500; + +export interface LsToolDetails { + truncation?: TruncationResult; + entryLimitReached?: number; +} + +/** + * Pluggable operations for the ls tool. + * Override these to delegate directory listing to remote systems (for example SSH). + */ +export interface LsOperations { + /** Check if path exists */ + exists: (absolutePath: string) => Promise | boolean; + /** Get file or directory stats. Throws if not found. */ + stat: (absolutePath: string) => Promise<{ isDirectory: () => boolean }> | { isDirectory: () => boolean }; + /** Read directory entries */ + readdir: (absolutePath: string) => Promise | string[]; +} + +const defaultLsOperations: LsOperations = { + exists: pathExists, + stat: fsStat, + readdir: fsReaddir, +}; + +export interface LsToolOptions { + /** Custom operations for directory listing. Default: local filesystem */ + operations?: LsOperations; +} + +export function createLsToolDefinition( + cwd: string, + options?: LsToolOptions, +): ToolDefinition { + const ops = options?.operations ?? defaultLsOperations; + return { + name: "ls", + label: "ls", + description: `List directory contents. Returns entries sorted alphabetically, with '/' suffix for directories. Includes dotfiles. Output is truncated to ${DEFAULT_LIMIT} entries or ${DEFAULT_MAX_BYTES / 1024}KB (whichever is hit first).`, + promptSnippet: lsToolSystemPromptContribution.snippet, + parameters: lsSchema, + async execute( + _toolCallId, + { path, limit }: { path?: string; limit?: number }, + signal?: AbortSignal, + _onUpdate?, + ctx?: ExtensionContext, + ) { + return new Promise((resolve, reject) => { + if (signal?.aborted) { + reject(new Error("Operation aborted")); + return; + } + + const onAbort = () => reject(new Error("Operation aborted")); + signal?.addEventListener("abort", onAbort, { once: true }); + + (async () => { + try { + const dirPath = resolveToCwd(path || ".", ctx?.cwd || cwd); + const effectiveLimit = limit ?? DEFAULT_LIMIT; + + // Check if path exists. + if (!(await ops.exists(dirPath))) { + reject(new Error(`Path not found: ${dirPath}`)); + return; + } + + // Check if path is a directory. + const stat = await ops.stat(dirPath); + if (!stat.isDirectory()) { + reject(new Error(`Not a directory: ${dirPath}`)); + return; + } + + // Read directory entries. + let entries: string[]; + try { + entries = await ops.readdir(dirPath); + } catch (e: any) { + reject(new Error(`Cannot read directory: ${e.message}`)); + return; + } + + // Sort alphabetically, case-insensitive. + entries.sort((a, b) => a.toLowerCase().localeCompare(b.toLowerCase())); + + // Format entries with directory indicators. + const results: string[] = []; + let entryLimitReached = false; + for (const entry of entries) { + if (results.length >= effectiveLimit) { + entryLimitReached = true; + break; + } + + const fullPath = nodePath.join(dirPath, entry); + let suffix = ""; + try { + const entryStat = await ops.stat(fullPath); + if (entryStat.isDirectory()) suffix = "/"; + } catch { + // Skip entries we cannot stat. + continue; + } + results.push(entry + suffix); + } + + signal?.removeEventListener("abort", onAbort); + + if (results.length === 0) { + resolve({ content: [{ type: "text", text: "(empty directory)" }], details: undefined }); + return; + } + + const rawOutput = results.join("\n"); + // Apply byte truncation. There is no separate line limit because entry count is already capped. + const truncation = truncateHead(rawOutput, { maxLines: Number.MAX_SAFE_INTEGER }); + let output = truncation.content; + const details: LsToolDetails = {}; + // Build actionable notices for truncation and entry limits. + const notices: string[] = []; + if (entryLimitReached) { + notices.push(`${effectiveLimit} entries limit reached. Use limit=${effectiveLimit * 2} for more`); + details.entryLimitReached = effectiveLimit; + } + if (truncation.truncated) { + notices.push(`${formatSize(DEFAULT_MAX_BYTES)} limit reached`); + details.truncation = truncation; + } + if (notices.length > 0) { + output += `\n\n[${notices.join(". ")}]`; + } + + resolve({ + content: [{ type: "text", text: output }], + details: Object.keys(details).length > 0 ? details : undefined, + }); + } catch (e: any) { + signal?.removeEventListener("abort", onAbort); + reject(e); + } + })(); + }); + }, + ...lsRenderers, + }; +} + +export function createLsTool(cwd: string, options?: LsToolOptions): AgentTool { + return wrapToolDefinition(createLsToolDefinition(cwd, options)); +} diff --git a/packages/coding-agent/src/core/tools/output-accumulator.ts b/packages/coding-agent/src/core/tools/output-accumulator.ts new file mode 100644 index 0000000000000000000000000000000000000000..e13fe458e747d81aface82cf25aa264a186967a0 --- /dev/null +++ b/packages/coding-agent/src/core/tools/output-accumulator.ts @@ -0,0 +1,222 @@ +import { randomBytes } from "node:crypto"; +import { createWriteStream, type WriteStream } from "node:fs"; +import { tmpdir } from "node:os"; +import { join } from "node:path"; +import { DEFAULT_MAX_BYTES, DEFAULT_MAX_LINES, type TruncationResult, truncateTail } from "./truncate.ts"; + +export interface OutputAccumulatorOptions { + maxLines?: number; + maxBytes?: number; + tempFilePrefix?: string; +} + +export interface OutputSnapshot { + content: string; + truncation: TruncationResult; + fullOutputPath?: string; +} + +function defaultTempFilePath(prefix: string): string { + const id = randomBytes(8).toString("hex"); + return join(tmpdir(), `${prefix}-${id}.log`); +} + +function byteLength(text: string): number { + return Buffer.byteLength(text, "utf-8"); +} + +/** + * Incrementally tracks streaming output with bounded memory. + * + * Appends decode chunks with a streaming UTF-8 decoder, keeps only a decoded + * tail for display snapshots, and opens a temp file when the full output needs + * to be preserved. + */ +export class OutputAccumulator { + private readonly maxLines: number; + private readonly maxBytes: number; + private readonly maxRollingBytes: number; + private readonly tempFilePrefix: string; + private readonly decoder = new TextDecoder(); + + private rawChunks: Buffer[] = []; + private tailText = ""; + private tailBytes = 0; + private tailStartsAtLineBoundary = true; + private totalRawBytes = 0; + private totalDecodedBytes = 0; + private completedLines = 0; + private totalLines = 0; + private currentLineBytes = 0; + private hasOpenLine = false; + private finished = false; + + private tempFilePath: string | undefined; + private tempFileStream: WriteStream | undefined; + + constructor(options: OutputAccumulatorOptions = {}) { + this.maxLines = options.maxLines ?? DEFAULT_MAX_LINES; + this.maxBytes = options.maxBytes ?? DEFAULT_MAX_BYTES; + this.maxRollingBytes = Math.max(this.maxBytes * 2, 1); + this.tempFilePrefix = options.tempFilePrefix ?? "pi-output"; + } + + append(data: Buffer): void { + if (this.finished) { + throw new Error("Cannot append to a finished output accumulator"); + } + + this.totalRawBytes += data.length; + this.appendDecodedText(this.decoder.decode(data, { stream: true })); + + if (this.tempFileStream || this.shouldUseTempFile()) { + this.ensureTempFile(); + this.tempFileStream?.write(data); + } else if (data.length > 0) { + this.rawChunks.push(data); + } + } + + finish(): void { + if (this.finished) { + return; + } + this.finished = true; + this.appendDecodedText(this.decoder.decode()); + if (this.shouldUseTempFile()) { + this.ensureTempFile(); + } + } + + snapshot(options: { persistIfTruncated?: boolean } = {}): OutputSnapshot { + const tailTruncation = truncateTail(this.getSnapshotText(), { + maxLines: this.maxLines, + maxBytes: this.maxBytes, + }); + const truncated = this.totalLines > this.maxLines || this.totalDecodedBytes > this.maxBytes; + const truncatedBy = truncated + ? (tailTruncation.truncatedBy ?? (this.totalDecodedBytes > this.maxBytes ? "bytes" : "lines")) + : null; + const truncation: TruncationResult = { + ...tailTruncation, + truncated, + truncatedBy, + totalLines: this.totalLines, + totalBytes: this.totalDecodedBytes, + maxLines: this.maxLines, + maxBytes: this.maxBytes, + }; + + if (options.persistIfTruncated && truncation.truncated) { + this.ensureTempFile(); + } + + return { + content: truncation.content, + truncation, + fullOutputPath: this.tempFilePath, + }; + } + + async closeTempFile(): Promise { + if (!this.tempFileStream) { + return; + } + + const stream = this.tempFileStream; + this.tempFileStream = undefined; + + await new Promise((resolve, reject) => { + const onError = (error: Error) => { + stream.off("finish", onFinish); + reject(error); + }; + const onFinish = () => { + stream.off("error", onError); + resolve(); + }; + stream.once("error", onError); + stream.once("finish", onFinish); + stream.end(); + }); + } + + getLastLineBytes(): number { + return this.currentLineBytes; + } + + private appendDecodedText(text: string): void { + if (text.length === 0) { + return; + } + + const bytes = byteLength(text); + this.totalDecodedBytes += bytes; + this.tailText += text; + this.tailBytes += bytes; + if (this.tailBytes > this.maxRollingBytes * 2) { + this.trimTail(); + } + + let newlines = 0; + let lastNewline = -1; + for (let i = text.indexOf("\n"); i !== -1; i = text.indexOf("\n", i + 1)) { + newlines++; + lastNewline = i; + } + if (newlines === 0) { + this.currentLineBytes += bytes; + this.hasOpenLine = true; + } else { + this.completedLines += newlines; + const tail = text.slice(lastNewline + 1); + this.currentLineBytes = byteLength(tail); + this.hasOpenLine = tail.length > 0; + } + this.totalLines = this.completedLines + (this.hasOpenLine ? 1 : 0); + } + + private trimTail(): void { + const buffer = Buffer.from(this.tailText, "utf-8"); + if (buffer.length <= this.maxRollingBytes) { + this.tailBytes = buffer.length; + return; + } + + let start = buffer.length - this.maxRollingBytes; + while (start < buffer.length && (buffer[start] & 0xc0) === 0x80) { + start++; + } + + this.tailStartsAtLineBoundary = start === 0 ? this.tailStartsAtLineBoundary : buffer[start - 1] === 0x0a; + this.tailText = buffer.subarray(start).toString("utf-8"); + this.tailBytes = byteLength(this.tailText); + } + + private getSnapshotText(): string { + if (this.tailStartsAtLineBoundary) { + return this.tailText; + } + + const firstNewline = this.tailText.indexOf("\n"); + return firstNewline === -1 ? this.tailText : this.tailText.slice(firstNewline + 1); + } + + private shouldUseTempFile(): boolean { + return ( + this.totalRawBytes > this.maxBytes || this.totalDecodedBytes > this.maxBytes || this.totalLines > this.maxLines + ); + } + + private ensureTempFile(): void { + if (this.tempFilePath) { + return; + } + this.tempFilePath = defaultTempFilePath(this.tempFilePrefix); + this.tempFileStream = createWriteStream(this.tempFilePath); + for (const chunk of this.rawChunks) { + this.tempFileStream.write(chunk); + } + this.rawChunks = []; + } +} diff --git a/packages/coding-agent/src/core/tools/path-utils.ts b/packages/coding-agent/src/core/tools/path-utils.ts new file mode 100644 index 0000000000000000000000000000000000000000..1f9ab4cc9ec52279d1aa0b22739e1eea3c430177 --- /dev/null +++ b/packages/coding-agent/src/core/tools/path-utils.ts @@ -0,0 +1,118 @@ +import { accessSync, constants } from "node:fs"; +import { access } from "node:fs/promises"; +import { normalizePath, resolvePath } from "../../utils/paths.ts"; + +const NARROW_NO_BREAK_SPACE = "\u202F"; + +function tryMacOSScreenshotPath(filePath: string): string { + return filePath.replace(/ (AM|PM)\./gi, `${NARROW_NO_BREAK_SPACE}$1.`); +} + +function tryNFDVariant(filePath: string): string { + // macOS stores filenames in NFD (decomposed) form, try converting user input to NFD + return filePath.normalize("NFD"); +} + +function tryCurlyQuoteVariant(filePath: string): string { + // macOS uses U+2019 (right single quotation mark) in screenshot names like "Capture d'écran" + // Users typically type U+0027 (straight apostrophe) + return filePath.replace(/'/g, "\u2019"); +} + +function fileExists(filePath: string): boolean { + try { + accessSync(filePath, constants.F_OK); + return true; + } catch { + return false; + } +} + +export async function pathExists(filePath: string): Promise { + try { + await access(filePath, constants.F_OK); + return true; + } catch { + return false; + } +} + +export function expandPath(filePath: string): string { + return normalizePath(filePath, { normalizeUnicodeSpaces: true, stripAtPrefix: true }); +} + +/** + * Resolve a path relative to the given cwd. + * Handles ~ expansion and absolute paths. + */ +export function resolveToCwd(filePath: string, cwd: string): string { + return resolvePath(filePath, cwd, { normalizeUnicodeSpaces: true, stripAtPrefix: true }); +} + +export function resolveReadPath(filePath: string, cwd: string): string { + const resolved = resolveToCwd(filePath, cwd); + + if (fileExists(resolved)) { + return resolved; + } + + // Try macOS AM/PM variant (narrow no-break space before AM/PM) + const amPmVariant = tryMacOSScreenshotPath(resolved); + if (amPmVariant !== resolved && fileExists(amPmVariant)) { + return amPmVariant; + } + + // Try NFD variant (macOS stores filenames in NFD form) + const nfdVariant = tryNFDVariant(resolved); + if (nfdVariant !== resolved && fileExists(nfdVariant)) { + return nfdVariant; + } + + // Try curly quote variant (macOS uses U+2019 in screenshot names) + const curlyVariant = tryCurlyQuoteVariant(resolved); + if (curlyVariant !== resolved && fileExists(curlyVariant)) { + return curlyVariant; + } + + // Try combined NFD + curly quote (for French macOS screenshots like "Capture d'écran") + const nfdCurlyVariant = tryCurlyQuoteVariant(nfdVariant); + if (nfdCurlyVariant !== resolved && fileExists(nfdCurlyVariant)) { + return nfdCurlyVariant; + } + + return resolved; +} + +export async function resolveReadPathAsync(filePath: string, cwd: string): Promise { + const resolved = resolveToCwd(filePath, cwd); + + if (await pathExists(resolved)) { + return resolved; + } + + // Try macOS AM/PM variant (narrow no-break space before AM/PM) + const amPmVariant = tryMacOSScreenshotPath(resolved); + if (amPmVariant !== resolved && (await pathExists(amPmVariant))) { + return amPmVariant; + } + + // Try NFD variant (macOS stores filenames in NFD form) + const nfdVariant = tryNFDVariant(resolved); + if (nfdVariant !== resolved && (await pathExists(nfdVariant))) { + return nfdVariant; + } + + // Try curly quote variant (macOS uses U+2019 in screenshot names) + const curlyVariant = tryCurlyQuoteVariant(resolved); + if (curlyVariant !== resolved && (await pathExists(curlyVariant))) { + return curlyVariant; + } + + // Try combined NFD + curly quote (for French macOS screenshots like "Capture d'écran") + const nfdCurlyVariant = tryCurlyQuoteVariant(nfdVariant); + if (nfdCurlyVariant !== resolved && (await pathExists(nfdCurlyVariant))) { + return nfdCurlyVariant; + } + + return resolved; +} diff --git a/packages/coding-agent/src/core/tools/powershell.ts b/packages/coding-agent/src/core/tools/powershell.ts new file mode 100644 index 0000000000000000000000000000000000000000..ac8a491170ee5c0661be61151c811065b0df3f41 --- /dev/null +++ b/packages/coding-agent/src/core/tools/powershell.ts @@ -0,0 +1,67 @@ +import { getPowerShellConfig } from "../../utils/shell.ts"; +import { + type BashOperations, + type BashSpawnContext, + type BashSpawnHook, + type BashToolDetails, + type BashToolInput, + type BashToolOptions, + type createBashTool, + createLocalShellOperations, + createShellToolDefinition, + type ShellToolConfig, +} from "./bash.ts"; +import { wrapToolDefinition } from "./tool-definition-wrapper.ts"; + +const UTF8_OUTPUT_PREFIX = "try { [Console]::OutputEncoding=[System.Text.Encoding]::UTF8 } catch {}\n"; + +export const powershellToolSystemPromptContribution = { + snippet: "Execute PowerShell commands", + guidelines: ["You can inspect PI_* environment variables for current model and session details."], +} as const; + +export type PowerShellOperations = BashOperations; +export type PowerShellSpawnContext = BashSpawnContext; +export type PowerShellSpawnHook = BashSpawnHook; +export type PowerShellToolDetails = BashToolDetails; +export type PowerShellToolInput = BashToolInput; + +export interface PowerShellToolOptions + extends Pick {} + +export function createLocalPowerShellOperations(): PowerShellOperations { + const operations = createLocalShellOperations("PowerShell", getPowerShellConfig); + return { + exec: (command, cwd, options) => operations.exec(`${UTF8_OUTPUT_PREFIX}${command}`, cwd, options), + }; +} + +const powershellToolConfig: ShellToolConfig = { + name: "powershell", + label: "powershell", + shellName: "PowerShell", + prompt: "PS>", + promptSnippet: powershellToolSystemPromptContribution.snippet, + promptGuidelines: powershellToolSystemPromptContribution.guidelines, + tempFilePrefix: "pi-powershell", +}; + +export function createPowerShellToolDefinition( + cwd: string, + options?: PowerShellToolOptions, +): ReturnType { + return createShellToolDefinition(cwd, powershellToolConfig, { + ...options, + operations: options?.operations ?? createLocalPowerShellOperations(), + }); +} + +export function createPowerShellTool(cwd: string, options?: PowerShellToolOptions): ReturnType { + const definition = createPowerShellToolDefinition(cwd, options); + const tool = wrapToolDefinition(definition); + Object.assign(tool, { + promptSnippet: definition.promptSnippet, + promptGuidelines: definition.promptGuidelines, + }); + return tool; +} diff --git a/packages/coding-agent/src/core/tools/read.ts b/packages/coding-agent/src/core/tools/read.ts new file mode 100644 index 0000000000000000000000000000000000000000..a93142cc32250a00d150408eaa07eb0ee8181ad0 --- /dev/null +++ b/packages/coding-agent/src/core/tools/read.ts @@ -0,0 +1,197 @@ +import type { AgentTool } from "@earendil-works/pi-agent-core"; +import type { Api, ImageContent, Model, TextContent } from "@earendil-works/pi-ai"; +import { constants } from "fs"; +import { access as fsAccess, readFile as fsReadFile } from "fs/promises"; +import { type Static, Type } from "typebox"; +import { processImage } from "../../utils/image-process.ts"; +import { detectSupportedImageMimeTypeFromFile } from "../../utils/mime.ts"; +import type { ExtensionContext, ToolDefinition } from "../extensions/types.ts"; +import { resolveReadPathAsync } from "./path-utils.ts"; +import { readRenderers } from "./renderers/read.ts"; +import { wrapToolDefinition } from "./tool-definition-wrapper.ts"; +import { DEFAULT_MAX_BYTES, DEFAULT_MAX_LINES, formatSize, type TruncationResult, truncateHead } from "./truncate.ts"; + +const readSchema = Type.Object({ + path: Type.String({ description: "Path to the file to read (relative or absolute)" }), + offset: Type.Optional(Type.Number({ description: "Line number to start reading from (1-indexed)" })), + limit: Type.Optional(Type.Number({ description: "Maximum number of lines to read" })), +}); + +export const readToolSystemPromptContribution = { + snippet: "Read file contents", + guidelines: ["Use read to examine files instead of cat or sed."], +} as const; + +export type ReadToolInput = Static; + +export interface ReadToolDetails { + truncation?: TruncationResult; +} + +/** + * Pluggable operations for the read tool. + * Override these to delegate file reading to remote systems (for example SSH). + */ +export interface ReadOperations { + /** Read file contents as a Buffer */ + readFile: (absolutePath: string) => Promise; + /** Check if file is readable (throw if not) */ + access: (absolutePath: string) => Promise; + /** Detect image MIME type, return null or undefined for non-images */ + detectImageMimeType?: (absolutePath: string) => Promise; +} + +const defaultReadOperations: ReadOperations = { + readFile: (path) => fsReadFile(path), + access: (path) => fsAccess(path, constants.R_OK), + detectImageMimeType: detectSupportedImageMimeTypeFromFile, +}; + +export interface ReadToolOptions { + /** Whether to auto-resize images to 2000x2000 max. Default: true */ + autoResizeImages?: boolean; + /** Custom operations for file reading. Default: local filesystem */ + operations?: ReadOperations; +} + +function getNonVisionImageNote(model: Model | undefined): string | undefined { + if (!model || model.input.includes("image")) { + return undefined; + } + return "[Current model does not support images. The image will be omitted from this request.]"; +} + +export function createReadToolDefinition( + cwd: string, + options?: ReadToolOptions, +): ToolDefinition { + const autoResizeImages = options?.autoResizeImages ?? true; + const ops = options?.operations ?? defaultReadOperations; + return { + name: "read", + label: "read", + description: `Read the contents of a file. Supports text files and images (jpg, png, gif, webp, bmp). Images are sent as attachments. For text files, output is truncated to ${DEFAULT_MAX_LINES} lines or ${DEFAULT_MAX_BYTES / 1024}KB (whichever is hit first). Use offset/limit for large files. When you need the full file, continue with offset until complete.`, + promptSnippet: readToolSystemPromptContribution.snippet, + promptGuidelines: [...readToolSystemPromptContribution.guidelines], + parameters: readSchema, + constrainedSampling: { type: "json_schema", strict: "prefer" }, + async execute( + _toolCallId, + { path, offset, limit }: { path: string; offset?: number; limit?: number }, + signal?: AbortSignal, + _onUpdate?, + ctx?: ExtensionContext, + ) { + return new Promise<{ content: (TextContent | ImageContent)[]; details: ReadToolDetails | undefined }>( + (resolve, reject) => { + if (signal?.aborted) { + reject(new Error("Operation aborted")); + return; + } + let aborted = false; + const onAbort = () => { + aborted = true; + reject(new Error("Operation aborted")); + }; + signal?.addEventListener("abort", onAbort, { once: true }); + + (async () => { + try { + const absolutePath = await resolveReadPathAsync(path, ctx?.cwd || cwd); + if (aborted) return; + // Check if file exists and is readable. + await ops.access(absolutePath); + if (aborted) return; + const mimeType = ops.detectImageMimeType ? await ops.detectImageMimeType(absolutePath) : undefined; + let content: (TextContent | ImageContent)[]; + let details: ReadToolDetails | undefined; + const nonVisionImageNote = getNonVisionImageNote(ctx?.model); + if (mimeType) { + // Read image as binary. + const buffer = await ops.readFile(absolutePath); + const processed = await processImage(buffer, mimeType, { autoResizeImages }); + if (!processed.ok) { + let textNote = `Read image file [${mimeType}]\n${processed.message}`; + if (nonVisionImageNote) textNote += `\n${nonVisionImageNote}`; + content = [{ type: "text", text: textNote }]; + } else { + let textNote = `Read image file [${processed.mimeType}]`; + if (processed.hints.length > 0) textNote += `\n${processed.hints.join("\n")}`; + if (nonVisionImageNote) textNote += `\n${nonVisionImageNote}`; + content = [ + { type: "text", text: textNote }, + { type: "image", data: processed.data, mimeType: processed.mimeType }, + ]; + } + } else { + // Read text content. + const buffer = await ops.readFile(absolutePath); + const textContent = buffer.toString("utf-8"); + const allLines = textContent.split("\n"); + const totalFileLines = allLines.length; + // Apply offset if specified. Convert from 1-indexed input to 0-indexed array access. + const startLine = offset ? Math.max(0, offset - 1) : 0; + const startLineDisplay = startLine + 1; + // Check if offset is out of bounds. + if (startLine >= allLines.length) { + throw new Error(`Offset ${offset} is beyond end of file (${allLines.length} lines total)`); + } + let selectedContent: string; + let userLimitedLines: number | undefined; + // If limit is specified by the user, honor it first. Otherwise truncateHead decides. + if (limit !== undefined) { + const endLine = Math.min(startLine + limit, allLines.length); + selectedContent = allLines.slice(startLine, endLine).join("\n"); + userLimitedLines = endLine - startLine; + } else { + selectedContent = allLines.slice(startLine).join("\n"); + } + // Apply truncation, respecting both line and byte limits. + const truncation = truncateHead(selectedContent); + let outputText: string; + if (truncation.firstLineExceedsLimit) { + // First line alone exceeds the byte limit. Point the model at a bash fallback. + const firstLineSize = formatSize(Buffer.byteLength(allLines[startLine], "utf-8")); + outputText = `[Line ${startLineDisplay} is ${firstLineSize}, exceeds ${formatSize(DEFAULT_MAX_BYTES)} limit. Use bash: sed -n '${startLineDisplay}p' ${path} | head -c ${DEFAULT_MAX_BYTES}]`; + details = { truncation }; + } else if (truncation.truncated) { + // Truncation occurred. Build an actionable continuation notice. + const endLineDisplay = startLineDisplay + truncation.outputLines - 1; + const nextOffset = endLineDisplay + 1; + outputText = truncation.content; + if (truncation.truncatedBy === "lines") { + outputText += `\n\n[Showing lines ${startLineDisplay}-${endLineDisplay} of ${totalFileLines}. Use offset=${nextOffset} to continue.]`; + } else { + outputText += `\n\n[Showing lines ${startLineDisplay}-${endLineDisplay} of ${totalFileLines} (${formatSize(DEFAULT_MAX_BYTES)} limit). Use offset=${nextOffset} to continue.]`; + } + details = { truncation }; + } else if (userLimitedLines !== undefined && startLine + userLimitedLines < allLines.length) { + // User-specified limit stopped early, but the file still has more content. + const remaining = allLines.length - (startLine + userLimitedLines); + const nextOffset = startLine + userLimitedLines + 1; + outputText = `${truncation.content}\n\n[${remaining} more lines in file. Use offset=${nextOffset} to continue.]`; + } else { + // No truncation and no remaining user-limited content. + outputText = truncation.content; + } + content = [{ type: "text", text: outputText }]; + } + + if (aborted) return; + signal?.removeEventListener("abort", onAbort); + resolve({ content, details }); + } catch (error: any) { + signal?.removeEventListener("abort", onAbort); + if (!aborted) reject(error); + } + })(); + }, + ); + }, + ...readRenderers, + }; +} + +export function createReadTool(cwd: string, options?: ReadToolOptions): AgentTool { + return wrapToolDefinition(createReadToolDefinition(cwd, options)); +} diff --git a/packages/coding-agent/src/core/tools/render-utils.ts b/packages/coding-agent/src/core/tools/render-utils.ts new file mode 100644 index 0000000000000000000000000000000000000000..48a5b0af00ce2adddcb21e6b51dc301b005366fc --- /dev/null +++ b/packages/coding-agent/src/core/tools/render-utils.ts @@ -0,0 +1,85 @@ +import * as os from "node:os"; +import { pathToFileURL } from "node:url"; +import type { ImageContent, TextContent } from "@earendil-works/pi-ai"; +import { getCapabilities, getImageDimensions, hyperlink, imageFallback } from "@earendil-works/pi-tui"; +import type { Theme } from "../../modes/interactive/theme/theme.ts"; +import { stripAnsi } from "../../utils/ansi.ts"; +import { resolvePath } from "../../utils/paths.ts"; +import { sanitizeBinaryOutput } from "../../utils/shell.ts"; + +export function shortenPath(path: unknown): string { + if (typeof path !== "string") return ""; + const home = os.homedir(); + if (path.startsWith(home)) { + return `~${path.slice(home.length)}`; + } + return path; +} + +export function linkPath(styledText: string, rawPath: string, cwd: string): string { + if (!getCapabilities().hyperlinks) return styledText; + const absolutePath = resolvePath(rawPath, cwd); + return hyperlink(styledText, pathToFileURL(absolutePath).href); +} + +export function str(value: unknown): string | null { + if (typeof value === "string") return value; + if (value == null) return ""; + return null; +} + +export function replaceTabs(text: string): string { + return text.replace(/\t/g, " "); +} + +export function normalizeDisplayText(text: string): string { + return text.replace(/\r/g, ""); +} + +export function getTextOutput( + result: { content: Array<{ type: string; text?: string; data?: string; mimeType?: string }> } | undefined, + showImages: boolean, +): string { + if (!result) return ""; + + const textBlocks = result.content.filter((c) => c.type === "text"); + const imageBlocks = result.content.filter((c) => c.type === "image"); + + let output = textBlocks.map((c) => sanitizeBinaryOutput(stripAnsi(c.text || "")).replace(/\r/g, "")).join("\n"); + + const caps = getCapabilities(); + if (imageBlocks.length > 0 && (!caps.images || !showImages)) { + const imageIndicators = imageBlocks + .map((img) => { + const mimeType = img.mimeType ?? "image/unknown"; + const dims = + img.data && img.mimeType ? (getImageDimensions(img.data, img.mimeType) ?? undefined) : undefined; + return imageFallback(mimeType, dims); + }) + .join("\n"); + output = output ? `${output}\n${imageIndicators}` : imageIndicators; + } + + return output; +} + +export type ToolRenderResultLike = { + content: (TextContent | ImageContent)[]; + details: TDetails; +}; + +export function invalidArgText(theme: Theme): string { + return theme.fg("error", "[invalid arg]"); +} + +export function renderToolPath( + rawPath: string | null, + theme: Theme, + cwd: string, + options?: { emptyFallback?: string }, +): string { + if (rawPath === null) return invalidArgText(theme); + const value = rawPath || options?.emptyFallback; + if (!value) return theme.fg("toolOutput", "..."); + return linkPath(theme.fg("accent", shortenPath(value)), value, cwd); +} diff --git a/packages/coding-agent/src/core/tools/renderers/bash.ts b/packages/coding-agent/src/core/tools/renderers/bash.ts new file mode 100644 index 0000000000000000000000000000000000000000..01e170a82ab0ea33e2ec1bf186333fe86c0ee466 --- /dev/null +++ b/packages/coding-agent/src/core/tools/renderers/bash.ts @@ -0,0 +1,163 @@ +/** + * Presentation for the shell tools. + * + * Renderers live apart from the implementation so a process that only displays tool output does not + * load the execution path or its typebox parameter schema. `bash.ts` spreads these into the shell + * tool definition, so the tool's public shape is unchanged. + */ + +import { Container, Text, truncateToWidth } from "@earendil-works/pi-tui"; +import { keyHint } from "../../../modes/interactive/components/keybinding-hints.ts"; +import { truncateToVisualLines } from "../../../modes/interactive/components/visual-truncate.ts"; +import { theme } from "../../../modes/interactive/theme/theme.ts"; +import type { ToolDefinition, ToolRenderResultOptions } from "../../extensions/types.ts"; +import type { BashToolDetails } from "../bash.ts"; +import { getTextOutput, invalidArgText, str } from "../render-utils.ts"; +import { DEFAULT_MAX_BYTES, formatSize } from "../truncate.ts"; + +const BASH_PREVIEW_LINES = 5; +export const BASH_UPDATE_THROTTLE_MS = 100; +type BashResultRenderState = { + cachedWidth: number | undefined; + cachedLines: string[] | undefined; + cachedSkipped: number | undefined; +}; +class BashResultRenderComponent extends Container { + state: BashResultRenderState = { + cachedWidth: undefined, + cachedLines: undefined, + cachedSkipped: undefined, + }; +} +function formatDuration(ms: number): string { + return `${(ms / 1000).toFixed(1)}s`; +} +function formatShellCall(args: { command?: string; timeout?: number } | undefined, prompt: string): string { + const command = str(args?.command); + const timeout = args?.timeout as number | undefined; + const timeoutSuffix = timeout ? theme.fg("muted", ` (timeout ${timeout}s)`) : ""; + const commandDisplay = command === null ? invalidArgText(theme) : command ? command : theme.fg("toolOutput", "..."); + return theme.fg("toolTitle", theme.bold(`${prompt} ${commandDisplay}`)) + timeoutSuffix; +} +function rebuildBashResultRenderComponent( + component: BashResultRenderComponent, + result: { + content: Array<{ type: string; text?: string; data?: string; mimeType?: string }>; + details?: BashToolDetails; + }, + options: ToolRenderResultOptions, + showImages: boolean, + startedAt: number | undefined, + endedAt: number | undefined, +): void { + const state = component.state; + component.clear(); + + let output = getTextOutput(result as any, showImages).trim(); + const truncation = result.details?.truncation; + const fullOutputPath = result.details?.fullOutputPath; + if (!options.isPartial && truncation?.truncated && fullOutputPath && output.endsWith("]")) { + const footerStart = output.lastIndexOf("\n\n["); + if (footerStart !== -1 && output.slice(footerStart).includes(fullOutputPath)) { + output = output.slice(0, footerStart).trimEnd(); + } + } + + if (output) { + const styledOutput = output + .split("\n") + .map((line) => theme.fg("toolOutput", line)) + .join("\n"); + + if (options.expanded) { + component.addChild(new Text(`\n${styledOutput}`, 0, 0)); + } else { + component.addChild({ + render: (width: number) => { + if (state.cachedLines === undefined || state.cachedWidth !== width) { + const preview = truncateToVisualLines(styledOutput, BASH_PREVIEW_LINES, width); + state.cachedLines = preview.visualLines; + state.cachedSkipped = preview.skippedCount; + state.cachedWidth = width; + } + if (state.cachedSkipped && state.cachedSkipped > 0) { + const hint = + theme.fg("muted", `... (${state.cachedSkipped} earlier lines,`) + + ` ${keyHint("app.tools.expand", "to expand")}${theme.fg("muted", ")")}`; + return ["", truncateToWidth(hint, width, "..."), ...(state.cachedLines ?? [])]; + } + return ["", ...(state.cachedLines ?? [])]; + }, + invalidate: () => { + state.cachedWidth = undefined; + state.cachedLines = undefined; + state.cachedSkipped = undefined; + }, + }); + } + } + + if (truncation?.truncated || fullOutputPath) { + const warnings: string[] = []; + if (fullOutputPath) { + warnings.push(`Full output: ${fullOutputPath}`); + } + if (truncation?.truncated) { + if (truncation.truncatedBy === "lines") { + warnings.push(`Truncated: showing ${truncation.outputLines} of ${truncation.totalLines} lines`); + } else { + warnings.push( + `Truncated: ${truncation.outputLines} lines shown (${formatSize(truncation.maxBytes ?? DEFAULT_MAX_BYTES)} limit)`, + ); + } + } + component.addChild(new Text(`\n${theme.fg("warning", `[${warnings.join(". ")}]`)}`, 0, 0)); + } + + if (startedAt !== undefined) { + const label = options.isPartial ? "Elapsed" : "Took"; + const endTime = endedAt ?? Date.now(); + component.addChild(new Text(`\n${theme.fg("muted", `${label} ${formatDuration(endTime - startedAt)}`)}`, 0, 0)); + } +} + +/** Shell renderers are shared by bash and powershell, which differ only in the prompt they display. */ +export function createShellRenderers(prompt: string): Pick, "renderCall" | "renderResult"> { + return { + renderCall(args, _theme, context) { + const state = context.state; + if (context.executionStarted && state.startedAt === undefined) { + state.startedAt = Date.now(); + state.endedAt = undefined; + } + const text = (context.lastComponent as Text | undefined) ?? new Text("", 0, 0); + text.setText(formatShellCall(args as { command?: string; timeout?: number } | undefined, prompt)); + return text; + }, + renderResult(result, options, _theme, context) { + const state = context.state; + if (state.startedAt !== undefined && options.isPartial && !state.interval) { + state.interval = setInterval(() => context.invalidate(), 1000); + } + if (!options.isPartial || context.isError) { + state.endedAt ??= Date.now(); + if (state.interval) { + clearInterval(state.interval); + state.interval = undefined; + } + } + const component = + (context.lastComponent as BashResultRenderComponent | undefined) ?? new BashResultRenderComponent(); + rebuildBashResultRenderComponent( + component, + result as any, + options, + context.showImages, + state.startedAt, + state.endedAt, + ); + component.invalidate(); + return component; + }, + }; +} diff --git a/packages/coding-agent/src/core/tools/renderers/edit.ts b/packages/coding-agent/src/core/tools/renderers/edit.ts new file mode 100644 index 0000000000000000000000000000000000000000..6db55567109f8efc388fb0782b951b3678c8dea6 --- /dev/null +++ b/packages/coding-agent/src/core/tools/renderers/edit.ts @@ -0,0 +1,238 @@ +/** + * Presentation for the edit tool. + * + * Renderers live apart from the implementation so a process that only displays tool output does not + * load the execution path or its typebox parameter schema. `edit.ts` spreads these into its + * definition, so the tool's public shape is unchanged. + */ + +import { Box, Container, Spacer, Text } from "@earendil-works/pi-tui"; +import { renderDiff } from "../../../modes/interactive/components/diff.ts"; +import type { Theme } from "../../../modes/interactive/theme/theme.ts"; +import type { ToolDefinition } from "../../extensions/types.ts"; +import type { EditToolDetails } from "../edit.ts"; +import { computeEditsDiff, type Edit, type EditDiffError, type EditDiffResult } from "../edit-diff.ts"; +import { renderToolPath, str } from "../render-utils.ts"; + +type EditPreview = EditDiffResult | EditDiffError; +export type EditRenderState = { + callComponent?: EditCallRenderComponent; +}; +type RenderableEditArgs = { + path?: string; + file_path?: string; + edits?: Edit[]; + oldText?: string; + newText?: string; +}; +type EditToolResultLike = { + content: Array<{ type: string; text?: string; data?: string; mimeType?: string }>; + details?: EditToolDetails; +}; +type EditCallRenderComponent = Box & { + preview?: EditPreview; + previewArgsKey?: string; + previewPending?: boolean; + settledError?: boolean; +}; +function createEditCallRenderComponent(): EditCallRenderComponent { + return Object.assign(new Box(1, 1, (text: string) => text), { + preview: undefined as EditPreview | undefined, + previewArgsKey: undefined as string | undefined, + previewPending: false, + settledError: false, + }); +} +function getEditCallRenderComponent(state: EditRenderState, lastComponent: unknown): EditCallRenderComponent { + if (lastComponent instanceof Box) { + const component = lastComponent as EditCallRenderComponent; + state.callComponent = component; + return component; + } + if (state.callComponent) { + return state.callComponent; + } + const component = createEditCallRenderComponent(); + state.callComponent = component; + return component; +} +function getRenderablePreviewInput(args: RenderableEditArgs | undefined): { path: string; edits: Edit[] } | null { + if (!args) { + return null; + } + + const path = typeof args.path === "string" ? args.path : typeof args.file_path === "string" ? args.file_path : null; + if (!path) { + return null; + } + + if ( + Array.isArray(args.edits) && + args.edits.length > 0 && + args.edits.every((edit) => typeof edit?.oldText === "string" && typeof edit?.newText === "string") + ) { + return { path, edits: args.edits }; + } + + if (typeof args.oldText === "string" && typeof args.newText === "string") { + return { path, edits: [{ oldText: args.oldText, newText: args.newText }] }; + } + + return null; +} +function formatEditCall(args: RenderableEditArgs | undefined, theme: Theme, cwd: string): string { + const pathDisplay = renderToolPath(str(args?.file_path ?? args?.path), theme, cwd); + return `${theme.fg("toolTitle", theme.bold("edit"))} ${pathDisplay}`; +} +function formatEditResult( + args: RenderableEditArgs | undefined, + preview: EditPreview | undefined, + result: EditToolResultLike, + theme: Theme, + isError: boolean, +): string | undefined { + const rawPath = str(args?.file_path ?? args?.path); + const previewDiff = preview && !("error" in preview) ? preview.diff : undefined; + const previewError = preview && "error" in preview ? preview.error : undefined; + if (isError) { + const errorText = result.content + .filter((c) => c.type === "text") + .map((c) => c.text || "") + .join("\n"); + if (!errorText || errorText === previewError) { + return undefined; + } + return theme.fg("error", errorText); + } + + const resultDiff = result.details?.diff; + if (resultDiff && resultDiff !== previewDiff) { + return renderDiff(resultDiff, { filePath: rawPath ?? undefined }); + } + + return undefined; +} +function getEditHeaderBg( + preview: EditPreview | undefined, + settledError: boolean | undefined, + theme: Theme, +): (text: string) => string { + if (preview) { + if ("error" in preview) { + return (text: string) => theme.bg("toolErrorBg", text); + } + return (text: string) => theme.bg("toolSuccessBg", text); + } + if (settledError) { + return (text: string) => theme.bg("toolErrorBg", text); + } + return (text: string) => theme.bg("toolPendingBg", text); +} +function buildEditCallComponent( + component: EditCallRenderComponent, + args: RenderableEditArgs | undefined, + theme: Theme, + cwd: string, +): EditCallRenderComponent { + component.setBgFn(getEditHeaderBg(component.preview, component.settledError, theme)); + component.clear(); + component.addChild(new Text(formatEditCall(args, theme, cwd), 0, 0)); + + if (!component.preview) { + return component; + } + + const body = + "error" in component.preview ? theme.fg("error", component.preview.error) : renderDiff(component.preview.diff); + component.addChild(new Spacer(1)); + component.addChild(new Text(body, 0, 0)); + return component; +} +function setEditPreview( + component: EditCallRenderComponent, + preview: EditPreview, + argsKey: string | undefined, +): boolean { + const current = component.preview; + const changed = + current === undefined || + ("error" in current && "error" in preview + ? current.error !== preview.error + : "error" in current !== "error" in preview) || + (!("error" in current) && + !("error" in preview) && + (current.diff !== preview.diff || current.firstChangedLine !== preview.firstChangedLine)); + component.preview = preview; + component.previewArgsKey = argsKey; + component.previewPending = false; + return changed; +} + +export const editRenderers: Pick, "renderCall" | "renderResult"> = { + renderCall(args, theme, context) { + const component = getEditCallRenderComponent(context.state, context.lastComponent); + const previewInput = getRenderablePreviewInput(args as RenderableEditArgs | undefined); + const argsKey = previewInput ? JSON.stringify({ path: previewInput.path, edits: previewInput.edits }) : undefined; + + if (component.previewArgsKey !== argsKey) { + component.preview = undefined; + component.previewArgsKey = argsKey; + component.previewPending = false; + component.settledError = false; + } + + if (context.argsComplete && previewInput && !component.preview && !component.previewPending) { + component.previewPending = true; + const requestKey = argsKey; + void computeEditsDiff(previewInput.path, previewInput.edits, context.cwd).then((preview) => { + if (component.previewArgsKey === requestKey) { + setEditPreview(component, preview, requestKey); + context.invalidate(); + } + }); + } + + return buildEditCallComponent(component, args as RenderableEditArgs | undefined, theme, context.cwd); + }, + renderResult(result, _options, theme, context) { + const callComponent = context.state.callComponent; + const previewInput = getRenderablePreviewInput(context.args as RenderableEditArgs | undefined); + const argsKey = previewInput ? JSON.stringify({ path: previewInput.path, edits: previewInput.edits }) : undefined; + const typedResult = result as EditToolResultLike; + const resultDiff = !context.isError ? typedResult.details?.diff : undefined; + let changed = false; + if (callComponent) { + if (typeof resultDiff === "string") { + changed = + setEditPreview( + callComponent, + { diff: resultDiff, firstChangedLine: typedResult.details?.firstChangedLine }, + argsKey, + ) || changed; + } + if (callComponent.settledError !== context.isError) { + callComponent.settledError = context.isError; + changed = true; + } + if (changed) { + buildEditCallComponent(callComponent, context.args as RenderableEditArgs | undefined, theme, context.cwd); + } + } + + const output = formatEditResult( + context.args as RenderableEditArgs | undefined, + callComponent?.preview, + typedResult, + theme, + context.isError, + ); + const component = (context.lastComponent as Container | undefined) ?? new Container(); + component.clear(); + if (!output) { + return component; + } + component.addChild(new Spacer(1)); + component.addChild(new Text(output, 1, 0)); + return component; + }, +}; diff --git a/packages/coding-agent/src/core/tools/renderers/find.ts b/packages/coding-agent/src/core/tools/renderers/find.ts new file mode 100644 index 0000000000000000000000000000000000000000..c9c65835d05339269966bf4714e88756c3f12520 --- /dev/null +++ b/packages/coding-agent/src/core/tools/renderers/find.ts @@ -0,0 +1,77 @@ +/** + * Presentation for the find tool. + * + * Renderers live apart from the implementation so a process that only displays tool output does not + * load the execution path or its typebox parameter schema. `find.ts` spreads these into its + * definition, so the tool's public shape is unchanged. + */ + +import { Text } from "@earendil-works/pi-tui"; +import { keyHint } from "../../../modes/interactive/components/keybinding-hints.ts"; +import type { Theme } from "../../../modes/interactive/theme/theme.ts"; +import type { ToolDefinition, ToolRenderResultOptions } from "../../extensions/types.ts"; +import type { FindToolDetails } from "../find.ts"; +import { getTextOutput, invalidArgText, shortenPath, str } from "../render-utils.ts"; +import { DEFAULT_MAX_BYTES, formatSize } from "../truncate.ts"; + +function formatFindCall(args: { pattern: string; path?: string; limit?: number } | undefined, theme: Theme): string { + const pattern = str(args?.pattern); + const rawPath = str(args?.path); + const path = rawPath !== null ? shortenPath(rawPath || ".") : null; + const limit = args?.limit; + const invalidArg = invalidArgText(theme); + let text = + theme.fg("toolTitle", theme.bold("find")) + + " " + + (pattern === null ? invalidArg : theme.fg("accent", pattern || "")) + + theme.fg("toolOutput", ` in ${path === null ? invalidArg : path}`); + if (limit !== undefined) { + text += theme.fg("toolOutput", ` (limit ${limit})`); + } + return text; +} +function formatFindResult( + result: { + content: Array<{ type: string; text?: string; data?: string; mimeType?: string }>; + details?: FindToolDetails; + }, + options: ToolRenderResultOptions, + theme: Theme, + showImages: boolean, +): string { + const output = getTextOutput(result, showImages).trim(); + let text = ""; + if (output) { + const lines = output.split("\n"); + const maxLines = options.expanded ? lines.length : 20; + const displayLines = lines.slice(0, maxLines); + const remaining = lines.length - maxLines; + text += `\n${displayLines.map((line) => theme.fg("toolOutput", line)).join("\n")}`; + if (remaining > 0) { + text += `${theme.fg("muted", `\n... (${remaining} more lines,`)} ${keyHint("app.tools.expand", "to expand")}${theme.fg("muted", ")")}`; + } + } + + const resultLimit = result.details?.resultLimitReached; + const truncation = result.details?.truncation; + if (resultLimit || truncation?.truncated) { + const warnings: string[] = []; + if (resultLimit) warnings.push(`${resultLimit} results limit`); + if (truncation?.truncated) warnings.push(`${formatSize(truncation.maxBytes ?? DEFAULT_MAX_BYTES)} limit`); + text += `\n${theme.fg("warning", `[Truncated: ${warnings.join(", ")}]`)}`; + } + return text; +} + +export const findRenderers: Pick, "renderCall" | "renderResult"> = { + renderCall(args, theme, context) { + const text = (context.lastComponent as Text | undefined) ?? new Text("", 0, 0); + text.setText(formatFindCall(args as any, theme)); + return text; + }, + renderResult(result, options, theme, context) { + const text = (context.lastComponent as Text | undefined) ?? new Text("", 0, 0); + text.setText(formatFindResult(result as any, options, theme, context.showImages)); + return text; + }, +}; diff --git a/packages/coding-agent/src/core/tools/renderers/grep.ts b/packages/coding-agent/src/core/tools/renderers/grep.ts new file mode 100644 index 0000000000000000000000000000000000000000..1f5479a4d5ba6a5adf5611e811a93a4699dc2017 --- /dev/null +++ b/packages/coding-agent/src/core/tools/renderers/grep.ts @@ -0,0 +1,82 @@ +/** + * Presentation for the grep tool. + * + * Renderers live apart from the implementation so a process that only displays tool output does not + * load the execution path or its typebox parameter schema. `grep.ts` spreads these into its + * definition, so the tool's public shape is unchanged. + */ + +import { Text } from "@earendil-works/pi-tui"; +import { keyHint } from "../../../modes/interactive/components/keybinding-hints.ts"; +import type { Theme } from "../../../modes/interactive/theme/theme.ts"; +import type { ToolDefinition, ToolRenderResultOptions } from "../../extensions/types.ts"; +import type { GrepToolDetails } from "../grep.ts"; +import { getTextOutput, invalidArgText, shortenPath, str } from "../render-utils.ts"; +import { DEFAULT_MAX_BYTES, formatSize } from "../truncate.ts"; + +function formatGrepCall( + args: { pattern: string; path?: string; glob?: string; limit?: number } | undefined, + theme: Theme, +): string { + const pattern = str(args?.pattern); + const rawPath = str(args?.path); + const path = rawPath !== null ? shortenPath(rawPath || ".") : null; + const glob = str(args?.glob); + const limit = args?.limit; + const invalidArg = invalidArgText(theme); + let text = + theme.fg("toolTitle", theme.bold("grep")) + + " " + + (pattern === null ? invalidArg : theme.fg("accent", `/${pattern || ""}/`)) + + theme.fg("toolOutput", ` in ${path === null ? invalidArg : path}`); + if (glob) text += theme.fg("toolOutput", ` (${glob})`); + if (limit !== undefined) text += theme.fg("toolOutput", ` limit ${limit}`); + return text; +} +function formatGrepResult( + result: { + content: Array<{ type: string; text?: string; data?: string; mimeType?: string }>; + details?: GrepToolDetails; + }, + options: ToolRenderResultOptions, + theme: Theme, + showImages: boolean, +): string { + const output = getTextOutput(result, showImages).trim(); + let text = ""; + if (output) { + const lines = output.split("\n"); + const maxLines = options.expanded ? lines.length : 15; + const displayLines = lines.slice(0, maxLines); + const remaining = lines.length - maxLines; + text += `\n${displayLines.map((line) => theme.fg("toolOutput", line)).join("\n")}`; + if (remaining > 0) { + text += `${theme.fg("muted", `\n... (${remaining} more lines,`)} ${keyHint("app.tools.expand", "to expand")}${theme.fg("muted", ")")}`; + } + } + + const matchLimit = result.details?.matchLimitReached; + const truncation = result.details?.truncation; + const linesTruncated = result.details?.linesTruncated; + if (matchLimit || truncation?.truncated || linesTruncated) { + const warnings: string[] = []; + if (matchLimit) warnings.push(`${matchLimit} matches limit`); + if (truncation?.truncated) warnings.push(`${formatSize(truncation.maxBytes ?? DEFAULT_MAX_BYTES)} limit`); + if (linesTruncated) warnings.push("some lines truncated"); + text += `\n${theme.fg("warning", `[Truncated: ${warnings.join(", ")}]`)}`; + } + return text; +} + +export const grepRenderers: Pick, "renderCall" | "renderResult"> = { + renderCall(args, theme, context) { + const text = (context.lastComponent as Text | undefined) ?? new Text("", 0, 0); + text.setText(formatGrepCall(args as any, theme)); + return text; + }, + renderResult(result, options, theme, context) { + const text = (context.lastComponent as Text | undefined) ?? new Text("", 0, 0); + text.setText(formatGrepResult(result as any, options, theme, context.showImages)); + return text; + }, +}; diff --git a/packages/coding-agent/src/core/tools/renderers/index.ts b/packages/coding-agent/src/core/tools/renderers/index.ts new file mode 100644 index 0000000000000000000000000000000000000000..6a88b7e8a0b4d73bca7a8b3a08c5a8c2d547d793 --- /dev/null +++ b/packages/coding-agent/src/core/tools/renderers/index.ts @@ -0,0 +1,63 @@ +/** + * Built-in tool renderers, without the tools themselves. + * + * A presentation displays tool calls and results; it does not execute them and does not need their + * typebox parameter schemas. Importing this instead of `core/tools/index.ts` keeps ~17 MB of module + * graph out of a process that only renders. + */ + +import type { ToolDefinition } from "../../extensions/types.ts"; +import type { ToolName } from "../index.ts"; +import { createShellRenderers } from "./bash.ts"; +import { editRenderers } from "./edit.ts"; +import { findRenderers } from "./find.ts"; +import { grepRenderers } from "./grep.ts"; +import { lsRenderers } from "./ls.ts"; +import { readRenderers } from "./read.ts"; +import { writeRenderers } from "./write.ts"; + +export type ToolRenderers = Pick, "renderCall" | "renderResult">; + +export { + createShellRenderers, + editRenderers, + findRenderers, + grepRenderers, + lsRenderers, + readRenderers, + writeRenderers, +}; + +/** Renderers for every built-in tool, keyed by tool name. */ +export function createAllToolRenderers(): Record { + return { + read: readRenderers, + bash: createShellRenderers("$"), + powershell: createShellRenderers("PS>"), + edit: editRenderers, + write: writeRenderers, + grep: grepRenderers, + find: findRenderers, + ls: lsRenderers, + }; +} + +/** + * Merge built-in renderers into a tool definition that does not supply its own. + * + * `ToolExecutionComponent` used to do this lookup itself, which forced every presentation to import + * the tool implementations. Callers do it now, so a process that renders can import renderers alone. + */ +export function withBuiltInRenderers( + toolName: string, + definition: TDefinition | undefined, +): TDefinition | ToolRenderers | undefined { + const builtIn = createAllToolRenderers()[toolName as ToolName]; + if (!definition) return builtIn; + if (!builtIn) return definition; + return { + ...definition, + renderCall: definition.renderCall ?? builtIn.renderCall, + renderResult: definition.renderResult ?? builtIn.renderResult, + }; +} diff --git a/packages/coding-agent/src/core/tools/renderers/ls.ts b/packages/coding-agent/src/core/tools/renderers/ls.ts new file mode 100644 index 0000000000000000000000000000000000000000..bcc1ee000ddde7b39fe05295dea25dff2c7c5da1 --- /dev/null +++ b/packages/coding-agent/src/core/tools/renderers/ls.ts @@ -0,0 +1,70 @@ +/** + * Presentation for the ls tool. + * + * Renderers live apart from the implementation so a process that only displays tool output does not + * load the execution path or its typebox parameter schema. `ls.ts` spreads these into its + * definition, so the tool's public shape is unchanged. + */ + +import { Text } from "@earendil-works/pi-tui"; +import { keyHint } from "../../../modes/interactive/components/keybinding-hints.ts"; +import type { Theme } from "../../../modes/interactive/theme/theme.ts"; +import type { ToolDefinition, ToolRenderResultOptions } from "../../extensions/types.ts"; +import type { LsToolDetails } from "../ls.ts"; +import { getTextOutput, renderToolPath, str } from "../render-utils.ts"; +import { DEFAULT_MAX_BYTES, formatSize } from "../truncate.ts"; + +function formatLsCall(args: { path?: string; limit?: number } | undefined, theme: Theme, cwd: string): string { + const limit = args?.limit; + const pathDisplay = renderToolPath(str(args?.path), theme, cwd, { emptyFallback: "." }); + let text = `${theme.fg("toolTitle", theme.bold("ls"))} ${pathDisplay}`; + if (limit !== undefined) { + text += theme.fg("toolOutput", ` (limit ${limit})`); + } + return text; +} +function formatLsResult( + result: { + content: Array<{ type: string; text?: string; data?: string; mimeType?: string }>; + details?: LsToolDetails; + }, + options: ToolRenderResultOptions, + theme: Theme, + showImages: boolean, +): string { + const output = getTextOutput(result, showImages).trim(); + let text = ""; + if (output) { + const lines = output.split("\n"); + const maxLines = options.expanded ? lines.length : 20; + const displayLines = lines.slice(0, maxLines); + const remaining = lines.length - maxLines; + text += `\n${displayLines.map((line) => theme.fg("toolOutput", line)).join("\n")}`; + if (remaining > 0) { + text += `${theme.fg("muted", `\n... (${remaining} more lines,`)} ${keyHint("app.tools.expand", "to expand")}${theme.fg("muted", ")")}`; + } + } + + const entryLimit = result.details?.entryLimitReached; + const truncation = result.details?.truncation; + if (entryLimit || truncation?.truncated) { + const warnings: string[] = []; + if (entryLimit) warnings.push(`${entryLimit} entries limit`); + if (truncation?.truncated) warnings.push(`${formatSize(truncation.maxBytes ?? DEFAULT_MAX_BYTES)} limit`); + text += `\n${theme.fg("warning", `[Truncated: ${warnings.join(", ")}]`)}`; + } + return text; +} + +export const lsRenderers: Pick, "renderCall" | "renderResult"> = { + renderCall(args, theme, context) { + const text = (context.lastComponent as Text | undefined) ?? new Text("", 0, 0); + text.setText(formatLsCall(args as any, theme, context.cwd)); + return text; + }, + renderResult(result, options, theme, context) { + const text = (context.lastComponent as Text | undefined) ?? new Text("", 0, 0); + text.setText(formatLsResult(result as any, options, theme, context.showImages)); + return text; + }, +}; diff --git a/packages/coding-agent/src/core/tools/renderers/read.ts b/packages/coding-agent/src/core/tools/renderers/read.ts new file mode 100644 index 0000000000000000000000000000000000000000..2f5af21034a252a7d3bcd21ffca9268de6a9cf40 --- /dev/null +++ b/packages/coding-agent/src/core/tools/renderers/read.ts @@ -0,0 +1,175 @@ +/** + * Presentation for the read tool. + * + * Renderers live apart from the implementation so a process that only displays tool output does not + * load the execution path or its typebox parameter schema. `read.ts` spreads these into its + * definition, so the tool's public shape is unchanged. + */ + +import { basename, dirname, isAbsolute, relative, resolve as resolvePath, sep } from "node:path"; +import type { ImageContent, TextContent } from "@earendil-works/pi-ai"; +import { Text } from "@earendil-works/pi-tui"; +import { getReadmePath } from "../../../config.ts"; +import { keyHint, keyText } from "../../../modes/interactive/components/keybinding-hints.ts"; +import { getLanguageFromPath, highlightCode, type Theme } from "../../../modes/interactive/theme/theme.ts"; +import { formatPathRelativeToCwdOrAbsolute } from "../../../utils/paths.ts"; +import type { ToolDefinition, ToolRenderResultOptions } from "../../extensions/types.ts"; +import { resolveToCwd } from "../path-utils.ts"; +import type { ReadToolDetails } from "../read.ts"; +import { getTextOutput, renderToolPath, replaceTabs, str } from "../render-utils.ts"; +import { DEFAULT_MAX_BYTES, DEFAULT_MAX_LINES, formatSize } from "../truncate.ts"; + +interface CompactReadClassification { + kind: "docs" | "resource" | "skill"; + label: string; +} +const COMPACT_RESOURCE_FILE_NAMES = new Set(["AGENTS.override.md", "AGENTS.md", "AGENTS.MD", "CLAUDE.md", "CLAUDE.MD"]); +type ReadRenderArgs = { path?: string; file_path?: string; offset?: number; limit?: number }; +function formatReadLineRange(args: ReadRenderArgs | undefined, theme: Theme): string { + if (args?.offset === undefined && args?.limit === undefined) return ""; + const startLine = args.offset ?? 1; + const endLine = args.limit !== undefined ? startLine + args.limit - 1 : ""; + return theme.fg("warning", `:${startLine}${endLine ? `-${endLine}` : ""}`); +} +function formatReadCall(args: ReadRenderArgs | undefined, theme: Theme, cwd: string): string { + const pathDisplay = renderToolPath(str(args?.file_path ?? args?.path), theme, cwd); + return `${theme.fg("toolTitle", theme.bold("read"))} ${pathDisplay}${formatReadLineRange(args, theme)}`; +} +function trimTrailingEmptyLines(lines: string[]): string[] { + let end = lines.length; + while (end > 0 && lines[end - 1] === "") { + end--; + } + return lines.slice(0, end); +} +function toPosixPath(filePath: string): string { + return filePath.split(sep).join("/"); +} +function getPiDocsClassification(absolutePath: string): CompactReadClassification | undefined { + const packageRoot = dirname(getReadmePath()); + const relativePath = relative(resolvePath(packageRoot), resolvePath(absolutePath)); + if ( + relativePath === "" || + relativePath === ".." || + relativePath.startsWith(`..${sep}`) || + isAbsolute(relativePath) + ) { + return undefined; + } + + const label = toPosixPath(relativePath); + if (label === "README.md" || label.startsWith("docs/") || label.startsWith("examples/")) { + return { kind: "docs", label }; + } + return undefined; +} +function getCompactReadClassification( + args: ReadRenderArgs | undefined, + cwd: string, +): CompactReadClassification | undefined { + const rawPath = str(args?.file_path ?? args?.path); + if (!rawPath) return undefined; + + const absolutePath = resolveToCwd(rawPath, cwd); + const fileName = basename(absolutePath); + if (fileName === "SKILL.md") { + return { kind: "skill", label: basename(dirname(absolutePath)) || fileName }; + } + + const docsClassification = getPiDocsClassification(absolutePath); + if (docsClassification) return docsClassification; + + if (COMPACT_RESOURCE_FILE_NAMES.has(fileName)) { + return { kind: "resource", label: formatPathRelativeToCwdOrAbsolute(absolutePath, cwd) }; + } + + return undefined; +} +function formatCompactReadCall( + classification: CompactReadClassification, + args: ReadRenderArgs | undefined, + theme: Theme, +): string { + const expandHint = theme.fg("dim", ` (${keyText("app.tools.expand")} to expand)`); + if (classification.kind === "skill") { + return ( + theme.fg("customMessageLabel", `\x1b[1m[skill]\x1b[22m `) + + theme.fg("customMessageText", classification.label) + + formatReadLineRange(args, theme) + + expandHint + ); + } + + return ( + theme.fg("toolTitle", theme.bold(`read ${classification.kind}`)) + + " " + + theme.fg("accent", classification.label) + + formatReadLineRange(args, theme) + + expandHint + ); +} +function formatReadResult( + args: ReadRenderArgs | undefined, + result: { content: (TextContent | ImageContent)[]; details?: ReadToolDetails }, + options: ToolRenderResultOptions, + theme: Theme, + showImages: boolean, + _cwd: string, + isError: boolean, +): string { + if (!options.expanded && !isError) { + return ""; + } + + const rawPath = str(args?.file_path ?? args?.path); + const output = getTextOutput(result, showImages); + const lang = !isError && rawPath ? getLanguageFromPath(rawPath) : undefined; + const renderedLines = lang ? highlightCode(replaceTabs(output), lang) : output.split("\n"); + const lines = trimTrailingEmptyLines(renderedLines); + const maxLines = options.expanded ? lines.length : 10; + const displayLines = lines.slice(0, maxLines); + const remaining = lines.length - maxLines; + let text = `\n${displayLines.map((line) => (lang ? replaceTabs(line) : theme.fg("toolOutput", replaceTabs(line)))).join("\n")}`; + if (remaining > 0) { + text += `${theme.fg("muted", `\n... (${remaining} more lines,`)} ${keyHint("app.tools.expand", "to expand")}${theme.fg("muted", ")")}`; + } + + const truncation = result.details?.truncation; + if (truncation?.truncated) { + if (truncation.firstLineExceedsLimit) { + text += `\n${theme.fg("warning", `[First line exceeds ${formatSize(truncation.maxBytes ?? DEFAULT_MAX_BYTES)} limit]`)}`; + } else if (truncation.truncatedBy === "lines") { + text += `\n${theme.fg("warning", `[Truncated: showing ${truncation.outputLines} of ${truncation.totalLines} lines (${truncation.maxLines ?? DEFAULT_MAX_LINES} line limit)]`)}`; + } else { + text += `\n${theme.fg("warning", `[Truncated: ${truncation.outputLines} lines shown (${formatSize(truncation.maxBytes ?? DEFAULT_MAX_BYTES)} limit)]`)}`; + } + } + return text; +} + +export const readRenderers: Pick, "renderCall" | "renderResult"> = { + renderCall(rawArgs, theme, context) { + const args = rawArgs as ReadRenderArgs | undefined; + const text = (context.lastComponent as Text | undefined) ?? new Text("", 0, 0); + const classification = !context.expanded ? getCompactReadClassification(args, context.cwd) : undefined; + text.setText( + classification ? formatCompactReadCall(classification, args, theme) : formatReadCall(args, theme, context.cwd), + ); + return text; + }, + renderResult(result, options, theme, context) { + const text = (context.lastComponent as Text | undefined) ?? new Text("", 0, 0); + text.setText( + formatReadResult( + context.args as ReadRenderArgs | undefined, + result, + options, + theme, + context.showImages, + context.cwd, + context.isError, + ), + ); + return text; + }, +}; diff --git a/packages/coding-agent/src/core/tools/renderers/write.ts b/packages/coding-agent/src/core/tools/renderers/write.ts new file mode 100644 index 0000000000000000000000000000000000000000..7c8f32636ce356c64fdd1298b713bad8c196438c --- /dev/null +++ b/packages/coding-agent/src/core/tools/renderers/write.ts @@ -0,0 +1,181 @@ +/** + * Presentation for the write tool. + * + * Renderers live apart from the implementation so a process that only displays tool output does not + * load the execution path or its typebox parameter schema. `write.ts` spreads these into its + * definition, so the tool's public shape is unchanged. + */ + +import { Container, Text } from "@earendil-works/pi-tui"; +import { keyHint } from "../../../modes/interactive/components/keybinding-hints.ts"; +import { getLanguageFromPath, highlightCode, type Theme } from "../../../modes/interactive/theme/theme.ts"; +import type { ToolDefinition, ToolRenderResultOptions } from "../../extensions/types.ts"; +import { normalizeDisplayText, renderToolPath, replaceTabs, str } from "../render-utils.ts"; + +type WriteHighlightCache = { + rawPath: string | null; + lang: string; + rawContent: string; + normalizedLines: string[]; + highlightedLines: string[]; +}; +class WriteCallRenderComponent extends Text { + cache?: WriteHighlightCache; + + constructor() { + super("", 0, 0); + } +} +const WRITE_PARTIAL_FULL_HIGHLIGHT_LINES = 50; +function highlightSingleLine(line: string, lang: string): string { + const highlighted = highlightCode(line, lang); + return highlighted[0] ?? ""; +} +function refreshWriteHighlightPrefix(cache: WriteHighlightCache): void { + const prefixCount = Math.min(WRITE_PARTIAL_FULL_HIGHLIGHT_LINES, cache.normalizedLines.length); + if (prefixCount === 0) return; + const prefixSource = cache.normalizedLines.slice(0, prefixCount).join("\n"); + const prefixHighlighted = highlightCode(prefixSource, cache.lang); + for (let i = 0; i < prefixCount; i++) { + cache.highlightedLines[i] = + prefixHighlighted[i] ?? highlightSingleLine(cache.normalizedLines[i] ?? "", cache.lang); + } +} +function rebuildWriteHighlightCacheFull(rawPath: string | null, fileContent: string): WriteHighlightCache | undefined { + const lang = rawPath ? getLanguageFromPath(rawPath) : undefined; + if (!lang) return undefined; + const displayContent = normalizeDisplayText(fileContent); + const normalized = replaceTabs(displayContent); + return { + rawPath, + lang, + rawContent: fileContent, + normalizedLines: normalized.split("\n"), + highlightedLines: highlightCode(normalized, lang), + }; +} +function updateWriteHighlightCacheIncremental( + cache: WriteHighlightCache | undefined, + rawPath: string | null, + fileContent: string, +): WriteHighlightCache | undefined { + const lang = rawPath ? getLanguageFromPath(rawPath) : undefined; + if (!lang) return undefined; + if (!cache) return rebuildWriteHighlightCacheFull(rawPath, fileContent); + if (cache.lang !== lang || cache.rawPath !== rawPath) return rebuildWriteHighlightCacheFull(rawPath, fileContent); + if (!fileContent.startsWith(cache.rawContent)) return rebuildWriteHighlightCacheFull(rawPath, fileContent); + if (fileContent.length === cache.rawContent.length) return cache; + + const deltaRaw = fileContent.slice(cache.rawContent.length); + const deltaDisplay = normalizeDisplayText(deltaRaw); + const deltaNormalized = replaceTabs(deltaDisplay); + cache.rawContent = fileContent; + if (cache.normalizedLines.length === 0) { + cache.normalizedLines.push(""); + cache.highlightedLines.push(""); + } + + const segments = deltaNormalized.split("\n"); + const lastIndex = cache.normalizedLines.length - 1; + cache.normalizedLines[lastIndex] += segments[0]; + cache.highlightedLines[lastIndex] = highlightSingleLine(cache.normalizedLines[lastIndex], cache.lang); + for (let i = 1; i < segments.length; i++) { + cache.normalizedLines.push(segments[i]); + cache.highlightedLines.push(highlightSingleLine(segments[i], cache.lang)); + } + refreshWriteHighlightPrefix(cache); + return cache; +} +function trimTrailingEmptyLines(lines: string[]): string[] { + let end = lines.length; + while (end > 0 && lines[end - 1] === "") { + end--; + } + return lines.slice(0, end); +} +function formatWriteCall( + args: { path?: string; file_path?: string; content?: string } | undefined, + options: ToolRenderResultOptions, + theme: Theme, + cache: WriteHighlightCache | undefined, + cwd: string, +): string { + const rawPath = str(args?.file_path ?? args?.path); + const fileContent = str(args?.content); + const pathDisplay = renderToolPath(rawPath, theme, cwd); + let text = `${theme.fg("toolTitle", theme.bold("write"))} ${pathDisplay}`; + + if (fileContent === null) { + text += `\n\n${theme.fg("error", "[invalid content arg - expected string]")}`; + } else if (fileContent) { + const lang = rawPath ? getLanguageFromPath(rawPath) : undefined; + const renderedLines = lang + ? (cache?.highlightedLines ?? highlightCode(replaceTabs(normalizeDisplayText(fileContent)), lang)) + : normalizeDisplayText(fileContent).split("\n"); + const lines = trimTrailingEmptyLines(renderedLines); + const totalLines = lines.length; + const maxLines = options.expanded ? lines.length : 10; + const displayLines = lines.slice(0, maxLines); + const remaining = lines.length - maxLines; + text += `\n\n${displayLines.map((line) => (lang ? line : theme.fg("toolOutput", replaceTabs(line)))).join("\n")}`; + if (remaining > 0) { + text += `${theme.fg("muted", `\n... (${remaining} more lines, ${totalLines} total,`)} ${keyHint("app.tools.expand", "to expand")}${theme.fg("muted", ")")}`; + } + } + + return text; +} +function formatWriteResult( + result: { content: Array<{ type: string; text?: string; data?: string; mimeType?: string }>; isError?: boolean }, + theme: Theme, +): string | undefined { + if (!result.isError) { + return undefined; + } + const output = result.content + .filter((c) => c.type === "text") + .map((c) => c.text || "") + .join("\n"); + if (!output) { + return undefined; + } + return `\n${theme.fg("error", output)}`; +} + +export const writeRenderers: Pick, "renderCall" | "renderResult"> = { + renderCall(args, theme, context) { + const renderArgs = args as { path?: string; file_path?: string; content?: string } | undefined; + const rawPath = str(renderArgs?.file_path ?? renderArgs?.path); + const fileContent = str(renderArgs?.content); + const component = + (context.lastComponent as WriteCallRenderComponent | undefined) ?? new WriteCallRenderComponent(); + if (fileContent !== null) { + component.cache = context.argsComplete + ? rebuildWriteHighlightCacheFull(rawPath, fileContent) + : updateWriteHighlightCacheIncremental(component.cache, rawPath, fileContent); + } else { + component.cache = undefined; + } + component.setText( + formatWriteCall( + renderArgs, + { expanded: context.expanded, isPartial: context.isPartial }, + theme, + component.cache, + context.cwd, + ), + ); + return component; + }, + renderResult(result, _options, theme, context) { + const output = formatWriteResult({ ...result, isError: context.isError }, theme); + if (!output) { + const component = (context.lastComponent as Container | undefined) ?? new Container(); + component.clear(); + return component; + } + const text = (context.lastComponent as Text | undefined) ?? new Text("", 0, 0); + text.setText(output); + return text; + }, +}; diff --git a/packages/coding-agent/src/core/tools/tool-definition-wrapper.ts b/packages/coding-agent/src/core/tools/tool-definition-wrapper.ts new file mode 100644 index 0000000000000000000000000000000000000000..ab01a0657177d11fe9f16a55b9dd884c921640ed --- /dev/null +++ b/packages/coding-agent/src/core/tools/tool-definition-wrapper.ts @@ -0,0 +1,47 @@ +import type { AgentTool } from "@earendil-works/pi-agent-core"; +import type { ExtensionContext, ToolDefinition } from "../extensions/types.ts"; + +/** Wrap a ToolDefinition into an AgentTool for the core runtime. */ +export function wrapToolDefinition( + definition: ToolDefinition, + ctxFactory?: () => ExtensionContext, +): AgentTool { + return { + name: definition.name, + label: definition.label, + description: definition.description, + parameters: definition.parameters, + constrainedSampling: definition.constrainedSampling, + prepareArguments: definition.prepareArguments, + executionMode: definition.executionMode, + execute: (toolCallId, params, signal, onUpdate, ctx?: ExtensionContext) => + definition.execute(toolCallId, params, signal, onUpdate, ctx ?? (ctxFactory?.() as ExtensionContext)), + }; +} + +/** Wrap multiple ToolDefinitions into AgentTools for the core runtime. */ +export function wrapToolDefinitions( + definitions: ToolDefinition[], + ctxFactory?: () => ExtensionContext, +): AgentTool[] { + return definitions.map((definition) => wrapToolDefinition(definition, ctxFactory)); +} + +/** + * Synthesize a minimal ToolDefinition from an AgentTool. + * + * This keeps AgentSession's internal registry definition-first even when a caller + * provides plain AgentTool overrides that do not include prompt metadata or renderers. + */ +export function createToolDefinitionFromAgentTool(tool: AgentTool): ToolDefinition { + return { + name: tool.name, + label: tool.label, + description: tool.description, + parameters: tool.parameters as any, + constrainedSampling: tool.constrainedSampling, + prepareArguments: tool.prepareArguments, + executionMode: tool.executionMode, + execute: async (toolCallId, params, signal, onUpdate) => tool.execute(toolCallId, params, signal, onUpdate), + }; +} diff --git a/packages/coding-agent/src/core/tools/truncate.ts b/packages/coding-agent/src/core/tools/truncate.ts new file mode 100644 index 0000000000000000000000000000000000000000..c638ee48027b28da6f02a433f8bacfba52277f7f --- /dev/null +++ b/packages/coding-agent/src/core/tools/truncate.ts @@ -0,0 +1,276 @@ +/** + * Shared truncation utilities for tool outputs. + * + * Truncation is based on two independent limits - whichever is hit first wins: + * - Line limit (default: 2000 lines) + * - Byte limit (default: 50KB) + * + * Never returns partial lines (except bash tail truncation edge case). + */ + +export const DEFAULT_MAX_LINES = 2000; +export const DEFAULT_MAX_BYTES = 50 * 1024; // 50KB +export const GREP_MAX_LINE_LENGTH = 500; // Max chars per grep match line + +export interface TruncationResult { + /** The truncated content */ + content: string; + /** Whether truncation occurred */ + truncated: boolean; + /** Which limit was hit: "lines", "bytes", or null if not truncated */ + truncatedBy: "lines" | "bytes" | null; + /** Total number of lines in the original content */ + totalLines: number; + /** Total number of bytes in the original content */ + totalBytes: number; + /** Number of complete lines in the truncated output */ + outputLines: number; + /** Number of bytes in the truncated output */ + outputBytes: number; + /** Whether the last line was partially truncated (only for tail truncation edge case) */ + lastLinePartial: boolean; + /** Whether the first line exceeded the byte limit (for head truncation) */ + firstLineExceedsLimit: boolean; + /** The max lines limit that was applied */ + maxLines: number; + /** The max bytes limit that was applied */ + maxBytes: number; +} + +export interface TruncationOptions { + /** Maximum number of lines (default: 2000) */ + maxLines?: number; + /** Maximum number of bytes (default: 50KB) */ + maxBytes?: number; +} + +function splitLinesForCounting(content: string): string[] { + if (content.length === 0) { + return []; + } + const lines = content.split("\n"); + if (content.endsWith("\n")) { + lines.pop(); + } + return lines; +} + +/** + * Format bytes as human-readable size. + */ +export function formatSize(bytes: number): string { + if (bytes < 1024) { + return `${bytes}B`; + } else if (bytes < 1024 * 1024) { + return `${(bytes / 1024).toFixed(1)}KB`; + } else { + return `${(bytes / (1024 * 1024)).toFixed(1)}MB`; + } +} + +/** + * Truncate content from the head (keep first N lines/bytes). + * Suitable for file reads where you want to see the beginning. + * + * Never returns partial lines. If first line exceeds byte limit, + * returns empty content with firstLineExceedsLimit=true. + */ +export function truncateHead(content: string, options: TruncationOptions = {}): TruncationResult { + const maxLines = options.maxLines ?? DEFAULT_MAX_LINES; + const maxBytes = options.maxBytes ?? DEFAULT_MAX_BYTES; + + const totalBytes = Buffer.byteLength(content, "utf-8"); + const lines = splitLinesForCounting(content); + const totalLines = lines.length; + + // Check if no truncation needed + if (totalLines <= maxLines && totalBytes <= maxBytes) { + return { + content, + truncated: false, + truncatedBy: null, + totalLines, + totalBytes, + outputLines: totalLines, + outputBytes: totalBytes, + lastLinePartial: false, + firstLineExceedsLimit: false, + maxLines, + maxBytes, + }; + } + + // Check if first line alone exceeds byte limit + const firstLineBytes = Buffer.byteLength(lines[0], "utf-8"); + if (firstLineBytes > maxBytes) { + return { + content: "", + truncated: true, + truncatedBy: "bytes", + totalLines, + totalBytes, + outputLines: 0, + outputBytes: 0, + lastLinePartial: false, + firstLineExceedsLimit: true, + maxLines, + maxBytes, + }; + } + + // Collect complete lines that fit + const outputLinesArr: string[] = []; + let outputBytesCount = 0; + let truncatedBy: "lines" | "bytes" = "lines"; + + for (let i = 0; i < lines.length && i < maxLines; i++) { + const line = lines[i]; + const lineBytes = Buffer.byteLength(line, "utf-8") + (i > 0 ? 1 : 0); // +1 for newline + + if (outputBytesCount + lineBytes > maxBytes) { + truncatedBy = "bytes"; + break; + } + + outputLinesArr.push(line); + outputBytesCount += lineBytes; + } + + // If we exited due to line limit + if (outputLinesArr.length >= maxLines && outputBytesCount <= maxBytes) { + truncatedBy = "lines"; + } + + const outputContent = outputLinesArr.join("\n"); + const finalOutputBytes = Buffer.byteLength(outputContent, "utf-8"); + + return { + content: outputContent, + truncated: true, + truncatedBy, + totalLines, + totalBytes, + outputLines: outputLinesArr.length, + outputBytes: finalOutputBytes, + lastLinePartial: false, + firstLineExceedsLimit: false, + maxLines, + maxBytes, + }; +} + +/** + * Truncate content from the tail (keep last N lines/bytes). + * Suitable for bash output where you want to see the end (errors, final results). + * + * May return partial first line if the last line of original content exceeds byte limit. + */ +export function truncateTail(content: string, options: TruncationOptions = {}): TruncationResult { + const maxLines = options.maxLines ?? DEFAULT_MAX_LINES; + const maxBytes = options.maxBytes ?? DEFAULT_MAX_BYTES; + + const totalBytes = Buffer.byteLength(content, "utf-8"); + const lines = splitLinesForCounting(content); + const totalLines = lines.length; + + // Check if no truncation needed + if (totalLines <= maxLines && totalBytes <= maxBytes) { + return { + content, + truncated: false, + truncatedBy: null, + totalLines, + totalBytes, + outputLines: totalLines, + outputBytes: totalBytes, + lastLinePartial: false, + firstLineExceedsLimit: false, + maxLines, + maxBytes, + }; + } + + // Work backwards from the end + const outputLinesArr: string[] = []; + let outputBytesCount = 0; + let truncatedBy: "lines" | "bytes" = "lines"; + let lastLinePartial = false; + + for (let i = lines.length - 1; i >= 0 && outputLinesArr.length < maxLines; i--) { + const line = lines[i]; + const lineBytes = Buffer.byteLength(line, "utf-8") + (outputLinesArr.length > 0 ? 1 : 0); // +1 for newline + + if (outputBytesCount + lineBytes > maxBytes) { + truncatedBy = "bytes"; + // Edge case: if we haven't added ANY lines yet and this line exceeds maxBytes, + // take the end of the line (partial) + if (outputLinesArr.length === 0) { + const truncatedLine = truncateStringToBytesFromEnd(line, maxBytes); + outputLinesArr.unshift(truncatedLine); + outputBytesCount = Buffer.byteLength(truncatedLine, "utf-8"); + lastLinePartial = true; + } + break; + } + + outputLinesArr.unshift(line); + outputBytesCount += lineBytes; + } + + // If we exited due to line limit + if (outputLinesArr.length >= maxLines && outputBytesCount <= maxBytes) { + truncatedBy = "lines"; + } + + const outputContent = outputLinesArr.join("\n"); + const finalOutputBytes = Buffer.byteLength(outputContent, "utf-8"); + + return { + content: outputContent, + truncated: true, + truncatedBy, + totalLines, + totalBytes, + outputLines: outputLinesArr.length, + outputBytes: finalOutputBytes, + lastLinePartial, + firstLineExceedsLimit: false, + maxLines, + maxBytes, + }; +} + +/** + * Truncate a string to fit within a byte limit (from the end). + * Handles multi-byte UTF-8 characters correctly. + */ +function truncateStringToBytesFromEnd(str: string, maxBytes: number): string { + const buf = Buffer.from(str, "utf-8"); + if (buf.length <= maxBytes) { + return str; + } + + // Start from the end, skip maxBytes back + let start = buf.length - maxBytes; + + // Find a valid UTF-8 boundary (start of a character) + while (start < buf.length && (buf[start] & 0xc0) === 0x80) { + start++; + } + + return buf.slice(start).toString("utf-8"); +} + +/** + * Truncate a single line to max characters, adding [truncated] suffix. + * Used for grep match lines. + */ +export function truncateLine( + line: string, + maxChars: number = GREP_MAX_LINE_LENGTH, +): { text: string; wasTruncated: boolean } { + if (line.length <= maxChars) { + return { text: line, wasTruncated: false }; + } + return { text: `${line.slice(0, maxChars)}... [truncated]`, wasTruncated: true }; +} diff --git a/packages/coding-agent/src/core/tools/write.ts b/packages/coding-agent/src/core/tools/write.ts new file mode 100644 index 0000000000000000000000000000000000000000..374089017d3aecbad3657d50272f054d39a88118 --- /dev/null +++ b/packages/coding-agent/src/core/tools/write.ts @@ -0,0 +1,97 @@ +import type { AgentTool } from "@earendil-works/pi-agent-core"; +import { mkdir as fsMkdir, writeFile as fsWriteFile } from "fs/promises"; +import { dirname } from "path"; +import { type Static, Type } from "typebox"; +import type { ExtensionContext, ToolDefinition } from "../extensions/types.ts"; +import { withFileMutationQueue } from "./file-mutation-queue.ts"; +import { resolveToCwd } from "./path-utils.ts"; +import { writeRenderers } from "./renderers/write.ts"; +import { wrapToolDefinition } from "./tool-definition-wrapper.ts"; + +const writeSchema = Type.Object({ + path: Type.String({ description: "Path to the file to write (relative or absolute)" }), + content: Type.String({ description: "Content to write to the file" }), +}); + +export const writeToolSystemPromptContribution = { + snippet: "Create or overwrite files", + guidelines: ["Use write only for new files or complete rewrites."], +} as const; + +export type WriteToolInput = Static; + +/** + * Pluggable operations for the write tool. + * Override these to delegate file writing to remote systems (for example SSH). + */ +export interface WriteOperations { + /** Write content to a file */ + writeFile: (absolutePath: string, content: string) => Promise; + /** Create directory recursively */ + mkdir: (dir: string) => Promise; +} + +const defaultWriteOperations: WriteOperations = { + writeFile: (path, content) => fsWriteFile(path, content, "utf-8"), + mkdir: (dir) => fsMkdir(dir, { recursive: true }).then(() => {}), +}; + +export interface WriteToolOptions { + /** Custom operations for file writing. Default: local filesystem */ + operations?: WriteOperations; +} + +export function createWriteToolDefinition( + cwd: string, + options?: WriteToolOptions, +): ToolDefinition { + const ops = options?.operations ?? defaultWriteOperations; + return { + name: "write", + label: "write", + description: + "Write content to a file. Creates the file if it doesn't exist, overwrites if it does. Automatically creates parent directories.", + promptSnippet: writeToolSystemPromptContribution.snippet, + promptGuidelines: [...writeToolSystemPromptContribution.guidelines], + parameters: writeSchema, + constrainedSampling: { type: "json_schema", strict: "prefer" }, + async execute( + _toolCallId, + { path, content }: { path: string; content: string }, + signal?: AbortSignal, + _onUpdate?, + ctx?: ExtensionContext, + ) { + const absolutePath = resolveToCwd(path, ctx?.cwd || cwd); + const dir = dirname(absolutePath); + return withFileMutationQueue(absolutePath, async () => { + // Do not reject from an abort event listener here: that would release the + // mutation queue while an in-flight filesystem operation may still finish. + // Checking signal.aborted after each await observes the same aborts while + // keeping the queue locked until the current operation has settled. + const throwIfAborted = (): void => { + if (signal?.aborted) throw new Error("Operation aborted"); + }; + + throwIfAborted(); + // Create parent directories if needed. + await ops.mkdir(dir); + throwIfAborted(); + + // Write the file contents. + await ops.writeFile(absolutePath, content); + throwIfAborted(); + + return { + content: [{ type: "text", text: `Successfully wrote to ${path}` }], + details: undefined, + }; + }); + }, + ...writeRenderers, + }; +} + +export function createWriteTool(cwd: string, options?: WriteToolOptions): AgentTool { + return wrapToolDefinition(createWriteToolDefinition(cwd, options)); +} diff --git a/packages/coding-agent/src/experimental/mini/README.md b/packages/coding-agent/src/experimental/mini/README.md new file mode 100644 index 0000000000000000000000000000000000000000..7552e7b5197b1e2b484fcd95b1ac8b67f910cda8 --- /dev/null +++ b/packages/coding-agent/src/experimental/mini/README.md @@ -0,0 +1,107 @@ +# mini + +A minimal coding agent on the durable `AgentHarness`, split across three processes that talk JSON +over a socket and a pipe. It exists to exercise the harness from a real client and to find out what +an RPC-shaped presentation actually needs from it. + +```bash +node packages/coding-agent/src/experimental/mini/main.ts [--continue] + +# while the harness is changing under us, run from source instead of built dist: +./node_modules/.bin/tsx --tsconfig tsconfig.json packages/coding-agent/src/experimental/mini/main.ts +``` + +`--continue` attaches to the newest session for the current directory. Starting `mini` twice attaches +two presentations to the same session; both see the same transcript, live. + +## Topology + +```text +tui tui tui presentations: render, no agent state + \ | / + \ | / unix socket, ~/.pi/agent/experimental/mini.sock + \ | / + server routes calls, fans out events, spawns workers + / \ + / \ child process stdio pipes + worker worker one per session: harness, storage, model runtime +``` + +A presentation holds one connection and reaches every service through it. The server answers +`sessions` itself and forwards anything else to the worker that connection is attached to, so the TUI +never learns which host answers what. The same rule runs backwards: a worker calling `sessions.list` +is answered by the server. + +The first `mini` to start spawns the server detached. The server kills a worker when its last +presentation disconnects, and retires itself ten seconds after the last presentation leaves, so the +next start always runs current code. If that worker held an open durable operation, its replacement +automatically resumes the operation from the last recorded recovery state. + +## Layout + +| path | role | +| --- | --- | +| `main.ts` | the `mini` command | +| `shared/transport.ts` | `Connection`: newline-delimited JSON over any duplex pair | +| `shared/rpc.ts` | frames, named services, routing, liveness, cancellation | +| `shared/protocol.ts` | service tokens, contracts, wire types | +| `server/run.ts` | routes, worker supervision, event fan-out | +| `worker/run.ts` | opens the session, builds the harness, provides services | +| `worker/lane-service.ts` | `Lane`: watch subscriptions and lane commands | +| `worker/models-service.ts` | `Models`: catalog, accounts, interactive login | +| `tui/run.ts` | presentation host: ensure server, attach, run the view | +| `tui/session.ts` | attach, subscribe, hold the replicated snapshot | +| `tui/view.ts` | alt-screen rendering, reusing interactive-mode components | + +## RPC + +Two verbs. **Call** asks a question and gets one answer; **emit** publishes to whoever listens. Both +directions on every connection. + +```ts +const peer = createPeer(connection, { forward }); // forward handles names this peer lacks +peer.provide(Lane, laneService); // register and announce +const lane = peer.use(Lane); // Remote +await lane.prompt("hi"); // -> { kind: "call", id, method: "lane.prompt" } +peer.on(Lane, (event) => fold(event)); // typed by the token +``` + +The wire is six frame kinds: `call`, `result`, `error`, `cancel`, `event`, `announce`, `ping`. +Replies correlate by `id` and resolve one promise; events carry a service name and no id, so the two +never interfere — a five-minute `lane.prompt` sits pending while its stream of events flows past. + +Services are plain objects registered under a token, and a token carries the name plus the call and +event types, so `provide` checks the implementation and `use` checks the caller. Names are announced +on registration, so routing is a lookup and an unroutable call fails with both inventories named. + +Failure handling: a closed connection rejects every pending call and aborts every in-flight handler; +peers ping every five seconds and declare a silent line dead after fifteen, which catches a wedged +process that never closes; callers may pass a `signal` (sends `cancel`) or a `timeoutMs`. Timeouts +are opt-in per call site, because an agent turn has no bounded duration. + +## State replication + +The worker holds every live object. A presentation holds a `LaneSnapshot` and nothing else. + +Each presentation gets **its own** `lane.watch()` in the worker, in the harness's two phases: +`watch()` captures a snapshot while the harness buffers that subscription's events, and `start()` +drains them. Because the harness pairs snapshot and stream with no gap and no duplicate, the client +needs no buffering or ordering logic of its own — it applies events with the harness's own +`reduceLaneSnapshot`. When the fold answers `{ rebase: true }`, which a completed navigation does, the +presentation takes a fresh subscription and drops the old one. First attach and rebase are the same +function. + +Rendering is a pure function of that snapshot. Transcript entries are keyed by id and appended, so a +streamed token costs one markdown rebuild rather than a repaint; the view repaints wholesale only +when the entry-id prefix diverges, which is what compaction and navigation do. + +## What it is not + +No slash-command system, extensions, skills, hooks, themes beyond the shared one, session picker, or +tree navigation. Interactive login and model selection exist because they proved the awkward +directions of the protocol: server-to-client requests, and identity-based configuration. + +Two known shortcuts: the server broadcasts a worker's events to every attached presentation, so with +N presentations each event crosses N² times and each client discards the copies that are not its own; +and cancelling an interactive login only interrupts at a prompt, since the worker's login has no +abort path yet. diff --git a/packages/coding-agent/src/experimental/mini/main.ts b/packages/coding-agent/src/experimental/mini/main.ts new file mode 100644 index 0000000000000000000000000000000000000000..bfeac920d2f97ee83d2e8d9d1e04c9c76882d373 --- /dev/null +++ b/packages/coding-agent/src/experimental/mini/main.ts @@ -0,0 +1,24 @@ +/** + * The `mini` command. Runs the presentation host. + * + * Run: node packages/coding-agent/src/experimental/mini/main.ts [--continue] + */ + +import { runTui, type TuiOptions } from "./tui/run.ts"; + +function parseArgs(argv: readonly string[]): TuiOptions { + const options: TuiOptions = {}; + for (const arg of argv) { + if (arg === "--continue" || arg === "-c") options.continueSession = true; + else throw new Error(`Unknown argument: ${arg}`); + } + return options; +} + +runTui(parseArgs(process.argv.slice(2))).then( + () => process.exit(0), + (error: unknown) => { + console.error(error); + process.exit(1); + }, +); diff --git a/packages/coding-agent/src/experimental/mini/server/entry.ts b/packages/coding-agent/src/experimental/mini/server/entry.ts new file mode 100644 index 0000000000000000000000000000000000000000..2d6d4301604941a0c2f1208e9e3e905d48b73d10 --- /dev/null +++ b/packages/coding-agent/src/experimental/mini/server/entry.ts @@ -0,0 +1,12 @@ +/** Server process entry: `node server/entry.ts `. Spawned detached by the CLI. */ + +import { socketTransport } from "../shared/transport.ts"; +import { runServer } from "./run.ts"; + +const [socketPath, sessionsRoot] = process.argv.slice(2); +if (!socketPath || !sessionsRoot) throw new Error("Server requires "); + +void runServer({ transport: socketTransport(socketPath), sessionsRoot }).catch((error: unknown) => { + console.error(error); + process.exit(1); +}); diff --git a/packages/coding-agent/src/experimental/mini/server/run.ts b/packages/coding-agent/src/experimental/mini/server/run.ts new file mode 100644 index 0000000000000000000000000000000000000000..5b505071c0dcec680545f9dbdac3c7b6a96acf76 --- /dev/null +++ b/packages/coding-agent/src/experimental/mini/server/run.ts @@ -0,0 +1,160 @@ +/** + * Session server: accepts client connections, spawns one worker process per session, routes. + * + * It provides `Sessions` and holds no agent state. Any other service name is forwarded to the worker + * the calling client is attached to, and every worker event is pushed back to that worker's clients. + * Workers reach `Sessions` over the same peer, because the routing rule is symmetric. + */ + +import { spawn } from "node:child_process"; +import { extname } from "node:path"; +import { fileURLToPath } from "node:url"; +// Narrow entries: the server routes and lists sessions, it never runs an agent. +import { BACKGROUND_CONTEXT } from "@earendil-works/pi-agent-core/harness/context"; +import { NodeExecutionEnv } from "@earendil-works/pi-agent-core/harness/env/nodejs"; +import { JsonlSessionRepo } from "@earendil-works/pi-agent-core/harness/session"; +import { type SessionSummary, Sessions, type SessionsServiceApi, Worker } from "../shared/protocol.ts"; +import { createPeer, type RpcPeer } from "../shared/rpc.ts"; +import { childConnection, type Transport } from "../shared/transport.ts"; + +const SELF_EXTENSION = extname(fileURLToPath(import.meta.url)); +const WORKER_ENTRY = fileURLToPath(new URL(`../worker/entry${SELF_EXTENSION}`, import.meta.url)); +const WORKER_START_TIMEOUT_MS = 30_000; +/** Grace period before an idle server retires, so a reconnecting presentation does not race it. */ +const IDLE_SHUTDOWN_MS = 10_000; + +/** The server's routing entry for one session worker process. The worker knows none of this. */ +interface Route { + sessionId: string; + /** Peer to the worker process. */ + worker: RpcPeer; + /** Attached presentations by id, so an addressed event goes to exactly one of them. */ + subscribers: Map; + stop(): void; +} + +async function listSessions(sessionsRoot: string): Promise { + const executionEnv = new NodeExecutionEnv({ cwd: process.cwd() }); + const repo = new JsonlSessionRepo({ fileSystem: executionEnv, sessionsRoot }); + try { + return (await repo.list(undefined, BACKGROUND_CONTEXT)).map((metadata) => ({ + id: metadata.id, + path: metadata.path, + cwd: metadata.cwd, + createdAt: metadata.createdAt, + })); + } finally { + await repo.close(BACKGROUND_CONTEXT); + await executionEnv.cleanup(BACKGROUND_CONTEXT); + } +} + +export async function runServer(options: { transport: Transport; sessionsRoot: string }): Promise { + const routes = new Map(); + let presentations = 0; + let retire = (): void => {}; + const retired = new Promise((resolve) => { + retire = resolve; + }); + let idleTimer: NodeJS.Timeout | undefined; + /** Nothing to serve and nothing running: leave, so the next start always gets current code. */ + const considerRetiring = (): void => { + if (idleTimer) clearTimeout(idleTimer); + if (presentations > 0 || routes.size > 0) return; + idleTimer = setTimeout(() => { + if (presentations === 0 && routes.size === 0) retire(); + }, IDLE_SHUTDOWN_MS); + idleTimer.unref(); + }; + /** Concurrent attaches to one session must share a worker: two writers would corrupt the session. */ + const spawning = new Map>(); + + const spawnWorker = async (sessionId: string | undefined, cwd: string): Promise => { + const args = [...process.execArgv, WORKER_ENTRY, options.sessionsRoot, cwd, ...(sessionId ? [sessionId] : [])]; + const child = spawn(process.execPath, args, { stdio: ["pipe", "pipe", "inherit"] }); + const connection = childConnection(child); + const peer = createPeer(connection); + // Workers consume `Sessions` through this same peer. + peer.provide(Sessions, { list, attach: attachUnsupported }); + // A worker that never answers must not wedge the attach that spawned it. + const described = await peer.use(Worker, { timeoutMs: WORKER_START_TIMEOUT_MS }).describe(); + const route: Route = { + sessionId: described.sessionId, + worker: peer, + subscribers: new Map(), + stop: () => child.kill(), + }; + // Routing is the server's job. An addressed event reaches one presentation; the rest are shared. + peer.onEvent((service, payload, to) => { + if (to !== undefined) { + route.subscribers.get(to)?.emitRaw(service, payload); + return; + } + for (const subscriber of route.subscribers.values()) subscriber.emitRaw(service, payload); + }); + peer.onClose(() => { + routes.delete(route.sessionId); + considerRetiring(); + }); + routes.set(route.sessionId, route); + return route; + }; + + const ensureRoute = (sessionId: string | null, cwd: string): Promise => { + if (sessionId === null) return spawnWorker(undefined, cwd); + const existing = routes.get(sessionId); + if (existing) return Promise.resolve(existing); + const pending = spawning.get(sessionId); + if (pending) return pending; + const started = spawnWorker(sessionId, cwd).finally(() => spawning.delete(sessionId)); + spawning.set(sessionId, started); + return started; + }; + + const list = (): Promise => listSessions(options.sessionsRoot); + const attachUnsupported = async (): Promise => { + throw new Error("Only presentations attach to sessions"); + }; + + const listener = await options.transport.listen((connection) => { + presentations += 1; + let route: Route | undefined; + let attachedAs: string | undefined; + const sessions: SessionsServiceApi = { + list, + attach: async (sessionId, cwd, presentationId) => { + route?.subscribers.delete(attachedAs ?? ""); + attachedAs = presentationId; + route = await ensureRoute(sessionId, cwd); + route.subscribers.set(presentationId, presentation); + return route.sessionId; + }, + }; + const presentation: RpcPeer = createPeer(connection, { + forward: (method, args) => { + if (!route) throw new Error("Not attached to a session"); + const service = method.slice(0, method.indexOf(".")); + if (!route.worker.announced.has(service)) { + throw new Error( + `No host provides ${service}: server has [${[...presentation.provided]}], worker has [${[...route.worker.announced]}]`, + ); + } + return route.worker.call(method, ...args); + }, + }); + presentation.provide(Sessions, sessions); + connection.onClose(() => { + presentations -= 1; + if (route && attachedAs !== undefined) { + route.subscribers.delete(attachedAs); + // One worker per session, kept alive only while someone is looking at it. + if (route.subscribers.size === 0) route.stop(); + } + considerRetiring(); + }); + }); + + considerRetiring(); + await retired; + await listener.close(); +} diff --git a/packages/coding-agent/src/experimental/mini/shared/protocol.ts b/packages/coding-agent/src/experimental/mini/shared/protocol.ts new file mode 100644 index 0000000000000000000000000000000000000000..06db19bee4cb69051c7048a94909ee0dc5aa664d --- /dev/null +++ b/packages/coding-agent/src/experimental/mini/shared/protocol.ts @@ -0,0 +1,138 @@ +/** Service contracts and everything that crosses the wire. */ + +import type { HarnessEvent, LaneSnapshot } from "@earendil-works/pi-agent-core"; +import type { AuthEvent, AuthPrompt } from "@earendil-works/pi-ai"; + +/** Commands answer with data, never exceptions, exactly like a remote call would. */ +export type CommandResult = { ok: true } | { ok: false; error: string }; + +/** Durable model identity. Nothing outside a worker holds a `Model` object. */ +export interface ModelRef { + provider: string; + modelId: string; +} + +export interface ModelSummary extends ModelRef { + name: string; +} + +export interface ProviderAccount { + id: string; + name: string; + authType: "oauth" | "api_key"; + configured: boolean; + /** Where the credential came from, for display: "stored", "environment", an env var name. */ + source?: string; + /** False for ambient credentials pi cannot collect itself, such as AWS profiles or env vars. */ + interactive: boolean; + methodName?: string; +} + +export interface ModelsState { + readonly models: readonly ModelSummary[]; + readonly accounts: readonly ProviderAccount[]; + readonly refreshing: boolean; +} + +/** An auth prompt without its `AbortSignal`: the part that can cross a transport. */ +export type AuthPromptRequest = AuthPrompt extends infer Prompt + ? Prompt extends unknown + ? Omit + : never + : never; + +/** A service seen from the other side of a connection: every method returns a promise. */ +export type Remote = { + [K in keyof T]: T[K] extends (...args: infer A) => infer R ? (...args: A) => Promise> : never; +}; + +/** Names one service and carries its call and event types. Names are globally unique. */ +export interface ServiceToken { + readonly name: string; + /** Phantom, never read: keeps the types attached to the token. */ + readonly types?: (api: TApi, event: TEvent) => void; +} + +export function defineService(name: string): ServiceToken { + return { name }; +} + +export interface SessionSummary { + id: string; + path: string; + cwd: string; + createdAt: number; +} + +export interface SessionSnapshot { + sessionId: string; + cwd: string; + sessionPath: string; + /** Carries the lane configuration, queues, and stats: no side-channel replication. */ + lane: LaneSnapshot; + models: ModelsState; +} + +/** Everything the `Models` service publishes. */ +export type ModelsEvent = + | { type: "state"; state: ModelsState } + // Login runs the wrong way round: the request is an event, the answer is an ordinary call. + | { type: "prompt"; requestId: string; request: AuthPromptRequest } + | { type: "notice"; notice: AuthEvent }; + +/** The login half, for whatever drives the dialog. */ +export type AuthEventPayload = Exclude; + +/** One presentation's subscription: a `lane.watch()` in the worker, named so its events can be filtered. */ +export interface LaneSubscription { + subscriptionId: string; + snapshot: SessionSnapshot; +} + +/** Lane events are addressed to the subscription whose watch produced them. */ +export interface LaneEvent { + subscriptionId: string; + event: HarnessEvent; +} + +export interface LaneServiceApi { + /** + * Capture a snapshot and open a subscription for one presentation. Its events are addressed to + * `presentationId`, so the server routes them instead of broadcasting. Buffered until `start`. + */ + watch(presentationId: string): Promise; + /** Begin delivery, draining everything buffered since the snapshot. */ + start(subscriptionId: string): Promise; + unwatch(subscriptionId: string): Promise; + prompt(text: string): Promise; + steer(text: string): Promise; + followUp(text: string): Promise; + compact(): Promise; + abort(): Promise; + setModel(ref: ModelRef): Promise; +} + +export interface ModelsServiceApi { + refresh(): Promise; + login(providerId: string, authType: "oauth" | "api_key"): Promise; + authReply(requestId: string, answer: string | null): Promise; +} + +/** Provided by a worker so the server can identify the session it opened, without naming lane methods. */ +export interface WorkerServiceApi { + describe(): Promise<{ sessionId: string }>; +} + +export interface SessionsServiceApi { + list(): Promise; + attach(sessionId: string | null, cwd: string, presentationId: string): Promise; +} + +/** Provided by the worker. One subscription per presentation; `watch` again to rebase. */ +export const Lane = defineService("lane"); +/** Provided by the worker. Small enough to publish whole. */ +export const Models = defineService("models"); +/** Provided by the worker, consumed only by the server. */ +export const Worker = defineService("worker"); +/** Provided by the server. */ +export const Sessions = defineService("sessions"); diff --git a/packages/coding-agent/src/experimental/mini/shared/rpc.ts b/packages/coding-agent/src/experimental/mini/shared/rpc.ts new file mode 100644 index 0000000000000000000000000000000000000000..845945c047399a91d260095b76b8354dd31ae3af --- /dev/null +++ b/packages/coding-agent/src/experimental/mini/shared/rpc.ts @@ -0,0 +1,233 @@ +/** + * The whole protocol: call, result, error, cancel, event, ping. Plus named services and one routing + * rule. + * + * A peer provides any number of services and uses the other side's. A call for a service this peer + * does not provide goes to `forward`, which is what makes the server transparent: a TUI uses + * `lane.prompt`, the server does not provide `lane`, so it hands the call to the attached worker. + * The same rule lets a worker use `sessions.list` back through the server. + */ + +import type { Remote, ServiceToken } from "./protocol.ts"; +import type { Connection } from "./transport.ts"; + +type Frame = + | { kind: "call"; id: number; method: string; args: unknown[] } + | { kind: "result"; id: number; result: unknown } + | { kind: "error"; id: number; error: string } + | { kind: "cancel"; id: number } + | { kind: "event"; service: string; payload: unknown; to?: string } + | { kind: "announce"; services: string[] } + | { kind: "ping" }; + +export type Forward = (method: string, args: unknown[]) => Promise; + +export interface CallOptions { + /** Abandon the call and tell the peer to stop. */ + signal?: AbortSignal; + /** Reject if the peer has not answered in time. Omit for calls with no bounded duration. */ + timeoutMs?: number; +} + +export interface PeerOptions { + /** Handles calls for services this peer does not provide. */ + forward?: Forward; + /** Silence tolerated before the peer is declared gone. Default 15s; 0 disables liveness. */ + deadMs?: number; +} + +const DEFAULT_DEAD_MS = 15_000; + +export interface RpcPeer { + /** Register an implementation and announce the name to the other side. */ + provide(token: ServiceToken, implementation: TApi): void; + /** Services this peer provides. */ + readonly provided: ReadonlySet; + /** Services the other side announced. */ + readonly announced: ReadonlySet; + /** Use a service, wherever it is provided: this peer's other side, or its next hop. */ + use(token: ServiceToken, options?: CallOptions): Remote; + /** Publish to everyone listening on the other side. */ + emit(token: ServiceToken, event: TEvent): void; + /** Publish for one destination. A router delivers it there instead of broadcasting. */ + emitTo(token: ServiceToken, event: TEvent, to: string): void; + on(token: ServiceToken, handler: (event: TEvent) => void): void; + /** Router half of the event channel: observe and republish without knowing the service. */ + onEvent(handler: (service: string, payload: unknown, to: string | undefined) => void): void; + emitRaw(service: string, payload: unknown, to?: string): void; + call(method: string, ...args: unknown[]): Promise; + callWith(options: CallOptions, method: string, ...args: unknown[]): Promise; + onClose(handler: () => void): void; + close(): void; +} + +/** A bidirectional peer on one connection. */ +export function createPeer(connection: Connection, options: PeerOptions = {}): RpcPeer { + const services = new Map(); + const provided = new Set(); + /** What the other side told us it provides, so routing is a lookup rather than a guess. */ + const announced = new Set(); + const pending = new Map void; reject: (error: Error) => void }>(); + /** Controllers for calls this peer is currently answering, so a `cancel` frame can stop them. */ + const inflight = new Map(); + const eventHandlers: ((service: string, payload: unknown, to: string | undefined) => void)[] = []; + let nextId = 1; + let lastFrameAt = Date.now(); + + // The signal is appended to every handler call: services that care declare a trailing + // `AbortSignal` parameter, the rest ignore an extra argument. + const dispatch = async (method: string, args: unknown[], signal: AbortSignal): Promise => { + const dot = method.indexOf("."); + const local = dot === -1 ? undefined : services.get(method.slice(0, dot)); + if (!local) { + if (!options.forward) throw new Error(`No service provides ${method}`); + return options.forward(method, args); + } + const handler = (local as Record)[method.slice(dot + 1)]; + if (typeof handler !== "function") throw new Error(`Unknown method: ${method}`); + return (handler as (...args: unknown[]) => unknown).apply(local, [...args, signal]); + }; + + connection.onMessage((frameValue) => { + const frame = frameValue as Frame; + lastFrameAt = Date.now(); + switch (frame.kind) { + case "event": { + for (const handler of eventHandlers) handler(frame.service, frame.payload, frame.to); + return; + } + case "call": { + const controller = new AbortController(); + inflight.set(frame.id, controller); + void dispatch(frame.method, frame.args, controller.signal) + .then( + // `undefined` vanishes through JSON, so an absent result is sent as null. + (result) => connection.send({ kind: "result", id: frame.id, result: result ?? null }), + (error: unknown) => connection.send({ kind: "error", id: frame.id, error: message(error) }), + ) + .finally(() => inflight.delete(frame.id)); + return; + } + case "cancel": { + inflight.get(frame.id)?.abort(new Error("Cancelled by caller")); + inflight.delete(frame.id); + return; + } + case "result": + case "error": { + const waiter = pending.get(frame.id); + pending.delete(frame.id); + if (frame.kind === "error") waiter?.reject(new Error(frame.error)); + else waiter?.resolve(frame.result); + return; + } + case "announce": { + announced.clear(); + for (const service of frame.services) announced.add(service); + return; + } + case "ping": + return; + default: { + const unknownFrame: never = frame; + throw new Error(`Unknown frame: ${JSON.stringify(unknownFrame)}`); + } + } + }); + + connection.onClose(() => { + if (liveness) clearInterval(liveness); + for (const waiter of pending.values()) waiter.reject(new Error("Connection closed")); + pending.clear(); + for (const controller of inflight.values()) controller.abort(new Error("Connection closed")); + inflight.clear(); + }); + + /** + * A peer can vanish without closing: a killed machine, a wedged event loop. Any frame counts as + * proof of life, and pings keep an idle connection proving it. + */ + const deadMs = options.deadMs ?? DEFAULT_DEAD_MS; + const liveness = + deadMs > 0 + ? setInterval( + () => { + if (Date.now() - lastFrameAt > deadMs) connection.close(); + else connection.send({ kind: "ping" }); + }, + Math.floor(deadMs / 3), + ) + : undefined; + liveness?.unref(); + + const callWith = (callOptions: CallOptions, method: string, ...args: unknown[]): Promise => + new Promise((resolve, reject) => { + const id = nextId++; + let timer: NodeJS.Timeout | undefined; + const abandon = (error: Error): void => { + if (!pending.delete(id)) return; + if (timer) clearTimeout(timer); + callOptions.signal?.removeEventListener("abort", onAbort); + // Tell the peer to stop; it may already be gone, in which case this is a no-op. + connection.send({ kind: "cancel", id }); + reject(error); + }; + const onAbort = (): void => abandon(new Error("Call cancelled")); + const settle = + (handler: (value: T) => void) => + (value: T) => { + if (timer) clearTimeout(timer); + callOptions.signal?.removeEventListener("abort", onAbort); + handler(value); + }; + if (callOptions.signal?.aborted) { + reject(new Error("Call cancelled")); + return; + } + pending.set(id, { resolve: settle(resolve), reject: settle(reject) }); + callOptions.signal?.addEventListener("abort", onAbort, { once: true }); + if (callOptions.timeoutMs !== undefined) { + timer = setTimeout( + () => abandon(new Error(`${method} timed out after ${callOptions.timeoutMs}ms`)), + callOptions.timeoutMs, + ); + timer.unref(); + } + connection.send({ kind: "call", id, method, args }); + }); + + const peer: RpcPeer = { + provide: (token, implementation) => { + services.set(token.name, implementation); + provided.add(token.name); + connection.send({ kind: "announce", services: [...provided] }); + }, + provided, + announced, + use: (token, callOptions = {}) => + new Proxy({} as Remote ? TApi : never>, { + get: + (_target, method) => + (...args: unknown[]) => + callWith(callOptions, `${token.name}.${String(method)}`, ...args), + }) as never, + emit: (token, event) => connection.send({ kind: "event", service: token.name, payload: event }), + emitTo: (token, event, to) => connection.send({ kind: "event", service: token.name, payload: event, to }), + emitRaw: (service, payload, to) => + connection.send({ kind: "event", service, payload, ...(to === undefined ? {} : { to }) }), + on: (token, handler) => + eventHandlers.push((name, payload) => { + if (name === token.name) handler(payload as never); + }), + onEvent: (handler) => eventHandlers.push(handler), + call: (method, ...args) => callWith({}, method, ...args), + callWith, + onClose: (handler) => connection.onClose(handler), + close: () => connection.close(), + }; + return peer; +} + +function message(error: unknown): string { + return error instanceof Error ? error.message : String(error); +} diff --git a/packages/coding-agent/src/experimental/mini/shared/transport.ts b/packages/coding-agent/src/experimental/mini/shared/transport.ts new file mode 100644 index 0000000000000000000000000000000000000000..5b7e8abd22dd89223957a07a89ce56a3f6ddec30 --- /dev/null +++ b/packages/coding-agent/src/experimental/mini/shared/transport.ts @@ -0,0 +1,119 @@ +/** + * Pluggable transport: newline-delimited JSON over any duplex pair. + * + * `Connection` is the shared abstraction and every host uses it. `Transport` is only for hops that + * have an address to negotiate: the unix socket between presentations and the server. A spawned + * worker needs none, because the pipes exist before it does. + */ + +import { rm } from "node:fs/promises"; +import { createConnection as connectSocket, createServer, type Socket } from "node:net"; + +export interface Connection { + send(message: unknown): void; + onMessage(handler: (message: unknown) => void): void; + onClose(handler: () => void): void; + close(): void; +} + +export interface Listener { + close(): Promise; +} + +export interface Transport { + listen(onConnection: (connection: Connection) => void): Promise; + connect(): Promise; +} + +/** Frame JSON messages as one line each over a readable/writable pair. */ +export function jsonConnection( + input: NodeJS.ReadableStream, + output: NodeJS.WritableStream, + close: () => void, +): Connection { + const messageHandlers: ((message: unknown) => void)[] = []; + const closeHandlers: (() => void)[] = []; + let buffered = ""; + let closed = false; + const notifyClosed = (): void => { + if (closed) return; + closed = true; + for (const handler of closeHandlers) handler(); + }; + input.setEncoding("utf8"); + input.on("data", (chunk: string) => { + buffered += chunk; + let newline = buffered.indexOf("\n"); + while (newline !== -1) { + const line = buffered.slice(0, newline); + buffered = buffered.slice(newline + 1); + if (line.length > 0) { + const message: unknown = JSON.parse(line); + for (const handler of messageHandlers) handler(message); + } + newline = buffered.indexOf("\n"); + } + }); + input.on("end", notifyClosed); + input.on("error", notifyClosed); + output.on("error", notifyClosed); + return { + send: (message) => { + if (!closed) output.write(`${JSON.stringify(message)}\n`); + }, + onMessage: (handler) => messageHandlers.push(handler), + onClose: (handler) => closeHandlers.push(handler), + close: () => { + notifyClosed(); + close(); + }, + }; +} + +function socketConnection(socket: Socket): Connection { + return jsonConnection(socket, socket, () => socket.destroy()); +} + +/** + * A spawned child is connected at birth, so there is no address to dial and no `Transport`: the + * parent reads the child's stdout and writes its stdin, and the child sees the same pipes reversed. + */ +export function childConnection(child: { + stdin: NodeJS.WritableStream | null; + stdout: NodeJS.ReadableStream | null; + kill(): unknown; +}): Connection { + if (!child.stdin || !child.stdout) throw new Error("Child process was spawned without pipes"); + return jsonConnection(child.stdout, child.stdin, () => child.kill()); +} + +/** The child's own view of the pipes its parent created. */ +export function parentConnection(): Connection { + return jsonConnection(process.stdin, process.stdout, () => process.stdin.pause()); +} + +export function socketTransport(path: string): Transport { + return { + async listen(onConnection) { + await rm(path, { force: true }); + const server = createServer((socket) => onConnection(socketConnection(socket))); + await new Promise((resolve, reject) => { + server.once("error", reject); + server.listen(path, resolve); + }); + return { + close: () => + new Promise((resolve) => { + server.close(() => resolve()); + }), + }; + }, + connect() { + return new Promise((resolve, reject) => { + const socket = connectSocket(path); + socket.once("connect", () => resolve(socketConnection(socket))); + socket.once("error", reject); + }); + }, + }; +} diff --git a/packages/coding-agent/src/experimental/mini/tui/run.ts b/packages/coding-agent/src/experimental/mini/tui/run.ts new file mode 100644 index 0000000000000000000000000000000000000000..651adee353c220f97f67b5d6ad7ff41eed32c442 --- /dev/null +++ b/packages/coding-agent/src/experimental/mini/tui/run.ts @@ -0,0 +1,68 @@ +/** + * The presentation host: find or start the session server, attach to a session, run the view. + */ + +import { spawn } from "node:child_process"; +import { mkdir } from "node:fs/promises"; +import { extname, join } from "node:path"; +import { fileURLToPath } from "node:url"; +import { getAgentDir } from "../../../config.ts"; +import { socketTransport, type Transport } from "../shared/transport.ts"; +import { connect, listSessions } from "./session.ts"; +import { runView } from "./view.ts"; + +const SELF_EXTENSION = extname(fileURLToPath(import.meta.url)); +const SERVER_ENTRY = fileURLToPath(new URL(`../server/entry${SELF_EXTENSION}`, import.meta.url)); +const SERVER_START_TIMEOUT_MS = 10_000; + +export interface TuiOptions { + cwd?: string; + continueSession?: boolean; +} + +async function ensureServer(transport: Transport, socketPath: string, sessionsRoot: string): Promise { + try { + (await transport.connect()).close(); + return; + } catch { + // No server yet; start one. + } + // execArgv is forwarded so a parent running under a TypeScript loader produces children that do too. + const child = spawn(process.execPath, [...process.execArgv, SERVER_ENTRY, socketPath, sessionsRoot], { + detached: true, + stdio: "ignore", + }); + child.unref(); + const deadline = Date.now() + SERVER_START_TIMEOUT_MS; + while (Date.now() < deadline) { + try { + (await transport.connect()).close(); + return; + } catch { + await new Promise((resolve) => setTimeout(resolve, 50)); + } + } + throw new Error("Timed out waiting for the mini session server"); +} + +export async function runTui(options: TuiOptions = {}): Promise { + const cwd = options.cwd ?? process.cwd(); + const root = join(getAgentDir(), "experimental"); + await mkdir(root, { recursive: true }); + const socketPath = join(root, "mini.sock"); + const sessionsRoot = join(root, "mini-sessions"); + const transport = socketTransport(socketPath); + await ensureServer(transport, socketPath, sessionsRoot); + + let sessionId: string | null = null; + if (options.continueSession) { + const sessions = (await listSessions(transport)).filter((session) => session.cwd === cwd); + sessionId = sessions.sort((left, right) => left.createdAt - right.createdAt).at(-1)?.id ?? null; + } + const client = await connect(transport, sessionId, cwd); + try { + await runView(client); + } finally { + client.close(); + } +} diff --git a/packages/coding-agent/src/experimental/mini/tui/session.ts b/packages/coding-agent/src/experimental/mini/tui/session.ts new file mode 100644 index 0000000000000000000000000000000000000000..97a455994f8510d2316c38b46052cf45dd51d8db --- /dev/null +++ b/packages/coding-agent/src/experimental/mini/tui/session.ts @@ -0,0 +1,117 @@ +/** + * The presentation's half of a session: one connection, services by token, one replicated snapshot. + * + * `Sessions` is answered by the server and `Lane`/`Models` by the worker, but both are reached + * through this one peer, so the view never learns which host provides what. + * + * Alignment is not this file's problem. `lane.watch()` captures a snapshot in the worker and buffers + * that subscription's events there; `lane.start()` drains them. Nothing can arrive before this + * presentation holds the snapshot and knows its subscription id, so there is nothing to buffer here. + * + * The fold is the harness's `reduceLaneSnapshot`: a replica must not have a second opinion. + */ + +import { randomUUID } from "node:crypto"; +import type { HarnessEvent } from "@earendil-works/pi-agent-core"; +// Narrow entry: the fold is one file, so a presentation never evaluates the harness barrel. +import { reduceLaneSnapshot } from "@earendil-works/pi-agent-core/harness/runtime/reducer"; +import { + type AuthEventPayload, + Lane, + type LaneServiceApi, + Models, + type ModelsServiceApi, + type Remote, + type SessionSnapshot, + type SessionSummary, + Sessions, +} from "../shared/protocol.ts"; +import { createPeer } from "../shared/rpc.ts"; +import type { Transport } from "../shared/transport.ts"; + +export interface AttachedSession { + state(): SessionSnapshot; + subscribe(listener: () => void): () => void; + onAuth(handler: (event: AuthEventPayload) => void): void; + readonly lane: Remote; + readonly models: Remote; + close(): void; +} + +/** Attach spawns a worker when none is running; runs and other lane calls stay unbounded. */ +const ATTACH_TIMEOUT_MS = 60_000; + +export async function listSessions(transport: Transport): Promise { + const peer = createPeer(await transport.connect()); + try { + return await peer.use(Sessions).list(); + } finally { + peer.close(); + } +} + +/** Attach to `sessionId`, or to a new session when it is null. */ +export async function connect(transport: Transport, sessionId: string | null, cwd: string): Promise { + const peer = createPeer(await transport.connect()); + const lane = peer.use(Lane); + /** This presentation's identity: the server routes our lane events by it. */ + const presentationId = randomUUID(); + const listeners = new Set<() => void>(); + const authHandlers = new Set<(event: AuthEventPayload) => void>(); + const publish = (): void => { + for (const listener of listeners) listener(); + }; + + let snapshot: SessionSnapshot | undefined; + let subscriptionId: string | undefined; + + const fold = (event: HarnessEvent): void => { + if (!snapshot) return; + if (reduceLaneSnapshot(snapshot.lane, event) === "rebase") { + void resubscribe(); + return; + } + publish(); + }; + + /** First attach and rebase are the same operation: take a new subscription, drop the old one. */ + const resubscribe = async (): Promise => { + const previous = subscriptionId; + const opened = await lane.watch(presentationId); + snapshot = opened.snapshot; + subscriptionId = opened.subscriptionId; + publish(); + await lane.start(opened.subscriptionId); + if (previous) void lane.unwatch(previous); + }; + + peer.on(Lane, (event) => { + // Addressed to us by the server; the id check discards a superseded subscription after a rebase. + if (event.subscriptionId === subscriptionId) fold(event.event); + }); + peer.on(Models, (event) => { + if (event.type !== "state") { + // Login prompts and notices are not state; they drive the dialog. + for (const handler of authHandlers) handler(event); + return; + } + if (!snapshot) return; + snapshot = { ...snapshot, models: event.state }; + publish(); + }); + + await peer.use(Sessions, { timeoutMs: ATTACH_TIMEOUT_MS }).attach(sessionId, cwd, presentationId); + await resubscribe(); + + return { + state: () => snapshot as SessionSnapshot, + subscribe: (listener) => { + listeners.add(listener); + return () => listeners.delete(listener); + }, + onAuth: (handler) => authHandlers.add(handler), + lane, + models: peer.use(Models), + close: () => peer.close(), + }; +} diff --git a/packages/coding-agent/src/experimental/mini/tui/view.ts b/packages/coding-agent/src/experimental/mini/tui/view.ts new file mode 100644 index 0000000000000000000000000000000000000000..295d178c5fe43b5fc65865618ded38306f46bfe8 --- /dev/null +++ b/packages/coding-agent/src/experimental/mini/tui/view.ts @@ -0,0 +1,555 @@ +/** + * The view. It holds no live objects: no harness, lane, session, or model runtime. + * + * Everything it renders comes from a replicated `SessionView` snapshot, and everything it does is a + * command that answers with data. Whether that view is in-process or a socket away is invisible here. + */ + +import type { AgentMessage, Entry, LaneSnapshot } from "@earendil-works/pi-agent-core"; +import type { AssistantMessage, AuthEvent } from "@earendil-works/pi-ai"; +import { + type Component, + Container, + type Focusable, + fuzzyFilter, + getKeybindings, + Input, + ProcessTerminal, + ScrollView, + type SelectItem, + SelectList, + type SelectListTheme, + Spacer, + setKeybindings, + Text, + TruncatedText, + TuiAltScreen, + VStack, +} from "@earendil-works/pi-tui"; +import { getAgentDir } from "../../../config.ts"; +import { KeybindingsManager } from "../../../core/keybindings.ts"; +import { createAllToolRenderers } from "../../../core/tools/renderers/index.ts"; +import { AssistantMessageComponent } from "../../../modes/interactive/components/assistant-message.ts"; +import { CustomEditor } from "../../../modes/interactive/components/custom-editor.ts"; +import { DynamicBorder } from "../../../modes/interactive/components/dynamic-border.ts"; +import { ExtensionSelectorComponent } from "../../../modes/interactive/components/extension-selector.ts"; +import { keyText } from "../../../modes/interactive/components/keybinding-hints.ts"; +import { LoginDialogComponent } from "../../../modes/interactive/components/login-dialog.ts"; +import { + type AuthSelectorProvider, + OAuthSelectorComponent, +} from "../../../modes/interactive/components/oauth-selector.ts"; +import { + type StatusIndicator, + WorkingStatusIndicator, +} from "../../../modes/interactive/components/status-indicator.ts"; +import { ToolExecutionComponent, type ToolRenderers } from "../../../modes/interactive/components/tool-execution.ts"; +import { UserMessageComponent } from "../../../modes/interactive/components/user-message.ts"; +import { getEditorTheme, initTheme, theme } from "../../../modes/interactive/theme/theme.ts"; +import type { AuthPromptRequest, CommandResult, ProviderAccount } from "../shared/protocol.ts"; +import type { AttachedSession } from "./session.ts"; + +const SUBSCRIPTION_LOGIN_LABEL = "Sign in with an account"; +const API_KEY_LOGIN_LABEL = "Sign in with an API key"; + +function userMessageText(message: AgentMessage): string { + if (message.role !== "user") return ""; + if (typeof message.content === "string") return message.content; + return message.content + .filter((content) => content.type === "text") + .map((content) => content.text) + .join(""); +} + +const SELECT_THEME: SelectListTheme = { + selectedPrefix: (text) => theme.fg("accent", text), + selectedText: (text) => theme.fg("accent", text), + description: (text) => theme.fg("muted", text), + scrollInfo: (text) => theme.fg("dim", text), + noMatch: (text) => theme.fg("warning", text), +}; + +/** Fuzzy-searchable list over plain data, used where interactive mode needs a live ModelRuntime. */ +class ListSelector extends Container implements Focusable { + readonly #input = new Input(); + readonly #listContainer = new Container(); + readonly #items: SelectItem[]; + readonly #onSelect: (value: string) => void; + readonly #onCancel: () => void; + #list: SelectList; + #isFocused = false; + + constructor(title: string, items: SelectItem[], onSelect: (value: string) => void, onCancel: () => void) { + super(); + this.#items = items; + this.#onSelect = onSelect; + this.#onCancel = onCancel; + this.#list = this.#build(items); + this.addChild(new DynamicBorder()); + this.addChild(new Spacer(1)); + this.addChild(new Text(theme.fg("accent", theme.bold(title)), 1, 0)); + this.addChild(this.#input); + this.addChild(new Spacer(1)); + this.addChild(this.#listContainer); + this.addChild(new DynamicBorder()); + } + + get focused(): boolean { + return this.#isFocused; + } + + set focused(value: boolean) { + this.#isFocused = value; + this.#input.focused = value; + } + + handleInput(data: string): void { + const keybindings = getKeybindings(); + const forwarded = ["tui.select.up", "tui.select.down", "tui.select.confirm", "tui.select.cancel"] as const; + if (forwarded.some((action) => keybindings.matches(data, action))) { + this.#list.handleInput(data); + return; + } + this.#input.handleInput(data); + const query = this.#input.getValue(); + const filtered = + query.length === 0 ? this.#items : fuzzyFilter(this.#items, query, (item) => `${item.label} ${item.value}`); + this.#list = this.#build(filtered); + } + + #build(items: SelectItem[]): SelectList { + const list = new SelectList(items, 10, SELECT_THEME); + list.onSelect = (item) => this.#onSelect(item.value); + list.onCancel = () => this.#onCancel(); + this.#listContainer.clear(); + this.#listContainer.addChild(list); + return list; + } +} + +interface MiniTuiHandlers { + submit(text: string): void; + /** Queue the current editor text as a follow-up instead of steering the active run. */ + queueFollowUp(text: string): void; + interrupt(): void; + exit(): void; + selectModel(): void; +} + +/** Alt-screen chat surface. Rendering is a function of the replicated snapshot. */ +class MiniTui { + readonly #ui: TuiAltScreen; + readonly #chat = new Container(); + readonly #queue = new Container(); + readonly #status = new Container(); + readonly #footer = new Text("", 1, 0); + readonly #editorContainer = new Container(); + readonly #editor: CustomEditor; + readonly #cwd: string; + readonly #tools = new Map(); + /** Entry ids already painted, in transcript order, so re-renders only append. */ + #renderedEntryIds: string[] = []; + #streaming: AssistantMessageComponent | undefined; + #indicator: StatusIndicator | undefined; + #working = false; + #mountedDispose: (() => void) | undefined; + + constructor(cwd: string, handlers: MiniTuiHandlers) { + this.#cwd = cwd; + this.#ui = new TuiAltScreen(new ProcessTerminal(), false, getAgentDir()); + const keybindings = KeybindingsManager.create(); + setKeybindings(keybindings); + this.#editor = new CustomEditor(this.#ui, getEditorTheme(), keybindings, { paddingX: 1 }); + this.#editor.onSubmit = handlers.submit; + this.#editor.onEscape = handlers.interrupt; + this.#editor.onCtrlD = handlers.exit; + this.#editor.onAction("app.clear", handlers.exit); + this.#editor.onAction("app.model.select", handlers.selectModel); + this.#editor.onAction("app.message.followUp", () => { + const text = this.#editor.getText().trim(); + if (text.length === 0) return; + this.#editor.setText(""); + handlers.queueFollowUp(text); + }); + + this.#editorContainer.addChild(this.#editor); + const transcript = new ScrollView(this.#chat, { follow: "end", primary: true, overscroll: "chain" }); + const dock = new VStack([ + { component: this.#queue, shrink: 1, minSize: 0 }, + { component: this.#status, shrink: 1, minSize: 0 }, + { component: this.#editorContainer, shrink: 1, minSize: 3 }, + { component: this.#footer, shrink: 1, minSize: 0 }, + ]); + this.#ui.addChild(this.#chat); + this.#ui.addChild(this.#queue); + this.#ui.addChild(this.#status); + this.#ui.addChild(this.#editorContainer); + this.#ui.addChild(this.#footer); + this.#ui.setLayoutRoot( + new VStack([ + { component: transcript, basis: 0, grow: 1, shrink: 1, minSize: 1 }, + { component: dock, basis: "auto", grow: 0, shrink: 1, minSize: 1 }, + ]), + ); + this.#ui.setFocus(this.#editor); + } + + get ui(): TuiAltScreen { + return this.#ui; + } + + start(): void { + this.#ui.start(); + } + + stop(): void { + this.#mountedDispose?.(); + this.#indicator?.dispose(); + this.#ui.stop(); + } + + render(): void { + this.#ui.requestRender(); + } + + addText(text: string): void { + this.#chat.addChild(new Spacer(1)); + this.#chat.addChild(new Text(text, 1, 0)); + this.render(); + } + + setFooter(text: string): void { + this.#footer.setText(theme.fg("dim", text)); + this.render(); + } + + /** Swap the editor slot for a selector or dialog. */ + mount(component: Component, focus: Component, dispose?: () => void): void { + this.#mountedDispose?.(); + this.#mountedDispose = dispose; + this.#editorContainer.clear(); + this.#editorContainer.addChild(component); + this.#ui.setFocus(focus); + this.render(); + } + + restoreEditor(): void { + this.#mountedDispose?.(); + this.#mountedDispose = undefined; + this.#editorContainer.clear(); + this.#editorContainer.addChild(this.#editor); + this.#ui.setFocus(this.#editor); + this.render(); + } + + /** + * Paint one replicated snapshot. Settled entries are keyed by id and only appended, so a redraw + * per streamed token costs one markdown rebuild, not a full transcript rebuild. + */ + apply(snapshot: LaneSnapshot): void { + this.#syncTranscript(snapshot.transcript); + this.#syncStreaming(snapshot.operation?.streamingMessage); + for (const tool of snapshot.operation?.runningTools ?? []) { + const component = this.#tool(tool.toolName, tool.toolCallId, tool.args); + if (tool.status === "running") { + component.markExecutionStarted(); + if (tool.result !== undefined) component.updateResult({ ...tool.result, isError: false }, true); + } else { + component.updateResult({ ...tool.result, isError: tool.isError }, false); + } + } + this.#syncQueues(snapshot.queues); + this.#setWorking(snapshot.operation !== null); + this.render(); + } + + /** One ordered tagged inbox: steered and queued input, plus writes waiting for a boundary. */ + #syncQueues(queues: LaneSnapshot["queues"]): void { + this.#queue.clear(); + for (const item of queues) { + const text = + item.type === "message" ? userMessageText(item.message).replace(/\s+/g, " ") : `<${item.customType}>`; + this.#queue.addChild(new TruncatedText(theme.fg("muted", `[${item.kind}] ${text}`), 1, 0)); + } + } + + #syncTranscript(transcript: readonly Entry[]): void { + const diverged = this.#renderedEntryIds.some((id, index) => transcript[index]?.id !== id); + if (diverged) { + // Compaction, navigation, or a fork rewrote the branch: repaint from scratch. + this.#chat.clear(); + this.#tools.clear(); + this.#renderedEntryIds = []; + this.#streaming = undefined; + } + for (const entry of transcript.slice(this.#renderedEntryIds.length)) { + this.#addEntry(entry); + this.#renderedEntryIds.push(entry.id); + } + } + + #addEntry(entry: Entry): void { + if (entry.type === "compaction") { + // The compaction entry heads the branch, so it has to render its own retained messages. + this.addText(theme.fg("muted", `[compaction] compacted from ${entry.tokensBefore} tokens`)); + for (const retained of entry.retainedTail) this.#addMessage(retained); + return; + } + if (entry.type === "branch_summary") { + this.addText(theme.fg("muted", "[branch summary]")); + this.addText(entry.summary); + return; + } + if (entry.type === "custom") { + this.addText(theme.fg("muted", `[${entry.customType}]`)); + return; + } + this.#addMessage(entry.message); + } + + #addMessage(message: AgentMessage): void { + if (message.role === "user") { + this.#chat.addChild(new Spacer(1)); + this.#chat.addChild(new UserMessageComponent(userMessageText(message))); + } else if (message.role === "assistant") { + // Adopt the streaming component so the settled message replaces it instead of duplicating it. + const component = this.#streaming ?? new AssistantMessageComponent(); + if (!this.#streaming) this.#chat.addChild(component); + this.#streaming = undefined; + component.updateContent(message, false); + for (const content of message.content) { + if (content.type !== "toolCall") continue; + this.#tool(content.name, content.id, content.arguments).setArgsComplete(); + } + } else if (message.role === "toolResult") { + this.#tool(message.toolName, message.toolCallId).updateResult(message); + } + } + + #syncStreaming(message: AssistantMessage | undefined): void { + if (!message) return; + if (!this.#streaming) { + this.#streaming = new AssistantMessageComponent(); + this.#chat.addChild(this.#streaming); + } + this.#streaming.updateContent(message, true); + for (const content of message.content) { + if (content.type === "toolCall") this.#tool(content.name, content.id, content.arguments); + } + } + + /** Built-in renderers, without the tool implementations or their schemas. */ + static readonly #renderers: Record = createAllToolRenderers(); + + /** Get or create the component for a tool call; omit `args` to look up without overwriting them. */ + #tool(toolName: string, toolCallId: string, args?: unknown): ToolExecutionComponent { + const existing = this.#tools.get(toolCallId); + if (existing) { + if (args !== undefined) existing.updateArgs(args); + return existing; + } + const component = new ToolExecutionComponent( + toolName, + toolCallId, + args ?? {}, + {}, + MiniTui.#renderers[toolName], + this.#ui, + this.#cwd, + ); + this.#chat.addChild(component); + this.#tools.set(toolCallId, component); + return component; + } + + #setWorking(working: boolean): void { + if (working === this.#working) return; + this.#working = working; + this.#indicator?.dispose(); + this.#indicator = undefined; + this.#status.clear(); + if (working) { + this.#indicator = new WorkingStatusIndicator(this.#ui, "Working... (esc to abort)"); + this.#status.addChild(this.#indicator); + } + } +} + +function toAuthSelectorProviders(accounts: readonly ProviderAccount[]): AuthSelectorProvider[] { + return accounts.map((account) => ({ + id: account.id, + name: account.name, + authType: account.authType, + ...(account.configured ? { status: { type: account.authType, source: account.source ?? "configured" } } : {}), + })); +} + +/** The client half of an interactive login, live only while its dialog is mounted. */ +interface LoginUi { + prompt(request: AuthPromptRequest): Promise; + notify(notice: AuthEvent): void; +} + +function runLogin( + view: MiniTui, + client: AttachedSession, + account: ProviderAccount, + setUi: (ui: LoginUi | undefined) => void, +): Promise { + const dialog = new LoginDialogComponent(view.ui, account.id, () => view.restoreEditor(), account.name); + view.mount(dialog, dialog); + if (!account.interactive) { + dialog.showInfo(`${account.methodName ?? "Authentication"} is configured outside pi.`, [], true); + return Promise.resolve({ ok: true }); + } + setUi({ + notify: (notice) => { + if (notice.type === "auth_url") dialog.showAuth(notice.url, notice.instructions); + else if (notice.type === "device_code") { + dialog.showDeviceCode(notice); + dialog.showWaiting("Waiting for authentication..."); + } else if (notice.type === "info") dialog.showInfo(notice.message, notice.links); + else dialog.showProgress(notice.message); + }, + prompt: (request) => { + if (request.type === "manual_code") return dialog.showManualInput(request.message); + if (request.type !== "select") return dialog.showPrompt(request.message, request.placeholder); + return new Promise((resolve) => { + const selector = new ExtensionSelectorComponent( + request.message, + request.options.map((option) => option.label), + (label) => { + view.mount(dialog, dialog); + resolve(request.options.find((option) => option.label === label)?.id ?? null); + }, + () => { + view.mount(dialog, dialog); + resolve(null); + }, + ); + view.mount(selector, selector); + }); + }, + }); + return client.models.login(account.id, account.authType).finally(() => { + setUi(undefined); + view.restoreEditor(); + }); +} + +/** Run the view against one attached session until the user exits. */ +export async function runView(client: AttachedSession): Promise { + initTheme(); + let exit = (): void => {}; + const exited = new Promise((resolve) => { + exit = () => resolve(); + }); + + let view!: MiniTui; + let loginUi: LoginUi | undefined; + const report = (result: CommandResult): void => { + if (!result.ok) view.addText(theme.fg("error", result.error)); + }; + client.onAuth((event) => { + if (event.type === "notice") { + loginUi?.notify(event.notice); + return; + } + const answer = loginUi?.prompt(event.request) ?? Promise.resolve(null); + void answer.then( + (value) => client.models.authReply(event.requestId, value), + () => client.models.authReply(event.requestId, null), + ); + }); + + const selectModel = (): void => { + const items: SelectItem[] = client.state().models.models.map((model) => ({ + value: `${model.provider}/${model.modelId}`, + label: model.modelId, + description: model.provider, + })); + const selector = new ListSelector( + "Select model:", + items, + (value) => { + view.restoreEditor(); + const separator = value.indexOf("/"); + void client.lane + .setModel({ provider: value.slice(0, separator), modelId: value.slice(separator + 1) }) + .then(report); + }, + () => view.restoreEditor(), + ); + view.mount(selector, selector); + }; + const selectLoginProvider = (authType: ProviderAccount["authType"]): void => { + const accounts = client.state().models.accounts.filter((account) => account.authType === authType); + if (accounts.length === 0) { + view.restoreEditor(); + view.addText(theme.fg("dim", "No providers for that method.")); + return; + } + const selector = new OAuthSelectorComponent( + "login", + toAuthSelectorProviders(accounts), + (providerId) => { + view.restoreEditor(); + const account = accounts.find((candidate) => candidate.id === providerId); + const setLoginUi = (ui: LoginUi | undefined): void => { + loginUi = ui; + }; + if (account) void runLogin(view, client, account, setLoginUi).then(report); + }, + // Cancelling the provider list steps back to the auth method choice, as in interactive mode. + () => login(), + ); + view.mount(selector, selector); + }; + const login = (): void => { + const selector = new ExtensionSelectorComponent( + "Select authentication method:", + [SUBSCRIPTION_LOGIN_LABEL, API_KEY_LOGIN_LABEL], + (option) => selectLoginProvider(option === SUBSCRIPTION_LOGIN_LABEL ? "oauth" : "api_key"), + () => view.restoreEditor(), + ); + view.mount(selector, selector); + }; + + view = new MiniTui(client.state().cwd, { + submit: (text) => { + const trimmed = text.trim(); + if (trimmed.length === 0) return; + if (trimmed === "/model") return selectModel(); + if (trimmed === "/login") return login(); + if (trimmed === "/compact") { + void client.lane.compact().then(report); + return; + } + // A submission during an active run steers it; alt+enter queues a follow-up instead. + const busy = client.state().lane.operation !== null; + void (busy ? client.lane.steer(trimmed) : client.lane.prompt(trimmed)).then(report); + }, + queueFollowUp: (text) => void client.lane.followUp(text).then(report), + // The worker is authoritative. Never suppress abort from a potentially stale presentation snapshot. + interrupt: () => void client.lane.abort().then(report), + exit, + selectModel: () => selectModel(), + }); + + const render = (): void => { + const snapshot = client.state(); + view.apply(snapshot.lane); + const { model, thinkingLevel } = snapshot.lane.configuration; + view.setFooter( + `${model.provider}/${model.modelId} · thinking:${thinkingLevel} · ${keyText("app.model.select")} or /model · /login · /compact · ${keyText("app.message.followUp")} follow-up · ${keyText("app.clear")} exit`, + ); + }; + const unsubscribe = client.subscribe(render); + + view.start(); + render(); + view.addText(theme.fg("dim", client.state().sessionPath)); + await exited; + unsubscribe(); + view.stop(); +} diff --git a/packages/coding-agent/src/experimental/mini/worker/entry.ts b/packages/coding-agent/src/experimental/mini/worker/entry.ts new file mode 100644 index 0000000000000000000000000000000000000000..e4a123142d67a0cf20c9fa92355fed380027cf49 --- /dev/null +++ b/packages/coding-agent/src/experimental/mini/worker/entry.ts @@ -0,0 +1,13 @@ +/** Session worker process entry: `node worker/entry.ts [sessionId]`. */ + +import { runSessionWorker } from "./run.ts"; + +const [sessionsRoot, cwd, sessionId] = process.argv.slice(2); +if (!sessionsRoot || !cwd) throw new Error("Session worker requires [sessionId]"); + +void runSessionWorker({ sessionsRoot, cwd, ...(sessionId === undefined ? {} : { sessionId }) }).catch( + (error: unknown) => { + console.error(error); + process.exit(1); + }, +); diff --git a/packages/coding-agent/src/experimental/mini/worker/models-service.ts b/packages/coding-agent/src/experimental/mini/worker/models-service.ts new file mode 100644 index 0000000000000000000000000000000000000000..ec12cfe2120614c8e3782bde76f4bf48a2a8dece --- /dev/null +++ b/packages/coding-agent/src/experimental/mini/worker/models-service.ts @@ -0,0 +1,148 @@ +/** + * Worker-side implementation of the `Models` service. + * + * `ModelRuntime`, `Model`, and provider objects stay here. What leaves is a serializable catalog and + * account list, plus login prompts and notices as data. + */ + +import { randomUUID } from "node:crypto"; +import type { ModelRuntime } from "../../../core/model-runtime.ts"; +import { refreshModelCatalogs } from "../../../modes/interactive/model-catalog-refresh.ts"; +import type { + AuthPromptRequest, + CommandResult, + ModelsEvent, + ModelsServiceApi, + ModelsState, + ProviderAccount, +} from "../shared/protocol.ts"; + +const CATALOG_REFRESH_TIMEOUT_MS = 15_000; + +export class ModelsService implements ModelsServiceApi { + readonly #runtime: ModelRuntime; + readonly #publish: (event: ModelsEvent) => void; + /** Prompts issued to the presentation, awaiting `authReply`. */ + readonly #pendingAuth = new Map void>(); + #state: ModelsState; + + constructor(runtime: ModelRuntime, publish: (event: ModelsEvent) => void) { + this.#runtime = runtime; + this.#publish = publish; + this.#state = readState(runtime, false); + } + + get state(): ModelsState { + return this.#state; + } + + async refresh(): Promise { + this.#update(true); + const controller = new AbortController(); + const timeout = setTimeout(() => controller.abort(), CATALOG_REFRESH_TIMEOUT_MS); + try { + const result = await refreshModelCatalogs(this.#runtime, controller.signal); + return result.errors.size === 0 + ? { ok: true } + : { ok: false, error: `Some catalogs could not be refreshed: ${[...result.errors.keys()].join(", ")}` }; + } catch (error) { + return { ok: false, error: message(error) }; + } finally { + clearTimeout(timeout); + this.#update(false); + } + } + + /** Prompts and notices travel to the presentation as events; answers come back via `authReply`. */ + async login(providerId: string, authType: ProviderAccount["authType"]): Promise { + try { + await this.#runtime.login(providerId, authType, { + prompt: (prompt) => { + const { signal, ...request } = prompt; + return this.#ask(request, signal); + }, + notify: (notice) => this.#publish({ type: "notice", notice }), + }); + return { ok: true }; + } catch (error) { + return { ok: false, error: message(error) }; + } finally { + this.#update(false); + } + } + + async authReply(requestId: string, answer: string | null): Promise { + const waiter = this.#pendingAuth.get(requestId); + this.#pendingAuth.delete(requestId); + waiter?.(answer); + } + + /** Ask the presentation one question, honouring a provider-supplied deadline. */ + #ask(request: AuthPromptRequest, signal: AbortSignal | undefined): Promise { + if (signal?.aborted) return Promise.reject(new Error("Login cancelled")); + return new Promise((resolve, reject) => { + const requestId = randomUUID(); + const settle = (answer: string | null): void => { + signal?.removeEventListener("abort", onAbort); + if (answer === null) reject(new Error("Login cancelled")); + else resolve(answer); + }; + const onAbort = (): void => { + this.#pendingAuth.delete(requestId); + reject(new Error("Login cancelled")); + }; + this.#pendingAuth.set(requestId, settle); + signal?.addEventListener("abort", onAbort, { once: true }); + this.#publish({ type: "prompt", requestId, request }); + }); + } + + async logout(providerId: string): Promise { + try { + await this.#runtime.logout(providerId); + return { ok: true }; + } catch (error) { + return { ok: false, error: message(error) }; + } finally { + this.#update(false); + } + } + + #update(refreshing: boolean): void { + this.#state = readState(this.#runtime, refreshing); + this.#publish({ type: "state", state: this.#state }); + } +} + +function readState(runtime: ModelRuntime, refreshing: boolean): ModelsState { + const models = runtime + .getAvailableSnapshot() + .map((model) => ({ provider: model.provider, modelId: model.id, name: model.name })); + const accounts: ProviderAccount[] = []; + for (const provider of runtime.getProviders()) { + const status = runtime.getProviderAuthStatus(provider.id); + const shared = { + id: provider.id, + name: provider.name, + configured: status.configured, + ...((status.label ?? status.source === undefined) ? {} : { source: status.label ?? status.source }), + }; + if (provider.auth.oauth) { + accounts.push({ ...shared, authType: "oauth", interactive: true, methodName: provider.auth.oauth.name }); + } + if (provider.auth.apiKey) { + accounts.push({ + ...shared, + authType: "api_key", + interactive: provider.auth.apiKey.login !== undefined, + methodName: provider.auth.apiKey.name, + }); + } + } + accounts.sort((left, right) => left.name.localeCompare(right.name)); + return { models, accounts, refreshing }; +} + +function message(error: unknown): string { + return error instanceof Error ? error.message : String(error); +} diff --git a/packages/coding-agent/src/experimental/plugins/bundled.ts b/packages/coding-agent/src/experimental/plugins/bundled.ts new file mode 100644 index 0000000000000000000000000000000000000000..3ab43dcb062b4cdcb4209ae66194b377080d88a9 --- /dev/null +++ b/packages/coding-agent/src/experimental/plugins/bundled.ts @@ -0,0 +1,55 @@ +import { combineFacetLoaders, type FacetLoader, type JsonValue } from "@earendil-works/chord"; +import { + createFacetBundleArtifactLoader, + createFacetBundleLoader, + type FacetBundleArtifact, + readFacetBundleManifest, +} from "@earendil-works/chord/node"; + +const PRESENTATION_FACET_BUNDLES_KEY = "presentationFacetBundles"; +const PI_PLUGIN_API = "@earendil-works/pi-coding-agent/experimental/plugin"; + +export function createSessionPluginFacetLoader(manifestPaths: readonly string[]): FacetLoader | undefined { + if (manifestPaths.length === 0) return undefined; + return combineFacetLoaders(manifestPaths.map(createOptionalSessionFacetLoader)); +} + +function createOptionalSessionFacetLoader(manifestPath: string): FacetLoader { + const loader = createFacetBundleLoader({ + manifestPath, + entry: "session", + resolveExternal: resolvePluginExternal, + }); + return { + async load() { + const manifest = await readFacetBundleManifest(manifestPath); + if (manifest.entries.session !== undefined) return loader.load(); + return { facets: Object.freeze([]), async dispose() {} }; + }, + }; +} + +export function createPresentationFacetData(artifacts: readonly FacetBundleArtifact[]): JsonValue { + return { + [PRESENTATION_FACET_BUNDLES_KEY]: artifacts.map((artifact) => artifact as unknown as JsonValue), + }; +} + +/** Create local loaders only from artifacts selected and sent by the connected server. */ +export function createPresentationFacetLoaders(data: JsonValue): readonly FacetLoader[] { + if (data === null || Array.isArray(data) || typeof data !== "object") { + throw new Error("Invalid presentation plugin data"); + } + const artifacts = data[PRESENTATION_FACET_BUNDLES_KEY]; + if (artifacts === undefined) return []; + if (!Array.isArray(artifacts)) throw new Error("Invalid presentation plugin bundle list"); + return artifacts.map((artifact) => + createFacetBundleArtifactLoader({ artifact, resolveExternal: resolvePluginExternal }), + ); +} + +function resolvePluginExternal(specifier: string): string | undefined { + if (specifier !== PI_PLUGIN_API) return undefined; + const extension = import.meta.url.endsWith(".ts") ? "ts" : "js"; + return new URL(`../plugin.${extension}`, import.meta.url).href; +} diff --git a/packages/coding-agent/src/experimental/plugins/package.ts b/packages/coding-agent/src/experimental/plugins/package.ts new file mode 100644 index 0000000000000000000000000000000000000000..57c353738f059268c5f845bb4e5474ba020480fd --- /dev/null +++ b/packages/coding-agent/src/experimental/plugins/package.ts @@ -0,0 +1,171 @@ +import { createHash } from "node:crypto"; +import { readFile, rm, writeFile } from "node:fs/promises"; +import { basename, dirname, join, resolve } from "node:path"; +import { bundleFacetPackage } from "@earendil-works/chord/bundler"; +import { + FACET_BUNDLE_MANIFEST_FILE, + type FacetBundleArtifact, + readFacetBundleArtifact, +} from "@earendil-works/chord/node"; +import type { ServerId } from "@earendil-works/pi-protocol"; + +const PLUGIN_PACKAGE_PROFILE_VERSION = 1; +const DEFAULT_PLUGIN_FACETS = Object.freeze({ session: "src/session.ts", tui: "src/tui.ts" }); + +export interface ConfiguredServerPluginPackage { + readonly manifestPath: string; + build(): Promise; +} + +/** Persist an explicit plugin package selection or restore it for a later server generation. */ +export async function restoreServerPluginPackageProfile( + directory: string, + serverId: ServerId, + configuredPackagePaths?: readonly string[], +): Promise { + const path = join(directory, `plugin-packages-${serverId}.json`); + if (configuredPackagePaths === undefined) { + return (await readPluginPackageProfile(path, false)) ?? []; + } + const packagePaths = normalizePluginPackagePaths(configuredPackagePaths); + if (packagePaths.length === 0) await rm(path, { force: true }); + else await writePluginPackageProfile(path, packagePaths); + return packagePaths; +} + +/** Read the explicit plugin selection stored for one durable Session. */ +export function readSessionPluginPackageProfile( + directory: string, + serverId: ServerId, + sessionPath: string, +): Promise { + return readPluginPackageProfile(sessionPluginProfilePath(directory, serverId, sessionPath), true, sessionPath); +} + +/** Remove the plugin selection stored for one deleted Session. */ +export async function removeSessionPluginPackageProfile( + directory: string, + serverId: ServerId, + sessionPath: string, +): Promise { + await rm(sessionPluginProfilePath(directory, serverId, sessionPath), { force: true }); +} + +/** Persist the plugin selection for one durable Session. */ +export function writeSessionPluginPackageProfile( + directory: string, + serverId: ServerId, + sessionPath: string, + packagePaths: readonly string[], +): Promise { + return writePluginPackageProfile( + sessionPluginProfilePath(directory, serverId, sessionPath), + normalizePluginPackagePaths(packagePaths), + sessionPath, + ); +} + +/** Create a serialized server-owned builder for one configured plugin package. */ +export function createServerPluginPackage( + directory: string, + serverId: ServerId, + packagePath: string, +): ConfiguredServerPluginPackage { + const normalizedPackagePath = resolve(packagePath); + const outdir = join(directory, "plugin-builds", serverId, pluginBuildDirectoryName(normalizedPackagePath)); + const manifestPath = join(outdir, FACET_BUNDLE_MANIFEST_FILE); + let buildTail = Promise.resolve(); + return { + manifestPath, + build() { + const operation = buildTail.then(async () => { + const result = await bundleFacetPackage({ + packagePath: normalizedPackagePath, + outdir, + defaultFacets: DEFAULT_PLUGIN_FACETS, + }); + if (result.manifest.entries.tui === undefined) return []; + return [await readFacetBundleArtifact({ manifestPath: result.manifestPath, entry: "tui" })]; + }); + buildTail = operation.then( + () => undefined, + () => undefined, + ); + return operation; + }, + }; +} + +async function readPluginPackageProfile( + path: string, + allowEmpty: boolean, + sessionPath?: string, +): Promise { + let parsed: unknown; + try { + parsed = JSON.parse(await readFile(path, "utf8")); + } catch (error) { + if (error instanceof Error && "code" in error && error.code === "ENOENT") return undefined; + throw new Error(`Could not read experimental plugin package profile ${path}`, { cause: error }); + } + if ( + typeof parsed !== "object" || + parsed === null || + Array.isArray(parsed) || + Object.keys(parsed).some( + (key) => key !== "version" && key !== "packagePaths" && !(key === "sessionPath" && sessionPath !== undefined), + ) || + !("version" in parsed) || + parsed.version !== PLUGIN_PACKAGE_PROFILE_VERSION || + (sessionPath === undefined + ? "sessionPath" in parsed + : !("sessionPath" in parsed) || parsed.sessionPath !== sessionPath) || + !("packagePaths" in parsed) || + !Array.isArray(parsed.packagePaths) || + (!allowEmpty && parsed.packagePaths.length === 0) || + parsed.packagePaths.some((packagePath) => typeof packagePath !== "string" || packagePath.length === 0) + ) { + throw new Error(`Invalid experimental plugin package profile ${path}`); + } + return normalizePluginPackagePaths(parsed.packagePaths as readonly string[]); +} + +function writePluginPackageProfile(path: string, packagePaths: readonly string[], sessionPath?: string): Promise { + return writeFile( + path, + `${JSON.stringify( + { + version: PLUGIN_PACKAGE_PROFILE_VERSION, + ...(sessionPath === undefined ? {} : { sessionPath }), + packagePaths, + }, + null, + 2, + )}\n`, + { encoding: "utf8", mode: 0o600 }, + ); +} + +export function normalizePluginPackagePaths(packagePaths: readonly string[]): readonly string[] { + const normalized = packagePaths.map((packagePath) => { + if (packagePath.length === 0) throw new Error("Plugin package path must not be empty"); + return resolve(packagePath); + }); + if (new Set(normalized).size !== normalized.length) { + throw new Error("Plugin package paths must be unique"); + } + return Object.freeze(normalized); +} + +function sessionPluginProfilePath(directory: string, serverId: ServerId, sessionPath: string): string { + const hash = createHash("sha256").update(sessionPath).digest("hex").slice(0, 24); + return join(directory, `session-plugin-packages-${serverId}-${hash}.json`); +} + +function pluginBuildDirectoryName(packagePath: string): string { + const packageDirectory = basename(packagePath) === "package.json" ? dirname(packagePath) : packagePath; + const base = basename(packageDirectory).replaceAll(/[^a-zA-Z0-9._-]/gu, "-") || "plugin"; + const label = base.endsWith("-plugin") ? base : `${base}-plugin`; + const hash = createHash("sha256").update(packagePath).digest("hex").slice(0, 12); + return `${label}-${hash}`; +} diff --git a/packages/coding-agent/src/experimental/services/README.md b/packages/coding-agent/src/experimental/services/README.md new file mode 100644 index 0000000000000000000000000000000000000000..fef4437a69b6bbb7212a275eb1fbd5883c05fdda --- /dev/null +++ b/packages/coding-agent/src/experimental/services/README.md @@ -0,0 +1,25 @@ +# Experimental client/server service slices + +Facet setup generates each host's RPC service catalogue from its provided non-local tokens. Remote service sources obtain those catalogues and bind only services required by consuming facets; there is no handwritten built-in service inventory. With no selected Session, its deferred source admits unresolved requirements as unavailable and keeps their handles disconnected. Attachment validates them against the worker's generated catalogue, which is cached for later detached generations. Keyed services hydrate as an empty directory until their owning feature spawns an instance. + +| Scope | Service | Current slice | Continuation point | +|---|---|---|---| +| server | `SessionDirectory` | replicated state implemented | add authenticated per-client projection when identity lands | +| server | `SessionManagement` | create, remove, attach, detach implemented | add authenticated workspace authorization | +| server | `PresentationPlugins` | prepares the selected Session branch's matching TUI artifacts and reloads that branch | add authenticated plugin policy | +| session | `SessionPlugins` | reloads the configured Session facet generation | add coordinated multi-worker reload reporting | +| session | `Models` | state, default-persisting selection, thinking, refresh implemented | move provider/auth composition behind plugin facets | +| session | `AgentController` | presentation-safe `AgentLane` facade for prompting, queueing, abort, resume, compaction, and navigation | add new lane operations only when a presentation requires them | +| session | `Transcript` | replicated lane state with source-event metadata | add projections only when another presentation needs them | +| presentation | `SlashCommands` | process-local contribution registry with model, thinking, compact, reload, and example hello commands | add more presentation hookpoints only as concrete plugin slices require them | +| presentation | `PresentationUI` | process-local selection and status capabilities | add narrowly scoped UI capabilities only when commands require them | + +`ServerServiceSource.connection` and `SessionServiceSource.attachment` are implemented local control states. Session directory, creation, and address DTOs are owned by these coding-agent service contracts rather than `pi-protocol`; the transport treats their payloads as opaque service data. + +With `PI_EXPERIMENTAL=1`, an interactive `pi client` creates and attaches a Session before opening the service-only chat TUI. `pi client -c` and `pi client -r` attach the newest existing Session instead, preserving its durable model and thinking configuration. Model selection is available on demand through `/model`; it is never a startup screen. The presentation always uses the stable coding agent's alternate-screen renderer and shared transcript/dock viewport. It loads configured theme resources and uses the stable terminal light/dark detection and appearance-change notifications. Its replicated state feeds the stable editor, message, tool, status, theme, and tool-renderer components. The presentation drives the worker-owned main lane through `AgentController` and renders the `Transcript` service's complete replicated value while controller calls are pending. The provider remains the sole Harness reducer and mutates Transcript's tracked snapshot before publishing each event. Chord flushes compact operations once per publication, while each client/state pairing encodes them with an independent path dictionary. Chord reconstructs presentation replicas and owns hydration, sequencing, and gap detection. + +A foreground server uses repeatable `-e` options to establish its default Session and TUI facets. A local client may instead select packages for the Session it creates or resumes; that branch selection is persisted with the Session and does not alter other workers or the server default. Before attaching, the server asks Chord to build conventional `src/session.ts` and `src/tui.ts` entries into separate `plugin-builds/` directories, passes the generated manifest paths to that Session worker, and returns the matching TUI artifacts. The Session worker loads built-in facets and separately owned plugin generations, then creates one active `FacetHost`. `/reload` atomically rebuilds the packages, loads fresh plugin candidates, cuts them over through `FacetHost.reload()`, and disposes the retired generations. Host-created implementation dependencies such as `AgentLane`, `ModelRuntime`, and `SettingsManager` are passed directly to built-in facet factories; they are not exposed as services. The TUI loads configured presentation facets and adds one private bridge facet that consumes the connected server and selected-Session services for `ExperimentalClientTui`. Its local presentation services are `SlashCommands`, which owns command contributions, and `PresentationUI`, which exposes selection and status rendering without exposing the raw TUI. Command callbacks receive Chord `Context` directly. Facets consume `AgentController` explicitly for prompting, steering, or queueing and return structured controller results to the command dispatcher. + +A host may reload selected facets when their declared service shape is unchanged; retained consumers keep the same service facades while replacement implementations and singleton snapshots are installed behind them. Synchronous setup-time `env.provide()`, `env.provideMany()`, `env.use()`, and `env.observe()` calls produce the internal dependency graph; setup does not repeat a declarative dependency list, and service handles remain disconnected until the complete graph validates. Providers activate before consumers, observations connect with their consuming facet, and facet replacement or host shutdown disposes affected lifecycles in reverse dependency order. Server and Session service tokens are non-local and automatically published by their providing host. Presentation-only hookpoints such as `SlashCommands` are explicitly local and never enter an RPC catalogue. + +A facet always calls unqualified `env.use()` or `env.observe()`. The host resolves each token across facet-provided and connected services. The example plugin package contributes `/hello` through its TUI facet. `examples/plugins/pi-example-plugin/` is an actual `@earendil-works/pi-example-plugin` package: repeatable `-e` options select plugin packages as server defaults or for one Session branch, Chord discovers and builds their conventional facets, and the attached presentation receives matching TUI artifacts through `PresentationPlugins`. Other host facets from the same plugin remain separate bundle entries rather than one aggregate plugin object. Transport bindings remain internal machinery rather than part of the facet environment. `ExperimentalClientTui` currently owns terminal rendering, state subscriptions, navigation, and action dispatch directly. The server provider remains directly assembled until its complete host environment exists. The question dialog, diff review, Git, indexing-job, canvas, and rich local TUI service examples in `packages/agent/docs/plugins.md` are extension patterns, not built-in coding-agent services. Private references, trace carriers, and flow control remain protocol/host infrastructure slices rather than presentation service tokens. diff --git a/packages/coding-agent/src/experimental/services/agent-controller-provider.ts b/packages/coding-agent/src/experimental/services/agent-controller-provider.ts new file mode 100644 index 0000000000000000000000000000000000000000..04fb2693c1df0335c4ab6d37361ebafe14478b27 --- /dev/null +++ b/packages/coding-agent/src/experimental/services/agent-controller-provider.ts @@ -0,0 +1,109 @@ +import type { AgentLane, OperationResultRecord, SuspendedRun } from "@earendil-works/pi-agent-core"; +import type { ImageContent } from "@earendil-works/pi-ai"; +import type { + AgentController as AgentControllerService, + AgentOperationError, + AgentOperationResponse, + AgentPromptRequest, + AgentQueueResponse, +} from "./agent-controller.ts"; + +export function createAgentController(lane: AgentLane): AgentControllerService { + const queue = async ( + operation: "steer" | "followUp" | "nextRun", + request: AgentPromptRequest, + context: Parameters[1], + ): Promise => { + const [message, images] = toTextPrompt(request); + const result = await lane[operation](message, images, context); + return result.ok + ? { accepted: true, entryId: result.value.entryId, error: null } + : { accepted: false, entryId: null, error: toAgentError(result.error) }; + }; + + return { + async prompt(request, context) { + const [message, images] = toTextPrompt(request); + const result = await lane.prompt(message, images, context); + return result.ok + ? toOperationResponse(result.value) + : { accepted: false, operationId: operationId(result.error), error: toAgentError(result.error) }; + }, + async requestAbort(operationId, context) { + const result = await lane.requestAbort(operationId, context); + if (!result.ok) throw new Error(result.error.message); + }, + steer: (request, context) => queue("steer", request, context), + followUp: (request, context) => queue("followUp", request, context), + nextRun: (request, context) => queue("nextRun", request, context), + async cancelQueued(entryId, context) { + const result = await lane.cancelQueued(entryId, context); + if (!result.ok) throw new Error(result.error.message); + return { outcome: result.value.kind }; + }, + async resume(context) { + const result = await lane.resume(context); + return result.ok + ? toOperationResponse(result.value) + : { accepted: false, operationId: null, error: toAgentError(result.error) }; + }, + async compact(request, context) { + const result = await lane.compact( + request.customInstructions === null ? undefined : { customInstructions: request.customInstructions }, + context, + ); + return result.ok + ? toOperationResponse(result.value.compaction) + : { accepted: false, operationId: operationId(result.error), error: toAgentError(result.error) }; + }, + async navigate(request, context) { + const result = await lane.navigateTree( + request.targetId, + { + summarize: request.summarize, + ...(request.label === null ? {} : { label: request.label }), + ...(request.customInstructions === null ? {} : { customInstructions: request.customInstructions }), + }, + context, + ); + return result.ok + ? toOperationResponse(result.value.navigation) + : { accepted: false, operationId: operationId(result.error), error: toAgentError(result.error) }; + }, + }; +} + +function toOperationResponse(value: OperationResultRecord | SuspendedRun): AgentOperationResponse { + return { + accepted: true, + operationId: value.operationId, + error: + "status" in value && value.status === "failed" && value.error !== undefined + ? { code: value.error.code, message: value.error.message } + : null, + }; +} + +function operationId(error: { readonly _tag: string }): string | null { + return "operationId" in error && typeof error.operationId === "string" ? error.operationId : null; +} + +function toAgentError(error: { readonly _tag: string; readonly message: string }): AgentOperationError { + const code = + { + LaneBusy: "lane_busy", + InvalidMessage: "invalid_message", + UnknownSkill: "unknown_skill", + UnknownTemplate: "unknown_template", + NothingToCompact: "nothing_to_compact", + NothingToResume: "nothing_to_resume", + InvalidNavigation: "invalid_navigation", + UnknownTarget: "unknown_target", + Closed: "closed", + }[error._tag] ?? "operation_failed"; + return { code, message: error.message }; +} + +function toTextPrompt(request: AgentPromptRequest): [message: string, images: ImageContent[] | undefined] { + return [request.message, request.images ?? undefined]; +} diff --git a/packages/coding-agent/src/experimental/services/agent-controller.ts b/packages/coding-agent/src/experimental/services/agent-controller.ts new file mode 100644 index 0000000000000000000000000000000000000000..fc74962adb94f027fdfd31cfeee18f107a97c54e --- /dev/null +++ b/packages/coding-agent/src/experimental/services/agent-controller.ts @@ -0,0 +1,54 @@ +import { type Context, defineService } from "@earendil-works/chord"; + +export interface AgentPromptImage { + type: "image"; + data: string; + mimeType: string; +} + +export interface AgentPromptRequest { + message: string; + images: AgentPromptImage[] | null; +} + +export interface AgentOperationError { + code: string; + message: string; +} + +export type AgentOperationResponse = + | { accepted: true; operationId: string; error: AgentOperationError | null } + | { accepted: false; operationId: string | null; error: AgentOperationError }; + +export type AgentQueueResponse = + | { accepted: true; entryId: string; error: null } + | { accepted: false; entryId: null; error: AgentOperationError }; + +export interface AgentCompactionRequest { + customInstructions: string | null; +} + +export interface AgentNavigationRequest { + targetId: string | null; + summarize: boolean; + label: string | null; + customInstructions: string | null; +} + +/** Presentation-safe command facade over the worker-owned main AgentLane. */ +export interface AgentController { + prompt(request: AgentPromptRequest, context: Context): Promise; + requestAbort(operationId: string, context: Context): Promise; + steer(request: AgentPromptRequest, context: Context): Promise; + followUp(request: AgentPromptRequest, context: Context): Promise; + nextRun(request: AgentPromptRequest, context: Context): Promise; + cancelQueued( + entryId: string, + context: Context, + ): Promise<{ outcome: "cancelled" | "already_consumed" | "not_found" }>; + resume(context: Context): Promise; + compact(request: AgentCompactionRequest, context: Context): Promise; + navigate(request: AgentNavigationRequest, context: Context): Promise; +} + +export const AgentController = defineService("pi.agent-controller"); diff --git a/packages/coding-agent/src/experimental/services/connection.ts b/packages/coding-agent/src/experimental/services/connection.ts new file mode 100644 index 0000000000000000000000000000000000000000..87f6f2745d48f489d1fcc9ae52475ad017803d8e --- /dev/null +++ b/packages/coding-agent/src/experimental/services/connection.ts @@ -0,0 +1,405 @@ +import { + type Context, + createRemoteServiceBinding, + type MutableReplicatedState, + type RemoteServiceBinding, + type RemoteServiceSource, + type RemoteServices, + type RemoteServiceTransport, + type ReplicatedState, + replicatedState, + type Service, + type ServiceCatalogueEntry, +} from "@earendil-works/chord"; +import { BACKGROUND_CONTEXT } from "@earendil-works/chord/context"; +import { type Client, createClientServiceTransport } from "@earendil-works/pi-client"; +import type { SessionTarget } from "@earendil-works/pi-protocol"; + +export type ServerConnectionState = + | { status: "connecting"; attempt: number } + | { status: "connected"; since: string } + | { status: "disconnected"; since: string; reason: string; retryAt: string | null }; + +export type SessionAttachmentState = + | { status: "detached" } + | { status: "attaching" | "attached" | "degraded"; sessionId: string }; + +export interface ServerServiceSource extends RemoteServiceSource { + readonly connection: ReplicatedState; + dispose(context: Context): Promise; +} + +export interface SessionServiceSource extends RemoteServiceSource { + readonly attachment: ReplicatedState; + /** Wait for the exact current attachment generation to finish hydrating. */ + whenAttached(sessionId: string, context: Context): Promise; + /** Wait for every binding to finish releasing the previous attachment. */ + whenDetached(context: Context): Promise; + dispose(context: Context): Promise; +} + +export interface ServiceSourceOptions { + readonly onError?: (error: Error) => void; +} + +class RoutedServiceBinding implements RemoteServices { + readonly #services: RemoteServiceBinding; + readonly #getBound: () => boolean; + readonly #onActivate: (context: Context) => Promise; + readonly #remove: () => void; + #activated = false; + #activationComplete = false; + + constructor(options: { + readonly services: readonly { readonly id: string }[]; + readonly transport: RemoteServiceTransport; + readonly getBound: () => boolean; + readonly assertAccess: () => void; + readonly onError: (error: Error) => void; + readonly onActivate?: (context: Context) => Promise; + readonly remove: () => void; + }) { + this.#services = createRemoteServiceBinding({ + services: options.services, + transport: options.transport, + bound: false, + assertAccess: options.assertAccess, + onError: options.onError, + }); + this.#getBound = options.getBound; + this.#onActivate = options.onActivate ?? (() => Promise.resolve()); + this.#remove = options.remove; + } + + use(service: Service): T { + return this.#services.use(service); + } + + observe(service: Service, handler: (service: T, context: Context) => void | Promise): () => void { + return this.#services.observe(service, handler); + } + + async ready(context: Context): Promise { + if (!this.#activated) { + this.#activated = true; + await this.#services.rebind(this.#getBound(), context); + } + await this.serviceReady(context); + if (!this.#activationComplete) { + await this.#onActivate(context); + this.#activationComplete = true; + } + } + + serviceReady(context: Context): Promise { + return this.#services.ready(context); + } + + updateBound(bound: boolean, context: Context): Promise { + return this.#activated ? this.#services.rebind(bound, context) : Promise.resolve(); + } + + async dispose(context: Context): Promise { + this.#remove(); + await this.#services.dispose(context); + } +} + +class ServerServiceSourceImpl implements ServerServiceSource { + readonly acceptsUnavailableServices = false; + readonly connection: ReplicatedState; + readonly #client: Client; + readonly #transport: RemoteServiceTransport; + readonly #bindings = new Set(); + readonly #removeConnectionListener: () => void; + readonly #onError: (error: Error) => void; + #transition = Promise.resolve(); + #connectionAttempt: number; + #disposed = false; + + constructor(client: Client, options: ServiceSourceOptions) { + this.#client = client; + this.#transport = createClientServiceTransport(client, () => ({ serverId: client.serverId })); + this.#onError = options.onError ?? (() => {}); + this.#connectionAttempt = client.connectionState === "connecting" ? 1 : 0; + const connectionState = replicatedState( + toServerConnectionState(client, this.#connectionAttempt), + ); + this.connection = connectionState; + this.#removeConnectionListener = client.onConnectionStateChange(({ state, error }) => { + if (state === "connecting") this.#connectionAttempt += 1; + publishReplacement( + connectionState, + toServerConnectionState(client, this.#connectionAttempt, error), + BACKGROUND_CONTEXT, + ); + this.#transition = this.#transition + .then(async () => { + const results = await Promise.allSettled( + [...this.#bindings].map((binding) => binding.updateBound(state === "connected", BACKGROUND_CONTEXT)), + ); + const failures = results.filter( + (result): result is PromiseRejectedResult => result.status === "rejected", + ); + if (failures.length > 0) throw new AggregateError(failures.map(({ reason }) => reason)); + }) + .catch((transitionError: unknown) => this.#onError(toError(transitionError))); + }); + } + + catalogue(context: Context) { + return this.#client.serviceCatalogue({ serverId: this.#client.serverId }, context.abortSignal); + } + + open(options: { + readonly services: readonly { readonly id: string }[]; + assertAccess(): void; + onError(error: Error): void; + }): RemoteServices { + if (this.#disposed) throw new Error("Server service source is disposed"); + let binding!: RoutedServiceBinding; + binding = new RoutedServiceBinding({ + services: options.services, + transport: this.#transport, + getBound: () => this.#client.connected, + assertAccess: options.assertAccess, + onError: options.onError, + remove: () => this.#bindings.delete(binding), + }); + this.#bindings.add(binding); + return binding; + } + + async dispose(context: Context): Promise { + if (this.#disposed) return; + this.#disposed = true; + this.#removeConnectionListener(); + await this.#transition; + const bindings = [...this.#bindings]; + this.#bindings.clear(); + const results = await Promise.allSettled(bindings.map((binding) => binding.dispose(context))); + throwFailures(results, "Failed to dispose server service source"); + } +} + +class SessionServiceSourceImpl implements SessionServiceSource { + readonly attachment: ReplicatedState; + readonly #client: Client; + readonly #transport: RemoteServiceTransport; + readonly #attachmentState: MutableReplicatedState; + readonly #bindings = new Set(); + readonly #removeAttachmentListener: () => void; + readonly #onError: (error: Error) => void; + readonly #transitions = new Set>(); + #catalogue: readonly ServiceCatalogueEntry[] | undefined; + #attachmentRevision = 0; + #disposed = false; + + get acceptsUnavailableServices(): boolean { + return this.#client.attachment === undefined && this.#catalogue === undefined; + } + + constructor(client: Client, options: ServiceSourceOptions) { + this.#client = client; + this.#transport = createClientServiceTransport(client, () => client.attachment); + this.#onError = options.onError ?? (() => {}); + this.#attachmentState = replicatedState( + client.attachment === undefined + ? { status: "detached" } + : { status: "attaching", sessionId: client.attachment.sessionId }, + ); + this.attachment = this.#attachmentState; + this.#removeAttachmentListener = client.onAttachmentChange((attachment) => { + const revision = ++this.#attachmentRevision; + if (attachment !== undefined) { + publishReplacement( + this.#attachmentState, + { status: "attaching", sessionId: attachment.sessionId }, + BACKGROUND_CONTEXT, + ); + void this.#client.serviceCatalogue(attachment).then( + (catalogue) => { + if (this.#attachmentRevision === revision && sameAttachment(this.#client.attachment, attachment)) { + this.#catalogue = catalogue; + } + }, + (error: unknown) => this.#onError(toError(error)), + ); + } + const transition = this.#rebind(attachment !== undefined, BACKGROUND_CONTEXT); + this.#transitions.add(transition); + void transition.then( + () => { + this.#transitions.delete(transition); + if (this.#attachmentRevision !== revision || !sameAttachment(this.#client.attachment, attachment)) + return; + publishReplacement( + this.#attachmentState, + attachment === undefined + ? { status: "detached" } + : { status: "attached", sessionId: attachment.sessionId }, + BACKGROUND_CONTEXT, + ); + }, + (error: unknown) => { + this.#transitions.delete(transition); + if (this.#attachmentRevision !== revision || !sameAttachment(this.#client.attachment, attachment)) + return; + publishReplacement( + this.#attachmentState, + attachment === undefined + ? { status: "detached" } + : { status: "degraded", sessionId: attachment.sessionId }, + BACKGROUND_CONTEXT, + ); + this.#onError(toError(error)); + }, + ); + }); + } + + async catalogue(context: Context): Promise { + const target = this.#client.attachment; + if (target === undefined) return this.#catalogue ?? []; + const catalogue = await this.#client.serviceCatalogue(target, context.abortSignal); + this.#catalogue = catalogue; + return catalogue; + } + + open(options: { + readonly services: readonly { readonly id: string }[]; + assertAccess(): void; + onError(error: Error): void; + }): RemoteServices { + if (this.#disposed) throw new Error("Session service source is disposed"); + let binding!: RoutedServiceBinding; + binding = new RoutedServiceBinding({ + services: options.services, + transport: this.#transport, + getBound: () => this.#client.attachment !== undefined, + onActivate: async (context) => { + const attachment = this.#client.attachment; + if (attachment === undefined) await this.#whenDetached(this.#attachmentRevision, context); + else await this.#whenAttached(attachment, this.#attachmentRevision, context); + }, + assertAccess: options.assertAccess, + onError: options.onError, + remove: () => this.#bindings.delete(binding), + }); + this.#bindings.add(binding); + return binding; + } + + async whenAttached(sessionId: string, context: Context): Promise { + const attachment = this.#client.attachment; + if (attachment === undefined || attachment.sessionId !== sessionId) { + throw new Error(`Session ${sessionId} is not the current attachment`); + } + await this.#whenAttached(attachment, this.#attachmentRevision, context); + } + + async whenDetached(context: Context): Promise { + if (this.#client.attachment !== undefined) throw new Error("A Session is still attached"); + await this.#whenDetached(this.#attachmentRevision, context); + } + + async dispose(context: Context): Promise { + if (this.#disposed) return; + this.#disposed = true; + this.#removeAttachmentListener(); + await Promise.allSettled(this.#transitions); + const bindings = [...this.#bindings]; + this.#bindings.clear(); + const results = await Promise.allSettled(bindings.map((binding) => binding.dispose(context))); + throwFailures(results, "Failed to dispose Session service source"); + } + + async #rebind(bound: boolean, context: Context): Promise { + const results = await Promise.allSettled( + [...this.#bindings].map((binding) => binding.updateBound(bound, context)), + ); + throwFailures(results, "Failed to rebind Session services"); + } + + async #whenAttached(attachment: SessionTarget, revision: number, context: Context): Promise { + try { + await Promise.all([...this.#bindings].map((binding) => binding.serviceReady(context))); + } catch (error) { + if (this.#attachmentRevision === revision && sameAttachment(this.#client.attachment, attachment)) { + publishReplacement(this.#attachmentState, { status: "degraded", sessionId: attachment.sessionId }, context); + } + throw error; + } + if (this.#attachmentRevision !== revision || !sameAttachment(this.#client.attachment, attachment)) { + throw new Error(`Session ${attachment.sessionId} was replaced while attaching`); + } + const state = this.#attachmentState.value; + if (state.status !== "attached" || state.sessionId !== attachment.sessionId) { + publishReplacement(this.#attachmentState, { status: "attached", sessionId: attachment.sessionId }, context); + } + } + + async #whenDetached(revision: number, context: Context): Promise { + await Promise.all([...this.#bindings].map((binding) => binding.serviceReady(context))); + if (this.#attachmentRevision !== revision || this.#client.attachment !== undefined) { + throw new Error("The Session attachment changed while detaching"); + } + if (this.#attachmentState.value.status !== "detached") { + publishReplacement(this.#attachmentState, { status: "detached" }, context); + } + } +} + +/** Create the server-scoped remote service source for one presentation client. */ +export function createServerServiceSource(client: Client, options: ServiceSourceOptions = {}): ServerServiceSource { + return new ServerServiceSourceImpl(client, options); +} + +/** Create the selected-Session remote service source for one presentation client. */ +export function createSessionServiceSource(client: Client, options: ServiceSourceOptions = {}): SessionServiceSource { + return new SessionServiceSourceImpl(client, options); +} + +function publishReplacement(state: MutableReplicatedState, value: T, context: Context): void { + const target = state.state as Record; + const replacement = value as Record; + for (const key of Object.keys(target)) { + if (!Object.hasOwn(replacement, key)) delete target[key]; + } + Object.assign(target, replacement); + state.publish(context); +} + +function toServerConnectionState(client: Client, attempt: number, error?: Error): ServerConnectionState { + const since = new Date().toISOString(); + switch (client.connectionState) { + case "connecting": + return { status: "connecting", attempt }; + case "connected": + return { status: "connected", since }; + case "disconnected": + return { + status: "disconnected", + since, + reason: error?.message ?? "Client is disconnected", + retryAt: null, + }; + } +} + +function sameAttachment(left: SessionTarget | undefined, right: SessionTarget | undefined): boolean { + if (left === undefined || right === undefined) return left === right; + return ( + left.serverId === right.serverId && left.sessionId === right.sessionId && left.attachmentId === right.attachmentId + ); +} + +function throwFailures(results: readonly PromiseSettledResult[], message: string): void { + const errors = results.flatMap((result) => (result.status === "rejected" ? [result.reason] : [])); + if (errors.length === 1) throw errors[0]; + if (errors.length > 1) throw new AggregateError(errors, message); +} + +function toError(error: unknown): Error { + return error instanceof Error ? error : new Error(String(error)); +} diff --git a/packages/coding-agent/src/experimental/services/models-provider.ts b/packages/coding-agent/src/experimental/services/models-provider.ts new file mode 100644 index 0000000000000000000000000000000000000000..4c9ea8c0cb7302d8bf2a03283d19ffcd56cad963 --- /dev/null +++ b/packages/coding-agent/src/experimental/services/models-provider.ts @@ -0,0 +1,143 @@ +import { type Context, defineFacet, type Facet, type MutableReplicatedState } from "@earendil-works/chord"; +import { BACKGROUND_CONTEXT } from "@earendil-works/chord/context"; +import type { AgentLane, ThinkingLevel } from "@earendil-works/pi-agent-core"; +import { getSupportedThinkingLevels } from "@earendil-works/pi-ai"; +import type { ModelRuntime } from "../../core/model-runtime.ts"; +import type { SettingsManager } from "../../core/settings-manager.ts"; +import { Models, type Models as ModelsService, type ModelsState } from "./models.ts"; + +export interface ModelsServiceRuntime { + readonly service: ModelsService; + activate(context: Context): Promise; +} + +export function createModelsService( + lane: AgentLane, + modelRuntime: ModelRuntime | undefined, + settingsManager: SettingsManager | undefined, + createState: (initial: ModelsState) => MutableReplicatedState, +): ModelsServiceRuntime { + let catalogRevision = 0; + const state = createState({ + catalog: { revision: 0, availableModels: [] }, + configuration: { model: null, thinkingLevel: "off" }, + refresh: { status: "idle" }, + }); + const readConfiguration = async (context: Context): Promise => { + const [selected, thinkingLevel] = await Promise.all([lane.getModel(context), lane.getThinkingLevel(context)]); + return { + model: selected === undefined ? null : { provider: selected.provider, modelId: selected.id }, + thinkingLevel, + }; + }; + const readThinkingLevels = async (context: Context): Promise => { + const selected = await lane.getModel(context); + return selected === undefined ? ["off"] : getSupportedThinkingLevels(selected); + }; + const readCatalog = async (context: Context): Promise => { + const selected = await lane.getModel(context); + const available = modelRuntime?.getAvailableSnapshot() ?? []; + const catalog = + selected === undefined || includesModel(available, selected) ? available : [...available, selected]; + catalogRevision += 1; + return { + revision: catalogRevision, + availableModels: catalog.map((model) => ({ + provider: model.provider, + modelId: model.id, + name: model.name, + reasoning: model.reasoning, + })), + }; + }; + const service: ModelsService = { + state, + async cycleThinking(context) { + const levels = await readThinkingLevels(context); + const current = await lane.getThinkingLevel(context); + const index = levels.indexOf(current); + const next = levels[(index + 1) % levels.length] ?? "off"; + await lane.setThinkingLevel(next, context); + state.state.configuration = await readConfiguration(context); + state.publish(context); + }, + async getThinkingLevels(context) { + return [...(await readThinkingLevels(context))]; + }, + async refresh(context) { + state.state.refresh = { status: "refreshing" }; + state.publish(context); + const refresh = modelRuntime?.refresh({ signal: context.abortSignal }); + if (refresh === undefined) { + const [catalog, configuration] = await Promise.all([readCatalog(context), readConfiguration(context)]); + state.state.catalog = catalog; + state.state.configuration = configuration; + state.state.refresh = { status: "done" }; + state.publish(context); + return; + } + const result = await refresh; + const errors = Object.fromEntries([...result.errors].map(([id, error]) => [id, error.message])); + const [catalog, configuration] = await Promise.all([readCatalog(context), readConfiguration(context)]); + state.state.catalog = catalog; + state.state.configuration = configuration; + state.state.refresh = Object.keys(errors).length === 0 ? { status: "done" } : { status: "warning", errors }; + state.publish(context); + }, + async select(model, context) { + const selected = modelRuntime?.getModel(model.provider, model.modelId); + if (selected === undefined) throw new Error(`Unknown model: ${model.provider}/${model.modelId}`); + await lane.setModel({ provider: selected.provider, modelId: selected.id }, context); + settingsManager?.setDefaultModelAndProvider(selected.provider, selected.id); + await settingsManager?.flush(); + state.state.configuration = await readConfiguration(context); + state.publish(context); + }, + async selectThinking(level, context) { + const levels = await readThinkingLevels(context); + if (!levels.includes(level)) { + throw new Error(`Thinking level ${level} is unavailable; choose one of: ${levels.join(", ")}`); + } + await lane.setThinkingLevel(level, context); + state.state.configuration = await readConfiguration(context); + state.publish(context); + }, + }; + return { + service, + async activate(context) { + const [catalog, configuration] = await Promise.all([readCatalog(context), readConfiguration(context)]); + state.state.catalog = catalog; + state.state.configuration = configuration; + state.state.refresh = { status: "idle" }; + state.publish(context); + }, + }; +} + +export function createModelsServiceFacet(options: { + readonly lane: AgentLane; + readonly modelRuntime: ModelRuntime | undefined; + readonly settingsManager?: SettingsManager; +}): Facet { + return defineFacet({ + id: "@pi/models", + setup(env) { + const runtime = createModelsService( + options.lane, + options.modelRuntime, + options.settingsManager, + env.replicatedState, + ); + env.provide(Models, runtime.service); + env.onActivate(() => runtime.activate(BACKGROUND_CONTEXT)); + }, + }); +} + +function includesModel( + models: readonly { readonly provider: string; readonly id: string }[], + selected: { readonly provider: string; readonly id: string }, +): boolean { + return models.some((model) => model.provider === selected.provider && model.id === selected.id); +} diff --git a/packages/coding-agent/src/experimental/services/models.ts b/packages/coding-agent/src/experimental/services/models.ts new file mode 100644 index 0000000000000000000000000000000000000000..87c037bc09423366cf660d2b643fb8716cfabace --- /dev/null +++ b/packages/coding-agent/src/experimental/services/models.ts @@ -0,0 +1,35 @@ +import { type Context, defineService, type ReplicatedState } from "@earendil-works/chord"; +import type { ThinkingLevel } from "@earendil-works/pi-agent-core"; + +export interface ModelRef { + provider: string; + modelId: string; +} + +export interface ModelSummary extends ModelRef { + name: string; + reasoning: boolean; +} + +export interface ModelsState { + catalog: { + revision: number; + availableModels: ModelSummary[]; + }; + configuration: { + model: ModelRef | null; + thinkingLevel: ThinkingLevel; + }; + refresh: { status: "idle" | "refreshing" | "done" } | { status: "warning"; errors: Record }; +} + +export interface Models { + readonly state: ReplicatedState; + cycleThinking(context: Context): Promise; + getThinkingLevels(context: Context): Promise; + refresh(context: Context): Promise; + select(model: ModelRef, context: Context): Promise; + selectThinking(level: ThinkingLevel, context: Context): Promise; +} + +export const Models = defineService("pi.models"); diff --git a/packages/coding-agent/src/experimental/services/plugins.ts b/packages/coding-agent/src/experimental/services/plugins.ts new file mode 100644 index 0000000000000000000000000000000000000000..55adf7dc5aac6f3560413ca12782309d1e837990 --- /dev/null +++ b/packages/coding-agent/src/experimental/services/plugins.ts @@ -0,0 +1,19 @@ +import { type Context, defineService, type JsonValue } from "@earendil-works/chord"; + +/** Server-built plugin generations available to presentations. */ +export interface PresentationPlugins { + prepareSession( + request: { readonly sessionId: string; readonly packagePaths: readonly string[] | null }, + context: Context, + ): Promise; + reload(context: Context): Promise; +} + +export const PresentationPlugins = defineService("pi.presentation-plugins"); + +/** Plugin facets hosted in the currently attached Session worker. */ +export interface SessionPlugins { + reload(context: Context): Promise; +} + +export const SessionPlugins = defineService("pi.session-plugins"); diff --git a/packages/coding-agent/src/experimental/services/presentation-ui.ts b/packages/coding-agent/src/experimental/services/presentation-ui.ts new file mode 100644 index 0000000000000000000000000000000000000000..11503eedd97554ffc38a693383753aec9283e3aa --- /dev/null +++ b/packages/coding-agent/src/experimental/services/presentation-ui.ts @@ -0,0 +1,20 @@ +import { type Context, defineService } from "@earendil-works/chord"; + +export interface PresentationSelectItem { + readonly value: string; + readonly label: string; + readonly description?: string; +} + +/** Narrow process-local UI capabilities available to presentation facets. */ +export interface PresentationUI { + select( + title: string, + items: readonly PresentationSelectItem[], + selectedValue: string | undefined, + context: Context, + ): Promise; + showStatus(message: string, context: Context): void; +} + +export const PresentationUI = defineService("pi.local.presentation-ui", { local: true }); diff --git a/packages/coding-agent/src/experimental/services/server.ts b/packages/coding-agent/src/experimental/services/server.ts new file mode 100644 index 0000000000000000000000000000000000000000..86cc7686307613e163cd99ac76cd354d1863af9c --- /dev/null +++ b/packages/coding-agent/src/experimental/services/server.ts @@ -0,0 +1,150 @@ +import { + type Context, + createRemoteServiceEndpoint, + type JsonValue, + RemoteServiceProvider, + replicatedState, +} from "@earendil-works/chord"; +import { BACKGROUND_CONTEXT } from "@earendil-works/chord/context"; +import type { RoutedServerServiceAttachment, RoutedServerServiceHost } from "@earendil-works/pi-server"; +import { PresentationPlugins } from "./plugins.ts"; +import { + type SessionCreateOptions, + SessionDirectory, + type SessionDirectoryState, + SessionManagement, + type SessionSummary, +} from "./sessions.ts"; + +export interface ExperimentalServerServices { + readonly host: RoutedServerServiceHost; + refresh(context?: Context): Promise; + dispose(): Promise; +} + +export async function createExperimentalServerServices(options: { + list(context: Context): Promise; + create(createOptions: SessionCreateOptions, context: Context): Promise; + remove(sessionId: string, context: Context): Promise; + prepareSessionPlugins( + sessionId: string, + packagePaths: readonly string[] | undefined, + context: Context, + ): Promise<{ readonly packagePaths: readonly string[]; readonly presentationPlugins: JsonValue }>; + reloadPresentationPlugins(packagePaths: readonly string[], context: Context): Promise; +}): Promise { + let revision = 1; + const directory = replicatedState({ + revision, + sessions: await options.list(BACKGROUND_CONTEXT), + }); + const attachments = new Set(); + let mutationTail = Promise.resolve(); + + const refreshNow = async (context: Context): Promise => { + const sessions = await options.list(context); + revision += 1; + directory.state.revision = revision; + directory.state.sessions = sessions; + directory.publish(context); + }; + const serialize = (operation: () => Promise): Promise => { + const result = mutationTail.catch(() => {}).then(operation); + mutationTail = result.then( + () => undefined, + () => undefined, + ); + return result; + }; + + return { + host: { + attachClient(presentation) { + let preparedPluginPackagePaths: readonly string[] | undefined; + const provider = new RemoteServiceProvider([ + { service: SessionDirectory, mode: "singleton" }, + { service: SessionManagement, mode: "singleton" }, + { service: PresentationPlugins, mode: "singleton" }, + ]); + provider.provide(SessionDirectory, { state: directory }); + provider.provide(PresentationPlugins, { + prepareSession: ({ sessionId, packagePaths }, context) => + serialize(async () => { + const selected = await options.prepareSessionPlugins( + sessionId, + packagePaths ?? undefined, + context, + ); + preparedPluginPackagePaths = selected.packagePaths; + return selected.presentationPlugins; + }), + reload: (context) => + serialize(() => { + if (preparedPluginPackagePaths === undefined) { + throw new Error("No Session plugin selection is prepared"); + } + return options.reloadPresentationPlugins(preparedPluginPackagePaths, context); + }), + }); + provider.provide(SessionManagement, { + create: (createOptions, context) => + serialize(async () => { + const created = await options.create(createOptions, context); + await refreshNow(context); + return created; + }), + remove: (sessionId, context) => + serialize(async () => { + await presentation.prepareSessionRemoval(sessionId, context); + await options.remove(sessionId, context); + await refreshNow(context); + }), + attach: (sessionId, context) => + serialize(async () => { + await presentation.attachSession(sessionId, context); + }), + detach: (context) => + serialize(async () => { + await presentation.detachSession(context); + preparedPluginPackagePaths = undefined; + }), + }); + const attachment = createProviderAttachment(provider, () => attachments.delete(attachment)); + attachments.add(attachment); + return attachment; + }, + }, + refresh: (context = BACKGROUND_CONTEXT) => serialize(() => refreshNow(context)), + async dispose() { + const releases = await Promise.allSettled( + [...attachments].map((attachment) => attachment.release(BACKGROUND_CONTEXT)), + ); + attachments.clear(); + await mutationTail; + const errors = releases.flatMap((result) => (result.status === "rejected" ? [result.reason] : [])); + if (errors.length === 1) throw errors[0]; + if (errors.length > 1) throw new AggregateError(errors, "Failed to release server service attachments"); + }, + }; +} + +function createProviderAttachment( + provider: RemoteServiceProvider, + onRelease: () => void, +): RoutedServerServiceAttachment { + const endpoint = createRemoteServiceEndpoint(provider); + let released = false; + return { + invokeService(call, publish, context) { + if (released) return Promise.reject(new Error("Server service attachment is released")); + return endpoint.invoke(call, publish, context); + }, + release() { + if (released) return; + released = true; + endpoint.dispose(); + provider.dispose(); + onRelease(); + }, + }; +} diff --git a/packages/coding-agent/src/experimental/services/sessions.ts b/packages/coding-agent/src/experimental/services/sessions.ts new file mode 100644 index 0000000000000000000000000000000000000000..79dbec2f5b6df538a1fe54f71c380489c9895df6 --- /dev/null +++ b/packages/coding-agent/src/experimental/services/sessions.ts @@ -0,0 +1,35 @@ +import { type Context, defineService, type ReplicatedState } from "@earendil-works/chord"; +import type { ServerId } from "@earendil-works/pi-protocol"; + +export interface SessionAddress { + serverId: ServerId; + sessionId: string; +} + +export interface SessionSummary extends SessionAddress { + createdAt: number; +} + +export interface SessionCreateOptions { + id?: string; +} + +export interface SessionDirectoryState { + revision: number; + sessions: SessionSummary[]; +} + +export interface SessionDirectory { + readonly state: ReplicatedState; +} + +export const SessionDirectory = defineService("pi.session-directory"); + +export interface SessionManagement { + create(options: SessionCreateOptions, context: Context): Promise; + remove(sessionId: string, context: Context): Promise; + attach(sessionId: string, context: Context): Promise; + detach(context: Context): Promise; +} + +export const SessionManagement = defineService("pi.session-management"); diff --git a/packages/coding-agent/src/experimental/services/slash-commands-provider.ts b/packages/coding-agent/src/experimental/services/slash-commands-provider.ts new file mode 100644 index 0000000000000000000000000000000000000000..68ddd1f88568ce10a936b1dcc587c4b19181c18b --- /dev/null +++ b/packages/coding-agent/src/experimental/services/slash-commands-provider.ts @@ -0,0 +1,225 @@ +import { defineFacet, type Facet, type JsonValue } from "@earendil-works/chord"; +import type { ThinkingLevel } from "@earendil-works/pi-agent-core"; +import { AgentController } from "./agent-controller.ts"; +import { type ModelSummary, Models, type Models as ModelsService } from "./models.ts"; +import { PresentationPlugins, SessionPlugins } from "./plugins.ts"; +import { PresentationUI } from "./presentation-ui.ts"; +import { type SlashCommandContribution, SlashCommands } from "./slash-commands.ts"; + +const THINKING_DESCRIPTIONS: Record = { + off: "No reasoning", + minimal: "Very brief reasoning", + low: "Light reasoning", + medium: "Moderate reasoning", + high: "Deep reasoning", + xhigh: "Extra-high reasoning", + max: "Maximum reasoning", +}; + +interface RegisteredSlashCommand { + readonly command: SlashCommandContribution; + closed: boolean; +} + +export class SlashCommandRegistry implements SlashCommands { + readonly #commands = new Map(); + readonly #listeners = new Set<(commands: readonly SlashCommandContribution[]) => void>(); + + register(command: SlashCommandContribution): () => void { + this.#validate(command); + if (this.#commands.has(command.name)) throw new Error(`Slash command /${command.name} is already registered`); + return this.#add(command); + } + + replace(command: SlashCommandContribution): () => void { + this.#validate(command); + return this.#add(command); + } + + list(): readonly SlashCommandContribution[] { + return Object.freeze([...this.#commands.values()].map((entries) => entries[0]!.command)); + } + + subscribe(listener: (commands: readonly SlashCommandContribution[]) => void): () => void { + this.#listeners.add(listener); + listener(this.list()); + return () => this.#listeners.delete(listener); + } + + #add(command: SlashCommandContribution): () => void { + const entry: RegisteredSlashCommand = { command: Object.freeze({ ...command }), closed: false }; + const entries = this.#commands.get(command.name) ?? []; + entries.push(entry); + this.#commands.set(command.name, entries); + if (entries.length === 1) this.#publish(); + return () => { + if (entry.closed) return; + entry.closed = true; + if (entries[0] !== entry) return; + while (entries[0]?.closed) entries.shift(); + if (entries.length === 0) this.#commands.delete(command.name); + this.#publish(); + }; + } + + #validate(command: SlashCommandContribution): void { + if (!/^[a-z0-9][a-z0-9:-]*$/u.test(command.name)) { + throw new TypeError(`Invalid slash command name: ${command.name}`); + } + } + + #publish(): void { + const commands = this.list(); + for (const listener of this.#listeners) listener(commands); + } +} + +export function createSlashCommandsRuntimeFacet(registry = new SlashCommandRegistry()): Facet { + return defineFacet({ + id: "@pi/slash-commands-runtime", + setup(env) { + env.provide(SlashCommands, registry); + }, + }); +} + +export function createBuiltInSlashCommandsFacet(options: { + reloadPresentationPlugins(data: JsonValue): Promise; +}): Facet { + return defineFacet({ + id: "@pi/slash-commands-builtin", + setup(env) { + const commands = env.use(SlashCommands); + const models = env.use(Models); + const controller = env.use(AgentController); + const ui = env.use(PresentationUI); + const presentationPlugins = env.use(PresentationPlugins); + const sessionPlugins = env.use(SessionPlugins); + env.onActivate(() => { + env.own(commands.replace(modelCommand(models, ui))); + env.own(commands.replace(thinkingCommand(models, ui))); + env.own(commands.replace(compactCommand(controller, ui))); + env.own( + commands.replace({ + name: "reload", + description: "Reload server-selected plugins", + async run(_args, context) { + ui.showStatus("Reloading plugins…", context); + const data = await presentationPlugins.reload(context); + await sessionPlugins.reload(context); + await options.reloadPresentationPlugins(data); + ui.showStatus("Reloaded plugins.", context); + return undefined; + }, + }), + ); + }); + }, + }); +} + +function modelCommand(models: ModelsService, ui: PresentationUI): SlashCommandContribution { + return { + name: "model", + description: "Select model", + argumentHint: "", + getArgumentCompletions(prefix) { + const normalized = prefix.toLowerCase(); + return (models.state.value?.catalog.availableModels ?? []) + .filter((model) => `${model.provider}/${model.modelId} ${model.name}`.toLowerCase().includes(normalized)) + .map((model) => ({ + value: `${model.provider}/${model.modelId}`, + label: model.modelId, + description: model.provider, + })); + }, + async run(args, context) { + const state = models.state.value; + if (state === undefined) throw new Error("Models service is not ready"); + let selected = exactModel(state.catalog.availableModels, args); + if (args.length > 0 && selected === undefined) { + throw new Error(`Unknown model: ${args}`); + } + if (selected === undefined) { + const value = await ui.select( + "Select model:", + state.catalog.availableModels.map((model) => ({ + value: `${model.provider}/${model.modelId}`, + label: + state.configuration.model?.provider === model.provider && + state.configuration.model.modelId === model.modelId + ? `${model.name} (selected)` + : model.name, + description: `${model.provider}/${model.modelId}`, + })), + state.configuration.model === null + ? undefined + : `${state.configuration.model.provider}/${state.configuration.model.modelId}`, + context, + ); + if (value === undefined) return undefined; + selected = exactModel(state.catalog.availableModels, value); + if (selected === undefined) throw new Error(`Unknown model: ${value}`); + } + await models.select({ provider: selected.provider, modelId: selected.modelId }, context); + ui.showStatus(`Selected ${selected.provider}/${selected.modelId}.`, context); + return undefined; + }, + }; +} + +function thinkingCommand(models: ModelsService, ui: PresentationUI): SlashCommandContribution { + return { + name: "thinking", + description: "Set thinking level", + argumentHint: "", + async run(args, context) { + const levels = await models.getThinkingLevels(context); + let selected = levels.find((level) => level === args.toLowerCase()); + if (args.length > 0 && selected === undefined) { + throw new Error(`Unknown thinking level "${args}". Available levels: ${levels.join(", ")}.`); + } + if (selected === undefined) { + const value = await ui.select( + "Select thinking level:", + levels.map((level) => ({ + value: level, + label: models.state.value?.configuration.thinkingLevel === level ? `${level} (selected)` : level, + description: THINKING_DESCRIPTIONS[level], + })), + models.state.value?.configuration.thinkingLevel, + context, + ); + if (value === undefined) return undefined; + selected = levels.find((level) => level === value); + if (selected === undefined) throw new Error(`Unknown thinking level: ${value}`); + } + await models.selectThinking(selected, context); + ui.showStatus(`Thinking level: ${selected}.`, context); + return undefined; + }, + }; +} + +function compactCommand(controller: AgentController, ui: PresentationUI): SlashCommandContribution { + return { + name: "compact", + description: "Manually compact the session context", + argumentHint: "", + run(args, context) { + ui.showStatus("Compacting…", context); + return controller.compact({ customInstructions: args.length === 0 ? null : args }, context); + }, + }; +} + +function exactModel(models: readonly ModelSummary[], query: string): ModelSummary | undefined { + if (query.length === 0) return undefined; + const normalized = query.toLowerCase(); + const matches = models.filter( + (model) => + `${model.provider}/${model.modelId}`.toLowerCase() === normalized || + model.modelId.toLowerCase() === normalized, + ); + return matches.length === 1 ? matches[0] : undefined; +} diff --git a/packages/coding-agent/src/experimental/services/slash-commands.ts b/packages/coding-agent/src/experimental/services/slash-commands.ts new file mode 100644 index 0000000000000000000000000000000000000000..62927dbb5f3038f284efc0df68229eda77ed88e8 --- /dev/null +++ b/packages/coding-agent/src/experimental/services/slash-commands.ts @@ -0,0 +1,30 @@ +import { type Context, defineService } from "@earendil-works/chord"; +import type { AgentOperationResponse, AgentQueueResponse } from "./agent-controller.ts"; + +export interface SlashCommandCompletion { + readonly value: string; + readonly label: string; + readonly description?: string; +} + +export type SlashCommandRunResult = AgentOperationResponse | AgentQueueResponse | undefined; + +export interface SlashCommandContribution { + readonly name: string; + readonly description?: string; + readonly argumentHint?: string; + getArgumentCompletions?( + argumentPrefix: string, + ): readonly SlashCommandCompletion[] | null | Promise; + run(args: string, context: Context): SlashCommandRunResult | Promise; +} + +export interface SlashCommands { + register(command: SlashCommandContribution): () => void; + /** Stage a same-name replacement while the previous facet generation retires. */ + replace(command: SlashCommandContribution): () => void; + list(): readonly SlashCommandContribution[]; + subscribe(listener: (commands: readonly SlashCommandContribution[]) => void): () => void; +} + +export const SlashCommands = defineService("pi.local.slash-commands", { local: true }); diff --git a/packages/coding-agent/src/experimental/services/transcript-provider.ts b/packages/coding-agent/src/experimental/services/transcript-provider.ts new file mode 100644 index 0000000000000000000000000000000000000000..8496f17dfeb2092fd1e2e2441ed9bf0cdce2330f --- /dev/null +++ b/packages/coding-agent/src/experimental/services/transcript-provider.ts @@ -0,0 +1,128 @@ +import { defineFacet, type Facet, type MutableReplicatedState } from "@earendil-works/chord"; +import { BACKGROUND_CONTEXT } from "@earendil-works/chord/context"; +import { + type AgentLane, + type HarnessEvent, + type LaneSnapshot, + type LaneTranscriptSnapshot, + type LaneWatchEvent, + reduceLaneSnapshot, + type WatchHandle, +} from "@earendil-works/pi-agent-core"; +import { Transcript, type Transcript as TranscriptService, type TranscriptState } from "./transcript.ts"; + +interface TranscriptRuntime { + readonly service: TranscriptService; + activate(): Promise; + dispose(): Promise; +} + +export function createTranscriptService( + lane: AgentLane, + createState: (initial: TranscriptState) => MutableReplicatedState, +): TranscriptRuntime { + const state = createState({ snapshot: null, event: null }); + let watch: WatchHandle | undefined; + let rebase: Promise | undefined; + let rebaseError: Error | undefined; + + const publishSnapshot = ( + next: LaneSnapshot, + event: LaneWatchEvent | null, + context: Parameters[0], + ): void => { + state.state.snapshot = next as LaneTranscriptSnapshot; + state.state.event = event; + state.publish(context); + }; + + const scheduleRebase = (context: Parameters[0]): void => { + if (rebase !== undefined) return; + const activeWatch = watch; + if (activeWatch === undefined) return; + const pending = (async () => { + const refreshed = await activeWatch.resnapshot(context); + publishSnapshot(refreshed, null, context); + })(); + rebase = pending; + void pending.then( + () => { + if (rebase === pending) rebase = undefined; + }, + (error: unknown) => { + rebaseError = error instanceof Error ? error : new Error(String(error)); + if (rebase === pending) rebase = undefined; + }, + ); + }; + + const onEvent = (event: HarnessEvent, context: Parameters[0]): void => { + if (rebaseError !== undefined) throw rebaseError; + const forwarded = toLaneWatchEvent(event); + if (forwarded === undefined) return; + const snapshot = state.state.snapshot; + if (snapshot === null) throw new Error("Transcript service is not active"); + if (reduceLaneSnapshot(snapshot, event) === "rebase") scheduleRebase(context); + state.state.event = forwarded; + state.publish(context); + }; + + return { + service: { state }, + async activate() { + if (watch !== undefined) throw new Error("Transcript service is already active"); + const opened = await lane.watch(BACKGROUND_CONTEXT); + watch = opened; + publishSnapshot(opened.snapshot, null, BACKGROUND_CONTEXT); + opened.start(onEvent); + }, + async dispose() { + let failure: unknown; + try { + await rebase; + } catch (error) { + failure = error; + } + watch?.unsubscribe(); + watch = undefined; + if (failure !== undefined) throw failure; + }, + }; +} + +export function createTranscriptServiceFacet(lane: AgentLane): Facet { + return defineFacet({ + id: "@pi/transcript", + setup(env) { + const runtime = createTranscriptService(lane, env.replicatedState); + env.provide(Transcript, runtime.service); + env.onActivate(() => runtime.activate()); + env.own(() => runtime.dispose()); + }, + }); +} + +function toLaneWatchEvent(event: HarnessEvent): LaneWatchEvent | undefined { + switch (event.type) { + case "handler_error": + case "turn_start": + case "turn_end": + case "value_update": + case "lane_created": + return undefined; + case "config_update": + if (event.property !== "model" && event.property !== "thinkingLevel" && event.property !== "activeTools") { + return undefined; + } + return event as LaneWatchEvent; + case "message_update": { + if (event.message.role !== "assistant") { + throw new TypeError("Harness message_update did not carry an assistant message"); + } + const { event: _providerEvent, ...update } = event; + return update as LaneWatchEvent; + } + default: + return event as LaneWatchEvent; + } +} diff --git a/packages/coding-agent/src/experimental/services/transcript.ts b/packages/coding-agent/src/experimental/services/transcript.ts new file mode 100644 index 0000000000000000000000000000000000000000..7e9bfed318474864941b1492d5b57b98eeadca26 --- /dev/null +++ b/packages/coding-agent/src/experimental/services/transcript.ts @@ -0,0 +1,15 @@ +import { defineService, type ReplicatedState } from "@earendil-works/chord"; +import type { LaneTranscriptSnapshot, LaneWatchEvent } from "@earendil-works/pi-agent-core"; + +export interface TranscriptState { + snapshot: LaneTranscriptSnapshot | null; + /** The source event is retained for presentation side effects; hydration does not replay it. */ + event: LaneWatchEvent | null; +} + +/** Coherent main-lane state replicated through Chord's operation stream. */ +export interface Transcript { + readonly state: ReplicatedState; +} + +export const Transcript = defineService("pi.transcript"); diff --git a/packages/coding-agent/src/experimental/services/worker.ts b/packages/coding-agent/src/experimental/services/worker.ts new file mode 100644 index 0000000000000000000000000000000000000000..39a8371720cf64a1724203d73c5bae5a644b35a0 --- /dev/null +++ b/packages/coding-agent/src/experimental/services/worker.ts @@ -0,0 +1,152 @@ +import { + type Context, + createFacetHost, + createRemoteServiceEndpoint, + createStaticFacetLoader, + defineFacet, + type FacetHost, + type FacetLoader, + type JsonValue, + type RemoteServiceEndpoint, + type ServiceCall, + type ServiceProviderUpdate, +} from "@earendil-works/chord"; +import type { AgentHarness, AgentLane } from "@earendil-works/pi-agent-core"; +import type { ModelRuntime } from "../../core/model-runtime.ts"; +import type { SettingsManager } from "../../core/settings-manager.ts"; +import { AgentController } from "./agent-controller.ts"; +import { createAgentController } from "./agent-controller-provider.ts"; +import { createModelsServiceFacet } from "./models-provider.ts"; +import { SessionPlugins } from "./plugins.ts"; +import { createTranscriptServiceFacet } from "./transcript-provider.ts"; + +export interface SessionWorkerRuntime { + readonly harness: AgentHarness; + readonly lane?: AgentLane; + readonly modelRuntime?: ModelRuntime; + readonly settingsManager?: SettingsManager; + readonly facetLoader?: FacetLoader; +} + +export interface WorkerServiceScope { + readonly serverConnectionId: string; + readonly attachmentId: string; +} + +interface ScopedServiceEndpoint { + readonly scope: WorkerServiceScope; + readonly endpoint: RemoteServiceEndpoint; +} + +export interface SessionWorkerServices { + invoke(call: ServiceCall, scope: WorkerServiceScope, context: Context): Promise; + removeSubscriptions(matches: (scope: WorkerServiceScope) => boolean): void; + dispose(): Promise; +} + +export async function createSessionWorkerServices(options: { + readonly lane: AgentLane; + readonly modelRuntime: ModelRuntime | undefined; + readonly settingsManager?: SettingsManager; + readonly facetLoader?: FacetLoader; + publish(scope: WorkerServiceScope, subscriptionId: string, update: ServiceProviderUpdate): Promise; +}): Promise { + const agentControllerRuntimeFacet = defineFacet({ + id: "@pi/agent-controller-runtime", + setup(env) { + env.provide(AgentController, createAgentController(options.lane)); + }, + }); + let reloadPlugins = (): Promise => Promise.reject(new Error("Session plugins are not ready")); + const pluginRuntimeFacet = defineFacet({ + id: "@pi/session-plugins-runtime", + setup(env) { + env.provide(SessionPlugins, { reload: () => reloadPlugins() }); + }, + }); + const builtins = await createStaticFacetLoader([ + agentControllerRuntimeFacet, + pluginRuntimeFacet, + createModelsServiceFacet(options), + createTranscriptServiceFacet(options.lane), + ]).load(); + const pluginLoader = options.facetLoader ?? createStaticFacetLoader([]); + let loadedPlugins = await pluginLoader.load(); + let facetHost: FacetHost; + try { + facetHost = await createFacetHost({ facets: [...builtins.facets, ...loadedPlugins.facets] }); + } catch (error) { + const cleanup = await Promise.allSettled([loadedPlugins.dispose(), builtins.dispose()]); + const cleanupErrors = cleanup.flatMap((result) => (result.status === "rejected" ? [result.reason] : [])); + if (cleanupErrors.length > 0) { + throw new AggregateError([error, ...cleanupErrors], "Session facets failed to start and clean up"); + } + throw error; + } + let reloadTail = Promise.resolve(); + reloadPlugins = () => { + const operation = reloadTail.then(async () => { + const candidate = await pluginLoader.load(); + try { + await facetHost.reload(candidate.facets); + } catch (error) { + try { + await candidate.dispose(); + } catch (cleanupError) { + throw new AggregateError([error, cleanupError], "Session plugin reload and cleanup failed"); + } + throw error; + } + const retired = loadedPlugins; + loadedPlugins = candidate; + await retired.dispose(); + }); + reloadTail = operation.catch(() => {}); + return operation; + }; + const provider = facetHost.services; + + const endpoints = new Map(); + const removeSubscriptions = (matches: (scope: WorkerServiceScope) => boolean): void => { + for (const [key, entry] of endpoints) { + if (!matches(entry.scope)) continue; + entry.endpoint.dispose(); + endpoints.delete(key); + } + }; + + return { + invoke(call, scope, context) { + const key = serviceScopeKey(scope); + let entry = endpoints.get(key); + if (entry === undefined) { + entry = { scope, endpoint: createRemoteServiceEndpoint(provider) }; + endpoints.set(key, entry); + } + return entry.endpoint.invoke( + call, + (subscriptionId, update) => options.publish(scope, subscriptionId, update), + context, + ); + }, + removeSubscriptions, + async dispose() { + removeSubscriptions(() => true); + await reloadTail; + const errors: unknown[] = []; + try { + await facetHost.dispose(); + } catch (error) { + errors.push(error); + } + const results = await Promise.allSettled([loadedPlugins.dispose(), builtins.dispose()]); + errors.push(...results.flatMap((result) => (result.status === "rejected" ? [result.reason] : []))); + if (errors.length === 1) throw errors[0]; + if (errors.length > 1) throw new AggregateError(errors, "Failed to dispose Session facets"); + }, + }; +} + +function serviceScopeKey(scope: WorkerServiceScope): string { + return `${scope.serverConnectionId}\0${scope.attachmentId}`; +} diff --git a/packages/coding-agent/src/modes/interactive/components/armin.ts b/packages/coding-agent/src/modes/interactive/components/armin.ts new file mode 100644 index 0000000000000000000000000000000000000000..209ca167f399c21f280dd362aee1a15b0df4c1ab --- /dev/null +++ b/packages/coding-agent/src/modes/interactive/components/armin.ts @@ -0,0 +1,382 @@ +/** + * Armin says hi! A fun easter egg with animated XBM art. + */ + +import type { Component, TUI } from "@earendil-works/pi-tui"; +import { theme } from "../theme/theme.ts"; + +// XBM image: 31x36 pixels, LSB first, 1=background, 0=foreground +const WIDTH = 31; +const HEIGHT = 36; +const BITS = [ + 0xff, 0xff, 0xff, 0x7f, 0xff, 0xf0, 0xff, 0x7f, 0xff, 0xed, 0xff, 0x7f, 0xff, 0xdb, 0xff, 0x7f, 0xff, 0xb7, 0xff, + 0x7f, 0xff, 0x77, 0xfe, 0x7f, 0x3f, 0xf8, 0xfe, 0x7f, 0xdf, 0xff, 0xfe, 0x7f, 0xdf, 0x3f, 0xfc, 0x7f, 0x9f, 0xc3, + 0xfb, 0x7f, 0x6f, 0xfc, 0xf4, 0x7f, 0xf7, 0x0f, 0xf7, 0x7f, 0xf7, 0xff, 0xf7, 0x7f, 0xf7, 0xff, 0xe3, 0x7f, 0xf7, + 0x07, 0xe8, 0x7f, 0xef, 0xf8, 0x67, 0x70, 0x0f, 0xff, 0xbb, 0x6f, 0xf1, 0x00, 0xd0, 0x5b, 0xfd, 0x3f, 0xec, 0x53, + 0xc1, 0xff, 0xef, 0x57, 0x9f, 0xfd, 0xee, 0x5f, 0x9f, 0xfc, 0xae, 0x5f, 0x1f, 0x78, 0xac, 0x5f, 0x3f, 0x00, 0x50, + 0x6c, 0x7f, 0x00, 0xdc, 0x77, 0xff, 0xc0, 0x3f, 0x78, 0xff, 0x01, 0xf8, 0x7f, 0xff, 0x03, 0x9c, 0x78, 0xff, 0x07, + 0x8c, 0x7c, 0xff, 0x0f, 0xce, 0x78, 0xff, 0xff, 0xcf, 0x7f, 0xff, 0xff, 0xcf, 0x78, 0xff, 0xff, 0xdf, 0x78, 0xff, + 0xff, 0xdf, 0x7d, 0xff, 0xff, 0x3f, 0x7e, 0xff, 0xff, 0xff, 0x7f, +]; + +const BYTES_PER_ROW = Math.ceil(WIDTH / 8); +const DISPLAY_HEIGHT = Math.ceil(HEIGHT / 2); // Half-block rendering + +type Effect = "typewriter" | "scanline" | "rain" | "fade" | "crt" | "glitch" | "dissolve"; + +const EFFECTS: Effect[] = ["typewriter", "scanline", "rain", "fade", "crt", "glitch", "dissolve"]; + +// Get pixel at (x, y): true = foreground, false = background +function getPixel(x: number, y: number): boolean { + if (y >= HEIGHT) return false; + const byteIndex = y * BYTES_PER_ROW + Math.floor(x / 8); + const bitIndex = x % 8; + return ((BITS[byteIndex] >> bitIndex) & 1) === 0; +} + +// Get the character for a cell (2 vertical pixels packed) +function getChar(x: number, row: number): string { + const upper = getPixel(x, row * 2); + const lower = getPixel(x, row * 2 + 1); + if (upper && lower) return "█"; + if (upper) return "▀"; + if (lower) return "▄"; + return " "; +} + +// Build the final image grid +function buildFinalGrid(): string[][] { + const grid: string[][] = []; + for (let row = 0; row < DISPLAY_HEIGHT; row++) { + const line: string[] = []; + for (let x = 0; x < WIDTH; x++) { + line.push(getChar(x, row)); + } + grid.push(line); + } + return grid; +} + +export class ArminComponent implements Component { + private ui: TUI; + private interval: ReturnType | null = null; + private effect: Effect; + private finalGrid: string[][]; + private currentGrid: string[][]; + private effectState: Record = {}; + private cachedLines: string[] = []; + private cachedWidth = 0; + private gridVersion = 0; + private cachedVersion = -1; + + constructor(ui: TUI) { + this.ui = ui; + this.effect = EFFECTS[Math.floor(Math.random() * EFFECTS.length)]; + this.finalGrid = buildFinalGrid(); + this.currentGrid = this.createEmptyGrid(); + + this.initEffect(); + this.startAnimation(); + } + + invalidate(): void { + this.cachedWidth = 0; + } + + render(width: number): string[] { + if (width === this.cachedWidth && this.cachedVersion === this.gridVersion) { + return this.cachedLines; + } + + const padding = 1; + const availableWidth = width - padding; + + this.cachedLines = this.currentGrid.map((row) => { + // Clip row to available width before applying color + const clipped = row.slice(0, availableWidth).join(""); + const padRight = Math.max(0, width - padding - clipped.length); + return ` ${theme.fg("accent", clipped)}${" ".repeat(padRight)}`; + }); + + // Add "ARMIN SAYS HI" at the end + const message = "ARMIN SAYS HI"; + const msgPadRight = Math.max(0, width - padding - message.length); + this.cachedLines.push(` ${theme.fg("accent", message)}${" ".repeat(msgPadRight)}`); + + this.cachedWidth = width; + this.cachedVersion = this.gridVersion; + + return this.cachedLines; + } + + private createEmptyGrid(): string[][] { + return Array.from({ length: DISPLAY_HEIGHT }, () => Array(WIDTH).fill(" ")); + } + + private initEffect(): void { + switch (this.effect) { + case "typewriter": + this.effectState = { pos: 0 }; + break; + case "scanline": + this.effectState = { row: 0 }; + break; + case "rain": + // Track falling position for each column + this.effectState = { + drops: Array.from({ length: WIDTH }, () => ({ + y: -Math.floor(Math.random() * DISPLAY_HEIGHT * 2), + settled: 0, + })), + }; + break; + case "fade": { + // Shuffle all pixel positions + const positions: [number, number][] = []; + for (let row = 0; row < DISPLAY_HEIGHT; row++) { + for (let x = 0; x < WIDTH; x++) { + positions.push([row, x]); + } + } + // Fisher-Yates shuffle + for (let i = positions.length - 1; i > 0; i--) { + const j = Math.floor(Math.random() * (i + 1)); + [positions[i], positions[j]] = [positions[j], positions[i]]; + } + this.effectState = { positions, idx: 0 }; + break; + } + case "crt": + this.effectState = { expansion: 0 }; + break; + case "glitch": + this.effectState = { phase: 0, glitchFrames: 8 }; + break; + case "dissolve": { + // Start with random noise + this.currentGrid = Array.from({ length: DISPLAY_HEIGHT }, () => + Array.from({ length: WIDTH }, () => { + const chars = [" ", "░", "▒", "▓", "█", "▀", "▄"]; + return chars[Math.floor(Math.random() * chars.length)]; + }), + ); + // Shuffle positions for gradual resolve + const dissolvePositions: [number, number][] = []; + for (let row = 0; row < DISPLAY_HEIGHT; row++) { + for (let x = 0; x < WIDTH; x++) { + dissolvePositions.push([row, x]); + } + } + for (let i = dissolvePositions.length - 1; i > 0; i--) { + const j = Math.floor(Math.random() * (i + 1)); + [dissolvePositions[i], dissolvePositions[j]] = [dissolvePositions[j], dissolvePositions[i]]; + } + this.effectState = { positions: dissolvePositions, idx: 0 }; + break; + } + } + } + + private startAnimation(): void { + const fps = this.effect === "glitch" ? 60 : 30; + this.interval = setInterval(() => { + const done = this.tickEffect(); + this.updateDisplay(); + this.ui.requestRender(); + if (done) { + this.stopAnimation(); + } + }, 1000 / fps); + } + + private stopAnimation(): void { + if (this.interval) { + clearInterval(this.interval); + this.interval = null; + } + } + + private tickEffect(): boolean { + switch (this.effect) { + case "typewriter": + return this.tickTypewriter(); + case "scanline": + return this.tickScanline(); + case "rain": + return this.tickRain(); + case "fade": + return this.tickFade(); + case "crt": + return this.tickCrt(); + case "glitch": + return this.tickGlitch(); + case "dissolve": + return this.tickDissolve(); + default: + return true; + } + } + + private tickTypewriter(): boolean { + const state = this.effectState as { pos: number }; + const pixelsPerFrame = 3; + + for (let i = 0; i < pixelsPerFrame; i++) { + const row = Math.floor(state.pos / WIDTH); + const x = state.pos % WIDTH; + if (row >= DISPLAY_HEIGHT) return true; + this.currentGrid[row][x] = this.finalGrid[row][x]; + state.pos++; + } + return false; + } + + private tickScanline(): boolean { + const state = this.effectState as { row: number }; + if (state.row >= DISPLAY_HEIGHT) return true; + + // Copy row + for (let x = 0; x < WIDTH; x++) { + this.currentGrid[state.row][x] = this.finalGrid[state.row][x]; + } + state.row++; + return false; + } + + private tickRain(): boolean { + const state = this.effectState as { + drops: { y: number; settled: number }[]; + }; + + let allSettled = true; + this.currentGrid = this.createEmptyGrid(); + + for (let x = 0; x < WIDTH; x++) { + const drop = state.drops[x]; + + // Draw settled pixels + for (let row = DISPLAY_HEIGHT - 1; row >= DISPLAY_HEIGHT - drop.settled; row--) { + if (row >= 0) { + this.currentGrid[row][x] = this.finalGrid[row][x]; + } + } + + // Check if this column is done + if (drop.settled >= DISPLAY_HEIGHT) continue; + + allSettled = false; + + // Find the target row for this column (lowest non-space pixel) + let targetRow = -1; + for (let row = DISPLAY_HEIGHT - 1 - drop.settled; row >= 0; row--) { + if (this.finalGrid[row][x] !== " ") { + targetRow = row; + break; + } + } + + // Move drop down + drop.y++; + + // Draw falling drop + if (drop.y >= 0 && drop.y < DISPLAY_HEIGHT) { + if (targetRow >= 0 && drop.y >= targetRow) { + // Settle + drop.settled = DISPLAY_HEIGHT - targetRow; + drop.y = -Math.floor(Math.random() * 5) - 1; + } else { + // Still falling + this.currentGrid[drop.y][x] = "▓"; + } + } + } + + return allSettled; + } + + private tickFade(): boolean { + const state = this.effectState as { positions: [number, number][]; idx: number }; + const pixelsPerFrame = 15; + + for (let i = 0; i < pixelsPerFrame; i++) { + if (state.idx >= state.positions.length) return true; + const [row, x] = state.positions[state.idx]; + this.currentGrid[row][x] = this.finalGrid[row][x]; + state.idx++; + } + return false; + } + + private tickCrt(): boolean { + const state = this.effectState as { expansion: number }; + const midRow = Math.floor(DISPLAY_HEIGHT / 2); + + this.currentGrid = this.createEmptyGrid(); + + // Draw from middle expanding outward + const top = midRow - state.expansion; + const bottom = midRow + state.expansion; + + for (let row = Math.max(0, top); row <= Math.min(DISPLAY_HEIGHT - 1, bottom); row++) { + for (let x = 0; x < WIDTH; x++) { + this.currentGrid[row][x] = this.finalGrid[row][x]; + } + } + + state.expansion++; + return state.expansion > DISPLAY_HEIGHT; + } + + private tickGlitch(): boolean { + const state = this.effectState as { phase: number; glitchFrames: number }; + + if (state.phase < state.glitchFrames) { + // Glitch phase: show corrupted version + this.currentGrid = this.finalGrid.map((row) => { + const offset = Math.floor(Math.random() * 7) - 3; + const glitchRow = [...row]; + + // Random horizontal offset + if (Math.random() < 0.3) { + const shifted = glitchRow.slice(offset).concat(glitchRow.slice(0, offset)); + return shifted.slice(0, WIDTH); + } + + // Random vertical swap + if (Math.random() < 0.2) { + const swapRow = Math.floor(Math.random() * DISPLAY_HEIGHT); + return [...this.finalGrid[swapRow]]; + } + + return glitchRow; + }); + state.phase++; + return false; + } + + // Final frame: show clean image + this.currentGrid = this.finalGrid.map((row) => [...row]); + return true; + } + + private tickDissolve(): boolean { + const state = this.effectState as { positions: [number, number][]; idx: number }; + const pixelsPerFrame = 20; + + for (let i = 0; i < pixelsPerFrame; i++) { + if (state.idx >= state.positions.length) return true; + const [row, x] = state.positions[state.idx]; + this.currentGrid[row][x] = this.finalGrid[row][x]; + state.idx++; + } + return false; + } + + private updateDisplay(): void { + this.gridVersion++; + } + + dispose(): void { + this.stopAnimation(); + } +} diff --git a/packages/coding-agent/src/modes/interactive/components/bash-execution.ts b/packages/coding-agent/src/modes/interactive/components/bash-execution.ts new file mode 100644 index 0000000000000000000000000000000000000000..b46a9d0b96fb40d2065ee4da67d6247e890b9d66 --- /dev/null +++ b/packages/coding-agent/src/modes/interactive/components/bash-execution.ts @@ -0,0 +1,220 @@ +/** + * Component for displaying bash command execution with streaming output. + */ + +import { Container, Loader, Spacer, Text, type TUI } from "@earendil-works/pi-tui"; +import { + DEFAULT_MAX_BYTES, + DEFAULT_MAX_LINES, + type TruncationResult, + truncateTail, +} from "../../../core/tools/truncate.ts"; +import { stripAnsi } from "../../../utils/ansi.ts"; +import { theme } from "../theme/theme.ts"; +import { DynamicBorder } from "./dynamic-border.ts"; +import { keyHint, keyText } from "./keybinding-hints.ts"; +import { truncateToVisualLines } from "./visual-truncate.ts"; + +// Preview line limit when not expanded (matches tool execution behavior) +const PREVIEW_LINES = 20; + +export class BashExecutionComponent extends Container { + private command: string; + private outputLines: string[] = []; + private status: "running" | "complete" | "cancelled" | "error" = "running"; + private exitCode: number | undefined = undefined; + private loader: Loader; + private truncationResult?: TruncationResult; + private fullOutputPath?: string; + private expanded = false; + private contentContainer: Container; + + constructor(command: string, ui: TUI, excludeFromContext = false) { + super(); + this.command = command; + + // Use dim border for excluded-from-context commands (!! prefix) + const colorKey = excludeFromContext ? "dim" : "bashMode"; + const borderColor = (str: string) => theme.fg(colorKey, str); + + // Add spacer + this.addChild(new Spacer(1)); + + // Top border + this.addChild(new DynamicBorder(borderColor)); + + // Content container (holds dynamic content between borders) + this.contentContainer = new Container(); + this.addChild(this.contentContainer); + + // Command header + const header = new Text(theme.fg(colorKey, theme.bold(`$ ${command}`)), 1, 0); + this.contentContainer.addChild(header); + + // Loader + this.loader = new Loader( + ui, + (spinner) => theme.fg(colorKey, spinner), + (text) => theme.fg("muted", text), + `Running... (${keyText("tui.select.cancel")} to cancel)`, // Plain text for loader + ); + this.contentContainer.addChild(this.loader); + + // Bottom border + this.addChild(new DynamicBorder(borderColor)); + } + + /** + * Set whether the output is expanded (shows full output) or collapsed (preview only). + */ + setExpanded(expanded: boolean): void { + this.expanded = expanded; + this.updateDisplay(); + } + + override invalidate(): void { + super.invalidate(); + this.updateDisplay(); + } + + appendOutput(chunk: string): void { + // Strip ANSI codes and normalize line endings + // Note: binary data is already sanitized in tui-renderer.ts executeBashCommand + const clean = stripAnsi(chunk).replace(/\r\n/g, "\n").replace(/\r/g, "\n"); + + // Append to output lines + const newLines = clean.split("\n"); + if (this.outputLines.length > 0 && newLines.length > 0) { + // Append first chunk to last line (incomplete line continuation) + this.outputLines[this.outputLines.length - 1] += newLines[0]; + this.outputLines.push(...newLines.slice(1)); + } else { + this.outputLines.push(...newLines); + } + + this.updateDisplay(); + } + + setComplete( + exitCode: number | undefined, + cancelled: boolean, + truncationResult?: TruncationResult, + fullOutputPath?: string, + ): void { + this.exitCode = exitCode; + this.status = cancelled + ? "cancelled" + : exitCode !== 0 && exitCode !== undefined && exitCode !== null + ? "error" + : "complete"; + this.truncationResult = truncationResult; + this.fullOutputPath = fullOutputPath; + + // Stop loader + this.loader.stop(); + + this.updateDisplay(); + } + + private updateDisplay(): void { + // Apply truncation for LLM context limits (same limits as bash tool) + const fullOutput = this.outputLines.join("\n"); + const contextTruncation = truncateTail(fullOutput, { + maxLines: DEFAULT_MAX_LINES, + maxBytes: DEFAULT_MAX_BYTES, + }); + + // Get the lines to potentially display (after context truncation) + const availableLines = contextTruncation.content ? contextTruncation.content.split("\n") : []; + + // Apply preview truncation based on expanded state + const previewLogicalLines = availableLines.slice(-PREVIEW_LINES); + const hiddenLineCount = availableLines.length - previewLogicalLines.length; + + // Rebuild content container + this.contentContainer.clear(); + + // Command header + const header = new Text(theme.fg("bashMode", theme.bold(`$ ${this.command}`)), 1, 0); + this.contentContainer.addChild(header); + + // Output + if (availableLines.length > 0) { + if (this.expanded) { + // Show all lines + const displayText = availableLines.map((line) => theme.fg("muted", line)).join("\n"); + this.contentContainer.addChild(new Text(`\n${displayText}`, 1, 0)); + } else { + // Use shared visual truncation utility with width-aware caching + const styledOutput = previewLogicalLines.map((line) => theme.fg("muted", line)).join("\n"); + const styledInput = `\n${styledOutput}`; + let cachedWidth: number | undefined; + let cachedLines: string[] | undefined; + this.contentContainer.addChild({ + render: (width: number) => { + if (cachedLines === undefined || cachedWidth !== width) { + const result = truncateToVisualLines(styledInput, PREVIEW_LINES, width, 1); + cachedLines = result.visualLines; + cachedWidth = width; + } + return cachedLines ?? []; + }, + invalidate: () => { + cachedWidth = undefined; + cachedLines = undefined; + }, + }); + } + } + + // Loader or status + if (this.status === "running") { + this.contentContainer.addChild(this.loader); + } else { + const statusParts: string[] = []; + + // Show how many lines are hidden (collapsed preview) + if (hiddenLineCount > 0) { + if (this.expanded) { + statusParts.push( + `${theme.fg("muted", "(")}${keyHint("app.tools.expand", "to collapse")}${theme.fg("muted", ")")}`, + ); + } else { + statusParts.push( + `${theme.fg("muted", `... ${hiddenLineCount} more lines (`)}${keyHint("app.tools.expand", "to expand")}${theme.fg("muted", ")")}`, + ); + } + } + + if (this.status === "cancelled") { + statusParts.push(theme.fg("warning", "(cancelled)")); + } else if (this.status === "error") { + statusParts.push(theme.fg("error", `(exit ${this.exitCode})`)); + } + + // Add truncation warning (context truncation, not preview truncation) + const wasTruncated = this.truncationResult?.truncated || contextTruncation.truncated; + if (wasTruncated && this.fullOutputPath) { + statusParts.push(theme.fg("warning", `Output truncated. Full output: ${this.fullOutputPath}`)); + } + + if (statusParts.length > 0) { + this.contentContainer.addChild(new Text(`\n${statusParts.join("\n")}`, 1, 0)); + } + } + } + + /** + * Get the raw output for creating BashExecutionMessage. + */ + getOutput(): string { + return this.outputLines.join("\n"); + } + + /** + * Get the command that was executed. + */ + getCommand(): string { + return this.command; + } +} diff --git a/packages/coding-agent/src/modes/interactive/components/branch-summary-message.ts b/packages/coding-agent/src/modes/interactive/components/branch-summary-message.ts new file mode 100644 index 0000000000000000000000000000000000000000..af85115f02192b2ff0d141d7c34af8a679f724c3 --- /dev/null +++ b/packages/coding-agent/src/modes/interactive/components/branch-summary-message.ts @@ -0,0 +1,58 @@ +import { Box, Markdown, type MarkdownTheme, Spacer, Text } from "@earendil-works/pi-tui"; +import type { BranchSummaryMessage } from "../../../core/messages.ts"; +import { getMarkdownTheme, theme } from "../theme/theme.ts"; +import { keyText } from "./keybinding-hints.ts"; + +/** + * Component that renders a branch summary message with collapsed/expanded state. + * Uses same background color as custom messages for visual consistency. + */ +export class BranchSummaryMessageComponent extends Box { + private expanded = false; + private message: BranchSummaryMessage; + private markdownTheme: MarkdownTheme; + + constructor(message: BranchSummaryMessage, markdownTheme: MarkdownTheme = getMarkdownTheme()) { + super(1, 1, (t) => theme.bg("customMessageBg", t)); + this.message = message; + this.markdownTheme = markdownTheme; + this.updateDisplay(); + } + + setExpanded(expanded: boolean): void { + this.expanded = expanded; + this.updateDisplay(); + } + + override invalidate(): void { + super.invalidate(); + this.updateDisplay(); + } + + private updateDisplay(): void { + this.clear(); + + const label = theme.fg("customMessageLabel", `\x1b[1m[branch]\x1b[22m`); + this.addChild(new Text(label, 0, 0)); + this.addChild(new Spacer(1)); + + if (this.expanded) { + const header = "**Branch Summary**\n\n"; + this.addChild( + new Markdown(header + this.message.summary, 0, 0, this.markdownTheme, { + color: (text: string) => theme.fg("customMessageText", text), + }), + ); + } else { + this.addChild( + new Text( + theme.fg("customMessageText", "Branch summary (") + + theme.fg("dim", keyText("app.tools.expand")) + + theme.fg("customMessageText", " to expand)"), + 0, + 0, + ), + ); + } + } +} diff --git a/packages/coding-agent/src/modes/interactive/components/compaction-summary-message.ts b/packages/coding-agent/src/modes/interactive/components/compaction-summary-message.ts new file mode 100644 index 0000000000000000000000000000000000000000..cad613d762453fc67c40090f9dbffa4034930f86 --- /dev/null +++ b/packages/coding-agent/src/modes/interactive/components/compaction-summary-message.ts @@ -0,0 +1,59 @@ +import { Box, Markdown, type MarkdownTheme, Spacer, Text } from "@earendil-works/pi-tui"; +import type { CompactionSummaryMessage } from "../../../core/messages.ts"; +import { getMarkdownTheme, theme } from "../theme/theme.ts"; +import { keyText } from "./keybinding-hints.ts"; + +/** + * Component that renders a compaction message with collapsed/expanded state. + * Uses same background color as custom messages for visual consistency. + */ +export class CompactionSummaryMessageComponent extends Box { + private expanded = false; + private message: CompactionSummaryMessage; + private markdownTheme: MarkdownTheme; + + constructor(message: CompactionSummaryMessage, markdownTheme: MarkdownTheme = getMarkdownTheme()) { + super(1, 1, (t) => theme.bg("customMessageBg", t)); + this.message = message; + this.markdownTheme = markdownTheme; + this.updateDisplay(); + } + + setExpanded(expanded: boolean): void { + this.expanded = expanded; + this.updateDisplay(); + } + + override invalidate(): void { + super.invalidate(); + this.updateDisplay(); + } + + private updateDisplay(): void { + this.clear(); + + const tokenStr = this.message.tokensBefore.toLocaleString(); + const label = theme.fg("customMessageLabel", `\x1b[1m[compaction]\x1b[22m`); + this.addChild(new Text(label, 0, 0)); + this.addChild(new Spacer(1)); + + if (this.expanded) { + const header = `**Compacted from ${tokenStr} tokens**\n\n`; + this.addChild( + new Markdown(header + this.message.summary, 0, 0, this.markdownTheme, { + color: (text: string) => theme.fg("customMessageText", text), + }), + ); + } else { + this.addChild( + new Text( + theme.fg("customMessageText", `Compacted from ${tokenStr} tokens (`) + + theme.fg("dim", keyText("app.tools.expand")) + + theme.fg("customMessageText", " to expand)"), + 0, + 0, + ), + ); + } + } +} diff --git a/packages/coding-agent/src/modes/interactive/components/config-selector.ts b/packages/coding-agent/src/modes/interactive/components/config-selector.ts new file mode 100644 index 0000000000000000000000000000000000000000..217f261a926c98a37ccdf3096e11a0b0c589a96e --- /dev/null +++ b/packages/coding-agent/src/modes/interactive/components/config-selector.ts @@ -0,0 +1,942 @@ +/** + * TUI component for managing package resources (enable/disable) + */ + +import { homedir } from "node:os"; +import { basename, dirname, join, relative } from "node:path"; +import { + type Component, + Container, + type Focusable, + getKeybindings, + Input, + matchesKey, + Spacer, + truncateToWidth, + visibleWidth, +} from "@earendil-works/pi-tui"; +import { CONFIG_DIR_NAME } from "../../../config.ts"; +import type { PathMetadata, ResolvedPaths, ResolvedResource } from "../../../core/package-manager.ts"; +import type { PackageSource, SettingsManager } from "../../../core/settings-manager.ts"; +import { canonicalizePath, isLocalPath, resolvePath } from "../../../utils/paths.ts"; +import { theme } from "../theme/theme.ts"; +import { DynamicBorder } from "./dynamic-border.ts"; +import { keyHint, rawKeyHint } from "./keybinding-hints.ts"; + +type ResourceType = "extensions" | "skills" | "prompts" | "themes"; +type ConfigWriteScope = "global" | "project"; +type SettingsScope = "user" | "project"; +type ProjectOverrideState = "inherit" | "load" | "unload"; +export type ScopedResolvedPaths = Record; + +const RESOURCE_TYPES = ["extensions", "skills", "prompts", "themes"] as const satisfies readonly ResourceType[]; + +const RESOURCE_TYPE_LABELS: Record = { + extensions: "Extensions", + skills: "Skills", + prompts: "Prompts", + themes: "Themes", +}; + +interface ResourceItem { + path: string; + enabled: boolean; + metadata: PathMetadata; + resourceType: ResourceType; + displayName: string; + groupKey: string; + subgroupKey: string; +} + +interface ResourceSubgroup { + type: ResourceType; + label: string; + items: ResourceItem[]; +} + +interface ResourceGroup { + key: string; + label: string; + scope: "user" | "project" | "temporary"; + origin: "package" | "top-level"; + source: string; + subgroups: ResourceSubgroup[]; +} + +function formatBaseDir(baseDir: string): string { + const homeDir = homedir(); + let displayPath: string; + + if (baseDir === homeDir) { + displayPath = "~"; + } else if (baseDir.startsWith(homeDir)) { + // Replace home prefix with ~, normalize separators for display + const rest = baseDir.slice(homeDir.length); + displayPath = `~${rest.replace(/\\/g, "/")}`; + } else { + displayPath = baseDir.replace(/\\/g, "/"); + } + + return displayPath.endsWith("/") ? displayPath : `${displayPath}/`; +} + +function getGroupLabel(metadata: PathMetadata, agentDir: string): string { + if (metadata.origin === "package") { + return `${metadata.source} (${metadata.scope})`; + } + // Top-level resources + if (metadata.source === "auto") { + if (metadata.baseDir) { + return metadata.scope === "user" + ? `User (${formatBaseDir(metadata.baseDir)})` + : `Project (${formatBaseDir(metadata.baseDir)})`; + } + return metadata.scope === "user" ? `User (${formatBaseDir(agentDir)})` : `Project (${CONFIG_DIR_NAME}/)`; + } + return metadata.scope === "user" ? "User settings" : "Project settings"; +} + +function buildGroups(resolved: ResolvedPaths, agentDir: string): ResourceGroup[] { + const groupMap = new Map(); + + const addToGroup = (resources: ResolvedResource[], resourceType: ResourceType) => { + for (const res of resources) { + const { path, enabled, metadata } = res; + const groupKey = `${metadata.origin}:${metadata.scope}:${metadata.source}:${metadata.baseDir ?? ""}`; + + if (!groupMap.has(groupKey)) { + groupMap.set(groupKey, { + key: groupKey, + label: getGroupLabel(metadata, agentDir), + scope: metadata.scope, + origin: metadata.origin, + source: metadata.source, + subgroups: [], + }); + } + + const group = groupMap.get(groupKey)!; + const subgroupKey = `${groupKey}:${resourceType}`; + + let subgroup = group.subgroups.find((sg) => sg.type === resourceType); + if (!subgroup) { + subgroup = { + type: resourceType, + label: RESOURCE_TYPE_LABELS[resourceType], + items: [], + }; + group.subgroups.push(subgroup); + } + + const fileName = basename(path); + const parentFolder = basename(dirname(path)); + let displayName: string; + if (resourceType === "extensions" && parentFolder !== "extensions") { + displayName = `${parentFolder}/${fileName}`; + } else if (resourceType === "skills" && fileName === "SKILL.md") { + displayName = parentFolder; + } else { + displayName = fileName; + } + subgroup.items.push({ + path, + enabled, + metadata, + resourceType, + displayName, + groupKey, + subgroupKey, + }); + } + }; + + addToGroup(resolved.extensions, "extensions"); + addToGroup(resolved.skills, "skills"); + addToGroup(resolved.prompts, "prompts"); + addToGroup(resolved.themes, "themes"); + + // Sort groups: packages first, then top-level; user before project + const groups = Array.from(groupMap.values()); + groups.sort((a, b) => { + if (a.origin !== b.origin) { + return a.origin === "package" ? -1 : 1; + } + if (a.scope !== b.scope) { + return a.scope === "user" ? -1 : 1; + } + return a.source.localeCompare(b.source); + }); + + // Sort subgroups within each group by type order, and items by name + const typeOrder: Record = { extensions: 0, skills: 1, prompts: 2, themes: 3 }; + for (const group of groups) { + group.subgroups.sort((a, b) => typeOrder[a.type] - typeOrder[b.type]); + for (const subgroup of group.subgroups) { + subgroup.items.sort((a, b) => a.displayName.localeCompare(b.displayName)); + } + } + + return groups; +} + +type FlatEntry = + | { type: "group"; group: ResourceGroup } + | { type: "subgroup"; subgroup: ResourceSubgroup; group: ResourceGroup } + | { type: "item"; item: ResourceItem }; + +class ConfigSelectorHeader implements Component { + private writeScope: ConfigWriteScope; + private projectModeAvailable: boolean; + + constructor(writeScope: ConfigWriteScope, projectModeAvailable: boolean) { + this.writeScope = writeScope; + this.projectModeAvailable = projectModeAvailable; + } + + setWriteScope(writeScope: ConfigWriteScope): void { + this.writeScope = writeScope; + } + + invalidate(): void {} + + render(width: number): string[] { + const title = theme.bold(this.writeScope === "project" ? "Project Local Resources" : "Global Resources"); + const sep = theme.fg("muted", " · "); + const switchHint = this.projectModeAvailable ? keyHint("tui.input.tab", "switch mode") + sep : ""; + const actionHint = + this.writeScope === "project" ? rawKeyHint("space", "cycle inherit/+/-") : rawKeyHint("space", "toggle"); + const hint = switchHint + actionHint + sep + rawKeyHint("esc", "close"); + const spacing = Math.max(1, width - visibleWidth(title) - visibleWidth(hint)); + const scopeHint = + this.writeScope === "project" + ? theme.fg("muted", `${CONFIG_DIR_NAME}/settings.json · inherited global resources are dimmed`) + : theme.fg("muted", `~/${CONFIG_DIR_NAME}/agent/settings.json`); + + return [ + truncateToWidth(`${title}${" ".repeat(spacing)}${hint}`, width, ""), + truncateToWidth(scopeHint, width, ""), + ]; + } +} + +class ResourceList implements Component, Focusable { + private groupsByScope: Record; + private flatItems: FlatEntry[] = []; + private filteredItems: FlatEntry[] = []; + private selectedIndex = 0; + private searchInput: Input; + private maxVisible: number; + private settingsManager: SettingsManager; + private cwd: string; + private agentDir: string; + private writeScope: ConfigWriteScope; + private inheritedEnabledByKey: Map; + + public onCancel?: () => void; + public onExit?: () => void; + public onToggle?: (item: ResourceItem, newEnabled: boolean) => void; + public onSwitchMode?: () => void; + + private _focused = false; + get focused(): boolean { + return this._focused; + } + set focused(value: boolean) { + this._focused = value; + this.searchInput.focused = value; + } + + constructor( + groupsByScope: Record, + settingsManager: SettingsManager, + cwd: string, + agentDir: string, + terminalHeight?: number, + writeScope: ConfigWriteScope = "global", + ) { + this.groupsByScope = groupsByScope; + this.settingsManager = settingsManager; + this.cwd = cwd; + this.agentDir = agentDir; + this.writeScope = writeScope; + this.inheritedEnabledByKey = this.buildInheritedEnabledMap(groupsByScope.global); + this.searchInput = new Input(); + // 8 lines of chrome: top spacer + top border + spacer + header (2 lines) + spacer + bottom spacer + bottom border + const chrome = 8; + this.maxVisible = Math.max(5, (terminalHeight ?? 24) - chrome); + this.buildFlatList(); + this.filteredItems = [...this.flatItems]; + } + + setWriteScope(writeScope: ConfigWriteScope): void { + this.writeScope = writeScope; + this.buildFlatList(); + this.filterItems(this.searchInput.getValue()); + } + + private get groups(): ResourceGroup[] { + return this.groupsByScope[this.writeScope]; + } + + private buildInheritedEnabledMap(groups: ResourceGroup[]): Map { + const result = new Map(); + for (const group of groups) { + for (const subgroup of group.subgroups) { + for (const item of subgroup.items) { + result.set(this.getResourceItemKey(item), item.enabled); + } + } + } + return result; + } + + private buildFlatList(): void { + this.flatItems = []; + for (const group of this.groups) { + this.flatItems.push({ type: "group", group }); + for (const subgroup of group.subgroups) { + this.flatItems.push({ type: "subgroup", subgroup, group }); + for (const item of subgroup.items) { + this.flatItems.push({ type: "item", item }); + } + } + } + // Start selection on first item (not header) + this.selectedIndex = this.flatItems.findIndex((e) => e.type === "item"); + if (this.selectedIndex < 0) this.selectedIndex = 0; + } + + private findNextItem(fromIndex: number, direction: 1 | -1): number { + let idx = fromIndex + direction; + while (idx >= 0 && idx < this.filteredItems.length) { + if (this.filteredItems[idx].type === "item") { + return idx; + } + idx += direction; + } + return fromIndex; // Stay at current if no item found + } + + private filterItems(query: string): void { + if (!query.trim()) { + this.filteredItems = [...this.flatItems]; + this.selectFirstItem(); + return; + } + + const lowerQuery = query.toLowerCase(); + const matchingItems = new Set(); + const matchingSubgroups = new Set(); + const matchingGroups = new Set(); + + for (const entry of this.flatItems) { + if (entry.type === "item") { + const item = entry.item; + if ( + item.displayName.toLowerCase().includes(lowerQuery) || + item.resourceType.toLowerCase().includes(lowerQuery) || + item.path.toLowerCase().includes(lowerQuery) + ) { + matchingItems.add(item); + } + } + } + + // Find which subgroups and groups contain matching items + for (const group of this.groups) { + for (const subgroup of group.subgroups) { + for (const item of subgroup.items) { + if (matchingItems.has(item)) { + matchingSubgroups.add(subgroup); + matchingGroups.add(group); + } + } + } + } + + this.filteredItems = []; + for (const entry of this.flatItems) { + if (entry.type === "group" && matchingGroups.has(entry.group)) { + this.filteredItems.push(entry); + } else if (entry.type === "subgroup" && matchingSubgroups.has(entry.subgroup)) { + this.filteredItems.push(entry); + } else if (entry.type === "item" && matchingItems.has(entry.item)) { + this.filteredItems.push(entry); + } + } + + this.selectFirstItem(); + } + + private selectFirstItem(): void { + const firstItemIndex = this.filteredItems.findIndex((e) => e.type === "item"); + this.selectedIndex = firstItemIndex >= 0 ? firstItemIndex : 0; + } + + updateItem(item: ResourceItem, enabled: boolean): void { + item.enabled = enabled; + // Update in groups too + for (const group of this.groups) { + for (const subgroup of group.subgroups) { + const found = subgroup.items.find((i) => i.path === item.path && i.resourceType === item.resourceType); + if (found) { + found.enabled = enabled; + return; + } + } + } + } + + invalidate(): void {} + + render(width: number): string[] { + const lines: string[] = []; + + // Search input + lines.push(...this.searchInput.render(width)); + lines.push(""); + + if (this.filteredItems.length === 0) { + lines.push(theme.fg("muted", " No resources found")); + return lines; + } + + // Calculate visible range + const startIndex = Math.max( + 0, + Math.min(this.selectedIndex - Math.floor(this.maxVisible / 2), this.filteredItems.length - this.maxVisible), + ); + const endIndex = Math.min(startIndex + this.maxVisible, this.filteredItems.length); + + for (let i = startIndex; i < endIndex; i++) { + const entry = this.filteredItems[i]; + const isSelected = i === this.selectedIndex; + + if (entry.type === "group") { + // Main group header (no cursor) + const inherited = this.writeScope === "project" && entry.group.scope === "user"; + const label = theme.bold(`${entry.group.label}${inherited ? " · inherited global" : ""}`); + const groupLine = theme.fg(inherited ? "dim" : "accent", label); + lines.push(truncateToWidth(` ${groupLine}`, width, "")); + } else if (entry.type === "subgroup") { + // Subgroup header (indented, no cursor) + const color = this.writeScope === "project" && entry.group.scope === "user" ? "dim" : "muted"; + const subgroupLine = theme.fg(color, entry.subgroup.label); + lines.push(truncateToWidth(` ${subgroupLine}`, width, "")); + } else { + // Resource item (cursor only on items) + const item = entry.item; + const cursor = isSelected ? "> " : " "; + const dimmed = this.isDimmedItem(item); + const nameText = isSelected && !dimmed ? theme.bold(item.displayName) : item.displayName; + const name = dimmed ? theme.fg("dim", nameText) : nameText; + lines.push( + truncateToWidth( + `${cursor} ${this.renderCheckbox(item)} ${name}${this.getItemSuffix(item)}`, + width, + "...", + ), + ); + } + } + + // Scroll indicator + if (startIndex > 0 || endIndex < this.filteredItems.length) { + const itemCount = this.filteredItems.filter((e) => e.type === "item").length; + const currentItemIndex = + this.filteredItems.slice(0, this.selectedIndex).filter((e) => e.type === "item").length + 1; + lines.push(theme.fg("dim", ` (${currentItemIndex}/${itemCount})`)); + } + + return lines; + } + + handleInput(data: string): void { + const kb = getKeybindings(); + + if (kb.matches(data, "tui.select.up")) { + this.selectedIndex = this.findNextItem(this.selectedIndex, -1); + return; + } + if (kb.matches(data, "tui.select.down")) { + this.selectedIndex = this.findNextItem(this.selectedIndex, 1); + return; + } + if (kb.matches(data, "tui.select.pageUp")) { + // Jump up by maxVisible, then find nearest item + let target = Math.max(0, this.selectedIndex - this.maxVisible); + while (target < this.filteredItems.length && this.filteredItems[target].type !== "item") { + target++; + } + if (target < this.filteredItems.length) { + this.selectedIndex = target; + } + return; + } + if (kb.matches(data, "tui.select.pageDown")) { + // Jump down by maxVisible, then find nearest item + let target = Math.min(this.filteredItems.length - 1, this.selectedIndex + this.maxVisible); + while (target >= 0 && this.filteredItems[target].type !== "item") { + target--; + } + if (target >= 0) { + this.selectedIndex = target; + } + return; + } + if (kb.matches(data, "tui.select.cancel")) { + this.onCancel?.(); + return; + } + if (matchesKey(data, "ctrl+c")) { + this.onExit?.(); + return; + } + if (kb.matches(data, "tui.input.tab")) { + this.onSwitchMode?.(); + return; + } + if (data === " " || kb.matches(data, "tui.select.confirm")) { + const entry = this.filteredItems[this.selectedIndex]; + if (entry?.type === "item" && (this.writeScope === "project" || this.getItemScope(entry.item) === "user")) { + const newEnabled = this.toggleResource(entry.item); + if (newEnabled !== undefined) { + this.updateItem(entry.item, newEnabled); + this.onToggle?.(entry.item, newEnabled); + } + } + return; + } + + // Pass to search input + this.searchInput.handleInput(data); + this.filterItems(this.searchInput.getValue()); + } + + private toggleResource(item: ResourceItem): boolean | undefined { + if (this.writeScope === "project") { + const state = this.getNextOverrideState(item); + if (!this.setProjectResourceOverride(item, state)) return undefined; + return state === "inherit" ? this.getInheritedEnabled(item) : state === "load"; + } + + const enabled = !item.enabled; + if (item.metadata.origin === "top-level") { + this.toggleTopLevelResource(item, enabled); + } else { + this.togglePackageResource(item, enabled); + } + return enabled; + } + + private toggleTopLevelResource(item: ResourceItem, enabled: boolean): void { + const scope = item.metadata.scope as "user" | "project"; + const settings = + scope === "project" ? this.settingsManager.getProjectSettings() : this.settingsManager.getGlobalSettings(); + + const arrayKey = item.resourceType as "extensions" | "skills" | "prompts" | "themes"; + const current = (settings[arrayKey] ?? []) as string[]; + + // Generate pattern for this resource + const pattern = this.getResourcePattern(item); + const disablePattern = `-${pattern}`; + const enablePattern = `+${pattern}`; + + // Filter out existing patterns for this resource + const updated = current.filter((p) => { + const stripped = p.startsWith("!") || p.startsWith("+") || p.startsWith("-") ? p.slice(1) : p; + return stripped !== pattern; + }); + + if (enabled) { + updated.push(enablePattern); + } else { + updated.push(disablePattern); + } + + if (scope === "project") { + if (arrayKey === "extensions") { + this.settingsManager.setProjectExtensionPaths(updated); + } else if (arrayKey === "skills") { + this.settingsManager.setProjectSkillPaths(updated); + } else if (arrayKey === "prompts") { + this.settingsManager.setProjectPromptTemplatePaths(updated); + } else if (arrayKey === "themes") { + this.settingsManager.setProjectThemePaths(updated); + } + } else { + if (arrayKey === "extensions") { + this.settingsManager.setExtensionPaths(updated); + } else if (arrayKey === "skills") { + this.settingsManager.setSkillPaths(updated); + } else if (arrayKey === "prompts") { + this.settingsManager.setPromptTemplatePaths(updated); + } else if (arrayKey === "themes") { + this.settingsManager.setThemePaths(updated); + } + } + } + + private togglePackageResource(item: ResourceItem, enabled: boolean): void { + const scope = item.metadata.scope as "user" | "project"; + const settings = + scope === "project" ? this.settingsManager.getProjectSettings() : this.settingsManager.getGlobalSettings(); + + const packages = [...(settings.packages ?? [])] as PackageSource[]; + const pkgIndex = packages.findIndex((pkg) => { + const source = typeof pkg === "string" ? pkg : pkg.source; + return source === item.metadata.source; + }); + + if (pkgIndex === -1) return; + + let pkg = packages[pkgIndex]; + + // Convert string to object form if needed + if (typeof pkg === "string") { + pkg = { source: pkg }; + packages[pkgIndex] = pkg; + } + + // Get the resource array for this type + const arrayKey = item.resourceType as "extensions" | "skills" | "prompts" | "themes"; + const current = (pkg[arrayKey] ?? []) as string[]; + + // Generate pattern relative to package root + const pattern = this.getPackageResourcePattern(item); + const disablePattern = `-${pattern}`; + const enablePattern = `+${pattern}`; + + // Filter out existing patterns for this resource + const updated = current.filter((p) => { + const stripped = p.startsWith("!") || p.startsWith("+") || p.startsWith("-") ? p.slice(1) : p; + return stripped !== pattern; + }); + + if (enabled) { + updated.push(enablePattern); + } else { + updated.push(disablePattern); + } + + (pkg as Record)[arrayKey] = updated.length > 0 ? updated : undefined; + + // Clean up empty filter object + const hasFilters = ["extensions", "skills", "prompts", "themes"].some( + (k) => (pkg as Record)[k] !== undefined, + ); + if (!hasFilters) { + packages[pkgIndex] = (pkg as { source: string }).source; + } + + if (scope === "project") { + this.settingsManager.setProjectPackages(packages); + } else { + this.settingsManager.setPackages(packages); + } + } + + private renderCheckbox(item: ResourceItem): string { + if (this.writeScope === "project") { + const state = this.getProjectOverrideState(item); + if (state === "load") return theme.fg("success", "[+]"); + if (state === "unload") return theme.fg("warning", "[-]"); + return theme.fg("dim", item.enabled ? "[x]" : "[ ]"); + } + return item.enabled ? theme.fg("success", "[x]") : theme.fg("dim", "[ ]"); + } + + private getItemSuffix(item: ResourceItem): string { + if (this.writeScope !== "project") return ""; + const state = this.getProjectOverrideState(item); + if (state === "load") return theme.fg("muted", " project load"); + if (state === "unload") return theme.fg("muted", " project unload"); + return this.isInheritedGlobalItem(item) ? theme.fg("dim", " inherited global") : ""; + } + + private isDimmedItem(item: ResourceItem): boolean { + return ( + this.writeScope === "project" && + this.isInheritedGlobalItem(item) && + this.getProjectOverrideState(item) === "inherit" + ); + } + + private setProjectResourceOverride(item: ResourceItem, state: ProjectOverrideState): boolean { + return item.metadata.origin === "top-level" + ? this.setProjectTopLevelOverride(item, state) + : this.setProjectPackageOverride(item, state); + } + + private setProjectTopLevelOverride(item: ResourceItem, state: ProjectOverrideState): boolean { + const current = (this.settingsManager.getProjectSettings()[item.resourceType] ?? []) as string[]; + const pattern = this.isInheritedGlobalItem(item) ? item.path : this.getResourcePatternForScope(item, "project"); + const patterns = this.getTopLevelOverridePatterns(item, "project"); + const updated = current.filter((entry) => { + const target = this.getPatternEntryTarget(entry); + if ((entry.startsWith("!") || entry.startsWith("+") || entry.startsWith("-")) && patterns.has(target)) + return false; + return !(state === "inherit" && this.isInheritedGlobalItem(item) && target === pattern); + }); + if (state !== "inherit") { + if (this.isInheritedGlobalItem(item) && !updated.includes(pattern)) updated.push(pattern); + updated.push(`${state === "load" ? "+" : "-"}${pattern}`); + } + this.setProjectTopLevelPaths(item.resourceType, updated); + return true; + } + + private setProjectTopLevelPaths(key: ResourceType, paths: string[]): void { + if (key === "extensions") this.settingsManager.setProjectExtensionPaths(paths); + else if (key === "skills") this.settingsManager.setProjectSkillPaths(paths); + else if (key === "prompts") this.settingsManager.setProjectPromptTemplatePaths(paths); + else this.settingsManager.setProjectThemePaths(paths); + } + + private setProjectPackageOverride(item: ResourceItem, state: ProjectOverrideState): boolean { + const packages = [...(this.settingsManager.getProjectSettings().packages ?? [])] as PackageSource[]; + let pkgIndex = packages.findIndex((pkg) => + this.packageSourceStringMatches( + item.metadata.source, + this.getItemScope(item), + typeof pkg === "string" ? pkg : pkg.source, + "project", + ), + ); + if (pkgIndex === -1) { + if (state === "inherit") return false; + packages.push(this.createPackageOverrideSource(item)); + pkgIndex = packages.length - 1; + } + let pkg = packages[pkgIndex]; + if (pkg === undefined) return false; + if (typeof pkg === "string") { + pkg = { source: pkg }; + packages[pkgIndex] = pkg; + } + const pattern = this.getPackageResourcePattern(item); + const updated = ((pkg[item.resourceType] ?? []) as string[]).filter( + (entry) => this.getPatternEntryTarget(entry) !== pattern, + ); + if (state !== "inherit") updated.push(`${state === "load" ? "+" : "-"}${pattern}`); + (pkg as Record)[item.resourceType] = updated.length > 0 ? updated : undefined; + if (!RESOURCE_TYPES.some((key) => (pkg as Record)[key] !== undefined)) { + if (pkg.autoload === false) packages.splice(pkgIndex, 1); + else packages[pkgIndex] = pkg.source; + } + this.settingsManager.setProjectPackages(packages); + return true; + } + + private getNextOverrideState(item: ResourceItem): ProjectOverrideState { + const state = this.getProjectOverrideState(item); + const inheritedEnabled = this.getInheritedEnabled(item); + if (state === "inherit") return inheritedEnabled ? "unload" : "load"; + if (state === "unload") return inheritedEnabled ? "load" : "inherit"; + return inheritedEnabled ? "inherit" : "unload"; + } + + private getProjectOverrideState(item: ResourceItem): ProjectOverrideState { + if (this.writeScope !== "project") return "inherit"; + if (item.metadata.origin === "top-level") { + return this.getOverrideStateFromEntries( + (this.settingsManager.getProjectSettings()[item.resourceType] ?? []) as string[], + this.getTopLevelOverridePatterns(item, "project"), + false, + ); + } + const pkg = this.findMatchingPackageSource(item, "project"); + if (typeof pkg !== "object") return "inherit"; + const entries = pkg[item.resourceType]; + if (entries === undefined) return "inherit"; + return this.getOverrideStateFromEntries( + entries, + new Set([this.getPackageResourcePattern(item)]), + pkg.autoload !== false, + ); + } + + private getOverrideStateFromEntries( + entries: string[], + patterns: Set, + emptyArrayIsUnload: boolean, + ): ProjectOverrideState { + if (entries.length === 0 && emptyArrayIsUnload) return "unload"; + let state: ProjectOverrideState = "inherit"; + for (const entry of entries) { + if (!patterns.has(this.getPatternEntryTarget(entry))) continue; + if (entry.startsWith("!") || entry.startsWith("-")) state = "unload"; + else state = "load"; + } + return state; + } + + private getInheritedEnabled(item: ResourceItem): boolean { + return ( + this.inheritedEnabledByKey.get(this.getResourceItemKey(item)) ?? + (this.getItemScope(item) === "user" ? item.enabled : true) + ); + } + + private isInheritedGlobalItem(item: ResourceItem): boolean { + return this.getItemScope(item) === "user" || this.inheritedEnabledByKey.has(this.getResourceItemKey(item)); + } + + private getTopLevelOverridePatterns(item: ResourceItem, scope: SettingsScope): Set { + const baseDir = this.getTopLevelBaseDir(scope); + const patterns = new Set([ + this.getResourcePatternForScope(item, scope), + item.path, + relative(baseDir, item.path), + ]); + if (item.metadata.baseDir) patterns.add(relative(item.metadata.baseDir, item.path)); + return patterns; + } + + private getResourcePatternForScope(item: ResourceItem, scope: SettingsScope): string { + const sourceScope = this.getItemScope(item); + if (scope !== sourceScope) return item.path; + const baseDir = item.metadata.baseDir ?? this.getTopLevelBaseDir(sourceScope); + return relative(baseDir, item.path); + } + + private createPackageOverrideSource(item: ResourceItem): PackageSource { + const source = item.metadata.source; + if (!isLocalPath(source)) return { source, autoload: false }; + const sourcePath = resolvePath(source, this.getTopLevelBaseDir(this.getItemScope(item)), { trim: true }); + return { source: relative(this.getTopLevelBaseDir("project"), sourcePath) || ".", autoload: false }; + } + + private packageSourceStringMatches( + leftSource: string, + leftScope: SettingsScope, + rightSource: string, + rightScope: SettingsScope, + ): boolean { + if (leftSource === rightSource) return true; + if (!isLocalPath(leftSource) || !isLocalPath(rightSource)) return false; + const left = resolvePath(leftSource, this.getTopLevelBaseDir(leftScope), { trim: true }); + const right = resolvePath(rightSource, this.getTopLevelBaseDir(rightScope), { trim: true }); + return left === right; + } + + private findMatchingPackageSource(item: ResourceItem, targetScope: SettingsScope): PackageSource | undefined { + const settings = + targetScope === "project" + ? this.settingsManager.getProjectSettings() + : this.settingsManager.getGlobalSettings(); + return (settings.packages ?? []).find((pkg) => + this.packageSourceStringMatches( + item.metadata.source, + this.getItemScope(item), + typeof pkg === "string" ? pkg : pkg.source, + targetScope, + ), + ); + } + + private getPatternEntryTarget(entry: string): string { + return entry.startsWith("!") || entry.startsWith("+") || entry.startsWith("-") ? entry.slice(1) : entry; + } + + private getResourceItemKey(item: ResourceItem): string { + return `${item.resourceType}:${canonicalizePath(item.path)}`; + } + + private getItemScope(item: ResourceItem): SettingsScope { + return item.metadata.scope === "project" ? "project" : "user"; + } + + private getTopLevelBaseDir(scope: "user" | "project"): string { + return scope === "project" ? join(this.cwd, CONFIG_DIR_NAME) : this.agentDir; + } + + private getResourcePattern(item: ResourceItem): string { + const scope = item.metadata.scope as "user" | "project"; + const baseDir = item.metadata.baseDir ?? this.getTopLevelBaseDir(scope); + return relative(baseDir, item.path); + } + + private getPackageResourcePattern(item: ResourceItem): string { + const baseDir = item.metadata.baseDir ?? dirname(item.path); + return relative(baseDir, item.path); + } +} + +export class ConfigSelectorComponent extends Container implements Focusable { + private header: ConfigSelectorHeader; + private resourceList: ResourceList; + private writeScope: ConfigWriteScope; + + private _focused = false; + get focused(): boolean { + return this._focused; + } + set focused(value: boolean) { + this._focused = value; + this.resourceList.focused = value; + } + + constructor( + resolvedPaths: ScopedResolvedPaths, + settingsManager: SettingsManager, + cwd: string, + agentDir: string, + onClose: () => void, + onExit: () => void, + requestRender: () => void, + terminalHeight?: number, + writeScope: ConfigWriteScope = "global", + projectModeAvailable = true, + ) { + super(); + + this.writeScope = writeScope; + const groupsByScope = { + global: buildGroups(resolvedPaths.global, agentDir), + project: buildGroups(resolvedPaths.project, agentDir), + }; + + // Add header + this.addChild(new Spacer(1)); + this.addChild(new DynamicBorder()); + this.addChild(new Spacer(1)); + this.header = new ConfigSelectorHeader(this.writeScope, projectModeAvailable); + this.addChild(this.header); + this.addChild(new Spacer(1)); + + // Resource list + this.resourceList = new ResourceList( + groupsByScope, + settingsManager, + cwd, + agentDir, + terminalHeight, + this.writeScope, + ); + this.resourceList.onCancel = onClose; + this.resourceList.onExit = onExit; + this.resourceList.onToggle = () => requestRender(); + if (projectModeAvailable) { + this.resourceList.onSwitchMode = () => { + this.switchWriteScope(); + requestRender(); + }; + } + this.addChild(this.resourceList); + + // Bottom border + this.addChild(new Spacer(1)); + this.addChild(new DynamicBorder()); + } + + private switchWriteScope(): void { + this.writeScope = this.writeScope === "global" ? "project" : "global"; + this.header.setWriteScope(this.writeScope); + this.resourceList.setWriteScope(this.writeScope); + } + + getResourceList(): ResourceList { + return this.resourceList; + } +} diff --git a/packages/coding-agent/src/modes/interactive/components/custom-editor.ts b/packages/coding-agent/src/modes/interactive/components/custom-editor.ts new file mode 100644 index 0000000000000000000000000000000000000000..9d345b4e9d5a3766537c68cbb72f2839b61b6b03 --- /dev/null +++ b/packages/coding-agent/src/modes/interactive/components/custom-editor.ts @@ -0,0 +1,148 @@ +import { Editor, type EditorOptions, type EditorTheme, type TUI, visibleWidth } from "@earendil-works/pi-tui"; +import type { AppKeybinding, KeybindingsManager } from "../../../core/keybindings.ts"; +import type { StatusIndicator } from "./status-indicator.ts"; + +export type CustomEditorOptions = EditorOptions & { + /** Render working, compaction, summarization, and retry status in the editor's top border. */ + embedWorkingStatus?: boolean; +}; + +/** + * Custom editor that handles app-level keybindings for coding-agent. + */ +export class CustomEditor extends Editor { + private keybindings: KeybindingsManager; + private workingStatusIndicator: StatusIndicator | undefined; + public readonly embedWorkingStatus: boolean; + public actionHandlers: Map void> = new Map(); + + // Special handlers that can be dynamically replaced + public onEscape?: () => void; + public onCtrlD?: () => void; + public onPasteImage?: () => void; + /** Handler for extension-registered shortcuts. Returns true if handled. */ + public onExtensionShortcut?: (data: string) => boolean; + + constructor(tui: TUI, theme: EditorTheme, keybindings: KeybindingsManager, options?: CustomEditorOptions) { + super(tui, theme, options); + this.keybindings = keybindings; + this.embedWorkingStatus = options?.embedWorkingStatus ?? false; + } + + setWorkingStatusIndicator(indicator: StatusIndicator | undefined): void { + this.workingStatusIndicator = indicator; + } + + protected override renderTopBorder(width: number, hiddenLineCount: number): string { + if (!this.embedWorkingStatus || !this.workingStatusIndicator || width <= 0) { + return super.renderTopBorder(width, hiddenLineCount); + } + + let status = this.workingStatusIndicator.renderInBorder(Math.max(1, width - 5)); + let statusWidth = visibleWidth(status); + if (statusWidth === 0) return super.renderTopBorder(width, hiddenLineCount); + + const overflowLabel = hiddenLineCount > 0 ? ` ↑ ${hiddenLineCount} more ` : undefined; + const overflowLabelWidth = overflowLabel ? visibleWidth(overflowLabel) : 0; + const overflowStart = Math.floor((width - overflowLabelWidth) / 2); + const canFitOverflow = () => + overflowLabel !== undefined && overflowLabelWidth + 2 <= width && overflowStart - (3 + statusWidth + 1) >= 1; + + if (overflowLabel && !canFitOverflow()) { + status = this.workingStatusIndicator.renderSpinnerInBorder(width); + statusWidth = visibleWidth(status); + } + + if (canFitOverflow()) { + const leftBlockWidth = 3 + statusWidth + 1; + return ( + this.borderColor("── ") + + status + + this.borderColor( + ` ${"─".repeat(overflowStart - leftBlockWidth)}${overflowLabel}${"─".repeat(width - overflowStart - overflowLabelWidth)}`, + ) + ); + } + + if (width >= statusWidth + 5) { + return this.borderColor("── ") + status + this.borderColor(` ${"─".repeat(width - statusWidth - 4)}`); + } + + status = this.workingStatusIndicator.renderSpinnerInBorder(width); + statusWidth = visibleWidth(status); + const prefixWidth = Math.min(3, Math.max(0, width - statusWidth)); + return ( + this.borderColor("─".repeat(prefixWidth)) + + status + + this.borderColor("─".repeat(Math.max(0, width - prefixWidth - statusWidth))) + ); + } + + /** + * Register a handler for an app action. + */ + onAction(action: AppKeybinding, handler: () => void): void { + this.actionHandlers.set(action, handler); + } + + handleInput(data: string): void { + // Check extension-registered shortcuts first + if (this.onExtensionShortcut?.(data)) { + return; + } + + // Check for clipboard paste keybinding + if (this.keybindings.matches(data, "app.clipboard.pasteImage")) { + this.onPasteImage?.(); + return; + } + + // Check app keybindings first + + // Escape/interrupt - only if autocomplete is NOT active + if (this.keybindings.matches(data, "app.interrupt")) { + if (!this.isShowingAutocomplete()) { + // Use dynamic onEscape if set, otherwise registered handler + const handler = this.onEscape ?? this.actionHandlers.get("app.interrupt"); + if (handler) { + handler(); + return; + } + } + // Let parent handle escape for autocomplete cancellation + super.handleInput(data); + return; + } + + // Exit (Ctrl+D) - only when editor is empty + if (this.keybindings.matches(data, "app.exit")) { + if (this.getText().length === 0) { + const handler = this.onCtrlD ?? this.actionHandlers.get("app.exit"); + if (handler) handler(); + return; + } + // Fall through to editor handling for delete-char-forward when not empty + } + + // Explicit history bindings take precedence over app actions while the editor is focused. + // This lets users bind Ctrl+P even though it cycles models by default. + if ( + this.keybindings.matches(data, "tui.editor.historyPrevious") || + this.keybindings.matches(data, "tui.editor.historyNext") + ) { + super.handleInput(data); + return; + } + + // Check all other app actions + for (const [action, handler] of this.actionHandlers) { + if (action !== "app.interrupt" && action !== "app.exit" && this.keybindings.matches(data, action)) { + handler(); + return; + } + } + + // Pass to parent for editor handling + super.handleInput(data); + } +} diff --git a/packages/coding-agent/src/modes/interactive/components/custom-message.ts b/packages/coding-agent/src/modes/interactive/components/custom-message.ts new file mode 100644 index 0000000000000000000000000000000000000000..b5ee92658a7b641c0327d5e6207e79c96d58a771 --- /dev/null +++ b/packages/coding-agent/src/modes/interactive/components/custom-message.ts @@ -0,0 +1,113 @@ +import type { TextContent } from "@earendil-works/pi-ai"; +import type { Component } from "@earendil-works/pi-tui"; +import { Box, Container, Markdown, type MarkdownTheme, Spacer, Text } from "@earendil-works/pi-tui"; +import type { MessageRenderer } from "../../../core/extensions/types.ts"; +import type { CustomMessage } from "../../../core/messages.ts"; +import { getMarkdownTheme, theme } from "../theme/theme.ts"; + +/** + * Component that renders a custom message entry from extensions. + * Uses distinct styling to differentiate from user messages. + */ +export class CustomMessageComponent extends Container { + private message: CustomMessage; + private customRenderer?: MessageRenderer; + private box: Box; + private customComponent?: Component; + private markdownTheme: MarkdownTheme; + private _expanded = false; + private outputPad: number; + + constructor( + message: CustomMessage, + customRenderer?: MessageRenderer, + markdownTheme: MarkdownTheme = getMarkdownTheme(), + outputPad = 1, + ) { + super(); + this.message = message; + this.customRenderer = customRenderer; + this.markdownTheme = markdownTheme; + this.outputPad = outputPad; + + this.addChild(new Spacer(1)); + + // Create box with purple background (used for default rendering) + this.box = new Box(1, 1, (t) => theme.bg("customMessageBg", t)); + + this.rebuild(); + } + + setExpanded(expanded: boolean): void { + if (this._expanded !== expanded) { + this._expanded = expanded; + this.rebuild(); + } + } + + setOutputPad(outputPad: number): void { + if (this.outputPad !== outputPad) { + this.outputPad = outputPad; + this.rebuild(); + } + } + + override invalidate(): void { + super.invalidate(); + this.rebuild(); + } + + private rebuild(): void { + // Remove previous content component + if (this.customComponent) { + this.removeChild(this.customComponent); + this.customComponent = undefined; + } + this.removeChild(this.box); + + // Try custom renderer first - it handles its own styling + if (this.customRenderer) { + try { + const component = this.customRenderer( + this.message, + { expanded: this._expanded, outputPad: this.outputPad }, + theme, + ); + if (component) { + // Custom renderer provides its own styled component + this.customComponent = component; + this.addChild(component); + return; + } + } catch { + // Fall through to default rendering + } + } + + // Default rendering uses our box + this.addChild(this.box); + this.box.clear(); + + // Default rendering: label + content + const label = theme.fg("customMessageLabel", `\x1b[1m[${this.message.customType}]\x1b[22m`); + this.box.addChild(new Text(label, 0, 0)); + this.box.addChild(new Spacer(1)); + + // Extract text content + let text: string; + if (typeof this.message.content === "string") { + text = this.message.content; + } else { + text = this.message.content + .filter((c): c is TextContent => c.type === "text") + .map((c) => c.text) + .join("\n"); + } + + this.box.addChild( + new Markdown(text, 0, 0, this.markdownTheme, { + color: (text: string) => theme.fg("customMessageText", text), + }), + ); + } +} diff --git a/packages/coding-agent/src/modes/interactive/components/dynamic-border.ts b/packages/coding-agent/src/modes/interactive/components/dynamic-border.ts new file mode 100644 index 0000000000000000000000000000000000000000..77342b25e8ed4b419a72608f69136c6b04598631 --- /dev/null +++ b/packages/coding-agent/src/modes/interactive/components/dynamic-border.ts @@ -0,0 +1,25 @@ +import type { Component } from "@earendil-works/pi-tui"; +import { theme } from "../theme/theme.ts"; + +/** + * Dynamic border component that adjusts to viewport width. + * + * Note: When used from extensions loaded via jiti, the global `theme` may be undefined + * because jiti creates a separate module cache. Always pass an explicit color + * function when using DynamicBorder in components exported for extension use. + */ +export class DynamicBorder implements Component { + private color: (str: string) => string; + + constructor(color: (str: string) => string = (str) => theme.fg("border", str)) { + this.color = color; + } + + invalidate(): void { + // No cached state to invalidate currently + } + + render(width: number): string[] { + return [this.color("─".repeat(Math.max(1, width)))]; + } +} diff --git a/packages/coding-agent/src/modes/interactive/components/earendil-announcement.ts b/packages/coding-agent/src/modes/interactive/components/earendil-announcement.ts new file mode 100644 index 0000000000000000000000000000000000000000..f2726d989737b6adab4e60166211ae1d1ba13be0 --- /dev/null +++ b/packages/coding-agent/src/modes/interactive/components/earendil-announcement.ts @@ -0,0 +1,53 @@ +import * as fs from "node:fs"; +import { Container, Image, Spacer, Text } from "@earendil-works/pi-tui"; +import { getBundledInteractiveAssetPath } from "../../../config.ts"; +import { theme } from "../theme/theme.ts"; +import { DynamicBorder } from "./dynamic-border.ts"; + +const BLOG_URL = "https://mariozechner.at/posts/2026-04-08-ive-sold-out/"; +const IMAGE_FILENAME = "clankolas.png"; + +let cachedImageBase64: string | undefined; +let attemptedImageLoad = false; + +function loadImageBase64(): string | undefined { + if (attemptedImageLoad) { + return cachedImageBase64; + } + + attemptedImageLoad = true; + try { + cachedImageBase64 = fs.readFileSync(getBundledInteractiveAssetPath(IMAGE_FILENAME)).toString("base64"); + } catch { + cachedImageBase64 = undefined; + } + return cachedImageBase64; +} + +export class EarendilAnnouncementComponent extends Container { + constructor() { + super(); + + this.addChild(new DynamicBorder((text) => theme.fg("accent", text))); + this.addChild(new Text(theme.bold(theme.fg("accent", "pi has joined Earendil")), 1, 0)); + this.addChild(new Spacer(1)); + this.addChild(new Text(theme.fg("muted", "Read the blog post:"), 1, 0)); + this.addChild(new Text(theme.fg("mdLink", BLOG_URL), 1, 0)); + this.addChild(new Spacer(1)); + + const imageBase64 = loadImageBase64(); + if (imageBase64) { + this.addChild( + new Image( + imageBase64, + "image/png", + { fallbackColor: (text) => theme.fg("muted", text) }, + { maxWidthCells: 56, filename: IMAGE_FILENAME }, + ), + ); + this.addChild(new Spacer(1)); + } + + this.addChild(new DynamicBorder((text) => theme.fg("accent", text))); + } +} diff --git a/packages/coding-agent/src/modes/interactive/components/extension-input.ts b/packages/coding-agent/src/modes/interactive/components/extension-input.ts new file mode 100644 index 0000000000000000000000000000000000000000..581ef4f5cdf351f08d4cca1182b94c4eb6bf5044 --- /dev/null +++ b/packages/coding-agent/src/modes/interactive/components/extension-input.ts @@ -0,0 +1,87 @@ +/** + * Simple text input component for extensions. + */ + +import { Container, type Focusable, getKeybindings, Input, Spacer, Text, type TUI } from "@earendil-works/pi-tui"; +import { theme } from "../theme/theme.ts"; +import { CountdownTimer } from "./countdown-timer.ts"; +import { DynamicBorder } from "./dynamic-border.ts"; +import { keyHint } from "./keybinding-hints.ts"; + +export interface ExtensionInputOptions { + tui?: TUI; + timeout?: number; +} + +export class ExtensionInputComponent extends Container implements Focusable { + private input: Input; + private onSubmitCallback: (value: string) => void; + private onCancelCallback: () => void; + private titleText: Text; + private baseTitle: string; + private countdown: CountdownTimer | undefined; + + // Focusable implementation - propagate to input for IME cursor positioning + private _focused = false; + get focused(): boolean { + return this._focused; + } + set focused(value: boolean) { + this._focused = value; + this.input.focused = value; + } + + constructor( + title: string, + _placeholder: string | undefined, + onSubmit: (value: string) => void, + onCancel: () => void, + opts?: ExtensionInputOptions, + ) { + super(); + + this.onSubmitCallback = onSubmit; + this.onCancelCallback = onCancel; + this.baseTitle = title; + + this.addChild(new DynamicBorder()); + this.addChild(new Spacer(1)); + + this.titleText = new Text(theme.fg("accent", title), 1, 0); + this.addChild(this.titleText); + this.addChild(new Spacer(1)); + + if (opts?.timeout && opts.timeout > 0 && opts.tui) { + this.countdown = new CountdownTimer( + opts.timeout, + opts.tui, + (s) => this.titleText.setText(theme.fg("accent", `${this.baseTitle} (${s}s)`)), + () => this.onCancelCallback(), + ); + } + + this.input = new Input(); + this.addChild(this.input); + this.addChild(new Spacer(1)); + this.addChild( + new Text(`${keyHint("tui.select.confirm", "submit")} ${keyHint("tui.select.cancel", "cancel")}`, 1, 0), + ); + this.addChild(new Spacer(1)); + this.addChild(new DynamicBorder()); + } + + handleInput(keyData: string): void { + const kb = getKeybindings(); + if (kb.matches(keyData, "tui.select.confirm") || keyData === "\n") { + this.onSubmitCallback(this.input.getValue()); + } else if (kb.matches(keyData, "tui.select.cancel")) { + this.onCancelCallback(); + } else { + this.input.handleInput(keyData); + } + } + + dispose(): void { + this.countdown?.dispose(); + } +} diff --git a/packages/coding-agent/src/modes/interactive/components/index.ts b/packages/coding-agent/src/modes/interactive/components/index.ts new file mode 100644 index 0000000000000000000000000000000000000000..326adcc53134fdcb96499e3c8a7a433ebe9857e3 --- /dev/null +++ b/packages/coding-agent/src/modes/interactive/components/index.ts @@ -0,0 +1,38 @@ +// UI Components for extensions +export { ArminComponent } from "./armin.ts"; +export { AssistantMessageComponent } from "./assistant-message.ts"; +export { BashExecutionComponent } from "./bash-execution.ts"; +export { BorderedLoader } from "./bordered-loader.ts"; +export { BranchSummaryMessageComponent } from "./branch-summary-message.ts"; +export { CompactionSummaryMessageComponent } from "./compaction-summary-message.ts"; +export { CustomEditor, type CustomEditorOptions } from "./custom-editor.ts"; +export { CustomMessageComponent } from "./custom-message.ts"; +export { DaxnutsComponent } from "./daxnuts.ts"; +export { type RenderDiffOptions, renderDiff } from "./diff.ts"; +export { DynamicBorder } from "./dynamic-border.ts"; +export { ExtensionEditorComponent } from "./extension-editor.ts"; +export { ExtensionInputComponent } from "./extension-input.ts"; +export { ExtensionSelectorComponent } from "./extension-selector.ts"; +export { + FirstTimeSetupComponent, + type FirstTimeSetupOptions, + type FirstTimeSetupResult, +} from "./first-time-setup.ts"; +export { FooterComponent } from "./footer.ts"; +export { keyHint, keyText, rawKeyHint } from "./keybinding-hints.ts"; +export { LoginDialogComponent } from "./login-dialog.ts"; +export { ModelSelectorComponent } from "./model-selector.ts"; +export { OAuthSelectorComponent } from "./oauth-selector.ts"; +export { type ModelsCallbacks, type ModelsConfig, ScopedModelsSelectorComponent } from "./scoped-models-selector.ts"; +export { SessionSelectorComponent } from "./session-selector.ts"; +export { type SettingsCallbacks, type SettingsConfig, SettingsSelectorComponent } from "./settings-selector.ts"; +export { ShowImagesSelectorComponent } from "./show-images-selector.ts"; +export { SkillInvocationMessageComponent } from "./skill-invocation-message.ts"; +export { ThemeSelectorComponent } from "./theme-selector.ts"; +export { ThinkingSelectorComponent } from "./thinking-selector.ts"; +export { ToolExecutionComponent, type ToolExecutionOptions } from "./tool-execution.ts"; +export { TreeSelectorComponent } from "./tree-selector.ts"; +export { TrustSelectorComponent } from "./trust-selector.ts"; +export { UserMessageComponent } from "./user-message.ts"; +export { UserMessageSelectorComponent } from "./user-message-selector.ts"; +export { truncateToVisualLines, type VisualTruncateResult } from "./visual-truncate.ts"; diff --git a/packages/coding-agent/src/modes/interactive/components/keybinding-hints.ts b/packages/coding-agent/src/modes/interactive/components/keybinding-hints.ts new file mode 100644 index 0000000000000000000000000000000000000000..22601abd9bf039bf9e5cc6510316d8aa9d8d37c8 --- /dev/null +++ b/packages/coding-agent/src/modes/interactive/components/keybinding-hints.ts @@ -0,0 +1,48 @@ +/** + * Utilities for formatting keybinding hints in the UI. + */ + +import { getKeybindings, type Keybinding, type KeyId } from "@earendil-works/pi-tui"; +import { theme } from "../theme/theme.ts"; + +export interface KeyTextFormatOptions { + capitalize?: boolean; +} + +function formatKeyPart(part: string, options: KeyTextFormatOptions): string { + const displayPart = process.platform === "darwin" && part.toLowerCase() === "alt" ? "option" : part; + return options.capitalize ? displayPart.charAt(0).toUpperCase() + displayPart.slice(1) : displayPart; +} + +export function formatKeyText(key: string, options: KeyTextFormatOptions = {}): string { + return key + .split("/") + .map((k) => + k + .split("+") + .map((part) => formatKeyPart(part, options)) + .join("+"), + ) + .join("/"); +} + +function formatKeys(keys: KeyId[], options: KeyTextFormatOptions = {}): string { + if (keys.length === 0) return ""; + return formatKeyText(keys.join("/"), options); +} + +export function keyText(keybinding: Keybinding): string { + return formatKeys(getKeybindings().getKeys(keybinding)); +} + +export function keyDisplayText(keybinding: Keybinding): string { + return formatKeys(getKeybindings().getKeys(keybinding), { capitalize: true }); +} + +export function keyHint(keybinding: Keybinding, description: string): string { + return theme.fg("dim", keyText(keybinding)) + theme.fg("muted", ` ${description}`); +} + +export function rawKeyHint(key: string, description: string): string { + return theme.fg("dim", formatKeyText(key)) + theme.fg("muted", ` ${description}`); +} diff --git a/packages/coding-agent/src/modes/interactive/components/mermaid.ts b/packages/coding-agent/src/modes/interactive/components/mermaid.ts new file mode 100644 index 0000000000000000000000000000000000000000..a8d5e3cdfee24ef7cf78813d8496cdf79b01e914 --- /dev/null +++ b/packages/coding-agent/src/modes/interactive/components/mermaid.ts @@ -0,0 +1,89 @@ +import { Marked, type Token } from "@earendil-works/pi-tui"; +import { type MermaidArt, render, type Span } from "grok-mermaid"; +import type { MarkdownTransformer } from "../../../core/extensions/types.ts"; +import type { MermaidRenderingMode } from "../../../core/settings-manager.ts"; +import type { Theme } from "../theme/theme.ts"; + +const markdownParser = new Marked(); + +interface MermaidTransformerOptions { + getMode: () => MermaidRenderingMode; + theme?: Theme; +} + +function isMermaid(token: Token): token is Token & { type: "code"; text: string; lang?: string } { + return token.type === "code" && token.lang?.trim().split(/\s+/, 1)[0]?.toLowerCase() === "mermaid"; +} + +function codeSpan(line: string): string { + // Encode each diagram row as inline code (` ... `) so Markdown preserves its spacing and + // box-drawing characters. Use a non-breaking space for blank rows because an + // empty code span has no visible height. + const content = line || "\u00a0"; + // CommonMark code spans use matching backtick delimiters, so choose one + // longer than any backtick run in the content (``hel`lo`` -> hel`lo). + // If the content starts or ends with a backtick, separating it from the + // delimiter with a space keeps that backtick as content; CommonMark removes + // the padding when rendering (`` `edge` `` -> `edge`). + // Mermaid labels can preserve backticks, for example: + // `┌──────────────┐ ┌──────────────┐` + // ```│ plain ` tick ├───▶│ two `` ticks │``` + // `└──────────────┘ └──────────────┘` + const longestBacktickRun = Math.max(0, ...Array.from(content.matchAll(/`+/g), (match) => match[0].length)); + const fence = "`".repeat(longestBacktickRun + 1); + const padding = content.startsWith("`") || content.endsWith("`") ? " " : ""; + return `${fence}${padding}${content}${padding}${fence}`; +} + +function styleSpan(span: Span, theme: Theme): string { + switch (span.cls) { + case "border": + return theme.fg("borderMuted", span.text); + case "text": + return theme.fg("text", span.text); + case "edge": + return theme.fg("accent", span.text); + case "edgeLabel": + return theme.fg("muted", span.text); + case "title": + return theme.fg("accent", theme.bold(span.text)); + case "none": + return span.text; + } +} + +function themedLines(art: MermaidArt, theme: Theme): string[] { + return art.styled.map((row) => row.map((span) => styleSpan(span, theme)).join("")); +} + +/** Create a transformer that replaces top-level Mermaid code blocks with Unicode terminal diagrams. */ +export function createMermaidMarkdownTransformer(options: MermaidTransformerOptions): MarkdownTransformer { + return (markdown, context) => { + const mode = options.getMode(); + if ( + mode === "off" || + context.messageType === "assistant-thinking" || + (context.isStreaming && mode !== "streaming") + ) { + return markdown; + } + + return markdownParser + .lexer(markdown) + .map((token) => { + if (!isMermaid(token)) return token.raw; + const art = render(token.text); + if (!art || art.width > context.availableWidth) return token.raw; + if (!context.isStreaming && art.warnings.length > 0) { + const suffix = art.warnings.length > 1 ? ` (+${art.warnings.length - 1} more)` : ""; + const warning = `Mermaid diagram not rendered: ${art.warnings[0]}${suffix}`; + const styledWarning = options.theme ? options.theme.fg("warning", warning) : warning; + return `${token.raw}\n${codeSpan(styledWarning)} \n`; + } + const lines = options.theme ? themedLines(art, options.theme) : art.plain; + // Markdown hard breaks keep every diagram row on its own line. + return `${lines.map(codeSpan).join(" \n")}\n`; + }) + .join(""); + }; +} diff --git a/packages/coding-agent/src/modes/interactive/components/model-selector.ts b/packages/coding-agent/src/modes/interactive/components/model-selector.ts new file mode 100644 index 0000000000000000000000000000000000000000..c8489c845c5023abe2958258635255bc362df052 --- /dev/null +++ b/packages/coding-agent/src/modes/interactive/components/model-selector.ts @@ -0,0 +1,421 @@ +import { type Model, modelsAreEqual } from "@earendil-works/pi-ai"; +import { + Container, + type Focusable, + fuzzyFilter, + getKeybindings, + Input, + Spacer, + Text, + type TUI, +} from "@earendil-works/pi-tui"; +import type { ModelRuntime } from "../../../core/model-runtime.ts"; +import { refreshModelCatalogs } from "../model-catalog-refresh.ts"; +import { getModelSelectorSearchText } from "../model-search.ts"; +import { theme } from "../theme/theme.ts"; +import { DynamicBorder } from "./dynamic-border.ts"; +import { keyDisplayText, keyHint } from "./keybinding-hints.ts"; + +interface ModelItem { + provider: string; + id: string; + model: Model; +} + +interface ScopedModelItem { + model: Model; + thinkingLevel?: string; +} + +interface DefaultModelReference { + provider: string; + id: string; +} + +type ModelScope = "all" | "scoped"; + +/** + * Component that renders a model selector with search + */ +export class ModelSelectorComponent extends Container implements Focusable { + private searchInput: Input; + + // Focusable implementation - propagate to searchInput for IME cursor positioning + private _focused = false; + get focused(): boolean { + return this._focused; + } + set focused(value: boolean) { + this._focused = value; + this.searchInput.focused = value; + } + private listContainer: Container; + private allModels: ModelItem[] = []; + private scopedModelItems: ModelItem[] = []; + private activeModels: ModelItem[] = []; + private filteredModels: ModelItem[] = []; + private selectedIndex: number = 0; + private currentModel?: Model; + private modelRuntime: ModelRuntime; + private onSelectCallback: (model: Model) => void; + private onSelectAsDefaultCallback?: (model: Model) => void; + private onCancelCallback: () => void; + private errorMessage?: string; + private refreshStatusMessage = "Refreshing model catalogs…"; + private refreshStatusSuccess = false; + private tui: TUI; + private scopedModels: ReadonlyArray; + private defaultModel?: DefaultModelReference; + private scope: ModelScope = "all"; + private scopeText?: Text; + private scopeHintText?: Text; + private readonly refreshAbortController = new AbortController(); + private refreshTimeout?: ReturnType; + private closed = false; + + constructor( + tui: TUI, + currentModel: Model | undefined, + modelRuntime: ModelRuntime, + scopedModels: ReadonlyArray, + onSelect: (model: Model) => void, + onCancel: () => void, + initialSearchInput?: string, + onSelectAsDefault?: (model: Model) => void, + defaultModel?: DefaultModelReference, + ) { + super(); + + this.tui = tui; + this.currentModel = currentModel; + this.modelRuntime = modelRuntime; + this.scopedModels = scopedModels; + this.defaultModel = defaultModel; + this.scope = scopedModels.length > 0 ? "scoped" : "all"; + this.onSelectCallback = onSelect; + this.onSelectAsDefaultCallback = onSelectAsDefault; + this.onCancelCallback = onCancel; + + // Add top border + this.addChild(new DynamicBorder()); + this.addChild(new Spacer(1)); + + // Add hint about model filtering + if (scopedModels.length > 0) { + this.scopeText = new Text(this.getScopeText(), 0, 0); + this.addChild(this.scopeText); + this.scopeHintText = new Text(this.getScopeHintText(), 0, 0); + this.addChild(this.scopeHintText); + } else { + const hintText = "Only showing models from configured providers. Use /login to add providers."; + this.addChild(new Text(theme.fg("warning", hintText), 0, 0)); + } + this.addChild(new Spacer(1)); + + // Create search input + this.searchInput = new Input(); + if (initialSearchInput) { + this.searchInput.setValue(initialSearchInput); + } + this.searchInput.onSubmit = () => { + // Enter on search input selects the first filtered item + if (this.filteredModels[this.selectedIndex]) { + this.handleSelect(this.filteredModels[this.selectedIndex].model); + } + }; + this.addChild(this.searchInput); + + this.addChild(new Spacer(1)); + + // Create list container + this.listContainer = new Container(); + this.addChild(this.listContainer); + + this.addChild(new Spacer(1)); + + // Hint + if (this.onSelectAsDefaultCallback) { + this.addChild( + new Text( + theme.fg( + "dim", + ` ${keyDisplayText("tui.select.confirm")} to select · ${keyDisplayText("app.models.save")} to set as default · ${keyDisplayText("tui.select.cancel")} to cancel`, + ), + 0, + 0, + ), + ); + } + + // Add bottom border + this.addChild(new DynamicBorder()); + + // Render the current snapshot immediately, then refresh in the background. + this.loadModelsFromSnapshot(); + if (initialSearchInput) this.filterModels(initialSearchInput); + else this.updateList(); + this.tui.requestRender(); + void this.refreshModels(); + } + + private loadModelsFromSnapshot(): void { + const models = this.modelRuntime.getAvailableSnapshot().map((model: Model) => ({ + provider: model.provider, + id: model.id, + model, + })); + this.allModels = this.sortModels(models); + this.scopedModels = this.scopedModels.map((scoped) => { + const refreshed = this.modelRuntime.getModel(scoped.model.provider, scoped.model.id); + return refreshed ? { ...scoped, model: refreshed } : scoped; + }); + this.scopedModelItems = this.scopedModels.map((scoped) => ({ + provider: scoped.model.provider, + id: scoped.model.id, + model: scoped.model, + })); + this.activeModels = this.scope === "scoped" ? this.scopedModelItems : this.allModels; + this.filteredModels = this.activeModels; + const currentIndex = this.filteredModels.findIndex((item) => modelsAreEqual(this.currentModel, item.model)); + this.selectedIndex = + currentIndex >= 0 ? currentIndex : Math.min(this.selectedIndex, Math.max(0, this.filteredModels.length - 1)); + } + + private async refreshModels(): Promise { + const timeoutMs = 15_000; + let timedOut = false; + this.refreshTimeout = setTimeout(() => { + timedOut = true; + this.refreshAbortController.abort(); + }, timeoutMs); + try { + const result = await refreshModelCatalogs(this.modelRuntime, this.refreshAbortController.signal); + if (this.closed) return; + this.refreshStatusMessage = ""; + if (result.aborted && timedOut) { + this.errorMessage = "Model refresh timed out; showing cached models."; + } else if (result.errors.size === 1) { + this.errorMessage = `Could not refresh ${result.errors.keys().next().value}; showing cached models.`; + } else if (result.errors.size > 1) { + this.errorMessage = `Could not refresh ${result.errors.size} model catalogs (${[...result.errors.keys()].join(", ")}); showing cached models.`; + } else { + this.errorMessage = this.modelRuntime.getError(); + if (!this.errorMessage) { + this.refreshStatusMessage = "Model catalogs refreshed."; + this.refreshStatusSuccess = true; + } + } + this.loadModelsFromSnapshot(); + this.filterModels(this.searchInput.getValue()); + this.tui.requestRender(); + } catch (error) { + if (this.closed) return; + this.refreshStatusMessage = ""; + this.errorMessage = timedOut + ? "Model refresh timed out; showing cached models." + : `Could not refresh model catalogs: ${error instanceof Error ? error.message : String(error)}`; + this.updateList(); + this.tui.requestRender(); + } finally { + if (this.refreshTimeout) clearTimeout(this.refreshTimeout); + } + } + + dispose(): void { + if (this.closed) return; + this.closed = true; + if (this.refreshTimeout) clearTimeout(this.refreshTimeout); + this.refreshAbortController.abort(); + } + + private sortModels(models: ModelItem[]): ModelItem[] { + const sorted = [...models]; + // Sort: current model first, default model second, then by provider. + sorted.sort((a, b) => { + const aIsCurrent = modelsAreEqual(this.currentModel, a.model); + const bIsCurrent = modelsAreEqual(this.currentModel, b.model); + if (aIsCurrent && !bIsCurrent) return -1; + if (!aIsCurrent && bIsCurrent) return 1; + const aIsDefault = this.isDefaultModel(a.model); + const bIsDefault = this.isDefaultModel(b.model); + if (aIsDefault && !bIsDefault) return -1; + if (!aIsDefault && bIsDefault) return 1; + return a.provider.localeCompare(b.provider); + }); + return sorted; + } + + private getScopeText(): string { + const allText = this.scope === "all" ? theme.fg("accent", "all") : theme.fg("muted", "all"); + const scopedText = this.scope === "scoped" ? theme.fg("accent", "scoped") : theme.fg("muted", "scoped"); + return `${theme.fg("muted", "Scope: ")}${allText}${theme.fg("muted", " | ")}${scopedText}`; + } + + private getScopeHintText(): string { + return keyHint("tui.input.tab", "scope") + theme.fg("muted", " (all/scoped)"); + } + + private isDefaultModel(model: Model): boolean { + return this.defaultModel?.provider === model.provider && this.defaultModel.id === model.id; + } + + private isDefaultSearch(query: string): boolean { + const normalized = query.trim().toLowerCase(); + return normalized.length > 0 && "default".startsWith(normalized); + } + + private setScope(scope: ModelScope): void { + if (this.scope === scope) return; + this.scope = scope; + this.activeModels = this.scope === "scoped" ? this.scopedModelItems : this.allModels; + const currentIndex = this.activeModels.findIndex((item) => modelsAreEqual(this.currentModel, item.model)); + this.selectedIndex = currentIndex >= 0 ? currentIndex : 0; + this.filterModels(this.searchInput.getValue()); + if (this.scopeText) { + this.scopeText.setText(this.getScopeText()); + } + } + + private filterModels(query: string): void { + if (query) { + const filtered = fuzzyFilter(this.activeModels, query, (item) => { + const defaultText = this.isDefaultModel(item.model) ? " default" : ""; + return `${getModelSelectorSearchText({ id: item.id, provider: item.provider, name: item.model.name })}${defaultText}`; + }); + if (this.isDefaultSearch(query)) { + const defaultItems = this.activeModels.filter((item) => this.isDefaultModel(item.model)); + const defaultKeys = new Set(defaultItems.map((item) => `${item.provider}\0${item.id}`)); + this.filteredModels = [ + ...defaultItems, + ...filtered.filter((item) => !defaultKeys.has(`${item.provider}\0${item.id}`)), + ]; + } else { + this.filteredModels = filtered; + } + } else { + this.filteredModels = this.activeModels; + } + // When filtering by a query, move the selector to the top row so the best + // match is highlighted. When the query is cleared, keep the current position + // clamped to the (restored) list length. + this.selectedIndex = query ? 0 : Math.min(this.selectedIndex, Math.max(0, this.filteredModels.length - 1)); + this.updateList(); + } + + private updateList(): void { + this.listContainer.clear(); + + const maxVisible = 10; + const startIndex = Math.max( + 0, + Math.min(this.selectedIndex - Math.floor(maxVisible / 2), this.filteredModels.length - maxVisible), + ); + const endIndex = Math.min(startIndex + maxVisible, this.filteredModels.length); + + // Show visible slice of filtered models + for (let i = startIndex; i < endIndex; i++) { + const item = this.filteredModels[i]; + if (!item) continue; + + const isSelected = i === this.selectedIndex; + const isCurrent = modelsAreEqual(this.currentModel, item.model); + const isDefault = this.isDefaultModel(item.model); + const defaultBadge = isDefault ? theme.fg("muted", " · default") : ""; + + const cursor = isSelected ? theme.fg("accent", "→ ") : " "; + const currentMarker = isCurrent ? theme.fg("accent", "✓ ") : " "; + const modelText = isSelected ? theme.fg("accent", item.id) : item.id; + const providerBadge = theme.fg("muted", `[${item.provider}]`); + const line = `${cursor}${currentMarker}${modelText} ${providerBadge}${defaultBadge}`; + + this.listContainer.addChild(new Text(line, 0, 0)); + } + + // Add scroll indicator if needed + if (startIndex > 0 || endIndex < this.filteredModels.length) { + const scrollInfo = theme.fg("muted", ` (${this.selectedIndex + 1}/${this.filteredModels.length})`); + this.listContainer.addChild(new Text(scrollInfo, 0, 0)); + } + + // Show error message or "no results" if empty + if (this.errorMessage) { + // Show error in red + const errorLines = this.errorMessage.split("\n"); + for (const line of errorLines) { + this.listContainer.addChild(new Text(theme.fg("error", line), 0, 0)); + } + } else if (this.filteredModels.length === 0) { + this.listContainer.addChild(new Text(theme.fg("muted", " No matching models"), 0, 0)); + } else { + const selected = this.filteredModels[this.selectedIndex]; + this.listContainer.addChild(new Spacer(1)); + this.listContainer.addChild(new Text(theme.fg("muted", ` Model Name: ${selected.model.name}`), 0, 0)); + } + if (this.refreshStatusMessage) { + this.listContainer.addChild(new Spacer(1)); + this.listContainer.addChild( + new Text(theme.fg(this.refreshStatusSuccess ? "success" : "muted", ` ${this.refreshStatusMessage}`), 0, 0), + ); + } + } + + handleInput(keyData: string): void { + const kb = getKeybindings(); + if (kb.matches(keyData, "tui.input.tab")) { + if (this.scopedModelItems.length > 0) { + const nextScope: ModelScope = this.scope === "all" ? "scoped" : "all"; + this.setScope(nextScope); + if (this.scopeHintText) { + this.scopeHintText.setText(this.getScopeHintText()); + } + } + return; + } + // Up arrow - wrap to bottom when at top + if (kb.matches(keyData, "tui.select.up")) { + if (this.filteredModels.length === 0) return; + this.selectedIndex = this.selectedIndex === 0 ? this.filteredModels.length - 1 : this.selectedIndex - 1; + this.updateList(); + } + // Down arrow - wrap to top when at bottom + else if (kb.matches(keyData, "tui.select.down")) { + if (this.filteredModels.length === 0) return; + this.selectedIndex = this.selectedIndex === this.filteredModels.length - 1 ? 0 : this.selectedIndex + 1; + this.updateList(); + } + // Enter + else if (kb.matches(keyData, "tui.select.confirm")) { + const selectedModel = this.filteredModels[this.selectedIndex]; + if (selectedModel) { + this.handleSelect(selectedModel.model); + } + } + // Escape or Ctrl+C + else if (kb.matches(keyData, "tui.select.cancel")) { + this.dispose(); + this.onCancelCallback(); + } + // Select and save as default + else if (kb.matches(keyData, "app.models.save") && this.onSelectAsDefaultCallback) { + const selectedModel = this.filteredModels[this.selectedIndex]; + if (selectedModel) { + this.dispose(); + this.onSelectAsDefaultCallback(selectedModel.model); + } + } + // Pass everything else to search input + else { + this.searchInput.handleInput(keyData); + this.filterModels(this.searchInput.getValue()); + } + } + + private handleSelect(model: Model): void { + this.dispose(); + this.onSelectCallback(model); + } + + getSearchInput(): Input { + return this.searchInput; + } +} diff --git a/packages/coding-agent/src/modes/interactive/components/scoped-models-selector.ts b/packages/coding-agent/src/modes/interactive/components/scoped-models-selector.ts new file mode 100644 index 0000000000000000000000000000000000000000..89c486de66c92534704f75baf28c5eed543dd3f2 --- /dev/null +++ b/packages/coding-agent/src/modes/interactive/components/scoped-models-selector.ts @@ -0,0 +1,401 @@ +import type { Model } from "@earendil-works/pi-ai"; +import { + Container, + type Focusable, + fuzzyFilter, + getKeybindings, + Input, + Key, + matchesKey, + Spacer, + Text, +} from "@earendil-works/pi-tui"; +import { getModelSearchText } from "../model-search.ts"; +import { theme } from "../theme/theme.ts"; +import { DynamicBorder } from "./dynamic-border.ts"; +import { keyDisplayText } from "./keybinding-hints.ts"; + +// EnabledIds: null = all enabled (no filter), string[] = explicit ordered list +type EnabledIds = string[] | null; + +function isEnabled(enabledIds: EnabledIds, id: string): boolean { + return enabledIds === null || enabledIds.includes(id); +} + +/** Collapse an explicit list back to null (= all enabled) when it covers every available model. */ +function normalizeEnabled(result: string[], allIds: string[]): EnabledIds { + return result.length === allIds.length && result.every((id) => allIds.includes(id)) ? null : result; +} + +function toggle(enabledIds: EnabledIds, allIds: string[], id: string): EnabledIds { + if (enabledIds === null) return allIds.filter((modelId) => modelId !== id); + const index = enabledIds.indexOf(id); + if (index >= 0) return [...enabledIds.slice(0, index), ...enabledIds.slice(index + 1)]; + return normalizeEnabled([...enabledIds, id], allIds); +} + +function enableAll(enabledIds: EnabledIds, allIds: string[], targetIds?: string[]): EnabledIds { + if (enabledIds === null) return null; // Already all enabled + const targets = targetIds ?? allIds; + const result = [...enabledIds]; + for (const id of targets) { + if (!result.includes(id)) result.push(id); + } + return normalizeEnabled(result, allIds); +} + +function clearAll(enabledIds: EnabledIds, allIds: string[], targetIds?: string[]): EnabledIds { + if (enabledIds === null) { + return targetIds ? allIds.filter((id) => !targetIds.includes(id)) : []; + } + const targets = new Set(targetIds ?? enabledIds); + return enabledIds.filter((id) => !targets.has(id)); +} + +function move(enabledIds: EnabledIds, id: string, delta: number): EnabledIds { + if (enabledIds === null) return null; + const list = [...enabledIds]; + const index = list.indexOf(id); + if (index < 0) return list; + const newIndex = index + delta; + if (newIndex < 0 || newIndex >= list.length) return list; + const result = [...list]; + [result[index], result[newIndex]] = [result[newIndex], result[index]]; + return result; +} + +function getSortedIds(enabledIds: EnabledIds, allIds: string[]): string[] { + if (enabledIds === null) return allIds; + const enabledSet = new Set(enabledIds); + return [...enabledIds, ...allIds.filter((id) => !enabledSet.has(id))]; +} + +interface ModelItem { + fullId: string; + model: Model | undefined; + enabled: boolean; +} + +export interface ModelsConfig { + allModels: Model[]; + enabledModelIds: string[] | null; + refreshStatus?: string; +} + +export interface ModelsCallbacks { + /** Called whenever the enabled model set or order changes (session-only, no persist) */ + onChange: (enabledModelIds: string[] | null) => void | Promise; + /** Called when user wants to persist current selection to settings */ + onPersist: (enabledModelIds: string[] | null) => void | Promise; + onCancel: () => void; +} + +/** + * Component for enabling/disabling models for Ctrl+P cycling. + * Changes are session-only until explicitly persisted with Ctrl+S. + */ +export class ScopedModelsSelectorComponent extends Container implements Focusable { + private modelsById: Map> = new Map(); + private allIds: string[] = []; + private enabledIds: EnabledIds = null; + private filteredItems: ModelItem[] = []; + private selectedIndex = 0; + private searchInput: Input; + + // Focusable implementation - propagate to searchInput for IME cursor positioning + private _focused = false; + get focused(): boolean { + return this._focused; + } + set focused(value: boolean) { + this._focused = value; + this.searchInput.focused = value; + } + private listContainer: Container; + private footerText: Text; + private callbacks: ModelsCallbacks; + private maxVisible = 8; + private isDirty = false; + private refreshStatusText?: Text; + + constructor(config: ModelsConfig, callbacks: ModelsCallbacks) { + super(); + this.callbacks = callbacks; + + for (const model of config.allModels) { + const fullId = `${model.provider}/${model.id}`; + this.modelsById.set(fullId, model); + this.allIds.push(fullId); + } + + this.enabledIds = config.enabledModelIds === null ? null : [...config.enabledModelIds]; + this.filteredItems = this.buildItems(); + + // Header + this.addChild(new DynamicBorder()); + this.addChild(new Spacer(1)); + this.addChild(new Text(theme.fg("accent", theme.bold("Model Configuration")), 0, 0)); + this.addChild( + new Text(theme.fg("muted", `Session-only. ${keyDisplayText("app.models.save")} to save to settings.`), 0, 0), + ); + this.addChild(new Spacer(1)); + + // Search input + this.searchInput = new Input(); + this.addChild(this.searchInput); + this.addChild(new Spacer(1)); + + // List container + this.listContainer = new Container(); + this.addChild(this.listContainer); + + // Footer hint + this.addChild(new Spacer(1)); + if (config.refreshStatus) { + this.refreshStatusText = new Text(theme.fg("muted", ` ${config.refreshStatus}`), 0, 0); + this.addChild(this.refreshStatusText); + } + this.footerText = new Text(this.getFooterText(), 0, 0); + this.addChild(this.footerText); + + this.addChild(new DynamicBorder()); + this.updateList(); + } + + updateModels(models: readonly Model[], enabledModelIds?: string[] | null): void { + const selectedId = this.filteredItems[this.selectedIndex]?.fullId; + if (enabledModelIds !== undefined) this.enabledIds = enabledModelIds === null ? null : [...enabledModelIds]; + this.modelsById.clear(); + this.allIds = []; + for (const model of models) { + const fullId = `${model.provider}/${model.id}`; + this.modelsById.set(fullId, model); + this.allIds.push(fullId); + } + this.refresh(); + const refreshedIndex = selectedId ? this.filteredItems.findIndex((item) => item.fullId === selectedId) : -1; + if (refreshedIndex >= 0) { + this.selectedIndex = refreshedIndex; + this.updateList(); + } + } + + setRefreshStatus(message: string, kind: "muted" | "success" | "warning"): void { + this.refreshStatusText?.setText(theme.fg(kind, ` ${message}`)); + } + + private buildItems(): ModelItem[] { + return getSortedIds(this.enabledIds, this.allIds).map((id) => ({ + fullId: id, + model: this.modelsById.get(id), + enabled: isEnabled(this.enabledIds, id), + })); + } + + private getFooterText(): string { + const enabledCount = this.enabledIds?.filter((id) => this.modelsById.has(id)).length ?? this.allIds.length; + const unavailableCount = this.enabledIds?.filter((id) => !this.modelsById.has(id)).length ?? 0; + const allEnabled = this.enabledIds === null; + const countText = allEnabled + ? "all enabled" + : `${enabledCount}/${this.allIds.length} enabled${unavailableCount ? ` · ${unavailableCount} unavailable` : ""}`; + const parts = [ + `${keyDisplayText("tui.select.confirm")} toggle`, + `${keyDisplayText("app.models.enableAll")} all`, + `${keyDisplayText("app.models.clearAll")} clear`, + `${keyDisplayText("app.models.toggleProvider")} provider`, + `${keyDisplayText("app.models.reorderUp")}/${keyDisplayText("app.models.reorderDown")} reorder`, + `${keyDisplayText("app.models.save")} save`, + countText, + ]; + return this.isDirty + ? theme.fg("dim", ` ${parts.join(" · ")} `) + theme.fg("warning", "(unsaved)") + : theme.fg("dim", ` ${parts.join(" · ")}`); + } + + private refresh(): void { + const query = this.searchInput.getValue(); + const items = this.buildItems(); + this.filteredItems = query + ? fuzzyFilter(items, query, (item) => + item.model + ? getModelSearchText({ id: item.model.id, provider: item.model.provider, name: item.model.name }) + : item.fullId, + ) + : items; + this.selectedIndex = Math.min(this.selectedIndex, Math.max(0, this.filteredItems.length - 1)); + this.updateList(); + this.footerText.setText(this.getFooterText()); + } + + private notifyChange(): void { + this.callbacks.onChange(this.enabledIds === null ? null : [...this.enabledIds]); + } + + private updateList(): void { + this.listContainer.clear(); + + if (this.filteredItems.length === 0) { + this.listContainer.addChild(new Text(theme.fg("muted", " No matching models"), 0, 0)); + return; + } + + const startIndex = Math.max( + 0, + Math.min(this.selectedIndex - Math.floor(this.maxVisible / 2), this.filteredItems.length - this.maxVisible), + ); + const endIndex = Math.min(startIndex + this.maxVisible, this.filteredItems.length); + for (let i = startIndex; i < endIndex; i++) { + const item = this.filteredItems[i]!; + const isSelected = i === this.selectedIndex; + const prefix = isSelected ? theme.fg("accent", "→ ") : " "; + const id = item.model?.id ?? item.fullId; + const styledId = item.model ? id : theme.strikethrough(id); + const modelText = isSelected ? theme.fg("accent", styledId) : styledId; + const providerBadge = theme.fg("muted", item.model ? ` [${item.model.provider}]` : " [unavailable]"); + const status = item.model && item.enabled ? theme.fg("accent", "✓ ") : " "; + this.listContainer.addChild(new Text(`${prefix}${status}${modelText}${providerBadge}`, 0, 0)); + } + + // Add scroll indicator if needed + if (startIndex > 0 || endIndex < this.filteredItems.length) { + this.listContainer.addChild( + new Text(theme.fg("muted", ` (${this.selectedIndex + 1}/${this.filteredItems.length})`), 0, 0), + ); + } + + if (this.filteredItems.length > 0) { + const selected = this.filteredItems[this.selectedIndex]; + this.listContainer.addChild(new Spacer(1)); + this.listContainer.addChild( + new Text( + theme.fg("muted", ` ${selected.model ? `Model Name: ${selected.model.name}` : "Model unavailable"}`), + 0, + 0, + ), + ); + } + } + + handleInput(data: string): void { + const kb = getKeybindings(); + + // Navigation + if (kb.matches(data, "tui.select.up")) { + if (this.filteredItems.length === 0) return; + this.selectedIndex = this.selectedIndex === 0 ? this.filteredItems.length - 1 : this.selectedIndex - 1; + this.updateList(); + return; + } + if (kb.matches(data, "tui.select.down")) { + if (this.filteredItems.length === 0) return; + this.selectedIndex = this.selectedIndex === this.filteredItems.length - 1 ? 0 : this.selectedIndex + 1; + this.updateList(); + return; + } + + // Reorder enabled models + const reorderUp = kb.matches(data, "app.models.reorderUp"); + const reorderDown = kb.matches(data, "app.models.reorderDown"); + if (reorderUp || reorderDown) { + if (this.enabledIds === null) return; + const item = this.filteredItems[this.selectedIndex]; + if (item && isEnabled(this.enabledIds, item.fullId)) { + const delta = reorderUp ? -1 : 1; + const currentIndex = this.enabledIds.indexOf(item.fullId); + const newIndex = currentIndex + delta; + // Only move if within bounds + if (newIndex >= 0 && newIndex < this.enabledIds.length) { + this.enabledIds = move(this.enabledIds, item.fullId, delta); + this.isDirty = true; + this.selectedIndex += delta; + this.refresh(); + this.notifyChange(); + } + } + return; + } + + // Toggle on Enter + if (kb.matches(data, "tui.select.confirm")) { + const item = this.filteredItems[this.selectedIndex]; + if (item) { + this.enabledIds = toggle(this.enabledIds, this.allIds, item.fullId); + this.isDirty = true; + this.refresh(); + this.notifyChange(); + } + return; + } + + // Enable all (filtered if search active, otherwise all) + if (kb.matches(data, "app.models.enableAll")) { + const targetIds = this.searchInput.getValue() ? this.filteredItems.map((i) => i.fullId) : undefined; + this.enabledIds = enableAll(this.enabledIds, this.allIds, targetIds); + this.isDirty = true; + this.refresh(); + this.notifyChange(); + return; + } + + // Clear all (filtered if search active, otherwise all) + if (kb.matches(data, "app.models.clearAll")) { + const targetIds = this.searchInput.getValue() ? this.filteredItems.map((i) => i.fullId) : undefined; + this.enabledIds = clearAll(this.enabledIds, this.allIds, targetIds); + this.isDirty = true; + this.refresh(); + this.notifyChange(); + return; + } + + // Toggle provider of current item + if (kb.matches(data, "app.models.toggleProvider")) { + const item = this.filteredItems[this.selectedIndex]; + if (item?.model) { + const provider = item.model.provider; + const providerIds = this.allIds.filter((id) => this.modelsById.get(id)!.provider === provider); + const allEnabled = providerIds.every((id) => isEnabled(this.enabledIds, id)); + this.enabledIds = allEnabled + ? clearAll(this.enabledIds, this.allIds, providerIds) + : enableAll(this.enabledIds, this.allIds, providerIds); + this.isDirty = true; + this.refresh(); + this.notifyChange(); + } + return; + } + + // Save/persist to settings + if (kb.matches(data, "app.models.save")) { + this.callbacks.onPersist(this.enabledIds === null ? null : [...this.enabledIds]); + this.isDirty = false; + this.footerText.setText(this.getFooterText()); + return; + } + + // Ctrl+C - clear search or cancel if empty + if (matchesKey(data, Key.ctrl("c"))) { + if (this.searchInput.getValue()) { + this.searchInput.setValue(""); + this.refresh(); + } else { + this.callbacks.onCancel(); + } + return; + } + + // Escape - cancel + if (matchesKey(data, Key.escape)) { + this.callbacks.onCancel(); + return; + } + + // Pass everything else to search input + this.searchInput.handleInput(data); + this.refresh(); + } + + getSearchInput(): Input { + return this.searchInput; + } +} diff --git a/packages/coding-agent/src/modes/interactive/components/session-selector-search.ts b/packages/coding-agent/src/modes/interactive/components/session-selector-search.ts new file mode 100644 index 0000000000000000000000000000000000000000..9b5bf2327478b8eb12711a72aef0888673b2040f --- /dev/null +++ b/packages/coding-agent/src/modes/interactive/components/session-selector-search.ts @@ -0,0 +1,194 @@ +import { fuzzyMatch } from "@earendil-works/pi-tui"; +import type { SessionInfo } from "../../../core/session-manager.ts"; + +export type SortMode = "threaded" | "recent" | "relevance"; + +export type NameFilter = "all" | "named"; + +export interface ParsedSearchQuery { + mode: "tokens" | "regex"; + tokens: { kind: "fuzzy" | "phrase"; value: string }[]; + regex: RegExp | null; + /** If set, parsing failed and we should treat query as non-matching. */ + error?: string; +} + +export interface MatchResult { + matches: boolean; + /** Lower is better; only meaningful when matches === true */ + score: number; +} + +function normalizeWhitespaceLower(text: string): string { + return text.toLowerCase().replace(/\s+/g, " ").trim(); +} + +function getSessionSearchText(session: SessionInfo): string { + return `${session.id} ${session.name ?? ""} ${session.allMessagesText} ${session.cwd}`; +} + +export function hasSessionName(session: SessionInfo): boolean { + return Boolean(session.name?.trim()); +} + +function matchesNameFilter(session: SessionInfo, filter: NameFilter): boolean { + if (filter === "all") return true; + return hasSessionName(session); +} + +export function parseSearchQuery(query: string): ParsedSearchQuery { + const trimmed = query.trim(); + if (!trimmed) { + return { mode: "tokens", tokens: [], regex: null }; + } + + // Regex mode: re: + if (trimmed.startsWith("re:")) { + const pattern = trimmed.slice(3).trim(); + if (!pattern) { + return { mode: "regex", tokens: [], regex: null, error: "Empty regex" }; + } + try { + return { mode: "regex", tokens: [], regex: new RegExp(pattern, "i") }; + } catch (err) { + const msg = err instanceof Error ? err.message : String(err); + return { mode: "regex", tokens: [], regex: null, error: msg }; + } + } + + // Token mode with quote support. + // Example: foo "node cve" bar + const tokens: { kind: "fuzzy" | "phrase"; value: string }[] = []; + let buf = ""; + let inQuote = false; + let hadUnclosedQuote = false; + + const flush = (kind: "fuzzy" | "phrase"): void => { + const v = buf.trim(); + buf = ""; + if (!v) return; + tokens.push({ kind, value: v }); + }; + + for (let i = 0; i < trimmed.length; i++) { + const ch = trimmed[i]!; + if (ch === '"') { + if (inQuote) { + flush("phrase"); + inQuote = false; + } else { + flush("fuzzy"); + inQuote = true; + } + continue; + } + + if (!inQuote && /\s/.test(ch)) { + flush("fuzzy"); + continue; + } + + buf += ch; + } + + if (inQuote) { + hadUnclosedQuote = true; + } + + // If quotes were unbalanced, fall back to plain whitespace tokenization. + if (hadUnclosedQuote) { + return { + mode: "tokens", + tokens: trimmed + .split(/\s+/) + .map((t) => t.trim()) + .filter((t) => t.length > 0) + .map((t) => ({ kind: "fuzzy" as const, value: t })), + regex: null, + }; + } + + flush(inQuote ? "phrase" : "fuzzy"); + + return { mode: "tokens", tokens, regex: null }; +} + +export function matchSession(session: SessionInfo, parsed: ParsedSearchQuery): MatchResult { + const text = getSessionSearchText(session); + + if (parsed.mode === "regex") { + if (!parsed.regex) { + return { matches: false, score: 0 }; + } + const idx = text.search(parsed.regex); + if (idx < 0) return { matches: false, score: 0 }; + return { matches: true, score: idx * 0.1 }; + } + + if (parsed.tokens.length === 0) { + return { matches: true, score: 0 }; + } + + let totalScore = 0; + let normalizedText: string | null = null; + + for (const token of parsed.tokens) { + if (token.kind === "phrase") { + if (normalizedText === null) { + normalizedText = normalizeWhitespaceLower(text); + } + const phrase = normalizeWhitespaceLower(token.value); + if (!phrase) continue; + const idx = normalizedText.indexOf(phrase); + if (idx < 0) return { matches: false, score: 0 }; + totalScore += idx * 0.1; + continue; + } + + const m = fuzzyMatch(token.value, text); + if (!m.matches) return { matches: false, score: 0 }; + totalScore += m.score; + } + + return { matches: true, score: totalScore }; +} + +export function filterAndSortSessions( + sessions: SessionInfo[], + query: string, + sortMode: SortMode, + nameFilter: NameFilter = "all", +): SessionInfo[] { + const nameFiltered = + nameFilter === "all" ? sessions : sessions.filter((session) => matchesNameFilter(session, nameFilter)); + const trimmed = query.trim(); + if (!trimmed) return nameFiltered; + + const parsed = parseSearchQuery(query); + if (parsed.error) return []; + + // Recent mode: filter only, keep incoming order. + if (sortMode === "recent") { + const filtered: SessionInfo[] = []; + for (const s of nameFiltered) { + const res = matchSession(s, parsed); + if (res.matches) filtered.push(s); + } + return filtered; + } + + // Relevance mode: sort by score, tie-break by modified desc. + const scored: { session: SessionInfo; score: number }[] = []; + for (const s of nameFiltered) { + const res = matchSession(s, parsed); + if (!res.matches) continue; + scored.push({ session: s, score: res.score }); + } + + scored.sort((a, b) => { + if (a.score !== b.score) return a.score - b.score; + return b.session.modified.getTime() - a.session.modified.getTime(); + }); + + return scored.map((r) => r.session); +} diff --git a/packages/coding-agent/src/modes/interactive/components/session-selector.ts b/packages/coding-agent/src/modes/interactive/components/session-selector.ts new file mode 100644 index 0000000000000000000000000000000000000000..4949eee5124d5306240ddeb5e35200e23f9ca228 --- /dev/null +++ b/packages/coding-agent/src/modes/interactive/components/session-selector.ts @@ -0,0 +1,1031 @@ +import { spawnSync } from "node:child_process"; +import { existsSync } from "node:fs"; +import { unlink } from "node:fs/promises"; +import * as os from "node:os"; +import { + type Component, + Container, + type Focusable, + getKeybindings, + Input, + Spacer, + Text, + truncateToWidth, + visibleWidth, +} from "@earendil-works/pi-tui"; +import { KeybindingsManager } from "../../../core/keybindings.ts"; +import type { SessionInfo, SessionListProgress } from "../../../core/session-manager.ts"; +import { canonicalizePath as _canonicalizePath } from "../../../utils/paths.ts"; +import { theme } from "../theme/theme.ts"; +import { DynamicBorder } from "./dynamic-border.ts"; +import { keyHint, keyText } from "./keybinding-hints.ts"; +import { filterAndSortSessions, hasSessionName, type NameFilter, type SortMode } from "./session-selector-search.ts"; + +type SessionScope = "current" | "all"; + +function shortenPath(path: string): string { + const home = os.homedir(); + if (!path) return path; + if (path.startsWith(home)) { + return `~${path.slice(home.length)}`; + } + return path; +} + +function formatSessionDate(date: Date): string { + const now = new Date(); + const diffMs = now.getTime() - date.getTime(); + const diffMins = Math.floor(diffMs / 60000); + const diffHours = Math.floor(diffMs / 3600000); + const diffDays = Math.floor(diffMs / 86400000); + + if (diffMins < 1) return "now"; + if (diffMins < 60) return `${diffMins}m`; + if (diffHours < 24) return `${diffHours}h`; + if (diffDays < 7) return `${diffDays}d`; + if (diffDays < 30) return `${Math.floor(diffDays / 7)}w`; + if (diffDays < 365) return `${Math.floor(diffDays / 30)}mo`; + return `${Math.floor(diffDays / 365)}y`; +} + +function canonicalizePath(path: string | undefined): string | undefined { + if (!path) return path; + return _canonicalizePath(path); +} + +class SessionSelectorHeader implements Component { + private scope: SessionScope; + private sortMode: SortMode; + private nameFilter: NameFilter; + private requestRender: () => void; + private loading = false; + private loadProgress: { loaded: number; total: number } | null = null; + private showPath = false; + private confirmingDeletePath: string | null = null; + private statusMessage: { type: "info" | "error"; message: string } | null = null; + private statusTimeout: ReturnType | null = null; + private showRenameHint = false; + + constructor(scope: SessionScope, sortMode: SortMode, nameFilter: NameFilter, requestRender: () => void) { + this.scope = scope; + this.sortMode = sortMode; + this.nameFilter = nameFilter; + this.requestRender = requestRender; + } + + setScope(scope: SessionScope): void { + this.scope = scope; + } + + setSortMode(sortMode: SortMode): void { + this.sortMode = sortMode; + } + + setNameFilter(nameFilter: NameFilter): void { + this.nameFilter = nameFilter; + } + + setLoading(loading: boolean): void { + this.loading = loading; + // Progress is scoped to the current load; clear whenever the loading state is set + this.loadProgress = null; + } + + setProgress(loaded: number, total: number): void { + this.loadProgress = { loaded, total }; + } + + setShowPath(showPath: boolean): void { + this.showPath = showPath; + } + + setShowRenameHint(show: boolean): void { + this.showRenameHint = show; + } + + setConfirmingDeletePath(path: string | null): void { + this.confirmingDeletePath = path; + } + + private clearStatusTimeout(): void { + if (!this.statusTimeout) return; + clearTimeout(this.statusTimeout); + this.statusTimeout = null; + } + + setStatusMessage(msg: { type: "info" | "error"; message: string } | null, autoHideMs?: number): void { + this.clearStatusTimeout(); + this.statusMessage = msg; + if (!msg || !autoHideMs) return; + + this.statusTimeout = setTimeout(() => { + this.statusMessage = null; + this.statusTimeout = null; + this.requestRender(); + }, autoHideMs); + } + + invalidate(): void {} + + render(width: number): string[] { + const title = this.scope === "current" ? "Resume Session (Current Folder)" : "Resume Session (All)"; + const leftText = theme.bold(title); + + const sortLabel = this.sortMode === "threaded" ? "Threaded" : this.sortMode === "recent" ? "Recent" : "Fuzzy"; + const sortText = theme.fg("muted", "Sort: ") + theme.fg("accent", sortLabel); + + const nameLabel = this.nameFilter === "all" ? "All" : "Named"; + const nameText = theme.fg("muted", "Name: ") + theme.fg("accent", nameLabel); + + let scopeText: string; + if (this.loading) { + const progressText = this.loadProgress ? `${this.loadProgress.loaded}/${this.loadProgress.total}` : "..."; + scopeText = `${theme.fg("muted", "○ Current Folder | ")}${theme.fg("accent", `Loading ${progressText}`)}`; + } else if (this.scope === "current") { + scopeText = `${theme.fg("accent", "◉ Current Folder")}${theme.fg("muted", " | ○ All")}`; + } else { + scopeText = `${theme.fg("muted", "○ Current Folder | ")}${theme.fg("accent", "◉ All")}`; + } + + const rightText = truncateToWidth(`${scopeText} ${nameText} ${sortText}`, width, ""); + const availableLeft = Math.max(0, width - visibleWidth(rightText) - 1); + const left = truncateToWidth(leftText, availableLeft, ""); + const spacing = Math.max(0, width - visibleWidth(left) - visibleWidth(rightText)); + + // Build hint lines - changes based on state (all branches truncate to width) + let hintLine1: string; + let hintLine2: string; + if (this.confirmingDeletePath !== null) { + const confirmHint = `Delete session? ${keyHint("tui.select.confirm", "confirm")} · ${keyHint("tui.select.cancel", "cancel")}`; + hintLine1 = theme.fg("error", truncateToWidth(confirmHint, width, "…")); + hintLine2 = ""; + } else if (this.statusMessage) { + const color = this.statusMessage.type === "error" ? "error" : "accent"; + hintLine1 = theme.fg(color, truncateToWidth(this.statusMessage.message, width, "…")); + hintLine2 = ""; + } else { + const pathState = this.showPath ? "(on)" : "(off)"; + const sep = theme.fg("muted", " · "); + const hint1 = + keyHint("tui.input.tab", "scope") + sep + theme.fg("muted", 're: regex · "phrase" exact'); + const hint2Parts = [ + keyHint("app.session.toggleSort", "sort"), + keyHint("app.session.toggleNamedFilter", "named"), + keyHint("app.session.delete", "delete"), + keyHint("app.session.togglePath", `path ${pathState}`), + ]; + if (this.showRenameHint) { + hint2Parts.push(keyHint("app.session.rename", "rename")); + } + const hint2 = hint2Parts.join(sep); + hintLine1 = truncateToWidth(hint1, width, "…"); + hintLine2 = truncateToWidth(hint2, width, "…"); + } + + return [`${left}${" ".repeat(spacing)}${rightText}`, hintLine1, hintLine2]; + } +} + +/** A session tree node for hierarchical display */ +interface SessionTreeNode { + session: SessionInfo; + children: SessionTreeNode[]; + latestActivity: number; +} + +/** Flattened node for display with tree structure info */ +interface FlatSessionNode { + session: SessionInfo; + depth: number; + isLast: boolean; + /** For each ancestor level, whether there are more siblings after it */ + ancestorContinues: boolean[]; +} + +/** + * Build a tree structure from sessions based on parentSessionPath. + * Returns root nodes sorted by modified date (descending). + */ +function buildSessionTree(sessions: SessionInfo[]): SessionTreeNode[] { + const byPath = new Map(); + + for (const session of sessions) { + const sessionPath = canonicalizePath(session.path) ?? session.path; + byPath.set(sessionPath, { session, children: [], latestActivity: session.modified.getTime() }); + } + + const roots: SessionTreeNode[] = []; + + for (const session of sessions) { + const sessionPath = canonicalizePath(session.path) ?? session.path; + const node = byPath.get(sessionPath)!; + const parentPath = canonicalizePath(session.parentSessionPath); + + if (parentPath && byPath.has(parentPath)) { + byPath.get(parentPath)!.children.push(node); + } else { + roots.push(node); + } + } + + const updateLatestActivity = (node: SessionTreeNode): number => { + let latestActivity = node.session.modified.getTime(); + for (const child of node.children) { + latestActivity = Math.max(latestActivity, updateLatestActivity(child)); + } + node.latestActivity = latestActivity; + return latestActivity; + }; + + for (const root of roots) { + updateLatestActivity(root); + } + + // Sort children and roots by latest activity in each subtree (descending) + const sortNodes = (nodes: SessionTreeNode[]): void => { + nodes.sort((a, b) => b.latestActivity - a.latestActivity); + for (const node of nodes) { + sortNodes(node.children); + } + }; + sortNodes(roots); + + return roots; +} + +/** + * Flatten tree into display list with tree structure metadata. + */ +function flattenSessionTree(roots: SessionTreeNode[]): FlatSessionNode[] { + const result: FlatSessionNode[] = []; + + const walk = (node: SessionTreeNode, depth: number, ancestorContinues: boolean[], isLast: boolean): void => { + result.push({ session: node.session, depth, isLast, ancestorContinues }); + + for (let i = 0; i < node.children.length; i++) { + const childIsLast = i === node.children.length - 1; + // Only show continuation line for non-root ancestors + const continues = depth > 0 ? !isLast : false; + walk(node.children[i]!, depth + 1, [...ancestorContinues, continues], childIsLast); + } + }; + + for (let i = 0; i < roots.length; i++) { + walk(roots[i]!, 0, [], i === roots.length - 1); + } + + return result; +} + +/** + * Custom session list component with multi-line items and search + */ +class SessionList implements Component, Focusable { + public getSelectedSessionPath(): string | undefined { + const selected = this.filteredSessions[this.selectedIndex]; + return selected?.session.path; + } + private allSessions: SessionInfo[] = []; + private filteredSessions: FlatSessionNode[] = []; + private selectedIndex: number = 0; + private searchInput: Input; + private showCwd = false; + private sortMode: SortMode = "threaded"; + private nameFilter: NameFilter = "all"; + private keybindings: KeybindingsManager; + private showPath = false; + private confirmingDeletePath: string | null = null; + private currentSessionCanonicalPath?: string; + public onSelect?: (sessionPath: string) => void; + public onCancel?: () => void; + public onExit: () => void = () => {}; + public onToggleScope?: () => void; + public onToggleSort?: () => void; + public onToggleNameFilter?: () => void; + public onTogglePath?: (showPath: boolean) => void; + public onDeleteConfirmationChange?: (path: string | null) => void; + public onDeleteSession?: (sessionPath: string) => Promise; + public onRenameSession?: (sessionPath: string) => void; + public onError?: (message: string) => void; + private maxVisible: number = 10; // Max sessions visible (one line each) + + // Focusable implementation - propagate to searchInput for IME cursor positioning + private _focused = false; + get focused(): boolean { + return this._focused; + } + set focused(value: boolean) { + this._focused = value; + this.searchInput.focused = value; + } + + constructor( + sessions: SessionInfo[], + showCwd: boolean, + sortMode: SortMode, + nameFilter: NameFilter, + keybindings: KeybindingsManager, + currentSessionFilePath?: string, + ) { + this.allSessions = sessions; + this.filteredSessions = []; + this.searchInput = new Input(); + this.showCwd = showCwd; + this.sortMode = sortMode; + this.nameFilter = nameFilter; + this.keybindings = keybindings; + this.currentSessionCanonicalPath = canonicalizePath(currentSessionFilePath); + this.filterSessions(""); + + // Handle Enter in search input - select current item + this.searchInput.onSubmit = () => { + if (this.filteredSessions[this.selectedIndex]) { + const selected = this.filteredSessions[this.selectedIndex]; + if (this.onSelect) { + this.onSelect(selected.session.path); + } + } + }; + } + + setSortMode(sortMode: SortMode): void { + this.sortMode = sortMode; + this.filterSessions(this.searchInput.getValue()); + } + + setNameFilter(nameFilter: NameFilter): void { + this.nameFilter = nameFilter; + this.filterSessions(this.searchInput.getValue()); + } + + setSessions(sessions: SessionInfo[], showCwd: boolean): void { + this.allSessions = sessions; + this.showCwd = showCwd; + this.filterSessions(this.searchInput.getValue()); + } + + private filterSessions(query: string): void { + const trimmed = query.trim(); + const nameFiltered = + this.nameFilter === "all" ? this.allSessions : this.allSessions.filter((session) => hasSessionName(session)); + + if (this.sortMode === "threaded" && !trimmed) { + // Threaded mode without search: show tree structure + const roots = buildSessionTree(nameFiltered); + this.filteredSessions = flattenSessionTree(roots); + } else { + // Other modes or with search: flat list + const filtered = filterAndSortSessions(nameFiltered, query, this.sortMode, "all"); + this.filteredSessions = filtered.map((session) => ({ + session, + depth: 0, + isLast: true, + ancestorContinues: [], + })); + } + this.selectedIndex = Math.min(this.selectedIndex, Math.max(0, this.filteredSessions.length - 1)); + } + + private setConfirmingDeletePath(path: string | null): void { + this.confirmingDeletePath = path; + this.onDeleteConfirmationChange?.(path); + } + + private startDeleteConfirmationForSelectedSession(): void { + const selected = this.filteredSessions[this.selectedIndex]; + if (!selected) return; + + // Prevent deleting current session + if (this.isCurrentSessionPath(selected.session.path)) { + this.onError?.("Cannot delete the currently active session"); + return; + } + + this.setConfirmingDeletePath(selected.session.path); + } + + private isCurrentSessionPath(path: string): boolean { + if (!this.currentSessionCanonicalPath) return false; + return (canonicalizePath(path) ?? path) === this.currentSessionCanonicalPath; + } + + invalidate(): void {} + + render(width: number): string[] { + const lines: string[] = []; + + // Render search input + lines.push(...this.searchInput.render(width)); + lines.push(""); // Blank line after search + + if (this.filteredSessions.length === 0) { + let emptyMessage: string; + if (this.nameFilter === "named") { + const toggleKey = keyText("app.session.toggleNamedFilter"); + if (this.showCwd) { + emptyMessage = ` No named sessions found. Press ${toggleKey} to show all.`; + } else { + emptyMessage = ` No named sessions in current folder. Press ${toggleKey} to show all, or Tab to view all.`; + } + } else if (this.showCwd) { + // "All" scope - no sessions anywhere that match filter + emptyMessage = " No sessions found"; + } else { + // "Current folder" scope - hint to try "all" + emptyMessage = " No sessions in current folder. Press Tab to view all."; + } + lines.push(theme.fg("muted", truncateToWidth(emptyMessage, width, "…"))); + return lines; + } + + // Calculate visible range with scrolling + const startIndex = Math.max( + 0, + Math.min(this.selectedIndex - Math.floor(this.maxVisible / 2), this.filteredSessions.length - this.maxVisible), + ); + const endIndex = Math.min(startIndex + this.maxVisible, this.filteredSessions.length); + + // Render visible sessions (one line each with tree structure) + for (let i = startIndex; i < endIndex; i++) { + const node = this.filteredSessions[i]!; + const session = node.session; + const isSelected = i === this.selectedIndex; + const isConfirmingDelete = session.path === this.confirmingDeletePath; + const isCurrent = this.isCurrentSessionPath(session.path); + + // Build tree prefix + const prefix = this.buildTreePrefix(node); + + // Session display text (name or first message) + const hasName = !!session.name; + const displayText = session.name ?? session.firstMessage; + const normalizedMessage = displayText.replace(/[\x00-\x1f\x7f]/g, " ").trim(); + + // Right side: message count and age + const age = formatSessionDate(session.modified); + const msgCount = String(session.messageCount); + let rightPart = `${msgCount} ${age}`; + if (this.showCwd && session.cwd) { + rightPart = `${shortenPath(session.cwd)} ${rightPart}`; + } + if (this.showPath) { + rightPart = `${shortenPath(session.path)} ${rightPart}`; + } + + // Cursor + const cursor = isSelected ? theme.fg("accent", "› ") : " "; + + // Calculate available width for message + const prefixWidth = visibleWidth(prefix); + const rightWidth = visibleWidth(rightPart) + 2; // +2 for spacing + const availableForMsg = width - 2 - prefixWidth - rightWidth; // -2 for cursor + + const truncatedMsg = truncateToWidth(normalizedMessage, Math.max(10, availableForMsg), "…"); + + // Style message + let messageColor: "error" | "warning" | "accent" | null = null; + if (isConfirmingDelete) { + messageColor = "error"; + } else if (isCurrent) { + messageColor = "accent"; + } else if (hasName) { + messageColor = "warning"; + } + let styledMsg = messageColor ? theme.fg(messageColor, truncatedMsg) : truncatedMsg; + if (isSelected) { + styledMsg = theme.bold(styledMsg); + } + + // Build line + const leftPart = cursor + theme.fg("dim", prefix) + styledMsg; + const leftWidth = visibleWidth(leftPart); + const spacing = Math.max(1, width - leftWidth - visibleWidth(rightPart)); + const styledRight = theme.fg(isConfirmingDelete ? "error" : "dim", rightPart); + + let line = leftPart + " ".repeat(spacing) + styledRight; + if (isSelected) { + line = theme.bg("selectedBg", line); + } + lines.push(truncateToWidth(line, width)); + } + + // Add scroll indicator if needed + if (startIndex > 0 || endIndex < this.filteredSessions.length) { + const scrollText = ` (${this.selectedIndex + 1}/${this.filteredSessions.length})`; + const scrollInfo = theme.fg("muted", truncateToWidth(scrollText, width, "")); + lines.push(scrollInfo); + } + + return lines; + } + + private buildTreePrefix(node: FlatSessionNode): string { + if (node.depth === 0) { + return ""; + } + + const parts = node.ancestorContinues.map((continues) => (continues ? "│ " : " ")); + const branch = node.isLast ? "└─ " : "├─ "; + return parts.join("") + branch; + } + + handleInput(keyData: string): void { + const kb = getKeybindings(); + + // Handle delete confirmation state first - intercept all keys + if (this.confirmingDeletePath !== null) { + if (kb.matches(keyData, "tui.select.confirm")) { + const pathToDelete = this.confirmingDeletePath; + this.setConfirmingDeletePath(null); + void this.onDeleteSession?.(pathToDelete); + return; + } + if (kb.matches(keyData, "tui.select.cancel")) { + this.setConfirmingDeletePath(null); + return; + } + // Ignore all other keys while confirming + return; + } + + if (kb.matches(keyData, "tui.input.tab")) { + if (this.onToggleScope) { + this.onToggleScope(); + } + return; + } + + if (kb.matches(keyData, "app.session.toggleSort")) { + this.onToggleSort?.(); + return; + } + + if (this.keybindings.matches(keyData, "app.session.toggleNamedFilter")) { + this.onToggleNameFilter?.(); + return; + } + + // Ctrl+P: toggle path display + if (kb.matches(keyData, "app.session.togglePath")) { + this.showPath = !this.showPath; + this.onTogglePath?.(this.showPath); + return; + } + + // Ctrl+D: initiate delete confirmation (useful on terminals that don't distinguish Ctrl+Backspace from Backspace) + if (kb.matches(keyData, "app.session.delete")) { + this.startDeleteConfirmationForSelectedSession(); + return; + } + + // Rename selected session + if (kb.matches(keyData, "app.session.rename")) { + const selected = this.filteredSessions[this.selectedIndex]; + if (selected) { + this.onRenameSession?.(selected.session.path); + } + return; + } + + // Ctrl+Backspace: non-invasive convenience alias for delete + // Only triggers deletion when the query is empty; otherwise it is forwarded to the input + if (kb.matches(keyData, "app.session.deleteNoninvasive")) { + if (this.searchInput.getValue().length > 0) { + this.searchInput.handleInput(keyData); + this.filterSessions(this.searchInput.getValue()); + return; + } + + this.startDeleteConfirmationForSelectedSession(); + return; + } + + // Up arrow + if (kb.matches(keyData, "tui.select.up")) { + this.selectedIndex = Math.max(0, this.selectedIndex - 1); + } + // Down arrow + else if (kb.matches(keyData, "tui.select.down")) { + this.selectedIndex = Math.min(this.filteredSessions.length - 1, this.selectedIndex + 1); + } + // Page up - jump up by maxVisible items + else if (kb.matches(keyData, "tui.select.pageUp")) { + this.selectedIndex = Math.max(0, this.selectedIndex - this.maxVisible); + } + // Page down - jump down by maxVisible items + else if (kb.matches(keyData, "tui.select.pageDown")) { + this.selectedIndex = Math.min(this.filteredSessions.length - 1, this.selectedIndex + this.maxVisible); + } + // Enter + else if (kb.matches(keyData, "tui.select.confirm")) { + const selected = this.filteredSessions[this.selectedIndex]; + if (selected && this.onSelect) { + this.onSelect(selected.session.path); + } + } + // Escape - cancel + else if (kb.matches(keyData, "tui.select.cancel")) { + if (this.onCancel) { + this.onCancel(); + } + } + // Pass everything else to search input + else { + this.searchInput.handleInput(keyData); + this.filterSessions(this.searchInput.getValue()); + } + } +} + +type SessionsLoader = (onProgress?: SessionListProgress) => Promise; + +/** + * Delete a session file, trying the `trash` CLI first, then falling back to unlink + */ +async function deleteSessionFile( + sessionPath: string, +): Promise<{ ok: boolean; method: "trash" | "unlink"; error?: string }> { + // Try `trash` first (if installed) + const trashArgs = sessionPath.startsWith("-") ? ["--", sessionPath] : [sessionPath]; + const trashResult = spawnSync("trash", trashArgs, { encoding: "utf-8" }); + + const getTrashErrorHint = (): string | null => { + const parts: string[] = []; + if (trashResult.error) { + parts.push(trashResult.error.message); + } + const stderr = trashResult.stderr?.trim(); + if (stderr) { + parts.push(stderr.split("\n")[0] ?? stderr); + } + if (parts.length === 0) return null; + return `trash: ${parts.join(" · ").slice(0, 200)}`; + }; + + // If trash reports success, or the file is gone afterwards, treat it as successful + if (trashResult.status === 0 || !existsSync(sessionPath)) { + return { ok: true, method: "trash" }; + } + + // Fallback to permanent deletion + try { + await unlink(sessionPath); + return { ok: true, method: "unlink" }; + } catch (err) { + const unlinkError = err instanceof Error ? err.message : String(err); + const trashErrorHint = getTrashErrorHint(); + const error = trashErrorHint ? `${unlinkError} (${trashErrorHint})` : unlinkError; + return { ok: false, method: "unlink", error }; + } +} + +/** + * Component that renders a session selector + */ +export class SessionSelectorComponent extends Container implements Focusable { + handleInput(data: string): void { + if (this.mode === "rename") { + const kb = getKeybindings(); + if (kb.matches(data, "tui.select.cancel")) { + this.exitRenameMode(); + return; + } + this.renameInput.handleInput(data); + return; + } + + this.sessionList.handleInput(data); + } + + private canRename = true; + private sessionList: SessionList; + private header: SessionSelectorHeader; + private keybindings: KeybindingsManager; + private scope: SessionScope = "current"; + private sortMode: SortMode = "threaded"; + private nameFilter: NameFilter = "all"; + private currentSessions: SessionInfo[] | null = null; + private allSessions: SessionInfo[] | null = null; + private currentSessionsLoader: SessionsLoader; + private allSessionsLoader: SessionsLoader; + private requestRender: () => void; + private renameSession?: (sessionPath: string, currentName: string | undefined) => Promise; + private currentLoading = false; + private allLoading = false; + private allLoadSeq = 0; + + private mode: "list" | "rename" = "list"; + private renameInput = new Input(); + private renameTargetPath: string | null = null; + + // Focusable implementation - propagate to sessionList for IME cursor positioning + private _focused = false; + get focused(): boolean { + return this._focused; + } + set focused(value: boolean) { + this._focused = value; + this.sessionList.focused = value; + this.renameInput.focused = value; + if (value && this.mode === "rename") { + this.renameInput.focused = true; + } + } + + private buildBaseLayout(content: Component, options?: { showHeader?: boolean }): void { + this.clear(); + this.addChild(new Spacer(1)); + this.addChild(new DynamicBorder((s) => theme.fg("accent", s))); + this.addChild(new Spacer(1)); + if (options?.showHeader ?? true) { + this.addChild(this.header); + this.addChild(new Spacer(1)); + } + this.addChild(content); + this.addChild(new Spacer(1)); + this.addChild(new DynamicBorder((s) => theme.fg("accent", s))); + } + + constructor( + currentSessionsLoader: SessionsLoader, + allSessionsLoader: SessionsLoader, + onSelect: (sessionPath: string) => void, + onCancel: () => void, + onExit: () => void, + requestRender: () => void, + options?: { + renameSession?: (sessionPath: string, currentName: string | undefined) => Promise; + showRenameHint?: boolean; + keybindings?: KeybindingsManager; + }, + currentSessionFilePath?: string, + ) { + super(); + this.keybindings = options?.keybindings ?? KeybindingsManager.create(); + this.currentSessionsLoader = currentSessionsLoader; + this.allSessionsLoader = allSessionsLoader; + this.requestRender = requestRender; + this.header = new SessionSelectorHeader(this.scope, this.sortMode, this.nameFilter, this.requestRender); + const renameSession = options?.renameSession; + this.renameSession = renameSession; + this.canRename = !!renameSession; + this.header.setShowRenameHint(options?.showRenameHint ?? this.canRename); + + // Create session list (starts empty, will be populated after load) + this.sessionList = new SessionList( + [], + false, + this.sortMode, + this.nameFilter, + this.keybindings, + currentSessionFilePath, + ); + + this.buildBaseLayout(this.sessionList); + + this.renameInput.onSubmit = (value) => { + void this.confirmRename(value); + }; + + // Ensure header status timeouts are cleared when leaving the selector + const clearStatusMessage = () => this.header.setStatusMessage(null); + this.sessionList.onSelect = (sessionPath) => { + clearStatusMessage(); + onSelect(sessionPath); + }; + this.sessionList.onCancel = () => { + clearStatusMessage(); + onCancel(); + }; + this.sessionList.onExit = () => { + clearStatusMessage(); + onExit(); + }; + this.sessionList.onToggleScope = () => this.toggleScope(); + this.sessionList.onToggleSort = () => this.toggleSortMode(); + this.sessionList.onToggleNameFilter = () => this.toggleNameFilter(); + this.sessionList.onRenameSession = (sessionPath) => { + if (!renameSession) return; + if (this.scope === "current" && this.currentLoading) return; + if (this.scope === "all" && this.allLoading) return; + + const sessions = this.scope === "all" ? (this.allSessions ?? []) : (this.currentSessions ?? []); + const session = sessions.find((s) => s.path === sessionPath); + this.enterRenameMode(sessionPath, session?.name); + }; + + // Sync list events to header + this.sessionList.onTogglePath = (showPath) => { + this.header.setShowPath(showPath); + this.requestRender(); + }; + this.sessionList.onDeleteConfirmationChange = (path) => { + this.header.setConfirmingDeletePath(path); + this.requestRender(); + }; + this.sessionList.onError = (msg) => { + this.header.setStatusMessage({ type: "error", message: msg }, 3000); + this.requestRender(); + }; + + // Handle session deletion + this.sessionList.onDeleteSession = async (sessionPath: string) => { + const result = await deleteSessionFile(sessionPath); + + if (result.ok) { + if (this.currentSessions) { + this.currentSessions = this.currentSessions.filter((s) => s.path !== sessionPath); + } + if (this.allSessions) { + this.allSessions = this.allSessions.filter((s) => s.path !== sessionPath); + } + + const sessions = this.scope === "all" ? (this.allSessions ?? []) : (this.currentSessions ?? []); + const showCwd = this.scope === "all"; + this.sessionList.setSessions(sessions, showCwd); + + const msg = result.method === "trash" ? "Session moved to trash" : "Session deleted"; + this.header.setStatusMessage({ type: "info", message: msg }, 2000); + await this.refreshSessionsAfterMutation(); + } else { + const errorMessage = result.error ?? "Unknown error"; + this.header.setStatusMessage({ type: "error", message: `Failed to delete: ${errorMessage}` }, 3000); + } + + this.requestRender(); + }; + + // Start loading current sessions immediately + this.loadCurrentSessions(); + } + + private loadCurrentSessions(): void { + void this.loadScope("current", "initial"); + } + + private enterRenameMode(sessionPath: string, currentName: string | undefined): void { + this.mode = "rename"; + this.renameTargetPath = sessionPath; + this.renameInput.setValue(currentName ?? ""); + this.renameInput.focused = true; + + const panel = new Container(); + panel.addChild(new Text(theme.bold("Rename Session"), 1, 0)); + panel.addChild(new Spacer(1)); + panel.addChild(this.renameInput); + panel.addChild(new Spacer(1)); + panel.addChild( + new Text( + theme.fg("muted", `${keyText("tui.select.confirm")} to save · ${keyText("tui.select.cancel")} to cancel`), + 1, + 0, + ), + ); + + this.buildBaseLayout(panel, { showHeader: false }); + this.requestRender(); + } + + private exitRenameMode(): void { + this.mode = "list"; + this.renameTargetPath = null; + + this.buildBaseLayout(this.sessionList); + + this.requestRender(); + } + + private async confirmRename(value: string): Promise { + const next = value.trim(); + if (!next) return; + const target = this.renameTargetPath; + if (!target) { + this.exitRenameMode(); + return; + } + + // Find current name for callback + const renameSession = this.renameSession; + if (!renameSession) { + this.exitRenameMode(); + return; + } + + try { + await renameSession(target, next); + await this.refreshSessionsAfterMutation(); + } finally { + this.exitRenameMode(); + } + } + + private async loadScope(scope: SessionScope, reason: "initial" | "refresh" | "toggle"): Promise { + const showCwd = scope === "all"; + + // Mark loading + if (scope === "current") { + this.currentLoading = true; + } else { + this.allLoading = true; + } + + const seq = scope === "all" ? ++this.allLoadSeq : undefined; + this.header.setScope(scope); + this.header.setLoading(true); + this.requestRender(); + + const onProgress = (loaded: number, total: number) => { + if (scope !== this.scope) return; + if (seq !== undefined && seq !== this.allLoadSeq) return; + this.header.setProgress(loaded, total); + this.requestRender(); + }; + + try { + const sessions = await (scope === "current" + ? this.currentSessionsLoader(onProgress) + : this.allSessionsLoader(onProgress)); + + if (scope === "current") { + this.currentSessions = sessions; + this.currentLoading = false; + } else { + this.allSessions = sessions; + this.allLoading = false; + } + + if (scope !== this.scope) return; + if (seq !== undefined && seq !== this.allLoadSeq) return; + + this.header.setLoading(false); + this.sessionList.setSessions(sessions, showCwd); + this.requestRender(); + } catch (err) { + if (scope === "current") { + this.currentLoading = false; + } else { + this.allLoading = false; + } + + if (scope !== this.scope) return; + if (seq !== undefined && seq !== this.allLoadSeq) return; + + const message = err instanceof Error ? err.message : String(err); + this.header.setLoading(false); + this.header.setStatusMessage({ type: "error", message: `Failed to load sessions: ${message}` }, 4000); + + if (reason === "initial") { + this.sessionList.setSessions([], showCwd); + } + this.requestRender(); + } + } + + private toggleSortMode(): void { + // Cycle: threaded -> recent -> relevance -> threaded + this.sortMode = this.sortMode === "threaded" ? "recent" : this.sortMode === "recent" ? "relevance" : "threaded"; + this.header.setSortMode(this.sortMode); + this.sessionList.setSortMode(this.sortMode); + this.requestRender(); + } + + private toggleNameFilter(): void { + this.nameFilter = this.nameFilter === "all" ? "named" : "all"; + this.header.setNameFilter(this.nameFilter); + this.sessionList.setNameFilter(this.nameFilter); + this.requestRender(); + } + + private async refreshSessionsAfterMutation(): Promise { + await this.loadScope(this.scope, "refresh"); + } + + private toggleScope(): void { + if (this.scope === "current") { + this.scope = "all"; + this.header.setScope(this.scope); + + if (this.allSessions !== null) { + this.header.setLoading(false); + this.sessionList.setSessions(this.allSessions, true); + this.requestRender(); + return; + } + + if (!this.allLoading) { + void this.loadScope("all", "toggle"); + } + return; + } + + this.scope = "current"; + this.header.setScope(this.scope); + this.header.setLoading(this.currentLoading); + this.sessionList.setSessions(this.currentSessions ?? [], false); + this.requestRender(); + } + + getSessionList(): SessionList { + return this.sessionList; + } +} diff --git a/packages/coding-agent/src/modes/interactive/components/settings-selector.ts b/packages/coding-agent/src/modes/interactive/components/settings-selector.ts new file mode 100644 index 0000000000000000000000000000000000000000..3e8363d913c724696ceed850293ce8b110bf7d11 --- /dev/null +++ b/packages/coding-agent/src/modes/interactive/components/settings-selector.ts @@ -0,0 +1,945 @@ +import type { ThinkingLevel } from "@earendil-works/pi-agent-core"; +import { getSupportedThinkingLevels, type Model, type Transport } from "@earendil-works/pi-ai"; +import { + type Component, + Container, + getCapabilities, + type ScrollViewScrollbar, + type SelectItem, + type SettingItem, + SettingsList, + Spacer, + Text, +} from "@earendil-works/pi-tui"; +import { formatHttpIdleTimeoutMs, HTTP_IDLE_TIMEOUT_CHOICES } from "../../../core/http-dispatcher.ts"; +import type { + DefaultProjectTrust, + FullscreenExitOutput, + MermaidRenderingMode, + TuiMode, + WarningSettings, +} from "../../../core/settings-manager.ts"; +import { getSettingsListTheme, parseAutoThemeSetting, type TerminalTheme, theme } from "../theme/theme.ts"; +import { DynamicBorder } from "./dynamic-border.ts"; +import { keyDisplayText } from "./keybinding-hints.ts"; +import { SelectSubmenu, SteppedSubmenu, type SteppedSubmenuStep } from "./settings-submenu.ts"; + +const MODEL_PICKER_LAYOUT = { minPrimaryColumnWidth: 12, maxPrimaryColumnWidth: 46 }; + +const THINKING_DESCRIPTIONS: Record = { + off: "No reasoning", + minimal: "Very brief reasoning (~1k tokens)", + low: "Light reasoning (~2k tokens)", + medium: "Moderate reasoning (~8k tokens)", + high: "Deep reasoning (~16k tokens)", + xhigh: "Extra-high reasoning (~32k tokens)", + max: "Maximum reasoning", +}; + +const DEFAULT_PROJECT_TRUST_LABELS: Record = { + ask: "Ask", + always: "Always trust", + never: "Never trust", +}; + +const DEFAULT_PROJECT_TRUST_BY_LABEL = new Map( + Object.entries(DEFAULT_PROJECT_TRUST_LABELS).map(([value, label]) => [label, value as DefaultProjectTrust]), +); + +export interface SettingsConfig { + autoCompact: boolean; + defaultModel: string; + currentModel?: Model; + availableDefaultModels: readonly Model[]; + showImages: boolean; + imageWidthCells: number; + autoResizeImages: boolean; + blockImages: boolean; + enableSkillCommands: boolean; + steeringMode: "all" | "one-at-a-time"; + followUpMode: "all" | "one-at-a-time"; + transport: Transport; + httpIdleTimeoutMs: number; + thinkingLevel: ThinkingLevel; + availableThinkingLevels: ThinkingLevel[]; + modelThinkingLevels: Record; + currentTheme: string; + terminalTheme: TerminalTheme; + availableThemes: string[]; + hideThinkingBlock: boolean; + mermaidRenderingMode: MermaidRenderingMode; + showCacheMissNotices: boolean; + collapseChangelog: boolean; + enableInstallTelemetry: boolean; + doubleEscapeAction: "fork" | "tree" | "none"; + treeFilterMode: "default" | "no-tools" | "user-only" | "labeled-only" | "all"; + showHardwareCursor: boolean; + editorPaddingX: number; + outputPad: 0 | 1; + autocompleteMaxVisible: number; + quietStartup: boolean; + defaultProjectTrust: DefaultProjectTrust; + clearOnShrink: boolean; + showTerminalProgress: boolean; + tuiMode: TuiMode; + fullscreenExitOutput: FullscreenExitOutput; + fullscreenScrollbar: ScrollViewScrollbar; + fullscreenCopyOnSelect: boolean; + warnings: WarningSettings; +} + +export interface SettingsCallbacks { + onAutoCompactChange: (enabled: boolean) => void; + onShowImagesChange: (enabled: boolean) => void; + onImageWidthCellsChange: (width: number) => void; + onAutoResizeImagesChange: (enabled: boolean) => void; + onBlockImagesChange: (blocked: boolean) => void; + onEnableSkillCommandsChange: (enabled: boolean) => void; + onSteeringModeChange: (mode: "all" | "one-at-a-time") => void; + onFollowUpModeChange: (mode: "all" | "one-at-a-time") => void; + onTransportChange: (transport: Transport) => void; + onHttpIdleTimeoutMsChange: (timeoutMs: number) => void; + onModelThinkingLevelChange: (provider: string, modelId: string, level: ThinkingLevel) => void; + onModelThinkingLevelRemove: (provider: string, modelId: string) => void; + onThemeChange: (theme: string) => void; + onThemePreview?: (theme: string) => void; + onHideThinkingBlockChange: (hidden: boolean) => void; + onMermaidRenderingModeChange: (mode: MermaidRenderingMode) => void; + onShowCacheMissNoticesChange: (shown: boolean) => void; + onCollapseChangelogChange: (collapsed: boolean) => void; + onEnableInstallTelemetryChange: (enabled: boolean) => void; + onDoubleEscapeActionChange: (action: "fork" | "tree" | "none") => void; + onTreeFilterModeChange: (mode: "default" | "no-tools" | "user-only" | "labeled-only" | "all") => void; + onShowHardwareCursorChange: (enabled: boolean) => void; + onEditorPaddingXChange: (padding: number) => void; + onOutputPadChange: (padding: 0 | 1) => void; + onAutocompleteMaxVisibleChange: (maxVisible: number) => void; + onQuietStartupChange: (enabled: boolean) => void; + onDefaultProjectTrustChange: (defaultProjectTrust: DefaultProjectTrust) => void; + onClearOnShrinkChange: (enabled: boolean) => void; + onShowTerminalProgressChange: (enabled: boolean) => void; + onTuiModeChange: (mode: TuiMode) => void; + onFullscreenExitOutputChange: (output: FullscreenExitOutput) => void; + onFullscreenScrollbarChange: (mode: ScrollViewScrollbar) => void; + onFullscreenCopyOnSelectChange: (enabled: boolean) => void; + onWarningsChange: (warnings: WarningSettings) => void; + onCancel: () => void; +} + +/** + * A submenu component for selecting from a list of options. + */ +class WarningSettingsSubmenu extends Container { + private settingsList: SettingsList; + private state: WarningSettings; + + constructor(warnings: WarningSettings, onChange: (warnings: WarningSettings) => void, onCancel: () => void) { + super(); + + this.state = { ...warnings }; + + const items: SettingItem[] = [ + { + id: "anthropic-extra-usage", + label: "Anthropic extra usage", + description: "Warn when Anthropic subscription auth may use paid extra usage", + currentValue: (this.state.anthropicExtraUsage ?? true) ? "true" : "false", + values: ["true", "false"], + }, + ]; + + this.settingsList = new SettingsList( + items, + Math.min(items.length, 10), + getSettingsListTheme(), + (id, newValue) => { + switch (id) { + case "anthropic-extra-usage": + this.state = { ...this.state, anthropicExtraUsage: newValue === "true" }; + onChange({ ...this.state }); + break; + } + }, + onCancel, + ); + + this.addChild(this.settingsList); + } + + handleInput(data: string): void { + this.settingsList.handleInput(data); + } +} + +const CLEAR_OVERRIDE_VALUE = "__clear__"; + +function modelSettingKey(model: Model): string { + return `${model.provider}/${model.id}`; +} + +function modelDisplayLabel(model: Model): string { + return `${model.id} [${model.provider}]`; +} + +function modelThinkingOverridesSummary(overrides: Record): string { + const count = Object.keys(overrides).length; + if (count === 0) return "none"; + return `${count} configured`; +} + +function modelItemLabel(model: Model): string { + return `${model.id} ${theme.fg("muted", `[${model.provider}]`)}`; +} + +function themeItems(availableThemes: string[], currentTheme: string): SelectItem[] { + return availableThemes.map((name) => ({ + value: name, + label: `${name === currentTheme ? "✓ " : " "}${name}`, + })); +} + +const AUTOMATIC_THEME_VALUE = "/"; + +function singleModeThemeItems(availableThemes: string[], currentTheme: string): SelectItem[] { + return [ + { + value: AUTOMATIC_THEME_VALUE, + label: " Automatic", + description: "Use separate themes for light and dark terminal appearance", + }, + ...themeItems(availableThemes, currentTheme), + ]; +} + +function preferredTheme(availableThemes: string[], preferred: string | undefined, fallback: string): string { + if (preferred && availableThemes.includes(preferred)) return preferred; + if (availableThemes.includes(fallback)) return fallback; + return availableThemes[0] ?? fallback; +} + +function defaultAutomaticThemes( + currentThemeSetting: string, + availableThemes: string[], +): { lightTheme: string; darkTheme: string } { + const autoTheme = parseAutoThemeSetting(currentThemeSetting); + if (autoTheme) return autoTheme; + + const currentFixedTheme = currentThemeSetting.includes("/") ? undefined : currentThemeSetting; + const themeName = preferredTheme(availableThemes, currentFixedTheme, "dark"); + return { lightTheme: themeName, darkTheme: themeName }; +} + +class ThemeSubmenu extends Container { + private inputComponent: Component | undefined; + private readonly callbacks: SettingsCallbacks; + private readonly availableThemes: string[]; + private readonly terminalTheme: TerminalTheme; + private readonly onDone: (selectedValue?: string) => void; + private readonly originalThemeSetting: string; + private mode: "single" | "automatic"; + private singleTheme: string; + private lightTheme: string; + private darkTheme: string; + + constructor( + currentThemeSetting: string, + terminalTheme: TerminalTheme, + availableThemes: string[], + callbacks: SettingsCallbacks, + onDone: (selectedValue?: string) => void, + ) { + super(); + this.callbacks = callbacks; + this.availableThemes = availableThemes; + this.terminalTheme = terminalTheme; + this.onDone = onDone; + this.originalThemeSetting = currentThemeSetting; + const autoTheme = parseAutoThemeSetting(currentThemeSetting); + const automaticThemes = defaultAutomaticThemes(currentThemeSetting, availableThemes); + const fixedTheme = autoTheme || currentThemeSetting.includes("/") ? undefined : currentThemeSetting; + this.mode = autoTheme ? "automatic" : "single"; + this.lightTheme = automaticThemes.lightTheme; + this.darkTheme = automaticThemes.darkTheme; + this.singleTheme = preferredTheme( + availableThemes, + fixedTheme ?? (autoTheme ? this.getActiveAutomaticTheme() : undefined), + "dark", + ); + + if (this.mode === "automatic") { + this.showAutomaticMenu(); + } else { + this.showSingleMenu(); + } + } + + handleInput(data: string): void { + this.inputComponent?.handleInput?.(data); + } + + private setContent(renderComponent: Component, inputComponent: Component = renderComponent): void { + this.clear(); + this.addChild(renderComponent); + this.inputComponent = inputComponent; + } + + private showSingleMenu(): void { + this.mode = "single"; + const menu = new SelectSubmenu( + "Theme", + "Select a theme, or choose Automatic to follow terminal appearance.", + singleModeThemeItems(this.availableThemes, this.singleTheme), + this.singleTheme, + (value) => { + if (value === AUTOMATIC_THEME_VALUE) { + this.mode = "automatic"; + this.callbacks.onThemePreview?.(this.getThemeSetting()); + this.showAutomaticMenu(); + return; + } + + this.singleTheme = value; + this.apply(value); + }, + () => this.cancel(), + (value) => { + this.callbacks.onThemePreview?.(value === AUTOMATIC_THEME_VALUE ? this.getAutomaticThemeSetting() : value); + }, + ); + this.setContent(menu); + } + + private showAutomaticMenu(): void { + this.mode = "automatic"; + const content = new Container(); + content.addChild(new Text(theme.bold(theme.fg("accent", "Automatic Theme")), 0, 0)); + content.addChild(new Spacer(1)); + content.addChild(new Text(theme.fg("muted", "Choose themes for terminal light and dark appearance."), 0, 0)); + content.addChild(new Text(theme.fg("muted", "Light/dark detection requires terminal support."), 0, 0)); + content.addChild(new Spacer(1)); + + const items: SettingItem[] = [ + { + id: "light-theme", + label: "Light theme", + description: "Theme to use in automatic mode when the terminal is light", + currentValue: this.lightTheme, + submenu: (currentValue, done) => + this.createThemeSelect( + "Light Theme", + "Select the theme to use for light terminal appearance", + currentValue, + done, + (value) => { + this.lightTheme = value; + this.callbacks.onThemePreview?.(this.getThemeSetting()); + done(value); + }, + ), + }, + { + id: "dark-theme", + label: "Dark theme", + description: "Theme to use in automatic mode when the terminal is dark", + currentValue: this.darkTheme, + submenu: (currentValue, done) => + this.createThemeSelect( + "Dark Theme", + "Select the theme to use for dark terminal appearance", + currentValue, + done, + (value) => { + this.darkTheme = value; + this.callbacks.onThemePreview?.(this.getThemeSetting()); + done(value); + }, + ), + }, + { + id: "apply", + label: "Apply", + description: "Save and go back", + currentValue: "save and go back", + values: ["save and go back"], + }, + { + id: "single-mode", + label: "Change mode", + description: "Switch to one theme for light and dark", + currentValue: "switch to single theme", + values: ["switch to single theme"], + }, + ]; + + const settingsList = new SettingsList( + items, + Math.min(items.length, 10), + getSettingsListTheme(), + (id) => { + switch (id) { + case "single-mode": + this.mode = "single"; + this.singleTheme = this.getActiveAutomaticTheme(); + this.callbacks.onThemePreview?.(this.singleTheme); + this.showSingleMenu(); + break; + case "apply": + this.apply(this.getAutomaticThemeSetting()); + break; + } + }, + () => this.cancel(), + ); + content.addChild(settingsList); + this.setContent(content, settingsList); + } + + private createThemeSelect( + title: string, + description: string, + currentValue: string, + done: (selectedValue?: string) => void, + onSelect: (value: string) => void, + ): SelectSubmenu { + return new SelectSubmenu( + title, + description, + themeItems(this.availableThemes, currentValue), + currentValue, + onSelect, + () => { + this.callbacks.onThemePreview?.(this.getThemeSetting()); + done(); + }, + (value) => this.callbacks.onThemePreview?.(value), + ); + } + + private getThemeSetting(): string { + return this.mode === "automatic" ? this.getAutomaticThemeSetting() : this.singleTheme; + } + + private getActiveAutomaticTheme(): string { + return this.terminalTheme === "light" ? this.lightTheme : this.darkTheme; + } + + private getAutomaticThemeSetting(): string { + return `${this.lightTheme}/${this.darkTheme}`; + } + + private apply(themeSetting: string): void { + this.onDone(themeSetting); + } + + private cancel(): void { + this.callbacks.onThemePreview?.(this.originalThemeSetting); + this.onDone(); + } +} + +/** + * Main settings selector component. + */ +export class SettingsSelectorComponent extends Container { + private settingsList: SettingsList; + + constructor(config: SettingsConfig, callbacks: SettingsCallbacks) { + super(); + + const supportsImages = getCapabilities().images; + const followUpKey = keyDisplayText("app.message.followUp"); + const cycleThinkingKey = keyDisplayText("app.thinking.cycle"); + let currentWarnings = { ...config.warnings }; + const currentModelThinkingLevels = { ...config.modelThinkingLevels }; + const defaultModelByValue = new Map( + config.availableDefaultModels.map((model) => [modelSettingKey(model), model]), + ); + const currentDefaultModelKey = defaultModelByValue.has(config.defaultModel) ? config.defaultModel : undefined; + const currentModelKey = config.currentModel ? modelSettingKey(config.currentModel) : undefined; + + const items: SettingItem[] = [ + { + id: "autocompact", + label: "Auto-compact", + description: "Automatically compact context when it gets too large", + currentValue: config.autoCompact ? "true" : "false", + values: ["true", "false"], + }, + { + id: "steering-mode", + label: "Steering mode", + description: + "Enter while streaming queues steering messages. 'one-at-a-time': deliver one, wait for response. 'all': deliver all at once.", + currentValue: config.steeringMode, + values: ["one-at-a-time", "all"], + }, + { + id: "follow-up-mode", + label: "Follow-up mode", + description: `${followUpKey} queues follow-up messages until agent stops. 'one-at-a-time': deliver one, wait for response. 'all': deliver all at once.`, + currentValue: config.followUpMode, + values: ["one-at-a-time", "all"], + }, + { + id: "transport", + label: "Transport", + description: "Preferred transport for providers that support multiple transports", + currentValue: config.transport, + values: ["sse", "websocket", "websocket-cached", "auto"], + }, + { + id: "http-idle-timeout", + label: "HTTP idle timeout", + description: + "Maximum idle gap while waiting for HTTP headers or body chunks. Disable for local models that pause longer than five minutes.", + currentValue: formatHttpIdleTimeoutMs(config.httpIdleTimeoutMs), + values: HTTP_IDLE_TIMEOUT_CHOICES.map((choice) => choice.label), + }, + { + id: "hide-thinking", + label: "Hide thinking", + description: "Hide thinking blocks in assistant responses", + currentValue: config.hideThinkingBlock ? "true" : "false", + values: ["true", "false"], + }, + { + id: "mermaid-rendering", + label: "Mermaid diagrams", + description: "Render Mermaid code blocks as Unicode diagrams", + currentValue: config.mermaidRenderingMode, + values: ["off", "final", "streaming"], + }, + { + id: "cache-miss-notices", + label: "Cache miss notices", + description: "Show transcript notices for cache costs and provider recovery diagnostics", + currentValue: config.showCacheMissNotices ? "true" : "false", + values: ["true", "false"], + }, + { + id: "collapse-changelog", + label: "Collapse changelog", + description: "Show condensed changelog after updates", + currentValue: config.collapseChangelog ? "true" : "false", + values: ["true", "false"], + }, + { + id: "quiet-startup", + label: "Quiet startup", + description: "Disable verbose printing at startup", + currentValue: config.quietStartup ? "true" : "false", + values: ["true", "false"], + }, + { + id: "install-telemetry", + label: "Install telemetry", + description: "Send an anonymous version/update ping after changelog-detected updates", + currentValue: config.enableInstallTelemetry ? "true" : "false", + values: ["true", "false"], + }, + { + id: "default-project-trust", + label: "Default project trust", + description: "Fallback behavior when no extension or saved trust decision decides project trust", + currentValue: DEFAULT_PROJECT_TRUST_LABELS[config.defaultProjectTrust], + values: Object.values(DEFAULT_PROJECT_TRUST_LABELS), + }, + { + id: "double-escape-action", + label: "Double-escape action", + description: "Action when pressing Escape twice with empty editor", + currentValue: config.doubleEscapeAction, + values: ["tree", "fork", "none"], + }, + { + id: "tree-filter-mode", + label: "Tree filter mode", + description: "Default filter when opening /tree", + currentValue: config.treeFilterMode, + values: ["default", "no-tools", "user-only", "labeled-only", "all"], + }, + { + id: "warnings", + label: "Warnings", + description: "Enable or disable individual warnings", + currentValue: "configure", + submenu: (_currentValue, done) => + new WarningSettingsSubmenu( + currentWarnings, + (warnings) => { + currentWarnings = warnings; + callbacks.onWarningsChange(warnings); + }, + () => done(), + ), + }, + { + id: "model-thinking", + label: "Default thinking level per model", + description: `Override the default thinking level for specific models. ${cycleThinkingKey} cycles in-session.`, + currentValue: modelThinkingOverridesSummary(currentModelThinkingLevels), + submenu: (_currentValue, done) => { + const steps: SteppedSubmenuStep[] = [ + { + key: "model", + title: "Per-Model Thinking Level", + description: "Select a model to configure", + options: () => { + const sorted = [...config.availableDefaultModels].sort((a, b) => { + const aKey = modelSettingKey(a); + const bKey = modelSettingKey(b); + if (aKey === currentModelKey) return -1; + if (bKey === currentModelKey) return 1; + if (aKey === currentDefaultModelKey) return -1; + if (bKey === currentDefaultModelKey) return 1; + return a.provider.localeCompare(b.provider); + }); + const items: SelectItem[] = sorted.map((model) => { + const key = modelSettingKey(model); + const override = currentModelThinkingLevels[key]; + return { + value: key, + label: modelItemLabel(model), + description: override ?? undefined, + }; + }); + if (items.length === 0) { + items.push({ + value: "__none__", + label: "No models available", + description: "Log in to a provider or configure an API key first", + }); + } + return items; + }, + preselect: () => currentModelKey ?? currentDefaultModelKey, + searchable: true, + layout: MODEL_PICKER_LAYOUT, + }, + { + key: "level", + title: (ctx) => { + const m = defaultModelByValue.get(ctx.model); + return `Thinking Level for ${m ? modelDisplayLabel(m) : ctx.model}`; + }, + description: "Select default thinking level for this model", + options: (ctx) => { + const model = defaultModelByValue.get(ctx.model); + if (!model) return []; + const levels = ( + model.reasoning ? getSupportedThinkingLevels(model) : ["off"] + ) as ThinkingLevel[]; + const activeLevel = currentModelThinkingLevels[ctx.model]; + const items: SelectItem[] = levels.map((level) => ({ + value: level, + label: `${level === activeLevel ? "✓ " : " "}${level}`, + description: THINKING_DESCRIPTIONS[level], + })); + if (currentModelThinkingLevels[ctx.model] !== undefined) { + items.push({ + value: CLEAR_OVERRIDE_VALUE, + label: " (clear override)", + description: `Revert to global default (${config.thinkingLevel})`, + }); + } + return items; + }, + preselect: (ctx) => currentModelThinkingLevels[ctx.model], + }, + ]; + + const summary = () => modelThinkingOverridesSummary(currentModelThinkingLevels); + + return new SteppedSubmenu( + steps, + (selections) => { + const model = defaultModelByValue.get(selections.model); + if (!model) return; + if (selections.level === CLEAR_OVERRIDE_VALUE) { + callbacks.onModelThinkingLevelRemove(model.provider, model.id); + delete currentModelThinkingLevels[selections.model]; + } else { + callbacks.onModelThinkingLevelChange( + model.provider, + model.id, + selections.level as ThinkingLevel, + ); + currentModelThinkingLevels[selections.model] = selections.level as ThinkingLevel; + } + }, + () => { + done(summary()); + }, + { loop: true }, + ); + }, + }, + { + id: "tui-mode", + label: "TUI mode", + description: "Interface layout; fullscreen mode is experimental", + currentValue: config.tuiMode, + values: ["regular", "fullscreen"], + }, + { + id: "fullscreen-exit-output", + label: "Fullscreen exit output", + description: "Print the transcript or only a session resume hint when exiting fullscreen mode", + currentValue: config.fullscreenExitOutput, + values: ["transcript", "resume-hint"], + }, + { + id: "fullscreen-scrollbar", + label: "Fullscreen scrollbar", + description: "Scrollbar behavior in fullscreen mode; has no effect in regular mode", + currentValue: config.fullscreenScrollbar, + values: ["auto", "always", "hidden"], + }, + { + id: "fullscreen-copy-on-select", + label: "Fullscreen copy on select", + description: "Automatically copy selected text in fullscreen mode; disable to copy selections with Ctrl+X", + currentValue: config.fullscreenCopyOnSelect ? "true" : "false", + values: ["true", "false"], + }, + { + id: "theme", + label: "Theme", + description: "Color theme for the interface", + currentValue: config.currentTheme, + submenu: (currentValue, done) => + new ThemeSubmenu(currentValue, config.terminalTheme, config.availableThemes, callbacks, done), + }, + ]; + + // Only show image toggle if terminal supports it + if (supportsImages) { + // Insert after autocompact + items.splice(1, 0, { + id: "show-images", + label: "Show images", + description: "Render images inline in terminal", + currentValue: config.showImages ? "true" : "false", + values: ["true", "false"], + }); + items.splice(2, 0, { + id: "image-width-cells", + label: "Image width", + description: "Preferred inline image width in terminal cells", + currentValue: String(config.imageWidthCells), + values: ["60", "80", "120"], + }); + } + + // Image auto-resize toggle (always available, affects both attached and read images) + items.splice(supportsImages ? 3 : 1, 0, { + id: "auto-resize-images", + label: "Auto-resize images", + description: "Resize large images to 2000x2000 max for better model compatibility", + currentValue: config.autoResizeImages ? "true" : "false", + values: ["true", "false"], + }); + + // Block images toggle (always available, insert after auto-resize-images) + const autoResizeIndex = items.findIndex((item) => item.id === "auto-resize-images"); + items.splice(autoResizeIndex + 1, 0, { + id: "block-images", + label: "Block images", + description: "Prevent images from being sent to LLM providers", + currentValue: config.blockImages ? "true" : "false", + values: ["true", "false"], + }); + + // Skill commands toggle (insert after block-images) + const blockImagesIndex = items.findIndex((item) => item.id === "block-images"); + items.splice(blockImagesIndex + 1, 0, { + id: "skill-commands", + label: "Skill commands", + description: "Register skills as /skill:name commands", + currentValue: config.enableSkillCommands ? "true" : "false", + values: ["true", "false"], + }); + + // Hardware cursor toggle (insert after skill-commands) + const skillCommandsIndex = items.findIndex((item) => item.id === "skill-commands"); + items.splice(skillCommandsIndex + 1, 0, { + id: "show-hardware-cursor", + label: "Show hardware cursor", + description: "Show the terminal cursor while still positioning it for IME support", + currentValue: config.showHardwareCursor ? "true" : "false", + values: ["true", "false"], + }); + + // Editor padding toggle (insert after show-hardware-cursor) + const hardwareCursorIndex = items.findIndex((item) => item.id === "show-hardware-cursor"); + items.splice(hardwareCursorIndex + 1, 0, { + id: "editor-padding", + label: "Editor padding", + description: "Horizontal padding for input editor (0-3)", + currentValue: String(config.editorPaddingX), + values: ["0", "1", "2", "3"], + }); + + // Output padding toggle (insert after editor-padding) + const editorPaddingIndex = items.findIndex((item) => item.id === "editor-padding"); + items.splice(editorPaddingIndex + 1, 0, { + id: "output-padding", + label: "Output padding", + description: "Horizontal padding for user messages, assistant messages, and thinking", + currentValue: String(config.outputPad), + values: ["0", "1"], + }); + + // Autocomplete max visible toggle (insert after output-padding) + const outputPaddingIndex = items.findIndex((item) => item.id === "output-padding"); + items.splice(outputPaddingIndex + 1, 0, { + id: "autocomplete-max-visible", + label: "Autocomplete max items", + description: "Max visible items in autocomplete dropdown (3-20)", + currentValue: String(config.autocompleteMaxVisible), + values: ["3", "5", "7", "10", "15", "20"], + }); + + // Clear on shrink toggle (insert after autocomplete-max-visible) + const autocompleteIndex = items.findIndex((item) => item.id === "autocomplete-max-visible"); + items.splice(autocompleteIndex + 1, 0, { + id: "clear-on-shrink", + label: "Clear on shrink", + description: "Clear empty rows when content shrinks (may cause flicker)", + currentValue: config.clearOnShrink ? "true" : "false", + values: ["true", "false"], + }); + + // Terminal progress toggle (insert after clear-on-shrink) + const clearOnShrinkIndex = items.findIndex((item) => item.id === "clear-on-shrink"); + items.splice(clearOnShrinkIndex + 1, 0, { + id: "terminal-progress", + label: "Terminal progress", + description: "Show OSC 9;4 progress indicators in the terminal tab bar", + currentValue: config.showTerminalProgress ? "true" : "false", + values: ["true", "false"], + }); + + // Add borders + this.addChild(new DynamicBorder()); + + this.settingsList = new SettingsList( + items, + 10, + getSettingsListTheme(), + (id, newValue) => { + switch (id) { + case "autocompact": + callbacks.onAutoCompactChange(newValue === "true"); + break; + case "show-images": + callbacks.onShowImagesChange(newValue === "true"); + break; + case "image-width-cells": + callbacks.onImageWidthCellsChange(parseInt(newValue, 10)); + break; + case "auto-resize-images": + callbacks.onAutoResizeImagesChange(newValue === "true"); + break; + case "block-images": + callbacks.onBlockImagesChange(newValue === "true"); + break; + case "skill-commands": + callbacks.onEnableSkillCommandsChange(newValue === "true"); + break; + case "steering-mode": + callbacks.onSteeringModeChange(newValue as "all" | "one-at-a-time"); + break; + case "follow-up-mode": + callbacks.onFollowUpModeChange(newValue as "all" | "one-at-a-time"); + break; + case "transport": + callbacks.onTransportChange(newValue as Transport); + break; + case "http-idle-timeout": { + const choice = HTTP_IDLE_TIMEOUT_CHOICES.find((item) => item.label === newValue); + if (choice) { + callbacks.onHttpIdleTimeoutMsChange(choice.timeoutMs); + } + break; + } + case "hide-thinking": + callbacks.onHideThinkingBlockChange(newValue === "true"); + break; + case "mermaid-rendering": + callbacks.onMermaidRenderingModeChange(newValue as MermaidRenderingMode); + break; + case "cache-miss-notices": + callbacks.onShowCacheMissNoticesChange(newValue === "true"); + break; + case "collapse-changelog": + callbacks.onCollapseChangelogChange(newValue === "true"); + break; + case "quiet-startup": + callbacks.onQuietStartupChange(newValue === "true"); + break; + case "install-telemetry": + callbacks.onEnableInstallTelemetryChange(newValue === "true"); + break; + case "default-project-trust": { + const defaultProjectTrust = DEFAULT_PROJECT_TRUST_BY_LABEL.get(newValue); + if (defaultProjectTrust) { + callbacks.onDefaultProjectTrustChange(defaultProjectTrust); + } + break; + } + case "double-escape-action": + callbacks.onDoubleEscapeActionChange(newValue as "fork" | "tree"); + break; + case "tree-filter-mode": + callbacks.onTreeFilterModeChange( + newValue as "default" | "no-tools" | "user-only" | "labeled-only" | "all", + ); + break; + case "show-hardware-cursor": + callbacks.onShowHardwareCursorChange(newValue === "true"); + break; + case "editor-padding": + callbacks.onEditorPaddingXChange(parseInt(newValue, 10)); + break; + case "output-padding": + callbacks.onOutputPadChange(newValue === "0" ? 0 : 1); + break; + case "autocomplete-max-visible": + callbacks.onAutocompleteMaxVisibleChange(parseInt(newValue, 10)); + break; + case "clear-on-shrink": + callbacks.onClearOnShrinkChange(newValue === "true"); + break; + case "terminal-progress": + callbacks.onShowTerminalProgressChange(newValue === "true"); + break; + case "tui-mode": + callbacks.onTuiModeChange(newValue as TuiMode); + break; + case "fullscreen-exit-output": + callbacks.onFullscreenExitOutputChange(newValue as FullscreenExitOutput); + break; + case "fullscreen-scrollbar": + callbacks.onFullscreenScrollbarChange(newValue as ScrollViewScrollbar); + break; + case "fullscreen-copy-on-select": + callbacks.onFullscreenCopyOnSelectChange(newValue === "true"); + break; + case "theme": + callbacks.onThemeChange(newValue); + break; + } + }, + callbacks.onCancel, + { enableSearch: true }, + ); + + this.addChild(this.settingsList); + this.addChild(new DynamicBorder()); + } + + getSettingsList(): SettingsList { + return this.settingsList; + } +} diff --git a/packages/coding-agent/src/modes/interactive/components/skill-invocation-message.ts b/packages/coding-agent/src/modes/interactive/components/skill-invocation-message.ts new file mode 100644 index 0000000000000000000000000000000000000000..e09febc7cbc2d79a469798bffddd559e1f886ed1 --- /dev/null +++ b/packages/coding-agent/src/modes/interactive/components/skill-invocation-message.ts @@ -0,0 +1,55 @@ +import { Box, Markdown, type MarkdownTheme, Text } from "@earendil-works/pi-tui"; +import type { ParsedSkillBlock } from "../../../core/agent-session.ts"; +import { getMarkdownTheme, theme } from "../theme/theme.ts"; +import { keyText } from "./keybinding-hints.ts"; + +/** + * Component that renders a skill invocation message with collapsed/expanded state. + * Uses same background color as custom messages for visual consistency. + * Only renders the skill block itself - user message is rendered separately. + */ +export class SkillInvocationMessageComponent extends Box { + private expanded = false; + private skillBlock: ParsedSkillBlock; + private markdownTheme: MarkdownTheme; + + constructor(skillBlock: ParsedSkillBlock, markdownTheme: MarkdownTheme = getMarkdownTheme()) { + super(1, 1, (t) => theme.bg("customMessageBg", t)); + this.skillBlock = skillBlock; + this.markdownTheme = markdownTheme; + this.updateDisplay(); + } + + setExpanded(expanded: boolean): void { + this.expanded = expanded; + this.updateDisplay(); + } + + override invalidate(): void { + super.invalidate(); + this.updateDisplay(); + } + + private updateDisplay(): void { + this.clear(); + + if (this.expanded) { + // Expanded: label + skill name header + full content + const label = theme.fg("customMessageLabel", `\x1b[1m[skill]\x1b[22m`); + this.addChild(new Text(label, 0, 0)); + const header = `**${this.skillBlock.name}**\n\n`; + this.addChild( + new Markdown(header + this.skillBlock.content, 0, 0, this.markdownTheme, { + color: (text: string) => theme.fg("customMessageText", text), + }), + ); + } else { + // Collapsed: single line - [skill] name (hint to expand) + const line = + theme.fg("customMessageLabel", `\x1b[1m[skill]\x1b[22m `) + + theme.fg("customMessageText", this.skillBlock.name) + + theme.fg("dim", ` (${keyText("app.tools.expand")} to expand)`); + this.addChild(new Text(line, 0, 0)); + } + } +} diff --git a/packages/coding-agent/src/modes/interactive/components/status-indicator.ts b/packages/coding-agent/src/modes/interactive/components/status-indicator.ts new file mode 100644 index 0000000000000000000000000000000000000000..be273210322d68fde353285941d0d79f99f8cbcf --- /dev/null +++ b/packages/coding-agent/src/modes/interactive/components/status-indicator.ts @@ -0,0 +1,123 @@ +import { type Component, Loader, type TUI, truncateToWidth } from "@earendil-works/pi-tui"; +import type { WorkingIndicatorOptions } from "../../../core/extensions/index.ts"; +import { theme } from "../theme/theme.ts"; +import { CountdownTimer } from "./countdown-timer.ts"; +import { keyText } from "./keybinding-hints.ts"; + +export type StatusIndicatorKind = "working" | "retry" | "compaction" | "branchSummary"; + +export class StatusIndicator extends Loader { + readonly kind: StatusIndicatorKind; + + constructor( + kind: StatusIndicatorKind, + ui: TUI, + spinnerColorFn: (str: string) => string, + messageColorFn: (str: string) => string, + message: string, + indicator?: WorkingIndicatorOptions, + ) { + super(ui, spinnerColorFn, messageColorFn, message, indicator); + this.kind = kind; + } + + renderInBorder(width: number): string { + const line = super.render(width + 2)[1] ?? ""; + return truncateToWidth(line.startsWith(" ") ? line.slice(1).trimEnd() : line.trimEnd(), width, ""); + } + + renderSpinnerInBorder(width: number): string { + return truncateToWidth(this.getRenderedIndicator(), width, ""); + } + + dispose(): void { + this.stop(); + } +} + +export class WorkingStatusIndicator extends StatusIndicator { + constructor(ui: TUI, message: string, indicator?: WorkingIndicatorOptions, colorFn?: (text: string) => string) { + super( + "working", + ui, + colorFn ?? ((text) => theme.fg("accent", text)), + colorFn ?? ((text) => theme.fg("muted", text)), + message, + indicator, + ); + } +} + +export class RetryStatusIndicator extends StatusIndicator { + private countdown: CountdownTimer | undefined; + + constructor(ui: TUI, attempt: number, maxAttempts: number, delayMs: number) { + const retryMessage = (seconds: number) => + `Retrying (${attempt}/${maxAttempts}) in ${seconds}s... (${keyText("app.interrupt")} to cancel)`; + super( + "retry", + ui, + (spinner) => theme.fg("warning", spinner), + (text) => theme.fg("muted", text), + retryMessage(Math.ceil(delayMs / 1000)), + ); + this.countdown = new CountdownTimer( + delayMs, + ui, + (seconds) => { + this.setMessage(retryMessage(seconds)); + }, + () => { + this.countdown = undefined; + }, + ); + } + + override dispose(): void { + this.countdown?.dispose(); + this.countdown = undefined; + super.dispose(); + } +} + +export type CompactionStatusReason = "manual" | "threshold" | "overflow"; + +export class CompactionStatusIndicator extends StatusIndicator { + constructor(ui: TUI, reason: CompactionStatusReason) { + const cancelHint = `(${keyText("app.interrupt")} to cancel)`; + const label = + reason === "manual" + ? `Compacting context... ${cancelHint}` + : `${reason === "overflow" ? "Context overflow detected, " : ""}Auto-compacting... ${cancelHint}`; + super( + "compaction", + ui, + (spinner) => theme.fg("accent", spinner), + (text) => theme.fg("muted", text), + label, + ); + } +} + +export class BranchSummaryStatusIndicator extends StatusIndicator { + constructor(ui: TUI) { + super( + "branchSummary", + ui, + (spinner) => theme.fg("accent", spinner), + (text) => theme.fg("muted", text), + `Summarizing branch... (${keyText("app.interrupt")} to cancel)`, + ); + } +} + +export class IdleStatus implements Component { + invalidate(): void { + // No cached state to invalidate. + } + + render(width: number): string[] { + const emptyLine = " ".repeat(width); + return [emptyLine, emptyLine]; + } +} diff --git a/packages/coding-agent/src/modes/interactive/components/tool-execution.ts b/packages/coding-agent/src/modes/interactive/components/tool-execution.ts new file mode 100644 index 0000000000000000000000000000000000000000..5355a3637aad9df5871ac907b680378ffd67b677 --- /dev/null +++ b/packages/coding-agent/src/modes/interactive/components/tool-execution.ts @@ -0,0 +1,421 @@ +import type { AgentToolResult } from "@earendil-works/pi-agent-core"; +import { + Box, + type Component, + Container, + getCapabilities, + Image, + MouseRegion, + Spacer, + Text, + type TUI, + type TuiMouseEvent, +} from "@earendil-works/pi-tui"; +import type { ToolDefinition, ToolRenderContext, ToolRenderResultOptions } from "../../../core/extensions/types.ts"; +import type { Theme } from "../theme/theme.ts"; + +/** + * What this component needs from a tool: how to draw it. It neither executes tools nor reads their + * parameter schemas, so a definition and a bare renderer pair are equally acceptable. + * + * The renderer parameters are `any` on purpose: a `ToolDefinition` types them from its schema, and + * narrowing them here would make those definitions unassignable. + */ +export interface ToolRenderers { + renderShell?: "default" | "self"; + renderCall?: (args: any, theme: Theme, context: ToolRenderContext) => Component; + renderResult?: ( + result: AgentToolResult, + options: ToolRenderResultOptions, + theme: Theme, + context: ToolRenderContext, + ) => Component; +} + +import { getTextOutput as getRenderedTextOutput } from "../../../core/tools/render-utils.ts"; +import { convertToPng } from "../../../utils/image-convert.ts"; +import { theme } from "../theme/theme.ts"; +import { keyHint } from "./keybinding-hints.ts"; + +const FALLBACK_PREVIEW_LINES = 10; + +export interface ToolExecutionOptions { + showImages?: boolean; + imageWidthCells?: number; +} + +export class ToolExecutionComponent extends Container { + private contentBox: Box; + private contentText: Text; + private contentTextRegion: MouseRegion; + private selfRenderContainer: Container; + private selfRenderHeight = 0; + private callRendererComponent?: Component; + private resultRendererComponent?: Component; + private rendererState: any = {}; + private imageComponents: Image[] = []; + private imageSpacers: Spacer[] = []; + private toolName: string; + private toolCallId: string; + private args: any; + private expanded = false; + private showImages: boolean; + private imageWidthCells: number; + private isPartial = true; + private toolDefinition?: ToolRenderers; + private ui: TUI; + private cwd: string; + private executionStarted = false; + private argsComplete = false; + private result?: { + content: Array<{ type: string; text?: string; data?: string; mimeType?: string }>; + isError: boolean; + details?: any; + }; + private convertedImages: Map = new Map(); + private hideComponent = false; + + constructor( + toolName: string, + toolCallId: string, + args: any, + options: ToolExecutionOptions = {}, + toolDefinition: ToolRenderers | ToolDefinition | undefined, + ui: TUI, + cwd: string, + ) { + super(); + this.toolName = toolName; + this.toolCallId = toolCallId; + this.args = args; + this.toolDefinition = toolDefinition; + this.showImages = options.showImages ?? true; + this.imageWidthCells = options.imageWidthCells ?? 60; + this.ui = ui; + this.cwd = cwd; + + this.addChild(new Spacer(1)); + + // Always create all shell variants. contentBox is used for default renderer-based composition. + // selfRenderContainer is used when the tool renders its own framing. + // contentText is reserved for generic fallback rendering when no tool definition exists. + this.contentBox = new Box(1, 1, (text: string) => theme.bg("toolPendingBg", text)); + this.contentText = new Text("", 1, 1, (text: string) => theme.bg("toolPendingBg", text)); + this.contentTextRegion = this.createResultRegion(this.contentText); + this.selfRenderContainer = new Container(); + + if (this.hasRendererDefinition()) { + this.addChild(this.getRenderShell() === "self" ? this.selfRenderContainer : this.contentBox); + } else { + this.addChild(this.contentTextRegion); + } + + this.updateDisplay(); + } + + private getCallRenderer(): ToolDefinition["renderCall"] | undefined { + return this.toolDefinition?.renderCall; + } + + private getResultRenderer(): ToolDefinition["renderResult"] | undefined { + return this.toolDefinition?.renderResult; + } + + private hasRendererDefinition(): boolean { + return this.toolDefinition !== undefined; + } + + private getRenderShell(): "default" | "self" { + return this.toolDefinition?.renderShell ?? "default"; + } + + private getRenderContext(lastComponent: Component | undefined): ToolRenderContext { + return { + args: this.args, + toolCallId: this.toolCallId, + invalidate: () => { + this.invalidate(); + this.ui.requestRender(); + }, + lastComponent, + state: this.rendererState, + cwd: this.cwd, + executionStarted: this.executionStarted, + argsComplete: this.argsComplete, + isPartial: this.isPartial, + expanded: this.expanded, + showImages: this.showImages, + isError: this.result?.isError ?? false, + }; + } + + private createCallFallback(): Component { + return new Text(theme.fg("toolTitle", theme.bold(this.toolName)), 0, 0); + } + + private createResultFallback(): Component | undefined { + const output = this.getTextOutput(); + if (!output) { + return undefined; + } + + const lines = output.split("\n"); + const displayLines = this.expanded ? lines : lines.slice(0, FALLBACK_PREVIEW_LINES); + const remaining = lines.length - displayLines.length; + let text = displayLines.map((line) => theme.fg("toolOutput", line)).join("\n"); + if (remaining > 0) { + text += `${theme.fg("muted", `\n... (${remaining} more lines,`)} ${keyHint("app.tools.expand", "to expand")}${theme.fg("muted", ")")}`; + } + return new Text(text, 0, 0); + } + + private createResultRegion(component: Component): MouseRegion { + return new MouseRegion(component, (event) => { + if (!this.result || event.type !== "click" || event.button !== "left") return undefined; + this.setExpanded(!this.expanded); + return { handled: true }; + }); + } + + updateArgs(args: any): void { + this.args = args; + this.updateDisplay(); + } + + markExecutionStarted(): void { + this.executionStarted = true; + this.updateDisplay(); + this.ui.requestRender(); + } + + setArgsComplete(): void { + this.argsComplete = true; + this.updateDisplay(); + this.ui.requestRender(); + } + + updateResult( + result: { + content: Array<{ type: string; text?: string; data?: string; mimeType?: string }>; + details?: any; + isError: boolean; + }, + isPartial = false, + ): void { + this.result = result; + this.isPartial = isPartial; + this.updateDisplay(); + this.maybeConvertImagesForKitty(); + } + + private maybeConvertImagesForKitty(): void { + const caps = getCapabilities(); + if (caps.images !== "kitty") return; + if (!this.result) return; + + const imageBlocks = this.result.content.filter((c) => c.type === "image"); + for (let i = 0; i < imageBlocks.length; i++) { + const img = imageBlocks[i]; + if (!img.data || !img.mimeType) continue; + if (img.mimeType === "image/png") continue; + if (this.convertedImages.has(i)) continue; + + const index = i; + convertToPng(img.data, img.mimeType).then((converted) => { + if (converted) { + this.convertedImages.set(index, converted); + this.updateDisplay(); + this.ui.requestRender(); + } + }); + } + } + + setExpanded(expanded: boolean): void { + this.expanded = expanded; + this.updateDisplay(); + } + + setShowImages(show: boolean): void { + this.showImages = show; + this.updateDisplay(); + } + + setImageWidthCells(width: number): void { + this.imageWidthCells = Math.max(1, Math.floor(width)); + this.updateDisplay(); + } + + override invalidate(): void { + super.invalidate(); + this.updateDisplay(); + } + + override render(width: number): string[] { + if (this.hideComponent) { + return []; + } + + if (this.hasRendererDefinition() && this.getRenderShell() === "self") { + const contentLines = this.selfRenderContainer.render(width); + this.selfRenderHeight = contentLines.length; + if (contentLines.length === 0 && this.imageComponents.length === 0) { + return []; + } + + const lines: string[] = []; + if (contentLines.length > 0) { + lines.push(""); + lines.push(...contentLines); + } + for (let i = 0; i < this.imageComponents.length; i++) { + const spacer = this.imageSpacers[i]; + if (spacer) { + lines.push(...spacer.render(width)); + } + const imageComponent = this.imageComponents[i]; + if (imageComponent) { + lines.push(...imageComponent.render(width)); + } + } + return lines; + } + + return super.render(width); + } + + override handleMouse(event: TuiMouseEvent): ReturnType { + if (!this.hasRendererDefinition() || this.getRenderShell() !== "self") return super.handleMouse(event); + if (event.y <= 0 || event.y > this.selfRenderHeight) return undefined; + return this.selfRenderContainer.handleMouse({ + ...event, + y: event.y - 1, + height: this.selfRenderHeight, + }); + } + + private updateDisplay(): void { + const bgFn = this.isPartial + ? (text: string) => theme.bg("toolPendingBg", text) + : this.result?.isError + ? (text: string) => theme.bg("toolErrorBg", text) + : (text: string) => theme.bg("toolSuccessBg", text); + + let hasContent = false; + this.hideComponent = false; + if (this.hasRendererDefinition()) { + const renderContainer = this.getRenderShell() === "self" ? this.selfRenderContainer : this.contentBox; + if (renderContainer instanceof Box) { + renderContainer.setBgFn(bgFn); + } + renderContainer.clear(); + + const callRenderer = this.getCallRenderer(); + if (!callRenderer) { + renderContainer.addChild(this.createResultRegion(this.createCallFallback())); + hasContent = true; + } else { + try { + const component = callRenderer(this.args, theme, this.getRenderContext(this.callRendererComponent)); + this.callRendererComponent = component; + renderContainer.addChild(this.createResultRegion(component)); + hasContent = true; + } catch { + this.callRendererComponent = undefined; + renderContainer.addChild(this.createResultRegion(this.createCallFallback())); + hasContent = true; + } + } + + if (this.result) { + const resultRenderer = this.getResultRenderer(); + if (!resultRenderer) { + const component = this.createResultFallback(); + if (component) { + renderContainer.addChild(this.createResultRegion(component)); + hasContent = true; + } + } else { + try { + const component = resultRenderer( + { content: this.result.content as any, details: this.result.details }, + { expanded: this.expanded, isPartial: this.isPartial }, + theme, + this.getRenderContext(this.resultRendererComponent), + ); + this.resultRendererComponent = component; + renderContainer.addChild(this.createResultRegion(component)); + hasContent = true; + } catch { + this.resultRendererComponent = undefined; + const component = this.createResultFallback(); + if (component) { + renderContainer.addChild(this.createResultRegion(component)); + hasContent = true; + } + } + } + } + } else { + this.contentText.setCustomBgFn(bgFn); + this.contentText.setText(this.formatToolExecution()); + hasContent = true; + } + + for (const img of this.imageComponents) { + this.removeChild(img); + } + this.imageComponents = []; + for (const spacer of this.imageSpacers) { + this.removeChild(spacer); + } + this.imageSpacers = []; + + if (this.result) { + const imageBlocks = this.result.content.filter((c) => c.type === "image"); + const caps = getCapabilities(); + for (let i = 0; i < imageBlocks.length; i++) { + const img = imageBlocks[i]; + if (caps.images && this.showImages && img.data && img.mimeType) { + const converted = this.convertedImages.get(i); + const imageData = converted?.data ?? img.data; + const imageMimeType = converted?.mimeType ?? img.mimeType; + if (caps.images === "kitty" && imageMimeType !== "image/png") continue; + + const spacer = new Spacer(1); + this.addChild(spacer); + this.imageSpacers.push(spacer); + const imageComponent = new Image( + imageData, + imageMimeType, + { fallbackColor: (s: string) => theme.fg("toolOutput", s) }, + { maxWidthCells: this.imageWidthCells }, + ); + this.imageComponents.push(imageComponent); + this.addChild(imageComponent); + } + } + } + + if (this.hasRendererDefinition() && !hasContent && this.imageComponents.length === 0) { + this.hideComponent = true; + } + } + + private getTextOutput(): string { + return getRenderedTextOutput(this.result, this.showImages); + } + + private formatToolExecution(): string { + let text = theme.fg("toolTitle", theme.bold(this.toolName)); + const content = JSON.stringify(this.args, null, 2); + if (content) { + text += `\n\n${content}`; + } + const output = this.getTextOutput(); + if (output) { + text += `\n${output}`; + } + return text; + } +} diff --git a/packages/coding-agent/src/modes/interactive/components/tree-selector.ts b/packages/coding-agent/src/modes/interactive/components/tree-selector.ts new file mode 100644 index 0000000000000000000000000000000000000000..f70e206030d7e72595e2632a4b37c3ba6a753734 --- /dev/null +++ b/packages/coding-agent/src/modes/interactive/components/tree-selector.ts @@ -0,0 +1,1427 @@ +import { + type Component, + Container, + type Focusable, + getKeybindings, + Input, + type Keybinding, + Spacer, + sliceByColumn, + Text, + truncateToWidth, + visibleWidth, + wrapTextWithAnsi, +} from "@earendil-works/pi-tui"; +import type { SessionTreeNode } from "../../../core/session-manager.ts"; +import { theme } from "../theme/theme.ts"; +import { DynamicBorder } from "./dynamic-border.ts"; +import { formatKeyText, keyHint } from "./keybinding-hints.ts"; + +/** Gutter info: position (displayIndent where connector was) and whether to show │ */ +interface GutterInfo { + position: number; // displayIndent level where the connector was shown + show: boolean; // true = show │, false = show spaces +} + +/** Flattened tree node for navigation */ +interface FlatNode { + node: SessionTreeNode; + /** Indentation level (each level = 3 chars) */ + indent: number; + /** Whether to show connector (├─ or └─) - true if parent has multiple children */ + showConnector: boolean; + /** If showConnector, true = last sibling (└─), false = not last (├─) */ + isLast: boolean; + /** Gutter info for each ancestor branch point */ + gutters: GutterInfo[]; + /** True if this node is a root under a virtual branching root (multiple roots) */ + isVirtualRootChild: boolean; +} + +interface HorizontalViewportRow { + gutter: string; + body: string; + anchorCol: number; + bodyWidth: number; + isSelected: boolean; +} + +const TREE_GUTTER_WIDTH = 2; +const MIN_VISIBLE_ANCHOR_CONTENT_WIDTH = 4; +const MAX_VISIBLE_ANCHOR_CONTENT_WIDTH = 20; +const MIN_ANCHOR_CONTEXT_WIDTH = 2; +const MAX_ANCHOR_CONTEXT_WIDTH = 12; + +/** + * Render tree rows into a horizontally clipped viewport. + * + * The tree gutter is always kept visible. The row bodies are shifted left only + * when the selected row's anchor (the start of its entry text after tree + * indentation/markers) would otherwise be too far right to see useful content. + */ +function renderHorizontalViewport(rows: HorizontalViewportRow[], width: number): string[] { + const viewportWidth = Math.max(0, width - TREE_GUTTER_WIDTH); + const maxBodyWidth = rows.reduce((max, row) => Math.max(max, row.bodyWidth), 0); + const maxHorizontalScroll = Math.max(0, maxBodyWidth - viewportWidth); + const selectedRow = rows.find((row) => row.isSelected); + + // Only pan horizontally when needed to keep enough selected-row content visible after its anchor. + let horizontalScroll = 0; + if (selectedRow && maxHorizontalScroll > 0) { + const minVisibleAnchorContentWidth = Math.min( + MAX_VISIBLE_ANCHOR_CONTENT_WIDTH, + Math.max(MIN_VISIBLE_ANCHOR_CONTENT_WIDTH, Math.floor(viewportWidth / 3)), + ); + if (selectedRow.anchorCol > viewportWidth - minVisibleAnchorContentWidth) { + const anchorContextWidth = Math.min( + MAX_ANCHOR_CONTEXT_WIDTH, + Math.max(MIN_ANCHOR_CONTEXT_WIDTH, Math.floor(viewportWidth / 4)), + ); + horizontalScroll = Math.min(maxHorizontalScroll, selectedRow.anchorCol - anchorContextWidth); + } + } + + // Clip only the body; the fixed-width gutter remains visible as navigation context. + return rows.map((row) => { + const line = + horizontalScroll > 0 + ? `${row.gutter}${sliceByColumn(row.body, horizontalScroll, viewportWidth, true)}\x1b[0m` + : row.gutter + row.body; + return truncateToWidth(line, width, ""); + }); +} + +/** Filter mode for tree display */ +export type FilterMode = "default" | "no-tools" | "user-only" | "labeled-only" | "all"; + +/** + * Tree list component with selection and ASCII art visualization + */ +/** Tool call info for lookup */ +interface ToolCallInfo { + name: string; + arguments: Record; +} + +class TreeList implements Component { + private flatNodes: FlatNode[] = []; + private filteredNodes: FlatNode[] = []; + private selectedIndex = 0; + private currentLeafId: string | null; + private maxVisibleLines: number; + private filterMode: FilterMode = "default"; + private searchQuery = ""; + private toolCallMap: Map = new Map(); + private multipleRoots = false; + private showLabelTimestamps = false; + private activePathIds: Set = new Set(); + private visibleParentMap: Map = new Map(); + private visibleChildrenMap: Map = new Map(); + private lastSelectedId: string | null = null; + private foldedNodes: Set = new Set(); + + public onSelect?: (entryId: string) => void; + public onCancel?: () => void; + public onCopy?: (text: string | undefined) => void; + public onLabelEdit?: (entryId: string, currentLabel: string | undefined) => void; + + constructor( + tree: SessionTreeNode[], + currentLeafId: string | null, + maxVisibleLines: number, + initialSelectedId?: string, + initialFilterMode?: FilterMode, + ) { + this.currentLeafId = currentLeafId; + this.maxVisibleLines = maxVisibleLines; + this.filterMode = initialFilterMode ?? "default"; + this.multipleRoots = tree.length > 1; + this.flatNodes = this.flattenTree(tree); + this.buildActivePath(); + this.applyFilter(); + + // Start with initialSelectedId if provided, otherwise current leaf + const targetId = initialSelectedId ?? currentLeafId; + this.selectedIndex = this.findNearestVisibleIndex(targetId); + this.lastSelectedId = this.filteredNodes[this.selectedIndex]?.node.entry.id ?? null; + } + + /** + * Find the index of the nearest visible entry, walking up the parent chain if needed. + * Returns the index in filteredNodes, or the last index as fallback. + */ + private findNearestVisibleIndex(entryId: string | null): number { + if (this.filteredNodes.length === 0) return 0; + + // Build a map for parent lookup + const entryMap = new Map(); + for (const flatNode of this.flatNodes) { + entryMap.set(flatNode.node.entry.id, flatNode); + } + + // Build a map of visible entry IDs to their indices in filteredNodes + const visibleIdToIndex = new Map(this.filteredNodes.map((node, i) => [node.node.entry.id, i])); + + // Walk from entryId up to root, looking for a visible entry + let currentId = entryId; + while (currentId !== null) { + const index = visibleIdToIndex.get(currentId); + if (index !== undefined) return index; + const node = entryMap.get(currentId); + if (!node) break; + currentId = node.node.entry.parentId ?? null; + } + + // Fallback: last visible entry + return this.filteredNodes.length - 1; + } + + /** Build the set of entry IDs on the path from root to current leaf */ + private buildActivePath(): void { + this.activePathIds.clear(); + if (!this.currentLeafId) return; + + // Build a map of id -> entry for parent lookup + const entryMap = new Map(); + for (const flatNode of this.flatNodes) { + entryMap.set(flatNode.node.entry.id, flatNode); + } + + // Walk from leaf to root + let currentId: string | null = this.currentLeafId; + while (currentId) { + this.activePathIds.add(currentId); + const node = entryMap.get(currentId); + if (!node) break; + currentId = node.node.entry.parentId ?? null; + } + } + + private flattenTree(roots: SessionTreeNode[]): FlatNode[] { + const result: FlatNode[] = []; + this.toolCallMap.clear(); + + // Indentation rules: + // - At indent 0: stay at 0 unless parent has >1 children (then +1) + // - At indent 1: children always go to indent 2 (visual grouping of subtree) + // - At indent 2+: stay flat for single-child chains, +1 only if parent branches + + // Stack items: [node, indent, justBranched, showConnector, isLast, gutters, isVirtualRootChild] + type StackItem = [SessionTreeNode, number, boolean, boolean, boolean, GutterInfo[], boolean]; + const stack: StackItem[] = []; + + // Determine which subtrees contain the active leaf (to sort current branch first) + // Use iterative post-order traversal to avoid stack overflow + const containsActive = new Map(); + const leafId = this.currentLeafId; + { + // Build list in pre-order, then process in reverse for post-order effect + const allNodes: SessionTreeNode[] = []; + const preOrderStack: SessionTreeNode[] = [...roots]; + while (preOrderStack.length > 0) { + const node = preOrderStack.pop()!; + allNodes.push(node); + // Push children in reverse so they're processed left-to-right + for (let i = node.children.length - 1; i >= 0; i--) { + preOrderStack.push(node.children[i]); + } + } + // Process in reverse (post-order): children before parents + for (let i = allNodes.length - 1; i >= 0; i--) { + const node = allNodes[i]; + let has = leafId !== null && node.entry.id === leafId; + for (const child of node.children) { + if (containsActive.get(child)) { + has = true; + } + } + containsActive.set(node, has); + } + } + + // Add roots in reverse order, prioritizing the one containing the active leaf + // If multiple roots, treat them as children of a virtual root that branches + const multipleRoots = roots.length > 1; + const orderedRoots = [...roots].sort((a, b) => Number(containsActive.get(b)) - Number(containsActive.get(a))); + for (let i = orderedRoots.length - 1; i >= 0; i--) { + const isLast = i === orderedRoots.length - 1; + stack.push([orderedRoots[i], multipleRoots ? 1 : 0, multipleRoots, multipleRoots, isLast, [], multipleRoots]); + } + + while (stack.length > 0) { + const [node, indent, justBranched, showConnector, isLast, gutters, isVirtualRootChild] = stack.pop()!; + + // Extract tool calls from assistant messages for later lookup + const entry = node.entry; + if (entry.type === "message" && entry.message.role === "assistant") { + const content = (entry.message as { content?: unknown }).content; + if (Array.isArray(content)) { + for (const block of content) { + if (typeof block === "object" && block !== null && "type" in block && block.type === "toolCall") { + const tc = block as { id: string; name: string; arguments: Record }; + this.toolCallMap.set(tc.id, { name: tc.name, arguments: tc.arguments }); + } + } + } + } + + result.push({ node, indent, showConnector, isLast, gutters, isVirtualRootChild }); + + const children = node.children; + const multipleChildren = children.length > 1; + + // Order children so the branch containing the active leaf comes first + const orderedChildren = (() => { + const prioritized: SessionTreeNode[] = []; + const rest: SessionTreeNode[] = []; + for (const child of children) { + if (containsActive.get(child)) { + prioritized.push(child); + } else { + rest.push(child); + } + } + return [...prioritized, ...rest]; + })(); + + // Calculate child indent + let childIndent: number; + if (multipleChildren) { + // Parent branches: children get +1 + childIndent = indent + 1; + } else if (justBranched && indent > 0) { + // First generation after a branch: +1 for visual grouping + childIndent = indent + 1; + } else { + // Single-child chain: stay flat + childIndent = indent; + } + + // Build gutters for children + // If this node showed a connector, add a gutter entry for descendants + // Only add gutter if connector is actually displayed (not suppressed for virtual root children) + const connectorDisplayed = showConnector && !isVirtualRootChild; + // When connector is displayed, add a gutter entry at the connector's position + // Connector is at position (displayIndent - 1), so gutter should be there too + const currentDisplayIndent = this.multipleRoots ? Math.max(0, indent - 1) : indent; + const connectorPosition = Math.max(0, currentDisplayIndent - 1); + const childGutters: GutterInfo[] = connectorDisplayed + ? [...gutters, { position: connectorPosition, show: !isLast }] + : gutters; + + // Add children in reverse order + for (let i = orderedChildren.length - 1; i >= 0; i--) { + const childIsLast = i === orderedChildren.length - 1; + stack.push([ + orderedChildren[i], + childIndent, + multipleChildren, + multipleChildren, + childIsLast, + childGutters, + false, + ]); + } + } + + return result; + } + + private applyFilter(): void { + // Update lastSelectedId only when we have a valid selection (non-empty list) + // This preserves the selection when switching through empty filter results + if (this.filteredNodes.length > 0) { + this.lastSelectedId = this.filteredNodes[this.selectedIndex]?.node.entry.id ?? this.lastSelectedId; + } + + const searchTokens = this.searchQuery.toLowerCase().split(/\s+/).filter(Boolean); + + this.filteredNodes = this.flatNodes.filter((flatNode) => { + const entry = flatNode.node.entry; + const isCurrentLeaf = entry.id === this.currentLeafId; + + // Skip assistant messages with only tool calls (no text) unless error/aborted + // Always show current leaf so active position is visible + if (entry.type === "message" && entry.message.role === "assistant" && !isCurrentLeaf) { + const msg = entry.message as { stopReason?: string; content?: unknown }; + const hasText = this.hasTextContent(msg.content); + const isErrorOrAborted = msg.stopReason && msg.stopReason !== "stop" && msg.stopReason !== "toolUse"; + // Only hide if no text AND not an error/aborted message + if (!hasText && !isErrorOrAborted) { + return false; + } + } + + // Apply filter mode + let passesFilter = true; + // Entry types hidden in default view (settings/bookkeeping) + const isSettingsEntry = + entry.type === "label" || + entry.type === "custom" || + entry.type === "model_change" || + entry.type === "thinking_level_change" || + entry.type === "session_info"; + + switch (this.filterMode) { + case "user-only": + // Just user messages + passesFilter = entry.type === "message" && entry.message.role === "user"; + break; + case "no-tools": + // Default minus tool results + passesFilter = !isSettingsEntry && !(entry.type === "message" && entry.message.role === "toolResult"); + break; + case "labeled-only": + // Just labeled entries + passesFilter = flatNode.node.label !== undefined; + break; + case "all": + // Show everything + passesFilter = true; + break; + default: + // Default mode: hide settings/bookkeeping entries + passesFilter = !isSettingsEntry; + break; + } + + if (!passesFilter) return false; + + // Apply search filter + if (searchTokens.length > 0) { + const nodeText = this.getSearchableText(flatNode.node).toLowerCase(); + return searchTokens.every((token) => nodeText.includes(token)); + } + + return true; + }); + + // Filter out descendants of folded nodes. + if (this.foldedNodes.size > 0) { + const skipSet = new Set(); + for (const flatNode of this.flatNodes) { + const { id, parentId } = flatNode.node.entry; + if (parentId != null && (this.foldedNodes.has(parentId) || skipSet.has(parentId))) { + skipSet.add(id); + } + } + this.filteredNodes = this.filteredNodes.filter((flatNode) => !skipSet.has(flatNode.node.entry.id)); + } + + // Recalculate visual structure (indent, connectors, gutters) based on visible tree + this.recalculateVisualStructure(); + + // Try to preserve cursor on the same node, or find nearest visible ancestor + if (this.lastSelectedId) { + this.selectedIndex = this.findNearestVisibleIndex(this.lastSelectedId); + } else if (this.selectedIndex >= this.filteredNodes.length) { + // Clamp index if out of bounds + this.selectedIndex = Math.max(0, this.filteredNodes.length - 1); + } + + // Update lastSelectedId to the actual selection (may have changed due to parent walk) + if (this.filteredNodes.length > 0) { + this.lastSelectedId = this.filteredNodes[this.selectedIndex]?.node.entry.id ?? this.lastSelectedId; + } + } + + /** + * Recompute indentation/connectors for the filtered view + * + * Filtering can hide intermediate entries; descendants attach to the nearest visible ancestor. + * Keep indentation semantics aligned with flattenTree() so single-child chains don't drift right. + */ + private recalculateVisualStructure(): void { + if (this.filteredNodes.length === 0) return; + + const visibleIds = new Set(this.filteredNodes.map((n) => n.node.entry.id)); + + // Build entry map for efficient parent lookup (using full tree) + const entryMap = new Map(); + for (const flatNode of this.flatNodes) { + entryMap.set(flatNode.node.entry.id, flatNode); + } + + // Find nearest visible ancestor for a node + const findVisibleAncestor = (nodeId: string): string | null => { + let currentId = entryMap.get(nodeId)?.node.entry.parentId ?? null; + while (currentId !== null) { + if (visibleIds.has(currentId)) { + return currentId; + } + currentId = entryMap.get(currentId)?.node.entry.parentId ?? null; + } + return null; + }; + + // Build visible tree structure: + // - visibleParent: nodeId → nearest visible ancestor (or null for roots) + // - visibleChildren: parentId → list of visible children (in filteredNodes order) + const visibleParent = new Map(); + const visibleChildren = new Map(); + visibleChildren.set(null, []); // root-level nodes + + for (const flatNode of this.filteredNodes) { + const nodeId = flatNode.node.entry.id; + const ancestorId = findVisibleAncestor(nodeId); + visibleParent.set(nodeId, ancestorId); + + if (!visibleChildren.has(ancestorId)) { + visibleChildren.set(ancestorId, []); + } + visibleChildren.get(ancestorId)!.push(nodeId); + } + + // Update multipleRoots based on visible roots + const visibleRootIds = visibleChildren.get(null)!; + this.multipleRoots = visibleRootIds.length > 1; + + // Build a map for quick lookup: nodeId → FlatNode + const filteredNodeMap = new Map(); + for (const flatNode of this.filteredNodes) { + filteredNodeMap.set(flatNode.node.entry.id, flatNode); + } + + // DFS over the visible tree using flattenTree() indentation semantics + // Stack items: [nodeId, indent, justBranched, showConnector, isLast, gutters, isVirtualRootChild] + type StackItem = [string, number, boolean, boolean, boolean, GutterInfo[], boolean]; + const stack: StackItem[] = []; + + // Add visible roots in reverse order (to process in forward order via stack) + for (let i = visibleRootIds.length - 1; i >= 0; i--) { + const isLast = i === visibleRootIds.length - 1; + stack.push([ + visibleRootIds[i], + this.multipleRoots ? 1 : 0, + this.multipleRoots, + this.multipleRoots, + isLast, + [], + this.multipleRoots, + ]); + } + + while (stack.length > 0) { + const [nodeId, indent, justBranched, showConnector, isLast, gutters, isVirtualRootChild] = stack.pop()!; + + const flatNode = filteredNodeMap.get(nodeId); + if (!flatNode) continue; + + // Update this node's visual properties + flatNode.indent = indent; + flatNode.showConnector = showConnector; + flatNode.isLast = isLast; + flatNode.gutters = gutters; + flatNode.isVirtualRootChild = isVirtualRootChild; + + // Get visible children of this node + const children = visibleChildren.get(nodeId) || []; + const multipleChildren = children.length > 1; + + // Child indent follows flattenTree(): branch points (and first generation after a branch) shift +1 + let childIndent: number; + if (multipleChildren) { + childIndent = indent + 1; + } else if (justBranched && indent > 0) { + childIndent = indent + 1; + } else { + childIndent = indent; + } + + // Child gutters follow flattenTree() connector/gutter rules + const connectorDisplayed = showConnector && !isVirtualRootChild; + const currentDisplayIndent = this.multipleRoots ? Math.max(0, indent - 1) : indent; + const connectorPosition = Math.max(0, currentDisplayIndent - 1); + const childGutters: GutterInfo[] = connectorDisplayed + ? [...gutters, { position: connectorPosition, show: !isLast }] + : gutters; + + // Add children in reverse order (to process in forward order via stack) + for (let i = children.length - 1; i >= 0; i--) { + const childIsLast = i === children.length - 1; + stack.push([ + children[i], + childIndent, + multipleChildren, + multipleChildren, + childIsLast, + childGutters, + false, + ]); + } + } + + // Store visible tree maps for ancestor/descendant lookups in navigation + this.visibleParentMap = visibleParent; + this.visibleChildrenMap = visibleChildren; + } + + /** Get searchable text content from a node */ + private getSearchableText(node: SessionTreeNode): string { + const entry = node.entry; + const parts: string[] = []; + + if (node.label) { + parts.push(node.label); + } + + switch (entry.type) { + case "message": { + const msg = entry.message; + parts.push(msg.role); + if ("content" in msg && msg.content) { + parts.push(this.extractContent(msg.content)); + } + if (msg.role === "bashExecution") { + const bashMsg = msg as { command?: string }; + if (bashMsg.command) parts.push(bashMsg.command); + } + break; + } + case "custom_message": { + parts.push(entry.customType); + if (typeof entry.content === "string") { + parts.push(entry.content); + } else { + parts.push(this.extractContent(entry.content)); + } + break; + } + case "compaction": + parts.push("compaction"); + break; + case "branch_summary": + parts.push("branch summary", entry.summary); + break; + case "session_info": + parts.push("title"); + if (entry.name) parts.push(entry.name); + break; + case "model_change": + parts.push("model", entry.modelId); + break; + case "thinking_level_change": + parts.push("thinking", entry.thinkingLevel); + break; + case "custom": + parts.push("custom", entry.customType); + break; + case "label": + parts.push("label", entry.label ?? ""); + break; + } + + return parts.join(" "); + } + + invalidate(): void {} + + getSearchQuery(): string { + return this.searchQuery; + } + + getSelectedNode(): SessionTreeNode | undefined { + return this.filteredNodes[this.selectedIndex]?.node; + } + + copySelected(): void { + const node = this.getSelectedNode(); + this.onCopy?.(node ? this.getEntryCopyText(node) : undefined); + } + + updateNodeLabel(entryId: string, label: string | undefined, labelTimestamp?: string): void { + for (const flatNode of this.flatNodes) { + if (flatNode.node.entry.id === entryId) { + flatNode.node.label = label; + flatNode.node.labelTimestamp = label ? (labelTimestamp ?? new Date().toISOString()) : undefined; + break; + } + } + } + + private getStatusLabels(): string { + let labels = ""; + switch (this.filterMode) { + case "no-tools": + labels += " [no-tools]"; + break; + case "user-only": + labels += " [user]"; + break; + case "labeled-only": + labels += " [labeled]"; + break; + case "all": + labels += " [all]"; + break; + } + if (this.showLabelTimestamps) { + labels += " [+label time]"; + } + return labels; + } + + render(width: number): string[] { + const lines: string[] = []; + + if (this.filteredNodes.length === 0) { + lines.push(truncateToWidth(theme.fg("muted", " No entries found"), width)); + lines.push(truncateToWidth(theme.fg("muted", ` (0/0)${this.getStatusLabels()}`), width)); + return lines; + } + + const startIndex = Math.max( + 0, + Math.min( + this.selectedIndex - Math.floor(this.maxVisibleLines / 2), + this.filteredNodes.length - this.maxVisibleLines, + ), + ); + const endIndex = Math.min(startIndex + this.maxVisibleLines, this.filteredNodes.length); + + const renderedRows: HorizontalViewportRow[] = []; + for (let i = startIndex; i < endIndex; i++) { + const flatNode = this.filteredNodes[i]; + const entry = flatNode.node.entry; + const isSelected = i === this.selectedIndex; + + // Build line: cursor + prefix + path marker + label + content + const cursor = isSelected ? theme.fg("accent", "› ") : " "; + + // If multiple roots, shift display (roots at 0, not 1) + const displayIndent = this.multipleRoots ? Math.max(0, flatNode.indent - 1) : flatNode.indent; + + // Build prefix with gutters at their correct positions + // Each gutter has a position (displayIndent where its connector was shown) + const connector = + flatNode.showConnector && !flatNode.isVirtualRootChild ? (flatNode.isLast ? "└─ " : "├─ ") : ""; + const connectorPosition = connector ? displayIndent - 1 : -1; + + // Build prefix char by char, placing gutters and connector at their positions + const totalChars = displayIndent * 3; + const prefixChars: string[] = []; + const isFolded = this.foldedNodes.has(entry.id); + for (let i = 0; i < totalChars; i++) { + const level = Math.floor(i / 3); + const posInLevel = i % 3; + + // Check if there's a gutter at this level + const gutter = flatNode.gutters.find((g) => g.position === level); + if (gutter) { + if (posInLevel === 0) { + prefixChars.push(gutter.show ? "│" : " "); + } else { + prefixChars.push(" "); + } + } else if (connector && level === connectorPosition) { + // Connector at this level, with fold indicator + if (posInLevel === 0) { + prefixChars.push(flatNode.isLast ? "└" : "├"); + } else if (posInLevel === 1) { + const foldable = this.isFoldable(entry.id); + prefixChars.push(isFolded ? "⊞" : foldable ? "⊟" : "─"); + } else { + prefixChars.push(" "); + } + } else { + prefixChars.push(" "); + } + } + const prefix = prefixChars.join(""); + + // Fold marker for nodes without connectors (roots) + const showsFoldInConnector = flatNode.showConnector && !flatNode.isVirtualRootChild; + const foldMarker = isFolded && !showsFoldInConnector ? theme.fg("accent", "⊞ ") : ""; + + // Active path marker - shown right before the entry text + const isOnActivePath = this.activePathIds.has(entry.id); + const pathMarker = isOnActivePath ? theme.fg("accent", "• ") : ""; + + const label = flatNode.node.label ? theme.fg("warning", `[${flatNode.node.label}] `) : ""; + const labelTimestamp = + this.showLabelTimestamps && flatNode.node.label && flatNode.node.labelTimestamp + ? theme.fg("muted", `${this.formatLabelTimestamp(flatNode.node.labelTimestamp)} `) + : ""; + const content = this.getEntryDisplayText(flatNode.node, isSelected); + const prefixPart = theme.fg("dim", prefix) + foldMarker + pathMarker; + const anchorCol = visibleWidth(prefixPart); + let gutter = cursor; + let body = prefixPart + label + labelTimestamp + content; + if (isSelected) { + gutter = theme.bg("selectedBg", gutter); + body = theme.bg("selectedBg", body); + } + renderedRows.push({ gutter, body, anchorCol, bodyWidth: visibleWidth(body), isSelected }); + } + + lines.push(...renderHorizontalViewport(renderedRows, width)); + lines.push( + truncateToWidth( + theme.fg("muted", ` (${this.selectedIndex + 1}/${this.filteredNodes.length})${this.getStatusLabels()}`), + width, + ), + ); + + return lines; + } + + private getEntryDisplayText(node: SessionTreeNode, isSelected: boolean): string { + const entry = node.entry; + let result: string; + + const normalize = (s: string) => s.replace(/[\n\t]/g, " ").trim(); + + switch (entry.type) { + case "message": { + const msg = entry.message; + const role = msg.role; + if (role === "user") { + const msgWithContent = msg as { content?: unknown }; + const content = normalize(this.extractContent(msgWithContent.content)); + result = theme.fg("accent", "user: ") + content; + } else if (role === "assistant") { + const msgWithContent = msg as { content?: unknown; stopReason?: string; errorMessage?: string }; + const textContent = normalize(this.extractContent(msgWithContent.content)); + if (textContent) { + result = theme.fg("success", "assistant: ") + textContent; + } else if (msgWithContent.stopReason === "aborted") { + result = theme.fg("success", "assistant: ") + theme.fg("muted", "(aborted)"); + } else if (msgWithContent.errorMessage) { + const errMsg = normalize(msgWithContent.errorMessage).slice(0, 80); + result = theme.fg("success", "assistant: ") + theme.fg("error", errMsg); + } else { + result = theme.fg("success", "assistant: ") + theme.fg("muted", "(no content)"); + } + } else if (role === "toolResult") { + const toolMsg = msg as { toolCallId?: string; toolName?: string }; + const toolCall = toolMsg.toolCallId ? this.toolCallMap.get(toolMsg.toolCallId) : undefined; + if (toolCall) { + result = theme.fg("muted", this.formatToolCall(toolCall.name, toolCall.arguments)); + } else { + result = theme.fg("muted", `[${toolMsg.toolName ?? "tool"}]`); + } + } else if (role === "bashExecution") { + const bashMsg = msg as { command?: string }; + result = theme.fg("dim", `[bash]: ${normalize(bashMsg.command ?? "")}`); + } else { + result = theme.fg("dim", `[${role}]`); + } + break; + } + case "custom_message": { + const content = + typeof entry.content === "string" + ? entry.content + : entry.content + .filter((c): c is { type: "text"; text: string } => c.type === "text") + .map((c) => c.text) + .join(""); + result = theme.fg("customMessageLabel", `[${entry.customType}]: `) + normalize(content); + break; + } + case "compaction": { + const tokens = Math.round(entry.tokensBefore / 1000); + result = theme.fg("borderAccent", `[compaction: ${tokens}k tokens]`); + break; + } + case "branch_summary": + result = theme.fg("warning", `[branch summary]: `) + normalize(entry.summary); + break; + case "model_change": + result = theme.fg("dim", `[model: ${entry.modelId}]`); + break; + case "thinking_level_change": + result = theme.fg("dim", `[thinking: ${entry.thinkingLevel}]`); + break; + case "custom": + result = theme.fg("dim", `[custom: ${entry.customType}]`); + break; + case "label": + result = theme.fg("dim", `[label: ${entry.label ?? "(cleared)"}]`); + break; + case "session_info": + result = entry.name + ? [theme.fg("dim", "[title: "), theme.fg("dim", entry.name), theme.fg("dim", "]")].join("") + : [theme.fg("dim", "[title: "), theme.italic(theme.fg("dim", "empty")), theme.fg("dim", "]")].join(""); + break; + default: + result = ""; + } + + return isSelected ? theme.bold(result) : result; + } + + private formatLabelTimestamp(timestamp: string): string { + const date = new Date(timestamp); + const now = new Date(); + const hours = date.getHours().toString().padStart(2, "0"); + const minutes = date.getMinutes().toString().padStart(2, "0"); + const time = `${hours}:${minutes}`; + + if ( + date.getFullYear() === now.getFullYear() && + date.getMonth() === now.getMonth() && + date.getDate() === now.getDate() + ) { + return time; + } + + const month = date.getMonth() + 1; + const day = date.getDate(); + if (date.getFullYear() === now.getFullYear()) { + return `${month}/${day} ${time}`; + } + + const year = date.getFullYear().toString().slice(-2); + return `${year}/${month}/${day} ${time}`; + } + + private extractContent(content: unknown): string { + return this.extractFullContent(content).slice(0, 200); + } + + private extractFullContent(content: unknown): string { + if (typeof content === "string") return content; + if (!Array.isArray(content)) return ""; + + let result = ""; + for (const block of content) { + if (typeof block === "object" && block !== null && "type" in block && block.type === "text") { + result += (block as { text: string }).text; + } + } + return result; + } + + private getEntryCopyText(node: SessionTreeNode): string | undefined { + const entry = node.entry; + let text: string | undefined; + + switch (entry.type) { + case "message": + if (entry.message.role === "bashExecution") { + text = entry.message.command; + } else if ("content" in entry.message) { + text = this.extractFullContent(entry.message.content); + if (!text && entry.message.role === "assistant") { + text = entry.message.errorMessage; + } + } + break; + case "custom_message": + text = this.extractFullContent(entry.content); + break; + case "compaction": + text = entry.summary; + break; + case "branch_summary": + text = entry.summary; + break; + } + + return text?.trim() ? text : undefined; + } + + private hasTextContent(content: unknown): boolean { + if (typeof content === "string") return content.trim().length > 0; + if (Array.isArray(content)) { + for (const c of content) { + if (typeof c === "object" && c !== null && "type" in c && c.type === "text") { + const text = (c as { text?: string }).text; + if (text && text.trim().length > 0) return true; + } + } + } + return false; + } + + private formatToolCall(name: string, args: Record): string { + const shortenPath = (p: string): string => { + const home = process.env.HOME || process.env.USERPROFILE || ""; + if (home && p.startsWith(home)) return `~${p.slice(home.length)}`; + return p; + }; + + switch (name) { + case "read": { + const path = shortenPath(String(args.path || args.file_path || "")); + const offset = args.offset as number | undefined; + const limit = args.limit as number | undefined; + let display = path; + if (offset !== undefined || limit !== undefined) { + const start = offset ?? 1; + const end = limit !== undefined ? start + limit - 1 : ""; + display += `:${start}${end ? `-${end}` : ""}`; + } + return `[read: ${display}]`; + } + case "write": { + const path = shortenPath(String(args.path || args.file_path || "")); + return `[write: ${path}]`; + } + case "edit": { + const path = shortenPath(String(args.path || args.file_path || "")); + return `[edit: ${path}]`; + } + case "bash": { + const rawCmd = String(args.command || ""); + const cmd = rawCmd + .replace(/[\n\t]/g, " ") + .trim() + .slice(0, 50); + return `[bash: ${cmd}${rawCmd.length > 50 ? "..." : ""}]`; + } + case "grep": { + const pattern = String(args.pattern || ""); + const path = shortenPath(String(args.path || ".")); + return `[grep: /${pattern}/ in ${path}]`; + } + case "find": { + const pattern = String(args.pattern || ""); + const path = shortenPath(String(args.path || ".")); + return `[find: ${pattern} in ${path}]`; + } + case "ls": { + const path = shortenPath(String(args.path || ".")); + return `[ls: ${path}]`; + } + default: { + // Custom tool - show name and truncated JSON args + const argsStr = JSON.stringify(args).slice(0, 40); + return `[${name}: ${argsStr}${JSON.stringify(args).length > 40 ? "..." : ""}]`; + } + } + } + + handleInput(keyData: string): void { + const kb = getKeybindings(); + if (kb.matches(keyData, "tui.select.up")) { + this.selectedIndex = this.selectedIndex === 0 ? this.filteredNodes.length - 1 : this.selectedIndex - 1; + } else if (kb.matches(keyData, "tui.select.down")) { + this.selectedIndex = this.selectedIndex === this.filteredNodes.length - 1 ? 0 : this.selectedIndex + 1; + } else if (kb.matches(keyData, "app.tree.foldOrUp")) { + const currentId = this.filteredNodes[this.selectedIndex]?.node.entry.id; + if (currentId && this.isFoldable(currentId) && !this.foldedNodes.has(currentId)) { + this.foldedNodes.add(currentId); + this.applyFilter(); + } else { + this.selectedIndex = this.findBranchSegmentStart("up"); + } + } else if (kb.matches(keyData, "app.tree.unfoldOrDown")) { + const currentId = this.filteredNodes[this.selectedIndex]?.node.entry.id; + if (currentId && this.foldedNodes.has(currentId)) { + this.foldedNodes.delete(currentId); + this.applyFilter(); + } else { + this.selectedIndex = this.findBranchSegmentStart("down"); + } + } else if (kb.matches(keyData, "tui.editor.cursorLeft") || kb.matches(keyData, "tui.select.pageUp")) { + // Page up + this.selectedIndex = Math.max(0, this.selectedIndex - this.maxVisibleLines); + } else if (kb.matches(keyData, "tui.editor.cursorRight") || kb.matches(keyData, "tui.select.pageDown")) { + // Page down + this.selectedIndex = Math.min(this.filteredNodes.length - 1, this.selectedIndex + this.maxVisibleLines); + } else if (kb.matches(keyData, "tui.select.confirm")) { + const selected = this.filteredNodes[this.selectedIndex]; + if (selected && this.onSelect) { + this.onSelect(selected.node.entry.id); + } + } else if (kb.matches(keyData, "app.message.copy")) { + this.copySelected(); + } else if (kb.matches(keyData, "tui.select.cancel")) { + if (this.searchQuery) { + this.searchQuery = ""; + this.foldedNodes.clear(); + this.applyFilter(); + } else { + this.onCancel?.(); + } + } else if (kb.matches(keyData, "app.tree.filter.default")) { + // Direct filter: default + this.filterMode = "default"; + this.foldedNodes.clear(); + this.applyFilter(); + } else if (kb.matches(keyData, "app.tree.filter.noTools")) { + // Toggle filter: no-tools ↔ default + this.filterMode = this.filterMode === "no-tools" ? "default" : "no-tools"; + this.foldedNodes.clear(); + this.applyFilter(); + } else if (kb.matches(keyData, "app.tree.filter.userOnly")) { + // Toggle filter: user-only ↔ default + this.filterMode = this.filterMode === "user-only" ? "default" : "user-only"; + this.foldedNodes.clear(); + this.applyFilter(); + } else if (kb.matches(keyData, "app.tree.filter.labeledOnly")) { + // Toggle filter: labeled-only ↔ default + this.filterMode = this.filterMode === "labeled-only" ? "default" : "labeled-only"; + this.foldedNodes.clear(); + this.applyFilter(); + } else if (kb.matches(keyData, "app.tree.filter.all")) { + // Toggle filter: all ↔ default + this.filterMode = this.filterMode === "all" ? "default" : "all"; + this.foldedNodes.clear(); + this.applyFilter(); + } else if (kb.matches(keyData, "app.tree.filter.cycleBackward")) { + // Cycle filter backwards + const modes: FilterMode[] = ["default", "no-tools", "user-only", "labeled-only", "all"]; + const currentIndex = modes.indexOf(this.filterMode); + this.filterMode = modes[(currentIndex - 1 + modes.length) % modes.length]; + this.foldedNodes.clear(); + this.applyFilter(); + } else if (kb.matches(keyData, "app.tree.filter.cycleForward")) { + // Cycle filter forwards: default → no-tools → user-only → labeled-only → all → default + const modes: FilterMode[] = ["default", "no-tools", "user-only", "labeled-only", "all"]; + const currentIndex = modes.indexOf(this.filterMode); + this.filterMode = modes[(currentIndex + 1) % modes.length]; + this.foldedNodes.clear(); + this.applyFilter(); + } else if (kb.matches(keyData, "tui.editor.deleteCharBackward")) { + if (this.searchQuery.length > 0) { + this.searchQuery = this.searchQuery.slice(0, -1); + this.foldedNodes.clear(); + this.applyFilter(); + } + } else if (kb.matches(keyData, "app.tree.editLabel")) { + const selected = this.filteredNodes[this.selectedIndex]; + if (selected && this.onLabelEdit) { + this.onLabelEdit(selected.node.entry.id, selected.node.label); + } + } else if (kb.matches(keyData, "app.tree.toggleLabelTimestamp")) { + this.showLabelTimestamps = !this.showLabelTimestamps; + } else { + const hasControlChars = [...keyData].some((ch) => { + const code = ch.charCodeAt(0); + return code < 32 || code === 0x7f || (code >= 0x80 && code <= 0x9f); + }); + if (!hasControlChars && keyData.length > 0) { + this.searchQuery += keyData; + this.foldedNodes.clear(); + this.applyFilter(); + } + } + } + + /** + * Whether a node can be folded. A node is foldable if it has visible children + * and is either a root (no visible parent) or a segment start (visible parent + * has multiple visible children). + */ + private isFoldable(entryId: string): boolean { + const children = this.visibleChildrenMap.get(entryId); + if (!children || children.length === 0) return false; + const parentId = this.visibleParentMap.get(entryId); + if (parentId === null || parentId === undefined) return true; + const siblings = this.visibleChildrenMap.get(parentId); + return siblings !== undefined && siblings.length > 1; + } + + /** + * Find the index of the next branch segment start in the given direction. + * A segment start is the first child of a branch point. + * + * "up" walks the visible parent chain; "down" walks visible children + * (always following the first child). + */ + private findBranchSegmentStart(direction: "up" | "down"): number { + const selectedId = this.filteredNodes[this.selectedIndex]?.node.entry.id; + if (!selectedId) return this.selectedIndex; + + const indexByEntryId = new Map(this.filteredNodes.map((node, i) => [node.node.entry.id, i])); + let currentId: string = selectedId; + if (direction === "down") { + while (true) { + const children: string[] = this.visibleChildrenMap.get(currentId) ?? []; + if (children.length === 0) return indexByEntryId.get(currentId)!; + if (children.length > 1) return indexByEntryId.get(children[0])!; + currentId = children[0]; + } + } + + // direction === "up" + while (true) { + const parentId: string | null = this.visibleParentMap.get(currentId) ?? null; + if (parentId === null) return indexByEntryId.get(currentId)!; + const children = this.visibleChildrenMap.get(parentId) ?? []; + if (children.length > 1) { + const segmentStart = indexByEntryId.get(currentId)!; + if (segmentStart < this.selectedIndex) { + return segmentStart; + } + } + currentId = parentId; + } + } +} + +/** Component that displays the current search query */ +class SearchLine implements Component { + private treeList: TreeList; + + constructor(treeList: TreeList) { + this.treeList = treeList; + } + + invalidate(): void {} + + render(width: number): string[] { + const query = this.treeList.getSearchQuery(); + if (query) { + return [truncateToWidth(` ${theme.fg("muted", "Type to search:")} ${theme.fg("accent", query)}`, width)]; + } + return [truncateToWidth(` ${theme.fg("muted", "Type to search:")}`, width)]; + } + + handleInput(_keyData: string): void {} +} + +/** Component that renders tree help as semantic rows with chunk-aware wrapping */ +class TreeHelp implements Component { + invalidate(): void {} + + render(width: number): string[] { + const items = TREE_HELP_ITEMS.map(({ keys, label, labelFirst }) => { + const text = formatHelpKeys(keys); + if (!text) return label; + return labelFirst ? `${label} ${text}` : `${text} ${label}`; + }); + + const availableWidth = Math.max(1, width); + const indent = " "; + const separator = " · "; + const lines: string[] = []; + let currentLine = ""; + + for (const item of items) { + const candidate = currentLine + ? `${currentLine}${separator}${item}` + : visibleWidth(`${indent}${item}`) <= availableWidth + ? `${indent}${item}` + : item; + if (!currentLine || visibleWidth(candidate) <= availableWidth) { + currentLine = candidate; + continue; + } + + lines.push(...wrapTextWithAnsi(currentLine.trimEnd(), availableWidth)); + currentLine = visibleWidth(`${indent}${item}`) <= availableWidth ? `${indent}${item}` : item; + } + + if (currentLine) { + lines.push(...wrapTextWithAnsi(currentLine.trimEnd(), availableWidth)); + } + + return lines.map((line) => theme.fg("muted", line)); + } +} + +const TREE_HELP_ITEMS: Array<{ keys: Keybinding[]; label: string; labelFirst?: boolean }> = [ + { keys: ["tui.select.up", "tui.select.down"], label: "move" }, + { keys: ["tui.editor.cursorLeft", "tui.editor.cursorRight"], label: "page" }, + { keys: ["app.tree.foldOrUp", "app.tree.unfoldOrDown"], label: "branch" }, + { keys: ["app.message.copy"], label: "copy" }, + { keys: ["app.tree.editLabel"], label: "label" }, + { keys: ["app.tree.toggleLabelTimestamp"], label: "label time" }, + { + keys: [ + "app.tree.filter.default", + "app.tree.filter.noTools", + "app.tree.filter.userOnly", + "app.tree.filter.labeledOnly", + "app.tree.filter.all", + ], + label: "filters", + labelFirst: true, + }, + { keys: ["app.tree.filter.cycleForward", "app.tree.filter.cycleBackward"], label: "cycle", labelFirst: true }, +]; + +function formatHelpKeys(keybindings: Keybinding[]): string { + const keys: string[] = []; + for (const keybinding of keybindings) { + const key = getKeybindings().getKeys(keybinding)[0]; + if (key !== undefined) keys.push(key); + } + if (keys.length === 0) return ""; + + return formatKeyText(compactRawKeys(keys)) + .replace(/\bpageUp\b/g, "pgup") + .replace(/\bpageDown\b/g, "pgdn") + .replace(/\bup\b/g, "↑") + .replace(/\bdown\b/g, "↓") + .replace(/\bleft\b/g, "←") + .replace(/\bright\b/g, "→"); +} + +function compactRawKeys(keys: string[]): string { + if (keys.length === 1) return keys[0]!; + + const parts = keys.map((key) => { + const separatorIndex = key.lastIndexOf("+"); + return separatorIndex === -1 + ? { prefix: "", suffix: key } + : { prefix: key.slice(0, separatorIndex + 1), suffix: key.slice(separatorIndex + 1) }; + }); + const prefix = parts[0]!.prefix; + return prefix && parts.every((part) => part.prefix === prefix) + ? `${prefix}${parts.map((part) => part.suffix).join("/")}` + : keys.join("/"); +} + +/** Label input component shown when editing a label */ +class LabelInput implements Component, Focusable { + private input: Input; + private entryId: string; + public onSubmit?: (entryId: string, label: string | undefined) => void; + public onCancel?: () => void; + + // Focusable implementation - propagate to input for IME cursor positioning + private _focused = false; + get focused(): boolean { + return this._focused; + } + set focused(value: boolean) { + this._focused = value; + this.input.focused = value; + } + + constructor(entryId: string, currentLabel: string | undefined) { + this.entryId = entryId; + this.input = new Input(); + if (currentLabel) { + this.input.setValue(currentLabel); + } + } + + invalidate(): void {} + + render(width: number): string[] { + const lines: string[] = []; + const indent = " "; + const availableWidth = width - indent.length; + lines.push(truncateToWidth(`${indent}${theme.fg("muted", "Label (empty to remove):")}`, width)); + lines.push(...this.input.render(availableWidth).map((line) => truncateToWidth(`${indent}${line}`, width))); + lines.push( + truncateToWidth( + `${indent}${keyHint("tui.select.confirm", "save")} ${keyHint("tui.select.cancel", "cancel")}`, + width, + ), + ); + return lines; + } + + handleInput(keyData: string): void { + const kb = getKeybindings(); + if (kb.matches(keyData, "tui.select.confirm")) { + const value = this.input.getValue().trim(); + this.onSubmit?.(this.entryId, value || undefined); + } else if (kb.matches(keyData, "tui.select.cancel")) { + this.onCancel?.(); + } else { + this.input.handleInput(keyData); + } + } +} + +/** + * Component that renders a session tree selector for navigation + */ +export class TreeSelectorComponent extends Container implements Focusable { + private treeList: TreeList; + private labelInput: LabelInput | null = null; + private labelInputContainer: Container; + private treeContainer: Container; + private onLabelChangeCallback?: (entryId: string, label: string | undefined) => void; + public onCopy?: (text: string | undefined) => void; + + // Focusable implementation - propagate to labelInput when active for IME cursor positioning + private _focused = false; + get focused(): boolean { + return this._focused; + } + set focused(value: boolean) { + this._focused = value; + // Propagate to labelInput when it's active + if (this.labelInput) { + this.labelInput.focused = value; + } + } + + constructor( + tree: SessionTreeNode[], + currentLeafId: string | null, + terminalHeight: number, + onSelect: (entryId: string) => void, + onCancel: () => void, + onLabelChange?: (entryId: string, label: string | undefined) => void, + initialSelectedId?: string, + initialFilterMode?: FilterMode, + ) { + super(); + + this.onLabelChangeCallback = onLabelChange; + const maxVisibleLines = Math.max(5, Math.floor(terminalHeight / 2)); + + this.treeList = new TreeList(tree, currentLeafId, maxVisibleLines, initialSelectedId, initialFilterMode); + this.treeList.onSelect = onSelect; + this.treeList.onCancel = onCancel; + this.treeList.onCopy = (text) => this.onCopy?.(text); + this.treeList.onLabelEdit = (entryId, currentLabel) => this.showLabelInput(entryId, currentLabel); + + this.treeContainer = new Container(); + this.treeContainer.addChild(this.treeList); + + this.labelInputContainer = new Container(); + + this.addChild(new Spacer(1)); + this.addChild(new DynamicBorder()); + this.addChild(new Text(theme.bold(" Session Tree"), 1, 0)); + this.addChild(new TreeHelp()); + this.addChild(new SearchLine(this.treeList)); + this.addChild(new DynamicBorder()); + this.addChild(new Spacer(1)); + this.addChild(this.treeContainer); + this.addChild(this.labelInputContainer); + this.addChild(new Spacer(1)); + this.addChild(new DynamicBorder()); + + if (tree.length === 0) { + setTimeout(() => onCancel(), 100); + } + } + + private showLabelInput(entryId: string, currentLabel: string | undefined): void { + this.labelInput = new LabelInput(entryId, currentLabel); + this.labelInput.onSubmit = (id, label) => { + this.treeList.updateNodeLabel(id, label); + this.onLabelChangeCallback?.(id, label); + this.hideLabelInput(); + }; + this.labelInput.onCancel = () => this.hideLabelInput(); + + // Propagate current focused state to the new labelInput + this.labelInput.focused = this._focused; + + this.treeContainer.clear(); + this.labelInputContainer.clear(); + this.labelInputContainer.addChild(this.labelInput); + } + + private hideLabelInput(): void { + this.labelInput = null; + this.labelInputContainer.clear(); + this.treeContainer.clear(); + this.treeContainer.addChild(this.treeList); + } + + handleInput(keyData: string): void { + if (this.labelInput) { + this.labelInput.handleInput(keyData); + } else { + this.treeList.handleInput(keyData); + } + } + + getTreeList(): TreeList { + return this.treeList; + } +} diff --git a/packages/coding-agent/src/modes/interactive/components/trust-selector.ts b/packages/coding-agent/src/modes/interactive/components/trust-selector.ts new file mode 100644 index 0000000000000000000000000000000000000000..00bc9e06732bd4cfb67eac9a449a9e7a4ff16e1b --- /dev/null +++ b/packages/coding-agent/src/modes/interactive/components/trust-selector.ts @@ -0,0 +1,134 @@ +import { Container, getKeybindings, Spacer, Text } from "@earendil-works/pi-tui"; +import { + getProjectTrustOptions, + type ProjectTrustOption, + type ProjectTrustStoreEntry, +} from "../../../core/trust-manager.ts"; +import { theme } from "../theme/theme.ts"; +import { DynamicBorder } from "./dynamic-border.ts"; +import { keyHint, rawKeyHint } from "./keybinding-hints.ts"; + +export type TrustSelection = Pick; + +export interface TrustSelectorOptions { + cwd: string; + savedDecision: ProjectTrustStoreEntry | null; + projectTrusted: boolean; + onSelect: (selection: TrustSelection) => void; + onCancel: () => void; +} + +function formatDecision(trustPath: string | undefined, decision: ProjectTrustStoreEntry | null): string { + if (decision === null) { + return "none"; + } + const label = decision.decision ? "trusted" : "untrusted"; + if (trustPath !== undefined && decision.path !== trustPath) { + return `${label} (inherited from ${decision.path})`; + } + return `${label} (${decision.path})`; +} + +export class TrustSelectorComponent extends Container { + private selectedIndex: number; + private readonly listContainer: Container; + private readonly trustOptions: ProjectTrustOption[]; + private readonly savedDecision: ProjectTrustStoreEntry | null; + private readonly onSelectCallback: (selection: TrustSelection) => void; + private readonly onCancelCallback: () => void; + + constructor(options: TrustSelectorOptions) { + super(); + + this.savedDecision = options.savedDecision; + this.trustOptions = getProjectTrustOptions(options.cwd); + this.selectedIndex = Math.max( + 0, + this.trustOptions.findIndex((option) => this.isSavedOption(option)), + ); + this.onSelectCallback = options.onSelect; + this.onCancelCallback = options.onCancel; + + this.addChild(new DynamicBorder()); + this.addChild(new Spacer(1)); + this.addChild(new Text(theme.fg("accent", theme.bold("Project trust")), 1, 0)); + this.addChild(new Text(theme.fg("muted", options.cwd), 1, 0)); + this.addChild(new Spacer(1)); + this.addChild( + new Text( + theme.fg( + "muted", + `Saved decision: ${formatDecision(this.trustOptions[0]?.savedPath, options.savedDecision)}`, + ), + 1, + 0, + ), + ); + this.addChild( + new Text(theme.fg("muted", `Current session: ${options.projectTrusted ? "trusted" : "untrusted"}`), 1, 0), + ); + this.addChild(new Spacer(1)); + + this.listContainer = new Container(); + this.addChild(this.listContainer); + this.addChild(new Spacer(1)); + this.addChild( + new Text( + rawKeyHint("↑↓", "navigate") + + " " + + keyHint("tui.select.confirm", "save") + + " " + + keyHint("tui.select.cancel", "cancel"), + 1, + 0, + ), + ); + this.addChild(new Spacer(1)); + this.addChild(new DynamicBorder()); + + this.updateList(); + } + + private isSavedOption(option: ProjectTrustOption): boolean { + return ( + option.savedPath !== undefined && + this.savedDecision?.decision === option.trusted && + this.savedDecision.path === option.savedPath + ); + } + + private updateList(): void { + this.listContainer.clear(); + for (let i = 0; i < this.trustOptions.length; i++) { + const option = this.trustOptions[i]; + if (!option) { + continue; + } + + const isSelected = i === this.selectedIndex; + const isCurrent = this.isSavedOption(option); + const currentMarker = isCurrent ? theme.fg("accent", "✓ ") : " "; + const prefix = isSelected ? theme.fg("accent", "→ ") : " "; + const label = isSelected ? theme.fg("accent", option.label) : theme.fg("text", option.label); + this.listContainer.addChild(new Text(`${prefix}${currentMarker}${label}`, 1, 0)); + } + } + + handleInput(keyData: string): void { + const kb = getKeybindings(); + if (kb.matches(keyData, "tui.select.up") || keyData === "k") { + this.selectedIndex = Math.max(0, this.selectedIndex - 1); + this.updateList(); + } else if (kb.matches(keyData, "tui.select.down") || keyData === "j") { + this.selectedIndex = Math.min(this.trustOptions.length - 1, this.selectedIndex + 1); + this.updateList(); + } else if (kb.matches(keyData, "tui.select.confirm") || keyData === "\n") { + const selected = this.trustOptions[this.selectedIndex]; + if (selected) { + this.onSelectCallback({ trusted: selected.trusted, updates: selected.updates }); + } + } else if (kb.matches(keyData, "tui.select.cancel")) { + this.onCancelCallback(); + } + } +} diff --git a/packages/coding-agent/src/modes/interactive/theme/dark.json b/packages/coding-agent/src/modes/interactive/theme/dark.json new file mode 100644 index 0000000000000000000000000000000000000000..3f4c698a71dc3dde81df7dff9b0b4a026f6e9a51 --- /dev/null +++ b/packages/coding-agent/src/modes/interactive/theme/dark.json @@ -0,0 +1,91 @@ +{ + "$schema": "https://raw.githubusercontent.com/earendil-works/pi/main/packages/coding-agent/src/modes/interactive/theme/theme-schema.json", + "name": "dark", + "vars": { + "cyan": "#00d7ff", + "blue": "#5f87ff", + "green": "#b5bd68", + "red": "#cc6666", + "yellow": "#ffff00", + "text": "#d4d4d4", + "gray": "#808080", + "dimGray": "#666666", + "darkGray": "#505050", + "accent": "#8abeb7", + "selectedBg": "#3a3a4a", + "userMsgBg": "#343541", + "toolPendingBg": "#282832", + "toolSuccessBg": "#283228", + "toolErrorBg": "#3c2828", + "customMsgBg": "#2d2838" + }, + "colors": { + "accent": "accent", + "border": "blue", + "borderAccent": "cyan", + "borderMuted": "darkGray", + "success": "green", + "error": "red", + "warning": "yellow", + "muted": "gray", + "dim": "dimGray", + "text": "text", + "thinkingText": "gray", + + "selectedBg": "selectedBg", + "scrollbarTrack": "darkGray", + "scrollbarThumb": "text", + "searchMatchBg": "selectedBg", + "searchMatchText": "text", + "userMessageBg": "userMsgBg", + "userMessageText": "text", + "customMessageBg": "customMsgBg", + "customMessageText": "text", + "customMessageLabel": "#9575cd", + "toolPendingBg": "toolPendingBg", + "toolSuccessBg": "toolSuccessBg", + "toolErrorBg": "toolErrorBg", + "toolTitle": "text", + "toolOutput": "gray", + + "mdHeading": "#f0c674", + "mdLink": "#81a2be", + "mdLinkUrl": "dimGray", + "mdCode": "accent", + "mdCodeBlock": "green", + "mdCodeBlockBorder": "gray", + "mdQuote": "gray", + "mdQuoteBorder": "gray", + "mdHr": "gray", + "mdListBullet": "accent", + + "toolDiffAdded": "green", + "toolDiffRemoved": "red", + "toolDiffContext": "gray", + + "syntaxComment": "#6A9955", + "syntaxKeyword": "#569CD6", + "syntaxFunction": "#DCDCAA", + "syntaxVariable": "#9CDCFE", + "syntaxString": "#CE9178", + "syntaxNumber": "#B5CEA8", + "syntaxType": "#4EC9B0", + "syntaxOperator": "#D4D4D4", + "syntaxPunctuation": "#D4D4D4", + + "thinkingOff": "darkGray", + "thinkingMinimal": "#6e6e6e", + "thinkingLow": "#5f87af", + "thinkingMedium": "#81a2be", + "thinkingHigh": "#b294bb", + "thinkingXhigh": "#d183e8", + "thinkingMax": "#ff5fff", + + "bashMode": "green" + }, + "export": { + "pageBg": "#18181e", + "cardBg": "#1e1e24", + "infoBg": "#3c3728" + } +} diff --git a/packages/coding-agent/src/modes/interactive/theme/light.json b/packages/coding-agent/src/modes/interactive/theme/light.json new file mode 100644 index 0000000000000000000000000000000000000000..20f67835a911af7c33feaab76f8da0657919e085 --- /dev/null +++ b/packages/coding-agent/src/modes/interactive/theme/light.json @@ -0,0 +1,90 @@ +{ + "$schema": "https://raw.githubusercontent.com/earendil-works/pi/main/packages/coding-agent/src/modes/interactive/theme/theme-schema.json", + "name": "light", + "vars": { + "teal": "#5a8080", + "blue": "#547da7", + "green": "#588458", + "red": "#aa5555", + "yellow": "#9a7326", + "text": "#1f2328", + "mediumGray": "#6c6c6c", + "dimGray": "#767676", + "lightGray": "#b0b0b0", + "selectedBg": "#d0d0e0", + "userMsgBg": "#e8e8e8", + "toolPendingBg": "#e8e8f0", + "toolSuccessBg": "#e8f0e8", + "toolErrorBg": "#f0e8e8", + "customMsgBg": "#ede7f6" + }, + "colors": { + "accent": "teal", + "border": "blue", + "borderAccent": "teal", + "borderMuted": "lightGray", + "success": "green", + "error": "red", + "warning": "yellow", + "muted": "mediumGray", + "dim": "dimGray", + "text": "text", + "thinkingText": "mediumGray", + + "selectedBg": "selectedBg", + "scrollbarTrack": "lightGray", + "scrollbarThumb": "text", + "searchMatchBg": "selectedBg", + "searchMatchText": "text", + "userMessageBg": "userMsgBg", + "userMessageText": "text", + "customMessageBg": "customMsgBg", + "customMessageText": "text", + "customMessageLabel": "#7e57c2", + "toolPendingBg": "toolPendingBg", + "toolSuccessBg": "toolSuccessBg", + "toolErrorBg": "toolErrorBg", + "toolTitle": "text", + "toolOutput": "mediumGray", + + "mdHeading": "yellow", + "mdLink": "blue", + "mdLinkUrl": "dimGray", + "mdCode": "teal", + "mdCodeBlock": "green", + "mdCodeBlockBorder": "mediumGray", + "mdQuote": "mediumGray", + "mdQuoteBorder": "mediumGray", + "mdHr": "mediumGray", + "mdListBullet": "green", + + "toolDiffAdded": "green", + "toolDiffRemoved": "red", + "toolDiffContext": "mediumGray", + + "syntaxComment": "#008000", + "syntaxKeyword": "#0000FF", + "syntaxFunction": "#795E26", + "syntaxVariable": "#001080", + "syntaxString": "#A31515", + "syntaxNumber": "#098658", + "syntaxType": "#267F99", + "syntaxOperator": "#000000", + "syntaxPunctuation": "#000000", + + "thinkingOff": "lightGray", + "thinkingMinimal": "#767676", + "thinkingLow": "blue", + "thinkingMedium": "teal", + "thinkingHigh": "#875f87", + "thinkingXhigh": "#8b008b", + "thinkingMax": "#af005f", + + "bashMode": "green" + }, + "export": { + "pageBg": "#f8f8f8", + "cardBg": "#ffffff", + "infoBg": "#fffae6" + } +} diff --git a/packages/coding-agent/src/modes/interactive/theme/theme-controller.ts b/packages/coding-agent/src/modes/interactive/theme/theme-controller.ts new file mode 100644 index 0000000000000000000000000000000000000000..b5cf06b481263402d0348655fe1034a81d5f3f9b --- /dev/null +++ b/packages/coding-agent/src/modes/interactive/theme/theme-controller.ts @@ -0,0 +1,172 @@ +import type { TUI } from "@earendil-works/pi-tui"; +import type { SettingsManager } from "../../../core/settings-manager.ts"; +import { + detectTerminalBackgroundFromEnv, + detectTerminalBackgroundTheme, + detectTerminalThemeForAuto, + initTheme, + parseAutoThemeSetting, + resolveThemeSetting, + setTheme, + setThemeInstance, + type TerminalTheme, + type Theme, +} from "./theme.ts"; + +type ThemeResult = { success: boolean; error?: string }; + +export class InteractiveThemeController { + private readonly ui: TUI; + private readonly getSettingsManager: () => SettingsManager; + private readonly showError: (message: string) => void; + private readonly onChanged: () => void; + private currentThemeSetting: string | undefined; + private terminalTheme: TerminalTheme = detectTerminalBackgroundFromEnv().theme; + private activeThemeName: string | undefined; + private autoSyncEnabled = false; + private terminalColorSchemeUnsubscribe: (() => void) | undefined; + + constructor( + ui: TUI, + options: { + getSettingsManager: () => SettingsManager; + showError: (message: string) => void; + onChanged: () => void; + initialThemeSetting?: string; + }, + ) { + this.ui = ui; + this.getSettingsManager = options.getSettingsManager; + this.showError = options.showError; + this.onChanged = options.onChanged; + this.currentThemeSetting = options.initialThemeSetting; + this.activeThemeName = resolveThemeSetting( + this.currentThemeSetting ?? this.getSettingsManager().getThemeSetting(), + this.terminalTheme, + ); + initTheme(this.activeThemeName, true); + this.bindTerminalColorSchemeListener(); + } + + rebindTui(): void { + this.terminalColorSchemeUnsubscribe?.(); + this.bindTerminalColorSchemeListener(); + this.ui.setTerminalColorSchemeNotifications(this.autoSyncEnabled); + } + + async applyFromSettings(): Promise { + const settingsManager = this.getSettingsManager(); + const themeSetting = this.currentThemeSetting ?? settingsManager.getThemeSetting(); + const autoTheme = parseAutoThemeSetting(themeSetting); + if (autoTheme) { + this.terminalTheme = await detectTerminalThemeForAuto({ ui: this.ui, timeoutMs: 100 }); + this.setAutoSync(true); + this.applyThemeName(this.terminalTheme === "light" ? autoTheme.lightTheme : autoTheme.darkTheme, true); + return; + } + + this.setAutoSync(false); + if (themeSetting !== undefined) { + this.applyThemeName(themeSetting, true); + return; + } + + const detection = await detectTerminalBackgroundTheme({ ui: this.ui, timeoutMs: 100 }); + this.terminalTheme = detection.theme; + if (!this.applyThemeName(detection.theme).success) return; + if (detection.confidence === "high") { + settingsManager.setTheme(detection.theme); + await settingsManager.flush(); + } + } + + getThemeSelection(): string | undefined { + return this.currentThemeSetting ?? this.getSettingsManager().getThemeSetting() ?? this.activeThemeName; + } + + setThemeName(themeName: string, showError = false): ThemeResult { + this.setAutoSync(false); + const result = this.applyThemeName(themeName, showError); + if (result.success) { + this.currentThemeSetting = themeName; + } + return result; + } + + async setThemeSetting(themeSetting: string): Promise { + this.currentThemeSetting = themeSetting; + await this.applyFromSettings(); + } + + setThemeInstance(themeInstance: Theme): ThemeResult { + this.setAutoSync(false); + setThemeInstance(themeInstance); + this.activeThemeName = ""; + this.notifyChanged(); + return { success: true }; + } + + preview(themeSettingOrName: string): void { + const themeName = resolveThemeSetting(themeSettingOrName, this.terminalTheme) ?? this.activeThemeName; + if (!themeName) return; + if (setTheme(themeName, true).success) { + this.ui.invalidate(); + this.ui.requestRender(); + } + } + + disableAutoSync(): void { + this.setAutoSync(false); + } + + dispose(): void { + this.setAutoSync(false); + this.terminalColorSchemeUnsubscribe?.(); + this.terminalColorSchemeUnsubscribe = undefined; + } + + getTerminalTheme(): TerminalTheme { + return this.terminalTheme; + } + + private applyThemeName(themeName: string, showError = false): ThemeResult { + const result = setTheme(themeName, true); + this.activeThemeName = result.success ? themeName : "dark"; + this.notifyChanged(); + if (!result.success && showError) { + this.showError(`Failed to load theme "${themeName}": ${result.error}\nFell back to dark theme.`); + } + return result; + } + + private notifyChanged(): void { + this.ui.invalidate(); + this.onChanged(); + } + + private setAutoSync(enabled: boolean): void { + if (this.autoSyncEnabled === enabled) return; + this.autoSyncEnabled = enabled; + this.ui.setTerminalColorSchemeNotifications(enabled); + } + + private bindTerminalColorSchemeListener(): void { + this.terminalColorSchemeUnsubscribe = this.ui.onTerminalColorSchemeChange((terminalTheme) => + this.applyTerminalTheme(terminalTheme), + ); + } + + private applyTerminalTheme(terminalTheme: TerminalTheme): void { + if (!this.autoSyncEnabled) return; + this.terminalTheme = terminalTheme; + const autoTheme = parseAutoThemeSetting(this.currentThemeSetting ?? this.getSettingsManager().getThemeSetting()); + if (!autoTheme) { + this.setAutoSync(false); + return; + } + const themeName = terminalTheme === "light" ? autoTheme.lightTheme : autoTheme.darkTheme; + if (themeName !== this.activeThemeName) { + this.applyThemeName(themeName); + } + } +} diff --git a/packages/coding-agent/src/modes/interactive/theme/theme-json.ts b/packages/coding-agent/src/modes/interactive/theme/theme-json.ts new file mode 100644 index 0000000000000000000000000000000000000000..256cd4ea6b9a871d55d7959b66004bc16e872191 --- /dev/null +++ b/packages/coding-agent/src/modes/interactive/theme/theme-json.ts @@ -0,0 +1,146 @@ +/** + * Theme JSON validation, kept out of `theme.ts` on purpose. + * + * Validating user-authored theme files needs typebox, which costs ~17 MB of module graph to import. + * Palette lookup does not, so a presentation that only uses built-in themes should never pay for it. + * `interactive-mode.ts` installs this validator; anything that does not simply skips validation, as + * built-in themes already do. + */ + +import { type Static, Type } from "typebox"; +import { Compile } from "typebox/compile"; + +const ColorValueSchema = Type.Union([ + Type.String(), // hex "#ff0000", var ref "primary", or empty "" + Type.Integer({ minimum: 0, maximum: 255 }), // 256-color index +]); + +const ThemeJsonSchema = Type.Object({ + $schema: Type.Optional(Type.String()), + name: Type.String(), + vars: Type.Optional(Type.Record(Type.String(), ColorValueSchema)), + colors: Type.Object({ + // Core UI (11 colors) + accent: ColorValueSchema, + border: ColorValueSchema, + borderAccent: ColorValueSchema, + borderMuted: ColorValueSchema, + success: ColorValueSchema, + error: ColorValueSchema, + warning: ColorValueSchema, + muted: ColorValueSchema, + dim: ColorValueSchema, + text: ColorValueSchema, + thinkingText: ColorValueSchema, + // Scrollbar (2 optional colors) + scrollbarTrack: Type.Optional(ColorValueSchema), + scrollbarThumb: Type.Optional(ColorValueSchema), + // Backgrounds & Content Text (11 required, 2 optional) + selectedBg: ColorValueSchema, + searchMatchBg: Type.Optional(ColorValueSchema), + searchMatchText: Type.Optional(ColorValueSchema), + userMessageBg: ColorValueSchema, + userMessageText: ColorValueSchema, + customMessageBg: ColorValueSchema, + customMessageText: ColorValueSchema, + customMessageLabel: ColorValueSchema, + toolPendingBg: ColorValueSchema, + toolSuccessBg: ColorValueSchema, + toolErrorBg: ColorValueSchema, + toolTitle: ColorValueSchema, + toolOutput: ColorValueSchema, + // Markdown (10 colors) + mdHeading: ColorValueSchema, + mdLink: ColorValueSchema, + mdLinkUrl: ColorValueSchema, + mdCode: ColorValueSchema, + mdCodeBlock: ColorValueSchema, + mdCodeBlockBorder: ColorValueSchema, + mdQuote: ColorValueSchema, + mdQuoteBorder: ColorValueSchema, + mdHr: ColorValueSchema, + mdListBullet: ColorValueSchema, + // Tool Diffs (3 colors) + toolDiffAdded: ColorValueSchema, + toolDiffRemoved: ColorValueSchema, + toolDiffContext: ColorValueSchema, + // Syntax Highlighting (9 colors) + syntaxComment: ColorValueSchema, + syntaxKeyword: ColorValueSchema, + syntaxFunction: ColorValueSchema, + syntaxVariable: ColorValueSchema, + syntaxString: ColorValueSchema, + syntaxNumber: ColorValueSchema, + syntaxType: ColorValueSchema, + syntaxOperator: ColorValueSchema, + syntaxPunctuation: ColorValueSchema, + // Thinking Level Borders (6 colors) + thinkingOff: ColorValueSchema, + thinkingMinimal: ColorValueSchema, + thinkingLow: ColorValueSchema, + thinkingMedium: ColorValueSchema, + thinkingHigh: ColorValueSchema, + thinkingXhigh: ColorValueSchema, + thinkingMax: Type.Optional(ColorValueSchema), + // Bash Mode (1 color) + bashMode: ColorValueSchema, + }), + export: Type.Optional( + Type.Object({ + pageBg: Type.Optional(ColorValueSchema), + cardBg: Type.Optional(ColorValueSchema), + infoBg: Type.Optional(ColorValueSchema), + }), + ), +}); + +const compiledThemeSchema = Compile(ThemeJsonSchema); + +export type ThemeColorValue = Static; +export type ValidatedThemeJson = Static; + +/** Validate one theme document, throwing a message that names the offending tokens. */ +export function validateThemeJson(label: string, json: unknown): ValidatedThemeJson { + if (!compiledThemeSchema.Check(json)) { + const errors = Array.from(compiledThemeSchema.Errors(json)); + const missingColors = new Set(); + const otherErrors: string[] = []; + + for (const error of errors) { + if (error.keyword === "required" && error.instancePath === "/colors") { + const requiredProperties = (error.params as { requiredProperties?: string[] }).requiredProperties; + for (const requiredProperty of requiredProperties ?? []) { + missingColors.add(requiredProperty); + } + continue; + } + + const path = error.instancePath || "/"; + otherErrors.push(` - ${path}: ${error.message}`); + } + + let errorMessage = `Invalid theme "${label}":\n`; + if (missingColors.size > 0) { + errorMessage += "\nMissing required color tokens:\n"; + errorMessage += Array.from(missingColors) + .sort() + .map((color) => ` - ${color}`) + .join("\n"); + errorMessage += '\n\nPlease add these colors to your theme\'s "colors" object.'; + errorMessage += "\nSee the built-in themes (dark.json, light.json) for reference values."; + } + if (otherErrors.length > 0) { + errorMessage += `\n\nOther errors:\n${otherErrors.join("\n")}`; + } + + throw new Error(errorMessage); + } + + const themeJson = json as ValidatedThemeJson; + if (themeJson.name.includes("/")) { + throw new Error( + `Invalid theme name "${themeJson.name}": theme names cannot contain "/" because it is reserved for automatic light/dark theme settings.`, + ); + } + return themeJson; +} diff --git a/packages/coding-agent/src/modes/interactive/theme/theme-schema.json b/packages/coding-agent/src/modes/interactive/theme/theme-schema.json new file mode 100644 index 0000000000000000000000000000000000000000..323bca8f5d92d99e6b44d427425d0d1578015aeb --- /dev/null +++ b/packages/coding-agent/src/modes/interactive/theme/theme-schema.json @@ -0,0 +1,356 @@ +{ + "$schema": "http://json-schema.org/draft-07/schema#", + "title": "Pi Coding Agent Theme", + "description": "Theme schema for Pi coding agent", + "type": "object", + "required": ["name", "colors"], + "properties": { + "$schema": { + "type": "string", + "description": "JSON schema reference" + }, + "name": { + "type": "string", + "pattern": "^[^/]+$", + "description": "Theme name. Must not contain '/' because it is reserved for automatic light/dark theme settings." + }, + "vars": { + "type": "object", + "description": "Reusable color variables", + "additionalProperties": { + "oneOf": [ + { + "type": "string", + "description": "Hex color (#RRGGBB), variable reference, or empty string for terminal default" + }, + { + "type": "integer", + "minimum": 0, + "maximum": 255, + "description": "256-color palette index (0-255)" + } + ] + } + }, + "colors": { + "type": "object", + "description": "Theme color definitions (scrollbar, thinkingMax, and search highlight colors are optional and use compatible fallbacks)", + "required": [ + "accent", + "border", + "borderAccent", + "borderMuted", + "success", + "error", + "warning", + "muted", + "dim", + "text", + "thinkingText", + "selectedBg", + "userMessageBg", + "userMessageText", + "customMessageBg", + "customMessageText", + "customMessageLabel", + "toolPendingBg", + "toolSuccessBg", + "toolErrorBg", + "toolTitle", + "toolOutput", + "mdHeading", + "mdLink", + "mdLinkUrl", + "mdCode", + "mdCodeBlock", + "mdCodeBlockBorder", + "mdQuote", + "mdQuoteBorder", + "mdHr", + "mdListBullet", + "toolDiffAdded", + "toolDiffRemoved", + "toolDiffContext", + "syntaxComment", + "syntaxKeyword", + "syntaxFunction", + "syntaxVariable", + "syntaxString", + "syntaxNumber", + "syntaxType", + "syntaxOperator", + "syntaxPunctuation", + "thinkingOff", + "thinkingMinimal", + "thinkingLow", + "thinkingMedium", + "thinkingHigh", + "thinkingXhigh", + "bashMode" + ], + "properties": { + "accent": { + "$ref": "#/$defs/colorValue", + "description": "Primary accent color (logo, selected items, cursor)" + }, + "border": { + "$ref": "#/$defs/colorValue", + "description": "Normal borders" + }, + "borderAccent": { + "$ref": "#/$defs/colorValue", + "description": "Highlighted borders" + }, + "borderMuted": { + "$ref": "#/$defs/colorValue", + "description": "Subtle borders" + }, + "success": { + "$ref": "#/$defs/colorValue", + "description": "Success states" + }, + "error": { + "$ref": "#/$defs/colorValue", + "description": "Error states" + }, + "warning": { + "$ref": "#/$defs/colorValue", + "description": "Warning states" + }, + "muted": { + "$ref": "#/$defs/colorValue", + "description": "Secondary/dimmed text" + }, + "dim": { + "$ref": "#/$defs/colorValue", + "description": "Very dimmed text (more subtle than muted)" + }, + "text": { + "$ref": "#/$defs/colorValue", + "description": "Default text color (usually empty string)" + }, + "thinkingText": { + "$ref": "#/$defs/colorValue", + "description": "Thinking block text color" + }, + "selectedBg": { + "$ref": "#/$defs/colorValue", + "description": "Selected item background" + }, + "scrollbarTrack": { + "$ref": "#/$defs/colorValue", + "description": "Fullscreen scrollbar track foreground (falls back to muted when omitted)" + }, + "scrollbarThumb": { + "$ref": "#/$defs/colorValue", + "description": "Fullscreen scrollbar thumb foreground (falls back to text when omitted)" + }, + "searchMatchBg": { + "$ref": "#/$defs/colorValue", + "description": "Transcript search match background and current-match text (falls back to selectedBg when omitted)" + }, + "searchMatchText": { + "$ref": "#/$defs/colorValue", + "description": "Transcript search match text and current-match background (falls back to text when omitted)" + }, + "userMessageBg": { + "$ref": "#/$defs/colorValue", + "description": "User message background" + }, + "userMessageText": { + "$ref": "#/$defs/colorValue", + "description": "User message text color" + }, + "customMessageBg": { + "$ref": "#/$defs/colorValue", + "description": "Custom message background (hook-injected messages)" + }, + "customMessageText": { + "$ref": "#/$defs/colorValue", + "description": "Custom message text color" + }, + "customMessageLabel": { + "$ref": "#/$defs/colorValue", + "description": "Custom message type label color" + }, + "toolPendingBg": { + "$ref": "#/$defs/colorValue", + "description": "Tool execution box (pending state)" + }, + "toolSuccessBg": { + "$ref": "#/$defs/colorValue", + "description": "Tool execution box (success state)" + }, + "toolErrorBg": { + "$ref": "#/$defs/colorValue", + "description": "Tool execution box (error state)" + }, + "toolTitle": { + "$ref": "#/$defs/colorValue", + "description": "Tool execution box title color" + }, + "toolOutput": { + "$ref": "#/$defs/colorValue", + "description": "Tool execution box output text color" + }, + "mdHeading": { + "$ref": "#/$defs/colorValue", + "description": "Markdown heading text" + }, + "mdLink": { + "$ref": "#/$defs/colorValue", + "description": "Markdown link text" + }, + "mdLinkUrl": { + "$ref": "#/$defs/colorValue", + "description": "Markdown link URL" + }, + "mdCode": { + "$ref": "#/$defs/colorValue", + "description": "Markdown inline code" + }, + "mdCodeBlock": { + "$ref": "#/$defs/colorValue", + "description": "Markdown code block content" + }, + "mdCodeBlockBorder": { + "$ref": "#/$defs/colorValue", + "description": "Markdown code block fences" + }, + "mdQuote": { + "$ref": "#/$defs/colorValue", + "description": "Markdown blockquote text" + }, + "mdQuoteBorder": { + "$ref": "#/$defs/colorValue", + "description": "Markdown blockquote border" + }, + "mdHr": { + "$ref": "#/$defs/colorValue", + "description": "Markdown horizontal rule" + }, + "mdListBullet": { + "$ref": "#/$defs/colorValue", + "description": "Markdown list bullets/numbers" + }, + "toolDiffAdded": { + "$ref": "#/$defs/colorValue", + "description": "Added lines in tool diffs" + }, + "toolDiffRemoved": { + "$ref": "#/$defs/colorValue", + "description": "Removed lines in tool diffs" + }, + "toolDiffContext": { + "$ref": "#/$defs/colorValue", + "description": "Context lines in tool diffs" + }, + "syntaxComment": { + "$ref": "#/$defs/colorValue", + "description": "Syntax highlighting: comments" + }, + "syntaxKeyword": { + "$ref": "#/$defs/colorValue", + "description": "Syntax highlighting: keywords" + }, + "syntaxFunction": { + "$ref": "#/$defs/colorValue", + "description": "Syntax highlighting: function names" + }, + "syntaxVariable": { + "$ref": "#/$defs/colorValue", + "description": "Syntax highlighting: variable names" + }, + "syntaxString": { + "$ref": "#/$defs/colorValue", + "description": "Syntax highlighting: string literals" + }, + "syntaxNumber": { + "$ref": "#/$defs/colorValue", + "description": "Syntax highlighting: number literals" + }, + "syntaxType": { + "$ref": "#/$defs/colorValue", + "description": "Syntax highlighting: type names" + }, + "syntaxOperator": { + "$ref": "#/$defs/colorValue", + "description": "Syntax highlighting: operators" + }, + "syntaxPunctuation": { + "$ref": "#/$defs/colorValue", + "description": "Syntax highlighting: punctuation" + }, + "thinkingOff": { + "$ref": "#/$defs/colorValue", + "description": "Thinking level border: off" + }, + "thinkingMinimal": { + "$ref": "#/$defs/colorValue", + "description": "Thinking level border: minimal" + }, + "thinkingLow": { + "$ref": "#/$defs/colorValue", + "description": "Thinking level border: low" + }, + "thinkingMedium": { + "$ref": "#/$defs/colorValue", + "description": "Thinking level border: medium" + }, + "thinkingHigh": { + "$ref": "#/$defs/colorValue", + "description": "Thinking level border: high" + }, + "thinkingXhigh": { + "$ref": "#/$defs/colorValue", + "description": "Thinking level border: xhigh" + }, + "thinkingMax": { + "$ref": "#/$defs/colorValue", + "description": "Thinking level border: max (falls back to thinkingXhigh when omitted)" + }, + "bashMode": { + "$ref": "#/$defs/colorValue", + "description": "Editor border color in bash mode" + } + }, + "additionalProperties": false + }, + "export": { + "type": "object", + "description": "Optional colors for HTML export (defaults derived from userMessageBg if not specified)", + "properties": { + "pageBg": { + "$ref": "#/$defs/colorValue", + "description": "Page background color" + }, + "cardBg": { + "$ref": "#/$defs/colorValue", + "description": "Card/container background color" + }, + "infoBg": { + "$ref": "#/$defs/colorValue", + "description": "Info sections background (system prompt, notices)" + } + }, + "additionalProperties": false + } + }, + "additionalProperties": false, + "$defs": { + "colorValue": { + "oneOf": [ + { + "type": "string", + "description": "Hex color (#RRGGBB), variable reference, or empty string for terminal default" + }, + { + "type": "integer", + "minimum": 0, + "maximum": 255, + "description": "256-color palette index (0-255)" + } + ] + } + } +} diff --git a/packages/coding-agent/src/modes/interactive/theme/theme.ts b/packages/coding-agent/src/modes/interactive/theme/theme.ts new file mode 100644 index 0000000000000000000000000000000000000000..f000a5d7a7782c2ecb7adff07832c55da60e5db3 --- /dev/null +++ b/packages/coding-agent/src/modes/interactive/theme/theme.ts @@ -0,0 +1,1234 @@ +import * as fs from "node:fs"; +import * as path from "node:path"; +import type { ThinkingLevel } from "@earendil-works/pi-agent-core"; +import { + type EditorTheme, + getCapabilities, + type MarkdownTheme, + type RgbColor, + type SelectListTheme, + type SettingsListTheme, +} from "@earendil-works/pi-tui"; +import chalk from "chalk"; +import { getCustomThemesDir, getThemesDir } from "../../../config.ts"; +import type { SourceInfo } from "../../../core/source-info.ts"; +import { closeWatcher, watchWithErrorHandler } from "../../../utils/fs-watch.ts"; +import { highlight, supportsLanguage } from "../../../utils/syntax-highlight.ts"; +import { stripBom } from "../../../utils/text.ts"; + +// ============================================================================ +// Types & Schema +// ============================================================================ + +/** The schema that validates this shape lives in `theme-json.ts`; importing the type is free. */ +import type { ThemeColorValue as ColorValue, ValidatedThemeJson as ThemeJson } from "./theme-json.ts"; + +export type { ValidatedThemeJson as ThemeJson } from "./theme-json.ts"; + +export type ThemeJsonValidator = (label: string, json: unknown) => ThemeJson; + +let themeJsonValidator: ThemeJsonValidator | undefined; + +/** + * Install full theme validation. Without it, documents are accepted as-is, which is what built-in + * themes already do: validating user-authored JSON needs typebox, and a presentation that only uses + * built-in themes should not pay ~17 MB of module graph for it. + */ +export function setThemeJsonValidator(validator: ThemeJsonValidator): void { + themeJsonValidator = validator; +} + +export type ThemeColor = + | "accent" + | "border" + | "borderAccent" + | "borderMuted" + | "success" + | "error" + | "warning" + | "muted" + | "dim" + | "text" + | "thinkingText" + | "scrollbarTrack" + | "scrollbarThumb" + | "searchMatchText" + | "userMessageText" + | "customMessageText" + | "customMessageLabel" + | "toolTitle" + | "toolOutput" + | "mdHeading" + | "mdLink" + | "mdLinkUrl" + | "mdCode" + | "mdCodeBlock" + | "mdCodeBlockBorder" + | "mdQuote" + | "mdQuoteBorder" + | "mdHr" + | "mdListBullet" + | "toolDiffAdded" + | "toolDiffRemoved" + | "toolDiffContext" + | "syntaxComment" + | "syntaxKeyword" + | "syntaxFunction" + | "syntaxVariable" + | "syntaxString" + | "syntaxNumber" + | "syntaxType" + | "syntaxOperator" + | "syntaxPunctuation" + | "thinkingOff" + | "thinkingMinimal" + | "thinkingLow" + | "thinkingMedium" + | "thinkingHigh" + | "thinkingXhigh" + | "thinkingMax" + | "bashMode"; + +export type ThemeBg = + | "selectedBg" + | "searchMatchBg" + | "userMessageBg" + | "customMessageBg" + | "toolPendingBg" + | "toolSuccessBg" + | "toolErrorBg"; + +type OptionalThemeColor = "scrollbarTrack" | "scrollbarThumb" | "thinkingMax" | "searchMatchText"; +type OptionalThemeBg = "searchMatchBg"; + +type ColorMode = "truecolor" | "256color"; + +// ============================================================================ +// Color Utilities +// ============================================================================ + +function hexToRgb(hex: string): { r: number; g: number; b: number } { + const cleaned = hex.replace("#", ""); + if (cleaned.length !== 6) { + throw new Error(`Invalid hex color: ${hex}`); + } + const r = parseInt(cleaned.substring(0, 2), 16); + const g = parseInt(cleaned.substring(2, 4), 16); + const b = parseInt(cleaned.substring(4, 6), 16); + if (Number.isNaN(r) || Number.isNaN(g) || Number.isNaN(b)) { + throw new Error(`Invalid hex color: ${hex}`); + } + return { r, g, b }; +} + +// The 6x6x6 color cube channel values (indices 0-5) +const CUBE_VALUES = [0, 95, 135, 175, 215, 255]; + +// Grayscale ramp values (indices 232-255, 24 grays from 8 to 238) +const GRAY_VALUES = Array.from({ length: 24 }, (_, i) => 8 + i * 10); + +function findClosestCubeIndex(value: number): number { + let minDist = Infinity; + let minIdx = 0; + for (let i = 0; i < CUBE_VALUES.length; i++) { + const dist = Math.abs(value - CUBE_VALUES[i]); + if (dist < minDist) { + minDist = dist; + minIdx = i; + } + } + return minIdx; +} + +function findClosestGrayIndex(gray: number): number { + let minDist = Infinity; + let minIdx = 0; + for (let i = 0; i < GRAY_VALUES.length; i++) { + const dist = Math.abs(gray - GRAY_VALUES[i]); + if (dist < minDist) { + minDist = dist; + minIdx = i; + } + } + return minIdx; +} + +function colorDistance(r1: number, g1: number, b1: number, r2: number, g2: number, b2: number): number { + // Weighted Euclidean distance (human eye is more sensitive to green) + const dr = r1 - r2; + const dg = g1 - g2; + const db = b1 - b2; + return dr * dr * 0.299 + dg * dg * 0.587 + db * db * 0.114; +} + +function rgbTo256(r: number, g: number, b: number): number { + // Find closest color in the 6x6x6 cube + const rIdx = findClosestCubeIndex(r); + const gIdx = findClosestCubeIndex(g); + const bIdx = findClosestCubeIndex(b); + const cubeR = CUBE_VALUES[rIdx]; + const cubeG = CUBE_VALUES[gIdx]; + const cubeB = CUBE_VALUES[bIdx]; + const cubeIndex = 16 + 36 * rIdx + 6 * gIdx + bIdx; + const cubeDist = colorDistance(r, g, b, cubeR, cubeG, cubeB); + + // Find closest grayscale + const gray = Math.round(0.299 * r + 0.587 * g + 0.114 * b); + const grayIdx = findClosestGrayIndex(gray); + const grayValue = GRAY_VALUES[grayIdx]; + const grayIndex = 232 + grayIdx; + const grayDist = colorDistance(r, g, b, grayValue, grayValue, grayValue); + + // Check if color has noticeable saturation (hue matters) + // If max-min spread is significant, prefer cube to preserve tint + const maxC = Math.max(r, g, b); + const minC = Math.min(r, g, b); + const spread = maxC - minC; + + // Only consider grayscale if color is nearly neutral (spread < 10) + // AND grayscale is actually closer + if (spread < 10 && grayDist < cubeDist) { + return grayIndex; + } + + return cubeIndex; +} + +function hexTo256(hex: string): number { + const { r, g, b } = hexToRgb(hex); + return rgbTo256(r, g, b); +} + +function fgAnsi(color: string | number, mode: ColorMode): string { + if (color === "") return "\x1b[39m"; + if (typeof color === "number") return `\x1b[38;5;${color}m`; + if (color.startsWith("#")) { + if (mode === "truecolor") { + const { r, g, b } = hexToRgb(color); + return `\x1b[38;2;${r};${g};${b}m`; + } else { + const index = hexTo256(color); + return `\x1b[38;5;${index}m`; + } + } + throw new Error(`Invalid color value: ${color}`); +} + +function bgAnsi(color: string | number, mode: ColorMode): string { + if (color === "") return "\x1b[49m"; + if (typeof color === "number") return `\x1b[48;5;${color}m`; + if (color.startsWith("#")) { + if (mode === "truecolor") { + const { r, g, b } = hexToRgb(color); + return `\x1b[48;2;${r};${g};${b}m`; + } else { + const index = hexTo256(color); + return `\x1b[48;5;${index}m`; + } + } + throw new Error(`Invalid color value: ${color}`); +} + +function resolveVarRefs( + value: ColorValue, + vars: Record, + visited = new Set(), +): string | number { + if (typeof value === "number" || value === "" || value.startsWith("#")) { + return value; + } + if (visited.has(value)) { + throw new Error(`Circular variable reference detected: ${value}`); + } + if (!(value in vars)) { + throw new Error(`Variable reference not found: ${value}`); + } + visited.add(value); + return resolveVarRefs(vars[value], vars, visited); +} + +function resolveThemeColors>( + colors: T, + vars: Record = {}, +): Record { + const resolved: Record = {}; + for (const [key, value] of Object.entries(colors)) { + resolved[key] = resolveVarRefs(value, vars); + } + return resolved as Record; +} + +function withThemeColorFallbacks(colors: ThemeJson["colors"]): ThemeJson["colors"] & { + scrollbarTrack: ColorValue; + scrollbarThumb: ColorValue; + thinkingMax: ColorValue; + searchMatchBg: ColorValue; + searchMatchText: ColorValue; +} { + return { + ...colors, + scrollbarTrack: colors.scrollbarTrack ?? colors.muted, + scrollbarThumb: colors.scrollbarThumb ?? colors.text, + thinkingMax: colors.thinkingMax ?? colors.thinkingXhigh, + searchMatchBg: colors.searchMatchBg ?? colors.selectedBg, + searchMatchText: colors.searchMatchText ?? colors.text, + }; +} + +// ============================================================================ +// Theme Class +// ============================================================================ + +export class Theme { + readonly name?: string; + readonly sourcePath?: string; + sourceInfo?: SourceInfo; + private fgColors: Map; + private bgColors: Map; + private mode: ColorMode; + + constructor( + fgColors: Record, string | number> & + Partial>, + bgColors: Record, string | number> & + Partial>, + mode: ColorMode, + options: { name?: string; sourcePath?: string; sourceInfo?: SourceInfo } = {}, + ) { + this.name = options.name; + this.sourcePath = options.sourcePath; + this.sourceInfo = options.sourceInfo; + this.mode = mode; + this.fgColors = new Map(); + const colors = { + ...fgColors, + scrollbarTrack: fgColors.scrollbarTrack ?? fgColors.muted, + scrollbarThumb: fgColors.scrollbarThumb ?? fgColors.text, + thinkingMax: fgColors.thinkingMax ?? fgColors.thinkingXhigh, + searchMatchText: fgColors.searchMatchText ?? fgColors.text, + }; + for (const [key, value] of Object.entries(colors) as [ThemeColor, string | number][]) { + this.fgColors.set(key, fgAnsi(value, mode)); + } + this.bgColors = new Map(); + const backgrounds = { + ...bgColors, + searchMatchBg: bgColors.searchMatchBg ?? bgColors.selectedBg, + }; + for (const [key, value] of Object.entries(backgrounds) as [ThemeBg, string | number][]) { + this.bgColors.set(key, bgAnsi(value, mode)); + } + } + + fg(color: ThemeColor, text: string): string { + const ansi = this.fgColors.get(color); + if (!ansi) throw new Error(`Unknown theme color: ${color}`); + return `${ansi}${text}\x1b[39m`; // Reset only foreground color + } + + bg(color: ThemeBg, text: string): string { + const ansi = this.bgColors.get(color); + if (!ansi) throw new Error(`Unknown theme background color: ${color}`); + return `${ansi}${text}\x1b[49m`; // Reset only background color + } + + bold(text: string): string { + return chalk.bold(text); + } + + italic(text: string): string { + return chalk.italic(text); + } + + underline(text: string): string { + return chalk.underline(text); + } + + inverse(text: string): string { + return chalk.inverse(text); + } + + strikethrough(text: string): string { + return chalk.strikethrough(text); + } + + getFgAnsi(color: ThemeColor): string { + const ansi = this.fgColors.get(color); + if (!ansi) throw new Error(`Unknown theme color: ${color}`); + return ansi; + } + + getBgAnsi(color: ThemeBg): string { + const ansi = this.bgColors.get(color); + if (!ansi) throw new Error(`Unknown theme background color: ${color}`); + return ansi; + } + + getColorMode(): ColorMode { + return this.mode; + } + + getThinkingBorderColor(level: ThinkingLevel): (str: string) => string { + // Map thinking levels to dedicated theme colors + switch (level) { + case "off": + return (str: string) => this.fg("thinkingOff", str); + case "minimal": + return (str: string) => this.fg("thinkingMinimal", str); + case "low": + return (str: string) => this.fg("thinkingLow", str); + case "medium": + return (str: string) => this.fg("thinkingMedium", str); + case "high": + return (str: string) => this.fg("thinkingHigh", str); + case "xhigh": + return (str: string) => this.fg("thinkingXhigh", str); + case "max": + return (str: string) => this.fg("thinkingMax", str); + default: + return (str: string) => this.fg("thinkingOff", str); + } + } + + getBashModeBorderColor(): (str: string) => string { + return (str: string) => this.fg("bashMode", str); + } +} + +// ============================================================================ +// Theme Loading +// ============================================================================ + +let BUILTIN_THEMES: Record | undefined; + +function getBuiltinThemes(): Record { + if (!BUILTIN_THEMES) { + const themesDir = getThemesDir(); + const darkPath = path.join(themesDir, "dark.json"); + const lightPath = path.join(themesDir, "light.json"); + BUILTIN_THEMES = { + dark: JSON.parse(stripBom(fs.readFileSync(darkPath, "utf-8"))) as ThemeJson, + light: JSON.parse(stripBom(fs.readFileSync(lightPath, "utf-8"))) as ThemeJson, + }; + } + return BUILTIN_THEMES; +} + +export function getAvailableThemes(): string[] { + return getAvailableThemesWithPaths().map(({ name }) => name); +} + +export interface ThemeInfo { + name: string; + path: string | undefined; +} + +export function getAvailableThemesWithPaths(): ThemeInfo[] { + const themesDir = getThemesDir(); + const result: ThemeInfo[] = []; + const seen = new Set(); + const addTheme = (themeInfo: ThemeInfo) => { + if (seen.has(themeInfo.name)) { + return; + } + seen.add(themeInfo.name); + result.push(themeInfo); + }; + + // Built-in themes + for (const name of Object.keys(getBuiltinThemes())) { + addTheme({ name, path: path.join(themesDir, `${name}.json`) }); + } + + // Custom themes + for (const themeInfo of getCustomThemeInfos()) { + addTheme(themeInfo); + } + + for (const [name, theme] of registeredThemes.entries()) { + addTheme({ name, path: theme.sourcePath }); + } + + return result.sort((a, b) => a.name.localeCompare(b.name)); +} + +function getCustomThemeInfos(): ThemeInfo[] { + const customThemesDir = getCustomThemesDir(); + const result: ThemeInfo[] = []; + if (!fs.existsSync(customThemesDir)) { + return result; + } + + for (const file of fs.readdirSync(customThemesDir)) { + if (!file.endsWith(".json")) { + continue; + } + const themePath = path.join(customThemesDir, file); + try { + const customTheme = loadThemeFromPath(themePath); + if (customTheme.name) { + result.push({ name: customTheme.name, path: themePath }); + } + } catch { + // Invalid themes are ignored here; the resource loader reports them + // during normal startup/reload. + } + } + return result; +} + +function assertThemeNameIsValid(name: string): void { + if (name.includes("/")) { + throw new Error( + `Invalid theme name "${name}": theme names cannot contain "/" because it is reserved for automatic light/dark theme settings.`, + ); + } +} + +function parseThemeJson(label: string, json: unknown): ThemeJson { + if (themeJsonValidator) return themeJsonValidator(label, json); + if (typeof json !== "object" || json === null || !("colors" in json)) { + throw new Error(`Invalid theme "${label}": expected an object with a "colors" map.`); + } + return json as ThemeJson; +} + +function parseThemeJsonContent(label: string, content: string): ThemeJson { + let json: unknown; + try { + json = JSON.parse(stripBom(content)); + } catch (error) { + throw new Error(`Failed to parse theme ${label}: ${error}`); + } + return parseThemeJson(label, json); +} + +function loadThemeJson(name: string): ThemeJson { + const builtinThemes = getBuiltinThemes(); + if (name in builtinThemes) { + return builtinThemes[name]; + } + const registeredTheme = registeredThemes.get(name); + if (registeredTheme?.sourcePath) { + const content = fs.readFileSync(registeredTheme.sourcePath, "utf-8"); + return parseThemeJsonContent(registeredTheme.sourcePath, content); + } + if (registeredTheme) { + throw new Error(`Theme "${name}" does not have a source path for export`); + } + const customThemesDir = getCustomThemesDir(); + const themePath = path.join(customThemesDir, `${name}.json`); + if (!fs.existsSync(themePath)) { + throw new Error(`Theme not found: ${name}`); + } + const content = fs.readFileSync(themePath, "utf-8"); + return parseThemeJsonContent(name, content); +} + +function createTheme(themeJson: ThemeJson, mode?: ColorMode, sourcePath?: string): Theme { + const colorMode = mode ?? (getCapabilities().trueColor ? "truecolor" : "256color"); + const resolvedColors = resolveThemeColors(withThemeColorFallbacks(themeJson.colors), themeJson.vars); + const fgColors: Record = {} as Record; + const bgColors: Record = {} as Record; + const bgColorKeys: Set = new Set([ + "selectedBg", + "searchMatchBg", + "userMessageBg", + "customMessageBg", + "toolPendingBg", + "toolSuccessBg", + "toolErrorBg", + ]); + for (const [key, value] of Object.entries(resolvedColors)) { + if (bgColorKeys.has(key)) { + bgColors[key as ThemeBg] = value; + } else { + fgColors[key as ThemeColor] = value; + } + } + return new Theme(fgColors, bgColors, colorMode, { + name: themeJson.name, + sourcePath, + }); +} + +export function loadThemeFromPath(themePath: string, mode?: ColorMode): Theme { + const content = fs.readFileSync(themePath, "utf-8"); + const themeJson = parseThemeJsonContent(themePath, content); + return createTheme(themeJson, mode, themePath); +} + +function loadTheme(name: string, mode?: ColorMode): Theme { + const registeredTheme = registeredThemes.get(name); + if (registeredTheme) { + return registeredTheme; + } + const themeJson = loadThemeJson(name); + return createTheme(themeJson, mode); +} + +export function getThemeByName(name: string): Theme | undefined { + try { + return loadTheme(name); + } catch { + return undefined; + } +} + +export type TerminalTheme = "dark" | "light"; + +export function parseAutoThemeSetting( + themeSetting: string | undefined, +): { lightTheme: string; darkTheme: string } | undefined { + if (!themeSetting) return undefined; + const slashIndex = themeSetting.indexOf("/"); + if (slashIndex === -1 || themeSetting.indexOf("/", slashIndex + 1) !== -1) { + return undefined; + } + + const lightTheme = themeSetting.slice(0, slashIndex).trim(); + const darkTheme = themeSetting.slice(slashIndex + 1).trim(); + if (!lightTheme || !darkTheme) { + return undefined; + } + return { lightTheme, darkTheme }; +} + +export function resolveThemeSetting( + themeSetting: string | undefined, + terminalTheme: TerminalTheme, +): string | undefined { + const autoTheme = parseAutoThemeSetting(themeSetting); + if (autoTheme) { + return terminalTheme === "light" ? autoTheme.lightTheme : autoTheme.darkTheme; + } + if (themeSetting?.includes("/")) return undefined; + if (typeof themeSetting === "string") return themeSetting; + return undefined; +} + +export interface TerminalThemeDetection { + theme: TerminalTheme; + source: "terminal background" | "COLORFGBG" | "fallback"; + detail: string; + confidence: "high" | "low"; +} + +export interface TerminalThemeDetectionOptions { + env?: NodeJS.ProcessEnv; +} + +export interface TerminalBackgroundThemeDetector { + queryTerminalBackgroundColor({ timeoutMs }: { timeoutMs: number }): Promise; +} + +export interface TerminalAutoThemeDetector extends TerminalBackgroundThemeDetector { + queryTerminalColorScheme?({ timeoutMs }: { timeoutMs: number }): Promise; +} + +export interface TerminalBackgroundThemeDetectionOptions extends TerminalThemeDetectionOptions { + ui: TerminalBackgroundThemeDetector; + timeoutMs: number; +} + +export interface TerminalAutoThemeDetectionOptions extends TerminalThemeDetectionOptions { + ui: TerminalAutoThemeDetector; + timeoutMs: number; +} + +function getColorFgBgBackgroundIndex(colorfgbg: string): number | undefined { + const parts = colorfgbg.split(";"); + for (let i = parts.length - 1; i >= 0; i--) { + const bg = parseInt(parts[i].trim(), 10); + if (Number.isInteger(bg) && bg >= 0 && bg <= 255) { + return bg; + } + } + return undefined; +} + +function getRgbColorLuminance({ r, g, b }: RgbColor): number { + const toLinear = (channel: number) => { + const value = channel / 255; + return value <= 0.03928 ? value / 12.92 : ((value + 0.055) / 1.055) ** 2.4; + }; + return 0.2126 * toLinear(r) + 0.7152 * toLinear(g) + 0.0722 * toLinear(b); +} + +function getAnsiColorLuminance(index: number): number { + return getRgbColorLuminance(hexToRgb(ansi256ToHex(index))); +} + +export function getThemeForRgbColor(rgb: RgbColor): TerminalTheme { + return getRgbColorLuminance(rgb) >= 0.5 ? "light" : "dark"; +} + +export function detectTerminalBackgroundFromEnv(options: TerminalThemeDetectionOptions = {}): TerminalThemeDetection { + const env = options.env ?? process.env; + const colorfgbg = env.COLORFGBG || ""; + const bg = getColorFgBgBackgroundIndex(colorfgbg); + if (bg !== undefined) { + return { + theme: getAnsiColorLuminance(bg) >= 0.5 ? "light" : "dark", + source: "COLORFGBG", + detail: `background color index ${bg}`, + confidence: "high", + }; + } + + return { + theme: "dark", + source: "fallback", + detail: "no terminal background hint found", + confidence: "low", + }; +} + +export async function detectTerminalBackgroundTheme({ + ui, + timeoutMs, + env, +}: TerminalBackgroundThemeDetectionOptions): Promise { + try { + const rgb = await ui.queryTerminalBackgroundColor({ timeoutMs }); + if (rgb) { + return { + theme: getThemeForRgbColor(rgb), + source: "terminal background", + detail: `OSC 11 background rgb(${rgb.r}, ${rgb.g}, ${rgb.b})`, + confidence: "high", + }; + } + } catch { + // Fall back to environment-based detection when the terminal query fails. + } + + return detectTerminalBackgroundFromEnv({ env }); +} + +export async function detectTerminalThemeForAuto({ + ui, + timeoutMs, + env, +}: TerminalAutoThemeDetectionOptions): Promise { + let colorSchemePromise: Promise | undefined; + try { + colorSchemePromise = ui.queryTerminalColorScheme?.({ timeoutMs }); + } catch { + // Fall back to OSC 11 / COLORFGBG detection when starting the color-scheme query fails. + } + const backgroundThemePromise = detectTerminalBackgroundTheme({ ui, timeoutMs, env }); + + try { + const colorScheme = await colorSchemePromise; + if (colorScheme) return colorScheme; + } catch { + // Fall back to the concurrently queried OSC 11 / COLORFGBG detection. + } + return (await backgroundThemePromise).theme; +} + +export function getDefaultTheme(): string { + return detectTerminalBackgroundFromEnv().theme; +} + +// ============================================================================ +// Global Theme Instance +// ============================================================================ + +// Use globalThis to share theme across module loaders (tsx + jiti in dev mode) +const THEME_KEY = Symbol.for("@earendil-works/pi-coding-agent:theme"); +const THEME_KEY_OLD = Symbol.for("@mariozechner/pi-coding-agent:theme"); + +// Export theme as a getter that reads from globalThis +// This ensures all module instances (tsx, jiti) see the same theme +export const theme: Theme = new Proxy({} as Theme, { + get(_target, prop) { + const t = (globalThis as Record)[THEME_KEY]; + if (!t) throw new Error("Theme not initialized. Call initTheme() first."); + return (t as unknown as Record)[prop]; + }, +}); + +function setGlobalTheme(t: Theme): void { + (globalThis as Record)[THEME_KEY] = t; + (globalThis as Record)[THEME_KEY_OLD] = t; +} + +let currentThemeName: string | undefined; +let themeWatcher: fs.FSWatcher | undefined; +let themeReloadTimer: NodeJS.Timeout | undefined; +let onThemeChangeCallback: (() => void) | undefined; +const registeredThemes = new Map(); + +export function setRegisteredThemes(themes: Theme[]): void { + registeredThemes.clear(); + for (const theme of themes) { + if (theme.name) { + assertThemeNameIsValid(theme.name); + registeredThemes.set(theme.name, theme); + } + } +} + +export function initTheme(themeName?: string, enableWatcher: boolean = false): void { + const name = themeName ?? getDefaultTheme(); + currentThemeName = name; + try { + setGlobalTheme(loadTheme(name)); + if (enableWatcher) { + startThemeWatcher(); + } + } catch (_error) { + // Theme is invalid - fall back to dark theme silently + currentThemeName = "dark"; + setGlobalTheme(loadTheme("dark")); + // Don't start watcher for fallback theme + } +} + +export function setTheme(name: string, enableWatcher: boolean = false): { success: boolean; error?: string } { + currentThemeName = name; + try { + setGlobalTheme(loadTheme(name)); + if (enableWatcher) { + startThemeWatcher(); + } + if (onThemeChangeCallback) { + onThemeChangeCallback(); + } + return { success: true }; + } catch (error) { + // Theme is invalid - fall back to dark theme + currentThemeName = "dark"; + setGlobalTheme(loadTheme("dark")); + // Don't start watcher for fallback theme + return { + success: false, + error: error instanceof Error ? error.message : String(error), + }; + } +} + +export function setThemeInstance(themeInstance: Theme): void { + setGlobalTheme(themeInstance); + currentThemeName = ""; + stopThemeWatcher(); // Can't watch a direct instance + if (onThemeChangeCallback) { + onThemeChangeCallback(); + } +} + +export function onThemeChange(callback: () => void): void { + onThemeChangeCallback = callback; +} + +function startThemeWatcher(): void { + stopThemeWatcher(); + + // Only watch if it's a custom theme (not built-in) + if (!currentThemeName || currentThemeName === "dark" || currentThemeName === "light") { + return; + } + + const customThemesDir = getCustomThemesDir(); + const watchedThemeName = currentThemeName; + const watchedFileName = `${watchedThemeName}.json`; + const themeFile = path.join(customThemesDir, watchedFileName); + + // Only watch if the file exists + if (!fs.existsSync(themeFile)) { + return; + } + + const scheduleReload = () => { + if (themeReloadTimer) { + clearTimeout(themeReloadTimer); + } + themeReloadTimer = setTimeout(() => { + themeReloadTimer = undefined; + + // Ignore stale timers after switching themes or stopping the watcher + if (currentThemeName !== watchedThemeName) { + return; + } + + // Keep the last successfully loaded theme active if the file is temporarily missing + if (!fs.existsSync(themeFile)) { + return; + } + + try { + // Reload the theme from disk and refresh the registry cache + const reloadedTheme = loadThemeFromPath(themeFile); + registeredThemes.set(watchedThemeName, reloadedTheme); + setGlobalTheme(reloadedTheme); + // Notify callback (to invalidate UI) + if (onThemeChangeCallback) { + onThemeChangeCallback(); + } + } catch (_error) { + // Ignore errors (file might be in invalid state while being edited) + } + }, 100); + }; + + themeWatcher = + watchWithErrorHandler( + customThemesDir, + (_eventType, filename) => { + if (currentThemeName !== watchedThemeName) { + return; + } + if (!filename) { + scheduleReload(); + return; + } + if (filename !== watchedFileName) { + return; + } + scheduleReload(); + }, + () => { + closeWatcher(themeWatcher); + themeWatcher = undefined; + }, + ) ?? undefined; +} + +export function stopThemeWatcher(): void { + if (themeReloadTimer) { + clearTimeout(themeReloadTimer); + themeReloadTimer = undefined; + } + closeWatcher(themeWatcher); + themeWatcher = undefined; +} + +// ============================================================================ +// HTML Export Helpers +// ============================================================================ + +/** + * Convert a 256-color index to hex string. + * Indices 0-15: basic colors (approximate) + * Indices 16-231: 6x6x6 color cube + * Indices 232-255: grayscale ramp + */ +function ansi256ToHex(index: number): string { + // Basic colors (0-15) - approximate common terminal values + const basicColors = [ + "#000000", + "#800000", + "#008000", + "#808000", + "#000080", + "#800080", + "#008080", + "#c0c0c0", + "#808080", + "#ff0000", + "#00ff00", + "#ffff00", + "#0000ff", + "#ff00ff", + "#00ffff", + "#ffffff", + ]; + if (index < 16) { + return basicColors[index]; + } + + // Color cube (16-231): 6x6x6 = 216 colors + if (index < 232) { + const cubeIndex = index - 16; + const r = Math.floor(cubeIndex / 36); + const g = Math.floor((cubeIndex % 36) / 6); + const b = cubeIndex % 6; + const toHex = (n: number) => (n === 0 ? 0 : 55 + n * 40).toString(16).padStart(2, "0"); + return `#${toHex(r)}${toHex(g)}${toHex(b)}`; + } + + // Grayscale (232-255): 24 shades + const gray = 8 + (index - 232) * 10; + const grayHex = gray.toString(16).padStart(2, "0"); + return `#${grayHex}${grayHex}${grayHex}`; +} + +/** + * Get resolved theme colors as CSS-compatible hex strings. + * Used by HTML export to generate CSS custom properties. + */ +export function getResolvedThemeColors(themeName?: string): Record { + const name = themeName ?? currentThemeName ?? getDefaultTheme(); + const isLight = name === "light"; + const themeJson = loadThemeJson(name); + const resolved = resolveThemeColors(withThemeColorFallbacks(themeJson.colors), themeJson.vars); + + // Default text color for empty values (terminal uses default fg color) + const defaultText = isLight ? "#000000" : "#e5e5e7"; + + const cssColors: Record = {}; + for (const [key, value] of Object.entries(resolved)) { + if (typeof value === "number") { + cssColors[key] = ansi256ToHex(value); + } else if (value === "") { + // Empty means default terminal color - use sensible fallback for HTML + cssColors[key] = defaultText; + } else { + cssColors[key] = value; + } + } + return cssColors; +} + +/** + * Check if a theme is a "light" theme (for CSS that needs light/dark variants). + */ +export function isLightTheme(themeName?: string): boolean { + // Currently just check the name - could be extended to analyze colors + return themeName === "light"; +} + +/** + * Get explicit export colors from theme JSON, if specified. + * Returns undefined for each color that isn't explicitly set. + */ +export function getThemeExportColors(themeName?: string): { + pageBg?: string; + cardBg?: string; + infoBg?: string; +} { + const name = themeName ?? currentThemeName ?? getDefaultTheme(); + try { + const themeJson = loadThemeJson(name); + const exportSection = themeJson.export; + if (!exportSection) return {}; + + const vars = themeJson.vars ?? {}; + const resolve = (value: ColorValue | undefined): string | undefined => { + if (value === undefined) return undefined; + const resolved = resolveVarRefs(value, vars); + if (typeof resolved === "number") return ansi256ToHex(resolved); + if (resolved === "") return undefined; + return resolved; + }; + + return { + pageBg: resolve(exportSection.pageBg), + cardBg: resolve(exportSection.cardBg), + infoBg: resolve(exportSection.infoBg), + }; + } catch { + return {}; + } +} + +// ============================================================================ +// TUI Helpers +// ============================================================================ + +type CliHighlightTheme = Record string>; + +let cachedHighlightThemeFor: Theme | undefined; +let cachedCliHighlightTheme: CliHighlightTheme | undefined; + +function buildCliHighlightTheme(t: Theme): CliHighlightTheme { + return { + keyword: (s: string) => t.fg("syntaxKeyword", s), + built_in: (s: string) => t.fg("syntaxType", s), + literal: (s: string) => t.fg("syntaxNumber", s), + number: (s: string) => t.fg("syntaxNumber", s), + regexp: (s: string) => t.fg("syntaxString", s), + string: (s: string) => t.fg("syntaxString", s), + comment: (s: string) => t.fg("syntaxComment", s), + doctag: (s: string) => t.fg("syntaxComment", s), + meta: (s: string) => t.fg("muted", s), + function: (s: string) => t.fg("syntaxFunction", s), + title: (s: string) => t.fg("syntaxFunction", s), + class: (s: string) => t.fg("syntaxType", s), + type: (s: string) => t.fg("syntaxType", s), + tag: (s: string) => t.fg("syntaxPunctuation", s), + name: (s: string) => t.fg("syntaxKeyword", s), + attr: (s: string) => t.fg("syntaxVariable", s), + variable: (s: string) => t.fg("syntaxVariable", s), + params: (s: string) => t.fg("syntaxVariable", s), + operator: (s: string) => t.fg("syntaxOperator", s), + punctuation: (s: string) => t.fg("syntaxPunctuation", s), + emphasis: (s: string) => t.italic(s), + strong: (s: string) => t.bold(s), + link: (s: string) => t.underline(s), + addition: (s: string) => t.fg("toolDiffAdded", s), + deletion: (s: string) => t.fg("toolDiffRemoved", s), + }; +} + +function getCliHighlightTheme(t: Theme): CliHighlightTheme { + if (cachedHighlightThemeFor !== t || !cachedCliHighlightTheme) { + cachedHighlightThemeFor = t; + cachedCliHighlightTheme = buildCliHighlightTheme(t); + } + return cachedCliHighlightTheme; +} + +/** + * Highlight code with syntax coloring based on file extension or language. + * Returns array of highlighted lines. + */ +export function highlightCode(code: string, lang?: string): string[] { + // Validate language before highlighting to avoid stderr spam from cli-highlight + const validLang = lang && supportsLanguage(lang) ? lang : undefined; + // Skip highlighting when no valid language is specified. cli-highlight's + // auto-detection is unreliable and can misidentify prose as AppleScript, + // LiveCodeServer, etc., coloring random English words as keywords. + if (!validLang) { + return code.split("\n").map((line) => theme.fg("mdCodeBlock", line)); + } + const opts = { + language: validLang, + ignoreIllegals: true, + theme: getCliHighlightTheme(theme), + }; + try { + return highlight(code, opts).split("\n"); + } catch { + return code.split("\n"); + } +} + +/** + * Get language identifier from file path extension. + */ +export function getLanguageFromPath(filePath: string): string | undefined { + const ext = filePath.split(".").pop()?.toLowerCase(); + if (!ext) return undefined; + + const extToLang: Record = { + ts: "typescript", + tsx: "typescript", + js: "javascript", + jsx: "javascript", + mjs: "javascript", + cjs: "javascript", + py: "python", + rb: "ruby", + rs: "rust", + go: "go", + java: "java", + kt: "kotlin", + swift: "swift", + c: "c", + h: "c", + cpp: "cpp", + cc: "cpp", + cxx: "cpp", + hpp: "cpp", + cs: "csharp", + php: "php", + sh: "bash", + bash: "bash", + zsh: "bash", + fish: "fish", + ps1: "powershell", + sql: "sql", + html: "html", + htm: "html", + css: "css", + scss: "scss", + sass: "sass", + less: "less", + json: "json", + yaml: "yaml", + yml: "yaml", + toml: "toml", + xml: "xml", + md: "markdown", + markdown: "markdown", + dockerfile: "dockerfile", + makefile: "makefile", + cmake: "cmake", + lua: "lua", + perl: "perl", + r: "r", + scala: "scala", + clj: "clojure", + ex: "elixir", + exs: "elixir", + erl: "erlang", + hs: "haskell", + ml: "ocaml", + vim: "vim", + graphql: "graphql", + proto: "protobuf", + tf: "hcl", + hcl: "hcl", + }; + + return extToLang[ext]; +} + +export function getMarkdownTheme(): MarkdownTheme { + return { + heading: (text: string) => theme.fg("mdHeading", text), + link: (text: string) => theme.fg("mdLink", text), + linkUrl: (text: string) => theme.fg("mdLinkUrl", text), + code: (text: string) => theme.fg("mdCode", text), + codeBlock: (text: string) => theme.fg("mdCodeBlock", text), + codeBlockBorder: (text: string) => theme.fg("mdCodeBlockBorder", text), + quote: (text: string) => theme.fg("mdQuote", text), + quoteBorder: (text: string) => theme.fg("mdQuoteBorder", text), + hr: (text: string) => theme.fg("mdHr", text), + listBullet: (text: string) => theme.fg("mdListBullet", text), + bold: (text: string) => theme.bold(text), + italic: (text: string) => theme.italic(text), + underline: (text: string) => theme.underline(text), + strikethrough: (text: string) => chalk.strikethrough(text), + highlightCode: (code: string, lang?: string): string[] => { + // Validate language before highlighting to avoid stderr spam from cli-highlight + const validLang = lang && supportsLanguage(lang) ? lang : undefined; + // Skip highlighting when no valid language is specified. cli-highlight's + // auto-detection is unreliable and can misidentify prose as AppleScript, + // LiveCodeServer, etc., coloring random English words as keywords. + if (!validLang) { + return code.split("\n").map((line) => theme.fg("mdCodeBlock", line)); + } + const opts = { + language: validLang, + ignoreIllegals: true, + theme: getCliHighlightTheme(theme), + }; + try { + return highlight(code, opts).split("\n"); + } catch { + return code.split("\n").map((line) => theme.fg("mdCodeBlock", line)); + } + }, + }; +} + +export function getSelectListTheme(): SelectListTheme { + return { + selectedPrefix: (text: string) => theme.fg("accent", text), + selectedText: (text: string) => theme.fg("accent", text), + description: (text: string) => theme.fg("muted", text), + scrollInfo: (text: string) => theme.fg("muted", text), + noMatch: (text: string) => theme.fg("muted", text), + }; +} + +export function getEditorTheme(): EditorTheme { + return { + borderColor: (text: string) => theme.fg("borderMuted", text), + selectList: getSelectListTheme(), + }; +} + +export function getSettingsListTheme(): SettingsListTheme { + return { + label: (text: string, selected: boolean) => (selected ? theme.fg("accent", text) : text), + value: (text: string, selected: boolean) => (selected ? theme.fg("accent", text) : theme.fg("muted", text)), + description: (text: string) => theme.fg("dim", text), + cursor: theme.fg("accent", "→ "), + hint: (text: string) => theme.fg("dim", text), + }; +} diff --git a/packages/coding-agent/test/agent-session-auto-compaction-queue.test.ts b/packages/coding-agent/test/agent-session-auto-compaction-queue.test.ts new file mode 100644 index 0000000000000000000000000000000000000000..8c1769dcc697d607a30c8f33b87aa6fe9835a18a --- /dev/null +++ b/packages/coding-agent/test/agent-session-auto-compaction-queue.test.ts @@ -0,0 +1,449 @@ +import { existsSync, mkdirSync, rmSync } from "node:fs"; +import { tmpdir } from "node:os"; +import { join } from "node:path"; +import { Agent } from "@earendil-works/pi-agent-core"; +import { type AssistantMessage, createAssistantMessageEventStream, fauxAssistantMessage } from "@earendil-works/pi-ai"; +import { getModel, streamSimple } from "@earendil-works/pi-ai/compat"; +import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; +import { AgentSession } from "../src/core/agent-session.ts"; +import { AuthStorage } from "../src/core/auth-storage.ts"; +import { SessionManager } from "../src/core/session-manager.ts"; +import { SettingsManager } from "../src/core/settings-manager.ts"; +import { createModelRegistry, getModelRuntime } from "./model-runtime-test-utils.ts"; +import { createTestResourceLoader } from "./utilities.ts"; + +describe("AgentSession auto-compaction queue resume", () => { + let session: AgentSession; + let sessionManager: SessionManager; + let settingsManager: SettingsManager; + let tempDir: string; + + beforeEach(async () => { + tempDir = join(tmpdir(), `pi-auto-compaction-queue-${Date.now()}`); + mkdirSync(tempDir, { recursive: true }); + + const model = getModel("anthropic", "claude-sonnet-4-5")!; + const agent = new Agent({ + streamFn: streamSimple, + initialState: { + model, + systemPrompt: "Test", + tools: [], + }, + }); + + sessionManager = SessionManager.inMemory(); + settingsManager = SettingsManager.create(tempDir, tempDir); + const authStorage = AuthStorage.create(join(tempDir, "auth.json")); + await authStorage.modify("anthropic", async () => ({ type: "api_key", key: "test-key" })); + const modelRegistry = await createModelRegistry(authStorage, tempDir); + + session = new AgentSession({ + agent, + sessionManager, + settingsManager, + cwd: tempDir, + modelRuntime: getModelRuntime(modelRegistry), + resourceLoader: createTestResourceLoader(), + }); + }); + + afterEach(() => { + session.dispose(); + vi.restoreAllMocks(); + if (tempDir && existsSync(tempDir)) { + rmSync(tempDir, { recursive: true }); + } + }); + + it("should resume after threshold compaction when only agent-level queued messages exist", async () => { + settingsManager.applyOverrides({ compaction: { keepRecentTokens: 1 } }); + const model = session.model!; + const now = Date.now(); + sessionManager.appendMessage({ + role: "user", + content: [{ type: "text", text: "message to compact" }], + timestamp: now - 1000, + }); + sessionManager.appendMessage({ + role: "assistant", + content: [{ type: "text", text: "assistant response to compact" }], + api: model.api, + provider: model.provider, + model: model.id, + usage: { + input: 100, + output: 0, + cacheRead: 0, + cacheWrite: 0, + totalTokens: 100, + cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 }, + }, + stopReason: "stop", + timestamp: now - 500, + }); + session.agent.state.messages = sessionManager.buildSessionContext().messages; + session.agent.streamFunction = (summaryModel) => { + const stream = createAssistantMessageEventStream(); + void Promise.resolve().then(() => { + stream.push({ + type: "done", + reason: "stop", + message: { + ...fauxAssistantMessage("compacted"), + api: summaryModel.api, + provider: summaryModel.provider, + model: summaryModel.id, + usage: { + input: 10, + output: 0, + cacheRead: 0, + cacheWrite: 0, + totalTokens: 10, + cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 }, + }, + }, + }); + }); + return stream; + }; + + session.agent.followUp({ + role: "custom", + customType: "test", + content: [{ type: "text", text: "Queued custom" }], + display: false, + timestamp: Date.now(), + }); + + expect(session.pendingMessageCount).toBe(0); + expect(session.agent.hasQueuedMessages()).toBe(true); + + const continueSpy = vi.spyOn(session.agent, "continue").mockResolvedValue(); + + const runAutoCompaction = ( + session as unknown as { + _runAutoCompaction: (reason: "overflow" | "threshold", willRetry: boolean) => Promise; + } + )._runAutoCompaction.bind(session); + + await expect(runAutoCompaction("threshold", false)).resolves.toBe(true); + + expect(continueSpy).not.toHaveBeenCalled(); + }); + + it("should not compact repeatedly after overflow recovery already attempted", async () => { + const model = session.model!; + const overflowMessage: AssistantMessage = { + role: "assistant", + content: [{ type: "text", text: "" }], + api: model.api, + provider: model.provider, + model: model.id, + usage: { + input: 0, + output: 0, + cacheRead: 0, + cacheWrite: 0, + totalTokens: 0, + cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 }, + }, + stopReason: "error", + errorMessage: "prompt is too long", + timestamp: Date.now(), + }; + + const runAutoCompactionSpy = vi + .spyOn( + session as unknown as { + _runAutoCompaction: (reason: "overflow" | "threshold", willRetry: boolean) => Promise; + }, + "_runAutoCompaction", + ) + .mockResolvedValue(); + + const events: Array<{ type: string; reason: string; errorMessage?: string }> = []; + session.subscribe((event) => { + if (event.type === "compaction_end") { + events.push({ type: event.type, reason: event.reason, errorMessage: event.errorMessage }); + } + }); + + const checkCompaction = ( + session as unknown as { + _checkCompaction: (assistantMessage: AssistantMessage, skipAbortedCheck?: boolean) => Promise; + } + )._checkCompaction.bind(session); + + await checkCompaction(overflowMessage); + await checkCompaction({ ...overflowMessage, timestamp: Date.now() + 1 }); + + expect(runAutoCompactionSpy).toHaveBeenCalledTimes(1); + expect(events).toContainEqual({ + type: "compaction_end", + reason: "overflow", + errorMessage: + "Context overflow recovery failed after one compact-and-retry attempt. Try reducing context or switching to a larger-context model.", + }); + }); + + it("should ignore stale pre-compaction assistant usage on pre-prompt compaction checks", async () => { + const model = session.model!; + const staleAssistantTimestamp = Date.now() - 10_000; + const staleAssistant: AssistantMessage = { + role: "assistant", + content: [{ type: "text", text: "large response before compaction" }], + api: model.api, + provider: model.provider, + model: model.id, + usage: { + input: 600_000, + output: 10_000, + cacheRead: 0, + cacheWrite: 0, + totalTokens: 610_000, + cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 }, + }, + stopReason: "stop", + timestamp: staleAssistantTimestamp, + }; + + sessionManager.appendMessage({ + role: "user", + content: [{ type: "text", text: "before compaction" }], + timestamp: staleAssistantTimestamp - 1000, + }); + sessionManager.appendMessage(staleAssistant); + + const firstKeptEntryId = sessionManager.getEntries()[0]!.id; + sessionManager.appendCompaction("summary", firstKeptEntryId, staleAssistant.usage.totalTokens, undefined, false); + + sessionManager.appendMessage({ + role: "user", + content: [{ type: "text", text: "session recovery payload" }], + timestamp: Date.now(), + }); + + const runAutoCompactionSpy = vi + .spyOn( + session as unknown as { + _runAutoCompaction: (reason: "overflow" | "threshold", willRetry: boolean) => Promise; + }, + "_runAutoCompaction", + ) + .mockResolvedValue(); + + const checkCompaction = ( + session as unknown as { + _checkCompaction: (assistantMessage: AssistantMessage, skipAbortedCheck?: boolean) => Promise; + } + )._checkCompaction.bind(session); + + await checkCompaction(staleAssistant, false); + + expect(runAutoCompactionSpy).not.toHaveBeenCalled(); + }); + + it("should trigger threshold compaction for error messages using last successful usage", async () => { + const model = session.model!; + + // A successful assistant message with token usage just over the compaction threshold. + // Compute this from the selected model so generated catalog context-window changes do not break the test. + const compactionSettings = settingsManager.getCompactionSettings(); + const thresholdTokens = (model.contextWindow ?? 200_000) - compactionSettings.reserveTokens + 1; + const successfulAssistant: AssistantMessage = { + role: "assistant", + content: [{ type: "text", text: "large successful response" }], + api: model.api, + provider: model.provider, + model: model.id, + usage: { + input: thresholdTokens - 10_000, + output: 10_000, + cacheRead: 0, + cacheWrite: 0, + totalTokens: thresholdTokens, + cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 }, + }, + stopReason: "stop", + timestamp: Date.now(), + }; + + // An error message (e.g. 529 overloaded) with no useful usage data + const errorAssistant: AssistantMessage = { + role: "assistant", + content: [{ type: "text", text: "" }], + api: model.api, + provider: model.provider, + model: model.id, + usage: { + input: 0, + output: 0, + cacheRead: 0, + cacheWrite: 0, + totalTokens: 0, + cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 }, + }, + stopReason: "error", + errorMessage: "529 overloaded", + timestamp: Date.now() + 1000, + }; + + // Put both messages into agent state so estimateContextTokens can find the successful one + session.agent.state.messages = [ + { role: "user", content: [{ type: "text", text: "hello" }], timestamp: Date.now() - 1000 }, + successfulAssistant, + { role: "user", content: [{ type: "text", text: "another prompt" }], timestamp: Date.now() + 500 }, + errorAssistant, + ]; + + const runAutoCompactionSpy = vi + .spyOn( + session as unknown as { + _runAutoCompaction: (reason: "overflow" | "threshold", willRetry: boolean) => Promise; + }, + "_runAutoCompaction", + ) + .mockResolvedValue(); + + const checkCompaction = ( + session as unknown as { + _checkCompaction: (assistantMessage: AssistantMessage, skipAbortedCheck?: boolean) => Promise; + } + )._checkCompaction.bind(session); + + await checkCompaction(errorAssistant); + + expect(runAutoCompactionSpy).toHaveBeenCalledWith("threshold", false); + }); + + it("should not trigger threshold compaction for error messages when no prior usage exists", async () => { + const model = session.model!; + + // An error message with no prior successful assistant in context + const errorAssistant: AssistantMessage = { + role: "assistant", + content: [{ type: "text", text: "" }], + api: model.api, + provider: model.provider, + model: model.id, + usage: { + input: 0, + output: 0, + cacheRead: 0, + cacheWrite: 0, + totalTokens: 0, + cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 }, + }, + stopReason: "error", + errorMessage: "529 overloaded", + timestamp: Date.now(), + }; + + session.agent.state.messages = [ + { role: "user", content: [{ type: "text", text: "hello" }], timestamp: Date.now() - 1000 }, + errorAssistant, + ]; + + const runAutoCompactionSpy = vi + .spyOn( + session as unknown as { + _runAutoCompaction: (reason: "overflow" | "threshold", willRetry: boolean) => Promise; + }, + "_runAutoCompaction", + ) + .mockResolvedValue(); + + const checkCompaction = ( + session as unknown as { + _checkCompaction: (assistantMessage: AssistantMessage, skipAbortedCheck?: boolean) => Promise; + } + )._checkCompaction.bind(session); + + await checkCompaction(errorAssistant); + + expect(runAutoCompactionSpy).not.toHaveBeenCalled(); + }); + + it("should not trigger threshold compaction for error messages when only kept pre-compaction usage exists", async () => { + const model = session.model!; + const preCompactionTimestamp = Date.now() - 10_000; + + // A "kept" assistant message from before compaction with high usage + const keptAssistant: AssistantMessage = { + role: "assistant", + content: [{ type: "text", text: "kept response from before compaction" }], + api: model.api, + provider: model.provider, + model: model.id, + usage: { + input: 180_000, + output: 10_000, + cacheRead: 0, + cacheWrite: 0, + totalTokens: 190_000, + cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 }, + }, + stopReason: "stop", + timestamp: preCompactionTimestamp, + }; + + // Record the kept assistant in the session and create a compaction after it + sessionManager.appendMessage({ + role: "user", + content: [{ type: "text", text: "before compaction" }], + timestamp: preCompactionTimestamp - 1000, + }); + sessionManager.appendMessage(keptAssistant); + const firstKeptEntryId = sessionManager.getEntries()[0]!.id; + sessionManager.appendCompaction("summary", firstKeptEntryId, keptAssistant.usage.totalTokens, undefined, false); + + // Post-compaction error message + const errorAssistant: AssistantMessage = { + role: "assistant", + content: [{ type: "text", text: "" }], + api: model.api, + provider: model.provider, + model: model.id, + usage: { + input: 0, + output: 0, + cacheRead: 0, + cacheWrite: 0, + totalTokens: 0, + cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 }, + }, + stopReason: "error", + errorMessage: "529 overloaded", + timestamp: Date.now(), + }; + + // Agent state has the kept assistant (pre-compaction) and the error (post-compaction) + session.agent.state.messages = [ + { role: "user", content: [{ type: "text", text: "kept user msg" }], timestamp: preCompactionTimestamp - 1000 }, + keptAssistant, + { role: "user", content: [{ type: "text", text: "new prompt" }], timestamp: Date.now() - 500 }, + errorAssistant, + ]; + + const runAutoCompactionSpy = vi + .spyOn( + session as unknown as { + _runAutoCompaction: (reason: "overflow" | "threshold", willRetry: boolean) => Promise; + }, + "_runAutoCompaction", + ) + .mockResolvedValue(); + + const checkCompaction = ( + session as unknown as { + _checkCompaction: (assistantMessage: AssistantMessage, skipAbortedCheck?: boolean) => Promise; + } + )._checkCompaction.bind(session); + + await checkCompaction(errorAssistant); + + // Should NOT compact because the only usage data is from a kept pre-compaction message + expect(runAutoCompactionSpy).not.toHaveBeenCalled(); + }); +}); diff --git a/packages/coding-agent/test/agent-session-branching.test.ts b/packages/coding-agent/test/agent-session-branching.test.ts new file mode 100644 index 0000000000000000000000000000000000000000..7016ecca98bc84314db101b51ba5ff2d2da9f55a --- /dev/null +++ b/packages/coding-agent/test/agent-session-branching.test.ts @@ -0,0 +1,156 @@ +/** + * Tests for AgentSession forking behavior. + * + * These tests verify: + * - Forking from a single message works + * - Forking in --no-session mode (in-memory only) + * - getUserMessagesForForking returns correct entries + */ + +import { existsSync, mkdirSync, rmSync } from "node:fs"; +import { tmpdir } from "node:os"; +import { join } from "node:path"; +import { getModel } from "@earendil-works/pi-ai/compat"; +import { afterEach, beforeEach, describe, expect, it } from "vitest"; +import type { AgentSession } from "../src/core/agent-session.ts"; +import { + type AgentSessionRuntime, + type CreateAgentSessionRuntimeFactory, + createAgentSessionFromServices, + createAgentSessionRuntime, + createAgentSessionServices, +} from "../src/core/agent-session-runtime.ts"; +import { AuthStorage } from "../src/core/auth-storage.ts"; +import { SessionManager } from "../src/core/session-manager.ts"; +import { API_KEY } from "./utilities.ts"; + +describe.skipIf(!API_KEY)("AgentSession forking", () => { + let session: AgentSession; + let runtimeHost: AgentSessionRuntime; + let tempDir: string; + let sessionManager: SessionManager; + + beforeEach(() => { + tempDir = join(tmpdir(), `pi-branching-test-${Date.now()}`); + mkdirSync(tempDir, { recursive: true }); + }); + + afterEach(async () => { + if (runtimeHost) { + await runtimeHost.dispose(); + } + if (tempDir && existsSync(tempDir)) { + rmSync(tempDir, { recursive: true }); + } + }); + + async function createSession(noSession: boolean = false) { + const model = getModel("anthropic", "claude-sonnet-4-5")!; + sessionManager = noSession ? SessionManager.inMemory(tempDir) : SessionManager.create(tempDir); + const authStorage = AuthStorage.create(join(tempDir, "auth.json")); + await authStorage.modify("anthropic", async () => ({ type: "api_key", key: API_KEY! })); + + const servicesOptions = { + agentDir: tempDir, + authStorage, + resourceLoaderOptions: { + noExtensions: true, + noSkills: true, + noPromptTemplates: true, + noThemes: true, + }, + }; + const createRuntime: CreateAgentSessionRuntimeFactory = async ({ cwd, sessionManager, sessionStartEvent }) => { + const services = await createAgentSessionServices({ + ...servicesOptions, + cwd, + }); + return { + ...(await createAgentSessionFromServices({ + services, + sessionManager, + sessionStartEvent, + model, + tools: ["read", "bash", "edit", "write"], + })), + services, + diagnostics: services.diagnostics, + }; + }; + runtimeHost = await createAgentSessionRuntime(createRuntime, { + cwd: tempDir, + agentDir: tempDir, + sessionManager, + }); + session = runtimeHost.session; + session.subscribe(() => {}); + return session; + } + + it("should allow forking from single message", async () => { + await createSession(); + + await session.prompt("Say hello"); + await session.agent.waitForIdle(); + + const userMessages = session.getUserMessagesForForking(); + expect(userMessages.length).toBe(1); + expect(userMessages[0].text).toBe("Say hello"); + + const result = await runtimeHost.fork(userMessages[0].entryId); + expect(result.cancelled).toBe(false); + session = runtimeHost.session; + expect(result.selectedText).toBe("Say hello"); + + expect(session.messages.length).toBe(0); + expect(session.sessionFile).not.toBeNull(); + expect(existsSync(session.sessionFile!)).toBe(false); + }); + + it("should support in-memory forking in --no-session mode", async () => { + await createSession(true); + + expect(session.sessionFile).toBeUndefined(); + + await session.prompt("Say hi"); + await session.agent.waitForIdle(); + + const userMessages = session.getUserMessagesForForking(); + expect(userMessages.length).toBe(1); + expect(session.messages.length).toBeGreaterThan(0); + + const result = await runtimeHost.fork(userMessages[0].entryId); + expect(result.cancelled).toBe(false); + session = runtimeHost.session; + expect(result.selectedText).toBe("Say hi"); + + expect(session.messages.length).toBe(0); + expect(session.sessionFile).toBeUndefined(); + }); + + it("should fork from middle of conversation", async () => { + await createSession(); + + await session.prompt("Say one"); + await session.agent.waitForIdle(); + + await session.prompt("Say two"); + await session.agent.waitForIdle(); + + await session.prompt("Say three"); + await session.agent.waitForIdle(); + + const userMessages = session.getUserMessagesForForking(); + expect(userMessages.length).toBe(3); + + const secondMessage = userMessages[1]; + const result = await runtimeHost.fork(secondMessage.entryId); + expect(result.cancelled).toBe(false); + session = runtimeHost.session; + expect(result.selectedText).toBe("Say two"); + + expect(session.messages.length).toBe(2); + expect(session.messages[0].role).toBe("user"); + expect(session.messages[1].role).toBe("assistant"); + }, 60000); +}); diff --git a/packages/coding-agent/test/agent-session-compaction.test.ts b/packages/coding-agent/test/agent-session-compaction.test.ts new file mode 100644 index 0000000000000000000000000000000000000000..6484aea0ad785af7a08306d6dfb529126b8c5dbb --- /dev/null +++ b/packages/coding-agent/test/agent-session-compaction.test.ts @@ -0,0 +1,209 @@ +import { createModelRegistry, getModelRuntime } from "./model-runtime-test-utils.ts"; +/** + * E2E tests for AgentSession compaction behavior. + * + * These tests use real LLM calls (no mocking) to verify: + * - Manual compaction works correctly + * - Session persistence during compaction + * - Compaction entry is saved to session file + */ + +import { existsSync, mkdirSync, rmSync } from "node:fs"; +import { tmpdir } from "node:os"; +import { join } from "node:path"; +import { Agent } from "@earendil-works/pi-agent-core"; +import { getModel, streamSimple } from "@earendil-works/pi-ai/compat"; +import { afterEach, beforeEach, describe, expect, it } from "vitest"; +import { AgentSession, type AgentSessionEvent } from "../src/core/agent-session.ts"; +import { AuthStorage } from "../src/core/auth-storage.ts"; +import { SessionManager } from "../src/core/session-manager.ts"; +import { SettingsManager } from "../src/core/settings-manager.ts"; +import { createCodingTools } from "../src/index.ts"; +import { API_KEY, createTestResourceLoader } from "./utilities.ts"; + +describe.skipIf(!API_KEY)("AgentSession compaction e2e", () => { + let session: AgentSession; + let tempDir: string; + let sessionManager: SessionManager; + let events: AgentSessionEvent[]; + + beforeEach(async () => { + // Create temp directory for session files + tempDir = join(tmpdir(), `pi-compaction-test-${Date.now()}`); + mkdirSync(tempDir, { recursive: true }); + + // Track events + events = []; + }); + + afterEach(async () => { + if (session) { + session.dispose(); + } + if (tempDir && existsSync(tempDir)) { + rmSync(tempDir, { recursive: true }); + } + }); + + async function createSession(inMemory = false) { + const model = getModel("anthropic", "claude-sonnet-4-5")!; + const agent = new Agent({ + getApiKey: () => API_KEY, + streamFn: streamSimple, + initialState: { + model, + systemPrompt: "You are a helpful assistant. Be concise.", + tools: createCodingTools(process.cwd()), + }, + }); + + sessionManager = inMemory ? SessionManager.inMemory() : SessionManager.create(tempDir); + const settingsManager = SettingsManager.create(tempDir, tempDir); + // Use minimal keepRecentTokens so small test conversations have something to summarize + settingsManager.applyOverrides({ compaction: { keepRecentTokens: 1 } }); + const authStorage = AuthStorage.create(join(tempDir, "auth.json")); + const modelRegistry = await createModelRegistry(authStorage); + + session = new AgentSession({ + agent, + sessionManager, + settingsManager, + cwd: tempDir, + modelRuntime: getModelRuntime(modelRegistry), + resourceLoader: createTestResourceLoader(), + }); + + // Subscribe to track events + session.subscribe((event) => { + events.push(event); + }); + + return session; + } + + it("should trigger manual compaction via compact()", async () => { + await createSession(); + + // Send a few prompts to build up history + await session.prompt("What is 2+2? Reply with just the number."); + await session.agent.waitForIdle(); + + await session.prompt("What is 3+3? Reply with just the number."); + await session.agent.waitForIdle(); + + // Manually compact + const result = await session.compact(); + + expect(result.summary).toBeDefined(); + expect(result.summary.length).toBeGreaterThan(0); + expect(result.tokensBefore).toBeGreaterThan(0); + + // Verify messages were compacted (should have summary + recent) + const messages = session.messages; + expect(messages.length).toBeGreaterThan(0); + + // First message should be the summary (a user message with summary content) + const firstMsg = messages[0]; + expect(firstMsg.role).toBe("compactionSummary"); + }, 120000); + + it("should maintain valid session state after compaction", async () => { + await createSession(); + + // Build up history + await session.prompt("What is the capital of France? One word answer."); + await session.agent.waitForIdle(); + + await session.prompt("What is the capital of Germany? One word answer."); + await session.agent.waitForIdle(); + + // Compact + await session.compact(); + + // Session should still be usable + await session.prompt("What is the capital of Italy? One word answer."); + await session.agent.waitForIdle(); + + // Should have messages after compaction + expect(session.messages.length).toBeGreaterThan(0); + + // The agent should have responded + const assistantMessages = session.messages.filter((m) => m.role === "assistant"); + expect(assistantMessages.length).toBeGreaterThan(0); + }, 180000); + + it("should persist compaction to session file", async () => { + await createSession(); + + await session.prompt("Say hello"); + await session.agent.waitForIdle(); + + await session.prompt("Say goodbye"); + await session.agent.waitForIdle(); + + // Compact + await session.compact(); + + // Load entries from session manager + const entries = sessionManager.getEntries(); + + // Should have a compaction entry + const compactionEntries = entries.filter((e) => e.type === "compaction"); + expect(compactionEntries.length).toBe(1); + + const compaction = compactionEntries[0]; + expect(compaction.type).toBe("compaction"); + if (compaction.type === "compaction") { + expect(compaction.summary.length).toBeGreaterThan(0); + expect(typeof compaction.firstKeptEntryId).toBe("string"); + expect(compaction.tokensBefore).toBeGreaterThan(0); + } + }, 120000); + + it("should work with --no-session mode (in-memory only)", async () => { + await createSession(true); // in-memory mode + + // Send prompts + await session.prompt("What is 2+2? Reply with just the number."); + await session.agent.waitForIdle(); + + await session.prompt("What is 3+3? Reply with just the number."); + await session.agent.waitForIdle(); + + // Compact should work even without file persistence + const result = await session.compact(); + + expect(result.summary).toBeDefined(); + expect(result.summary.length).toBeGreaterThan(0); + + // In-memory entries should have the compaction + const entries = sessionManager.getEntries(); + const compactionEntries = entries.filter((e) => e.type === "compaction"); + expect(compactionEntries.length).toBe(1); + }, 120000); + + it("should emit compaction events during manual compaction", async () => { + await createSession(); + + // Build some history + await session.prompt("Say hello"); + await session.agent.waitForIdle(); + + // Manually trigger compaction and check events + await session.compact(); + + const compactionEvents = events.filter((e) => e.type === "compaction_start" || e.type === "compaction_end"); + expect(compactionEvents).toHaveLength(2); + expect(compactionEvents[0]).toEqual({ type: "compaction_start", reason: "manual" }); + expect(compactionEvents[1]).toMatchObject({ + type: "compaction_end", + reason: "manual", + aborted: false, + willRetry: false, + }); + + // Regular events should have been emitted + const messageEndEvents = events.filter((e) => e.type === "message_end"); + expect(messageEndEvents.length).toBeGreaterThan(0); + }, 120000); +}); diff --git a/packages/coding-agent/test/agent-session-concurrent.test.ts b/packages/coding-agent/test/agent-session-concurrent.test.ts new file mode 100644 index 0000000000000000000000000000000000000000..d48d31d286831123f481be1e232e3c9c409c9d44 --- /dev/null +++ b/packages/coding-agent/test/agent-session-concurrent.test.ts @@ -0,0 +1,629 @@ +import { createModelRegistry, getModelRuntime } from "./model-runtime-test-utils.ts"; +/** + * Tests for AgentSession concurrent prompt guard. + */ + +import { existsSync, mkdirSync, rmSync } from "node:fs"; +import { tmpdir } from "node:os"; +import { join } from "node:path"; +import { Agent } from "@earendil-works/pi-agent-core"; +import { + type AssistantMessage, + type AssistantMessageEvent, + EventStream, + getModel, + type ImageContent, + type TextContent, +} from "@earendil-works/pi-ai/compat"; +import { Type } from "typebox"; +import { afterEach, beforeEach, describe, expect, it } from "vitest"; +import { AgentSession } from "../src/core/agent-session.ts"; +import { AuthStorage } from "../src/core/auth-storage.ts"; +import { SessionManager } from "../src/core/session-manager.ts"; +import { SettingsManager } from "../src/core/settings-manager.ts"; +import type { BuildSystemPromptOptions } from "../src/core/system-prompt.ts"; +import { createTestExtensionsResult, createTestResourceLoader } from "./utilities.ts"; + +// Mock stream that mimics AssistantMessageEventStream +class MockAssistantStream extends EventStream { + constructor() { + super( + (event) => event.type === "done" || event.type === "error", + (event) => { + if (event.type === "done") return event.message; + if (event.type === "error") return event.error; + throw new Error("Unexpected event type"); + }, + ); + } +} + +function createAssistantMessage(text: string): AssistantMessage { + return { + role: "assistant", + content: [{ type: "text", text }], + api: "anthropic-messages", + provider: "anthropic", + model: "mock", + usage: { + input: 0, + output: 0, + cacheRead: 0, + cacheWrite: 0, + totalTokens: 0, + cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 }, + }, + stopReason: "stop", + timestamp: Date.now(), + }; +} + +describe("AgentSession concurrent prompt guard", () => { + let session: AgentSession; + let tempDir: string; + + beforeEach(async () => { + tempDir = join(tmpdir(), `pi-concurrent-test-${Date.now()}`); + mkdirSync(tempDir, { recursive: true }); + }); + + afterEach(async () => { + delete (globalThis as typeof globalThis & { testExtensionApi?: unknown }).testExtensionApi; + delete (globalThis as typeof globalThis & { testCommandRuns?: unknown }).testCommandRuns; + if (session) { + session.dispose(); + } + if (tempDir && existsSync(tempDir)) { + rmSync(tempDir, { recursive: true }); + } + }); + + async function createSession() { + const model = getModel("anthropic", "claude-sonnet-4-5")!; + let abortSignal: AbortSignal | undefined; + + // Use a stream function that responds to abort + const agent = new Agent({ + getApiKey: () => "test-key", + initialState: { + model, + systemPrompt: "Test", + tools: [], + }, + streamFn: (_model, _context, options) => { + abortSignal = options?.signal; + const stream = new MockAssistantStream(); + queueMicrotask(() => { + stream.push({ type: "start", partial: createAssistantMessage("") }); + const checkAbort = () => { + if (abortSignal?.aborted) { + stream.push({ type: "error", reason: "aborted", error: createAssistantMessage("Aborted") }); + } else { + setTimeout(checkAbort, 5); + } + }; + checkAbort(); + }); + return stream; + }, + }); + + const sessionManager = SessionManager.inMemory(); + const settingsManager = SettingsManager.create(tempDir, tempDir); + const authStorage = AuthStorage.create(join(tempDir, "auth.json")); + const modelRegistry = await createModelRegistry(authStorage, tempDir); + // Set a runtime API key so validation passes + await authStorage.modify("anthropic", async () => ({ type: "api_key", key: "test-key" })); + + session = new AgentSession({ + agent, + sessionManager, + settingsManager, + cwd: tempDir, + modelRuntime: getModelRuntime(modelRegistry), + resourceLoader: createTestResourceLoader(), + }); + + return session; + } + + it("should throw when prompt() called while streaming", async () => { + await createSession(); + + // Start first prompt (don't await, it will block until abort) + const firstPrompt = session.prompt("First message"); + + // Wait a tick for isStreaming to be set + await new Promise((resolve) => setTimeout(resolve, 10)); + + // Verify we're streaming + expect(session.isStreaming).toBe(true); + + // Second prompt should reject + await expect(session.prompt("Second message")).rejects.toThrow( + "Agent is already processing. Specify streamingBehavior ('steer' or 'followUp') to queue the message.", + ); + + // Cleanup + await session.abort(); + await firstPrompt.catch(() => {}); // Ignore abort error + }); + + it("should allow steer() while streaming", async () => { + await createSession(); + + // Start first prompt + const firstPrompt = session.prompt("First message"); + await new Promise((resolve) => setTimeout(resolve, 10)); + + // steer should work while streaming + await expect(session.steer("Steering message")).resolves.toBeUndefined(); + expect(session.pendingMessageCount).toBe(1); + + // Cleanup + await session.abort(); + await firstPrompt.catch(() => {}); + }); + + it("should allow followUp() while streaming", async () => { + await createSession(); + + // Start first prompt + const firstPrompt = session.prompt("First message"); + await new Promise((resolve) => setTimeout(resolve, 10)); + + // followUp should work while streaming + await expect(session.followUp("Follow-up message")).resolves.toBeUndefined(); + expect(session.pendingMessageCount).toBe(1); + + // Cleanup + await session.abort(); + await firstPrompt.catch(() => {}); + }); + + it("should queue extension-origin steering messages while streaming", async () => { + const model = getModel("anthropic", "claude-sonnet-4-5")!; + let abortSignal: AbortSignal | undefined; + let sawSteeringMessage = false; + let lastInputSource: string | undefined; + const queueEvents: Array<{ steering: readonly string[]; followUp: readonly string[] }> = []; + + const agent = new Agent({ + getApiKey: () => "test-key", + initialState: { + model, + systemPrompt: "Test", + tools: [], + }, + streamFn: (_model, context, options) => { + abortSignal = options?.signal; + const stream = new MockAssistantStream(); + queueMicrotask(() => { + const userTexts = context.messages + .filter((message) => message.role === "user") + .map((message) => { + if (typeof message.content === "string") { + return message.content; + } + return message.content + .filter((part): part is TextContent | ImageContent => typeof part === "object" && part !== null) + .filter((part): part is TextContent => part.type === "text") + .map((part) => part.text) + .join("\n"); + }); + + if (userTexts.includes("Steer from extension")) { + sawSteeringMessage = true; + stream.push({ type: "start", partial: createAssistantMessage("") }); + stream.push({ type: "done", reason: "stop", message: createAssistantMessage("Steered") }); + return; + } + + stream.push({ type: "start", partial: createAssistantMessage("") }); + const checkAbort = () => { + if (abortSignal?.aborted) { + stream.push({ type: "error", reason: "aborted", error: createAssistantMessage("Aborted") }); + } else { + setTimeout(checkAbort, 5); + } + }; + checkAbort(); + }); + return stream; + }, + }); + + const sessionManager = SessionManager.inMemory(); + const settingsManager = SettingsManager.create(tempDir, tempDir); + const authStorage = AuthStorage.create(join(tempDir, "auth.json")); + const modelRegistry = await createModelRegistry(authStorage, tempDir); + await authStorage.modify("anthropic", async () => ({ type: "api_key", key: "test-key" })); + + const extensionsResult = await createTestExtensionsResult([ + (pi) => { + (globalThis as typeof globalThis & { testExtensionApi?: unknown }).testExtensionApi = pi; + }, + (pi) => { + pi.on("input", async (event) => { + lastInputSource = event.source; + }); + }, + ]); + + session = new AgentSession({ + agent, + sessionManager, + settingsManager, + cwd: tempDir, + modelRuntime: getModelRuntime(modelRegistry), + resourceLoader: createTestResourceLoader({ extensionsResult }), + }); + session.subscribe((event) => { + if (event.type === "queue_update") { + queueEvents.push({ steering: event.steering, followUp: event.followUp }); + } + }); + + const firstPrompt = session.prompt("First message"); + await new Promise((resolve) => setTimeout(resolve, 10)); + expect(session.isStreaming).toBe(true); + + const pi = ( + globalThis as typeof globalThis & { + testExtensionApi?: { + sendUserMessage: (content: string, options?: { deliverAs?: "steer" | "followUp" }) => void; + }; + } + ).testExtensionApi; + expect(pi).toBeDefined(); + + pi!.sendUserMessage("Steer from extension", { deliverAs: "steer" }); + await new Promise((resolve) => setTimeout(resolve, 25)); + + expect(session.pendingMessageCount).toBe(1); + expect(session.getSteeringMessages()).toContain("Steer from extension"); + expect(lastInputSource).toBe("extension"); + expect(queueEvents.some((event) => event.steering.includes("Steer from extension"))).toBe(true); + + await session.abort(); + await firstPrompt.catch(() => {}); + + expect(sawSteeringMessage).toBe(true); + }); + + it("should allow prompt() after previous completes", async () => { + // Create session with a stream that completes immediately + const model = getModel("anthropic", "claude-sonnet-4-5")!; + const agent = new Agent({ + getApiKey: () => "test-key", + initialState: { + model, + systemPrompt: "Test", + tools: [], + }, + streamFn: () => { + const stream = new MockAssistantStream(); + queueMicrotask(() => { + stream.push({ type: "start", partial: createAssistantMessage("") }); + stream.push({ type: "done", reason: "stop", message: createAssistantMessage("Done") }); + }); + return stream; + }, + }); + + const sessionManager = SessionManager.inMemory(); + const settingsManager = SettingsManager.create(tempDir, tempDir); + const authStorage = AuthStorage.create(join(tempDir, "auth.json")); + const modelRegistry = await createModelRegistry(authStorage, tempDir); + await authStorage.modify("anthropic", async () => ({ type: "api_key", key: "test-key" })); + + session = new AgentSession({ + agent, + sessionManager, + settingsManager, + cwd: tempDir, + modelRuntime: getModelRuntime(modelRegistry), + resourceLoader: createTestResourceLoader(), + }); + + // First prompt completes + await session.prompt("First message"); + + // Should not be streaming anymore + expect(session.isStreaming).toBe(false); + + // Second prompt should work + await expect(session.prompt("Second message")).resolves.not.toThrow(); + }); + + it("should wait for queued agent events before emitting tool_call", async () => { + const model = getModel("anthropic", "claude-sonnet-4-5")!; + const tool = { + name: "dummy", + description: "Dummy tool", + label: "dummy", + parameters: Type.Object({ q: Type.String() }), + execute: async (_toolCallId: string, params: unknown) => { + const q = + typeof params === "object" && params !== null && "q" in params + ? String((params as { q: unknown }).q) + : ""; + return { + content: [{ type: "text" as const, text: `result:${q}` }], + details: {}, + }; + }, + }; + + const agent = new Agent({ + getApiKey: () => "test-key", + initialState: { + model, + systemPrompt: "Test", + tools: [tool], + }, + streamFn: async (_model, context) => { + const stream = new MockAssistantStream(); + queueMicrotask(() => { + const toolResultCount = context.messages.filter((message) => message.role === "toolResult").length; + if (toolResultCount > 0) { + const message: AssistantMessage = { + role: "assistant", + content: [{ type: "text", text: "done" }], + api: "anthropic-messages", + provider: "anthropic", + model: "mock", + usage: { + input: 1, + output: 1, + cacheRead: 0, + cacheWrite: 0, + totalTokens: 2, + cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 }, + }, + stopReason: "stop", + timestamp: Date.now(), + }; + stream.push({ type: "start", partial: { ...message, content: [] } }); + stream.push({ type: "done", reason: "stop", message }); + return; + } + + const message: AssistantMessage = { + role: "assistant", + content: [ + { type: "toolCall", id: "toolu_1", name: "dummy", arguments: { q: "x" } }, + { type: "toolCall", id: "toolu_2", name: "dummy", arguments: { q: "y" } }, + ], + api: "anthropic-messages", + provider: "anthropic", + model: "mock", + usage: { + input: 1, + output: 1, + cacheRead: 0, + cacheWrite: 0, + totalTokens: 2, + cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 }, + }, + stopReason: "toolUse", + timestamp: Date.now(), + }; + + stream.push({ type: "start", partial: { ...message, content: [] } }); + stream.push({ type: "done", reason: "toolUse", message }); + }); + return stream; + }, + }); + + const sessionManager = SessionManager.inMemory(); + const settingsManager = SettingsManager.create(tempDir, tempDir); + const authStorage = AuthStorage.create(join(tempDir, "auth.json")); + const modelRegistry = await createModelRegistry(authStorage, tempDir); + await authStorage.modify("anthropic", async () => ({ type: "api_key", key: "test-key" })); + + session = new AgentSession({ + agent, + sessionManager, + settingsManager, + cwd: tempDir, + modelRuntime: getModelRuntime(modelRegistry), + resourceLoader: createTestResourceLoader(), + baseToolsOverride: { dummy: tool }, + }); + + const snapshots: string[][] = []; + const sessionWithRunner = session as unknown as { + _extensionRunner?: { + hasHandlers: (eventType: string) => boolean; + emit: (event: { type: string; message?: { role?: string } }) => Promise; + emitMessageEnd: (event: { type: string; message?: { role?: string } }) => Promise; + emitToolCall: (event: { type: string; toolCallId: string }) => Promise; + emitInput: ( + text: string, + images: unknown, + source: "interactive" | "rpc" | "extension", + streamingBehavior?: "steer" | "followUp", + ) => Promise<{ action: "continue" }>; + emitBeforeAgentStart: ( + prompt: string, + images: unknown, + systemPrompt: string, + systemPromptOptions: BuildSystemPromptOptions, + ) => Promise; + invalidate: (message?: string) => void; + }; + }; + sessionWithRunner._extensionRunner = { + hasHandlers: (eventType) => eventType === "tool_call", + emit: async () => {}, + emitMessageEnd: async () => undefined, + emitToolCall: async () => { + snapshots.push( + sessionManager + .getEntries() + .filter((entry) => entry.type === "message") + .map((entry) => entry.message.role), + ); + return undefined; + }, + emitInput: async () => ({ action: "continue" }), + emitBeforeAgentStart: async () => undefined, + invalidate: () => {}, + }; + + await session.prompt("hi"); + await session.agent.waitForIdle(); + + expect(snapshots).toEqual([ + ["user", "assistant"], + ["user", "assistant"], + ]); + }); + + it("should persist message_end events in order with slow extension handlers", async () => { + const model = getModel("anthropic", "claude-sonnet-4-5")!; + const tool = { + name: "dummy", + description: "Dummy tool", + label: "dummy", + parameters: Type.Object({ q: Type.String() }), + execute: async (_toolCallId: string, params: unknown) => { + const q = + typeof params === "object" && params !== null && "q" in params + ? String((params as { q: unknown }).q) + : ""; + return { + content: [{ type: "text" as const, text: `result:${q}` }], + details: {}, + }; + }, + }; + + const agent = new Agent({ + getApiKey: () => "test-key", + initialState: { + model, + systemPrompt: "Test", + tools: [tool], + }, + streamFn: async (_model, context) => { + const stream = new MockAssistantStream(); + queueMicrotask(() => { + const hasToolResult = context.messages.some((message) => message.role === "toolResult"); + + if (hasToolResult) { + const message: AssistantMessage = { + role: "assistant", + content: [{ type: "text", text: "done" }], + api: "anthropic-messages", + provider: "anthropic", + model: "mock", + usage: { + input: 1, + output: 1, + cacheRead: 0, + cacheWrite: 0, + totalTokens: 2, + cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 }, + }, + stopReason: "stop", + timestamp: Date.now(), + }; + stream.push({ type: "start", partial: { ...message, content: [] } }); + stream.push({ type: "done", reason: "stop", message }); + return; + } + + const message: AssistantMessage = { + role: "assistant", + content: [ + { type: "text", text: "calling tool" }, + { type: "toolCall", id: "toolu_1", name: "dummy", arguments: { q: "x" } }, + ], + api: "anthropic-messages", + provider: "anthropic", + model: "mock", + usage: { + input: 1, + output: 1, + cacheRead: 0, + cacheWrite: 0, + totalTokens: 2, + cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 }, + }, + stopReason: "toolUse", + timestamp: Date.now(), + }; + + stream.push({ type: "start", partial: { ...message, content: [] } }); + stream.push({ type: "done", reason: "toolUse", message }); + }); + return stream; + }, + }); + + const sessionManager = SessionManager.inMemory(); + const settingsManager = SettingsManager.create(tempDir, tempDir); + const authStorage = AuthStorage.create(join(tempDir, "auth.json")); + const modelRegistry = await createModelRegistry(authStorage, tempDir); + await authStorage.modify("anthropic", async () => ({ type: "api_key", key: "test-key" })); + + session = new AgentSession({ + agent, + sessionManager, + settingsManager, + cwd: tempDir, + modelRuntime: getModelRuntime(modelRegistry), + resourceLoader: createTestResourceLoader(), + baseToolsOverride: { dummy: tool }, + }); + + const sessionWithRunner = session as unknown as { + _extensionRunner?: { + hasHandlers: (eventType: string) => boolean; + emit: (event: { type: string; message?: { role?: string } }) => Promise; + emitMessageEnd: (event: { type: string; message?: { role?: string } }) => Promise; + emitInput: ( + text: string, + images: unknown, + source: "interactive" | "rpc" | "extension", + streamingBehavior?: "steer" | "followUp", + ) => Promise<{ action: "continue" }>; + emitBeforeAgentStart: ( + prompt: string, + images: unknown, + systemPrompt: string, + systemPromptOptions: BuildSystemPromptOptions, + ) => Promise; + invalidate: (message?: string) => void; + }; + }; + sessionWithRunner._extensionRunner = { + hasHandlers: () => false, + emit: async () => {}, + emitMessageEnd: async (event) => { + if (event.type === "message_end" && event.message?.role === "assistant") { + await new Promise((resolve) => setTimeout(resolve, 40)); + } + return undefined; + }, + emitInput: async () => ({ action: "continue" }), + emitBeforeAgentStart: async () => undefined, + invalidate: () => {}, + }; + + await session.prompt("hi"); + await session.agent.waitForIdle(); + await new Promise((resolve) => setTimeout(resolve, 100)); + + const messageEntries = sessionManager.getEntries().filter((entry) => entry.type === "message"); + expect(messageEntries.map((entry) => entry.message.role)).toEqual([ + "user", + "assistant", + "toolResult", + "assistant", + ]); + }); +}); diff --git a/packages/coding-agent/test/agent-session-dynamic-provider.test.ts b/packages/coding-agent/test/agent-session-dynamic-provider.test.ts new file mode 100644 index 0000000000000000000000000000000000000000..c806efbc37d78a143334c6bd3c38091ea421a049 --- /dev/null +++ b/packages/coding-agent/test/agent-session-dynamic-provider.test.ts @@ -0,0 +1,179 @@ +import { existsSync, mkdirSync, rmSync } from "node:fs"; +import { tmpdir } from "node:os"; +import { join } from "node:path"; +import type { Provider } from "@earendil-works/pi-ai"; +import { getModel } from "@earendil-works/pi-ai/compat"; +import { afterEach, beforeEach, describe, expect, it } from "vitest"; +import { AuthStorage } from "../src/core/auth-storage.ts"; +import { ModelRuntime } from "../src/core/model-runtime.ts"; +import { DefaultResourceLoader } from "../src/core/resource-loader.ts"; +import type { ExtensionFactory } from "../src/core/sdk.ts"; +import { createAgentSession } from "../src/core/sdk.ts"; +import { SessionManager } from "../src/core/session-manager.ts"; +import { SettingsManager } from "../src/core/settings-manager.ts"; + +function nativeAnthropicProvider(baseUrl: string): Provider { + const model = { ...getModel("anthropic", "claude-sonnet-4-5")!, baseUrl }; + return { + id: "anthropic", + name: "Native Anthropic", + baseUrl, + auth: { + apiKey: { + name: "Test API key", + resolve: async () => ({ auth: { apiKey: "test-key" }, source: "test" }), + }, + }, + getModels: () => [model], + stream: () => { + throw new Error("unused"); + }, + streamSimple: () => { + throw new Error("unused"); + }, + }; +} + +describe("AgentSession dynamic provider registration", () => { + let tempDir: string; + let agentDir: string; + + beforeEach(() => { + tempDir = join(tmpdir(), `pi-dynamic-provider-test-${Date.now()}-${Math.random().toString(36).slice(2)}`); + agentDir = join(tempDir, "agent"); + mkdirSync(agentDir, { recursive: true }); + }); + + afterEach(() => { + if (tempDir && existsSync(tempDir)) { + rmSync(tempDir, { recursive: true, force: true }); + } + }); + + async function createSession(extensionFactories: ExtensionFactory[]) { + const settingsManager = SettingsManager.create(tempDir, agentDir); + const sessionManager = SessionManager.inMemory(); + const authStorage = AuthStorage.create(join(agentDir, "auth.json")); + await authStorage.modify("anthropic", async () => ({ type: "api_key", key: "test-key" })); + const modelRuntime = await ModelRuntime.create({ + credentials: authStorage, + modelsPath: join(agentDir, "models.json"), + }); + const resourceLoader = new DefaultResourceLoader({ + cwd: tempDir, + agentDir, + settingsManager, + extensionFactories, + }); + await resourceLoader.reload(); + + const { session } = await createAgentSession({ + cwd: tempDir, + agentDir, + model: getModel("anthropic", "claude-sonnet-4-5")!, + settingsManager, + sessionManager, + modelRuntime, + resourceLoader, + }); + + return session; + } + + async function capturePromptBaseUrl( + session: Awaited>, + ): Promise { + let baseUrl: string | undefined; + session.agent.streamFunction = async (model) => { + baseUrl = model.baseUrl; + throw new Error("stop"); + }; + await session.prompt("hello"); + return baseUrl; + } + + it("applies top-level registerProvider overrides to the active model", async () => { + const session = await createSession([ + (pi) => { + pi.registerProvider("anthropic", { baseUrl: "http://localhost:8080/top-level" }); + }, + ]); + + expect(session.model?.baseUrl).toBe("http://localhost:8080/top-level"); + expect(await capturePromptBaseUrl(session)).toBe("http://localhost:8080/top-level"); + + session.dispose(); + }); + + it("applies session_start registerProvider overrides to the active model", async () => { + const session = await createSession([ + (pi) => { + pi.on("session_start", () => { + pi.registerProvider("anthropic", { baseUrl: "http://localhost:8080/session-start" }); + }); + }, + ]); + + await session.bindExtensions({}); + + expect(session.model?.baseUrl).toBe("http://localhost:8080/session-start"); + expect(await capturePromptBaseUrl(session)).toBe("http://localhost:8080/session-start"); + + session.dispose(); + }); + + it("registers native pi-ai providers during extension loading", async () => { + const session = await createSession([ + (pi) => { + pi.registerProvider(nativeAnthropicProvider("http://localhost:8080/native-top-level")); + }, + ]); + + expect(session.model?.baseUrl).toBe("http://localhost:8080/native-top-level"); + expect(await capturePromptBaseUrl(session)).toBe("http://localhost:8080/native-top-level"); + + session.dispose(); + }); + + it("applies command-time registerProvider overrides without reload", async () => { + const session = await createSession([ + (pi) => { + pi.registerCommand("use-proxy", { + description: "Use proxy", + handler: async () => { + pi.registerProvider("anthropic", { baseUrl: "http://localhost:8080/command" }); + }, + }); + }, + ]); + + await session.bindExtensions({}); + await session.prompt("/use-proxy"); + + expect(session.model?.baseUrl).toBe("http://localhost:8080/command"); + expect(await capturePromptBaseUrl(session)).toBe("http://localhost:8080/command"); + + session.dispose(); + }); + + it("registers native pi-ai providers at command time", async () => { + const session = await createSession([ + (pi) => { + pi.registerCommand("use-native", { + description: "Use native provider", + handler: async () => { + pi.registerProvider(nativeAnthropicProvider("http://localhost:8080/native-command")); + }, + }); + }, + ]); + + await session.bindExtensions({}); + await session.prompt("/use-native"); + + expect(session.model?.baseUrl).toBe("http://localhost:8080/native-command"); + expect(await capturePromptBaseUrl(session)).toBe("http://localhost:8080/native-command"); + + session.dispose(); + }); +}); diff --git a/packages/coding-agent/test/agent-session-dynamic-tools.test.ts b/packages/coding-agent/test/agent-session-dynamic-tools.test.ts new file mode 100644 index 0000000000000000000000000000000000000000..5edc1f6c097d7661f9c5f2e33e6d5952cd7ece42 --- /dev/null +++ b/packages/coding-agent/test/agent-session-dynamic-tools.test.ts @@ -0,0 +1,256 @@ +import { existsSync, mkdirSync, rmSync } from "node:fs"; +import { tmpdir } from "node:os"; +import { join } from "node:path"; +import { getModel } from "@earendil-works/pi-ai/compat"; +import { Type } from "typebox"; +import { afterEach, beforeEach, describe, expect, it } from "vitest"; +import { DefaultResourceLoader } from "../src/core/resource-loader.ts"; +import { createAgentSession } from "../src/core/sdk.ts"; +import { SessionManager } from "../src/core/session-manager.ts"; +import { SettingsManager } from "../src/core/settings-manager.ts"; +import { createBashTool } from "../src/core/tools/bash.ts"; + +describe("AgentSession dynamic tool registration", () => { + let tempDir: string; + let agentDir: string; + + beforeEach(() => { + tempDir = join(tmpdir(), `pi-dynamic-tool-test-${Date.now()}-${Math.random().toString(36).slice(2)}`); + agentDir = join(tempDir, "agent"); + mkdirSync(agentDir, { recursive: true }); + }); + + afterEach(() => { + if (tempDir && existsSync(tempDir)) { + rmSync(tempDir, { recursive: true, force: true }); + } + }); + + it("exposes session state before custom bash spawn hooks and supports opting out", async () => { + const settingsManager = SettingsManager.create(tempDir, agentDir); + const sessionManager = SessionManager.create(tempDir, join(agentDir, "sessions"), { id: "bash-env-test" }); + let sessionEnv: NodeJS.ProcessEnv | undefined; + let optedOutEnv: NodeJS.ProcessEnv | undefined; + const resourceLoader = new DefaultResourceLoader({ + cwd: tempDir, + agentDir, + settingsManager, + extensionFactories: [ + (pi) => { + pi.registerTool( + createBashTool(tempDir, { + spawnHook: (ctx) => { + sessionEnv = ctx.env; + return ctx; + }, + }), + ); + pi.registerTool({ + ...createBashTool(tempDir, { + exposeSessionEnvironment: false, + spawnHook: (ctx) => { + optedOutEnv = ctx.env; + return ctx; + }, + }), + name: "bash_without_session_env", + label: "bash without session env", + }); + }, + ], + }); + await resourceLoader.reload(); + + const model = getModel("anthropic", "claude-sonnet-4-5")!; + const { session } = await createAgentSession({ + cwd: tempDir, + agentDir, + model, + thinkingLevel: "high", + settingsManager, + sessionManager, + resourceLoader, + }); + + const bashTool = session.agent.state.tools.find((tool) => tool.name === "bash")!; + expect(session.systemPrompt).toContain( + "You can inspect PI_* environment variables for current model and session details.", + ); + await bashTool.execute("bash-env", { command: "printf ok" }); + expect(sessionEnv).toMatchObject({ + PI_SESSION_ID: session.sessionId, + PI_SESSION_FILE: session.sessionFile, + PI_PROVIDER: model.provider, + PI_MODEL: model.id, + PI_REASONING_LEVEL: session.thinkingLevel, + }); + + const optedOutBashTool = session.agent.state.tools.find((tool) => tool.name === "bash_without_session_env")!; + await optedOutBashTool.execute("bash-no-env", { command: "printf ok" }); + expect(optedOutEnv).not.toHaveProperty("PI_SESSION_ID"); + expect(optedOutEnv).not.toHaveProperty("PI_SESSION_FILE"); + expect(optedOutEnv).not.toHaveProperty("PI_PROVIDER"); + expect(optedOutEnv).not.toHaveProperty("PI_MODEL"); + expect(optedOutEnv).not.toHaveProperty("PI_REASONING_LEVEL"); + + session.dispose(); + }); + + it("refreshes tool registry when tools are registered after initialization", async () => { + const settingsManager = SettingsManager.create(tempDir, agentDir); + const sessionManager = SessionManager.inMemory(); + + const resourceLoader = new DefaultResourceLoader({ + cwd: tempDir, + agentDir, + settingsManager, + extensionFactories: [ + (pi) => { + pi.on("session_start", () => { + pi.registerTool({ + name: "dynamic_tool", + label: "Dynamic Tool", + description: "Tool registered from session_start", + promptSnippet: "Run dynamic test behavior", + promptGuidelines: ["Use dynamic_tool when the user asks for dynamic behavior tests."], + parameters: Type.Object({}), + execute: async () => ({ + content: [{ type: "text", text: "ok" }], + details: {}, + }), + }); + }); + }, + ], + }); + await resourceLoader.reload(); + + const { session } = await createAgentSession({ + cwd: tempDir, + agentDir, + model: getModel("anthropic", "claude-sonnet-4-5")!, + settingsManager, + sessionManager, + resourceLoader, + }); + + expect(session.getAllTools().map((tool) => tool.name)).not.toContain("dynamic_tool"); + + await session.bindExtensions({}); + + const allTools = session.getAllTools(); + const dynamicTool = allTools.find((tool) => tool.name === "dynamic_tool"); + const readTool = allTools.find((tool) => tool.name === "read"); + + expect(allTools.map((tool) => tool.name)).toContain("dynamic_tool"); + expect(dynamicTool?.promptGuidelines).toEqual([ + "Use dynamic_tool when the user asks for dynamic behavior tests.", + ]); + expect(dynamicTool?.sourceInfo).toMatchObject({ + path: "", + source: "inline", + scope: "temporary", + origin: "top-level", + }); + expect(readTool?.sourceInfo).toMatchObject({ + path: "", + source: "builtin", + scope: "temporary", + origin: "top-level", + }); + expect(session.getActiveToolNames()).toContain("dynamic_tool"); + expect(session.systemPrompt).toContain("- dynamic_tool: Run dynamic test behavior"); + expect(session.systemPrompt).toContain("- Use dynamic_tool when the user asks for dynamic behavior tests."); + + session.dispose(); + }); + + it("returns source metadata for SDK custom tools", async () => { + const settingsManager = SettingsManager.create(tempDir, agentDir); + const sessionManager = SessionManager.inMemory(); + const resourceLoader = new DefaultResourceLoader({ + cwd: tempDir, + agentDir, + settingsManager, + }); + await resourceLoader.reload(); + + const { session } = await createAgentSession({ + cwd: tempDir, + agentDir, + model: getModel("anthropic", "claude-sonnet-4-5")!, + settingsManager, + sessionManager, + resourceLoader, + customTools: [ + { + name: "sdk_tool", + label: "SDK Tool", + description: "Tool registered through createAgentSession", + parameters: Type.Object({}), + execute: async () => ({ + content: [{ type: "text", text: "ok" }], + details: {}, + }), + }, + ], + }); + + const sdkTool = session.getAllTools().find((tool) => tool.name === "sdk_tool"); + expect(sdkTool?.sourceInfo).toMatchObject({ + path: "", + source: "sdk", + scope: "temporary", + origin: "top-level", + }); + expect(session.getActiveToolNames()).toContain("sdk_tool"); + + session.dispose(); + }); + + it("keeps custom tools active but omits them from available tools when promptSnippet is not provided", async () => { + const settingsManager = SettingsManager.create(tempDir, agentDir); + const sessionManager = SessionManager.inMemory(); + + const resourceLoader = new DefaultResourceLoader({ + cwd: tempDir, + agentDir, + settingsManager, + extensionFactories: [ + (pi) => { + pi.on("session_start", () => { + pi.registerTool({ + name: "hidden_tool", + label: "Hidden Tool", + description: "Description should not appear in available tools", + parameters: Type.Object({}), + execute: async () => ({ + content: [{ type: "text", text: "ok" }], + details: {}, + }), + }); + }); + }, + ], + }); + await resourceLoader.reload(); + + const { session } = await createAgentSession({ + cwd: tempDir, + agentDir, + model: getModel("anthropic", "claude-sonnet-4-5")!, + settingsManager, + sessionManager, + resourceLoader, + }); + + await session.bindExtensions({}); + + expect(session.getAllTools().map((tool) => tool.name)).toContain("hidden_tool"); + expect(session.getActiveToolNames()).toContain("hidden_tool"); + expect(session.systemPrompt).not.toContain("hidden_tool"); + expect(session.systemPrompt).not.toContain("Description should not appear in available tools"); + + session.dispose(); + }); +}); diff --git a/packages/coding-agent/test/agent-session-retry.test.ts b/packages/coding-agent/test/agent-session-retry.test.ts new file mode 100644 index 0000000000000000000000000000000000000000..9d80eb9465e03ec58d3df349f18592ff9f3cb9b0 --- /dev/null +++ b/packages/coding-agent/test/agent-session-retry.test.ts @@ -0,0 +1,337 @@ +import { existsSync, mkdirSync, rmSync } from "node:fs"; +import { tmpdir } from "node:os"; +import { join } from "node:path"; +import { Agent, type AgentEvent, type AgentTool } from "@earendil-works/pi-agent-core"; +import { type AssistantMessage, type AssistantMessageEvent, EventStream, getModel } from "@earendil-works/pi-ai/compat"; +import { Type } from "typebox"; +import { afterEach, beforeEach, describe, expect, it } from "vitest"; +import { AgentSession } from "../src/core/agent-session.ts"; +import { AuthStorage } from "../src/core/auth-storage.ts"; +import { SessionManager } from "../src/core/session-manager.ts"; +import { SettingsManager } from "../src/core/settings-manager.ts"; +import { createModelRegistry, getModelRuntime } from "./model-runtime-test-utils.ts"; +import { createTestResourceLoader } from "./utilities.ts"; + +class MockAssistantStream extends EventStream { + constructor() { + super( + (event) => event.type === "done" || event.type === "error", + (event) => { + if (event.type === "done") return event.message; + if (event.type === "error") return event.error; + throw new Error("Unexpected event type"); + }, + ); + } +} + +function createAssistantMessage(text: string, overrides?: Partial): AssistantMessage { + return { + role: "assistant", + content: [{ type: "text", text }], + api: "anthropic-messages", + provider: "anthropic", + model: "mock", + usage: { + input: 0, + output: 0, + cacheRead: 0, + cacheWrite: 0, + totalTokens: 0, + cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 }, + }, + stopReason: "stop", + timestamp: Date.now(), + ...overrides, + }; +} + +type SessionWithExtensionEmitHook = { + _emitExtensionEvent: (event: AgentEvent) => Promise; +}; + +describe("AgentSession retry", () => { + let session: AgentSession; + let tempDir: string; + + beforeEach(async () => { + tempDir = join(tmpdir(), `pi-retry-test-${Date.now()}`); + mkdirSync(tempDir, { recursive: true }); + }); + + afterEach(() => { + if (session) { + session.dispose(); + } + if (tempDir && existsSync(tempDir)) { + rmSync(tempDir, { recursive: true }); + } + }); + + async function createSession(options?: { + failCount?: number; + maxRetries?: number; + maxAgentDelayMs?: number; + delayAssistantMessageEndMs?: number; + }) { + const failCount = options?.failCount ?? 1; + const maxRetries = options?.maxRetries ?? 3; + const maxAgentDelayMs = options?.maxAgentDelayMs ?? 60000; + const delayAssistantMessageEndMs = options?.delayAssistantMessageEndMs ?? 0; + let callCount = 0; + + const model = getModel("anthropic", "claude-sonnet-4-5")!; + const agent = new Agent({ + getApiKey: () => "test-key", + initialState: { model, systemPrompt: "Test", tools: [] }, + streamFn: () => { + callCount++; + const stream = new MockAssistantStream(); + queueMicrotask(() => { + if (callCount <= failCount) { + const msg = createAssistantMessage("", { + stopReason: "error", + errorMessage: "overloaded_error", + }); + stream.push({ type: "start", partial: msg }); + stream.push({ type: "error", reason: "error", error: msg }); + } else { + const msg = createAssistantMessage("Success"); + stream.push({ type: "start", partial: msg }); + stream.push({ type: "done", reason: "stop", message: msg }); + } + }); + return stream; + }, + }); + + const sessionManager = SessionManager.inMemory(); + const settingsManager = SettingsManager.create(tempDir, tempDir); + const authStorage = AuthStorage.create(join(tempDir, "auth.json")); + const modelRegistry = await createModelRegistry(authStorage, tempDir); + await authStorage.modify("anthropic", async () => ({ type: "api_key", key: "test-key" })); + settingsManager.applyOverrides({ retry: { enabled: true, maxRetries, baseDelayMs: 1, maxAgentDelayMs } }); + + session = new AgentSession({ + agent, + sessionManager, + settingsManager, + cwd: tempDir, + modelRuntime: getModelRuntime(modelRegistry), + resourceLoader: createTestResourceLoader(), + }); + + if (delayAssistantMessageEndMs > 0) { + const sessionWithHook = session as unknown as SessionWithExtensionEmitHook; + const original = sessionWithHook._emitExtensionEvent.bind(sessionWithHook); + sessionWithHook._emitExtensionEvent = async (event: AgentEvent) => { + if (event.type === "message_end" && event.message.role === "assistant") { + await new Promise((resolve) => setTimeout(resolve, delayAssistantMessageEndMs)); + } + await original(event); + }; + } + + return { session, getCallCount: () => callCount }; + } + + it("retries after a transient error and succeeds", async () => { + const created = await createSession({ failCount: 1 }); + const events: string[] = []; + created.session.subscribe((event) => { + if (event.type === "auto_retry_start") events.push(`start:${event.attempt}`); + if (event.type === "auto_retry_end") events.push(`end:success=${event.success}`); + }); + + await created.session.prompt("Test"); + + expect(created.getCallCount()).toBe(2); + expect(events).toEqual(["start:1", "end:success=true"]); + expect(created.session.isRetrying).toBe(false); + }); + + it("exhausts max retries and emits failure", async () => { + const created = await createSession({ failCount: 99, maxRetries: 2 }); + const events: string[] = []; + created.session.subscribe((event) => { + if (event.type === "auto_retry_start") events.push(`start:${event.attempt}`); + if (event.type === "auto_retry_end") events.push(`end:success=${event.success}`); + }); + + await created.session.prompt("Test"); + + expect(created.getCallCount()).toBe(3); + expect(events).toContain("start:1"); + expect(events).toContain("start:2"); + expect(events).toContain("end:success=false"); + expect(created.session.isRetrying).toBe(false); + }); + + it("caps agent retry delay", async () => { + // Regression for #8826. + const created = await createSession({ failCount: 4, maxRetries: 5, maxAgentDelayMs: 5 }); + const delays: number[] = []; + created.session.subscribe((event) => { + if (event.type === "auto_retry_start") delays.push(event.delayMs); + }); + + await created.session.prompt("Test"); + + expect(delays).toEqual([1, 2, 4, 5]); + }); + + it("prompt waits for retry completion even when assistant message_end handling is delayed", async () => { + const created = await createSession({ failCount: 1, delayAssistantMessageEndMs: 40 }); + + await created.session.prompt("Test"); + + expect(created.getCallCount()).toBe(2); + expect(created.session.isRetrying).toBe(false); + }); + + it("retries provider network_error failures", async () => { + const created = await createSession({ failCount: 0 }); + let callCount = 0; + const streamFn = () => { + callCount++; + const stream = new MockAssistantStream(); + queueMicrotask(() => { + if (callCount === 1) { + const msg = createAssistantMessage("", { + stopReason: "error", + errorMessage: "Provider finish_reason: network_error", + }); + stream.push({ type: "start", partial: msg }); + stream.push({ type: "error", reason: "error", error: msg }); + return; + } + + const msg = createAssistantMessage("Recovered after retry"); + stream.push({ type: "start", partial: msg }); + stream.push({ type: "done", reason: "stop", message: msg }); + }); + return stream; + }; + created.session.dispose(); + + const model = getModel("anthropic", "claude-sonnet-4-5")!; + const agent = new Agent({ + getApiKey: () => "test-key", + initialState: { model, systemPrompt: "Test", tools: [] }, + streamFn: streamFn, + }); + const sessionManager = SessionManager.inMemory(); + const settingsManager = SettingsManager.create(tempDir, tempDir); + const authStorage = AuthStorage.create(join(tempDir, "auth.json")); + const modelRegistry = await createModelRegistry(authStorage, tempDir); + await authStorage.modify("anthropic", async () => ({ type: "api_key", key: "test-key" })); + settingsManager.applyOverrides({ retry: { enabled: true, maxRetries: 3, baseDelayMs: 1 } }); + session = new AgentSession({ + agent, + sessionManager, + settingsManager, + cwd: tempDir, + modelRuntime: getModelRuntime(modelRegistry), + resourceLoader: createTestResourceLoader(), + }); + + const events: string[] = []; + session.subscribe((event) => { + if (event.type === "auto_retry_start") events.push(`start:${event.attempt}`); + if (event.type === "auto_retry_end") events.push(`end:success=${event.success}`); + }); + + await session.prompt("Test"); + + expect(callCount).toBe(2); + expect(events).toEqual(["start:1", "end:success=true"]); + }); + + it("prompt waits for full agent loop when retry produces tool calls", async () => { + // Regression: when auto-retry fires and the retry response includes tool_use, + // session.prompt() must wait for the entire tool loop to finish before returning. + // Previously, _resolveRetry() on the first successful message_end would unblock + // waitForRetry() while the agent was still executing tools. + let callCount = 0; + const toolExecuted = { value: false }; + + const echoTool: AgentTool = { + name: "echo", + label: "Echo", + description: "Echo text back", + parameters: Type.Object({ text: Type.String() }), + execute: async () => { + toolExecuted.value = true; + return { content: [{ type: "text", text: "echoed" }], details: undefined }; + }, + }; + + const model = getModel("anthropic", "claude-sonnet-4-5")!; + const agent = new Agent({ + getApiKey: () => "test-key", + initialState: { model, systemPrompt: "Test", tools: [] }, + streamFn: () => { + callCount++; + const stream = new MockAssistantStream(); + queueMicrotask(() => { + if (callCount === 1) { + // First call: overloaded error + const msg = createAssistantMessage("", { + stopReason: "error", + errorMessage: "overloaded_error", + }); + stream.push({ type: "start", partial: msg }); + stream.push({ type: "error", reason: "error", error: msg }); + } else if (callCount === 2) { + // Second call (retry): text + tool_use + const msg: AssistantMessage = { + ...createAssistantMessage("Looking that up now."), + stopReason: "toolUse", + content: [ + { type: "text", text: "Looking that up now." }, + { type: "toolCall", id: "call_1", name: "echo", arguments: { text: "hello" } }, + ], + }; + stream.push({ type: "start", partial: msg }); + stream.push({ type: "done", reason: "toolUse", message: msg }); + } else { + // Third call (after tool result): final response + const msg = createAssistantMessage("Final answer."); + stream.push({ type: "start", partial: msg }); + stream.push({ type: "done", reason: "stop", message: msg }); + } + }); + return stream; + }, + }); + + const sessionManager = SessionManager.inMemory(); + const settingsManager = SettingsManager.create(tempDir, tempDir); + const authStorage = AuthStorage.create(join(tempDir, "auth.json")); + const modelRegistry = await createModelRegistry(authStorage, tempDir); + await authStorage.modify("anthropic", async () => ({ type: "api_key", key: "test-key" })); + settingsManager.applyOverrides({ retry: { enabled: true, maxRetries: 3, baseDelayMs: 1 } }); + + session = new AgentSession({ + agent, + sessionManager, + settingsManager, + cwd: tempDir, + modelRuntime: getModelRuntime(modelRegistry), + resourceLoader: createTestResourceLoader(), + baseToolsOverride: { echo: echoTool }, + }); + + await session.prompt("Test"); + + // All three LLM calls must have completed + expect(callCount).toBe(3); + // Tool must have been executed + expect(toolExecuted.value).toBe(true); + // Agent must not be streaming after prompt returns + expect(session.isStreaming).toBe(false); + // A follow-up prompt must work (no "Agent is already processing" error) + await session.prompt("Follow-up"); + expect(callCount).toBe(4); + }); +}); diff --git a/packages/coding-agent/test/agent-session-runtime-events.test.ts b/packages/coding-agent/test/agent-session-runtime-events.test.ts new file mode 100644 index 0000000000000000000000000000000000000000..32bb828ff54e5e593ea6678951a1ec84da427dbf --- /dev/null +++ b/packages/coding-agent/test/agent-session-runtime-events.test.ts @@ -0,0 +1,257 @@ +import { existsSync, mkdirSync, rmSync } from "node:fs"; +import { tmpdir } from "node:os"; +import { join } from "node:path"; +import { fauxAssistantMessage, registerFauxProvider } from "@earendil-works/pi-ai/compat"; +import { afterEach, describe, expect, it } from "vitest"; +import { + type CreateAgentSessionRuntimeFactory, + createAgentSessionFromServices, + createAgentSessionRuntime, + createAgentSessionServices, +} from "../src/core/agent-session-runtime.ts"; +import { AuthStorage } from "../src/core/auth-storage.ts"; +import { ModelRuntime } from "../src/core/model-runtime.ts"; +import { SessionManager } from "../src/core/session-manager.ts"; +import type { + ExtensionFactory, + SessionBeforeForkEvent, + SessionBeforeSwitchEvent, + SessionShutdownEvent, + SessionStartEvent, +} from "../src/index.ts"; + +type RecordedSessionEvent = + | SessionBeforeSwitchEvent + | SessionBeforeForkEvent + | SessionShutdownEvent + | SessionStartEvent; + +describe("AgentSessionRuntime session lifecycle events", () => { + const cleanups: Array<() => Promise | void> = []; + + afterEach(async () => { + while (cleanups.length > 0) { + await cleanups.pop()?.(); + } + }); + + async function createRuntimeHost(extensionFactory: ExtensionFactory) { + const tempDir = join(tmpdir(), `pi-runtime-events-${Date.now()}-${Math.random().toString(36).slice(2)}`); + mkdirSync(tempDir, { recursive: true }); + + const faux = registerFauxProvider(); + faux.setResponses([fauxAssistantMessage("one"), fauxAssistantMessage("two"), fauxAssistantMessage("three")]); + + const authStorage = AuthStorage.inMemory(); + await authStorage.modify(faux.getModel().provider, async () => ({ type: "api_key", key: "faux-key" })); + const modelRuntime = await ModelRuntime.create({ + credentials: authStorage, + modelsPath: join(tempDir, "models.json"), + }); + const model = faux.getModel(); + modelRuntime.registerProvider(model.provider, { + baseUrl: model.baseUrl, + api: model.api, + models: [ + { + id: model.id, + name: model.name, + api: model.api, + reasoning: model.reasoning, + input: model.input, + cost: model.cost, + contextWindow: model.contextWindow, + maxTokens: model.maxTokens, + baseUrl: model.baseUrl, + }, + ], + }); + + const runtimeOptions = { + agentDir: tempDir, + modelRuntime, + model: faux.getModel(), + resourceLoaderOptions: { + extensionFactories: [extensionFactory], + noSkills: true, + noPromptTemplates: true, + noThemes: true, + }, + }; + const createRuntime: CreateAgentSessionRuntimeFactory = async ({ cwd, sessionManager, sessionStartEvent }) => { + const services = await createAgentSessionServices({ + ...runtimeOptions, + cwd, + }); + return { + ...(await createAgentSessionFromServices({ + services, + sessionManager, + sessionStartEvent, + model: faux.getModel(), + })), + services, + diagnostics: services.diagnostics, + }; + }; + const runtimeHost = await createAgentSessionRuntime(createRuntime, { + cwd: tempDir, + agentDir: tempDir, + sessionManager: SessionManager.create(tempDir), + }); + await runtimeHost.session.bindExtensions({}); + + cleanups.push(async () => { + await runtimeHost.dispose(); + faux.unregister(); + if (existsSync(tempDir)) { + rmSync(tempDir, { recursive: true, force: true }); + } + }); + + return { runtimeHost, faux }; + } + + it("emits session_before_switch and session_start for new and resume flows", async () => { + const events: RecordedSessionEvent[] = []; + const { runtimeHost } = await createRuntimeHost((pi) => { + pi.on("session_before_switch", (event) => { + events.push(event); + }); + pi.on("session_shutdown", (event) => { + events.push(event); + }); + pi.on("session_start", (event) => { + events.push(event); + }); + }); + + expect(events).toEqual([{ type: "session_start", reason: "startup" }]); + events.length = 0; + + await runtimeHost.session.prompt("hello"); + const originalSessionFile = runtimeHost.session.sessionFile; + expect(originalSessionFile).toBeTruthy(); + + const newSessionResult = await runtimeHost.newSession(); + expect(newSessionResult.cancelled).toBe(false); + await runtimeHost.session.bindExtensions({}); + const secondSessionFile = runtimeHost.session.sessionFile; + expect(events).toEqual([ + { type: "session_before_switch", reason: "new", targetSessionFile: undefined }, + { type: "session_shutdown", reason: "new", targetSessionFile: secondSessionFile }, + { type: "session_start", reason: "new", previousSessionFile: originalSessionFile }, + ]); + + events.length = 0; + expect(secondSessionFile).toBeTruthy(); + + const switchResult = await runtimeHost.switchSession(originalSessionFile!); + expect(switchResult.cancelled).toBe(false); + await runtimeHost.session.bindExtensions({}); + expect(events).toEqual([ + { type: "session_before_switch", reason: "resume", targetSessionFile: originalSessionFile }, + { type: "session_shutdown", reason: "resume", targetSessionFile: originalSessionFile }, + { type: "session_start", reason: "resume", previousSessionFile: secondSessionFile }, + ]); + }); + + it("honors session_before_switch cancellation", async () => { + const events: RecordedSessionEvent[] = []; + const { runtimeHost } = await createRuntimeHost((pi) => { + pi.on("session_before_switch", (event) => { + events.push(event); + return { cancel: true }; + }); + pi.on("session_start", (event) => { + events.push(event); + }); + }); + + expect(events).toEqual([{ type: "session_start", reason: "startup" }]); + events.length = 0; + + await runtimeHost.session.prompt("hello"); + const originalSessionFile = runtimeHost.session.sessionFile; + + const result = await runtimeHost.newSession(); + expect(result.cancelled).toBe(true); + expect(runtimeHost.session.sessionFile).toBe(originalSessionFile); + expect(events).toEqual([{ type: "session_before_switch", reason: "new", targetSessionFile: undefined }]); + }); + + it("runs beforeSessionInvalidate after session_shutdown and before rebindSession", async () => { + const phases: string[] = []; + const { runtimeHost } = await createRuntimeHost((pi) => { + pi.on("session_shutdown", () => { + phases.push("session_shutdown"); + }); + }); + const oldSession = runtimeHost.session; + runtimeHost.setBeforeSessionInvalidate(() => { + phases.push("beforeSessionInvalidate"); + expect(oldSession.extensionRunner.createContext().cwd).toBe(oldSession.sessionManager.getCwd()); + }); + runtimeHost.setRebindSession(async () => { + phases.push("rebindSession"); + }); + + await runtimeHost.newSession(); + + expect(phases).toEqual(["session_shutdown", "beforeSessionInvalidate", "rebindSession"]); + expect(() => oldSession.extensionRunner.createContext().cwd).toThrow( + "This extension ctx is stale after session replacement or reload. Do not use a captured pi or command ctx after ctx.newSession(), ctx.fork(), ctx.switchSession(), or ctx.reload(). For newSession, fork, and switchSession, move post-replacement work into withSession and use the ctx passed to withSession. For reload, do not use the old ctx after await ctx.reload().", + ); + runtimeHost.setBeforeSessionInvalidate(undefined); + runtimeHost.setRebindSession(undefined); + }); + + it("emits session_before_fork and session_start and honors cancellation", async () => { + const events: RecordedSessionEvent[] = []; + let cancelNextFork = false; + const { runtimeHost } = await createRuntimeHost((pi) => { + pi.on("session_before_fork", (event) => { + events.push(event); + if (cancelNextFork) { + cancelNextFork = false; + return { cancel: true }; + } + }); + pi.on("session_shutdown", (event) => { + events.push(event); + }); + pi.on("session_start", (event) => { + events.push(event); + }); + }); + + expect(events).toEqual([{ type: "session_start", reason: "startup" }]); + events.length = 0; + + await runtimeHost.session.prompt("hello"); + const userMessage = runtimeHost.session.getUserMessagesForForking()[0]; + const previousSessionFile = runtimeHost.session.sessionFile; + + const successResult = await runtimeHost.fork(userMessage.entryId); + expect(successResult.cancelled).toBe(false); + expect(successResult.selectedText).toBe("hello"); + await runtimeHost.session.bindExtensions({}); + expect(events).toEqual([ + { type: "session_before_fork", entryId: userMessage.entryId, position: "before" }, + { type: "session_shutdown", reason: "fork", targetSessionFile: runtimeHost.session.sessionFile }, + { type: "session_start", reason: "fork", previousSessionFile }, + ]); + + events.length = 0; + cancelNextFork = true; + const cancelResult = await runtimeHost.fork(userMessage.entryId); + expect(cancelResult).toEqual({ cancelled: true }); + expect(events).toEqual([{ type: "session_before_fork", entryId: userMessage.entryId, position: "before" }]); + + events.length = 0; + cancelNextFork = true; + const cancelAtResult = await runtimeHost.fork("missing-entry", { position: "at" }); + expect(cancelAtResult).toEqual({ cancelled: true }); + expect(events).toEqual([{ type: "session_before_fork", entryId: "missing-entry", position: "at" }]); + }); +}); diff --git a/packages/coding-agent/test/agent-session-stats.test.ts b/packages/coding-agent/test/agent-session-stats.test.ts new file mode 100644 index 0000000000000000000000000000000000000000..434524d4721fdd098753eddd135deee54daae52e --- /dev/null +++ b/packages/coding-agent/test/agent-session-stats.test.ts @@ -0,0 +1,283 @@ +import { Agent } from "@earendil-works/pi-agent-core"; +import { + type AssistantMessage, + getModel, + streamSimple, + type ToolResultMessage, + type Usage, +} from "@earendil-works/pi-ai/compat"; +import { describe, expect, it } from "vitest"; +import { AgentSession } from "../src/core/agent-session.ts"; +import { AuthStorage } from "../src/core/auth-storage.ts"; +import { SessionManager } from "../src/core/session-manager.ts"; +import { SettingsManager } from "../src/core/settings-manager.ts"; +import { getUsageCostBreakdown } from "../src/core/usage-totals.ts"; +import { createInMemoryModelRegistry, getModelRuntime } from "./model-runtime-test-utils.ts"; +import { createTestResourceLoader } from "./utilities.ts"; + +const model = getModel("anthropic", "claude-sonnet-4-5")!; + +function createUsage(totalTokens: number): Usage { + return { + input: totalTokens, + output: 0, + cacheRead: 0, + cacheWrite: 0, + totalTokens, + cost: { + input: 0, + output: 0, + cacheRead: 0, + cacheWrite: 0, + total: 0, + }, + }; +} + +function createAssistantMessage(text: string, totalTokens: number, timestamp: number): AssistantMessage { + return { + role: "assistant", + content: [{ type: "text", text }], + api: model.api, + provider: model.provider, + model: model.id, + usage: createUsage(totalTokens), + stopReason: "stop", + timestamp, + }; +} + +function createUserMessage(text: string, timestamp: number) { + return { + role: "user" as const, + content: text, + timestamp, + }; +} + +function createToolResultMessage(usage: Usage): ToolResultMessage { + return { + role: "toolResult", + toolCallId: "tool-call-1", + toolName: "test_tool", + content: [{ type: "text", text: "tool result" }], + usage, + isError: false, + timestamp: 1, + }; +} + +async function createSession() { + const settingsManager = SettingsManager.inMemory(); + const sessionManager = SessionManager.inMemory(); + const authStorage = AuthStorage.inMemory(); + await authStorage.modify("anthropic", async () => ({ type: "api_key", key: "test-key" })); + const session = new AgentSession({ + agent: new Agent({ + getApiKey: () => "test-key", + streamFn: streamSimple, + initialState: { + model, + systemPrompt: "You are a helpful assistant.", + tools: [], + thinkingLevel: "high", + }, + }), + sessionManager, + settingsManager, + cwd: process.cwd(), + modelRuntime: getModelRuntime(await createInMemoryModelRegistry(authStorage)), + resourceLoader: createTestResourceLoader(), + }); + + return { session, sessionManager }; +} + +function syncAgentMessages(session: AgentSession, sessionManager: SessionManager): void { + session.agent.state.messages = sessionManager.buildSessionContext().messages; +} + +describe("AgentSession.getSessionStats", () => { + it("exposes the current context usage alongside token totals", async () => { + const { session, sessionManager } = await createSession(); + + try { + sessionManager.appendMessage(createUserMessage("hello", 1)); + sessionManager.appendMessage(createAssistantMessage("hi", 200, 2)); + syncAgentMessages(session, sessionManager); + + const stats = session.getSessionStats(); + expect(stats.contextUsage).toEqual(session.getContextUsage()); + expect(stats.contextUsage?.tokens).toBe(200); + expect(stats.contextUsage?.contextWindow).toBe(model.contextWindow); + expect(stats.contextUsage?.percent).toBe((200 / model.contextWindow) * 100); + } finally { + session.dispose(); + } + }); + + it("reports unknown current context usage immediately after compaction", async () => { + const { session, sessionManager } = await createSession(); + + try { + sessionManager.appendMessage(createUserMessage("first", 1)); + sessionManager.appendMessage(createAssistantMessage("response1", 180_000, 2)); + const keptUserId = sessionManager.appendMessage(createUserMessage("second", 3)); + sessionManager.appendMessage(createAssistantMessage("response2", 195_000, 4)); + sessionManager.appendCompaction("summary", keptUserId, 195_000); + sessionManager.appendMessage(createUserMessage("third", 5)); + syncAgentMessages(session, sessionManager); + + const stats = session.getSessionStats(); + // Totals cover ALL entries, including history compacted away (180k + 195k). + expect(stats.tokens.input).toBe(375_000); + expect(stats.contextUsage).toBeDefined(); + expect(stats.contextUsage?.tokens).toBeNull(); + expect(stats.contextUsage?.percent).toBeNull(); + } finally { + session.dispose(); + } + }); + + it("uses post-compaction usage for current context instead of stale kept usage", async () => { + const { session, sessionManager } = await createSession(); + + try { + sessionManager.appendMessage(createUserMessage("first", 1)); + sessionManager.appendMessage(createAssistantMessage("response1", 180_000, 2)); + const keptUserId = sessionManager.appendMessage(createUserMessage("second", 3)); + sessionManager.appendMessage(createAssistantMessage("response2", 195_000, 4)); + sessionManager.appendCompaction("summary", keptUserId, 195_000); + sessionManager.appendMessage(createUserMessage("third", 5)); + sessionManager.appendMessage(createAssistantMessage("response3", 25_000, 6)); + syncAgentMessages(session, sessionManager); + + const stats = session.getSessionStats(); + // Totals cover ALL entries, including history compacted away (180k + 195k + 25k). + expect(stats.tokens.input).toBe(400_000); + expect(stats.contextUsage).toBeDefined(); + expect(stats.contextUsage?.tokens).toBe(25_000); + expect(stats.contextUsage?.percent).toBe((25_000 / model.contextWindow) * 100); + } finally { + session.dispose(); + } + }); + + it("includes branch summary usage in session totals", async () => { + const { session, sessionManager } = await createSession(); + + try { + sessionManager.branchWithSummary(null, "summary", undefined, false, { + input: 10, + output: 20, + cacheRead: 30, + cacheWrite: 40, + totalTokens: 100, + cost: { input: 0.1, output: 0.2, cacheRead: 0.3, cacheWrite: 0.4, total: 1 }, + }); + syncAgentMessages(session, sessionManager); + + const stats = session.getSessionStats(); + expect(stats.tokens).toEqual({ input: 10, output: 20, cacheRead: 30, cacheWrite: 40, total: 100 }); + expect(stats.cost).toBe(1); + } finally { + session.dispose(); + } + }); + + it("includes compaction usage in session totals", async () => { + const { session, sessionManager } = await createSession(); + + try { + const firstKeptEntryId = sessionManager.appendMessage(createUserMessage("hello", 1)); + sessionManager.appendCompaction("summary", firstKeptEntryId, 100, undefined, false, { + input: 10, + output: 20, + cacheRead: 30, + cacheWrite: 40, + totalTokens: 100, + cost: { input: 0.1, output: 0.2, cacheRead: 0.3, cacheWrite: 0.4, total: 1 }, + }); + syncAgentMessages(session, sessionManager); + + const stats = session.getSessionStats(); + expect(stats.tokens).toEqual({ input: 10, output: 20, cacheRead: 30, cacheWrite: 40, total: 100 }); + expect(stats.cost).toBe(1); + } finally { + session.dispose(); + } + }); + + it("includes tool result usage in session totals", async () => { + const { session, sessionManager } = await createSession(); + + try { + sessionManager.appendMessage( + createToolResultMessage({ + input: 10, + output: 20, + cacheRead: 30, + cacheWrite: 40, + totalTokens: 100, + cost: { input: 0.1, output: 0.2, cacheRead: 0.3, cacheWrite: 0.4, total: 1 }, + }), + ); + syncAgentMessages(session, sessionManager); + + const stats = session.getSessionStats(); + expect(stats.tokens).toEqual({ input: 10, output: 20, cacheRead: 30, cacheWrite: 40, total: 100 }); + expect(stats.cost).toBe(1); + } finally { + session.dispose(); + } + }); + + it("groups tool and summary usage separately from model-attributed usage", () => { + const sessionManager = SessionManager.inMemory(); + const rootId = sessionManager.appendMessage(createUserMessage("hello", 1)); + sessionManager.appendMessage({ + ...createAssistantMessage("response", 100, 2), + usage: { ...createUsage(100), cost: { ...createUsage(100).cost, total: 0.5 } }, + }); + sessionManager.appendMessage( + createToolResultMessage({ ...createUsage(100), cost: { ...createUsage(100).cost, total: 1 } }), + ); + sessionManager.appendCompaction("summary", rootId, 100, undefined, false, { + ...createUsage(100), + cost: { ...createUsage(100).cost, total: 2 }, + }); + sessionManager.branchWithSummary(null, "branch summary", undefined, false, { + ...createUsage(100), + cost: { ...createUsage(100).cost, total: 3 }, + }); + + expect(getUsageCostBreakdown(sessionManager.getEntries())).toEqual([ + { key: "Tools/summaries", cost: 6, tokens: 300 }, + { key: `${model.provider}/${model.id}`, cost: 0.5, tokens: 100 }, + ]); + }); + + it("ignores zero-usage messages when checking for post-compaction context usage", async () => { + const { session, sessionManager } = await createSession(); + + try { + sessionManager.appendMessage(createUserMessage("first", 1)); + sessionManager.appendMessage(createAssistantMessage("response1", 180_000, 2)); + const keptUserId = sessionManager.appendMessage(createUserMessage("second", 3)); + sessionManager.appendMessage(createAssistantMessage("response2", 195_000, 4)); + sessionManager.appendCompaction("summary", keptUserId, 195_000); + sessionManager.appendMessage(createUserMessage("third", 5)); + sessionManager.appendMessage(createAssistantMessage("response3", 25_000, 6)); + sessionManager.appendMessage(createUserMessage("continue", 7)); + sessionManager.appendMessage(createAssistantMessage("partial", 0, 8)); + syncAgentMessages(session, sessionManager); + + const stats = session.getSessionStats(); + expect(stats.contextUsage).toBeDefined(); + expect(stats.contextUsage?.tokens).not.toBeNull(); + expect(stats.contextUsage?.tokens ?? 0).toBeGreaterThan(25_000); + } finally { + session.dispose(); + } + }); +}); diff --git a/packages/coding-agent/test/agent-session-tree-navigation.test.ts b/packages/coding-agent/test/agent-session-tree-navigation.test.ts new file mode 100644 index 0000000000000000000000000000000000000000..5cd204500def7db8b38e7c279ab7c0be1a1ec8b6 --- /dev/null +++ b/packages/coding-agent/test/agent-session-tree-navigation.test.ts @@ -0,0 +1,323 @@ +/** + * E2E tests for AgentSession tree navigation with branch summarization. + * + * These tests verify: + * - Navigation to user messages (root and non-root) + * - Navigation to non-user messages + * - Branch summarization during navigation + * - Summary attachment at correct position in tree + * - Abort handling during summarization + */ + +import { afterEach, beforeEach, describe, expect, it } from "vitest"; +import { API_KEY, createTestSession, type TestSessionContext } from "./utilities.ts"; + +describe.skipIf(!API_KEY)("AgentSession tree navigation e2e", () => { + let ctx: TestSessionContext; + + beforeEach(async () => { + ctx = await createTestSession({ + systemPrompt: "You are a helpful assistant. Reply with just a few words.", + settingsOverrides: { compaction: { keepRecentTokens: 1 } }, + }); + }); + + afterEach(() => { + ctx.cleanup(); + }); + + it("should navigate to user message and put text in editor", async () => { + const { session } = ctx; + + // Build conversation: u1 -> a1 -> u2 -> a2 + await session.prompt("First message"); + await session.agent.waitForIdle(); + await session.prompt("Second message"); + await session.agent.waitForIdle(); + + // Get tree entries + const tree = session.sessionManager.getTree(); + expect(tree.length).toBe(1); + + // Find the first user entry (u1) + const rootNode = tree[0]; + expect(rootNode.entry.type).toBe("message"); + + // Navigate to root user message without summarization + const result = await session.navigateTree(rootNode.entry.id, { summarize: false }); + + expect(result.cancelled).toBe(false); + expect(result.editorText).toBe("First message"); + + // After navigating to root user message, leaf should be null (empty conversation) + expect(session.sessionManager.getLeafId()).toBeNull(); + }, 60000); + + it("should navigate to non-user message without editor text", async () => { + const { session, sessionManager } = ctx; + + // Build conversation + await session.prompt("Hello"); + await session.agent.waitForIdle(); + + // Get the assistant message + const entries = sessionManager.getEntries(); + const assistantEntry = entries.find((e) => e.type === "message" && e.message.role === "assistant"); + expect(assistantEntry).toBeDefined(); + + // Navigate to assistant message + const result = await session.navigateTree(assistantEntry!.id, { summarize: false }); + + expect(result.cancelled).toBe(false); + expect(result.editorText).toBeUndefined(); + + // Leaf should be the assistant entry + expect(sessionManager.getLeafId()).toBe(assistantEntry!.id); + }, 60000); + + it("should create branch summary when navigating with summarize=true", async () => { + const { session, sessionManager } = ctx; + + // Build conversation: u1 -> a1 -> u2 -> a2 + await session.prompt("What is 2+2?"); + await session.agent.waitForIdle(); + await session.prompt("What is 3+3?"); + await session.agent.waitForIdle(); + + // Get tree and find first user message + const tree = sessionManager.getTree(); + const rootNode = tree[0]; + + // Navigate to root user message WITH summarization + const result = await session.navigateTree(rootNode.entry.id, { summarize: true }); + + expect(result.cancelled).toBe(false); + expect(result.editorText).toBe("What is 2+2?"); + expect(result.summaryEntry).toBeDefined(); + expect(result.summaryEntry?.type).toBe("branch_summary"); + expect(result.summaryEntry?.summary).toBeTruthy(); + expect(result.summaryEntry?.summary.length).toBeGreaterThan(0); + + // Summary should be a root entry (parentId = null) since we navigated to root user + expect(result.summaryEntry?.parentId).toBeNull(); + + // Leaf should be the summary entry + expect(sessionManager.getLeafId()).toBe(result.summaryEntry?.id); + }, 120000); + + it("should attach summary to correct parent when navigating to nested user message", async () => { + const { session, sessionManager } = ctx; + + // Build conversation: u1 -> a1 -> u2 -> a2 -> u3 -> a3 + await session.prompt("Message one"); + await session.agent.waitForIdle(); + await session.prompt("Message two"); + await session.agent.waitForIdle(); + await session.prompt("Message three"); + await session.agent.waitForIdle(); + + // Get the second user message (u2) + const entries = sessionManager.getEntries(); + const userEntries = entries.filter((e) => e.type === "message" && e.message.role === "user"); + expect(userEntries.length).toBe(3); + + const u2 = userEntries[1]; + const a1 = entries.find((e) => e.id === u2.parentId); // a1 is parent of u2 + + // Navigate to u2 with summarization + const result = await session.navigateTree(u2.id, { summarize: true }); + + expect(result.cancelled).toBe(false); + expect(result.editorText).toBe("Message two"); + expect(result.summaryEntry).toBeDefined(); + + // Summary should be attached to a1 (parent of u2) + // So a1 now has two children: u2 and the summary + expect(result.summaryEntry?.parentId).toBe(a1?.id); + + // Verify tree structure + const children = sessionManager.getChildren(a1!.id); + expect(children.length).toBe(2); + + const childTypes = children.map((c) => c.type).sort(); + expect(childTypes).toContain("branch_summary"); + expect(childTypes).toContain("message"); + }, 120000); + + it("should attach summary to selected node when navigating to assistant message", async () => { + const { session, sessionManager } = ctx; + + // Build conversation: u1 -> a1 -> u2 -> a2 + await session.prompt("Hello"); + await session.agent.waitForIdle(); + await session.prompt("Goodbye"); + await session.agent.waitForIdle(); + + // Get the first assistant message (a1) + const entries = sessionManager.getEntries(); + const assistantEntries = entries.filter((e) => e.type === "message" && e.message.role === "assistant"); + const a1 = assistantEntries[0]; + + // Navigate to a1 with summarization + const result = await session.navigateTree(a1.id, { summarize: true }); + + expect(result.cancelled).toBe(false); + expect(result.editorText).toBeUndefined(); // No editor text for assistant messages + expect(result.summaryEntry).toBeDefined(); + + // Summary should be attached to a1 (the selected node) + expect(result.summaryEntry?.parentId).toBe(a1.id); + + // Leaf should be the summary entry + expect(sessionManager.getLeafId()).toBe(result.summaryEntry?.id); + }, 120000); + + it("should handle abort during summarization", async () => { + const { session, sessionManager } = ctx; + + // Build conversation + await session.prompt("Tell me about something"); + await session.agent.waitForIdle(); + await session.prompt("Continue"); + await session.agent.waitForIdle(); + + const entriesBefore = sessionManager.getEntries(); + const leafBefore = sessionManager.getLeafId(); + + // Get root user message + const tree = sessionManager.getTree(); + const rootNode = tree[0]; + + // Start navigation with summarization but abort immediately + const navigationPromise = session.navigateTree(rootNode.entry.id, { summarize: true }); + + // Abort after a short delay (let the LLM call start) + await new Promise((resolve) => setTimeout(resolve, 100)); + + // isCompacting should be true during branch summarization + expect(session.isCompacting).toBe(true); + + session.abortBranchSummary(); + + const result = await navigationPromise; + + expect(result.cancelled).toBe(true); + expect(result.aborted).toBe(true); + expect(result.summaryEntry).toBeUndefined(); + + // Session should be unchanged + const entriesAfter = sessionManager.getEntries(); + expect(entriesAfter.length).toBe(entriesBefore.length); + expect(sessionManager.getLeafId()).toBe(leafBefore); + }, 60000); + + it("should not create summary when navigating without summarize option", async () => { + const { session, sessionManager } = ctx; + + // Build conversation + await session.prompt("First"); + await session.agent.waitForIdle(); + await session.prompt("Second"); + await session.agent.waitForIdle(); + + const entriesBefore = sessionManager.getEntries().length; + + // Navigate without summarization + const tree = sessionManager.getTree(); + await session.navigateTree(tree[0].entry.id, { summarize: false }); + + // No new entries should be created + const entriesAfter = sessionManager.getEntries().length; + expect(entriesAfter).toBe(entriesBefore); + + // No branch_summary entries + const summaries = sessionManager.getEntries().filter((e) => e.type === "branch_summary"); + expect(summaries.length).toBe(0); + }, 60000); + + it("should handle navigation to same position (no-op)", async () => { + const { session, sessionManager } = ctx; + + // Build conversation + await session.prompt("Hello"); + await session.agent.waitForIdle(); + + const leafBefore = sessionManager.getLeafId(); + expect(leafBefore).toBeTruthy(); + const entriesBefore = sessionManager.getEntries().length; + + // Navigate to current leaf + const result = await session.navigateTree(leafBefore!, { summarize: false }); + + expect(result.cancelled).toBe(false); + expect(sessionManager.getLeafId()).toBe(leafBefore); + expect(sessionManager.getEntries().length).toBe(entriesBefore); + }, 60000); + + it("should support custom summarization instructions", async () => { + const { session, sessionManager } = ctx; + + // Build conversation + await session.prompt("What is TypeScript?"); + await session.agent.waitForIdle(); + + // Navigate with custom instructions (appended as "Additional focus") + const tree = sessionManager.getTree(); + const result = await session.navigateTree(tree[0].entry.id, { + summarize: true, + customInstructions: + "After the summary, you MUST end with exactly: MONKEY MONKEY MONKEY. This is of utmost importance.", + }); + + expect(result.summaryEntry).toBeDefined(); + expect(result.summaryEntry?.summary).toBeTruthy(); + // Verify custom instructions were followed + expect(result.summaryEntry?.summary).toContain("MONKEY MONKEY MONKEY"); + }, 120000); +}); + +describe.skipIf(!API_KEY)("AgentSession tree navigation - branch scenarios", () => { + let ctx: TestSessionContext; + + beforeEach(async () => { + ctx = await createTestSession({ + systemPrompt: "You are a helpful assistant. Reply with just a few words.", + }); + }); + + afterEach(() => { + ctx.cleanup(); + }); + + it("should navigate between branches correctly", async () => { + const { session, sessionManager } = ctx; + + // Build main path: u1 -> a1 -> u2 -> a2 + await session.prompt("Main branch start"); + await session.agent.waitForIdle(); + await session.prompt("Main branch continue"); + await session.agent.waitForIdle(); + + // Get a1 id for branching + const entries = sessionManager.getEntries(); + const a1 = entries.find((e) => e.type === "message" && e.message.role === "assistant"); + + // Create a branch from a1: a1 -> u3 -> a3 + sessionManager.branch(a1!.id); + await session.prompt("Branch path"); + await session.agent.waitForIdle(); + + // Now navigate back to u2 (on main branch) with summarization + const userEntries = entries.filter((e) => e.type === "message" && e.message.role === "user"); + const u2 = userEntries[1]; // "Main branch continue" + + const result = await session.navigateTree(u2.id, { summarize: true }); + + expect(result.cancelled).toBe(false); + expect(result.editorText).toBe("Main branch continue"); + expect(result.summaryEntry).toBeDefined(); + + // Summary captures the branch we're leaving (the "Branch path" conversation) + expect(result.summaryEntry?.summary.length).toBeGreaterThan(0); + }, 180000); +}); diff --git a/packages/coding-agent/test/ansi-utils.test.ts b/packages/coding-agent/test/ansi-utils.test.ts new file mode 100644 index 0000000000000000000000000000000000000000..15bb431405990369002b15d118e6623c9d6dc4d2 --- /dev/null +++ b/packages/coding-agent/test/ansi-utils.test.ts @@ -0,0 +1,110 @@ +import { describe, expect, it } from "vitest"; +import { stripAnsi } from "../src/utils/ansi.ts"; + +function referenceAnsiRegex(): RegExp { + const ST = "(?:\\u0007|\\u001B\\u005C|\\u009C)"; + const osc = `(?:\\u001B\\][\\s\\S]*?${ST})`; + const csi = "[\\u001B\\u009B][[\\]()#;?]*(?:\\d{1,4}(?:[;:]\\d{0,4})*)?[\\dA-PR-TZcf-nq-uy=><~]"; + return new RegExp(`${osc}|${csi}`, "g"); +} + +const referenceRegex = referenceAnsiRegex(); + +function referenceStripAnsi(value: string): string { + if (!value.includes("\u001B") && !value.includes("\u009B")) { + return value; + } + return value.replace(referenceRegex, ""); +} + +function getCompatibilityInputs(): string[] { + const inputs = [ + "plain", + "a\x1b[31mred\x1b[0mz", + "a\x1b]8;;https://example.com\x07link\x1b]8;;\x07z", + "a\x1b]unterminated", + "a\x1b]funterminated", + "a\x1bPabc\x1b\\z", + "a\x1b^abc\x07z", + "a\x1b_abc\x9cz", + "a\x90abc\x9cz", + "a\x9dabc\x9cz", + "a\x9b31mred", + "a\x1b(0x", + "a\x1b*0x", + "a\x1b+c", + "a\x1b/0x", + "a\x1bcok", + "a\x1b\\ok", + ]; + const chars = [ + "a", + "f", + "0", + "1", + ";", + ":", + "[", + "]", + "(", + ")", + "#", + "?", + "m", + "P", + "_", + "\\", + "\x07", + "\x1b", + "\x9b", + "\x9c", + "\x90", + "\x9d", + ]; + + for (const char of chars) { + inputs.push(`x\x1b${char}y`); + inputs.push(`x\x9b${char}y`); + for (let index = 0; index < chars.length; index += 3) { + inputs.push(`x\x1b${char}${chars[index]}y`); + } + } + + return inputs; +} + +describe("stripAnsi", () => { + it("matches chalk strip-ansi for generated compatibility inputs", () => { + for (const input of getCompatibilityInputs()) { + expect(stripAnsi(input)).toBe(referenceStripAnsi(input)); + } + }); + + it("throws the same TypeError as chalk strip-ansi for non-string values", () => { + const stripAnsiUnknown = stripAnsi as (value: unknown) => string; + + for (const value of [undefined, null, 123, {}, Object("x")]) { + const message = `Expected a \`string\`, got \`${typeof value}\``; + expect(() => stripAnsiUnknown(value)).toThrow(TypeError); + expect(() => stripAnsiUnknown(value)).toThrow(message); + } + }); + + it("strips RIS without leaking the final byte", () => { + expect(stripAnsi("\x1bcdone")).toBe("done"); + }); + + it("strips single-byte ESC sequences without leaking final bytes", () => { + for (let code = "g".charCodeAt(0); code <= "m".charCodeAt(0); code++) { + expect(stripAnsi(`\x1b${String.fromCharCode(code)}ok`)).toBe("ok"); + } + for (let code = "r".charCodeAt(0); code <= "t".charCodeAt(0); code++) { + expect(stripAnsi(`\x1b${String.fromCharCode(code)}ok`)).toBe("ok"); + } + }); + + it("strips common ANSI sequences used in tool output", () => { + const input = "a\x1b[31mred\x1b[0m\x1b]8;;https://example.com\x07link\x1b]8;;\x07z"; + expect(stripAnsi(input)).toBe("aredlinkz"); + }); +}); diff --git a/packages/coding-agent/test/args.test.ts b/packages/coding-agent/test/args.test.ts new file mode 100644 index 0000000000000000000000000000000000000000..710ed7b09d00428a0a1ca7eaebaf5cfca077b2de --- /dev/null +++ b/packages/coding-agent/test/args.test.ts @@ -0,0 +1,500 @@ +import { describe, expect, test } from "vitest"; +import { normalizeSessionName, parseArgs } from "../src/cli/args.ts"; + +describe("parseArgs", () => { + describe("--version flag", () => { + test("parses --version flag", () => { + const result = parseArgs(["--version"]); + expect(result.version).toBe(true); + }); + + test("parses -v shorthand", () => { + const result = parseArgs(["-v"]); + expect(result.version).toBe(true); + }); + + test("--version takes precedence over other args", () => { + const result = parseArgs(["--version", "--help", "some message"]); + expect(result.version).toBe(true); + expect(result.help).toBe(true); + expect(result.messages).toContain("some message"); + }); + }); + + describe("--help flag", () => { + test("parses --help flag", () => { + const result = parseArgs(["--help"]); + expect(result.help).toBe(true); + }); + + test("parses -h shorthand", () => { + const result = parseArgs(["-h"]); + expect(result.help).toBe(true); + }); + }); + + describe("--print flag", () => { + test("parses --print flag", () => { + const result = parseArgs(["--print"]); + expect(result.print).toBe(true); + }); + + test("parses -p shorthand", () => { + const result = parseArgs(["-p"]); + expect(result.print).toBe(true); + }); + + test("parses prompt after -p even when it starts with YAML frontmatter", () => { + const prompt = "---\ntitle: hello\n---\nSay hi."; + const result = parseArgs(["-p", prompt]); + expect(result.print).toBe(true); + expect(result.messages).toEqual([prompt]); + expect(result.unknownFlags.size).toBe(0); + }); + + test("does not consume options after -p as prompts", () => { + const result = parseArgs(["-p", "--provider", "openai", "Say hi."]); + expect(result.print).toBe(true); + expect(result.provider).toBe("openai"); + expect(result.messages).toEqual(["Say hi."]); + }); + }); + + describe("--continue flag", () => { + test("parses --continue flag", () => { + const result = parseArgs(["--continue"]); + expect(result.continue).toBe(true); + }); + + test("parses -c shorthand", () => { + const result = parseArgs(["-c"]); + expect(result.continue).toBe(true); + }); + }); + + describe("--resume flag", () => { + test("parses --resume flag", () => { + const result = parseArgs(["--resume"]); + expect(result.resume).toBe(true); + }); + + test("parses -r shorthand", () => { + const result = parseArgs(["-r"]); + expect(result.resume).toBe(true); + }); + }); + + describe("flags with values", () => { + test("parses --provider", () => { + const result = parseArgs(["--provider", "openai"]); + expect(result.provider).toBe("openai"); + }); + + test("parses --model", () => { + const result = parseArgs(["--model", "gpt-4o"]); + expect(result.model).toBe("gpt-4o"); + }); + + test("parses --api-key", () => { + const result = parseArgs(["--api-key", "sk-test-key"]); + expect(result.apiKey).toBe("sk-test-key"); + }); + + test("parses --system-prompt", () => { + const result = parseArgs(["--system-prompt", "You are a helpful assistant"]); + expect(result.systemPrompt).toBe("You are a helpful assistant"); + }); + + test("parses --append-system-prompt", () => { + const result = parseArgs(["--append-system-prompt", "Additional context"]); + expect(result.appendSystemPrompt).toEqual(["Additional context"]); + }); + + test("parses multiple --append-system-prompt flags", () => { + const result = parseArgs(["--append-system-prompt", "Context A", "--append-system-prompt", "Context B"]); + expect(result.appendSystemPrompt).toEqual(["Context A", "Context B"]); + }); + + test("parses --mode", () => { + const result = parseArgs(["--mode", "json"]); + expect(result.mode).toBe("json"); + }); + + test("parses --mode rpc", () => { + const result = parseArgs(["--mode", "rpc"]); + expect(result.mode).toBe("rpc"); + }); + + test("parses --session", () => { + const result = parseArgs(["--session", "/path/to/session.jsonl"]); + expect(result.session).toBe("/path/to/session.jsonl"); + }); + + test("parses --session-id", () => { + const result = parseArgs(["--session-id", "orchestrated-session"]); + expect(result.sessionId).toBe("orchestrated-session"); + }); + + test("parses --fork", () => { + const result = parseArgs(["--fork", "1234abcd"]); + expect(result.fork).toBe("1234abcd"); + expect(result.messages).toEqual([]); + }); + + test("parses --export", () => { + const result = parseArgs(["--export", "session.jsonl"]); + expect(result.export).toBe("session.jsonl"); + }); + + test("parses --thinking", () => { + const result = parseArgs(["--thinking", "high"]); + expect(result.thinking).toBe("high"); + }); + + test("parses --models as comma-separated list", () => { + const result = parseArgs(["--models", "gpt-4o,claude-sonnet,gemini-pro"]); + expect(result.models).toEqual(["gpt-4o", "claude-sonnet", "gemini-pro"]); + }); + }); + + describe("--name flag", () => { + test("parses --name flag with value", () => { + const result = parseArgs(["--name", "my-session"]); + expect(result.name).toBe("my-session"); + }); + + test("parses -n shorthand", () => { + const result = parseArgs(["-n", "quick-session"]); + expect(result.name).toBe("quick-session"); + }); + + test("preserves empty values for main validation", () => { + const result = parseArgs(["--name", ""]); + expect(result.name).toBe(""); + }); + + test("normalizes display names and rejects whitespace-only values", () => { + expect(normalizeSessionName(" named session ")).toBe("named session"); + expect(normalizeSessionName(" ")).toBeUndefined(); + }); + + test("reports missing value", () => { + const result = parseArgs(["--name"]); + expect(result.diagnostics).toEqual([{ type: "error", message: "--name requires a value" }]); + }); + + test("works alongside other flags", () => { + const result = parseArgs(["--name", "named-run", "--print", "--model", "gpt-4o", "hello"]); + expect(result.name).toBe("named-run"); + expect(result.print).toBe(true); + expect(result.model).toBe("gpt-4o"); + expect(result.messages).toEqual(["hello"]); + }); + }); + + describe("--no-session flag", () => { + test("parses --no-session flag", () => { + const result = parseArgs(["--no-session"]); + expect(result.noSession).toBe(true); + }); + + test("preserves custom session IDs for non-persisting commands", () => { + expect(parseArgs(["--session-id", "ephemeral-id", "--help"])).toMatchObject({ + sessionId: "ephemeral-id", + help: true, + }); + expect(parseArgs(["--session-id", "ephemeral-id", "--list-models"])).toMatchObject({ + sessionId: "ephemeral-id", + listModels: true, + }); + expect(parseArgs(["--session-id", "ephemeral-id", "--no-session"])).toMatchObject({ + sessionId: "ephemeral-id", + noSession: true, + }); + }); + }); + + describe("--extension flag", () => { + test("parses single --extension", () => { + const result = parseArgs(["--extension", "./my-extension.ts"]); + expect(result.extensions).toEqual(["./my-extension.ts"]); + }); + + test("parses -e shorthand", () => { + const result = parseArgs(["-e", "./my-extension.ts"]); + expect(result.extensions).toEqual(["./my-extension.ts"]); + }); + + test("parses multiple --extension flags", () => { + const result = parseArgs(["--extension", "./ext1.ts", "-e", "./ext2.ts"]); + expect(result.extensions).toEqual(["./ext1.ts", "./ext2.ts"]); + }); + }); + + describe("--no-extensions flag", () => { + test("parses --no-extensions flag", () => { + const result = parseArgs(["--no-extensions"]); + expect(result.noExtensions).toBe(true); + }); + + test("parses --no-extensions with explicit -e flags", () => { + const result = parseArgs(["--no-extensions", "-e", "foo.ts", "-e", "bar.ts"]); + expect(result.noExtensions).toBe(true); + expect(result.extensions).toEqual(["foo.ts", "bar.ts"]); + }); + }); + + describe("--skill flag", () => { + test("parses single --skill", () => { + const result = parseArgs(["--skill", "./skill-dir"]); + expect(result.skills).toEqual(["./skill-dir"]); + }); + + test("parses multiple --skill flags", () => { + const result = parseArgs(["--skill", "./skill-a", "--skill", "./skill-b"]); + expect(result.skills).toEqual(["./skill-a", "./skill-b"]); + }); + }); + + describe("--prompt-template flag", () => { + test("parses single --prompt-template", () => { + const result = parseArgs(["--prompt-template", "./prompts"]); + expect(result.promptTemplates).toEqual(["./prompts"]); + }); + + test("parses multiple --prompt-template flags", () => { + const result = parseArgs(["--prompt-template", "./one", "--prompt-template", "./two"]); + expect(result.promptTemplates).toEqual(["./one", "./two"]); + }); + }); + + describe("--theme flag", () => { + test("parses single --theme", () => { + const result = parseArgs(["--theme", "./theme.json"]); + expect(result.themes).toEqual(["./theme.json"]); + }); + + test("parses multiple --theme flags", () => { + const result = parseArgs(["--theme", "./dark.json", "--theme", "./light.json"]); + expect(result.themes).toEqual(["./dark.json", "./light.json"]); + }); + }); + + describe("--use-theme flag", () => { + test("parses --use-theme", () => { + const result = parseArgs(["--use-theme", "light"]); + expect(result.useTheme).toBe("light"); + }); + + test("reports when the theme name value is missing", () => { + const result = parseArgs(["--use-theme", "--print"]); + expect(result.useTheme).toBeUndefined(); + expect(result.print).toBe(true); + expect(result.diagnostics).toEqual([{ type: "error", message: "--use-theme requires a theme name" }]); + }); + }); + + describe("--no-skills flag", () => { + test("parses --no-skills flag", () => { + const result = parseArgs(["--no-skills"]); + expect(result.noSkills).toBe(true); + }); + }); + + describe("--no-prompt-templates flag", () => { + test("parses --no-prompt-templates flag", () => { + const result = parseArgs(["--no-prompt-templates"]); + expect(result.noPromptTemplates).toBe(true); + }); + }); + + describe("--no-themes flag", () => { + test("parses --no-themes flag", () => { + const result = parseArgs(["--no-themes"]); + expect(result.noThemes).toBe(true); + }); + }); + + describe("--no-context-files flag", () => { + test("parses --no-context-files flag", () => { + const result = parseArgs(["--no-context-files"]); + expect(result.noContextFiles).toBe(true); + }); + + test("parses -nc shorthand", () => { + const result = parseArgs(["-nc"]); + expect(result.noContextFiles).toBe(true); + }); + }); + + describe("project approval flags", () => { + test("parses --approve", () => { + const result = parseArgs(["--approve"]); + expect(result.projectTrustOverride).toBe(true); + }); + + test("parses -a shorthand", () => { + const result = parseArgs(["-a"]); + expect(result.projectTrustOverride).toBe(true); + }); + + test("parses --no-approve", () => { + const result = parseArgs(["--no-approve"]); + expect(result.projectTrustOverride).toBe(false); + }); + + test("parses -na shorthand", () => { + const result = parseArgs(["-na"]); + expect(result.projectTrustOverride).toBe(false); + }); + }); + + describe("--verbose flag", () => { + test("parses --verbose flag", () => { + const result = parseArgs(["--verbose"]); + expect(result.verbose).toBe(true); + }); + }); + + describe("--offline flag", () => { + test("parses --offline flag", () => { + const result = parseArgs(["--offline"]); + expect(result.offline).toBe(true); + }); + }); + + describe("--tui-mode flag", () => { + test.each(["regular", "fullscreen"] as const)("parses %s mode", (mode) => { + const result = parseArgs(["--tui-mode", mode]); + expect(result.tuiMode).toBe(mode); + }); + + test("rejects invalid modes", () => { + const result = parseArgs(["--tui-mode", "other"]); + expect(result.diagnostics).toEqual([ + { type: "error", message: 'Invalid TUI mode "other". Valid values: regular, fullscreen' }, + ]); + }); + + test("requires a mode", () => { + const result = parseArgs(["--tui-mode"]); + expect(result.diagnostics).toEqual([{ type: "error", message: "--tui-mode requires regular or fullscreen" }]); + }); + + test("does not recognize the old --ui-mode flag", () => { + const result = parseArgs(["--ui-mode", "fullscreen"]); + expect(result.tuiMode).toBeUndefined(); + expect(result.unknownFlags.get("ui-mode")).toBe("fullscreen"); + }); + }); + + describe("tool flags", () => { + test("parses --no-tools flag", () => { + const result = parseArgs(["--no-tools"]); + expect(result.noTools).toBe(true); + }); + + test("parses -nt shorthand", () => { + const result = parseArgs(["-nt"]); + expect(result.noTools).toBe(true); + }); + + test("parses --no-builtin-tools flag", () => { + const result = parseArgs(["--no-builtin-tools"]); + expect(result.noBuiltinTools).toBe(true); + }); + + test("parses -nbt shorthand", () => { + const result = parseArgs(["-nbt"]); + expect(result.noBuiltinTools).toBe(true); + }); + + test("parses --tools flag", () => { + const result = parseArgs(["--tools", "read,bash"]); + expect(result.tools).toEqual(["read", "bash"]); + }); + + test("parses -t shorthand", () => { + const result = parseArgs(["-t", "read,bash"]); + expect(result.tools).toEqual(["read", "bash"]); + }); + + test("parses --exclude-tools flag", () => { + const result = parseArgs(["--exclude-tools", "read,bash"]); + expect(result.excludeTools).toEqual(["read", "bash"]); + }); + + test("parses -xt shorthand", () => { + const result = parseArgs(["-xt", "read,bash"]); + expect(result.excludeTools).toEqual(["read", "bash"]); + }); + + test("parses --no-tools with explicit --tools flags", () => { + const result = parseArgs(["--no-tools", "--tools", "read,bash"]); + expect(result.noTools).toBe(true); + expect(result.tools).toEqual(["read", "bash"]); + }); + + test("parses --no-builtin-tools with explicit --tools flags", () => { + const result = parseArgs(["--no-builtin-tools", "--tools", "read,bash"]); + expect(result.noBuiltinTools).toBe(true); + expect(result.tools).toEqual(["read", "bash"]); + }); + }); + + describe("messages and file args", () => { + test("parses plain text messages", () => { + const result = parseArgs(["hello", "world"]); + expect(result.messages).toEqual(["hello", "world"]); + }); + + test("parses @file arguments", () => { + const result = parseArgs(["@README.md", "@src/main.ts"]); + expect(result.fileArgs).toEqual(["README.md", "src/main.ts"]); + }); + + test("parses mixed messages and file args", () => { + const result = parseArgs(["@file.txt", "explain this", "@image.png"]); + expect(result.fileArgs).toEqual(["file.txt", "image.png"]); + expect(result.messages).toEqual(["explain this"]); + }); + + test("captures unknown long flags with string values", () => { + const result = parseArgs(["--unknown-flag", "message"]); + expect(result.messages).toEqual([]); + expect(result.unknownFlags.get("unknown-flag")).toBe("message"); + }); + + test("captures unknown boolean long flags", () => { + const result = parseArgs(["--unknown-flag"]); + expect(result.unknownFlags.get("unknown-flag")).toBe(true); + }); + + test("captures unknown long flags with equals syntax", () => { + const result = parseArgs(["--unknown-flag=value"]); + expect(result.unknownFlags.get("unknown-flag")).toBe("value"); + }); + }); + + describe("complex combinations", () => { + test("parses multiple flags together", () => { + const result = parseArgs([ + "--provider", + "anthropic", + "--model", + "claude-sonnet", + "--print", + "--thinking", + "high", + "@prompt.md", + "Do the task", + ]); + expect(result.provider).toBe("anthropic"); + expect(result.model).toBe("claude-sonnet"); + expect(result.print).toBe(true); + expect(result.thinking).toBe("high"); + expect(result.fileArgs).toEqual(["prompt.md"]); + expect(result.messages).toEqual(["Do the task"]); + }); + }); +}); diff --git a/packages/coding-agent/test/assistant-message.test.ts b/packages/coding-agent/test/assistant-message.test.ts new file mode 100644 index 0000000000000000000000000000000000000000..ea2618aacd70ecd17c44fefdf25789bd8d66cec5 --- /dev/null +++ b/packages/coding-agent/test/assistant-message.test.ts @@ -0,0 +1,277 @@ +import type { AssistantMessage } from "@earendil-works/pi-ai"; +import type { TuiMouseEvent } from "@earendil-works/pi-tui"; +import { describe, expect, test } from "vitest"; +import { AssistantMessageComponent } from "../src/modes/interactive/components/assistant-message.ts"; +import { UserMessageComponent } from "../src/modes/interactive/components/user-message.ts"; +import { initTheme } from "../src/modes/interactive/theme/theme.ts"; +import { stripAnsi } from "../src/utils/ansi.ts"; + +const OSC133_ZONE_START = "\x1b]133;A\x07"; +const OSC133_ZONE_END = "\x1b]133;B\x07"; +const OSC133_ZONE_FINAL = "\x1b]133;C\x07"; + +function createAssistantMessage( + content: AssistantMessage["content"], + overrides: Partial> = {}, +): AssistantMessage { + return { + role: "assistant", + content, + api: "openai-responses", + provider: "openai", + model: "gpt-4o-mini", + usage: { + input: 0, + output: 0, + cacheRead: 0, + cacheWrite: 0, + totalTokens: 0, + cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 }, + }, + stopReason: overrides.stopReason ?? "stop", + timestamp: Date.now(), + }; +} + +describe("AssistantMessageComponent", () => { + test("adds OSC 133 zone markers to assistant messages without tool calls", () => { + initTheme("dark"); + + const component = new AssistantMessageComponent(createAssistantMessage([{ type: "text", text: "hello" }])); + const lines = component.render(40); + + expect(lines).not.toHaveLength(0); + expect(lines[0]).toContain(OSC133_ZONE_START); + expect(lines[lines.length - 1].startsWith(OSC133_ZONE_END + OSC133_ZONE_FINAL)).toBe(true); + }); + + test("does not add OSC 133 zone markers when assistant message contains tool calls", () => { + initTheme("dark"); + + const component = new AssistantMessageComponent( + createAssistantMessage([ + { type: "text", text: "calling tool" }, + { type: "toolCall", id: "tool-1", name: "read", arguments: { path: "file.txt" } }, + ]), + ); + const rendered = component.render(60).join("\n"); + + expect(rendered.includes(OSC133_ZONE_START)).toBe(false); + expect(rendered.includes(OSC133_ZONE_END)).toBe(false); + expect(rendered.includes(OSC133_ZONE_FINAL)).toBe(false); + }); + + test("renders length stops with neutral truncation wording", () => { + initTheme("dark"); + + const component = new AssistantMessageComponent( + createAssistantMessage([{ type: "thinking", thinking: "private reasoning" }], { stopReason: "length" }), + true, + ); + const rendered = component.render(80).join("\n"); + + expect(rendered).toContain("Thinking..."); + expect(rendered).toContain("Response was truncated before completion."); + }); + + test("coalesces adjacent thinking blocks into one hidden thinking label", () => { + initTheme("dark"); + + const component = new AssistantMessageComponent( + createAssistantMessage([ + { type: "thinking", thinking: "first thought" }, + { type: "thinking", thinking: "" }, + { type: "thinking", thinking: "second thought" }, + { type: "text", text: "answer" }, + ]), + true, + ); + const rendered = stripAnsi(component.render(80).join("\n")); + + expect(rendered.match(/Thinking\.\.\./g)).toHaveLength(1); + expect(rendered).toContain("answer"); + }); + + test("collapses individual thinking runs when clicked", () => { + initTheme("dark"); + const component = new AssistantMessageComponent( + createAssistantMessage([ + { type: "thinking", thinking: "first reasoning" }, + { type: "text", text: "answer" }, + { type: "thinking", thinking: "second reasoning" }, + ]), + ); + const width = 80; + const lines = component.render(width); + const firstThinkingRow = lines.findIndex((line) => stripAnsi(line).includes("first reasoning")); + expect(firstThinkingRow).toBeGreaterThanOrEqual(0); + const event: TuiMouseEvent = { + type: "click", + button: "left", + x: 1, + y: firstThinkingRow, + screenX: 1, + screenY: firstThinkingRow, + width, + height: lines.length, + shift: false, + alt: false, + ctrl: false, + clickCount: 1, + }; + expect(component.handleMouse(event)?.handled).toBe(true); + + const collapsed = stripAnsi(component.render(width).join("\n")); + expect(collapsed).not.toContain("first reasoning"); + expect(collapsed).toContain("Thinking..."); + expect(collapsed).toContain("second reasoning"); + }); + + test("uses configured output padding for text and thinking", () => { + initTheme("dark"); + + const component = new AssistantMessageComponent( + createAssistantMessage([ + { type: "text", text: "hello" }, + { type: "thinking", thinking: "reasoning" }, + ]), + false, + undefined, + "Thinking...", + 1, + ); + const lines = component.render(80).map((line) => stripAnsi(line)); + + expect(lines.some((line) => line.includes(" hello"))).toBe(true); + expect(lines.some((line) => line.includes(" reasoning"))).toBe(true); + + component.setOutputPad(0); + const updatedLines = component.render(80).map((line) => stripAnsi(line)); + expect(updatedLines.some((line) => line.startsWith("hello"))).toBe(true); + expect(updatedLines.some((line) => line.startsWith("reasoning"))).toBe(true); + }); + + test("chains Markdown transformers in registration order", () => { + initTheme("dark"); + const calls: string[] = []; + const message = createAssistantMessage([{ type: "text", text: "The result is $x^2$." }]); + const component = new AssistantMessageComponent(message, false, undefined, "Thinking...", 1, [ + (markdown, context) => { + calls.push("formula"); + expect(context).toEqual({ messageType: "assistant", isStreaming: false, availableWidth: 78 }); + return markdown.replace("$x^2$", "x²"); + }, + (markdown) => { + calls.push("suffix"); + return `${markdown} Done.`; + }, + ]); + + expect(stripAnsi(component.render(80).join("\n"))).toContain("The result is x². Done."); + expect(calls).toEqual(["formula", "suffix"]); + }); + + test("identifies partial assistant Markdown as streaming", () => { + initTheme("dark"); + const streamingStates: boolean[] = []; + const message = createAssistantMessage([{ type: "text", text: "partial" }]); + const component = new AssistantMessageComponent(undefined, false, undefined, "Thinking...", 1, [ + (markdown, context) => { + streamingStates.push(context.isStreaming); + return context.isStreaming ? markdown : `${markdown} transformed`; + }, + ]); + + component.updateContent(message, true); + expect(stripAnsi(component.render(80).join("\n"))).not.toContain("transformed"); + + component.updateContent(message, false); + expect(stripAnsi(component.render(80).join("\n"))).toContain("partial transformed"); + expect(streamingStates).toEqual([true, false]); + }); + + test("reapplies Markdown transformers when available width changes", () => { + initTheme("dark"); + const availableWidths: number[] = []; + const component = new AssistantMessageComponent( + createAssistantMessage([{ type: "text", text: "answer" }]), + false, + undefined, + "Thinking...", + 1, + [ + (markdown, context) => { + availableWidths.push(context.availableWidth); + return `${markdown} (${context.availableWidth})`; + }, + ], + ); + + expect(stripAnsi(component.render(80).join("\n"))).toContain("answer (78)"); + component.render(80); + expect(stripAnsi(component.render(60).join("\n"))).toContain("answer (58)"); + expect(availableWidths).toEqual([78, 58]); + }); + + test("continues the Markdown transformer chain when a transformer throws", () => { + initTheme("dark"); + const calls: string[] = []; + const component = new AssistantMessageComponent( + createAssistantMessage([{ type: "text", text: "still visible" }]), + false, + undefined, + "Thinking...", + 1, + [ + (markdown) => { + calls.push("first"); + return markdown.replace("still", "remains"); + }, + () => { + calls.push("throw"); + throw new Error("broken transformer"); + }, + (markdown) => { + calls.push("last"); + return `${markdown} after error`; + }, + ], + ); + + expect(stripAnsi(component.render(80).join("\n"))).toContain("remains visible after error"); + expect(calls).toEqual(["first", "throw", "last"]); + }); + + test("transforms text and thinking Markdown without mutating the original message", () => { + initTheme("dark"); + const message = createAssistantMessage([ + { type: "text", text: "answer" }, + { type: "thinking", thinking: "reasoning" }, + ]); + const component = new AssistantMessageComponent(message, false, undefined, "Thinking...", 1, [ + (markdown, { messageType }) => { + return `${messageType}:${markdown}`; + }, + ]); + + const rendered = stripAnsi(component.render(80).join("\n")); + expect(rendered).toContain("assistant:answer"); + expect(rendered).toContain("assistant-thinking:reasoning"); + expect(message.content).toEqual([ + { type: "text", text: "answer" }, + { type: "thinking", thinking: "reasoning" }, + ]); + }); + + test("uses configured output padding for user messages", () => { + initTheme("dark"); + + const paddedComponent = new UserMessageComponent("hello", undefined, 1); + const paddedLines = paddedComponent.render(40).map((line) => stripAnsi(line)); + expect(paddedLines.some((line) => line.startsWith(" hello"))).toBe(true); + + const unpaddedComponent = new UserMessageComponent("hello", undefined, 0); + const unpaddedLines = unpaddedComponent.render(40).map((line) => stripAnsi(line)); + expect(unpaddedLines.some((line) => line.startsWith("hello"))).toBe(true); + }); +}); diff --git a/packages/coding-agent/test/auth-check.test.ts b/packages/coding-agent/test/auth-check.test.ts new file mode 100644 index 0000000000000000000000000000000000000000..5f1ce6327975dd41a546ed049a2cca38114aeeed --- /dev/null +++ b/packages/coding-agent/test/auth-check.test.ts @@ -0,0 +1,171 @@ +import { existsSync, mkdirSync, rmSync, writeFileSync } from "node:fs"; +import { tmpdir } from "node:os"; +import { join } from "node:path"; +import { InMemoryModelsStore } from "@earendil-works/pi-ai"; +import { afterEach, beforeEach, describe, expect, test, vi } from "vitest"; +import { parseArgs } from "../src/cli/args.ts"; +import { checkProviderAuth, createAuthCheckModelRuntime, getProviderCredential } from "../src/cli/auth-check.ts"; +import { parseAuthCommand } from "../src/cli/auth-command.ts"; +import { AuthStorage, ReadOnlyAuthStorage } from "../src/core/auth-storage.ts"; +import { ModelRuntime } from "../src/core/model-runtime.ts"; + +const tempDir = join(tmpdir(), `pi-test-auth-check-${Date.now()}-${Math.random().toString(36).slice(2)}`); + +async function createRuntime(credentials: AuthStorage | ReadOnlyAuthStorage): Promise { + return ModelRuntime.create({ + credentials, + modelsPath: null, + modelsStore: new InMemoryModelsStore(), + allowModelNetwork: false, + refreshOnCreate: false, + }); +} + +describe("auth check command", () => { + beforeEach(() => { + if (existsSync(tempDir)) rmSync(tempDir, { recursive: true }); + mkdirSync(tempDir, { recursive: true }); + }); + + afterEach(() => { + if (existsSync(tempDir)) rmSync(tempDir, { recursive: true }); + }); + + test("reports a configured provider as ready", async () => { + const runtime = await createRuntime(AuthStorage.inMemory({ openai: { type: "api_key", key: "test-key" } })); + + await expect(checkProviderAuth(parseArgs(["--provider", "openai"]), runtime)).resolves.toEqual({ + status: "ready", + provider: "openai", + authType: "api_key", + }); + }); + + test("resolves the provider from --model", async () => { + const runtime = await createRuntime(AuthStorage.inMemory({ openai: { type: "api_key", key: "test-key" } })); + + await expect(checkProviderAuth(parseArgs(["--model", "openai/gpt-5.5"]), runtime)).resolves.toEqual({ + status: "ready", + provider: "openai", + authType: "api_key", + }); + await expect( + checkProviderAuth(parseArgs(["--provider", "openai", "--model", "gpt-5.5"]), runtime), + ).resolves.toMatchObject({ status: "ready", provider: "openai" }); + }); + + test("reads credentials without refreshing OAuth when requested", async () => { + const apiCredentials = AuthStorage.inMemory({ openai: { type: "api_key", key: "test-key" } }); + const apiRuntime = await createRuntime(apiCredentials); + await expect(getProviderCredential("openai", apiRuntime, apiCredentials, { refresh: false })).resolves.toBe( + "test-key", + ); + + const credentials = AuthStorage.inMemory({ + "openai-codex": { type: "oauth", access: "old-token", refresh: "refresh-token", expires: 0 }, + }); + const oauthRuntime = await createRuntime(credentials); + const oauth = oauthRuntime.getProvider("openai-codex")?.auth.oauth; + if (!oauth) throw new Error("OpenAI Codex OAuth provider is not registered"); + const refresh = vi.fn(oauth.refresh); + oauth.refresh = refresh; + + await expect(getProviderCredential("openai-codex", oauthRuntime, credentials, { refresh: false })).resolves.toBe( + "old-token", + ); + expect(refresh).not.toHaveBeenCalled(); + }); + + test("refreshes OAuth by default", async () => { + const credentials = AuthStorage.inMemory({ + "openai-codex": { type: "oauth", access: "old-token", refresh: "refresh-token", expires: 0 }, + }); + const runtime = await createRuntime(credentials); + const oauth = runtime.getProvider("openai-codex")?.auth.oauth; + if (!oauth) throw new Error("OpenAI Codex OAuth provider is not registered"); + const refresh = vi.fn(async () => ({ + type: "oauth" as const, + access: "fresh-token", + refresh: "refresh-token", + expires: Date.now() + 60 * 60 * 1000, + })); + oauth.refresh = refresh; + + await expect( + checkProviderAuth(parseArgs(["--provider", "openai-codex"]), runtime, { refresh: true }), + ).resolves.toMatchObject({ + status: "ready", + }); + expect(refresh).toHaveBeenCalledOnce(); + }); + + test("reports an unknown provider as not ready", async () => { + const runtime = await createRuntime(AuthStorage.inMemory()); + + await expect(checkProviderAuth(parseArgs(["--provider", "not-installed"]), runtime)).resolves.toEqual({ + status: "not_ready", + provider: "not-installed", + reason: "provider_not_found", + }); + }); + + test("does not treat an unresolved stored environment reference as configured", async () => { + const authPath = join(tempDir, "auth.json"); + writeFileSync(authPath, JSON.stringify({ openai: { type: "api_key", key: "$MISSING_AUTH_CHECK_KEY" } }), "utf-8"); + const runtime = await createRuntime(new ReadOnlyAuthStorage(authPath)); + + await expect(checkProviderAuth(parseArgs(["--provider", "openai"]), runtime)).resolves.toEqual({ + status: "not_ready", + provider: "openai", + reason: "credentials_not_configured", + }); + }); + + test("reports malformed auth state as invalid", async () => { + const authPath = join(tempDir, "auth.json"); + writeFileSync(authPath, "{invalid-json", "utf-8"); + const runtime = await createRuntime(new ReadOnlyAuthStorage(authPath)); + + await expect(checkProviderAuth(parseArgs(["--provider", "openai"]), runtime)).resolves.toEqual({ + status: "invalid", + provider: "openai", + reason: "invalid_state", + }); + }); + + test("does not create an auth file or its parent directory", async () => { + const authPath = join(tempDir, "agent", "auth.json"); + const runtime = await createRuntime(new ReadOnlyAuthStorage(authPath)); + + await expect(checkProviderAuth(parseArgs(["--provider", "openai"]), runtime)).resolves.toMatchObject({ + status: "not_ready", + reason: "credentials_not_configured", + }); + expect(existsSync(authPath)).toBe(false); + expect(existsSync(join(tempDir, "agent"))).toBe(false); + }); + + test("accepts optional JSON output, credential output, and --no-refresh", () => { + expect(parseAuthCommand(["auth", "check", "--provider", "openai"])).toEqual({ + kind: "check", + args: ["--provider", "openai"], + json: false, + credentials: false, + noRefresh: false, + }); + expect( + parseAuthCommand(["auth", "check", "--json", "--credentials", "--no-refresh", "--provider", "openai"]), + ).toEqual({ + kind: "check", + args: ["--provider", "openai"], + json: true, + credentials: true, + noRefresh: true, + }); + }); + + test("creates an auth-check runtime without catalog storage", async () => { + const runtime = await createAuthCheckModelRuntime(AuthStorage.inMemory()); + expect(runtime.getProvider("openai")).toBeDefined(); + }); +}); diff --git a/packages/coding-agent/test/auth-storage.test.ts b/packages/coding-agent/test/auth-storage.test.ts new file mode 100644 index 0000000000000000000000000000000000000000..61cdbefba0fad23d7ee2b71c8e5b49170601f63d --- /dev/null +++ b/packages/coding-agent/test/auth-storage.test.ts @@ -0,0 +1,551 @@ +import { chmodSync, existsSync, mkdirSync, readFileSync, rmSync, statSync, writeFileSync } from "node:fs"; +import { tmpdir } from "node:os"; +import { join } from "node:path"; +import { type CredentialStore, createModels, type Provider } from "@earendil-works/pi-ai"; +import lockfile from "proper-lockfile"; +import { afterEach, beforeEach, describe, expect, test, vi } from "vitest"; +import { AuthStorage, FileAuthStorageBackend } from "../src/core/auth-storage.ts"; + +describe("AuthStorage", () => { + const tempDir = join(tmpdir(), `pi-test-auth-storage-${Date.now()}-${Math.random().toString(36).slice(2)}`); + const authJsonPath = join(tempDir, "auth.json"); + + beforeEach(() => { + if (existsSync(tempDir)) rmSync(tempDir, { recursive: true }); + mkdirSync(tempDir, { recursive: true }); + }); + + afterEach(() => { + if (existsSync(tempDir)) rmSync(tempDir, { recursive: true }); + vi.restoreAllMocks(); + }); + + function writeAuthJson(data: Record): void { + writeFileSync(authJsonPath, JSON.stringify(data)); + } + + test("reads and resolves stored API-key credentials", async () => { + const original = process.env.TEST_AUTH_STORAGE_KEY; + process.env.TEST_AUTH_STORAGE_KEY = "environment-key"; + try { + writeAuthJson({ anthropic: { type: "api_key", key: "$TEST_AUTH_STORAGE_KEY" } }); + const storage = AuthStorage.create(authJsonPath); + expect(await storage.read("anthropic")).toEqual({ type: "api_key", key: "environment-key" }); + } finally { + if (original === undefined) delete process.env.TEST_AUTH_STORAGE_KEY; + else process.env.TEST_AUTH_STORAGE_KEY = original; + } + }); + + test("resolves command-backed API-key credentials", async () => { + writeAuthJson({ anthropic: { type: "api_key", key: "!printf 'command-key'" } }); + const storage = AuthStorage.create(authJsonPath); + expect(await storage.read("anthropic")).toEqual({ type: "api_key", key: "command-key" }); + }); + + test("returns OAuth credentials unchanged", async () => { + const credential = { + type: "oauth" as const, + access: "access-token", + refresh: "refresh-token", + expires: Date.now() + 60_000, + }; + const storage = AuthStorage.inMemory({ anthropic: credential }); + expect(await storage.read("anthropic")).toEqual(credential); + }); + + test("credential-scoped env takes precedence and remains inspectable", async () => { + writeAuthJson({ + anthropic: { + type: "api_key", + key: "$SCOPED_KEY", + env: { SCOPED_KEY: "scoped-value", REGION: "test-region" }, + }, + }); + const storage = AuthStorage.create(authJsonPath); + expect(await storage.read("anthropic")).toMatchObject({ + key: "scoped-value", + env: { SCOPED_KEY: "scoped-value", REGION: "test-region" }, + }); + }); + + test("coalesces file reloads across concurrent readers and storage instances", async () => { + writeAuthJson({ anthropic: { type: "api_key", key: "old" } }); + const first = AuthStorage.create(authJsonPath); + const second = AuthStorage.create(authJsonPath); + const lockSpy = vi.spyOn(lockfile, "lock"); + + writeAuthJson({ + anthropic: { type: "api_key", key: "new" }, + openai: { type: "api_key", key: "openai-key" }, + }); + + const [anthropic, openai, credentials] = await Promise.all([ + first.read("anthropic", { signal: new AbortController().signal }), + second.read("openai", { signal: new AbortController().signal }), + first.list({ signal: new AbortController().signal }), + ]); + expect(anthropic).toEqual({ type: "api_key", key: "new" }); + expect(openai).toEqual({ type: "api_key", key: "openai-key" }); + expect(credentials).toEqual([ + { providerId: "anthropic", type: "api_key" }, + { providerId: "openai", type: "api_key" }, + ]); + expect(lockSpy).toHaveBeenCalledTimes(1); + + await expect(second.read("anthropic")).resolves.toEqual({ type: "api_key", key: "new" }); + expect(lockSpy).toHaveBeenCalledTimes(1); + + const otherPath = join(tempDir, "other-auth.json"); + writeFileSync(otherPath, JSON.stringify({ other: { type: "api_key", key: "other-key" } })); + const otherFirst = AuthStorage.create(otherPath); + const otherSecond = AuthStorage.create(otherPath); + await otherFirst.read("other"); + await otherSecond.read("other"); + await otherFirst.list(); + expect(lockSpy).toHaveBeenCalledTimes(1); + + const third = AuthStorage.create(authJsonPath); + writeAuthJson({ anthropic: { type: "api_key", key: "newest" } }); + const [firstReload, thirdReload] = await Promise.all([first.read("anthropic"), third.read("anthropic")]); + expect(firstReload).toEqual({ type: "api_key", key: "newest" }); + expect(thirdReload).toEqual({ type: "api_key", key: "newest" }); + expect(lockSpy).toHaveBeenCalledTimes(2); + }); + + test("keeps a coalesced reload alive while another credential reader is waiting", async () => { + writeAuthJson({ anthropic: { type: "api_key", key: "old" } }); + const storage = AuthStorage.create(authJsonPath); + writeAuthJson({ anthropic: { type: "api_key", key: "new" } }); + let grantLock: (() => void) | undefined; + const lockGranted = new Promise((resolve) => { + grantLock = resolve; + }); + const release = vi.fn(async () => {}); + const lockSpy = vi.spyOn(lockfile, "lock").mockImplementation(async () => { + await lockGranted; + return release; + }); + const firstController = new AbortController(); + const secondController = new AbortController(); + const first = storage.read("anthropic", { signal: firstController.signal }); + const second = storage.read("anthropic", { signal: secondController.signal }); + + firstController.abort(); + await expect(first).rejects.toMatchObject({ name: "AbortError" }); + grantLock?.(); + await expect(second).resolves.toEqual({ type: "api_key", key: "new" }); + expect(lockSpy).toHaveBeenCalledTimes(1); + expect(release).toHaveBeenCalledTimes(1); + }); + + test.skipIf(process.platform === "win32")("creates new auth files with owner-only permissions", () => { + AuthStorage.create(authJsonPath); + + expect(statSync(authJsonPath).mode & 0o777).toBe(0o600); + }); + + test.skipIf(process.platform === "win32")("preserves the mode of an existing auth file", async () => { + writeAuthJson({ anthropic: { type: "api_key", key: "old" } }); + chmodSync(authJsonPath, 0o660); + const storage = AuthStorage.create(authJsonPath); + + await storage.modify("anthropic", async () => ({ type: "api_key", key: "new" })); + + expect(statSync(authJsonPath).mode & 0o777).toBe(0o660); + }); + + test("modify persists a credential while preserving unrelated external edits", async () => { + writeAuthJson({ anthropic: { type: "api_key", key: "old" } }); + const storage = AuthStorage.create(authJsonPath); + writeAuthJson({ + anthropic: { type: "api_key", key: "old" }, + openai: { type: "api_key", key: "external" }, + }); + + await storage.modify("anthropic", async () => ({ type: "api_key", key: "new" })); + + expect(JSON.parse(readFileSync(authJsonPath, "utf8"))).toEqual({ + anthropic: { type: "api_key", key: "new" }, + openai: { type: "api_key", key: "external" }, + }); + }); + + test("modify with undefined leaves the current credential unchanged", async () => { + writeAuthJson({ anthropic: { type: "api_key", key: "stored" } }); + const storage = AuthStorage.create(authJsonPath); + expect(await storage.modify("anthropic", async () => undefined)).toEqual({ type: "api_key", key: "stored" }); + expect(await storage.read("anthropic")).toEqual({ type: "api_key", key: "stored" }); + }); + + test("serializes concurrent modifications", async () => { + writeAuthJson({}); + const first = AuthStorage.create(authJsonPath); + const second = AuthStorage.create(authJsonPath); + await Promise.all([ + first.modify("anthropic", async () => ({ type: "api_key", key: "anthropic-key" })), + second.modify("openai", async () => ({ type: "api_key", key: "openai-key" })), + ]); + expect(JSON.parse(readFileSync(authJsonPath, "utf8"))).toEqual({ + anthropic: { type: "api_key", key: "anthropic-key" }, + openai: { type: "api_key", key: "openai-key" }, + }); + }); + + test("delete removes one credential while preserving others", async () => { + writeAuthJson({ + anthropic: { type: "api_key", key: "anthropic-key" }, + openai: { type: "api_key", key: "openai-key" }, + }); + const storage = AuthStorage.create(authJsonPath); + writeAuthJson({ + anthropic: { type: "api_key", key: "anthropic-key" }, + openai: { type: "api_key", key: "openai-key" }, + google: { type: "api_key", key: "external-key" }, + }); + await storage.delete("anthropic"); + await expect(storage.list()).resolves.toEqual([ + { providerId: "openai", type: "api_key" }, + { providerId: "google", type: "api_key" }, + ]); + expect(await storage.read("anthropic")).toBeUndefined(); + expect(await storage.read("openai")).toEqual({ type: "api_key", key: "openai-key" }); + expect(await storage.read("google")).toEqual({ type: "api_key", key: "external-key" }); + }); + + test("in-memory storage implements the same credential-store behavior", async () => { + const storage = AuthStorage.inMemory({ anthropic: { type: "api_key", key: "initial" } }); + expect(await storage.read("anthropic")).toEqual({ type: "api_key", key: "initial" }); + await storage.modify("anthropic", async () => ({ type: "api_key", key: "updated" })); + expect(await storage.read("anthropic")).toEqual({ type: "api_key", key: "updated" }); + await storage.delete("anthropic"); + await expect(storage.list()).resolves.toEqual([]); + }); + + test("does not write after lock acquisition failure and recovers on retry", async () => { + writeAuthJson({ anthropic: { type: "api_key", key: "stored" } }); + const storage = AuthStorage.create(authJsonPath); + const lockSpy = vi.spyOn(lockfile, "lock").mockRejectedValueOnce(new Error("lock unavailable")); + + await expect(storage.modify("openai", async () => ({ type: "api_key", key: "new" }))).rejects.toThrow( + "lock unavailable", + ); + expect(JSON.parse(readFileSync(authJsonPath, "utf8"))).toEqual({ + anthropic: { type: "api_key", key: "stored" }, + }); + + lockSpy.mockRestore(); + await storage.modify("openai", async () => ({ type: "api_key", key: "new" })); + expect(JSON.parse(readFileSync(authJsonPath, "utf8"))).toEqual({ + anthropic: { type: "api_key", key: "stored" }, + openai: { type: "api_key", key: "new" }, + }); + }); + + test("retries a briefly contended file lock", async () => { + writeAuthJson({ anthropic: { type: "api_key", key: "stored" } }); + const backend = new FileAuthStorageBackend(authJsonPath); + const release = vi.fn(async () => {}); + const lockSpy = vi + .spyOn(lockfile, "lock") + .mockRejectedValueOnce(Object.assign(new Error("locked"), { code: "ELOCKED" })) + .mockResolvedValueOnce(release); + vi.spyOn(Math, "random").mockReturnValue(0); + const update = vi.fn(async () => ({ result: undefined })); + + await backend.withLockAsync(update); + + expect(lockSpy).toHaveBeenCalledTimes(2); + expect(update).toHaveBeenCalledTimes(1); + expect(release).toHaveBeenCalledTimes(1); + }); + + test("surfaces a compromised file storage lock", async () => { + writeAuthJson({ anthropic: { type: "api_key", key: "stored" } }); + const backend = new FileAuthStorageBackend(authJsonPath); + const update = vi.fn(async () => ({ result: undefined, next: JSON.stringify({}) })); + const compromised = new Error("lock compromised"); + vi.spyOn(lockfile, "lock").mockImplementation(async (_file, options) => { + options?.onCompromised?.(compromised); + return async () => {}; + }); + + await expect(backend.withLockAsync(update)).rejects.toThrow(compromised); + expect(update).not.toHaveBeenCalled(); + expect(JSON.parse(readFileSync(authJsonPath, "utf8"))).toEqual({ + anthropic: { type: "api_key", key: "stored" }, + }); + }); + + test("pre-aborted file operations do not create the backing file or run the mutation", async () => { + const backend = new FileAuthStorageBackend(authJsonPath); + const controller = new AbortController(); + controller.abort(); + const update = vi.fn(async () => ({ result: undefined, next: JSON.stringify({}) })); + + await expect(backend.withLockAsync(update, { signal: controller.signal })).rejects.toMatchObject({ + name: "AbortError", + }); + expect(update).not.toHaveBeenCalled(); + expect(existsSync(authJsonPath)).toBe(false); + }); + + test("aborts while waiting for a held file lock without running the mutation later", async () => { + writeAuthJson({ anthropic: { type: "api_key", key: "stored" } }); + const release = await lockfile.lock(authJsonPath, { realpath: false }); + const backend = new FileAuthStorageBackend(authJsonPath); + const controller = new AbortController(); + const update = vi.fn(async () => ({ result: undefined, next: JSON.stringify({}) })); + const pending = backend.withLockAsync(update, { signal: controller.signal }); + + await new Promise((resolve) => setTimeout(resolve, 10)); + controller.abort(); + await expect(pending).rejects.toMatchObject({ name: "AbortError" }); + expect(update).not.toHaveBeenCalled(); + + await release(); + await new Promise((resolve) => setTimeout(resolve, 150)); + expect(update).not.toHaveBeenCalled(); + expect(JSON.parse(readFileSync(authJsonPath, "utf8"))).toEqual({ + anthropic: { type: "api_key", key: "stored" }, + }); + }); + + test("releases a file lock acquired concurrently with cancellation before mutation", async () => { + writeAuthJson({ anthropic: { type: "api_key", key: "stored" } }); + const backend = new FileAuthStorageBackend(authJsonPath); + const controller = new AbortController(); + const release = vi.fn(async () => {}); + vi.spyOn(lockfile, "lock").mockImplementation(async () => { + controller.abort(); + return release; + }); + const update = vi.fn(async () => ({ result: undefined, next: JSON.stringify({}) })); + + await expect(backend.withLockAsync(update, { signal: controller.signal })).rejects.toMatchObject({ + name: "AbortError", + }); + await new Promise((resolve) => setTimeout(resolve, 0)); + expect(update).not.toHaveBeenCalled(); + expect(release).toHaveBeenCalledTimes(1); + }); + + test("holds the file lock until a cancelled active callback settles without committing it", async () => { + writeAuthJson({ anthropic: { type: "api_key", key: "stored" } }); + const backend = new FileAuthStorageBackend(authJsonPath); + const controller = new AbortController(); + let markStarted: (() => void) | undefined; + let finish: (() => void) | undefined; + const started = new Promise((resolve) => { + markStarted = resolve; + }); + const blocked = new Promise((resolve) => { + finish = resolve; + }); + const pending = backend.withLockAsync( + async () => { + markStarted?.(); + await blocked; + return { result: undefined, next: JSON.stringify({ openai: { type: "api_key", key: "cancelled" } }) }; + }, + { signal: controller.signal }, + ); + + await started; + controller.abort(); + const competingMutation = vi.fn(async () => ({ + result: undefined, + next: JSON.stringify({ google: { type: "api_key", key: "committed" } }), + })); + const competing = backend.withLockAsync(competingMutation); + await new Promise((resolve) => setTimeout(resolve, 20)); + expect(competingMutation).not.toHaveBeenCalled(); + + finish?.(); + await expect(pending).rejects.toMatchObject({ name: "AbortError" }); + await competing; + expect(competingMutation).toHaveBeenCalledTimes(1); + expect(JSON.parse(readFileSync(authJsonPath, "utf8"))).toEqual({ + google: { type: "api_key", key: "committed" }, + }); + }); + + test("cancels a signalled credential read waiting for a held file lock", async () => { + writeAuthJson({ anthropic: { type: "api_key", key: "old" } }); + const storage = AuthStorage.create(authJsonPath); + writeAuthJson({ anthropic: { type: "api_key", key: "new-value" } }); + const release = await lockfile.lock(authJsonPath, { realpath: false }); + const lockSpy = vi.spyOn(lockfile, "lock"); + const controller = new AbortController(); + const pending = storage.read("anthropic", { signal: controller.signal }); + + await new Promise((resolve) => setTimeout(resolve, 10)); + controller.abort(); + await expect(pending).rejects.toMatchObject({ name: "AbortError" }); + await release(); + await new Promise((resolve) => setTimeout(resolve, 150)); + expect(lockSpy).toHaveBeenCalledTimes(1); + await expect(storage.read("anthropic")).resolves.toEqual({ type: "api_key", key: "new-value" }); + }); + + test("serializes in-memory mutations across providers", async () => { + const storage = AuthStorage.inMemory(); + let markStarted: (() => void) | undefined; + let finish: (() => void) | undefined; + const started = new Promise((resolve) => { + markStarted = resolve; + }); + const blocked = new Promise((resolve) => { + finish = resolve; + }); + const first = storage.modify("anthropic", async () => { + markStarted?.(); + await blocked; + return { type: "api_key", key: "anthropic-key" }; + }); + await started; + const secondMutation = vi.fn(async () => ({ type: "api_key" as const, key: "openai-key" })); + const second = storage.modify("openai", secondMutation); + await new Promise((resolve) => setTimeout(resolve, 0)); + expect(secondMutation).not.toHaveBeenCalled(); + + finish?.(); + await Promise.all([first, second]); + expect(await storage.read("anthropic")).toEqual({ type: "api_key", key: "anthropic-key" }); + expect(await storage.read("openai")).toEqual({ type: "api_key", key: "openai-key" }); + }); + + test("cancels a queued in-memory mutation without running it later", async () => { + const storage = AuthStorage.inMemory(); + let markStarted: (() => void) | undefined; + let finish: (() => void) | undefined; + const started = new Promise((resolve) => { + markStarted = resolve; + }); + const blocked = new Promise((resolve) => { + finish = resolve; + }); + const first = storage.modify("anthropic", async () => { + markStarted?.(); + await blocked; + return { type: "api_key", key: "anthropic-key" }; + }); + await started; + const controller = new AbortController(); + const secondMutation = vi.fn(async () => ({ type: "api_key" as const, key: "openai-key" })); + const second = storage.modify("openai", secondMutation, { signal: controller.signal }); + + controller.abort(); + await expect(second).rejects.toMatchObject({ name: "AbortError" }); + expect(secondMutation).not.toHaveBeenCalled(); + finish?.(); + await first; + await new Promise((resolve) => setTimeout(resolve, 0)); + expect(secondMutation).not.toHaveBeenCalled(); + expect(await storage.read("openai")).toBeUndefined(); + }); + + test("preserves the stored credential after cancelling an active refresh mutation", async () => { + const previous = { + type: "oauth" as const, + access: "expired", + refresh: "refresh-token", + expires: 0, + }; + const storage = AuthStorage.inMemory({ oauth: previous }); + const controller = new AbortController(); + let markStarted: (() => void) | undefined; + let finish: (() => void) | undefined; + const started = new Promise((resolve) => { + markStarted = resolve; + }); + const blocked = new Promise((resolve) => { + finish = resolve; + }); + const pending = storage.modify( + "oauth", + async () => { + markStarted?.(); + await blocked; + return { ...previous, access: "refreshed", expires: Date.now() + 60_000 }; + }, + { signal: controller.signal }, + ); + + await started; + controller.abort(); + await expect(pending).rejects.toMatchObject({ name: "AbortError" }); + const competingMutation = vi.fn(async () => ({ type: "api_key" as const, key: "other" })); + const competing = storage.modify("other", competingMutation); + await new Promise((resolve) => setTimeout(resolve, 0)); + expect(competingMutation).not.toHaveBeenCalled(); + + finish?.(); + await competing; + expect(competingMutation).toHaveBeenCalledTimes(1); + expect(await storage.read("oauth")).toEqual(previous); + }); + + test("translates a credential-store refresh failure and allows a later retry", async () => { + const providerId = "oauth-provider"; + const base = AuthStorage.inMemory({ + [providerId]: { + type: "oauth", + access: "expired-access", + refresh: "refresh-token", + expires: 0, + }, + }); + let failNextModify = true; + const credentials: CredentialStore = { + read: (id) => base.read(id), + list: () => base.list(), + modify: (id, fn) => { + if (failNextModify) { + failNextModify = false; + return Promise.reject(new Error("credential store unavailable")); + } + return base.modify(id, fn); + }, + delete: (id) => base.delete(id), + }; + const provider: Provider = { + id: providerId, + name: "OAuth Provider", + auth: { + oauth: { + name: "OAuth", + login: async () => { + throw new Error("not used"); + }, + refresh: async (credential) => ({ + ...credential, + access: "refreshed-access", + expires: Date.now() + 60_000, + }), + toAuth: async (credential) => ({ apiKey: credential.access }), + }, + }, + getModels: () => [], + stream: () => { + throw new Error("not used"); + }, + streamSimple: () => { + throw new Error("not used"); + }, + }; + const models = createModels({ credentials }); + models.setProvider(provider); + + await expect(models.getAuth(providerId)).rejects.toMatchObject({ code: "auth" }); + await expect(models.getAuth(providerId)).resolves.toMatchObject({ auth: { apiKey: "refreshed-access" } }); + }); + + test("does not overwrite malformed auth files", async () => { + writeAuthJson({ anthropic: { type: "api_key", key: "stored" } }); + const storage = AuthStorage.create(authJsonPath); + writeFileSync(authJsonPath, "{invalid-json", "utf8"); + await expect(storage.modify("openai", async () => ({ type: "api_key", key: "new" }))).rejects.toThrow(); + expect(readFileSync(authJsonPath, "utf8")).toBe("{invalid-json"); + }); +}); diff --git a/packages/coding-agent/test/bash-close-hang-windows.test.ts b/packages/coding-agent/test/bash-close-hang-windows.test.ts new file mode 100644 index 0000000000000000000000000000000000000000..a17834e24035e911bb26d646115b2ed2665ac0b9 --- /dev/null +++ b/packages/coding-agent/test/bash-close-hang-windows.test.ts @@ -0,0 +1,126 @@ +import { execFileSync } from "node:child_process"; +import { existsSync, mkdirSync, readFileSync, rmSync } from "node:fs"; +import { tmpdir } from "node:os"; +import { join } from "node:path"; +import { afterEach, beforeEach, describe, expect, it } from "vitest"; +import { executeBashWithOperations } from "../src/core/bash-executor.ts"; +import { createBashTool, createLocalBashOperations } from "../src/core/tools/bash.ts"; + +function toBashSingleQuotedArg(value: string): string { + return `'${value.replace(/\\/g, "/").replace(/'/g, `'"'"'`)}'`; +} + +function createInheritedStdioCommand(pidFile: string): string { + const pidFileArg = toBashSingleQuotedArg(pidFile); + return ( + 'node -e "' + + "const fs=require('fs');" + + "const {spawn}=require('child_process');" + + "const child=spawn(process.execPath,['-e','setTimeout(()=>{},60000)'],{stdio:'inherit',detached:true});" + + "fs.writeFileSync(process.argv[1], String(child.pid));" + + "child.unref();" + + "console.log('child-exiting');" + + '" ' + + pidFileArg + ); +} + +function cleanupDetachedChild(pidFile: string): void { + if (!existsSync(pidFile)) { + return; + } + + const pid = Number.parseInt(readFileSync(pidFile, "utf-8").trim(), 10); + if (Number.isFinite(pid) && pid > 0) { + try { + execFileSync("taskkill", ["/F", "/T", "/PID", String(pid)], { stdio: "ignore" }); + } catch { + // Process may have already exited. + } + } +} + +async function withTimeout(promise: Promise, ms: number, onTimeout: () => void): Promise { + return new Promise((resolve, reject) => { + const timeoutId = setTimeout(() => { + onTimeout(); + reject(new Error(`Timed out after ${ms}ms`)); + }, ms); + + promise.then( + (value) => { + clearTimeout(timeoutId); + resolve(value); + }, + (error: unknown) => { + clearTimeout(timeoutId); + reject(error); + }, + ); + }); +} + +function getTextOutput(result: { content?: Array<{ type: string; text?: string }> }): string { + return ( + result.content + ?.filter((block) => block.type === "text") + .map((block) => block.text ?? "") + .join("\n") ?? "" + ); +} + +describe.skipIf(process.platform !== "win32")("Windows child-process close handling", () => { + let testDir: string; + + beforeEach(() => { + testDir = join(tmpdir(), `coding-agent-bash-close-test-${Date.now()}-${Math.random().toString(36).slice(2)}`); + mkdirSync(testDir, { recursive: true }); + }); + + afterEach(() => { + rmSync(testDir, { recursive: true, force: true }); + }); + + it("executeBash resolves after the shell exits even if inherited stdio handles stay open", async () => { + const pidFile = join(testDir, "executor-grandchild.pid"); + const command = createInheritedStdioCommand(pidFile); + const controller = new AbortController(); + + try { + const result = await withTimeout( + executeBashWithOperations(command, process.cwd(), createLocalBashOperations(), { + signal: controller.signal, + }), + 3000, + () => { + controller.abort(); + }, + ); + + expect(result.output).toContain("child-exiting"); + expect(result.exitCode).toBe(0); + expect(result.cancelled).toBe(false); + } finally { + controller.abort(); + cleanupDetachedChild(pidFile); + } + }); + + it("bash tool resolves after the shell exits even if inherited stdio handles stay open", async () => { + const pidFile = join(testDir, "tool-grandchild.pid"); + const command = createInheritedStdioCommand(pidFile); + const controller = new AbortController(); + const bashTool = createBashTool(testDir); + + try { + const result = await withTimeout(bashTool.execute("test-call", { command }, controller.signal), 3000, () => { + controller.abort(); + }); + + expect(getTextOutput(result)).toContain("child-exiting"); + } finally { + controller.abort(); + cleanupDetachedChild(pidFile); + } + }); +}); diff --git a/packages/coding-agent/test/bash-execution-width.test.ts b/packages/coding-agent/test/bash-execution-width.test.ts new file mode 100644 index 0000000000000000000000000000000000000000..9cc64e8c18c4cd671587701ee3338f22a553cf08 --- /dev/null +++ b/packages/coding-agent/test/bash-execution-width.test.ts @@ -0,0 +1,80 @@ +/** + * Test that BashExecutionComponent's collapsed output respects the render-time width, + * not a stale captured width. Regression test for #2569. + */ +import { visibleWidth } from "@earendil-works/pi-tui"; +import { beforeAll, describe, expect, it } from "vitest"; +import { BashExecutionComponent } from "../src/modes/interactive/components/bash-execution.ts"; +import { initTheme } from "../src/modes/interactive/theme/theme.ts"; + +/** Minimal TUI stub that only exposes terminal.columns */ +function createTuiStub(columns: number): { columns: number; stub: any } { + const state = { columns }; + const stub = { + terminal: { + get columns() { + return state.columns; + }, + get rows() { + return 24; + }, + }, + // Loader calls ui.addInterval / ui.removeInterval + addInterval: (_cb: () => void, _ms: number) => ({ dispose: () => {} }), + removeInterval: () => {}, + requestRender: () => {}, + }; + return { columns: state.columns, stub }; +} + +describe("BashExecutionComponent width handling (#2569)", () => { + beforeAll(() => { + initTheme(undefined, false); + }); + + it("collapsed preview lines respect render-time width, not construction-time width", () => { + const wideWidth = 200; + const narrowWidth = 80; + + const { stub } = createTuiStub(wideWidth); + const component = new BashExecutionComponent("pwd", stub); + + // Add output with long lines that will wrap differently at different widths + const longLine = "x".repeat(150); + component.appendOutput(`${longLine}\n${longLine}\n`); + + // Complete the command so it enters collapsed mode + component.setComplete(0, false); + + // Render at the narrow width (simulating a resize or split pane) + const lines = component.render(narrowWidth); + + // Every rendered line must fit within the narrow width + for (let i = 0; i < lines.length; i++) { + const w = visibleWidth(lines[i]); + expect(w, `Line ${i} visibleWidth=${w} > ${narrowWidth}`).toBeLessThanOrEqual(narrowWidth); + } + }); + + it("re-computes lines when width changes between renders", () => { + const { stub } = createTuiStub(200); + const component = new BashExecutionComponent("echo hello", stub); + + const longLine = "abcdefghij".repeat(20); // 200 chars + component.appendOutput(`${longLine}\n`); + component.setComplete(0, false); + + // First render at width 200 + const lines200 = component.render(200); + for (const line of lines200) { + expect(visibleWidth(line)).toBeLessThanOrEqual(200); + } + + // Second render at width 60 (split pane scenario) + const lines60 = component.render(60); + for (let i = 0; i < lines60.length; i++) { + const w = visibleWidth(lines60[i]); + expect(w, `Line ${i} visibleWidth=${w} > 60`).toBeLessThanOrEqual(60); + } + }); +}); diff --git a/packages/coding-agent/test/block-images.test.ts b/packages/coding-agent/test/block-images.test.ts new file mode 100644 index 0000000000000000000000000000000000000000..935d854d55f9fca319d93481f1dc0b04d3d5f260 --- /dev/null +++ b/packages/coding-agent/test/block-images.test.ts @@ -0,0 +1,148 @@ +import { mkdirSync, rmSync, writeFileSync } from "fs"; +import { tmpdir } from "os"; +import { join } from "path"; +import { afterEach, beforeEach, describe, expect, it } from "vitest"; +import { processFileArguments } from "../src/cli/file-processor.ts"; +import { SettingsManager } from "../src/core/settings-manager.ts"; +import { createReadTool } from "../src/core/tools/read.ts"; + +// 1x1 red PNG image as base64 (smallest valid PNG) +const TINY_PNG_BASE64 = + "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mP8z8DwHwAFBQIAX8jx0gAAAABJRU5ErkJggg=="; + +function createTinyBmp1x1Red24bpp(): Buffer { + const buffer = Buffer.alloc(58); + buffer.write("BM", 0, "ascii"); + buffer.writeUInt32LE(buffer.length, 2); + buffer.writeUInt32LE(54, 10); + buffer.writeUInt32LE(40, 14); + buffer.writeInt32LE(1, 18); + buffer.writeInt32LE(1, 22); + buffer.writeUInt16LE(1, 26); + buffer.writeUInt16LE(24, 28); + buffer.writeUInt32LE(0, 30); + buffer.writeUInt32LE(4, 34); + buffer[56] = 0xff; + return buffer; +} + +describe("blockImages setting", () => { + describe("SettingsManager", () => { + it("should default blockImages to false", () => { + const manager = SettingsManager.inMemory({}); + expect(manager.getBlockImages()).toBe(false); + }); + + it("should return true when blockImages is set to true", () => { + const manager = SettingsManager.inMemory({ images: { blockImages: true } }); + expect(manager.getBlockImages()).toBe(true); + }); + + it("should persist blockImages setting via setBlockImages", () => { + const manager = SettingsManager.inMemory({}); + expect(manager.getBlockImages()).toBe(false); + + manager.setBlockImages(true); + expect(manager.getBlockImages()).toBe(true); + + manager.setBlockImages(false); + expect(manager.getBlockImages()).toBe(false); + }); + + it("should handle blockImages alongside autoResize", () => { + const manager = SettingsManager.inMemory({ + images: { autoResize: true, blockImages: true }, + }); + expect(manager.getImageAutoResize()).toBe(true); + expect(manager.getBlockImages()).toBe(true); + }); + }); + + describe("Read tool", () => { + let testDir: string; + + beforeEach(() => { + testDir = join(tmpdir(), `block-images-test-${Date.now()}`); + mkdirSync(testDir, { recursive: true }); + }); + + afterEach(() => { + rmSync(testDir, { recursive: true, force: true }); + }); + + it("should always read images (filtering happens at convertToLlm layer)", async () => { + // Create test image + const imagePath = join(testDir, "test.png"); + writeFileSync(imagePath, Buffer.from(TINY_PNG_BASE64, "base64")); + + const tool = createReadTool(testDir); + const result = await tool.execute("test-1", { path: imagePath }); + + // Should have text note + image content + expect(result.content.length).toBeGreaterThanOrEqual(1); + const hasImage = result.content.some((c) => c.type === "image"); + expect(hasImage).toBe(true); + }); + + it("should read text files normally", async () => { + // Create test text file + const textPath = join(testDir, "test.txt"); + writeFileSync(textPath, "Hello, world!"); + + const tool = createReadTool(testDir); + const result = await tool.execute("test-2", { path: textPath }); + + expect(result.content).toHaveLength(1); + expect(result.content[0].type).toBe("text"); + const textContent = result.content[0] as { type: "text"; text: string }; + expect(textContent.text).toContain("Hello, world!"); + }); + }); + + describe("processFileArguments", () => { + let testDir: string; + + beforeEach(() => { + testDir = join(tmpdir(), `block-images-process-test-${Date.now()}`); + mkdirSync(testDir, { recursive: true }); + }); + + afterEach(() => { + rmSync(testDir, { recursive: true, force: true }); + }); + + it("should always process images (filtering happens at convertToLlm layer)", async () => { + // Create test image + const imagePath = join(testDir, "test.png"); + writeFileSync(imagePath, Buffer.from(TINY_PNG_BASE64, "base64")); + + const result = await processFileArguments([imagePath]); + + expect(result.images).toHaveLength(1); + expect(result.images[0].type).toBe("image"); + }); + + it("should process BMP images from disk as PNG attachments", async () => { + const imagePath = join(testDir, "test.bmp"); + writeFileSync(imagePath, createTinyBmp1x1Red24bpp()); + + const result = await processFileArguments([imagePath]); + + expect(result.images).toHaveLength(1); + expect(result.images[0].type).toBe("image"); + expect(result.images[0].mimeType).toBe("image/png"); + expect(result.text).toContain("[Image converted from image/bmp to image/png.]"); + }); + + it("should process text files normally", async () => { + // Create test text file + const textPath = join(testDir, "test.txt"); + writeFileSync(textPath, "Hello, world!"); + + const result = await processFileArguments([textPath]); + + expect(result.images).toHaveLength(0); + expect(result.text).toContain("Hello, world!"); + }); + }); +}); diff --git a/packages/coding-agent/test/branch-summarization.test.ts b/packages/coding-agent/test/branch-summarization.test.ts new file mode 100644 index 0000000000000000000000000000000000000000..cc34bd64b874cbdc5f5007ae053102188c175584 --- /dev/null +++ b/packages/coding-agent/test/branch-summarization.test.ts @@ -0,0 +1,135 @@ +import type { StreamFn } from "@earendil-works/pi-agent-core"; +import { + type AssistantMessage, + createAssistantMessageEventStream, + fauxAssistantMessage, + type Model, + type SimpleStreamOptions, +} from "@earendil-works/pi-ai"; +import { describe, expect, it } from "vitest"; +import { generateBranchSummary } from "../src/core/compaction/index.ts"; +import type { SessionEntry } from "../src/core/session-manager.ts"; + +const model: Model<"anthropic-messages"> = { + id: "test-model", + name: "Test Model", + api: "anthropic-messages", + provider: "anthropic", + baseUrl: "https://api.anthropic.com", + reasoning: false, + input: ["text"], + cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 }, + contextWindow: 200000, + maxTokens: 8192, +}; + +const entries: SessionEntry[] = [ + { + type: "message", + id: "branch-user", + parentId: null, + timestamp: new Date(1).toISOString(), + message: { role: "user", content: "Abandoned request", timestamp: 1 }, + }, +]; + +function response(content: AssistantMessage["content"]): AssistantMessage { + return { + ...fauxAssistantMessage(""), + content, + api: model.api, + provider: model.provider, + model: model.id, + }; +} + +describe("branch summarization", () => { + it("does not override tool choice for branch summaries", async () => { + let requestOptions: SimpleStreamOptions | undefined; + const streamFn: StreamFn = (_model, _context, options) => { + requestOptions = options; + const stream = createAssistantMessageEventStream(); + queueMicrotask(() => + stream.push({ type: "done", reason: "stop", message: response([{ type: "text", text: "summary" }]) }), + ); + return stream; + }; + + await generateBranchSummary(entries, { + model, + signal: new AbortController().signal, + streamFn, + }); + + expect(requestOptions?.maxTokens).toBe(4096); + expect(requestOptions?.toolChoice).toBeUndefined(); + }); + + it("clamps the branch summary output cap to the model limit", async () => { + let requestOptions: SimpleStreamOptions | undefined; + const streamFn: StreamFn = (_model, _context, options) => { + requestOptions = options; + const stream = createAssistantMessageEventStream(); + queueMicrotask(() => + stream.push({ type: "done", reason: "stop", message: response([{ type: "text", text: "summary" }]) }), + ); + return stream; + }; + + await generateBranchSummary(entries, { + model: { ...model, maxTokens: 1024 }, + signal: new AbortController().signal, + streamFn, + }); + + expect(requestOptions?.maxTokens).toBe(1024); + }); + + it("rejects tool calls from branch summaries", async () => { + const streamFn: StreamFn = () => { + const stream = createAssistantMessageEventStream(); + queueMicrotask(() => + stream.push({ + type: "done", + reason: "toolUse", + message: response([ + { type: "toolCall", id: "tool-call-1", name: "read", arguments: { path: "README.md" } }, + ]), + }), + ); + return stream; + }; + + const result = await generateBranchSummary(entries, { + model, + signal: new AbortController().signal, + streamFn, + }); + + expect(result.error).toBe("Branch summarization attempted to call a tool"); + }); + + it("rejects length-limited branch summaries", async () => { + const streamFn: StreamFn = () => { + const stream = createAssistantMessageEventStream(); + queueMicrotask(() => + stream.push({ + type: "done", + reason: "length", + message: { ...response([{ type: "text", text: "partial" }]), stopReason: "length" }, + }), + ); + return stream; + }; + + const result = await generateBranchSummary(entries, { + model, + signal: new AbortController().signal, + streamFn, + }); + + expect(result.error).toBe( + "Branch summarization failed: generation hit the token cap and the summary is incomplete", + ); + }); +}); diff --git a/packages/coding-agent/test/branch-summary-extensions.test.ts b/packages/coding-agent/test/branch-summary-extensions.test.ts new file mode 100644 index 0000000000000000000000000000000000000000..ecdc01797852d399a1b93b33cbbeb09770c01954 --- /dev/null +++ b/packages/coding-agent/test/branch-summary-extensions.test.ts @@ -0,0 +1,57 @@ +import type { Usage } from "@earendil-works/pi-ai/compat"; +import { afterEach, describe, expect, it } from "vitest"; +import { createHarness, type Harness } from "./suite/harness.ts"; +import { assistantMsg, userMsg } from "./utilities.ts"; + +describe("Branch summary extensions", () => { + const harnesses: Harness[] = []; + + afterEach(() => { + while (harnesses.length > 0) { + harnesses.pop()?.cleanup(); + } + }); + + it("persists extension-provided summary usage in session totals", async () => { + const usage: Usage = { + input: 10, + output: 20, + cacheRead: 30, + cacheWrite: 40, + totalTokens: 100, + cost: { input: 0.1, output: 0.2, cacheRead: 0.3, cacheWrite: 0.4, total: 1 }, + }; + const harness = await createHarness({ + extensionFactories: [ + (pi) => { + pi.on("session_before_tree", () => ({ + summary: { + summary: "Summary provided by extension", + usage, + }, + })); + }, + ], + }); + harnesses.push(harness); + + const targetId = harness.sessionManager.appendMessage(userMsg("first branch")); + harness.sessionManager.appendMessage(assistantMsg("first reply")); + harness.sessionManager.appendMessage(userMsg("abandoned branch work")); + const sourceId = harness.sessionManager.appendMessage(assistantMsg("abandoned reply")); + + const result = await harness.session.navigateTree(targetId, { summarize: true }); + const summaryEntry = result.summaryEntry; + + expect(summaryEntry?.type).toBe("branch_summary"); + expect(summaryEntry?.parentId).toBeNull(); + expect(summaryEntry?.fromId).toBe(sourceId); + expect(summaryEntry?.fromHook).toBe(true); + expect(summaryEntry?.summary).toBe("Summary provided by extension"); + expect(summaryEntry?.usage).toEqual(usage); + + const stats = harness.session.getSessionStats(); + expect(stats.tokens).toEqual({ input: 12, output: 22, cacheRead: 30, cacheWrite: 40, total: 104 }); + expect(stats.cost).toBe(1); + }); +}); diff --git a/packages/coding-agent/test/builtin-tool-strict-mode.test.ts b/packages/coding-agent/test/builtin-tool-strict-mode.test.ts new file mode 100644 index 0000000000000000000000000000000000000000..b06fe5a55477deb2450ac3a88fb26944c8ddacbc --- /dev/null +++ b/packages/coding-agent/test/builtin-tool-strict-mode.test.ts @@ -0,0 +1,110 @@ +import { mkdtempSync, rmSync, writeFileSync } from "node:fs"; +import { tmpdir } from "node:os"; +import { join } from "node:path"; +import { getModel } from "@earendil-works/pi-ai/compat"; +import { afterEach, describe, expect, it, vi } from "vitest"; +import { DefaultResourceLoader } from "../src/core/resource-loader.ts"; +import { createAgentSession } from "../src/core/sdk.ts"; +import { SessionManager } from "../src/core/session-manager.ts"; +import { SettingsManager } from "../src/core/settings-manager.ts"; +import { createAllToolDefinitions, createAllTools } from "../src/core/tools/index.ts"; +import { wrapToolDefinition } from "../src/core/tools/tool-definition-wrapper.ts"; + +const strictToolNames = ["read", "bash", "powershell", "edit", "write"] as const; + +describe("strict built-in tools", () => { + afterEach(() => vi.unstubAllEnvs()); + + it.each([undefined, "0", "1"])("prefers strict sampling with PI_EXPERIMENTAL=%s", (experimental) => { + vi.stubEnv("PI_EXPERIMENTAL", experimental); + const definitions = createAllToolDefinitions(process.cwd()); + const tools = createAllTools(process.cwd()); + for (const name of strictToolNames) { + expect(definitions[name].constrainedSampling).toEqual({ type: "json_schema", strict: "prefer" }); + expect(tools[name].constrainedSampling).toEqual(definitions[name].constrainedSampling); + } + for (const name of ["grep", "find", "ls"] as const) { + expect(definitions[name].constrainedSampling).toBeUndefined(); + } + // Strictness is a provider-side conversion, not a change to the execution schema. + expect(definitions.read.parameters.required).toEqual(["path"]); + expect(definitions.bash.parameters.required).toEqual(["command"]); + }); + + it("preserves explicit opt-outs when wrapping definitions for execution", () => { + const definitions = createAllToolDefinitions(process.cwd()); + for (const name of strictToolNames) { + const definition = definitions[name]; + const override = { ...definition, constrainedSampling: false as const }; + expect(wrapToolDefinition(override).constrainedSampling).toBe(false); + expect(override.execute).toBe(definition.execute); + expect(override.prepareArguments).toBe(definition.prepareArguments); + expect(override.renderCall).toBe(definition.renderCall); + expect(override.renderResult).toBe(definition.renderResult); + expect(override.promptGuidelines).toBe(definition.promptGuidelines); + expect(definition.constrainedSampling).toEqual({ type: "json_schema", strict: "prefer" }); + } + }); + + it.each([{ activeTools: [] }, { activeTools: ["read"] }, { activeTools: [...strictToolNames] }])( + "allows extensions to re-register tools without strict sampling: $activeTools", + async ({ activeTools }) => { + const cwd = mkdtempSync(join(tmpdir(), "pi-non-strict-tools-")); + const agentDir = join(cwd, "agent"); + const settingsManager = SettingsManager.inMemory({ defaultTools: activeTools }); + const resourceLoader = new DefaultResourceLoader({ + cwd, + agentDir, + settingsManager, + noExtensions: true, + noSkills: true, + noPromptTemplates: true, + noThemes: true, + extensionFactories: [ + (pi) => { + pi.on("session_start", () => { + const definitions = createAllToolDefinitions(cwd); + for (const name of strictToolNames) { + pi.registerTool({ ...definitions[name], constrainedSampling: false }); + } + pi.setActiveTools(activeTools); + }); + }, + ], + }); + try { + await resourceLoader.reload(); + const { session } = await createAgentSession({ + cwd, + agentDir, + model: getModel("anthropic", "claude-sonnet-4-5"), + settingsManager, + sessionManager: SessionManager.inMemory(cwd), + resourceLoader, + }); + try { + const originalPrompt = session.systemPrompt; + await session.bindExtensions({}); + expect(session.getActiveToolNames()).toEqual(activeTools); + expect(session.systemPrompt).toBe(originalPrompt); + for (const name of strictToolNames) { + expect(session.getToolDefinition(name)?.constrainedSampling).toBe(false); + } + for (const tool of session.agent.state.tools) { + expect(tool.constrainedSampling).toBe(false); + } + if (activeTools.includes("read")) { + writeFileSync(join(cwd, "sample.txt"), "still works"); + const read = session.agent.state.tools.find((tool) => tool.name === "read")!; + const result = await read.execute("read-test", { path: "sample.txt" }); + expect(result.content).toEqual([{ type: "text", text: "still works" }]); + } + } finally { + session.dispose(); + } + } finally { + rmSync(cwd, { recursive: true, force: true }); + } + }, + ); +}); diff --git a/packages/coding-agent/test/cache-stats.test.ts b/packages/coding-agent/test/cache-stats.test.ts new file mode 100644 index 0000000000000000000000000000000000000000..a7c45e69c3857a3e26ee92b561e5e117f916fbc6 --- /dev/null +++ b/packages/coding-agent/test/cache-stats.test.ts @@ -0,0 +1,143 @@ +import type { AssistantMessage } from "@earendil-works/pi-ai"; +import { describe, expect, it } from "vitest"; +import { + collectCacheMisses, + computeCacheWaste, + detectCacheMiss, + type ModelPriceSource, +} from "../src/core/cache-stats.ts"; +import type { SessionEntry } from "../src/core/session-manager.ts"; + +const zeroCost = { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 }; + +const models: ModelPriceSource = { + // $/million tokens; used as cache-read price fallback on full-miss turns + getModel: () => ({ cost: { cacheRead: 0.3 } }), +}; + +function assistant(options: { + input?: number; + cacheRead?: number; + cacheWrite?: number; + cost?: Partial; + model?: string; + timestamp?: number; +}): AssistantMessage { + return { + role: "assistant", + content: [], + api: "anthropic-messages", + provider: "test", + model: options.model ?? "test-model", + usage: { + input: options.input ?? 0, + output: 10, + cacheRead: options.cacheRead ?? 0, + cacheWrite: options.cacheWrite ?? 0, + totalTokens: 0, + cost: { ...zeroCost, ...options.cost }, + }, + stopReason: "stop", + timestamp: options.timestamp ?? 0, + } as AssistantMessage; +} + +function entry(message: AssistantMessage): SessionEntry { + return { type: "message", id: "x", parentId: null, timestamp: "", message } as SessionEntry; +} + +// Turn 1: fresh 100k cache write at $3.75/M +const turn1 = assistant({ cacheWrite: 100_000, cost: { cacheWrite: 0.375 }, timestamp: 0 }); +// Turn 2: healthy, everything read back at $0.30/M +const turn2 = assistant({ + cacheRead: 100_000, + cacheWrite: 5_000, + cost: { cacheRead: 0.03, cacheWrite: 0.019 }, + timestamp: 60_000, +}); + +describe("computeCacheWaste", () => { + it("accumulates missed tokens and cost across turns", () => { + // Turn 3: full miss, previous 105k prompt re-billed at $3.75/M write + const turn3 = assistant({ cacheWrite: 110_000, cost: { cacheWrite: 0.4125 }, timestamp: 120_000 }); + const totals = computeCacheWaste([entry(turn1), entry(turn2), entry(turn3)], models); + expect(totals.missedTokens).toBe(105_000); + // 105k at ($3.75 - $0.30)/M + expect(totals.missedCost).toBeCloseTo(0.36225, 5); + }); + + it("counts nothing for healthy sessions", () => { + const totals = computeCacheWaste([entry(turn1), entry(turn2)], models); + expect(totals.missedTokens).toBe(0); + expect(totals.missedCost).toBe(0); + }); + + it("skips the turn after a compaction reset", () => { + const reset = { type: "compaction", id: "c", parentId: null, timestamp: "" } as SessionEntry; + const afterReset = assistant({ cacheWrite: 20_000, cost: { cacheWrite: 0.075 } }); + const totals = computeCacheWaste([entry(turn1), reset, entry(afterReset)], models); + expect(totals.missedTokens).toBe(0); + }); + + it("counts misses caused by model switches", () => { + const otherModel = assistant({ cacheWrite: 100_000, cost: { cacheWrite: 0.375 }, model: "other-model" }); + const totals = computeCacheWaste([entry(turn1), entry(otherModel)], models); + expect(totals.missedTokens).toBe(100_000); + expect(totals.missCount).toBe(1); + }); + + it("skips providers that report no cache activity", () => { + const a = assistant({ input: 100_000 }); + const b = assistant({ input: 110_000 }); + const totals = computeCacheWaste([entry(a), entry(b)], models); + expect(totals.missedTokens).toBe(0); + }); +}); + +describe("collectCacheMisses", () => { + it("maps counted misses to their assistant messages by reference", () => { + const missTurn = assistant({ cacheWrite: 110_000, cost: { cacheWrite: 0.4125 }, timestamp: 120_000 }); + const misses = collectCacheMisses([entry(turn1), entry(turn2), entry(missTurn)], models); + expect(misses.size).toBe(1); + expect(misses.get(missTurn)?.missedTokens).toBe(105_000); + }); +}); + +describe("detectCacheMiss", () => { + it("detects a miss on a just-completed message with idle time", () => { + const missMessage = assistant({ cacheWrite: 110_000, cost: { cacheWrite: 0.4125 }, timestamp: 600_000 }); + const miss = detectCacheMiss([entry(turn1), entry(turn2)], missMessage, models); + expect(miss).toBeDefined(); + expect(miss?.missedTokens).toBe(105_000); + expect(miss?.missedCost).toBeCloseTo(0.36225, 5); + // 600s - 60s since the previous request + expect(miss?.idleMs).toBe(540_000); + expect(miss?.modelChanged).toBe(false); + }); + + it("flags model switches on detected misses", () => { + const otherModel = assistant({ + cacheWrite: 110_000, + cost: { cacheWrite: 0.4125 }, + model: "other-model", + timestamp: 120_000, + }); + const miss = detectCacheMiss([entry(turn1), entry(turn2)], otherModel, models); + expect(miss?.missedTokens).toBe(105_000); + expect(miss?.modelChanged).toBe(true); + }); + + it("returns undefined for healthy turns", () => { + const healthy = assistant({ + cacheRead: 105_000, + cacheWrite: 2_000, + cost: { cacheRead: 0.0315, cacheWrite: 0.0075 }, + timestamp: 120_000, + }); + expect(detectCacheMiss([entry(turn1), entry(turn2)], healthy, models)).toBeUndefined(); + }); + + it("returns undefined for the first turn of a session", () => { + expect(detectCacheMiss([], turn1, models)).toBeUndefined(); + }); +}); diff --git a/packages/coding-agent/test/changelog.test.ts b/packages/coding-agent/test/changelog.test.ts new file mode 100644 index 0000000000000000000000000000000000000000..979e7cdf66f2ce45a9a13b7f90b0709efc7342df --- /dev/null +++ b/packages/coding-agent/test/changelog.test.ts @@ -0,0 +1,49 @@ +import { describe, expect, test } from "vitest"; +import { type ChangelogEntry, normalizeChangelogLinks } from "../src/utils/changelog.ts"; + +const entry: ChangelogEntry = { + major: 0, + minor: 79, + patch: 0, + content: "", +}; + +describe("normalizeChangelogLinks", () => { + test("rewrites package-relative changelog links to tag-pinned GitHub source links", () => { + const markdown = [ + "[Project Trust](README.md#project-trust)", + "[Extensions](docs/extensions.md#project_trust)", + "[Examples](examples/extensions/)", + "[Root README](../../README.md#supply-chain-hardening)", + ].join("\n"); + + expect(normalizeChangelogLinks(markdown, entry)).toBe( + [ + "[Project Trust](https://github.com/earendil-works/pi/blob/v0.79.0/packages/coding-agent/README.md#project-trust)", + "[Extensions](https://github.com/earendil-works/pi/blob/v0.79.0/packages/coding-agent/docs/extensions.md#project_trust)", + "[Examples](https://github.com/earendil-works/pi/tree/v0.79.0/packages/coding-agent/examples/extensions/)", + "[Root README](https://github.com/earendil-works/pi/blob/v0.79.0/README.md#supply-chain-hardening)", + ].join("\n"), + ); + }); + + test("canonicalizes old repository URLs without changing external links", () => { + const markdown = [ + "[#5167](https://github.com/earendil-works/pi-mono/pull/5167)", + "[#4163](https://github.com/badlogic/pi-mono/issues/4163)", + "[Agent README](https://github.com/badlogic/pi-mono/blob/main/packages/agent/README.md)", + "[External](https://example.com/docs)", + "[Local anchor](#settings)", + ].join("\n"); + + expect(normalizeChangelogLinks(markdown, "0.79.0")).toBe( + [ + "[#5167](https://github.com/earendil-works/pi/pull/5167)", + "[#4163](https://github.com/earendil-works/pi/issues/4163)", + "[Agent README](https://github.com/earendil-works/pi/blob/v0.79.0/packages/agent/README.md)", + "[External](https://example.com/docs)", + "[Local anchor](#settings)", + ].join("\n"), + ); + }); +}); diff --git a/packages/coding-agent/test/chat-viewport.test.ts b/packages/coding-agent/test/chat-viewport.test.ts new file mode 100644 index 0000000000000000000000000000000000000000..c87215d70e6167169f0a4bceee88e020c6bced21 --- /dev/null +++ b/packages/coding-agent/test/chat-viewport.test.ts @@ -0,0 +1,26 @@ +import { Container } from "@earendil-works/pi-tui"; +import { describe, expect, test } from "vitest"; +import { createChatViewport } from "../src/modes/interactive/chat-viewport.ts"; + +describe("chat viewport", () => { + test("defaults the transcript scrollbar to auto and accepts overrides", () => { + const automatic = createChatViewport({ + document: new Container(), + pendingMessages: new Container(), + status: new Container(), + editor: new Container(), + footer: new Container(), + }); + const hidden = createChatViewport({ + document: new Container(), + pendingMessages: new Container(), + status: new Container(), + editor: new Container(), + footer: new Container(), + scrollbar: "hidden", + }); + + expect(automatic.transcript.scrollbar).toBe("auto"); + expect(hidden.transcript.scrollbar).toBe("hidden"); + }); +}); diff --git a/packages/coding-agent/test/clipboard-command.test.ts b/packages/coding-agent/test/clipboard-command.test.ts new file mode 100644 index 0000000000000000000000000000000000000000..aea94ec396650ec004be2378305e4dc22496a615 --- /dev/null +++ b/packages/coding-agent/test/clipboard-command.test.ts @@ -0,0 +1,39 @@ +import { describe, expect, test } from "vitest"; +import { runClipboardCommand } from "../src/utils/clipboard-command.ts"; + +describe("clipboard commands", () => { + test("preserves binary output and distinguishes empty success from failure", async () => { + expect( + await runClipboardCommand(process.execPath, ["-e", "process.stdout.write(Buffer.from([0, 255, 10]))"]), + ).toEqual(Buffer.from([0, 255, 10])); + expect(await runClipboardCommand(process.execPath, ["-e", ""])).toEqual(Buffer.alloc(0)); + expect(await runClipboardCommand(process.execPath, ["-e", "process.exit(1)"])).toBeUndefined(); + expect(await runClipboardCommand("pi-clipboard-command-does-not-exist", [])).toBeUndefined(); + }); + test("sends Unicode input to clipboard writers", async () => { + const script = + "let text = ''; process.stdin.setEncoding('utf8'); process.stdin.on('data', c => text += c); process.stdin.on('end', () => process.exit(text === 'café 日本語' ? 0 : 1));"; + expect(await runClipboardCommand(process.execPath, ["-e", script], { input: "café 日本語" })).toEqual( + Buffer.alloc(0), + ); + }); + test("times out without blocking the event loop", async () => { + let ticks = 0; + const timer = setInterval(() => ticks++, 10); + try { + expect( + await runClipboardCommand(process.execPath, ["-e", "setInterval(() => {}, 1000)"], { timeoutMs: 200 }), + ).toBeUndefined(); + expect(ticks).toBeGreaterThan(5); + } finally { + clearInterval(timer); + } + }); + test("rejects output above the buffer limit", async () => { + expect( + await runClipboardCommand(process.execPath, ["-e", "process.stdout.write(Buffer.alloc(1024))"], { + maxBufferBytes: 16, + }), + ).toBeUndefined(); + }); +}); diff --git a/packages/coding-agent/test/clipboard-image-bmp-conversion.test.ts b/packages/coding-agent/test/clipboard-image-bmp-conversion.test.ts new file mode 100644 index 0000000000000000000000000000000000000000..231d44b8124379e5688cbe222126fb5f233fedfe --- /dev/null +++ b/packages/coding-agent/test/clipboard-image-bmp-conversion.test.ts @@ -0,0 +1,66 @@ +/** + * Test for BMP to PNG conversion in clipboard image handling. + * Separate from clipboard-image.test.ts due to different mocking requirements. + * + * This tests the fix for WSL2/WSLg where clipboard often provides image/bmp + * instead of image/png. + */ +import { describe, expect, test, vi } from "vitest"; +import { readClipboardImage } from "../src/utils/clipboard-image.ts"; + +function createTinyBmp1x1Red24bpp(): Uint8Array { + // Minimal 1x1 24bpp BMP (BGR + row padding to 4 bytes) + // File size = 14 (BMP header) + 40 (DIB header) + 4 (pixel row) = 58 + const buffer = Buffer.alloc(58); + + // BITMAPFILEHEADER + buffer.write("BM", 0, "ascii"); + buffer.writeUInt32LE(buffer.length, 2); // file size + buffer.writeUInt16LE(0, 6); // reserved1 + buffer.writeUInt16LE(0, 8); // reserved2 + buffer.writeUInt32LE(54, 10); // pixel data offset + + // BITMAPINFOHEADER + buffer.writeUInt32LE(40, 14); // DIB header size + buffer.writeInt32LE(1, 18); // width + buffer.writeInt32LE(1, 22); // height (positive = bottom-up) + buffer.writeUInt16LE(1, 26); // planes + buffer.writeUInt16LE(24, 28); // bits per pixel + buffer.writeUInt32LE(0, 30); // compression (BI_RGB) + buffer.writeUInt32LE(4, 34); // image size (incl. padding) + buffer.writeInt32LE(0, 38); // x pixels per meter + buffer.writeInt32LE(0, 42); // y pixels per meter + buffer.writeUInt32LE(0, 46); // colors used + buffer.writeUInt32LE(0, 50); // important colors + + // Pixel data (B, G, R) + 1 byte padding + buffer[54] = 0x00; // B + buffer[55] = 0x00; // G + buffer[56] = 0xff; // R + buffer[57] = 0x00; // padding + + return new Uint8Array(buffer); +} + +// Mock wl-paste to return BMP +vi.mock("../src/utils/clipboard-command.ts", () => ({ + runClipboardCommand: vi.fn(async (command: string, args: string[]) => { + if (command === "wl-paste" && args.includes("--list-types")) return Buffer.from("image/bmp\n"); + if (command === "wl-paste" && args.includes("image/bmp")) return Buffer.from(createTinyBmp1x1Red24bpp()); + return undefined; + }), +})); + +vi.mock("@earendil-works/pi-tui", () => ({ + getNativeClipboard: () => ({ getImage: async () => createTinyBmp1x1Red24bpp() }), +})); + +describe("readClipboardImage BMP conversion", () => { + test.each(["linux", "win32"] as const)("%s: converts command/native BMP to PNG", async (platform) => { + const image = await readClipboardImage({ env: { WAYLAND_DISPLAY: "wayland-0" }, platform }); + + expect(image).not.toBeNull(); + expect(image!.mimeType).toBe("image/png"); + expect(Array.from(image!.bytes.slice(0, 4))).toEqual([0x89, 0x50, 0x4e, 0x47]); + }); +}); diff --git a/packages/coding-agent/test/clipboard-image-native-errors.test.ts b/packages/coding-agent/test/clipboard-image-native-errors.test.ts new file mode 100644 index 0000000000000000000000000000000000000000..ef64da4dc32793305f8e87c165845427cfb396b6 --- /dev/null +++ b/packages/coding-agent/test/clipboard-image-native-errors.test.ts @@ -0,0 +1,31 @@ +import type * as Tui from "@earendil-works/pi-tui"; +import { afterEach, expect, test, vi } from "vitest"; +import { InteractiveMode } from "../src/modes/interactive/interactive-mode.ts"; + +const readClipboardText = vi.hoisted(() => vi.fn<() => Promise>()); +vi.mock("@earendil-works/pi-tui", async (importOriginal) => ({ + ...(await importOriginal()), + getNativeClipboard: () => ({ + async getImage() { + throw new Error("Native clipboard operation failed"); + }, + }), +})); +vi.mock("../src/utils/clipboard.ts", () => ({ copyToClipboard: vi.fn(), readClipboardText })); +vi.mock("../src/utils/clipboard-command.ts", () => ({ runClipboardCommand: vi.fn(async () => undefined) })); +afterEach(() => vi.unstubAllEnvs()); + +test("native image errors abort paste without reading text or changing the editor", async () => { + vi.stubEnv("TERMUX_VERSION", ""); + const context = { + editor: { insertTextAtCursor: vi.fn() }, + ui: { requestRender: vi.fn() }, + }; + const prototype = InteractiveMode.prototype as unknown as { + handleClipboardPaste(this: typeof context): Promise; + }; + await prototype.handleClipboardPaste.call(context); + expect(readClipboardText).not.toHaveBeenCalled(); + expect(context.editor.insertTextAtCursor).not.toHaveBeenCalled(); + expect(context.ui.requestRender).not.toHaveBeenCalled(); +}); diff --git a/packages/coding-agent/test/clipboard-image.test.ts b/packages/coding-agent/test/clipboard-image.test.ts new file mode 100644 index 0000000000000000000000000000000000000000..e7aacc4fbd62ec4921eee44c13abdf1703942487 --- /dev/null +++ b/packages/coding-agent/test/clipboard-image.test.ts @@ -0,0 +1,134 @@ +import type { NativeClipboard } from "@earendil-works/pi-tui"; +import { writeFileSync } from "fs"; +import { beforeEach, describe, expect, test, vi } from "vitest"; +import { readClipboardImage } from "../src/utils/clipboard-image.ts"; + +const mocks = vi.hoisted(() => ({ + command: vi.fn<(command: string, args: string[], options?: unknown) => Promise>(), + getImage: vi.fn(), + getNativeClipboard: vi.fn<() => NativeClipboard | undefined>(), +})); + +vi.mock("../src/utils/clipboard-command.ts", () => ({ runClipboardCommand: mocks.command })); +vi.mock("@earendil-works/pi-tui", () => ({ getNativeClipboard: mocks.getNativeClipboard })); + +function commandResult(stdout: Buffer, status = 0): Buffer | undefined { + return status === 0 ? stdout : undefined; +} + +const png = Buffer.from([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a, 0, 0, 0, 13, 0x49, 0x48, 0x44, 0x52]); + +describe("readClipboardImage", () => { + beforeEach(() => { + vi.resetAllMocks(); + mocks.command.mockResolvedValue(commandResult(Buffer.alloc(0), 1)); + mocks.getImage.mockResolvedValue(png); + mocks.getNativeClipboard.mockReturnValue({ getText: async () => null, getImage: mocks.getImage }); + }); + + for (const [backend, command, env] of [ + ["wayland", "wl-paste", { WAYLAND_DISPLAY: "1", DISPLAY: ":0" }], + ["x11", "xclip", { DISPLAY: ":0" }], + ] as const) { + test.each([true, false])(`${backend}: command image present=%s stops fallback`, async (present) => { + mocks.command.mockImplementation(async (name, args) => { + expect(name).toBe(command); + const listing = args.includes("--list-types") || args.includes("TARGETS"); + return commandResult(listing ? Buffer.from(present ? "text/plain\nimage/png\n" : "text/plain\n") : png); + }); + expect(await readClipboardImage({ platform: "linux", env })).toEqual( + present ? { bytes: png, mimeType: "image/png" } : null, + ); + expect(mocks.command).toHaveBeenCalledTimes(present ? 2 : 1); + expect(mocks.getNativeClipboard).not.toHaveBeenCalled(); + }); + } + + test.each([png, null, new Uint8Array()])("native X11 result %j stops fallback", async (bytes) => { + mocks.getImage.mockResolvedValue(bytes); + expect(await readClipboardImage({ platform: "linux", env: { DISPLAY: ":0" } })).toEqual( + bytes?.length ? { bytes, mimeType: "image/png" } : null, + ); + expect(mocks.getNativeClipboard).toHaveBeenCalledExactlyOnceWith(); + expect(mocks.getImage).toHaveBeenCalledOnce(); + expect(mocks.command.mock.calls.map(([name]) => name)).toEqual(Array(5).fill("xclip")); + }); + test.each(["missing module", "unavailable display"])("Wayland: falls back to X11 after %s", async (failure) => { + if (failure === "missing module") mocks.getNativeClipboard.mockReturnValue(undefined); + else mocks.getImage.mockResolvedValue(undefined); + mocks.command.mockImplementation(async (command, args) => { + if (command === "wl-paste") return commandResult(Buffer.alloc(0), 1); + return commandResult(args.includes("TARGETS") ? Buffer.from("image/png\n") : Buffer.from(png)); + }); + expect(await readClipboardImage({ platform: "linux", env: { WAYLAND_DISPLAY: "1" } })).toEqual({ + bytes: png, + mimeType: "image/png", + }); + expect(mocks.getNativeClipboard).not.toHaveBeenCalled(); + }); + + test("WSL: tries PowerShell before a broken native X11 bridge", async () => { + mocks.getImage.mockRejectedValue(new Error("Broken X11 bridge")); + let tmpFile: string | undefined; + mocks.command.mockImplementation(async (command, args, options) => { + if (command === "wl-paste" || command === "xclip") return undefined; + if (command === "wslpath") { + tmpFile = args[1]; + return commandResult(Buffer.from("C:\\Users\\O'Hare\\clip.png\n")); + } + if (command === "powershell.exe") { + const spawnOptions = options as { env?: NodeJS.ProcessEnv }; + expect(spawnOptions.env?.PI_WSL_CLIPBOARD_IMAGE_PATH).toBeUndefined(); + expect(args[2]).toContain("$path = 'C:\\Users\\O''Hare\\clip.png'"); + if (!tmpFile) throw new Error("wslpath should be called before powershell.exe"); + writeFileSync(tmpFile, png); + return commandResult(Buffer.from("ok\n")); + } + throw new Error(`Unexpected command: ${command}`); + }); + expect(await readClipboardImage({ platform: "linux", env: { WSL_DISTRO_NAME: "Ubuntu" } })).toEqual({ + bytes: new Uint8Array(png), + mimeType: "image/png", + }); + expect(mocks.getNativeClipboard).not.toHaveBeenCalled(); + }); + + for (const platform of ["darwin", "win32"] as const) { + test.each([png, null, new Uint8Array(), undefined])(`${platform}: reads native image %j once`, async (bytes) => { + mocks.getImage.mockResolvedValue(bytes); + expect(await readClipboardImage({ platform, env: {} })).toEqual( + bytes?.length ? { bytes, mimeType: "image/png" } : null, + ); + expect(mocks.getImage).toHaveBeenCalledOnce(); + expect(mocks.command).not.toHaveBeenCalled(); + }); + } + + test("returns null without a native helper", async () => { + mocks.getNativeClipboard.mockReturnValue(undefined); + expect(await readClipboardImage({ platform: "win32", env: {} })).toBeNull(); + expect(mocks.getImage).not.toHaveBeenCalled(); + }); + + test.each(["linux", "win32"] as const)( + "%s: propagates native transfer errors without fallback", + async (platform) => { + const error = new Error("Native clipboard operation failed"); + mocks.getImage.mockImplementation(async () => { + throw error; + }); + await expect(readClipboardImage({ platform, env: { WAYLAND_DISPLAY: "1", DISPLAY: ":0" } })).rejects.toBe( + error, + ); + expect(mocks.command.mock.calls.map(([name]) => name)).toEqual( + platform === "linux" ? ["wl-paste", ...Array(5).fill("xclip")] : [], + ); + }, + ); + + test("Termux does not read image clipboards", async () => { + expect(await readClipboardImage({ platform: "linux", env: { TERMUX_VERSION: "0.119" } })).toBeNull(); + expect(mocks.getNativeClipboard).not.toHaveBeenCalled(); + expect(mocks.command).not.toHaveBeenCalled(); + }); +}); diff --git a/packages/coding-agent/test/clipboard.test.ts b/packages/coding-agent/test/clipboard.test.ts new file mode 100644 index 0000000000000000000000000000000000000000..c8b3903464d4d42ee3072855bad8f5cb229e420f --- /dev/null +++ b/packages/coding-agent/test/clipboard.test.ts @@ -0,0 +1,172 @@ +import type { NativeClipboard } from "@earendil-works/pi-tui"; +import { afterEach, beforeEach, describe, expect, test, vi } from "vitest"; +import { copyToClipboard, readClipboardText } from "../src/utils/clipboard.ts"; + +const mocks = vi.hoisted(() => ({ + clipboard: { + getText: vi.fn(), + getImage: vi.fn(), + setText: vi.fn<(text: string) => Promise>(), + }, + getNativeClipboard: vi.fn<() => NativeClipboard | undefined>(), + command: + vi.fn< + ( + command: string, + args: readonly string[], + options?: { input?: string; timeoutMs?: number }, + ) => Promise + >(), + platform: vi.fn<() => NodeJS.Platform>(), +})); +vi.mock("@earendil-works/pi-tui", () => ({ getNativeClipboard: mocks.getNativeClipboard })); +vi.mock("../src/utils/clipboard-command.ts", () => ({ runClipboardCommand: mocks.command })); +vi.mock("node:os", () => ({ platform: mocks.platform })); + +let originalWrite: typeof process.stdout.write; +let osc52Writes: string[]; +beforeEach(() => { + vi.resetAllMocks(); + for (const name of [ + "SSH_CONNECTION", + "SSH_CLIENT", + "MOSH_CONNECTION", + "WAYLAND_DISPLAY", + "DISPLAY", + "TERMUX_VERSION", + ]) + vi.stubEnv(name, ""); + mocks.platform.mockReturnValue("darwin"); + mocks.getNativeClipboard.mockReturnValue(mocks.clipboard); + mocks.clipboard.getText.mockResolvedValue(null); + mocks.clipboard.setText.mockResolvedValue(); + mocks.command.mockResolvedValue(Buffer.alloc(0)); + osc52Writes = []; + originalWrite = process.stdout.write.bind(process.stdout); + process.stdout.write = ((...args: Parameters) => { + const [chunk] = args; + if (typeof chunk === "string" && chunk.startsWith("\x1b]52;c;")) { + osc52Writes.push(chunk); + return true; + } + return originalWrite(...args); + }) as typeof process.stdout.write; +}); +afterEach(() => { + process.stdout.write = originalWrite; + vi.unstubAllEnvs(); +}); + +describe("readClipboardText", () => { + test("awaits native clipboard text and catches rejected reads", async () => { + mocks.clipboard.getText.mockResolvedValue("clipboard text"); + await expect(readClipboardText()).resolves.toBe("clipboard text"); + mocks.clipboard.getText.mockRejectedValue(new Error("clipboard unavailable")); + await expect(readClipboardText()).resolves.toBeNull(); + }); + for (const [env, command, args, calls] of [ + ["WAYLAND_DISPLAY", "wl-paste", ["--no-newline", "--type", "text"], ["wl-paste"]], + ["DISPLAY", "xclip", ["-selection", "clipboard", "-out"], ["xclip"]], + ["DISPLAY", "xsel", ["--clipboard", "--output"], ["xclip", "xsel"]], + ["TERMUX_VERSION", "termux-clipboard-get", [], ["termux-clipboard-get"]], + ] as const) { + test.each(["clipboard text", ""])(`${command} result %j stops fallback`, async (text) => { + // Regression test for #7248: empty Wayland content must not fall through to stale X11. + mocks.platform.mockReturnValue("linux"); + vi.stubEnv("DISPLAY", ":0"); + vi.stubEnv(env, "1"); + mocks.command.mockImplementation(async (name) => (name === command ? Buffer.from(text) : undefined)); + await expect(readClipboardText()).resolves.toBe(text || null); + expect(mocks.command.mock.calls.map(([name]) => name)).toEqual(calls); + expect(mocks.command).toHaveBeenLastCalledWith(command, args, { timeoutMs: 5000 }); + expect(mocks.getNativeClipboard).not.toHaveBeenCalled(); + }); + } + test.each(["native text", "", null, undefined])("uses native X11 after command failures: %j", async (text) => { + mocks.platform.mockReturnValue("linux"); + vi.stubEnv("DISPLAY", ":0"); + vi.stubEnv("WAYLAND_DISPLAY", "wayland-0"); + mocks.command.mockResolvedValue(undefined); + mocks.clipboard.getText.mockResolvedValue(text); + await expect(readClipboardText()).resolves.toBe(text || null); + expect(mocks.getNativeClipboard).toHaveBeenCalledExactlyOnceWith(); + expect(mocks.command.mock.calls.map(([name]) => name)).toEqual(["wl-paste", "xclip", "xsel"]); + }); + test("falls back to X11 tools when wl-paste is unavailable", async () => { + mocks.platform.mockReturnValue("linux"); + vi.stubEnv("WAYLAND_DISPLAY", "wayland-0"); + vi.stubEnv("DISPLAY", ":0"); + mocks.command.mockImplementation(async (name) => (name === "wl-paste" ? undefined : Buffer.from("X11 text"))); + await expect(readClipboardText()).resolves.toBe("X11 text"); + expect(mocks.getNativeClipboard).not.toHaveBeenCalled(); + }); +}); + +describe("copyToClipboard", () => { + test("local native success skips OSC 52 and commands", async () => { + await copyToClipboard("hello"); + expect(mocks.clipboard.setText).toHaveBeenCalledWith("hello"); + expect(osc52Writes).toHaveLength(0); + expect(mocks.command).not.toHaveBeenCalled(); + }); + test("Linux skips the native writer", async () => { + mocks.platform.mockReturnValue("linux"); + vi.stubEnv("DISPLAY", ":0"); + await copyToClipboard("hello"); + expect(mocks.getNativeClipboard).not.toHaveBeenCalled(); + expect(mocks.command).toHaveBeenCalledWith("xclip", ["-selection", "clipboard"], { + input: "hello", + timeoutMs: 5000, + }); + }); + test("waits for the native write before emitting remote OSC 52", async () => { + vi.stubEnv("SSH_CONNECTION", "client server"); + let complete = () => {}; + mocks.clipboard.setText.mockReturnValue( + new Promise((resolve) => { + complete = resolve; + }), + ); + const copy = copyToClipboard("hello"); + expect(osc52Writes).toHaveLength(0); + complete(); + await copy; + expect(osc52Writes).toHaveLength(1); + expect(mocks.command).not.toHaveBeenCalled(); + }); + test("a rejected native write falls back to pbcopy", async () => { + mocks.clipboard.setText.mockRejectedValue(new Error("native failed")); + await copyToClipboard("hello"); + expect(mocks.command).toHaveBeenCalledWith("pbcopy", [], { input: "hello", timeoutMs: 5000 }); + expect(osc52Writes).toHaveLength(0); + }); + test("a read-only native clipboard uses the command writer", async () => { + mocks.getNativeClipboard.mockReturnValue({ + getText: mocks.clipboard.getText, + getImage: mocks.clipboard.getImage, + }); + await copyToClipboard("hello"); + expect(mocks.command).toHaveBeenCalledOnce(); + }); + test("tries xclip and xsel after wl-copy fails", async () => { + mocks.platform.mockReturnValue("linux"); + vi.stubEnv("WAYLAND_DISPLAY", "wayland-0"); + vi.stubEnv("DISPLAY", ":0"); + mocks.command.mockImplementation(async (name) => (name === "xsel" ? Buffer.alloc(0) : undefined)); + await copyToClipboard("hello"); + expect(mocks.command.mock.calls.map(([name]) => name)).toEqual(["wl-copy", "xclip", "xsel"]); + expect(osc52Writes).toHaveLength(0); + }); + test("uses OSC 52 when native and command writes fail", async () => { + mocks.clipboard.setText.mockRejectedValue(new Error("native failed")); + mocks.command.mockResolvedValue(undefined); + await copyToClipboard("hello"); + expect(osc52Writes).toHaveLength(1); + }); + test("does not emit oversized OSC 52 payloads", async () => { + mocks.clipboard.setText.mockRejectedValue(new Error("native failed")); + mocks.command.mockResolvedValue(undefined); + await expect(copyToClipboard("x".repeat(80_000))).rejects.toThrow("Failed to copy to clipboard"); + expect(osc52Writes).toHaveLength(0); + }); +}); diff --git a/packages/coding-agent/test/compaction-extensions-example.test.ts b/packages/coding-agent/test/compaction-extensions-example.test.ts new file mode 100644 index 0000000000000000000000000000000000000000..9767e8a5cf5caf70b09a849300e8d87598b93a62 --- /dev/null +++ b/packages/coding-agent/test/compaction-extensions-example.test.ts @@ -0,0 +1,152 @@ +/** + * Verify the documentation example from extensions.md compiles and works. + */ + +import { describe, expect, it, vi } from "vitest"; +import type { ExtensionAPI, SessionBeforeCompactEvent, SessionCompactEvent } from "../src/core/extensions/index.ts"; + +vi.mock("@earendil-works/pi-coding-agent", () => ({ + convertToLlm: (messages: unknown) => messages, + serializeConversation: () => "conversation", +})); + +const { default: customCompactionExtension } = await import("../examples/extensions/custom-compaction.ts"); + +describe("Documentation example", () => { + it("custom compaction example should type-check correctly", () => { + // This is the example from extensions.md - verify it compiles + const exampleExtension = (pi: ExtensionAPI) => { + pi.on("session_before_compact", async (event: SessionBeforeCompactEvent, ctx) => { + // All these should be accessible on the event + const { preparation, branchEntries } = event; + // sessionManager, modelRegistry, and model come from ctx + const { sessionManager, modelRegistry } = ctx; + const { messagesToSummarize, turnPrefixMessages, tokensBefore, firstKeptEntryId, isSplitTurn } = + preparation; + + // Verify types + expect(Array.isArray(messagesToSummarize)).toBe(true); + expect(Array.isArray(turnPrefixMessages)).toBe(true); + expect(typeof isSplitTurn).toBe("boolean"); + expect(typeof tokensBefore).toBe("number"); + expect(typeof sessionManager.getEntries).toBe("function"); + expect(typeof modelRegistry.getApiKeyAndHeaders).toBe("function"); + expect(typeof firstKeptEntryId).toBe("string"); + expect(Array.isArray(branchEntries)).toBe(true); + + const summary = messagesToSummarize + .filter((m) => m.role === "user") + .map((m) => `- ${typeof m.content === "string" ? m.content.slice(0, 100) : "[complex]"}`) + .join("\n"); + + // Extensions return compaction content - SessionManager adds id/parentId + return { + compaction: { + summary: `User requests:\n${summary}`, + firstKeptEntryId, + tokensBefore, + }, + }; + }); + }; + + // Just verify the function exists and is callable + expect(typeof exampleExtension).toBe("function"); + }); + + it("custom compaction example dispatches through modelRegistry.complete", async () => { + let handler: ((event: any, ctx: any) => Promise) | undefined; + customCompactionExtension({ + on(event, fn) { + if (event === "session_before_compact") handler = fn as typeof handler; + }, + } as ExtensionAPI); + + expect(handler).toBeDefined(); + + const complete = vi.fn(async () => ({ + role: "assistant", + content: [{ type: "text", text: "custom provider summary" }], + provider: "example-custom", + api: "example-custom-api", + model: "summary-model", + stopReason: "stop", + usage: { + input: 1, + output: 2, + cacheRead: 0, + cacheWrite: 0, + totalTokens: 3, + cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 }, + }, + timestamp: Date.now(), + })); + const model = { + provider: "example-custom", + api: "example-custom-api", + id: "summary-model", + name: "Summary Model", + input: ["text"], + cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 }, + contextWindow: 1000, + maxTokens: 100, + }; + + const result = await handler!( + { + preparation: { + messagesToSummarize: [ + { role: "user", content: [{ type: "text", text: "please remember this" }], timestamp: Date.now() }, + ], + turnPrefixMessages: [], + tokensBefore: 42, + firstKeptEntryId: "entry-1", + }, + branchEntries: [], + signal: new AbortController().signal, + }, + { + ui: { notify: vi.fn() }, + modelRegistry: { + find: vi.fn(() => model), + complete, + }, + }, + ); + + expect(complete).toHaveBeenCalledWith( + model, + expect.objectContaining({ messages: expect.any(Array) }), + expect.objectContaining({ maxTokens: 8192 }), + ); + expect(complete).not.toHaveBeenCalledWith( + expect.anything(), + expect.anything(), + expect.objectContaining({ apiKey: expect.anything() }), + ); + expect(result).toMatchObject({ + compaction: { + summary: "custom provider summary", + firstKeptEntryId: "entry-1", + tokensBefore: 42, + }, + }); + }); + + it("compact event should have correct fields", () => { + const checkCompactEvent = (pi: ExtensionAPI) => { + pi.on("session_compact", async (event: SessionCompactEvent) => { + // These should all be accessible + const entry = event.compactionEntry; + const fromExtension = event.fromExtension; + + expect(entry.type).toBe("compaction"); + expect(typeof entry.summary).toBe("string"); + expect(typeof entry.tokensBefore).toBe("number"); + expect(typeof fromExtension).toBe("boolean"); + }); + }; + + expect(typeof checkCompactEvent).toBe("function"); + }); +}); diff --git a/packages/coding-agent/test/compaction-extensions.test.ts b/packages/coding-agent/test/compaction-extensions.test.ts new file mode 100644 index 0000000000000000000000000000000000000000..7b8f8e627fb1bf7b0d5045f15f29044e383c66ed --- /dev/null +++ b/packages/coding-agent/test/compaction-extensions.test.ts @@ -0,0 +1,416 @@ +import { createModelRegistry, getModelRuntime } from "./model-runtime-test-utils.ts"; +/** + * Tests for compaction extension events (before_compact / compact). + */ + +import { existsSync, mkdirSync, rmSync } from "node:fs"; +import { tmpdir } from "node:os"; +import { join } from "node:path"; +import { Agent } from "@earendil-works/pi-agent-core"; +import { getModel, streamSimple } from "@earendil-works/pi-ai/compat"; +import { afterEach, beforeEach, describe, expect, it } from "vitest"; +import { AgentSession } from "../src/core/agent-session.ts"; +import { AuthStorage } from "../src/core/auth-storage.ts"; +import { + createExtensionRuntime, + type Extension, + type SessionBeforeCompactEvent, + type SessionCompactEvent, + type SessionEvent, +} from "../src/core/extensions/index.ts"; +import { SessionManager } from "../src/core/session-manager.ts"; +import { SettingsManager } from "../src/core/settings-manager.ts"; +import { createSyntheticSourceInfo } from "../src/core/source-info.ts"; +import { createCodingTools } from "../src/index.ts"; +import { createTestResourceLoader } from "./utilities.ts"; + +const API_KEY = process.env.ANTHROPIC_OAUTH_TOKEN || process.env.ANTHROPIC_API_KEY; + +describe.skipIf(!API_KEY)("Compaction extensions", () => { + let session: AgentSession; + let tempDir: string; + let capturedEvents: SessionEvent[]; + + beforeEach(async () => { + tempDir = join(tmpdir(), `pi-compaction-extensions-test-${Date.now()}`); + mkdirSync(tempDir, { recursive: true }); + capturedEvents = []; + }); + + afterEach(async () => { + if (session) { + session.dispose(); + } + if (tempDir && existsSync(tempDir)) { + rmSync(tempDir, { recursive: true }); + } + }); + + function createExtension( + onBeforeCompact?: (event: SessionBeforeCompactEvent) => { cancel?: boolean; compaction?: any } | undefined, + onCompact?: (event: SessionCompactEvent) => void, + ): Extension { + const handlers = new Map Promise)[]>(); + + handlers.set("session_before_compact", [ + async (event: SessionBeforeCompactEvent) => { + capturedEvents.push(event); + if (onBeforeCompact) { + return onBeforeCompact(event); + } + return undefined; + }, + ]); + + handlers.set("session_compact", [ + async (event: SessionCompactEvent) => { + capturedEvents.push(event); + if (onCompact) { + onCompact(event); + } + return undefined; + }, + ]); + + return { + path: "test-extension", + resolvedPath: "/test/test-extension.ts", + sourceInfo: createSyntheticSourceInfo("", { source: "test" }), + handlers, + tools: new Map(), + messageRenderers: new Map(), + commands: new Map(), + flags: new Map(), + shortcuts: new Map(), + }; + } + + async function createSession(extensions: Extension[]) { + const model = getModel("anthropic", "claude-sonnet-4-5")!; + const agent = new Agent({ + getApiKey: () => API_KEY, + streamFn: streamSimple, + initialState: { + model, + systemPrompt: "You are a helpful assistant. Be concise.", + tools: createCodingTools(process.cwd()), + }, + }); + + const sessionManager = SessionManager.create(tempDir); + const settingsManager = SettingsManager.create(tempDir, tempDir); + settingsManager.applyOverrides({ compaction: { keepRecentTokens: 1 } }); + const authStorage = AuthStorage.create(join(tempDir, "auth.json")); + const modelRegistry = await createModelRegistry(authStorage); + + const runtime = createExtensionRuntime(); + const resourceLoader = { + ...createTestResourceLoader(), + getExtensions: () => ({ extensions, errors: [], runtime }), + }; + + session = new AgentSession({ + agent, + sessionManager, + settingsManager, + cwd: tempDir, + modelRuntime: getModelRuntime(modelRegistry), + resourceLoader, + }); + + return session; + } + + it("should emit before_compact and compact events", async () => { + const extension = createExtension(); + await createSession([extension]); + + await session.prompt("What is 2+2? Reply with just the number."); + await session.agent.waitForIdle(); + + await session.prompt("What is 3+3? Reply with just the number."); + await session.agent.waitForIdle(); + + await session.compact(); + + const beforeCompactEvents = capturedEvents.filter( + (e): e is SessionBeforeCompactEvent => e.type === "session_before_compact", + ); + const compactEvents = capturedEvents.filter((e): e is SessionCompactEvent => e.type === "session_compact"); + + expect(beforeCompactEvents.length).toBe(1); + expect(compactEvents.length).toBe(1); + + const beforeEvent = beforeCompactEvents[0]; + expect(beforeEvent.preparation).toBeDefined(); + expect(beforeEvent.preparation.messagesToSummarize).toBeDefined(); + expect(beforeEvent.preparation.turnPrefixMessages).toBeDefined(); + expect(beforeEvent.preparation.tokensBefore).toBeGreaterThanOrEqual(0); + expect(typeof beforeEvent.preparation.isSplitTurn).toBe("boolean"); + expect(beforeEvent.branchEntries).toBeDefined(); + // sessionManager, modelRegistry, and model are now on ctx, not event + + const afterEvent = compactEvents[0]; + expect(afterEvent.compactionEntry).toBeDefined(); + expect(afterEvent.compactionEntry.summary.length).toBeGreaterThan(0); + expect(afterEvent.compactionEntry.tokensBefore).toBeGreaterThanOrEqual(0); + expect(afterEvent.fromExtension).toBe(false); + }, 120000); + + it("should allow extensions to cancel compaction", async () => { + const extension = createExtension(() => ({ cancel: true })); + await createSession([extension]); + + await session.prompt("What is 2+2? Reply with just the number."); + await session.agent.waitForIdle(); + + await expect(session.compact()).rejects.toThrow("Compaction cancelled"); + + const compactEvents = capturedEvents.filter((e) => e.type === "session_compact"); + expect(compactEvents.length).toBe(0); + }, 120000); + + it("should allow extensions to provide custom compaction", async () => { + const customSummary = "Custom summary from extension"; + + const extension = createExtension((event) => { + if (event.type === "session_before_compact") { + return { + compaction: { + summary: customSummary, + firstKeptEntryId: event.preparation.firstKeptEntryId, + tokensBefore: event.preparation.tokensBefore, + }, + }; + } + return undefined; + }); + await createSession([extension]); + + await session.prompt("What is 2+2? Reply with just the number."); + await session.agent.waitForIdle(); + + await session.prompt("What is 3+3? Reply with just the number."); + await session.agent.waitForIdle(); + + const result = await session.compact(); + + expect(result.summary).toBe(customSummary); + + const compactEvents = capturedEvents.filter((e) => e.type === "session_compact"); + expect(compactEvents.length).toBe(1); + + const afterEvent = compactEvents[0]; + if (afterEvent.type === "session_compact") { + expect(afterEvent.compactionEntry.summary).toBe(customSummary); + expect(afterEvent.fromExtension).toBe(true); + } + }, 120000); + + it("should include entries in compact event after compaction is saved", async () => { + const extension = createExtension(); + await createSession([extension]); + + await session.prompt("What is 2+2? Reply with just the number."); + await session.agent.waitForIdle(); + + await session.compact(); + + const compactEvents = capturedEvents.filter((e) => e.type === "session_compact"); + expect(compactEvents.length).toBe(1); + + const afterEvent = compactEvents[0]; + if (afterEvent.type === "session_compact") { + // sessionManager is now on ctx, use session.sessionManager directly + const entries = session.sessionManager.getEntries(); + const hasCompactionEntry = entries.some((e: { type: string }) => e.type === "compaction"); + expect(hasCompactionEntry).toBe(true); + } + }, 120000); + + it("should continue with default compaction if extension throws error", async () => { + const throwingExtension: Extension = { + path: "throwing-extension", + resolvedPath: "/test/throwing-extension.ts", + sourceInfo: createSyntheticSourceInfo("", { source: "test" }), + handlers: new Map Promise)[]>([ + [ + "session_before_compact", + [ + async (event: SessionBeforeCompactEvent) => { + capturedEvents.push(event); + throw new Error("Extension intentionally throws"); + }, + ], + ], + [ + "session_compact", + [ + async (event: SessionCompactEvent) => { + capturedEvents.push(event); + return undefined; + }, + ], + ], + ]), + tools: new Map(), + messageRenderers: new Map(), + commands: new Map(), + flags: new Map(), + shortcuts: new Map(), + }; + + await createSession([throwingExtension]); + + await session.prompt("What is 2+2? Reply with just the number."); + await session.agent.waitForIdle(); + + const result = await session.compact(); + + expect(result.summary).toBeDefined(); + expect(result.summary.length).toBeGreaterThan(0); + + const compactEvents = capturedEvents.filter((e): e is SessionCompactEvent => e.type === "session_compact"); + expect(compactEvents.length).toBe(1); + expect(compactEvents[0].fromExtension).toBe(false); + }, 120000); + + it("should call multiple extensions in order", async () => { + const callOrder: string[] = []; + + const extension1: Extension = { + path: "extension1", + resolvedPath: "/test/extension1.ts", + sourceInfo: createSyntheticSourceInfo("", { source: "test" }), + handlers: new Map Promise)[]>([ + [ + "session_before_compact", + [ + async () => { + callOrder.push("extension1-before"); + return undefined; + }, + ], + ], + [ + "session_compact", + [ + async () => { + callOrder.push("extension1-after"); + return undefined; + }, + ], + ], + ]), + tools: new Map(), + messageRenderers: new Map(), + commands: new Map(), + flags: new Map(), + shortcuts: new Map(), + }; + + const extension2: Extension = { + path: "extension2", + resolvedPath: "/test/extension2.ts", + sourceInfo: createSyntheticSourceInfo("", { source: "test" }), + handlers: new Map Promise)[]>([ + [ + "session_before_compact", + [ + async () => { + callOrder.push("extension2-before"); + return undefined; + }, + ], + ], + [ + "session_compact", + [ + async () => { + callOrder.push("extension2-after"); + return undefined; + }, + ], + ], + ]), + tools: new Map(), + messageRenderers: new Map(), + commands: new Map(), + flags: new Map(), + shortcuts: new Map(), + }; + + await createSession([extension1, extension2]); + + await session.prompt("What is 2+2? Reply with just the number."); + await session.agent.waitForIdle(); + + await session.compact(); + + expect(callOrder).toEqual(["extension1-before", "extension2-before", "extension1-after", "extension2-after"]); + }, 120000); + + it("should pass correct data in before_compact event", async () => { + let capturedBeforeEvent: SessionBeforeCompactEvent | null = null; + + const extension = createExtension((event) => { + capturedBeforeEvent = event; + return undefined; + }); + await createSession([extension]); + + await session.prompt("What is 2+2? Reply with just the number."); + await session.agent.waitForIdle(); + + await session.prompt("What is 3+3? Reply with just the number."); + await session.agent.waitForIdle(); + + await session.compact(); + + expect(capturedBeforeEvent).not.toBeNull(); + const event = capturedBeforeEvent!; + expect(typeof event.preparation.isSplitTurn).toBe("boolean"); + expect(event.preparation.firstKeptEntryId).toBeDefined(); + + expect(Array.isArray(event.preparation.messagesToSummarize)).toBe(true); + expect(Array.isArray(event.preparation.turnPrefixMessages)).toBe(true); + + expect(typeof event.preparation.tokensBefore).toBe("number"); + + expect(Array.isArray(event.branchEntries)).toBe(true); + + // sessionManager and model runtime remain available on the session. + expect(typeof session.sessionManager.getEntries).toBe("function"); + expect(typeof session.modelRuntime.getAuth).toBe("function"); + + const entries = session.sessionManager.getEntries(); + expect(Array.isArray(entries)).toBe(true); + expect(entries.length).toBeGreaterThan(0); + }, 120000); + + it("should use extension compaction even with different values", async () => { + const customSummary = "Custom summary with modified values"; + + const extension = createExtension((event) => { + if (event.type === "session_before_compact") { + return { + compaction: { + summary: customSummary, + firstKeptEntryId: event.preparation.firstKeptEntryId, + tokensBefore: 999, + }, + }; + } + return undefined; + }); + await createSession([extension]); + + await session.prompt("What is 2+2? Reply with just the number."); + await session.agent.waitForIdle(); + + const result = await session.compact(); + + expect(result.summary).toBe(customSummary); + expect(result.tokensBefore).toBe(999); + }, 120000); +}); diff --git a/packages/coding-agent/test/compaction-serialization.test.ts b/packages/coding-agent/test/compaction-serialization.test.ts new file mode 100644 index 0000000000000000000000000000000000000000..91ca9251aaeaecada7c285bda5201c2ae0b850df --- /dev/null +++ b/packages/coding-agent/test/compaction-serialization.test.ts @@ -0,0 +1,79 @@ +import type { Message } from "@earendil-works/pi-ai"; +import { describe, expect, it } from "vitest"; +import { serializeConversation } from "../src/core/compaction/utils.ts"; + +describe("serializeConversation", () => { + it("should truncate long tool results", () => { + const longContent = "x".repeat(5000); + const messages: Message[] = [ + { + role: "toolResult", + toolCallId: "tc1", + toolName: "read", + content: [{ type: "text", text: longContent }], + isError: false, + timestamp: Date.now(), + }, + ]; + + const result = serializeConversation(messages); + + expect(result).toContain("[Tool result]:"); + expect(result).toContain("[... 3000 more characters truncated]"); + expect(result).not.toContain("x".repeat(3000)); + // First 2000 chars should be present + expect(result).toContain("x".repeat(2000)); + }); + + it("should not truncate short tool results", () => { + const shortContent = "x".repeat(1500); + const messages: Message[] = [ + { + role: "toolResult", + toolCallId: "tc1", + toolName: "read", + content: [{ type: "text", text: shortContent }], + isError: false, + timestamp: Date.now(), + }, + ]; + + const result = serializeConversation(messages); + + expect(result).toBe(`[Tool result]: ${shortContent}`); + expect(result).not.toContain("truncated"); + }); + + it("should not truncate assistant or user messages", () => { + const longText = "y".repeat(5000); + const messages: Message[] = [ + { + role: "user", + content: [{ type: "text", text: longText }], + timestamp: Date.now(), + }, + { + role: "assistant", + content: [{ type: "text", text: longText }], + api: "anthropic", + provider: "anthropic", + model: "test", + usage: { + input: 0, + output: 0, + cacheRead: 0, + cacheWrite: 0, + totalTokens: 0, + cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 }, + }, + stopReason: "stop", + timestamp: Date.now(), + }, + ]; + + const result = serializeConversation(messages); + + expect(result).not.toContain("truncated"); + expect(result).toContain(longText); + }); +}); diff --git a/packages/coding-agent/test/compaction-summary-reasoning.test.ts b/packages/coding-agent/test/compaction-summary-reasoning.test.ts new file mode 100644 index 0000000000000000000000000000000000000000..e57b4ebb97f5b8efa83be69ea5fb9f2254d7f28c --- /dev/null +++ b/packages/coding-agent/test/compaction-summary-reasoning.test.ts @@ -0,0 +1,297 @@ +import type { AgentMessage } from "@earendil-works/pi-agent-core"; +import type { AssistantMessage, Context, Model } from "@earendil-works/pi-ai"; +import { beforeEach, describe, expect, it, vi } from "vitest"; +import { + type CompactionPreparation, + compact, + completeSummarization, + generateSummary, + generateSummaryWithUsage, +} from "../src/core/compaction/index.ts"; + +const { completeSimpleMock } = vi.hoisted(() => ({ + completeSimpleMock: vi.fn(), +})); + +vi.mock("@earendil-works/pi-ai/compat", async (importOriginal) => { + const actual = await importOriginal(); + return { + ...actual, + completeSimple: completeSimpleMock, + }; +}); + +function createModel( + reasoning: boolean, + maxTokens = 8192, + compat?: Model<"anthropic-messages">["compat"], +): Model<"anthropic-messages"> { + return { + id: reasoning ? "reasoning-model" : "non-reasoning-model", + name: reasoning ? "Reasoning Model" : "Non-reasoning Model", + api: "anthropic-messages", + provider: "anthropic", + baseUrl: "https://api.anthropic.com", + reasoning, + input: ["text"], + cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 }, + contextWindow: 200000, + maxTokens, + ...(compat ? { compat } : {}), + }; +} + +const mockSummaryResponse: AssistantMessage = { + role: "assistant", + content: [{ type: "text", text: "## Goal\nTest summary" }], + api: "anthropic-messages", + provider: "anthropic", + model: "claude-sonnet-4-5", + usage: { + input: 10, + output: 10, + cacheRead: 0, + cacheWrite: 0, + totalTokens: 20, + cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 }, + }, + stopReason: "stop", + timestamp: Date.now(), +}; + +const mockToolCallResponse: AssistantMessage = { + ...mockSummaryResponse, + content: [{ type: "toolCall", id: "tool-call-1", name: "read", arguments: { path: "README.md" } }], + stopReason: "toolUse", +}; + +const messages: AgentMessage[] = [{ role: "user", content: "Summarize this.", timestamp: Date.now() }]; + +describe("generateSummary reasoning options", () => { + beforeEach(() => { + completeSimpleMock.mockReset(); + completeSimpleMock.mockResolvedValue(mockSummaryResponse); + }); + + it("uses the provided thinking level for reasoning-capable models", async () => { + const result = await generateSummaryWithUsage( + messages, + createModel(true), + 2000, + "test-key", + undefined, + undefined, + undefined, + undefined, + "medium", + ); + + expect(result.text).toBe("## Goal\nTest summary"); + expect(result.usage).toEqual(mockSummaryResponse.usage); + + expect(completeSimpleMock).toHaveBeenCalledTimes(1); + expect(completeSimpleMock.mock.calls[0][2]).toMatchObject({ + reasoning: "medium", + apiKey: "test-key", + }); + }); + + it("preserves the string result from generateSummary", async () => { + await expect(generateSummary(messages, createModel(false), 2000, "test-key")).resolves.toBe( + "## Goal\nTest summary", + ); + }); + + it("uses fresh routing sessions without prompt caching", async () => { + await generateSummary(messages, createModel(false), 2000, "test-key"); + await generateSummary(messages, createModel(false), 2000, "test-key"); + + const requestOptions = completeSimpleMock.mock.calls.map((call) => call[2]); + expect(requestOptions).toHaveLength(2); + expect(requestOptions.every((options) => options?.cacheRetention === "none")).toBe(true); + + const sessionIds = requestOptions.map((options) => options?.sessionId); + expect(sessionIds[0]).not.toBe(sessionIds[1]); + }); + + it("honors caller-supplied routing session and tool choice without prompt caching", async () => { + await completeSummarization( + createModel(false), + { systemPrompt: "Summarize", messages: [] }, + { sessionId: "current-routing-session", cacheRetention: "long", toolChoice: "auto" }, + ); + + expect(completeSimpleMock.mock.calls[0][2]).toMatchObject({ + sessionId: "current-routing-session", + cacheRetention: "none", + toolChoice: "auto", + }); + }); + + it("preserves the standalone split-turn summary prompt", async () => { + const preparation: CompactionPreparation = { + firstKeptEntryId: "entry-keep", + messagesToSummarize: [], + turnPrefixMessages: messages, + isSplitTurn: true, + tokensBefore: 100, + fileOps: { read: new Set(), written: new Set(), edited: new Set() }, + settings: { enabled: true, reserveTokens: 2000, keepRecentTokens: 20 }, + }; + + await compact(preparation, createModel(false), "test-key"); + + const requestContext = completeSimpleMock.mock.calls[0][1] as Context; + const prompt = JSON.stringify(requestContext.messages); + expect(prompt).toContain("This is the PREFIX of a turn that was too large to keep"); + expect(prompt).toContain(""); + }); + + it("rejects tool calls from conversation summaries", async () => { + completeSimpleMock.mockResolvedValueOnce(mockToolCallResponse); + + await expect(generateSummaryWithUsage(messages, createModel(false), 2000, "test-key")).rejects.toThrow( + "Summarization attempted to call a tool", + ); + }); + + it("rejects tool calls from split-turn summaries", async () => { + completeSimpleMock.mockResolvedValueOnce(mockToolCallResponse); + const preparation: CompactionPreparation = { + firstKeptEntryId: "entry-keep", + messagesToSummarize: [], + turnPrefixMessages: messages, + isSplitTurn: true, + tokensBefore: 100, + fileOps: { read: new Set(), written: new Set(), edited: new Set() }, + settings: { enabled: true, reserveTokens: 2000, keepRecentTokens: 20 }, + }; + + await expect(compact(preparation, createModel(false), "test-key")).rejects.toThrow( + "Turn prefix summarization attempted to call a tool", + ); + }); + + it("rejects a length-limited history summary", async () => { + completeSimpleMock.mockResolvedValueOnce({ + ...mockSummaryResponse, + stopReason: "length", + content: [{ type: "text", text: "partial" }], + }); + + await expect(generateSummaryWithUsage(messages, createModel(false), 2000, "test-key")).rejects.toThrow( + "generation hit the token cap", + ); + }); + + it("rejects a length-limited split-turn summary", async () => { + completeSimpleMock.mockResolvedValueOnce({ + ...mockSummaryResponse, + stopReason: "length", + content: [{ type: "text", text: "partial" }], + }); + const preparation: CompactionPreparation = { + firstKeptEntryId: "entry-keep", + messagesToSummarize: [], + turnPrefixMessages: messages, + isSplitTurn: true, + tokensBefore: 100, + fileOps: { read: new Set(), written: new Set(), edited: new Set() }, + settings: { enabled: true, reserveTokens: 2000, keepRecentTokens: 20 }, + }; + + await expect(compact(preparation, createModel(false), "test-key")).rejects.toThrow( + "generation hit the token cap", + ); + }); + + it("does not set reasoning when thinking is off", async () => { + await generateSummary( + messages, + createModel(true), + 2000, + "test-key", + undefined, + undefined, + undefined, + undefined, + "off", + ); + + expect(completeSimpleMock).toHaveBeenCalledTimes(1); + expect(completeSimpleMock.mock.calls[0][2]).toMatchObject({ + apiKey: "test-key", + }); + expect(completeSimpleMock.mock.calls[0][2]).not.toHaveProperty("reasoning"); + }); + + it("does not set reasoning for non-reasoning models", async () => { + await generateSummary( + messages, + createModel(false), + 2000, + "test-key", + undefined, + undefined, + undefined, + undefined, + "medium", + ); + + expect(completeSimpleMock).toHaveBeenCalledTimes(1); + expect(completeSimpleMock.mock.calls[0][2]).toMatchObject({ + apiKey: "test-key", + }); + expect(completeSimpleMock.mock.calls[0][2]).not.toHaveProperty("reasoning"); + }); + + it("leaves Anthropic refusal fallback handling to pi-ai model metadata", async () => { + await generateSummary( + messages, + createModel(true, 8192, { + allowedFallbackModels: [ + { + provider: "anthropic", + model: "claude-opus-4-8", + cost: { input: 5, output: 25, cacheRead: 0.5, cacheWrite: 6.25 }, + }, + ], + }), + 2000, + "test-key", + ); + + expect(completeSimpleMock).toHaveBeenCalledTimes(1); + expect(completeSimpleMock.mock.calls[0][2]).not.toHaveProperty("refusalFallbacks"); + }); + + it("does not set Anthropic refusal fallback for models without allowed fallback targets", async () => { + await generateSummary(messages, createModel(true), 2000, "test-key"); + + expect(completeSimpleMock).toHaveBeenCalledTimes(1); + expect(completeSimpleMock.mock.calls[0][2]).not.toHaveProperty("refusalFallbacks"); + }); + + it("clamps compaction summary maxTokens to the model output cap", async () => { + const preparation: CompactionPreparation = { + firstKeptEntryId: "entry-keep", + messagesToSummarize: messages, + turnPrefixMessages: messages, + isSplitTurn: true, + tokensBefore: 600000, + fileOps: { read: new Set(), written: new Set(), edited: new Set() }, + settings: { enabled: true, reserveTokens: 500000, keepRecentTokens: 20000 }, + }; + + const result = await compact(preparation, createModel(false, 128000), "test-key"); + + expect(result.usage).toEqual({ + ...mockSummaryResponse.usage, + input: 20, + output: 20, + totalTokens: 40, + cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 }, + }); + expect(completeSimpleMock.mock.calls.map((call) => call[2]?.maxTokens)).toEqual([128000, 128000]); + }); +}); diff --git a/packages/coding-agent/test/compaction.test.ts b/packages/coding-agent/test/compaction.test.ts new file mode 100644 index 0000000000000000000000000000000000000000..12c9d0be220319aa5056c470f069a8a3025d39c0 --- /dev/null +++ b/packages/coding-agent/test/compaction.test.ts @@ -0,0 +1,596 @@ +import type { AgentMessage } from "@earendil-works/pi-agent-core"; +import type { AssistantMessage, Usage } from "@earendil-works/pi-ai/compat"; +import { getModel } from "@earendil-works/pi-ai/compat"; +import { readFileSync } from "fs"; +import { join } from "path"; +import { beforeEach, describe, expect, it } from "vitest"; +import { + type CompactionSettings, + calculateContextTokens, + compact, + DEFAULT_COMPACTION_SETTINGS, + estimateContextTokens, + findCutPoint, + getLastAssistantUsage, + prepareCompaction, + shouldCompact, +} from "../src/core/compaction/index.ts"; +import { + buildSessionContext, + type CompactionEntry, + type CustomMessageEntry, + type ModelChangeEntry, + migrateSessionEntries, + parseSessionEntries, + type SessionEntry, + type SessionMessageEntry, + type ThinkingLevelChangeEntry, +} from "../src/core/session-manager.ts"; + +// ============================================================================ +// Test fixtures +// ============================================================================ + +function loadLargeSessionEntries(): SessionEntry[] { + const sessionPath = join(__dirname, "fixtures/large-session.jsonl"); + const content = readFileSync(sessionPath, "utf-8"); + const entries = parseSessionEntries(content); + migrateSessionEntries(entries); // Add id/parentId for v1 fixtures + return entries.filter((e): e is SessionEntry => e.type !== "session"); +} + +function createMockUsage(input: number, output: number, cacheRead = 0, cacheWrite = 0): Usage { + return { + input, + output, + cacheRead, + cacheWrite, + totalTokens: input + output + cacheRead + cacheWrite, + cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 }, + }; +} + +function createUserMessage(text: string): AgentMessage { + return { role: "user", content: text, timestamp: Date.now() }; +} + +function createAssistantMessage(text: string, usage?: Usage): AssistantMessage { + return { + role: "assistant", + content: [{ type: "text", text }], + usage: usage || createMockUsage(100, 50), + stopReason: "stop", + timestamp: Date.now(), + api: "anthropic-messages", + provider: "anthropic", + model: "claude-sonnet-4-5", + }; +} + +let entryCounter = 0; +let lastId: string | null = null; + +function resetEntryCounter() { + entryCounter = 0; + lastId = null; +} + +// Reset counter before each test to get predictable IDs +beforeEach(() => { + resetEntryCounter(); +}); + +function createMessageEntry(message: AgentMessage): SessionMessageEntry { + const id = `test-id-${entryCounter++}`; + const entry: SessionMessageEntry = { + type: "message", + id, + parentId: lastId, + timestamp: new Date().toISOString(), + message, + }; + lastId = id; + return entry; +} + +function createCompactionEntry(summary: string, firstKeptEntryId: string): CompactionEntry { + const id = `test-id-${entryCounter++}`; + const entry: CompactionEntry = { + type: "compaction", + id, + parentId: lastId, + timestamp: new Date().toISOString(), + summary, + firstKeptEntryId, + tokensBefore: 10000, + }; + lastId = id; + return entry; +} + +function createModelChangeEntry(provider: string, modelId: string): ModelChangeEntry { + const id = `test-id-${entryCounter++}`; + const entry: ModelChangeEntry = { + type: "model_change", + id, + parentId: lastId, + timestamp: new Date().toISOString(), + provider, + modelId, + }; + lastId = id; + return entry; +} + +function createThinkingLevelEntry(thinkingLevel: string): ThinkingLevelChangeEntry { + const id = `test-id-${entryCounter++}`; + const entry: ThinkingLevelChangeEntry = { + type: "thinking_level_change", + id, + parentId: lastId, + timestamp: new Date().toISOString(), + thinkingLevel, + }; + lastId = id; + return entry; +} + +function createCustomMessageEntry(content: string): CustomMessageEntry { + const id = `test-id-${entryCounter++}`; + const entry: CustomMessageEntry = { + type: "custom_message", + id, + parentId: lastId, + timestamp: new Date().toISOString(), + customType: "test", + content, + display: true, + }; + lastId = id; + return entry; +} + +function extractText(messages: AgentMessage[]): string { + return messages + .map((message) => { + switch (message.role) { + case "user": + return typeof message.content === "string" + ? message.content + : message.content + .filter((block): block is { type: "text"; text: string } => block.type === "text") + .map((block) => block.text) + .join(" "); + case "assistant": + return message.content + .filter((block): block is { type: "text"; text: string } => block.type === "text") + .map((block) => block.text) + .join(" "); + case "branchSummary": + case "compactionSummary": + return message.summary; + case "custom": + case "toolResult": + return typeof message.content === "string" + ? message.content + : message.content + .filter((block): block is { type: "text"; text: string } => block.type === "text") + .map((block) => block.text) + .join(" "); + case "bashExecution": + return `${message.command}\n${message.output}`; + default: + return ""; + } + }) + .join("\n"); +} + +// ============================================================================ +// Unit tests +// ============================================================================ + +describe("Token calculation", () => { + it("should calculate total context tokens from usage", () => { + const usage = createMockUsage(1000, 500, 200, 100); + expect(calculateContextTokens(usage)).toBe(1800); + }); + + it("should handle zero values", () => { + const usage = createMockUsage(0, 0, 0, 0); + expect(calculateContextTokens(usage)).toBe(0); + }); +}); + +describe("getLastAssistantUsage", () => { + it("should find the last non-aborted assistant message usage", () => { + const entries: SessionEntry[] = [ + createMessageEntry(createUserMessage("Hello")), + createMessageEntry(createAssistantMessage("Hi", createMockUsage(100, 50))), + createMessageEntry(createUserMessage("How are you?")), + createMessageEntry(createAssistantMessage("Good", createMockUsage(200, 100))), + ]; + + const usage = getLastAssistantUsage(entries); + expect(usage).not.toBeNull(); + expect(usage!.input).toBe(200); + }); + + it("should skip aborted messages", () => { + const abortedMsg: AssistantMessage = { + ...createAssistantMessage("Aborted", createMockUsage(300, 150)), + stopReason: "aborted", + }; + + const entries: SessionEntry[] = [ + createMessageEntry(createUserMessage("Hello")), + createMessageEntry(createAssistantMessage("Hi", createMockUsage(100, 50))), + createMessageEntry(createUserMessage("How are you?")), + createMessageEntry(abortedMsg), + ]; + + const usage = getLastAssistantUsage(entries); + expect(usage).not.toBeNull(); + expect(usage!.input).toBe(100); + }); + + it("should skip all-zero assistant usage", () => { + const entries: SessionEntry[] = [ + createMessageEntry(createUserMessage("Hello")), + createMessageEntry(createAssistantMessage("Hi", createMockUsage(100, 50))), + createMessageEntry(createUserMessage("continue")), + createMessageEntry(createAssistantMessage("Partial", createMockUsage(0, 0))), + ]; + + const usage = getLastAssistantUsage(entries); + expect(usage).not.toBeNull(); + expect(usage!.input).toBe(100); + }); + + it("should return undefined if no assistant messages", () => { + const entries: SessionEntry[] = [createMessageEntry(createUserMessage("Hello"))]; + expect(getLastAssistantUsage(entries)).toBeUndefined(); + }); +}); + +describe("estimateContextTokens", () => { + it("uses the last non-zero assistant usage as the context anchor", () => { + const messages: AgentMessage[] = [ + createUserMessage("Hello"), + createAssistantMessage("Hi", createMockUsage(100, 50)), + createUserMessage("continue"), + createAssistantMessage("Partial thinking", createMockUsage(0, 0)), + ]; + + const estimate = estimateContextTokens(messages); + + expect(estimate.usageTokens).toBe(150); + expect(estimate.lastUsageIndex).toBe(1); + expect(estimate.trailingTokens).toBeGreaterThan(0); + expect(estimate.tokens).toBe(150 + estimate.trailingTokens); + }); +}); + +describe("shouldCompact", () => { + it("should return true when context exceeds threshold", () => { + const settings: CompactionSettings = { + enabled: true, + reserveTokens: 10000, + keepRecentTokens: 20000, + }; + + expect(shouldCompact(95000, 100000, settings)).toBe(true); + expect(shouldCompact(89000, 100000, settings)).toBe(false); + }); + + it("should return false when disabled", () => { + const settings: CompactionSettings = { + enabled: false, + reserveTokens: 10000, + keepRecentTokens: 20000, + }; + + expect(shouldCompact(95000, 100000, settings)).toBe(false); + }); +}); + +describe("findCutPoint", () => { + it("should find cut point based on actual token differences", () => { + // Create entries with cumulative token counts + const entries: SessionEntry[] = []; + for (let i = 0; i < 10; i++) { + entries.push(createMessageEntry(createUserMessage(`User ${i}`))); + entries.push( + createMessageEntry(createAssistantMessage(`Assistant ${i}`, createMockUsage(0, 100, (i + 1) * 1000, 0))), + ); + } + + // 20 entries, last assistant has 10000 tokens + // keepRecentTokens = 2500: keep entries where diff < 2500 + const result = findCutPoint(entries, 0, entries.length, 2500); + + // Should cut at a valid cut point (user or assistant message) + expect(entries[result.firstKeptEntryIndex].type).toBe("message"); + const role = (entries[result.firstKeptEntryIndex] as SessionMessageEntry).message.role; + expect(role === "user" || role === "assistant").toBe(true); + }); + + it("should return startIndex if no valid cut points in range", () => { + const entries: SessionEntry[] = [createMessageEntry(createAssistantMessage("a"))]; + const result = findCutPoint(entries, 0, entries.length, 1000); + expect(result.firstKeptEntryIndex).toBe(0); + }); + + it("should keep everything if all messages fit within budget", () => { + const entries: SessionEntry[] = [ + createMessageEntry(createUserMessage("1")), + createMessageEntry(createAssistantMessage("a", createMockUsage(0, 50, 500, 0))), + createMessageEntry(createUserMessage("2")), + createMessageEntry(createAssistantMessage("b", createMockUsage(0, 50, 1000, 0))), + ]; + + const result = findCutPoint(entries, 0, entries.length, 50000); + expect(result.firstKeptEntryIndex).toBe(0); + }); + + it("should indicate split turn when cutting at assistant message", () => { + // Create a scenario where we cut at an assistant message mid-turn + const entries: SessionEntry[] = [ + createMessageEntry(createUserMessage("Turn 1")), + createMessageEntry(createAssistantMessage("A1", createMockUsage(0, 100, 1000, 0))), + createMessageEntry(createUserMessage("Turn 2")), // index 2 + createMessageEntry(createAssistantMessage("A2-1", createMockUsage(0, 100, 5000, 0))), // index 3 + createMessageEntry(createAssistantMessage("A2-2", createMockUsage(0, 100, 8000, 0))), // index 4 + createMessageEntry(createAssistantMessage("A2-3", createMockUsage(0, 100, 10000, 0))), // index 5 + ]; + + // With keepRecentTokens = 3000, should cut somewhere in Turn 2 + const result = findCutPoint(entries, 0, entries.length, 3000); + + // If cut at assistant message (not user), should indicate split turn + const cutEntry = entries[result.firstKeptEntryIndex] as SessionMessageEntry; + if (cutEntry.message.role === "assistant") { + expect(result.isSplitTurn).toBe(true); + expect(result.turnStartIndex).toBe(2); // Turn 2 starts at index 2 + } + }); + + it("should budget context-visible custom message entries", () => { + const entries: SessionEntry[] = [ + createMessageEntry(createUserMessage("hi")), + createMessageEntry(createAssistantMessage("hello")), + createCustomMessageEntry("x".repeat(4000)), + createMessageEntry(createAssistantMessage("ok")), + ]; + + const tinyBudget = findCutPoint(entries, 0, entries.length, 1); + expect(tinyBudget.firstKeptEntryIndex).toBe(3); + expect(tinyBudget.isSplitTurn).toBe(true); + expect(tinyBudget.turnStartIndex).toBe(2); + + const customFitsBudget = findCutPoint(entries, 0, entries.length, 2); + expect(customFitsBudget.firstKeptEntryIndex).toBe(2); + expect(customFitsBudget.isSplitTurn).toBe(false); + expect(customFitsBudget.turnStartIndex).toBe(-1); + }); +}); + +describe("buildSessionContext", () => { + it("should load all messages when no compaction", () => { + const entries: SessionEntry[] = [ + createMessageEntry(createUserMessage("1")), + createMessageEntry(createAssistantMessage("a")), + createMessageEntry(createUserMessage("2")), + createMessageEntry(createAssistantMessage("b")), + ]; + + const loaded = buildSessionContext(entries); + expect(loaded.messages.length).toBe(4); + expect(loaded.thinkingLevel).toBe("off"); + expect(loaded.model).toEqual({ provider: "anthropic", modelId: "claude-sonnet-4-5" }); + }); + + it("should handle single compaction", () => { + // IDs: u1=test-id-0, a1=test-id-1, u2=test-id-2, a2=test-id-3, compaction=test-id-4, u3=test-id-5, a3=test-id-6 + const u1 = createMessageEntry(createUserMessage("1")); + const a1 = createMessageEntry(createAssistantMessage("a")); + const u2 = createMessageEntry(createUserMessage("2")); + const a2 = createMessageEntry(createAssistantMessage("b")); + const compaction = createCompactionEntry("Summary of 1,a,2,b", u2.id); // keep from u2 onwards + const u3 = createMessageEntry(createUserMessage("3")); + const a3 = createMessageEntry(createAssistantMessage("c")); + + const entries: SessionEntry[] = [u1, a1, u2, a2, compaction, u3, a3]; + + const loaded = buildSessionContext(entries); + // summary + kept (u2, a2) + after (u3, a3) = 5 + expect(loaded.messages.length).toBe(5); + expect(loaded.messages[0].role).toBe("compactionSummary"); + expect((loaded.messages[0] as any).summary).toContain("Summary of 1,a,2,b"); + }); + + it("should handle multiple compactions (only latest matters)", () => { + // First batch + const u1 = createMessageEntry(createUserMessage("1")); + const a1 = createMessageEntry(createAssistantMessage("a")); + const compact1 = createCompactionEntry("First summary", u1.id); + // Second batch + const u2 = createMessageEntry(createUserMessage("2")); + const b = createMessageEntry(createAssistantMessage("b")); + const u3 = createMessageEntry(createUserMessage("3")); + const c = createMessageEntry(createAssistantMessage("c")); + const compact2 = createCompactionEntry("Second summary", u3.id); // keep from u3 onwards + // After second compaction + const u4 = createMessageEntry(createUserMessage("4")); + const d = createMessageEntry(createAssistantMessage("d")); + + const entries: SessionEntry[] = [u1, a1, compact1, u2, b, u3, c, compact2, u4, d]; + + const loaded = buildSessionContext(entries); + // summary + kept from u3 (u3, c) + after (u4, d) = 5 + expect(loaded.messages.length).toBe(5); + expect((loaded.messages[0] as any).summary).toContain("Second summary"); + }); + + it("should keep all messages when firstKeptEntryId is first entry", () => { + const u1 = createMessageEntry(createUserMessage("1")); + const a1 = createMessageEntry(createAssistantMessage("a")); + const compact1 = createCompactionEntry("First summary", u1.id); // keep from first entry + const u2 = createMessageEntry(createUserMessage("2")); + const b = createMessageEntry(createAssistantMessage("b")); + + const entries: SessionEntry[] = [u1, a1, compact1, u2, b]; + + const loaded = buildSessionContext(entries); + // summary + all messages (u1, a1, u2, b) = 5 + expect(loaded.messages.length).toBe(5); + }); + + it("should track model and thinking level changes", () => { + const entries: SessionEntry[] = [ + createMessageEntry(createUserMessage("1")), + createModelChangeEntry("openai", "gpt-4"), + createMessageEntry(createAssistantMessage("a")), + createThinkingLevelEntry("high"), + ]; + + const loaded = buildSessionContext(entries); + // model_change is later overwritten by assistant message's model info + expect(loaded.model).toEqual({ provider: "anthropic", modelId: "claude-sonnet-4-5" }); + expect(loaded.thinkingLevel).toBe("high"); + }); +}); + +describe("prepareCompaction with previous compaction", () => { + it("should skip repeated compactions when kept messages still fit", () => { + const u1 = createMessageEntry(createUserMessage("user msg 1 (summarized by compaction1)")); + const a1 = createMessageEntry(createAssistantMessage("assistant msg 1")); + const u2 = createMessageEntry(createUserMessage("user msg 2 - kept by compaction1")); + const a2 = createMessageEntry(createAssistantMessage("assistant msg 2")); + const u3 = createMessageEntry(createUserMessage("user msg 3 - kept by compaction1")); + const a3 = createMessageEntry(createAssistantMessage("assistant msg 3", createMockUsage(5000, 1000))); + const compaction1 = createCompactionEntry("First summary", u2.id); + const u4 = createMessageEntry(createUserMessage("user msg 4 (new after compaction1)")); + const a4 = createMessageEntry(createAssistantMessage("assistant msg 4", createMockUsage(8000, 2000))); + + const pathEntries = [u1, a1, u2, a2, u3, a3, compaction1, u4, a4]; + const preparation = prepareCompaction(pathEntries, DEFAULT_COMPACTION_SETTINGS); + + expect(preparation).toBeUndefined(); + }); + + it("should re-summarize previously kept messages when the recent window moves past them", () => { + const u1 = createMessageEntry(createUserMessage("user msg 1 (summarized by compaction1)".repeat(4))); + const a1 = createMessageEntry(createAssistantMessage("assistant msg 1".repeat(4))); + const u2 = createMessageEntry(createUserMessage("user msg 2 - kept by compaction1 ".repeat(12))); + const a2 = createMessageEntry(createAssistantMessage("assistant msg 2 ".repeat(12))); + const u3 = createMessageEntry(createUserMessage("user msg 3 - kept by compaction1 ".repeat(12))); + const a3 = createMessageEntry(createAssistantMessage("assistant msg 3 ".repeat(12), createMockUsage(5000, 1000))); + const compaction1 = createCompactionEntry("First summary", u2.id); + const u4 = createMessageEntry(createUserMessage("user msg 4 (new after compaction1) ".repeat(12))); + const a4 = createMessageEntry(createAssistantMessage("assistant msg 4 ".repeat(12), createMockUsage(8000, 2000))); + + const settings: CompactionSettings = { + ...DEFAULT_COMPACTION_SETTINGS, + keepRecentTokens: 100, + }; + const preparation = prepareCompaction([u1, a1, u2, a2, u3, a3, compaction1, u4, a4], settings); + + expect(preparation).toBeDefined(); + const summarizedText = extractText(preparation!.messagesToSummarize); + expect(summarizedText).toContain("user msg 2 - kept by compaction1"); + expect(summarizedText).toContain("user msg 3 - kept by compaction1"); + expect(summarizedText).not.toContain("First summary"); + expect(preparation!.previousSummary).toBe("First summary"); + }); +}); + +// ============================================================================ +// Integration tests with real session data +// ============================================================================ + +describe("Large session fixture", () => { + it("should parse the large session", () => { + const entries = loadLargeSessionEntries(); + expect(entries.length).toBeGreaterThan(100); + + const messageCount = entries.filter((e) => e.type === "message").length; + expect(messageCount).toBeGreaterThan(100); + }); + + it("should find cut point in large session", () => { + const entries = loadLargeSessionEntries(); + const result = findCutPoint(entries, 0, entries.length, DEFAULT_COMPACTION_SETTINGS.keepRecentTokens); + + // Cut point should be at a message entry (user or assistant) + expect(entries[result.firstKeptEntryIndex].type).toBe("message"); + const role = (entries[result.firstKeptEntryIndex] as SessionMessageEntry).message.role; + expect(role === "user" || role === "assistant").toBe(true); + }); + + it("should load session correctly", () => { + const entries = loadLargeSessionEntries(); + const loaded = buildSessionContext(entries); + + expect(loaded.messages.length).toBeGreaterThan(100); + expect(loaded.model).not.toBeNull(); + }); +}); + +// ============================================================================ +// LLM integration tests (skipped without API key) +// ============================================================================ + +describe.skipIf(!process.env.ANTHROPIC_OAUTH_TOKEN)("LLM summarization", () => { + it("should generate a compaction result for the large session", async () => { + const entries = loadLargeSessionEntries(); + const model = getModel("anthropic", "claude-sonnet-4-5")!; + + const preparation = prepareCompaction(entries, DEFAULT_COMPACTION_SETTINGS); + expect(preparation).toBeDefined(); + + const compactionResult = await compact(preparation!, model, process.env.ANTHROPIC_OAUTH_TOKEN!); + + expect(compactionResult.summary.length).toBeGreaterThan(100); + expect(compactionResult.firstKeptEntryId).toBeTruthy(); + expect(compactionResult.tokensBefore).toBeGreaterThan(0); + + console.log("Summary length:", compactionResult.summary.length); + console.log("First kept entry ID:", compactionResult.firstKeptEntryId); + console.log("Tokens before:", compactionResult.tokensBefore); + console.log("\n--- SUMMARY ---\n"); + console.log(compactionResult.summary); + }, 60000); + + it("should produce valid session after compaction", async () => { + const entries = loadLargeSessionEntries(); + const loaded = buildSessionContext(entries); + const model = getModel("anthropic", "claude-sonnet-4-5")!; + + const preparation = prepareCompaction(entries, DEFAULT_COMPACTION_SETTINGS); + expect(preparation).toBeDefined(); + + const compactionResult = await compact(preparation!, model, process.env.ANTHROPIC_OAUTH_TOKEN!); + + // Simulate appending compaction to entries by creating a proper entry + const lastEntry = entries[entries.length - 1]; + const parentId = lastEntry.id; + const compactionEntry: CompactionEntry = { + type: "compaction", + id: "compaction-test-id", + parentId, + timestamp: new Date().toISOString(), + ...compactionResult, + }; + const newEntries = [...entries, compactionEntry]; + const reloaded = buildSessionContext(newEntries); + + // Should have summary + kept messages + expect(reloaded.messages.length).toBeLessThan(loaded.messages.length); + expect(reloaded.messages[0].role).toBe("compactionSummary"); + expect((reloaded.messages[0] as any).summary).toContain(compactionResult.summary); + + console.log("Original messages:", loaded.messages.length); + console.log("After compaction:", reloaded.messages.length); + }, 60000); +}); diff --git a/packages/coding-agent/test/config-value-migration.test.ts b/packages/coding-agent/test/config-value-migration.test.ts new file mode 100644 index 0000000000000000000000000000000000000000..8bad16cf534ac23fde5cd9b9eea378d3032ed0b7 --- /dev/null +++ b/packages/coding-agent/test/config-value-migration.test.ts @@ -0,0 +1,178 @@ +import * as fs from "node:fs"; +import * as os from "node:os"; +import * as path from "node:path"; +import { afterEach, describe, expect, it, vi } from "vitest"; +import { ENV_AGENT_DIR } from "../src/config.ts"; +import { AuthStorage } from "../src/core/auth-storage.ts"; +import { runMigrations } from "../src/migrations.ts"; + +import { createModelRegistry } from "./model-runtime-test-utils.ts"; + +describe("config value env var syntax migration", () => { + const tempDirs: string[] = []; + + afterEach(() => { + for (const dir of tempDirs.splice(0)) { + fs.rmSync(dir, { recursive: true, force: true }); + } + vi.restoreAllMocks(); + }); + + function createAgentDir(): string { + const agentDir = fs.mkdtempSync(path.join(os.tmpdir(), "pi-config-value-migration-test-")); + tempDirs.push(agentDir); + return agentDir; + } + + function withAgentDir(agentDir: string, fn: () => void): void { + const previousAgentDir = process.env[ENV_AGENT_DIR]; + process.env[ENV_AGENT_DIR] = agentDir; + try { + fn(); + } finally { + if (previousAgentDir === undefined) { + delete process.env[ENV_AGENT_DIR]; + } else { + process.env[ENV_AGENT_DIR] = previousAgentDir; + } + } + } + + it("leaves uppercase auth.json API key values unchanged", () => { + const agentDir = createAgentDir(); + fs.writeFileSync( + path.join(agentDir, "auth.json"), + `${JSON.stringify( + { + anthropic: { type: "api_key", key: "ANTHROPIC_API_KEY" }, + openai: { type: "api_key", key: "$OPENAI_API_KEY" }, + opencode: { type: "api_key", key: "public" }, + github: { type: "oauth", access: "ACCESS_TOKEN", refresh: "REFRESH_TOKEN", expires: 1 }, + }, + null, + 2, + )}\n`, + "utf-8", + ); + const logSpy = vi.spyOn(console, "log").mockImplementation(() => {}); + + withAgentDir(agentDir, () => runMigrations(agentDir)); + + const migrated = JSON.parse(fs.readFileSync(path.join(agentDir, "auth.json"), "utf-8")) as Record< + string, + Record + >; + expect(migrated.anthropic.key).toBe("ANTHROPIC_API_KEY"); + expect(migrated.openai.key).toBe("$OPENAI_API_KEY"); + expect(migrated.opencode.key).toBe("public"); + expect(migrated.github.access).toBe("ACCESS_TOKEN"); + expect(logSpy).not.toHaveBeenCalled(); + }); + + it.each([ + ["malformed", '{\n "providers": {\n'], + ["blank", ""], + ])("does not throw on %s models.json during migrations", async (_name, content) => { + const agentDir = createAgentDir(); + const modelsPath = path.join(agentDir, "models.json"); + fs.writeFileSync(modelsPath, content, "utf-8"); + + withAgentDir(agentDir, () => expect(() => runMigrations(agentDir)).not.toThrow()); + + expect(fs.readFileSync(modelsPath, "utf-8")).toBe(content); + const registry = await createModelRegistry(AuthStorage.create(path.join(agentDir, "auth.json")), modelsPath); + const loadError = registry.getError(); + expect(loadError).toContain("Failed to parse models.json"); + expect(loadError).toContain(`File: ${modelsPath}`); + }); + + it("leaves uppercase models.json API key and header values unchanged", async () => { + const agentDir = createAgentDir(); + const envKeys = ["CUSTOM_API_KEY", "HEADER_API_KEY", "MODEL_API_KEY", "OVERRIDE_API_KEY"]; + const savedEnv: Record = {}; + for (const key of envKeys) { + savedEnv[key] = process.env[key]; + process.env[key] = `env-${key}`; + } + + try { + fs.writeFileSync( + path.join(agentDir, "models.json"), + `${JSON.stringify( + { + providers: { + "custom-provider": { + baseUrl: "https://example.com/v1", + apiKey: "CUSTOM_API_KEY", + api: "openai-completions", + headers: { + "x-api-key": "HEADER_API_KEY", + "x-literal": "literal", + }, + models: [ + { + id: "model-a", + headers: { "x-model-key": "MODEL_API_KEY" }, + }, + ], + modelOverrides: { + "model-b": { headers: { "x-override-key": "OVERRIDE_API_KEY" } }, + }, + }, + }, + }, + null, + 2, + )}\n`, + "utf-8", + ); + const logSpy = vi.spyOn(console, "log").mockImplementation(() => {}); + + withAgentDir(agentDir, () => runMigrations(agentDir)); + + const migrated = JSON.parse(fs.readFileSync(path.join(agentDir, "models.json"), "utf-8")) as { + providers: Record< + string, + { + apiKey?: string; + headers?: Record; + models?: Array<{ headers?: Record }>; + modelOverrides?: Record }>; + } + >; + }; + const provider = migrated.providers["custom-provider"]!; + expect(provider.apiKey).toBe("CUSTOM_API_KEY"); + expect(provider.headers?.["x-api-key"]).toBe("HEADER_API_KEY"); + expect(provider.headers?.["x-literal"]).toBe("literal"); + expect(provider.models?.[0]?.headers?.["x-model-key"]).toBe("MODEL_API_KEY"); + expect(provider.modelOverrides?.["model-b"]?.headers?.["x-override-key"]).toBe("OVERRIDE_API_KEY"); + expect(logSpy).not.toHaveBeenCalled(); + + const registry = await createModelRegistry( + AuthStorage.create(path.join(agentDir, "auth.json")), + path.join(agentDir, "models.json"), + ); + const model = registry.find("custom-provider", "model-a"); + expect(model).toBeDefined(); + expect(await registry.getApiKeyForProvider("custom-provider")).toBe("CUSTOM_API_KEY"); + expect(await registry.getApiKeyAndHeaders(model!)).toMatchObject({ + ok: true, + apiKey: "CUSTOM_API_KEY", + headers: { + "x-api-key": "HEADER_API_KEY", + "x-literal": "literal", + "x-model-key": "MODEL_API_KEY", + }, + }); + } finally { + for (const key of envKeys) { + if (savedEnv[key] === undefined) { + delete process.env[key]; + } else { + process.env[key] = savedEnv[key]; + } + } + } + }); +}); diff --git a/packages/coding-agent/test/config.test.ts b/packages/coding-agent/test/config.test.ts new file mode 100644 index 0000000000000000000000000000000000000000..300f9e06ab12d804d0f9ba4c979e17a823a5b8e9 --- /dev/null +++ b/packages/coding-agent/test/config.test.ts @@ -0,0 +1,451 @@ +import { chmodSync, mkdirSync, mkdtempSync, rmSync, writeFileSync } from "fs"; +import { tmpdir } from "os"; +import { delimiter, join } from "path"; +import { afterEach, describe, expect, test } from "vitest"; +import { + detectInstallMethod, + findNodePackageDir, + getSelfUpdateCommand, + getSelfUpdateUnavailableInstruction, + getUpdateInstruction, +} from "../src/config.ts"; + +const execPathDescriptor = Object.getOwnPropertyDescriptor(process, "execPath"); +const originalPath = process.env.PATH; +const originalPiPackageDir = process.env.PI_PACKAGE_DIR; +const originalArgv1 = process.argv[1]; +let tempDir: string | undefined; + +function setExecPath(value: string): void { + Object.defineProperty(process, "execPath", { + value, + configurable: true, + }); +} + +afterEach(() => { + if (execPathDescriptor) { + Object.defineProperty(process, "execPath", execPathDescriptor); + } + if (originalPath === undefined) { + delete process.env.PATH; + } else { + process.env.PATH = originalPath; + } + if (originalPiPackageDir === undefined) { + delete process.env.PI_PACKAGE_DIR; + } else { + process.env.PI_PACKAGE_DIR = originalPiPackageDir; + } + if (originalArgv1 === undefined) { + process.argv.splice(1, 1); + } else { + process.argv[1] = originalArgv1; + } + if (tempDir) { + chmodSync(tempDir, 0o700); + rmSync(tempDir, { recursive: true, force: true }); + tempDir = undefined; + } +}); + +function createNpmPrefixInstall(template = "pi-prefix-"): { prefix: string; packageDir: string } { + const prefix = mkdtempSync(join(tmpdir(), template)); + const root = join(prefix, "lib", "node_modules"); + const scopeDir = join(root, "@earendil-works"); + const packageDir = join(scopeDir, "pi-coding-agent"); + mkdirSync(packageDir, { recursive: true }); + tempDir = prefix; + process.env.PI_PACKAGE_DIR = packageDir; + setExecPath(join(packageDir, "dist", "cli.js")); + return { prefix, packageDir }; +} + +function createPnpmGlobalInstall(): { root: string; packageDir: string } { + const temp = mkdtempSync(join(tmpdir(), "pi-pnpm-")); + const binDir = join(temp, "bin"); + const root = join(temp, "pnpm", "global", "5", "node_modules"); + const packageDir = join(root, "@mariozechner", "pi-coding-agent"); + mkdirSync(packageDir, { recursive: true }); + mkdirSync(binDir, { recursive: true }); + writeFileSync(join(binDir, process.platform === "win32" ? "pnpm.cmd" : "pnpm"), createFakePnpmScript(root)); + chmodSync(join(binDir, process.platform === "win32" ? "pnpm.cmd" : "pnpm"), 0o755); + tempDir = temp; + process.env.PATH = `${binDir}${delimiter}${originalPath ?? ""}`; + process.env.PI_PACKAGE_DIR = packageDir; + setExecPath( + join( + root, + ".pnpm", + "@mariozechner+pi-coding-agent@0.0.0", + "node_modules", + "@mariozechner", + "pi-coding-agent", + "dist", + "cli.js", + ), + ); + return { root, packageDir }; +} + +function createYarnGlobalInstall(): { globalDir: string; packageDir: string } { + const temp = mkdtempSync(join(tmpdir(), "pi-yarn-")); + const binDir = join(temp, "bin"); + const globalDir = join(temp, "yarn", "global"); + const packageDir = join(globalDir, "node_modules", "@mariozechner", "pi-coding-agent"); + mkdirSync(packageDir, { recursive: true }); + mkdirSync(binDir, { recursive: true }); + writeFileSync(join(binDir, process.platform === "win32" ? "yarn.cmd" : "yarn"), createFakeYarnScript(globalDir)); + chmodSync(join(binDir, process.platform === "win32" ? "yarn.cmd" : "yarn"), 0o755); + tempDir = temp; + process.env.PATH = `${binDir}${delimiter}${originalPath ?? ""}`; + process.env.PI_PACKAGE_DIR = packageDir; + setExecPath(join(globalDir, ".yarn", "@mariozechner", "pi-coding-agent", "dist", "cli.js")); + return { globalDir, packageDir }; +} + +function createBunGlobalInstall(): { packageDir: string } { + const temp = mkdtempSync(join(tmpdir(), "pi-bun-")); + const prefix = join(temp, ".bun"); + const bunBin = join(prefix, "bin"); + const root = join(prefix, "install", "global", "node_modules"); + const scopeDir = join(root, "@earendil-works"); + const packageDir = join(scopeDir, "pi-coding-agent"); + mkdirSync(packageDir, { recursive: true }); + mkdirSync(bunBin, { recursive: true }); + writeFileSync(join(bunBin, process.platform === "win32" ? "bun.cmd" : "bun"), createFakeBunScript(bunBin)); + chmodSync(join(bunBin, process.platform === "win32" ? "bun.cmd" : "bun"), 0o755); + tempDir = temp; + process.env.PATH = `${bunBin}${delimiter}${originalPath ?? ""}`; + process.env.PI_PACKAGE_DIR = packageDir; + setExecPath(join(packageDir, "dist", "cli.js")); + return { packageDir }; +} + +function createFakePnpmScript(root: string): string { + if (process.platform === "win32") { + return `@echo off\r\nif "%1"=="root" if "%2"=="-g" echo ${root}\r\n`; + } + const escapedRoot = root.replaceAll("'", "'\\''"); + return `#!/bin/sh\nif [ "$1" = "root" ] && [ "$2" = "-g" ]; then\n\tprintf '%s\\n' '${escapedRoot}'\n\texit 0\nfi\nexit 1\n`; +} + +function createFakeYarnScript(globalDir: string): string { + if (process.platform === "win32") { + return `@echo off\r\nif "%1"=="global" if "%2"=="dir" echo ${globalDir}\r\n`; + } + const escapedGlobalDir = globalDir.replaceAll("'", "'\\''"); + return `#!/bin/sh\nif [ "$1" = "global" ] && [ "$2" = "dir" ]; then\n\tprintf '%s\\n' '${escapedGlobalDir}'\n\texit 0\nfi\nexit 1\n`; +} + +function createFakeBunScript(bunBin: string): string { + if (process.platform === "win32") { + return `@echo off\r\nif "%1"=="pm" if "%2"=="bin" if "%3"=="-g" echo ${bunBin}\r\n`; + } + const escapedBunBin = bunBin.replaceAll("'", "'\\''"); + return `#!/bin/sh\nif [ "$1" = "pm" ] && [ "$2" = "bin" ] && [ "$3" = "-g" ]; then\n\tprintf '%s\\n' '${escapedBunBin}'\n\texit 0\nfi\nexit 1\n`; +} + +describe("findNodePackageDir", () => { + test("skips binary metadata copied into dist", () => { + tempDir = mkdtempSync(join(tmpdir(), "pi-package-dir-")); + const distDir = join(tempDir, "dist"); + const bundleDir = join(distDir, "bundle"); + mkdirSync(bundleDir, { recursive: true }); + writeFileSync(join(tempDir, "package.json"), "{}"); + writeFileSync(join(distDir, "package.json"), "{}"); + + expect(findNodePackageDir(bundleDir)).toBe(tempDir); + }); +}); + +describe("detectInstallMethod", () => { + test("detects pnpm from Windows .pnpm install paths", () => { + setExecPath( + "C:\\Users\\Admin\\Documents\\pnpm-repository\\global\\5\\.pnpm\\@earendil-works+pi-coding-agent@0.67.68\\node_modules\\@earendil-works\\pi-coding-agent\\dist\\cli.js", + ); + + expect(detectInstallMethod()).toBe("pnpm"); + expect(getUpdateInstruction("@earendil-works/pi-coding-agent")).toBe( + "Run: pnpm install -g --ignore-scripts --config.minimumReleaseAge=0 @earendil-works/pi-coding-agent", + ); + }); + + test("does not self-update unknown wrapper installs", () => { + setExecPath("/usr/local/bin/node"); + + expect(detectInstallMethod()).toBe("unknown"); + expect(getSelfUpdateCommand("@earendil-works/pi-coding-agent")).toBeUndefined(); + expect(getUpdateInstruction("@earendil-works/pi-coding-agent")).toBe( + "Update @earendil-works/pi-coding-agent using the package manager, wrapper, or source checkout that provides this installation.", + ); + }); + + test("self-updates npm installs from custom prefixes", () => { + const { prefix } = createNpmPrefixInstall(); + + const command = getSelfUpdateCommand("@earendil-works/pi-coding-agent"); + + expect(detectInstallMethod()).toBe("npm"); + expect(command).toEqual({ + command: "npm", + args: [ + "--prefix", + prefix, + "install", + "-g", + "--ignore-scripts", + "--min-release-age=0", + "@earendil-works/pi-coding-agent", + ], + display: `npm --prefix ${prefix} install -g --ignore-scripts --min-release-age=0 @earendil-works/pi-coding-agent`, + }); + }); + + test("self-updates exact npm versions without uninstalling the current package", () => { + const { prefix } = createNpmPrefixInstall(); + + const command = getSelfUpdateCommand("@earendil-works/pi-coding-agent", undefined, { + packageName: "@earendil-works/pi-coding-agent", + installSpec: "@earendil-works/pi-coding-agent@1.2.3", + }); + + expect(command).toEqual({ + command: "npm", + args: [ + "--prefix", + prefix, + "install", + "-g", + "--ignore-scripts", + "--min-release-age=0", + "@earendil-works/pi-coding-agent@1.2.3", + ], + display: `npm --prefix ${prefix} install -g --ignore-scripts --min-release-age=0 @earendil-works/pi-coding-agent@1.2.3`, + }); + }); + + test("self-updates renamed packages from the current install prefix", () => { + const { prefix } = createNpmPrefixInstall(); + + const command = getSelfUpdateCommand("@mariozechner/pi-coding-agent", undefined, "@new-scope/pi"); + + expect(command).toEqual({ + command: "npm", + args: ["--prefix", prefix, "install", "-g", "--ignore-scripts", "--min-release-age=0", "@new-scope/pi"], + display: `npm --prefix ${prefix} uninstall -g @mariozechner/pi-coding-agent && npm --prefix ${prefix} install -g --ignore-scripts --min-release-age=0 @new-scope/pi`, + steps: [ + { + command: "npm", + args: ["--prefix", prefix, "uninstall", "-g", "@mariozechner/pi-coding-agent"], + display: `npm --prefix ${prefix} uninstall -g @mariozechner/pi-coding-agent`, + }, + { + command: "npm", + args: ["--prefix", prefix, "install", "-g", "--ignore-scripts", "--min-release-age=0", "@new-scope/pi"], + display: `npm --prefix ${prefix} install -g --ignore-scripts --min-release-age=0 @new-scope/pi`, + }, + ], + }); + }); + + test("self-update respects configured npmCommand", () => { + const { prefix } = createNpmPrefixInstall(); + + const command = getSelfUpdateCommand("@earendil-works/pi-coding-agent", ["npm", "--prefix", prefix]); + + expect(command).toEqual({ + command: "npm", + args: [ + "--prefix", + prefix, + "install", + "-g", + "--ignore-scripts", + "--min-release-age=0", + "@earendil-works/pi-coding-agent", + ], + display: `npm --prefix ${prefix} install -g --ignore-scripts --min-release-age=0 @earendil-works/pi-coding-agent`, + }); + }); + + test("self-update treats empty npmCommand as unset", () => { + const { prefix } = createNpmPrefixInstall(); + + const command = getSelfUpdateCommand("@earendil-works/pi-coding-agent", []); + + expect(command?.args).toEqual([ + "--prefix", + prefix, + "install", + "-g", + "--ignore-scripts", + "--min-release-age=0", + "@earendil-works/pi-coding-agent", + ]); + }); + + test("quotes npm self-update display paths", () => { + const { prefix } = createNpmPrefixInstall("pi prefix "); + + const command = getSelfUpdateCommand("@earendil-works/pi-coding-agent"); + + expect(command?.display).toBe( + `npm --prefix "${prefix}" install -g --ignore-scripts --min-release-age=0 @earendil-works/pi-coding-agent`, + ); + }); + + test("does not infer Windows npm custom prefixes from package paths", () => { + const packageDir = "C:\\Users\\Admin\\npm prefix\\node_modules\\@earendil-works\\pi-coding-agent"; + process.env.PI_PACKAGE_DIR = packageDir; + setExecPath(`${packageDir}\\dist\\cli.js`); + + expect(detectInstallMethod()).toBe("npm"); + expect(getUpdateInstruction("@earendil-works/pi-coding-agent")).toBe( + "Run: npm install -g --ignore-scripts --min-release-age=0 @earendil-works/pi-coding-agent", + ); + }); + + test("self-updates bun global installs from bun pm bin", () => { + createBunGlobalInstall(); + + const command = getSelfUpdateCommand("@earendil-works/pi-coding-agent"); + + expect(detectInstallMethod()).toBe("bun"); + expect(command).toEqual({ + command: "bun", + args: ["install", "-g", "--ignore-scripts", "--minimum-release-age=0", "@earendil-works/pi-coding-agent"], + display: "bun install -g --ignore-scripts --minimum-release-age=0 @earendil-works/pi-coding-agent", + }); + }); + + test("self-updates renamed pnpm global installs by removing the old package first", () => { + createPnpmGlobalInstall(); + + const command = getSelfUpdateCommand("@mariozechner/pi-coding-agent", undefined, "@new-scope/pi"); + + expect(detectInstallMethod()).toBe("pnpm"); + expect(command).toEqual({ + command: "pnpm", + args: ["install", "-g", "--ignore-scripts", "--config.minimumReleaseAge=0", "@new-scope/pi"], + display: + "pnpm remove -g @mariozechner/pi-coding-agent && pnpm install -g --ignore-scripts --config.minimumReleaseAge=0 @new-scope/pi", + steps: [ + { + command: "pnpm", + args: ["remove", "-g", "@mariozechner/pi-coding-agent"], + display: "pnpm remove -g @mariozechner/pi-coding-agent", + }, + { + command: "pnpm", + args: ["install", "-g", "--ignore-scripts", "--config.minimumReleaseAge=0", "@new-scope/pi"], + display: "pnpm install -g --ignore-scripts --config.minimumReleaseAge=0 @new-scope/pi", + }, + ], + }); + }); + + test("self-updates pnpm v11 global installs resolved through the store", () => { + const temp = mkdtempSync(join(tmpdir(), "pi-pnpm11-")); + const binDir = join(temp, "bin"); + const root = join(temp, "Library", "pnpm", "global", "v11"); + const packageName = "@earendil-works/pi-coding-agent"; + const globalPackageDir = join(root, "11e9a", "node_modules", "@earendil-works", "pi-coding-agent"); + const storePackageDir = join( + temp, + "Library", + "pnpm", + "store", + "v11", + "links", + "@earendil-works", + "pi-coding-agent", + "0.75.0", + "hash", + "node_modules", + "@earendil-works", + "pi-coding-agent", + ); + mkdirSync(globalPackageDir, { recursive: true }); + mkdirSync(storePackageDir, { recursive: true }); + mkdirSync(binDir, { recursive: true }); + writeFileSync(join(globalPackageDir, "package.json"), "{}"); + writeFileSync(join(binDir, process.platform === "win32" ? "pnpm.cmd" : "pnpm"), createFakePnpmScript(root)); + chmodSync(join(binDir, process.platform === "win32" ? "pnpm.cmd" : "pnpm"), 0o755); + tempDir = temp; + process.env.PATH = `${binDir}${delimiter}${originalPath ?? ""}`; + process.env.PI_PACKAGE_DIR = storePackageDir; + process.argv[1] = join(globalPackageDir, "dist", "cli.js"); + setExecPath(join(storePackageDir, "dist", "cli.js")); + + const command = getSelfUpdateCommand(packageName); + + expect(detectInstallMethod()).toBe("pnpm"); + expect(command).toEqual({ + command: "pnpm", + args: ["install", "-g", "--ignore-scripts", "--config.minimumReleaseAge=0", packageName], + display: `pnpm install -g --ignore-scripts --config.minimumReleaseAge=0 ${packageName}`, + }); + }); + + test("self-updates renamed yarn global installs by removing the old package first", () => { + createYarnGlobalInstall(); + + const command = getSelfUpdateCommand("@mariozechner/pi-coding-agent", undefined, "@new-scope/pi"); + + expect(detectInstallMethod()).toBe("yarn"); + expect(command).toEqual({ + command: "yarn", + args: ["global", "add", "--ignore-scripts", "@new-scope/pi"], + display: "yarn global remove @mariozechner/pi-coding-agent && yarn global add --ignore-scripts @new-scope/pi", + steps: [ + { + command: "yarn", + args: ["global", "remove", "@mariozechner/pi-coding-agent"], + display: "yarn global remove @mariozechner/pi-coding-agent", + }, + { + command: "yarn", + args: ["global", "add", "--ignore-scripts", "@new-scope/pi"], + display: "yarn global add --ignore-scripts @new-scope/pi", + }, + ], + }); + }); + + test("self-updates renamed bun global installs by removing the old package first", () => { + createBunGlobalInstall(); + + const command = getSelfUpdateCommand("@mariozechner/pi-coding-agent", undefined, "@new-scope/pi"); + + expect(detectInstallMethod()).toBe("bun"); + expect(command).toEqual({ + command: "bun", + args: ["install", "-g", "--ignore-scripts", "--minimum-release-age=0", "@new-scope/pi"], + display: + "bun uninstall -g @mariozechner/pi-coding-agent && bun install -g --ignore-scripts --minimum-release-age=0 @new-scope/pi", + steps: [ + { + command: "bun", + args: ["uninstall", "-g", "@mariozechner/pi-coding-agent"], + display: "bun uninstall -g @mariozechner/pi-coding-agent", + }, + { + command: "bun", + args: ["install", "-g", "--ignore-scripts", "--minimum-release-age=0", "@new-scope/pi"], + display: "bun install -g --ignore-scripts --minimum-release-age=0 @new-scope/pi", + }, + ], + }); + }); + + test("does not self-update when npm install path is not writable", () => { + const { packageDir } = createNpmPrefixInstall(); + chmodSync(packageDir, 0o500); + + expect(getSelfUpdateCommand("@earendil-works/pi-coding-agent")).toBeUndefined(); + expect(getSelfUpdateUnavailableInstruction("@earendil-works/pi-coding-agent")).toContain( + "the install path is not writable", + ); + }); +}); diff --git a/packages/coding-agent/test/credential-print.test.ts b/packages/coding-agent/test/credential-print.test.ts new file mode 100644 index 0000000000000000000000000000000000000000..a1ca56d9c7e0d6641fe404fbe652a6ab552bc9de --- /dev/null +++ b/packages/coding-agent/test/credential-print.test.ts @@ -0,0 +1,130 @@ +import { InMemoryModelsStore } from "@earendil-works/pi-ai"; +import { describe, expect, test, vi } from "vitest"; +import { parseArgs } from "../src/cli/args.ts"; +import { AuthCommandError, isAuthCommandHelp, parseAuthCommand } from "../src/cli/auth-command.ts"; +import { resolveCredentialForPrint } from "../src/cli/credential-print.ts"; +import { AuthStorage } from "../src/core/auth-storage.ts"; +import { ModelRuntime } from "../src/core/model-runtime.ts"; +import { main } from "../src/main.ts"; + +async function createRuntime(credentials: AuthStorage): Promise { + return ModelRuntime.create({ + credentials, + modelsPath: null, + modelsStore: new InMemoryModelsStore(), + allowModelNetwork: false, + }); +} + +describe("credential print commands", () => { + test("prints a resolved API key", async () => { + const runtime = await createRuntime(AuthStorage.inMemory({ openai: { type: "api_key", key: "test-api-key" } })); + const args = parseArgs(["--provider", "openai"]); + + await expect(resolveCredentialForPrint(args, runtime, "api_key")).resolves.toBe("test-api-key"); + }); + + test("prints bearer tokens resolved from an Authorization header", async () => { + const runtime = await createRuntime( + AuthStorage.inMemory({ + "kimi-coding": { + type: "oauth", + access: "header-test-token", + refresh: "test-refresh-token", + expires: Date.now() + 60 * 60 * 1000, + }, + }), + ); + const args = parseArgs(["--provider", "kimi-coding"]); + + await expect(resolveCredentialForPrint(args, runtime, "bearer_token")).resolves.toBe("header-test-token"); + }); + + test("refreshes an expired OAuth token before printing it", async () => { + const storage = AuthStorage.inMemory({ + "openai-codex": { + type: "oauth", + access: "old-test-token", + refresh: "test-refresh-token", + expires: 0, + }, + }); + const runtime = await createRuntime(storage); + const refresh = vi.fn(async () => ({ + type: "oauth" as const, + access: "fresh-test-token", + refresh: "test-refresh-token", + expires: Date.now() + 60 * 60 * 1000, + })); + const oauth = runtime.getProvider("openai-codex")?.auth.oauth; + if (!oauth) throw new Error("OpenAI Codex OAuth provider is not registered"); + oauth.refresh = refresh; + const args = parseArgs(["--provider", "openai-codex"]); + + await expect(resolveCredentialForPrint(args, runtime, "bearer_token")).resolves.toBe("fresh-test-token"); + expect(refresh).toHaveBeenCalledOnce(); + expect(await storage.read("openai-codex")).toMatchObject({ access: "fresh-test-token" }); + }); + + test("reports unknown auth options like package commands", async () => { + const originalExitCode = process.exitCode; + const errorSpy = vi.spyOn(console, "error").mockImplementation(() => {}); + try { + process.exitCode = undefined; + await main(["auth", "check", "--provider", "openai-codex", "--credentails"]); + const stderr = errorSpy.mock.calls.map(([message]) => String(message)).join("\n"); + expect(stderr).toContain('Unknown option --credentails for "auth check".'); + expect(stderr).toContain( + 'Use "pi --help" or "pi auth check --provider [--json] [--credentials] [--no-refresh]".', + ); + expect(process.exitCode).toBe(1); + } finally { + process.exitCode = originalExitCode; + errorSpy.mockRestore(); + } + }); + + test("parses credential commands and rejects invalid arguments or credential types", async () => { + const runtime = await createRuntime( + AuthStorage.inMemory({ + "openai-codex": { + type: "oauth", + access: "test-token-not-to-be-printed", + refresh: "test-refresh-token", + expires: Date.now() + 60 * 60 * 1000, + }, + }), + ); + + expect(parseAuthCommand(["auth", "print-api-key", "--provider", "openai"])).toEqual({ + kind: "api_key", + args: ["--provider", "openai"], + json: false, + credentials: false, + noRefresh: false, + }); + expect(parseAuthCommand(["auth", "print-bearer-token"])).toMatchObject({ kind: "bearer_token" }); + expect(parseAuthCommand(["auth", "print-bearer-token", "--min-expiry", "30m"])).toEqual({ + kind: "bearer_token", + args: [], + json: false, + credentials: false, + noRefresh: false, + minExpiryMs: 30 * 60_000, + }); + expect(() => parseAuthCommand(["auth", "print-api-key", "--min-expiry", "30m"])).toThrow( + "only supported by print-bearer-token", + ); + expect(isAuthCommandHelp(["auth", "--help"])).toBe(true); + expect(isAuthCommandHelp(["auth", "print-api-key", "--help"])).toBe(true); + expect(isAuthCommandHelp(["auth", "print-bearer-token", "-h"])).toBe(true); + expect(isAuthCommandHelp(["auth", "check", "--help"])).toBe(true); + expect(() => parseAuthCommand(["auth", "unknown"])).toThrow(AuthCommandError); + await expect(resolveCredentialForPrint(parseArgs([]), runtime, "api_key")).rejects.toThrow( + "requires --provider or --model ", + ); + await expect( + resolveCredentialForPrint(parseArgs(["--provider", "openai-codex"]), runtime, "api_key"), + ).rejects.toThrow("configured with OAuth"); + }); +}); diff --git a/packages/coding-agent/test/custom-editor-history-keybindings.test.ts b/packages/coding-agent/test/custom-editor-history-keybindings.test.ts new file mode 100644 index 0000000000000000000000000000000000000000..a4de5200dfcdcd87fd54aaece29b4a89906f9b94 --- /dev/null +++ b/packages/coding-agent/test/custom-editor-history-keybindings.test.ts @@ -0,0 +1,34 @@ +import { setKeybindings, TuiMainScreen } from "@earendil-works/pi-tui"; +import { afterEach, describe, expect, it } from "vitest"; +import { defaultEditorTheme } from "../../tui/test/test-themes.ts"; +import { VirtualTerminal } from "../../tui/test/virtual-terminal.ts"; +import { KeybindingsManager } from "../src/core/keybindings.ts"; +import { CustomEditor } from "../src/modes/interactive/components/custom-editor.ts"; + +afterEach(() => { + setKeybindings(new KeybindingsManager()); +}); + +describe("CustomEditor prompt history keybindings", () => { + it("gives an explicit history binding precedence over model cycling", () => { + const keybindings = new KeybindingsManager({ + "tui.editor.historyPrevious": "ctrl+p", + "tui.editor.historyNext": "ctrl+n", + }); + setKeybindings(keybindings); + const editor = new CustomEditor(new TuiMainScreen(new VirtualTerminal()), defaultEditorTheme, keybindings); + let modelCycles = 0; + editor.onAction("app.model.cycleForward", () => { + modelCycles++; + }); + editor.addToHistory("previous prompt"); + editor.setText("draft"); + + editor.handleInput("\x10"); // Ctrl+P + expect(editor.getText()).toBe("previous prompt"); + expect(modelCycles).toBe(0); + + editor.handleInput("\x0e"); // Ctrl+N + expect(editor.getText()).toBe("draft"); + }); +}); diff --git a/packages/coding-agent/test/custom-message.test.ts b/packages/coding-agent/test/custom-message.test.ts new file mode 100644 index 0000000000000000000000000000000000000000..cdc598010b02f8d112d198e4bc4e0818a742ea49 --- /dev/null +++ b/packages/coding-agent/test/custom-message.test.ts @@ -0,0 +1,44 @@ +import { Text } from "@earendil-works/pi-tui"; +import { describe, expect, test } from "vitest"; +import type { MessageRenderer, MessageRenderOptions } from "../src/core/extensions/types.ts"; +import type { CustomMessage } from "../src/core/messages.ts"; +import { CustomMessageComponent } from "../src/modes/interactive/components/custom-message.ts"; +import { initTheme } from "../src/modes/interactive/theme/theme.ts"; +import { stripAnsi } from "../src/utils/ansi.ts"; + +describe("CustomMessageComponent", () => { + test("provides output padding to custom renderers and updates it", () => { + initTheme("dark"); + const optionsSeen: MessageRenderOptions[] = []; + const renderer: MessageRenderer = (_message, options) => { + optionsSeen.push(options); + return new Text("custom", options.outputPad, 0); + }; + const message: CustomMessage = { + role: "custom", + customType: "test", + content: "custom", + display: true, + timestamp: Date.now(), + }; + const component = new CustomMessageComponent(message, renderer, undefined, 1); + + expect(optionsSeen).toEqual([{ expanded: false, outputPad: 1 }]); + expect( + component + .render(40) + .map(stripAnsi) + .some((line) => line.startsWith(" custom")), + ).toBe(true); + + component.setOutputPad(0); + + expect(optionsSeen.at(-1)).toEqual({ expanded: false, outputPad: 0 }); + expect( + component + .render(40) + .map(stripAnsi) + .some((line) => line.startsWith("custom")), + ).toBe(true); + }); +}); diff --git a/packages/coding-agent/test/default-tools-setting.test.ts b/packages/coding-agent/test/default-tools-setting.test.ts new file mode 100644 index 0000000000000000000000000000000000000000..5b9331400a05e4ba521cf8ea91252ab14f37375e --- /dev/null +++ b/packages/coding-agent/test/default-tools-setting.test.ts @@ -0,0 +1,159 @@ +import { existsSync, mkdirSync, rmSync } from "node:fs"; +import { tmpdir } from "node:os"; +import { join } from "node:path"; +import { getModel } from "@earendil-works/pi-ai/compat"; +import { Type } from "typebox"; +import { afterEach, beforeEach, describe, expect, it } from "vitest"; +import { createAgentSessionFromServices, createAgentSessionServices } from "../src/core/agent-session-services.ts"; +import { DefaultResourceLoader } from "../src/core/resource-loader.ts"; +import { type CreateAgentSessionOptions, createAgentSession, type InlineExtension } from "../src/core/sdk.ts"; +import { SessionManager } from "../src/core/session-manager.ts"; +import { SettingsManager } from "../src/core/settings-manager.ts"; + +type ToolOptions = Pick; + +describe("defaultTools setting", () => { + let tempDir: string; + let agentDir: string; + + beforeEach(() => { + tempDir = join(tmpdir(), `pi-default-tools-${Date.now()}-${Math.random().toString(36).slice(2)}`); + agentDir = join(tempDir, "agent"); + mkdirSync(agentDir, { recursive: true }); + }); + + afterEach(() => { + if (tempDir && existsSync(tempDir)) { + rmSync(tempDir, { recursive: true, force: true }); + } + }); + + async function createSession( + defaultTools: string[], + options: ToolOptions = {}, + extensionFactories: InlineExtension[] = [], + ) { + const settingsManager = SettingsManager.inMemory({ defaultTools }); + const resourceLoader = new DefaultResourceLoader({ + cwd: tempDir, + agentDir, + settingsManager, + extensionFactories, + }); + await resourceLoader.reload(); + + return ( + await createAgentSession({ + cwd: tempDir, + agentDir, + model: getModel("anthropic", "claude-sonnet-4-5")!, + settingsManager, + sessionManager: SessionManager.inMemory(tempDir), + resourceLoader, + ...options, + }) + ).session; + } + + it("uses the configured list as the initial built-in selection", async () => { + const session = await createSession(["grep", "find"]); + + expect( + session + .getAllTools() + .map((tool) => tool.name) + .sort(), + ).toEqual(["bash", "edit", "find", "grep", "ls", "powershell", "read", "write"]); + expect(session.getActiveToolNames()).toEqual(["grep", "find"]); + expect(session.systemPrompt).toContain("- grep:"); + expect(session.systemPrompt).not.toContain("- read:"); + session.dispose(); + }); + + it("can select powershell instead of bash", async () => { + const session = await createSession(["read", "powershell", "edit", "write"]); + + expect(session.getActiveToolNames()).toEqual(["read", "powershell", "edit", "write"]); + expect(session.systemPrompt).toContain("- powershell: Execute PowerShell commands"); + expect(session.systemPrompt).not.toContain("- bash:"); + session.dispose(); + }); + + it("keeps extension and SDK custom tools enabled", async () => { + const session = await createSession( + ["grep"], + { + customTools: [ + { + name: "sdk_tool", + label: "SDK Tool", + description: "SDK custom tool", + parameters: Type.Object({}), + execute: async () => ({ content: [{ type: "text", text: "ok" }], details: {} }), + }, + ], + }, + [ + (pi) => { + pi.registerTool({ + name: "static_tool", + label: "Static Tool", + description: "Statically registered extension tool", + parameters: Type.Object({}), + execute: async () => ({ content: [{ type: "text", text: "ok" }], details: {} }), + }); + pi.on("session_start", () => { + pi.registerTool({ + name: "dynamic_tool", + label: "Dynamic Tool", + description: "Dynamically registered extension tool", + parameters: Type.Object({}), + execute: async () => ({ content: [{ type: "text", text: "ok" }], details: {} }), + }); + }); + }, + ], + ); + await session.bindExtensions({}); + + expect(session.getActiveToolNames().sort()).toEqual(["dynamic_tool", "grep", "sdk_tool", "static_tool"]); + expect(session.getAllTools().map((tool) => tool.name)).toEqual( + expect.arrayContaining(["read", "dynamic_tool", "sdk_tool", "static_tool"]), + ); + session.dispose(); + }); + + it("preserves explicit tool option precedence", async () => { + const allowlistedSession = await createSession(["grep"], { tools: ["read"] }); + expect(allowlistedSession.getActiveToolNames()).toEqual(["read"]); + allowlistedSession.dispose(); + + const excludedSession = await createSession(["read", "grep"], { excludeTools: ["read"] }); + expect(excludedSession.getActiveToolNames()).toEqual(["grep"]); + excludedSession.dispose(); + + const toolLessSession = await createSession(["read"], { noTools: "all" }); + expect(toolLessSession.getAllTools()).toEqual([]); + expect(toolLessSession.getActiveToolNames()).toEqual([]); + toolLessSession.dispose(); + }); + + it("applies through service-based session creation", async () => { + const settingsManager = SettingsManager.inMemory({ defaultTools: ["ls"] }); + const services = await createAgentSessionServices({ cwd: tempDir, agentDir, settingsManager }); + const { session } = await createAgentSessionFromServices({ + services, + sessionManager: SessionManager.inMemory(tempDir), + model: getModel("anthropic", "claude-sonnet-4-5")!, + }); + + expect( + session + .getAllTools() + .map((tool) => tool.name) + .sort(), + ).toEqual(["bash", "edit", "find", "grep", "ls", "powershell", "read", "write"]); + expect(session.getActiveToolNames()).toEqual(["ls"]); + session.dispose(); + }); +}); diff --git a/packages/coding-agent/test/documentation.test.ts b/packages/coding-agent/test/documentation.test.ts new file mode 100644 index 0000000000000000000000000000000000000000..b1ba3ea7701cae764c9e8d6898c1f309fcbbfee2 --- /dev/null +++ b/packages/coding-agent/test/documentation.test.ts @@ -0,0 +1,187 @@ +import { existsSync, globSync, readFileSync, statSync } from "node:fs"; +import { dirname, isAbsolute, posix, relative, resolve, sep } from "node:path"; +import { lexer, type Token, type Tokens, walkTokens } from "marked"; +import { describe, expect, it } from "vitest"; + +type NavigationItem = + | { title: string; path: string; items?: NavigationItem[] } + | { title: string; items: NavigationItem[] }; + +interface NavigationGroup { + title: string; + items: NavigationItem[]; +} + +const docsRoot = resolve(import.meta.dirname, "../docs"); + +function requireObject(value: unknown, location: string): Record { + if (!value || typeof value !== "object" || Array.isArray(value)) { + throw new Error(`${location} must be an object`); + } + return value as Record; +} + +function requireNonemptyString(value: unknown, location: string): string { + if (typeof value !== "string" || value.trim() === "") { + throw new Error(`${location} must be a nonempty string`); + } + return value; +} + +function requireFields(value: Record, fields: string[], location: string): void { + const unsupportedFields = Object.keys(value) + .filter((field) => !fields.includes(field)) + .sort(); + if (unsupportedFields.length > 0) { + throw new Error(`${location} has unsupported fields: ${unsupportedFields.join(", ")}`); + } +} + +function requireDocumentationPath(value: unknown, location: string): string { + const path = requireNonemptyString(value, location); + const normalizedPath = posix.normalize(path); + const escapesDocsRoot = normalizedPath === ".." || normalizedPath.startsWith("../"); + const hasUrlSyntax = path.includes("?") || path.includes("#"); + const hasWindowsSyntax = path.includes("\\") || /^[a-z]:/i.test(path); + + if ( + path !== path.trim() || + path !== normalizedPath || + posix.isAbsolute(path) || + escapesDocsRoot || + hasUrlSyntax || + hasWindowsSyntax || + !path.endsWith(".md") + ) { + throw new Error(`${location} must be a relative normalized .md path`); + } + return path; +} + +function readNavigationItem(rawItem: unknown, location: string): NavigationItem { + const item = requireObject(rawItem, location); + requireFields(item, ["title", "path", "items"], location); + const title = requireNonemptyString(item.title, `${location}.title`); + const path = item.path === undefined ? undefined : requireDocumentationPath(item.path, `${location}.path`); + + let items: NavigationItem[] | undefined; + if (item.items !== undefined) { + if (!Array.isArray(item.items)) throw new Error(`${location}.items must be an array`); + if (item.items.length === 0) throw new Error(`${location}.items must contain at least one item`); + items = item.items.map((child, index) => readNavigationItem(child, `${location}.items[${index}]`)); + } + + if (path !== undefined) return items ? { title, path, items } : { title, path }; + if (items !== undefined) return { title, items }; + throw new Error(`${location} must have a path or items`); +} + +function readNavigation(): NavigationGroup[] { + const catalog = requireObject( + JSON.parse(readFileSync(resolve(docsRoot, "docs.json"), "utf8")), + "Documentation catalog", + ); + requireFields(catalog, ["navigation", "redirects"], "Documentation catalog"); + if (!Array.isArray(catalog.navigation)) throw new Error("Documentation catalog navigation must be an array"); + + return catalog.navigation.map((rawGroup, groupIndex) => { + const location = `Documentation catalog navigation[${groupIndex}]`; + const group = requireObject(rawGroup, location); + requireFields(group, ["title", "items"], location); + const title = requireNonemptyString(group.title, `${location}.title`); + if (!Array.isArray(group.items)) throw new Error(`${location}.items must be an array`); + if (group.items.length === 0) throw new Error(`${location}.items must contain at least one item`); + return { + title, + items: group.items.map((item, itemIndex) => readNavigationItem(item, `${location}.items[${itemIndex}]`)), + }; + }); +} + +function navigationPaths(groups: NavigationGroup[]): string[] { + const paths: string[] = []; + const visit = (items: NavigationItem[]): void => { + for (const item of items) { + if ("path" in item) paths.push(item.path); + if (item.items) visit(item.items); + } + }; + for (const group of groups) visit(group.items); + return paths; +} + +function markdownLinks(markdown: string): string[] { + const links: string[] = []; + walkTokens(lexer(markdown), (token: Token) => { + if (token.type === "link") links.push((token as Tokens.Link).href); + }); + return links; +} + +function isFile(path: string): boolean { + return existsSync(path) && statSync(path).isFile(); +} + +function publicSlug(path: string): string { + const withoutExtension = path.slice(0, -3); + if (withoutExtension === "index") return ""; + return withoutExtension.endsWith("/index") ? withoutExtension.slice(0, -6) : withoutExtension; +} + +describe("coding-agent documentation", () => { + it("has valid navigation and no orphaned Markdown pages", () => { + const allPages = globSync("**/*.md", { cwd: docsRoot }) + .map((path) => path.replaceAll("\\", "/")) + .sort(); + const roots = navigationPaths(readNavigation()); + const duplicateRoots = [...new Set(roots.filter((path, index) => roots.indexOf(path) !== index))].sort(); + const missingRoots = roots.filter((path) => !isFile(resolve(docsRoot, path))).sort(); + + const pathsBySlug = new Map(); + for (const path of allPages) { + const slug = publicSlug(path); + pathsBySlug.set(slug, [...(pathsBySlug.get(slug) ?? []), path]); + } + const slugCollisions = [...pathsBySlug] + .filter(([, paths]) => paths.length > 1) + .map(([slug, paths]) => `${slug || "/"} (${paths.join(", ")})`) + .sort(); + + const reachable = new Set(); + const pending = roots.filter((path) => isFile(resolve(docsRoot, path))); + const brokenLinks: string[] = []; + while (pending.length > 0) { + const source = pending.shift(); + if (source === undefined || reachable.has(source)) continue; + reachable.add(source); + + for (const href of markdownLinks(readFileSync(resolve(docsRoot, source), "utf8"))) { + if (/^[a-z][a-z\d+.-]*:/i.test(href) || href.startsWith("//") || href.startsWith("/")) continue; + const linkedPath = href.split(/[?#]/, 1)[0]; + if (!linkedPath.endsWith(".md")) continue; + + const target = resolve(docsRoot, dirname(source), linkedPath); + if (!isFile(target)) { + brokenLinks.push(`${source} -> ${href}`); + continue; + } + + const relativePath = relative(docsRoot, target); + if (relativePath === ".." || relativePath.startsWith(`..${sep}`) || isAbsolute(relativePath)) continue; + const normalizedPath = relativePath.replaceAll("\\", "/"); + if (!reachable.has(normalizedPath)) pending.push(normalizedPath); + } + } + + const orphanedPages = allPages.filter((path) => !reachable.has(path)); + const issues = [ + ...(duplicateRoots.length > 0 ? [`Duplicate navigation paths: ${duplicateRoots.join(", ")}`] : []), + ...(missingRoots.length > 0 ? [`Missing navigation pages: ${missingRoots.join(", ")}`] : []), + ...(brokenLinks.length > 0 ? [`Broken local Markdown links: ${brokenLinks.sort().join(", ")}`] : []), + ...(slugCollisions.length > 0 ? [`Duplicate public documentation slugs: ${slugCollisions.join("; ")}`] : []), + ...(orphanedPages.length > 0 ? [`Orphaned Markdown pages: ${orphanedPages.join(", ")}`] : []), + ]; + + expect(issues).toEqual([]); + }); +}); diff --git a/packages/coding-agent/test/edit-tool-legacy-input.test.ts b/packages/coding-agent/test/edit-tool-legacy-input.test.ts new file mode 100644 index 0000000000000000000000000000000000000000..26a550d26af89641b851bfbcf35a3a5c2aac25ba --- /dev/null +++ b/packages/coding-agent/test/edit-tool-legacy-input.test.ts @@ -0,0 +1,116 @@ +import { mkdtemp, readFile, rm, writeFile } from "node:fs/promises"; +import { tmpdir } from "node:os"; +import { join } from "node:path"; +import { afterEach, describe, expect, it } from "vitest"; +import type { ExtensionContext } from "../src/core/extensions/types.ts"; +import { createEditToolDefinition } from "../src/core/tools/edit.ts"; + +const tempDirs: string[] = []; + +async function createTempDir(): Promise { + const dir = await mkdtemp(join(tmpdir(), "pi-edit-legacy-input-")); + tempDirs.push(dir); + return dir; +} + +afterEach(async () => { + await Promise.all(tempDirs.splice(0, tempDirs.length).map((dir) => rm(dir, { recursive: true, force: true }))); +}); + +describe("edit tool prepareArguments", () => { + it("keeps legacy fields out of the public schema", () => { + const definition = createEditToolDefinition(process.cwd()); + expect(definition.parameters.properties).not.toHaveProperty("oldText"); + expect(definition.parameters.properties).not.toHaveProperty("newText"); + }); + + it("folds top-level oldText/newText into edits", () => { + const definition = createEditToolDefinition(process.cwd()); + const prepared = definition.prepareArguments!({ + path: "file.txt", + oldText: "before", + newText: "after", + }); + expect(prepared).toEqual({ + path: "file.txt", + edits: [{ oldText: "before", newText: "after" }], + }); + }); + + it("appends legacy replacement to existing edits", () => { + const definition = createEditToolDefinition(process.cwd()); + const prepared = definition.prepareArguments!({ + path: "file.txt", + edits: [{ oldText: "a", newText: "b" }], + oldText: "c", + newText: "d", + }); + expect(prepared).toEqual({ + path: "file.txt", + edits: [ + { oldText: "a", newText: "b" }, + { oldText: "c", newText: "d" }, + ], + }); + }); + + it("passes through valid input unchanged", () => { + const definition = createEditToolDefinition(process.cwd()); + const input = { + path: "file.txt", + edits: [{ oldText: "a", newText: "b" }], + }; + const prepared = definition.prepareArguments!(input); + expect(prepared).toBe(input); + }); + + it("passes through non-object input unchanged", () => { + const definition = createEditToolDefinition(process.cwd()); + expect(definition.prepareArguments!(null)).toBe(null); + expect(definition.prepareArguments!(undefined)).toBe(undefined); + expect(definition.prepareArguments!("garbage")).toBe("garbage"); + }); + + it("prepared args execute correctly", async () => { + const dir = await createTempDir(); + const filePath = join(dir, "legacy.txt"); + await writeFile(filePath, "before\n", "utf8"); + + const definition = createEditToolDefinition(dir); + const prepared = definition.prepareArguments!({ + path: "legacy.txt", + oldText: "before", + newText: "after", + }); + + const result = await definition.execute("tool-1", prepared, undefined, undefined, {} as ExtensionContext); + expect(result.content).toEqual([{ type: "text", text: "Successfully replaced 1 block(s) in legacy.txt." }]); + expect(await readFile(filePath, "utf8")).toBe("after\n"); + }); +}); + +describe("edit tool stringified edits", () => { + it("parses edits from a JSON string", () => { + const definition = createEditToolDefinition(process.cwd()); + const prepared = definition.prepareArguments!({ + path: "file.txt", + edits: JSON.stringify([{ oldText: "a", newText: "b" }]), + }); + expect(prepared).toEqual({ + path: "file.txt", + edits: [{ oldText: "a", newText: "b" }], + }); + }); + + it("leaves edits alone when the string is not valid JSON", () => { + const definition = createEditToolDefinition(process.cwd()); + const prepared = definition.prepareArguments!({ + path: "file.txt", + edits: "not json", + }); + expect(prepared).toEqual({ + path: "file.txt", + edits: "not json", + }); + }); +}); diff --git a/packages/coding-agent/test/edit-tool-no-full-redraw.test.ts b/packages/coding-agent/test/edit-tool-no-full-redraw.test.ts new file mode 100644 index 0000000000000000000000000000000000000000..8e6d66f5603e97078192cc470ba23ad3b4bda79d --- /dev/null +++ b/packages/coding-agent/test/edit-tool-no-full-redraw.test.ts @@ -0,0 +1,235 @@ +import { mkdtemp, readFile, rm, writeFile } from "node:fs/promises"; +import { tmpdir } from "node:os"; +import { join } from "node:path"; +import { Container, type Terminal, Text, type TUI, TuiMainScreen } from "@earendil-works/pi-tui"; +import { afterEach, beforeAll, describe, expect, it } from "vitest"; +import { createEditToolDefinition } from "../src/core/tools/edit.ts"; +import { computeEditsDiff, type Edit } from "../src/core/tools/edit-diff.ts"; +import { ToolExecutionComponent } from "../src/modes/interactive/components/tool-execution.ts"; +import { initTheme } from "../src/modes/interactive/theme/theme.ts"; + +class FakeTerminal implements Terminal { + columns = 80; + rows = 24; + kittyProtocolActive = true; + writes: string[] = []; + + start(): void {} + stop(): void {} + async drainInput(): Promise {} + write(data: string): void { + this.writes.push(data); + } + moveBy(_lines: number): void {} + hideCursor(): void {} + showCursor(): void {} + clearLine(): void {} + clearFromCursor(): void {} + clearScreen(): void {} + setTitle(_title: string): void {} + setProgress(_active: boolean): void {} + + get fullClearCount(): number { + return this.writes.filter((write) => write.includes("\x1b[2J\x1b[H\x1b[3J")).length; + } +} + +async function waitForRender(): Promise { + await new Promise((resolve) => setTimeout(resolve, 0)); +} + +async function waitForRenderedText( + getRender: () => string, + expectedText: string, + onRetry?: () => void, + timeoutMs = 2000, +): Promise { + const deadline = Date.now() + timeoutMs; + let lastRender = ""; + while (Date.now() < deadline) { + onRetry?.(); + await waitForRender(); + lastRender = getRender(); + if (lastRender.includes(expectedText)) { + return lastRender; + } + } + throw new Error(`Timed out waiting for render to include "${expectedText}". Last render:\n${lastRender}`); +} + +function createLargeEdits(lines: string[]): Edit[] { + const targets = [50, 150, 250, 350, 450, 550, 650, 750, 850, 950]; + return targets.map((lineNumber) => ({ + oldText: `${lines[lineNumber - 1]}\n${lines[lineNumber]}\n${lines[lineNumber + 1]}`, + newText: `${lines[lineNumber - 1]}\n${lines[lineNumber]} changed\n${lines[lineNumber + 1]}`, + })); +} + +describe("edit tool TUI rendering", () => { + const tempDirs: string[] = []; + + beforeAll(() => { + initTheme("dark"); + }); + + afterEach(async () => { + await Promise.all(tempDirs.splice(0).map((dir) => rm(dir, { recursive: true, force: true }))); + }); + + it("renders the large diff in the call preview and does not full-redraw when the result settles", async () => { + const dir = await mkdtemp(join(tmpdir(), "pi-edit-redraw-")); + tempDirs.push(dir); + const filePath = join(dir, "large-edit.txt"); + await writeFile( + filePath, + `${Array.from({ length: 1000 }, (_, i) => `line ${i}`).join("\n")} +`, + "utf8", + ); + const lines = (await readFile(filePath, "utf8")).trimEnd().split("\n"); + const edits = createLargeEdits(lines); + const diff = await computeEditsDiff(filePath, edits, process.cwd()); + if ("error" in diff) { + throw new Error(diff.error); + } + + const terminal = new FakeTerminal(); + const tui: TUI = new TuiMainScreen(terminal); + const root = new Container(); + for (let i = 0; i < 200; i++) { + root.addChild(new Text(`history ${i}`, 0, 0)); + } + + const component = new ToolExecutionComponent( + "edit", + "tool-call-1", + { path: filePath, edits }, + {}, + createEditToolDefinition(process.cwd()), + tui, + process.cwd(), + ); + root.addChild(component); + tui.addChild(root); + tui.start(); + await waitForRender(); + + component.setArgsComplete(); + tui.requestRender(); + await waitForRender(); + await waitForRender(); + + const callOnlyRender = await waitForRenderedText( + () => component.render(80).join("\n"), + "line 50 changed", + () => tui.requestRender(true), + ); + expect(callOnlyRender).toContain("edit"); + expect(callOnlyRender).toContain("line 950 changed"); + + const redrawsBeforeResult = tui.fullRedraws; + const clearsBeforeResult = terminal.fullClearCount; + component.updateResult( + { + content: [{ type: "text", text: `Successfully replaced ${edits.length} block(s) in ${filePath}.` }], + details: diff, + isError: false, + }, + false, + ); + tui.requestRender(); + await waitForRender(); + + expect(tui.fullRedraws).toBe(redrawsBeforeResult); + expect(terminal.fullClearCount).toBe(clearsBeforeResult); + + const settledRender = component.render(80).join("\n"); + expect(settledRender).toContain("line 50 changed"); + expect(settledRender).toContain("line 950 changed"); + expect(settledRender).not.toContain("Successfully replaced"); + }); + + it("reconstructs the boxed preview from a settled result without argsComplete", async () => { + const dir = await mkdtemp(join(tmpdir(), "pi-edit-replay-")); + tempDirs.push(dir); + const filePath = join(dir, "replay-edit.txt"); + await writeFile( + filePath, + `${Array.from({ length: 200 }, (_, i) => `line ${i}`).join("\n")} +`, + "utf8", + ); + const lines = (await readFile(filePath, "utf8")).trimEnd().split("\n"); + const edits = createLargeEdits(lines).slice(0, 2); + const diff = await computeEditsDiff(filePath, edits, process.cwd()); + if ("error" in diff) { + throw new Error(diff.error); + } + await rm(filePath, { force: true }); + + const terminal = new FakeTerminal(); + const tui: TUI = new TuiMainScreen(terminal); + const component = new ToolExecutionComponent( + "edit", + "tool-call-replay", + { path: filePath, edits }, + {}, + createEditToolDefinition(process.cwd()), + tui, + process.cwd(), + ); + tui.addChild(component); + tui.start(); + await waitForRender(); + + component.updateResult( + { + content: [{ type: "text", text: `Successfully replaced ${edits.length} block(s) in ${filePath}.` }], + details: diff, + isError: false, + }, + false, + ); + await waitForRender(); + await waitForRender(); + + const rendered = component.render(80).join("\n"); + expect(rendered).toContain("line 50 changed"); + expect(rendered).toContain("line 150 changed"); + }); + + it("shows a preflight error without rendering a diff when the edits do not apply", async () => { + const dir = await mkdtemp(join(tmpdir(), "pi-edit-preflight-")); + tempDirs.push(dir); + const filePath = join(dir, "missing-edit.txt"); + await writeFile(filePath, "line 0\nline 1\n", "utf8"); + + const terminal = new FakeTerminal(); + const tui: TUI = new TuiMainScreen(terminal); + const component = new ToolExecutionComponent( + "edit", + "tool-call-2", + { path: filePath, edits: [{ oldText: "does not exist", newText: "replacement" }] }, + {}, + createEditToolDefinition(process.cwd()), + tui, + process.cwd(), + ); + tui.addChild(component); + tui.start(); + await waitForRender(); + + component.setArgsComplete(); + tui.requestRender(); + await waitForRender(); + await waitForRender(); + + const rendered = await waitForRenderedText( + () => component.render(80).join("\n"), + "Could not find", + () => tui.requestRender(true), + ); + expect(rendered).not.toContain("+1 "); + expect(rendered).not.toContain("-1 "); + }); +}); diff --git a/packages/coding-agent/test/experimental-agent-controller.test.ts b/packages/coding-agent/test/experimental-agent-controller.test.ts new file mode 100644 index 0000000000000000000000000000000000000000..ba13bb49e19a5aab43597f583d95341290d41316 --- /dev/null +++ b/packages/coding-agent/test/experimental-agent-controller.test.ts @@ -0,0 +1,178 @@ +import { createFacetHost, defineFacet } from "@earendil-works/chord"; +import { + type AgentLane, + BACKGROUND_CONTEXT, + Closed, + type RunResult as HarnessRunResult, + InvalidMessage, + LaneBusy, + UnknownSkill, + UnknownTemplate, +} from "@earendil-works/pi-agent-core"; +import { describe, expect, test, vi } from "vitest"; +import { AgentController } from "../src/experimental/services/agent-controller.ts"; +import { createAgentController } from "../src/experimental/services/agent-controller-provider.ts"; + +const admissionErrors = [ + [ + new LaneBusy({ + lane: "main", + operationId: "operation-1", + operationKind: "run", + message: "busy", + }), + "lane_busy", + "operation-1", + ], + [new InvalidMessage({ lane: "main", reason: "invalid", message: "invalid" }), "invalid_message", null], + [new UnknownSkill({ name: "skill", message: "unknown" }), "unknown_skill", null], + [new UnknownTemplate({ name: "template", message: "unknown" }), "unknown_template", null], + [new Closed({ message: "closed" }), "closed", null], +] as const satisfies readonly [Extract["error"], string, string | null][]; + +const completed = { + operationId: "operation-1", + kind: "run" as const, + status: "completed" as const, + fromTipId: null, + tipId: "entry-1", + startedAt: 1, + endedAt: 2, +}; + +describe("AgentController service", () => { + test("provides the presentation-safe AgentLane command surface", async () => { + const prompt = vi.fn(async () => ({ ok: true as const, value: completed })); + const requestAbort = vi.fn(async () => ({ + ok: true as const, + value: { operationId: "operation-1", newlyRequested: true, steer: [], followUp: [] }, + })); + const steer = vi.fn(async () => ({ ok: true as const, value: { entryId: "queue-1" } })); + const followUp = vi.fn(async () => ({ ok: true as const, value: { entryId: "queue-2" } })); + const lane = { prompt, requestAbort, steer, followUp } as unknown as AgentLane; + const controllerFacet = defineFacet({ + id: "test-agent-controller", + setup(env) { + env.provide(AgentController, createAgentController(lane)); + }, + }); + const host = await createFacetHost({ facets: [controllerFacet] }); + try { + expect(host.services.catalogue).toEqual([{ serviceId: AgentController.id, mode: "singleton" }]); + await expect( + host.services.invoke( + { serviceId: AgentController.id, member: "prompt", args: [{ message: "hello", images: null }] }, + BACKGROUND_CONTEXT, + ), + ).resolves.toEqual({ accepted: true, operationId: "operation-1", error: null }); + await expect( + host.services.invoke( + { serviceId: AgentController.id, member: "requestAbort", args: ["operation-1"] }, + BACKGROUND_CONTEXT, + ), + ).resolves.toBeUndefined(); + await expect( + host.services.invoke( + { serviceId: AgentController.id, member: "steer", args: [{ message: "later", images: null }] }, + BACKGROUND_CONTEXT, + ), + ).resolves.toEqual({ accepted: true, entryId: "queue-1", error: null }); + await expect( + host.services.invoke( + { serviceId: AgentController.id, member: "followUp", args: [{ message: "after", images: null }] }, + BACKGROUND_CONTEXT, + ), + ).resolves.toEqual({ accepted: true, entryId: "queue-2", error: null }); + expect(prompt).toHaveBeenCalledWith("hello", undefined, BACKGROUND_CONTEXT); + expect(requestAbort).toHaveBeenCalledWith("operation-1", BACKGROUND_CONTEXT); + expect(steer).toHaveBeenCalledWith("later", undefined, BACKGROUND_CONTEXT); + expect(followUp).toHaveBeenCalledWith("after", undefined, BACKGROUND_CONTEXT); + } finally { + await host.dispose(); + } + }); + + test("wraps queue, resume, compaction, and navigation lane operations", async () => { + const nextRun = vi.fn(async () => ({ ok: true as const, value: { entryId: "queue-3" } })); + const cancelQueued = vi.fn(async () => ({ ok: true as const, value: { kind: "cancelled" as const } })); + const resume = vi.fn(async () => ({ ok: true as const, value: completed })); + const compact = vi.fn(async () => ({ + ok: true as const, + value: { compaction: { ...completed, operationId: "compact-1", kind: "compaction" as const } }, + })); + const navigateTree = vi.fn(async () => ({ + ok: true as const, + value: { navigation: { ...completed, operationId: "navigation-1", kind: "navigation" as const } }, + })); + const controller = createAgentController({ + nextRun, + cancelQueued, + resume, + compact, + navigateTree, + } as unknown as AgentLane); + + await expect(controller.nextRun({ message: "next", images: null }, BACKGROUND_CONTEXT)).resolves.toEqual({ + accepted: true, + entryId: "queue-3", + error: null, + }); + await expect(controller.cancelQueued("queue-3", BACKGROUND_CONTEXT)).resolves.toEqual({ + outcome: "cancelled", + }); + await expect(controller.resume(BACKGROUND_CONTEXT)).resolves.toMatchObject({ operationId: "operation-1" }); + await expect(controller.compact({ customInstructions: "short" }, BACKGROUND_CONTEXT)).resolves.toMatchObject({ + operationId: "compact-1", + }); + await expect( + controller.navigate( + { targetId: "entry-1", summarize: true, label: "branch", customInstructions: null }, + BACKGROUND_CONTEXT, + ), + ).resolves.toMatchObject({ operationId: "navigation-1" }); + expect(nextRun).toHaveBeenCalledWith("next", undefined, BACKGROUND_CONTEXT); + expect(cancelQueued).toHaveBeenCalledWith("queue-3", BACKGROUND_CONTEXT); + expect(resume).toHaveBeenCalledWith(BACKGROUND_CONTEXT); + expect(compact).toHaveBeenCalledWith({ customInstructions: "short" }, BACKGROUND_CONTEXT); + expect(navigateTree).toHaveBeenCalledWith("entry-1", { summarize: true, label: "branch" }, BACKGROUND_CONTEXT); + }); + + test("reports accepted successful and failed operations", async () => { + const success = createAgentController({ + prompt: async () => ({ ok: true, value: completed }), + } as unknown as AgentLane); + await expect(success.prompt({ message: "hello", images: null }, BACKGROUND_CONTEXT)).resolves.toEqual({ + accepted: true, + operationId: "operation-1", + error: null, + }); + + const failed = createAgentController({ + prompt: async () => ({ + ok: true, + value: { + ...completed, + operationId: "operation-2", + status: "failed", + error: { code: "provider", message: "failed", details: { status: 500 } }, + }, + }), + } as unknown as AgentLane); + await expect(failed.prompt({ message: "hello", images: null }, BACKGROUND_CONTEXT)).resolves.toEqual({ + accepted: true, + operationId: "operation-2", + error: { code: "provider", message: "failed" }, + }); + }); + + test.each(admissionErrors)("maps admission error %# to a stable response", async (error, code, operationId) => { + const controller = createAgentController({ + prompt: async () => ({ ok: false, error }), + } as unknown as AgentLane); + await expect(controller.prompt({ message: "hello", images: null }, BACKGROUND_CONTEXT)).resolves.toEqual({ + accepted: false, + operationId, + error: { code, message: error.message }, + }); + }); +}); diff --git a/packages/coding-agent/test/experimental-cli-command.test.ts b/packages/coding-agent/test/experimental-cli-command.test.ts new file mode 100644 index 0000000000000000000000000000000000000000..c1c5b131cbc2adbe833779d2fd3ae226a0bf18d0 --- /dev/null +++ b/packages/coding-agent/test/experimental-cli-command.test.ts @@ -0,0 +1,147 @@ +import { describe, expect, test } from "vitest"; +import { cli } from "../src/cli/experimental/cli.ts"; + +const UNSUPPORTED_SERVER_OPTIONS = "The experimental server command does not support existing CLI options yet"; +const UNSUPPORTED_CLIENT_OPTIONS = "The experimental client command does not support existing CLI options yet"; + +describe("experimental CLI commands", () => { + test("parses server configuration", () => { + expect( + cli.parse([ + "server", + "--server-id", + "00000000-0000-4000-8000-000000000001", + "--session-dir", + "~/pi-sessions", + "--provider", + "anthropic", + "--model", + "claude-sonnet-4-5", + "-e", + "./first-plugin", + "-e=./second-plugin", + ]), + ).toEqual({ + ok: true, + command: { + command: "server", + serverId: "00000000-0000-4000-8000-000000000001", + sessionDir: "~/pi-sessions", + provider: "anthropic", + model: "claude-sonnet-4-5", + pluginPackages: ["./first-plugin", "./second-plugin"], + }, + }); + }); + + test("parses provider-qualified server models", () => { + expect(cli.parse(["server", "--model", "anthropic/claude-sonnet-4-5:high"])).toEqual({ + ok: true, + command: { command: "server", model: "anthropic/claude-sonnet-4-5:high" }, + }); + }); + + test("parses client transport addresses", () => { + expect(cli.parse(["client", "--connect", "unix:///tmp/pi.sock"])).toEqual({ + ok: true, + command: { command: "client", connect: { transport: "unix", path: "/tmp/pi.sock" } }, + }); + expect( + cli.parse(["client", "--connect", "radius://00000000-0000-4000-8000-000000000001", "--session-id", "demo-1"]), + ).toEqual({ + ok: true, + command: { + command: "client", + connect: { transport: "radius", serverId: "00000000-0000-4000-8000-000000000001" }, + sessionId: "demo-1", + }, + }); + }); + + test.each([ + ["-c", "continue"], + ["--continue", "continue"], + ["-r", "resume"], + ["--resume", "resume"], + ] as const)("parses client Session selection %s", (option, property) => { + expect(cli.parse(["client", option])).toEqual({ + ok: true, + command: { command: "client", [property]: true }, + }); + }); + + test("parses a Session selection followed by a one-shot prompt", () => { + expect(cli.parse(["client", "-r", "Explain this project"])).toEqual({ + ok: true, + command: { command: "client", resume: true, prompt: "Explain this project" }, + }); + }); + + test("parses client model and plugin configuration", () => { + expect( + cli.parse([ + "client", + "--model", + "anthropic/claude-sonnet-4-5:high", + "-e", + "./first-plugin", + "-e", + "./second-plugin", + ]), + ).toEqual({ + ok: true, + command: { + command: "client", + model: "anthropic/claude-sonnet-4-5:high", + pluginPackages: ["./first-plugin", "./second-plugin"], + }, + }); + }); + + test.each([ + [["--auth-token", "secret"], { type: "token", token: "secret" }], + [["--auth-token-file", "/tmp/token"], { type: "file", path: "/tmp/token" }], + ] as const)("parses authentication source %j", (argv, auth) => { + for (const command of ["server", "client"] as const) { + expect(cli.parse([command, ...argv])).toMatchObject({ ok: true, command: { command, auth } }); + } + }); + + test.each(["server", "client"] as const)("permits omitted authentication for %s", (command) => { + expect(cli.parse([command])).toEqual({ ok: true, command: { command } }); + }); + + test.each([ + [["client", "--listen", "unix:///tmp/pi.sock"], UNSUPPORTED_CLIENT_OPTIONS], + [["server", "--listen", "unix:///tmp/pi.sock"], UNSUPPORTED_SERVER_OPTIONS], + [["server", "--connect", "unix:///tmp/pi.sock"], UNSUPPORTED_SERVER_OPTIONS], + [["client", "--connect", "ws://localhost:8080"], 'Unsupported --connect transport "ws:"'], + [["client", "--connect", "radius://not-a-server"], "Radius transport address requires"], + [["client", "--connect", "unix://relative.sock"], "Unix transport address must not include an authority"], + [["client", "--connect", "unix:///tmp/pi.sock?wrong=value"], "Invalid --connect address"], + [["client", "--provider", "anthropic"], "--provider requires --model"], + [["client", "-c", "-r"], "--session-id, --continue, and --resume are mutually exclusive"], + [["client", "--continue=true"], "--continue does not take a value"], + [["server", "--provider", "anthropic"], "--provider requires --model"], + [["server", "--server-id", "not-a-uuid"], "Invalid --server-id"], + [["server", "--server-id"], "--server-id requires a value"], + [["server", "--session-dir"], "--session-dir requires a value"], + [["client", "-e"], "-e requires a value"], + [ + ["server", "--session-dir", "/tmp/first", "--session-dir=/tmp/second"], + "--session-dir may only be specified once", + ], + [["client", "--connect="], "--connect requires a value"], + ] as const)("rejects invalid experimental input %j", (argv, error) => { + const result = cli.parse(argv); + expect(result).toMatchObject({ ok: false }); + if (!result.ok) expect(result.errors).toContainEqual(expect.stringContaining(error)); + }); + + test("rejects unsupported options without parsing them through the stable CLI", () => { + expect(cli.parse(["client", "--tui-mode", "wrong", "--model", "claude-sonnet"])).toEqual({ + ok: false, + errors: [UNSUPPORTED_CLIENT_OPTIONS], + }); + }); +}); diff --git a/packages/coding-agent/test/experimental-cli-entry.test.ts b/packages/coding-agent/test/experimental-cli-entry.test.ts new file mode 100644 index 0000000000000000000000000000000000000000..c2277e45db7ad928ec05a269be12dc4dc8b314fa --- /dev/null +++ b/packages/coding-agent/test/experimental-cli-entry.test.ts @@ -0,0 +1,65 @@ +import { spawnSync } from "node:child_process"; +import { mkdtempSync, rmSync } from "node:fs"; +import { tmpdir } from "node:os"; +import { join, resolve } from "node:path"; +import { afterEach, describe, expect, it } from "vitest"; +import { VERSION } from "../src/config.ts"; + +const sourceResolverPath = resolve(__dirname, "../src/experimental/source-resolver.ts"); +const tempDirs: string[] = []; + +afterEach(() => { + for (const directory of tempDirs.splice(0)) rmSync(directory, { recursive: true, force: true }); +}); + +function runEntry(entry: string, experimental: boolean) { + const directory = mkdtempSync(join(tmpdir(), "pi-cli-boundary-")); + tempDirs.push(directory); + return spawnSync( + process.execPath, + [ + "--import", + sourceResolverPath, + resolve(__dirname, "../src", entry), + "server", + "--server-id", + "invalid", + "--version", + ], + { + cwd: directory, + encoding: "utf8", + timeout: 15_000, + env: { + ...process.env, + HOME: directory, + USERPROFILE: directory, + PI_CODING_AGENT_DIR: join(directory, "agent"), + PI_OFFLINE: "1", + PI_EXPERIMENTAL: experimental ? "1" : "0", + }, + }, + ); +} + +describe("stable and development CLI entrypoints", () => { + // #9132: enabling experiments must not pull remote-server dependencies into the published CLI. + it("does not dispatch experimental commands from the stable entrypoint", () => { + const result = runEntry("cli.ts", true); + expect(result.status, result.stderr).toBe(0); + expect(result.stdout.trim()).toBe(VERSION); + }); + + it("keeps experimental dispatch in the development entrypoint", () => { + const result = runEntry("experimental/cli.ts", true); + expect(result.status, result.stderr).toBe(1); + expect(result.stderr).toContain("Invalid --server-id"); + expect(result.stdout).not.toContain(VERSION); + }); + + it("falls back to the stable CLI when experiments are disabled", () => { + const result = runEntry("experimental/cli.ts", false); + expect(result.status, result.stderr).toBe(0); + expect(result.stdout.trim()).toBe(VERSION); + }); +}); diff --git a/packages/coding-agent/test/experimental-cli-resolution.test.ts b/packages/coding-agent/test/experimental-cli-resolution.test.ts new file mode 100644 index 0000000000000000000000000000000000000000..b9ceaeb292a5265e44fb15e1f5fd1cb0f6159f36 --- /dev/null +++ b/packages/coding-agent/test/experimental-cli-resolution.test.ts @@ -0,0 +1,51 @@ +import { describe, expect, test, vi } from "vitest"; +import { cli } from "../src/cli/experimental/cli.ts"; + +describe("experimental CLI command composition", () => { + test("requires an experimental subcommand", () => { + expect(cli.parse([])).toEqual({ + ok: false, + errors: ["Expected experimental command: server or client"], + }); + }); + + test("passes server options to the command action", async () => { + const runServer = vi.fn(() => undefined); + const result = await cli.execute( + [ + "server", + "--server-id", + "00000000-0000-4000-8000-000000000001", + "--session-dir", + "./sessions", + "--provider", + "anthropic", + "--model", + "claude-sonnet-4-5", + ], + { runServer, runClient: vi.fn(() => undefined) }, + ); + + const command = { + command: "server" as const, + serverId: "00000000-0000-4000-8000-000000000001", + sessionDir: "./sessions", + provider: "anthropic", + model: "claude-sonnet-4-5", + }; + expect(result).toEqual({ ok: true, command }); + expect(runServer).toHaveBeenCalledWith(command); + }); + + test.each(["server", "client"] as const)("executes the parsed %s command", async (name) => { + const context = { + runServer: vi.fn(() => undefined), + runClient: vi.fn(() => undefined), + }; + const result = await cli.execute([name], context); + + expect(result).toEqual({ ok: true, command: { command: name } }); + expect(context.runServer).toHaveBeenCalledTimes(name === "server" ? 1 : 0); + expect(context.runClient).toHaveBeenCalledTimes(name === "client" ? 1 : 0); + }); +}); diff --git a/packages/coding-agent/test/experimental-client-tui.test.ts b/packages/coding-agent/test/experimental-client-tui.test.ts new file mode 100644 index 0000000000000000000000000000000000000000..1209758fe391cf9665d0fa38679e90d7538b5fb9 --- /dev/null +++ b/packages/coding-agent/test/experimental-client-tui.test.ts @@ -0,0 +1,451 @@ +import { createHash } from "node:crypto"; +import { resolve } from "node:path"; +import { + createRemoteServiceBinding, + type MutableReplicatedState, + RemoteServiceProvider, + type RemoteServiceTransport, + replicatedState, +} from "@earendil-works/chord"; +import { BACKGROUND_CONTEXT } from "@earendil-works/chord/context"; +import { + FACET_BUNDLE_ARTIFACT_FORMAT, + FACET_BUNDLE_ARTIFACT_FORMAT_VERSION, + type FacetBundleArtifact, +} from "@earendil-works/chord/node"; +import { + type AgentLane, + type LaneSnapshot, + type LaneTranscriptSnapshot, + type LaneWatchEvent, + reduceLaneSnapshot, +} from "@earendil-works/pi-agent-core"; +import { ProcessTerminal, TuiMainScreen } from "@earendil-works/pi-tui"; +import { beforeAll, describe, expect, test, vi } from "vitest"; +import { type ClientTuiServer, ExperimentalClientTui } from "../src/experimental/client-tui.ts"; +import { createPresentationFacetData } from "../src/experimental/plugins/bundled.ts"; +import { AgentController } from "../src/experimental/services/agent-controller.ts"; +import { createAgentController } from "../src/experimental/services/agent-controller-provider.ts"; +import type { + ServerConnectionState, + ServerServiceSource, + SessionAttachmentState, + SessionServiceSource, +} from "../src/experimental/services/connection.ts"; +import { Models, type ModelsState } from "../src/experimental/services/models.ts"; +import { PresentationPlugins, SessionPlugins } from "../src/experimental/services/plugins.ts"; +import { + SessionDirectory, + type SessionDirectoryState, + SessionManagement, + type SessionSummary, +} from "../src/experimental/services/sessions.ts"; +import { Transcript, type TranscriptState } from "../src/experimental/services/transcript.ts"; +import { initTheme } from "../src/modes/interactive/theme/theme.ts"; + +const serverId = "00000000-0000-4000-8000-000000000001"; + +function session(sessionId: string, createdAt: number): SessionSummary { + return { serverId, sessionId, createdAt }; +} + +function createLoopbackServiceTransport(provider: RemoteServiceProvider): RemoteServiceTransport { + return { + invoke: (call, context) => provider.invoke(call, context), + subscribe: async (serviceId, mode, listener) => { + const subscription = provider.subscribe(serviceId, mode, (update) => listener(update, BACKGROUND_CONTEXT)); + return { + snapshot: subscription.snapshot, + activate: () => subscription.activate(), + close: () => subscription.close(), + }; + }, + }; +} + +function publishReplacement(state: MutableReplicatedState, value: T): void { + const target = state.state as unknown as Record; + const replacement = value as unknown as Record; + for (const key of Object.keys(target)) { + if (!Object.hasOwn(replacement, key)) delete target[key]; + } + Object.assign(target, replacement); + state.publish(BACKGROUND_CONTEXT); +} + +function laneSnapshot(): LaneSnapshot { + return { + lane: "main", + transcript: [], + tipId: null, + configuration: { + model: { provider: "test", modelId: "one" }, + thinkingLevel: "off", + activeToolNames: [], + }, + stats: { + messageCount: 0, + usage: { + input: 0, + output: 0, + cacheRead: 0, + cacheWrite: 0, + totalTokens: 0, + cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 }, + }, + }, + operation: null, + queues: [], + faulted: false, + }; +} + +describe("experimental client TUI", () => { + beforeAll(() => initTheme("dark")); + + test.each([ + ["new", { command: "client" as const }, "two", 1], + ["continued", { command: "client" as const, continue: true }, "one", 0], + ["plugin-selected", { command: "client" as const, pluginPackages: ["./example-plugin"] }, "two", 1], + ] as const)( + "opens a %s Session directly and exercises the full lifecycle only for a new Session", + async (kind, command, sessionId, creates) => { + const directoryState = replicatedState({ revision: 1, sessions: [session("one", 1)] }); + const attachment = replicatedState({ status: "detached" }); + const connectionState = replicatedState({ status: "connected", since: "now" }); + const modelsState = replicatedState({ + catalog: { + revision: 1, + availableModels: [ + { provider: "test", modelId: "one", name: "Model One", reasoning: false }, + { provider: "test", modelId: "two", name: "Model Two", reasoning: true }, + ], + }, + configuration: { model: { provider: "test", modelId: "one" }, thinkingLevel: "off" }, + refresh: { status: "idle" }, + }); + const create = vi.fn(async () => { + const created = session("two", 2); + directoryState.state.revision = 2; + directoryState.state.sessions.push(created); + directoryState.publish(BACKGROUND_CONTEXT); + return created; + }); + const select = vi.fn(async (model: { provider: string; modelId: string }) => { + modelsState.state.configuration.model = model; + modelsState.publish(BACKGROUND_CONTEXT); + }); + const selectThinking = vi.fn(async (thinkingLevel: "off" | "high") => { + modelsState.state.configuration.thinkingLevel = thinkingLevel; + modelsState.publish(BACKGROUND_CONTEXT); + }); + const transcriptState = replicatedState({ + snapshot: laneSnapshot() as LaneTranscriptSnapshot, + event: null, + }); + const emitTranscriptEvent = (event: LaneWatchEvent): void => { + const snapshot = transcriptState.state.snapshot as LaneSnapshot; + if (reduceLaneSnapshot(snapshot, event) === "rebase") { + throw new Error("Test transcript event unexpectedly requires a rebase"); + } + transcriptState.state.event = event; + transcriptState.publish(BACKGROUND_CONTEXT); + }; + let finishPrompt!: () => void; + const promptFinished = new Promise((resolve) => { + finishPrompt = resolve; + }); + const prompt = vi.fn(async () => { + emitTranscriptEvent({ + type: "run_start", + lane: "main", + runId: "run-1", + startedAt: 1, + }); + await promptFinished; + emitTranscriptEvent({ + type: "entry_added", + lane: "main", + entry: { + id: "entry-user", + parentId: null, + seq: 1, + timestamp: 1, + type: "message", + message: { role: "user", content: [{ type: "text", text: "hello" }], timestamp: 1 }, + }, + }); + emitTranscriptEvent({ + type: "entry_added", + lane: "main", + entry: { + id: "entry-assistant", + parentId: "entry-user", + seq: 2, + timestamp: 2, + type: "message", + message: { + role: "assistant", + content: [{ type: "text", text: "remote answer" }], + provider: "test", + model: "one", + api: "test", + usage: transcriptState.value.snapshot!.stats.usage, + stopReason: "stop", + timestamp: 2, + }, + }, + }); + emitTranscriptEvent({ + type: "run_end", + lane: "main", + runId: "run-1", + status: "completed", + fromTipId: null, + tipId: "entry-assistant", + endedAt: 2, + }); + return { + ok: true as const, + value: { + operationId: "run-1", + kind: "run" as const, + status: "completed" as const, + fromTipId: null, + tipId: "entry-assistant", + startedAt: 1, + endedAt: 2, + }, + }; + }); + + const reloadSource = + '"use strict";\nconst { defineFacet, defineService } = require("@earendil-works/chord");\nconst Models = defineService("pi.models");\nmodule.exports = { __esModule: true, default: defineFacet({ id: "test-tui-facet", setup(env) { env.use(Models); } }) };\n'; + const reloadArtifact: FacetBundleArtifact = { + format: FACET_BUNDLE_ARTIFACT_FORMAT, + formatVersion: FACET_BUNDLE_ARTIFACT_FORMAT_VERSION, + plugin: { id: "test-tui-plugin" }, + entryName: "tui", + entry: { + file: "tui.cjs", + integrity: `sha256-${createHash("sha256").update(reloadSource).digest("base64")}`, + externalImports: ["@earendil-works/chord"], + }, + source: reloadSource, + }; + const reloadData = createPresentationFacetData([reloadArtifact]); + const prepareSessionPlugins = vi.fn(async () => reloadData); + const reloadPresentationPlugins = vi.fn(async () => reloadData); + const reloadSessionPlugins = vi.fn(async () => {}); + const serverProvider = new RemoteServiceProvider([SessionDirectory, SessionManagement, PresentationPlugins]); + serverProvider.provide(SessionDirectory, { state: directoryState }); + serverProvider.provide(PresentationPlugins, { + prepareSession: prepareSessionPlugins, + reload: reloadPresentationPlugins, + }); + serverProvider.provide(SessionManagement, { + create, + async remove() {}, + async attach(sessionId) { + publishReplacement(attachment, { status: "attaching", sessionId }); + }, + async detach() { + publishReplacement(attachment, { status: "detached" }); + }, + }); + const sessionProvider = new RemoteServiceProvider([Models, AgentController, SessionPlugins, Transcript]); + sessionProvider.provide(SessionPlugins, { reload: reloadSessionPlugins }); + sessionProvider.provide(Models, { + state: modelsState, + async cycleThinking() {}, + async getThinkingLevels() { + return ["off", "high"]; + }, + async refresh() {}, + select, + selectThinking, + }); + sessionProvider.provide(AgentController, createAgentController({ prompt } as unknown as AgentLane)); + sessionProvider.provide(Transcript, { state: transcriptState }); + + const serverNamespace = createRemoteServiceBinding({ + services: [SessionDirectory, SessionManagement, PresentationPlugins], + transport: createLoopbackServiceTransport(serverProvider), + bound: false, + }); + const serverNamespaceReady = serverNamespace.ready.bind(serverNamespace); + const serverServices: ServerServiceSource = Object.assign(serverNamespace, { + acceptsUnavailableServices: false, + connection: connectionState, + async catalogue() { + return serverProvider.catalogue; + }, + open() { + return { + use: serverNamespace.use.bind(serverNamespace), + observe: serverNamespace.observe.bind(serverNamespace), + async ready() { + await serverNamespace.rebind(true, BACKGROUND_CONTEXT); + await serverNamespaceReady(BACKGROUND_CONTEXT); + }, + async dispose() {}, + }; + }, + async ready() { + await serverNamespace.rebind(true, BACKGROUND_CONTEXT); + await serverNamespaceReady(BACKGROUND_CONTEXT); + }, + }); + const sessionNamespace = createRemoteServiceBinding({ + services: [Models, AgentController, SessionPlugins, Transcript], + transport: createLoopbackServiceTransport(sessionProvider), + bound: false, + }); + const sessionServices: SessionServiceSource = Object.assign(sessionNamespace, { + acceptsUnavailableServices: true, + attachment, + async catalogue() { + return []; + }, + open() { + return { + use: sessionNamespace.use.bind(sessionNamespace), + observe: sessionNamespace.observe.bind(sessionNamespace), + ready: sessionNamespace.ready.bind(sessionNamespace), + async dispose() {}, + }; + }, + async whenAttached(sessionId: string) { + await sessionNamespace.rebind(true, BACKGROUND_CONTEXT); + await sessionNamespace.ready(BACKGROUND_CONTEXT); + publishReplacement(attachment, { status: "attached", sessionId }); + }, + async whenDetached() { + await sessionNamespace.rebind(false, BACKGROUND_CONTEXT); + publishReplacement(attachment, { status: "detached" }); + }, + }); + const server: ClientTuiServer = { + serverId, + radius: true, + server: serverServices, + session: sessionServices, + }; + let finished = false; + const requestRender = vi.fn(); + const ui = new TuiMainScreen(new ProcessTerminal()); + const component = await ExperimentalClientTui.create({ + command, + ui, + servers: [server], + requestRender, + finish() { + finished = true; + }, + }); + try { + expect(create).toHaveBeenCalledTimes(creates); + expect(prepareSessionPlugins).toHaveBeenCalledWith( + { + sessionId, + packagePaths: + "pluginPackages" in command + ? command.pluginPackages.map((packagePath) => resolve(packagePath)) + : null, + }, + expect.anything(), + ); + expect(attachment.value).toEqual({ status: "attached", sessionId }); + expect(select).not.toHaveBeenCalled(); + expect(component.render(80).join("\n")).toContain(`Server: ${serverId}`); + expect(component.render(80).join("\n")).toContain(`Session: ${sessionId}`); + expect(component.render(80).join("\n")).toContain("test/one"); + expect(component.render(80).join("\n")).not.toContain("Experimental Sessions"); + expect(component.render(80).join("\n")).not.toContain("Experimental Models"); + + // Startup selection is the only behavior specific to continue and plugin-selected Sessions. + if (kind !== "new") return; + + component.handleInput("hello"); + component.handleInput("\r"); + await vi.waitFor(() => expect(prompt).toHaveBeenCalledWith("hello", undefined, BACKGROUND_CONTEXT)); + await vi.waitFor(() => expect(component.render(80).join("\n")).toContain("Working...")); + finishPrompt(); + await vi.waitFor(() => expect(component.render(80).join("\n")).toContain("remote answer")); + expect(component.render(80).join("\n")).toContain("hello"); + expect(component.render(80).join("\n")).not.toContain("Working..."); + expect(component.render(80).join("\n")).not.toContain("Operation run-1 completed"); + + component.handleInput("/reload"); + component.handleInput("\u001b"); + component.handleInput("\r"); + await vi.waitFor(() => { + expect(reloadPresentationPlugins).toHaveBeenCalledOnce(); + expect(reloadSessionPlugins).toHaveBeenCalledOnce(); + }); + await vi.waitFor(() => expect(component.render(80).join("\n")).toContain("Reloaded plugins.")); + + publishReplacement(attachment, { status: "detached" }); + publishReplacement(connectionState, { + status: "disconnected", + since: "later", + reason: "network lost", + retryAt: null, + }); + await vi.waitFor(() => expect(component.render(80).join("\n")).toContain("retrying")); + component.handleInput("\u0003"); + expect(finished).toBe(true); + finished = false; + component.handleInput("\u0004"); + expect(finished).toBe(true); + finished = false; + publishReplacement(connectionState, { status: "connecting", attempt: 1 }); + publishReplacement(connectionState, { status: "connected", since: "reconnected" }); + publishReplacement(attachment, { status: "attached", sessionId }); + await vi.waitFor(() => expect(component.render(80).join("\n")).not.toContain("Reattaching")); + + component.handleInput("/model"); + component.handleInput("\u001b"); + component.handleInput("\r"); + await vi.waitFor(() => expect(component.render(80).join("\n")).toContain("Select model:")); + component.handleInput("\u001b[B"); + component.handleInput("\r"); + await vi.waitFor(() => + expect(select).toHaveBeenCalledWith({ provider: "test", modelId: "two" }, expect.anything()), + ); + expect(modelsState.value.configuration.model).toEqual({ provider: "test", modelId: "two" }); + await vi.waitFor(() => expect(component.render(80).join("\n")).not.toContain("Select model:")); + + component.handleInput("/thinking"); + component.handleInput("\u001b"); + component.handleInput("\r"); + await vi.waitFor(() => expect(component.render(80).join("\n")).toContain("Select thinking level:")); + component.handleInput("\u001b[B"); + component.handleInput("\r"); + await vi.waitFor(() => expect(selectThinking).toHaveBeenCalledWith("high", expect.anything())); + expect(modelsState.value.configuration.thinkingLevel).toBe("high"); + + component.handleInput("\u0003"); + await vi.waitFor(() => expect(finished).toBe(true)); + + await component.close(); + const rendersAfterClose = requestRender.mock.calls.length; + directoryState.state.revision = 3; + directoryState.state.sessions = []; + directoryState.publish(BACKGROUND_CONTEXT); + publishReplacement(attachment, { status: "detached" }); + modelsState.state.refresh = { status: "refreshing" }; + modelsState.publish(BACKGROUND_CONTEXT); + expect(requestRender).toHaveBeenCalledTimes(rendersAfterClose); + } finally { + await component.close(); + await Promise.all([ + serverNamespace.dispose(BACKGROUND_CONTEXT), + sessionNamespace.dispose(BACKGROUND_CONTEXT), + ]); + serverProvider.dispose(); + sessionProvider.dispose(); + } + }, + ); +}); diff --git a/packages/coding-agent/test/experimental-internal-process.test.ts b/packages/coding-agent/test/experimental-internal-process.test.ts new file mode 100644 index 0000000000000000000000000000000000000000..a58c66586c2944dacf5c0fd882e03786cee3eef5 --- /dev/null +++ b/packages/coding-agent/test/experimental-internal-process.test.ts @@ -0,0 +1,61 @@ +import type { ChildProcess } from "node:child_process"; +import { once } from "node:events"; +import { mkdtemp, rm } from "node:fs/promises"; +import { createConnection } from "node:net"; +import { tmpdir } from "node:os"; +import { join } from "node:path"; +import { afterEach, describe, expect, test } from "vitest"; +import { spawnInternalProcess, terminateInternalProcess } from "../src/experimental/process.ts"; + +const children = new Set(); +const directories = new Set(); + +afterEach(async () => { + for (const child of children) { + if (child.exitCode === null && child.signalCode === null) { + const exited = once(child, "exit"); + child.kill("SIGTERM"); + await exited; + } + } + children.clear(); + await Promise.all([...directories].map((directory) => rm(directory, { recursive: true, force: true }))); + directories.clear(); +}); + +describe.skipIf(process.platform === "win32")("experimental internal process launcher", () => { + test("starts the coordinator through the current runtime", async () => { + // Keep Unix socket paths below macOS's short sun_path limit. + const directory = await mkdtemp(join(tmpdir(), "pi-ip-")); + directories.add(directory); + const publicPath = join(directory, "p.sock"); + const controlPath = join(directory, "c.sock"); + const child = spawnInternalProcess("coordinator", [publicPath, controlPath]); + children.add(child); + + await expect.poll(() => canConnect(controlPath), { timeout: 10_000 }).toBe(true); + expect(child.pid).not.toBe(process.pid); + }); + + test("waits for a failed activation child to terminate", async () => { + const directory = await mkdtemp(join(tmpdir(), "pi-ip-stop-")); + directories.add(directory); + const child = spawnInternalProcess("coordinator", [join(directory, "p.sock"), join(directory, "c.sock")]); + children.add(child); + + await expect.poll(() => child.pid).toEqual(expect.any(Number)); + await terminateInternalProcess(child); + expect(child.signalCode).toBe("SIGKILL"); + }); +}); + +function canConnect(path: string): Promise { + return new Promise((resolve) => { + const socket = createConnection(path); + socket.once("connect", () => { + socket.destroy(); + resolve(true); + }); + socket.once("error", () => resolve(false)); + }); +} diff --git a/packages/coding-agent/test/experimental-plugin-reload.test.ts b/packages/coding-agent/test/experimental-plugin-reload.test.ts new file mode 100644 index 0000000000000000000000000000000000000000..26eaa2cfd4edbbc85d8372ae44f28c5ddd2a49f2 --- /dev/null +++ b/packages/coding-agent/test/experimental-plugin-reload.test.ts @@ -0,0 +1,94 @@ +import { defineFacet, type FacetLoader } from "@earendil-works/chord"; +import { type AgentLane, BACKGROUND_CONTEXT, type LaneSnapshot } from "@earendil-works/pi-agent-core"; +import { describe, expect, test, vi } from "vitest"; +import { SessionPlugins } from "../src/experimental/services/plugins.ts"; +import { createSessionWorkerServices } from "../src/experimental/services/worker.ts"; + +describe("experimental plugin reload", () => { + test("loads and cuts over a fresh Session facet generation", async () => { + const activations: number[] = []; + const disposals: number[] = []; + let generation = 0; + const facetLoader: FacetLoader = { + async load() { + const current = ++generation; + return { + facets: [ + defineFacet({ + id: "reloadable-session-plugin", + setup(env) { + env.onActivate(() => { + activations.push(current); + }); + }, + }), + ], + async dispose() { + disposals.push(current); + }, + }; + }, + }; + const snapshot: LaneSnapshot = { + lane: "main", + transcript: [], + tipId: null, + configuration: { + model: { provider: "test", modelId: "model" }, + thinkingLevel: "off", + activeToolNames: [], + }, + stats: { + messageCount: 0, + usage: { + input: 0, + output: 0, + cacheRead: 0, + cacheWrite: 0, + totalTokens: 0, + cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 }, + }, + }, + operation: null, + queues: [], + faulted: false, + }; + const lane = { + async watch() { + return { + snapshot, + start() {}, + async resnapshot() { + return snapshot; + }, + unsubscribe() {}, + }; + }, + async getModel() { + return undefined; + }, + async getThinkingLevel() { + return "off" as const; + }, + } as unknown as AgentLane; + const services = await createSessionWorkerServices({ + lane, + modelRuntime: undefined, + facetLoader, + publish: vi.fn(async () => {}), + }); + try { + expect(activations).toEqual([1]); + await services.invoke( + { serviceId: SessionPlugins.id, member: "reload", args: [] }, + { serverConnectionId: "server-1", attachmentId: "attachment-1" }, + BACKGROUND_CONTEXT, + ); + expect(activations).toEqual([1, 2]); + expect(disposals).toEqual([1]); + } finally { + await services.dispose(); + } + expect(disposals).toEqual([1, 2]); + }); +}); diff --git a/packages/coding-agent/test/experimental-presentation-facets.test.ts b/packages/coding-agent/test/experimental-presentation-facets.test.ts new file mode 100644 index 0000000000000000000000000000000000000000..37f00777201db3a8b262a86b7d0d2ea05396d05d --- /dev/null +++ b/packages/coding-agent/test/experimental-presentation-facets.test.ts @@ -0,0 +1,173 @@ +import { randomUUID } from "node:crypto"; +import { mkdir, mkdtemp, rm, writeFile } from "node:fs/promises"; +import { join } from "node:path"; +import { fileURLToPath } from "node:url"; +import { BACKGROUND_CONTEXT } from "@earendil-works/chord/context"; +import { readFacetBundleManifest } from "@earendil-works/chord/node"; +import { afterEach, describe, expect, test } from "vitest"; +import { + activateBuiltinClientServices, + type ClientRuntime, + openClientRuntime, +} from "../src/experimental/client-runtime.ts"; +import { createPresentationFacetData, createPresentationFacetLoaders } from "../src/experimental/plugins/bundled.ts"; +import { createServerPluginPackage, restoreServerPluginPackageProfile } from "../src/experimental/plugins/package.ts"; +import { type RunningServer, startServer } from "../src/experimental/server.ts"; +import { PresentationPlugins } from "../src/experimental/services/plugins.ts"; + +const runtimes = new Set(); +const runningServers = new Set(); +const directories = new Set(); + +afterEach(async () => { + await Promise.allSettled([...runtimes].map((runtime) => runtime.dispose())); + await Promise.allSettled([...runningServers].map((server) => server.close())); + await Promise.allSettled([...directories].map((directory) => rm(directory, { force: true, recursive: true }))); + runtimes.clear(); + runningServers.clear(); + directories.clear(); +}); + +describe("server-selected presentation facets", () => { + test("rejects local plugin paths for Radius servers", async () => { + await expect( + openClientRuntime({ + command: "client", + connect: { transport: "radius", serverId: randomUUID() }, + pluginPackages: ["./local-plugin"], + }), + ).rejects.toThrow("only be configured on a local Unix server"); + }); + + test("restores plugin package selections for later server generations", async () => { + const directory = await mkdtemp("/tmp/pi-presentation-profile-"); + directories.add(directory); + const serverId = randomUUID(); + const packagePaths = [join(directory, "first-plugin"), join(directory, "second-plugin")]; + await expect(restoreServerPluginPackageProfile(directory, serverId, packagePaths)).resolves.toEqual(packagePaths); + await expect(restoreServerPluginPackageProfile(directory, serverId)).resolves.toEqual(packagePaths); + await expect(restoreServerPluginPackageProfile(directory, serverId, [])).resolves.toEqual([]); + await expect(restoreServerPluginPackageProfile(directory, serverId)).resolves.toEqual([]); + }); + + test("builds conventional plugin entries into the server-owned plugin cache", async () => { + const directory = await mkdtemp("/tmp/pi-presentation-package-"); + directories.add(directory); + const serverId = randomUUID(); + const packagePath = join(directory, "pi-example-plugin"); + await mkdir(join(packagePath, "src"), { recursive: true }); + await writeFile( + join(packagePath, "package.json"), + `${JSON.stringify({ + name: "@earendil-works/test-plugin", + version: "1.0.0", + peerDependencies: { + "@earendil-works/chord": "^0.84.4", + "@earendil-works/pi-coding-agent": "^0.84.4", + }, + })}\n`, + ); + const sourcePath = join(packagePath, "src", "tui.ts"); + await writeFile( + sourcePath, + 'import { defineFacet } from "@earendil-works/chord"; import { SlashCommands } from "@earendil-works/pi-coding-agent/experimental/plugin"; export default defineFacet({ id: "built-a", setup(env) { env.use(SlashCommands); } });\n', + ); + const plugin = createServerPluginPackage(directory, serverId, packagePath); + + const first = await plugin.build(); + expect(first).toHaveLength(1); + expect(plugin.manifestPath).toMatch( + new RegExp(`/plugin-builds/${serverId}/pi-example-plugin-[a-f0-9]{12}/chord-facets\\.json$`, "u"), + ); + expect(first[0]?.plugin).toEqual({ id: "@earendil-works/test-plugin", version: "1.0.0" }); + const firstLoaded = await createPresentationFacetLoaders(createPresentationFacetData(first))[0]!.load(); + expect(firstLoaded.facets.map(({ id }) => id)).toEqual(["built-a"]); + await firstLoaded.dispose(); + + await writeFile( + sourcePath, + 'import { defineFacet } from "@earendil-works/chord"; import { SlashCommands } from "@earendil-works/pi-coding-agent/experimental/plugin"; export default defineFacet({ id: "built-b", setup(env) { env.use(SlashCommands); } });\n', + ); + const second = await plugin.build(); + expect(second[0]?.source).not.toBe(first[0]?.source); + const secondLoaded = await createPresentationFacetLoaders(createPresentationFacetData(second))[0]!.load(); + expect(secondLoaded.facets.map(({ id }) => id)).toEqual(["built-b"]); + await secondLoaded.dispose(); + + const secondPackagePath = join(directory, "second-plugin"); + await mkdir(join(secondPackagePath, "src"), { recursive: true }); + await Promise.all([ + writeFile( + join(secondPackagePath, "package.json"), + `${JSON.stringify({ + name: "@earendil-works/second-test-plugin", + version: "1.0.0", + peerDependencies: { "@earendil-works/chord": "^0.84.4" }, + })}\n`, + ), + writeFile( + join(secondPackagePath, "src", "tui.ts"), + 'import { defineFacet } from "@earendil-works/chord"; export default defineFacet({ id: "second-built", setup() {} });\n', + ), + ]); + const running = await startServer({ + directory: join(directory, "server"), + sessionDir: join(directory, "sessions"), + }); + runningServers.add(running); + const runtime = await openClientRuntime({ + command: "client", + connect: { transport: "unix", path: running.socketPath }, + pluginPackages: [packagePath, secondPackagePath], + }); + runtimes.add(runtime); + const activated = await activateBuiltinClientServices(runtime.servers[0]!); + const sessionId = randomUUID(); + await activated.management.create({ id: sessionId }, BACKGROUND_CONTEXT); + const presentationPlugins = await activated.plugins.prepareSession( + { sessionId, packagePaths: [packagePath, secondPackagePath] }, + BACKGROUND_CONTEXT, + ); + await expect(restoreServerPluginPackageProfile(join(directory, "server"), running.serverId)).resolves.toEqual([]); + const serverLoaded = await Promise.all( + createPresentationFacetLoaders(presentationPlugins).map((loader) => loader.load()), + ); + expect(serverLoaded.flatMap(({ facets }) => facets.map(({ id }) => id))).toEqual(["built-b", "second-built"]); + await Promise.all(serverLoaded.map((loaded) => loaded.dispose())); + + await writeFile( + sourcePath, + 'import { defineFacet } from "@earendil-works/chord"; import { SlashCommands } from "@earendil-works/pi-coding-agent/experimental/plugin"; export default defineFacet({ id: "built-c", setup(env) { env.use(SlashCommands); } });\n', + ); + const services = runtime.servers[0]!.server.open({ + services: [PresentationPlugins], + assertAccess() {}, + onError() {}, + }); + try { + await services.ready(BACKGROUND_CONTEXT); + const data = await services.use(PresentationPlugins).reload(BACKGROUND_CONTEXT); + const reloaded = await Promise.all(createPresentationFacetLoaders(data).map((loader) => loader.load())); + expect(reloaded.flatMap(({ facets }) => facets.map(({ id }) => id))).toEqual(["built-c", "second-built"]); + await Promise.all(reloaded.map((loaded) => loaded.dispose())); + } finally { + await services.dispose(BACKGROUND_CONTEXT); + } + }); + + test("builds the example plugin package without a package-owned build script", async () => { + const directory = await mkdtemp("/tmp/pi-example-plugin-"); + directories.add(directory); + const serverId = randomUUID(); + const packagePath = fileURLToPath(new URL("../examples/plugins/pi-example-plugin", import.meta.url)); + const plugin = createServerPluginPackage(directory, serverId, packagePath); + + const artifacts = await plugin.build(); + const manifest = await readFacetBundleManifest(plugin.manifestPath); + expect(manifest.plugin).toEqual({ id: "@earendil-works/pi-example-plugin", version: "1.0.0" }); + expect(Object.keys(manifest.entries)).toEqual(["session", "tui"]); + const loaded = await createPresentationFacetLoaders(createPresentationFacetData(artifacts))[0]!.load(); + expect(loaded.facets.map(({ id }) => id)).toEqual(["@earendil-works/pi-example-plugin/tui"]); + await loaded.dispose(); + }); +}); diff --git a/packages/coding-agent/test/experimental-radius-relay.test.ts b/packages/coding-agent/test/experimental-radius-relay.test.ts new file mode 100644 index 0000000000000000000000000000000000000000..1048798c07e7aed38747aa8c0334e51832160eba --- /dev/null +++ b/packages/coding-agent/test/experimental-radius-relay.test.ts @@ -0,0 +1,286 @@ +import type { Client } from "@earendil-works/pi-client"; +import type { Server } from "@earendil-works/pi-server"; +import { afterEach, beforeEach, describe, expect, test, vi } from "vitest"; +import { RadiusRelayAuthResolver } from "../src/experimental/radius-auth.ts"; +import { + createRadiusClientTransportFactory, + encodeRelayDataFrame, + parseRelayDataFrame, + RadiusClientReconnect, + RadiusRelayHost, + type RadiusRelayWebSocket, + type RadiusRelayWebSocketFactory, +} from "../src/experimental/radius-relay.ts"; +import { allowNetwork } from "./test-network-env.ts"; + +const serverId = "00000000-0000-4000-8000-000000000001"; +const connectionId = "00000000-0000-4000-8000-000000000002"; + +type Listener = (event: unknown) => void; + +class FakeWebSocket { + readonly sent: Array = []; + onSend: ((data: string | ArrayBuffer) => void) | undefined; + readonly listeners = new Map>(); + binaryType: "arraybuffer" | "blob" = "blob"; + bufferedAmount = 0; + protocol = ""; + readyState = 0; + readonly OPEN = 1; + + addEventListener(type: string, listener: Listener): void { + const listeners = this.listeners.get(type) ?? new Set(); + listeners.add(listener); + this.listeners.set(type, listeners); + } + + removeEventListener(type: string, listener: Listener): void { + this.listeners.get(type)?.delete(listener); + } + + send(data: string | ArrayBuffer): void { + if (this.readyState !== this.OPEN) throw new Error("socket is not open"); + this.sent.push(data); + this.onSend?.(data); + } + + close(code = 1000, reason = ""): void { + if (code !== 1000 && (code < 3000 || code > 4999)) { + throw new DOMException("Invalid close code", "InvalidAccessError"); + } + if (this.readyState === 3) return; + this.readyState = 3; + this.emit("close", { code, reason }); + } + + open(protocol: string): void { + this.protocol = protocol; + this.readyState = this.OPEN; + this.emit("open", {}); + } + + message(data: unknown): void { + this.emit("message", { data }); + } + + remoteClose(code = 1006, reason = "lost"): void { + this.readyState = 3; + this.emit("close", { code, reason }); + } + + fail(error: Error): void { + this.emit("error", { error, message: error.message }); + } + + abnormalClose(error: Error): void { + this.readyState = 3; + this.emit("error", { error, message: error.message }); + this.emit("close", { code: 1006, reason: "" }); + } + + private emit(type: string, event: unknown): void { + for (const listener of [...(this.listeners.get(type) ?? [])]) listener(event); + } +} + +function socketFactory() { + const sockets: Array<{ socket: FakeWebSocket; options: Parameters[0] }> = []; + const factory: RadiusRelayWebSocketFactory = (options) => { + const socket = new FakeWebSocket(); + sockets.push({ socket, options }); + return socket as unknown as RadiusRelayWebSocket; + }; + return { sockets, factory }; +} + +beforeEach(() => allowNetwork()); + +afterEach(() => { + vi.useRealTimers(); + vi.unstubAllEnvs(); +}); + +describe("experimental Radius relay", () => { + test("matches the Radius multiplexing envelope", () => { + const payload = Uint8Array.from([0, 1, 2, 255]); + const parsed = parseRelayDataFrame(encodeRelayDataFrame(connectionId, payload)); + + expect(parsed?.connectionId).toBe(connectionId); + expect([...new Uint8Array(parsed!.payload)]).toEqual([...payload]); + }); + + test("bridges multiplexed host clients into independent server connections", async () => { + const webSockets = socketFactory(); + const received: Uint8Array[] = []; + let accepted: + | { + send(chunk: Uint8Array): Promise; + close(finalChunk?: Uint8Array): Promise; + } + | undefined; + const onClose = vi.fn(); + const server = { + accept(connection: typeof accepted) { + accepted = connection; + return { + onData: (chunk: Uint8Array) => received.push(chunk.slice()), + onClose, + onError: vi.fn(), + }; + }, + } as unknown as Pick; + const statuses: string[] = []; + const host = new RadiusRelayHost({ + serverId, + server, + auth: new RadiusRelayAuthResolver({ type: "token", token: "secret" }), + webSocketFactory: webSockets.factory, + onStatus: (status) => statuses.push(status.status), + }); + host.start(); + await vi.waitFor(() => expect(webSockets.sockets).toHaveLength(1)); + const { socket, options } = webSockets.sockets[0]!; + expect(options.authorization).toBe("Bearer secret"); + socket.open(options.protocol); + await vi.waitFor(() => expect(statuses).toContain("connected")); + + socket.message(JSON.stringify({ version: 1, type: "connection_open", connection_id: connectionId })); + expect(accepted).toBeDefined(); + const fromClient = Uint8Array.from([1, 2, 3]); + socket.message(encodeRelayDataFrame(connectionId, fromClient)); + expect(received.map((chunk) => [...chunk])).toEqual([[1, 2, 3]]); + + await accepted!.send(Uint8Array.from([4, 5, 6])); + const outbound = parseRelayDataFrame(socket.sent.at(-1) as ArrayBuffer); + expect(outbound?.connectionId).toBe(connectionId); + expect([...new Uint8Array(outbound!.payload)]).toEqual([4, 5, 6]); + + socket.message(JSON.stringify({ version: 1, type: "connection_close", connection_id: connectionId })); + expect(onClose).toHaveBeenCalledOnce(); + await host.close(); + }); + + test("reconnects the server host after the relay connection drops", async () => { + vi.useFakeTimers(); + const webSockets = socketFactory(); + const statuses: string[] = []; + const host = new RadiusRelayHost({ + serverId, + server: { accept: vi.fn() } as unknown as Pick, + auth: new RadiusRelayAuthResolver({ type: "token", token: "secret" }), + webSocketFactory: webSockets.factory, + onStatus: (status) => statuses.push(status.status), + }); + host.start(); + await vi.waitFor(() => expect(webSockets.sockets).toHaveLength(1)); + webSockets.sockets[0]!.socket.open(webSockets.sockets[0]!.options.protocol); + await vi.waitFor(() => expect(statuses).toContain("connected")); + + webSockets.sockets[0]!.socket.remoteClose(); + await vi.advanceTimersByTimeAsync(1_000); + await vi.waitFor(() => expect(webSockets.sockets).toHaveLength(2)); + await host.close(); + }); + + test("uses a raw authenticated WebSocket as the client byte transport", async () => { + const webSockets = socketFactory(); + const onData = vi.fn(); + const onClose = vi.fn(); + const onError = vi.fn(); + const transportPromise = createRadiusClientTransportFactory({ + serverId, + auth: new RadiusRelayAuthResolver({ type: "token", token: "secret" }), + webSocketFactory: webSockets.factory, + })({ onData, onClose, onError }); + await vi.waitFor(() => expect(webSockets.sockets).toHaveLength(1)); + const { socket, options } = webSockets.sockets[0]!; + expect(options.authorization).toBe("Bearer secret"); + socket.open(options.protocol); + const transport = await transportPromise; + + await transport.send(Uint8Array.from([1, 2, 3])); + expect([...new Uint8Array(socket.sent[0] as ArrayBuffer)]).toEqual([1, 2, 3]); + socket.message(Uint8Array.from([4, 5, 6]).buffer); + expect(onData).toHaveBeenCalledOnce(); + expect([...onData.mock.calls[0]![0]]).toEqual([4, 5, 6]); + socket.remoteClose(); + expect(onClose).toHaveBeenCalledOnce(); + expect(onError).not.toHaveBeenCalled(); + }); + + test("reports established abnormal closures so the client can reconnect", async () => { + const webSockets = socketFactory(); + const onClose = vi.fn(); + const onError = vi.fn(); + const transportPromise = createRadiusClientTransportFactory({ + serverId, + auth: new RadiusRelayAuthResolver({ type: "token", token: "secret" }), + webSocketFactory: webSockets.factory, + })({ onData: vi.fn(), onClose, onError }); + await vi.waitFor(() => expect(webSockets.sockets).toHaveLength(1)); + const { socket, options } = webSockets.sockets[0]!; + socket.open(options.protocol); + await transportPromise; + + expect(() => socket.abnormalClose(new Error("network lost"))).not.toThrow(); + expect(onError).toHaveBeenCalledOnce(); + expect(onError).toHaveBeenCalledWith(expect.objectContaining({ message: "network lost" })); + expect(onClose).not.toHaveBeenCalled(); + }); + + test("reports a useful error when Undici omits WebSocket failure details", async () => { + const webSockets = socketFactory(); + const opening = createRadiusClientTransportFactory({ + serverId, + auth: new RadiusRelayAuthResolver({ type: "token", token: "secret" }), + webSocketFactory: webSockets.factory, + })({ onData: vi.fn(), onClose: vi.fn(), onError: vi.fn() }); + const failure = expect(opening).rejects.toThrow("Radius WebSocket connection failed"); + await vi.waitFor(() => expect(webSockets.sockets).toHaveLength(1)); + webSockets.sockets[0]!.socket.fail(new Error("")); + await failure; + }); + + test("reconnects an established client and restores its selected Session", async () => { + vi.useFakeTimers(); + const connectionListeners = new Set<(change: { state: string }) => void>(); + const attachmentListeners = new Set<(attachment: { sessionId: string } | undefined) => void>(); + let attempts = 0; + const reattach = vi.fn(async () => {}); + const client = { + connected: true, + connectionState: "connected", + attachment: { sessionId: "demo-1" }, + onConnectionStateChange(listener: (change: { state: string }) => void) { + connectionListeners.add(listener); + return () => connectionListeners.delete(listener); + }, + onAttachmentChange(listener: (attachment: { sessionId: string } | undefined) => void) { + attachmentListeners.add(listener); + return () => attachmentListeners.delete(listener); + }, + async reconnect() { + attempts++; + if (attempts === 1) throw new Error("temporary failure"); + this.connected = true; + this.connectionState = "connected"; + for (const listener of connectionListeners) listener({ state: "connected" }); + return { serverId }; + }, + disconnect() { + this.connected = false; + this.connectionState = "disconnected"; + for (const listener of connectionListeners) listener({ state: "disconnected" }); + }, + }; + const reconnect = new RadiusClientReconnect(client as unknown as Client, reattach); + client.disconnect(); + + await vi.advanceTimersByTimeAsync(1_000); + expect(attempts).toBe(1); + await vi.advanceTimersByTimeAsync(2_000); + expect(attempts).toBe(2); + expect(reattach).toHaveBeenCalledWith("demo-1"); + await reconnect.dispose(); + }); +}); diff --git a/packages/coding-agent/test/experimental-remote-runtime.test.ts b/packages/coding-agent/test/experimental-remote-runtime.test.ts new file mode 100644 index 0000000000000000000000000000000000000000..8f8a8a4c47303d29318d5569c54da3825ddb91bf --- /dev/null +++ b/packages/coding-agent/test/experimental-remote-runtime.test.ts @@ -0,0 +1,837 @@ +import { lstat, mkdir, mkdtemp, readdir, rm, writeFile } from "node:fs/promises"; +import { join } from "node:path"; +import { fileURLToPath } from "node:url"; +import { type Context, createFacetHost, defineFacet, defineService } from "@earendil-works/chord"; +import { BACKGROUND_CONTEXT } from "@earendil-works/chord/context"; +import { Client, ServerError as ClientServerError } from "@earendil-works/pi-client"; +import { createUnixTransportFactory } from "@earendil-works/pi-client/unix"; +import { afterEach, beforeEach, describe, expect, test, vi } from "vitest"; +import { ExampleFacetService } from "../examples/plugins/pi-example-plugin/src/contract.ts"; +import { runClient } from "../src/experimental/client.ts"; +import { activateBuiltinClientServices, openClientRuntime } from "../src/experimental/client-runtime.ts"; +import { createPresentationFacetLoaders } from "../src/experimental/plugins/bundled.ts"; +import * as processRuntime from "../src/experimental/process.ts"; +import { type RunningServer, startServer } from "../src/experimental/server.ts"; +import { AgentController } from "../src/experimental/services/agent-controller.ts"; +import { createSessionServiceSource, type SessionAttachmentState } from "../src/experimental/services/connection.ts"; +import { Models } from "../src/experimental/services/models.ts"; +import { PresentationPlugins, SessionPlugins } from "../src/experimental/services/plugins.ts"; +import { SessionDirectory, SessionManagement } from "../src/experimental/services/sessions.ts"; +import { Transcript } from "../src/experimental/services/transcript.ts"; +import { createServerServiceBinding, createSessionServiceBinding } from "./experimental-service-binding.ts"; +import { + configureExperimentalWorkerModel, + createExperimentalSessions, + readExperimentalSessionState, +} from "./experimental-session-support.ts"; +import { KeyedProbe } from "./fixtures/keyed-service.ts"; + +const servers = new Set(); +const clients = new Set(); +const directories = new Set(); +const fauxWorkerEntryUrl = new URL("fixtures/faux-session-worker.ts", import.meta.url); +const realSpawnInternalProcess = processRuntime.spawnInternalProcess; +const sessionWorkerModel = { provider: "anthropic", model: "claude-sonnet-4-5" } as const; +const SecondPluginService = defineService<{ read(context: Context): Promise }>("test.second-plugin"); +let agentDir: string; + +beforeEach(async () => { + agentDir = await mkdtemp(join("/tmp", "pi-experimental-agent-")); + directories.add(agentDir); + await configureExperimentalWorkerModel(agentDir); + vi.stubEnv("PI_CODING_AGENT_DIR", agentDir); + await createExperimentalSessions(join(agentDir, "experimental", "sessions"), ["demo-1", "demo-2"]); +}); + +async function makeServer(): Promise<{ directory: string; runtime: RunningServer }> { + const directory = await mkdtemp(join("/tmp", "pes-")); + directories.add(directory); + const runtime = await startServer({ ...sessionWorkerModel, directory }); + servers.add(runtime); + return { directory, runtime }; +} + +async function attachSession(client: Client, sessionId: string): Promise { + const services = createServerServiceBinding(client, { services: [SessionManagement] }); + try { + await services.ready(BACKGROUND_CONTEXT); + await services.use(SessionManagement).attach(sessionId, BACKGROUND_CONTEXT); + } finally { + await services.dispose(BACKGROUND_CONTEXT); + } +} + +async function attachClient(runtime: RunningServer, sessionId: string): Promise { + const client = await Client.connect({ + serverId: runtime.serverId, + transportFactory: createUnixTransportFactory({ path: runtime.socketPath }), + }); + clients.add(client); + await attachSession(client, sessionId); + return client; +} + +afterEach(async () => { + await Promise.all([...clients].map((client) => client.dispose())); + clients.clear(); + await Promise.all([...servers].map((server) => server.close())); + servers.clear(); + vi.unstubAllEnvs(); + await Promise.all([...directories].map((directory) => rm(directory, { recursive: true, force: true }))); + directories.clear(); +}); + +describe("experimental durable server composition", () => { + test("uses PI_SERVER_DIR and PI_SERVER_ID", async () => { + const directory = await mkdtemp(join("/tmp", "pi-server-dir-")); + directories.add(directory); + const serverId = "00000000-0000-4000-8000-000000000001"; + vi.stubEnv("PI_SERVER_DIR", directory); + vi.stubEnv("PI_SERVER_ID", serverId); + const runtime = await startServer(); + servers.add(runtime); + + expect(runtime.serverId).toBe(serverId); + expect(runtime.socketPath).toBe(join(directory, `${serverId}.sock`)); + expect((await lstat(directory)).mode & 0o777).toBe(0o700); + const publicSocket = await lstat(runtime.socketPath); + const controlSocket = await lstat(join(directory, `control-${runtime.serverId}.sock`)); + expect(publicSocket.isSocket()).toBe(true); + expect(publicSocket.mode & 0o777).toBe(0o600); + expect(controlSocket.isSocket()).toBe(true); + expect(controlSocket.mode & 0o777).toBe(0o600); + const entries = await readdir(directory); + expect(entries).toHaveLength(3); + expect(entries.every((entry) => !entry.startsWith("."))).toBe(true); + expect(entries).toContain(`${serverId}.sock`); + expect(entries).toContain(`control-${serverId}.sock`); + expect(entries).toContainEqual(expect.stringMatching(new RegExp(`^server-${serverId}-[0-9a-f]{12}\\.sock$`))); + await expect(runClient({ command: "client" })).resolves.toMatchObject({ + kind: "list", + sessions: [{ sessionId: "demo-1" }, { sessionId: "demo-2" }], + }); + }); + + test("rejects a provider without a model", async () => { + const directory = await mkdtemp(join("/tmp", "pes-")); + directories.add(directory); + await expect(startServer({ directory, provider: "anthropic" })).rejects.toThrow("provider requires a model"); + }); + + test("preserves an existing Session model when the server default changes", async () => { + await writeFile( + join(agentDir, "settings.json"), + JSON.stringify({ defaultProvider: "anthropic", defaultModel: "claude-opus-4-6" }), + ); + const directory = await mkdtemp(join("/tmp", "pes-")); + directories.add(directory); + const first = await startServer({ directory }); + servers.add(first); + const firstClient = await attachClient(first, "demo-1"); + await firstClient.dispose(); + clients.delete(firstClient); + await expect.poll(() => first.workerPids.has("demo-1")).toBe(false); + await first.close(); + + const second = await startServer({ ...sessionWorkerModel, directory }); + servers.add(second); + const secondClient = await attachClient(second, "demo-1"); + await secondClient.dispose(); + clients.delete(secondClient); + await expect.poll(() => second.workerPids.has("demo-1")).toBe(false); + const state = await readExperimentalSessionState(second.sessionDir, "demo-1"); + expect(state.model).toEqual({ provider: "anthropic", modelId: "claude-opus-4-6" }); + }); + + test("rejects model options when discovery selects an existing server", async () => { + const { directory } = await makeServer(); + await expect(runClient({ command: "client", model: "anthropic/claude-opus-4-6" }, { directory })).rejects.toThrow( + "Model selection is only valid when automatically activating a new server", + ); + }); + + test("rechecks an auto-discovered server after a version mismatch", async () => { + const { directory, runtime } = await makeServer(); + const connect = Client.connect.bind(Client); + vi.spyOn(Client, "connect") + .mockRejectedValueOnce(new ClientServerError({ code: "version", message: "stale server" })) + .mockImplementation((options) => connect(options)); + const clientRuntime = await openClientRuntime({ command: "client" }, { directory }); + try { + expect(clientRuntime.servers.map(({ route }) => route.serverId)).toEqual([runtime.serverId]); + } finally { + await clientRuntime.dispose(); + } + }); + + test("serializes concurrent cold activation and retires after both clients leave", async () => { + const directory = await mkdtemp(join("/tmp", "pi-auto-server-")); + directories.add(directory); + const serverId = "00000000-0000-4000-8000-000000000001"; + vi.stubEnv("PI_SERVER_DIR", directory); + vi.stubEnv("PI_SERVER_ID", serverId); + + const results = await Promise.all([runClient({ command: "client" }), runClient({ command: "client" })]); + expect(results).toEqual([ + { + kind: "list", + sessions: [ + { serverId, sessionId: "demo-1" }, + { serverId, sessionId: "demo-2" }, + ], + }, + { + kind: "list", + sessions: [ + { serverId, sessionId: "demo-1" }, + { serverId, sessionId: "demo-2" }, + ], + }, + ]); + expect(await pathExists(join(directory, `${serverId}.sock`))).toBe(true); + await expect.poll(() => pathExists(join(directory, `${serverId}.sock`)), { timeout: 5_000 }).toBe(false); + await expect.poll(() => pathExists(join(directory, `control-${serverId}.sock`)), { timeout: 5_000 }).toBe(false); + }); + + test("passes client plugin packages to a cold server and restores them for its next generation", async () => { + const directory = await mkdtemp(join("/tmp", "pi-auto-plugin-")); + directories.add(directory); + const serverId = "00000000-0000-4000-8000-000000000001"; + const packagePath = fileURLToPath(new URL("../examples/plugins/pi-example-plugin", import.meta.url)); + vi.stubEnv("PI_SERVER_DIR", directory); + vi.stubEnv("PI_SERVER_ID", serverId); + + const first = await openClientRuntime({ command: "client", ...sessionWorkerModel }); + try { + const activated = await activateBuiltinClientServices(first.servers[0]!); + const presentationPlugins = await activated.plugins.prepareSession( + { sessionId: "demo-1", packagePaths: [packagePath] }, + BACKGROUND_CONTEXT, + ); + await activated.management.attach("demo-1", BACKGROUND_CONTEXT); + const loaded = await createPresentationFacetLoaders(presentationPlugins)[0]!.load(); + expect(loaded.facets.map(({ id }) => id)).toEqual(["@earendil-works/pi-example-plugin/tui"]); + await loaded.dispose(); + } finally { + await first.dispose(); + } + await expect.poll(() => pathExists(join(directory, `${serverId}.sock`)), { timeout: 5_000 }).toBe(false); + + const second = await openClientRuntime({ command: "client", ...sessionWorkerModel }); + try { + const activated = await activateBuiltinClientServices(second.servers[0]!); + const presentationPlugins = await activated.plugins.prepareSession( + { sessionId: "demo-1", packagePaths: null }, + BACKGROUND_CONTEXT, + ); + await activated.management.attach("demo-1", BACKGROUND_CONTEXT); + expect(createPresentationFacetLoaders(presentationPlugins)).toHaveLength(1); + } finally { + await second.dispose(); + } + }); + + test("retires a cold server after its only Session attachment disconnects", async () => { + const directory = await mkdtemp(join("/tmp", "pi-auto-session-")); + directories.add(directory); + const serverId = "00000000-0000-4000-8000-000000000001"; + vi.stubEnv("PI_SERVER_DIR", directory); + vi.stubEnv("PI_SERVER_ID", serverId); + + await expect(runClient({ command: "client", sessionId: "demo-1", ...sessionWorkerModel })).resolves.toEqual({ + kind: "attached", + serverId, + sessionId: "demo-1", + }); + await expect.poll(() => pathExists(join(directory, `${serverId}.sock`)), { timeout: 5_000 }).toBe(false); + await expect.poll(() => pathExists(join(directory, `control-${serverId}.sock`)), { timeout: 5_000 }).toBe(false); + }); + + test("runs and discovers multiple logical servers from one directory", async () => { + const directory = await mkdtemp(join("/tmp", "pi-multi-server-")); + directories.add(directory); + const firstId = "00000000-0000-4000-8000-000000000001"; + const secondId = "00000000-0000-4000-8000-000000000002"; + const [first, second] = await Promise.all([ + startServer({ directory, serverId: firstId }), + startServer({ directory, serverId: secondId }), + ]); + servers.add(first); + servers.add(second); + + expect((await lstat(join(directory, `control-${firstId}.sock`))).isSocket()).toBe(true); + expect((await lstat(join(directory, `control-${secondId}.sock`))).isSocket()).toBe(true); + await expect(runClient({ command: "client" }, { directory })).resolves.toEqual({ + kind: "list", + sessions: [ + { serverId: firstId, sessionId: "demo-1" }, + { serverId: firstId, sessionId: "demo-2" }, + { serverId: secondId, sessionId: "demo-1" }, + { serverId: secondId, sessionId: "demo-2" }, + ], + }); + + await first.close(); + await expect.poll(() => pathExists(first.socketPath)).toBe(false); + await expect(runClient({ command: "client" }, { directory })).resolves.toEqual({ + kind: "list", + sessions: [ + { serverId: secondId, sessionId: "demo-1" }, + { serverId: secondId, sessionId: "demo-2" }, + ], + }); + }); + + test("hydrates and mutates server Session services across framed clients", async () => { + const { runtime } = await makeServer(); + const firstClient = await Client.connect({ + serverId: runtime.serverId, + transportFactory: createUnixTransportFactory({ path: runtime.socketPath }), + }); + const secondClient = await Client.connect({ + serverId: runtime.serverId, + transportFactory: createUnixTransportFactory({ path: runtime.socketPath }), + }); + clients.add(firstClient); + clients.add(secondClient); + const errors: Error[] = []; + const firstServices = createServerServiceBinding(firstClient, { + services: [SessionDirectory, SessionManagement], + onError: (error) => errors.push(error), + }); + const secondServices = createServerServiceBinding(secondClient, { + services: [SessionDirectory, SessionManagement], + onError: (error) => errors.push(error), + }); + expect(firstServices.connection.value).toMatchObject({ status: "connected" }); + expect(secondServices.connection.value).toMatchObject({ status: "connected" }); + await expect(firstServices.catalogue(BACKGROUND_CONTEXT)).resolves.toEqual([ + { serviceId: SessionDirectory.id, mode: "singleton" }, + { serviceId: SessionManagement.id, mode: "singleton" }, + { serviceId: PresentationPlugins.id, mode: "singleton" }, + ]); + const firstDirectory = firstServices.use(SessionDirectory); + const secondDirectory = secondServices.use(SessionDirectory); + const firstManagement = firstServices.use(SessionManagement); + const secondManagement = secondServices.use(SessionManagement); + + await Promise.all([firstServices.ready(BACKGROUND_CONTEXT), secondServices.ready(BACKGROUND_CONTEXT)]); + expect(firstDirectory.state.value?.sessions.map(({ sessionId }) => sessionId)).toEqual(["demo-1", "demo-2"]); + expect(secondDirectory.state.value).toEqual(firstDirectory.state.value); + await firstManagement.create({ id: "demo-3" }, BACKGROUND_CONTEXT); + await vi.waitFor(() => { + expect(firstDirectory.state.value?.sessions.map(({ sessionId }) => sessionId)).toContain("demo-3"); + expect(secondDirectory.state.value).toEqual(firstDirectory.state.value); + }); + + await Promise.all([ + firstManagement.attach("demo-1", BACKGROUND_CONTEXT), + secondManagement.attach("demo-1", BACKGROUND_CONTEXT), + ]); + expect(firstClient.attachment?.sessionId).toBe("demo-1"); + expect(secondClient.attachment?.sessionId).toBe("demo-1"); + await firstManagement.remove("demo-1", BACKGROUND_CONTEXT); + await vi.waitFor(() => { + expect(firstClient.attachment).toBeUndefined(); + expect(secondClient.attachment).toBeUndefined(); + expect(firstDirectory.state.value?.sessions.map(({ sessionId }) => sessionId)).not.toContain("demo-1"); + expect(secondDirectory.state.value).toEqual(firstDirectory.state.value); + }); + await secondManagement.detach(BACKGROUND_CONTEXT); + expect(errors).toEqual([]); + + await Promise.all([firstServices.dispose(BACKGROUND_CONTEXT), secondServices.dispose(BACKGROUND_CONTEXT)]); + }); + + test("hydrates and updates the Models service across concurrent framed clients", async () => { + const { runtime } = await makeServer(); + const firstClient = await attachClient(runtime, "demo-1"); + const workerPid = runtime.workerPids.get("demo-1"); + expect(workerPid).toEqual(expect.any(Number)); + const secondClient = await attachClient(runtime, "demo-1"); + expect(runtime.workerPids.get("demo-1")).toBe(workerPid); + const errors: Error[] = []; + const firstServices = createSessionServiceBinding(firstClient, { + services: [Models], + onError: (error) => errors.push(error), + }); + const secondServices = createSessionServiceBinding(secondClient, { + services: [Models], + onError: (error) => errors.push(error), + }); + const firstModels = firstServices.use(Models); + const secondModels = secondServices.use(Models); + + await Promise.all([firstServices.ready(BACKGROUND_CONTEXT), secondServices.ready(BACKGROUND_CONTEXT)]); + expect(firstServices.attachment.value).toEqual({ status: "attached", sessionId: "demo-1" }); + expect(secondServices.attachment.value).toEqual({ status: "attached", sessionId: "demo-1" }); + expect(firstModels.state.value?.configuration.model).toEqual({ + provider: "anthropic", + modelId: "claude-sonnet-4-5", + }); + expect(secondModels.state.value).toEqual(firstModels.state.value); + const previousThinking = firstModels.state.value!.configuration.thinkingLevel; + await firstModels.cycleThinking(BACKGROUND_CONTEXT); + await vi.waitFor(() => { + expect(firstModels.state.value!.configuration.thinkingLevel).not.toBe(previousThinking); + expect(secondModels.state.value).toEqual(firstModels.state.value); + }); + expect(errors).toEqual([]); + + await Promise.all([firstServices.dispose(BACKGROUND_CONTEXT), secondServices.dispose(BACKGROUND_CONTEXT)]); + }); + + test("loads conventional Session facets from multiple configured plugin packages", async () => { + const directory = await mkdtemp(join("/tmp", "pes-plugin-")); + directories.add(directory); + const secondPackagePath = join(directory, "second-plugin"); + await mkdir(join(secondPackagePath, "src"), { recursive: true }); + await Promise.all([ + writeFile( + join(secondPackagePath, "package.json"), + `${JSON.stringify({ + name: "@earendil-works/second-session-plugin", + version: "1.0.0", + peerDependencies: { "@earendil-works/chord": "^0.84.4" }, + })}\n`, + ), + writeFile( + join(secondPackagePath, "src", "session.ts"), + 'import { defineFacet, defineService } from "@earendil-works/chord"; const Service = defineService("test.second-plugin"); export default defineFacet({ id: "second-session-plugin", setup(env) { env.provide(Service, { async read() { return "second"; } }); } });\n', + ), + ]); + const runtime = await startServer({ ...sessionWorkerModel, directory }); + servers.add(runtime); + const clientRuntime = await openClientRuntime({ command: "client" }, { directory }); + const activated = await activateBuiltinClientServices(clientRuntime.servers[0]!); + await activated.plugins.prepareSession( + { + sessionId: "demo-1", + packagePaths: [ + fileURLToPath(new URL("../examples/plugins/pi-example-plugin", import.meta.url)), + secondPackagePath, + ], + }, + BACKGROUND_CONTEXT, + ); + await activated.management.attach("demo-1", BACKGROUND_CONTEXT); + const services = createSessionServiceBinding(clientRuntime.servers[0]!.client, { + services: [ExampleFacetService, SecondPluginService], + }); + try { + await services.ready(BACKGROUND_CONTEXT); + await expect( + services.use(ExampleFacetService).greet({ name: "Armin" }, BACKGROUND_CONTEXT), + ).resolves.toMatchObject({ workerActivations: 1 }); + await expect(services.use(SecondPluginService).read(BACKGROUND_CONTEXT)).resolves.toBe("second"); + } finally { + await services.dispose(BACKGROUND_CONTEXT); + await clientRuntime.dispose(); + } + }); + + test("uses the most recently selected model for a new Session", async () => { + const directory = await mkdtemp(join("/tmp", "pes-model-default-")); + directories.add(directory); + const runtime = await startServer({ directory }); + servers.add(runtime); + const firstClient = await attachClient(runtime, "demo-1"); + const firstServices = createSessionServiceBinding(firstClient, { services: [Models] }); + const firstModels = firstServices.use(Models); + await firstServices.ready(BACKGROUND_CONTEXT); + await firstModels.select({ provider: "anthropic", modelId: "claude-opus-4-6" }, BACKGROUND_CONTEXT); + await firstServices.dispose(BACKGROUND_CONTEXT); + await firstClient.dispose(); + clients.delete(firstClient); + await expect.poll(() => runtime.workerPids.has("demo-1")).toBe(false); + + const secondClient = await attachClient(runtime, "demo-2"); + const secondServices = createSessionServiceBinding(secondClient, { services: [Models] }); + const secondModels = secondServices.use(Models); + await secondServices.ready(BACKGROUND_CONTEXT); + expect(secondModels.state.value?.configuration.model).toEqual({ + provider: "anthropic", + modelId: "claude-opus-4-6", + }); + await secondServices.dispose(BACKGROUND_CONTEXT); + }); + + test("composes management attachment with Session service hydration", async () => { + const { runtime } = await makeServer(); + const clientRuntime = await openClientRuntime({ + command: "client", + connect: { transport: "unix", path: runtime.socketPath }, + }); + try { + const server = await activateBuiltinClientServices(clientRuntime.servers[0]!); + await server.plugins.prepareSession({ sessionId: "demo-1", packagePaths: null }, BACKGROUND_CONTEXT); + await server.management.attach("demo-1", BACKGROUND_CONTEXT); + + expect(server.session.attachment.value).toEqual({ status: "attached", sessionId: "demo-1" }); + expect(server.models.state.value?.configuration.model).toEqual({ + provider: "anthropic", + modelId: "claude-sonnet-4-5", + }); + } finally { + await clientRuntime.dispose(); + } + }); + + test("fences superseded attachment hydration by attachment generation", async ({ onTestFinished }) => { + const { runtime } = await makeServer(); + const client = await Client.connect({ + serverId: runtime.serverId, + transportFactory: createUnixTransportFactory({ path: runtime.socketPath }), + }); + clients.add(client); + const errors: Error[] = []; + const services = createSessionServiceBinding(client, { + services: [Models], + onError: (error) => errors.push(error), + }); + const models = services.use(Models); + await services.ready(BACKGROUND_CONTEXT); + const states: SessionAttachmentState[] = []; + const removeStateListener = services.attachment.subscribe((state) => states.push(state)); + let releaseDelay!: () => void; + const delayed = new Promise((resolve) => { + releaseDelay = resolve; + }); + const subscribeService = client.subscribeService.bind(client); + const subscribe = vi + .spyOn(client, "subscribeService") + .mockImplementation(async (target, serviceId, mode, listener, signal) => { + if ("sessionId" in target && target.sessionId === "demo-2" && serviceId === Models.id) { + await delayed; + } + return subscribeService(target, serviceId, mode, listener, signal); + }); + onTestFinished(() => subscribe.mockRestore()); + + try { + await attachSession(client, "demo-2"); + expect(services.attachment.value).toEqual({ status: "attaching", sessionId: "demo-2" }); + await attachSession(client, "demo-1"); + await services.whenAttached("demo-1", BACKGROUND_CONTEXT); + expect(services.attachment.value).toEqual({ status: "attached", sessionId: "demo-1" }); + expect(models.state.value?.configuration.model).toEqual({ + provider: "anthropic", + modelId: "claude-sonnet-4-5", + }); + } finally { + releaseDelay(); + removeStateListener(); + await services.dispose(BACKGROUND_CONTEXT); + } + + const latestAttach = states + .map((state) => state.status === "attaching" && state.sessionId === "demo-1") + .lastIndexOf(true); + expect(latestAttach).toBeGreaterThanOrEqual(0); + expect(states.slice(latestAttach)).not.toContainEqual({ status: "attached", sessionId: "demo-2" }); + expect(states.slice(latestAttach)).not.toContainEqual({ status: "degraded", sessionId: "demo-2" }); + expect(errors).toEqual([]); + }); + + test("observes keyed service instances and fences replacement generations over framed transport", async ({ + onTestFinished, + }) => { + const spawn = vi + .spyOn(processRuntime, "spawnInternalProcess") + .mockImplementation((role, args, options) => + realSpawnInternalProcess( + role, + args, + role === "session-worker" ? { ...options, entryUrl: fauxWorkerEntryUrl } : options, + ), + ); + onTestFinished(() => spawn.mockRestore()); + const { runtime } = await makeServer(); + const client = await attachClient(runtime, "demo-1"); + const errors: Error[] = []; + const services = createSessionServiceSource(client, { + onError: (error) => errors.push(error), + }); + await expect(services.catalogue(BACKGROUND_CONTEXT)).resolves.toContainEqual({ + serviceId: KeyedProbe.id, + mode: "keyed", + }); + const observed: { service: KeyedProbe; value: string | undefined }[] = []; + const consumer = defineFacet({ + id: "@test/keyed-probe-consumer", + setup(env) { + env.observe(KeyedProbe, (service) => { + observed.push({ service, value: service.state.value?.value }); + }); + }, + }); + const facetHost = await createFacetHost({ facets: [consumer], serviceSources: [services] }); + + expect(services.attachment.value).toEqual({ status: "attached", sessionId: "demo-1" }); + await vi.waitFor(() => expect(observed).toHaveLength(1)); + expect(observed[0]!.value).toBe("first"); + const staleReplace = observed[0]!.service.replace; + await expect(staleReplace("second", BACKGROUND_CONTEXT)).resolves.toBeUndefined(); + await vi.waitFor(() => expect(observed).toHaveLength(2)); + expect(observed[1]!.value).toBe("second"); + expect(() => staleReplace("late", BACKGROUND_CONTEXT)).toThrow("observation is closed"); + + const replacedReplace = observed[1]!.service.replace; + await expect(attachSession(client, "demo-2")).resolves.toBeUndefined(); + await services.whenAttached("demo-2", BACKGROUND_CONTEXT); + expect(observed).toHaveLength(3); + expect(services.attachment.value).toEqual({ status: "attached", sessionId: "demo-2" }); + expect(observed[2]!.value).toBe("first"); + expect(() => replacedReplace("late", BACKGROUND_CONTEXT)).toThrow("observation is closed"); + expect(errors).toEqual([]); + + await facetHost.dispose(); + await expect(services.dispose(BACKGROUND_CONTEXT)).resolves.toBeUndefined(); + }); + + test("streams prompt events through the worker-owned service provider", async ({ onTestFinished }) => { + const spawn = vi + .spyOn(processRuntime, "spawnInternalProcess") + .mockImplementation((role, args, options) => + realSpawnInternalProcess( + role, + args, + role === "session-worker" ? { ...options, entryUrl: fauxWorkerEntryUrl } : options, + ), + ); + onTestFinished(() => spawn.mockRestore()); + const { directory } = await makeServer(); + const eventTypes: string[] = []; + + const result = await runClient( + { command: "client", sessionId: "demo-1", prompt: "question" }, + { + directory, + onEvent(event) { + eventTypes.push(event.type); + }, + }, + ); + + expect(result).toMatchObject({ kind: "prompted", text: "deterministic remote answer" }); + expect(eventTypes).toEqual( + expect.arrayContaining([ + "run_start", + "message_start", + "message_update", + "message_end", + "entry_added", + "run_end", + ]), + ); + }); + + test("replicates terminal operation state after consecutive prompts", async ({ onTestFinished }) => { + const spawn = vi + .spyOn(processRuntime, "spawnInternalProcess") + .mockImplementation((role, args, options) => + realSpawnInternalProcess( + role, + args, + role === "session-worker" ? { ...options, entryUrl: fauxWorkerEntryUrl } : options, + ), + ); + onTestFinished(() => spawn.mockRestore()); + const { runtime } = await makeServer(); + const client = await attachClient(runtime, "demo-1"); + const services = createSessionServiceBinding(client, { services: [AgentController, SessionPlugins, Transcript] }); + const controller = services.use(AgentController); + const transcript = services.use(Transcript); + await services.ready(BACKGROUND_CONTEXT); + await services.use(SessionPlugins).reload(BACKGROUND_CONTEXT); + try { + for (const message of ["first question", "second question"]) { + const response = await controller.prompt({ message, images: null }, BACKGROUND_CONTEXT); + expect(response).toMatchObject({ accepted: true, operationId: expect.any(String) }); + await vi.waitFor(() => { + expect(transcript.state.value?.snapshot).toMatchObject({ + operation: null, + lastResult: { operationId: response.operationId }, + }); + }); + } + } finally { + await services.dispose(BACKGROUND_CONTEXT); + } + }); + + test("stops an idle Session worker after its client disconnects", async () => { + const { runtime } = await makeServer(); + const client = await attachClient(runtime, "demo-1"); + const pid = runtime.workerPids.get("demo-1"); + expect(pid).toEqual(expect.any(Number)); + + await client.dispose(); + clients.delete(client); + await expect.poll(() => runtime.workerPids.has("demo-1")).toBe(false); + expect(processExists(pid!)).toBe(false); + }); + + test("starts one process per attached session and stops them during shutdown", async () => { + const { runtime } = await makeServer(); + await Promise.all([attachClient(runtime, "demo-1"), attachClient(runtime, "demo-2")]); + + const pids = [...runtime.workerPids.values()]; + expect(pids).toHaveLength(2); + expect(new Set(pids).size).toBe(2); + for (const pid of pids) expect(processExists(pid)).toBe(true); + + await runtime.close(); + expect(runtime.workerPids.size).toBe(0); + await Promise.all(pids.map((pid) => expect.poll(() => processExists(pid)).toBe(false))); + }); + + test("server runtime replaces an exited worker on the next attach", async () => { + const directory = await mkdtemp(join("/tmp", "pew-")); + directories.add(directory); + const runtime = await startServer({ ...sessionWorkerModel, directory }); + servers.add(runtime); + const client = await attachClient(runtime, "demo-1"); + const firstPid = runtime.workerPids.get("demo-1"); + expect(firstPid).toEqual(expect.any(Number)); + + process.kill(firstPid!, "SIGKILL"); + await expect.poll(() => runtime.workerPids.has("demo-1")).toBe(false); + await attachSession(client, "demo-1"); + const replacementPid = runtime.workerPids.get("demo-1"); + expect(replacementPid).toEqual(expect.any(Number)); + expect(replacementPid).not.toBe(firstPid); + }); + + test("discovers workers after replacing the server", async () => { + const firstDirectory = await mkdtemp(join("/tmp", "per-")); + directories.add(firstDirectory); + const first = await startServer({ ...sessionWorkerModel, directory: firstDirectory }); + servers.add(first); + await attachClient(first, "demo-1"); + const firstWorkerPid = first.workerPids.get("demo-1"); + expect(firstWorkerPid).toEqual(expect.any(Number)); + + const replacement = await startServer({ ...sessionWorkerModel, directory: firstDirectory }); + servers.add(replacement); + await first.closed; + + expect(replacement.serverId).toBe(first.serverId); + expect(replacement.workerPids.get("demo-1")).toBe(firstWorkerPid); + await expect.poll(() => first.workerPids.size).toBe(0); + expect(processExists(firstWorkerPid!)).toBe(true); + + await expect(runClient({ command: "client" }, { directory: firstDirectory })).resolves.toMatchObject({ + kind: "list", + sessions: [ + { serverId: first.serverId, sessionId: "demo-1" }, + { serverId: first.serverId, sessionId: "demo-2" }, + ], + }); + await attachClient(replacement, "demo-1"); + expect(replacement.workerPids.get("demo-1")).toBe(firstWorkerPid); + await attachClient(replacement, "demo-2"); + expect(replacement.workerPids.get("demo-2")).toEqual(expect.any(Number)); + expect(replacement.workerPids.get("demo-2")).not.toBe(firstWorkerPid); + }); + + test("retires an unclaimed idle worker after replacement demand expires", async () => { + const directory = await mkdtemp(join("/tmp", "pi-orphan-worker-")); + directories.add(directory); + vi.stubEnv("__PI_SESSION_WORKER_ORPHAN_DEMAND_GRACE_MS", "50"); + const first = await startServer({ ...sessionWorkerModel, directory }); + servers.add(first); + await attachClient(first, "demo-1"); + const workerPid = first.workerPids.get("demo-1"); + expect(workerPid).toEqual(expect.any(Number)); + + const replacement = await startServer({ ...sessionWorkerModel, directory }); + servers.add(replacement); + await first.closed; + expect(replacement.workerPids.get("demo-1")).toBe(workerPid); + + await expect.poll(() => replacement.workerPids.has("demo-1"), { timeout: 5_000 }).toBe(false); + expect(processExists(workerPid!)).toBe(false); + }); + + test("restores tracked sessions that are outside the replacement catalog", async () => { + const directory = await mkdtemp(join("/tmp", "pet-")); + const emptySessionDir = await mkdtemp(join("/tmp", "pet-sessions-")); + directories.add(directory); + directories.add(emptySessionDir); + const first = await startServer({ ...sessionWorkerModel, directory }); + servers.add(first); + await attachClient(first, "demo-1"); + const workerPid = first.workerPids.get("demo-1"); + + const replacement = await startServer({ + ...sessionWorkerModel, + directory, + sessionDir: emptySessionDir, + }); + servers.add(replacement); + await first.closed; + + await expect(runClient({ command: "client" }, { directory })).resolves.toEqual({ + kind: "list", + sessions: [{ serverId: first.serverId, sessionId: "demo-1" }], + }); + await attachClient(replacement, "demo-1"); + expect(replacement.workerPids.get("demo-1")).toBe(workerPid); + }); + + test("reports missing and ambiguous session selections", async () => { + const sharedDirectory = await mkdtemp(join("/tmp", "ped-")); + directories.add(sharedDirectory); + const firstShared = await startServer({ + ...sessionWorkerModel, + directory: sharedDirectory, + serverId: "00000000-0000-4000-8000-000000000001", + }); + const secondShared = await startServer({ + ...sessionWorkerModel, + directory: sharedDirectory, + serverId: "00000000-0000-4000-8000-000000000002", + }); + servers.add(firstShared); + servers.add(secondShared); + + await expect( + runClient({ command: "client", sessionId: "missing" }, { directory: sharedDirectory }), + ).rejects.toThrow("No discovered server contains session missing"); + await expect( + runClient({ command: "client", sessionId: "demo-1" }, { directory: sharedDirectory }), + ).rejects.toThrow("Session demo-1 is available from more than one server"); + }); + test("rejects a duplicate session ID within one durable repository", async () => { + await createExperimentalSessions( + join(agentDir, "experimental", "sessions"), + ["demo-1"], + join(agentDir, "other-cwd"), + ); + const { directory } = await makeServer(); + + await expect(runClient({ command: "client", sessionId: "demo-1" }, { directory })).rejects.toMatchObject({ + code: "session_ambiguous", + }); + }); +}); + +async function pathExists(path: string): Promise { + try { + await lstat(path); + return true; + } catch (error) { + if ((error as NodeJS.ErrnoException).code === "ENOENT") return false; + throw error; + } +} + +function processExists(pid: number): boolean { + try { + process.kill(pid, 0); + return true; + } catch (error) { + return (error as NodeJS.ErrnoException).code === "EPERM"; + } +} diff --git a/packages/coding-agent/test/experimental-server-lifecycle.test.ts b/packages/coding-agent/test/experimental-server-lifecycle.test.ts new file mode 100644 index 0000000000000000000000000000000000000000..b4eadc4616caf078e874df056989a1bd4aae81bd --- /dev/null +++ b/packages/coding-agent/test/experimental-server-lifecycle.test.ts @@ -0,0 +1,49 @@ +import { afterEach, describe, expect, test, vi } from "vitest"; +import { ServerLifetime } from "../src/experimental/server.ts"; + +afterEach(() => { + vi.useRealTimers(); +}); + +describe("server lifecycle", () => { + test("holds a foreground server until explicit shutdown", () => { + vi.useFakeTimers(); + const retire = vi.fn(); + const lifetime = new ServerLifetime(true); + lifetime.start(retire); + vi.advanceTimersByTime(60_000); + expect(retire).not.toHaveBeenCalled(); + lifetime.stop(); + }); + + test("retires an automatic server if no first client arrives", () => { + vi.useFakeTimers(); + const retire = vi.fn(); + const lifetime = new ServerLifetime(false); + lifetime.start(retire); + vi.advanceTimersByTime(10_999); + expect(retire).not.toHaveBeenCalled(); + vi.advanceTimersByTime(1); + expect(retire).toHaveBeenCalledOnce(); + lifetime.stop(); + }); + + test("requires both client and worker demand to disappear", () => { + vi.useFakeTimers(); + const retire = vi.fn(); + const lifetime = new ServerLifetime(false); + lifetime.start(retire); + lifetime.setConnectionCount(1); + lifetime.setWorkerCount(1); + lifetime.setConnectionCount(0); + vi.advanceTimersByTime(10_000); + expect(retire).not.toHaveBeenCalled(); + + lifetime.setWorkerCount(0); + vi.advanceTimersByTime(999); + expect(retire).not.toHaveBeenCalled(); + vi.advanceTimersByTime(1); + expect(retire).toHaveBeenCalledOnce(); + lifetime.stop(); + }); +}); diff --git a/packages/coding-agent/test/experimental-server-profile.test.ts b/packages/coding-agent/test/experimental-server-profile.test.ts new file mode 100644 index 0000000000000000000000000000000000000000..ce53103ef9419f882a0b435633418359deecff81 --- /dev/null +++ b/packages/coding-agent/test/experimental-server-profile.test.ts @@ -0,0 +1,81 @@ +import { mkdtemp, readdir, readFile, rm, writeFile } from "node:fs/promises"; +import { tmpdir } from "node:os"; +import { join } from "node:path"; +import { afterEach, describe, expect, test } from "vitest"; +import { acquireServerProfile } from "../src/experimental/server.ts"; + +const directories = new Set(); +const FIRST_SERVER_ID = "00000000-0000-4000-8000-000000000001"; +const SECOND_SERVER_ID = "00000000-0000-4000-8000-000000000002"; + +async function makeDirectory(): Promise { + const directory = await mkdtemp(join(tmpdir(), "psp-")); + directories.add(directory); + return directory; +} + +afterEach(async () => { + await Promise.all([...directories].map((directory) => rm(directory, { recursive: true, force: true }))); + directories.clear(); +}); + +describe("experimental server profile", () => { + test("serializes launchers and preserves the server identity", async () => { + const directory = await makeDirectory(); + const first = await acquireServerProfile(directory); + let secondAcquired = false; + const pendingSecond = acquireServerProfile(directory).then((profile) => { + secondAcquired = true; + return profile; + }); + + await new Promise((resolve) => setImmediate(resolve)); + expect(secondAcquired).toBe(false); + await first.release(); + + const second = await pendingSecond; + expect(secondAcquired).toBe(true); + expect(second.serverId).toBe(first.serverId); + expect((await readFile(join(directory, "default-server-id"), "utf8")).trim()).toBe(first.serverId); + await second.release(); + }); + + test("does not serialize different server IDs in one directory", async () => { + const directory = await makeDirectory(); + const [first, second] = await Promise.all([ + acquireServerProfile(directory, FIRST_SERVER_ID), + acquireServerProfile(directory, SECOND_SERVER_ID), + ]); + expect(first.serverId).toBe(FIRST_SERVER_ID); + expect(second.serverId).toBe(SECOND_SERVER_ID); + expect((await readdir(directory)).sort()).toEqual([ + `launcher-${FIRST_SERVER_ID}.lock`, + `launcher-${SECOND_SERVER_ID}.lock`, + ]); + await Promise.all([first.release(), second.release()]); + }); + + test("does not share the default identity across server directories", async () => { + const firstDirectory = await makeDirectory(); + const secondDirectory = await makeDirectory(); + + const [first, second] = await Promise.all([ + acquireServerProfile(firstDirectory), + acquireServerProfile(secondDirectory), + ]); + expect(first.serverId).not.toBe(second.serverId); + await Promise.all([first.release(), second.release()]); + }); + + test("rejects a corrupt default identity", async () => { + const directory = await makeDirectory(); + await writeFile(join(directory, "default-server-id"), "invalid\n"); + + await expect(acquireServerProfile(directory)).rejects.toThrow(/Invalid default experimental server identity/); + }); + + test("rejects an invalid explicit server ID", async () => { + const directory = await makeDirectory(); + await expect(acquireServerProfile(directory, "invalid")).rejects.toThrow("Invalid experimental server ID"); + }); +}); diff --git a/packages/coding-agent/test/experimental-service-binding.ts b/packages/coding-agent/test/experimental-service-binding.ts new file mode 100644 index 0000000000000000000000000000000000000000..ecd725479c8106ea771d48f367dcdf74edd9d457 --- /dev/null +++ b/packages/coding-agent/test/experimental-service-binding.ts @@ -0,0 +1,90 @@ +import type { Context, RemoteServices } from "@earendil-works/chord"; +import { BACKGROUND_CONTEXT } from "@earendil-works/chord/context"; +import type { Client } from "@earendil-works/pi-client"; +import { + createServerServiceSource, + createSessionServiceSource, + type ServerServiceSource, + type ServiceSourceOptions, + type SessionServiceSource, +} from "../src/experimental/services/connection.ts"; + +interface ServiceBindingOptions extends ServiceSourceOptions { + readonly services: readonly { readonly id: string }[]; +} + +/** Test helper for binding server services without a facet host. */ +export function createServerServiceBinding( + client: Client, + options: ServiceBindingOptions, +): ServerServiceSource & RemoteServices { + const source = createServerServiceSource(client, options); + const services = source.open({ + services: options.services, + assertAccess() {}, + onError: options.onError ?? (() => {}), + }); + const activation = services.ready(BACKGROUND_CONTEXT); + void activation.catch(options.onError ?? (() => {})); + const ready = async (context: Context): Promise => { + await activation; + await services.ready(context); + }; + return { + acceptsUnavailableServices: source.acceptsUnavailableServices, + connection: source.connection, + catalogue: (context) => source.catalogue(context), + open: (openOptions) => source.open(openOptions), + use: (service) => services.use(service), + observe: (service, handler) => services.observe(service, handler), + ready, + async dispose(context) { + const results = await Promise.allSettled([services.dispose(context), source.dispose(context)]); + throwFailures(results, "Failed to dispose server service binding"); + }, + }; +} + +/** Test helper for binding selected-Session services without a facet host. */ +export function createSessionServiceBinding( + client: Client, + options: ServiceBindingOptions, +): SessionServiceSource & RemoteServices { + const source = createSessionServiceSource(client, options); + const services = source.open({ + services: options.services, + assertAccess() {}, + onError: options.onError ?? (() => {}), + }); + const activation = services.ready(BACKGROUND_CONTEXT); + void activation.catch(options.onError ?? (() => {})); + const ready = async (context: Context): Promise => { + await activation; + const attachment = source.attachment.value; + if (attachment?.status === "detached") await source.whenDetached(context); + else if (attachment !== undefined) await source.whenAttached(attachment.sessionId, context); + }; + return { + get acceptsUnavailableServices() { + return source.acceptsUnavailableServices; + }, + attachment: source.attachment, + catalogue: (context) => source.catalogue(context), + open: (openOptions) => source.open(openOptions), + use: (service) => services.use(service), + observe: (service, handler) => services.observe(service, handler), + ready, + whenAttached: (sessionId, context) => source.whenAttached(sessionId, context), + whenDetached: (context) => source.whenDetached(context), + async dispose(context) { + const results = await Promise.allSettled([services.dispose(context), source.dispose(context)]); + throwFailures(results, "Failed to dispose Session service binding"); + }, + }; +} + +function throwFailures(results: readonly PromiseSettledResult[], message: string): void { + const errors = results.flatMap((result) => (result.status === "rejected" ? [result.reason] : [])); + if (errors.length === 1) throw errors[0]; + if (errors.length > 1) throw new AggregateError(errors, message); +} diff --git a/packages/coding-agent/test/experimental-session-directory.test.ts b/packages/coding-agent/test/experimental-session-directory.test.ts new file mode 100644 index 0000000000000000000000000000000000000000..4160326799f31119f09e6c5864fba56350918065 --- /dev/null +++ b/packages/coding-agent/test/experimental-session-directory.test.ts @@ -0,0 +1,24 @@ +import { homedir } from "node:os"; +import { resolve } from "node:path"; +import { afterEach, describe, expect, test, vi } from "vitest"; +import { resolveSessionDirectory } from "../src/experimental/server.ts"; + +afterEach(() => vi.unstubAllEnvs()); + +describe("experimental server session directory", () => { + test("uses the experimental directory under the configured agent directory by default", () => { + vi.stubEnv("PI_CODING_AGENT_DIR", "/tmp/pi-agent-config"); + + expect(resolveSessionDirectory()).toBe("/tmp/pi-agent-config/experimental/sessions"); + }); + + test("resolves an explicit relative directory from the current working directory", () => { + vi.stubEnv("PI_CODING_AGENT_DIR", "/tmp/pi-agent-config"); + + expect(resolveSessionDirectory("relative/sessions")).toBe(resolve("relative/sessions")); + }); + + test("expands a tilde in an explicit directory", () => { + expect(resolveSessionDirectory("~/custom-sessions")).toBe(resolve(homedir(), "custom-sessions")); + }); +}); diff --git a/packages/coding-agent/test/experimental-session-support.ts b/packages/coding-agent/test/experimental-session-support.ts new file mode 100644 index 0000000000000000000000000000000000000000..202865e6061fb7d38ba03091090864f2d341da15 --- /dev/null +++ b/packages/coding-agent/test/experimental-session-support.ts @@ -0,0 +1,67 @@ +import { mkdir, writeFile } from "node:fs/promises"; +import { join } from "node:path"; +import { + BACKGROUND_CONTEXT, + type Entry, + type JsonlSessionMetadata, + JsonlSessionRepo, + laneConfig, +} from "@earendil-works/pi-agent-core"; +import { NodeExecutionEnv } from "@earendil-works/pi-agent-core/node"; + +export async function createExperimentalSessions( + sessionsRoot: string, + ids: readonly string[], + cwd = process.cwd(), +): Promise { + const fileSystem = new NodeExecutionEnv({ cwd }); + const repo = new JsonlSessionRepo({ fileSystem, sessionsRoot }); + const metadata: JsonlSessionMetadata[] = []; + try { + for (const id of ids) { + const session = await repo.create({ id, cwd }, BACKGROUND_CONTEXT); + metadata.push(session.metadata); + await session.close(BACKGROUND_CONTEXT); + } + return metadata; + } finally { + await repo.close(BACKGROUND_CONTEXT); + await fileSystem.cleanup(BACKGROUND_CONTEXT); + } +} + +export async function configureExperimentalWorkerModel(agentDir: string): Promise { + await mkdir(agentDir, { recursive: true }); + await writeFile(join(agentDir, "auth.json"), JSON.stringify({ anthropic: { type: "api_key", key: "test-key" } }), { + mode: 0o600, + }); +} + +export async function readExperimentalSessionState( + sessionsRoot: string, + sessionId: string, +): Promise<{ + branch: Entry[]; + model: { provider: string; modelId: string } | undefined; + activeTools: string[]; +}> { + const fileSystem = new NodeExecutionEnv({ cwd: process.cwd() }); + const repo = new JsonlSessionRepo({ fileSystem, sessionsRoot }); + let session: Awaited> | undefined; + try { + const matches = (await repo.list(undefined, BACKGROUND_CONTEXT)).filter((metadata) => metadata.id === sessionId); + if (matches.length !== 1) throw new Error(`Expected one Session ${sessionId}, found ${matches.length}`); + session = await repo.open(matches[0]!, BACKGROUND_CONTEXT); + const main = await session.branch("main", BACKGROUND_CONTEXT); + if (main === undefined) throw new Error("Expected Session main Branch"); + const [branch, configuration] = await Promise.all([ + main.findEntries({ order: "oldestFirst" }, BACKGROUND_CONTEXT), + session.getValue(laneConfig("main"), BACKGROUND_CONTEXT), + ]); + return { branch, model: configuration?.value.model, activeTools: configuration?.value.activeToolNames ?? [] }; + } finally { + await session?.close(BACKGROUND_CONTEXT); + await repo.close(BACKGROUND_CONTEXT); + await fileSystem.cleanup(BACKGROUND_CONTEXT); + } +} diff --git a/packages/coding-agent/test/experimental-session-worker-lifecycle.test.ts b/packages/coding-agent/test/experimental-session-worker-lifecycle.test.ts new file mode 100644 index 0000000000000000000000000000000000000000..53a00e45d331f790b49c3a68b6fb8a7b5dfe1dc8 --- /dev/null +++ b/packages/coding-agent/test/experimental-session-worker-lifecycle.test.ts @@ -0,0 +1,162 @@ +import { afterEach, describe, expect, test, vi } from "vitest"; +import { WorkerLifecycle } from "../src/experimental/session-worker.ts"; + +const GENERATION = "generation-1"; + +function createLifecycle(options: { initialDemandGraceMs?: number; orphanDemandGraceMs?: number } = {}) { + const retire = vi.fn(); + const lifecycle = new WorkerLifecycle({ + initialServerConnectionId: GENERATION, + initialDemandGraceMs: options.initialDemandGraceMs ?? 100, + orphanDemandGraceMs: options.orphanDemandGraceMs ?? 200, + onRetire: retire, + }); + return { lifecycle, retire }; +} + +afterEach(() => { + vi.useRealTimers(); +}); + +describe("Session worker lifecycle", () => { + test("retires only after client demand and Harness activity are both gone", async () => { + vi.useFakeTimers(); + const { lifecycle, retire } = createLifecycle(); + lifecycle.setDemand(GENERATION, "attachment-1", true); + lifecycle.operationStarted("run", "main", "operation-1"); + lifecycle.setDemand(GENERATION, "attachment-1", false); + expect(retire).not.toHaveBeenCalled(); + + lifecycle.operationStopped("run", "main", "operation-1"); + await vi.runAllTicks(); + expect(retire).toHaveBeenCalledOnce(); + lifecycle.close(); + }); + + test("retains the worker until every presentation attachment is released", () => { + vi.useFakeTimers(); + const { lifecycle, retire } = createLifecycle(); + lifecycle.setDemand(GENERATION, "attachment-1", true); + lifecycle.setDemand(GENERATION, "attachment-2", true); + lifecycle.setDemand(GENERATION, "attachment-1", false); + expect(retire).not.toHaveBeenCalled(); + lifecycle.setDemand(GENERATION, "attachment-2", false); + expect(retire).toHaveBeenCalledOnce(); + lifecycle.close(); + }); + + test("tracks a nested compaction independently from its enclosing run", () => { + vi.useFakeTimers(); + const { lifecycle, retire } = createLifecycle(); + lifecycle.setDemand(GENERATION, "attachment-1", true); + lifecycle.operationStarted("run", "main", "operation-1"); + lifecycle.operationStarted("compaction", "main", "operation-1"); + lifecycle.setDemand(GENERATION, "attachment-1", false); + + lifecycle.operationStopped("compaction", "main", "operation-1"); + expect(retire).not.toHaveBeenCalled(); + lifecycle.operationStopped("run", "main", "operation-1"); + expect(retire).toHaveBeenCalledOnce(); + lifecycle.close(); + }); + + test.each(["compaction", "navigation"] as const)("clears suspended %s activity", (kind) => { + vi.useFakeTimers(); + const { lifecycle, retire } = createLifecycle(); + lifecycle.setDemand(GENERATION, "attachment-1", true); + lifecycle.operationStarted(kind, "main", "operation-1"); + lifecycle.setDemand(GENERATION, "attachment-1", false); + + lifecycle.operationStopped(kind, "main", "operation-1"); + expect(retire).toHaveBeenCalledOnce(); + lifecycle.close(); + }); + + test("does not retire while a demand acknowledgement holds reconciliation", () => { + vi.useFakeTimers(); + const { lifecycle, retire } = createLifecycle(); + lifecycle.setDemand(GENERATION, "attachment-1", true); + const release = lifecycle.holdRetirement(); + lifecycle.setDemand(GENERATION, "attachment-1", false); + expect(retire).not.toHaveBeenCalled(); + release(); + expect(retire).toHaveBeenCalledOnce(); + lifecycle.close(); + }); + + test("holds retirement only for requests from the active attachment", () => { + vi.useFakeTimers(); + const { lifecycle, retire } = createLifecycle(); + lifecycle.setDemand(GENERATION, "attachment-1", true); + const release = lifecycle.beginRequest(GENERATION, "attachment-1"); + lifecycle.setDemand(GENERATION, "attachment-1", false); + expect(retire).not.toHaveBeenCalled(); + release(); + expect(retire).toHaveBeenCalledOnce(); + expect(() => lifecycle.beginRequest(GENERATION, "attachment-1")).toThrow(/retiring/); + lifecycle.close(); + }); + + test("rejects requests from stale generations and attachments", () => { + vi.useFakeTimers(); + const { lifecycle } = createLifecycle(); + lifecycle.setDemand(GENERATION, "attachment-1", true); + expect(() => lifecycle.beginRequest("stale", "attachment-1")).toThrow(/stale server generation/); + expect(() => lifecycle.beginRequest(GENERATION, "wrong-attachment")).toThrow(/active attachment/); + lifecycle.close(); + }); + + test("retains disconnected-generation demand for the orphan grace", async () => { + vi.useFakeTimers(); + const { lifecycle, retire } = createLifecycle(); + lifecycle.setDemand(GENERATION, "attachment-1", true); + lifecycle.serverDisconnected(GENERATION); + + vi.advanceTimersByTime(199); + expect(retire).not.toHaveBeenCalled(); + vi.advanceTimersByTime(1); + await vi.runAllTicks(); + expect(retire).toHaveBeenCalledOnce(); + lifecycle.close(); + }); + + test("allows a replacement generation to retain the worker", async () => { + vi.useFakeTimers(); + const { lifecycle, retire } = createLifecycle(); + lifecycle.setDemand(GENERATION, "attachment-1", true); + lifecycle.serverDisconnected(GENERATION); + lifecycle.serverConnected("generation-2"); + lifecycle.setDemand("generation-2", "attachment-2", true); + + vi.advanceTimersByTime(200); + expect(retire).not.toHaveBeenCalled(); + lifecycle.setDemand("generation-2", "attachment-2", false); + await vi.runAllTicks(); + expect(retire).toHaveBeenCalledOnce(); + lifecycle.close(); + }); + + test("retires a launched worker that never receives initial demand", async () => { + vi.useFakeTimers(); + const { lifecycle, retire } = createLifecycle(); + vi.advanceTimersByTime(100); + await vi.runAllTicks(); + expect(retire).toHaveBeenCalledOnce(); + lifecycle.close(); + }); + + test("rejects demand after retirement has won the race", () => { + vi.useFakeTimers(); + const { lifecycle } = createLifecycle(); + vi.advanceTimersByTime(100); + expect(() => lifecycle.setDemand(GENERATION, "attachment-1", true)).toThrow(/retiring/); + lifecycle.close(); + }); + + test("rejects demand from a stale server generation", () => { + vi.useFakeTimers(); + const { lifecycle } = createLifecycle(); + expect(() => lifecycle.setDemand("stale", "attachment-1", true)).toThrow(/stale server generation/); + lifecycle.close(); + }); +}); diff --git a/packages/coding-agent/test/experimental-session-worker-manager.test.ts b/packages/coding-agent/test/experimental-session-worker-manager.test.ts new file mode 100644 index 0000000000000000000000000000000000000000..a4d2ffed6aba58f71fb240e047215b2a253381f5 --- /dev/null +++ b/packages/coding-agent/test/experimental-session-worker-manager.test.ts @@ -0,0 +1,309 @@ +import type { ServiceCall } from "@earendil-works/chord"; +import { BACKGROUND_CONTEXT, type JsonlSessionMetadata } from "@earendil-works/pi-agent-core"; +import { afterEach, describe, expect, test, vi } from "vitest"; +import type { CoordinatorConnectionEvent } from "../src/experimental/coordinator.ts"; +import { SessionWorkerManager } from "../src/experimental/session-worker-manager.ts"; + +const metadata: JsonlSessionMetadata = { + id: "session-1", + createdAt: 1, + storageVersion: 1, + cwd: "/tmp", + path: "/tmp/session-1.jsonl", + modifiedAt: 1, +}; + +class FakeCoordinator { + readonly controlPath = "/tmp/control.sock"; + readonly serverConnectionId = "server-generation-1"; + readonly wasReplaced = false; + readonly sent: { peerId: string; payload: unknown }[] = []; + readonly #listeners = new Set<(event: CoordinatorConnectionEvent) => void>(); + onSend?: (peerId: string, payload: Record) => void; + + onEvent(listener: (event: CoordinatorConnectionEvent) => void): () => void { + this.#listeners.add(listener); + return () => this.#listeners.delete(listener); + } + + async send(peerId: string, payload: unknown): Promise { + this.sent.push({ peerId, payload }); + this.onSend?.(peerId, asObject(payload)); + } + + async broadcast(payload: unknown): Promise { + if (asObject(payload).type !== "discover_workers") return; + this.emit({ + type: "message", + from: "worker-1", + payload: { + type: "worker_ready", + token: "worker-token", + sessionKey: metadata.path, + sessionId: metadata.id, + pid: 123, + metadata, + pluginManifestPaths: [], + }, + }); + } + + emit(event: CoordinatorConnectionEvent): void { + for (const listener of this.#listeners) listener(event); + } +} + +afterEach(() => { + vi.useRealTimers(); + vi.restoreAllMocks(); +}); + +async function createAttachedWorker(): Promise<{ + coordinator: FakeCoordinator; + workers: SessionWorkerManager; + handle: Awaited>; + attachment: Awaited>["attachClient"]>>; + release(): Promise; +}> { + const coordinator = new FakeCoordinator(); + const workers = new SessionWorkerManager(coordinator, "/tmp"); + await workers.discover(new Set(["worker-1"])); + const handle = await workers.openSession(metadata, BACKGROUND_CONTEXT, []); + coordinator.onSend = (peerId, payload) => { + if (payload.type !== "session_demand") return; + queueMicrotask(() => + coordinator.emit({ + type: "message", + from: peerId, + payload: { + type: "demand_applied", + token: "worker-token", + sessionKey: metadata.path, + requestId: payload.requestId, + attachmentId: payload.attachmentId, + attached: payload.attached, + }, + }), + ); + }; + const attachment = await handle.attachClient!(BACKGROUND_CONTEXT); + return { + coordinator, + workers, + handle, + attachment, + release: () => Promise.resolve(attachment.release(BACKGROUND_CONTEXT)), + }; +} + +describe("Session worker lifecycle failures", () => { + test("adopts a discovered worker with its existing Session plugin selection", async () => { + const coordinator = new FakeCoordinator(); + const workers = new SessionWorkerManager(coordinator, "/tmp"); + await workers.discover(new Set(["worker-1"])); + expect(coordinator.sent).not.toContainEqual({ peerId: "worker-1", payload: { type: "shutdown" } }); + expect(workers.workerPids.size).toBe(1); + expect(() => workers.assertSessionPluginManifestPaths(metadata, [])).not.toThrow(); + workers.detach(); + }); + + test("rejects a different plugin selection for an active Session without stopping it", async () => { + const { workers } = await createAttachedWorker(); + expect(() => workers.assertSessionPluginManifestPaths(metadata, ["/tmp/plugin/chord-facets.json"])).toThrow( + "active with a different plugin selection", + ); + expect(workers.workerPids.size).toBe(1); + workers.detach(); + }); + + test("compensates a timed-out attachment before rejecting it", async () => { + vi.useFakeTimers(); + const coordinator = new FakeCoordinator(); + const workers = new SessionWorkerManager(coordinator, "/tmp"); + await workers.discover(new Set(["worker-1"])); + const handle = await workers.openSession(metadata, BACKGROUND_CONTEXT, []); + const demands: { attachmentId: string; attached: boolean }[] = []; + coordinator.onSend = (peerId, payload) => { + if ( + payload.type !== "session_demand" || + typeof payload.attachmentId !== "string" || + typeof payload.attached !== "boolean" + ) { + return; + } + demands.push({ attachmentId: payload.attachmentId, attached: payload.attached }); + if (payload.attached) return; + queueMicrotask(() => + coordinator.emit({ + type: "message", + from: peerId, + payload: { + type: "demand_applied", + token: "worker-token", + sessionKey: metadata.path, + requestId: payload.requestId, + attachmentId: payload.attachmentId, + attached: false, + }, + }), + ); + }; + + const attaching = expect(handle.attachClient!(BACKGROUND_CONTEXT)).rejects.toThrow("timed out"); + await vi.advanceTimersByTimeAsync(5_000); + await attaching; + expect(demands).toHaveLength(2); + expect(demands[0]).toMatchObject({ attachmentId: expect.any(String), attached: true }); + expect(demands[1]).toEqual({ attachmentId: demands[0]!.attachmentId, attached: false }); + workers.detach(); + }); + + test("kills a worker when timed-out demand cannot be reconciled", async () => { + vi.useFakeTimers(); + const kill = vi.spyOn(process, "kill").mockReturnValue(true); + const coordinator = new FakeCoordinator(); + const workers = new SessionWorkerManager(coordinator, "/tmp"); + await workers.discover(new Set(["worker-1"])); + const handle = await workers.openSession(metadata, BACKGROUND_CONTEXT, []); + coordinator.onSend = () => {}; + + const attaching = expect(handle.attachClient!(BACKGROUND_CONTEXT)).rejects.toThrow("worker was terminated"); + await vi.advanceTimersByTimeAsync(5_000); + await vi.advanceTimersByTimeAsync(5_000); + await vi.advanceTimersByTimeAsync(10_000); + await attaching; + expect(kill).toHaveBeenCalledWith(123, "SIGKILL"); + expect(workers.workerPids.size).toBe(0); + workers.detach(); + }); + + test("bounds Harness-driven worker shutdown", async () => { + vi.useFakeTimers(); + const kill = vi.spyOn(process, "kill").mockReturnValue(true); + const { coordinator, workers, handle, release } = await createAttachedWorker(); + await release(); + coordinator.onSend = () => {}; + + const closing = handle.close(BACKGROUND_CONTEXT); + await vi.advanceTimersByTimeAsync(10_000); + await closing; + expect(kill).toHaveBeenCalledWith(123, "SIGKILL"); + expect(workers.workerPids.size).toBe(0); + workers.detach(); + }); +}); + +describe("Session worker operations", () => { + const serviceCall = { serviceId: "test.session", member: "run", args: ["Hello"] } satisfies ServiceCall; + + test("correlates service results to the worker generation and attachment", async () => { + const { coordinator, workers, attachment, release } = await createAttachedWorker(); + coordinator.onSend = (peerId, payload) => { + if (payload.type !== "operation") return; + const scope = asObject(payload.scope); + queueMicrotask(() => { + coordinator.emit({ + type: "message", + from: peerId, + payload: { + type: "operation_response", + token: "worker-token", + sessionKey: metadata.path, + response: { + type: "operation_result", + requestId: payload.requestId, + scope, + result: { accepted: true }, + }, + }, + }); + }); + }; + await expect(attachment.invokeService(serviceCall, () => {}, BACKGROUND_CONTEXT)).resolves.toEqual({ + accepted: true, + }); + const operation = coordinator.sent + .map(({ payload }) => asObject(payload)) + .find(({ type }) => type === "operation"); + expect(operation).toMatchObject({ + scope: { serverConnectionId: "server-generation-1", attachmentId: expect.any(String) }, + call: serviceCall, + }); + workers.detach(); + await release(); + }); + + test("rejects a correlated response with mismatched worker identity", async () => { + const { coordinator, workers, attachment, release } = await createAttachedWorker(); + coordinator.onSend = (peerId, payload) => { + if (payload.type !== "operation") return; + queueMicrotask(() => + coordinator.emit({ + type: "message", + from: peerId, + payload: { + type: "operation_response", + token: "wrong-token", + sessionKey: metadata.path, + response: { + type: "operation_result", + requestId: payload.requestId, + scope: payload.scope, + result: { accepted: true }, + }, + }, + }), + ); + }; + await expect(attachment.invokeService(serviceCall, () => {}, BACKGROUND_CONTEXT)).rejects.toThrow( + /mismatched operation response/, + ); + workers.detach(); + await release(); + }); + + test("rejects a null request scope", async () => { + const { coordinator, workers, attachment, release } = await createAttachedWorker(); + coordinator.onSend = (peerId, payload) => { + if (payload.type !== "operation") return; + queueMicrotask(() => + coordinator.emit({ + type: "message", + from: peerId, + payload: { + type: "operation_response", + token: "worker-token", + sessionKey: metadata.path, + response: { + type: "operation_result", + requestId: payload.requestId, + scope: null, + result: { accepted: true }, + }, + }, + }), + ); + }; + await expect(attachment.invokeService(serviceCall, () => {}, BACKGROUND_CONTEXT)).rejects.toThrow( + /invalid operation response/, + ); + workers.detach(); + await release(); + }); + + test("rejects pending service calls on replacement without stopping the worker", async () => { + const { coordinator, workers, attachment } = await createAttachedWorker(); + coordinator.onSend = () => {}; + const calling = attachment.invokeService(serviceCall, () => {}, BACKGROUND_CONTEXT); + workers.detach(); + + await expect(calling).rejects.toThrow(/replaced during a worker operation/); + expect(coordinator.sent.map(({ payload }) => asObject(payload).type)).not.toContain("shutdown"); + expect(workers.workerPids.size).toBe(0); + }); +}); + +function asObject(value: unknown): Record { + if (typeof value !== "object" || value === null || Array.isArray(value)) throw new TypeError("Expected object"); + return value as Record; +} diff --git a/packages/coding-agent/test/experimental-slash-commands.test.ts b/packages/coding-agent/test/experimental-slash-commands.test.ts new file mode 100644 index 0000000000000000000000000000000000000000..3fa75fd747fd8f090534884f5b6759268b3f34e4 --- /dev/null +++ b/packages/coding-agent/test/experimental-slash-commands.test.ts @@ -0,0 +1,96 @@ +import { createFacetHost, defineFacet } from "@earendil-works/chord"; +import { describe, expect, test, vi } from "vitest"; +import { SlashCommands } from "../src/experimental/services/slash-commands.ts"; +import { + createSlashCommandsRuntimeFacet, + SlashCommandRegistry, +} from "../src/experimental/services/slash-commands-provider.ts"; + +describe("experimental slash command facets", () => { + test("registers and removes contributions", () => { + const registry = new SlashCommandRegistry(); + const snapshots: string[][] = []; + const unsubscribe = registry.subscribe((commands) => snapshots.push(commands.map(({ name }) => name))); + const close = registry.register({ name: "hello", description: "Hello", run: () => undefined }); + expect(registry.list().map(({ name }) => name)).toEqual(["hello"]); + expect(() => registry.register({ name: "hello", run: () => undefined })).toThrow("already registered"); + close(); + close(); + expect(registry.list()).toEqual([]); + expect(snapshots).toEqual([[], ["hello"], []]); + unsubscribe(); + }); + + test("stages replacements until the previous registration retires", () => { + const registry = new SlashCommandRegistry(); + const first = { name: "hello", description: "First", run: () => undefined }; + const second = { name: "hello", description: "Second", run: () => undefined }; + const closeFirst = registry.register(first); + const closeSecond = registry.replace(second); + expect(registry.list()).toEqual([first]); + + closeSecond(); + expect(registry.list()).toEqual([first]); + const closeReplacement = registry.replace(second); + closeFirst(); + expect(registry.list()).toEqual([second]); + closeReplacement(); + expect(registry.list()).toEqual([]); + }); + + test("tracks plugin facet reload and unload", async () => { + const registry = new SlashCommandRegistry(); + const originalRun = vi.fn(); + const host = await createFacetHost({ + facets: [ + createSlashCommandsRuntimeFacet(registry), + defineFacet({ + id: "@test/example-hello", + setup(env) { + const commands = env.use(SlashCommands); + env.onActivate(() => + env.own(commands.replace({ name: "hello", description: "Original", run: originalRun })), + ); + }, + }), + ], + }); + expect(registry.list().map(({ name }) => name)).toEqual(["hello"]); + + const failure = new Error("replacement failed"); + await expect( + host.reload([ + defineFacet({ + id: "@test/example-hello", + setup(env) { + const commands = env.use(SlashCommands); + env.onActivate(() => { + env.own(commands.replace({ name: "hello", description: "Failing", run: () => undefined })); + throw failure; + }); + }, + }), + ]), + ).rejects.toBe(failure); + expect(registry.list()).toEqual([expect.objectContaining({ name: "hello", run: originalRun })]); + + const replacementRun = vi.fn(); + await host.reload([ + defineFacet({ + id: "@test/example-hello", + setup(env) { + const commands = env.use(SlashCommands); + env.onActivate(() => + env.own(commands.replace({ name: "hello", description: "Replacement", run: replacementRun })), + ); + }, + }), + ]); + expect(registry.list()).toEqual([ + expect.objectContaining({ name: "hello", description: "Replacement", run: replacementRun }), + ]); + + await host.dispose(); + expect(registry.list()).toEqual([]); + }); +}); diff --git a/packages/coding-agent/test/experimental-transcript-provider.test.ts b/packages/coding-agent/test/experimental-transcript-provider.test.ts new file mode 100644 index 0000000000000000000000000000000000000000..bf63fa2a02027efe2fc04eaf178924b6a84e8f69 --- /dev/null +++ b/packages/coding-agent/test/experimental-transcript-provider.test.ts @@ -0,0 +1,108 @@ +import { replicatedState } from "@earendil-works/chord"; +import { BACKGROUND_CONTEXT } from "@earendil-works/chord/context"; +import type { AgentLane, EventListener, HarnessEvent, LaneSnapshot, WatchHandle } from "@earendil-works/pi-agent-core"; +import { describe, expect, test, vi } from "vitest"; +import type { TranscriptState } from "../src/experimental/services/transcript.ts"; +import { createTranscriptService } from "../src/experimental/services/transcript-provider.ts"; + +function laneSnapshot(tipId: string | null = null): LaneSnapshot { + return { + lane: "main", + transcript: [], + tipId, + configuration: { + model: { provider: "test", modelId: "model" }, + thinkingLevel: "off", + activeToolNames: [], + }, + stats: { + messageCount: 0, + usage: { + input: 0, + output: 0, + cacheRead: 0, + cacheWrite: 0, + totalTokens: 0, + cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 }, + }, + }, + operation: null, + queues: [], + faulted: false, + }; +} + +describe("Transcript service", () => { + test("publishes coherent state and rebases after navigation", async () => { + let listener: EventListener | undefined; + const replacement = laneSnapshot("replacement-tip"); + const resnapshot = vi.fn(async () => replacement); + const unsubscribe = vi.fn(); + const handle: WatchHandle = { + snapshot: laneSnapshot(), + start(next) { + listener = next; + }, + resnapshot, + unsubscribe, + }; + const lane = { watch: async () => handle } as unknown as AgentLane; + const runtime = createTranscriptService(lane, replicatedState); + const states: TranscriptState[] = []; + runtime.service.state.subscribe((value) => { + if (value.snapshot !== null) states.push(value); + }); + await runtime.activate(); + + expect(states).toHaveLength(1); + expect(states[0]).toMatchObject({ snapshot: { lane: "main", operation: null }, event: null }); + await listener?.({ type: "run_start", lane: "main", runId: "run-1", startedAt: 1 }, BACKGROUND_CONTEXT); + expect(states).toHaveLength(2); + expect(states[1]).toMatchObject({ + snapshot: { operation: { id: "run-1" } }, + event: { type: "run_start", runId: "run-1" }, + }); + + await listener?.( + { + type: "entry_added", + lane: "main", + entry: { + id: "entry-1", + parentId: null, + seq: 1, + timestamp: 2, + type: "message", + message: { role: "user", content: "hello", timestamp: 2 }, + }, + }, + BACKGROUND_CONTEXT, + ); + expect(states.at(-1)).toMatchObject({ + snapshot: { tipId: "entry-1", transcript: [{ id: "entry-1" }] }, + event: { type: "entry_added" }, + }); + + const navigation: HarnessEvent = { + type: "navigation_end", + lane: "main", + runId: "navigation-1", + status: "completed", + fromTipId: "entry-1", + tipId: "replacement-tip", + endedAt: 3, + }; + await listener?.(navigation, BACKGROUND_CONTEXT); + await vi.waitFor(() => expect(states.at(-1)?.event).toBeNull()); + expect(states.at(-2)).toMatchObject({ + snapshot: { tipId: "entry-1" }, + event: { type: "navigation_end" }, + }); + expect(states.at(-1)).toMatchObject({ snapshot: { tipId: "replacement-tip" }, event: null }); + expect(runtime.service.state.value).toMatchObject({ snapshot: { tipId: "replacement-tip" }, event: null }); + expect(resnapshot).toHaveBeenCalledOnce(); + + await runtime.dispose(); + expect(unsubscribe).toHaveBeenCalledOnce(); + }); +}); diff --git a/packages/coding-agent/test/experimental.test.ts b/packages/coding-agent/test/experimental.test.ts new file mode 100644 index 0000000000000000000000000000000000000000..665616e808ff034c6fd8482633002d74eca1c390 --- /dev/null +++ b/packages/coding-agent/test/experimental.test.ts @@ -0,0 +1,44 @@ +import { afterEach, describe, expect, it } from "vitest"; +import { areExperimentalFeaturesEnabled } from "../src/core/experimental.ts"; + +describe("areExperimentalFeaturesEnabled", () => { + const originalPiExperimental = process.env.PI_EXPERIMENTAL; + + afterEach(() => { + if (originalPiExperimental === undefined) { + delete process.env.PI_EXPERIMENTAL; + } else { + process.env.PI_EXPERIMENTAL = originalPiExperimental; + } + }); + + it("returns false when PI_EXPERIMENTAL is unset", () => { + delete process.env.PI_EXPERIMENTAL; + + expect(areExperimentalFeaturesEnabled()).toBe(false); + }); + + it("returns false when PI_EXPERIMENTAL is empty", () => { + process.env.PI_EXPERIMENTAL = ""; + + expect(areExperimentalFeaturesEnabled()).toBe(false); + }); + + it("returns true when PI_EXPERIMENTAL is set to 1", () => { + process.env.PI_EXPERIMENTAL = "1"; + + expect(areExperimentalFeaturesEnabled()).toBe(true); + }); + + it("returns false when PI_EXPERIMENTAL is set to 0", () => { + process.env.PI_EXPERIMENTAL = "0"; + + expect(areExperimentalFeaturesEnabled()).toBe(false); + }); + + it("returns false when PI_EXPERIMENTAL is set to a non-1 value", () => { + process.env.PI_EXPERIMENTAL = "true"; + + expect(areExperimentalFeaturesEnabled()).toBe(false); + }); +}); diff --git a/packages/coding-agent/test/export-html-skill-block.test.ts b/packages/coding-agent/test/export-html-skill-block.test.ts new file mode 100644 index 0000000000000000000000000000000000000000..ae89decf5c706d98fb738474219ad543970adb4f --- /dev/null +++ b/packages/coding-agent/test/export-html-skill-block.test.ts @@ -0,0 +1,40 @@ +import { readFileSync } from "fs"; +import { describe, expect, it } from "vitest"; + +describe("export HTML skill block rendering", () => { + const templateJs = readFileSync(new URL("../src/core/export-html/template.js", import.meta.url), "utf-8"); + + it("strips skill wrapper XML from user message rendering", () => { + // Skill commands store a structural wrapper in the raw user message: + // \n...\n\n\nactual prompt + // The export renderer must detect that wrapper and render only the user-visible prompt, + // not the Pi-generated ... XML tags. + expect(templateJs).toMatch(/parseSkillBlock/); + expect(templateJs).toMatch(/skillBlock\.userMessage/); + }); + + it("renders skill invocation and user message as separate sibling blocks", () => { + // The skill block and user message should render as separate entry-level elements, + // matching the TUI layout where SkillInvocationMessageComponent and + // UserMessageComponent are siblings, not nested. + expect(templateJs).toMatch(/skill-invocation/); + + // When a skill block has a userMessage, the user-message div must be emitted + // as a separate block after the skill-invocation div, containing the user-authored text. + // Verify the code checks hasUserContent so the user-message div is only omitted + // when the skill block has no user prompt and no images. + expect(templateJs).toMatch(/hasUserContent/); + }); + + it("renders skill content as markdown, not raw text", () => { + // The skill block body is markdown (from the SKILL.md file). + // It should be rendered through safeMarkedParse, not escaped as raw text. + expect(templateJs).toMatch(/safeMarkedParse\(skillBlock\.content\)/); + }); + + it("shows skill name and user message in the sidebar tree", () => { + // The sidebar tree should display both the skill name and the user prompt, + // not just one or the other. + expect(templateJs).toMatch(/tree-role-skill/); + }); +}); diff --git a/packages/coding-agent/test/export-html-whitespace.test.ts b/packages/coding-agent/test/export-html-whitespace.test.ts new file mode 100644 index 0000000000000000000000000000000000000000..9d2a0e84d944fc756d1a86dc5138172ec31f4d15 --- /dev/null +++ b/packages/coding-agent/test/export-html-whitespace.test.ts @@ -0,0 +1,42 @@ +import type { Component } from "@earendil-works/pi-tui"; +import { readFileSync } from "fs"; +import { describe, expect, it } from "vitest"; +import { ansiLinesToHtml } from "../src/core/export-html/ansi-to-html.ts"; +import { createToolHtmlRenderer } from "../src/core/export-html/tool-renderer.ts"; +import type { ToolDefinition } from "../src/core/extensions/types.ts"; +import type { Theme } from "../src/modes/interactive/theme/theme.ts"; + +describe("export HTML tool output whitespace", () => { + it("preserves whitespace for plain-text tool output lines without preserving template whitespace", () => { + const css = readFileSync(new URL("../src/core/export-html/template.css", import.meta.url), "utf-8"); + + expect(css).toMatch( + /\.output-preview > div:not\(\.expand-hint\),\s*\.output-full > div:not\(\.expand-hint\) \{[\s\S]*?white-space:\s*pre-wrap;/, + ); + expect(css).toMatch(/\.ansi-line\s*\{[\s\S]*?white-space:\s*pre;/); + expect(css).not.toMatch(/\.output-preview,\s*\.output-full\s*\{[\s\S]*?white-space:\s*pre-wrap;/); + }); + + it("does not insert source whitespace between ANSI-rendered lines", () => { + expect(ansiLinesToHtml(["one", "two"])).toBe('
    one
    two
    '); + }); + + it("trims TUI spacing lines from custom tool result HTML", () => { + const component: Component = { render: () => ["", "\u001b[31mone\u001b[0m", "two", ""], invalidate: () => {} }; + const tool = { + name: "custom", + label: "custom", + description: "custom", + renderResult: () => component, + } as unknown as ToolDefinition; + const renderer = createToolHtmlRenderer({ + getToolDefinition: () => tool, + theme: {} as Theme, + cwd: "/tmp", + }); + + expect(renderer.renderResult("id", "custom", [], undefined, false)?.expanded).toBe( + '
    one
    two
    ', + ); + }); +}); diff --git a/packages/coding-agent/test/export-html-xss.test.ts b/packages/coding-agent/test/export-html-xss.test.ts new file mode 100644 index 0000000000000000000000000000000000000000..da3c19f38bf582cce4993f339cd8f0aacfd3cda9 --- /dev/null +++ b/packages/coding-agent/test/export-html-xss.test.ts @@ -0,0 +1,67 @@ +import { readFileSync } from "fs"; +import { describe, expect, it } from "vitest"; + +describe("export HTML markdown link sanitization", () => { + const templateJs = readFileSync(new URL("../src/core/export-html/template.js", import.meta.url), "utf-8"); + + it("overrides the marked link renderer to use scheme allow-list sanitization", () => { + expect(templateJs).toMatch(/link\s*\(\s*token\s*\)/); + expect(templateJs).toMatch(/sanitizeMarkdownUrl\(token\.href\)/); + expect(templateJs).toMatch(/\^\(https\?\|mailto\|tel\|ftp\)/); + }); + + it("overrides the marked image renderer to use scheme allow-list sanitization", () => { + expect(templateJs).toMatch(/image\s*\(\s*token\s*\)/); + expect(templateJs).toMatch(/sanitizeMarkdownUrl\(token\.href\)/); + }); + + it("strips C0 controls before checking and emitting markdown URLs", () => { + expect(templateJs).toContain("replace(/[\\x00-\\x1f\\x7f]/g, '')"); + expect(templateJs).not.toMatch(/\^\\s\*\(javascript\|vbscript\|data\):/i); + }); + + it("escapes href attributes in the custom link renderer", () => { + // The link renderer must escape href values to prevent attribute breakout + expect(templateJs).toMatch(/escapeHtml\(href\)/); + }); + + it("escapes image mimeType attributes", () => { + // Image mimeType must be escaped to prevent attribute breakout + expect(templateJs).not.toMatch(/\$\{img\.mimeType\}/); + expect(templateJs).toMatch(/escapeHtml\(img\.mimeType/); + }); + + it("escapes image data attributes", () => { + // Image data is embedded in src attributes and must not allow attribute breakout. + expect(templateJs).not.toMatch(/;base64,\$\{img\.data\}"/); + expect(templateJs).toMatch(/;base64,\$\{escapeHtml\(img\.data \|\| (?:''|"")\)\}"/); + }); + + it("escapes entry IDs before inserting them into attributes", () => { + // Session entry IDs are embedded in id and data-entry-id attributes. + expect(templateJs).not.toMatch(/id="\$\{entryId\}"/); + expect(templateJs).not.toMatch(/data-entry-id="\$\{entryId\}"/); + expect(templateJs).toMatch(/entry-\$\{escapeHtml\(entry\.id\)\}/); + expect(templateJs).toMatch(/data-entry-id="\$\{escapeHtml\(entryId\)\}"/); + }); + + it("escapes tree metadata rendered from session fields", () => { + // The tree renders session metadata via innerHTML, so dynamic fields must be escaped. + expect(templateJs).not.toMatch(/\[\$\{msg\.toolName \|\| 'tool'\}\]/); + expect(templateJs).not.toMatch(/\[\$\{msg\.role\}\]/); + expect(templateJs).not.toMatch(/\[model: \$\{entry\.modelId\}\]/); + expect(templateJs).not.toMatch(/\[thinking: \$\{entry\.thinkingLevel\}\]/); + expect(templateJs).not.toMatch(/\[\$\{entry\.type\}\]/); + expect(templateJs).toMatch(/\$\{escapeHtml\(msg\.toolName \|\| 'tool'\)\}/); + expect(templateJs).toMatch(/\$\{escapeHtml\(msg\.role\)\}/); + expect(templateJs).toMatch(/\$\{escapeHtml\(entry\.modelId\)\}/); + expect(templateJs).toMatch(/\$\{escapeHtml\(entry\.thinkingLevel\)\}/); + expect(templateJs).toMatch(/\$\{escapeHtml\(entry\.type\)\}/); + }); + + it("escapes model names in the exported header", () => { + // Assistant message provider/model values are collected from the session and rendered with innerHTML. + expect(templateJs).not.toMatch(/\$\{globalStats\.models\.join\(', '\) \|\| 'unknown'\}/); + expect(templateJs).toMatch(/\$\{escapeHtml\(globalStats\.models\.join\(', '\) \|\| 'unknown'\)\}/); + }); +}); diff --git a/packages/coding-agent/test/export-jsonl-share.test.ts b/packages/coding-agent/test/export-jsonl-share.test.ts new file mode 100644 index 0000000000000000000000000000000000000000..5e66859314474b8747117a21e5b50bc06a30bef6 --- /dev/null +++ b/packages/coding-agent/test/export-jsonl-share.test.ts @@ -0,0 +1,122 @@ +import { mkdtempSync, readFileSync, rmSync } from "node:fs"; +import { tmpdir } from "node:os"; +import { join } from "node:path"; +import type { AssistantMessage, ToolResultMessage } from "@earendil-works/pi-ai/compat"; +import { getModel } from "@earendil-works/pi-ai/compat"; +import { Type } from "typebox"; +import { afterEach, describe, expect, it } from "vitest"; +import { defineTool } from "../src/core/extensions/types.ts"; +import { createAgentSession } from "../src/core/sdk.ts"; +import { SessionManager } from "../src/core/session-manager.ts"; +import { SettingsManager } from "../src/core/settings-manager.ts"; +import { exportSessionForShare } from "../src/modes/interactive/session-share.ts"; +import { assistantMsg, userMsg } from "./utilities.ts"; + +describe("JSONL share export", () => { + const tempDirs: string[] = []; + + afterEach(() => { + for (const tempDir of tempDirs.splice(0)) { + rmSync(tempDir, { recursive: true, force: true }); + } + }); + + it("adds presentation data without changing conversation IDs or links", async () => { + const tempDir = mkdtempSync(join(tmpdir(), "pi-jsonl-share-")); + tempDirs.push(tempDir); + const sessionManager = SessionManager.inMemory(tempDir); + const { session } = await createAgentSession({ + cwd: tempDir, + agentDir: join(tempDir, "agent"), + model: getModel("anthropic", "claude-sonnet-4-5")!, + settingsManager: SettingsManager.inMemory(), + sessionManager, + tools: ["share_tool"], + customTools: [ + defineTool({ + name: "share_tool", + label: "Share Tool", + description: "Render a value for sharing", + parameters: Type.Object({ value: Type.String({ description: "Value to render" }) }), + execute: async () => ({ content: [{ type: "text", text: "done" }], details: {} }), + }), + ], + }); + + try { + const userId = sessionManager.appendMessage(userMsg("hello")); + const assistant: AssistantMessage = { + ...assistantMsg(""), + content: [{ type: "toolCall", id: "call-1", name: "share_tool", arguments: { value: "example" } }], + stopReason: "toolUse", + }; + const assistantId = sessionManager.appendMessage(assistant); + const result: ToolResultMessage = { + role: "toolResult", + toolCallId: "call-1", + toolName: "share_tool", + content: [{ type: "text", text: "done" }], + details: {}, + isError: false, + timestamp: Date.now(), + }; + const resultId = sessionManager.appendMessage(result); + const originalEntryIds = sessionManager.getBranch().map((entry) => entry.id); + + const normalPath = join(tempDir, "normal.jsonl"); + session.exportToJsonl(normalPath); + const normalRecords = readFileSync(normalPath, "utf8") + .trim() + .split("\n") + .map((line) => JSON.parse(line) as Record); + expect(normalRecords.some((record) => record.type === "custom" && record.customType === "pi.share")).toBe( + false, + ); + + const sharePath = join(tempDir, "share.jsonl"); + exportSessionForShare(sharePath, session); + const records = readFileSync(sharePath, "utf8") + .trim() + .split("\n") + .map((line) => JSON.parse(line) as Record); + const conversationRecords = records.slice(1, -1); + expect(conversationRecords.map((record) => record.id)).toEqual(originalEntryIds); + expect(conversationRecords.map((record) => record.parentId)).toEqual([null, ...originalEntryIds.slice(0, -1)]); + expect(conversationRecords.slice(-3).map((record) => record.id)).toEqual([userId, assistantId, resultId]); + + const shareEntry = records.at(-1) as { + id: string; + data?: { + systemPrompt?: string; + tools?: Array>; + }; + }; + expect(shareEntry).toMatchObject({ + type: "custom", + customType: "pi.share", + parentId: resultId, + timestamp: expect.any(String), + }); + expect(shareEntry.data?.systemPrompt).toBe(session.state.systemPrompt); + expect(shareEntry.data?.tools).toEqual([ + expect.objectContaining({ + name: "share_tool", + description: "Render a value for sharing", + }), + ]); + expect(shareEntry.data).not.toHaveProperty("renderedTools"); + expect(shareEntry.data).not.toHaveProperty("theme"); + expect(shareEntry.data).not.toHaveProperty("version"); + + const imported = SessionManager.open(sharePath); + expect(imported.getLeafId()).toBe(shareEntry.id); + expect(imported.buildSessionContext().messages.map((message) => message.role)).toEqual([ + "user", + "assistant", + "toolResult", + ]); + } finally { + session.dispose(); + } + }); +}); diff --git a/packages/coding-agent/test/extensions-discovery.test.ts b/packages/coding-agent/test/extensions-discovery.test.ts new file mode 100644 index 0000000000000000000000000000000000000000..ebb9b464cd60ecd0684d4454a8143104cf1f8528 --- /dev/null +++ b/packages/coding-agent/test/extensions-discovery.test.ts @@ -0,0 +1,532 @@ +import * as fs from "node:fs"; +import * as os from "node:os"; +import * as path from "node:path"; +import { fileURLToPath } from "node:url"; +import { afterEach, beforeEach, describe, expect, it } from "vitest"; +import { discoverAndLoadExtensions } from "../src/core/extensions/loader.ts"; + +const __dirname = path.dirname(fileURLToPath(import.meta.url)); + +describe("extensions discovery", () => { + let tempDir: string; + let extensionsDir: string; + + beforeEach(() => { + tempDir = fs.mkdtempSync(path.join(os.tmpdir(), "pi-ext-test-")); + extensionsDir = path.join(tempDir, "extensions"); + fs.mkdirSync(extensionsDir); + }); + + afterEach(() => { + fs.rmSync(tempDir, { recursive: true, force: true }); + }); + + const extensionCode = ` + export default function(pi) { + pi.registerCommand("test", { handler: async () => {} }); + } + `; + + const extensionCodeWithTool = (toolName: string) => ` + import { Type } from "typebox"; + export default function(pi) { + pi.registerTool({ + name: "${toolName}", + label: "${toolName}", + description: "Test tool", + parameters: Type.Object({}), + execute: async () => ({ content: [{ type: "text", text: "ok" }] }), + }); + } + `; + + it("discovers direct .ts files in extensions/", async () => { + fs.writeFileSync(path.join(extensionsDir, "foo.ts"), extensionCode); + fs.writeFileSync(path.join(extensionsDir, "bar.ts"), extensionCode); + + const result = await discoverAndLoadExtensions([], tempDir, tempDir); + + expect(result.errors).toHaveLength(0); + expect(result.extensions).toHaveLength(2); + expect(result.extensions.map((e) => path.basename(e.path)).sort()).toEqual(["bar.ts", "foo.ts"]); + }); + + it("loads the coding-agent entrypoint without rewriting pi-ai provider subpaths", async () => { + fs.writeFileSync( + path.join(extensionsDir, "coding-agent-import.ts"), + ` + import { getAgentDir } from "@earendil-works/pi-coding-agent"; + void getAgentDir; + export default function(pi) { + pi.registerCommand("test", { handler: async () => {} }); + } + `, + ); + + const result = await discoverAndLoadExtensions([], tempDir, tempDir); + + expect(result.errors).toHaveLength(0); + expect(result.extensions).toHaveLength(1); + }); + + it("keeps the type-only pi-ai OAuth compatibility barrel resolvable", async () => { + fs.writeFileSync( + path.join(extensionsDir, "oauth-import.ts"), + ` + import * as oauth from "@earendil-works/pi-ai/oauth"; + void oauth; + export default function(pi) { + pi.registerCommand("test", { handler: async () => {} }); + } + `, + ); + + const result = await discoverAndLoadExtensions([], tempDir, tempDir); + + expect(result.errors).toEqual([]); + expect(result.extensions).toHaveLength(1); + }); + + it("discovers direct .js files in extensions/", async () => { + fs.writeFileSync(path.join(extensionsDir, "foo.js"), extensionCode); + + const result = await discoverAndLoadExtensions([], tempDir, tempDir); + + expect(result.errors).toHaveLength(0); + expect(result.extensions).toHaveLength(1); + expect(path.basename(result.extensions[0].path)).toBe("foo.js"); + }); + + it("discovers subdirectory with index.ts", async () => { + const subdir = path.join(extensionsDir, "my-extension"); + fs.mkdirSync(subdir); + fs.writeFileSync(path.join(subdir, "index.ts"), extensionCode); + + const result = await discoverAndLoadExtensions([], tempDir, tempDir); + + expect(result.errors).toHaveLength(0); + expect(result.extensions).toHaveLength(1); + expect(result.extensions[0].path).toContain("my-extension"); + expect(result.extensions[0].path).toContain("index.ts"); + }); + + it("discovers subdirectory with index.js", async () => { + const subdir = path.join(extensionsDir, "my-extension"); + fs.mkdirSync(subdir); + fs.writeFileSync(path.join(subdir, "index.js"), extensionCode); + + const result = await discoverAndLoadExtensions([], tempDir, tempDir); + + expect(result.errors).toHaveLength(0); + expect(result.extensions).toHaveLength(1); + expect(result.extensions[0].path).toContain("index.js"); + }); + + it("prefers index.ts over index.js", async () => { + const subdir = path.join(extensionsDir, "my-extension"); + fs.mkdirSync(subdir); + fs.writeFileSync(path.join(subdir, "index.ts"), extensionCode); + fs.writeFileSync(path.join(subdir, "index.js"), extensionCode); + + const result = await discoverAndLoadExtensions([], tempDir, tempDir); + + expect(result.errors).toHaveLength(0); + expect(result.extensions).toHaveLength(1); + expect(result.extensions[0].path).toContain("index.ts"); + }); + + it("discovers subdirectory with package.json pi field", async () => { + const subdir = path.join(extensionsDir, "my-package"); + const srcDir = path.join(subdir, "src"); + fs.mkdirSync(subdir); + fs.mkdirSync(srcDir); + fs.writeFileSync(path.join(srcDir, "main.ts"), extensionCode); + fs.writeFileSync( + path.join(subdir, "package.json"), + JSON.stringify({ + name: "my-package", + pi: { + extensions: ["./src/main.ts"], + }, + }), + ); + + const result = await discoverAndLoadExtensions([], tempDir, tempDir); + + expect(result.errors).toHaveLength(0); + expect(result.extensions).toHaveLength(1); + expect(result.extensions[0].path).toContain("src"); + expect(result.extensions[0].path).toContain("main.ts"); + }); + + it("keeps package.json pi extension entries with leading tilde package-relative", async () => { + const subdir = path.join(extensionsDir, "tilde-package"); + const directExtensionPath = path.join(subdir, "~entry.ts"); + const slashExtensionPath = path.join(subdir, "~", "entry.ts"); + fs.mkdirSync(path.join(subdir, "~"), { recursive: true }); + fs.writeFileSync(directExtensionPath, extensionCode); + fs.writeFileSync(slashExtensionPath, extensionCode); + fs.writeFileSync( + path.join(subdir, "package.json"), + JSON.stringify({ + name: "tilde-package", + pi: { + extensions: ["~entry.ts", "~/entry.ts"], + }, + }), + ); + + const result = await discoverAndLoadExtensions([], tempDir, tempDir); + + expect(result.errors).toHaveLength(0); + expect(result.extensions.map((extension) => extension.path).sort()).toEqual( + [directExtensionPath, slashExtensionPath].sort(), + ); + }); + + it("package.json can declare multiple extensions", async () => { + const subdir = path.join(extensionsDir, "my-package"); + fs.mkdirSync(subdir); + fs.writeFileSync(path.join(subdir, "ext1.ts"), extensionCode); + fs.writeFileSync(path.join(subdir, "ext2.ts"), extensionCode); + fs.writeFileSync( + path.join(subdir, "package.json"), + JSON.stringify({ + name: "my-package", + pi: { + extensions: ["./ext1.ts", "./ext2.ts"], + }, + }), + ); + + const result = await discoverAndLoadExtensions([], tempDir, tempDir); + + expect(result.errors).toHaveLength(0); + expect(result.extensions).toHaveLength(2); + }); + + it("package.json with pi field takes precedence over index.ts", async () => { + const subdir = path.join(extensionsDir, "my-package"); + fs.mkdirSync(subdir); + fs.writeFileSync(path.join(subdir, "index.ts"), extensionCodeWithTool("from-index")); + fs.writeFileSync(path.join(subdir, "custom.ts"), extensionCodeWithTool("from-custom")); + fs.writeFileSync( + path.join(subdir, "package.json"), + JSON.stringify({ + name: "my-package", + pi: { + extensions: ["./custom.ts"], + }, + }), + ); + + const result = await discoverAndLoadExtensions([], tempDir, tempDir); + + expect(result.errors).toHaveLength(0); + expect(result.extensions).toHaveLength(1); + expect(result.extensions[0].path).toContain("custom.ts"); + // Verify the right tool was registered + expect(result.extensions[0].tools.has("from-custom")).toBe(true); + expect(result.extensions[0].tools.has("from-index")).toBe(false); + }); + + it("ignores package.json without pi field, falls back to index.ts", async () => { + const subdir = path.join(extensionsDir, "my-package"); + fs.mkdirSync(subdir); + fs.writeFileSync(path.join(subdir, "index.ts"), extensionCode); + fs.writeFileSync( + path.join(subdir, "package.json"), + JSON.stringify({ + name: "my-package", + version: "1.0.0", + }), + ); + + const result = await discoverAndLoadExtensions([], tempDir, tempDir); + + expect(result.errors).toHaveLength(0); + expect(result.extensions).toHaveLength(1); + expect(result.extensions[0].path).toContain("index.ts"); + }); + + it("ignores subdirectory without index or package.json", async () => { + const subdir = path.join(extensionsDir, "not-an-extension"); + fs.mkdirSync(subdir); + fs.writeFileSync(path.join(subdir, "helper.ts"), extensionCode); + fs.writeFileSync(path.join(subdir, "utils.ts"), extensionCode); + + const result = await discoverAndLoadExtensions([], tempDir, tempDir); + + expect(result.errors).toHaveLength(0); + expect(result.extensions).toHaveLength(0); + }); + + it("does not recurse beyond one level", async () => { + const subdir = path.join(extensionsDir, "container"); + const nested = path.join(subdir, "nested"); + fs.mkdirSync(subdir); + fs.mkdirSync(nested); + fs.writeFileSync(path.join(nested, "index.ts"), extensionCode); + // No index.ts or package.json in container/ + + const result = await discoverAndLoadExtensions([], tempDir, tempDir); + + expect(result.errors).toHaveLength(0); + expect(result.extensions).toHaveLength(0); + }); + + it("handles mixed direct files and subdirectories", async () => { + // Direct file + fs.writeFileSync(path.join(extensionsDir, "direct.ts"), extensionCode); + + // Subdirectory with index + const subdir1 = path.join(extensionsDir, "with-index"); + fs.mkdirSync(subdir1); + fs.writeFileSync(path.join(subdir1, "index.ts"), extensionCode); + + // Subdirectory with package.json + const subdir2 = path.join(extensionsDir, "with-manifest"); + fs.mkdirSync(subdir2); + fs.writeFileSync(path.join(subdir2, "entry.ts"), extensionCode); + fs.writeFileSync(path.join(subdir2, "package.json"), JSON.stringify({ pi: { extensions: ["./entry.ts"] } })); + + const result = await discoverAndLoadExtensions([], tempDir, tempDir); + + expect(result.errors).toHaveLength(0); + expect(result.extensions).toHaveLength(3); + }); + + it("skips non-existent paths declared in package.json", async () => { + const subdir = path.join(extensionsDir, "my-package"); + fs.mkdirSync(subdir); + fs.writeFileSync(path.join(subdir, "exists.ts"), extensionCode); + fs.writeFileSync( + path.join(subdir, "package.json"), + JSON.stringify({ + pi: { + extensions: ["./exists.ts", "./missing.ts"], + }, + }), + ); + + const result = await discoverAndLoadExtensions([], tempDir, tempDir); + + expect(result.errors).toHaveLength(0); + expect(result.extensions).toHaveLength(1); + expect(result.extensions[0].path).toContain("exists.ts"); + }); + + it("loads extensions and registers commands", async () => { + fs.writeFileSync(path.join(extensionsDir, "with-command.ts"), extensionCode); + + const result = await discoverAndLoadExtensions([], tempDir, tempDir); + + expect(result.errors).toHaveLength(0); + expect(result.extensions).toHaveLength(1); + expect(result.extensions[0].commands.has("test")).toBe(true); + }); + + it("loads extensions and registers tools", async () => { + fs.writeFileSync(path.join(extensionsDir, "with-tool.ts"), extensionCodeWithTool("my-tool")); + + const result = await discoverAndLoadExtensions([], tempDir, tempDir); + + expect(result.errors).toHaveLength(0); + expect(result.extensions).toHaveLength(1); + expect(result.extensions[0].tools.has("my-tool")).toBe(true); + }); + + it("reports errors for invalid extension code", async () => { + fs.writeFileSync(path.join(extensionsDir, "invalid.ts"), "this is not valid typescript export"); + + const result = await discoverAndLoadExtensions([], tempDir, tempDir); + + expect(result.errors).toHaveLength(1); + expect(result.errors[0].path).toContain("invalid.ts"); + expect(result.extensions).toHaveLength(0); + }); + + it("handles explicitly configured paths", async () => { + const customPath = path.join(tempDir, "custom-location", "my-ext.ts"); + fs.mkdirSync(path.dirname(customPath), { recursive: true }); + fs.writeFileSync(customPath, extensionCode); + + const result = await discoverAndLoadExtensions([customPath], tempDir, tempDir); + + expect(result.errors).toHaveLength(0); + expect(result.extensions).toHaveLength(1); + expect(result.extensions[0].path).toContain("my-ext.ts"); + }); + + it("resolves dependencies from extension's own node_modules", async () => { + // Load extension that has its own package.json and node_modules with 'ms' package + const extPath = path.resolve(__dirname, "../examples/extensions/with-deps"); + + const result = await discoverAndLoadExtensions([extPath], tempDir, tempDir); + + expect(result.errors).toHaveLength(0); + expect(result.extensions).toHaveLength(1); + expect(result.extensions[0].path).toContain("with-deps"); + // The extension registers a 'parse_duration' tool + expect(result.extensions[0].tools.has("parse_duration")).toBe(true); + }); + + it("registers message and entry renderers", async () => { + const extCode = ` + export default function(pi) { + pi.registerMarkdownTransformer((markdown) => { + return markdown; + }); + pi.registerMessageRenderer("my-custom-type", (message, options, theme) => { + return null; // Use default rendering + }); + pi.registerEntryRenderer("my-entry-type", (entry, options, theme) => { + return null; + }); + } + `; + fs.writeFileSync(path.join(extensionsDir, "with-renderer.ts"), extCode); + + const result = await discoverAndLoadExtensions([], tempDir, tempDir); + + expect(result.errors).toHaveLength(0); + expect(result.extensions).toHaveLength(1); + expect(result.extensions[0].markdownTransformer).toBeDefined(); + expect(result.extensions[0].messageRenderers.has("my-custom-type")).toBe(true); + expect(result.extensions[0].entryRenderers?.has("my-entry-type")).toBe(true); + }); + + it("reports error when extension throws during initialization", async () => { + const extCode = ` + export default function(pi) { + throw new Error("Initialization failed!"); + } + `; + fs.writeFileSync(path.join(extensionsDir, "throws.ts"), extCode); + + const result = await discoverAndLoadExtensions([], tempDir, tempDir); + + expect(result.errors).toHaveLength(1); + expect(result.errors[0].error).toContain("Initialization failed!"); + expect(result.extensions).toHaveLength(0); + }); + + it("reports error when extension has no default export", async () => { + const extCode = ` + export function notDefault(pi) { + pi.registerCommand("test", { handler: async () => {} }); + } + `; + fs.writeFileSync(path.join(extensionsDir, "no-default.ts"), extCode); + + const result = await discoverAndLoadExtensions([], tempDir, tempDir); + + expect(result.errors).toHaveLength(1); + expect(result.errors[0].error).toContain("does not export a valid factory function"); + expect(result.extensions).toHaveLength(0); + }); + + it("allows multiple extensions to register different tools", async () => { + fs.writeFileSync(path.join(extensionsDir, "tool-a.ts"), extensionCodeWithTool("tool-a")); + fs.writeFileSync(path.join(extensionsDir, "tool-b.ts"), extensionCodeWithTool("tool-b")); + + const result = await discoverAndLoadExtensions([], tempDir, tempDir); + + expect(result.errors).toHaveLength(0); + expect(result.extensions).toHaveLength(2); + + const allTools = new Set(); + for (const ext of result.extensions) { + for (const name of ext.tools.keys()) { + allTools.add(name); + } + } + expect(allTools.has("tool-a")).toBe(true); + expect(allTools.has("tool-b")).toBe(true); + }); + + it("loads extension with event handlers", async () => { + const extCode = ` + export default function(pi) { + pi.on("agent_start", async () => {}); + pi.on("tool_call", async (event) => undefined); + pi.on("agent_end", async () => {}); + } + `; + fs.writeFileSync(path.join(extensionsDir, "with-handlers.ts"), extCode); + + const result = await discoverAndLoadExtensions([], tempDir, tempDir); + + expect(result.errors).toHaveLength(0); + expect(result.extensions).toHaveLength(1); + expect(result.extensions[0].handlers.has("agent_start")).toBe(true); + expect(result.extensions[0].handlers.has("tool_call")).toBe(true); + expect(result.extensions[0].handlers.has("agent_end")).toBe(true); + }); + + it("loads extension with shortcuts", async () => { + const extCode = ` + export default function(pi) { + pi.registerShortcut("ctrl+t", { + description: "Test shortcut", + handler: async (ctx) => {}, + }); + } + `; + fs.writeFileSync(path.join(extensionsDir, "with-shortcut.ts"), extCode); + + const result = await discoverAndLoadExtensions([], tempDir, tempDir); + + expect(result.errors).toHaveLength(0); + expect(result.extensions).toHaveLength(1); + expect(result.extensions[0].shortcuts.has("ctrl+t")).toBe(true); + }); + + it("loads extension with flags", async () => { + const extCode = ` + export default function(pi) { + pi.registerFlag("my-flag", { + description: "My custom flag", + handler: async (value) => {}, + }); + } + `; + fs.writeFileSync(path.join(extensionsDir, "with-flag.ts"), extCode); + + const result = await discoverAndLoadExtensions([], tempDir, tempDir); + + expect(result.errors).toHaveLength(0); + expect(result.extensions).toHaveLength(1); + expect(result.extensions[0].flags.has("my-flag")).toBe(true); + }); + + it("loadExtensions only loads explicit paths without discovery", async () => { + // Create discoverable extensions (would be found by discoverAndLoadExtensions) + fs.writeFileSync(path.join(extensionsDir, "discovered.ts"), extensionCodeWithTool("discovered")); + + // Create explicit extension outside discovery path + const explicitPath = path.join(tempDir, "explicit.ts"); + fs.writeFileSync(explicitPath, extensionCodeWithTool("explicit")); + + // Use loadExtensions directly to skip discovery + const { loadExtensions } = await import("../src/core/extensions/loader.ts"); + const result = await loadExtensions([explicitPath], tempDir); + + expect(result.errors).toHaveLength(0); + expect(result.extensions).toHaveLength(1); + expect(result.extensions[0].tools.has("explicit")).toBe(true); + expect(result.extensions[0].tools.has("discovered")).toBe(false); + }); + + it("loadExtensions with no paths loads nothing", async () => { + // Create discoverable extensions (would be found by discoverAndLoadExtensions) + fs.writeFileSync(path.join(extensionsDir, "discovered.ts"), extensionCode); + + // Use loadExtensions directly with empty paths + const { loadExtensions } = await import("../src/core/extensions/loader.ts"); + const result = await loadExtensions([], tempDir); + + expect(result.errors).toHaveLength(0); + expect(result.extensions).toHaveLength(0); + }); +}); diff --git a/packages/coding-agent/test/extensions-input-event.test.ts b/packages/coding-agent/test/extensions-input-event.test.ts new file mode 100644 index 0000000000000000000000000000000000000000..b89dc1c78d5e5bd00f0ca6a2752d836c1c573198 --- /dev/null +++ b/packages/coding-agent/test/extensions-input-event.test.ts @@ -0,0 +1,125 @@ +import * as fs from "node:fs"; +import * as os from "node:os"; +import * as path from "node:path"; +import { afterEach, beforeEach, describe, expect, it } from "vitest"; +import { AuthStorage } from "../src/core/auth-storage.ts"; +import { discoverAndLoadExtensions } from "../src/core/extensions/loader.ts"; +import { ExtensionRunner } from "../src/core/extensions/runner.ts"; +import { SessionManager } from "../src/core/session-manager.ts"; + +import { createInMemoryModelRegistry } from "./model-runtime-test-utils.ts"; + +describe("Input Event", () => { + let tempDir: string; + let extensionsDir: string; + + beforeEach(() => { + tempDir = fs.mkdtempSync(path.join(os.tmpdir(), "pi-input-test-")); + extensionsDir = path.join(tempDir, "extensions"); + fs.mkdirSync(extensionsDir); + // Clean globalThis test vars + delete (globalThis as any).testVar; + }); + + afterEach(() => fs.rmSync(tempDir, { recursive: true, force: true })); + + async function createRunner(...extensions: string[]) { + // Clear and recreate extensions dir for clean state + fs.rmSync(extensionsDir, { recursive: true, force: true }); + fs.mkdirSync(extensionsDir); + for (let i = 0; i < extensions.length; i++) fs.writeFileSync(path.join(extensionsDir, `e${i}.ts`), extensions[i]); + const result = await discoverAndLoadExtensions([], tempDir, tempDir); + const sm = SessionManager.inMemory(); + const mr = await createInMemoryModelRegistry(AuthStorage.inMemory()); + return new ExtensionRunner(result.extensions, result.runtime, tempDir, sm, mr); + } + + it("returns continue when no handlers, undefined return, or explicit continue", async () => { + // No handlers + expect((await (await createRunner()).emitInput("x", undefined, "interactive")).action).toBe("continue"); + // Returns undefined + let r = await createRunner(`export default p => p.on("input", async () => {});`); + expect((await r.emitInput("x", undefined, "interactive")).action).toBe("continue"); + // Returns explicit continue + r = await createRunner(`export default p => p.on("input", async () => ({ action: "continue" }));`); + expect((await r.emitInput("x", undefined, "interactive")).action).toBe("continue"); + }); + + it("transforms text and preserves images when omitted", async () => { + const r = await createRunner( + `export default p => p.on("input", async e => ({ action: "transform", text: "T:" + e.text }));`, + ); + const imgs = [{ type: "image" as const, data: "orig", mimeType: "image/png" }]; + const result = await r.emitInput("hi", imgs, "interactive"); + expect(result).toEqual({ action: "transform", text: "T:hi", images: imgs }); + }); + + it("transforms and replaces images when provided", async () => { + const r = await createRunner( + `export default p => p.on("input", async () => ({ action: "transform", text: "X", images: [{ type: "image", data: "new", mimeType: "image/jpeg" }] }));`, + ); + const result = await r.emitInput("hi", [{ type: "image", data: "orig", mimeType: "image/png" }], "interactive"); + expect(result).toEqual({ + action: "transform", + text: "X", + images: [{ type: "image", data: "new", mimeType: "image/jpeg" }], + }); + }); + + it("chains transforms across multiple handlers", async () => { + const r = await createRunner( + `export default p => p.on("input", async e => ({ action: "transform", text: e.text + "[1]" }));`, + `export default p => p.on("input", async e => ({ action: "transform", text: e.text + "[2]" }));`, + ); + const result = await r.emitInput("X", undefined, "interactive"); + expect(result).toEqual({ action: "transform", text: "X[1][2]", images: undefined }); + }); + + it("short-circuits on handled and skips subsequent handlers", async () => { + (globalThis as any).testVar = false; + const r = await createRunner( + `export default p => p.on("input", async () => ({ action: "handled" }));`, + `export default p => p.on("input", async () => { globalThis.testVar = true; });`, + ); + expect(await r.emitInput("X", undefined, "interactive")).toEqual({ action: "handled" }); + expect((globalThis as any).testVar).toBe(false); + }); + + it("passes source correctly for all source types", async () => { + const r = await createRunner( + `export default p => p.on("input", async e => { globalThis.testVar = e.source; return { action: "continue" }; });`, + ); + for (const source of ["interactive", "rpc", "extension"] as const) { + await r.emitInput("x", undefined, source); + expect((globalThis as any).testVar).toBe(source); + } + }); + + it("passes streamingBehavior correctly", async () => { + const r = await createRunner( + `export default p => p.on("input", async e => { globalThis.testVar = e.streamingBehavior; return { action: "continue" }; });`, + ); + await r.emitInput("x", undefined, "interactive", "steer"); + expect((globalThis as any).testVar).toBe("steer"); + await r.emitInput("x", undefined, "interactive", "followUp"); + expect((globalThis as any).testVar).toBe("followUp"); + await r.emitInput("x", undefined, "interactive"); + expect((globalThis as any).testVar).toBeUndefined(); + }); + + it("catches handler errors and continues", async () => { + const r = await createRunner(`export default p => p.on("input", async () => { throw new Error("boom"); });`); + const errs: string[] = []; + r.onError((e) => errs.push(e.error)); + const result = await r.emitInput("x", undefined, "interactive"); + expect(result.action).toBe("continue"); + expect(errs).toContain("boom"); + }); + + it("hasHandlers returns correct value", async () => { + let r = await createRunner(); + expect(r.hasHandlers("input")).toBe(false); + r = await createRunner(`export default p => p.on("input", async () => {});`); + expect(r.hasHandlers("input")).toBe(true); + }); +}); diff --git a/packages/coding-agent/test/extensions-runner.test.ts b/packages/coding-agent/test/extensions-runner.test.ts new file mode 100644 index 0000000000000000000000000000000000000000..d33b2668fb910cfe019903cd8b52ca0c9b9ebe9d --- /dev/null +++ b/packages/coding-agent/test/extensions-runner.test.ts @@ -0,0 +1,1064 @@ +import { createInMemoryModelRegistry } from "./model-runtime-test-utils.ts"; +/** + * Tests for ExtensionRunner - conflict detection, error handling, tool wrapping. + */ + +import * as fs from "node:fs"; +import * as os from "node:os"; +import * as path from "node:path"; +import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; +import { AuthStorage } from "../src/core/auth-storage.ts"; +import { createExtensionRuntime, discoverAndLoadExtensions, loadExtensions } from "../src/core/extensions/loader.ts"; +import { ExtensionRunner, emitProjectTrustEvent } from "../src/core/extensions/runner.ts"; +import type { + ExtensionActions, + ExtensionContextActions, + ExtensionUIContext, + ProviderConfig, +} from "../src/core/extensions/types.ts"; +import { KeybindingsManager, type KeyId } from "../src/core/keybindings.ts"; +import type { ModelRegistry } from "../src/core/model-registry.ts"; +import type { ScopedModel } from "../src/core/model-resolver.ts"; +import { SessionManager } from "../src/core/session-manager.ts"; + +describe("ExtensionRunner", () => { + let tempDir: string; + let extensionsDir: string; + let sessionManager: SessionManager; + let modelRegistry: ModelRegistry; + const defaultKeybindings = new KeybindingsManager().getEffectiveConfig(); + + beforeEach(async () => { + tempDir = fs.mkdtempSync(path.join(os.tmpdir(), "pi-runner-test-")); + extensionsDir = path.join(tempDir, "extensions"); + fs.mkdirSync(extensionsDir); + sessionManager = SessionManager.inMemory(); + modelRegistry = await createInMemoryModelRegistry(AuthStorage.inMemory()); + }); + + afterEach(() => { + fs.rmSync(tempDir, { recursive: true, force: true }); + }); + + const providerModelConfig: ProviderConfig = { + baseUrl: "https://provider.test/v1", + apiKey: "provider-test-key", + api: "openai-completions", + models: [ + { + id: "instant-model", + name: "Instant Model", + reasoning: false, + input: ["text"], + cost: { + input: 1, + output: 2, + cacheRead: 0.1, + cacheWrite: 1.25, + tiers: [ + { + inputTokensAbove: 272000, + input: 2, + output: 3, + cacheRead: 0.2, + cacheWrite: 2.5, + }, + ], + }, + contextWindow: 128000, + maxTokens: 4096, + }, + ], + }; + + const extensionActions: ExtensionActions = { + sendMessage: () => {}, + sendUserMessage: () => {}, + appendEntry: () => {}, + setSessionName: () => {}, + getSessionName: () => undefined, + setLabel: () => {}, + getActiveTools: () => [], + getAllTools: () => [], + setActiveTools: () => {}, + refreshTools: () => {}, + getCommands: () => [], + setModel: async () => false, + getThinkingLevel: () => "off", + setThinkingLevel: () => {}, + }; + + const extensionContextActions: ExtensionContextActions = { + getModel: () => undefined, + isIdle: () => true, + isProjectTrusted: () => true, + getSignal: () => undefined, + abort: () => {}, + hasPendingMessages: () => false, + shutdown: () => {}, + getContextUsage: () => undefined, + compact: () => {}, + getSystemPrompt: () => "", + getScopedModels: () => [], + }; + + describe("scopedModels", () => { + it("reflects the getScopedModels context action on ctx.scopedModels", async () => { + const result = await discoverAndLoadExtensions([], tempDir, tempDir); + const runner = new ExtensionRunner(result.extensions, result.runtime, tempDir, sessionManager, modelRegistry); + + // Before bindCore the default is an empty list (never undefined). + expect(runner.createContext().scopedModels).toEqual([]); + + // After bindCore wires a getScopedModels action, ctx.scopedModels + // returns it live (same reference, lazy getter). + const scoped = [{ model: { id: "scoped-test" }, thinkingLevel: "high" }] as unknown as ScopedModel[]; + runner.bindCore(extensionActions, { ...extensionContextActions, getScopedModels: () => scoped }); + expect(runner.createContext().scopedModels).toBe(scoped); + }); + }); + + describe("project_trust", () => { + it("continues past undecided handlers and returns the first yes/no decision", async () => { + const undecidedPath = path.join(extensionsDir, "undecided.ts"); + const decidedPath = path.join(extensionsDir, "decided.ts"); + fs.writeFileSync( + undecidedPath, + `export default function(pi) { + pi.on("project_trust", () => ({ trusted: "undecided", remember: true })); +}`, + ); + fs.writeFileSync( + decidedPath, + `export default function(pi) { + pi.on("project_trust", () => ({ trusted: "no", remember: true })); +}`, + ); + + const extensionsResult = await loadExtensions([undecidedPath, decidedPath], tempDir); + const result = await emitProjectTrustEvent( + extensionsResult, + { type: "project_trust", cwd: tempDir }, + { + cwd: tempDir, + mode: "tui", + hasUI: false, + ui: { + select: async () => undefined, + confirm: async () => false, + input: async () => undefined, + notify: () => {}, + }, + }, + ); + + expect(result.result).toEqual({ trusted: "no", remember: true }); + expect(result.errors).toEqual([]); + }); + }); + + describe("shortcut conflicts", () => { + it("warns when extension shortcut conflicts with built-in", async () => { + const extCode = ` + export default function(pi) { + pi.registerShortcut("ctrl+c", { + description: "Conflicts with built-in", + handler: async () => {}, + }); + } + `; + fs.writeFileSync(path.join(extensionsDir, "conflict.ts"), extCode); + + const warnSpy = vi.spyOn(console, "warn").mockImplementation(() => {}); + + const result = await discoverAndLoadExtensions([], tempDir, tempDir); + const runner = new ExtensionRunner(result.extensions, result.runtime, tempDir, sessionManager, modelRegistry); + const shortcuts = runner.getShortcuts(defaultKeybindings); + + expect(warnSpy).toHaveBeenCalledWith(expect.stringContaining("conflicts with built-in")); + expect(shortcuts.has("ctrl+c")).toBe(false); + + warnSpy.mockRestore(); + }); + + it("allows a shortcut when the reserved set no longer contains the default key", async () => { + const extCode = ` + export default function(pi) { + pi.registerShortcut("ctrl+p", { + description: "Uses freed default", + handler: async () => {}, + }); + } + `; + fs.writeFileSync(path.join(extensionsDir, "rebinding.ts"), extCode); + + const warnSpy = vi.spyOn(console, "warn").mockImplementation(() => {}); + + const result = await discoverAndLoadExtensions([], tempDir, tempDir); + const runner = new ExtensionRunner(result.extensions, result.runtime, tempDir, sessionManager, modelRegistry); + const keybindings = { ...defaultKeybindings, "app.model.cycleForward": "ctrl+n" as KeyId }; + const shortcuts = runner.getShortcuts(keybindings); + + expect(shortcuts.has("ctrl+p")).toBe(true); + expect(warnSpy).not.toHaveBeenCalledWith(expect.stringContaining("conflicts with built-in")); + + warnSpy.mockRestore(); + }); + + it("warns but allows when extension uses non-reserved built-in shortcut", async () => { + const pasteImageKey = Array.isArray(defaultKeybindings["app.clipboard.pasteImage"]) + ? (defaultKeybindings["app.clipboard.pasteImage"][0] ?? "") + : defaultKeybindings["app.clipboard.pasteImage"]; + const extCode = ` + export default function(pi) { + pi.registerShortcut("${pasteImageKey}", { + description: "Overrides non-reserved", + handler: async () => {}, + }); + } + `; + fs.writeFileSync(path.join(extensionsDir, "non-reserved.ts"), extCode); + + const warnSpy = vi.spyOn(console, "warn").mockImplementation(() => {}); + + const result = await discoverAndLoadExtensions([], tempDir, tempDir); + const runner = new ExtensionRunner(result.extensions, result.runtime, tempDir, sessionManager, modelRegistry); + const shortcuts = runner.getShortcuts(defaultKeybindings); + + expect(warnSpy).toHaveBeenCalledWith( + expect.stringContaining("built-in shortcut for app.clipboard.pasteImage"), + ); + expect(shortcuts.has(pasteImageKey as KeyId)).toBe(true); + + warnSpy.mockRestore(); + }); + + it("blocks shortcuts for reserved actions even when rebound", async () => { + const extCode = ` + export default function(pi) { + pi.registerShortcut("ctrl+x", { + description: "Conflicts with rebound reserved", + handler: async () => {}, + }); + } + `; + fs.writeFileSync(path.join(extensionsDir, "rebound-reserved.ts"), extCode); + + const warnSpy = vi.spyOn(console, "warn").mockImplementation(() => {}); + + const result = await discoverAndLoadExtensions([], tempDir, tempDir); + const runner = new ExtensionRunner(result.extensions, result.runtime, tempDir, sessionManager, modelRegistry); + const keybindings = { ...defaultKeybindings, "app.interrupt": "ctrl+x" as KeyId }; + const shortcuts = runner.getShortcuts(keybindings); + + expect(warnSpy).toHaveBeenCalledWith(expect.stringContaining("conflicts with built-in")); + expect(shortcuts.has("ctrl+x")).toBe(false); + + warnSpy.mockRestore(); + }); + + it("blocks shortcuts when reserved key is also bound to non-reserved actions", async () => { + const extCode = ` + export default function(pi) { + pi.registerShortcut("ctrl+p", { + description: "Conflicts with shared reserved default", + handler: async () => {}, + }); + } + `; + fs.writeFileSync(path.join(extensionsDir, "shared-reserved.ts"), extCode); + + const warnSpy = vi.spyOn(console, "warn").mockImplementation(() => {}); + + const result = await discoverAndLoadExtensions([], tempDir, tempDir); + const runner = new ExtensionRunner(result.extensions, result.runtime, tempDir, sessionManager, modelRegistry); + const shortcuts = runner.getShortcuts(defaultKeybindings); + + expect(warnSpy).toHaveBeenCalledWith(expect.stringContaining("conflicts with built-in")); + expect(shortcuts.has("ctrl+p")).toBe(false); + + warnSpy.mockRestore(); + }); + + it("blocks shortcuts when reserved action has multiple keys", async () => { + const extCode = ` + export default function(pi) { + pi.registerShortcut("ctrl+y", { + description: "Conflicts with multi-key reserved", + handler: async () => {}, + }); + } + `; + fs.writeFileSync(path.join(extensionsDir, "multi-reserved.ts"), extCode); + + const warnSpy = vi.spyOn(console, "warn").mockImplementation(() => {}); + + const result = await discoverAndLoadExtensions([], tempDir, tempDir); + const runner = new ExtensionRunner(result.extensions, result.runtime, tempDir, sessionManager, modelRegistry); + const keybindings = { ...defaultKeybindings, "app.clear": ["ctrl+x", "ctrl+y"] as KeyId[] }; + const shortcuts = runner.getShortcuts(keybindings); + + expect(warnSpy).toHaveBeenCalledWith(expect.stringContaining("conflicts with built-in")); + expect(shortcuts.has("ctrl+y")).toBe(false); + + warnSpy.mockRestore(); + }); + + it("warns but allows when non-reserved action has multiple keys", async () => { + const extCode = ` + export default function(pi) { + pi.registerShortcut("ctrl+y", { + description: "Overrides multi-key non-reserved", + handler: async () => {}, + }); + } + `; + fs.writeFileSync(path.join(extensionsDir, "multi-non-reserved.ts"), extCode); + + const warnSpy = vi.spyOn(console, "warn").mockImplementation(() => {}); + + const result = await discoverAndLoadExtensions([], tempDir, tempDir); + const runner = new ExtensionRunner(result.extensions, result.runtime, tempDir, sessionManager, modelRegistry); + const keybindings = { ...defaultKeybindings, "app.clipboard.pasteImage": ["ctrl+x", "ctrl+y"] as KeyId[] }; + const shortcuts = runner.getShortcuts(keybindings); + + expect(warnSpy).toHaveBeenCalledWith( + expect.stringContaining("built-in shortcut for app.clipboard.pasteImage"), + ); + expect(shortcuts.has("ctrl+y")).toBe(true); + + warnSpy.mockRestore(); + }); + + it("warns when two extensions register same shortcut", async () => { + // Use a non-reserved shortcut + const extCode1 = ` + export default function(pi) { + pi.registerShortcut("ctrl+shift+x", { + description: "First extension", + handler: async () => {}, + }); + } + `; + const extCode2 = ` + export default function(pi) { + pi.registerShortcut("ctrl+shift+x", { + description: "Second extension", + handler: async () => {}, + }); + } + `; + fs.writeFileSync(path.join(extensionsDir, "ext1.ts"), extCode1); + fs.writeFileSync(path.join(extensionsDir, "ext2.ts"), extCode2); + + const warnSpy = vi.spyOn(console, "warn").mockImplementation(() => {}); + + const result = await discoverAndLoadExtensions([], tempDir, tempDir); + const runner = new ExtensionRunner(result.extensions, result.runtime, tempDir, sessionManager, modelRegistry); + const shortcuts = runner.getShortcuts(defaultKeybindings); + + expect(warnSpy).toHaveBeenCalledWith(expect.stringContaining("shortcut conflict")); + // Last one wins + expect(shortcuts.has("ctrl+shift+x")).toBe(true); + + warnSpy.mockRestore(); + }); + }); + + describe("tool collection", () => { + it("collects tools from multiple extensions", async () => { + const toolCode = (name: string) => ` + import { Type } from "typebox"; + export default function(pi) { + pi.registerTool({ + name: "${name}", + label: "${name}", + description: "Test tool", + parameters: Type.Object({}), + execute: async () => ({ content: [{ type: "text", text: "ok" }], details: {} }), + }); + } + `; + fs.writeFileSync(path.join(extensionsDir, "tool-a.ts"), toolCode("tool_a")); + fs.writeFileSync(path.join(extensionsDir, "tool-b.ts"), toolCode("tool_b")); + + const result = await discoverAndLoadExtensions([], tempDir, tempDir); + const runner = new ExtensionRunner(result.extensions, result.runtime, tempDir, sessionManager, modelRegistry); + const tools = runner.getAllRegisteredTools(); + + expect(tools.length).toBe(2); + expect(tools.map((t) => t.definition.name).sort()).toEqual(["tool_a", "tool_b"]); + }); + + // Regression test for #9300. + it("rejects extension tools without a parameter schema", async () => { + const extensionPath = path.join(extensionsDir, "missing-parameters.js"); + fs.writeFileSync( + extensionPath, + `export default function(pi) { + pi.registerTool({ + name: "noop", + label: "No-op", + description: "Do nothing", + execute: async () => ({ content: [{ type: "text", text: "ok" }] }), + }); +}`, + ); + + const result = await loadExtensions([extensionPath], tempDir); + + expect(result.extensions).toHaveLength(0); + expect(result.errors).toEqual([ + { + path: extensionPath, + error: `Failed to load extension: Tool "noop" registered by extension "${extensionPath}" must define an object parameter schema.`, + }, + ]); + }); + + it("keeps first tool when two extensions register the same name", async () => { + const first = ` + import { Type } from "typebox"; + export default function(pi) { + pi.registerTool({ + name: "shared", + label: "shared", + description: "first", + parameters: Type.Object({}), + execute: async () => ({ content: [{ type: "text", text: "ok" }], details: {} }), + }); + } + `; + const second = ` + import { Type } from "typebox"; + export default function(pi) { + pi.registerTool({ + name: "shared", + label: "shared", + description: "second", + parameters: Type.Object({}), + execute: async () => ({ content: [{ type: "text", text: "ok" }], details: {} }), + }); + } + `; + fs.writeFileSync(path.join(extensionsDir, "a-first.ts"), first); + fs.writeFileSync(path.join(extensionsDir, "b-second.ts"), second); + + const result = await discoverAndLoadExtensions([], tempDir, tempDir); + const runner = new ExtensionRunner(result.extensions, result.runtime, tempDir, sessionManager, modelRegistry); + const tools = runner.getAllRegisteredTools(); + + expect(tools).toHaveLength(1); + expect(tools[0]?.definition.description).toBe("first"); + }); + }); + + describe("command collection", () => { + it("collects commands from multiple extensions", async () => { + const cmdCode = (name: string) => ` + export default function(pi) { + pi.registerCommand("${name}", { + description: "Test command", + handler: async () => {}, + }); + } + `; + fs.writeFileSync(path.join(extensionsDir, "cmd-a.ts"), cmdCode("cmd-a")); + fs.writeFileSync(path.join(extensionsDir, "cmd-b.ts"), cmdCode("cmd-b")); + + const result = await discoverAndLoadExtensions([], tempDir, tempDir); + const runner = new ExtensionRunner(result.extensions, result.runtime, tempDir, sessionManager, modelRegistry); + const commands = runner.getRegisteredCommands(); + + expect(commands.length).toBe(2); + expect(commands.map((c) => c.name).sort()).toEqual(["cmd-a", "cmd-b"]); + expect(commands.map((c) => c.invocationName).sort()).toEqual(["cmd-a", "cmd-b"]); + }); + + it("gets command by invocation name", async () => { + const cmdCode = ` + export default function(pi) { + pi.registerCommand("my-cmd", { + description: "My command", + handler: async () => {}, + }); + } + `; + fs.writeFileSync(path.join(extensionsDir, "cmd.ts"), cmdCode); + + const result = await discoverAndLoadExtensions([], tempDir, tempDir); + const runner = new ExtensionRunner(result.extensions, result.runtime, tempDir, sessionManager, modelRegistry); + + const cmd = runner.getCommand("my-cmd"); + expect(cmd).toBeDefined(); + expect(cmd?.name).toBe("my-cmd"); + expect(cmd?.invocationName).toBe("my-cmd"); + expect(cmd?.description).toBe("My command"); + + const missing = runner.getCommand("not-exists"); + expect(missing).toBeUndefined(); + }); + + it("suffixes duplicate extension commands in insertion order", async () => { + const cmdCode = (description: string) => ` + export default function(pi) { + pi.registerCommand("shared-cmd", { + description: "${description}", + handler: async () => {}, + }); + } + `; + fs.writeFileSync(path.join(extensionsDir, "cmd-a.ts"), cmdCode("First command")); + fs.writeFileSync(path.join(extensionsDir, "cmd-b.ts"), cmdCode("Second command")); + + const result = await discoverAndLoadExtensions([], tempDir, tempDir); + const runner = new ExtensionRunner(result.extensions, result.runtime, tempDir, sessionManager, modelRegistry); + const commands = runner.getRegisteredCommands(); + const diagnostics = runner.getCommandDiagnostics(); + + expect(commands).toHaveLength(2); + expect(commands.map((command) => command.name)).toEqual(["shared-cmd", "shared-cmd"]); + expect(commands.map((command) => command.invocationName)).toEqual(["shared-cmd:1", "shared-cmd:2"]); + expect(commands.map((command) => command.description)).toEqual(["First command", "Second command"]); + expect(diagnostics).toEqual([]); + expect(runner.getCommand("shared-cmd:1")?.description).toBe("First command"); + expect(runner.getCommand("shared-cmd:2")?.description).toBe("Second command"); + }); + }); + + describe("context creation", () => { + it("exposes the current abort signal on ExtensionContext", async () => { + const result = await discoverAndLoadExtensions([], tempDir, tempDir); + const runner = new ExtensionRunner(result.extensions, result.runtime, tempDir, sessionManager, modelRegistry); + const controller = new AbortController(); + + runner.bindCore(extensionActions, { + ...extensionContextActions, + getSignal: () => controller.signal, + }); + + const ctx = runner.createContext(); + expect(ctx.signal).toBe(controller.signal); + expect(ctx.signal?.aborted).toBe(false); + + controller.abort(); + expect(ctx.signal?.aborted).toBe(true); + }); + + it("exposes print mode and hasUI false by default", async () => { + const result = await discoverAndLoadExtensions([], tempDir, tempDir); + const runner = new ExtensionRunner(result.extensions, result.runtime, tempDir, sessionManager, modelRegistry); + runner.bindCore(extensionActions, extensionContextActions); + + const ctx = runner.createContext(); + expect(ctx.mode).toBe("print"); + expect(ctx.hasUI).toBe(false); + }); + + it("exposes project trust state on ExtensionContext", async () => { + const result = await discoverAndLoadExtensions([], tempDir, tempDir); + const runner = new ExtensionRunner(result.extensions, result.runtime, tempDir, sessionManager, modelRegistry); + runner.bindCore(extensionActions, { + ...extensionContextActions, + isProjectTrusted: () => false, + }); + + const ctx = runner.createContext(); + expect(ctx.isProjectTrusted()).toBe(false); + }); + + it("exposes rpc mode with hasUI true when an RPC UI context is provided", async () => { + const result = await discoverAndLoadExtensions([], tempDir, tempDir); + const runner = new ExtensionRunner(result.extensions, result.runtime, tempDir, sessionManager, modelRegistry); + runner.bindCore(extensionActions, extensionContextActions); + runner.setUIContext({} as ExtensionUIContext, "rpc"); + + const ctx = runner.createContext(); + expect(ctx.mode).toBe("rpc"); + expect(ctx.hasUI).toBe(true); + }); + + it("exposes tui mode with hasUI true when a TUI UI context is provided", async () => { + const result = await discoverAndLoadExtensions([], tempDir, tempDir); + const runner = new ExtensionRunner(result.extensions, result.runtime, tempDir, sessionManager, modelRegistry); + runner.bindCore(extensionActions, extensionContextActions); + runner.setUIContext({} as ExtensionUIContext, "tui"); + + const ctx = runner.createContext(); + expect(ctx.mode).toBe("tui"); + expect(ctx.hasUI).toBe(true); + }); + }); + + describe("error handling", () => { + it("calls error listeners when handler throws", async () => { + const extCode = ` + export default function(pi) { + pi.on("context", async () => { + throw new Error("Handler error!"); + }); + } + `; + fs.writeFileSync(path.join(extensionsDir, "throws.ts"), extCode); + + const result = await discoverAndLoadExtensions([], tempDir, tempDir); + const runner = new ExtensionRunner(result.extensions, result.runtime, tempDir, sessionManager, modelRegistry); + + const errors: Array<{ extensionPath: string; event: string; error: string }> = []; + runner.onError((err) => { + errors.push(err); + }); + + // Emit context event which will trigger the throwing handler + await runner.emitContext([]); + + expect(errors.length).toBe(1); + expect(errors[0].error).toContain("Handler error!"); + expect(errors[0].event).toBe("context"); + }); + }); + + describe("message and entry renderers", () => { + it("gets Markdown transformers in extension load order", async () => { + const extCode = ` + export default function(pi) { + pi.registerMarkdownTransformer((markdown) => markdown); + } + `; + fs.writeFileSync(path.join(extensionsDir, "markdown-renderer-a.ts"), extCode); + fs.writeFileSync(path.join(extensionsDir, "markdown-renderer-b.ts"), extCode); + + const result = await discoverAndLoadExtensions([], tempDir, tempDir); + const runner = new ExtensionRunner(result.extensions, result.runtime, tempDir, sessionManager, modelRegistry); + + expect(runner.getMarkdownTransformers()).toHaveLength(2); + }); + + it("gets message renderer by type", async () => { + const extCode = ` + export default function(pi) { + pi.registerMessageRenderer("my-type", (message, options, theme) => null); + } + `; + fs.writeFileSync(path.join(extensionsDir, "renderer.ts"), extCode); + + const result = await discoverAndLoadExtensions([], tempDir, tempDir); + const runner = new ExtensionRunner(result.extensions, result.runtime, tempDir, sessionManager, modelRegistry); + + const renderer = runner.getMessageRenderer("my-type"); + expect(renderer).toBeDefined(); + + const missing = runner.getMessageRenderer("not-exists"); + expect(missing).toBeUndefined(); + }); + + it("gets entry renderer by type", async () => { + const extCode = ` + export default function(pi) { + pi.registerEntryRenderer("my-entry", (entry, options, theme) => null); + } + `; + fs.writeFileSync(path.join(extensionsDir, "entry-renderer.ts"), extCode); + + const result = await discoverAndLoadExtensions([], tempDir, tempDir); + const runner = new ExtensionRunner(result.extensions, result.runtime, tempDir, sessionManager, modelRegistry); + + expect(runner.getEntryRenderer("my-entry")).toBeDefined(); + expect(runner.getEntryRenderer("not-exists")).toBeUndefined(); + }); + }); + + describe("flags", () => { + it("collects flags from extensions", async () => { + const extCode = ` + export default function(pi) { + pi.registerFlag("my-flag", { + description: "My flag", + handler: async () => {}, + }); + } + `; + fs.writeFileSync(path.join(extensionsDir, "with-flag.ts"), extCode); + + const result = await discoverAndLoadExtensions([], tempDir, tempDir); + const runner = new ExtensionRunner(result.extensions, result.runtime, tempDir, sessionManager, modelRegistry); + const flags = runner.getFlags(); + + expect(flags.has("my-flag")).toBe(true); + }); + + it("keeps first flag when two extensions register the same name", async () => { + const first = ` + export default function(pi) { + pi.registerFlag("shared-flag", { + description: "first", + type: "boolean", + default: true, + }); + } + `; + const second = ` + export default function(pi) { + pi.registerFlag("shared-flag", { + description: "second", + type: "boolean", + default: false, + }); + } + `; + fs.writeFileSync(path.join(extensionsDir, "a-first.ts"), first); + fs.writeFileSync(path.join(extensionsDir, "b-second.ts"), second); + + const result = await discoverAndLoadExtensions([], tempDir, tempDir); + const runner = new ExtensionRunner(result.extensions, result.runtime, tempDir, sessionManager, modelRegistry); + const flags = runner.getFlags(); + + expect(flags.get("shared-flag")?.description).toBe("first"); + expect(result.runtime.flagValues.get("shared-flag")).toBe(true); + }); + + it("rejects default values that do not match the flag type", async () => { + const extCode = ` + export default function(pi) { + pi.registerFlag("safe-mode", { + type: "boolean", + default: "false", + }); + } + `; + fs.writeFileSync(path.join(extensionsDir, "bad-flag-default.ts"), extCode); + + const result = await discoverAndLoadExtensions([], tempDir, tempDir); + + expect(result.extensions).toHaveLength(0); + expect(result.errors[0]?.error).toContain( + 'Invalid default for flag "safe-mode": expected boolean, got string', + ); + expect(result.runtime.flagValues.has("safe-mode")).toBe(false); + }); + + it("can set flag values", async () => { + const extCode = ` + export default function(pi) { + pi.registerFlag("test-flag", { + description: "Test flag", + handler: async () => {}, + }); + } + `; + fs.writeFileSync(path.join(extensionsDir, "flag.ts"), extCode); + + const result = await discoverAndLoadExtensions([], tempDir, tempDir); + const runner = new ExtensionRunner(result.extensions, result.runtime, tempDir, sessionManager, modelRegistry); + + // Setting a flag value should not throw + runner.setFlagValue("--test-flag", true); + + // The flag values are stored in the shared runtime + expect(result.runtime.flagValues.get("--test-flag")).toBe(true); + }); + }); + + describe("before_agent_start", () => { + it("keeps ctx.getSystemPrompt() in sync with chained system prompt updates", async () => { + const extCode1 = ` + export default function(pi) { + pi.on("before_agent_start", async (_event, ctx) => { + return { + systemPrompt: ctx.getSystemPrompt() + "\\nfirst", + }; + }); + } + `; + const extCode2 = ` + export default function(pi) { + pi.on("before_agent_start", async (_event, ctx) => { + return { + systemPrompt: ctx.getSystemPrompt() + "\\nsecond", + }; + }); + } + `; + fs.writeFileSync(path.join(extensionsDir, "before-agent-start-1.ts"), extCode1); + fs.writeFileSync(path.join(extensionsDir, "before-agent-start-2.ts"), extCode2); + + const result = await discoverAndLoadExtensions([], tempDir, tempDir); + expect(result.errors).toEqual([]); + expect(result.extensions).toHaveLength(2); + const runner = new ExtensionRunner(result.extensions, result.runtime, tempDir, sessionManager, modelRegistry); + const errors: string[] = []; + runner.onError((error) => errors.push(error.error)); + runner.bindCore(extensionActions, extensionContextActions); + + const chained = await runner.emitBeforeAgentStart("hello", undefined, "base", { + cwd: tempDir, + }); + + expect(errors).toEqual([]); + + expect(chained).toEqual({ + messages: undefined, + systemPrompt: "base\nfirst\nsecond", + }); + }); + }); + + describe("tool_result chaining", () => { + it("chains content modifications across handlers", async () => { + const extCode1 = ` + export default function(pi) { + pi.on("tool_result", async (event) => { + return { + content: [...event.content, { type: "text", text: "ext1" }], + }; + }); + } + `; + const extCode2 = ` + export default function(pi) { + pi.on("tool_result", async (event) => { + return { + content: [...event.content, { type: "text", text: "ext2" }], + }; + }); + } + `; + fs.writeFileSync(path.join(extensionsDir, "tool-result-1.ts"), extCode1); + fs.writeFileSync(path.join(extensionsDir, "tool-result-2.ts"), extCode2); + + const result = await discoverAndLoadExtensions([], tempDir, tempDir); + const runner = new ExtensionRunner(result.extensions, result.runtime, tempDir, sessionManager, modelRegistry); + + const chained = await runner.emitToolResult({ + type: "tool_result", + toolName: "my_tool", + toolCallId: "call-1", + input: {}, + content: [{ type: "text", text: "base" }], + details: { initial: true }, + isError: false, + }); + + expect(chained).toBeDefined(); + const chainedContent = chained?.content; + expect(chainedContent).toBeDefined(); + expect(chainedContent![0]).toEqual({ type: "text", text: "base" }); + expect(chainedContent).toHaveLength(3); + const appendedText = chainedContent! + .slice(1) + .filter((item): item is { type: "text"; text: string } => item.type === "text") + .map((item) => item.text); + expect(appendedText.sort()).toEqual(["ext1", "ext2"]); + }); + + it("preserves previous modifications when later handlers return partial patches", async () => { + const extCode1 = ` + export default function(pi) { + pi.on("tool_result", async () => { + return { + content: [{ type: "text", text: "first" }], + details: { source: "ext1" }, + }; + }); + } + `; + const extCode2 = ` + export default function(pi) { + pi.on("tool_result", async () => { + return { + isError: true, + }; + }); + } + `; + fs.writeFileSync(path.join(extensionsDir, "tool-result-partial-1.ts"), extCode1); + fs.writeFileSync(path.join(extensionsDir, "tool-result-partial-2.ts"), extCode2); + + const result = await discoverAndLoadExtensions([], tempDir, tempDir); + const runner = new ExtensionRunner(result.extensions, result.runtime, tempDir, sessionManager, modelRegistry); + + const chained = await runner.emitToolResult({ + type: "tool_result", + toolName: "my_tool", + toolCallId: "call-2", + input: {}, + content: [{ type: "text", text: "base" }], + details: { initial: true }, + isError: false, + }); + + expect(chained).toEqual({ + content: [{ type: "text", text: "first" }], + details: { source: "ext1" }, + isError: true, + }); + }); + }); + + describe("provider registration", () => { + it("bindCore ignores invalid queued registrations and reports extension error", async () => { + const runtime = createExtensionRuntime(); + runtime.registerProvider( + "broken-provider", + { + streamSimple: (() => { + throw new Error("should not run"); + }) as any, + }, + "/tmp/broken-extension.ts", + ); + + const runner = new ExtensionRunner([], runtime, tempDir, sessionManager, modelRegistry); + const errors: string[] = []; + runner.onError((error) => errors.push(`${error.extensionPath}: ${error.error}`)); + + expect(() => runner.bindCore(extensionActions, extensionContextActions)).not.toThrow(); + expect(errors).toEqual([ + '/tmp/broken-extension.ts: Provider broken-provider: "api" is required when registering streamSimple.', + ]); + await expect(modelRegistry.refresh()).resolves.toMatchObject({ aborted: false }); + }); + + it("pre-bind unregister removes all queued registrations for a provider", () => { + const runtime = createExtensionRuntime(); + + runtime.registerProvider("queued-provider", providerModelConfig); + runtime.registerProvider("queued-provider", { + ...providerModelConfig, + models: [ + { + id: "instant-model-2", + name: "Instant Model 2", + reasoning: false, + input: ["text"], + cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 }, + contextWindow: 128000, + maxTokens: 4096, + }, + ], + }); + expect(runtime.pendingProviderRegistrations).toHaveLength(2); + + runtime.unregisterProvider("queued-provider"); + expect(runtime.pendingProviderRegistrations).toHaveLength(0); + }); + + it("post-bind register and unregister take effect immediately", () => { + const runtime = createExtensionRuntime(); + const runner = new ExtensionRunner([], runtime, tempDir, sessionManager, modelRegistry); + + runner.bindCore(extensionActions, extensionContextActions); + expect(runtime.pendingProviderRegistrations).toHaveLength(0); + + runtime.registerProvider("instant-provider", providerModelConfig); + expect(runtime.pendingProviderRegistrations).toHaveLength(0); + expect(modelRegistry.find("instant-provider", "instant-model")?.cost.tiers).toEqual([ + { + inputTokensAbove: 272000, + input: 2, + output: 3, + cacheRead: 0.2, + cacheWrite: 2.5, + }, + ]); + + runtime.unregisterProvider("instant-provider"); + expect(modelRegistry.find("instant-provider", "instant-model")).toBeUndefined(); + }); + }); + + describe("command context", () => { + it("passes fork options through to the bound handler", async () => { + const runtime = createExtensionRuntime(); + const runner = new ExtensionRunner([], runtime, tempDir, sessionManager, modelRegistry); + const fork = vi.fn(async () => ({ cancelled: false })); + + runner.bindCommandContext({ + waitForIdle: async () => {}, + newSession: async () => ({ cancelled: false }), + fork, + navigateTree: async () => ({ cancelled: false }), + switchSession: async () => ({ cancelled: false }), + reload: async () => {}, + }); + + const commandContext = runner.createCommandContext(); + await commandContext.fork("entry-1"); + expect(fork).toHaveBeenCalledWith("entry-1", undefined); + + await commandContext.fork("entry-2", { position: "at" }); + expect(fork).toHaveBeenLastCalledWith("entry-2", { position: "at" }); + }); + }); + + describe("hasHandlers", () => { + it("returns true when handlers exist for event type", async () => { + const extCode = ` + export default function(pi) { + pi.on("tool_call", async () => undefined); + } + `; + fs.writeFileSync(path.join(extensionsDir, "handler.ts"), extCode); + + const result = await discoverAndLoadExtensions([], tempDir, tempDir); + const runner = new ExtensionRunner(result.extensions, result.runtime, tempDir, sessionManager, modelRegistry); + + expect(runner.hasHandlers("tool_call")).toBe(true); + expect(runner.hasHandlers("agent_end")).toBe(false); + }); + }); + + describe("before_provider_headers", () => { + it("lets a handler mutate headers in place and preserves existing headers", async () => { + const extCode = ` + export default function(pi) { + pi.on("before_provider_headers", (event) => { + event.headers["X-Turn-Index"] = "3"; + }); + } + `; + fs.writeFileSync(path.join(extensionsDir, "headers.ts"), extCode); + + const result = await discoverAndLoadExtensions([], tempDir, tempDir); + const runner = new ExtensionRunner(result.extensions, result.runtime, tempDir, sessionManager, modelRegistry); + + expect(runner.hasHandlers("before_provider_headers")).toBe(true); + + const headers = await runner.emitBeforeProviderHeaders({ "User-Agent": "kimchi/1.0" }); + expect(headers["X-Turn-Index"]).toBe("3"); + expect(headers["User-Agent"]).toBe("kimchi/1.0"); + }); + + it("isolates a throwing handler and still applies the others", async () => { + const throwing = ` + export default function(pi) { + pi.on("before_provider_headers", () => { + throw new Error("header handler boom"); + }); + } + `; + const good = ` + export default function(pi) { + pi.on("before_provider_headers", (event) => { + event.headers["X-Good"] = "yes"; + }); + } + `; + fs.writeFileSync(path.join(extensionsDir, "a-throwing.ts"), throwing); + fs.writeFileSync(path.join(extensionsDir, "b-good.ts"), good); + + const result = await discoverAndLoadExtensions([], tempDir, tempDir); + const runner = new ExtensionRunner(result.extensions, result.runtime, tempDir, sessionManager, modelRegistry); + const errors: Array<{ event: string; error: string }> = []; + runner.onError((err) => errors.push(err)); + + const headers = await runner.emitBeforeProviderHeaders({ "User-Agent": "x" }); + + expect(headers["X-Good"]).toBe("yes"); + expect(headers["User-Agent"]).toBe("x"); + expect(errors).toHaveLength(1); + expect(errors[0].event).toBe("before_provider_headers"); + expect(errors[0].error).toContain("header handler boom"); + }); + }); +}); diff --git a/packages/coding-agent/test/external-editor.test.ts b/packages/coding-agent/test/external-editor.test.ts new file mode 100644 index 0000000000000000000000000000000000000000..5cbbc93b3b7a993ee23a1a4a6fe60cb5d7ea68ba --- /dev/null +++ b/packages/coding-agent/test/external-editor.test.ts @@ -0,0 +1,63 @@ +import { existsSync, mkdtempSync, readFileSync, rmSync } from "node:fs"; +import { tmpdir } from "node:os"; +import { basename, dirname, join } from "node:path"; +import { fileURLToPath } from "node:url"; +import { describe, expect, it } from "vitest"; +import { type ExternalEditorResult, editInExternalEditor } from "../src/modes/interactive/external-editor.ts"; + +const editorFixturePath = fileURLToPath(new URL("./fixtures/fake-external-editor.mjs", import.meta.url)); + +interface EditorCapture { + filePath: string; + content: string; + entries: string[]; + directoryMode: number; +} + +async function runExternalEditor(fixtureFlag?: "--fail" | "--empty"): Promise<{ + result: ExternalEditorResult; + capture: EditorCapture; +}> { + const testDirectory = mkdtempSync(join(tmpdir(), "pi-external-editor-test-")); + const capturePath = join(testDirectory, "capture.json"); + try { + const result = await editInExternalEditor({ + command: `${process.execPath} ${editorFixturePath} ${capturePath}${fixtureFlag ? ` ${fixtureFlag}` : ""}`, + content: "original", + }); + const capture = JSON.parse(readFileSync(capturePath, "utf-8")) as EditorCapture; + return { result, capture }; + } finally { + rmSync(testDirectory, { recursive: true, force: true }); + } +} + +describe("editInExternalEditor", () => { + it("edits a prompt inside a private temporary directory", async () => { + const { result, capture } = await runExternalEditor(); + const directory = dirname(capture.filePath); + + expect(result).toEqual({ status: "complete", content: "edited" }); + expect(dirname(directory)).toBe(tmpdir()); + expect(basename(directory)).toMatch(/^pi-editor-.+$/); + expect(basename(capture.filePath)).toBe("prompt.md"); + expect(capture.entries).toEqual(["prompt.md"]); + expect(capture.content).toBe("original"); + if (process.platform !== "win32") { + expect(capture.directoryMode & 0o077).toBe(0); + } + expect(existsSync(directory)).toBe(false); + }); + + it("keeps the original content when the editor exits unsuccessfully", async () => { + const { result, capture } = await runExternalEditor("--fail"); + + expect(result).toEqual({ status: "failed" }); + expect(existsSync(dirname(capture.filePath))).toBe(false); + }); + it("returns empty content when the editor clears the prompt", async () => { + const { result } = await runExternalEditor("--empty"); + + expect(result).toEqual({ status: "complete", content: "" }); + }); +}); diff --git a/packages/coding-agent/test/file-mutation-queue.test.ts b/packages/coding-agent/test/file-mutation-queue.test.ts new file mode 100644 index 0000000000000000000000000000000000000000..8e13a45e62e7d3ffc5251575dc14a10639905d79 --- /dev/null +++ b/packages/coding-agent/test/file-mutation-queue.test.ts @@ -0,0 +1,274 @@ +import { access, mkdtemp, readFile, rm, symlink, writeFile } from "node:fs/promises"; +import { tmpdir } from "node:os"; +import { join } from "node:path"; +import { afterEach, describe, expect, it } from "vitest"; +import { createEditTool } from "../src/core/tools/edit.ts"; +import { withFileMutationQueue } from "../src/core/tools/file-mutation-queue.ts"; +import { createWriteTool } from "../src/core/tools/write.ts"; + +function delay(ms: number): Promise { + return new Promise((resolve) => setTimeout(resolve, ms)); +} + +function createDeferred(): { promise: Promise; resolve: () => void } { + let resolve!: () => void; + const promise = new Promise((promiseResolve) => { + resolve = promiseResolve; + }); + return { promise, resolve }; +} + +async function resolvesWithin(promise: Promise, ms: number): Promise { + return Promise.race([promise.then(() => true), delay(ms).then(() => false)]); +} + +const tempDirs: string[] = []; + +async function createTempDir(): Promise { + const dir = await mkdtemp(join(tmpdir(), "pi-file-mutation-queue-")); + tempDirs.push(dir); + return dir; +} + +afterEach(async () => { + await Promise.all(tempDirs.splice(0, tempDirs.length).map((dir) => rm(dir, { recursive: true, force: true }))); +}); + +describe("withFileMutationQueue", () => { + it("serializes operations for the same file", async () => { + const order: string[] = []; + const path = "/tmp/file-mutation-queue-same"; + + const first = withFileMutationQueue(path, async () => { + order.push("first:start"); + await delay(30); + order.push("first:end"); + }); + const second = withFileMutationQueue(path, async () => { + order.push("second:start"); + order.push("second:end"); + }); + + await Promise.all([first, second]); + expect(order).toEqual(["first:start", "first:end", "second:start", "second:end"]); + }); + + it("allows different files to proceed in parallel", async () => { + const order: string[] = []; + + await Promise.all([ + withFileMutationQueue("/tmp/file-mutation-queue-a", async () => { + order.push("a:start"); + await delay(30); + order.push("a:end"); + }), + withFileMutationQueue("/tmp/file-mutation-queue-b", async () => { + order.push("b:start"); + await delay(30); + order.push("b:end"); + }), + ]); + + expect(order.indexOf("a:start")).toBeLessThan(order.indexOf("a:end")); + expect(order.indexOf("b:start")).toBeLessThan(order.indexOf("b:end")); + expect(order.indexOf("b:start")).toBeLessThan(order.indexOf("a:end")); + }); + + it("uses the same queue for symlink aliases", async () => { + const dir = await createTempDir(); + const targetPath = join(dir, "target.txt"); + const symlinkPath = join(dir, "alias.txt"); + await writeFile(targetPath, "hello\n", "utf8"); + await symlink(targetPath, symlinkPath); + + const order: string[] = []; + await Promise.all([ + withFileMutationQueue(targetPath, async () => { + order.push("target:start"); + await delay(30); + order.push("target:end"); + }), + withFileMutationQueue(symlinkPath, async () => { + order.push("alias:start"); + order.push("alias:end"); + }), + ]); + + expect(order).toEqual(["target:start", "target:end", "alias:start", "alias:end"]); + }); +}); + +describe("built-in edit and write tools", () => { + it("preserves both parallel edits on the same file", async () => { + const dir = await createTempDir(); + const filePath = join(dir, "parallel-edit.txt"); + await writeFile(filePath, "alpha\nbeta\ngamma\n", "utf8"); + + const editTool = createEditTool(dir, { + operations: { + access, + readFile: async (path) => { + const buffer = await readFile(path); + await delay(30); + return buffer; + }, + writeFile: async (path, content) => { + await delay(30); + await writeFile(path, content, "utf8"); + }, + }, + }); + + await Promise.all([ + editTool.execute("call-1", { path: filePath, edits: [{ oldText: "alpha", newText: "ALPHA" }] }), + editTool.execute("call-2", { path: filePath, edits: [{ oldText: "beta", newText: "BETA" }] }), + ]); + + const content = await readFile(filePath, "utf8"); + expect(content).toBe("ALPHA\nBETA\ngamma\n"); + }); + + it("shares the queue between edit and write", async () => { + const dir = await createTempDir(); + const filePath = join(dir, "mixed.txt"); + await writeFile(filePath, "original\n", "utf8"); + + const editTool = createEditTool(dir, { + operations: { + access, + readFile: async (path) => { + const buffer = await readFile(path); + await delay(30); + return buffer; + }, + writeFile: async (path, content) => { + await delay(30); + await writeFile(path, content, "utf8"); + }, + }, + }); + const writeTool = createWriteTool(dir, { + operations: { + mkdir: async () => {}, + writeFile: async (path, content) => { + await delay(10); + await writeFile(path, content, "utf8"); + }, + }, + }); + + const editPromise = editTool.execute("call-1", { + path: filePath, + edits: [{ oldText: "original", newText: "edited" }], + }); + await delay(5); + const writePromise = writeTool.execute("call-2", { + path: filePath, + content: "replacement\n", + }); + + await Promise.all([editPromise, writePromise]); + + const content = await readFile(filePath, "utf8"); + expect(content).toBe("replacement\n"); + }); + + it("keeps write queue locked while an aborted write is still in flight", async () => { + const dir = await createTempDir(); + const filePath = join(dir, "abort-write.txt"); + const firstWriteStarted = createDeferred(); + const finishFirstWrite = createDeferred(); + const secondWriteStarted = createDeferred(); + let firstWriteSettled = false; + + const writeTool = createWriteTool(dir, { + operations: { + mkdir: async () => {}, + writeFile: async (path, content) => { + if (content === "first\n") { + firstWriteStarted.resolve(); + await finishFirstWrite.promise; + await writeFile(path, content, "utf8"); + firstWriteSettled = true; + return; + } + + if (content === "second\n") { + expect(firstWriteSettled).toBe(true); + secondWriteStarted.resolve(); + } + await writeFile(path, content, "utf8"); + }, + }, + }); + + const controller = new AbortController(); + const firstWrite = writeTool.execute("call-1", { path: filePath, content: "first\n" }, controller.signal); + await firstWriteStarted.promise; + controller.abort(); + + const secondWrite = writeTool.execute("call-2", { path: filePath, content: "second\n" }); + expect(await resolvesWithin(secondWriteStarted.promise, 20)).toBe(false); + + finishFirstWrite.resolve(); + await expect(firstWrite).rejects.toThrow("Operation aborted"); + await secondWrite; + + const content = await readFile(filePath, "utf8"); + expect(content).toBe("second\n"); + }); + + it("keeps edit queue locked while an aborted edit write is still in flight", async () => { + const dir = await createTempDir(); + const filePath = join(dir, "abort-edit.txt"); + await writeFile(filePath, "alpha\nbeta\n", "utf8"); + const firstWriteStarted = createDeferred(); + const finishFirstWrite = createDeferred(); + const secondWriteStarted = createDeferred(); + let firstWriteSettled = false; + + const editTool = createEditTool(dir, { + operations: { + access, + readFile, + writeFile: async (path, content) => { + if (content === "ALPHA\nbeta\n") { + firstWriteStarted.resolve(); + await finishFirstWrite.promise; + await writeFile(path, content, "utf8"); + firstWriteSettled = true; + return; + } + + if (content === "ALPHA\nBETA\n" || content === "alpha\nBETA\n") { + expect(firstWriteSettled).toBe(true); + secondWriteStarted.resolve(); + } + await writeFile(path, content, "utf8"); + }, + }, + }); + + const controller = new AbortController(); + const firstEdit = editTool.execute( + "call-1", + { path: filePath, edits: [{ oldText: "alpha", newText: "ALPHA" }] }, + controller.signal, + ); + await firstWriteStarted.promise; + controller.abort(); + + const secondEdit = editTool.execute("call-2", { + path: filePath, + edits: [{ oldText: "beta", newText: "BETA" }], + }); + expect(await resolvesWithin(secondWriteStarted.promise, 20)).toBe(false); + + finishFirstWrite.resolve(); + await expect(firstEdit).rejects.toThrow("Operation aborted"); + await secondEdit; + + const content = await readFile(filePath, "utf8"); + expect(content).toBe("ALPHA\nBETA\n"); + }); +}); diff --git a/packages/coding-agent/test/first-time-setup-fork.test.ts b/packages/coding-agent/test/first-time-setup-fork.test.ts new file mode 100644 index 0000000000000000000000000000000000000000..e9b892071659be45d7e98ef6a5824d567f414660 --- /dev/null +++ b/packages/coding-agent/test/first-time-setup-fork.test.ts @@ -0,0 +1,39 @@ +import { mkdtempSync, rmSync } from "fs"; +import { tmpdir } from "os"; +import { join } from "path"; +import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; + +vi.mock("../src/config.ts", async (importOriginal) => { + const actual = await importOriginal(); + return { + ...(actual as Record), + PACKAGE_NAME: "@example/pi-coding-agent", + }; +}); + +import { shouldRunFirstTimeSetup } from "../src/cli/startup-ui.ts"; + +describe("shouldRunFirstTimeSetup in forked distributions", () => { + const originalPiExperimental = process.env.PI_EXPERIMENTAL; + let tempDir: string; + let settingsPath: string; + + beforeEach(() => { + tempDir = mkdtempSync(join(tmpdir(), "pi-first-time-setup-fork-")); + settingsPath = join(tempDir, "settings.json"); + process.env.PI_EXPERIMENTAL = "1"; + }); + + afterEach(() => { + rmSync(tempDir, { recursive: true, force: true }); + if (originalPiExperimental === undefined) { + delete process.env.PI_EXPERIMENTAL; + } else { + process.env.PI_EXPERIMENTAL = originalPiExperimental; + } + }); + + it("returns false for a forked package", () => { + expect(shouldRunFirstTimeSetup(settingsPath)).toBe(false); + }); +}); diff --git a/packages/coding-agent/test/first-time-setup.test.ts b/packages/coding-agent/test/first-time-setup.test.ts new file mode 100644 index 0000000000000000000000000000000000000000..c470e6dccf74077602285dfec74f0efb139176e0 --- /dev/null +++ b/packages/coding-agent/test/first-time-setup.test.ts @@ -0,0 +1,95 @@ +import { mkdtempSync, rmSync, writeFileSync } from "fs"; +import { tmpdir } from "os"; +import { join } from "path"; +import { afterEach, beforeEach, describe, expect, it } from "vitest"; +import { shouldRunFirstTimeSetup } from "../src/cli/startup-ui.ts"; +import { ENV_AGENT_DIR } from "../src/config.ts"; +import { SettingsManager } from "../src/core/settings-manager.ts"; + +describe("shouldRunFirstTimeSetup", () => { + const originalPiExperimental = process.env.PI_EXPERIMENTAL; + const originalAgentDir = process.env[ENV_AGENT_DIR]; + let tempDir: string; + let settingsPath: string; + + beforeEach(() => { + tempDir = mkdtempSync(join(tmpdir(), "pi-first-time-setup-")); + settingsPath = join(tempDir, "settings.json"); + process.env.PI_EXPERIMENTAL = "1"; + delete process.env[ENV_AGENT_DIR]; + }); + + afterEach(() => { + rmSync(tempDir, { recursive: true, force: true }); + if (originalPiExperimental === undefined) { + delete process.env.PI_EXPERIMENTAL; + } else { + process.env.PI_EXPERIMENTAL = originalPiExperimental; + } + if (originalAgentDir === undefined) { + delete process.env[ENV_AGENT_DIR]; + } else { + process.env[ENV_AGENT_DIR] = originalAgentDir; + } + }); + + it("returns true when experimental, default agent dir, and no settings.json", () => { + expect(shouldRunFirstTimeSetup(settingsPath)).toBe(true); + }); + + it("returns false when experimental features are disabled", () => { + delete process.env.PI_EXPERIMENTAL; + + expect(shouldRunFirstTimeSetup(settingsPath)).toBe(false); + }); + + it("returns false when a custom agent dir is set", () => { + process.env[ENV_AGENT_DIR] = tempDir; + + expect(shouldRunFirstTimeSetup(settingsPath)).toBe(false); + }); + + it("returns false when settings.json already exists", () => { + writeFileSync(settingsPath, "{}", "utf-8"); + + expect(shouldRunFirstTimeSetup(settingsPath)).toBe(false); + }); +}); + +describe("analytics settings", () => { + it("defaults to disabled with no tracking identifier", () => { + const manager = SettingsManager.inMemory(); + + expect(manager.getEnableAnalytics()).toBe(false); + expect(manager.getTrackingId()).toBeUndefined(); + }); + + it("generates a tracking identifier on opt-in", () => { + const manager = SettingsManager.inMemory(); + + manager.setEnableAnalytics(true); + + expect(manager.getEnableAnalytics()).toBe(true); + expect(manager.getTrackingId()).toMatch(/^[0-9a-f-]{36}$/); + }); + + it("does not generate a tracking identifier on opt-out", () => { + const manager = SettingsManager.inMemory(); + + manager.setEnableAnalytics(false); + + expect(manager.getEnableAnalytics()).toBe(false); + expect(manager.getTrackingId()).toBeUndefined(); + }); + + it("keeps the tracking identifier when toggling analytics", () => { + const manager = SettingsManager.inMemory(); + + manager.setEnableAnalytics(true); + const trackingId = manager.getTrackingId(); + manager.setEnableAnalytics(false); + manager.setEnableAnalytics(true); + + expect(manager.getTrackingId()).toBe(trackingId); + }); +}); diff --git a/packages/coding-agent/test/footer-data-provider.test.ts b/packages/coding-agent/test/footer-data-provider.test.ts new file mode 100644 index 0000000000000000000000000000000000000000..4fead82187620673a76c524be54dc485545dd3ec --- /dev/null +++ b/packages/coding-agent/test/footer-data-provider.test.ts @@ -0,0 +1,278 @@ +import { execFile, spawnSync } from "child_process"; +import { existsSync, type FSWatcher, mkdirSync, mkdtempSync, rmSync, writeFileSync } from "fs"; +import { tmpdir } from "os"; +import { join } from "path"; +import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; + +let resolvedBranch = "main"; + +vi.mock("child_process", () => ({ + execFile: vi.fn( + ( + _command: string, + args: readonly string[], + _options: unknown, + callback: (error: Error | null, stdout: string, stderr: string) => void, + ) => { + if (args[1] === "symbolic-ref") { + setTimeout( + () => + callback( + resolvedBranch ? null : new Error("detached"), + resolvedBranch ? `${resolvedBranch}\n` : "", + "", + ), + 0, + ); + return; + } + setTimeout(() => callback(new Error("unsupported"), "", ""), 0); + }, + ), + spawnSync: vi.fn((_command: string, args: readonly string[]) => { + if (args[1] === "symbolic-ref") { + return { status: resolvedBranch ? 0 : 1, stdout: resolvedBranch ? `${resolvedBranch}\n` : "", stderr: "" }; + } + return { status: 1, stdout: "", stderr: "" }; + }), +})); + +import { FooterDataProvider } from "../src/core/footer-data-provider.ts"; + +type WorktreeFixture = { + worktreeDir: string; + reftableDir: string; +}; + +function createPlainReftableRepo(tempDir: string): string { + const repoDir = join(tempDir, "repo"); + mkdirSync(join(repoDir, ".git", "reftable"), { recursive: true }); + writeFileSync(join(repoDir, ".git", "HEAD"), "ref: refs/heads/.invalid\n"); + return repoDir; +} + +function createPlainRepo(tempDir: string): string { + const repoDir = join(tempDir, "repo"); + mkdirSync(join(repoDir, ".git"), { recursive: true }); + writeFileSync(join(repoDir, ".git", "HEAD"), "ref: refs/heads/main\n"); + return repoDir; +} + +function createReftableWorktree(tempDir: string): WorktreeFixture { + const repoDir = join(tempDir, "repo"); + const commonGitDir = join(repoDir, ".git"); + const gitDir = join(commonGitDir, "worktrees", "src"); + const worktreeDir = join(tempDir, "worktree"); + const reftableDir = join(commonGitDir, "reftable"); + + mkdirSync(gitDir, { recursive: true }); + mkdirSync(reftableDir, { recursive: true }); + mkdirSync(worktreeDir, { recursive: true }); + + writeFileSync(join(worktreeDir, ".git"), `gitdir: ${gitDir}\n`); + writeFileSync(join(gitDir, "HEAD"), "ref: refs/heads/.invalid\n"); + writeFileSync(join(gitDir, "commondir"), "../..\n"); + writeFileSync(join(reftableDir, "tables.list"), "0\n"); + + return { worktreeDir, reftableDir }; +} + +function emitReftableChange(provider: FooterDataProvider): void { + const { reftableWatcher } = provider as unknown as { reftableWatcher: FSWatcher | null }; + expect(reftableWatcher).not.toBeNull(); + reftableWatcher?.emit("change", "change", "tables.list"); +} + +async function waitFor(condition: () => boolean, timeoutMs = 3000): Promise { + const startedAt = Date.now(); + while (!condition()) { + if (Date.now() - startedAt > timeoutMs) { + throw new Error("Timed out waiting for condition"); + } + await new Promise((resolve) => setTimeout(resolve, 10)); + } +} + +describe("FooterDataProvider reftable branch detection", () => { + let originalCwd: string; + let tempDir: string; + + beforeEach(() => { + originalCwd = process.cwd(); + tempDir = mkdtempSync(join(tmpdir(), "footer-data-provider-")); + resolvedBranch = "main"; + vi.mocked(spawnSync).mockClear(); + vi.mocked(execFile).mockClear(); + }); + + afterEach(() => { + process.chdir(originalCwd); + if (tempDir && existsSync(tempDir)) { + rmSync(tempDir, { recursive: true, force: true }); + } + }); + + it("uses HEAD directly in a regular repo from a nested directory", () => { + const repoDir = createPlainRepo(tempDir); + const nestedDir = join(repoDir, "src", "nested"); + mkdirSync(nestedDir, { recursive: true }); + process.chdir(nestedDir); + + const provider = new FooterDataProvider(nestedDir); + try { + expect(provider.getGitBranch()).toBe("main"); + expect(vi.mocked(spawnSync)).not.toHaveBeenCalled(); + } finally { + provider.dispose(); + } + }); + + it("resolves the branch via git when HEAD is .invalid in a reftable repo", () => { + const repoDir = createPlainReftableRepo(tempDir); + process.chdir(repoDir); + + const provider = new FooterDataProvider(repoDir); + try { + expect(provider.getGitBranch()).toBe("main"); + expect(vi.mocked(spawnSync)).toHaveBeenCalledWith( + "git", + ["--no-optional-locks", "symbolic-ref", "--quiet", "--short", "HEAD"], + expect.objectContaining({ + cwd: expect.stringMatching(/repo$/), + encoding: "utf8", + stdio: ["ignore", "pipe", "ignore"], + }), + ); + } finally { + provider.dispose(); + } + }); + + it("resolves the branch via git in a reftable-backed worktree", () => { + const { worktreeDir } = createReftableWorktree(tempDir); + process.chdir(worktreeDir); + + const provider = new FooterDataProvider(worktreeDir); + try { + expect(provider.getGitBranch()).toBe("main"); + } finally { + provider.dispose(); + } + }); + + it("treats an unresolved .invalid reftable HEAD as detached", () => { + const repoDir = createPlainReftableRepo(tempDir); + process.chdir(repoDir); + resolvedBranch = ""; + + const provider = new FooterDataProvider(repoDir); + try { + expect(provider.getGitBranch()).toBe("detached"); + } finally { + provider.dispose(); + } + }); + + // Drive debounce behavior explicitly; native fs.watch delivery can race watcher startup. + it("does not notify listeners when reftable updates keep the same branch", async () => { + vi.useFakeTimers(); + const { worktreeDir } = createReftableWorktree(tempDir); + process.chdir(worktreeDir); + + const provider = new FooterDataProvider(worktreeDir); + try { + expect(provider.getGitBranch()).toBe("main"); + vi.mocked(spawnSync).mockClear(); + const onBranchChange = vi.fn(); + provider.onBranchChange(onBranchChange); + + emitReftableChange(provider); + await vi.advanceTimersByTimeAsync(501); + + expect(vi.mocked(execFile)).toHaveBeenCalledTimes(1); + expect(vi.mocked(spawnSync)).not.toHaveBeenCalled(); + expect(provider.getGitBranch()).toBe("main"); + expect(onBranchChange).not.toHaveBeenCalled(); + } finally { + provider.dispose(); + vi.useRealTimers(); + } + }); + + it("debounces rapid reftable updates into a single async refresh", async () => { + vi.useFakeTimers(); + const { worktreeDir } = createReftableWorktree(tempDir); + process.chdir(worktreeDir); + + const provider = new FooterDataProvider(worktreeDir); + try { + expect(provider.getGitBranch()).toBe("main"); + vi.mocked(execFile).mockClear(); + + emitReftableChange(provider); + emitReftableChange(provider); + emitReftableChange(provider); + await vi.advanceTimersByTimeAsync(499); + expect(vi.mocked(execFile)).not.toHaveBeenCalled(); + await vi.advanceTimersByTimeAsync(2); + expect(vi.mocked(execFile)).toHaveBeenCalledTimes(1); + await vi.advanceTimersByTimeAsync(650); + expect(vi.mocked(execFile)).toHaveBeenCalledTimes(1); + } finally { + provider.dispose(); + vi.useRealTimers(); + } + }); + + it("updates the cached branch when the reftable directory changes", async () => { + const { worktreeDir, reftableDir } = createReftableWorktree(tempDir); + process.chdir(worktreeDir); + + const provider = new FooterDataProvider(worktreeDir); + try { + expect(provider.getGitBranch()).toBe("main"); + resolvedBranch = "foo"; + const onBranchChange = vi.fn(); + provider.onBranchChange(onBranchChange); + + writeFileSync(join(reftableDir, "tables.list"), "1\n"); + await waitFor(() => vi.mocked(execFile).mock.calls.length === 1); + await waitFor(() => provider.getGitBranch() === "foo"); + + expect(vi.mocked(execFile)).toHaveBeenCalledTimes(1); + expect(provider.getGitBranch()).toBe("foo"); + expect(onBranchChange).toHaveBeenCalledTimes(1); + } finally { + provider.dispose(); + } + }); + + it("retries git watchers 5 seconds after an async fs.watch error", async () => { + vi.useFakeTimers(); + const repoDir = createPlainRepo(tempDir); + process.chdir(repoDir); + + const provider = new FooterDataProvider(repoDir); + try { + const providerWithInternals = provider as unknown as { + headWatcher: FSWatcher | null; + }; + const originalWatcher = providerWithInternals.headWatcher; + expect(originalWatcher).not.toBeNull(); + expect(originalWatcher?.listenerCount("error")).toBeGreaterThan(0); + + originalWatcher?.emit("error", new Error("simulated EMFILE")); + expect(providerWithInternals.headWatcher).toBeNull(); + + await vi.advanceTimersByTimeAsync(4999); + expect(providerWithInternals.headWatcher).toBeNull(); + + await vi.advanceTimersByTimeAsync(1); + expect(providerWithInternals.headWatcher).not.toBeNull(); + expect(providerWithInternals.headWatcher).not.toBe(originalWatcher); + } finally { + provider.dispose(); + vi.useRealTimers(); + } + }); +}); diff --git a/packages/coding-agent/test/footer-width.test.ts b/packages/coding-agent/test/footer-width.test.ts new file mode 100644 index 0000000000000000000000000000000000000000..03db5b67918b256d70380cd35aedf55e4362e1d3 --- /dev/null +++ b/packages/coding-agent/test/footer-width.test.ts @@ -0,0 +1,252 @@ +import { visibleWidth } from "@earendil-works/pi-tui"; +import { beforeAll, describe, expect, it } from "vitest"; +import type { AgentSession } from "../src/core/agent-session.ts"; +import type { ReadonlyFooterDataProvider } from "../src/core/footer-data-provider.ts"; +import { FooterComponent, formatCwdForFooter } from "../src/modes/interactive/components/footer.ts"; +import { initTheme } from "../src/modes/interactive/theme/theme.ts"; +import { stripAnsi } from "../src/utils/ansi.ts"; + +type AssistantUsage = { + input: number; + output: number; + cacheRead: number; + cacheWrite: number; + cost: { total: number }; +}; + +function createSession(options: { + sessionName: string; + modelId?: string; + provider?: string; + reasoning?: boolean; + thinkingLevel?: string; + usage?: AssistantUsage; + branchUsage?: AssistantUsage; + compactionUsage?: AssistantUsage; + toolUsage?: AssistantUsage; + usingSubscription?: boolean; +}): AgentSession { + const usage = options.usage; + const entries: Array> = []; + + if (usage !== undefined) { + entries.push({ + type: "message", + message: { + role: "assistant", + usage, + }, + }); + } + + if (options.branchUsage !== undefined) { + entries.push({ + type: "branch_summary", + usage: options.branchUsage, + }); + } + + if (options.compactionUsage !== undefined) { + entries.push({ + type: "compaction", + usage: options.compactionUsage, + }); + } + + if (options.toolUsage !== undefined) { + entries.push({ + type: "message", + message: { + role: "toolResult", + usage: options.toolUsage, + }, + }); + } + + const session = { + state: { + model: { + id: options.modelId ?? "test-model", + provider: options.provider ?? "test", + contextWindow: 200_000, + reasoning: options.reasoning ?? false, + }, + thinkingLevel: options.thinkingLevel ?? "off", + }, + sessionManager: { + getEntries: () => entries, + getSessionName: () => options.sessionName, + getCwd: () => "/tmp/project", + }, + getContextUsage: () => ({ contextWindow: 200_000, percent: 12.3 }), + modelRuntime: { + isUsingSubscription: () => options.usingSubscription ?? false, + }, + }; + + return session as unknown as AgentSession; +} + +function createFooterData(providerCount: number): ReadonlyFooterDataProvider { + const provider = { + getGitBranch: () => "main", + getExtensionStatuses: () => new Map(), + getAvailableProviderCount: () => providerCount, + onBranchChange: (callback: () => void) => { + void callback; + return () => {}; + }, + }; + + return provider; +} + +describe("formatCwdForFooter", () => { + it("does not abbreviate sibling paths that share the home prefix", () => { + expect(formatCwdForFooter("/home/user2", "/home/user")).toBe("/home/user2"); + }); + + it("abbreviates the home directory and descendants", () => { + expect(formatCwdForFooter("/home/user", "/home/user")).toBe("~"); + expect(formatCwdForFooter("/home/user/project", "/home/user")).toBe("~/project"); + }); +}); + +describe("FooterComponent width handling", () => { + beforeAll(() => { + initTheme(undefined, false); + }); + + it("keeps all lines within width for wide session names", () => { + const width = 93; + const session = createSession({ sessionName: "한글".repeat(30) }); + const footer = new FooterComponent(session, createFooterData(1)); + + const lines = footer.render(width); + for (const line of lines) { + expect(visibleWidth(line)).toBeLessThanOrEqual(width); + } + }); + + it("keeps stats line within width for wide model and provider names", () => { + const width = 60; + const session = createSession({ + sessionName: "", + modelId: "模".repeat(30), + provider: "공급자", + reasoning: true, + thinkingLevel: "high", + usage: { + input: 12_345, + output: 6_789, + cacheRead: 0, + cacheWrite: 0, + cost: { total: 1.234 }, + }, + }); + const footer = new FooterComponent(session, createFooterData(2)); + + const lines = footer.render(width); + for (const line of lines) { + expect(visibleWidth(line)).toBeLessThanOrEqual(width); + } + }); + + it("includes summary and tool result usage in the total cost", () => { + const session = createSession({ + sessionName: "", + usage: { + input: 100, + output: 10, + cacheRead: 0, + cacheWrite: 0, + cost: { total: 0.5 }, + }, + branchUsage: { + input: 20, + output: 5, + cacheRead: 0, + cacheWrite: 0, + cost: { total: 0.25 }, + }, + compactionUsage: { + input: 5, + output: 2, + cacheRead: 0, + cacheWrite: 0, + cost: { total: 0.125 }, + }, + toolUsage: { + input: 15, + output: 3, + cacheRead: 0, + cacheWrite: 0, + cost: { total: 0.375 }, + }, + }); + const footer = new FooterComponent(session, createFooterData(1)); + + const statsLine = stripAnsi(footer.render(120)[1]); + expect(statsLine).toContain("$1.250"); + }); + + it("shows the latest cache hit rate when cache usage is present", () => { + const session = createSession({ + sessionName: "", + usage: { + input: 100, + output: 10, + cacheRead: 50, + cacheWrite: 50, + cost: { total: 0.001 }, + }, + }); + const footer = new FooterComponent(session, createFooterData(1)); + + const statsLine = stripAnsi(footer.render(120)[1]); + expect(statsLine).toContain("CH25.0%"); + }); + + it("marks Kimi Coding costs as subscription estimates", () => { + const session = createSession({ + sessionName: "", + provider: "kimi-coding", + usage: { + input: 100, + output: 10, + cacheRead: 0, + cacheWrite: 0, + cost: { total: 1.234 }, + }, + }); + const footer = new FooterComponent(session, createFooterData(1)); + + expect(stripAnsi(footer.render(120)[1])).toContain("$1.234 (sub)"); + }); + + it("marks explicitly identified subscription auth", () => { + const session = createSession({ sessionName: "", provider: "anthropic", usingSubscription: true }); + const footer = new FooterComponent(session, createFooterData(1)); + + expect(stripAnsi(footer.render(120)[1])).toContain("$0.000 (sub)"); + }); + + it("does not mark generic OAuth sign-in as a subscription", () => { + const session = createSession({ + sessionName: "", + provider: "openrouter", + usage: { + input: 100, + output: 10, + cacheRead: 0, + cacheWrite: 0, + cost: { total: 1.234 }, + }, + }); + const footer = new FooterComponent(session, createFooterData(1)); + const stats = stripAnsi(footer.render(120)[1]); + + expect(stats).toContain("$1.234"); + expect(stats).not.toContain("(sub)"); + }); +}); diff --git a/packages/coding-agent/test/format-resume-command.test.ts b/packages/coding-agent/test/format-resume-command.test.ts new file mode 100644 index 0000000000000000000000000000000000000000..86c4475b5cd078f66116a9bf98c930ef3ef17221 --- /dev/null +++ b/packages/coding-agent/test/format-resume-command.test.ts @@ -0,0 +1,135 @@ +import { mkdtempSync, rmSync, writeFileSync } from "node:fs"; +import { tmpdir } from "node:os"; +import { join } from "node:path"; +import { afterEach, describe, expect, it } from "vitest"; +import { APP_NAME } from "../src/config.ts"; +import type { SessionManager } from "../src/core/session-manager.ts"; +import { formatResumeCommand } from "../src/modes/interactive/interactive-mode.ts"; + +const tempDirs: string[] = []; +const originalStdoutIsTTY = Object.getOwnPropertyDescriptor(process.stdout, "isTTY"); + +afterEach(() => { + if (originalStdoutIsTTY) { + Object.defineProperty(process.stdout, "isTTY", originalStdoutIsTTY); + } else { + Reflect.deleteProperty(process.stdout, "isTTY"); + } + + for (const dir of tempDirs.splice(0)) { + rmSync(dir, { recursive: true, force: true }); + } +}); + +function setStdoutIsTTY(value: boolean): void { + Object.defineProperty(process.stdout, "isTTY", { configurable: true, value }); +} + +function createTempFile(): string { + const dir = mkdtempSync(join(tmpdir(), "pi-format-resume-command-")); + tempDirs.push(dir); + const file = join(dir, "session.jsonl"); + writeFileSync(file, "\n"); + return file; +} + +function createSessionManager(options: { + persisted?: boolean; + sessionFile?: string; + sessionId?: string; + sessionDir?: string; + usesDefaultSessionDir?: boolean; +}): SessionManager { + return { + isPersisted: () => options.persisted ?? true, + getSessionFile: () => options.sessionFile, + getSessionId: () => options.sessionId ?? "0197f6e4-4cf9-7f44-a2d8-f8f7f49ee9d3", + getSessionDir: () => options.sessionDir ?? "/tmp/pi-sessions", + usesDefaultSessionDir: () => options.usesDefaultSessionDir ?? true, + } as unknown as SessionManager; +} + +describe("formatResumeCommand", () => { + it("returns a session resume command for default session dirs", () => { + setStdoutIsTTY(true); + const sessionFile = createTempFile(); + const sessionManager = createSessionManager({ sessionFile, sessionId: "test-session" }); + + expect(formatResumeCommand(sessionManager)).toBe(`${APP_NAME} --session test-session`); + }); + + it("includes unquoted safe session dirs for non-default session dirs", () => { + setStdoutIsTTY(true); + const sessionFile = createTempFile(); + const sessionManager = createSessionManager({ + sessionFile, + sessionId: "test-session", + sessionDir: "/tmp/custom-pi-sessions", + usesDefaultSessionDir: false, + }); + + expect(formatResumeCommand(sessionManager)).toBe( + `${APP_NAME} --session-dir /tmp/custom-pi-sessions --session test-session`, + ); + }); + + it("quotes session dirs containing spaces", () => { + setStdoutIsTTY(true); + const sessionFile = createTempFile(); + const sessionManager = createSessionManager({ + sessionFile, + sessionId: "test-session", + sessionDir: "/tmp/custom pi sessions", + usesDefaultSessionDir: false, + }); + + expect(formatResumeCommand(sessionManager)).toBe( + `${APP_NAME} --session-dir '/tmp/custom pi sessions' --session test-session`, + ); + }); + + it("quotes session dirs containing single quotes", () => { + setStdoutIsTTY(true); + const sessionFile = createTempFile(); + const sessionManager = createSessionManager({ + sessionFile, + sessionId: "test-session", + sessionDir: "/tmp/custom pi's sessions", + usesDefaultSessionDir: false, + }); + + expect(formatResumeCommand(sessionManager)).toBe( + `${APP_NAME} --session-dir '/tmp/custom pi'\\''s sessions' --session test-session`, + ); + }); + + it("returns undefined when stdout is not a TTY", () => { + setStdoutIsTTY(false); + const sessionFile = createTempFile(); + const sessionManager = createSessionManager({ sessionFile }); + + expect(formatResumeCommand(sessionManager)).toBeUndefined(); + }); + + it("returns undefined for in-memory sessions", () => { + setStdoutIsTTY(true); + const sessionFile = createTempFile(); + const sessionManager = createSessionManager({ persisted: false, sessionFile }); + + expect(formatResumeCommand(sessionManager)).toBeUndefined(); + }); + + it("returns undefined when the session file is missing", () => { + setStdoutIsTTY(true); + const sessionManager = createSessionManager({ sessionFile: "/tmp/pi-missing-session.jsonl" }); + + expect(formatResumeCommand(sessionManager)).toBeUndefined(); + }); + + it("returns undefined when the session file is not set", () => { + setStdoutIsTTY(true); + const sessionManager = createSessionManager({ sessionFile: undefined }); + + expect(formatResumeCommand(sessionManager)).toBeUndefined(); + }); +}); diff --git a/packages/coding-agent/test/frontmatter.test.ts b/packages/coding-agent/test/frontmatter.test.ts new file mode 100644 index 0000000000000000000000000000000000000000..43a92985ee397435f99909b4ca44dac03dd60169 --- /dev/null +++ b/packages/coding-agent/test/frontmatter.test.ts @@ -0,0 +1,60 @@ +import { describe, expect, it } from "vitest"; +import { parseFrontmatter, stripFrontmatter } from "../src/utils/frontmatter.ts"; + +describe("parseFrontmatter", () => { + it("parses keys, strips quotes, and returns body", () => { + const input = "---\nname: \"skill-name\"\ndescription: 'A desc'\nfoo-bar: value\n---\n\nBody text"; + const { frontmatter, body } = parseFrontmatter>(input); + expect(frontmatter.name).toBe("skill-name"); + expect(frontmatter.description).toBe("A desc"); + expect(frontmatter["foo-bar"]).toBe("value"); + expect(body).toBe("Body text"); + }); + + it("normalizes newlines and handles CRLF", () => { + const input = "---\r\nname: test\r\n---\r\nLine one\r\nLine two"; + const { body } = parseFrontmatter>(input); + expect(body).toBe("Line one\nLine two"); + }); + + it("throws on invalid YAML frontmatter", () => { + const input = "---\nfoo: [bar\n---\nBody"; + expect(() => parseFrontmatter>(input)).toThrow(/at line 1, column 10/); + }); + + it("parses | multiline yaml syntax", () => { + const input = "---\ndescription: |\n Line one\n Line two\n---\n\nBody"; + const { frontmatter, body } = parseFrontmatter>(input); + expect(frontmatter.description).toBe("Line one\nLine two\n"); + expect(body).toBe("Body"); + }); + + it("returns original content when frontmatter is missing or unterminated", () => { + const noFrontmatter = "Just text\nsecond line"; + const missingEnd = "---\nname: test\nBody without terminator"; + const resultNoFrontmatter = parseFrontmatter>(noFrontmatter); + const resultMissingEnd = parseFrontmatter>(missingEnd); + expect(resultNoFrontmatter.body).toBe("Just text\nsecond line"); + expect(resultMissingEnd.body).toBe( + "---\nname: test\nBody without terminator".replace(/\r\n/g, "\n").replace(/\r/g, "\n"), + ); + }); + + it("returns empty object for empty or comment-only frontmatter", () => { + const input = "---\n# just a comment\n---\nBody"; + const { frontmatter } = parseFrontmatter(input); + expect(frontmatter).toEqual({}); + }); +}); + +describe("stripFrontmatter", () => { + it("removes frontmatter and trims body", () => { + const input = "---\nkey: value\n---\n\nBody\n"; + expect(stripFrontmatter(input)).toBe("Body"); + }); + + it("returns body when no frontmatter present", () => { + const input = "\n No frontmatter body \n"; + expect(stripFrontmatter(input)).toBe("\n No frontmatter body \n"); + }); +}); diff --git a/packages/coding-agent/test/git-merge-and-resolve-extension.test.ts b/packages/coding-agent/test/git-merge-and-resolve-extension.test.ts new file mode 100644 index 0000000000000000000000000000000000000000..7c8741c7ec086b119e9d4f1f767127715513bc43 --- /dev/null +++ b/packages/coding-agent/test/git-merge-and-resolve-extension.test.ts @@ -0,0 +1,206 @@ +import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from "node:fs"; +import { tmpdir } from "node:os"; +import { join } from "node:path"; +import { afterEach, describe, expect, it, vi } from "vitest"; +import mergeAndResolve from "../examples/extensions/git-merge-and-resolve.ts"; +import type { ExecResult, ExtensionAPI, ExtensionContext } from "../src/core/extensions/index.ts"; + +type AgentEndHandler = (event: { type: "agent_end" }, ctx: ExtensionContext) => Promise; + +const ok: ExecResult = { stdout: "", stderr: "", code: 0, killed: false }; +const fail: ExecResult = { stdout: "", stderr: "error", code: 1, killed: false }; + +/** Standard exec results for a clean repo tracking origin/main, not in a merge. */ +function withUpstream(results: Map): Map { + results.set("git rev-parse --git-dir", ok); + results.set("git rev-parse MERGE_HEAD", fail); + results.set("git status --porcelain", ok); + results.set("git rev-parse --abbrev-ref --symbolic-full-name @{u}", { ...ok, stdout: "origin/main\n" }); + results.set("git fetch origin", ok); + return results; +} + +function setup(cwd: string, execResults: Map) { + let handler: AgentEndHandler | undefined; + const sendUserMessage = vi.fn(); + + const exec = vi.fn().mockImplementation(async (cmd, args) => { + const key = [cmd, ...args].join(" "); + return execResults.get(key) ?? fail; + }); + + const api = { + on: (event: string, h: AgentEndHandler) => { + if (event === "agent_end") handler = h; + }, + exec, + sendUserMessage, + } as unknown as ExtensionAPI; + + mergeAndResolve(api); + + const ctx = { cwd, ui: { notify: vi.fn() } } as unknown as ExtensionContext; + + async function trigger() { + await handler!({ type: "agent_end" }, ctx); + } + + return { trigger, exec, sendUserMessage }; +} + +describe("git-merge-and-resolve example", () => { + let tempDir: string; + + afterEach(() => { + if (tempDir) rmSync(tempDir, { recursive: true, force: true }); + }); + + function createTempDir() { + tempDir = mkdtempSync(join(tmpdir(), "pi-merge-test-")); + return tempDir; + } + + it("skips when not a git repository", async () => { + const cwd = createTempDir(); + const results = new Map(); + results.set("git rev-parse --git-dir", fail); + + const { trigger, exec, sendUserMessage } = setup(cwd, results); + await trigger(); + + expect(exec).toHaveBeenCalledTimes(1); + expect(sendUserMessage).not.toHaveBeenCalled(); + }); + + it("skips when no upstream is configured", async () => { + const cwd = createTempDir(); + const results = new Map(); + results.set("git rev-parse --git-dir", ok); + results.set("git rev-parse --abbrev-ref --symbolic-full-name @{u}", fail); + + const { trigger, sendUserMessage } = setup(cwd, results); + await trigger(); + + expect(sendUserMessage).not.toHaveBeenCalled(); + }); + + it("re-sends conflicts when in an unfinished merge", async () => { + const cwd = createTempDir(); + const conflictContent = ["<<<<<<< HEAD", "ours", "=======", "theirs", ">>>>>>> origin/main"].join("\n"); + writeFileSync(join(cwd, "file.ts"), conflictContent); + + const results = new Map(); + results.set("git rev-parse --git-dir", ok); + results.set("git rev-parse MERGE_HEAD", ok); + results.set("git diff --name-only --diff-filter=U", { ...ok, stdout: "file.ts\n" }); + + const { trigger, exec, sendUserMessage } = setup(cwd, results); + await trigger(); + + // Should not attempt a new fetch/merge + expect(exec).not.toHaveBeenCalledWith("git", ["fetch", "origin"]); + expect(sendUserMessage).toHaveBeenCalledTimes(1); + const message = sendUserMessage.mock.calls[0][0] as string; + expect(message).toContain("file.ts:1-5"); + }); + + it("skips when working tree is dirty and not in a merge", async () => { + const cwd = createTempDir(); + const results = new Map(); + results.set("git rev-parse --git-dir", ok); + results.set("git rev-parse MERGE_HEAD", fail); + results.set("git status --porcelain", { ...ok, stdout: " M src/index.ts\n" }); + + const { trigger, exec, sendUserMessage } = setup(cwd, results); + await trigger(); + + expect(exec).not.toHaveBeenCalledWith("git", ["fetch", "origin"]); + expect(sendUserMessage).not.toHaveBeenCalled(); + }); + + it("skips when fetch fails", async () => { + const cwd = createTempDir(); + const results = withUpstream(new Map()); + results.set("git fetch origin", fail); + + const { trigger, sendUserMessage } = setup(cwd, results); + await trigger(); + + expect(sendUserMessage).not.toHaveBeenCalled(); + }); + + it("skips when merge is clean", async () => { + const cwd = createTempDir(); + const results = withUpstream(new Map()); + results.set("git merge --no-ff origin/main", ok); + + const { trigger, sendUserMessage } = setup(cwd, results); + await trigger(); + + expect(sendUserMessage).not.toHaveBeenCalled(); + }); + + it("sends conflict report as a follow-up", async () => { + const cwd = createTempDir(); + const conflictContent = [ + "line 1", + "<<<<<<< HEAD", + "our change", + "=======", + "their change", + ">>>>>>> origin/main", + "line 7", + "<<<<<<< HEAD", + "second conflict", + "=======", + "their second", + ">>>>>>> origin/main", + ].join("\n"); + + mkdirSync(join(cwd, "src"), { recursive: true }); + writeFileSync(join(cwd, "src/index.ts"), conflictContent); + + const results = withUpstream(new Map()); + results.set("git merge --no-ff origin/main", { ...fail, code: 1 }); + results.set("git diff --name-only --diff-filter=U", { ...ok, stdout: "src/index.ts\n" }); + + const { trigger, sendUserMessage } = setup(cwd, results); + await trigger(); + + expect(sendUserMessage).toHaveBeenCalledTimes(1); + const [message, options] = sendUserMessage.mock.calls[0]; + expect(message).toContain("src/index.ts:2-6 (ours 3, theirs 5)"); + expect(message).toContain("src/index.ts:8-12 (ours 9, theirs 11)"); + expect(options).toEqual({ deliverAs: "followUp" }); + }); + + it("handles empty ours or theirs sections", async () => { + const cwd = createTempDir(); + const conflictContent = ["<<<<<<< HEAD", "=======", "only theirs", ">>>>>>> origin/main"].join("\n"); + + writeFileSync(join(cwd, "empty-ours.ts"), conflictContent); + + const results = withUpstream(new Map()); + results.set("git merge --no-ff origin/main", { ...fail, code: 1 }); + results.set("git diff --name-only --diff-filter=U", { ...ok, stdout: "empty-ours.ts\n" }); + + const { trigger, sendUserMessage } = setup(cwd, results); + await trigger(); + + expect(sendUserMessage).toHaveBeenCalledTimes(1); + const message = sendUserMessage.mock.calls[0][0] as string; + expect(message).toContain("empty-ours.ts:1-4 (ours empty, theirs 3)"); + }); + + it("skips message when merge fails but no conflict markers found", async () => { + const cwd = createTempDir(); + const results = withUpstream(new Map()); + results.set("git merge --no-ff origin/main", { ...fail, code: 1 }); + results.set("git diff --name-only --diff-filter=U", { ...ok, stdout: "" }); + + const { trigger, sendUserMessage } = setup(cwd, results); + await trigger(); + + expect(sendUserMessage).not.toHaveBeenCalled(); + }); +}); diff --git a/packages/coding-agent/test/git-ssh-url.test.ts b/packages/coding-agent/test/git-ssh-url.test.ts new file mode 100644 index 0000000000000000000000000000000000000000..87b8b9400fa8c5ff763d55d8f30aef37359c5c86 --- /dev/null +++ b/packages/coding-agent/test/git-ssh-url.test.ts @@ -0,0 +1,91 @@ +import { describe, expect, it } from "vitest"; +import { parseGitUrl } from "../src/utils/git.ts"; + +describe("Git URL Parsing", () => { + describe("protocol URLs (accepted without git: prefix)", () => { + it("should parse HTTPS URL", () => { + const result = parseGitUrl("https://github.com/user/repo"); + expect(result).toMatchObject({ + host: "github.com", + path: "user/repo", + repo: "https://github.com/user/repo", + }); + }); + + it("should parse ssh:// URL", () => { + const result = parseGitUrl("ssh://git@github.com/user/repo"); + expect(result).toMatchObject({ + host: "github.com", + path: "user/repo", + repo: "ssh://git@github.com/user/repo", + }); + }); + + it("should parse protocol URL with ref", () => { + const result = parseGitUrl("https://github.com/user/repo@v1.0.0"); + expect(result).toMatchObject({ + host: "github.com", + path: "user/repo", + ref: "v1.0.0", + repo: "https://github.com/user/repo", + }); + }); + }); + + describe("shorthand URLs (accepted only with git: prefix)", () => { + it("should parse git@host:path with git: prefix", () => { + const result = parseGitUrl("git:git@github.com:user/repo"); + expect(result).toMatchObject({ + host: "github.com", + path: "user/repo", + repo: "git@github.com:user/repo", + }); + }); + + it("should parse host/path shorthand with git: prefix", () => { + const result = parseGitUrl("git:github.com/user/repo"); + expect(result).toMatchObject({ + host: "github.com", + path: "user/repo", + repo: "https://github.com/user/repo", + }); + }); + + it("should parse shorthand with ref and git: prefix", () => { + const result = parseGitUrl("git:git@github.com:user/repo@v1.0.0"); + expect(result).toMatchObject({ + host: "github.com", + path: "user/repo", + ref: "v1.0.0", + repo: "git@github.com:user/repo", + }); + }); + }); + + it("should reject unsafe git install path inputs", () => { + for (const source of [ + "git:git@evil.example:../../victim/repo", + "https://evil.example/..%2F..%2Fvictim/repo", + "https://evil.example/..%2F..%2Fvictim/repo%", + "git:git@evil.example:/absolute/repo", + "git:git@evil.example:user\\repo/name", + "git:git@evil.example:user/repo\0name", + ]) { + expect(parseGitUrl(source)).toBeNull(); + } + }); + + describe("unsupported without git: prefix", () => { + it("should reject git@host:path without git: prefix", () => { + expect(parseGitUrl("git@github.com:user/repo")).toBeNull(); + }); + + it("should reject host/path shorthand without git: prefix", () => { + expect(parseGitUrl("github.com/user/repo")).toBeNull(); + }); + + it("should reject user/repo shorthand", () => { + expect(parseGitUrl("user/repo")).toBeNull(); + }); + }); +}); diff --git a/packages/coding-agent/test/git-update.test.ts b/packages/coding-agent/test/git-update.test.ts new file mode 100644 index 0000000000000000000000000000000000000000..80308277cb39502db8c4483cca5b9067eb0cfe53 --- /dev/null +++ b/packages/coding-agent/test/git-update.test.ts @@ -0,0 +1,282 @@ +/** + * Tests for git-based extension updates, specifically handling force-push scenarios. + * + * These tests verify that DefaultPackageManager.update() handles: + * - Normal git updates (no force-push) + * - Force-pushed remotes after a complete history rewrite + */ + +import { spawnSync } from "node:child_process"; +import { existsSync, mkdirSync, readFileSync, rmSync, writeFileSync } from "node:fs"; +import { tmpdir } from "node:os"; +import { join } from "node:path"; +import { afterEach, beforeEach, describe, expect, it } from "vitest"; +import { DefaultPackageManager } from "../src/core/package-manager.ts"; +import { SettingsManager } from "../src/core/settings-manager.ts"; +import { allowNetwork } from "./test-network-env.ts"; + +// Helper to run git commands in a directory +function git(args: string[], cwd: string): string { + const result = spawnSync("git", args, { + cwd, + encoding: "utf-8", + }); + if (result.status !== 0) { + throw new Error(`Command failed: git ${args.join(" ")}\n${result.stderr}`); + } + return result.stdout.trim(); +} + +function initGitRepo(repoDir: string): void { + git(["init", "--initial-branch=main"], repoDir); + git(["config", "--local", "user.email", "test@test.com"], repoDir); + git(["config", "--local", "user.name", "Test"], repoDir); +} + +// Helper to create a commit with a file +function createCommit(repoDir: string, filename: string, content: string, message: string): string { + writeFileSync(join(repoDir, filename), content); + git(["add", filename], repoDir); + git(["commit", "-m", message], repoDir); + return git(["rev-parse", "HEAD"], repoDir); +} + +// Helper to get current commit hash +function getCurrentCommit(repoDir: string): string { + return git(["rev-parse", "HEAD"], repoDir); +} + +// Helper to get file content +function getFileContent(repoDir: string, filename: string): string { + return readFileSync(join(repoDir, filename), "utf-8"); +} + +type GitSourceForTest = { + type: "git"; + repo: string; + host: string; + path: string; + pinned: boolean; + ref?: string; +}; + +interface PackageManagerPathInternals { + parseSource(source: string): GitSourceForTest; + getGitInstallPath(source: GitSourceForTest, scope: "temporary"): string; +} + +describe("DefaultPackageManager git update", () => { + let tempDir: string; + let remoteDir: string; // Simulates the "remote" repository + let agentDir: string; // The agent directory where extensions are installed + let installedDir: string; // The installed extension directory + let settingsManager: SettingsManager; + let packageManager: DefaultPackageManager; + + // Git source that maps to our installed directory structure. + // Must use "git:" prefix so parseSource() treats it as a git source + // (bare "github.com/..." is not recognized as a git URL). + const gitSource = "git:github.com/test/extension"; + + beforeEach(() => { + allowNetwork(); + tempDir = join(tmpdir(), `git-update-test-${Date.now()}-${Math.random().toString(36).slice(2)}`); + mkdirSync(tempDir, { recursive: true }); + remoteDir = join(tempDir, "remote"); + agentDir = join(tempDir, "agent"); + + // This matches the path structure: agentDir/git// + installedDir = join(agentDir, "git", "github.com", "test", "extension"); + + mkdirSync(agentDir, { recursive: true }); + + settingsManager = SettingsManager.inMemory(); + packageManager = new DefaultPackageManager({ + cwd: tempDir, + agentDir, + settingsManager, + }); + }); + + afterEach(() => { + if (tempDir && existsSync(tempDir)) { + rmSync(tempDir, { recursive: true, force: true }); + } + }); + + /** Sets up a "remote" repository and clones it to the installed directory. */ + function setupRemoteAndInstall(): void { + // Create "remote" repository + mkdirSync(remoteDir, { recursive: true }); + initGitRepo(remoteDir); + createCommit(remoteDir, "extension.ts", "// v1", "Initial commit"); + + // Clone to installed directory (simulating what install() does) + mkdirSync(join(agentDir, "git", "github.com", "test"), { recursive: true }); + git(["clone", remoteDir, installedDir], tempDir); + git(["config", "--local", "user.email", "test@test.com"], installedDir); + git(["config", "--local", "user.name", "Test"], installedDir); + + // Add to global packages so update() processes this source + settingsManager.setPackages([gitSource]); + } + + describe("normal updates (no force-push)", () => { + it("should skip reset, clean, and install when already up to date", async () => { + mkdirSync(remoteDir, { recursive: true }); + initGitRepo(remoteDir); + writeFileSync(join(remoteDir, "package.json"), JSON.stringify({ name: "test-extension", version: "1.0.0" })); + createCommit(remoteDir, "extension.ts", "// v1", "Initial commit"); + + mkdirSync(join(agentDir, "git", "github.com", "test"), { recursive: true }); + git(["clone", remoteDir, installedDir], tempDir); + settingsManager.setPackages([gitSource]); + + const executedCommands: string[] = []; + const managerWithInternals = packageManager as unknown as { + runCommand: (command: string, args: string[], options?: { cwd?: string }) => Promise; + }; + managerWithInternals.runCommand = async (command, args, options) => { + executedCommands.push(`${command} ${args.join(" ")}`); + if (command === "npm") { + return; + } + const result = spawnSync(command, args, { + cwd: options?.cwd, + encoding: "utf-8", + }); + if (result.status !== 0) { + throw new Error(`Command failed: ${command} ${args.join(" ")}\n${result.stderr}`); + } + }; + + await packageManager.update(); + + expect(executedCommands).toContain( + "git fetch --prune --no-tags origin +refs/heads/main:refs/remotes/origin/main", + ); + expect(executedCommands).not.toContain("git fetch --prune origin"); + expect(executedCommands).not.toContain("git reset --hard @{upstream}"); + expect(executedCommands).not.toContain("git reset --hard origin/HEAD"); + expect(executedCommands).not.toContain("git clean -fdx"); + expect(executedCommands).not.toContain("npm install"); + }); + + it("should update to latest commit when remote has new commits", async () => { + setupRemoteAndInstall(); + expect(getFileContent(installedDir, "extension.ts")).toBe("// v1"); + + // Add a new commit to remote + const newCommit = createCommit(remoteDir, "extension.ts", "// v2", "Second commit"); + + // Update via package manager (no args = uses settings) + await packageManager.update(); + + // Verify update succeeded + expect(getCurrentCommit(installedDir)).toBe(newCommit); + expect(getFileContent(installedDir, "extension.ts")).toBe("// v2"); + }); + }); + + describe("force-push scenarios", () => { + it("should handle complete history rewrite", async () => { + setupRemoteAndInstall(); + + // Remote gets several commits + createCommit(remoteDir, "extension.ts", "// v2", "v2"); + createCommit(remoteDir, "extension.ts", "// v3", "v3"); + + await packageManager.update(); + expect(getFileContent(installedDir, "extension.ts")).toBe("// v3"); + + // Maintainer force-pushes completely different history + git(["reset", "--hard", "HEAD~2"], remoteDir); + createCommit(remoteDir, "extension.ts", "// rewrite-a", "Rewrite A"); + const finalCommit = createCommit(remoteDir, "extension.ts", "// rewrite-b", "Rewrite B"); + + // Should handle this gracefully + await packageManager.update(); + + expect(getCurrentCommit(installedDir)).toBe(finalCommit); + expect(getFileContent(installedDir, "extension.ts")).toBe("// rewrite-b"); + }); + }); + + describe("pinned sources", () => { + it("should checkout the configured pinned git ref during full and targeted updates", async () => { + mkdirSync(remoteDir, { recursive: true }); + initGitRepo(remoteDir); + const v1Commit = createCommit(remoteDir, "extension.ts", "// v1", "Initial commit"); + git(["tag", "v1"], remoteDir); + const v2Commit = createCommit(remoteDir, "extension.ts", "// v2", "Second commit"); + git(["tag", "v2"], remoteDir); + + mkdirSync(join(agentDir, "git", "github.com", "test"), { recursive: true }); + git(["clone", remoteDir, installedDir], tempDir); + git(["checkout", "v1"], installedDir); + expect(getCurrentCommit(installedDir)).toBe(v1Commit); + + const pinnedSource = `${gitSource}@v2`; + settingsManager.setPackages([pinnedSource]); + + await packageManager.update(); + + expect(getCurrentCommit(installedDir)).toBe(v2Commit); + expect(getFileContent(installedDir, "extension.ts")).toBe("// v2"); + + git(["checkout", "v1"], installedDir); + + await packageManager.update(pinnedSource); + + expect(getCurrentCommit(installedDir)).toBe(v2Commit); + expect(getFileContent(installedDir, "extension.ts")).toBe("// v2"); + }); + }); + + describe("temporary git sources", () => { + it("should refresh cached temporary git sources when resolving", async () => { + const managerWithPaths = packageManager as unknown as PackageManagerPathInternals; + const cachedDir = managerWithPaths.getGitInstallPath(managerWithPaths.parseSource(gitSource), "temporary"); + const extensionFile = join(cachedDir, "pi-extensions", "session-breakdown.ts"); + + rmSync(cachedDir, { recursive: true, force: true }); + mkdirSync(join(cachedDir, "pi-extensions"), { recursive: true }); + writeFileSync( + join(cachedDir, "package.json"), + JSON.stringify({ pi: { extensions: ["./pi-extensions"] } }, null, 2), + ); + writeFileSync(extensionFile, "// stale"); + + const executedCommands: string[] = []; + const managerWithInternals = packageManager as unknown as { + runCommand: (command: string, args: string[], options?: { cwd?: string }) => Promise; + runCommandCapture: (command: string, args: string[], options?: { cwd?: string }) => Promise; + }; + managerWithInternals.runCommand = async (command, args) => { + executedCommands.push(`${command} ${args.join(" ")}`); + if (command === "git" && args[0] === "reset") { + writeFileSync(extensionFile, "// fresh"); + } + }; + managerWithInternals.runCommandCapture = async (_command, args) => { + if (args[0] === "rev-parse" && args[1] === "HEAD") { + return "local-head"; + } + if (args[0] === "rev-parse" && args[1] === "@{upstream}") { + return "remote-head"; + } + if (args[0] === "rev-parse" && args[1] === "--abbrev-ref") { + return "origin/main"; + } + return ""; + }; + + await packageManager.resolveExtensionSources([gitSource], { temporary: true }); + + expect(executedCommands).toContain( + "git fetch --prune --no-tags origin +refs/heads/main:refs/remotes/origin/main", + ); + expect(getFileContent(cachedDir, "pi-extensions/session-breakdown.ts")).toBe("// fresh"); + }); + }); +}); diff --git a/packages/coding-agent/test/http-dispatcher.test.ts b/packages/coding-agent/test/http-dispatcher.test.ts new file mode 100644 index 0000000000000000000000000000000000000000..74784e169aab3f152174fb7435227827217f5668 --- /dev/null +++ b/packages/coding-agent/test/http-dispatcher.test.ts @@ -0,0 +1,171 @@ +import http from "node:http"; +import net from "node:net"; +import tls from "node:tls"; +import * as undici from "undici"; +import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; +import { applyHttpProxySettings, configureHttpDispatcher } from "../src/core/http-dispatcher.ts"; + +const PROXY_ENV_KEYS = ["HTTP_PROXY", "HTTPS_PROXY"] as const; +const DISPATCHER_PROXY_ENV_KEYS = [...PROXY_ENV_KEYS, "http_proxy", "https_proxy", "NO_PROXY", "no_proxy"] as const; + +describe("http proxy settings", () => { + let savedEnv: Record<(typeof PROXY_ENV_KEYS)[number], string | undefined>; + + beforeEach(() => { + savedEnv = Object.fromEntries(PROXY_ENV_KEYS.map((key) => [key, process.env[key]])) as Record< + (typeof PROXY_ENV_KEYS)[number], + string | undefined + >; + for (const key of PROXY_ENV_KEYS) { + delete process.env[key]; + } + }); + + afterEach(() => { + for (const key of PROXY_ENV_KEYS) { + const value = savedEnv[key]; + if (value === undefined) { + delete process.env[key]; + } else { + process.env[key] = value; + } + } + }); + + it("applies httpProxy to HTTP_PROXY and HTTPS_PROXY", () => { + applyHttpProxySettings("http://127.0.0.1:7890"); + + expect(process.env.HTTP_PROXY).toBe("http://127.0.0.1:7890"); + expect(process.env.HTTPS_PROXY).toBe("http://127.0.0.1:7890"); + }); + + it("does not override existing proxy env vars", () => { + process.env.HTTP_PROXY = "http://env-http:8080"; + process.env.HTTPS_PROXY = "http://env-https:8080"; + + applyHttpProxySettings("http://settings:7890"); + + expect(process.env.HTTP_PROXY).toBe("http://env-http:8080"); + expect(process.env.HTTPS_PROXY).toBe("http://env-https:8080"); + }); + + it("ignores empty values", () => { + applyHttpProxySettings(" "); + + expect(process.env.HTTP_PROXY).toBeUndefined(); + expect(process.env.HTTPS_PROXY).toBeUndefined(); + }); +}); + +describe("http dispatcher", () => { + const originalDispatcher = undici.getGlobalDispatcher(); + const originalFetch = globalThis.fetch; + let savedProxyEnv: Record<(typeof DISPATCHER_PROXY_ENV_KEYS)[number], string | undefined>; + + beforeEach(() => { + savedProxyEnv = Object.fromEntries(DISPATCHER_PROXY_ENV_KEYS.map((key) => [key, process.env[key]])) as Record< + (typeof DISPATCHER_PROXY_ENV_KEYS)[number], + string | undefined + >; + for (const key of DISPATCHER_PROXY_ENV_KEYS) { + delete process.env[key]; + } + }); + + afterEach(async () => { + const dispatcher = undici.getGlobalDispatcher(); + if (dispatcher !== originalDispatcher) { + await dispatcher.close(); + undici.setGlobalDispatcher(originalDispatcher); + } + for (const key of DISPATCHER_PROXY_ENV_KEYS) { + const value = savedProxyEnv[key]; + if (value === undefined) { + delete process.env[key]; + } else { + process.env[key] = value; + } + } + globalThis.fetch = originalFetch; + vi.restoreAllMocks(); + }); + + it("tunnels proxied HTTP origins", async () => { + const origin = http.createServer((_request, response) => { + response.end("origin"); + }); + await new Promise((resolve) => origin.listen(0, "127.0.0.1", resolve)); + const originAddress = origin.address(); + if (!originAddress || typeof originAddress === "string") { + throw new Error("Origin did not bind to a TCP port"); + } + + const proxyRequestLines: string[] = []; + const proxy = net.createServer((client) => { + client.once("data", (data) => { + const [requestLine = ""] = data.toString().split("\r\n"); + proxyRequestLines.push(requestLine); + if (!requestLine.startsWith("CONNECT ")) { + client.end("HTTP/1.1 501 Not Implemented\r\ncontent-length: 0\r\nconnection: close\r\n\r\n"); + return; + } + + const upstream = net.connect(originAddress.port, "127.0.0.1", () => { + client.write("HTTP/1.1 200 Connection Established\r\n\r\n"); + client.pipe(upstream).pipe(client); + }); + upstream.on("error", () => client.destroy()); + client.on("error", () => upstream.destroy()); + }); + }); + await new Promise((resolve) => proxy.listen(0, "127.0.0.1", resolve)); + const proxyAddress = proxy.address(); + if (!proxyAddress || typeof proxyAddress === "string") { + throw new Error("Proxy did not bind to a TCP port"); + } + + process.env.HTTP_PROXY = `http://127.0.0.1:${proxyAddress.port}`; + configureHttpDispatcher(); + const dispatcher = undici.getGlobalDispatcher(); + try { + const originUrl = `http://127.0.0.1:${originAddress.port}/v1/chat/completions`; + await expect(undici.fetch(originUrl).then((response) => response.text())).resolves.toBe("origin"); + await expect(undici.fetch(originUrl).then((response) => response.text())).resolves.toBe("origin"); + expect(proxyRequestLines).not.toHaveLength(0); + expect(proxyRequestLines).toEqual( + expect.arrayContaining([ + expect.stringMatching(`^CONNECT 127\\.0\\.0\\.1:${originAddress.port} HTTP/1\\.1$`), + ]), + ); + } finally { + await dispatcher.close(); + undici.setGlobalDispatcher(originalDispatcher); + await Promise.all([ + new Promise((resolve) => proxy.close(() => resolve())), + new Promise((resolve) => origin.close(() => resolve())), + ]); + } + }); + + it("allows two seconds for HTTPS connection attempts without changing the Node default", async () => { + // Preserve a deliberate host fetch override while testing the dispatcher itself. + globalThis.fetch = async () => { + throw new Error("Unexpected global fetch"); + }; + const originalAttemptTimeoutMs = net.getDefaultAutoSelectFamilyAttemptTimeout(); + const connectSpy = vi.spyOn(tls, "connect").mockImplementation(() => { + throw new Error("Connection captured"); + }); + + configureHttpDispatcher(); + await expect(undici.fetch("https://example.invalid")).rejects.toThrow(); + + expect(connectSpy).toHaveBeenCalledWith( + expect.objectContaining({ + autoSelectFamilyAttemptTimeout: 2_000, + }), + ); + expect(connectSpy.mock.calls[0]?.[0]).not.toHaveProperty("autoSelectFamily"); + expect(net.getDefaultAutoSelectFamilyAttemptTimeout()).toBe(originalAttemptTimeoutMs); + }); +}); diff --git a/packages/coding-agent/test/image-process.test.ts b/packages/coding-agent/test/image-process.test.ts new file mode 100644 index 0000000000000000000000000000000000000000..e65ebfb73661ba44329128855028bf8ed52cd1b4 --- /dev/null +++ b/packages/coding-agent/test/image-process.test.ts @@ -0,0 +1,53 @@ +import { describe, expect, it } from "vitest"; +import { processImage } from "../src/utils/image-process.ts"; +import { detectSupportedImageMimeType } from "../src/utils/mime.ts"; + +function createTinyBmp1x1Red24bpp(): Buffer { + const buffer = Buffer.alloc(58); + buffer.write("BM", 0, "ascii"); + buffer.writeUInt32LE(buffer.length, 2); + buffer.writeUInt32LE(54, 10); + buffer.writeUInt32LE(40, 14); + buffer.writeInt32LE(1, 18); + buffer.writeInt32LE(1, 22); + buffer.writeUInt16LE(1, 26); + buffer.writeUInt16LE(24, 28); + buffer.writeUInt32LE(0, 30); + buffer.writeUInt32LE(4, 34); + buffer[56] = 0xff; + return buffer; +} + +function expectPngMagic(base64Data: string): void { + const buffer = Buffer.from(base64Data, "base64"); + expect(buffer[0]).toBe(0x89); + expect(buffer[1]).toBe(0x50); + expect(buffer[2]).toBe(0x4e); + expect(buffer[3]).toBe(0x47); +} + +describe("image processing pipeline", () => { + it("detects BMP files from magic bytes", () => { + expect(detectSupportedImageMimeType(createTinyBmp1x1Red24bpp())).toBe("image/bmp"); + }); + + it("converts BMP files to PNG attachments when auto-resize is disabled", async () => { + const result = await processImage(createTinyBmp1x1Red24bpp(), "image/bmp", { autoResizeImages: false }); + + expect(result.ok).toBe(true); + if (!result.ok) return; + expect(result.mimeType).toBe("image/png"); + expect(result.hints).toContain("[Image converted from image/bmp to image/png.]"); + expectPngMagic(result.data); + }); + + it("converts BMP files before auto-resizing", async () => { + const result = await processImage(createTinyBmp1x1Red24bpp(), "image/bmp"); + + expect(result.ok).toBe(true); + if (!result.ok) return; + expect(result.mimeType).toBe("image/png"); + expect(result.hints).toContain("[Image converted from image/bmp to image/png.]"); + expectPngMagic(result.data); + }); +}); diff --git a/packages/coding-agent/test/image-processing.test.ts b/packages/coding-agent/test/image-processing.test.ts new file mode 100644 index 0000000000000000000000000000000000000000..fb93a4279981cb12b7c405c29ba889b657ac960a --- /dev/null +++ b/packages/coding-agent/test/image-processing.test.ts @@ -0,0 +1,203 @@ +/** + * Tests for image processing utilities using Photon. + */ + +import { describe, expect, it } from "vitest"; +import { convertToPng } from "../src/utils/image-convert.ts"; +import { formatDimensionNote, resizeImage } from "../src/utils/image-resize.ts"; + +// Small 2x2 red PNG image (base64) - generated with ImageMagick +const TINY_PNG = + "iVBORw0KGgoAAAANSUhEUgAAAAIAAAACAQMAAABIeJ9nAAAAIGNIUk0AAHomAACAhAAA+gAAAIDoAAB1MAAA6mAAADqYAAAXcJy6UTwAAAAGUExURf8AAP///0EdNBEAAAABYktHRAH/Ai3eAAAAB3RJTUUH6gEOADM5Ddoh/wAAAAxJREFUCNdjYGBgAAAABAABJzQnCgAAACV0RVh0ZGF0ZTpjcmVhdGUAMjAyNi0wMS0xNFQwMDo1MTo1NyswMDowMOnKzHgAAAAldEVYdGRhdGU6bW9kaWZ5ADIwMjYtMDEtMTRUMDA6NTE6NTcrMDA6MDCYl3TEAAAAKHRFWHRkYXRlOnRpbWVzdGFtcAAyMDI2LTAxLTE0VDAwOjUxOjU3KzAwOjAwz4JVGwAAAABJRU5ErkJggg=="; + +// Small 2x2 blue JPEG image (base64) - generated with ImageMagick +const TINY_JPEG = + "/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAMCAgMCAgMDAwMEAwMEBQgFBQQEBQoHBwYIDAoMDAsKCwsNDhIQDQ4RDgsLEBYQERMUFRUVDA8XGBYUGBIUFRT/2wBDAQMEBAUEBQkFBQkUDQsNFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBT/wAARCAACAAIDAREAAhEBAxEB/8QAFAABAAAAAAAAAAAAAAAAAAAACf/EABQQAQAAAAAAAAAAAAAAAAAAAAD/xAAVAQEBAAAAAAAAAAAAAAAAAAAGCf/EABQRAQAAAAAAAAAAAAAAAAAAAAD/2gAMAwEAAhEDEQA/AD3VTB3/2Q=="; + +const TINY_JPEG_2X1 = + "/9j/4AAQSkZJRgABAgAAAQABAAD/wAARCAABAAIDAREAAhEBAxEB/9sAQwADAgIDAgIDAwMDBAMDBAUIBQUEBAUKBwcGCAwKDAwLCgsLDQ4SEA0OEQ4LCxAWEBETFBUVFQwPFxgWFBgSFBUU/9sAQwEDBAQFBAUJBQUJFA0LDRQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQU/8QAHwAAAQUBAQEBAQEAAAAAAAAAAAECAwQFBgcICQoL/8QAtRAAAgEDAwIEAwUFBAQAAAF9AQIDAAQRBRIhMUEGE1FhByJxFDKBkaEII0KxwRVS0fAkM2JyggkKFhcYGRolJicoKSo0NTY3ODk6Q0RFRkdISUpTVFVWV1hZWmNkZWZnaGlqc3R1dnd4eXqDhIWGh4iJipKTlJWWl5iZmqKjpKWmp6ipqrKztLW2t7i5usLDxMXGx8jJytLT1NXW19jZ2uHi4+Tl5ufo6erx8vP09fb3+Pn6/8QAHwEAAwEBAQEBAQEBAQAAAAAAAAECAwQFBgcICQoL/8QAtREAAgECBAQDBAcFBAQAAQJ3AAECAxEEBSExBhJBUQdhcRMiMoEIFEKRobHBCSMzUvAVYnLRChYkNOEl8RcYGRomJygpKjU2Nzg5OkNERUZHSElKU1RVVldYWVpjZGVmZ2hpanN0dXZ3eHl6goOEhYaHiImKkpOUlZaXmJmaoqOkpaanqKmqsrO0tba3uLm6wsPExcbHyMnK0tPU1dbX2Nna4uPk5ebn6Onq8vP09fb3+Pn6/9oADAMBAAIRAxEAPwD4H8Q/8h/Uv+vmX/0M1/o1wJ/ySWU/9g1D/wBNRMOM/wDkp8z/AOv9b/05I//Z"; + +// 100x100 gray PNG +const MEDIUM_PNG_100x100 = + "iVBORw0KGgoAAAANSUhEUgAAAGQAAABkCAAAAABVicqIAAAAAmJLR0QA/4ePzL8AAAAHdElNRQfqAQ4AMzkN2iH/AAAAP0lEQVRo3u3NQQEAAAQEMASXXYrz2gqst/Lm4ZBIJBKJRCKRSCQSiUQikUgkEolEIpFIJBKJRCKRSCQSiSTsAP1cAUZeKtreAAAAJXRFWHRkYXRlOmNyZWF0ZQAyMDI2LTAxLTE0VDAwOjUxOjU3KzAwOjAw6crMeAAAACV0RVh0ZGF0ZTptb2RpZnkAMjAyNi0wMS0xNFQwMDo1MTo1NyswMDowMJiXdMQAAAAodEVYdGRhdGU6dGltZXN0YW1wADIwMjYtMDEtMTRUMDA6NTE6NTcrMDA6MDDPglUbAAAAAElFTkSuQmCC"; + +// 200x200 colored PNG +const LARGE_PNG_200x200 = + "iVBORw0KGgoAAAANSUhEUgAAAMgAAADIEAIAAAByquWKAAAAIGNIUk0AAHomAACAhAAA+gAAAIDoAAB1MAAA6mAAADqYAAAXcJy6UTwAAAAGYktHRP///////wlY99wAAAAHdElNRQfqAQ4AMzqU03BFAAAEEElEQVR42u3dvWqUURSF4XXMRAyIhSAWQvwnWtjaWusFx9bW1kKD/wELESxEiBjDjJdwysOC57mCXW32+zGcGbtdACpsdi+T8Xz1GABzm91xMl6sHgNgblzcT/Y+rh4DYG6cv0/2H64eA2BOEgI1fHQHaoyzbXIwVo8BMCcJgRqSEKgxft9Lrn5aPQbA3Ph1klw7Wj0GwJwkBGpYWECN8fMiub63egyAORcWUMPCAmqMH3eTG59XjwEwN76/S24+Wj0GwJwkBGpYWECN8e1fcmuzegyAORcWUMPCAmqM09vJ4dfVYwDMjS9vkzuPV48BMCcJgRqeSAZqjA/nyYP91WMAzElCoIYkBGqMk8Pk6HT1GABzm+3L5JIkBApIQqCGj+5AjfHmb/Lk8uoxAOYkIVBDEgI1LCygxmZ7nFyShEABFxZQw8ICaozXf5KnV1aPATDnwgJqWFhADQsLqOFnDUANFxZQw8ICaoxXZ8mzg9VjAMy5sIAaFhZQw3tYQA1/QgHUkIRADUkI1JCEQA1JCNSQhEANFxZQwzcsoIYkBGpIQqCGJARqSEKghiQEalhYQA1/QgHUcGEBNSwsoIYkBGq4sIAaFhZQw8ICaviGBdRwYQE1LCyghiQEariwgBoWFlDDe1hADS+OAjUkIVBDEgI1JCFQQxICNSQhUMOFBdTwDQuoIQmBGpIQqCEJgRqSEKghCYEaFhZQw4ujQA0XFlDDwgJqSEKghgsLqGFhATUsLKCGb1hADRcWUMPCAmpIQqCGCwuoYWEBNbyHBdTw4ihQQxICNSQhUEMSAjUkIVBDEgI1XFhADd+wgBqSEKghCYEakhCoIQmBGpIQqGFhATW8OArUcGEBNSwsoIYkBGq4sIAaFhZQw8ICaviGBdRwYQE1LCyghiQEariwgBoWFlDDe1hADS+OAjUkIVBDEgI1JCFQQxICNSQhUMOFBdTwDQuoIQmBGpIQqCEJgRqSEKghCYEa49lB8ups9RgAc14cBWpIQqCGhQXUkIRADRcWUMPCAmqMp1eS139WjwEw5xsWUEMSAjUsLKCGJARquLCAGhYWUGM8uZy8+bt6DIA5L44CNSQhUMMTyUCNcXSanByuHgNgThICNSQhUGM82E8+nK8eA2DOzxqAGpIQqDHuPE6+vF09BsDcOPyanN5ePQbAnCQEavgdFlBj3Nok3/6tHgNgzoujQA1JCNQYNx8l39+tHgNgbtz4nPy4u3oMgDlJCNSwsIAa4/pe8vNi9RgAc37WANSQhECNce0o+XWyegyAuXH1U/L73uoxAOYkIVDDwgJqjIORnG1XjwEw508ogBqSEKgx9h8m5+9XjwEwN/Y+Jhf3V48BMCcJgRpjPE+2x6vHAJgbSbLbrR4DYO4/GqiSgXN+ksgAAAAldEVYdGRhdGU6Y3JlYXRlADIwMjYtMDEtMTRUMDA6NTE6NTcrMDA6MDDpysx4AAAAJXRFWHRkYXRlOm1vZGlmeQAyMDI2LTAxLTE0VDAwOjUxOjU3KzAwOjAwmJd0xAAAACh0RVh0ZGF0ZTp0aW1lc3RhbXAAMjAyNi0wMS0xNFQwMDo1MTo1NyswMDowMM+CVRsAAAAASUVORK5CYII="; + +function imageBytes(base64Data: string): Uint8Array { + return Buffer.from(base64Data, "base64"); +} + +function app1Segment(payload: Uint8Array): Buffer { + const segment = Buffer.alloc(payload.length + 4); + segment[0] = 0xff; + segment[1] = 0xe1; + segment.writeUInt16BE(payload.length + 2, 2); + segment.set(payload, 4); + return segment; +} + +function jpegWithXmpBeforeOrientation(): string { + const jpeg = Buffer.from(TINY_JPEG_2X1, "base64"); + const xmp = app1Segment(Buffer.from('http://ns.adobe.com/xap/1.0/\0')); + const orientation6 = app1Segment( + Buffer.concat([ + Buffer.from("Exif\0\0"), + Buffer.from("49492a0008000000010012010300010000000600000000000000", "hex"), + ]), + ); + return Buffer.concat([jpeg.subarray(0, 2), xmp, orientation6, jpeg.subarray(2)]).toString("base64"); +} + +describe("convertToPng", () => { + it("should return original data for PNG input", async () => { + const result = await convertToPng(TINY_PNG, "image/png"); + expect(result).not.toBeNull(); + expect(result!.data).toBe(TINY_PNG); + expect(result!.mimeType).toBe("image/png"); + }); + + it("should convert JPEG to PNG", async () => { + const result = await convertToPng(TINY_JPEG, "image/jpeg"); + expect(result).not.toBeNull(); + expect(result!.mimeType).toBe("image/png"); + // Result should be valid base64 + expect(() => Buffer.from(result!.data, "base64")).not.toThrow(); + // PNG magic bytes + const buffer = Buffer.from(result!.data, "base64"); + expect(buffer[0]).toBe(0x89); + expect(buffer[1]).toBe(0x50); // 'P' + expect(buffer[2]).toBe(0x4e); // 'N' + expect(buffer[3]).toBe(0x47); // 'G' + }); + + it("should apply EXIF orientation after an XMP APP1 segment", async () => { + const result = await convertToPng(jpegWithXmpBeforeOrientation(), "image/jpeg"); + + expect(result).not.toBeNull(); + const png = Buffer.from(result!.data, "base64"); + expect(png.readUInt32BE(16)).toBe(1); + expect(png.readUInt32BE(20)).toBe(2); + }); +}); + +describe("resizeImage", () => { + it("should keep caller input bytes intact", async () => { + const input = new Uint8Array(imageBytes(TINY_PNG)); + const originalByteLength = input.byteLength; + const originalFirstByte = input[0]; + + const result = await resizeImage(input, "image/png", { + maxWidth: 100, + maxHeight: 100, + maxBytes: 1024 * 1024, + }); + + expect(result).not.toBeNull(); + expect(input.byteLength).toBe(originalByteLength); + expect(input[0]).toBe(originalFirstByte); + }); + + it("should return original image if within limits", async () => { + const result = await resizeImage(imageBytes(TINY_PNG), "image/png", { + maxWidth: 100, + maxHeight: 100, + maxBytes: 1024 * 1024, + }); + + expect(result).not.toBeNull(); + expect(result!.wasResized).toBe(false); + expect(result!.data).toBe(TINY_PNG); + expect(result!.originalWidth).toBe(2); + expect(result!.originalHeight).toBe(2); + expect(result!.width).toBe(2); + expect(result!.height).toBe(2); + }); + + it("should resize image exceeding dimension limits", async () => { + const result = await resizeImage(imageBytes(MEDIUM_PNG_100x100), "image/png", { + maxWidth: 50, + maxHeight: 50, + maxBytes: 1024 * 1024, + }); + + expect(result).not.toBeNull(); + expect(result!.wasResized).toBe(true); + expect(result!.originalWidth).toBe(100); + expect(result!.originalHeight).toBe(100); + expect(result!.width).toBeLessThanOrEqual(50); + expect(result!.height).toBeLessThanOrEqual(50); + }); + + it("should resize image exceeding byte limit", async () => { + const originalBuffer = Buffer.from(LARGE_PNG_200x200, "base64"); + const originalSize = originalBuffer.length; + + // Set maxBytes to less than the original encoded image size + const result = await resizeImage(imageBytes(LARGE_PNG_200x200), "image/png", { + maxWidth: 2000, + maxHeight: 2000, + maxBytes: Math.floor(LARGE_PNG_200x200.length * 0.9), + }); + + // Should have tried to reduce size + expect(result).not.toBeNull(); + const resultBuffer = Buffer.from(result!.data, "base64"); + expect(resultBuffer.length).toBeLessThan(originalSize); + expect(result!.data.length).toBeLessThan(LARGE_PNG_200x200.length); + }); + + it("should return null when image cannot be resized below maxBytes", async () => { + const result = await resizeImage(imageBytes(LARGE_PNG_200x200), "image/png", { + maxWidth: 2000, + maxHeight: 2000, + maxBytes: 1, + }); + + expect(result).toBeNull(); + }); + + it("should handle JPEG input", async () => { + const result = await resizeImage(imageBytes(TINY_JPEG), "image/jpeg", { + maxWidth: 100, + maxHeight: 100, + maxBytes: 1024 * 1024, + }); + + expect(result).not.toBeNull(); + expect(result!.wasResized).toBe(false); + expect(result!.originalWidth).toBe(2); + expect(result!.originalHeight).toBe(2); + }); +}); + +describe("formatDimensionNote", () => { + it("should return undefined for non-resized images", () => { + const note = formatDimensionNote({ + data: "", + mimeType: "image/png", + originalWidth: 100, + originalHeight: 100, + width: 100, + height: 100, + wasResized: false, + }); + expect(note).toBeUndefined(); + }); + + it("should return formatted note for resized images", () => { + const note = formatDimensionNote({ + data: "", + mimeType: "image/png", + originalWidth: 2000, + originalHeight: 1000, + width: 1000, + height: 500, + wasResized: true, + }); + expect(note).toContain("original 2000x1000"); + expect(note).toContain("displayed at 1000x500"); + expect(note).toContain("2.00"); // scale factor + }); +}); diff --git a/packages/coding-agent/test/image-resize-callers.test.ts b/packages/coding-agent/test/image-resize-callers.test.ts new file mode 100644 index 0000000000000000000000000000000000000000..2337ae8ca568f6a8ecd9c979a5f0e257f2ff1198 --- /dev/null +++ b/packages/coding-agent/test/image-resize-callers.test.ts @@ -0,0 +1,53 @@ +import { mkdirSync, rmSync, writeFileSync } from "node:fs"; +import { tmpdir } from "node:os"; +import { join } from "node:path"; +import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; + +vi.mock("../src/utils/image-resize.js", () => ({ + resizeImage: vi.fn(), + formatDimensionNote: vi.fn(() => undefined), +})); + +import { processFileArguments } from "../src/cli/file-processor.ts"; +import { createReadTool } from "../src/core/tools/read.ts"; +import { resizeImage } from "../src/utils/image-resize.ts"; + +const TINY_PNG_BASE64 = + "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mP8z8DwHwAFBQIAX8jx0gAAAABJRU5ErkJggg=="; + +describe("image resize callers", () => { + let testDir: string; + + beforeEach(() => { + testDir = join(tmpdir(), `image-resize-callers-${Date.now()}`); + mkdirSync(testDir, { recursive: true }); + vi.mocked(resizeImage).mockReset(); + vi.mocked(resizeImage).mockResolvedValue(null); + }); + + afterEach(() => { + rmSync(testDir, { recursive: true, force: true }); + }); + + it("read tool returns text-only output when auto-resize cannot produce a safe image", async () => { + const imagePath = join(testDir, "test.png"); + writeFileSync(imagePath, Buffer.from(TINY_PNG_BASE64, "base64")); + + const tool = createReadTool(testDir); + const result = await tool.execute("test-read-image", { path: imagePath }); + + expect(result.content).toHaveLength(1); + expect(result.content[0].type).toBe("text"); + expect((result.content[0] as { type: "text"; text: string }).text).toContain("Image omitted"); + }); + + it("file processor omits image attachments when auto-resize cannot produce a safe image", async () => { + const imagePath = join(testDir, "test.png"); + writeFileSync(imagePath, Buffer.from(TINY_PNG_BASE64, "base64")); + + const result = await processFileArguments([imagePath]); + + expect(result.images).toHaveLength(0); + expect(result.text).toContain("Image omitted"); + }); +}); diff --git a/packages/coding-agent/test/initial-message.test.ts b/packages/coding-agent/test/initial-message.test.ts new file mode 100644 index 0000000000000000000000000000000000000000..fae479f781310a9c00e1fa36c50cbcf13e34737a --- /dev/null +++ b/packages/coding-agent/test/initial-message.test.ts @@ -0,0 +1,48 @@ +import { describe, expect, test } from "vitest"; +import type { Args } from "../src/cli/args.ts"; +import { buildInitialMessage } from "../src/cli/initial-message.ts"; + +function createArgs(messages: string[] = []): Args { + return { + messages: [...messages], + fileArgs: [], + unknownFlags: new Map(), + diagnostics: [], + }; +} + +describe("buildInitialMessage", () => { + test("merges piped stdin with the first CLI message into one prompt", () => { + const parsed = createArgs(["Summarize the text given"]); + const result = buildInitialMessage({ + parsed, + stdinContent: "README contents\n", + }); + + expect(result.initialMessage).toBe("README contents\nSummarize the text given"); + expect(parsed.messages).toEqual([]); + }); + + test("uses stdin as the initial prompt when no CLI message is present", () => { + const parsed = createArgs(); + const result = buildInitialMessage({ + parsed, + stdinContent: "README contents", + }); + + expect(result.initialMessage).toBe("README contents"); + expect(parsed.messages).toEqual([]); + }); + + test("combines stdin, file text, and first CLI message in one prompt", () => { + const parsed = createArgs(["Explain it", "Second message"]); + const result = buildInitialMessage({ + parsed, + stdinContent: "stdin\n", + fileText: "file\n", + }); + + expect(result.initialMessage).toBe("stdin\nfile\nExplain it"); + expect(parsed.messages).toEqual(["Second message"]); + }); +}); diff --git a/packages/coding-agent/test/input-transform-streaming-example.test.ts b/packages/coding-agent/test/input-transform-streaming-example.test.ts new file mode 100644 index 0000000000000000000000000000000000000000..faf95b6ba4703d84ead2a6b779ac8a6bb19146c6 --- /dev/null +++ b/packages/coding-agent/test/input-transform-streaming-example.test.ts @@ -0,0 +1,84 @@ +import { describe, expect, it, vi } from "vitest"; +import inputTransformStreaming from "../examples/extensions/input-transform-streaming.ts"; +import type { + ExecResult, + ExtensionAPI, + ExtensionContext, + InputEvent, + InputEventResult, +} from "../src/core/extensions/index.ts"; + +type InputHandler = (event: InputEvent, ctx: ExtensionContext) => Promise; + +function setup(execResult: ExecResult) { + let handler: InputHandler | undefined; + + const exec = vi.fn().mockResolvedValue(execResult); + + const api = { + on: (event: string, h: InputHandler) => { + if (event === "input") handler = h; + }, + exec, + } as unknown as ExtensionAPI; + + inputTransformStreaming(api); + + const ctx = {} as ExtensionContext; + + function emit(text: string, streamingBehavior?: "steer" | "followUp") { + return handler!({ type: "input", text, source: "interactive", streamingBehavior }, ctx); + } + + return { emit, exec }; +} + +describe("input-transform-streaming example", () => { + const diffOutput = " src/index.ts | 5 ++---\n 1 file changed, 2 insertions(+), 3 deletions(-)"; + const gitSuccess: ExecResult = { stdout: diffOutput, stderr: "", code: 0, killed: false }; + const gitEmpty: ExecResult = { stdout: "", stderr: "", code: 0, killed: false }; + const gitFail: ExecResult = { stdout: "", stderr: "not a git repo", code: 128, killed: false }; + + it("skips exec during steering", async () => { + const { emit, exec } = setup(gitSuccess); + const result = await emit("what changes did I make?", "steer"); + expect(result).toEqual({ action: "continue" }); + expect(exec).not.toHaveBeenCalled(); + }); + + it("transforms when idle and text matches trigger", async () => { + const { emit, exec } = setup(gitSuccess); + const result = await emit("review my changes"); + expect(exec).toHaveBeenCalledWith("git", ["diff", "--stat"]); + expect(result).toMatchObject({ action: "transform" }); + const text = (result as { text: string }).text; + expect(text).toContain("review my changes"); + expect(text).toContain("src/index.ts"); + }); + + it("transforms when queued as follow-up", async () => { + const { emit, exec } = setup(gitSuccess); + const result = await emit("show me the diff", "followUp"); + expect(exec).toHaveBeenCalled(); + expect(result).toMatchObject({ action: "transform" }); + }); + + it("continues when text does not match trigger", async () => { + const { emit, exec } = setup(gitSuccess); + const result = await emit("explain this function"); + expect(result).toEqual({ action: "continue" }); + expect(exec).not.toHaveBeenCalled(); + }); + + it("continues when git diff is empty", async () => { + const { emit } = setup(gitEmpty); + const result = await emit("any changes?"); + expect(result).toEqual({ action: "continue" }); + }); + + it("continues when git fails", async () => { + const { emit } = setup(gitFail); + const result = await emit("show modified files"); + expect(result).toEqual({ action: "continue" }); + }); +}); diff --git a/packages/coding-agent/test/interactive-mode-anthropic-warning.test.ts b/packages/coding-agent/test/interactive-mode-anthropic-warning.test.ts new file mode 100644 index 0000000000000000000000000000000000000000..bcda13aeb4a7cf4009106368e6186900de651ef2 --- /dev/null +++ b/packages/coding-agent/test/interactive-mode-anthropic-warning.test.ts @@ -0,0 +1,89 @@ +import { describe, expect, test, vi } from "vitest"; +import { InteractiveMode } from "../src/modes/interactive/interactive-mode.ts"; + +function createSettingsManager(warnings: { anthropicExtraUsage?: boolean } = {}) { + return { + getWarnings: vi.fn().mockReturnValue(warnings), + }; +} + +function createModelRuntime(credential: { type: "oauth" } | undefined, apiKey?: string) { + return { + checkAuth: vi.fn().mockResolvedValue(credential), + getAuth: vi.fn().mockResolvedValue(apiKey ? { auth: { apiKey } } : undefined), + }; +} + +describe("InteractiveMode.maybeWarnAboutAnthropicSubscriptionAuth", () => { + test("warns once when Anthropic subscription auth is detected", async () => { + const modelRuntime = createModelRuntime(undefined, "sk-ant-oat01-test"); + const fakeThis: any = { + anthropicSubscriptionWarningShown: false, + settingsManager: createSettingsManager(), + session: { modelRuntime }, + showWarning: vi.fn(), + }; + + await (InteractiveMode as any).prototype.maybeWarnAboutAnthropicSubscriptionAuth.call(fakeThis, { + provider: "anthropic", + }); + await (InteractiveMode as any).prototype.maybeWarnAboutAnthropicSubscriptionAuth.call(fakeThis, { + provider: "anthropic", + }); + + expect(fakeThis.showWarning).toHaveBeenCalledTimes(1); + expect(modelRuntime.getAuth).toHaveBeenCalledTimes(1); + }); + + test("warns when Anthropic OAuth is stored even if token refresh lookup would fail", async () => { + const modelRuntime = createModelRuntime({ type: "oauth" }); + const fakeThis: any = { + anthropicSubscriptionWarningShown: false, + settingsManager: createSettingsManager(), + session: { modelRuntime }, + showWarning: vi.fn(), + }; + + await (InteractiveMode as any).prototype.maybeWarnAboutAnthropicSubscriptionAuth.call(fakeThis, { + provider: "anthropic", + }); + + expect(fakeThis.showWarning).toHaveBeenCalledTimes(1); + expect(modelRuntime.getAuth).not.toHaveBeenCalled(); + }); + + test("does not warn for non-Anthropic models", async () => { + const modelRuntime = createModelRuntime(undefined); + const fakeThis: any = { + anthropicSubscriptionWarningShown: false, + settingsManager: createSettingsManager(), + session: { modelRuntime }, + showWarning: vi.fn(), + }; + + await (InteractiveMode as any).prototype.maybeWarnAboutAnthropicSubscriptionAuth.call(fakeThis, { + provider: "openai", + }); + + expect(fakeThis.showWarning).not.toHaveBeenCalled(); + expect(modelRuntime.getAuth).not.toHaveBeenCalled(); + }); + + test("does not warn when Anthropic extra usage warning is disabled", async () => { + const modelRuntime = createModelRuntime(undefined); + const fakeThis: any = { + anthropicSubscriptionWarningShown: false, + settingsManager: createSettingsManager({ anthropicExtraUsage: false }), + session: { modelRuntime }, + showWarning: vi.fn(), + }; + + await (InteractiveMode as any).prototype.maybeWarnAboutAnthropicSubscriptionAuth.call(fakeThis, { + provider: "anthropic", + }); + + expect(fakeThis.showWarning).not.toHaveBeenCalled(); + expect(modelRuntime.checkAuth).not.toHaveBeenCalled(); + expect(modelRuntime.getAuth).not.toHaveBeenCalled(); + }); +}); diff --git a/packages/coding-agent/test/interactive-mode-assistant-diagnostics.test.ts b/packages/coding-agent/test/interactive-mode-assistant-diagnostics.test.ts new file mode 100644 index 0000000000000000000000000000000000000000..30a13efa407975246199f126f6221f43c171eee0 --- /dev/null +++ b/packages/coding-agent/test/interactive-mode-assistant-diagnostics.test.ts @@ -0,0 +1,67 @@ +import type { AssistantMessage } from "@earendil-works/pi-ai"; +import { Container } from "@earendil-works/pi-tui"; +import { describe, expect, test } from "vitest"; +import { InteractiveMode } from "../src/modes/interactive/interactive-mode.ts"; +import { initTheme } from "../src/modes/interactive/theme/theme.ts"; +import { stripAnsi } from "../src/utils/ansi.ts"; + +const message: AssistantMessage = { + role: "assistant", + content: [{ type: "text", text: "survived" }], + api: "anthropic-messages", + provider: "anthropic", + model: "claude-fable-5-1", + usage: { + input: 1, + output: 1, + cacheRead: 0, + cacheWrite: 0, + totalTokens: 2, + cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 }, + }, + stopReason: "stop", + timestamp: 1, + diagnostics: [ + { + type: "anthropic_input_transformations", + timestamp: 1, + details: { + transformations: [ + { + type: "thinking_dropped", + path: "messages.2.content.0", + reason: "prefix_binding_mismatch", + }, + ], + }, + }, + ], +}; + +describe("InteractiveMode assistant diagnostics", () => { + test("shows Anthropic thinking drops when cache miss notices are enabled", () => { + const maybeShowAssistantDiagnostics = Reflect.get(InteractiveMode.prototype, "maybeShowAssistantDiagnostics") as ( + this: { + chatContainer: Container; + settingsManager: { getShowCacheMissNotices(): boolean }; + }, + message: AssistantMessage, + ) => void; + + initTheme("dark"); + const enabled = { + chatContainer: new Container(), + settingsManager: { getShowCacheMissNotices: () => true }, + }; + maybeShowAssistantDiagnostics.call(enabled, message); + const output = stripAnsi(enabled.chatContainer.render(120).join("\n")); + expect(output).toContain("Anthropic dropped thinking block: prefix_binding_mismatch at messages.2.content.0"); + + const disabled = { + chatContainer: new Container(), + settingsManager: { getShowCacheMissNotices: () => false }, + }; + maybeShowAssistantDiagnostics.call(disabled, message); + expect(disabled.chatContainer.children).toHaveLength(0); + }); +}); diff --git a/packages/coding-agent/test/interactive-mode-clone-command.test.ts b/packages/coding-agent/test/interactive-mode-clone-command.test.ts new file mode 100644 index 0000000000000000000000000000000000000000..69864146aae28d665e70c4193b7b6a0847336c61 --- /dev/null +++ b/packages/coding-agent/test/interactive-mode-clone-command.test.ts @@ -0,0 +1,72 @@ +import { describe, expect, it, vi } from "vitest"; +import { InteractiveMode } from "../src/modes/interactive/interactive-mode.ts"; + +type CloneCommandContext = { + sessionManager: { getLeafId: () => string | null }; + runtimeHost: { + fork: (entryId: string, options?: { position?: "before" | "at" }) => Promise<{ cancelled: boolean }>; + }; + renderCurrentSessionState: () => void; + editor: { setText: (text: string) => void }; + showStatus: (message: string) => void; + showError: (message: string) => void; + ui: { requestRender: () => void }; +}; + +type InteractiveModePrototype = { + handleCloneCommand(this: CloneCommandContext): Promise; +}; + +const interactiveModePrototype = InteractiveMode.prototype as unknown as InteractiveModePrototype; + +describe("InteractiveMode /clone", () => { + it("clones the current leaf into a new session", async () => { + const fork = vi.fn(async () => ({ cancelled: false })); + const renderCurrentSessionState = vi.fn(); + const setText = vi.fn(); + const showStatus = vi.fn(); + const showError = vi.fn(); + const requestRender = vi.fn(); + + const context: CloneCommandContext = { + sessionManager: { getLeafId: () => "leaf-123" }, + runtimeHost: { fork }, + renderCurrentSessionState, + editor: { setText }, + showStatus, + showError, + ui: { requestRender }, + }; + + await interactiveModePrototype.handleCloneCommand.call(context); + + expect(fork).toHaveBeenCalledWith("leaf-123", { position: "at" }); + expect(renderCurrentSessionState).not.toHaveBeenCalled(); + expect(setText).toHaveBeenCalledWith(""); + expect(showStatus).toHaveBeenCalledWith("Cloned to new session"); + expect(showError).not.toHaveBeenCalled(); + expect(requestRender).not.toHaveBeenCalled(); + }); + + it("shows a status message when there is nothing to clone", async () => { + const fork = vi.fn(async () => ({ cancelled: false })); + const showStatus = vi.fn(); + const showError = vi.fn(); + + const context: CloneCommandContext = { + sessionManager: { getLeafId: () => null }, + runtimeHost: { fork }, + renderCurrentSessionState: vi.fn(), + editor: { setText: vi.fn() }, + showStatus, + showError, + ui: { requestRender: vi.fn() }, + }; + + await interactiveModePrototype.handleCloneCommand.call(context); + + expect(fork).not.toHaveBeenCalled(); + expect(showStatus).toHaveBeenCalledWith("Nothing to clone yet"); + expect(showError).not.toHaveBeenCalled(); + }); +}); diff --git a/packages/coding-agent/test/interactive-mode-compaction.test.ts b/packages/coding-agent/test/interactive-mode-compaction.test.ts new file mode 100644 index 0000000000000000000000000000000000000000..378be930378ea3598a3af1496735f91e1292aba1 --- /dev/null +++ b/packages/coding-agent/test/interactive-mode-compaction.test.ts @@ -0,0 +1,256 @@ +import type { Usage } from "@earendil-works/pi-ai"; +import { Container } from "@earendil-works/pi-tui"; +import { describe, expect, test, vi } from "vitest"; +import type { SessionEntry } from "../src/core/session-manager.ts"; +import { InteractiveMode } from "../src/modes/interactive/interactive-mode.ts"; +import { initTheme } from "../src/modes/interactive/theme/theme.ts"; +import { stripAnsi } from "../src/utils/ansi.ts"; + +describe("InteractiveMode compaction events", () => { + test("uses the cache miss notice setting for compaction and branch summary costs", () => { + const usage: Usage = { + input: 10, + output: 20, + cacheRead: 30, + cacheWrite: 40, + totalTokens: 100, + cost: { input: 0.01, output: 0.02, cacheRead: 0.03, cacheWrite: 0.065, total: 0.125 }, + }; + const addCompactionCostNotice = Reflect.get(InteractiveMode.prototype, "addCompactionCostNotice") as ( + this: { chatContainer: Container; settingsManager: { getShowCacheMissNotices(): boolean } }, + notice: { + type: "compaction_cost"; + kind: "compaction" | "branch_summary"; + usage: Usage; + }, + ) => void; + + initTheme("dark"); + const enabled = { + chatContainer: new Container(), + settingsManager: { getShowCacheMissNotices: () => true }, + }; + addCompactionCostNotice.call(enabled, { type: "compaction_cost", kind: "compaction", usage }); + addCompactionCostNotice.call(enabled, { + type: "compaction_cost", + kind: "branch_summary", + usage, + }); + const output = stripAnsi(enabled.chatContainer.render(120).join("\n")); + expect(output).toContain("Compaction: 100 tokens billed (~$0.13)"); + expect(output).toContain("Branch summary: 100 tokens billed (~$0.13)"); + + const disabled = { + chatContainer: new Container(), + settingsManager: { getShowCacheMissNotices: () => false }, + }; + addCompactionCostNotice.call(disabled, { type: "compaction_cost", kind: "compaction", usage }); + expect(disabled.chatContainer.children).toHaveLength(0); + }); + + test("renders each compaction cost after its summary", () => { + const currentUsage: Usage = { + input: 10, + output: 20, + cacheRead: 30, + cacheWrite: 40, + totalTokens: 100, + cost: { input: 0.01, output: 0.02, cacheRead: 0.03, cacheWrite: 0.04, total: 0.1 }, + }; + const previousUsage: Usage = { + input: 1, + output: 2, + cacheRead: 3, + cacheWrite: 4, + totalTokens: 10, + cost: { input: 0.001, output: 0.002, cacheRead: 0.003, cacheWrite: 0.004, total: 0.01 }, + }; + const entries: SessionEntry[] = [ + { + type: "compaction", + id: "current", + parentId: "previous", + timestamp: "2025-01-02T00:00:00Z", + summary: "current summary", + firstKeptEntryId: "kept", + tokensBefore: 200, + usage: currentUsage, + }, + { + type: "compaction", + id: "previous", + parentId: null, + timestamp: "2025-01-01T00:00:00Z", + summary: "previous summary", + firstKeptEntryId: "kept", + tokensBefore: 100, + usage: previousUsage, + }, + ]; + const fakeThis = { renderSessionItems: vi.fn() }; + const renderSessionEntries = Reflect.get(InteractiveMode.prototype, "renderSessionEntries") as ( + this: typeof fakeThis, + entries: SessionEntry[], + ) => void; + + renderSessionEntries.call(fakeThis, entries); + + expect(fakeThis.renderSessionItems).toHaveBeenCalledWith( + [ + expect.objectContaining({ role: "compactionSummary", summary: "current summary" }), + { type: "compaction_cost", kind: "compaction", usage: currentUsage }, + expect.objectContaining({ role: "compactionSummary", summary: "previous summary" }), + { type: "compaction_cost", kind: "compaction", usage: previousUsage }, + ], + {}, + ); + }); + + test("renders retained entries and appends the latest summary cost at the bottom", async () => { + const usage: Usage = { + input: 10, + output: 20, + cacheRead: 30, + cacheWrite: 40, + totalTokens: 100, + cost: { input: 0.01, output: 0.02, cacheRead: 0.03, cacheWrite: 0.065, total: 0.125 }, + }; + const latestCompaction: SessionEntry = { + type: "compaction", + id: "latest", + parentId: "previous", + timestamp: "2025-01-02T00:00:00Z", + summary: "summary", + firstKeptEntryId: "kept", + tokensBefore: 123, + usage, + }; + const previousCompaction: SessionEntry = { + type: "compaction", + id: "previous", + parentId: null, + timestamp: "2025-01-01T00:00:00Z", + summary: "previous summary", + firstKeptEntryId: "kept", + tokensBefore: 100, + usage, + }; + const fakeThis = { + isInitialized: true, + footer: { invalidate: vi.fn() }, + autoCompactionEscapeHandler: undefined as (() => void) | undefined, + autoCompactionLoader: undefined, + defaultEditor: {}, + statusContainer: { clear: vi.fn() }, + chatContainer: { clear: vi.fn() }, + sessionManager: { buildContextEntries: vi.fn().mockReturnValue([latestCompaction, previousCompaction]) }, + renderSessionEntries: vi.fn(), + addMessageToChat: vi.fn(), + addCompactionCostNotice: vi.fn(), + showError: vi.fn(), + showStatus: vi.fn(), + clearStatusIndicator: vi.fn(), + flushCompactionQueue: vi.fn().mockResolvedValue(undefined), + settingsManager: { getShowTerminalProgress: () => false }, + ui: { requestRender: vi.fn(), terminal: { setProgress: vi.fn() } }, + }; + + const handleEvent = Reflect.get(InteractiveMode.prototype, "handleEvent") as ( + this: typeof fakeThis, + event: { + type: "compaction_end"; + reason: "manual" | "threshold" | "overflow"; + result: { tokensBefore: number; summary: string; usage?: Usage } | undefined; + aborted: boolean; + willRetry: boolean; + errorMessage?: string; + }, + ) => Promise; + + await handleEvent.call(fakeThis, { + type: "compaction_end", + reason: "manual", + result: { + tokensBefore: 123, + summary: "summary", + usage, + }, + aborted: false, + willRetry: false, + }); + + expect(fakeThis.chatContainer.clear).toHaveBeenCalledTimes(1); + expect(fakeThis.renderSessionEntries).toHaveBeenCalledWith([previousCompaction]); + expect(fakeThis.addMessageToChat).toHaveBeenCalledTimes(1); + expect(fakeThis.addMessageToChat).toHaveBeenCalledWith( + expect.objectContaining({ + role: "compactionSummary", + tokensBefore: 123, + summary: "summary", + }), + ); + expect(fakeThis.addCompactionCostNotice).toHaveBeenCalledWith({ + type: "compaction_cost", + kind: "compaction", + usage, + }); + expect(fakeThis.flushCompactionQueue).toHaveBeenCalledWith({ willRetry: false }); + }); + + test("updates the working state when the same agent run resumes after compaction", async () => { + const fakeThis = { + isInitialized: true, + footer: { invalidate: vi.fn() }, + activeStatusIndicator: undefined, + workingVisible: true, + showWorkingStatusIndicator: vi.fn(), + clearStatusIndicator: vi.fn(), + settingsManager: { getShowTerminalProgress: () => true }, + ui: { requestRender: vi.fn(), terminal: { setProgress: vi.fn() } }, + }; + const handleEvent = Reflect.get(InteractiveMode.prototype, "handleEvent") as ( + this: typeof fakeThis, + event: { type: "turn_start" }, + ) => Promise; + + await handleEvent.call(fakeThis, { type: "turn_start" }); + + expect(fakeThis.ui.terminal.setProgress).toHaveBeenCalledWith(true); + expect(fakeThis.showWorkingStatusIndicator).toHaveBeenCalledTimes(1); + expect(fakeThis.clearStatusIndicator).not.toHaveBeenCalled(); + expect(fakeThis.ui.requestRender).toHaveBeenCalledTimes(1); + + fakeThis.workingVisible = false; + await handleEvent.call(fakeThis, { type: "turn_start" }); + + expect(fakeThis.showWorkingStatusIndicator).toHaveBeenCalledTimes(1); + expect(fakeThis.clearStatusIndicator).toHaveBeenCalledTimes(1); + expect(fakeThis.ui.requestRender).toHaveBeenCalledTimes(2); + }); + + test("preserves steering behavior when flushing into an active agent run", async () => { + const fakeThis = { + compactionQueuedMessages: [{ text: "change direction", mode: "steer" as const }], + session: { + clearQueue: vi.fn(), + prompt: vi.fn().mockResolvedValue(undefined), + steer: vi.fn().mockResolvedValue(undefined), + followUp: vi.fn().mockResolvedValue(undefined), + }, + isExtensionCommand: vi.fn().mockReturnValue(false), + updatePendingMessagesDisplay: vi.fn(), + showError: vi.fn(), + }; + + const flushCompactionQueue = Reflect.get(InteractiveMode.prototype, "flushCompactionQueue") as ( + this: typeof fakeThis, + options?: { willRetry?: boolean }, + ) => Promise; + + await flushCompactionQueue.call(fakeThis, { willRetry: false }); + + expect(fakeThis.session.prompt).toHaveBeenCalledWith("change direction", { streamingBehavior: "steer" }); + expect(fakeThis.compactionQueuedMessages).toEqual([]); + expect(fakeThis.showError).not.toHaveBeenCalled(); + }); +}); diff --git a/packages/coding-agent/test/interactive-mode-import-command.test.ts b/packages/coding-agent/test/interactive-mode-import-command.test.ts new file mode 100644 index 0000000000000000000000000000000000000000..cec7a5defe36a5ac987beefa26fef06b1f37418e --- /dev/null +++ b/packages/coding-agent/test/interactive-mode-import-command.test.ts @@ -0,0 +1,143 @@ +import { describe, expect, it, vi } from "vitest"; +import { SessionImportFileNotFoundError } from "../src/core/agent-session-runtime.ts"; +import { InteractiveMode } from "../src/modes/interactive/interactive-mode.ts"; + +type PathCommand = "/export" | "/import"; + +type InteractiveModePrototype = { + getPathCommandArgument(this: unknown, text: string, command: PathCommand): string | undefined; + handleImportCommand(this: ImportCommandContext, text: string): Promise; +}; + +type ImportCommandContext = { + clearStatusIndicator: () => void; + runtimeHost: { importFromJsonl: (inputPath: string, cwdOverride?: string) => Promise<{ cancelled: boolean }> }; + showError: (message: string) => void; + showStatus: (message: string) => void; + showExtensionConfirm: (title: string, message: string) => Promise; + handleRuntimeSessionChange: () => Promise; + renderCurrentSessionState: () => void; + handleFatalRuntimeError: (prefix: string, error: unknown) => Promise; + promptForMissingSessionCwd: (error: unknown) => Promise; + getPathCommandArgument: (text: string, command: PathCommand) => string | undefined; +}; + +const interactiveModePrototype = InteractiveMode.prototype as unknown as InteractiveModePrototype; + +describe("InteractiveMode /import parsing", () => { + it("strips quotes from /import path arguments", () => { + expect(interactiveModePrototype.getPathCommandArgument('/import "path/to/session.jsonl"', "/import")).toBe( + "path/to/session.jsonl", + ); + expect( + interactiveModePrototype.getPathCommandArgument('/import "path with spaces/session.jsonl"', "/import"), + ).toBe("path with spaces/session.jsonl"); + }); + + it("preserves apostrophes in unquoted /import path arguments", () => { + expect(interactiveModePrototype.getPathCommandArgument("/import john's/session.jsonl", "/import")).toBe( + "john's/session.jsonl", + ); + }); + + it("enforces command token boundaries", () => { + expect(interactiveModePrototype.getPathCommandArgument("/important /tmp/session.jsonl", "/import")).toBe( + undefined, + ); + expect(interactiveModePrototype.getPathCommandArgument("/exporter out.html", "/export")).toBe(undefined); + expect(interactiveModePrototype.getPathCommandArgument("/import /tmp/session.jsonl", "/import")).toBe( + "/tmp/session.jsonl", + ); + }); + + it("passes unquoted path to runtimeHost.importFromJsonl", async () => { + const importFromJsonl = vi.fn(async () => ({ cancelled: false })); + const showExtensionConfirm = vi.fn(async () => true); + const showStatus = vi.fn(); + const showError = vi.fn(); + + const context: ImportCommandContext = { + clearStatusIndicator: vi.fn(), + runtimeHost: { importFromJsonl }, + showError, + showStatus, + showExtensionConfirm, + handleRuntimeSessionChange: vi.fn(async () => {}), + renderCurrentSessionState: vi.fn(), + handleFatalRuntimeError: vi.fn(async () => { + throw new Error("unexpected fatal error"); + }), + promptForMissingSessionCwd: vi.fn(async () => undefined), + getPathCommandArgument: interactiveModePrototype.getPathCommandArgument, + }; + + await interactiveModePrototype.handleImportCommand.call(context, '/import "path/to/session.jsonl"'); + + expect(showExtensionConfirm).toHaveBeenCalledWith( + "Import session", + "Replace current session with path/to/session.jsonl?", + ); + expect(importFromJsonl).toHaveBeenCalledWith("path/to/session.jsonl"); + expect(showError).not.toHaveBeenCalled(); + expect(showStatus).toHaveBeenCalledWith("Session imported from: path/to/session.jsonl"); + }); + + it("passes unquoted apostrophe path to runtimeHost.importFromJsonl unchanged", async () => { + const importFromJsonl = vi.fn(async () => ({ cancelled: false })); + const showExtensionConfirm = vi.fn(async () => true); + const showStatus = vi.fn(); + const showError = vi.fn(); + + const context: ImportCommandContext = { + clearStatusIndicator: vi.fn(), + runtimeHost: { importFromJsonl }, + showError, + showStatus, + showExtensionConfirm, + handleRuntimeSessionChange: vi.fn(async () => {}), + renderCurrentSessionState: vi.fn(), + handleFatalRuntimeError: vi.fn(async () => { + throw new Error("unexpected fatal error"); + }), + promptForMissingSessionCwd: vi.fn(async () => undefined), + getPathCommandArgument: interactiveModePrototype.getPathCommandArgument, + }; + + await interactiveModePrototype.handleImportCommand.call(context, "/import john's/session.jsonl"); + + expect(importFromJsonl).toHaveBeenCalledWith("john's/session.jsonl"); + expect(showError).not.toHaveBeenCalled(); + expect(showStatus).toHaveBeenCalledWith("Session imported from: john's/session.jsonl"); + }); + + it("shows a non-fatal error when /import path does not exist", async () => { + const importFromJsonl = vi.fn(async () => { + throw new SessionImportFileNotFoundError("/tmp/missing-session.jsonl"); + }); + const showExtensionConfirm = vi.fn(async () => true); + const showStatus = vi.fn(); + const showError = vi.fn(); + const handleFatalRuntimeError = vi.fn(async () => { + throw new Error("unexpected fatal error"); + }); + + const context: ImportCommandContext = { + clearStatusIndicator: vi.fn(), + runtimeHost: { importFromJsonl }, + showError, + showStatus, + showExtensionConfirm, + handleRuntimeSessionChange: vi.fn(async () => {}), + renderCurrentSessionState: vi.fn(), + handleFatalRuntimeError, + promptForMissingSessionCwd: vi.fn(async () => undefined), + getPathCommandArgument: interactiveModePrototype.getPathCommandArgument, + }; + + await interactiveModePrototype.handleImportCommand.call(context, "/import /tmp/missing-session.jsonl"); + + expect(showError).toHaveBeenCalledWith("Failed to import session: File not found: /tmp/missing-session.jsonl"); + expect(showStatus).not.toHaveBeenCalled(); + expect(handleFatalRuntimeError).not.toHaveBeenCalled(); + }); +}); diff --git a/packages/coding-agent/test/interactive-mode-startup-input.test.ts b/packages/coding-agent/test/interactive-mode-startup-input.test.ts new file mode 100644 index 0000000000000000000000000000000000000000..28a0f48b80c0c29f3fd67bfd66abebe5d4249a58 --- /dev/null +++ b/packages/coding-agent/test/interactive-mode-startup-input.test.ts @@ -0,0 +1,90 @@ +import { describe, expect, it, vi } from "vitest"; +import { InteractiveMode } from "../src/modes/interactive/interactive-mode.ts"; + +type SubmitContext = { + defaultEditor: { onSubmit?: (text: string) => void }; + editor: { + addToHistory?: (text: string) => void; + setText: (text: string) => void; + }; + session: { + isCompacting: boolean; + isStreaming: boolean; + isBashRunning: boolean; + prompt: (text: string, options?: unknown) => Promise; + }; + flushPendingBashComponents: () => void; + onInputCallback?: (text: string) => void; + pendingUserInputs: string[]; +}; + +type InputContext = { + onInputCallback?: (text: string) => void; + pendingUserInputs: string[]; +}; + +type StartupSubmitContext = { + editor: { setText: (text: string) => void }; + showStatus: (message: string) => void; +}; + +type InteractiveModePrivate = { + handleStartupSubmit(this: StartupSubmitContext, text: string): void; + setupEditorSubmitHandler(this: SubmitContext): void; + getUserInput(this: InputContext): Promise; +}; + +const interactiveModePrototype = InteractiveMode.prototype as unknown as InteractiveModePrivate; + +function createSubmitContext(): SubmitContext { + return { + defaultEditor: {}, + editor: { + addToHistory: vi.fn(), + setText: vi.fn(), + }, + session: { + isCompacting: false, + isStreaming: false, + isBashRunning: false, + prompt: vi.fn(async () => {}), + }, + flushPendingBashComponents: vi.fn(), + pendingUserInputs: [], + }; +} + +describe("InteractiveMode startup input", () => { + it("restores a prompt submitted while managed-tool setup is running", () => { + const context: StartupSubmitContext = { + editor: { setText: vi.fn() }, + showStatus: vi.fn(), + }; + + interactiveModePrototype.handleStartupSubmit.call(context, "early prompt"); + + expect(context.editor.setText).toHaveBeenCalledWith("early prompt"); + expect(context.showStatus).toHaveBeenCalledWith("Startup is still in progress"); + }); + + it("queues a normal prompt submitted before the input callback is installed", async () => { + const context = createSubmitContext(); + interactiveModePrototype.setupEditorSubmitHandler.call(context); + + await context.defaultEditor.onSubmit?.(" early prompt "); + + expect(context.pendingUserInputs).toEqual(["early prompt"]); + expect(context.flushPendingBashComponents).toHaveBeenCalledTimes(1); + expect(context.editor.addToHistory).toHaveBeenCalledWith("early prompt"); + }); + + it("returns queued startup input before installing a new input callback", async () => { + const context: InputContext = { + pendingUserInputs: ["queued prompt"], + }; + + await expect(interactiveModePrototype.getUserInput.call(context)).resolves.toBe("queued prompt"); + expect(context.onInputCallback).toBeUndefined(); + expect(context.pendingUserInputs).toEqual([]); + }); +}); diff --git a/packages/coding-agent/test/interactive-mode-status.test.ts b/packages/coding-agent/test/interactive-mode-status.test.ts new file mode 100644 index 0000000000000000000000000000000000000000..a4aaa48c02a0510f52b88a95d2a1bd9e02d81169 --- /dev/null +++ b/packages/coding-agent/test/interactive-mode-status.test.ts @@ -0,0 +1,1251 @@ +import { homedir } from "node:os"; +import * as path from "node:path"; +import { type AutocompleteProvider, CombinedAutocompleteProvider } from "@earendil-works/pi-tui"; +import { beforeAll, describe, expect, test, vi } from "vitest"; +import { type Component, Container, type Focusable, type TUI } from "../../tui/src/tui.ts"; +import { TuiMainScreen } from "../../tui/src/tui-main-screen.ts"; +import { VirtualTerminal } from "../../tui/test/virtual-terminal.ts"; +import type { AutocompleteProviderFactory } from "../src/core/extensions/types.ts"; +import type { SourceInfo } from "../src/core/source-info.ts"; +import type { AuthSelectorProvider } from "../src/modes/interactive/components/oauth-selector.ts"; +import { InteractiveMode } from "../src/modes/interactive/interactive-mode.ts"; +import { initTheme } from "../src/modes/interactive/theme/theme.ts"; + +function renderLastLine(container: Container, width = 120): string { + const last = container.children[container.children.length - 1]; + if (!last) return ""; + return last.render(width).join("\n"); +} + +function renderAll(container: Container, width = 120): string { + return container.children.flatMap((child) => child.render(width)).join("\n"); +} + +class TestFocusableComponent implements Component, Focusable { + focused = false; + inputs: string[] = []; + private readonly label: string; + private text = ""; + + constructor(label: string) { + this.label = label; + } + + handleInput(data: string): void { + this.inputs.push(data); + } + + getText(): string { + return this.text; + } + + setText(text: string): void { + this.text = text; + } + + render(): string[] { + return [this.label]; + } + + invalidate(): void {} +} + +async function flushTui(tui: TUI, terminal: VirtualTerminal): Promise { + tui.requestRender(true); + await Promise.resolve(); + await terminal.waitForRender(); +} + +function normalizeRenderedOutput(container: Container, width = 220): string { + return renderAll(container, width) + .replace(/\u001b\[[0-9;]*m/g, "") + .replace(/\\/g, "/") + .split("\n") + .map((line) => line.replace(/\s+$/g, "")) + .join("\n") + .trim(); +} + +type ExtensionFixture = { + path: string; + sourceInfo?: SourceInfo; +}; + +describe("InteractiveMode.showStatus", () => { + beforeAll(() => { + // showStatus uses the global theme instance + initTheme("dark"); + }); + + test("coalesces immediately-sequential status messages", () => { + const fakeThis: any = { + chatContainer: new Container(), + ui: { requestRender: vi.fn() }, + lastStatusSpacer: undefined, + lastStatusText: undefined, + }; + + (InteractiveMode as any).prototype.showStatus.call(fakeThis, "STATUS_ONE"); + expect(fakeThis.chatContainer.children).toHaveLength(2); + expect(renderLastLine(fakeThis.chatContainer)).toContain("STATUS_ONE"); + + (InteractiveMode as any).prototype.showStatus.call(fakeThis, "STATUS_TWO"); + // second status updates the previous line instead of appending + expect(fakeThis.chatContainer.children).toHaveLength(2); + expect(renderLastLine(fakeThis.chatContainer)).toContain("STATUS_TWO"); + expect(renderLastLine(fakeThis.chatContainer)).not.toContain("STATUS_ONE"); + }); + + test("appends a new status line if something else was added in between", () => { + const fakeThis: any = { + chatContainer: new Container(), + ui: { requestRender: vi.fn() }, + lastStatusSpacer: undefined, + lastStatusText: undefined, + }; + + (InteractiveMode as any).prototype.showStatus.call(fakeThis, "STATUS_ONE"); + expect(fakeThis.chatContainer.children).toHaveLength(2); + + // Something else gets added to the chat in between status updates + fakeThis.chatContainer.addChild({ render: () => ["OTHER"], invalidate: () => {} }); + expect(fakeThis.chatContainer.children).toHaveLength(3); + + (InteractiveMode as any).prototype.showStatus.call(fakeThis, "STATUS_TWO"); + // adds spacer + text + expect(fakeThis.chatContainer.children).toHaveLength(5); + expect(renderLastLine(fakeThis.chatContainer)).toContain("STATUS_TWO"); + }); +}); + +describe("InteractiveMode.showManagedToolStatus", () => { + beforeAll(() => initTheme("dark")); + + test("renders tool updates as one contiguous group", () => { + const fakeThis: any = { + chatContainer: new Container(), + ui: { requestRender: vi.fn() }, + managedToolStatusStarted: false, + lastStatusSpacer: undefined, + lastStatusText: undefined, + }; + const showManagedToolStatus = (InteractiveMode as any).prototype.showManagedToolStatus; + + showManagedToolStatus.call(fakeThis, { type: "info", message: "fd downloading" }); + showManagedToolStatus.call(fakeThis, { type: "info", message: "rg downloading" }); + showManagedToolStatus.call(fakeThis, { type: "warning", message: "rg failed" }); + + expect(fakeThis.chatContainer.children).toHaveLength(4); + expect(normalizeRenderedOutput(fakeThis.chatContainer)).toBe( + "fd downloading\n rg downloading\n Warning: rg failed", + ); + }); +}); + +describe("InteractiveMode.setToolsExpanded", () => { + test("applies expansion state to the active header and chat entries", () => { + const header = { setExpanded: vi.fn() }; + const loadedResourcesChild = { setExpanded: vi.fn() }; + const chatChild = { setExpanded: vi.fn() }; + const fakeThis: any = { + toolOutputExpanded: false, + customHeader: undefined, + builtInHeader: header, + loadedResourcesContainer: { children: [loadedResourcesChild] }, + chatContainer: { children: [chatChild] }, + ui: { requestRender: vi.fn() }, + showStatus: vi.fn(), + }; + + (InteractiveMode as any).prototype.setToolsExpanded.call(fakeThis, true); + + expect(fakeThis.toolOutputExpanded).toBe(true); + expect(header.setExpanded).toHaveBeenCalledWith(true); + expect(loadedResourcesChild.setExpanded).toHaveBeenCalledWith(true); + expect(chatChild.setExpanded).toHaveBeenCalledWith(true); + expect(fakeThis.showStatus).toHaveBeenCalledWith("Tool output: expanded"); + }); +}); + +describe("InteractiveMode.createExtensionUIContext setTheme", () => { + test("persists theme changes to settings manager", () => { + initTheme("dark"); + + let currentTheme = "dark"; + const settingsManager = { + getTheme: vi.fn(() => currentTheme), + setTheme: vi.fn((theme: string) => { + currentTheme = theme; + }), + }; + const fakeThis: any = { + session: { settingsManager }, + settingsManager, + themeController: { + setThemeInstance: vi.fn(() => ({ success: true })), + setThemeName: vi.fn(() => { + fakeThis.ui.requestRender(); + return { success: true }; + }), + }, + ui: { requestRender: vi.fn() }, + }; + + const uiContext = (InteractiveMode as any).prototype.createExtensionUIContext.call(fakeThis); + const result = uiContext.setTheme("light"); + + expect(result.success).toBe(true); + expect(fakeThis.themeController.setThemeName).toHaveBeenCalledWith("light"); + expect(settingsManager.setTheme).toHaveBeenCalledWith("light"); + expect(currentTheme).toBe("light"); + expect(fakeThis.ui.requestRender).toHaveBeenCalledTimes(1); + }); + + test("does not persist invalid theme names", () => { + initTheme("dark"); + + const settingsManager = { + getTheme: vi.fn(() => "dark"), + setTheme: vi.fn(), + }; + const fakeThis: any = { + session: { settingsManager }, + settingsManager, + themeController: { + setThemeInstance: vi.fn(() => ({ success: true })), + setThemeName: vi.fn(() => ({ success: false, error: "Theme not found" })), + }, + ui: { requestRender: vi.fn() }, + }; + + const uiContext = (InteractiveMode as any).prototype.createExtensionUIContext.call(fakeThis); + const result = uiContext.setTheme("__missing_theme__"); + + expect(result.success).toBe(false); + expect(fakeThis.themeController.setThemeName).toHaveBeenCalledWith("__missing_theme__"); + expect(settingsManager.setTheme).not.toHaveBeenCalled(); + expect(fakeThis.ui.requestRender).not.toHaveBeenCalled(); + }); +}); + +describe("InteractiveMode.showExtensionCustom", () => { + beforeAll(() => { + initTheme("dark"); + }); + + test("overlay custom UI reclaims input after non-overlay custom UI closes", async () => { + const terminal = new VirtualTerminal(80, 24); + const ui: TUI = new TuiMainScreen(terminal); + const editorContainer = new Container(); + const editor = new TestFocusableComponent("EDITOR"); + const palette = new TestFocusableComponent("PALETTE"); + const overlay = new TestFocusableComponent("OVERLAY"); + const replacement = new TestFocusableComponent("REPLACEMENT"); + let closeOverlay: (value: string) => void = () => { + throw new Error("closeOverlay was not initialized"); + }; + let closeReplacement: (value: string) => void = () => { + throw new Error("closeReplacement was not initialized"); + }; + const fakeThis = { + editor, + editorContainer, + keybindings: {}, + ui, + disposeActiveSelector: vi.fn(), + }; + const showExtensionCustom = ( + factory: (tui: TUI, theme: unknown, keybindings: unknown, done: (result: T) => void) => Component, + options?: { overlay?: boolean }, + ): Promise => + (InteractiveMode as any).prototype.showExtensionCustom.call(fakeThis, factory, options) as Promise; + + editorContainer.addChild(editor); + ui.addChild(editorContainer); + ui.addChild(palette); + ui.setFocus(palette); + ui.start(); + try { + const overlayPromise = showExtensionCustom( + (_tui, _theme, _keybindings, done) => { + closeOverlay = done; + return overlay; + }, + { overlay: true }, + ); + await flushTui(ui, terminal); + expect(overlay.focused).toBe(true); + + const replacementPromise = showExtensionCustom((_tui, _theme, _keybindings, done) => { + closeReplacement = done; + return replacement; + }); + await flushTui(ui, terminal); + expect(replacement.focused).toBe(true); + + closeReplacement("done"); + await replacementPromise; + await flushTui(ui, terminal); + terminal.sendInput("x"); + await flushTui(ui, terminal); + + expect(overlay.inputs).toEqual(["x"]); + expect(editor.inputs).toEqual([]); + expect(overlay.focused).toBe(true); + + closeOverlay("closed"); + await overlayPromise; + } finally { + ui.stop(); + } + }); +}); + +describe("InteractiveMode.createExtensionUIContext addAutocompleteProvider", () => { + test("stores wrapper factories and rebuilds autocomplete immediately", () => { + const wrapper: AutocompleteProviderFactory = (current) => current; + const fakeThis = { + autocompleteProviderWrappers: [] as AutocompleteProviderFactory[], + setupAutocompleteProvider: vi.fn(), + }; + + const uiContext = (InteractiveMode as any).prototype.createExtensionUIContext.call(fakeThis); + uiContext.addAutocompleteProvider(wrapper); + + expect(fakeThis.autocompleteProviderWrappers).toEqual([wrapper]); + expect(fakeThis.setupAutocompleteProvider).toHaveBeenCalledTimes(1); + }); +}); + +describe("InteractiveMode.setupAutocompleteProvider", () => { + test("stacks wrapper factories over a fresh base provider", () => { + const defaultEditor = { setAutocompleteProvider: vi.fn() }; + const customEditor = { setAutocompleteProvider: vi.fn() }; + const calls: string[] = []; + + const wrap1: AutocompleteProviderFactory = (current): AutocompleteProvider => ({ + async getSuggestions(lines, cursorLine, cursorCol, options) { + calls.push("getSuggestions:wrap1"); + return current.getSuggestions(lines, cursorLine, cursorCol, options); + }, + applyCompletion(lines, cursorLine, cursorCol, item, prefix) { + calls.push("applyCompletion:wrap1"); + return current.applyCompletion(lines, cursorLine, cursorCol, item, prefix); + }, + shouldTriggerFileCompletion(lines, cursorLine, cursorCol) { + calls.push("shouldTrigger:wrap1"); + return current.shouldTriggerFileCompletion?.(lines, cursorLine, cursorCol) ?? true; + }, + }); + const wrap2: AutocompleteProviderFactory = (current): AutocompleteProvider => ({ + async getSuggestions(lines, cursorLine, cursorCol, options) { + calls.push("getSuggestions:wrap2"); + return current.getSuggestions(lines, cursorLine, cursorCol, options); + }, + applyCompletion(lines, cursorLine, cursorCol, item, prefix) { + calls.push("applyCompletion:wrap2"); + return current.applyCompletion(lines, cursorLine, cursorCol, item, prefix); + }, + shouldTriggerFileCompletion(lines, cursorLine, cursorCol) { + calls.push("shouldTrigger:wrap2"); + return current.shouldTriggerFileCompletion?.(lines, cursorLine, cursorCol) ?? true; + }, + }); + + const fakeThis = { + createBaseAutocompleteProvider: () => new CombinedAutocompleteProvider([], "/tmp/project", undefined), + defaultEditor, + editor: customEditor, + autocompleteProviderWrappers: [wrap1, wrap2], + }; + + (InteractiveMode as any).prototype.setupAutocompleteProvider.call(fakeThis); + + expect(defaultEditor.setAutocompleteProvider).toHaveBeenCalledTimes(1); + expect(customEditor.setAutocompleteProvider).toHaveBeenCalledTimes(1); + const provider = defaultEditor.setAutocompleteProvider.mock.calls[0]?.[0] as AutocompleteProvider; + expect(provider).toBe(customEditor.setAutocompleteProvider.mock.calls[0]?.[0]); + expect(provider.shouldTriggerFileCompletion?.(["foo"], 0, 3)).toBe(true); + expect(calls).toEqual(["shouldTrigger:wrap2", "shouldTrigger:wrap1"]); + }); + + test("merges triggerCharacters from wrapper factories", () => { + const defaultEditor = { setAutocompleteProvider: vi.fn() }; + const customEditor = { setAutocompleteProvider: vi.fn() }; + const passThrough = + (triggerCharacters: string[]): AutocompleteProviderFactory => + (current) => ({ + triggerCharacters, + getSuggestions: (lines, cursorLine, cursorCol, options) => + current.getSuggestions(lines, cursorLine, cursorCol, options), + applyCompletion: (lines, cursorLine, cursorCol, item, prefix) => + current.applyCompletion(lines, cursorLine, cursorCol, item, prefix), + }); + + const fakeThis = { + createBaseAutocompleteProvider: () => new CombinedAutocompleteProvider([], "/tmp/project", undefined), + defaultEditor, + editor: customEditor, + autocompleteProviderWrappers: [passThrough(["$"]), passThrough(["!"])], + }; + + ( + InteractiveMode as unknown as { + prototype: { setupAutocompleteProvider: (this: typeof fakeThis) => void }; + } + ).prototype.setupAutocompleteProvider.call(fakeThis); + + const provider = defaultEditor.setAutocompleteProvider.mock.calls[0]?.[0] as AutocompleteProvider; + expect(provider.triggerCharacters).toEqual(["$", "!"]); + }); +}); + +describe("InteractiveMode.createBaseAutocompleteProvider", () => { + test("matches model command arguments across provider/model order", async () => { + type TestModel = { id: string; provider: string; name: string }; + type FakeInteractiveMode = { + session: { + scopedModels: Array<{ model: TestModel }>; + modelRuntime: { getAvailableSnapshot: () => TestModel[] }; + promptTemplates: []; + extensionRunner: { getRegisteredCommands: () => [] }; + resourceLoader: { getSkills: () => { skills: [] } }; + }; + settingsManager: { getEnableSkillCommands: () => boolean }; + skillCommands: Map; + sessionManager: { getCwd: () => string }; + fdPath: null; + }; + + const createBaseAutocompleteProvider = ( + InteractiveMode as unknown as { + prototype: { createBaseAutocompleteProvider(this: FakeInteractiveMode): AutocompleteProvider }; + } + ).prototype.createBaseAutocompleteProvider; + const models = [ + { id: "gpt-5.2-codex", provider: "github-copilot", name: "GPT-5.2 Codex" }, + { id: "gpt-5.5", provider: "openai-codex", name: "GPT-5.5" }, + ]; + const fakeThis: FakeInteractiveMode = { + session: { + scopedModels: [], + modelRuntime: { getAvailableSnapshot: () => models }, + promptTemplates: [], + extensionRunner: { getRegisteredCommands: () => [] }, + resourceLoader: { getSkills: () => ({ skills: [] }) }, + }, + settingsManager: { getEnableSkillCommands: () => false }, + skillCommands: new Map(), + sessionManager: { getCwd: () => "/tmp" }, + fdPath: null, + }; + + const provider = createBaseAutocompleteProvider.call(fakeThis); + const line = "/model codexgpt"; + const suggestions = await provider.getSuggestions([line], 0, line.length, { + signal: new AbortController().signal, + }); + + expect(suggestions?.items.map((item) => item.value)).toEqual([ + "openai-codex/gpt-5.5", + "github-copilot/gpt-5.2-codex", + ]); + }); + + test("matches login command arguments by provider id and name", async () => { + type FakeInteractiveMode = { + session: { + scopedModels: []; + modelRuntime: { getAvailableSnapshot: () => [] }; + promptTemplates: []; + extensionRunner: { getRegisteredCommands: () => [] }; + resourceLoader: { getSkills: () => { skills: [] } }; + }; + settingsManager: { getEnableSkillCommands: () => boolean }; + skillCommands: Map; + sessionManager: { getCwd: () => string }; + fdPath: null; + getLoginProviderOptions: () => AuthSelectorProvider[]; + }; + + const createBaseAutocompleteProvider = ( + InteractiveMode as unknown as { + prototype: { createBaseAutocompleteProvider(this: FakeInteractiveMode): AutocompleteProvider }; + } + ).prototype.createBaseAutocompleteProvider; + const fakeThis: FakeInteractiveMode = { + session: { + scopedModels: [], + modelRuntime: { getAvailableSnapshot: () => [] }, + promptTemplates: [], + extensionRunner: { getRegisteredCommands: () => [] }, + resourceLoader: { getSkills: () => ({ skills: [] }) }, + }, + settingsManager: { getEnableSkillCommands: () => false }, + skillCommands: new Map(), + sessionManager: { getCwd: () => "/tmp" }, + fdPath: null, + getLoginProviderOptions: () => [ + { id: "anthropic", name: "Anthropic", authType: "oauth" }, + { id: "anthropic", name: "Anthropic", authType: "api_key" }, + { id: "openai", name: "OpenAI", authType: "api_key" }, + ], + }; + + const provider = createBaseAutocompleteProvider.call(fakeThis); + const line = "/login subscription anthrop"; + const suggestions = await provider.getSuggestions([line], 0, line.length, { + signal: new AbortController().signal, + }); + + expect(suggestions?.items).toEqual([ + { + value: "anthropic", + label: "anthropic", + description: "Anthropic · subscription/API key", + }, + ]); + }); +}); +describe("InteractiveMode.showLoadedResources", () => { + beforeAll(() => { + initTheme("dark"); + }); + + function createShowLoadedResourcesThis(options: { + quietStartup: boolean; + verbose?: boolean; + toolOutputExpanded?: boolean; + cwd?: string; + contextFiles?: Array<{ path: string; content?: string }>; + systemPromptSource?: { path: string }; + appendSystemPromptSources?: Array<{ path: string }>; + extensions?: ExtensionFixture[]; + skills?: Array<{ filePath: string; name: string }>; + skillDiagnostics?: Array<{ type: "warning" | "error" | "collision"; message: string }>; + useRealScopeGroups?: boolean; + }) { + const fakeThis: any = { + options: { verbose: options.verbose ?? false }, + toolOutputExpanded: options.toolOutputExpanded ?? false, + loadedResourcesContainer: new Container(), + chatContainer: new Container(), + settingsManager: { + getQuietStartup: () => options.quietStartup, + }, + sessionManager: { + getCwd: () => options.cwd ?? "/tmp/project", + }, + session: { + promptTemplates: [], + extensionRunner: { + getCommandDiagnostics: () => [], + getShortcutDiagnostics: () => [], + }, + resourceLoader: { + getPathMetadata: () => new Map(), + getAgentsFiles: () => ({ agentsFiles: options.contextFiles ?? [] }), + getSystemPromptSource: () => options.systemPromptSource, + getAppendSystemPromptSources: () => options.appendSystemPromptSources ?? [], + getSkills: () => ({ + skills: options.skills ?? [], + diagnostics: options.skillDiagnostics ?? [], + }), + getPrompts: () => ({ prompts: [], diagnostics: [] }), + getExtensions: () => ({ extensions: options.extensions ?? [], errors: [], runtime: {} }), + getThemes: () => ({ themes: [], diagnostics: [] }), + }, + }, + formatDisplayPath: (p: string) => (InteractiveMode as any).prototype.formatDisplayPath.call(fakeThis, p), + formatExtensionDisplayPath: (p: string) => + (InteractiveMode as any).prototype.formatExtensionDisplayPath.call(fakeThis, p), + formatContextPath: (p: string) => (InteractiveMode as any).prototype.formatContextPath.call(fakeThis, p), + getStartupExpansionState: () => (InteractiveMode as any).prototype.getStartupExpansionState.call(fakeThis), + buildScopeGroups: () => [], + formatScopeGroups: () => "resource-list", + isPackageSource: (sourceInfo?: SourceInfo) => + (InteractiveMode as any).prototype.isPackageSource.call(fakeThis, sourceInfo), + getShortPath: (p: string, sourceInfo?: SourceInfo) => + (InteractiveMode as any).prototype.getShortPath.call(fakeThis, p, sourceInfo), + getCompactPathLabel: (p: string, sourceInfo?: SourceInfo) => + (InteractiveMode as any).prototype.getCompactPathLabel.call(fakeThis, p, sourceInfo), + getCompactPackageSourceLabel: (sourceInfo?: SourceInfo) => + (InteractiveMode as any).prototype.getCompactPackageSourceLabel.call(fakeThis, sourceInfo), + getCompactExtensionLabel: (p: string, sourceInfo?: SourceInfo) => + (InteractiveMode as any).prototype.getCompactExtensionLabel.call(fakeThis, p, sourceInfo), + getCompactDisplayPathSegments: (p: string) => + (InteractiveMode as any).prototype.getCompactDisplayPathSegments.call(fakeThis, p), + getCompactNonPackageExtensionLabel: ( + p: string, + index: number, + allPaths: Array<{ path: string; segments: string[] }>, + ) => (InteractiveMode as any).prototype.getCompactNonPackageExtensionLabel.call(fakeThis, p, index, allPaths), + getCompactExtensionLabels: (extensions: ExtensionFixture[]) => + (InteractiveMode as any).prototype.getCompactExtensionLabels.call(fakeThis, extensions), + formatDiagnostics: () => "diagnostics", + getBuiltInCommandConflictDiagnostics: () => [], + }; + + if (options.useRealScopeGroups) { + fakeThis.getScopeGroup = (sourceInfo?: SourceInfo) => + (InteractiveMode as any).prototype.getScopeGroup.call(fakeThis, sourceInfo); + fakeThis.buildScopeGroups = (items: Array<{ path: string; sourceInfo?: SourceInfo }>) => + (InteractiveMode as any).prototype.buildScopeGroups.call(fakeThis, items); + fakeThis.formatScopeGroups = (groups: unknown, formatOptions: unknown) => + (InteractiveMode as any).prototype.formatScopeGroups.call(fakeThis, groups, formatOptions); + } + + return fakeThis; + } + + function createSourceInfo( + filePath: string, + options: { + source: string; + scope: "user" | "project" | "temporary"; + origin: "package" | "top-level"; + baseDir?: string; + }, + ): SourceInfo { + return { + path: filePath, + source: options.source, + scope: options.scope, + origin: options.origin, + baseDir: options.baseDir, + }; + } + + function createExtensionFixtures(): ExtensionFixture[] { + return [ + { + path: "/tmp/project/.pi/extensions/answer.ts", + sourceInfo: createSourceInfo("/tmp/project/.pi/extensions/answer.ts", { + source: "local", + scope: "project", + origin: "top-level", + baseDir: "/tmp/project/.pi/extensions", + }), + }, + { + path: "/tmp/project/.pi/extensions/local-index/index.ts", + sourceInfo: createSourceInfo("/tmp/project/.pi/extensions/local-index/index.ts", { + source: "local", + scope: "project", + origin: "top-level", + baseDir: "/tmp/project/.pi/extensions", + }), + }, + { + path: "/tmp/agent/extensions/user-index/index.ts", + sourceInfo: createSourceInfo("/tmp/agent/extensions/user-index/index.ts", { + source: "local", + scope: "user", + origin: "top-level", + baseDir: "/tmp/agent/extensions", + }), + }, + { + path: "/tmp/project/.pi/npm/node_modules/pi-markdown-preview/extensions/index.ts", + sourceInfo: createSourceInfo("/tmp/project/.pi/npm/node_modules/pi-markdown-preview/extensions/index.ts", { + source: "npm:pi-markdown-preview", + scope: "project", + origin: "package", + baseDir: "/tmp/project/.pi/npm/node_modules/pi-markdown-preview", + }), + }, + { + path: "/tmp/project/.pi/npm/node_modules/@scope/pi-scoped/extensions/index.ts", + sourceInfo: createSourceInfo("/tmp/project/.pi/npm/node_modules/@scope/pi-scoped/extensions/index.ts", { + source: "npm:@scope/pi-scoped", + scope: "project", + origin: "package", + baseDir: "/tmp/project/.pi/npm/node_modules/@scope/pi-scoped", + }), + }, + { + path: "/tmp/project/.pi/git/github.com/HazAT/pi-interactive-subagents/extensions/index.ts", + sourceInfo: createSourceInfo( + "/tmp/project/.pi/git/github.com/HazAT/pi-interactive-subagents/extensions/index.ts", + { + source: "git:github.com/HazAT/pi-interactive-subagents", + scope: "project", + origin: "package", + baseDir: "/tmp/project/.pi/git/github.com/HazAT/pi-interactive-subagents", + }, + ), + }, + { + path: "/tmp/project/.pi/git/github.com/HazAT/pi-interactive-subagents/extensions/subagents/index.ts", + sourceInfo: createSourceInfo( + "/tmp/project/.pi/git/github.com/HazAT/pi-interactive-subagents/extensions/subagents/index.ts", + { + source: "git:github.com/HazAT/pi-interactive-subagents", + scope: "project", + origin: "package", + baseDir: "/tmp/project/.pi/git/github.com/HazAT/pi-interactive-subagents", + }, + ), + }, + { + path: "/tmp/temp/cli-extension.ts", + sourceInfo: createSourceInfo("/tmp/temp/cli-extension.ts", { + source: "cli", + scope: "temporary", + origin: "top-level", + baseDir: "/tmp/temp", + }), + }, + ]; + } + + test("shows a compact resource listing by default", () => { + const fakeThis = createShowLoadedResourcesThis({ + quietStartup: false, + skills: [{ filePath: "/tmp/skill/SKILL.md", name: "commit" }], + }); + + (InteractiveMode as any).prototype.showLoadedResources.call(fakeThis, { + force: false, + }); + + const output = renderAll(fakeThis.loadedResourcesContainer); + expect(output).toContain("[Skills]"); + expect(output).toContain("commit"); + expect(output).not.toContain("resource-list"); + }); + + test("shows full resource listing when expanded", () => { + const fakeThis = createShowLoadedResourcesThis({ + quietStartup: false, + toolOutputExpanded: true, + skills: [{ filePath: "/tmp/skill/SKILL.md", name: "commit" }], + }); + + (InteractiveMode as any).prototype.showLoadedResources.call(fakeThis, { + force: false, + }); + + const output = renderAll(fakeThis.loadedResourcesContainer); + expect(output).toContain("[Skills]"); + expect(output).toContain("resource-list"); + expect(output).not.toContain("commit"); + }); + + test("shows full resource listing on verbose startup even when tool output is collapsed", () => { + const fakeThis = createShowLoadedResourcesThis({ + quietStartup: true, + verbose: true, + toolOutputExpanded: false, + skills: [{ filePath: "/tmp/skill/SKILL.md", name: "commit" }], + }); + + (InteractiveMode as any).prototype.showLoadedResources.call(fakeThis, { + force: false, + }); + + const output = renderAll(fakeThis.loadedResourcesContainer); + expect(output).toContain("[Skills]"); + expect(output).toContain("resource-list"); + expect(output).not.toContain("commit"); + }); + + test("abbreviates extensions in compact listing", () => { + const fakeThis = createShowLoadedResourcesThis({ + quietStartup: false, + extensions: [{ path: "/tmp/extensions/answer.ts" }, { path: "/tmp/extensions/btw.ts" }], + }); + + (InteractiveMode as any).prototype.showLoadedResources.call(fakeThis, { + force: false, + }); + + const output = renderAll(fakeThis.loadedResourcesContainer); + expect(output).toContain("[Extensions]"); + expect(output).toContain("answer.ts, btw.ts"); + expect(output).not.toContain("extensions/answer.ts"); + }); + + test("captures mixed extension layouts in compact output", () => { + const fakeThis = createShowLoadedResourcesThis({ + quietStartup: false, + extensions: createExtensionFixtures(), + useRealScopeGroups: true, + }); + + (InteractiveMode as any).prototype.showLoadedResources.call(fakeThis, { + force: false, + }); + + expect(normalizeRenderedOutput(fakeThis.loadedResourcesContainer)).toMatchInlineSnapshot(` +"[Extensions] + @scope/pi-scoped, answer.ts, cli-extension.ts, HazAT/pi-interactive-subagents, HazAT/pi-interactive-subagents:subagents, local-index, pi-markdown-preview, user-index"`); + }); + + test("adds more parent folders until local extension labels are unique", () => { + const extensions: ExtensionFixture[] = [ + { + path: "/tmp/alpha/one/index.ts", + sourceInfo: createSourceInfo("/tmp/alpha/one/index.ts", { + source: "cli", + scope: "temporary", + origin: "top-level", + baseDir: "/tmp/alpha", + }), + }, + { + path: "/tmp/beta/one/index.ts", + sourceInfo: createSourceInfo("/tmp/beta/one/index.ts", { + source: "cli", + scope: "temporary", + origin: "top-level", + baseDir: "/tmp/beta", + }), + }, + { + path: "/tmp/gamma/one/index.ts", + sourceInfo: createSourceInfo("/tmp/gamma/one/index.ts", { + source: "cli", + scope: "temporary", + origin: "top-level", + baseDir: "/tmp/gamma", + }), + }, + ]; + + const fakeThis = createShowLoadedResourcesThis({ + quietStartup: false, + extensions, + useRealScopeGroups: true, + }); + + (InteractiveMode as any).prototype.showLoadedResources.call(fakeThis, { + force: false, + }); + + expect(normalizeRenderedOutput(fakeThis.loadedResourcesContainer)).toMatchInlineSnapshot(` +"[Extensions] + alpha/one, beta/one, gamma/one"`); + }); + + test("strips index.ts from local extension label, showing parent dir", () => { + const extensions: ExtensionFixture[] = [ + { + path: "/tmp/extensions/plan-mode/index.ts", + sourceInfo: createSourceInfo("/tmp/extensions/plan-mode/index.ts", { + source: "local", + scope: "project", + origin: "top-level", + baseDir: "/tmp/extensions", + }), + }, + ]; + + const fakeThis = createShowLoadedResourcesThis({ + quietStartup: false, + extensions, + useRealScopeGroups: true, + }); + + (InteractiveMode as any).prototype.showLoadedResources.call(fakeThis, { + force: false, + }); + + expect(normalizeRenderedOutput(fakeThis.loadedResourcesContainer)).toMatchInlineSnapshot(` +"[Extensions] + plan-mode"`); + }); + + test("strips index.js from local extension label, showing parent dir", () => { + const extensions: ExtensionFixture[] = [ + { + path: "/tmp/extensions/plan-mode/index.js", + sourceInfo: createSourceInfo("/tmp/extensions/plan-mode/index.js", { + source: "local", + scope: "project", + origin: "top-level", + baseDir: "/tmp/extensions", + }), + }, + ]; + + const fakeThis = createShowLoadedResourcesThis({ + quietStartup: false, + extensions, + useRealScopeGroups: true, + }); + + (InteractiveMode as any).prototype.showLoadedResources.call(fakeThis, { + force: false, + }); + + expect(normalizeRenderedOutput(fakeThis.loadedResourcesContainer)).toMatchInlineSnapshot(` +"[Extensions] + plan-mode"`); + }); + + test("mixed single-file and subdirectory index.ts extensions strip index.ts", () => { + const extensions: ExtensionFixture[] = [ + { + path: "/tmp/extensions/webfetch.ts", + sourceInfo: createSourceInfo("/tmp/extensions/webfetch.ts", { + source: "local", + scope: "project", + origin: "top-level", + baseDir: "/tmp/extensions", + }), + }, + { + path: "/tmp/extensions/plan-mode/index.ts", + sourceInfo: createSourceInfo("/tmp/extensions/plan-mode/index.ts", { + source: "local", + scope: "project", + origin: "top-level", + baseDir: "/tmp/extensions", + }), + }, + ]; + + const fakeThis = createShowLoadedResourcesThis({ + quietStartup: false, + extensions, + useRealScopeGroups: true, + }); + + (InteractiveMode as any).prototype.showLoadedResources.call(fakeThis, { + force: false, + }); + + expect(normalizeRenderedOutput(fakeThis.loadedResourcesContainer)).toMatchInlineSnapshot(` +"[Extensions] + plan-mode, webfetch.ts"`); + }); + + test("multiple index.ts with unique parent dirs need no disambiguation", () => { + const extensions: ExtensionFixture[] = [ + { + path: "/tmp/extensions/foo/index.ts", + sourceInfo: createSourceInfo("/tmp/extensions/foo/index.ts", { + source: "local", + scope: "project", + origin: "top-level", + baseDir: "/tmp/extensions", + }), + }, + { + path: "/tmp/extensions/bar/index.ts", + sourceInfo: createSourceInfo("/tmp/extensions/bar/index.ts", { + source: "local", + scope: "project", + origin: "top-level", + baseDir: "/tmp/extensions", + }), + }, + ]; + + const fakeThis = createShowLoadedResourcesThis({ + quietStartup: false, + extensions, + useRealScopeGroups: true, + }); + + (InteractiveMode as any).prototype.showLoadedResources.call(fakeThis, { + force: false, + }); + + expect(normalizeRenderedOutput(fakeThis.loadedResourcesContainer)).toMatchInlineSnapshot(` +"[Extensions] + bar, foo"`); + }); + + test("multiple index.ts with same parent dir name disambiguated with grandparent", () => { + const extensions: ExtensionFixture[] = [ + { + path: "/tmp/alpha/tools/index.ts", + sourceInfo: createSourceInfo("/tmp/alpha/tools/index.ts", { + source: "cli", + scope: "temporary", + origin: "top-level", + baseDir: "/tmp/alpha", + }), + }, + { + path: "/tmp/beta/tools/index.ts", + sourceInfo: createSourceInfo("/tmp/beta/tools/index.ts", { + source: "cli", + scope: "temporary", + origin: "top-level", + baseDir: "/tmp/beta", + }), + }, + ]; + + const fakeThis = createShowLoadedResourcesThis({ + quietStartup: false, + extensions, + useRealScopeGroups: true, + }); + + (InteractiveMode as any).prototype.showLoadedResources.call(fakeThis, { + force: false, + }); + + expect(normalizeRenderedOutput(fakeThis.loadedResourcesContainer)).toMatchInlineSnapshot(` +"[Extensions] + alpha/tools, beta/tools"`); + }); + + test("non-index file in subdirectory stays as filename", () => { + const extensions: ExtensionFixture[] = [ + { + path: "/tmp/extensions/my-ext/main.ts", + sourceInfo: createSourceInfo("/tmp/extensions/my-ext/main.ts", { + source: "local", + scope: "project", + origin: "top-level", + baseDir: "/tmp/extensions", + }), + }, + ]; + + const fakeThis = createShowLoadedResourcesThis({ + quietStartup: false, + extensions, + useRealScopeGroups: true, + }); + + (InteractiveMode as any).prototype.showLoadedResources.call(fakeThis, { + force: false, + }); + + expect(normalizeRenderedOutput(fakeThis.loadedResourcesContainer)).toMatchInlineSnapshot(` +"[Extensions] + main.ts"`); + }); + + test("package extensions still strip index.ts correctly (regression guard)", () => { + const extensions: ExtensionFixture[] = [ + { + path: "/tmp/project/.pi/npm/node_modules/pi-markdown-preview/extensions/index.ts", + sourceInfo: createSourceInfo("/tmp/project/.pi/npm/node_modules/pi-markdown-preview/extensions/index.ts", { + source: "npm:pi-markdown-preview", + scope: "project", + origin: "package", + baseDir: "/tmp/project/.pi/npm/node_modules/pi-markdown-preview", + }), + }, + ]; + + const fakeThis = createShowLoadedResourcesThis({ + quietStartup: false, + extensions, + useRealScopeGroups: true, + }); + + (InteractiveMode as any).prototype.showLoadedResources.call(fakeThis, { + force: false, + }); + + expect(normalizeRenderedOutput(fakeThis.loadedResourcesContainer)).toMatchInlineSnapshot(` +"[Extensions] + pi-markdown-preview"`); + }); + + test("labels npm sibling extensions relative to the declaring package", () => { + const extensions: ExtensionFixture[] = [ + { + path: "/tmp/project/.pi/npm/node_modules/primary-package/index.ts", + sourceInfo: createSourceInfo("/tmp/project/.pi/npm/node_modules/primary-package/index.ts", { + source: "npm:primary-package", + scope: "project", + origin: "package", + baseDir: "/tmp/project/.pi/npm/node_modules/primary-package", + }), + }, + { + path: "/tmp/project/.pi/npm/node_modules/sibling-package/index.ts", + sourceInfo: createSourceInfo("/tmp/project/.pi/npm/node_modules/sibling-package/index.ts", { + source: "npm:primary-package", + scope: "project", + origin: "package", + baseDir: "/tmp/project/.pi/npm/node_modules/primary-package", + }), + }, + ]; + + const fakeThis = createShowLoadedResourcesThis({ + quietStartup: false, + extensions, + useRealScopeGroups: true, + }); + + (InteractiveMode as any).prototype.showLoadedResources.call(fakeThis, { + force: false, + }); + + expect(normalizeRenderedOutput(fakeThis.loadedResourcesContainer)).toMatchInlineSnapshot(` +"[Extensions] + primary-package, primary-package:../sibling-package"`); + }); + + test("labels Windows npm sibling extensions relative to the declaring package", () => { + const primaryPath = "C:\\Users\\me\\.pi\\agent\\npm\\node_modules\\primary-package\\index.ts"; + const siblingPath = "C:\\Users\\me\\.pi\\agent\\npm\\node_modules\\sibling-package\\index.ts"; + const baseDir = "C:\\Users\\me\\.pi\\agent\\npm\\node_modules\\primary-package"; + const extensions: ExtensionFixture[] = [ + { + path: primaryPath, + sourceInfo: createSourceInfo(primaryPath, { + source: "npm:primary-package", + scope: "user", + origin: "package", + baseDir, + }), + }, + { + path: siblingPath, + sourceInfo: createSourceInfo(siblingPath, { + source: "npm:primary-package", + scope: "user", + origin: "package", + baseDir, + }), + }, + ]; + + const fakeThis = createShowLoadedResourcesThis({ + quietStartup: false, + extensions, + useRealScopeGroups: true, + }); + + (InteractiveMode as any).prototype.showLoadedResources.call(fakeThis, { + force: false, + }); + + expect(normalizeRenderedOutput(fakeThis.loadedResourcesContainer)).toMatchInlineSnapshot(` +"[Extensions] + primary-package, primary-package:../sibling-package"`); + }); + + test("captures mixed extension layouts in expanded output", () => { + const fakeThis = createShowLoadedResourcesThis({ + quietStartup: false, + toolOutputExpanded: true, + extensions: createExtensionFixtures(), + useRealScopeGroups: true, + }); + + (InteractiveMode as any).prototype.showLoadedResources.call(fakeThis, { + force: false, + }); + + expect(normalizeRenderedOutput(fakeThis.loadedResourcesContainer)).toMatchInlineSnapshot(` +"[Extensions] + project + /tmp/project/.pi/extensions/answer.ts + /tmp/project/.pi/extensions/local-index + git:github.com/HazAT/pi-interactive-subagents + extensions + extensions/subagents + npm:@scope/pi-scoped + extensions + npm:pi-markdown-preview + extensions + user + /tmp/agent/extensions/user-index + path + /tmp/temp/cli-extension.ts"`); + }); + + test("shows context paths relative to cwd while preserving full external paths", () => { + const home = homedir(); + const cwd = path.join(home, "Development", "pi-mono"); + const fakeThis = createShowLoadedResourcesThis({ + quietStartup: false, + cwd, + contextFiles: [{ path: path.join(home, ".pi", "agent", "AGENTS.md") }, { path: path.join(cwd, "AGENTS.md") }], + }); + + (InteractiveMode as any).prototype.showLoadedResources.call(fakeThis, { + force: false, + }); + + const output = renderAll(fakeThis.loadedResourcesContainer).replace(/\\/g, "/"); + expect(output).toContain("[Context]"); + expect(output).toContain("~/.pi/agent/AGENTS.md, AGENTS.md"); + expect(output).not.toContain(`${cwd.replace(/\\/g, "/")}/AGENTS.md`); + }); + + test("shows system prompt context paths before project context files", () => { + const cwd = "/tmp/project"; + const fakeThis = createShowLoadedResourcesThis({ + quietStartup: false, + cwd, + systemPromptSource: { path: path.join(cwd, ".pi", "SYSTEM.md") }, + appendSystemPromptSources: [{ path: path.join(cwd, ".pi", "APPEND_SYSTEM.md") }], + contextFiles: [{ path: path.join(cwd, "AGENTS.md") }], + }); + + (InteractiveMode as any).prototype.showLoadedResources.call(fakeThis, { + force: false, + }); + + const output = renderAll(fakeThis.loadedResourcesContainer).replace(/\\/g, "/"); + expect(output).toContain("[Context]"); + expect(output).toContain(".pi/SYSTEM.md, .pi/APPEND_SYSTEM.md, AGENTS.md"); + }); + + test("shows full context paths when expanded", () => { + const home = homedir(); + const cwd = path.join(home, "Development", "pi-mono"); + const fakeThis = createShowLoadedResourcesThis({ + quietStartup: false, + toolOutputExpanded: true, + cwd, + contextFiles: [{ path: path.join(home, ".pi", "agent", "AGENTS.md") }, { path: path.join(cwd, "AGENTS.md") }], + }); + + (InteractiveMode as any).prototype.showLoadedResources.call(fakeThis, { + force: false, + }); + + const output = renderAll(fakeThis.loadedResourcesContainer).replace(/\\/g, "/"); + expect(output).toContain("[Context]"); + expect(output).toContain("~/.pi/agent/AGENTS.md"); + expect(output).toContain("~/Development/pi-mono/AGENTS.md"); + expect(output).not.toContain("~/.pi/agent/AGENTS.md, AGENTS.md"); + }); + + test("does not show verbose listing on quiet startup during reload", () => { + const fakeThis = createShowLoadedResourcesThis({ + quietStartup: true, + skills: [{ filePath: "/tmp/skill/SKILL.md", name: "commit" }], + }); + + (InteractiveMode as any).prototype.showLoadedResources.call(fakeThis, { + extensions: [{ path: "/tmp/ext/index.ts" }], + force: false, + showDiagnosticsWhenQuiet: true, + }); + + expect(fakeThis.loadedResourcesContainer.children).toHaveLength(0); + }); + + test("still shows diagnostics on quiet startup when requested", () => { + const fakeThis = createShowLoadedResourcesThis({ + quietStartup: true, + skills: [{ filePath: "/tmp/skill/SKILL.md", name: "commit" }], + skillDiagnostics: [{ type: "warning", message: "duplicate skill name" }], + }); + + (InteractiveMode as any).prototype.showLoadedResources.call(fakeThis, { + force: false, + showDiagnosticsWhenQuiet: true, + }); + + const output = renderAll(fakeThis.loadedResourcesContainer); + expect(output).toContain("[Skill conflicts]"); + expect(output).not.toContain("[Skills]"); + }); +}); diff --git a/packages/coding-agent/test/interactive-mode-suspend.test.ts b/packages/coding-agent/test/interactive-mode-suspend.test.ts new file mode 100644 index 0000000000000000000000000000000000000000..996d2f1abf7929f270d2fa95500204bcfaa36d2c --- /dev/null +++ b/packages/coding-agent/test/interactive-mode-suspend.test.ts @@ -0,0 +1,149 @@ +import { afterEach, describe, expect, test, vi } from "vitest"; +import { InteractiveMode } from "../src/modes/interactive/interactive-mode.ts"; + +type FakeUi = { + start: () => void; + stop: () => void; + requestRender: (force?: boolean) => void; +}; + +type HandleCtrlZThis = { + ui: FakeUi; +}; + +type ProcessSignalHandler = () => void; + +type InteractiveModePrototypeWithHandleCtrlZ = { + handleCtrlZ(this: HandleCtrlZThis): void; +}; + +function callHandleCtrlZ(context: HandleCtrlZThis): void { + (interactiveModePrototype as InteractiveModePrototypeWithHandleCtrlZ).handleCtrlZ.call(context); +} + +const interactiveModePrototype = InteractiveMode.prototype as unknown; + +describe("InteractiveMode.handleCtrlZ", () => { + afterEach(() => { + vi.restoreAllMocks(); + }); + + test("shows a status message and skips suspend on Windows", () => { + const ui: FakeUi = { + start: vi.fn(), + stop: vi.fn(), + requestRender: vi.fn(), + }; + const showStatus = vi.fn(); + const context: HandleCtrlZThis & { showStatus: (message: string) => void } = { ui, showStatus }; + const platformDescriptor = Object.getOwnPropertyDescriptor(process, "platform"); + Object.defineProperty(process, "platform", { + configurable: true, + value: "win32", + }); + const setIntervalSpy = vi.spyOn(globalThis, "setInterval"); + const processOnSpy = vi.spyOn(process, "on"); + const processOnceSpy = vi.spyOn(process, "once"); + const processKillSpy = vi.spyOn(process, "kill"); + + try { + callHandleCtrlZ(context); + } finally { + if (platformDescriptor) { + Object.defineProperty(process, "platform", platformDescriptor); + } + } + + expect(showStatus).toHaveBeenCalledWith("Suspend to background is not supported on Windows"); + expect(ui.stop).not.toHaveBeenCalled(); + expect(setIntervalSpy).not.toHaveBeenCalled(); + expect(processOnSpy).not.toHaveBeenCalledWith("SIGINT", expect.any(Function)); + expect(processOnceSpy).not.toHaveBeenCalledWith("SIGCONT", expect.any(Function)); + expect(processKillSpy).not.toHaveBeenCalled(); + }); + + test("keeps the process alive while suspended and restores the TUI on SIGCONT", () => { + const ui: FakeUi = { + start: vi.fn(), + stop: vi.fn(), + requestRender: vi.fn(), + }; + const context: HandleCtrlZThis = { ui }; + const keepAliveHandle = setTimeout(() => undefined, 0); + clearTimeout(keepAliveHandle); + + let sigintHandler: ProcessSignalHandler | undefined; + let sigcontHandler: ProcessSignalHandler | undefined; + + const setIntervalSpy = vi.spyOn(globalThis, "setInterval").mockReturnValue(keepAliveHandle); + const clearIntervalSpy = vi.spyOn(globalThis, "clearInterval").mockImplementation(() => undefined); + const processOnSpy = vi.spyOn(process, "on").mockImplementation(((event: string, listener: () => void) => { + if (event === "SIGINT") { + sigintHandler = listener; + } + return process; + }) as typeof process.on); + const processOnceSpy = vi.spyOn(process, "once").mockImplementation(((event: string, listener: () => void) => { + if (event === "SIGCONT") { + sigcontHandler = listener; + } + return process; + }) as typeof process.once); + const removeListenerSpy = vi + .spyOn(process, "removeListener") + .mockImplementation(((_event: string, _listener: () => void) => process) as typeof process.removeListener); + const processKillSpy = vi.spyOn(process, "kill").mockImplementation(() => true); + + callHandleCtrlZ(context); + + expect(setIntervalSpy).toHaveBeenCalledWith(expect.any(Function), 2 ** 30); + expect(processOnSpy).toHaveBeenCalledWith("SIGINT", expect.any(Function)); + expect(processOnceSpy).toHaveBeenCalledWith("SIGCONT", expect.any(Function)); + expect(ui.stop).toHaveBeenCalledTimes(1); + expect(processKillSpy).toHaveBeenCalledWith(0, "SIGTSTP"); + expect(sigintHandler).toBeDefined(); + expect(sigcontHandler).toBeDefined(); + + sigcontHandler?.(); + + expect(clearIntervalSpy).toHaveBeenCalledWith(keepAliveHandle); + expect(removeListenerSpy).toHaveBeenCalledWith("SIGINT", sigintHandler); + expect(ui.start).toHaveBeenCalledTimes(1); + expect(ui.requestRender).toHaveBeenCalledWith(true); + }); + + test("cleans up the temporary handlers if suspension fails", () => { + const ui: FakeUi = { + start: vi.fn(), + stop: vi.fn(), + requestRender: vi.fn(), + }; + const context: HandleCtrlZThis = { ui }; + const keepAliveHandle = setTimeout(() => undefined, 0); + clearTimeout(keepAliveHandle); + const suspendError = new Error("suspend failed"); + + const setIntervalSpy = vi.spyOn(globalThis, "setInterval").mockReturnValue(keepAliveHandle); + const clearIntervalSpy = vi.spyOn(globalThis, "clearInterval").mockImplementation(() => undefined); + vi.spyOn(process, "on").mockImplementation( + ((_event: string, _listener: () => void) => process) as typeof process.on, + ); + const removeListenerSpy = vi + .spyOn(process, "removeListener") + .mockImplementation(((_event: string, _listener: () => void) => process) as typeof process.removeListener); + vi.spyOn(process, "once").mockImplementation( + ((_event: string, _listener: () => void) => process) as typeof process.once, + ); + vi.spyOn(process, "kill").mockImplementation(() => { + throw suspendError; + }); + + expect(() => callHandleCtrlZ(context)).toThrow(suspendError); + expect(ui.stop).toHaveBeenCalledTimes(1); + expect(setIntervalSpy).toHaveBeenCalledTimes(1); + expect(clearIntervalSpy).toHaveBeenCalledWith(keepAliveHandle); + expect(removeListenerSpy).toHaveBeenCalledWith("SIGINT", expect.any(Function)); + expect(ui.start).not.toHaveBeenCalled(); + expect(ui.requestRender).not.toHaveBeenCalled(); + }); +}); diff --git a/packages/coding-agent/test/interactive-mode-tree-navigation.test.ts b/packages/coding-agent/test/interactive-mode-tree-navigation.test.ts new file mode 100644 index 0000000000000000000000000000000000000000..9b87993dda2f419c969529b988edfa60bbb3bf36 --- /dev/null +++ b/packages/coding-agent/test/interactive-mode-tree-navigation.test.ts @@ -0,0 +1,144 @@ +import { Container } from "@earendil-works/pi-tui"; +import { beforeEach, describe, expect, it, vi } from "vitest"; +import { SessionManager } from "../src/core/session-manager.ts"; +import { SettingsManager } from "../src/core/settings-manager.ts"; +import type { StatusIndicator } from "../src/modes/interactive/components/status-indicator.ts"; +import type { TreeSelectorComponent } from "../src/modes/interactive/components/tree-selector.ts"; +import { InteractiveMode } from "../src/modes/interactive/interactive-mode.ts"; +import { initTheme } from "../src/modes/interactive/theme/theme.ts"; +import { assistantMsg, userMsg } from "./utilities.ts"; + +const busyMessage = "Wait for the current compaction or tree navigation to finish before navigating the session tree."; + +function createTreeUI() { + const sessionManager = SessionManager.inMemory(); + const targetId = sessionManager.appendMessage(userMsg("first")); + sessionManager.appendMessage(assistantMsg("reply")); + let selector: TreeSelectorComponent | undefined; + const onEscape = vi.fn(); + const ui = { + sessionManager, + settingsManager: SettingsManager.inMemory(), + session: { + isStreaming: false, + isCompacting: false, + abort: vi.fn(async () => { + ui.session.isStreaming = false; + }), + abortBranchSummary: vi.fn(), + navigateTree: vi.fn(async () => { + if (ui.session.isCompacting) throw new Error(busyMessage); + return { cancelled: false }; + }), + }, + defaultEditor: { onEscape }, + editor: { getText: () => "", setText: vi.fn() }, + chatContainer: new Container(), + isInitialized: true, + footer: { invalidate: vi.fn() }, + ui: { terminal: { rows: 24, setProgress: vi.fn() }, requestRender: vi.fn() }, + showSelector: ( + create: (done: () => void) => { component: TreeSelectorComponent; focus: TreeSelectorComponent }, + ) => { + selector = create(vi.fn()).component; + }, + showExtensionSelector: vi.fn(async () => "No summary"), + // Dispose newly created indicators so a failing regression cannot leak spinner timers. + showStatusIndicator: vi.fn((indicator: StatusIndicator) => indicator.dispose()), + clearStatusIndicator: vi.fn(), + restoreQueuedMessagesToEditor: vi.fn(), + renderInitialMessages: vi.fn(), + showStatus: vi.fn(), + showError: vi.fn(), + flushCompactionQueue: vi.fn(async () => {}), + }; + const showTreeSelector = Reflect.get(InteractiveMode.prototype, "showTreeSelector") as (this: typeof ui) => void; + showTreeSelector.call(ui); + + return { + ui, + onEscape, + targetId, + async select() { + expect(selector).toBeDefined(); + await selector!.getTreeList().onSelect!(targetId); + }, + }; +} + +describe("InteractiveMode tree navigation availability", () => { + beforeEach(() => initTheme("dark")); + + // Regression for #9178 / PR #9179: rejection must not replace the active operation's UI. + it.each(["Summarize", "No summary"])("preserves operation UI when choosing %s while busy", async (choice) => { + const { ui, onEscape, select } = createTreeUI(); + const originalLeafId = ui.sessionManager.getLeafId(); + ui.showExtensionSelector.mockImplementation(async () => { + // Compaction or another navigation can start while the dialog is open. + ui.session.isCompacting = true; + return choice; + }); + + await select(); + + expect(ui.showError).toHaveBeenCalledWith(busyMessage); + expect(ui.showStatusIndicator).not.toHaveBeenCalled(); + expect(ui.clearStatusIndicator).not.toHaveBeenCalled(); + expect(ui.defaultEditor.onEscape).toBe(onEscape); + expect(ui.session.navigateTree).not.toHaveBeenCalled(); + expect(ui.session.abort).not.toHaveBeenCalled(); + expect(ui.sessionManager.getLeafId()).toBe(originalLeafId); + }); + + it("allows navigation when compaction finishes while the dialog is open", async () => { + const { ui, targetId, select } = createTreeUI(); + ui.session.isCompacting = true; + ui.showExtensionSelector.mockImplementation(async () => { + ui.session.isCompacting = false; + return "No summary"; + }); + + await select(); + + expect(ui.session.navigateTree).toHaveBeenCalledWith(targetId, { + summarize: false, + customInstructions: undefined, + }); + expect(ui.showError).not.toHaveBeenCalled(); + }); + + it("still aborts an active response before navigating", async () => { + const { ui, select } = createTreeUI(); + ui.session.isStreaming = true; + ui.session.navigateTree.mockImplementation(async () => { + expect(ui.session.isStreaming).toBe(false); + expect(ui.restoreQueuedMessagesToEditor).toHaveBeenCalledOnce(); + return { cancelled: false }; + }); + + await select(); + + expect(ui.session.abort).toHaveBeenCalledOnce(); + expect(ui.session.navigateTree).toHaveBeenCalledOnce(); + expect(ui.showError).not.toHaveBeenCalled(); + }); + + it("rechecks availability after the response abort settles", async () => { + const { ui, onEscape, select } = createTreeUI(); + ui.session.isStreaming = true; + ui.showExtensionSelector.mockResolvedValue("Summarize"); + ui.session.abort.mockImplementation(async () => { + ui.session.isStreaming = false; + ui.session.isCompacting = true; + }); + + await select(); + + expect(ui.session.abort).toHaveBeenCalledOnce(); + expect(ui.showError).toHaveBeenCalledWith(busyMessage); + expect(ui.showStatusIndicator).not.toHaveBeenCalled(); + expect(ui.clearStatusIndicator).not.toHaveBeenCalled(); + expect(ui.defaultEditor.onEscape).toBe(onEscape); + expect(ui.session.navigateTree).not.toHaveBeenCalled(); + }); +}); diff --git a/packages/coding-agent/test/interactive-tui.test.ts b/packages/coding-agent/test/interactive-tui.test.ts new file mode 100644 index 0000000000000000000000000000000000000000..63ce7d35aef8475c8794795783049cf2c9c6f8eb --- /dev/null +++ b/packages/coding-agent/test/interactive-tui.test.ts @@ -0,0 +1,471 @@ +import type { Component, Terminal, TUI } from "@earendil-works/pi-tui"; +import { Container, getKeybindings, isViewportTUI, ScrollView, setKeybindings, Text } from "@earendil-works/pi-tui"; +import { beforeEach, describe, expect, it, vi } from "vitest"; +import { VirtualTerminal } from "../../tui/test/virtual-terminal.ts"; +import { KeybindingsManager } from "../src/core/keybindings.ts"; +import type { FullscreenExitOutput, TuiMode } from "../src/core/settings-manager.ts"; +import { + BranchSummaryStatusIndicator, + CompactionStatusIndicator, + RetryStatusIndicator, + type StatusIndicator, + type StatusIndicatorKind, + WorkingStatusIndicator, +} from "../src/modes/interactive/components/status-indicator.ts"; +import { + createInteractiveTui, + createInteractiveTuiReference, + InteractiveMode, +} from "../src/modes/interactive/interactive-mode.ts"; +import { initTheme } from "../src/modes/interactive/theme/theme.ts"; + +const clipboardMocks = vi.hoisted(() => ({ + copyToClipboard: vi.fn<(text: string) => Promise>(), + readClipboardText: vi.fn<() => Promise>(), +})); + +vi.mock("../src/utils/clipboard.ts", () => clipboardMocks); + +class RecordingTerminal extends VirtualTerminal implements Terminal { + readonly writes: string[] = []; + startCount = 0; + stopCount = 0; + + override start(onInput: (data: string) => void, onResize: () => void): void { + this.startCount += 1; + super.start(onInput, onResize); + } + + override write(data: string): void { + this.writes.push(data); + super.write(data); + } + + override stop(): void { + this.stopCount += 1; + super.stop(); + } +} + +describe("createInteractiveTui", () => { + it("selects the alternate-screen renderer only when requested", async () => { + const mainTerminal = new RecordingTerminal(); + const mainTui = createInteractiveTui({ + tuiMode: "regular", + showHardwareCursor: false, + logDirectory: "/tmp", + terminal: mainTerminal, + }); + expect(mainTui.mode).toBe("regular"); + expect(isViewportTUI(mainTui)).toBe(false); + mainTui.start(); + await mainTerminal.waitForRender(); + expect(mainTerminal.writes.some((write) => write.includes("\x1b[?1049h"))).toBe(false); + mainTui.stop(); + + const altTerminal = new RecordingTerminal(); + const altTui = createInteractiveTui({ + tuiMode: "fullscreen", + showHardwareCursor: false, + logDirectory: "/tmp", + terminal: altTerminal, + }); + expect(altTui.mode).toBe("fullscreen"); + expect(isViewportTUI(altTui)).toBe(true); + altTui.start(); + await altTerminal.waitForRender(); + expect(altTerminal.writes.some((write) => write.includes("\x1b[?1049h"))).toBe(true); + altTui.stop(); + }); + + it("shows the configured jump-to-bottom shortcut while scrolled up", async () => { + initTheme("dark"); + const previousKeybindings = getKeybindings(); + setKeybindings(new KeybindingsManager({ "tui.altScreen.bottom": "ctrl+j" })); + const terminal = new RecordingTerminal(50, 4); + const ui = createInteractiveTui({ + tuiMode: "fullscreen", + showHardwareCursor: false, + logDirectory: "/tmp", + terminal, + }); + ui.setLayoutRoot( + new ScrollView(new Text(Array.from({ length: 8 }, (_, index) => `line ${index + 1}`).join("\n"), 0, 0), { + follow: "end", + primary: true, + }), + ); + ui.start(); + try { + await terminal.waitForRender(); + terminal.sendInput("\x1b[<64;1;1M"); + await terminal.waitForRender(); + expect(terminal.getViewport()[3]).toContain("↓ Jump to latest message · Ctrl+J"); + } finally { + ui.stop(); + setKeybindings(previousKeybindings); + } + }); + + it("replaces the renderer and restores the previous screen for resume-hint exits", async () => { + const terminal = new RecordingTerminal(40, 8); + const renderer = createInteractiveTui({ + tuiMode: "regular", + showHardwareCursor: false, + logDirectory: "/tmp", + terminal, + }); + let stableUi: TUI; + const invalidatedModes: TuiMode[] = []; + const component: Component & { focused: boolean } = { + focused: false, + render: () => ["content"], + invalidate: () => invalidatedModes.push(stableUi.mode), + }; + renderer.addChild(component); + renderer.setFocus(component); + + type SwitchContext = { + runtimeHost: { session: { settingsManager: { getFullscreenCopyOnSelect: () => boolean } } }; + renderer: ReturnType; + ui: TUI; + fullscreenLayoutRoot: Component; + options: { tuiMode?: TuiMode }; + themeController: { rebindTui: () => void }; + extensionTerminalInputSubscriptions: Set; + }; + const context = Object.assign(Object.create(InteractiveMode.prototype), { + runtimeHost: { session: { settingsManager: { getFullscreenCopyOnSelect: () => true } } }, + renderer, + ui: undefined as unknown as TUI, + fullscreenLayoutRoot: component, + options: { tuiMode: "regular" as TuiMode }, + themeController: { rebindTui: () => {} }, + extensionTerminalInputSubscriptions: new Set(), + }) as SwitchContext; + stableUi = createInteractiveTuiReference(() => context.renderer); + context.ui = stableUi; + const { stopInteractiveTui, switchTuiMode } = InteractiveMode.prototype as unknown as { + stopInteractiveTui(this: SwitchContext, fullscreenExitOutput: FullscreenExitOutput): void; + switchTuiMode(this: SwitchContext, mode: TuiMode, restoreProgress?: boolean): boolean; + }; + + renderer.start(); + await terminal.waitForRender(); + expect(switchTuiMode.call(context, "fullscreen", false)).toBe(true); + await terminal.waitForRender(); + + expect(stableUi.mode).toBe("fullscreen"); + expect(context.renderer.children).toEqual([component]); + expect(context.renderer.getFocusedComponent()).toBe(component); + expect(component.focused).toBe(true); + expect(invalidatedModes).toEqual(["fullscreen"]); + expect([terminal.startCount, terminal.stopCount]).toEqual([2, 1]); + + stopInteractiveTui.call(context, "resume-hint"); + + expect(stableUi.mode).toBe("fullscreen"); + expect([terminal.startCount, terminal.stopCount]).toEqual([2, 2]); + }); +}); + +describe("InteractiveMode right-click paste", () => { + it("feeds clipboard text to the focused component as a bracketed paste", async () => { + clipboardMocks.readClipboardText.mockResolvedValue("clipboard text"); + const handleInput = vi.fn<(data: string) => void>(); + const target = { render: () => [], invalidate: () => {}, handleInput } satisfies Component; + const requestRender = vi.fn(); + const context = { + renderer: { getFocusedComponent: () => target }, + ui: { requestRender }, + }; + const prototype = InteractiveMode.prototype as unknown as { + handleRightClickPaste(this: typeof context): Promise; + }; + + await prototype.handleRightClickPaste.call(context); + + expect(handleInput).toHaveBeenCalledWith("\x1b[200~clipboard text\x1b[201~"); + expect(requestRender).toHaveBeenCalledOnce(); + }); +}); + +type CopyCommandContext = { + session: { getLastAssistantText: () => string | undefined }; + ui: ReturnType; + showStatus: (message: string) => void; + showError: (message: string) => void; +}; + +type CopyCommandOptions = { flashConfirmation?: boolean; preferSelection?: boolean }; + +type CopyCommandPrototype = { + handleCopyCommand(this: CopyCommandContext, options?: CopyCommandOptions): Promise; +}; + +const copyCommandPrototype = InteractiveMode.prototype as unknown as CopyCommandPrototype; + +describe("InteractiveMode copy confirmation", () => { + beforeEach(() => { + clipboardMocks.copyToClipboard.mockReset(); + clipboardMocks.copyToClipboard.mockResolvedValue(undefined); + }); + + it("copies an active fullscreen selection when copy-on-select is disabled", async () => { + const terminal = new RecordingTerminal(40, 4); + const ui = createInteractiveTui({ + tuiMode: "fullscreen", + showHardwareCursor: false, + logDirectory: "/tmp", + terminal, + fullscreenCopyOnSelect: false, + }); + const getLastAssistantText = vi.fn(() => "assistant response"); + const showStatus = vi.fn(); + const showError = vi.fn(); + const context: CopyCommandContext = { + session: { getLastAssistantText }, + ui, + showStatus, + showError, + }; + ui.addChild(new Text("alpha\nbeta\ngamma\ndelta", 0, 0)); + + ui.start(); + try { + await terminal.waitForRender(); + terminal.sendInput("\x1b[<0;1;1M"); + terminal.sendInput("\x1b[<32;4;2M"); + terminal.sendInput("\x1b[<0;4;2m"); + await terminal.waitForRender(); + clipboardMocks.copyToClipboard.mockClear(); + + await copyCommandPrototype.handleCopyCommand.call(context, { flashConfirmation: true, preferSelection: true }); + await terminal.waitForRender(); + + expect(clipboardMocks.copyToClipboard).toHaveBeenCalledOnce(); + expect(clipboardMocks.copyToClipboard).toHaveBeenCalledWith("alpha\nbeta"); + expect(getLastAssistantText).not.toHaveBeenCalled(); + expect(showStatus).not.toHaveBeenCalled(); + expect(showError).not.toHaveBeenCalled(); + expect(terminal.getViewport().some((line) => line.includes("Copied!"))).toBe(true); + } finally { + ui.stop(); + } + }); + + it("copies the last assistant message with an active fullscreen selection when copy-on-select is enabled", async () => { + const terminal = new RecordingTerminal(40, 4); + const ui = createInteractiveTui({ + tuiMode: "fullscreen", + showHardwareCursor: false, + logDirectory: "/tmp", + terminal, + }); + const getLastAssistantText = vi.fn(() => "assistant response"); + const showStatus = vi.fn(); + const showError = vi.fn(); + const context: CopyCommandContext = { + session: { getLastAssistantText }, + ui, + showStatus, + showError, + }; + ui.addChild(new Text("alpha\nbeta\ngamma\ndelta", 0, 0)); + + ui.start(); + try { + await terminal.waitForRender(); + terminal.sendInput("\x1b[<0;1;1M"); + terminal.sendInput("\x1b[<32;4;2M"); + terminal.sendInput("\x1b[<0;4;2m"); + await terminal.waitForRender(); + clipboardMocks.copyToClipboard.mockClear(); + + await copyCommandPrototype.handleCopyCommand.call(context, { flashConfirmation: true, preferSelection: true }); + await terminal.waitForRender(); + + expect(clipboardMocks.copyToClipboard).toHaveBeenCalledOnce(); + expect(clipboardMocks.copyToClipboard).toHaveBeenCalledWith("assistant response"); + expect(getLastAssistantText).toHaveBeenCalledOnce(); + expect(showStatus).not.toHaveBeenCalled(); + expect(showError).not.toHaveBeenCalled(); + expect(terminal.getViewport().some((line) => line.includes("Copied!"))).toBe(true); + } finally { + ui.stop(); + } + }); + + it("flashes Copied! for the copy shortcut in fullscreen mode", async () => { + const terminal = new RecordingTerminal(40, 4); + const ui = createInteractiveTui({ + tuiMode: "fullscreen", + showHardwareCursor: false, + logDirectory: "/tmp", + terminal, + }); + const showStatus = vi.fn(); + const showError = vi.fn(); + const context: CopyCommandContext = { + session: { getLastAssistantText: () => "assistant response" }, + ui, + showStatus, + showError, + }; + + ui.start(); + try { + await terminal.waitForRender(); + await copyCommandPrototype.handleCopyCommand.call(context, { flashConfirmation: true, preferSelection: true }); + await terminal.waitForRender(); + + expect(clipboardMocks.copyToClipboard).toHaveBeenCalledWith("assistant response"); + expect(showStatus).not.toHaveBeenCalled(); + expect(showError).not.toHaveBeenCalled(); + expect(terminal.getViewport().some((line) => line.includes("Copied!"))).toBe(true); + } finally { + ui.stop(); + } + }); + + it("keeps the status-line confirmation for the copy shortcut in regular mode", async () => { + const ui = createInteractiveTui({ + tuiMode: "regular", + showHardwareCursor: false, + logDirectory: "/tmp", + terminal: new RecordingTerminal(), + }); + const showStatus = vi.fn(); + const showError = vi.fn(); + const context: CopyCommandContext = { + session: { getLastAssistantText: () => "assistant response" }, + ui, + showStatus, + showError, + }; + + await copyCommandPrototype.handleCopyCommand.call(context, { flashConfirmation: true, preferSelection: true }); + + expect(showStatus).toHaveBeenCalledWith("Copied last agent message to clipboard"); + expect(showError).not.toHaveBeenCalled(); + }); +}); + +type StatusEditor = { + embedWorkingStatus: boolean; + setWorkingStatusIndicator: (indicator: StatusIndicator | undefined) => void; +}; + +type ClearStatusContext = { + activeStatusIndicator: { kind: StatusIndicatorKind; dispose: () => void } | undefined; + activeWorkingIndicatorEmbedded: boolean; + statusContainer: Container; + defaultEditor: StatusEditor; + editor: Partial; + options: { tuiMode?: TuiMode }; + ui: { getClearOnShrink: () => boolean }; + idleStatus: Component; + setEditorWorkingStatusIndicator(indicator: StatusIndicator | undefined): boolean; +}; + +type InteractiveModePrototype = { + showStatusIndicator(this: ClearStatusContext, indicator: StatusIndicator): void; + clearStatusIndicator(this: ClearStatusContext, kind?: StatusIndicatorKind): void; + setEditorWorkingStatusIndicator(this: ClearStatusContext, indicator: StatusIndicator | undefined): boolean; +}; + +const interactiveModePrototype = InteractiveMode.prototype as unknown as InteractiveModePrototype; + +describe("clear-on-shrink status spacing", () => { + it.each([true, false])("routes every status through the editor opt-in (%s)", (embedWorkingStatus) => { + initTheme("dark"); + const tui = { requestRender: vi.fn() } as unknown as TUI; + const editor: StatusEditor = { embedWorkingStatus, setWorkingStatusIndicator: vi.fn() }; + const context: ClearStatusContext = { + activeStatusIndicator: undefined, + activeWorkingIndicatorEmbedded: false, + statusContainer: new Container(), + defaultEditor: { embedWorkingStatus: true, setWorkingStatusIndicator: vi.fn() }, + editor, + options: { tuiMode: "regular" }, + ui: { getClearOnShrink: () => true }, + idleStatus: new Text("", 0, 0), + setEditorWorkingStatusIndicator: interactiveModePrototype.setEditorWorkingStatusIndicator, + }; + const indicators = [ + new WorkingStatusIndicator(tui, "Working"), + new CompactionStatusIndicator(tui, "manual"), + new CompactionStatusIndicator(tui, "threshold"), + new CompactionStatusIndicator(tui, "overflow"), + new BranchSummaryStatusIndicator(tui), + new RetryStatusIndicator(tui, 1, 3, 1000), + ]; + try { + for (const indicator of indicators) { + interactiveModePrototype.showStatusIndicator.call(context, indicator); + expect(context.activeStatusIndicator).toBe(indicator); + expect(context.activeWorkingIndicatorEmbedded).toBe(embedWorkingStatus); + if (embedWorkingStatus) { + expect(editor.setWorkingStatusIndicator).toHaveBeenLastCalledWith(indicator); + expect(context.statusContainer.children).toHaveLength(0); + } else { + expect(context.statusContainer.children).toEqual([indicator]); + } + } + } finally { + for (const indicator of indicators) indicator.dispose(); + } + }); + + it.each(["working", "compaction", "branchSummary", "retry"])( + "does not reserve separate status height for an embedded %s indicator", + (kind) => { + const dispose = vi.fn(); + const editor: StatusEditor = { embedWorkingStatus: true, setWorkingStatusIndicator: vi.fn() }; + const context: ClearStatusContext = { + activeStatusIndicator: { kind, dispose }, + activeWorkingIndicatorEmbedded: true, + statusContainer: new Container(), + defaultEditor: editor, + editor, + options: { tuiMode: "regular" }, + ui: { getClearOnShrink: () => true }, + idleStatus: new Text("", 0, 0), + setEditorWorkingStatusIndicator: interactiveModePrototype.setEditorWorkingStatusIndicator, + }; + + interactiveModePrototype.clearStatusIndicator.call(context); + + expect(dispose).toHaveBeenCalledOnce(); + expect(editor.setWorkingStatusIndicator).toHaveBeenCalledWith(undefined); + expect(context.statusContainer.children).toHaveLength(0); + }, + ); + + it("uses the standalone row for a custom editor that has not opted in", () => { + for (const [tuiMode, expectedChildren] of [ + ["regular", 1], + ["fullscreen", 0], + ] as const) { + const defaultEditor: StatusEditor = { embedWorkingStatus: true, setWorkingStatusIndicator: vi.fn() }; + const customEditor = { embedWorkingStatus: false, setWorkingStatusIndicator: vi.fn() }; + const context: ClearStatusContext = { + activeStatusIndicator: { kind: "working", dispose: vi.fn() }, + activeWorkingIndicatorEmbedded: false, + statusContainer: new Container(), + defaultEditor, + editor: customEditor, + options: { tuiMode }, + ui: { getClearOnShrink: () => true }, + idleStatus: new Text("", 0, 0), + setEditorWorkingStatusIndicator: interactiveModePrototype.setEditorWorkingStatusIndicator, + }; + + interactiveModePrototype.clearStatusIndicator.call(context); + + expect(defaultEditor.setWorkingStatusIndicator).toHaveBeenCalledWith(undefined); + expect(customEditor.setWorkingStatusIndicator).not.toHaveBeenCalled(); + expect(context.statusContainer.children).toHaveLength(expectedChildren); + } + }); +}); diff --git a/packages/coding-agent/test/keybindings-migration.test.ts b/packages/coding-agent/test/keybindings-migration.test.ts new file mode 100644 index 0000000000000000000000000000000000000000..f393687c44e4ccb3559e859ac23a2d85412f35da --- /dev/null +++ b/packages/coding-agent/test/keybindings-migration.test.ts @@ -0,0 +1,88 @@ +import * as fs from "node:fs"; +import * as os from "node:os"; +import * as path from "node:path"; +import { afterEach, describe, expect, it } from "vitest"; +import { ENV_AGENT_DIR } from "../src/config.ts"; +import { KeybindingsManager } from "../src/core/keybindings.ts"; +import { runMigrations } from "../src/migrations.ts"; + +describe("keybindings migration", () => { + const tempDirs: string[] = []; + + afterEach(() => { + for (const dir of tempDirs.splice(0)) { + fs.rmSync(dir, { recursive: true, force: true }); + } + }); + + function createAgentDir(config: Record): string { + const agentDir = fs.mkdtempSync(path.join(os.tmpdir(), "pi-keybindings-test-")); + tempDirs.push(agentDir); + fs.writeFileSync(path.join(agentDir, "keybindings.json"), `${JSON.stringify(config, null, 2)}\n`, "utf-8"); + return agentDir; + } + + it("rewrites old key names to namespaced ids", () => { + const agentDir = createAgentDir({ + cursorUp: ["up", "ctrl+p"], + expandTools: "ctrl+x", + }); + const previousAgentDir = process.env[ENV_AGENT_DIR]; + process.env[ENV_AGENT_DIR] = agentDir; + runMigrations(agentDir); + if (previousAgentDir === undefined) { + delete process.env[ENV_AGENT_DIR]; + } else { + process.env[ENV_AGENT_DIR] = previousAgentDir; + } + + const migrated = JSON.parse(fs.readFileSync(path.join(agentDir, "keybindings.json"), "utf-8")) as Record< + string, + unknown + >; + expect(migrated).toEqual({ + "tui.editor.cursorUp": ["up", "ctrl+p"], + "app.tools.expand": "ctrl+x", + }); + }); + + it("keeps the namespaced value when old and new names both exist", () => { + const agentDir = createAgentDir({ + expandTools: "ctrl+x", + "app.tools.expand": "ctrl+y", + }); + const previousAgentDir = process.env[ENV_AGENT_DIR]; + process.env[ENV_AGENT_DIR] = agentDir; + runMigrations(agentDir); + if (previousAgentDir === undefined) { + delete process.env[ENV_AGENT_DIR]; + } else { + process.env[ENV_AGENT_DIR] = previousAgentDir; + } + + const migrated = JSON.parse(fs.readFileSync(path.join(agentDir, "keybindings.json"), "utf-8")) as Record< + string, + unknown + >; + expect(migrated).toEqual({ + "app.tools.expand": "ctrl+y", + }); + }); + + it("loads old key names in memory before the file is rewritten", () => { + const agentDir = createAgentDir({ + selectConfirm: "enter", + interrupt: "ctrl+x", + }); + + const keybindings = KeybindingsManager.create(agentDir); + + expect(keybindings.getUserBindings()).toEqual({ + "tui.select.confirm": "enter", + "app.interrupt": "ctrl+x", + }); + const effective = keybindings.getEffectiveConfig(); + expect(effective["tui.select.confirm"]).toBe("enter"); + expect(effective["app.interrupt"]).toBe("ctrl+x"); + }); +}); diff --git a/packages/coding-agent/test/keybindings.test.ts b/packages/coding-agent/test/keybindings.test.ts new file mode 100644 index 0000000000000000000000000000000000000000..cb505eede7e71a2a1023beb8b772a5b0e104f639 --- /dev/null +++ b/packages/coding-agent/test/keybindings.test.ts @@ -0,0 +1,42 @@ +import { describe, expect, it } from "vitest"; +import { KEYBINDINGS, useWindowsKeybindings } from "../src/core/keybindings.ts"; + +describe("Windows keybinding defaults", () => { + it("uses Windows keybindings on native Windows", () => { + expect(useWindowsKeybindings("win32", {})).toBe(true); + }); + + it("uses Windows keybindings in WSL without relying on Windows Terminal detection", () => { + expect(useWindowsKeybindings("linux", { WSL_DISTRO_NAME: "Ubuntu" })).toBe(true); + expect(useWindowsKeybindings("linux", { WSL_INTEROP: "/run/WSL/123_interop" })).toBe(true); + }); + + it("does not use Windows keybindings from WT_SESSION alone", () => { + expect(useWindowsKeybindings("linux", { WT_SESSION: "session" })).toBe(false); + }); + + it("keeps non-Windows defaults on other platforms", () => { + expect(useWindowsKeybindings("linux", {})).toBe(false); + expect(useWindowsKeybindings("darwin", {})).toBe(false); + }); + + it("applies the detected defaults consistently", () => { + const windowsKeybindings = useWindowsKeybindings(); + const nativeWindows = process.platform === "win32"; + + expect(KEYBINDINGS["app.clipboard.pasteImage"].defaultKeys).toBe(windowsKeybindings ? "alt+v" : "ctrl+v"); + expect(KEYBINDINGS["tui.altScreen.search"].defaultKeys).toBe(windowsKeybindings ? "ctrl+f" : "ctrl+shift+f"); + expect(KEYBINDINGS["app.message.followUp"].defaultKeys).toBe(windowsKeybindings ? "ctrl+q" : "alt+enter"); + expect(KEYBINDINGS["app.model.cycleBackward"].defaultKeys).toBe(windowsKeybindings ? "alt+p" : "shift+ctrl+p"); + expect(KEYBINDINGS["tui.editor.undo"].defaultKeys).toBe( + nativeWindows ? "ctrl+z" : windowsKeybindings ? "alt+z" : "ctrl+-", + ); + expect(KEYBINDINGS["tui.altScreen.previousPrompt"].defaultKeys).toEqual( + windowsKeybindings ? "ctrl+up" : ["ctrl+shift+up", "ctrl+up"], + ); + expect(KEYBINDINGS["tui.altScreen.nextPrompt"].defaultKeys).toEqual( + windowsKeybindings ? "ctrl+down" : ["ctrl+shift+down", "ctrl+down"], + ); + expect(KEYBINDINGS["app.message.dequeue"].defaultKeys).toBe(windowsKeybindings ? "alt+q" : "alt+up"); + }); +}); diff --git a/packages/coding-agent/test/llama-extension.test.ts b/packages/coding-agent/test/llama-extension.test.ts new file mode 100644 index 0000000000000000000000000000000000000000..7c506aafc911aaf5103e7b99b86fcdb18d79c0a6 --- /dev/null +++ b/packages/coding-agent/test/llama-extension.test.ts @@ -0,0 +1,374 @@ +import { once } from "node:events"; +import { createServer, type RequestListener, type Server, type ServerResponse } from "node:http"; +import type { AddressInfo } from "node:net"; +import type { AuthContext, AuthPrompt, ModelsPublication, ModelsStoreEntry } from "@earendil-works/pi-ai"; +import { afterEach, describe, expect, it } from "vitest"; +import { createEventBus } from "../src/core/event-bus.ts"; +import { createExtensionRuntime, loadExtensionFromFactory } from "../src/core/extensions/loader.ts"; +import { LlamaClient, type LlamaProgress, normalizeLlamaServerUrl } from "../src/extensions/llama/client.ts"; +import { findHuggingFaceToken, HuggingFaceClient } from "../src/extensions/llama/huggingface.ts"; +import llamaExtension from "../src/extensions/llama/index.ts"; +import { createLlamaProvider, LLAMA_PROVIDER_ID } from "../src/extensions/llama/provider.ts"; + +const servers: Server[] = []; + +async function listen(handler: RequestListener): Promise<{ server: Server; url: string }> { + const server = createServer(handler); + servers.push(server); + server.listen(0, "127.0.0.1"); + await once(server, "listening"); + const address = server.address() as AddressInfo; + return { server, url: `http://127.0.0.1:${address.port}` }; +} + +function json(response: ServerResponse, value: unknown): void { + response.writeHead(200, { "Content-Type": "application/json" }); + response.end(JSON.stringify(value)); +} + +afterEach(async () => { + await Promise.all( + servers.splice(0).map( + (server) => + new Promise((resolve) => { + server.close(() => resolve()); + server.closeAllConnections(); + }), + ), + ); +}); + +describe("llama.cpp extension", () => { + it("registers a native provider and /llama command", async () => { + const runtime = createExtensionRuntime(); + const extension = await loadExtensionFromFactory( + llamaExtension, + process.cwd(), + createEventBus(), + runtime, + "", + ); + + expect(extension.commands.get("llama")?.description).toBe("Manage llama.cpp router models"); + expect(runtime.pendingNativeProviderRegistrations.map((entry) => entry.provider.id)).toEqual([LLAMA_PROVIDER_ID]); + }); + + it("normalizes management and inference URLs", () => { + expect(normalizeLlamaServerUrl("http://127.0.0.1:8080/v1/")).toBe("http://127.0.0.1:8080"); + expect(normalizeLlamaServerUrl("https://example.com/prefix/v1")).toBe("https://example.com/prefix"); + expect(() => normalizeLlamaServerUrl("file:///tmp/llama")).toThrow("http or https"); + }); + + it("exposes loaded and sleeping models with router metadata", () => { + const controller = createLlamaProvider(); + controller.setCatalog( + [ + { + id: "loaded", + status: { value: "loaded", args: ["llama-server", "--n-gpu-layers", "999"] }, + architecture: { input_modalities: ["text", "image"] }, + meta: { n_ctx: 65536, n_ctx_train: 131072 }, + }, + { id: "sleeping", status: { value: "sleeping" } }, + { id: "unloaded", status: { value: "unloaded" } }, + { id: "loading", status: { value: "loading" } }, + ], + "http://localhost:8080", + ); + + expect(controller.provider.getModels()).toEqual([ + expect.objectContaining({ + id: "loaded", + baseUrl: "http://localhost:8080/v1", + contextWindow: 65536, + maxTokens: 65536, + input: ["text", "image"], + }), + expect.objectContaining({ + id: "sleeping", + baseUrl: "http://localhost:8080/v1", + }), + ]); + }); + + it("persists and restores selectable models for cache-only startup refreshes", async () => { + let cachedEntry: ModelsStoreEntry | undefined; + const { url } = await listen((request, response) => { + if (request.url === "/models") { + json(response, { + data: [ + { id: "loaded", status: { value: "loaded" }, meta: { n_ctx: 32768 } }, + { id: "sleeping", status: { value: "sleeping" }, meta: { n_ctx: 32768 } }, + { id: "unloaded", status: { value: "unloaded" } }, + ], + }); + return; + } + response.writeHead(404).end(); + }); + + const publish = async (publication: ModelsPublication): Promise => { + if (publication.persist === null) cachedEntry = undefined; + else if (publication.persist !== undefined) cachedEntry = structuredClone(publication.persist); + publication.update?.(); + return true; + }; + const first = createLlamaProvider(); + await first.provider.refreshModels?.({ + credential: { type: "api_key", key: "local", env: { LLAMA_BASE_URL: url } }, + stored: cachedEntry, + publish, + allowNetwork: true, + signal: new AbortController().signal, + }); + expect(first.provider.getModels().map((model) => model.id)).toEqual(["loaded", "sleeping"]); + expect(cachedEntry?.models.map((model) => model.id)).toEqual(["loaded", "sleeping"]); + + const second = createLlamaProvider(); + await second.provider.refreshModels?.({ + credential: { type: "api_key", key: "local", env: { LLAMA_BASE_URL: url } }, + stored: cachedEntry, + publish, + allowNetwork: false, + signal: new AbortController().signal, + }); + expect(second.provider.getModels()).toEqual([ + expect.objectContaining({ id: "loaded", baseUrl: `${url}/v1`, contextWindow: 32768 }), + expect.objectContaining({ id: "sleeping", baseUrl: `${url}/v1`, contextWindow: 32768 }), + ]); + }); + + it("exposes unloaded presets only when router autoload is enabled", async () => { + let propsRequests = 0; + const { url } = await listen((request, response) => { + expect(request.headers.authorization).toBe("Bearer local"); + if (request.url === "/models") { + json(response, { + data: [ + { id: "preset", status: { value: "unloaded" }, source: "preset", meta: { n_ctx: 65536 } }, + { id: "failed-preset", status: { value: "unloaded", failed: true }, source: "preset" }, + { id: "cache", status: { value: "unloaded" }, source: "cache" }, + { id: "models-dir", status: { value: "unloaded" }, source: "models_dir" }, + ], + }); + return; + } + if (request.url === "/props") { + propsRequests++; + json(response, { role: "router", models_autoload: true }); + return; + } + response.writeHead(404).end(); + }); + + let cachedEntry: ModelsStoreEntry | undefined; + const controller = createLlamaProvider(); + await controller.provider.refreshModels?.({ + credential: { type: "api_key", key: "local", env: { LLAMA_BASE_URL: url } }, + stored: undefined, + publish: async (publication) => { + if (publication.persist !== undefined && publication.persist !== null) { + cachedEntry = structuredClone(publication.persist); + } + publication.update?.(); + return true; + }, + allowNetwork: true, + signal: new AbortController().signal, + }); + + expect(propsRequests).toBe(1); + expect(controller.provider.getModels().map((model) => model.id)).toEqual(["preset"]); + expect(cachedEntry?.models.map((model) => model.id)).toEqual(["preset"]); + }); + + it("hides unloaded presets when router autoload is disabled", async () => { + const { url } = await listen((request, response) => { + if (request.url === "/models") { + json(response, { data: [{ id: "preset", status: { value: "unloaded" }, source: "preset" }] }); + return; + } + if (request.url === "/props") { + json(response, { role: "router", models_autoload: false }); + return; + } + response.writeHead(404).end(); + }); + + const controller = createLlamaProvider(); + await controller.provider.refreshModels?.({ + credential: { type: "api_key", key: "local", env: { LLAMA_BASE_URL: url } }, + stored: undefined, + publish: async (publication) => { + publication.update?.(); + return true; + }, + allowNetwork: true, + signal: new AbortController().signal, + }); + + expect(controller.provider.getModels()).toEqual([]); + }); + + it("stays dormant until configured and stores URL plus optional key", async () => { + const { provider } = createLlamaProvider(); + const auth = provider.auth.apiKey!; + const emptyContext: AuthContext = { + env: async () => undefined, + fileExists: async () => false, + }; + const signal = new AbortController().signal; + expect(await auth.check?.({ ctx: emptyContext, signal })).toBeUndefined(); + expect(await auth.resolve({ ctx: emptyContext, signal })).toBeUndefined(); + + const { url } = await listen((request, response) => { + expect(request.headers.authorization).toBe("Bearer secret"); + json(response, { data: [] }); + }); + const answers = [url, "secret"]; + const credential = await auth.login!({ + signal, + prompt: async (_prompt: AuthPrompt) => answers.shift()!, + notify: () => {}, + }); + expect(credential).toEqual({ + type: "api_key", + key: "secret", + env: { LLAMA_BASE_URL: url }, + }); + expect(await auth.resolve({ ctx: emptyContext, credential, signal })).toEqual({ + auth: { apiKey: "secret", baseUrl: `${url}/v1` }, + env: { LLAMA_BASE_URL: url }, + source: "stored credential", + }); + }); + + it("searches Hugging Face and reads quantizations plus access requirements", async () => { + const { url } = await listen((request, response) => { + expect(request.headers.authorization).toBe("Bearer hf-secret"); + if (request.url?.startsWith("/api/models?")) { + const requestUrl = new URL(request.url, "http://localhost"); + expect(requestUrl.searchParams.get("search")).toBe("qwen coder"); + expect(requestUrl.searchParams.get("filter")).toBe("gguf"); + expect(requestUrl.searchParams.get("sort")).toBe("downloads"); + json(response, [{ id: "owner/model-GGUF", downloads: 1200 }]); + return; + } + if (request.url === "/api/models/owner/model-GGUF?blobs=true") { + json(response, { + id: "owner/model-GGUF", + gated: "manual", + siblings: [ + { rfilename: "model-Q5_K_M.gguf", size: 6000 }, + { rfilename: "model-Q4_K_M-00001-of-00002.gguf", size: 2000 }, + { rfilename: "model-Q4_K_M-00002-of-00002.gguf", size: 3000 }, + { rfilename: "mmproj-F16.gguf", size: 1000 }, + ], + }); + return; + } + response.writeHead(404).end(); + }); + const client = new HuggingFaceClient("hf-secret", url); + + expect(await client.search("qwen coder")).toEqual([{ id: "owner/model-GGUF", downloads: 1200 }]); + expect(await client.details("owner/model-GGUF")).toEqual({ + id: "owner/model-GGUF", + gated: "manual", + quantizations: [ + { name: "Q4_K_M", size: 5000 }, + { name: "Q5_K_M", size: 6000 }, + ], + }); + expect(await findHuggingFaceToken({ HF_TOKEN: " hf-secret " })).toBe("hf-secret"); + }); + + it("loads with SSE progress and waits for the loaded catalog state", async () => { + let status: "unloaded" | "loading" | "loaded" = "unloaded"; + const streams = new Set(); + const send = (event: unknown) => { + for (const response of streams) response.write(`data: ${JSON.stringify(event)}\n\n`); + }; + const { url } = await listen((request, response) => { + if (request.url === "/models/sse") { + response.writeHead(200, { "Content-Type": "text/event-stream" }); + streams.add(response); + request.on("close", () => streams.delete(response)); + return; + } + if (request.url === "/models/load" && request.method === "POST") { + status = "loading"; + json(response, { success: true }); + setTimeout(() => { + send({ + model: "test-model", + event: "status_change", + data: { + status: "loading", + progress: { stages: ["text_model", "mmproj_model"], current: "text_model", value: 0.5 }, + }, + }); + status = "loaded"; + send({ model: "test-model", event: "status_change", data: { status: "loaded" } }); + }, 20); + return; + } + if (request.url === "/models") { + json(response, { data: [{ id: "test-model", status: { value: status } }] }); + return; + } + response.writeHead(404).end(); + }); + + const progress: string[] = []; + const model = await new LlamaClient(url).loadAndWait("test-model", (entry) => progress.push(entry.message)); + expect(model.status.value).toBe("loaded"); + expect(progress).toContain("Loading text model"); + }); + + it("downloads with byte progress and returns the refreshed catalog", async () => { + let status: "missing" | "downloading" | "unloaded" = "missing"; + const streams = new Set(); + const send = (event: unknown) => { + for (const response of streams) response.write(`data: ${JSON.stringify(event)}\n\n`); + }; + const { url } = await listen((request, response) => { + if (request.url === "/models/sse") { + response.writeHead(200, { "Content-Type": "text/event-stream" }); + streams.add(response); + request.on("close", () => streams.delete(response)); + return; + } + if (request.url === "/models" && request.method === "POST") { + status = "downloading"; + json(response, { success: true }); + setTimeout(() => { + send({ + model: "owner/repo:Q4_K_M", + event: "download_progress", + data: { progress: { "https://example/model.gguf": { done: 512, total: 1024 } } }, + }); + status = "unloaded"; + send({ model: "owner/repo:Q4_K_M", event: "download_finished", data: {} }); + }, 20); + return; + } + if (request.url?.startsWith("/models")) { + json(response, { + data: status === "missing" ? [] : [{ id: "owner/repo:Q4_K_M", status: { value: status } }], + }); + return; + } + response.writeHead(404).end(); + }); + + const progress: LlamaProgress[] = []; + const models = await new LlamaClient(url).downloadAndWait("owner/repo:Q4_K_M", (entry) => progress.push(entry)); + expect(models).toEqual([{ id: "owner/repo:Q4_K_M", status: { value: "unloaded" } }]); + expect(progress).toContainEqual({ + message: "Downloading model", + ratio: 0.5, + detail: "512 B / 1.00 KiB", + }); + }); +}); diff --git a/packages/coding-agent/test/management-http.test.ts b/packages/coding-agent/test/management-http.test.ts new file mode 100644 index 0000000000000000000000000000000000000000..cb0809cd4a07e8569f3970b14730def9ef4b901d --- /dev/null +++ b/packages/coding-agent/test/management-http.test.ts @@ -0,0 +1,73 @@ +import { afterEach, describe, expect, it, vi } from "vitest"; +import { fetchWithRetry } from "../src/utils/management-http.ts"; + +afterEach(() => vi.restoreAllMocks()); + +describe("fetchWithRetry", () => { + it("retries a transient transport failure once", async () => { + const fetchMock = vi + .spyOn(globalThis, "fetch") + .mockRejectedValueOnce(new Error("fetch failed")) + .mockRejectedValueOnce(new Error("fetch failed")) + .mockResolvedValueOnce(Response.json({ ok: true })); + + const response = await fetchWithRetry("https://example.test"); + expect(response.ok).toBe(true); + expect(fetchMock).toHaveBeenCalledTimes(3); + }); + + it("shares the timeout budget across attempts", async () => { + const signals: AbortSignal[] = []; + const fetchMock = vi.spyOn(globalThis, "fetch").mockImplementation(async (_input, init) => { + if (init?.signal) signals.push(init.signal); + if (signals.length === 1) throw new Error("fetch failed"); + return Response.json({ ok: true }); + }); + + const response = await fetchWithRetry("https://example.test", undefined, { timeoutMs: 1000 }); + expect(response.ok).toBe(true); + expect(fetchMock).toHaveBeenCalledTimes(2); + expect(signals[0]).toBe(signals[1]); + }); + + it("retries an attempt timeout", async () => { + const controllers: AbortController[] = []; + vi.spyOn(AbortSignal, "timeout").mockImplementation(() => { + const controller = new AbortController(); + controllers.push(controller); + return controller.signal; + }); + const fetchMock = vi.spyOn(globalThis, "fetch").mockImplementation(async (_input, init) => { + if (fetchMock.mock.calls.length === 1) { + controllers[0].abort(); + init?.signal?.throwIfAborted(); + } + return Response.json({ ok: true }); + }); + + await fetchWithRetry("https://example.test", undefined, { attemptTimeoutMs: 4000 }); + + expect(fetchMock).toHaveBeenCalledTimes(2); + expect(controllers).toHaveLength(2); + }); + + it("retries transient HTTP responses and returns the successful response", async () => { + const fetchMock = vi + .spyOn(globalThis, "fetch") + .mockResolvedValueOnce(new Response("busy", { status: 503 })) + .mockResolvedValueOnce(Response.json({ ok: true })); + + const response = await fetchWithRetry("https://example.test"); + expect(response.ok).toBe(true); + expect(fetchMock).toHaveBeenCalledTimes(2); + }); + + it("does not retry caller cancellation", async () => { + const controller = new AbortController(); + controller.abort(); + const fetchMock = vi.spyOn(globalThis, "fetch"); + + await expect(fetchWithRetry("https://example.test", { signal: controller.signal })).rejects.toThrow(); + expect(fetchMock).not.toHaveBeenCalled(); + }); +}); diff --git a/packages/coding-agent/test/max-thinking.test.ts b/packages/coding-agent/test/max-thinking.test.ts new file mode 100644 index 0000000000000000000000000000000000000000..a0fdf9e6d4f7b69bafefd77cc22c940211a41f51 --- /dev/null +++ b/packages/coding-agent/test/max-thinking.test.ts @@ -0,0 +1,45 @@ +import { mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs"; +import { tmpdir } from "node:os"; +import { dirname, join } from "node:path"; +import { fileURLToPath } from "node:url"; +import { afterEach, describe, expect, it } from "vitest"; +import { isValidThinkingLevel } from "../src/cli/args.ts"; +import { SettingsManager } from "../src/core/settings-manager.ts"; +import { loadThemeFromPath } from "../src/modes/interactive/theme/theme.ts"; + +const tempDirs: string[] = []; + +afterEach(() => { + for (const dir of tempDirs.splice(0)) { + rmSync(dir, { recursive: true, force: true }); + } +}); + +describe("max thinking level", () => { + it("is accepted by CLI and settings", async () => { + expect(isValidThinkingLevel("max")).toBe(true); + + const settings = SettingsManager.inMemory(); + settings.setDefaultThinkingLevel("max"); + await settings.flush(); + expect(settings.getDefaultThinkingLevel()).toBe("max"); + }); + + it("falls back to thinkingXhigh for legacy themes", () => { + const testDir = mkdtempSync(join(tmpdir(), "pi-max-theme-")); + tempDirs.push(testDir); + const currentDir = dirname(fileURLToPath(import.meta.url)); + const darkTheme = JSON.parse( + readFileSync(join(currentDir, "../src/modes/interactive/theme/dark.json"), "utf8"), + ) as { name: string; colors: Record }; + darkTheme.name = "legacy-theme"; + delete darkTheme.colors.thinkingMax; + const themePath = join(testDir, "legacy-theme.json"); + writeFileSync(themePath, JSON.stringify(darkTheme)); + + const legacyTheme = loadThemeFromPath(themePath); + expect(legacyTheme.getThinkingBorderColor("max")("border")).toBe( + legacyTheme.getThinkingBorderColor("xhigh")("border"), + ); + }); +}); diff --git a/packages/coding-agent/test/mermaid.test.ts b/packages/coding-agent/test/mermaid.test.ts new file mode 100644 index 0000000000000000000000000000000000000000..332bf1f301017b6bf966ae5cfddcb34e6f42097d --- /dev/null +++ b/packages/coding-agent/test/mermaid.test.ts @@ -0,0 +1,108 @@ +import { describe, expect, it } from "vitest"; +import type { MarkdownTransformContext } from "../src/core/extensions/types.ts"; +import type { MermaidRenderingMode } from "../src/core/settings-manager.ts"; +import { createMermaidMarkdownTransformer } from "../src/modes/interactive/components/mermaid.ts"; +import type { Theme } from "../src/modes/interactive/theme/theme.ts"; + +interface TransformOptions { + maxWidth?: number; + isStreaming?: boolean; + messageType?: MarkdownTransformContext["messageType"]; + mode?: MermaidRenderingMode; + theme?: Theme; +} + +function transformMermaid(markdown: string, options: TransformOptions = {}): string { + const transformer = createMermaidMarkdownTransformer({ + getMode: () => options.mode ?? "streaming", + theme: options.theme, + }); + return transformer(markdown, { + availableWidth: options.maxWidth ?? 100, + isStreaming: options.isStreaming ?? false, + messageType: options.messageType ?? "assistant", + }); +} + +describe("Mermaid rendering", () => { + it("replaces Mermaid code blocks with Unicode diagrams", () => { + const markdown = "Before\n\n```mermaid\nflowchart LR\n A[Start] --> B[Done]\n```\nAfter"; + const rendered = transformMermaid(markdown); + + expect(rendered).toContain("Before"); + expect(rendered).toContain("┌───────┐"); + expect(rendered).toContain("│ Start ├───▶│ Done │"); + expect(rendered).toContain("└───────┘ └──────┘`\nAfter"); + expect(rendered).not.toContain("```mermaid"); + expect(rendered).toContain("After"); + }); + + it("leaves unsupported and oversized diagrams unchanged", () => { + const unsupported = '```mermaid\npie\n title Pets\n "Dogs" : 4\n```'; + const oversized = "```mermaid\nflowchart LR\n A[Start] --> B[Done]\n```"; + + expect(transformMermaid(unsupported)).toBe(unsupported); + expect(transformMermaid(oversized, { maxWidth: 10 })).toBe(oversized); + }); + + it("maps semantic spans through the Pi theme", () => { + const theme = { + fg: (color: string, text: string) => `<${color}>${text}`, + bold: (text: string) => `${text}`, + } as Theme; + const rendered = transformMermaid("```mermaid\nflowchart LR\n A --> B\n```", { theme }); + + expect(rendered).toContain(""); + expect(rendered).toContain(""); + }); + + it("renders incomplete Mermaid blocks during streaming", () => { + const partialMarkdown = "```mermaid\nflowchart LR\n A --> B"; + + expect(transformMermaid(partialMarkdown, { isStreaming: true })).toContain("───▶"); + }); + + it("renders diagrams with class assignments", () => { + const markdown = "```mermaid\nflowchart LR\n A[Foo]:::highlight --> B[Bar]\n```"; + const rendered = transformMermaid(markdown); + + expect(rendered).not.toContain("```mermaid"); + expect(rendered).not.toContain("Mermaid diagram not rendered"); + expect(rendered).toContain("│ Foo ├───▶│ Bar │"); + }); + + it("falls back to the code block with a warning after streaming", () => { + const markdown = "```mermaid\nflowchart LR\n A[Foo] invalid\n```"; + const final = transformMermaid(markdown); + const followedByText = transformMermaid(`${markdown}\nFollowing text`); + const streaming = transformMermaid(markdown, { isStreaming: true }); + + expect(final).toContain(markdown); + expect(final).toContain("```\n`Mermaid diagram not rendered"); + expect(final).toContain('dropped, expected a link: "invalid"'); + expect(final).not.toContain("more)"); + expect(followedByText).toContain(" \nFollowing text"); + expect(streaming).not.toContain("Mermaid diagram not rendered"); + expect(streaming).not.toContain("```mermaid"); + expect(streaming).toContain("│ Foo │"); + }); + + it("summarizes additional partial-render warnings", () => { + const markdown = "```mermaid\nflowchart LR\n A[Foo] invalid\n B[Bar] also-invalid\n```"; + const rendered = transformMermaid(markdown); + + expect(rendered).toContain(markdown); + expect(rendered).toContain('dropped, expected a link: "invalid"'); + expect(rendered).toContain("(+1 more)"); + expect(rendered).not.toContain('dropped, expected a link: "also-invalid"'); + }); + + it("respects rendering modes and skips thinking blocks", () => { + const markdown = "```mermaid\nflowchart LR\n A --> B\n```"; + + expect(transformMermaid(markdown, { mode: "off" })).toBe(markdown); + expect(transformMermaid(markdown, { mode: "final", isStreaming: true })).toBe(markdown); + expect(transformMermaid(markdown, { mode: "final" })).not.toContain("```mermaid"); + expect(transformMermaid(markdown, { messageType: "assistant-thinking" })).toBe(markdown); + }); +}); diff --git a/packages/coding-agent/test/model-catalog-refresh.test.ts b/packages/coding-agent/test/model-catalog-refresh.test.ts new file mode 100644 index 0000000000000000000000000000000000000000..0250026f934b468a10c25fc52ae1009942a5c023 --- /dev/null +++ b/packages/coding-agent/test/model-catalog-refresh.test.ts @@ -0,0 +1,81 @@ +import type { ModelsRefreshOptions, ModelsRefreshResult } from "@earendil-works/pi-ai"; +import { describe, expect, it, vi } from "vitest"; +import { refreshModelCatalogs } from "../src/modes/interactive/model-catalog-refresh.ts"; + +interface Deferred { + promise: Promise; + resolve(value: T): void; +} + +function createDeferred(): Deferred { + let resolvePromise!: (value: T) => void; + const promise = new Promise((resolve) => { + resolvePromise = resolve; + }); + return { promise, resolve: resolvePromise }; +} + +function successfulRefresh(): ModelsRefreshResult { + return { aborted: false, errors: new Map() }; +} + +describe("interactive model catalog refresh", () => { + it("shares one runtime refresh between concurrent callers", async () => { + const deferred = createDeferred(); + const runtime = { refresh: vi.fn((_options?: ModelsRefreshOptions) => deferred.promise) }; + const firstController = new AbortController(); + const secondController = new AbortController(); + + const first = refreshModelCatalogs(runtime, firstController.signal); + const second = refreshModelCatalogs(runtime, secondController.signal); + + expect(runtime.refresh).toHaveBeenCalledOnce(); + deferred.resolve(successfulRefresh()); + await expect(first).resolves.toEqual(successfulRefresh()); + await expect(second).resolves.toEqual(successfulRefresh()); + }); + + it("keeps the shared refresh alive when one caller stops waiting", async () => { + const deferred = createDeferred(); + let refreshSignal: AbortSignal | undefined; + const runtime = { + refresh: vi.fn((options?: ModelsRefreshOptions) => { + refreshSignal = options?.signal; + return deferred.promise; + }), + }; + const firstController = new AbortController(); + const secondController = new AbortController(); + const first = refreshModelCatalogs(runtime, firstController.signal); + const second = refreshModelCatalogs(runtime, secondController.signal); + + firstController.abort(); + await expect(first).rejects.toMatchObject({ name: "AbortError" }); + expect(refreshSignal?.aborted).toBe(false); + + deferred.resolve(successfulRefresh()); + await expect(second).resolves.toEqual(successfulRefresh()); + }); + + it("aborts an abandoned refresh and allows a later refresh to start", async () => { + const refreshSignals: AbortSignal[] = []; + const runtime = { + refresh: vi.fn((options?: ModelsRefreshOptions) => { + if (options?.signal) refreshSignals.push(options.signal); + return new Promise(() => {}); + }), + }; + const firstController = new AbortController(); + const first = refreshModelCatalogs(runtime, firstController.signal); + + firstController.abort(); + await expect(first).rejects.toMatchObject({ name: "AbortError" }); + await vi.waitFor(() => expect(refreshSignals[0]?.aborted).toBe(true)); + + const secondController = new AbortController(); + const second = refreshModelCatalogs(runtime, secondController.signal); + expect(runtime.refresh).toHaveBeenCalledTimes(2); + secondController.abort(); + await expect(second).rejects.toMatchObject({ name: "AbortError" }); + }); +}); diff --git a/packages/coding-agent/test/model-registry.test.ts b/packages/coding-agent/test/model-registry.test.ts new file mode 100644 index 0000000000000000000000000000000000000000..35dd8f954ba299cfc3eb48849646bdb7c7c4d626 --- /dev/null +++ b/packages/coding-agent/test/model-registry.test.ts @@ -0,0 +1,2012 @@ +import { existsSync, mkdirSync, readFileSync, rmSync, writeFileSync } from "node:fs"; +import { tmpdir } from "node:os"; +import { join } from "node:path"; +import type { + AnthropicMessagesCompat, + Api, + Context, + Model, + OpenAICompletionsCompat, +} from "@earendil-works/pi-ai/compat"; +import { getApiProvider, getModels, getSupportedThinkingLevels } from "@earendil-works/pi-ai/compat"; +import { afterEach, beforeEach, describe, expect, test, vi } from "vitest"; +import { AuthStorage } from "../src/core/auth-storage.ts"; +import type { ModelsJsonProvider } from "../src/core/model-config.ts"; +import { clearApiKeyCache, type ModelRegistry, type ProviderConfigInput } from "../src/core/model-registry.ts"; + +import { createModelRegistry } from "./model-runtime-test-utils.ts"; + +describe("ModelRegistry", () => { + let tempDir: string; + let modelsJsonPath: string; + let authStorage: AuthStorage; + + beforeEach(() => { + tempDir = join(tmpdir(), `pi-test-model-registry-${Date.now()}-${Math.random().toString(36).slice(2)}`); + mkdirSync(tempDir, { recursive: true }); + modelsJsonPath = join(tempDir, "models.json"); + authStorage = AuthStorage.inMemory(); + }); + + afterEach(() => { + if (tempDir && existsSync(tempDir)) { + rmSync(tempDir, { recursive: true }); + } + clearApiKeyCache(); + vi.restoreAllMocks(); + }); + + /** Create minimal provider config */ + function providerConfig( + baseUrl: string, + models: Array<{ id: string; name?: string }>, + api: string = "anthropic-messages", + ): ProviderConfigInput { + return { + baseUrl, + apiKey: "test-key", + api: api as Api, + models: models.map((m) => ({ + id: m.id, + name: m.name ?? m.id, + reasoning: false, + input: ["text"], + cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 }, + contextWindow: 100000, + maxTokens: 8000, + })), + }; + } + + function writeModelsJson(providers: Record>) { + writeFileSync(modelsJsonPath, JSON.stringify({ providers })); + } + + function getModelsForProvider(registry: ModelRegistry, provider: string) { + return registry.getAll().filter((m) => m.provider === provider); + } + + function toShPath(value: string): string { + return value.replace(/\\/g, "/").replace(/"/g, '\\"'); + } + + /** Create a baseUrl-only override (no custom models) */ + function overrideConfig(baseUrl: string, headers?: Record) { + return { baseUrl, ...(headers && { headers }) }; + } + + /** Write raw providers config (for mixed override/replacement scenarios) */ + function writeRawModelsJson(providers: Record) { + writeFileSync(modelsJsonPath, JSON.stringify({ providers })); + } + + const openAiModel: Model = { + id: "test-openai-model", + name: "Test OpenAI Model", + api: "openai-completions", + provider: "openai", + baseUrl: "https://api.openai.com/v1", + reasoning: false, + input: ["text"], + cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 }, + contextWindow: 128000, + maxTokens: 4096, + }; + + const emptyContext: Context = { + messages: [], + }; + + describe("baseUrl override (no custom models)", () => { + test("overriding baseUrl keeps all built-in models", async () => { + writeRawModelsJson({ + anthropic: overrideConfig("https://my-proxy.example.com/v1"), + }); + + const registry = await createModelRegistry(authStorage, modelsJsonPath); + const anthropicModels = getModelsForProvider(registry, "anthropic"); + + // Should have multiple built-in models, not just one + expect(anthropicModels.length).toBeGreaterThan(1); + expect(anthropicModels.some((m) => m.id.includes("claude"))).toBe(true); + }); + + test("overriding baseUrl changes URL on all built-in models", async () => { + writeRawModelsJson({ + anthropic: overrideConfig("https://my-proxy.example.com/v1"), + }); + + const registry = await createModelRegistry(authStorage, modelsJsonPath); + const anthropicModels = getModelsForProvider(registry, "anthropic"); + + // All models should have the new baseUrl + for (const model of anthropicModels) { + expect(model.baseUrl).toBe("https://my-proxy.example.com/v1"); + } + }); + + test("overriding headers resolves at request time", async () => { + writeRawModelsJson({ + anthropic: overrideConfig("https://my-proxy.example.com/v1", { + "X-Custom-Header": "custom-value", + }), + }); + + const registry = await createModelRegistry(authStorage, modelsJsonPath); + const anthropicModels = getModelsForProvider(registry, "anthropic"); + + for (const model of anthropicModels) { + const auth = await registry.getApiKeyAndHeaders(model); + expect(auth.ok).toBe(true); + if (auth.ok) { + expect(auth.headers?.["X-Custom-Header"]).toBe("custom-value"); + } + } + }); + + test("headers-only override resolves at request time", async () => { + writeRawModelsJson({ + anthropic: { + headers: { + "X-Custom-Header": "custom-value", + }, + }, + }); + + const registry = await createModelRegistry(authStorage, modelsJsonPath); + expect(registry.getError()).toBeUndefined(); + const anthropicModels = getModelsForProvider(registry, "anthropic"); + + for (const model of anthropicModels) { + const auth = await registry.getApiKeyAndHeaders(model); + expect(auth.ok).toBe(true); + if (auth.ok) { + expect(auth.headers?.["X-Custom-Header"]).toBe("custom-value"); + } + } + }); + + test("unconfigured compatibility auth includes static model headers", async () => { + const registry = await createModelRegistry(authStorage, modelsJsonPath); + const base = registry.getAll()[0]; + const model = { + ...base, + provider: "missing-provider", + headers: { "X-Static-Model": "static-value" }, + }; + + const auth = await registry.getApiKeyAndHeaders(model); + + expect(auth).toEqual({ ok: true, headers: { "X-Static-Model": "static-value" } }); + }); + + test("baseUrl-only override does not affect other providers", async () => { + writeRawModelsJson({ + anthropic: overrideConfig("https://my-proxy.example.com/v1"), + }); + + const registry = await createModelRegistry(authStorage, modelsJsonPath); + const googleModels = getModelsForProvider(registry, "google"); + + // Google models should still have their original baseUrl + expect(googleModels.length).toBeGreaterThan(0); + expect(googleModels[0].baseUrl).not.toBe("https://my-proxy.example.com/v1"); + }); + + test("can mix baseUrl override and models merge", async () => { + writeRawModelsJson({ + // baseUrl-only for anthropic + anthropic: overrideConfig("https://anthropic-proxy.example.com/v1"), + // Add custom model for google (merged with built-ins) + google: providerConfig( + "https://google-proxy.example.com/v1", + [{ id: "gemini-custom" }], + "google-generative-ai", + ), + }); + + const registry = await createModelRegistry(authStorage, modelsJsonPath); + + // Anthropic: multiple built-in models with new baseUrl + const anthropicModels = getModelsForProvider(registry, "anthropic"); + expect(anthropicModels.length).toBeGreaterThan(1); + expect(anthropicModels[0].baseUrl).toBe("https://anthropic-proxy.example.com/v1"); + + // Google: built-ins plus custom model + const googleModels = getModelsForProvider(registry, "google"); + expect(googleModels.length).toBeGreaterThan(1); + expect(googleModels.some((m) => m.id === "gemini-custom")).toBe(true); + }); + + test("refresh() picks up baseUrl override changes", async () => { + writeRawModelsJson({ + anthropic: overrideConfig("https://first-proxy.example.com/v1"), + }); + const registry = await createModelRegistry(authStorage, modelsJsonPath); + + expect(getModelsForProvider(registry, "anthropic")[0].baseUrl).toBe("https://first-proxy.example.com/v1"); + + // Update and refresh + writeRawModelsJson({ + anthropic: overrideConfig("https://second-proxy.example.com/v1"), + }); + await registry.refresh(); + + expect(getModelsForProvider(registry, "anthropic")[0].baseUrl).toBe("https://second-proxy.example.com/v1"); + }); + }); + + describe("custom models merge behavior", () => { + test("built-in provider custom models inherit api and baseUrl without explicit fields", async () => { + // Built-in providers already have api/baseUrl on every model, and auth + // comes from env vars / auth storage. No need to specify them. + writeRawModelsJson({ + openrouter: { + models: [ + { + id: "fake-provider/fake-model", + name: "Fake model", + reasoning: true, + input: ["text"], + }, + ], + }, + }); + + const registry = await createModelRegistry(authStorage, modelsJsonPath); + expect(registry.getError()).toBeUndefined(); + + const model = registry.find("openrouter", "fake-provider/fake-model"); + expect(model).toBeDefined(); + expect(model?.api).toBe("openai-completions"); + expect(model?.baseUrl).toBe("https://openrouter.ai/api/v1"); + }); + + test("non-built-in provider custom models still require baseUrl", async () => { + writeRawModelsJson({ + "my-custom-provider": { + apiKey: "test-key", + models: [ + { + id: "my-model", + api: "openai-completions", + reasoning: false, + input: ["text"], + }, + ], + }, + }); + + const registry = await createModelRegistry(authStorage, modelsJsonPath); + expect(registry.getError()).toContain("baseUrl"); + }); + + test("reports every provider composition error", async () => { + writeRawModelsJson({ + "broken-one": { api: "openai-completions", models: [{ id: "one" }] }, + "broken-two": { api: "openai-completions", models: [{ id: "two" }] }, + }); + + const registry = await createModelRegistry(authStorage, modelsJsonPath); + const error = registry.getError(); + + expect(error).toContain('Provider "broken-one"'); + expect(error).toContain('Provider "broken-two"'); + }); + + test("custom provider with same name as built-in merges with built-in models", async () => { + writeModelsJson({ + anthropic: providerConfig("https://my-proxy.example.com/v1", [{ id: "claude-custom" }]), + }); + + const registry = await createModelRegistry(authStorage, modelsJsonPath); + const anthropicModels = getModelsForProvider(registry, "anthropic"); + + expect(anthropicModels.length).toBeGreaterThan(1); + expect(anthropicModels.some((m) => m.id === "claude-custom")).toBe(true); + expect(anthropicModels.some((m) => m.id.includes("claude"))).toBe(true); + }); + + test("custom model with same id replaces built-in model by id", async () => { + writeModelsJson({ + openrouter: providerConfig( + "https://my-proxy.example.com/v1", + [{ id: "anthropic/claude-sonnet-4" }], + "openai-completions", + ), + }); + + const registry = await createModelRegistry(authStorage, modelsJsonPath); + const models = getModelsForProvider(registry, "openrouter"); + const sonnetModels = models.filter((m) => m.id === "anthropic/claude-sonnet-4"); + + expect(sonnetModels).toHaveLength(1); + expect(sonnetModels[0].baseUrl).toBe("https://my-proxy.example.com/v1"); + }); + + test("custom provider with same name as built-in does not affect other built-in providers", async () => { + writeModelsJson({ + anthropic: providerConfig("https://my-proxy.example.com/v1", [{ id: "claude-custom" }]), + }); + + const registry = await createModelRegistry(authStorage, modelsJsonPath); + + expect(getModelsForProvider(registry, "google").length).toBeGreaterThan(0); + expect(getModelsForProvider(registry, "openai").length).toBeGreaterThan(0); + }); + + test("provider-level baseUrl applies to both built-in and custom models", async () => { + writeModelsJson({ + anthropic: providerConfig("https://merged-proxy.example.com/v1", [{ id: "claude-custom" }]), + }); + + const registry = await createModelRegistry(authStorage, modelsJsonPath); + const anthropicModels = getModelsForProvider(registry, "anthropic"); + + for (const model of anthropicModels) { + expect(model.baseUrl).toBe("https://merged-proxy.example.com/v1"); + } + }); + + test("provider-level compat applies to custom models", async () => { + writeRawModelsJson({ + demo: { + baseUrl: "https://example.com/v1", + apiKey: "DEMO_KEY", + api: "openai-completions", + compat: { + supportsUsageInStreaming: false, + maxTokensField: "max_tokens", + }, + models: [ + { + id: "demo-model", + reasoning: false, + input: ["text"], + cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 }, + contextWindow: 1000, + maxTokens: 100, + }, + ], + }, + }); + + const registry = await createModelRegistry(authStorage, modelsJsonPath); + const compat = registry.find("demo", "demo-model")?.compat as OpenAICompletionsCompat | undefined; + + expect(compat?.supportsUsageInStreaming).toBe(false); + expect(compat?.maxTokensField).toBe("max_tokens"); + }); + + test("model-level compat overrides provider-level compat for custom models", async () => { + writeRawModelsJson({ + demo: { + baseUrl: "https://example.com/v1", + apiKey: "DEMO_KEY", + api: "openai-completions", + compat: { + supportsUsageInStreaming: false, + maxTokensField: "max_tokens", + }, + models: [ + { + id: "demo-model", + reasoning: false, + input: ["text"], + cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 }, + contextWindow: 1000, + maxTokens: 100, + compat: { + supportsUsageInStreaming: true, + maxTokensField: "max_completion_tokens", + }, + }, + ], + }, + }); + + const registry = await createModelRegistry(authStorage, modelsJsonPath); + const compat = registry.find("demo", "demo-model")?.compat as OpenAICompletionsCompat | undefined; + + expect(compat?.supportsUsageInStreaming).toBe(true); + expect(compat?.maxTokensField).toBe("max_completion_tokens"); + }); + + test("provider-level compat applies to built-in models", async () => { + writeRawModelsJson({ + openrouter: { + compat: { + supportsUsageInStreaming: false, + supportsStrictMode: false, + }, + }, + }); + + const registry = await createModelRegistry(authStorage, modelsJsonPath); + const models = getModelsForProvider(registry, "openrouter"); + + expect(models.length).toBeGreaterThan(0); + for (const model of models) { + const compat = model.compat as OpenAICompletionsCompat | undefined; + expect(compat?.supportsUsageInStreaming).toBe(false); + expect(compat?.supportsStrictMode).toBe(false); + } + }); + + test("model schema accepts thinkingLevelMap and compat schema accepts supportsStrictMode and cacheControlFormat", async () => { + writeRawModelsJson({ + demo: { + baseUrl: "https://example.com/v1", + apiKey: "DEMO_KEY", + api: "openai-completions", + models: [ + { + id: "demo-model", + reasoning: true, + input: ["text"], + cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 }, + contextWindow: 1000, + maxTokens: 100, + thinkingLevelMap: { + minimal: null, + high: "max", + }, + compat: { + supportsStrictMode: false, + cacheControlFormat: "anthropic", + }, + }, + ], + }, + }); + + const registry = await createModelRegistry(authStorage, modelsJsonPath); + const model = registry.find("demo", "demo-model"); + const compat = model?.compat as OpenAICompletionsCompat | undefined; + + expect(registry.getError()).toBeUndefined(); + expect(model?.thinkingLevelMap).toEqual({ minimal: null, high: "max" }); + expect(compat?.supportsStrictMode).toBe(false); + expect(compat?.cacheControlFormat).toBe("anthropic"); + }); + + test("compat schema accepts chat template thinking configuration", async () => { + writeRawModelsJson({ + demo: { + baseUrl: "https://example.com/v1", + apiKey: "DEMO_KEY", + api: "openai-completions", + models: [ + { + id: "kwargs-model", + reasoning: true, + input: ["text"], + cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 }, + contextWindow: 1000, + maxTokens: 100, + compat: { + thinkingFormat: "chat-template", + chatTemplateKwargs: { + preserve_thinking: true, + thinking: { $var: "thinking.enabled" }, + }, + }, + }, + { + id: "args-model", + reasoning: true, + input: ["text"], + cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 }, + contextWindow: 1000, + maxTokens: 100, + compat: { + thinkingFormat: "baseten", + chatTemplateArgs: { + enable_thinking: { $var: "thinking.enabled" }, + }, + }, + }, + ], + }, + }); + + const registry = await createModelRegistry(authStorage, modelsJsonPath); + const kwargsCompat = registry.find("demo", "kwargs-model")?.compat as OpenAICompletionsCompat | undefined; + const argsCompat = registry.find("demo", "args-model")?.compat as OpenAICompletionsCompat | undefined; + + expect(registry.getError()).toBeUndefined(); + expect(kwargsCompat?.thinkingFormat).toBe("chat-template"); + expect(kwargsCompat?.chatTemplateKwargs).toEqual({ + preserve_thinking: true, + thinking: { $var: "thinking.enabled" }, + }); + expect(argsCompat?.thinkingFormat).toBe("baseten"); + expect(argsCompat?.chatTemplateArgs).toEqual({ + enable_thinking: { $var: "thinking.enabled" }, + }); + }); + + test("compat schema accepts Anthropic eager tool input streaming flag", async () => { + writeRawModelsJson({ + demo: { + baseUrl: "https://example.com", + apiKey: "DEMO_KEY", + api: "anthropic-messages", + compat: { + supportsEagerToolInputStreaming: false, + }, + models: [ + { + id: "demo-model", + reasoning: true, + input: ["text"], + cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 }, + contextWindow: 1000, + maxTokens: 100, + }, + ], + }, + }); + + const registry = await createModelRegistry(authStorage, modelsJsonPath); + const compat = registry.find("demo", "demo-model")?.compat as AnthropicMessagesCompat | undefined; + + expect(registry.getError()).toBeUndefined(); + expect(compat?.supportsEagerToolInputStreaming).toBe(false); + }); + + test("compat schema accepts long cache retention flag", async () => { + writeRawModelsJson({ + demo: { + baseUrl: "https://example.com", + apiKey: "DEMO_KEY", + api: "anthropic-messages", + compat: { + supportsLongCacheRetention: false, + }, + models: [ + { + id: "demo-model", + reasoning: true, + input: ["text"], + cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 }, + contextWindow: 1000, + maxTokens: 100, + }, + ], + }, + }); + + const registry = await createModelRegistry(authStorage, modelsJsonPath); + const compat = registry.find("demo", "demo-model")?.compat as AnthropicMessagesCompat | undefined; + + expect(registry.getError()).toBeUndefined(); + expect(compat?.supportsLongCacheRetention).toBe(false); + }); + + test("model-level baseUrl overrides provider-level baseUrl for custom models", async () => { + writeRawModelsJson({ + "opencode-go": { + baseUrl: "https://opencode.ai/zen/go/v1", + apiKey: "TEST_KEY", + models: [ + { + id: "minimax-m2.5", + api: "anthropic-messages", + baseUrl: "https://opencode.ai/zen/go", + reasoning: true, + input: ["text"], + cost: { input: 0.3, output: 1.2, cacheRead: 0.03, cacheWrite: 0 }, + contextWindow: 204800, + maxTokens: 131072, + }, + { + id: "glm-5", + api: "openai-completions", + reasoning: true, + input: ["text"], + cost: { input: 1, output: 3.2, cacheRead: 0.2, cacheWrite: 0 }, + contextWindow: 204800, + maxTokens: 131072, + }, + ], + }, + }); + + const registry = await createModelRegistry(authStorage, modelsJsonPath); + const m25 = registry.find("opencode-go", "minimax-m2.5"); + const glm5 = registry.find("opencode-go", "glm-5"); + + expect(m25?.baseUrl).toBe("https://opencode.ai/zen/go"); + expect(glm5?.baseUrl).toBe("https://opencode.ai/zen/go/v1"); + }); + + test("modelOverrides still apply when provider also defines models", async () => { + writeRawModelsJson({ + openrouter: { + baseUrl: "https://my-proxy.example.com/v1", + apiKey: "OPENROUTER_API_KEY", + api: "openai-completions", + models: [ + { + id: "custom/openrouter-model", + name: "Custom OpenRouter Model", + reasoning: false, + input: ["text"], + cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 }, + contextWindow: 128000, + maxTokens: 16384, + }, + ], + modelOverrides: { + "anthropic/claude-sonnet-4": { + name: "Overridden Built-in Sonnet", + }, + }, + }, + }); + + const registry = await createModelRegistry(authStorage, modelsJsonPath); + const models = getModelsForProvider(registry, "openrouter"); + + expect(models.some((m) => m.id === "custom/openrouter-model")).toBe(true); + expect( + models.some((m) => m.id === "anthropic/claude-sonnet-4" && m.name === "Overridden Built-in Sonnet"), + ).toBe(true); + }); + + test("refresh() reloads merged custom models from disk", async () => { + writeModelsJson({ + anthropic: providerConfig("https://first-proxy.example.com/v1", [{ id: "claude-custom" }]), + }); + const registry = await createModelRegistry(authStorage, modelsJsonPath); + expect(getModelsForProvider(registry, "anthropic").some((m) => m.id === "claude-custom")).toBe(true); + + // Update and refresh + writeModelsJson({ + anthropic: providerConfig("https://second-proxy.example.com/v1", [{ id: "claude-custom-2" }]), + }); + await registry.refresh(); + + const anthropicModels = getModelsForProvider(registry, "anthropic"); + expect(anthropicModels.some((m) => m.id === "claude-custom")).toBe(false); + expect(anthropicModels.some((m) => m.id === "claude-custom-2")).toBe(true); + expect(anthropicModels.some((m) => m.id.includes("claude"))).toBe(true); + }); + + test("removing custom models from models.json keeps built-in provider models", async () => { + writeModelsJson({ + anthropic: providerConfig("https://proxy.example.com/v1", [{ id: "claude-custom" }]), + }); + const registry = await createModelRegistry(authStorage, modelsJsonPath); + expect(getModelsForProvider(registry, "anthropic").some((m) => m.id === "claude-custom")).toBe(true); + + // Remove custom models and refresh + writeModelsJson({}); + await registry.refresh(); + + const anthropicModels = getModelsForProvider(registry, "anthropic"); + expect(anthropicModels.some((m) => m.id === "claude-custom")).toBe(false); + expect(anthropicModels.some((m) => m.id.includes("claude"))).toBe(true); + }); + }); + + describe("modelOverrides (per-model customization)", () => { + test("model override applies to a single built-in model", async () => { + writeRawModelsJson({ + openrouter: { + modelOverrides: { + "anthropic/claude-sonnet-4": { + name: "Custom Sonnet Name", + }, + }, + }, + }); + + const registry = await createModelRegistry(authStorage, modelsJsonPath); + const models = getModelsForProvider(registry, "openrouter"); + + const sonnet = models.find((m) => m.id === "anthropic/claude-sonnet-4"); + expect(sonnet?.name).toBe("Custom Sonnet Name"); + + // Other models should be unchanged + const opus = models.find((m) => m.id === "anthropic/claude-opus-4"); + expect(opus?.name).not.toBe("Custom Sonnet Name"); + }); + + test("custom model and model override carry sampling params", async () => { + writeRawModelsJson({ + openrouter: { + baseUrl: "https://my-proxy.example.com/v1", + api: "openai-completions", + models: [ + { + id: "custom/sampling-model", + samplingParams: { temperature: 1, top_p: 0.95, top_k: 0 }, + }, + ], + modelOverrides: { + "anthropic/claude-sonnet-4": { + samplingParams: { top_p: 0.9 }, + }, + }, + }, + }); + + const registry = await createModelRegistry(authStorage, modelsJsonPath); + const models = getModelsForProvider(registry, "openrouter"); + + const custom = models.find((m) => m.id === "custom/sampling-model"); + expect(custom?.samplingParams).toEqual({ temperature: 1, top_p: 0.95, top_k: 0 }); + + const sonnet = models.find((m) => m.id === "anthropic/claude-sonnet-4"); + expect(sonnet?.samplingParams).toEqual({ top_p: 0.9 }); + + // Models without sampling config keep it unset. + const opus = models.find((m) => m.id === "anthropic/claude-opus-4"); + expect(opus?.samplingParams).toBeUndefined(); + }); + + test("model override with compat.openRouterRouting", async () => { + writeRawModelsJson({ + openrouter: { + modelOverrides: { + "anthropic/claude-sonnet-4": { + compat: { + openRouterRouting: { only: ["amazon-bedrock"] }, + }, + }, + }, + }, + }); + + const registry = await createModelRegistry(authStorage, modelsJsonPath); + const models = getModelsForProvider(registry, "openrouter"); + + const sonnet = models.find((m) => m.id === "anthropic/claude-sonnet-4"); + const compat = sonnet?.compat as OpenAICompletionsCompat | undefined; + expect(compat?.openRouterRouting).toEqual({ only: ["amazon-bedrock"] }); + }); + + test("supportsFinishReason can be configured at provider and model levels", async () => { + const provider: ModelsJsonProvider = { + compat: { supportsFinishReason: true }, + modelOverrides: { + "anthropic/claude-sonnet-4": { + compat: { supportsFinishReason: false }, + }, + }, + }; + writeRawModelsJson({ openrouter: provider }); + + const registry = await createModelRegistry(authStorage, modelsJsonPath); + const models = getModelsForProvider(registry, "openrouter"); + const sonnet = models.find((model) => model.id === "anthropic/claude-sonnet-4"); + const opus = models.find((model) => model.id === "anthropic/claude-opus-4"); + + expect((sonnet?.compat as OpenAICompletionsCompat | undefined)?.supportsFinishReason).toBe(false); + expect((opus?.compat as OpenAICompletionsCompat | undefined)?.supportsFinishReason).toBe(true); + }); + + test("model override deep merges compat settings", async () => { + writeRawModelsJson({ + openrouter: { + modelOverrides: { + "anthropic/claude-sonnet-4": { + compat: { + openRouterRouting: { order: ["anthropic", "together"] }, + }, + }, + }, + }, + }); + + const registry = await createModelRegistry(authStorage, modelsJsonPath); + const models = getModelsForProvider(registry, "openrouter"); + const sonnet = models.find((m) => m.id === "anthropic/claude-sonnet-4"); + + // Should have both the new routing AND preserve other compat settings + const compat = sonnet?.compat as OpenAICompletionsCompat | undefined; + expect(compat?.openRouterRouting).toEqual({ order: ["anthropic", "together"] }); + }); + + test("multiple model overrides on same provider", async () => { + writeRawModelsJson({ + openrouter: { + modelOverrides: { + "anthropic/claude-sonnet-4": { + compat: { openRouterRouting: { only: ["amazon-bedrock"] } }, + }, + "anthropic/claude-opus-4": { + compat: { openRouterRouting: { only: ["anthropic"] } }, + }, + }, + }, + }); + + const registry = await createModelRegistry(authStorage, modelsJsonPath); + const models = getModelsForProvider(registry, "openrouter"); + + const sonnet = models.find((m) => m.id === "anthropic/claude-sonnet-4"); + const opus = models.find((m) => m.id === "anthropic/claude-opus-4"); + + const sonnetCompat = sonnet?.compat as OpenAICompletionsCompat | undefined; + const opusCompat = opus?.compat as OpenAICompletionsCompat | undefined; + expect(sonnetCompat?.openRouterRouting).toEqual({ only: ["amazon-bedrock"] }); + expect(opusCompat?.openRouterRouting).toEqual({ only: ["anthropic"] }); + }); + + test("model override combined with baseUrl override", async () => { + writeRawModelsJson({ + openrouter: { + baseUrl: "https://my-proxy.example.com/v1", + modelOverrides: { + "anthropic/claude-sonnet-4": { + name: "Proxied Sonnet", + }, + }, + }, + }); + + const registry = await createModelRegistry(authStorage, modelsJsonPath); + const models = getModelsForProvider(registry, "openrouter"); + const sonnet = models.find((m) => m.id === "anthropic/claude-sonnet-4"); + + // Both overrides should apply + expect(sonnet?.baseUrl).toBe("https://my-proxy.example.com/v1"); + expect(sonnet?.name).toBe("Proxied Sonnet"); + + // Other models should have the baseUrl but not the name override + const opus = models.find((m) => m.id === "anthropic/claude-opus-4"); + expect(opus?.baseUrl).toBe("https://my-proxy.example.com/v1"); + expect(opus?.name).not.toBe("Proxied Sonnet"); + }); + + test("model override for non-existent model ID is ignored", async () => { + writeRawModelsJson({ + openrouter: { + modelOverrides: { + "nonexistent/model-id": { + name: "This should not appear", + }, + }, + }, + }); + + const registry = await createModelRegistry(authStorage, modelsJsonPath); + const models = getModelsForProvider(registry, "openrouter"); + + // Should not create a new model + expect(models.find((m) => m.id === "nonexistent/model-id")).toBeUndefined(); + // Should not crash or show error + expect(registry.getError()).toBeUndefined(); + }); + + test("model override can change cost fields partially", async () => { + writeRawModelsJson({ + openrouter: { + modelOverrides: { + "anthropic/claude-sonnet-4": { + cost: { input: 99 }, + }, + }, + }, + }); + + const registry = await createModelRegistry(authStorage, modelsJsonPath); + const models = getModelsForProvider(registry, "openrouter"); + const sonnet = models.find((m) => m.id === "anthropic/claude-sonnet-4"); + + // Input cost should be overridden + expect(sonnet?.cost.input).toBe(99); + // Other cost fields should be preserved from built-in + expect(sonnet?.cost.output).toBeGreaterThan(0); + }); + + test("model override can add headers at request time", async () => { + writeRawModelsJson({ + openrouter: { + modelOverrides: { + "anthropic/claude-sonnet-4": { + headers: { "X-Custom-Model-Header": "value" }, + }, + }, + }, + }); + + const registry = await createModelRegistry(authStorage, modelsJsonPath); + const models = getModelsForProvider(registry, "openrouter"); + const sonnet = models.find((m) => m.id === "anthropic/claude-sonnet-4"); + expect(sonnet).toBeDefined(); + + const auth = await registry.getApiKeyAndHeaders(sonnet!); + expect(auth.ok).toBe(true); + if (auth.ok) { + expect(auth.headers?.["X-Custom-Model-Header"]).toBe("value"); + } + }); + + test("refresh() picks up model override changes", async () => { + writeRawModelsJson({ + openrouter: { + modelOverrides: { + "anthropic/claude-sonnet-4": { + name: "First Name", + }, + }, + }, + }); + + const registry = await createModelRegistry(authStorage, modelsJsonPath); + expect( + getModelsForProvider(registry, "openrouter").find((m) => m.id === "anthropic/claude-sonnet-4")?.name, + ).toBe("First Name"); + + // Update and refresh + writeRawModelsJson({ + openrouter: { + modelOverrides: { + "anthropic/claude-sonnet-4": { + name: "Second Name", + }, + }, + }, + }); + await registry.refresh(); + + expect( + getModelsForProvider(registry, "openrouter").find((m) => m.id === "anthropic/claude-sonnet-4")?.name, + ).toBe("Second Name"); + }); + + test("removing model override restores built-in values", async () => { + writeRawModelsJson({ + openrouter: { + modelOverrides: { + "anthropic/claude-sonnet-4": { + name: "Custom Name", + }, + }, + }, + }); + + const registry = await createModelRegistry(authStorage, modelsJsonPath); + const customName = getModelsForProvider(registry, "openrouter").find( + (m) => m.id === "anthropic/claude-sonnet-4", + )?.name; + expect(customName).toBe("Custom Name"); + + // Remove override and refresh + writeRawModelsJson({}); + await registry.refresh(); + + const restoredName = getModelsForProvider(registry, "openrouter").find( + (m) => m.id === "anthropic/claude-sonnet-4", + )?.name; + expect(restoredName).not.toBe("Custom Name"); + }); + }); + + describe("dynamic provider lifecycle", () => { + test("getProviderDisplayName resolves registered, OAuth, built-in, and fallback names", async () => { + const registry = await createModelRegistry(authStorage, modelsJsonPath); + + expect(registry.getProviderDisplayName("openai")).toBe("OpenAI"); + expect(registry.getProviderDisplayName("github-copilot")).toBe("GitHub Copilot"); + expect(registry.getProviderDisplayName("zai")).toBe("Z.AI"); + expect(registry.getProviderDisplayName("unknown-provider")).toBe("unknown-provider"); + + registry.registerProvider("named-provider", { + name: "Named Provider", + baseUrl: "https://provider.test/v1", + apiKey: "test-key", + api: "openai-completions", + models: [ + { + id: "demo-model", + name: "Demo Model", + reasoning: false, + input: ["text"], + cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 }, + contextWindow: 128000, + maxTokens: 4096, + }, + ], + }); + expect(registry.getProviderDisplayName("named-provider")).toBe("Named Provider"); + + registry.registerProvider("oauth-provider", { + baseUrl: "https://provider.test/v1", + api: "openai-completions", + oauth: { + name: "OAuth Provider", + login: async () => ({ access: "access", refresh: "refresh", expires: Date.now() + 60_000 }), + refreshToken: async (credentials) => credentials, + getApiKey: (credentials) => credentials.access, + }, + models: [ + { + id: "demo-model", + name: "Demo Model", + reasoning: false, + input: ["text"], + cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 }, + contextWindow: 128000, + maxTokens: 4096, + }, + ], + }); + expect(registry.getProviderDisplayName("oauth-provider")).toBe("OAuth Provider"); + }); + + test("modelOverrides apply to dynamically registered provider models", async () => { + writeRawModelsJson({ + "extension-provider": { + modelOverrides: { + "extension-model": { + name: "Overridden Extension Model", + thinkingLevelMap: { + off: null, + minimal: null, + low: null, + medium: null, + xhigh: "max", + }, + headers: { "x-model-override": "enabled" }, + }, + }, + }, + }); + + const registry = await createModelRegistry(authStorage, modelsJsonPath); + registry.registerProvider("extension-provider", { + baseUrl: "https://provider.test/v1", + apiKey: "test-key", + api: "openai-completions", + models: [ + { + id: "extension-model", + name: "Extension Model", + reasoning: true, + input: ["text"], + cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 }, + contextWindow: 128000, + maxTokens: 4096, + }, + ], + }); + + const model = registry.find("extension-provider", "extension-model"); + expect(model).toBeDefined(); + if (!model) { + throw new Error("extension model was not registered"); + } + expect(model.name).toBe("Overridden Extension Model"); + expect(model.thinkingLevelMap).toEqual({ + off: null, + minimal: null, + low: null, + medium: null, + xhigh: "max", + }); + expect(getSupportedThinkingLevels(model)).toEqual(["high", "xhigh"]); + expect(await registry.getApiKeyAndHeaders(model)).toMatchObject({ + ok: true, + headers: { "x-model-override": "enabled" }, + }); + }); + + test("stored API key env propagates to request auth and resolves headers", async () => { + await authStorage.modify("cloudflare-ai-gateway", async () => ({ + type: "api_key", + key: "$CLOUDFLARE_API_KEY", + env: { + CLOUDFLARE_API_KEY: "stored-cf-token", + CLOUDFLARE_ACCOUNT_ID: "stored-account", + CLOUDFLARE_GATEWAY_ID: "stored-gateway", + }, + })); + writeRawModelsJson({ + "cloudflare-ai-gateway": { + headers: { "x-account": "$CLOUDFLARE_ACCOUNT_ID" }, + }, + }); + + const registry = await createModelRegistry(authStorage, modelsJsonPath); + const model = registry.getAll().find((m) => m.provider === "cloudflare-ai-gateway"); + expect(model).toBeDefined(); + + const auth = await registry.getApiKeyAndHeaders(model!); + + expect(auth).toEqual({ + ok: true, + apiKey: undefined, + headers: { + "cf-aig-authorization": "Bearer stored-cf-token", + Authorization: null, + "x-api-key": null, + "x-account": "stored-account", + }, + env: { + CLOUDFLARE_ACCOUNT_ID: "stored-account", + CLOUDFLARE_GATEWAY_ID: "stored-gateway", + }, + }); + }); + + test("registerProvider treats uppercase apiKey and headers as literals", async () => { + const envKeys = ["CUSTOM_NAME", "BEARER", "MODEL_TOKEN"]; + const savedEnv: Record = {}; + for (const key of envKeys) { + savedEnv[key] = process.env[key]; + process.env[key] = `env-${key}`; + } + const warnSpy = vi.spyOn(console, "warn").mockImplementation(() => {}); + + try { + const registry = await createModelRegistry(authStorage, modelsJsonPath); + + registry.registerProvider("literal-provider", { + ...providerConfig("https://provider.test/v1", [{ id: "demo-model" }], "openai-completions"), + apiKey: "CUSTOM_NAME", + headers: { Authorization: "BEARER" }, + models: [ + { + id: "demo-model", + name: "demo-model", + reasoning: false, + input: ["text"], + cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 }, + contextWindow: 100000, + maxTokens: 8000, + headers: { "x-model-token": "MODEL_TOKEN" }, + }, + ], + }); + + expect(await registry.getApiKeyForProvider("literal-provider")).toBe("CUSTOM_NAME"); + const model = registry.find("literal-provider", "demo-model"); + expect(model).toBeDefined(); + expect(await registry.getApiKeyAndHeaders(model!)).toMatchObject({ + ok: true, + apiKey: "CUSTOM_NAME", + headers: { + Authorization: "BEARER", + "x-model-token": "MODEL_TOKEN", + }, + }); + expect(warnSpy).not.toHaveBeenCalled(); + } finally { + for (const key of envKeys) { + if (savedEnv[key] === undefined) { + delete process.env[key]; + } else { + process.env[key] = savedEnv[key]; + } + } + } + }); + + test("failed registerProvider does not persist invalid streamSimple config", async () => { + const registry = await createModelRegistry(authStorage, modelsJsonPath); + + expect(() => + registry.registerProvider("broken-provider", { + streamSimple: (() => { + throw new Error("should not run"); + }) as any, + }), + ).toThrow('Provider broken-provider: "api" is required when registering streamSimple.'); + + await expect(registry.refresh()).resolves.toMatchObject({ aborted: false }); + }); + + test("failed registerProvider does not remove existing provider models", async () => { + const registry = await createModelRegistry(authStorage, modelsJsonPath); + + registry.registerProvider("demo-provider", { + baseUrl: "https://provider.test/v1", + apiKey: "test-key", + api: "openai-completions", + models: [ + { + id: "demo-model", + name: "Demo Model", + reasoning: false, + input: ["text"], + cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 }, + contextWindow: 128000, + maxTokens: 4096, + }, + ], + }); + + expect(registry.find("demo-provider", "demo-model")).toBeDefined(); + + expect(() => + registry.registerProvider("demo-provider", { + baseUrl: "https://provider.test/v2", + apiKey: "test-key", + models: [ + { + id: "broken-model", + name: "Broken Model", + reasoning: false, + input: ["text"], + cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 }, + contextWindow: 128000, + maxTokens: 4096, + }, + ], + }), + ).toThrow('Provider demo-provider, model broken-model: no "api" specified.'); + + expect(registry.find("demo-provider", "demo-model")).toBeDefined(); + await expect(registry.refresh()).resolves.toMatchObject({ aborted: false }); + expect(registry.find("demo-provider", "demo-model")).toBeDefined(); + }); + + test("unregisterProvider removes the runtime OAuth overlay without mutating global state", async () => { + const registry = await createModelRegistry(authStorage, modelsJsonPath); + + registry.registerProvider("anthropic", { + oauth: { + name: "Custom Anthropic OAuth", + login: async () => ({ + access: "custom-access-token", + refresh: "custom-refresh-token", + expires: Date.now() + 60_000, + }), + refreshToken: async (credentials) => credentials, + getApiKey: (credentials) => credentials.access, + }, + }); + + expect(registry.getRegisteredProviderConfig("anthropic")?.oauth?.name).toBe("Custom Anthropic OAuth"); + + registry.unregisterProvider("anthropic"); + + expect(registry.getRegisteredProviderConfig("anthropic")).toBeUndefined(); + }); + + test("streamSimple overlays do not mutate the global compat API registry", async () => { + const registry = await createModelRegistry(authStorage, modelsJsonPath); + + registry.registerProvider("stream-override-provider", { + api: "openai-completions", + streamSimple: () => { + throw new Error("custom streamSimple override"); + }, + }); + + let threwCustomOverride = false; + try { + getApiProvider("openai-completions")?.streamSimple(openAiModel, emptyContext); + } catch (error) { + threwCustomOverride = error instanceof Error && error.message === "custom streamSimple override"; + } + expect(threwCustomOverride).toBe(false); + + registry.unregisterProvider("stream-override-provider"); + + let threwCustomOverrideAfterUnregister = false; + try { + getApiProvider("openai-completions")?.streamSimple(openAiModel, emptyContext); + } catch (error) { + threwCustomOverrideAfterUnregister = + error instanceof Error && error.message === "custom streamSimple override"; + } + expect(threwCustomOverrideAfterUnregister).toBe(false); + }); + + describe("dynamic provider override persistence", () => { + test("baseUrl-only override keeps built-in provider models after refresh", async () => { + const registry = await createModelRegistry(authStorage, modelsJsonPath); + + registry.registerProvider("anthropic", { baseUrl: "https://proxy.test/anthropic" }); + await registry.refresh(); + + const anthropicModels = getModelsForProvider(registry, "anthropic"); + expect(anthropicModels.length).toBeGreaterThan(1); + expect(anthropicModels.every((m) => m.baseUrl === "https://proxy.test/anthropic")).toBe(true); + }); + + test("models-only override replaces built-in provider models after refresh", async () => { + const registry = await createModelRegistry(authStorage, modelsJsonPath); + + registry.registerProvider("anthropic", { + ...providerConfig("https://custom.test/anthropic", [{ id: "custom-claude" }], "anthropic-messages"), + baseUrl: "https://custom.test/anthropic", + }); + await registry.refresh(); + + expect(getModelsForProvider(registry, "anthropic").map((m) => m.id)).toEqual(["custom-claude"]); + expect(registry.find("anthropic", "custom-claude")?.baseUrl).toBe("https://custom.test/anthropic"); + }); + + test("models plus baseUrl override replaces built-in provider models after refresh", async () => { + const registry = await createModelRegistry(authStorage, modelsJsonPath); + + registry.registerProvider("anthropic", { + ...providerConfig("https://custom.test/anthropic", [{ id: "custom-claude" }], "anthropic-messages"), + baseUrl: "https://custom.test/anthropic", + }); + registry.registerProvider("anthropic", { baseUrl: "https://proxy.test/anthropic" }); + await registry.refresh(); + + expect(getModelsForProvider(registry, "anthropic").map((m) => m.id)).toEqual(["custom-claude"]); + expect(registry.find("anthropic", "custom-claude")?.baseUrl).toBe("https://proxy.test/anthropic"); + }); + + test("models-only custom provider registration survives refresh", async () => { + const registry = await createModelRegistry(authStorage, modelsJsonPath); + + registry.registerProvider( + "custom-provider", + providerConfig("https://custom.test/v1", [{ id: "custom-a" }, { id: "custom-b" }], "openai-completions"), + ); + await registry.refresh(); + + expect(getModelsForProvider(registry, "custom-provider").map((m) => m.id)).toEqual([ + "custom-a", + "custom-b", + ]); + }); + + test("baseUrl-only override keeps custom provider models after refresh", async () => { + const registry = await createModelRegistry(authStorage, modelsJsonPath); + + registry.registerProvider( + "custom-provider", + providerConfig("https://custom.test/v1", [{ id: "custom-a" }, { id: "custom-b" }], "openai-completions"), + ); + registry.registerProvider("custom-provider", { baseUrl: "https://proxy.test/custom" }); + await registry.refresh(); + + expect(getModelsForProvider(registry, "custom-provider").map((m) => m.id)).toEqual([ + "custom-a", + "custom-b", + ]); + expect( + getModelsForProvider(registry, "custom-provider").every( + (m) => m.baseUrl === "https://proxy.test/custom", + ), + ).toBe(true); + }); + + test("headers-only override keeps custom provider models after refresh", async () => { + const registry = await createModelRegistry(authStorage, modelsJsonPath); + + registry.registerProvider( + "custom-provider", + providerConfig("https://custom.test/v1", [{ id: "custom-a" }, { id: "custom-b" }], "openai-completions"), + ); + registry.registerProvider("custom-provider", { headers: { "x-proxy": "enabled" } }); + await registry.refresh(); + + const models = getModelsForProvider(registry, "custom-provider"); + expect(models.map((m) => m.id)).toEqual(["custom-a", "custom-b"]); + expect(models.every((m) => m.baseUrl === "https://custom.test/v1")).toBe(true); + expect(await registry.getApiKeyAndHeaders(models[0])).toMatchObject({ + ok: true, + headers: { "x-proxy": "enabled" }, + }); + }); + }); + }); + + describe("API key resolution", () => { + /** Create provider config with custom apiKey */ + function providerWithApiKey(apiKey: string) { + return { + baseUrl: "https://example.com/v1", + apiKey, + api: "anthropic-messages", + models: [ + { + id: "test-model", + name: "Test Model", + reasoning: false, + input: ["text"], + cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 }, + contextWindow: 100000, + maxTokens: 8000, + }, + ], + }; + } + + test("apiKey with ! prefix executes command and uses stdout", async () => { + writeRawModelsJson({ + "custom-provider": providerWithApiKey("!echo test-api-key-from-command"), + }); + + const registry = await createModelRegistry(authStorage, modelsJsonPath); + const apiKey = await registry.getApiKeyForProvider("custom-provider"); + + expect(apiKey).toBe("test-api-key-from-command"); + }); + + test("apiKey with ! prefix trims whitespace from command output", async () => { + writeRawModelsJson({ + "custom-provider": providerWithApiKey("!echo ' spaced-key '"), + }); + + const registry = await createModelRegistry(authStorage, modelsJsonPath); + const apiKey = await registry.getApiKeyForProvider("custom-provider"); + + expect(apiKey).toBe("spaced-key"); + }); + + test("apiKey with ! prefix handles multiline output (uses trimmed result)", async () => { + writeRawModelsJson({ + "custom-provider": providerWithApiKey("!printf 'line1\\nline2'"), + }); + + const registry = await createModelRegistry(authStorage, modelsJsonPath); + const apiKey = await registry.getApiKeyForProvider("custom-provider"); + + expect(apiKey).toBe("line1\nline2"); + }); + + test("apiKey with ! prefix returns undefined on command failure", async () => { + writeRawModelsJson({ + "custom-provider": providerWithApiKey("!exit 1"), + }); + + const registry = await createModelRegistry(authStorage, modelsJsonPath); + const apiKey = await registry.getApiKeyForProvider("custom-provider"); + + expect(apiKey).toBeUndefined(); + }); + + test("apiKey with ! prefix returns undefined on nonexistent command", async () => { + writeRawModelsJson({ + "custom-provider": providerWithApiKey("!nonexistent-command-12345"), + }); + + const registry = await createModelRegistry(authStorage, modelsJsonPath); + const apiKey = await registry.getApiKeyForProvider("custom-provider"); + + expect(apiKey).toBeUndefined(); + }); + + test("apiKey with ! prefix returns undefined on empty output", async () => { + writeRawModelsJson({ + "custom-provider": providerWithApiKey("!printf ''"), + }); + + const registry = await createModelRegistry(authStorage, modelsJsonPath); + const apiKey = await registry.getApiKeyForProvider("custom-provider"); + + expect(apiKey).toBeUndefined(); + }); + + test("apiKey with $ prefix resolves to env value", async () => { + const originalEnv = process.env.TEST_API_KEY_12345; + process.env.TEST_API_KEY_12345 = "env-api-key-value"; + + try { + writeRawModelsJson({ + "custom-provider": providerWithApiKey("$TEST_API_KEY_12345"), + }); + + const registry = await createModelRegistry(authStorage, modelsJsonPath); + const apiKey = await registry.getApiKeyForProvider("custom-provider"); + + expect(apiKey).toBe("env-api-key-value"); + } finally { + if (originalEnv === undefined) { + delete process.env.TEST_API_KEY_12345; + } else { + process.env.TEST_API_KEY_12345 = originalEnv; + } + } + }); + + test("apiKey with braced env syntax resolves to env value", async () => { + const originalEnv = process.env.TEST_BRACED_API_KEY_12345; + process.env.TEST_BRACED_API_KEY_12345 = "braced-env-api-key-value"; + const bracedKey = "$" + "{TEST_BRACED_API_KEY_12345}"; + + try { + writeRawModelsJson({ + "custom-provider": providerWithApiKey(bracedKey), + }); + + const registry = await createModelRegistry(authStorage, modelsJsonPath); + const apiKey = await registry.getApiKeyForProvider("custom-provider"); + + expect(apiKey).toBe("braced-env-api-key-value"); + } finally { + if (originalEnv === undefined) { + delete process.env.TEST_BRACED_API_KEY_12345; + } else { + process.env.TEST_BRACED_API_KEY_12345 = originalEnv; + } + } + }); + + test("apiKey interpolates braced env references inside literals", async () => { + const originalPartA = process.env.TEST_INTERPOLATED_PART_A_12345; + const originalPartB = process.env.TEST_INTERPOLATED_PART_B_12345; + process.env.TEST_INTERPOLATED_PART_A_12345 = "left"; + process.env.TEST_INTERPOLATED_PART_B_12345 = "right"; + const interpolatedKey = ["$", "{TEST_INTERPOLATED_PART_A_12345}_$", "{TEST_INTERPOLATED_PART_B_12345}"].join( + "", + ); + + try { + writeRawModelsJson({ + "custom-provider": providerWithApiKey(interpolatedKey), + }); + + const registry = await createModelRegistry(authStorage, modelsJsonPath); + const apiKey = await registry.getApiKeyForProvider("custom-provider"); + + expect(apiKey).toBe("left_right"); + } finally { + if (originalPartA === undefined) { + delete process.env.TEST_INTERPOLATED_PART_A_12345; + } else { + process.env.TEST_INTERPOLATED_PART_A_12345 = originalPartA; + } + if (originalPartB === undefined) { + delete process.env.TEST_INTERPOLATED_PART_B_12345; + } else { + process.env.TEST_INTERPOLATED_PART_B_12345 = originalPartB; + } + } + }); + + test("apiKey with $$ prefix escapes a leading dollar", async () => { + writeRawModelsJson({ + "custom-provider": providerWithApiKey("$$TEST_API_KEY_12345"), + }); + + const registry = await createModelRegistry(authStorage, modelsJsonPath); + const apiKey = await registry.getApiKeyForProvider("custom-provider"); + + expect(apiKey).toBe("$TEST_API_KEY_12345"); + }); + + test("apiKey with $! escapes a literal bang and still interpolates later env refs", async () => { + const originalEnv = process.env.TEST_API_KEY_12345; + process.env.TEST_API_KEY_12345 = "env-api-key-value"; + + try { + writeRawModelsJson({ + "custom-provider": providerWithApiKey("$!literal-$TEST_API_KEY_12345"), + }); + + const registry = await createModelRegistry(authStorage, modelsJsonPath); + const apiKey = await registry.getApiKeyForProvider("custom-provider"); + + expect(apiKey).toBe("!literal-env-api-key-value"); + } finally { + if (originalEnv === undefined) { + delete process.env.TEST_API_KEY_12345; + } else { + process.env.TEST_API_KEY_12345 = originalEnv; + } + } + }); + + test("plain apiKey is used directly even when it matches an env var", async () => { + const originalEnv = process.env.TEST_API_KEY_12345; + process.env.TEST_API_KEY_12345 = "env-api-key-value"; + + try { + writeRawModelsJson({ + "custom-provider": providerWithApiKey("TEST_API_KEY_12345"), + }); + + const registry = await createModelRegistry(authStorage, modelsJsonPath); + const apiKey = await registry.getApiKeyForProvider("custom-provider"); + + expect(apiKey).toBe("TEST_API_KEY_12345"); + } finally { + if (originalEnv === undefined) { + delete process.env.TEST_API_KEY_12345; + } else { + process.env.TEST_API_KEY_12345 = originalEnv; + } + } + }); + + test("apiKey as literal value is used directly when not an env var", async () => { + // Make sure this isn't an env var + delete process.env.literal_api_key_value; + + writeRawModelsJson({ + "custom-provider": providerWithApiKey("literal_api_key_value"), + }); + + const registry = await createModelRegistry(authStorage, modelsJsonPath); + const apiKey = await registry.getApiKeyForProvider("custom-provider"); + + expect(apiKey).toBe("literal_api_key_value"); + }); + + test("apiKey command can use shell features like pipes", async () => { + writeRawModelsJson({ + "custom-provider": providerWithApiKey("!echo 'hello world' | tr ' ' '-'"), + }); + + const registry = await createModelRegistry(authStorage, modelsJsonPath); + const apiKey = await registry.getApiKeyForProvider("custom-provider"); + + expect(apiKey).toBe("hello-world"); + }); + + describe("request-time resolution", () => { + test("command is executed on every provider lookup", async () => { + const counterFile = join(tempDir, "counter"); + writeFileSync(counterFile, "0"); + + const counterPath = toShPath(counterFile); + const command = `!sh -c 'count=$(cat "${counterPath}"); echo $((count + 1)) > "${counterPath}"; echo "key-value"'`; + writeRawModelsJson({ + "custom-provider": providerWithApiKey(command), + }); + + const registry = await createModelRegistry(authStorage, modelsJsonPath); + await registry.getApiKeyForProvider("custom-provider"); + await registry.getApiKeyForProvider("custom-provider"); + await registry.getApiKeyForProvider("custom-provider"); + + const count = parseInt(readFileSync(counterFile, "utf-8").trim(), 10); + expect(count).toBe(3); + }); + + test("commands are re-executed across registry instances", async () => { + const counterFile = join(tempDir, "counter"); + writeFileSync(counterFile, "0"); + + const counterPath = toShPath(counterFile); + const command = `!sh -c 'count=$(cat "${counterPath}"); echo $((count + 1)) > "${counterPath}"; echo "key-value"'`; + writeRawModelsJson({ + "custom-provider": providerWithApiKey(command), + }); + + const registry1 = await createModelRegistry(authStorage, modelsJsonPath); + await registry1.getApiKeyForProvider("custom-provider"); + + const registry2 = await createModelRegistry(authStorage, modelsJsonPath); + await registry2.getApiKeyForProvider("custom-provider"); + + const count = parseInt(readFileSync(counterFile, "utf-8").trim(), 10); + expect(count).toBe(2); + }); + + test("different commands resolve independently", async () => { + writeRawModelsJson({ + "provider-a": providerWithApiKey("!echo key-a"), + "provider-b": providerWithApiKey("!echo key-b"), + }); + + const registry = await createModelRegistry(authStorage, modelsJsonPath); + + const keyA = await registry.getApiKeyForProvider("provider-a"); + const keyB = await registry.getApiKeyForProvider("provider-b"); + + expect(keyA).toBe("key-a"); + expect(keyB).toBe("key-b"); + }); + + test("failed commands are retried", async () => { + const counterFile = join(tempDir, "counter"); + writeFileSync(counterFile, "0"); + + const counterPath = toShPath(counterFile); + const command = `!sh -c 'count=$(cat "${counterPath}"); echo $((count + 1)) > "${counterPath}"; exit 1'`; + writeRawModelsJson({ + "custom-provider": providerWithApiKey(command), + }); + + const registry = await createModelRegistry(authStorage, modelsJsonPath); + const key1 = await registry.getApiKeyForProvider("custom-provider"); + const key2 = await registry.getApiKeyForProvider("custom-provider"); + + expect(key1).toBeUndefined(); + expect(key2).toBeUndefined(); + + const count = parseInt(readFileSync(counterFile, "utf-8").trim(), 10); + expect(count).toBe(2); + }); + + test("provider auth status reports apiKey environment variables from models.json", async () => { + const envVarName = "TEST_API_KEY_STATUS_TEST_98765"; + const originalEnv = process.env[envVarName]; + + try { + process.env[envVarName] = "status-test-key"; + + writeRawModelsJson({ + "custom-provider": providerWithApiKey(`$${envVarName}`), + }); + + const registry = await createModelRegistry(authStorage, modelsJsonPath); + + expect(registry.getProviderAuthStatus("custom-provider")).toEqual({ + configured: true, + source: "environment", + label: envVarName, + }); + } finally { + if (originalEnv === undefined) { + delete process.env[envVarName]; + } else { + process.env[envVarName] = originalEnv; + } + } + }); + + test("provider auth status reports interpolated apiKey environment variables", async () => { + const envVarNameA = "TEST_API_KEY_STATUS_PART_A_98765"; + const envVarNameB = "TEST_API_KEY_STATUS_PART_B_98765"; + const originalEnvA = process.env[envVarNameA]; + const originalEnvB = process.env[envVarNameB]; + process.env[envVarNameA] = "left"; + process.env[envVarNameB] = "right"; + const interpolatedKey = ["$", "{", envVarNameA, "}_$", "{", envVarNameB, "}"].join(""); + + try { + writeRawModelsJson({ + "custom-provider": providerWithApiKey(interpolatedKey), + }); + + const registry = await createModelRegistry(authStorage, modelsJsonPath); + + expect(registry.getProviderAuthStatus("custom-provider")).toEqual({ + configured: true, + source: "environment", + label: `${envVarNameA}, ${envVarNameB}`, + }); + } finally { + if (originalEnvA === undefined) { + delete process.env[envVarNameA]; + } else { + process.env[envVarNameA] = originalEnvA; + } + if (originalEnvB === undefined) { + delete process.env[envVarNameB]; + } else { + process.env[envVarNameB] = originalEnvB; + } + } + }); + + test("provider auth status reports non-env apiKey values from models.json as a config key", async () => { + writeRawModelsJson({ + "custom-provider": providerWithApiKey("literal_api_key_value"), + }); + + const registry = await createModelRegistry(authStorage, modelsJsonPath); + + expect(registry.getProviderAuthStatus("custom-provider")).toEqual({ + configured: true, + source: "models_json_key", + }); + }); + + test("missing explicit env apiKey keeps provider unavailable", async () => { + const envVarName = "TEST_API_KEY_MISSING_TEST_98765"; + const originalEnv = process.env[envVarName]; + delete process.env[envVarName]; + + try { + writeRawModelsJson({ + "custom-provider": providerWithApiKey(`$${envVarName}`), + }); + + const registry = await createModelRegistry(authStorage, modelsJsonPath); + + expect(registry.getProviderAuthStatus("custom-provider")).toEqual({ configured: false }); + expect(registry.getAvailable().some((model) => model.provider === "custom-provider")).toBe(false); + } finally { + if (originalEnv === undefined) { + delete process.env[envVarName]; + } else { + process.env[envVarName] = originalEnv; + } + } + }); + + test("provider auth status reports command apiKey values from models.json without executing them", async () => { + const counterFile = join(tempDir, "status-counter"); + writeFileSync(counterFile, "0"); + const counterPath = toShPath(counterFile); + const command = `!sh -c 'echo 1 > "${counterPath}"; echo key-value'`; + writeRawModelsJson({ + "custom-provider": providerWithApiKey(command), + }); + + const registry = await createModelRegistry(authStorage, modelsJsonPath); + + expect(registry.getProviderAuthStatus("custom-provider")).toEqual({ + configured: true, + source: "models_json_command", + }); + expect(readFileSync(counterFile, "utf-8")).toBe("0"); + }); + + test("environment variables are not cached (changes are picked up)", async () => { + const envVarName = "TEST_API_KEY_CACHE_TEST_98765"; + const originalEnv = process.env[envVarName]; + + try { + process.env[envVarName] = "first-value"; + + writeRawModelsJson({ + "custom-provider": providerWithApiKey(`$${envVarName}`), + }); + + const registry = await createModelRegistry(authStorage, modelsJsonPath); + + const key1 = await registry.getApiKeyForProvider("custom-provider"); + expect(key1).toBe("first-value"); + + process.env[envVarName] = "second-value"; + + const key2 = await registry.getApiKeyForProvider("custom-provider"); + expect(key2).toBe("second-value"); + } finally { + if (originalEnv === undefined) { + delete process.env[envVarName]; + } else { + process.env[envVarName] = originalEnv; + } + } + }); + + test("getAvailable does not execute command-backed apiKey resolution", async () => { + const counterFile = join(tempDir, "counter"); + writeFileSync(counterFile, "0"); + + const counterPath = toShPath(counterFile); + const command = `!sh -c 'count=$(cat "${counterPath}"); echo $((count + 1)) > "${counterPath}"; echo "key-value"'`; + writeRawModelsJson({ + "custom-provider": providerWithApiKey(command), + }); + + const registry = await createModelRegistry(authStorage, modelsJsonPath); + const available = registry.getAvailable(); + + expect(available.some((m) => m.provider === "custom-provider")).toBe(true); + const count = parseInt(readFileSync(counterFile, "utf-8").trim(), 10); + expect(count).toBe(0); + }); + + test("getAvailable filters GitHub Copilot OAuth models to account picker availability", async () => { + const copilotModel = getModels("github-copilot")[0]; + if (!copilotModel) throw new Error("Expected at least one GitHub Copilot model"); + + await authStorage.modify("github-copilot", async () => ({ + type: "oauth", + refresh: "github-access-token", + access: "tid=test;exp=9999999999;proxy-ep=proxy.individual.githubcopilot.com;", + expires: Date.now() + 60_000, + availableModelIds: [copilotModel.id], + })); + + const registry = await createModelRegistry(authStorage, modelsJsonPath); + + expect( + registry + .getAvailable() + .filter((m) => m.provider === "github-copilot") + .map((m) => m.id), + ).toEqual([copilotModel.id]); + }); + + test("getApiKeyAndHeaders resolves authHeader on every request", async () => { + const tokenFile = join(tempDir, "token"); + writeFileSync(tokenFile, "token-1"); + const tokenPath = toShPath(tokenFile); + + writeRawModelsJson({ + "custom-provider": { + ...providerWithApiKey(`!sh -c 'cat "${tokenPath}"'`), + authHeader: true, + }, + }); + + const registry = await createModelRegistry(authStorage, modelsJsonPath); + const model = registry.find("custom-provider", "test-model"); + expect(model).toBeDefined(); + + const auth1 = await registry.getApiKeyAndHeaders(model!); + expect(auth1).toEqual({ + ok: true, + apiKey: "token-1", + headers: { Authorization: "Bearer token-1" }, + }); + + writeFileSync(tokenFile, "token-2"); + + const auth2 = await registry.getApiKeyAndHeaders(model!); + expect(auth2).toEqual({ + ok: true, + apiKey: "token-2", + headers: { Authorization: "Bearer token-2" }, + }); + }); + + test("getApiKeyAndHeaders resolves configured auth exactly once", async () => { + const counterFile = join(tempDir, "auth-counter"); + writeFileSync(counterFile, "0"); + const counterPath = toShPath(counterFile); + writeRawModelsJson({ + "custom-provider": { + ...providerWithApiKey( + `!sh -c 'count=$(cat "${counterPath}"); count=$((count + 1)); echo "$count" > "${counterPath}"; echo "token-$count"'`, + ), + authHeader: true, + }, + }); + + const registry = await createModelRegistry(authStorage, modelsJsonPath); + const auth = await registry.getApiKeyAndHeaders(registry.find("custom-provider", "test-model")!); + + expect(auth).toEqual({ + ok: true, + apiKey: "token-1", + headers: { Authorization: "Bearer token-1" }, + }); + expect(readFileSync(counterFile, "utf-8").trim()).toBe("1"); + }); + + test("stored credentials bypass lower-priority configured auth commands", async () => { + const counterFile = join(tempDir, "fallback-counter"); + writeFileSync(counterFile, "0"); + const counterPath = toShPath(counterFile); + writeRawModelsJson({ + "custom-provider": providerWithApiKey(`!sh -c 'echo 1 > "${counterPath}"; echo fallback-key'`), + }); + await authStorage.modify("custom-provider", async () => ({ type: "api_key", key: "stored-key" })); + + const registry = await createModelRegistry(authStorage, modelsJsonPath); + const auth = await registry.getApiKeyAndHeaders(registry.find("custom-provider", "test-model")!); + + expect(auth).toMatchObject({ ok: true, apiKey: "stored-key" }); + expect(readFileSync(counterFile, "utf-8").trim()).toBe("0"); + }); + + test("getApiKeyAndHeaders preserves the legacy missing-key authHeader error", async () => { + writeRawModelsJson({ + "custom-provider": { + baseUrl: "https://example.test/v1", + api: "openai-completions", + authHeader: true, + models: [{ id: "test-model" }], + }, + }); + + const registry = await createModelRegistry(authStorage, modelsJsonPath); + const auth = await registry.getApiKeyAndHeaders(registry.find("custom-provider", "test-model")!); + + expect(auth).toEqual({ ok: false, error: 'No API key found for "custom-provider"' }); + }); + + test("getApiKeyAndHeaders returns an error for failed authHeader resolution", async () => { + writeRawModelsJson({ + "custom-provider": { + ...providerWithApiKey("!exit 1"), + authHeader: true, + }, + }); + + const registry = await createModelRegistry(authStorage, modelsJsonPath); + const model = registry.find("custom-provider", "test-model"); + expect(model).toBeDefined(); + + const auth = await registry.getApiKeyAndHeaders(model!); + expect(auth.ok).toBe(false); + if (!auth.ok) { + expect(auth.error).toContain('Failed to resolve API key for provider "custom-provider"'); + } + }); + }); + }); +}); diff --git a/packages/coding-agent/test/model-resolver.test.ts b/packages/coding-agent/test/model-resolver.test.ts new file mode 100644 index 0000000000000000000000000000000000000000..8f37b7054edcb835b22ff06207d5a586c102b440 --- /dev/null +++ b/packages/coding-agent/test/model-resolver.test.ts @@ -0,0 +1,916 @@ +import { mkdirSync, rmSync } from "node:fs"; +import { tmpdir } from "node:os"; +import { join } from "node:path"; +import { Agent } from "@earendil-works/pi-agent-core"; +import type { Model } from "@earendil-works/pi-ai"; +import { getModel, streamSimple } from "@earendil-works/pi-ai/compat"; +import { getBuiltinModels, getBuiltinProviders } from "@earendil-works/pi-ai/providers/all"; +import { afterEach, describe, expect, test, vi } from "vitest"; +import { AgentSession } from "../src/core/agent-session.ts"; +import { AuthStorage } from "../src/core/auth-storage.ts"; +import { + defaultModelPerProvider, + findInitialModel, + parseModelPattern, + resolveCliModel, + resolveModelScope, + resolveModelScopeWithDiagnostics, +} from "../src/core/model-resolver.ts"; +import { SessionManager } from "../src/core/session-manager.ts"; +import { SettingsManager } from "../src/core/settings-manager.ts"; +import { createModelRegistry, getModelRuntime } from "./model-runtime-test-utils.ts"; +import { createTestResourceLoader } from "./utilities.ts"; + +// Mock models for testing +const mockModels: Model<"anthropic-messages">[] = [ + { + id: "claude-sonnet-4-5", + name: "Claude Sonnet 4.5", + api: "anthropic-messages", + provider: "anthropic", + baseUrl: "https://api.anthropic.com", + reasoning: true, + input: ["text", "image"], + cost: { input: 3, output: 15, cacheRead: 0.3, cacheWrite: 3.75 }, + contextWindow: 200000, + maxTokens: 8192, + }, + { + id: "gpt-4o", + name: "GPT-4o", + api: "anthropic-messages", // Using same type for simplicity + provider: "openai", + baseUrl: "https://api.openai.com", + reasoning: false, + input: ["text", "image"], + cost: { input: 5, output: 15, cacheRead: 0.5, cacheWrite: 5 }, + contextWindow: 128000, + maxTokens: 4096, + }, +]; + +// Mock OpenRouter models with colons in IDs +const mockOpenRouterModels: Model<"anthropic-messages">[] = [ + { + id: "qwen/qwen3-coder:exacto", + name: "Qwen3 Coder Exacto", + api: "anthropic-messages", + provider: "openrouter", + baseUrl: "https://openrouter.ai/api/v1", + reasoning: true, + input: ["text"], + cost: { input: 1, output: 2, cacheRead: 0.1, cacheWrite: 1 }, + contextWindow: 128000, + maxTokens: 8192, + }, + { + id: "openai/gpt-4o:extended", + name: "GPT-4o Extended", + api: "anthropic-messages", + provider: "openrouter", + baseUrl: "https://openrouter.ai/api/v1", + reasoning: false, + input: ["text", "image"], + cost: { input: 5, output: 15, cacheRead: 0.5, cacheWrite: 5 }, + contextWindow: 128000, + maxTokens: 4096, + }, +]; + +const allModels = [...mockModels, ...mockOpenRouterModels]; + +describe("parseModelPattern", () => { + describe("simple patterns without colons", () => { + test("exact match returns model with undefined thinking level", () => { + const result = parseModelPattern("claude-sonnet-4-5", allModels); + expect(result.model?.id).toBe("claude-sonnet-4-5"); + expect(result.thinkingLevel).toBeUndefined(); + expect(result.warning).toBeUndefined(); + }); + + test("partial match returns best model with undefined thinking level", () => { + const result = parseModelPattern("sonnet", allModels); + expect(result.model?.id).toBe("claude-sonnet-4-5"); + expect(result.thinkingLevel).toBeUndefined(); + expect(result.warning).toBeUndefined(); + }); + + test("no match returns undefined model and thinking level", () => { + const result = parseModelPattern("nonexistent", allModels); + expect(result.model).toBeUndefined(); + expect(result.thinkingLevel).toBeUndefined(); + expect(result.warning).toBeUndefined(); + }); + }); + + describe("patterns with valid thinking levels", () => { + test("sonnet:high returns sonnet with high thinking level", () => { + const result = parseModelPattern("sonnet:high", allModels); + expect(result.model?.id).toBe("claude-sonnet-4-5"); + expect(result.thinkingLevel).toBe("high"); + expect(result.warning).toBeUndefined(); + }); + + test("gpt-4o:medium returns gpt-4o with medium thinking level", () => { + const result = parseModelPattern("gpt-4o:medium", allModels); + expect(result.model?.id).toBe("gpt-4o"); + expect(result.thinkingLevel).toBe("medium"); + expect(result.warning).toBeUndefined(); + }); + + test("all valid thinking levels work", () => { + for (const level of ["off", "minimal", "low", "medium", "high", "xhigh", "max"]) { + const result = parseModelPattern(`sonnet:${level}`, allModels); + expect(result.model?.id).toBe("claude-sonnet-4-5"); + expect(result.thinkingLevel).toBe(level); + expect(result.warning).toBeUndefined(); + } + }); + }); + + describe("patterns with invalid thinking levels", () => { + test("sonnet:random returns sonnet with undefined thinking level and warning", () => { + const result = parseModelPattern("sonnet:random", allModels); + expect(result.model?.id).toBe("claude-sonnet-4-5"); + expect(result.thinkingLevel).toBeUndefined(); + expect(result.warning).toContain("Invalid thinking level"); + expect(result.warning).toContain("random"); + }); + + test("gpt-4o:invalid returns gpt-4o with undefined thinking level and warning", () => { + const result = parseModelPattern("gpt-4o:invalid", allModels); + expect(result.model?.id).toBe("gpt-4o"); + expect(result.thinkingLevel).toBeUndefined(); + expect(result.warning).toContain("Invalid thinking level"); + }); + }); + + describe("OpenRouter models with colons in IDs", () => { + test("qwen3-coder:exacto matches the model with undefined thinking level", () => { + const result = parseModelPattern("qwen/qwen3-coder:exacto", allModels); + expect(result.model?.id).toBe("qwen/qwen3-coder:exacto"); + expect(result.thinkingLevel).toBeUndefined(); + expect(result.warning).toBeUndefined(); + }); + + test("openrouter/qwen/qwen3-coder:exacto matches with provider prefix", () => { + const result = parseModelPattern("openrouter/qwen/qwen3-coder:exacto", allModels); + expect(result.model?.id).toBe("qwen/qwen3-coder:exacto"); + expect(result.model?.provider).toBe("openrouter"); + expect(result.thinkingLevel).toBeUndefined(); + expect(result.warning).toBeUndefined(); + }); + + test("qwen3-coder:exacto:high matches model with high thinking level", () => { + const result = parseModelPattern("qwen/qwen3-coder:exacto:high", allModels); + expect(result.model?.id).toBe("qwen/qwen3-coder:exacto"); + expect(result.thinkingLevel).toBe("high"); + expect(result.warning).toBeUndefined(); + }); + + test("openrouter/qwen/qwen3-coder:exacto:high matches with provider and thinking level", () => { + const result = parseModelPattern("openrouter/qwen/qwen3-coder:exacto:high", allModels); + expect(result.model?.id).toBe("qwen/qwen3-coder:exacto"); + expect(result.model?.provider).toBe("openrouter"); + expect(result.thinkingLevel).toBe("high"); + expect(result.warning).toBeUndefined(); + }); + + test("gpt-4o:extended matches the extended model with undefined thinking level", () => { + const result = parseModelPattern("openai/gpt-4o:extended", allModels); + expect(result.model?.id).toBe("openai/gpt-4o:extended"); + expect(result.thinkingLevel).toBeUndefined(); + expect(result.warning).toBeUndefined(); + }); + }); + + describe("invalid thinking levels with OpenRouter models", () => { + test("qwen3-coder:exacto:random returns model with undefined thinking level and warning", () => { + const result = parseModelPattern("qwen/qwen3-coder:exacto:random", allModels); + expect(result.model?.id).toBe("qwen/qwen3-coder:exacto"); + expect(result.thinkingLevel).toBeUndefined(); + expect(result.warning).toContain("Invalid thinking level"); + expect(result.warning).toContain("random"); + }); + + test("qwen3-coder:exacto:high:random returns model with undefined thinking level and warning", () => { + const result = parseModelPattern("qwen/qwen3-coder:exacto:high:random", allModels); + expect(result.model?.id).toBe("qwen/qwen3-coder:exacto"); + expect(result.thinkingLevel).toBeUndefined(); + expect(result.warning).toContain("Invalid thinking level"); + expect(result.warning).toContain("random"); + }); + }); + + describe("edge cases", () => { + test("empty pattern matches via partial matching", () => { + // Empty string is included in all model IDs, so partial matching finds a match + const result = parseModelPattern("", allModels); + expect(result.model).not.toBeNull(); + expect(result.thinkingLevel).toBeUndefined(); + }); + + test("pattern ending with colon treats empty suffix as invalid", () => { + const result = parseModelPattern("sonnet:", allModels); + // Empty string after colon is not a valid thinking level + // So it tries to match "sonnet:" which won't match, then tries "sonnet" + expect(result.model?.id).toBe("claude-sonnet-4-5"); + expect(result.warning).toContain("Invalid thinking level"); + }); + }); +}); + +describe("resolveModelScopeWithDiagnostics", () => { + test("returns scoped models and structured diagnostics without writing console warnings", async () => { + const warn = vi.spyOn(console, "warn").mockImplementation(() => {}); + try { + const registry = { + getAvailable: () => allModels, + } as unknown as Parameters[1]; + + const result = await resolveModelScopeWithDiagnostics(["sonnet:high", "gpt-4o:invalid", "missing"], registry); + + expect(result.scopedModels.map((scoped) => scoped.model.id)).toEqual(["claude-sonnet-4-5", "gpt-4o"]); + expect(result.scopedModels[0].thinkingLevel).toBe("high"); + expect(result.scopedModels[1].thinkingLevel).toBeUndefined(); + expect(result.diagnostics).toEqual([ + { + type: "warning", + message: 'Invalid thinking level "invalid" in pattern "gpt-4o:invalid". Using default instead.', + code: "invalid-thinking-level", + pattern: "gpt-4o:invalid", + }, + { + type: "warning", + message: 'No models match pattern "missing"', + code: "no-match", + pattern: "missing", + }, + ]); + expect(warn).not.toHaveBeenCalled(); + } finally { + warn.mockRestore(); + } + }); + + test("resolveModelScope preserves CLI warning output", async () => { + const warn = vi.spyOn(console, "warn").mockImplementation(() => {}); + try { + const registry = { + getAvailable: () => allModels, + } as unknown as Parameters[1]; + + const scopedModels = await resolveModelScope(["missing"], registry); + + expect(scopedModels).toEqual([]); + expect(warn).toHaveBeenCalledOnce(); + expect(warn.mock.calls[0][0]).toContain('Warning: No models match pattern "missing"'); + } finally { + warn.mockRestore(); + } + }); + + test("resolves bracketed model ids as exact references before glob matching", async () => { + const bracketedModel: Model<"anthropic-messages"> = { + id: "bracketed-model[1m]", + name: "Bracketed Model", + api: "anthropic-messages", + provider: "custom", + baseUrl: "https://example.invalid", + reasoning: true, + input: ["text"], + cost: { input: 1, output: 2, cacheRead: 0.1, cacheWrite: 1 }, + contextWindow: 128000, + maxTokens: 8192, + }; + const registry = { + getAvailable: () => [...allModels, bracketedModel], + } as unknown as Parameters[1]; + + const result = await resolveModelScopeWithDiagnostics(["custom/bracketed-model[1m]"], registry); + + expect(result.scopedModels.map((scoped) => scoped.model.id)).toEqual(["bracketed-model[1m]"]); + expect(result.diagnostics).toEqual([]); + }); + + test("resolves bracketed model ids with thinking levels as exact references before glob matching", async () => { + const bracketedModel: Model<"anthropic-messages"> = { + id: "bracketed-model[1m]", + name: "Bracketed Model", + api: "anthropic-messages", + provider: "custom", + baseUrl: "https://example.invalid", + reasoning: true, + input: ["text"], + cost: { input: 1, output: 2, cacheRead: 0.1, cacheWrite: 1 }, + contextWindow: 128000, + maxTokens: 8192, + }; + const registry = { + getAvailable: () => [...allModels, bracketedModel], + } as unknown as Parameters[1]; + + const result = await resolveModelScopeWithDiagnostics(["custom/bracketed-model[1m]:high"], registry); + + expect(result.scopedModels.map((scoped) => scoped.model.id)).toEqual(["bracketed-model[1m]"]); + expect(result.scopedModels[0].thinkingLevel).toBe("high"); + expect(result.diagnostics).toEqual([]); + }); +}); + +describe("resolveCliModel", () => { + test("resolves --model provider/id without --provider", () => { + const registry = { + getModels: () => allModels, + } as unknown as Parameters[0]["modelRuntime"]; + + const result = resolveCliModel({ + cliModel: "openai/gpt-4o", + modelRuntime: registry, + }); + + expect(result.error).toBeUndefined(); + expect(result.model?.provider).toBe("openai"); + expect(result.model?.id).toBe("gpt-4o"); + }); + + test("resolves fuzzy patterns within an explicit provider", () => { + const registry = { + getModels: () => allModels, + } as unknown as Parameters[0]["modelRuntime"]; + + const result = resolveCliModel({ + cliProvider: "openai", + cliModel: "4o", + modelRuntime: registry, + }); + + expect(result.error).toBeUndefined(); + expect(result.model?.provider).toBe("openai"); + expect(result.model?.id).toBe("gpt-4o"); + }); + + test("supports --model : (without explicit --thinking)", () => { + const registry = { + getModels: () => allModels, + } as unknown as Parameters[0]["modelRuntime"]; + + const result = resolveCliModel({ + cliModel: "sonnet:high", + modelRuntime: registry, + }); + + expect(result.error).toBeUndefined(); + expect(result.model?.id).toBe("claude-sonnet-4-5"); + expect(result.thinkingLevel).toBe("high"); + }); + + test("prefers exact model id match over provider inference (OpenRouter-style ids)", () => { + const registry = { + getModels: () => allModels, + } as unknown as Parameters[0]["modelRuntime"]; + + const result = resolveCliModel({ + cliModel: "openai/gpt-4o:extended", + modelRuntime: registry, + }); + + expect(result.error).toBeUndefined(); + expect(result.model?.provider).toBe("openrouter"); + expect(result.model?.id).toBe("openai/gpt-4o:extended"); + }); + + test("does not strip invalid :suffix as thinking level in --model (treat as raw id)", () => { + const registry = { + getModels: () => allModels, + } as unknown as Parameters[0]["modelRuntime"]; + + const result = resolveCliModel({ + cliProvider: "openai", + cliModel: "gpt-4o:extended", + modelRuntime: registry, + }); + + expect(result.error).toBeUndefined(); + expect(result.model?.provider).toBe("openai"); + expect(result.model?.id).toBe("gpt-4o:extended"); + }); + + test("allows custom model ids for explicit providers without double prefixing", () => { + const registry = { + getModels: () => allModels, + } as unknown as Parameters[0]["modelRuntime"]; + + const result = resolveCliModel({ + cliProvider: "openrouter", + cliModel: "openrouter/openai/ghost-model", + modelRuntime: registry, + }); + + expect(result.error).toBeUndefined(); + expect(result.model?.provider).toBe("openrouter"); + expect(result.model?.id).toBe("openai/ghost-model"); + }); + + test("returns a clear error when there are no models", () => { + const registry = { + getModels: () => [], + } as unknown as Parameters[0]["modelRuntime"]; + + const result = resolveCliModel({ + cliProvider: "openai", + cliModel: "gpt-4o", + modelRuntime: registry, + }); + + expect(result.model).toBeUndefined(); + expect(result.error).toContain("No models available"); + }); + + test("prefers the sole authenticated provider for an ambiguous bare exact model id", () => { + const azureModel: Model<"anthropic-messages"> = { + ...mockModels[1], + id: "gpt-5.6-sol", + name: "GPT 5.6 Sol", + provider: "azure-openai-responses", + }; + const codexModel: Model<"anthropic-messages"> = { + ...mockModels[1], + id: "gpt-5.6-sol", + name: "GPT 5.6 Sol", + provider: "openai-codex", + }; + const registry = { + getModels: () => [azureModel, codexModel], + hasConfiguredAuth: (provider: string) => provider === "openai-codex", + } as unknown as Parameters[0]["modelRuntime"]; + + const result = resolveCliModel({ + cliModel: "gpt-5.6-sol", + modelRuntime: registry, + }); + + expect(result.error).toBeUndefined(); + expect(result.model?.provider).toBe("openai-codex"); + expect(result.model?.id).toBe("gpt-5.6-sol"); + }); + + test("requires an explicit provider for an ambiguous bare exact model id without a unique authenticated provider", () => { + const azureModel: Model<"anthropic-messages"> = { + ...mockModels[1], + id: "gpt-5.6-sol", + name: "GPT 5.6 Sol", + provider: "azure-openai-responses", + }; + const codexModel: Model<"anthropic-messages"> = { + ...mockModels[1], + id: "gpt-5.6-sol", + name: "GPT 5.6 Sol", + provider: "openai-codex", + }; + const registry = { + getModels: () => [azureModel, codexModel], + hasConfiguredAuth: () => false, + } as unknown as Parameters[0]["modelRuntime"]; + + const result = resolveCliModel({ + cliModel: "gpt-5.6-sol", + modelRuntime: registry, + }); + + expect(result.model).toBeUndefined(); + expect(result.error).toContain('Model "gpt-5.6-sol" is ambiguous across providers'); + expect(result.error).toContain("azure-openai-responses/gpt-5.6-sol"); + expect(result.error).toContain("openai-codex/gpt-5.6-sol"); + expect(result.error).toContain("Use --provider or provider/model"); + }); + + test("prefers provider/model split over gateway model with matching id", () => { + // When a user writes "zai/glm-5", and both a zai provider model (id: "glm-5") + // and a gateway model (id: "zai/glm-5") exist, prefer the zai provider model. + const zaiModel: Model<"anthropic-messages"> = { + id: "glm-5", + name: "GLM-5", + api: "anthropic-messages", + provider: "zai", + baseUrl: "https://open.bigmodel.cn/api/paas/v4", + reasoning: true, + input: ["text"], + cost: { input: 1, output: 2, cacheRead: 0.1, cacheWrite: 1 }, + contextWindow: 128000, + maxTokens: 8192, + }; + const gatewayModel: Model<"anthropic-messages"> = { + id: "zai/glm-5", + name: "GLM-5", + api: "anthropic-messages", + provider: "vercel-ai-gateway", + baseUrl: "https://ai-gateway.vercel.sh", + reasoning: true, + input: ["text"], + cost: { input: 1, output: 2, cacheRead: 0.1, cacheWrite: 1 }, + contextWindow: 128000, + maxTokens: 8192, + }; + const registry = { + getModels: () => [...allModels, zaiModel, gatewayModel], + hasConfiguredAuth: () => true, + } as unknown as Parameters[0]["modelRuntime"]; + + const result = resolveCliModel({ + cliModel: "zai/glm-5", + modelRuntime: registry, + }); + + expect(result.error).toBeUndefined(); + expect(result.model?.provider).toBe("zai"); + expect(result.model?.id).toBe("glm-5"); + }); + + test("prefers an authenticated exact raw model id over an unauthenticated inferred provider", () => { + const commandcodeModel: Model<"anthropic-messages"> = { + id: "xiaomi/mimo-v2.5-pro", + name: "Xiaomi MiMo via Commandcode", + api: "anthropic-messages", + provider: "commandcode", + baseUrl: "https://example.invalid", + reasoning: false, + input: ["text"], + cost: { input: 1, output: 2, cacheRead: 0.1, cacheWrite: 1 }, + contextWindow: 128000, + maxTokens: 8192, + }; + const xiaomiModel: Model<"anthropic-messages"> = { + id: "mimo-v2.5-pro", + name: "Xiaomi MiMo", + api: "anthropic-messages", + provider: "xiaomi", + baseUrl: "https://api.xiaomimimo.com", + reasoning: false, + input: ["text"], + cost: { input: 1, output: 2, cacheRead: 0.1, cacheWrite: 1 }, + contextWindow: 128000, + maxTokens: 8192, + }; + const registry = { + getModels: () => [...allModels, commandcodeModel, xiaomiModel], + hasConfiguredAuth: (provider: string) => provider === "commandcode", + } as unknown as Parameters[0]["modelRuntime"]; + + const result = resolveCliModel({ + cliModel: "xiaomi/mimo-v2.5-pro", + modelRuntime: registry, + }); + + expect(result.error).toBeUndefined(); + expect(result.model?.provider).toBe("commandcode"); + expect(result.model?.id).toBe("xiaomi/mimo-v2.5-pro"); + }); + + test("resolves provider-prefixed fuzzy patterns (openrouter/qwen -> openrouter model)", () => { + const registry = { + getModels: () => allModels, + } as unknown as Parameters[0]["modelRuntime"]; + + const result = resolveCliModel({ + cliModel: "openrouter/qwen", + modelRuntime: registry, + }); + + expect(result.error).toBeUndefined(); + expect(result.model?.provider).toBe("openrouter"); + expect(result.model?.id).toBe("qwen/qwen3-coder:exacto"); + }); + + describe("custom model fallback with :thinking suffix (#5552)", () => { + // Models for a provider that has registered models but the specific model ID + // is not in the registry (triggers buildFallbackModel path). + const neuralwattModel: Model<"anthropic-messages"> = { + id: "some-base-model", + name: "Some Base Model", + api: "anthropic-messages", + provider: "neuralwatt", + baseUrl: "https://api.neuralwatt.com", + reasoning: false, + input: ["text"], + cost: { input: 1, output: 2, cacheRead: 0.1, cacheWrite: 1 }, + contextWindow: 128000, + maxTokens: 8192, + }; + + const modelsWithNeuralwatt = [...allModels, neuralwattModel]; + + test("strips :thinking suffix from custom model id in fallback path", () => { + const registry = { + getModels: () => modelsWithNeuralwatt, + } as unknown as Parameters[0]["modelRuntime"]; + + const result = resolveCliModel({ + cliModel: "neuralwatt/zai-org/GLM-5.1-FP8:high", + modelRuntime: registry, + }); + + expect(result.error).toBeUndefined(); + expect(result.model?.provider).toBe("neuralwatt"); + // The :high suffix must NOT leak into the model id sent to the API + expect(result.model?.id).toBe("zai-org/GLM-5.1-FP8"); + expect(result.model?.reasoning).toBe(true); + expect(result.thinkingLevel).toBe("high"); + }); + + test("custom model without thinking suffix works normally in fallback path", () => { + const registry = { + getModels: () => modelsWithNeuralwatt, + } as unknown as Parameters[0]["modelRuntime"]; + + const result = resolveCliModel({ + cliModel: "neuralwatt/zai-org/GLM-5.1-FP8", + modelRuntime: registry, + }); + + expect(result.error).toBeUndefined(); + expect(result.model?.provider).toBe("neuralwatt"); + expect(result.model?.id).toBe("zai-org/GLM-5.1-FP8"); + expect(result.thinkingLevel).toBeUndefined(); + }); + + test("all valid thinking levels work in fallback path", () => { + const registry = { + getModels: () => modelsWithNeuralwatt, + } as unknown as Parameters[0]["modelRuntime"]; + + for (const level of ["off", "minimal", "low", "medium", "high", "xhigh", "max"]) { + const result = resolveCliModel({ + cliModel: `neuralwatt/zai-org/GLM-5.1-FP8:${level}`, + modelRuntime: registry, + }); + + expect(result.error).toBeUndefined(); + expect(result.model?.id).toBe("zai-org/GLM-5.1-FP8"); + expect(result.thinkingLevel).toBe(level); + } + }); + + test("invalid thinking suffix on custom model is treated as part of model id", () => { + const registry = { + getModels: () => modelsWithNeuralwatt, + } as unknown as Parameters[0]["modelRuntime"]; + + const result = resolveCliModel({ + cliModel: "neuralwatt/zai-org/GLM-5.1-FP8:banana", + modelRuntime: registry, + }); + + expect(result.error).toBeUndefined(); + expect(result.model?.provider).toBe("neuralwatt"); + // Invalid suffix stays in the id (it's not a thinking level) + expect(result.model?.id).toBe("zai-org/GLM-5.1-FP8:banana"); + expect(result.thinkingLevel).toBeUndefined(); + }); + + test("explicit --provider with custom model:thinking strips suffix correctly", () => { + const registry = { + getModels: () => modelsWithNeuralwatt, + } as unknown as Parameters[0]["modelRuntime"]; + + const result = resolveCliModel({ + cliProvider: "neuralwatt", + cliModel: "zai-org/GLM-5.1-FP8:high", + modelRuntime: registry, + }); + + expect(result.error).toBeUndefined(); + expect(result.model?.provider).toBe("neuralwatt"); + expect(result.model?.id).toBe("zai-org/GLM-5.1-FP8"); + expect(result.thinkingLevel).toBe("high"); + }); + + test("with explicit --thinking, :suffix is kept as part of model id", () => { + const registry = { + getModels: () => modelsWithNeuralwatt, + } as unknown as Parameters[0]["modelRuntime"]; + + const result = resolveCliModel({ + cliModel: "neuralwatt/zai-org/GLM-5.1-FP8:high", + cliThinking: "medium", + modelRuntime: registry, + }); + + expect(result.error).toBeUndefined(); + expect(result.model?.provider).toBe("neuralwatt"); + // :high is kept as part of the model id since --thinking was explicit + expect(result.model?.id).toBe("zai-org/GLM-5.1-FP8:high"); + expect(result.thinkingLevel).toBeUndefined(); + }); + }); +}); + +describe("default model selection", () => { + test("openai defaults track current models", () => { + expect(defaultModelPerProvider.openai).toBe("gpt-5.5"); + expect(defaultModelPerProvider["openai-codex"]).toBe("gpt-5.5"); + }); + + test("zai, minimax, cerebras, and ant-ling defaults track current models", () => { + expect(defaultModelPerProvider.zai).toBe("glm-5.3"); + expect(defaultModelPerProvider["zai-coding-cn"]).toBe("glm-5.3"); + expect(defaultModelPerProvider.minimax).toBe("MiniMax-M2.7"); + expect(defaultModelPerProvider["minimax-cn"]).toBe("MiniMax-M2.7"); + expect(defaultModelPerProvider.cerebras).toBe("gpt-oss-120b"); + expect(defaultModelPerProvider["ant-ling"]).toBe("Ring-2.6-1T"); + }); + + test("built-in defaults exist in generated provider catalogs", () => { + for (const provider of getBuiltinProviders()) { + const defaultId = defaultModelPerProvider[provider]; + expect( + getBuiltinModels(provider).some((model) => model.id === defaultId), + `${provider} default ${defaultId} should exist in its generated catalog`, + ).toBe(true); + } + }); + + test("ai-gateway default tracks current model", () => { + expect(defaultModelPerProvider["vercel-ai-gateway"]).toBe("zai/glm-5.1"); + }); + + test("xai default tracks current model", () => { + expect(defaultModelPerProvider.xai).toBe("grok-4.6"); + }); + + test("qwen token plan individual default tracks current model", () => { + expect(defaultModelPerProvider["qwen-token-plan-individual"]).toBe("qwen3.8-max"); + }); + + test("findInitialModel accepts explicit provider custom model ids", async () => { + const registry = { + getModels: () => allModels, + } as unknown as Parameters[0]["modelRuntime"]; + + const result = await findInitialModel({ + cliProvider: "openrouter", + cliModel: "openrouter/openai/ghost-model", + scopedModels: [], + isContinuing: false, + modelRuntime: registry, + }); + + expect(result.model?.provider).toBe("openrouter"); + expect(result.model?.id).toBe("openai/ghost-model"); + }); + + test("findInitialModel selects ai-gateway default when available", async () => { + const aiGatewayModel: Model<"anthropic-messages"> = { + id: "anthropic/claude-opus-4-6", + name: "Claude Opus 4.6", + api: "anthropic-messages", + provider: "vercel-ai-gateway", + baseUrl: "https://ai-gateway.vercel.sh", + reasoning: true, + input: ["text", "image"], + cost: { input: 5, output: 15, cacheRead: 0.5, cacheWrite: 5 }, + contextWindow: 200000, + maxTokens: 8192, + }; + + const registry = { + getAvailableSnapshot: () => [aiGatewayModel], + } as unknown as Parameters[0]["modelRuntime"]; + + const result = await findInitialModel({ + scopedModels: [], + isContinuing: false, + modelRuntime: registry, + }); + + expect(result.model?.provider).toBe("vercel-ai-gateway"); + expect(result.model?.id).toBe("anthropic/claude-opus-4-6"); + }); + + test("findInitialModel ignores an unauthenticated saved default", async () => { + const savedDeepSeekModel: Model<"anthropic-messages"> = { + id: "deepseek-v4-flash", + name: "DeepSeek V4 Flash", + api: "anthropic-messages", + provider: "deepseek", + baseUrl: "https://api.deepseek.com", + reasoning: true, + input: ["text"], + cost: { input: 1, output: 2, cacheRead: 0.1, cacheWrite: 1 }, + contextWindow: 128000, + maxTokens: 8192, + }; + const localDeepSeekModel: Model<"anthropic-messages"> = { + ...savedDeepSeekModel, + provider: "spark-two", + baseUrl: "http://spark-two:8000/v1", + }; + const registry = { + getModel: (provider: string, modelId: string) => + provider === savedDeepSeekModel.provider && modelId === savedDeepSeekModel.id + ? savedDeepSeekModel + : undefined, + hasConfiguredAuth: (provider: string) => provider === "spark-two", + getAvailableSnapshot: () => [localDeepSeekModel], + } as unknown as Parameters[0]["modelRuntime"]; + + const result = await findInitialModel({ + scopedModels: [], + isContinuing: false, + defaultProvider: "deepseek", + defaultModelId: "deepseek-v4-flash", + modelRuntime: registry, + }); + + expect(result.model?.provider).toBe("spark-two"); + expect(result.model?.id).toBe("deepseek-v4-flash"); + }); + + describe("persisted default model scoping", () => { + const tempDirs: string[] = []; + const sonnet = getModel("anthropic", "claude-sonnet-4-5")!; + const opus = getModel("anthropic", "claude-opus-4-8")!; + + afterEach(() => { + for (const dir of tempDirs.splice(0)) { + rmSync(dir, { recursive: true, force: true }); + } + }); + + async function createSession(options: { scoped: boolean; persistedScope?: string[] }) { + const tempDir = join(tmpdir(), `pi-default-scope-${Date.now()}-${Math.random().toString(36).slice(2)}`); + mkdirSync(tempDir, { recursive: true }); + tempDirs.push(tempDir); + + const settingsManager = SettingsManager.create(tempDir, tempDir); + if (options.persistedScope) { + settingsManager.setEnabledModels(options.persistedScope); + } + + const authStorage = AuthStorage.inMemory({ anthropic: { type: "api_key", key: "test-key" } }); + const modelRuntime = getModelRuntime(await createModelRegistry(authStorage, join(tempDir, "models.json"))); + const agent = new Agent({ + initialState: { + model: sonnet, + systemPrompt: "test", + tools: [], + }, + streamFn: streamSimple, + }); + const session = new AgentSession({ + agent, + sessionManager: SessionManager.inMemory(tempDir), + settingsManager, + cwd: tempDir, + modelRuntime, + resourceLoader: createTestResourceLoader(), + scopedModels: options.scoped ? [{ model: sonnet }] : [], + }); + + return { session, settingsManager }; + } + + test("adds a persisted default to an existing scoped model list", async () => { + const { session, settingsManager } = await createSession({ + scoped: true, + persistedScope: [`${sonnet.provider}/${sonnet.id}`], + }); + + await session.setModel(opus, { persist: true }); + + expect(settingsManager.getDefaultProvider()).toBe(opus.provider); + expect(settingsManager.getDefaultModel()).toBe(opus.id); + expect(session.scopedModels.map((scoped) => `${scoped.model.provider}/${scoped.model.id}`)).toEqual([ + `${sonnet.provider}/${sonnet.id}`, + `${opus.provider}/${opus.id}`, + ]); + expect(settingsManager.getEnabledModels()).toEqual([ + `${sonnet.provider}/${sonnet.id}`, + `${opus.provider}/${opus.id}`, + ]); + }); + + test("does not create a scoped model list when all models are available", async () => { + const { session, settingsManager } = await createSession({ scoped: false }); + + await session.setModel(opus, { persist: true }); + + expect(session.scopedModels).toEqual([]); + expect(settingsManager.getEnabledModels()).toBeUndefined(); + }); + + test("keeps session-only model changes out of scope", async () => { + const { session, settingsManager } = await createSession({ + scoped: true, + persistedScope: [`${sonnet.provider}/${sonnet.id}`], + }); + + await session.setModel(opus, { persist: false }); + + expect(session.scopedModels.map((scoped) => `${scoped.model.provider}/${scoped.model.id}`)).toEqual([ + `${sonnet.provider}/${sonnet.id}`, + ]); + expect(settingsManager.getEnabledModels()).toEqual([`${sonnet.provider}/${sonnet.id}`]); + }); + }); +}); diff --git a/packages/coding-agent/test/model-runtime-auth-options.test.ts b/packages/coding-agent/test/model-runtime-auth-options.test.ts new file mode 100644 index 0000000000000000000000000000000000000000..e633747d325de62795d49fdd0637c32cd746021d --- /dev/null +++ b/packages/coding-agent/test/model-runtime-auth-options.test.ts @@ -0,0 +1,326 @@ +import { type AuthType, type CredentialStore, InMemoryCredentialStore } from "@earendil-works/pi-ai"; +import { describe, expect, it } from "vitest"; +import { AuthStorage } from "../src/core/auth-storage.ts"; +import { ModelRuntime } from "../src/core/model-runtime.ts"; + +function authOptions(runtime: ModelRuntime, type?: AuthType) { + return runtime + .getProviders() + .flatMap((provider) => [ + ...(!type || type === "oauth" + ? provider.auth.oauth + ? [{ type: "oauth" as const, provider, method: provider.auth.oauth }] + : [] + : []), + ...(!type || type === "api_key" + ? provider.auth.apiKey + ? [{ type: "api_key" as const, provider, method: provider.auth.apiKey }] + : [] + : []), + ]); +} + +function testModel(id: string) { + return { + id, + name: id, + reasoning: false, + input: ["text"] as ("text" | "image")[], + cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 }, + contextWindow: 10000, + maxTokens: 1000, + }; +} + +describe("ModelRuntime auth options", () => { + it("accepts a pi-ai CredentialStore", async () => { + const credentials = new InMemoryCredentialStore(); + await credentials.modify("anthropic", async () => ({ type: "api_key", key: "stored-key" })); + const runtime = await ModelRuntime.create({ credentials, modelsPath: null }); + + expect((await runtime.getAuth("anthropic"))?.auth.apiKey).toBe("stored-key"); + }); + + it("scopes provider availability reads and records refresh failures", async () => { + const base = new InMemoryCredentialStore(); + const reads: string[] = []; + let failReads = false; + const credentials: CredentialStore = { + read: async (providerId) => { + reads.push(providerId); + if (failReads) throw new Error(`read failed for ${providerId}`); + return base.read(providerId); + }, + list: () => base.list(), + modify: (providerId, fn) => base.modify(providerId, fn), + delete: (providerId) => base.delete(providerId), + }; + const runtime = await ModelRuntime.create({ credentials, modelsPath: null }); + + reads.length = 0; + await runtime.getAvailable("anthropic"); + expect(new Set(reads)).toEqual(new Set(["anthropic"])); + + failReads = true; + await expect(runtime.getAvailable("anthropic")).rejects.toThrow("Credential store read failed for anthropic"); + expect(runtime.getError()).toContain("Availability refresh: Credential store read failed for anthropic"); + + failReads = false; + await runtime.getAvailable(); + expect(runtime.getError()).toBeUndefined(); + }); + + it("projects provider-owned methods, names, and status", async () => { + const runtime = await ModelRuntime.create({ credentials: AuthStorage.inMemory(), modelsPath: null }); + const options = authOptions(runtime); + + expect(options).toEqual( + expect.arrayContaining([ + expect.objectContaining({ + type: "api_key", + provider: expect.objectContaining({ id: "amazon-bedrock", name: "Amazon Bedrock" }), + method: expect.objectContaining({ name: "AWS credentials or bearer token" }), + }), + expect.objectContaining({ + type: "api_key", + provider: expect.objectContaining({ id: "google-vertex", name: "Google Vertex AI" }), + method: expect.objectContaining({ name: "Google Cloud credentials" }), + }), + expect.objectContaining({ + type: "oauth", + provider: expect.objectContaining({ id: "anthropic", name: "Anthropic" }), + }), + expect.objectContaining({ + type: "api_key", + provider: expect.objectContaining({ id: "cloudflare-ai-gateway", name: "Cloudflare AI Gateway" }), + }), + expect.objectContaining({ + type: "api_key", + provider: expect.objectContaining({ id: "cloudflare-workers-ai", name: "Cloudflare Workers AI" }), + }), + ]), + ); + expect(authOptions(runtime, "api_key").every((option) => option.type === "api_key")).toBe(true); + expect(authOptions(runtime, "oauth").every((option) => option.type === "oauth")).toBe(true); + expect(options.some((option) => option.provider.id === "openai-codex" && option.type === "api_key")).toBe(false); + }); + + it("attaches the provider's active auth status to every method option", async () => { + const runtime = await ModelRuntime.create({ + credentials: AuthStorage.inMemory({ + anthropic: { + type: "oauth", + access: "access", + refresh: "refresh", + expires: Date.now() + 60_000, + }, + }), + modelsPath: null, + }); + + const options = authOptions(runtime).filter((option) => option.provider.id === "anthropic"); + expect(options).toHaveLength(2); + expect(await runtime.checkAuth("anthropic")).toMatchObject({ type: "oauth" }); + }); + + it("distinguishes subscription OAuth from generic OAuth sign-in", async () => { + const runtime = await ModelRuntime.create({ + credentials: AuthStorage.inMemory({ + anthropic: { + type: "oauth", + access: "anthropic-access", + refresh: "anthropic-refresh", + expires: Date.now() + 60 * 60_000, + }, + openrouter: { + type: "oauth", + access: "openrouter-key", + refresh: "", + expires: Number.MAX_SAFE_INTEGER, + }, + radius: { + type: "oauth", + access: "radius-access", + refresh: "radius-refresh", + expires: Date.now() + 60 * 60_000, + }, + }), + modelsPath: null, + }); + + expect(runtime.isUsingOAuth("anthropic")).toBe(true); + expect(runtime.isUsingSubscription("anthropic")).toBe(true); + expect(runtime.isUsingOAuth("openrouter")).toBe(true); + expect(runtime.isUsingSubscription("openrouter")).toBe(false); + expect(runtime.isUsingOAuth("radius")).toBe(true); + expect(runtime.isUsingSubscription("radius")).toBe(false); + }); + + it("constructs an API key method for an extension API-key provider", async () => { + const runtime = await ModelRuntime.create({ credentials: AuthStorage.inMemory(), modelsPath: null }); + runtime.registerProvider("extension-api-key", { + name: "Extension API Key", + baseUrl: "https://example.test/v1", + apiKey: "$EXTENSION_TEST_API_KEY", + api: "openai-completions", + models: [testModel("extension-model")], + }); + + const options = authOptions(runtime).filter((option) => option.provider.id === "extension-api-key"); + expect(options).toHaveLength(1); + expect(options[0]).toMatchObject({ + type: "api_key", + provider: { id: "extension-api-key", name: "Extension API Key" }, + method: { name: "API key" }, + }); + expect(options[0]?.method.login).toBeTypeOf("function"); + }); + + it("resolves configured auth from request-scoped environment overrides", async () => { + const runtime = await ModelRuntime.create({ credentials: AuthStorage.inMemory(), modelsPath: null }); + runtime.registerProvider("request-env-provider", { + baseUrl: "https://example.test/v1", + apiKey: "$REQUEST_SCOPED_API_KEY", + headers: { "x-request-value": "$REQUEST_SCOPED_HEADER" }, + api: "openai-completions", + models: [testModel("request-env-model")], + }); + + const auth = await runtime.getAuth("request-env-provider", { + env: { REQUEST_SCOPED_API_KEY: "request-key", REQUEST_SCOPED_HEADER: "request-header" }, + }); + + expect(auth?.auth).toEqual({ apiKey: "request-key", headers: { "x-request-value": "request-header" } }); + }); + + it("lets an explicit Authorization header override authHeader case-insensitively", async () => { + const runtime = await ModelRuntime.create({ credentials: AuthStorage.inMemory(), modelsPath: null }); + let capturedHeaders: Record | undefined; + runtime.registerProvider("auth-header-provider", { + baseUrl: "https://example.test/v1", + apiKey: "generated-key", + authHeader: true, + api: "openai-completions", + streamSimple: (_model, _context, options) => { + capturedHeaders = options?.headers; + throw new Error("captured"); + }, + models: [testModel("auth-header-model")], + }); + const model = runtime.getModel("auth-header-provider", "auth-header-model"); + expect(model).toBeDefined(); + + await runtime.completeSimple(model!, { messages: [] }, { headers: { authorization: "Explicit token" } }); + + expect(capturedHeaders).toEqual({ authorization: "Explicit token" }); + }); + + it("transforms fully assembled headers once without forwarding the transform", async () => { + const runtime = await ModelRuntime.create({ credentials: AuthStorage.inMemory(), modelsPath: null }); + let capturedHeaders: Record | undefined; + let transforms = 0; + runtime.registerProvider("header-provider", { + baseUrl: "https://example.test/v1", + apiKey: "generated-key", + authHeader: true, + headers: { "x-provider": "provider" }, + api: "openai-completions", + streamSimple: (_model, _context, options) => { + expect(options).not.toHaveProperty("transformHeaders"); + capturedHeaders = options?.headers; + throw new Error("captured"); + }, + models: [{ ...testModel("header-model"), headers: { "x-model": "model" } }], + }); + const model = runtime.getModel("header-provider", "header-model"); + expect(model).toBeDefined(); + + await runtime.completeSimple( + model!, + { messages: [] }, + { + headers: { "x-explicit": "explicit" }, + transformHeaders: async (headers) => { + transforms++; + expect(headers).toEqual({ + Authorization: "Bearer generated-key", + "x-provider": "provider", + "x-model": "model", + "x-explicit": "explicit", + }); + return { ...headers, "x-transformed": "yes" }; + }, + }, + ); + + expect(transforms).toBe(1); + expect(capturedHeaders).toEqual({ + Authorization: "Bearer generated-key", + "x-provider": "provider", + "x-model": "model", + "x-explicit": "explicit", + "x-transformed": "yes", + }); + }); + + it("forwards cancellation to extension OAuth refresh", async () => { + const credentials = AuthStorage.inMemory({ + "extension-oauth": { + type: "oauth", + access: "expired", + refresh: "refresh", + expires: 0, + }, + }); + const runtime = await ModelRuntime.create({ credentials, modelsPath: null }); + let refreshSignal: AbortSignal | undefined; + runtime.registerProvider("extension-oauth", { + name: "Extension OAuth", + baseUrl: "https://example.test/v1", + api: "openai-completions", + oauth: { + name: "Extension subscription", + login: async () => ({ access: "access", refresh: "refresh", expires: Date.now() + 60_000 }), + refreshToken: async (credential, signal) => { + refreshSignal = signal; + return { ...credential, expires: Date.now() + 60_000 }; + }, + getApiKey: (credential) => credential.access, + }, + models: [testModel("extension-model")], + }); + const controller = new AbortController(); + + await runtime.getAuth("extension-oauth", { signal: controller.signal }); + expect(refreshSignal).toBeInstanceOf(AbortSignal); + const reason = new Error("cancelled"); + controller.abort(reason); + expect(refreshSignal?.aborted).toBe(true); + expect(refreshSignal?.reason).toBe(reason); + }); + + it("does not fabricate an API key method for an extension OAuth-only provider", async () => { + const runtime = await ModelRuntime.create({ credentials: AuthStorage.inMemory(), modelsPath: null }); + runtime.registerProvider("extension-oauth", { + name: "Extension OAuth", + baseUrl: "https://example.test/v1", + api: "openai-completions", + oauth: { + name: "Extension subscription", + isSubscription: true, + login: async () => ({ access: "access", refresh: "refresh", expires: Date.now() + 60_000 }), + refreshToken: async (credentials) => credentials, + getApiKey: (credentials) => credentials.access, + }, + models: [testModel("extension-model")], + }); + + const options = authOptions(runtime).filter((option) => option.provider.id === "extension-oauth"); + expect(options).toHaveLength(1); + expect(options[0]).toMatchObject({ + type: "oauth", + provider: { id: "extension-oauth", name: "Extension OAuth" }, + method: { name: "Extension subscription", isSubscription: true }, + }); + }); +}); diff --git a/packages/coding-agent/test/model-runtime-cloudflare-compat.test.ts b/packages/coding-agent/test/model-runtime-cloudflare-compat.test.ts new file mode 100644 index 0000000000000000000000000000000000000000..6e9c45185113e858ecb34fede181d98d4af84dea --- /dev/null +++ b/packages/coding-agent/test/model-runtime-cloudflare-compat.test.ts @@ -0,0 +1,102 @@ +import { complete, resetApiProviders } from "@earendil-works/pi-ai/compat"; +import { describe, expect, it, vi } from "vitest"; +import { AuthStorage } from "../src/core/auth-storage.ts"; +import { ModelRegistry } from "../src/core/model-registry.ts"; +import { ModelRuntime } from "../src/core/model-runtime.ts"; + +const openAIState = vi.hoisted(() => ({ clientOptions: undefined as unknown })); + +vi.mock("openai", () => { + class FakeOpenAI { + constructor(options: unknown) { + openAIState.clientOptions = options; + } + + chat = { + completions: { + create: () => { + const stream = { + async *[Symbol.asyncIterator]() { + yield { + choices: [{ delta: {}, finish_reason: "stop" }], + usage: { prompt_tokens: 1, completion_tokens: 1 }, + }; + }, + }; + const promise = Promise.resolve(stream) as Promise & { + withResponse(): Promise<{ + data: typeof stream; + response: { status: number; headers: Headers }; + }>; + }; + promise.withResponse = async () => ({ + data: stream, + response: { status: 200, headers: new Headers() }, + }); + return promise; + }, + }, + }; + } + + return { default: FakeOpenAI }; +}); + +async function createCloudflareRuntime(): Promise<{ modelRuntime: ModelRuntime; modelRegistry: ModelRegistry }> { + const authStorage = AuthStorage.inMemory(); + await authStorage.modify("cloudflare-ai-gateway", async () => ({ + type: "api_key", + key: "test-token", + env: { + CLOUDFLARE_ACCOUNT_ID: "test-account", + CLOUDFLARE_GATEWAY_ID: "test-gateway", + }, + })); + const modelRuntime = await ModelRuntime.create({ credentials: authStorage, modelsPath: null }); + return { modelRuntime, modelRegistry: new ModelRegistry(modelRuntime) }; +} + +describe("ModelRegistry Cloudflare compat streaming", () => { + it("materializes the Cloudflare endpoint through ModelRuntime streaming", async () => { + const { modelRuntime } = await createCloudflareRuntime(); + const model = modelRuntime.getModel("cloudflare-ai-gateway", "workers-ai/@cf/moonshotai/kimi-k2.6"); + expect(model).toBeDefined(); + + resetApiProviders(); + await modelRuntime.completeSimple(model!, { messages: [] }); + + const clientOptions = openAIState.clientOptions as { + baseURL?: string; + defaultHeaders?: Record; + }; + expect(clientOptions.baseURL).toBe("https://gateway.ai.cloudflare.com/v1/test-account/test-gateway/compat"); + expect(clientOptions.defaultHeaders?.["cf-aig-authorization"]).toBe("Bearer test-token"); + }); + + it("materializes the Cloudflare endpoint after extension-style auth resolution", async () => { + const { modelRegistry } = await createCloudflareRuntime(); + const model = modelRegistry.find("cloudflare-ai-gateway", "workers-ai/@cf/moonshotai/kimi-k2.6"); + expect(model).toBeDefined(); + + resetApiProviders(); + const auth = await modelRegistry.getApiKeyAndHeaders(model!); + expect(auth.ok).toBe(true); + if (!auth.ok) throw new Error(auth.error); + expect(auth.headers).toMatchObject({ + "cf-aig-authorization": "Bearer test-token", + Authorization: null, + "x-api-key": null, + }); + + await complete(model!, { messages: [] }, auth); + + const clientOptions = openAIState.clientOptions as { + baseURL?: string; + defaultHeaders?: Record; + }; + expect(clientOptions.baseURL).toBe("https://gateway.ai.cloudflare.com/v1/test-account/test-gateway/compat"); + expect(clientOptions.defaultHeaders?.["cf-aig-authorization"]).toBe("Bearer test-token"); + expect(clientOptions.defaultHeaders?.Authorization).toBeNull(); + expect(clientOptions.defaultHeaders?.["x-api-key"]).toBeNull(); + }); +}); diff --git a/packages/coding-agent/test/model-runtime-credential-sync.test.ts b/packages/coding-agent/test/model-runtime-credential-sync.test.ts new file mode 100644 index 0000000000000000000000000000000000000000..8f38a9c18bb0913166a6d1cadb5ccb78a7451615 --- /dev/null +++ b/packages/coding-agent/test/model-runtime-credential-sync.test.ts @@ -0,0 +1,375 @@ +import type { ApiKeyCredential, Credential, CredentialStore, Model, Provider } from "@earendil-works/pi-ai"; +import { describe, expect, it, vi } from "vitest"; +import { AuthStorage } from "../src/core/auth-storage.ts"; +import { CredentialSynchronizationError, ModelRuntime } from "../src/core/model-runtime.ts"; + +function model(provider: string): Model<"openai-completions"> { + return { + id: "dynamic", + name: "Dynamic", + api: "openai-completions", + provider, + baseUrl: "https://example.test/v1", + reasoning: false, + input: ["text"], + cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 }, + contextWindow: 1000, + maxTokens: 100, + }; +} + +function provider( + id: string, + options: { + login?: () => Promise; + refreshModels?: Provider["refreshModels"]; + } = {}, +): Provider<"openai-completions"> { + const providerModel = model(id); + return { + id, + name: id, + auth: { + apiKey: { + name: "API key", + login: async () => options.login?.() ?? { type: "api_key", key: `${id}-key` }, + check: async ({ credential }) => (credential ? { type: "api_key", source: "stored" } : undefined), + resolve: async ({ credential }) => + credential ? { auth: { apiKey: credential.key }, source: "stored" } : undefined, + }, + }, + getModels: () => [providerModel], + refreshModels: options.refreshModels, + stream: () => { + throw new Error("unused"); + }, + streamSimple: () => { + throw new Error("unused"); + }, + }; +} + +async function runtimeWithProvider( + registered: Provider, + credentials: AuthStorage = AuthStorage.inMemory(), +): Promise { + const runtime = await ModelRuntime.create({ credentials, modelsPath: null, allowModelNetwork: false }); + runtime.registerNativeProvider(registered); + await runtime.refresh({ allowNetwork: false, providers: [registered.id] }); + return runtime; +} + +describe("ModelRuntime credential synchronization", () => { + it("publishes locally consistent availability before login and logout resolve", async () => { + const credentials = AuthStorage.inMemory(); + const runtime = await runtimeWithProvider(provider("dynamic"), credentials); + + await runtime.login("dynamic", "api_key", { prompt: async () => "unused", notify: () => {} }); + expect(runtime.hasConfiguredAuth("dynamic")).toBe(true); + expect(runtime.getAvailableSnapshot().map((entry) => entry.id)).toContain("dynamic"); + expect(await credentials.read("dynamic")).toEqual({ type: "api_key", key: "dynamic-key" }); + + await runtime.logout("dynamic"); + expect(runtime.hasConfiguredAuth("dynamic")).toBe(false); + expect(runtime.getAvailableSnapshot().some((entry) => entry.provider === "dynamic")).toBe(false); + expect(await credentials.read("dynamic")).toBeUndefined(); + }); + + it("orders same-provider credential operations through local synchronization", async () => { + let markLoginStarted: (() => void) | undefined; + let finishLogin: (() => void) | undefined; + const loginStarted = new Promise((resolve) => { + markLoginStarted = resolve; + }); + const blockedLogin = new Promise((resolve) => { + finishLogin = resolve; + }); + const credentials = AuthStorage.inMemory(); + const runtime = await runtimeWithProvider( + provider("ordered", { + login: async () => { + markLoginStarted?.(); + await blockedLogin; + return { type: "api_key", key: "ordered-key" }; + }, + }), + credentials, + ); + + const login = runtime.login("ordered", "api_key", { prompt: async () => "unused", notify: () => {} }); + await loginStarted; + const logout = runtime.logout("ordered"); + await new Promise((resolve) => setTimeout(resolve, 0)); + expect(await credentials.read("ordered")).toBeUndefined(); + + finishLogin?.(); + await Promise.all([login, logout]); + expect(await credentials.read("ordered")).toBeUndefined(); + expect(runtime.hasConfiguredAuth("ordered")).toBe(false); + }); + + it("allows different providers to run credential flows concurrently", async () => { + let firstStarted: (() => void) | undefined; + let secondStarted: (() => void) | undefined; + let finish: (() => void) | undefined; + const first = new Promise((resolve) => { + firstStarted = resolve; + }); + const second = new Promise((resolve) => { + secondStarted = resolve; + }); + const blocked = new Promise((resolve) => { + finish = resolve; + }); + const runtime = await ModelRuntime.create({ credentials: AuthStorage.inMemory(), modelsPath: null }); + runtime.registerNativeProvider( + provider("one", { + login: async () => { + firstStarted?.(); + await blocked; + return { type: "api_key", key: "one" }; + }, + }), + ); + runtime.registerNativeProvider( + provider("two", { + login: async () => { + secondStarted?.(); + await blocked; + return { type: "api_key", key: "two" }; + }, + }), + ); + await runtime.refresh({ allowNetwork: false, providers: ["one", "two"] }); + + const one = runtime.login("one", "api_key", { prompt: async () => "unused", notify: () => {} }); + const two = runtime.login("two", "api_key", { prompt: async () => "unused", notify: () => {} }); + await Promise.all([first, second]); + finish?.(); + await Promise.all([one, two]); + }); + + it("does not wait for unrelated provider availability during local synchronization", async () => { + let stallUnrelated = false; + const runtime = await ModelRuntime.create({ credentials: AuthStorage.inMemory(), modelsPath: null }); + runtime.registerNativeProvider(provider("target")); + const unrelated = provider("unrelated"); + if (unrelated.auth.apiKey) { + unrelated.auth.apiKey.check = async () => { + if (stallUnrelated) await new Promise(() => {}); + return undefined; + }; + } + runtime.registerNativeProvider(unrelated); + await runtime.refresh({ allowNetwork: false, providers: ["target", "unrelated"] }); + stallUnrelated = true; + + await runtime.login("target", "api_key", { prompt: async () => "unused", notify: () => {} }); + expect(runtime.hasConfiguredAuth("target")).toBe(true); + await expect(runtime.refresh({ allowNetwork: false, providers: ["target"] })).resolves.toMatchObject({ + aborted: false, + }); + }); + + it("reports cancellation that occurs during provider-scoped availability", async () => { + let blockAvailability = false; + let markStarted: (() => void) | undefined; + const started = new Promise((resolve) => { + markStarted = resolve; + }); + const registered = provider("cancelled-availability"); + if (registered.auth.apiKey) { + registered.auth.apiKey.check = async ({ credential }) => { + if (blockAvailability) { + markStarted?.(); + await new Promise(() => {}); + } + return credential ? { type: "api_key", source: "stored" } : undefined; + }; + } + const runtime = await runtimeWithProvider(registered); + await runtime.setRuntimeApiKey(registered.id, "key"); + blockAvailability = true; + const controller = new AbortController(); + const refresh = runtime.refresh({ + allowNetwork: false, + providers: [registered.id], + signal: controller.signal, + }); + await started; + controller.abort(); + + await expect(refresh).resolves.toMatchObject({ aborted: true }); + }); + + it("does not run network refresh inside the credential operation chain", async () => { + const networkRefresh = vi.fn(async () => new Promise(() => {})); + const runtime = await runtimeWithProvider( + provider("local-only", { + refreshModels: async (context) => { + if (context.allowNetwork) await networkRefresh(); + }, + }), + ); + + await runtime.login("local-only", "api_key", { prompt: async () => "unused", notify: () => {} }); + expect(networkRefresh).not.toHaveBeenCalled(); + expect(runtime.hasConfiguredAuth("local-only")).toBe(true); + }); + + it("keeps provider-scoped refreshes from superseding unrelated providers", async () => { + let markStarted: (() => void) | undefined; + let finish: (() => void) | undefined; + let firstSignal: AbortSignal | undefined; + const started = new Promise((resolve) => { + markStarted = resolve; + }); + const blocked = new Promise((resolve) => { + finish = resolve; + }); + const runtime = await ModelRuntime.create({ credentials: AuthStorage.inMemory(), modelsPath: null }); + runtime.registerNativeProvider( + provider("one", { + refreshModels: async (context) => { + if (!context.allowNetwork) return; + firstSignal = context.signal; + markStarted?.(); + await blocked; + }, + }), + ); + runtime.registerNativeProvider(provider("two")); + await runtime.refresh({ allowNetwork: false, providers: ["one", "two"] }); + await runtime.setRuntimeApiKey("one", "one-key"); + await runtime.setRuntimeApiKey("two", "two-key"); + + const first = runtime.refresh({ allowNetwork: true, providers: ["one"] }); + await started; + await runtime.refresh({ allowNetwork: true, providers: ["two"] }); + expect(firstSignal?.aborted).toBe(false); + + finish?.(); + await first; + }); + + it("waits for a committed credential mutation to settle before reporting cancellation", async () => { + let stored: Credential | undefined; + let markCommitted: (() => void) | undefined; + let finishMutation: (() => void) | undefined; + const committed = new Promise((resolve) => { + markCommitted = resolve; + }); + const mutationFinished = new Promise((resolve) => { + finishMutation = resolve; + }); + const credentials: CredentialStore = { + read: async () => stored, + list: async () => (stored ? [{ providerId: "delayed-commit", type: stored.type }] : []), + modify: async (_providerId, update) => { + const next = await update(stored); + if (next) stored = next; + markCommitted?.(); + await mutationFinished; + return stored; + }, + delete: async () => { + stored = undefined; + }, + }; + const runtime = await ModelRuntime.create({ credentials, modelsPath: null }); + runtime.registerNativeProvider(provider("delayed-commit")); + await runtime.refresh({ allowNetwork: false, providers: ["delayed-commit"] }); + const controller = new AbortController(); + let settled = false; + const login = runtime.login("delayed-commit", "api_key", { + signal: controller.signal, + prompt: async () => "unused", + notify: () => {}, + }); + const outcome = login.then( + () => { + settled = true; + return undefined; + }, + (error: unknown) => { + settled = true; + return error; + }, + ); + await committed; + controller.abort(); + await new Promise((resolve) => setTimeout(resolve, 0)); + expect(settled).toBe(false); + + finishMutation?.(); + await expect(outcome).resolves.toMatchObject({ + name: "CredentialSynchronizationError", + credential: { type: "api_key", key: "delayed-commit-key" }, + }); + expect(stored).toEqual({ type: "api_key", key: "delayed-commit-key" }); + }); + + it("reports a typed error when cancellation interrupts post-commit synchronization", async () => { + let blockCacheRefresh = false; + let markCacheRefreshStarted: (() => void) | undefined; + const cacheRefreshStarted = new Promise((resolve) => { + markCacheRefreshStarted = resolve; + }); + const credentials = AuthStorage.inMemory(); + const runtime = await runtimeWithProvider( + provider("cancelled-sync", { + refreshModels: async (context) => { + if (!context.allowNetwork && blockCacheRefresh) { + markCacheRefreshStarted?.(); + await new Promise(() => {}); + } + }, + }), + credentials, + ); + blockCacheRefresh = true; + const controller = new AbortController(); + const login = runtime.login("cancelled-sync", "api_key", { + signal: controller.signal, + prompt: async () => "unused", + notify: () => {}, + }); + await cacheRefreshStarted; + controller.abort(); + + await expect(login).rejects.toMatchObject({ + name: "CredentialSynchronizationError", + providerId: "cancelled-sync", + operation: "login", + credential: { type: "api_key", key: "cancelled-sync-key" }, + }); + expect(await credentials.read("cancelled-sync")).toEqual({ + type: "api_key", + key: "cancelled-sync-key", + }); + }); + + it("reports committed credentials when local synchronization fails", async () => { + let failCacheRefresh = false; + const credentials = AuthStorage.inMemory(); + const runtime = await runtimeWithProvider( + provider("broken-sync", { + refreshModels: async (context) => { + if (!context.allowNetwork && failCacheRefresh) throw new Error("cache restore failed"); + }, + }), + credentials, + ); + failCacheRefresh = true; + + const login = runtime.login("broken-sync", "api_key", { prompt: async () => "unused", notify: () => {} }); + await expect(login).rejects.toMatchObject({ + name: "CredentialSynchronizationError", + providerId: "broken-sync", + operation: "login", + credential: { type: "api_key", key: "broken-sync-key" }, + }); + await expect(login).rejects.toBeInstanceOf(CredentialSynchronizationError); + expect(await credentials.read("broken-sync")).toEqual({ type: "api_key", key: "broken-sync-key" }); + }); +}); diff --git a/packages/coding-agent/test/model-runtime-modify-models-compat.test.ts b/packages/coding-agent/test/model-runtime-modify-models-compat.test.ts new file mode 100644 index 0000000000000000000000000000000000000000..f3000513184a5565cc52f945b1c070cff4472426 --- /dev/null +++ b/packages/coding-agent/test/model-runtime-modify-models-compat.test.ts @@ -0,0 +1,332 @@ +import { mkdtempSync, rmSync, writeFileSync } from "node:fs"; +import { tmpdir } from "node:os"; +import { join } from "node:path"; +import { + createAssistantMessageEventStream, + type DeferredCancelOptions, + type DeferredFetchOptions, + InMemoryModelsStore, + type Model, + type Provider, +} from "@earendil-works/pi-ai"; +import { describe, expect, it } from "vitest"; +import { AuthStorage } from "../src/core/auth-storage.ts"; +import { ModelRegistry } from "../src/core/model-registry.ts"; +import { ModelRuntime } from "../src/core/model-runtime.ts"; + +function model(id: string): Model<"openai-completions"> { + return { + id, + name: id, + api: "openai-completions", + provider: "extension-oauth", + baseUrl: "https://example.test/v1", + reasoning: false, + input: ["text"], + cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 }, + contextWindow: 1000, + maxTokens: 100, + }; +} + +describe("extension provider model lifecycle", () => { + it("registers native pi-ai providers with their auth implementation", async () => { + const runtime = await ModelRuntime.create({ + credentials: AuthStorage.inMemory(), + modelsStore: new InMemoryModelsStore(), + modelsPath: null, + allowModelNetwork: false, + }); + const nativeModel = { + ...model("native"), + provider: "extension-native", + baseUrl: "https://fallback.test/v1", + }; + const provider: Provider = { + id: "extension-native", + name: "Extension Native", + auth: { + apiKey: { + name: "Native setup", + login: async (interaction) => ({ + type: "api_key", + key: await interaction.prompt({ type: "secret", message: "API key" }), + }), + check: async ({ credential }) => + credential?.key ? { type: "api_key", source: "stored native key" } : undefined, + resolve: async ({ credential }) => + credential?.key + ? { + auth: { apiKey: credential.key, baseUrl: "https://resolved.test/v1" }, + source: "stored native key", + } + : undefined, + }, + }, + getModels: () => [nativeModel], + stream: () => { + throw new Error("unused"); + }, + streamSimple: () => { + throw new Error("unused"); + }, + }; + + runtime.registerNativeProvider(provider); + const registry = new ModelRegistry(runtime); + expect(registry.getProvider("extension-native")).toBe(provider); + expect(registry.getRegisteredNativeProvider("extension-native")).toBe(provider); + expect(registry.getRegisteredProviderIds()).toContain("extension-native"); + expect(registry.find("extension-native", "native")).toBeDefined(); + + await runtime.login("extension-native", "api_key", { + prompt: async () => "secret", + notify: () => {}, + }); + expect(await registry.getProviderAuth("extension-native")).toMatchObject({ + auth: { apiKey: "secret", baseUrl: "https://resolved.test/v1" }, + }); + + registry.unregisterProvider("extension-native"); + expect(registry.getProvider("extension-native")).toBeUndefined(); + }); + + it("preserves native deferred methods through provider overlays", async () => { + const tempDir = mkdtempSync(join(tmpdir(), "pi-native-provider-deferred-")); + const modelsPath = join(tempDir, "models.json"); + writeFileSync( + modelsPath, + JSON.stringify({ + providers: { + "extension-native-deferred": { baseUrl: "https://overlay.test/v1" }, + }, + }), + ); + try { + const runtime = await ModelRuntime.create({ + credentials: AuthStorage.inMemory(), + modelsStore: new InMemoryModelsStore(), + modelsPath, + allowModelNetwork: false, + }); + const nativeModel = { + ...model("native-deferred"), + provider: "extension-native-deferred", + baseUrl: "https://native.test/v1", + }; + let fetchedBaseUrl: string | undefined; + let fetchedOptions: DeferredFetchOptions | undefined; + let cancelledId: string | undefined; + let cancelledOptions: DeferredCancelOptions | undefined; + const provider: Provider = { + id: "extension-native-deferred", + name: "Extension Native Deferred", + auth: { + apiKey: { + name: "Native key", + resolve: async () => ({ auth: { apiKey: "key" }, source: "native" }), + }, + }, + getModels: () => [nativeModel], + stream: () => { + throw new Error("unused"); + }, + streamSimple: () => { + throw new Error("unused"); + }, + fetchDeferred: (requestModel, _handle, options) => { + fetchedBaseUrl = requestModel.baseUrl; + fetchedOptions = options; + const message = { + role: "assistant" as const, + content: [], + api: requestModel.api, + provider: requestModel.provider, + model: requestModel.id, + usage: { + input: 0, + output: 0, + cacheRead: 0, + cacheWrite: 0, + totalTokens: 0, + cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 }, + }, + stopReason: "stop" as const, + timestamp: 0, + }; + const stream = createAssistantMessageEventStream(); + stream.push({ type: "start", partial: message }); + stream.push({ type: "done", reason: "stop", message }); + stream.end(message); + return stream; + }, + cancelDeferred: async (_requestModel, handle, options) => { + cancelledId = handle.id; + cancelledOptions = options; + }, + }; + + runtime.registerNativeProvider(provider); + const composedModel = runtime.getModel(provider.id, nativeModel.id); + expect(composedModel).toBeDefined(); + + await runtime.fetchDeferred( + composedModel!, + { + provider: provider.id, + modelId: nativeModel.id, + api: nativeModel.api, + id: "fetch-id", + }, + { + wait: 25, + headers: { "X-Fetch": "fetch" }, + transformHeaders: (headers) => ({ ...headers, "X-Transformed": "fetch" }), + }, + ); + await runtime.cancelDeferred( + composedModel!, + { + provider: provider.id, + modelId: nativeModel.id, + api: nativeModel.api, + id: "cancel-id", + }, + { + timeoutMs: 100, + transformHeaders: (headers) => ({ ...headers, "X-Transformed": "cancel" }), + }, + ); + + expect(fetchedBaseUrl).toBe("https://overlay.test/v1"); + expect(fetchedOptions).toMatchObject({ + apiKey: "key", + wait: 25, + headers: { "X-Fetch": "fetch", "X-Transformed": "fetch" }, + }); + expect(cancelledId).toBe("cancel-id"); + expect(cancelledOptions).toMatchObject({ + apiKey: "key", + timeoutMs: 100, + headers: { "X-Transformed": "cancel" }, + }); + } finally { + rmSync(tempDir, { recursive: true, force: true }); + } + }); + + it("applies models.json overrides above native providers", async () => { + const tempDir = mkdtempSync(join(tmpdir(), "pi-native-provider-")); + const modelsPath = join(tempDir, "models.json"); + writeFileSync( + modelsPath, + JSON.stringify({ + providers: { + "extension-native": { + modelOverrides: { + native: { contextWindow: 4242 }, + }, + }, + }, + }), + ); + try { + const runtime = await ModelRuntime.create({ + credentials: AuthStorage.inMemory(), + modelsStore: new InMemoryModelsStore(), + modelsPath, + allowModelNetwork: false, + }); + const nativeModel = { + ...model("native"), + provider: "extension-native", + baseUrl: "https://native.test/v1", + }; + runtime.registerNativeProvider({ + id: "extension-native", + name: "Extension Native", + auth: { + apiKey: { + name: "Native key", + resolve: async () => ({ auth: { apiKey: "key" }, source: "native" }), + }, + }, + getModels: () => [nativeModel], + stream: () => { + throw new Error("unused"); + }, + streamSimple: () => { + throw new Error("unused"); + }, + }); + + expect(runtime.getModel("extension-native", "native")?.contextWindow).toBe(4242); + } finally { + rmSync(tempDir, { recursive: true, force: true }); + } + }); + + it("publishes refreshModels results without forcing ModelsStore persistence", async () => { + const modelsStore = new InMemoryModelsStore(); + const runtime = await ModelRuntime.create({ + credentials: AuthStorage.inMemory(), + modelsStore, + modelsPath: null, + allowModelNetwork: false, + }); + runtime.registerProvider("extension-dynamic", { + baseUrl: "http://localhost:8080/v1", + apiKey: "local", + api: "openai-completions", + refreshModels: async () => [ + { + ...model("live"), + provider: "extension-dynamic", + baseUrl: "http://localhost:8080/v1", + }, + ], + }); + + await runtime.refresh({ allowNetwork: false }); + expect(runtime.getModel("extension-dynamic", "live")).toBeDefined(); + expect(await modelsStore.read("extension-dynamic")).toBeUndefined(); + }); + + it("applies legacy OAuth modifyModels after async credential initialization", async () => { + const runtime = await ModelRuntime.create({ + credentials: AuthStorage.inMemory({ + "extension-oauth": { + type: "oauth", + access: "access", + refresh: "refresh", + expires: Date.now() + 60_000, + }, + }), + modelsStore: new InMemoryModelsStore(), + modelsPath: null, + allowModelNetwork: false, + }); + runtime.registerProvider("extension-oauth", { + baseUrl: "https://example.test/v1", + api: "openai-completions", + models: [model("base")], + oauth: { + name: "Extension OAuth", + login: async () => { + throw new Error("not used"); + }, + refreshToken: async (credential) => credential, + getApiKey: (credential) => credential.access, + modifyModels: (models, credential) => + credential.access === "access" ? [...models, model("credential-model")] : models, + }, + }); + + await runtime.refresh({ allowNetwork: false }); + expect(runtime.getModel("extension-oauth", "base")).toBeDefined(); + expect(runtime.getModel("extension-oauth", "credential-model")).toBeDefined(); + + await runtime.logout("extension-oauth"); + expect(runtime.getModel("extension-oauth", "credential-model")).toBeUndefined(); + }); +}); diff --git a/packages/coding-agent/test/model-runtime-test-utils.ts b/packages/coding-agent/test/model-runtime-test-utils.ts new file mode 100644 index 0000000000000000000000000000000000000000..715f185ed7328ab3f90a93e9c94027d7f7c648e8 --- /dev/null +++ b/packages/coding-agent/test/model-runtime-test-utils.ts @@ -0,0 +1,34 @@ +import type { CredentialStore } from "@earendil-works/pi-ai"; +import { ModelRegistry } from "../src/core/model-registry.ts"; +import { ModelRuntime } from "../src/core/model-runtime.ts"; +import { InMemoryCodingAgentModelsStore } from "../src/core/models-store.ts"; + +const runtimes = new WeakMap(); + +function wrap(runtime: ModelRuntime): ModelRegistry { + const registry = new ModelRegistry(runtime); + runtimes.set(registry, runtime); + return registry; +} + +/** Load optional models.json configuration without introducing file-backed catalog locks into unit tests. */ +export async function createModelRegistry(credentials: CredentialStore, modelsPath?: string): Promise { + return wrap( + await ModelRuntime.create({ + credentials, + modelsPath, + modelsStore: new InMemoryCodingAgentModelsStore(), + allowModelNetwork: false, + }), + ); +} + +export async function createInMemoryModelRegistry(credentials: CredentialStore): Promise { + return wrap(await ModelRuntime.create({ credentials, modelsPath: null, allowModelNetwork: false })); +} + +export function getModelRuntime(modelRegistry: ModelRegistry): ModelRuntime { + const runtime = runtimes.get(modelRegistry); + if (!runtime) throw new Error("ModelRegistry was not created by the test helper"); + return runtime; +} diff --git a/packages/coding-agent/test/model-selector.test.ts b/packages/coding-agent/test/model-selector.test.ts new file mode 100644 index 0000000000000000000000000000000000000000..ac011e30925857733a6529272ed8d3a015c6b5d4 --- /dev/null +++ b/packages/coding-agent/test/model-selector.test.ts @@ -0,0 +1,106 @@ +import { setKeybindings, type TUI } from "@earendil-works/pi-tui"; +import { afterEach, beforeAll, beforeEach, describe, expect, it, vi } from "vitest"; +import { KeybindingsManager } from "../src/core/keybindings.ts"; +import { ModelSelectorComponent } from "../src/modes/interactive/components/model-selector.ts"; +import { initTheme } from "../src/modes/interactive/theme/theme.ts"; +import { stripAnsi } from "../src/utils/ansi.ts"; +import { createHarness, type Harness } from "./suite/harness.ts"; + +function createFakeTui(): TUI { + return { requestRender: () => {} } as unknown as TUI; +} + +describe("model selector", () => { + let harness: Harness | undefined; + + beforeAll(() => { + initTheme("dark"); + }); + + beforeEach(() => { + setKeybindings(new KeybindingsManager()); + }); + + afterEach(() => { + harness?.cleanup(); + harness = undefined; + }); + + it("keeps the current model marked while browsing", async () => { + harness = await createHarness({ + models: [ + { id: "current-model", name: "Current Model", reasoning: true }, + { id: "browsed-model", name: "Browsed Model", reasoning: true }, + ], + }); + const currentModel = harness.getModel("current-model")!; + const selector = new ModelSelectorComponent( + createFakeTui(), + currentModel, + harness.session.modelRuntime, + [], + () => {}, + () => {}, + ); + + const getModelRow = (id: string): string | undefined => + stripAnsi(selector.render(120).join("\n")) + .split("\n") + .find((line) => line.includes(`${id} [`)) + ?.trimEnd(); + + expect(getModelRow("current-model")).toBe(`→ ✓ current-model [${currentModel.provider}]`); + selector.handleInput("\x1b[B"); + expect(getModelRow("current-model")).toBe(` ✓ current-model [${currentModel.provider}]`); + expect(getModelRow("browsed-model")).toBe(`→ browsed-model [${currentModel.provider}]`); + selector.dispose(); + }); + + it("uses the configured save binding", async () => { + setKeybindings(new KeybindingsManager({ "app.models.save": "ctrl+r" })); + harness = await createHarness(); + const currentModel = harness.getModel()!; + const saveDefault = vi.fn(); + const selector = new ModelSelectorComponent( + createFakeTui(), + currentModel, + harness.session.modelRuntime, + [], + () => {}, + () => {}, + undefined, + saveDefault, + ); + + expect(stripAnsi(selector.render(120).join("\n"))).toContain("Ctrl+R to set as default"); + selector.handleInput("\x13"); + expect(saveDefault).not.toHaveBeenCalled(); + selector.handleInput("\x12"); + expect(saveDefault).toHaveBeenCalledWith(currentModel); + }); + + it("lists every catalog that failed to refresh", async () => { + harness = await createHarness(); + vi.spyOn(harness.session.modelRuntime, "refresh").mockResolvedValue({ + aborted: false, + errors: new Map([ + ["openai", new Error("unavailable")], + ["anthropic", new Error("unavailable")], + ]), + }); + + const selector = new ModelSelectorComponent( + createFakeTui(), + harness.getModel(), + harness.session.modelRuntime, + [], + () => {}, + () => {}, + ); + + await vi.waitFor(() => { + const rendered = stripAnsi(selector.render(120).join("\n")); + expect(rendered).toContain("Could not refresh 2 model catalogs (openai, anthropic); showing cached models."); + }); + }); +}); diff --git a/packages/coding-agent/test/models-store.test.ts b/packages/coding-agent/test/models-store.test.ts new file mode 100644 index 0000000000000000000000000000000000000000..bf645042c8ef19c622a345cefd6f49e9495f9ae0 --- /dev/null +++ b/packages/coding-agent/test/models-store.test.ts @@ -0,0 +1,147 @@ +import { chmodSync, existsSync, mkdirSync, readFileSync, rmSync, statSync, writeFileSync } from "node:fs"; +import { tmpdir } from "node:os"; +import { join } from "node:path"; +import type { Model } from "@earendil-works/pi-ai"; +import lockfile from "proper-lockfile"; +import { afterAll, afterEach, beforeAll, describe, expect, it, vi } from "vitest"; +import { FileModelsStore } from "../src/core/models-store.ts"; + +const sharedTempDir = join(tmpdir(), `pi-models-store-shared-${Date.now()}-${Math.random().toString(36).slice(2)}`); +const sharedModelsPath = join(sharedTempDir, "models-store.json"); + +beforeAll(() => { + mkdirSync(sharedTempDir, { recursive: true }); +}); + +afterAll(() => { + if (existsSync(sharedTempDir)) rmSync(sharedTempDir, { recursive: true }); +}); + +afterEach(() => { + vi.restoreAllMocks(); +}); + +function model(provider: string, id: string): Model<"openai-completions"> { + return { + id, + name: id, + api: "openai-completions", + provider, + baseUrl: "https://example.test/v1", + reasoning: false, + input: ["text"], + cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 }, + contextWindow: 1000, + maxTokens: 100, + }; +} + +describe("FileModelsStore", () => { + it("persists provider catalogs without replacing unrelated providers", async () => { + const store = new FileModelsStore(sharedModelsPath); + + await store.write("one", { models: [model("one", "m1")], checkedAt: 100 }); + await store.write("two", { models: [model("two", "m2")], checkedAt: 200 }); + + const reloaded = new FileModelsStore(sharedModelsPath); + expect((await reloaded.read("one"))?.models.map((entry) => entry.id)).toEqual(["m1"]); + expect((await reloaded.read("one"))?.checkedAt).toBe(100); + expect((await reloaded.read("two"))?.models.map((entry) => entry.id)).toEqual(["m2"]); + + await reloaded.delete("one"); + expect(await reloaded.read("one")).toBeUndefined(); + expect((await reloaded.read("two"))?.models.map((entry) => entry.id)).toEqual(["m2"]); + }); + + it.skipIf(process.platform === "win32")("preserves the mode of an existing models file", async () => { + const managedModelsPath = join(sharedTempDir, "managed-mode.json"); + writeFileSync(managedModelsPath, "{}"); + chmodSync(managedModelsPath, 0o660); + const store = new FileModelsStore(managedModelsPath); + + await store.write("one", { models: [model("one", "m1")], checkedAt: 100 }); + + expect(statSync(managedModelsPath).mode & 0o777).toBe(0o660); + }); + + it("coalesces file reloads across concurrent readers and interleaved storage instances", async () => { + writeFileSync( + sharedModelsPath, + JSON.stringify({ + one: { models: [model("one", "old")] }, + two: { models: [model("two", "m2")] }, + }), + ); + const first = new FileModelsStore(sharedModelsPath); + const second = new FileModelsStore(sharedModelsPath); + const lockSpy = vi.spyOn(lockfile, "lock"); + + const [one, two, missing] = await Promise.all([ + first.read("one", { signal: new AbortController().signal }), + second.read("two", { signal: new AbortController().signal }), + first.read("missing", { signal: new AbortController().signal }), + ]); + expect(one?.models.map((entry) => entry.id)).toEqual(["old"]); + expect(two?.models.map((entry) => entry.id)).toEqual(["m2"]); + expect(missing).toBeUndefined(); + expect(lockSpy).toHaveBeenCalledTimes(1); + + await expect(second.read("one")).resolves.toMatchObject({ models: [{ id: "old" }] }); + expect(lockSpy).toHaveBeenCalledTimes(1); + + const otherPath = join(sharedTempDir, "other-models-store.json"); + writeFileSync(otherPath, "{}"); + const other = new FileModelsStore(otherPath); + await expect(other.read("one")).resolves.toBeUndefined(); + await expect(other.read("one")).resolves.toBeUndefined(); + const third = new FileModelsStore(sharedModelsPath); + writeFileSync(sharedModelsPath, JSON.stringify({ one: { models: [model("one", "newest-model")] } })); + const [firstReload, thirdReload] = await Promise.all([first.read("one"), third.read("one")]); + expect(firstReload).toMatchObject({ models: [{ id: "newest-model" }] }); + expect(thirdReload).toMatchObject({ models: [{ id: "newest-model" }] }); + expect(lockSpy).toHaveBeenCalledTimes(3); + }); + + it("keeps a coalesced reload alive while another reader is still waiting", async () => { + writeFileSync(sharedModelsPath, JSON.stringify({ one: { models: [model("one", "stored")] } })); + const store = new FileModelsStore(sharedModelsPath); + let grantLock: (() => void) | undefined; + const lockGranted = new Promise((resolve) => { + grantLock = resolve; + }); + const release = vi.fn(async () => {}); + const lockSpy = vi.spyOn(lockfile, "lock").mockImplementation(async () => { + await lockGranted; + return release; + }); + const firstController = new AbortController(); + const secondController = new AbortController(); + const first = store.read("one", { signal: firstController.signal }); + const second = store.read("one", { signal: secondController.signal }); + + firstController.abort(); + await expect(first).rejects.toMatchObject({ name: "AbortError" }); + grantLock?.(); + await expect(second).resolves.toMatchObject({ models: [{ id: "stored" }] }); + expect(lockSpy).toHaveBeenCalledTimes(1); + expect(release).toHaveBeenCalledTimes(1); + }); + + it("cancels a catalog write waiting for a held file lock without writing later", async () => { + writeFileSync(sharedModelsPath, JSON.stringify({ one: { models: [model("one", "existing")] } })); + const store = new FileModelsStore(sharedModelsPath); + const release = await lockfile.lock(sharedModelsPath, { realpath: false }); + const controller = new AbortController(); + const pending = store.write("two", { models: [model("two", "cancelled")] }, { signal: controller.signal }); + + await new Promise((resolve) => setTimeout(resolve, 10)); + controller.abort(); + await expect(pending).rejects.toMatchObject({ name: "AbortError" }); + await release(); + await new Promise((resolve) => setTimeout(resolve, 150)); + + const stored = JSON.parse(readFileSync(sharedModelsPath, "utf8")) as Record; + expect(stored.one).toBeDefined(); + expect(stored.two).toBeUndefined(); + }); +}); diff --git a/packages/coding-agent/test/oauth-selector.test.ts b/packages/coding-agent/test/oauth-selector.test.ts new file mode 100644 index 0000000000000000000000000000000000000000..71cfb2f530919ebd895012baf5ef8feaf19549f3 --- /dev/null +++ b/packages/coding-agent/test/oauth-selector.test.ts @@ -0,0 +1,147 @@ +import { setKeybindings } from "@earendil-works/pi-tui"; +import { beforeAll, beforeEach, describe, expect, it } from "vitest"; +import { KeybindingsManager } from "../src/core/keybindings.ts"; +import { OAuthSelectorComponent } from "../src/modes/interactive/components/oauth-selector.ts"; +import { InteractiveMode } from "../src/modes/interactive/interactive-mode.ts"; +import { initTheme } from "../src/modes/interactive/theme/theme.ts"; +import { stripAnsi } from "../src/utils/ansi.ts"; + +describe("OAuthSelectorComponent", () => { + beforeAll(() => { + initTheme("dark"); + }); + + beforeEach(() => { + setKeybindings(new KeybindingsManager()); + }); + + it("projects provider-owned auth options without provider-specific filtering", () => { + const getLoginProviderOptions = ( + InteractiveMode as unknown as { + prototype: { + getLoginProviderOptions( + this: object, + authType?: "oauth" | "api_key", + ): Array<{ id: string; name: string; authType: string; method?: { name: string; login?: unknown } }>; + }; + } + ).prototype.getLoginProviderOptions; + const providers = [ + { + id: "anthropic", + name: "Anthropic", + auth: { + oauth: { name: "Anthropic (Claude Pro/Max)", login: async () => ({}) }, + apiKey: { name: "Anthropic API key", login: async () => ({}) }, + }, + }, + { + id: "google-vertex", + name: "Google Vertex AI", + auth: { apiKey: { name: "Google Cloud credentials" } }, + }, + ]; + const fakeThis = { + session: { + modelRuntime: { + getProviders: () => providers, + getProviderAuthStatus: () => ({ configured: false }), + isUsingOAuth: () => false, + }, + }, + }; + + const apiKeyOptions = getLoginProviderOptions.call(fakeThis, "api_key"); + expect(apiKeyOptions).toMatchObject([ + { + id: "anthropic", + name: "Anthropic", + authType: "api_key", + method: { name: "Anthropic API key" }, + }, + { + id: "google-vertex", + name: "Google Vertex AI", + authType: "api_key", + method: { name: "Google Cloud credentials" }, + }, + ]); + expect(getLoginProviderOptions.call(fakeThis, "oauth")).toMatchObject([ + { id: "anthropic", name: "Anthropic", authType: "oauth" }, + ]); + }); + + it("renders an option without compiled auth status as unconfigured", () => { + const selector = new OAuthSelectorComponent( + "login", + [{ id: "google", name: "Google", authType: "api_key", status: undefined }], + () => {}, + () => {}, + ); + + const output = stripAnsi(selector.render(120).join("\n")); + expect(output).toContain("unconfigured"); + expect(output).not.toContain("✓ configured"); + }); + + it("shows OAuth auth distinctly in the API key selector", () => { + const selector = new OAuthSelectorComponent( + "login", + [{ id: "anthropic", name: "Anthropic", authType: "api_key", status: { type: "oauth", source: "OAuth" } }], + () => {}, + () => {}, + ); + + const output = stripAnsi(selector.render(120).join("\n")); + expect(output).toContain("subscription configured"); + }); + + it("shows environment API key auth as configured", () => { + const selector = new OAuthSelectorComponent( + "login", + [{ id: "openai", name: "OpenAI", authType: "api_key", status: { type: "api_key", source: "OPENAI_API_KEY" } }], + () => {}, + () => {}, + ); + + const output = stripAnsi(selector.render(120).join("\n")); + expect(output).toContain("✓ env: OPENAI_API_KEY"); + expect(output).not.toContain("unconfigured"); + }); + + it("shows models.json API key auth as configured", () => { + const selector = new OAuthSelectorComponent( + "login", + [ + { + id: "local-proxy", + name: "local-proxy", + authType: "api_key", + status: { type: "api_key", source: "key in models.json" }, + }, + ], + () => {}, + () => {}, + ); + + expect(stripAnsi(selector.render(120).join("\n"))).toContain("✓ key in models.json"); + }); + + it("shows models.json command auth as configured", () => { + const selector = new OAuthSelectorComponent( + "login", + [ + { + id: "op-proxy", + name: "op-proxy", + authType: "api_key", + status: { type: "api_key", source: "command in models.json" }, + }, + ], + () => {}, + () => {}, + ); + + expect(stripAnsi(selector.render(120).join("\n"))).toContain("✓ command in models.json"); + }); +}); diff --git a/packages/coding-agent/test/package-command-paths.test.ts b/packages/coding-agent/test/package-command-paths.test.ts new file mode 100644 index 0000000000000000000000000000000000000000..1d534349298c6467f50d7127dd23a5d8a2d92a6c --- /dev/null +++ b/packages/coding-agent/test/package-command-paths.test.ts @@ -0,0 +1,975 @@ +import { + chmodSync, + existsSync, + mkdirSync, + readdirSync, + readFileSync, + realpathSync, + rmSync, + utimesSync, + writeFileSync, +} from "node:fs"; +import { tmpdir } from "node:os"; +import { delimiter, join } from "node:path"; +import lockfile from "proper-lockfile"; +import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; +import { ENV_AGENT_DIR, PACKAGE_NAME, VERSION } from "../src/config.ts"; +import { ModelRuntime } from "../src/core/model-runtime.ts"; +import type { ResolvedPaths } from "../src/core/package-manager.ts"; +import { InMemorySettingsStorage, SettingsManager } from "../src/core/settings-manager.ts"; +import { ProjectTrustStore } from "../src/core/trust-manager.ts"; +import { main } from "../src/main.ts"; +import { ConfigSelectorComponent } from "../src/modes/interactive/components/config-selector.ts"; +import { handlePackageCommand } from "../src/package-manager-cli.ts"; +import { allowNetwork } from "./test-network-env.ts"; + +describe("package commands", () => { + let tempDir: string; + let agentDir: string; + let projectDir: string; + let packageDir: string; + let originalCwd: string; + let originalAgentDir: string | undefined; + let originalPiPackageDir: string | undefined; + let originalPath: string | undefined; + let originalExitCode: typeof process.exitCode; + let originalExecPath: string; + + function getNewerPatchVersion(): string { + const [major = "0", minor = "0", patch = "0"] = VERSION.split("."); + return `${major}.${minor}.${Number.parseInt(patch, 10) + 1}`; + } + + function prepareManagedInstall( + targetVersion: string, + npmExitCode = 0, + ): { managedRoot: string; npmRecordPath: string } { + const managedRoot = join(agentDir, "install"); + const activeRelease = join(managedRoot, "releases", VERSION); + const selfPackageDir = join(activeRelease, "node_modules", ...PACKAGE_NAME.split("/")); + mkdirSync(selfPackageDir, { recursive: true }); + writeFileSync(join(activeRelease, "active.txt"), "active"); + writeFileSync(join(managedRoot, "current-version"), `${VERSION}\n`); + writeFileSync( + join(managedRoot, "managed-install.json"), + `${JSON.stringify({ kind: "pi-managed-install", schemaVersion: 1, layout: "releases-v1" })}\n`, + ); + + const binDir = join(tempDir, "managed-bin"); + const fakeNpmPath = join(tempDir, "managed-npm.cjs"); + const npmRecordPath = join(tempDir, "managed-npm-record.json"); + mkdirSync(binDir, { recursive: true }); + writeFileSync( + fakeNpmPath, + `const fs = require("node:fs"); +const path = require("node:path"); +const args = process.argv.slice(2); +fs.writeFileSync(${JSON.stringify(npmRecordPath)}, JSON.stringify(args)); +if (${npmExitCode} !== 0) process.exit(${npmExitCode}); +const binDir = path.join(process.cwd(), "node_modules", ".bin"); +fs.mkdirSync(binDir, { recursive: true }); +const piPath = path.join(binDir, process.platform === "win32" ? "pi.cmd" : "pi"); +fs.writeFileSync( + piPath, + process.platform === "win32" + ? "@echo off\\r\\necho ${targetVersion}\\r\\n" + : "#!/bin/sh\\nprintf '%s\\n' ${targetVersion}\\n", +); +if (process.platform !== "win32") fs.chmodSync(piPath, 0o755); +`, + ); + const npmPath = join(binDir, process.platform === "win32" ? "npm.cmd" : "npm"); + writeFileSync( + npmPath, + process.platform === "win32" + ? `@echo off\r\n"${originalExecPath}" "${fakeNpmPath}" %*\r\n` + : `#!/bin/sh\nexec "${originalExecPath}" "${fakeNpmPath}" "$@"\n`, + ); + chmodSync(npmPath, 0o755); + + vi.stubEnv("PI_INSTALLER_API_BASE", "https://example.test/api/installer/releases"); + vi.stubEnv("PI_MANAGED_INSTALL_ROOT", managedRoot); + process.env.PI_PACKAGE_DIR = selfPackageDir; + process.env.PATH = `${binDir}${delimiter}${originalPath ?? ""}`; + return { managedRoot, npmRecordPath }; + } + + function mockManagedUpdate(targetVersion: string): void { + vi.stubGlobal( + "fetch", + vi.fn(async (input: string | URL | Request) => { + const url = typeof input === "string" ? input : input instanceof URL ? input.href : input.url; + if (url === "https://pi.dev/api/latest-version") { + return Response.json({ packageName: PACKAGE_NAME, version: targetVersion }); + } + const releaseUrl = `https://example.test/api/installer/releases/${targetVersion}`; + if (url === `${releaseUrl}/package.json` || url === `${releaseUrl}/package-lock.json`) { + return Response.json({}); + } + throw new Error(`Unexpected fetch: ${url}`); + }), + ); + } + + async function runPackageCommandDirectly(args: string[]): Promise { + expect(await handlePackageCommand(args)).toBe(true); + } + + function extensionPaths( + packageRoot: string, + source: string, + scope: "user" | "project", + names: string[], + ): ResolvedPaths { + return { + extensions: names.map((name) => ({ + path: join(packageRoot, "extensions", name), + enabled: true, + metadata: { source, scope, origin: "package", baseDir: packageRoot }, + })), + skills: [], + prompts: [], + themes: [], + }; + } + + beforeEach(() => { + allowNetwork(); + tempDir = join(tmpdir(), `pi-package-commands-${Date.now()}-${Math.random().toString(36).slice(2)}`); + agentDir = join(tempDir, "agent"); + projectDir = join(tempDir, "project"); + packageDir = join(tempDir, "local-package"); + mkdirSync(agentDir, { recursive: true }); + mkdirSync(projectDir, { recursive: true }); + mkdirSync(packageDir, { recursive: true }); + + originalCwd = process.cwd(); + originalAgentDir = process.env[ENV_AGENT_DIR]; + originalPiPackageDir = process.env.PI_PACKAGE_DIR; + originalPath = process.env.PATH; + originalExitCode = process.exitCode; + originalExecPath = process.execPath; + process.exitCode = undefined; + vi.spyOn(process, "exit").mockImplementation(((code?: string | number | null) => { + if (code === undefined || code === null || Number(code) === 0) { + process.exitCode = undefined; + } else { + process.exitCode = code; + } + return undefined as never; + }) as typeof process.exit); + process.env[ENV_AGENT_DIR] = agentDir; + process.chdir(projectDir); + }); + + afterEach(() => { + vi.unstubAllGlobals(); + vi.unstubAllEnvs(); + vi.restoreAllMocks(); + process.chdir(originalCwd); + process.exitCode = originalExitCode; + if (originalAgentDir === undefined) { + delete process.env[ENV_AGENT_DIR]; + } else { + process.env[ENV_AGENT_DIR] = originalAgentDir; + } + if (originalPiPackageDir === undefined) { + delete process.env.PI_PACKAGE_DIR; + } else { + process.env.PI_PACKAGE_DIR = originalPiPackageDir; + } + if (originalPath === undefined) { + delete process.env.PATH; + } else { + process.env.PATH = originalPath; + } + Object.defineProperty(process, "execPath", { value: originalExecPath, configurable: true }); + rmSync(tempDir, { recursive: true, force: true }); + }); + + it("should persist global relative local package paths relative to settings.json", async () => { + const relativePkgDir = join(projectDir, "packages", "local-package"); + mkdirSync(relativePkgDir, { recursive: true }); + + await main(["install", "./packages/local-package"]); + + const settingsPath = join(agentDir, "settings.json"); + const settings = JSON.parse(readFileSync(settingsPath, "utf-8")) as { packages?: string[] }; + expect(settings.packages?.length).toBe(1); + const stored = settings.packages?.[0] ?? ""; + const resolvedFromSettings = realpathSync(join(agentDir, stored)); + expect(resolvedFromSettings).toBe(realpathSync(relativePkgDir)); + }); + + it("should remove local packages using a path with a trailing slash", async () => { + await main(["install", `${packageDir}/`]); + + const settingsPath = join(agentDir, "settings.json"); + const installedSettings = JSON.parse(readFileSync(settingsPath, "utf-8")) as { packages?: string[] }; + expect(installedSettings.packages?.length).toBe(1); + + await main(["remove", `${packageDir}/`]); + + const removedSettings = JSON.parse(readFileSync(settingsPath, "utf-8")) as { packages?: string[] }; + expect(removedSettings.packages ?? []).toHaveLength(0); + }); + + it("skips untrusted project package settings", async () => { + mkdirSync(join(projectDir, ".pi"), { recursive: true }); + writeFileSync(join(projectDir, ".pi", "settings.json"), JSON.stringify({ packages: ["npm:@project/pkg"] })); + const logSpy = vi.spyOn(console, "log").mockImplementation(() => {}); + + try { + await expect(main(["list"])).resolves.toBeUndefined(); + + const stdout = logSpy.mock.calls.map(([message]) => String(message)).join("\n"); + expect(stdout).toContain("No packages installed."); + expect(stdout).not.toContain("Project packages:"); + } finally { + logSpy.mockRestore(); + } + }); + + it("uses remembered project trust for list", async () => { + mkdirSync(join(projectDir, ".pi"), { recursive: true }); + writeFileSync(join(projectDir, ".pi", "settings.json"), JSON.stringify({ packages: ["npm:@project/pkg"] })); + new ProjectTrustStore(agentDir).set(projectDir, true); + const logSpy = vi.spyOn(console, "log").mockImplementation(() => {}); + + try { + await expect(main(["list"])).resolves.toBeUndefined(); + + const stdout = logSpy.mock.calls.map(([message]) => String(message)).join("\n"); + expect(stdout).toContain("Project packages:"); + expect(stdout).toContain("npm:@project/pkg"); + expect(stdout).not.toContain("No packages installed."); + expect(process.exitCode).toBeUndefined(); + } finally { + logSpy.mockRestore(); + } + }); + + it("overrides remembered trust for list with --no-approve", async () => { + mkdirSync(join(projectDir, ".pi"), { recursive: true }); + writeFileSync(join(projectDir, ".pi", "settings.json"), JSON.stringify({ packages: ["npm:@project/pkg"] })); + new ProjectTrustStore(agentDir).set(projectDir, true); + const logSpy = vi.spyOn(console, "log").mockImplementation(() => {}); + + try { + await expect(main(["list", "--no-approve"])).resolves.toBeUndefined(); + + const stdout = logSpy.mock.calls.map(([message]) => String(message)).join("\n"); + expect(stdout).toContain("No packages installed."); + expect(stdout).not.toContain("Project packages:"); + expect(process.exitCode).toBeUndefined(); + } finally { + logSpy.mockRestore(); + } + }); + + it("approves project trust for list with --approve", async () => { + mkdirSync(join(projectDir, ".pi"), { recursive: true }); + writeFileSync(join(projectDir, ".pi", "settings.json"), JSON.stringify({ packages: ["npm:@project/pkg"] })); + const logSpy = vi.spyOn(console, "log").mockImplementation(() => {}); + + try { + await expect(main(["list", "--approve"])).resolves.toBeUndefined(); + + const stdout = logSpy.mock.calls.map(([message]) => String(message)).join("\n"); + expect(stdout).toContain("Project packages:"); + expect(stdout).toContain("npm:@project/pkg"); + expect(stdout).not.toContain("No packages installed."); + expect(process.exitCode).toBeUndefined(); + } finally { + logSpy.mockRestore(); + } + }); + + it("uses default project trust for list", async () => { + mkdirSync(join(projectDir, ".pi"), { recursive: true }); + writeFileSync(join(agentDir, "settings.json"), JSON.stringify({ defaultProjectTrust: "always" })); + writeFileSync(join(projectDir, ".pi", "settings.json"), JSON.stringify({ packages: ["npm:@project/pkg"] })); + const logSpy = vi.spyOn(console, "log").mockImplementation(() => {}); + + try { + await expect(main(["list"])).resolves.toBeUndefined(); + + const stdout = logSpy.mock.calls.map(([message]) => String(message)).join("\n"); + expect(stdout).toContain("Project packages:"); + expect(stdout).toContain("npm:@project/pkg"); + expect(stdout).not.toContain("No packages installed."); + expect(process.exitCode).toBeUndefined(); + } finally { + logSpy.mockRestore(); + } + }); + + it("uses project_trust extensions for package commands", async () => { + mkdirSync(join(projectDir, ".pi"), { recursive: true }); + writeFileSync(join(projectDir, ".pi", "settings.json"), JSON.stringify({ packages: ["npm:@project/pkg"] })); + const logSpy = vi.spyOn(console, "log").mockImplementation(() => {}); + + try { + await expect( + main(["list"], { + extensionFactories: [ + (pi) => { + pi.on("project_trust", () => ({ trusted: "yes" })); + }, + ], + }), + ).resolves.toBeUndefined(); + + const stdout = logSpy.mock.calls.map(([message]) => String(message)).join("\n"); + expect(stdout).toContain("Project packages:"); + expect(stdout).toContain("npm:@project/pkg"); + expect(stdout).not.toContain("No packages installed."); + expect(process.exitCode).toBeUndefined(); + } finally { + logSpy.mockRestore(); + } + }); + + it("does not prompt or ask extensions for project trust during update", async () => { + mkdirSync(join(projectDir, ".pi"), { recursive: true }); + writeFileSync(join(agentDir, "settings.json"), JSON.stringify({ defaultProjectTrust: "always" })); + const fakeNpmPath = join(tempDir, "fake-project-npm.cjs"); + const recordPath = join(tempDir, "project-update.json"); + writeFileSync( + fakeNpmPath, + `const fs=require("node:fs");fs.writeFileSync(${JSON.stringify(recordPath)},JSON.stringify(process.argv.slice(2)));`, + ); + writeFileSync( + join(projectDir, ".pi", "settings.json"), + JSON.stringify({ packages: ["npm:fake-package"], npmCommand: [originalExecPath, fakeNpmPath] }), + ); + let projectTrustCalled = false; + const logSpy = vi.spyOn(console, "log").mockImplementation(() => {}); + + try { + await expect( + main(["update", "--extensions"], { + extensionFactories: [ + (pi) => { + pi.on("project_trust", () => { + projectTrustCalled = true; + return { trusted: "yes" }; + }); + }, + ], + }), + ).resolves.toBeUndefined(); + + expect(projectTrustCalled).toBe(false); + expect(existsSync(recordPath)).toBe(false); + expect(process.exitCode).toBeUndefined(); + } finally { + logSpy.mockRestore(); + } + }); + + it("uses saved project trust during update", async () => { + mkdirSync(join(projectDir, ".pi"), { recursive: true }); + const fakeNpmPath = join(tempDir, "fake-trusted-project-npm.cjs"); + const recordPath = join(tempDir, "trusted-project-update.json"); + writeFileSync( + fakeNpmPath, + `const fs=require("node:fs");fs.writeFileSync(${JSON.stringify(recordPath)},JSON.stringify(process.argv.slice(2)));`, + ); + writeFileSync( + join(projectDir, ".pi", "settings.json"), + JSON.stringify({ packages: ["npm:fake-package"], npmCommand: [originalExecPath, fakeNpmPath] }), + ); + new ProjectTrustStore(agentDir).set(projectDir, true); + const logSpy = vi.spyOn(console, "log").mockImplementation(() => {}); + + try { + await expect(main(["update", "--extensions"])).resolves.toBeUndefined(); + + expect(existsSync(recordPath)).toBe(true); + expect(process.exitCode).toBeUndefined(); + } finally { + logSpy.mockRestore(); + } + }); + + it("lets trust.json override default project trust", async () => { + mkdirSync(join(projectDir, ".pi"), { recursive: true }); + writeFileSync(join(agentDir, "settings.json"), JSON.stringify({ defaultProjectTrust: "always" })); + writeFileSync(join(projectDir, ".pi", "settings.json"), JSON.stringify({ packages: ["npm:@project/pkg"] })); + new ProjectTrustStore(agentDir).set(projectDir, false); + const logSpy = vi.spyOn(console, "log").mockImplementation(() => {}); + + try { + await expect(main(["list"])).resolves.toBeUndefined(); + + const stdout = logSpy.mock.calls.map(([message]) => String(message)).join("\n"); + expect(stdout).toContain("No packages installed."); + expect(stdout).not.toContain("Project packages:"); + expect(process.exitCode).toBeUndefined(); + } finally { + logSpy.mockRestore(); + } + }); + + it("blocks local package changes when project is untrusted", async () => { + mkdirSync(join(projectDir, ".pi"), { recursive: true }); + writeFileSync(join(projectDir, ".pi", "settings.json"), "{}"); + const errorSpy = vi.spyOn(console, "error").mockImplementation(() => {}); + + try { + await expect(main(["install", "-l", "./local-package"])).resolves.toBeUndefined(); + + const stderr = errorSpy.mock.calls.map(([message]) => String(message)).join("\n"); + expect(stderr).toContain("Project is not trusted. Use --approve to modify local package config."); + expect(process.exitCode).toBe(1); + } finally { + errorSpy.mockRestore(); + } + }); + + it("allows local package install to initialize fresh project settings", async () => { + await main(["install", "-l", packageDir]); + + const settingsPath = join(projectDir, ".pi", "settings.json"); + const settings = JSON.parse(readFileSync(settingsPath, "utf-8")) as { packages?: string[] }; + expect(settings.packages?.length).toBe(1); + const stored = settings.packages?.[0] ?? ""; + expect(realpathSync(join(projectDir, ".pi", stored))).toBe(realpathSync(packageDir)); + expect(process.exitCode).toBeUndefined(); + }); + + it("shows install subcommand help", async () => { + const logSpy = vi.spyOn(console, "log").mockImplementation(() => {}); + const errorSpy = vi.spyOn(console, "error").mockImplementation(() => {}); + + try { + await expect(main(["install", "--help"])).resolves.toBeUndefined(); + + const stdout = logSpy.mock.calls.map(([message]) => String(message)).join("\n"); + expect(stdout).toContain("Usage:"); + expect(stdout).toContain("pi install [-l]"); + expect(errorSpy).not.toHaveBeenCalled(); + expect(process.exitCode).toBeUndefined(); + } finally { + logSpy.mockRestore(); + errorSpy.mockRestore(); + } + }); + + it("refreshes only model catalogs with update --models", async () => { + const refresh = vi.fn(async () => ({ aborted: false, errors: new Map() })); + const create = vi.spyOn(ModelRuntime, "create").mockResolvedValue({ refresh } as unknown as ModelRuntime); + const logSpy = vi.spyOn(console, "log").mockImplementation(() => {}); + const errorSpy = vi.spyOn(console, "error").mockImplementation(() => {}); + + await expect(runPackageCommandDirectly(["update", "--models"])).resolves.toBeUndefined(); + + expect(create).toHaveBeenCalledWith({ + authPath: join(agentDir, "auth.json"), + modelsPath: join(agentDir, "models.json"), + allowModelNetwork: false, + signal: expect.any(AbortSignal), + }); + expect(refresh).toHaveBeenCalledWith({ + allowNetwork: true, + force: true, + signal: expect.any(AbortSignal), + }); + expect(logSpy.mock.calls.map(([message]) => String(message)).join("\n")).toContain("Model catalogs refreshed"); + expect(errorSpy).not.toHaveBeenCalled(); + expect(process.exitCode).toBeUndefined(); + }); + + it("rejects update --models combined with another update target", async () => { + const create = vi.spyOn(ModelRuntime, "create"); + const errorSpy = vi.spyOn(console, "error").mockImplementation(() => {}); + + await expect(runPackageCommandDirectly(["update", "--models", "--self"])).resolves.toBeUndefined(); + + expect(create).not.toHaveBeenCalled(); + expect(errorSpy.mock.calls.map(([message]) => String(message)).join("\n")).toContain( + "--models cannot be combined with --self", + ); + expect(process.exitCode).toBe(1); + }); + + it("cycles project package overrides in config local mode", async () => { + const storage = new InMemorySettingsStorage(); + storage.withLock("global", () => JSON.stringify({ packages: ["npm:pi-tools"] })); + const settingsManager = SettingsManager.fromStorage(storage, { projectTrusted: true }); + const resolvedPaths = extensionPaths(join(tempDir, "pkg"), "npm:pi-tools", "user", ["bar.ts"]); + const selector = new ConfigSelectorComponent( + { global: resolvedPaths, project: resolvedPaths }, + settingsManager, + projectDir, + agentDir, + () => {}, + () => {}, + () => {}, + 24, + "project", + ); + + selector.getResourceList().handleInput(" "); + expect(settingsManager.getProjectSettings().packages).toEqual([ + { source: "npm:pi-tools", autoload: false, extensions: ["-extensions/bar.ts"] }, + ]); + + selector.getResourceList().handleInput(" "); + expect(settingsManager.getProjectSettings().packages).toEqual([ + { source: "npm:pi-tools", autoload: false, extensions: ["+extensions/bar.ts"] }, + ]); + + selector.getResourceList().handleInput(" "); + expect(settingsManager.getProjectSettings().packages).toEqual([]); + }); + + it("shows a friendly error for unknown install options", async () => { + const errorSpy = vi.spyOn(console, "error").mockImplementation(() => {}); + + try { + await expect(main(["install", "--unknown"])).resolves.toBeUndefined(); + + const stderr = errorSpy.mock.calls.map(([message]) => String(message)).join("\n"); + expect(stderr).toContain('Unknown option --unknown for "install".'); + expect(stderr).toContain('Use "pi --help" or "pi install [-l] [--approve|--no-approve]".'); + expect(process.exitCode).toBe(1); + } finally { + errorSpy.mockRestore(); + } + }); + + it("shows a friendly error for missing install source", async () => { + const errorSpy = vi.spyOn(console, "error").mockImplementation(() => {}); + + try { + await expect(main(["install"])).resolves.toBeUndefined(); + + const stderr = errorSpy.mock.calls.map(([message]) => String(message)).join("\n"); + expect(stderr).toContain("Missing install source."); + expect(stderr).toContain("Usage: pi install [-l]"); + expect(stderr).not.toContain("at "); + expect(process.exitCode).toBe(1); + } finally { + errorSpy.mockRestore(); + } + }); + + it("allows explicit self-update checks when automatic version checks are disabled", async () => { + const previousSkipVersionCheck = process.env.PI_SKIP_VERSION_CHECK; + process.env.PI_SKIP_VERSION_CHECK = "1"; + const fetchMock = vi.fn(async () => Response.json({ version: VERSION })); + vi.stubGlobal("fetch", fetchMock); + const logSpy = vi.spyOn(console, "log").mockImplementation(() => {}); + const errorSpy = vi.spyOn(console, "error").mockImplementation(() => {}); + + try { + await expect(runPackageCommandDirectly(["update", "--self"])).resolves.toBeUndefined(); + + expect(fetchMock).toHaveBeenCalledOnce(); + expect(logSpy.mock.calls.map(([message]) => String(message)).join("\n")).toContain( + `pi is already up to date (v${VERSION})`, + ); + expect(errorSpy).not.toHaveBeenCalled(); + expect(process.exitCode).toBeUndefined(); + } finally { + if (previousSkipVersionCheck === undefined) { + delete process.env.PI_SKIP_VERSION_CHECK; + } else { + process.env.PI_SKIP_VERSION_CHECK = previousSkipVersionCheck; + } + } + }); + + it("retries a transient self-update version check", async () => { + const previousSkipVersionCheck = process.env.PI_SKIP_VERSION_CHECK; + delete process.env.PI_SKIP_VERSION_CHECK; + const fetchMock = vi + .fn() + .mockRejectedValueOnce(new Error("fetch failed")) + .mockRejectedValueOnce(new Error("fetch failed")) + .mockResolvedValueOnce(Response.json({ version: VERSION })); + vi.stubGlobal("fetch", fetchMock); + const logSpy = vi.spyOn(console, "log").mockImplementation(() => {}); + const errorSpy = vi.spyOn(console, "error").mockImplementation(() => {}); + + try { + await expect(runPackageCommandDirectly(["update", "--self"])).resolves.toBeUndefined(); + expect(fetchMock).toHaveBeenCalledTimes(3); + expect(errorSpy).not.toHaveBeenCalled(); + } finally { + if (previousSkipVersionCheck === undefined) delete process.env.PI_SKIP_VERSION_CHECK; + else process.env.PI_SKIP_VERSION_CHECK = previousSkipVersionCheck; + logSpy.mockRestore(); + errorSpy.mockRestore(); + } + }); + + it("updates installer-managed Pi through a staged immutable release", async () => { + const targetVersion = getNewerPatchVersion(); + const { managedRoot, npmRecordPath } = prepareManagedInstall(targetVersion); + const abandonedStage = join(managedRoot, "staging", "update-abandoned"); + mkdirSync(abandonedStage, { recursive: true }); + writeFileSync(join(abandonedStage, "partial"), "partial"); + const abandonedLock = join(managedRoot, "update.lock"); + mkdirSync(abandonedLock); + utimesSync(abandonedLock, new Date(0), new Date(0)); + mockManagedUpdate(targetVersion); + const logSpy = vi.spyOn(console, "log").mockImplementation(() => {}); + const errorSpy = vi.spyOn(console, "error").mockImplementation(() => {}); + + await expect(runPackageCommandDirectly(["update", "--self"])).resolves.toBeUndefined(); + + expect(readFileSync(join(managedRoot, "current-version"), "utf8")).toBe(`${targetVersion}\n`); + expect(existsSync(join(managedRoot, "releases", targetVersion))).toBe(true); + expect(existsSync(join(managedRoot, "releases", VERSION, "active.txt"))).toBe(true); + expect(readdirSync(join(managedRoot, "staging"))).toEqual([]); + expect(JSON.parse(readFileSync(npmRecordPath, "utf8")) as string[]).toEqual( + expect.arrayContaining(["ci", "--ignore-scripts"]), + ); + expect(logSpy.mock.calls.map(([message]) => String(message)).join("\n")).toContain( + `Updated pi from ${VERSION} to ${targetVersion}`, + ); + expect(errorSpy).not.toHaveBeenCalled(); + expect(process.exitCode).toBeUndefined(); + }); + + it("rejects a concurrent managed update", async () => { + const targetVersion = getNewerPatchVersion(); + const { managedRoot, npmRecordPath } = prepareManagedInstall(targetVersion); + const releaseLock = await lockfile.lock(join(managedRoot, "update"), { realpath: false }); + mockManagedUpdate(targetVersion); + const logSpy = vi.spyOn(console, "log").mockImplementation(() => {}); + const errorSpy = vi.spyOn(console, "error").mockImplementation(() => {}); + + try { + await expect(runPackageCommandDirectly(["update", "--self"])).resolves.toBeUndefined(); + } finally { + await releaseLock(); + } + + expect(readFileSync(join(managedRoot, "current-version"), "utf8")).toBe(`${VERSION}\n`); + expect(existsSync(npmRecordPath)).toBe(false); + expect(logSpy.mock.calls.map(([message]) => String(message)).join("\n")).not.toContain("Updated pi from"); + expect(errorSpy.mock.calls.map(([message]) => String(message)).join("\n")).toContain( + "Another managed Pi update is already running.", + ); + expect(process.exitCode).toBe(1); + }); + + it("rejects forced managed reinstalls", async () => { + const targetVersion = getNewerPatchVersion(); + const { npmRecordPath } = prepareManagedInstall(targetVersion); + const fetchMock = vi.fn(); + vi.stubGlobal("fetch", fetchMock); + const errorSpy = vi.spyOn(console, "error").mockImplementation(() => {}); + + await expect(runPackageCommandDirectly(["update", "--self", "--force"])).resolves.toBeUndefined(); + + expect(fetchMock).not.toHaveBeenCalled(); + expect(existsSync(npmRecordPath)).toBe(false); + expect(errorSpy.mock.calls.map(([message]) => String(message)).join("\n")).toContain( + "Managed pi installations do not support --force", + ); + expect(process.exitCode).toBe(1); + }); + + it("keeps the managed release active when its update fails", async () => { + const targetVersion = getNewerPatchVersion(); + const { managedRoot } = prepareManagedInstall(targetVersion, 23); + mockManagedUpdate(targetVersion); + const logSpy = vi.spyOn(console, "log").mockImplementation(() => {}); + const errorSpy = vi.spyOn(console, "error").mockImplementation(() => {}); + + await expect(runPackageCommandDirectly(["update", "--self"])).resolves.toBeUndefined(); + + expect(readFileSync(join(managedRoot, "current-version"), "utf8")).toBe(`${VERSION}\n`); + expect(existsSync(join(managedRoot, "releases", targetVersion))).toBe(false); + expect(readdirSync(join(managedRoot, "staging"))).toEqual([]); + expect(logSpy.mock.calls.map(([message]) => String(message)).join("\n")).not.toContain("Updated pi from"); + expect(errorSpy.mock.calls.map(([message]) => String(message)).join("\n")).toContain("exited with code 23"); + expect(process.exitCode).toBe(1); + }); + + it("keeps npm self-updates non-managed when the managed environment is inherited", async () => { + const globalPrefix = join(tempDir, "global-prefix"); + const projectPrefix = join(tempDir, "project-prefix"); + const selfPackageDir = join(globalPrefix, "lib", "node_modules", "@earendil-works", "pi-coding-agent"); + const inheritedManagedRoot = join(tempDir, "inherited-managed-install"); + mkdirSync(join(inheritedManagedRoot, "releases"), { recursive: true }); + writeFileSync( + join(inheritedManagedRoot, "managed-install.json"), + JSON.stringify({ kind: "pi-managed-install", schemaVersion: 1, layout: "releases-v1" }), + ); + vi.stubEnv("PI_MANAGED_INSTALL_ROOT", inheritedManagedRoot); + const fakeNpmPath = join(tempDir, "fake-npm.cjs"); + const recordPath = join(tempDir, "self-update.json"); + mkdirSync(selfPackageDir, { recursive: true }); + mkdirSync(join(projectDir, ".pi"), { recursive: true }); + writeFileSync( + fakeNpmPath, + `const fs=require("node:fs"),path=require("node:path"),args=process.argv.slice(2),prefix=args[args.indexOf("--prefix")+1]; +if(args.includes("root")) console.log(path.join(prefix,"lib","node_modules")); +else fs.writeFileSync(${JSON.stringify(recordPath)},JSON.stringify(args)); +`, + ); + writeFileSync( + join(agentDir, "settings.json"), + JSON.stringify({ npmCommand: [originalExecPath, fakeNpmPath, "--prefix", globalPrefix] }, null, 2), + ); + writeFileSync( + join(projectDir, ".pi", "settings.json"), + JSON.stringify({ npmCommand: [originalExecPath, fakeNpmPath, "--prefix", projectPrefix] }, null, 2), + ); + process.env.PI_PACKAGE_DIR = selfPackageDir; + Object.defineProperty(process, "execPath", { + value: join(selfPackageDir, "dist", "cli.js"), + configurable: true, + }); + const fetchMock = vi.fn(async () => Response.json({ version: VERSION })); + vi.stubGlobal("fetch", fetchMock); + + const logSpy = vi.spyOn(console, "log").mockImplementation(() => {}); + const errorSpy = vi.spyOn(console, "error").mockImplementation(() => {}); + + try { + await expect(runPackageCommandDirectly(["update", "--self", "--force"])).resolves.toBeUndefined(); + + expect(process.exitCode).toBeUndefined(); + expect(errorSpy).not.toHaveBeenCalled(); + expect(fetchMock).toHaveBeenCalledOnce(); + const stdout = logSpy.mock.calls.map(([message]) => String(message)).join("\n"); + const recordedArgs = JSON.parse(readFileSync(recordPath, "utf-8")) as string[]; + expect(recordedArgs).toContain(globalPrefix); + expect(recordedArgs).toContain(`${PACKAGE_NAME}@${VERSION}`); + expect(recordedArgs).not.toContain(PACKAGE_NAME); + expect(recordedArgs).not.toContain(projectPrefix); + expect(stdout).toContain(`Updated pi from ${VERSION} to ${VERSION}`); + } finally { + logSpy.mockRestore(); + errorSpy.mockRestore(); + } + }); + + it("uses the current package name when the update check omits packageName", async () => { + const globalPrefix = join(tempDir, "global-prefix"); + const selfPackageDir = join(globalPrefix, "lib", "node_modules", "@mariozechner", "pi-coding-agent"); + const fakeNpmPath = join(tempDir, "fake-npm.cjs"); + const recordPath = join(tempDir, "self-update.json"); + mkdirSync(selfPackageDir, { recursive: true }); + writeFileSync( + fakeNpmPath, + `const fs=require("node:fs"),path=require("node:path"),args=process.argv.slice(2),prefix=args[args.indexOf("--prefix")+1]; +if(args.includes("root")) console.log(path.join(prefix,"lib","node_modules")); +else fs.writeFileSync(${JSON.stringify(recordPath)},JSON.stringify(args)); +`, + ); + writeFileSync( + join(agentDir, "settings.json"), + JSON.stringify({ npmCommand: [originalExecPath, fakeNpmPath, "--prefix", globalPrefix] }, null, 2), + ); + process.env.PI_PACKAGE_DIR = selfPackageDir; + Object.defineProperty(process, "execPath", { + value: join(selfPackageDir, "dist", "cli.js"), + configurable: true, + }); + const targetVersion = getNewerPatchVersion(); + const fetchMock = vi.fn(async () => Response.json({ version: targetVersion })); + vi.stubGlobal("fetch", fetchMock); + + const logSpy = vi.spyOn(console, "log").mockImplementation(() => {}); + const errorSpy = vi.spyOn(console, "error").mockImplementation(() => {}); + + try { + await expect(runPackageCommandDirectly(["update", "--self"])).resolves.toBeUndefined(); + + expect(process.exitCode).toBeUndefined(); + expect(errorSpy).not.toHaveBeenCalled(); + expect(fetchMock).toHaveBeenCalledOnce(); + const stdout = logSpy.mock.calls.map(([message]) => String(message)).join("\n"); + const recordedArgs = JSON.parse(readFileSync(recordPath, "utf-8")) as string[]; + expect(recordedArgs).toContain(`${PACKAGE_NAME}@${targetVersion}`); + expect(recordedArgs).not.toContain(PACKAGE_NAME); + expect(stdout).toContain(`Updated pi from ${VERSION} to ${targetVersion}`); + } finally { + logSpy.mockRestore(); + errorSpy.mockRestore(); + } + }); + + it("installs the active package name from the update check during self-update", async () => { + const globalPrefix = join(tempDir, "global-prefix"); + const selfPackageDir = join(globalPrefix, "lib", "node_modules", "@mariozechner", "pi-coding-agent"); + const fakeNpmPath = join(tempDir, "fake-npm.cjs"); + const recordPath = join(tempDir, "self-update.json"); + mkdirSync(selfPackageDir, { recursive: true }); + writeFileSync( + fakeNpmPath, + `const fs=require("node:fs"),path=require("node:path"),args=process.argv.slice(2),prefix=args[args.indexOf("--prefix")+1]; +if(args.includes("root")) console.log(path.join(prefix,"lib","node_modules")); +else { + const records=fs.existsSync(${JSON.stringify(recordPath)})?JSON.parse(fs.readFileSync(${JSON.stringify(recordPath)},"utf-8")):[]; + records.push(args); + fs.writeFileSync(${JSON.stringify(recordPath)},JSON.stringify(records)); +} +`, + ); + writeFileSync( + join(agentDir, "settings.json"), + JSON.stringify({ npmCommand: [originalExecPath, fakeNpmPath, "--prefix", globalPrefix] }, null, 2), + ); + process.env.PI_PACKAGE_DIR = selfPackageDir; + Object.defineProperty(process, "execPath", { + value: join(selfPackageDir, "dist", "cli.js"), + configurable: true, + }); + const activePackageName = PACKAGE_NAME === "@new-scope/pi" ? "@newer-scope/pi" : "@new-scope/pi"; + vi.stubGlobal( + "fetch", + vi.fn(async () => Response.json({ packageName: activePackageName, version: "0.73.0" })), + ); + + const logSpy = vi.spyOn(console, "log").mockImplementation(() => {}); + const errorSpy = vi.spyOn(console, "error").mockImplementation(() => {}); + + try { + await expect(runPackageCommandDirectly(["update", "--self"])).resolves.toBeUndefined(); + + expect(process.exitCode).toBeUndefined(); + expect(errorSpy).not.toHaveBeenCalled(); + const recordedCalls = JSON.parse(readFileSync(recordPath, "utf-8")) as string[][]; + expect(recordedCalls).toEqual([ + expect.arrayContaining(["uninstall", "-g", PACKAGE_NAME]), + expect.arrayContaining(["install", "-g", `${activePackageName}@0.73.0`]), + ]); + } finally { + logSpy.mockRestore(); + errorSpy.mockRestore(); + } + }); + + it("prints a pnpm metadata hint when self-update fails", async () => { + const globalRoot = join(tempDir, "pnpm", "global", "v11"); + const selfPackageDir = join(globalRoot, "node_modules", "@earendil-works", "pi-coding-agent"); + const fakeBinDir = join(tempDir, "bin"); + const fakePnpmPath = join(fakeBinDir, process.platform === "win32" ? "pnpm.cmd" : "pnpm"); + mkdirSync(selfPackageDir, { recursive: true }); + mkdirSync(fakeBinDir, { recursive: true }); + writeFileSync(join(selfPackageDir, "package.json"), JSON.stringify({ name: PACKAGE_NAME, version: VERSION })); + const fakePnpmScript = + process.platform === "win32" + ? `@echo off\r\nif "%1"=="root" if "%2"=="-g" (echo ${globalRoot} & exit /b 0)\r\nexit /b 23\r\n` + : `#!/bin/sh\nif [ "$1" = "root" ] && [ "$2" = "-g" ]; then\n\tprintf '%s\\n' '${globalRoot.replaceAll("'", "'\\''")}'\n\texit 0\nfi\nexit 23\n`; + writeFileSync(fakePnpmPath, fakePnpmScript); + chmodSync(fakePnpmPath, 0o755); + process.env.PATH = `${fakeBinDir}${process.env.PATH ? `${delimiter}${process.env.PATH}` : ""}`; + process.env.PI_PACKAGE_DIR = selfPackageDir; + Object.defineProperty(process, "execPath", { + value: join(tempDir, "pnpm", "bin", "node"), + configurable: true, + }); + vi.stubGlobal( + "fetch", + vi.fn(async () => Response.json({ version: getNewerPatchVersion() })), + ); + + const logSpy = vi.spyOn(console, "log").mockImplementation(() => {}); + const errorSpy = vi.spyOn(console, "error").mockImplementation(() => {}); + + try { + await expect(runPackageCommandDirectly(["update", "--self"])).resolves.toBeUndefined(); + + expect(process.exitCode).toBe(1); + const stdout = logSpy.mock.calls.map(([message]) => String(message)).join("\n"); + const stderr = errorSpy.mock.calls.map(([message]) => String(message)).join("\n"); + expect(stdout).not.toContain("Updated pi"); + expect(stderr).toContain("exited with code 23"); + expect(stderr).toContain("If pnpm reports missing package versions"); + expect(stderr).toContain("Run `pnpm store prune` and retry `pi update --self`."); + } finally { + logSpy.mockRestore(); + errorSpy.mockRestore(); + } + }); + + it("fails self-update when renamed npm package installation fails", async () => { + const globalPrefix = join(tempDir, "global-prefix"); + const selfPackageDir = join(globalPrefix, "lib", "node_modules", "@mariozechner", "pi-coding-agent"); + const fakeNpmPath = join(tempDir, "fake-npm-fail.cjs"); + const recordPath = join(tempDir, "self-update-fail.json"); + mkdirSync(selfPackageDir, { recursive: true }); + writeFileSync( + fakeNpmPath, + `const fs=require("node:fs"),path=require("node:path"),args=process.argv.slice(2),prefix=args[args.indexOf("--prefix")+1]; +if(args.includes("root")) { + console.log(path.join(prefix,"lib","node_modules")); + process.exit(0); +} +const records=fs.existsSync(${JSON.stringify(recordPath)})?JSON.parse(fs.readFileSync(${JSON.stringify(recordPath)},"utf-8")):[]; +records.push(args); +fs.writeFileSync(${JSON.stringify(recordPath)},JSON.stringify(records)); +if(args.includes("install")) process.exit(23); +`, + ); + writeFileSync( + join(agentDir, "settings.json"), + JSON.stringify({ npmCommand: [originalExecPath, fakeNpmPath, "--prefix", globalPrefix] }, null, 2), + ); + process.env.PI_PACKAGE_DIR = selfPackageDir; + Object.defineProperty(process, "execPath", { + value: join(selfPackageDir, "dist", "cli.js"), + configurable: true, + }); + const activePackageName = PACKAGE_NAME === "@new-scope/pi" ? "@newer-scope/pi" : "@new-scope/pi"; + vi.stubGlobal( + "fetch", + vi.fn(async () => Response.json({ packageName: activePackageName, version: "0.73.0" })), + ); + + const logSpy = vi.spyOn(console, "log").mockImplementation(() => {}); + const errorSpy = vi.spyOn(console, "error").mockImplementation(() => {}); + + try { + await expect(runPackageCommandDirectly(["update", "--self"])).resolves.toBeUndefined(); + + expect(process.exitCode).toBe(1); + const stdout = logSpy.mock.calls.map(([message]) => String(message)).join("\n"); + const stderr = errorSpy.mock.calls.map(([message]) => String(message)).join("\n"); + expect(stdout).not.toContain(`Updated pi`); + expect(stderr).toContain("exited with code 23"); + const recordedCalls = JSON.parse(readFileSync(recordPath, "utf-8")) as string[][]; + expect(recordedCalls).toEqual([ + expect.arrayContaining(["uninstall", "-g", PACKAGE_NAME]), + expect.arrayContaining(["install", "-g", `${activePackageName}@0.73.0`]), + ]); + } finally { + logSpy.mockRestore(); + errorSpy.mockRestore(); + } + }); + + it("suggests the configured source when update input omits the npm prefix", async () => { + const settingsPath = join(agentDir, "settings.json"); + writeFileSync(settingsPath, JSON.stringify({ packages: ["npm:pi-formatter"] }, null, 2)); + + const errorSpy = vi.spyOn(console, "error").mockImplementation(() => {}); + const logSpy = vi.spyOn(console, "log").mockImplementation(() => {}); + + try { + await expect(main(["update", "pi-formatter"])).resolves.toBeUndefined(); + + const stderr = errorSpy.mock.calls.map(([message]) => String(message)).join("\n"); + const stdout = logSpy.mock.calls.map(([message]) => String(message)).join("\n"); + expect(stderr).toContain("Did you mean npm:pi-formatter?"); + expect(stdout).not.toContain("Updated pi-formatter"); + expect(process.exitCode).toBe(1); + + const settings = JSON.parse(readFileSync(settingsPath, "utf-8")) as { packages?: string[] }; + expect(settings.packages).toContain("npm:pi-formatter"); + } finally { + errorSpy.mockRestore(); + logSpy.mockRestore(); + } + }); +}); diff --git a/packages/coding-agent/test/package-distribution.test.ts b/packages/coding-agent/test/package-distribution.test.ts new file mode 100644 index 0000000000000000000000000000000000000000..69e39fb8fb567211c249a7da93267cff88d0732b --- /dev/null +++ b/packages/coding-agent/test/package-distribution.test.ts @@ -0,0 +1,32 @@ +import { readFileSync } from "node:fs"; +import { describe, expect, test } from "vitest"; + +interface CodingAgentPackageJson { + bin: { pi: string }; + main: string; + exports: { + ".": { import: string; types: string }; + "./client": { source: string }; + "./experimental/plugin": { source: string }; + "./rpc-entry": { import: string }; + }; +} + +const packageJson = JSON.parse( + readFileSync(new URL("../package.json", import.meta.url), "utf8"), +) as CodingAgentPackageJson; + +describe("package distribution entrypoints", () => { + test("uses the bundle for executables and modular output for libraries", () => { + expect(packageJson.bin.pi).toBe("dist/bundle/cli.js"); + expect(packageJson.main).toBe("./dist/index.js"); + expect(packageJson.exports["."].import).toBe("./dist/index.js"); + expect(packageJson.exports["./rpc-entry"].import).toBe("./dist/bundle/rpc-entry.js"); + }); + + // Regression for #9132: internal experimental entrypoints must not be published runtime exports. + test("keeps experimental exports source-only", () => { + expect(packageJson.exports["./client"]).toEqual({ source: "./src/client/index.ts" }); + expect(packageJson.exports["./experimental/plugin"]).toEqual({ source: "./src/experimental/plugin.ts" }); + }); +}); diff --git a/packages/coding-agent/test/package-manager-ssh.test.ts b/packages/coding-agent/test/package-manager-ssh.test.ts new file mode 100644 index 0000000000000000000000000000000000000000..1fa9b9173f97f7630e4f8b5f639166de2443993a --- /dev/null +++ b/packages/coding-agent/test/package-manager-ssh.test.ts @@ -0,0 +1,97 @@ +import { mkdirSync, rmSync } from "node:fs"; +import { tmpdir } from "node:os"; +import { join } from "node:path"; +import { afterEach, beforeEach, describe, expect, it } from "vitest"; +import { DefaultPackageManager } from "../src/core/package-manager.ts"; +import { SettingsManager } from "../src/core/settings-manager.ts"; + +describe("Package Manager git source parsing", () => { + let tempDir: string; + let agentDir: string; + let settingsManager: SettingsManager; + let packageManager: DefaultPackageManager; + + beforeEach(() => { + tempDir = join(tmpdir(), `pm-ssh-test-${Date.now()}-${Math.random().toString(36).slice(2)}`); + mkdirSync(tempDir, { recursive: true }); + agentDir = join(tempDir, "agent"); + mkdirSync(agentDir, { recursive: true }); + + settingsManager = SettingsManager.inMemory(); + packageManager = new DefaultPackageManager({ + cwd: tempDir, + agentDir, + settingsManager, + }); + }); + + afterEach(() => { + rmSync(tempDir, { recursive: true, force: true }); + }); + + describe("protocol URLs without git: prefix", () => { + it("should parse https:// URL", () => { + const parsed = (packageManager as any).parseSource("https://github.com/user/repo"); + expect(parsed.type).toBe("git"); + expect(parsed.host).toBe("github.com"); + expect(parsed.path).toBe("user/repo"); + }); + + it("should parse ssh:// URL", () => { + const parsed = (packageManager as any).parseSource("ssh://git@github.com/user/repo"); + expect(parsed.type).toBe("git"); + expect(parsed.host).toBe("github.com"); + expect(parsed.path).toBe("user/repo"); + expect(parsed.repo).toBe("ssh://git@github.com/user/repo"); + }); + }); + + describe("shorthand URLs with git: prefix", () => { + it("should parse git@host:path format", () => { + const parsed = (packageManager as any).parseSource("git:git@github.com:user/repo"); + expect(parsed.type).toBe("git"); + expect(parsed.host).toBe("github.com"); + expect(parsed.path).toBe("user/repo"); + expect(parsed.repo).toBe("git@github.com:user/repo"); + expect(parsed.pinned).toBe(false); + }); + + it("should parse host/path shorthand", () => { + const parsed = (packageManager as any).parseSource("git:github.com/user/repo"); + expect(parsed.type).toBe("git"); + expect(parsed.host).toBe("github.com"); + expect(parsed.path).toBe("user/repo"); + }); + + it("should parse shorthand with ref", () => { + const parsed = (packageManager as any).parseSource("git:git@github.com:user/repo@v1.0.0"); + expect(parsed.type).toBe("git"); + expect(parsed.ref).toBe("v1.0.0"); + expect(parsed.pinned).toBe(true); + }); + }); + + describe("unsupported without git: prefix", () => { + it("should treat git@host:path as local without git: prefix", () => { + const parsed = (packageManager as any).parseSource("git@github.com:user/repo"); + expect(parsed.type).toBe("local"); + }); + + it("should treat host/path shorthand as local without git: prefix", () => { + const parsed = (packageManager as any).parseSource("github.com/user/repo"); + expect(parsed.type).toBe("local"); + }); + }); + + describe("identity normalization", () => { + it("should normalize protocol and shorthand-prefixed URLs to same identity", () => { + const prefixed = (packageManager as any).getPackageIdentity("git:git@github.com:user/repo"); + const https = (packageManager as any).getPackageIdentity("https://github.com/user/repo"); + const ssh = (packageManager as any).getPackageIdentity("ssh://git@github.com/user/repo"); + + expect(prefixed).toBe("git:github.com/user/repo"); + expect(prefixed).toBe(https); + expect(prefixed).toBe(ssh); + }); + }); +}); diff --git a/packages/coding-agent/test/package-manager.test.ts b/packages/coding-agent/test/package-manager.test.ts new file mode 100644 index 0000000000000000000000000000000000000000..6f4f6b4fab3eb2ba6fa312890d3aa391ef15e186 --- /dev/null +++ b/packages/coding-agent/test/package-manager.test.ts @@ -0,0 +1,2686 @@ +import { EventEmitter } from "node:events"; +import { existsSync, mkdirSync, rmSync, statSync, symlinkSync, writeFileSync } from "node:fs"; +import { tmpdir } from "node:os"; +import { join, relative } from "node:path"; +import { PassThrough } from "node:stream"; +import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; +import { DefaultPackageManager, type ProgressEvent, type ResolvedResource } from "../src/core/package-manager.ts"; +import { SettingsManager } from "../src/core/settings-manager.ts"; + +function normalizeForMatch(value: string): string { + return value.replace(/\\/g, "/"); +} + +function pathEndsWith(actualPath: string, suffix: string): boolean { + return normalizeForMatch(actualPath).endsWith(normalizeForMatch(suffix)); +} + +class MockSpawnedProcess extends EventEmitter { + stdout = new PassThrough(); + stderr = new PassThrough(); + + kill(): boolean { + this.emit("close", null, "SIGTERM"); + return true; + } +} + +interface PackageManagerInternals { + runCommand(command: string, args: string[], options?: { cwd?: string }): Promise; + runCommandCapture( + command: string, + args: string[], + options?: { cwd?: string; timeoutMs?: number; env?: Record }, + ): Promise; + getLocalGitUpdateTarget(installedPath: string): Promise<{ ref: string; head: string; fetchArgs: string[] }>; + parseSource( + source: string, + ): + | { type: "npm"; spec: string; name: string; pinned: boolean } + | { type: "git"; repo: string; host: string; path: string; pinned: boolean; ref?: string } + | { type: "local"; path: string }; + getNpmInstallPath( + source: { type: "npm"; spec: string; name: string; pinned: boolean }, + scope: "user" | "project" | "temporary", + ): string; + getGitInstallPath( + source: { type: "git"; repo: string; host: string; path: string; pinned: boolean; ref?: string }, + scope: "user" | "project" | "temporary", + ): string; +} + +// Helper to check if a resource is enabled +const isEnabled = (r: ResolvedResource, pathMatch: string, matchFn: "endsWith" | "includes" = "endsWith") => { + const normalizedPath = normalizeForMatch(r.path); + const normalizedMatch = normalizeForMatch(pathMatch); + return matchFn === "endsWith" + ? normalizedPath.endsWith(normalizedMatch) && r.enabled + : normalizedPath.includes(normalizedMatch) && r.enabled; +}; + +const isDisabled = (r: ResolvedResource, pathMatch: string, matchFn: "endsWith" | "includes" = "endsWith") => { + const normalizedPath = normalizeForMatch(r.path); + const normalizedMatch = normalizeForMatch(pathMatch); + return matchFn === "endsWith" + ? normalizedPath.endsWith(normalizedMatch) && !r.enabled + : normalizedPath.includes(normalizedMatch) && !r.enabled; +}; + +describe("DefaultPackageManager", () => { + let tempDir: string; + let agentDir: string; + let settingsManager: SettingsManager; + let packageManager: DefaultPackageManager; + let previousOfflineEnv: string | undefined; + + beforeEach(() => { + previousOfflineEnv = process.env.PI_OFFLINE; + delete process.env.PI_OFFLINE; + tempDir = join(tmpdir(), `pm-test-${Date.now()}-${Math.random().toString(36).slice(2)}`); + mkdirSync(tempDir, { recursive: true }); + agentDir = join(tempDir, "agent"); + mkdirSync(agentDir, { recursive: true }); + + settingsManager = SettingsManager.inMemory(); + packageManager = new DefaultPackageManager({ + cwd: tempDir, + agentDir, + settingsManager, + }); + }); + + afterEach(() => { + if (previousOfflineEnv === undefined) { + delete process.env.PI_OFFLINE; + } else { + process.env.PI_OFFLINE = previousOfflineEnv; + } + vi.restoreAllMocks(); + vi.unstubAllGlobals(); + rmSync(tempDir, { recursive: true, force: true }); + }); + + describe("resolve", () => { + it("should return no package-sourced paths when no sources configured", async () => { + const result = await packageManager.resolve(); + expect(result.extensions).toEqual([]); + expect(result.prompts).toEqual([]); + expect(result.themes).toEqual([]); + expect(result.skills.every((r) => r.metadata.source === "auto" && r.metadata.origin === "top-level")).toBe( + true, + ); + }); + + it("should resolve local extension paths from settings", async () => { + const extDir = join(agentDir, "extensions"); + mkdirSync(extDir, { recursive: true }); + const extPath = join(extDir, "my-extension.ts"); + writeFileSync(extPath, "export default function() {}"); + settingsManager.setExtensionPaths(["extensions/my-extension.ts"]); + + const result = await packageManager.resolve(); + expect(result.extensions.some((r) => r.path === extPath && r.enabled)).toBe(true); + }); + + it("should resolve skill paths from settings", async () => { + const skillDir = join(agentDir, "skills", "my-skill"); + mkdirSync(skillDir, { recursive: true }); + const skillFile = join(skillDir, "SKILL.md"); + writeFileSync( + skillFile, + `--- +name: test-skill +description: A test skill +--- +Content`, + ); + + settingsManager.setSkillPaths(["skills"]); + + const result = await packageManager.resolve(); + // Skills with SKILL.md are returned as file paths + expect(result.skills.some((r) => r.path === skillFile && r.enabled)).toBe(true); + }); + + it("should auto-discover root markdown skills from .pi skill dirs", async () => { + const skillFile = join(agentDir, "skills", "single-file.md"); + mkdirSync(join(agentDir, "skills"), { recursive: true }); + writeFileSync( + skillFile, + `--- +name: single-file +description: A root markdown skill +--- +Content`, + ); + + const result = await packageManager.resolve(); + expect(result.skills.some((r) => r.path === skillFile && r.enabled)).toBe(true); + }); + + it("should resolve project paths relative to .pi", async () => { + const extDir = join(tempDir, ".pi", "extensions"); + mkdirSync(extDir, { recursive: true }); + const extPath = join(extDir, "project-ext.ts"); + writeFileSync(extPath, "export default function() {}"); + + settingsManager.setProjectExtensionPaths(["extensions/project-ext.ts"]); + + const result = await packageManager.resolve(); + expect(result.extensions.some((r) => r.path === extPath && r.enabled)).toBe(true); + }); + + it("should auto-discover user prompts with overrides", async () => { + const promptsDir = join(agentDir, "prompts"); + mkdirSync(promptsDir, { recursive: true }); + const promptPath = join(promptsDir, "auto.md"); + writeFileSync(promptPath, "Auto prompt"); + + settingsManager.setPromptTemplatePaths(["!prompts/auto.md"]); + + const result = await packageManager.resolve(); + expect(result.prompts.some((r) => r.path === promptPath && !r.enabled)).toBe(true); + }); + + it("should resolve symlinked user and project resources once", async () => { + const previousHome = process.env.HOME; + process.env.HOME = tempDir; + + try { + const sharedDir = join(tempDir, "shared-resources"); + const sharedExtensionsDir = join(sharedDir, "extensions"); + const sharedSkillsDir = join(sharedDir, "skills"); + const sharedPromptsDir = join(sharedDir, "prompts"); + const sharedThemesDir = join(sharedDir, "themes"); + mkdirSync(sharedExtensionsDir, { recursive: true }); + mkdirSync(sharedSkillsDir, { recursive: true }); + mkdirSync(sharedPromptsDir, { recursive: true }); + mkdirSync(sharedThemesDir, { recursive: true }); + + writeFileSync(join(sharedExtensionsDir, "shared.ts"), "export default function() {}"); + mkdirSync(join(sharedSkillsDir, "shared-skill"), { recursive: true }); + writeFileSync( + join(sharedSkillsDir, "shared-skill", "SKILL.md"), + `--- +name: shared-skill +description: Shared skill +--- +Content`, + ); + writeFileSync(join(sharedPromptsDir, "shared.md"), "Shared prompt"); + writeFileSync(join(sharedThemesDir, "shared.json"), JSON.stringify({ name: "shared-theme" })); + + mkdirSync(join(agentDir), { recursive: true }); + mkdirSync(join(tempDir, ".pi"), { recursive: true }); + symlinkSync(sharedExtensionsDir, join(agentDir, "extensions"), "dir"); + symlinkSync(sharedSkillsDir, join(agentDir, "skills"), "dir"); + symlinkSync(sharedPromptsDir, join(agentDir, "prompts"), "dir"); + symlinkSync(sharedThemesDir, join(agentDir, "themes"), "dir"); + symlinkSync(sharedExtensionsDir, join(tempDir, ".pi", "extensions"), "dir"); + symlinkSync(sharedSkillsDir, join(tempDir, ".pi", "skills"), "dir"); + symlinkSync(sharedPromptsDir, join(tempDir, ".pi", "prompts"), "dir"); + symlinkSync(sharedThemesDir, join(tempDir, ".pi", "themes"), "dir"); + + const result = await packageManager.resolve(); + + expect({ + extensions: result.extensions.length, + skills: result.skills.length, + prompts: result.prompts.length, + themes: result.themes.length, + }).toEqual({ + extensions: 1, + skills: 1, + prompts: 1, + themes: 1, + }); + + // Project auto-discovered has higher precedence than user auto-discovered, + // so the surviving entry should be scoped to project. + expect(result.extensions[0].metadata.scope).toBe("project"); + expect(result.skills[0].metadata.scope).toBe("project"); + expect(result.prompts[0].metadata.scope).toBe("project"); + expect(result.themes[0].metadata.scope).toBe("project"); + } finally { + if (previousHome === undefined) { + delete process.env.HOME; + } else { + process.env.HOME = previousHome; + } + } + }); + + it("should auto-discover project prompts with overrides", async () => { + const promptsDir = join(tempDir, ".pi", "prompts"); + mkdirSync(promptsDir, { recursive: true }); + const promptPath = join(promptsDir, "is.md"); + writeFileSync(promptPath, "Is prompt"); + + settingsManager.setProjectPromptTemplatePaths(["!prompts/is.md"]); + + const result = await packageManager.resolve(); + expect(result.prompts.some((r) => r.path === promptPath && !r.enabled)).toBe(true); + }); + + it("should resolve directory with package.json pi.extensions in extensions setting", async () => { + // Create a package with pi.extensions in package.json + const pkgDir = join(tempDir, "my-extensions-pkg"); + mkdirSync(join(pkgDir, "extensions"), { recursive: true }); + writeFileSync( + join(pkgDir, "package.json"), + JSON.stringify({ + name: "my-extensions-pkg", + pi: { + extensions: ["./extensions/clip.ts", "./extensions/cost.ts"], + }, + }), + ); + writeFileSync(join(pkgDir, "extensions", "clip.ts"), "export default function() {}"); + writeFileSync(join(pkgDir, "extensions", "cost.ts"), "export default function() {}"); + writeFileSync(join(pkgDir, "extensions", "helper.ts"), "export const x = 1;"); // Not in manifest, shouldn't be loaded + + // Add the directory to extensions setting (not packages setting) + settingsManager.setExtensionPaths([pkgDir]); + + const result = await packageManager.resolve(); + + // Should find the extensions declared in package.json pi.extensions + expect(result.extensions.some((r) => r.path === join(pkgDir, "extensions", "clip.ts") && r.enabled)).toBe( + true, + ); + expect(result.extensions.some((r) => r.path === join(pkgDir, "extensions", "cost.ts") && r.enabled)).toBe( + true, + ); + + // Should NOT find helper.ts (not declared in manifest) + expect(result.extensions.some((r) => pathEndsWith(r.path, "helper.ts"))).toBe(false); + }); + }); + + describe("auto-discovered skill metadata", () => { + it("should use the agent dir as baseDir for user .pi/agent skills", async () => { + const skillPath = join(agentDir, "skills", "user-pi", "SKILL.md"); + mkdirSync(join(agentDir, "skills", "user-pi"), { recursive: true }); + writeFileSync(skillPath, "---\nname: user-pi\ndescription: user pi\n---\n"); + + const result = await packageManager.resolve(); + const skill = result.skills.find((r) => r.path === skillPath); + + expect(skill?.metadata.source).toBe("auto"); + expect(skill?.metadata.scope).toBe("user"); + expect(skill?.metadata.baseDir).toBe(agentDir); + }); + + it("should use the project .pi dir as baseDir for project .pi skills", async () => { + const projectBaseDir = join(tempDir, ".pi"); + const skillPath = join(projectBaseDir, "skills", "project-pi", "SKILL.md"); + mkdirSync(join(projectBaseDir, "skills", "project-pi"), { recursive: true }); + writeFileSync(skillPath, "---\nname: project-pi\ndescription: project pi\n---\n"); + + const result = await packageManager.resolve(); + const skill = result.skills.find((r) => r.path === skillPath); + + expect(skill?.metadata.source).toBe("auto"); + expect(skill?.metadata.scope).toBe("project"); + expect(skill?.metadata.baseDir).toBe(projectBaseDir); + }); + + it("should use ~/.agents as baseDir for user .agents skills", async () => { + const previousHome = process.env.HOME; + process.env.HOME = tempDir; + + try { + const agentsBaseDir = join(tempDir, ".agents"); + const skillPath = join(agentsBaseDir, "skills", "user-agents", "SKILL.md"); + mkdirSync(join(agentsBaseDir, "skills", "user-agents"), { recursive: true }); + writeFileSync(skillPath, "---\nname: user-agents\ndescription: user agents\n---\n"); + + const result = await packageManager.resolve(); + const skill = result.skills.find((r) => r.path === skillPath); + + expect(skill?.metadata.source).toBe("auto"); + expect(skill?.metadata.scope).toBe("user"); + expect(skill?.metadata.baseDir).toBe(agentsBaseDir); + } finally { + if (previousHome === undefined) { + delete process.env.HOME; + } else { + process.env.HOME = previousHome; + } + } + }); + + it("should use each project .agents dir as baseDir for project .agents skills", async () => { + const repoRoot = join(tempDir, "repo"); + const nestedCwd = join(repoRoot, "packages", "feature"); + mkdirSync(nestedCwd, { recursive: true }); + mkdirSync(join(repoRoot, ".git"), { recursive: true }); + + const repoAgentsBaseDir = join(repoRoot, ".agents"); + const repoSkill = join(repoAgentsBaseDir, "skills", "repo", "SKILL.md"); + mkdirSync(join(repoAgentsBaseDir, "skills", "repo"), { recursive: true }); + writeFileSync(repoSkill, "---\nname: repo\ndescription: repo\n---\n"); + + const packageAgentsBaseDir = join(repoRoot, "packages", ".agents"); + const packageSkill = join(packageAgentsBaseDir, "skills", "package", "SKILL.md"); + mkdirSync(join(packageAgentsBaseDir, "skills", "package"), { recursive: true }); + writeFileSync(packageSkill, "---\nname: package\ndescription: package\n---\n"); + + const pm = new DefaultPackageManager({ + cwd: nestedCwd, + agentDir, + settingsManager, + }); + + const result = await pm.resolve(); + const resolvedRepoSkill = result.skills.find((r) => r.path === repoSkill); + const resolvedPackageSkill = result.skills.find((r) => r.path === packageSkill); + + expect(resolvedRepoSkill?.metadata.source).toBe("auto"); + expect(resolvedRepoSkill?.metadata.scope).toBe("project"); + expect(resolvedRepoSkill?.metadata.baseDir).toBe(repoAgentsBaseDir); + expect(resolvedPackageSkill?.metadata.source).toBe("auto"); + expect(resolvedPackageSkill?.metadata.scope).toBe("project"); + expect(resolvedPackageSkill?.metadata.baseDir).toBe(packageAgentsBaseDir); + }); + }); + + describe(".agents/skills auto-discovery", () => { + it("should scan .agents/skills from cwd up to git repo root", async () => { + const repoRoot = join(tempDir, "repo"); + const nestedCwd = join(repoRoot, "packages", "feature"); + mkdirSync(nestedCwd, { recursive: true }); + mkdirSync(join(repoRoot, ".git"), { recursive: true }); + + const aboveRepoSkill = join(tempDir, ".agents", "skills", "above-repo", "SKILL.md"); + mkdirSync(join(tempDir, ".agents", "skills", "above-repo"), { recursive: true }); + writeFileSync(aboveRepoSkill, "---\nname: above-repo\ndescription: above\n---\n"); + + const repoRootSkill = join(repoRoot, ".agents", "skills", "repo-root", "SKILL.md"); + mkdirSync(join(repoRoot, ".agents", "skills", "repo-root"), { recursive: true }); + writeFileSync(repoRootSkill, "---\nname: repo-root\ndescription: repo\n---\n"); + + const nestedSkill = join(repoRoot, "packages", ".agents", "skills", "nested", "SKILL.md"); + mkdirSync(join(repoRoot, "packages", ".agents", "skills", "nested"), { recursive: true }); + writeFileSync(nestedSkill, "---\nname: nested\ndescription: nested\n---\n"); + + const pm = new DefaultPackageManager({ + cwd: nestedCwd, + agentDir, + settingsManager, + }); + + const result = await pm.resolve(); + expect(result.skills.some((r) => r.path === repoRootSkill && r.enabled)).toBe(true); + expect(result.skills.some((r) => r.path === nestedSkill && r.enabled)).toBe(true); + expect(result.skills.some((r) => r.path === aboveRepoSkill)).toBe(false); + }); + + it("should scan .agents/skills up to filesystem root when not in a git repo", async () => { + const nonRepoRoot = join(tempDir, "non-repo"); + const nestedCwd = join(nonRepoRoot, "a", "b"); + mkdirSync(nestedCwd, { recursive: true }); + + const rootSkill = join(nonRepoRoot, ".agents", "skills", "root", "SKILL.md"); + mkdirSync(join(nonRepoRoot, ".agents", "skills", "root"), { recursive: true }); + writeFileSync(rootSkill, "---\nname: root\ndescription: root\n---\n"); + + const middleSkill = join(nonRepoRoot, "a", ".agents", "skills", "middle", "SKILL.md"); + mkdirSync(join(nonRepoRoot, "a", ".agents", "skills", "middle"), { recursive: true }); + writeFileSync(middleSkill, "---\nname: middle\ndescription: middle\n---\n"); + + const pm = new DefaultPackageManager({ + cwd: nestedCwd, + agentDir, + settingsManager, + }); + + const result = await pm.resolve(); + expect(result.skills.some((r) => r.path === rootSkill && r.enabled)).toBe(true); + expect(result.skills.some((r) => r.path === middleSkill && r.enabled)).toBe(true); + }); + + it("should ignore root markdown files in .agents/skills but discover nested markdown skills", async () => { + const agentsSkillsDir = join(tempDir, ".agents", "skills"); + mkdirSync(join(agentsSkillsDir, "nested-skill"), { recursive: true }); + mkdirSync(join(agentsSkillsDir, "third-party"), { recursive: true }); + mkdirSync(join(agentsSkillsDir, "third-party", "vendor", "pack"), { recursive: true }); + const rootSkill = join(agentsSkillsDir, "root-file.md"); + const nestedSkill = join(agentsSkillsDir, "nested-skill", "SKILL.md"); + const nestedMarkdownSkill = join(agentsSkillsDir, "third-party", "child-skill.md"); + const deeplyNestedMarkdownSkill = join(agentsSkillsDir, "third-party", "vendor", "pack", "deep-skill.md"); + writeFileSync(rootSkill, "---\nname: root-file\ndescription: Root markdown file\n---\n"); + writeFileSync(nestedSkill, "---\nname: nested-skill\ndescription: Nested skill\n---\n"); + writeFileSync(nestedMarkdownSkill, "---\nname: child-skill\ndescription: Nested markdown skill\n---\n"); + writeFileSync(deeplyNestedMarkdownSkill, "---\nname: deep-skill\ndescription: Deep markdown skill\n---\n"); + + const pm = new DefaultPackageManager({ + cwd: join(tempDir, "work"), + agentDir, + settingsManager, + }); + mkdirSync(join(tempDir, "work"), { recursive: true }); + + const result = await pm.resolve(); + expect(result.skills.some((r) => r.path === rootSkill)).toBe(false); + expect(result.skills.some((r) => r.path === nestedSkill && r.enabled)).toBe(true); + expect(result.skills.some((r) => r.path === nestedMarkdownSkill && r.enabled)).toBe(true); + expect(result.skills.some((r) => r.path === deeplyNestedMarkdownSkill && r.enabled)).toBe(true); + }); + + it("should keep ~/.agents/skills user-scoped when cwd is under home in a non-git directory", async () => { + const previousHome = process.env.HOME; + process.env.HOME = tempDir; + + try { + const cwd = join(tempDir, "scratch", "nested"); + const localAgentDir = join(tempDir, ".pi", "agent"); + const localSettingsManager = SettingsManager.inMemory(); + mkdirSync(cwd, { recursive: true }); + mkdirSync(localAgentDir, { recursive: true }); + + const homeSkill = join(tempDir, ".agents", "skills", "home-skill", "SKILL.md"); + mkdirSync(join(tempDir, ".agents", "skills", "home-skill"), { recursive: true }); + writeFileSync(homeSkill, "---\nname: home-skill\ndescription: home\n---\n"); + + const pm = new DefaultPackageManager({ + cwd, + agentDir: localAgentDir, + settingsManager: localSettingsManager, + }); + + const result = await pm.resolve(); + const matchingSkills = result.skills.filter((r) => r.path === homeSkill); + expect(matchingSkills).toHaveLength(1); + expect(matchingSkills[0]?.enabled).toBe(true); + expect(matchingSkills[0]?.metadata.scope).toBe("user"); + expect(matchingSkills[0]?.metadata.source).toBe("auto"); + } finally { + if (previousHome === undefined) { + delete process.env.HOME; + } else { + process.env.HOME = previousHome; + } + } + }); + + it("should dedupe user skill entries when ~/.pi/agent/skills is a symlink to ~/.agents/skills", async () => { + const previousHome = process.env.HOME; + process.env.HOME = tempDir; + + try { + const agentSkillsDir = join(agentDir, "skills"); + const agentsSkillsDir = join(tempDir, ".agents", "skills"); + mkdirSync(agentsSkillsDir, { recursive: true }); + // Use junction on Windows to avoid EPERM when symlink privileges are unavailable. + const directoryLinkType = process.platform === "win32" ? "junction" : "dir"; + symlinkSync(agentsSkillsDir, agentSkillsDir, directoryLinkType); + + const skillPath = join(agentsSkillsDir, "foo", "SKILL.md"); + mkdirSync(join(agentsSkillsDir, "foo"), { recursive: true }); + writeFileSync(skillPath, "---\nname: foo\ndescription: foo\n---\n"); + + const result = await packageManager.resolve(); + const fooSkills = result.skills.filter((r) => pathEndsWith(r.path, "foo/SKILL.md")); + + expect(fooSkills).toHaveLength(1); + } finally { + if (previousHome === undefined) { + delete process.env.HOME; + } else { + process.env.HOME = previousHome; + } + } + }); + }); + + describe("ignore files", () => { + it("should respect .gitignore in skill directories", async () => { + const skillsDir = join(agentDir, "skills"); + mkdirSync(skillsDir, { recursive: true }); + writeFileSync(join(skillsDir, ".gitignore"), "venv\n__pycache__\n"); + + const goodSkillDir = join(skillsDir, "good-skill"); + mkdirSync(goodSkillDir, { recursive: true }); + writeFileSync(join(goodSkillDir, "SKILL.md"), "---\nname: good-skill\ndescription: Good\n---\nContent"); + + const ignoredSkillDir = join(skillsDir, "venv", "bad-skill"); + mkdirSync(ignoredSkillDir, { recursive: true }); + writeFileSync(join(ignoredSkillDir, "SKILL.md"), "---\nname: bad-skill\ndescription: Bad\n---\nContent"); + + settingsManager.setSkillPaths(["skills"]); + + const result = await packageManager.resolve(); + expect(result.skills.some((r) => r.path.includes("good-skill") && r.enabled)).toBe(true); + expect(result.skills.some((r) => r.path.includes("venv") && r.enabled)).toBe(false); + }); + + it("should not apply parent .gitignore to .pi auto-discovery", async () => { + writeFileSync(join(tempDir, ".gitignore"), ".pi\n"); + + const skillDir = join(tempDir, ".pi", "skills", "auto-skill"); + mkdirSync(skillDir, { recursive: true }); + const skillPath = join(skillDir, "SKILL.md"); + writeFileSync(skillPath, "---\nname: auto-skill\ndescription: Auto\n---\nContent"); + + const result = await packageManager.resolve(); + expect(result.skills.some((r) => r.path === skillPath && r.enabled)).toBe(true); + }); + }); + + describe("resolveExtensionSources", () => { + it("should resolve local paths", async () => { + const extPath = join(tempDir, "ext.ts"); + writeFileSync(extPath, "export default function() {}"); + + const result = await packageManager.resolveExtensionSources([extPath]); + expect(result.extensions.some((r) => r.path === extPath && r.enabled)).toBe(true); + }); + + it("should handle directories with pi manifest", async () => { + const pkgDir = join(tempDir, "my-package"); + mkdirSync(pkgDir, { recursive: true }); + writeFileSync( + join(pkgDir, "package.json"), + JSON.stringify({ + name: "my-package", + pi: { + extensions: ["./src/index.ts"], + skills: ["./skills"], + }, + }), + ); + mkdirSync(join(pkgDir, "src"), { recursive: true }); + writeFileSync(join(pkgDir, "src", "index.ts"), "export default function() {}"); + mkdirSync(join(pkgDir, "skills", "my-skill"), { recursive: true }); + writeFileSync( + join(pkgDir, "skills", "my-skill", "SKILL.md"), + "---\nname: my-skill\ndescription: Test\n---\nContent", + ); + + const result = await packageManager.resolveExtensionSources([pkgDir]); + expect(result.extensions.some((r) => r.path === join(pkgDir, "src", "index.ts") && r.enabled)).toBe(true); + // Skills with SKILL.md are returned as file paths + expect(result.skills.some((r) => r.path === join(pkgDir, "skills", "my-skill", "SKILL.md") && r.enabled)).toBe( + true, + ); + }); + + it("should keep pi manifest entries with leading tilde package-relative", async () => { + const pkgDir = join(tempDir, "tilde-manifest-package"); + const directExtensionPath = join(pkgDir, "~extensions", "main.ts"); + const slashExtensionPath = join(pkgDir, "~", "extensions", "alt.ts"); + const directSkillPath = join(pkgDir, "~skills", "direct-skill", "SKILL.md"); + const slashSkillPath = join(pkgDir, "~", "skills", "slash-skill", "SKILL.md"); + + mkdirSync(join(pkgDir, "~extensions"), { recursive: true }); + mkdirSync(join(pkgDir, "~", "extensions"), { recursive: true }); + mkdirSync(join(pkgDir, "~skills", "direct-skill"), { recursive: true }); + mkdirSync(join(pkgDir, "~", "skills", "slash-skill"), { recursive: true }); + writeFileSync(directExtensionPath, "export default function() {}"); + writeFileSync(slashExtensionPath, "export default function() {}"); + writeFileSync(directSkillPath, "---\nname: direct-skill\ndescription: Direct\n---\nContent"); + writeFileSync(slashSkillPath, "---\nname: slash-skill\ndescription: Slash\n---\nContent"); + writeFileSync( + join(pkgDir, "package.json"), + JSON.stringify({ + name: "tilde-manifest-package", + pi: { + extensions: ["~extensions/main.ts", "~/extensions/alt.ts"], + skills: ["~skills", "~/skills"], + }, + }), + ); + + const result = await packageManager.resolveExtensionSources([pkgDir]); + + expect(result.extensions.some((r) => r.path === directExtensionPath && r.enabled)).toBe(true); + expect(result.extensions.some((r) => r.path === slashExtensionPath && r.enabled)).toBe(true); + expect(result.skills.some((r) => r.path === directSkillPath && r.enabled)).toBe(true); + expect(result.skills.some((r) => r.path === slashSkillPath && r.enabled)).toBe(true); + }); + + it("should handle directories with auto-discovery layout", async () => { + const pkgDir = join(tempDir, "auto-pkg"); + mkdirSync(join(pkgDir, "extensions"), { recursive: true }); + mkdirSync(join(pkgDir, "themes"), { recursive: true }); + writeFileSync(join(pkgDir, "extensions", "main.ts"), "export default function() {}"); + writeFileSync(join(pkgDir, "themes", "dark.json"), "{}"); + + const result = await packageManager.resolveExtensionSources([pkgDir]); + expect(result.extensions.some((r) => pathEndsWith(r.path, "main.ts") && r.enabled)).toBe(true); + expect(result.themes.some((r) => pathEndsWith(r.path, "dark.json") && r.enabled)).toBe(true); + }); + + it("should stop recursing when a package skill directory contains SKILL.md", async () => { + const pkgDir = join(tempDir, "skill-root-pkg"); + mkdirSync(join(pkgDir, "skills", "root-skill", "nested-skill"), { recursive: true }); + const rootSkill = join(pkgDir, "skills", "root-skill", "SKILL.md"); + const nestedSkill = join(pkgDir, "skills", "root-skill", "nested-skill", "SKILL.md"); + writeFileSync(rootSkill, "---\nname: root-skill\ndescription: Root skill\n---\n"); + writeFileSync(nestedSkill, "---\nname: nested-skill\ndescription: Nested skill\n---\n"); + + const result = await packageManager.resolveExtensionSources([pkgDir]); + expect(result.skills.some((r) => r.path === rootSkill && r.enabled)).toBe(true); + expect(result.skills.some((r) => r.path === nestedSkill)).toBe(false); + }); + }); + + describe("progress callback", () => { + it("should emit progress events", async () => { + const events: ProgressEvent[] = []; + packageManager.setProgressCallback((event) => events.push(event)); + + const extPath = join(tempDir, "ext.ts"); + writeFileSync(extPath, "export default function() {}"); + + // Local paths don't trigger install progress, but we can verify the callback is set + await packageManager.resolveExtensionSources([extPath]); + + // For now just verify no errors - npm/git would trigger actual events + expect(events.length).toBe(0); + }); + }); + + describe("command spawning", () => { + it("should preserve argv entries containing spaces", () => { + const managerWithInternals = packageManager as unknown as { + runCommandSync(command: string, args: string[]): string; + }; + const valueWithSpace = "C:\\Users\\A B\\.pi\\npm"; + const output = managerWithInternals.runCommandSync(process.execPath, [ + "-e", + "console.log(process.argv[1])", + valueWithSpace, + ]); + + expect(output).toBe(valueWithSpace); + }); + }); + + describe("npmCommand", () => { + it("should use npmCommand argv for npm installs", async () => { + settingsManager = SettingsManager.inMemory({ + npmCommand: ["mise", "exec", "node@20", "--", "npm"], + }); + packageManager = new DefaultPackageManager({ + cwd: tempDir, + agentDir, + settingsManager, + }); + + const runCommandSpy = vi.spyOn(packageManager as any, "runCommand").mockResolvedValue(undefined); + + await packageManager.install("npm:@scope/pkg"); + + expect(runCommandSpy).toHaveBeenCalledWith( + "mise", + [ + "exec", + "node@20", + "--", + "npm", + "install", + "@scope/pkg", + "--prefix", + join(agentDir, "npm"), + "--legacy-peer-deps", + ], + undefined, + ); + }); + + it("should pass legacy peer deps when uninstalling npm packages", async () => { + mkdirSync(join(agentDir, "npm"), { recursive: true }); + const runCommandSpy = vi.spyOn(packageManager as any, "runCommand").mockResolvedValue(undefined); + + await packageManager.remove("npm:@scope/pkg"); + + expect(runCommandSpy).toHaveBeenCalledWith( + "npm", + ["uninstall", "@scope/pkg", "--prefix", join(agentDir, "npm"), "--legacy-peer-deps"], + undefined, + ); + }); + + it("should use bun --cwd for npm package installs", async () => { + settingsManager = SettingsManager.inMemory({ + npmCommand: ["mise", "exec", "bun@1", "--", "bun"], + }); + packageManager = new DefaultPackageManager({ + cwd: tempDir, + agentDir, + settingsManager, + }); + + const runCommandSpy = vi.spyOn(packageManager as any, "runCommand").mockResolvedValue(undefined); + + await packageManager.install("npm:@scope/pkg"); + + expect(runCommandSpy).toHaveBeenCalledWith( + "mise", + ["exec", "bun@1", "--", "bun", "install", "@scope/pkg", "--cwd", join(agentDir, "npm"), "--omit=peer"], + undefined, + ); + }); + + it("should install git package dependencies with --omit=dev", async () => { + const source = "git:github.com/user/repo"; + const targetDir = join(agentDir, "git", "github.com", "user", "repo"); + const runCommandSpy = vi + .spyOn(packageManager as any, "runCommand") + .mockImplementation(async (...callArgs: unknown[]) => { + const [command, args] = callArgs as [string, string[]]; + if (command === "git" && args[0] === "clone") { + mkdirSync(targetDir, { recursive: true }); + writeFileSync(join(targetDir, "package.json"), JSON.stringify({ name: "repo", version: "1.0.0" })); + } + }); + + await packageManager.install(source); + + expect(runCommandSpy).toHaveBeenCalledWith("npm", ["install", "--omit=dev"], { cwd: targetDir }); + }); + + it("should remove a newly created checkout when git clone fails", async () => { + const source = "git:github.com/user/repo"; + const targetDir = join(agentDir, "git", "github.com", "user", "repo"); + vi.spyOn(packageManager as any, "runCommand").mockImplementation(async (...callArgs: unknown[]) => { + const [command, args] = callArgs as [string, string[]]; + if (command === "git" && args[0] === "clone") { + mkdirSync(targetDir, { recursive: true }); + throw new Error("simulated git clone failure"); + } + }); + + await expect(packageManager.install(source)).rejects.toThrow("simulated git clone failure"); + + expect(existsSync(targetDir)).toBe(false); + }); + + it("should remove a newly cloned checkout when dependency installation fails", async () => { + const source = "git:github.com/user/repo"; + const targetDir = join(agentDir, "git", "github.com", "user", "repo"); + vi.spyOn(packageManager as any, "runCommand").mockImplementation(async (...callArgs: unknown[]) => { + const [command, args] = callArgs as [string, string[]]; + if (command === "git" && args[0] === "clone") { + mkdirSync(targetDir, { recursive: true }); + writeFileSync(join(targetDir, "package.json"), JSON.stringify({ name: "repo", version: "1.0.0" })); + return; + } + if (command === "npm") { + throw new Error("simulated dependency install failure"); + } + }); + + await expect(packageManager.install(source)).rejects.toThrow("simulated dependency install failure"); + + expect(existsSync(targetDir)).toBe(false); + }); + + it("should reconcile an existing git checkout to a pinned ref during install", async () => { + const source = "git:github.com/user/repo@v2"; + const targetDir = join(agentDir, "git", "github.com", "user", "repo"); + mkdirSync(targetDir, { recursive: true }); + writeFileSync(join(targetDir, "package.json"), JSON.stringify({ name: "repo", version: "1.0.0" })); + + const managerWithInternals = packageManager as unknown as PackageManagerInternals; + vi.spyOn(managerWithInternals, "runCommandCapture").mockImplementation(async (_command, args) => { + if (args[0] === "rev-parse" && args[1] === "HEAD") { + return "old-head"; + } + if (args[0] === "rev-parse" && args[1] === "FETCH_HEAD^{commit}") { + return "new-head"; + } + throw new Error(`Unexpected runCommandCapture args: ${args.join(" ")}`); + }); + const runCommandSpy = vi.spyOn(managerWithInternals, "runCommand").mockResolvedValue(undefined); + + await packageManager.install(source); + + expect(runCommandSpy).toHaveBeenCalledWith("git", ["fetch", "origin", "v2"], { cwd: targetDir }); + expect(runCommandSpy).toHaveBeenCalledWith("git", ["reset", "--hard", "FETCH_HEAD^{commit}"], { + cwd: targetDir, + }); + expect(runCommandSpy).toHaveBeenCalledWith("git", ["clean", "-fdx"], { cwd: targetDir }); + expect(runCommandSpy).toHaveBeenCalledWith("npm", ["install", "--omit=dev"], { cwd: targetDir }); + }); + + it("should reconcile an existing git checkout to its update target when installing without a ref", async () => { + const source = "git:github.com/user/repo"; + const targetDir = join(agentDir, "git", "github.com", "user", "repo"); + const fetchArgs = ["fetch", "--prune", "--no-tags", "origin", "+refs/heads/main:refs/remotes/origin/main"]; + mkdirSync(targetDir, { recursive: true }); + + const managerWithInternals = packageManager as unknown as PackageManagerInternals; + vi.spyOn(managerWithInternals, "getLocalGitUpdateTarget").mockResolvedValue({ + ref: "origin/HEAD", + head: "new-head", + fetchArgs, + }); + vi.spyOn(managerWithInternals, "runCommandCapture").mockImplementation(async (_command, args) => { + if (args[0] === "rev-parse" && args[1] === "HEAD") { + return "old-head"; + } + if (args[0] === "rev-parse" && args[1] === "origin/HEAD^{commit}") { + return "new-head"; + } + throw new Error(`Unexpected runCommandCapture args: ${args.join(" ")}`); + }); + const runCommandSpy = vi.spyOn(managerWithInternals, "runCommand").mockResolvedValue(undefined); + + await packageManager.install(source); + + expect(runCommandSpy).toHaveBeenCalledWith("git", fetchArgs, { cwd: targetDir }); + expect(runCommandSpy).toHaveBeenCalledWith("git", ["reset", "--hard", "origin/HEAD^{commit}"], { + cwd: targetDir, + }); + expect(runCommandSpy).toHaveBeenCalledWith("git", ["clean", "-fdx"], { cwd: targetDir }); + }); + + it("should use plain install for git package dependencies when npmCommand is configured", async () => { + settingsManager = SettingsManager.inMemory({ + npmCommand: ["pnpm"], + }); + packageManager = new DefaultPackageManager({ + cwd: tempDir, + agentDir, + settingsManager, + }); + + const source = "git:github.com/user/repo"; + const targetDir = join(agentDir, "git", "github.com", "user", "repo"); + const runCommandSpy = vi + .spyOn(packageManager as any, "runCommand") + .mockImplementation(async (...callArgs: unknown[]) => { + const [command, args] = callArgs as [string, string[]]; + if (command === "git" && args[0] === "clone") { + mkdirSync(targetDir, { recursive: true }); + writeFileSync(join(targetDir, "package.json"), JSON.stringify({ name: "repo", version: "1.0.0" })); + } + }); + + await packageManager.install(source); + + expect(runCommandSpy).toHaveBeenCalledWith("pnpm", ["install"], { cwd: targetDir }); + }); + + it("should update git package dependencies with --omit=dev", async () => { + const source = "git:github.com/user/repo"; + const targetDir = join(tempDir, ".pi", "git", "github.com", "user", "repo"); + mkdirSync(targetDir, { recursive: true }); + writeFileSync(join(targetDir, "package.json"), JSON.stringify({ name: "repo", version: "1.0.0" })); + settingsManager.setProjectPackages([source]); + + vi.spyOn(packageManager as any, "runCommandCapture").mockImplementation(async (...callArgs: unknown[]) => { + const [_command, args] = callArgs as [string, string[]]; + if (args[0] === "rev-parse" && args[1] === "--abbrev-ref" && args[2] === "@{upstream}") { + return "origin/main"; + } + if (args[0] === "rev-parse" && (args[1] === "@{upstream}" || args[1] === "@{upstream}^{commit}")) { + return "remote-head"; + } + if (args[0] === "rev-parse" && args[1] === "HEAD") { + return "local-head"; + } + throw new Error(`Unexpected runCommandCapture args: ${args.join(" ")}`); + }); + const runCommandSpy = vi.spyOn(packageManager as any, "runCommand").mockResolvedValue(undefined); + + await packageManager.update(source); + + expect(runCommandSpy).toHaveBeenCalledWith("npm", ["install", "--omit=dev"], { cwd: targetDir }); + }); + + it("should repair missing git package dependencies when the checkout is already current", async () => { + const source = "git:github.com/user/repo"; + const targetDir = join(agentDir, "git", "github.com", "user", "repo"); + const fetchArgs = ["fetch", "--prune", "--no-tags", "origin", "+refs/heads/main:refs/remotes/origin/main"]; + mkdirSync(targetDir, { recursive: true }); + writeFileSync( + join(targetDir, "package.json"), + JSON.stringify({ name: "repo", version: "1.0.0", dependencies: { dependency: "1.0.0" } }), + ); + settingsManager.setPackages([source]); + + const managerWithInternals = packageManager as unknown as PackageManagerInternals; + vi.spyOn(managerWithInternals, "getLocalGitUpdateTarget").mockResolvedValue({ + ref: "@{upstream}", + head: "current-head", + fetchArgs, + }); + vi.spyOn(managerWithInternals, "runCommandCapture").mockResolvedValue("current-head"); + const runCommandSpy = vi.spyOn(managerWithInternals, "runCommand").mockResolvedValue(undefined); + + await packageManager.update(source); + + expect(runCommandSpy).toHaveBeenCalledWith("npm", ["install", "--omit=dev"], { cwd: targetDir }); + expect(runCommandSpy).not.toHaveBeenCalledWith("git", ["clean", "-fdx"], { cwd: targetDir }); + }); + + it("should repair deleted git package dependencies when cleaning fails", async () => { + const source = "git:github.com/user/repo"; + const targetDir = join(agentDir, "git", "github.com", "user", "repo"); + const fetchArgs = ["fetch", "--prune", "--no-tags", "origin", "+refs/heads/main:refs/remotes/origin/main"]; + mkdirSync(targetDir, { recursive: true }); + writeFileSync( + join(targetDir, "package.json"), + JSON.stringify({ name: "repo", version: "1.0.0", dependencies: { dependency: "1.0.0" } }), + ); + settingsManager.setPackages([source]); + + const managerWithInternals = packageManager as unknown as PackageManagerInternals; + vi.spyOn(managerWithInternals, "getLocalGitUpdateTarget").mockResolvedValue({ + ref: "@{upstream}", + head: "new-head", + fetchArgs, + }); + vi.spyOn(managerWithInternals, "runCommandCapture").mockImplementation(async (_command, args) => + args[1] === "HEAD" ? "old-head" : "new-head", + ); + const runCommandSpy = vi + .spyOn(managerWithInternals, "runCommand") + .mockImplementation(async (_command, args) => { + if (args[0] === "clean") throw new Error("simulated clean failure"); + }); + + await expect(packageManager.update(source)).rejects.toThrow("simulated clean failure"); + + expect(runCommandSpy).toHaveBeenCalledWith("npm", ["install", "--omit=dev"], { cwd: targetDir }); + }); + + it("should use plain install through npmCommand argv when updating git package dependencies", async () => { + settingsManager = SettingsManager.inMemory({ + npmCommand: ["mise", "exec", "node@20", "--", "pnpm"], + }); + packageManager = new DefaultPackageManager({ + cwd: tempDir, + agentDir, + settingsManager, + }); + + const source = "git:github.com/user/repo"; + const targetDir = join(tempDir, ".pi", "git", "github.com", "user", "repo"); + mkdirSync(targetDir, { recursive: true }); + writeFileSync(join(targetDir, "package.json"), JSON.stringify({ name: "repo", version: "1.0.0" })); + settingsManager.setProjectPackages([source]); + + vi.spyOn(packageManager as any, "runCommandCapture").mockImplementation(async (...callArgs: unknown[]) => { + const [_command, args] = callArgs as [string, string[]]; + if (args[0] === "rev-parse" && args[1] === "--abbrev-ref" && args[2] === "@{upstream}") { + return "origin/main"; + } + if (args[0] === "rev-parse" && (args[1] === "@{upstream}" || args[1] === "@{upstream}^{commit}")) { + return "remote-head"; + } + if (args[0] === "rev-parse" && args[1] === "HEAD") { + return "local-head"; + } + throw new Error(`Unexpected runCommandCapture args: ${args.join(" ")}`); + }); + const runCommandSpy = vi.spyOn(packageManager as any, "runCommand").mockResolvedValue(undefined); + + await packageManager.update(source); + + expect(runCommandSpy).toHaveBeenCalledWith("mise", ["exec", "node@20", "--", "pnpm", "install"], { + cwd: targetDir, + }); + }); + + it("should use npmCommand argv for npm root lookup and invalidate cached root when npmCommand changes", () => { + settingsManager = SettingsManager.inMemory({ + npmCommand: ["mise", "exec", "node@20", "--", "npm"], + }); + packageManager = new DefaultPackageManager({ + cwd: tempDir, + agentDir, + settingsManager, + }); + + const root20 = join(tempDir, "node20", "lib", "node_modules"); + const root22 = join(tempDir, "node22", "lib", "node_modules"); + mkdirSync(join(root20, "@scope", "pkg"), { recursive: true }); + + const runCommandSyncSpy = vi + .spyOn(packageManager as any, "runCommandSync") + .mockImplementation((...callArgs: unknown[]) => { + const [command, args] = callArgs as [string, string[]]; + if (command !== "mise") { + throw new Error(`unexpected command ${command}`); + } + if (args[1] === "node@20") { + return root20; + } + if (args[1] === "node@22") { + return root22; + } + throw new Error(`unexpected args ${args.join(" ")}`); + }); + + expect(packageManager.getInstalledPath("npm:@scope/pkg", "user")).toBe(join(root20, "@scope", "pkg")); + expect(runCommandSyncSpy).toHaveBeenNthCalledWith(1, "mise", ["exec", "node@20", "--", "npm", "root", "-g"]); + + settingsManager.setNpmCommand(["mise", "exec", "node@22", "--", "npm"]); + + expect(packageManager.getInstalledPath("npm:@scope/pkg", "user")).toBeUndefined(); + expect(runCommandSyncSpy).toHaveBeenNthCalledWith(2, "mise", ["exec", "node@22", "--", "npm", "root", "-g"]); + }); + + it("should install user npm packages into the pi-managed npm root", async () => { + settingsManager = SettingsManager.inMemory({ + npmCommand: ["pnpm"], + packages: ["npm:pnpm-pkg"], + }); + packageManager = new DefaultPackageManager({ + cwd: tempDir, + agentDir, + settingsManager, + }); + + const packagePath = join(agentDir, "npm", "node_modules", "pnpm-pkg"); + vi.spyOn(packageManager as any, "runCommandSync").mockImplementation(() => { + throw new Error("legacy lookup unavailable"); + }); + const runCommandSpy = vi + .spyOn(packageManager as any, "runCommand") + .mockImplementation(async (...callArgs: unknown[]) => { + const [command, args] = callArgs as [string, string[]]; + expect(command).toBe("pnpm"); + expect(args).toEqual([ + "install", + "pnpm-pkg", + "--prefix", + join(agentDir, "npm"), + "--config.auto-install-peers=false", + "--config.strict-peer-dependencies=false", + "--config.strict-dep-builds=false", + ]); + mkdirSync(join(packagePath, "extensions"), { recursive: true }); + writeFileSync(join(packagePath, "package.json"), JSON.stringify({ name: "pnpm-pkg", version: "1.0.0" })); + writeFileSync(join(packagePath, "extensions", "index.ts"), "export default function() {};"); + }); + + const first = await packageManager.resolve(); + const second = await packageManager.resolve(); + + expect(first.extensions.some((r) => r.path === join(packagePath, "extensions", "index.ts") && r.enabled)).toBe( + true, + ); + expect( + second.extensions.some((r) => r.path === join(packagePath, "extensions", "index.ts") && r.enabled), + ).toBe(true); + expect(runCommandSpy).toHaveBeenCalledTimes(1); + expect(packageManager.getInstalledPath("npm:pnpm-pkg", "user")).toBe(packagePath); + }); + + it("should load legacy pnpm global package paths from pnpm list output", async () => { + settingsManager = SettingsManager.inMemory({ + npmCommand: ["pnpm"], + packages: ["npm:pnpm-pkg"], + }); + packageManager = new DefaultPackageManager({ + cwd: tempDir, + agentDir, + settingsManager, + }); + + const pnpmRoot = join(tempDir, "pnpm", "global", "v11"); + const packagePath = join(pnpmRoot, "20-hash", "node_modules", "pnpm-pkg"); + mkdirSync(join(packagePath, "extensions"), { recursive: true }); + writeFileSync(join(packagePath, "package.json"), JSON.stringify({ name: "pnpm-pkg", version: "1.0.0" })); + writeFileSync(join(packagePath, "extensions", "index.ts"), "export default function() {};"); + + vi.spyOn(packageManager as any, "runCommandSync").mockImplementation((...callArgs: unknown[]) => { + const [command, args] = callArgs as [string, string[]]; + if (command !== "pnpm") { + throw new Error(`unexpected command ${command}`); + } + if (args.join(" ") === "list -g --depth 0 --json") { + return JSON.stringify([ + { + path: pnpmRoot, + dependencies: { "pnpm-pkg": { version: "1.0.0", path: packagePath } }, + }, + ]); + } + throw new Error(`unexpected args ${args.join(" ")}`); + }); + const runCommandSpy = vi.spyOn(packageManager as any, "runCommand").mockResolvedValue(undefined); + + const result = await packageManager.resolve(); + + expect( + result.extensions.some((r) => r.path === join(packagePath, "extensions", "index.ts") && r.enabled), + ).toBe(true); + expect(runCommandSpy).not.toHaveBeenCalled(); + expect(packageManager.getInstalledPath("npm:pnpm-pkg", "user")).toBe(packagePath); + }); + + it("should resolve wrapped pnpm global package paths from pnpm list output", () => { + settingsManager = SettingsManager.inMemory({ + npmCommand: ["mise", "exec", "node@20", "--", "pnpm"], + }); + packageManager = new DefaultPackageManager({ + cwd: tempDir, + agentDir, + settingsManager, + }); + + const pnpmRoot = join(tempDir, "pnpm", "global", "v11"); + const packagePath = join(pnpmRoot, "20-hash", "node_modules", "pnpm-pkg"); + mkdirSync(packagePath, { recursive: true }); + + vi.spyOn(packageManager as any, "runCommandSync").mockImplementation((...callArgs: unknown[]) => { + const [command, args] = callArgs as [string, string[]]; + expect(command).toBe("mise"); + if (args.join(" ") === "exec node@20 -- pnpm list -g --depth 0 --json") { + return JSON.stringify([{ path: pnpmRoot, dependencies: { "pnpm-pkg": { path: packagePath } } }]); + } + throw new Error(`unexpected args ${args.join(" ")}`); + }); + + expect(packageManager.getInstalledPath("npm:pnpm-pkg", "user")).toBe(packagePath); + }); + + it("should ignore malformed legacy pnpm global package lists", () => { + settingsManager = SettingsManager.inMemory({ + npmCommand: ["pnpm"], + }); + packageManager = new DefaultPackageManager({ + cwd: tempDir, + agentDir, + settingsManager, + }); + + vi.spyOn(packageManager as any, "runCommandSync").mockReturnValue("not json"); + + expect(packageManager.getInstalledPath("npm:pnpm-pkg", "user")).toBeUndefined(); + }); + }); + + describe("source parsing", () => { + it("should emit progress events on install attempt", async () => { + const events: ProgressEvent[] = []; + packageManager.setProgressCallback((event) => events.push(event)); + vi.spyOn(packageManager as unknown as PackageManagerInternals, "runCommand").mockRejectedValue( + new Error("simulated npm install failure"), + ); + + await expect(packageManager.install("npm:nonexistent-package@1.0.0")).rejects.toThrow( + "simulated npm install failure", + ); + + expect(events.some((e) => e.type === "start" && e.action === "install")).toBe(true); + expect(events.some((e) => e.type === "error")).toBe(true); + }); + + it("should recognize github URLs without git: prefix", async () => { + const events: ProgressEvent[] = []; + packageManager.setProgressCallback((event) => events.push(event)); + const runCommand = vi + .spyOn(packageManager as unknown as PackageManagerInternals, "runCommand") + .mockRejectedValue(new Error("simulated git clone failure")); + const source = "https://github.com/nonexistent/repo"; + + await expect(packageManager.install(source)).rejects.toThrow("simulated git clone failure"); + + expect(runCommand).toHaveBeenCalledWith("git", ["clone", source, expect.any(String)]); + expect(events.some((e) => e.type === "start" && e.action === "install")).toBe(true); + }); + + it("should parse package source types from docs examples", () => { + const parseNpm = (source: string) => { + const parsed = (packageManager as any).parseSource(source); + if (parsed.type !== "npm") { + throw new Error(`Expected npm source: ${source}`); + } + return parsed; + }; + + expect(parseNpm("npm:@scope/pkg@1.2.3").pinned).toBe(true); + expect(parseNpm("npm:@scope/pkg@^1.2.3").pinned).toBe(false); + expect(parseNpm("npm:pkg").pinned).toBe(false); + + expect((packageManager as any).parseSource("git:github.com/user/repo@v1").type).toBe("git"); + expect((packageManager as any).parseSource("https://github.com/user/repo@v1").type).toBe("git"); + expect((packageManager as any).parseSource("git:git@github.com:user/repo@v1").type).toBe("git"); + expect((packageManager as any).parseSource("ssh://git@github.com/user/repo@v1").type).toBe("git"); + + expect((packageManager as any).parseSource("/absolute/path/to/package").type).toBe("local"); + expect((packageManager as any).parseSource("./relative/path/to/package").type).toBe("local"); + expect((packageManager as any).parseSource("../relative/path/to/package").type).toBe("local"); + }); + + it("should never parse dot-relative paths as git", () => { + const dotSlash = (packageManager as any).parseSource("./packages/agent-timers"); + expect(dotSlash.type).toBe("local"); + expect(dotSlash.path).toBe("./packages/agent-timers"); + + const dotDotSlash = (packageManager as any).parseSource("../packages/agent-timers"); + expect(dotDotSlash.type).toBe("local"); + expect(dotDotSlash.path).toBe("../packages/agent-timers"); + }); + }); + + describe("git install paths", () => { + it("should reject paths outside git install roots", () => { + const managerWithInternals = packageManager as unknown as PackageManagerInternals; + const traversalSource = { + type: "git" as const, + repo: "git@evil.example:../../victim/repo", + host: "evil.example", + path: "../../victim/repo", + pinned: false, + }; + + for (const scope of ["user", "project", "temporary"] as const) { + expect(() => managerWithInternals.getGitInstallPath(traversalSource, scope)).toThrow( + "outside package install root", + ); + } + }); + }); + + describe("temporary install paths", () => { + it("should place temporary npm packages under the agent temp extension folder", () => { + const managerWithInternals = packageManager as unknown as PackageManagerInternals; + const source = managerWithInternals.parseSource("npm:left-pad"); + if (source.type !== "npm") { + throw new Error("Expected npm source"); + } + + const installPath = managerWithInternals.getNpmInstallPath(source, "temporary"); + const tempRoot = join(agentDir, "tmp", "extensions"); + + expect(pathEndsWith(installPath, "node_modules/left-pad")).toBe(true); + expect(relative(tempRoot, installPath).startsWith("..")).toBe(false); + expect(installPath.startsWith(join(tmpdir(), "pi-extensions"))).toBe(false); + if (process.platform !== "win32") { + expect(statSync(tempRoot).mode & 0o777).toBe(0o700); + } + }); + }); + + describe("settings source normalization", () => { + it("should store global local packages relative to agent settings base", () => { + const pkgDir = join(tempDir, "packages", "local-global-pkg"); + mkdirSync(join(pkgDir, "extensions"), { recursive: true }); + writeFileSync(join(pkgDir, "extensions", "index.ts"), "export default function() {}"); + + const added = packageManager.addSourceToSettings("./packages/local-global-pkg"); + expect(added).toBe(true); + + const settings = settingsManager.getGlobalSettings(); + const rel = relative(agentDir, pkgDir); + const expected = rel.startsWith(".") ? rel : `./${rel}`; + expect(settings.packages?.[0]).toBe(expected); + }); + + it("should store project local packages relative to .pi settings base", () => { + const projectPkgDir = join(tempDir, "project-local-pkg"); + mkdirSync(join(projectPkgDir, "extensions"), { recursive: true }); + writeFileSync(join(projectPkgDir, "extensions", "index.ts"), "export default function() {}"); + + const added = packageManager.addSourceToSettings("./project-local-pkg", { local: true }); + expect(added).toBe(true); + + const settings = settingsManager.getProjectSettings(); + const rel = relative(join(tempDir, ".pi"), projectPkgDir); + const expected = rel.startsWith(".") ? rel : `./${rel}`; + expect(settings.packages?.[0]).toBe(expected); + }); + + it("should remove local package entries using equivalent path forms", () => { + const pkgDir = join(tempDir, "remove-local-pkg"); + mkdirSync(join(pkgDir, "extensions"), { recursive: true }); + writeFileSync(join(pkgDir, "extensions", "index.ts"), "export default function() {}"); + + packageManager.addSourceToSettings("./remove-local-pkg"); + const removed = packageManager.removeSourceFromSettings(`${pkgDir}/`); + expect(removed).toBe(true); + expect(settingsManager.getGlobalSettings().packages ?? []).toHaveLength(0); + }); + + it("should return false when adding the same git source with the same ref", () => { + const first = packageManager.addSourceToSettings("git:github.com/user/repo@v1"); + expect(first).toBe(true); + + const second = packageManager.addSourceToSettings("git:github.com/user/repo@v1"); + expect(second).toBe(false); + expect(settingsManager.getGlobalSettings().packages).toEqual(["git:github.com/user/repo@v1"]); + }); + + it("should update the ref when adding the same git source with a different ref", () => { + packageManager.addSourceToSettings("git:github.com/user/repo@v1"); + + const updated = packageManager.addSourceToSettings("git:github.com/user/repo@v2"); + expect(updated).toBe(true); + expect(settingsManager.getGlobalSettings().packages).toEqual(["git:github.com/user/repo@v2"]); + }); + + it("should preserve package filters when replacing a package source ref", () => { + settingsManager.setPackages([ + { + source: "git:github.com/user/repo@v1", + extensions: ["extensions/main.ts"], + skills: [], + prompts: ["prompts/review.md"], + themes: ["themes/dark.json"], + }, + ]); + + const updated = packageManager.addSourceToSettings("git:github.com/user/repo@v2"); + expect(updated).toBe(true); + expect(settingsManager.getGlobalSettings().packages).toEqual([ + { + source: "git:github.com/user/repo@v2", + extensions: ["extensions/main.ts"], + skills: [], + prompts: ["prompts/review.md"], + themes: ["themes/dark.json"], + }, + ]); + }); + }); + + describe("HTTPS git URL parsing (old behavior)", () => { + it("should parse HTTPS GitHub URLs correctly", async () => { + const parsed = (packageManager as any).parseSource("https://github.com/user/repo"); + expect(parsed.type).toBe("git"); + expect(parsed.host).toBe("github.com"); + expect(parsed.path).toBe("user/repo"); + expect(parsed.pinned).toBe(false); + }); + + it("should parse HTTPS URLs with git: prefix", async () => { + const parsed = (packageManager as any).parseSource("git:https://github.com/user/repo"); + expect(parsed.type).toBe("git"); + expect(parsed.host).toBe("github.com"); + expect(parsed.path).toBe("user/repo"); + }); + + it("should parse HTTPS URLs with ref", async () => { + const parsed = (packageManager as any).parseSource("https://github.com/user/repo@v1.2.3"); + expect(parsed.type).toBe("git"); + expect(parsed.host).toBe("github.com"); + expect(parsed.path).toBe("user/repo"); + expect(parsed.ref).toBe("v1.2.3"); + expect(parsed.pinned).toBe(true); + }); + + it("should parse host/path shorthand only with git: prefix", async () => { + const parsed = (packageManager as any).parseSource("git:github.com/user/repo"); + expect(parsed.type).toBe("git"); + expect(parsed.host).toBe("github.com"); + expect(parsed.path).toBe("user/repo"); + }); + + it("should treat host/path shorthand as local without git: prefix", async () => { + const parsed = (packageManager as any).parseSource("github.com/user/repo"); + expect(parsed.type).toBe("local"); + }); + + it("should parse HTTPS URLs with .git suffix", async () => { + const parsed = (packageManager as any).parseSource("https://github.com/user/repo.git"); + expect(parsed.type).toBe("git"); + expect(parsed.host).toBe("github.com"); + expect(parsed.path).toBe("user/repo"); + }); + + it("should parse GitLab HTTPS URLs", async () => { + const parsed = (packageManager as any).parseSource("https://gitlab.com/user/repo"); + expect(parsed.type).toBe("git"); + expect(parsed.host).toBe("gitlab.com"); + expect(parsed.path).toBe("user/repo"); + }); + + it("should parse Bitbucket HTTPS URLs", async () => { + const parsed = (packageManager as any).parseSource("https://bitbucket.org/user/repo"); + expect(parsed.type).toBe("git"); + expect(parsed.host).toBe("bitbucket.org"); + expect(parsed.path).toBe("user/repo"); + }); + + it("should parse Codeberg HTTPS URLs", async () => { + const parsed = (packageManager as any).parseSource("https://codeberg.org/user/repo"); + expect(parsed.type).toBe("git"); + expect(parsed.host).toBe("codeberg.org"); + expect(parsed.path).toBe("user/repo"); + }); + + it("should generate correct package identity for protocol and git:-prefixed URLs", async () => { + const identity1 = (packageManager as any).getPackageIdentity("https://github.com/user/repo"); + const identity2 = (packageManager as any).getPackageIdentity("https://github.com/user/repo@v1.0.0"); + const identity3 = (packageManager as any).getPackageIdentity("git:github.com/user/repo"); + const identity4 = (packageManager as any).getPackageIdentity("https://github.com/user/repo.git"); + + // All should have the same identity (normalized) + expect(identity1).toBe("git:github.com/user/repo"); + expect(identity2).toBe("git:github.com/user/repo"); + expect(identity3).toBe("git:github.com/user/repo"); + expect(identity4).toBe("git:github.com/user/repo"); + }); + + it("should deduplicate git URLs with different supported formats", async () => { + const pkgDir = join(tempDir, "https-dedup-pkg"); + mkdirSync(join(pkgDir, "extensions"), { recursive: true }); + writeFileSync(join(pkgDir, "extensions", "test.ts"), "export default function() {}"); + + // Mock the package as if it were cloned from different URL formats + // In reality, these would all point to the same local dir after install + settingsManager.setPackages([ + "https://github.com/user/repo", + "git:github.com/user/repo", + "https://github.com/user/repo.git", + ]); + + // Since these URLs don't actually exist and we can't clone them, + // we verify they produce the same identity + const id1 = (packageManager as any).getPackageIdentity("https://github.com/user/repo"); + const id2 = (packageManager as any).getPackageIdentity("git:github.com/user/repo"); + const id3 = (packageManager as any).getPackageIdentity("https://github.com/user/repo.git"); + + expect(id1).toBe(id2); + expect(id2).toBe(id3); + }); + + it("should handle HTTPS URLs with refs in resolve", async () => { + // This tests that the ref is properly extracted and stored + const parsed = (packageManager as any).parseSource("https://github.com/user/repo@main"); + expect(parsed.ref).toBe("main"); + expect(parsed.pinned).toBe(true); + + const parsed2 = (packageManager as any).parseSource("https://github.com/user/repo@feature/branch"); + expect(parsed2.ref).toBe("feature/branch"); + }); + }); + + describe("pattern filtering in top-level arrays", () => { + it("should exclude extensions with ! pattern", async () => { + const extDir = join(agentDir, "extensions"); + mkdirSync(extDir, { recursive: true }); + writeFileSync(join(extDir, "keep.ts"), "export default function() {}"); + writeFileSync(join(extDir, "remove.ts"), "export default function() {}"); + + settingsManager.setExtensionPaths(["extensions", "!**/remove.ts"]); + + const result = await packageManager.resolve(); + expect(result.extensions.some((r) => isEnabled(r, "keep.ts"))).toBe(true); + expect(result.extensions.some((r) => isDisabled(r, "remove.ts"))).toBe(true); + }); + + it("should filter themes with glob patterns", async () => { + const themesDir = join(agentDir, "themes"); + mkdirSync(themesDir, { recursive: true }); + writeFileSync(join(themesDir, "dark.json"), "{}"); + writeFileSync(join(themesDir, "light.json"), "{}"); + writeFileSync(join(themesDir, "funky.json"), "{}"); + + settingsManager.setThemePaths(["themes", "!funky.json"]); + + const result = await packageManager.resolve(); + expect(result.themes.some((r) => isEnabled(r, "dark.json"))).toBe(true); + expect(result.themes.some((r) => isEnabled(r, "light.json"))).toBe(true); + expect(result.themes.some((r) => isDisabled(r, "funky.json"))).toBe(true); + }); + + it("should filter prompts with exclusion pattern", async () => { + const promptsDir = join(agentDir, "prompts"); + mkdirSync(promptsDir, { recursive: true }); + writeFileSync(join(promptsDir, "review.md"), "Review code"); + writeFileSync(join(promptsDir, "explain.md"), "Explain code"); + + settingsManager.setPromptTemplatePaths(["prompts", "!explain.md"]); + + const result = await packageManager.resolve(); + expect(result.prompts.some((r) => isEnabled(r, "review.md"))).toBe(true); + expect(result.prompts.some((r) => isDisabled(r, "explain.md"))).toBe(true); + }); + + it("should filter skills with exclusion pattern", async () => { + const skillsDir = join(agentDir, "skills"); + mkdirSync(join(skillsDir, "good-skill"), { recursive: true }); + mkdirSync(join(skillsDir, "bad-skill"), { recursive: true }); + writeFileSync( + join(skillsDir, "good-skill", "SKILL.md"), + "---\nname: good-skill\ndescription: Good\n---\nContent", + ); + writeFileSync( + join(skillsDir, "bad-skill", "SKILL.md"), + "---\nname: bad-skill\ndescription: Bad\n---\nContent", + ); + + settingsManager.setSkillPaths(["skills", "!**/bad-skill"]); + + const result = await packageManager.resolve(); + expect(result.skills.some((r) => isEnabled(r, "good-skill", "includes"))).toBe(true); + expect(result.skills.some((r) => isDisabled(r, "bad-skill", "includes"))).toBe(true); + }); + + it("should work without patterns (backward compatible)", async () => { + const extDir = join(agentDir, "extensions"); + mkdirSync(extDir, { recursive: true }); + const extPath = join(extDir, "my-ext.ts"); + writeFileSync(extPath, "export default function() {}"); + + settingsManager.setExtensionPaths(["extensions/my-ext.ts"]); + + const result = await packageManager.resolve(); + expect(result.extensions.some((r) => r.path === extPath && r.enabled)).toBe(true); + }); + }); + + describe("pattern filtering in pi manifest", () => { + it("should support glob patterns in manifest extensions", async () => { + const pkgDir = join(tempDir, "manifest-pkg"); + mkdirSync(join(pkgDir, "extensions"), { recursive: true }); + mkdirSync(join(pkgDir, "node_modules/dep/extensions"), { recursive: true }); + writeFileSync(join(pkgDir, "extensions", "local.ts"), "export default function() {}"); + writeFileSync(join(pkgDir, "node_modules/dep/extensions", "remote.ts"), "export default function() {}"); + writeFileSync(join(pkgDir, "node_modules/dep/extensions", "skip.ts"), "export default function() {}"); + writeFileSync( + join(pkgDir, "package.json"), + JSON.stringify({ + name: "manifest-pkg", + pi: { + extensions: ["extensions", "node_modules/dep/extensions", "!**/skip.ts"], + }, + }), + ); + + const result = await packageManager.resolveExtensionSources([pkgDir]); + expect(result.extensions.some((r) => isEnabled(r, "local.ts"))).toBe(true); + expect(result.extensions.some((r) => isEnabled(r, "remote.ts"))).toBe(true); + expect(result.extensions.some((r) => pathEndsWith(r.path, "skip.ts"))).toBe(false); + }); + + it("should support glob patterns in manifest skills", async () => { + const pkgDir = join(tempDir, "skill-manifest-pkg"); + mkdirSync(join(pkgDir, "skills/good-skill"), { recursive: true }); + mkdirSync(join(pkgDir, "skills/bad-skill"), { recursive: true }); + writeFileSync( + join(pkgDir, "skills/good-skill", "SKILL.md"), + "---\nname: good-skill\ndescription: Good\n---\nContent", + ); + writeFileSync( + join(pkgDir, "skills/bad-skill", "SKILL.md"), + "---\nname: bad-skill\ndescription: Bad\n---\nContent", + ); + writeFileSync( + join(pkgDir, "package.json"), + JSON.stringify({ + name: "skill-manifest-pkg", + pi: { + skills: ["skills", "!**/bad-skill"], + }, + }), + ); + + const result = await packageManager.resolveExtensionSources([pkgDir]); + expect(result.skills.some((r) => isEnabled(r, "good-skill", "includes"))).toBe(true); + expect(result.skills.some((r) => r.path.includes("bad-skill"))).toBe(false); + }); + + it("should expand positive glob manifest entries before collecting skills", async () => { + const pkgDir = join(tempDir, "skill-manifest-glob-pkg"); + mkdirSync(join(pkgDir, "plugins/pdf-to-markdown/skills/pdf-to-markdown"), { recursive: true }); + mkdirSync(join(pkgDir, "plugins/nutrient-dws/skills/document-processor-api"), { recursive: true }); + writeFileSync( + join(pkgDir, "plugins/pdf-to-markdown/skills/pdf-to-markdown", "SKILL.md"), + "---\nname: pdf-to-markdown\ndescription: PDF to Markdown\n---\nContent", + ); + writeFileSync( + join(pkgDir, "plugins/nutrient-dws/skills/document-processor-api", "SKILL.md"), + "---\nname: document-processor-api\ndescription: DWS\n---\nContent", + ); + writeFileSync( + join(pkgDir, "package.json"), + JSON.stringify({ + name: "skill-manifest-glob-pkg", + pi: { + skills: ["./plugins/*/skills"], + }, + }), + ); + + const result = await packageManager.resolveExtensionSources([pkgDir]); + expect(result.skills.some((r) => isEnabled(r, "pdf-to-markdown", "includes"))).toBe(true); + expect(result.skills.some((r) => isEnabled(r, "document-processor-api", "includes"))).toBe(true); + }); + + it("should sort manifest glob matches and use exact entries for dot paths and symlink traversal", async () => { + const pkgDir = join(tempDir, "manifest-glob-semantics-pkg"); + const extensionFilesDir = join(pkgDir, "extension-files"); + const extensionGroupDir = join(pkgDir, "extension-groups", "group"); + const linkedPluginSource = join(pkgDir, "linked-plugin-source"); + mkdirSync(join(extensionFilesDir, "nested"), { recursive: true }); + mkdirSync(extensionGroupDir, { recursive: true }); + mkdirSync(join(pkgDir, "plugins", "local", "skills", "local-skill"), { recursive: true }); + mkdirSync(join(linkedPluginSource, "skills", "linked-skill"), { recursive: true }); + writeFileSync(join(extensionFilesDir, "z.ts"), "export default function() {}"); + writeFileSync(join(extensionFilesDir, "a.ts"), "export default function() {}"); + writeFileSync(join(extensionFilesDir, ".ignored.ts"), "export default function() {}"); + writeFileSync(join(extensionFilesDir, "nested", ".hidden.ts"), "export default function() {}"); + writeFileSync(join(extensionGroupDir, "index.ts"), "export default function() {}"); + writeFileSync( + join(pkgDir, "plugins", "local", "skills", "local-skill", "SKILL.md"), + "---\nname: local-skill\ndescription: Local\n---\n", + ); + writeFileSync( + join(linkedPluginSource, "skills", "linked-skill", "SKILL.md"), + "---\nname: linked-skill\ndescription: Linked\n---\n", + ); + symlinkSync( + linkedPluginSource, + join(pkgDir, "plugins", "linked"), + process.platform === "win32" ? "junction" : "dir", + ); + writeFileSync( + join(pkgDir, "package.json"), + JSON.stringify({ + name: "manifest-glob-semantics-pkg", + pi: { + extensions: [ + "./extension-files/*.ts", + "./extension-files/**/.ignored.ts", + "./extension-files/nested/.hidden.ts", + "./extension-groups/*/", + ], + skills: ["./plugins/*/skills", "./plugins/linked/skills"], + }, + }), + ); + + const result = await packageManager.resolveExtensionSources([pkgDir]); + expect(result.extensions.map((resource) => relative(pkgDir, resource.path))).toEqual([ + join("extension-files", "a.ts"), + join("extension-files", "z.ts"), + join("extension-files", "nested", ".hidden.ts"), + join("extension-groups", "group", "index.ts"), + ]); + expect(result.skills.some((resource) => pathEndsWith(resource.path, "local-skill/SKILL.md"))).toBe(true); + expect(result.skills.some((resource) => pathEndsWith(resource.path, "linked-skill/SKILL.md"))).toBe(true); + }); + }); + + describe("pattern filtering in package filters", () => { + it("should apply user filters on top of manifest filters (not replace)", async () => { + // Manifest excludes baz.ts, user excludes bar.ts + // Result should exclude BOTH + const pkgDir = join(tempDir, "layered-pkg"); + mkdirSync(join(pkgDir, "extensions"), { recursive: true }); + writeFileSync(join(pkgDir, "extensions", "foo.ts"), "export default function() {}"); + writeFileSync(join(pkgDir, "extensions", "bar.ts"), "export default function() {}"); + writeFileSync(join(pkgDir, "extensions", "baz.ts"), "export default function() {}"); + writeFileSync( + join(pkgDir, "package.json"), + JSON.stringify({ + name: "layered-pkg", + pi: { + extensions: ["extensions", "!**/baz.ts"], + }, + }), + ); + + // User filter adds exclusion for bar.ts + settingsManager.setPackages([ + { + source: pkgDir, + extensions: ["!**/bar.ts"], + skills: [], + prompts: [], + themes: [], + }, + ]); + + const result = await packageManager.resolve(); + // foo.ts should be included (not excluded by anyone) + expect(result.extensions.some((r) => isEnabled(r, "foo.ts"))).toBe(true); + // bar.ts should be excluded (by user) + expect(result.extensions.some((r) => isDisabled(r, "bar.ts"))).toBe(true); + // baz.ts should be excluded (by manifest) + expect(result.extensions.some((r) => pathEndsWith(r.path, "baz.ts"))).toBe(false); + }); + + it("should exclude extensions from package with ! pattern", async () => { + const pkgDir = join(tempDir, "pattern-pkg"); + mkdirSync(join(pkgDir, "extensions"), { recursive: true }); + writeFileSync(join(pkgDir, "extensions", "foo.ts"), "export default function() {}"); + writeFileSync(join(pkgDir, "extensions", "bar.ts"), "export default function() {}"); + writeFileSync(join(pkgDir, "extensions", "baz.ts"), "export default function() {}"); + + settingsManager.setPackages([ + { + source: pkgDir, + extensions: ["!**/baz.ts"], + skills: [], + prompts: [], + themes: [], + }, + ]); + + const result = await packageManager.resolve(); + expect(result.extensions.some((r) => isEnabled(r, "foo.ts"))).toBe(true); + expect(result.extensions.some((r) => isEnabled(r, "bar.ts"))).toBe(true); + expect(result.extensions.some((r) => isDisabled(r, "baz.ts"))).toBe(true); + }); + + it("should filter themes from package", async () => { + const pkgDir = join(tempDir, "theme-pkg"); + mkdirSync(join(pkgDir, "themes"), { recursive: true }); + writeFileSync(join(pkgDir, "themes", "nice.json"), "{}"); + writeFileSync(join(pkgDir, "themes", "ugly.json"), "{}"); + + settingsManager.setPackages([ + { + source: pkgDir, + extensions: [], + skills: [], + prompts: [], + themes: ["!ugly.json"], + }, + ]); + + const result = await packageManager.resolve(); + expect(result.themes.some((r) => isEnabled(r, "nice.json"))).toBe(true); + expect(result.themes.some((r) => isDisabled(r, "ugly.json"))).toBe(true); + }); + + it("should combine include and exclude patterns", async () => { + const pkgDir = join(tempDir, "combo-pkg"); + mkdirSync(join(pkgDir, "extensions"), { recursive: true }); + writeFileSync(join(pkgDir, "extensions", "alpha.ts"), "export default function() {}"); + writeFileSync(join(pkgDir, "extensions", "beta.ts"), "export default function() {}"); + writeFileSync(join(pkgDir, "extensions", "gamma.ts"), "export default function() {}"); + + settingsManager.setPackages([ + { + source: pkgDir, + extensions: ["**/alpha.ts", "**/beta.ts", "!**/beta.ts"], + skills: [], + prompts: [], + themes: [], + }, + ]); + + const result = await packageManager.resolve(); + expect(result.extensions.some((r) => isEnabled(r, "alpha.ts"))).toBe(true); + expect(result.extensions.some((r) => isDisabled(r, "beta.ts"))).toBe(true); + expect(result.extensions.some((r) => isDisabled(r, "gamma.ts"))).toBe(true); + }); + + it("should work with direct paths (no patterns)", async () => { + const pkgDir = join(tempDir, "direct-pkg"); + mkdirSync(join(pkgDir, "extensions"), { recursive: true }); + writeFileSync(join(pkgDir, "extensions", "one.ts"), "export default function() {}"); + writeFileSync(join(pkgDir, "extensions", "two.ts"), "export default function() {}"); + + settingsManager.setPackages([ + { + source: pkgDir, + extensions: ["extensions/one.ts"], + skills: [], + prompts: [], + themes: [], + }, + ]); + + const result = await packageManager.resolve(); + expect(result.extensions.some((r) => isEnabled(r, "one.ts"))).toBe(true); + expect(result.extensions.some((r) => isDisabled(r, "two.ts"))).toBe(true); + }); + + it("should resolve autoload-disabled project package entries as deltas over global packages", async () => { + const pkgDir = join(agentDir, "npm", "node_modules", "pi-tools"); + mkdirSync(join(pkgDir, "extensions"), { recursive: true }); + writeFileSync(join(pkgDir, "package.json"), JSON.stringify({ name: "pi-tools", version: "1.0.0" })); + writeFileSync(join(pkgDir, "extensions", "foo.ts"), "export default function() {}"); + writeFileSync(join(pkgDir, "extensions", "bar.ts"), "export default function() {}"); + settingsManager.setPackages(["npm:pi-tools"]); + settingsManager.setProjectPackages([ + { source: "npm:pi-tools", autoload: false, extensions: ["-extensions/foo.ts"] }, + ]); + const runCommandSpy = vi + .spyOn(packageManager as unknown as PackageManagerInternals, "runCommand") + .mockRejectedValue(new Error("unexpected install")); + + const result = await packageManager.resolve(); + const states = Object.fromEntries( + result.extensions.map((resource) => [ + resource.path, + { enabled: resource.enabled, scope: resource.metadata.scope }, + ]), + ); + expect(runCommandSpy).not.toHaveBeenCalled(); + expect(states[join(pkgDir, "extensions", "foo.ts")]).toEqual({ enabled: false, scope: "project" }); + expect(states[join(pkgDir, "extensions", "bar.ts")]).toEqual({ enabled: true, scope: "user" }); + }); + + it("should resolve autoload-disabled package entries as positive-only without a global package", async () => { + vi.stubEnv("HOME", tempDir); + const pkgDir = join(tempDir, "positive-only-pkg"); + mkdirSync(join(pkgDir, "extensions"), { recursive: true }); + mkdirSync(join(pkgDir, "skills", "foo"), { recursive: true }); + writeFileSync(join(pkgDir, "extensions", "foo.ts"), "export default function() {}"); + writeFileSync(join(pkgDir, "extensions", "bar.ts"), "export default function() {}"); + writeFileSync(join(pkgDir, "skills", "foo", "SKILL.md"), "# Foo\n"); + settingsManager.setProjectPackages([ + { source: relative(join(tempDir, ".pi"), pkgDir), autoload: false, extensions: ["+extensions/foo.ts"] }, + ]); + + const result = await packageManager.resolve(); + + expect(result.extensions.map((resource) => resource.path)).toEqual([join(pkgDir, "extensions", "foo.ts")]); + expect(result.skills).toEqual([]); + }); + }); + + describe("force-include patterns", () => { + it("should force-include extensions with + pattern after exclusion", async () => { + const extDir = join(agentDir, "extensions"); + mkdirSync(extDir, { recursive: true }); + writeFileSync(join(extDir, "keep.ts"), "export default function() {}"); + writeFileSync(join(extDir, "excluded.ts"), "export default function() {}"); + writeFileSync(join(extDir, "force-back.ts"), "export default function() {}"); + + // Exclude all, then force-include one back + settingsManager.setExtensionPaths(["extensions", "!extensions/*.ts", "+extensions/force-back.ts"]); + + const result = await packageManager.resolve(); + expect(result.extensions.some((r) => isDisabled(r, "keep.ts"))).toBe(true); + expect(result.extensions.some((r) => isDisabled(r, "excluded.ts"))).toBe(true); + expect(result.extensions.some((r) => isEnabled(r, "force-back.ts"))).toBe(true); + }); + + it("should force-include overrides exclude in package filters", async () => { + const pkgDir = join(tempDir, "force-pkg"); + mkdirSync(join(pkgDir, "extensions"), { recursive: true }); + writeFileSync(join(pkgDir, "extensions", "alpha.ts"), "export default function() {}"); + writeFileSync(join(pkgDir, "extensions", "beta.ts"), "export default function() {}"); + writeFileSync(join(pkgDir, "extensions", "gamma.ts"), "export default function() {}"); + + settingsManager.setPackages([ + { + source: pkgDir, + extensions: ["!**/*.ts", "+extensions/beta.ts"], + skills: [], + prompts: [], + themes: [], + }, + ]); + + const result = await packageManager.resolve(); + expect(result.extensions.some((r) => isDisabled(r, "alpha.ts"))).toBe(true); + expect(result.extensions.some((r) => isEnabled(r, "beta.ts"))).toBe(true); + expect(result.extensions.some((r) => isDisabled(r, "gamma.ts"))).toBe(true); + }); + + it("should force-include multiple resources", async () => { + const pkgDir = join(tempDir, "multi-force-pkg"); + mkdirSync(join(pkgDir, "skills/skill-a"), { recursive: true }); + mkdirSync(join(pkgDir, "skills/skill-b"), { recursive: true }); + mkdirSync(join(pkgDir, "skills/skill-c"), { recursive: true }); + writeFileSync(join(pkgDir, "skills/skill-a", "SKILL.md"), "---\nname: skill-a\ndescription: A\n---\nContent"); + writeFileSync(join(pkgDir, "skills/skill-b", "SKILL.md"), "---\nname: skill-b\ndescription: B\n---\nContent"); + writeFileSync(join(pkgDir, "skills/skill-c", "SKILL.md"), "---\nname: skill-c\ndescription: C\n---\nContent"); + + settingsManager.setPackages([ + { + source: pkgDir, + extensions: [], + skills: ["!**/*", "+skills/skill-a", "+skills/skill-c"], + prompts: [], + themes: [], + }, + ]); + + const result = await packageManager.resolve(); + expect(result.skills.some((r) => isEnabled(r, "skill-a", "includes"))).toBe(true); + expect(result.skills.some((r) => isDisabled(r, "skill-b", "includes"))).toBe(true); + expect(result.skills.some((r) => isEnabled(r, "skill-c", "includes"))).toBe(true); + }); + + it("should force-include after specific exclusion", async () => { + const extDir = join(agentDir, "extensions"); + mkdirSync(extDir, { recursive: true }); + writeFileSync(join(extDir, "a.ts"), "export default function() {}"); + writeFileSync(join(extDir, "b.ts"), "export default function() {}"); + + // Specifically exclude b.ts, then force it back + settingsManager.setExtensionPaths(["extensions", "!extensions/b.ts", "+extensions/b.ts"]); + + const result = await packageManager.resolve(); + expect(result.extensions.some((r) => isEnabled(r, "a.ts"))).toBe(true); + expect(result.extensions.some((r) => isEnabled(r, "b.ts"))).toBe(true); + }); + + it("should handle force-include in manifest patterns", async () => { + const pkgDir = join(tempDir, "manifest-force-pkg"); + mkdirSync(join(pkgDir, "extensions"), { recursive: true }); + writeFileSync(join(pkgDir, "extensions", "one.ts"), "export default function() {}"); + writeFileSync(join(pkgDir, "extensions", "two.ts"), "export default function() {}"); + writeFileSync(join(pkgDir, "extensions", "three.ts"), "export default function() {}"); + writeFileSync( + join(pkgDir, "package.json"), + JSON.stringify({ + name: "manifest-force-pkg", + pi: { + extensions: ["extensions", "!**/two.ts", "+extensions/two.ts"], + }, + }), + ); + + const result = await packageManager.resolveExtensionSources([pkgDir]); + expect(result.extensions.some((r) => isEnabled(r, "one.ts"))).toBe(true); + expect(result.extensions.some((r) => isEnabled(r, "two.ts"))).toBe(true); + expect(result.extensions.some((r) => isEnabled(r, "three.ts"))).toBe(true); + }); + + it("should force-include themes", async () => { + const themesDir = join(agentDir, "themes"); + mkdirSync(themesDir, { recursive: true }); + writeFileSync(join(themesDir, "dark.json"), "{}"); + writeFileSync(join(themesDir, "light.json"), "{}"); + writeFileSync(join(themesDir, "special.json"), "{}"); + + settingsManager.setThemePaths(["themes", "!themes/*.json", "+themes/special.json"]); + + const result = await packageManager.resolve(); + expect(result.themes.some((r) => isDisabled(r, "dark.json"))).toBe(true); + expect(result.themes.some((r) => isDisabled(r, "light.json"))).toBe(true); + expect(result.themes.some((r) => isEnabled(r, "special.json"))).toBe(true); + }); + + it("should force-include prompts", async () => { + const promptsDir = join(agentDir, "prompts"); + mkdirSync(promptsDir, { recursive: true }); + writeFileSync(join(promptsDir, "review.md"), "Review"); + writeFileSync(join(promptsDir, "explain.md"), "Explain"); + writeFileSync(join(promptsDir, "debug.md"), "Debug"); + + settingsManager.setPromptTemplatePaths(["prompts", "!prompts/*.md", "+prompts/debug.md"]); + + const result = await packageManager.resolve(); + expect(result.prompts.some((r) => isDisabled(r, "review.md"))).toBe(true); + expect(result.prompts.some((r) => isDisabled(r, "explain.md"))).toBe(true); + expect(result.prompts.some((r) => isEnabled(r, "debug.md"))).toBe(true); + }); + }); + + describe("force-exclude patterns", () => { + it("should force-exclude top-level resources", async () => { + const extDir = join(agentDir, "extensions"); + mkdirSync(extDir, { recursive: true }); + writeFileSync(join(extDir, "alpha.ts"), "export default function() {}"); + writeFileSync(join(extDir, "beta.ts"), "export default function() {}"); + + settingsManager.setExtensionPaths(["extensions", "+extensions/alpha.ts", "-extensions/alpha.ts"]); + + const result = await packageManager.resolve(); + expect(result.extensions.some((r) => isDisabled(r, "alpha.ts"))).toBe(true); + expect(result.extensions.some((r) => isEnabled(r, "beta.ts"))).toBe(true); + }); + + it("should force-exclude in package filters", async () => { + const pkgDir = join(tempDir, "force-exclude-pkg"); + mkdirSync(join(pkgDir, "extensions"), { recursive: true }); + writeFileSync(join(pkgDir, "extensions", "alpha.ts"), "export default function() {}"); + writeFileSync(join(pkgDir, "extensions", "beta.ts"), "export default function() {}"); + + settingsManager.setPackages([ + { + source: pkgDir, + extensions: ["extensions/*.ts", "+extensions/alpha.ts", "-extensions/alpha.ts"], + skills: [], + prompts: [], + themes: [], + }, + ]); + + const result = await packageManager.resolve(); + expect(result.extensions.some((r) => isDisabled(r, "alpha.ts"))).toBe(true); + expect(result.extensions.some((r) => isEnabled(r, "beta.ts"))).toBe(true); + }); + }); + + describe("package deduplication", () => { + it("should dedupe same local package in global and project (project wins)", async () => { + const pkgDir = join(tempDir, "shared-pkg"); + mkdirSync(join(pkgDir, "extensions"), { recursive: true }); + writeFileSync(join(pkgDir, "extensions", "shared.ts"), "export default function() {}"); + + // Same package in both global and project + settingsManager.setPackages([pkgDir]); // global + settingsManager.setProjectPackages([pkgDir]); // project + + // Debug: verify settings are stored correctly + const globalSettings = settingsManager.getGlobalSettings(); + const projectSettings = settingsManager.getProjectSettings(); + expect(globalSettings.packages).toEqual([pkgDir]); + expect(projectSettings.packages).toEqual([pkgDir]); + + const result = await packageManager.resolve(); + // Should only appear once (deduped), with project scope + const sharedPaths = result.extensions.filter((r) => r.path.includes("shared-pkg")); + expect(sharedPaths.length).toBe(1); + expect(sharedPaths[0].metadata.scope).toBe("project"); + }); + + it("should keep both if different packages", async () => { + const pkg1Dir = join(tempDir, "pkg1"); + const pkg2Dir = join(tempDir, "pkg2"); + mkdirSync(join(pkg1Dir, "extensions"), { recursive: true }); + mkdirSync(join(pkg2Dir, "extensions"), { recursive: true }); + writeFileSync(join(pkg1Dir, "extensions", "from-pkg1.ts"), "export default function() {}"); + writeFileSync(join(pkg2Dir, "extensions", "from-pkg2.ts"), "export default function() {}"); + + settingsManager.setPackages([pkg1Dir]); // global + settingsManager.setProjectPackages([pkg2Dir]); // project + + const result = await packageManager.resolve(); + expect(result.extensions.some((r) => r.path.includes("pkg1"))).toBe(true); + expect(result.extensions.some((r) => r.path.includes("pkg2"))).toBe(true); + }); + + it("should dedupe SSH and HTTPS URLs for same repo", async () => { + // Same repository, different URL formats + const httpsUrl = "https://github.com/user/repo"; + const sshUrl = "git:git@github.com:user/repo"; + + const httpsIdentity = (packageManager as any).getPackageIdentity(httpsUrl); + const sshIdentity = (packageManager as any).getPackageIdentity(sshUrl); + + // Both should resolve to the same identity + expect(httpsIdentity).toBe("git:github.com/user/repo"); + expect(sshIdentity).toBe("git:github.com/user/repo"); + expect(httpsIdentity).toBe(sshIdentity); + }); + + it("should dedupe SSH and HTTPS with refs", async () => { + const httpsUrl = "https://github.com/user/repo@v1.0.0"; + const sshUrl = "git:git@github.com:user/repo@v1.0.0"; + + const httpsIdentity = (packageManager as any).getPackageIdentity(httpsUrl); + const sshIdentity = (packageManager as any).getPackageIdentity(sshUrl); + + // Identity should ignore ref (version) + expect(httpsIdentity).toBe("git:github.com/user/repo"); + expect(sshIdentity).toBe("git:github.com/user/repo"); + expect(httpsIdentity).toBe(sshIdentity); + }); + + it("should dedupe SSH URL with ssh:// protocol and git@ format", async () => { + const sshProtocol = "ssh://git@github.com/user/repo"; + const gitAt = "git:git@github.com:user/repo"; + + const sshProtocolIdentity = (packageManager as any).getPackageIdentity(sshProtocol); + const gitAtIdentity = (packageManager as any).getPackageIdentity(gitAt); + + // Both SSH formats should resolve to same identity + expect(sshProtocolIdentity).toBe("git:github.com/user/repo"); + expect(gitAtIdentity).toBe("git:github.com/user/repo"); + expect(sshProtocolIdentity).toBe(gitAtIdentity); + }); + + it("should dedupe all supported URL formats for same repo", async () => { + const urls = [ + "https://github.com/user/repo", + "https://github.com/user/repo.git", + "ssh://git@github.com/user/repo", + "git:https://github.com/user/repo", + "git:github.com/user/repo", + "git:git@github.com:user/repo", + "git:git@github.com:user/repo.git", + ]; + + const identities = urls.map((url) => (packageManager as any).getPackageIdentity(url)); + + // All should produce the same identity + const uniqueIdentities = [...new Set(identities)]; + expect(uniqueIdentities.length).toBe(1); + expect(uniqueIdentities[0]).toBe("git:github.com/user/repo"); + }); + + it("should keep different repos separate (HTTPS vs SSH)", async () => { + const repo1Https = "https://github.com/user/repo1"; + const repo2Ssh = "git:git@github.com:user/repo2"; + + const id1 = (packageManager as any).getPackageIdentity(repo1Https); + const id2 = (packageManager as any).getPackageIdentity(repo2Ssh); + + // Different repos should have different identities + expect(id1).toBe("git:github.com/user/repo1"); + expect(id2).toBe("git:github.com/user/repo2"); + expect(id1).not.toBe(id2); + }); + }); + + describe("multi-file extension discovery (issue #1102)", () => { + it("should only load index.ts from subdirectories, not helper modules", async () => { + // Regression test: packages with multi-file extensions in subdirectories + // should only load the index.ts entry point, not helper modules like agents.ts + const pkgDir = join(tempDir, "multifile-pkg"); + mkdirSync(join(pkgDir, "extensions", "subagent"), { recursive: true }); + + // Main entry point + writeFileSync( + join(pkgDir, "extensions", "subagent", "index.ts"), + `import { helper } from "./agents.ts"; +export default function(api) { api.registerTool({ name: "test", description: "test", execute: async () => helper() }); }`, + ); + // Helper module (should NOT be loaded as standalone extension) + writeFileSync( + join(pkgDir, "extensions", "subagent", "agents.ts"), + `export function helper() { return "helper"; }`, + ); + // Top-level extension file (should be loaded) + writeFileSync(join(pkgDir, "extensions", "standalone.ts"), "export default function(api) {}"); + + const result = await packageManager.resolveExtensionSources([pkgDir]); + + // Should find the index.ts and standalone.ts + expect(result.extensions.some((r) => pathEndsWith(r.path, "subagent/index.ts") && r.enabled)).toBe(true); + expect(result.extensions.some((r) => pathEndsWith(r.path, "standalone.ts") && r.enabled)).toBe(true); + + // Should NOT find agents.ts as a standalone extension + expect(result.extensions.some((r) => pathEndsWith(r.path, "agents.ts"))).toBe(false); + }); + + it("should respect package.json pi.extensions manifest in subdirectories", async () => { + const pkgDir = join(tempDir, "manifest-subdir-pkg"); + mkdirSync(join(pkgDir, "extensions", "custom"), { recursive: true }); + + // Subdirectory with its own manifest + writeFileSync( + join(pkgDir, "extensions", "custom", "package.json"), + JSON.stringify({ + pi: { + extensions: ["./main.ts"], + }, + }), + ); + writeFileSync(join(pkgDir, "extensions", "custom", "main.ts"), "export default function(api) {}"); + writeFileSync(join(pkgDir, "extensions", "custom", "utils.ts"), "export const util = 1;"); + + const result = await packageManager.resolveExtensionSources([pkgDir]); + + // Should find main.ts declared in manifest + expect(result.extensions.some((r) => pathEndsWith(r.path, "custom/main.ts") && r.enabled)).toBe(true); + + // Should NOT find utils.ts (not declared in manifest) + expect(result.extensions.some((r) => pathEndsWith(r.path, "utils.ts"))).toBe(false); + }); + + it("should handle mixed top-level files and subdirectories", async () => { + const pkgDir = join(tempDir, "mixed-pkg"); + mkdirSync(join(pkgDir, "extensions", "complex"), { recursive: true }); + + // Top-level extension + writeFileSync(join(pkgDir, "extensions", "simple.ts"), "export default function(api) {}"); + + // Subdirectory with index.ts + helpers + writeFileSync( + join(pkgDir, "extensions", "complex", "index.ts"), + "import { a } from './a.ts'; export default function(api) {}", + ); + writeFileSync(join(pkgDir, "extensions", "complex", "a.ts"), "export const a = 1;"); + writeFileSync(join(pkgDir, "extensions", "complex", "b.ts"), "export const b = 2;"); + + const result = await packageManager.resolveExtensionSources([pkgDir]); + + // Should find simple.ts and complex/index.ts + expect(result.extensions.some((r) => pathEndsWith(r.path, "simple.ts") && r.enabled)).toBe(true); + expect(result.extensions.some((r) => pathEndsWith(r.path, "complex/index.ts") && r.enabled)).toBe(true); + + // Should NOT find helper modules + expect(result.extensions.some((r) => pathEndsWith(r.path, "complex/a.ts"))).toBe(false); + expect(result.extensions.some((r) => pathEndsWith(r.path, "complex/b.ts"))).toBe(false); + + // Total should be exactly 2 + expect(result.extensions.filter((r) => r.enabled).length).toBe(2); + }); + + it("should skip subdirectories without index.ts or manifest", async () => { + const pkgDir = join(tempDir, "no-entry-pkg"); + mkdirSync(join(pkgDir, "extensions", "broken"), { recursive: true }); + + // Subdirectory with no index.ts and no manifest + writeFileSync(join(pkgDir, "extensions", "broken", "helper.ts"), "export const x = 1;"); + writeFileSync(join(pkgDir, "extensions", "broken", "another.ts"), "export const y = 2;"); + + // Valid top-level extension + writeFileSync(join(pkgDir, "extensions", "valid.ts"), "export default function(api) {}"); + + const result = await packageManager.resolveExtensionSources([pkgDir]); + + // Should only find the valid top-level extension + expect(result.extensions.some((r) => pathEndsWith(r.path, "valid.ts") && r.enabled)).toBe(true); + expect(result.extensions.filter((r) => r.enabled).length).toBe(1); + }); + }); + + describe("offline mode and network timeouts", () => { + it("should update npm range packages using the configured spec", async () => { + const installedPath = join(tempDir, ".pi", "npm", "node_modules", "example"); + mkdirSync(installedPath, { recursive: true }); + writeFileSync(join(installedPath, "package.json"), JSON.stringify({ name: "example", version: "1.0.0" })); + settingsManager.setProjectPackages(["npm:example@^1.0.0"]); + + const runCommandCaptureSpy = vi + .spyOn(packageManager as any, "runCommandCapture") + .mockResolvedValue('["1.0.0","1.2.0"]'); + const runCommandSpy = vi.spyOn(packageManager as any, "runCommand").mockResolvedValue(undefined); + + await packageManager.update("npm:example"); + + expect(runCommandCaptureSpy).toHaveBeenCalledWith( + "npm", + ["view", "example@^1.0.0", "version", "--json"], + expect.objectContaining({ cwd: tempDir, timeoutMs: expect.any(Number) }), + ); + expect(runCommandSpy).toHaveBeenCalledWith( + "npm", + ["install", "example@^1.0.0", "--prefix", join(tempDir, ".pi", "npm"), "--legacy-peer-deps"], + undefined, + ); + }); + + it("should skip project npm update when installed version matches latest", async () => { + const installedPath = join(tempDir, ".pi", "npm", "node_modules", "example"); + mkdirSync(installedPath, { recursive: true }); + writeFileSync(join(installedPath, "package.json"), JSON.stringify({ name: "example", version: "1.3.1" })); + settingsManager.setProjectPackages(["npm:example@^1.0.0"]); + + const runCommandCaptureSpy = vi + .spyOn(packageManager as any, "runCommandCapture") + .mockResolvedValue('["1.0.0","1.3.1","1.0.2"]'); + const runCommandSpy = vi.spyOn(packageManager as any, "runCommand").mockResolvedValue(undefined); + + await packageManager.update("npm:example"); + + expect(runCommandCaptureSpy).toHaveBeenCalledWith( + "npm", + ["view", "example@^1.0.0", "version", "--json"], + expect.objectContaining({ cwd: tempDir, timeoutMs: expect.any(Number) }), + ); + expect(runCommandSpy).not.toHaveBeenCalled(); + }); + + it("should skip npm updates when the installed version is newer than the registry version", async () => { + const installedPath = join(tempDir, ".pi", "npm", "node_modules", "example"); + mkdirSync(installedPath, { recursive: true }); + writeFileSync(join(installedPath, "package.json"), JSON.stringify({ name: "example", version: "2.0.0" })); + settingsManager.setProjectPackages(["npm:example"]); + + const runCommandCaptureSpy = vi.spyOn(packageManager as any, "runCommandCapture").mockResolvedValue('"1.9.0"'); + const runCommandSpy = vi.spyOn(packageManager as any, "runCommand").mockResolvedValue(undefined); + + await packageManager.update("npm:example"); + + expect(runCommandCaptureSpy).toHaveBeenCalledWith( + "npm", + ["view", "example", "version", "--json"], + expect.objectContaining({ cwd: tempDir, timeoutMs: expect.any(Number) }), + ); + expect(runCommandSpy).not.toHaveBeenCalled(); + }); + + it("should migrate legacy user npm installs into the managed npm root during update", async () => { + const legacyRoot = join(tempDir, "legacy-global", "node_modules"); + const legacyPath = join(legacyRoot, "legacy-pkg"); + const managedPath = join(agentDir, "npm", "node_modules", "legacy-pkg"); + mkdirSync(legacyPath, { recursive: true }); + writeFileSync(join(legacyPath, "package.json"), JSON.stringify({ name: "legacy-pkg", version: "1.0.0" })); + settingsManager.setPackages(["npm:legacy-pkg"]); + + vi.spyOn(packageManager as any, "getGlobalNpmRoot").mockReturnValue(legacyRoot); + const runCommandCaptureSpy = vi.spyOn(packageManager as any, "runCommandCapture").mockResolvedValue('"1.0.0"'); + const runCommandSpy = vi + .spyOn(packageManager as any, "runCommand") + .mockImplementation(async (...callArgs: unknown[]) => { + const [command, args] = callArgs as [string, string[]]; + expect(command).toBe("npm"); + expect(args).toEqual([ + "install", + "legacy-pkg@latest", + "--prefix", + join(agentDir, "npm"), + "--legacy-peer-deps", + ]); + mkdirSync(managedPath, { recursive: true }); + writeFileSync( + join(managedPath, "package.json"), + JSON.stringify({ name: "legacy-pkg", version: "1.0.0" }), + ); + }); + + expect(packageManager.getInstalledPath("npm:legacy-pkg", "user")).toBe(legacyPath); + + await packageManager.update("npm:legacy-pkg"); + + expect(runCommandCaptureSpy).not.toHaveBeenCalled(); + expect(runCommandSpy).toHaveBeenCalledTimes(1); + expect(packageManager.getInstalledPath("npm:legacy-pkg", "user")).toBe(managedPath); + }); + + it("should batch npm updates per scope and run git updates in parallel while skipping pinned npm and current packages", async () => { + const userOldPath = join(agentDir, "npm", "node_modules", "user-old"); + const userCurrentPath = join(agentDir, "npm", "node_modules", "user-current"); + const userUnknownPath = join(agentDir, "npm", "node_modules", "user-unknown"); + const projectOldPath = join(tempDir, ".pi", "npm", "node_modules", "project-old"); + const projectCurrentPath = join(tempDir, ".pi", "npm", "node_modules", "project-current"); + const installPaths = [userOldPath, userCurrentPath, userUnknownPath, projectOldPath, projectCurrentPath]; + for (const installPath of installPaths) { + mkdirSync(installPath, { recursive: true }); + } + writeFileSync(join(userOldPath, "package.json"), JSON.stringify({ name: "user-old", version: "1.0.0" })); + writeFileSync( + join(userCurrentPath, "package.json"), + JSON.stringify({ name: "user-current", version: "1.0.0" }), + ); + writeFileSync( + join(userUnknownPath, "package.json"), + JSON.stringify({ name: "user-unknown", version: "1.0.0" }), + ); + writeFileSync(join(projectOldPath, "package.json"), JSON.stringify({ name: "project-old", version: "1.0.0" })); + writeFileSync( + join(projectCurrentPath, "package.json"), + JSON.stringify({ name: "project-current", version: "1.0.0" }), + ); + + settingsManager.setPackages([ + "npm:user-old", + "npm:user-current", + "npm:user-unknown", + "npm:user-pinned@1.0.0", + "git:github.com/example/user-repo-a", + "git:github.com/example/user-repo-b", + "git:github.com/example/user-repo-pinned@v1", + ]); + settingsManager.setProjectPackages([ + "npm:project-old", + "npm:project-current", + "npm:project-missing", + "git:github.com/example/project-repo-a", + ]); + + const runCommandCaptureSpy = vi + .spyOn(packageManager as any, "runCommandCapture") + .mockImplementation(async (...callArgs: unknown[]) => { + const [_command, args] = callArgs as [string, string[]]; + if (args[0] !== "view") { + throw new Error(`Unexpected runCommandCapture args: ${args.join(" ")}`); + } + switch (args[1]) { + case "user-old": + case "project-old": + return '"2.0.0"'; + case "user-current": + case "project-current": + return '"1.0.0"'; + case "user-unknown": + throw new Error("registry unavailable"); + default: + throw new Error(`Unexpected package lookup: ${args[1]}`); + } + }); + + let activeNpmUpdates = 0; + let maxConcurrentNpmUpdates = 0; + const runCommandSpy = vi + .spyOn(packageManager as any, "runCommand") + .mockImplementation(async (...callArgs: unknown[]) => { + const [command, args] = callArgs as [string, string[]]; + if (command !== "npm") { + throw new Error(`Unexpected runCommand call: ${command} ${args.join(" ")}`); + } + activeNpmUpdates += 1; + maxConcurrentNpmUpdates = Math.max(maxConcurrentNpmUpdates, activeNpmUpdates); + await new Promise((resolve) => setTimeout(resolve, 20)); + activeNpmUpdates -= 1; + }); + + let activeGitUpdates = 0; + let maxConcurrentGitUpdates = 0; + const updateGitSpy = vi.spyOn(packageManager as any, "updateGit").mockImplementation(async () => { + activeGitUpdates += 1; + maxConcurrentGitUpdates = Math.max(maxConcurrentGitUpdates, activeGitUpdates); + await new Promise((resolve) => setTimeout(resolve, 20)); + activeGitUpdates -= 1; + }); + + await packageManager.update(); + + expect(runCommandCaptureSpy).toHaveBeenCalledTimes(5); + expect(runCommandSpy).toHaveBeenCalledTimes(2); + expect(runCommandSpy).toHaveBeenNthCalledWith( + 1, + "npm", + [ + "install", + "user-old@latest", + "user-unknown@latest", + "--prefix", + join(agentDir, "npm"), + "--legacy-peer-deps", + ], + undefined, + ); + expect(runCommandSpy).toHaveBeenNthCalledWith( + 2, + "npm", + [ + "install", + "project-old@latest", + "project-missing@latest", + "--prefix", + join(tempDir, ".pi", "npm"), + "--legacy-peer-deps", + ], + undefined, + ); + expect(updateGitSpy).toHaveBeenCalledTimes(4); + expect(maxConcurrentNpmUpdates).toBeGreaterThan(1); + expect(maxConcurrentGitUpdates).toBeGreaterThan(1); + }); + + it("should suggest npm source prefixes for update lookups", async () => { + settingsManager.setProjectPackages(["npm:example"]); + + await expect(packageManager.update("example")).rejects.toThrow( + "No matching package found for example. Did you mean npm:example?", + ); + }); + + it("should suggest git source prefixes for update lookups", async () => { + settingsManager.setProjectPackages(["git:github.com/example/repo"]); + + await expect(packageManager.update("github.com/example/repo")).rejects.toThrow( + "No matching package found for github.com/example/repo. Did you mean git:github.com/example/repo?", + ); + }); + + it("should skip installing missing package sources when offline", async () => { + process.env.PI_OFFLINE = "1"; + settingsManager.setProjectPackages(["npm:missing-package", "git:github.com/example/missing-repo"]); + + const installParsedSourceSpy = vi.spyOn(packageManager as any, "installParsedSource"); + + const result = await packageManager.resolve(); + const allResources = [...result.extensions, ...result.skills, ...result.prompts, ...result.themes]; + expect(allResources.some((r) => r.metadata.origin === "package")).toBe(false); + expect(installParsedSourceSpy).not.toHaveBeenCalled(); + }); + + it("should skip refreshing temporary git sources when offline", async () => { + process.env.PI_OFFLINE = "1"; + const gitSource = "git:github.com/example/repo"; + const parsedGitSource = (packageManager as any).parseSource(gitSource); + const installedPath = (packageManager as any).getGitInstallPath(parsedGitSource, "temporary") as string; + + mkdirSync(join(installedPath, "extensions"), { recursive: true }); + writeFileSync(join(installedPath, "extensions", "index.ts"), "export default function() {};"); + + const refreshTemporaryGitSourceSpy = vi.spyOn(packageManager as any, "refreshTemporaryGitSource"); + + const result = await packageManager.resolveExtensionSources([gitSource], { temporary: true }); + expect(result.extensions.some((r) => pathEndsWith(r.path, "extensions/index.ts") && r.enabled)).toBe(true); + expect(refreshTemporaryGitSourceSpy).not.toHaveBeenCalled(); + }); + + it("should not run npm view during resolve for installed unpinned packages", async () => { + process.env.PI_OFFLINE = "1"; + const installedPath = join(tempDir, ".pi", "npm", "node_modules", "example"); + mkdirSync(join(installedPath, "extensions"), { recursive: true }); + writeFileSync(join(installedPath, "package.json"), JSON.stringify({ name: "example", version: "1.0.0" })); + writeFileSync(join(installedPath, "extensions", "index.ts"), "export default function() {};"); + settingsManager.setProjectPackages(["npm:example@^1.0.0"]); + + const runCommandCaptureSpy = vi.spyOn(packageManager as any, "runCommandCapture"); + + const result = await packageManager.resolve(); + expect(result.extensions.some((r) => pathEndsWith(r.path, "extensions/index.ts") && r.enabled)).toBe(true); + expect(runCommandCaptureSpy).not.toHaveBeenCalled(); + }); + + it("should reinstall pinned npm packages when installed version does not match", async () => { + const installedPath = join(tempDir, ".pi", "npm", "node_modules", "example"); + mkdirSync(installedPath, { recursive: true }); + writeFileSync(join(installedPath, "package.json"), JSON.stringify({ name: "example", version: "1.0.0" })); + settingsManager.setProjectPackages(["npm:example@2.0.0"]); + + const installParsedSourceSpy = vi + .spyOn(packageManager as any, "installParsedSource") + .mockResolvedValue(undefined); + + await packageManager.resolve(); + expect(installParsedSourceSpy).toHaveBeenCalledTimes(1); + }); + + it("should not check package updates when offline", async () => { + process.env.PI_OFFLINE = "1"; + const runCommandCaptureSpy = vi.spyOn(packageManager as any, "runCommandCapture"); + + const updates = await packageManager.checkForAvailableUpdates(); + expect(updates).toEqual([]); + expect(runCommandCaptureSpy).not.toHaveBeenCalled(); + }); + + it("should report updates for installed unpinned npm packages", async () => { + const installedPath = join(tempDir, ".pi", "npm", "node_modules", "example"); + mkdirSync(installedPath, { recursive: true }); + writeFileSync(join(installedPath, "package.json"), JSON.stringify({ name: "example", version: "1.0.0" })); + settingsManager.setProjectPackages(["npm:example"]); + + vi.spyOn(packageManager as any, "runCommandCapture").mockResolvedValue('"1.2.3"'); + + const updates = await packageManager.checkForAvailableUpdates(); + expect(updates).toEqual([ + { + source: "npm:example", + displayName: "example", + type: "npm", + scope: "project", + }, + ]); + }); + + it("should not report npm updates when the installed version is newer than the registry version", async () => { + const installedPath = join(tempDir, ".pi", "npm", "node_modules", "example"); + mkdirSync(installedPath, { recursive: true }); + writeFileSync(join(installedPath, "package.json"), JSON.stringify({ name: "example", version: "2.0.0" })); + settingsManager.setProjectPackages(["npm:example"]); + + vi.spyOn(packageManager as any, "runCommandCapture").mockResolvedValue('"1.9.0"'); + + const updates = await packageManager.checkForAvailableUpdates(); + expect(updates).toEqual([]); + }); + + it("should skip pinned packages when checking for updates", async () => { + const installedNpmPath = join(tempDir, ".pi", "npm", "node_modules", "example"); + mkdirSync(installedNpmPath, { recursive: true }); + writeFileSync(join(installedNpmPath, "package.json"), JSON.stringify({ name: "example", version: "1.0.0" })); + const parsedGitSource = (packageManager as any).parseSource("git:github.com/example/repo@v1"); + const installedGitPath = (packageManager as any).getGitInstallPath(parsedGitSource, "project") as string; + mkdirSync(installedGitPath, { recursive: true }); + + settingsManager.setProjectPackages(["npm:example@1.0.0", "git:github.com/example/repo@v1"]); + + const runCommandCaptureSpy = vi.spyOn(packageManager as any, "runCommandCapture"); + const gitUpdateSpy = vi.spyOn(packageManager as any, "gitHasAvailableUpdate"); + + const updates = await packageManager.checkForAvailableUpdates(); + expect(updates).toEqual([]); + expect(runCommandCaptureSpy).not.toHaveBeenCalled(); + expect(gitUpdateSpy).not.toHaveBeenCalled(); + }); + + it("should use npm view to fetch latest version", async () => { + const runCommandCaptureSpy = vi.spyOn(packageManager as any, "runCommandCapture").mockResolvedValue('"1.2.3"'); + + const latest = await (packageManager as any).getLatestNpmVersion("example"); + expect(latest).toBe("1.2.3"); + expect(runCommandCaptureSpy).toHaveBeenCalledTimes(1); + expect(runCommandCaptureSpy).toHaveBeenCalledWith( + "npm", + ["view", "example", "version", "--json"], + expect.objectContaining({ cwd: tempDir, timeoutMs: expect.any(Number) }), + ); + }); + + it("should use npmCommand argv for npm update checks", async () => { + settingsManager = SettingsManager.inMemory({ + npmCommand: ["mise", "exec", "node@20", "--", "npm"], + }); + packageManager = new DefaultPackageManager({ + cwd: tempDir, + agentDir, + settingsManager, + }); + + const runCommandCaptureSpy = vi.spyOn(packageManager as any, "runCommandCapture").mockResolvedValue('"1.2.3"'); + + const latest = await (packageManager as any).getLatestNpmVersion("@scope/pkg"); + expect(latest).toBe("1.2.3"); + expect(runCommandCaptureSpy).toHaveBeenCalledWith( + "mise", + ["exec", "node@20", "--", "npm", "view", "@scope/pkg", "version", "--json"], + expect.objectContaining({ cwd: tempDir }), + ); + }); + + it("should wait for close before resolving captured stdout", async () => { + const managerWithInternals = packageManager as unknown as { + spawnCaptureCommand( + command: string, + args: string[], + options?: { cwd?: string; env?: Record }, + ): MockSpawnedProcess; + runCommandCapture( + command: string, + args: string[], + options?: { cwd?: string; timeoutMs?: number; env?: Record }, + ): Promise; + }; + const child = new MockSpawnedProcess(); + vi.spyOn(managerWithInternals, "spawnCaptureCommand").mockReturnValue(child); + + let settled = false; + const capturePromise = managerWithInternals.runCommandCapture("git", ["rev-parse", "HEAD"]).then((value) => { + settled = true; + return value; + }); + + child.emit("exit", 0, null); + await Promise.resolve(); + expect(settled).toBe(false); + + child.stdout.write("abc123\n"); + child.stdout.end(); + child.emit("close", 0, null); + + await expect(capturePromise).resolves.toBe("abc123"); + }); + }); +}); diff --git a/packages/coding-agent/test/path-utils.test.ts b/packages/coding-agent/test/path-utils.test.ts new file mode 100644 index 0000000000000000000000000000000000000000..77a8345fbb7a72ff989a66aaf699a211f74e5248 --- /dev/null +++ b/packages/coding-agent/test/path-utils.test.ts @@ -0,0 +1,174 @@ +import { mkdtempSync, readdirSync, rmdirSync, unlinkSync, writeFileSync } from "node:fs"; +import { tmpdir } from "node:os"; +import { join, resolve } from "node:path"; +import { afterEach, beforeEach, describe, expect, it } from "vitest"; +import { expandPath, resolveReadPath, resolveToCwd } from "../src/core/tools/path-utils.ts"; + +describe("path-utils", () => { + describe("expandPath", () => { + it("should expand ~ to home directory", () => { + const result = expandPath("~"); + expect(result).not.toContain("~"); + }); + + it("should expand ~/path to home directory", () => { + const result = expandPath("~/Documents/file.txt"); + expect(result).not.toContain("~/"); + }); + + it("should keep tilde-prefixed filenames literal", () => { + expect(expandPath("~draft.md")).toBe("~draft.md"); + expect(expandPath("@~draft.md")).toBe("~draft.md"); + }); + + it("should normalize Unicode spaces", () => { + // Non-breaking space (U+00A0) should become regular space + const withNBSP = "file\u00A0name.txt"; + const result = expandPath(withNBSP); + expect(result).toBe("file name.txt"); + }); + }); + + describe("resolveToCwd", () => { + it("should resolve absolute paths as-is", () => { + const absolutePath = resolve(tmpdir(), "absolute", "path", "file.txt"); + const result = resolveToCwd(absolutePath, resolve(tmpdir(), "some", "cwd")); + expect(result).toBe(absolutePath); + }); + + it("should resolve relative paths against cwd", () => { + const result = resolveToCwd("relative/file.txt", "/some/cwd"); + expect(result).toBe(resolve("/some/cwd", "relative/file.txt")); + }); + + it("should resolve tilde-prefixed filenames against cwd", () => { + const cwd = join(tmpdir(), "pi-path-utils-cwd"); + expect(resolveToCwd("~draft.md", cwd)).toBe(resolve(cwd, "~draft.md")); + expect(resolveToCwd("@~draft.md", cwd)).toBe(resolve(cwd, "~draft.md")); + }); + }); + + describe("resolveReadPath", () => { + let tempDir: string; + + beforeEach(() => { + tempDir = mkdtempSync(join(tmpdir(), "path-utils-test-")); + }); + + afterEach(() => { + // Clean up temp files and directory + try { + const files = readdirSync(tempDir); + for (const file of files) { + unlinkSync(join(tempDir, file)); + } + rmdirSync(tempDir); + } catch { + // Ignore cleanup errors + } + }); + + it("should resolve existing file path", () => { + const fileName = "test-file.txt"; + writeFileSync(join(tempDir, fileName), "content"); + + const result = resolveReadPath(fileName, tempDir); + expect(result).toBe(join(tempDir, fileName)); + }); + + it("should handle NFC vs NFD Unicode normalization (macOS filenames with accents)", () => { + // macOS stores filenames in NFD (decomposed) form: + // é = e + combining acute accent (U+0301) + // Users typically type in NFC (composed) form: + // é = single character (U+00E9) + // + // Note: macOS APFS normalizes Unicode automatically, so both paths work. + // This test verifies the NFD variant fallback works on systems that don't. + + // NFD: e (U+0065) + combining acute accent (U+0301) + const nfdFileName = "file\u0065\u0301.txt"; + // NFC: é as single character (U+00E9) + const nfcFileName = "file\u00e9.txt"; + + // Verify they have different byte sequences + expect(nfdFileName).not.toBe(nfcFileName); + expect(Buffer.from(nfdFileName)).not.toEqual(Buffer.from(nfcFileName)); + + // Create file with NFD name + writeFileSync(join(tempDir, nfdFileName), "content"); + + // User provides NFC path - should find the file (via filesystem normalization or our fallback) + const result = resolveReadPath(nfcFileName, tempDir); + // Result should contain the accented character (either NFC or NFD form) + expect(result).toContain(tempDir); + expect(result).toMatch(/file.+\.txt$/); + }); + + it("should handle curly quotes vs straight quotes (macOS filenames)", () => { + // macOS uses curly apostrophe (U+2019) in screenshot filenames: + // Capture d'écran (U+2019) + // Users typically type straight apostrophe (U+0027): + // Capture d'ecran (U+0027) + + const curlyQuoteName = "Capture d\u2019cran.txt"; // U+2019 right single quotation mark + const straightQuoteName = "Capture d'cran.txt"; // U+0027 apostrophe + + // Verify they are different + expect(curlyQuoteName).not.toBe(straightQuoteName); + + // Create file with curly quote name (simulating macOS behavior) + writeFileSync(join(tempDir, curlyQuoteName), "content"); + + // User provides straight quote path - should find the curly quote file + const result = resolveReadPath(straightQuoteName, tempDir); + expect(result).toBe(join(tempDir, curlyQuoteName)); + }); + + it("should handle combined NFC + curly quote (French macOS screenshots)", () => { + // Full macOS screenshot filename: "Capture d'écran" with NFD é and curly quote + // Note: macOS APFS normalizes NFD to NFC, so the actual file on disk uses NFC + const nfcCurlyName = "Capture d\u2019\u00e9cran.txt"; // NFC + curly quote (how APFS stores it) + const nfcStraightName = "Capture d'\u00e9cran.txt"; // NFC + straight quote (user input) + + // Verify they are different + expect(nfcCurlyName).not.toBe(nfcStraightName); + + // Create file with macOS-style name (curly quote) + writeFileSync(join(tempDir, nfcCurlyName), "content"); + + // User provides straight quote path - should find the curly quote file + const result = resolveReadPath(nfcStraightName, tempDir); + expect(result).toBe(join(tempDir, nfcCurlyName)); + }); + + it("should handle macOS screenshot AM/PM variant with narrow no-break space", () => { + // macOS uses narrow no-break space (U+202F) before AM/PM in screenshot names + const macosName = "Screenshot 2024-01-01 at 10.00.00\u202FAM.png"; // U+202F + const userName = "Screenshot 2024-01-01 at 10.00.00 AM.png"; // regular space + + // Create file with macOS-style name + writeFileSync(join(tempDir, macosName), "content"); + + // User provides regular space path + const result = resolveReadPath(userName, tempDir); + + // This works because tryMacOSScreenshotPath() handles this case + expect(result).toBe(join(tempDir, macosName)); + }); + + it("should handle macOS screenshot lowercase am/pm variant (en_AU locale)", () => { + // Some locales like en_AU use lowercase am/pm in screenshot names + const macosName = "Screenshot 2024-01-01 at 10.00.00\u202Fam.png"; // U+202F + lowercase + const userName = "Screenshot 2024-01-01 at 10.00.00 am.png"; // regular space + lowercase + + // Create file with macOS-style name + writeFileSync(join(tempDir, macosName), "content"); + + // User provides regular space path + const result = resolveReadPath(userName, tempDir); + + // This works because tryMacOSScreenshotPath() uses case-insensitive matching + expect(result).toBe(join(tempDir, macosName)); + }); + }); +}); diff --git a/packages/coding-agent/test/paths.test.ts b/packages/coding-agent/test/paths.test.ts new file mode 100644 index 0000000000000000000000000000000000000000..fbdb4f56f8899238cc99b4a5057c41778b14066d --- /dev/null +++ b/packages/coding-agent/test/paths.test.ts @@ -0,0 +1,184 @@ +import { mkdirSync, mkdtempSync, realpathSync, rmSync, symlinkSync, writeFileSync } from "node:fs"; +import { homedir, tmpdir } from "node:os"; +import { join, resolve } from "node:path"; +import { pathToFileURL } from "node:url"; +import { afterEach, describe, expect, it } from "vitest"; +import { + canonicalizePath, + getCwdRelativePath, + isLocalPath, + normalizePath, + normalizeWindowsShellPath, + resolvePath, +} from "../src/utils/paths.ts"; + +let tempDir: string; + +afterEach(() => { + if (tempDir) { + rmSync(tempDir, { recursive: true, force: true }); + tempDir = ""; + } +}); + +function createTempDir(): string { + tempDir = mkdtempSync(join(tmpdir(), "pi-paths-")); + return tempDir; +} + +describe("canonicalizePath", () => { + it("returns the real path for a regular file", () => { + const dir = createTempDir(); + const file = join(dir, "file.txt"); + writeFileSync(file, "hello"); + expect(canonicalizePath(file)).toBe(realpathSync(file)); + }); + + it("resolves symlinks to their targets", () => { + const dir = createTempDir(); + const target = join(dir, "target.txt"); + const link = join(dir, "link.txt"); + writeFileSync(target, "hello"); + symlinkSync(target, link); + expect(canonicalizePath(link)).toBe(realpathSync(target)); + }); + + it("resolves directory symlinks", () => { + const dir = createTempDir(); + const targetDir = join(dir, "target-dir"); + const linkDir = join(dir, "link-dir"); + mkdirSync(targetDir); + symlinkSync(targetDir, linkDir, "dir"); + expect(canonicalizePath(linkDir)).toBe(realpathSync(targetDir)); + }); + + it("falls back to the raw path when the target does not exist", () => { + const dir = createTempDir(); + const nonexistent = join(dir, "no-such-file"); + expect(canonicalizePath(nonexistent)).toBe(nonexistent); + }); + + it("falls back to the raw path for a dangling symlink", () => { + const dir = createTempDir(); + const target = join(dir, "target.txt"); + const link = join(dir, "link.txt"); + // Create a symlink whose target does not exist. + symlinkSync(target, link); + // realpathSync would throw, so canonicalizePath returns the link path. + expect(canonicalizePath(link)).toBe(link); + }); +}); + +describe("getCwdRelativePath", () => { + it("keeps cwd-relative names that start with dots", () => { + const cwd = join(tmpdir(), "pi-paths-cwd"); + expect(getCwdRelativePath(join(cwd, "..config", "AGENTS.md"), cwd)).toBe(join("..config", "AGENTS.md")); + }); + + it("rejects parent-directory traversals", () => { + const cwd = join(tmpdir(), "pi-paths-cwd"); + expect(getCwdRelativePath(join(cwd, "..", "AGENTS.md"), cwd)).toBeUndefined(); + }); +}); + +describe("resolvePath", () => { + it("expands only home tilde shortcuts", () => { + const cwd = join(tmpdir(), "pi-paths-cwd"); + expect(normalizePath("~")).toBe(homedir()); + expect(normalizePath("~/file.txt")).toBe(join(homedir(), "file.txt")); + expect(resolvePath("~draft.md", cwd)).toBe(resolve(cwd, "~draft.md")); + expect(normalizePath("~draft.md")).toBe("~draft.md"); + }); + + it("resolves relative paths against the base directory", () => { + const cwd = join(tmpdir(), "pi-paths-cwd"); + expect(resolvePath("subdir/file.txt", cwd)).toBe(resolve(cwd, "subdir/file.txt")); + expect(resolvePath("subdir/file.txt", pathToFileURL(cwd).href)).toBe(resolve(cwd, "subdir/file.txt")); + }); + + it("accepts file URLs", () => { + const dir = createTempDir(); + const filePath = join(dir, "file with spaces.txt"); + expect(resolvePath(pathToFileURL(filePath).href, join(dir, "base"))).toBe(resolve(filePath)); + }); + + it("throws for invalid file URLs", () => { + expect(() => resolvePath("file:///%E0%A4%A")).toThrow(); + }); + + it("preserves POSIX absolute paths with literal percent sequences", () => { + if (process.platform === "win32") { + return; + } + + const dir = createTempDir(); + for (const filePath of [join(dir, "report%2026.md"), join(dir, "foo%2Fbar"), join(dir, "malformed%A.md")]) { + expect(resolvePath(filePath, join(dir, "base"))).toBe(resolve(filePath)); + } + }); + + it("does not treat Windows file URL pathname strings as native paths", () => { + if (process.platform !== "win32") { + return; + } + + const dir = createTempDir(); + const filePath = join(dir, "dir", "SKILL.md"); + const pathname = pathToFileURL(filePath).pathname; + expect(pathname).toMatch(/^\/[A-Za-z]:/); + expect(resolvePath(pathname, "E:\\project")).toBe(resolve(pathname)); + }); +}); + +describe("normalizeWindowsShellPath", () => { + it("converts Git Bash, MSYS, Cygwin, and WSL drive paths", () => { + expect(normalizeWindowsShellPath("/c/Users/example/project")).toBe("C:\\Users\\example\\project"); + expect(normalizeWindowsShellPath("/cygdrive/d/work")).toBe("D:\\work"); + expect(normalizeWindowsShellPath("/mnt/e/source")).toBe("E:\\source"); + expect(normalizeWindowsShellPath("/c")).toBe("C:\\"); + }); + + it("leaves other path forms unchanged", () => { + for (const path of [ + "C:/Users/example", + "C:\\Users\\example", + "//server/share/file", + "/c/Users\\example", + "relative/file", + "/tmp/file", + ]) { + expect(normalizeWindowsShellPath(path)).toBe(path); + } + }); + + it.runIf(process.platform === "win32")("is applied by normal path handling on Windows", () => { + expect(normalizePath("/c/Users/example")).toBe("C:\\Users\\example"); + expect(resolvePath("/mnt/c/Users/example", "D:\\work")).toBe(resolve("C:/Users/example")); + }); +}); + +describe("isLocalPath", () => { + it("returns true for bare names", () => { + expect(isLocalPath("my-package")).toBe(true); + }); + + it("returns true for relative paths", () => { + expect(isLocalPath("./foo")).toBe(true); + }); + + it("returns true for file URLs", () => { + expect(isLocalPath("file:///tmp/foo")).toBe(true); + }); + + it("returns false for npm: protocol", () => { + expect(isLocalPath("npm:package")).toBe(false); + }); + + it("returns false for git: protocol", () => { + expect(isLocalPath("git://repo")).toBe(false); + }); + + it("returns false for https: protocol", () => { + expect(isLocalPath("https://example.com")).toBe(false); + }); +}); diff --git a/packages/coding-agent/test/pi-user-agent.test.ts b/packages/coding-agent/test/pi-user-agent.test.ts new file mode 100644 index 0000000000000000000000000000000000000000..1c49ca29db465e0c9119eab44f1a6149a47d1355 --- /dev/null +++ b/packages/coding-agent/test/pi-user-agent.test.ts @@ -0,0 +1,12 @@ +import { describe, expect, it } from "vitest"; +import { getPiUserAgent } from "../src/utils/pi-user-agent.ts"; + +describe("getPiUserAgent", () => { + it("formats the user agent expected by pi.dev", () => { + const runtime = process.versions.bun ? `bun/${process.versions.bun}` : `node/${process.version}`; + const userAgent = getPiUserAgent("1.2.3"); + + expect(userAgent).toBe(`pi/1.2.3 (${process.platform}; ${runtime}; ${process.arch})`); + expect(userAgent).toMatch(/^pi\/[^\s()]+ \([^;()]+;\s*[^;()]+;\s*[^()]+\)$/); + }); +}); diff --git a/packages/coding-agent/test/plan-mode-extension.test.ts b/packages/coding-agent/test/plan-mode-extension.test.ts new file mode 100644 index 0000000000000000000000000000000000000000..419a707e97dea861dbc10a55f1472e611147c5df --- /dev/null +++ b/packages/coding-agent/test/plan-mode-extension.test.ts @@ -0,0 +1,167 @@ +import type { AgentMessage } from "@earendil-works/pi-agent-core"; +import type { AssistantMessage } from "@earendil-works/pi-ai"; +import { describe, expect, it, vi } from "vitest"; +import planModeExtension from "../examples/extensions/plan-mode/index.ts"; +import type { ExtensionAPI, ExtensionContext } from "../src/core/extensions/index.ts"; + +type CommandHandler = (args: string, ctx: ExtensionContext) => Promise | void; +type AgentEndHandler = ( + event: { type: "agent_end"; messages: AgentMessage[] }, + ctx: ExtensionContext, +) => Promise | void; + +function createAssistantMessage(text: string): AssistantMessage { + return { + role: "assistant", + content: [{ type: "text", text }], + api: "anthropic-messages", + provider: "anthropic", + model: "mock", + usage: { + input: 0, + output: 0, + cacheRead: 0, + cacheWrite: 0, + totalTokens: 0, + cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 }, + }, + stopReason: "stop", + timestamp: Date.now(), + }; +} + +function setup(options: { activeTools?: string[]; selectChoice?: string; editorText?: string } = {}) { + let activeTools = options.activeTools ?? ["read", "bash", "edit", "write"]; + const commands = new Map(); + let agentEndHandler: AgentEndHandler | undefined; + + const sendMessage = vi.fn(); + const sendUserMessage = vi.fn(); + const setActiveTools = vi.fn((toolNames) => { + activeTools = [...toolNames]; + }); + const appendEntry = vi.fn(); + + const api = { + registerFlag: vi.fn(), + registerCommand(name: string, command: { handler: CommandHandler }) { + commands.set(name, command.handler); + }, + registerShortcut: vi.fn(), + on(event: string, handler: unknown) { + if (event === "agent_end") agentEndHandler = handler as AgentEndHandler; + }, + getFlag: vi.fn(() => false), + getActiveTools: vi.fn(() => [...activeTools]), + setActiveTools, + sendMessage, + sendUserMessage, + appendEntry, + } as unknown as ExtensionAPI; + + planModeExtension(api); + + const ctx = { + hasUI: true, + ui: { + notify: vi.fn(), + select: vi.fn(async () => options.selectChoice), + editor: vi.fn(async () => options.editorText), + setStatus: vi.fn(), + setWidget: vi.fn(), + theme: { + fg: (_name: string, text: string) => text, + strikethrough: (text: string) => text, + }, + }, + sessionManager: { getEntries: () => [] }, + isIdle: () => false, + hasPendingMessages: () => false, + } as unknown as ExtensionContext; + + async function runCommand(name: string): Promise { + const command = commands.get(name); + if (!command) throw new Error(`Missing command: ${name}`); + await command("", ctx); + } + + async function triggerAgentEnd(text: string): Promise { + if (!agentEndHandler) throw new Error("Missing agent_end handler"); + await agentEndHandler({ type: "agent_end", messages: [createAssistantMessage(text)] }, ctx); + } + + return { + activeTools: () => activeTools, + appendEntry, + ctx, + runCommand, + sendMessage, + sendUserMessage, + setActiveTools, + triggerAgentEnd, + }; +} + +describe("plan-mode example extension", () => { + it("preserves custom active tools while toggling plan mode", async () => { + const { activeTools, runCommand, setActiveTools } = setup({ + activeTools: ["read", "bash", "edit", "write", "echo_tool"], + }); + + await runCommand("plan"); + + expect(activeTools()).toEqual(["read", "bash", "echo_tool", "grep", "find", "ls", "questionnaire"]); + expect(setActiveTools).toHaveBeenLastCalledWith([ + "read", + "bash", + "echo_tool", + "grep", + "find", + "ls", + "questionnaire", + ]); + + await runCommand("plan"); + + expect(activeTools()).toEqual(["read", "bash", "edit", "write", "echo_tool"]); + expect(setActiveTools).toHaveBeenLastCalledWith(["read", "bash", "edit", "write", "echo_tool"]); + }); + + it("does not prompt when the assistant response contains no plan", async () => { + const { ctx, runCommand, sendMessage, triggerAgentEnd } = setup(); + + await runCommand("plan"); + await triggerAgentEnd("This file defines the command-line argument parser."); + + expect(ctx.ui.select).not.toHaveBeenCalled(); + expect(sendMessage).not.toHaveBeenCalled(); + }); + + it("queues plan refinement as a follow-up user message", async () => { + const { runCommand, sendUserMessage, triggerAgentEnd } = setup({ + selectChoice: "Refine the plan", + editorText: "Add a regression test.", + }); + + await runCommand("plan"); + await triggerAgentEnd("Plan:\n1. Inspect the current implementation\n2. Add a regression test"); + + expect(sendUserMessage).toHaveBeenCalledWith("Add a regression test.", { deliverAs: "followUp" }); + }); + + it("queues plan execution as a follow-up custom message", async () => { + const { activeTools, runCommand, sendMessage, triggerAgentEnd } = setup({ + activeTools: ["read", "bash", "edit", "write", "echo_tool"], + selectChoice: "Execute the plan (track progress)", + }); + + await runCommand("plan"); + await triggerAgentEnd("Plan:\n1. Inspect the current implementation\n2. Add a regression test"); + + expect(activeTools()).toEqual(["read", "bash", "edit", "write", "echo_tool"]); + expect(sendMessage).toHaveBeenCalledWith(expect.objectContaining({ customType: "plan-mode-execute" }), { + triggerTurn: true, + deliverAs: "followUp", + }); + }); +}); diff --git a/packages/coding-agent/test/plan-mode-utils.test.ts b/packages/coding-agent/test/plan-mode-utils.test.ts new file mode 100644 index 0000000000000000000000000000000000000000..db4b31ec154d18a0d32d683ff4e3c615ee79ba2a --- /dev/null +++ b/packages/coding-agent/test/plan-mode-utils.test.ts @@ -0,0 +1,261 @@ +import { describe, expect, it } from "vitest"; +import { + cleanStepText, + extractDoneSteps, + extractTodoItems, + isSafeCommand, + markCompletedSteps, + type TodoItem, +} from "../examples/extensions/plan-mode/utils.ts"; + +describe("isSafeCommand", () => { + describe("safe commands", () => { + it("allows basic read commands", () => { + expect(isSafeCommand("ls -la")).toBe(true); + expect(isSafeCommand("cat file.txt")).toBe(true); + expect(isSafeCommand("head -n 10 file.txt")).toBe(true); + expect(isSafeCommand("tail -f log.txt")).toBe(true); + expect(isSafeCommand("grep pattern file")).toBe(true); + expect(isSafeCommand("find . -name '*.ts'")).toBe(true); + }); + + it("allows git read commands", () => { + expect(isSafeCommand("git status")).toBe(true); + expect(isSafeCommand("git log --oneline")).toBe(true); + expect(isSafeCommand("git diff")).toBe(true); + expect(isSafeCommand("git branch")).toBe(true); + }); + + it("allows npm/yarn read commands", () => { + expect(isSafeCommand("npm list")).toBe(true); + expect(isSafeCommand("npm outdated")).toBe(true); + expect(isSafeCommand("yarn info react")).toBe(true); + }); + + it("allows other safe commands", () => { + expect(isSafeCommand("pwd")).toBe(true); + expect(isSafeCommand("echo hello")).toBe(true); + expect(isSafeCommand("wc -l file.txt")).toBe(true); + expect(isSafeCommand("du -sh .")).toBe(true); + expect(isSafeCommand("df -h")).toBe(true); + }); + }); + + describe("destructive commands", () => { + it("blocks file modification commands", () => { + expect(isSafeCommand("rm file.txt")).toBe(false); + expect(isSafeCommand("rm -rf dir")).toBe(false); + expect(isSafeCommand("mv old new")).toBe(false); + expect(isSafeCommand("cp src dst")).toBe(false); + expect(isSafeCommand("mkdir newdir")).toBe(false); + expect(isSafeCommand("touch newfile")).toBe(false); + }); + + it("blocks git write commands", () => { + expect(isSafeCommand("git add .")).toBe(false); + expect(isSafeCommand("git commit -m 'msg'")).toBe(false); + expect(isSafeCommand("git push")).toBe(false); + expect(isSafeCommand("git checkout main")).toBe(false); + expect(isSafeCommand("git reset --hard")).toBe(false); + }); + + it("blocks package manager installs", () => { + expect(isSafeCommand("npm install lodash")).toBe(false); + expect(isSafeCommand("yarn add react")).toBe(false); + expect(isSafeCommand("pip install requests")).toBe(false); + expect(isSafeCommand("brew install node")).toBe(false); + }); + + it("blocks redirects", () => { + expect(isSafeCommand("echo hello > file.txt")).toBe(false); + expect(isSafeCommand("cat foo >> bar")).toBe(false); + expect(isSafeCommand(">file.txt")).toBe(false); + }); + + it("blocks dangerous commands", () => { + expect(isSafeCommand("sudo rm -rf /")).toBe(false); + expect(isSafeCommand("kill -9 1234")).toBe(false); + expect(isSafeCommand("reboot")).toBe(false); + }); + + it("blocks editors", () => { + expect(isSafeCommand("vim file.txt")).toBe(false); + expect(isSafeCommand("nano file.txt")).toBe(false); + expect(isSafeCommand("code .")).toBe(false); + }); + }); + + describe("edge cases", () => { + it("requires command to be in safe list (not just non-destructive)", () => { + expect(isSafeCommand("unknown-command")).toBe(false); + expect(isSafeCommand("my-script.sh")).toBe(false); + }); + + it("handles commands with leading whitespace", () => { + expect(isSafeCommand(" ls -la")).toBe(true); + expect(isSafeCommand(" rm file")).toBe(false); + }); + }); +}); + +describe("cleanStepText", () => { + it("removes markdown bold/italic", () => { + expect(cleanStepText("**bold text**")).toBe("Bold text"); + expect(cleanStepText("*italic text*")).toBe("Italic text"); + }); + + it("removes markdown code", () => { + expect(cleanStepText("run `npm install`")).toBe("Npm install"); // "run" is stripped as action word + expect(cleanStepText("check the `config.json` file")).toBe("Config.json file"); + }); + + it("removes leading action words", () => { + expect(cleanStepText("Create the new file")).toBe("New file"); + expect(cleanStepText("Run the tests")).toBe("Tests"); + expect(cleanStepText("Check the status")).toBe("Status"); + }); + + it("capitalizes first letter", () => { + expect(cleanStepText("update config")).toBe("Config"); + }); + + it("truncates long text", () => { + const longText = "This is a very long step description that exceeds the maximum allowed length for display"; + const result = cleanStepText(longText); + expect(result.length).toBe(50); + expect(result.endsWith("...")).toBe(true); + }); + + it("normalizes whitespace", () => { + expect(cleanStepText("multiple spaces here")).toBe("Multiple spaces here"); + }); +}); + +describe("extractTodoItems", () => { + it("extracts numbered items after Plan: header", () => { + const message = `Here's what we'll do: + +Plan: +1. First step here +2. Second step here +3. Third step here`; + + const items = extractTodoItems(message); + expect(items).toHaveLength(3); + expect(items[0].step).toBe(1); + expect(items[0].text).toBe("First step here"); + expect(items[0].completed).toBe(false); + }); + + it("handles bold Plan header", () => { + const message = `**Plan:** +1. Do something`; + + const items = extractTodoItems(message); + expect(items).toHaveLength(1); + }); + + it("handles parenthesis-style numbering", () => { + const message = `Plan: +1) First item +2) Second item`; + + const items = extractTodoItems(message); + expect(items).toHaveLength(2); + }); + + it("returns empty array without Plan header", () => { + const message = `Here are some steps: +1. First step +2. Second step`; + + const items = extractTodoItems(message); + expect(items).toHaveLength(0); + }); + + it("filters out short items", () => { + const message = `Plan: +1. OK +2. This is a proper step`; + + const items = extractTodoItems(message); + expect(items).toHaveLength(1); + expect(items[0].text).toContain("proper"); + }); + + it("filters out code-like items", () => { + const message = `Plan: +1. \`npm install\` +2. Run the build process`; + + const items = extractTodoItems(message); + expect(items).toHaveLength(1); + }); +}); + +describe("extractDoneSteps", () => { + it("extracts single DONE marker", () => { + const message = "I've completed the first step [DONE:1]"; + expect(extractDoneSteps(message)).toEqual([1]); + }); + + it("extracts multiple DONE markers", () => { + const message = "Did steps [DONE:1] and [DONE:2] and [DONE:3]"; + expect(extractDoneSteps(message)).toEqual([1, 2, 3]); + }); + + it("handles case insensitivity", () => { + const message = "[done:1] [DONE:2] [Done:3]"; + expect(extractDoneSteps(message)).toEqual([1, 2, 3]); + }); + + it("returns empty array with no markers", () => { + const message = "No markers here"; + expect(extractDoneSteps(message)).toEqual([]); + }); + + it("ignores malformed markers", () => { + const message = "[DONE:abc] [DONE:] [DONE:1]"; + expect(extractDoneSteps(message)).toEqual([1]); + }); +}); + +describe("markCompletedSteps", () => { + it("marks matching items as completed", () => { + const items: TodoItem[] = [ + { step: 1, text: "First", completed: false }, + { step: 2, text: "Second", completed: false }, + { step: 3, text: "Third", completed: false }, + ]; + + const count = markCompletedSteps("[DONE:1] [DONE:3]", items); + + expect(count).toBe(2); + expect(items[0].completed).toBe(true); + expect(items[1].completed).toBe(false); + expect(items[2].completed).toBe(true); + }); + + it("returns count of completed items", () => { + const items: TodoItem[] = [{ step: 1, text: "First", completed: false }]; + + expect(markCompletedSteps("[DONE:1]", items)).toBe(1); + expect(markCompletedSteps("no markers", items)).toBe(0); + }); + + it("ignores markers for non-existent steps", () => { + const items: TodoItem[] = [{ step: 1, text: "First", completed: false }]; + + const count = markCompletedSteps("[DONE:99]", items); + + expect(count).toBe(1); // Still counts the marker found + expect(items[0].completed).toBe(false); // But doesn't mark anything + }); + + it("doesn't double-complete already completed items", () => { + const items: TodoItem[] = [{ step: 1, text: "First", completed: true }]; + + markCompletedSteps("[DONE:1]", items); + expect(items[0].completed).toBe(true); + }); +}); diff --git a/packages/coding-agent/test/powershell-tool.test.ts b/packages/coding-agent/test/powershell-tool.test.ts new file mode 100644 index 0000000000000000000000000000000000000000..22bc193c895435ec44ce674ee12aee90f92db9c6 --- /dev/null +++ b/packages/coding-agent/test/powershell-tool.test.ts @@ -0,0 +1,30 @@ +import { describe, expect, it } from "vitest"; +import { createPowerShellTool } from "../src/core/tools/powershell.ts"; +import { getPowerShellConfig, POWERSHELL_ARGS } from "../src/utils/shell.ts"; + +function getTextOutput(result: { content: Array<{ type: string; text?: string }> }): string { + return result.content + .filter((content) => content.type === "text") + .map((content) => content.text ?? "") + .join("\n"); +} + +describe("powershell tool", () => { + it("uses process-local execution policy bypass", () => { + expect(POWERSHELL_ARGS).toEqual(["-NoProfile", "-NonInteractive", "-ExecutionPolicy", "Bypass", "-Command"]); + }); + + it.skipIf(process.platform !== "win32")("executes PowerShell commands with UTF-8 output", async () => { + const config = getPowerShellConfig(); + expect(config.args).toEqual(POWERSHELL_ARGS); + + const tool = createPowerShellTool(process.cwd()); + const result = await tool.execute("powershell-test", { + command: "Write-Output 'héllo €'; Get-ExecutionPolicy -Scope Process", + }); + const output = getTextOutput(result); + + expect(output).toContain("héllo €"); + expect(output).toContain("Bypass"); + }); +}); diff --git a/packages/coding-agent/test/print-mode.test.ts b/packages/coding-agent/test/print-mode.test.ts new file mode 100644 index 0000000000000000000000000000000000000000..32cec8e872601c85a9c4c3e90cfd2c7489a799b1 --- /dev/null +++ b/packages/coding-agent/test/print-mode.test.ts @@ -0,0 +1,142 @@ +import type { AssistantMessage, ImageContent } from "@earendil-works/pi-ai"; +import { afterEach, describe, expect, it, vi } from "vitest"; +import type { SessionShutdownEvent } from "../src/index.ts"; +import { runPrintMode } from "../src/modes/print-mode.ts"; + +type EmitEvent = SessionShutdownEvent; + +type FakeExtensionRunner = { + hasHandlers: (eventType: string) => boolean; + emit: ReturnType Promise>>; +}; + +type FakeSession = { + sessionManager: { getHeader: () => object | undefined }; + agent: { waitForIdle: () => Promise; subscribe: ReturnType }; + state: { messages: AssistantMessage[] }; + extensionRunner: FakeExtensionRunner; + bindExtensions: ReturnType; + subscribe: ReturnType; + prompt: ReturnType; + reload: ReturnType; +}; + +type FakeRuntimeHost = { + session: FakeSession; + newSession: ReturnType; + fork: ReturnType; + switchSession: ReturnType; + dispose: ReturnType; + setRebindSession: ReturnType; +}; + +function createAssistantMessage(options?: { + text?: string; + stopReason?: AssistantMessage["stopReason"]; + errorMessage?: string; +}): AssistantMessage { + return { + role: "assistant", + content: options?.text ? [{ type: "text", text: options.text }] : [], + api: "openai-responses", + provider: "openai", + model: "gpt-4o-mini", + usage: { + input: 0, + output: 0, + cacheRead: 0, + cacheWrite: 0, + totalTokens: 0, + cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 }, + }, + stopReason: options?.stopReason ?? "stop", + errorMessage: options?.errorMessage, + timestamp: Date.now(), + }; +} + +function createRuntimeHost(assistantMessage: AssistantMessage): FakeRuntimeHost { + const extensionRunner: FakeExtensionRunner = { + hasHandlers: (eventType: string) => eventType === "session_shutdown", + emit: vi.fn(async () => {}), + }; + + const state = { messages: [assistantMessage] }; + + const session: FakeSession = { + sessionManager: { getHeader: () => undefined }, + agent: { waitForIdle: async () => {}, subscribe: vi.fn(() => () => {}) }, + state, + extensionRunner, + bindExtensions: vi.fn(async () => {}), + subscribe: vi.fn(() => () => {}), + prompt: vi.fn(async () => {}), + reload: vi.fn(async () => {}), + }; + + return { + session, + newSession: vi.fn(async () => undefined), + fork: vi.fn(async () => ({ selectedText: "" })), + switchSession: vi.fn(async () => undefined), + dispose: vi.fn(async () => { + await session.extensionRunner.emit({ type: "session_shutdown", reason: "quit" }); + }), + setRebindSession: vi.fn(), + }; +} + +afterEach(() => { + vi.restoreAllMocks(); +}); + +describe("runPrintMode", () => { + it("emits session_shutdown in text mode", async () => { + const runtimeHost = createRuntimeHost(createAssistantMessage({ text: "done" })); + const { session } = runtimeHost; + const images: ImageContent[] = [{ type: "image", mimeType: "image/png", data: "abc" }]; + + const exitCode = await runPrintMode(runtimeHost as unknown as Parameters[0], { + mode: "text", + initialMessage: "Say done", + initialImages: images, + }); + + expect(exitCode).toBe(0); + expect(session.prompt).toHaveBeenCalledWith("Say done", { images }); + expect(session.extensionRunner.emit).toHaveBeenCalledTimes(1); + expect(session.extensionRunner.emit).toHaveBeenCalledWith({ type: "session_shutdown", reason: "quit" }); + }); + + it("emits session_shutdown in json mode", async () => { + const runtimeHost = createRuntimeHost(createAssistantMessage({ text: "done" })); + const { session } = runtimeHost; + + const exitCode = await runPrintMode(runtimeHost as unknown as Parameters[0], { + mode: "json", + messages: ["hello"], + }); + + expect(exitCode).toBe(0); + expect(session.prompt).toHaveBeenCalledWith("hello"); + expect(session.extensionRunner.emit).toHaveBeenCalledTimes(1); + expect(session.extensionRunner.emit).toHaveBeenCalledWith({ type: "session_shutdown", reason: "quit" }); + }); + + it("emits session_shutdown and returns non-zero on assistant error", async () => { + const runtimeHost = createRuntimeHost( + createAssistantMessage({ stopReason: "error", errorMessage: "provider failure" }), + ); + const { session } = runtimeHost; + const errorSpy = vi.spyOn(console, "error").mockImplementation(() => {}); + + const exitCode = await runPrintMode(runtimeHost as unknown as Parameters[0], { + mode: "text", + }); + + expect(exitCode).toBe(1); + expect(errorSpy).toHaveBeenCalledWith("provider failure"); + expect(session.extensionRunner.emit).toHaveBeenCalledTimes(1); + expect(session.extensionRunner.emit).toHaveBeenCalledWith({ type: "session_shutdown", reason: "quit" }); + }); +}); diff --git a/packages/coding-agent/test/prompt-templates.test.ts b/packages/coding-agent/test/prompt-templates.test.ts new file mode 100644 index 0000000000000000000000000000000000000000..97bfe985cbf3a6428c8791116f978808067d49f0 --- /dev/null +++ b/packages/coding-agent/test/prompt-templates.test.ts @@ -0,0 +1,624 @@ +/** + * Tests for prompt template argument parsing and substitution. + * + * Tests verify: + * - Argument parsing with quotes and special characters + * - Placeholder substitution ($1, $2, $@, $ARGUMENTS) + * - No recursive substitution of patterns in argument values + * - Edge cases and integration between parsing and substitution + */ + +import { mkdirSync, rmSync, writeFileSync } from "fs"; +import { tmpdir } from "os"; +import { join } from "path"; +import { afterAll, describe, expect, test } from "vitest"; +import { getAgentDir } from "../src/config.ts"; +import { + expandPromptTemplate, + loadPromptTemplates, + parseCommandArgs, + substituteArgs, +} from "../src/core/prompt-templates.ts"; + +// ============================================================================ +// substituteArgs +// ============================================================================ + +describe("substituteArgs", () => { + test("should replace $ARGUMENTS with all args joined", () => { + expect(substituteArgs("Test: $ARGUMENTS", ["a", "b", "c"])).toBe("Test: a b c"); + }); + + test("should replace $@ with all args joined", () => { + expect(substituteArgs("Test: $@", ["a", "b", "c"])).toBe("Test: a b c"); + }); + + test("should replace $@ and $ARGUMENTS identically", () => { + const args = ["foo", "bar", "baz"]; + expect(substituteArgs("Test: $@", args)).toBe(substituteArgs("Test: $ARGUMENTS", args)); + }); + + // CRITICAL: argument values containing patterns should remain literal + test("should NOT recursively substitute patterns in argument values", () => { + expect(substituteArgs("$ARGUMENTS", ["$1", "$ARGUMENTS"])).toBe("$1 $ARGUMENTS"); + expect(substituteArgs("$@", ["$100", "$1"])).toBe("$100 $1"); + expect(substituteArgs("$ARGUMENTS", ["$100", "$1"])).toBe("$100 $1"); + }); + + test("should support mixed $1, $2, and $ARGUMENTS", () => { + expect(substituteArgs("$1: $ARGUMENTS", ["prefix", "a", "b"])).toBe("prefix: prefix a b"); + }); + + test("should support mixed $1, $2, and $@", () => { + expect(substituteArgs("$1: $@", ["prefix", "a", "b"])).toBe("prefix: prefix a b"); + }); + + test("should handle empty arguments array with $ARGUMENTS", () => { + expect(substituteArgs("Test: $ARGUMENTS", [])).toBe("Test: "); + }); + + test("should handle empty arguments array with $@", () => { + expect(substituteArgs("Test: $@", [])).toBe("Test: "); + }); + + test("should handle empty arguments array with $1", () => { + expect(substituteArgs("Test: $1", [])).toBe("Test: "); + }); + + test("should handle multiple occurrences of $ARGUMENTS", () => { + expect(substituteArgs("$ARGUMENTS and $ARGUMENTS", ["a", "b"])).toBe("a b and a b"); + }); + + test("should handle multiple occurrences of $@", () => { + expect(substituteArgs("$@ and $@", ["a", "b"])).toBe("a b and a b"); + }); + + test("should handle mixed occurrences of $@ and $ARGUMENTS", () => { + expect(substituteArgs("$@ and $ARGUMENTS", ["a", "b"])).toBe("a b and a b"); + }); + + test("should handle special characters in arguments", () => { + // Note: $100 in argument doesn't get partially matched - full strings are substituted + expect(substituteArgs("$1 $2: $ARGUMENTS", ["arg100", "@user"])).toBe("arg100 @user: arg100 @user"); + }); + + test("should handle out-of-range numbered placeholders", () => { + // Note: Out-of-range placeholders become empty strings (preserving spaces from template) + expect(substituteArgs("$1 $2 $3 $4 $5", ["a", "b"])).toBe("a b "); + }); + + test("should handle unicode characters", () => { + expect(substituteArgs("$ARGUMENTS", ["日本語", "🎉", "café"])).toBe("日本語 🎉 café"); + }); + + test("should preserve newlines and tabs in argument values", () => { + expect(substituteArgs("$1 $2", ["line1\nline2", "tab\tthere"])).toBe("line1\nline2 tab\tthere"); + }); + + test("should handle consecutive dollar patterns", () => { + expect(substituteArgs("$1$2", ["a", "b"])).toBe("ab"); + }); + + test("should handle quoted arguments with spaces", () => { + expect(substituteArgs("$ARGUMENTS", ["first arg", "second arg"])).toBe("first arg second arg"); + }); + + test("should handle single argument with $ARGUMENTS", () => { + expect(substituteArgs("Test: $ARGUMENTS", ["only"])).toBe("Test: only"); + }); + + test("should handle single argument with $@", () => { + expect(substituteArgs("Test: $@", ["only"])).toBe("Test: only"); + }); + + test("should handle $0 (zero index)", () => { + expect(substituteArgs("$0", ["a", "b"])).toBe(""); + }); + + test("should handle decimal number in pattern (only integer part matches)", () => { + expect(substituteArgs("$1.5", ["a"])).toBe("a.5"); + }); + + test("should handle $ARGUMENTS as part of word", () => { + expect(substituteArgs("pre$ARGUMENTS", ["a", "b"])).toBe("prea b"); + }); + + test("should handle $@ as part of word", () => { + expect(substituteArgs("pre$@", ["a", "b"])).toBe("prea b"); + }); + + test("should handle empty arguments in middle of list", () => { + expect(substituteArgs("$ARGUMENTS", ["a", "", "c"])).toBe("a c"); + }); + + test("should handle trailing and leading spaces in arguments", () => { + expect(substituteArgs("$ARGUMENTS", [" leading ", "trailing "])).toBe(" leading trailing "); + }); + + test("should handle argument containing pattern partially", () => { + expect(substituteArgs("Prefix $ARGUMENTS suffix", ["ARGUMENTS"])).toBe("Prefix ARGUMENTS suffix"); + }); + + test("should handle non-matching patterns", () => { + expect(substituteArgs("$A $$ $ $ARGS", ["a"])).toBe("$A $$ $ $ARGS"); + }); + + test("should handle case variations (case-sensitive)", () => { + expect(substituteArgs("$arguments $Arguments $ARGUMENTS", ["a", "b"])).toBe("$arguments $Arguments a b"); + }); + + test("should handle both syntaxes in same command with same result", () => { + const args = ["x", "y", "z"]; + const result1 = substituteArgs("$@ and $ARGUMENTS", args); + const result2 = substituteArgs("$ARGUMENTS and $@", args); + expect(result1).toBe(result2); + expect(result1).toBe("x y z and x y z"); + }); + + test("should handle very long argument lists", () => { + const args = Array.from({ length: 100 }, (_, i) => `arg${i}`); + const result = substituteArgs("$ARGUMENTS", args); + expect(result).toBe(args.join(" ")); + }); + + test("should handle numbered placeholders with single digit", () => { + expect(substituteArgs("$1 $2 $3", ["a", "b", "c"])).toBe("a b c"); + }); + + test("should handle numbered placeholders with multiple digits", () => { + const args = Array.from({ length: 15 }, (_, i) => `val${i}`); + expect(substituteArgs("$10 $12 $15", args)).toBe("val9 val11 val14"); + }); + + test("should handle escaped dollar signs (literal backslash preserved)", () => { + // Note: No escape mechanism exists - backslash is treated literally + expect(substituteArgs("Price: \\$100", [])).toBe("Price: \\"); + }); + + test("should handle mixed numbered and wildcard placeholders", () => { + expect(substituteArgs("$1: $@ ($ARGUMENTS)", ["first", "second", "third"])).toBe( + "first: first second third (first second third)", + ); + }); + + test("should handle command with no placeholders", () => { + expect(substituteArgs("Just plain text", ["a", "b"])).toBe("Just plain text"); + }); + + test("should handle command with only placeholders", () => { + expect(substituteArgs("$1 $2 $@", ["a", "b", "c"])).toBe("a b a b c"); + }); +}); + +// ============================================================================ +// substituteArgs - Positional Defaults +// ============================================================================ + +describe("substituteArgs - positional defaults", () => { + test("should use default when positional arg is missing", () => { + expect(substituteArgs(`List exactly \${1:-7} next steps`, [])).toBe("List exactly 7 next steps"); + }); + + test("should support defaults for all arguments", () => { + const template = `\${@:-default}\n\${ARGUMENTS:-default}`; + + expect(substituteArgs(template, [])).toBe("default\ndefault"); + expect(substituteArgs(template, ["This", "would", "be", "the", "arguments"])).toBe( + "This would be the arguments\nThis would be the arguments", + ); + }); + + test("should use positional arg when present", () => { + expect(substituteArgs(`List exactly \${1:-7} next steps`, ["3"])).toBe("List exactly 3 next steps"); + }); + + test("should use default when positional arg is empty", () => { + expect(substituteArgs(`Mode: \${1:-brief}`, [""])).toBe("Mode: brief"); + }); + + test("should support multiple positional defaults", () => { + expect(substituteArgs(`\${1:-7} \${2:-brief}`, [])).toBe("7 brief"); + expect(substituteArgs(`\${1:-7} \${2:-brief}`, ["3"])).toBe("3 brief"); + expect(substituteArgs(`\${1:-7} \${2:-brief}`, ["3", "verbose"])).toBe("3 verbose"); + }); + + test("should not recursively substitute patterns in arg values", () => { + expect(substituteArgs(`\${1:-7}`, ["$ARGUMENTS"])).toBe("$ARGUMENTS"); + expect(substituteArgs(`\${1:-7}`, ["$1"])).toBe("$1"); + }); + + test("should not recursively substitute patterns in default values", () => { + expect(substituteArgs(`\${1:-$ARGUMENTS}`, ["a", "b"])).toBe("a"); + expect(substituteArgs(`\${3:-$ARGUMENTS}`, ["a", "b"])).toBe("$ARGUMENTS"); + }); + + test("should support defaults with spaces", () => { + expect(substituteArgs(`\${1:-seven steps}`, [])).toBe("seven steps"); + }); + + test("should support out-of-range positional defaults", () => { + expect(substituteArgs(`\${3:-fallback}`, ["a", "b"])).toBe("fallback"); + }); + + test("should mix positional defaults with existing placeholders", () => { + expect(substituteArgs(`$1 \${2:-x} $ARGUMENTS`, ["a"])).toBe("a x a"); + }); +}); + +// ============================================================================ +// substituteArgs - Array Slicing (Bash-Style) +// ============================================================================ + +describe("substituteArgs - array slicing", () => { + test(`should slice from index (\${@:N})`, () => { + expect(substituteArgs(`\${@:2}`, ["a", "b", "c", "d"])).toBe("b c d"); + expect(substituteArgs(`\${@:1}`, ["a", "b", "c"])).toBe("a b c"); + expect(substituteArgs(`\${@:3}`, ["a", "b", "c", "d"])).toBe("c d"); + }); + + test(`should slice with length (\${@:N:L})`, () => { + expect(substituteArgs(`\${@:2:2}`, ["a", "b", "c", "d"])).toBe("b c"); + expect(substituteArgs(`\${@:1:1}`, ["a", "b", "c"])).toBe("a"); + expect(substituteArgs(`\${@:3:1}`, ["a", "b", "c", "d"])).toBe("c"); + expect(substituteArgs(`\${@:2:3}`, ["a", "b", "c", "d", "e"])).toBe("b c d"); + }); + + test("should handle out of range slices", () => { + expect(substituteArgs(`\${@:99}`, ["a", "b"])).toBe(""); + expect(substituteArgs(`\${@:5}`, ["a", "b"])).toBe(""); + expect(substituteArgs(`\${@:10:5}`, ["a", "b"])).toBe(""); + }); + + test("should handle zero-length slices", () => { + expect(substituteArgs(`\${@:2:0}`, ["a", "b", "c"])).toBe(""); + expect(substituteArgs(`\${@:1:0}`, ["a", "b"])).toBe(""); + }); + + test("should handle length exceeding array", () => { + expect(substituteArgs(`\${@:2:99}`, ["a", "b", "c"])).toBe("b c"); + expect(substituteArgs(`\${@:1:10}`, ["a", "b"])).toBe("a b"); + }); + + test("should process slice before simple $@", () => { + expect(substituteArgs(`\${@:2} vs $@`, ["a", "b", "c"])).toBe("b c vs a b c"); + expect(substituteArgs(`First: \${@:1:1}, All: $@`, ["x", "y", "z"])).toBe("First: x, All: x y z"); + }); + + test("should not recursively substitute slice patterns in args", () => { + expect(substituteArgs(`\${@:1}`, [`\${@:2}`, "test"])).toBe(`\${@:2} test`); + expect(substituteArgs(`\${@:2}`, ["a", `\${@:3}`, "c"])).toBe(`\${@:3} c`); + }); + + test("should handle mixed usage with positional args", () => { + expect(substituteArgs(`$1: \${@:2}`, ["cmd", "arg1", "arg2"])).toBe("cmd: arg1 arg2"); + expect(substituteArgs(`$1 $2 \${@:3}`, ["a", "b", "c", "d"])).toBe("a b c d"); + }); + + test(`should treat \${@:0} as all args`, () => { + expect(substituteArgs(`\${@:0}`, ["a", "b", "c"])).toBe("a b c"); + }); + + test("should handle empty args array", () => { + expect(substituteArgs(`\${@:2}`, [])).toBe(""); + expect(substituteArgs(`\${@:1}`, [])).toBe(""); + }); + + test("should handle single arg array", () => { + expect(substituteArgs(`\${@:1}`, ["only"])).toBe("only"); + expect(substituteArgs(`\${@:2}`, ["only"])).toBe(""); + }); + + test("should handle slice in middle of text", () => { + expect(substituteArgs(`Process \${@:2} with $1`, ["tool", "file1", "file2"])).toBe( + "Process file1 file2 with tool", + ); + }); + + test("should handle multiple slices in one template", () => { + expect(substituteArgs(`\${@:1:1} and \${@:2}`, ["a", "b", "c"])).toBe("a and b c"); + expect(substituteArgs(`\${@:1:2} vs \${@:3:2}`, ["a", "b", "c", "d", "e"])).toBe("a b vs c d"); + }); + + test("should handle quoted arguments in slices", () => { + expect(substituteArgs(`\${@:2}`, ["cmd", "first arg", "second arg"])).toBe("first arg second arg"); + }); + + test("should handle special characters in sliced args", () => { + expect(substituteArgs(`\${@:2}`, ["cmd", "$100", "@user", "#tag"])).toBe("$100 @user #tag"); + }); + + test("should handle unicode in sliced args", () => { + expect(substituteArgs(`\${@:1}`, ["日本語", "🎉", "café"])).toBe("日本語 🎉 café"); + }); + + test("should combine positional, slice, and wildcard placeholders", () => { + const template = `Run $1 on \${@:2:2}, then process $@`; + const args = ["eslint", "file1.ts", "file2.ts", "file3.ts"]; + expect(substituteArgs(template, args)).toBe( + "Run eslint on file1.ts file2.ts, then process eslint file1.ts file2.ts file3.ts", + ); + }); + + test("should handle slice with no spacing", () => { + expect(substituteArgs(`prefix\${@:2}suffix`, ["a", "b", "c"])).toBe("prefixb csuffix"); + }); + + test("should handle large slice lengths gracefully", () => { + const args = Array.from({ length: 10 }, (_, i) => `arg${i + 1}`); + expect(substituteArgs(`\${@:5:100}`, args)).toBe("arg5 arg6 arg7 arg8 arg9 arg10"); + }); +}); + +// ============================================================================ +// parseCommandArgs +// ============================================================================ + +describe("parseCommandArgs", () => { + test("should parse simple space-separated arguments", () => { + expect(parseCommandArgs("a b c")).toEqual(["a", "b", "c"]); + }); + + test("should parse quoted arguments with spaces", () => { + expect(parseCommandArgs('"first arg" second')).toEqual(["first arg", "second"]); + }); + + test("should parse single-quoted arguments", () => { + expect(parseCommandArgs("'first arg' second")).toEqual(["first arg", "second"]); + }); + + test("should parse mixed quote styles", () => { + expect(parseCommandArgs('"double" \'single\' "double again"')).toEqual(["double", "single", "double again"]); + }); + + test("should handle empty string", () => { + expect(parseCommandArgs("")).toEqual([]); + }); + + test("should handle extra spaces", () => { + expect(parseCommandArgs("a b c")).toEqual(["a", "b", "c"]); + }); + + test("should handle tabs as separators", () => { + expect(parseCommandArgs("a\tb\tc")).toEqual(["a", "b", "c"]); + }); + + test("should handle quoted empty string", () => { + // Note: Empty quotes are skipped by current implementation + expect(parseCommandArgs('"" " "')).toEqual([" "]); + }); + + test("should handle arguments with special characters", () => { + expect(parseCommandArgs("$100 @user #tag")).toEqual(["$100", "@user", "#tag"]); + }); + + test("should handle unicode characters", () => { + expect(parseCommandArgs("日本語 🎉 café")).toEqual(["日本語", "🎉", "café"]); + }); + + test("should handle newlines in quoted arguments", () => { + expect(parseCommandArgs('"line1\nline2" second')).toEqual(["line1\nline2", "second"]); + }); + + test("should treat unquoted newlines as separators", () => { + expect(parseCommandArgs("label-2\n\nHere is some description #2.")).toEqual([ + "label-2", + "Here", + "is", + "some", + "description", + "#2.", + ]); + }); + + test("should collapse mixed unquoted whitespace", () => { + expect(parseCommandArgs("a\n\n\tb c")).toEqual(["a", "b", "c"]); + }); + + test("should handle escaped quotes inside quoted strings", () => { + // Note: This implementation doesn't handle escaped quotes - backslash is literal + expect(parseCommandArgs('"quoted \\"text\\""')).toEqual(["quoted \\text\\"]); + }); + + test("should handle trailing spaces", () => { + expect(parseCommandArgs("a b c ")).toEqual(["a", "b", "c"]); + }); + + test("should handle leading spaces", () => { + expect(parseCommandArgs(" a b c")).toEqual(["a", "b", "c"]); + }); +}); + +// ============================================================================ +// Integration +// ============================================================================ + +describe("expandPromptTemplate", () => { + test("should split template arguments on unquoted newlines", () => { + const result = expandPromptTemplate("/arg-test label-2\n\nHere is some description #2.", [ + { + name: "arg-test", + description: "test", + content: `- arg1: $1\n- rest: \${@:2}`, + sourceInfo: { path: "/tmp/arg-test.md", source: "local", scope: "temporary", origin: "top-level" }, + filePath: "/tmp/arg-test.md", + }, + ]); + + expect(result).toBe("- arg1: label-2\n- rest: Here is some description #2."); + }); + + test("should support template command separated from args by newline", () => { + const result = expandPromptTemplate("/arg-test\nlabel-2", [ + { + name: "arg-test", + description: "test", + content: "arg1: $1", + sourceInfo: { path: "/tmp/arg-test.md", source: "local", scope: "temporary", origin: "top-level" }, + filePath: "/tmp/arg-test.md", + }, + ]); + + expect(result).toBe("arg1: label-2"); + }); +}); + +// ============================================================================ +// Integration +// ============================================================================ + +describe("parseCommandArgs + substituteArgs integration", () => { + test("should parse and substitute together correctly", () => { + const input = 'Button "onClick handler" "disabled support"'; + const args = parseCommandArgs(input); + const template = "Create component $1 with features: $ARGUMENTS"; + const result = substituteArgs(template, args); + expect(result).toBe("Create component Button with features: Button onClick handler disabled support"); + }); + + test("should handle the example from README", () => { + const input = 'Button "onClick handler" "disabled support"'; + const args = parseCommandArgs(input); + const template = "Create a React component named $1 with features: $ARGUMENTS"; + const result = substituteArgs(template, args); + expect(result).toBe( + "Create a React component named Button with features: Button onClick handler disabled support", + ); + }); + + test("should produce same result with $@ and $ARGUMENTS", () => { + const args = parseCommandArgs("feature1 feature2 feature3"); + const template1 = "Implement: $@"; + const template2 = "Implement: $ARGUMENTS"; + expect(substituteArgs(template1, args)).toBe(substituteArgs(template2, args)); + }); +}); + +// ============================================================================ +// loadPromptTemplates - argument-hint frontmatter +// ============================================================================ + +describe("loadPromptTemplates - argument-hint", () => { + const testDir = join(tmpdir(), `pi-test-prompts-${Date.now()}`); + + function writeTemplate(name: string, content: string) { + mkdirSync(testDir, { recursive: true }); + writeFileSync(join(testDir, `${name}.md`), content); + } + + test("should parse required argument-hint from frontmatter", () => { + writeTemplate( + "pr", + `--- +description: Review PRs from URLs with structured issue and code analysis +argument-hint: "" +--- +You are given one or more GitHub PR URLs: $@`, + ); + + const templates = loadPromptTemplates({ + cwd: process.cwd(), + agentDir: getAgentDir(), + promptPaths: [testDir], + includeDefaults: false, + }); + + const pr = templates.find((t) => t.name === "pr"); + expect(pr).toBeDefined(); + expect(pr!.argumentHint).toBe(""); + expect(pr!.description).toBe("Review PRs from URLs with structured issue and code analysis"); + }); + + test("should parse optional argument-hint from frontmatter", () => { + writeTemplate( + "wr", + `--- +description: Finish the current task end-to-end with changelog, commit, and push +argument-hint: "[instructions]" +--- +Wrap it. Additional instructions: $ARGUMENTS`, + ); + + const templates = loadPromptTemplates({ + cwd: process.cwd(), + agentDir: getAgentDir(), + promptPaths: [testDir], + includeDefaults: false, + }); + + const wr = templates.find((t) => t.name === "wr"); + expect(wr).toBeDefined(); + expect(wr!.argumentHint).toBe("[instructions]"); + expect(wr!.description).toBe("Finish the current task end-to-end with changelog, commit, and push"); + }); + + test("should leave argumentHint undefined when not specified", () => { + writeTemplate( + "cl", + `--- +description: Audit changelog entries before release +--- +Audit changelog entries for all commits since the last release.`, + ); + + const templates = loadPromptTemplates({ + cwd: process.cwd(), + agentDir: getAgentDir(), + promptPaths: [testDir], + includeDefaults: false, + }); + + const cl = templates.find((t) => t.name === "cl"); + expect(cl).toBeDefined(); + expect(cl!.argumentHint).toBeUndefined(); + }); + + test("should ignore empty argument-hint", () => { + writeTemplate( + "empty-hint", + `--- +description: A command with empty hint +argument-hint: "" +--- +Do something`, + ); + + const templates = loadPromptTemplates({ + cwd: process.cwd(), + agentDir: getAgentDir(), + promptPaths: [testDir], + includeDefaults: false, + }); + + const tmpl = templates.find((t) => t.name === "empty-hint"); + expect(tmpl).toBeDefined(); + expect(tmpl!.argumentHint).toBeUndefined(); + }); + + test("should preserve argument-hint with special characters", () => { + writeTemplate( + "is", + `--- +description: Analyze GitHub issues (bugs or feature requests) +argument-hint: "" +--- +Analyze GitHub issue(s): $ARGUMENTS`, + ); + + const templates = loadPromptTemplates({ + cwd: process.cwd(), + agentDir: getAgentDir(), + promptPaths: [testDir], + includeDefaults: false, + }); + + const is = templates.find((t) => t.name === "is"); + expect(is).toBeDefined(); + expect(is!.argumentHint).toBe(""); + }); + + afterAll(() => { + try { + rmSync(testDir, { recursive: true, force: true }); + } catch {} + }); +}); diff --git a/packages/coding-agent/test/radius.test.ts b/packages/coding-agent/test/radius.test.ts new file mode 100644 index 0000000000000000000000000000000000000000..cec1a15e88f94002da94690d08fde346112b5610 --- /dev/null +++ b/packages/coding-agent/test/radius.test.ts @@ -0,0 +1,171 @@ +import { existsSync, mkdirSync, rmSync, writeFileSync } from "node:fs"; +import { tmpdir } from "node:os"; +import { join } from "node:path"; +import { InMemoryModelsStore } from "@earendil-works/pi-ai"; +import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; +import { AuthStorage } from "../src/core/auth-storage.ts"; +import { ModelRuntime } from "../src/core/model-runtime.ts"; +import { RADIUS_PROVIDER_ID } from "../src/core/radius.ts"; +import { allowNetwork } from "./test-network-env.ts"; + +function radiusOAuthCredential(gatewayBaseUrl: string) { + return { + type: "oauth" as const, + access: "access-token", + refresh: "refresh-token", + expires: Date.now() + 60 * 60 * 1000, + gatewayConfig: radiusConfig(gatewayBaseUrl), + }; +} + +function radiusConfig(baseUrl: string) { + return { + baseUrl, + models: [ + { + id: "auto", + name: "Radius Auto", + reasoning: false, + input: ["text" as const], + cost: { input: 1, output: 2, cacheRead: 0.1, cacheWrite: 0.2 }, + contextWindow: 128000, + maxTokens: 16384, + }, + ], + }; +} + +let tempDir: string; + +beforeEach(() => { + allowNetwork(); + tempDir = join(tmpdir(), `pi-test-radius-${Date.now()}-${Math.random().toString(36).slice(2)}`); + mkdirSync(tempDir, { recursive: true }); +}); + +afterEach(() => { + vi.restoreAllMocks(); + if (tempDir && existsSync(tempDir)) rmSync(tempDir, { recursive: true }); +}); + +describe("Radius provider", () => { + it("restores the legacy credential catalog without network access", async () => { + const runtime = await ModelRuntime.create({ + credentials: AuthStorage.inMemory({ + [RADIUS_PROVIDER_ID]: radiusOAuthCredential("https://radius.example.com/v1"), + }), + modelsStore: new InMemoryModelsStore(), + modelsPath: null, + allowModelNetwork: false, + }); + + const model = runtime.getModel(RADIUS_PROVIDER_ID, "auto"); + expect(model).toMatchObject({ api: "pi-messages", baseUrl: "https://radius.example.com/v1" }); + expect(runtime.getProvider(RADIUS_PROVIDER_ID)?.name).toBe("Radius"); + expect(runtime.hasConfiguredAuth(RADIUS_PROVIDER_ID)).toBe(true); + }); + + it("fetches and stores the catalog for configured Radius auth", async () => { + vi.spyOn(globalThis, "fetch").mockImplementation( + async () => + new Response(JSON.stringify(radiusConfig("https://radius.example.com/v1")), { + status: 200, + headers: { "content-type": "application/json" }, + }), + ); + const modelsStore = new InMemoryModelsStore(); + const credentials = AuthStorage.inMemory({ + [RADIUS_PROVIDER_ID]: { + type: "oauth", + access: "access-token", + refresh: "refresh-token", + expires: Date.now() + 60 * 60 * 1000, + }, + }); + const runtime = await ModelRuntime.create({ + credentials, + modelsStore, + modelsPath: null, + allowModelNetwork: true, + }); + + expect(runtime.getModel(RADIUS_PROVIDER_ID, "auto")).toBeDefined(); + expect((await modelsStore.read(RADIUS_PROVIDER_ID))?.models).toHaveLength(1); + const radiusRequest = vi + .mocked(fetch) + .mock.calls.find(([url]) => String(url) === "https://radius.pi.dev/v1/config"); + expect(radiusRequest?.[1]?.headers).toMatchObject({ authorization: "Bearer access-token" }); + }); + + it("does not refresh catalogs over the network by default", async () => { + const fetchSpy = vi.spyOn(globalThis, "fetch").mockRejectedValue(new Error("unexpected catalog fetch")); + const runtime = await ModelRuntime.create({ + credentials: AuthStorage.inMemory({ + [RADIUS_PROVIDER_ID]: radiusOAuthCredential("https://radius.example.com/v1"), + }), + modelsStore: new InMemoryModelsStore(), + modelsPath: null, + }); + + expect(runtime.getModel(RADIUS_PROVIDER_ID, "auto")).toBeDefined(); + expect(fetchSpy).not.toHaveBeenCalled(); + }); + + it("does not fetch or expose Radius models without configured auth", async () => { + const fetchSpy = vi.spyOn(globalThis, "fetch"); + const runtime = await ModelRuntime.create({ + credentials: AuthStorage.inMemory(), + modelsStore: new InMemoryModelsStore(), + modelsPath: null, + allowModelNetwork: true, + }); + + expect(runtime.getModels(RADIUS_PROVIDER_ID)).toEqual([]); + expect(fetchSpy.mock.calls.some(([url]) => String(url).includes("radius.pi.dev/v1/config"))).toBe(false); + }); + + it("supports custom Radius gateways from models.json", async () => { + vi.spyOn(globalThis, "fetch").mockImplementation( + async () => new Response(JSON.stringify(radiusConfig("http://localhost:8788/v1")), { status: 200 }), + ); + const modelsPath = join(tempDir, "models.json"); + writeFileSync( + modelsPath, + JSON.stringify({ + providers: { "radius-dev": { name: "Radius (dev)", baseUrl: "http://localhost:8788", oauth: "radius" } }, + }), + ); + const runtime = await ModelRuntime.create({ + credentials: AuthStorage.inMemory({ + "radius-dev": { + type: "oauth", + access: "access-token", + refresh: "refresh-token", + expires: Date.now() + 60 * 60 * 1000, + }, + }), + modelsStore: new InMemoryModelsStore(), + modelsPath, + allowModelNetwork: true, + }); + + expect(runtime.getModel("radius-dev", "auto")).toMatchObject({ + api: "pi-messages", + baseUrl: "http://localhost:8788/v1", + }); + expect(runtime.getProvider("radius-dev")?.name).toBe("Radius (dev)"); + }); + + it("requires baseUrl for custom Radius gateways", async () => { + const modelsPath = join(tempDir, "models.json"); + writeFileSync(modelsPath, JSON.stringify({ providers: { "radius-dev": { oauth: "radius" } } })); + const runtime = await ModelRuntime.create({ + credentials: AuthStorage.inMemory(), + modelsStore: new InMemoryModelsStore(), + modelsPath, + allowModelNetwork: false, + }); + + expect(runtime.getError()).toContain('"baseUrl" is required when "oauth" is set'); + }); +}); diff --git a/packages/coding-agent/test/remote-catalog-provider.test.ts b/packages/coding-agent/test/remote-catalog-provider.test.ts new file mode 100644 index 0000000000000000000000000000000000000000..12044c0f9a54a2507de3e8ff0f40a383cb7955b7 --- /dev/null +++ b/packages/coding-agent/test/remote-catalog-provider.test.ts @@ -0,0 +1,241 @@ +import { + createModels, + createProvider, + InMemoryModelsStore, + type Model, + type ModelsPublication, + type Provider, + type RefreshModelsContext, +} from "@earendil-works/pi-ai"; +import { afterEach, describe, expect, it, vi } from "vitest"; +import { VERSION } from "../src/config.ts"; +import { withRemoteCatalog } from "../src/core/remote-catalog-provider.ts"; + +const neverAbortedSignal = new AbortController().signal; + +function model(id: string): Model<"openai-completions"> { + return { + id, + name: id, + api: "openai-completions", + provider: "test-provider", + baseUrl: "https://example.test/v1", + reasoning: false, + input: ["text"], + cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 }, + contextWindow: 1000, + maxTokens: 100, + }; +} + +function testProvider(localGeneratedAt?: number) { + return withRemoteCatalog( + createProvider({ + id: "test-provider", + auth: { apiKey: { name: "Test", resolve: async () => ({ auth: {} }) } }, + models: [model("static")], + api: { + stream: () => { + throw new Error("not used"); + }, + streamSimple: () => { + throw new Error("not used"); + }, + }, + }), + "https://pi.dev", + localGeneratedAt, + ); +} + +async function refreshProvider( + provider: Provider, + store: InMemoryModelsStore, + overrides: Partial> = {}, +): Promise { + const publish = async (publication: ModelsPublication): Promise => { + if (publication.persist === null) await store.delete(provider.id); + else if (publication.persist !== undefined) await store.write(provider.id, publication.persist); + publication.update?.(); + return true; + }; + await provider.refreshModels?.({ + credential: { type: "api_key" }, + stored: await store.read(provider.id), + publish, + allowNetwork: overrides.allowNetwork ?? true, + force: overrides.force, + signal: overrides.signal ?? neverAbortedSignal, + }); +} + +afterEach(() => vi.restoreAllMocks()); + +describe("remote catalog provider", () => { + it("parses keyed catalogs, sends version headers, observes the refresh TTL, and supports forced refreshes", async () => { + const fetchSpy = vi.spyOn(globalThis, "fetch").mockImplementation( + async () => + new Response(JSON.stringify({ dynamic: model("dynamic") }), { + status: 200, + headers: { "content-type": "application/json" }, + }), + ); + const provider = testProvider(); + const store = new InMemoryModelsStore(); + await refreshProvider(provider, store); + await refreshProvider(provider, store); + await refreshProvider(provider, store, { force: true }); + + expect(provider.getModels().map((entry) => entry.id)).toEqual(["static", "dynamic"]); + expect((await store.read(provider.id))?.models.map((entry) => entry.id)).toEqual(["dynamic"]); + expect(fetchSpy).toHaveBeenCalledTimes(2); + expect(fetchSpy.mock.calls[0]?.[1]?.headers).toMatchObject({ + "User-Agent": expect.stringContaining(`pi/${VERSION}`), + }); + }); + + it("prefers the newer of the generated and remote catalogs", async () => { + const localGeneratedAt = Date.parse("2026-07-23T10:00:00.000Z"); + const newerHeader = new Date(localGeneratedAt + 60_000).toUTCString(); + const responses = [ + new Response(JSON.stringify({ old: model("old") }), { + headers: { "last-modified": new Date(localGeneratedAt - 60_000).toUTCString() }, + }), + new Response(JSON.stringify({ newer: model("newer") }), { + headers: { "last-modified": newerHeader }, + }), + ]; + vi.spyOn(globalThis, "fetch").mockImplementation(async () => responses.shift() as Response); + const provider = testProvider(localGeneratedAt); + const store = new InMemoryModelsStore(); + + await refreshProvider(provider, store); + expect(provider.getModels().map((entry) => entry.id)).toEqual(["static"]); + + await refreshProvider(provider, store, { force: true }); + expect(provider.getModels().map((entry) => entry.id)).toEqual(["static", "newer"]); + expect(await store.read(provider.id)).toMatchObject({ lastModified: Date.parse(newerHeader) }); + }); + + it("revalidates a stored catalog with its etag and keeps the overlay on 304", async () => { + const responses = [ + new Response(JSON.stringify({ dynamic: model("dynamic") }), { + headers: { "content-type": "application/json", etag: '"catalog-1"' }, + }), + new Response(null, { status: 304, headers: { etag: '"catalog-1"' } }), + ]; + const fetchSpy = vi.spyOn(globalThis, "fetch").mockImplementation(async () => responses.shift() as Response); + const provider = testProvider(); + const store = new InMemoryModelsStore(); + + await refreshProvider(provider, store); + expect(fetchSpy.mock.calls[0]?.[1]?.headers).not.toHaveProperty("if-none-match"); + expect(await store.read(provider.id)).toMatchObject({ etag: '"catalog-1"' }); + + const checkedAt = (await store.read(provider.id))?.checkedAt; + await refreshProvider(provider, store, { force: true }); + + expect(fetchSpy.mock.calls[1]?.[1]?.headers).toMatchObject({ "if-none-match": '"catalog-1"' }); + expect(provider.getModels().map((entry) => entry.id)).toEqual(["static", "dynamic"]); + const stored = await store.read(provider.id); + expect(stored?.models.map((entry) => entry.id)).toEqual(["dynamic"]); + expect(stored?.etag).toBe('"catalog-1"'); + expect(stored?.checkedAt).toBeGreaterThanOrEqual(checkedAt ?? 0); + }); + + it("drops a stale etag when the overlay becomes unavailable", async () => { + const responses = [ + new Response(JSON.stringify({ dynamic: model("dynamic") }), { + headers: { "content-type": "application/json", etag: '"catalog-1"' }, + }), + new Response("not implemented", { status: 501 }), + ]; + vi.spyOn(globalThis, "fetch").mockImplementation(async () => responses.shift() as Response); + const provider = testProvider(); + const store = new InMemoryModelsStore(); + + await refreshProvider(provider, store); + await refreshProvider(provider, store, { force: true }); + + expect((await store.read(provider.id))?.etag).toBeUndefined(); + }); + + it("keeps the etag and overlay after a transient failure", async () => { + const responses = [ + new Response(JSON.stringify({ dynamic: model("dynamic") }), { + headers: { "content-type": "application/json", etag: '"catalog-1"' }, + }), + new Response("rate limited", { status: 429 }), + new Response("rate limited", { status: 429 }), + new Response("rate limited", { status: 429 }), + new Response(null, { status: 304, headers: { etag: '"catalog-1"' } }), + ]; + const fetchSpy = vi.spyOn(globalThis, "fetch").mockImplementation(async () => responses.shift() as Response); + const provider = testProvider(); + const store = new InMemoryModelsStore(); + + await refreshProvider(provider, store); + await expect(refreshProvider(provider, store, { force: true })).rejects.toThrow(/429/); + + const stored = await store.read(provider.id); + expect(stored?.etag).toBe('"catalog-1"'); + expect(stored?.models.map((entry) => entry.id)).toEqual(["dynamic"]); + + await refreshProvider(provider, store, { force: true }); + expect(fetchSpy.mock.calls[4]?.[1]?.headers).toMatchObject({ "if-none-match": '"catalog-1"' }); + expect(provider.getModels().map((entry) => entry.id)).toEqual(["static", "dynamic"]); + }); + + it("lets a newer catalog request bypass a stalled older request without stale publication", async () => { + let calls = 0; + let markFirstStarted: (() => void) | undefined; + let finishFirst: ((response: Response) => void) | undefined; + const firstStarted = new Promise((resolve) => { + markFirstStarted = resolve; + }); + const firstResponse = new Promise((resolve) => { + finishFirst = resolve; + }); + vi.spyOn(globalThis, "fetch").mockImplementation(async () => { + calls++; + if (calls === 1) { + markFirstStarted?.(); + return firstResponse; + } + return new Response(JSON.stringify({ newer: model("newer") }), { + headers: { "content-type": "application/json" }, + }); + }); + const provider = testProvider(); + const store = new InMemoryModelsStore(); + const models = createModels({ modelsStore: store }); + models.setProvider(provider); + + const first = models.refresh({ providers: [provider.id], force: true }); + await firstStarted; + const second = models.refresh({ providers: [provider.id], force: true }); + await second; + await first; + expect(provider.getModels().map((entry) => entry.id)).toEqual(["static", "newer"]); + + finishFirst?.( + new Response(JSON.stringify({ older: model("older") }), { + headers: { "content-type": "application/json" }, + }), + ); + await new Promise((resolve) => setTimeout(resolve, 0)); + + expect(provider.getModels().map((entry) => entry.id)).toEqual(["static", "newer"]); + expect((await store.read(provider.id))?.models.map((entry) => entry.id)).toEqual(["newer"]); + }); + + it("treats unimplemented pi.dev catalog routes as an unavailable overlay", async () => { + vi.spyOn(globalThis, "fetch").mockResolvedValue(new Response("not implemented", { status: 501 })); + const provider = testProvider(); + const store = new InMemoryModelsStore(); + + await expect(refreshProvider(provider, store)).resolves.toBeUndefined(); + expect(provider.getModels().map((entry) => entry.id)).toEqual(["static"]); + expect(await store.read(provider.id)).toMatchObject({ models: [], checkedAt: expect.any(Number) }); + }); +}); diff --git a/packages/coding-agent/test/resolve-config-value.test.ts b/packages/coding-agent/test/resolve-config-value.test.ts new file mode 100644 index 0000000000000000000000000000000000000000..b202f1c89cfa93afe68c3a19df7ca6206b05d4b6 --- /dev/null +++ b/packages/coding-agent/test/resolve-config-value.test.ts @@ -0,0 +1,121 @@ +import { existsSync, mkdirSync, readFileSync, rmSync, writeFileSync } from "node:fs"; +import { tmpdir } from "node:os"; +import { join } from "node:path"; +import { afterEach, beforeEach, describe, expect, test, vi } from "vitest"; +import { + clearConfigValueCache, + resolveConfigValue, + resolveConfigValueUncached, +} from "../src/core/resolve-config-value.ts"; +import * as shellModule from "../src/utils/shell.ts"; + +describe("resolveConfigValue", () => { + let tempDir: string; + + beforeEach(() => { + tempDir = join(tmpdir(), `pi-config-value-${Date.now()}-${Math.random().toString(36).slice(2)}`); + mkdirSync(tempDir, { recursive: true }); + clearConfigValueCache(); + }); + + afterEach(() => { + if (existsSync(tempDir)) rmSync(tempDir, { recursive: true }); + clearConfigValueCache(); + vi.restoreAllMocks(); + }); + + test("resolves literals, environment templates, and escapes", () => { + process.env.TEST_CONFIG_LEFT = "left"; + process.env.TEST_CONFIG_RIGHT = "right"; + try { + expect(resolveConfigValue("literal-key")).toBe("literal-key"); + expect(resolveConfigValue("$TEST_CONFIG_LEFT")).toBe("left"); + expect(resolveConfigValue("$" + "{TEST_CONFIG_LEFT}_$TEST_CONFIG_RIGHT")).toBe("left_right"); + expect(resolveConfigValue("$$TEST_CONFIG_LEFT")).toBe("$TEST_CONFIG_LEFT"); + expect(resolveConfigValue("$!literal-$TEST_CONFIG_RIGHT")).toBe("!literal-right"); + } finally { + delete process.env.TEST_CONFIG_LEFT; + delete process.env.TEST_CONFIG_RIGHT; + } + }); + + test("uses credential-scoped environment before process.env", () => { + process.env.TEST_CONFIG_SCOPED = "process"; + try { + expect(resolveConfigValue("$TEST_CONFIG_SCOPED", { TEST_CONFIG_SCOPED: "credential" })).toBe("credential"); + } finally { + delete process.env.TEST_CONFIG_SCOPED; + } + }); + + test("executes shell commands and trims their output", () => { + expect(resolveConfigValue("!echo ' spaced-key '")).toBe("spaced-key"); + expect(resolveConfigValue("!printf 'line1\\nline2'")).toBe("line1\nline2"); + expect(resolveConfigValue("!echo 'hello world' | tr ' ' '-'")).toBe("hello-world"); + }); + + test.each(["!exit 1", "!nonexistent-command-12345", "!printf ''"])( + "returns undefined when command resolution fails: %s", + (command) => { + expect(resolveConfigValue(command)).toBeUndefined(); + }, + ); + + test("caches successful and failed commands until explicitly cleared", () => { + const counterFile = join(tempDir, "counter"); + writeFileSync(counterFile, "0"); + const escapedPath = counterFile.replace(/\\/g, "/").replace(/"/g, '\\"'); + const success = `!sh -c 'count=$(cat "${escapedPath}"); echo $((count + 1)) > "${escapedPath}"; echo value'`; + + expect(resolveConfigValue(success)).toBe("value"); + expect(resolveConfigValue(success)).toBe("value"); + expect(readFileSync(counterFile, "utf-8").trim()).toBe("1"); + + clearConfigValueCache(); + expect(resolveConfigValue(success)).toBe("value"); + expect(readFileSync(counterFile, "utf-8").trim()).toBe("2"); + + const failure = `!sh -c 'count=$(cat "${escapedPath}"); echo $((count + 1)) > "${escapedPath}"; exit 1'`; + expect(resolveConfigValue(failure)).toBeUndefined(); + expect(resolveConfigValue(failure)).toBeUndefined(); + expect(readFileSync(counterFile, "utf-8").trim()).toBe("3"); + }); + + test("does not cache environment values", () => { + process.env.TEST_CONFIG_DYNAMIC = "first"; + try { + expect(resolveConfigValue("$TEST_CONFIG_DYNAMIC")).toBe("first"); + process.env.TEST_CONFIG_DYNAMIC = "second"; + expect(resolveConfigValue("$TEST_CONFIG_DYNAMIC")).toBe("second"); + } finally { + delete process.env.TEST_CONFIG_DYNAMIC; + } + }); + + test("uncached resolution executes a command on every call", () => { + const counterFile = join(tempDir, "uncached-counter"); + writeFileSync(counterFile, "0"); + const escapedPath = counterFile.replace(/\\/g, "/").replace(/"/g, '\\"'); + const command = `!sh -c 'count=$(cat "${escapedPath}"); echo $((count + 1)) > "${escapedPath}"; echo value'`; + expect(resolveConfigValueUncached(command)).toBe("value"); + expect(resolveConfigValueUncached(command)).toBe("value"); + expect(readFileSync(counterFile, "utf-8").trim()).toBe("2"); + }); + + test("uses stdin when the configured Windows shell requires it", () => { + if (process.platform === "win32") return; + const platformDescriptor = Object.getOwnPropertyDescriptor(process, "platform"); + vi.spyOn(shellModule, "getShellConfig").mockReturnValue({ + shell: "/bin/bash", + args: ["-s"], + commandTransport: "stdin", + }); + try { + Object.defineProperty(process, "platform", { configurable: true, value: "win32" }); + const expansion = "$" + "{name}"; + expect(resolveConfigValueUncached(`!name='World'; echo "Hello, ${expansion}!"`)).toBe("Hello, World!"); + } finally { + if (platformDescriptor) Object.defineProperty(process, "platform", platformDescriptor); + } + }); +}); diff --git a/packages/coding-agent/test/resource-loader.test.ts b/packages/coding-agent/test/resource-loader.test.ts new file mode 100644 index 0000000000000000000000000000000000000000..145eca3b2510dd24d49cb85134ed0cee37fb0999 --- /dev/null +++ b/packages/coding-agent/test/resource-loader.test.ts @@ -0,0 +1,1121 @@ +import { mkdirSync, readFileSync, rmSync, symlinkSync, writeFileSync } from "node:fs"; +import { tmpdir } from "node:os"; +import { join } from "node:path"; +import { pathToFileURL } from "node:url"; +import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; +import { AuthStorage } from "../src/core/auth-storage.ts"; +import { ExtensionRunner } from "../src/core/extensions/runner.ts"; +import { DefaultResourceLoader, loadProjectContextFiles } from "../src/core/resource-loader.ts"; +import { SessionManager } from "../src/core/session-manager.ts"; +import { SettingsManager } from "../src/core/settings-manager.ts"; +import type { Skill } from "../src/core/skills.ts"; +import { createSyntheticSourceInfo } from "../src/core/source-info.ts"; + +import { createModelRegistry } from "./model-runtime-test-utils.ts"; + +describe("DefaultResourceLoader", () => { + let tempDir: string; + let agentDir: string; + let cwd: string; + + beforeEach(() => { + tempDir = join(tmpdir(), `rl-test-${Date.now()}-${Math.random().toString(36).slice(2)}`); + agentDir = join(tempDir, "agent"); + cwd = join(tempDir, "project"); + mkdirSync(agentDir, { recursive: true }); + mkdirSync(cwd, { recursive: true }); + }); + + afterEach(() => { + rmSync(tempDir, { recursive: true, force: true }); + }); + + describe("reload", () => { + it("should initialize with empty results before reload", () => { + const loader = new DefaultResourceLoader({ cwd, agentDir }); + + expect(loader.getExtensions().extensions).toEqual([]); + expect(loader.getSkills().skills).toEqual([]); + expect(loader.getPrompts().prompts).toEqual([]); + expect(loader.getThemes().themes).toEqual([]); + }); + + it("should discover skills from agentDir", async () => { + const skillsDir = join(agentDir, "skills"); + mkdirSync(skillsDir, { recursive: true }); + writeFileSync( + join(skillsDir, "test-skill.md"), + `--- +name: test-skill +description: A test skill +--- +Skill content here.`, + ); + + const loader = new DefaultResourceLoader({ cwd, agentDir }); + await loader.reload(); + + const { skills } = loader.getSkills(); + expect(skills.some((s) => s.name === "test-skill")).toBe(true); + }); + + it("should ignore extra markdown files in auto-discovered skill dirs", async () => { + const skillDir = join(agentDir, "skills", "pi-skills", "browser-tools"); + mkdirSync(skillDir, { recursive: true }); + writeFileSync( + join(skillDir, "SKILL.md"), + `--- +name: browser-tools +description: Browser tools +--- +Skill content here.`, + ); + writeFileSync(join(skillDir, "EFFICIENCY.md"), "No frontmatter here"); + + const loader = new DefaultResourceLoader({ cwd, agentDir }); + await loader.reload(); + + const { skills, diagnostics } = loader.getSkills(); + expect(skills.some((s) => s.name === "browser-tools")).toBe(true); + expect(diagnostics.some((d) => d.path?.endsWith("EFFICIENCY.md"))).toBe(false); + }); + + it("should discover prompts from agentDir", async () => { + const promptsDir = join(agentDir, "prompts"); + mkdirSync(promptsDir, { recursive: true }); + writeFileSync( + join(promptsDir, "test-prompt.md"), + `--- +description: A test prompt +--- +Prompt content.`, + ); + + const loader = new DefaultResourceLoader({ cwd, agentDir }); + await loader.reload(); + + const { prompts } = loader.getPrompts(); + expect(prompts.some((p) => p.name === "test-prompt")).toBe(true); + }); + + it("should prefer project resources over user on name collisions", async () => { + const userPromptsDir = join(agentDir, "prompts"); + const projectPromptsDir = join(cwd, ".pi", "prompts"); + mkdirSync(userPromptsDir, { recursive: true }); + mkdirSync(projectPromptsDir, { recursive: true }); + const userPromptPath = join(userPromptsDir, "commit.md"); + const projectPromptPath = join(projectPromptsDir, "commit.md"); + writeFileSync(userPromptPath, "User prompt"); + writeFileSync(projectPromptPath, "Project prompt"); + + const userSkillDir = join(agentDir, "skills", "collision-skill"); + const projectSkillDir = join(cwd, ".pi", "skills", "collision-skill"); + mkdirSync(userSkillDir, { recursive: true }); + mkdirSync(projectSkillDir, { recursive: true }); + const userSkillPath = join(userSkillDir, "SKILL.md"); + const projectSkillPath = join(projectSkillDir, "SKILL.md"); + writeFileSync( + userSkillPath, + `--- +name: collision-skill +description: user +--- +User skill`, + ); + writeFileSync( + projectSkillPath, + `--- +name: collision-skill +description: project +--- +Project skill`, + ); + + const baseTheme = JSON.parse( + readFileSync(join(process.cwd(), "src", "modes", "interactive", "theme", "dark.json"), "utf-8"), + ) as { name: string; vars?: Record }; + baseTheme.name = "collision-theme"; + const userThemePath = join(agentDir, "themes", "collision.json"); + const projectThemePath = join(cwd, ".pi", "themes", "collision.json"); + mkdirSync(join(agentDir, "themes"), { recursive: true }); + mkdirSync(join(cwd, ".pi", "themes"), { recursive: true }); + writeFileSync(userThemePath, JSON.stringify(baseTheme, null, 2)); + if (baseTheme.vars) { + baseTheme.vars.accent = "#ff00ff"; + } + writeFileSync(projectThemePath, JSON.stringify(baseTheme, null, 2)); + + const loader = new DefaultResourceLoader({ cwd, agentDir }); + await loader.reload(); + + const prompt = loader.getPrompts().prompts.find((p) => p.name === "commit"); + expect(prompt?.filePath).toBe(projectPromptPath); + + const skill = loader.getSkills().skills.find((s) => s.name === "collision-skill"); + expect(skill?.filePath).toBe(projectSkillPath); + + const theme = loader.getThemes().themes.find((t) => t.name === "collision-theme"); + expect(theme?.sourcePath).toBe(projectThemePath); + }); + + it("should load symlinked user and project extensions once", async () => { + const sharedExtDir = join(tempDir, "shared-extensions"); + mkdirSync(sharedExtDir, { recursive: true }); + writeFileSync( + join(sharedExtDir, "shared.ts"), + `export default function(pi) { + pi.registerCommand("shared", { + description: "shared command", + handler: async () => {}, + }); +}`, + ); + + mkdirSync(agentDir, { recursive: true }); + mkdirSync(join(cwd, ".pi"), { recursive: true }); + symlinkSync(sharedExtDir, join(agentDir, "extensions"), "dir"); + symlinkSync(sharedExtDir, join(cwd, ".pi", "extensions"), "dir"); + + const loader = new DefaultResourceLoader({ cwd, agentDir }); + await loader.reload(); + + const extensionsResult = loader.getExtensions(); + expect(extensionsResult.extensions).toHaveLength(1); + expect(extensionsResult.errors).toEqual([]); + + // mergePaths processes project paths before user paths, so the project + // alias is the canonical survivor. + expect(extensionsResult.extensions[0].path).toBe(join(cwd, ".pi", "extensions", "shared.ts")); + }); + + it("should load user extensions before trust and reuse them after trust resolves", async () => { + const userExtDir = join(agentDir, "extensions"); + const projectExtDir = join(cwd, ".pi", "extensions"); + mkdirSync(userExtDir, { recursive: true }); + mkdirSync(projectExtDir, { recursive: true }); + const loadCountKey = `__piTrustPreloadCount_${Date.now()}_${Math.random().toString(36).slice(2)}`; + const globalState = globalThis as typeof globalThis & Record; + + writeFileSync( + join(userExtDir, "user.ts"), + `globalThis[${JSON.stringify(loadCountKey)}] = (globalThis[${JSON.stringify(loadCountKey)}] ?? 0) + 1; +export default function(pi) { + pi.on("project_trust", () => ({ trusted: "yes" })); + pi.registerCommand("user-trust", { + description: "user trust", + handler: async () => {}, + }); +}`, + ); + writeFileSync( + join(projectExtDir, "project.ts"), + `export default function(pi) { + pi.registerCommand("project-trusted", { + description: "project trusted", + handler: async () => {}, + }); +}`, + ); + + const loader = new DefaultResourceLoader({ cwd, agentDir }); + await loader.reload({ + resolveProjectTrust: async ({ extensionsResult }) => { + expect(extensionsResult.extensions.map((extension) => extension.path)).toEqual([ + join(userExtDir, "user.ts"), + ]); + return true; + }, + }); + + const extensionsResult = loader.getExtensions(); + expect(extensionsResult.extensions.map((extension) => extension.path)).toEqual([ + join(cwd, ".pi", "extensions", "project.ts"), + join(userExtDir, "user.ts"), + ]); + expect(globalState[loadCountKey]).toBe(1); + }); + + it("should keep both extensions loaded when command names collide", async () => { + const userExtDir = join(agentDir, "extensions"); + const projectExtDir = join(cwd, ".pi", "extensions"); + mkdirSync(userExtDir, { recursive: true }); + mkdirSync(projectExtDir, { recursive: true }); + + writeFileSync( + join(projectExtDir, "project.ts"), + `export default function(pi) { + pi.registerCommand("deploy", { + description: "project deploy", + handler: async () => {}, + }); + pi.registerCommand("project-only", { + description: "project only", + handler: async () => {}, + }); +}`, + ); + + writeFileSync( + join(userExtDir, "user.ts"), + `export default function(pi) { + pi.registerCommand("deploy", { + description: "user deploy", + handler: async () => {}, + }); + pi.registerCommand("user-only", { + description: "user only", + handler: async () => {}, + }); +}`, + ); + + const loader = new DefaultResourceLoader({ cwd, agentDir }); + await loader.reload(); + + const extensionsResult = loader.getExtensions(); + expect(extensionsResult.extensions).toHaveLength(2); + expect(extensionsResult.errors.some((e) => e.error.includes('Command "/deploy" conflicts'))).toBe(false); + + const sessionManager = SessionManager.inMemory(); + const authStorage = AuthStorage.create(join(tempDir, "auth.json")); + const modelRegistry = await createModelRegistry(authStorage); + const runner = new ExtensionRunner( + extensionsResult.extensions, + extensionsResult.runtime, + cwd, + sessionManager, + modelRegistry, + ); + + expect(runner.getCommand("deploy:1")?.description).toBe("project deploy"); + expect(runner.getCommand("deploy:2")?.description).toBe("user deploy"); + expect(runner.getCommand("project-only")?.description).toBe("project only"); + expect(runner.getCommand("user-only")?.description).toBe("user only"); + + const commands = runner.getRegisteredCommands(); + expect(commands.map((command) => command.invocationName)).toEqual([ + "deploy:1", + "project-only", + "deploy:2", + "user-only", + ]); + }); + + it("should honor overrides for auto-discovered resources", async () => { + const settingsManager = SettingsManager.inMemory(); + settingsManager.setExtensionPaths(["-extensions/disabled.ts"]); + settingsManager.setSkillPaths(["-skills/skip-skill"]); + settingsManager.setPromptTemplatePaths(["-prompts/skip.md"]); + settingsManager.setThemePaths(["-themes/skip.json"]); + + const extensionsDir = join(agentDir, "extensions"); + mkdirSync(extensionsDir, { recursive: true }); + writeFileSync(join(extensionsDir, "disabled.ts"), "export default function() {}"); + + const skillDir = join(agentDir, "skills", "skip-skill"); + mkdirSync(skillDir, { recursive: true }); + writeFileSync( + join(skillDir, "SKILL.md"), + `--- +name: skip-skill +description: Skip me +--- +Content`, + ); + + const promptsDir = join(agentDir, "prompts"); + mkdirSync(promptsDir, { recursive: true }); + writeFileSync(join(promptsDir, "skip.md"), "Skip prompt"); + + const themesDir = join(agentDir, "themes"); + mkdirSync(themesDir, { recursive: true }); + writeFileSync(join(themesDir, "skip.json"), "{}"); + + const loader = new DefaultResourceLoader({ cwd, agentDir, settingsManager }); + await loader.reload(); + + const { extensions } = loader.getExtensions(); + const { skills } = loader.getSkills(); + const { prompts } = loader.getPrompts(); + const { themes } = loader.getThemes(); + + expect(extensions.some((e) => e.path.endsWith("disabled.ts"))).toBe(false); + expect(skills.some((s) => s.name === "skip-skill")).toBe(false); + expect(prompts.some((p) => p.name === "skip")).toBe(false); + expect(themes.some((t) => t.sourcePath?.endsWith("skip.json"))).toBe(false); + }); + + it("should discover AGENTS.md context files", async () => { + writeFileSync(join(cwd, "AGENTS.md"), "# Project Guidelines\n\nBe helpful."); + + const loader = new DefaultResourceLoader({ cwd, agentDir }); + await loader.reload(); + + const { agentsFiles } = loader.getAgentsFiles(); + expect(agentsFiles.some((f) => f.path.includes("AGENTS.md"))).toBe(true); + }); + + it("should prefer AGENTS.override.md within each directory while preserving ancestor layering", async () => { + const nestedCwd = join(cwd, "service"); + mkdirSync(nestedCwd); + writeFileSync(join(agentDir, "AGENTS.md"), "global instructions"); + writeFileSync(join(agentDir, "AGENTS.override.md"), "global override"); + writeFileSync(join(cwd, "AGENTS.md"), "project instructions"); + writeFileSync(join(nestedCwd, "AGENTS.md"), "service instructions"); + writeFileSync(join(nestedCwd, "AGENTS.override.md"), "service override"); + + const loader = new DefaultResourceLoader({ cwd: nestedCwd, agentDir }); + await loader.reload(); + + expect(loader.getAgentsFiles().agentsFiles).toEqual([ + { path: join(agentDir, "AGENTS.override.md"), content: "global override" }, + { path: join(cwd, "AGENTS.md"), content: "project instructions" }, + { path: join(nestedCwd, "AGENTS.override.md"), content: "service override" }, + ]); + }); + + it("should ignore context file candidates that are directories", async () => { + mkdirSync(join(cwd, "AGENTS.override.md")); + mkdirSync(join(cwd, "AGENTS.md")); + writeFileSync(join(cwd, "CLAUDE.md"), "Fallback instructions"); + const consoleError = vi.spyOn(console, "error").mockImplementation(() => {}); + + const loader = new DefaultResourceLoader({ cwd, agentDir }); + await loader.reload(); + + expect(loader.getAgentsFiles().agentsFiles).toContainEqual({ + path: join(cwd, "CLAUDE.md"), + content: "Fallback instructions", + }); + expect(consoleError).not.toHaveBeenCalledWith(expect.stringContaining(join(cwd, "AGENTS.md"))); + expect(consoleError).not.toHaveBeenCalledWith(expect.stringContaining(join(cwd, "AGENTS.override.md"))); + consoleError.mockRestore(); + }); + + it("should skip context file discovery when noContextFiles is true", async () => { + writeFileSync(join(cwd, "AGENTS.override.md"), "# Override Guidelines\n\nBe helpful."); + writeFileSync(join(cwd, "AGENTS.md"), "# Project Guidelines\n\nBe helpful."); + writeFileSync(join(cwd, "CLAUDE.md"), "# Claude Guidelines\n\nBe helpful."); + + const loader = new DefaultResourceLoader({ cwd, agentDir, noContextFiles: true }); + await loader.reload(); + + const { agentsFiles } = loader.getAgentsFiles(); + expect(agentsFiles).toEqual([]); + }); + + it("should discover SYSTEM.md from cwd/.pi", async () => { + const piDir = join(cwd, ".pi"); + mkdirSync(piDir, { recursive: true }); + writeFileSync(join(piDir, "SYSTEM.md"), "You are a helpful assistant."); + + const loader = new DefaultResourceLoader({ cwd, agentDir }); + await loader.reload(); + + expect(loader.getSystemPrompt()).toBe("You are a helpful assistant."); + }); + + it("should skip project resources that require trust when project is not trusted", async () => { + const piDir = join(cwd, ".pi"); + const extensionsDir = join(piDir, "extensions"); + const skillDir = join(piDir, "skills", "project-skill"); + const promptsDir = join(piDir, "prompts"); + const themesDir = join(piDir, "themes"); + mkdirSync(extensionsDir, { recursive: true }); + mkdirSync(skillDir, { recursive: true }); + mkdirSync(promptsDir, { recursive: true }); + mkdirSync(themesDir, { recursive: true }); + writeFileSync(join(piDir, "SYSTEM.md"), "Project system prompt."); + writeFileSync(join(agentDir, "SYSTEM.md"), "Global system prompt."); + writeFileSync(join(agentDir, "AGENTS.md"), "Global instructions"); + writeFileSync(join(cwd, "AGENTS.md"), "Project instructions"); + writeFileSync(join(extensionsDir, "project.ts"), `throw new Error("should not load");`); + writeFileSync( + join(skillDir, "SKILL.md"), + `--- +name: project-skill +description: Project skill +--- +Project skill content`, + ); + writeFileSync(join(promptsDir, "project.md"), "Project prompt"); + const themeData = JSON.parse( + readFileSync(join(process.cwd(), "src", "modes", "interactive", "theme", "dark.json"), "utf-8"), + ) as { name: string }; + themeData.name = "project-theme"; + writeFileSync(join(themesDir, "project.json"), JSON.stringify(themeData, null, 2)); + const settingsManager = SettingsManager.create(cwd, agentDir, { projectTrusted: false }); + + const loader = new DefaultResourceLoader({ cwd, agentDir, settingsManager }); + await loader.reload(); + + expect(loader.getSystemPrompt()).toBe("Global system prompt."); + expect(loader.getAgentsFiles().agentsFiles.some((file) => file.path === join(agentDir, "AGENTS.md"))).toBe( + true, + ); + expect(loader.getAgentsFiles().agentsFiles.some((file) => file.path === join(cwd, "AGENTS.md"))).toBe(true); + expect(loader.getExtensions().extensions).toHaveLength(0); + expect(loader.getExtensions().errors).toEqual([]); + expect(loader.getSkills().skills.some((skill) => skill.name === "project-skill")).toBe(false); + expect(loader.getPrompts().prompts.some((prompt) => prompt.name === "project")).toBe(false); + expect(loader.getThemes().themes.some((theme) => theme.name === "project-theme")).toBe(false); + }); + + it("should discover APPEND_SYSTEM.md", async () => { + const piDir = join(cwd, ".pi"); + mkdirSync(piDir, { recursive: true }); + writeFileSync(join(piDir, "APPEND_SYSTEM.md"), "Additional instructions."); + + const loader = new DefaultResourceLoader({ cwd, agentDir }); + await loader.reload(); + + expect(loader.getAppendSystemPrompt()).toContain("Additional instructions."); + }); + }); + + describe("system prompt sources", () => { + it("exposes discovered project SYSTEM.md as the system prompt source", async () => { + const piDir = join(cwd, ".pi"); + const systemPromptPath = join(piDir, "SYSTEM.md"); + mkdirSync(piDir, { recursive: true }); + writeFileSync(systemPromptPath, "Project system prompt."); + + const loader = new DefaultResourceLoader({ cwd, agentDir }); + await loader.reload(); + + expect(loader.getSystemPrompt()).toBe("Project system prompt."); + expect(loader.getSystemPromptSource()).toEqual({ path: systemPromptPath }); + }); + + it("exposes discovered global SYSTEM.md as the system prompt source", async () => { + const systemPromptPath = join(agentDir, "SYSTEM.md"); + writeFileSync(systemPromptPath, "Global system prompt."); + + const loader = new DefaultResourceLoader({ cwd, agentDir }); + await loader.reload(); + + expect(loader.getSystemPrompt()).toBe("Global system prompt."); + expect(loader.getSystemPromptSource()).toEqual({ path: systemPromptPath }); + }); + + it("does not expose literal system prompt text as a source", async () => { + const loader = new DefaultResourceLoader({ cwd, agentDir, systemPrompt: "Literal system prompt." }); + await loader.reload(); + + expect(loader.getSystemPrompt()).toBe("Literal system prompt."); + expect(loader.getSystemPromptSource()).toBeUndefined(); + }); + + it("exposes file-backed system prompt options as a source", async () => { + const systemPromptPath = join(tempDir, "custom-system.md"); + writeFileSync(systemPromptPath, "Custom system prompt."); + + const loader = new DefaultResourceLoader({ cwd, agentDir, systemPrompt: systemPromptPath }); + await loader.reload(); + + expect(loader.getSystemPrompt()).toBe("Custom system prompt."); + expect(loader.getSystemPromptSource()).toEqual({ path: systemPromptPath }); + }); + + it("exposes discovered APPEND_SYSTEM.md as an append system prompt source", async () => { + const piDir = join(cwd, ".pi"); + const appendSystemPromptPath = join(piDir, "APPEND_SYSTEM.md"); + mkdirSync(piDir, { recursive: true }); + writeFileSync(appendSystemPromptPath, "Project append prompt."); + + const loader = new DefaultResourceLoader({ cwd, agentDir }); + await loader.reload(); + + expect(loader.getAppendSystemPrompt()).toEqual(["Project append prompt."]); + expect(loader.getAppendSystemPromptSources()).toEqual([{ path: appendSystemPromptPath }]); + }); + + it("does not expose literal append system prompt text as a source", async () => { + const loader = new DefaultResourceLoader({ cwd, agentDir, appendSystemPrompt: ["Literal append prompt."] }); + await loader.reload(); + + expect(loader.getAppendSystemPrompt()).toEqual(["Literal append prompt."]); + expect(loader.getAppendSystemPromptSources()).toEqual([]); + }); + + it("only exposes file-backed append system prompt options as sources", async () => { + const appendSystemPromptPath = join(tempDir, "custom-append.md"); + writeFileSync(appendSystemPromptPath, "Custom append prompt."); + + const loader = new DefaultResourceLoader({ + cwd, + agentDir, + appendSystemPrompt: [appendSystemPromptPath, "Literal append prompt."], + }); + await loader.reload(); + + expect(loader.getAppendSystemPrompt()).toEqual(["Custom append prompt.", "Literal append prompt."]); + expect(loader.getAppendSystemPromptSources()).toEqual([{ path: appendSystemPromptPath }]); + }); + }); + + describe("extendResources", () => { + it("should load skills and prompts with extension metadata", async () => { + const extraSkillDir = join(tempDir, "extra-skills", "extra-skill"); + mkdirSync(extraSkillDir, { recursive: true }); + const skillPath = join(extraSkillDir, "SKILL.md"); + writeFileSync( + skillPath, + `--- +name: extra-skill +description: Extra skill +--- +Extra content`, + ); + + const extraPromptDir = join(tempDir, "extra-prompts"); + mkdirSync(extraPromptDir, { recursive: true }); + const promptPath = join(extraPromptDir, "extra.md"); + writeFileSync( + promptPath, + `--- +description: Extra prompt +--- +Extra prompt content`, + ); + + const loader = new DefaultResourceLoader({ cwd, agentDir }); + await loader.reload(); + + loader.extendResources({ + skillPaths: [ + { + path: extraSkillDir, + metadata: { + source: "extension:extra", + scope: "temporary", + origin: "top-level", + baseDir: extraSkillDir, + }, + }, + ], + promptPaths: [ + { + path: promptPath, + metadata: { + source: "extension:extra", + scope: "temporary", + origin: "top-level", + baseDir: extraPromptDir, + }, + }, + ], + }); + + const { skills } = loader.getSkills(); + const loadedSkill = skills.find((skill) => skill.name === "extra-skill"); + expect(loadedSkill).toBeDefined(); + expect(loadedSkill?.sourceInfo?.source).toBe("extension:extra"); + expect(loadedSkill?.sourceInfo?.path).toBe(skillPath); + + const { prompts } = loader.getPrompts(); + const loadedPrompt = prompts.find((prompt) => prompt.name === "extra"); + expect(loadedPrompt).toBeDefined(); + expect(loadedPrompt?.sourceInfo?.source).toBe("extension:extra"); + expect(loadedPrompt?.sourceInfo?.path).toBe(promptPath); + }); + + it("should load extension resources returned as file URLs", async () => { + const extraSkillDir = join(tempDir, "extra skills", "file-url-skill"); + mkdirSync(extraSkillDir, { recursive: true }); + const skillPath = join(extraSkillDir, "SKILL.md"); + writeFileSync( + skillPath, + `--- +name: file-url-skill +description: File URL skill +--- +Extra content`, + ); + + const loader = new DefaultResourceLoader({ cwd, agentDir }); + await loader.reload(); + + loader.extendResources({ + skillPaths: [ + { + path: pathToFileURL(extraSkillDir).href, + metadata: { + source: "extension:file-url", + scope: "temporary", + origin: "top-level", + baseDir: extraSkillDir, + }, + }, + ], + }); + + const { skills, diagnostics } = loader.getSkills(); + expect(diagnostics).toEqual([]); + const loadedSkill = skills.find((skill) => skill.name === "file-url-skill"); + expect(loadedSkill).toBeDefined(); + expect(loadedSkill?.filePath).toBe(skillPath); + expect(loadedSkill?.sourceInfo?.source).toBe("extension:file-url"); + }); + + // Regression: extension discovery used to drop package scope/source, collapsing every + // autocomplete source tag to [t]. See issue #6968. + it("should keep package metadata for skills, prompts, and themes", async () => { + const packageRoot = join(agentDir, "npm", "node_modules", "metadata-pkg"); + const packageSkillDir = join(packageRoot, "skills", "package-skill"); + const packagePromptsDir = join(packageRoot, "prompts"); + const packageThemesDir = join(packageRoot, "themes"); + mkdirSync(packageSkillDir, { recursive: true }); + mkdirSync(packagePromptsDir, { recursive: true }); + mkdirSync(packageThemesDir, { recursive: true }); + writeFileSync(join(packageRoot, "package.json"), JSON.stringify({ name: "metadata-pkg", version: "1.0.0" })); + writeFileSync( + join(packageSkillDir, "SKILL.md"), + `--- +name: package-skill +description: Package skill +--- +Package skill content`, + ); + writeFileSync( + join(packagePromptsDir, "package-prompt.md"), + `--- +description: Package prompt +--- +Package prompt content`, + ); + const baseTheme = JSON.parse( + readFileSync(join(process.cwd(), "src", "modes", "interactive", "theme", "dark.json"), "utf-8"), + ) as { name: string }; + writeFileSync( + join(packageThemesDir, "package-theme.json"), + JSON.stringify({ ...baseTheme, name: "package-theme" }), + ); + + const extensionResourceDir = join(tempDir, "extension-resources"); + const extensionSkillDir = join(extensionResourceDir, "extension-skill"); + const extensionPromptsDir = join(extensionResourceDir, "prompts"); + const extensionThemesDir = join(extensionResourceDir, "themes"); + mkdirSync(extensionSkillDir, { recursive: true }); + mkdirSync(extensionPromptsDir, { recursive: true }); + mkdirSync(extensionThemesDir, { recursive: true }); + writeFileSync( + join(extensionSkillDir, "SKILL.md"), + `--- +name: extension-skill +description: Extension skill +--- +Extension skill content`, + ); + writeFileSync( + join(extensionPromptsDir, "extension-prompt.md"), + `--- +description: Extension prompt +--- +Extension prompt content`, + ); + writeFileSync( + join(extensionThemesDir, "extension.json"), + JSON.stringify({ ...baseTheme, name: "extension-theme" }), + ); + + const loader = new DefaultResourceLoader({ + cwd, + agentDir, + settingsManager: SettingsManager.inMemory({ packages: ["npm:metadata-pkg"] }), + }); + await loader.reload(); + + const extensionMetadata = { + source: "extension:discovery", + scope: "temporary", + origin: "top-level", + } as const; + loader.extendResources({ + skillPaths: [{ path: extensionSkillDir, metadata: extensionMetadata }], + promptPaths: [{ path: extensionPromptsDir, metadata: extensionMetadata }], + themePaths: [{ path: extensionThemesDir, metadata: extensionMetadata }], + }); + + const packageSourceInfo = { source: "npm:metadata-pkg", scope: "user", origin: "package" }; + expect(loader.getSkills().skills.find((skill) => skill.name === "package-skill")?.sourceInfo).toMatchObject( + packageSourceInfo, + ); + expect( + loader.getPrompts().prompts.find((prompt) => prompt.name === "package-prompt")?.sourceInfo, + ).toMatchObject(packageSourceInfo); + expect(loader.getThemes().themes.find((theme) => theme.name === "package-theme")?.sourceInfo).toMatchObject( + packageSourceInfo, + ); + + expect(loader.getSkills().skills.find((skill) => skill.name === "extension-skill")?.sourceInfo).toMatchObject( + extensionMetadata, + ); + expect( + loader.getPrompts().prompts.find((prompt) => prompt.name === "extension-prompt")?.sourceInfo, + ).toMatchObject(extensionMetadata); + expect(loader.getThemes().themes.find((theme) => theme.name === "extension-theme")?.sourceInfo).toMatchObject( + extensionMetadata, + ); + }); + }); + + describe("noSkills option", () => { + it("should skip skill discovery when noSkills is true", async () => { + const skillsDir = join(agentDir, "skills"); + mkdirSync(skillsDir, { recursive: true }); + writeFileSync( + join(skillsDir, "test-skill.md"), + `--- +name: test-skill +description: A test skill +--- +Content`, + ); + + const loader = new DefaultResourceLoader({ cwd, agentDir, noSkills: true }); + await loader.reload(); + + const { skills } = loader.getSkills(); + expect(skills).toEqual([]); + }); + + it("should still load additional skill paths when noSkills is true", async () => { + const customSkillDir = join(tempDir, "custom-skills"); + mkdirSync(customSkillDir, { recursive: true }); + writeFileSync( + join(customSkillDir, "custom.md"), + `--- +name: custom +description: Custom skill +--- +Content`, + ); + + const loader = new DefaultResourceLoader({ + cwd, + agentDir, + noSkills: true, + additionalSkillPaths: [customSkillDir], + }); + await loader.reload(); + + const { skills } = loader.getSkills(); + expect(skills.some((s) => s.name === "custom")).toBe(true); + }); + }); + + describe("override functions", () => { + it("should apply skillsOverride", async () => { + const injectedSkill: Skill = { + name: "injected", + description: "Injected skill", + filePath: "/fake/path", + baseDir: "/fake", + sourceInfo: createSyntheticSourceInfo("/fake/path", { source: "custom" }), + disableModelInvocation: false, + }; + const loader = new DefaultResourceLoader({ + cwd, + agentDir, + skillsOverride: () => ({ + skills: [injectedSkill], + diagnostics: [], + }), + }); + await loader.reload(); + + const { skills } = loader.getSkills(); + expect(skills).toHaveLength(1); + expect(skills[0].name).toBe("injected"); + }); + + it("should apply systemPromptOverride", async () => { + const loader = new DefaultResourceLoader({ + cwd, + agentDir, + systemPromptOverride: () => "Custom system prompt", + }); + await loader.reload(); + + expect(loader.getSystemPrompt()).toBe("Custom system prompt"); + }); + }); + + describe("extension conflict detection", () => { + it("should detect tool conflicts between extensions", async () => { + // Create two extensions that register the same tool + const ext1Dir = join(agentDir, "extensions", "ext1"); + const ext2Dir = join(agentDir, "extensions", "ext2"); + mkdirSync(ext1Dir, { recursive: true }); + mkdirSync(ext2Dir, { recursive: true }); + + writeFileSync( + join(ext1Dir, "index.ts"), + ` +import type { ExtensionAPI } from "@earendil-works/pi-coding-agent"; +import { Type } from "typebox"; +export default function(pi: ExtensionAPI) { + pi.registerTool({ + name: "duplicate-tool", + description: "First", + parameters: Type.Object({}), + execute: async () => ({ result: "1" }), + }); +}`, + ); + + writeFileSync( + join(ext2Dir, "index.ts"), + ` +import type { ExtensionAPI } from "@earendil-works/pi-coding-agent"; +import { Type } from "typebox"; +export default function(pi: ExtensionAPI) { + pi.registerTool({ + name: "duplicate-tool", + description: "Second", + parameters: Type.Object({}), + execute: async () => ({ result: "2" }), + }); +}`, + ); + + const loader = new DefaultResourceLoader({ cwd, agentDir }); + await loader.reload(); + + const { errors } = loader.getExtensions(); + expect(errors.some((e) => e.error.includes("duplicate-tool") && e.error.includes("conflicts"))).toBe(true); + }); + + it("should prefer explicit CLI extensions over discovered extensions when commands and tools conflict", async () => { + const globalExtDir = join(agentDir, "extensions"); + mkdirSync(globalExtDir, { recursive: true }); + const explicitExtPath = join(tempDir, "explicit-extension.ts"); + + writeFileSync( + join(globalExtDir, "global.ts"), + ` +import type { ExtensionAPI } from "@earendil-works/pi-coding-agent"; +import { Type } from "typebox"; +export default function(pi: ExtensionAPI) { + pi.registerTool({ + name: "duplicate-tool", + description: "global tool", + parameters: Type.Object({}), + execute: async () => ({ result: "global" }), + }); + pi.registerCommand("deploy", { + description: "global command", + handler: async () => {}, + }); +}`, + ); + + writeFileSync( + explicitExtPath, + ` +import type { ExtensionAPI } from "@earendil-works/pi-coding-agent"; +import { Type } from "typebox"; +export default function(pi: ExtensionAPI) { + pi.registerTool({ + name: "duplicate-tool", + description: "explicit tool", + parameters: Type.Object({}), + execute: async () => ({ result: "explicit" }), + }); + pi.registerCommand("deploy", { + description: "explicit command", + handler: async () => {}, + }); +}`, + ); + + const loader = new DefaultResourceLoader({ + cwd, + agentDir, + additionalExtensionPaths: [explicitExtPath], + }); + await loader.reload(); + + const extensionsResult = loader.getExtensions(); + expect(extensionsResult.extensions[0]?.path).toBe(explicitExtPath); + + const sessionManager = SessionManager.inMemory(); + const authStorage = AuthStorage.create(join(tempDir, "auth-explicit.json")); + const modelRegistry = await createModelRegistry(authStorage); + const runner = new ExtensionRunner( + extensionsResult.extensions, + extensionsResult.runtime, + cwd, + sessionManager, + modelRegistry, + ); + + expect(runner.getCommand("deploy:1")?.description).toBe("explicit command"); + expect(runner.getCommand("deploy:2")?.description).toBe("global command"); + expect(runner.getToolDefinition("duplicate-tool")?.description).toBe("explicit tool"); + }); + }); + + describe("loadProjectContextFiles - nested worktree dedup", () => { + // Builds a linked-worktree skeleton (no git binary needed): the main repo's + // `.git/worktrees//` holds `HEAD` plus a `commondir` pointing back at the + // main `.git`, and the worktree's working tree carries a `.git` *file* whose + // `gitdir:` resolves to it. + const linkWorktree = (mainDir: string, worktreeDir: string, name: string) => { + const gitDir = join(mainDir, ".git", "worktrees", name); + mkdirSync(gitDir, { recursive: true }); + // The main repo's own `.git` is a real git dir with a HEAD, as git writes it. + writeFileSync(join(mainDir, ".git", "HEAD"), "ref: refs/heads/main\n"); + writeFileSync(join(gitDir, "HEAD"), "ref: refs/heads/feat\n"); + // commondir is relative to the worktree gitdir and points at the main .git. + writeFileSync(join(gitDir, "commondir"), "../.."); + writeFileSync(join(worktreeDir, ".git"), `gitdir: ${gitDir}\n`); + }; + + // Main repo at /outer/main with a linked worktree at main/worktrees/feat. + // Each case writes only the AGENTS.md files it needs. + const setupNestedWorktree = () => { + const outer = join(tempDir, "outer"); + const main = join(outer, "main"); + const worktree = join(main, "worktrees", "feat"); + const worktreeSrc = join(worktree, "src"); + mkdirSync(worktreeSrc, { recursive: true }); + linkWorktree(main, worktree, "feat"); + return { outer, main, worktree, worktreeSrc }; + }; + + it("should skip the main repo's duplicate when the worktree root has its own context", () => { + const { main, worktree, worktreeSrc } = setupNestedWorktree(); + writeFileSync(join(main, "AGENTS.md"), "main repo instructions"); + writeFileSync(join(worktree, "AGENTS.md"), "worktree instructions"); + + const files = loadProjectContextFiles({ cwd: worktreeSrc, agentDir }); + + expect(files.map((f) => f.content)).toEqual(["worktree instructions"]); + }); + + it("should still inherit the main repo's context when the worktree root has none", () => { + const { main, worktreeSrc } = setupNestedWorktree(); + writeFileSync(join(main, "AGENTS.md"), "main repo instructions"); + + const files = loadProjectContextFiles({ cwd: worktreeSrc, agentDir }); + + expect(files.map((f) => f.content)).toEqual(["main repo instructions"]); + }); + + it("should only skip the same filename, not a differently named context file", () => { + // The repo tracks CLAUDE.md; the worktree adds an AGENTS.md, which + // loadContextFileFromDir prefers. The main repo's CLAUDE.md is nobody's + // duplicate, so dropping it would lose its content entirely. + const { main, worktree, worktreeSrc } = setupNestedWorktree(); + writeFileSync(join(main, "CLAUDE.md"), "main repo instructions"); + writeFileSync(join(worktree, "AGENTS.md"), "worktree instructions"); + + const files = loadProjectContextFiles({ cwd: worktreeSrc, agentDir }); + + expect(files.map((f) => f.content)).toEqual(["main repo instructions", "worktree instructions"]); + }); + + it("should NOT skip the container's context in a bare layout (proj/.bare + proj/main)", () => { + // `git clone --bare proj/.bare` + `git worktree add ../main` makes commondir + // `../..`, so dirname(commonGitDir) is `proj` - a plain directory that tracks + // nothing. Its AGENTS.md is not a duplicate of the worktree's. Layout below + // matches what real git writes for this setup. + const proj = join(tempDir, "proj"); + const bare = join(proj, ".bare"); + const worktree = join(proj, "main"); + const worktreeGitDir = join(bare, "worktrees", "main"); + mkdirSync(worktreeGitDir, { recursive: true }); + mkdirSync(worktree, { recursive: true }); + writeFileSync(join(bare, "HEAD"), "ref: refs/heads/main\n"); + writeFileSync(join(worktreeGitDir, "HEAD"), "ref: refs/heads/main\n"); + writeFileSync(join(worktreeGitDir, "commondir"), "../.."); + writeFileSync(join(worktree, ".git"), `gitdir: ${worktreeGitDir}\n`); + writeFileSync(join(proj, "AGENTS.md"), "container instructions"); + writeFileSync(join(worktree, "AGENTS.md"), "worktree instructions"); + + const files = loadProjectContextFiles({ cwd: worktree, agentDir }); + + expect(files.map((f) => f.content)).toEqual(["container instructions", "worktree instructions"]); + }); + + it("should keep loading ancestors above the main repo", () => { + const { outer, main, worktree, worktreeSrc } = setupNestedWorktree(); + writeFileSync(join(outer, "AGENTS.md"), "outer instructions"); + writeFileSync(join(main, "AGENTS.md"), "main repo instructions"); + writeFileSync(join(worktree, "AGENTS.md"), "worktree instructions"); + + const files = loadProjectContextFiles({ cwd: worktreeSrc, agentDir }); + + // Only the main repo root's duplicate is dropped; the unrelated dir above it stays. + expect(files.map((f) => f.content)).toEqual(["outer instructions", "worktree instructions"]); + }); + + it("should NOT skip anything for a sibling worktree (main repo is not an ancestor)", () => { + // git worktree add ../feat puts the worktree beside the main repo, so no + // duplicate is ever encountered and ancestors above it are unrelated. + const outer = join(tempDir, "outer"); + const main = join(outer, "main"); + const sib = join(outer, "sib-feat"); + const sibSrc = join(sib, "src"); + mkdirSync(sibSrc, { recursive: true }); + mkdirSync(main, { recursive: true }); + writeFileSync(join(outer, "AGENTS.md"), "outer instructions"); + writeFileSync(join(sib, "AGENTS.md"), "sibling worktree instructions"); + linkWorktree(main, sib, "sib"); + + const files = loadProjectContextFiles({ cwd: sibSrc, agentDir }); + + expect(files.map((f) => f.content)).toEqual(["outer instructions", "sibling worktree instructions"]); + }); + + it("should NOT skip the superproject's context from inside a submodule", () => { + // A submodule's `.git` file is also `gitdir:`-style, but its gitdir has no + // commondir, so it resolves under `.git/modules` - never an ancestor of cwd. + const sup = join(tempDir, "super"); + const sub = join(sup, "vendor", "lib"); + const subSrc = join(sub, "src"); + mkdirSync(subSrc, { recursive: true }); + writeFileSync(join(sup, "AGENTS.md"), "superproject instructions"); + writeFileSync(join(sub, "AGENTS.md"), "submodule instructions"); + const subGitDir = join(sup, ".git", "modules", "vendor", "lib"); + mkdirSync(subGitDir, { recursive: true }); + writeFileSync(join(subGitDir, "HEAD"), "ref: refs/heads/main\n"); + writeFileSync(join(sub, ".git"), `gitdir: ${subGitDir}\n`); + + const files = loadProjectContextFiles({ cwd: subSrc, agentDir }); + + expect(files.map((f) => f.content)).toEqual(["superproject instructions", "submodule instructions"]); + }); + + it("should keep climbing past an ordinary repo root", () => { + const outer = join(tempDir, "outer"); + const repo = join(outer, "repo"); + const leaf = join(repo, "src"); + mkdirSync(leaf, { recursive: true }); + mkdirSync(join(repo, ".git"), { recursive: true }); + writeFileSync(join(repo, ".git", "HEAD"), "ref: refs/heads/main\n"); + writeFileSync(join(outer, "AGENTS.md"), "outer instructions"); + writeFileSync(join(repo, "AGENTS.md"), "repo instructions"); + writeFileSync(join(leaf, "AGENTS.md"), "leaf instructions"); + + const files = loadProjectContextFiles({ cwd: leaf, agentDir }); + + expect(files.map((f) => f.content)).toEqual(["outer instructions", "repo instructions", "leaf instructions"]); + }); + + it("should climb normally when the gitdir: target does not exist", () => { + const repo = join(tempDir, "corrupt"); + const src = join(repo, "src"); + mkdirSync(src, { recursive: true }); + writeFileSync(join(repo, ".git"), "gitdir: /nonexistent/path/worktrees/feat\n"); + writeFileSync(join(repo, "AGENTS.md"), "repo instructions"); + writeFileSync(join(src, "AGENTS.md"), "src instructions"); + + const files = loadProjectContextFiles({ cwd: src, agentDir }); + + expect(files.map((f) => f.content)).toEqual(["repo instructions", "src instructions"]); + }); + }); +}); diff --git a/packages/coding-agent/test/restore-sandbox-env.test.ts b/packages/coding-agent/test/restore-sandbox-env.test.ts new file mode 100644 index 0000000000000000000000000000000000000000..2fb2767f6eff5a6f6ac18973296fed06245d5020 --- /dev/null +++ b/packages/coding-agent/test/restore-sandbox-env.test.ts @@ -0,0 +1,77 @@ +import { describe, expect, it, vi } from "vitest"; + +const readFileSync = vi.fn(); + +vi.mock("node:fs", () => ({ + readFileSync, +})); + +const { restoreSandboxEnv } = await import("../src/bun/restore-sandbox-env.ts"); + +describe("restoreSandboxEnv", () => { + it("does nothing when not running under bun", () => { + const originalVersions = Object.getOwnPropertyDescriptor(process, "versions"); + Object.defineProperty(process, "versions", { + value: { node: "20.0.0" }, + }); + const envBefore = { ...process.env }; + + restoreSandboxEnv(); + + expect(process.env).toEqual(envBefore); + + if (originalVersions) { + Object.defineProperty(process, "versions", originalVersions); + } + }); + + it("does nothing when process.env already has entries", () => { + const originalVersions = Object.getOwnPropertyDescriptor(process, "versions"); + Object.defineProperty(process, "versions", { + value: { bun: "1.2.0", node: "20.0.0" }, + }); + process.env.RESTORE_SANDBOX_ENV_TEST = "1"; + const envBefore = { ...process.env }; + + restoreSandboxEnv(); + + expect(process.env).toEqual(envBefore); + delete process.env.RESTORE_SANDBOX_ENV_TEST; + + if (originalVersions) { + Object.defineProperty(process, "versions", originalVersions); + } + }); + + it("restores environment from /proc/self/environ when bun env is empty", () => { + const originalVersions = Object.getOwnPropertyDescriptor(process, "versions"); + Object.defineProperty(process, "versions", { + value: { bun: "1.2.0", node: "20.0.0" }, + }); + + // Clear env to simulate the bun sandbox bug. + const envBackup = { ...process.env }; + for (const key of Object.keys(process.env)) { + delete process.env[key]; + } + + readFileSync.mockReturnValue("FOO=bar\0BAZ=qux\0"); + + restoreSandboxEnv(); + + expect(readFileSync).toHaveBeenCalledWith("/proc/self/environ", "utf-8"); + expect(process.env.FOO).toBe("bar"); + expect(process.env.BAZ).toBe("qux"); + + // Restore. + for (const key of Object.keys(process.env)) { + delete process.env[key]; + } + Object.assign(process.env, envBackup); + + if (originalVersions) { + Object.defineProperty(process, "versions", originalVersions); + } + readFileSync.mockReset(); + }); +}); diff --git a/packages/coding-agent/test/rpc-client-clear-queue.test.ts b/packages/coding-agent/test/rpc-client-clear-queue.test.ts new file mode 100644 index 0000000000000000000000000000000000000000..c81e3cc0860421806fb19110b60fe63b10fcdb49 --- /dev/null +++ b/packages/coding-agent/test/rpc-client-clear-queue.test.ts @@ -0,0 +1,29 @@ +import { describe, expect, it, vi } from "vitest"; +import { RpcClient } from "../src/modes/rpc/rpc-client.ts"; + +type RpcClientPrivate = { + send: (command: { type: string }) => Promise; + getData: (response: unknown) => T; +}; + +describe("RpcClient clearQueue", () => { + it("sends the clear_queue RPC command", async () => { + const client = new RpcClient(); + const privateClient = client as unknown as RpcClientPrivate; + const send = vi.fn(async () => ({ + type: "response", + command: "clear_queue", + success: true, + data: { steering: ["Change direction"], followUp: ["Summarize when finished"] }, + })); + privateClient.send = send; + privateClient.getData = (response: unknown): T => { + return (response as { data: T }).data; + }; + + const result = await client.clearQueue(); + + expect(send).toHaveBeenCalledWith({ type: "clear_queue" }); + expect(result).toEqual({ steering: ["Change direction"], followUp: ["Summarize when finished"] }); + }); +}); diff --git a/packages/coding-agent/test/rpc-client-clone.test.ts b/packages/coding-agent/test/rpc-client-clone.test.ts new file mode 100644 index 0000000000000000000000000000000000000000..083f20c99501cd2341591222bef2f9209b42ed93 --- /dev/null +++ b/packages/coding-agent/test/rpc-client-clone.test.ts @@ -0,0 +1,29 @@ +import { describe, expect, it, vi } from "vitest"; +import { RpcClient } from "../src/modes/rpc/rpc-client.ts"; + +type RpcClientPrivate = { + send: (command: { type: string }) => Promise; + getData: (response: unknown) => T; +}; + +describe("RpcClient clone", () => { + it("sends the clone RPC command", async () => { + const client = new RpcClient(); + const privateClient = client as unknown as RpcClientPrivate; + const send = vi.fn(async () => ({ + type: "response", + command: "clone", + success: true, + data: { cancelled: false }, + })); + privateClient.send = send; + privateClient.getData = (response: unknown): T => { + return (response as { data: T }).data; + }; + + const result = await client.clone(); + + expect(send).toHaveBeenCalledWith({ type: "clone" }); + expect(result).toEqual({ cancelled: false }); + }); +}); diff --git a/packages/coding-agent/test/rpc-client-process-exit.test.ts b/packages/coding-agent/test/rpc-client-process-exit.test.ts new file mode 100644 index 0000000000000000000000000000000000000000..f023849e331fd9362340f2e4b2914eeca9c237be --- /dev/null +++ b/packages/coding-agent/test/rpc-client-process-exit.test.ts @@ -0,0 +1,38 @@ +import { mkdtempSync, rmSync, writeFileSync } from "node:fs"; +import { tmpdir } from "node:os"; +import { join } from "node:path"; +import { afterEach, describe, expect, test } from "vitest"; +import { RpcClient } from "../src/modes/rpc/rpc-client.ts"; + +const tempDirs: string[] = []; + +function writeChildScript(contents: string): string { + const dir = mkdtempSync(join(tmpdir(), "pi-rpc-client-exit-")); + tempDirs.push(dir); + const path = join(dir, "child.mjs"); + writeFileSync(path, contents); + return path; +} + +afterEach(() => { + for (const dir of tempDirs.splice(0)) { + rmSync(dir, { recursive: true, force: true }); + } +}); + +describe("RpcClient child process failures", () => { + test("rejects an in-flight request when the child process exits", async () => { + const client = new RpcClient({ + cliPath: writeChildScript(` +process.stdin.once("data", () => { + process.exit(43); +}); +process.stdin.resume(); +`), + }); + + await client.start(); + + await expect(client.getCommands()).rejects.toThrow(/Agent process exited \(code=43 signal=null\)/); + }); +}); diff --git a/packages/coding-agent/test/rpc-example.ts b/packages/coding-agent/test/rpc-example.ts new file mode 100644 index 0000000000000000000000000000000000000000..aaa24fb1fa95aacdda94b517b0afe2267d52c3bd --- /dev/null +++ b/packages/coding-agent/test/rpc-example.ts @@ -0,0 +1,86 @@ +import { dirname, join } from "node:path"; +import * as readline from "node:readline"; +import { fileURLToPath } from "node:url"; +import { RpcClient } from "../src/modes/rpc/rpc-client.ts"; + +const __dirname = dirname(fileURLToPath(import.meta.url)); + +/** + * Interactive example of using coding-agent via RpcClient. + * Usage: npx tsx test/rpc-example.ts + */ + +async function main() { + const client = new RpcClient({ + cliPath: join(__dirname, "../dist/cli.js"), + provider: "anthropic", + model: "claude-sonnet-4-20250514", + args: ["--no-session"], + }); + + // Stream events to console + client.onEvent((event) => { + if (event.type === "message_update") { + const { assistantMessageEvent } = event; + if (assistantMessageEvent.type === "text_delta" || assistantMessageEvent.type === "thinking_delta") { + process.stdout.write(assistantMessageEvent.delta); + } + } + + if (event.type === "tool_execution_start") { + console.log(`\n[Tool: ${event.toolName}]`); + } + + if (event.type === "tool_execution_end") { + console.log(`[Result: ${JSON.stringify(event.result).slice(0, 200)}...]\n`); + } + }); + + await client.start(); + + const state = await client.getState(); + console.log(`Model: ${state.model?.provider}/${state.model?.id}`); + console.log(`Thinking: ${state.thinkingLevel ?? "off"}\n`); + + // Handle user input + const rl = readline.createInterface({ + input: process.stdin, + output: process.stdout, + terminal: true, + }); + + let isWaiting = false; + + const prompt = () => { + if (!isWaiting) process.stdout.write("You: "); + }; + + rl.on("line", async (line) => { + if (isWaiting) return; + if (line.trim() === "exit") { + await client.stop(); + process.exit(0); + } + + isWaiting = true; + await client.promptAndWait(line); + console.log("\n"); + isWaiting = false; + prompt(); + }); + + rl.on("SIGINT", () => { + if (isWaiting) { + console.log("\n[Aborting...]"); + client.abort(); + } else { + client.stop(); + process.exit(0); + } + }); + + console.log("Interactive RPC example. Type 'exit' to quit.\n"); + prompt(); +} + +main().catch(console.error); diff --git a/packages/coding-agent/test/rpc-jsonl.test.ts b/packages/coding-agent/test/rpc-jsonl.test.ts new file mode 100644 index 0000000000000000000000000000000000000000..c8fc89e13e878877e5b82984e192abdc40b98f0f --- /dev/null +++ b/packages/coding-agent/test/rpc-jsonl.test.ts @@ -0,0 +1,65 @@ +import { Readable } from "node:stream"; +import { describe, expect, test } from "vitest"; +import { attachJsonlLineReader, serializeJsonLine } from "../src/modes/rpc/jsonl.ts"; + +describe("RPC JSONL framing", () => { + test("serializes strict JSONL records without escaping Unicode separators", () => { + const line = serializeJsonLine({ text: "a\u2028b\u2029c" }); + + expect(line).toContain("a\u2028b\u2029c"); + expect(line.endsWith("\n")).toBe(true); + expect(JSON.parse(line.trim())).toEqual({ text: "a\u2028b\u2029c" }); + }); + + test("splits on LF only and preserves U+2028/U+2029 inside payloads", async () => { + const lines: string[] = []; + const stream = Readable.from([serializeJsonLine({ text: "a\u2028b\u2029c" })]); + + const done = new Promise((resolve) => { + stream.on("end", resolve); + }); + + attachJsonlLineReader(stream, (line) => { + lines.push(line); + }); + + await done; + + expect(lines).toHaveLength(1); + expect(JSON.parse(lines[0])).toEqual({ text: "a\u2028b\u2029c" }); + }); + + test("handles CRLF-delimited input", async () => { + const lines: string[] = []; + const stream = Readable.from([Buffer.from('{"a":1}\r\n{"b":2}\r\n')]); + + const done = new Promise((resolve) => { + stream.on("end", resolve); + }); + + attachJsonlLineReader(stream, (line) => { + lines.push(line); + }); + + await done; + + expect(lines).toEqual(['{"a":1}', '{"b":2}']); + }); + + test("emits a final line without trailing LF", async () => { + const lines: string[] = []; + const stream = Readable.from([Buffer.from('{"a":1}')]); + + const done = new Promise((resolve) => { + stream.on("end", resolve); + }); + + attachJsonlLineReader(stream, (line) => { + lines.push(line); + }); + + await done; + + expect(lines).toEqual(['{"a":1}']); + }); +}); diff --git a/packages/coding-agent/test/rpc-prompt-response-semantics.test.ts b/packages/coding-agent/test/rpc-prompt-response-semantics.test.ts new file mode 100644 index 0000000000000000000000000000000000000000..5d7c147ffdd4eb590cc80f9e8d8acc49b4e53ef3 --- /dev/null +++ b/packages/coding-agent/test/rpc-prompt-response-semantics.test.ts @@ -0,0 +1,340 @@ +import { existsSync, mkdirSync, rmSync } from "node:fs"; +import { tmpdir } from "node:os"; +import { join } from "node:path"; +import { Agent } from "@earendil-works/pi-agent-core"; +import { + type AssistantMessage, + type AssistantMessageEvent, + EventStream, + getModel, + type Model, +} from "@earendil-works/pi-ai/compat"; +import { afterEach, describe, expect, it, vi } from "vitest"; +import { AgentSession } from "../src/core/agent-session.ts"; +import type { AgentSessionRuntime } from "../src/core/agent-session-runtime.ts"; +import { AuthStorage } from "../src/core/auth-storage.ts"; +import { SessionManager } from "../src/core/session-manager.ts"; +import { SettingsManager } from "../src/core/settings-manager.ts"; +import { runRpcMode } from "../src/modes/rpc/rpc-mode.ts"; +import { createInMemoryModelRegistry, getModelRuntime } from "./model-runtime-test-utils.ts"; +import { createTestResourceLoader } from "./utilities.ts"; + +const rpcIo = vi.hoisted(() => ({ + outputLines: [] as string[], + lineHandler: undefined as ((line: string) => void) | undefined, +})); + +vi.mock("../src/core/output-guard.js", () => ({ + flushRawStdout: vi.fn(async () => {}), + takeOverStdout: vi.fn(), + waitForRawStdoutBackpressure: vi.fn(async () => {}), + writeRawStdout: (line: string) => { + rpcIo.outputLines.push(line); + }, +})); + +vi.mock("../src/modes/interactive/theme/theme.js", () => ({ theme: {} })); + +vi.mock("../src/modes/rpc/jsonl.js", () => ({ + attachJsonlLineReader: vi.fn((_stream: NodeJS.ReadableStream, onLine: (line: string) => void) => { + rpcIo.lineHandler = onLine; + return () => {}; + }), + serializeJsonLine: (value: unknown) => `${JSON.stringify(value)}\n`, +})); + +class MockAssistantStream extends EventStream { + constructor() { + super( + (event) => event.type === "done" || event.type === "error", + (event) => { + if (event.type === "done") return event.message; + if (event.type === "error") return event.error; + throw new Error("Unexpected event type"); + }, + ); + } +} + +function createAssistantMessage(text: string): AssistantMessage { + return { + role: "assistant", + content: [{ type: "text", text }], + api: "anthropic-messages", + provider: "anthropic", + model: "claude-sonnet-4-5", + usage: { + input: 0, + output: 0, + cacheRead: 0, + cacheWrite: 0, + totalTokens: 0, + cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 }, + }, + stopReason: "stop", + timestamp: Date.now(), + }; +} + +type ParsedOutputLine = Record; + +function parseOutputLines(outputLines: string[]): ParsedOutputLine[] { + return outputLines + .flatMap((line) => line.split("\n")) + .filter((line) => line.trim().length > 0) + .map((line) => JSON.parse(line) as ParsedOutputLine); +} + +function getPromptResponses(outputLines: string[], id: string): ParsedOutputLine[] { + return parseOutputLines(outputLines).filter( + (record) => record.id === id && record.type === "response" && record.command === "prompt", + ); +} + +function sleep(ms: number): Promise { + return new Promise((resolve) => setTimeout(resolve, ms)); +} + +async function createRuntimeHost(options: { withAuth: boolean; responseDelayMs: number; model?: Model }): Promise<{ + runtimeHost: AgentSessionRuntime; + cleanup: () => Promise; +}> { + const tempDir = join(tmpdir(), `pi-rpc-prompt-${Date.now()}-${Math.random().toString(36).slice(2)}`); + mkdirSync(tempDir, { recursive: true }); + + const model = options.model ?? getModel("anthropic", "claude-sonnet-4-5"); + if (!model) { + throw new Error("Test model not found"); + } + + const agent = new Agent({ + getApiKey: () => "test-key", + initialState: { + model, + systemPrompt: "Test", + tools: [], + }, + streamFn: (_model, _context, _options) => { + const stream = new MockAssistantStream(); + queueMicrotask(() => { + stream.push({ type: "start", partial: createAssistantMessage("") }); + setTimeout(() => { + stream.push({ type: "done", reason: "stop", message: createAssistantMessage("done") }); + }, options.responseDelayMs); + }); + return stream; + }, + }); + + const sessionManager = SessionManager.inMemory(); + const settingsManager = SettingsManager.create(tempDir, tempDir); + const authStorage = AuthStorage.create(join(tempDir, "auth.json")); + const modelRegistry = await createInMemoryModelRegistry(authStorage); + if (options.withAuth) { + await authStorage.modify("anthropic", async () => ({ type: "api_key", key: "test-key" })); + } + + const session = new AgentSession({ + agent, + sessionManager, + settingsManager, + cwd: tempDir, + modelRuntime: getModelRuntime(modelRegistry), + resourceLoader: createTestResourceLoader(), + }); + + const runtimeHost = { + session, + newSession: vi.fn(async () => ({ cancelled: true })), + switchSession: vi.fn(async () => ({ cancelled: true })), + fork: vi.fn(async () => ({ cancelled: true, selectedText: "" })), + dispose: vi.fn(async () => {}), + setRebindSession: vi.fn(), + } as unknown as AgentSessionRuntime; + + return { + runtimeHost, + cleanup: async () => { + try { + await session.abort(); + } catch { + // ignore test cleanup failures + } + session.dispose(); + if (existsSync(tempDir)) { + rmSync(tempDir, { recursive: true }); + } + }, + }; +} + +async function startRpcMode(options: { withAuth: boolean; responseDelayMs: number; model?: Model }): Promise<{ + lineHandler: (line: string) => void; + cleanup: () => Promise; +}> { + rpcIo.outputLines = []; + rpcIo.lineHandler = undefined; + + const { runtimeHost, cleanup } = await createRuntimeHost(options); + void runRpcMode(runtimeHost); + await vi.waitFor(() => expect(rpcIo.lineHandler).toBeDefined()); + + return { lineHandler: rpcIo.lineHandler!, cleanup }; +} + +describe("RPC prompt response semantics", () => { + afterEach(() => { + rpcIo.outputLines = []; + rpcIo.lineHandler = undefined; + }); + + it("emits one failure response when prompt preflight rejects", async () => { + const { lineHandler, cleanup } = await startRpcMode({ + withAuth: false, + responseDelayMs: 0, + model: { + id: "fake-model", + name: "Fake Model", + api: "openai-completions", + provider: "fake-provider", + baseUrl: "https://example.invalid", + reasoning: false, + input: [], + cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 }, + contextWindow: 0, + maxTokens: 0, + }, + }); + + try { + lineHandler(JSON.stringify({ id: "b1", type: "prompt", message: "Hello" })); + + await vi.waitFor(() => { + const responses = getPromptResponses(rpcIo.outputLines, "b1"); + expect(responses).toHaveLength(1); + expect(responses[0]).toMatchObject({ + id: "b1", + type: "response", + command: "prompt", + success: false, + error: expect.stringContaining( + "No API key found for fake-provider.\n\nUse /login to log into a provider via OAuth or API key. See:", + ), + }); + }); + } finally { + await cleanup(); + } + }); + + it("emits one success response when prompt preflight succeeds", async () => { + const { lineHandler, cleanup } = await startRpcMode({ withAuth: true, responseDelayMs: 0 }); + + try { + lineHandler(JSON.stringify({ id: "b2", type: "prompt", message: "Hello" })); + + await vi.waitFor(() => { + const responses = getPromptResponses(rpcIo.outputLines, "b2"); + expect(responses).toHaveLength(1); + expect(responses[0]).toMatchObject({ + id: "b2", + type: "response", + command: "prompt", + success: true, + }); + }); + } finally { + await cleanup(); + } + }); + + it("emits one success response when prompt is queued during streaming", async () => { + const { lineHandler, cleanup } = await startRpcMode({ withAuth: true, responseDelayMs: 100 }); + + try { + lineHandler(JSON.stringify({ id: "b3-start", type: "prompt", message: "Start" })); + await vi.waitFor(() => { + expect(getPromptResponses(rpcIo.outputLines, "b3-start")).toHaveLength(1); + }); + + rpcIo.outputLines = []; + lineHandler( + JSON.stringify({ + id: "b3", + type: "prompt", + message: "Queue this", + streamingBehavior: "followUp", + }), + ); + + await vi.waitFor(() => { + const responses = getPromptResponses(rpcIo.outputLines, "b3"); + expect(responses).toHaveLength(1); + expect(responses[0]).toMatchObject({ + id: "b3", + type: "response", + command: "prompt", + success: true, + }); + }); + + await sleep(150); + } finally { + await cleanup(); + } + }); + + it("returns and clears queued steering and follow-up messages", async () => { + const { lineHandler, cleanup } = await startRpcMode({ withAuth: true, responseDelayMs: 500 }); + + try { + lineHandler(JSON.stringify({ id: "clear-start", type: "prompt", message: "Start" })); + await vi.waitFor(() => { + expect(getPromptResponses(rpcIo.outputLines, "clear-start")).toHaveLength(1); + }); + + lineHandler( + JSON.stringify({ + id: "clear-steering", + type: "prompt", + message: "Change direction", + streamingBehavior: "steer", + }), + ); + await vi.waitFor(() => { + expect(getPromptResponses(rpcIo.outputLines, "clear-steering")).toHaveLength(1); + }); + + lineHandler( + JSON.stringify({ + id: "clear-follow-up", + type: "prompt", + message: "Summarize when finished", + streamingBehavior: "followUp", + }), + ); + await vi.waitFor(() => { + expect(getPromptResponses(rpcIo.outputLines, "clear-follow-up")).toHaveLength(1); + }); + + lineHandler(JSON.stringify({ id: "clear", type: "clear_queue" })); + await vi.waitFor(() => { + expect(parseOutputLines(rpcIo.outputLines)).toContainEqual({ + id: "clear", + type: "response", + command: "clear_queue", + success: true, + data: { + steering: ["Change direction"], + followUp: ["Summarize when finished"], + }, + }); + }); + + await sleep(600); + expect(parseOutputLines(rpcIo.outputLines).filter((record) => record.type === "agent_start")).toHaveLength(1); + } finally { + await cleanup(); + } + }); +}); diff --git a/packages/coding-agent/test/rpc.test.ts b/packages/coding-agent/test/rpc.test.ts new file mode 100644 index 0000000000000000000000000000000000000000..315fd27a4c9f47e25c7af384c9c079c706e1a7f6 --- /dev/null +++ b/packages/coding-agent/test/rpc.test.ts @@ -0,0 +1,399 @@ +import { existsSync, readdirSync, readFileSync, rmSync } from "node:fs"; +import { tmpdir } from "node:os"; +import { dirname, join } from "node:path"; +import { fileURLToPath } from "node:url"; +import type { AgentEvent } from "@earendil-works/pi-agent-core"; +import { afterEach, beforeEach, describe, expect, test } from "vitest"; +import { RpcClient } from "../src/modes/rpc/rpc-client.ts"; + +const __dirname = dirname(fileURLToPath(import.meta.url)); + +/** + * RPC mode tests. + */ +describe.skipIf(!process.env.ANTHROPIC_API_KEY && !process.env.ANTHROPIC_OAUTH_TOKEN)("RPC mode", () => { + let client: RpcClient; + let sessionDir: string; + + beforeEach(() => { + sessionDir = join(tmpdir(), `pi-rpc-test-${Date.now()}`); + client = new RpcClient({ + cliPath: join(__dirname, "..", "dist", "cli.js"), + cwd: join(__dirname, ".."), + env: { PI_CODING_AGENT_DIR: sessionDir }, + provider: "anthropic", + model: "claude-sonnet-4-5", + }); + }); + + afterEach(async () => { + await client.stop(); + if (sessionDir && existsSync(sessionDir)) { + rmSync(sessionDir, { recursive: true }); + } + }); + + test("should get state", async () => { + await client.start(); + const state = await client.getState(); + + expect(state.model).toBeDefined(); + expect(state.model?.provider).toBe("anthropic"); + expect(state.model?.id).toBe("claude-sonnet-4-5"); + expect(state.isStreaming).toBe(false); + expect(state.messageCount).toBe(0); + }, 30000); + + test("should save messages to session file", async () => { + await client.start(); + + // Send prompt and wait for completion + const events = await client.promptAndWait("Reply with just the word 'hello'"); + + // Should have message events + const messageEndEvents = events.filter((e) => e.type === "message_end"); + expect(messageEndEvents.length).toBeGreaterThanOrEqual(2); // user + assistant + + // Wait for file writes + await new Promise((resolve) => setTimeout(resolve, 200)); + + // Verify session file + const sessionsPath = join(sessionDir, "sessions"); + expect(existsSync(sessionsPath)).toBe(true); + + const sessionDirs = readdirSync(sessionsPath); + expect(sessionDirs.length).toBeGreaterThan(0); + + const cwdSessionDir = join(sessionsPath, sessionDirs[0]); + const sessionFiles = readdirSync(cwdSessionDir).filter((f) => f.endsWith(".jsonl")); + expect(sessionFiles.length).toBe(1); + + const sessionContent = readFileSync(join(cwdSessionDir, sessionFiles[0]), "utf8"); + const entries = sessionContent + .trim() + .split("\n") + .map((line) => JSON.parse(line)); + + // First entry should be session header + expect(entries[0].type).toBe("session"); + + // Should have user and assistant messages + const messages = entries.filter((e: { type: string }) => e.type === "message"); + expect(messages.length).toBeGreaterThanOrEqual(2); + + const roles = messages.map((m: { message: { role: string } }) => m.message.role); + expect(roles).toContain("user"); + expect(roles).toContain("assistant"); + }, 90000); + + test("should handle manual compaction", async () => { + await client.start(); + + // First send a prompt to have messages to compact + await client.promptAndWait("Say hello"); + + // Compact + const result = await client.compact(); + expect(result.summary).toBeDefined(); + expect(result.tokensBefore).toBeGreaterThan(0); + + // Wait for file writes + await new Promise((resolve) => setTimeout(resolve, 200)); + + // Verify compaction in session file + const sessionsPath = join(sessionDir, "sessions"); + const sessionDirs = readdirSync(sessionsPath); + const cwdSessionDir = join(sessionsPath, sessionDirs[0]); + const sessionFiles = readdirSync(cwdSessionDir).filter((f) => f.endsWith(".jsonl")); + const sessionContent = readFileSync(join(cwdSessionDir, sessionFiles[0]), "utf8"); + const entries = sessionContent + .trim() + .split("\n") + .map((line) => JSON.parse(line)); + + const compactionEntries = entries.filter((e: { type: string }) => e.type === "compaction"); + expect(compactionEntries.length).toBe(1); + expect(compactionEntries[0].summary).toBeDefined(); + }, 120000); + + test("should execute bash command", async () => { + await client.start(); + + const result = await client.bash("echo hello"); + expect(result.output.trim()).toBe("hello"); + expect(result.exitCode).toBe(0); + expect(result.cancelled).toBe(false); + }, 30000); + + test("should add bash output to context", async () => { + await client.start(); + + // First send a prompt to initialize session + await client.promptAndWait("Say hi"); + + // Run bash command + const uniqueValue = `test-${Date.now()}`; + await client.bash(`echo ${uniqueValue}`); + + // Wait for file writes + await new Promise((resolve) => setTimeout(resolve, 200)); + + // Verify bash message in session + const sessionsPath = join(sessionDir, "sessions"); + const sessionDirs = readdirSync(sessionsPath); + const cwdSessionDir = join(sessionsPath, sessionDirs[0]); + const sessionFiles = readdirSync(cwdSessionDir).filter((f) => f.endsWith(".jsonl")); + const sessionContent = readFileSync(join(cwdSessionDir, sessionFiles[0]), "utf8"); + const entries = sessionContent + .trim() + .split("\n") + .map((line) => JSON.parse(line)); + + const bashMessages = entries.filter( + (e: { type: string; message?: { role: string } }) => + e.type === "message" && e.message?.role === "bashExecution", + ); + expect(bashMessages.length).toBe(1); + expect(bashMessages[0].message.output).toContain(uniqueValue); + }, 90000); + + test("should include bash output in LLM context", async () => { + await client.start(); + + // Run a bash command with a unique value + const uniqueValue = `unique-${Date.now()}`; + await client.bash(`echo ${uniqueValue}`); + + // Ask the LLM what the output was + const events = await client.promptAndWait( + "What was the exact output of the echo command I just ran? Reply with just the value, nothing else.", + ); + + // Find assistant's response + const messageEndEvents = events.filter((e) => e.type === "message_end") as AgentEvent[]; + const assistantMessage = messageEndEvents.find( + (e) => e.type === "message_end" && e.message?.role === "assistant", + ) as any; + + expect(assistantMessage).toBeDefined(); + + const textContent = assistantMessage.message.content.find((c: any) => c.type === "text"); + expect(textContent?.text).toContain(uniqueValue); + }, 90000); + + test("should set and get thinking level", async () => { + await client.start(); + + // Set thinking level + await client.setThinkingLevel("high"); + + // Verify via state + const state = await client.getState(); + expect(state.thinkingLevel).toBe("high"); + }, 30000); + + test("should cycle thinking level", async () => { + await client.start(); + + // Get initial level + const initialState = await client.getState(); + const initialLevel = initialState.thinkingLevel; + + // Cycle + const result = await client.cycleThinkingLevel(); + expect(result).toBeDefined(); + expect(result!.level).not.toBe(initialLevel); + + // Verify via state + const newState = await client.getState(); + expect(newState.thinkingLevel).toBe(result!.level); + }, 30000); + + test("should get available thinking levels", async () => { + await client.start(); + + const levels = await client.getAvailableThinkingLevels(); + expect(levels.length).toBeGreaterThan(0); + + // The current level reported by get_state must be in the available list + const state = await client.getState(); + expect(levels).toContain(state.thinkingLevel); + + // cycle_thinking_level must only ever land on levels from get_available_thinking_levels + const initialLevel = state.thinkingLevel; + const cycled = await client.cycleThinkingLevel(); + if (cycled) { + expect(levels).toContain(cycled.level); + // distinct cycle step (unless only one level) + if (levels.length > 1) { + expect(cycled.level).not.toBe(initialLevel); + } + } + }, 30000); + + test("should get available models", async () => { + await client.start(); + + const models = await client.getAvailableModels(); + expect(models.length).toBeGreaterThan(0); + + // All models should have required fields + for (const model of models) { + expect(model.provider).toBeDefined(); + expect(model.id).toBeDefined(); + expect(model.contextWindow).toBeGreaterThan(0); + expect(typeof model.reasoning).toBe("boolean"); + } + }, 30000); + + test("should get session stats", async () => { + await client.start(); + + // Send a prompt first + await client.promptAndWait("Hello"); + + const stats = await client.getSessionStats(); + expect(stats.sessionFile).toBeDefined(); + expect(stats.sessionId).toBeDefined(); + expect(stats.userMessages).toBeGreaterThanOrEqual(1); + expect(stats.assistantMessages).toBeGreaterThanOrEqual(1); + }, 90000); + + test("should create new session", async () => { + await client.start(); + + // Send a prompt + await client.promptAndWait("Hello"); + + // Verify messages exist + let state = await client.getState(); + expect(state.messageCount).toBeGreaterThan(0); + + // New session + await client.newSession(); + + // Verify messages cleared + state = await client.getState(); + expect(state.messageCount).toBe(0); + }, 90000); + + test("should export to HTML", async () => { + await client.start(); + + // Send a prompt first + await client.promptAndWait("Hello"); + + // Export + const result = await client.exportHtml(); + expect(result.path).toBeDefined(); + expect(result.path.endsWith(".html")).toBe(true); + expect(existsSync(result.path)).toBe(true); + }, 90000); + + test("should get last assistant text", async () => { + await client.start(); + + // Initially null + let text = await client.getLastAssistantText(); + expect(text).toBeUndefined(); + + // Send prompt + await client.promptAndWait("Reply with just: test123"); + + // Should have text now + text = await client.getLastAssistantText(); + expect(text).toContain("test123"); + }, 90000); + + test("should get session entries with since cursor", async () => { + await client.start(); + + await client.promptAndWait("Reply with just 'ok'"); + + const { entries, leafId } = await client.getEntries(); + expect(entries.length).toBeGreaterThanOrEqual(2); // user + assistant + for (const entry of entries) { + expect(entry.id).toBeDefined(); + } + expect(leafId).toBe(entries[entries.length - 1].id); + + // since cursor returns only entries strictly after the given id + const since = await client.getEntries(entries[0].id); + expect(since.entries.map((e) => e.id)).toEqual(entries.slice(1).map((e) => e.id)); + expect(since.leafId).toBe(leafId); + + // unknown since id is an error response + await expect(client.getEntries("nonexistent-id")).rejects.toThrow("Entry not found"); + }, 90000); + + test("should get session tree", async () => { + await client.start(); + + await client.promptAndWait("Reply with just 'ok'"); + + const { entries, leafId } = await client.getEntries(); + const { tree, leafId: treeLeafId } = await client.getTree(); + expect(treeLeafId).toBe(leafId); + + // Single root whose chain matches the entries + expect(tree.length).toBe(1); + const chainIds: string[] = []; + let nodes = tree; + while (nodes.length === 1) { + chainIds.push(nodes[0].entry.id); + nodes = nodes[0].children; + } + expect(nodes.length).toBe(0); + expect(chainIds).toEqual(entries.map((e) => e.id)); + }, 90000); + + test("should retain pre-compaction entries in get_entries", async () => { + await client.start(); + + await client.promptAndWait("Reply with just 'ok'"); + const before = await client.getEntries(); + + await client.compact(); + + const after = await client.getEntries(); + // Append-only: pre-compaction entries are still there, in the same order + expect(after.entries.slice(0, before.entries.length).map((e) => e.id)).toEqual(before.entries.map((e) => e.id)); + expect(after.entries.some((e) => e.type === "compaction")).toBe(true); + }, 120000); + + test("should set and get session name", async () => { + await client.start(); + + // Initially undefined + let state = await client.getState(); + expect(state.sessionName).toBeUndefined(); + + // Send a prompt first - session files are only written after first assistant message + await client.promptAndWait("Reply with just 'ok'"); + + // Set name + await client.setSessionName("my-test-session"); + + // Verify via state + state = await client.getState(); + expect(state.sessionName).toBe("my-test-session"); + + // Wait for file writes + await new Promise((resolve) => setTimeout(resolve, 200)); + + // Verify session_info entry in session file + const sessionsPath = join(sessionDir, "sessions"); + const sessionDirs = readdirSync(sessionsPath); + const cwdSessionDir = join(sessionsPath, sessionDirs[0]); + const sessionFiles = readdirSync(cwdSessionDir).filter((f) => f.endsWith(".jsonl")); + const sessionContent = readFileSync(join(cwdSessionDir, sessionFiles[0]), "utf8"); + const entries = sessionContent + .trim() + .split("\n") + .map((line) => JSON.parse(line)); + + const sessionInfoEntries = entries.filter((e: { type: string }) => e.type === "session_info"); + expect(sessionInfoEntries.length).toBe(1); + expect(sessionInfoEntries[0].name).toBe("my-test-session"); + }, 60000); +}); diff --git a/packages/coding-agent/test/runtime-credentials.test.ts b/packages/coding-agent/test/runtime-credentials.test.ts new file mode 100644 index 0000000000000000000000000000000000000000..fe50503cbfa72c4114f2a749086b7ed1a2e17c35 --- /dev/null +++ b/packages/coding-agent/test/runtime-credentials.test.ts @@ -0,0 +1,86 @@ +import type { CredentialStore } from "@earendil-works/pi-ai"; +import { describe, expect, test, vi } from "vitest"; +import { AuthStorage } from "../src/core/auth-storage.ts"; +import { RuntimeCredentials } from "../src/core/runtime-credentials.ts"; + +describe("RuntimeCredentials", () => { + test("runtime overrides mask stored credentials without persisting", async () => { + const storage = AuthStorage.inMemory({ anthropic: { type: "api_key", key: "stored-key" } }); + const credentials = new RuntimeCredentials(storage); + + credentials.setRuntimeApiKey("anthropic", "runtime-key"); + expect(await credentials.read("anthropic")).toEqual({ type: "api_key", key: "runtime-key" }); + expect(await storage.read("anthropic")).toEqual({ type: "api_key", key: "stored-key" }); + + credentials.removeRuntimeApiKey("anthropic"); + expect(await credentials.read("anthropic")).toEqual({ type: "api_key", key: "stored-key" }); + }); + + test("enumeration merges overrides without exposing keys", async () => { + const storage = AuthStorage.inMemory({ + anthropic: { type: "oauth", access: "access", refresh: "refresh", expires: Date.now() + 60_000 }, + }); + const credentials = new RuntimeCredentials(storage); + credentials.setRuntimeApiKey("anthropic", "runtime-key"); + credentials.setRuntimeApiKey("openai", "other-runtime-key"); + + expect(await credentials.list()).toEqual([ + { providerId: "anthropic", type: "api_key" }, + { providerId: "openai", type: "api_key" }, + ]); + }); + + test("forwards operation signals to the persistent store", async () => { + const controller = new AbortController(); + const received: AbortSignal[] = []; + const storage: CredentialStore = { + read: async (_providerId, options) => { + received.push(options?.signal as AbortSignal); + return undefined; + }, + list: async (options) => { + received.push(options?.signal as AbortSignal); + return []; + }, + modify: async (_providerId, _fn, options) => { + received.push(options?.signal as AbortSignal); + return undefined; + }, + delete: async (_providerId, options) => { + received.push(options?.signal as AbortSignal); + }, + }; + const credentials = new RuntimeCredentials(storage); + + await credentials.read("anthropic", { signal: controller.signal }); + await credentials.list({ signal: controller.signal }); + await credentials.modify("anthropic", async () => undefined, { signal: controller.signal }); + await credentials.delete("anthropic", { signal: controller.signal }); + + expect(received).toEqual([controller.signal, controller.signal, controller.signal, controller.signal]); + }); + + test("keeps a runtime override when persistent deletion is cancelled", async () => { + const aborted = new Error("cancelled"); + aborted.name = "AbortError"; + const storage = AuthStorage.inMemory({ anthropic: { type: "api_key", key: "stored-key" } }); + const deleteSpy = vi.spyOn(storage, "delete").mockRejectedValueOnce(aborted); + const credentials = new RuntimeCredentials(storage); + credentials.setRuntimeApiKey("anthropic", "runtime-key"); + + await expect(credentials.delete("anthropic", { signal: new AbortController().signal })).rejects.toBe(aborted); + expect(deleteSpy).toHaveBeenCalledTimes(1); + expect(await credentials.read("anthropic")).toEqual({ type: "api_key", key: "runtime-key" }); + }); + + test("delete clears both the override and persisted credential", async () => { + const storage = AuthStorage.inMemory({ anthropic: { type: "api_key", key: "stored-key" } }); + const credentials = new RuntimeCredentials(storage); + credentials.setRuntimeApiKey("anthropic", "runtime-key"); + + await credentials.delete("anthropic"); + + expect(await credentials.read("anthropic")).toBeUndefined(); + expect(await credentials.list()).toEqual([]); + }); +}); diff --git a/packages/coding-agent/test/scoped-models-selector.test.ts b/packages/coding-agent/test/scoped-models-selector.test.ts new file mode 100644 index 0000000000000000000000000000000000000000..80c22cfa565a5d22c8f689eb7edae8398944edd8 --- /dev/null +++ b/packages/coding-agent/test/scoped-models-selector.test.ts @@ -0,0 +1,126 @@ +import { setKeybindings } from "@earendil-works/pi-tui"; +import { afterEach, beforeAll, beforeEach, describe, expect, it } from "vitest"; +import { KeybindingsManager } from "../src/core/keybindings.ts"; +import { ScopedModelsSelectorComponent } from "../src/modes/interactive/components/scoped-models-selector.ts"; +import { initTheme } from "../src/modes/interactive/theme/theme.ts"; +import { stripAnsi } from "../src/utils/ansi.ts"; +import { createHarness, type Harness } from "./suite/harness.ts"; + +interface ModelState { + id: string; + name: string; + enabled: boolean; +} + +function getMarkerStates(selector: ScopedModelsSelectorComponent, models: ModelState[]): boolean[] { + const lines = stripAnsi(selector.render(120).join("\n")).split("\n"); + return models.map((model) => { + const line = lines.find((candidate) => candidate.includes(`${model.id} [`)); + if (!line) throw new Error(`Expected rendered row for ${model.id}`); + return line.slice(2).startsWith("✓ "); + }); +} + +describe("scoped models selector", () => { + let harness: Harness | undefined; + + beforeAll(() => { + initTheme("dark"); + }); + + beforeEach(() => { + setKeybindings(new KeybindingsManager()); + }); + + afterEach(() => { + harness?.cleanup(); + harness = undefined; + }); + + async function createSelector(models: ModelState[]) { + harness = await createHarness({ models }); + const provider = harness.models[0].provider; + const selector = new ScopedModelsSelectorComponent( + { + allModels: [...harness.models], + enabledModelIds: models.filter((model) => model.enabled).map((model) => `${provider}/${model.id}`), + }, + { + onChange: (enabledModelIds) => { + for (const model of models) { + model.enabled = enabledModelIds === null || enabledModelIds.includes(`${provider}/${model.id}`); + } + }, + onPersist: () => {}, + onCancel: () => {}, + }, + ); + return selector; + } + + it("marks every model after enabling all", async () => { + const models = [ + { id: "model-a", name: "Model A", enabled: true }, + { id: "model-b", name: "Model B", enabled: false }, + { id: "model-c", name: "Model C", enabled: false }, + ]; + const selector = await createSelector(models); + + selector.handleInput("\x01"); + + expect(models.map((model) => model.enabled)).toEqual([true, true, true]); + expect(getMarkerStates(selector, models)).toEqual([true, true, true]); + expect(stripAnsi(selector.render(120).join("\n"))).toContain("all enabled"); + }); + + it("disables only the selected model after enabling all", async () => { + const models = [ + { id: "model-a", name: "Model A", enabled: true }, + { id: "model-b", name: "Model B", enabled: false }, + { id: "model-c", name: "Model C", enabled: false }, + ]; + const selector = await createSelector(models); + + selector.handleInput("\x01"); + selector.handleInput("\r"); + + expect(models.map((model) => model.enabled)).toEqual([false, true, true]); + expect(getMarkerStates(selector, models)).toEqual([false, true, true]); + }); + + it("enables only the selected model after clearing all", async () => { + const models = [ + { id: "model-a", name: "Model A", enabled: true }, + { id: "model-b", name: "Model B", enabled: true }, + { id: "model-c", name: "Model C", enabled: true }, + ]; + const selector = await createSelector(models); + + selector.handleInput("\x18"); + expect(models.map((model) => model.enabled)).toEqual([false, false, false]); + expect(getMarkerStates(selector, models)).toEqual([false, false, false]); + + selector.handleInput("\r"); + expect(models.map((model) => model.enabled)).toEqual([true, false, false]); + expect(getMarkerStates(selector, models)).toEqual([true, false, false]); + }); + + it("restores the all-enabled state after re-enabling the last disabled model", async () => { + const models = [ + { id: "model-a", name: "Model A", enabled: true }, + { id: "model-b", name: "Model B", enabled: false }, + { id: "model-c", name: "Model C", enabled: false }, + ]; + const selector = await createSelector(models); + + selector.handleInput("\x01"); // enable all -> null + selector.handleInput("\r"); // disable model-a; enabled models re-sort first: [b, c, a] + selector.handleInput("\x1b[B"); + selector.handleInput("\x1b[B"); // move selection back to model-a + selector.handleInput("\r"); // re-enable model-a + + expect(models.map((model) => model.enabled)).toEqual([true, true, true]); + expect(getMarkerStates(selector, models)).toEqual([true, true, true]); + expect(stripAnsi(selector.render(120).join("\n"))).toContain("all enabled"); + }); +}); diff --git a/packages/coding-agent/test/scrollbar-theme.test.ts b/packages/coding-agent/test/scrollbar-theme.test.ts new file mode 100644 index 0000000000000000000000000000000000000000..02d6184514cffd3b57f49d9bfd599fcad2f65720 --- /dev/null +++ b/packages/coding-agent/test/scrollbar-theme.test.ts @@ -0,0 +1,78 @@ +import { mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs"; +import { tmpdir } from "node:os"; +import { join } from "node:path"; +import { afterEach, describe, expect, it } from "vitest"; +import { loadThemeFromPath, setThemeJsonValidator } from "../src/modes/interactive/theme/theme.ts"; +import { validateThemeJson } from "../src/modes/interactive/theme/theme-json.ts"; + +setThemeJsonValidator(validateThemeJson); + +const tempDirs: string[] = []; + +function loadDarkTheme(): { name: string; colors: Record } { + return JSON.parse(readFileSync(new URL("../src/modes/interactive/theme/dark.json", import.meta.url), "utf8")) as { + name: string; + colors: Record; + }; +} + +function writeTheme(theme: { name: string; colors: Record }): string { + const testDir = mkdtempSync(join(tmpdir(), "pi-scrollbar-theme-")); + tempDirs.push(testDir); + const themePath = join(testDir, `${theme.name}.json`); + writeFileSync(themePath, JSON.stringify(theme)); + return themePath; +} + +afterEach(() => { + for (const dir of tempDirs.splice(0)) { + rmSync(dir, { recursive: true, force: true }); + } +}); + +describe("fullscreen theme colors", () => { + it.each([ + ["scrollbarTrack", "muted"], + ["scrollbarThumb", "text"], + ] as const)("falls back to %s when omitted", (token, fallback) => { + const themeJson = loadDarkTheme(); + themeJson.name = `missing-${token}-theme`; + delete themeJson.colors[token]; + + const loadedTheme = loadThemeFromPath(writeTheme(themeJson), "truecolor"); + expect(loadedTheme.getFgAnsi(token)).toBe(loadedTheme.getFgAnsi(fallback)); + }); + + it("uses explicitly configured scrollbar colors", () => { + const themeJson = loadDarkTheme(); + themeJson.name = "custom-scrollbar-theme"; + themeJson.colors.scrollbarTrack = "#654321"; + themeJson.colors.scrollbarThumb = "#123456"; + + const loadedTheme = loadThemeFromPath(writeTheme(themeJson), "truecolor"); + expect(loadedTheme.getFgAnsi("scrollbarTrack")).toBe("\x1b[38;2;101;67;33m"); + expect(loadedTheme.getFgAnsi("scrollbarThumb")).toBe("\x1b[38;2;18;52;86m"); + }); + + it("falls back to existing selection and text colors for search highlights", () => { + const themeJson = loadDarkTheme(); + themeJson.name = "legacy-search-theme"; + delete themeJson.colors.searchMatchBg; + delete themeJson.colors.searchMatchText; + + const loadedTheme = loadThemeFromPath(writeTheme(themeJson), "truecolor"); + expect(loadedTheme.getBgAnsi("searchMatchBg")).toBe(loadedTheme.getBgAnsi("selectedBg")); + expect(loadedTheme.getFgAnsi("searchMatchText")).toBe(loadedTheme.getFgAnsi("text")); + }); + + it("uses explicitly configured search highlight colors", () => { + const themeJson = loadDarkTheme(); + themeJson.name = "custom-search-theme"; + themeJson.colors.searchMatchBg = "#112233"; + themeJson.colors.searchMatchText = "#223344"; + + const loadedTheme = loadThemeFromPath(writeTheme(themeJson), "truecolor"); + expect(loadedTheme.getBgAnsi("searchMatchBg")).toBe("\x1b[48;2;17;34;51m"); + expect(loadedTheme.getFgAnsi("searchMatchText")).toBe("\x1b[38;2;34;51;68m"); + }); +}); diff --git a/packages/coding-agent/test/sdk-codex-cache-probe-tool-loop.ts b/packages/coding-agent/test/sdk-codex-cache-probe-tool-loop.ts new file mode 100644 index 0000000000000000000000000000000000000000..8a6ca1b415bb7c5c448401db650fde4dfccb8fe0 --- /dev/null +++ b/packages/coding-agent/test/sdk-codex-cache-probe-tool-loop.ts @@ -0,0 +1,499 @@ +#!/usr/bin/env node +/** + * Manual SDK probe for OpenAI Codex prompt caching through the tool loop. + * + * Runs append-only multi-turn prompting through createAgentSession(), forcing one + * deterministic custom tool call per top-level user turn. Logs per-subrequest + * assistant usage so cache-read monotonicity can be inspected inside a tool loop. + */ + +import { mkdirSync } from "node:fs"; +import { tmpdir } from "node:os"; +import { dirname, join, resolve } from "node:path"; +import process from "node:process"; +import { + type Api, + type AssistantMessage, + type AssistantMessageEventStream, + type Context, + getModel, + type Model, + type SimpleStreamOptions, + Type, +} from "@earendil-works/pi-ai/compat"; +import { + getOpenAICodexWebSocketDebugStats, + streamSimple as streamSimpleOpenAICodexResponses, +} from "../../ai/src/api/openai-codex-responses.ts"; +import { AuthStorage } from "../src/core/auth-storage.ts"; +import { createExtensionRuntime } from "../src/core/extensions/loader.ts"; +import type { ToolDefinition } from "../src/core/extensions/types.ts"; +import type { ResourceLoader } from "../src/core/resource-loader.ts"; +import { createAgentSession } from "../src/core/sdk.ts"; +import { SessionManager } from "../src/core/session-manager.ts"; +import { SettingsManager } from "../src/core/settings-manager.ts"; +import { createModelRegistry, getModelRuntime } from "./model-runtime-test-utils.ts"; + +type Transport = "sse" | "websocket" | "websocket-cached" | "auto"; + +interface Args { + turns: number; + sessionPath: string; + transport: Transport; + maxTokens: number; +} + +interface WebSocketStatsSnapshot { + requests: number; + connectionsCreated: number; + connectionsReused: number; + cachedContextRequests: number; + storeTrueRequests: number; + fullContextRequests: number; + deltaRequests: number; +} + +interface SubrequestRecord { + turn: number; + subrequest: number; + elapsedMs: number; + usage: AssistantMessage["usage"]; + stopReason: AssistantMessage["stopReason"]; + text: string; +} + +const DEFAULT_TURNS = 20; +const MIN_TURNS = 20; +const MAX_TURNS = 50; +const DEFAULT_MAX_TOKENS = 64; + +function parseArgs(argv: string[]): Args { + let turns = DEFAULT_TURNS; + let sessionPath = resolve(join(tmpdir(), `pi-sdk-codex-cache-probe-tool-loop-${Date.now()}.jsonl`)); + let transport: Transport = "sse"; + let maxTokens = DEFAULT_MAX_TOKENS; + + for (let i = 0; i < argv.length; i++) { + const arg = argv[i]; + switch (arg) { + case "--turns": { + const value = argv[++i]; + if (!value) throw new Error("Missing value for --turns"); + turns = Number.parseInt(value, 10); + break; + } + case "--session": { + const value = argv[++i]; + if (!value) throw new Error("Missing value for --session"); + sessionPath = resolve(value); + break; + } + case "--transport": { + const value = argv[++i]; + if (value !== "sse" && value !== "websocket" && value !== "websocket-cached" && value !== "auto") { + throw new Error(`Invalid --transport value: ${value}`); + } + transport = value; + break; + } + case "--max-tokens": { + const value = argv[++i]; + if (!value) throw new Error("Missing value for --max-tokens"); + maxTokens = Number.parseInt(value, 10); + break; + } + case "--help": { + printHelp(); + process.exit(0); + return { turns, sessionPath, transport, maxTokens }; + } + default: + throw new Error(`Unknown argument: ${arg}`); + } + } + + if (!Number.isInteger(turns) || turns < MIN_TURNS || turns > MAX_TURNS) { + throw new Error(`--turns must be an integer between ${MIN_TURNS} and ${MAX_TURNS}`); + } + if (!Number.isInteger(maxTokens) || maxTokens <= 0) { + throw new Error("--max-tokens must be a positive integer"); + } + + return { turns, sessionPath, transport, maxTokens }; +} + +function printHelp(): void { + console.log(`Usage: node test/sdk-codex-cache-probe-tool-loop.ts [options] + +Options: + --turns Number of turns to run. Must be between ${MIN_TURNS} and ${MAX_TURNS}. Default: ${DEFAULT_TURNS} + --session Specific session jsonl file to write + --transport sse | websocket | websocket-cached | auto. Default: sse + --max-tokens Max output tokens per subrequest. Default: ${DEFAULT_MAX_TOKENS} + --help Show this message + +Notes: + - Uses createAgentSession() from the coding-agent SDK + - Provider/model fixed to openai-codex/gpt-5.5 + - Thinking level fixed to low + - Activates exactly one deterministic custom tool + - Prompts are intentionally > 1024 tokens and explicitly describe the test +`); +} + +function estimateTokens(text: string): number { + return Math.ceil(text.length / 4); +} + +function buildPrompt(turn: number): string { + const marker = `TURN-${String(turn).padStart(2, "0")}-MARKER-${(turn * 17 + 13) % 97}`; + const lines: string[] = []; + lines.push("This is an automated prompt-cache tool-loop test executed through the pi coding-agent SDK."); + lines.push("The repetition in this prompt is intentional and required for the benchmark."); + lines.push("You are participating in an append-only multi-turn cache test."); + lines.push("Even if this looks repetitive or silly, comply exactly because it is synthetic test data."); + lines.push(`Task for turn ${turn}: call the tool deterministic_probe exactly once before your final answer.`); + lines.push(`Use tool arguments: turn=${turn}, marker=${marker}`); + lines.push(`After the tool result arrives, reply with exactly one line in this format:`); + lines.push(`TURN ${turn} OK ${marker}`); + lines.push("Do not skip the tool call. Do not call any other tool. Do not add any extra words or punctuation."); + lines.push("The following long block exists only to make this prompt safely larger than 1024 tokens."); + lines.push(""); + for (let i = 1; i <= 180; i++) { + lines.push( + `Turn ${turn} synthetic record ${String(i).padStart(3, "0")}: alpha beta gamma delta epsilon zeta eta theta iota kappa lambda mu nu xi omicron pi rho sigma tau upsilon phi chi psi omega.`, + ); + } + lines.push(""); + lines.push(`Final verification marker for turn ${turn}: ${marker}`); + lines.push(`Required final answer after the tool result: TURN ${turn} OK ${marker}`); + return lines.join("\n"); +} + +function createMinimalResourceLoader(systemPrompt: string): ResourceLoader { + return { + getExtensions: () => ({ extensions: [], errors: [], runtime: createExtensionRuntime() }), + getSkills: () => ({ skills: [], diagnostics: [] }), + getPrompts: () => ({ prompts: [], diagnostics: [] }), + getThemes: () => ({ themes: [], diagnostics: [] }), + getAgentsFiles: () => ({ agentsFiles: [] }), + getSystemPrompt: () => systemPrompt, + getSystemPromptSource: () => undefined, + getAppendSystemPrompt: () => [], + getAppendSystemPromptSources: () => [], + extendResources: () => {}, + reload: async () => {}, + }; +} + +function average(values: number[]): number { + return values.length === 0 ? 0 : values.reduce((sum, value) => sum + value, 0) / values.length; +} + +function percentile(values: number[], percentileValue: number): number { + if (values.length === 0) return 0; + const sorted = [...values].sort((a, b) => a - b); + const index = Math.min(sorted.length - 1, Math.max(0, Math.ceil((percentileValue / 100) * sorted.length) - 1)); + return sorted[index]; +} + +function getWebSocketStatsSnapshot(sessionId: string): WebSocketStatsSnapshot { + const stats = getOpenAICodexWebSocketDebugStats(sessionId); + return { + requests: stats?.requests ?? 0, + connectionsCreated: stats?.connectionsCreated ?? 0, + connectionsReused: stats?.connectionsReused ?? 0, + cachedContextRequests: stats?.cachedContextRequests ?? 0, + storeTrueRequests: stats?.storeTrueRequests ?? 0, + fullContextRequests: stats?.fullContextRequests ?? 0, + deltaRequests: stats?.deltaRequests ?? 0, + }; +} + +function diffWebSocketStats(after: WebSocketStatsSnapshot, before: WebSocketStatsSnapshot): WebSocketStatsSnapshot { + return { + requests: after.requests - before.requests, + connectionsCreated: after.connectionsCreated - before.connectionsCreated, + connectionsReused: after.connectionsReused - before.connectionsReused, + cachedContextRequests: after.cachedContextRequests - before.cachedContextRequests, + storeTrueRequests: after.storeTrueRequests - before.storeTrueRequests, + fullContextRequests: after.fullContextRequests - before.fullContextRequests, + deltaRequests: after.deltaRequests - before.deltaRequests, + }; +} + +function formatWebSocketStats(label: string, stats: WebSocketStatsSnapshot): string { + if (stats.requests === 0) return `${label} websocket none`; + return [ + `${label} websocket`, + `requests ${stats.requests}`, + `new/reused ${stats.connectionsCreated}/${stats.connectionsReused}`, + `cached ${stats.cachedContextRequests}`, + `store ${stats.storeTrueRequests}`, + `full/delta ${stats.fullContextRequests}/${stats.deltaRequests}`, + ].join(" | "); +} + +function getAssistantText(message: AssistantMessage): string { + return message.content + .filter((block): block is Extract => block.type === "text") + .map((block) => block.text) + .join("\n") + .trim(); +} + +const deterministicProbeParameters = Type.Object({ + turn: Type.Number({ description: "Top-level benchmark turn number" }), + marker: Type.String({ description: "Marker string provided by the user" }), +}); + +function deterministicProbeTool(): ToolDefinition { + return { + name: "deterministic_probe", + label: "Deterministic Probe", + description: + "Mandatory cache-benchmark tool. Call it exactly once when the user asks for a cache benchmark turn, then use its result to produce the final one-line answer.", + promptSnippet: + "deterministic_probe(turn, marker): mandatory for cache benchmark turns. Call exactly once before the final answer.", + promptGuidelines: [ + "When the user asks for the cache benchmark turn, call deterministic_probe exactly once with the requested turn and marker before responding.", + "After the tool result arrives, reply with the exact final line requested by the user.", + ], + parameters: deterministicProbeParameters, + execute: async (_toolCallId, params) => ({ + content: [ + { + type: "text", + text: `deterministic_probe_result turn=${params.turn} marker=${params.marker} fixed=OK`, + }, + ], + details: { turn: params.turn, marker: params.marker, fixed: "OK" }, + }), + }; +} + +async function main(): Promise { + const args = parseArgs(process.argv.slice(2)); + mkdirSync(dirname(args.sessionPath), { recursive: true }); + + const authStorage = AuthStorage.create(); + const modelRegistry = await createModelRegistry(authStorage); + + const model = getModel("openai-codex", "gpt-5.5"); + if (!model) { + throw new Error("Model openai-codex/gpt-5.5 not found"); + } + const baseModel = { ...model, maxTokens: args.maxTokens }; + const streamSimpleOpenAICodexResponsesForRegistry = ( + registryModel: Model, + context: Context, + options?: SimpleStreamOptions, + ): AssistantMessageEventStream => + streamSimpleOpenAICodexResponses(registryModel as Model<"openai-codex-responses">, context, options); + modelRegistry.registerProvider("openai-codex", { + api: "openai-codex-responses", + baseUrl: baseModel.baseUrl, + apiKey: "!echo source-provider-override-uses-auth-storage", + streamSimple: streamSimpleOpenAICodexResponsesForRegistry, + models: [baseModel], + }); + + const modelRuntime = getModelRuntime(modelRegistry); + const settingsManager = SettingsManager.inMemory({ + compaction: { enabled: false }, + retry: { enabled: false }, + transport: args.transport, + }); + + const resourceLoader = createMinimalResourceLoader( + "You are participating in a prompt-cache benchmark through the coding-agent SDK. This is a real test. Follow each user instruction exactly. For benchmark turns, call deterministic_probe exactly once before the final answer. Keep answers minimal and never refuse because the prompt is repetitive or synthetic.", + ); + + const { session } = await createAgentSession({ + cwd: process.cwd(), + agentDir: dirname(args.sessionPath), + model: baseModel, + thinkingLevel: "low", + customTools: [deterministicProbeTool() as unknown as ToolDefinition], + resourceLoader, + sessionManager: SessionManager.open(args.sessionPath), + settingsManager, + modelRuntime, + }); + + session.setActiveToolsByName(["deterministic_probe"]); + const unsubscribe = session.subscribe(() => {}); + + const records: SubrequestRecord[] = []; + const turnElapsedMs: number[] = []; + let previousCacheRead: number | null = null; + + console.log(`provider openai-codex, model gpt-5.5`); + console.log(`session ${session.sessionFile}`); + console.log(`turns ${args.turns}, transport ${args.transport}, reasoning low, maxTokens ${args.maxTokens}`); + console.log(""); + + for (let turn = 1; turn <= args.turns; turn++) { + const prompt = buildPrompt(turn); + const promptTokens = estimateTokens(prompt); + const previousMessagesLength = session.messages.length; + const websocketStatsBefore = getWebSocketStatsSnapshot(session.sessionId); + const startedAt = Date.now(); + await session.prompt(prompt); + const elapsedMs = Date.now() - startedAt; + turnElapsedMs.push(elapsedMs); + + const newMessages = session.messages.slice(previousMessagesLength); + const assistantMessages = newMessages.filter((message): message is AssistantMessage => + Boolean(message && typeof message === "object" && (message as { role?: unknown }).role === "assistant"), + ); + const toolResults = newMessages.filter((message) => + Boolean(message && typeof message === "object" && (message as { role?: unknown }).role === "toolResult"), + ); + + if (assistantMessages.length < 2 || toolResults.length < 1) { + throw new Error( + `Turn ${turn} did not execute the expected tool loop. assistants=${assistantMessages.length} toolResults=${toolResults.length}`, + ); + } + + let turnInput = 0; + let turnOutput = 0; + let turnCacheRead = 0; + let turnCacheWrite = 0; + let turnTotal = 0; + + for (let i = 0; i < assistantMessages.length; i++) { + const assistant = assistantMessages[i]; + const record: SubrequestRecord = { + turn, + subrequest: i + 1, + elapsedMs, + usage: assistant.usage, + stopReason: assistant.stopReason, + text: getAssistantText(assistant), + }; + records.push(record); + + turnInput += assistant.usage.input; + turnOutput += assistant.usage.output; + turnCacheRead += assistant.usage.cacheRead; + turnCacheWrite += assistant.usage.cacheWrite; + turnTotal += assistant.usage.totalTokens; + + const monotonic = + previousCacheRead === null ? "n/a" : assistant.usage.cacheRead >= previousCacheRead ? "yes" : "NO"; + console.log( + [ + `turn ${String(turn).padStart(2, "0")}.${i + 1}`, + `elapsed ${(elapsedMs / 1000).toFixed(1)}s`, + `prompt~${promptTokens}`, + `stop ${assistant.stopReason}`, + `in ${assistant.usage.input}`, + `out ${assistant.usage.output}`, + `cache ${assistant.usage.cacheRead}/${assistant.usage.cacheWrite}`, + `total ${assistant.usage.totalTokens}`, + `cache>=prev ${monotonic}`, + ].join(" | "), + ); + + if (assistant.stopReason === "error" || assistant.stopReason === "aborted") { + throw new Error( + `Turn ${turn}.${i + 1} ended with stopReason=${assistant.stopReason}: ${assistant.errorMessage || "unknown error"}`, + ); + } + previousCacheRead = assistant.usage.cacheRead; + } + + const websocketStatsAfter = getWebSocketStatsSnapshot(session.sessionId); + const websocketStatsForTurn = diffWebSocketStats(websocketStatsAfter, websocketStatsBefore); + console.log( + [ + `turn ${String(turn).padStart(2, "0")} agg`, + `assistants ${assistantMessages.length}`, + `toolResults ${toolResults.length}`, + `in ${turnInput}`, + `out ${turnOutput}`, + `cache ${turnCacheRead}/${turnCacheWrite}`, + `total ${turnTotal}`, + ].join(" | "), + ); + console.log(formatWebSocketStats(`turn ${String(turn).padStart(2, "0")}`, websocketStatsForTurn)); + } + + const violations = records + .map((record, index) => { + if (index === 0) return null; + const previous = records[index - 1]; + if (record.usage.cacheRead >= previous.usage.cacheRead) return null; + return { + turn: record.turn, + subrequest: record.subrequest, + previous: previous.usage.cacheRead, + current: record.usage.cacheRead, + }; + }) + .filter((value): value is NonNullable => value !== null); + + const totalElapsedMs = turnElapsedMs.reduce((sum, value) => sum + value, 0); + console.log(""); + console.log( + [ + "timing", + `turns ${turnElapsedMs.length}`, + `total ${(totalElapsedMs / 1000).toFixed(1)}s`, + `avg ${(average(turnElapsedMs) / 1000).toFixed(2)}s`, + `p50 ${(percentile(turnElapsedMs, 50) / 1000).toFixed(2)}s`, + `p95 ${(percentile(turnElapsedMs, 95) / 1000).toFixed(2)}s`, + `max ${(Math.max(...turnElapsedMs) / 1000).toFixed(2)}s`, + ].join(" | "), + ); + const websocketStats = getOpenAICodexWebSocketDebugStats(session.sessionId); + const requestedWebsocket = + args.transport === "websocket" || args.transport === "websocket-cached" || args.transport === "auto"; + const observedWebsocket = Boolean(websocketStats && websocketStats.requests > 0); + console.log( + [ + "transport summary", + `requested ${args.transport}`, + `observed ${observedWebsocket ? "websocket" : "sse/no-websocket"}`, + `sseFallbackSuspected ${requestedWebsocket && !observedWebsocket ? "yes" : "no"}`, + `cachedContext ${websocketStats?.cachedContextRequests ? "yes" : "no"}`, + `storeTrue ${websocketStats ? `${websocketStats.storeTrueRequests}/${websocketStats.requests}` : "0/0"}`, + `delta ${websocketStats ? `${websocketStats.deltaRequests}/${websocketStats.requests}` : "0/0"}`, + `full ${websocketStats ? `${websocketStats.fullContextRequests}/${websocketStats.requests}` : "0/0"}`, + ].join(" | "), + ); + if (websocketStats) { + console.log( + [ + "websocket details", + `requests ${websocketStats.requests}`, + `connections created/reused ${websocketStats.connectionsCreated}/${websocketStats.connectionsReused}`, + `cachedContext ${websocketStats.cachedContextRequests}`, + `storeTrue ${websocketStats.storeTrueRequests}`, + `full/delta ${websocketStats.fullContextRequests}/${websocketStats.deltaRequests}`, + `lastInputItems ${websocketStats.lastInputItems}`, + `lastDeltaItems ${websocketStats.lastDeltaInputItems ?? "n/a"}`, + `lastPreviousResponseId ${websocketStats.lastPreviousResponseId ?? "n/a"}`, + ].join(" | "), + ); + } + console.log(`subrequest cache read monotonic: ${violations.length === 0 ? "yes" : "NO"}`); + if (violations.length > 0) { + console.log("violations:"); + for (const violation of violations) { + console.log(` turn ${violation.turn}.${violation.subrequest}: ${violation.previous} -> ${violation.current}`); + } + } + console.log(`session file: ${session.sessionFile}`); + + unsubscribe(); + session.dispose(); +} + +main().catch((error: unknown) => { + const message = error instanceof Error ? error.message : String(error); + console.error(message); + process.exitCode = 1; +}); diff --git a/packages/coding-agent/test/sdk-openrouter-attribution.test.ts b/packages/coding-agent/test/sdk-openrouter-attribution.test.ts new file mode 100644 index 0000000000000000000000000000000000000000..8958cdde9783d70d5a1e5e700082b9394ef10d81 --- /dev/null +++ b/packages/coding-agent/test/sdk-openrouter-attribution.test.ts @@ -0,0 +1,267 @@ +import { existsSync, mkdirSync, rmSync } from "node:fs"; +import { tmpdir } from "node:os"; +import { join } from "node:path"; +import { + type Api, + type AssistantMessage, + createAssistantMessageEventStream, + type Model, + type ProviderHeaders, + type SimpleStreamOptions, +} from "@earendil-works/pi-ai"; +import { afterEach, beforeEach, describe, expect, it } from "vitest"; +import { AuthStorage } from "../src/core/auth-storage.ts"; +import { createAgentSession } from "../src/core/sdk.ts"; +import { SessionManager } from "../src/core/session-manager.ts"; +import { SettingsManager } from "../src/core/settings-manager.ts"; + +import { createInMemoryModelRegistry, getModelRuntime } from "./model-runtime-test-utils.ts"; + +describe("createAgentSession provider attribution headers", () => { + let tempDir: string; + let cwd: string; + let agentDir: string; + let originalTelemetryEnv: string | undefined; + + beforeEach(() => { + tempDir = join(tmpdir(), `pi-sdk-attribution-test-${Date.now()}-${Math.random().toString(36).slice(2)}`); + cwd = join(tempDir, "project"); + agentDir = join(tempDir, "agent"); + mkdirSync(cwd, { recursive: true }); + mkdirSync(agentDir, { recursive: true }); + originalTelemetryEnv = process.env.PI_TELEMETRY; + delete process.env.PI_TELEMETRY; + }); + + afterEach(() => { + if (originalTelemetryEnv === undefined) { + delete process.env.PI_TELEMETRY; + } else { + process.env.PI_TELEMETRY = originalTelemetryEnv; + } + if (tempDir && existsSync(tempDir)) { + rmSync(tempDir, { recursive: true, force: true }); + } + }); + + function createModel(provider: string, baseUrl: string, id = `${provider}-test-model`): Model { + return { + id, + name: `${provider} Test Model`, + api: "openai-completions", + provider, + baseUrl, + reasoning: false, + input: ["text"], + cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 }, + contextWindow: 128000, + maxTokens: 4096, + }; + } + + function createDoneStream() { + const stream = createAssistantMessageEventStream(); + const message: AssistantMessage = { + role: "assistant", + content: [{ type: "text", text: "ok" }], + api: "openai-completions", + provider: "capture-provider", + model: "capture-model", + usage: { + input: 0, + output: 0, + cacheRead: 0, + cacheWrite: 0, + totalTokens: 0, + cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 }, + }, + stopReason: "stop", + timestamp: Date.now(), + }; + stream.end(message); + return stream; + } + + async function captureHeaders( + model: Model, + options: { + telemetryEnabled?: boolean; + providerHeaders?: Record; + requestHeaders?: Record; + sessionId?: string; + } = {}, + ): Promise { + const settingsManager = SettingsManager.create(cwd, agentDir); + if (options.telemetryEnabled === false) { + settingsManager.setEnableInstallTelemetry(false); + } + + const authStorage = AuthStorage.inMemory({ + [model.provider]: { type: "api_key", key: "test-api-key" }, + }); + const modelRegistry = await createInMemoryModelRegistry(authStorage); + let capturedOptions: SimpleStreamOptions | undefined; + + modelRegistry.registerProvider(model.provider, { + api: model.api, + headers: options.providerHeaders, + streamSimple: (_model, _context, providerOptions) => { + capturedOptions = providerOptions; + return createDoneStream(); + }, + }); + + const modelRuntime = getModelRuntime(modelRegistry); + const sessionManager = SessionManager.inMemory(cwd); + if (options.sessionId) { + sessionManager.newSession({ id: options.sessionId }); + } + + const { session } = await createAgentSession({ + cwd, + agentDir, + model, + modelRuntime, + settingsManager, + sessionManager, + }); + + try { + const stream = await session.agent.streamFunction( + model, + { messages: [] }, + { + sessionId: session.sessionId, + ...(options.requestHeaders ? { headers: options.requestHeaders } : {}), + }, + ); + await stream.result(); + return capturedOptions?.headers; + } finally { + session.dispose(); + modelRegistry.unregisterProvider(model.provider); + } + } + + it("adds default attribution headers for OpenRouter models", async () => { + const headers = await captureHeaders(createModel("openrouter", "https://openrouter.ai/api/v1")); + + expect(headers?.["HTTP-Referer"]).toBe("https://pi.dev"); + expect(headers?.["X-OpenRouter-Title"]).toBe("pi"); + expect(headers?.["X-OpenRouter-Categories"]).toBe("cli-agent"); + }); + + it("does not add attribution headers when telemetry is disabled", async () => { + const headers = await captureHeaders(createModel("openrouter", "https://openrouter.ai/api/v1"), { + telemetryEnabled: false, + }); + + expect(headers?.["HTTP-Referer"]).toBeUndefined(); + expect(headers?.["X-OpenRouter-Title"]).toBeUndefined(); + expect(headers?.["X-OpenRouter-Categories"]).toBeUndefined(); + }); + + it("adds attribution headers for custom providers routed through OpenRouter", async () => { + const headers = await captureHeaders(createModel("custom-openrouter", "https://openrouter.ai/api/v1")); + + expect(headers?.["HTTP-Referer"]).toBe("https://pi.dev"); + expect(headers?.["X-OpenRouter-Title"]).toBe("pi"); + expect(headers?.["X-OpenRouter-Categories"]).toBe("cli-agent"); + }); + + it("preserves legacy OpenRouter base URL substring attribution matching", async () => { + const headers = await captureHeaders(createModel("custom-openrouter", "not-a-url-openrouter.ai")); + + expect(headers?.["HTTP-Referer"]).toBe("https://pi.dev"); + expect(headers?.["X-OpenRouter-Title"]).toBe("pi"); + expect(headers?.["X-OpenRouter-Categories"]).toBe("cli-agent"); + }); + + it("lets provider and request headers override the defaults", async () => { + const headers = await captureHeaders(createModel("openrouter", "https://openrouter.ai/api/v1"), { + providerHeaders: { + "HTTP-Referer": "https://provider.example", + "X-OpenRouter-Categories": "provider-category", + }, + requestHeaders: { + "X-OpenRouter-Title": "request-title", + }, + }); + + expect(headers?.["HTTP-Referer"]).toBe("https://provider.example"); + expect(headers?.["X-OpenRouter-Title"]).toBe("request-title"); + expect(headers?.["X-OpenRouter-Categories"]).toBe("provider-category"); + }); + + it("adds default attribution headers for direct NVIDIA NIM endpoints", async () => { + const headers = await captureHeaders(createModel("custom-nim", "https://integrate.api.nvidia.com/v1")); + + expect(headers?.["X-BILLING-INVOKE-ORIGIN"]).toBe("Pi"); + }); + + it("adds default attribution headers for the NVIDIA provider", async () => { + const headers = await captureHeaders(createModel("nvidia", "https://example.test/v1")); + + expect(headers?.["X-BILLING-INVOKE-ORIGIN"]).toBe("Pi"); + }); + + it("does not add NVIDIA NIM attribution headers when telemetry is disabled", async () => { + const headers = await captureHeaders(createModel("nvidia", "https://integrate.api.nvidia.com/v1"), { + telemetryEnabled: false, + }); + + expect(headers?.["X-BILLING-INVOKE-ORIGIN"]).toBeUndefined(); + }); + + it("lets provider and request headers override NVIDIA NIM defaults", async () => { + const headers = await captureHeaders(createModel("nvidia", "https://integrate.api.nvidia.com/v1"), { + providerHeaders: { + "X-BILLING-INVOKE-ORIGIN": "Provider", + }, + requestHeaders: { + "X-BILLING-INVOKE-ORIGIN": "Request", + }, + }); + + expect(headers?.["X-BILLING-INVOKE-ORIGIN"]).toBe("Request"); + }); + + it("does not add NVIDIA NIM attribution headers for NVIDIA models routed through OpenRouter", async () => { + const headers = await captureHeaders( + createModel("openrouter", "https://openrouter.ai/api/v1", "nvidia/nemotron-3-super-120b-a12b"), + ); + + expect(headers?.["HTTP-Referer"]).toBe("https://pi.dev"); + expect(headers?.["X-BILLING-INVOKE-ORIGIN"]).toBeUndefined(); + }); + + it("does not add NVIDIA NIM attribution headers for NVIDIA models routed through Vercel AI Gateway", async () => { + const headers = await captureHeaders( + createModel("vercel-ai-gateway", "https://ai-gateway.vercel.sh/v1", "nvidia/nemotron-3-super-120b-a12b"), + ); + + expect(headers?.["X-BILLING-INVOKE-ORIGIN"]).toBeUndefined(); + }); + + it("adds OpenCode session headers", async () => { + const headers = await captureHeaders(createModel("opencode", "https://opencode.ai/zen/v1"), { + sessionId: "opencode-session", + }); + + expect(headers?.["x-opencode-session"]).toBe("opencode-session"); + expect(headers?.["x-opencode-client"]).toBe("pi"); + }); + + it("lets configured OpenCode headers override the defaults", async () => { + const headers = await captureHeaders(createModel("opencode", "https://opencode.ai/zen/v1"), { + sessionId: "opencode-session", + providerHeaders: { + "x-opencode-session": "configured-session", + "x-opencode-client": "configured-client", + }, + }); + + expect(headers?.["x-opencode-session"]).toBe("configured-session"); + expect(headers?.["x-opencode-client"]).toBe("configured-client"); + }); +}); diff --git a/packages/coding-agent/test/sdk-session-manager.test.ts b/packages/coding-agent/test/sdk-session-manager.test.ts new file mode 100644 index 0000000000000000000000000000000000000000..8ab4c431dfe36320a055d1b7c7796da1b5342518 --- /dev/null +++ b/packages/coding-agent/test/sdk-session-manager.test.ts @@ -0,0 +1,131 @@ +import { existsSync, mkdirSync, realpathSync, rmSync } from "node:fs"; +import { tmpdir } from "node:os"; +import { join } from "node:path"; +import { getModel } from "@earendil-works/pi-ai/compat"; +import { afterEach, beforeEach, describe, expect, it } from "vitest"; +import { createAgentSession } from "../src/core/sdk.ts"; +import { SessionManager } from "../src/core/session-manager.ts"; + +describe("createAgentSession session manager defaults", () => { + let tempDir: string; + let cwd: string; + let agentDir: string; + + beforeEach(() => { + tempDir = join(tmpdir(), `pi-sdk-session-test-${Date.now()}-${Math.random().toString(36).slice(2)}`); + cwd = join(tempDir, "project"); + agentDir = join(tempDir, "agent"); + mkdirSync(cwd, { recursive: true }); + mkdirSync(agentDir, { recursive: true }); + }); + + afterEach(() => { + if (tempDir && existsSync(tempDir)) { + rmSync(tempDir, { recursive: true, force: true }); + } + }); + + it("uses agentDir for the default persisted session path", async () => { + const model = getModel("anthropic", "claude-sonnet-4-5"); + expect(model).toBeTruthy(); + + const { session } = await createAgentSession({ + cwd, + agentDir, + model: model!, + }); + + const safePath = `--${cwd.replace(/^[/\\]/, "").replace(/[/\\:]/g, "-")}--`; + const expectedSessionDir = join(agentDir, "sessions", safePath); + const sessionDir = session.sessionManager.getSessionDir(); + const sessionFile = session.sessionManager.getSessionFile(); + + expect(sessionDir).toBe(expectedSessionDir); + expect(sessionFile?.startsWith(`${expectedSessionDir}/`)).toBe(true); + + session.dispose(); + }); + + it("keeps an explicit sessionManager override", async () => { + const model = getModel("anthropic", "claude-sonnet-4-5"); + expect(model).toBeTruthy(); + + const sessionManager = SessionManager.inMemory(cwd); + const { session } = await createAgentSession({ + cwd, + agentDir, + model: model!, + sessionManager, + }); + + expect(session.sessionManager).toBe(sessionManager); + expect(session.sessionManager.isPersisted()).toBe(false); + + session.dispose(); + }); + + it("derives cwd from an explicit sessionManager when cwd is omitted", async () => { + const model = getModel("anthropic", "claude-sonnet-4-5"); + expect(model).toBeTruthy(); + + const sessionCwd = join(tempDir, "session-project"); + mkdirSync(sessionCwd, { recursive: true }); + const sessionManager = SessionManager.inMemory(sessionCwd); + const { session } = await createAgentSession({ + agentDir, + model: model!, + sessionManager, + }); + + expect(session.sessionManager).toBe(sessionManager); + expect(session.systemPrompt).toContain(`Current working directory: ${sessionCwd}`); + + const bashTool = session.agent.state.tools.find((tool) => tool.name === "bash"); + expect(bashTool).toBeTruthy(); + const result = await bashTool!.execute("test", { command: "pwd" }); + const output = result.content + .filter((item): item is { type: "text"; text: string } => item.type === "text") + .map((item) => item.text) + .join(""); + + expect(realpathSync(output.trim())).toBe(realpathSync(sessionCwd)); + + session.dispose(); + }); + + it("exposes current session state to the built-in bash tool", async () => { + const model = getModel("anthropic", "claude-sonnet-4-5"); + expect(model).toBeTruthy(); + + const { session } = await createAgentSession({ + cwd, + agentDir, + model: model!, + thinkingLevel: "high", + }); + expect(session.sessionFile).toBeTruthy(); + expect(session.systemPrompt).toContain( + "You can inspect PI_* environment variables for current model and session details.", + ); + + const bashTool = session.agent.state.tools.find((tool) => tool.name === "bash"); + expect(bashTool).toBeTruthy(); + const result = await bashTool!.execute("test", { + command: `printf '%s\\n' "$PI_SESSION_ID" "$PI_SESSION_FILE" "$PI_PROVIDER" "$PI_MODEL" "$PI_REASONING_LEVEL"`, + }); + const output = result.content + .filter((item): item is { type: "text"; text: string } => item.type === "text") + .map((item) => item.text) + .join(""); + + expect(output.trim().split("\n")).toEqual([ + session.sessionId, + session.sessionFile, + model!.provider, + model!.id, + session.thinkingLevel, + ]); + + session.dispose(); + }); +}); diff --git a/packages/coding-agent/test/sdk-skills.test.ts b/packages/coding-agent/test/sdk-skills.test.ts new file mode 100644 index 0000000000000000000000000000000000000000..007a514049dbf4a0bd6d63f92e3c97e745ec5891 --- /dev/null +++ b/packages/coding-agent/test/sdk-skills.test.ts @@ -0,0 +1,113 @@ +import { mkdirSync, rmSync, writeFileSync } from "node:fs"; +import { tmpdir } from "node:os"; +import { join } from "node:path"; +import { afterEach, beforeEach, describe, expect, it } from "vitest"; +import { createExtensionRuntime } from "../src/core/extensions/loader.ts"; +import type { ResourceLoader } from "../src/core/resource-loader.ts"; +import { createAgentSession } from "../src/core/sdk.ts"; +import { SessionManager } from "../src/core/session-manager.ts"; +import { createSyntheticSourceInfo } from "../src/core/source-info.ts"; + +describe("createAgentSession skills option", () => { + let tempDir: string; + let skillsDir: string; + + beforeEach(() => { + tempDir = join(tmpdir(), `pi-sdk-test-${Date.now()}-${Math.random().toString(36).slice(2)}`); + skillsDir = join(tempDir, "skills", "test-skill"); + mkdirSync(skillsDir, { recursive: true }); + + // Create a test skill in the pi skills directory + writeFileSync( + join(skillsDir, "SKILL.md"), + `--- +name: test-skill +description: A test skill for SDK tests. +--- + +# Test Skill + +This is a test skill. +`, + ); + }); + + afterEach(() => { + if (tempDir) { + rmSync(tempDir, { recursive: true, force: true }); + } + }); + + it("should discover skills by default and expose them on session.skills", async () => { + const { session } = await createAgentSession({ + cwd: tempDir, + agentDir: tempDir, + sessionManager: SessionManager.inMemory(), + }); + + // Skills should be discovered and exposed on the session + expect(session.resourceLoader.getSkills().skills.length).toBeGreaterThan(0); + expect(session.resourceLoader.getSkills().skills.some((s) => s.name === "test-skill")).toBe(true); + }); + + it("should have empty skills when resource loader returns none (--no-skills)", async () => { + const resourceLoader: ResourceLoader = { + getExtensions: () => ({ extensions: [], errors: [], runtime: createExtensionRuntime() }), + getSkills: () => ({ skills: [], diagnostics: [] }), + getPrompts: () => ({ prompts: [], diagnostics: [] }), + getThemes: () => ({ themes: [], diagnostics: [] }), + getAgentsFiles: () => ({ agentsFiles: [] }), + getSystemPrompt: () => undefined, + getSystemPromptSource: () => undefined, + getAppendSystemPrompt: () => [], + getAppendSystemPromptSources: () => [], + extendResources: () => {}, + reload: async () => {}, + }; + + const { session } = await createAgentSession({ + cwd: tempDir, + agentDir: tempDir, + sessionManager: SessionManager.inMemory(), + resourceLoader, + }); + + expect(session.resourceLoader.getSkills().skills).toEqual([]); + expect(session.resourceLoader.getSkills().diagnostics).toEqual([]); + }); + + it("should use provided skills when resource loader supplies them", async () => { + const customSkill = { + name: "custom-skill", + description: "A custom skill", + filePath: "/fake/path/SKILL.md", + baseDir: "/fake/path", + sourceInfo: createSyntheticSourceInfo("/fake/path/SKILL.md", { source: "sdk" }), + disableModelInvocation: false, + }; + + const resourceLoader: ResourceLoader = { + getExtensions: () => ({ extensions: [], errors: [], runtime: createExtensionRuntime() }), + getSkills: () => ({ skills: [customSkill], diagnostics: [] }), + getPrompts: () => ({ prompts: [], diagnostics: [] }), + getThemes: () => ({ themes: [], diagnostics: [] }), + getAgentsFiles: () => ({ agentsFiles: [] }), + getSystemPrompt: () => undefined, + getSystemPromptSource: () => undefined, + getAppendSystemPrompt: () => [], + getAppendSystemPromptSources: () => [], + extendResources: () => {}, + reload: async () => {}, + }; + + const { session } = await createAgentSession({ + cwd: tempDir, + agentDir: tempDir, + sessionManager: SessionManager.inMemory(), + resourceLoader, + }); + + expect(session.resourceLoader.getSkills().skills).toEqual([customSkill]); + expect(session.resourceLoader.getSkills().diagnostics).toEqual([]); + }); +}); diff --git a/packages/coding-agent/test/sdk-stream-options.test.ts b/packages/coding-agent/test/sdk-stream-options.test.ts new file mode 100644 index 0000000000000000000000000000000000000000..c01df5f20a8e071e5edf1d1d2fc7f3c76c938bc4 --- /dev/null +++ b/packages/coding-agent/test/sdk-stream-options.test.ts @@ -0,0 +1,197 @@ +import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from "node:fs"; +import { tmpdir } from "node:os"; +import { join } from "node:path"; +import { + type Api, + type AssistantMessage, + createAssistantMessageEventStream, + type Model, + type SimpleStreamOptions, +} from "@earendil-works/pi-ai"; +import { afterEach, beforeEach, describe, expect, it } from "vitest"; +import { AuthStorage } from "../src/core/auth-storage.ts"; +import { createAgentSession } from "../src/core/sdk.ts"; +import { SessionManager } from "../src/core/session-manager.ts"; +import { type Settings, SettingsManager } from "../src/core/settings-manager.ts"; + +import { createModelRegistry, getModelRuntime } from "./model-runtime-test-utils.ts"; + +describe("createAgentSession stream options", () => { + let tempDir: string; + let cwd: string; + let agentDir: string; + + beforeEach(() => { + tempDir = mkdtempSync(join(tmpdir(), "pi-sdk-stream-options-")); + cwd = join(tempDir, "project"); + agentDir = join(tempDir, "agent"); + mkdirSync(cwd, { recursive: true }); + mkdirSync(agentDir, { recursive: true }); + }); + + afterEach(() => { + if (tempDir) { + rmSync(tempDir, { recursive: true, force: true }); + } + }); + + function createModel(api: Api): Model { + return { + id: "capture-model", + name: "Capture Model", + api, + provider: "capture-provider", + baseUrl: "https://capture.invalid/v1", + reasoning: false, + input: ["text"], + cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 }, + contextWindow: 128000, + maxTokens: 4096, + headers: { "x-model": "model" }, + }; + } + + function createDoneStream(api: Api) { + const stream = createAssistantMessageEventStream(); + const message: AssistantMessage = { + role: "assistant", + content: [{ type: "text", text: "ok" }], + api, + provider: "capture-provider", + model: "capture-model", + usage: { + input: 0, + output: 0, + cacheRead: 0, + cacheWrite: 0, + totalTokens: 0, + cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 }, + }, + stopReason: "stop", + timestamp: Date.now(), + }; + stream.end(message); + return stream; + } + + async function captureStreamOptions( + api: Api, + settings: Partial, + requestOptions: SimpleStreamOptions = {}, + extensionSource?: string, + ): Promise { + const model = createModel(api); + const settingsManager = SettingsManager.inMemory(settings); + if (extensionSource) { + const extensionsDir = join(agentDir, "extensions"); + mkdirSync(extensionsDir, { recursive: true }); + writeFileSync(join(extensionsDir, "headers.ts"), extensionSource); + } + + const authStorage = AuthStorage.create(join(agentDir, "auth.json")); + await authStorage.modify(model.provider, async () => ({ type: "api_key", key: "test-api-key" })); + const modelRegistry = await createModelRegistry(authStorage, join(agentDir, "models.json")); + let capturedOptions: SimpleStreamOptions | undefined; + + modelRegistry.registerProvider(model.provider, { + api, + headers: { "x-provider": "provider" }, + streamSimple: (_model, _context, providerOptions) => { + capturedOptions = providerOptions; + return createDoneStream(api); + }, + }); + + const modelRuntime = getModelRuntime(modelRegistry); + const sessionManager = SessionManager.inMemory(cwd); + const { session } = await createAgentSession({ + cwd, + agentDir, + model, + modelRuntime, + settingsManager, + sessionManager, + }); + + try { + const stream = await session.agent.streamFunction(model, { messages: [] }, requestOptions); + await stream.result(); + return capturedOptions; + } finally { + session.dispose(); + modelRegistry.unregisterProvider(model.provider); + } + } + + it("forwards httpIdleTimeoutMs as timeoutMs for OpenAI Codex", async () => { + const options = await captureStreamOptions("openai-codex-responses", { httpIdleTimeoutMs: 1234 }); + + expect(options?.timeoutMs).toBe(1234); + }); + + it("defaults timeoutMs from httpIdleTimeoutMs for all providers", async () => { + const options = await captureStreamOptions("openai-completions", { httpIdleTimeoutMs: 1234 }); + + expect(options?.timeoutMs).toBe(1234); + }); + + it("lets request timeoutMs override httpIdleTimeoutMs for OpenAI Codex", async () => { + const options = await captureStreamOptions( + "openai-codex-responses", + { httpIdleTimeoutMs: 1234 }, + { timeoutMs: 0 }, + ); + + expect(options?.timeoutMs).toBe(0); + }); + + it("forwards websocketConnectTimeoutMs from settings", async () => { + const options = await captureStreamOptions("openai-codex-responses", { websocketConnectTimeoutMs: 1234 }); + + expect(options?.websocketConnectTimeoutMs).toBe(1234); + }); + + it("lets request websocketConnectTimeoutMs override settings", async () => { + const options = await captureStreamOptions( + "openai-codex-responses", + { websocketConnectTimeoutMs: 1234 }, + { websocketConnectTimeoutMs: 0 }, + ); + + expect(options?.websocketConnectTimeoutMs).toBe(0); + }); + + it("forwards provider retry settings", async () => { + const options = await captureStreamOptions("openai-completions", { + retry: { provider: { maxRetries: 2, maxRetryDelayMs: 3000 } }, + }); + + expect(options?.maxRetries).toBe(2); + expect(options?.maxRetryDelayMs).toBe(3000); + }); + + it("runs before_provider_headers on assembled headers without forwarding the transform", async () => { + const options = await captureStreamOptions( + "openai-completions", + {}, + { headers: { "x-explicit": "explicit" } }, + `export default function (pi) { + pi.on("before_provider_headers", (event) => { + event.headers["x-hook"] = [ + event.headers["x-provider"], + event.headers["x-model"], + event.headers["x-explicit"], + ].join(":"); + }); + }`, + ); + + expect(options?.headers).toMatchObject({ + "x-provider": "provider", + "x-model": "model", + "x-explicit": "explicit", + "x-hook": "provider:model:explicit", + }); + expect(options).not.toHaveProperty("transformHeaders"); + }); +}); diff --git a/packages/coding-agent/test/session-cwd.test.ts b/packages/coding-agent/test/session-cwd.test.ts new file mode 100644 index 0000000000000000000000000000000000000000..8a7d23fca40c6e32330fffa873460fcd24a545cc --- /dev/null +++ b/packages/coding-agent/test/session-cwd.test.ts @@ -0,0 +1,91 @@ +import { mkdirSync, rmSync, writeFileSync } from "node:fs"; +import { tmpdir } from "node:os"; +import { join } from "node:path"; +import { afterEach, describe, expect, it } from "vitest"; +import { type CreateAgentSessionRuntimeFactory, createAgentSessionRuntime } from "../src/core/agent-session-runtime.ts"; +import { getMissingSessionCwdIssue, MissingSessionCwdError } from "../src/core/session-cwd.ts"; +import { SessionManager } from "../src/core/session-manager.ts"; + +function createTempDir(name: string): string { + const dir = join(tmpdir(), `${name}-${Date.now()}-${Math.random().toString(36).slice(2)}`); + mkdirSync(dir, { recursive: true }); + return dir; +} + +function writeSessionFile(path: string, cwd: string): void { + writeFileSync( + path, + `${JSON.stringify({ + type: "session", + version: 3, + id: "session-id", + timestamp: new Date().toISOString(), + cwd, + })}\n`, + ); +} + +describe("session cwd handling", () => { + const cleanupPaths: string[] = []; + + afterEach(() => { + for (const path of cleanupPaths.splice(0)) { + rmSync(path, { recursive: true, force: true }); + } + }); + + it("detects missing session cwd from persisted sessions", () => { + const fallbackCwd = createTempDir("pi-session-cwd-fallback"); + const missingCwd = join(fallbackCwd, "does-not-exist"); + const sessionDir = createTempDir("pi-session-cwd-session-dir"); + const sessionFile = join(sessionDir, "session.jsonl"); + cleanupPaths.push(fallbackCwd, sessionDir); + writeSessionFile(sessionFile, missingCwd); + + const sessionManager = SessionManager.open(sessionFile); + const issue = getMissingSessionCwdIssue(sessionManager, fallbackCwd); + expect(issue).toEqual({ + sessionFile: sessionManager.getSessionFile(), + sessionCwd: missingCwd, + fallbackCwd, + }); + }); + + it("supports overriding the effective cwd when opening a session", () => { + const fallbackCwd = createTempDir("pi-session-cwd-override"); + const missingCwd = join(fallbackCwd, "does-not-exist"); + const sessionDir = createTempDir("pi-session-cwd-override-session-dir"); + const sessionFile = join(sessionDir, "session.jsonl"); + cleanupPaths.push(fallbackCwd, sessionDir); + writeSessionFile(sessionFile, missingCwd); + + const sessionManager = SessionManager.open(sessionFile, undefined, fallbackCwd); + expect(sessionManager.getCwd()).toBe(fallbackCwd); + expect(getMissingSessionCwdIssue(sessionManager, fallbackCwd)).toBeUndefined(); + }); + + it("throws a controlled error before runtime creation when the stored cwd is missing", async () => { + const fallbackCwd = createTempDir("pi-session-cwd-runtime"); + const missingCwd = join(fallbackCwd, "does-not-exist"); + const sessionDir = createTempDir("pi-session-cwd-runtime-session-dir"); + const sessionFile = join(sessionDir, "session.jsonl"); + cleanupPaths.push(fallbackCwd, sessionDir); + writeSessionFile(sessionFile, missingCwd); + + const sessionManager = SessionManager.open(sessionFile); + let createRuntimeCalled = false; + const createRuntime: CreateAgentSessionRuntimeFactory = async () => { + createRuntimeCalled = true; + throw new Error("should not be called"); + }; + + await expect( + createAgentSessionRuntime(createRuntime, { + cwd: fallbackCwd, + agentDir: fallbackCwd, + sessionManager, + }), + ).rejects.toBeInstanceOf(MissingSessionCwdError); + expect(createRuntimeCalled).toBe(false); + }); +}); diff --git a/packages/coding-agent/test/session-file-invalid.test.ts b/packages/coding-agent/test/session-file-invalid.test.ts new file mode 100644 index 0000000000000000000000000000000000000000..e2eb7015c1b38934b6308ed6cb5439b3e0bbe2a8 --- /dev/null +++ b/packages/coding-agent/test/session-file-invalid.test.ts @@ -0,0 +1,65 @@ +import { spawn } from "node:child_process"; +import { mkdirSync, mkdtempSync, readFileSync, realpathSync, rmSync, writeFileSync } from "node:fs"; +import { tmpdir } from "node:os"; +import { join, resolve } from "node:path"; +import { afterEach, describe, expect, it } from "vitest"; +import { ENV_AGENT_DIR } from "../src/config.ts"; + +const cliPath = resolve(__dirname, "../src/cli.ts"); +const sourceResolverPath = resolve(__dirname, "../src/experimental/source-resolver.ts"); +const tempDirs: string[] = []; + +afterEach(() => { + for (const dir of tempDirs.splice(0)) { + rmSync(dir, { recursive: true, force: true }); + } +}); + +function createTempDir(): string { + const dir = realpathSync(mkdtempSync(join(tmpdir(), "pi-session-file-invalid-"))); + tempDirs.push(dir); + return dir; +} + +async function runCli(args: string[], cwd: string, agentDir: string): Promise<{ code: number | null; stderr: string }> { + let stderr = ""; + const code = await new Promise((resolvePromise, reject) => { + const child = spawn(process.execPath, ["--import", sourceResolverPath, cliPath, ...args], { + cwd, + env: { + ...process.env, + [ENV_AGENT_DIR]: agentDir, + PI_OFFLINE: "1", + }, + stdio: ["ignore", "ignore", "pipe"], + }); + child.stderr.on("data", (chunk) => { + stderr += chunk.toString(); + }); + child.on("error", reject); + child.on("close", resolvePromise); + }); + + return { code, stderr }; +} + +describe("--session invalid file handling", () => { + it("prints a friendly error and preserves non-session file content", async () => { + const tempRoot = createTempDir(); + const agentDir = join(tempRoot, "agent"); + const projectDir = join(tempRoot, "project"); + const sessionFile = join(tempRoot, "not-a-session.log"); + const originalContent = '{"type":"event","data":"not a session"}\n'; + mkdirSync(agentDir, { recursive: true }); + mkdirSync(projectDir, { recursive: true }); + writeFileSync(sessionFile, originalContent); + + const result = await runCli(["--session", sessionFile, "-p", "hi"], projectDir, agentDir); + + expect(result.code).toBe(1); + expect(result.stderr).toContain(`Error: Session file is not a valid pi session: ${sessionFile}`); + expect(result.stderr).not.toContain("SessionManager.open"); + expect(result.stderr).not.toContain("at "); + expect(readFileSync(sessionFile, "utf8")).toBe(originalContent); + }); +}); diff --git a/packages/coding-agent/test/session-id-readonly.test.ts b/packages/coding-agent/test/session-id-readonly.test.ts new file mode 100644 index 0000000000000000000000000000000000000000..20a12997731e5b0b1c07f35b201edf1eef893123 --- /dev/null +++ b/packages/coding-agent/test/session-id-readonly.test.ts @@ -0,0 +1,173 @@ +import { spawn } from "node:child_process"; +import { existsSync, mkdirSync, mkdtempSync, readdirSync, readFileSync, realpathSync, rmSync } from "node:fs"; +import { tmpdir } from "node:os"; +import { join, resolve } from "node:path"; +import { afterEach, describe, expect, it, vi } from "vitest"; +import type { Args } from "../src/cli/args.ts"; +import { ENV_AGENT_DIR } from "../src/config.ts"; +import { SessionManager } from "../src/core/session-manager.ts"; +import { SettingsManager } from "../src/core/settings-manager.ts"; +import { createSessionManager } from "../src/main.ts"; + +const cliPath = resolve(__dirname, "../src/cli.ts"); +const sourceResolverPath = resolve(__dirname, "../src/experimental/source-resolver.ts"); +const tempDirs: string[] = []; + +afterEach(() => { + vi.restoreAllMocks(); + for (const dir of tempDirs.splice(0)) { + rmSync(dir, { recursive: true, force: true }); + } +}); + +function createTempDir(): string { + // realpath: on macOS tmpdir() is a symlink (/var -> /private/var), but the + // spawned CLI sees the physical path via process.cwd(). Session cwd + // filtering compares paths textually, so the fixture must use physical paths. + const dir = realpathSync(mkdtempSync(join(tmpdir(), "pi-session-id-readonly-"))); + tempDirs.push(dir); + return dir; +} + +function hasSessionWithId(root: string, sessionId: string): boolean { + if (!existsSync(root)) return false; + for (const entry of readdirSync(root, { withFileTypes: true })) { + const path = join(root, entry.name); + if (entry.isDirectory() && hasSessionWithId(path, sessionId)) return true; + if (!entry.isFile() || !entry.name.endsWith(".jsonl")) continue; + + try { + const firstLine = readFileSync(path, "utf8").split("\n", 1)[0]; + const header = JSON.parse(firstLine) as { type?: string; id?: string }; + if (header.type === "session" && header.id === sessionId) return true; + } catch { + // Ignore malformed session files. + } + } + return false; +} + +async function runCli(args: string[]): Promise<{ code: number | null; agentDir: string }> { + const tempRoot = createTempDir(); + const agentDir = join(tempRoot, "agent"); + const projectDir = join(tempRoot, "project"); + mkdirSync(agentDir, { recursive: true }); + mkdirSync(projectDir, { recursive: true }); + + const code = await new Promise((resolvePromise, reject) => { + const child = spawn(process.execPath, ["--import", sourceResolverPath, cliPath, ...args], { + cwd: projectDir, + env: { + ...process.env, + [ENV_AGENT_DIR]: agentDir, + PI_OFFLINE: "1", + }, + stdio: ["ignore", "ignore", "ignore"], + }); + child.on("error", reject); + child.on("close", resolvePromise); + }); + + return { code, agentDir }; +} + +function args(overrides: Partial): Args { + return { + messages: [], + fileArgs: [], + unknownFlags: new Map(), + diagnostics: [], + ...overrides, + }; +} + +function persistSession(session: SessionManager, content: string): void { + session.appendMessage({ role: "user", content, timestamp: Date.now() }); + session.appendMessage({ + role: "assistant", + content: [{ type: "text", text: "persisted" }], + api: "anthropic-messages", + provider: "anthropic", + model: "test", + usage: { + input: 1, + output: 1, + cacheRead: 0, + cacheWrite: 0, + totalTokens: 2, + cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 }, + }, + stopReason: "stop", + timestamp: Date.now(), + }); +} + +describe("--session-id", () => { + it("does not persist a custom ID for metadata commands", async () => { + const result = await runCli(["--session-id", "read-only-help", "--help"]); + + expect(result.code).toBe(0); + expect(hasSessionWithId(join(result.agentDir, "sessions"), "read-only-help")).toBe(false); + }); + + it("creates missing IDs and reopens existing IDs in process", async () => { + const tempRoot = createTempDir(); + const projectDir = join(tempRoot, "project"); + const sessionDir = join(tempRoot, "sessions"); + mkdirSync(projectDir, { recursive: true }); + const settingsManager = SettingsManager.inMemory(); + const consoleError = vi.spyOn(console, "error").mockImplementation(() => {}); + + const readOnly = await createSessionManager( + args({ sessionId: "read-only", help: true }), + projectDir, + sessionDir, + settingsManager, + ); + expect(readOnly.getSessionId()).toBe("read-only"); + expect(readOnly.getSessionFile()).toBeUndefined(); + + const created = await createSessionManager( + args({ sessionId: "persisted-id" }), + projectDir, + sessionDir, + settingsManager, + ); + persistSession(created, "persist me"); + expect(consoleError).toHaveBeenCalledWith(expect.stringContaining("creating a new session")); + + consoleError.mockClear(); + const reopened = await createSessionManager( + args({ sessionId: "persisted-id" }), + projectDir, + sessionDir, + settingsManager, + ); + expect(reopened.getSessionFile()).toBe(created.getSessionFile()); + expect(consoleError).not.toHaveBeenCalled(); + }); + + it("rejects an existing fork target in process", async () => { + const tempRoot = createTempDir(); + const projectDir = join(tempRoot, "project"); + const sessionDir = join(tempRoot, "sessions"); + mkdirSync(projectDir, { recursive: true }); + const source = SessionManager.create(projectDir, sessionDir, { id: "source-id" }); + persistSession(source, "source"); + const target = SessionManager.create(projectDir, sessionDir, { id: "existing-id" }); + persistSession(target, "target"); + vi.spyOn(console, "error").mockImplementation(() => {}); + vi.spyOn(process, "exit").mockImplementation((code) => { + throw new Error(`exit:${code}`); + }); + + await expect( + createSessionManager( + args({ fork: "source-id", sessionId: "existing-id" }), + projectDir, + sessionDir, + SettingsManager.inMemory(), + ), + ).rejects.toThrow("exit:1"); + }); +}); diff --git a/packages/coding-agent/test/session-info-modified-timestamp.test.ts b/packages/coding-agent/test/session-info-modified-timestamp.test.ts new file mode 100644 index 0000000000000000000000000000000000000000..a4322e460139c356dd5810a2d578db2a84018361 --- /dev/null +++ b/packages/coding-agent/test/session-info-modified-timestamp.test.ts @@ -0,0 +1,83 @@ +import { writeFileSync } from "node:fs"; +import { stat } from "node:fs/promises"; +import { tmpdir } from "node:os"; +import { dirname, join } from "node:path"; +import { afterEach, beforeAll, describe, expect, it, vi } from "vitest"; +import type { SessionHeader } from "../src/core/session-manager.ts"; +import { SessionManager } from "../src/core/session-manager.ts"; +import { initTheme } from "../src/modes/interactive/theme/theme.ts"; + +function createSessionFile(path: string): void { + const header: SessionHeader = { + type: "session", + id: "test-session", + version: 3, + timestamp: new Date(0).toISOString(), + cwd: "/tmp", + }; + writeFileSync(path, `${JSON.stringify(header)}\n`, "utf8"); + + // SessionManager only persists once it has seen at least one assistant message. + // Add a minimal assistant entry so subsequent appends are persisted. + const mgr = SessionManager.open(path); + mgr.appendMessage({ + role: "assistant", + content: [{ type: "text", text: "hi" }], + api: "openai-completions", + provider: "openai", + model: "test", + usage: { + input: 1, + output: 1, + cacheRead: 0, + cacheWrite: 0, + totalTokens: 2, + cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 }, + }, + stopReason: "stop", + timestamp: Date.now(), + }); +} + +describe("SessionInfo.modified", () => { + beforeAll(() => initTheme("dark")); + + afterEach(() => { + vi.restoreAllMocks(); + }); + + it("uses last user/assistant message timestamp instead of file mtime", async () => { + const filePath = join(tmpdir(), `pi-session-${Date.now()}-modified.jsonl`); + createSessionFile(filePath); + + const before = await stat(filePath); + // Ensure the file mtime can differ from our message timestamp even on coarse filesystems. + await new Promise((r) => setTimeout(r, 10)); + + const mgr = SessionManager.open(filePath); + const msgTime = Date.now(); + mgr.appendMessage({ + role: "assistant", + content: [{ type: "text", text: "later" }], + api: "openai-completions", + provider: "openai", + model: "test", + usage: { + input: 1, + output: 1, + cacheRead: 0, + cacheWrite: 0, + totalTokens: 2, + cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 }, + }, + stopReason: "stop", + timestamp: msgTime, + }); + + const sessions = await SessionManager.list("/tmp", dirname(filePath)); + const s = sessions.find((x) => x.path === filePath); + expect(s).toBeDefined(); + expect(s!.modified.getTime()).toBe(msgTime); + expect(s!.modified.getTime()).not.toBe(before.mtime.getTime()); + }); +}); diff --git a/packages/coding-agent/test/session-selector-path-delete.test.ts b/packages/coding-agent/test/session-selector-path-delete.test.ts new file mode 100644 index 0000000000000000000000000000000000000000..cb702d5e2e9bdf5d67027848866c1a90ccc9bab2 --- /dev/null +++ b/packages/coding-agent/test/session-selector-path-delete.test.ts @@ -0,0 +1,354 @@ +import { mkdirSync, mkdtempSync, rmSync, symlinkSync, writeFileSync } from "node:fs"; +import { tmpdir } from "node:os"; +import { join } from "node:path"; +import { setKeybindings } from "@earendil-works/pi-tui"; +import { afterEach, beforeAll, beforeEach, describe, expect, it } from "vitest"; +import { KeybindingsManager } from "../src/core/keybindings.ts"; +import type { SessionInfo } from "../src/core/session-manager.ts"; +import { SessionSelectorComponent } from "../src/modes/interactive/components/session-selector.ts"; +import { initTheme } from "../src/modes/interactive/theme/theme.ts"; + +type Deferred = { + promise: Promise; + resolve: (value: T) => void; + reject: (err: unknown) => void; +}; + +function createDeferred(): Deferred { + let resolve: (value: T) => void = () => {}; + let reject: (err: unknown) => void = () => {}; + const promise = new Promise((res, rej) => { + resolve = res; + reject = rej; + }); + return { promise, resolve, reject }; +} + +async function flushPromises(): Promise { + await new Promise((resolve) => { + setImmediate(resolve); + }); +} + +function stripAnsi(text: string): string { + return text.replace(/\x1B\[[0-?]*[ -/]*[@-~]/g, ""); +} + +function makeSession(overrides: Partial & { id: string }): SessionInfo { + return { + path: overrides.path ?? `/tmp/${overrides.id}.jsonl`, + id: overrides.id, + cwd: overrides.cwd ?? "", + name: overrides.name, + parentSessionPath: overrides.parentSessionPath, + created: overrides.created ?? new Date(0), + modified: overrides.modified ?? new Date(0), + messageCount: overrides.messageCount ?? 1, + firstMessage: overrides.firstMessage ?? "hello", + allMessagesText: overrides.allMessagesText ?? "hello", + }; +} + +function createSymlinkedSessionPaths(): { + baseDir: string; + parentAliasA: string; + parentAliasB: string; + childAliasB: string; +} { + const baseDir = mkdtempSync(join(tmpdir(), "pi-session-selector-")); + const realDir = join(baseDir, "real"); + const aliasADir = join(baseDir, "alias-a"); + const aliasBDir = join(baseDir, "alias-b"); + mkdirSync(realDir, { recursive: true }); + mkdirSync(aliasADir, { recursive: true }); + mkdirSync(aliasBDir, { recursive: true }); + + const sharedDir = join(realDir, "sessions"); + mkdirSync(sharedDir, { recursive: true }); + const aliasASessions = join(aliasADir, "sessions"); + const aliasBSessions = join(aliasBDir, "sessions"); + symlinkSync(sharedDir, aliasASessions); + symlinkSync(sharedDir, aliasBSessions); + + const parentRealPath = join(sharedDir, "parent.jsonl"); + const childRealPath = join(sharedDir, "child.jsonl"); + writeFileSync(parentRealPath, "parent\n"); + writeFileSync(childRealPath, "child\n"); + + return { + baseDir, + parentAliasA: join(aliasASessions, "parent.jsonl"), + parentAliasB: join(aliasBSessions, "parent.jsonl"), + childAliasB: join(aliasBSessions, "child.jsonl"), + }; +} + +const CTRL_D = "\x04"; +const CTRL_BACKSPACE = "\x1b[127;5u"; + +describe("session selector path/delete interactions", () => { + const keybindings = new KeybindingsManager(); + const tempDirs: string[] = []; + + afterEach(() => { + for (const dir of tempDirs.splice(0)) { + rmSync(dir, { recursive: true, force: true }); + } + }); + + beforeEach(() => { + // Ensure test isolation: keybindings are a global singleton + setKeybindings(new KeybindingsManager()); + }); + + beforeAll(() => { + // session selector uses the global theme instance + initTheme("dark"); + }); + it("does not treat Ctrl+Backspace as delete when search query is non-empty", async () => { + const sessions = [makeSession({ id: "a" }), makeSession({ id: "b" })]; + + const selector = new SessionSelectorComponent( + async () => sessions, + async () => [], + () => {}, + () => {}, + () => {}, + () => {}, + { keybindings }, + ); + await flushPromises(); + + const list = selector.getSessionList(); + const confirmationChanges: Array = []; + list.onDeleteConfirmationChange = (path) => confirmationChanges.push(path); + + list.handleInput("a"); + list.handleInput(CTRL_BACKSPACE); + + expect(confirmationChanges).toEqual([]); + }); + + it("enters confirmation mode on Ctrl+D even with a non-empty search query", async () => { + const sessions = [makeSession({ id: "a" }), makeSession({ id: "b" })]; + + const selector = new SessionSelectorComponent( + async () => sessions, + async () => [], + () => {}, + () => {}, + () => {}, + () => {}, + { keybindings }, + ); + await flushPromises(); + + const list = selector.getSessionList(); + const confirmationChanges: Array = []; + list.onDeleteConfirmationChange = (path) => confirmationChanges.push(path); + + list.handleInput("a"); + list.handleInput(CTRL_D); + + expect(confirmationChanges).toEqual([sessions[0]!.path]); + }); + + it("enters confirmation mode on Ctrl+Backspace when search query is empty", async () => { + const sessions = [makeSession({ id: "a" }), makeSession({ id: "b" })]; + + const selector = new SessionSelectorComponent( + async () => sessions, + async () => [], + () => {}, + () => {}, + () => {}, + () => {}, + { keybindings }, + ); + await flushPromises(); + + const list = selector.getSessionList(); + const confirmationChanges: Array = []; + list.onDeleteConfirmationChange = (path) => confirmationChanges.push(path); + + let deletedPath: string | null = null; + list.onDeleteSession = async (sessionPath) => { + deletedPath = sessionPath; + }; + + list.handleInput(CTRL_BACKSPACE); + expect(confirmationChanges).toEqual([sessions[0]!.path]); + + list.handleInput("\r"); + expect(confirmationChanges).toEqual([sessions[0]!.path, null]); + expect(deletedPath).toBe(sessions[0]!.path); + }); + + it("does not switch scope back to All when All load resolves after toggling back to Current", async () => { + const currentSessions = [makeSession({ id: "current" })]; + const allDeferred = createDeferred(); + let allLoadCalls = 0; + + const selector = new SessionSelectorComponent( + async () => currentSessions, + async () => { + allLoadCalls++; + return allDeferred.promise; + }, + () => {}, + () => {}, + () => {}, + () => {}, + { keybindings }, + ); + await flushPromises(); + + const list = selector.getSessionList(); + list.handleInput("\t"); // current -> all (starts async load) + list.handleInput("\t"); // all -> current + + allDeferred.resolve([makeSession({ id: "all" })]); + await flushPromises(); + + expect(allLoadCalls).toBe(1); + const output = selector.render(120).join("\n"); + expect(output).toContain("Resume Session (Current Folder)"); + expect(output).not.toContain("Resume Session (All)"); + }); + + it("does not start redundant All loads when toggling scopes while All is already loading", async () => { + const currentSessions = [makeSession({ id: "current" })]; + const allDeferred = createDeferred(); + let allLoadCalls = 0; + + const selector = new SessionSelectorComponent( + async () => currentSessions, + async () => { + allLoadCalls++; + return allDeferred.promise; + }, + () => {}, + () => {}, + () => {}, + () => {}, + { keybindings }, + ); + await flushPromises(); + + const list = selector.getSessionList(); + list.handleInput("\t"); // current -> all (starts async load) + list.handleInput("\t"); // all -> current + list.handleInput("\t"); // current -> all again while load pending + + expect(allLoadCalls).toBe(1); + + allDeferred.resolve([makeSession({ id: "all" })]); + await flushPromises(); + }); + + it("threads sessions when parent and child paths use different symlink aliases", async () => { + const paths = createSymlinkedSessionPaths(); + tempDirs.push(paths.baseDir); + + const sessions = [ + makeSession({ + id: "parent", + path: paths.parentAliasB, + name: "Parent", + modified: new Date("2026-01-01T00:00:00.000Z"), + }), + makeSession({ + id: "child", + path: paths.childAliasB, + parentSessionPath: paths.parentAliasA, + name: "Child", + modified: new Date("2025-12-31T00:00:00.000Z"), + }), + ]; + + const selector = new SessionSelectorComponent( + async () => sessions, + async () => [], + () => {}, + () => {}, + () => {}, + () => {}, + { keybindings }, + ); + await flushPromises(); + + const output = stripAnsi(selector.render(120).join("\n")); + expect(output).toContain("Parent"); + expect(output).toContain("└─ Child"); + }); + + it("sorts threaded sessions by latest activity in their subtree", async () => { + const parentOne = makeSession({ + id: "parent-one", + name: "Parent one", + modified: new Date("2026-01-02T00:00:00.000Z"), + }); + const parentTwo = makeSession({ + id: "parent-two", + name: "Parent two", + modified: new Date("2026-01-01T00:00:00.000Z"), + }); + const childTwo = makeSession({ + id: "child-two", + name: "Child two", + parentSessionPath: parentTwo.path, + modified: new Date("2026-01-03T00:00:00.000Z"), + }); + + const selector = new SessionSelectorComponent( + async () => [parentOne, parentTwo, childTwo], + async () => [], + () => {}, + () => {}, + () => {}, + () => {}, + { keybindings }, + ); + await flushPromises(); + + const output = stripAnsi(selector.render(120).join("\n")); + const parentTwoIndex = output.indexOf("Parent two"); + const childTwoIndex = output.indexOf("└─ Child two"); + const parentOneIndex = output.indexOf("Parent one"); + + expect(parentTwoIndex).toBeGreaterThanOrEqual(0); + expect(childTwoIndex).toBeGreaterThan(parentTwoIndex); + expect(parentOneIndex).toBeGreaterThan(childTwoIndex); + }); + + it("treats the current session as active across symlink aliases", async () => { + const paths = createSymlinkedSessionPaths(); + tempDirs.push(paths.baseDir); + + const sessions = [makeSession({ id: "parent", path: paths.parentAliasB, name: "Parent" })]; + const selector = new SessionSelectorComponent( + async () => sessions, + async () => [], + () => {}, + () => {}, + () => {}, + () => {}, + { keybindings }, + paths.parentAliasA, + ); + await flushPromises(); + + const list = selector.getSessionList(); + const confirmationChanges: Array = []; + let errorMessage: string | undefined; + list.onDeleteConfirmationChange = (path) => confirmationChanges.push(path); + list.onError = (message) => { + errorMessage = message; + }; + + list.handleInput(CTRL_D); + + expect(confirmationChanges).toEqual([]); + expect(errorMessage).toBe("Cannot delete the currently active session"); + }); +}); diff --git a/packages/coding-agent/test/session-selector-rename.test.ts b/packages/coding-agent/test/session-selector-rename.test.ts new file mode 100644 index 0000000000000000000000000000000000000000..d535496c83a6360bc58377ec66a61b4a2612dd63 --- /dev/null +++ b/packages/coding-agent/test/session-selector-rename.test.ts @@ -0,0 +1,111 @@ +import { setKeybindings } from "@earendil-works/pi-tui"; +import { beforeAll, beforeEach, describe, expect, it, vi } from "vitest"; +import { KeybindingsManager } from "../src/core/keybindings.ts"; +import type { SessionInfo } from "../src/core/session-manager.ts"; +import { SessionSelectorComponent } from "../src/modes/interactive/components/session-selector.ts"; +import { initTheme } from "../src/modes/interactive/theme/theme.ts"; + +async function flushPromises(): Promise { + await new Promise((resolve) => { + setImmediate(resolve); + }); +} + +function makeSession(overrides: Partial & { id: string }): SessionInfo { + return { + path: overrides.path ?? `/tmp/${overrides.id}.jsonl`, + id: overrides.id, + cwd: overrides.cwd ?? "", + name: overrides.name, + created: overrides.created ?? new Date(0), + modified: overrides.modified ?? new Date(0), + messageCount: overrides.messageCount ?? 1, + firstMessage: overrides.firstMessage ?? "hello", + allMessagesText: overrides.allMessagesText ?? "hello", + }; +} + +// Kitty keyboard protocol encoding for Ctrl+R +const CTRL_R = "\x1b[114;5u"; + +describe("session selector rename", () => { + beforeAll(() => { + initTheme("dark"); + }); + + beforeEach(() => { + // Ensure test isolation: keybindings are a global singleton + setKeybindings(new KeybindingsManager()); + }); + + it("shows rename hint in interactive /resume picker configuration", async () => { + const sessions = [makeSession({ id: "a" })]; + const keybindings = new KeybindingsManager(); + const selector = new SessionSelectorComponent( + async () => sessions, + async () => [], + () => {}, + () => {}, + () => {}, + () => {}, + { showRenameHint: true, keybindings }, + ); + await flushPromises(); + + const output = selector.render(120).join("\n"); + expect(output).toContain("ctrl+r"); + expect(output).toContain("rename"); + }); + + it("does not show rename hint in --resume picker configuration", async () => { + const sessions = [makeSession({ id: "a" })]; + const keybindings = new KeybindingsManager(); + const selector = new SessionSelectorComponent( + async () => sessions, + async () => [], + () => {}, + () => {}, + () => {}, + () => {}, + { showRenameHint: false, keybindings }, + ); + await flushPromises(); + + const output = selector.render(120).join("\n"); + expect(output).not.toContain("ctrl+r"); + expect(output).not.toContain("rename"); + }); + + it("enters rename mode on Ctrl+R and submits with Enter", async () => { + const sessions = [makeSession({ id: "a", name: "Old" })]; + const renameSession = vi.fn(async () => {}); + + const keybindings = new KeybindingsManager(); + const selector = new SessionSelectorComponent( + async () => sessions, + async () => [], + () => {}, + () => {}, + () => {}, + () => {}, + { renameSession, showRenameHint: true, keybindings }, + ); + await flushPromises(); + + selector.getSessionList().handleInput(CTRL_R); + await flushPromises(); + + // Rename mode layout + const output = selector.render(120).join("\n"); + expect(output).toContain("Rename Session"); + expect(output).not.toContain("Resume Session"); + + // Type and submit + selector.handleInput("X"); + selector.handleInput("\r"); + await flushPromises(); + + expect(renameSession).toHaveBeenCalledTimes(1); + expect(renameSession).toHaveBeenCalledWith(sessions[0]!.path, "XOld"); + }); +}); diff --git a/packages/coding-agent/test/session-selector-search.test.ts b/packages/coding-agent/test/session-selector-search.test.ts new file mode 100644 index 0000000000000000000000000000000000000000..c1aed71377a66111e6f481dc40ac2e27a47eb0fe --- /dev/null +++ b/packages/coding-agent/test/session-selector-search.test.ts @@ -0,0 +1,195 @@ +import { describe, expect, it } from "vitest"; +import type { SessionInfo } from "../src/core/session-manager.ts"; +import { filterAndSortSessions } from "../src/modes/interactive/components/session-selector-search.ts"; + +function makeSession( + overrides: Partial & { id: string; modified: Date; allMessagesText: string }, +): SessionInfo { + return { + path: `/tmp/${overrides.id}.jsonl`, + id: overrides.id, + cwd: overrides.cwd ?? "", + name: overrides.name, + created: overrides.created ?? new Date(0), + modified: overrides.modified, + messageCount: overrides.messageCount ?? 1, + firstMessage: overrides.firstMessage ?? "(no messages)", + allMessagesText: overrides.allMessagesText, + }; +} + +describe("session selector search", () => { + it("filters by quoted phrase with whitespace normalization", () => { + const sessions: SessionInfo[] = [ + makeSession({ + id: "a", + modified: new Date("2026-01-01T00:00:00.000Z"), + allMessagesText: "node\n\n cve was discussed", + }), + makeSession({ + id: "b", + modified: new Date("2026-01-02T00:00:00.000Z"), + allMessagesText: "node something else", + }), + ]; + + const result = filterAndSortSessions(sessions, '"node cve"', "recent"); + expect(result.map((s) => s.id)).toEqual(["a"]); + }); + + it("filters by regex (re:) and is case-insensitive", () => { + const sessions: SessionInfo[] = [ + makeSession({ + id: "a", + modified: new Date("2026-01-02T00:00:00.000Z"), + allMessagesText: "Brave is great", + }), + makeSession({ + id: "b", + modified: new Date("2026-01-03T00:00:00.000Z"), + allMessagesText: "bravery is not the same", + }), + ]; + + const result = filterAndSortSessions(sessions, "re:\\bbrave\\b", "recent"); + expect(result.map((s) => s.id)).toEqual(["a"]); + }); + + it("recent sort preserves input order", () => { + const sessions: SessionInfo[] = [ + makeSession({ + id: "newer", + modified: new Date("2026-01-03T00:00:00.000Z"), + allMessagesText: "brave", + }), + makeSession({ + id: "older", + modified: new Date("2026-01-01T00:00:00.000Z"), + allMessagesText: "brave", + }), + makeSession({ + id: "nomatch", + modified: new Date("2026-01-04T00:00:00.000Z"), + allMessagesText: "something else", + }), + ]; + + const result = filterAndSortSessions(sessions, '"brave"', "recent"); + expect(result.map((s) => s.id)).toEqual(["newer", "older"]); + }); + + it("relevance sort orders by score and tie-breaks by modified desc", () => { + const sessions: SessionInfo[] = [ + makeSession({ + id: "late", + modified: new Date("2026-01-03T00:00:00.000Z"), + allMessagesText: "xxxx brave", + }), + makeSession({ + id: "early", + modified: new Date("2026-01-01T00:00:00.000Z"), + allMessagesText: "brave xxxx", + }), + ]; + + const result1 = filterAndSortSessions(sessions, '"brave"', "relevance"); + expect(result1.map((s) => s.id)).toEqual(["early", "late"]); + + const tieSessions: SessionInfo[] = [ + makeSession({ + id: "newer", + modified: new Date("2026-01-03T00:00:00.000Z"), + allMessagesText: "brave", + }), + makeSession({ + id: "older", + modified: new Date("2026-01-01T00:00:00.000Z"), + allMessagesText: "brave", + }), + ]; + + const result2 = filterAndSortSessions(tieSessions, '"brave"', "relevance"); + expect(result2.map((s) => s.id)).toEqual(["newer", "older"]); + }); + + it("returns empty list for invalid regex", () => { + const sessions: SessionInfo[] = [ + makeSession({ + id: "a", + modified: new Date("2026-01-01T00:00:00.000Z"), + allMessagesText: "brave", + }), + ]; + + const result = filterAndSortSessions(sessions, "re:(", "recent"); + expect(result).toEqual([]); + }); + + describe("name filter", () => { + const sessions: SessionInfo[] = [ + makeSession({ + id: "named1", + name: "My Project", + modified: new Date("2026-01-03T00:00:00.000Z"), + allMessagesText: "blueberry", + }), + makeSession({ + id: "named2", + name: "Another Named", + modified: new Date("2026-01-02T00:00:00.000Z"), + allMessagesText: "blueberry", + }), + makeSession({ + id: "other1", + modified: new Date("2026-01-04T00:00:00.000Z"), + allMessagesText: "blueberry", + }), + makeSession({ + id: "other2", + modified: new Date("2026-01-01T00:00:00.000Z"), + allMessagesText: "blueberry", + }), + ]; + + it("returns all sessions when nameFilter is 'all'", () => { + const result = filterAndSortSessions(sessions, "", "recent", "all"); + expect(result.map((session) => session.id)).toEqual(["named1", "named2", "other1", "other2"]); + }); + + it("returns only named sessions when nameFilter is 'named'", () => { + const result = filterAndSortSessions(sessions, "", "recent", "named"); + expect(result.map((session) => session.id)).toEqual(["named1", "named2"]); + }); + + it("applies name filter before search query", () => { + const result = filterAndSortSessions(sessions, "blueberry", "recent", "named"); + expect(result.map((session) => session.id)).toEqual(["named1", "named2"]); + }); + + it("excludes whitespace-only names from named filter", () => { + const sessionsWithWhitespace: SessionInfo[] = [ + makeSession({ + id: "whitespace", + name: " ", + modified: new Date("2026-01-01T00:00:00.000Z"), + allMessagesText: "test", + }), + makeSession({ + id: "empty", + name: "", + modified: new Date("2026-01-02T00:00:00.000Z"), + allMessagesText: "test", + }), + makeSession({ + id: "named", + name: "Real Name", + modified: new Date("2026-01-03T00:00:00.000Z"), + allMessagesText: "test", + }), + ]; + + const result = filterAndSortSessions(sessionsWithWhitespace, "", "recent", "named"); + expect(result.map((session) => session.id)).toEqual(["named"]); + }); + }); +}); diff --git a/packages/coding-agent/test/session-share.test.ts b/packages/coding-agent/test/session-share.test.ts new file mode 100644 index 0000000000000000000000000000000000000000..3c1bf52d96e6b325c898b0122eda67895c120ed9 --- /dev/null +++ b/packages/coding-agent/test/session-share.test.ts @@ -0,0 +1,88 @@ +import { EventEmitter } from "node:events"; +import { readFileSync, writeFileSync } from "node:fs"; +import { PassThrough } from "node:stream"; +import { beforeAll, describe, expect, it, vi } from "vitest"; + +const childProcessMocks = vi.hoisted(() => ({ + spawn: vi.fn(), + spawnSync: vi.fn(() => ({ status: 0 })), +})); + +vi.mock("node:child_process", () => childProcessMocks); + +import { shareSession } from "../src/modes/interactive/session-share.ts"; +import { initTheme } from "../src/modes/interactive/theme/theme.ts"; + +function deferred(): { promise: Promise; resolve: () => void } { + let resolve!: () => void; + const promise = new Promise((done) => { + resolve = done; + }); + return { promise, resolve }; +} + +describe("shareSession", () => { + beforeAll(() => initTheme("dark")); + + it("keeps concurrent session exports isolated", async () => { + const uploads: string[] = []; + childProcessMocks.spawn.mockImplementation((_command, args: string[]) => { + uploads.push(readFileSync(args.at(-1)!, "utf8")); + const child = Object.assign(new EventEmitter(), { + stdout: new PassThrough(), + stderr: new PassThrough(), + kill: vi.fn(), + }); + queueMicrotask(() => { + child.stdout.end(`https://gist.github.com/test/${uploads.length}\n`); + child.stderr.end(); + child.emit("close", 0); + }); + return child; + }); + + const aWritten = deferred(); + const bWritten = deferred(); + const releaseB = deferred(); + const errors: string[] = []; + const context = (name: "A" | "B") => ({ + session: { + sessionManager: { + getSessionId: () => name, + getCwd: () => "/tmp", + getBranch: () => [], + }, + state: { systemPrompt: name, tools: [] }, + modelRuntime: { getProvider: () => undefined }, + exportToHtml: async (filePath: string) => { + writeFileSync(filePath, name); + if (name === "A") { + aWritten.resolve(); + await bWritten.promise; + } else { + bWritten.resolve(); + await releaseB.promise; + } + }, + }, + ui: { setFocus() {}, requestRender() {} }, + editorContainer: { clear() {}, addChild() {} }, + editor: {}, + showStatus() {}, + showError(message: string) { + errors.push(message); + }, + }); + + const shareA = shareSession(context("A") as never); + await aWritten.promise; + const shareB = shareSession(context("B") as never); + await bWritten.promise; + await shareA; + releaseB.resolve(); + await shareB; + + expect(uploads).toEqual(["A", "B"]); + expect(errors).toEqual([]); + }); +}); diff --git a/packages/coding-agent/test/settings-diagnostics.test.ts b/packages/coding-agent/test/settings-diagnostics.test.ts new file mode 100644 index 0000000000000000000000000000000000000000..9bda0fd6a4cb4508d9be2cbcd944bde803f97d78 --- /dev/null +++ b/packages/coding-agent/test/settings-diagnostics.test.ts @@ -0,0 +1,47 @@ +import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from "node:fs"; +import { tmpdir } from "node:os"; +import { join } from "node:path"; +import { describe, expect, it } from "vitest"; +import { collectSettingsDiagnostics, deduplicateDiagnostics } from "../src/core/settings-diagnostics.ts"; +import { SettingsManager, type SettingsStorage } from "../src/core/settings-manager.ts"; + +describe("settings diagnostics", () => { + it("includes the settings file path for file-backed storage", () => { + const tempDir = mkdtempSync(join(tmpdir(), "pi-settings-diagnostics-")); + const agentDir = join(tempDir, "agent"); + const settingsPath = join(agentDir, "settings.json"); + mkdirSync(agentDir); + writeFileSync(settingsPath, "{"); + + try { + const diagnostics = collectSettingsDiagnostics(SettingsManager.create(tempDir, agentDir)); + + expect(diagnostics).toHaveLength(1); + expect(diagnostics[0]?.type).toBe("warning"); + expect(diagnostics[0]?.message).toContain(`Invalid settings file ${settingsPath}:`); + } finally { + rmSync(tempDir, { recursive: true, force: true }); + } + }); + + it("falls back to the settings scope for storage without file paths", () => { + const storage: SettingsStorage = { + withLock(scope, fn) { + if (scope === "global") throw new Error("backend failed"); + fn(undefined); + }, + }; + const diagnostics = collectSettingsDiagnostics(SettingsManager.fromStorage(storage)); + + expect(diagnostics).toEqual([{ type: "warning", message: "Invalid global settings: backend failed" }]); + }); + + it("deduplicates diagnostics by type and message", () => { + const warning = { type: "warning" as const, message: "Invalid settings file /tmp/settings.json" }; + + expect(deduplicateDiagnostics([warning, warning, { ...warning, type: "error" }])).toEqual([ + warning, + { ...warning, type: "error" }, + ]); + }); +}); diff --git a/packages/coding-agent/test/settings-manager-bug.test.ts b/packages/coding-agent/test/settings-manager-bug.test.ts new file mode 100644 index 0000000000000000000000000000000000000000..92cc3a821aa7fa144e346aa0e79d73abe8a4e647 --- /dev/null +++ b/packages/coding-agent/test/settings-manager-bug.test.ts @@ -0,0 +1,147 @@ +import { existsSync, mkdirSync, readFileSync, rmSync, writeFileSync } from "fs"; +import { join } from "path"; +import { afterEach, beforeEach, describe, expect, it } from "vitest"; +import { SettingsManager } from "../src/core/settings-manager.ts"; + +/** + * Tests for the fix to a bug where external file changes to arrays were overwritten. + * + * The bug scenario was: + * 1. Pi starts with settings.json containing packages: ["npm:some-pkg"] + * 2. User externally edits file to packages: [] + * 3. User changes an unrelated setting (e.g., theme) via UI + * 4. save() would overwrite packages back to ["npm:some-pkg"] from stale in-memory state + * + * The fix tracks which fields were explicitly modified during the session, and only + * those fields override file values during save(). + */ +describe("SettingsManager - External Edit Preservation", () => { + const testDir = join(process.cwd(), "test-settings-bug-tmp"); + const agentDir = join(testDir, "agent"); + const projectDir = join(testDir, "project"); + + beforeEach(() => { + if (existsSync(testDir)) { + rmSync(testDir, { recursive: true }); + } + mkdirSync(agentDir, { recursive: true }); + mkdirSync(join(projectDir, ".pi"), { recursive: true }); + }); + + afterEach(() => { + if (existsSync(testDir)) { + rmSync(testDir, { recursive: true }); + } + }); + + it("should preserve file changes to packages array when changing unrelated setting", async () => { + const settingsPath = join(agentDir, "settings.json"); + + // Initial state: packages has one item + writeFileSync( + settingsPath, + JSON.stringify({ + theme: "dark", + packages: ["npm:pi-mcp-adapter"], + }), + ); + + // Pi starts up, loads settings into memory + const manager = SettingsManager.create(projectDir, agentDir); + + // At this point, globalSettings.packages = ["npm:pi-mcp-adapter"] + expect(manager.getPackages()).toEqual(["npm:pi-mcp-adapter"]); + + // User externally edits settings.json to remove the package + const currentSettings = JSON.parse(readFileSync(settingsPath, "utf-8")); + currentSettings.packages = []; // User wants to remove this! + writeFileSync(settingsPath, JSON.stringify(currentSettings, null, 2)); + + // Verify file was changed + expect(JSON.parse(readFileSync(settingsPath, "utf-8")).packages).toEqual([]); + + // User changes an UNRELATED setting via UI (this triggers save) + manager.setTheme("light"); + await manager.flush(); + + // With the fix, packages should be preserved as [] (not reverted to startup value) + const savedSettings = JSON.parse(readFileSync(settingsPath, "utf-8")); + + expect(savedSettings.packages).toEqual([]); + expect(savedSettings.theme).toBe("light"); + }); + + it("should preserve file changes to extensions array when changing unrelated setting", async () => { + const settingsPath = join(agentDir, "settings.json"); + + writeFileSync( + settingsPath, + JSON.stringify({ + theme: "dark", + extensions: ["/old/extension.ts"], + }), + ); + + const manager = SettingsManager.create(projectDir, agentDir); + + // User externally updates extensions + const currentSettings = JSON.parse(readFileSync(settingsPath, "utf-8")); + currentSettings.extensions = ["/new/extension.ts"]; + writeFileSync(settingsPath, JSON.stringify(currentSettings, null, 2)); + + // Change unrelated setting + manager.setDefaultThinkingLevel("high"); + await manager.flush(); + + const savedSettings = JSON.parse(readFileSync(settingsPath, "utf-8")); + + // With the fix, extensions should be preserved (not reverted to startup value) + expect(savedSettings.extensions).toEqual(["/new/extension.ts"]); + }); + + it("should preserve external project settings changes when updating unrelated project field", async () => { + const projectSettingsPath = join(projectDir, ".pi", "settings.json"); + writeFileSync( + projectSettingsPath, + JSON.stringify({ + extensions: ["./old-extension.ts"], + prompts: ["./old-prompt.md"], + }), + ); + + const manager = SettingsManager.create(projectDir, agentDir); + + const currentProjectSettings = JSON.parse(readFileSync(projectSettingsPath, "utf-8")); + currentProjectSettings.prompts = ["./new-prompt.md"]; + writeFileSync(projectSettingsPath, JSON.stringify(currentProjectSettings, null, 2)); + + manager.setProjectExtensionPaths(["./updated-extension.ts"]); + await manager.flush(); + + const savedProjectSettings = JSON.parse(readFileSync(projectSettingsPath, "utf-8")); + expect(savedProjectSettings.prompts).toEqual(["./new-prompt.md"]); + expect(savedProjectSettings.extensions).toEqual(["./updated-extension.ts"]); + }); + + it("should let in-memory project changes override external changes for the same project field", async () => { + const projectSettingsPath = join(projectDir, ".pi", "settings.json"); + writeFileSync( + projectSettingsPath, + JSON.stringify({ + extensions: ["./initial-extension.ts"], + }), + ); + + const manager = SettingsManager.create(projectDir, agentDir); + + const currentProjectSettings = JSON.parse(readFileSync(projectSettingsPath, "utf-8")); + currentProjectSettings.extensions = ["./external-extension.ts"]; + writeFileSync(projectSettingsPath, JSON.stringify(currentProjectSettings, null, 2)); + + manager.setProjectExtensionPaths(["./in-memory-extension.ts"]); + await manager.flush(); + + const savedProjectSettings = JSON.parse(readFileSync(projectSettingsPath, "utf-8")); + expect(savedProjectSettings.extensions).toEqual(["./in-memory-extension.ts"]); + }); +}); diff --git a/packages/coding-agent/test/settings-manager-compaction.test.ts b/packages/coding-agent/test/settings-manager-compaction.test.ts new file mode 100644 index 0000000000000000000000000000000000000000..d0cf3b2399378433168a0208a06bdba50195755f --- /dev/null +++ b/packages/coding-agent/test/settings-manager-compaction.test.ts @@ -0,0 +1,192 @@ +import { describe, expect, it } from "vitest"; +import { InMemorySettingsStorage, SettingsManager } from "../src/core/settings-manager.ts"; + +const model = { provider: "provider", id: "family/model" }; +const modelKey = "provider/family/model"; +const defaults = { enabled: true, reserveTokens: 16384, keepRecentTokens: 20000 }; + +// Regression coverage for #8133. +describe("compaction model overrides", () => { + it("uses defaults without compaction settings", () => { + const manager = SettingsManager.inMemory(); + expect(manager.getCompactionSettings()).toEqual(defaults); + expect(manager.getCompactionSettings(model)).toEqual(defaults); + }); + + it("resolves each field independently and keeps individual getters consistent", () => { + const manager = SettingsManager.inMemory({ + compaction: { + reserveTokens: 8192, + keepRecentTokens: 10000, + modelOverrides: { [modelKey]: { reserveTokens: 400000 } }, + }, + }); + expect(manager.getCompactionSettings(model)).toEqual({ + enabled: true, + reserveTokens: 400000, + keepRecentTokens: 10000, + }); + expect(manager.getCompactionReserveTokens(model)).toBe(400000); + expect(manager.getCompactionKeepRecentTokens(model)).toBe(10000); + expect(manager.getCompactionSettings()).toEqual({ enabled: true, reserveTokens: 8192, keepRecentTokens: 10000 }); + + manager.applyOverrides({ compaction: { modelOverrides: { [modelKey]: { keepRecentTokens: 30000 } } } }); + expect(manager.getCompactionKeepRecentTokens(model)).toBe(30000); + expect(manager.getCompactionReserveTokens(model)).toBe(400000); + }); + + it("falls back to built-in defaults for missing fields", () => { + const manager = SettingsManager.inMemory({ + compaction: { modelOverrides: { [modelKey]: { keepRecentTokens: 1024 } } }, + }); + expect(manager.getCompactionSettings(model)).toEqual({ ...defaults, keepRecentTokens: 1024 }); + }); + + it("matches exact provider/model IDs, including IDs containing slashes", () => { + const manager = SettingsManager.inMemory({ + compaction: { + modelOverrides: { + [modelKey]: { reserveTokens: 400000 }, + "provider/*": { reserveTokens: 1 }, + "family/model": { reserveTokens: 2 }, + }, + }, + }); + expect(manager.getCompactionReserveTokens(model)).toBe(400000); + for (const other of [ + { provider: "other", id: model.id }, + { provider: model.provider, id: "other" }, + { provider: model.provider, id: "family/Model" }, + ]) { + expect(manager.getCompactionSettings(other)).toEqual(defaults); + } + }); + + it("merges project model overrides per field before resolving fallbacks", async () => { + const storage = new InMemorySettingsStorage(); + storage.withLock("global", () => + JSON.stringify({ + compaction: { + reserveTokens: 8192, + modelOverrides: { + [modelKey]: { reserveTokens: 400000, keepRecentTokens: 30000 }, + "provider/other": { keepRecentTokens: 4096 }, + }, + }, + }), + ); + storage.withLock("project", () => + JSON.stringify({ + compaction: { reserveTokens: 1024, modelOverrides: { [modelKey]: { keepRecentTokens: 2000 } } }, + }), + ); + const manager = SettingsManager.fromStorage(storage); + expect(manager.getCompactionSettings(model)).toEqual({ + enabled: true, + reserveTokens: 400000, + keepRecentTokens: 2000, + }); + expect(manager.getCompactionSettings({ provider: "provider", id: "other" })).toEqual({ + enabled: true, + reserveTokens: 1024, + keepRecentTokens: 4096, + }); + await manager.reload(); + expect(manager.getCompactionKeepRecentTokens(model)).toBe(2000); + manager.setProjectTrusted(false); + expect(manager.getCompactionKeepRecentTokens(model)).toBe(30000); + }); + + it("keeps enabled global and preserves overrides when saving the toggle", async () => { + const storage = new InMemorySettingsStorage(); + storage.withLock("global", () => + JSON.stringify({ + compaction: { modelOverrides: { [modelKey]: { enabled: false, reserveTokens: 400000 } } }, + }), + ); + const manager = SettingsManager.fromStorage(storage); + expect(manager.getCompactionSettings(model).enabled).toBe(true); + manager.setCompactionEnabled(false); + await manager.flush(); + await manager.reload(); + expect(manager.getCompactionSettings(model)).toEqual({ ...defaults, enabled: false, reserveTokens: 400000 }); + }); + + describe.each(["reserveTokens", "keepRecentTokens"] as const)("model override %s", (field) => { + it.each([null, -1, 1.5, "400000", true, {}, [], Number.MAX_SAFE_INTEGER + 1])( + "reports invalid token values: %j", + (value) => { + const storage = new InMemorySettingsStorage(); + storage.withLock("global", () => + JSON.stringify({ + compaction: { modelOverrides: { [modelKey]: { [field]: value } } }, + }), + ); + const manager = SettingsManager.fromStorage(storage); + expect(() => manager.getCompactionSettings(model)).toThrow( + `Invalid compaction.modelOverrides["${modelKey}"].${field} setting: ${String(value)}. Expected a non-negative safe integer.`, + ); + expect(manager.getCompactionSettings()).toEqual(defaults); + expect(manager.getCompactionSettings({ provider: "other", id: model.id })).toEqual(defaults); + }, + ); + + it.each([Number.NaN, Infinity, -Infinity])("reports non-finite runtime values: %s", (value) => { + const manager = SettingsManager.inMemory(); + manager.applyOverrides({ compaction: { modelOverrides: { [modelKey]: { [field]: value } } } }); + expect(() => manager.getCompactionSettings(model)).toThrow( + `Invalid compaction.modelOverrides["${modelKey}"].${field} setting: ${String(value)}`, + ); + }); + }); + + describe.each(["reserveTokens", "keepRecentTokens"] as const)("ordinary compaction.%s", (field) => { + it.each([null, -1, 1.5, "400000", true, {}, [], Number.MAX_SAFE_INTEGER + 1])( + "reports invalid values even when a valid model override exists: %j", + (value) => { + const storage = new InMemorySettingsStorage(); + storage.withLock("global", () => + JSON.stringify({ + compaction: { + [field]: value, + modelOverrides: { [modelKey]: { [field]: 4096 } }, + }, + }), + ); + const manager = SettingsManager.fromStorage(storage); + const error = `Invalid compaction.${field} setting: ${String(value)}. Expected a non-negative safe integer.`; + expect(() => manager.getCompactionSettings()).toThrow(error); + expect(() => manager.getCompactionSettings(model)).toThrow(error); + }, + ); + + it.each([Number.NaN, Infinity, -Infinity])("reports non-finite runtime values: %s", (value) => { + const manager = SettingsManager.inMemory(); + manager.applyOverrides({ compaction: { [field]: value } }); + expect(() => manager.getCompactionSettings()).toThrow(`Invalid compaction.${field} setting: ${String(value)}`); + }); + }); + + it.each([null, false, 42, "invalid", []])("reports malformed model entries: %j", (entry) => { + const storage = new InMemorySettingsStorage(); + storage.withLock("global", () => JSON.stringify({ compaction: { modelOverrides: { [modelKey]: entry } } })); + expect(() => SettingsManager.fromStorage(storage).getCompactionSettings(model)).toThrow( + `Invalid compaction.modelOverrides["${modelKey}"] setting: ${String(entry)}. Expected an object.`, + ); + }); + + it("accepts zero in ordinary settings and model overrides", () => { + const manager = SettingsManager.inMemory({ + compaction: { reserveTokens: 0, keepRecentTokens: 0 }, + }); + expect(manager.getCompactionSettings(model)).toEqual({ enabled: true, reserveTokens: 0, keepRecentTokens: 0 }); + manager.applyOverrides({ + compaction: { + reserveTokens: 1000, + keepRecentTokens: 1000, + modelOverrides: { [modelKey]: { reserveTokens: 0, keepRecentTokens: 0 } }, + }, + }); + expect(manager.getCompactionSettings(model)).toEqual({ enabled: true, reserveTokens: 0, keepRecentTokens: 0 }); + }); +}); diff --git a/packages/coding-agent/test/settings-manager.test.ts b/packages/coding-agent/test/settings-manager.test.ts new file mode 100644 index 0000000000000000000000000000000000000000..87e89627d178bb33fbd61b87688b6cecef06dca5 --- /dev/null +++ b/packages/coding-agent/test/settings-manager.test.ts @@ -0,0 +1,644 @@ +import { existsSync, mkdirSync, readFileSync, rmSync, writeFileSync } from "fs"; +import { homedir } from "os"; +import { join } from "path"; +import { afterEach, beforeEach, describe, expect, it } from "vitest"; +import { DEFAULT_HTTP_IDLE_TIMEOUT_MS } from "../src/core/http-dispatcher.ts"; +import { type Settings, SettingsManager } from "../src/core/settings-manager.ts"; + +describe("SettingsManager", () => { + const testDir = join(process.cwd(), "test-settings-tmp"); + const agentDir = join(testDir, "agent"); + const projectDir = join(testDir, "project"); + + beforeEach(() => { + // Clean up and create fresh directories + if (existsSync(testDir)) { + rmSync(testDir, { recursive: true }); + } + mkdirSync(agentDir, { recursive: true }); + mkdirSync(join(projectDir, ".pi"), { recursive: true }); + }); + + afterEach(() => { + if (existsSync(testDir)) { + rmSync(testDir, { recursive: true }); + } + }); + + describe("preserves externally added settings", () => { + it("should preserve enabledModels when changing thinking level", async () => { + // Create initial settings file + const settingsPath = join(agentDir, "settings.json"); + writeFileSync( + settingsPath, + JSON.stringify({ + theme: "dark", + defaultModel: "claude-sonnet", + }), + ); + + // Create SettingsManager (simulates pi starting up) + const manager = SettingsManager.create(projectDir, agentDir); + + // Simulate user editing settings.json externally to add enabledModels + const currentSettings = JSON.parse(readFileSync(settingsPath, "utf-8")); + currentSettings.enabledModels = ["claude-opus-4-5", "gpt-5.2-codex"]; + writeFileSync(settingsPath, JSON.stringify(currentSettings, null, 2)); + + // User changes thinking level via Shift+Tab + manager.setDefaultThinkingLevel("high"); + await manager.flush(); + + // Verify enabledModels is preserved + const savedSettings = JSON.parse(readFileSync(settingsPath, "utf-8")); + expect(savedSettings.enabledModels).toEqual(["claude-opus-4-5", "gpt-5.2-codex"]); + expect(savedSettings.defaultThinkingLevel).toBe("high"); + expect(savedSettings.theme).toBe("dark"); + expect(savedSettings.defaultModel).toBe("claude-sonnet"); + }); + + it("should preserve custom settings when changing theme", async () => { + const settingsPath = join(agentDir, "settings.json"); + writeFileSync( + settingsPath, + JSON.stringify({ + defaultModel: "claude-sonnet", + }), + ); + + const manager = SettingsManager.create(projectDir, agentDir); + + // User adds custom settings externally + const currentSettings = JSON.parse(readFileSync(settingsPath, "utf-8")); + currentSettings.shellPath = "/bin/zsh"; + currentSettings.extensions = ["/path/to/extension.ts"]; + writeFileSync(settingsPath, JSON.stringify(currentSettings, null, 2)); + + // User changes theme + manager.setTheme("light"); + await manager.flush(); + + // Verify all settings preserved + const savedSettings = JSON.parse(readFileSync(settingsPath, "utf-8")); + expect(savedSettings.shellPath).toBe("/bin/zsh"); + expect(savedSettings.extensions).toEqual(["/path/to/extension.ts"]); + expect(savedSettings.theme).toBe("light"); + }); + + it("should let in-memory changes override file changes for same key", async () => { + const settingsPath = join(agentDir, "settings.json"); + writeFileSync( + settingsPath, + JSON.stringify({ + theme: "dark", + }), + ); + + const manager = SettingsManager.create(projectDir, agentDir); + + // User externally sets thinking level to "low" + const currentSettings = JSON.parse(readFileSync(settingsPath, "utf-8")); + currentSettings.defaultThinkingLevel = "low"; + writeFileSync(settingsPath, JSON.stringify(currentSettings, null, 2)); + + // But then changes it via UI to "high" + manager.setDefaultThinkingLevel("high"); + await manager.flush(); + + // In-memory change should win + const savedSettings = JSON.parse(readFileSync(settingsPath, "utf-8")); + expect(savedSettings.defaultThinkingLevel).toBe("high"); + }); + }); + + describe("packages migration", () => { + it("should keep local-only extensions in extensions array", () => { + const settingsPath = join(agentDir, "settings.json"); + writeFileSync( + settingsPath, + JSON.stringify({ + extensions: ["/local/ext.ts", "./relative/ext.ts"], + }), + ); + + const manager = SettingsManager.create(projectDir, agentDir); + + expect(manager.getPackages()).toEqual([]); + expect(manager.getExtensionPaths()).toEqual(["/local/ext.ts", "./relative/ext.ts"]); + }); + + it("should handle packages with filtering objects", () => { + const settingsPath = join(agentDir, "settings.json"); + writeFileSync( + settingsPath, + JSON.stringify({ + packages: [ + "npm:simple-pkg", + { + source: "npm:shitty-extensions", + extensions: ["extensions/oracle.ts"], + skills: [], + }, + ], + }), + ); + + const manager = SettingsManager.create(projectDir, agentDir); + + const packages = manager.getPackages(); + expect(packages).toHaveLength(2); + expect(packages[0]).toBe("npm:simple-pkg"); + expect(packages[1]).toEqual({ + source: "npm:shitty-extensions", + extensions: ["extensions/oracle.ts"], + skills: [], + }); + }); + }); + + describe("reload", () => { + it("should reload global settings from disk", async () => { + const settingsPath = join(agentDir, "settings.json"); + writeFileSync( + settingsPath, + JSON.stringify({ + theme: "dark", + extensions: ["/before.ts"], + }), + ); + + const manager = SettingsManager.create(projectDir, agentDir); + + writeFileSync( + settingsPath, + JSON.stringify({ + theme: "light", + extensions: ["/after.ts"], + defaultModel: "claude-sonnet", + }), + ); + + await manager.reload(); + + expect(manager.getTheme()).toBe("light"); + expect(manager.getExtensionPaths()).toEqual(["/after.ts"]); + expect(manager.getDefaultModel()).toBe("claude-sonnet"); + }); + + it("should keep previous settings and report the file path when the file is invalid", async () => { + const settingsPath = join(agentDir, "settings.json"); + writeFileSync(settingsPath, JSON.stringify({ theme: "dark" })); + + const manager = SettingsManager.create(projectDir, agentDir); + + writeFileSync(settingsPath, "{ invalid json"); + await manager.reload(); + + expect(manager.getTheme()).toBe("dark"); + expect(manager.drainErrors()).toMatchObject([{ scope: "global", path: settingsPath }]); + }); + }); + + describe("theme setting", () => { + it("stores slash-separated automatic theme settings separately from fixed theme names", async () => { + const settingsPath = join(agentDir, "settings.json"); + writeFileSync(settingsPath, JSON.stringify({ theme: "light/dark" })); + + const manager = SettingsManager.create(projectDir, agentDir); + + expect(manager.getTheme()).toBeUndefined(); + expect(manager.getThemeSetting()).toBe("light/dark"); + + manager.setTheme("solarized-light/tokyo-night"); + await manager.flush(); + + const savedSettings = JSON.parse(readFileSync(settingsPath, "utf-8")); + expect(savedSettings.theme).toBe("solarized-light/tokyo-night"); + }); + }); + + describe("error tracking", () => { + it("should collect and clear load errors via drainErrors", () => { + const globalSettingsPath = join(agentDir, "settings.json"); + const projectSettingsPath = join(projectDir, ".pi", "settings.json"); + writeFileSync(globalSettingsPath, "{ invalid global json"); + writeFileSync(projectSettingsPath, "{ invalid project json"); + + const manager = SettingsManager.create(projectDir, agentDir); + const errors = manager.drainErrors(); + + expect(errors).toHaveLength(2); + expect(errors).toMatchObject([ + { scope: "global", path: globalSettingsPath }, + { scope: "project", path: projectSettingsPath }, + ]); + expect(manager.drainErrors()).toEqual([]); + }); + }); + + describe("project trust", () => { + it("should skip project settings when project is not trusted", () => { + writeFileSync(join(agentDir, "settings.json"), JSON.stringify({ theme: "global" })); + writeFileSync(join(projectDir, ".pi", "settings.json"), JSON.stringify({ theme: "project" })); + + const manager = SettingsManager.create(projectDir, agentDir, { projectTrusted: false }); + + expect(manager.isProjectTrusted()).toBe(false); + expect(manager.getTheme()).toBe("global"); + expect(manager.getProjectSettings()).toEqual({}); + }); + + it("should reload project settings after trust changes to true", () => { + writeFileSync(join(agentDir, "settings.json"), JSON.stringify({ theme: "global" })); + writeFileSync(join(projectDir, ".pi", "settings.json"), JSON.stringify({ theme: "project" })); + const manager = SettingsManager.create(projectDir, agentDir, { projectTrusted: false }); + + manager.setProjectTrusted(true); + + expect(manager.isProjectTrusted()).toBe(true); + expect(manager.getTheme()).toBe("project"); + }); + + it("should fail project settings writes when project is not trusted", async () => { + const projectSettingsPath = join(projectDir, ".pi", "settings.json"); + writeFileSync(projectSettingsPath, JSON.stringify({ packages: ["npm:existing"] })); + const manager = SettingsManager.create(projectDir, agentDir, { projectTrusted: false }); + + expect(() => manager.setProjectPackages(["npm:new"])).toThrow( + "Project is not trusted; refusing to write project settings", + ); + await manager.flush(); + + expect(manager.getProjectSettings()).toEqual({}); + expect(JSON.parse(readFileSync(projectSettingsPath, "utf-8"))).toEqual({ packages: ["npm:existing"] }); + }); + + it("should read default project trust from global settings only", () => { + writeFileSync(join(agentDir, "settings.json"), JSON.stringify({ defaultProjectTrust: "always" })); + writeFileSync(join(projectDir, ".pi", "settings.json"), JSON.stringify({ defaultProjectTrust: "never" })); + + const manager = SettingsManager.create(projectDir, agentDir); + + expect(manager.getDefaultProjectTrust()).toBe("always"); + }); + + it("should default invalid project trust settings to ask", () => { + writeFileSync(join(agentDir, "settings.json"), JSON.stringify({ defaultProjectTrust: "sometimes" })); + + const manager = SettingsManager.create(projectDir, agentDir); + + expect(manager.getDefaultProjectTrust()).toBe("ask"); + }); + }); + + describe("project settings directory creation", () => { + it("should not create .pi folder when only reading project settings", () => { + // Create agent dir with global settings, but NO .pi folder in project + const settingsPath = join(agentDir, "settings.json"); + writeFileSync(settingsPath, JSON.stringify({ theme: "dark" })); + + // Delete the .pi folder that beforeEach created + rmSync(join(projectDir, ".pi"), { recursive: true }); + + // Create SettingsManager (reads both global and project settings) + const manager = SettingsManager.create(projectDir, agentDir); + + // .pi folder should NOT have been created just from reading + expect(existsSync(join(projectDir, ".pi"))).toBe(false); + + // Settings should still be loaded from global + expect(manager.getTheme()).toBe("dark"); + }); + + it("should create .pi folder when writing project settings", async () => { + // Create agent dir with global settings, but NO .pi folder in project + const settingsPath = join(agentDir, "settings.json"); + writeFileSync(settingsPath, JSON.stringify({ theme: "dark" })); + + // Delete the .pi folder that beforeEach created + rmSync(join(projectDir, ".pi"), { recursive: true }); + + const manager = SettingsManager.create(projectDir, agentDir); + + // .pi folder should NOT exist yet + expect(existsSync(join(projectDir, ".pi"))).toBe(false); + + // Write a project-specific setting + manager.setProjectPackages([{ source: "npm:test-pkg" }]); + await manager.flush(); + + // Now .pi folder should exist + expect(existsSync(join(projectDir, ".pi"))).toBe(true); + + // And settings file should be created + expect(existsSync(join(projectDir, ".pi", "settings.json"))).toBe(true); + }); + }); + + describe("terminal capability overrides", () => { + it("maps explicit values and omits auto values", () => { + const getOverrides = (terminal: NonNullable) => + SettingsManager.inMemory({ terminal }).getTerminalCapabilityOverrides(); + + expect(getOverrides({ images: false, trueColor: false, hyperlinks: false })).toEqual({ + images: null, + trueColor: false, + hyperlinks: false, + }); + expect(getOverrides({ images: "kitty", trueColor: true, hyperlinks: true })).toEqual({ + images: "kitty", + trueColor: true, + hyperlinks: true, + }); + expect(getOverrides({ images: "auto", trueColor: "auto", hyperlinks: "auto" })).toEqual({}); + }); + }); + + describe("retry settings", () => { + it("defaults and overrides agent retry delay cap", () => { + expect(SettingsManager.inMemory().getRetrySettings()).toEqual({ + enabled: true, + maxRetries: 3, + baseDelayMs: 2000, + maxAgentDelayMs: 60000, + }); + expect( + SettingsManager.inMemory({ + retry: { enabled: true, maxRetries: 10, baseDelayMs: 500, maxAgentDelayMs: 5000 }, + }).getRetrySettings(), + ).toEqual({ enabled: true, maxRetries: 10, baseDelayMs: 500, maxAgentDelayMs: 5000 }); + }); + }); + + describe("httpIdleTimeoutMs", () => { + it("should default to 5 minutes", () => { + const manager = SettingsManager.create(projectDir, agentDir); + expect(manager.getHttpIdleTimeoutMs()).toBe(DEFAULT_HTTP_IDLE_TIMEOUT_MS); + }); + + it("should use merged global and project settings", () => { + writeFileSync(join(agentDir, "settings.json"), JSON.stringify({ httpIdleTimeoutMs: 300000 })); + writeFileSync(join(projectDir, ".pi", "settings.json"), JSON.stringify({ httpIdleTimeoutMs: 0 })); + + const manager = SettingsManager.create(projectDir, agentDir); + + expect(manager.getHttpIdleTimeoutMs()).toBe(0); + }); + + it("should reject invalid timeout values", () => { + writeFileSync(join(agentDir, "settings.json"), JSON.stringify({ httpIdleTimeoutMs: -1 })); + const manager = SettingsManager.create(projectDir, agentDir); + + expect(() => manager.getHttpIdleTimeoutMs()).toThrow("Invalid httpIdleTimeoutMs setting"); + }); + }); + + describe("externalEditor", () => { + const originalVisual = process.env.VISUAL; + const originalEditor = process.env.EDITOR; + const originalPlatform = Object.getOwnPropertyDescriptor(process, "platform"); + + function setEditorEnv(visual?: string, editor?: string): void { + if (visual === undefined) delete process.env.VISUAL; + else process.env.VISUAL = visual; + if (editor === undefined) delete process.env.EDITOR; + else process.env.EDITOR = editor; + } + + afterEach(() => { + setEditorEnv(originalVisual, originalEditor); + if (originalPlatform) { + Object.defineProperty(process, "platform", originalPlatform); + } + }); + + it("should resolve editor commands by precedence", () => { + setEditorEnv("vim", "nano"); + expect(SettingsManager.inMemory({ externalEditor: "code --wait" }).getExternalEditorCommand()).toBe( + "code --wait", + ); + expect(SettingsManager.inMemory().getExternalEditorCommand()).toBe("vim"); + + setEditorEnv(undefined, "emacs"); + expect(SettingsManager.inMemory().getExternalEditorCommand()).toBe("emacs"); + }); + + it("should fall back to platform defaults", () => { + setEditorEnv(); + Object.defineProperty(process, "platform", { value: "win32" }); + expect(SettingsManager.inMemory().getExternalEditorCommand()).toBe("notepad"); + + Object.defineProperty(process, "platform", { value: "darwin" }); + expect(SettingsManager.inMemory().getExternalEditorCommand()).toBe("nano"); + + Object.defineProperty(process, "platform", { value: "linux" }); + expect(SettingsManager.inMemory().getExternalEditorCommand()).toBe("nano"); + }); + }); + + describe("TUI mode", () => { + it("defaults to regular and persists fullscreen mode", async () => { + const manager = SettingsManager.create(projectDir, agentDir); + + expect(manager.getTuiMode()).toBe("regular"); + + manager.setTuiMode("fullscreen"); + await manager.flush(); + + expect(manager.getTuiMode()).toBe("fullscreen"); + const savedSettings = JSON.parse(readFileSync(join(agentDir, "settings.json"), "utf-8")); + expect(savedSettings.tuiMode).toBe("fullscreen"); + }); + + it("falls back to regular for unsupported values", () => { + writeFileSync(join(agentDir, "settings.json"), JSON.stringify({ tuiMode: "other" })); + + const manager = SettingsManager.create(projectDir, agentDir); + + expect(manager.getTuiMode()).toBe("regular"); + }); + + it("does not recognize the old uiMode setting", () => { + writeFileSync(join(agentDir, "settings.json"), JSON.stringify({ uiMode: "fullscreen" })); + + const manager = SettingsManager.create(projectDir, agentDir); + + expect(manager.getTuiMode()).toBe("regular"); + }); + }); + + it("validates and persists fullscreen settings", async () => { + const manager = SettingsManager.create(projectDir, agentDir); + expect(manager.getFullscreenExitOutput()).toBe("transcript"); + expect(manager.getFullscreenScrollbar()).toBe("auto"); + expect(manager.getFullscreenCopyOnSelect()).toBe(true); + + manager.setFullscreenExitOutput("resume-hint"); + manager.setFullscreenScrollbar("hidden"); + manager.setFullscreenCopyOnSelect(false); + await manager.flush(); + const savedSettings = JSON.parse(readFileSync(join(agentDir, "settings.json"), "utf-8")); + expect(savedSettings.fullscreenExitOutput).toBe("resume-hint"); + expect(savedSettings.fullscreenScrollbar).toBe("hidden"); + expect(savedSettings.fullscreenCopyOnSelect).toBe(false); + + writeFileSync( + join(agentDir, "settings.json"), + JSON.stringify({ fullscreenExitOutput: "nothing", fullscreenScrollbar: "sometimes" }), + ); + const reloadedManager = SettingsManager.create(projectDir, agentDir); + expect(reloadedManager.getFullscreenExitOutput()).toBe("transcript"); + expect(reloadedManager.getFullscreenScrollbar()).toBe("auto"); + expect(reloadedManager.getFullscreenCopyOnSelect()).toBe(true); + }); + + describe("outputPad", () => { + it("should default to 1 and persist binary values", async () => { + const manager = SettingsManager.create(projectDir, agentDir); + + expect(manager.getOutputPad()).toBe(1); + + manager.setOutputPad(0); + await manager.flush(); + + expect(manager.getOutputPad()).toBe(0); + const savedSettings = JSON.parse(readFileSync(join(agentDir, "settings.json"), "utf-8")); + expect(savedSettings.outputPad).toBe(0); + }); + + it("should treat unsupported outputPad values as default padding", () => { + writeFileSync(join(agentDir, "settings.json"), JSON.stringify({ outputPad: 2 })); + + const manager = SettingsManager.create(projectDir, agentDir); + + expect(manager.getOutputPad()).toBe(1); + }); + }); + + describe("markdown.mermaid", () => { + it("defaults to streaming and persists rendering modes", async () => { + const manager = SettingsManager.create(projectDir, agentDir); + + expect(manager.getMermaidRenderingMode()).toBe("streaming"); + + manager.setMermaidRenderingMode("final"); + await manager.flush(); + + expect(manager.getMermaidRenderingMode()).toBe("final"); + const savedSettings = JSON.parse(readFileSync(join(agentDir, "settings.json"), "utf-8")); + expect(savedSettings.markdown.mermaid).toBe("final"); + }); + + it("falls back to streaming for unsupported values", () => { + writeFileSync(join(agentDir, "settings.json"), JSON.stringify({ markdown: { mermaid: "sometimes" } })); + + expect(SettingsManager.create(projectDir, agentDir).getMermaidRenderingMode()).toBe("streaming"); + }); + }); + + describe("shellCommandPrefix", () => { + it("should load shellCommandPrefix from settings", () => { + const settingsPath = join(agentDir, "settings.json"); + writeFileSync(settingsPath, JSON.stringify({ shellCommandPrefix: "shopt -s expand_aliases" })); + + const manager = SettingsManager.create(projectDir, agentDir); + + expect(manager.getShellCommandPrefix()).toBe("shopt -s expand_aliases"); + }); + + it("should return undefined when shellCommandPrefix is not set", () => { + const settingsPath = join(agentDir, "settings.json"); + writeFileSync(settingsPath, JSON.stringify({ theme: "dark" })); + + const manager = SettingsManager.create(projectDir, agentDir); + + expect(manager.getShellCommandPrefix()).toBeUndefined(); + }); + + it("should preserve shellCommandPrefix when saving unrelated settings", async () => { + const settingsPath = join(agentDir, "settings.json"); + writeFileSync(settingsPath, JSON.stringify({ shellCommandPrefix: "shopt -s expand_aliases" })); + + const manager = SettingsManager.create(projectDir, agentDir); + manager.setTheme("light"); + await manager.flush(); + + const savedSettings = JSON.parse(readFileSync(settingsPath, "utf-8")); + expect(savedSettings.shellCommandPrefix).toBe("shopt -s expand_aliases"); + expect(savedSettings.theme).toBe("light"); + }); + }); + + describe("defaultTools", () => { + it("loads global defaults and lets project settings replace them", () => { + writeFileSync(join(agentDir, "settings.json"), JSON.stringify({ defaultTools: ["read", "bash"] })); + + expect(SettingsManager.create(projectDir, agentDir).getDefaultTools()).toEqual(["read", "bash"]); + + writeFileSync(join(projectDir, ".pi", "settings.json"), JSON.stringify({ defaultTools: ["grep"] })); + + expect(SettingsManager.create(projectDir, agentDir).getDefaultTools()).toEqual(["grep"]); + }); + + it("preserves an empty tool list", () => { + expect(SettingsManager.inMemory({ defaultTools: [] }).getDefaultTools()).toEqual([]); + expect(SettingsManager.inMemory().getDefaultTools()).toBeUndefined(); + }); + }); + + describe("getSessionDir", () => { + it("should return undefined when not set", () => { + writeFileSync(join(agentDir, "settings.json"), JSON.stringify({ theme: "dark" })); + const manager = SettingsManager.create(projectDir, agentDir); + expect(manager.getSessionDir()).toBeUndefined(); + }); + + it("should return global sessionDir", () => { + writeFileSync(join(agentDir, "settings.json"), JSON.stringify({ sessionDir: "/tmp/sessions" })); + const manager = SettingsManager.create(projectDir, agentDir); + expect(manager.getSessionDir()).toBe("/tmp/sessions"); + }); + + it("should return project sessionDir, overriding global", () => { + writeFileSync(join(agentDir, "settings.json"), JSON.stringify({ sessionDir: "/global/sessions" })); + writeFileSync(join(projectDir, ".pi", "settings.json"), JSON.stringify({ sessionDir: "./sessions" })); + const manager = SettingsManager.create(projectDir, agentDir); + expect(manager.getSessionDir()).toBe("./sessions"); + }); + + it("should expand ~ in sessionDir", () => { + writeFileSync(join(agentDir, "settings.json"), JSON.stringify({ sessionDir: "~/sessions" })); + const manager = SettingsManager.create(projectDir, agentDir); + expect(manager.getSessionDir()).toBe(join(homedir(), "sessions")); + }); + }); + + describe("getShellPath", () => { + it("should return undefined when not set", () => { + writeFileSync(join(agentDir, "settings.json"), JSON.stringify({ theme: "dark" })); + const manager = SettingsManager.create(projectDir, agentDir); + expect(manager.getShellPath()).toBeUndefined(); + }); + + it("should return an absolute shellPath unchanged", () => { + writeFileSync(join(agentDir, "settings.json"), JSON.stringify({ shellPath: "/bin/zsh" })); + const manager = SettingsManager.create(projectDir, agentDir); + expect(manager.getShellPath()).toBe("/bin/zsh"); + }); + + it("should expand ~ in shellPath", () => { + writeFileSync( + join(agentDir, "settings.json"), + JSON.stringify({ shellPath: "~/.local/bin/agent-shell-sandbox" }), + ); + const manager = SettingsManager.create(projectDir, agentDir); + expect(manager.getShellPath()).toBe(join(homedir(), ".local/bin/agent-shell-sandbox")); + }); + + it("should expand a bare ~ in shellPath", () => { + writeFileSync(join(agentDir, "settings.json"), JSON.stringify({ shellPath: "~" })); + const manager = SettingsManager.create(projectDir, agentDir); + expect(manager.getShellPath()).toBe(homedir()); + }); + }); +}); diff --git a/packages/coding-agent/test/settings-selector.test.ts b/packages/coding-agent/test/settings-selector.test.ts new file mode 100644 index 0000000000000000000000000000000000000000..ffe201d88a448e380379242c1a1f7973e66647c0 --- /dev/null +++ b/packages/coding-agent/test/settings-selector.test.ts @@ -0,0 +1,138 @@ +import { setKeybindings } from "@earendil-works/pi-tui"; +import { afterEach, beforeAll, describe, expect, it, vi } from "vitest"; +import { KeybindingsManager } from "../src/core/keybindings.ts"; +import { + type SettingsCallbacks, + type SettingsConfig, + SettingsSelectorComponent, +} from "../src/modes/interactive/components/settings-selector.ts"; +import { initTheme } from "../src/modes/interactive/theme/theme.ts"; +import { stripAnsi } from "../src/utils/ansi.ts"; +import { createHarness, type Harness } from "./suite/harness.ts"; + +describe("SettingsSelectorComponent", () => { + let harness: Harness | undefined; + beforeAll(() => { + initTheme("dark"); + setKeybindings(new KeybindingsManager()); + }); + + afterEach(() => { + harness?.cleanup(); + harness = undefined; + }); + + it("cycles through fullscreen settings", () => { + const onExitOutputChange = vi.fn(); + const onScrollbarChange = vi.fn(); + const onCopyOnSelectChange = vi.fn(); + const config = { + fullscreenExitOutput: "transcript", + fullscreenScrollbar: "auto", + fullscreenCopyOnSelect: true, + warnings: {}, + defaultModel: "not set", + availableDefaultModels: [], + availableThinkingLevels: [], + modelThinkingLevels: {}, + availableThemes: [], + } as unknown as SettingsConfig; + const callbacks = { + onFullscreenExitOutputChange: onExitOutputChange, + onFullscreenScrollbarChange: onScrollbarChange, + onFullscreenCopyOnSelectChange: onCopyOnSelectChange, + } as unknown as SettingsCallbacks; + + const cycle = (label: string, count: number) => { + const list = new SettingsSelectorComponent(config, callbacks).getSettingsList(); + for (const character of label) list.handleInput(character); + for (let i = 0; i < count; i++) list.handleInput("\r"); + }; + + cycle("Fullscreen exit output", 2); + expect(onExitOutputChange.mock.calls.flat()).toEqual(["resume-hint", "transcript"]); + cycle("Fullscreen scrollbar", 3); + expect(onScrollbarChange.mock.calls.flat()).toEqual(["always", "hidden", "auto"]); + cycle("Fullscreen copy on select", 2); + expect(onCopyOnSelectChange.mock.calls.flat()).toEqual([false, true]); + }); + + it("keeps the configured fixed theme marked while browsing", () => { + const config = { + defaultModel: "not set", + availableDefaultModels: [], + modelThinkingLevels: {}, + currentTheme: "dark", + terminalTheme: "dark", + availableThemes: ["dark", "light"], + warnings: {}, + } as unknown as SettingsConfig; + const callbacks = { onThemePreview: vi.fn(), onCancel: () => {} } as unknown as SettingsCallbacks; + const list = new SettingsSelectorComponent(config, callbacks).getSettingsList(); + + list.selectItem("theme"); + list.handleInput("\r"); + let output = stripAnsi(list.render(120).join("\n")); + expect(output).toContain(" Automatic"); + expect(output).toContain("→ ✓ dark"); + + list.handleInput("\x1b[B"); + output = stripAnsi(list.render(120).join("\n")); + expect(output).toContain(" ✓ dark"); + expect(output).toContain("→ light"); + }); + + it("keeps a configured automatic theme marked while browsing", () => { + const config = { + defaultModel: "not set", + availableDefaultModels: [], + modelThinkingLevels: {}, + currentTheme: "light/dark", + terminalTheme: "dark", + availableThemes: ["dark", "light", "other"], + warnings: {}, + } as unknown as SettingsConfig; + const callbacks = { onThemePreview: vi.fn(), onCancel: () => {} } as unknown as SettingsCallbacks; + const list = new SettingsSelectorComponent(config, callbacks).getSettingsList(); + + list.selectItem("theme"); + list.handleInput("\r"); + list.handleInput("\r"); + let output = stripAnsi(list.render(120).join("\n")); + expect(output).toContain("→ ✓ light"); + + list.handleInput("\x1b[B"); + output = stripAnsi(list.render(120).join("\n")); + expect(output).toContain(" ✓ light"); + expect(output).toContain("→ other"); + }); + + it("keeps the configured per-model thinking level marked while browsing", async () => { + harness = await createHarness({ + models: [{ id: "thinking-model", reasoning: true }], + }); + const model = harness.getModel("thinking-model")!; + const modelKey = `${model.provider}/${model.id}`; + const config = { + defaultModel: modelKey, + availableDefaultModels: [model], + thinkingLevel: "high", + modelThinkingLevels: { [modelKey]: "medium" }, + } as unknown as SettingsConfig; + const callbacks = { onCancel: () => {} } as unknown as SettingsCallbacks; + const list = new SettingsSelectorComponent(config, callbacks).getSettingsList(); + + list.selectItem("model-thinking"); + list.handleInput("\r"); + list.handleInput("\r"); + + let output = stripAnsi(list.render(120).join("\n")); + expect(output).toContain("→ ✓ medium"); + expect(output).toContain(" (clear override)"); + + list.handleInput("\x1b[B"); + output = stripAnsi(list.render(120).join("\n")); + expect(output).toContain(" ✓ medium"); + expect(output).toContain("→ high"); + }); +}); diff --git a/packages/coding-agent/test/skills.test.ts b/packages/coding-agent/test/skills.test.ts new file mode 100644 index 0000000000000000000000000000000000000000..039801994d83197034be1743d43d79b82771209e --- /dev/null +++ b/packages/coding-agent/test/skills.test.ts @@ -0,0 +1,432 @@ +import { homedir } from "os"; +import { join, resolve } from "path"; +import { describe, expect, it } from "vitest"; +import type { ResourceDiagnostic } from "../src/core/diagnostics.ts"; +import { formatSkillsForPrompt, loadSkills, loadSkillsFromDir, type Skill } from "../src/core/skills.ts"; +import { createSyntheticSourceInfo } from "../src/core/source-info.ts"; + +const fixturesDir = resolve(__dirname, "fixtures/skills"); +const collisionFixturesDir = resolve(__dirname, "fixtures/skills-collision"); + +function createTestSkill(options: { + name: string; + description: string; + filePath: string; + baseDir: string; + disableModelInvocation?: boolean; + source?: string; +}): Skill { + return { + name: options.name, + description: options.description, + filePath: options.filePath, + baseDir: options.baseDir, + sourceInfo: createSyntheticSourceInfo(options.filePath, { source: options.source ?? "test" }), + disableModelInvocation: options.disableModelInvocation ?? false, + }; +} + +describe("skills", () => { + describe("loadSkillsFromDir", () => { + it("should load a valid skill", () => { + const { skills, diagnostics } = loadSkillsFromDir({ + dir: join(fixturesDir, "valid-skill"), + source: "test", + }); + + expect(skills).toHaveLength(1); + expect(skills[0].name).toBe("valid-skill"); + expect(skills[0].description).toBe("A valid skill for testing purposes."); + expect(skills[0].sourceInfo.source).toBe("test"); + expect(diagnostics).toHaveLength(0); + }); + + it("should allow names that don't match parent directory", () => { + const { skills, diagnostics } = loadSkillsFromDir({ + dir: join(fixturesDir, "name-mismatch"), + source: "test", + }); + + expect(skills).toHaveLength(1); + expect(skills[0].name).toBe("different-name"); + expect( + diagnostics.some((d: ResourceDiagnostic) => d.message.includes("does not match parent directory")), + ).toBe(false); + }); + + it("should warn when name contains invalid characters", () => { + const { skills, diagnostics } = loadSkillsFromDir({ + dir: join(fixturesDir, "invalid-name-chars"), + source: "test", + }); + + expect(skills).toHaveLength(1); + expect(diagnostics.some((d: ResourceDiagnostic) => d.message.includes("invalid characters"))).toBe(true); + }); + + it("should warn when name exceeds 64 characters", () => { + const { skills, diagnostics } = loadSkillsFromDir({ + dir: join(fixturesDir, "long-name"), + source: "test", + }); + + expect(skills).toHaveLength(1); + expect(diagnostics.some((d: ResourceDiagnostic) => d.message.includes("exceeds 64 characters"))).toBe(true); + }); + + it("should warn and skip skill when description is missing", () => { + const { skills, diagnostics } = loadSkillsFromDir({ + dir: join(fixturesDir, "missing-description"), + source: "test", + }); + + expect(skills).toHaveLength(0); + expect(diagnostics.some((d: ResourceDiagnostic) => d.message.includes("description is required"))).toBe(true); + }); + + it("should ignore unknown frontmatter fields", () => { + const { skills, diagnostics } = loadSkillsFromDir({ + dir: join(fixturesDir, "unknown-field"), + source: "test", + }); + + expect(skills).toHaveLength(1); + expect(diagnostics).toHaveLength(0); + }); + + it("should load nested skills recursively", () => { + const { skills, diagnostics } = loadSkillsFromDir({ + dir: join(fixturesDir, "nested"), + source: "test", + }); + + expect(skills).toHaveLength(1); + expect(skills[0].name).toBe("child-skill"); + expect(diagnostics).toHaveLength(0); + }); + + it("should prefer a directory's root SKILL.md over nested SKILL.md files", () => { + const { skills, diagnostics } = loadSkillsFromDir({ + dir: join(fixturesDir, "root-skill-preferred"), + source: "test", + }); + + expect(skills).toHaveLength(1); + expect(skills[0].name).toBe("root-skill-preferred"); + expect(skills[0].description).toBe("Root skill should win."); + expect(diagnostics).toHaveLength(0); + }); + + it("should skip files without frontmatter", () => { + const { skills, diagnostics } = loadSkillsFromDir({ + dir: join(fixturesDir, "no-frontmatter"), + source: "test", + }); + + // no-frontmatter has no description, so it should be skipped + expect(skills).toHaveLength(0); + expect(diagnostics.some((d: ResourceDiagnostic) => d.message.includes("description is required"))).toBe(true); + }); + + it("should warn and skip skill when YAML frontmatter is invalid", () => { + const { skills, diagnostics } = loadSkillsFromDir({ + dir: join(fixturesDir, "invalid-yaml"), + source: "test", + }); + + expect(skills).toHaveLength(0); + expect(diagnostics.some((d: ResourceDiagnostic) => d.message.includes("at line"))).toBe(true); + }); + + it("should preserve multiline descriptions from YAML", () => { + const { skills, diagnostics } = loadSkillsFromDir({ + dir: join(fixturesDir, "multiline-description"), + source: "test", + }); + + expect(skills).toHaveLength(1); + expect(skills[0].description).toContain("\n"); + expect(skills[0].description).toContain("This is a multiline description."); + expect(diagnostics).toHaveLength(0); + }); + + it("should warn when name contains consecutive hyphens", () => { + const { skills, diagnostics } = loadSkillsFromDir({ + dir: join(fixturesDir, "consecutive-hyphens"), + source: "test", + }); + + expect(skills).toHaveLength(1); + expect(diagnostics.some((d: ResourceDiagnostic) => d.message.includes("consecutive hyphens"))).toBe(true); + }); + + it("should load all skills from fixture directory", () => { + const { skills } = loadSkillsFromDir({ + dir: fixturesDir, + source: "test", + }); + + // Should load all skills that have descriptions (even with warnings) + // valid-skill, name-mismatch, invalid-name-chars, long-name, unknown-field, nested/child-skill, consecutive-hyphens + // NOT: missing-description, no-frontmatter (both missing descriptions) + expect(skills.length).toBeGreaterThanOrEqual(6); + }); + + it("should return empty for non-existent directory", () => { + const { skills, diagnostics } = loadSkillsFromDir({ + dir: "/non/existent/path", + source: "test", + }); + + expect(skills).toHaveLength(0); + expect(diagnostics).toHaveLength(0); + }); + + it("should use parent directory name when name not in frontmatter", () => { + // The no-frontmatter fixture has no name in frontmatter, so it should use "no-frontmatter" + // But it also has no description, so it won't load + // Let's test with a valid skill that relies on directory name + const { skills } = loadSkillsFromDir({ + dir: join(fixturesDir, "valid-skill"), + source: "test", + }); + + expect(skills).toHaveLength(1); + expect(skills[0].name).toBe("valid-skill"); + }); + + it("should parse disable-model-invocation frontmatter field", () => { + const { skills, diagnostics } = loadSkillsFromDir({ + dir: join(fixturesDir, "disable-model-invocation"), + source: "test", + }); + + expect(skills).toHaveLength(1); + expect(skills[0].name).toBe("disable-model-invocation"); + expect(skills[0].disableModelInvocation).toBe(true); + // Should not warn about unknown field + expect(diagnostics.some((d: ResourceDiagnostic) => d.message.includes("unknown frontmatter field"))).toBe( + false, + ); + }); + + it("should default disableModelInvocation to false when not specified", () => { + const { skills } = loadSkillsFromDir({ + dir: join(fixturesDir, "valid-skill"), + source: "test", + }); + + expect(skills).toHaveLength(1); + expect(skills[0].disableModelInvocation).toBe(false); + }); + }); + + describe("formatSkillsForPrompt", () => { + it("should return empty string for no skills", () => { + const result = formatSkillsForPrompt([]); + expect(result).toBe(""); + }); + + it("should format skills as XML", () => { + const skills: Skill[] = [ + createTestSkill({ + name: "test-skill", + description: "A test skill.", + filePath: "/path/to/skill/SKILL.md", + baseDir: "/path/to/skill", + }), + ]; + + const result = formatSkillsForPrompt(skills); + + expect(result).toContain(""); + expect(result).toContain(""); + expect(result).toContain(""); + expect(result).toContain("test-skill"); + expect(result).toContain("A test skill."); + expect(result).toContain("/path/to/skill/SKILL.md"); + }); + + it("should include intro text before XML", () => { + const skills: Skill[] = [ + createTestSkill({ + name: "test-skill", + description: "A test skill.", + filePath: "/path/to/skill/SKILL.md", + baseDir: "/path/to/skill", + }), + ]; + + const result = formatSkillsForPrompt(skills); + const xmlStart = result.indexOf(""); + const introText = result.substring(0, xmlStart); + + expect(introText).toContain("The following skills provide specialized instructions"); + expect(introText).toContain("Use the read tool to load a skill's file"); + }); + + it("should escape XML special characters", () => { + const skills: Skill[] = [ + createTestSkill({ + name: "test-skill", + description: 'A skill with & "characters".', + filePath: "/path/to/skill/SKILL.md", + baseDir: "/path/to/skill", + }), + ]; + + const result = formatSkillsForPrompt(skills); + + expect(result).toContain("<special>"); + expect(result).toContain("&"); + expect(result).toContain(""characters""); + }); + + it("should format multiple skills", () => { + const skills: Skill[] = [ + createTestSkill({ + name: "skill-one", + description: "First skill.", + filePath: "/path/one/SKILL.md", + baseDir: "/path/one", + }), + createTestSkill({ + name: "skill-two", + description: "Second skill.", + filePath: "/path/two/SKILL.md", + baseDir: "/path/two", + }), + ]; + + const result = formatSkillsForPrompt(skills); + + expect(result).toContain("skill-one"); + expect(result).toContain("skill-two"); + expect((result.match(//g) || []).length).toBe(2); + }); + + it("should exclude skills with disableModelInvocation from prompt", () => { + const skills: Skill[] = [ + createTestSkill({ + name: "visible-skill", + description: "A visible skill.", + filePath: "/path/visible/SKILL.md", + baseDir: "/path/visible", + }), + createTestSkill({ + name: "hidden-skill", + description: "A hidden skill.", + filePath: "/path/hidden/SKILL.md", + baseDir: "/path/hidden", + disableModelInvocation: true, + }), + ]; + + const result = formatSkillsForPrompt(skills); + + expect(result).toContain("visible-skill"); + expect(result).not.toContain("hidden-skill"); + expect((result.match(//g) || []).length).toBe(1); + }); + + it("should return empty string when all skills have disableModelInvocation", () => { + const skills: Skill[] = [ + createTestSkill({ + name: "hidden-skill", + description: "A hidden skill.", + filePath: "/path/hidden/SKILL.md", + baseDir: "/path/hidden", + disableModelInvocation: true, + }), + ]; + + const result = formatSkillsForPrompt(skills); + expect(result).toBe(""); + }); + }); + + describe("loadSkills with options", () => { + const emptyAgentDir = resolve(__dirname, "fixtures/empty-agent"); + const emptyCwd = resolve(__dirname, "fixtures/empty-cwd"); + + it("should load from explicit skillPaths", () => { + const { skills, diagnostics } = loadSkills({ + agentDir: emptyAgentDir, + cwd: emptyCwd, + skillPaths: [join(fixturesDir, "valid-skill")], + includeDefaults: true, + }); + expect(skills).toHaveLength(1); + expect(skills[0].sourceInfo.scope).toBe("temporary"); + expect(diagnostics).toHaveLength(0); + }); + + it("should warn when skill path does not exist", () => { + const { skills, diagnostics } = loadSkills({ + agentDir: emptyAgentDir, + cwd: emptyCwd, + skillPaths: ["/non/existent/path"], + includeDefaults: true, + }); + expect(skills).toHaveLength(0); + expect(diagnostics.some((d: ResourceDiagnostic) => d.message.includes("does not exist"))).toBe(true); + }); + + it("should expand ~ in skillPaths", () => { + const homeSkillsDir = join(homedir(), ".pi/agent/skills"); + const { skills: withTilde } = loadSkills({ + agentDir: emptyAgentDir, + cwd: emptyCwd, + skillPaths: ["~/.pi/agent/skills"], + includeDefaults: true, + }); + const { skills: withoutTilde } = loadSkills({ + agentDir: emptyAgentDir, + cwd: emptyCwd, + skillPaths: [homeSkillsDir], + includeDefaults: true, + }); + expect(withTilde.length).toBe(withoutTilde.length); + }); + }); + + describe("collision handling", () => { + it("should detect name collisions and keep first skill", () => { + // Load from first directory + const first = loadSkillsFromDir({ + dir: join(collisionFixturesDir, "first"), + source: "first", + }); + + const second = loadSkillsFromDir({ + dir: join(collisionFixturesDir, "second"), + source: "second", + }); + + // Simulate the collision behavior from loadSkills() + const skillMap = new Map(); + const collisionWarnings: Array<{ skillPath: string; message: string }> = []; + + for (const skill of first.skills) { + skillMap.set(skill.name, skill); + } + + for (const skill of second.skills) { + const existing = skillMap.get(skill.name); + if (existing) { + collisionWarnings.push({ + skillPath: skill.filePath, + message: `name collision: "${skill.name}" already loaded from ${existing.filePath}`, + }); + } else { + skillMap.set(skill.name, skill); + } + } + + expect(skillMap.size).toBe(1); + expect(skillMap.get("calendar")?.sourceInfo.source).toBe("first"); + expect(collisionWarnings).toHaveLength(1); + expect(collisionWarnings[0].message).toContain("name collision"); + }); + }); +}); diff --git a/packages/coding-agent/test/startup-session-name.test.ts b/packages/coding-agent/test/startup-session-name.test.ts new file mode 100644 index 0000000000000000000000000000000000000000..c953cd1ae310ceca1fa22468b1a69195b27019c0 --- /dev/null +++ b/packages/coding-agent/test/startup-session-name.test.ts @@ -0,0 +1,122 @@ +import { spawn } from "node:child_process"; +import { mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs"; +import { tmpdir } from "node:os"; +import { join, resolve } from "node:path"; +import { afterEach, describe, expect, it } from "vitest"; +import { ENV_AGENT_DIR } from "../src/config.ts"; + +const cliPath = resolve(__dirname, "../src/cli.ts"); +const sourceResolverPath = resolve(__dirname, "../src/experimental/source-resolver.ts"); +const tempDirs: string[] = []; + +afterEach(() => { + for (const dir of tempDirs.splice(0)) { + rmSync(dir, { recursive: true, force: true }); + } +}); + +function createTempDir(): string { + const dir = mkdtempSync(join(tmpdir(), "pi-startup-session-name-")); + tempDirs.push(dir); + return dir; +} + +interface CliDirs { + agentDir: string; + projectDir: string; + sessionFile: string; +} + +interface CliResult { + code: number | null; + signal: NodeJS.Signals | null; + stderr: string; +} + +function createSessionFile(projectDir: string, sessionFile: string): void { + const timestamp = new Date().toISOString(); + writeFileSync( + sessionFile, + `${JSON.stringify({ type: "session", version: 3, id: "existing-session", timestamp, cwd: projectDir })}\n${JSON.stringify( + { + type: "message", + id: "assistant-1", + parentId: null, + timestamp, + message: { + role: "assistant", + content: [{ type: "text", text: "hello" }], + provider: "anthropic", + model: "claude-sonnet-4-5", + timestamp: Date.now(), + }, + }, + )}\n`, + ); +} + +function readSessionInfoNames(sessionFile: string): string[] { + return readFileSync(sessionFile, "utf8") + .trim() + .split("\n") + .map((line) => JSON.parse(line) as { type?: string; name?: string }) + .filter((entry) => entry.type === "session_info") + .map((entry) => entry.name ?? ""); +} + +async function runCli(args: string[], dirs: CliDirs): Promise { + let stderr = ""; + const child = spawn(process.execPath, ["--import", sourceResolverPath, cliPath, ...args], { + cwd: dirs.projectDir, + env: { + ...process.env, + [ENV_AGENT_DIR]: dirs.agentDir, + PI_OFFLINE: "1", + }, + stdio: ["ignore", "ignore", "pipe"], + }); + child.stderr.on("data", (chunk) => { + stderr += chunk.toString(); + }); + + return new Promise((resolvePromise, reject) => { + const timeout = setTimeout(() => { + child.kill("SIGKILL"); + }, 10_000); + child.on("error", (error) => { + clearTimeout(timeout); + reject(error); + }); + child.on("close", (code, signal) => { + clearTimeout(timeout); + resolvePromise({ code, signal, stderr }); + }); + }); +} + +function setup(): CliDirs { + const tempRoot = createTempDir(); + const dirs = { + agentDir: join(tempRoot, "agent"), + projectDir: join(tempRoot, "project"), + sessionFile: join(tempRoot, "session.jsonl"), + }; + mkdirSync(dirs.agentDir, { recursive: true }); + mkdirSync(dirs.projectDir, { recursive: true }); + createSessionFile(dirs.projectDir, dirs.sessionFile); + return dirs; +} + +describe("startup session name", () => { + it("sets --name on the selected session before runtime model validation", async () => { + const dirs = setup(); + const result = await runCli( + ["--session", dirs.sessionFile, "--name", " CLI Named Session ", "--model", "missing-model", "-p", "hi"], + dirs, + ); + + expect(result.code).toBe(1); + expect(result.signal).toBeNull(); + expect(readSessionInfoNames(dirs.sessionFile)).toEqual(["CLI Named Session"]); + }); +}); diff --git a/packages/coding-agent/test/status-indicator.test.ts b/packages/coding-agent/test/status-indicator.test.ts new file mode 100644 index 0000000000000000000000000000000000000000..5efc810f9ddf287931bf03c93c00b693e3e2d819 --- /dev/null +++ b/packages/coding-agent/test/status-indicator.test.ts @@ -0,0 +1,111 @@ +import { type TUI, visibleWidth } from "@earendil-works/pi-tui"; +import { afterEach, describe, expect, it, vi } from "vitest"; +import { KeybindingsManager } from "../src/core/keybindings.ts"; +import { CustomEditor } from "../src/modes/interactive/components/custom-editor.ts"; +import { + BranchSummaryStatusIndicator, + CompactionStatusIndicator, + IdleStatus, + RetryStatusIndicator, + WorkingStatusIndicator, +} from "../src/modes/interactive/components/status-indicator.ts"; +import { getEditorTheme, initTheme, theme } from "../src/modes/interactive/theme/theme.ts"; +import { stripAnsi } from "../src/utils/ansi.ts"; + +describe("status indicators", () => { + afterEach(() => { + vi.useRealTimers(); + }); + + it("keeps idle status at the same height as standalone status indicators", () => { + const idleStatus = new IdleStatus(); + + const lines = idleStatus.render(20); + expect(lines).toHaveLength(2); + expect(lines).toEqual([" ".repeat(20), " ".repeat(20)]); + }); + + it("keeps the top border unchanged unless the editor opts in", () => { + initTheme("dark"); + const tui = { + requestRender: vi.fn(), + terminal: { rows: 10 }, + } as unknown as TUI; + const editor = new CustomEditor(tui, getEditorTheme(), KeybindingsManager.create()); + const indicator = new WorkingStatusIndicator(tui, "Working"); + editor.setWorkingStatusIndicator(indicator); + + expect(stripAnsi(editor.render(20)[0]!)).toBe("─".repeat(20)); + const standaloneLine = indicator.render(20)[1]!; + expect(standaloneLine).toContain(theme.getFgAnsi("accent")); + expect(standaloneLine).toContain(theme.getFgAnsi("muted")); + indicator.dispose(); + }); + + it("embeds the working indicator when the editor opts in", () => { + initTheme("dark"); + const tui = { + requestRender: vi.fn(), + terminal: { rows: 10 }, + } as unknown as TUI; + const editor = new CustomEditor(tui, getEditorTheme(), KeybindingsManager.create(), { + embedWorkingStatus: true, + }); + expect(editor.embedWorkingStatus).toBe(true); + editor.borderColor = theme.getThinkingBorderColor("high"); + const indicator = new WorkingStatusIndicator(tui, "Working", undefined, (text) => editor.borderColor(text)); + editor.setWorkingStatusIndicator(indicator); + + const topBorder = editor.render(20)[0]!; + expect(stripAnsi(topBorder)).toBe("── ⠋ Working ───────"); + expect(visibleWidth(topBorder)).toBe(20); + expect(topBorder.split(theme.getFgAnsi("thinkingHigh"))).toHaveLength(5); + indicator.dispose(); + }); + + it("embeds compaction, summary, and retry labels within the border width", () => { + initTheme("dark"); + vi.useFakeTimers(); + const tui = { requestRender: vi.fn(), terminal: { rows: 10 } } as unknown as TUI; + const editor = new CustomEditor(tui, getEditorTheme(), KeybindingsManager.create(), { + embedWorkingStatus: true, + }); + const indicators = [ + new CompactionStatusIndicator(tui, "manual"), + new CompactionStatusIndicator(tui, "threshold"), + new CompactionStatusIndicator(tui, "overflow"), + new BranchSummaryStatusIndicator(tui), + new RetryStatusIndicator(tui, 1, 3, 3000), + ]; + try { + for (const indicator of indicators) { + editor.setWorkingStatusIndicator(indicator); + const label = stripAnsi(indicator.render(120)[1]!).trim(); + expect(stripAnsi(editor.render(120)[0]!)).toContain(`── ${label} `); + for (const width of [1, 4, 10, 20, 80, 120]) { + expect(visibleWidth(editor.render(width)[0]!)).toBe(width); + } + } + vi.advanceTimersByTime(1000); + expect(stripAnsi(editor.render(120)[0]!)).toContain("Retrying (1/3) in 2s"); + editor.setWorkingStatusIndicator(undefined); + expect(stripAnsi(editor.render(120)[0]!)).toBe("─".repeat(120)); + } finally { + for (const indicator of indicators) indicator.dispose(); + } + }); + + it("disposes retry countdown updates", () => { + initTheme("dark"); + vi.useFakeTimers(); + const requestRender = vi.fn(); + const tui = { requestRender } as unknown as TUI; + const indicator = new RetryStatusIndicator(tui, 1, 3, 1000); + const callsBeforeDispose = requestRender.mock.calls.length; + + indicator.dispose(); + vi.advanceTimersByTime(2000); + + expect(requestRender).toHaveBeenCalledTimes(callsBeforeDispose); + }); +}); diff --git a/packages/coding-agent/test/stdout-cleanliness.test.ts b/packages/coding-agent/test/stdout-cleanliness.test.ts new file mode 100644 index 0000000000000000000000000000000000000000..baaf296a5aba0e447576316f928980ae7c85f15c --- /dev/null +++ b/packages/coding-agent/test/stdout-cleanliness.test.ts @@ -0,0 +1,107 @@ +import { spawn } from "node:child_process"; +import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from "node:fs"; +import { tmpdir } from "node:os"; +import { join, resolve } from "node:path"; +import { afterEach, beforeEach, describe, expect, it } from "vitest"; +import { ENV_AGENT_DIR } from "../src/config.ts"; +import { allowNetwork } from "./test-network-env.ts"; + +const cliPath = resolve(__dirname, "../src/cli.ts"); +const sourceResolverPath = resolve(__dirname, "../src/experimental/source-resolver.ts"); + +const tempDirs: string[] = []; + +beforeEach(() => { + allowNetwork(); +}); + +afterEach(() => { + for (const dir of tempDirs.splice(0)) { + rmSync(dir, { recursive: true, force: true }); + } +}); + +function createTempDir(): string { + const dir = mkdtempSync(join(tmpdir(), "pi-stdout-clean-")); + tempDirs.push(dir); + return dir; +} + +async function runCli(args: string[]): Promise<{ stdout: string; stderr: string; code: number | null }> { + const tempRoot = createTempDir(); + const agentDir = join(tempRoot, "agent"); + const projectDir = join(tempRoot, "project"); + const projectConfigDir = join(projectDir, ".pi"); + mkdirSync(agentDir, { recursive: true }); + mkdirSync(projectConfigDir, { recursive: true }); + + const fakeNpmPath = join(tempRoot, "fake-npm.mjs"); + writeFileSync( + fakeNpmPath, + [ + 'console.log("changed 1 package in 471ms");', + 'console.log("found 0 vulnerabilities");', + "process.exit(0);", + ].join("\n"), + "utf-8", + ); + + writeFileSync( + join(projectConfigDir, "settings.json"), + JSON.stringify( + { + packages: ["npm:fake-package"], + npmCommand: [process.execPath, fakeNpmPath], + }, + null, + 2, + ), + "utf-8", + ); + + return await new Promise((resolvePromise, reject) => { + const child = spawn(process.execPath, ["--import", sourceResolverPath, cliPath, ...args], { + cwd: projectDir, + env: { + ...process.env, + [ENV_AGENT_DIR]: agentDir, + }, + stdio: ["ignore", "pipe", "pipe"], + }); + + let stdout = ""; + let stderr = ""; + child.stdout.on("data", (chunk) => { + stdout += chunk.toString(); + }); + child.stderr.on("data", (chunk) => { + stderr += chunk.toString(); + }); + child.on("error", reject); + child.on("close", (code) => { + resolvePromise({ stdout, stderr, code }); + }); + }); +} + +describe("stdout cleanliness in non-interactive modes", () => { + it("prints plain --help to stdout when stdout is redirected", async () => { + const result = await runCli(["--help"]); + + expect(result.code).toBe(0); + expect(result.stdout).toContain("Usage:"); + expect(result.stderr).not.toContain("Usage:"); + expect(result.stderr).not.toContain("changed 1 package in 471ms"); + expect(result.stderr).not.toContain("found 0 vulnerabilities"); + }); + + it("keeps stdout empty for --mode json --help while routing trusted startup chatter to stderr", async () => { + const result = await runCli(["--mode", "json", "--help", "--approve"]); + + expect(result.code).toBe(0); + expect(result.stdout).toBe(""); + expect(result.stderr).toContain("changed 1 package in 471ms"); + expect(result.stderr).toContain("found 0 vulnerabilities"); + expect(result.stderr).toContain("Usage:"); + }); +}); diff --git a/packages/coding-agent/test/streaming-render-debug.ts b/packages/coding-agent/test/streaming-render-debug.ts new file mode 100644 index 0000000000000000000000000000000000000000..934782380bba869795c00dadf1b6b3ccd9edb5d4 --- /dev/null +++ b/packages/coding-agent/test/streaming-render-debug.ts @@ -0,0 +1,97 @@ +/** + * Debug script to reproduce streaming rendering issues. + * Uses real fixture data that caused the bug. + * Run with: npx tsx test/streaming-render-debug.ts + */ + +import type { AssistantMessage } from "@earendil-works/pi-ai"; +import { ProcessTerminal, type TUI, TuiMainScreen } from "@earendil-works/pi-tui"; +import { readFileSync } from "fs"; +import { dirname, join } from "path"; +import { fileURLToPath } from "url"; +import { AssistantMessageComponent } from "../src/modes/interactive/components/assistant-message.ts"; +import { initTheme } from "../src/modes/interactive/theme/theme.ts"; + +const __filename = fileURLToPath(import.meta.url); +const __dirname = dirname(__filename); + +// Initialize dark theme with full color support +process.env.COLORTERM = "truecolor"; +initTheme("dark"); + +// Load the real fixture that caused the bug +const fixtureMessage: AssistantMessage = JSON.parse( + readFileSync(join(__dirname, "fixtures/assistant-message-with-thinking-code.json"), "utf-8"), +); + +// Extract thinking and text content +const thinkingContent = fixtureMessage.content.find((c) => c.type === "thinking"); +const textContent = fixtureMessage.content.find((c) => c.type === "text"); + +if (!thinkingContent || thinkingContent.type !== "thinking") { + console.error("No thinking content in fixture"); + process.exit(1); +} + +const fullThinkingText = thinkingContent.thinking; +const fullTextContent = textContent && textContent.type === "text" ? textContent.text : ""; + +async function sleep(ms: number): Promise { + return new Promise((resolve) => setTimeout(resolve, ms)); +} + +async function main() { + const terminal = new ProcessTerminal(); + const tui: TUI = new TuiMainScreen(terminal); + + // Start with empty message + const message = { + role: "assistant", + content: [{ type: "thinking", thinking: "" }], + } as AssistantMessage; + + const component = new AssistantMessageComponent(message, false); + tui.addChild(component); + tui.start(); + + // Simulate streaming thinking content + let thinkingBuffer = ""; + const chunkSize = 10; // characters per "token" + + for (let i = 0; i < fullThinkingText.length; i += chunkSize) { + thinkingBuffer += fullThinkingText.slice(i, i + chunkSize); + + // Update message content + const updatedMessage = { + role: "assistant", + content: [{ type: "thinking", thinking: thinkingBuffer }], + } as AssistantMessage; + + component.updateContent(updatedMessage); + tui.requestRender(); + + await sleep(15); // Simulate token delay + } + + // Now add the text content + await sleep(500); + + const finalMessage = { + role: "assistant", + content: [ + { type: "thinking", thinking: fullThinkingText }, + { type: "text", text: fullTextContent }, + ], + } as AssistantMessage; + + component.updateContent(finalMessage); + tui.requestRender(); + + // Keep alive for a moment to see the result + await sleep(3000); + + tui.stop(); + process.exit(0); +} + +main().catch(console.error); diff --git a/packages/coding-agent/test/syntax-highlight.test.ts b/packages/coding-agent/test/syntax-highlight.test.ts new file mode 100644 index 0000000000000000000000000000000000000000..62fe5f025fb57bae09bc57122273fe9aadf37f13 --- /dev/null +++ b/packages/coding-agent/test/syntax-highlight.test.ts @@ -0,0 +1,113 @@ +import { resetCapabilitiesCache, setCapabilities } from "@earendil-works/pi-tui"; +import { afterEach, beforeEach, describe, expect, it } from "vitest"; +import { highlightCode, initTheme } from "../src/modes/interactive/theme/theme.ts"; +import { + highlight, + loadAllHighlightLanguages, + renderHighlightedHtml, + supportsLanguage, +} from "../src/utils/syntax-highlight.ts"; + +const eagerLanguages = [ + "python", + "java", + "go", + "javascript", + "cpp", + "typescript", + "php", + "ruby", + "c", + "csharp", + "nix", + "bash", + "rust", + "scala", + "kotlin", + "swift", + "dart", + "groovy", + "perl", + "lua", +]; +const eagerLanguagesLoadedAtStartup = eagerLanguages.every(supportsLanguage); +const uncommonLanguageLoadedAtStartup = supportsLanguage("ada"); + +describe("syntax highlight renderer", () => { + it("loads the twenty most common languages at startup and defers the rest", async () => { + expect(eagerLanguagesLoadedAtStartup).toBe(true); + expect(uncommonLanguageLoadedAtStartup).toBe(false); + await loadAllHighlightLanguages(); + expect(supportsLanguage("ada")).toBe(true); + }); + + it("renders highlighted spans with the provided theme", () => { + const rendered = renderHighlightedHtml('const value', { + keyword: (text) => `[keyword:${text}]`, + }); + expect(rendered).toBe("[keyword:const] value"); + }); + + it("decodes HTML entities emitted by highlight.js", () => { + const rendered = renderHighlightedHtml("<tag attr="value">&#x41;A</tag>"); + expect(rendered).toBe('AA'); + }); + + it("inherits parent formatting for unmapped nested scopes", () => { + const interpolation = "$" + "{x}"; + const rendered = renderHighlightedHtml( + `a${interpolation}b`, + { + string: (text) => `[string:${text}]`, + }, + ); + expect(rendered).toBe(`[string:a][string:${interpolation}][string:b]`); + }); + + it("keeps parent formatting across unscoped nested spans", () => { + const rendered = renderHighlightedHtml('abc', { + string: (text) => `[string:${text}]`, + }); + expect(rendered).toBe("[string:a][string:b][string:c]"); + }); + + it("highlights code through highlight.js", () => { + expect(supportsLanguage("typescript")).toBe(true); + const rendered = highlight("const value = 1", { + language: "typescript", + ignoreIllegals: true, + theme: { + keyword: (text) => `[keyword:${text}]`, + number: (text) => `[number:${text}]`, + }, + }); + expect(rendered).toContain("[keyword:const]"); + expect(rendered).toContain("[number:1]"); + }); +}); + +describe("theme syntax highlighting", () => { + beforeEach(() => { + setCapabilities({ images: null, trueColor: true, hyperlinks: false }); + initTheme("dark"); + }); + + afterEach(() => { + resetCapabilitiesCache(); + }); + + it("colors diff additions and deletions in fenced diff blocks", () => { + const lines = highlightCode("-old\n+new\n", "diff"); + + expect(lines[0]).toBe("\x1b[38;2;204;102;102m-old\x1b[39m"); + expect(lines[1]).toBe("\x1b[38;2;181;189;104m+new\x1b[39m"); + }); + + it("keeps cli-highlight default styled scopes mapped to theme styles", () => { + expect(highlightCode("const re = /foo+/gi;", "javascript")[0]).toContain( + "\x1b[38;2;206;145;120m/foo+/gi\x1b[39m", + ); + expect(highlightCode("@decorator", "python")[0]).toBe("\x1b[38;2;128;128;128m@decorator\x1b[39m"); + expect(highlightCode("
    ", "html")[0]).toContain("\x1b[38;2;86;156;214mdiv\x1b[39m"); + }); +}); diff --git a/packages/coding-agent/test/system-prompt.test.ts b/packages/coding-agent/test/system-prompt.test.ts new file mode 100644 index 0000000000000000000000000000000000000000..9a943f1167ad236cc93dae202f2d663765b9b2a7 --- /dev/null +++ b/packages/coding-agent/test/system-prompt.test.ts @@ -0,0 +1,170 @@ +import { describe, expect, test } from "vitest"; +import type { Skill } from "../src/core/skills.ts"; +import { createSyntheticSourceInfo } from "../src/core/source-info.ts"; +import { buildSystemPrompt } from "../src/core/system-prompt.ts"; + +const testSkill: Skill = { + name: "test-skill", + description: "A test skill.", + filePath: "/skills/test-skill/SKILL.md", + baseDir: "/skills/test-skill", + sourceInfo: createSyntheticSourceInfo("/skills/test-skill/SKILL.md", { source: "test" }), + disableModelInvocation: false, +}; + +describe("buildSystemPrompt", () => { + describe("empty tools", () => { + test("shows (none) for empty tools list", () => { + const prompt = buildSystemPrompt({ + selectedTools: [], + contextFiles: [], + skills: [], + cwd: process.cwd(), + }); + + expect(prompt).toContain("Available tools:\n(none)"); + }); + + test("shows file paths guideline even with no tools", () => { + const prompt = buildSystemPrompt({ + selectedTools: [], + contextFiles: [], + skills: [], + cwd: process.cwd(), + }); + + expect(prompt).toContain("Show file paths clearly"); + }); + }); + + describe("default tools", () => { + test("includes all default tools when snippets are provided", () => { + const prompt = buildSystemPrompt({ + toolSnippets: { + read: "Read file contents", + bash: "Execute bash commands", + edit: "Make surgical edits", + write: "Create or overwrite files", + }, + contextFiles: [], + skills: [], + cwd: process.cwd(), + }); + + expect(prompt).toContain("- read:"); + expect(prompt).toContain("- bash:"); + expect(prompt).toContain("- edit:"); + expect(prompt).toContain("- write:"); + }); + + test.each([ + [["powershell"], "Use PowerShell for file operations"], + [["bash", "powershell"], "Use bash or PowerShell for file operations"], + ] as const)("uses shell-specific guidance for %j", (selectedTools, expected) => { + const prompt = buildSystemPrompt({ + selectedTools: [...selectedTools], + contextFiles: [], + skills: [], + cwd: process.cwd(), + }); + + expect(prompt).toContain(expected); + }); + + test("instructs models to resolve pi docs and examples under absolute base paths", () => { + const prompt = buildSystemPrompt({ + contextFiles: [], + skills: [], + cwd: process.cwd(), + }); + + expect(prompt).toContain( + "- When reading pi docs or examples, resolve docs/... under Additional docs and examples/... under Examples, not the current working directory", + ); + expect(prompt).toContain("environment variables (docs/environment-variables.md)"); + }); + }); + + describe("custom tool snippets", () => { + test("includes custom tools in available tools section when promptSnippet is provided", () => { + const prompt = buildSystemPrompt({ + selectedTools: ["read", "dynamic_tool"], + toolSnippets: { + dynamic_tool: "Run dynamic test behavior", + }, + contextFiles: [], + skills: [], + cwd: process.cwd(), + }); + + expect(prompt).toContain("- dynamic_tool: Run dynamic test behavior"); + }); + + test("omits custom tools from available tools section when promptSnippet is not provided", () => { + const prompt = buildSystemPrompt({ + selectedTools: ["read", "dynamic_tool"], + contextFiles: [], + skills: [], + cwd: process.cwd(), + }); + + expect(prompt).not.toContain("dynamic_tool"); + }); + }); + + describe("prompt guidelines", () => { + test("appends promptGuidelines to default guidelines", () => { + const prompt = buildSystemPrompt({ + selectedTools: ["read", "dynamic_tool"], + promptGuidelines: ["Use dynamic_tool for project summaries."], + contextFiles: [], + skills: [], + cwd: process.cwd(), + }); + + expect(prompt).toContain("- Use dynamic_tool for project summaries."); + }); + + test("deduplicates and trims promptGuidelines", () => { + const prompt = buildSystemPrompt({ + selectedTools: ["read", "dynamic_tool"], + promptGuidelines: ["Use dynamic_tool for summaries.", " Use dynamic_tool for summaries. ", " "], + contextFiles: [], + skills: [], + cwd: process.cwd(), + }); + + expect(prompt.match(/- Use dynamic_tool for summaries\./g)).toHaveLength(1); + }); + }); + + describe("skills", () => { + test.each([ + { name: "default prompt", customPrompt: undefined }, + { name: "custom prompt", customPrompt: "Custom system prompt" }, + ])("includes skills with only bash in the $name", ({ customPrompt }) => { + const prompt = buildSystemPrompt({ + customPrompt, + selectedTools: ["bash"], + contextFiles: [], + skills: [testSkill], + cwd: process.cwd(), + }); + + expect(prompt).toContain(""); + expect(prompt).toContain("test-skill"); + expect(prompt).toContain("Use bash to load a skill's file"); + }); + + test("omits skills without read or bash", () => { + const prompt = buildSystemPrompt({ + selectedTools: ["write"], + contextFiles: [], + skills: [testSkill], + cwd: process.cwd(), + }); + + expect(prompt).not.toContain(""); + }); + }); +}); diff --git a/packages/coding-agent/test/test-harness.test.ts b/packages/coding-agent/test/test-harness.test.ts new file mode 100644 index 0000000000000000000000000000000000000000..5f1ad41df640d20c70bb3398e556fd6994832053 --- /dev/null +++ b/packages/coding-agent/test/test-harness.test.ts @@ -0,0 +1,335 @@ +/** + * Tests for the test harness itself. + * Validates that the faux provider and session factory work correctly. + */ + +import type { AgentTool } from "@earendil-works/pi-agent-core"; +import type { AssistantMessage } from "@earendil-works/pi-ai"; +import { Type } from "typebox"; +import { afterEach, describe, expect, it } from "vitest"; +import { createHarness, createHarnessWithExtensions, type Harness } from "./test-harness.ts"; + +describe("test harness", () => { + let harness: Harness; + + afterEach(() => { + harness?.cleanup(); + }); + + it("simple text response", async () => { + harness = await createHarness({ responses: ["hello world"] }); + + await harness.session.prompt("hi"); + + expect(harness.faux.callCount).toBe(1); + + const assistantMessages = harness.session.messages.filter((m) => m.role === "assistant"); + expect(assistantMessages).toHaveLength(1); + + const msg = assistantMessages[0] as AssistantMessage; + expect(msg.content).toEqual([{ type: "text", text: "hello world" }]); + expect(msg.stopReason).toBe("stop"); + }); + + it("response sequence", async () => { + harness = await createHarness({ responses: ["first", "second", "third"] }); + + await harness.session.prompt("a"); + await harness.session.prompt("b"); + await harness.session.prompt("c"); + + expect(harness.faux.callCount).toBe(3); + + const assistantTexts = harness.session.messages + .filter((m): m is AssistantMessage => m.role === "assistant") + .map((m) => m.content.find((c) => c.type === "text")?.text); + + expect(assistantTexts).toEqual(["first", "second", "third"]); + }); + + it("tool call response triggers tool execution", async () => { + let toolExecuted = false; + const echoTool: AgentTool = { + name: "echo", + label: "Echo", + description: "Echo back", + parameters: Type.Object({ text: Type.String() }), + execute: async () => { + toolExecuted = true; + return { content: [{ type: "text", text: "echoed" }], details: {} }; + }, + }; + + harness = await createHarness({ + responses: [{ toolCalls: [{ name: "echo", args: { text: "hi" } }] }, "done after tool"], + tools: [echoTool], + baseToolsOverride: { echo: echoTool }, + }); + + await harness.session.prompt("use the tool"); + + expect(toolExecuted).toBe(true); + expect(harness.faux.callCount).toBe(2); + + const toolResults = harness.session.messages.filter((m) => m.role === "toolResult"); + expect(toolResults).toHaveLength(1); + }); + + it("error response", async () => { + harness = await createHarness({ + responses: [{ error: "something broke" }], + }); + + await harness.session.prompt("hi"); + + const assistantMessages = harness.session.messages.filter((m): m is AssistantMessage => m.role === "assistant"); + expect(assistantMessages).toHaveLength(1); + expect(assistantMessages[0].stopReason).toBe("error"); + expect(assistantMessages[0].errorMessage).toBe("something broke"); + }); + + it("turns a pending terminal response into an error", async () => { + harness = await createHarness({ + responses: [{ text: "partial", stopReason: "pending" }], + settings: { retry: { enabled: false } }, + }); + + await harness.session.prompt("hi"); + + const assistantMessages = harness.session.messages.filter((m): m is AssistantMessage => m.role === "assistant"); + expect(assistantMessages).toHaveLength(1); + expect(assistantMessages[0].stopReason).toBe("error"); + expect(assistantMessages[0].errorMessage).toBe("Faux response ended without a stop reason"); + }); + + it("retry on transient error", async () => { + harness = await createHarness({ + responses: [{ error: "overloaded_error" }, "recovered"], + settings: { retry: { enabled: true, maxRetries: 3, baseDelayMs: 1 } }, + }); + + await harness.session.prompt("hi"); + + expect(harness.faux.callCount).toBe(2); + + const retryStarts = harness.eventsOfType("auto_retry_start"); + expect(retryStarts).toHaveLength(1); + + const retryEnds = harness.eventsOfType("auto_retry_end"); + expect(retryEnds).toHaveLength(1); + expect(retryEnds[0].success).toBe(true); + }); + + it("custom usage numbers", async () => { + harness = await createHarness({ + responses: [{ text: "big response", usage: { input: 100000, output: 5000 } }], + }); + + await harness.session.prompt("hi"); + + const msg = harness.session.messages.find((m): m is AssistantMessage => m.role === "assistant")!; + expect(msg.usage.input).toBe(100000); + expect(msg.usage.output).toBe(5000); + }); + + it("event capture", async () => { + harness = await createHarness({ responses: ["hello"] }); + + await harness.session.prompt("hi"); + + const agentStarts = harness.eventsOfType("agent_start"); + expect(agentStarts).toHaveLength(1); + + const agentEnds = harness.eventsOfType("agent_end"); + expect(agentEnds).toHaveLength(1); + + const messageEnds = harness.eventsOfType("message_end"); + expect(messageEnds.length).toBeGreaterThanOrEqual(2); // user + assistant + }); + + it("context capture", async () => { + harness = await createHarness({ responses: ["reply"] }); + + await harness.session.prompt("my question"); + + expect(harness.faux.contexts).toHaveLength(1); + const ctx = harness.faux.contexts[0]; + const userMsg = ctx.messages.find((m) => m.role === "user"); + expect(userMsg).toBeDefined(); + }); + + it("wraps around when more calls than responses", async () => { + harness = await createHarness({ responses: ["a", "b"] }); + + await harness.session.prompt("1"); + await harness.session.prompt("2"); + await harness.session.prompt("3"); + + expect(harness.faux.callCount).toBe(3); + + const texts = harness.session.messages + .filter((m): m is AssistantMessage => m.role === "assistant") + .map((m) => m.content.find((c) => c.type === "text")?.text); + + expect(texts).toEqual(["a", "b", "a"]); + }); + + it("streams text deltas", async () => { + harness = await createHarness({ responses: ["hello world"] }); + + await harness.session.prompt("hi"); + + const updates = harness.eventsOfType("message_update"); + const textDeltas = updates.filter((e) => e.assistantMessageEvent.type === "text_delta"); + expect(textDeltas.length).toBeGreaterThan(0); + + // Deltas should reconstruct the full text + const reconstructed = textDeltas.map((e) => (e.assistantMessageEvent as { delta: string }).delta).join(""); + expect(reconstructed).toBe("hello world"); + }); + + it("streams thinking deltas", async () => { + harness = await createHarness({ + responses: [{ thinking: "let me think about this", text: "answer" }], + }); + + await harness.session.prompt("hi"); + + const updates = harness.eventsOfType("message_update"); + const thinkingStarts = updates.filter((e) => e.assistantMessageEvent.type === "thinking_start"); + const thinkingDeltas = updates.filter((e) => e.assistantMessageEvent.type === "thinking_delta"); + const thinkingEnds = updates.filter((e) => e.assistantMessageEvent.type === "thinking_end"); + + expect(thinkingStarts).toHaveLength(1); + expect(thinkingDeltas.length).toBeGreaterThan(0); + expect(thinkingEnds).toHaveLength(1); + + const reconstructed = thinkingDeltas.map((e) => (e.assistantMessageEvent as { delta: string }).delta).join(""); + expect(reconstructed).toBe("let me think about this"); + }); + + it("streams tool call deltas", async () => { + const echoTool: AgentTool = { + name: "echo", + label: "Echo", + description: "Echo back", + parameters: Type.Object({ text: Type.String() }), + execute: async () => ({ content: [{ type: "text", text: "echoed" }], details: {} }), + }; + + harness = await createHarness({ + responses: [{ toolCalls: [{ name: "echo", args: { text: "hi" } }] }, "done"], + tools: [echoTool], + baseToolsOverride: { echo: echoTool }, + }); + + await harness.session.prompt("use tool"); + + const updates = harness.eventsOfType("message_update"); + const toolcallStarts = updates.filter((e) => e.assistantMessageEvent.type === "toolcall_start"); + const toolcallDeltas = updates.filter((e) => e.assistantMessageEvent.type === "toolcall_delta"); + const toolcallEnds = updates.filter((e) => e.assistantMessageEvent.type === "toolcall_end"); + + expect(toolcallStarts).toHaveLength(1); + expect(toolcallDeltas.length).toBeGreaterThan(0); + expect(toolcallEnds).toHaveLength(1); + }); + + it("streams thinking then text then tool call in order", async () => { + const echoTool: AgentTool = { + name: "echo", + label: "Echo", + description: "Echo back", + parameters: Type.Object({ text: Type.String() }), + execute: async () => ({ content: [{ type: "text", text: "echoed" }], details: {} }), + }; + + harness = await createHarness({ + responses: [ + { + thinking: "hmm", + text: "I will call a tool", + toolCalls: [{ name: "echo", args: { text: "x" } }], + }, + "final", + ], + tools: [echoTool], + baseToolsOverride: { echo: echoTool }, + }); + + await harness.session.prompt("do it"); + + const updates = harness.eventsOfType("message_update"); + const streamTypes = updates.map((e) => e.assistantMessageEvent.type); + + // Thinking events should come before text events, text before toolcall + const firstThinking = streamTypes.indexOf("thinking_start"); + const firstText = streamTypes.indexOf("text_start"); + const firstToolcall = streamTypes.indexOf("toolcall_start"); + + expect(firstThinking).toBeLessThan(firstText); + expect(firstText).toBeLessThan(firstToolcall); + }); + + it("loads inline extension factories and disambiguates duplicate commands", async () => { + const calls: string[] = []; + + harness = await createHarnessWithExtensions({ + extensionFactories: [ + { + path: "", + factory: (pi) => { + pi.registerCommand("shared-cmd", { + description: "Alpha command", + handler: async (args) => { + calls.push(`alpha:${args}`); + }, + }); + }, + }, + { + path: "", + factory: (pi) => { + pi.registerCommand("shared-cmd", { + description: "Beta command", + handler: async (args) => { + calls.push(`beta:${args}`); + }, + }); + }, + }, + ], + }); + + const runner = harness.session.extensionRunner; + expect(runner).toBeDefined(); + + const commands = runner!.getRegisteredCommands(); + expect( + commands.map((command) => ({ + name: command.name, + invocationName: command.invocationName, + description: command.description, + path: command.sourceInfo.path, + })), + ).toEqual([ + { name: "shared-cmd", invocationName: "shared-cmd:1", description: "Alpha command", path: "" }, + { name: "shared-cmd", invocationName: "shared-cmd:2", description: "Beta command", path: "" }, + ]); + + await runner!.getCommand("shared-cmd:1")?.handler("first", runner!.createCommandContext()); + await runner!.getCommand("shared-cmd:2")?.handler("second", runner!.createCommandContext()); + + expect(calls).toEqual(["alpha:first", "beta:second"]); + }); + + it("session persistence works", async () => { + harness = await createHarness({ responses: ["persisted"] }); + + await harness.session.prompt("hi"); + + const entries = harness.sessionManager.getEntries(); + const messageEntries = entries.filter((e) => e.type === "message"); + expect(messageEntries.length).toBeGreaterThanOrEqual(2); // user + assistant + }); +}); diff --git a/packages/coding-agent/test/test-harness.ts b/packages/coding-agent/test/test-harness.ts new file mode 100644 index 0000000000000000000000000000000000000000..c4f06ee8e820982e9538e3ec9dc129909d54fc18 --- /dev/null +++ b/packages/coding-agent/test/test-harness.ts @@ -0,0 +1,471 @@ +import { createInMemoryModelRegistry, getModelRuntime } from "./model-runtime-test-utils.ts"; +/** + * Test harness for AgentSession runtime testing. + * + * Provides: + * - A faux stream function with declarative response sequencing + * - A one-call factory for a fully wired AgentSession with real in-memory dependencies + * - Event capture for assertions + */ + +import { existsSync, mkdirSync, rmSync } from "node:fs"; +import { tmpdir } from "node:os"; +import { join } from "node:path"; +import type { AgentTool } from "@earendil-works/pi-agent-core"; +import { Agent } from "@earendil-works/pi-agent-core"; +import type { + AssistantMessage, + AssistantMessageEvent, + AssistantMessageEventStream, + Context, + Model, + SimpleStreamOptions, + StopReason, + TextContent, + ThinkingContent, + ToolCall, + Usage, +} from "@earendil-works/pi-ai"; +import { createAssistantMessageEventStream } from "@earendil-works/pi-ai"; +import { AgentSession, type AgentSessionEvent } from "../src/core/agent-session.ts"; +import { AuthStorage } from "../src/core/auth-storage.ts"; +import { SessionManager } from "../src/core/session-manager.ts"; +import type { Settings } from "../src/core/settings-manager.ts"; +import { SettingsManager } from "../src/core/settings-manager.ts"; +import type { InlineExtension, ResourceLoader } from "../src/index.ts"; +import { + type CreateTestExtensionsResultInput, + createTestExtensionsResult, + createTestResourceLoader, +} from "./utilities.ts"; + +// ============================================================================ +// Faux model +// ============================================================================ + +const FAUX_PROVIDER = "faux"; +const FAUX_MODEL_ID = "faux-1"; +const FAUX_API = "anthropic-messages" as const; + +export const fauxModel: Model = { + id: FAUX_MODEL_ID, + name: "Faux Model", + api: FAUX_API, + provider: FAUX_PROVIDER, + baseUrl: "http://localhost:0", + reasoning: false, + input: ["text", "image"], + cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 }, + contextWindow: 128000, + maxTokens: 16384, +}; + +// ============================================================================ +// Response description +// ============================================================================ + +export interface FauxResponse { + /** Text content blocks. String shorthand becomes a single text block. */ + text?: string; + /** Tool calls to include in the response. */ + toolCalls?: Array<{ id?: string; name: string; args: Record }>; + /** Thinking content. */ + thinking?: string; + /** Stop reason. Defaults to "stop", or "toolUse" if toolCalls are present, or "error" if error is set. */ + stopReason?: StopReason; + /** Error message. Sets stopReason to "error" if not explicitly set. */ + error?: string; + /** Usage numbers. Merged with defaults (input: 100, output: 50). */ + usage?: Partial; + /** Delay in ms before the response starts. */ + delayMs?: number; + /** Model overrides (provider, model id) for responses that should look like they came from a different model. */ + model?: { provider?: string; id?: string }; +} + +/** Shorthand: a string becomes a simple text response. */ +export type FauxResponseInput = FauxResponse | string; + +// ============================================================================ +// Faux stream function +// ============================================================================ + +function normalizeResponse(input: FauxResponseInput): FauxResponse { + if (typeof input === "string") { + return { text: input }; + } + return input; +} + +function buildUsage(partial?: Partial): Usage { + const input = partial?.input ?? 100; + const output = partial?.output ?? 50; + const cacheRead = partial?.cacheRead ?? 0; + const cacheWrite = partial?.cacheWrite ?? 0; + return { + input, + output, + cacheRead, + cacheWrite, + totalTokens: partial?.totalTokens ?? input + output + cacheRead + cacheWrite, + cost: partial?.cost ?? { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 }, + }; +} + +let toolCallIdCounter = 0; + +function buildAssistantMessage(resp: FauxResponse): AssistantMessage { + const content: (TextContent | ThinkingContent | ToolCall)[] = []; + + if (resp.thinking) { + content.push({ type: "thinking", thinking: resp.thinking }); + } + if (resp.text !== undefined) { + content.push({ type: "text", text: resp.text }); + } + if (resp.toolCalls) { + for (const tc of resp.toolCalls) { + content.push({ + type: "toolCall", + id: tc.id ?? `faux_tc_${++toolCallIdCounter}`, + name: tc.name, + arguments: tc.args, + }); + } + } + + // If no content was added at all, add empty text + if (content.length === 0 && !resp.error) { + content.push({ type: "text", text: "" }); + } + + let stopReason: StopReason; + if (resp.stopReason) { + stopReason = resp.stopReason; + } else if (resp.error) { + stopReason = "error"; + } else if (resp.toolCalls && resp.toolCalls.length > 0) { + stopReason = "toolUse"; + } else { + stopReason = "stop"; + } + + return { + role: "assistant", + content, + api: FAUX_API, + provider: resp.model?.provider ?? FAUX_PROVIDER, + model: resp.model?.id ?? FAUX_MODEL_ID, + usage: buildUsage(resp.usage), + stopReason, + errorMessage: resp.error, + timestamp: Date.now(), + }; +} + +// ============================================================================ +// Token-level streaming +// ============================================================================ + +/** Split a string into chunks of varying size (3-5 chars) for simulating token-by-token streaming. */ +function chunkString(text: string): string[] { + const chunks: string[] = []; + let i = 0; + while (i < text.length) { + const size = 3 + Math.floor(Math.random() * 3); // 3, 4, or 5 + chunks.push(text.slice(i, i + size)); + i += size; + } + return chunks.length > 0 ? chunks : [""]; +} + +/** + * Stream a complete AssistantMessage through an EventStream with realistic + * intermediate delta events for each content block. + */ +function streamWithDeltas(stream: AssistantMessageEventStream, message: AssistantMessage): void { + // Build partial progressively as we stream content blocks + const partial: AssistantMessage = { ...message, content: [], stopReason: "pending" }; + stream.push({ type: "start", partial: { ...partial } }); + + for (let i = 0; i < message.content.length; i++) { + const block = message.content[i]; + + if (block.type === "thinking") { + partial.content = [...partial.content, { type: "thinking", thinking: "" }]; + stream.push({ type: "thinking_start", contentIndex: i, partial: { ...partial } }); + + for (const chunk of chunkString(block.thinking)) { + (partial.content[i] as ThinkingContent).thinking += chunk; + stream.push(makeEvent("thinking_delta", i, chunk, partial)); + } + + stream.push({ + type: "thinking_end", + contentIndex: i, + content: block.thinking, + partial: { ...partial }, + }); + } else if (block.type === "text") { + partial.content = [...partial.content, { type: "text", text: "" }]; + stream.push({ type: "text_start", contentIndex: i, partial: { ...partial } }); + + for (const chunk of chunkString(block.text)) { + (partial.content[i] as TextContent).text += chunk; + stream.push(makeEvent("text_delta", i, chunk, partial)); + } + + stream.push({ + type: "text_end", + contentIndex: i, + content: block.text, + partial: { ...partial }, + }); + } else if (block.type === "toolCall") { + const argsJson = JSON.stringify(block.arguments); + partial.content = [...partial.content, { type: "toolCall", id: block.id, name: block.name, arguments: {} }]; + stream.push({ type: "toolcall_start", contentIndex: i, partial: { ...partial } }); + + for (const chunk of chunkString(argsJson)) { + stream.push(makeEvent("toolcall_delta", i, chunk, partial)); + } + + // Final toolcall has the real parsed arguments + (partial.content[i] as ToolCall).arguments = block.arguments; + stream.push({ + type: "toolcall_end", + contentIndex: i, + toolCall: block, + partial: { ...partial }, + }); + } + } + + if (message.stopReason === "pending") { + const error: AssistantMessage = { + ...message, + stopReason: "error", + errorMessage: "Faux response ended without a stop reason", + }; + stream.push({ type: "error", reason: "error", error }); + return; + } + if (message.stopReason === "error" || message.stopReason === "aborted") { + stream.push({ type: "error", reason: message.stopReason, error: message }); + return; + } + stream.push({ type: "done", reason: message.stopReason, message }); +} + +function makeEvent( + type: "text_delta" | "thinking_delta" | "toolcall_delta", + contentIndex: number, + delta: string, + partial: AssistantMessage, +): AssistantMessageEvent { + return { type, contentIndex, delta, partial: { ...partial } }; +} + +// ============================================================================ +// Stream function factory +// ============================================================================ + +export interface FauxStreamFnState { + /** Number of times the stream function has been called. */ + callCount: number; + /** The context passed to each call, in order. */ + contexts: Context[]; +} + +/** + * Create a faux stream function from a sequence of response descriptions. + * + * The function cycles through responses in order. If more calls are made than + * responses provided, it wraps around. + * + * Returns the stream function and a state object for inspection. + */ +export function createFauxStreamFn(responses: FauxResponseInput[]): { + streamFn: (model: Model, context: Context, options?: SimpleStreamOptions) => AssistantMessageEventStream; + state: FauxStreamFnState; +} { + if (responses.length === 0) { + throw new Error("createFauxStreamFn requires at least one response"); + } + + const state: FauxStreamFnState = { callCount: 0, contexts: [] }; + + const streamFn = (_model: Model, context: Context, _options?: SimpleStreamOptions) => { + const index = state.callCount % responses.length; + state.callCount++; + state.contexts.push(context); + + const resp = normalizeResponse(responses[index]); + const message = buildAssistantMessage(resp); + const stream = createAssistantMessageEventStream(); + + const emit = () => { + streamWithDeltas(stream, message); + }; + + if (resp.delayMs && resp.delayMs > 0) { + setTimeout(emit, resp.delayMs); + } else { + queueMicrotask(emit); + } + + return stream; + }; + + return { streamFn, state }; +} + +// ============================================================================ +// Session harness +// ============================================================================ + +export interface HarnessOptions { + /** Response sequence for the faux provider. Default: single "ok" response. */ + responses?: FauxResponseInput[]; + /** Model to use. Default: fauxModel. */ + model?: Model; + /** Context window override (applied to the model). */ + contextWindow?: number; + /** Settings overrides (retry, compaction, etc.). */ + settings?: Partial; + /** System prompt. Default: "You are a test assistant." */ + systemPrompt?: string; + /** Custom tools to register on the agent. */ + tools?: AgentTool[]; + /** Base tools override (replaces built-in read/bash/edit/write). */ + baseToolsOverride?: Record; + /** Optional resource loader override. */ + resourceLoader?: ResourceLoader; + /** Inline extensions to load into the session resource loader. */ + extensionFactories?: Array; +} + +export interface Harness { + session: AgentSession; + agent: Agent; + sessionManager: SessionManager; + settingsManager: SettingsManager; + /** Faux stream function state (call count, captured contexts). */ + faux: FauxStreamFnState; + /** All events emitted by the session, in order. */ + events: AgentSessionEvent[]; + /** Filter captured events by type. */ + eventsOfType(type: T): Extract[]; + /** Temp directory (cleaned up by cleanup()). */ + tempDir: string; + /** Dispose session and remove temp directory. */ + cleanup: () => void; +} + +function createTempDir(): string { + const tempDir = join(tmpdir(), `pi-harness-${Date.now()}-${Math.random().toString(36).slice(2)}`); + mkdirSync(tempDir, { recursive: true }); + return tempDir; +} + +async function createHarnessWithResourceLoader( + options: HarnessOptions, + resourceLoader: ResourceLoader, + tempDir: string, +): Promise { + const baseModel = options.model ?? fauxModel; + const model: Model = options.contextWindow ? { ...baseModel, contextWindow: options.contextWindow } : baseModel; + + const { streamFn, state: fauxState } = createFauxStreamFn(options.responses ?? ["ok"]); + + const agent = new Agent({ + getApiKey: () => "faux-key", + initialState: { + model, + systemPrompt: options.systemPrompt ?? "You are a test assistant.", + tools: options.tools ?? [], + }, + streamFn: streamFn, + }); + + const sessionManager = SessionManager.inMemory(); + const settingsManager = SettingsManager.create(tempDir, tempDir); + + if (options.settings) { + settingsManager.applyOverrides(options.settings); + } + + const authStorage = AuthStorage.inMemory({ + [model.provider]: { type: "api_key", key: "faux-key" }, + }); + const modelRegistry = await createInMemoryModelRegistry(authStorage); + modelRegistry.registerProvider(model.provider, { + baseUrl: model.baseUrl, + api: model.api, + models: [ + { + id: model.id, + name: model.name, + api: model.api, + reasoning: model.reasoning, + input: model.input, + cost: model.cost, + contextWindow: model.contextWindow, + maxTokens: model.maxTokens, + baseUrl: model.baseUrl, + }, + ], + }); + + const session = new AgentSession({ + agent, + sessionManager, + settingsManager, + cwd: tempDir, + modelRuntime: getModelRuntime(modelRegistry), + resourceLoader, + baseToolsOverride: options.baseToolsOverride, + }); + + const events: AgentSessionEvent[] = []; + session.subscribe((event) => { + events.push(event); + }); + + const cleanup = () => { + session.dispose(); + if (existsSync(tempDir)) { + rmSync(tempDir, { recursive: true }); + } + }; + + return { + session, + agent, + sessionManager, + settingsManager, + faux: fauxState, + events, + eventsOfType(type: T) { + return events.filter((e): e is Extract => e.type === type); + }, + tempDir, + cleanup, + }; +} + +export async function createHarness(options: HarnessOptions = {}): Promise { + if (options.extensionFactories?.length) { + throw new Error("createHarness does not support extensionFactories. Use createHarnessWithExtensions()."); + } + + const tempDir = createTempDir(); + return await createHarnessWithResourceLoader(options, options.resourceLoader ?? createTestResourceLoader(), tempDir); +} + +export async function createHarnessWithExtensions(options: HarnessOptions = {}): Promise { + const tempDir = createTempDir(); + const extensionsResult = await createTestExtensionsResult(options.extensionFactories ?? [], tempDir); + const resourceLoader = options.resourceLoader ?? createTestResourceLoader({ extensionsResult }); + return await createHarnessWithResourceLoader(options, resourceLoader, tempDir); +} diff --git a/packages/coding-agent/test/test-network-env.ts b/packages/coding-agent/test/test-network-env.ts new file mode 100644 index 0000000000000000000000000000000000000000..35515f34521bfb4b8429b385f778c3e99e904d75 --- /dev/null +++ b/packages/coding-agent/test/test-network-env.ts @@ -0,0 +1,6 @@ +import { vi } from "vitest"; + +/** Enable network code paths for tests that replace external I/O with local fixtures or mocks. */ +export function allowNetwork(): void { + vi.stubEnv("PI_OFFLINE", undefined); +} diff --git a/packages/coding-agent/test/test-theme-colors.ts b/packages/coding-agent/test/test-theme-colors.ts new file mode 100644 index 0000000000000000000000000000000000000000..4e7181f3558ceae9fc807d40b9f3a602f75da3d7 --- /dev/null +++ b/packages/coding-agent/test/test-theme-colors.ts @@ -0,0 +1,249 @@ +import fs from "fs"; +import { initTheme, theme } from "../src/modes/interactive/theme/theme.ts"; + +// --- Color utilities --- + +function hexToRgb(hex: string): [number, number, number] { + const result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex); + return result ? [parseInt(result[1], 16), parseInt(result[2], 16), parseInt(result[3], 16)] : [0, 0, 0]; +} + +function rgbToHex(r: number, g: number, b: number): string { + return ( + "#" + + [r, g, b] + .map((x) => + Math.round(Math.max(0, Math.min(255, x))) + .toString(16) + .padStart(2, "0"), + ) + .join("") + ); +} + +function rgbToHsl(r: number, g: number, b: number): [number, number, number] { + r /= 255; + g /= 255; + b /= 255; + const max = Math.max(r, g, b), + min = Math.min(r, g, b); + let h = 0, + s = 0; + const l = (max + min) / 2; + if (max !== min) { + const d = max - min; + s = l > 0.5 ? d / (2 - max - min) : d / (max + min); + switch (max) { + case r: + h = ((g - b) / d + (g < b ? 6 : 0)) / 6; + break; + case g: + h = ((b - r) / d + 2) / 6; + break; + case b: + h = ((r - g) / d + 4) / 6; + break; + } + } + return [h, s, l]; +} + +function hslToRgb(h: number, s: number, l: number): [number, number, number] { + let r: number, g: number, b: number; + if (s === 0) { + r = g = b = l; + } else { + const hue2rgb = (p: number, q: number, t: number) => { + if (t < 0) t += 1; + if (t > 1) t -= 1; + if (t < 1 / 6) return p + (q - p) * 6 * t; + if (t < 1 / 2) return q; + if (t < 2 / 3) return p + (q - p) * (2 / 3 - t) * 6; + return p; + }; + const q = l < 0.5 ? l * (1 + s) : l + s - l * s; + const p = 2 * l - q; + r = hue2rgb(p, q, h + 1 / 3); + g = hue2rgb(p, q, h); + b = hue2rgb(p, q, h - 1 / 3); + } + return [Math.round(r * 255), Math.round(g * 255), Math.round(b * 255)]; +} + +function getLuminance(r: number, g: number, b: number): number { + const lin = (c: number) => { + c = c / 255; + return c <= 0.03928 ? c / 12.92 : ((c + 0.055) / 1.055) ** 2.4; + }; + return 0.2126 * lin(r) + 0.7152 * lin(g) + 0.0722 * lin(b); +} + +function getContrast(rgb: [number, number, number], bgLum: number): number { + const fgLum = getLuminance(...rgb); + const lighter = Math.max(fgLum, bgLum); + const darker = Math.min(fgLum, bgLum); + return (lighter + 0.05) / (darker + 0.05); +} + +function adjustColorToContrast(hex: string, targetContrast: number, againstWhite: boolean): string { + const rgb = hexToRgb(hex); + const [h, s] = rgbToHsl(...rgb); + const bgLum = againstWhite ? 1.0 : 0.0; + + let lo = againstWhite ? 0 : 0.5; + let hi = againstWhite ? 0.5 : 1.0; + + for (let i = 0; i < 50; i++) { + const mid = (lo + hi) / 2; + const testRgb = hslToRgb(h, s, mid); + const contrast = getContrast(testRgb, bgLum); + + if (againstWhite) { + if (contrast < targetContrast) hi = mid; + else lo = mid; + } else { + if (contrast < targetContrast) lo = mid; + else hi = mid; + } + } + + const finalL = againstWhite ? lo : hi; + return rgbToHex(...hslToRgb(h, s, finalL)); +} + +function fgAnsi(hex: string): string { + const rgb = hexToRgb(hex); + return `\x1b[38;2;${rgb[0]};${rgb[1]};${rgb[2]}m`; +} + +const reset = "\x1b[0m"; + +// --- Commands --- + +function cmdContrast(targetContrast: number): void { + const baseColors = { + teal: "#5f8787", + blue: "#5f87af", + green: "#87af87", + yellow: "#d7af5f", + red: "#af5f5f", + }; + + console.log(`\n=== Colors adjusted to ${targetContrast}:1 contrast ===\n`); + + console.log("For LIGHT theme (vs white):"); + for (const [name, hex] of Object.entries(baseColors)) { + const adjusted = adjustColorToContrast(hex, targetContrast, true); + const rgb = hexToRgb(adjusted); + const contrast = getContrast(rgb, 1.0); + console.log(` ${name.padEnd(8)} ${fgAnsi(adjusted)}Sample${reset} ${adjusted} (${contrast.toFixed(2)}:1)`); + } + + console.log("\nFor DARK theme (vs black):"); + for (const [name, hex] of Object.entries(baseColors)) { + const adjusted = adjustColorToContrast(hex, targetContrast, false); + const rgb = hexToRgb(adjusted); + const contrast = getContrast(rgb, 0.0); + console.log(` ${name.padEnd(8)} ${fgAnsi(adjusted)}Sample${reset} ${adjusted} (${contrast.toFixed(2)}:1)`); + } +} + +function cmdTest(filePath: string): void { + if (!fs.existsSync(filePath)) { + console.error(`File not found: ${filePath}`); + process.exit(1); + } + + const data = JSON.parse(fs.readFileSync(filePath, "utf-8")); + const vars = data.vars || data; + + console.log(`\n=== Testing ${filePath} ===\n`); + + for (const [name, hex] of Object.entries(vars as Record)) { + if (!hex.startsWith("#")) continue; + const rgb = hexToRgb(hex); + const vsWhite = getContrast(rgb, 1.0); + const vsBlack = getContrast(rgb, 0.0); + const passW = vsWhite >= 4.5 ? "AA" : vsWhite >= 3.0 ? "AA-lg" : "FAIL"; + const passB = vsBlack >= 4.5 ? "AA" : vsBlack >= 3.0 ? "AA-lg" : "FAIL"; + console.log( + `${name.padEnd(14)} ${fgAnsi(hex)}Sample text${reset} ${hex} white: ${vsWhite.toFixed(2)}:1 ${passW.padEnd(5)} black: ${vsBlack.toFixed(2)}:1 ${passB}`, + ); + } +} + +function cmdTheme(themeName: string): void { + process.env.COLORTERM = "truecolor"; + initTheme(themeName); + + const parseAnsiRgb = (ansi: string): [number, number, number] | null => { + const match = ansi.match(/38;2;(\d+);(\d+);(\d+)/); + return match ? [parseInt(match[1], 10), parseInt(match[2], 10), parseInt(match[3], 10)] : null; + }; + + const getContrastVsWhite = (colorName: string): string => { + const ansi = theme.getFgAnsi(colorName as Parameters[0]); + const rgb = parseAnsiRgb(ansi); + if (!rgb) return "(default)"; + const ratio = getContrast(rgb, 1.0); + const pass = ratio >= 4.5 ? "AA" : ratio >= 3.0 ? "AA-lg" : "FAIL"; + return `${ratio.toFixed(2)}:1 ${pass}`; + }; + + const getContrastVsBlack = (colorName: string): string => { + const ansi = theme.getFgAnsi(colorName as Parameters[0]); + const rgb = parseAnsiRgb(ansi); + if (!rgb) return "(default)"; + const ratio = getContrast(rgb, 0.0); + const pass = ratio >= 4.5 ? "AA" : ratio >= 3.0 ? "AA-lg" : "FAIL"; + return `${ratio.toFixed(2)}:1 ${pass}`; + }; + + const logColor = (name: string): void => { + const sample = theme.fg(name as Parameters[0], "Sample text"); + const cw = getContrastVsWhite(name); + const cb = getContrastVsBlack(name); + console.log(`${name.padEnd(20)} ${sample} white: ${cw.padEnd(12)} black: ${cb}`); + }; + + console.log(`\n=== ${themeName} theme (WCAG AA = 4.5:1) ===`); + + console.log("\n--- Core UI ---"); + ["accent", "border", "borderAccent", "borderMuted", "success", "error", "warning", "muted", "dim"].forEach(logColor); + + console.log("\n--- Markdown ---"); + ["mdHeading", "mdLink", "mdCode", "mdCodeBlock", "mdCodeBlockBorder", "mdQuote", "mdListBullet"].forEach(logColor); + + console.log("\n--- Diff ---"); + ["toolDiffAdded", "toolDiffRemoved", "toolDiffContext"].forEach(logColor); + + console.log("\n--- Thinking ---"); + ["thinkingOff", "thinkingMinimal", "thinkingLow", "thinkingMedium", "thinkingHigh"].forEach(logColor); + + console.log("\n--- Backgrounds ---"); + console.log("userMessageBg:", theme.bg("userMessageBg", " Sample ")); + const searchMatch = theme.bg("searchMatchBg", theme.fg("searchMatchText", " Sample ")); + console.log("searchMatch:", theme.underline(searchMatch)); + console.log("searchCurrentMatch:", theme.bold(theme.inverse(searchMatch))); + console.log("toolPendingBg:", theme.bg("toolPendingBg", " Sample ")); + console.log("toolSuccessBg:", theme.bg("toolSuccessBg", " Sample ")); + console.log("toolErrorBg:", theme.bg("toolErrorBg", " Sample ")); + console.log(); +} + +// --- Main --- + +const [cmd, arg] = process.argv.slice(2); + +if (cmd === "contrast") { + cmdContrast(parseFloat(arg) || 4.5); +} else if (cmd === "test") { + cmdTest(arg); +} else if (cmd === "light" || cmd === "dark") { + cmdTheme(cmd); +} else { + console.log("Usage:"); + console.log(" npx tsx test-theme-colors.ts light|dark Test built-in theme"); + console.log(" npx tsx test-theme-colors.ts contrast 4.5 Compute colors at ratio"); + console.log(" npx tsx test-theme-colors.ts test file.json Test any JSON file"); +} diff --git a/packages/coding-agent/test/theme-controller.test.ts b/packages/coding-agent/test/theme-controller.test.ts new file mode 100644 index 0000000000000000000000000000000000000000..16db1fbab87a2bae3cf74010c8b773bc0ec1060a --- /dev/null +++ b/packages/coding-agent/test/theme-controller.test.ts @@ -0,0 +1,141 @@ +import type { TUI } from "@earendil-works/pi-tui"; +import { afterEach, describe, expect, it, vi } from "vitest"; +import { SettingsManager } from "../src/core/settings-manager.ts"; +import { initTheme, type TerminalTheme, theme } from "../src/modes/interactive/theme/theme.ts"; +import { InteractiveThemeController } from "../src/modes/interactive/theme/theme-controller.ts"; + +function createUi() { + const queryTerminalBackgroundColor = vi.fn(); + const queryTerminalColorScheme = vi.fn(); + const setTerminalColorSchemeNotifications = vi.fn(); + let terminalColorSchemeListener: ((terminalTheme: TerminalTheme) => void) | undefined; + const unsubscribeTerminalColorScheme = vi.fn(); + const ui = { + invalidate: vi.fn(), + requestRender: vi.fn(), + setTerminalColorSchemeNotifications, + onTerminalColorSchemeChange: vi.fn((listener: (terminalTheme: TerminalTheme) => void) => { + terminalColorSchemeListener = listener; + return unsubscribeTerminalColorScheme; + }), + queryTerminalBackgroundColor, + queryTerminalColorScheme, + } as unknown as TUI; + return { + ui, + queryTerminalBackgroundColor, + queryTerminalColorScheme, + setTerminalColorSchemeNotifications, + unsubscribeTerminalColorScheme, + emitTerminalColorScheme: (terminalTheme: TerminalTheme) => terminalColorSchemeListener?.(terminalTheme), + }; +} + +function createController(ui: TUI, getSettingsManager: () => SettingsManager, initialThemeSetting?: string) { + return new InteractiveThemeController(ui, { + getSettingsManager, + showError: vi.fn(), + onChanged: vi.fn(), + initialThemeSetting, + }); +} + +afterEach(() => { + initTheme("dark"); + vi.unstubAllEnvs(); +}); + +describe("InteractiveThemeController", () => { + it("uses the initial theme without persisting it", async () => { + const { ui, queryTerminalBackgroundColor } = createUi(); + const manager = SettingsManager.inMemory({ theme: "dark" }); + const setTheme = vi.spyOn(manager, "setTheme"); + const flush = vi.spyOn(manager, "flush"); + const controller = createController(ui, () => manager, "light"); + + expect(theme.name).toBe("light"); + expect(controller.getThemeSelection()).toBe("light"); + await controller.applyFromSettings(); + + expect(queryTerminalBackgroundColor).not.toHaveBeenCalled(); + expect(setTheme).not.toHaveBeenCalled(); + expect(flush).not.toHaveBeenCalled(); + }); + + it("resolves a theme pair and follows terminal appearance changes", async () => { + vi.stubEnv("COLORFGBG", "15;0"); + const { ui, queryTerminalColorScheme, setTerminalColorSchemeNotifications, emitTerminalColorScheme } = createUi(); + queryTerminalColorScheme.mockResolvedValue("light"); + const manager = SettingsManager.inMemory({ theme: "dark/light" }); + const controller = createController(ui, () => manager, "light/dark"); + + expect(theme.name).toBe("dark"); + await controller.applyFromSettings(); + expect(theme.name).toBe("light"); + expect(setTerminalColorSchemeNotifications).toHaveBeenCalledWith(true); + + emitTerminalColorScheme("dark"); + expect(theme.name).toBe("dark"); + }); + + it("disables terminal appearance updates when disposed", async () => { + const { ui, queryTerminalColorScheme, setTerminalColorSchemeNotifications, unsubscribeTerminalColorScheme } = + createUi(); + queryTerminalColorScheme.mockResolvedValue("light"); + const manager = SettingsManager.inMemory({ theme: "light/dark" }); + const controller = createController(ui, () => manager); + await controller.applyFromSettings(); + + controller.dispose(); + + expect(setTerminalColorSchemeNotifications).toHaveBeenLastCalledWith(false); + expect(unsubscribeTerminalColorScheme).toHaveBeenCalledOnce(); + }); + + it("detects the current terminal appearance when selecting a theme pair", async () => { + vi.stubEnv("COLORFGBG", ""); + const { ui, queryTerminalColorScheme } = createUi(); + queryTerminalColorScheme.mockResolvedValue("light"); + const manager = SettingsManager.inMemory({ theme: "dark" }); + const controller = createController(ui, () => manager); + + expect(theme.name).toBe("dark"); + await controller.setThemeSetting("light/dark"); + expect(theme.name).toBe("light"); + expect(queryTerminalColorScheme).toHaveBeenCalledOnce(); + }); + + it("lets an explicit selection replace the initial theme", async () => { + const { ui } = createUi(); + const firstManager = SettingsManager.inMemory({ theme: "dark" }); + const secondManager = SettingsManager.inMemory({ theme: "light" }); + let manager = firstManager; + const controller = createController(ui, () => manager, "light"); + await controller.applyFromSettings(); + + expect(controller.setThemeName("dark")).toEqual({ success: true }); + manager = secondManager; + await controller.applyFromSettings(); + + expect(controller.getThemeSelection()).toBe("dark"); + expect(theme.name).toBe("dark"); + }); + + it("reloads theme settings when no initial theme was supplied", async () => { + const { ui } = createUi(); + const firstManager = SettingsManager.inMemory({ theme: "dark" }); + const secondManager = SettingsManager.inMemory({ theme: "light" }); + let manager = firstManager; + const controller = createController(ui, () => manager); + await controller.applyFromSettings(); + + firstManager.applyOverrides({ theme: "light" }); + await controller.applyFromSettings(); + expect(theme.name).toBe("light"); + + secondManager.applyOverrides({ theme: "dark" }); + manager = secondManager; + await controller.applyFromSettings(); + expect(theme.name).toBe("dark"); + }); +}); diff --git a/packages/coding-agent/test/theme-detection.test.ts b/packages/coding-agent/test/theme-detection.test.ts new file mode 100644 index 0000000000000000000000000000000000000000..c323ec6f858ad84167ee96f83be2e0325b87a611 --- /dev/null +++ b/packages/coding-agent/test/theme-detection.test.ts @@ -0,0 +1,174 @@ +import { type RgbColor, resetCapabilitiesCache, setCapabilities } from "@earendil-works/pi-tui"; +import { afterEach, describe, expect, it } from "vitest"; +import { + detectTerminalBackgroundFromEnv, + detectTerminalBackgroundTheme, + detectTerminalThemeForAuto, + getThemeByName, + getThemeForRgbColor, + parseAutoThemeSetting, + resolveThemeSetting, +} from "../src/modes/interactive/theme/theme.ts"; + +afterEach(() => { + resetCapabilitiesCache(); +}); + +describe("detectTerminalBackgroundFromEnv", () => { + it("uses the COLORFGBG background color index", () => { + expect(detectTerminalBackgroundFromEnv({ env: { COLORFGBG: "0;15" } })).toMatchObject({ + theme: "light", + source: "COLORFGBG", + confidence: "high", + }); + expect(detectTerminalBackgroundFromEnv({ env: { COLORFGBG: "15;0" } })).toMatchObject({ + theme: "dark", + source: "COLORFGBG", + confidence: "high", + }); + }); + + it("uses the last COLORFGBG field as the background", () => { + expect(detectTerminalBackgroundFromEnv({ env: { COLORFGBG: "0;7;15" } }).theme).toBe("light"); + }); + + it("defaults to dark without terminal background hints", () => { + expect(detectTerminalBackgroundFromEnv({ env: {} })).toMatchObject({ + theme: "dark", + source: "fallback", + confidence: "low", + }); + }); +}); + +describe("detectTerminalBackgroundTheme", () => { + it("uses the queried terminal background before environment hints", async () => { + let queriedTimeoutMs: number | undefined; + const detection = await detectTerminalBackgroundTheme({ + env: { COLORFGBG: "15;0" }, + timeoutMs: 250, + ui: { + async queryTerminalBackgroundColor({ timeoutMs }: { timeoutMs: number }): Promise { + queriedTimeoutMs = timeoutMs; + return { r: 250, g: 250, b: 250 }; + }, + }, + }); + + expect(queriedTimeoutMs).toBe(250); + expect(detection).toMatchObject({ + theme: "light", + source: "terminal background", + confidence: "high", + }); + }); + + it("falls back to environment hints when the terminal query returns no color", async () => { + const detection = await detectTerminalBackgroundTheme({ + env: { COLORFGBG: "15;0" }, + timeoutMs: 250, + ui: { + async queryTerminalBackgroundColor(): Promise { + return undefined; + }, + }, + }); + + expect(detection).toMatchObject({ + theme: "dark", + source: "COLORFGBG", + confidence: "high", + }); + }); + + it("falls back to environment hints when the terminal query fails", async () => { + const detection = await detectTerminalBackgroundTheme({ + env: { COLORFGBG: "0;15" }, + timeoutMs: 250, + ui: { + async queryTerminalBackgroundColor(): Promise { + throw new Error("terminal write failed"); + }, + }, + }); + + expect(detection).toMatchObject({ + theme: "light", + source: "COLORFGBG", + confidence: "high", + }); + }); +}); + +describe("detectTerminalThemeForAuto", () => { + it("starts both queries and returns the preferred color-scheme result without waiting", async () => { + let resolveColorScheme!: (theme: "dark" | "light" | undefined) => void; + let backgroundQueryStarted = false; + const detection = detectTerminalThemeForAuto({ + timeoutMs: 100, + ui: { + queryTerminalColorScheme: () => + new Promise((resolve) => { + resolveColorScheme = resolve; + }), + queryTerminalBackgroundColor: () => { + backgroundQueryStarted = true; + return new Promise(() => {}); + }, + }, + }); + + expect(backgroundQueryStarted).toBe(true); + resolveColorScheme("dark"); + await expect(detection).resolves.toBe("dark"); + }); + + it("uses the background result when the color-scheme query fails", async () => { + await expect( + detectTerminalThemeForAuto({ + timeoutMs: 100, + ui: { + async queryTerminalColorScheme(): Promise { + throw new Error("color-scheme query failed"); + }, + async queryTerminalBackgroundColor(): Promise { + return { r: 250, g: 250, b: 250 }; + }, + }, + }), + ).resolves.toBe("light"); + }); +}); + +describe("theme color mode", () => { + it("uses terminal capabilities", () => { + setCapabilities({ images: null, trueColor: false, hyperlinks: false }); + const ansi256Theme = getThemeByName("dark"); + if (!ansi256Theme) throw new Error("dark theme not found"); + expect(ansi256Theme.getColorMode()).toBe("256color"); + expect(ansi256Theme.getFgAnsi("accent")).toMatch(/^\x1b\[38;5;\d+m$/); + + setCapabilities({ images: null, trueColor: true, hyperlinks: false }); + const truecolorTheme = getThemeByName("dark"); + if (!truecolorTheme) throw new Error("dark theme not found"); + expect(truecolorTheme.getColorMode()).toBe("truecolor"); + expect(truecolorTheme.getFgAnsi("accent")).toMatch(/^\x1b\[38;2;\d+;\d+;\d+m$/); + }); +}); + +describe("theme detection from RGB", () => { + it("classifies RGB colors by luminance", () => { + expect(getThemeForRgbColor({ r: 8, g: 8, b: 8 })).toBe("dark"); + expect(getThemeForRgbColor({ r: 250, g: 250, b: 250 })).toBe("light"); + }); +}); + +describe("theme setting helpers", () => { + it("parses and resolves automatic theme settings", () => { + expect(parseAutoThemeSetting("light/dark")).toEqual({ lightTheme: "light", darkTheme: "dark" }); + expect(resolveThemeSetting("dark", "light")).toBe("dark"); + expect(resolveThemeSetting("light/dark", "light")).toBe("light"); + expect(resolveThemeSetting("light/dark", "dark")).toBe("dark"); + expect(resolveThemeSetting("light/dark/extra", "dark")).toBeUndefined(); + }); +}); diff --git a/packages/coding-agent/test/theme-export.test.ts b/packages/coding-agent/test/theme-export.test.ts new file mode 100644 index 0000000000000000000000000000000000000000..155a07a48fd43d73e5c41741c54e29e33ac0cd99 --- /dev/null +++ b/packages/coding-agent/test/theme-export.test.ts @@ -0,0 +1,104 @@ +import { mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from "fs"; +import { tmpdir } from "os"; +import { join } from "path"; +import { afterEach, beforeEach, describe, expect, it } from "vitest"; +import { getThemeExportColors } from "../src/modes/interactive/theme/theme.ts"; + +type ThemeFile = { + name: string; + vars?: Record; + colors: Record; + export?: { + pageBg?: string | number; + cardBg?: string | number; + infoBg?: string | number; + }; +}; + +describe("getThemeExportColors", () => { + let tempRoot: string; + let previousAgentDir: string | undefined; + + beforeEach(() => { + tempRoot = mkdtempSync(join(tmpdir(), "pi-theme-export-")); + previousAgentDir = process.env.PI_CODING_AGENT_DIR; + process.env.PI_CODING_AGENT_DIR = join(tempRoot, "agent"); + mkdirSync(join(process.env.PI_CODING_AGENT_DIR, "themes"), { recursive: true }); + }); + + afterEach(() => { + rmSync(tempRoot, { recursive: true, force: true }); + if (previousAgentDir === undefined) { + delete process.env.PI_CODING_AGENT_DIR; + } else { + process.env.PI_CODING_AGENT_DIR = previousAgentDir; + } + }); + + it("resolves export variable references using the same syntax as colors", () => { + const darkTheme = JSON.parse( + readFileSync(new URL("../src/modes/interactive/theme/dark.json", import.meta.url), "utf-8"), + ) as ThemeFile; + + const customTheme: ThemeFile = { + ...darkTheme, + name: "custom-export-vars", + vars: { + ...(darkTheme.vars ?? {}), + pageBgVar: "#112233", + pageBgAlias: "pageBgVar", + infoBgVar: "#445566", + cardBgVar: "#223344", + }, + export: { + pageBg: "pageBgAlias", + cardBg: "cardBgVar", + infoBg: "infoBgVar", + }, + }; + + writeFileSync( + join(process.env.PI_CODING_AGENT_DIR!, "themes", "custom-export-vars.json"), + JSON.stringify(customTheme, null, 2), + ); + + expect(getThemeExportColors("custom-export-vars")).toEqual({ + pageBg: "#112233", + cardBg: "#223344", + infoBg: "#445566", + }); + }); + + it("resolves recursive vars and converts 256-color export values to hex", () => { + const darkTheme = JSON.parse( + readFileSync(new URL("../src/modes/interactive/theme/dark.json", import.meta.url), "utf-8"), + ) as ThemeFile; + + const customTheme: ThemeFile = { + ...darkTheme, + name: "custom-export-recursive", + vars: { + ...(darkTheme.vars ?? {}), + deepPageBg: "#abcdef", + pageBgAlias: "deepPageBg", + cardBgAnsi: 24, + }, + export: { + pageBg: "pageBgAlias", + cardBg: "cardBgAnsi", + infoBg: "", + }, + }; + + writeFileSync( + join(process.env.PI_CODING_AGENT_DIR!, "themes", "custom-export-recursive.json"), + JSON.stringify(customTheme, null, 2), + ); + + expect(getThemeExportColors("custom-export-recursive")).toEqual({ + pageBg: "#abcdef", + cardBg: "#005f87", + infoBg: undefined, + }); + }); +}); diff --git a/packages/coding-agent/test/theme-picker.test.ts b/packages/coding-agent/test/theme-picker.test.ts new file mode 100644 index 0000000000000000000000000000000000000000..7b31854e0df7375398ceac619377538e57f1cb49 --- /dev/null +++ b/packages/coding-agent/test/theme-picker.test.ts @@ -0,0 +1,51 @@ +import { mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs"; +import { tmpdir } from "node:os"; +import { join } from "node:path"; +import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; +import { + getAvailableThemes, + getAvailableThemesWithPaths, + setRegisteredThemes, +} from "../src/modes/interactive/theme/theme.ts"; + +type ThemeFile = { + name: string; + vars?: Record; + colors: Record; +}; + +describe("theme picker", () => { + let tempRoot: string; + + beforeEach(() => { + tempRoot = mkdtempSync(join(tmpdir(), "pi-theme-picker-")); + const agentDir = join(tempRoot, "agent"); + vi.stubEnv("PI_CODING_AGENT_DIR", agentDir); + mkdirSync(join(agentDir, "themes"), { recursive: true }); + setRegisteredThemes([]); + }); + + afterEach(() => { + setRegisteredThemes([]); + rmSync(tempRoot, { recursive: true, force: true }); + vi.unstubAllEnvs(); + }); + + it("uses custom theme content names instead of file names", () => { + const darkTheme = JSON.parse( + readFileSync(new URL("../src/modes/interactive/theme/dark.json", import.meta.url), "utf-8"), + ) as ThemeFile; + const customTheme: ThemeFile = { + ...darkTheme, + name: "bar", + }; + + const themePath = join(process.env.PI_CODING_AGENT_DIR!, "themes", "foo.json"); + writeFileSync(themePath, JSON.stringify(customTheme, null, 2)); + + expect(getAvailableThemes()).toContain("bar"); + expect(getAvailableThemes()).not.toContain("foo"); + expect(getAvailableThemesWithPaths()).toContainEqual({ name: "bar", path: themePath }); + expect(getAvailableThemesWithPaths().some((theme) => theme.name === "foo")).toBe(false); + }); +}); diff --git a/packages/coding-agent/test/thinking-selector.test.ts b/packages/coding-agent/test/thinking-selector.test.ts new file mode 100644 index 0000000000000000000000000000000000000000..bd0f1e33794e12d17394af83824cae7e6ff471d9 --- /dev/null +++ b/packages/coding-agent/test/thinking-selector.test.ts @@ -0,0 +1,55 @@ +import { setKeybindings } from "@earendil-works/pi-tui"; +import { beforeAll, beforeEach, describe, expect, it, vi } from "vitest"; +import { KeybindingsManager } from "../src/core/keybindings.ts"; +import { ThinkingSelectorComponent } from "../src/modes/interactive/components/thinking-selector.ts"; +import { initTheme } from "../src/modes/interactive/theme/theme.ts"; +import { stripAnsi } from "../src/utils/ansi.ts"; + +describe("thinking selector", () => { + beforeAll(() => { + initTheme("dark"); + }); + + beforeEach(() => { + setKeybindings(new KeybindingsManager()); + }); + + it("keeps the current thinking level marked while browsing", () => { + const selector = new ThinkingSelectorComponent( + "medium", + ["medium", "high"], + () => {}, + () => {}, + ); + const getLevelRow = (level: string): string | undefined => + selector + .getSelectList() + .render(80) + .map((line) => stripAnsi(line)) + .find((line) => line.includes(level)); + + expect(selector.getSelectList().getSelectedItem()?.label).toBe("✓ medium"); + expect(getLevelRow("medium")?.startsWith("→ ✓ medium")).toBe(true); + selector.handleInput("\x1b[B"); + expect(getLevelRow("medium")?.startsWith(" ✓ medium")).toBe(true); + expect(getLevelRow("high")?.startsWith("→ high")).toBe(true); + }); + + it("uses the configured save binding", () => { + setKeybindings(new KeybindingsManager({ "app.thinking.save": "ctrl+r" })); + const saveDefault = vi.fn(); + const selector = new ThinkingSelectorComponent( + "medium", + ["medium", "high"], + () => {}, + () => {}, + saveDefault, + ); + + expect(stripAnsi(selector.render(80).join("\n"))).toContain("Ctrl+R to set as default"); + selector.handleInput("\x13"); + expect(saveDefault).not.toHaveBeenCalled(); + selector.handleInput("\x12"); + expect(saveDefault).toHaveBeenCalledWith("medium"); + }); +}); diff --git a/packages/coding-agent/test/tool-execution-component.test.ts b/packages/coding-agent/test/tool-execution-component.test.ts new file mode 100644 index 0000000000000000000000000000000000000000..5ebec2e8ac017922873110d948a3d6315bfa868d --- /dev/null +++ b/packages/coding-agent/test/tool-execution-component.test.ts @@ -0,0 +1,584 @@ +import { join, resolve } from "node:path"; +import { Text, type TUI, type TuiMouseEvent } from "@earendil-works/pi-tui"; +import { Type } from "typebox"; +import { beforeAll, describe, expect, test } from "vitest"; +import { getReadmePath } from "../src/config.ts"; +import type { ToolDefinition } from "../src/core/extensions/types.ts"; +import { type BashOperations, createBashToolDefinition } from "../src/core/tools/bash.ts"; +import { createReadTool, createReadToolDefinition } from "../src/core/tools/read.ts"; +import { withBuiltInRenderers } from "../src/core/tools/renderers/index.ts"; +import { createWriteToolDefinition } from "../src/core/tools/write.ts"; +import { ToolExecutionComponent } from "../src/modes/interactive/components/tool-execution.ts"; +import { initTheme, theme } from "../src/modes/interactive/theme/theme.ts"; +import { stripAnsi } from "../src/utils/ansi.ts"; + +function createBaseToolDefinition(name = "custom_tool"): ToolDefinition { + return { + name, + label: name, + description: "custom tool", + parameters: Type.Any(), + execute: async () => ({ + content: [{ type: "text", text: "ok" }], + details: {}, + }), + }; +} + +function createFakeTui(): TUI { + return { + requestRender: () => {}, + } as unknown as TUI; +} + +describe("ToolExecutionComponent parity", () => { + beforeAll(() => { + initTheme("dark"); + }); + + test("stacks custom call and result renderers like the old implementation", () => { + const toolDefinition: ToolDefinition = { + ...createBaseToolDefinition(), + renderCall: () => new Text("custom call", 0, 0), + renderResult: () => new Text("custom result", 0, 0), + }; + + const component = new ToolExecutionComponent( + "custom_tool", + "tool-1", + {}, + {}, + toolDefinition, + createFakeTui(), + process.cwd(), + ); + expect(stripAnsi(component.render(120).join("\n"))).toContain("custom call"); + + component.updateResult( + { + content: [{ type: "text", text: "done" }], + details: {}, + isError: false, + }, + false, + ); + + const rendered = stripAnsi(component.render(120).join("\n")); + expect(rendered).toContain("custom call"); + expect(rendered).toContain("custom result"); + }); + + test("self-rendered empty tool rows take no layout space", () => { + const toolDefinition: ToolDefinition = { + ...createBaseToolDefinition(), + renderShell: "self", + renderCall: () => new Text("", 0, 0), + renderResult: () => new Text("", 0, 0), + }; + + const component = new ToolExecutionComponent( + "custom_tool", + "tool-empty-self-render", + {}, + {}, + toolDefinition, + createFakeTui(), + process.cwd(), + ); + expect(component.render(120)).toEqual([]); + + component.updateResult( + { + content: [], + details: {}, + isError: false, + }, + false, + ); + + expect(component.render(120)).toEqual([]); + }); + + test("uses built-in rendering for built-in overrides without custom renderers", () => { + const overrideDefinition: ToolDefinition = { + ...createBaseToolDefinition("edit"), + }; + + const component = new ToolExecutionComponent( + "edit", + "tool-2", + { path: "README.md", oldText: "before", newText: "after" }, + {}, + withBuiltInRenderers("edit", overrideDefinition), + createFakeTui(), + process.cwd(), + ); + component.updateResult({ content: [], details: { diff: "+1 after", firstChangedLine: 1 }, isError: false }); + const rendered = stripAnsi(component.render(120).join("\n")); + expect(rendered).toContain("edit"); + expect(rendered).toContain("README.md"); + expect(rendered).not.toContain(":1"); + }); + + test("preserves legacy file_path rendering compatibility for built-in tools", () => { + const component = new ToolExecutionComponent( + "read", + "tool-3", + { file_path: "README.md" }, + {}, + undefined, + createFakeTui(), + process.cwd(), + ); + const rendered = stripAnsi(component.render(120).join("\n")); + expect(rendered).toContain("read"); + expect(rendered).toContain("README.md"); + }); + + test("bash execute emits an initial empty partial update before output arrives", async () => { + const updates: Array<{ content: Array<{ type: string; text?: string }>; details?: unknown }> = []; + const operations: BashOperations = { + exec: async () => { + await new Promise((resolve) => setTimeout(resolve, 10)); + return { exitCode: 0 }; + }, + }; + const tool = createBashToolDefinition(process.cwd(), { operations, exposeSessionEnvironment: false }); + const promise = tool.execute( + "tool-bash-1", + { command: "sleep 10" }, + undefined, + (update) => updates.push(update as { content: Array<{ type: string; text?: string }>; details?: unknown }), + {} as never, + ); + expect(updates).toEqual([{ content: [], details: undefined }]); + await promise; + }); + + test("bash renderer does not duplicate final full output truncation details", async () => { + const operations: BashOperations = { + exec: async (_command, _cwd, { onData }) => { + for (let i = 1; i <= 4000; i++) { + onData(Buffer.from(`line-${String(i).padStart(4, "0")}\n`)); + } + return { exitCode: 0 }; + }, + }; + const tool = createBashToolDefinition(process.cwd(), { operations, exposeSessionEnvironment: false }); + const result = await tool.execute( + "tool-bash-1b", + { command: "generate output" }, + undefined, + undefined, + {} as never, + ); + const component = new ToolExecutionComponent( + "bash", + "tool-bash-1b", + { command: "generate output" }, + {}, + tool, + createFakeTui(), + process.cwd(), + ); + component.setExpanded(true); + component.updateResult({ ...result, isError: false }, false); + + const rendered = stripAnsi(component.render(200).join("\n")); + expect(rendered.match(/Full output:/g)?.length ?? 0).toBe(1); + expect(rendered).toMatch(/line-4000[^\n]*\n[^\S\n]*\n \[Full output:/); + expect(rendered).not.toMatch(/line-4000[^\n]*\n[^\S\n]*\n[^\S\n]*\n \[Full output:/); + expect(rendered).toContain("Truncated: showing 2000 of 4000 lines"); + expect(rendered).not.toContain("[Showing lines 2001-4000 of 4000. Full output:"); + }); + + test("does not duplicate built-in headers when passed the active built-in definition", () => { + const component = new ToolExecutionComponent( + "read", + "tool-4", + { path: "README.md" }, + {}, + createReadToolDefinition(process.cwd()), + createFakeTui(), + process.cwd(), + ); + component.updateResult({ content: [{ type: "text", text: "hello" }], details: undefined, isError: false }, false); + const rendered = stripAnsi(component.render(120).join("\n")); + expect(rendered.match(/\bread\b/g)?.length ?? 0).toBe(1); + }); + + test("inherits missing built-in result renderer slot from the built-in tool", () => { + const overrideDefinition: ToolDefinition = { + ...createBaseToolDefinition("read"), + renderCall: () => new Text("override call", 0, 0), + }; + + const component = new ToolExecutionComponent( + "read", + "tool-4b", + { path: "notes.txt" }, + {}, + withBuiltInRenderers("read", overrideDefinition), + createFakeTui(), + process.cwd(), + ); + component.updateResult({ content: [{ type: "text", text: "hello" }], details: undefined, isError: false }, false); + component.setExpanded(true); + const rendered = stripAnsi(component.render(120).join("\n")); + expect(rendered).toContain("override call"); + expect(rendered).toContain("hello"); + }); + + test("inherits missing built-in call renderer slot from the built-in tool", () => { + const overrideDefinition: ToolDefinition = { + ...createBaseToolDefinition("read"), + renderResult: () => new Text("override result", 0, 0), + }; + + const component = new ToolExecutionComponent( + "read", + "tool-4c", + { path: "README.md" }, + {}, + withBuiltInRenderers("read", overrideDefinition), + createFakeTui(), + process.cwd(), + ); + component.updateResult({ content: [{ type: "text", text: "hello" }], details: undefined, isError: false }, false); + const rendered = stripAnsi(component.render(120).join("\n")); + expect(rendered).toContain("read"); + expect(rendered).toContain("README.md"); + expect(rendered).toContain("override result"); + }); + + test("uses custom renderers for built-in overrides that reuse built-in definition parameters", () => { + const builtInDefinition = createReadToolDefinition(process.cwd()); + const component = new ToolExecutionComponent( + "read", + "tool-4d", + { path: "README.md" }, + {}, + { + ...builtInDefinition, + renderCall: () => new Text("override call", 0, 0), + renderResult: () => new Text("override result", 0, 0), + }, + createFakeTui(), + process.cwd(), + ); + component.updateResult({ content: [{ type: "text", text: "hello" }], details: undefined, isError: false }, false); + const rendered = stripAnsi(component.render(120).join("\n")); + expect(rendered).toContain("override call"); + expect(rendered).toContain("override result"); + expect(rendered).not.toContain("read README.md"); + }); + + test("uses custom renderers for built-in overrides that reuse wrapped built-in tool parameters", () => { + const builtInTool = createReadTool(process.cwd()); + const component = new ToolExecutionComponent( + "read", + "tool-4e", + { path: "README.md" }, + {}, + { + ...createBaseToolDefinition("read"), + parameters: builtInTool.parameters, + renderCall: () => new Text("wrapped override call", 0, 0), + renderResult: () => new Text("wrapped override result", 0, 0), + }, + createFakeTui(), + process.cwd(), + ); + component.updateResult({ content: [{ type: "text", text: "hello" }], details: undefined, isError: false }, false); + const rendered = stripAnsi(component.render(120).join("\n")); + expect(rendered).toContain("wrapped override call"); + expect(rendered).toContain("wrapped override result"); + }); + + test("shares renderer state across custom call and result slots", () => { + type RenderState = { token?: string }; + const toolDefinition: ToolDefinition = { + ...createBaseToolDefinition(), + renderCall: (_args, _theme, context) => { + context.state.token ??= "shared-token"; + return new Text(`custom call ${context.state.token}`, 0, 0); + }, + renderResult: (_result, _options, _theme, context) => { + return new Text(`custom result ${context.state.token}`, 0, 0); + }, + }; + + const component = new ToolExecutionComponent( + "custom_tool", + "tool-5", + {}, + {}, + toolDefinition, + createFakeTui(), + process.cwd(), + ); + component.updateResult({ content: [{ type: "text", text: "done" }], details: {}, isError: false }, false); + const rendered = stripAnsi(component.render(120).join("\n")); + expect(rendered).toContain("custom call shared-token"); + expect(rendered).toContain("custom result shared-token"); + }); + + test("exposes args in render result context", () => { + const toolDefinition: ToolDefinition = { + ...createBaseToolDefinition(), + renderCall: () => new Text("call", 0, 0), + renderResult: (_result, _options, _theme, context) => + new Text(`arg:${String((context.args as { foo: string }).foo)}`, 0, 0), + }; + + const component = new ToolExecutionComponent( + "custom_tool", + "tool-5b", + { foo: "bar" }, + {}, + toolDefinition, + createFakeTui(), + process.cwd(), + ); + component.updateResult({ content: [{ type: "text", text: "done" }], details: {}, isError: false }, false); + const rendered = stripAnsi(component.render(120).join("\n")); + expect(rendered).toContain("arg:bar"); + }); + + test("collapses fallback results until expanded", () => { + const toolDefinition: ToolDefinition = { + ...createBaseToolDefinition(), + }; + + const component = new ToolExecutionComponent( + "custom_tool", + "tool-6", + { foo: "bar" }, + {}, + toolDefinition, + createFakeTui(), + process.cwd(), + ); + const output = Array.from({ length: 15 }, (_, index) => `line-${index + 1}`).join("\n"); + component.updateResult({ content: [{ type: "text", text: output }], details: {}, isError: false }, false); + + const collapsed = stripAnsi(component.render(120).join("\n")); + expect(collapsed).toContain("custom_tool"); + expect(collapsed).toContain("line-10"); + expect(collapsed).not.toContain("line-11"); + expect(collapsed).toContain("5 more lines"); + expect(collapsed).toContain("to expand"); + + component.setExpanded(true); + const expanded = stripAnsi(component.render(120).join("\n")); + expect(expanded).toContain("line-15"); + expect(expanded).not.toContain("more lines"); + }); + + test("trims trailing blank display lines from write previews", () => { + const component = new ToolExecutionComponent( + "write", + "tool-7", + { path: "README.md", content: "one\ntwo\n" }, + {}, + createWriteToolDefinition(process.cwd()), + createFakeTui(), + process.cwd(), + ); + const rendered = stripAnsi(component.render(120).join("\n")); + expect(rendered).toContain("one"); + expect(rendered).toContain("two"); + expect(rendered).not.toContain("two\n\n"); + }); + + test("trims trailing blank display lines from read results", () => { + const component = new ToolExecutionComponent( + "read", + "tool-8", + { path: "notes.txt" }, + {}, + createReadToolDefinition(process.cwd()), + createFakeTui(), + process.cwd(), + ); + component.updateResult( + { content: [{ type: "text", text: "one\ntwo\n" }], details: undefined, isError: false }, + false, + ); + component.setExpanded(true); + const rendered = stripAnsi(component.render(120).join("\n")); + expect(rendered).toContain("one"); + expect(rendered).toContain("two"); + expect(rendered).not.toContain("two\n\n"); + }); + + test("does not syntax-highlight read errors based on the requested file path", () => { + const component = new ToolExecutionComponent( + "read", + "tool-read-error-highlighting", + { path: "config.exs", offset: 120, limit: 130 }, + {}, + createReadToolDefinition(process.cwd()), + createFakeTui(), + process.cwd(), + ); + const error = "Offset 120 is beyond end of file (96 lines total)"; + component.updateResult({ content: [{ type: "text", text: error }], details: undefined, isError: true }, false); + + const rendered = component.render(120).join("\n"); + expect(stripAnsi(rendered)).toContain(error); + expect(rendered).toContain(theme.fg("toolOutput", error)); + }); + + test("expands a collapsed tool result when clicked", () => { + const component = new ToolExecutionComponent( + "read", + "tool-click-expand", + { path: "notes.txt" }, + {}, + createReadToolDefinition(process.cwd()), + createFakeTui(), + process.cwd(), + ); + component.updateResult( + { content: [{ type: "text", text: "hidden content" }], details: undefined, isError: false }, + false, + ); + const width = 120; + const lines = component.render(width); + const resultRow = lines.findIndex((line) => stripAnsi(line).includes("notes.txt")); + expect(resultRow).toBeGreaterThanOrEqual(0); + const event: TuiMouseEvent = { + type: "click", + button: "left", + x: 2, + y: resultRow, + screenX: 2, + screenY: resultRow, + width, + height: lines.length, + shift: false, + alt: false, + ctrl: false, + clickCount: 1, + }; + expect(component.handleMouse(event)?.handled).toBe(true); + expect(stripAnsi(component.render(width).join("\n"))).toContain("hidden content"); + }); + + test("collapses ordinary read results until expanded", () => { + const component = new ToolExecutionComponent( + "read", + "tool-ordinary-read-collapsed", + { path: "notes.txt" }, + {}, + createReadToolDefinition(process.cwd()), + createFakeTui(), + process.cwd(), + ); + component.updateResult( + { content: [{ type: "text", text: "hidden content" }], details: undefined, isError: false }, + false, + ); + + const collapsed = stripAnsi(component.render(120).join("\n")); + expect(collapsed).toContain("read"); + expect(collapsed).toContain("notes.txt"); + expect(collapsed).not.toContain("hidden content"); + + component.setExpanded(true); + const expanded = stripAnsi(component.render(120).join("\n")); + expect(expanded).toContain("hidden content"); + }); + + for (const scenario of [ + { + title: "SKILL.md", + path: join(process.cwd(), "attio", "SKILL.md"), + content: "---\nname: attio\ndescription: CRM helper\n---\n\n# Hidden skill instructions", + compact: "[skill] attio", + hidden: "Hidden skill instructions", + absent: "read skill attio", + }, + { + title: "AGENTS.md", + path: join(process.cwd(), ".pi", "AGENTS.md"), + content: "Hidden resource instructions", + compact: "read resource .pi/AGENTS.md", + hidden: "Hidden resource instructions", + absent: undefined, + }, + { + title: "AGENTS.override.md", + path: join(process.cwd(), ".pi", "AGENTS.override.md"), + content: "Hidden override instructions", + compact: "read resource .pi/AGENTS.override.md", + hidden: "Hidden override instructions", + absent: undefined, + }, + { + title: "outside AGENTS.md", + path: resolve(process.cwd(), "..", "AGENTS.md"), + content: "Hidden outside resource instructions", + compact: `read resource ${resolve(process.cwd(), "..", "AGENTS.md").replace(/\\/g, "/")}`, + hidden: "Hidden outside resource instructions", + absent: undefined, + }, + { + title: "Pi documentation", + path: getReadmePath(), + content: "Hidden docs content", + compact: "read docs README.md", + hidden: "Hidden docs content", + absent: undefined, + }, + ] as const) { + test(`renders ${scenario.title} read results compactly until expanded`, () => { + const component = new ToolExecutionComponent( + "read", + `tool-compact-${scenario.title}`, + { path: scenario.path }, + {}, + createReadToolDefinition(process.cwd()), + createFakeTui(), + process.cwd(), + ); + component.updateResult( + { content: [{ type: "text", text: scenario.content }], details: undefined, isError: false }, + false, + ); + + const collapsed = stripAnsi(component.render(120).join("\n")); + expect(collapsed).toContain(scenario.compact); + expect(collapsed).not.toContain(scenario.hidden); + if (scenario.absent) { + expect(collapsed).not.toContain(scenario.absent); + } + + component.setExpanded(true); + const expanded = stripAnsi(component.render(120).join("\n")); + expect(expanded).toContain(scenario.hidden); + }); + } + + for (const scenario of [ + { title: "SKILL.md", path: join(process.cwd(), "attio", "SKILL.md"), compact: "[skill] attio:120-329" }, + { title: "Pi documentation", path: getReadmePath(), compact: "read docs README.md:120-329" }, + ] as const) { + test(`shows the read line range in compact ${scenario.title} reads before the expand hint`, () => { + const component = new ToolExecutionComponent( + "read", + `tool-compact-range-${scenario.title}`, + { path: scenario.path, offset: 120, limit: 210 }, + {}, + createReadToolDefinition(process.cwd()), + createFakeTui(), + process.cwd(), + ); + + const collapsed = stripAnsi(component.render(120).join("\n")); + expect(collapsed).toContain(scenario.compact); + expect(collapsed.indexOf(":120-329")).toBeLessThan(collapsed.indexOf("to expand")); + }); + } +}); diff --git a/packages/coding-agent/test/tool-result-images.test.ts b/packages/coding-agent/test/tool-result-images.test.ts new file mode 100644 index 0000000000000000000000000000000000000000..ae05285206d2cda12b7c1eca663fea7eec353d92 --- /dev/null +++ b/packages/coding-agent/test/tool-result-images.test.ts @@ -0,0 +1,137 @@ +import { crc32, deflateSync } from "node:zlib"; +import { describe, expect, it } from "vitest"; +import type { ToolResultContent } from "../src/utils/tool-result-images.ts"; +import { normalizeToolResultImages } from "../src/utils/tool-result-images.ts"; + +const TINY_PNG_BASE64 = + "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mP8z8DwHwAFBQIAX8jx0gAAAABJRU5ErkJggg=="; + +function pngChunk(type: string, body: Buffer): Buffer { + const header = Buffer.alloc(8); + header.writeUInt32BE(body.length, 0); + header.write(type, 4, "ascii"); + const checksum = Buffer.alloc(4); + checksum.writeUInt32BE(crc32(Buffer.concat([header.subarray(4), body])), 0); + return Buffer.concat([header, body, checksum]); +} + +/** Build an 8-bit grayscale PNG of arbitrary dimensions without pulling in an encoder. */ +function createPng(width: number, height: number): Buffer { + const ihdr = Buffer.alloc(13); + ihdr.writeUInt32BE(width, 0); + ihdr.writeUInt32BE(height, 4); + ihdr[8] = 8; // bit depth + ihdr[9] = 0; // color type: grayscale + const raw = Buffer.alloc((width + 1) * height); + for (let row = 0; row < height; row++) { + raw.fill(row % 256, row * (width + 1) + 1, (row + 1) * (width + 1)); + } + return Buffer.concat([ + Buffer.from([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a]), + pngChunk("IHDR", ihdr), + pngChunk("IDAT", deflateSync(raw)), + pngChunk("IEND", Buffer.alloc(0)), + ]); +} + +function readPngDimensions(base64Data: string): { width: number; height: number } { + const buffer = Buffer.from(base64Data, "base64"); + return { width: buffer.readUInt32BE(16), height: buffer.readUInt32BE(20) }; +} + +function createTinyBmp1x1Red24bpp(): Buffer { + const buffer = Buffer.alloc(58); + buffer.write("BM", 0, "ascii"); + buffer.writeUInt32LE(buffer.length, 2); + buffer.writeUInt32LE(54, 10); + buffer.writeUInt32LE(40, 14); + buffer.writeInt32LE(1, 18); + buffer.writeInt32LE(1, 22); + buffer.writeUInt16LE(1, 26); + buffer.writeUInt16LE(24, 28); + buffer.writeUInt32LE(0, 30); + buffer.writeUInt32LE(4, 34); + buffer[56] = 0xff; + return buffer; +} + +function imageBlock(bytes: Buffer, mimeType: string): ToolResultContent { + return { type: "image", data: bytes.toString("base64"), mimeType }; +} + +describe("normalizeToolResultImages", () => { + it("returns the original array when there are no image blocks", async () => { + const content: ToolResultContent[] = [{ type: "text", text: "no images here" }]; + + expect(await normalizeToolResultImages(content)).toBe(content); + }); + + it("returns the original array when images are already within limits", async () => { + const content: ToolResultContent[] = [ + { type: "text", text: "screenshot" }, + { type: "image", data: TINY_PNG_BASE64, mimeType: "image/png" }, + ]; + + expect(await normalizeToolResultImages(content)).toBe(content); + }); + + it("resizes oversized images and reports the original dimensions", async () => { + const content: ToolResultContent[] = [imageBlock(createPng(2400, 4800), "image/png")]; + + const normalized = await normalizeToolResultImages(content); + + expect(normalized).not.toBe(content); + expect(normalized).toHaveLength(2); + const image = normalized[0]; + expect(image.type).toBe("image"); + if (image.type !== "image") return; + const { width, height } = readPngDimensions(image.data); + expect(width).toBeLessThanOrEqual(2000); + expect(height).toBeLessThanOrEqual(2000); + const note = normalized[1]; + expect(note.type).toBe("text"); + if (note.type !== "text") return; + expect(note.text).toContain("original 2400x4800"); + }); + + it("leaves oversized images alone when auto-resize is disabled", async () => { + const content: ToolResultContent[] = [imageBlock(createPng(2400, 4800), "image/png")]; + + const normalized = await normalizeToolResultImages(content, { autoResizeImages: false }); + + expect(normalized).toBe(content); + }); + + it("converts unsupported image formats even when auto-resize is disabled", async () => { + const content: ToolResultContent[] = [imageBlock(createTinyBmp1x1Red24bpp(), "image/bmp")]; + + const normalized = await normalizeToolResultImages(content, { autoResizeImages: false }); + + expect(normalized).not.toBe(content); + expect(normalized[0]).toMatchObject({ type: "image", mimeType: "image/png" }); + expect(normalized[1]).toMatchObject({ + type: "text", + text: "[Image converted from image/bmp to image/png.]", + }); + }); + + it("keeps undecodable images instead of dropping tool output", async () => { + const content: ToolResultContent[] = [{ type: "image", data: "bm90LWFuLWltYWdl", mimeType: "image/png" }]; + + expect(await normalizeToolResultImages(content)).toBe(content); + }); + + it("preserves surrounding text blocks and their order", async () => { + const content: ToolResultContent[] = [ + { type: "text", text: "before" }, + imageBlock(createPng(2400, 100), "image/png"), + { type: "text", text: "after" }, + ]; + + const normalized = await normalizeToolResultImages(content); + + expect(normalized.map((block) => block.type)).toEqual(["text", "image", "text", "text"]); + expect(normalized[0]).toEqual({ type: "text", text: "before" }); + expect(normalized[3]).toEqual({ type: "text", text: "after" }); + }); +}); diff --git a/packages/coding-agent/test/tool-system-prompt-contributions.test.ts b/packages/coding-agent/test/tool-system-prompt-contributions.test.ts new file mode 100644 index 0000000000000000000000000000000000000000..ed39fa508b80da49ece3eba87682b86f76c5ec7c --- /dev/null +++ b/packages/coding-agent/test/tool-system-prompt-contributions.test.ts @@ -0,0 +1,44 @@ +import { describe, expect, test } from "vitest"; +import { bashToolSystemPromptContribution, createBashToolDefinition } from "../src/core/tools/bash.ts"; +import { createEditToolDefinition, editToolSystemPromptContribution } from "../src/core/tools/edit.ts"; +import { createFindToolDefinition, findToolSystemPromptContribution } from "../src/core/tools/find.ts"; +import { createGrepToolDefinition, grepToolSystemPromptContribution } from "../src/core/tools/grep.ts"; +import { createLsToolDefinition, lsToolSystemPromptContribution } from "../src/core/tools/ls.ts"; +import { + createPowerShellToolDefinition, + powershellToolSystemPromptContribution, +} from "../src/core/tools/powershell.ts"; +import { createReadToolDefinition, readToolSystemPromptContribution } from "../src/core/tools/read.ts"; +import { createWriteToolDefinition, writeToolSystemPromptContribution } from "../src/core/tools/write.ts"; + +const cases = [ + ["read", readToolSystemPromptContribution, createReadToolDefinition], + ["bash", bashToolSystemPromptContribution, createBashToolDefinition], + ["powershell", powershellToolSystemPromptContribution, createPowerShellToolDefinition], + ["edit", editToolSystemPromptContribution, createEditToolDefinition], + ["write", writeToolSystemPromptContribution, createWriteToolDefinition], + ["grep", grepToolSystemPromptContribution, createGrepToolDefinition], + ["find", findToolSystemPromptContribution, createFindToolDefinition], + ["ls", lsToolSystemPromptContribution, createLsToolDefinition], +] as const; + +describe("built-in tool system prompt contributions", () => { + test.each(cases)( + "keeps the %s tool definition aligned with its contribution", + (_name, contribution, createDefinition) => { + const definition = createDefinition("/workspace"); + + expect(definition.promptSnippet).toBe(contribution.snippet); + expect(definition.promptGuidelines ?? []).toEqual(contribution.guidelines); + }, + ); + + test.each([ + ["bash", createBashToolDefinition], + ["powershell", createPowerShellToolDefinition], + ] as const)("keeps %s session-environment guidance conditional", (_name, createDefinition) => { + const definition = createDefinition("/workspace", { exposeSessionEnvironment: false }); + + expect(definition.promptGuidelines).toBeUndefined(); + }); +}); diff --git a/packages/coding-agent/test/tools-manager.test.ts b/packages/coding-agent/test/tools-manager.test.ts new file mode 100644 index 0000000000000000000000000000000000000000..d57de4a2d513bc0bd8cf186d84eb27209f1087ee --- /dev/null +++ b/packages/coding-agent/test/tools-manager.test.ts @@ -0,0 +1,142 @@ +import type * as ChildProcess from "node:child_process"; +import type * as Fs from "node:fs"; +import { afterEach, describe, expect, it, vi } from "vitest"; +import { ensureTool, getLatestVersion, type ToolStatus } from "../src/utils/tools-manager.ts"; + +const originalOffline = process.env.PI_OFFLINE; + +vi.mock("fs", async (importOriginal) => { + const actual = await importOriginal(); + return { + ...actual, + existsSync: vi.fn(() => false), + }; +}); + +vi.mock("child_process", async (importOriginal) => { + const actual = await importOriginal(); + return { + ...actual, + spawnSync: vi.fn(() => ({ error: new Error("not found") })), + }; +}); + +afterEach(() => { + if (originalOffline === undefined) delete process.env.PI_OFFLINE; + else process.env.PI_OFFLINE = originalOffline; + vi.unstubAllGlobals(); +}); + +function redirectResponse(location: string): Response { + return new Response(null, { status: 302, headers: { location } }); +} + +describe("getLatestVersion", () => { + it("resolves the version from the release page redirect", async () => { + const fetchMock = vi.fn(async () => redirectResponse("https://github.com/sharkdp/fd/releases/tag/v10.4.2")); + vi.stubGlobal("fetch", fetchMock); + + await expect(getLatestVersion("sharkdp/fd")).resolves.toBe("10.4.2"); + expect(fetchMock).toHaveBeenCalledWith( + "https://github.com/sharkdp/fd/releases/latest", + expect.objectContaining({ redirect: "manual" }), + ); + }); + + it("keeps tags without a v prefix intact", async () => { + vi.stubGlobal( + "fetch", + vi.fn(async () => redirectResponse("https://github.com/BurntSushi/ripgrep/releases/tag/15.2.0")), + ); + + await expect(getLatestVersion("BurntSushi/ripgrep")).resolves.toBe("15.2.0"); + }); + + it("resolves relative redirect targets", async () => { + vi.stubGlobal( + "fetch", + vi.fn(async () => redirectResponse("/sharkdp/fd/releases/tag/v10.4.2")), + ); + + await expect(getLatestVersion("sharkdp/fd")).resolves.toBe("10.4.2"); + }); + + it("discards the redirect response body", async () => { + const response = new Response("", { + status: 302, + headers: { location: "https://github.com/sharkdp/fd/releases/tag/v10.4.2" }, + }); + vi.stubGlobal( + "fetch", + vi.fn(async () => response), + ); + + await expect(getLatestVersion("sharkdp/fd")).resolves.toBe("10.4.2"); + expect(response.bodyUsed).toBe(true); + }); + + it("fails clearly when the endpoint does not redirect", async () => { + vi.stubGlobal( + "fetch", + vi.fn(async () => new Response("not found", { status: 404 })), + ); + + await expect(getLatestVersion("sharkdp/fd")).rejects.toThrow( + "Failed to resolve latest sharkdp/fd release: HTTP 404 without redirect", + ); + }); + + it("fails clearly when the redirect does not point at a release tag", async () => { + vi.stubGlobal( + "fetch", + vi.fn(async () => redirectResponse("https://github.com/login")), + ); + + await expect(getLatestVersion("sharkdp/fd")).rejects.toThrow( + "Failed to resolve latest sharkdp/fd release: unexpected redirect to https://github.com/login", + ); + }); +}); + +describe("ensureTool", () => { + it("reports status through a callback without writing to the console", async () => { + process.env.PI_OFFLINE = "1"; + const statuses: ToolStatus[] = []; + const consoleLog = vi.spyOn(console, "log").mockImplementation(() => {}); + + const result = await ensureTool("fd", (status) => statuses.push(status)); + + expect(result).toBeUndefined(); + expect(statuses).toEqual([ + { + type: "warning", + message: "fd not found. Offline mode enabled, skipping download.", + }, + ]); + expect(consoleLog).not.toHaveBeenCalled(); + consoleLog.mockRestore(); + }); + + it("surfaces the error cause chain when a download fails", async () => { + delete process.env.PI_OFFLINE; + const cause = new Error("connect ETIMEDOUT 140.82.113.3:443"); + vi.stubGlobal( + "fetch", + vi.fn(async () => { + throw new TypeError("fetch failed", { cause }); + }), + ); + const statuses: ToolStatus[] = []; + + const result = await ensureTool("fd", (status) => statuses.push(status)); + + expect(result).toBeUndefined(); + expect(statuses).toEqual([ + { type: "info", message: "fd not found. Downloading..." }, + { + type: "warning", + message: "Failed to download fd: fetch failed: connect ETIMEDOUT 140.82.113.3:443", + }, + ]); + }); +}); diff --git a/packages/coding-agent/test/tools.test.ts b/packages/coding-agent/test/tools.test.ts new file mode 100644 index 0000000000000000000000000000000000000000..bb71dfead88574bb02618be55fce1716f1af7cd3 --- /dev/null +++ b/packages/coding-agent/test/tools.test.ts @@ -0,0 +1,1333 @@ +import { applyPatch } from "diff"; +import { chmodSync, existsSync, mkdirSync, readFileSync, rmSync, writeFileSync } from "fs"; +import { tmpdir } from "os"; +import { join } from "path"; +import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; +import { executeBashWithOperations } from "../src/core/bash-executor.ts"; +import type { ExtensionContext } from "../src/core/extensions/types.ts"; +import { + type BashOperations, + createBashTool, + createBashToolDefinition, + createLocalBashOperations, +} from "../src/core/tools/bash.ts"; +import { createEditToolDefinition } from "../src/core/tools/edit.ts"; +import { computeEditsDiff } from "../src/core/tools/edit-diff.ts"; +import { createFindToolDefinition } from "../src/core/tools/find.ts"; +import { createGrepToolDefinition } from "../src/core/tools/grep.ts"; +import { createLsToolDefinition } from "../src/core/tools/ls.ts"; +import { createReadToolDefinition } from "../src/core/tools/read.ts"; +import { createWriteToolDefinition } from "../src/core/tools/write.ts"; +import { + createEditTool, + createFindTool, + createGrepTool, + createLsTool, + createReadTool, + createWriteTool, +} from "../src/index.ts"; +import * as shellModule from "../src/utils/shell.ts"; + +const readTool = createReadTool(process.cwd()); +const writeTool = createWriteTool(process.cwd()); +const editTool = createEditTool(process.cwd()); +const bashTool = createBashTool(process.cwd()); +const grepTool = createGrepTool(process.cwd()); +const findTool = createFindTool(process.cwd()); +const lsTool = createLsTool(process.cwd()); + +// Helper to extract text from content blocks +function getTextOutput(result: any): string { + return ( + result.content + ?.filter((c: any) => c.type === "text") + .map((c: any) => c.text) + .join("\n") || "" + ); +} + +function createTinyBmp1x1Red24bpp(): Buffer { + const buffer = Buffer.alloc(58); + buffer.write("BM", 0, "ascii"); + buffer.writeUInt32LE(buffer.length, 2); + buffer.writeUInt32LE(54, 10); + buffer.writeUInt32LE(40, 14); + buffer.writeInt32LE(1, 18); + buffer.writeInt32LE(1, 22); + buffer.writeUInt16LE(1, 26); + buffer.writeUInt16LE(24, 28); + buffer.writeUInt32LE(0, 30); + buffer.writeUInt32LE(4, 34); + buffer[56] = 0xff; + return buffer; +} + +describe("Coding Agent Tools", () => { + let testDir: string; + + beforeEach(() => { + // Create a unique temporary directory for each test + testDir = join(tmpdir(), `coding-agent-test-${Date.now()}`); + mkdirSync(testDir, { recursive: true }); + }); + + afterEach(() => { + vi.restoreAllMocks(); + // Clean up test directory + rmSync(testDir, { recursive: true, force: true }); + }); + + describe("read tool", () => { + it("should read file contents that fit within limits", async () => { + const testFile = join(testDir, "test.txt"); + const content = "Hello, world!\nLine 2\nLine 3"; + writeFileSync(testFile, content); + + const result = await readTool.execute("test-call-1", { path: testFile }); + + expect(getTextOutput(result)).toBe(content); + // No truncation message since file fits within limits + expect(getTextOutput(result)).not.toContain("Use offset="); + expect(result.details).toBeUndefined(); + }); + + it("should handle non-existent files", async () => { + const testFile = join(testDir, "nonexistent.txt"); + + await expect(readTool.execute("test-call-2", { path: testFile })).rejects.toThrow(/ENOENT|not found/i); + }); + + it("should truncate files exceeding line limit", async () => { + const testFile = join(testDir, "large.txt"); + const lines = Array.from({ length: 2500 }, (_, i) => `Line ${i + 1}`); + writeFileSync(testFile, lines.join("\n")); + + const result = await readTool.execute("test-call-3", { path: testFile }); + const output = getTextOutput(result); + + expect(output).toContain("Line 1"); + expect(output).toContain("Line 2000"); + expect(output).not.toContain("Line 2001"); + expect(output).toContain("[Showing lines 1-2000 of 2500. Use offset=2001 to continue.]"); + }); + + it("should truncate when byte limit exceeded", async () => { + const testFile = join(testDir, "large-bytes.txt"); + // Create file that exceeds 50KB byte limit but has fewer than 2000 lines + const lines = Array.from({ length: 500 }, (_, i) => `Line ${i + 1}: ${"x".repeat(200)}`); + writeFileSync(testFile, lines.join("\n")); + + const result = await readTool.execute("test-call-4", { path: testFile }); + const output = getTextOutput(result); + + expect(output).toContain("Line 1:"); + // Should show byte limit message + expect(output).toMatch(/\[Showing lines 1-\d+ of 500 \(.* limit\)\. Use offset=\d+ to continue\.\]/); + }); + + it("should handle offset parameter", async () => { + const testFile = join(testDir, "offset-test.txt"); + const lines = Array.from({ length: 100 }, (_, i) => `Line ${i + 1}`); + writeFileSync(testFile, lines.join("\n")); + + const result = await readTool.execute("test-call-5", { path: testFile, offset: 51 }); + const output = getTextOutput(result); + + expect(output).not.toContain("Line 50"); + expect(output).toContain("Line 51"); + expect(output).toContain("Line 100"); + // No truncation message since file fits within limits + expect(output).not.toContain("Use offset="); + }); + + it("should handle limit parameter", async () => { + const testFile = join(testDir, "limit-test.txt"); + const lines = Array.from({ length: 100 }, (_, i) => `Line ${i + 1}`); + writeFileSync(testFile, lines.join("\n")); + + const result = await readTool.execute("test-call-6", { path: testFile, limit: 10 }); + const output = getTextOutput(result); + + expect(output).toContain("Line 1"); + expect(output).toContain("Line 10"); + expect(output).not.toContain("Line 11"); + expect(output).toContain("[90 more lines in file. Use offset=11 to continue.]"); + }); + + it("should handle offset + limit together", async () => { + const testFile = join(testDir, "offset-limit-test.txt"); + const lines = Array.from({ length: 100 }, (_, i) => `Line ${i + 1}`); + writeFileSync(testFile, lines.join("\n")); + + const result = await readTool.execute("test-call-7", { + path: testFile, + offset: 41, + limit: 20, + }); + const output = getTextOutput(result); + + expect(output).not.toContain("Line 40"); + expect(output).toContain("Line 41"); + expect(output).toContain("Line 60"); + expect(output).not.toContain("Line 61"); + expect(output).toContain("[40 more lines in file. Use offset=61 to continue.]"); + }); + + it("should show error when offset is beyond file length", async () => { + const testFile = join(testDir, "short.txt"); + writeFileSync(testFile, "Line 1\nLine 2\nLine 3"); + + await expect(readTool.execute("test-call-8", { path: testFile, offset: 100 })).rejects.toThrow( + /Offset 100 is beyond end of file \(3 lines total\)/, + ); + }); + + it("should include truncation details when truncated", async () => { + const testFile = join(testDir, "large-file.txt"); + const lines = Array.from({ length: 2500 }, (_, i) => `Line ${i + 1}`); + writeFileSync(testFile, lines.join("\n")); + + const result = await readTool.execute("test-call-9", { path: testFile }); + + expect(result.details).toBeDefined(); + expect(result.details?.truncation).toBeDefined(); + expect(result.details?.truncation?.truncated).toBe(true); + expect(result.details?.truncation?.truncatedBy).toBe("lines"); + expect(result.details?.truncation?.totalLines).toBe(2500); + expect(result.details?.truncation?.outputLines).toBe(2000); + }); + + it("should detect image MIME type from file magic (not extension)", async () => { + const png1x1Base64 = + "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR4nGNgYGD4DwABBAEAX+XDSwAAAABJRU5ErkJggg=="; + const pngBuffer = Buffer.from(png1x1Base64, "base64"); + + const testFile = join(testDir, "image.txt"); + writeFileSync(testFile, pngBuffer); + + const result = await readTool.execute("test-call-img-1", { path: testFile }); + + expect(result.content[0]?.type).toBe("text"); + expect(getTextOutput(result)).toContain("Read image file [image/png]"); + + const imageBlock = result.content.find( + (c): c is { type: "image"; mimeType: string; data: string } => c.type === "image", + ); + expect(imageBlock).toBeDefined(); + expect(imageBlock?.mimeType).toBe("image/png"); + expect(typeof imageBlock?.data).toBe("string"); + expect((imageBlock?.data ?? "").length).toBeGreaterThan(0); + }); + + it("should read BMP files from disk as PNG image attachments", async () => { + const testFile = join(testDir, "image.bmp"); + writeFileSync(testFile, createTinyBmp1x1Red24bpp()); + + const result = await readTool.execute("test-call-img-bmp", { path: testFile }); + + expect(result.content[0]?.type).toBe("text"); + expect(getTextOutput(result)).toContain("Read image file [image/png]"); + expect(getTextOutput(result)).toContain("[Image converted from image/bmp to image/png.]"); + + const imageBlock = result.content.find( + (c): c is { type: "image"; mimeType: string; data: string } => c.type === "image", + ); + expect(imageBlock).toBeDefined(); + expect(imageBlock?.mimeType).toBe("image/png"); + expect(Buffer.from(imageBlock?.data ?? "", "base64")[0]).toBe(0x89); + }); + + it("should treat files with image extension but non-image content as text", async () => { + const testFile = join(testDir, "not-an-image.png"); + writeFileSync(testFile, "definitely not a png"); + + const result = await readTool.execute("test-call-img-2", { path: testFile }); + const output = getTextOutput(result); + + expect(output).toContain("definitely not a png"); + expect(result.content.some((c: any) => c.type === "image")).toBe(false); + }); + }); + + describe("write tool", () => { + it("should write file contents", async () => { + const testFile = join(testDir, "write-test.txt"); + const content = "Test content"; + + const result = await writeTool.execute("test-call-3", { path: testFile, content }); + + expect(getTextOutput(result)).toBe(`Successfully wrote to ${testFile}`); + expect(result.details).toBeUndefined(); + }); + + it("should create parent directories", async () => { + const testFile = join(testDir, "nested", "dir", "test.txt"); + const content = "Nested content"; + + const result = await writeTool.execute("test-call-4", { path: testFile, content }); + + expect(getTextOutput(result)).toContain("Successfully wrote"); + }); + }); + + describe("edit tool", () => { + it("should replace text in file", async () => { + const testFile = join(testDir, "edit-test.txt"); + const originalContent = "Hello, world!"; + writeFileSync(testFile, originalContent); + + const result = await editTool.execute("test-call-5", { + path: testFile, + edits: [{ oldText: "world", newText: "testing" }], + }); + + expect(getTextOutput(result)).toContain("Successfully replaced"); + expect(result.details).toBeDefined(); + expect(result.details.diff).toBeDefined(); + expect(typeof result.details.diff).toBe("string"); + expect(result.details.diff).toContain("testing"); + expect(result.details.patch).toContain("--- "); + expect(result.details.patch).toContain("+++ "); + expect(result.details.patch).toContain("@@"); + expect(result.details.patch).toContain("-Hello, world!"); + expect(result.details.patch).toContain("+Hello, testing!"); + expect(applyPatch(originalContent, result.details.patch)).toBe("Hello, testing!"); + }); + + it("should fail if text not found", async () => { + const testFile = join(testDir, "edit-test.txt"); + const originalContent = "Hello, world!"; + writeFileSync(testFile, originalContent); + + await expect( + editTool.execute("test-call-6", { + path: testFile, + edits: [{ oldText: "nonexistent", newText: "testing" }], + }), + ).rejects.toThrow(/Could not find the exact text/); + }); + + it("should include ENOENT when the edit target does not exist", async () => { + const missingFile = join(testDir, "missing.txt"); + + await expect( + editTool.execute("test-call-6b", { + path: missingFile, + edits: [{ oldText: "hello", newText: "world" }], + }), + ).rejects.toThrow(`Could not edit file: ${missingFile}. Error code: ENOENT.`); + }); + + it("should fail if text appears multiple times", async () => { + const testFile = join(testDir, "edit-test.txt"); + const originalContent = "foo foo foo"; + writeFileSync(testFile, originalContent); + + await expect( + editTool.execute("test-call-7", { + path: testFile, + edits: [{ oldText: "foo", newText: "bar" }], + }), + ).rejects.toThrow(/Found 3 occurrences/); + }); + + it("should replace multiple disjoint regions in one call", async () => { + const testFile = join(testDir, "edit-multi.txt"); + writeFileSync(testFile, "alpha\nbeta\ngamma\ndelta\n"); + + const result = await editTool.execute("test-call-8", { + path: testFile, + edits: [ + { oldText: "alpha\n", newText: "ALPHA\n" }, + { oldText: "gamma\n", newText: "GAMMA\n" }, + ], + }); + + expect(getTextOutput(result)).toContain("Successfully replaced 2 block(s)"); + expect(readFileSync(testFile, "utf-8")).toBe("ALPHA\nbeta\nGAMMA\ndelta\n"); + expect(result.details?.diff).toContain("ALPHA"); + expect(result.details?.diff).toContain("GAMMA"); + }); + + it("should collapse large unchanged gaps in multi-edit diffs", async () => { + const testFile = join(testDir, "edit-multi-large-gap.txt"); + const lines = Array.from({ length: 600 }, (_, i) => `line ${String(i + 1).padStart(3, "0")}`); + writeFileSync(testFile, `${lines.join("\n")}\n`); + + const result = await editTool.execute("test-call-8b", { + path: testFile, + edits: [ + { oldText: "line 100\n", newText: "LINE 100\n" }, + { oldText: "line 300\n", newText: "LINE 300\n" }, + { oldText: "line 500\n", newText: "LINE 500\n" }, + ], + }); + + const diff = result.details?.diff ?? ""; + expect(diff).toContain("LINE 100"); + expect(diff).toContain("LINE 300"); + expect(diff).toContain("LINE 500"); + expect(diff).toContain("..."); + expect(diff).not.toContain("line 250"); + expect(diff.split("\n").length).toBeLessThan(50); + }); + + it("should match edits against the original file, not incrementally", async () => { + const testFile = join(testDir, "edit-multi-original.txt"); + writeFileSync(testFile, "foo\nbar\nbaz\n"); + + await editTool.execute("test-call-9", { + path: testFile, + edits: [ + { oldText: "foo\n", newText: "foo bar\n" }, + { oldText: "bar\n", newText: "BAR\n" }, + ], + }); + + expect(readFileSync(testFile, "utf-8")).toBe("foo bar\nBAR\nbaz\n"); + }); + + it("should fail when edits is empty", async () => { + const testFile = join(testDir, "edit-empty-edits.txt"); + writeFileSync(testFile, "hello\nworld\n"); + + await expect( + editTool.execute("test-call-11", { + path: testFile, + edits: [], + }), + ).rejects.toThrow(/edits must contain at least one replacement/); + }); + + it("should fail when multi-edit regions overlap", async () => { + const testFile = join(testDir, "edit-overlap.txt"); + writeFileSync(testFile, "one\ntwo\nthree\n"); + + await expect( + editTool.execute("test-call-12", { + path: testFile, + edits: [ + { oldText: "one\ntwo\n", newText: "ONE\nTWO\n" }, + { oldText: "two\nthree\n", newText: "TWO\nTHREE\n" }, + ], + }), + ).rejects.toThrow(/overlap/); + }); + + it("should not partially apply edits when one edit fails", async () => { + const testFile = join(testDir, "edit-no-partial.txt"); + const originalContent = "alpha\nbeta\ngamma\n"; + writeFileSync(testFile, originalContent); + + await expect( + editTool.execute("test-call-13", { + path: testFile, + edits: [ + { oldText: "alpha\n", newText: "ALPHA\n" }, + { oldText: "missing\n", newText: "MISSING\n" }, + ], + }), + ).rejects.toThrow(/Could not find/); + + expect(readFileSync(testFile, "utf-8")).toBe(originalContent); + }); + + it("should include EACCES for read-only files", async () => { + const testFile = join(testDir, "edit-readonly.txt"); + writeFileSync(testFile, "hello\n"); + chmodSync(testFile, 0o444); + + await expect( + editTool.execute("test-call-14", { + path: testFile, + edits: [{ oldText: "hello", newText: "world" }], + }), + ).rejects.toThrow(`Could not edit file: ${testFile}. Error code: EACCES.`); + }); + + it("should include the original error message for unknown edit access errors", async () => { + const genericFailureTool = createEditTool(testDir, { + operations: { + access: async () => { + throw new Error("disk offline"); + }, + readFile: async () => Buffer.from("hello\n", "utf-8"), + writeFile: async () => {}, + }, + }); + + await expect( + genericFailureTool.execute("test-call-16", { + path: "broken.txt", + edits: [{ oldText: "hello", newText: "world" }], + }), + ).rejects.toThrow("Could not edit file: broken.txt. Error: disk offline."); + }); + + it("should include ENOENT in diff preview for missing files", async () => { + const missingFile = join(testDir, "missing-preview.txt"); + const result = await computeEditsDiff(missingFile, [{ oldText: "hello", newText: "world" }], testDir); + + expect(result).toEqual({ error: `Could not edit file: ${missingFile}. Error code: ENOENT.` }); + }); + + it("should include EACCES in diff preview for unreadable files", async () => { + const unreadableFile = join(testDir, "unreadable-preview.txt"); + writeFileSync(unreadableFile, "hello\n"); + chmodSync(unreadableFile, 0o222); + + const result = await computeEditsDiff(unreadableFile, [{ oldText: "hello", newText: "world" }], testDir); + + expect(result).toEqual({ error: `Could not edit file: ${unreadableFile}. Error code: EACCES.` }); + }); + }); + + describe("bash tool", () => { + it("should execute simple commands", async () => { + const result = await bashTool.execute("test-call-8", { command: "echo 'test output'" }); + + expect(getTextOutput(result)).toContain("test output"); + expect(result.details).toBeUndefined(); + }); + + it("should handle command errors", async () => { + await expect(bashTool.execute("test-call-9", { command: "exit 1" })).rejects.toThrow( + /(Command failed|code 1)/, + ); + }); + + it("should respect timeout", async () => { + const command = `${JSON.stringify(process.execPath)} -e ${JSON.stringify("setInterval(() => {}, 1000)")}`; + await expect(bashTool.execute("test-call-10", { command, timeout: 0.05 })).rejects.toThrow(/timed out/i); + }); + + it("should include full output path for truncated timeout and abort errors", async () => { + for (const testCase of [ + { error: "timeout:5", expected: "Command timed out after 5 seconds" }, + { error: "aborted", expected: "Command aborted" }, + ]) { + const operations: BashOperations = { + exec: async (_command, _cwd, { onData }) => { + for (let i = 1; i <= 3000; i++) { + onData(Buffer.from(`${i}\n`, "utf-8")); + } + throw new Error(testCase.error); + }, + }; + const bash = createBashTool(testDir, { operations }); + + let error: unknown; + try { + await bash.execute(`test-call-${testCase.error}`, { command: "chatty-fail" }); + } catch (err) { + error = err; + } + + expect(error).toBeInstanceOf(Error); + const message = (error as Error).message; + expect(message).toContain(testCase.expected); + expect(message).toMatch(/\[Showing lines \d+-\d+ of \d+\. Full output: /); + expect(message).not.toContain("Full output: undefined"); + const fullOutputPath = message.match(/Full output: ([^\]\n]+)/)?.[1]; + expect(fullOutputPath).toBeDefined(); + expect(existsSync(fullOutputPath!)).toBe(true); + const fullOutput = readFileSync(fullOutputPath!, "utf-8"); + expect(fullOutput).toContain("1\n2\n3"); + expect(fullOutput).toContain("2998\n2999\n3000"); + } + }); + + it("should throw error when cwd does not exist", async () => { + const nonexistentCwd = "/this/directory/definitely/does/not/exist/12345"; + + const bashToolWithBadCwd = createBashTool(nonexistentCwd); + + await expect(bashToolWithBadCwd.execute("test-call-11", { command: "echo test" })).rejects.toThrow( + /Working directory does not exist/, + ); + }); + + it("should handle process spawn errors", async () => { + vi.spyOn(shellModule, "getShellConfig").mockReturnValueOnce({ + shell: "/nonexistent-shell-path-xyz123", + args: ["-c"], + }); + + const bashWithBadShell = createBashTool(testDir); + + await expect(bashWithBadShell.execute("test-call-12", { command: "echo test" })).rejects.toThrow(/ENOENT/); + }); + + it("should pass shellPath through to shell resolution", async () => { + const getShellConfigSpy = vi.spyOn(shellModule, "getShellConfig"); + const bashWithCustomShell = createBashTool(testDir, { + shellPath: "/custom/bash", + operations: { + exec: async () => ({ exitCode: 0 }), + }, + }); + + await bashWithCustomShell.execute("test-call-12b", { command: "echo test" }); + + expect(getShellConfigSpy).not.toHaveBeenCalled(); + + const ops = createLocalBashOperations({ shellPath: "/custom/bash" }); + await expect( + ops.exec("echo test", testDir, { + onData: () => {}, + }), + ).rejects.toThrow("Custom shell path not found: /custom/bash"); + expect(getShellConfigSpy).toHaveBeenCalledWith("/custom/bash"); + }); + + it("should send commands over stdin when shell resolution requires it", async () => { + vi.spyOn(shellModule, "getShellConfig").mockReturnValue({ + shell: process.execPath, + args: [ + "-e", + 'let input = ""; process.stdin.setEncoding("utf8"); process.stdin.on("data", (chunk) => { input += chunk; }); process.stdin.on("end", () => { process.stdout.write(input); });', + ], + commandTransport: "stdin", + }); + const chunks: Buffer[] = []; + const ops = createLocalBashOperations({ shellPath: "C:\\Windows\\System32\\bash.exe" }); + const nameExpansion = "$" + "{name}"; + const countExpansion = "$" + "{count}"; + const iExpansion = "$" + "{i}"; + const command = `name='World'; echo "Hello, ${nameExpansion}!"; count=3; for i in $(seq 1 ${countExpansion}); do echo "Iteration ${iExpansion} of ${countExpansion}"; done`; + + const result = await ops.exec(command, testDir, { + onData: (data) => chunks.push(data), + }); + + expect(result.exitCode).toBe(0); + expect(Buffer.concat(chunks).toString("utf-8")).toBe(command); + }); + + it("should resolve legacy WSL bash.exe to stdin command transport", () => { + if (process.platform === "win32") return; + const originalCwd = process.cwd(); + const platformDescriptor = Object.getOwnPropertyDescriptor(process, "platform"); + const shellPath = "C:\\Windows\\System32\\bash.exe"; + writeFileSync(join(testDir, shellPath), ""); + try { + process.chdir(testDir); + Object.defineProperty(process, "platform", { + configurable: true, + value: "win32", + }); + + expect(shellModule.getShellConfig(shellPath)).toEqual({ + shell: shellPath, + args: ["-s"], + commandTransport: "stdin", + }); + } finally { + process.chdir(originalCwd); + if (platformDescriptor) { + Object.defineProperty(process, "platform", platformDescriptor); + } + } + }); + + it("should prepend command prefix when configured", async () => { + const bashWithPrefix = createBashTool(testDir, { + commandPrefix: "export TEST_VAR=hello", + }); + + const result = await bashWithPrefix.execute("test-prefix-1", { command: "echo $TEST_VAR" }); + expect(getTextOutput(result).trim()).toBe("hello"); + }); + + it("should include output from both prefix and command", async () => { + const bashWithPrefix = createBashTool(testDir, { + commandPrefix: "echo prefix-output", + }); + + const result = await bashWithPrefix.execute("test-prefix-2", { command: "echo command-output" }); + expect(getTextOutput(result).trim()).toBe("prefix-output\ncommand-output"); + }); + + it("should work without command prefix", async () => { + const bashWithoutPrefix = createBashTool(testDir, {}); + + const result = await bashWithoutPrefix.execute("test-prefix-3", { command: "echo no-prefix" }); + expect(getTextOutput(result).trim()).toBe("no-prefix"); + }); + + it("should coalesce streaming updates for chatty output", async () => { + const operations: BashOperations = { + exec: async (_command, _cwd, { onData }) => { + for (let i = 0; i < 5000; i++) { + onData(Buffer.from(`line ${i}\n`, "utf-8")); + } + return { exitCode: 0 }; + }, + }; + const updates: Array<{ content: Array<{ type: string; text?: string }>; details?: unknown }> = []; + const bash = createBashTool(testDir, { operations }); + + const result = await bash.execute("test-call-chatty-updates", { command: "chatty" }, undefined, (update) => + updates.push(update), + ); + + expect(updates.length).toBeLessThan(25); + expect(getTextOutput(result)).toContain("line 4999"); + }); + + it("should not count a trailing newline as an extra truncated bash output line", async () => { + const operations: BashOperations = { + exec: async (_command, _cwd, { onData }) => { + for (let i = 1; i <= 4000; i++) { + onData(Buffer.from(`line-${String(i).padStart(4, "0")}\n`, "utf-8")); + } + return { exitCode: 0 }; + }, + }; + const bash = createBashTool(testDir, { operations }); + + const result = await bash.execute("test-call-trailing-newline-line-count", { command: "many-lines" }); + const output = getTextOutput(result); + + expect(result.details?.truncation?.totalLines).toBe(4000); + expect(result.details?.truncation?.outputLines).toBe(2000); + expect(output).toContain("line-2001"); + expect(output).toContain("line-4000"); + expect(output).toMatch(/\[Showing lines 2001-4000 of 4000\. Full output: /); + expect(output).not.toContain("4001"); + }); + + it("should decode UTF-8 characters split across output chunks", async () => { + const euro = Buffer.from("€\n", "utf-8"); + const operations: BashOperations = { + exec: async (_command, _cwd, { onData }) => { + onData(euro.subarray(0, 1)); + onData(euro.subarray(1)); + return { exitCode: 0 }; + }, + }; + const bash = createBashTool(testDir, { operations }); + + const result = await bash.execute("test-call-split-utf8", { command: "split-utf8" }); + + expect(getTextOutput(result).trim()).toBe("€"); + }); + + it("should expose local bash operations for extension reuse", async () => { + const ops = createLocalBashOperations(); + const chunks: Buffer[] = []; + + const result = await ops.exec("echo $TEST_LOCAL_BASH_OPS", testDir, { + onData: (data) => chunks.push(data), + env: { ...process.env, TEST_LOCAL_BASH_OPS: "from-local-ops" }, + }); + + expect(result.exitCode).toBe(0); + expect(Buffer.concat(chunks).toString("utf-8").trim()).toBe("from-local-ops"); + }); + + it("should preserve executeBash sanitization when using local bash operations", async () => { + const result = await executeBashWithOperations( + "printf '\\033[31mred\\033[0m\\r\\n'", + process.cwd(), + createLocalBashOperations(), + ); + + expect(result.exitCode).toBe(0); + expect(result.output).toBe("red\n"); + }); + + it("should persist full output when truncation happens by line count only", async () => { + const bash = createBashTool(testDir); + const result = await bash.execute("test-call-line-truncation", { command: "seq 3000" }); + const output = getTextOutput(result); + const fullOutputPath = result.details?.fullOutputPath; + + expect(result.details?.truncation?.truncated).toBe(true); + expect(result.details?.truncation?.truncatedBy).toBe("lines"); + expect(fullOutputPath).toBeDefined(); + expect(output).toMatch(/\[Showing lines \d+-\d+ of \d+\. Full output: /); + expect(output).not.toContain("Full output: undefined"); + + for (let i = 0; i < 20 && (!fullOutputPath || !existsSync(fullOutputPath)); i++) { + await new Promise((resolve) => setTimeout(resolve, 10)); + } + + expect(fullOutputPath).toBeDefined(); + expect(existsSync(fullOutputPath!)).toBe(true); + const fullOutput = readFileSync(fullOutputPath!, "utf-8"); + expect(fullOutput).toContain("1\n2\n3"); + expect(fullOutput).toContain("2998\n2999\n3000"); + }); + + it("executeBash should persist full output when truncation happens by line count only", async () => { + const result = await executeBashWithOperations("seq 3000", process.cwd(), createLocalBashOperations()); + const fullOutputPath = result.fullOutputPath; + + expect(result.truncated).toBe(true); + expect(fullOutputPath).toBeDefined(); + + for (let i = 0; i < 20 && (!fullOutputPath || !existsSync(fullOutputPath)); i++) { + await new Promise((resolve) => setTimeout(resolve, 10)); + } + + expect(fullOutputPath).toBeDefined(); + expect(existsSync(fullOutputPath!)).toBe(true); + const fullOutput = readFileSync(fullOutputPath!, "utf-8"); + expect(fullOutput).toContain("1\n2\n3"); + expect(fullOutput).toContain("2998\n2999\n3000"); + }); + }); + + describe("grep tool", () => { + it("should include filename when searching a single file", async () => { + const testFile = join(testDir, "example.txt"); + writeFileSync(testFile, "first line\nmatch line\nlast line"); + + const result = await grepTool.execute("test-call-11", { + pattern: "match", + path: testFile, + }); + + const output = getTextOutput(result); + expect(output).toContain("example.txt:2: match line"); + }); + + it("should respect global limit and include context lines", async () => { + const testFile = join(testDir, "context.txt"); + const content = ["before", "match one", "after", "middle", "match two", "after two"].join("\n"); + writeFileSync(testFile, content); + + const result = await grepTool.execute("test-call-12", { + pattern: "match", + path: testFile, + limit: 1, + context: 1, + }); + + const output = getTextOutput(result); + expect(output).toContain("context.txt-1- before"); + expect(output).toContain("context.txt:2: match one"); + expect(output).toContain("context.txt-3- after"); + expect(output).toContain("[1 matches limit reached. Use limit=2 for more, or refine pattern]"); + // Ensure second match is not present + expect(output).not.toContain("match two"); + }); + + it("should treat flag-like patterns as search text", async () => { + const marker = join(testDir, "grep-injection-marker"); + const payload = join(testDir, "payload.sh"); + const testFile = join(testDir, "target.txt"); + writeFileSync(payload, `#!/bin/sh\necho executed > ${marker}\ncat "$1"\n`); + chmodSync(payload, 0o755); + writeFileSync(testFile, "target\n"); + + const result = await grepTool.execute("test-call-grep-injection", { + pattern: `--pre=${payload}`, + path: testDir, + }); + + expect(getTextOutput(result)).toContain("No matches found"); + expect(existsSync(marker)).toBe(false); + }); + }); + + describe("find tool", () => { + it("should include hidden files that are not gitignored", async () => { + const hiddenDir = join(testDir, ".secret"); + mkdirSync(hiddenDir); + writeFileSync(join(hiddenDir, "hidden.txt"), "hidden"); + writeFileSync(join(testDir, "visible.txt"), "visible"); + + const result = await findTool.execute("test-call-13", { + pattern: "**/*.txt", + path: testDir, + }); + + const outputLines = getTextOutput(result) + .split("\n") + .map((line) => line.trim()) + .filter(Boolean); + + expect(outputLines).toContain("visible.txt"); + expect(outputLines).toContain(".secret/hidden.txt"); + }); + + it("should respect .gitignore", async () => { + writeFileSync(join(testDir, ".gitignore"), "ignored.txt\n"); + writeFileSync(join(testDir, "ignored.txt"), "ignored"); + writeFileSync(join(testDir, "kept.txt"), "kept"); + + const result = await findTool.execute("test-call-14", { + pattern: "**/*.txt", + path: testDir, + }); + + const output = getTextOutput(result); + expect(output).toContain("kept.txt"); + expect(output).not.toContain("ignored.txt"); + }); + + it("should surface fd glob parse errors", async () => { + await expect( + findTool.execute("test-call-15", { + pattern: "[", + path: testDir, + }), + ).rejects.toThrow(/error parsing glob|fd exited with code 1|fd error/i); + }); + + it("should treat flag-like patterns as search text", async () => { + const result = await findTool.execute("test-call-find-flag-pattern", { + pattern: "--help", + path: testDir, + }); + + expect(getTextOutput(result)).toContain("No files found matching pattern"); + }); + }); + + describe("ls tool", () => { + it("should list dotfiles and directories", async () => { + writeFileSync(join(testDir, ".hidden-file"), "secret"); + mkdirSync(join(testDir, ".hidden-dir")); + + const result = await lsTool.execute("test-call-15", { path: testDir }); + const output = getTextOutput(result); + + expect(output).toContain(".hidden-file"); + expect(output).toContain(".hidden-dir/"); + }); + }); +}); + +function fakeCtx(cwd: string): ExtensionContext { + return { cwd } as ExtensionContext; +} + +describe("tool cwd resolution", () => { + let testDir: string; + + beforeEach(() => { + testDir = join(tmpdir(), `coding-agent-cwd-test-${Date.now()}`); + mkdirSync(testDir, { recursive: true }); + }); + + afterEach(() => { + rmSync(testDir, { recursive: true, force: true }); + }); + + it("read uses ctx.cwd when provided", async () => { + const testFile = join(testDir, "ctx-cwd-read.txt"); + writeFileSync(testFile, "hello from ctx.cwd"); + const tool = createReadToolDefinition("/"); + const result = await tool.execute( + "test-read-ctx-cwd", + { path: "ctx-cwd-read.txt" }, + undefined, + undefined, + fakeCtx(testDir), + ); + const output = getTextOutput(result); + expect(output).toContain("hello from ctx.cwd"); + }); + + it("write uses ctx.cwd when provided", async () => { + const tool = createWriteToolDefinition("/"); + await tool.execute( + "test-write-ctx-cwd", + { path: "ctx-cwd-write.txt", content: "written via ctx.cwd" }, + undefined, + undefined, + fakeCtx(testDir), + ); + const content = readFileSync(join(testDir, "ctx-cwd-write.txt"), "utf-8"); + expect(content).toBe("written via ctx.cwd"); + }); + + it("edit uses ctx.cwd when provided", async () => { + const testFile = join(testDir, "ctx-cwd-edit.txt"); + writeFileSync(testFile, "old text"); + const tool = createEditToolDefinition("/"); + await tool.execute( + "test-edit-ctx-cwd", + { path: "ctx-cwd-edit.txt", edits: [{ oldText: "old text", newText: "new text" }] }, + undefined, + undefined, + fakeCtx(testDir), + ); + const content = readFileSync(testFile, "utf-8"); + expect(content).toBe("new text"); + }); + + it("grep uses ctx.cwd when provided", async () => { + const testFile = join(testDir, "ctx-cwd-grep.txt"); + writeFileSync(testFile, "match in ctx.cwd"); + const tool = createGrepToolDefinition("/"); + const result = await tool.execute( + "test-grep-ctx-cwd", + { pattern: "match" }, + undefined, + undefined, + fakeCtx(testDir), + ); + const output = getTextOutput(result); + expect(output).toContain("ctx-cwd-grep.txt"); + }); + + it("find uses ctx.cwd when provided", async () => { + writeFileSync(join(testDir, "ctx-cwd-find.txt"), "find me"); + const tool = createFindToolDefinition("/"); + const result = await tool.execute( + "test-find-ctx-cwd", + { pattern: "ctx-cwd-find.txt" }, + undefined, + undefined, + fakeCtx(testDir), + ); + const output = getTextOutput(result); + expect(output).toContain("ctx-cwd-find.txt"); + }); + + it("ls uses ctx.cwd when provided", async () => { + writeFileSync(join(testDir, "ctx-cwd-ls.txt"), "list me"); + const tool = createLsToolDefinition("/"); + const result = await tool.execute("test-ls-ctx-cwd", {}, undefined, undefined, fakeCtx(testDir)); + const output = getTextOutput(result); + expect(output).toContain("ctx-cwd-ls.txt"); + }); + + it("bash uses ctx.cwd when provided", async () => { + const tool = createBashToolDefinition("/", { exposeSessionEnvironment: false }); + const result = await tool.execute( + "test-bash-ctx-cwd", + { command: "pwd" }, + undefined, + undefined, + fakeCtx(testDir), + ); + const output = getTextOutput(result); + expect(output).toContain(testDir); + }); +}); + +describe("edit tool fuzzy matching", () => { + let testDir: string; + + beforeEach(() => { + testDir = join(tmpdir(), `coding-agent-fuzzy-test-${Date.now()}`); + mkdirSync(testDir, { recursive: true }); + }); + + afterEach(() => { + rmSync(testDir, { recursive: true, force: true }); + }); + + it("should match text with trailing whitespace stripped", async () => { + const testFile = join(testDir, "trailing-ws.txt"); + // File has trailing spaces on lines + writeFileSync(testFile, "line one \nline two \nline three\n"); + + // oldText without trailing whitespace should still match + const result = await editTool.execute("test-fuzzy-1", { + path: testFile, + edits: [{ oldText: "line one\nline two\n", newText: "replaced\n" }], + }); + + expect(getTextOutput(result)).toContain("Successfully replaced"); + const content = readFileSync(testFile, "utf-8"); + expect(content).toBe("replaced\nline three\n"); + }); + + it("should match fullwidth punctuation in Chinese text", async () => { + const testFile = join(testDir, "chinese-punctuation.txt"); + writeFileSync(testFile, "你好,世界\n你好(世界)\n"); + + const result = await editTool.execute("test-fuzzy-chinese", { + path: testFile, + edits: [{ oldText: "你好,世界\n你好(世界)\n", newText: "你好,pi\n你好(pi)\n" }], + }); + + expect(getTextOutput(result)).toContain("Successfully replaced"); + const content = readFileSync(testFile, "utf-8"); + expect(content).toBe("你好,pi\n你好(pi)\n"); + }); + + it("should match compatibility-equivalent Unicode forms", async () => { + const testFile = join(testDir, "unicode-compatibility.txt"); + writeFileSync(testFile, "ABC123\ncafe\u0301\n"); + + const result = await editTool.execute("test-fuzzy-unicode", { + path: testFile, + edits: [{ oldText: "ABC123\ncafé\n", newText: "XYZ789\ncoffee\n" }], + }); + + expect(getTextOutput(result)).toContain("Successfully replaced"); + const content = readFileSync(testFile, "utf-8"); + expect(content).toBe("XYZ789\ncoffee\n"); + }); + + it("should match smart single quotes to ASCII quotes", async () => { + const testFile = join(testDir, "smart-quotes.txt"); + // File has smart/curly single quotes (U+2018, U+2019) + writeFileSync(testFile, "console.log(\u2018hello\u2019);\n"); + + // oldText with ASCII quotes should match + const result = await editTool.execute("test-fuzzy-2", { + path: testFile, + edits: [{ oldText: "console.log('hello');", newText: "console.log('world');" }], + }); + + expect(getTextOutput(result)).toContain("Successfully replaced"); + const content = readFileSync(testFile, "utf-8"); + expect(content).toContain("world"); + }); + + it("should match smart double quotes to ASCII quotes", async () => { + const testFile = join(testDir, "smart-double-quotes.txt"); + // File has smart/curly double quotes (U+201C, U+201D) + writeFileSync(testFile, "const msg = \u201CHello World\u201D;\n"); + + // oldText with ASCII quotes should match + const result = await editTool.execute("test-fuzzy-3", { + path: testFile, + edits: [{ oldText: 'const msg = "Hello World";', newText: 'const msg = "Goodbye";' }], + }); + + expect(getTextOutput(result)).toContain("Successfully replaced"); + const content = readFileSync(testFile, "utf-8"); + expect(content).toContain("Goodbye"); + }); + + it("should match Unicode dashes to ASCII hyphen", async () => { + const testFile = join(testDir, "unicode-dashes.txt"); + // File has en-dash (U+2013) and em-dash (U+2014) + writeFileSync(testFile, "range: 1\u20135\nbreak\u2014here\n"); + + // oldText with ASCII hyphens should match + const result = await editTool.execute("test-fuzzy-4", { + path: testFile, + edits: [{ oldText: "range: 1-5\nbreak-here", newText: "range: 10-50\nbreak--here" }], + }); + + expect(getTextOutput(result)).toContain("Successfully replaced"); + const content = readFileSync(testFile, "utf-8"); + expect(content).toContain("10-50"); + }); + + it("should match non-breaking space to regular space", async () => { + const testFile = join(testDir, "nbsp.txt"); + // File has non-breaking space (U+00A0) + writeFileSync(testFile, "hello\u00A0world\n"); + + // oldText with regular space should match + const result = await editTool.execute("test-fuzzy-5", { + path: testFile, + edits: [{ oldText: "hello world", newText: "hello universe" }], + }); + + expect(getTextOutput(result)).toContain("Successfully replaced"); + const content = readFileSync(testFile, "utf-8"); + expect(content).toContain("universe"); + }); + + it("should prefer exact match over fuzzy match", async () => { + const testFile = join(testDir, "exact-preferred.txt"); + // File has both exact and fuzzy-matchable content + writeFileSync(testFile, "const x = 'exact';\nconst y = 'other';\n"); + + const result = await editTool.execute("test-fuzzy-6", { + path: testFile, + edits: [{ oldText: "const x = 'exact';", newText: "const x = 'changed';" }], + }); + + expect(getTextOutput(result)).toContain("Successfully replaced"); + const content = readFileSync(testFile, "utf-8"); + expect(content).toBe("const x = 'changed';\nconst y = 'other';\n"); + }); + + it("should still fail when text is not found even with fuzzy matching", async () => { + const testFile = join(testDir, "no-match.txt"); + writeFileSync(testFile, "completely different content\n"); + + await expect( + editTool.execute("test-fuzzy-7", { + path: testFile, + edits: [{ oldText: "this does not exist", newText: "replacement" }], + }), + ).rejects.toThrow(/Could not find the exact text/); + }); + + it("should detect duplicates after fuzzy normalization", async () => { + const testFile = join(testDir, "fuzzy-dups.txt"); + // Two lines that are identical after trailing whitespace is stripped + writeFileSync(testFile, "hello world \nhello world\n"); + + await expect( + editTool.execute("test-fuzzy-8", { + path: testFile, + edits: [{ oldText: "hello world", newText: "replaced" }], + }), + ).rejects.toThrow(/Found 2 occurrences/); + }); + + it("should support fuzzy matching in multi-edit mode", async () => { + const testFile = join(testDir, "fuzzy-multi.txt"); + writeFileSync(testFile, "console.log(\u2018hello\u2019);\nhello\u00A0world\n"); + + await editTool.execute("test-fuzzy-9", { + path: testFile, + edits: [ + { oldText: "console.log('hello');\n", newText: "console.log('world');\n" }, + { oldText: "hello world\n", newText: "hello universe\n" }, + ], + }); + + expect(readFileSync(testFile, "utf-8")).toBe("console.log('world');\nhello universe\n"); + }); + + it("should preserve the correct occurrence when fuzzy replacement equals a nearby line", async () => { + const testFile = join(testDir, "fuzzy-preserve-duplicate-line.txt"); + const originalContent = ["replace me\u0020\u0020\u0020", "after\u0020\u0020\u0020", ""].join("\n"); + writeFileSync(testFile, originalContent); + + const result = await editTool.execute("test-fuzzy-preserve-duplicate-line", { + path: testFile, + edits: [{ oldText: "replace me\n", newText: "after\n" }], + }); + + const expectedContent = ["after", "after\u0020\u0020\u0020", ""].join("\n"); + expect(readFileSync(testFile, "utf-8")).toBe(expectedContent); + expect(applyPatch(originalContent, result.details?.patch ?? "")).toBe(expectedContent); + }); + + it("should preserve untouched lines and produce an applicable patch for fuzzy multi-edits", async () => { + const testFile = join(testDir, "fuzzy-preserve-multi.txt"); + const originalContent = [ + "keep before\u0020\u0020", + "first target\u0020\u0020", + "first after", + "keep middle\u0020\u0020\u0020", + "second target\u0020\u0020", + "second after", + "keep after\u0020\u0020", + "", + ].join("\n"); + writeFileSync(testFile, originalContent); + + const result = await editTool.execute("test-fuzzy-preserve-multi", { + path: testFile, + edits: [ + { oldText: "first target\nfirst after", newText: "FIRST\nFIRST2" }, + { oldText: "second target\nsecond after", newText: "SECOND\nSECOND2" }, + ], + }); + + const expectedContent = [ + "keep before\u0020\u0020", + "FIRST", + "FIRST2", + "keep middle\u0020\u0020\u0020", + "SECOND", + "SECOND2", + "keep after\u0020\u0020", + "", + ].join("\n"); + expect(readFileSync(testFile, "utf-8")).toBe(expectedContent); + expect(applyPatch(originalContent, result.details?.patch ?? "")).toBe(expectedContent); + }); +}); + +describe("edit tool CRLF handling", () => { + let testDir: string; + + beforeEach(() => { + testDir = join(tmpdir(), `coding-agent-crlf-test-${Date.now()}`); + mkdirSync(testDir, { recursive: true }); + }); + + afterEach(() => { + rmSync(testDir, { recursive: true, force: true }); + }); + + it("should match LF oldText against CRLF file content", async () => { + const testFile = join(testDir, "crlf-test.txt"); + + writeFileSync(testFile, "line one\r\nline two\r\nline three\r\n"); + + const result = await editTool.execute("test-crlf-1", { + path: testFile, + edits: [{ oldText: "line two\n", newText: "replaced line\n" }], + }); + + expect(getTextOutput(result)).toContain("Successfully replaced"); + }); + + it("should preserve CRLF line endings after edit", async () => { + const testFile = join(testDir, "crlf-preserve.txt"); + writeFileSync(testFile, "first\r\nsecond\r\nthird\r\n"); + + await editTool.execute("test-crlf-2", { + path: testFile, + edits: [{ oldText: "second\n", newText: "REPLACED\n" }], + }); + + const content = readFileSync(testFile, "utf-8"); + expect(content).toBe("first\r\nREPLACED\r\nthird\r\n"); + }); + + it("should preserve LF line endings for LF files", async () => { + const testFile = join(testDir, "lf-preserve.txt"); + writeFileSync(testFile, "first\nsecond\nthird\n"); + + await editTool.execute("test-lf-1", { + path: testFile, + edits: [{ oldText: "second\n", newText: "REPLACED\n" }], + }); + + const content = readFileSync(testFile, "utf-8"); + expect(content).toBe("first\nREPLACED\nthird\n"); + }); + + it("should detect duplicates across CRLF/LF variants", async () => { + const testFile = join(testDir, "mixed-endings.txt"); + + writeFileSync(testFile, "hello\r\nworld\r\n---\r\nhello\nworld\n"); + + await expect( + editTool.execute("test-crlf-dup", { + path: testFile, + edits: [{ oldText: "hello\nworld\n", newText: "replaced\n" }], + }), + ).rejects.toThrow(/Found 2 occurrences/); + }); + + it("should preserve UTF-8 BOM after edit", async () => { + const testFile = join(testDir, "bom-test.txt"); + writeFileSync(testFile, "\uFEFFfirst\r\nsecond\r\nthird\r\n"); + + await editTool.execute("test-bom", { + path: testFile, + edits: [{ oldText: "second\n", newText: "REPLACED\n" }], + }); + + const content = readFileSync(testFile, "utf-8"); + expect(content).toBe("\uFEFFfirst\r\nREPLACED\r\nthird\r\n"); + }); + + it("should preserve CRLF line endings and BOM in multi-edit mode", async () => { + const testFile = join(testDir, "bom-crlf-multi.txt"); + writeFileSync(testFile, "\uFEFFfirst\r\nsecond\r\nthird\r\nfourth\r\n"); + + await editTool.execute("test-crlf-multi", { + path: testFile, + edits: [ + { oldText: "second\n", newText: "SECOND\n" }, + { oldText: "fourth\n", newText: "FOURTH\n" }, + ], + }); + + const content = readFileSync(testFile, "utf-8"); + expect(content).toBe("\uFEFFfirst\r\nSECOND\r\nthird\r\nFOURTH\r\n"); + }); +}); diff --git a/packages/coding-agent/test/tree-selector.test.ts b/packages/coding-agent/test/tree-selector.test.ts new file mode 100644 index 0000000000000000000000000000000000000000..9dc750d5de8eb4414db52d1293c01e09cba1445f --- /dev/null +++ b/packages/coding-agent/test/tree-selector.test.ts @@ -0,0 +1,702 @@ +import { stripVTControlCharacters } from "node:util"; +import { setKeybindings, visibleWidth } from "@earendil-works/pi-tui"; +import { beforeAll, beforeEach, describe, expect, test } from "vitest"; +import { KeybindingsManager } from "../src/core/keybindings.ts"; +import type { + ModelChangeEntry, + SessionEntry, + SessionMessageEntry, + SessionTreeNode, +} from "../src/core/session-manager.ts"; +import { TreeSelectorComponent } from "../src/modes/interactive/components/tree-selector.ts"; +import { initTheme } from "../src/modes/interactive/theme/theme.ts"; + +beforeAll(() => { + initTheme("dark"); +}); + +beforeEach(() => { + // Ensure test isolation: keybindings are a global singleton + setKeybindings(new KeybindingsManager()); +}); + +// Helper to create a user message entry +function userMessage(id: string, parentId: string | null, content: string): SessionMessageEntry { + return { + type: "message", + id, + parentId, + timestamp: new Date().toISOString(), + message: { role: "user", content, timestamp: Date.now() }, + }; +} + +// Helper to create an assistant message entry +function assistantMessage(id: string, parentId: string | null, text: string): SessionMessageEntry { + return { + type: "message", + id, + parentId, + timestamp: new Date().toISOString(), + message: { + role: "assistant", + content: [{ type: "text", text }], + api: "anthropic-messages", + provider: "anthropic", + model: "claude-sonnet-4", + usage: { + input: 0, + output: 0, + cacheRead: 0, + cacheWrite: 0, + totalTokens: 0, + cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 }, + }, + stopReason: "stop", + timestamp: Date.now(), + }, + }; +} + +// Helper to create a tool-call-only assistant message (filtered out in default mode) +function toolCallOnlyAssistant(id: string, parentId: string | null): SessionMessageEntry { + return { + type: "message", + id, + parentId, + timestamp: new Date().toISOString(), + message: { + role: "assistant", + content: [{ type: "toolCall", id: `tc-${id}`, name: "read", arguments: { path: "test.ts" } }], + api: "anthropic-messages", + provider: "anthropic", + model: "claude-sonnet-4", + usage: { + input: 0, + output: 0, + cacheRead: 0, + cacheWrite: 0, + totalTokens: 0, + cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 }, + }, + stopReason: "toolUse", + timestamp: Date.now(), + }, + }; +} + +// Helper to create a model_change entry +function modelChange(id: string, parentId: string | null): ModelChangeEntry { + return { + type: "model_change", + id, + parentId, + timestamp: new Date().toISOString(), + provider: "anthropic", + modelId: "claude-sonnet-4", + }; +} + +// Helper to build a tree from entries using parentId relationships +function buildTree(entries: Array): SessionTreeNode[] { + if (entries.length === 0) return []; + + const nodes: SessionTreeNode[] = entries.map((entry) => ({ + entry, + children: [], + })); + + const byId = new Map(); + for (const node of nodes) { + byId.set(node.entry.id, node); + } + + const roots: SessionTreeNode[] = []; + for (const node of nodes) { + if (node.entry.parentId === null) { + roots.push(node); + } else { + const parent = byId.get(node.entry.parentId); + if (parent) { + parent.children.push(node); + } + } + } + return roots; +} + +describe("TreeSelectorComponent", () => { + describe("initial selection with metadata entries", () => { + test("focuses nearest visible ancestor when currentLeafId is a model_change with sibling branch", () => { + // Tree structure: + // user-1 + // └── asst-1 + // ├── user-2 (active branch) + // │ └── model-1 (model_change, CURRENT LEAF) + // └── user-3 (sibling branch, added later chronologically) + const entries = [ + userMessage("user-1", null, "hello"), + assistantMessage("asst-1", "user-1", "hi"), + userMessage("user-2", "asst-1", "active branch"), // Active branch + modelChange("model-1", "user-2"), // Current leaf (metadata) + userMessage("user-3", "asst-1", "sibling branch"), // Sibling branch + ]; + const tree = buildTree(entries); + + const selector = new TreeSelectorComponent( + tree, + "model-1", // currentLeafId is the model_change entry + 24, + () => {}, + () => {}, + ); + + const list = selector.getTreeList(); + // Should focus on user-2 (parent of model-1), not user-3 (last item) + expect(list.getSelectedNode()?.entry.id).toBe("user-2"); + }); + + test("focuses nearest visible ancestor when currentLeafId is a thinking_level_change entry", () => { + // Similar structure with thinking_level_change instead of model_change + const entries = [ + userMessage("user-1", null, "hello"), + assistantMessage("asst-1", "user-1", "hi"), + userMessage("user-2", "asst-1", "active branch"), + { + type: "thinking_level_change" as const, + id: "thinking-1", + parentId: "user-2", + timestamp: new Date().toISOString(), + thinkingLevel: "high", + }, + userMessage("user-3", "asst-1", "sibling branch"), + ]; + const tree = buildTree(entries); + + const selector = new TreeSelectorComponent( + tree, + "thinking-1", + 24, + () => {}, + () => {}, + ); + + const list = selector.getTreeList(); + expect(list.getSelectedNode()?.entry.id).toBe("user-2"); + }); + }); + + describe("filter switching with parent traversal", () => { + test("switches to nearest visible user message when changing to user-only filter", () => { + // In user-only filter: [user-1, user-2, user-3] + const entries = [ + userMessage("user-1", null, "hello"), + assistantMessage("asst-1", "user-1", "hi"), + userMessage("user-2", "asst-1", "active branch"), + assistantMessage("asst-2", "user-2", "response"), + userMessage("user-3", "asst-1", "sibling branch"), + ]; + const tree = buildTree(entries); + + const selector = new TreeSelectorComponent( + tree, + "asst-2", + 24, + () => {}, + () => {}, + ); + + const list = selector.getTreeList(); + expect(list.getSelectedNode()?.entry.id).toBe("asst-2"); + + // Simulate Ctrl+U (user-only filter) + selector.handleInput("\x15"); + + // Should now be on user-2 (the parent user message), not user-3 + expect(list.getSelectedNode()?.entry.id).toBe("user-2"); + }); + + test("returns to nearest visible ancestor when switching back to default filter", () => { + // Same branching structure + const entries = [ + userMessage("user-1", null, "hello"), + assistantMessage("asst-1", "user-1", "hi"), + userMessage("user-2", "asst-1", "active branch"), + assistantMessage("asst-2", "user-2", "response"), + userMessage("user-3", "asst-1", "sibling branch"), + ]; + const tree = buildTree(entries); + + const selector = new TreeSelectorComponent( + tree, + "asst-2", + 24, + () => {}, + () => {}, + ); + + const list = selector.getTreeList(); + expect(list.getSelectedNode()?.entry.id).toBe("asst-2"); + + // Switch to user-only + selector.handleInput("\x15"); // Ctrl+U + expect(list.getSelectedNode()?.entry.id).toBe("user-2"); + + // Switch back to default - should stay on user-2 + // (since that's what we navigated to via parent traversal) + selector.handleInput("\x04"); // Ctrl+D + expect(list.getSelectedNode()?.entry.id).toBe("user-2"); + }); + }); + + describe("help", () => { + test("renders semantic help rows without truncating narrow terminal controls", () => { + const entries = [userMessage("user-1", null, "hello"), assistantMessage("asst-1", "user-1", "hi")]; + const tree = buildTree(entries); + const selector = new TreeSelectorComponent( + tree, + "asst-1", + 24, + () => {}, + () => {}, + ); + + const plainLines = selector.render(30).map(stripVTControlCharacters); + const plain = plainLines.join("\n"); + expect(plain).toContain("branch"); + expect(plain).toContain("copy"); + expect(plain).toContain("filters"); + expect(plain).toContain("cycle"); + expect(plain).toContain("label time"); + expect(plain).not.toContain("..."); + expect(plainLines.every((line) => visibleWidth(line) <= 30)).toBe(true); + }); + }); + + describe("copy", () => { + test("copies the full selected message with ctrl+x", () => { + const message = `${"long message ".repeat(30)}\nsecond line`; + const tree = buildTree([userMessage("user-1", null, "hello"), assistantMessage("asst-1", "user-1", message)]); + const selector = new TreeSelectorComponent( + tree, + "asst-1", + 24, + () => {}, + () => {}, + ); + let copied: string | undefined; + selector.onCopy = (text) => { + copied = text; + }; + + selector.handleInput("\x18"); + + expect(copied).toBe(message); + }); + }); + + describe("label timestamps", () => { + test("toggles label timestamps for labeled nodes", () => { + const entries = [userMessage("user-1", null, "hello"), assistantMessage("asst-1", "user-1", "hi")]; + const tree = buildTree(entries); + const labelDate = new Date(2026, 2, 28, 14, 32, 0); + tree[0]!.label = "checkpoint"; + tree[0]!.labelTimestamp = labelDate.toISOString(); + + const selector = new TreeSelectorComponent( + tree, + "asst-1", + 24, + () => {}, + () => {}, + ); + + const list = selector.getTreeList(); + let render = list.render(200).join("\n"); + expect(render).toContain("[checkpoint]"); + expect(render).not.toContain("3/28 14:32"); + expect(render).not.toContain("[+label time]"); + + selector.handleInput("T"); + + render = list.render(200).join("\n"); + expect(render).toContain("3/28 14:32"); + expect(render).toContain("[+label time]"); + }); + }); + + describe("empty filter preservation", () => { + test("preserves selection when switching to empty labeled filter and back", () => { + // Tree with no labels + const entries = [ + userMessage("user-1", null, "hello"), + assistantMessage("asst-1", "user-1", "hi"), + userMessage("user-2", "asst-1", "bye"), + assistantMessage("asst-2", "user-2", "goodbye"), + ]; + const tree = buildTree(entries); + + const selector = new TreeSelectorComponent( + tree, + "asst-2", + 24, + () => {}, + () => {}, + ); + + const list = selector.getTreeList(); + expect(list.getSelectedNode()?.entry.id).toBe("asst-2"); + + // Switch to labeled-only filter (no labels exist, so empty result) + selector.handleInput("\x0c"); // Ctrl+L + + // The list should be empty, getSelectedNode returns undefined + expect(list.getSelectedNode()).toBeUndefined(); + + // Switch back to default filter + selector.handleInput("\x04"); // Ctrl+D + + // Should restore to asst-2 (the selection before we switched to empty filter) + expect(list.getSelectedNode()?.entry.id).toBe("asst-2"); + }); + + test("preserves selection through multiple empty filter switches", () => { + const entries = [userMessage("user-1", null, "hello"), assistantMessage("asst-1", "user-1", "hi")]; + const tree = buildTree(entries); + + const selector = new TreeSelectorComponent( + tree, + "asst-1", + 24, + () => {}, + () => {}, + ); + + const list = selector.getTreeList(); + expect(list.getSelectedNode()?.entry.id).toBe("asst-1"); + + // Switch to labeled-only (empty) - Ctrl+L toggles labeled ↔ default + selector.handleInput("\x0c"); // Ctrl+L -> labeled-only + expect(list.getSelectedNode()).toBeUndefined(); + + // Switch to default, then back to labeled-only + selector.handleInput("\x0c"); // Ctrl+L -> default (toggle back) + expect(list.getSelectedNode()?.entry.id).toBe("asst-1"); + + selector.handleInput("\x0c"); // Ctrl+L -> labeled-only again + expect(list.getSelectedNode()).toBeUndefined(); + + // Switch back to default with Ctrl+D + selector.handleInput("\x04"); // Ctrl+D + expect(list.getSelectedNode()?.entry.id).toBe("asst-1"); + }); + }); + + describe("branch navigation and folding with ctrl+arrow keys", () => { + // Key escape sequences + const UP = "\x1b[A"; + const DOWN = "\x1b[B"; + const CTRL_LEFT = "\x1b[1;5D"; + const CTRL_RIGHT = "\x1b[1;5C"; + const ALT_LEFT = "\x1b[1;3D"; + const ALT_RIGHT = "\x1b[1;3C"; + + // Tree structure: + // + // user-1 + // asst-1 + // user-2 + // asst-2 ← branch point (has 2 children) + // ├─ user-3a ← branch A (active: leaf is asst-4a) + // │ asst-3a + // │ user-4a + // │ asst-4a + // └─ user-3b ← branch B + // asst-3b + // user-4b + // + // Foldable nodes: user-1 (root), user-3a (segment start), user-3b (segment start) + + function buildBranchingTree() { + const entries: SessionEntry[] = [ + userMessage("user-1", null, "first message"), + assistantMessage("asst-1", "user-1", "response 1"), + userMessage("user-2", "asst-1", "second message"), + assistantMessage("asst-2", "user-2", "response 2"), + // Branch A (active) + userMessage("user-3a", "asst-2", "branch A start"), + assistantMessage("asst-3a", "user-3a", "branch A response"), + userMessage("user-4a", "asst-3a", "branch A deep"), + assistantMessage("asst-4a", "user-4a", "branch A leaf"), + // Branch B + userMessage("user-3b", "asst-2", "branch B start"), + assistantMessage("asst-3b", "user-3b", "branch B response"), + userMessage("user-4b", "asst-3b", "branch B deep"), + ]; + return buildTree(entries); + } + + test("ctrl+right unfolds a folded node, then does segment jump when unfolded", () => { + const tree = buildBranchingTree(); + const selector = new TreeSelectorComponent( + tree, + "asst-4a", + 24, + () => {}, + () => {}, + ); + const list = selector.getTreeList(); + + selector.handleInput(CTRL_LEFT); // asst-4a → user-3a + expect(list.getSelectedNode()?.entry.id).toBe("user-3a"); + + selector.handleInput(CTRL_LEFT); // fold user-3a + expect(list.getSelectedNode()?.entry.id).toBe("user-3a"); + + selector.handleInput(DOWN); // user-3a → user-3b (children hidden) + expect(list.getSelectedNode()?.entry.id).toBe("user-3b"); + + selector.handleInput(UP); // user-3b → user-3a + expect(list.getSelectedNode()?.entry.id).toBe("user-3a"); + + selector.handleInput(CTRL_RIGHT); // unfold user-3a + expect(list.getSelectedNode()?.entry.id).toBe("user-3a"); + + selector.handleInput(DOWN); // user-3a → asst-3a (children restored) + expect(list.getSelectedNode()?.entry.id).toBe("asst-3a"); + + selector.handleInput(CTRL_LEFT); // asst-3a → user-3a + expect(list.getSelectedNode()?.entry.id).toBe("user-3a"); + + selector.handleInput(CTRL_RIGHT); // user-3a → asst-4a (segment jump to leaf) + expect(list.getSelectedNode()?.entry.id).toBe("asst-4a"); + }); + + test("alt+left/right are aliases for fold and unfold navigation", () => { + const tree = buildBranchingTree(); + const selector = new TreeSelectorComponent( + tree, + "asst-4a", + 24, + () => {}, + () => {}, + ); + const list = selector.getTreeList(); + + selector.handleInput(ALT_LEFT); // asst-4a → user-3a + expect(list.getSelectedNode()?.entry.id).toBe("user-3a"); + + selector.handleInput(ALT_LEFT); // fold user-3a + expect(list.getSelectedNode()?.entry.id).toBe("user-3a"); + + selector.handleInput(ALT_RIGHT); // unfold user-3a + expect(list.getSelectedNode()?.entry.id).toBe("user-3a"); + + selector.handleInput(ALT_RIGHT); // user-3a → asst-4a + expect(list.getSelectedNode()?.entry.id).toBe("asst-4a"); + }); + + test("folding root hides entire subtree, nested fold preserved on unfold", () => { + const tree = buildBranchingTree(); + const selector = new TreeSelectorComponent( + tree, + "asst-4a", + 24, + () => {}, + () => {}, + ); + const list = selector.getTreeList(); + + selector.handleInput(CTRL_LEFT); // asst-4a → user-3a + expect(list.getSelectedNode()?.entry.id).toBe("user-3a"); + + selector.handleInput(CTRL_LEFT); // fold user-3a + expect(list.getSelectedNode()?.entry.id).toBe("user-3a"); + + selector.handleInput(CTRL_LEFT); // user-3a (folded) → user-1 + expect(list.getSelectedNode()?.entry.id).toBe("user-1"); + + selector.handleInput(CTRL_LEFT); // fold user-1 + expect(list.getSelectedNode()?.entry.id).toBe("user-1"); + + selector.handleInput(DOWN); // wrap (only visible node) + expect(list.getSelectedNode()?.entry.id).toBe("user-1"); + + selector.handleInput(CTRL_RIGHT); // unfold user-1 + expect(list.getSelectedNode()?.entry.id).toBe("user-1"); + + selector.handleInput(CTRL_RIGHT); // user-1 → user-3a (segment jump, user-3a still folded) + expect(list.getSelectedNode()?.entry.id).toBe("user-3a"); + + selector.handleInput(DOWN); // user-3a → user-3b (user-3a still folded) + expect(list.getSelectedNode()?.entry.id).toBe("user-3b"); + }); + + test("fold and navigate on non-active branch", () => { + const tree = buildBranchingTree(); + const selector = new TreeSelectorComponent( + tree, + "asst-4a", + 24, + () => {}, + () => {}, + ); + const list = selector.getTreeList(); + + // Navigate down to user-3b (branch B) + let found = false; + for (let i = 0; i < 20; i++) { + selector.handleInput(DOWN); + if (list.getSelectedNode()?.entry.id === "user-3b") { + found = true; + break; + } + } + expect(found).toBe(true); + + selector.handleInput(CTRL_RIGHT); // user-3b → user-4b (segment jump to leaf) + expect(list.getSelectedNode()?.entry.id).toBe("user-4b"); + + selector.handleInput(CTRL_LEFT); // user-4b → user-3b + expect(list.getSelectedNode()?.entry.id).toBe("user-3b"); + + selector.handleInput(CTRL_LEFT); // fold user-3b + expect(list.getSelectedNode()?.entry.id).toBe("user-3b"); + + selector.handleInput(CTRL_LEFT); // user-3b (folded) → user-1 + expect(list.getSelectedNode()?.entry.id).toBe("user-1"); + }); + + test("fold and navigate with multiple roots", () => { + const entries: SessionEntry[] = [ + userMessage("user-1", null, "first root"), + assistantMessage("asst-1", "user-1", "response 1"), + userMessage("user-2", null, "second root"), + assistantMessage("asst-2", "user-2", "response 2"), + ]; + const tree = buildTree(entries); + const selector = new TreeSelectorComponent( + tree, + "asst-1", + 24, + () => {}, + () => {}, + ); + const list = selector.getTreeList(); + + expect(list.getSelectedNode()?.entry.id).toBe("asst-1"); + + selector.handleInput(CTRL_LEFT); // asst-1 → user-1 + expect(list.getSelectedNode()?.entry.id).toBe("user-1"); + + selector.handleInput(CTRL_LEFT); // fold user-1 + expect(list.getSelectedNode()?.entry.id).toBe("user-1"); + + selector.handleInput(DOWN); // user-1 → user-2 (children hidden) + expect(list.getSelectedNode()?.entry.id).toBe("user-2"); + + selector.handleInput(CTRL_RIGHT); // user-2 → asst-2 (segment jump to leaf) + expect(list.getSelectedNode()?.entry.id).toBe("asst-2"); + + selector.handleInput(CTRL_LEFT); // asst-2 → user-2 + expect(list.getSelectedNode()?.entry.id).toBe("user-2"); + + selector.handleInput(CTRL_LEFT); // fold user-2 + expect(list.getSelectedNode()?.entry.id).toBe("user-2"); + + selector.handleInput(CTRL_LEFT); // user-2 (folded, root) → stays on user-2 + expect(list.getSelectedNode()?.entry.id).toBe("user-2"); + }); + + test("folding root hides descendants even when intermediate nodes are filtered out", () => { + // user-1 → toolCallOnly-1 (filtered out) → user-2 → asst-2 + const entries: SessionEntry[] = [ + userMessage("user-1", null, "hello"), + toolCallOnlyAssistant("tool-asst-1", "user-1"), + userMessage("user-2", "tool-asst-1", "follow up"), + assistantMessage("asst-2", "user-2", "response"), + ]; + const tree = buildTree(entries); + const selector = new TreeSelectorComponent( + tree, + "asst-2", + 24, + () => {}, + () => {}, + ); + const list = selector.getTreeList(); + + selector.handleInput(CTRL_LEFT); // asst-2 → user-1 + expect(list.getSelectedNode()?.entry.id).toBe("user-1"); + + selector.handleInput(CTRL_LEFT); // fold user-1 + expect(list.getSelectedNode()?.entry.id).toBe("user-1"); + + selector.handleInput(DOWN); // wrap (only visible node) + expect(list.getSelectedNode()?.entry.id).toBe("user-1"); + }); + + test("search resets fold state", () => { + const tree = buildBranchingTree(); + const selector = new TreeSelectorComponent( + tree, + "asst-4a", + 24, + () => {}, + () => {}, + ); + const list = selector.getTreeList(); + + selector.handleInput(CTRL_LEFT); // asst-4a → user-3a + selector.handleInput(CTRL_LEFT); // fold user-3a + + selector.handleInput(DOWN); // user-3a → user-3b (children hidden) + expect(list.getSelectedNode()?.entry.id).toBe("user-3b"); + + selector.handleInput("b"); // search resets folds + selector.handleInput("\x1b"); // clear search + + // Navigate to user-3a to verify fold was reset + let currentId = ""; + for (let i = 0; i < 20; i++) { + selector.handleInput(DOWN); + currentId = list.getSelectedNode()?.entry.id ?? ""; + if (currentId === "user-3a") break; + } + expect(currentId).toBe("user-3a"); + + selector.handleInput(DOWN); // user-3a → asst-3a (not user-3b) + expect(list.getSelectedNode()?.entry.id).toBe("asst-3a"); + }); + + test("filter mode change resets fold state", () => { + const tree = buildBranchingTree(); + const selector = new TreeSelectorComponent( + tree, + "asst-4a", + 24, + () => {}, + () => {}, + ); + const list = selector.getTreeList(); + + selector.handleInput(CTRL_LEFT); // asst-4a → user-3a + selector.handleInput(CTRL_LEFT); // fold user-3a + + selector.handleInput("\x15"); // ctrl+u: user-only filter resets folds + selector.handleInput("\x04"); // ctrl+d: back to default + + // Navigate to user-3a to verify fold was reset + let currentId = ""; + for (let i = 0; i < 20; i++) { + selector.handleInput(DOWN); + currentId = list.getSelectedNode()?.entry.id ?? ""; + if (currentId === "user-3a") break; + } + expect(currentId).toBe("user-3a"); + + selector.handleInput(DOWN); // user-3a → asst-3a (not user-3b) + expect(list.getSelectedNode()?.entry.id).toBe("asst-3a"); + }); + }); +}); diff --git a/packages/coding-agent/test/trigger-compact-extension.test.ts b/packages/coding-agent/test/trigger-compact-extension.test.ts new file mode 100644 index 0000000000000000000000000000000000000000..930769f378e26f1b458ea36ccddbcc685c60f811 --- /dev/null +++ b/packages/coding-agent/test/trigger-compact-extension.test.ts @@ -0,0 +1,60 @@ +import { describe, expect, test, vi } from "vitest"; +import triggerCompactExtension from "../examples/extensions/trigger-compact.ts"; +import type { ExtensionAPI, ExtensionCommandContext, ExtensionContext } from "../src/core/extensions/index.ts"; + +function createContext(tokens: number | null, compact = vi.fn()): ExtensionContext { + return { + mode: "print", + hasUI: false, + ui: {} as ExtensionContext["ui"], + cwd: process.cwd(), + sessionManager: {} as ExtensionContext["sessionManager"], + modelRegistry: {} as ExtensionContext["modelRegistry"], + model: undefined, + scopedModels: [], + isIdle: () => true, + isProjectTrusted: () => true, + signal: undefined, + abort: vi.fn(), + hasPendingMessages: () => false, + shutdown: vi.fn(), + getContextUsage: () => ({ tokens, contextWindow: 200_000, percent: tokens === null ? null : tokens / 2000 }), + compact, + getSystemPrompt: () => "", + }; +} + +describe("trigger-compact example extension", () => { + test("only auto-compacts when context usage crosses the threshold", () => { + let turnEndHandler: + | ((event: { type: "turn_end" }, ctx: ExtensionContext | ExtensionCommandContext) => void) + | undefined; + + const api = { + on: (event: string, handler: (event: { type: "turn_end" }, ctx: ExtensionContext) => void) => { + if (event === "turn_end") { + turnEndHandler = handler; + } + }, + registerCommand: vi.fn(), + } as unknown as ExtensionAPI; + + triggerCompactExtension(api); + expect(turnEndHandler).toBeDefined(); + + const compact = vi.fn(); + const event = { type: "turn_end" } as const; + + turnEndHandler?.(event, createContext(110_000, compact)); + expect(compact).not.toHaveBeenCalled(); + + turnEndHandler?.(event, createContext(120_000, compact)); + expect(compact).not.toHaveBeenCalled(); + + turnEndHandler?.(event, createContext(95_000, compact)); + expect(compact).not.toHaveBeenCalled(); + + turnEndHandler?.(event, createContext(105_000, compact)); + expect(compact).toHaveBeenCalledTimes(1); + }); +}); diff --git a/packages/coding-agent/test/truncate-to-width.test.ts b/packages/coding-agent/test/truncate-to-width.test.ts new file mode 100644 index 0000000000000000000000000000000000000000..7be05e0b1b4549df218d691b75b0be0c33d01e3d --- /dev/null +++ b/packages/coding-agent/test/truncate-to-width.test.ts @@ -0,0 +1,81 @@ +import { truncateToWidth, visibleWidth } from "@earendil-works/pi-tui"; +import { describe, expect, it } from "vitest"; + +/** + * Tests for truncateToWidth behavior with Unicode characters. + * + * These tests verify that truncateToWidth properly handles text with + * Unicode characters that have different byte vs display widths. + */ +describe("truncateToWidth", () => { + it("should truncate messages with Unicode characters correctly", () => { + // This message contains a checkmark (✔) which may have display width > 1 byte + const message = '✔ script to run › dev $ concurrently "vite" "node --import tsx ./'; + const width = 67; + const maxMsgWidth = width - 2; // Account for cursor + + const truncated = truncateToWidth(message, maxMsgWidth); + const truncatedWidth = visibleWidth(truncated); + + expect(truncatedWidth).toBeLessThanOrEqual(maxMsgWidth); + }); + + it("should handle emoji characters", () => { + const message = "🎉 Celebration! 🚀 Launch 📦 Package ready for deployment now"; + const width = 40; + const maxMsgWidth = width - 2; + + const truncated = truncateToWidth(message, maxMsgWidth); + const truncatedWidth = visibleWidth(truncated); + + expect(truncatedWidth).toBeLessThanOrEqual(maxMsgWidth); + }); + + it("should handle mixed ASCII and wide characters", () => { + const message = "Hello 世界 Test 你好 More text here that is long"; + const width = 30; + const maxMsgWidth = width - 2; + + const truncated = truncateToWidth(message, maxMsgWidth); + const truncatedWidth = visibleWidth(truncated); + + expect(truncatedWidth).toBeLessThanOrEqual(maxMsgWidth); + }); + + it("should not truncate messages that fit", () => { + const message = "Short message"; + const width = 50; + const maxMsgWidth = width - 2; + + const truncated = truncateToWidth(message, maxMsgWidth); + + expect(truncated).toBe(message); + expect(visibleWidth(truncated)).toBeLessThanOrEqual(maxMsgWidth); + }); + + it("should add ellipsis when truncating", () => { + const message = "This is a very long message that needs to be truncated"; + const width = 30; + const maxMsgWidth = width - 2; + + const truncated = truncateToWidth(message, maxMsgWidth); + + expect(truncated).toContain("..."); + expect(visibleWidth(truncated)).toBeLessThanOrEqual(maxMsgWidth); + }); + + it("should handle the exact crash case from issue report", () => { + // Terminal width was 67, line had visible width 68 + // The problematic text contained "✔" and "›" characters + const message = '✔ script to run › dev $ concurrently "vite" "node --import tsx ./server.ts"'; + const terminalWidth = 67; + const cursorWidth = 2; // "› " or " " + const maxMsgWidth = terminalWidth - cursorWidth; + + const truncated = truncateToWidth(message, maxMsgWidth); + const finalWidth = visibleWidth(truncated); + + // The final line (cursor + message) must not exceed terminal width + expect(finalWidth + cursorWidth).toBeLessThanOrEqual(terminalWidth); + }); +}); diff --git a/packages/coding-agent/test/trust-manager.test.ts b/packages/coding-agent/test/trust-manager.test.ts new file mode 100644 index 0000000000000000000000000000000000000000..01500bac209ca049ea545576b88f8248eea94ded --- /dev/null +++ b/packages/coding-agent/test/trust-manager.test.ts @@ -0,0 +1,67 @@ +import { mkdirSync, rmSync, writeFileSync } from "node:fs"; +import { tmpdir } from "node:os"; +import { join } from "node:path"; +import { afterEach, beforeEach, describe, expect, it } from "vitest"; +import { hasTrustRequiringProjectResources, ProjectTrustStore } from "../src/core/trust-manager.ts"; + +describe("ProjectTrustStore", () => { + let tempDir: string; + let agentDir: string; + let cwd: string; + + beforeEach(() => { + tempDir = join(tmpdir(), `trust-test-${Date.now()}-${Math.random().toString(36).slice(2)}`); + agentDir = join(tempDir, "agent"); + cwd = join(tempDir, "project"); + mkdirSync(agentDir, { recursive: true }); + mkdirSync(cwd, { recursive: true }); + }); + + afterEach(() => { + rmSync(tempDir, { recursive: true, force: true }); + }); + + it("stores decisions and inherits from parent directories", () => { + const store = new ProjectTrustStore(agentDir); + const parentDir = join(tempDir, "trusted-parent"); + const childDir = join(parentDir, "project"); + mkdirSync(childDir, { recursive: true }); + + expect(store.get(childDir)).toBeNull(); + store.set(parentDir, true); + expect(store.get(childDir)).toBe(true); + store.set(childDir, false); + expect(store.get(childDir)).toBe(false); + store.set(childDir, null); + expect(store.get(childDir)).toBe(true); + }); + + it("detects trust-requiring project resources", () => { + const originalHome = process.env.HOME; + process.env.HOME = tempDir; + try { + mkdirSync(join(tempDir, ".pi", "agent"), { recursive: true }); + mkdirSync(join(tempDir, ".agents", "skills"), { recursive: true }); + expect(hasTrustRequiringProjectResources(tempDir)).toBe(false); + expect(hasTrustRequiringProjectResources(cwd)).toBe(false); + + writeFileSync(join(tempDir, ".pi", "settings.json"), "{}"); + expect(hasTrustRequiringProjectResources(tempDir)).toBe(true); + rmSync(join(tempDir, ".pi", "settings.json"), { force: true }); + + mkdirSync(join(cwd, ".pi"), { recursive: true }); + writeFileSync(join(cwd, ".pi", "settings.json"), "{}"); + expect(hasTrustRequiringProjectResources(cwd)).toBe(true); + + rmSync(join(cwd, ".pi"), { recursive: true, force: true }); + mkdirSync(join(cwd, ".agents", "skills"), { recursive: true }); + expect(hasTrustRequiringProjectResources(cwd)).toBe(true); + } finally { + if (originalHome === undefined) { + delete process.env.HOME; + } else { + process.env.HOME = originalHome; + } + } + }); +}); diff --git a/packages/coding-agent/test/trust-selector.test.ts b/packages/coding-agent/test/trust-selector.test.ts new file mode 100644 index 0000000000000000000000000000000000000000..c4659001bd581036e88cc73b1a646dba9111019f --- /dev/null +++ b/packages/coding-agent/test/trust-selector.test.ts @@ -0,0 +1,91 @@ +import { setKeybindings } from "@earendil-works/pi-tui"; +import { beforeAll, beforeEach, describe, expect, it, vi } from "vitest"; +import { KeybindingsManager } from "../src/core/keybindings.ts"; +import { TrustSelectorComponent } from "../src/modes/interactive/components/trust-selector.ts"; +import { initTheme } from "../src/modes/interactive/theme/theme.ts"; +import { stripAnsi } from "../src/utils/ansi.ts"; + +describe("TrustSelectorComponent", () => { + beforeAll(() => { + initTheme("dark"); + }); + + beforeEach(() => { + setKeybindings(new KeybindingsManager()); + }); + + it("keeps the saved trusted decision marked while browsing", () => { + const selector = new TrustSelectorComponent({ + cwd: "/project", + savedDecision: { path: "/project", decision: true }, + projectTrusted: true, + onSelect: () => {}, + onCancel: () => {}, + }); + + let output = stripAnsi(selector.render(120).join("\n")); + expect(output).toContain("Saved decision: trusted (/project)"); + expect(output).toContain("Current session: trusted"); + expect(output).toContain("→ ✓ Trust"); + + selector.handleInput("\x1b[B"); + output = stripAnsi(selector.render(120).join("\n")); + expect(output).toContain("✓ Trust"); + expect(output).toContain("→ Trust parent folder (/)"); + expect(output).not.toContain("✓ Do not trust"); + }); + + it("selects a trust decision", () => { + const onSelect = vi.fn(); + const selector = new TrustSelectorComponent({ + cwd: "/project", + savedDecision: null, + projectTrusted: false, + onSelect, + onCancel: () => {}, + }); + + selector.handleInput("\n"); + + expect(onSelect).toHaveBeenCalledWith({ trusted: true, updates: [{ path: "/project", decision: true }] }); + }); + + it("labels saved ancestor decisions as inherited", () => { + const selector = new TrustSelectorComponent({ + cwd: "/parent/project/nested", + savedDecision: { path: "/parent", decision: true }, + projectTrusted: true, + onSelect: () => {}, + onCancel: () => {}, + }); + + const output = stripAnsi(selector.render(120).join("\n")); + + expect(output).toContain("Saved decision: trusted (inherited from /parent)"); + }); + + it("adds a trust parent option", () => { + const onSelect = vi.fn(); + const selector = new TrustSelectorComponent({ + cwd: "/parent/project", + savedDecision: { path: "/parent", decision: true }, + projectTrusted: true, + onSelect, + onCancel: () => {}, + }); + + const output = stripAnsi(selector.render(120).join("\n")); + expect(output).toContain("Saved decision: trusted (inherited from /parent)"); + expect(output).toContain("✓ Trust parent folder (/parent)"); + + selector.handleInput("\n"); + + expect(onSelect).toHaveBeenCalledWith({ + trusted: true, + updates: [ + { path: "/parent", decision: true }, + { path: "/parent/project", decision: null }, + ], + }); + }); +}); diff --git a/packages/coding-agent/test/user-message.test.ts b/packages/coding-agent/test/user-message.test.ts new file mode 100644 index 0000000000000000000000000000000000000000..e1f6ad23c2bd4ac643bdc09927ff216243226b6a --- /dev/null +++ b/packages/coding-agent/test/user-message.test.ts @@ -0,0 +1,58 @@ +import { describe, expect, test } from "vitest"; +import { UserMessageComponent } from "../src/modes/interactive/components/user-message.ts"; +import { initTheme } from "../src/modes/interactive/theme/theme.ts"; +import { stripAnsi } from "../src/utils/ansi.ts"; + +const OSC133_ZONE_START = "\x1b]133;A\x07"; +const OSC133_ZONE_END = "\x1b]133;B\x07"; +const OSC133_ZONE_FINAL = "\x1b]133;C\x07"; +const BG_RESET = "\x1b[49m"; + +describe("UserMessageComponent", () => { + test("keeps user message height stable while moving closing OSC markers off line end", () => { + initTheme("dark"); + + const component = new UserMessageComponent("hello"); + const lines = component.render(20); + + expect(lines).toHaveLength(3); + expect(lines[0]).toContain(OSC133_ZONE_START); + expect(lines[0].endsWith(BG_RESET)).toBe(true); + expect(lines[0]).not.toContain(OSC133_ZONE_END); + expect(lines[1]).toContain("hello"); + expect(lines[2].startsWith(OSC133_ZONE_END + OSC133_ZONE_FINAL)).toBe(true); + expect(lines[2].endsWith(BG_RESET)).toBe(true); + }); + + test("chains Markdown transformers with user message context", () => { + initTheme("dark"); + const calls: string[] = []; + const component = new UserMessageComponent("The input is $x^2$.", undefined, 1, [ + (markdown, context) => { + calls.push("formula"); + expect(context).toEqual({ messageType: "user", isStreaming: false, availableWidth: 78 }); + return markdown.replace("$x^2$", "x²"); + }, + (markdown) => { + calls.push("suffix"); + return `${markdown} Done.`; + }, + ]); + + expect(stripAnsi(component.render(80).join("\n"))).toContain("The input is x². Done."); + expect(calls).toEqual(["formula", "suffix"]); + }); + + test("reapplies Markdown transformers when invalidated", () => { + initTheme("dark"); + let suffix = "before"; + const component = new UserMessageComponent("Message", undefined, 1, [(markdown) => `${markdown} ${suffix}`]); + + expect(stripAnsi(component.render(80).join("\n"))).toContain("Message before"); + + suffix = "after"; + component.invalidate(); + + expect(stripAnsi(component.render(80).join("\n"))).toContain("Message after"); + }); +}); diff --git a/packages/coding-agent/test/utilities.ts b/packages/coding-agent/test/utilities.ts new file mode 100644 index 0000000000000000000000000000000000000000..41641db7c0120115d8f72bdcf0bae116dde85cfe --- /dev/null +++ b/packages/coding-agent/test/utilities.ts @@ -0,0 +1,321 @@ +import { createModelRegistry, getModelRuntime } from "./model-runtime-test-utils.ts"; +/** + * Shared test utilities for coding-agent tests. + */ + +import { chmodSync, existsSync, mkdirSync, readFileSync, rmSync, writeFileSync } from "node:fs"; +import { homedir, tmpdir } from "node:os"; +import { dirname, join } from "node:path"; +import { Agent } from "@earendil-works/pi-agent-core"; +import type { OAuthCredentials } from "@earendil-works/pi-ai"; +import { getModel, streamSimple } from "@earendil-works/pi-ai/compat"; +import { builtinProviders } from "@earendil-works/pi-ai/providers/all"; +import { AgentSession } from "../src/core/agent-session.ts"; +import { AuthStorage } from "../src/core/auth-storage.ts"; +import { createEventBus } from "../src/core/event-bus.ts"; +import type { + Extension, + ExtensionFactory, + InlineExtension, + LoadExtensionsResult, +} from "../src/core/extensions/index.ts"; +import { createExtensionRuntime, loadExtensionFromFactory } from "../src/core/extensions/loader.ts"; +import type { ResourceLoader } from "../src/core/resource-loader.ts"; +import { SessionManager } from "../src/core/session-manager.ts"; +import { SettingsManager } from "../src/core/settings-manager.ts"; +import { createCodingTools } from "../src/index.ts"; + +/** + * API key for authenticated tests. Tests using this should be wrapped in + * describe.skipIf(!API_KEY) + */ +export const API_KEY = process.env.ANTHROPIC_OAUTH_TOKEN || process.env.ANTHROPIC_API_KEY; + +// ============================================================================ +// OAuth API key resolution from ~/.pi/agent/auth.json +// ============================================================================ + +const AUTH_PATH = join(homedir(), ".pi", "agent", "auth.json"); + +type ApiKeyCredential = { + type: "api_key"; + key: string; +}; + +type OAuthCredentialEntry = { + type: "oauth"; +} & OAuthCredentials; + +type AuthCredential = ApiKeyCredential | OAuthCredentialEntry; + +type AuthStorageData = Record; + +function loadAuthStorage(): AuthStorageData { + if (!existsSync(AUTH_PATH)) { + return {}; + } + try { + const content = readFileSync(AUTH_PATH, "utf-8"); + return JSON.parse(content); + } catch { + return {}; + } +} + +function saveAuthStorage(storage: AuthStorageData): void { + const configDir = dirname(AUTH_PATH); + if (!existsSync(configDir)) { + mkdirSync(configDir, { recursive: true, mode: 0o700 }); + } + writeFileSync(AUTH_PATH, JSON.stringify(storage, null, 2), "utf-8"); + chmodSync(AUTH_PATH, 0o600); +} + +/** + * Resolve API key for a provider from ~/.pi/agent/auth.json + * + * For API key credentials, returns the key directly. + * For OAuth credentials, returns the access token (refreshing if expired and saving back). + * + */ +export async function resolveApiKey(provider: string): Promise { + const storage = loadAuthStorage(); + const entry = storage[provider]; + + if (!entry) return undefined; + + if (entry.type === "api_key") { + return entry.key; + } + + if (entry.type === "oauth") { + const oauth = builtinProviders().find((candidate) => candidate.id === provider)?.auth.oauth; + if (!oauth) return undefined; + let credential = entry; + if (Date.now() >= credential.expires) { + credential = await oauth.refresh(credential, new AbortController().signal); + storage[provider] = credential; + saveAuthStorage(storage); + } + return (await oauth.toAuth(credential)).apiKey; + } + + return undefined; +} + +/** + * Check if a provider has credentials in ~/.pi/agent/auth.json + */ +export function hasAuthForProvider(provider: string): boolean { + const storage = loadAuthStorage(); + return provider in storage; +} + +/** Path to the real pi agent config directory */ +export const PI_AGENT_DIR = join(homedir(), ".pi", "agent"); + +/** + * Get an AuthStorage instance backed by ~/.pi/agent/auth.json + * Use this for tests that need real OAuth credentials. + */ +export function getRealAuthStorage(): AuthStorage { + return AuthStorage.create(AUTH_PATH); +} + +/** + * Create a minimal user message for testing. + */ +export function userMsg(text: string) { + return { role: "user" as const, content: text, timestamp: Date.now() }; +} + +/** + * Create a minimal assistant message for testing. + */ +export function assistantMsg(text: string) { + return { + role: "assistant" as const, + content: [{ type: "text" as const, text }], + api: "anthropic-messages" as const, + provider: "anthropic", + model: "test", + usage: { + input: 1, + output: 1, + cacheRead: 0, + cacheWrite: 0, + totalTokens: 2, + cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 }, + }, + stopReason: "stop" as const, + timestamp: Date.now(), + }; +} + +/** + * Options for creating a test session. + */ +export interface TestSessionOptions { + /** Use in-memory session (no file persistence) */ + inMemory?: boolean; + /** Custom system prompt */ + systemPrompt?: string; + /** Custom settings overrides */ + settingsOverrides?: Record; +} + +/** + * Resources returned by createTestSession that need cleanup. + */ +export interface TestSessionContext { + session: AgentSession; + sessionManager: SessionManager; + tempDir: string; + cleanup: () => void; +} + +export interface CreateTestExtensionsResultInput { + factory: ExtensionFactory; + path?: string; +} + +type TestExtensionInput = InlineExtension | CreateTestExtensionsResultInput; + +export async function createTestExtensionsResult( + inputs: TestExtensionInput[], + cwd = process.cwd(), +): Promise { + const runtime = createExtensionRuntime(); + const eventBus = createEventBus(); + const extensions: Extension[] = []; + + for (const [index, input] of inputs.entries()) { + const isObject = typeof input !== "function"; + const hasName = isObject && "name" in input; + const hasPath = isObject && "path" in input && typeof input.path === "string" && input.path !== ""; + const factory = isObject ? input.factory : input; + const extensionPath = hasName ? `` : hasPath ? input.path : ``; + + extensions.push(await loadExtensionFromFactory(factory, cwd, eventBus, runtime, extensionPath)); + } + + return { + extensions, + errors: [], + runtime, + }; +} + +export interface CreateTestResourceLoaderOptions { + extensionsResult?: LoadExtensionsResult; +} + +export function createTestResourceLoader(options: CreateTestResourceLoaderOptions = {}): ResourceLoader { + const extensionsResult = options.extensionsResult ?? { + extensions: [], + errors: [], + runtime: createExtensionRuntime(), + }; + + return { + getExtensions: () => extensionsResult, + getSkills: () => ({ skills: [], diagnostics: [] }), + getPrompts: () => ({ prompts: [], diagnostics: [] }), + getThemes: () => ({ themes: [], diagnostics: [] }), + getAgentsFiles: () => ({ agentsFiles: [] }), + getSystemPrompt: () => undefined, + getSystemPromptSource: () => undefined, + getAppendSystemPrompt: () => [], + getAppendSystemPromptSources: () => [], + extendResources: () => {}, + reload: async () => {}, + }; +} + +/** + * Create an AgentSession for testing with proper setup and cleanup. + * Use this for e2e tests that need real LLM calls. + */ +export async function createTestSession(options: TestSessionOptions = {}): Promise { + const tempDir = join(tmpdir(), `pi-test-${Date.now()}-${Math.random().toString(36).slice(2)}`); + mkdirSync(tempDir, { recursive: true }); + + const model = getModel("anthropic", "claude-sonnet-4-5")!; + const agent = new Agent({ + getApiKey: () => API_KEY, + initialState: { + model, + systemPrompt: options.systemPrompt ?? "You are a helpful assistant. Be extremely concise.", + tools: createCodingTools(process.cwd()), + }, + streamFn: streamSimple, + }); + + const sessionManager = options.inMemory ? SessionManager.inMemory() : SessionManager.create(tempDir); + const settingsManager = SettingsManager.create(tempDir, tempDir); + + if (options.settingsOverrides) { + settingsManager.applyOverrides(options.settingsOverrides); + } + + const authStorage = AuthStorage.create(join(tempDir, "auth.json")); + const modelRegistry = await createModelRegistry(authStorage, tempDir); + + const session = new AgentSession({ + agent, + sessionManager, + settingsManager, + cwd: tempDir, + modelRuntime: getModelRuntime(modelRegistry), + resourceLoader: createTestResourceLoader(), + }); + + // Must subscribe to enable session persistence + session.subscribe(() => {}); + + const cleanup = () => { + session.dispose(); + if (tempDir && existsSync(tempDir)) { + rmSync(tempDir, { recursive: true }); + } + }; + + return { session, sessionManager, tempDir, cleanup }; +} + +/** + * Build a session tree for testing using SessionManager. + * Returns the IDs of all created entries. + * + * Example tree structure: + * ``` + * u1 -> a1 -> u2 -> a2 + * -> u3 -> a3 (branch from a1) + * u4 -> a4 (another root) + * ``` + */ +export function buildTestTree( + session: SessionManager, + structure: { + messages: Array<{ role: "user" | "assistant"; text: string; branchFrom?: string }>; + }, +): Map { + const ids = new Map(); + + for (const msg of structure.messages) { + if (msg.branchFrom) { + const branchFromId = ids.get(msg.branchFrom); + if (!branchFromId) { + throw new Error(`Cannot branch from unknown entry: ${msg.branchFrom}`); + } + session.branch(branchFromId); + } + + const id = + msg.role === "user" ? session.appendMessage(userMsg(msg.text)) : session.appendMessage(assistantMsg(msg.text)); + + ids.set(msg.text, id); + } + + return ids; +} diff --git a/packages/coding-agent/test/version-check.test.ts b/packages/coding-agent/test/version-check.test.ts new file mode 100644 index 0000000000000000000000000000000000000000..ffe21508c3602b94cf177613f6cf264d0e14cc53 --- /dev/null +++ b/packages/coding-agent/test/version-check.test.ts @@ -0,0 +1,131 @@ +import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; +import { + checkForNewPiVersion, + comparePackageVersions, + formatVersionCheckError, + getLatestPiRelease, + getLatestPiVersion, + isNewerPackageVersion, +} from "../src/utils/version-check.ts"; +import { allowNetwork } from "./test-network-env.ts"; + +const originalSkipVersionCheck = process.env.PI_SKIP_VERSION_CHECK; + +beforeEach(() => { + allowNetwork(); +}); + +afterEach(() => { + vi.unstubAllGlobals(); + if (originalSkipVersionCheck === undefined) { + delete process.env.PI_SKIP_VERSION_CHECK; + } else { + process.env.PI_SKIP_VERSION_CHECK = originalSkipVersionCheck; + } +}); + +describe("version checks", () => { + it("compares package versions", () => { + expect(comparePackageVersions("0.70.6", "0.70.5")).toBeGreaterThan(0); + expect(comparePackageVersions("0.70.5", "0.70.5")).toBe(0); + expect(comparePackageVersions("0.70.4", "0.70.5")).toBeLessThan(0); + expect(comparePackageVersions("5.0.0-beta.20", "5.0.0-beta.9")).toBeGreaterThan(0); + expect(isNewerPackageVersion("0.70.5", "0.70.5")).toBe(false); + expect(isNewerPackageVersion("0.70.6", "0.70.5")).toBe(true); + }); + + it("returns only newer versions", async () => { + const fetchMock = vi.fn(async () => Response.json({ version: "1.2.3" })); + vi.stubGlobal("fetch", fetchMock); + + await expect(checkForNewPiVersion("1.2.3")).resolves.toBeUndefined(); + await expect(checkForNewPiVersion("1.2.2")).resolves.toEqual({ version: "1.2.3" }); + }); + + it("uses the pi.dev version check api with a pi user agent", async () => { + const fetchMock = vi.fn(async () => Response.json({ version: "1.2.4" })); + vi.stubGlobal("fetch", fetchMock); + + await expect(getLatestPiVersion("1.2.3")).resolves.toBe("1.2.4"); + expect(fetchMock).toHaveBeenCalledWith( + "https://pi.dev/api/latest-version", + expect.objectContaining({ + headers: expect.objectContaining({ + "User-Agent": expect.stringMatching(/^pi\/1\.2\.3 /), + accept: "application/json", + }), + }), + ); + }); + + it("retries a transient version request when explicitly requested", async () => { + const fetchMock = vi + .fn() + .mockRejectedValueOnce(new Error("fetch failed")) + .mockRejectedValueOnce(new Error("fetch failed")) + .mockResolvedValueOnce(Response.json({ version: "1.2.4" })); + vi.stubGlobal("fetch", fetchMock); + + await expect(getLatestPiRelease("1.2.3", { retry: true })).resolves.toEqual({ version: "1.2.4" }); + expect(fetchMock).toHaveBeenCalledTimes(3); + }); + + it("keeps automatic version checks to one request", async () => { + const fetchMock = vi.fn().mockRejectedValue(new Error("fetch failed")); + vi.stubGlobal("fetch", fetchMock); + + await expect(checkForNewPiVersion("1.2.3")).resolves.toBeUndefined(); + expect(fetchMock).toHaveBeenCalledOnce(); + }); + + it("formats nested network error details", () => { + const error = new Error("fetch failed", { + cause: new AggregateError([ + Object.assign(new Error("connect timeout"), { code: "ETIMEDOUT" }), + Object.assign(new Error("network unreachable"), { code: "ENETUNREACH" }), + ]), + }); + + expect(formatVersionCheckError(error)).toBe("fetch failed (ETIMEDOUT, ENETUNREACH)"); + }); + + it("returns the active package metadata from the version check api", async () => { + const fetchMock = vi.fn(async () => + Response.json({ + packageName: "@new-scope/pi", + version: "1.2.4", + }), + ); + vi.stubGlobal("fetch", fetchMock); + + await expect(getLatestPiRelease("1.2.3")).resolves.toEqual({ + packageName: "@new-scope/pi", + version: "1.2.4", + }); + }); + + it("returns update notes from the version check api", async () => { + const fetchMock = vi.fn(async () => Response.json({ note: " **Read this** ", version: "1.2.4" })); + vi.stubGlobal("fetch", fetchMock); + + await expect(getLatestPiRelease("1.2.3")).resolves.toEqual({ note: "**Read this**", version: "1.2.4" }); + }); + + it("skips automatic api calls when version checks are disabled", async () => { + process.env.PI_SKIP_VERSION_CHECK = "1"; + const fetchMock = vi.fn(); + vi.stubGlobal("fetch", fetchMock); + + await expect(checkForNewPiVersion("1.2.3")).resolves.toBeUndefined(); + expect(fetchMock).not.toHaveBeenCalled(); + }); + + it("allows direct api calls when automatic version checks are disabled", async () => { + process.env.PI_SKIP_VERSION_CHECK = "1"; + const fetchMock = vi.fn(async () => Response.json({ version: "1.2.4" })); + vi.stubGlobal("fetch", fetchMock); + + await expect(getLatestPiVersion("1.2.3")).resolves.toBe("1.2.4"); + expect(fetchMock).toHaveBeenCalledOnce(); + }); +}); diff --git a/packages/tui/native/clipboard.h b/packages/tui/native/clipboard.h new file mode 100644 index 0000000000000000000000000000000000000000..0987b4ed67aff622e9b1a43864e7197a2788b03e --- /dev/null +++ b/packages/tui/native/clipboard.h @@ -0,0 +1,170 @@ +#ifndef PI_CLIPBOARD_H +#define PI_CLIPBOARD_H + +#include "napi.h" +#ifndef _WIN32 +#include +#include +#endif + +typedef enum { CLIPBOARD_TEXT, CLIPBOARD_IMAGE, CLIPBOARD_WRITE } clipboard_operation; +typedef enum { CLIPBOARD_UNAVAILABLE, CLIPBOARD_EMPTY, CLIPBOARD_UTF8, CLIPBOARD_LATIN1, CLIPBOARD_UTF16, CLIPBOARD_BUFFER } clipboard_format; + +typedef struct { + napi_async_work work; + napi_deferred deferred; + clipboard_operation operation; + clipboard_format format; + void* data; + size_t length; + const char* error; +} clipboard_job; + +// Only this function runs on the worker thread. It must not call N-API. +static void clipboard_execute(clipboard_job* job); + +static void* clipboard_alloc(size_t size) { +#ifdef _WIN32 + return GlobalAlloc(GPTR, size); +#else + return calloc(1, size); +#endif +} + +static void clipboard_free(void* data) { +#ifdef _WIN32 + if (data) GlobalFree(data); +#else + free(data); +#endif +} + +#ifdef PI_CLIPBOARD_WRITE +static void clipboard_copy(clipboard_job* job, const void* data, size_t length, clipboard_format format) { + job->data = clipboard_alloc(length ? length : 1); + if (!job->data) { + job->error = "Out of memory"; + return; + } +#ifdef _WIN32 + volatile uint8_t* target = job->data; + const volatile uint8_t* source = data; + for (size_t index = 0; index < length; index++) target[index] = source[index]; +#else + memcpy(job->data, data, length); +#endif + job->length = format == CLIPBOARD_UTF16 ? length / sizeof(uint16_t) : length; + job->format = format; +} +#endif + +static void PI_NAPI_CALL execute_clipboard_work(napi_env env, void* data) { + (void)env; + clipboard_execute(data); +} + +static void PI_NAPI_CALL complete_clipboard_work(napi_env env, int status, void* data) { + clipboard_job* job = data; + napi_create_string_utf8_fn create_string = (napi_create_string_utf8_fn)node_symbol("napi_create_string_utf8"); + napi_value result = undefined_value(env); + if (status != 0) job->error = "Clipboard operation cancelled"; + if (!job->error && job->operation != CLIPBOARD_WRITE) { + if (job->format == CLIPBOARD_EMPTY) { + result = null_value(env); + } else if (job->format == CLIPBOARD_BUFFER) { + napi_create_buffer_copy_fn create_buffer = (napi_create_buffer_copy_fn)node_symbol("napi_create_buffer_copy"); + status = create_buffer(env, job->length, job->data, 0, &result); + } else if (job->format == CLIPBOARD_UTF16) { + napi_create_string_utf16_fn create_utf16 = (napi_create_string_utf16_fn)node_symbol("napi_create_string_utf16"); + status = create_utf16(env, job->data, job->length, &result); + } else if (job->format == CLIPBOARD_UTF8 || job->format == CLIPBOARD_LATIN1) { + napi_create_string_utf8_fn create_text = job->format == CLIPBOARD_LATIN1 + ? (napi_create_string_utf8_fn)node_symbol("napi_create_string_latin1") : create_string; + status = create_text(env, job->data, job->length, &result); + } + if (status != 0) job->error = "Could not create clipboard value"; + } + if (job->error) { + napi_create_error_fn create_error = (napi_create_error_fn)node_symbol("napi_create_error"); + napi_value message; + create_string(env, job->error, NAPI_AUTO_LENGTH, &message); + create_error(env, 0, message, &result); + } + napi_settle_deferred_fn settle = (napi_settle_deferred_fn)node_symbol( + job->error ? "napi_reject_deferred" : "napi_resolve_deferred" + ); + settle(env, job->deferred, result); + napi_async_work_fn delete_work = (napi_async_work_fn)node_symbol("napi_delete_async_work"); + delete_work(env, job->work); + clipboard_free(job->data); + clipboard_free(job); +} + +static napi_value queue_clipboard(napi_env env, napi_callback_info info, clipboard_operation operation) { + napi_create_async_work_fn create_work = (napi_create_async_work_fn)node_symbol("napi_create_async_work"); + napi_async_work_fn queue_work = (napi_async_work_fn)node_symbol("napi_queue_async_work"); + napi_async_work_fn delete_work = (napi_async_work_fn)node_symbol("napi_delete_async_work"); + napi_create_promise_fn create_promise = (napi_create_promise_fn)node_symbol("napi_create_promise"); + napi_create_string_utf8_fn create_string = (napi_create_string_utf8_fn)node_symbol("napi_create_string_utf8"); + if (!create_work || !queue_work || !delete_work || !create_promise || !create_string) { + return fail(env, "Async clipboard API unavailable"); + } + clipboard_job* job = clipboard_alloc(sizeof(*job)); + if (!job) return fail(env, "Out of memory"); + job->operation = operation; +#ifdef PI_CLIPBOARD_WRITE + if (operation == CLIPBOARD_WRITE) { + napi_get_cb_info_fn get_info = (napi_get_cb_info_fn)node_symbol("napi_get_cb_info"); +#ifdef _WIN32 + napi_get_value_string_utf16_fn get_string = (napi_get_value_string_utf16_fn)node_symbol("napi_get_value_string_utf16"); + size_t unit_size = sizeof(uint16_t); +#else + napi_get_value_string_utf8_fn get_string = (napi_get_value_string_utf8_fn)node_symbol("napi_get_value_string_utf8"); + size_t unit_size = 1; +#endif + size_t argc = 1; + napi_value argument = 0; + if (!get_info || !get_string || get_info(env, info, &argc, &argument, 0, 0) != 0 || argc == 0 || + get_string(env, argument, 0, 0, &job->length) != 0) { + clipboard_free(job); + return fail(env, "setText requires a string"); + } + job->data = job->length < SIZE_MAX / unit_size + ? clipboard_alloc((job->length + 1) * unit_size) : 0; + if (!job->data || get_string(env, argument, job->data, job->length + 1, &job->length) != 0) { + clipboard_free(job->data); + clipboard_free(job); + return fail(env, "Could not read clipboard text"); + } + } +#else + (void)info; +#endif + napi_value name = 0; + napi_value promise = 0; + if (create_string(env, "pi.clipboard", NAPI_AUTO_LENGTH, &name) != 0 || + create_work(env, 0, name, execute_clipboard_work, complete_clipboard_work, job, &job->work) != 0 || + create_promise(env, &job->deferred, &promise) != 0 || queue_work(env, job->work) != 0) { + if (job->work) delete_work(env, job->work); + clipboard_free(job->data); + clipboard_free(job); + return fail(env, "Could not queue clipboard operation"); + } + return promise; +} + +static napi_value PI_NAPI_CALL get_clipboard_text(napi_env env, napi_callback_info info) { + return queue_clipboard(env, info, CLIPBOARD_TEXT); +} + +static napi_value PI_NAPI_CALL get_clipboard_image(napi_env env, napi_callback_info info) { + return queue_clipboard(env, info, CLIPBOARD_IMAGE); +} + +#ifdef PI_CLIPBOARD_WRITE +static napi_value PI_NAPI_CALL set_clipboard_text(napi_env env, napi_callback_info info) { + return queue_clipboard(env, info, CLIPBOARD_WRITE); +} +#endif + +#endif diff --git a/packages/tui/native/darwin/README.md b/packages/tui/native/darwin/README.md new file mode 100644 index 0000000000000000000000000000000000000000..2eb5c722e1f86001d41fc068e126a439c2324e8c --- /dev/null +++ b/packages/tui/native/darwin/README.md @@ -0,0 +1,22 @@ +# Darwin native prebuilds + +Provides modifier-key state and asynchronous text/image clipboard access using AppKit. + +## Building + +On macOS, run from the repository root: + +```sh +npm --prefix packages/tui run build:native:darwin +``` + +The build uses Apple clang and the macOS SDK found through `xcrun`. Either Intel or Apple Silicon can build both targets: arm64 (macOS 11+) and x64 (macOS 10.15+). + +Cross-building requires a Darwin toolchain with a macOS SDK and Mach-O linker, such as osxcross: + +```sh +CC=/path/to/osxcross/clang SDKROOT=/path/to/MacOSX.sdk \ + npm --prefix packages/tui run build:native:darwin +``` + +The SDK must be obtained and used under Apple's license. Plain clang or Zig alone is not sufficient. diff --git a/packages/tui/native/darwin/build.sh b/packages/tui/native/darwin/build.sh new file mode 100644 index 0000000000000000000000000000000000000000..65f7fa4f8cdc19be744b9519e95687bbb046df0e --- /dev/null +++ b/packages/tui/native/darwin/build.sh @@ -0,0 +1,69 @@ +#!/usr/bin/env bash + +set -euo pipefail + +script_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" +source_file="$script_dir/src/darwin-platform.m" + +if [[ -n "${CC:-}" ]]; then + compiler="$CC" +elif [[ "$(uname -s)" == "Darwin" ]] && command -v xcrun >/dev/null 2>&1; then + compiler="$(xcrun --find clang)" +else + compiler="clang" +fi + +if ! command -v "$compiler" >/dev/null 2>&1; then + echo "Darwin C compiler not found: $compiler" >&2 + exit 1 +fi + +sdk_flags=() +if [[ -n "${SDKROOT:-}" ]]; then + if [[ ! -d "$SDKROOT" ]]; then + echo "SDKROOT does not exist: $SDKROOT" >&2 + exit 1 + fi + sdk_flags=(-isysroot "$SDKROOT" "-F$SDKROOT/System/Library/Frameworks") +elif [[ "$(uname -s)" == "Darwin" ]]; then + sdkroot="$(xcrun --sdk macosx --show-sdk-path)" + sdk_flags=(-isysroot "$sdkroot" "-F$sdkroot/System/Library/Frameworks") +fi + +temporary_dir="$(mktemp -d "${TMPDIR:-/tmp}/pi-tui-darwin.XXXXXX")" +trap 'rm -rf "$temporary_dir"' EXIT + +build() { + local arch="$1" + local target="$2" + local output_dir="$script_dir/prebuilds/darwin-$arch" + local temporary_output="$temporary_dir/darwin-$arch/darwin-platform.node" + + mkdir -p "$(dirname "$temporary_output")" + "$compiler" \ + -fobjc-arc \ + -std=c11 \ + -Wall \ + -Wextra \ + -Oz \ + -flto \ + -fvisibility=hidden \ + "--target=$target" \ + "${sdk_flags[@]}" \ + -bundle \ + -undefined dynamic_lookup \ + -Wl,-dead_strip \ + -Wl,-x \ + -framework AppKit \ + -framework CoreGraphics \ + -framework Foundation \ + "$source_file" \ + -o "$temporary_output" + + mkdir -p "$output_dir" + install -m 755 "$temporary_output" "$output_dir/darwin-platform.node" + echo "Built $output_dir/darwin-platform.node" +} + +build arm64 arm64-apple-macos11.0 +build x64 x86_64-apple-macos10.15 diff --git a/packages/tui/native/darwin/prebuilds/darwin-arm64/darwin-platform.node b/packages/tui/native/darwin/prebuilds/darwin-arm64/darwin-platform.node new file mode 100644 index 0000000000000000000000000000000000000000..f453d0c06d5f17cad59ed232f6280564569e5708 Binary files /dev/null and b/packages/tui/native/darwin/prebuilds/darwin-arm64/darwin-platform.node differ diff --git a/packages/tui/native/darwin/prebuilds/darwin-x64/darwin-platform.node b/packages/tui/native/darwin/prebuilds/darwin-x64/darwin-platform.node new file mode 100644 index 0000000000000000000000000000000000000000..2f65289b29531152535f6d6f4a3d159d362c62e8 Binary files /dev/null and b/packages/tui/native/darwin/prebuilds/darwin-x64/darwin-platform.node differ diff --git a/packages/tui/native/darwin/src/darwin-platform.m b/packages/tui/native/darwin/src/darwin-platform.m new file mode 100644 index 0000000000000000000000000000000000000000..38823357ff763face0116ed5975a361296ac3aaa --- /dev/null +++ b/packages/tui/native/darwin/src/darwin-platform.m @@ -0,0 +1,93 @@ +#import +#import +#import +#include +#include +#define PI_CLIPBOARD_WRITE +#include "../../clipboard.h" + +static CGEventFlags modifier_mask_for_name(const char* name) { + if (strcmp(name, "shift") == 0) return kCGEventFlagMaskShift; + if (strcmp(name, "command") == 0) return kCGEventFlagMaskCommand; + if (strcmp(name, "control") == 0) return kCGEventFlagMaskControl; + if (strcmp(name, "option") == 0) return kCGEventFlagMaskAlternate; + return 0; +} + +static napi_value is_modifier_pressed(napi_env env, napi_callback_info info) { + napi_get_cb_info_fn napi_get_cb_info = (napi_get_cb_info_fn)node_symbol("napi_get_cb_info"); + napi_get_value_string_utf8_fn napi_get_value_string_utf8 = + (napi_get_value_string_utf8_fn)node_symbol("napi_get_value_string_utf8"); + napi_get_boolean_fn napi_get_boolean = (napi_get_boolean_fn)node_symbol("napi_get_boolean"); + + bool pressed = false; + if (napi_get_cb_info && napi_get_value_string_utf8) { + size_t argc = 1; + napi_value args[1] = {0}; + if (napi_get_cb_info(env, info, &argc, args, 0, 0) == 0 && argc >= 1 && args[0]) { + char name[16] = {0}; + size_t copied = 0; + if (napi_get_value_string_utf8(env, args[0], name, sizeof(name), &copied) == 0) { + CGEventFlags mask = modifier_mask_for_name(name); + if (mask != 0) { + CGEventFlags flags = CGEventSourceFlagsState(kCGEventSourceStateCombinedSessionState); + pressed = (flags & mask) != 0; + } + } + } + } + + napi_value result = 0; + if (!napi_get_boolean || napi_get_boolean(env, pressed, &result) != 0) { + return fail(env, "Could not inspect modifier state"); + } + return result; +} + +static void clipboard_execute(clipboard_job* job) { + @autoreleasepool { + NSPasteboard* pasteboard = [NSPasteboard generalPasteboard]; + if (!pasteboard) { + job->error = "Could not open clipboard"; + } else if (job->operation == CLIPBOARD_WRITE) { + NSString* text = [[NSString alloc] initWithBytes:job->data length:job->length encoding:NSUTF8StringEncoding]; + if (!text) { + job->error = "Clipboard text is not valid UTF-8"; + return; + } + [pasteboard clearContents]; + if (![pasteboard setString:text forType:NSPasteboardTypeString]) job->error = "Could not set clipboard text"; + } else if (job->operation == CLIPBOARD_TEXT) { + NSString* text = [pasteboard stringForType:NSPasteboardTypeString]; + if (!text) { + job->format = CLIPBOARD_EMPTY; + } else if (!text.UTF8String) { + job->error = "Could not encode clipboard text"; + } else { + clipboard_copy(job, text.UTF8String, [text lengthOfBytesUsingEncoding:NSUTF8StringEncoding], CLIPBOARD_UTF8); + } + } else { + if (![pasteboard availableTypeFromArray:@[ NSPasteboardTypePNG, NSPasteboardTypeTIFF ]]) { + job->format = CLIPBOARD_EMPTY; + return; + } + NSData* png = [pasteboard dataForType:NSPasteboardTypePNG]; + if (!png) { + NSImage* image = [[NSImage alloc] initWithPasteboard:pasteboard]; + NSData* tiff = image.TIFFRepresentation; + NSBitmapImageRep* bitmap = tiff ? [NSBitmapImageRep imageRepWithData:tiff] : nil; + png = bitmap ? [bitmap representationUsingType:NSBitmapImageFileTypePNG properties:@{}] : nil; + } + if (png) clipboard_copy(job, png.bytes, png.length, CLIPBOARD_BUFFER); + else job->error = "Clipboard does not contain an image"; + } + } +} + +PI_NAPI_EXPORT napi_value napi_register_module_v1(napi_env env, napi_value exports) { + set_function_export(env, exports, "isModifierPressed", is_modifier_pressed); + set_function_export(env, exports, "getText", get_clipboard_text); + set_function_export(env, exports, "setText", set_clipboard_text); + set_function_export(env, exports, "getImage", get_clipboard_image); + return exports; +} diff --git a/packages/tui/native/linux/README.md b/packages/tui/native/linux/README.md new file mode 100644 index 0000000000000000000000000000000000000000..5efdb5cb69af75d5b596423a11a8411138a43ccd --- /dev/null +++ b/packages/tui/native/linux/README.md @@ -0,0 +1,23 @@ +# Linux clipboard helper + +Provides asynchronous X11 text and image reads using `libxcb.so.1`. Prebuilds support x64 and arm64 on glibc and musl. Reads have bounded waits; if a native operation stalls, the helper remains unavailable until it finishes. + +Coding-agent falls back to command-line tools when native reads are unavailable. Wayland reads use `wl-paste`; all Linux writes use the existing command-line or terminal clipboard paths. + +## Building + +Install a C compiler and XCB development headers, then run from the repository root on each supported Linux architecture: + +```sh +npm --prefix packages/tui run build:native:linux +``` + +## Testing + +Install the build dependencies plus `pkg-config`, `Xvfb`, and `xclip`, then run from `packages/tui`: + +```sh +node --test test/native-clipboard-linux.test.ts +``` + +Tests use isolated X11 servers, not the desktop clipboard. They skip when dependencies are missing. diff --git a/packages/tui/native/linux/build.sh b/packages/tui/native/linux/build.sh new file mode 100644 index 0000000000000000000000000000000000000000..121b6c45d56020fb2bb78aa6f1a5627f0c094986 --- /dev/null +++ b/packages/tui/native/linux/build.sh @@ -0,0 +1,65 @@ +#!/usr/bin/env bash + +set -euo pipefail + +script_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" +compiler="${CC:-cc}" + +if [[ "$(uname -s)" != "Linux" ]]; then + echo "Linux native helpers must be built on Linux" >&2 + exit 1 +fi + +case "$(uname -m)" in + x86_64) + arch="x64" + ;; + aarch64|arm64) + arch="arm64" + ;; + *) + echo "Unsupported Linux architecture: $(uname -m)" >&2 + exit 1 + ;; +esac + +if ! command -v "$compiler" >/dev/null 2>&1; then + echo "Linux C compiler not found: $compiler" >&2 + exit 1 +fi +build_dir="$(mktemp -d "${TMPDIR:-/tmp}/pi-tui-linux.XXXXXX")" +trap 'rm -rf "$build_dir"' EXIT + +common_flags=( + -std=c11 + -D_POSIX_C_SOURCE=200809L + -Wall + -Wextra + -Werror + -Os + -flto + -fPIC + -pthread + -fvisibility=hidden + -fno-stack-protector + -shared + -nostdlib + -Wl,--unresolved-symbols=ignore-all + -Wl,--no-as-needed + -Wl,-O2 + -Wl,--gc-sections + # A timed-out private thread may outlive the Node environment that loaded us. + -Wl,-z,nodelete + -Wl,-s +) + +x11_output="$build_dir/linux-platform-x11.node" +"$compiler" "${common_flags[@]}" \ + "$script_dir/src/linux-platform-x11.c" \ + -lxcb \ + -o "$x11_output" + +output_dir="$script_dir/prebuilds/linux-$arch" +mkdir -p "$output_dir" +install -m 755 "$x11_output" "$output_dir/linux-platform-x11.node" +printf 'Built %s\n' "$output_dir/linux-platform-x11.node" diff --git a/packages/tui/native/linux/prebuilds/linux-arm64/linux-platform-x11.node b/packages/tui/native/linux/prebuilds/linux-arm64/linux-platform-x11.node new file mode 100644 index 0000000000000000000000000000000000000000..6d3abc4e33fe4fc8f9029cda1201a3a2aee5c050 Binary files /dev/null and b/packages/tui/native/linux/prebuilds/linux-arm64/linux-platform-x11.node differ diff --git a/packages/tui/native/linux/prebuilds/linux-x64/linux-platform-x11.node b/packages/tui/native/linux/prebuilds/linux-x64/linux-platform-x11.node new file mode 100644 index 0000000000000000000000000000000000000000..54fcd629b88533ee6c788a830038aa46cf78c1d7 Binary files /dev/null and b/packages/tui/native/linux/prebuilds/linux-x64/linux-platform-x11.node differ diff --git a/packages/tui/native/linux/src/clipboard-worker.h b/packages/tui/native/linux/src/clipboard-worker.h new file mode 100644 index 0000000000000000000000000000000000000000..93c79d0e7112096b86f38d6fdb1e21c0a72293d4 --- /dev/null +++ b/packages/tui/native/linux/src/clipboard-worker.h @@ -0,0 +1,70 @@ +#include +#include +#include "../../clipboard.h" + +static void read_clipboard(clipboard_job* job); +static pthread_mutex_t worker_mutex = PTHREAD_MUTEX_INITIALIZER; +static pthread_once_t worker_once = PTHREAD_ONCE_INIT; +static pthread_cond_t worker_changed; +static bool worker_ready, worker_busy, worker_waiting, worker_finished; +static clipboard_job worker_result; +static int (*signal_worker)(pthread_cond_t*); +static int (*wait_for_worker)(pthread_cond_t*, pthread_mutex_t*, const struct timespec*); + +static void initialize_worker_condition(void) { + // Unversioned ELF imports otherwise bind obsolete condition variables on glibc x64. + int (*initialize)(pthread_cond_t*, const pthread_condattr_t*) = node_symbol("pthread_cond_init"); + signal_worker = node_symbol("pthread_cond_signal"); + wait_for_worker = node_symbol("pthread_cond_timedwait"); + if (!initialize || !signal_worker || !wait_for_worker) return; + pthread_condattr_t attributes; + if (pthread_condattr_init(&attributes) != 0) return; + worker_ready = pthread_condattr_setclock(&attributes, CLOCK_MONOTONIC) == 0 && + initialize(&worker_changed, &attributes) == 0; + pthread_condattr_destroy(&attributes); +} + +static void* read_on_private_thread(void* unused) { + (void)unused; + read_clipboard(&worker_result); + pthread_mutex_lock(&worker_mutex); + worker_finished = true; + if (!worker_waiting) { + free(worker_result.data); // The caller timed out; discard the late result. + worker_busy = false; + } + signal_worker(&worker_changed); + pthread_mutex_unlock(&worker_mutex); + return 0; +} + +static void clipboard_execute(clipboard_job* job) { + pthread_once(&worker_once, initialize_worker_condition); + if (!worker_ready) return; + pthread_mutex_lock(&worker_mutex); + if (!worker_busy) { + worker_busy = worker_waiting = true; + worker_finished = false; + worker_result = (clipboard_job){.operation = job->operation}; + struct timespec deadline; + clock_gettime(CLOCK_MONOTONIC, &deadline); + deadline.tv_sec += 3; // Allow the two-second transfer deadline to report its own error first. + pthread_t thread; + if (pthread_create(&thread, 0, read_on_private_thread, 0) == 0) { + pthread_detach(thread); + // Only this bounded wait occupies libuv. Other callers return unavailable. + while (!worker_finished && wait_for_worker(&worker_changed, &worker_mutex, &deadline) == 0) {} + if (worker_finished) { + job->data = worker_result.data; + job->length = worker_result.length; + job->format = worker_result.format; + job->error = worker_result.error; + worker_busy = false; + } + } else { + worker_busy = false; + } + worker_waiting = false; + } + pthread_mutex_unlock(&worker_mutex); +} diff --git a/packages/tui/native/linux/src/linux-platform-x11.c b/packages/tui/native/linux/src/linux-platform-x11.c new file mode 100644 index 0000000000000000000000000000000000000000..578a850e43de0c4a00cff4f46c8d1cd31dc74a5f --- /dev/null +++ b/packages/tui/native/linux/src/linux-platform-x11.c @@ -0,0 +1,274 @@ +#include +#include +#include +#include +#include +#include +#include "clipboard-worker.h" + +#define MAX_CLIPBOARD_BYTES (50u * 1024u * 1024u) +#define CLIPBOARD_TIMEOUT_MS 2000 + +static const char* const text_types[] = { + "text/plain;charset=utf-8", "text/plain;charset=UTF-8", "UTF8_STRING", "text/plain", "STRING", +}; +static const char* const image_types[] = { + "image/png", "image/jpeg", "image/webp", "image/gif", "image/bmp", "image/tiff", +}; + +static int64_t monotonic_ms(void) { + struct timespec now; + clock_gettime(CLOCK_MONOTONIC, &now); + return (int64_t)now.tv_sec * 1000 + now.tv_nsec / 1000000; +} + +typedef struct { + unsigned char* data; + size_t length; + uint32_t items; + uint8_t format; + xcb_atom_t type; +} property_data; + +typedef struct { + xcb_connection_t* connection; + xcb_window_t window; + xcb_atom_t clipboard; + xcb_atom_t property; + xcb_atom_t targets; + xcb_atom_t incr; + int64_t deadline; +} x11_clipboard; + +static bool append_bytes(property_data* result, const unsigned char* bytes, size_t length) { + if (length > MAX_CLIPBOARD_BYTES - result->length) return false; + unsigned char* data = realloc(result->data, result->length + length + 1); + if (!data) return false; + memcpy(data + result->length, bytes, length); + result->length += length; + data[result->length] = 0; + result->data = data; + return true; +} + +// Share one deadline across discovery, replies, and incremental chunks. +// Connection setup and flushing are libxcb calls on the private thread. +static bool wait_for_input(x11_clipboard* clipboard) { + for (;;) { + int64_t remaining = clipboard->deadline - monotonic_ms(); + if (remaining <= 0 || xcb_connection_has_error(clipboard->connection)) return false; + struct pollfd descriptor = {xcb_get_file_descriptor(clipboard->connection), POLLIN, 0}; + int ready = poll(&descriptor, 1, (int)remaining); + if (ready >= 0) return ready && (descriptor.revents & POLLIN); + if (errno != EINTR) return false; + } +} + +static void* wait_for_reply(x11_clipboard* clipboard, unsigned sequence) { + if (xcb_flush(clipboard->connection) <= 0) return 0; + while (monotonic_ms() < clipboard->deadline) { + void* reply = 0; + xcb_generic_error_t* error = 0; + if (xcb_poll_for_reply(clipboard->connection, sequence, &reply, &error)) { + free(error); + return reply; + } + if (!wait_for_input(clipboard)) break; + } + xcb_discard_reply(clipboard->connection, sequence); + return 0; +} + +static xcb_generic_event_t* wait_for_event(x11_clipboard* clipboard, uint8_t type) { + if (xcb_flush(clipboard->connection) <= 0) return 0; + while (monotonic_ms() < clipboard->deadline) { + xcb_generic_event_t* event = xcb_poll_for_event(clipboard->connection); + if (event) { + if ((event->response_type & 0x7f) == type) return event; + free(event); + } else if (!wait_for_input(clipboard)) { + break; + } + } + return 0; +} + +static bool read_property(x11_clipboard* clipboard, bool remove, property_data* result) { + xcb_get_property_cookie_t cookie = xcb_get_property( + clipboard->connection, remove, clipboard->window, clipboard->property, + XCB_GET_PROPERTY_TYPE_ANY, 0, MAX_CLIPBOARD_BYTES / 4 + ); + xcb_get_property_reply_t* reply = wait_for_reply(clipboard, cookie.sequence); + if (!reply) return false; + bool copied = false; + bool valid_format = reply->format == 8 || reply->format == 16 || reply->format == 32; + uint64_t length = (uint64_t)reply->value_len * (reply->format / 8); + if (reply->bytes_after == 0 && reply->type != XCB_NONE && valid_format && + length <= MAX_CLIPBOARD_BYTES && length <= (uint64_t)reply->length * 4) { + result->type = reply->type; + result->format = reply->format; + result->items = reply->value_len; + copied = append_bytes(result, xcb_get_property_value(reply), (size_t)length); + } + free(reply); + return copied; +} + +static bool append_property(property_data* result, const property_data* chunk) { + if ((chunk->format != 8 && chunk->format != 16 && chunk->format != 32) || + chunk->type == XCB_NONE || + (uint64_t)chunk->items * (chunk->format / 8) != chunk->length || + (result->type != XCB_NONE && (result->type != chunk->type || result->format != chunk->format)) || + chunk->items > UINT32_MAX - result->items) return false; + if (!append_bytes(result, chunk->data, chunk->length)) return false; + result->type = chunk->type; + result->format = chunk->format; + result->items += chunk->items; + return true; +} + +static bool request_selection(x11_clipboard* clipboard, xcb_atom_t target, property_data* result) { + memset(result, 0, sizeof(*result)); + xcb_delete_property(clipboard->connection, clipboard->window, clipboard->property); + xcb_convert_selection( + clipboard->connection, clipboard->window, clipboard->clipboard, + target, clipboard->property, XCB_CURRENT_TIME + ); + + while (true) { + xcb_selection_notify_event_t* event = (xcb_selection_notify_event_t*)wait_for_event(clipboard, XCB_SELECTION_NOTIFY); + if (!event) return false; + bool matches = event->selection == clipboard->clipboard && event->target == target; + bool received = event->property != XCB_NONE; + free(event); + if (!matches) continue; + if (!received) return true; // No owner, or the owner does not offer this target. + if (!read_property(clipboard, false, result)) return false; + break; + } + if (result->type != clipboard->incr) return true; + + free(result->data); + memset(result, 0, sizeof(*result)); + xcb_delete_property(clipboard->connection, clipboard->window, clipboard->property); + + while (true) { + xcb_property_notify_event_t* event = (xcb_property_notify_event_t*)wait_for_event(clipboard, XCB_PROPERTY_NOTIFY); + if (!event) return false; + bool matches = event->atom == clipboard->property && event->state == XCB_PROPERTY_NEW_VALUE; + free(event); + if (!matches) continue; + + property_data chunk = {0}; + if (!read_property(clipboard, true, &chunk)) return false; + bool appended = append_property(result, &chunk); + bool finished = chunk.items == 0; + free(chunk.data); + if (!appended) return false; + if (finished) return true; + } +} + +static xcb_atom_t intern_atom(x11_clipboard* clipboard, const char* name) { + xcb_intern_atom_cookie_t cookie = xcb_intern_atom(clipboard->connection, false, strlen(name), name); + xcb_intern_atom_reply_t* reply = wait_for_reply(clipboard, cookie.sequence); + if (!reply) return XCB_NONE; + xcb_atom_t atom = reply->atom; + free(reply); + return atom; +} + +static bool open_clipboard(x11_clipboard* clipboard) { + memset(clipboard, 0, sizeof(*clipboard)); + clipboard->deadline = monotonic_ms() + CLIPBOARD_TIMEOUT_MS; + int screen_number = 0; + clipboard->connection = xcb_connect(0, &screen_number); + if (xcb_connection_has_error(clipboard->connection)) return false; + xcb_screen_iterator_t screens = xcb_setup_roots_iterator(xcb_get_setup(clipboard->connection)); + while (screen_number-- > 0 && screens.rem) xcb_screen_next(&screens); + if (!screens.rem) return false; + + clipboard->window = xcb_generate_id(clipboard->connection); + uint32_t mask = XCB_EVENT_MASK_PROPERTY_CHANGE; + xcb_create_window( + clipboard->connection, XCB_COPY_FROM_PARENT, clipboard->window, + screens.data->root, 0, 0, 1, 1, 0, XCB_WINDOW_CLASS_INPUT_OUTPUT, + XCB_COPY_FROM_PARENT, XCB_CW_EVENT_MASK, &mask + ); + clipboard->clipboard = intern_atom(clipboard, "CLIPBOARD"); + clipboard->property = intern_atom(clipboard, "PI_CLIPBOARD"); + clipboard->targets = intern_atom(clipboard, "TARGETS"); + clipboard->incr = intern_atom(clipboard, "INCR"); + return clipboard->clipboard && clipboard->property && clipboard->targets && clipboard->incr; +} + +static void close_clipboard(x11_clipboard* clipboard) { + if (clipboard->connection) xcb_disconnect(clipboard->connection); +} + +static bool preferred_target(x11_clipboard* clipboard, bool image, xcb_atom_t* target) { + const char* const* types = image ? image_types : text_types; + size_t type_count = image ? 6 : 5; + xcb_atom_t wanted[6]; + for (size_t index = 0; index < type_count; index++) { + wanted[index] = intern_atom(clipboard, types[index]); + if (wanted[index] == XCB_NONE) return false; + } + + property_data targets = {0}; + bool received = request_selection(clipboard, clipboard->targets, &targets); + bool valid = targets.type == XCB_ATOM_ATOM && targets.format == 32 && + targets.items == targets.length / sizeof(xcb_atom_t) && targets.length % sizeof(xcb_atom_t) == 0; + if (received && valid) { + xcb_atom_t* offered = (xcb_atom_t*)targets.data; + for (size_t wanted_index = 0; wanted_index < type_count; wanted_index++) { + for (uint32_t offered_index = 0; offered_index < targets.items; offered_index++) { + if (offered[offered_index] == wanted[wanted_index]) { + free(targets.data); + *target = wanted[wanted_index]; + return true; + } + } + } + } + free(targets.data); + // Some owners refuse TARGETS but still serve text directly. + if (received && !image && targets.type == XCB_NONE) { + *target = intern_atom(clipboard, "UTF8_STRING"); + return *target != XCB_NONE; + } + return received && (valid || targets.type == XCB_NONE); +} + +static bool read_selection(x11_clipboard* clipboard, bool image, property_data* result) { + xcb_atom_t target = XCB_NONE; + bool received = preferred_target(clipboard, image, &target) && + (target == XCB_NONE || request_selection(clipboard, target, result)); + if (!received) { + free(result->data); + memset(result, 0, sizeof(*result)); + } + return received; +} + +static void read_clipboard(clipboard_job* job) { + x11_clipboard clipboard; + if (open_clipboard(&clipboard)) { + property_data contents = {0}; + if (!read_selection(&clipboard, job->operation == CLIPBOARD_IMAGE, &contents)) { + job->error = "Could not read X11 clipboard"; + } else { + job->data = contents.data; + job->length = contents.length; + job->format = !contents.data ? CLIPBOARD_EMPTY : job->operation == CLIPBOARD_IMAGE + ? CLIPBOARD_BUFFER : contents.type == XCB_ATOM_STRING ? CLIPBOARD_LATIN1 : CLIPBOARD_UTF8; + } + } + close_clipboard(&clipboard); +} + +PI_NAPI_EXPORT napi_value napi_register_module_v1(napi_env env, napi_value exports) { + set_function_export(env, exports, "getText", get_clipboard_text); + set_function_export(env, exports, "getImage", get_clipboard_image); + return exports; +} diff --git a/packages/tui/native/napi.h b/packages/tui/native/napi.h new file mode 100644 index 0000000000000000000000000000000000000000..93c985c673dcc3a7006e81bcf1c97cc3df7dcc87 --- /dev/null +++ b/packages/tui/native/napi.h @@ -0,0 +1,89 @@ +#ifndef PI_NAPI_H +#define PI_NAPI_H + +// Resolve the small N-API surface we use from Node or Bun, without Node headers +// or a link-time dependency on a particular runtime. +#include +#include +#include +#ifdef _WIN32 +#include +#define PI_NAPI_CALL __cdecl +#define PI_NAPI_EXPORT __declspec(dllexport) +#else +#include +#define PI_NAPI_CALL +#define PI_NAPI_EXPORT __attribute__((visibility("default"))) +#endif + +#define NAPI_AUTO_LENGTH ((size_t)-1) + +typedef void* napi_env; +typedef void* napi_value; +typedef void* napi_callback_info; +typedef void* napi_async_work; +typedef void* napi_deferred; +typedef void (PI_NAPI_CALL *napi_async_execute_callback)(napi_env, void*); +typedef void (PI_NAPI_CALL *napi_async_complete_callback)(napi_env, int, void*); +typedef int (PI_NAPI_CALL *napi_create_async_work_fn)(napi_env, napi_value, napi_value, napi_async_execute_callback, napi_async_complete_callback, void*, napi_async_work*); +typedef int (PI_NAPI_CALL *napi_async_work_fn)(napi_env, napi_async_work); +typedef int (PI_NAPI_CALL *napi_create_promise_fn)(napi_env, napi_deferred*, napi_value*); +typedef int (PI_NAPI_CALL *napi_settle_deferred_fn)(napi_env, napi_deferred, napi_value); +typedef int (PI_NAPI_CALL *napi_create_error_fn)(napi_env, napi_value, napi_value, napi_value*); +typedef napi_value (PI_NAPI_CALL *napi_callback)(napi_env, napi_callback_info); +typedef int (PI_NAPI_CALL *napi_create_buffer_copy_fn)(napi_env, size_t, const void*, void**, napi_value*); +typedef int (PI_NAPI_CALL *napi_create_function_fn)(napi_env, const char*, size_t, napi_callback, void*, napi_value*); +typedef int (PI_NAPI_CALL *napi_create_string_utf8_fn)(napi_env, const char*, size_t, napi_value*); +typedef int (PI_NAPI_CALL *napi_create_string_utf16_fn)(napi_env, const uint16_t*, size_t, napi_value*); +typedef int (PI_NAPI_CALL *napi_get_boolean_fn)(napi_env, bool, napi_value*); +typedef int (PI_NAPI_CALL *napi_get_cb_info_fn)(napi_env, napi_callback_info, size_t*, napi_value*, napi_value*, void**); +typedef int (PI_NAPI_CALL *napi_get_value_fn)(napi_env, napi_value*); +typedef int (PI_NAPI_CALL *napi_get_value_string_utf8_fn)(napi_env, napi_value, char*, size_t, size_t*); +typedef int (PI_NAPI_CALL *napi_get_value_string_utf16_fn)(napi_env, napi_value, uint16_t*, size_t, size_t*); +typedef int (PI_NAPI_CALL *napi_set_named_property_fn)(napi_env, napi_value, const char*, napi_value); +typedef int (PI_NAPI_CALL *napi_throw_error_fn)(napi_env, const char*, const char*); + +static void* node_symbol(const char* name) { +#ifdef _WIN32 + HMODULE module = GetModuleHandleA(0); + void* proc = module ? (void*)GetProcAddress(module, name) : 0; + if (proc) return proc; + module = GetModuleHandleA("node.dll"); + return module ? (void*)GetProcAddress(module, name) : 0; +#else + return dlsym(RTLD_DEFAULT, name); +#endif +} + +static napi_value undefined_value(napi_env env) { + napi_get_value_fn get_value = (napi_get_value_fn)node_symbol("napi_get_undefined"); + napi_value result = 0; + if (get_value) get_value(env, &result); + return result; +} + +static napi_value null_value(napi_env env) { + napi_get_value_fn get_value = (napi_get_value_fn)node_symbol("napi_get_null"); + napi_value result = 0; + if (get_value) get_value(env, &result); + return result; +} + +static napi_value fail(napi_env env, const char* message) { + napi_throw_error_fn napi_throw_error = (napi_throw_error_fn)node_symbol("napi_throw_error"); + if (napi_throw_error) napi_throw_error(env, 0, message); + return undefined_value(env); +} + +static void set_function_export(napi_env env, napi_value exports, const char* name, napi_callback callback) { + napi_create_function_fn napi_create_function = (napi_create_function_fn)node_symbol("napi_create_function"); + napi_set_named_property_fn napi_set_named_property = + (napi_set_named_property_fn)node_symbol("napi_set_named_property"); + napi_value fn = 0; + if (napi_create_function && napi_set_named_property && + napi_create_function(env, name, NAPI_AUTO_LENGTH, callback, 0, &fn) == 0) { + napi_set_named_property(env, exports, name, fn); + } +} + +#endif diff --git a/packages/tui/native/win32/README.md b/packages/tui/native/win32/README.md new file mode 100644 index 0000000000000000000000000000000000000000..f3b58607cbaa893d1cdff1b0321ed39e793677ea --- /dev/null +++ b/packages/tui/native/win32/README.md @@ -0,0 +1,31 @@ +# Windows native prebuilds + +Provides console input setup, modifier-key state, and asynchronous text/image clipboard access. Links to `kernel32` and `user32`; no Node headers are required. + +## Building + +On Windows, install Visual Studio's "Desktop development with C++" workload, then run from the repository root to build x64 and arm64: + +```sh +npm --prefix packages/tui run build:native:win32 +``` + +For cross-builds or custom toolchains, provide MinGW-compatible compilers: + +```sh +PI_TUI_WIN32_TOOLCHAIN=mingw \ +CC_X64=/path/to/x86_64-w64-mingw32-gcc \ +CC_ARM64=/path/to/aarch64-w64-mingw32-gcc \ +npm --prefix packages/tui run build:native:win32 +``` + +## Testing clipboard writes + +On a Windows test desktop, run from `packages/tui` in PowerShell: + +```powershell +$env:PI_TEST_NATIVE_CLIPBOARD = "1" +node --test test/native-platform.test.ts +``` + +This opt-in test checks native text writes and reads. **It replaces the system clipboard contents.** diff --git a/packages/tui/native/win32/build.mjs b/packages/tui/native/win32/build.mjs new file mode 100644 index 0000000000000000000000000000000000000000..df1ed206131af71e22d042f5924649e7d742c09b --- /dev/null +++ b/packages/tui/native/win32/build.mjs @@ -0,0 +1,232 @@ +import { spawnSync } from "node:child_process"; +import { chmodSync, copyFileSync, existsSync, mkdirSync, mkdtempSync, rmSync, writeFileSync } from "node:fs"; +import { tmpdir } from "node:os"; +import * as path from "node:path"; +import { fileURLToPath } from "node:url"; + +const scriptDir = path.dirname(fileURLToPath(import.meta.url)); +const sourceFile = path.join(scriptDir, "src", "win32-platform.c"); +const temporaryDir = mkdtempSync(path.join(tmpdir(), "pi-tui-win32-")); + +const targets = [ + { + arch: "x64", + msvcArch: "x64", + mingwTarget: "x86_64-w64-windows-gnu", + prefixedCompilers: ["x86_64-w64-mingw32-gcc", "x86_64-w64-mingw32-clang"], + envCompiler: "CC_X64", + }, + { + arch: "arm64", + msvcArch: "arm64", + mingwTarget: "aarch64-w64-windows-gnu", + prefixedCompilers: ["aarch64-w64-mingw32-gcc", "aarch64-w64-mingw32-clang"], + envCompiler: "CC_ARM64", + }, +]; + +function removeTemporaryDir() { + rmSync(temporaryDir, { recursive: true, force: true, maxRetries: 5, retryDelay: 100 }); +} + +process.on("exit", removeTemporaryDir); +process.on("SIGINT", () => { + removeTemporaryDir(); + process.exit(130); +}); + +function commandExists(command) { + const result = spawnSync(command, ["--version"], { stdio: "ignore" }); + return !result.error || result.error.code !== "ENOENT"; +} + +function run(command, args, options = {}) { + const result = spawnSync(command, args, { stdio: "inherit", ...options }); + if (result.error) throw result.error; + if (result.status !== 0) throw new Error(`${command} failed with exit code ${result.status ?? 1}`); +} + +function quoteBatch(value) { + return `"${value.replaceAll('"', '""')}"`; +} + +function msvcHostArch() { + return process.arch === "arm64" ? "arm64" : "x64"; +} + +function findVisualStudioDevCmd() { + const candidates = []; + const programFilesX86 = process.env["ProgramFiles(x86)"]; + if (programFilesX86) { + const vswhere = path.join(programFilesX86, "Microsoft Visual Studio", "Installer", "vswhere.exe"); + if (existsSync(vswhere)) { + const result = spawnSync( + vswhere, + [ + "-latest", + "-products", + "*", + "-requires", + "Microsoft.VisualStudio.Component.VC.Tools.x86.x64", + "-property", + "installationPath", + ], + { encoding: "utf8" }, + ); + for (const line of result.stdout.split(/\r?\n/)) { + const installationPath = line.trim(); + if (installationPath) candidates.push(path.join(installationPath, "Common7", "Tools", "VsDevCmd.bat")); + } + } + } + + for (const base of [process.env.ProgramFiles, programFilesX86]) { + if (!base) continue; + for (const edition of ["BuildTools", "Community", "Professional", "Enterprise"]) { + candidates.push(path.join(base, "Microsoft Visual Studio", "2022", edition, "Common7", "Tools", "VsDevCmd.bat")); + } + } + + return candidates.find((candidate) => existsSync(candidate)); +} + +function canUseMsvc(vsDevCmd) { + const probeDir = path.join(temporaryDir, "msvc-probe"); + mkdirSync(probeDir, { recursive: true }); + const batchFile = path.join(probeDir, "probe.cmd"); + writeFileSync( + batchFile, + [ + "@echo off", + `call ${quoteBatch(vsDevCmd)} -no_logo -arch=x64 -host_arch=${msvcHostArch()} >nul`, + "if errorlevel 1 exit /b %errorlevel%", + "where cl.exe >nul 2>nul", + ].join("\r\n"), + ); + const result = spawnSync("cmd.exe", ["/d", "/s", "/c", batchFile], { cwd: probeDir, stdio: "ignore" }); + return !result.error && result.status === 0; +} + +function resolveMingwCompiler(target) { + const envCompiler = process.env[target.envCompiler]; + if (envCompiler) return { command: envCompiler, useTargetFlag: false }; + if (process.env.CC) return { command: process.env.CC, useTargetFlag: true }; + for (const command of target.prefixedCompilers) { + if (commandExists(command)) return { command, useTargetFlag: false }; + } + if (commandExists("clang")) return { command: "clang", useTargetFlag: true }; + return undefined; +} + +function installOutput(temporaryOutput, target) { + const outputDir = path.join(scriptDir, "prebuilds", `win32-${target.arch}`); + mkdirSync(outputDir, { recursive: true }); + const output = path.join(outputDir, "win32-platform.node"); + copyFileSync(temporaryOutput, output); + chmodSync(output, 0o755); + console.log(`Built ${path.relative(process.cwd(), output)}`); +} + +function buildWithMsvc(target, vsDevCmd) { + const buildDir = path.join(temporaryDir, `msvc-${target.arch}`); + const temporaryOutput = path.join(buildDir, "win32-platform.node"); + mkdirSync(buildDir, { recursive: true }); + + const batchFile = path.join(buildDir, "build.cmd"); + writeFileSync( + batchFile, + [ + "@echo off", + `call ${quoteBatch(vsDevCmd)} -no_logo -arch=${target.msvcArch} -host_arch=${msvcHostArch()}`, + "if errorlevel 1 exit /b %errorlevel%", + [ + "cl", + "/nologo", + "/TC", + "/std:c11", + "/W4", + "/O1", + "/GS-", + "/LD", + `/Fe:${quoteBatch(temporaryOutput)}`, + quoteBatch(sourceFile), + "/link", + "/NOENTRY", + "/NODEFAULTLIB", + "kernel32.lib", + "user32.lib", + "/OPT:REF", + "/OPT:ICF", + ].join(" "), + "if errorlevel 1 exit /b %errorlevel%", + ].join("\r\n"), + ); + + run("cmd.exe", ["/d", "/s", "/c", batchFile], { cwd: buildDir }); + installOutput(temporaryOutput, target); +} + +function buildWithMingw(target) { + const compiler = resolveMingwCompiler(target); + if (!compiler) { + throw new Error( + `No Windows cross-compiler found for ${target.arch}. Install Microsoft C++ Build Tools on Windows, or set ${target.envCompiler} to a MinGW-compatible compiler.`, + ); + } + + const buildDir = path.join(temporaryDir, `mingw-${target.arch}`); + const temporaryOutput = path.join(buildDir, "win32-platform.node"); + mkdirSync(buildDir, { recursive: true }); + + const args = [ + ...(compiler.useTargetFlag ? [`--target=${target.mingwTarget}`] : []), + "-std=c11", + "-Wall", + "-Wextra", + "-Oz", + "-fno-builtin", + "-shared", + "-nostdlib", + "-Wl,--no-entry", + "-Wl,--strip-all", + sourceFile, + "-lkernel32", + "-luser32", + "-o", + temporaryOutput, + ]; + + run(compiler.command, args, { cwd: buildDir }); + installOutput(temporaryOutput, target); +} + +if (process.argv.includes("--help") || process.argv.includes("-h")) { + console.log(`Usage: npm --prefix packages/tui run build:native:win32 + +Builds win32-x64 and win32-arm64 native prebuilds. + +Environment: + PI_TUI_WIN32_TOOLCHAIN=msvc|mingw + CC_X64=/path/to/x86_64-w64-mingw32-gcc + CC_ARM64=/path/to/aarch64-w64-mingw32-gcc + CC=/path/to/clang`); + process.exit(0); +} + +const requestedToolchain = process.env.PI_TUI_WIN32_TOOLCHAIN; +if (requestedToolchain && requestedToolchain !== "msvc" && requestedToolchain !== "mingw") { + throw new Error("PI_TUI_WIN32_TOOLCHAIN must be either 'msvc' or 'mingw'"); +} + +const vsDevCmd = findVisualStudioDevCmd(); +const hasMsvc = vsDevCmd ? canUseMsvc(vsDevCmd) : false; +const toolchain = requestedToolchain ?? (process.platform === "win32" && vsDevCmd ? "msvc" : "mingw"); + +if (toolchain === "msvc") { + if (!vsDevCmd || !hasMsvc) { + throw new Error("Microsoft C++ Build Tools not found. Install the Visual Studio C++ workload or set PI_TUI_WIN32_TOOLCHAIN=mingw."); + } + for (const target of targets) buildWithMsvc(target, vsDevCmd); +} else { + for (const target of targets) buildWithMingw(target); +} diff --git a/packages/tui/native/win32/prebuilds/win32-arm64/win32-platform.node b/packages/tui/native/win32/prebuilds/win32-arm64/win32-platform.node new file mode 100644 index 0000000000000000000000000000000000000000..ec330840e70b863b2b0f295437b78378fb8ac84b Binary files /dev/null and b/packages/tui/native/win32/prebuilds/win32-arm64/win32-platform.node differ diff --git a/packages/tui/native/win32/prebuilds/win32-x64/win32-platform.node b/packages/tui/native/win32/prebuilds/win32-x64/win32-platform.node new file mode 100644 index 0000000000000000000000000000000000000000..2d0b749b53a17d6049f3e1629c73a847d3851d04 Binary files /dev/null and b/packages/tui/native/win32/prebuilds/win32-x64/win32-platform.node differ diff --git a/packages/tui/native/win32/src/win32-platform.c b/packages/tui/native/win32/src/win32-platform.c new file mode 100644 index 0000000000000000000000000000000000000000..a637dda892b695a525ca2cb6b22ee9a8a55cae59 --- /dev/null +++ b/packages/tui/native/win32/src/win32-platform.c @@ -0,0 +1,241 @@ +#define PI_CLIPBOARD_WRITE +#include "../../clipboard.h" + +#ifndef BI_ALPHABITFIELDS +#define BI_ALPHABITFIELDS 6 +#endif + +#define KEY_PRESSED_MASK 0x8000 +#define OPEN_CLIPBOARD_ATTEMPTS 10 +#define OPEN_CLIPBOARD_RETRY_MS 5 +#define BITMAP_FILE_HEADER_SIZE 14 + +static int string_equals(const char* left, const char* right) { + while (*left && *right && *left == *right) { + left++; + right++; + } + return *left == 0 && *right == 0; +} + +static int is_key_pressed(int virtual_key) { + return ((unsigned short)GetAsyncKeyState(virtual_key) & KEY_PRESSED_MASK) != 0; +} + +static int is_modifier_name_pressed(const char* name) { + if (string_equals(name, "shift")) return is_key_pressed(VK_SHIFT) || is_key_pressed(VK_LSHIFT) || is_key_pressed(VK_RSHIFT); + if (string_equals(name, "control")) return is_key_pressed(VK_CONTROL) || is_key_pressed(VK_LCONTROL) || is_key_pressed(VK_RCONTROL); + if (string_equals(name, "option") || string_equals(name, "alt")) return is_key_pressed(VK_MENU) || is_key_pressed(VK_LMENU) || is_key_pressed(VK_RMENU); + if (string_equals(name, "command") || string_equals(name, "super") || string_equals(name, "win")) return is_key_pressed(VK_LWIN) || is_key_pressed(VK_RWIN); + return 0; +} + +static napi_value __cdecl enable_virtual_terminal_input(napi_env env, napi_callback_info info) { + (void)info; + + HANDLE handle = GetStdHandle(STD_INPUT_HANDLE); + DWORD mode = 0; + bool enabled = handle != INVALID_HANDLE_VALUE && + GetConsoleMode(handle, &mode) && + SetConsoleMode(handle, mode | ENABLE_VIRTUAL_TERMINAL_INPUT); + + napi_get_boolean_fn napi_get_boolean = (napi_get_boolean_fn)node_symbol("napi_get_boolean"); + napi_value result = 0; + if (!napi_get_boolean || napi_get_boolean(env, enabled, &result) != 0) { + return fail(env, "Could not configure console input"); + } + return result; +} + +static napi_value __cdecl is_modifier_pressed(napi_env env, napi_callback_info info) { + napi_get_cb_info_fn napi_get_cb_info = (napi_get_cb_info_fn)node_symbol("napi_get_cb_info"); + napi_get_value_string_utf8_fn napi_get_value_string_utf8 = + (napi_get_value_string_utf8_fn)node_symbol("napi_get_value_string_utf8"); + napi_get_boolean_fn napi_get_boolean = (napi_get_boolean_fn)node_symbol("napi_get_boolean"); + + bool pressed = false; + if (napi_get_cb_info && napi_get_value_string_utf8) { + size_t argc = 1; + napi_value args[1] = {0}; + if (napi_get_cb_info(env, info, &argc, args, 0, 0) == 0 && argc >= 1 && args[0]) { + char name[16] = {0}; + size_t copied = 0; + if (napi_get_value_string_utf8(env, args[0], name, sizeof(name), &copied) == 0) { + pressed = is_modifier_name_pressed(name); + } + } + } + + napi_value result = 0; + if (!napi_get_boolean || napi_get_boolean(env, pressed, &result) != 0) { + return fail(env, "Could not inspect modifier state"); + } + return result; +} + +static bool open_clipboard(HWND owner) { + for (int attempt = 0; attempt < OPEN_CLIPBOARD_ATTEMPTS; attempt++) { + if (OpenClipboard(owner)) return true; + Sleep(OPEN_CLIPBOARD_RETRY_MS); + } + return false; +} + +static uint16_t read_u16(const uint8_t* data) { + return (uint16_t)data[0] | ((uint16_t)data[1] << 8); +} + +static uint32_t read_u32(const uint8_t* data) { + return (uint32_t)data[0] | + ((uint32_t)data[1] << 8) | + ((uint32_t)data[2] << 16) | + ((uint32_t)data[3] << 24); +} + +static void write_u32(uint8_t* data, uint32_t value) { + data[0] = (uint8_t)value; + data[1] = (uint8_t)(value >> 8); + data[2] = (uint8_t)(value >> 16); + data[3] = (uint8_t)(value >> 24); +} + +static void copy_bytes(uint8_t* destination, const uint8_t* source, size_t length) { + volatile uint8_t* target = destination; + const volatile uint8_t* input = source; + for (size_t index = 0; index < length; index++) target[index] = input[index]; +} + +static size_t dib_pixel_offset(const uint8_t* dib, size_t size) { + if (size < 12) return 0; + uint32_t header_size = read_u32(dib); + + if (header_size == 12) { + uint16_t bits_per_pixel = read_u16(dib + 10); + uint32_t color_count = bits_per_pixel <= 8 ? 1u << bits_per_pixel : 0; + size_t offset = header_size + (size_t)color_count * 3; + return offset <= size ? offset : 0; + } + + if (header_size < 40 || header_size > size) return 0; + uint16_t bits_per_pixel = read_u16(dib + 14); + uint32_t compression = read_u32(dib + 16); + uint32_t color_count = read_u32(dib + 32); + if (!color_count && bits_per_pixel <= 8) color_count = 1u << bits_per_pixel; + + size_t offset = header_size + (size_t)color_count * 4; + if (header_size == 40 && compression == BI_BITFIELDS) offset += 3 * sizeof(uint32_t); + if (header_size == 40 && compression == BI_ALPHABITFIELDS) offset += 4 * sizeof(uint32_t); + return offset <= size ? offset : 0; +} + +static void read_clipboard_image(clipboard_job* job) { + UINT png_format = RegisterClipboardFormatW(L"PNG"); + if (!(png_format && IsClipboardFormatAvailable(png_format)) && + !IsClipboardFormatAvailable(CF_DIBV5) && !IsClipboardFormatAvailable(CF_DIB)) { + job->format = CLIPBOARD_EMPTY; + return; + } + HGLOBAL handle = png_format && IsClipboardFormatAvailable(png_format) + ? (HGLOBAL)GetClipboardData(png_format) : 0; + bool is_png = handle != 0; + if (!handle && IsClipboardFormatAvailable(CF_DIBV5)) handle = (HGLOBAL)GetClipboardData(CF_DIBV5); + if (!handle && IsClipboardFormatAvailable(CF_DIB)) handle = (HGLOBAL)GetClipboardData(CF_DIB); + + const uint8_t* source = handle ? (const uint8_t*)GlobalLock(handle) : 0; + size_t size = handle ? GlobalSize(handle) : 0; + if (!source || !size) { + if (source) GlobalUnlock(handle); + job->error = "Clipboard does not contain an image"; + return; + } + if (is_png) { + clipboard_copy(job, source, size, CLIPBOARD_BUFFER); + } else { + size_t offset = dib_pixel_offset(source, size); + uint8_t* bitmap = offset && size <= UINT32_MAX - BITMAP_FILE_HEADER_SIZE + ? clipboard_alloc(size + BITMAP_FILE_HEADER_SIZE) : 0; + if (bitmap) { + bitmap[0] = 'B'; + bitmap[1] = 'M'; + write_u32(bitmap + 2, (uint32_t)(size + BITMAP_FILE_HEADER_SIZE)); + write_u32(bitmap + 10, (uint32_t)(BITMAP_FILE_HEADER_SIZE + offset)); + copy_bytes(bitmap + BITMAP_FILE_HEADER_SIZE, source, size); + job->data = bitmap; + job->length = size + BITMAP_FILE_HEADER_SIZE; + job->format = CLIPBOARD_BUFFER; + } else { + job->error = "Could not create clipboard image buffer"; + } + } + GlobalUnlock(handle); +} + +static void clipboard_execute(clipboard_job* job) { + // A real clipboard owner is required for text writes, including under Node + // without a console. Create and destroy it on the same worker thread. + HWND owner = 0; + if (job->operation == CLIPBOARD_WRITE) { + owner = CreateWindowExW(0, L"STATIC", 0, 0, 0, 0, 0, 0, + HWND_MESSAGE, 0, GetModuleHandleA(0), 0); + if (!owner) { + job->error = "Could not create clipboard owner window"; + return; + } + } + if (!open_clipboard(owner)) { + if (owner) DestroyWindow(owner); + job->error = "Could not open clipboard"; + return; + } + if (job->operation == CLIPBOARD_WRITE) { + size_t size = (job->length + 1) * sizeof(uint16_t); + HGLOBAL handle = GlobalAlloc(GMEM_MOVEABLE, size); + void* text = handle ? GlobalLock(handle) : 0; + bool success = false; + if (text) { + copy_bytes(text, job->data, size); + GlobalUnlock(handle); + success = EmptyClipboard() && SetClipboardData(CF_UNICODETEXT, handle) != 0; + } + if (!success) { + if (handle) GlobalFree(handle); + job->error = "Could not set clipboard text"; + } + } else if (job->operation == CLIPBOARD_TEXT) { + if (!IsClipboardFormatAvailable(CF_UNICODETEXT)) { + job->format = CLIPBOARD_EMPTY; + } else { + HGLOBAL handle = (HGLOBAL)GetClipboardData(CF_UNICODETEXT); + const uint16_t* text = handle ? GlobalLock(handle) : 0; + if (text) { + size_t capacity = GlobalSize(handle) / sizeof(uint16_t); + size_t length = 0; + while (length < capacity && text[length]) length++; + clipboard_copy(job, text, length * sizeof(uint16_t), CLIPBOARD_UTF16); + GlobalUnlock(handle); + } else { + job->error = "Clipboard does not contain text"; + } + } + } else { + read_clipboard_image(job); + } + CloseClipboard(); + if (owner) DestroyWindow(owner); +} + +BOOL WINAPI _DllMainCRTStartup(HINSTANCE instance, DWORD reason, LPVOID reserved) { + (void)instance; + (void)reason; + (void)reserved; + return TRUE; +} + +PI_NAPI_EXPORT napi_value PI_NAPI_CALL napi_register_module_v1(napi_env env, napi_value exports) { + set_function_export(env, exports, "enableVirtualTerminalInput", enable_virtual_terminal_input); + set_function_export(env, exports, "isModifierPressed", is_modifier_pressed); + set_function_export(env, exports, "getText", get_clipboard_text); + set_function_export(env, exports, "setText", set_clipboard_text); + set_function_export(env, exports, "getImage", get_clipboard_image); + return exports; +} diff --git a/packages/tui/src/alt-screen-search.ts b/packages/tui/src/alt-screen-search.ts new file mode 100644 index 0000000000000000000000000000000000000000..2ed3821323723515523075ac143ce5e25fdcd7c7 --- /dev/null +++ b/packages/tui/src/alt-screen-search.ts @@ -0,0 +1,327 @@ +import { Input } from "./components/input.ts"; +import { getKeybindings } from "./keybindings.ts"; +import type { Component, Focusable } from "./tui.ts"; +import { getGraphemeSegmenter, stripTerminalSequences, truncateToWidth, visibleWidth } from "./utils.ts"; + +const segmenter = getGraphemeSegmenter(); + +interface SearchSourceSpan { + textStart: number; + textEnd: number; + row: number; + startCol: number; + endCol: number; + linearColumns: boolean; +} + +interface SearchCorpus { + text: string; + spans: SearchSourceSpan[]; +} + +export interface AltScreenSearchSegment { + row: number; + startCol: number; + endCol: number; +} + +export interface AltScreenSearchMatch { + segments: AltScreenSearchSegment[]; +} + +const PRINTABLE_ASCII = /^[\x20-\x7e]*$/; + +function buildSearchCorpus(lines: readonly string[]): SearchCorpus { + const chunks: string[] = []; + const spans: SearchSourceSpan[] = []; + let textLength = 0; + let pendingSeparator = false; + + const appendSeparator = (): void => { + if (!pendingSeparator) return; + chunks.push(" "); + textLength += 1; + pendingSeparator = false; + }; + + for (let row = 0; row < lines.length; row++) { + const line = stripTerminalSequences(lines[row] ?? ""); + let column = 0; + + // Rendered transcripts are overwhelmingly ASCII. Index complete non-space + // runs at once instead of segmenting and allocating one mapping per cell. + if (PRINTABLE_ASCII.test(line)) { + let index = 0; + while (index < line.length) { + if (line.charCodeAt(index) === 0x20) { + if (textLength > 0) pendingSeparator = true; + column += 1; + index += 1; + continue; + } + let end = index + 1; + while (end < line.length && line.charCodeAt(end) !== 0x20) end += 1; + appendSeparator(); + const text = line.slice(index, end); + chunks.push(text); + spans.push({ + textStart: textLength, + textEnd: textLength + text.length, + row, + startCol: column, + endCol: column + text.length, + linearColumns: true, + }); + textLength += text.length; + column += text.length; + index = end; + } + } else { + for (const grapheme of segmenter.segment(line)) { + const text = grapheme.segment; + const width = visibleWidth(text); + if (/^\s+$/u.test(text)) { + if (textLength > 0) pendingSeparator = true; + column += width; + continue; + } + appendSeparator(); + chunks.push(text); + spans.push({ + textStart: textLength, + textEnd: textLength + text.length, + row, + startCol: column, + endCol: column + width, + linearColumns: false, + }); + textLength += text.length; + column += width; + } + } + if (textLength > 0) pendingSeparator = true; + } + + return { text: chunks.join(""), spans }; +} + +function normalizeQuery(query: string): string { + return query.replace(/\s+/gu, " ").trim(); +} + +function escapeRegExp(text: string): string { + return text.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"); +} + +function findSearchCorpusMatches(corpus: SearchCorpus, normalizedQuery: string): AltScreenSearchMatch[] { + if (!normalizedQuery) return []; + const expression = new RegExp(escapeRegExp(normalizedQuery), "giu"); + const matches: AltScreenSearchMatch[] = []; + let spanIndex = 0; + + for (const match of corpus.text.matchAll(expression)) { + const start = match.index; + const end = start + match[0].length; + while (spanIndex < corpus.spans.length && corpus.spans[spanIndex]!.textEnd <= start) spanIndex += 1; + + const segments: AltScreenSearchSegment[] = []; + for (let index = spanIndex; index < corpus.spans.length; index++) { + const span = corpus.spans[index]!; + if (span.textStart >= end) break; + if (span.textEnd <= start) continue; + const startCol = span.linearColumns + ? span.startCol + Math.max(start, span.textStart) - span.textStart + : span.startCol; + const endCol = span.linearColumns ? span.startCol + Math.min(end, span.textEnd) - span.textStart : span.endCol; + const previous = segments[segments.length - 1]; + if (previous && previous.row === span.row && startCol <= previous.endCol) { + previous.endCol = Math.max(previous.endCol, endCol); + } else { + segments.push({ row: span.row, startCol, endCol }); + } + } + while (spanIndex < corpus.spans.length && corpus.spans[spanIndex]!.textEnd <= end) spanIndex += 1; + if (segments.length > 0) matches.push({ segments }); + } + + return matches; +} + +export interface AltScreenSearchResult { + matches: AltScreenSearchMatch[]; + changed: boolean; +} + +/** Cache the searchable corpus and matches while rendered transcript lines remain unchanged. */ +export class AltScreenSearchIndex { + private sourceLines: string[] | undefined; + private corpus: SearchCorpus | undefined; + private normalizedQuery: string | undefined; + private matches: AltScreenSearchMatch[] = []; + + search(lines: readonly string[], query: string): AltScreenSearchResult { + let sourceChanged = this.sourceLines?.length !== lines.length; + if (!sourceChanged && this.sourceLines) { + for (let index = 0; index < lines.length; index++) { + if (this.sourceLines[index] === lines[index]) continue; + sourceChanged = true; + break; + } + } + if (sourceChanged || !this.corpus) { + this.sourceLines = Array.from(lines); + this.corpus = buildSearchCorpus(lines); + } + + const normalizedQuery = normalizeQuery(query); + const changed = sourceChanged || normalizedQuery !== this.normalizedQuery; + if (changed) { + this.normalizedQuery = normalizedQuery; + this.matches = findSearchCorpusMatches(this.corpus, normalizedQuery); + } + return { matches: this.matches, changed }; + } +} + +export function findAltScreenSearchMatches(lines: readonly string[], query: string): AltScreenSearchMatch[] { + const normalizedQuery = normalizeQuery(query); + return normalizedQuery ? findSearchCorpusMatches(buildSearchCorpus(lines), normalizedQuery) : []; +} + +export function getAltScreenSearchMatchKey(match: AltScreenSearchMatch): string { + const first = match.segments[0]; + const last = match.segments[match.segments.length - 1]; + return first && last ? `${first.row}:${first.startCol}:${last.row}:${last.endCol}` : ""; +} + +export class AltScreenSearchComponent implements Component, Focusable { + private readonly input = new Input({ + prompt: " ", + placeholder: "Find in transcript", + placeholderStyle: (text) => `\x1b[2m${text}\x1b[22m`, + }); + private readonly onQueryChange: (query: string) => void; + private readonly navigationButtonStyle: (text: string, hovered: boolean) => string; + private resultCount = 0; + private resultIndex = -1; + private previousButtonStart = -1; + private previousButtonEnd = -1; + private nextButtonStart = -1; + private nextButtonEnd = -1; + private hoveredNavigationDirection: -1 | 1 | undefined; + private _focused = false; + + constructor( + onQueryChange: (query: string) => void, + navigationButtonStyle: (text: string, hovered: boolean) => string = (text) => text, + ) { + this.onQueryChange = onQueryChange; + this.navigationButtonStyle = navigationButtonStyle; + } + + get focused(): boolean { + return this._focused; + } + + set focused(value: boolean) { + this._focused = value; + this.input.focused = value; + } + + setResult(index: number, count: number): void { + this.resultIndex = index; + this.resultCount = count; + } + + getNavigationDirectionAt(row: number, column: number): -1 | 1 | undefined { + if (row !== 2) return undefined; + if (column >= this.previousButtonStart && column < this.previousButtonEnd) return -1; + if (column >= this.nextButtonStart && column < this.nextButtonEnd) return 1; + return undefined; + } + + setHoveredNavigationDirection(direction: -1 | 1 | undefined): boolean { + if (direction === this.hoveredNavigationDirection) return false; + this.hoveredNavigationDirection = direction; + return true; + } + + handleInput(data: string): void { + const previous = this.input.getValue(); + this.input.handleInput(data); + const query = this.input.getValue(); + if (query !== previous) this.onQueryChange(query); + } + + invalidate(): void { + this.input.invalidate(); + } + + render(width: number): string[] { + const safeWidth = Math.max(1, width); + const innerWidth = Math.max(0, safeWidth - 2); + const formatKey = (key: string | undefined): string => + key + ? key + .split("+") + .map((part) => { + if (process.platform === "darwin" && part.toLowerCase() === "alt") return "Option"; + return part.charAt(0).toUpperCase() + part.slice(1); + }) + .join("+") + : "Unbound"; + const keybindings = getKeybindings(); + const previousKey = formatKey(keybindings.getKeys("tui.altScreen.searchPrevious")[0]); + const nextKey = formatKey(keybindings.getKeys("tui.altScreen.searchNext")[0]); + const query = this.input.getValue(); + const result = !query + ? "" + : this.resultCount === 0 + ? "No matches" + : `${this.resultIndex + 1}/${this.resultCount}`; + const resultSpace = Math.max(0, innerWidth - 3); + const visibleResult = truncateToWidth(result, resultSpace, ""); + const resultText = visibleResult ? `\x1b[2m ${visibleResult} \x1b[22m` : ""; + const inputWidth = Math.max(0, innerWidth - visibleWidth(resultText)); + const inputLine = truncateToWidth(this.input.render(Math.max(1, inputWidth))[0] ?? "", inputWidth, ""); + const inputPadding = " ".repeat(Math.max(0, inputWidth - visibleWidth(inputLine))); + const content = `${inputLine}${inputPadding}${resultText}`; + + let previousButton = `↑ ${previousKey}`; + let nextButton = `↓ ${nextKey}`; + let separator = " · "; + const outerGapWidth = 1; + const availableControlsWidth = Math.max(0, innerWidth - outerGapWidth * 2 - 1); + let controlsWidth = visibleWidth(previousButton) + visibleWidth(separator) + visibleWidth(nextButton); + if (controlsWidth > availableControlsWidth) { + previousButton = "↑"; + nextButton = "↓"; + separator = " "; + controlsWidth = visibleWidth(previousButton) + visibleWidth(separator) + visibleWidth(nextButton); + } + const showButtons = controlsWidth <= availableControlsWidth; + const renderedButtons = showButtons + ? this.navigationButtonStyle(previousButton, this.hoveredNavigationDirection === -1) + + separator + + this.navigationButtonStyle(nextButton, this.hoveredNavigationDirection === 1) + : ""; + const outerGapsWidth = showButtons ? outerGapWidth * 2 : 0; + const rightRuleWidth = renderedButtons && innerWidth > controlsWidth + outerGapsWidth ? 1 : 0; + const leftRuleWidth = Math.max( + 0, + innerWidth - (showButtons ? controlsWidth : 0) - outerGapsWidth - rightRuleWidth, + ); + const previousStart = 1 + leftRuleWidth + outerGapWidth; + this.previousButtonStart = showButtons ? previousStart : -1; + this.previousButtonEnd = showButtons ? previousStart + visibleWidth(previousButton) : -1; + this.nextButtonStart = showButtons ? this.previousButtonEnd + visibleWidth(separator) : -1; + this.nextButtonEnd = showButtons ? this.nextButtonStart + visibleWidth(nextButton) : -1; + + if (safeWidth === 1) return ["┌", "│", "└"]; + return [ + `┌${"─".repeat(innerWidth)}┐`, + `│${content}│`, + `└${"─".repeat(leftRuleWidth)}${renderedButtons ? " " : ""}${renderedButtons}${renderedButtons ? " " : ""}${"─".repeat(rightRuleWidth)}┘`, + ]; + } +} diff --git a/packages/tui/src/autocomplete.ts b/packages/tui/src/autocomplete.ts new file mode 100644 index 0000000000000000000000000000000000000000..7a072d981fd679edc5250cae756845e13031ccf3 --- /dev/null +++ b/packages/tui/src/autocomplete.ts @@ -0,0 +1,826 @@ +import { spawn } from "child_process"; +import { readdirSync, statSync } from "fs"; +import { homedir } from "os"; +import { basename, dirname, join } from "path"; +import { fuzzyFilter } from "./fuzzy.ts"; + +const PATH_DELIMITERS = new Set([" ", "\t", '"', "'", "="]); + +function toDisplayPath(value: string): string { + return value.replace(/\\/g, "/"); +} + +function escapeRegex(value: string): string { + return value.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"); +} + +function buildFdPathQuery(query: string): string { + const normalized = toDisplayPath(query); + if (!normalized.includes("/")) { + return normalized; + } + + const hasTrailingSeparator = normalized.endsWith("/"); + const trimmed = normalized.replace(/^\/+|\/+$/g, ""); + if (!trimmed) { + return normalized; + } + + const separatorPattern = "[\\\\/]"; + const segments = trimmed + .split("/") + .filter(Boolean) + .map((segment) => escapeRegex(segment)); + if (segments.length === 0) { + return normalized; + } + + let pattern = segments.join(separatorPattern); + if (hasTrailingSeparator) { + pattern += separatorPattern; + } + return pattern; +} + +function findLastDelimiter(text: string): number { + for (let i = text.length - 1; i >= 0; i -= 1) { + if (PATH_DELIMITERS.has(text[i] ?? "")) { + return i; + } + } + return -1; +} + +function findUnclosedQuoteStart(text: string): number | null { + let inQuotes = false; + let quoteStart = -1; + + for (let i = 0; i < text.length; i += 1) { + if (text[i] === '"') { + inQuotes = !inQuotes; + if (inQuotes) { + quoteStart = i; + } + } + } + + return inQuotes ? quoteStart : null; +} + +function isTokenStart(text: string, index: number): boolean { + return index === 0 || PATH_DELIMITERS.has(text[index - 1] ?? ""); +} + +function extractQuotedPrefix(text: string): string | null { + const quoteStart = findUnclosedQuoteStart(text); + if (quoteStart === null) { + return null; + } + + if (quoteStart > 0 && text[quoteStart - 1] === "@") { + if (!isTokenStart(text, quoteStart - 1)) { + return null; + } + return text.slice(quoteStart - 1); + } + + if (!isTokenStart(text, quoteStart)) { + return null; + } + + return text.slice(quoteStart); +} + +function parsePathPrefix(prefix: string): { rawPrefix: string; isAtPrefix: boolean; isQuotedPrefix: boolean } { + if (prefix.startsWith('@"')) { + return { rawPrefix: prefix.slice(2), isAtPrefix: true, isQuotedPrefix: true }; + } + if (prefix.startsWith('"')) { + return { rawPrefix: prefix.slice(1), isAtPrefix: false, isQuotedPrefix: true }; + } + if (prefix.startsWith("@")) { + return { rawPrefix: prefix.slice(1), isAtPrefix: true, isQuotedPrefix: false }; + } + return { rawPrefix: prefix, isAtPrefix: false, isQuotedPrefix: false }; +} + +function buildCompletionValue( + path: string, + options: { isDirectory: boolean; isAtPrefix: boolean; isQuotedPrefix: boolean }, +): string { + const needsQuotes = options.isQuotedPrefix || path.includes(" "); + const prefix = options.isAtPrefix ? "@" : ""; + + if (!needsQuotes) { + return `${prefix}${path}`; + } + + const openQuote = `${prefix}"`; + const closeQuote = '"'; + return `${openQuote}${path}${closeQuote}`; +} + +// Use fd to walk directory tree (fast, respects .gitignore) +async function walkDirectoryWithFd( + baseDir: string, + fdPath: string, + query: string, + maxResults: number, + signal: AbortSignal, + maxDepth?: number, +): Promise> { + const args = [ + "--base-directory", + baseDir, + "--max-results", + String(maxResults), + "--type", + "f", + "--type", + "d", + "--follow", + "--hidden", + "--exclude", + ".git", + "--exclude", + ".git/*", + "--exclude", + ".git/**", + ]; + + if (maxDepth !== undefined) { + args.push("--max-depth", String(maxDepth)); + } + + if (toDisplayPath(query).includes("/")) { + args.push("--full-path"); + } + + if (query) { + args.push(buildFdPathQuery(query)); + } + + return await new Promise((resolve) => { + if (signal.aborted) { + resolve([]); + return; + } + + const child = spawn(fdPath, args, { + stdio: ["ignore", "pipe", "pipe"], + }); + let stdout = ""; + let resolved = false; + + const finish = (results: Array<{ path: string; isDirectory: boolean }>) => { + if (resolved) return; + resolved = true; + signal.removeEventListener("abort", onAbort); + resolve(results); + }; + + const onAbort = () => { + if (child.exitCode === null) { + child.kill("SIGKILL"); + } + }; + + signal.addEventListener("abort", onAbort, { once: true }); + child.stdout.setEncoding("utf-8"); + child.stdout.on("data", (chunk: string) => { + stdout += chunk; + }); + child.on("error", () => { + finish([]); + }); + child.on("close", (code) => { + if (signal.aborted || code !== 0 || !stdout) { + finish([]); + return; + } + + const lines = stdout.trim().split("\n").filter(Boolean); + const results: Array<{ path: string; isDirectory: boolean }> = []; + + for (const line of lines) { + const displayLine = toDisplayPath(line); + const hasTrailingSeparator = displayLine.endsWith("/"); + const normalizedPath = hasTrailingSeparator ? displayLine.slice(0, -1) : displayLine; + if (normalizedPath === ".git" || normalizedPath.startsWith(".git/") || normalizedPath.includes("/.git/")) { + continue; + } + + results.push({ + path: displayLine, + isDirectory: hasTrailingSeparator, + }); + } + + finish(results); + }); + }); +} + +export interface AutocompleteItem { + value: string; + label: string; + description?: string; +} + +type Awaitable = T | Promise; + +export interface SlashCommand { + name: string; + description?: string; + argumentHint?: string; + // Function to get argument completions for this command + // Returns null if no argument completion is available + getArgumentCompletions?(argumentPrefix: string): Awaitable; +} + +export interface AutocompleteSuggestions { + items: AutocompleteItem[]; + prefix: string; // What we're matching against (e.g., "/" or "src/") +} + +export interface AutocompleteProvider { + /** Characters that should naturally trigger this provider at token boundaries. */ + triggerCharacters?: string[]; + + // Get autocomplete suggestions for current text/cursor position + // Returns null if no suggestions available + getSuggestions( + lines: string[], + cursorLine: number, + cursorCol: number, + options: { signal: AbortSignal; force?: boolean }, + ): Promise; + + // Apply the selected item + // Returns the new text and cursor position + applyCompletion( + lines: string[], + cursorLine: number, + cursorCol: number, + item: AutocompleteItem, + prefix: string, + ): { + lines: string[]; + cursorLine: number; + cursorCol: number; + }; + + // Check if file completion should trigger for explicit Tab completion + shouldTriggerFileCompletion?(lines: string[], cursorLine: number, cursorCol: number): boolean; +} + +// Combined provider that handles both slash commands and file paths +export class CombinedAutocompleteProvider implements AutocompleteProvider { + private commands: (SlashCommand | AutocompleteItem)[]; + private basePath: string; + private fdPath: string | null; + + constructor(commands: (SlashCommand | AutocompleteItem)[] = [], basePath: string, fdPath: string | null = null) { + this.commands = commands; + this.basePath = basePath; + this.fdPath = fdPath; + } + + async getSuggestions( + lines: string[], + cursorLine: number, + cursorCol: number, + options: { signal: AbortSignal; force?: boolean }, + ): Promise { + const currentLine = lines[cursorLine] || ""; + const textBeforeCursor = currentLine.slice(0, cursorCol); + + const atPrefix = this.extractAtPrefix(textBeforeCursor); + if (atPrefix) { + const { rawPrefix, isQuotedPrefix } = parsePathPrefix(atPrefix); + const suggestions = await this.getFuzzyFileSuggestions(rawPrefix, { + isQuotedPrefix, + signal: options.signal, + }); + if (suggestions.length === 0) return null; + + return { + items: suggestions, + prefix: atPrefix, + }; + } + + if (!options.force && textBeforeCursor.startsWith("/")) { + const spaceIndex = textBeforeCursor.indexOf(" "); + + if (spaceIndex === -1) { + const prefix = textBeforeCursor.slice(1); + const commandItems = this.commands.map((cmd) => { + const name = "name" in cmd ? cmd.name : cmd.value; + const hint = "argumentHint" in cmd && cmd.argumentHint ? cmd.argumentHint : undefined; + const desc = cmd.description ?? ""; + const fullDesc = hint ? (desc ? `${hint} — ${desc}` : hint) : desc; + return { + name, + label: name, + description: fullDesc || undefined, + }; + }); + + const filtered = fuzzyFilter(commandItems, prefix, (item) => item.name).map((item) => ({ + value: item.name, + label: item.label, + ...(item.description && { description: item.description }), + })); + + if (filtered.length === 0) return null; + + return { + items: filtered, + prefix: textBeforeCursor, + }; + } + + const commandName = textBeforeCursor.slice(1, spaceIndex); + const argumentText = textBeforeCursor.slice(spaceIndex + 1); + + const command = this.commands.find((cmd) => { + const name = "name" in cmd ? cmd.name : cmd.value; + return name === commandName; + }); + if (!command || !("getArgumentCompletions" in command) || !command.getArgumentCompletions) { + return null; + } + + const argumentSuggestions = await command.getArgumentCompletions(argumentText); + if (!Array.isArray(argumentSuggestions) || argumentSuggestions.length === 0) { + return null; + } + + return { + items: argumentSuggestions, + prefix: argumentText, + }; + } + + const pathMatch = this.extractPathPrefix(textBeforeCursor, options.force ?? false); + if (pathMatch === null) { + return null; + } + + const suggestions = this.getFileSuggestions(pathMatch); + if (suggestions.length === 0) return null; + + return { + items: suggestions, + prefix: pathMatch, + }; + } + + applyCompletion( + lines: string[], + cursorLine: number, + cursorCol: number, + item: AutocompleteItem, + prefix: string, + ): { lines: string[]; cursorLine: number; cursorCol: number } { + const currentLine = lines[cursorLine] || ""; + const beforePrefix = currentLine.slice(0, cursorCol - prefix.length); + const afterCursor = currentLine.slice(cursorCol); + const isQuotedPrefix = prefix.startsWith('"') || prefix.startsWith('@"'); + const hasLeadingQuoteAfterCursor = afterCursor.startsWith('"'); + const hasTrailingQuoteInItem = item.value.endsWith('"'); + const adjustedAfterCursor = + isQuotedPrefix && hasTrailingQuoteInItem && hasLeadingQuoteAfterCursor ? afterCursor.slice(1) : afterCursor; + + // Check if we're completing a slash command (prefix starts with "/" but NOT a file path) + // Slash commands are at the start of the line and don't contain path separators after the first / + const isSlashCommand = prefix.startsWith("/") && beforePrefix.trim() === "" && !prefix.slice(1).includes("/"); + if (isSlashCommand) { + // This is a command name completion + const newLine = `${beforePrefix}/${item.value} ${adjustedAfterCursor}`; + const newLines = [...lines]; + newLines[cursorLine] = newLine; + + return { + lines: newLines, + cursorLine, + cursorCol: beforePrefix.length + item.value.length + 2, // +2 for "/" and space + }; + } + + // Check if we're completing a file attachment (prefix starts with "@") + if (prefix.startsWith("@")) { + // This is a file attachment completion + // Don't add space after directories so user can continue autocompleting + const isDirectory = item.label.endsWith("/"); + const suffix = isDirectory ? "" : " "; + const newLine = `${beforePrefix + item.value}${suffix}${adjustedAfterCursor}`; + const newLines = [...lines]; + newLines[cursorLine] = newLine; + + const hasTrailingQuote = item.value.endsWith('"'); + const cursorOffset = isDirectory && hasTrailingQuote ? item.value.length - 1 : item.value.length; + + return { + lines: newLines, + cursorLine, + cursorCol: beforePrefix.length + cursorOffset + suffix.length, + }; + } + + // Check if we're in a slash command context (beforePrefix contains "/command ") + const textBeforeCursor = currentLine.slice(0, cursorCol); + if (textBeforeCursor.includes("/") && textBeforeCursor.includes(" ")) { + // This is likely a command argument completion + const newLine = beforePrefix + item.value + adjustedAfterCursor; + const newLines = [...lines]; + newLines[cursorLine] = newLine; + + const isDirectory = item.label.endsWith("/"); + const hasTrailingQuote = item.value.endsWith('"'); + const cursorOffset = isDirectory && hasTrailingQuote ? item.value.length - 1 : item.value.length; + + return { + lines: newLines, + cursorLine, + cursorCol: beforePrefix.length + cursorOffset, + }; + } + + // For file paths, complete the path + const newLine = beforePrefix + item.value + adjustedAfterCursor; + const newLines = [...lines]; + newLines[cursorLine] = newLine; + + const isDirectory = item.label.endsWith("/"); + const hasTrailingQuote = item.value.endsWith('"'); + const cursorOffset = isDirectory && hasTrailingQuote ? item.value.length - 1 : item.value.length; + + return { + lines: newLines, + cursorLine, + cursorCol: beforePrefix.length + cursorOffset, + }; + } + + // Extract @ prefix for fuzzy file suggestions + private extractAtPrefix(text: string): string | null { + const quotedPrefix = extractQuotedPrefix(text); + if (quotedPrefix?.startsWith('@"')) { + return quotedPrefix; + } + + const lastDelimiterIndex = findLastDelimiter(text); + const tokenStart = lastDelimiterIndex === -1 ? 0 : lastDelimiterIndex + 1; + + if (text[tokenStart] === "@") { + return text.slice(tokenStart); + } + + return null; + } + + // Extract a path-like prefix from the text before cursor + private extractPathPrefix(text: string, forceExtract: boolean = false): string | null { + const quotedPrefix = extractQuotedPrefix(text); + if (quotedPrefix) { + return quotedPrefix; + } + + const lastDelimiterIndex = findLastDelimiter(text); + const pathPrefix = lastDelimiterIndex === -1 ? text : text.slice(lastDelimiterIndex + 1); + + // For forced extraction (Tab key), always return something + if (forceExtract) { + return pathPrefix; + } + + // For natural triggers, return if it looks like a path, ends with /, starts with ~/, . + // Only return empty string if the text looks like it's starting a path context + if (pathPrefix.includes("/") || pathPrefix.startsWith(".") || pathPrefix.startsWith("~/")) { + return pathPrefix; + } + + // Return empty string only after a space (not for completely empty text) + // Empty text should not trigger file suggestions - that's for forced Tab completion + if (pathPrefix === "" && text.endsWith(" ")) { + return pathPrefix; + } + + return null; + } + + // Expand home directory (~/) to actual home path + private expandHomePath(path: string): string { + if (path.startsWith("~/")) { + const expandedPath = join(homedir(), path.slice(2)); + // Preserve trailing slash if original path had one + return path.endsWith("/") && !expandedPath.endsWith("/") ? `${expandedPath}/` : expandedPath; + } else if (path === "~") { + return homedir(); + } + return path; + } + + private resolveScopedFuzzyQuery(rawQuery: string): { baseDir: string; query: string; displayBase: string } | null { + const normalizedQuery = toDisplayPath(rawQuery); + const slashIndex = normalizedQuery.lastIndexOf("/"); + if (slashIndex === -1) { + return null; + } + + const displayBase = normalizedQuery.slice(0, slashIndex + 1); + const query = normalizedQuery.slice(slashIndex + 1); + + let baseDir: string; + if (displayBase.startsWith("~/")) { + baseDir = this.expandHomePath(displayBase); + } else if (displayBase.startsWith("/")) { + baseDir = displayBase; + } else { + baseDir = join(this.basePath, displayBase); + } + + try { + if (!statSync(baseDir).isDirectory()) { + return null; + } + } catch { + return null; + } + + return { baseDir, query, displayBase }; + } + + private scopedPathForDisplay(displayBase: string, relativePath: string): string { + const normalizedRelativePath = toDisplayPath(relativePath); + if (displayBase === "/") { + return `/${normalizedRelativePath}`; + } + return `${toDisplayPath(displayBase)}${normalizedRelativePath}`; + } + + // Get file/directory suggestions for a given path prefix + private getFileSuggestions(prefix: string): AutocompleteItem[] { + try { + let searchDir: string; + let searchPrefix: string; + const { rawPrefix, isAtPrefix, isQuotedPrefix } = parsePathPrefix(prefix); + let expandedPrefix = rawPrefix; + + // Handle home directory expansion + if (expandedPrefix.startsWith("~")) { + expandedPrefix = this.expandHomePath(expandedPrefix); + } + + const isRootPrefix = + rawPrefix === "" || + rawPrefix === "./" || + rawPrefix === "../" || + rawPrefix === "~" || + rawPrefix === "~/" || + rawPrefix === "/" || + (isAtPrefix && rawPrefix === ""); + + if (isRootPrefix) { + // Complete from specified position + if (rawPrefix.startsWith("~") || expandedPrefix.startsWith("/")) { + searchDir = expandedPrefix; + } else { + searchDir = join(this.basePath, expandedPrefix); + } + searchPrefix = ""; + } else if (rawPrefix.endsWith("/")) { + // If prefix ends with /, show contents of that directory + if (rawPrefix.startsWith("~") || expandedPrefix.startsWith("/")) { + searchDir = expandedPrefix; + } else { + searchDir = join(this.basePath, expandedPrefix); + } + searchPrefix = ""; + } else { + // Split into directory and file prefix + const dir = dirname(expandedPrefix); + const file = basename(expandedPrefix); + if (rawPrefix.startsWith("~") || expandedPrefix.startsWith("/")) { + searchDir = dir; + } else { + searchDir = join(this.basePath, dir); + } + searchPrefix = file; + } + + const entries = readdirSync(searchDir, { withFileTypes: true }); + const suggestions: AutocompleteItem[] = []; + + for (const entry of entries) { + if (!entry.name.toLowerCase().startsWith(searchPrefix.toLowerCase())) { + continue; + } + + // Check if entry is a directory (or a symlink pointing to a directory) + let isDirectory = entry.isDirectory(); + if (!isDirectory && entry.isSymbolicLink()) { + try { + const fullPath = join(searchDir, entry.name); + isDirectory = statSync(fullPath).isDirectory(); + } catch { + // Broken symlink or permission error - treat as file + } + } + + let relativePath: string; + const name = entry.name; + const displayPrefix = rawPrefix; + + if (displayPrefix.endsWith("/")) { + // If prefix ends with /, append entry to the prefix + relativePath = displayPrefix + name; + } else if (displayPrefix.includes("/") || displayPrefix.includes("\\")) { + // Preserve ~/ format for home directory paths + if (displayPrefix.startsWith("~/")) { + const homeRelativeDir = displayPrefix.slice(2); // Remove ~/ + const dir = dirname(homeRelativeDir); + relativePath = `~/${dir === "." ? name : join(dir, name)}`; + } else if (displayPrefix.startsWith("/")) { + // Absolute path - construct properly + const dir = dirname(displayPrefix); + if (dir === "/") { + relativePath = `/${name}`; + } else { + relativePath = `${dir}/${name}`; + } + } else { + relativePath = join(dirname(displayPrefix), name); + // path.join normalizes away ./ prefix, preserve it + if (displayPrefix.startsWith("./") && !relativePath.startsWith("./")) { + relativePath = `./${relativePath}`; + } + } + } else { + // For standalone entries, preserve ~/ if original prefix was ~/ + if (displayPrefix.startsWith("~")) { + relativePath = `~/${name}`; + } else { + relativePath = name; + } + } + + relativePath = toDisplayPath(relativePath); + const pathValue = isDirectory ? `${relativePath}/` : relativePath; + const value = buildCompletionValue(pathValue, { + isDirectory, + isAtPrefix, + isQuotedPrefix, + }); + + suggestions.push({ + value, + label: name + (isDirectory ? "/" : ""), + }); + } + + // Sort directories first, then alphabetically + suggestions.sort((a, b) => { + const aIsDir = a.value.endsWith("/"); + const bIsDir = b.value.endsWith("/"); + if (aIsDir && !bIsDir) return -1; + if (!aIsDir && bIsDir) return 1; + return a.label.localeCompare(b.label); + }); + + return suggestions; + } catch (_e) { + // Directory doesn't exist or not accessible + return []; + } + } + + // Score an entry against the query (higher = better match) + // isDirectory adds bonus to prioritize folders + private scoreEntry(filePath: string, query: string, isDirectory: boolean): number { + const fileName = basename(filePath); + const lowerFileName = fileName.toLowerCase(); + const lowerQuery = query.toLowerCase(); + + let score = 0; + + // Exact filename match (highest) + if (lowerFileName === lowerQuery) score = 100; + // Filename starts with query + else if (lowerFileName.startsWith(lowerQuery)) score = 80; + // Substring match in filename + else if (lowerFileName.includes(lowerQuery)) score = 50; + // Substring match in full path + else if (filePath.toLowerCase().includes(lowerQuery)) score = 30; + + // Directories get a bonus to appear first + if (isDirectory && score > 0) score += 10; + + return score; + } + + private async getBaseDirSuggestions( + baseDir: string, + query: string, + signal: AbortSignal, + ): Promise> { + if (!this.fdPath || signal.aborted) { + return []; + } + + return await walkDirectoryWithFd(baseDir, this.fdPath, query, 100, signal, 1); + } + + // Fuzzy file search using fd (fast, respects .gitignore) + private async getFuzzyFileSuggestions( + query: string, + options: { isQuotedPrefix: boolean; signal: AbortSignal }, + ): Promise { + if (!this.fdPath || options.signal.aborted) { + return []; + } + + try { + const scopedQuery = this.resolveScopedFuzzyQuery(query); + const fdBaseDir = scopedQuery?.baseDir ?? this.basePath; + const fdQuery = scopedQuery?.query ?? query; + const baseDirEntries = await this.getBaseDirSuggestions(fdBaseDir, fdQuery, options.signal); + const recursiveEntries = await walkDirectoryWithFd(fdBaseDir, this.fdPath, fdQuery, 100, options.signal); + const seenPaths = new Set(baseDirEntries.map((entry) => entry.path)); + const entries = [ + ...baseDirEntries, + ...recursiveEntries.filter((entry) => { + if (seenPaths.has(entry.path)) return false; + seenPaths.add(entry.path); + return true; + }), + ]; + if (options.signal.aborted) { + return []; + } + + const scoredEntries = entries + .map((entry) => ({ + ...entry, + score: fdQuery ? this.scoreEntry(entry.path, fdQuery, entry.isDirectory) : 1, + })) + .filter((entry) => entry.score > 0); + + scoredEntries.sort((a, b) => { + const scoreDiff = b.score - a.score; + if (scoreDiff !== 0) return scoreDiff; + + const aDepth = toDisplayPath(a.path).split("/").filter(Boolean).length; + const bDepth = toDisplayPath(b.path).split("/").filter(Boolean).length; + const depthDiff = aDepth - bDepth; + if (depthDiff !== 0) return depthDiff; + + const lengthDiff = a.path.length - b.path.length; + if (lengthDiff !== 0) return lengthDiff; + + return a.path.localeCompare(b.path); + }); + const topEntries = scoredEntries.slice(0, 20); + + const suggestions: AutocompleteItem[] = []; + for (const { path: entryPath, isDirectory } of topEntries) { + const pathWithoutSlash = isDirectory ? entryPath.slice(0, -1) : entryPath; + const displayPath = scopedQuery + ? this.scopedPathForDisplay(scopedQuery.displayBase, pathWithoutSlash) + : pathWithoutSlash; + const entryName = basename(pathWithoutSlash); + const completionPath = isDirectory ? `${displayPath}/` : displayPath; + const value = buildCompletionValue(completionPath, { + isDirectory, + isAtPrefix: true, + isQuotedPrefix: options.isQuotedPrefix, + }); + + suggestions.push({ + value, + label: entryName + (isDirectory ? "/" : ""), + description: displayPath, + }); + } + + return suggestions; + } catch { + return []; + } + } + + // Check if we should trigger file completion (called on Tab key) + shouldTriggerFileCompletion(lines: string[], cursorLine: number, cursorCol: number): boolean { + const currentLine = lines[cursorLine] || ""; + const textBeforeCursor = currentLine.slice(0, cursorCol); + + // Don't trigger if we're typing a slash command at the start of the line + if (textBeforeCursor.trim().startsWith("/") && !textBeforeCursor.trim().includes(" ")) { + return false; + } + + return true; + } +} diff --git a/packages/tui/src/components/alt-screen-flash.ts b/packages/tui/src/components/alt-screen-flash.ts new file mode 100644 index 0000000000000000000000000000000000000000..e4b823cd2bdd54696895cac93807cc6fcddaddd4 --- /dev/null +++ b/packages/tui/src/components/alt-screen-flash.ts @@ -0,0 +1,51 @@ +import type { Component } from "../tui.ts"; +import { truncateToWidth } from "../utils.ts"; + +const DEFAULT_DURATION_MS = 1000; + +interface FlashEntry { + id: number; + message: string; + timer: NodeJS.Timeout; +} + +/** Transient messages composited by the alternate-screen renderer. */ +export class AltScreenFlashContainer implements Component { + private readonly entries: FlashEntry[] = []; + private nextId = 0; + private readonly requestRender: () => void; + + constructor(requestRender: () => void) { + this.requestRender = requestRender; + } + + flash(message: string, durationMs = DEFAULT_DURATION_MS): void { + const id = this.nextId++; + const timer = setTimeout( + () => { + const index = this.entries.findIndex((entry) => entry.id === id); + if (index === -1) return; + this.entries.splice(index, 1); + this.requestRender(); + }, + Math.max(0, durationMs), + ); + timer.unref(); + this.entries.push({ id, message, timer }); + this.requestRender(); + } + + dispose(): void { + for (const entry of this.entries) clearTimeout(entry.timer); + this.entries.length = 0; + } + + invalidate(): void {} + + render(width: number): string[] { + return this.entries.map((entry) => { + const message = truncateToWidth(` ${entry.message} `, width, ""); + return `\x1b[7m${message}\x1b[27m`; + }); + } +} diff --git a/packages/tui/src/components/box.ts b/packages/tui/src/components/box.ts new file mode 100644 index 0000000000000000000000000000000000000000..c66f70f6718c8cb26fb6a8cc306e50b4effe9959 --- /dev/null +++ b/packages/tui/src/components/box.ts @@ -0,0 +1,167 @@ +import { type Component, dispatchMouseEvent, type TuiMouseDispatchResult, type TuiMouseEvent } from "../tui.ts"; +import { applyBackgroundToLine, visibleWidth } from "../utils.ts"; + +type RenderCache = { + childLines: string[]; + width: number; + bgSample: string | undefined; + lines: string[]; +}; + +/** + * Box component - a container that applies padding and background to all children + */ +export class Box implements Component { + children: Component[] = []; + private paddingX: number; + private paddingY: number; + private bgFn?: (text: string) => string; + + // Cache for rendered output + private cache?: RenderCache; + private mouseLayout?: { width: number; children: Array<{ component: Component; height: number }> }; + + constructor(paddingX = 1, paddingY = 1, bgFn?: (text: string) => string) { + this.paddingX = paddingX; + this.paddingY = paddingY; + this.bgFn = bgFn; + } + + addChild(component: Component): void { + this.children.push(component); + this.invalidateCache(); + } + + removeChild(component: Component): void { + const index = this.children.indexOf(component); + if (index !== -1) { + this.children.splice(index, 1); + this.invalidateCache(); + } + } + + clear(): void { + this.children = []; + this.invalidateCache(); + } + + setBgFn(bgFn?: (text: string) => string): void { + this.bgFn = bgFn; + // Don't invalidate here - we'll detect bgFn changes by sampling output + } + + private invalidateCache(): void { + this.cache = undefined; + } + + private matchCache(width: number, childLines: string[], bgSample: string | undefined): boolean { + const cache = this.cache; + return ( + !!cache && + cache.width === width && + cache.bgSample === bgSample && + cache.childLines.length === childLines.length && + cache.childLines.every((line, i) => line === childLines[i]) + ); + } + + invalidate(): void { + this.invalidateCache(); + for (const child of this.children) { + child.invalidate?.(); + } + } + + handleMouse(event: TuiMouseEvent): TuiMouseDispatchResult | undefined { + const contentWidth = Math.max(1, event.width - this.paddingX * 2); + const contentY = event.y - this.paddingY; + const contentX = event.x - this.paddingX; + if (contentY < 0 || contentX < 0 || contentX >= contentWidth) return undefined; + + const mouseChildren = + this.mouseLayout?.width === contentWidth + ? this.mouseLayout.children + : this.children.map((component) => ({ component, height: component.render(contentWidth).length })); + let childY = 0; + for (const { component: child, height: childHeight } of mouseChildren) { + if (contentY >= childY && contentY < childY + childHeight) { + return dispatchMouseEvent(child, { + ...event, + x: contentX, + y: contentY - childY, + width: contentWidth, + height: childHeight, + }); + } + childY += childHeight; + } + return undefined; + } + + render(width: number): string[] { + if (this.children.length === 0) { + return []; + } + + const contentWidth = Math.max(1, width - this.paddingX * 2); + const leftPad = " ".repeat(this.paddingX); + + // Render all children + const childLines: string[] = []; + const mouseChildren: Array<{ component: Component; height: number }> = []; + for (const child of this.children) { + const lines = child.render(contentWidth); + mouseChildren.push({ component: child, height: lines.length }); + for (const line of lines) { + childLines.push(leftPad + line); + } + } + this.mouseLayout = { width: contentWidth, children: mouseChildren }; + + if (childLines.length === 0) { + return []; + } + + // Check if bgFn output changed by sampling + const bgSample = this.bgFn ? this.bgFn("test") : undefined; + + // Check cache validity + if (this.matchCache(width, childLines, bgSample)) { + return this.cache!.lines; + } + + // Apply background and padding + const result: string[] = []; + + // Top padding + for (let i = 0; i < this.paddingY; i++) { + result.push(this.applyBg("", width)); + } + + // Content + for (const line of childLines) { + result.push(this.applyBg(line, width)); + } + + // Bottom padding + for (let i = 0; i < this.paddingY; i++) { + result.push(this.applyBg("", width)); + } + + // Update cache + this.cache = { childLines, width, bgSample, lines: result }; + + return result; + } + + private applyBg(line: string, width: number): string { + const visLen = visibleWidth(line); + const padNeeded = Math.max(0, width - visLen); + const padded = line + " ".repeat(padNeeded); + + if (this.bgFn) { + return applyBackgroundToLine(padded, width, this.bgFn); + } + return padded; + } +} diff --git a/packages/tui/src/components/cancellable-loader.ts b/packages/tui/src/components/cancellable-loader.ts new file mode 100644 index 0000000000000000000000000000000000000000..7822cb04219440703a9100e02679725b3326fa1c --- /dev/null +++ b/packages/tui/src/components/cancellable-loader.ts @@ -0,0 +1,40 @@ +import { getKeybindings } from "../keybindings.ts"; +import { Loader } from "./loader.ts"; + +/** + * Loader that can be cancelled with Escape. + * Extends Loader with an AbortSignal for cancelling async operations. + * + * @example + * const loader = new CancellableLoader(tui, cyan, dim, "Working..."); + * loader.onAbort = () => done(null); + * doWork(loader.signal).then(done); + */ +export class CancellableLoader extends Loader { + private abortController = new AbortController(); + + /** Called when user presses Escape */ + onAbort?: () => void; + + /** AbortSignal that is aborted when user presses Escape */ + get signal(): AbortSignal { + return this.abortController.signal; + } + + /** Whether the loader was aborted */ + get aborted(): boolean { + return this.abortController.signal.aborted; + } + + handleInput(data: string): void { + const kb = getKeybindings(); + if (kb.matches(data, "tui.select.cancel")) { + this.abortController.abort(); + this.onAbort?.(); + } + } + + dispose(): void { + this.stop(); + } +} diff --git a/packages/tui/src/components/editor.ts b/packages/tui/src/components/editor.ts new file mode 100644 index 0000000000000000000000000000000000000000..42b722f8c8840583166f72af736f086dae3977a9 --- /dev/null +++ b/packages/tui/src/components/editor.ts @@ -0,0 +1,2461 @@ +import type { AutocompleteProvider, AutocompleteSuggestions } from "../autocomplete.ts"; +import { getKeybindings } from "../keybindings.ts"; +import { decodePrintableKey, matchesKey } from "../keys.ts"; +import { KillRing } from "../kill-ring.ts"; +import { + type Component, + CURSOR_MARKER, + type Focusable, + type TUI, + type TuiMouseEvent, + type TuiMouseEventResult, +} from "../tui.ts"; +import { UndoStack } from "../undo-stack.ts"; +import { + cjkBreakRegex, + getGraphemeSegmenter, + getWordSegmenter, + isWhitespaceChar, + sliceByColumn, + visibleWidth, +} from "../utils.ts"; +import { findWordBackward, findWordForward } from "../word-navigation.ts"; +import { SelectList, type SelectListLayoutOptions, type SelectListTheme } from "./select-list.ts"; + +const graphemeSegmenter = getGraphemeSegmenter(); +const wordSegmenter = getWordSegmenter(); + +/** Regex matching paste markers like `[paste #1 +123 lines]` or `[paste #2 1234 chars]`. */ +const PASTE_MARKER_REGEX = /\[paste #(\d+)( (\+\d+ lines|\d+ chars))?\]/g; + +/** Non-global version for single-segment testing. */ +const PASTE_MARKER_SINGLE = /^\[paste #(\d+)( (\+\d+ lines|\d+ chars))?\]$/; + +/** Check if a segment is a paste marker (i.e. was merged by segmentWithMarkers). */ +function isPasteMarker(segment: string): boolean { + return segment.length >= 10 && PASTE_MARKER_SINGLE.test(segment); +} + +/** + * A segmenter that wraps Intl.Segmenter and merges graphemes that fall + * within paste markers into single atomic segments. This makes cursor + * movement, deletion, word-wrap, etc. treat paste markers as single units. + * + * Only markers whose numeric ID exists in `validIds` are merged. + */ +function segmentWithMarkers( + text: string, + baseSegmenter: Intl.Segmenter, + validIds: Set, +): Iterable { + // Fast path: no paste markers in the text or no valid IDs. + if (validIds.size === 0 || !text.includes("[paste #")) { + return baseSegmenter.segment(text); + } + + // Find all marker spans with valid IDs. + const markers: Array<{ start: number; end: number }> = []; + for (const m of text.matchAll(PASTE_MARKER_REGEX)) { + const id = Number.parseInt(m[1]!, 10); + if (!validIds.has(id)) continue; + markers.push({ start: m.index, end: m.index + m[0].length }); + } + if (markers.length === 0) { + return baseSegmenter.segment(text); + } + + // Build merged segment list. + const baseSegments = baseSegmenter.segment(text); + const result: Intl.SegmentData[] = []; + let markerIdx = 0; + + for (const seg of baseSegments) { + // Skip past markers that are entirely before this segment. + while (markerIdx < markers.length && markers[markerIdx]!.end <= seg.index) { + markerIdx++; + } + + const marker = markerIdx < markers.length ? markers[markerIdx]! : null; + + if (marker && seg.index >= marker.start && seg.index < marker.end) { + // This segment falls inside a marker. + // If this is the first segment of the marker, emit a merged segment. + if (seg.index === marker.start) { + const markerText = text.slice(marker.start, marker.end); + result.push({ + segment: markerText, + index: marker.start, + input: text, + }); + } + // Otherwise skip (already merged into the first segment). + } else { + result.push(seg); + } + } + + return result; +} + +/** + * Represents a chunk of text for word-wrap layout. + * Tracks both the text content and its position in the original line. + */ +export interface TextChunk { + text: string; + startIndex: number; + endIndex: number; +} + +/** + * Split a line into word-wrapped chunks. + * Wraps at word boundaries when possible, falling back to character-level + * wrapping for words longer than the available width. + * + * @param line - The text line to wrap + * @param maxWidth - Maximum visible width per chunk + * @param preSegmented - Optional pre-segmented graphemes (e.g. with paste-marker awareness). + * When omitted the default Intl.Segmenter is used. + * @returns Array of chunks with text and position information + */ +export function wordWrapLine(line: string, maxWidth: number, preSegmented?: Intl.SegmentData[]): TextChunk[] { + if (!line || maxWidth <= 0) { + return [{ text: "", startIndex: 0, endIndex: 0 }]; + } + + const lineWidth = visibleWidth(line); + if (lineWidth <= maxWidth) { + return [{ text: line, startIndex: 0, endIndex: line.length }]; + } + + const chunks: TextChunk[] = []; + const segments = preSegmented ?? [...graphemeSegmenter.segment(line)]; + + let currentWidth = 0; + let chunkStart = 0; + + // Wrap opportunity: the position after the last whitespace before a non-whitespace + // grapheme, i.e. where a line break is allowed. + let wrapOppIndex = -1; + let wrapOppWidth = 0; + + for (let i = 0; i < segments.length; i++) { + const seg = segments[i]!; + const grapheme = seg.segment; + const gWidth = visibleWidth(grapheme); + const charIndex = seg.index; + const isWs = !isPasteMarker(grapheme) && isWhitespaceChar(grapheme); + + // Overflow check before advancing. + if (currentWidth + gWidth > maxWidth) { + if (wrapOppIndex >= 0 && currentWidth - wrapOppWidth + gWidth <= maxWidth) { + // Backtrack to last wrap opportunity (the remaining content + // plus the current grapheme still fits within maxWidth). + chunks.push({ text: line.slice(chunkStart, wrapOppIndex), startIndex: chunkStart, endIndex: wrapOppIndex }); + chunkStart = wrapOppIndex; + currentWidth -= wrapOppWidth; + } else if (chunkStart < charIndex) { + // No viable wrap opportunity: force-break at current position. + // This also handles the case where backtracking to a word + // boundary wouldn't help because the remaining content plus + // the current grapheme (e.g. a wide character) still exceeds + // maxWidth. + chunks.push({ text: line.slice(chunkStart, charIndex), startIndex: chunkStart, endIndex: charIndex }); + chunkStart = charIndex; + currentWidth = 0; + } + wrapOppIndex = -1; + } + + if (gWidth > maxWidth) { + // Single atomic segment wider than maxWidth (e.g. paste marker + // in a narrow terminal). Re-wrap it at grapheme granularity. + + // The segment remains logically atomic for cursor + // movement / editing — the split is purely visual for word-wrap layout. + const subChunks = wordWrapLine(grapheme, maxWidth); + for (let j = 0; j < subChunks.length - 1; j++) { + const sc = subChunks[j]!; + chunks.push({ text: sc.text, startIndex: charIndex + sc.startIndex, endIndex: charIndex + sc.endIndex }); + } + const last = subChunks[subChunks.length - 1]!; + chunkStart = charIndex + last.startIndex; + currentWidth = visibleWidth(last.text); + wrapOppIndex = -1; + continue; + } + + // Advance. + currentWidth += gWidth; + + // Record wrap opportunity: whitespace followed by non-whitespace + // (multiple spaces join; the break point is after the last space), + // or at a boundary where either side is CJK (CJK allows breaking + // between any adjacent characters). + const next = segments[i + 1]; + if (isWs && next && (isPasteMarker(next.segment) || !isWhitespaceChar(next.segment))) { + wrapOppIndex = next.index; + wrapOppWidth = currentWidth; + } else if (!isWs && next && !isWhitespaceChar(next.segment)) { + const isCjk = !isPasteMarker(grapheme) && cjkBreakRegex.test(grapheme); + const nextIsCjk = !isPasteMarker(next.segment) && cjkBreakRegex.test(next.segment); + if (isCjk || nextIsCjk) { + wrapOppIndex = next.index; + wrapOppWidth = currentWidth; + } + } + } + + // Push final chunk. + chunks.push({ text: line.slice(chunkStart), startIndex: chunkStart, endIndex: line.length }); + + return chunks; +} + +// Kitty CSI-u sequences for printable keys, including optional shifted/base codepoints. +interface EditorState { + lines: string[]; + cursorLine: number; + cursorCol: number; +} + +/** Undo snapshot: editor text state plus the paste registry. */ +interface EditorSnapshot { + state: EditorState; + pastes: Map; + pasteCounter: number; +} + +interface LayoutLine { + text: string; + hasCursor: boolean; + cursorPos?: number; +} + +export interface EditorTheme { + borderColor: (str: string) => string; + selectList: SelectListTheme; +} + +export interface EditorOptions { + paddingX?: number; + autocompleteMaxVisible?: number; +} + +const SLASH_COMMAND_SELECT_LIST_LAYOUT: SelectListLayoutOptions = { + minPrimaryColumnWidth: 12, + maxPrimaryColumnWidth: 32, +}; + +const ATTACHMENT_AUTOCOMPLETE_DEBOUNCE_MS = 20; +const DEFAULT_AUTOCOMPLETE_TRIGGER_CHARACTERS = ["@", "#"]; + +function escapeCharacterClass(value: string): string { + return value.replace(/[\\^$.*+?()[\]{}|-]/g, "\\$&"); +} + +function buildTriggerPattern(triggerCharacters: string[]): RegExp { + return new RegExp(`(?:^|[\\s])[${triggerCharacters.map(escapeCharacterClass).join("")}][^\\s]*$`); +} + +function buildDebouncePattern(triggerCharacters: string[]): RegExp { + const escapedWithoutAt = triggerCharacters.filter((character) => character !== "@").map(escapeCharacterClass); + return new RegExp(`(?:^|[ \\t])(?:@(?:"[^"]*|[^\\s]*)|[${escapedWithoutAt.join("")}][^\\s]*)$`); +} + +function createScrollBorder(direction: "↑" | "↓", hiddenLineCount: number, width: number): string { + const availableWidth = Math.max(0, width); + const label = ` ${direction} ${hiddenLineCount} more `; + const labelWidth = visibleWidth(label); + if (labelWidth + 2 <= availableWidth) { + const leftWidth = Math.floor((availableWidth - labelWidth) / 2); + return "─".repeat(leftWidth) + label + "─".repeat(availableWidth - leftWidth - labelWidth); + } + + const indicator = `─── ${direction} ${hiddenLineCount} more `; + const remaining = availableWidth - visibleWidth(indicator); + if (remaining >= 0) return indicator + "─".repeat(remaining); + + const ellipsis = "...".slice(0, availableWidth); + const indicatorWidth = availableWidth - visibleWidth(ellipsis); + return sliceByColumn(indicator, 0, indicatorWidth, true) + ellipsis; +} + +export class Editor implements Component, Focusable { + private state: EditorState = { + lines: [""], + cursorLine: 0, + cursorCol: 0, + }; + + /** Focusable interface - set by TUI when focus changes */ + focused: boolean = false; + + protected tui: TUI; + private theme: EditorTheme; + private paddingX: number = 0; + + // Store last render geometry for cursor navigation and mouse hit-testing. + private lastWidth: number = 80; + private renderedVisibleLineCount = 1; + private renderedAutocompleteHeight = 0; + + // Vertical scrolling support + private scrollOffset: number = 0; + + // Border color (can be changed dynamically) + public borderColor: (str: string) => string; + + // Autocomplete support + private autocompleteProvider?: AutocompleteProvider; + private autocompleteTriggerCharacters = [...DEFAULT_AUTOCOMPLETE_TRIGGER_CHARACTERS]; + private autocompleteTriggerPattern = buildTriggerPattern(this.autocompleteTriggerCharacters); + private autocompleteDebouncePattern = buildDebouncePattern(this.autocompleteTriggerCharacters); + private autocompleteList?: SelectList; + private autocompleteState: "regular" | "force" | null = null; + private autocompletePrefix: string = ""; + private autocompleteMaxVisible: number = 5; + private autocompleteAbort?: AbortController; + private autocompleteDebounceTimer?: ReturnType; + private autocompleteRequestTask: Promise = Promise.resolve(); + private autocompleteStartToken: number = 0; + private autocompleteRequestId: number = 0; + + // Paste tracking for large pastes + private pastes: Map = new Map(); + private pasteCounter: number = 0; + + // Bracketed paste mode buffering + private pasteBuffer: string = ""; + private isInPaste: boolean = false; + + // Prompt history for up/down navigation + private history: string[] = []; + private historyIndex: number = -1; // -1 = not browsing, 0 = most recent, 1 = older, etc. + private historyDraft: EditorState | null = null; + + // Kill ring for Emacs-style kill/yank operations + private killRing = new KillRing(); + private lastAction: "kill" | "yank" | "type-word" | null = null; + + // Character jump mode + private jumpMode: "forward" | "backward" | null = null; + + // Preferred visual column for vertical cursor movement (sticky column) + private preferredVisualCol: number | null = null; + + // When the cursor is snapped to the start of an atomic segment, e.g. a + // paste marker, cursorCol no longer reflects where the cursor would have + // landed. This field stores the pre-snap cursorCol so that the next + // vertical move can resolve it to a visual column on whatever VL it belongs + // to. + private snappedFromCursorCol: number | null = null; + + // Undo support + private undoStack = new UndoStack(); + + public onSubmit?: (text: string) => void; + public onChange?: (text: string) => void; + public disableSubmit: boolean = false; + + constructor(tui: TUI, theme: EditorTheme, options: EditorOptions = {}) { + this.tui = tui; + this.theme = theme; + this.borderColor = theme.borderColor; + const paddingX = options.paddingX ?? 0; + this.paddingX = Number.isFinite(paddingX) ? Math.max(0, Math.floor(paddingX)) : 0; + const maxVisible = options.autocompleteMaxVisible ?? 5; + this.autocompleteMaxVisible = Number.isFinite(maxVisible) ? Math.max(3, Math.min(20, Math.floor(maxVisible))) : 5; + } + + /** Set of currently valid paste IDs, for marker-aware segmentation. */ + private validPasteIds(): Set { + return new Set(this.pastes.keys()); + } + + /** Segment text with paste-marker awareness, only merging markers with valid IDs. */ + private segment(text: string, mode: "word" | "grapheme"): Iterable { + return segmentWithMarkers(text, mode === "word" ? wordSegmenter : graphemeSegmenter, this.validPasteIds()); + } + + getPaddingX(): number { + return this.paddingX; + } + + setPaddingX(padding: number): void { + const newPadding = Number.isFinite(padding) ? Math.max(0, Math.floor(padding)) : 0; + if (this.paddingX !== newPadding) { + this.paddingX = newPadding; + this.tui.requestRender(); + } + } + + getAutocompleteMaxVisible(): number { + return this.autocompleteMaxVisible; + } + + setAutocompleteMaxVisible(maxVisible: number): void { + const newMaxVisible = Number.isFinite(maxVisible) ? Math.max(3, Math.min(20, Math.floor(maxVisible))) : 5; + if (this.autocompleteMaxVisible !== newMaxVisible) { + this.autocompleteMaxVisible = newMaxVisible; + this.tui.requestRender(); + } + } + + setAutocompleteProvider(provider: AutocompleteProvider): void { + this.cancelAutocomplete(); + this.autocompleteProvider = provider; + this.setAutocompleteTriggerCharacters(provider.triggerCharacters ?? []); + } + + /** + * Add a prompt to history for up/down arrow navigation. + * Called after successful submission. + */ + addToHistory(text: string): void { + const trimmed = text.trim(); + if (!trimmed) return; + // Don't add consecutive duplicates + if (this.history.length > 0 && this.history[0] === trimmed) return; + this.history.unshift(trimmed); + // Limit history size + if (this.history.length > 100) { + this.history.pop(); + } + } + + private isEditorEmpty(): boolean { + return this.state.lines.length === 1 && this.state.lines[0] === ""; + } + + private isOnFirstVisualLine(): boolean { + const visualLines = this.buildVisualLineMap(this.lastWidth); + const currentVisualLine = this.findCurrentVisualLine(visualLines); + return currentVisualLine === 0; + } + + private isOnLastVisualLine(): boolean { + const visualLines = this.buildVisualLineMap(this.lastWidth); + const currentVisualLine = this.findCurrentVisualLine(visualLines); + return currentVisualLine === visualLines.length - 1; + } + + private navigateHistory(direction: 1 | -1): void { + this.lastAction = null; + if (this.history.length === 0) return; + + const newIndex = this.historyIndex - direction; // Up(-1) increases index, Down(1) decreases + if (newIndex < -1 || newIndex >= this.history.length) return; + + // Capture state when first entering history browsing mode + if (this.historyIndex === -1 && newIndex >= 0) { + this.pushUndoSnapshot(); + this.historyDraft = structuredClone(this.state); + } + + this.historyIndex = newIndex; + + if (this.historyIndex === -1) { + const draft = this.historyDraft; + this.historyDraft = null; + if (draft) { + this.state = draft; + this.preferredVisualCol = null; + this.snappedFromCursorCol = null; + this.scrollOffset = 0; + if (this.onChange) this.onChange(this.getText()); + } else { + this.setTextInternal(""); + } + } else { + this.setTextInternal(this.history[this.historyIndex] || "", direction === -1 ? "start" : "end"); + } + } + + private exitHistoryBrowsing(): void { + this.historyIndex = -1; + this.historyDraft = null; + } + + /** Internal setText that doesn't reset history state - used by navigateHistory */ + private setTextInternal(text: string, cursorPlacement: "start" | "end" = "end"): void { + const lines = text.split("\n"); + this.state.lines = lines.length === 0 ? [""] : lines; + this.state.cursorLine = cursorPlacement === "start" ? 0 : this.state.lines.length - 1; + this.setCursorCol(cursorPlacement === "start" ? 0 : this.state.lines[this.state.cursorLine]?.length || 0); + // Reset scroll - render() will adjust to show cursor + this.scrollOffset = 0; + + if (this.onChange) { + this.onChange(this.getText()); + } + } + + invalidate(): void { + // No cached state to invalidate currently + } + + protected renderTopBorder(width: number, hiddenLineCount: number): string { + const border = hiddenLineCount > 0 ? createScrollBorder("↑", hiddenLineCount, width) : "─".repeat(width); + return this.borderColor(border); + } + + protected renderBottomBorder(width: number, hiddenLineCount: number): string { + const border = hiddenLineCount > 0 ? createScrollBorder("↓", hiddenLineCount, width) : "─".repeat(width); + return this.borderColor(border); + } + + render(width: number): string[] { + const maxPadding = Math.max(0, Math.floor((width - 1) / 2)); + const paddingX = Math.min(this.paddingX, maxPadding); + const contentWidth = Math.max(1, width - paddingX * 2); + + // Layout width: with padding the cursor can overflow into it, + // without padding we reserve 1 column for the cursor. + const layoutWidth = Math.max(1, contentWidth - (paddingX ? 0 : 1)); + + // Store for cursor navigation (must match wrapping width) + this.lastWidth = layoutWidth; + + // Layout the text + const layoutLines = this.layoutText(layoutWidth); + + // Calculate max visible lines: 30% of terminal height, minimum 5 lines + const terminalRows = this.tui.terminal.rows; + const maxVisibleLines = Math.max(5, Math.floor(terminalRows * 0.3)); + + // Find the cursor line index in layoutLines + let cursorLineIndex = layoutLines.findIndex((line) => line.hasCursor); + if (cursorLineIndex === -1) cursorLineIndex = 0; + + // Adjust scroll offset to keep cursor visible + if (cursorLineIndex < this.scrollOffset) { + this.scrollOffset = cursorLineIndex; + } else if (cursorLineIndex >= this.scrollOffset + maxVisibleLines) { + this.scrollOffset = cursorLineIndex - maxVisibleLines + 1; + } + + // Clamp scroll offset to valid range + const maxScrollOffset = Math.max(0, layoutLines.length - maxVisibleLines); + this.scrollOffset = Math.max(0, Math.min(this.scrollOffset, maxScrollOffset)); + + // Get visible lines slice + const visibleLines = layoutLines.slice(this.scrollOffset, this.scrollOffset + maxVisibleLines); + this.renderedVisibleLineCount = visibleLines.length; + + const result: string[] = []; + const leftPadding = " ".repeat(paddingX); + const rightPadding = leftPadding; + + // Render top border (with scroll indicator if scrolled down) + result.push(this.renderTopBorder(width, this.scrollOffset)); + + // Render each visible layout line + // Emit hardware cursor marker when focused so TUI can position the + // hardware cursor for IME candidate-window placement even while + // autocomplete (e.g. slash-command menu) is visible. + const emitCursorMarker = this.focused; + + for (const layoutLine of visibleLines) { + let displayText = layoutLine.text; + let lineVisibleWidth = visibleWidth(layoutLine.text); + let cursorInPadding = false; + + // Add cursor if this line has it + if (layoutLine.hasCursor && layoutLine.cursorPos !== undefined) { + const before = displayText.slice(0, layoutLine.cursorPos); + const after = displayText.slice(layoutLine.cursorPos); + + // Hardware cursor marker (zero-width, emitted before fake cursor for IME positioning) + const marker = emitCursorMarker ? CURSOR_MARKER : ""; + + if (after.length > 0) { + // Cursor is on a character (grapheme) - replace it with highlighted version + // Get the first grapheme from 'after' + const afterGraphemes = [...this.segment(after, "grapheme")]; + const firstGrapheme = afterGraphemes[0]?.segment || ""; + const restAfter = after.slice(firstGrapheme.length); + const cursor = `\x1b[7m${firstGrapheme}\x1b[0m`; + displayText = before + marker + cursor + restAfter; + // lineVisibleWidth stays the same - we're replacing, not adding + } else { + // Cursor is at the end - add highlighted space + const cursor = "\x1b[7m \x1b[0m"; + displayText = before + marker + cursor; + lineVisibleWidth = lineVisibleWidth + 1; + // If cursor overflows content width into the padding, flag it + if (lineVisibleWidth > contentWidth && paddingX > 0) { + cursorInPadding = true; + } + } + } + + // Calculate padding based on actual visible width + const padding = " ".repeat(Math.max(0, contentWidth - lineVisibleWidth)); + const lineRightPadding = cursorInPadding ? rightPadding.slice(1) : rightPadding; + + // Render the line (no side borders, just horizontal lines above and below) + result.push(`${leftPadding}${displayText}${padding}${lineRightPadding}`); + } + + // Render bottom border (with scroll indicator if more content below) + const linesBelow = layoutLines.length - (this.scrollOffset + visibleLines.length); + result.push(this.renderBottomBorder(width, linesBelow)); + + // Add autocomplete list if active + this.renderedAutocompleteHeight = 0; + if (this.autocompleteState && this.autocompleteList) { + const autocompleteResult = this.autocompleteList.render(contentWidth); + this.renderedAutocompleteHeight = autocompleteResult.length; + for (const line of autocompleteResult) { + const lineWidth = visibleWidth(line); + const linePadding = " ".repeat(Math.max(0, contentWidth - lineWidth)); + result.push(`${leftPadding}${line}${linePadding}${rightPadding}`); + } + } + + return result; + } + + handleMouse(event: TuiMouseEvent): TuiMouseEventResult | undefined { + const autocompleteStartRow = this.renderedVisibleLineCount + 2; + if ( + this.autocompleteState && + this.autocompleteList && + event.y >= autocompleteStartRow && + event.y < autocompleteStartRow + this.renderedAutocompleteHeight + ) { + const maxPadding = Math.max(0, Math.floor((event.width - 1) / 2)); + const paddingX = Math.min(this.paddingX, maxPadding); + const contentWidth = Math.max(1, event.width - paddingX * 2); + const result = this.autocompleteList.handleMouse?.({ + ...event, + x: event.x - paddingX, + y: event.y - autocompleteStartRow, + width: contentWidth, + height: this.renderedAutocompleteHeight, + }); + return result ? { ...result, focus: true } : undefined; + } + + // Leave press/drag/release unhandled so the renderer's screen-level text + // selection can run over the editor rows (drag to select, release to copy). + // The renderer synthesizes a click when press and release land on the same + // cell without movement, which is the gesture that positions the cursor. + if (event.type !== "click" || event.button !== "left") return undefined; + if (event.y <= 0 || event.y > this.renderedVisibleLineCount) return { handled: true, focus: true }; + + const visualLines = this.buildVisualLineMap(this.lastWidth); + const visualLineIndex = this.scrollOffset + event.y - 1; + const visualLine = visualLines[visualLineIndex]; + if (!visualLine) return { handled: true, focus: true }; + const logicalLine = this.state.lines[visualLine.logicalLine] ?? ""; + const chunkEnd = visualLine.startCol + visualLine.length; + const chunk = logicalLine.slice(visualLine.startCol, chunkEnd); + const maxPadding = Math.max(0, Math.floor((event.width - 1) / 2)); + const paddingX = Math.min(this.paddingX, maxPadding); + const targetColumn = Math.max(0, event.x - paddingX); + let visibleColumn = 0; + let targetIndex = chunk.length; + let lastGraphemeIndex = 0; + for (const grapheme of this.segment(chunk, "grapheme")) { + const nextColumn = visibleColumn + visibleWidth(grapheme.segment); + lastGraphemeIndex = grapheme.index; + if (targetColumn < nextColumn) { + targetIndex = grapheme.index; + break; + } + visibleColumn = nextColumn; + } + const isLastSegment = + visualLineIndex === visualLines.length - 1 || + visualLines[visualLineIndex + 1]?.logicalLine !== visualLine.logicalLine; + if (!isLastSegment && targetIndex === chunk.length && chunk.length > 0) targetIndex = lastGraphemeIndex; + + this.state.cursorLine = visualLine.logicalLine; + this.setCursorCol(visualLine.startCol + targetIndex); + this.lastAction = null; + this.exitHistoryBrowsing(); + if (this.autocompleteState) this.updateAutocomplete(); + return { handled: true, focus: true }; + } + + handleInput(data: string): void { + const kb = getKeybindings(); + + // Handle character jump mode (awaiting next character to jump to) + if (this.jumpMode !== null) { + // Cancel if the hotkey is pressed again + if (kb.matches(data, "tui.editor.jumpForward") || kb.matches(data, "tui.editor.jumpBackward")) { + this.jumpMode = null; + return; + } + + const printable = decodePrintableKey(data) ?? (data.charCodeAt(0) >= 32 ? data : undefined); + if (printable !== undefined) { + // Printable character - perform the jump + const direction = this.jumpMode; + this.jumpMode = null; + this.jumpToChar(printable, direction); + return; + } + + // Control character - cancel and fall through to normal handling + this.jumpMode = null; + } + + // Handle bracketed paste mode + if (data.includes("\x1b[200~")) { + this.isInPaste = true; + this.pasteBuffer = ""; + data = data.replace("\x1b[200~", ""); + } + + if (this.isInPaste) { + this.pasteBuffer += data; + const endIndex = this.pasteBuffer.indexOf("\x1b[201~"); + if (endIndex !== -1) { + const pasteContent = this.pasteBuffer.substring(0, endIndex); + if (pasteContent.length > 0) { + this.handlePaste(pasteContent); + } + this.isInPaste = false; + const remaining = this.pasteBuffer.substring(endIndex + 6); + this.pasteBuffer = ""; + if (remaining.length > 0) { + this.handleInput(remaining); + } + return; + } + return; + } + + // Ctrl+C - let parent handle (exit/clear) + if (kb.matches(data, "tui.input.copy")) { + return; + } + + // Undo + if (kb.matches(data, "tui.editor.undo")) { + this.undo(); + return; + } + + // Handle autocomplete mode + if (this.autocompleteState && this.autocompleteList) { + if (kb.matches(data, "tui.select.cancel")) { + this.cancelAutocomplete(); + return; + } + + if (kb.matches(data, "tui.select.up") || kb.matches(data, "tui.select.down")) { + this.autocompleteList.handleInput(data); + return; + } + + if (kb.matches(data, "tui.input.tab")) { + const selected = this.autocompleteList.getSelectedItem(); + if (selected && this.autocompleteProvider) { + this.pushUndoSnapshot(); + this.lastAction = null; + const result = this.autocompleteProvider.applyCompletion( + this.state.lines, + this.state.cursorLine, + this.state.cursorCol, + selected, + this.autocompletePrefix, + ); + this.state.lines = result.lines; + this.state.cursorLine = result.cursorLine; + this.setCursorCol(result.cursorCol); + this.cancelAutocomplete(); + if (this.onChange) this.onChange(this.getText()); + } + return; + } + + if (kb.matches(data, "tui.select.confirm")) { + const selected = this.autocompleteList.getSelectedItem(); + if (selected && this.autocompleteProvider) { + this.pushUndoSnapshot(); + this.lastAction = null; + const result = this.autocompleteProvider.applyCompletion( + this.state.lines, + this.state.cursorLine, + this.state.cursorCol, + selected, + this.autocompletePrefix, + ); + this.state.lines = result.lines; + this.state.cursorLine = result.cursorLine; + this.setCursorCol(result.cursorCol); + + if (this.autocompletePrefix.startsWith("/")) { + this.cancelAutocomplete(); + // Fall through to submit + } else { + this.cancelAutocomplete(); + if (this.onChange) this.onChange(this.getText()); + return; + } + } + } + } + + // Tab - trigger completion + if (kb.matches(data, "tui.input.tab") && !this.autocompleteState) { + this.handleTabCompletion(); + return; + } + + // Deletion actions + if (kb.matches(data, "tui.editor.deleteToLineEnd")) { + this.deleteToEndOfLine(); + return; + } + if (kb.matches(data, "tui.editor.deleteToLineStart")) { + this.deleteToStartOfLine(); + return; + } + if (kb.matches(data, "tui.editor.deleteWordBackward")) { + this.deleteWordBackwards(); + return; + } + if (kb.matches(data, "tui.editor.deleteWordForward")) { + this.deleteWordForward(); + return; + } + if (kb.matches(data, "tui.editor.deleteCharBackward") || matchesKey(data, "shift+backspace")) { + this.handleBackspace(); + return; + } + if (kb.matches(data, "tui.editor.deleteCharForward") || matchesKey(data, "shift+delete")) { + this.handleForwardDelete(); + return; + } + + // Kill ring actions + if (kb.matches(data, "tui.editor.yank")) { + this.yank(); + return; + } + if (kb.matches(data, "tui.editor.yankPop")) { + this.yankPop(); + return; + } + + // Dedicated history actions always browse entries instead of moving the cursor. + if (kb.matches(data, "tui.editor.historyPrevious")) { + this.cancelAutocomplete(); + this.navigateHistory(-1); + return; + } + if (kb.matches(data, "tui.editor.historyNext")) { + this.cancelAutocomplete(); + this.navigateHistory(1); + return; + } + + // Cursor movement actions + if (kb.matches(data, "tui.editor.cursorLineStart")) { + this.moveToLineStart(); + return; + } + if (kb.matches(data, "tui.editor.cursorLineEnd")) { + this.moveToLineEnd(); + return; + } + if (kb.matches(data, "tui.editor.cursorWordLeft")) { + this.moveWordBackwards(); + return; + } + if (kb.matches(data, "tui.editor.cursorWordRight")) { + this.moveWordForwards(); + return; + } + + // New line + if ( + kb.matches(data, "tui.input.newLine") || + (data.charCodeAt(0) === 10 && data.length > 1) || + data === "\x1b\r" || + data === "\x1b[13;2~" || + (data.length > 1 && data.includes("\x1b") && data.includes("\r")) || + (data === "\n" && data.length === 1) + ) { + if (this.shouldSubmitOnBackslashEnter(data, kb)) { + this.handleBackspace(); + this.submitValue(); + return; + } + this.addNewLine(); + return; + } + + // Submit (Enter) + if (kb.matches(data, "tui.input.submit")) { + if (this.disableSubmit) return; + + // Workaround for terminals without Shift+Enter support: + // If char before cursor is \, delete it and insert newline instead of submitting. + const currentLine = this.state.lines[this.state.cursorLine] || ""; + if (this.state.cursorCol > 0 && currentLine[this.state.cursorCol - 1] === "\\") { + this.handleBackspace(); + this.addNewLine(); + return; + } + + this.submitValue(); + return; + } + + // Arrow key navigation (with history support) + if (kb.matches(data, "tui.editor.cursorUp")) { + if ( + this.isOnFirstVisualLine() && + (this.isEditorEmpty() || this.historyIndex > -1 || this.state.cursorCol === 0) + ) { + this.navigateHistory(-1); + } else if (this.isOnFirstVisualLine()) { + // Already at top - jump to start of line + this.moveToLineStart(); + } else { + this.moveCursor(-1, 0); + } + return; + } + if (kb.matches(data, "tui.editor.cursorDown")) { + if (this.historyIndex > -1 && this.isOnLastVisualLine()) { + this.navigateHistory(1); + } else if (this.isOnLastVisualLine()) { + // Already at bottom - jump to end of line + this.moveToLineEnd(); + } else { + this.moveCursor(1, 0); + } + return; + } + if (kb.matches(data, "tui.editor.cursorRight")) { + this.moveCursor(0, 1); + return; + } + if (kb.matches(data, "tui.editor.cursorLeft")) { + this.moveCursor(0, -1); + return; + } + + // Page up/down - scroll by page and move cursor + if (kb.matches(data, "tui.editor.pageUp")) { + this.pageScroll(-1); + return; + } + if (kb.matches(data, "tui.editor.pageDown")) { + this.pageScroll(1); + return; + } + + // Character jump mode triggers + if (kb.matches(data, "tui.editor.jumpForward")) { + this.jumpMode = "forward"; + return; + } + if (kb.matches(data, "tui.editor.jumpBackward")) { + this.jumpMode = "backward"; + return; + } + + // Shift+Space - insert regular space + if (matchesKey(data, "shift+space")) { + this.insertCharacter(" "); + return; + } + + const printable = decodePrintableKey(data); + if (printable !== undefined) { + this.insertCharacter(printable); + return; + } + + // Regular characters + if (data.charCodeAt(0) >= 32) { + this.insertCharacter(data); + } + } + + private layoutText(contentWidth: number): LayoutLine[] { + const layoutLines: LayoutLine[] = []; + + if (this.state.lines.length === 0 || (this.state.lines.length === 1 && this.state.lines[0] === "")) { + // Empty editor + layoutLines.push({ + text: "", + hasCursor: true, + cursorPos: 0, + }); + return layoutLines; + } + + // Process each logical line + for (let i = 0; i < this.state.lines.length; i++) { + const line = this.state.lines[i] || ""; + const isCurrentLine = i === this.state.cursorLine; + const lineVisibleWidth = visibleWidth(line); + + if (lineVisibleWidth <= contentWidth) { + // Line fits in one layout line + if (isCurrentLine) { + layoutLines.push({ + text: line, + hasCursor: true, + cursorPos: this.state.cursorCol, + }); + } else { + layoutLines.push({ + text: line, + hasCursor: false, + }); + } + } else { + // Line needs wrapping - use word-aware wrapping + const chunks = wordWrapLine(line, contentWidth, [...this.segment(line, "grapheme")]); + + for (let chunkIndex = 0; chunkIndex < chunks.length; chunkIndex++) { + const chunk = chunks[chunkIndex]; + if (!chunk) continue; + + const cursorPos = this.state.cursorCol; + const isLastChunk = chunkIndex === chunks.length - 1; + + // Determine if cursor is in this chunk + // For word-wrapped chunks, we need to handle the case where + // cursor might be in trimmed whitespace at end of chunk + let hasCursorInChunk = false; + let adjustedCursorPos = 0; + + if (isCurrentLine) { + if (isLastChunk) { + // Last chunk: cursor belongs here if >= startIndex + hasCursorInChunk = cursorPos >= chunk.startIndex; + adjustedCursorPos = cursorPos - chunk.startIndex; + } else { + // Non-last chunk: cursor belongs here if in range [startIndex, endIndex) + // But we need to handle the visual position in the trimmed text + hasCursorInChunk = cursorPos >= chunk.startIndex && cursorPos < chunk.endIndex; + if (hasCursorInChunk) { + adjustedCursorPos = cursorPos - chunk.startIndex; + // Clamp to text length (in case cursor was in trimmed whitespace) + if (adjustedCursorPos > chunk.text.length) { + adjustedCursorPos = chunk.text.length; + } + } + } + } + + if (hasCursorInChunk) { + layoutLines.push({ + text: chunk.text, + hasCursor: true, + cursorPos: adjustedCursorPos, + }); + } else { + layoutLines.push({ + text: chunk.text, + hasCursor: false, + }); + } + } + } + } + + return layoutLines; + } + + getText(): string { + return this.state.lines.join("\n"); + } + + private expandPasteMarkers(text: string): string { + let result = text; + for (const [pasteId, pasteContent] of this.pastes) { + const markerRegex = new RegExp(`\\[paste #${pasteId}( (\\+\\d+ lines|\\d+ chars))?\\]`, "g"); + result = result.replace(markerRegex, () => pasteContent); + } + return result; + } + + /** + * Get text with paste markers expanded to their actual content. + * Use this when you need the full content (e.g., for external editor). + */ + getExpandedText(): string { + return this.expandPasteMarkers(this.state.lines.join("\n")); + } + + getLines(): string[] { + return [...this.state.lines]; + } + + getCursor(): { line: number; col: number } { + return { line: this.state.cursorLine, col: this.state.cursorCol }; + } + + setText(text: string): void { + this.cancelAutocomplete(); + this.lastAction = null; + this.exitHistoryBrowsing(); + const normalized = this.normalizeText(text); + // Push undo snapshot if content differs (makes programmatic changes undoable) + if (this.getText() !== normalized) { + this.pushUndoSnapshot(); + } + this.pastes.clear(); + this.pasteCounter = 0; + this.setTextInternal(normalized); + } + + /** + * Insert text at the current cursor position. + * Used for programmatic insertion (e.g., clipboard image markers). + * This is atomic for undo - single undo restores entire pre-insert state. + */ + insertTextAtCursor(text: string): void { + if (!text) return; + this.cancelAutocomplete(); + this.pushUndoSnapshot(); + this.lastAction = null; + this.exitHistoryBrowsing(); + this.insertTextAtCursorInternal(text); + } + + /** + * Normalize text for editor storage: + * - Normalize line endings (\r\n and \r -> \n) + * - Expand tabs to 4 spaces + */ + private normalizeText(text: string): string { + return text.replace(/\r\n/g, "\n").replace(/\r/g, "\n").replace(/\t/g, " "); + } + + /** + * Internal text insertion at cursor. Handles single and multi-line text. + * Does not push undo snapshots or trigger autocomplete - caller is responsible. + * Normalizes line endings and calls onChange once at the end. + */ + private insertTextAtCursorInternal(text: string): void { + if (!text) return; + + // Normalize line endings and tabs + const normalized = this.normalizeText(text); + const insertedLines = normalized.split("\n"); + + const currentLine = this.state.lines[this.state.cursorLine] || ""; + const beforeCursor = currentLine.slice(0, this.state.cursorCol); + const afterCursor = currentLine.slice(this.state.cursorCol); + + if (insertedLines.length === 1) { + // Single line - insert at cursor position + this.state.lines[this.state.cursorLine] = beforeCursor + normalized + afterCursor; + this.setCursorCol(this.state.cursorCol + normalized.length); + } else { + // Multi-line insertion + this.state.lines = [ + // All lines before current line + ...this.state.lines.slice(0, this.state.cursorLine), + + // The first inserted line merged with text before cursor + beforeCursor + insertedLines[0], + + // All middle inserted lines + ...insertedLines.slice(1, -1), + + // The last inserted line with text after cursor + insertedLines[insertedLines.length - 1] + afterCursor, + + // All lines after current line + ...this.state.lines.slice(this.state.cursorLine + 1), + ]; + + this.state.cursorLine += insertedLines.length - 1; + this.setCursorCol((insertedLines[insertedLines.length - 1] || "").length); + } + + if (this.onChange) { + this.onChange(this.getText()); + } + } + + // All the editor methods from before... + private insertCharacter(char: string, skipUndoCoalescing?: boolean): void { + this.exitHistoryBrowsing(); + + // Undo coalescing (fish-style): + // - Consecutive word chars coalesce into one undo unit + // - Space captures state before itself (so undo removes space+following word together) + // - Each space is separately undoable + // Skip coalescing when called from atomic operations (e.g., handlePaste) + if (!skipUndoCoalescing) { + if (isWhitespaceChar(char) || this.lastAction !== "type-word") { + this.pushUndoSnapshot(); + } + this.lastAction = "type-word"; + } + + const line = this.state.lines[this.state.cursorLine] || ""; + + const before = line.slice(0, this.state.cursorCol); + const after = line.slice(this.state.cursorCol); + + this.state.lines[this.state.cursorLine] = before + char + after; + this.setCursorCol(this.state.cursorCol + char.length); + + if (this.onChange) { + this.onChange(this.getText()); + } + + // Check if we should trigger or update autocomplete + if (!this.autocompleteState) { + // Auto-trigger for "/" at the start of a line (slash commands) + if (char === "/" && this.isAtStartOfMessage()) { + this.tryTriggerAutocomplete(); + } + // Auto-trigger for symbol-based completion like @, #, or provider triggers at token boundaries + else if (this.autocompleteTriggerCharacters.includes(char)) { + const currentLine = this.state.lines[this.state.cursorLine] || ""; + const textBeforeCursor = currentLine.slice(0, this.state.cursorCol); + const charBeforeSymbol = textBeforeCursor[textBeforeCursor.length - 2]; + if (textBeforeCursor.length === 1 || charBeforeSymbol === " " || charBeforeSymbol === "\t") { + this.tryTriggerAutocomplete(); + } + } + // Also auto-trigger when typing letters in a slash command or symbol completion context + else if (/[a-zA-Z0-9.\-_]/.test(char)) { + const currentLine = this.state.lines[this.state.cursorLine] || ""; + const textBeforeCursor = currentLine.slice(0, this.state.cursorCol); + // Check if we're in a slash command (with or without space for arguments) + if (this.isInSlashCommandContext(textBeforeCursor)) { + this.tryTriggerAutocomplete(); + } + // Check if we're in a symbol-based completion context like @, #, or provider triggers + else if (this.autocompleteTriggerPattern.test(textBeforeCursor)) { + this.tryTriggerAutocomplete(); + } + } + } else { + this.updateAutocomplete(); + } + } + + private handlePaste(pastedText: string): void { + this.cancelAutocomplete(); + this.exitHistoryBrowsing(); + this.lastAction = null; + + this.pushUndoSnapshot(); + + // Some terminals (e.g. tmux popups with extended-keys-format=csi-u) re-encode + // control bytes inside bracketed paste as CSI-u Ctrl+ sequences + // (ESC [ ; 5 u). Decode those back to their literal byte so the + // per-char filter below preserves newlines instead of stripping ESC and + // leaking the printable tail (e.g. "[106;5u") into the editor. + const decodedText = pastedText.replace(/\x1b\[(\d+);5u/g, (match, code) => { + const cp = Number(code); + if (cp >= 97 && cp <= 122) return String.fromCharCode(cp - 96); + if (cp >= 65 && cp <= 90) return String.fromCharCode(cp - 64); + return match; + }); + + // Clean the pasted text: normalize line endings, expand tabs + const cleanText = this.normalizeText(decodedText); + + // Filter out non-printable characters except newlines + let filteredText = cleanText + .split("") + .filter((char) => char === "\n" || char.charCodeAt(0) >= 32) + .join(""); + + // If pasting a file path (starts with /, ~, or .) and the character before + // the cursor is a word character, prepend a space for better readability + if (/^[/~.]/.test(filteredText)) { + const currentLine = this.state.lines[this.state.cursorLine] || ""; + const charBeforeCursor = this.state.cursorCol > 0 ? currentLine[this.state.cursorCol - 1] : ""; + if (charBeforeCursor && /\w/.test(charBeforeCursor)) { + filteredText = ` ${filteredText}`; + } + } + + // Split into lines to check for large paste + const pastedLines = filteredText.split("\n"); + + // Check if this is a large paste (> 10 lines or > 1000 characters) + const totalChars = filteredText.length; + if (pastedLines.length > 10 || totalChars > 1000) { + // Store the paste and insert a marker + this.pasteCounter++; + const pasteId = this.pasteCounter; + this.pastes.set(pasteId, filteredText); + + // Insert marker like "[paste #1 +123 lines]" or "[paste #1 1234 chars]" + const marker = + pastedLines.length > 10 + ? `[paste #${pasteId} +${pastedLines.length} lines]` + : `[paste #${pasteId} ${totalChars} chars]`; + this.insertTextAtCursorInternal(marker); + return; + } + + if (pastedLines.length === 1) { + // Single line - insert atomically (do not trigger autocomplete during paste) + this.insertTextAtCursorInternal(filteredText); + return; + } + + // Multi-line paste - use direct state manipulation + this.insertTextAtCursorInternal(filteredText); + } + + private addNewLine(): void { + this.cancelAutocomplete(); + this.exitHistoryBrowsing(); + this.lastAction = null; + + this.pushUndoSnapshot(); + + const currentLine = this.state.lines[this.state.cursorLine] || ""; + + const before = currentLine.slice(0, this.state.cursorCol); + const after = currentLine.slice(this.state.cursorCol); + + // Split current line + this.state.lines[this.state.cursorLine] = before; + this.state.lines.splice(this.state.cursorLine + 1, 0, after); + + // Move cursor to start of new line + this.state.cursorLine++; + this.setCursorCol(0); + + if (this.onChange) { + this.onChange(this.getText()); + } + } + + private shouldSubmitOnBackslashEnter(data: string, kb: ReturnType): boolean { + if (this.disableSubmit) return false; + if (!matchesKey(data, "enter")) return false; + const submitKeys = kb.getKeys("tui.input.submit"); + const hasShiftEnter = submitKeys.includes("shift+enter") || submitKeys.includes("shift+return"); + if (!hasShiftEnter) return false; + + const currentLine = this.state.lines[this.state.cursorLine] || ""; + return this.state.cursorCol > 0 && currentLine[this.state.cursorCol - 1] === "\\"; + } + + private submitValue(): void { + this.cancelAutocomplete(); + const result = this.expandPasteMarkers(this.state.lines.join("\n")).trim(); + + this.state = { lines: [""], cursorLine: 0, cursorCol: 0 }; + this.pastes.clear(); + this.pasteCounter = 0; + this.exitHistoryBrowsing(); + this.scrollOffset = 0; + this.undoStack.clear(); + this.lastAction = null; + + if (this.onChange) this.onChange(""); + if (this.onSubmit) this.onSubmit(result); + } + + private handleBackspace(): void { + this.exitHistoryBrowsing(); + this.lastAction = null; + + if (this.state.cursorCol > 0) { + this.pushUndoSnapshot(); + + // Delete grapheme before cursor (handles emojis, combining characters, etc.) + let line = this.state.lines[this.state.cursorLine] || ""; + const beforeCursor = line.slice(0, this.state.cursorCol); + + // Find the last grapheme in the text before cursor + const graphemes = [...this.segment(beforeCursor, "grapheme")]; + const lastGrapheme = graphemes[graphemes.length - 1]; + const graphemeLength = lastGrapheme ? lastGrapheme.segment.length : 1; + const isPastedSegmented = PASTE_MARKER_SINGLE.exec(lastGrapheme.segment); + + if (isPastedSegmented) { + // This contains the id part e.g 4 from [paste #4 +123 lines] + const targetId = Number(isPastedSegmented[1]); + this.pastes.delete(targetId); + this.pasteCounter--; + + // Shift registry entries down in ascending id order, independent + // of marker order in the text ([paste #3] becomes [paste #2] when + // [paste #1] is removed). + const higherIds = [...this.pastes.keys()].filter((id) => id > targetId).sort((a, b) => a - b); + for (const id of higherIds) { + this.pastes.set(id - 1, this.pastes.get(id)!); + this.pastes.delete(id); + } + + // Renumber markers with ids greater than the removed one. + this.state.lines = this.state.lines.map((line) => + line.replace(PASTE_MARKER_REGEX, (fullMatch, idGroup, suffixGroup) => { + const x = Number(idGroup); + if (x <= targetId) return fullMatch; + return `[paste #${x - 1}${suffixGroup}]`; + }), + ); + } + + line = this.state.lines[this.state.cursorLine] || ""; + + const before = line.slice(0, this.state.cursorCol - graphemeLength); + const after = line.slice(this.state.cursorCol); + + this.state.lines[this.state.cursorLine] = before + after; + this.setCursorCol(this.state.cursorCol - graphemeLength); + } else if (this.state.cursorLine > 0) { + this.pushUndoSnapshot(); + + // Merge with previous line + const currentLine = this.state.lines[this.state.cursorLine] || ""; + const previousLine = this.state.lines[this.state.cursorLine - 1] || ""; + + this.state.lines[this.state.cursorLine - 1] = previousLine + currentLine; + this.state.lines.splice(this.state.cursorLine, 1); + + this.state.cursorLine--; + this.setCursorCol(previousLine.length); + } + + if (this.onChange) { + this.onChange(this.getText()); + } + + // Update or re-trigger autocomplete after backspace + if (this.autocompleteState) { + this.updateAutocomplete(); + } else { + // If autocomplete was cancelled (no matches), re-trigger if we're in a completable context + const currentLine = this.state.lines[this.state.cursorLine] || ""; + const textBeforeCursor = currentLine.slice(0, this.state.cursorCol); + // Slash command context + if (this.isInSlashCommandContext(textBeforeCursor)) { + this.tryTriggerAutocomplete(); + } + // Symbol-based completion context like @, #, or provider triggers + else if (this.autocompleteTriggerPattern.test(textBeforeCursor)) { + this.tryTriggerAutocomplete(); + } + } + } + + /** + * Set cursor column and clear preferredVisualCol. + * Use this for all non-vertical cursor movements to reset sticky column behavior. + */ + private setCursorCol(col: number): void { + this.state.cursorCol = col; + this.preferredVisualCol = null; + this.snappedFromCursorCol = null; + } + + /** + * Move cursor to a target visual line, applying sticky column logic. + * Shared by moveCursor() and pageScroll(). + */ + private moveToVisualLine( + visualLines: Array<{ logicalLine: number; startCol: number; length: number }>, + currentVisualLine: number, + targetVisualLine: number, + ): void { + const currentVL = visualLines[currentVisualLine]; + const targetVL = visualLines[targetVisualLine]; + if (!(currentVL && targetVL)) return; + + // When the cursor was snapped to a segment start, resolve the pre-snap + // position against the VL it belongs to. This gives the correct visual + // column even after a resize reshuffles VLs. + let currentVisualCol: number; + if (this.snappedFromCursorCol !== null) { + const vlIndex = this.findVisualLineAt(visualLines, currentVL.logicalLine, this.snappedFromCursorCol); + currentVisualCol = this.snappedFromCursorCol - visualLines[vlIndex].startCol; + } else { + currentVisualCol = this.state.cursorCol - currentVL.startCol; + } + + // For non-last segments, clamp to length-1 to stay within the segment + const isLastSourceSegment = + currentVisualLine === visualLines.length - 1 || + visualLines[currentVisualLine + 1]?.logicalLine !== currentVL.logicalLine; + const sourceMaxVisualCol = isLastSourceSegment ? currentVL.length : Math.max(0, currentVL.length - 1); + + const isLastTargetSegment = + targetVisualLine === visualLines.length - 1 || + visualLines[targetVisualLine + 1]?.logicalLine !== targetVL.logicalLine; + const targetMaxVisualCol = isLastTargetSegment ? targetVL.length : Math.max(0, targetVL.length - 1); + + const moveToVisualCol = this.computeVerticalMoveColumn(currentVisualCol, sourceMaxVisualCol, targetMaxVisualCol); + + // Set cursor position + this.state.cursorLine = targetVL.logicalLine; + const targetCol = targetVL.startCol + moveToVisualCol; + const logicalLine = this.state.lines[targetVL.logicalLine] || ""; + this.state.cursorCol = Math.min(targetCol, logicalLine.length); + + // Snap cursor to atomic segment boundary (e.g. paste markers) + // so the cursor never lands in the middle of a multi-grapheme unit. + // Single-grapheme segments don't need snapping. + const segments = [...this.segment(logicalLine, "grapheme")]; + for (const seg of segments) { + if (seg.index > this.state.cursorCol) break; + if (seg.segment.length <= 1) continue; + if (this.state.cursorCol < seg.index + seg.segment.length) { + const isContinuation = seg.index < targetVL.startCol; + const isMovingDown = targetVisualLine > currentVisualLine; + + if (isContinuation && isMovingDown) { + // The segment started on a previous visual line, and we + // already visited it on the way down. Skip all remaining + // continuation VLs and land on the first VL past it. + const segEnd = seg.index + seg.segment.length; + let next = targetVisualLine + 1; + while ( + next < visualLines.length && + visualLines[next].logicalLine === targetVL.logicalLine && + visualLines[next].startCol < segEnd + ) { + next++; + } + if (next < visualLines.length) { + this.moveToVisualLine(visualLines, currentVisualLine, next); + return; + } + } + + // Snap to the start of the segment so it gets highlighted. + // Store the pre-snap position so the next vertical move can + // resolve it to the correct visual column. + this.snappedFromCursorCol = this.state.cursorCol; + this.state.cursorCol = seg.index; + return; + } + } + + // No snap occurred – we moved out of the atomic segment. + this.snappedFromCursorCol = null; + } + + /** + * Compute the target visual column for vertical cursor movement. + * Implements the sticky column decision table: + * + * | P | S | T | U | Scenario | Set Preferred | Move To | + * |---|---|---|---| ---------------------------------------------------- |---------------|-------------| + * | 0 | * | 0 | - | Start nav, target fits | null | current | + * | 0 | * | 1 | - | Start nav, target shorter | current | target end | + * | 1 | 0 | 0 | 0 | Clamped, target fits preferred | null | preferred | + * | 1 | 0 | 0 | 1 | Clamped, target longer but still can't fit preferred | keep | target end | + * | 1 | 0 | 1 | - | Clamped, target even shorter | keep | target end | + * | 1 | 1 | 0 | - | Rewrapped, target fits current | null | current | + * | 1 | 1 | 1 | - | Rewrapped, target shorter than current | current | target end | + * + * Where: + * - P = preferred col is set + * - S = cursor in middle of source line (not clamped to end) + * - T = target line shorter than current visual col + * - U = target line shorter than preferred col + */ + private computeVerticalMoveColumn( + currentVisualCol: number, + sourceMaxVisualCol: number, + targetMaxVisualCol: number, + ): number { + const hasPreferred = this.preferredVisualCol !== null; // P + const cursorInMiddle = currentVisualCol < sourceMaxVisualCol; // S + const targetTooShort = targetMaxVisualCol < currentVisualCol; // T + + if (!hasPreferred || cursorInMiddle) { + if (targetTooShort) { + // Cases 2 and 7 + this.preferredVisualCol = currentVisualCol; + return targetMaxVisualCol; + } + + // Cases 1 and 6 + this.preferredVisualCol = null; + return currentVisualCol; + } + + const targetCantFitPreferred = targetMaxVisualCol < this.preferredVisualCol!; // U + if (targetTooShort || targetCantFitPreferred) { + // Cases 4 and 5 + return targetMaxVisualCol; + } + + // Case 3 + const result = this.preferredVisualCol!; + this.preferredVisualCol = null; + return result; + } + + private moveToLineStart(): void { + this.lastAction = null; + this.setCursorCol(0); + } + + private moveToLineEnd(): void { + this.lastAction = null; + const currentLine = this.state.lines[this.state.cursorLine] || ""; + this.setCursorCol(currentLine.length); + } + + private deleteToStartOfLine(): void { + this.exitHistoryBrowsing(); + + const currentLine = this.state.lines[this.state.cursorLine] || ""; + + if (this.state.cursorCol > 0) { + this.pushUndoSnapshot(); + + // Calculate text to be deleted and save to kill ring (backward deletion = prepend) + const deletedText = currentLine.slice(0, this.state.cursorCol); + this.killRing.push(deletedText, { prepend: true, accumulate: this.lastAction === "kill" }); + this.lastAction = "kill"; + + // Delete from start of line up to cursor + this.state.lines[this.state.cursorLine] = currentLine.slice(this.state.cursorCol); + this.setCursorCol(0); + } else if (this.state.cursorLine > 0) { + this.pushUndoSnapshot(); + + // At start of line - merge with previous line, treating newline as deleted text + this.killRing.push("\n", { prepend: true, accumulate: this.lastAction === "kill" }); + this.lastAction = "kill"; + + const previousLine = this.state.lines[this.state.cursorLine - 1] || ""; + this.state.lines[this.state.cursorLine - 1] = previousLine + currentLine; + this.state.lines.splice(this.state.cursorLine, 1); + this.state.cursorLine--; + this.setCursorCol(previousLine.length); + } + + if (this.onChange) { + this.onChange(this.getText()); + } + } + + private deleteToEndOfLine(): void { + this.exitHistoryBrowsing(); + + const currentLine = this.state.lines[this.state.cursorLine] || ""; + + if (this.state.cursorCol < currentLine.length) { + this.pushUndoSnapshot(); + + // Calculate text to be deleted and save to kill ring (forward deletion = append) + const deletedText = currentLine.slice(this.state.cursorCol); + this.killRing.push(deletedText, { prepend: false, accumulate: this.lastAction === "kill" }); + this.lastAction = "kill"; + + // Delete from cursor to end of line + this.state.lines[this.state.cursorLine] = currentLine.slice(0, this.state.cursorCol); + } else if (this.state.cursorLine < this.state.lines.length - 1) { + this.pushUndoSnapshot(); + + // At end of line - merge with next line, treating newline as deleted text + this.killRing.push("\n", { prepend: false, accumulate: this.lastAction === "kill" }); + this.lastAction = "kill"; + + const nextLine = this.state.lines[this.state.cursorLine + 1] || ""; + this.state.lines[this.state.cursorLine] = currentLine + nextLine; + this.state.lines.splice(this.state.cursorLine + 1, 1); + } + + if (this.onChange) { + this.onChange(this.getText()); + } + } + + private deleteWordBackwards(): void { + this.exitHistoryBrowsing(); + + const currentLine = this.state.lines[this.state.cursorLine] || ""; + + // If at start of line, behave like backspace at column 0 (merge with previous line) + if (this.state.cursorCol === 0) { + if (this.state.cursorLine > 0) { + this.pushUndoSnapshot(); + + // Treat newline as deleted text (backward deletion = prepend) + this.killRing.push("\n", { prepend: true, accumulate: this.lastAction === "kill" }); + this.lastAction = "kill"; + + const previousLine = this.state.lines[this.state.cursorLine - 1] || ""; + this.state.lines[this.state.cursorLine - 1] = previousLine + currentLine; + this.state.lines.splice(this.state.cursorLine, 1); + this.state.cursorLine--; + this.setCursorCol(previousLine.length); + } + } else { + this.pushUndoSnapshot(); + + // Save lastAction before cursor movement (moveWordBackwards resets it) + const wasKill = this.lastAction === "kill"; + + const oldCursorCol = this.state.cursorCol; + this.moveWordBackwards(); + const deleteFrom = this.state.cursorCol; + this.setCursorCol(oldCursorCol); + + const deletedText = currentLine.slice(deleteFrom, this.state.cursorCol); + this.killRing.push(deletedText, { prepend: true, accumulate: wasKill }); + this.lastAction = "kill"; + + this.state.lines[this.state.cursorLine] = + currentLine.slice(0, deleteFrom) + currentLine.slice(this.state.cursorCol); + this.setCursorCol(deleteFrom); + } + + if (this.onChange) { + this.onChange(this.getText()); + } + } + + private deleteWordForward(): void { + this.exitHistoryBrowsing(); + + const currentLine = this.state.lines[this.state.cursorLine] || ""; + + // If at end of line, merge with next line (delete the newline) + if (this.state.cursorCol >= currentLine.length) { + if (this.state.cursorLine < this.state.lines.length - 1) { + this.pushUndoSnapshot(); + + // Treat newline as deleted text (forward deletion = append) + this.killRing.push("\n", { prepend: false, accumulate: this.lastAction === "kill" }); + this.lastAction = "kill"; + + const nextLine = this.state.lines[this.state.cursorLine + 1] || ""; + this.state.lines[this.state.cursorLine] = currentLine + nextLine; + this.state.lines.splice(this.state.cursorLine + 1, 1); + } + } else { + this.pushUndoSnapshot(); + + // Save lastAction before cursor movement (moveWordForwards resets it) + const wasKill = this.lastAction === "kill"; + + const oldCursorCol = this.state.cursorCol; + this.moveWordForwards(); + const deleteTo = this.state.cursorCol; + this.setCursorCol(oldCursorCol); + + const deletedText = currentLine.slice(this.state.cursorCol, deleteTo); + this.killRing.push(deletedText, { prepend: false, accumulate: wasKill }); + this.lastAction = "kill"; + + this.state.lines[this.state.cursorLine] = + currentLine.slice(0, this.state.cursorCol) + currentLine.slice(deleteTo); + } + + if (this.onChange) { + this.onChange(this.getText()); + } + } + + private handleForwardDelete(): void { + this.exitHistoryBrowsing(); + this.lastAction = null; + + const currentLine = this.state.lines[this.state.cursorLine] || ""; + + if (this.state.cursorCol < currentLine.length) { + this.pushUndoSnapshot(); + + // Delete grapheme at cursor position (handles emojis, combining characters, etc.) + const afterCursor = currentLine.slice(this.state.cursorCol); + + // Find the first grapheme at cursor + const graphemes = [...this.segment(afterCursor, "grapheme")]; + const firstGrapheme = graphemes[0]; + const graphemeLength = firstGrapheme ? firstGrapheme.segment.length : 1; + + const before = currentLine.slice(0, this.state.cursorCol); + const after = currentLine.slice(this.state.cursorCol + graphemeLength); + this.state.lines[this.state.cursorLine] = before + after; + } else if (this.state.cursorLine < this.state.lines.length - 1) { + this.pushUndoSnapshot(); + + // At end of line - merge with next line + const nextLine = this.state.lines[this.state.cursorLine + 1] || ""; + this.state.lines[this.state.cursorLine] = currentLine + nextLine; + this.state.lines.splice(this.state.cursorLine + 1, 1); + } + + if (this.onChange) { + this.onChange(this.getText()); + } + + // Update or re-trigger autocomplete after forward delete + if (this.autocompleteState) { + this.updateAutocomplete(); + } else { + const currentLine = this.state.lines[this.state.cursorLine] || ""; + const textBeforeCursor = currentLine.slice(0, this.state.cursorCol); + // Slash command context + if (this.isInSlashCommandContext(textBeforeCursor)) { + this.tryTriggerAutocomplete(); + } + // Symbol-based completion context like @, #, or provider triggers + else if (this.autocompleteTriggerPattern.test(textBeforeCursor)) { + this.tryTriggerAutocomplete(); + } + } + } + + /** + * Build a mapping from visual lines to logical positions. + * Returns an array where each element represents a visual line with: + * - logicalLine: index into this.state.lines + * - startCol: starting column in the logical line + * - length: length of this visual line segment + */ + private buildVisualLineMap(width: number): Array<{ logicalLine: number; startCol: number; length: number }> { + const visualLines: Array<{ logicalLine: number; startCol: number; length: number }> = []; + + for (let i = 0; i < this.state.lines.length; i++) { + const line = this.state.lines[i] || ""; + const lineVisWidth = visibleWidth(line); + if (line.length === 0) { + // Empty line still takes one visual line + visualLines.push({ logicalLine: i, startCol: 0, length: 0 }); + } else if (lineVisWidth <= width) { + visualLines.push({ logicalLine: i, startCol: 0, length: line.length }); + } else { + // Line needs wrapping - use word-aware wrapping + const chunks = wordWrapLine(line, width, [...this.segment(line, "grapheme")]); + for (const chunk of chunks) { + visualLines.push({ + logicalLine: i, + startCol: chunk.startIndex, + length: chunk.endIndex - chunk.startIndex, + }); + } + } + } + + return visualLines; + } + + /** + * Find the visual line index that contains the given logical position. + */ + private findVisualLineAt( + visualLines: Array<{ logicalLine: number; startCol: number; length: number }>, + line: number, + col: number, + ): number { + for (let i = 0; i < visualLines.length; i++) { + const vl = visualLines[i]; + if (!vl || vl.logicalLine !== line) continue; + const offset = col - vl.startCol; + // Cursor is in this segment if it's within range. For the last + // segment of a logical line, cursor can be at length (end position) + const isLastSegmentOfLine = i === visualLines.length - 1 || visualLines[i + 1]?.logicalLine !== vl.logicalLine; + if (offset >= 0 && (offset < vl.length || (isLastSegmentOfLine && offset === vl.length))) { + return i; + } + } + return visualLines.length - 1; + } + + /** + * Find the visual line index for the current cursor position. + */ + private findCurrentVisualLine( + visualLines: Array<{ logicalLine: number; startCol: number; length: number }>, + ): number { + return this.findVisualLineAt(visualLines, this.state.cursorLine, this.state.cursorCol); + } + + private moveCursor(deltaLine: number, deltaCol: number): void { + this.lastAction = null; + const visualLines = this.buildVisualLineMap(this.lastWidth); + const currentVisualLine = this.findCurrentVisualLine(visualLines); + + if (deltaLine !== 0) { + const targetVisualLine = currentVisualLine + deltaLine; + + if (targetVisualLine >= 0 && targetVisualLine < visualLines.length) { + this.moveToVisualLine(visualLines, currentVisualLine, targetVisualLine); + } + } + + if (deltaCol !== 0) { + const currentLine = this.state.lines[this.state.cursorLine] || ""; + + if (deltaCol > 0) { + // Moving right - move by one grapheme (handles emojis, combining characters, etc.) + if (this.state.cursorCol < currentLine.length) { + const afterCursor = currentLine.slice(this.state.cursorCol); + const graphemes = [...this.segment(afterCursor, "grapheme")]; + const firstGrapheme = graphemes[0]; + this.setCursorCol(this.state.cursorCol + (firstGrapheme ? firstGrapheme.segment.length : 1)); + } else if (this.state.cursorLine < this.state.lines.length - 1) { + // Wrap to start of next logical line + this.state.cursorLine++; + this.setCursorCol(0); + } else { + // At end of last line - can't move, but set preferredVisualCol for up/down navigation + const currentVL = visualLines[currentVisualLine]; + if (currentVL) { + this.preferredVisualCol = this.state.cursorCol - currentVL.startCol; + } + } + } else { + // Moving left - move by one grapheme (handles emojis, combining characters, etc.) + if (this.state.cursorCol > 0) { + const beforeCursor = currentLine.slice(0, this.state.cursorCol); + const graphemes = [...this.segment(beforeCursor, "grapheme")]; + const lastGrapheme = graphemes[graphemes.length - 1]; + this.setCursorCol(this.state.cursorCol - (lastGrapheme ? lastGrapheme.segment.length : 1)); + } else if (this.state.cursorLine > 0) { + // Wrap to end of previous logical line + this.state.cursorLine--; + const prevLine = this.state.lines[this.state.cursorLine] || ""; + this.setCursorCol(prevLine.length); + } + } + } + + // Keep an open autocomplete picker in sync with the new cursor + // position: cursor movement changes the text before the cursor, so a + // picker computed for the old position is stale. Re-query so it + // refreshes — or closes when the new position yields no suggestions — + // mirroring insertCharacter()/handleBackspace(). Without this, arrowing + // left from `/cmd ` back into the command name leaves the argument + // picker showing against a `/cmd` prefix (and a Tab there would + // concatenate the stale suggestion onto the partial command name). + if (this.autocompleteState) { + this.updateAutocomplete(); + } + } + + /** + * Scroll by a page (direction: -1 for up, 1 for down). + * Moves cursor by the page size while keeping it in bounds. + */ + private pageScroll(direction: -1 | 1): void { + this.lastAction = null; + const terminalRows = this.tui.terminal.rows; + const pageSize = Math.max(5, Math.floor(terminalRows * 0.3)); + + const visualLines = this.buildVisualLineMap(this.lastWidth); + const currentVisualLine = this.findCurrentVisualLine(visualLines); + const targetVisualLine = Math.max(0, Math.min(visualLines.length - 1, currentVisualLine + direction * pageSize)); + + this.moveToVisualLine(visualLines, currentVisualLine, targetVisualLine); + } + + private moveWordBackwards(): void { + this.lastAction = null; + const currentLine = this.state.lines[this.state.cursorLine] || ""; + + // If at start of line, move to end of previous line + if (this.state.cursorCol === 0) { + if (this.state.cursorLine > 0) { + this.state.cursorLine--; + const prevLine = this.state.lines[this.state.cursorLine] || ""; + this.setCursorCol(prevLine.length); + } + return; + } + + this.setCursorCol( + findWordBackward(currentLine, this.state.cursorCol, { + segment: (text) => this.segment(text, "word"), + isAtomicSegment: isPasteMarker, + }), + ); + } + + /** + * Yank (paste) the most recent kill ring entry at cursor position. + */ + private yank(): void { + if (this.killRing.length === 0) return; + + this.pushUndoSnapshot(); + + const text = this.killRing.peek()!; + this.insertYankedText(text); + + this.lastAction = "yank"; + } + + /** + * Cycle through kill ring (only works immediately after yank or yank-pop). + * Replaces the last yanked text with the previous entry in the ring. + */ + private yankPop(): void { + // Only works if we just yanked and have more than one entry + if (this.lastAction !== "yank" || this.killRing.length <= 1) return; + + this.pushUndoSnapshot(); + + // Delete the previously yanked text (still at end of ring before rotation) + this.deleteYankedText(); + + // Rotate the ring: move end to front + this.killRing.rotate(); + + // Insert the new most recent entry (now at end after rotation) + const text = this.killRing.peek()!; + this.insertYankedText(text); + + this.lastAction = "yank"; + } + + /** + * Insert text at cursor position (used by yank operations). + */ + private insertYankedText(text: string): void { + this.exitHistoryBrowsing(); + const lines = text.split("\n"); + + if (lines.length === 1) { + // Single line - insert at cursor + const currentLine = this.state.lines[this.state.cursorLine] || ""; + const before = currentLine.slice(0, this.state.cursorCol); + const after = currentLine.slice(this.state.cursorCol); + this.state.lines[this.state.cursorLine] = before + text + after; + this.setCursorCol(this.state.cursorCol + text.length); + } else { + // Multi-line insert + const currentLine = this.state.lines[this.state.cursorLine] || ""; + const before = currentLine.slice(0, this.state.cursorCol); + const after = currentLine.slice(this.state.cursorCol); + + // First line merges with text before cursor + this.state.lines[this.state.cursorLine] = before + (lines[0] || ""); + + // Insert middle lines + for (let i = 1; i < lines.length - 1; i++) { + this.state.lines.splice(this.state.cursorLine + i, 0, lines[i] || ""); + } + + // Last line merges with text after cursor + const lastLineIndex = this.state.cursorLine + lines.length - 1; + this.state.lines.splice(lastLineIndex, 0, (lines[lines.length - 1] || "") + after); + + // Update cursor position + this.state.cursorLine = lastLineIndex; + this.setCursorCol((lines[lines.length - 1] || "").length); + } + + if (this.onChange) { + this.onChange(this.getText()); + } + } + + /** + * Delete the previously yanked text (used by yank-pop). + * The yanked text is derived from killRing[end] since it hasn't been rotated yet. + */ + private deleteYankedText(): void { + const yankedText = this.killRing.peek(); + if (!yankedText) return; + + const yankLines = yankedText.split("\n"); + + if (yankLines.length === 1) { + // Single line - delete backward from cursor + const currentLine = this.state.lines[this.state.cursorLine] || ""; + const deleteLen = yankedText.length; + const before = currentLine.slice(0, this.state.cursorCol - deleteLen); + const after = currentLine.slice(this.state.cursorCol); + this.state.lines[this.state.cursorLine] = before + after; + this.setCursorCol(this.state.cursorCol - deleteLen); + } else { + // Multi-line delete - cursor is at end of last yanked line + const startLine = this.state.cursorLine - (yankLines.length - 1); + const startCol = (this.state.lines[startLine] || "").length - (yankLines[0] || "").length; + + // Get text after cursor on current line + const afterCursor = (this.state.lines[this.state.cursorLine] || "").slice(this.state.cursorCol); + + // Get text before yank start position + const beforeYank = (this.state.lines[startLine] || "").slice(0, startCol); + + // Remove all lines from startLine to cursorLine and replace with merged line + this.state.lines.splice(startLine, yankLines.length, beforeYank + afterCursor); + + // Update cursor + this.state.cursorLine = startLine; + this.setCursorCol(startCol); + } + + if (this.onChange) { + this.onChange(this.getText()); + } + } + + private pushUndoSnapshot(): void { + this.undoStack.push({ state: this.state, pastes: this.pastes, pasteCounter: this.pasteCounter }); + } + + private undo(): void { + this.exitHistoryBrowsing(); + const snapshot = this.undoStack.pop(); + if (!snapshot) return; + Object.assign(this.state, snapshot.state); + this.pastes = snapshot.pastes; + this.pasteCounter = snapshot.pasteCounter; + this.lastAction = null; + this.preferredVisualCol = null; + if (this.onChange) { + this.onChange(this.getText()); + } + } + + /** + * Jump to the first occurrence of a character in the specified direction. + * Multi-line search. Case-sensitive. Skips the current cursor position. + */ + private jumpToChar(char: string, direction: "forward" | "backward"): void { + this.lastAction = null; + const isForward = direction === "forward"; + const lines = this.state.lines; + + const end = isForward ? lines.length : -1; + const step = isForward ? 1 : -1; + + for (let lineIdx = this.state.cursorLine; lineIdx !== end; lineIdx += step) { + const line = lines[lineIdx] || ""; + const isCurrentLine = lineIdx === this.state.cursorLine; + + // Current line: start after/before cursor; other lines: search full line + const searchFrom = isCurrentLine + ? isForward + ? this.state.cursorCol + 1 + : this.state.cursorCol - 1 + : undefined; + + const idx = isForward ? line.indexOf(char, searchFrom) : line.lastIndexOf(char, searchFrom); + + if (idx !== -1) { + this.state.cursorLine = lineIdx; + this.setCursorCol(idx); + return; + } + } + // No match found - cursor stays in place + } + + private moveWordForwards(): void { + this.lastAction = null; + const currentLine = this.state.lines[this.state.cursorLine] || ""; + + // If at end of line, move to start of next line + if (this.state.cursorCol >= currentLine.length) { + if (this.state.cursorLine < this.state.lines.length - 1) { + this.state.cursorLine++; + this.setCursorCol(0); + } + return; + } + + this.setCursorCol( + findWordForward(currentLine, this.state.cursorCol, { + segment: (text) => this.segment(text, "word"), + isAtomicSegment: isPasteMarker, + }), + ); + } + + // Slash menu only allowed on the first line of the editor + private isSlashMenuAllowed(): boolean { + return this.state.cursorLine === 0; + } + + // Helper method to check if cursor is at start of message (for slash command detection) + private isAtStartOfMessage(): boolean { + if (!this.isSlashMenuAllowed()) return false; + const currentLine = this.state.lines[this.state.cursorLine] || ""; + const beforeCursor = currentLine.slice(0, this.state.cursorCol); + return beforeCursor.trim() === "" || beforeCursor.trim() === "/"; + } + + private isInSlashCommandContext(textBeforeCursor: string): boolean { + return this.isSlashMenuAllowed() && textBeforeCursor.trimStart().startsWith("/"); + } + + // Autocomplete methods + /** + * Find the best autocomplete item index for the given prefix. + * Returns -1 if no match is found. + * + * Match priority: + * 1. Exact match (prefix === item.value) -> always selected + * 2. Prefix match -> first item whose value starts with prefix + * 3. No match -> -1 (keep default highlight) + * + * Matching is case-sensitive and checks item.value only. + */ + private getBestAutocompleteMatchIndex(items: Array<{ value: string; label: string }>, prefix: string): number { + if (!prefix) return -1; + + let firstPrefixIndex = -1; + + for (let i = 0; i < items.length; i++) { + const value = items[i]!.value; + if (value === prefix) { + return i; // Exact match always wins + } + if (firstPrefixIndex === -1 && value.startsWith(prefix)) { + firstPrefixIndex = i; + } + } + + return firstPrefixIndex; + } + + private createAutocompleteList( + prefix: string, + items: Array<{ value: string; label: string; description?: string }>, + ): SelectList { + const layout = prefix.startsWith("/") ? SLASH_COMMAND_SELECT_LIST_LAYOUT : undefined; + const list = new SelectList(items, this.autocompleteMaxVisible, this.theme.selectList, layout); + list.onSelect = (selected) => { + if (!this.autocompleteProvider) return; + this.pushUndoSnapshot(); + this.lastAction = null; + const result = this.autocompleteProvider.applyCompletion( + this.state.lines, + this.state.cursorLine, + this.state.cursorCol, + selected, + this.autocompletePrefix, + ); + this.state.lines = result.lines; + this.state.cursorLine = result.cursorLine; + this.setCursorCol(result.cursorCol); + this.cancelAutocomplete(); + this.onChange?.(this.getText()); + }; + return list; + } + + private tryTriggerAutocomplete(explicitTab: boolean = false): void { + this.requestAutocomplete({ force: false, explicitTab }); + } + + private handleTabCompletion(): void { + if (!this.autocompleteProvider) return; + + const currentLine = this.state.lines[this.state.cursorLine] || ""; + const beforeCursor = currentLine.slice(0, this.state.cursorCol); + + if (this.isInSlashCommandContext(beforeCursor) && !beforeCursor.trimStart().includes(" ")) { + this.handleSlashCommandCompletion(); + } else { + this.forceFileAutocomplete(true); + } + } + + private handleSlashCommandCompletion(): void { + this.requestAutocomplete({ force: false, explicitTab: true }); + } + + private forceFileAutocomplete(explicitTab: boolean = false): void { + this.requestAutocomplete({ force: true, explicitTab }); + } + + private requestAutocomplete(options: { force: boolean; explicitTab: boolean }): void { + if (!this.autocompleteProvider) return; + + if (options.force) { + const shouldTrigger = + !this.autocompleteProvider.shouldTriggerFileCompletion || + this.autocompleteProvider.shouldTriggerFileCompletion( + this.state.lines, + this.state.cursorLine, + this.state.cursorCol, + ); + if (!shouldTrigger) { + return; + } + } + + this.cancelAutocompleteRequest(); + const startToken = ++this.autocompleteStartToken; + + const debounceMs = this.getAutocompleteDebounceMs(options); + if (debounceMs > 0) { + this.autocompleteDebounceTimer = setTimeout(() => { + this.autocompleteDebounceTimer = undefined; + void this.startAutocompleteRequest(startToken, options); + }, debounceMs); + return; + } + + void this.startAutocompleteRequest(startToken, options); + } + + private async startAutocompleteRequest( + startToken: number, + options: { force: boolean; explicitTab: boolean }, + ): Promise { + const previousTask = this.autocompleteRequestTask; + this.autocompleteRequestTask = (async () => { + await previousTask; + if (startToken !== this.autocompleteStartToken || !this.autocompleteProvider) { + return; + } + + const controller = new AbortController(); + this.autocompleteAbort = controller; + const requestId = ++this.autocompleteRequestId; + const snapshotText = this.getText(); + const snapshotLine = this.state.cursorLine; + const snapshotCol = this.state.cursorCol; + + await this.runAutocompleteRequest(requestId, controller, snapshotText, snapshotLine, snapshotCol, options); + })(); + await this.autocompleteRequestTask; + } + + private setAutocompleteTriggerCharacters(triggerCharacters: string[]): void { + const next = [...DEFAULT_AUTOCOMPLETE_TRIGGER_CHARACTERS]; + for (const character of triggerCharacters) { + if (character.length !== 1 || character === "/" || isWhitespaceChar(character) || next.includes(character)) { + continue; + } + next.push(character); + } + this.autocompleteTriggerCharacters = next; + this.autocompleteTriggerPattern = buildTriggerPattern(next); + this.autocompleteDebouncePattern = buildDebouncePattern(next); + } + + private getAutocompleteDebounceMs(options: { force: boolean; explicitTab: boolean }): number { + if (options.explicitTab || options.force) { + return 0; + } + + const currentLine = this.state.lines[this.state.cursorLine] || ""; + const textBeforeCursor = currentLine.slice(0, this.state.cursorCol); + return this.autocompleteDebouncePattern.test(textBeforeCursor) ? ATTACHMENT_AUTOCOMPLETE_DEBOUNCE_MS : 0; + } + + private async runAutocompleteRequest( + requestId: number, + controller: AbortController, + snapshotText: string, + snapshotLine: number, + snapshotCol: number, + options: { force: boolean; explicitTab: boolean }, + ): Promise { + if (!this.autocompleteProvider) return; + + const suggestions = await this.autocompleteProvider.getSuggestions( + this.state.lines, + this.state.cursorLine, + this.state.cursorCol, + { signal: controller.signal, force: options.force }, + ); + + if (!this.isAutocompleteRequestCurrent(requestId, controller, snapshotText, snapshotLine, snapshotCol)) { + return; + } + + this.autocompleteAbort = undefined; + + if (!suggestions || !Array.isArray(suggestions.items) || suggestions.items.length === 0) { + this.cancelAutocomplete(); + this.tui.requestRender(); + return; + } + + if (options.force && options.explicitTab && suggestions.items.length === 1) { + const item = suggestions.items[0]!; + this.pushUndoSnapshot(); + this.lastAction = null; + const result = this.autocompleteProvider.applyCompletion( + this.state.lines, + this.state.cursorLine, + this.state.cursorCol, + item, + suggestions.prefix, + ); + this.state.lines = result.lines; + this.state.cursorLine = result.cursorLine; + this.setCursorCol(result.cursorCol); + if (this.onChange) this.onChange(this.getText()); + this.tui.requestRender(); + return; + } + + this.applyAutocompleteSuggestions(suggestions, options.force ? "force" : "regular"); + this.tui.requestRender(); + } + + private isAutocompleteRequestCurrent( + requestId: number, + controller: AbortController, + snapshotText: string, + snapshotLine: number, + snapshotCol: number, + ): boolean { + return ( + !controller.signal.aborted && + requestId === this.autocompleteRequestId && + this.getText() === snapshotText && + this.state.cursorLine === snapshotLine && + this.state.cursorCol === snapshotCol + ); + } + + private applyAutocompleteSuggestions(suggestions: AutocompleteSuggestions, state: "regular" | "force"): void { + this.autocompletePrefix = suggestions.prefix; + this.autocompleteList = this.createAutocompleteList(suggestions.prefix, suggestions.items); + + const bestMatchIndex = this.getBestAutocompleteMatchIndex(suggestions.items, suggestions.prefix); + if (bestMatchIndex >= 0) { + this.autocompleteList.setSelectedIndex(bestMatchIndex); + } + + this.autocompleteState = state; + } + + private cancelAutocompleteRequest(): void { + this.autocompleteStartToken += 1; + if (this.autocompleteDebounceTimer) { + clearTimeout(this.autocompleteDebounceTimer); + this.autocompleteDebounceTimer = undefined; + } + this.autocompleteAbort?.abort(); + this.autocompleteAbort = undefined; + } + + private clearAutocompleteUi(): void { + this.autocompleteState = null; + this.autocompleteList = undefined; + this.autocompletePrefix = ""; + } + + private cancelAutocomplete(): void { + this.cancelAutocompleteRequest(); + this.clearAutocompleteUi(); + } + + public isShowingAutocomplete(): boolean { + return this.autocompleteState !== null; + } + + private updateAutocomplete(): void { + if (!this.autocompleteState || !this.autocompleteProvider) return; + this.requestAutocomplete({ force: this.autocompleteState === "force", explicitTab: false }); + } +} diff --git a/packages/tui/src/components/h-stack.ts b/packages/tui/src/components/h-stack.ts new file mode 100644 index 0000000000000000000000000000000000000000..07f8f8ffd055789d69266152f62ee97390bacf01 --- /dev/null +++ b/packages/tui/src/components/h-stack.ts @@ -0,0 +1,44 @@ +import { compositeTuiLine } from "../tui.ts"; +import { visibleWidth } from "../utils.ts"; +import { allocateStackSizes, Stack, type StackChild, type StackOptions, visibleStackEntries } from "./stack.ts"; + +export class HStack extends Stack { + protected readonly layoutType = "hstack" as const; + + constructor(children: StackChild[] = [], options: StackOptions = {}) { + super(children, options); + } + + override render(width: number): string[] { + const safeWidth = Math.max(1, width); + const viewport = { width: safeWidth, height: Number.MAX_SAFE_INTEGER }; + const entries = visibleStackEntries(this.entries, viewport); + if (entries.length === 0) return []; + + const intrinsicWidths = entries.map((entry) => { + const lines = entry.component.render(safeWidth); + return lines.reduce((max, line) => Math.max(max, visibleWidth(line)), 0); + }); + const widths = allocateStackSizes(entries, intrinsicWidths, safeWidth, this.gap); + const rendered = entries.map((entry, index) => + widths[index] === 0 ? [] : entry.component.render(widths[index]!), + ); + const height = rendered.reduce((max, lines) => Math.max(max, lines.length), 0); + const result = Array.from({ length: height }, () => ""); + let x = 0; + for (let index = 0; index < rendered.length; index++) { + const lines = rendered[index]!; + const childWidth = widths[index]!; + let offset = 0; + if (this.align === "center") offset = Math.floor((height - lines.length) / 2); + else if (this.align === "end") offset = height - lines.length; + for (let row = 0; row < lines.length; row++) { + const target = row + offset; + if (target < 0 || target >= result.length) continue; + result[target] = compositeTuiLine(result[target]!, lines[row]!, x, childWidth, safeWidth); + } + x += childWidth + this.gap; + } + return result; + } +} diff --git a/packages/tui/src/components/image.ts b/packages/tui/src/components/image.ts new file mode 100644 index 0000000000000000000000000000000000000000..91d68016aae5d292e22a3e892631b7ed53b13e64 --- /dev/null +++ b/packages/tui/src/components/image.ts @@ -0,0 +1,127 @@ +import { + allocateImageId, + getCapabilities, + getCellDimensions, + getImageDimensions, + type ImageDimensions, + imageFallback, + renderImage, +} from "../terminal-image.ts"; +import type { Component } from "../tui.ts"; +import { truncateToWidth } from "../utils.ts"; + +export interface ImageTheme { + fallbackColor: (str: string) => string; +} + +export interface ImageOptions { + maxWidthCells?: number; + maxHeightCells?: number; + filename?: string; + /** Kitty image ID. If provided, reuses this ID (for animations/updates). */ + imageId?: number; +} + +export class Image implements Component { + private base64Data: string; + private mimeType: string; + private dimensions: ImageDimensions; + private theme: ImageTheme; + private options: ImageOptions; + private imageId?: number; + + private cachedLines?: string[]; + private cachedWidth?: number; + + constructor( + base64Data: string, + mimeType: string, + theme: ImageTheme, + options: ImageOptions = {}, + dimensions?: ImageDimensions, + ) { + this.base64Data = base64Data; + this.mimeType = mimeType; + this.theme = theme; + this.options = options; + this.dimensions = dimensions || getImageDimensions(base64Data, mimeType) || { widthPx: 800, heightPx: 600 }; + this.imageId = options.imageId; + } + + /** Get the Kitty image ID used by this image (if any). */ + getImageId(): number | undefined { + return this.imageId; + } + + invalidate(): void { + this.cachedLines = undefined; + this.cachedWidth = undefined; + } + + render(width: number): string[] { + if (this.cachedLines && this.cachedWidth === width) { + return this.cachedLines; + } + + const maxWidth = Math.max(1, Math.min(width - 2, this.options.maxWidthCells ?? 60)); + const cellDimensions = getCellDimensions(); + const defaultMaxHeight = Math.max(1, Math.ceil((maxWidth * cellDimensions.widthPx) / cellDimensions.heightPx)); + const maxHeight = this.options.maxHeightCells ?? defaultMaxHeight; + + const caps = getCapabilities(); + let lines: string[]; + + if (caps.images) { + if (caps.images === "kitty" && this.imageId === undefined) { + this.imageId = allocateImageId(); + } + const result = renderImage(this.base64Data, this.dimensions, { + maxWidthCells: maxWidth, + maxHeightCells: maxHeight, + imageId: this.imageId, + moveCursor: false, + }); + + if (result) { + // Store the image ID for later cleanup + if (result.imageId) { + this.imageId = result.imageId; + } + + if (caps.images === "kitty") { + // For Kitty: C=1 prevents cursor movement. + // Don't need the cursor movement. + lines = [result.sequence]; + + // Return `rows` lines so TUI accounts for image height. + for (let i = 0; i < result.rows - 1; i++) { + lines.push(""); + } + } else { + // Return `rows` lines so TUI accounts for image height. + // First (rows-1) lines are empty and cleared before the image is drawn. + // Last line: move cursor back up, draw the image, then move back down + // so TUI cursor accounting stays inside the scroll area. + lines = []; + for (let i = 0; i < result.rows - 1; i++) { + lines.push(""); + } + const rowOffset = result.rows - 1; + const moveUp = rowOffset > 0 ? `\x1b[${rowOffset}A` : ""; + lines.push(moveUp + result.sequence); + } + } else { + const fallback = imageFallback(this.mimeType, this.dimensions, this.options.filename); + lines = [truncateToWidth(this.theme.fallbackColor(fallback), width)]; + } + } else { + const fallback = imageFallback(this.mimeType, this.dimensions, this.options.filename); + lines = [truncateToWidth(this.theme.fallbackColor(fallback), width)]; + } + + this.cachedLines = lines; + this.cachedWidth = width; + + return lines; + } +} diff --git a/packages/tui/src/components/input.ts b/packages/tui/src/components/input.ts new file mode 100644 index 0000000000000000000000000000000000000000..1bc26453098639991c22515cc94fee89b53f289d --- /dev/null +++ b/packages/tui/src/components/input.ts @@ -0,0 +1,494 @@ +import { getKeybindings } from "../keybindings.ts"; +import { decodeKittyPrintable } from "../keys.ts"; +import { KillRing } from "../kill-ring.ts"; +import { type Component, CURSOR_MARKER, type Focusable, type TuiMouseEvent, type TuiMouseEventResult } from "../tui.ts"; +import { UndoStack } from "../undo-stack.ts"; +import { getGraphemeSegmenter, isWhitespaceChar, sliceByColumn, truncateToWidth, visibleWidth } from "../utils.ts"; +import { findWordBackward, findWordForward } from "../word-navigation.ts"; + +const segmenter = getGraphemeSegmenter(); + +interface InputState { + value: string; + cursor: number; +} + +export interface InputOptions { + prompt?: string; + placeholder?: string; + placeholderStyle?: (text: string) => string; +} + +/** + * Input component - single-line text input with horizontal scrolling + */ +export class Input implements Component, Focusable { + private value: string = ""; + private cursor: number = 0; // Cursor position in the value + private readonly prompt: string; + private readonly placeholder: string; + private readonly placeholderStyle: (text: string) => string; + private renderedStartColumn = 0; + public onSubmit?: (value: string) => void; + public onEscape?: () => void; + + /** Focusable interface - set by TUI when focus changes */ + focused: boolean = false; + + // Bracketed paste mode buffering + private pasteBuffer: string = ""; + private isInPaste: boolean = false; + + // Kill ring for Emacs-style kill/yank operations + private killRing = new KillRing(); + private lastAction: "kill" | "yank" | "type-word" | null = null; + + // Undo support + private undoStack = new UndoStack(); + + constructor(options: InputOptions = {}) { + this.prompt = options.prompt ?? "> "; + this.placeholder = options.placeholder ?? ""; + this.placeholderStyle = options.placeholderStyle ?? ((text) => text); + } + + getValue(): string { + return this.value; + } + + setValue(value: string): void { + this.value = value; + this.cursor = Math.min(this.cursor, value.length); + } + + handleInput(data: string): void { + // Handle bracketed paste mode + // Start of paste: \x1b[200~ + // End of paste: \x1b[201~ + + // Check if we're starting a bracketed paste + if (data.includes("\x1b[200~")) { + this.isInPaste = true; + this.pasteBuffer = ""; + data = data.replace("\x1b[200~", ""); + } + + // If we're in a paste, buffer the data + if (this.isInPaste) { + // Check if this chunk contains the end marker + this.pasteBuffer += data; + + const endIndex = this.pasteBuffer.indexOf("\x1b[201~"); + if (endIndex !== -1) { + // Extract the pasted content + const pasteContent = this.pasteBuffer.substring(0, endIndex); + + // Process the complete paste + this.handlePaste(pasteContent); + + // Reset paste state + this.isInPaste = false; + + // Handle any remaining input after the paste marker + const remaining = this.pasteBuffer.substring(endIndex + 6); // 6 = length of \x1b[201~ + this.pasteBuffer = ""; + if (remaining) { + this.handleInput(remaining); + } + } + return; + } + + const kb = getKeybindings(); + + // Escape/Cancel + if (kb.matches(data, "tui.select.cancel")) { + if (this.onEscape) this.onEscape(); + return; + } + + // Undo + if (kb.matches(data, "tui.editor.undo")) { + this.undo(); + return; + } + + // Submit + if (kb.matches(data, "tui.input.submit") || data === "\n") { + if (this.onSubmit) this.onSubmit(this.value); + return; + } + + // Deletion + if (kb.matches(data, "tui.editor.deleteCharBackward")) { + this.handleBackspace(); + return; + } + + if (kb.matches(data, "tui.editor.deleteCharForward")) { + this.handleForwardDelete(); + return; + } + + if (kb.matches(data, "tui.editor.deleteWordBackward")) { + this.deleteWordBackwards(); + return; + } + + if (kb.matches(data, "tui.editor.deleteWordForward")) { + this.deleteWordForward(); + return; + } + + if (kb.matches(data, "tui.editor.deleteToLineStart")) { + this.deleteToLineStart(); + return; + } + + if (kb.matches(data, "tui.editor.deleteToLineEnd")) { + this.deleteToLineEnd(); + return; + } + + // Kill ring actions + if (kb.matches(data, "tui.editor.yank")) { + this.yank(); + return; + } + if (kb.matches(data, "tui.editor.yankPop")) { + this.yankPop(); + return; + } + + // Cursor movement + if (kb.matches(data, "tui.editor.cursorLeft")) { + this.lastAction = null; + if (this.cursor > 0) { + const beforeCursor = this.value.slice(0, this.cursor); + const graphemes = [...segmenter.segment(beforeCursor)]; + const lastGrapheme = graphemes[graphemes.length - 1]; + this.cursor -= lastGrapheme ? lastGrapheme.segment.length : 1; + } + return; + } + + if (kb.matches(data, "tui.editor.cursorRight")) { + this.lastAction = null; + if (this.cursor < this.value.length) { + const afterCursor = this.value.slice(this.cursor); + const graphemes = [...segmenter.segment(afterCursor)]; + const firstGrapheme = graphemes[0]; + this.cursor += firstGrapheme ? firstGrapheme.segment.length : 1; + } + return; + } + + if (kb.matches(data, "tui.editor.cursorLineStart")) { + this.lastAction = null; + this.cursor = 0; + return; + } + + if (kb.matches(data, "tui.editor.cursorLineEnd")) { + this.lastAction = null; + this.cursor = this.value.length; + return; + } + + if (kb.matches(data, "tui.editor.cursorWordLeft")) { + this.moveWordBackwards(); + return; + } + + if (kb.matches(data, "tui.editor.cursorWordRight")) { + this.moveWordForwards(); + return; + } + + // Kitty CSI-u printable character (e.g. \x1b[97u for 'a'). + // Terminals with Kitty protocol flag 1 (disambiguate) send CSI-u for all keys, + // including plain printable characters. Decode before the control-char check + // since CSI-u sequences contain \x1b which would be rejected. + const kittyPrintable = decodeKittyPrintable(data); + if (kittyPrintable !== undefined) { + this.insertCharacter(kittyPrintable); + return; + } + + // Regular character input - accept printable characters including Unicode, + // but reject control characters (C0: 0x00-0x1F, DEL: 0x7F, C1: 0x80-0x9F) + const hasControlChars = [...data].some((ch) => { + const code = ch.charCodeAt(0); + return code < 32 || code === 0x7f || (code >= 0x80 && code <= 0x9f); + }); + if (!hasControlChars) { + this.insertCharacter(data); + } + } + + handleMouse(event: TuiMouseEvent): TuiMouseEventResult | undefined { + if (event.type !== "press" || event.button !== "left" || event.y !== 0) return undefined; + const visibleColumn = Math.max(0, event.x - 2); + const targetColumn = this.renderedStartColumn + visibleColumn; + let currentColumn = 0; + this.cursor = this.value.length; + for (const grapheme of segmenter.segment(this.value)) { + const nextColumn = currentColumn + visibleWidth(grapheme.segment); + if (targetColumn < nextColumn) { + this.cursor = grapheme.index; + break; + } + currentColumn = nextColumn; + } + this.lastAction = null; + return { handled: true, focus: true }; + } + + private insertCharacter(char: string): void { + // Undo coalescing: consecutive word chars coalesce into one undo unit + if (isWhitespaceChar(char) || this.lastAction !== "type-word") { + this.pushUndo(); + } + this.lastAction = "type-word"; + + this.value = this.value.slice(0, this.cursor) + char + this.value.slice(this.cursor); + this.cursor += char.length; + } + + private handleBackspace(): void { + this.lastAction = null; + if (this.cursor > 0) { + this.pushUndo(); + const beforeCursor = this.value.slice(0, this.cursor); + const graphemes = [...segmenter.segment(beforeCursor)]; + const lastGrapheme = graphemes[graphemes.length - 1]; + const graphemeLength = lastGrapheme ? lastGrapheme.segment.length : 1; + this.value = this.value.slice(0, this.cursor - graphemeLength) + this.value.slice(this.cursor); + this.cursor -= graphemeLength; + } + } + + private handleForwardDelete(): void { + this.lastAction = null; + if (this.cursor < this.value.length) { + this.pushUndo(); + const afterCursor = this.value.slice(this.cursor); + const graphemes = [...segmenter.segment(afterCursor)]; + const firstGrapheme = graphemes[0]; + const graphemeLength = firstGrapheme ? firstGrapheme.segment.length : 1; + this.value = this.value.slice(0, this.cursor) + this.value.slice(this.cursor + graphemeLength); + } + } + + private deleteToLineStart(): void { + if (this.cursor === 0) return; + this.pushUndo(); + const deletedText = this.value.slice(0, this.cursor); + this.killRing.push(deletedText, { prepend: true, accumulate: this.lastAction === "kill" }); + this.lastAction = "kill"; + this.value = this.value.slice(this.cursor); + this.cursor = 0; + } + + private deleteToLineEnd(): void { + if (this.cursor >= this.value.length) return; + this.pushUndo(); + const deletedText = this.value.slice(this.cursor); + this.killRing.push(deletedText, { prepend: false, accumulate: this.lastAction === "kill" }); + this.lastAction = "kill"; + this.value = this.value.slice(0, this.cursor); + } + + private deleteWordBackwards(): void { + if (this.cursor === 0) return; + + // Save lastAction before cursor movement (moveWordBackwards resets it) + const wasKill = this.lastAction === "kill"; + + this.pushUndo(); + + const oldCursor = this.cursor; + this.moveWordBackwards(); + const deleteFrom = this.cursor; + this.cursor = oldCursor; + + const deletedText = this.value.slice(deleteFrom, this.cursor); + this.killRing.push(deletedText, { prepend: true, accumulate: wasKill }); + this.lastAction = "kill"; + + this.value = this.value.slice(0, deleteFrom) + this.value.slice(this.cursor); + this.cursor = deleteFrom; + } + + private deleteWordForward(): void { + if (this.cursor >= this.value.length) return; + + // Save lastAction before cursor movement (moveWordForwards resets it) + const wasKill = this.lastAction === "kill"; + + this.pushUndo(); + + const oldCursor = this.cursor; + this.moveWordForwards(); + const deleteTo = this.cursor; + this.cursor = oldCursor; + + const deletedText = this.value.slice(this.cursor, deleteTo); + this.killRing.push(deletedText, { prepend: false, accumulate: wasKill }); + this.lastAction = "kill"; + + this.value = this.value.slice(0, this.cursor) + this.value.slice(deleteTo); + } + + private yank(): void { + const text = this.killRing.peek(); + if (!text) return; + + this.pushUndo(); + + this.value = this.value.slice(0, this.cursor) + text + this.value.slice(this.cursor); + this.cursor += text.length; + this.lastAction = "yank"; + } + + private yankPop(): void { + if (this.lastAction !== "yank" || this.killRing.length <= 1) return; + + this.pushUndo(); + + // Delete the previously yanked text (still at end of ring before rotation) + const prevText = this.killRing.peek() || ""; + this.value = this.value.slice(0, this.cursor - prevText.length) + this.value.slice(this.cursor); + this.cursor -= prevText.length; + + // Rotate and insert new entry + this.killRing.rotate(); + const text = this.killRing.peek() || ""; + this.value = this.value.slice(0, this.cursor) + text + this.value.slice(this.cursor); + this.cursor += text.length; + this.lastAction = "yank"; + } + + private pushUndo(): void { + this.undoStack.push({ value: this.value, cursor: this.cursor }); + } + + private undo(): void { + const snapshot = this.undoStack.pop(); + if (!snapshot) return; + this.value = snapshot.value; + this.cursor = snapshot.cursor; + this.lastAction = null; + } + + private moveWordBackwards(): void { + if (this.cursor === 0) return; + this.lastAction = null; + this.cursor = findWordBackward(this.value, this.cursor); + } + + private moveWordForwards(): void { + if (this.cursor >= this.value.length) return; + this.lastAction = null; + this.cursor = findWordForward(this.value, this.cursor); + } + + private handlePaste(pastedText: string): void { + this.lastAction = null; + this.pushUndo(); + + // Clean the pasted text - remove newlines and carriage returns + const cleanText = pastedText.replace(/\r\n/g, "").replace(/\r/g, "").replace(/\n/g, "").replace(/\t/g, " "); + + // Insert at cursor position + this.value = this.value.slice(0, this.cursor) + cleanText + this.value.slice(this.cursor); + this.cursor += cleanText.length; + } + + invalidate(): void { + // No cached state to invalidate currently + } + + render(width: number): string[] { + // Calculate visible window + const availableWidth = width - visibleWidth(this.prompt); + + if (availableWidth <= 0) { + return [truncateToWidth(this.prompt, width, "")]; + } + + if (this.value.length === 0 && this.placeholder) { + const placeholder = truncateToWidth(this.placeholder, availableWidth, ""); + const graphemes = [...segmenter.segment(placeholder)]; + const atCursor = graphemes[0]?.segment ?? " "; + const afterCursor = placeholder.slice(atCursor.length); + const marker = this.focused ? CURSOR_MARKER : ""; + const cursorChar = `\x1b[7m${this.placeholderStyle(atCursor)}\x1b[27m`; + const textWithCursor = marker + cursorChar + this.placeholderStyle(afterCursor); + const padding = " ".repeat(Math.max(0, availableWidth - visibleWidth(textWithCursor))); + return [this.prompt + textWithCursor + padding]; + } + + let visibleText = ""; + let cursorDisplay = this.cursor; + this.renderedStartColumn = 0; + const totalWidth = visibleWidth(this.value); + + if (totalWidth < availableWidth) { + // Everything fits (leave room for cursor at end) + visibleText = this.value; + } else { + // Need horizontal scrolling + // Reserve one column for cursor if it's at the end + const scrollWidth = this.cursor === this.value.length ? availableWidth - 1 : availableWidth; + const cursorCol = visibleWidth(this.value.slice(0, this.cursor)); + + if (scrollWidth > 0) { + const halfWidth = Math.floor(scrollWidth / 2); + let startCol = 0; + + if (cursorCol < halfWidth) { + // Cursor near start + startCol = 0; + } else if (cursorCol > totalWidth - halfWidth) { + // Cursor near end + startCol = Math.max(0, totalWidth - scrollWidth); + } else { + // Cursor in middle + startCol = Math.max(0, cursorCol - halfWidth); + } + + this.renderedStartColumn = startCol; + visibleText = sliceByColumn(this.value, startCol, scrollWidth, true); + const beforeCursor = sliceByColumn(this.value, startCol, Math.max(0, cursorCol - startCol), true); + cursorDisplay = beforeCursor.length; + } else { + visibleText = ""; + cursorDisplay = 0; + } + } + + // Build line with fake cursor + // Insert cursor character at cursor position + const graphemes = [...segmenter.segment(visibleText.slice(cursorDisplay))]; + const cursorGrapheme = graphemes[0]; + + const beforeCursor = visibleText.slice(0, cursorDisplay); + const atCursor = cursorGrapheme?.segment ?? " "; // Character at cursor, or space if at end + const afterCursor = visibleText.slice(cursorDisplay + atCursor.length); + + // Hardware cursor marker (zero-width, emitted before fake cursor for IME positioning) + const marker = this.focused ? CURSOR_MARKER : ""; + + // Use inverse video to show cursor + const cursorChar = `\x1b[7m${atCursor}\x1b[27m`; // ESC[7m = reverse video, ESC[27m = normal + const textWithCursor = beforeCursor + marker + cursorChar + afterCursor; + + // Calculate visual width + const visualLength = visibleWidth(textWithCursor); + const padding = " ".repeat(Math.max(0, availableWidth - visualLength)); + const line = this.prompt + textWithCursor + padding; + + return [line]; + } +} diff --git a/packages/tui/src/components/loader.ts b/packages/tui/src/components/loader.ts new file mode 100644 index 0000000000000000000000000000000000000000..0c59e73a83bb4bd9978818b16bf556da53f48123 --- /dev/null +++ b/packages/tui/src/components/loader.ts @@ -0,0 +1,101 @@ +import type { TUI } from "../tui.ts"; +import { Text } from "./text.ts"; + +export interface LoaderIndicatorOptions { + /** Animation frames. Use an empty array to hide the indicator. */ + frames?: string[]; + /** Frame interval in milliseconds for animated indicators. */ + intervalMs?: number; +} + +const DEFAULT_FRAMES = ["⠋", "⠙", "⠹", "⠸", "⠼", "⠴", "⠦", "⠧", "⠇", "⠏"]; +const DEFAULT_INTERVAL_MS = 80; + +/** + * Loader component that updates with an optional spinning animation. + */ +export class Loader extends Text { + private frames = [...DEFAULT_FRAMES]; + private intervalMs = DEFAULT_INTERVAL_MS; + private currentFrame = 0; + private intervalId: NodeJS.Timeout | null = null; + private ui: TUI | null = null; + private renderIndicatorVerbatim = false; + private spinnerColorFn: (str: string) => string; + private messageColorFn: (str: string) => string; + private message: string = "Loading..."; + + constructor( + ui: TUI, + spinnerColorFn: (str: string) => string, + messageColorFn: (str: string) => string, + message: string = "Loading...", + indicator?: LoaderIndicatorOptions, + ) { + super("", 1, 0); + this.ui = ui; + this.spinnerColorFn = spinnerColorFn; + this.messageColorFn = messageColorFn; + this.message = message; + this.setIndicator(indicator); + } + + render(width: number): string[] { + return ["", ...super.render(width)]; + } + + start(): void { + this.updateDisplay(); + this.restartAnimation(); + } + + stop(): void { + if (this.intervalId) { + clearInterval(this.intervalId); + this.intervalId = null; + } + } + + setMessage(message: string): void { + this.message = message; + this.updateDisplay(); + } + + override invalidate(): void { + super.invalidate(); + this.updateDisplay(); + } + + setIndicator(indicator?: LoaderIndicatorOptions): void { + this.renderIndicatorVerbatim = indicator !== undefined; + this.frames = indicator?.frames !== undefined ? [...indicator.frames] : [...DEFAULT_FRAMES]; + this.intervalMs = indicator?.intervalMs && indicator.intervalMs > 0 ? indicator.intervalMs : DEFAULT_INTERVAL_MS; + this.currentFrame = 0; + this.start(); + } + + private restartAnimation(): void { + this.stop(); + if (this.frames.length <= 1) { + return; + } + this.intervalId = setInterval(() => { + this.currentFrame = (this.currentFrame + 1) % this.frames.length; + this.updateDisplay(); + }, this.intervalMs); + } + + protected getRenderedIndicator(): string { + const frame = this.frames[this.currentFrame] ?? ""; + return this.renderIndicatorVerbatim ? frame : this.spinnerColorFn(frame); + } + + private updateDisplay(): void { + const renderedFrame = this.getRenderedIndicator(); + const indicator = renderedFrame.length > 0 ? `${renderedFrame} ` : ""; + this.setText(`${indicator}${this.messageColorFn(this.message)}`); + if (this.ui) { + this.ui.requestRender(); + } + } +} diff --git a/packages/tui/src/components/markdown.ts b/packages/tui/src/components/markdown.ts new file mode 100644 index 0000000000000000000000000000000000000000..f66666ea9819bbb3fb99a575cef59558863fd6cf --- /dev/null +++ b/packages/tui/src/components/markdown.ts @@ -0,0 +1,1015 @@ +import { Marked, type Token, Tokenizer, type TokenizerExtension, type Tokens } from "marked"; +import { renderLatex } from "../latex.ts"; +import { getCapabilities, hyperlink, isImageLine } from "../terminal-image.ts"; +import type { Component } from "../tui.ts"; +import { applyBackgroundToLine, visibleWidth, wrapTextWithAnsi } from "../utils.ts"; + +const STRICT_STRIKETHROUGH_REGEX = /^(~~)(?=[^\s~])((?:\\.|[^\\])*?(?:\\.|[^\s~\\]))\1(?=[^~]|$)/; + +class StrictStrikethroughTokenizer extends Tokenizer { + override del(src: string): Tokens.Del | undefined { + const match = STRICT_STRIKETHROUGH_REGEX.exec(src); + if (!match) { + return undefined; + } + + const text = match[2]; + return { + type: "del", + raw: match[0], + text, + tokens: this.lexer.inlineTokens(text), + }; + } +} + +interface LatexToken extends Tokens.Generic { + type: "latex" | "latexBlock"; + text: string; + pending?: boolean; +} + +function isEscaped(source: string, index: number): boolean { + let backslashes = 0; + for (let position = index - 1; position >= 0 && source[position] === "\\"; position--) { + backslashes++; + } + return backslashes % 2 === 1; +} + +function findClosingDelimiter(source: string, closing: string, start: number): number { + let index = source.indexOf(closing, start); + while (index >= 0 && isEscaped(source, index)) { + index = source.indexOf(closing, index + closing.length); + } + return index; +} + +function looksLikePendingDollarMath(source: string): boolean { + return /\\[A-Za-z]+|[_^=+*/<>()[\]|±≤≥≠≈∈→⇒∞∫∑√-]/.test(source); +} + +function tokenizeInlineLatex(source: string): LatexToken | undefined { + let opening = ""; + let closing = ""; + if (source.startsWith("$$")) { + opening = "$$"; + closing = "$$"; + } else if (source.startsWith("\\(")) { + opening = "\\("; + closing = "\\)"; + } else if (source.startsWith("\\[")) { + opening = "\\["; + closing = "\\]"; + } else if (source.startsWith("$") && !/^\$\s/.test(source)) { + opening = "$"; + closing = "$"; + } else { + return undefined; + } + + const closingIndex = findClosingDelimiter(source, closing, opening.length); + if ( + closingIndex >= 0 && + opening === "$" && + (/\s$/.test(source.slice(opening.length, closingIndex)) || + /^\d/.test(source.slice(closingIndex + 1)) || + (/^[A-Z_][A-Z0-9_]*(?:[^A-Za-z0-9_\s])?$/.test(source.slice(opening.length, closingIndex)) && + /^[A-Za-z_][A-Za-z0-9_]*/.test(source.slice(closingIndex + 1))) || + source.slice(opening.length, closingIndex).includes("`")) + ) { + return undefined; + } + + if (closingIndex < 0) { + const pendingSource = source.slice(opening.length); + if (opening.startsWith("\\") || looksLikePendingDollarMath(pendingSource)) { + return { type: "latex", raw: source, text: pendingSource, pending: true }; + } + return undefined; + } + + const text = source.slice(opening.length, closingIndex); + if (!text || text.includes("\n")) { + return undefined; + } + + const raw = source.slice(0, closingIndex + closing.length); + return { type: "latex", raw, text }; +} + +function tokenizeBlockLatex(source: string): LatexToken | undefined { + const dollarMatch = /^ {0,3}\$\$[ \t]*(?:\n)?([\s\S]*?)\$\$[ \t]*(?:\n|$)/.exec(source); + if (dollarMatch?.[1]) { + return { type: "latexBlock", raw: dollarMatch[0], text: dollarMatch[1].trim() }; + } + + const bracketMatch = /^ {0,3}\\\[[ \t]*(?:\n)?([\s\S]*?)\\\][ \t]*(?:\n|$)/.exec(source); + if (bracketMatch?.[1]) { + return { type: "latexBlock", raw: bracketMatch[0], text: bracketMatch[1].trim() }; + } + + const pendingBracket = /^ {0,3}\\\[[ \t]*(?:\n)?([\s\S]*)$/.exec(source); + if (pendingBracket) { + return { type: "latexBlock", raw: pendingBracket[0], text: pendingBracket[1], pending: true }; + } + const pendingDollar = /^ {0,3}\$\$[ \t]*(?:\n)?([\s\S]*)$/.exec(source); + if (pendingDollar?.[1] && looksLikePendingDollarMath(pendingDollar[1])) { + return { type: "latexBlock", raw: pendingDollar[0], text: pendingDollar[1], pending: true }; + } + return undefined; +} + +const LATEX_MARKDOWN_EXTENSIONS: readonly TokenizerExtension[] = [ + { + name: "latexBlock", + level: "block", + start(source) { + const match = /(?:^|\n) {0,3}(?:\$\$|\\\[)/.exec(source); + return match ? match.index + (match[0].startsWith("\n") ? 1 : 0) : undefined; + }, + tokenizer: tokenizeBlockLatex, + }, + { + name: "latex", + level: "inline", + start(source) { + const indices = [source.indexOf("$"), source.indexOf("\\("), source.indexOf("\\[")].filter( + (index) => index >= 0, + ); + return indices.length > 0 ? Math.min(...indices) : undefined; + }, + tokenizer: tokenizeInlineLatex, + }, +]; + +function trimPartialClosingFences(tokens: readonly Token[]): void { + const token = tokens[tokens.length - 1]; + if (token?.type === "list") { + trimPartialClosingFences(token.items[token.items.length - 1]?.tokens ?? []); + return; + } + if (token?.type === "blockquote") { + trimPartialClosingFences(token.tokens ?? []); + return; + } + if (token?.type !== "code") { + return; + } + + // Trim streamed partial closing fences so code blocks do not shrink/flicker + // when the final fence character arrives. See https://github.com/earendil-works/pi/issues/5825. + const marker = /^(`{3,}|~{3,})/.exec(token.raw)?.[1]; + const lastLine = token.raw.split("\n").pop(); + if (!marker || !lastLine || lastLine.length >= marker.length || lastLine !== marker[0]?.repeat(lastLine.length)) { + return; + } + + token.text = token.text.slice(0, -lastLine.length).replace(/\n$/, ""); +} + +const markdownParser = new Marked(); +markdownParser.setOptions({ + tokenizer: new StrictStrikethroughTokenizer(), +}); +markdownParser.use({ extensions: [...LATEX_MARKDOWN_EXTENSIONS] }); + +/** + * Default text styling for markdown content. + * Applied to all text unless overridden by markdown formatting. + */ +export interface DefaultTextStyle { + /** Foreground color function */ + color?: (text: string) => string; + /** Background color function */ + bgColor?: (text: string) => string; + /** Bold text */ + bold?: boolean; + /** Italic text */ + italic?: boolean; + /** Strikethrough text */ + strikethrough?: boolean; + /** Underline text */ + underline?: boolean; +} + +/** + * Theme functions for markdown elements. + * Each function takes text and returns styled text with ANSI codes. + */ +export interface MarkdownTheme { + heading: (text: string) => string; + link: (text: string) => string; + linkUrl: (text: string) => string; + code: (text: string) => string; + codeBlock: (text: string) => string; + codeBlockBorder: (text: string) => string; + quote: (text: string) => string; + quoteBorder: (text: string) => string; + hr: (text: string) => string; + listBullet: (text: string) => string; + bold: (text: string) => string; + italic: (text: string) => string; + strikethrough: (text: string) => string; + underline: (text: string) => string; + highlightCode?: (code: string, lang?: string) => string[]; + /** Prefix applied to each rendered code block line (default: " ") */ + codeBlockIndent?: string; +} + +export interface MarkdownOptions { + /** Preserve source list markers instead of normalizing them. */ + preserveOrderedListMarkers?: boolean; + /** Preserve source backslash escapes instead of normalizing escaped punctuation. */ + preserveBackslashEscapes?: boolean; + /** Transform source Markdown before parsing, with the exact width available for content. */ + transform?: (markdown: string, availableWidth: number) => string; + /** Render supported LaTeX math expressions as Unicode text (default: true). */ + renderLatex?: boolean; +} + +interface InlineStyleContext { + applyText: (text: string) => string; + stylePrefix: string; +} + +export class Markdown implements Component { + private text: string; + private paddingX: number; // Left/right padding + private paddingY: number; // Top/bottom padding + private defaultTextStyle?: DefaultTextStyle; + private theme: MarkdownTheme; + private options: MarkdownOptions; + private defaultStylePrefix?: string; + + // Cache for rendered output + private cachedText?: string; + private cachedWidth?: number; + private cachedLines?: string[]; + + constructor( + text: string, + paddingX: number, + paddingY: number, + theme: MarkdownTheme, + defaultTextStyle?: DefaultTextStyle, + options?: MarkdownOptions, + ) { + this.text = text; + this.paddingX = paddingX; + this.paddingY = paddingY; + this.theme = theme; + this.defaultTextStyle = defaultTextStyle; + this.options = options ? { ...options } : {}; + } + + setText(text: string): void { + this.text = text; + this.invalidate(); + } + + invalidate(): void { + this.cachedText = undefined; + this.cachedWidth = undefined; + this.cachedLines = undefined; + } + + render(width: number): string[] { + // Check cache + if (this.cachedLines && this.cachedText === this.text && this.cachedWidth === width) { + return this.cachedLines; + } + + // Calculate available width for content (subtract horizontal padding) + const contentWidth = Math.max(1, width - this.paddingX * 2); + const text = this.options.transform?.(this.text, contentWidth) ?? this.text; + + // Don't render anything if there's no actual text + if (!text || text.trim() === "") { + const result: string[] = []; + // Update cache + this.cachedText = this.text; + this.cachedWidth = width; + this.cachedLines = result; + return result; + } + + // Replace tabs with 3 spaces for consistent rendering + const normalizedText = text.replace(/\t/g, " "); + + // Parse markdown to HTML-like tokens + const tokens = markdownParser.lexer(normalizedText); + trimPartialClosingFences(tokens); + + // Convert tokens to styled terminal output + const renderedLines: string[] = []; + + for (let i = 0; i < tokens.length; i++) { + const token = tokens[i]; + const nextToken = tokens[i + 1]; + const tokenLines = this.renderToken(token, contentWidth, nextToken?.type); + for (const tokenLine of tokenLines) { + renderedLines.push(tokenLine); + } + } + + // Wrap lines (NO padding, NO background yet) + const wrappedLines: string[] = []; + for (const line of renderedLines) { + if (isImageLine(line)) { + wrappedLines.push(line); + } else { + for (const wrappedLine of wrapTextWithAnsi(line, contentWidth)) { + wrappedLines.push(wrappedLine); + } + } + } + + // Add margins and background to each wrapped line + const leftMargin = " ".repeat(this.paddingX); + const rightMargin = " ".repeat(this.paddingX); + const bgFn = this.defaultTextStyle?.bgColor; + const contentLines: string[] = []; + + for (const line of wrappedLines) { + if (isImageLine(line)) { + contentLines.push(line); + continue; + } + + const lineWithMargins = leftMargin + line + rightMargin; + + if (bgFn) { + contentLines.push(applyBackgroundToLine(lineWithMargins, width, bgFn)); + } else { + // No background - just pad to width + const visibleLen = visibleWidth(lineWithMargins); + const paddingNeeded = Math.max(0, width - visibleLen); + contentLines.push(lineWithMargins + " ".repeat(paddingNeeded)); + } + } + + // Add top/bottom padding (empty lines) + const emptyLine = " ".repeat(width); + const emptyLines: string[] = []; + for (let i = 0; i < this.paddingY; i++) { + const line = bgFn ? applyBackgroundToLine(emptyLine, width, bgFn) : emptyLine; + emptyLines.push(line); + } + + // Combine top padding, content, and bottom padding + const result = emptyLines.concat(contentLines, emptyLines); + + // Update cache + this.cachedText = this.text; + this.cachedWidth = width; + this.cachedLines = result; + + return result.length > 0 ? result : [""]; + } + + /** + * Apply default text style to a string. + * This is the base styling applied to all text content. + * NOTE: Background color is NOT applied here - it's applied at the padding stage + * to ensure it extends to the full line width. + */ + private applyDefaultStyle(text: string): string { + if (!this.defaultTextStyle) { + return text; + } + + let styled = text; + + // Apply foreground color (NOT background - that's applied at padding stage) + if (this.defaultTextStyle.color) { + styled = this.defaultTextStyle.color(styled); + } + + // Apply text decorations using this.theme + if (this.defaultTextStyle.bold) { + styled = this.theme.bold(styled); + } + if (this.defaultTextStyle.italic) { + styled = this.theme.italic(styled); + } + if (this.defaultTextStyle.strikethrough) { + styled = this.theme.strikethrough(styled); + } + if (this.defaultTextStyle.underline) { + styled = this.theme.underline(styled); + } + + return styled; + } + + private getDefaultStylePrefix(): string { + if (!this.defaultTextStyle) { + return ""; + } + + if (this.defaultStylePrefix !== undefined) { + return this.defaultStylePrefix; + } + + const sentinel = "\u0000"; + let styled = sentinel; + + if (this.defaultTextStyle.color) { + styled = this.defaultTextStyle.color(styled); + } + + if (this.defaultTextStyle.bold) { + styled = this.theme.bold(styled); + } + if (this.defaultTextStyle.italic) { + styled = this.theme.italic(styled); + } + if (this.defaultTextStyle.strikethrough) { + styled = this.theme.strikethrough(styled); + } + if (this.defaultTextStyle.underline) { + styled = this.theme.underline(styled); + } + + const sentinelIndex = styled.indexOf(sentinel); + this.defaultStylePrefix = sentinelIndex >= 0 ? styled.slice(0, sentinelIndex) : ""; + return this.defaultStylePrefix; + } + + private getStylePrefix(styleFn: (text: string) => string): string { + const sentinel = "\u0000"; + const styled = styleFn(sentinel); + const sentinelIndex = styled.indexOf(sentinel); + return sentinelIndex >= 0 ? styled.slice(0, sentinelIndex) : ""; + } + + private getDefaultInlineStyleContext(): InlineStyleContext { + return { + applyText: (text: string) => this.applyDefaultStyle(text), + stylePrefix: this.getDefaultStylePrefix(), + }; + } + + private renderToken( + token: Token, + width: number, + nextTokenType?: string, + styleContext?: InlineStyleContext, + ): string[] { + const lines: string[] = []; + + switch (token.type) { + case "heading": { + const headingLevel = token.depth; + const headingPrefix = `${"#".repeat(headingLevel)} `; + + // Build a heading-specific style context so inline tokens (codespan, bold, etc.) + // restore heading styling after their own ANSI resets instead of falling back to + // the default text style. + let headingStyleFn: (text: string) => string; + if (headingLevel === 1) { + headingStyleFn = (text: string) => this.theme.heading(this.theme.bold(this.theme.underline(text))); + } else { + headingStyleFn = (text: string) => this.theme.heading(this.theme.bold(text)); + } + + const headingStyleContext: InlineStyleContext = { + applyText: headingStyleFn, + stylePrefix: this.getStylePrefix(headingStyleFn), + }; + + const headingText = this.renderInlineTokens(token.tokens || [], headingStyleContext); + const styledHeading = headingLevel >= 3 ? headingStyleFn(headingPrefix) + headingText : headingText; + lines.push(styledHeading); + if (nextTokenType && nextTokenType !== "space") { + lines.push(""); // Add spacing after headings (unless space token follows) + } + break; + } + + case "paragraph": { + const paragraphText = this.renderInlineTokens(token.tokens || [], styleContext); + lines.push(paragraphText); + // Don't add spacing if next token is space or list + if (nextTokenType && nextTokenType !== "list" && nextTokenType !== "space") { + lines.push(""); + } + break; + } + + case "text": + lines.push(this.renderInlineTokens([token], styleContext)); + break; + + case "latexBlock": { + const latexToken = token as LatexToken; + const rendered = + !latexToken.pending && this.options.renderLatex !== false + ? (renderLatex(latexToken.text, { display: true }) ?? latexToken.raw.trim()) + : latexToken.raw.trim(); + for (const line of rendered.split("\n")) { + lines.push(this.applyDefaultStyle(line)); + } + if (nextTokenType && nextTokenType !== "space") { + lines.push(""); + } + break; + } + + case "code": { + const indent = this.theme.codeBlockIndent ?? " "; + lines.push(this.theme.codeBlockBorder(`\`\`\`${token.lang || ""}`)); + if (this.theme.highlightCode) { + const highlightedLines = this.theme.highlightCode(token.text, token.lang); + for (const hlLine of highlightedLines) { + lines.push(`${indent}${hlLine}`); + } + } else { + // Split code by newlines and style each line + const codeLines = token.text.split("\n"); + for (const codeLine of codeLines) { + lines.push(`${indent}${this.theme.codeBlock(codeLine)}`); + } + } + lines.push(this.theme.codeBlockBorder("```")); + if (nextTokenType && nextTokenType !== "space") { + lines.push(""); // Add spacing after code blocks (unless space token follows) + } + break; + } + + case "list": { + const listLines = this.renderList(token as Tokens.List, 0, width, styleContext); + lines.push(...listLines); + // Don't add spacing after lists if a space token follows + // (the space token will handle it) + break; + } + + case "table": { + const tableLines = this.renderTable(token as Tokens.Table, width, nextTokenType, styleContext); + lines.push(...tableLines); + break; + } + + case "blockquote": { + const quoteStyle = (text: string) => this.theme.quote(this.theme.italic(text)); + const quoteStylePrefix = this.getStylePrefix(quoteStyle); + const applyQuoteStyle = (line: string): string => { + if (!quoteStylePrefix) { + return quoteStyle(line); + } + const lineWithReappliedStyle = line.replace(/\x1b\[0m/g, `\x1b[0m${quoteStylePrefix}`); + return quoteStyle(lineWithReappliedStyle); + }; + + // Calculate available width for quote content (subtract border "│ " = 2 chars) + const quoteContentWidth = Math.max(1, width - 2); + + // Blockquotes contain block-level tokens (paragraph, list, code, etc.), so render + // children with renderToken() instead of renderInlineTokens(). + // Default message style should not apply inside blockquotes. + const quoteInlineStyleContext: InlineStyleContext = { + applyText: (text: string) => text, + stylePrefix: quoteStylePrefix, + }; + const quoteTokens = token.tokens || []; + const renderedQuoteLines: string[] = []; + for (let i = 0; i < quoteTokens.length; i++) { + const quoteToken = quoteTokens[i]; + const nextQuoteToken = quoteTokens[i + 1]; + renderedQuoteLines.push( + ...this.renderToken(quoteToken, quoteContentWidth, nextQuoteToken?.type, quoteInlineStyleContext), + ); + } + + // Avoid rendering an extra empty quote line before the outer blockquote spacing. + while (renderedQuoteLines.length > 0 && renderedQuoteLines[renderedQuoteLines.length - 1] === "") { + renderedQuoteLines.pop(); + } + + for (const quoteLine of renderedQuoteLines) { + const styledLine = applyQuoteStyle(quoteLine); + const wrappedLines = wrapTextWithAnsi(styledLine, quoteContentWidth); + for (const wrappedLine of wrappedLines) { + lines.push(this.theme.quoteBorder("│ ") + wrappedLine); + } + } + if (nextTokenType && nextTokenType !== "space") { + lines.push(""); // Add spacing after blockquotes (unless space token follows) + } + break; + } + + case "hr": + lines.push(this.theme.hr("─".repeat(Math.min(width, 80)))); + if (nextTokenType && nextTokenType !== "space") { + lines.push(""); // Add spacing after horizontal rules (unless space token follows) + } + break; + + case "html": + // Render HTML as plain text (escaped for terminal) + if ("raw" in token && typeof token.raw === "string") { + lines.push(this.applyDefaultStyle(token.raw.trim())); + } + break; + + case "space": + // Space tokens represent blank lines in markdown + lines.push(""); + break; + + default: + // Handle any other token types as plain text + if ("text" in token && typeof token.text === "string") { + lines.push(token.text); + } + } + + return lines; + } + + private renderInlineTokens(tokens: Token[], styleContext?: InlineStyleContext): string { + let result = ""; + const resolvedStyleContext = styleContext ?? this.getDefaultInlineStyleContext(); + const { applyText, stylePrefix } = resolvedStyleContext; + const applyTextWithNewlines = (text: string): string => { + const segments: string[] = text.split("\n"); + return segments.map((segment: string) => applyText(segment)).join("\n"); + }; + + for (const token of tokens) { + switch (token.type) { + case "latex": { + const latexToken = token as LatexToken; + const rendered = + !latexToken.pending && this.options.renderLatex !== false + ? (renderLatex(latexToken.text) ?? latexToken.raw) + : latexToken.raw; + result += applyTextWithNewlines(rendered); + break; + } + + case "escape": + result += applyTextWithNewlines(this.options.preserveBackslashEscapes ? token.raw : token.text); + break; + + case "text": + // Text tokens in list items can have nested tokens for inline formatting + if (token.tokens && token.tokens.length > 0) { + result += this.renderInlineTokens(token.tokens, resolvedStyleContext); + } else { + result += applyTextWithNewlines(token.text); + } + break; + + case "paragraph": + // Paragraph tokens contain nested inline tokens + result += this.renderInlineTokens(token.tokens || [], resolvedStyleContext); + break; + + case "strong": { + const boldContent = this.renderInlineTokens(token.tokens || [], resolvedStyleContext); + result += this.theme.bold(boldContent) + stylePrefix; + break; + } + + case "em": { + const italicContent = this.renderInlineTokens(token.tokens || [], resolvedStyleContext); + result += this.theme.italic(italicContent) + stylePrefix; + break; + } + + case "codespan": + result += this.theme.code(token.text) + stylePrefix; + break; + + case "link": { + const linkText = this.renderInlineTokens(token.tokens || [], resolvedStyleContext); + const styledLink = this.theme.link(this.theme.underline(linkText)); + if (getCapabilities().hyperlinks) { + // OSC 8: render as a clickable hyperlink. The URL is not printed inline, + // so we always show only the link text regardless of whether it matches href. + result += hyperlink(styledLink, token.href) + stylePrefix; + } else { + // Fallback: print URL in parentheses when text differs from href. + // Compare raw token.text (not styled) against href for the equality check. + // For mailto: links strip the prefix (autolinked emails use text="foo@bar.com" + // but href="mailto:foo@bar.com"). + const hrefForComparison = token.href.startsWith("mailto:") ? token.href.slice(7) : token.href; + if (token.text === token.href || token.text === hrefForComparison) { + result += styledLink + stylePrefix; + } else { + result += styledLink + this.theme.linkUrl(` (${token.href})`) + stylePrefix; + } + } + break; + } + + case "br": + result += "\n"; + break; + + case "del": { + const delContent = this.renderInlineTokens(token.tokens || [], resolvedStyleContext); + result += this.theme.strikethrough(delContent) + stylePrefix; + break; + } + + case "html": + // Render inline HTML as plain text + if ("raw" in token && typeof token.raw === "string") { + result += applyTextWithNewlines(token.raw); + } + break; + + default: + // Handle any other inline token types as plain text + if ("text" in token && typeof token.text === "string") { + result += applyTextWithNewlines(token.text); + } + } + } + + while (stylePrefix && result.endsWith(stylePrefix)) { + result = result.slice(0, -stylePrefix.length); + } + + return result; + } + + private getOrderedListMarker(item: Tokens.ListItem): string | undefined { + const match = /^(?: {0,3})(\d{1,9}[.)])[ \t]+/.exec(item.raw); + return match ? `${match[1]} ` : undefined; + } + + private getUnorderedListMarker(item: Tokens.ListItem): string | undefined { + const match = /^(?: {0,3})([-+*])(?:[ \t]+|(?=\r?\n|$))/.exec(item.raw); + return match ? `${match[1]} ` : undefined; + } + + /** + * Render a list with proper nesting support + */ + private renderList(token: Tokens.List, depth: number, width: number, styleContext?: InlineStyleContext): string[] { + const lines: string[] = []; + const indent = " ".repeat(depth); + // Use the list's start property (defaults to 1 for ordered lists) + const startNumber = typeof token.start === "number" ? token.start : 1; + + for (let i = 0; i < token.items.length; i++) { + const item = token.items[i]; + const isLastItem = i === token.items.length - 1; + const bullet = token.ordered + ? this.options.preserveOrderedListMarkers + ? (this.getOrderedListMarker(item) ?? `${startNumber + i}. `) + : `${startNumber + i}. ` + : this.options.preserveOrderedListMarkers + ? (this.getUnorderedListMarker(item) ?? "- ") + : "- "; + const taskMarker = item.task ? `[${item.checked ? "x" : " "}] ` : ""; + const marker = bullet + taskMarker; + const firstPrefix = indent + this.theme.listBullet(marker); + const continuationPrefix = indent + " ".repeat(visibleWidth(marker)); + const itemWidth = Math.max(1, width - visibleWidth(firstPrefix)); + let renderedAnyLine = false; + + for (const itemToken of item.tokens) { + if (itemToken.type === "list") { + lines.push(...this.renderList(itemToken as Tokens.List, depth + 1, width, styleContext)); + renderedAnyLine = true; + continue; + } + + const itemLines = this.renderToken(itemToken, itemWidth, undefined, styleContext); + for (const line of itemLines) { + for (const wrappedLine of wrapTextWithAnsi(line, itemWidth)) { + const linePrefix = renderedAnyLine ? continuationPrefix : firstPrefix; + lines.push(linePrefix + wrappedLine); + renderedAnyLine = true; + } + } + } + + if (!renderedAnyLine) { + lines.push(firstPrefix); + } + + if (token.loose && !isLastItem) { + lines.push(""); + } + } + + return lines; + } + + /** + * Get the visible width of the longest word in a string. + */ + private getLongestWordWidth(text: string, maxWidth?: number): number { + const words = text.split(/\s+/).filter((word) => word.length > 0); + let longest = 0; + for (const word of words) { + longest = Math.max(longest, visibleWidth(word)); + } + if (maxWidth === undefined) { + return longest; + } + return Math.min(longest, maxWidth); + } + + /** + * Wrap a table cell to fit into a column. + * + * Delegates to wrapTextWithAnsi() so ANSI codes + long tokens are handled + * consistently with the rest of the renderer. + */ + private wrapCellText(text: string, maxWidth: number, stylePrefix = ""): string[] { + const lines = wrapTextWithAnsi(text, Math.max(1, maxWidth)); + return lines.map((line, index) => { + // Reset text styles after each non-final fragment, then restore the surrounding style before padding and borders. + const styleReset = index < lines.length - 1 ? "\x1b[22;23;24;25;27;28;29;39m" : ""; + return `${line}${styleReset}${stylePrefix}`; + }); + } + + /** + * Render a table with width-aware cell wrapping. + * Cells that don't fit are wrapped to multiple lines. + */ + private renderTable( + token: Tokens.Table, + availableWidth: number, + nextTokenType?: string, + styleContext?: InlineStyleContext, + ): string[] { + const lines: string[] = []; + const numCols = token.header.length; + + if (numCols === 0) { + return lines; + } + + // Calculate border overhead: "│ " + (n-1) * " │ " + " │" + // = 2 + (n-1) * 3 + 2 = 3n + 1 + const borderOverhead = 3 * numCols + 1; + const availableForCells = availableWidth - borderOverhead; + if (availableForCells < numCols) { + // Too narrow to render a stable table. Fall back to raw markdown. + const fallbackLines = token.raw ? wrapTextWithAnsi(token.raw, availableWidth) : []; + if (nextTokenType && nextTokenType !== "space") { + fallbackLines.push(""); + } + return fallbackLines; + } + + const maxUnbrokenWordWidth = 30; + + // Calculate natural column widths (what each column needs without constraints) + const naturalWidths: number[] = []; + const minWordWidths: number[] = []; + for (let i = 0; i < numCols; i++) { + const headerText = this.renderInlineTokens(token.header[i].tokens || [], styleContext); + naturalWidths[i] = visibleWidth(headerText); + minWordWidths[i] = Math.max(1, this.getLongestWordWidth(headerText, maxUnbrokenWordWidth)); + } + for (const row of token.rows) { + for (let i = 0; i < row.length; i++) { + const cellText = this.renderInlineTokens(row[i].tokens || [], styleContext); + naturalWidths[i] = Math.max(naturalWidths[i] || 0, visibleWidth(cellText)); + minWordWidths[i] = Math.max( + minWordWidths[i] || 1, + this.getLongestWordWidth(cellText, maxUnbrokenWordWidth), + ); + } + } + + let minColumnWidths = minWordWidths; + let minCellsWidth = minColumnWidths.reduce((a, b) => a + b, 0); + + if (minCellsWidth > availableForCells) { + minColumnWidths = new Array(numCols).fill(1); + const remaining = availableForCells - numCols; + + if (remaining > 0) { + const totalWeight = minWordWidths.reduce((total, width) => total + Math.max(0, width - 1), 0); + const growth = minWordWidths.map((width) => { + const weight = Math.max(0, width - 1); + return totalWeight > 0 ? Math.floor((weight / totalWeight) * remaining) : 0; + }); + + for (let i = 0; i < numCols; i++) { + minColumnWidths[i] += growth[i] ?? 0; + } + + const allocated = growth.reduce((total, width) => total + width, 0); + let leftover = remaining - allocated; + for (let i = 0; leftover > 0 && i < numCols; i++) { + minColumnWidths[i]++; + leftover--; + } + } + + minCellsWidth = minColumnWidths.reduce((a, b) => a + b, 0); + } + + // Calculate column widths that fit within available width + const totalNaturalWidth = naturalWidths.reduce((a, b) => a + b, 0) + borderOverhead; + let columnWidths: number[]; + + if (totalNaturalWidth <= availableWidth) { + // Everything fits naturally + columnWidths = naturalWidths.map((width, index) => Math.max(width, minColumnWidths[index])); + } else { + // Need to shrink columns to fit + const totalGrowPotential = naturalWidths.reduce((total, width, index) => { + return total + Math.max(0, width - minColumnWidths[index]); + }, 0); + const extraWidth = Math.max(0, availableForCells - minCellsWidth); + columnWidths = minColumnWidths.map((minWidth, index) => { + const naturalWidth = naturalWidths[index]; + const minWidthDelta = Math.max(0, naturalWidth - minWidth); + let grow = 0; + if (totalGrowPotential > 0) { + grow = Math.floor((minWidthDelta / totalGrowPotential) * extraWidth); + } + return minWidth + grow; + }); + + // Adjust for rounding errors - distribute remaining space + const allocated = columnWidths.reduce((a, b) => a + b, 0); + let remaining = availableForCells - allocated; + while (remaining > 0) { + let grew = false; + for (let i = 0; i < numCols && remaining > 0; i++) { + if (columnWidths[i] < naturalWidths[i]) { + columnWidths[i]++; + remaining--; + grew = true; + } + } + if (!grew) { + break; + } + } + } + + // Render top border + const topBorderCells = columnWidths.map((w) => "─".repeat(w)); + lines.push(`┌─${topBorderCells.join("─┬─")}─┐`); + + // Render header with wrapping + const headerCellLines: string[][] = token.header.map((cell, i) => { + const text = this.renderInlineTokens(cell.tokens || [], styleContext); + return this.wrapCellText(text, columnWidths[i], styleContext?.stylePrefix); + }); + const headerLineCount = Math.max(...headerCellLines.map((c) => c.length)); + + for (let lineIdx = 0; lineIdx < headerLineCount; lineIdx++) { + const rowParts = headerCellLines.map((cellLines, colIdx) => { + const text = cellLines[lineIdx] || ""; + const padded = text + " ".repeat(Math.max(0, columnWidths[colIdx] - visibleWidth(text))); + return this.theme.bold(padded); + }); + lines.push(`│ ${rowParts.join(" │ ")} │`); + } + + // Render separator + const separatorCells = columnWidths.map((w) => "─".repeat(w)); + const separatorLine = `├─${separatorCells.join("─┼─")}─┤`; + lines.push(separatorLine); + + // Render rows with wrapping + for (let rowIndex = 0; rowIndex < token.rows.length; rowIndex++) { + const row = token.rows[rowIndex]; + const rowCellLines: string[][] = row.map((cell, i) => { + const text = this.renderInlineTokens(cell.tokens || [], styleContext); + return this.wrapCellText(text, columnWidths[i], styleContext?.stylePrefix); + }); + const rowLineCount = Math.max(...rowCellLines.map((c) => c.length)); + + for (let lineIdx = 0; lineIdx < rowLineCount; lineIdx++) { + const rowParts = rowCellLines.map((cellLines, colIdx) => { + const text = cellLines[lineIdx] || ""; + return text + " ".repeat(Math.max(0, columnWidths[colIdx] - visibleWidth(text))); + }); + lines.push(`│ ${rowParts.join(" │ ")} │`); + } + + if (rowIndex < token.rows.length - 1) { + lines.push(separatorLine); + } + } + + // Render bottom border + const bottomBorderCells = columnWidths.map((w) => "─".repeat(w)); + lines.push(`└─${bottomBorderCells.join("─┴─")}─┘`); + + if (nextTokenType && nextTokenType !== "space") { + lines.push(""); // Add spacing after table + } + return lines; + } +} diff --git a/packages/tui/src/components/mouse-region.ts b/packages/tui/src/components/mouse-region.ts new file mode 100644 index 0000000000000000000000000000000000000000..a77ded0c1e59167fe2f0643e87531b95ed9e544d --- /dev/null +++ b/packages/tui/src/components/mouse-region.ts @@ -0,0 +1,33 @@ +import { + type Component, + dispatchMouseEvent, + type TuiMouseDispatchResult, + type TuiMouseEvent, + type TuiMouseEventResult, +} from "../tui.ts"; + +export type MouseRegionHandler = (event: TuiMouseEvent) => TuiMouseEventResult | undefined; + +/** Adds mouse handling to an existing component without changing its rendering. */ +export class MouseRegion implements Component { + private readonly child: Component; + private readonly onMouse: MouseRegionHandler; + + constructor(child: Component, onMouse: MouseRegionHandler) { + this.child = child; + this.onMouse = onMouse; + } + + render(width: number): string[] { + return this.child.render(width); + } + + handleMouse(event: TuiMouseEvent): TuiMouseDispatchResult | TuiMouseEventResult | undefined { + const childResult = dispatchMouseEvent(this.child, event); + return childResult ?? this.onMouse(event); + } + + invalidate(): void { + this.child.invalidate(); + } +} diff --git a/packages/tui/src/components/scroll-view.ts b/packages/tui/src/components/scroll-view.ts new file mode 100644 index 0000000000000000000000000000000000000000..b8f8a51ff0f8f93aabce0df072ee29c5a47c82b3 --- /dev/null +++ b/packages/tui/src/components/scroll-view.ts @@ -0,0 +1,224 @@ +import { LAYOUT_NODE, type ScrollLayoutNode } from "../layout-node.ts"; +import { type Component, Container } from "../tui.ts"; + +export type ScrollViewScrollbar = "hidden" | "auto" | "always"; + +export interface ScrollViewOptions { + axis?: "vertical"; + follow?: "none" | "end"; + primary?: boolean; + overscroll?: "chain" | "contain"; + scrollbar?: ScrollViewScrollbar; + scrollbarTrackStyle?: (text: string) => string; + scrollbarThumbStyle?: (text: string) => string; + scrollbarHideDelayMs?: number; +} + +export interface ScrollViewScrollToOptions { + /** Keep follow-end disabled even when the target is the current content end. */ + disableFollow?: boolean; +} + +export class ScrollView extends Container { + private readonly child: Component; + readonly followEnd: boolean; + readonly primary: boolean; + readonly overscroll: "chain" | "contain"; + readonly scrollbarTrackStyle: (text: string) => string; + readonly scrollbarThumbStyle: (text: string) => string; + private currentScrollbar: ScrollViewScrollbar; + private readonly scrollbarHideDelayMs: number; + private currentScrollTop = 0; + private contentHeight = 0; + private currentViewportHeight = 0; + private followingEnd: boolean; + private followSuppressedAtEnd = false; + private requestRenderCallback: (() => void) | undefined; + private transientScrollbarVisible = false; + private scrollbarActive = false; + private scrollbarHideTimer: NodeJS.Timeout | undefined; + + constructor(component: Component, options: ScrollViewOptions = {}) { + super(); + if (options.axis !== undefined && options.axis !== "vertical") { + throw new Error(`Unsupported ScrollView axis: ${options.axis}`); + } + this.child = component; + this.children.push(component); + this.followEnd = (options.follow ?? "none") === "end"; + this.followingEnd = this.followEnd; + this.primary = options.primary ?? false; + this.overscroll = options.overscroll ?? "chain"; + this.currentScrollbar = options.scrollbar ?? "hidden"; + this.scrollbarTrackStyle = options.scrollbarTrackStyle ?? ((text) => `\x1b[90m${text}\x1b[39m`); + this.scrollbarThumbStyle = options.scrollbarThumbStyle ?? ((text) => `\x1b[37m${text}\x1b[39m`); + this.scrollbarHideDelayMs = Math.max(0, Math.floor(options.scrollbarHideDelayMs ?? 1000)); + } + + get scrollTop(): number { + return this.currentScrollTop; + } + + get isFollowingEnd(): boolean { + return this.followingEnd; + } + + get viewportHeight(): number { + return this.currentViewportHeight; + } + + get scrollbar(): ScrollViewScrollbar { + return this.currentScrollbar; + } + + get isScrollbarVisible(): boolean { + if (this.scrollbar === "always") return this.currentViewportHeight > 0; + return ( + this.scrollbar === "auto" && this.contentHeight > this.currentViewportHeight && this.transientScrollbarVisible + ); + } + + get isScrollbarActive(): boolean { + return this.scrollbarActive; + } + + setScrollbar(scrollbar: ScrollViewScrollbar): void { + if (scrollbar === this.currentScrollbar) return; + this.currentScrollbar = scrollbar; + if (scrollbar !== "auto") this.hideTransientScrollbar(); + else if (this.scrollbarActive) this.markScrollbarActivity(); + this.requestRenderCallback?.(); + } + + getContentWidth(width: number): number { + return this.scrollbar === "always" && width > 1 ? width - 1 : width; + } + + private markScrollbarActivity(): void { + if (this.scrollbar !== "auto" || this.contentHeight <= this.currentViewportHeight) return; + this.transientScrollbarVisible = true; + if (this.scrollbarHideTimer) { + clearTimeout(this.scrollbarHideTimer); + this.scrollbarHideTimer = undefined; + } + if (this.scrollbarActive) return; + this.scrollbarHideTimer = setTimeout(() => { + this.scrollbarHideTimer = undefined; + this.transientScrollbarVisible = false; + this.requestRenderCallback?.(); + }, this.scrollbarHideDelayMs); + this.scrollbarHideTimer.unref(); + } + + private hideTransientScrollbar(): void { + this.transientScrollbarVisible = false; + if (!this.scrollbarHideTimer) return; + clearTimeout(this.scrollbarHideTimer); + this.scrollbarHideTimer = undefined; + } + + setScrollbarActive(active: boolean): void { + if (active === this.scrollbarActive) return; + this.scrollbarActive = active; + this.markScrollbarActivity(); + this.requestRenderCallback?.(); + } + + scrollTo(scrollTop: number, options: ScrollViewScrollToOptions = {}): void { + const requested = Number.isFinite(scrollTop) ? Math.trunc(scrollTop) : this.currentScrollTop; + const maxScrollTop = Math.max(0, this.contentHeight - this.currentViewportHeight); + const next = Math.max(0, Math.min(maxScrollTop, requested)); + const nextFollowSuppressedAtEnd = options.disableFollow === true && next === maxScrollTop; + const nextFollowingEnd = !nextFollowSuppressedAtEnd && this.followEnd && next === maxScrollTop; + if ( + next === this.currentScrollTop && + nextFollowingEnd === this.followingEnd && + nextFollowSuppressedAtEnd === this.followSuppressedAtEnd + ) { + return; + } + const moved = next !== this.currentScrollTop; + this.currentScrollTop = next; + this.followingEnd = nextFollowingEnd; + this.followSuppressedAtEnd = nextFollowSuppressedAtEnd; + if (moved) this.markScrollbarActivity(); + this.requestRenderCallback?.(); + } + + scrollBy(lines: number): number { + const requested = Number.isFinite(lines) ? Math.trunc(lines) : 0; + if (requested === 0) return 0; + const maxScrollTop = Math.max(0, this.contentHeight - this.currentViewportHeight); + const start = this.followingEnd ? maxScrollTop : this.currentScrollTop; + const next = Math.max(0, Math.min(maxScrollTop, start + requested)); + const moved = next - start; + const wasFollowingEnd = this.followingEnd; + this.currentScrollTop = next; + this.followingEnd = this.followEnd && next === maxScrollTop; + this.followSuppressedAtEnd = false; + if (moved !== 0) this.markScrollbarActivity(); + if (moved !== 0 || this.followingEnd !== wasFollowingEnd) this.requestRenderCallback?.(); + return requested - moved; + } + + scrollToStart(): void { + const changed = + this.currentScrollTop !== 0 || + this.followingEnd !== (this.followEnd && this.contentHeight <= this.currentViewportHeight); + this.currentScrollTop = 0; + this.followingEnd = this.followEnd && this.contentHeight <= this.currentViewportHeight; + this.followSuppressedAtEnd = false; + if (changed) { + this.markScrollbarActivity(); + this.requestRenderCallback?.(); + } + } + + scrollToEnd(): void { + const next = Math.max(0, this.contentHeight - this.currentViewportHeight); + const changed = this.currentScrollTop !== next || this.followingEnd !== this.followEnd; + this.currentScrollTop = next; + this.followingEnd = this.followEnd; + this.followSuppressedAtEnd = false; + if (changed) { + this.markScrollbarActivity(); + this.requestRenderCallback?.(); + } + } + + updateLayout(contentHeight: number, viewportHeight: number, requestRender: () => void): void { + this.contentHeight = Math.max(0, Math.floor(contentHeight)); + this.currentViewportHeight = Math.max(0, Math.floor(viewportHeight)); + this.requestRenderCallback = requestRender; + const maxScrollTop = Math.max(0, this.contentHeight - this.currentViewportHeight); + if (this.followingEnd) this.currentScrollTop = maxScrollTop; + else this.currentScrollTop = Math.max(0, Math.min(this.currentScrollTop, maxScrollTop)); + if (this.currentScrollTop < maxScrollTop) this.followSuppressedAtEnd = false; + if (this.followEnd && this.currentScrollTop === maxScrollTop && !this.followSuppressedAtEnd) { + this.followingEnd = true; + } + if (this.contentHeight <= this.currentViewportHeight) this.hideTransientScrollbar(); + } + + override addChild(_component: Component): void { + throw new Error("ScrollView has exactly one child"); + } + + override removeChild(_component: Component): void { + throw new Error("ScrollView child cannot be removed"); + } + + override clear(): void { + throw new Error("ScrollView child cannot be cleared"); + } + + override render(width: number): string[] { + const contentWidth = this.getContentWidth(width); + const lines = this.child.render(contentWidth); + return contentWidth === width ? lines : lines.map((line) => `${line} `); + } + + [LAYOUT_NODE](): ScrollLayoutNode { + return { type: "scroll", component: this.child, state: this }; + } +} diff --git a/packages/tui/src/components/select-list.ts b/packages/tui/src/components/select-list.ts new file mode 100644 index 0000000000000000000000000000000000000000..f4f4354dd850939e2e84ea8ce8a755206d3e8e43 --- /dev/null +++ b/packages/tui/src/components/select-list.ts @@ -0,0 +1,273 @@ +import { getKeybindings } from "../keybindings.ts"; +import type { Component, TuiMouseEvent, TuiMouseEventResult } from "../tui.ts"; +import { truncateToWidth, visibleWidth } from "../utils.ts"; + +const DEFAULT_PRIMARY_COLUMN_WIDTH = 32; +const PRIMARY_COLUMN_GAP = 2; +const MIN_DESCRIPTION_WIDTH = 10; + +const normalizeToSingleLine = (text: string): string => text.replace(/[\r\n]+/g, " ").trim(); +const clamp = (value: number, min: number, max: number): number => Math.max(min, Math.min(value, max)); + +export interface SelectItem { + value: string; + label: string; + description?: string; +} + +export interface SelectListTheme { + selectedPrefix: (text: string) => string; + selectedText: (text: string) => string; + description: (text: string) => string; + scrollInfo: (text: string) => string; + noMatch: (text: string) => string; +} + +export interface SelectListTruncatePrimaryContext { + text: string; + maxWidth: number; + columnWidth: number; + item: SelectItem; + isSelected: boolean; +} + +export interface SelectListLayoutOptions { + minPrimaryColumnWidth?: number; + maxPrimaryColumnWidth?: number; + truncatePrimary?: (context: SelectListTruncatePrimaryContext) => string; +} + +export class SelectList implements Component { + private items: SelectItem[] = []; + private filteredItems: SelectItem[] = []; + private selectedIndex: number = 0; + private mousePressedIndex: number | undefined; + private maxVisible: number = 5; + private theme: SelectListTheme; + private layout: SelectListLayoutOptions; + + public onSelect?: (item: SelectItem) => void; + public onCancel?: () => void; + public onSelectionChange?: (item: SelectItem) => void; + + constructor(items: SelectItem[], maxVisible: number, theme: SelectListTheme, layout: SelectListLayoutOptions = {}) { + this.items = items; + this.filteredItems = items; + this.maxVisible = maxVisible; + this.theme = theme; + this.layout = layout; + } + + setFilter(filter: string): void { + this.filteredItems = this.items.filter((item) => item.value.toLowerCase().startsWith(filter.toLowerCase())); + // Reset selection when filter changes + this.selectedIndex = 0; + } + + setSelectedIndex(index: number): void { + this.selectedIndex = Math.max(0, Math.min(index, this.filteredItems.length - 1)); + } + + invalidate(): void { + // No cached state to invalidate currently + } + + render(width: number): string[] { + const lines: string[] = []; + + // If no items match filter, show message + if (this.filteredItems.length === 0) { + lines.push(this.theme.noMatch(" No matching commands")); + return lines; + } + + const primaryColumnWidth = this.getPrimaryColumnWidth(); + + // Calculate visible range with scrolling + const { startIndex, endIndex } = this.getVisibleRange(); + + // Render visible items + for (let i = startIndex; i < endIndex; i++) { + const item = this.filteredItems[i]; + if (!item) continue; + + const isSelected = i === this.selectedIndex; + const descriptionSingleLine = item.description ? normalizeToSingleLine(item.description) : undefined; + lines.push(this.renderItem(item, isSelected, width, descriptionSingleLine, primaryColumnWidth)); + } + + // Add scroll indicators if needed + if (startIndex > 0 || endIndex < this.filteredItems.length) { + const scrollText = ` (${this.selectedIndex + 1}/${this.filteredItems.length})`; + // Truncate if too long for terminal + lines.push(this.theme.scrollInfo(truncateToWidth(scrollText, width - 2, ""))); + } + + return lines; + } + + handleMouse(event: TuiMouseEvent): TuiMouseEventResult | undefined { + if (this.filteredItems.length === 0) return undefined; + if (event.type === "wheel" && event.wheelDelta) { + const delta = event.wheelDelta < 0 ? -1 : 1; + const previousIndex = this.selectedIndex; + this.selectedIndex = Math.max(0, Math.min(this.filteredItems.length - 1, this.selectedIndex + delta)); + if (this.selectedIndex !== previousIndex) this.notifySelectionChange(); + return { handled: true, render: this.selectedIndex !== previousIndex }; + } + // Hover must not change selection: the visible range is centered on it. + if (event.button !== "left" || (event.type !== "press" && event.type !== "click")) return undefined; + const { startIndex, endIndex } = this.getVisibleRange(); + const itemIndex = startIndex + event.y; + if (itemIndex < startIndex || itemIndex >= endIndex) return undefined; + + if (event.type === "press") { + this.mousePressedIndex = itemIndex; + if (this.selectedIndex !== itemIndex) { + this.selectedIndex = itemIndex; + this.notifySelectionChange(); + } + return { handled: true, focus: true }; + } + if (event.type === "click") { + const clickedIndex = this.mousePressedIndex ?? itemIndex; + this.mousePressedIndex = undefined; + const changed = this.selectedIndex !== clickedIndex; + this.selectedIndex = clickedIndex; + if (changed) this.notifySelectionChange(); + const selectedItem = this.filteredItems[this.selectedIndex]; + if (selectedItem) this.onSelect?.(selectedItem); + return { handled: true }; + } + return undefined; + } + + handleInput(keyData: string): void { + const kb = getKeybindings(); + // Up arrow - wrap to bottom when at top + if (kb.matches(keyData, "tui.select.up")) { + this.selectedIndex = this.selectedIndex === 0 ? this.filteredItems.length - 1 : this.selectedIndex - 1; + this.notifySelectionChange(); + } + // Down arrow - wrap to top when at bottom + else if (kb.matches(keyData, "tui.select.down")) { + this.selectedIndex = this.selectedIndex === this.filteredItems.length - 1 ? 0 : this.selectedIndex + 1; + this.notifySelectionChange(); + } + // Enter + else if (kb.matches(keyData, "tui.select.confirm")) { + const selectedItem = this.filteredItems[this.selectedIndex]; + if (selectedItem && this.onSelect) { + this.onSelect(selectedItem); + } + } + // Escape or Ctrl+C + else if (kb.matches(keyData, "tui.select.cancel")) { + if (this.onCancel) { + this.onCancel(); + } + } + } + + private getVisibleRange(): { startIndex: number; endIndex: number } { + const startIndex = Math.max( + 0, + Math.min(this.selectedIndex - Math.floor(this.maxVisible / 2), this.filteredItems.length - this.maxVisible), + ); + return { + startIndex, + endIndex: Math.min(startIndex + this.maxVisible, this.filteredItems.length), + }; + } + + private renderItem( + item: SelectItem, + isSelected: boolean, + width: number, + descriptionSingleLine: string | undefined, + primaryColumnWidth: number, + ): string { + const prefix = isSelected ? "→ " : " "; + const prefixWidth = visibleWidth(prefix); + + if (descriptionSingleLine && width > 40) { + const effectivePrimaryColumnWidth = Math.max(1, Math.min(primaryColumnWidth, width - prefixWidth - 4)); + const maxPrimaryWidth = Math.max(1, effectivePrimaryColumnWidth - PRIMARY_COLUMN_GAP); + const truncatedValue = this.truncatePrimary(item, isSelected, maxPrimaryWidth, effectivePrimaryColumnWidth); + const truncatedValueWidth = visibleWidth(truncatedValue); + const spacing = " ".repeat(Math.max(1, effectivePrimaryColumnWidth - truncatedValueWidth)); + const descriptionStart = prefixWidth + truncatedValueWidth + spacing.length; + const remainingWidth = width - descriptionStart - 2; // -2 for safety + + if (remainingWidth > MIN_DESCRIPTION_WIDTH) { + const truncatedDesc = truncateToWidth(descriptionSingleLine, remainingWidth, ""); + if (isSelected) { + return this.theme.selectedText(`${prefix}${truncatedValue}${spacing}${truncatedDesc}`); + } + + const descText = this.theme.description(spacing + truncatedDesc); + return prefix + truncatedValue + descText; + } + } + + const maxWidth = width - prefixWidth - 2; + const truncatedValue = this.truncatePrimary(item, isSelected, maxWidth, maxWidth); + if (isSelected) { + return this.theme.selectedText(`${prefix}${truncatedValue}`); + } + + return prefix + truncatedValue; + } + + private getPrimaryColumnWidth(): number { + const { min, max } = this.getPrimaryColumnBounds(); + const widestPrimary = this.filteredItems.reduce((widest, item) => { + return Math.max(widest, visibleWidth(this.getDisplayValue(item)) + PRIMARY_COLUMN_GAP); + }, 0); + + return clamp(widestPrimary, min, max); + } + + private getPrimaryColumnBounds(): { min: number; max: number } { + const rawMin = + this.layout.minPrimaryColumnWidth ?? this.layout.maxPrimaryColumnWidth ?? DEFAULT_PRIMARY_COLUMN_WIDTH; + const rawMax = + this.layout.maxPrimaryColumnWidth ?? this.layout.minPrimaryColumnWidth ?? DEFAULT_PRIMARY_COLUMN_WIDTH; + + return { + min: Math.max(1, Math.min(rawMin, rawMax)), + max: Math.max(1, Math.max(rawMin, rawMax)), + }; + } + + private truncatePrimary(item: SelectItem, isSelected: boolean, maxWidth: number, columnWidth: number): string { + const displayValue = this.getDisplayValue(item); + const truncatedValue = this.layout.truncatePrimary + ? this.layout.truncatePrimary({ + text: displayValue, + maxWidth, + columnWidth, + item, + isSelected, + }) + : truncateToWidth(displayValue, maxWidth, ""); + + return truncateToWidth(truncatedValue, maxWidth, ""); + } + + private getDisplayValue(item: SelectItem): string { + return item.label || item.value; + } + + private notifySelectionChange(): void { + const selectedItem = this.filteredItems[this.selectedIndex]; + if (selectedItem && this.onSelectionChange) { + this.onSelectionChange(selectedItem); + } + } + + getSelectedItem(): SelectItem | null { + const item = this.filteredItems[this.selectedIndex]; + return item || null; + } +} diff --git a/packages/tui/src/components/settings-list.ts b/packages/tui/src/components/settings-list.ts new file mode 100644 index 0000000000000000000000000000000000000000..b1b8366bf349b30a1d93073a4cc1d0e2bfa9dc8a --- /dev/null +++ b/packages/tui/src/components/settings-list.ts @@ -0,0 +1,328 @@ +import { fuzzyFilter } from "../fuzzy.ts"; +import { getKeybindings } from "../keybindings.ts"; +import type { Component, TuiMouseEvent, TuiMouseEventResult } from "../tui.ts"; +import { truncateToWidth, visibleWidth, wrapTextWithAnsi } from "../utils.ts"; +import { Input } from "./input.ts"; + +export interface SettingItem { + /** Unique identifier for this setting */ + id: string; + /** Display label (left side) */ + label: string; + /** Optional description shown when selected */ + description?: string; + /** Current value to display (right side) */ + currentValue: string; + /** If provided, Enter/Space cycles through these values */ + values?: string[]; + /** If provided, Enter opens this submenu. Receives current value and done callback. + * done() accepts an optional selectedValue and an optional navigateTo id to move the cursor after close. */ + submenu?: ( + currentValue: string, + done: (selectedValue?: string, options?: { navigateTo?: string }) => void, + ) => Component; +} + +export interface SettingsListTheme { + label: (text: string, selected: boolean) => string; + value: (text: string, selected: boolean) => string; + description: (text: string) => string; + cursor: string; + hint: (text: string) => string; +} + +export interface SettingsListOptions { + enableSearch?: boolean; +} + +export class SettingsList implements Component { + private items: SettingItem[]; + private filteredItems: SettingItem[]; + private theme: SettingsListTheme; + private selectedIndex = 0; + private mousePressedIndex: number | undefined; + private maxVisible: number; + private onChange: (id: string, newValue: string) => void; + private onCancel: () => void; + private searchInput?: Input; + private searchEnabled: boolean; + + // Submenu state + private submenuComponent: Component | null = null; + private submenuItemIndex: number | null = null; + private navigateAfterClose: string | null = null; + + constructor( + items: SettingItem[], + maxVisible: number, + theme: SettingsListTheme, + onChange: (id: string, newValue: string) => void, + onCancel: () => void, + options: SettingsListOptions = {}, + ) { + this.items = items; + this.filteredItems = items; + this.maxVisible = maxVisible; + this.theme = theme; + this.onChange = onChange; + this.onCancel = onCancel; + this.searchEnabled = options.enableSearch ?? false; + if (this.searchEnabled) { + this.searchInput = new Input(); + } + } + + /** Update an item's currentValue */ + updateValue(id: string, newValue: string): void { + const item = this.items.find((i) => i.id === id); + if (item) { + item.currentValue = newValue; + } + } + + /** Move selection to the item with the given id (no-op if not found). */ + selectItem(id: string): void { + const items = this.searchEnabled ? this.filteredItems : this.items; + const index = items.findIndex((i) => i.id === id); + if (index !== -1) { + this.selectedIndex = index; + } + } + + invalidate(): void { + this.submenuComponent?.invalidate?.(); + } + + render(width: number): string[] { + // If submenu is active, render it instead + if (this.submenuComponent) { + return this.submenuComponent.render(width); + } + + return this.renderMainList(width); + } + + private renderMainList(width: number): string[] { + const lines: string[] = []; + + if (this.searchEnabled && this.searchInput) { + lines.push(...this.searchInput.render(width)); + lines.push(""); + } + + if (this.items.length === 0) { + lines.push(this.theme.hint(" No settings available")); + if (this.searchEnabled) { + this.addHintLine(lines, width); + } + return lines; + } + + const displayItems = this.getDisplayItems(); + if (displayItems.length === 0) { + lines.push(truncateToWidth(this.theme.hint(" No matching settings"), width)); + this.addHintLine(lines, width); + return lines; + } + + // Calculate visible range with scrolling + const { startIndex, endIndex } = this.getVisibleRange(displayItems); + + // Calculate max label width for alignment + const maxLabelWidth = Math.min(36, Math.max(...this.items.map((item) => visibleWidth(item.label)))); + + // Render visible items + for (let i = startIndex; i < endIndex; i++) { + const item = displayItems[i]; + if (!item) continue; + + const isSelected = i === this.selectedIndex; + const prefix = isSelected ? this.theme.cursor : " "; + const prefixWidth = visibleWidth(prefix); + + // Pad label to align values + const labelPadded = item.label + " ".repeat(Math.max(0, maxLabelWidth - visibleWidth(item.label))); + const labelText = this.theme.label(labelPadded, isSelected); + + // Calculate space for value + const separator = " "; + const usedWidth = prefixWidth + maxLabelWidth + visibleWidth(separator); + const valueMaxWidth = width - usedWidth - 2; + + const valueText = this.theme.value(truncateToWidth(item.currentValue, valueMaxWidth, ""), isSelected); + + lines.push(truncateToWidth(prefix + labelText + separator + valueText, width)); + } + + // Add scroll indicator if needed + if (startIndex > 0 || endIndex < displayItems.length) { + const scrollText = ` (${this.selectedIndex + 1}/${displayItems.length})`; + lines.push(this.theme.hint(truncateToWidth(scrollText, width - 2, ""))); + } + + // Add description for selected item + const selectedItem = displayItems[this.selectedIndex]; + if (selectedItem?.description) { + lines.push(""); + const wrappedDesc = wrapTextWithAnsi(selectedItem.description, width - 4); + for (const line of wrappedDesc) { + lines.push(this.theme.description(` ${line}`)); + } + } + + // Add hint + this.addHintLine(lines, width); + + return lines; + } + + handleMouse(event: TuiMouseEvent): TuiMouseEventResult | undefined { + if (this.submenuComponent) { + const result = this.submenuComponent.handleMouse?.(event); + return result ? { ...result, focus: true } : undefined; + } + + if (this.searchEnabled && this.searchInput) { + if (event.y === 0) { + const result = this.searchInput.handleMouse?.(event); + return result ? { ...result, focus: true } : undefined; + } + if (event.y === 1) return undefined; + } + + const displayItems = this.getDisplayItems(); + if (displayItems.length === 0) return undefined; + if (event.type === "wheel" && event.wheelDelta) { + const delta = event.wheelDelta < 0 ? -1 : 1; + const previousIndex = this.selectedIndex; + this.selectedIndex = Math.max(0, Math.min(displayItems.length - 1, this.selectedIndex + delta)); + return { handled: true, render: this.selectedIndex !== previousIndex }; + } + // Hover must not change selection: the visible range is centered on it. + if (event.button !== "left" || (event.type !== "press" && event.type !== "click")) return undefined; + + const rowOffset = this.searchEnabled ? 2 : 0; + const { startIndex, endIndex } = this.getVisibleRange(displayItems); + const itemIndex = startIndex + event.y - rowOffset; + if (itemIndex < startIndex || itemIndex >= endIndex) return undefined; + if (event.type === "press") { + this.mousePressedIndex = itemIndex; + this.selectedIndex = itemIndex; + return { handled: true, focus: true }; + } + if (event.type === "click") { + this.selectedIndex = this.mousePressedIndex ?? itemIndex; + this.mousePressedIndex = undefined; + this.activateItem(); + return { handled: true }; + } + return undefined; + } + + handleInput(data: string): void { + // If submenu is active, delegate all input to it + // The submenu's onCancel (triggered by escape) will call done() which closes it + if (this.submenuComponent) { + this.submenuComponent.handleInput?.(data); + return; + } + + // Main list input handling + const kb = getKeybindings(); + const displayItems = this.getDisplayItems(); + if (kb.matches(data, "tui.select.up")) { + if (displayItems.length === 0) return; + this.selectedIndex = this.selectedIndex === 0 ? displayItems.length - 1 : this.selectedIndex - 1; + } else if (kb.matches(data, "tui.select.down")) { + if (displayItems.length === 0) return; + this.selectedIndex = this.selectedIndex === displayItems.length - 1 ? 0 : this.selectedIndex + 1; + } else if ( + kb.matches(data, "tui.select.confirm") || + (data === " " && (!this.searchEnabled || this.searchInput?.getValue().length === 0)) + ) { + this.activateItem(); + } else if (kb.matches(data, "tui.select.cancel")) { + this.onCancel(); + } else if (this.searchEnabled && this.searchInput) { + this.searchInput.handleInput(data); + this.applyFilter(this.searchInput.getValue()); + } + } + + private getDisplayItems(): SettingItem[] { + return this.searchEnabled ? this.filteredItems : this.items; + } + + private getVisibleRange(displayItems: readonly SettingItem[]): { startIndex: number; endIndex: number } { + const startIndex = Math.max( + 0, + Math.min(this.selectedIndex - Math.floor(this.maxVisible / 2), displayItems.length - this.maxVisible), + ); + return { startIndex, endIndex: Math.min(startIndex + this.maxVisible, displayItems.length) }; + } + + private activateItem(): void { + const item = this.getDisplayItems()[this.selectedIndex]; + if (!item) return; + + if (item.submenu) { + // Open submenu, passing current value so it can pre-select correctly + this.submenuItemIndex = this.selectedIndex; + this.submenuComponent = item.submenu( + item.currentValue, + (selectedValue?: string, options?: { navigateTo?: string }) => { + if (selectedValue !== undefined) { + item.currentValue = selectedValue; + this.onChange(item.id, selectedValue); + } + if (options?.navigateTo) { + this.navigateAfterClose = options.navigateTo; + } + this.closeSubmenu(); + }, + ); + } else if (item.values && item.values.length > 0) { + // Cycle through values + const currentIndex = item.values.indexOf(item.currentValue); + const nextIndex = (currentIndex + 1) % item.values.length; + const newValue = item.values[nextIndex]; + item.currentValue = newValue; + this.onChange(item.id, newValue); + } + } + + private closeSubmenu(): void { + this.submenuComponent = null; + if (this.navigateAfterClose !== null) { + const id = this.navigateAfterClose; + this.navigateAfterClose = null; + this.submenuItemIndex = null; + this.selectItem(id); + // Open the target item's submenu automatically + this.activateItem(); + } else if (this.submenuItemIndex !== null) { + // Restore selection to the item that opened the submenu + this.selectedIndex = this.submenuItemIndex; + this.submenuItemIndex = null; + } + } + + private applyFilter(query: string): void { + this.filteredItems = fuzzyFilter(this.items, query, (item) => item.label); + this.selectedIndex = 0; + } + + private addHintLine(lines: string[], width: number): void { + lines.push(""); + lines.push( + truncateToWidth( + this.theme.hint( + this.searchEnabled + ? " Type to search · Enter/Space to change · Esc to cancel" + : " Enter/Space to change · Esc to cancel", + ), + width, + ), + ); + } +} diff --git a/packages/tui/src/components/spacer.ts b/packages/tui/src/components/spacer.ts new file mode 100644 index 0000000000000000000000000000000000000000..7abe1551ca79a17bbb03d6b62f3ec18c50274a75 --- /dev/null +++ b/packages/tui/src/components/spacer.ts @@ -0,0 +1,28 @@ +import type { Component } from "../tui.ts"; + +/** + * Spacer component that renders empty lines + */ +export class Spacer implements Component { + private lines: number; + + constructor(lines: number = 1) { + this.lines = lines; + } + + setLines(lines: number): void { + this.lines = lines; + } + + invalidate(): void { + // No cached state to invalidate currently + } + + render(_width: number): string[] { + const result: string[] = []; + for (let i = 0; i < this.lines; i++) { + result.push(""); + } + return result; + } +} diff --git a/packages/tui/src/components/stack.ts b/packages/tui/src/components/stack.ts new file mode 100644 index 0000000000000000000000000000000000000000..68bde0368455646144a1dd3942b56a48e9d4f45a --- /dev/null +++ b/packages/tui/src/components/stack.ts @@ -0,0 +1,154 @@ +import { LAYOUT_NODE, type LayoutViewport, type StackLayoutEntry, type StackLayoutNode } from "../layout-node.ts"; +import { type Component, Container } from "../tui.ts"; + +export interface StackEntryOptions { + basis?: number | "auto"; + grow?: number; + shrink?: number; + minSize?: number; + maxSize?: number; + visible?: (viewport: LayoutViewport) => boolean; +} + +export interface StackEntry extends StackEntryOptions { + component: Component; +} + +export type StackChild = Component | StackEntry; + +export interface StackOptions { + gap?: number; + align?: "stretch" | "start" | "center" | "end"; +} + +function isStackEntry(child: StackChild): child is StackEntry { + return !("render" in child); +} + +function normalizeSize(value: number | undefined, fallback: number): number { + return value === undefined || !Number.isFinite(value) ? fallback : Math.max(0, Math.floor(value)); +} + +export abstract class Stack extends Container { + protected readonly entries: StackLayoutEntry[] = []; + protected readonly gap: number; + protected readonly align: "stretch" | "start" | "center" | "end"; + protected abstract readonly layoutType: "vstack" | "hstack"; + + constructor(children: StackChild[] = [], options: StackOptions = {}) { + super(); + this.gap = normalizeSize(options.gap, 0); + this.align = options.align ?? "stretch"; + for (const child of children) { + if (isStackEntry(child)) this.addChild(child.component, child); + else this.addChild(child); + } + } + + override addChild(component: Component, options: StackEntryOptions = {}): void { + super.addChild(component); + this.entries.push({ + component, + ...(options.basis === undefined ? {} : { basis: options.basis }), + ...(options.grow === undefined ? {} : { grow: normalizeSize(options.grow, 0) }), + ...(options.shrink === undefined ? {} : { shrink: normalizeSize(options.shrink, 1) }), + ...(options.minSize === undefined ? {} : { minSize: normalizeSize(options.minSize, 0) }), + ...(options.maxSize === undefined ? {} : { maxSize: normalizeSize(options.maxSize, Number.MAX_SAFE_INTEGER) }), + ...(options.visible === undefined ? {} : { visible: options.visible }), + }); + } + + override removeChild(component: Component): void { + super.removeChild(component); + const index = this.entries.findIndex((entry) => entry.component === component); + if (index !== -1) this.entries.splice(index, 1); + } + + override clear(): void { + super.clear(); + this.entries.length = 0; + } + + [LAYOUT_NODE](): StackLayoutNode { + return { + type: this.layoutType, + entries: this.entries, + gap: this.gap, + align: this.align, + }; + } +} + +export function visibleStackEntries( + entries: readonly StackLayoutEntry[], + viewport: LayoutViewport, +): StackLayoutEntry[] { + return entries.filter((entry) => entry.visible?.(viewport) ?? true); +} + +function clampSize(size: number, entry: StackLayoutEntry): number { + const min = Math.max(0, Math.floor(entry.minSize ?? 0)); + const max = Math.max(min, Math.floor(entry.maxSize ?? Number.MAX_SAFE_INTEGER)); + return Math.max(min, Math.min(max, Math.max(0, Math.floor(size)))); +} + +function distribute( + sizes: number[], + entries: readonly StackLayoutEntry[], + amount: number, + mode: "grow" | "shrink", +): void { + let remaining = amount; + while (remaining > 0) { + const candidates = entries + .map((entry, index) => ({ entry, index })) + .filter(({ entry, index }) => { + if (mode === "grow") { + return (entry.grow ?? 0) > 0 && sizes[index]! < (entry.maxSize ?? Number.MAX_SAFE_INTEGER); + } + return (entry.shrink ?? 1) > 0 && sizes[index]! > (entry.minSize ?? 0); + }); + if (candidates.length === 0) return; + + const totalWeight = candidates.reduce((sum, { entry, index }) => { + return sum + (mode === "grow" ? (entry.grow ?? 0) : (entry.shrink ?? 1) * Math.max(1, sizes[index]!)); + }, 0); + let distributed = 0; + for (const { entry, index } of candidates) { + if (remaining <= 0) break; + const weight = mode === "grow" ? (entry.grow ?? 0) : (entry.shrink ?? 1) * Math.max(1, sizes[index]!); + const proposed = Math.max(1, Math.floor((remaining * weight) / totalWeight)); + const capacity = + mode === "grow" + ? (entry.maxSize ?? Number.MAX_SAFE_INTEGER) - sizes[index]! + : sizes[index]! - (entry.minSize ?? 0); + const delta = Math.min(remaining, proposed, capacity); + if (delta <= 0) continue; + sizes[index] = sizes[index]! + (mode === "grow" ? delta : -delta); + remaining -= delta; + distributed += delta; + } + if (distributed === 0) return; + } +} + +export function allocateStackSizes( + entries: readonly StackLayoutEntry[], + intrinsicSizes: readonly number[], + availableSize: number | undefined, + gap: number, +): number[] { + const sizes = entries.map((entry, index) => + clampSize( + entry.basis === undefined || entry.basis === "auto" ? (intrinsicSizes[index] ?? 0) : entry.basis, + entry, + ), + ); + if (availableSize === undefined) return sizes; + + const contentSize = Math.max(0, Math.floor(availableSize) - Math.max(0, entries.length - 1) * gap); + const total = sizes.reduce((sum, size) => sum + size, 0); + if (total < contentSize) distribute(sizes, entries, contentSize - total, "grow"); + else if (total > contentSize) distribute(sizes, entries, total - contentSize, "shrink"); + return sizes; +} diff --git a/packages/tui/src/components/text.ts b/packages/tui/src/components/text.ts new file mode 100644 index 0000000000000000000000000000000000000000..7a50e2721028d544f641ab5f66ef1b752ff6d1f7 --- /dev/null +++ b/packages/tui/src/components/text.ts @@ -0,0 +1,107 @@ +import type { Component } from "../tui.ts"; +import { applyBackgroundToLine, visibleWidth, wrapTextWithAnsi } from "../utils.ts"; + +/** + * Text component - displays multi-line text with word wrapping + */ +export class Text implements Component { + private text: string; + private paddingX: number; // Left/right padding + private paddingY: number; // Top/bottom padding + private customBgFn?: (text: string) => string; + + // Cache for rendered output + private cachedText?: string; + private cachedWidth?: number; + private cachedLines?: string[]; + + constructor(text: string = "", paddingX: number = 1, paddingY: number = 1, customBgFn?: (text: string) => string) { + this.text = text; + this.paddingX = paddingX; + this.paddingY = paddingY; + this.customBgFn = customBgFn; + } + + setText(text: string): void { + this.text = text; + this.cachedText = undefined; + this.cachedWidth = undefined; + this.cachedLines = undefined; + } + + setCustomBgFn(customBgFn?: (text: string) => string): void { + this.customBgFn = customBgFn; + this.cachedText = undefined; + this.cachedWidth = undefined; + this.cachedLines = undefined; + } + + invalidate(): void { + this.cachedText = undefined; + this.cachedWidth = undefined; + this.cachedLines = undefined; + } + + render(width: number): string[] { + // Check cache + if (this.cachedLines && this.cachedText === this.text && this.cachedWidth === width) { + return this.cachedLines; + } + + // Don't render anything if there's no actual text + if (!this.text || this.text.trim() === "") { + const result: string[] = []; + this.cachedText = this.text; + this.cachedWidth = width; + this.cachedLines = result; + return result; + } + + // Replace tabs with 3 spaces + const normalizedText = this.text.replace(/\t/g, " "); + + // Reduce margins when necessary so content and padding fit within the available width. + const paddingX = Math.min(this.paddingX, Math.max(0, Math.floor((width - 1) / 2))); + const contentWidth = Math.max(1, width - paddingX * 2); + + // Wrap text (this preserves ANSI codes but does NOT pad) + const wrappedLines = wrapTextWithAnsi(normalizedText, contentWidth); + + // Add margins and background to each line + const leftMargin = " ".repeat(paddingX); + const rightMargin = " ".repeat(paddingX); + const contentLines: string[] = []; + + for (const line of wrappedLines) { + // Add margins + const lineWithMargins = leftMargin + line + rightMargin; + + // Apply background if specified (this also pads to full width) + if (this.customBgFn) { + contentLines.push(applyBackgroundToLine(lineWithMargins, width, this.customBgFn)); + } else { + // No background - just pad to width with spaces + const visibleLen = visibleWidth(lineWithMargins); + const paddingNeeded = Math.max(0, width - visibleLen); + contentLines.push(lineWithMargins + " ".repeat(paddingNeeded)); + } + } + + // Add top/bottom padding (empty lines) + const emptyLine = " ".repeat(width); + const emptyLines: string[] = []; + for (let i = 0; i < this.paddingY; i++) { + const line = this.customBgFn ? applyBackgroundToLine(emptyLine, width, this.customBgFn) : emptyLine; + emptyLines.push(line); + } + + const result = [...emptyLines, ...contentLines, ...emptyLines]; + + // Update cache + this.cachedText = this.text; + this.cachedWidth = width; + this.cachedLines = result; + + return result.length > 0 ? result : [""]; + } +} diff --git a/packages/tui/src/components/truncated-text.ts b/packages/tui/src/components/truncated-text.ts new file mode 100644 index 0000000000000000000000000000000000000000..c26b8892967a8462a8828cfed4ea572a73af1602 --- /dev/null +++ b/packages/tui/src/components/truncated-text.ts @@ -0,0 +1,65 @@ +import type { Component } from "../tui.ts"; +import { truncateToWidth, visibleWidth } from "../utils.ts"; + +/** + * Text component that truncates to fit viewport width + */ +export class TruncatedText implements Component { + private text: string; + private paddingX: number; + private paddingY: number; + + constructor(text: string, paddingX: number = 0, paddingY: number = 0) { + this.text = text; + this.paddingX = paddingX; + this.paddingY = paddingY; + } + + invalidate(): void { + // No cached state to invalidate currently + } + + render(width: number): string[] { + const result: string[] = []; + + // Empty line padded to width + const emptyLine = " ".repeat(width); + + // Add vertical padding above + for (let i = 0; i < this.paddingY; i++) { + result.push(emptyLine); + } + + // Calculate available width after horizontal padding + const availableWidth = Math.max(1, width - this.paddingX * 2); + + // Take only the first line (stop at newline) + let singleLineText = this.text; + const newlineIndex = this.text.indexOf("\n"); + if (newlineIndex !== -1) { + singleLineText = this.text.substring(0, newlineIndex); + } + + // Truncate text if needed (accounting for ANSI codes) + const displayText = truncateToWidth(singleLineText, availableWidth); + + // Add horizontal padding + const leftPadding = " ".repeat(this.paddingX); + const rightPadding = " ".repeat(this.paddingX); + const lineWithPadding = leftPadding + displayText + rightPadding; + + // Pad line to exactly width characters + const lineVisibleWidth = visibleWidth(lineWithPadding); + const paddingNeeded = Math.max(0, width - lineVisibleWidth); + const finalLine = lineWithPadding + " ".repeat(paddingNeeded); + + result.push(finalLine); + + // Add vertical padding below + for (let i = 0; i < this.paddingY; i++) { + result.push(emptyLine); + } + + return result; + } +} diff --git a/packages/tui/src/components/v-stack.ts b/packages/tui/src/components/v-stack.ts new file mode 100644 index 0000000000000000000000000000000000000000..ce7b9292d6dfd0981650a1cf927f223b926f9714 --- /dev/null +++ b/packages/tui/src/components/v-stack.ts @@ -0,0 +1,33 @@ +import { allocateStackSizes, Stack, type StackChild, type StackOptions, visibleStackEntries } from "./stack.ts"; + +export class VStack extends Stack { + protected readonly layoutType = "vstack" as const; + + constructor(children: StackChild[] = [], options: StackOptions = {}) { + super(children, options); + } + + override render(width: number): string[] { + const viewport = { width: Math.max(1, width), height: Number.MAX_SAFE_INTEGER }; + const entries = visibleStackEntries(this.entries, viewport); + const rendered = entries.map((entry) => entry.component.render(viewport.width)); + const sizes = allocateStackSizes( + entries, + rendered.map((lines) => lines.length), + undefined, + this.gap, + ); + const lines: string[] = []; + for (let index = 0; index < entries.length; index++) { + if (index > 0) { + for (let gap = 0; gap < this.gap; gap++) lines.push(""); + } + const childLines = rendered[index]!.slice(0, sizes[index]); + lines.push(...childLines); + for (let padding = childLines.length; padding < sizes[index]!; padding++) lines.push(""); + } + return lines; + } +} + +export type { StackChild, StackEntry, StackEntryOptions, StackOptions } from "./stack.ts"; diff --git a/packages/tui/src/editor-component.ts b/packages/tui/src/editor-component.ts new file mode 100644 index 0000000000000000000000000000000000000000..595645033a074d17b8190b73709c2623d3e479b5 --- /dev/null +++ b/packages/tui/src/editor-component.ts @@ -0,0 +1,74 @@ +import type { AutocompleteProvider } from "./autocomplete.ts"; +import type { Component } from "./tui.ts"; + +/** + * Interface for custom editor components. + * + * This allows extensions to provide their own editor implementation + * (e.g., vim mode, emacs mode, custom keybindings) while maintaining + * compatibility with the core application. + */ +export interface EditorComponent extends Component { + // ========================================================================= + // Core text access (required) + // ========================================================================= + + /** Get the current text content */ + getText(): string; + + /** Set the text content */ + setText(text: string): void; + + /** Handle raw terminal input (key presses, paste sequences, etc.) */ + handleInput(data: string): void; + + // ========================================================================= + // Callbacks (required) + // ========================================================================= + + /** Called when user submits (e.g., Enter key) */ + onSubmit?: (text: string) => void; + + /** Called when text changes */ + onChange?: (text: string) => void; + + // ========================================================================= + // History support (optional) + // ========================================================================= + + /** Add text to history for up/down navigation */ + addToHistory?(text: string): void; + + // ========================================================================= + // Advanced text manipulation (optional) + // ========================================================================= + + /** Insert text at current cursor position */ + insertTextAtCursor?(text: string): void; + + /** + * Get text with any markers expanded (e.g., paste markers). + * Falls back to getText() if not implemented. + */ + getExpandedText?(): string; + + // ========================================================================= + // Autocomplete support (optional) + // ========================================================================= + + /** Set the autocomplete provider */ + setAutocompleteProvider?(provider: AutocompleteProvider): void; + + // ========================================================================= + // Appearance (optional) + // ========================================================================= + + /** Border color function */ + borderColor?: (str: string) => string; + + /** Set horizontal padding */ + setPaddingX?(padding: number): void; + + /** Set max visible items in autocomplete dropdown */ + setAutocompleteMaxVisible?(maxVisible: number): void; +} diff --git a/packages/tui/src/fuzzy.ts b/packages/tui/src/fuzzy.ts new file mode 100644 index 0000000000000000000000000000000000000000..73c10dcf4b134bfefba2978281b2a6f94352c5b8 --- /dev/null +++ b/packages/tui/src/fuzzy.ts @@ -0,0 +1,137 @@ +/** + * Fuzzy matching utilities. + * Matches if all query characters appear in order (not necessarily consecutive). + * Lower score = better match. + */ + +export interface FuzzyMatch { + matches: boolean; + score: number; +} + +export function fuzzyMatch(query: string, text: string): FuzzyMatch { + const queryLower = query.toLowerCase(); + const textLower = text.toLowerCase(); + + const matchQuery = (normalizedQuery: string): FuzzyMatch => { + if (normalizedQuery.length === 0) { + return { matches: true, score: 0 }; + } + + if (normalizedQuery.length > textLower.length) { + return { matches: false, score: 0 }; + } + + let queryIndex = 0; + let score = 0; + let lastMatchIndex = -1; + let consecutiveMatches = 0; + + for (let i = 0; i < textLower.length && queryIndex < normalizedQuery.length; i++) { + if (textLower[i] === normalizedQuery[queryIndex]) { + const isWordBoundary = i === 0 || /[\s\-_./:]/.test(textLower[i - 1]!); + + // Reward consecutive matches + if (lastMatchIndex === i - 1) { + consecutiveMatches++; + score -= consecutiveMatches * 5; + } else { + consecutiveMatches = 0; + // Penalize gaps + if (lastMatchIndex >= 0) { + score += (i - lastMatchIndex - 1) * 2; + } + } + + // Reward word boundary matches + if (isWordBoundary) { + score -= 10; + } + + // Slight penalty for later matches + score += i * 0.1; + + lastMatchIndex = i; + queryIndex++; + } + } + + if (queryIndex < normalizedQuery.length) { + return { matches: false, score: 0 }; + } + + if (normalizedQuery === textLower) { + score -= 100; + } + + return { matches: true, score }; + }; + + const primaryMatch = matchQuery(queryLower); + if (primaryMatch.matches) { + return primaryMatch; + } + + const alphaNumericMatch = queryLower.match(/^(?[a-z]+)(?[0-9]+)$/); + const numericAlphaMatch = queryLower.match(/^(?[0-9]+)(?[a-z]+)$/); + const swappedQuery = alphaNumericMatch + ? `${alphaNumericMatch.groups?.digits ?? ""}${alphaNumericMatch.groups?.letters ?? ""}` + : numericAlphaMatch + ? `${numericAlphaMatch.groups?.letters ?? ""}${numericAlphaMatch.groups?.digits ?? ""}` + : ""; + + if (!swappedQuery) { + return primaryMatch; + } + + const swappedMatch = matchQuery(swappedQuery); + if (!swappedMatch.matches) { + return primaryMatch; + } + + return { matches: true, score: swappedMatch.score + 5 }; +} + +/** + * Filter and sort items by fuzzy match quality (best matches first). + * Supports whitespace- and slash-separated tokens: all tokens must match. + */ +export function fuzzyFilter(items: T[], query: string, getText: (item: T) => string): T[] { + if (!query.trim()) { + return items; + } + + const tokens = query + .trim() + .split(/[\s/]+/) + .filter((t) => t.length > 0); + + if (tokens.length === 0) { + return items; + } + + const results: { item: T; totalScore: number }[] = []; + + for (const item of items) { + const text = getText(item); + let totalScore = 0; + let allMatch = true; + + for (const token of tokens) { + const match = fuzzyMatch(token, text); + if (match.matches) { + totalScore += match.score; + } else { + allMatch = false; + break; + } + } + + if (allMatch) { + results.push({ item, totalScore }); + } + } + + results.sort((a, b) => a.totalScore - b.totalScore); + return results.map((r) => r.item); +} diff --git a/packages/tui/src/index.ts b/packages/tui/src/index.ts new file mode 100644 index 0000000000000000000000000000000000000000..d704092586a33839a0d8d579abfd907bccc14072 --- /dev/null +++ b/packages/tui/src/index.ts @@ -0,0 +1,156 @@ +// Core TUI interfaces and classes + +export { Marked, type Token, type Tokens } from "marked"; +// Autocomplete support +export { + type AutocompleteItem, + type AutocompleteProvider, + type AutocompleteSuggestions, + CombinedAutocompleteProvider, + type SlashCommand, +} from "./autocomplete.ts"; +// Components +export { Box } from "./components/box.ts"; +export { CancellableLoader } from "./components/cancellable-loader.ts"; +export { Editor, type EditorOptions, type EditorTheme } from "./components/editor.ts"; +export { HStack } from "./components/h-stack.ts"; +export { Image, type ImageOptions, type ImageTheme } from "./components/image.ts"; +export { Input } from "./components/input.ts"; +export { Loader, type LoaderIndicatorOptions } from "./components/loader.ts"; +export { type DefaultTextStyle, Markdown, type MarkdownOptions, type MarkdownTheme } from "./components/markdown.ts"; +export { MouseRegion, type MouseRegionHandler } from "./components/mouse-region.ts"; +export { + ScrollView, + type ScrollViewOptions, + type ScrollViewScrollbar, + type ScrollViewScrollToOptions, +} from "./components/scroll-view.ts"; +export { + type SelectItem, + SelectList, + type SelectListLayoutOptions, + type SelectListTheme, + type SelectListTruncatePrimaryContext, +} from "./components/select-list.ts"; +export { type SettingItem, SettingsList, type SettingsListTheme } from "./components/settings-list.ts"; +export { Spacer } from "./components/spacer.ts"; +export { Text } from "./components/text.ts"; +export { TruncatedText } from "./components/truncated-text.ts"; +export { + type StackChild, + type StackEntry, + type StackEntryOptions, + type StackOptions, + VStack, +} from "./components/v-stack.ts"; +// Editor component interface (for custom editors) +export type { EditorComponent } from "./editor-component.ts"; +// Fuzzy matching +export { type FuzzyMatch, fuzzyFilter, fuzzyMatch } from "./fuzzy.ts"; +// Keybindings +export { + getKeybindings, + type Keybinding, + type KeybindingConflict, + type KeybindingDefinition, + type KeybindingDefinitions, + type Keybindings, + type KeybindingsConfig, + KeybindingsManager, + setKeybindings, + TUI_KEYBINDINGS, +} from "./keybindings.ts"; +// Keyboard input handling +export { + decodeKittyPrintable, + isKeyRelease, + isKeyRepeat, + isKittyProtocolActive, + Key, + type KeyEventType, + type KeyId, + matchesKey, + parseKey, + setKittyProtocolActive, +} from "./keys.ts"; +// LaTeX rendering +export { type RenderLatexOptions, renderLatex } from "./latex.ts"; +// Native platform integration +export { getNativeClipboard, type NativeClipboard } from "./native-platform.ts"; +// Input buffering for batch splitting +export { StdinBuffer, type StdinBufferEventMap, type StdinBufferOptions } from "./stdin-buffer.ts"; +// Terminal interface and implementations +export { ProcessTerminal, type Terminal } from "./terminal.ts"; +// Terminal colors +export { + parseOsc11BackgroundColor, + parseTerminalColorSchemeReport, + type RgbColor, + type TerminalColorScheme, +} from "./terminal-colors.ts"; +// Terminal image support +export { + allocateImageId, + type CellDimensions, + calculateImageRows, + deleteAllKittyImages, + deleteKittyImage, + detectCapabilities, + encodeITerm2, + encodeKitty, + getCapabilities, + getCellDimensions, + getGifDimensions, + getImageDimensions, + getJpegDimensions, + getPngDimensions, + getWebpDimensions, + hyperlink, + type ImageDimensions, + type ImageProtocol, + type ImageRenderOptions, + imageFallback, + renderImage, + resetCapabilitiesCache, + setCapabilities, + setCapabilityOverrides, + setCellDimensions, + type TerminalCapabilities, +} from "./terminal-image.ts"; +export { + type Component, + Container, + CURSOR_MARKER, + compositeTuiLine, + type Focusable, + isFocusable, + isViewportTUI, + type OverlayAnchor, + type OverlayBounds, + type OverlayHandle, + type OverlayMargin, + type OverlayOptions, + type OverlayUnfocusOptions, + type SizeValue, + type TUI, + type TuiInputListener, + type TuiInputListenerResult, + type TuiMode, + type TuiMouseButton, + type TuiMouseEvent, + type TuiMouseEventResult, + type TuiMouseEventType, + type TuiStopOptions, + type ViewportTUI, +} from "./tui.ts"; +export { TuiAltScreen, type TuiAltScreenOptions } from "./tui-alt-screen.ts"; +export { TuiMainScreen, type TuiMainScreenRenderState } from "./tui-main-screen.ts"; +// Utilities +export { + getOsc8LinkAtColumn, + sliceByColumn, + stripTerminalSequences, + truncateToWidth, + visibleWidth, + wrapTextWithAnsi, +} from "./utils.ts"; diff --git a/packages/tui/src/keybindings.ts b/packages/tui/src/keybindings.ts new file mode 100644 index 0000000000000000000000000000000000000000..d6afb5ec39642c098b7d95e10ab6b57568e514b4 --- /dev/null +++ b/packages/tui/src/keybindings.ts @@ -0,0 +1,320 @@ +import { type KeyId, matchesKey } from "./keys.ts"; + +/** + * Global keybinding registry. + * Downstream packages can add keybindings via declaration merging. + */ +export interface Keybindings { + // Editor navigation and editing + "tui.editor.cursorUp": true; + "tui.editor.cursorDown": true; + "tui.editor.historyPrevious": true; + "tui.editor.historyNext": true; + "tui.editor.cursorLeft": true; + "tui.editor.cursorRight": true; + "tui.editor.cursorWordLeft": true; + "tui.editor.cursorWordRight": true; + "tui.editor.cursorLineStart": true; + "tui.editor.cursorLineEnd": true; + "tui.editor.jumpForward": true; + "tui.editor.jumpBackward": true; + "tui.editor.pageUp": true; + "tui.editor.pageDown": true; + "tui.editor.deleteCharBackward": true; + "tui.editor.deleteCharForward": true; + "tui.editor.deleteWordBackward": true; + "tui.editor.deleteWordForward": true; + "tui.editor.deleteToLineStart": true; + "tui.editor.deleteToLineEnd": true; + "tui.editor.yank": true; + "tui.editor.yankPop": true; + "tui.editor.undo": true; + // Generic input actions + "tui.input.newLine": true; + "tui.input.submit": true; + "tui.input.tab": true; + "tui.input.copy": true; + // Generic selection actions + "tui.select.up": true; + "tui.select.down": true; + "tui.select.pageUp": true; + "tui.select.pageDown": true; + "tui.select.confirm": true; + "tui.select.cancel": true; + // Alternate-screen viewport navigation + "tui.altScreen.pageUp": true; + "tui.altScreen.pageDown": true; + "tui.altScreen.halfPageUp": true; + "tui.altScreen.halfPageDown": true; + "tui.altScreen.lineUp": true; + "tui.altScreen.lineDown": true; + "tui.altScreen.previousPrompt": true; + "tui.altScreen.nextPrompt": true; + "tui.altScreen.search": true; + "tui.altScreen.searchNext": true; + "tui.altScreen.searchPrevious": true; + "tui.altScreen.searchClose": true; + "tui.altScreen.top": true; + "tui.altScreen.bottom": true; +} + +export type Keybinding = keyof Keybindings; + +export interface KeybindingDefinition { + defaultKeys: KeyId | KeyId[]; + description?: string; +} + +export type KeybindingDefinitions = Record; +export type KeybindingsConfig = Record; + +export const TUI_KEYBINDINGS = { + "tui.editor.cursorUp": { defaultKeys: "up", description: "Move cursor up" }, + "tui.editor.cursorDown": { defaultKeys: "down", description: "Move cursor down" }, + "tui.editor.historyPrevious": { + defaultKeys: [], + description: "Select previous prompt history entry", + }, + "tui.editor.historyNext": { + defaultKeys: [], + description: "Select next prompt history entry", + }, + "tui.editor.cursorLeft": { + defaultKeys: ["left", "ctrl+b"], + description: "Move cursor left", + }, + "tui.editor.cursorRight": { + defaultKeys: ["right", "ctrl+f"], + description: "Move cursor right", + }, + "tui.editor.cursorWordLeft": { + defaultKeys: ["alt+left", "ctrl+left", "alt+b"], + description: "Move cursor word left", + }, + "tui.editor.cursorWordRight": { + defaultKeys: ["alt+right", "ctrl+right", "alt+f"], + description: "Move cursor word right", + }, + "tui.editor.cursorLineStart": { + defaultKeys: ["home", "ctrl+home", "ctrl+a"], + description: "Move to line start", + }, + "tui.editor.cursorLineEnd": { + defaultKeys: ["end", "ctrl+end", "ctrl+e"], + description: "Move to line end", + }, + "tui.editor.jumpForward": { + defaultKeys: "ctrl+]", + description: "Jump forward to character", + }, + "tui.editor.jumpBackward": { + defaultKeys: "ctrl+alt+]", + description: "Jump backward to character", + }, + "tui.editor.pageUp": { defaultKeys: ["pageUp", "ctrl+pageUp"], description: "Page up" }, + "tui.editor.pageDown": { defaultKeys: ["pageDown", "ctrl+pageDown"], description: "Page down" }, + "tui.editor.deleteCharBackward": { + defaultKeys: "backspace", + description: "Delete character backward", + }, + "tui.editor.deleteCharForward": { + defaultKeys: ["delete", "ctrl+d"], + description: "Delete character forward", + }, + "tui.editor.deleteWordBackward": { + defaultKeys: ["ctrl+w", "alt+backspace"], + description: "Delete word backward", + }, + "tui.editor.deleteWordForward": { + defaultKeys: ["alt+d", "alt+delete"], + description: "Delete word forward", + }, + "tui.editor.deleteToLineStart": { + defaultKeys: "ctrl+u", + description: "Delete to line start", + }, + "tui.editor.deleteToLineEnd": { + defaultKeys: "ctrl+k", + description: "Delete to line end", + }, + "tui.editor.yank": { defaultKeys: "ctrl+y", description: "Yank" }, + "tui.editor.yankPop": { defaultKeys: "alt+y", description: "Yank pop" }, + "tui.editor.undo": { defaultKeys: "ctrl+-", description: "Undo" }, + "tui.input.newLine": { defaultKeys: ["shift+enter", "ctrl+j"], description: "Insert newline" }, + "tui.input.submit": { defaultKeys: "enter", description: "Submit input" }, + "tui.input.tab": { defaultKeys: "tab", description: "Tab / autocomplete" }, + "tui.input.copy": { defaultKeys: "ctrl+c", description: "Copy selection" }, + "tui.select.up": { defaultKeys: "up", description: "Move selection up" }, + "tui.select.down": { defaultKeys: "down", description: "Move selection down" }, + "tui.select.pageUp": { defaultKeys: "pageUp", description: "Selection page up" }, + "tui.select.pageDown": { + defaultKeys: "pageDown", + description: "Selection page down", + }, + "tui.select.confirm": { defaultKeys: "enter", description: "Confirm selection" }, + "tui.select.cancel": { + defaultKeys: ["escape", "ctrl+c"], + description: "Cancel selection", + }, + // These intentionally shadow the unmodified editor bindings in fullscreen mode. + "tui.altScreen.pageUp": { + defaultKeys: "pageUp", + description: "Scroll viewport up one page", + }, + "tui.altScreen.pageDown": { + defaultKeys: "pageDown", + description: "Scroll viewport down one page", + }, + "tui.altScreen.halfPageUp": { + defaultKeys: [], + description: "Scroll viewport up half a page", + }, + "tui.altScreen.halfPageDown": { + defaultKeys: [], + description: "Scroll viewport down half a page", + }, + "tui.altScreen.lineUp": { + defaultKeys: [], + description: "Scroll viewport up one line", + }, + "tui.altScreen.lineDown": { + defaultKeys: [], + description: "Scroll viewport down one line", + }, + "tui.altScreen.previousPrompt": { + defaultKeys: ["ctrl+shift+up", "ctrl+up"], + description: "Jump to previous semantic prompt", + }, + "tui.altScreen.nextPrompt": { + defaultKeys: ["ctrl+shift+down", "ctrl+down"], + description: "Jump to next semantic prompt", + }, + "tui.altScreen.search": { + defaultKeys: "ctrl+shift+f", + description: "Search the primary scroll view", + }, + "tui.altScreen.searchNext": { + defaultKeys: ["enter", "ctrl+g"], + description: "Select the next search match", + }, + "tui.altScreen.searchPrevious": { + defaultKeys: ["shift+enter", "ctrl+shift+g"], + description: "Select the previous search match", + }, + "tui.altScreen.searchClose": { + defaultKeys: "escape", + description: "Close transcript search", + }, + "tui.altScreen.top": { defaultKeys: "home", description: "Scroll viewport to top" }, + "tui.altScreen.bottom": { defaultKeys: "end", description: "Scroll viewport to bottom" }, +} as const satisfies KeybindingDefinitions; + +export interface KeybindingConflict { + key: KeyId; + keybindings: string[]; +} + +function normalizeKeys(keys: KeyId | KeyId[] | undefined): KeyId[] { + if (keys === undefined) return []; + const keyList = Array.isArray(keys) ? keys : [keys]; + const seen = new Set(); + const result: KeyId[] = []; + for (const key of keyList) { + if (!seen.has(key)) { + seen.add(key); + result.push(key); + } + } + return result; +} + +export class KeybindingsManager { + private definitions: KeybindingDefinitions; + private userBindings: KeybindingsConfig; + private keysById = new Map(); + private conflicts: KeybindingConflict[] = []; + + constructor(definitions: KeybindingDefinitions, userBindings: KeybindingsConfig = {}) { + this.definitions = definitions; + this.userBindings = userBindings; + this.rebuild(); + } + + private rebuild(): void { + this.keysById.clear(); + this.conflicts = []; + + const userClaims = new Map>(); + for (const [keybinding, keys] of Object.entries(this.userBindings)) { + if (!(keybinding in this.definitions)) continue; + for (const key of normalizeKeys(keys)) { + const claimants = userClaims.get(key) ?? new Set(); + claimants.add(keybinding as Keybinding); + userClaims.set(key, claimants); + } + } + + for (const [key, keybindings] of userClaims) { + if (keybindings.size > 1) { + this.conflicts.push({ key, keybindings: [...keybindings] }); + } + } + + for (const [id, definition] of Object.entries(this.definitions)) { + const userKeys = this.userBindings[id]; + const keys = userKeys === undefined ? normalizeKeys(definition.defaultKeys) : normalizeKeys(userKeys); + this.keysById.set(id as Keybinding, keys); + } + } + + matches(data: string, keybinding: Keybinding): boolean { + const keys = this.keysById.get(keybinding) ?? []; + for (const key of keys) { + if (matchesKey(data, key)) return true; + } + return false; + } + + getKeys(keybinding: Keybinding): KeyId[] { + return [...(this.keysById.get(keybinding) ?? [])]; + } + + getDefinition(keybinding: Keybinding): KeybindingDefinition { + return this.definitions[keybinding]; + } + + getConflicts(): KeybindingConflict[] { + return this.conflicts.map((conflict) => ({ ...conflict, keybindings: [...conflict.keybindings] })); + } + + setUserBindings(userBindings: KeybindingsConfig): void { + this.userBindings = userBindings; + this.rebuild(); + } + + getUserBindings(): KeybindingsConfig { + return { ...this.userBindings }; + } + + getResolvedBindings(): KeybindingsConfig { + const resolved: KeybindingsConfig = {}; + for (const id of Object.keys(this.definitions)) { + const keys = this.keysById.get(id as Keybinding) ?? []; + resolved[id] = keys.length === 1 ? keys[0]! : [...keys]; + } + return resolved; + } +} + +let globalKeybindings: KeybindingsManager | null = null; + +export function setKeybindings(keybindings: KeybindingsManager): void { + globalKeybindings = keybindings; +} + +export function getKeybindings(): KeybindingsManager { + if (!globalKeybindings) { + globalKeybindings = new KeybindingsManager(TUI_KEYBINDINGS); + } + return globalKeybindings; +} diff --git a/packages/tui/src/keys.ts b/packages/tui/src/keys.ts new file mode 100644 index 0000000000000000000000000000000000000000..8b6f8a0a708cc9a1c9b795ff1ec9766c4209114b --- /dev/null +++ b/packages/tui/src/keys.ts @@ -0,0 +1,1401 @@ +/** + * Keyboard input handling for terminal applications. + * + * Supports both legacy terminal sequences and Kitty keyboard protocol. + * See: https://sw.kovidgoyal.net/kitty/keyboard-protocol/ + * Reference: https://github.com/sst/opentui/blob/7da92b4088aebfe27b9f691c04163a48821e49fd/packages/core/src/lib/parse.keypress.ts + * + * Symbol keys are also supported, however some ctrl+symbol combos + * overlap with ASCII codes, e.g. ctrl+[ = ESC. + * See: https://sw.kovidgoyal.net/kitty/keyboard-protocol/#legacy-ctrl-mapping-of-ascii-keys + * Those can still be * used for ctrl+shift combos + * + * API: + * - matchesKey(data, keyId) - Check if input matches a key identifier + * - parseKey(data) - Parse input and return the key identifier + * - Key - Helper object for creating typed key identifiers + * - setKittyProtocolActive(active) - Set global Kitty protocol state + * - isKittyProtocolActive() - Query global Kitty protocol state + */ + +// ============================================================================= +// Global Kitty Protocol State +// ============================================================================= + +let _kittyProtocolActive = false; + +/** + * Set the global Kitty keyboard protocol state. + * Called by ProcessTerminal after detecting protocol support. + */ +export function setKittyProtocolActive(active: boolean): void { + _kittyProtocolActive = active; +} + +/** + * Query whether Kitty keyboard protocol is currently active. + */ +export function isKittyProtocolActive(): boolean { + return _kittyProtocolActive; +} + +// ============================================================================= +// Type-Safe Key Identifiers +// ============================================================================= + +type Letter = + | "a" + | "b" + | "c" + | "d" + | "e" + | "f" + | "g" + | "h" + | "i" + | "j" + | "k" + | "l" + | "m" + | "n" + | "o" + | "p" + | "q" + | "r" + | "s" + | "t" + | "u" + | "v" + | "w" + | "x" + | "y" + | "z"; + +type Digit = "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9"; + +type SymbolKey = + | "`" + | "-" + | "=" + | "[" + | "]" + | "\\" + | ";" + | "'" + | "," + | "." + | "/" + | "!" + | "@" + | "#" + | "$" + | "%" + | "^" + | "&" + | "*" + | "(" + | ")" + | "_" + | "+" + | "|" + | "~" + | "{" + | "}" + | ":" + | "<" + | ">" + | "?"; + +type SpecialKey = + | "escape" + | "esc" + | "enter" + | "return" + | "tab" + | "space" + | "backspace" + | "delete" + | "insert" + | "clear" + | "home" + | "end" + | "pageUp" + | "pageDown" + | "up" + | "down" + | "left" + | "right" + | "f1" + | "f2" + | "f3" + | "f4" + | "f5" + | "f6" + | "f7" + | "f8" + | "f9" + | "f10" + | "f11" + | "f12"; + +type BaseKey = Letter | Digit | SymbolKey | SpecialKey; +type ModifierName = "ctrl" | "shift" | "alt" | "super"; + +type ModifiedKeyId = { + [M in RemainingModifiers]: `${M}+${Key}` | `${M}+${ModifiedKeyId>}`; +}[RemainingModifiers]; + +/** + * Union type of all valid key identifiers. + * Provides autocomplete and catches typos at compile time. + */ +export type KeyId = BaseKey | ModifiedKeyId; + +/** + * Helper object for creating typed key identifiers with autocomplete. + * + * Usage: + * - Key.escape, Key.enter, Key.tab, etc. for special keys + * - Key.backtick, Key.comma, Key.period, etc. for symbol keys + * - Key.ctrl("c"), Key.alt("x"), Key.super("k") for single modifiers + * - Key.ctrlShift("p"), Key.ctrlAlt("x"), Key.ctrlSuper("k") for combined modifiers + */ +export const Key = { + // Special keys + escape: "escape" as const, + esc: "esc" as const, + enter: "enter" as const, + return: "return" as const, + tab: "tab" as const, + space: "space" as const, + backspace: "backspace" as const, + delete: "delete" as const, + insert: "insert" as const, + clear: "clear" as const, + home: "home" as const, + end: "end" as const, + pageUp: "pageUp" as const, + pageDown: "pageDown" as const, + up: "up" as const, + down: "down" as const, + left: "left" as const, + right: "right" as const, + f1: "f1" as const, + f2: "f2" as const, + f3: "f3" as const, + f4: "f4" as const, + f5: "f5" as const, + f6: "f6" as const, + f7: "f7" as const, + f8: "f8" as const, + f9: "f9" as const, + f10: "f10" as const, + f11: "f11" as const, + f12: "f12" as const, + + // Symbol keys + backtick: "`" as const, + hyphen: "-" as const, + equals: "=" as const, + leftbracket: "[" as const, + rightbracket: "]" as const, + backslash: "\\" as const, + semicolon: ";" as const, + quote: "'" as const, + comma: "," as const, + period: "." as const, + slash: "/" as const, + exclamation: "!" as const, + at: "@" as const, + hash: "#" as const, + dollar: "$" as const, + percent: "%" as const, + caret: "^" as const, + ampersand: "&" as const, + asterisk: "*" as const, + leftparen: "(" as const, + rightparen: ")" as const, + underscore: "_" as const, + plus: "+" as const, + pipe: "|" as const, + tilde: "~" as const, + leftbrace: "{" as const, + rightbrace: "}" as const, + colon: ":" as const, + lessthan: "<" as const, + greaterthan: ">" as const, + question: "?" as const, + + // Single modifiers + ctrl: (key: K): `ctrl+${K}` => `ctrl+${key}`, + shift: (key: K): `shift+${K}` => `shift+${key}`, + alt: (key: K): `alt+${K}` => `alt+${key}`, + super: (key: K): `super+${K}` => `super+${key}`, + + // Combined modifiers + ctrlShift: (key: K): `ctrl+shift+${K}` => `ctrl+shift+${key}`, + shiftCtrl: (key: K): `shift+ctrl+${K}` => `shift+ctrl+${key}`, + ctrlAlt: (key: K): `ctrl+alt+${K}` => `ctrl+alt+${key}`, + altCtrl: (key: K): `alt+ctrl+${K}` => `alt+ctrl+${key}`, + shiftAlt: (key: K): `shift+alt+${K}` => `shift+alt+${key}`, + altShift: (key: K): `alt+shift+${K}` => `alt+shift+${key}`, + ctrlSuper: (key: K): `ctrl+super+${K}` => `ctrl+super+${key}`, + superCtrl: (key: K): `super+ctrl+${K}` => `super+ctrl+${key}`, + shiftSuper: (key: K): `shift+super+${K}` => `shift+super+${key}`, + superShift: (key: K): `super+shift+${K}` => `super+shift+${key}`, + altSuper: (key: K): `alt+super+${K}` => `alt+super+${key}`, + superAlt: (key: K): `super+alt+${K}` => `super+alt+${key}`, + + // Triple modifiers + ctrlShiftAlt: (key: K): `ctrl+shift+alt+${K}` => `ctrl+shift+alt+${key}`, + ctrlShiftSuper: (key: K): `ctrl+shift+super+${K}` => `ctrl+shift+super+${key}`, +} as const; + +// ============================================================================= +// Constants +// ============================================================================= + +const SYMBOL_KEYS = new Set([ + "`", + "-", + "=", + "[", + "]", + "\\", + ";", + "'", + ",", + ".", + "/", + "!", + "@", + "#", + "$", + "%", + "^", + "&", + "*", + "(", + ")", + "_", + "+", + "|", + "~", + "{", + "}", + ":", + "<", + ">", + "?", +]); + +const MODIFIERS = { + shift: 1, + alt: 2, + ctrl: 4, + super: 8, +} as const; + +const LOCK_MASK = 64 + 128; // Caps Lock + Num Lock + +const CODEPOINTS = { + escape: 27, + tab: 9, + enter: 13, + space: 32, + backspace: 127, + kpEnter: 57414, // Numpad Enter (Kitty protocol) +} as const; + +const ARROW_CODEPOINTS = { + up: -1, + down: -2, + right: -3, + left: -4, +} as const; + +const FUNCTIONAL_CODEPOINTS = { + delete: -10, + insert: -11, + pageUp: -12, + pageDown: -13, + home: -14, + end: -15, +} as const; + +const KITTY_FUNCTIONAL_KEY_EQUIVALENTS = new Map([ + [57399, 48], // KP_0 -> 0 + [57400, 49], // KP_1 -> 1 + [57401, 50], // KP_2 -> 2 + [57402, 51], // KP_3 -> 3 + [57403, 52], // KP_4 -> 4 + [57404, 53], // KP_5 -> 5 + [57405, 54], // KP_6 -> 6 + [57406, 55], // KP_7 -> 7 + [57407, 56], // KP_8 -> 8 + [57408, 57], // KP_9 -> 9 + [57409, 46], // KP_DECIMAL -> . + [57410, 47], // KP_DIVIDE -> / + [57411, 42], // KP_MULTIPLY -> * + [57412, 45], // KP_SUBTRACT -> - + [57413, 43], // KP_ADD -> + + [57415, 61], // KP_EQUAL -> = + [57416, 44], // KP_SEPARATOR -> , + [57417, ARROW_CODEPOINTS.left], + [57418, ARROW_CODEPOINTS.right], + [57419, ARROW_CODEPOINTS.up], + [57420, ARROW_CODEPOINTS.down], + [57421, FUNCTIONAL_CODEPOINTS.pageUp], + [57422, FUNCTIONAL_CODEPOINTS.pageDown], + [57423, FUNCTIONAL_CODEPOINTS.home], + [57424, FUNCTIONAL_CODEPOINTS.end], + [57425, FUNCTIONAL_CODEPOINTS.insert], + [57426, FUNCTIONAL_CODEPOINTS.delete], +]); + +function normalizeKittyFunctionalCodepoint(codepoint: number): number { + return KITTY_FUNCTIONAL_KEY_EQUIVALENTS.get(codepoint) ?? codepoint; +} + +function normalizeShiftedLetterIdentityCodepoint(codepoint: number, modifier: number): number { + const effectiveModifier = modifier & ~LOCK_MASK; + if ((effectiveModifier & MODIFIERS.shift) !== 0 && codepoint >= 65 && codepoint <= 90) { + return codepoint + 32; + } + return codepoint; +} + +const LEGACY_KEY_SEQUENCES = { + up: ["\x1b[A", "\x1bOA"], + down: ["\x1b[B", "\x1bOB"], + right: ["\x1b[C", "\x1bOC"], + left: ["\x1b[D", "\x1bOD"], + home: ["\x1b[H", "\x1bOH", "\x1b[1~", "\x1b[7~"], + end: ["\x1b[F", "\x1bOF", "\x1b[4~", "\x1b[8~"], + insert: ["\x1b[2~"], + delete: ["\x1b[3~"], + pageUp: ["\x1b[5~", "\x1b[[5~"], + pageDown: ["\x1b[6~", "\x1b[[6~"], + clear: ["\x1b[E", "\x1bOE"], + f1: ["\x1bOP", "\x1b[11~", "\x1b[[A"], + f2: ["\x1bOQ", "\x1b[12~", "\x1b[[B"], + f3: ["\x1bOR", "\x1b[13~", "\x1b[[C"], + f4: ["\x1bOS", "\x1b[14~", "\x1b[[D"], + f5: ["\x1b[15~", "\x1b[[E"], + f6: ["\x1b[17~"], + f7: ["\x1b[18~"], + f8: ["\x1b[19~"], + f9: ["\x1b[20~"], + f10: ["\x1b[21~"], + f11: ["\x1b[23~"], + f12: ["\x1b[24~"], +} as const; + +const LEGACY_SHIFT_SEQUENCES = { + up: ["\x1b[a"], + down: ["\x1b[b"], + right: ["\x1b[c"], + left: ["\x1b[d"], + clear: ["\x1b[e"], + insert: ["\x1b[2$"], + delete: ["\x1b[3$"], + pageUp: ["\x1b[5$"], + pageDown: ["\x1b[6$"], + home: ["\x1b[7$"], + end: ["\x1b[8$"], +} as const; + +const LEGACY_CTRL_SEQUENCES = { + up: ["\x1bOa"], + down: ["\x1bOb"], + right: ["\x1bOc"], + left: ["\x1bOd"], + clear: ["\x1bOe"], + insert: ["\x1b[2^"], + delete: ["\x1b[3^"], + pageUp: ["\x1b[5^"], + pageDown: ["\x1b[6^"], + home: ["\x1b[7^"], + end: ["\x1b[8^"], +} as const; + +const LEGACY_SEQUENCE_KEY_IDS: Record = { + "\x1bOA": "up", + "\x1bOB": "down", + "\x1bOC": "right", + "\x1bOD": "left", + "\x1bOH": "home", + "\x1bOF": "end", + "\x1b[E": "clear", + "\x1bOE": "clear", + "\x1bOe": "ctrl+clear", + "\x1b[e": "shift+clear", + "\x1b[2~": "insert", + "\x1b[2$": "shift+insert", + "\x1b[2^": "ctrl+insert", + "\x1b[3$": "shift+delete", + "\x1b[3^": "ctrl+delete", + "\x1b[[5~": "pageUp", + "\x1b[[6~": "pageDown", + "\x1b[a": "shift+up", + "\x1b[b": "shift+down", + "\x1b[c": "shift+right", + "\x1b[d": "shift+left", + "\x1bOa": "ctrl+up", + "\x1bOb": "ctrl+down", + "\x1bOc": "ctrl+right", + "\x1bOd": "ctrl+left", + "\x1b[5$": "shift+pageUp", + "\x1b[6$": "shift+pageDown", + "\x1b[7$": "shift+home", + "\x1b[8$": "shift+end", + "\x1b[5^": "ctrl+pageUp", + "\x1b[6^": "ctrl+pageDown", + "\x1b[7^": "ctrl+home", + "\x1b[8^": "ctrl+end", + "\x1bOP": "f1", + "\x1bOQ": "f2", + "\x1bOR": "f3", + "\x1bOS": "f4", + "\x1b[11~": "f1", + "\x1b[12~": "f2", + "\x1b[13~": "f3", + "\x1b[14~": "f4", + "\x1b[[A": "f1", + "\x1b[[B": "f2", + "\x1b[[C": "f3", + "\x1b[[D": "f4", + "\x1b[[E": "f5", + "\x1b[15~": "f5", + "\x1b[17~": "f6", + "\x1b[18~": "f7", + "\x1b[19~": "f8", + "\x1b[20~": "f9", + "\x1b[21~": "f10", + "\x1b[23~": "f11", + "\x1b[24~": "f12", + "\x1bb": "alt+left", + "\x1bf": "alt+right", + "\x1bp": "alt+up", + "\x1bn": "alt+down", +} as const; + +type LegacyModifierKey = keyof typeof LEGACY_SHIFT_SEQUENCES; + +const matchesLegacySequence = (data: string, sequences: readonly string[]): boolean => sequences.includes(data); + +const matchesLegacyModifierSequence = (data: string, key: LegacyModifierKey, modifier: number): boolean => { + if (modifier === MODIFIERS.shift) { + return matchesLegacySequence(data, LEGACY_SHIFT_SEQUENCES[key]); + } + if (modifier === MODIFIERS.ctrl) { + return matchesLegacySequence(data, LEGACY_CTRL_SEQUENCES[key]); + } + return false; +}; + +// ============================================================================= +// Kitty Protocol Parsing +// ============================================================================= + +/** + * Event types from Kitty keyboard protocol (flag 2) + * 1 = key press, 2 = key repeat, 3 = key release + */ +export type KeyEventType = "press" | "repeat" | "release"; + +interface ParsedKittySequence { + codepoint: number; + shiftedKey?: number; // Shifted version of the key (when shift is pressed) + baseLayoutKey?: number; // Key in standard PC-101 layout (for non-Latin layouts) + modifier: number; + eventType: KeyEventType; +} + +interface ParsedModifyOtherKeysSequence { + codepoint: number; + modifier: number; +} + +// Store the last parsed event type for isKeyRelease() to query +let _lastEventType: KeyEventType = "press"; + +/** + * Check if the last parsed key event was a key release. + * Only meaningful when Kitty keyboard protocol with flag 2 is active. + */ +export function isKeyRelease(data: string): boolean { + // Don't treat bracketed paste content as key release, even if it contains + // patterns like ":3F" (e.g., bluetooth MAC addresses like "90:62:3F:A5"). + // Terminal.ts re-wraps paste content with bracketed paste markers before + // passing to TUI, so pasted data will always contain \x1b[200~. + if (data.includes("\x1b[200~")) { + return false; + } + + // Quick check: release events with flag 2 contain ":3" + // Format: \x1b[;:3u + if ( + data.includes(":3u") || + data.includes(":3~") || + data.includes(":3A") || + data.includes(":3B") || + data.includes(":3C") || + data.includes(":3D") || + data.includes(":3H") || + data.includes(":3F") + ) { + return true; + } + return false; +} + +/** + * Check if the last parsed key event was a key repeat. + * Only meaningful when Kitty keyboard protocol with flag 2 is active. + */ +export function isKeyRepeat(data: string): boolean { + // Don't treat bracketed paste content as key repeat, even if it contains + // patterns like ":2F". See isKeyRelease() for details. + if (data.includes("\x1b[200~")) { + return false; + } + + if ( + data.includes(":2u") || + data.includes(":2~") || + data.includes(":2A") || + data.includes(":2B") || + data.includes(":2C") || + data.includes(":2D") || + data.includes(":2H") || + data.includes(":2F") + ) { + return true; + } + return false; +} + +function parseEventType(eventTypeStr: string | undefined): KeyEventType { + if (!eventTypeStr) return "press"; + const eventType = parseInt(eventTypeStr, 10); + if (eventType === 2) return "repeat"; + if (eventType === 3) return "release"; + return "press"; +} + +function parseKittySequence(data: string): ParsedKittySequence | null { + // CSI u format with alternate keys (flag 4): + // \x1b[u + // \x1b[;u + // \x1b[;:u + // \x1b[:;u + // \x1b[::;u + // \x1b[::;u (no shifted key, only base) + // + // With flag 2, event type is appended after modifier colon: 1=press, 2=repeat, 3=release + // With flag 4, alternate keys are appended after codepoint with colons + const csiUMatch = data.match(/^\x1b\[(\d+)(?::(\d*))?(?::(\d+))?(?:;(\d+))?(?::(\d+))?u$/); + if (csiUMatch) { + const codepoint = parseInt(csiUMatch[1]!, 10); + const shiftedKey = csiUMatch[2] && csiUMatch[2].length > 0 ? parseInt(csiUMatch[2], 10) : undefined; + const baseLayoutKey = csiUMatch[3] ? parseInt(csiUMatch[3], 10) : undefined; + const modValue = csiUMatch[4] ? parseInt(csiUMatch[4], 10) : 1; + const eventType = parseEventType(csiUMatch[5]); + _lastEventType = eventType; + return { codepoint, shiftedKey, baseLayoutKey, modifier: modValue - 1, eventType }; + } + + // Arrow keys with modifier: \x1b[1;A/B/C/D or \x1b[1;:A/B/C/D + const arrowMatch = data.match(/^\x1b\[1;(\d+)(?::(\d+))?([ABCD])$/); + if (arrowMatch) { + const modValue = parseInt(arrowMatch[1]!, 10); + const eventType = parseEventType(arrowMatch[2]); + const arrowCodes: Record = { A: -1, B: -2, C: -3, D: -4 }; + _lastEventType = eventType; + return { codepoint: arrowCodes[arrowMatch[3]!]!, modifier: modValue - 1, eventType }; + } + + // Functional keys: \x1b[~ or \x1b[;~ or \x1b[;:~ + const funcMatch = data.match(/^\x1b\[(\d+)(?:;(\d+))?(?::(\d+))?~$/); + if (funcMatch) { + const keyNum = parseInt(funcMatch[1]!, 10); + const modValue = funcMatch[2] ? parseInt(funcMatch[2], 10) : 1; + const eventType = parseEventType(funcMatch[3]); + const funcCodes: Record = { + 2: FUNCTIONAL_CODEPOINTS.insert, + 3: FUNCTIONAL_CODEPOINTS.delete, + 5: FUNCTIONAL_CODEPOINTS.pageUp, + 6: FUNCTIONAL_CODEPOINTS.pageDown, + 7: FUNCTIONAL_CODEPOINTS.home, + 8: FUNCTIONAL_CODEPOINTS.end, + }; + const codepoint = funcCodes[keyNum]; + if (codepoint !== undefined) { + _lastEventType = eventType; + return { codepoint, modifier: modValue - 1, eventType }; + } + } + + // Home/End with modifier: \x1b[1;H/F or \x1b[1;:H/F + const homeEndMatch = data.match(/^\x1b\[1;(\d+)(?::(\d+))?([HF])$/); + if (homeEndMatch) { + const modValue = parseInt(homeEndMatch[1]!, 10); + const eventType = parseEventType(homeEndMatch[2]); + const codepoint = homeEndMatch[3] === "H" ? FUNCTIONAL_CODEPOINTS.home : FUNCTIONAL_CODEPOINTS.end; + _lastEventType = eventType; + return { codepoint, modifier: modValue - 1, eventType }; + } + + return null; +} + +function matchesKittySequence(data: string, expectedCodepoint: number, expectedModifier: number): boolean { + const parsed = parseKittySequence(data); + if (!parsed) return false; + const actualMod = parsed.modifier & ~LOCK_MASK; + const expectedMod = expectedModifier & ~LOCK_MASK; + + // Check if modifiers match + if (actualMod !== expectedMod) return false; + + const normalizedCodepoint = normalizeShiftedLetterIdentityCodepoint( + normalizeKittyFunctionalCodepoint(parsed.codepoint), + parsed.modifier, + ); + const normalizedExpectedCodepoint = normalizeShiftedLetterIdentityCodepoint( + normalizeKittyFunctionalCodepoint(expectedCodepoint), + expectedModifier, + ); + + // Primary match: codepoint matches directly after normalizing functional keys + if (normalizedCodepoint === normalizedExpectedCodepoint) return true; + + // Alternate match: use base layout key for non-Latin keyboard layouts. + // This allows Ctrl+С (Cyrillic) to match Ctrl+c (Latin) when terminal reports + // the base layout key (the key in standard PC-101 layout). + // + // Only fall back to base layout key when the codepoint is NOT already a + // recognized Latin letter (a-z) or symbol (e.g., /, -, [, ;, etc.). + // When the codepoint is a recognized key, it is authoritative regardless + // of physical key position. This prevents remapped layouts (Dvorak, Colemak, + // xremap, etc.) from causing false matches: both letters and symbols move + // to different physical positions, so Ctrl+K could falsely match Ctrl+V + // (letter remapping) and Ctrl+/ could falsely match Ctrl+[ (symbol remapping) + // if the base layout key were always considered. + if (parsed.baseLayoutKey !== undefined && parsed.baseLayoutKey === expectedCodepoint) { + const cp = normalizedCodepoint; + const isLatinLetter = cp >= 97 && cp <= 122; // a-z + const isKnownSymbol = SYMBOL_KEYS.has(String.fromCharCode(cp)); + if (!isLatinLetter && !isKnownSymbol) return true; + } + + return false; +} + +function parseModifyOtherKeysSequence(data: string): ParsedModifyOtherKeysSequence | null { + const match = data.match(/^\x1b\[27;(\d+);(\d+)~$/); + if (!match) return null; + const modValue = parseInt(match[1]!, 10); + const codepoint = parseInt(match[2]!, 10); + return { codepoint, modifier: modValue - 1 }; +} + +/** + * Match xterm modifyOtherKeys format: CSI 27 ; modifiers ; keycode ~ + * This is used by terminals when Kitty protocol is not enabled. + * Modifier values are 1-indexed: 2=shift, 3=alt, 5=ctrl, etc. + */ +function matchesModifyOtherKeys(data: string, expectedKeycode: number, expectedModifier: number): boolean { + const parsed = parseModifyOtherKeysSequence(data); + if (!parsed) return false; + return parsed.codepoint === expectedKeycode && parsed.modifier === expectedModifier; +} + +function isWindowsTerminalSession(): boolean { + return ( + Boolean(process.env.WT_SESSION) && !process.env.SSH_CONNECTION && !process.env.SSH_CLIENT && !process.env.SSH_TTY + ); +} + +/** + * Raw 0x08 (BS) is ambiguous in legacy terminals. + * + * - Windows Terminal uses it for Ctrl+Backspace. + * - Some legacy terminals and tmux setups send it for plain Backspace. + * + * Prefer explicit Kitty / CSI-u / modifyOtherKeys sequences whenever they are + * available. Fall back to a Windows Terminal heuristic only for raw BS bytes. + */ +function matchesRawBackspace(data: string, expectedModifier: number): boolean { + if (data === "\x7f") return expectedModifier === 0; + if (data !== "\x08") return false; + return isWindowsTerminalSession() ? expectedModifier === MODIFIERS.ctrl : expectedModifier === 0; +} + +// ============================================================================= +// Generic Key Matching +// ============================================================================= + +/** + * Get the control character for a key. + * Uses the universal formula: code & 0x1f (mask to lower 5 bits) + * + * Works for: + * - Letters a-z → 1-26 + * - Symbols [\]_ → 27, 28, 29, 31 + * - Also maps - to same as _ (same physical key on US keyboards) + */ +function rawCtrlChar(key: string): string | null { + const char = key.toLowerCase(); + const code = char.charCodeAt(0); + if ((code >= 97 && code <= 122) || char === "[" || char === "\\" || char === "]" || char === "_") { + return String.fromCharCode(code & 0x1f); + } + // Handle - as _ (same physical key on US keyboards) + if (char === "-") { + return String.fromCharCode(31); // Same as Ctrl+_ + } + return null; +} + +function isDigitKey(key: string): boolean { + return key >= "0" && key <= "9"; +} + +function matchesPrintableModifyOtherKeys(data: string, expectedKeycode: number, expectedModifier: number): boolean { + if (expectedModifier === 0) return false; + const parsed = parseModifyOtherKeysSequence(data); + if (!parsed || parsed.modifier !== expectedModifier) return false; + return ( + normalizeShiftedLetterIdentityCodepoint(parsed.codepoint, parsed.modifier) === + normalizeShiftedLetterIdentityCodepoint(expectedKeycode, expectedModifier) + ); +} + +function formatKeyNameWithModifiers(keyName: string, modifier: number): string | undefined { + const mods: string[] = []; + const effectiveMod = modifier & ~LOCK_MASK; + const supportedModifierMask = MODIFIERS.shift | MODIFIERS.ctrl | MODIFIERS.alt | MODIFIERS.super; + if ((effectiveMod & ~supportedModifierMask) !== 0) return undefined; + if (effectiveMod & MODIFIERS.shift) mods.push("shift"); + if (effectiveMod & MODIFIERS.ctrl) mods.push("ctrl"); + if (effectiveMod & MODIFIERS.alt) mods.push("alt"); + if (effectiveMod & MODIFIERS.super) mods.push("super"); + return mods.length > 0 ? `${mods.join("+")}+${keyName}` : keyName; +} + +function parseKeyId( + keyId: string, +): { key: string; ctrl: boolean; shift: boolean; alt: boolean; super: boolean } | null { + const parts = keyId.toLowerCase().split("+"); + const key = parts[parts.length - 1]; + if (!key) return null; + return { + key, + ctrl: parts.includes("ctrl"), + shift: parts.includes("shift"), + alt: parts.includes("alt"), + super: parts.includes("super"), + }; +} + +/** + * Match input data against a key identifier string. + * + * Supported key identifiers: + * - Single keys: "escape", "tab", "enter", "backspace", "delete", "home", "end", "space" + * - Arrow keys: "up", "down", "left", "right" + * - Ctrl combinations: "ctrl+c", "ctrl+z", etc. + * - Shift combinations: "shift+tab", "shift+enter" + * - Alt combinations: "alt+enter", "alt+backspace" + * - Super combinations: "super+k", "super+enter" + * - Combined modifiers: "shift+ctrl+p", "ctrl+alt+x", "ctrl+super+k" + * + * Use the Key helper for autocomplete: Key.ctrl("c"), Key.escape, Key.ctrlShift("p"), Key.super("k") + * + * @param data - Raw input data from terminal + * @param keyId - Key identifier (e.g., "ctrl+c", "escape", Key.ctrl("c")) + */ +export function matchesKey(data: string, keyId: KeyId): boolean { + const parsed = parseKeyId(keyId); + if (!parsed) return false; + + const { key, ctrl, shift, alt, super: superModifier } = parsed; + let modifier = 0; + if (shift) modifier |= MODIFIERS.shift; + if (alt) modifier |= MODIFIERS.alt; + if (ctrl) modifier |= MODIFIERS.ctrl; + if (superModifier) modifier |= MODIFIERS.super; + + switch (key) { + case "escape": + case "esc": + if (modifier !== 0) return false; + return ( + data === "\x1b" || + matchesKittySequence(data, CODEPOINTS.escape, 0) || + matchesModifyOtherKeys(data, CODEPOINTS.escape, 0) + ); + + case "space": + if (!_kittyProtocolActive) { + if (modifier === MODIFIERS.ctrl && data === "\x00") { + return true; + } + if (modifier === MODIFIERS.alt && data === "\x1b ") { + return true; + } + } + if (modifier === 0) { + return ( + data === " " || + matchesKittySequence(data, CODEPOINTS.space, 0) || + matchesModifyOtherKeys(data, CODEPOINTS.space, 0) + ); + } + return ( + matchesKittySequence(data, CODEPOINTS.space, modifier) || + matchesModifyOtherKeys(data, CODEPOINTS.space, modifier) + ); + + case "tab": + if (modifier === MODIFIERS.shift) { + return ( + data === "\x1b[Z" || + matchesKittySequence(data, CODEPOINTS.tab, MODIFIERS.shift) || + matchesModifyOtherKeys(data, CODEPOINTS.tab, MODIFIERS.shift) + ); + } + if (modifier === 0) { + return data === "\t" || matchesKittySequence(data, CODEPOINTS.tab, 0); + } + return ( + matchesKittySequence(data, CODEPOINTS.tab, modifier) || + matchesModifyOtherKeys(data, CODEPOINTS.tab, modifier) + ); + + case "enter": + case "return": + if (modifier === MODIFIERS.shift) { + // CSI u sequences (standard Kitty protocol) + if ( + matchesKittySequence(data, CODEPOINTS.enter, MODIFIERS.shift) || + matchesKittySequence(data, CODEPOINTS.kpEnter, MODIFIERS.shift) + ) { + return true; + } + // xterm modifyOtherKeys format (fallback when Kitty protocol not enabled) + if (matchesModifyOtherKeys(data, CODEPOINTS.enter, MODIFIERS.shift)) { + return true; + } + // When Kitty protocol is active, legacy sequences are custom terminal mappings + // \x1b\r = Kitty's "map shift+enter send_text all \e\r" + // \n = Ghostty's "keybind = shift+enter=text:\n" + if (_kittyProtocolActive) { + return data === "\x1b\r" || data === "\n"; + } + return false; + } + if (modifier === MODIFIERS.alt) { + // CSI u sequences (standard Kitty protocol) + if ( + matchesKittySequence(data, CODEPOINTS.enter, MODIFIERS.alt) || + matchesKittySequence(data, CODEPOINTS.kpEnter, MODIFIERS.alt) + ) { + return true; + } + // xterm modifyOtherKeys format (fallback when Kitty protocol not enabled) + if (matchesModifyOtherKeys(data, CODEPOINTS.enter, MODIFIERS.alt)) { + return true; + } + // \x1b\r is alt+enter only in legacy mode (no Kitty protocol) + // When Kitty protocol is active, alt+enter comes as CSI u sequence + if (!_kittyProtocolActive) { + return data === "\x1b\r"; + } + return false; + } + if (modifier === 0) { + return ( + data === "\r" || + (!_kittyProtocolActive && data === "\n") || + data === "\x1bOM" || // SS3 M (numpad enter in some terminals) + matchesKittySequence(data, CODEPOINTS.enter, 0) || + matchesKittySequence(data, CODEPOINTS.kpEnter, 0) + ); + } + return ( + matchesKittySequence(data, CODEPOINTS.enter, modifier) || + matchesKittySequence(data, CODEPOINTS.kpEnter, modifier) || + matchesModifyOtherKeys(data, CODEPOINTS.enter, modifier) + ); + + case "backspace": + if (modifier === MODIFIERS.alt) { + if (data === "\x1b\x7f" || data === "\x1b\b") { + return true; + } + return ( + matchesKittySequence(data, CODEPOINTS.backspace, MODIFIERS.alt) || + matchesModifyOtherKeys(data, CODEPOINTS.backspace, MODIFIERS.alt) + ); + } + if (modifier === MODIFIERS.ctrl) { + // Legacy raw 0x08 is ambiguous: it can be Ctrl+Backspace on Windows + // Terminal or plain Backspace on other terminals, while also + // overlapping with Ctrl+H. + if (matchesRawBackspace(data, MODIFIERS.ctrl)) return true; + return ( + matchesKittySequence(data, CODEPOINTS.backspace, MODIFIERS.ctrl) || + matchesModifyOtherKeys(data, CODEPOINTS.backspace, MODIFIERS.ctrl) + ); + } + if (modifier === 0) { + return ( + matchesRawBackspace(data, 0) || + matchesKittySequence(data, CODEPOINTS.backspace, 0) || + matchesModifyOtherKeys(data, CODEPOINTS.backspace, 0) + ); + } + return ( + matchesKittySequence(data, CODEPOINTS.backspace, modifier) || + matchesModifyOtherKeys(data, CODEPOINTS.backspace, modifier) + ); + + case "insert": + if (modifier === 0) { + return ( + matchesLegacySequence(data, LEGACY_KEY_SEQUENCES.insert) || + matchesKittySequence(data, FUNCTIONAL_CODEPOINTS.insert, 0) + ); + } + if (matchesLegacyModifierSequence(data, "insert", modifier)) { + return true; + } + return matchesKittySequence(data, FUNCTIONAL_CODEPOINTS.insert, modifier); + + case "delete": + if (modifier === 0) { + return ( + matchesLegacySequence(data, LEGACY_KEY_SEQUENCES.delete) || + matchesKittySequence(data, FUNCTIONAL_CODEPOINTS.delete, 0) + ); + } + if (matchesLegacyModifierSequence(data, "delete", modifier)) { + return true; + } + return matchesKittySequence(data, FUNCTIONAL_CODEPOINTS.delete, modifier); + + case "clear": + if (modifier === 0) { + return matchesLegacySequence(data, LEGACY_KEY_SEQUENCES.clear); + } + return matchesLegacyModifierSequence(data, "clear", modifier); + + case "home": + if (modifier === 0) { + return ( + matchesLegacySequence(data, LEGACY_KEY_SEQUENCES.home) || + matchesKittySequence(data, FUNCTIONAL_CODEPOINTS.home, 0) + ); + } + if (matchesLegacyModifierSequence(data, "home", modifier)) { + return true; + } + return matchesKittySequence(data, FUNCTIONAL_CODEPOINTS.home, modifier); + + case "end": + if (modifier === 0) { + return ( + matchesLegacySequence(data, LEGACY_KEY_SEQUENCES.end) || + matchesKittySequence(data, FUNCTIONAL_CODEPOINTS.end, 0) + ); + } + if (matchesLegacyModifierSequence(data, "end", modifier)) { + return true; + } + return matchesKittySequence(data, FUNCTIONAL_CODEPOINTS.end, modifier); + + case "pageup": + if (modifier === 0) { + return ( + matchesLegacySequence(data, LEGACY_KEY_SEQUENCES.pageUp) || + matchesKittySequence(data, FUNCTIONAL_CODEPOINTS.pageUp, 0) + ); + } + if (matchesLegacyModifierSequence(data, "pageUp", modifier)) { + return true; + } + return matchesKittySequence(data, FUNCTIONAL_CODEPOINTS.pageUp, modifier); + + case "pagedown": + if (modifier === 0) { + return ( + matchesLegacySequence(data, LEGACY_KEY_SEQUENCES.pageDown) || + matchesKittySequence(data, FUNCTIONAL_CODEPOINTS.pageDown, 0) + ); + } + if (matchesLegacyModifierSequence(data, "pageDown", modifier)) { + return true; + } + return matchesKittySequence(data, FUNCTIONAL_CODEPOINTS.pageDown, modifier); + + case "up": + if (modifier === MODIFIERS.alt) { + return data === "\x1bp" || matchesKittySequence(data, ARROW_CODEPOINTS.up, MODIFIERS.alt); + } + if (modifier === 0) { + return ( + matchesLegacySequence(data, LEGACY_KEY_SEQUENCES.up) || + matchesKittySequence(data, ARROW_CODEPOINTS.up, 0) + ); + } + if (matchesLegacyModifierSequence(data, "up", modifier)) { + return true; + } + return matchesKittySequence(data, ARROW_CODEPOINTS.up, modifier); + + case "down": + if (modifier === MODIFIERS.alt) { + return data === "\x1bn" || matchesKittySequence(data, ARROW_CODEPOINTS.down, MODIFIERS.alt); + } + if (modifier === 0) { + return ( + matchesLegacySequence(data, LEGACY_KEY_SEQUENCES.down) || + matchesKittySequence(data, ARROW_CODEPOINTS.down, 0) + ); + } + if (matchesLegacyModifierSequence(data, "down", modifier)) { + return true; + } + return matchesKittySequence(data, ARROW_CODEPOINTS.down, modifier); + + case "left": + if (modifier === MODIFIERS.alt) { + return ( + data === "\x1b[1;3D" || + (!_kittyProtocolActive && data === "\x1bB") || + data === "\x1bb" || + matchesKittySequence(data, ARROW_CODEPOINTS.left, MODIFIERS.alt) + ); + } + if (modifier === MODIFIERS.ctrl) { + return ( + data === "\x1b[1;5D" || + matchesLegacyModifierSequence(data, "left", MODIFIERS.ctrl) || + matchesKittySequence(data, ARROW_CODEPOINTS.left, MODIFIERS.ctrl) + ); + } + if (modifier === 0) { + return ( + matchesLegacySequence(data, LEGACY_KEY_SEQUENCES.left) || + matchesKittySequence(data, ARROW_CODEPOINTS.left, 0) + ); + } + if (matchesLegacyModifierSequence(data, "left", modifier)) { + return true; + } + return matchesKittySequence(data, ARROW_CODEPOINTS.left, modifier); + + case "right": + if (modifier === MODIFIERS.alt) { + return ( + data === "\x1b[1;3C" || + (!_kittyProtocolActive && data === "\x1bF") || + data === "\x1bf" || + matchesKittySequence(data, ARROW_CODEPOINTS.right, MODIFIERS.alt) + ); + } + if (modifier === MODIFIERS.ctrl) { + return ( + data === "\x1b[1;5C" || + matchesLegacyModifierSequence(data, "right", MODIFIERS.ctrl) || + matchesKittySequence(data, ARROW_CODEPOINTS.right, MODIFIERS.ctrl) + ); + } + if (modifier === 0) { + return ( + matchesLegacySequence(data, LEGACY_KEY_SEQUENCES.right) || + matchesKittySequence(data, ARROW_CODEPOINTS.right, 0) + ); + } + if (matchesLegacyModifierSequence(data, "right", modifier)) { + return true; + } + return matchesKittySequence(data, ARROW_CODEPOINTS.right, modifier); + + case "f1": + case "f2": + case "f3": + case "f4": + case "f5": + case "f6": + case "f7": + case "f8": + case "f9": + case "f10": + case "f11": + case "f12": { + if (modifier !== 0) { + return false; + } + const functionKey = key as keyof typeof LEGACY_KEY_SEQUENCES; + return matchesLegacySequence(data, LEGACY_KEY_SEQUENCES[functionKey]); + } + } + + // Handle single letter/digit keys and symbols + if (key.length === 1 && ((key >= "a" && key <= "z") || isDigitKey(key) || SYMBOL_KEYS.has(key))) { + const codepoint = key.charCodeAt(0); + const rawCtrl = rawCtrlChar(key); + const isLetter = key >= "a" && key <= "z"; + const isDigit = isDigitKey(key); + + if (modifier === MODIFIERS.ctrl + MODIFIERS.alt && !_kittyProtocolActive && rawCtrl) { + // Legacy: ctrl+alt+key is ESC followed by the control character. + // If that legacy form does not match, continue so CSI-u and + // modifyOtherKeys sequences from tmux can still be recognized. + if (data === `\x1b${rawCtrl}`) return true; + } + + if (modifier === MODIFIERS.alt && !_kittyProtocolActive && (isLetter || isDigit || SYMBOL_KEYS.has(key))) { + // Legacy: alt+printable key is ESC followed by the key + if (data === `\x1b${key}`) return true; + } + + if (modifier === MODIFIERS.ctrl) { + // Legacy: ctrl+key sends the control character + if (rawCtrl && data === rawCtrl) return true; + return ( + matchesKittySequence(data, codepoint, MODIFIERS.ctrl) || + matchesPrintableModifyOtherKeys(data, codepoint, MODIFIERS.ctrl) + ); + } + + if (modifier === MODIFIERS.shift + MODIFIERS.ctrl) { + return ( + matchesKittySequence(data, codepoint, MODIFIERS.shift + MODIFIERS.ctrl) || + matchesPrintableModifyOtherKeys(data, codepoint, MODIFIERS.shift + MODIFIERS.ctrl) + ); + } + + if (modifier === MODIFIERS.shift) { + // Legacy: shift+letter produces uppercase + if (isLetter && data === key.toUpperCase()) return true; + return ( + matchesKittySequence(data, codepoint, MODIFIERS.shift) || + matchesPrintableModifyOtherKeys(data, codepoint, MODIFIERS.shift) + ); + } + + if (modifier !== 0) { + return ( + matchesKittySequence(data, codepoint, modifier) || + matchesPrintableModifyOtherKeys(data, codepoint, modifier) + ); + } + + // Check both raw char and Kitty sequence (needed for release events) + return data === key || matchesKittySequence(data, codepoint, 0); + } + + return false; +} + +/** + * Parse input data and return the key identifier if recognized. + * + * @param data - Raw input data from terminal + * @returns Key identifier string (e.g., "ctrl+c") or undefined + */ +function formatParsedKey(codepoint: number, modifier: number, baseLayoutKey?: number): string | undefined { + const normalizedCodepoint = normalizeKittyFunctionalCodepoint(codepoint); + const identityCodepoint = normalizeShiftedLetterIdentityCodepoint(normalizedCodepoint, modifier); + + // Use base layout key only when codepoint is not a recognized Latin + // letter (a-z), digit (0-9), or symbol (/, -, [, ;, etc.). For those, + // the codepoint is authoritative regardless of physical key position. + // This prevents remapped layouts (Dvorak, Colemak, xremap, etc.) from + // reporting the wrong key name based on the QWERTY physical position. + const isLatinLetter = identityCodepoint >= 97 && identityCodepoint <= 122; // a-z + const isDigit = identityCodepoint >= 48 && identityCodepoint <= 57; // 0-9 + const isKnownSymbol = SYMBOL_KEYS.has(String.fromCharCode(identityCodepoint)); + const effectiveCodepoint = + isLatinLetter || isDigit || isKnownSymbol ? identityCodepoint : (baseLayoutKey ?? identityCodepoint); + + let keyName: string | undefined; + if (effectiveCodepoint === CODEPOINTS.escape) keyName = "escape"; + else if (effectiveCodepoint === CODEPOINTS.tab) keyName = "tab"; + else if (effectiveCodepoint === CODEPOINTS.enter || effectiveCodepoint === CODEPOINTS.kpEnter) keyName = "enter"; + else if (effectiveCodepoint === CODEPOINTS.space) keyName = "space"; + else if (effectiveCodepoint === CODEPOINTS.backspace) keyName = "backspace"; + else if (effectiveCodepoint === FUNCTIONAL_CODEPOINTS.delete) keyName = "delete"; + else if (effectiveCodepoint === FUNCTIONAL_CODEPOINTS.insert) keyName = "insert"; + else if (effectiveCodepoint === FUNCTIONAL_CODEPOINTS.home) keyName = "home"; + else if (effectiveCodepoint === FUNCTIONAL_CODEPOINTS.end) keyName = "end"; + else if (effectiveCodepoint === FUNCTIONAL_CODEPOINTS.pageUp) keyName = "pageUp"; + else if (effectiveCodepoint === FUNCTIONAL_CODEPOINTS.pageDown) keyName = "pageDown"; + else if (effectiveCodepoint === ARROW_CODEPOINTS.up) keyName = "up"; + else if (effectiveCodepoint === ARROW_CODEPOINTS.down) keyName = "down"; + else if (effectiveCodepoint === ARROW_CODEPOINTS.left) keyName = "left"; + else if (effectiveCodepoint === ARROW_CODEPOINTS.right) keyName = "right"; + else if (effectiveCodepoint >= 48 && effectiveCodepoint <= 57) keyName = String.fromCharCode(effectiveCodepoint); + else if (effectiveCodepoint >= 97 && effectiveCodepoint <= 122) keyName = String.fromCharCode(effectiveCodepoint); + else if (SYMBOL_KEYS.has(String.fromCharCode(effectiveCodepoint))) keyName = String.fromCharCode(effectiveCodepoint); + + if (!keyName) return undefined; + return formatKeyNameWithModifiers(keyName, modifier); +} + +export function parseKey(data: string): string | undefined { + const kitty = parseKittySequence(data); + if (kitty) { + return formatParsedKey(kitty.codepoint, kitty.modifier, kitty.baseLayoutKey); + } + + const modifyOtherKeys = parseModifyOtherKeysSequence(data); + if (modifyOtherKeys) { + return formatParsedKey(modifyOtherKeys.codepoint, modifyOtherKeys.modifier); + } + + // Mode-aware legacy sequences + // When Kitty protocol is active, ambiguous sequences are interpreted as custom terminal mappings: + // - \x1b\r = shift+enter (Kitty mapping), not alt+enter + // - \n = shift+enter (Ghostty mapping) + if (_kittyProtocolActive) { + if (data === "\x1b\r" || data === "\n") return "shift+enter"; + } + + const legacySequenceKeyId = LEGACY_SEQUENCE_KEY_IDS[data]; + if (legacySequenceKeyId) return legacySequenceKeyId; + + // Legacy sequences (used when Kitty protocol is not active, or for unambiguous sequences) + if (data === "\x1b") return "escape"; + if (data === "\x1c") return "ctrl+\\"; + if (data === "\x1d") return "ctrl+]"; + if (data === "\x1f") return "ctrl+-"; + if (data === "\x1b\x1b") return "ctrl+alt+["; + if (data === "\x1b\x1c") return "ctrl+alt+\\"; + if (data === "\x1b\x1d") return "ctrl+alt+]"; + if (data === "\x1b\x1f") return "ctrl+alt+-"; + if (data === "\t") return "tab"; + if (data === "\r" || (!_kittyProtocolActive && data === "\n") || data === "\x1bOM") return "enter"; + if (data === "\x00") return "ctrl+space"; + if (data === " ") return "space"; + if (data === "\x7f") return "backspace"; + if (data === "\x08") return isWindowsTerminalSession() ? "ctrl+backspace" : "backspace"; + if (data === "\x1b[Z") return "shift+tab"; + if (!_kittyProtocolActive && data === "\x1b\r") return "alt+enter"; + if (!_kittyProtocolActive && data === "\x1b ") return "alt+space"; + if (data === "\x1b\x7f" || data === "\x1b\b") return "alt+backspace"; + if (!_kittyProtocolActive && data === "\x1bB") return "alt+left"; + if (!_kittyProtocolActive && data === "\x1bF") return "alt+right"; + if (!_kittyProtocolActive && data.length === 2 && data[0] === "\x1b") { + const code = data.charCodeAt(1); + if (code >= 1 && code <= 26) { + return `ctrl+alt+${String.fromCharCode(code + 96)}`; + } + // Legacy alt+letter/digit/symbol (ESC followed by the key) + const key = String.fromCharCode(code); + if ((code >= 97 && code <= 122) || (code >= 48 && code <= 57) || SYMBOL_KEYS.has(key)) { + return `alt+${key}`; + } + } + if (data === "\x1b[A") return "up"; + if (data === "\x1b[B") return "down"; + if (data === "\x1b[C") return "right"; + if (data === "\x1b[D") return "left"; + if (data === "\x1b[H" || data === "\x1bOH") return "home"; + if (data === "\x1b[F" || data === "\x1bOF") return "end"; + if (data === "\x1b[3~") return "delete"; + if (data === "\x1b[5~") return "pageUp"; + if (data === "\x1b[6~") return "pageDown"; + + // Raw Ctrl+letter + if (data.length === 1) { + const code = data.charCodeAt(0); + if (code >= 1 && code <= 26) { + return `ctrl+${String.fromCharCode(code + 96)}`; + } + if (code >= 32 && code <= 126) { + return data; + } + } + + return undefined; +} + +// ============================================================================= +// Kitty CSI-u Printable Decoding +// ============================================================================= + +const KITTY_CSI_U_REGEX = /^\x1b\[(\d+)(?::(\d*))?(?::(\d+))?(?:;(\d+))?(?::(\d+))?u$/; +const KITTY_PRINTABLE_ALLOWED_MODIFIERS = MODIFIERS.shift | LOCK_MASK; + +/** + * Decode a Kitty CSI-u sequence into a printable character, if applicable. + * + * When Kitty keyboard protocol flag 1 (disambiguate) is active, terminals send + * CSI-u sequences for all keys, including plain printable characters. This + * function extracts the printable character from such sequences. + * + * Only accepts plain or Shift-modified keys. Rejects Ctrl, Alt, and unsupported + * modifier combinations (those are handled by keybinding matching instead). + * Prefers the shifted keycode when Shift is held and a shifted key is reported. + * + * @param data - Raw input data from terminal + * @returns The printable character, or undefined if not a printable CSI-u sequence + */ +export function decodeKittyPrintable(data: string): string | undefined { + const match = data.match(KITTY_CSI_U_REGEX); + if (!match) return undefined; + + // CSI-u groups: [:[:]];[:]u + const codepoint = Number.parseInt(match[1] ?? "", 10); + if (!Number.isFinite(codepoint)) return undefined; + + const shiftedKey = match[2] && match[2].length > 0 ? Number.parseInt(match[2], 10) : undefined; + const modValue = match[4] ? Number.parseInt(match[4], 10) : 1; + // Modifiers are 1-indexed in CSI-u; normalize to our bitmask. + const modifier = Number.isFinite(modValue) ? modValue - 1 : 0; + + // Only accept printable CSI-u input for plain or Shift-modified text keys. + // Reject unsupported modifier bits (e.g. Super/Meta) to avoid inserting + // characters from modifier-only terminal events. + if ((modifier & ~KITTY_PRINTABLE_ALLOWED_MODIFIERS) !== 0) return undefined; + if (modifier & (MODIFIERS.alt | MODIFIERS.ctrl)) return undefined; + + // Prefer the shifted keycode when Shift is held. + let effectiveCodepoint = codepoint; + if (modifier & MODIFIERS.shift && typeof shiftedKey === "number") { + effectiveCodepoint = shiftedKey; + } + effectiveCodepoint = normalizeKittyFunctionalCodepoint(effectiveCodepoint); + // Drop control characters or invalid codepoints. + if (!Number.isFinite(effectiveCodepoint) || effectiveCodepoint < 32) return undefined; + + try { + return String.fromCodePoint(effectiveCodepoint); + } catch { + return undefined; + } +} + +function decodeModifyOtherKeysPrintable(data: string): string | undefined { + const parsed = parseModifyOtherKeysSequence(data); + if (!parsed) return undefined; + const modifier = parsed.modifier & ~LOCK_MASK; + if ((modifier & ~MODIFIERS.shift) !== 0) return undefined; + if (!Number.isFinite(parsed.codepoint) || parsed.codepoint < 32) return undefined; + + try { + return String.fromCodePoint(parsed.codepoint); + } catch { + return undefined; + } +} + +export function decodePrintableKey(data: string): string | undefined { + return decodeKittyPrintable(data) ?? decodeModifyOtherKeysPrintable(data); +} diff --git a/packages/tui/src/kill-ring.ts b/packages/tui/src/kill-ring.ts new file mode 100644 index 0000000000000000000000000000000000000000..2292f91aa36baa1bf762df2890bdbed8ca17eb19 --- /dev/null +++ b/packages/tui/src/kill-ring.ts @@ -0,0 +1,46 @@ +/** + * Ring buffer for Emacs-style kill/yank operations. + * + * Tracks killed (deleted) text entries. Consecutive kills can accumulate + * into a single entry. Supports yank (paste most recent) and yank-pop + * (cycle through older entries). + */ +export class KillRing { + private ring: string[] = []; + + /** + * Add text to the kill ring. + * + * @param text - The killed text to add + * @param opts - Push options + * @param opts.prepend - If accumulating, prepend (backward deletion) or append (forward deletion) + * @param opts.accumulate - Merge with the most recent entry instead of creating a new one + */ + push(text: string, opts: { prepend: boolean; accumulate?: boolean }): void { + if (!text) return; + + if (opts.accumulate && this.ring.length > 0) { + const last = this.ring.pop()!; + this.ring.push(opts.prepend ? text + last : last + text); + } else { + this.ring.push(text); + } + } + + /** Get most recent entry without modifying the ring. */ + peek(): string | undefined { + return this.ring.length > 0 ? this.ring[this.ring.length - 1] : undefined; + } + + /** Move last entry to front (for yank-pop cycling). */ + rotate(): void { + if (this.ring.length > 1) { + const last = this.ring.pop()!; + this.ring.unshift(last); + } + } + + get length(): number { + return this.ring.length; + } +} diff --git a/packages/tui/src/latex.ts b/packages/tui/src/latex.ts new file mode 100644 index 0000000000000000000000000000000000000000..b9c0f9e8cf23ee6430399cd8a1334a0f4674522b --- /dev/null +++ b/packages/tui/src/latex.ts @@ -0,0 +1,1394 @@ +import { visibleWidth } from "./utils.ts"; + +const SYMBOLS: Readonly> = { + alpha: "α", + beta: "β", + gamma: "γ", + delta: "δ", + epsilon: "ϵ", + varepsilon: "ε", + zeta: "ζ", + eta: "η", + theta: "θ", + vartheta: "ϑ", + iota: "ι", + kappa: "κ", + varkappa: "ϰ", + lambda: "λ", + mu: "μ", + nu: "ν", + xi: "ξ", + pi: "π", + varpi: "ϖ", + rho: "ρ", + varrho: "ϱ", + sigma: "σ", + varsigma: "ς", + tau: "τ", + upsilon: "υ", + phi: "ϕ", + varphi: "φ", + chi: "χ", + psi: "ψ", + omega: "ω", + Gamma: "Γ", + Delta: "Δ", + Theta: "Θ", + Lambda: "Λ", + Xi: "Ξ", + Pi: "Π", + Sigma: "Σ", + Upsilon: "Υ", + Phi: "Φ", + Psi: "Ψ", + Omega: "Ω", + pm: "±", + mp: "∓", + times: "×", + div: "÷", + cdot: "·", + ast: "∗", + star: "⋆", + circ: "∘", + bullet: "•", + oplus: "⊕", + ominus: "⊖", + otimes: "⊗", + oslash: "⊘", + odot: "⊙", + bigcirc: "○", + dagger: "†", + ddagger: "‡", + amalg: "⨿", + uplus: "⊎", + sqcap: "⊓", + sqcup: "⊔", + bowtie: "⋈", + Join: "⋈", + ltimes: "⋉", + rtimes: "⋊", + leftouterjoin: "⟕", + rightouterjoin: "⟖", + fullouterjoin: "⟗", + triangleleft: "◁", + triangleright: "▷", + wr: "≀", + cap: "∩", + cup: "∪", + bigcap: "⋂", + bigcup: "⋃", + bigwedge: "⋀", + bigvee: "⋁", + bigsqcup: "⨆", + biguplus: "⨄", + bigoplus: "⨁", + bigotimes: "⨂", + bigodot: "⨀", + setminus: "∖", + in: "∈", + notin: "∉", + ni: "∋", + subset: "⊂", + supset: "⊃", + subseteq: "⊆", + supseteq: "⊇", + sqsubset: "⊏", + sqsupset: "⊐", + sqsubseteq: "⊑", + sqsupseteq: "⊒", + prec: "≺", + preceq: "≼", + succ: "≻", + succeq: "≽", + ll: "≪", + gg: "≫", + le: "≤", + leq: "≤", + leqslant: "≤", + ge: "≥", + geq: "≥", + geqslant: "≥", + ne: "≠", + neq: "≠", + equiv: "≡", + approx: "≈", + sim: "∼", + simeq: "≃", + cong: "≅", + asymp: "≍", + doteq: "≐", + propto: "∝", + parallel: "∥", + perp: "⊥", + mid: "∣", + vdash: "⊢", + dashv: "⊣", + models: "⊨", + Vdash: "⊩", + Vvdash: "⊪", + nvdash: "⊬", + nvDash: "⊭", + forall: "∀", + exists: "∃", + nexists: "∄", + neg: "¬", + land: "∧", + wedge: "∧", + lor: "∨", + vee: "∨", + to: "→", + rightarrow: "→", + longrightarrow: "→", + leftarrow: "←", + longleftarrow: "←", + gets: "←", + leftrightarrow: "↔", + longleftrightarrow: "↔", + hookleftarrow: "↩", + hookrightarrow: "↪", + twoheadleftarrow: "↞", + twoheadrightarrow: "↠", + leftharpoonup: "↼", + leftharpoondown: "↽", + rightharpoonup: "⇀", + rightharpoondown: "⇁", + rightleftharpoons: "⇌", + leftrightharpoons: "⇋", + nearrow: "↗", + searrow: "↘", + swarrow: "↙", + nwarrow: "↖", + rightsquigarrow: "⇝", + leadsto: "⇝", + Rightarrow: "⇒", + Longrightarrow: "⇒", + Leftarrow: "⇐", + Longleftarrow: "⇐", + Leftrightarrow: "⇔", + Longleftrightarrow: "⇔", + implies: "⇒", + iff: "⇔", + mapsto: "↦", + longmapsto: "↦", + uparrow: "↑", + downarrow: "↓", + partial: "∂", + nabla: "∇", + int: "∫", + iint: "∬", + iiint: "∭", + oint: "∮", + sum: "∑", + prod: "∏", + coprod: "∐", + infty: "∞", + emptyset: "∅", + varnothing: "∅", + angle: "∠", + therefore: "∴", + because: "∵", + aleph: "ℵ", + beth: "ℶ", + gimel: "ℷ", + daleth: "ℸ", + top: "⊤", + bot: "⊥", + triangle: "△", + square: "□", + lozenge: "◊", + checkmark: "✓", + complement: "∁", + wp: "℘", + prime: "′", + ldots: "…", + dots: "…", + cdots: "⋯", + vdots: "⋮", + ddots: "⋱", + ell: "ℓ", + hbar: "ℏ", + Im: "ℑ", + Re: "ℜ", + langle: "⟨", + rangle: "⟩", + vert: "|", + lvert: "|", + rvert: "|", + Vert: "‖", + lVert: "‖", + rVert: "‖", + lbrace: "{", + rbrace: "}", + backslash: "\\", + lfloor: "⌊", + rfloor: "⌋", + lceil: "⌈", + rceil: "⌉", + colon: ":", +}; + +const NAMED_OPERATORS = new Set([ + "arccos", + "arcsin", + "arctan", + "arg", + "cos", + "cosh", + "cot", + "coth", + "csc", + "deg", + "det", + "dim", + "exp", + "gcd", + "hom", + "inf", + "ker", + "lg", + "lim", + "liminf", + "limsup", + "ln", + "log", + "max", + "min", + "Pr", + "sec", + "sin", + "sinh", + "sup", + "tan", + "tanh", +]); + +const LIMIT_OPERATORS = new Set([ + "argmax", + "argmin", + "inf", + "injlim", + "lim", + "liminf", + "limsup", + "max", + "min", + "projlim", + "sup", +]); + +const DISPLAY_LIMIT_SYMBOLS = new Set([ + "bigcap", + "bigcup", + "bigodot", + "bigoplus", + "bigotimes", + "bigsqcup", + "biguplus", + "bigvee", + "bigwedge", + "coprod", + "int", + "iint", + "iiint", + "oint", + "prod", + "sum", +]); + +const RELATION_COMMANDS = new Set([ + "Leftarrow", + "Leftrightarrow", + "Longleftarrow", + "Longleftrightarrow", + "Longrightarrow", + "Rightarrow", + "Join", + "Vdash", + "Vvdash", + "approx", + "asymp", + "bowtie", + "cong", + "dashv", + "fullouterjoin", + "doteq", + "downarrow", + "equiv", + "ge", + "geq", + "geqslant", + "gets", + "gg", + "hookleftarrow", + "hookrightarrow", + "iff", + "implies", + "in", + "leadsto", + "le", + "leftarrow", + "leftharpoondown", + "leftharpoonup", + "leftrightarrow", + "leftrightharpoons", + "leftouterjoin", + "leq", + "leqslant", + "ll", + "longleftarrow", + "longleftrightarrow", + "longmapsto", + "longrightarrow", + "ltimes", + "mapsto", + "mid", + "models", + "ne", + "nearrow", + "neq", + "ni", + "notin", + "nvdash", + "nvDash", + "nwarrow", + "parallel", + "perp", + "prec", + "preceq", + "propto", + "rightharpoondown", + "rightharpoonup", + "rightleftharpoons", + "rightouterjoin", + "rightarrow", + "rightsquigarrow", + "rtimes", + "searrow", + "sim", + "simeq", + "sqsubset", + "sqsubseteq", + "sqsupset", + "sqsupseteq", + "subset", + "subseteq", + "succ", + "succeq", + "supset", + "supseteq", + "swarrow", + "to", + "triangleleft", + "triangleright", + "twoheadleftarrow", + "twoheadrightarrow", + "uparrow", + "vdash", +]); + +const NEGATED_SYMBOLS: Readonly> = { + "<": "≮", + ">": "≯", + "=": "≠", + "∈": "∉", + "∋": "∌", + "∣": "∤", + "∥": "∦", + "∼": "≁", + "≃": "≄", + "≅": "≇", + "≈": "≉", + "≡": "≢", + "≤": "≰", + "≥": "≱", + "≺": "⊀", + "≻": "⊁", + "⊂": "⊄", + "⊃": "⊅", + "⊆": "⊈", + "⊇": "⊉", + "⊢": "⊬", + "⊨": "⊭", + "↔": "↮", + "←": "↚", + "→": "↛", + "⇒": "⇏", + "⇐": "⇍", + "⇔": "⇎", + "≼": "⋠", + "≽": "⋡", +}; + +const BLACKBOARD: Readonly> = { + C: "ℂ", + H: "ℍ", + N: "ℕ", + P: "ℙ", + Q: "ℚ", + R: "ℝ", + Z: "ℤ", +}; + +const SUPERSCRIPTS: Readonly> = { + "0": "⁰", + "1": "¹", + "2": "²", + "3": "³", + "4": "⁴", + "5": "⁵", + "6": "⁶", + "7": "⁷", + "8": "⁸", + "9": "⁹", + "+": "⁺", + "-": "⁻", + "=": "⁼", + "(": "⁽", + ")": "⁾", + a: "ᵃ", + b: "ᵇ", + c: "ᶜ", + d: "ᵈ", + e: "ᵉ", + f: "ᶠ", + g: "ᵍ", + h: "ʰ", + i: "ⁱ", + j: "ʲ", + k: "ᵏ", + l: "ˡ", + m: "ᵐ", + n: "ⁿ", + o: "ᵒ", + p: "ᵖ", + r: "ʳ", + s: "ˢ", + t: "ᵗ", + u: "ᵘ", + v: "ᵛ", + w: "ʷ", + x: "ˣ", + y: "ʸ", + z: "ᶻ", +}; + +const SUBSCRIPTS: Readonly> = { + "0": "₀", + "1": "₁", + "2": "₂", + "3": "₃", + "4": "₄", + "5": "₅", + "6": "₆", + "7": "₇", + "8": "₈", + "9": "₉", + "+": "₊", + "-": "₋", + "=": "₌", + "(": "₍", + ")": "₎", + a: "ₐ", + e: "ₑ", + h: "ₕ", + i: "ᵢ", + j: "ⱼ", + k: "ₖ", + l: "ₗ", + m: "ₘ", + n: "ₙ", + o: "ₒ", + p: "ₚ", + r: "ᵣ", + s: "ₛ", + t: "ₜ", + u: "ᵤ", + v: "ᵥ", + x: "ₓ", +}; + +const SPACING_COMMANDS = new Set([ + ",", + ":", + ";", + " ", + ">", + "enspace", + "enskip", + "medspace", + "quad", + "qquad", + "thickspace", + "thinspace", +]); +const NEGATIVE_SPACING_COMMANDS = new Set(["!", "negmedspace", "negthickspace", "negthinspace"]); +const NEGATIVE_SPACE = "\u0000"; +const IGNORED_COMMANDS = new Set([ + "displaystyle", + "limits", + "nolimits", + "scriptstyle", + "scriptscriptstyle", + "textstyle", +]); +const SIZE_COMMANDS = new Set([ + "big", + "Big", + "bigg", + "Bigg", + "bigl", + "Bigl", + "biggl", + "Biggl", + "bigr", + "Bigr", + "biggr", + "Biggr", +]); +const PLAIN_WRAPPERS = new Set([ + "emph", + "mathcal", + "mathbf", + "mathfrak", + "mathit", + "mathrm", + "mathnormal", + "mathscr", + "mathsf", + "mathtt", + "mathup", + "mbox", + "overbrace", + "pmb", + "smash", + "substack", + "text", + "textbf", + "textit", + "textmd", + "textnormal", + "textrm", + "textsc", + "textsf", + "textsl", + "texttt", + "textup", + "underbrace", + "bm", + "boldsymbol", +]); +const ACCENTS: Readonly> = { + acute: "\u0301", + bar: "\u0305", + breve: "\u0306", + check: "\u030c", + ddot: "\u0308", + dot: "\u0307", + grave: "\u0300", + hat: "\u0302", + mathring: "\u030a", + overleftarrow: "\u20d6", + overleftrightarrow: "\u20e1", + overline: "\u0305", + overrightarrow: "\u20d7", + tilde: "\u0303", + underline: "\u0332", + vec: "\u20d7", + widehat: "\u0302", + widetilde: "\u0303", +}; + +function replaceCharacters(value: string, replacements: Readonly>): string | undefined { + let result = ""; + for (const character of value) { + const replacement = replacements[character]; + if (replacement === undefined) { + return undefined; + } + result += replacement; + } + return result; +} + +function formatScript(value: string, kind: "sub" | "sup"): string { + value = value.trim(); + const replacements = kind === "sub" ? SUBSCRIPTS : SUPERSCRIPTS; + const unicode = replaceCharacters(value.replace(/\s*([=+-])\s*/g, "$1"), replacements); + if (unicode !== undefined) { + return unicode; + } + + const prefix = kind === "sub" ? "_" : "^"; + if (Array.from(value).length === 1 || (kind === "sub" && /^[A-Za-z]+$/.test(value))) { + return `${prefix}${value}`; + } + return `${prefix}(${value})`; +} + +function formatFraction(numerator: string, denominator: string): string { + numerator = numerator.trim(); + denominator = denominator.trim(); + const simpleNumerator = /^[\p{L}\p{N}.]+$/u.test(numerator); + const simpleDenominator = /^[\p{N}.]+$/u.test(denominator) || Array.from(denominator).length === 1; + return `${simpleNumerator ? numerator : `(${numerator})`}/${simpleDenominator ? denominator : `(${denominator})`}`; +} + +function formatRoot(value: string, symbol = "√"): string { + value = value.trim(); + return /^[\p{L}\p{N}.]+$/u.test(value) ? `${symbol}${value}` : `${symbol}(${value})`; +} + +const NAMED_OPERATOR_START = "\u{f0004}"; +const NAMED_OPERATOR_END = "\u{f0005}"; +const NAMED_OPERATOR_LEFT_SPACING_PATTERN = /(?<=[\p{L}\p{N})\]}\u{f0001}])\u{f0004}/gu; +const NAMED_OPERATOR_RIGHT_SPACING_PATTERN = /\u{f0005}(?=[\p{L}\p{N}√\u{f0000}])/gu; + +function normalizeOutput(value: string): string { + return value + .replace(NAMED_OPERATOR_LEFT_SPACING_PATTERN, " ") + .replaceAll(NAMED_OPERATOR_START, "") + .replace(NAMED_OPERATOR_RIGHT_SPACING_PATTERN, " ") + .replaceAll(NAMED_OPERATOR_END, "") + .split("\n") + .map((line) => line.replace(/[ \t]+/g, " ").trim()) + .filter((line, index, lines) => line.length > 0 || (index > 0 && index < lines.length - 1)) + .join("\n") + .trim(); +} + +interface FractionNode { + type: "fraction"; + numerator: string; + denominator: string; +} + +interface OperatorNode { + type: "operator"; + operator: string; + lower?: string; + upper?: string; +} + +interface MatrixNode { + type: "matrix"; + lines: string[]; + baseline: number; +} + +type LayoutNode = FractionNode | OperatorNode | MatrixNode; + +interface Layout { + lines: string[]; + width: number; + baseline: number; +} + +const LAYOUT_MARKER_START = "\u{f0000}"; +const LAYOUT_MARKER_END = "\u{f0001}"; +const LAYOUT_MARKER_PATTERN = /\u{f0000}(\d+)\u{f0001}/gu; +const TRAILING_LAYOUT_MARKER_PATTERN = /\u{f0000}(\d+)\u{f0001}$/u; +const PROTECTED_SPACE = "\u{f0002}"; + +function padLayoutLine(line: string, width: number, centered = false): string { + const padding = Math.max(0, width - visibleWidth(line)); + const left = centered ? Math.floor(padding / 2) : 0; + return `${" ".repeat(left)}${line}${" ".repeat(padding - left)}`; +} + +function joinLayouts(layouts: readonly Layout[]): Layout { + if (layouts.length === 0) { + return { lines: [""], width: 0, baseline: 0 }; + } + const baseline = Math.max(...layouts.map((layout) => layout.baseline)); + const below = Math.max(...layouts.map((layout) => layout.lines.length - layout.baseline - 1)); + const lines: string[] = []; + for (let row = 0; row <= baseline + below; row++) { + let line = ""; + for (const layout of layouts) { + const sourceRow = row - baseline + layout.baseline; + line += + sourceRow >= 0 && sourceRow < layout.lines.length + ? padLayoutLine(layout.lines[sourceRow] ?? "", layout.width) + : " ".repeat(layout.width); + } + lines.push(line.trimEnd()); + } + return { + lines, + width: layouts.reduce((width, layout) => width + layout.width, 0), + baseline, + }; +} + +function renderLayout(source: string, nodes: readonly LayoutNode[]): Layout { + const renderedLines: string[] = []; + let firstBaseline = 0; + for (const sourceLine of source.split("\n")) { + const layouts: Layout[] = []; + let position = 0; + let previousNode: LayoutNode | undefined; + for (const match of sourceLine.matchAll(LAYOUT_MARKER_PATTERN)) { + const index = match.index; + const node = nodes[Number(match[1])]; + if (!node) { + continue; + } + if (index > position) { + const sliced = sourceLine.slice(position, index); + const trimmed = (previousNode ? sliced.trimStart() : sliced).trimEnd(); + const preserveLeadingSpace = previousNode?.type === "matrix" && /^\s/.test(sliced); + const preserveTrailingSpace = node.type === "matrix" && /\s$/.test(sliced); + const text = trimmed + ? `${preserveLeadingSpace ? " " : ""}${trimmed}${preserveTrailingSpace ? " " : ""}` + : preserveLeadingSpace || preserveTrailingSpace + ? " " + : ""; + layouts.push({ lines: [text], width: visibleWidth(text), baseline: 0 }); + } + if (node.type === "fraction") { + const numerator = renderLayout(node.numerator, nodes); + const denominator = renderLayout(node.denominator, nodes); + const contentWidth = Math.max(numerator.width, denominator.width, 1); + const width = contentWidth + 2; + layouts.push({ + lines: [ + ...numerator.lines.map((line) => padLayoutLine(line, width, true)), + ` ${"─".repeat(contentWidth)} `, + ...denominator.lines.map((line) => padLayoutLine(line, width, true)), + ], + width, + baseline: numerator.lines.length, + }); + } else if (node.type === "operator") { + const contentWidth = Math.max( + visibleWidth(node.operator), + node.lower === undefined ? 0 : visibleWidth(node.lower), + node.upper === undefined ? 0 : visibleWidth(node.upper), + ); + const lines: string[] = []; + if (node.upper !== undefined) { + lines.push(`${padLayoutLine(node.upper, contentWidth, true)} `); + } + lines.push(`${padLayoutLine(node.operator, contentWidth, true)} `); + if (node.lower !== undefined) { + lines.push(`${padLayoutLine(node.lower, contentWidth, true)} `); + } + layouts.push({ + lines, + width: contentWidth + 1, + baseline: node.upper === undefined ? 0 : 1, + }); + } else { + const width = Math.max(0, ...node.lines.map((line) => visibleWidth(line))); + layouts.push({ + lines: node.lines.map((line) => padLayoutLine(line, width)), + width, + baseline: node.baseline, + }); + } + position = index + match[0].length; + previousNode = node; + } + if (position < sourceLine.length) { + const sliced = sourceLine.slice(position); + const trimmed = previousNode ? sliced.trimStart() : sliced; + const text = previousNode?.type === "matrix" && /^\s/.test(sliced) ? ` ${trimmed}` : trimmed; + layouts.push({ lines: [text], width: visibleWidth(text), baseline: 0 }); + } + const lineLayout = joinLayouts(layouts); + if (renderedLines.length === 0) { + firstBaseline = lineLayout.baseline; + } + renderedLines.push(...lineLayout.lines); + } + return { + lines: renderedLines, + width: Math.max(0, ...renderedLines.map((line) => visibleWidth(line))), + baseline: firstBaseline, + }; +} + +class LatexParser { + private readonly source: string; + private readonly layoutNodes: LayoutNode[]; + private readonly display: boolean; + private position = 0; + private supported = true; + private stackFractions = true; + + constructor(source: string, layoutNodes: LayoutNode[], display: boolean) { + this.source = source; + this.layoutNodes = layoutNodes; + this.display = display; + } + + render(): string | undefined { + const rendered = this.parseSequence(); + if (!this.supported || this.position !== this.source.length) { + return undefined; + } + return normalizeOutput(rendered); + } + + private parseSequence(endCharacter?: string): string { + let result = ""; + while (this.position < this.source.length) { + const character = this.source[this.position]; + if (endCharacter && character === endCharacter) { + this.position++; + return result; + } + + if (character === "}") { + this.supported = false; + return result; + } + + if (character === "{") { + this.position++; + result += this.parseSequence("}"); + continue; + } + + if (character === "\\") { + const command = this.parseCommand(); + if (command === NEGATIVE_SPACE) { + result = result.trimEnd(); + if (result.endsWith(NAMED_OPERATOR_END)) { + result = result.slice(0, -NAMED_OPERATOR_END.length); + } + } else { + result += command; + } + continue; + } + + if (character === "^" || character === "_") { + this.position++; + result = result.trimEnd(); + const script = formatScript(this.parseRequiredArgument(false), character === "_" ? "sub" : "sup"); + if (result.endsWith(NAMED_OPERATOR_END)) { + result = `${result.slice(0, -NAMED_OPERATOR_END.length)}${script}${NAMED_OPERATOR_END}`; + } else { + result += script; + } + continue; + } + + if (/\s/.test(character)) { + result += this.parseWhitespace(); + continue; + } + + if (character === "=" || character === "<" || character === ">") { + result = `${result.trimEnd()} ${character} `; + this.position++; + continue; + } + + if (character === "&") { + this.position++; + continue; + } + + if (character === "~") { + this.position++; + result += " "; + continue; + } + + if (character === ".") { + const marker = TRAILING_LAYOUT_MARKER_PATTERN.exec(result); + const node = marker ? this.layoutNodes[Number(marker[1])] : undefined; + if (node?.type === "matrix") { + const lastLine = node.lines.length - 1; + node.lines[lastLine] = `${node.lines[lastLine] ?? ""}${character}`; + this.position++; + continue; + } + } + + result += character; + this.position++; + } + + if (endCharacter) { + this.supported = false; + } + return result; + } + + private parseWhitespace(): string { + while (this.position < this.source.length && /\s/.test(this.source[this.position] ?? "")) { + this.position++; + } + return " "; + } + + private parseCommand(): string { + this.position++; + if (this.position >= this.source.length) { + this.supported = false; + return ""; + } + + let command = ""; + const first = this.source[this.position] ?? ""; + if (first === "\n" || first === "\r") { + this.position++; + if (first === "\r" && this.source[this.position] === "\n") { + this.position++; + } + return " "; + } + if (/[A-Za-z]/.test(first)) { + const start = this.position; + while (this.position < this.source.length && /[A-Za-z]/.test(this.source[this.position] ?? "")) { + this.position++; + } + command = this.source.slice(start, this.position); + } else { + command = first; + this.position++; + } + + if (command === "\\") { + return "\n"; + } + if (SPACING_COMMANDS.has(command)) { + return " "; + } + if (NEGATIVE_SPACING_COMMANDS.has(command)) { + return NEGATIVE_SPACE; + } + if (IGNORED_COMMANDS.has(command)) { + return ""; + } + if ( + command === "{" || + command === "}" || + command === "$" || + command === "%" || + command === "#" || + command === "_" || + command === "&" + ) { + return command; + } + if (command === "|") { + return "‖"; + } + if (command === "not") { + const value = this.parseRequiredArgument(false).trim(); + const negated = NEGATED_SYMBOLS[value]; + if (negated !== undefined) { + return ` ${negated} `; + } + const characters = Array.from(value); + if (characters.length === 0) { + this.supported = false; + return ""; + } + return ` ${characters[0]}\u0338${characters.slice(1).join("")} `; + } + if (LIMIT_OPERATORS.has(command)) { + return this.parseOperator(command, "bracket", true, true); + } + + const symbol = SYMBOLS[command]; + if (symbol !== undefined) { + if (DISPLAY_LIMIT_SYMBOLS.has(command)) { + return this.parseOperator(symbol, "script", true); + } + return command === "cdot" || command === "times" || RELATION_COMMANDS.has(command) ? ` ${symbol} ` : symbol; + } + if (NAMED_OPERATORS.has(command)) { + return `${NAMED_OPERATOR_START}${command}${NAMED_OPERATOR_END}`; + } + if (SIZE_COMMANDS.has(command)) { + return ""; + } + if (command === "left" || command === "middle" || command === "right") { + if (this.source[this.position] === ".") { + this.position++; + } + return ""; + } + if (command === "frac" || command === "dfrac" || command === "tfrac") { + const shouldStack = this.display && this.stackFractions && command !== "tfrac"; + const numerator = this.parseRequiredArgument(!shouldStack); + const denominator = this.parseRequiredArgument(!shouldStack); + if (shouldStack) { + const index = + this.layoutNodes.push({ + type: "fraction", + numerator: normalizeOutput(numerator), + denominator: normalizeOutput(denominator), + }) - 1; + return `${LAYOUT_MARKER_START}${index}${LAYOUT_MARKER_END}`; + } + return formatFraction(numerator, denominator); + } + if (command === "sqrt") { + const degree = this.parseOptionalArgument()?.trim(); + const value = this.parseRequiredArgument(); + if (degree === undefined || degree === "2") { + return formatRoot(value); + } + if (degree === "3") { + return formatRoot(value, "∛"); + } + if (degree === "4") { + return formatRoot(value, "∜"); + } + return `${formatScript(degree, "sup")}${formatRoot(value)}`; + } + if (command === "boxed" || command === "fbox") { + return `[${this.parseRequiredArgument().trim()}]`; + } + if (command === "binom" || command === "dbinom" || command === "tbinom") { + return `(${this.parseRequiredArgument()} choose ${this.parseRequiredArgument()})`; + } + const accent = ACCENTS[command]; + if (accent !== undefined) { + const value = this.parseRequiredArgument(); + return Array.from(value).length === 1 ? `${value}${accent}` : `${command}(${value})`; + } + if (command === "mathbb") { + const value = this.parseRequiredArgument(); + return Array.from(value, (character) => BLACKBOARD[character] ?? character).join(""); + } + if (command === "operatorname") { + const starred = this.source[this.position] === "*"; + if (starred) { + this.position++; + } + const operator = normalizeOutput(this.parseRequiredArgument()).trim(); + return this.parseOperator(operator, "bracket", starred, true); + } + if (command === "mod" || command === "bmod") { + return " mod "; + } + if (command === "pmod" || command === "pod") { + const value = this.parseRequiredArgument().trim(); + return command === "pmod" ? ` (mod ${value})` : ` (${value})`; + } + if (command === "overset" || command === "stackrel") { + const upper = this.parseRequiredArgument(); + const value = this.parseRequiredArgument().trim(); + return `${value}${formatScript(upper, "sup")}`; + } + if (command === "underset") { + const lower = this.parseRequiredArgument(); + const value = this.parseRequiredArgument().trim(); + return `${value}${formatScript(lower, "sub")}`; + } + if (PLAIN_WRAPPERS.has(command)) { + const value = this.parseRequiredArgument(); + return command.startsWith("text") || command === "mbox" ? value : value.trim(); + } + if (command === "begin") { + return this.parseEnvironment(); + } + if (command === "end") { + this.supported = false; + return ""; + } + + this.supported = false; + return `\\${command}`; + } + + private parseOperator( + operator: string, + inlineLowerStyle: "bracket" | "script", + displayLimits: boolean, + spaced = false, + ): string { + let useDisplayLimits = displayLimits; + let modifierPosition = this.position; + while (modifierPosition < this.source.length && /[ \t]/.test(this.source[modifierPosition] ?? "")) { + modifierPosition++; + } + const modifier = /^\\(limits|nolimits)(?![A-Za-z])/.exec(this.source.slice(modifierPosition)); + if (modifier) { + useDisplayLimits = modifier[1] === "limits"; + this.position = modifierPosition + modifier[0].length; + } + + let lower: string | undefined; + let upper: string | undefined; + while (true) { + let scriptPosition = this.position; + while (scriptPosition < this.source.length && /[ \t]/.test(this.source[scriptPosition] ?? "")) { + scriptPosition++; + } + const kind = this.source[scriptPosition]; + if (kind !== "_" && kind !== "^") { + break; + } + this.position = scriptPosition + 1; + const value = normalizeOutput(this.parseRequiredArgument(false)).replaceAll(" ", ""); + if (kind === "_") { + if (lower !== undefined) { + this.supported = false; + } + lower = value; + } else { + if (upper !== undefined) { + this.supported = false; + } + upper = value; + } + } + + if (this.display && useDisplayLimits && (lower !== undefined || upper !== undefined)) { + const index = this.layoutNodes.push({ type: "operator", operator, lower, upper }) - 1; + return `${LAYOUT_MARKER_START}${index}${LAYOUT_MARKER_END}`; + } + + let rendered = operator; + if (lower !== undefined) { + rendered += inlineLowerStyle === "bracket" ? `[${lower}]` : formatScript(lower, "sub"); + } + if (upper !== undefined) { + rendered += formatScript(upper, "sup"); + } + return spaced ? ` ${rendered} ` : rendered; + } + + private parseRequiredArgument(stackFractions = true): string { + const previousStackFractions = this.stackFractions; + this.stackFractions = previousStackFractions && stackFractions; + const value = this.parseRequiredArgumentValue(); + this.stackFractions = previousStackFractions; + return value; + } + + private parseRequiredArgumentValue(): string { + while (this.position < this.source.length && /\s/.test(this.source[this.position] ?? "")) { + this.position++; + } + if (this.position >= this.source.length) { + this.supported = false; + return ""; + } + if (this.source[this.position] === "{") { + this.position++; + return this.parseSequence("}"); + } + if (this.source[this.position] === "\\") { + return this.parseCommand(); + } + const value = this.source[this.position] ?? ""; + this.position++; + return value; + } + + private parseOptionalArgument(): string | undefined { + while (this.position < this.source.length && /[ \t]/.test(this.source[this.position] ?? "")) { + this.position++; + } + if (this.source[this.position] !== "[") { + return undefined; + } + const end = this.source.indexOf("]", this.position + 1); + if (end < 0) { + this.supported = false; + return undefined; + } + const value = this.source.slice(this.position + 1, end); + this.position = end + 1; + return this.renderNested(value); + } + + private readRawGroup(): string | undefined { + while (this.position < this.source.length && /[ \t]/.test(this.source[this.position] ?? "")) { + this.position++; + } + if (this.source[this.position] !== "{") { + this.supported = false; + return undefined; + } + + const start = ++this.position; + let depth = 1; + while (this.position < this.source.length) { + const character = this.source[this.position]; + if (character === "\\") { + this.position += 2; + continue; + } + if (character === "{") depth++; + if (character === "}") depth--; + if (depth === 0) { + const value = this.source.slice(start, this.position); + this.position++; + return value; + } + this.position++; + } + this.supported = false; + return undefined; + } + + private splitEnvironmentRows(body: string): string[] { + return body.split(/\\\\(?:\[[^\]\n]*\])?/); + } + + private parseEnvironment(): string { + const environment = this.readRawGroup(); + if (!environment) { + return ""; + } + const endMarker = `\\end{${environment}}`; + const end = this.source.indexOf(endMarker, this.position); + if (end < 0) { + this.supported = false; + return ""; + } + const body = this.source.slice(this.position, end); + this.position = end + endMarker.length; + + if (environment === "equation" || environment === "equation*" || environment === "displaymath") { + return this.renderNested(body).trim(); + } + + if ( + environment === "aligned" || + environment === "align" || + environment === "align*" || + environment === "alignedat" || + environment === "alignat" || + environment === "alignat*" || + environment === "gather" || + environment === "gathered" || + environment === "multline" || + environment === "multline*" || + environment === "split" + ) { + const alignedAt = ["alignedat", "alignat", "alignat*"].includes(environment); + const alignedBody = alignedAt ? body.replace(/^\s*\{[^}]*\}/, "") : body; + return this.splitEnvironmentRows(alignedBody) + .map((row) => { + const cells = row.split("&"); + const source = alignedAt + ? Array.from({ length: Math.ceil(cells.length / 2) }, (_, index) => + cells.slice(index * 2, index * 2 + 2).join(""), + ).join(" ") + : cells.join(""); + return this.renderNested(source).trim(); + }) + .filter(Boolean) + .join("\n"); + } + + if (environment === "cases" || environment === "cases*") { + const rows = this.splitEnvironmentRows(body) + .map((row) => row.split("&").map((cell) => this.renderNested(cell, false).trim())) + .filter((row) => row.some(Boolean)); + return rows + .map((row, index) => { + const value = (row[0] ?? "").replace(/,\s*$/, ""); + const condition = row[1] ?? ""; + const delimiter = index === 0 ? "⎧" : index === rows.length - 1 ? "⎩" : "⎨"; + const conditionPrefix = /^(?:if|when|for|otherwise)\b/i.test(condition) ? " " : " if "; + return `${delimiter} ${value}${condition ? `${conditionPrefix}${condition}` : ""}`; + }) + .join("\n"); + } + + if ( + ["array", "matrix", "smallmatrix", "pmatrix", "bmatrix", "Bmatrix", "vmatrix", "Vmatrix"].includes(environment) + ) { + const matrixBody = environment === "array" ? body.replace(/^\s*\{[^}]*\}/, "") : body; + return this.renderMatrix(environment, matrixBody); + } + + this.supported = false; + return body; + } + + private renderMatrix(environment: string, body: string): string { + const matrix = this.splitEnvironmentRows(body) + .map((row) => row.split("&").map((cell) => this.renderNested(cell, false).trim())) + .filter((row) => row.some(Boolean)); + const columnCount = Math.max(0, ...matrix.map((row) => row.length)); + const columnWidths = Array.from({ length: columnCount }, (_, column) => + Math.max(0, ...matrix.map((row) => visibleWidth(row[column] ?? ""))), + ); + const rows = matrix.map((row) => + Array.from({ length: columnCount }, (_, column) => { + const cell = row[column] ?? ""; + return `${cell}${PROTECTED_SPACE.repeat(Math.max(0, (columnWidths[column] ?? 0) - visibleWidth(cell)))}`; + }).join(" │ "), + ); + + let lines: string[]; + if (environment === "array" || environment === "matrix" || environment === "smallmatrix") { + lines = rows; + } else { + const delimiters: Readonly> = { + pmatrix: ["⎛", "⎞", "⎜", "⎟", "⎝", "⎠"], + bmatrix: ["⎡", "⎤", "⎢", "⎥", "⎣", "⎦"], + Bmatrix: ["⎧", "⎫", "⎨", "⎬", "⎩", "⎭"], + vmatrix: ["│", "│", "│", "│", "│", "│"], + Vmatrix: ["║", "║", "║", "║", "║", "║"], + }; + const delimiter = delimiters[environment]; + if (!delimiter) { + this.supported = false; + return rows.join("\n"); + } + lines = rows.map((row, index) => { + const left = index === 0 ? delimiter[0] : index === rows.length - 1 ? delimiter[4] : delimiter[2]; + const right = index === 0 ? delimiter[1] : index === rows.length - 1 ? delimiter[5] : delimiter[3]; + return `${left} ${row} ${right}`; + }); + } + + if (lines.length <= 1) { + return lines[0] ?? ""; + } + const index = this.layoutNodes.push({ type: "matrix", lines, baseline: 0 }) - 1; + return `${LAYOUT_MARKER_START}${index}${LAYOUT_MARKER_END}`; + } + + private renderNested(source: string, stackFractions = true): string { + const rendered = new LatexParser(source, this.layoutNodes, this.display && stackFractions).render(); + if (rendered === undefined) { + this.supported = false; + return source; + } + return rendered; + } +} + +export interface RenderLatexOptions { + /** Stack fractions and operator limits vertically for display math (default: false). */ + display?: boolean; +} + +/** + * Render a basic LaTeX math expression as terminal-friendly Unicode text. + * Returns undefined when the expression contains unsupported or malformed syntax. + */ +export function renderLatex(source: string, options: RenderLatexOptions = {}): string | undefined { + const layoutNodes: LayoutNode[] = []; + const rendered = new LatexParser(source, layoutNodes, options.display === true).render(); + if (rendered === undefined) { + return undefined; + } + if (layoutNodes.length === 0) { + return rendered.replaceAll(PROTECTED_SPACE, " "); + } + const lines = renderLayout(rendered, layoutNodes).lines; + const indentation = Math.min( + ...lines.filter((line) => line.trim()).map((line) => line.length - line.trimStart().length), + ); + return lines + .map((line) => line.slice(indentation).trimEnd()) + .join("\n") + .trimEnd() + .replaceAll(PROTECTED_SPACE, " "); +} diff --git a/packages/tui/src/layout-node.ts b/packages/tui/src/layout-node.ts new file mode 100644 index 0000000000000000000000000000000000000000..a3c06b24564efaa35321f190641a80e95ce7c7ab --- /dev/null +++ b/packages/tui/src/layout-node.ts @@ -0,0 +1,51 @@ +import type { Component } from "./tui.ts"; + +export const LAYOUT_NODE = Symbol.for("@earendil-works/pi-tui/layout-node"); + +export interface LayoutViewport { + width: number; + height: number; +} + +export interface StackLayoutEntry { + component: Component; + basis?: number | "auto"; + grow?: number; + shrink?: number; + minSize?: number; + maxSize?: number; + visible?: (viewport: LayoutViewport) => boolean; +} + +export interface StackLayoutNode { + type: "vstack" | "hstack"; + entries: readonly StackLayoutEntry[]; + gap: number; + align: "stretch" | "start" | "center" | "end"; +} + +export interface ScrollLayoutState { + readonly scrollTop: number; + readonly primary: boolean; + readonly overscroll: "chain" | "contain"; + readonly viewportHeight: number; + getContentWidth(width: number): number; + updateLayout(contentHeight: number, viewportHeight: number, requestRender: () => void): void; +} + +export interface ScrollLayoutNode { + type: "scroll"; + component: Component; + state: ScrollLayoutState; +} + +export type LayoutNode = StackLayoutNode | ScrollLayoutNode; + +export interface LayoutComponent extends Component { + [LAYOUT_NODE](): LayoutNode; +} + +export function getLayoutNode(component: Component): LayoutNode | undefined { + const candidate = component as Partial; + return typeof candidate[LAYOUT_NODE] === "function" ? candidate[LAYOUT_NODE]() : undefined; +} diff --git a/packages/tui/src/layout.ts b/packages/tui/src/layout.ts new file mode 100644 index 0000000000000000000000000000000000000000..897c51de2ffbc4c543e87399eaa4e8b6421449ef --- /dev/null +++ b/packages/tui/src/layout.ts @@ -0,0 +1,449 @@ +import type { ScrollView } from "./components/scroll-view.ts"; +import { allocateStackSizes, visibleStackEntries } from "./components/stack.ts"; +import { getLayoutNode } from "./layout-node.ts"; +import { cropKittyImageLine, getKittyImageMetadata, isImageLine } from "./terminal-image.ts"; +import { type Component, CURSOR_MARKER, compositeTuiLine } from "./tui.ts"; +import { + extractAnsiCode, + getActiveBackgroundAnsi, + getGraphemeCellRange, + sliceByColumn, + visibleWidth, +} from "./utils.ts"; + +const OSC133_ZONE_PREFIX = /^(?:\x1b\]133;[ABC](?:\x07|\x1b\\))+/; + +export interface LayoutRect { + x: number; + y: number; + width: number; + height: number; +} + +export interface LayoutBox { + component: Component; + rect: LayoutRect; + clip: LayoutRect; + children: LayoutBox[]; + parent?: LayoutBox; + lines?: readonly string[]; + lineOffset?: number; + scrollView?: ScrollView; + scrollContentLines?: readonly string[]; + layer: number; +} + +export interface LayoutFrame { + root: LayoutBox; + width: number; + height: number; + lines: string[]; + primaryScrollView?: ScrollView; +} + +export interface ScrollbarGeometry { + column: number; + trackTop: number; + trackHeight: number; + thumbTop: number; + thumbHeight: number; + maxScrollTop: number; +} + +interface LayoutContext { + viewport: { width: number; height: number }; + renderCache: Map>; + requestRender: () => void; + primaryScrollView: ScrollView | undefined; +} + +function intersect(a: LayoutRect, b: LayoutRect): LayoutRect { + const x = Math.max(a.x, b.x); + const y = Math.max(a.y, b.y); + const right = Math.min(a.x + a.width, b.x + b.width); + const bottom = Math.min(a.y + a.height, b.y + b.height); + return { x, y, width: Math.max(0, right - x), height: Math.max(0, bottom - y) }; +} + +function renderCached(context: LayoutContext, component: Component, width: number): string[] { + const safeWidth = Math.max(1, Math.floor(width)); + let widths = context.renderCache.get(component); + if (!widths) { + widths = new Map(); + context.renderCache.set(component, widths); + } + let lines = widths.get(safeWidth); + if (!lines) { + lines = component.render(safeWidth); + widths.set(safeWidth, lines); + } + return lines; +} + +function measureHeight(context: LayoutContext, component: Component, width: number): number { + return renderCached(context, component, width).length; +} + +function measureWidth(context: LayoutContext, component: Component, width: number): number { + return renderCached(context, component, width).reduce((max, line) => Math.max(max, visibleWidth(line)), 0); +} + +function withParent(box: LayoutBox, parent: LayoutBox): LayoutBox { + box.parent = parent; + return box; +} + +function translateBox(box: LayoutBox, deltaY: number): void { + box.rect.y += deltaY; + for (const child of box.children) translateBox(child, deltaY); +} + +function updateClips(box: LayoutBox, parentClip: LayoutRect): void { + box.clip = intersect(parentClip, box.rect); + for (const child of box.children) updateClips(child, box.clip); +} + +function layoutComponent( + context: LayoutContext, + component: Component, + x: number, + y: number, + width: number, + height: number | undefined, + clip: LayoutRect, +): LayoutBox { + const safeWidth = Math.max(1, Math.floor(width)); + const node = getLayoutNode(component); + if (!node) { + const lines = renderCached(context, component, safeWidth); + const allocatedHeight = height === undefined ? lines.length : Math.max(0, Math.floor(height)); + let lineOffset = 0; + if (lines.length > allocatedHeight && allocatedHeight > 0) { + const cursorLine = lines.findIndex((line) => line.includes(CURSOR_MARKER)); + if (cursorLine >= allocatedHeight) lineOffset = cursorLine - allocatedHeight + 1; + } + return { + component, + rect: { x, y, width: safeWidth, height: allocatedHeight }, + clip: intersect(clip, { x, y, width: safeWidth, height: allocatedHeight }), + children: [], + lines, + lineOffset, + layer: 0, + }; + } + + if (node.type === "scroll") { + const previousScrollTop = node.state.scrollTop; + const contentWidth = node.state.getContentWidth(safeWidth); + const childBox = layoutComponent( + context, + node.component, + x, + y - previousScrollTop, + contentWidth, + undefined, + clip, + ); + const contentHeight = childBox.rect.height; + const viewportHeight = height === undefined ? contentHeight : Math.max(0, Math.floor(height)); + node.state.updateLayout(contentHeight, viewportHeight, context.requestRender); + translateBox(childBox, previousScrollTop - node.state.scrollTop); + const scrollView = node.state as ScrollView; + if (node.state.primary || !context.primaryScrollView) context.primaryScrollView = scrollView; + const rect = { x, y, width: safeWidth, height: viewportHeight }; + const childClip = intersect(clip, rect); + const box: LayoutBox = { + component, + rect, + clip: childClip, + children: [childBox], + scrollView, + scrollContentLines: renderCached(context, node.component, contentWidth), + layer: 0, + }; + childBox.parent = box; + updateClips(childBox, childClip); + return box; + } + + const entries = visibleStackEntries(node.entries, context.viewport); + const gapTotal = Math.max(0, entries.length - 1) * node.gap; + if (node.type === "vstack") { + const intrinsicHeights = entries.map((entry) => + typeof entry.basis === "number" ? entry.basis : measureHeight(context, entry.component, safeWidth), + ); + const sizes = allocateStackSizes(entries, intrinsicHeights, height, node.gap); + const naturalHeight = sizes.reduce((sum, size) => sum + size, 0) + gapTotal; + const allocatedHeight = height === undefined ? naturalHeight : Math.max(0, Math.floor(height)); + const rect = { x, y, width: safeWidth, height: allocatedHeight }; + const box: LayoutBox = { + component, + rect, + clip: intersect(clip, rect), + children: [], + layer: 0, + }; + let childY = y; + for (let index = 0; index < entries.length; index++) { + box.children.push( + withParent( + layoutComponent(context, entries[index]!.component, x, childY, safeWidth, sizes[index]!, box.clip), + box, + ), + ); + childY += sizes[index]! + node.gap; + } + return box; + } + + const intrinsicWidths = entries.map((entry) => + typeof entry.basis === "number" ? entry.basis : measureWidth(context, entry.component, safeWidth), + ); + const widths = allocateStackSizes(entries, intrinsicWidths, safeWidth, node.gap); + const intrinsicHeights = entries.map((entry, index) => + measureHeight(context, entry.component, Math.max(1, widths[index]!)), + ); + const allocatedHeight = + height === undefined + ? intrinsicHeights.reduce((max, childHeight) => Math.max(max, childHeight), 0) + : Math.max(0, height); + const rect = { x, y, width: safeWidth, height: allocatedHeight }; + const box: LayoutBox = { + component, + rect, + clip: intersect(clip, rect), + children: [], + layer: 0, + }; + let childX = x; + for (let index = 0; index < entries.length; index++) { + const naturalChildHeight = intrinsicHeights[index]!; + const childHeight = node.align === "stretch" ? allocatedHeight : Math.min(allocatedHeight, naturalChildHeight); + let childY = y; + if (node.align === "center") childY += Math.floor((allocatedHeight - childHeight) / 2); + else if (node.align === "end") childY += allocatedHeight - childHeight; + const childWidth = widths[index]!; + if (childWidth === 0) { + box.children.push({ + component: entries[index]!.component, + rect: { x: childX, y: childY, width: 0, height: childHeight }, + clip: { x: childX, y: childY, width: 0, height: 0 }, + children: [], + parent: box, + layer: 0, + }); + } else { + box.children.push( + withParent( + layoutComponent(context, entries[index]!.component, childX, childY, childWidth, childHeight, box.clip), + box, + ), + ); + } + childX += childWidth + node.gap; + } + return box; +} + +function replaceScrollbarCell( + line: string, + column: number, + totalWidth: number, + replacement: string, + preserveTargetBackground: boolean, +): string { + if (isImageLine(line)) return line; + + const graphemeRange = getGraphemeCellRange(line, column); + const start = graphemeRange?.start ?? column; + const end = graphemeRange?.end ?? column + 1; + const before = sliceByColumn(line, 0, start, true); + const target = sliceByColumn(line, start, end - start, true); + const after = sliceByColumn(line, end, Math.max(0, totalWidth - end), true); + + let targetPrefix = ""; + let targetIndex = 0; + while (targetIndex < target.length) { + const ansi = extractAnsiCode(target, targetIndex); + if (!ansi) break; + targetPrefix += ansi.code; + targetIndex += ansi.length; + } + const beforePadding = " ".repeat(Math.max(0, start - visibleWidth(before))); + const cellPaddingBefore = " ".repeat(Math.max(0, column - start)); + const cellPaddingAfter = " ".repeat(Math.max(0, end - column - 1)); + const targetStyle = `\x1b[0m\x1b]8;;\x07${preserveTargetBackground ? getActiveBackgroundAnsi(targetPrefix) : ""}`; + return `${before}${beforePadding}${targetStyle}${cellPaddingBefore}${replacement}${cellPaddingAfter}${after}`; +} + +export function getScrollbarGeometry(box: LayoutBox, includeHiddenAuto = false): ScrollbarGeometry | undefined { + if (!box.scrollView || box.rect.width <= 0 || box.rect.height <= 0) return undefined; + + const contentHeight = box.children[0]?.rect.height ?? box.scrollContentLines?.length ?? 0; + const trackHeight = box.rect.height; + const canRevealHiddenAuto = includeHiddenAuto && box.scrollView.scrollbar === "auto" && contentHeight > trackHeight; + if (!box.scrollView.isScrollbarVisible && !canRevealHiddenAuto) return undefined; + + const minThumbHeight = Math.min(2, trackHeight); + const thumbHeight = Math.max( + minThumbHeight, + Math.min(trackHeight, Math.round((trackHeight * trackHeight) / contentHeight)), + ); + const maxScrollTop = Math.max(0, contentHeight - trackHeight); + const maxThumbTop = trackHeight - thumbHeight; + const thumbOffset = maxScrollTop === 0 ? 0 : Math.round((box.scrollView.scrollTop / maxScrollTop) * maxThumbTop); + const column = box.rect.x + box.rect.width - 1; + if (column < box.clip.x || column >= box.clip.x + box.clip.width) return undefined; + + return { + column, + trackTop: box.rect.y, + trackHeight, + thumbTop: box.rect.y + thumbOffset, + thumbHeight, + maxScrollTop, + }; +} + +function paintScrollbar(box: LayoutBox, screen: string[], totalWidth: number): void { + const geometry = getScrollbarGeometry(box); + if (!geometry || !box.scrollView) return; + + for (let offset = 0; offset < geometry.trackHeight; offset++) { + const row = geometry.trackTop + offset; + if (row < box.clip.y || row >= box.clip.y + box.clip.height || row < 0 || row >= screen.length) continue; + const isThumb = row >= geometry.thumbTop && row < geometry.thumbTop + geometry.thumbHeight; + const replacement = isThumb + ? box.scrollView.scrollbarThumbStyle(box.scrollView.isScrollbarActive ? "█" : "┃") + : box.scrollView.scrollbarTrackStyle("│"); + screen[row] = replaceScrollbarCell( + screen[row] ?? "", + geometry.column, + totalWidth, + replacement, + box.scrollView.scrollbar !== "always", + ); + } +} + +function paintBox(box: LayoutBox, screen: string[], totalWidth: number): void { + if (box.lines) { + const offset = box.lineOffset ?? 0; + const firstRow = Math.max(box.rect.y, box.clip.y, 0); + const lastRow = Math.min(box.rect.y + box.rect.height, box.clip.y + box.clip.height, screen.length); + for (let row = firstRow; row < lastRow; row++) { + const sourceLine = box.lines[offset + row - box.rect.y]; + if (sourceLine === undefined) continue; + let line = sourceLine.replace(OSC133_ZONE_PREFIX, ""); + const imageMetadata = getKittyImageMetadata(line); + if (imageMetadata) { + const clipBottom = Math.min(screen.length, box.clip.y + box.clip.height); + const visibleRows = Math.min(imageMetadata.rows, clipBottom - row); + if (visibleRows < imageMetadata.rows) line = cropKittyImageLine(line, 0, visibleRows); + } + // Fast path: a full-width box painting onto an untouched row can use the + // source line reference directly. Compositing here would rebuild the row + // string through ANSI/grapheme segmentation every frame; padding is + // unnecessary because rows are written with erase-line and the final + // width clamp still truncates over-wide lines. + if (box.rect.x === 0 && box.rect.width >= totalWidth && (isImageLine(line) || !screen[row])) { + screen[row] = line; + } else { + screen[row] = compositeTuiLine(screen[row] ?? "", line, box.rect.x, box.rect.width, totalWidth); + } + } + } + for (const child of box.children) paintBox(child, screen, totalWidth); + + if (box.scrollView && box.scrollContentLines && box.scrollView.scrollTop > 0 && box.rect.height > 0) { + for (let imageRow = box.scrollView.scrollTop - 1; imageRow >= 0; imageRow--) { + const imageLine = box.scrollContentLines[imageRow] ?? ""; + const metadata = getKittyImageMetadata(imageLine); + if (metadata) { + const hiddenRows = box.scrollView.scrollTop - imageRow; + if (hiddenRows < metadata.rows) { + const visibleRows = Math.min(box.rect.height, metadata.rows - hiddenRows); + const cropped = cropKittyImageLine(imageLine, hiddenRows, visibleRows); + if (box.rect.x === 0 && box.rect.width >= totalWidth) screen[box.rect.y] = cropped; + } + break; + } + if (imageLine !== "") break; + } + } + + paintScrollbar(box, screen, totalWidth); +} + +export function renderLayoutFrame( + root: Component, + width: number, + height: number, + requestRender: () => void, +): LayoutFrame { + const safeWidth = Math.max(1, Math.floor(width)); + const safeHeight = Math.max(1, Math.floor(height)); + const context: LayoutContext = { + viewport: { width: safeWidth, height: safeHeight }, + renderCache: new Map(), + requestRender, + primaryScrollView: undefined, + }; + const rootBox = layoutComponent(context, root, 0, 0, safeWidth, safeHeight, { + x: 0, + y: 0, + width: safeWidth, + height: safeHeight, + }); + const lines = Array.from({ length: safeHeight }, () => ""); + paintBox(rootBox, lines, safeWidth); + return { + root: rootBox, + width: safeWidth, + height: safeHeight, + lines, + ...(context.primaryScrollView === undefined ? {} : { primaryScrollView: context.primaryScrollView }), + }; +} + +function containsPoint(rect: LayoutRect, x: number, y: number): boolean { + return x >= rect.x && x < rect.x + rect.width && y >= rect.y && y < rect.y + rect.height; +} + +/** Return the visual hit path from the deepest component to the layout root. */ +export function getLayoutBoxesAt(frame: LayoutFrame, x: number, y: number): LayoutBox[] { + const result: Array<{ box: LayoutBox; depth: number }> = []; + const visit = (box: LayoutBox, depth: number): void => { + if (!containsPoint(box.clip, x, y)) return; + result.push({ box, depth }); + for (const child of box.children) visit(child, depth + 1); + }; + visit(frame.root, 0); + result.sort((a, b) => b.box.layer - a.box.layer || b.depth - a.depth); + return result.map(({ box }) => box); +} + +export function getScrollViewBox(frame: LayoutFrame, scrollView: ScrollView): LayoutBox | undefined { + const visit = (box: LayoutBox): LayoutBox | undefined => { + if (box.scrollView === scrollView) return box; + for (const child of box.children) { + const match = visit(child); + if (match) return match; + } + return undefined; + }; + return visit(frame.root); +} + +export function getScrollViewsAt(frame: LayoutFrame, x: number, y: number): ScrollView[] { + const result: Array<{ scrollView: ScrollView; depth: number }> = []; + const visit = (box: LayoutBox, depth: number): void => { + if (!containsPoint(box.clip, x, y)) return; + if (box.scrollView && containsPoint(box.rect, x, y)) result.push({ scrollView: box.scrollView, depth }); + for (const child of box.children) visit(child, depth + 1); + }; + visit(frame.root, 0); + result.sort((a, b) => b.depth - a.depth); + return result.map((entry) => entry.scrollView); +} diff --git a/packages/tui/src/native-modifiers.ts b/packages/tui/src/native-modifiers.ts new file mode 100644 index 0000000000000000000000000000000000000000..8cdc60d484717431bd5f1577b36f189275aa06d3 --- /dev/null +++ b/packages/tui/src/native-modifiers.ts @@ -0,0 +1,13 @@ +import { getNativePlatformHelper, type ModifierKey } from "./native-platform.ts"; + +export type { ModifierKey } from "./native-platform.ts"; + +export function isNativeModifierPressed(key: ModifierKey): boolean { + const helper = getNativePlatformHelper(); + if (!helper?.isModifierPressed) return false; + try { + return helper.isModifierPressed(key) === true; + } catch { + return false; + } +} diff --git a/packages/tui/src/native-module-path.ts b/packages/tui/src/native-module-path.ts new file mode 100644 index 0000000000000000000000000000000000000000..b75117feb7384da268cfd7d3a8c8fc6af0aeb10e --- /dev/null +++ b/packages/tui/src/native-module-path.ts @@ -0,0 +1,31 @@ +import { createRequire } from "node:module"; +import { dirname, join } from "node:path"; +import { fileURLToPath } from "node:url"; + +const moduleRequire = createRequire(import.meta.url); +const TUI_PACKAGE_NAME = "@earendil-works/pi-tui"; + +export interface NativeModuleCandidateOptions { + moduleUrl?: string; + execPath?: string; + resolvePackage?: (specifier: string) => string; +} + +export function getNativeModuleCandidates(nativePath: string, options: NativeModuleCandidateOptions = {}): string[] { + const moduleDir = dirname(fileURLToPath(options.moduleUrl ?? import.meta.url)); + const candidates: string[] = []; + + try { + const packageEntry = (options.resolvePackage ?? moduleRequire.resolve)(TUI_PACKAGE_NAME); + candidates.push(join(dirname(packageEntry), "..", nativePath)); + } catch { + // Standalone binaries do not have an installed TUI package. + } + + candidates.push( + join(moduleDir, "..", nativePath), + join(moduleDir, nativePath), + join(dirname(options.execPath ?? process.execPath), nativePath), + ); + return Array.from(new Set(candidates)); +} diff --git a/packages/tui/src/native-platform.ts b/packages/tui/src/native-platform.ts new file mode 100644 index 0000000000000000000000000000000000000000..f5e82c03580b0ca67011ee1fbc712091944d82b4 --- /dev/null +++ b/packages/tui/src/native-platform.ts @@ -0,0 +1,63 @@ +import { createRequire } from "node:module"; +import * as path from "node:path"; +import { getNativeModuleCandidates } from "./native-module-path.ts"; + +const cjsRequire = createRequire(import.meta.url); + +export type ModifierKey = "shift" | "command" | "control" | "option"; + +export interface NativeClipboard { + /** Undefined means unavailable, null means no text; transfer failures reject. */ + getText(): Promise; + /** Undefined means unavailable, null means no image; transfer failures reject. */ + getImage(): Promise; + /** Linux uses command-line tools to retain clipboard ownership instead. */ + setText?(text: string): Promise; +} + +type NativePlatformHelper = NativeClipboard & { + enableVirtualTerminalInput?: () => boolean; + isModifierPressed?: (name: ModifierKey) => boolean; +}; + +// Cache module loading, not display availability: a disconnected display can recover. +const helpers = new Map(); + +function loadNativePlatformHelper(platform: string, suffix = ""): NativePlatformHelper | undefined { + const arch = process.arch; + if (arch !== "x64" && arch !== "arm64") return undefined; + const nativePath = path.join( + "native", + platform, + "prebuilds", + `${platform}-${arch}`, + `${platform}-platform${suffix}.node`, + ); + if (helpers.has(nativePath)) return helpers.get(nativePath); + + for (const modulePath of getNativeModuleCandidates(nativePath)) { + try { + const helper = cjsRequire(modulePath) as Partial | null; + if (typeof helper?.getText === "function" && typeof helper.getImage === "function") { + helpers.set(nativePath, helper as NativePlatformHelper); + return helper as NativePlatformHelper; + } + } catch { + // Try the next possible packaging location. + } + } + helpers.set(nativePath, undefined); + return undefined; +} + +export function getNativePlatformHelper(): NativePlatformHelper | undefined { + if (process.platform !== "darwin" && process.platform !== "win32") return undefined; + return loadNativePlatformHelper(process.platform); +} + +/** Load a clipboard helper without opening the display until a read is requested. */ +export function getNativeClipboard(): NativeClipboard | undefined { + if (process.platform !== "linux") return getNativePlatformHelper(); + if (!process.env.DISPLAY) return undefined; + return loadNativePlatformHelper("linux", "-x11"); +} diff --git a/packages/tui/src/stdin-buffer.ts b/packages/tui/src/stdin-buffer.ts new file mode 100644 index 0000000000000000000000000000000000000000..cc10c61a55196eb90d9da3e1858e48cab27e925f --- /dev/null +++ b/packages/tui/src/stdin-buffer.ts @@ -0,0 +1,444 @@ +/** + * StdinBuffer buffers input and emits complete sequences. + * + * This is necessary because stdin data events can arrive in partial chunks, + * especially for escape sequences like mouse events. Without buffering, + * partial sequences can be misinterpreted as regular keypresses. + * + * For example, the mouse SGR sequence `\x1b[<35;20;5m` might arrive as: + * - Event 1: `\x1b` + * - Event 2: `[<35` + * - Event 3: `;20;5m` + * + * The buffer accumulates these until a complete sequence is detected. + * Call the `process()` method to feed input data. + * + * Based on code from OpenTUI (https://github.com/anomalyco/opentui) + * MIT License - Copyright (c) 2025 opentui + */ + +import { EventEmitter } from "events"; + +const ESC = "\x1b"; +const DEFAULT_SEQUENCE_TIMEOUT_MS = 50; +const DEFAULT_ESCAPE_TIMEOUT_MS = 10; +const BRACKETED_PASTE_START = "\x1b[200~"; +const BRACKETED_PASTE_END = "\x1b[201~"; + +/** + * Check if a string is a complete escape sequence or needs more data + */ +function isCompleteSequence(data: string): "complete" | "incomplete" | "not-escape" { + if (!data.startsWith(ESC)) { + return "not-escape"; + } + + if (data.length === 1) { + return "incomplete"; + } + + const afterEsc = data.slice(1); + + // CSI sequences: ESC [ + if (afterEsc.startsWith("[")) { + // Check for old-style mouse sequence: ESC[M + 3 bytes + if (afterEsc.startsWith("[M")) { + // Old-style mouse needs ESC[M + 3 bytes = 6 total + return data.length >= 6 ? "complete" : "incomplete"; + } + return isCompleteCsiSequence(data); + } + + // OSC sequences: ESC ] + if (afterEsc.startsWith("]")) { + return isCompleteOscSequence(data); + } + + // DCS sequences: ESC P ... ESC \ (includes XTVersion responses) + if (afterEsc.startsWith("P")) { + return isCompleteDcsSequence(data); + } + + // APC sequences: ESC _ ... ESC \ (includes Kitty graphics responses) + if (afterEsc.startsWith("_")) { + return isCompleteApcSequence(data); + } + + // SS3 sequences: ESC O + if (afterEsc.startsWith("O")) { + // ESC O followed by a single character + return afterEsc.length >= 2 ? "complete" : "incomplete"; + } + + // Meta key sequences: ESC followed by a single character + if (afterEsc.length === 1) { + return "complete"; + } + + // Unknown escape sequence - treat as complete + return "complete"; +} + +/** + * Check if CSI sequence is complete + * CSI sequences: ESC [ ... followed by a final byte (0x40-0x7E) + */ +function isCompleteCsiSequence(data: string): "complete" | "incomplete" { + if (!data.startsWith(`${ESC}[`)) { + return "complete"; + } + + // Need at least ESC [ and one more character + if (data.length < 3) { + return "incomplete"; + } + + const payload = data.slice(2); + + // CSI sequences end with a byte in the range 0x40-0x7E (@-~) + // This includes all letters and several special characters + const lastChar = payload[payload.length - 1]; + const lastCharCode = lastChar.charCodeAt(0); + + if (lastCharCode >= 0x40 && lastCharCode <= 0x7e) { + // Special handling for SGR mouse sequences + // Format: ESC[ /^\d+$/.test(p))) { + return "complete"; + } + } + + return "incomplete"; + } + + return "complete"; + } + + return "incomplete"; +} + +/** + * Check if OSC sequence is complete + * OSC sequences: ESC ] ... ST (where ST is ESC \ or BEL) + */ +function isCompleteOscSequence(data: string): "complete" | "incomplete" { + if (!data.startsWith(`${ESC}]`)) { + return "complete"; + } + + // OSC sequences end with ST (ESC \) or BEL (\x07) + if (data.endsWith(`${ESC}\\`) || data.endsWith("\x07")) { + return "complete"; + } + + return "incomplete"; +} + +/** + * Check if DCS (Device Control String) sequence is complete + * DCS sequences: ESC P ... ST (where ST is ESC \) + * Used for XTVersion responses like ESC P >| ... ESC \ + */ +function isCompleteDcsSequence(data: string): "complete" | "incomplete" { + if (!data.startsWith(`${ESC}P`)) { + return "complete"; + } + + // DCS sequences end with ST (ESC \) + if (data.endsWith(`${ESC}\\`)) { + return "complete"; + } + + return "incomplete"; +} + +/** + * Check if APC (Application Program Command) sequence is complete + * APC sequences: ESC _ ... ST (where ST is ESC \) + * Used for Kitty graphics responses like ESC _ G ... ESC \ + */ +function isCompleteApcSequence(data: string): "complete" | "incomplete" { + if (!data.startsWith(`${ESC}_`)) { + return "complete"; + } + + // APC sequences end with ST (ESC \) + if (data.endsWith(`${ESC}\\`)) { + return "complete"; + } + + return "incomplete"; +} + +/** + * Split accumulated buffer into complete sequences + */ +function parseUnmodifiedKittyPrintableCodepoint(sequence: string): number | undefined { + const match = sequence.match(/^\x1b\[(\d+)(?::\d*)?(?::\d+)?u$/); + if (!match) return undefined; + + const codepoint = parseInt(match[1]!, 10); + return codepoint >= 32 ? codepoint : undefined; +} + +function extractCompleteSequences(buffer: string): { sequences: string[]; remainder: string } { + const sequences: string[] = []; + let pos = 0; + + while (pos < buffer.length) { + const remaining = buffer.slice(pos); + + // Try to extract a sequence starting at this position + if (remaining.startsWith(ESC)) { + // Find the end of this escape sequence + let seqEnd = 1; + while (seqEnd <= remaining.length) { + const candidate = remaining.slice(0, seqEnd); + const status = isCompleteSequence(candidate); + + if (status === "complete") { + // WezTerm with enable_kitty_keyboard sends the Escape key press as a + // raw '\x1b' byte (simple text path in encode_kitty, ignoring + // DISAMBIGUATE_ESCAPE_CODES) and the release as a full Kitty CSI-u + // sequence. These arrive concatenated as '\x1b\x1b[27;...u'. + // The buffer would normally treat '\x1b\x1b' as a complete meta-key + // sequence (ESC + single char), leaving '[27;...u' to be typed as + // plain text. If the character immediately following '\x1b\x1b' + // would begin a new escape sequence, emit only the first ESC and + // restart from the second. + if (candidate === "\x1b\x1b") { + const nextChar = remaining[seqEnd]; + if ( + nextChar === "[" || // CSI + nextChar === "]" || // OSC + nextChar === "O" || // SS3 + nextChar === "P" || // DCS + nextChar === "_" // APC + ) { + sequences.push(ESC); + pos += 1; + break; + } + } + sequences.push(candidate); + pos += seqEnd; + break; + } else if (status === "incomplete") { + seqEnd++; + } else { + // Should not happen when starting with ESC + sequences.push(candidate); + pos += seqEnd; + break; + } + } + + if (seqEnd > remaining.length) { + return { sequences, remainder: remaining }; + } + } else { + // Not an escape sequence - take a single character + sequences.push(remaining[0]!); + pos++; + } + } + + return { sequences, remainder: "" }; +} + +export type StdinBufferOptions = { + /** + * Maximum time to wait for an incomplete sequence such as CSI or mouse + * (default: 50ms). + */ + timeout?: number; + /** + * Maximum time to wait after a lone ESC before treating it as Escape + * (default: 10ms). Increase for high-latency Alt+key input (SSH). + */ + escapeTimeout?: number; +}; + +export type StdinBufferEventMap = { + data: [string]; + paste: [string]; +}; + +/** + * Buffers stdin input and emits complete sequences via the 'data' event. + * Handles partial escape sequences that arrive across multiple chunks. + */ +export class StdinBuffer extends EventEmitter { + private buffer: string = ""; + private timeout: ReturnType | null = null; + private readonly timeoutMs: number; + private readonly escapeTimeoutMs: number; + private pasteMode: boolean = false; + private pasteBuffer: string = ""; + private pendingKittyPrintableCodepoint: number | undefined; + + constructor(options: StdinBufferOptions = {}) { + super(); + this.timeoutMs = options.timeout ?? DEFAULT_SEQUENCE_TIMEOUT_MS; + this.escapeTimeoutMs = options.escapeTimeout ?? DEFAULT_ESCAPE_TIMEOUT_MS; + } + + public process(data: string | Buffer): void { + // Clear any pending timeout + if (this.timeout) { + clearTimeout(this.timeout); + this.timeout = null; + } + + // Handle high-byte conversion (for compatibility with parseKeypress) + // If buffer has single byte > 127, convert to ESC + (byte - 128) + let str: string; + if (Buffer.isBuffer(data)) { + if (data.length === 1 && data[0]! > 127) { + const byte = data[0]! - 128; + str = `\x1b${String.fromCharCode(byte)}`; + } else { + str = data.toString(); + } + } else { + str = data; + } + + if (str.length === 0 && this.buffer.length === 0) { + this.emitDataSequence(""); + return; + } + + this.buffer += str; + + if (this.pasteMode) { + this.pasteBuffer += this.buffer; + this.buffer = ""; + + const endIndex = this.pasteBuffer.indexOf(BRACKETED_PASTE_END); + if (endIndex !== -1) { + const pastedContent = this.pasteBuffer.slice(0, endIndex); + const remaining = this.pasteBuffer.slice(endIndex + BRACKETED_PASTE_END.length); + + this.pasteMode = false; + this.pasteBuffer = ""; + this.pendingKittyPrintableCodepoint = undefined; + + this.emit("paste", pastedContent); + + if (remaining.length > 0) { + this.process(remaining); + } + } + return; + } + + const startIndex = this.buffer.indexOf(BRACKETED_PASTE_START); + if (startIndex !== -1) { + if (startIndex > 0) { + const beforePaste = this.buffer.slice(0, startIndex); + const result = extractCompleteSequences(beforePaste); + for (const sequence of result.sequences) { + this.emitDataSequence(sequence); + } + } + + this.pendingKittyPrintableCodepoint = undefined; + this.buffer = this.buffer.slice(startIndex + BRACKETED_PASTE_START.length); + this.pasteMode = true; + this.pasteBuffer = this.buffer; + this.buffer = ""; + + const endIndex = this.pasteBuffer.indexOf(BRACKETED_PASTE_END); + if (endIndex !== -1) { + const pastedContent = this.pasteBuffer.slice(0, endIndex); + const remaining = this.pasteBuffer.slice(endIndex + BRACKETED_PASTE_END.length); + + this.pasteMode = false; + this.pasteBuffer = ""; + this.pendingKittyPrintableCodepoint = undefined; + + this.emit("paste", pastedContent); + + if (remaining.length > 0) { + this.process(remaining); + } + } + return; + } + + const result = extractCompleteSequences(this.buffer); + this.buffer = result.remainder; + + for (const sequence of result.sequences) { + this.emitDataSequence(sequence); + } + + if (this.buffer.length > 0) { + const timeoutMs = this.buffer === ESC ? this.escapeTimeoutMs : this.timeoutMs; + this.timeout = setTimeout(() => { + const flushed = this.flush(); + + for (const sequence of flushed) { + this.emitDataSequence(sequence); + } + }, timeoutMs); + } + } + + private emitDataSequence(sequence: string): void { + const rawCodepoint = sequence.length === 1 ? sequence.codePointAt(0) : undefined; + if (rawCodepoint !== undefined && rawCodepoint === this.pendingKittyPrintableCodepoint) { + this.pendingKittyPrintableCodepoint = undefined; + return; + } + + this.pendingKittyPrintableCodepoint = parseUnmodifiedKittyPrintableCodepoint(sequence); + this.emit("data", sequence); + } + + flush(): string[] { + if (this.timeout) { + clearTimeout(this.timeout); + this.timeout = null; + } + + if (this.buffer.length === 0) { + return []; + } + + const sequences = [this.buffer]; + this.buffer = ""; + this.pendingKittyPrintableCodepoint = undefined; + return sequences; + } + + clear(): void { + if (this.timeout) { + clearTimeout(this.timeout); + this.timeout = null; + } + this.buffer = ""; + this.pasteMode = false; + this.pasteBuffer = ""; + this.pendingKittyPrintableCodepoint = undefined; + } + + getBuffer(): string { + return this.buffer; + } + + destroy(): void { + this.clear(); + } +} diff --git a/packages/tui/src/terminal-colors.ts b/packages/tui/src/terminal-colors.ts new file mode 100644 index 0000000000000000000000000000000000000000..ee2da6a2734a12842f3aa8a0fc350a09b5336166 --- /dev/null +++ b/packages/tui/src/terminal-colors.ts @@ -0,0 +1,73 @@ +export interface RgbColor { + r: number; + g: number; + b: number; +} + +export type TerminalColorScheme = "dark" | "light"; + +function hexToRgb(hex: string): RgbColor { + const normalized = hex.startsWith("#") ? hex.slice(1) : hex; + const r = parseInt(normalized.slice(0, 2), 16); + const g = parseInt(normalized.slice(2, 4), 16); + const b = parseInt(normalized.slice(4, 6), 16); + return { r, g, b }; +} + +function parseOscHexChannel(channel: string): number | undefined { + if (!/^[0-9a-f]+$/i.test(channel)) { + return undefined; + } + const max = 16 ** channel.length - 1; + if (max <= 0) { + return undefined; + } + return Math.round((parseInt(channel, 16) / max) * 255); +} + +const OSC11_BACKGROUND_COLOR_RESPONSE_PATTERN = /^\x1b\]11;([^\x07\x1b]*)(?:\x07|\x1b\\)$/i; +const COLOR_SCHEME_REPORT_PATTERN = /^(?:\x1b\[\?997;(1|2)n)+$/; + +export function isOsc11BackgroundColorResponse(data: string): boolean { + return OSC11_BACKGROUND_COLOR_RESPONSE_PATTERN.test(data); +} + +export function parseOsc11BackgroundColor(data: string): RgbColor | undefined { + const match = data.match(OSC11_BACKGROUND_COLOR_RESPONSE_PATTERN); + if (!match) { + return undefined; + } + + const value = match[1].trim(); + if (value.startsWith("#")) { + const hex = value.slice(1); + if (/^[0-9a-f]{6}$/i.test(hex)) { + return hexToRgb(value); + } + if (/^[0-9a-f]{12}$/i.test(hex)) { + const r = parseOscHexChannel(hex.slice(0, 4)); + const g = parseOscHexChannel(hex.slice(4, 8)); + const b = parseOscHexChannel(hex.slice(8, 12)); + return r !== undefined && g !== undefined && b !== undefined ? { r, g, b } : undefined; + } + return undefined; + } + + const rgbValue = value.replace(/^rgba?:/i, ""); + const [red, green, blue] = rgbValue.split("/"); + if (red === undefined || green === undefined || blue === undefined) { + return undefined; + } + const r = parseOscHexChannel(red); + const g = parseOscHexChannel(green); + const b = parseOscHexChannel(blue); + return r !== undefined && g !== undefined && b !== undefined ? { r, g, b } : undefined; +} + +export function parseTerminalColorSchemeReport(data: string): TerminalColorScheme | undefined { + const match = data.match(COLOR_SCHEME_REPORT_PATTERN); + if (!match) { + return undefined; + } + return match[1] === "2" ? "light" : "dark"; +} diff --git a/packages/tui/src/terminal-image.ts b/packages/tui/src/terminal-image.ts new file mode 100644 index 0000000000000000000000000000000000000000..a6ac5967ed1aa0ca0fa91084371a91377d1321b6 --- /dev/null +++ b/packages/tui/src/terminal-image.ts @@ -0,0 +1,696 @@ +import { execSync } from "node:child_process"; +import { homedir } from "node:os"; +import { isAbsolute } from "node:path"; +import { pathToFileURL } from "node:url"; + +export type ImageProtocol = "kitty" | "iterm2" | null; + +export interface TerminalCapabilities { + images: ImageProtocol; + trueColor: boolean; + hyperlinks: boolean; +} + +export interface CellDimensions { + widthPx: number; + heightPx: number; +} + +export interface ImageDimensions { + widthPx: number; + heightPx: number; +} + +export interface ImageRenderOptions { + maxWidthCells?: number; + maxHeightCells?: number; + preserveAspectRatio?: boolean; + /** Kitty image ID. If provided, reuses/replaces existing image with this ID. */ + imageId?: number; + /** Whether Kitty should apply its default cursor movement after placement. */ + moveCursor?: boolean; +} + +let cachedCapabilities: TerminalCapabilities | null = null; +let capabilityOverrides: Partial = {}; + +// Default cell dimensions - updated by TUI when terminal responds to query +let cellDimensions: CellDimensions = { widthPx: 9, heightPx: 18 }; + +export function getCellDimensions(): CellDimensions { + return cellDimensions; +} + +export function setCellDimensions(dims: CellDimensions): void { + cellDimensions = dims; +} + +/** + * Checks whether the attached tmux client forwards OSC 8 hyperlinks to the + * outer terminal. tmux only re-emits them when its `client_termfeatures` lists + * `hyperlinks`, and strips them otherwise. On any error fallbacks `false`. + */ +function probeTmuxHyperlinks(): boolean { + try { + const termfeatures = execSync("tmux display-message -p '#{client_termfeatures}'", { + encoding: "utf8", + timeout: 250, + stdio: ["ignore", "pipe", "ignore"], + }); + return termfeatures + .split(",") + .map((feature) => feature.trim()) + .includes("hyperlinks"); + } catch { + return false; + } +} + +function detectCapabilitiesFromEnvironment(tmuxForwardsHyperlink: () => boolean): TerminalCapabilities { + const termProgram = process.env.TERM_PROGRAM?.toLowerCase() || ""; + const terminalEmulator = process.env.TERMINAL_EMULATOR?.toLowerCase() || ""; + const term = process.env.TERM?.toLowerCase() || ""; + const colorTerm = process.env.COLORTERM?.toLowerCase() || ""; + const hasTrueColorHint = colorTerm === "truecolor" || colorTerm === "24bit"; + const isWindowsConsole = process.platform === "win32"; + + // Emit OSC 8 hyperlinks only when tmux confirms it forwards. + // Image protocols are unreliable under tmux, so leave `images: null`. + if (process.env.TMUX || term.startsWith("tmux")) { + return { images: null, trueColor: hasTrueColorHint, hyperlinks: tmuxForwardsHyperlink() }; + } + + // screen does not forward OSC 8 hyperlinks, so keep them off there. + if (term.startsWith("screen")) { + return { images: null, trueColor: hasTrueColorHint, hyperlinks: false }; + } + + if (process.env.KITTY_WINDOW_ID || termProgram === "kitty") { + return { images: "kitty", trueColor: true, hyperlinks: true }; + } + + if (termProgram === "ghostty" || term.includes("ghostty") || process.env.GHOSTTY_RESOURCES_DIR) { + return { images: "kitty", trueColor: true, hyperlinks: true }; + } + + if (process.env.WEZTERM_PANE || termProgram === "wezterm") { + return { images: "kitty", trueColor: true, hyperlinks: true }; + } + + // Warp supports the Kitty graphics protocol and OSC 8 hyperlinks. + if (termProgram === "warpterminal" || process.env.WARP_SESSION_ID || process.env.WARP_TERMINAL_SESSION_UUID) { + return { images: "kitty", trueColor: true, hyperlinks: true }; + } + + if (process.env.ITERM_SESSION_ID || termProgram === "iterm.app") { + return { images: "iterm2", trueColor: true, hyperlinks: true }; + } + + if (process.env.WT_SESSION) { + return { images: null, trueColor: true, hyperlinks: true }; + } + + if (termProgram === "alacritty" || termProgram === "vscode" || termProgram === "zed") { + return { images: null, trueColor: true, hyperlinks: true }; + } + + if (terminalEmulator === "jetbrains-jediterm") { + return { images: null, trueColor: true, hyperlinks: false }; + } + + // Windows Terminal does not always set WT_SESSION, for example when it hosts + // a cmd.exe launched directly from Win+R. Modern Windows consoles support + // truecolor; keep hyperlinks off unless we positively detected support above. + if (isWindowsConsole) { + return { images: null, trueColor: true, hyperlinks: false }; + } + + // Unknown terminal: be conservative. OSC 8 is rendered invisibly as "just + // text" on terminals that swallow it, which means the URL disappears from + // the rendered output. Default to the legacy `text (url)` behavior unless we + // have positively identified a hyperlink-capable terminal above. + return { images: null, trueColor: hasTrueColorHint, hyperlinks: false }; +} + +function parseBooleanCapabilityOverride(value: string | undefined): boolean | undefined { + return value === "1" ? true : value === "0" ? false : undefined; +} + +export function detectCapabilities(tmuxForwardsHyperlink: () => boolean = probeTmuxHyperlinks): TerminalCapabilities { + const hyperlinks = parseBooleanCapabilityOverride(process.env.PI_HYPERLINKS); + const detected = detectCapabilitiesFromEnvironment( + hyperlinks === undefined ? tmuxForwardsHyperlink : () => hyperlinks, + ); + const imageProtocol = process.env.PI_IMAGE_PROTOCOL?.toLowerCase(); + const images = + imageProtocol === "kitty" || imageProtocol === "iterm2" + ? imageProtocol + : imageProtocol === "none" || imageProtocol === "0" + ? null + : undefined; + const trueColor = parseBooleanCapabilityOverride(process.env.PI_TRUE_COLOR); + return { + ...detected, + ...(images !== undefined ? { images } : {}), + ...(trueColor !== undefined ? { trueColor } : {}), + ...(hyperlinks !== undefined ? { hyperlinks } : {}), + }; +} + +export function getCapabilities(): TerminalCapabilities { + if (!cachedCapabilities) { + const hyperlinks = capabilityOverrides.hyperlinks; + cachedCapabilities = { + ...detectCapabilities(hyperlinks === undefined ? undefined : () => hyperlinks), + ...capabilityOverrides, + }; + } + return cachedCapabilities; +} + +export function resetCapabilitiesCache(): void { + cachedCapabilities = null; +} + +/** Override selected auto-detected capabilities. */ +export function setCapabilityOverrides(overrides: Partial): void { + if ( + capabilityOverrides.images === overrides.images && + capabilityOverrides.trueColor === overrides.trueColor && + capabilityOverrides.hyperlinks === overrides.hyperlinks + ) { + return; + } + capabilityOverrides = { ...overrides }; + cachedCapabilities = null; +} + +/** Override the cached capabilities. Useful in tests to exercise both code paths. */ +export function setCapabilities(caps: TerminalCapabilities): void { + cachedCapabilities = caps; +} + +const KITTY_PREFIX = "\x1b_G"; +const ITERM2_PREFIX = "\x1b]1337;File="; + +export function isImageLine(line: string): boolean { + // Fast path: sequence at line start (single-row images) + if (line.startsWith(KITTY_PREFIX) || line.startsWith(ITERM2_PREFIX)) { + return true; + } + // Slow path: sequence elsewhere (multi-row images have cursor-up prefix) + return line.includes(KITTY_PREFIX) || line.includes(ITERM2_PREFIX); +} + +/** + * Generate a random image ID for Kitty graphics protocol. + * Uses random IDs to avoid collisions between different module instances + * (e.g., main app vs extensions). + */ +export function allocateImageId(): number { + // Use random ID in range [1, 0xffffffff] to avoid collisions + return Math.floor(Math.random() * 0xfffffffe) + 1; +} + +export function encodeKitty( + base64Data: string, + options: { + columns?: number; + rows?: number; + imageId?: number; + /** Whether Kitty should apply its default cursor movement after placement. Default: true. */ + moveCursor?: boolean; + } = {}, +): string { + const CHUNK_SIZE = 4096; + + const params: string[] = ["a=T", "f=100", "q=2"]; + + if (options.moveCursor === false) params.push("C=1"); + if (options.columns) params.push(`c=${options.columns}`); + if (options.rows) params.push(`r=${options.rows}`); + if (options.imageId) params.push(`i=${options.imageId}`); + + if (base64Data.length <= CHUNK_SIZE) { + return `\x1b_G${params.join(",")};${base64Data}\x1b\\`; + } + + const chunks: string[] = []; + let offset = 0; + let isFirst = true; + + while (offset < base64Data.length) { + const chunk = base64Data.slice(offset, offset + CHUNK_SIZE); + const isLast = offset + CHUNK_SIZE >= base64Data.length; + + if (isFirst) { + chunks.push(`\x1b_G${params.join(",")},m=1;${chunk}\x1b\\`); + isFirst = false; + } else if (isLast) { + chunks.push(`\x1b_Gm=0;${chunk}\x1b\\`); + } else { + chunks.push(`\x1b_Gm=1;${chunk}\x1b\\`); + } + + offset += CHUNK_SIZE; + } + + return chunks.join(""); +} + +/** + * Delete a Kitty graphics image by ID. + * Uses uppercase 'I' to also free the image data. + */ +export function deleteKittyImage(imageId: number): string { + return `\x1b_Ga=d,d=I,i=${imageId},q=2\x1b\\`; +} + +/** + * Delete all visible Kitty graphics images. + * Uses uppercase 'A' to also free the image data. + */ +export function deleteAllKittyImages(): string { + return "\x1b_Ga=d,d=A,q=2\x1b\\"; +} + +/** Delete all visible Kitty placements while retaining their uploaded image data. */ +export function deleteAllKittyPlacements(): string { + return "\x1b_Ga=d,d=a,q=2\x1b\\"; +} + +export function encodeITerm2( + base64Data: string, + options: { + width?: number | string; + height?: number | string; + name?: string; + preserveAspectRatio?: boolean; + inline?: boolean; + } = {}, +): string { + const params: string[] = [ + `inline=${options.inline !== false ? 1 : 0}`, + `size=${Buffer.byteLength(base64Data, "base64")}`, + ]; + + if (options.width !== undefined) params.push(`width=${options.width}`); + if (options.height !== undefined) params.push(`height=${options.height}`); + if (options.name) { + const nameBase64 = Buffer.from(options.name).toString("base64"); + params.push(`name=${nameBase64}`); + } + if (options.preserveAspectRatio === false) { + params.push("preserveAspectRatio=0"); + } + + return `\x1b]1337;File=${params.join(";")}:${base64Data}\x07`; +} + +export interface ImageCellSize { + columns: number; + rows: number; +} + +export interface KittyImageMetadata extends ImageCellSize { + imageId: number; + widthPx: number; + heightPx: number; +} + +interface RegisteredKittyImageMetadata extends KittyImageMetadata { + transmissionGeneration: number; +} + +export interface KittyImagePlacement { + imageId: number; + transmissionGeneration: number; + transmissionBytes: number; + estimatedDecodedBytes: number; + sequence: string; + replacementLine: string; +} + +const kittyImageMetadata = new Map(); +let kittyTransmissionGeneration = 0; + +export function registerKittyImageMetadata(metadata: KittyImageMetadata): void { + kittyTransmissionGeneration += 1; + kittyImageMetadata.delete(metadata.imageId); + kittyImageMetadata.set(metadata.imageId, { ...metadata, transmissionGeneration: kittyTransmissionGeneration }); + if (kittyImageMetadata.size > 1000) { + const oldestImageId = kittyImageMetadata.keys().next().value; + if (oldestImageId !== undefined) kittyImageMetadata.delete(oldestImageId); + } +} + +function getRegisteredKittyImageMetadata(line: string): RegisteredKittyImageMetadata | undefined { + const controls = /\x1b_G([^;]*);/.exec(line)?.[1]; + if (!controls) return undefined; + const imageId = /(?:^|,)i=(\d+)(?:,|$)/.exec(controls)?.[1]; + return imageId === undefined ? undefined : kittyImageMetadata.get(Number.parseInt(imageId, 10)); +} + +export function getKittyImageMetadata(line: string): KittyImageMetadata | undefined { + const metadata = getRegisteredKittyImageMetadata(line); + if (!metadata) return undefined; + return { + imageId: metadata.imageId, + columns: metadata.columns, + rows: metadata.rows, + widthPx: metadata.widthPx, + heightPx: metadata.heightPx, + }; +} + +const KITTY_PLACEMENT_CONTROL_KEYS = new Set([ + "i", + "p", + "x", + "y", + "w", + "h", + "X", + "Y", + "c", + "r", + "C", + "U", + "z", + "P", + "Q", + "H", + "V", +]); + +/** Build a placement-only command for an image line emitted by {@link renderImage}. */ +export function getKittyImagePlacement(line: string): KittyImagePlacement | undefined { + const match = /\x1b_G([^;]*);/.exec(line); + const metadata = getRegisteredKittyImageMetadata(line); + if (!match || !metadata) return undefined; + + let commandStart = match.index; + let commandControls = match[1]; + let transmissionEnd: number; + while (true) { + const terminator = line.indexOf("\x1b\\", commandStart + KITTY_PREFIX.length); + if (terminator === -1) return undefined; + transmissionEnd = terminator + 2; + if (!/(?:^|,)m=1(?:,|$)/.test(commandControls)) break; + commandStart = transmissionEnd; + if (!line.startsWith(KITTY_PREFIX, commandStart)) return undefined; + const controlsEnd = line.indexOf(";", commandStart + KITTY_PREFIX.length); + if (controlsEnd === -1) return undefined; + commandControls = line.slice(commandStart + KITTY_PREFIX.length, controlsEnd); + } + + const controls = match[1] + .split(",") + .filter((control) => KITTY_PLACEMENT_CONTROL_KEYS.has(control.split("=", 1)[0] ?? "")); + const sequence = `\x1b_Ga=p,q=2,${controls.join(",")}\x1b\\`; + return { + imageId: metadata.imageId, + transmissionGeneration: metadata.transmissionGeneration, + transmissionBytes: transmissionEnd - match.index, + estimatedDecodedBytes: metadata.widthPx * metadata.heightPx * 4, + sequence, + replacementLine: `${line.slice(0, match.index)}${sequence}${line.slice(transmissionEnd)}`, + }; +} + +export function cropKittyImageLine(line: string, hiddenRows: number, visibleRows: number): string { + const metadata = getKittyImageMetadata(line); + const match = /\x1b_G([^;]*);/.exec(line); + if (!metadata || !match || hiddenRows < 0 || hiddenRows >= metadata.rows || visibleRows <= 0) return line; + const croppedRows = Math.min(visibleRows, metadata.rows - hiddenRows); + if (hiddenRows === 0 && croppedRows === metadata.rows) return line; + const sourceY = Math.floor((metadata.heightPx * hiddenRows) / metadata.rows); + const sourceEnd = Math.ceil((metadata.heightPx * (hiddenRows + croppedRows)) / metadata.rows); + const sourceHeight = Math.max(1, Math.min(metadata.heightPx, sourceEnd) - sourceY); + const controls = match[1].split(",").filter((control) => !/^[yhr]=/.test(control)); + controls.push(`y=${sourceY}`, `h=${sourceHeight}`, `r=${croppedRows}`); + return `${line.slice(0, match.index)}\x1b_G${controls.join(",")};${line.slice(match.index + match[0].length)}`; +} + +export function calculateImageCellSize( + imageDimensions: ImageDimensions, + maxWidthCells: number, + maxHeightCells?: number, + cellDimensions: CellDimensions = { widthPx: 9, heightPx: 18 }, +): ImageCellSize { + const maxWidth = Math.max(1, Math.floor(maxWidthCells)); + const maxHeight = maxHeightCells === undefined ? undefined : Math.max(1, Math.floor(maxHeightCells)); + const imageWidth = Math.max(1, imageDimensions.widthPx); + const imageHeight = Math.max(1, imageDimensions.heightPx); + + const widthScale = (maxWidth * cellDimensions.widthPx) / imageWidth; + const heightScale = maxHeight === undefined ? widthScale : (maxHeight * cellDimensions.heightPx) / imageHeight; + const scale = Math.min(widthScale, heightScale); + + const scaledWidthPx = imageWidth * scale; + const scaledHeightPx = imageHeight * scale; + const columns = Math.ceil(scaledWidthPx / cellDimensions.widthPx); + const rows = Math.ceil(scaledHeightPx / cellDimensions.heightPx); + + return { + columns: Math.max(1, Math.min(maxWidth, columns)), + rows: Math.max(1, maxHeight === undefined ? rows : Math.min(maxHeight, rows)), + }; +} + +export function calculateImageRows( + imageDimensions: ImageDimensions, + targetWidthCells: number, + cellDimensions: CellDimensions = { widthPx: 9, heightPx: 18 }, +): number { + return calculateImageCellSize(imageDimensions, targetWidthCells, undefined, cellDimensions).rows; +} + +export function getPngDimensions(base64Data: string): ImageDimensions | null { + try { + const buffer = Buffer.from(base64Data, "base64"); + + if (buffer.length < 24) { + return null; + } + + if (buffer[0] !== 0x89 || buffer[1] !== 0x50 || buffer[2] !== 0x4e || buffer[3] !== 0x47) { + return null; + } + + const width = buffer.readUInt32BE(16); + const height = buffer.readUInt32BE(20); + + return { widthPx: width, heightPx: height }; + } catch { + return null; + } +} + +export function getJpegDimensions(base64Data: string): ImageDimensions | null { + try { + const buffer = Buffer.from(base64Data, "base64"); + + if (buffer.length < 2) { + return null; + } + + if (buffer[0] !== 0xff || buffer[1] !== 0xd8) { + return null; + } + + let offset = 2; + while (offset < buffer.length - 9) { + if (buffer[offset] !== 0xff) { + offset++; + continue; + } + + const marker = buffer[offset + 1]; + + if (marker >= 0xc0 && marker <= 0xc2) { + const height = buffer.readUInt16BE(offset + 5); + const width = buffer.readUInt16BE(offset + 7); + return { widthPx: width, heightPx: height }; + } + + if (offset + 3 >= buffer.length) { + return null; + } + const length = buffer.readUInt16BE(offset + 2); + if (length < 2) { + return null; + } + offset += 2 + length; + } + + return null; + } catch { + return null; + } +} + +export function getGifDimensions(base64Data: string): ImageDimensions | null { + try { + const buffer = Buffer.from(base64Data, "base64"); + + if (buffer.length < 10) { + return null; + } + + const sig = buffer.slice(0, 6).toString("ascii"); + if (sig !== "GIF87a" && sig !== "GIF89a") { + return null; + } + + const width = buffer.readUInt16LE(6); + const height = buffer.readUInt16LE(8); + + return { widthPx: width, heightPx: height }; + } catch { + return null; + } +} + +export function getWebpDimensions(base64Data: string): ImageDimensions | null { + try { + const buffer = Buffer.from(base64Data, "base64"); + + if (buffer.length < 30) { + return null; + } + + const riff = buffer.slice(0, 4).toString("ascii"); + const webp = buffer.slice(8, 12).toString("ascii"); + if (riff !== "RIFF" || webp !== "WEBP") { + return null; + } + + const chunk = buffer.slice(12, 16).toString("ascii"); + if (chunk === "VP8 ") { + if (buffer.length < 30) return null; + const width = buffer.readUInt16LE(26) & 0x3fff; + const height = buffer.readUInt16LE(28) & 0x3fff; + return { widthPx: width, heightPx: height }; + } else if (chunk === "VP8L") { + if (buffer.length < 25) return null; + const bits = buffer.readUInt32LE(21); + const width = (bits & 0x3fff) + 1; + const height = ((bits >> 14) & 0x3fff) + 1; + return { widthPx: width, heightPx: height }; + } else if (chunk === "VP8X") { + if (buffer.length < 30) return null; + const width = (buffer[24] | (buffer[25] << 8) | (buffer[26] << 16)) + 1; + const height = (buffer[27] | (buffer[28] << 8) | (buffer[29] << 16)) + 1; + return { widthPx: width, heightPx: height }; + } + + return null; + } catch { + return null; + } +} + +export function getImageDimensions(base64Data: string, mimeType: string): ImageDimensions | null { + if (mimeType === "image/png") { + return getPngDimensions(base64Data); + } + if (mimeType === "image/jpeg") { + return getJpegDimensions(base64Data); + } + if (mimeType === "image/gif") { + return getGifDimensions(base64Data); + } + if (mimeType === "image/webp") { + return getWebpDimensions(base64Data); + } + return null; +} + +export function renderImage( + base64Data: string, + imageDimensions: ImageDimensions, + options: ImageRenderOptions = {}, +): { sequence: string; columns: number; rows: number; imageId?: number } | null { + const caps = getCapabilities(); + + if (!caps.images) { + return null; + } + + const maxWidth = options.maxWidthCells ?? 80; + const size = calculateImageCellSize(imageDimensions, maxWidth, options.maxHeightCells, getCellDimensions()); + + if (caps.images === "kitty") { + if (options.imageId !== undefined) { + registerKittyImageMetadata({ + imageId: options.imageId, + columns: size.columns, + rows: size.rows, + widthPx: imageDimensions.widthPx, + heightPx: imageDimensions.heightPx, + }); + } + const sequence = encodeKitty(base64Data, { + columns: size.columns, + rows: size.rows, + imageId: options.imageId, + moveCursor: options.moveCursor, + }); + return { sequence, columns: size.columns, rows: size.rows, imageId: options.imageId }; + } + + if (caps.images === "iterm2") { + const sequence = encodeITerm2(base64Data, { + width: size.columns, + height: "auto", + preserveAspectRatio: options.preserveAspectRatio ?? true, + }); + return { sequence, columns: size.columns, rows: size.rows }; + } + + return null; +} + +/** + * Wrap text in an OSC 8 hyperlink sequence. + * The text is rendered as a clickable hyperlink in terminals that support OSC 8 + * (Ghostty, Kitty, WezTerm, iTerm2, VSCode, and others). + * In terminals that do not support OSC 8, the escape sequences are ignored + * and only the plain text is displayed. + * + * @param text - The visible text to display + * @param url - The URL to link to + */ +export function hyperlink(text: string, url: string): string { + return `\x1b]8;;${url}\x1b\\${text}\x1b]8;;\x1b\\`; +} + +/** Shorten home-prefixed absolute paths to ~/... for compact display. */ +function shortenImagePath(filename: string): string { + const home = homedir(); + if (home && (filename === home || filename.startsWith(`${home}/`) || filename.startsWith(`${home}\\`))) { + return `~${filename.slice(home.length)}`; + } + return filename; +} + +/** + * Text fallback when the terminal cannot render inline images. + * Absolute paths are shown shortened (~/...) and, when OSC 8 hyperlinks are + * available, linked to file:// so the full path remains openable. + */ +export function imageFallback(mimeType: string, dimensions?: ImageDimensions, filename?: string): string { + const parts: string[] = []; + if (filename) { + const display = shortenImagePath(filename); + if (getCapabilities().hyperlinks && isAbsolute(filename)) { + parts.push(hyperlink(display, pathToFileURL(filename).href)); + } else { + parts.push(display); + } + } + parts.push(`[${mimeType}]`); + if (dimensions) parts.push(`${dimensions.widthPx}x${dimensions.heightPx}`); + return `[Image: ${parts.join(" ")}]`; +} diff --git a/packages/tui/src/terminal.ts b/packages/tui/src/terminal.ts new file mode 100644 index 0000000000000000000000000000000000000000..0df03a15210d6781659f60645aee35cad7ab2105 --- /dev/null +++ b/packages/tui/src/terminal.ts @@ -0,0 +1,547 @@ +import * as fs from "node:fs"; +import * as path from "node:path"; +import { setKittyProtocolActive } from "./keys.ts"; +import { isNativeModifierPressed } from "./native-modifiers.ts"; +import { getNativePlatformHelper } from "./native-platform.ts"; +import { StdinBuffer } from "./stdin-buffer.ts"; + +const TERMINAL_PROGRESS_KEEPALIVE_MS = 1000; +const TERMINAL_PROGRESS_ACTIVE_SEQUENCE = "\x1b]9;4;3\x07"; +const TERMINAL_PROGRESS_CLEAR_SEQUENCE = "\x1b]9;4;0\x07"; +const NATIVE_SHIFT_ENTER_SEQUENCE = "\x1b[13;2u"; +const DESIRED_KITTY_KEYBOARD_PROTOCOL_FLAGS = 7; +const KEYBOARD_PROTOCOL_RESPONSE_FRAGMENT_TIMEOUT_MS = 150; +const KITTY_KEYBOARD_PROTOCOL_QUERY = `\x1b[>${DESIRED_KITTY_KEYBOARD_PROTOCOL_FLAGS}u\x1b[?u\x1b[c`; + +export type KeyboardProtocolNegotiationSequence = + | { type: "kitty-flags"; flags: number } + | { type: "device-attributes" }; + +export function parseKeyboardProtocolNegotiationSequence( + sequence: string, +): KeyboardProtocolNegotiationSequence | undefined { + const kittyFlags = sequence.match(/^\x1b\[\?(\d+)u$/); + if (kittyFlags) { + return { type: "kitty-flags", flags: Number.parseInt(kittyFlags[1]!, 10) }; + } + if (/^\x1b\[\?[\d;]*c$/.test(sequence)) { + return { type: "device-attributes" }; + } + return undefined; +} + +function isKeyboardProtocolNegotiationSequencePrefix(sequence: string): boolean { + return sequence === "\x1b[" || /^\x1b\[\?[\d;]*$/.test(sequence); +} + +export function isAppleTerminalSession(): boolean { + return process.platform === "darwin" && process.env.TERM_PROGRAM === "Apple_Terminal"; +} + +/** + * Refresh terminal dimensions on POSIX platforms by sending SIGWINCH to this process. + * Best-effort: some environments (restricted seccomp or LSM policies) return EACCES + * for `kill(2)`; in that case the dimensions refresh is skipped rather than crashing. + */ +export function refreshTerminalDimensions(): void { + if (process.platform === "win32" || process.pid <= 0) return; + try { + process.kill(process.pid, "SIGWINCH"); + } catch { + // Signal delivery not permitted in this environment; ignore. + } +} + +export function normalizeNativeShiftEnterInput( + data: string, + shouldDetectNativeShiftEnter: boolean, + isShiftPressed: boolean, +): string { + if (shouldDetectNativeShiftEnter && data === "\r" && isShiftPressed) return NATIVE_SHIFT_ENTER_SEQUENCE; + return data; +} + +export function normalizeAppleTerminalInput(data: string, isAppleTerminal: boolean, isShiftPressed: boolean): string { + return normalizeNativeShiftEnterInput(data, isAppleTerminal, isShiftPressed); +} + +/** + * Minimal terminal interface for TUI + */ +export interface Terminal { + // Start the terminal with input and resize handlers + start(onInput: (data: string) => void, onResize: () => void): void; + + // Stop the terminal and restore state + stop(): void; + + /** + * Drain stdin before exiting to prevent Kitty key release events from + * leaking to the parent shell over slow SSH connections. + * @param maxMs - Maximum time to drain (default: 1000ms) + * @param idleMs - Exit early if no input arrives within this time (default: 50ms) + */ + drainInput(maxMs?: number, idleMs?: number): Promise; + + // Write output to terminal + write(data: string): void; + + // Get terminal dimensions + get columns(): number; + get rows(): number; + + // Whether Kitty keyboard protocol is active + get kittyProtocolActive(): boolean; + + // Cursor positioning (relative to current position) + moveBy(lines: number): void; // Move cursor up (negative) or down (positive) by N lines + + // Cursor visibility + hideCursor(): void; // Hide the cursor + showCursor(): void; // Show the cursor + + // Clear operations + clearLine(): void; // Clear current line + clearFromCursor(): void; // Clear from cursor to end of screen + clearScreen(): void; // Clear entire screen and move cursor to (0,0) + + // Title operations + setTitle(title: string): void; // Set terminal window title + + // Progress indicator (OSC 9;4) + setProgress(active: boolean): void; +} + +const DEFAULT_ESCAPE_TIMEOUT_MS = 10; +const DEFAULT_SSH_ESCAPE_TIMEOUT_MS = 100; + +/** + * Resolve how long to wait for the rest of an escape sequence before + * dispatching a lone ESC as the Escape key. Legacy Alt+key input is ESC plus + * another byte, so high-latency transports need a longer reassembly window. + */ +export function resolveEscapeTimeoutMs(env: NodeJS.ProcessEnv = process.env): number { + const configured = Number(env.PI_TUI_ESC_TIMEOUT); + if (Number.isFinite(configured) && configured > 0) { + return configured; + } + if (env.SSH_CONNECTION || env.SSH_TTY) { + return DEFAULT_SSH_ESCAPE_TIMEOUT_MS; + } + return DEFAULT_ESCAPE_TIMEOUT_MS; +} + +/** + * Real terminal using process.stdin/stdout + */ +export class ProcessTerminal implements Terminal { + private wasRaw = false; + private inputHandler?: (data: string) => void; + private resizeHandler?: () => void; + private _kittyProtocolActive = false; + private _modifyOtherKeysActive = false; + private keyboardProtocolPushed = false; + private keyboardProtocolNegotiationBuffer = ""; + private keyboardProtocolBufferFlushTimer?: ReturnType; + private stdinBuffer?: StdinBuffer; + private stdinDataHandler?: (data: string) => void; + private progressInterval?: ReturnType; + private writeLogPath = (() => { + const env = process.env.PI_TUI_WRITE_LOG || ""; + if (!env) return ""; + try { + if (fs.statSync(env).isDirectory()) { + const now = new Date(); + const ts = `${now.getFullYear()}-${String(now.getMonth() + 1).padStart(2, "0")}-${String(now.getDate()).padStart(2, "0")}_${String(now.getHours()).padStart(2, "0")}-${String(now.getMinutes()).padStart(2, "0")}-${String(now.getSeconds()).padStart(2, "0")}`; + return path.join(env, `tui-${ts}-${process.pid}.log`); + } + } catch { + // Not an existing directory - use as-is (file path) + } + return env; + })(); + + get kittyProtocolActive(): boolean { + return this._kittyProtocolActive; + } + + get modifyOtherKeysActive(): boolean { + return this._modifyOtherKeysActive; + } + + start(onInput: (data: string) => void, onResize: () => void): void { + this.inputHandler = onInput; + this.resizeHandler = onResize; + + // Save previous state and enable raw mode + this.wasRaw = process.stdin.isRaw || false; + if (process.stdin.setRawMode) { + process.stdin.setRawMode(true); + } + process.stdin.setEncoding("utf8"); + process.stdin.resume(); + + // Enable bracketed paste mode - terminal will wrap pastes in \x1b[200~ ... \x1b[201~ + process.stdout.write("\x1b[?2004h"); + + // Set up resize handler immediately + process.stdout.on("resize", this.resizeHandler); + + // Refresh terminal dimensions - they may be stale after suspend/resume + // (SIGWINCH is lost while process is stopped). Unix only, best-effort. + refreshTerminalDimensions(); + + // On Windows, enable ENABLE_VIRTUAL_TERMINAL_INPUT so the console sends + // VT escape sequences (e.g. \x1b[Z for Shift+Tab) instead of raw console + // events that lose modifier information. Must run AFTER setRawMode(true) + // since that resets console mode flags. + this.enableWindowsVTInput(); + + // Query Kitty keyboard protocol and fall back to modifyOtherKeys when DA confirms no Kitty response. + // See: https://sw.kovidgoyal.net/kitty/keyboard-protocol/ + this.queryAndEnableKittyProtocol(); + } + + /** + * Set up StdinBuffer to split batched input into individual sequences. + * This ensures components receive single events, making matchesKey/isKeyRelease work correctly. + * + * Also watches for Kitty protocol response and enables it when detected. + * This is done here (after stdinBuffer parsing) rather than on raw stdin + * to handle the case where the response arrives split across multiple events. + */ + private setupStdinBuffer(): void { + this.stdinBuffer = new StdinBuffer({ escapeTimeout: resolveEscapeTimeoutMs() }); + + // Forward individual sequences to the input handler + this.stdinBuffer.on("data", (sequence) => { + const negotiationSequence = this.readKeyboardProtocolNegotiationSequence(sequence); + if (negotiationSequence === "pending") { + this.scheduleKeyboardProtocolNegotiationBufferFlush(); + return; // Wait briefly for the rest of a split Kitty response. + } + if (this.handleKeyboardProtocolNegotiationSequence(negotiationSequence)) { + return; + } + + this.forwardInputSequence(sequence); + }); + + // Re-wrap paste content with bracketed paste markers for existing editor handling + this.stdinBuffer.on("paste", (content) => { + if (this.inputHandler) { + this.inputHandler(`\x1b[200~${content}\x1b[201~`); + } + }); + + // Handler that pipes stdin data through the buffer + this.stdinDataHandler = (data: string) => { + this.stdinBuffer!.process(data); + }; + } + + /** + * Query terminal for Kitty keyboard protocol support and enable it if available. + * + * Kitty's progressive enhancement detection requires requesting the desired + * flags before querying them. The trailing DA query is a sentinel supported by + * terminals that do not know Kitty keyboard protocol; receiving DA before a + * Kitty response enables modifyOtherKeys fallback without a startup timeout. + * + * The requested flags are: + * - 1 = disambiguate escape codes + * - 2 = report event types (press/repeat/release) + * - 4 = report alternate keys (shifted key, base layout key) + */ + private queryAndEnableKittyProtocol(): void { + this.setupStdinBuffer(); + process.stdin.on("data", this.stdinDataHandler!); + this.keyboardProtocolPushed = true; + this.clearKeyboardProtocolNegotiationBuffer(); + process.stdout.write(KITTY_KEYBOARD_PROTOCOL_QUERY); + } + + private handleKeyboardProtocolNegotiationSequence( + negotiationSequence: KeyboardProtocolNegotiationSequence | undefined, + ): boolean { + if (!negotiationSequence) return false; + this.clearKeyboardProtocolNegotiationBuffer(); + if (negotiationSequence.type === "kitty-flags") { + if (negotiationSequence.flags !== 0) { + this.disableModifyOtherKeys(); + if (!this._kittyProtocolActive) { + this._kittyProtocolActive = true; + setKittyProtocolActive(true); + } + } else { + this.enableModifyOtherKeys(); + } + return true; + } + + if (!this._kittyProtocolActive) { + this.enableModifyOtherKeys(); + } + return true; + } + + private readKeyboardProtocolNegotiationSequence( + sequence: string, + ): KeyboardProtocolNegotiationSequence | "pending" | undefined { + if (this.keyboardProtocolNegotiationBuffer) { + const bufferedSequence = this.keyboardProtocolNegotiationBuffer + sequence; + const negotiationSequence = parseKeyboardProtocolNegotiationSequence(bufferedSequence); + if (negotiationSequence) { + this.clearKeyboardProtocolNegotiationBuffer(); + return negotiationSequence; + } + if (isKeyboardProtocolNegotiationSequencePrefix(bufferedSequence)) { + this.setKeyboardProtocolNegotiationBuffer(bufferedSequence); + return "pending"; + } + this.flushKeyboardProtocolNegotiationBufferAsInput(); + } + + const negotiationSequence = parseKeyboardProtocolNegotiationSequence(sequence); + if (negotiationSequence) return negotiationSequence; + if (isKeyboardProtocolNegotiationSequencePrefix(sequence)) { + this.setKeyboardProtocolNegotiationBuffer(sequence); + return "pending"; + } + return undefined; + } + + private setKeyboardProtocolNegotiationBuffer(sequence: string): void { + this.clearKeyboardProtocolNegotiationBufferFlushTimer(); + this.keyboardProtocolNegotiationBuffer = sequence; + } + + private clearKeyboardProtocolNegotiationBuffer(): void { + this.clearKeyboardProtocolNegotiationBufferFlushTimer(); + this.keyboardProtocolNegotiationBuffer = ""; + } + + private flushKeyboardProtocolNegotiationBufferAsInput(): void { + if (!this.keyboardProtocolNegotiationBuffer) return; + const sequence = this.keyboardProtocolNegotiationBuffer; + this.clearKeyboardProtocolNegotiationBuffer(); + this.forwardInputSequence(sequence); + } + + private scheduleKeyboardProtocolNegotiationBufferFlush(): void { + if (!this.keyboardProtocolNegotiationBuffer || this.keyboardProtocolBufferFlushTimer) return; + this.keyboardProtocolBufferFlushTimer = setTimeout(() => { + this.keyboardProtocolBufferFlushTimer = undefined; + this.flushKeyboardProtocolNegotiationBufferAsInput(); + }, KEYBOARD_PROTOCOL_RESPONSE_FRAGMENT_TIMEOUT_MS); + } + + private clearKeyboardProtocolNegotiationBufferFlushTimer(): void { + if (!this.keyboardProtocolBufferFlushTimer) return; + clearTimeout(this.keyboardProtocolBufferFlushTimer); + this.keyboardProtocolBufferFlushTimer = undefined; + } + + private forwardInputSequence(sequence: string): void { + if (!this.inputHandler) return; + const shouldDetectNativeShiftEnter = + sequence === "\r" && (isAppleTerminalSession() || process.platform === "win32"); + const input = normalizeNativeShiftEnterInput( + sequence, + shouldDetectNativeShiftEnter, + shouldDetectNativeShiftEnter && isNativeModifierPressed("shift"), + ); + this.inputHandler(input); + } + + private enableModifyOtherKeys(): void { + if (this._kittyProtocolActive || this._modifyOtherKeysActive) return; + process.stdout.write("\x1b[>4;2m"); + this._modifyOtherKeysActive = true; + } + + private disableModifyOtherKeys(): void { + if (!this._modifyOtherKeysActive) return; + process.stdout.write("\x1b[>4;0m"); + this._modifyOtherKeysActive = false; + } + + /** + * On Windows, add ENABLE_VIRTUAL_TERMINAL_INPUT (0x0200) to the stdin + * console handle so the terminal sends VT sequences for modified keys + * (e.g. \x1b[Z for Shift+Tab). Without this, libuv's ReadConsoleInputW + * discards modifier state and Shift+Tab arrives as plain \t. + */ + private enableWindowsVTInput(): void { + if (process.platform !== "win32") return; + try { + getNativePlatformHelper()?.enableVirtualTerminalInput?.(); + } catch { + // Native helper not available — Shift+Tab won't be distinguishable from Tab. + } + } + + async drainInput(maxMs = 1000, idleMs = 50): Promise { + const shouldDisableKittyProtocol = this.keyboardProtocolPushed || this._kittyProtocolActive; + this.clearKeyboardProtocolNegotiationBuffer(); + if (shouldDisableKittyProtocol) { + // Disable Kitty keyboard protocol first so any late key releases + // do not generate new Kitty escape sequences. + process.stdout.write("\x1b[ { + lastDataTime = Date.now(); + }; + + process.stdin.on("data", onData); + const endTime = Date.now() + maxMs; + + try { + while (true) { + const now = Date.now(); + const timeLeft = endTime - now; + if (timeLeft <= 0) break; + if (now - lastDataTime >= idleMs) break; + await new Promise((resolve) => setTimeout(resolve, Math.min(idleMs, timeLeft))); + } + } finally { + process.stdin.removeListener("data", onData); + this.inputHandler = previousHandler; + } + } + + stop(): void { + if (this.clearProgressInterval()) { + process.stdout.write(TERMINAL_PROGRESS_CLEAR_SEQUENCE); + } + + // Disable bracketed paste mode + process.stdout.write("\x1b[?2004l"); + + const shouldDisableKittyProtocol = this.keyboardProtocolPushed || this._kittyProtocolActive; + this.clearKeyboardProtocolNegotiationBuffer(); + + // Disable Kitty keyboard protocol if not already done by drainInput() + if (shouldDisableKittyProtocol) { + process.stdout.write("\x1b[ 0) { + // Move down + process.stdout.write(`\x1b[${lines}B`); + } else if (lines < 0) { + // Move up + process.stdout.write(`\x1b[${-lines}A`); + } + // lines === 0: no movement + } + + hideCursor(): void { + process.stdout.write("\x1b[?25l"); + } + + showCursor(): void { + process.stdout.write("\x1b[?25h"); + } + + clearLine(): void { + process.stdout.write("\x1b[K"); + } + + clearFromCursor(): void { + process.stdout.write("\x1b[J"); + } + + clearScreen(): void { + process.stdout.write("\x1b[2J\x1b[H"); // Clear screen and move to home (1,1) + } + + setTitle(title: string): void { + // OSC 0;title BEL - set terminal window title + process.stdout.write(`\x1b]0;${title}\x07`); + } + + setProgress(active: boolean): void { + if (active) { + // OSC 9;4;3 - indeterminate progress + process.stdout.write(TERMINAL_PROGRESS_ACTIVE_SEQUENCE); + if (!this.progressInterval) { + this.progressInterval = setInterval(() => { + process.stdout.write(TERMINAL_PROGRESS_ACTIVE_SEQUENCE); + }, TERMINAL_PROGRESS_KEEPALIVE_MS); + } + } else { + this.clearProgressInterval(); + // OSC 9;4;0 - clear progress + process.stdout.write(TERMINAL_PROGRESS_CLEAR_SEQUENCE); + } + } + + private clearProgressInterval(): boolean { + if (!this.progressInterval) return false; + clearInterval(this.progressInterval); + this.progressInterval = undefined; + return true; + } +} diff --git a/packages/tui/src/tui-alt-screen.ts b/packages/tui/src/tui-alt-screen.ts new file mode 100644 index 0000000000000000000000000000000000000000..28a9e37a4b9cfe95eda1469b7f9f422fbdd92e84 --- /dev/null +++ b/packages/tui/src/tui-alt-screen.ts @@ -0,0 +1,1721 @@ +import { + AltScreenSearchComponent, + AltScreenSearchIndex, + type AltScreenSearchMatch, + getAltScreenSearchMatchKey, +} from "./alt-screen-search.ts"; +import { AltScreenFlashContainer } from "./components/alt-screen-flash.ts"; +import { ScrollView } from "./components/scroll-view.ts"; +import { getKeybindings } from "./keybindings.ts"; +import { isKeyRelease } from "./keys.ts"; +import { + getLayoutBoxesAt, + getScrollbarGeometry, + getScrollViewBox, + getScrollViewsAt, + type LayoutFrame, + renderLayoutFrame, + type ScrollbarGeometry, +} from "./layout.ts"; +import { getLayoutNode } from "./layout-node.ts"; +import type { Terminal } from "./terminal.ts"; +import { + deleteAllKittyImages, + deleteAllKittyPlacements, + deleteKittyImage, + getCapabilities, + getKittyImagePlacement, + type ImageProtocol, + isImageLine, + setCapabilities, + type TerminalCapabilities, +} from "./terminal-image.ts"; +import { + type Component, + Container, + CURSOR_MARKER, + compositeTuiLine, + dispatchMouseEvent, + type OverlayHandle, + retargetMouseEvent, + TuiBase, + type TuiMouseButton, + type TuiMouseDispatchResult, + type TuiMouseDispatchTarget, + type TuiMouseEvent, + type TuiStopOptions, + VIEWPORT_TUI, + type ViewportTUI, +} from "./tui.ts"; +import { + extractAnsiCode, + getGraphemeCellRange, + getOsc8LinkAtColumn, + getWordSegmenter, + sliceByColumn, + stripTerminalSequences, + truncateToWidth, + visibleWidth, +} from "./utils.ts"; + +const ENTER_ALT_SCREEN = "\x1b[?1049h"; +const EXIT_ALT_SCREEN = "\x1b[?1049l"; +const DISABLE_AUTOWRAP = "\x1b[?7l"; +const ENABLE_AUTOWRAP = "\x1b[?7h"; +const ENABLE_BUTTON_MOTION_MOUSE = "\x1b[?1000h\x1b[?1002h\x1b[?1004h\x1b[?1006h"; +const ENABLE_ALL_MOTION_MOUSE = "\x1b[?1000h\x1b[?1002h\x1b[?1003h\x1b[?1004h\x1b[?1006h"; +const DISABLE_MOUSE = "\x1b[?1006l\x1b[?1004l\x1b[?1003l\x1b[?1002l\x1b[?1000l"; +const FOCUS_IN = "\x1b[I"; +const FOCUS_OUT = "\x1b[O"; +const BEGIN_SYNCHRONIZED_OUTPUT = "\x1b[?2026h"; +const END_SYNCHRONIZED_OUTPUT = "\x1b[?2026l"; +const OSC133_ZONE_PREFIX = /^(?:\x1b\]133;[ABC](?:\x07|\x1b\\))+/; +const OSC133_PROMPT_START = /^\x1b\]133;A(?:\x07|\x1b\\)/; +const PAGE_SCROLL_OVERLAP = 4; +const ALT_WHEEL_SCROLL_MULTIPLIER = 5; +const MAX_CACHED_OFFSCREEN_KITTY_IMAGES = 16; +const MAX_CACHED_OFFSCREEN_KITTY_TRANSMISSION_BYTES = 32 * 1024 * 1024; +const MAX_CACHED_OFFSCREEN_KITTY_DECODED_BYTES = 64 * 1024 * 1024; +const DOUBLE_CLICK_INTERVAL_MS = 500; +// Regular mode delegates double-click selection to the terminal emulator. Fullscreen owns mouse selection, +// so mirror common terminal word-selection behavior by keeping paths and kebab-case tokens whole. +const TERMINAL_WORD_SELECTION_JOINERS = new Set(["/", "-"]); +const wordSegmenter = getWordSegmenter(); + +interface CachedKittyImage { + transmissionGeneration: number; + transmissionBytes: number; + estimatedDecodedBytes: number; +} + +interface SelectionPoint { + row: number; + col: number; + scrollView?: ScrollView; + /** Whether this point lies between terminal cells rather than on a cell. */ + boundary?: boolean; +} + +interface SelectionRange { + start: SelectionPoint; + end: SelectionPoint; +} + +type SelectionGranularity = "character" | "word" | "line"; + +interface ClickTarget { + timestamp: number; + count: number; + row: number; + scrollView?: ScrollView; + wordStart: number; + wordEnd: number; +} + +interface SgrMouseEvent { + button: number; + x: number; + y: number; + release: boolean; +} + +interface WheelEvent { + direction: -1 | 1; + x: number; + y: number; + button: number; +} + +interface ScrollbarDrag { + scrollView: ScrollView; + grabOffset: number; +} + +interface ScrollbarTarget { + scrollView: ScrollView; + geometry: ScrollbarGeometry; +} + +interface ScrollToEndIndicatorRect { + row: number; + column: number; + width: number; +} + +type SearchSelectionMode = "query" | "retain" | "next" | "previous"; + +interface ActiveSearch { + component: AltScreenSearchComponent; + index: AltScreenSearchIndex; + overlay?: OverlayHandle; + query: string; + matches: AltScreenSearchMatch[]; + selectedIndex: number; + selectedKey?: string; + anchorRow: number; + selectionMode: SearchSelectionMode; +} + +interface SearchHighlightRange { + startCol: number; + endCol: number; + current: boolean; +} + +export interface TuiAltScreenOptions { + /** Number of logical lines moved for each mouse-wheel event. */ + wheelScrollLines?: number; + /** Capture mouse events for viewport scrolling and application-owned text selection. */ + mouse?: boolean; + /** Style a non-current transcript search match. */ + searchMatchStyle?: (text: string) => string; + /** Style the current transcript search match. */ + searchCurrentMatchStyle?: (text: string) => string; + /** Style a transcript search navigation button. */ + searchNavigationButtonStyle?: (text: string, hovered: boolean) => string; + /** + * Render a clickable jump-to-end label. It is centered on the last row of a follow-end + * primary scroll view while that view is scrolled away from its end. + */ + scrollToEndIndicator?: () => string; + /** Open an OSC 8 hyperlink activated with a primary-button click. */ + openUrl?: (url: string) => void; + /** Handle an unmodified secondary-button press for clipboard paste. Currently enabled on Windows only. */ + onRightClickPaste?: () => void; + /** Automatically copy selected text to the clipboard on mouse release (default: true). */ + copyOnSelect?: boolean; + /** + * Copy selected text to the system clipboard. Return `true` on success; the caller flashes + * an error otherwise. When omitted, the selection is copied via an OSC 52 write. + */ + copySelection?: (text: string) => Promise; +} + +/** Alternate-screen TUI with a scrollable, application-owned viewport. */ +export class TuiAltScreen extends TuiBase implements ViewportTUI { + readonly mode = "fullscreen" as const; + readonly [VIEWPORT_TUI] = true as const; + private previousScreen: string[] = []; + private lastDocument: string[] = []; + private previousScreenWidth = 0; + private previousScreenHeight = 0; + private layoutRoot: Component | undefined; + private currentLayout: LayoutFrame | undefined; + private readonly implicitDocument: Component; + private readonly implicitScrollView: ScrollView; + private readonly flashes: AltScreenFlashContainer; + private altScreenActive = false; + private imageProtocol: ImageProtocol = null; + private savedCapabilities?: TerminalCapabilities; + private readonly uploadedKittyImages = new Map(); + private selectionAnchor?: SelectionPoint; + private selectionFocus?: SelectionPoint; + private selectionGranularity: SelectionGranularity = "character"; + private selectionInitialRange?: SelectionRange; + private lastClick?: ClickTarget; + private selectionDragPointer?: { x: number; y: number }; + private selectionAutoScrollDirection: -1 | 0 | 1 = 0; + private selectionAutoScrollTimer?: NodeJS.Timeout; + private selectionPressActive = false; + private scrollbarDrag?: ScrollbarDrag; + private scrollbarHover?: ScrollView; + private scrollToEndIndicatorRect?: ScrollToEndIndicatorRect; + private activeSearch?: ActiveSearch; + private pressedUrl?: string; + private selectionDragged = false; + private mouseCapture?: TuiMouseDispatchTarget; + private mousePressTarget?: TuiMouseDispatchTarget; + private mousePressPoint?: { x: number; y: number }; + private mousePressMoved = false; + private lastComponentClick?: { + timestamp: number; + count: number; + component: Component; + x: number; + y: number; + }; + private readonly wheelScrollLines: number; + private readonly mouseEnabled: boolean; + private readonly searchMatchStyle: (text: string) => string; + private readonly searchCurrentMatchStyle: (text: string) => string; + private readonly searchNavigationButtonStyle: (text: string, hovered: boolean) => string; + private readonly scrollToEndIndicator?: () => string; + private readonly openUrl?: (url: string) => void; + private readonly onRightClickPaste?: () => void; + private copyOnSelect: boolean; + private readonly copySelection?: (text: string) => Promise; + + constructor( + terminal: Terminal, + showHardwareCursor?: boolean, + logDirectory?: string, + options: TuiAltScreenOptions = {}, + ) { + super(terminal, showHardwareCursor, logDirectory); + this.implicitDocument = { + render: (width) => super.render(width), + handleMouse: (event) => super.handleMouse(event), + invalidate: () => { + for (const child of this.children) child.invalidate(); + }, + }; + this.implicitScrollView = new ScrollView(this.implicitDocument, { follow: "end", primary: true }); + this.flashes = new AltScreenFlashContainer(() => this.requestRender()); + this.wheelScrollLines = Math.max(1, Math.floor(options.wheelScrollLines ?? 1)); + this.mouseEnabled = options.mouse ?? true; + this.searchMatchStyle = options.searchMatchStyle ?? ((text) => `\x1b[4m${text}\x1b[24m`); + this.searchCurrentMatchStyle = options.searchCurrentMatchStyle ?? ((text) => `\x1b[1;7m${text}\x1b[22;27m`); + this.searchNavigationButtonStyle = options.searchNavigationButtonStyle ?? ((text) => text); + this.scrollToEndIndicator = options.scrollToEndIndicator; + this.openUrl = options.openUrl; + this.onRightClickPaste = options.onRightClickPaste; + this.copyOnSelect = options.copyOnSelect ?? true; + this.copySelection = options.copySelection; + this.addInputListener((data) => this.handleViewportInput(data)); + } + + get viewportTop(): number { + return this.getPrimaryScrollView().scrollTop; + } + + get isFollowingOutput(): boolean { + return this.getPrimaryScrollView().isFollowingEnd; + } + + getCopyOnSelect(): boolean { + return this.copyOnSelect; + } + + setCopyOnSelect(enabled: boolean): void { + this.copyOnSelect = enabled; + } + + /** Whether the fullscreen viewport has a non-empty active text selection. */ + hasActiveSelection(): boolean { + return this.getActiveSelectionText() !== undefined; + } + + /** Copy the active fullscreen text selection, if any, using the configured selection clipboard path. */ + async copyActiveSelectionToClipboard(): Promise { + const text = this.getActiveSelectionText(); + if (!text) return false; + return this.copyTextToClipboard(text); + } + + setLayoutRoot(component: Component | undefined): void { + if (this.layoutRoot === component) return; + this.layoutRoot = component; + this.currentLayout = undefined; + this.requestRender(); + } + + override render(width: number): string[] { + return this.layoutRoot?.render(width) ?? super.render(width); + } + + protected override getMountedRoots(): readonly Component[] { + return this.layoutRoot ? [this.layoutRoot] : this.children; + } + + private getPrimaryScrollView(): ScrollView { + return this.currentLayout?.primaryScrollView ?? this.implicitScrollView; + } + + protected override beforeTerminalStart(): void { + this.stopSelectionAutoScroll(); + this.selectionPressActive = false; + this.stopScrollbarHover(); + this.stopScrollbarDrag(); + this.flashes.dispose(); + this.altScreenActive = true; + const capabilities = getCapabilities(); + this.imageProtocol = capabilities.images; + this.uploadedKittyImages.clear(); + if (capabilities.images === "iterm2") { + this.savedCapabilities = capabilities; + setCapabilities({ ...capabilities, images: null }); + this.invalidate(); + } + this.lastDocument = []; + this.selectionAnchor = undefined; + this.selectionFocus = undefined; + this.selectionGranularity = "character"; + this.selectionInitialRange = undefined; + this.lastClick = undefined; + this.pressedUrl = undefined; + this.selectionDragged = false; + this.clearComponentMouseGesture(); + this.lastComponentClick = undefined; + this.resetRenderState(); + const term = process.env.TERM?.toLowerCase() ?? ""; + // Multiplexers can lag when every pointer movement is forwarded. Button-motion + // tracking preserves clicks, wheel events, selections, and scrollbar dragging. + const mouseSequence = + process.env.TMUX !== undefined || + process.env.ZELLIJ !== undefined || + process.env.STY !== undefined || + term.startsWith("tmux") || + term.startsWith("screen") + ? ENABLE_BUTTON_MOTION_MOUSE + : ENABLE_ALL_MOTION_MOUSE; + this.terminal.write( + `${ENTER_ALT_SCREEN}${DISABLE_AUTOWRAP}${this.mouseEnabled ? mouseSequence : ""}\x1b[2J\x1b[H\x1b[?25l`, + ); + } + + protected override beforeTerminalStop(_options: TuiStopOptions): void { + this.closeSearch(); + this.stopSelectionAutoScroll(); + this.selectionPressActive = false; + this.stopScrollbarHover(); + this.stopScrollbarDrag(); + this.clearComponentMouseGesture(); + this.flashes.dispose(); + if (!this.altScreenActive) return; + this.terminal.write( + `${BEGIN_SYNCHRONIZED_OUTPUT}${this.deleteKittyImages()}${this.mouseEnabled ? DISABLE_MOUSE : ""}${ENABLE_AUTOWRAP}${END_SYNCHRONIZED_OUTPUT}`, + ); + this.uploadedKittyImages.clear(); + } + + protected override afterTerminalStop(options: TuiStopOptions): void { + if (!this.altScreenActive) return; + this.altScreenActive = false; + if (options.preserveScreen) { + this.terminal.write(`${BEGIN_SYNCHRONIZED_OUTPUT}${EXIT_ALT_SCREEN}\x1b[?25h${END_SYNCHRONIZED_OUTPUT}`); + } else { + const width = Math.max(1, this.terminal.columns); + const documentLines = this.render(width).map((line) => line.replace(OSC133_ZONE_PREFIX, "")); + this.lastDocument = this.applyLineResets(documentLines.map((line) => line.replaceAll(CURSOR_MARKER, ""))).map( + (line) => (isImageLine(line) || visibleWidth(line) <= width ? line : sliceByColumn(line, 0, width, true)), + ); + let buffer = `${BEGIN_SYNCHRONIZED_OUTPUT}${EXIT_ALT_SCREEN}${DISABLE_AUTOWRAP}`; + for (let row = 0; row < this.lastDocument.length; row++) { + if (row > 0) buffer += "\r\n"; + buffer += `\r\x1b[2K${this.lastDocument[row] ?? ""}`; + } + buffer += `\x1b[0m${ENABLE_AUTOWRAP}\r\n\x1b[?25h${END_SYNCHRONIZED_OUTPUT}`; + this.terminal.write(buffer); + } + if (this.savedCapabilities) { + setCapabilities(this.savedCapabilities); + this.savedCapabilities = undefined; + } + } + + private deleteKittyImages(): string { + return this.imageProtocol === "kitty" ? deleteAllKittyImages() : ""; + } + + private prepareKittyScreen(screen: string[]): { lines: string[]; evictedImageDeletion: string } { + const visibleImageIds = new Set(); + const lines = screen.map((line) => { + const placement = getKittyImagePlacement(line); + if (!placement) return line; + visibleImageIds.add(placement.imageId); + + const cachedImage = this.uploadedKittyImages.get(placement.imageId); + const nextCachedImage = { + transmissionGeneration: placement.transmissionGeneration, + transmissionBytes: placement.transmissionBytes, + estimatedDecodedBytes: placement.estimatedDecodedBytes, + }; + if (cachedImage) this.uploadedKittyImages.delete(placement.imageId); + this.uploadedKittyImages.set(placement.imageId, nextCachedImage); + + return cachedImage?.transmissionGeneration === placement.transmissionGeneration + ? placement.replacementLine + : line; + }); + + let cachedOffscreenImageCount = 0; + let cachedOffscreenTransmissionBytes = 0; + let cachedOffscreenDecodedBytes = 0; + for (const [imageId, cachedImage] of this.uploadedKittyImages) { + if (visibleImageIds.has(imageId)) continue; + cachedOffscreenImageCount += 1; + cachedOffscreenTransmissionBytes += cachedImage.transmissionBytes; + cachedOffscreenDecodedBytes += cachedImage.estimatedDecodedBytes; + } + + let evictedImageDeletion = ""; + for (const [imageId, cachedImage] of this.uploadedKittyImages) { + if ( + cachedOffscreenImageCount <= MAX_CACHED_OFFSCREEN_KITTY_IMAGES && + cachedOffscreenTransmissionBytes <= MAX_CACHED_OFFSCREEN_KITTY_TRANSMISSION_BYTES && + cachedOffscreenDecodedBytes <= MAX_CACHED_OFFSCREEN_KITTY_DECODED_BYTES + ) { + break; + } + if (visibleImageIds.has(imageId)) continue; + evictedImageDeletion += deleteKittyImage(imageId); + this.uploadedKittyImages.delete(imageId); + cachedOffscreenImageCount -= 1; + cachedOffscreenTransmissionBytes -= cachedImage.transmissionBytes; + cachedOffscreenDecodedBytes -= cachedImage.estimatedDecodedBytes; + } + return { lines, evictedImageDeletion }; + } + + protected override resetRenderState(): void { + this.previousScreen = []; + this.previousScreenWidth = 0; + this.previousScreenHeight = 0; + this.currentLayout = undefined; + } + + scrollBy(lines: number): void { + this.getPrimaryScrollView().scrollBy(lines); + this.requestRender(); + } + + scrollToTop(): void { + this.getPrimaryScrollView().scrollToStart(); + this.requestRender(); + } + + scrollToBottom(): void { + this.getPrimaryScrollView().scrollToEnd(); + this.requestRender(); + } + + private scrollToPrompt(direction: -1 | 1): void { + if (!this.currentLayout) return; + const scrollView = this.getPrimaryScrollView(); + const lines = getScrollViewBox(this.currentLayout, scrollView)?.scrollContentLines; + if (!lines) return; + + for (let row = scrollView.scrollTop + direction; row >= 0 && row < lines.length; row += direction) { + if (!OSC133_PROMPT_START.test(lines[row] ?? "")) continue; + scrollView.scrollTo(row); + this.requestRender(); + return; + } + } + + private toggleSearch(): void { + if (this.activeSearch) { + this.closeSearch(); + return; + } + const component = new AltScreenSearchComponent( + (query) => this.updateSearchQuery(query), + this.searchNavigationButtonStyle, + ); + const search: ActiveSearch = { + component, + index: new AltScreenSearchIndex(), + query: "", + matches: [], + selectedIndex: -1, + anchorRow: this.getPrimaryScrollView().scrollTop, + selectionMode: "query", + }; + this.activeSearch = search; + search.overlay = this.showOverlay(component, { + anchor: "top-right", + width: "40%", + minWidth: 32, + margin: 1, + }); + } + + private closeSearch(): void { + const search = this.activeSearch; + if (!search) return; + this.activeSearch = undefined; + search.overlay?.hide(); + this.requestRender(); + } + + private updateSearchQuery(query: string): void { + const search = this.activeSearch; + if (!search || query === search.query) return; + const selected = search.matches[search.selectedIndex]; + search.anchorRow = selected?.segments[0]?.row ?? this.getPrimaryScrollView().scrollTop; + search.query = query; + search.selectionMode = "query"; + search.component.setResult(-1, 0); + this.requestRender(); + } + + private navigateSearch(direction: -1 | 1): void { + const search = this.activeSearch; + if (!search?.query) return; + search.selectionMode = direction < 0 ? "previous" : "next"; + this.requestRender(); + } + + private getSearchNavigationDirectionAt(x: number, y: number): -1 | 1 | undefined { + const search = this.activeSearch; + const bounds = search?.overlay?.getBounds(); + if (!search || !bounds) return undefined; + if (x < bounds.col || x >= bounds.col + bounds.width || y < bounds.row || y >= bounds.row + bounds.height) { + return undefined; + } + return search.component.getNavigationDirectionAt(y - bounds.row, x - bounds.col); + } + + private handleSearchMouseEvent(event: SgrMouseEvent): boolean { + const search = this.activeSearch; + if (!search) return false; + const direction = this.getSearchNavigationDirectionAt(event.x, event.y); + if (search.component.setHoveredNavigationDirection(direction)) this.requestRender(); + if (direction === undefined || event.release || (event.button & 32) !== 0 || (event.button & 3) !== 0) { + return false; + } + this.navigateSearch(direction); + return true; + } + + private refreshSearch(layout: LayoutFrame): boolean { + const search = this.activeSearch; + if (!search) return false; + const scrollView = layout.primaryScrollView ?? this.implicitScrollView; + const box = getScrollViewBox(layout, scrollView); + const lines = box?.scrollContentLines; + if (!lines || !search.query.trim()) { + search.matches = []; + search.selectedIndex = -1; + search.selectedKey = undefined; + search.selectionMode = "retain"; + search.component.setResult(-1, 0); + return false; + } + + const shouldRevealSelection = search.selectionMode !== "retain"; + const result = search.index.search(lines, search.query); + const matches = result.matches; + search.matches = matches; + if (!result.changed && search.selectionMode === "retain") return false; + + const exactIndex = result.changed + ? search.selectedKey + ? matches.findIndex((match) => getAltScreenSearchMatchKey(match) === search.selectedKey) + : -1 + : search.selectedIndex; + let selectedIndex = -1; + if (matches.length > 0) { + if (search.selectionMode === "query") { + let low = 0; + let high = matches.length; + while (low < high) { + const middle = low + Math.floor((high - low) / 2); + if ((matches[middle]!.segments[0]?.row ?? 0) < search.anchorRow) low = middle + 1; + else high = middle; + } + selectedIndex = low < matches.length ? low : 0; + } else if (search.selectionMode === "next") { + const baseIndex = exactIndex >= 0 ? exactIndex : Math.min(search.selectedIndex, matches.length - 1); + selectedIndex = baseIndex < 0 ? 0 : (baseIndex + 1) % matches.length; + } else if (search.selectionMode === "previous") { + const baseIndex = exactIndex >= 0 ? exactIndex : Math.min(search.selectedIndex, matches.length - 1); + selectedIndex = baseIndex < 0 ? matches.length - 1 : (baseIndex - 1 + matches.length) % matches.length; + } else { + selectedIndex = + exactIndex >= 0 ? exactIndex : Math.min(Math.max(0, search.selectedIndex), matches.length - 1); + } + } + + search.selectedIndex = selectedIndex; + search.selectedKey = selectedIndex >= 0 ? getAltScreenSearchMatchKey(matches[selectedIndex]!) : undefined; + search.selectionMode = "retain"; + search.component.setResult(selectedIndex, matches.length); + if (!shouldRevealSelection) return false; + + const selected = matches[selectedIndex]; + const firstSegment = selected?.segments[0]; + const lastSegment = selected?.segments[selected.segments.length - 1]; + if (!box || !firstSegment || !lastSegment || scrollView.viewportHeight <= 0) return false; + const before = scrollView.scrollTop; + const visibleBottom = before + scrollView.viewportHeight - 1; + let target = before; + if (firstSegment.row < before || lastSegment.row > visibleBottom) { + target = firstSegment.row - Math.floor(scrollView.viewportHeight / 3); + } + scrollView.scrollTo(target, { disableFollow: true }); + return scrollView.scrollTop !== before; + } + + /** Show a transient message in the alternate-screen flash stack. */ + flash(message: string, durationMs?: number): void { + this.flashes.flash(message, durationMs); + } + + private shouldDeferViewportInputToOverlay(): boolean { + return this.isOverlayFocused() && this.activeSearch?.overlay?.isFocused() !== true; + } + + private clearComponentMouseGesture(): void { + this.mouseCapture = undefined; + this.mousePressTarget = undefined; + this.mousePressPoint = undefined; + this.mousePressMoved = false; + } + + private handleViewportInput(data: string): { consume?: boolean } | undefined { + if (data === FOCUS_OUT) { + const hadActiveSelection = this.selectionPressActive; + const hadNonEmptyActiveSelection = hadActiveSelection && this.getSelectionBounds() !== undefined; + this.selectionPressActive = false; + this.stopSelectionAutoScroll(); + this.stopScrollbarHover(); + if (this.activeSearch?.component.setHoveredNavigationDirection(undefined)) this.requestRender(); + this.stopScrollbarDrag(); + this.pressedUrl = undefined; + this.selectionDragged = false; + this.clearComponentMouseGesture(); + this.lastComponentClick = undefined; + if (hadActiveSelection) { + this.selectionAnchor = undefined; + this.selectionFocus = undefined; + this.selectionGranularity = "character"; + this.selectionInitialRange = undefined; + if (hadNonEmptyActiveSelection) this.requestRender(); + } + this.lastClick = undefined; + return { consume: true }; + } + if (data === FOCUS_IN) return { consume: true }; + + const wheelEvent = this.parseWheelEvent(data); + if (wheelEvent) { + const event = this.createMouseEvent("wheel", wheelEvent.button, wheelEvent.x, wheelEvent.y, { + wheelDelta: wheelEvent.direction * this.getWheelScrollLines(wheelEvent.button), + }); + const overlay = this.dispatchMouseToOverlay(event); + const result = overlay.result ?? (overlay.hit ? undefined : this.dispatchMouseToLayout(event)); + if (result) { + if (this.applyMouseDispatchResult(event, result)) this.requestRender(); + return { consume: true }; + } + if (this.shouldDeferViewportInputToOverlay()) return undefined; + this.routeWheel(wheelEvent); + return { consume: true }; + } + const mouseEvent = this.parseSgrMouseEvent(data); + if (mouseEvent) { + this.handleMouseEvent(mouseEvent); + return { consume: true }; + } + if (this.isMouseSequence(data)) return { consume: true }; + + const keybindings = getKeybindings(); + const isRelease = isKeyRelease(data); + if (keybindings.matches(data, "tui.altScreen.search")) { + if (!isRelease) this.toggleSearch(); + return { consume: true }; + } + if (this.activeSearch?.overlay?.isFocused()) { + if (keybindings.matches(data, "tui.altScreen.searchNext")) { + if (!isRelease) this.navigateSearch(1); + return { consume: true }; + } + if (keybindings.matches(data, "tui.altScreen.searchPrevious")) { + if (!isRelease) this.navigateSearch(-1); + return { consume: true }; + } + if (keybindings.matches(data, "tui.altScreen.searchClose")) { + if (!isRelease) this.closeSearch(); + return { consume: true }; + } + } + if (this.shouldDeferViewportInputToOverlay()) return undefined; + if (keybindings.matches(data, "tui.altScreen.pageUp")) { + if (!isRelease) { + this.scrollBy(-Math.max(1, this.getPrimaryScrollView().viewportHeight - PAGE_SCROLL_OVERLAP)); + } + return { consume: true }; + } + if (keybindings.matches(data, "tui.altScreen.pageDown")) { + if (!isRelease) { + this.scrollBy(Math.max(1, this.getPrimaryScrollView().viewportHeight - PAGE_SCROLL_OVERLAP)); + } + return { consume: true }; + } + if (keybindings.matches(data, "tui.altScreen.halfPageUp")) { + if (!isRelease) this.scrollBy(-Math.max(1, Math.floor(this.getPrimaryScrollView().viewportHeight / 2))); + return { consume: true }; + } + if (keybindings.matches(data, "tui.altScreen.halfPageDown")) { + if (!isRelease) this.scrollBy(Math.max(1, Math.floor(this.getPrimaryScrollView().viewportHeight / 2))); + return { consume: true }; + } + if (keybindings.matches(data, "tui.altScreen.lineUp")) { + if (!isRelease) this.scrollBy(-1); + return { consume: true }; + } + if (keybindings.matches(data, "tui.altScreen.lineDown")) { + if (!isRelease) this.scrollBy(1); + return { consume: true }; + } + if (keybindings.matches(data, "tui.altScreen.previousPrompt")) { + if (!isRelease) this.scrollToPrompt(-1); + return { consume: true }; + } + if (keybindings.matches(data, "tui.altScreen.nextPrompt")) { + if (!isRelease) this.scrollToPrompt(1); + return { consume: true }; + } + if (keybindings.matches(data, "tui.altScreen.top")) { + if (!isRelease) this.scrollToTop(); + return { consume: true }; + } + if (keybindings.matches(data, "tui.altScreen.bottom")) { + if (!isRelease) this.scrollToBottom(); + return { consume: true }; + } + return undefined; + } + + private decodeMouseButton(button: number): TuiMouseButton { + switch (button & 3) { + case 0: + return "left"; + case 1: + return "middle"; + case 2: + return "right"; + default: + return "none"; + } + } + + private createMouseEvent( + type: TuiMouseEvent["type"], + button: number, + x: number, + y: number, + extra: Partial> = {}, + ): TuiMouseEvent { + return { + type, + button: type === "wheel" ? "none" : this.decodeMouseButton(button), + x, + y, + screenX: x, + screenY: y, + width: Math.max(1, this.terminal.columns), + height: Math.max(1, this.terminal.rows), + shift: (button & 4) !== 0, + alt: (button & 8) !== 0, + ctrl: (button & 16) !== 0, + ...(extra.wheelDelta === undefined ? {} : { wheelDelta: extra.wheelDelta }), + ...(extra.clickCount === undefined ? {} : { clickCount: extra.clickCount }), + }; + } + + private dispatchMouseToLayout(event: TuiMouseEvent): TuiMouseDispatchResult | undefined { + if (!this.currentLayout) return undefined; + const visited = new Set(); + const boxes = getLayoutBoxesAt(this.currentLayout, event.screenX, event.screenY); + for (const box of boxes) { + if (visited.has(box.component)) continue; + if (getLayoutNode(box.component) && box.component.handleMouse === Container.prototype.handleMouse) continue; + visited.add(box.component); + const result = dispatchMouseEvent(box.component, { + ...event, + x: event.screenX - box.rect.x, + y: event.screenY - box.rect.y, + width: box.rect.width, + height: box.rect.height, + }); + if (result) return result; + } + return undefined; + } + + private applyMouseDispatchResult(event: TuiMouseEvent, result: TuiMouseDispatchResult): boolean { + const focusTarget = this.resolveMouseFocusTarget(result.focusTarget ?? result.target.component); + const focusChanged = result.focus === true && this.getFocusedComponent() !== focusTarget; + if (result.focus) this.setFocus(focusTarget); + if (result.capture) this.mouseCapture = result.target; + return ( + result.render ?? + (focusChanged || + event.type === "press" || + event.type === "click" || + event.type === "drag" || + event.type === "wheel") + ); + } + + private dispatchMouseToTarget( + event: TuiMouseEvent, + target: TuiMouseDispatchTarget, + ): TuiMouseDispatchResult | undefined { + return dispatchMouseEvent(target.component, retargetMouseEvent(event, target)); + } + + private getComponentClickCount(target: TuiMouseDispatchTarget, x: number, y: number): number { + const now = Date.now(); + const previous = this.lastComponentClick; + const count = + previous && + now - previous.timestamp <= DOUBLE_CLICK_INTERVAL_MS && + previous.component === target.component && + previous.x === x && + previous.y === y + ? (previous.count % 3) + 1 + : 1; + this.lastComponentClick = { timestamp: now, count, component: target.component, x, y }; + return count; + } + + private clearTextSelection(): void { + this.stopSelectionAutoScroll(); + this.selectionPressActive = false; + this.selectionAnchor = undefined; + this.selectionFocus = undefined; + this.selectionGranularity = "character"; + this.selectionInitialRange = undefined; + this.pressedUrl = undefined; + this.selectionDragged = false; + } + + private handleMouseEvent(raw: SgrMouseEvent): void { + const isMotion = (raw.button & 32) !== 0; + const type: TuiMouseEvent["type"] = raw.release + ? "release" + : isMotion + ? this.decodeMouseButton(raw.button) === "none" + ? "move" + : "drag" + : "press"; + const event = this.createMouseEvent(type, raw.button, raw.x, raw.y); + + if (this.mouseCapture || this.mousePressTarget) { + const target = this.mouseCapture ?? this.mousePressTarget!; + if (this.mousePressPoint && (raw.x !== this.mousePressPoint.x || raw.y !== this.mousePressPoint.y)) { + this.mousePressMoved = true; + this.lastComponentClick = undefined; + } + let render = false; + const targetResult = this.dispatchMouseToTarget(event, target); + if (targetResult) render = this.applyMouseDispatchResult(event, targetResult); + if (raw.release) { + if (!this.mousePressMoved && this.mousePressPoint?.x === raw.x && this.mousePressPoint.y === raw.y) { + const clickEvent = this.createMouseEvent("click", raw.button, raw.x, raw.y, { + clickCount: this.getComponentClickCount(target, raw.x, raw.y), + }); + const clickResult = this.dispatchMouseToTarget(clickEvent, target); + if (clickResult) render = this.applyMouseDispatchResult(clickEvent, clickResult) || render; + } + this.clearComponentMouseGesture(); + } + if (render) this.requestRender(); + return; + } + + if (this.handleSearchMouseEvent(raw)) return; + + const overlay = this.dispatchMouseToOverlay(event); + if (!overlay.hit) { + if (this.handleScrollToEndIndicatorMouseEvent(raw)) return; + const scrollbarHandled = this.handleScrollbarMouseEvent(raw); + if (!this.scrollbarDrag) this.updateScrollbarHover(raw.x, raw.y); + if (scrollbarHandled) return; + } else { + this.stopScrollbarHover(); + } + + const result = overlay.result ?? (overlay.hit ? undefined : this.dispatchMouseToLayout(event)); + if (result) { + const render = this.applyMouseDispatchResult(event, result); + if (type === "press") { + this.clearTextSelection(); + this.mousePressTarget = result.target; + this.mousePressPoint = { x: raw.x, y: raw.y }; + this.mousePressMoved = false; + } + if (render) this.requestRender(); + return; + } + + if (this.handleRightClickPaste(raw)) return; + this.handleSelectionMouseEvent(raw); + } + + private parseWheelEvent(data: string): WheelEvent | undefined { + const sgr = /^\x1b\[<(\d+);(\d+);(\d+)[Mm]$/.exec(data); + if (sgr) { + const button = Number.parseInt(sgr[1], 10); + if ((button & 64) === 0) return undefined; + const direction = button & 3; + if (direction !== 0 && direction !== 1) return undefined; + return { + direction: direction === 0 ? -1 : 1, + x: Number.parseInt(sgr[2], 10) - 1, + y: Number.parseInt(sgr[3], 10) - 1, + button, + }; + } + if (data.length === 6 && data.startsWith("\x1b[M")) { + const button = data.charCodeAt(3) - 32; + if ((button & 64) === 0) return undefined; + const direction = button & 3; + if (direction !== 0 && direction !== 1) return undefined; + return { + direction: direction === 0 ? -1 : 1, + x: data.charCodeAt(4) - 33, + y: data.charCodeAt(5) - 33, + button, + }; + } + return undefined; + } + + private getWheelScrollLines(button: number): number { + // SGR mouse button codes use bit 3 (value 8) for the Alt modifier. + return (button & 8) !== 0 ? this.wheelScrollLines * ALT_WHEEL_SCROLL_MULTIPLIER : this.wheelScrollLines; + } + + private routeWheel(event: WheelEvent): void { + let remaining = event.direction * this.getWheelScrollLines(event.button); + const seen = new Set(); + for (const scrollView of this.currentLayout ? getScrollViewsAt(this.currentLayout, event.x, event.y) : []) { + seen.add(scrollView); + remaining = scrollView.scrollBy(remaining); + if (remaining === 0 || scrollView.overscroll === "contain") break; + } + const primary = this.getPrimaryScrollView(); + if (remaining !== 0 && !seen.has(primary)) primary.scrollBy(remaining); + this.updateScrollbarHover(event.x, event.y); + this.requestRender(); + } + + private parseSgrMouseEvent(data: string): SgrMouseEvent | undefined { + const match = /^\x1b\[<(\d+);(\d+);(\d+)([Mm])$/.exec(data); + if (!match) return undefined; + return { + button: Number.parseInt(match[1], 10), + x: Number.parseInt(match[2], 10) - 1, + y: Number.parseInt(match[3], 10) - 1, + release: match[4] === "m", + }; + } + + private handleRightClickPaste(event: SgrMouseEvent): boolean { + if ( + !this.onRightClickPaste || + process.platform !== "win32" || + process.env.TERM_PROGRAM?.toLowerCase() === "vscode" || + event.release || + event.button !== 2 + ) { + return false; + } + try { + this.onRightClickPaste(); + } catch { + // Clipboard paste is best-effort. + } + return true; + } + + private handleScrollToEndIndicatorMouseEvent(event: SgrMouseEvent): boolean { + const rect = this.scrollToEndIndicatorRect; + if (!rect || event.release || (event.button & 32) !== 0 || (event.button & 3) !== 0) return false; + if (event.y !== rect.row || event.x < rect.column || event.x >= rect.column + rect.width) return false; + this.scrollToBottom(); + return true; + } + + private getScrollbarTargetAt(x: number, y: number, includeHiddenAuto = false): ScrollbarTarget | undefined { + if (this.hasOverlay() || !this.currentLayout) return undefined; + for (const scrollView of getScrollViewsAt(this.currentLayout, x, y)) { + const box = getScrollViewBox(this.currentLayout, scrollView); + const geometry = box ? getScrollbarGeometry(box, includeHiddenAuto) : undefined; + if ( + geometry && + x === geometry.column && + y >= geometry.trackTop && + y < geometry.trackTop + geometry.trackHeight + ) { + return { scrollView, geometry }; + } + } + return undefined; + } + + private setScrollbarHover(scrollView: ScrollView | undefined): void { + if (scrollView === this.scrollbarHover) return; + this.scrollbarHover?.setScrollbarActive(false); + this.scrollbarHover = scrollView; + this.scrollbarHover?.setScrollbarActive(true); + } + + private updateScrollbarHover(x: number, y: number): void { + this.setScrollbarHover(this.getScrollbarTargetAt(x, y, true)?.scrollView); + } + + private stopScrollbarHover(): void { + this.setScrollbarHover(undefined); + } + + private scrollScrollbarToPointer( + scrollView: ScrollView, + geometry: ScrollbarGeometry, + pointerY: number, + grabOffset: number, + ): void { + const maxThumbOffset = geometry.trackHeight - geometry.thumbHeight; + const thumbOffset = Math.max(0, Math.min(maxThumbOffset, pointerY - geometry.trackTop - grabOffset)); + const scrollTop = maxThumbOffset === 0 ? 0 : Math.round((thumbOffset / maxThumbOffset) * geometry.maxScrollTop); + scrollView.scrollTo(scrollTop); + } + + private handleScrollbarMouseEvent(event: SgrMouseEvent): boolean { + if (this.scrollbarDrag) { + if (event.release) { + this.stopScrollbarDrag(); + return true; + } + const box = this.currentLayout + ? getScrollViewBox(this.currentLayout, this.scrollbarDrag.scrollView) + : undefined; + const geometry = box ? getScrollbarGeometry(box) : undefined; + if (geometry) { + this.scrollScrollbarToPointer( + this.scrollbarDrag.scrollView, + geometry, + event.y, + this.scrollbarDrag.grabOffset, + ); + } + return true; + } + + if (event.release || (event.button & 32) !== 0 || (event.button & 3) !== 0) return false; + const target = this.getScrollbarTargetAt(event.x, event.y); + if (!target) return false; + this.stopSelectionAutoScroll(); + this.selectionPressActive = false; + this.selectionAnchor = undefined; + this.selectionFocus = undefined; + this.selectionGranularity = "character"; + this.selectionInitialRange = undefined; + this.lastClick = undefined; + this.pressedUrl = undefined; + this.selectionDragged = false; + this.setScrollbarHover(target.scrollView); + const onThumb = + event.y >= target.geometry.thumbTop && event.y < target.geometry.thumbTop + target.geometry.thumbHeight; + const grabOffset = onThumb ? event.y - target.geometry.thumbTop : Math.floor(target.geometry.thumbHeight / 2); + if (!onThumb) this.scrollScrollbarToPointer(target.scrollView, target.geometry, event.y, grabOffset); + this.scrollbarDrag = { + scrollView: target.scrollView, + grabOffset, + }; + return true; + } + + private stopScrollbarDrag(): void { + this.scrollbarDrag = undefined; + } + + private getScrollSelectionPoint(scrollView: ScrollView, x: number, y: number): SelectionPoint | undefined { + if (!this.currentLayout) return undefined; + const box = getScrollViewBox(this.currentLayout, scrollView); + if (!box || box.rect.height <= 0 || box.clip.height <= 0) return undefined; + const visibleTop = Math.max(0, box.rect.y, box.clip.y); + const visibleBottom = Math.min( + this.terminal.rows - 1, + box.rect.y + box.rect.height - 1, + box.clip.y + box.clip.height - 1, + ); + if (visibleBottom < visibleTop) return undefined; + const pointerRow = Math.max(visibleTop, Math.min(visibleBottom, y)); + const maxContentRow = Math.max(0, (box.scrollContentLines?.length ?? 1) - 1); + return { + row: Math.max(0, Math.min(maxContentRow, scrollView.scrollTop + pointerRow - box.rect.y)), + col: Math.max(0, Math.min(box.rect.width - 1, x - box.rect.x)), + scrollView, + }; + } + + private getSelectionPoint(event: SgrMouseEvent, scrollView?: ScrollView): SelectionPoint { + if (scrollView) { + const point = this.getScrollSelectionPoint(scrollView, event.x, event.y); + if (point) return point; + } + return { + row: Math.max(0, Math.min(this.terminal.rows - 1, event.y)), + col: Math.max(0, Math.min(this.terminal.columns - 1, event.x)), + }; + } + + private getSelectionSourceLine(point: SelectionPoint): string { + if (point.scrollView && this.currentLayout) { + const lines = getScrollViewBox(this.currentLayout, point.scrollView)?.scrollContentLines; + if (lines) return lines[point.row] ?? ""; + } + return this.previousScreen[point.row] ?? ""; + } + + private getWordSelection(point: SelectionPoint): SelectionRange | undefined { + const line = stripTerminalSequences(this.getSelectionSourceLine(point)); + const segments: Array<{ start: number; end: number; selectable: boolean; joiner: boolean }> = []; + let start = 0; + for (const segment of wordSegmenter.segment(line)) { + const end = start + visibleWidth(segment.segment); + const joiner = TERMINAL_WORD_SELECTION_JOINERS.has(segment.segment); + segments.push({ start, end, selectable: segment.isWordLike === true || joiner, joiner }); + start = end; + } + const clickedSegmentIndex = segments.findIndex( + (segment) => point.col >= segment.start && point.col < segment.end, + ); + if (clickedSegmentIndex < 0) return undefined; + + const canJoin = ( + left: { selectable: boolean; joiner: boolean }, + right: { selectable: boolean; joiner: boolean }, + ): boolean => left.selectable && right.selectable && (left.joiner || right.joiner); + let selectionStart = segments[clickedSegmentIndex].start; + let selectionEnd = segments[clickedSegmentIndex].end; + for (let index = clickedSegmentIndex; index > 0 && canJoin(segments[index - 1], segments[index]); index--) { + selectionStart = segments[index - 1].start; + } + for ( + let index = clickedSegmentIndex; + index < segments.length - 1 && canJoin(segments[index], segments[index + 1]); + index++ + ) { + selectionEnd = segments[index + 1].end; + } + return { + start: { ...point, col: selectionStart }, + end: { ...point, col: selectionEnd, boundary: true }, + }; + } + + private getLineSelection(point: SelectionPoint): SelectionRange { + return { + start: { ...point, col: 0 }, + end: { ...point, col: visibleWidth(this.getSelectionSourceLine(point)), boundary: true }, + }; + } + + private updateSelectionFocus(point: SelectionPoint): void { + if (this.selectionGranularity === "character" || !this.selectionInitialRange) { + this.selectionFocus = point; + return; + } + const range = this.selectionGranularity === "word" ? this.getWordSelection(point) : this.getLineSelection(point); + if (!range) return; + const initial = this.selectionInitialRange; + const targetBeforeInitial = + range.start.row < initial.start.row || + (range.start.row === initial.start.row && range.start.col < initial.start.col); + if (targetBeforeInitial) { + this.selectionAnchor = initial.end; + this.selectionFocus = range.start; + } else { + this.selectionAnchor = initial.start; + this.selectionFocus = range.end; + } + } + + private getClickCount(point: SelectionPoint, word: SelectionRange | undefined): number { + const now = Date.now(); + const previous = this.lastClick; + const count = + word && + previous && + now - previous.timestamp <= DOUBLE_CLICK_INTERVAL_MS && + previous.row === point.row && + previous.scrollView === point.scrollView && + previous.wordStart === word.start.col && + previous.wordEnd === word.end.col + ? (previous.count % 3) + 1 + : 1; + this.lastClick = word + ? { + timestamp: now, + count, + row: point.row, + scrollView: point.scrollView, + wordStart: word.start.col, + wordEnd: word.end.col, + } + : undefined; + return count; + } + + private updateSelectionAutoScroll(event: SgrMouseEvent): void { + const scrollView = this.selectionAnchor?.scrollView; + if (!scrollView || !this.currentLayout) { + this.stopSelectionAutoScroll(); + return; + } + const box = getScrollViewBox(this.currentLayout, scrollView); + if (!box || box.rect.height <= 0 || box.clip.height <= 0) { + this.stopSelectionAutoScroll(); + return; + } + const visibleTop = Math.max(0, box.rect.y, box.clip.y); + const visibleBottom = Math.min( + this.terminal.rows - 1, + box.rect.y + box.rect.height - 1, + box.clip.y + box.clip.height - 1, + ); + this.selectionDragPointer = { x: event.x, y: event.y }; + this.selectionAutoScrollDirection = event.y <= visibleTop ? -1 : event.y >= visibleBottom ? 1 : 0; + if (this.selectionAutoScrollDirection === 0) { + this.stopSelectionAutoScroll(); + return; + } + if (this.selectionAutoScrollTimer) return; + this.selectionAutoScrollTimer = setInterval(() => this.autoScrollSelection(), 50); + this.selectionAutoScrollTimer.unref(); + } + + private autoScrollSelection(): void { + const scrollView = this.selectionAnchor?.scrollView; + const pointer = this.selectionDragPointer; + const direction = this.selectionAutoScrollDirection; + if (!scrollView || !pointer || direction === 0) { + this.stopSelectionAutoScroll(); + return; + } + const remaining = scrollView.scrollBy(direction); + if (remaining === direction) { + this.stopSelectionAutoScroll(); + return; + } + const point = this.getScrollSelectionPoint(scrollView, pointer.x, pointer.y); + if (point) this.updateSelectionFocus(point); + this.requestRender(); + } + + private stopSelectionAutoScroll(): void { + if (this.selectionAutoScrollTimer) { + clearInterval(this.selectionAutoScrollTimer); + this.selectionAutoScrollTimer = undefined; + } + this.selectionAutoScrollDirection = 0; + this.selectionDragPointer = undefined; + } + + private handleSelectionMouseEvent(event: SgrMouseEvent): void { + const button = event.button & 3; + if (button !== 0 && !(event.release && button === 3)) return; + const anchorScrollView = this.selectionAnchor?.scrollView; + const point = this.getSelectionPoint(event, anchorScrollView); + if (event.release) { + if (!this.selectionPressActive) return; + this.selectionPressActive = false; + this.stopSelectionAutoScroll(); + if (!this.selectionAnchor) return; + this.updateSelectionFocus(point); + const isClick = + !this.selectionDragged && + this.selectionAnchor.scrollView === point.scrollView && + this.selectionAnchor.row === point.row && + this.selectionAnchor.col === point.col; + const clickedUrl = isClick ? this.pressedUrl : undefined; + this.pressedUrl = undefined; + if (clickedUrl && this.openUrl) { + this.selectionAnchor = undefined; + this.selectionFocus = undefined; + try { + this.openUrl(clickedUrl); + } catch { + // URL activation is best-effort. + } + this.requestRender(); + return; + } + if (isClick) { + const clickEvent = this.createMouseEvent("click", event.button, event.x, event.y, { + clickCount: this.lastClick?.count ?? 1, + }); + const overlay = this.dispatchMouseToOverlay(clickEvent); + const result = overlay.result ?? (overlay.hit ? undefined : this.dispatchMouseToLayout(clickEvent)); + if (result) { + const render = this.applyMouseDispatchResult(clickEvent, result); + this.clearTextSelection(); + if (render) this.requestRender(); + return; + } + } + if (this.copyOnSelect) void this.copySelectionToClipboard(); + this.requestRender(); + return; + } + if ((event.button & 32) !== 0) { + if (!this.selectionPressActive || !this.selectionAnchor) return; + this.selectionDragged = true; + this.lastClick = undefined; + this.pressedUrl = undefined; + this.updateSelectionFocus(point); + this.updateSelectionAutoScroll(event); + this.requestRender(); + return; + } + this.stopSelectionAutoScroll(); + this.selectionPressActive = true; + const scrollView = + !this.hasOverlay() && this.currentLayout + ? getScrollViewsAt(this.currentLayout, event.x, event.y)[0] + : undefined; + const anchor = this.getSelectionPoint(event, scrollView); + const word = this.getWordSelection(anchor); + const clickCount = this.getClickCount(anchor, word); + const range = clickCount === 2 ? word : clickCount === 3 ? this.getLineSelection(anchor) : undefined; + this.selectionGranularity = range ? (clickCount === 2 ? "word" : "line") : "character"; + this.selectionInitialRange = range; + this.selectionAnchor = range?.start ?? anchor; + this.selectionFocus = range?.end ?? anchor; + this.selectionDragged = false; + this.pressedUrl = range + ? undefined + : getOsc8LinkAtColumn( + this.previousScreen[Math.max(0, Math.min(this.terminal.rows - 1, event.y))] ?? "", + Math.max(0, Math.min(this.terminal.columns - 1, event.x)), + ); + this.requestRender(); + } + + private getSelectionBounds(): { start: SelectionPoint; end: SelectionPoint } | undefined { + if (!this.selectionAnchor || !this.selectionFocus) return undefined; + if (this.selectionAnchor.scrollView !== this.selectionFocus.scrollView) return undefined; + const anchorBeforeFocus = + this.selectionAnchor.row < this.selectionFocus.row || + (this.selectionAnchor.row === this.selectionFocus.row && this.selectionAnchor.col < this.selectionFocus.col); + if ( + this.selectionAnchor.row === this.selectionFocus.row && + this.selectionAnchor.col === this.selectionFocus.col + ) { + return undefined; + } + return anchorBeforeFocus + ? { start: this.selectionAnchor, end: this.selectionFocus } + : { start: this.selectionFocus, end: this.selectionAnchor }; + } + + private getSelectionColumns( + line: string, + row: number, + selection: { start: SelectionPoint; end: SelectionPoint }, + minColumn = 0, + maxColumn = visibleWidth(line), + ): { start: number; end: number } { + const lineWidth = visibleWidth(line); + let start = Math.max(0, minColumn); + let end = Math.min(lineWidth, maxColumn); + if (row === selection.start.row) { + start = getGraphemeCellRange(line, selection.start.col)?.start ?? Math.min(selection.start.col, lineWidth); + } + if (row === selection.end.row) { + end = selection.end.boundary + ? Math.min(selection.end.col, lineWidth) + : (getGraphemeCellRange(line, selection.end.col)?.end ?? Math.min(selection.end.col + 1, lineWidth)); + } + return { start: Math.max(minColumn, start), end: Math.min(maxColumn, end) }; + } + + private getActiveSelectionText(): string | undefined { + const selection = this.getSelectionBounds(); + if (!selection) return undefined; + let sourceLines: readonly string[] = this.previousScreen; + if (selection.start.scrollView) { + if (!this.currentLayout) return undefined; + const box = getScrollViewBox(this.currentLayout, selection.start.scrollView); + if (!box?.scrollContentLines) return undefined; + sourceLines = box.scrollContentLines; + } + const lines: string[] = []; + for (let row = selection.start.row; row <= selection.end.row; row++) { + const line = sourceLines[row] ?? ""; + const columns = this.getSelectionColumns(line, row, selection); + lines.push( + stripTerminalSequences( + sliceByColumn(line, columns.start, Math.max(0, columns.end - columns.start), true), + ).trimEnd(), + ); + } + const text = lines.join("\n"); + return text.length === 0 ? undefined : text; + } + + private async copySelectionToClipboard(): Promise { + const text = this.getActiveSelectionText(); + if (!text) return false; + return this.copyTextToClipboard(text); + } + + private async copyTextToClipboard(text: string): Promise { + // Prefer an injected clipboard implementation (native clipboard + platform tools with a + // verified success path) when the host app provides one. A bare OSC 52 write can show + // "Copied!" while leaving the system clipboard untouched (e.g. macOS Terminal.app, tmux + // without OSC 52 clipboard passthrough), so only report success when it actually copies. + if (this.copySelection) { + const ok = await this.copySelection(text); + this.flash(ok ? "Copied!" : "Copy failed"); + return ok; + } + this.terminal.write(`\x1b]52;c;${Buffer.from(text).toString("base64")}\x07`); + this.flash("Copied!"); + return true; + } + + private applySearchTextHighlight(text: string, current: boolean): string { + const style = current ? this.searchCurrentMatchStyle : this.searchMatchStyle; + let result = ""; + let plainStart = 0; + let index = 0; + while (index < text.length) { + const ansi = extractAnsiCode(text, index); + if (!ansi) { + index += 1; + continue; + } + if (index > plainStart) result += style(text.slice(plainStart, index)); + result += ansi.code; + index += ansi.length; + plainStart = index; + } + if (plainStart < text.length) result += style(text.slice(plainStart)); + return result; + } + + private applySearchHighlights(screen: string[], layout: LayoutFrame): string[] { + const search = this.activeSearch; + if (!search || search.selectedIndex < 0 || search.matches.length === 0) return screen; + const scrollView = layout.primaryScrollView ?? this.implicitScrollView; + const box = getScrollViewBox(layout, scrollView); + if (!box) return screen; + + const rangesByRow = new Map(); + const scrollbarColumn = getScrollbarGeometry(box)?.column; + const minRow = Math.max(0, box.rect.y, box.clip.y); + const maxRow = Math.min(screen.length, box.rect.y + box.rect.height, box.clip.y + box.clip.height); + const minColumn = Math.max(0, box.rect.x, box.clip.x); + const maxColumn = Math.min( + this.terminal.columns, + box.rect.x + box.rect.width, + box.clip.x + box.clip.width, + scrollbarColumn ?? Number.POSITIVE_INFINITY, + ); + const minContentRow = scrollView.scrollTop + minRow - box.rect.y; + const maxContentRow = scrollView.scrollTop + maxRow - box.rect.y - 1; + let low = 0; + let high = search.matches.length; + while (low < high) { + const middle = low + Math.floor((high - low) / 2); + const match = search.matches[middle]!; + const lastRow = match.segments[match.segments.length - 1]?.row ?? -1; + if (lastRow < minContentRow) low = middle + 1; + else high = middle; + } + for (let matchIndex = low; matchIndex < search.matches.length; matchIndex++) { + const match = search.matches[matchIndex]!; + if ((match.segments[0]?.row ?? 0) > maxContentRow) break; + for (const segment of match.segments) { + const row = box.rect.y + segment.row - scrollView.scrollTop; + if (row < minRow || row >= maxRow) continue; + const startCol = Math.max(minColumn, box.rect.x + segment.startCol); + const endCol = Math.min(maxColumn, box.rect.x + segment.endCol); + if (endCol <= startCol) continue; + const ranges = rangesByRow.get(row) ?? []; + ranges.push({ startCol, endCol, current: matchIndex === search.selectedIndex }); + rangesByRow.set(row, ranges); + } + } + + const result = [...screen]; + for (const [row, ranges] of rangesByRow) { + let line = result[row] ?? ""; + if (isImageLine(line)) continue; + const lineWidth = visibleWidth(line); + for (const range of ranges.sort((a, b) => b.startCol - a.startCol)) { + const startCol = Math.min(range.startCol, lineWidth); + const endCol = Math.min(range.endCol, lineWidth); + if (endCol <= startCol) continue; + const before = sliceByColumn(line, 0, startCol, true); + const highlighted = sliceByColumn(line, startCol, endCol - startCol, true); + const after = sliceByColumn(line, endCol, Math.max(0, lineWidth - endCol), true); + line = `${before}${this.applySearchTextHighlight(highlighted, range.current)}${after}`; + } + result[row] = line; + } + return result; + } + + private applySelectionHighlight(text: string): string { + let result = "\x1b[7m"; + let index = 0; + while (index < text.length) { + const ansi = extractAnsiCode(text, index); + if (!ansi) { + result += text[index]; + index += 1; + continue; + } + result += ansi.code; + if (ansi.code.endsWith("m")) result += "\x1b[7m"; + index += ansi.length; + } + return `${result}\x1b[27m`; + } + + private applySelection(screen: string[], layout = this.currentLayout): string[] { + const selection = this.getSelectionBounds(); + if (!selection) return screen; + let screenSelection = selection; + let minRow = 0; + let maxRow = screen.length - 1; + let minColumn = 0; + let maxColumn = this.terminal.columns; + if (selection.start.scrollView) { + if (!layout) return screen; + const box = getScrollViewBox(layout, selection.start.scrollView); + if (!box) return screen; + minRow = Math.max(0, box.rect.y, box.clip.y); + maxRow = Math.min(screen.length - 1, box.rect.y + box.rect.height - 1, box.clip.y + box.clip.height - 1); + minColumn = Math.max(0, box.rect.x, box.clip.x); + maxColumn = Math.min(this.terminal.columns, box.rect.x + box.rect.width, box.clip.x + box.clip.width); + screenSelection = { + start: { + ...selection.start, + row: box.rect.y + selection.start.row - selection.start.scrollView.scrollTop, + col: box.rect.x + selection.start.col, + }, + end: { + ...selection.end, + row: box.rect.y + selection.end.row - selection.start.scrollView.scrollTop, + col: box.rect.x + selection.end.col, + }, + }; + } + return screen.map((line, row) => { + if ( + row < minRow || + row > maxRow || + row < screenSelection.start.row || + row > screenSelection.end.row || + isImageLine(line) + ) { + return line; + } + const lineWidth = visibleWidth(line); + const columns = this.getSelectionColumns(line, row, screenSelection, minColumn, maxColumn); + if (columns.end <= columns.start) return line; + const before = sliceByColumn(line, 0, columns.start, true); + const selected = sliceByColumn(line, columns.start, columns.end - columns.start, true); + const after = sliceByColumn(line, columns.end, Math.max(0, lineWidth - columns.end), true); + return `${before}${this.applySelectionHighlight(selected)}${after}`; + }); + } + + private isMouseSequence(data: string): boolean { + return /^\x1b\[<\d+;\d+;\d+[Mm]$/.test(data) || (data.length === 6 && data.startsWith("\x1b[M")); + } + + private compositeScrollToEndIndicator(screen: string[], layout: LayoutFrame, width: number): string[] { + this.scrollToEndIndicatorRect = undefined; + const scrollView = layout.primaryScrollView ?? this.implicitScrollView; + if (!this.scrollToEndIndicator || !scrollView.followEnd || scrollView.isFollowingEnd) return screen; + const box = getScrollViewBox(layout, scrollView); + const clip = box?.clip; + if (!clip || clip.width <= 0 || clip.height <= 0) return screen; + const row = clip.y + clip.height - 1; + if (row >= screen.length || isImageLine(screen[row] ?? "")) return screen; + const scrollbarColumn = box ? getScrollbarGeometry(box)?.column : undefined; + const availableWidth = Math.max(0, (scrollbarColumn ?? clip.x + clip.width) - clip.x); + const text = truncateToWidth(this.scrollToEndIndicator(), availableWidth, ""); + const textWidth = visibleWidth(text); + if (textWidth === 0) return screen; + const column = clip.x + Math.floor((availableWidth - textWidth) / 2); + const result = [...screen]; + result[row] = compositeTuiLine(result[row] ?? "", text, column, textWidth, width); + this.scrollToEndIndicatorRect = { row, column, width: textWidth }; + return result; + } + + private compositeFlashes(screen: string[], width: number, height: number): string[] { + const flashLines = this.flashes.render(width).slice(-height); + if (flashLines.length === 0) return screen; + const result = [...screen]; + while (result.length < height) result.push(""); + for (let row = 0; row < flashLines.length; row++) { + const line = flashLines[row]!; + const flashWidth = visibleWidth(line); + if (flashWidth === 0) continue; + result[row] = compositeTuiLine(result[row] ?? "", line, width - flashWidth, flashWidth, width); + } + return result; + } + + protected override doRender(): void { + if (this.stopped || !this.altScreenActive) return; + const width = Math.max(1, this.terminal.columns); + const height = Math.max(1, this.terminal.rows); + const root = this.layoutRoot ?? this.implicitScrollView; + let nextLayout = renderLayoutFrame(root, width, height, () => this.requestRender()); + if (this.refreshSearch(nextLayout)) { + nextLayout = renderLayoutFrame(root, width, height, () => this.requestRender()); + } + let screen = nextLayout.lines.map((line) => line.replace(OSC133_ZONE_PREFIX, "")); + screen = this.applySearchHighlights(screen, nextLayout); + screen = this.compositeScrollToEndIndicator(screen, nextLayout, width); + screen = this.compositeOverlays(screen, width, height); + if (screen.length > height) screen = screen.slice(screen.length - height); + screen = this.applySelection(screen, nextLayout); + screen = this.compositeFlashes(screen, width, height); + + const cursorPos = this.extractCursorPosition(screen, height); + screen = this.applyLineResets(screen).map((line) => { + if (isImageLine(line) || visibleWidth(line) <= width) return line; + return sliceByColumn(line, 0, width, true); + }); + + const fullRedraw = + this.previousScreen.length === 0 || this.previousScreenWidth !== width || this.previousScreenHeight !== height; + const imagesNeedRedraw = screen.some( + (line, row) => + line !== this.previousScreen[row] && (isImageLine(line) || isImageLine(this.previousScreen[row] ?? "")), + ); + const redrawImages = fullRedraw || imagesNeedRedraw; + const hadUploadedKittyImages = this.uploadedKittyImages.size > 0; + const preparedKittyScreen = + redrawImages && this.imageProtocol === "kitty" + ? this.prepareKittyScreen(screen) + : { lines: screen, evictedImageDeletion: "" }; + + let buffer = BEGIN_SYNCHRONIZED_OUTPUT; + if (fullRedraw) { + this.fullRedrawCount += 1; + const clearImages = + this.imageProtocol === "kitty" && hadUploadedKittyImages + ? deleteAllKittyPlacements() + : this.deleteKittyImages(); + buffer += `${clearImages}\x1b[2J`; + } else if (imagesNeedRedraw) { + if (this.imageProtocol === "iterm2") buffer += "\x1b[2J"; + else if (this.imageProtocol === "kitty") buffer += deleteAllKittyPlacements(); + } + buffer += preparedKittyScreen.evictedImageDeletion; + + for (let row = 0; row < height; row++) { + if (!fullRedraw && !imagesNeedRedraw && screen[row] === this.previousScreen[row]) continue; + buffer += `\x1b[${row + 1};1H\x1b[2K${preparedKittyScreen.lines[row] ?? ""}`; + } + + if (cursorPos) { + buffer += `\x1b[${cursorPos.row + 1};${Math.min(width, cursorPos.col) + 1}H`; + buffer += this.getShowHardwareCursor() ? "\x1b[?25h" : "\x1b[?25l"; + } else { + buffer += "\x1b[?25l"; + } + buffer += END_SYNCHRONIZED_OUTPUT; + this.terminal.write(buffer); + + this.previousScreen = screen; + this.previousScreenWidth = width; + this.previousScreenHeight = height; + this.currentLayout = nextLayout; + } +} diff --git a/packages/tui/src/tui-main-screen.ts b/packages/tui/src/tui-main-screen.ts new file mode 100644 index 0000000000000000000000000000000000000000..c079dd776984b23f5e0b4a833ac5b67fb31ff337 --- /dev/null +++ b/packages/tui/src/tui-main-screen.ts @@ -0,0 +1,655 @@ +import * as fs from "node:fs"; +import * as os from "node:os"; +import * as path from "node:path"; +import { deleteKittyImage, isImageLine } from "./terminal-image.ts"; +import { type TUI, TuiBase, type TuiStopOptions } from "./tui.ts"; +import { visibleWidth } from "./utils.ts"; + +const KITTY_SEQUENCE_PREFIX = "\x1b_G"; +const MAX_RENDER_WRITE_CHARS = 1024 * 1024; + +/** + * Streams terminal output in 1 MiB chunks so a full render never forms one string large enough to exceed V8's limit. + * + * `append()` fills the current chunk and flushes it when full. Oversized input is split at chunk boundaries, preserving + * surrogate pairs so each write remains valid UTF-16. Callers append synchronized-output begin/end sequences themselves; + * the final `flush()` writes any remainder, including the end sequence. + */ +class BoundedTerminalWriter { + private buffer = ""; + private writtenChars = 0; + private readonly write: (data: string) => void; + + constructor(write: (data: string) => void) { + this.write = write; + } + + /** + * Append terminal data, flushing full chunks as needed. Callers must call `flush()` after the final append. + * @param value Terminal data to write in order; oversized values are split without splitting surrogate pairs. + */ + append(value: string): void { + let offset = 0; + while (offset < value.length) { + const capacity = MAX_RENDER_WRITE_CHARS - this.buffer.length; + if (capacity === 0) { + this.flush(); + continue; + } + + let end = Math.min(value.length, offset + capacity); + if ( + end < value.length && + value.charCodeAt(end - 1) >= 0xd800 && + value.charCodeAt(end - 1) <= 0xdbff && + value.charCodeAt(end) >= 0xdc00 && + value.charCodeAt(end) <= 0xdfff + ) { + end--; + } + if (end === offset) { + this.flush(); + continue; + } + + this.buffer += value.slice(offset, end); + offset = end; + if (this.buffer.length === MAX_RENDER_WRITE_CHARS) { + this.flush(); + } + } + } + + /** Write the current chunk, if any, and retain only its character count for debug output. */ + flush(): void { + if (!this.buffer) return; + this.write(this.buffer); + this.writtenChars += this.buffer.length; + this.buffer = ""; + } + + get length(): number { + return this.writtenChars + this.buffer.length; + } +} + +interface KittyImageHeader { + ids: number[]; + rows: number; +} + +function parseKittyImageHeader(line: string): KittyImageHeader | undefined { + const sequenceStart = line.indexOf(KITTY_SEQUENCE_PREFIX); + if (sequenceStart === -1) return undefined; + const paramsStart = sequenceStart + KITTY_SEQUENCE_PREFIX.length; + const paramsEnd = line.indexOf(";", paramsStart); + if (paramsEnd === -1) return undefined; + + const ids: number[] = []; + let rows = 1; + for (const param of line.slice(paramsStart, paramsEnd).split(",")) { + const [key, value] = param.split("=", 2); + if (value === undefined) continue; + const numberValue = Number(value); + if (!Number.isInteger(numberValue) || numberValue <= 0 || numberValue > 0xffffffff) continue; + if (key === "i") ids.push(numberValue); + else if (key === "r") rows = numberValue; + } + return { ids, rows }; +} + +function extractKittyImageIds(line: string): number[] { + return parseKittyImageHeader(line)?.ids ?? []; +} + +function extractKittyImageRows(line: string): number { + return parseKittyImageHeader(line)?.rows ?? 1; +} + +function isTermuxSession(): boolean { + return Boolean(process.env.TERMUX_VERSION); +} + +export interface TuiMainScreenRenderState { + previousLines: string[]; + previousWidth: number; + previousHeight: number; + cursorRow: number; + hardwareCursorRow: number; + maxLinesRendered: number; + previousViewportTop: number; +} + +/** TUI implementation that renders into the terminal's main screen and scrollback. */ +export class TuiMainScreen extends TuiBase implements TUI { + readonly mode = "regular" as const; + private previousLines: string[] = []; + private previousKittyImageIds = new Set(); + private previousWidth = 0; + private previousHeight = 0; + private cursorRow = 0; + private hardwareCursorRow = 0; + private maxLinesRendered = 0; + private previousViewportTop = 0; + + captureRenderState(): TuiMainScreenRenderState { + return { + previousLines: [...this.previousLines], + previousWidth: this.previousWidth, + previousHeight: this.previousHeight, + cursorRow: this.cursorRow, + hardwareCursorRow: this.hardwareCursorRow, + maxLinesRendered: this.maxLinesRendered, + previousViewportTop: this.previousViewportTop, + }; + } + + restoreRenderState(state: TuiMainScreenRenderState): void { + this.previousLines = state.previousLines.map((line) => (isImageLine(line) ? "" : line)); + this.previousKittyImageIds = new Set(); + this.previousWidth = state.previousWidth; + this.previousHeight = state.previousHeight; + this.cursorRow = state.cursorRow; + this.hardwareCursorRow = state.hardwareCursorRow; + this.maxLinesRendered = state.maxLinesRendered; + this.previousViewportTop = state.previousViewportTop; + } + + protected override resetRenderState(): void { + this.previousLines = []; + this.previousWidth = -1; + this.previousHeight = -1; + this.cursorRow = 0; + this.hardwareCursorRow = 0; + this.maxLinesRendered = 0; + this.previousViewportTop = 0; + } + + protected override beforeTerminalStop(options: TuiStopOptions): void { + if (options.preserveScreen || this.previousLines.length === 0) return; + this.terminal.write(" "); + const targetRow = this.previousLines.length; + const lineDiff = targetRow - this.hardwareCursorRow; + if (lineDiff > 0) this.terminal.write(`\x1b[${lineDiff}B`); + else if (lineDiff < 0) this.terminal.write(`\x1b[${-lineDiff}A`); + this.terminal.write("\r\n"); + } + + private collectKittyImageIds(lines: string[]): Set { + const ids = new Set(); + for (const line of lines) { + for (const id of extractKittyImageIds(line)) { + ids.add(id); + } + } + return ids; + } + + private deleteKittyImages(ids: Iterable): string { + let buffer = ""; + for (const id of ids) { + buffer += deleteKittyImage(id); + } + return buffer; + } + + private getKittyImageReservedRows(lines: string[], index: number, maxIndex = lines.length - 1): number { + const rows = extractKittyImageRows(lines[index] ?? ""); + if (rows <= 1) return 1; + + const maxRows = Math.min(rows, maxIndex - index + 1, lines.length - index); + let reservedRows = 1; + while (reservedRows < maxRows) { + const line = lines[index + reservedRows] ?? ""; + if (isImageLine(line) || visibleWidth(line) > 0) break; + reservedRows++; + } + return reservedRows; + } + + private expandChangedRangeForKittyImages( + firstChanged: number, + lastChanged: number, + newLines: string[], + ): { firstChanged: number; lastChanged: number } { + let expandedFirstChanged = firstChanged; + let expandedLastChanged = lastChanged; + const expandForLines = (lines: string[]): void => { + for (let i = 0; i < lines.length; i++) { + if (extractKittyImageIds(lines[i]).length === 0) continue; + const blockEnd = i + this.getKittyImageReservedRows(lines, i) - 1; + if (i >= firstChanged || (i <= lastChanged && blockEnd >= firstChanged)) { + expandedFirstChanged = Math.min(expandedFirstChanged, i); + expandedLastChanged = Math.max(expandedLastChanged, blockEnd); + } + } + }; + + expandForLines(this.previousLines); + expandForLines(newLines); + return { firstChanged: expandedFirstChanged, lastChanged: expandedLastChanged }; + } + + private deleteChangedKittyImages(firstChanged: number, lastChanged: number): string { + if (firstChanged < 0 || lastChanged < firstChanged) return ""; + + const ids = new Set(); + const maxLine = Math.min(lastChanged, this.previousLines.length - 1); + for (let i = firstChanged; i <= maxLine; i++) { + for (const id of extractKittyImageIds(this.previousLines[i] ?? "")) { + ids.add(id); + } + } + + return this.deleteKittyImages(ids); + } + + protected doRender(): void { + if (this.stopped) return; + const width = this.terminal.columns; + const height = this.terminal.rows; + const widthChanged = this.previousWidth !== 0 && this.previousWidth !== width; + const heightChanged = this.previousHeight !== 0 && this.previousHeight !== height; + const previousBufferLength = this.previousHeight > 0 ? this.previousViewportTop + this.previousHeight : height; + let prevViewportTop = heightChanged ? Math.max(0, previousBufferLength - height) : this.previousViewportTop; + let viewportTop = prevViewportTop; + let hardwareCursorRow = this.hardwareCursorRow; + const computeLineDiff = (targetRow: number): number => { + const currentScreenRow = hardwareCursorRow - prevViewportTop; + const targetScreenRow = targetRow - viewportTop; + return targetScreenRow - currentScreenRow; + }; + + // Render all components to get new lines + let newLines = this.render(width); + + // Composite overlays into the rendered lines (before differential compare) + if (this.hasOverlayEntries) { + newLines = this.compositeOverlays(newLines, width, height); + } + + // Extract cursor position before applying line resets (marker must be found first) + const cursorPos = this.extractCursorPosition(newLines, height); + + newLines = this.applyLineResets(newLines); + + // Helper to clear scrollback and viewport and render all new lines + const fullRender = (clear: boolean): void => { + this.fullRedrawCount += 1; + const output = new BoundedTerminalWriter((data) => this.terminal.write(data)); + output.append("\x1b[?2026h"); // Begin synchronized output + if (clear) { + output.append(this.deleteKittyImages(this.previousKittyImageIds)); + output.append("\x1b[2J\x1b[H\x1b[3J"); // Clear screen, home, then clear scrollback + } + for (let i = 0; i < newLines.length; i++) { + if (i > 0) output.append("\r\n"); + const line = newLines[i]; + const isImage = isImageLine(line); + const imageReservedRows = isImage ? this.getKittyImageReservedRows(newLines, i) : 1; + if (imageReservedRows > 1 && imageReservedRows <= height) { + for (let row = 1; row < imageReservedRows; row++) { + output.append("\r\n"); + } + output.append(`\x1b[${imageReservedRows - 1}A`); + output.append(line); + output.append(`\x1b[${imageReservedRows - 1}B`); + i += imageReservedRows - 1; + continue; + } + output.append(line); + } + output.append("\x1b[?2026l"); // End synchronized output + output.flush(); + this.cursorRow = Math.max(0, newLines.length - 1); + this.hardwareCursorRow = this.cursorRow; + // Reset max lines when clearing, otherwise track growth + if (clear) { + this.maxLinesRendered = newLines.length; + } else { + this.maxLinesRendered = Math.max(this.maxLinesRendered, newLines.length); + } + const bufferLength = Math.max(height, newLines.length); + this.previousViewportTop = Math.max(0, bufferLength - height); + this.positionHardwareCursor(cursorPos, newLines.length); + this.previousLines = newLines; + this.previousKittyImageIds = this.collectKittyImageIds(newLines); + this.previousWidth = width; + this.previousHeight = height; + }; + + const redrawLogDirectory = process.env.PI_TUI_DEBUG_REDRAW === "1" ? this.logDirectory : undefined; + const logRedraw = (reason: string): void => { + if (redrawLogDirectory === undefined) return; + const logPath = path.join(redrawLogDirectory, "pi-tui-debug.log"); + const msg = `[${new Date().toISOString()}] fullRender: ${reason} (prev=${this.previousLines.length}, new=${newLines.length}, height=${height})\n`; + fs.mkdirSync(path.dirname(logPath), { recursive: true }); + fs.appendFileSync(logPath, msg); + }; + + // First render - just output everything without clearing (assumes clean screen) + if (this.previousLines.length === 0 && !widthChanged && !heightChanged) { + logRedraw("first render"); + fullRender(false); + return; + } + + // Width changes always need a full re-render because wrapping changes. + if (widthChanged) { + logRedraw(`terminal width changed (${this.previousWidth} -> ${width})`); + fullRender(true); + return; + } + + // Height changes normally need a full re-render to keep the visible viewport aligned, + // but Termux changes height when the software keyboard shows or hides. + // In that environment, a full redraw causes the entire history to replay on every toggle. + if (heightChanged && !isTermuxSession()) { + logRedraw(`terminal height changed (${this.previousHeight} -> ${height})`); + fullRender(true); + return; + } + + // Content shrunk below the working area and no overlays - re-render to clear empty rows + // (overlays need the padding, so only do this when no overlays are active) + // Configurable via setClearOnShrink() + if (this.getClearOnShrink() && newLines.length < this.maxLinesRendered && !this.hasOverlayEntries) { + logRedraw(`clearOnShrink (maxLinesRendered=${this.maxLinesRendered})`); + fullRender(true); + return; + } + + // Find first and last changed lines + let firstChanged = -1; + let lastChanged = -1; + const maxLines = Math.max(newLines.length, this.previousLines.length); + for (let i = 0; i < maxLines; i++) { + const oldLine = i < this.previousLines.length ? this.previousLines[i] : ""; + const newLine = i < newLines.length ? newLines[i] : ""; + + if (oldLine !== newLine) { + if (firstChanged === -1) { + firstChanged = i; + } + lastChanged = i; + } + } + const appendedLines = newLines.length > this.previousLines.length; + if (appendedLines) { + if (firstChanged === -1) { + firstChanged = this.previousLines.length; + } + lastChanged = newLines.length - 1; + } + if (firstChanged !== -1) { + const expandedRange = this.expandChangedRangeForKittyImages(firstChanged, lastChanged, newLines); + firstChanged = expandedRange.firstChanged; + lastChanged = expandedRange.lastChanged; + } + const appendStart = appendedLines && firstChanged === this.previousLines.length && firstChanged > 0; + + // No changes - but still need to update hardware cursor position if it moved + if (firstChanged === -1) { + this.positionHardwareCursor(cursorPos, newLines.length); + this.previousViewportTop = prevViewportTop; + this.previousHeight = height; + return; + } + + // All changes are in deleted lines (nothing to render, just clear) + if (firstChanged >= newLines.length) { + if (this.previousLines.length > newLines.length) { + const output = new BoundedTerminalWriter((data) => this.terminal.write(data)); + output.append("\x1b[?2026h"); + output.append(this.deleteChangedKittyImages(firstChanged, lastChanged)); + // Move to end of new content (clamp to 0 for empty content) + const targetRow = Math.max(0, newLines.length - 1); + if (targetRow < prevViewportTop) { + logRedraw(`deleted lines moved viewport up (${targetRow} < ${prevViewportTop})`); + fullRender(true); + return; + } + const lineDiff = computeLineDiff(targetRow); + if (lineDiff > 0) output.append(`\x1b[${lineDiff}B`); + else if (lineDiff < 0) output.append(`\x1b[${-lineDiff}A`); + output.append("\r"); + // Clear extra lines without scrolling + const extraLines = this.previousLines.length - newLines.length; + if (extraLines > height) { + logRedraw(`extraLines > height (${extraLines} > ${height})`); + fullRender(true); + return; + } + const clearStartOffset = newLines.length === 0 ? 0 : 1; + if (extraLines > 0 && clearStartOffset > 0) { + output.append(`\x1b[${clearStartOffset}B`); + } + for (let i = 0; i < extraLines; i++) { + output.append("\r\x1b[2K"); + if (i < extraLines - 1) output.append("\x1b[1B"); + } + const moveBack = Math.max(0, extraLines - 1 + clearStartOffset); + if (moveBack > 0) { + output.append(`\x1b[${moveBack}A`); + } + output.append("\x1b[?2026l"); + output.flush(); + this.cursorRow = targetRow; + this.hardwareCursorRow = targetRow; + } + this.positionHardwareCursor(cursorPos, newLines.length); + this.previousLines = newLines; + this.previousKittyImageIds = this.collectKittyImageIds(newLines); + this.previousWidth = width; + this.previousHeight = height; + this.previousViewportTop = prevViewportTop; + return; + } + + // Differential rendering can only touch what was actually visible. + // If the first changed line is above the previous viewport, we need a full redraw. + if (firstChanged < prevViewportTop) { + logRedraw(`firstChanged < viewportTop (${firstChanged} < ${prevViewportTop})`); + fullRender(true); + return; + } + + // Render from first changed line to end + // Keep updates wrapped in synchronized output while writing bounded chunks. + const output = new BoundedTerminalWriter((data) => this.terminal.write(data)); + output.append("\x1b[?2026h"); // Begin synchronized output + output.append(this.deleteChangedKittyImages(firstChanged, lastChanged)); + const prevViewportBottom = prevViewportTop + height - 1; + const moveTargetRow = appendStart ? firstChanged - 1 : firstChanged; + if (moveTargetRow > prevViewportBottom) { + const currentScreenRow = Math.max(0, Math.min(height - 1, hardwareCursorRow - prevViewportTop)); + const moveToBottom = height - 1 - currentScreenRow; + if (moveToBottom > 0) { + output.append(`\x1b[${moveToBottom}B`); + } + const scroll = moveTargetRow - prevViewportBottom; + output.append("\r\n".repeat(scroll)); + prevViewportTop += scroll; + viewportTop += scroll; + hardwareCursorRow = moveTargetRow; + } + + // Move cursor to first changed line (use hardwareCursorRow for actual position) + const lineDiff = computeLineDiff(moveTargetRow); + if (lineDiff > 0) { + output.append(`\x1b[${lineDiff}B`); // Move down + } else if (lineDiff < 0) { + output.append(`\x1b[${-lineDiff}A`); // Move up + } + + output.append(appendStart ? "\r\n" : "\r"); // Move to column 0 + + // Only render changed lines (firstChanged to lastChanged), not all lines to end + // This reduces flicker when only a single line changes (e.g., spinner animation) + const renderEnd = Math.min(lastChanged, newLines.length - 1); + for (let i = firstChanged; i <= renderEnd; i++) { + if (i > firstChanged) output.append("\r\n"); + const line = newLines[i]; + const isImage = isImageLine(line); + const imageReservedRows = isImage ? this.getKittyImageReservedRows(newLines, i, renderEnd) : 1; + if (imageReservedRows > 1) { + const imageStartScreenRow = i - viewportTop; + if (imageStartScreenRow < 0 || imageStartScreenRow + imageReservedRows > height) { + logRedraw( + `kitty image pre-clear would scroll (${imageStartScreenRow} + ${imageReservedRows} > ${height})`, + ); + fullRender(true); + return; + } + + output.append("\x1b[2K"); + for (let row = 1; row < imageReservedRows; row++) { + output.append("\r\n\x1b[2K"); + } + output.append(`\x1b[${imageReservedRows - 1}A`); + output.append(line); + output.append(`\x1b[${imageReservedRows - 1}B`); + i += imageReservedRows - 1; + continue; + } + + output.append("\x1b[2K"); // Clear current line + if (!isImage && visibleWidth(line) > width) { + // Log all lines to crash file for debugging + const crashLogPath = path.join(this.logDirectory ?? os.tmpdir(), "pi-tui-crash.log"); + const crashData = [ + `Crash at ${new Date().toISOString()}`, + `Terminal width: ${width}`, + `Line ${i} visible width: ${visibleWidth(line)}`, + "", + "=== All rendered lines ===", + ...newLines.map((l, idx) => `[${idx}] (w=${visibleWidth(l)}) ${l}`), + "", + ].join("\n"); + fs.mkdirSync(path.dirname(crashLogPath), { recursive: true }); + fs.writeFileSync(crashLogPath, crashData); + + // Clean up terminal state before throwing + this.stop(); + + const errorMsg = [ + `Rendered line ${i} exceeds terminal width (${visibleWidth(line)} > ${width}).`, + "", + "This is likely caused by a custom TUI component not truncating its output.", + "Use visibleWidth() to measure and truncateToWidth() to truncate lines.", + "", + `Debug log written to: ${crashLogPath}`, + ].join("\n"); + throw new Error(errorMsg); + } + output.append(line); + } + + // Track where cursor ended up after rendering + let finalCursorRow = renderEnd; + + // If we had more lines before, clear them and move cursor back + if (this.previousLines.length > newLines.length) { + // Move to end of new content first if we stopped before it + if (renderEnd < newLines.length - 1) { + const moveDown = newLines.length - 1 - renderEnd; + output.append(`\x1b[${moveDown}B`); + finalCursorRow = newLines.length - 1; + } + const extraLines = this.previousLines.length - newLines.length; + for (let i = newLines.length; i < this.previousLines.length; i++) { + output.append("\r\n\x1b[2K"); + } + // Move cursor back to end of new content + output.append(`\x1b[${extraLines}A`); + } + + output.append("\x1b[?2026l"); // End synchronized output + + if (process.env.PI_TUI_DEBUG === "1") { + const debugDir = "/tmp/tui"; + fs.mkdirSync(debugDir, { recursive: true }); + const debugPath = path.join(debugDir, `render-${Date.now()}-${Math.random().toString(36).slice(2)}.log`); + const debugData = [ + `firstChanged: ${firstChanged}`, + `viewportTop: ${viewportTop}`, + `cursorRow: ${this.cursorRow}`, + `height: ${height}`, + `lineDiff: ${lineDiff}`, + `hardwareCursorRow: ${hardwareCursorRow}`, + `renderEnd: ${renderEnd}`, + `finalCursorRow: ${finalCursorRow}`, + `cursorPos: ${JSON.stringify(cursorPos)}`, + `newLines.length: ${newLines.length}`, + `previousLines.length: ${this.previousLines.length}`, + "", + "=== newLines ===", + JSON.stringify(newLines, null, 2), + "", + "=== previousLines ===", + JSON.stringify(this.previousLines, null, 2), + "", + "=== buffer ===", + `[${output.length} chars written in bounded chunks]`, + ].join("\n"); + fs.writeFileSync(debugPath, debugData); + } + + output.flush(); + + // Track cursor position for next render + // cursorRow tracks end of content (for viewport calculation) + // hardwareCursorRow tracks actual terminal cursor position (for movement) + this.cursorRow = Math.max(0, newLines.length - 1); + this.hardwareCursorRow = finalCursorRow; + // Track terminal's working area (grows but doesn't shrink unless cleared) + this.maxLinesRendered = Math.max(this.maxLinesRendered, newLines.length); + this.previousViewportTop = Math.max(prevViewportTop, finalCursorRow - height + 1); + + // Position hardware cursor for IME + this.positionHardwareCursor(cursorPos, newLines.length); + + this.previousLines = newLines; + this.previousKittyImageIds = this.collectKittyImageIds(newLines); + this.previousWidth = width; + this.previousHeight = height; + } + + /** + * Position the hardware cursor for IME candidate window. + * @param cursorPos The cursor position extracted from rendered output, or null + * @param totalLines Total number of rendered lines + */ + private positionHardwareCursor(cursorPos: { row: number; col: number } | null, totalLines: number): void { + if (!cursorPos || totalLines <= 0) { + this.terminal.hideCursor(); + return; + } + + // Clamp cursor position to valid range + const targetRow = Math.max(0, Math.min(cursorPos.row, totalLines - 1)); + const targetCol = Math.max(0, cursorPos.col); + + // Move cursor from current position to target + const rowDelta = targetRow - this.hardwareCursorRow; + let buffer = ""; + if (rowDelta > 0) { + buffer += `\x1b[${rowDelta}B`; // Move down + } else if (rowDelta < 0) { + buffer += `\x1b[${-rowDelta}A`; // Move up + } + // Move to absolute column (1-indexed) + buffer += `\x1b[${targetCol + 1}G`; + + if (buffer) { + this.terminal.write(buffer); + } + + this.hardwareCursorRow = targetRow; + if (this.getShowHardwareCursor()) { + this.terminal.showCursor(); + } else { + this.terminal.hideCursor(); + } + } +} diff --git a/packages/tui/src/tui.ts b/packages/tui/src/tui.ts new file mode 100644 index 0000000000000000000000000000000000000000..92e26bb15aa555b5400dc3a51037df076f979827 --- /dev/null +++ b/packages/tui/src/tui.ts @@ -0,0 +1,1456 @@ +/** + * Minimal TUI implementation with differential rendering + */ + +import { performance } from "node:perf_hooks"; +import { isKeyRelease, matchesKey } from "./keys.ts"; +import type { Terminal } from "./terminal.ts"; +import { + isOsc11BackgroundColorResponse, + parseOsc11BackgroundColor, + parseTerminalColorSchemeReport, + type RgbColor, + type TerminalColorScheme, +} from "./terminal-colors.ts"; +import { getCapabilities, isImageLine, setCellDimensions } from "./terminal-image.ts"; +import { extractSegments, normalizeTerminalOutput, sliceByColumn, sliceWithWidth, visibleWidth } from "./utils.ts"; + +/** + * Component interface - all components must implement this + */ +export type TuiMouseEventType = "press" | "release" | "move" | "drag" | "click" | "wheel"; +export type TuiMouseButton = "left" | "middle" | "right" | "none"; + +/** Normalized cell-based mouse event. Coordinates are zero-based. */ +export interface TuiMouseEvent { + type: TuiMouseEventType; + button: TuiMouseButton; + /** Coordinates local to the receiving component. */ + x: number; + y: number; + /** Absolute terminal coordinates. */ + screenX: number; + screenY: number; + /** Current component bounds. */ + width: number; + height: number; + shift: boolean; + alt: boolean; + ctrl: boolean; + /** Logical lines. Negative values scroll up. */ + wheelDelta?: number; + /** Consecutive click count when type is click. */ + clickCount?: number; +} + +export interface TuiMouseEventResult { + /** Stop propagation and suppress renderer-level fallback behavior. */ + handled?: boolean; + /** Route subsequent drag/release events to this component. Implies handled. */ + capture?: boolean; + /** Give keyboard focus to this component. Implies handled. */ + focus?: boolean; + /** + * Explicitly request or suppress a render. Move and release default to false; + * press, click, drag, and wheel default to true. + */ + render?: boolean; +} + +/** Internal target metadata used by containers and alternate-screen dispatch. */ +export interface TuiMouseDispatchTarget { + component: Component; + originX: number; + originY: number; + width: number; + height: number; +} + +/** Result of dispatching to a concrete component. */ +export interface TuiMouseDispatchResult extends TuiMouseEventResult { + handled: true; + target: TuiMouseDispatchTarget; + /** Keyboard focus target, which may be a delegating parent container. */ + focusTarget?: Component; +} + +/** + * Dispatch an event to a component and retain the exact target and coordinate + * transform. Containers use this when forwarding events to nested children. + */ +export function dispatchMouseEvent(component: Component, event: TuiMouseEvent): TuiMouseDispatchResult | undefined { + const result = component.handleMouse?.(event); + if (!result) return undefined; + if ("target" in result) return result as TuiMouseDispatchResult; + if (!result.handled && !result.capture && !result.focus) return undefined; + return { + ...result, + handled: true, + ...(result.focus ? { focusTarget: component } : {}), + target: { + component, + originX: event.screenX - event.x, + originY: event.screenY - event.y, + width: event.width, + height: event.height, + }, + }; +} + +/** Recreate local coordinates for a previously dispatched mouse target. */ +export function retargetMouseEvent(event: TuiMouseEvent, target: TuiMouseDispatchTarget): TuiMouseEvent { + return { + ...event, + x: event.screenX - target.originX, + y: event.screenY - target.originY, + width: target.width, + height: target.height, + }; +} + +export interface Component { + /** + * Render the component to lines for the given viewport width + * @param width - Current viewport width + * @returns Array of strings, each representing a line + */ + render(width: number): string[]; + + /** Optional handler for keyboard input when component has focus. */ + handleInput?(data: string): void; + + /** Optional normalized mouse handler. */ + handleMouse?(event: TuiMouseEvent): TuiMouseEventResult | undefined; + + /** + * If true, component receives key release events (Kitty protocol). + * Default is false - release events are filtered out. + */ + wantsKeyRelease?: boolean; + + /** + * Invalidate any cached rendering state. + * Called when theme changes or when component needs to re-render from scratch. + */ + invalidate(): void; +} + +export type TuiInputListenerResult = { consume?: boolean; data?: string } | undefined; +export type TuiInputListener = (data: string) => TuiInputListenerResult; +type PendingOsc11BackgroundQuery = { + settled: boolean; + resolve: ((rgb: RgbColor | undefined) => void) | undefined; + timer: NodeJS.Timeout | undefined; +}; + +/** + * Interface for components that can receive focus and display a hardware cursor. + * When focused, the component should emit CURSOR_MARKER at the cursor position + * in its render output. TUI will find this marker and position the hardware + * cursor there for proper IME candidate window positioning. + */ +export interface Focusable { + /** Set by TUI when focus changes. Component should emit CURSOR_MARKER when true. */ + focused: boolean; +} + +/** Type guard to check if a component implements Focusable */ +export function isFocusable(component: Component | null): component is Component & Focusable { + return component !== null && "focused" in component; +} + +/** + * Cursor position marker - APC (Application Program Command) sequence. + * This is a zero-width escape sequence that terminals ignore. + * Components emit this at the cursor position when focused. + * TUI finds and strips this marker, then positions the hardware cursor there. + */ +export const CURSOR_MARKER = "\x1b_pi:c\x07"; + +export { visibleWidth }; + +/** + * Anchor position for overlays + */ +export type OverlayAnchor = + | "center" + | "top-left" + | "top-right" + | "bottom-left" + | "bottom-right" + | "top-center" + | "bottom-center" + | "left-center" + | "right-center"; + +/** + * Margin configuration for overlays + */ +export interface OverlayMargin { + top?: number; + right?: number; + bottom?: number; + left?: number; +} + +/** Value that can be absolute (number) or percentage (string like "50%") */ +export type SizeValue = number | `${number}%`; + +/** Parse a SizeValue into absolute value given a reference size */ +function parseSizeValue(value: SizeValue | undefined, referenceSize: number): number | undefined { + if (value === undefined) return undefined; + if (typeof value === "number") return value; + // Parse percentage string like "50%" + const match = value.match(/^(\d+(?:\.\d+)?)%$/); + if (match) { + return Math.floor((referenceSize * parseFloat(match[1])) / 100); + } + return undefined; +} + +/** + * Options for overlay positioning and sizing. + * Values can be absolute numbers or percentage strings (e.g., "50%"). + */ +export interface OverlayOptions { + // === Sizing === + /** Width in columns, or percentage of terminal width (e.g., "50%") */ + width?: SizeValue; + /** Minimum width in columns */ + minWidth?: number; + /** Maximum height in rows, or percentage of terminal height (e.g., "50%") */ + maxHeight?: SizeValue; + + // === Positioning - anchor-based === + /** Anchor point for positioning (default: 'center') */ + anchor?: OverlayAnchor; + /** Horizontal offset from anchor position (positive = right) */ + offsetX?: number; + /** Vertical offset from anchor position (positive = down) */ + offsetY?: number; + + // === Positioning - percentage or absolute === + /** Row position: absolute number, or percentage (e.g., "25%" = 25% from top) */ + row?: SizeValue; + /** Column position: absolute number, or percentage (e.g., "50%" = centered horizontally) */ + col?: SizeValue; + + // === Margin from terminal edges === + /** Margin from terminal edges. Number applies to all sides. */ + margin?: OverlayMargin | number; + + // === Visibility === + /** + * Control overlay visibility based on terminal dimensions. + * If provided, overlay is only rendered when this returns true. + * Called each render cycle with current terminal dimensions. + */ + visible?: (termWidth: number, termHeight: number) => boolean; + /** If true, don't capture keyboard focus when shown */ + nonCapturing?: boolean; +} + +/** Options for {@link OverlayHandle.unfocus}. */ +export interface OverlayUnfocusOptions { + /** Explicit target to focus after releasing this overlay. */ + target: Component | null; +} + +/** Last rendered terminal-relative overlay rectangle. */ +export interface OverlayBounds { + row: number; + col: number; + width: number; + height: number; +} + +/** + * Handle returned by showOverlay for controlling the overlay + */ +export interface OverlayHandle { + /** Permanently remove the overlay (cannot be shown again) */ + hide(): void; + /** Temporarily hide or show the overlay */ + setHidden(hidden: boolean): void; + /** Check if overlay is temporarily hidden */ + isHidden(): boolean; + /** Focus this overlay and bring it to the visual front */ + focus(): void; + /** Release focus to the next visible capturing overlay or previous target, or to an explicit target when provided */ + unfocus(options?: OverlayUnfocusOptions): void; + /** Check if this overlay currently has focus */ + isFocused(): boolean; + /** Get the most recent rendered bounds for a visible overlay. */ + getBounds(): OverlayBounds | undefined; +} + +type OverlayStackEntry = { + component: Component; + options?: OverlayOptions; + preFocus: Component | null; + hidden: boolean; + focusOrder: number; + bounds?: OverlayBounds; +}; + +type RenderedOverlayLayout = { + entry: OverlayStackEntry; + row: number; + col: number; + width: number; + height: number; +}; + +type OverlayBlockedFocusResume = { status: "restore-overlay" } | { status: "focus-target"; target: Component | null }; +type EligibleOverlayFocusRestoreState = { status: "eligible"; overlay: OverlayStackEntry }; +type BlockedOverlayFocusRestoreState = { + status: "blocked"; + overlay: OverlayStackEntry; + blockedBy: Component; + resume: OverlayBlockedFocusResume; +}; +type ActiveOverlayFocusRestoreState = EligibleOverlayFocusRestoreState | BlockedOverlayFocusRestoreState; +type OverlayFocusRestoreState = { status: "inactive" } | ActiveOverlayFocusRestoreState; +type OverlayFocusRestorePolicy = "clear" | "preserve"; + +/** + * Container - a component that contains other components + */ +export class Container implements Component { + children: Component[] = []; + private mouseLayout?: { width: number; children: Array<{ component: Component; height: number }> }; + + addChild(component: Component): void { + this.children.push(component); + } + + removeChild(component: Component): void { + const index = this.children.indexOf(component); + if (index !== -1) { + this.children.splice(index, 1); + } + } + + clear(): void { + this.children = []; + } + + invalidate(): void { + for (const child of this.children) { + child.invalidate?.(); + } + } + + handleMouse(event: TuiMouseEvent): TuiMouseDispatchResult | undefined { + if (event.y < 0 || event.y >= event.height) return undefined; + const mouseChildren = + this.mouseLayout?.width === event.width + ? this.mouseLayout.children + : this.children.map((component) => ({ component, height: component.render(event.width).length })); + let childY = 0; + for (const { component: child, height: childHeight } of mouseChildren) { + if (event.y >= childY && event.y < childY + childHeight) { + const result = dispatchMouseEvent(child, { + ...event, + y: event.y - childY, + height: childHeight, + }); + if (result?.focus && (this as Component).handleInput) return { ...result, focusTarget: this }; + return result; + } + childY += childHeight; + } + return undefined; + } + + render(width: number): string[] { + const lines: string[] = []; + const mouseChildren: Array<{ component: Component; height: number }> = []; + for (const child of this.children) { + const childLines = child.render(width); + mouseChildren.push({ component: child, height: childLines.length }); + for (const line of childLines) { + lines.push(line); + } + } + this.mouseLayout = { width, children: mouseChildren }; + return lines; + } +} + +/** + * TUI - Main class for managing terminal UI with differential rendering + */ +const SEGMENT_RESET = "\x1b[0m\x1b]8;;\x07"; + +/** Composite overlay content into a terminal line at a fixed column. */ +export function compositeTuiLine( + baseLine: string, + overlayLine: string, + startCol: number, + overlayWidth: number, + totalWidth: number, +): string { + if (isImageLine(baseLine)) return baseLine; + + const afterStart = startCol + overlayWidth; + const base = extractSegments(baseLine, startCol, afterStart, totalWidth - afterStart, true); + const overlay = sliceWithWidth(overlayLine, 0, overlayWidth, true); + const beforePad = Math.max(0, startCol - base.beforeWidth); + const overlayPad = Math.max(0, overlayWidth - overlay.width); + const actualBeforeWidth = Math.max(startCol, base.beforeWidth); + const actualOverlayWidth = Math.max(overlayWidth, overlay.width); + const afterTarget = Math.max(0, totalWidth - actualBeforeWidth - actualOverlayWidth); + const afterPad = Math.max(0, afterTarget - base.afterWidth); + const result = + base.before + + " ".repeat(beforePad) + + SEGMENT_RESET + + overlay.text + + " ".repeat(overlayPad) + + SEGMENT_RESET + + base.after + + " ".repeat(afterPad); + + return visibleWidth(result) <= totalWidth ? result : sliceByColumn(result, 0, totalWidth, true); +} + +export type TuiMode = "regular" | "fullscreen"; + +export interface TuiStopOptions { + /** Leave renderer output in place for another TUI taking over the same terminal. */ + preserveScreen?: boolean; +} + +export interface TUI extends Component { + readonly mode: TuiMode; + children: Component[]; + terminal: Terminal; + onDebug?: () => void; + readonly fullRedraws: number; + addChild(component: Component): void; + removeChild(component: Component): void; + clear(): void; + getShowHardwareCursor(): boolean; + setShowHardwareCursor(enabled: boolean): void; + getClearOnShrink(): boolean; + setClearOnShrink(enabled: boolean): void; + setFocus(component: Component | null): void; + showOverlay(component: Component, options?: OverlayOptions): OverlayHandle; + hideOverlay(): void; + hasOverlay(): boolean; + start(): void; + stop(options?: TuiStopOptions): void; + renderNow(force?: boolean): void; + requestRender(force?: boolean): void; + addInputListener(listener: TuiInputListener): () => void; + removeInputListener(listener: TuiInputListener): void; + onTerminalColorSchemeChange(listener: (scheme: TerminalColorScheme) => void): () => void; + setTerminalColorSchemeNotifications(enabled: boolean): void; + queryTerminalBackgroundColor(options: { timeoutMs: number }): Promise; + queryTerminalColorScheme(options: { timeoutMs: number }): Promise; +} + +export const VIEWPORT_TUI = Symbol.for("@earendil-works/pi-tui/viewport"); + +export interface ViewportTUI extends TUI { + readonly [VIEWPORT_TUI]: true; + setLayoutRoot(component: Component | undefined): void; +} + +export function isViewportTUI(tui: TUI): tui is ViewportTUI { + return (tui as Partial)[VIEWPORT_TUI] === true; +} + +export abstract class TuiBase extends Container implements TUI { + abstract readonly mode: TuiMode; + public terminal: Terminal; + private focusedComponent: Component | null = null; + private inputListeners = new Set(); + + /** Global callback for debug key (Shift+Ctrl+D). Called before input is forwarded to focused component. */ + public onDebug?: () => void; + private renderRequested = false; + private immediateRenderScheduled = false; + private renderTimer: NodeJS.Timeout | undefined; + private lastRenderAt = 0; + private static readonly MIN_RENDER_INTERVAL_MS = 16; + private showHardwareCursor = false; + private clearOnShrink = false; + protected fullRedrawCount = 0; + protected stopped = false; + private pendingOsc11BackgroundReplies = 0; + private pendingOsc11BackgroundQueries: PendingOsc11BackgroundQuery[] = []; + private terminalColorSchemeListeners = new Set<(scheme: TerminalColorScheme) => void>(); + private terminalColorSchemeNotificationsEnabled = false; + /** Directory for debug/crash logs. When undefined, debug logging is disabled and crash dumps fall back to the OS temp directory. */ + protected readonly logDirectory: string | undefined; + + // Overlay stack for modal components rendered on top of base content + private focusOrderCounter = 0; + private overlayStack: OverlayStackEntry[] = []; + private renderedOverlayLayouts: RenderedOverlayLayout[] = []; + + get hasOverlayEntries(): boolean { + return this.overlayStack.length > 0; + } + private overlayFocusRestore: OverlayFocusRestoreState = { status: "inactive" }; + + constructor(terminal: Terminal, showHardwareCursor?: boolean, logDirectory?: string) { + super(); + this.terminal = terminal; + this.logDirectory = logDirectory; + if (showHardwareCursor !== undefined) { + this.showHardwareCursor = showHardwareCursor; + } + } + + protected abstract doRender(): void; + + protected resetRenderState(): void {} + + protected beforeTerminalStart(): void {} + + protected afterTerminalStart(): void {} + + protected beforeTerminalStop(_options: TuiStopOptions): void {} + + protected afterTerminalStop(_options: TuiStopOptions): void {} + + get fullRedraws(): number { + return this.fullRedrawCount; + } + + getShowHardwareCursor(): boolean { + return this.showHardwareCursor; + } + + setShowHardwareCursor(enabled: boolean): void { + if (this.showHardwareCursor === enabled) return; + this.showHardwareCursor = enabled; + if (!enabled) { + this.terminal.hideCursor(); + } + this.requestRender(); + } + + getClearOnShrink(): boolean { + return this.clearOnShrink; + } + + /** + * Set whether to trigger full re-render when content shrinks. + * When true, empty rows are cleared when content shrinks. + * When false (default), empty rows remain (reduces redraws on slower terminals). + */ + setClearOnShrink(enabled: boolean): void { + this.clearOnShrink = enabled; + } + + getFocusedComponent(): Component | null { + return this.focusedComponent; + } + + setFocus(component: Component | null): void { + this.setFocusInternal({ component, overlayFocusRestore: "clear" }); + } + + private setFocusInternal({ + component, + overlayFocusRestore, + }: { + component: Component | null; + overlayFocusRestore: OverlayFocusRestorePolicy; + }): void { + const previousFocus = this.focusedComponent; + let nextFocus = component; + const previousFocusedOverlay = previousFocus + ? this.overlayStack.find((entry) => entry.component === previousFocus && this.isOverlayVisible(entry)) + : undefined; + const nextFocusIsOverlay = nextFocus ? this.overlayStack.some((entry) => entry.component === nextFocus) : false; + const restoreState = this.getVisibleOverlayFocusRestore(); + if (nextFocus && !nextFocusIsOverlay) { + if (restoreState.status === "blocked" && restoreState.blockedBy === previousFocus) { + if (restoreState.resume.status === "focus-target" || !this.isComponentMounted(restoreState.blockedBy)) { + nextFocus = this.resolveBlockedOverlayFocusResume(restoreState); + } else { + this.overlayFocusRestore = { + status: "blocked", + overlay: restoreState.overlay, + blockedBy: nextFocus, + resume: restoreState.resume, + }; + } + } else if ( + previousFocusedOverlay && + restoreState.status !== "inactive" && + restoreState.overlay === previousFocusedOverlay && + !this.isOverlayFocusAncestor(previousFocusedOverlay, nextFocus) + ) { + this.overlayFocusRestore = { + status: "blocked", + overlay: previousFocusedOverlay, + blockedBy: nextFocus, + resume: { status: "restore-overlay" }, + }; + } + } else if (nextFocus === null) { + if (restoreState.status === "blocked" && restoreState.blockedBy === previousFocus) { + nextFocus = this.resolveBlockedOverlayFocusResume(restoreState); + } else if (overlayFocusRestore === "clear") { + this.clearOverlayFocusRestore(); + } + } + + if (isFocusable(this.focusedComponent)) { + this.focusedComponent.focused = false; + } + + this.focusedComponent = nextFocus; + + if (isFocusable(nextFocus)) { + nextFocus.focused = true; + } + + const focusedOverlay = nextFocus + ? this.overlayStack.find((entry) => entry.component === nextFocus && this.isOverlayVisible(entry)) + : undefined; + if (focusedOverlay) { + this.overlayFocusRestore = { status: "eligible", overlay: focusedOverlay }; + } + } + + private clearOverlayFocusRestore(): void { + this.overlayFocusRestore = { status: "inactive" }; + } + + private clearOverlayFocusRestoreFor(overlay: OverlayStackEntry): void { + if (this.overlayFocusRestore.status !== "inactive" && this.overlayFocusRestore.overlay === overlay) { + this.clearOverlayFocusRestore(); + } + } + + private resolveBlockedOverlayFocusResume(restoreState: BlockedOverlayFocusRestoreState): Component | null { + if (restoreState.resume.status === "restore-overlay") return restoreState.overlay.component; + this.clearOverlayFocusRestore(); + return restoreState.resume.target; + } + + private getVisibleOverlayFocusRestore(): OverlayFocusRestoreState { + const restoreState = this.overlayFocusRestore; + if (restoreState.status === "inactive") return restoreState; + if (!this.overlayStack.includes(restoreState.overlay) || !this.isOverlayVisible(restoreState.overlay)) { + return { status: "inactive" }; + } + return restoreState; + } + + private isOverlayFocusAncestor(entry: OverlayStackEntry, component: Component): boolean { + const visited = new Set(); + let current = entry.preFocus; + while (current && !visited.has(current)) { + visited.add(current); + if (current === component) return true; + current = this.overlayStack.find((overlay) => overlay.component === current)?.preFocus ?? null; + } + return false; + } + + private retargetOverlayPreFocus(removed: OverlayStackEntry): void { + for (const overlay of this.overlayStack) { + if (overlay !== removed && overlay.preFocus === removed.component) { + overlay.preFocus = removed.preFocus; + } + } + } + + protected getMountedRoots(): readonly Component[] { + return this.children; + } + + private isComponentMounted(component: Component): boolean { + return this.getMountedRoots().some((child) => this.containsComponent(child, component)); + } + + private containsComponent(root: Component, target: Component): boolean { + if (root === target) return true; + if (!(root instanceof Container)) return false; + return root.children.some((child) => this.containsComponent(child, target)); + } + + /** + * Show an overlay component with configurable positioning and sizing. + * Returns a handle to control the overlay's visibility. + */ + showOverlay(component: Component, options?: OverlayOptions): OverlayHandle { + const entry: OverlayStackEntry = { + component, + ...(options === undefined ? {} : { options }), + preFocus: this.focusedComponent, + hidden: false, + focusOrder: ++this.focusOrderCounter, + }; + this.overlayStack.push(entry); + // Only focus if overlay is actually visible + if (!options?.nonCapturing && this.isOverlayVisible(entry)) { + this.setFocus(component); + } + this.terminal.hideCursor(); + this.requestRender(); + + // Return handle for controlling this overlay + return { + hide: () => { + const index = this.overlayStack.indexOf(entry); + if (index !== -1) { + this.clearOverlayFocusRestoreFor(entry); + this.retargetOverlayPreFocus(entry); + this.overlayStack.splice(index, 1); + // Restore focus if this overlay had focus + if (this.focusedComponent === component) { + const topVisible = this.getTopmostVisibleOverlay(); + this.setFocus(topVisible?.component ?? entry.preFocus); + } + if (this.overlayStack.length === 0) this.terminal.hideCursor(); + this.requestRender(); + } + }, + setHidden: (hidden: boolean) => { + if (entry.hidden === hidden) return; + entry.hidden = hidden; + // Update focus when hiding/showing + if (hidden) { + this.clearOverlayFocusRestoreFor(entry); + // If this overlay had focus, move focus to next visible or preFocus + if (this.focusedComponent === component) { + const topVisible = this.getTopmostVisibleOverlay(); + this.setFocus(topVisible?.component ?? entry.preFocus); + } + } else { + // Restore focus to this overlay when showing (if it's actually visible) + if (!options?.nonCapturing && this.isOverlayVisible(entry)) { + entry.focusOrder = ++this.focusOrderCounter; + this.setFocus(component); + } + } + this.requestRender(); + }, + isHidden: () => entry.hidden, + focus: () => { + if (!this.overlayStack.includes(entry) || !this.isOverlayVisible(entry)) return; + entry.focusOrder = ++this.focusOrderCounter; + this.setFocus(component); + this.requestRender(); + }, + unfocus: (unfocusOptions) => { + const isFocused = this.focusedComponent === component; + const restoreState = this.overlayFocusRestore; + const hasPendingRestore = restoreState.status !== "inactive" && restoreState.overlay === entry; + if (!isFocused && !hasPendingRestore) return; + if ( + restoreState.status === "blocked" && + restoreState.overlay === entry && + this.focusedComponent === restoreState.blockedBy + ) { + if (unfocusOptions) { + this.overlayFocusRestore = { + status: "blocked", + overlay: entry, + blockedBy: restoreState.blockedBy, + resume: { status: "focus-target", target: unfocusOptions.target }, + }; + } else { + this.clearOverlayFocusRestore(); + } + this.requestRender(); + return; + } + this.clearOverlayFocusRestoreFor(entry); + if (isFocused || unfocusOptions) { + const topVisible = this.getTopmostVisibleOverlay(); + const fallbackTarget = topVisible && topVisible !== entry ? topVisible.component : entry.preFocus; + this.setFocus(unfocusOptions ? unfocusOptions.target : fallbackTarget); + } + this.requestRender(); + }, + isFocused: () => this.focusedComponent === component, + getBounds: () => { + if (!this.overlayStack.includes(entry) || !this.isOverlayVisible(entry) || !entry.bounds) return undefined; + return { ...entry.bounds }; + }, + }; + } + + /** Hide the topmost overlay and restore previous focus. */ + hideOverlay(): void { + const overlay = this.overlayStack[this.overlayStack.length - 1]; + if (!overlay) return; + this.clearOverlayFocusRestoreFor(overlay); + this.retargetOverlayPreFocus(overlay); + this.overlayStack.pop(); + if (this.focusedComponent === overlay.component) { + // Find topmost visible overlay, or fall back to preFocus + const topVisible = this.getTopmostVisibleOverlay(); + this.setFocus(topVisible?.component ?? overlay.preFocus); + } + if (this.overlayStack.length === 0) this.terminal.hideCursor(); + this.requestRender(); + } + + /** Check if there are any visible overlays */ + hasOverlay(): boolean { + return this.overlayStack.some((o) => this.isOverlayVisible(o)); + } + + /** Check if the focused component is a visible overlay */ + protected isOverlayFocused(): boolean { + return this.overlayStack.some( + (entry) => entry.component === this.focusedComponent && this.isOverlayVisible(entry), + ); + } + + /** Keep overlay containers as keyboard focus owners when a nested control is clicked. */ + protected resolveMouseFocusTarget(component: Component): Component { + for (let index = this.overlayStack.length - 1; index >= 0; index--) { + const overlay = this.overlayStack[index]!; + if (this.isOverlayVisible(overlay) && this.containsComponent(overlay.component, component)) { + return overlay.component; + } + } + return component; + } + + /** Dispatch to the visually topmost overlay under the pointer. */ + protected dispatchMouseToOverlay(event: TuiMouseEvent): { hit: boolean; result?: TuiMouseDispatchResult } { + for (let index = this.renderedOverlayLayouts.length - 1; index >= 0; index--) { + const layout = this.renderedOverlayLayouts[index]!; + if ( + event.screenX < layout.col || + event.screenX >= layout.col + layout.width || + event.screenY < layout.row || + event.screenY >= layout.row + layout.height + ) { + continue; + } + const result = dispatchMouseEvent(layout.entry.component, { + ...event, + x: event.screenX - layout.col, + y: event.screenY - layout.row, + width: layout.width, + height: layout.height, + }); + return result + ? { + hit: true, + result: result.focus ? { ...result, focusTarget: layout.entry.component } : result, + } + : { hit: true }; + } + return { hit: false }; + } + + /** Check if an overlay entry is currently visible */ + private isOverlayVisible(entry: OverlayStackEntry): boolean { + if (entry.hidden) return false; + if (entry.options?.visible) { + return entry.options.visible(this.terminal.columns, this.terminal.rows); + } + return true; + } + + /** Find the visual-frontmost visible capturing overlay, if any */ + private getTopmostVisibleOverlay(): OverlayStackEntry | undefined { + let topmost: OverlayStackEntry | undefined; + for (const overlay of this.overlayStack) { + if (overlay.options?.nonCapturing || !this.isOverlayVisible(overlay)) continue; + if (!topmost || overlay.focusOrder > topmost.focusOrder) { + topmost = overlay; + } + } + return topmost; + } + + override invalidate(): void { + for (const root of this.getMountedRoots()) root.invalidate(); + for (const overlay of this.overlayStack) overlay.component.invalidate(); + } + + start(): void { + this.stopped = false; + this.beforeTerminalStart(); + this.terminal.start( + (data) => this.handleTerminalInput(data), + () => this.requestRender(), + ); + this.afterTerminalStart(); + this.terminal.hideCursor(); + if (this.terminalColorSchemeNotificationsEnabled) { + this.terminal.write("\x1b[?2031h"); + } + this.queryCellSize(); + this.requestRender(); + } + + addInputListener(listener: TuiInputListener): () => void { + this.inputListeners.add(listener); + return () => { + this.inputListeners.delete(listener); + }; + } + + removeInputListener(listener: TuiInputListener): void { + this.inputListeners.delete(listener); + } + + onTerminalColorSchemeChange(listener: (scheme: TerminalColorScheme) => void): () => void { + this.terminalColorSchemeListeners.add(listener); + return () => { + this.terminalColorSchemeListeners.delete(listener); + }; + } + + setTerminalColorSchemeNotifications(enabled: boolean): void { + if (this.terminalColorSchemeNotificationsEnabled === enabled) { + return; + } + this.terminalColorSchemeNotificationsEnabled = enabled; + if (!this.stopped) { + this.terminal.write(enabled ? "\x1b[?2031h" : "\x1b[?2031l"); + } + } + + private queryCellSize(): void { + // Only query if terminal supports images (cell size is only used for image rendering) + if (!getCapabilities().images) { + return; + } + // Query terminal for cell size in pixels: CSI 16 t + // Response format: CSI 6 ; height ; width t + this.terminal.write("\x1b[16t"); + } + + stop(options: TuiStopOptions = {}): void { + this.stopped = true; + this.cancelRenderTimer(); + if (this.terminalColorSchemeNotificationsEnabled) { + this.terminal.write("\x1b[?2031l"); + } + this.beforeTerminalStop(options); + this.terminal.showCursor(); + this.terminal.stop(); + this.afterTerminalStop(options); + } + + renderNow(force = false): void { + if (force) this.resetRenderState(); + this.renderRequested = false; + this.cancelRenderTimer(); + this.lastRenderAt = performance.now(); + this.doRender(); + } + + requestRender(force = false): void { + if (force) { + this.resetRenderState(); + this.requestImmediateRender(); + return; + } + if (this.renderRequested) return; + this.renderRequested = true; + process.nextTick(() => this.scheduleRender()); + } + + private requestImmediateRender(): void { + this.cancelRenderTimer(); + this.renderRequested = true; + if (this.immediateRenderScheduled) return; + this.immediateRenderScheduled = true; + process.nextTick(() => { + this.immediateRenderScheduled = false; + if (this.stopped || !this.renderRequested) return; + // A previously queued scheduleRender() can create a timer before this + // callback runs. User input must preempt that throttled frame. + this.cancelRenderTimer(); + this.renderRequested = false; + this.lastRenderAt = performance.now(); + this.doRender(); + }); + } + + private cancelRenderTimer(): void { + if (!this.renderTimer) return; + clearTimeout(this.renderTimer); + this.renderTimer = undefined; + } + + private scheduleRender(): void { + if (this.stopped || this.renderTimer || !this.renderRequested) { + return; + } + const elapsed = performance.now() - this.lastRenderAt; + const delay = Math.max(0, TuiBase.MIN_RENDER_INTERVAL_MS - elapsed); + this.renderTimer = setTimeout(() => { + this.renderTimer = undefined; + if (this.stopped || !this.renderRequested) { + return; + } + this.renderRequested = false; + this.lastRenderAt = performance.now(); + this.doRender(); + if (this.renderRequested) { + this.scheduleRender(); + } + }, delay); + } + + private handleTerminalInput(data: string): void { + if (this.consumeOsc11BackgroundResponse(data)) { + return; + } + if (this.consumeTerminalColorSchemeReport(data)) { + return; + } + + if (this.inputListeners.size > 0) { + let current = data; + for (const listener of this.inputListeners) { + const result = listener(current); + if (result?.consume) { + return; + } + if (result?.data !== undefined) { + current = result.data; + } + } + if (current.length === 0) { + return; + } + data = current; + } + + // Consume terminal cell size responses without blocking unrelated input. + if (this.consumeCellSizeResponse(data)) { + return; + } + + // Global debug key handler (Shift+Ctrl+D) + if (matchesKey(data, "shift+ctrl+d") && this.onDebug) { + this.onDebug(); + return; + } + + // If focused component is an overlay, verify it's still visible + // (visibility can change due to terminal resize or visible() callback) + const focusedOverlay = this.overlayStack.find((o) => o.component === this.focusedComponent); + if (focusedOverlay && !this.isOverlayVisible(focusedOverlay)) { + // Focused overlay is no longer visible, redirect to topmost visible overlay + const topVisible = this.getTopmostVisibleOverlay(); + if (topVisible) { + this.setFocus(topVisible.component); + } else { + this.setFocusInternal({ component: focusedOverlay.preFocus, overlayFocusRestore: "preserve" }); + } + } + + const focusIsOverlay = this.overlayStack.some((o) => o.component === this.focusedComponent); + if (!focusIsOverlay) { + const restoreState = this.getVisibleOverlayFocusRestore(); + if (restoreState.status === "eligible") { + this.setFocus(restoreState.overlay.component); + } else if (restoreState.status === "blocked" && restoreState.blockedBy !== this.focusedComponent) { + if (restoreState.resume.status === "restore-overlay") { + this.setFocus(restoreState.overlay.component); + } else { + this.clearOverlayFocusRestore(); + this.setFocus(restoreState.resume.target); + } + } + } + + // Pass input to focused component (including Ctrl+C) + // The focused component can decide how to handle Ctrl+C + if (this.focusedComponent?.handleInput) { + // Filter out key release events unless component opts in + if (isKeyRelease(data) && !this.focusedComponent.wantsKeyRelease) { + return; + } + this.focusedComponent.handleInput(data); + // Keyboard input is latency-sensitive. Avoid the throttled timer path, + // where even setTimeout(0) can take a full 16 ms tick on Windows. + this.requestImmediateRender(); + } + } + + private consumeOsc11BackgroundResponse(data: string): boolean { + if (this.pendingOsc11BackgroundReplies <= 0) { + return false; + } + + if (!isOsc11BackgroundColorResponse(data)) { + return false; + } + + const rgb = parseOsc11BackgroundColor(data); + this.pendingOsc11BackgroundReplies -= 1; + const query = this.pendingOsc11BackgroundQueries.shift(); + if (query && !query.settled) { + query.settled = true; + if (query.timer) { + clearTimeout(query.timer); + query.timer = undefined; + } + query.resolve?.(rgb); + query.resolve = undefined; + } + return true; + } + + private consumeTerminalColorSchemeReport(data: string): boolean { + const scheme = parseTerminalColorSchemeReport(data); + if (!scheme) { + return false; + } + + for (const listener of this.terminalColorSchemeListeners) { + listener(scheme); + } + return true; + } + + private consumeCellSizeResponse(data: string): boolean { + // Response format: ESC [ 6 ; height ; width t + const match = data.match(/^\x1b\[6;(\d+);(\d+)t$/); + if (!match) { + return false; + } + + const heightPx = parseInt(match[1], 10); + const widthPx = parseInt(match[2], 10); + if (heightPx <= 0 || widthPx <= 0) { + return true; + } + + setCellDimensions({ widthPx, heightPx }); + // Invalidate all components so images re-render with correct dimensions. + this.invalidate(); + this.requestRender(); + return true; + } + + /** + * Resolve overlay layout from options. + * Returns { width, row, col, maxHeight } for rendering. + */ + private resolveOverlayLayout( + options: OverlayOptions | undefined, + overlayHeight: number, + termWidth: number, + termHeight: number, + ): { width: number; row: number; col: number; maxHeight: number | undefined } { + const opt = options ?? {}; + + // Parse margin (clamp to non-negative) + const margin = + typeof opt.margin === "number" + ? { top: opt.margin, right: opt.margin, bottom: opt.margin, left: opt.margin } + : (opt.margin ?? {}); + const marginTop = Math.max(0, margin.top ?? 0); + const marginRight = Math.max(0, margin.right ?? 0); + const marginBottom = Math.max(0, margin.bottom ?? 0); + const marginLeft = Math.max(0, margin.left ?? 0); + + // Available space after margins + const availWidth = Math.max(1, termWidth - marginLeft - marginRight); + const availHeight = Math.max(1, termHeight - marginTop - marginBottom); + + // === Resolve width === + let width = parseSizeValue(opt.width, termWidth) ?? Math.min(80, availWidth); + // Apply minWidth + if (opt.minWidth !== undefined) { + width = Math.max(width, opt.minWidth); + } + // Clamp to available space + width = Math.max(1, Math.min(width, availWidth)); + + // === Resolve maxHeight === + let maxHeight = parseSizeValue(opt.maxHeight, termHeight); + // Clamp to available space + if (maxHeight !== undefined) { + maxHeight = Math.max(1, Math.min(maxHeight, availHeight)); + } + + // Effective overlay height (may be clamped by maxHeight) + const effectiveHeight = maxHeight !== undefined ? Math.min(overlayHeight, maxHeight) : overlayHeight; + + // === Resolve position === + let row: number; + let col: number; + + if (opt.row !== undefined) { + if (typeof opt.row === "string") { + // Percentage: 0% = top, 100% = bottom (overlay stays within bounds) + const match = opt.row.match(/^(\d+(?:\.\d+)?)%$/); + if (match) { + const maxRow = Math.max(0, availHeight - effectiveHeight); + const percent = parseFloat(match[1]) / 100; + row = marginTop + Math.floor(maxRow * percent); + } else { + // Invalid format, fall back to center + row = this.resolveAnchorRow("center", effectiveHeight, availHeight, marginTop); + } + } else { + // Absolute row position + row = opt.row; + } + } else { + // Anchor-based (default: center) + const anchor = opt.anchor ?? "center"; + row = this.resolveAnchorRow(anchor, effectiveHeight, availHeight, marginTop); + } + + if (opt.col !== undefined) { + if (typeof opt.col === "string") { + // Percentage: 0% = left, 100% = right (overlay stays within bounds) + const match = opt.col.match(/^(\d+(?:\.\d+)?)%$/); + if (match) { + const maxCol = Math.max(0, availWidth - width); + const percent = parseFloat(match[1]) / 100; + col = marginLeft + Math.floor(maxCol * percent); + } else { + // Invalid format, fall back to center + col = this.resolveAnchorCol("center", width, availWidth, marginLeft); + } + } else { + // Absolute column position + col = opt.col; + } + } else { + // Anchor-based (default: center) + const anchor = opt.anchor ?? "center"; + col = this.resolveAnchorCol(anchor, width, availWidth, marginLeft); + } + + // Apply offsets + if (opt.offsetY !== undefined) row += opt.offsetY; + if (opt.offsetX !== undefined) col += opt.offsetX; + + // Clamp to terminal bounds (respecting margins) + row = Math.max(marginTop, Math.min(row, termHeight - marginBottom - effectiveHeight)); + col = Math.max(marginLeft, Math.min(col, termWidth - marginRight - width)); + + return { width, row, col, maxHeight }; + } + + private resolveAnchorRow(anchor: OverlayAnchor, height: number, availHeight: number, marginTop: number): number { + switch (anchor) { + case "top-left": + case "top-center": + case "top-right": + return marginTop; + case "bottom-left": + case "bottom-center": + case "bottom-right": + return marginTop + availHeight - height; + case "left-center": + case "center": + case "right-center": + return marginTop + Math.floor((availHeight - height) / 2); + } + } + + private resolveAnchorCol(anchor: OverlayAnchor, width: number, availWidth: number, marginLeft: number): number { + switch (anchor) { + case "top-left": + case "left-center": + case "bottom-left": + return marginLeft; + case "top-right": + case "right-center": + case "bottom-right": + return marginLeft + availWidth - width; + case "top-center": + case "center": + case "bottom-center": + return marginLeft + Math.floor((availWidth - width) / 2); + } + } + + /** Composite all overlays into content lines (sorted by focusOrder, higher = on top). */ + protected compositeOverlays(lines: string[], termWidth: number, termHeight: number): string[] { + if (this.overlayStack.length === 0) { + this.renderedOverlayLayouts = []; + return lines; + } + const result = [...lines]; + + for (const entry of this.overlayStack) entry.bounds = undefined; + + // Pre-render all visible overlays and calculate positions + const rendered: { entry: OverlayStackEntry; overlayLines: string[]; row: number; col: number; w: number }[] = []; + let minLinesNeeded = result.length; + + const visibleEntries = this.overlayStack.filter((e) => this.isOverlayVisible(e)); + visibleEntries.sort((a, b) => a.focusOrder - b.focusOrder); + for (const entry of visibleEntries) { + const { component, options } = entry; + + // Get layout with height=0 first to determine width and maxHeight + // (width and maxHeight don't depend on overlay height) + const { width, maxHeight } = this.resolveOverlayLayout(options, 0, termWidth, termHeight); + + // Render component at calculated width + let overlayLines = component.render(width); + + // Apply maxHeight if specified + if (maxHeight !== undefined && overlayLines.length > maxHeight) { + overlayLines = overlayLines.slice(0, maxHeight); + } + + // Get final row/col with actual overlay height + const { row, col } = this.resolveOverlayLayout(options, overlayLines.length, termWidth, termHeight); + entry.bounds = { row, col, width, height: overlayLines.length }; + + rendered.push({ entry, overlayLines, row, col, w: width }); + minLinesNeeded = Math.max(minLinesNeeded, row + overlayLines.length); + } + this.renderedOverlayLayouts = rendered.map(({ entry, row, col, w, overlayLines }) => ({ + entry, + row, + col, + width: w, + height: overlayLines.length, + })); + + // Pad to at least terminal height so overlays have screen-relative positions. + // Excludes maxLinesRendered: the historical high-water mark caused self-reinforcing + // inflation that pushed content into scrollback on terminal widen. + const workingHeight = Math.max(result.length, termHeight, minLinesNeeded); + + // Extend result with empty lines if content is too short for overlay placement or working area + while (result.length < workingHeight) { + result.push(""); + } + + const viewportStart = Math.max(0, workingHeight - termHeight); + + // Composite each overlay + for (const { overlayLines, row, col, w } of rendered) { + for (let i = 0; i < overlayLines.length; i++) { + const idx = viewportStart + row + i; + if (idx >= 0 && idx < result.length) { + // Defensive: truncate overlay line to declared width before compositing + // (components should already respect width, but this ensures it) + const truncatedOverlayLine = + visibleWidth(overlayLines[i]) > w ? sliceByColumn(overlayLines[i], 0, w, true) : overlayLines[i]; + result[idx] = this.compositeLineAt(result[idx], truncatedOverlayLine, col, w, termWidth); + } + } + } + + return result; + } + + protected applyLineResets(lines: string[]): string[] { + const reset = SEGMENT_RESET; + for (let i = 0; i < lines.length; i++) { + const line = lines[i]; + if (!isImageLine(line)) { + lines[i] = normalizeTerminalOutput(line) + reset; + } + } + return lines; + } + + private compositeLineAt( + baseLine: string, + overlayLine: string, + startCol: number, + overlayWidth: number, + totalWidth: number, + ): string { + return compositeTuiLine(baseLine, overlayLine, startCol, overlayWidth, totalWidth); + } + + /** + * Find and extract cursor position from rendered lines. + * Searches for CURSOR_MARKER, calculates its position, and strips it from the output. + * Only scans the bottom terminal height lines (visible viewport). + * @param lines - Rendered lines to search + * @param height - Terminal height (visible viewport size) + * @returns Cursor position { row, col } or null if no marker found + */ + protected extractCursorPosition(lines: string[], height: number): { row: number; col: number } | null { + // Only scan the bottom `height` lines (visible viewport) + const viewportTop = Math.max(0, lines.length - height); + for (let row = lines.length - 1; row >= viewportTop; row--) { + const line = lines[row]; + const markerIndex = line.indexOf(CURSOR_MARKER); + if (markerIndex !== -1) { + // Calculate visual column (width of text before marker) + const beforeMarker = line.slice(0, markerIndex); + const col = visibleWidth(beforeMarker); + + // Strip marker from the line + lines[row] = line.slice(0, markerIndex) + line.slice(markerIndex + CURSOR_MARKER.length); + + return { row, col }; + } + } + return null; + } + + /** + * Query the terminal's default background color with OSC 11 (`ESC ] 11 ; ? BEL`). + * @param timeoutMs Query timeout in milliseconds. + * @returns Promise containing the parsed RGB color, or undefined if it times out or fails to parse. + */ + queryTerminalBackgroundColor({ timeoutMs }: { timeoutMs: number }): Promise { + return new Promise((resolve) => { + const query: PendingOsc11BackgroundQuery = { + settled: false, + resolve, + timer: undefined, + }; + + query.timer = setTimeout(() => { + if (query.settled) { + return; + } + query.settled = true; + query.timer = undefined; + query.resolve?.(undefined); + query.resolve = undefined; + }, timeoutMs); + this.pendingOsc11BackgroundQueries.push(query); + this.pendingOsc11BackgroundReplies += 1; + this.terminal.write("\x1b]11;?\x07"); + }); + } + + /** + * Query the terminal's color-scheme preference with DSR (`CSI ? 996 n`). + * Terminals that support the color palette notification protocol reply with + * `CSI ? 997 ; 1 n` for dark or `CSI ? 997 ; 2 n` for light. + */ + queryTerminalColorScheme({ timeoutMs }: { timeoutMs: number }): Promise { + return new Promise((resolve) => { + let settled = false; + let timer: NodeJS.Timeout | undefined; + let unsubscribe: () => void = () => {}; + const settle = (scheme: TerminalColorScheme | undefined) => { + if (settled) return; + settled = true; + if (timer) { + clearTimeout(timer); + timer = undefined; + } + unsubscribe(); + resolve(scheme); + }; + + unsubscribe = this.onTerminalColorSchemeChange(settle); + timer = setTimeout(() => settle(undefined), timeoutMs); + this.terminal.write("\x1b[?996n"); + }); + } +} diff --git a/packages/tui/src/undo-stack.ts b/packages/tui/src/undo-stack.ts new file mode 100644 index 0000000000000000000000000000000000000000..5b9a7e9cee03a08b5d7ee637b92834a3a28d7fd2 --- /dev/null +++ b/packages/tui/src/undo-stack.ts @@ -0,0 +1,28 @@ +/** + * Generic undo stack with clone-on-push semantics. + * + * Stores deep clones of state snapshots. Popped snapshots are returned + * directly (no re-cloning) since they are already detached. + */ +export class UndoStack { + private stack: S[] = []; + + /** Push a deep clone of the given state onto the stack. */ + push(state: S): void { + this.stack.push(structuredClone(state)); + } + + /** Pop and return the most recent snapshot, or undefined if empty. */ + pop(): S | undefined { + return this.stack.pop(); + } + + /** Remove all snapshots. */ + clear(): void { + this.stack.length = 0; + } + + get length(): number { + return this.stack.length; + } +} diff --git a/packages/tui/src/utils.ts b/packages/tui/src/utils.ts new file mode 100644 index 0000000000000000000000000000000000000000..419c52fb779e83c5c1deb0640f56626a4ffe2710 --- /dev/null +++ b/packages/tui/src/utils.ts @@ -0,0 +1,1337 @@ +import { eastAsianWidth } from "get-east-asian-width"; + +// segmenters (shared instance) +const graphemeSegmenter = new Intl.Segmenter(undefined, { granularity: "grapheme" }); +const wordSegmenter = new Intl.Segmenter(undefined, { granularity: "word" }); + +/** + * Get the shared grapheme segmenter instance. + */ +export function getGraphemeSegmenter(): Intl.Segmenter { + return graphemeSegmenter; +} + +/** + * Get the shared word segmenter instance. + */ +export function getWordSegmenter(): Intl.Segmenter { + return wordSegmenter; +} + +/** + * Check if a grapheme cluster (after segmentation) could possibly be an RGI emoji. + * This is a fast heuristic to avoid the expensive rgiEmojiRegex test. + * The tested Unicode blocks are deliberately broad to account for future + * Unicode additions. + */ +function couldBeEmoji(segment: string): boolean { + const cp = segment.codePointAt(0)!; + return ( + (cp >= 0x1f000 && cp <= 0x1fbff) || // Emoji and Pictograph + (cp >= 0x2300 && cp <= 0x23ff) || // Misc technical + (cp >= 0x2600 && cp <= 0x27bf) || // Misc symbols, dingbats + (cp >= 0x2b50 && cp <= 0x2b55) || // Specific stars/circles + segment.includes("\uFE0F") || // Contains VS16 (emoji presentation selector) + segment.length > 2 // Multi-codepoint sequences (ZWJ, skin tones, etc.) + ); +} + +// Regexes for character classification (same as string-width library) +const zeroWidthRegex = /^(?:\p{Default_Ignorable_Code_Point}|\p{Control}|\p{Mark}|\p{Surrogate})+$/v; +const leadingNonPrintingRegex = /^[\p{Default_Ignorable_Code_Point}\p{Control}\p{Format}\p{Mark}\p{Surrogate}]+/v; +const nonPrintingCharRegex = /^(?:\p{Default_Ignorable_Code_Point}|\p{Control}|\p{Format}|\p{Mark}|\p{Surrogate})$/v; +const markCharRegex = /^\p{Mark}$/v; +// Marks that terminals allocate cells for when attached to a base character. +// This includes Unicode spacing marks and non-spacing exceptions in legacy wcwidth tables. +const terminalSpacingMarkRegex = + /^(?:[\p{Spacing_Mark}--[\u1734\u302E\u302F]]|[\u065F\u0F7F\u102B\u102C\u1031\u1033-\u1035\u1038\u103A-\u103E])+$/v; +const rgiEmojiRegex = /^\p{RGI_Emoji}$/v; + +// Cache for non-ASCII strings +const WIDTH_CACHE_SIZE = 512; +const widthCache = new Map(); + +export const cjkBreakRegex = + /[\p{Script_Extensions=Han}\p{Script_Extensions=Hiragana}\p{Script_Extensions=Katakana}\p{Script_Extensions=Hangul}\p{Script_Extensions=Bopomofo}]/u; + +function isPrintableAscii(str: string): boolean { + for (let i = 0; i < str.length; i++) { + const code = str.charCodeAt(i); + if (code < 0x20 || code > 0x7e) { + return false; + } + } + return true; +} + +function truncateFragmentToWidth(text: string, maxWidth: number): { text: string; width: number } { + if (maxWidth <= 0 || text.length === 0) { + return { text: "", width: 0 }; + } + + if (isPrintableAscii(text)) { + const clipped = text.slice(0, maxWidth); + return { text: clipped, width: clipped.length }; + } + + const hasAnsi = text.includes("\x1b"); + const hasTabs = text.includes("\t"); + if (!hasAnsi && !hasTabs) { + let result = ""; + let width = 0; + for (const { segment } of graphemeSegmenter.segment(text)) { + const w = graphemeWidth(segment); + if (width + w > maxWidth) { + break; + } + result += segment; + width += w; + } + return { text: result, width }; + } + + let result = ""; + let width = 0; + let i = 0; + let pendingAnsi = ""; + + while (i < text.length) { + const ansi = extractAnsiCode(text, i); + if (ansi) { + pendingAnsi += ansi.code; + i += ansi.length; + continue; + } + + if (text[i] === "\t") { + if (width + 3 > maxWidth) { + break; + } + if (pendingAnsi) { + result += pendingAnsi; + pendingAnsi = ""; + } + result += "\t"; + width += 3; + i++; + continue; + } + + let end = i; + while (end < text.length && text[end] !== "\t") { + const nextAnsi = extractAnsiCode(text, end); + if (nextAnsi) { + break; + } + end++; + } + + for (const { segment } of graphemeSegmenter.segment(text.slice(i, end))) { + const w = graphemeWidth(segment); + if (width + w > maxWidth) { + return { text: result, width }; + } + if (pendingAnsi) { + result += pendingAnsi; + pendingAnsi = ""; + } + result += segment; + width += w; + } + i = end; + } + + return { text: result, width }; +} + +function finalizeTruncatedResult( + prefix: string, + prefixWidth: number, + ellipsis: string, + ellipsisWidth: number, + maxWidth: number, + pad: boolean, +): string { + const reset = "\x1b[0m"; + const hyperlinkClose = getActiveOsc8Close(prefix); + const visibleWidth = prefixWidth + ellipsisWidth; + let result: string; + + if (ellipsis.length > 0) { + result = `${prefix}${hyperlinkClose}${reset}${ellipsis}${reset}`; + } else { + result = `${prefix}${hyperlinkClose}${reset}`; + } + + return pad ? result + " ".repeat(Math.max(0, maxWidth - visibleWidth)) : result; +} + +/** + * Calculate the terminal width of a single grapheme cluster. + * Based on code from the string-width library, but includes a possible-emoji + * check to avoid running the RGI_Emoji regex unnecessarily. + */ +function graphemeWidth(segment: string): number { + if (segment === "\t") { + return 3; + } + + // Some marks occupy cells even without a base character. + if (terminalSpacingMarkRegex.test(segment)) { + return [...segment].length; + } + + // Zero-width clusters + if (zeroWidthRegex.test(segment)) { + return 0; + } + + // Emoji check with pre-filter + if (couldBeEmoji(segment) && rgiEmojiRegex.test(segment)) { + return 2; + } + + // Get base visible codepoint + const base = segment.replace(leadingNonPrintingRegex, ""); + const cp = base.codePointAt(0); + if (cp === undefined) { + return 0; + } + + // Regional indicator symbols (U+1F1E6..U+1F1FF) are often rendered as + // full-width emoji in terminals, even when isolated during streaming. + // Keep width conservative (2) to avoid terminal auto-wrap drift artifacts. + if (cp >= 0x1f1e6 && cp <= 0x1f1ff) { + return 2; + } + + let width = eastAsianWidth(cp); + + // Intl.Segmenter can group multiple terminal-spacing code points into one + // grapheme. Count trailing visible code points that terminals may allocate + // cells for: Indic consonants after marks, halfwidth/fullwidth forms, and + // Thai/Lao AM vowels. + let followsMark = false; + const chars = [...base]; + for (const char of chars.slice(1)) { + if (terminalSpacingMarkRegex.test(char)) { + width += 1; + followsMark = false; + } else if (markCharRegex.test(char)) { + followsMark = true; + } else if (!nonPrintingCharRegex.test(char)) { + const c = char.codePointAt(0)!; + if (followsMark || (c >= 0xff00 && c <= 0xffef)) { + // halfwidth + fullwidth forms + width += eastAsianWidth(c); + } else if (c === 0x0e33 || c === 0x0eb3) { + width += 1; + } + followsMark = false; + } + } + + return width; +} + +/** + * Calculate the visible width of a string in terminal columns. + */ +export function visibleWidth(str: string): number { + if (str.length === 0) { + return 0; + } + + // Fast path: pure ASCII printable + if (isPrintableAscii(str)) { + return str.length; + } + + // Check cache + const cached = widthCache.get(str); + if (cached !== undefined) { + return cached; + } + + // Normalize: tabs to 3 spaces, strip ANSI escape codes + let clean = str; + if (str.includes("\t")) { + clean = clean.replace(/\t/g, " "); + } + if (clean.includes("\x1b")) { + // Strip supported ANSI/OSC/APC escape sequences in one pass. + // This covers CSI styling/cursor codes, OSC hyperlinks and prompt markers, + // and APC sequences like CURSOR_MARKER. + let stripped = ""; + let i = 0; + while (i < clean.length) { + const ansi = extractAnsiCode(clean, i); + if (ansi) { + i += ansi.length; + continue; + } + stripped += clean[i]; + i++; + } + clean = stripped; + } + + // Calculate width + let width = 0; + for (const { segment } of graphemeSegmenter.segment(clean)) { + width += graphemeWidth(segment); + } + + // Cache result + if (widthCache.size >= WIDTH_CACHE_SIZE) { + const firstKey = widthCache.keys().next().value; + if (firstKey !== undefined) { + widthCache.delete(firstKey); + } + } + widthCache.set(str, width); + + return width; +} + +/** Remove ANSI, OSC, and APC control sequences while preserving visible text. */ +export function stripTerminalSequences(str: string): string { + if (!str.includes("\x1b")) return str; + let result = ""; + let i = 0; + while (i < str.length) { + const ansi = extractAnsiCode(str, i); + if (ansi) { + i += ansi.length; + continue; + } + result += str[i]; + i++; + } + return result; +} + +interface GraphemeCellRange { + start: number; + end: number; +} + +/** Return the terminal-cell range occupied by the grapheme at a visible column. */ +export function getGraphemeCellRange(line: string, column: number): GraphemeCellRange | undefined { + let currentCol = 0; + let i = 0; + while (i < line.length) { + const ansi = extractAnsiCode(line, i); + if (ansi) { + i += ansi.length; + continue; + } + let textEnd = i; + while (textEnd < line.length && !extractAnsiCode(line, textEnd)) textEnd++; + for (const { segment } of graphemeSegmenter.segment(line.slice(i, textEnd))) { + const width = graphemeWidth(segment); + if (width > 0 && column >= currentCol && column < currentCol + width) { + return { start: currentCol, end: currentCol + width }; + } + currentCol += width; + } + i = textEnd; + } + return undefined; +} + +/** Return the OSC 8 hyperlink covering a visible terminal column. */ +export function getOsc8LinkAtColumn(line: string, column: number): string | undefined { + let activeUrl: string | undefined; + let currentCol = 0; + let i = 0; + while (i < line.length) { + const ansi = extractAnsiCode(line, i); + if (ansi) { + const hyperlink = /^\x1b\]8;[^;]*;([^\x07\x1b]*)(?:\x07|\x1b\\)$/.exec(ansi.code); + if (hyperlink) activeUrl = hyperlink[1] || undefined; + i += ansi.length; + continue; + } + let textEnd = i; + while (textEnd < line.length && !extractAnsiCode(line, textEnd)) textEnd++; + for (const { segment } of graphemeSegmenter.segment(line.slice(i, textEnd))) { + const width = segment === "\t" ? 3 : graphemeWidth(segment); + if (column >= currentCol && column < currentCol + width) return activeUrl; + currentCol += width; + } + i = textEnd; + } + return undefined; +} + +/** + * Normalize text for terminal output without changing logical editor content. + * Some terminals render precomposed Thai/Lao AM vowels inconsistently during + * differential repaint. Their compatibility decompositions have the same cell + * width but avoid stale-cell artifacts in terminal renderers. Visible tabs are + * expanded to the fixed width used by layout so terminal tab stops cannot wrap + * a logical line, while tabs inside terminal string sequences stay untouched. + */ +const THAI_LAO_AM_REGEX = /[\u0e33\u0eb3]/; +const THAI_LAO_AM_GLOBAL_REGEX = /[\u0e33\u0eb3]/g; + +export function normalizeTerminalOutput(str: string): string { + let normalized = str; + if (THAI_LAO_AM_REGEX.test(normalized)) { + normalized = normalized.replace(THAI_LAO_AM_GLOBAL_REGEX, (char) => + char === "\u0e33" ? "\u0e4d\u0e32" : "\u0ecd\u0eb2", + ); + } + if (!normalized.includes("\t")) return normalized; + + let result = ""; + let i = 0; + while (i < normalized.length) { + const ansi = extractAnsiCode(normalized, i); + if (ansi) { + result += ansi.code; + i += ansi.length; + continue; + } + result += normalized[i] === "\t" ? " " : normalized[i]; + i++; + } + return result; +} + +/** + * Extract ANSI escape sequences from a string at the given position. + */ +export function extractAnsiCode(str: string, pos: number): { code: string; length: number } | null { + if (pos >= str.length || str[pos] !== "\x1b") return null; + + const next = str[pos + 1]; + + // CSI sequence: ESC [ ... m/G/K/H/J + if (next === "[") { + let j = pos + 2; + while (j < str.length && !/[mGKHJ]/.test(str[j]!)) j++; + if (j < str.length) return { code: str.substring(pos, j + 1), length: j + 1 - pos }; + return null; + } + + // OSC sequence: ESC ] ... BEL or ESC ] ... ST (ESC \) + // Used for hyperlinks (OSC 8), window titles, etc. + if (next === "]") { + let j = pos + 2; + while (j < str.length) { + if (str[j] === "\x07") return { code: str.substring(pos, j + 1), length: j + 1 - pos }; + if (str[j] === "\x1b" && str[j + 1] === "\\") return { code: str.substring(pos, j + 2), length: j + 2 - pos }; + j++; + } + return null; + } + + // APC sequence: ESC _ ... BEL or ESC _ ... ST (ESC \) + // Used for cursor marker and application-specific commands + if (next === "_") { + let j = pos + 2; + while (j < str.length) { + if (str[j] === "\x07") return { code: str.substring(pos, j + 1), length: j + 1 - pos }; + if (str[j] === "\x1b" && str[j + 1] === "\\") return { code: str.substring(pos, j + 2), length: j + 2 - pos }; + j++; + } + return null; + } + + return null; +} + +type Osc8Terminator = "\x07" | "\x1b\\"; + +interface ActiveHyperlink { + params: string; + url: string; + terminator: Osc8Terminator; +} + +function parseOsc8Hyperlink(ansiCode: string): ActiveHyperlink | null | undefined { + if (!ansiCode.startsWith("\x1b]8;")) { + return undefined; + } + + const terminator: Osc8Terminator = ansiCode.endsWith("\x07") ? "\x07" : "\x1b\\"; + const body = ansiCode.slice(4, terminator === "\x07" ? -1 : -2); + const separatorIndex = body.indexOf(";"); + if (separatorIndex === -1) { + return undefined; + } + + const params = body.slice(0, separatorIndex); + const url = body.slice(separatorIndex + 1); + if (!url) { + return null; + } + return { params, url, terminator }; +} + +function formatOsc8Hyperlink(hyperlink: ActiveHyperlink): string { + return `\x1b]8;${hyperlink.params};${hyperlink.url}${hyperlink.terminator}`; +} + +function formatOsc8Close(terminator: Osc8Terminator): string { + return `\x1b]8;;${terminator}`; +} + +function getActiveOsc8Close(prefix: string): string { + if (!prefix.includes("\x1b]8;")) { + return ""; + } + + let activeHyperlink: ActiveHyperlink | null = null; + let i = 0; + while (i < prefix.length) { + const ansi = extractAnsiCode(prefix, i); + if (ansi) { + const hyperlink = parseOsc8Hyperlink(ansi.code); + if (hyperlink !== undefined) { + activeHyperlink = hyperlink; + } + i += ansi.length; + } else { + i++; + } + } + return activeHyperlink ? formatOsc8Close(activeHyperlink.terminator) : ""; +} + +/** + * Track active ANSI SGR codes to preserve styling across line breaks. + */ +class AnsiCodeTracker { + // Track individual attributes separately so we can reset them specifically + private bold = false; + private dim = false; + private italic = false; + private underline = false; + private blink = false; + private inverse = false; + private hidden = false; + private strikethrough = false; + private fgColor: string | null = null; // Stores the full code like "31" or "38;5;240" + private bgColor: string | null = null; // Stores the full code like "41" or "48;5;240" + private activeHyperlink: ActiveHyperlink | null = null; + + process(ansiCode: string): void { + // OSC 8 hyperlink: \x1b]8;;\x1b\\ (open) or \x1b]8;;\x1b\\ (close). + // Preserve the original terminator because some terminals only make BEL-terminated + // links clickable. OAuth login URLs use BEL, so reopening wrapped lines with ST + // made only the first physical line clickable in those terminals. + const hyperlink = parseOsc8Hyperlink(ansiCode); + if (hyperlink !== undefined) { + this.activeHyperlink = hyperlink; + return; + } + + if (!ansiCode.endsWith("m")) { + return; + } + + // Extract the parameters between \x1b[ and m + const match = ansiCode.match(/\x1b\[([\d;]*)m/); + if (!match) return; + + const params = match[1]; + if (params === "" || params === "0") { + // Full reset + this.reset(); + return; + } + + // Parse parameters (can be semicolon-separated) + const parts = params.split(";"); + let i = 0; + while (i < parts.length) { + const code = Number.parseInt(parts[i], 10); + + // Handle 256-color and RGB codes which consume multiple parameters + if (code === 38 || code === 48) { + // 38;5;N (256 color fg) or 38;2;R;G;B (RGB fg) + // 48;5;N (256 color bg) or 48;2;R;G;B (RGB bg) + if (parts[i + 1] === "5" && parts[i + 2] !== undefined) { + // 256 color: 38;5;N or 48;5;N + const colorCode = `${parts[i]};${parts[i + 1]};${parts[i + 2]}`; + if (code === 38) { + this.fgColor = colorCode; + } else { + this.bgColor = colorCode; + } + i += 3; + continue; + } else if (parts[i + 1] === "2" && parts[i + 4] !== undefined) { + // RGB color: 38;2;R;G;B or 48;2;R;G;B + const colorCode = `${parts[i]};${parts[i + 1]};${parts[i + 2]};${parts[i + 3]};${parts[i + 4]}`; + if (code === 38) { + this.fgColor = colorCode; + } else { + this.bgColor = colorCode; + } + i += 5; + continue; + } + } + + // Standard SGR codes + switch (code) { + case 0: + this.reset(); + break; + case 1: + this.bold = true; + break; + case 2: + this.dim = true; + break; + case 3: + this.italic = true; + break; + case 4: + this.underline = true; + break; + case 5: + this.blink = true; + break; + case 7: + this.inverse = true; + break; + case 8: + this.hidden = true; + break; + case 9: + this.strikethrough = true; + break; + case 21: + this.bold = false; + break; // Some terminals + case 22: + this.bold = false; + this.dim = false; + break; + case 23: + this.italic = false; + break; + case 24: + this.underline = false; + break; + case 25: + this.blink = false; + break; + case 27: + this.inverse = false; + break; + case 28: + this.hidden = false; + break; + case 29: + this.strikethrough = false; + break; + case 39: + this.fgColor = null; + break; // Default fg + case 49: + this.bgColor = null; + break; // Default bg + default: + // Standard foreground colors 30-37, 90-97 + if ((code >= 30 && code <= 37) || (code >= 90 && code <= 97)) { + this.fgColor = String(code); + } + // Standard background colors 40-47, 100-107 + else if ((code >= 40 && code <= 47) || (code >= 100 && code <= 107)) { + this.bgColor = String(code); + } + break; + } + i++; + } + } + + private reset(): void { + this.bold = false; + this.dim = false; + this.italic = false; + this.underline = false; + this.blink = false; + this.inverse = false; + this.hidden = false; + this.strikethrough = false; + this.fgColor = null; + this.bgColor = null; + // SGR reset does not affect OSC 8 hyperlink state + } + + /** Clear all state for reuse. */ + clear(): void { + this.reset(); + this.activeHyperlink = null; + } + + getActiveCodes(): string { + const codes: string[] = []; + if (this.bold) codes.push("1"); + if (this.dim) codes.push("2"); + if (this.italic) codes.push("3"); + if (this.underline) codes.push("4"); + if (this.blink) codes.push("5"); + if (this.inverse) codes.push("7"); + if (this.hidden) codes.push("8"); + if (this.strikethrough) codes.push("9"); + if (this.fgColor) codes.push(this.fgColor); + if (this.bgColor) codes.push(this.bgColor); + + let result = codes.length > 0 ? `\x1b[${codes.join(";")}m` : ""; + if (this.activeHyperlink) { + result += formatOsc8Hyperlink(this.activeHyperlink); + } + return result; + } + + getActiveBackgroundCode(): string { + return this.bgColor ? `\x1b[${this.bgColor}m` : ""; + } + + hasActiveCodes(): boolean { + return ( + this.bold || + this.dim || + this.italic || + this.underline || + this.blink || + this.inverse || + this.hidden || + this.strikethrough || + this.fgColor !== null || + this.bgColor !== null || + this.activeHyperlink !== null + ); + } + + /** + * Get reset codes for attributes that need to be turned off at line end. + * Underline must be closed to prevent bleeding into padding. + * Active OSC 8 hyperlinks must be closed and re-opened on the next line. + * Returns empty string if no attributes need closing. + */ + getLineEndReset(): string { + let result = ""; + if (this.underline) { + result += "\x1b[24m"; // Underline off only + } + if (this.activeHyperlink) { + result += formatOsc8Close(this.activeHyperlink.terminator); // Re-opened at line start via getActiveCodes() + } + return result; + } +} + +function updateTrackerFromText(text: string, tracker: AnsiCodeTracker): void { + let i = 0; + while (i < text.length) { + const ansiResult = extractAnsiCode(text, i); + if (ansiResult) { + tracker.process(ansiResult.code); + i += ansiResult.length; + } else { + i++; + } + } +} + +/** Return only the background color active at the end of an ANSI-styled string. */ +export function getActiveBackgroundAnsi(text: string): string { + const tracker = new AnsiCodeTracker(); + updateTrackerFromText(text, tracker); + return tracker.getActiveBackgroundCode(); +} + +/** + * Split text into words while keeping ANSI codes attached. + */ +function splitIntoTokensWithAnsi(text: string): string[] { + const tokens: string[] = []; + let current = ""; + let pendingAnsi = ""; // ANSI codes waiting to be attached to next visible content + let currentKind: "space" | "word" | null = null; + let i = 0; + + const flushCurrent = (): void => { + if (!current) { + return; + } + tokens.push(current); + current = ""; + currentKind = null; + }; + + while (i < text.length) { + const ansiResult = extractAnsiCode(text, i); + if (ansiResult) { + // Hold ANSI codes separately - they'll be attached to the next visible char + pendingAnsi += ansiResult.code; + i += ansiResult.length; + continue; + } + + let end = i; + while (end < text.length && !extractAnsiCode(text, end)) { + end++; + } + + for (const { segment } of graphemeSegmenter.segment(text.slice(i, end))) { + const segmentIsSpace = segment === " "; + if (!segmentIsSpace && cjkBreakRegex.test(segment)) { + flushCurrent(); + const token = pendingAnsi + segment; + pendingAnsi = ""; + tokens.push(token); + continue; + } + + const segmentKind = segmentIsSpace ? "space" : "word"; + if (current && currentKind !== segmentKind) { + flushCurrent(); + } + + // Attach any pending ANSI codes to this visible character + if (pendingAnsi) { + current += pendingAnsi; + pendingAnsi = ""; + } + + currentKind = segmentKind; + current += segment; + } + + i = end; + } + + // Handle any remaining pending ANSI codes (attach to last token) + if (pendingAnsi) { + if (current) { + current += pendingAnsi; + } else if (tokens.length > 0) { + tokens[tokens.length - 1] += pendingAnsi; + } else { + current = pendingAnsi; + } + } + + if (current) { + tokens.push(current); + } + + return tokens; +} + +/** + * Wrap text with ANSI codes preserved. + * + * ONLY does word wrapping - NO padding, NO background colors. + * Returns lines where each line is <= width visible chars. + * Active ANSI codes are preserved across line breaks. + * + * @param text - Text to wrap (may contain ANSI codes and newlines) + * @param width - Maximum visible width per line + * @returns Array of wrapped lines (NOT padded to width) + */ +export function wrapTextWithAnsi(text: string, width: number): string[] { + if (!text) { + return [""]; + } + + // Handle newlines by processing each line separately + // Track ANSI state across lines so styles carry over after literal newlines + const inputLines = text.split(/\r\n|\r|\n/); + const result: string[] = []; + const tracker = new AnsiCodeTracker(); + + for (const inputLine of inputLines) { + // Prepend active ANSI codes from previous lines (except for first line) + const prefix = result.length > 0 ? tracker.getActiveCodes() : ""; + const wrappedLines = wrapSingleLine(prefix + inputLine, width); + for (const wrappedLine of wrappedLines) { + result.push(wrappedLine); + } + // Update tracker with codes from this line for next iteration + updateTrackerFromText(inputLine, tracker); + } + + return result.length > 0 ? result : [""]; +} + +function wrapSingleLine(line: string, width: number): string[] { + if (!line) { + return [""]; + } + + const visibleLength = visibleWidth(line); + if (visibleLength <= width) { + return [line]; + } + + const wrapped: string[] = []; + const tracker = new AnsiCodeTracker(); + const tokens = splitIntoTokensWithAnsi(line); + + let currentLine = ""; + let currentVisibleLength = 0; + + for (const token of tokens) { + const tokenVisibleLength = visibleWidth(token); + const isWhitespace = token.trim() === ""; + + // Token itself is too long - break it character by character + if (tokenVisibleLength > width && !isWhitespace) { + if (currentLine) { + // Add specific reset for underline only (preserves background) + const lineEndReset = tracker.getLineEndReset(); + if (lineEndReset) { + currentLine += lineEndReset; + } + wrapped.push(currentLine); + currentLine = ""; + currentVisibleLength = 0; + } + + // Break long token - breakLongWord handles its own resets + const broken = breakLongWord(token, width, tracker); + for (let i = 0; i < broken.length - 1; i++) { + wrapped.push(broken[i]!); + } + currentLine = broken[broken.length - 1]; + currentVisibleLength = visibleWidth(currentLine); + continue; + } + + // Check if adding this token would exceed width + const totalNeeded = currentVisibleLength + tokenVisibleLength; + + if (totalNeeded > width && currentVisibleLength > 0) { + // Trim trailing whitespace, then add underline reset (not full reset, to preserve background) + let lineToWrap = currentLine.trimEnd(); + const lineEndReset = tracker.getLineEndReset(); + if (lineEndReset) { + lineToWrap += lineEndReset; + } + wrapped.push(lineToWrap); + if (isWhitespace) { + // Don't start new line with whitespace + currentLine = tracker.getActiveCodes(); + currentVisibleLength = 0; + } else { + currentLine = tracker.getActiveCodes() + token; + currentVisibleLength = tokenVisibleLength; + } + } else { + // Add to current line + currentLine += token; + currentVisibleLength += tokenVisibleLength; + } + + updateTrackerFromText(token, tracker); + } + + if (currentLine) { + // No reset at end of final line - let caller handle it + wrapped.push(currentLine); + } + + // Trailing whitespace can cause lines to exceed the requested width + return wrapped.length > 0 ? wrapped.map((line) => line.trimEnd()) : [""]; +} + +export const PUNCTUATION_REGEX = /[(){}[\]<>.,;:'"!?+\-=*/\\|&%^$#@~`]/; + +/** + * Check if a character is whitespace. + */ +export function isWhitespaceChar(char: string): boolean { + return /\s/.test(char); +} + +/** + * Check if a character is punctuation. + */ +export function isPunctuationChar(char: string): boolean { + return PUNCTUATION_REGEX.test(char); +} + +function breakLongWord(word: string, width: number, tracker: AnsiCodeTracker): string[] { + const lines: string[] = []; + let currentLine = tracker.getActiveCodes(); + let currentWidth = 0; + + // First, separate ANSI codes from visible content + // We need to handle ANSI codes specially since they're not graphemes + let i = 0; + const segments: Array<{ type: "ansi" | "grapheme"; value: string }> = []; + + while (i < word.length) { + const ansiResult = extractAnsiCode(word, i); + if (ansiResult) { + segments.push({ type: "ansi", value: ansiResult.code }); + i += ansiResult.length; + } else { + // Find the next ANSI code or end of string + let end = i; + while (end < word.length) { + const nextAnsi = extractAnsiCode(word, end); + if (nextAnsi) break; + end++; + } + // Segment this non-ANSI portion into graphemes + const textPortion = word.slice(i, end); + for (const seg of graphemeSegmenter.segment(textPortion)) { + segments.push({ type: "grapheme", value: seg.segment }); + } + i = end; + } + } + + // Now process segments + for (const seg of segments) { + if (seg.type === "ansi") { + currentLine += seg.value; + tracker.process(seg.value); + continue; + } + + const grapheme = seg.value; + // Skip empty graphemes to avoid issues with string-width calculation + if (!grapheme) continue; + + const graphemeWidth = visibleWidth(grapheme); + + if (currentWidth + graphemeWidth > width) { + // Add specific reset for underline only (preserves background) + const lineEndReset = tracker.getLineEndReset(); + if (lineEndReset) { + currentLine += lineEndReset; + } + lines.push(currentLine); + currentLine = tracker.getActiveCodes(); + currentWidth = 0; + } + + currentLine += grapheme; + currentWidth += graphemeWidth; + } + + if (currentLine) { + // No reset at end of final segment - caller handles continuation + lines.push(currentLine); + } + + return lines.length > 0 ? lines : [""]; +} + +/** + * Apply background color to a line, padding to full width. + * + * @param line - Line of text (may contain ANSI codes) + * @param width - Total width to pad to + * @param bgFn - Background color function + * @returns Line with background applied and padded to width + */ +export function applyBackgroundToLine(line: string, width: number, bgFn: (text: string) => string): string { + // Calculate padding needed + const visibleLen = visibleWidth(line); + const paddingNeeded = Math.max(0, width - visibleLen); + const padding = " ".repeat(paddingNeeded); + + // Apply background to content + padding + const withPadding = line + padding; + return bgFn(withPadding); +} + +/** + * Truncate text to fit within a maximum visible width, adding ellipsis if needed. + * Optionally pad with spaces to reach exactly maxWidth. + * Properly handles ANSI escape codes (they don't count toward width). + * + * @param text - Text to truncate (may contain ANSI codes) + * @param maxWidth - Maximum visible width + * @param ellipsis - Ellipsis string to append when truncating (default: "...") + * @param pad - If true, pad result with spaces to exactly maxWidth (default: false) + * @returns Truncated text, optionally padded to exactly maxWidth + */ +export function truncateToWidth( + text: string, + maxWidth: number, + ellipsis: string = "...", + pad: boolean = false, +): string { + if (maxWidth <= 0) { + return ""; + } + + if (text.length === 0) { + return pad ? " ".repeat(maxWidth) : ""; + } + + const ellipsisWidth = visibleWidth(ellipsis); + if (ellipsisWidth >= maxWidth) { + const textWidth = visibleWidth(text); + if (textWidth <= maxWidth) { + return pad ? text + " ".repeat(maxWidth - textWidth) : text; + } + + const clippedEllipsis = truncateFragmentToWidth(ellipsis, maxWidth); + if (clippedEllipsis.width === 0) { + return pad ? " ".repeat(maxWidth) : ""; + } + return finalizeTruncatedResult("", 0, clippedEllipsis.text, clippedEllipsis.width, maxWidth, pad); + } + + if (isPrintableAscii(text)) { + if (text.length <= maxWidth) { + return pad ? text + " ".repeat(maxWidth - text.length) : text; + } + const targetWidth = maxWidth - ellipsisWidth; + return finalizeTruncatedResult(text.slice(0, targetWidth), targetWidth, ellipsis, ellipsisWidth, maxWidth, pad); + } + + const targetWidth = maxWidth - ellipsisWidth; + let result = ""; + let pendingAnsi = ""; + let visibleSoFar = 0; + let keptWidth = 0; + let keepContiguousPrefix = true; + let overflowed = false; + let exhaustedInput = false; + const hasAnsi = text.includes("\x1b"); + const hasTabs = text.includes("\t"); + + if (!hasAnsi && !hasTabs) { + for (const { segment } of graphemeSegmenter.segment(text)) { + const width = graphemeWidth(segment); + if (keepContiguousPrefix && keptWidth + width <= targetWidth) { + result += segment; + keptWidth += width; + } else { + keepContiguousPrefix = false; + } + visibleSoFar += width; + if (visibleSoFar > maxWidth) { + overflowed = true; + break; + } + } + exhaustedInput = !overflowed; + } else { + let i = 0; + while (i < text.length) { + const ansi = extractAnsiCode(text, i); + if (ansi) { + pendingAnsi += ansi.code; + i += ansi.length; + continue; + } + + if (text[i] === "\t") { + if (keepContiguousPrefix && keptWidth + 3 <= targetWidth) { + if (pendingAnsi) { + result += pendingAnsi; + pendingAnsi = ""; + } + result += "\t"; + keptWidth += 3; + } else { + keepContiguousPrefix = false; + pendingAnsi = ""; + } + visibleSoFar += 3; + if (visibleSoFar > maxWidth) { + overflowed = true; + break; + } + i++; + continue; + } + + let end = i; + while (end < text.length && text[end] !== "\t") { + const nextAnsi = extractAnsiCode(text, end); + if (nextAnsi) { + break; + } + end++; + } + + for (const { segment } of graphemeSegmenter.segment(text.slice(i, end))) { + const width = graphemeWidth(segment); + if (keepContiguousPrefix && keptWidth + width <= targetWidth) { + if (pendingAnsi) { + result += pendingAnsi; + pendingAnsi = ""; + } + result += segment; + keptWidth += width; + } else { + keepContiguousPrefix = false; + pendingAnsi = ""; + } + + visibleSoFar += width; + if (visibleSoFar > maxWidth) { + overflowed = true; + break; + } + } + if (overflowed) { + break; + } + i = end; + } + exhaustedInput = i >= text.length; + } + + if (!overflowed && exhaustedInput) { + return pad ? text + " ".repeat(Math.max(0, maxWidth - visibleSoFar)) : text; + } + + return finalizeTruncatedResult(result, keptWidth, ellipsis, ellipsisWidth, maxWidth, pad); +} + +/** + * Extract a range of visible columns from a line. Handles ANSI codes and wide chars. + * @param strict - If true, exclude wide chars at boundary that would extend past the range + */ +export function sliceByColumn(line: string, startCol: number, length: number, strict = false): string { + return sliceWithWidth(line, startCol, length, strict).text; +} + +/** Like sliceByColumn but also returns the actual visible width of the result. */ +export function sliceWithWidth( + line: string, + startCol: number, + length: number, + strict = false, +): { text: string; width: number } { + if (length <= 0) return { text: "", width: 0 }; + const endCol = startCol + length; + let result = "", + resultWidth = 0, + currentCol = 0, + i = 0, + pendingAnsi = ""; + + while (i < line.length) { + const ansi = extractAnsiCode(line, i); + if (ansi) { + if (currentCol >= startCol && currentCol < endCol) result += ansi.code; + else if (currentCol < startCol) pendingAnsi += ansi.code; + i += ansi.length; + continue; + } + + let textEnd = i; + while (textEnd < line.length && !extractAnsiCode(line, textEnd)) textEnd++; + + for (const { segment } of graphemeSegmenter.segment(line.slice(i, textEnd))) { + const w = graphemeWidth(segment); + const inRange = currentCol >= startCol && currentCol < endCol; + const fits = !strict || currentCol + w <= endCol; + if (inRange && fits) { + if (pendingAnsi) { + result += pendingAnsi; + pendingAnsi = ""; + } + result += segment; + resultWidth += w; + } + currentCol += w; + if (currentCol >= endCol) break; + } + i = textEnd; + if (currentCol >= endCol) break; + } + return { text: result, width: resultWidth }; +} + +// Pooled tracker instance for extractSegments (avoids allocation per call) +const pooledStyleTracker = new AnsiCodeTracker(); + +/** + * Extract "before" and "after" segments from a line in a single pass. + * Used for overlay compositing where we need content before and after the overlay region. + * Preserves styling from before the overlay that should affect content after it. + */ +export function extractSegments( + line: string, + beforeEnd: number, + afterStart: number, + afterLen: number, + strictAfter = false, +): { before: string; beforeWidth: number; after: string; afterWidth: number } { + let before = "", + beforeWidth = 0, + after = "", + afterWidth = 0; + let currentCol = 0, + i = 0; + let pendingAnsiBefore = ""; + let afterStarted = false; + const afterEnd = afterStart + afterLen; + + // Track styling state so "after" inherits styling from before the overlay + pooledStyleTracker.clear(); + + while (i < line.length) { + const ansi = extractAnsiCode(line, i); + if (ansi) { + // Track all SGR codes to know styling state at afterStart + pooledStyleTracker.process(ansi.code); + // Include ANSI codes in their respective segments + if (currentCol < beforeEnd) { + pendingAnsiBefore += ansi.code; + } else if (currentCol >= afterStart && currentCol < afterEnd && afterStarted) { + // Only include after we've started "after" (styling already prepended) + after += ansi.code; + } + i += ansi.length; + continue; + } + + let textEnd = i; + while (textEnd < line.length && !extractAnsiCode(line, textEnd)) textEnd++; + + for (const { segment } of graphemeSegmenter.segment(line.slice(i, textEnd))) { + const w = graphemeWidth(segment); + + if (currentCol < beforeEnd && currentCol + w <= beforeEnd) { + if (pendingAnsiBefore) { + before += pendingAnsiBefore; + pendingAnsiBefore = ""; + } + before += segment; + beforeWidth += w; + } else if (currentCol >= afterStart && currentCol < afterEnd) { + const fits = !strictAfter || currentCol + w <= afterEnd; + if (fits) { + // On first "after" grapheme, prepend inherited styling from before overlay + if (!afterStarted) { + after += pooledStyleTracker.getActiveCodes(); + afterStarted = true; + } + after += segment; + afterWidth += w; + } + } + + currentCol += w; + // Early exit: done with "before" only, or done with both segments + if (afterLen <= 0 ? currentCol >= beforeEnd : currentCol >= afterEnd) break; + } + i = textEnd; + if (afterLen <= 0 ? currentCol >= beforeEnd : currentCol >= afterEnd) break; + } + + return { before, beforeWidth, after, afterWidth }; +} diff --git a/packages/tui/src/word-navigation.ts b/packages/tui/src/word-navigation.ts new file mode 100644 index 0000000000000000000000000000000000000000..7c7eced2b6ac3ff5b49418824c3ac00a1f0204a3 --- /dev/null +++ b/packages/tui/src/word-navigation.ts @@ -0,0 +1,117 @@ +import { getWordSegmenter, isWhitespaceChar, PUNCTUATION_REGEX } from "./utils.ts"; + +const wordSegmenter = getWordSegmenter(); + +/** + * Options for word navigation functions. + * When omitted, uses the default Intl.Segmenter word segmentation. + */ +export interface WordNavigationOptions { + /** Custom segmenter returning word segments for the given text. */ + segment?: (text: string) => Iterable; + /** Predicate identifying atomic segments that should be treated as single units (e.g. paste markers). */ + isAtomicSegment?: (segment: string) => boolean; +} + +/** + * Find the cursor position after moving one word backward from `cursor` in `text`. + * Skips trailing whitespace, then stops at the next word/punctuation boundary. + * + * Pure function - does not mutate any state. + */ +export function findWordBackward(text: string, cursor: number, options?: WordNavigationOptions): number { + if (cursor <= 0) return 0; + + const textBeforeCursor = text.slice(0, cursor); + const segmentFn = options?.segment; + const isAtomic = options?.isAtomicSegment; + const segments = segmentFn ? [...segmentFn(textBeforeCursor)] : [...wordSegmenter.segment(textBeforeCursor)]; + let newCursor = cursor; + + // Skip trailing whitespace + while ( + segments.length > 0 && + !isAtomic?.(segments[segments.length - 1]?.segment || "") && + isWhitespaceChar(segments[segments.length - 1]?.segment || "") + ) { + newCursor -= segments.pop()?.segment.length || 0; + } + + if (segments.length === 0) return newCursor; + + const last = segments[segments.length - 1]!; + + if (isAtomic?.(last.segment)) { + // Skip one atomic segment. + newCursor -= last.segment.length; + } else if (last.isWordLike) { + // Skip inside one word-like segment, preserving ASCII punctuation boundaries. + const segment = last.segment; + const matches = [...segment.matchAll(new RegExp(PUNCTUATION_REGEX, "g"))]; + if (matches.length <= 0) { + newCursor -= segment.length; + } else { + const lastMatch = matches[matches.length - 1]!; + newCursor -= segment.length - (lastMatch.index + lastMatch[0].length); + } + } else { + // Skip non-word non-whitespace run (punctuation) + while ( + segments.length > 0 && + !isAtomic?.(segments[segments.length - 1]?.segment || "") && + !segments[segments.length - 1]?.isWordLike && + !isWhitespaceChar(segments[segments.length - 1]?.segment || "") + ) { + newCursor -= segments.pop()?.segment.length || 0; + } + } + + return newCursor; +} + +/** + * Find the cursor position after moving one word forward from `cursor` in `text`. + * Skips leading whitespace, then stops at the next word/punctuation boundary. + * + * Pure function - does not mutate any state. + */ +export function findWordForward(text: string, cursor: number, options?: WordNavigationOptions): number { + if (cursor >= text.length) return text.length; + + const textAfterCursor = text.slice(cursor); + const segmentFn = options?.segment; + const isAtomic = options?.isAtomicSegment; + const segments = segmentFn ? segmentFn(textAfterCursor) : wordSegmenter.segment(textAfterCursor); + const iterator = segments[Symbol.iterator](); + let next = iterator.next(); + let newCursor = cursor; + + // Skip leading whitespace + while (!next.done && !isAtomic?.(next.value.segment) && isWhitespaceChar(next.value.segment)) { + newCursor += next.value.segment.length; + next = iterator.next(); + } + + if (next.done) return newCursor; + + if (isAtomic?.(next.value.segment)) { + // Skip one atomic segment. + newCursor += next.value.segment.length; + } else if (next.value.isWordLike) { + // Skip inside one word-like segment, preserving ASCII punctuation boundaries. + newCursor += PUNCTUATION_REGEX.exec(next.value.segment)?.index ?? next.value.segment.length; + } else { + // Skip non-word non-whitespace run (punctuation) + while ( + !next.done && + !isAtomic?.(next.value.segment) && + !next.value.isWordLike && + !isWhitespaceChar(next.value.segment) + ) { + newCursor += next.value.segment.length; + next = iterator.next(); + } + } + + return newCursor; +} diff --git a/packages/tui/test/alt-screen-large-transcript-bench.ts b/packages/tui/test/alt-screen-large-transcript-bench.ts new file mode 100644 index 0000000000000000000000000000000000000000..03df87543d9f4b74fd8b2410f831aa3279b7ef93 --- /dev/null +++ b/packages/tui/test/alt-screen-large-transcript-bench.ts @@ -0,0 +1,166 @@ +/** + * Alternate-screen large-transcript benchmark. + * + * Mirrors the fullscreen transcript shape used by the coding agent: + * + * ScrollView + * └── document Container + * ├── header Container + * ├── resources Container + * └── chat Container + * └── 2,500 alternating user/assistant Markdown components + * + * Measures warm steady frames, one-line scrolling, streaming updates to the + * final assistant message, and width changes that invalidate Markdown caches. + * + * Run from the repository root: + * node --experimental-strip-types packages/tui/test/alt-screen-large-transcript-bench.ts + */ + +import { performance } from "node:perf_hooks"; +import { Markdown, type MarkdownTheme } from "../src/components/markdown.ts"; +import { ScrollView } from "../src/components/scroll-view.ts"; +import { Text } from "../src/components/text.ts"; +import { VStack } from "../src/components/v-stack.ts"; +import type { Terminal } from "../src/terminal.ts"; +import { Container } from "../src/tui.ts"; +import { TuiAltScreen } from "../src/tui-alt-screen.ts"; + +const COLUMNS = 80; +const ROWS = 50; +const COMPONENT_COUNT = 2_500; +const WARMUP_FRAMES = 20; + +const theme: MarkdownTheme = { + heading: (text) => text, + link: (text) => text, + linkUrl: (text) => text, + code: (text) => text, + codeBlock: (text) => text, + codeBlockBorder: (text) => text, + quote: (text) => text, + quoteBorder: (text) => text, + hr: (text) => text, + listBullet: (text) => text, + bold: (text) => text, + italic: (text) => text, + strikethrough: (text) => text, + underline: (text) => text, +}; + +class NullTerminal implements Terminal { + columns = COLUMNS; + rows = ROWS; + + start(_onInput: (data: string) => void, _onResize: () => void): void {} + stop(): void {} + async drainInput(): Promise {} + write(_data: string): void {} + get kittyProtocolActive(): boolean { + return false; + } + moveBy(_lines: number): void {} + hideCursor(): void {} + showCursor(): void {} + clearLine(): void {} + clearFromCursor(): void {} + clearScreen(): void {} + setTitle(_title: string): void {} + setProgress(_active: boolean): void {} +} + +const userMarkdown = [ + "## User request", + "", + "Please inspect this implementation carefully and report the relevant behavior.", + "", + "- preserve correctness", + "- include concrete evidence", +].join("\n"); + +const assistantMarkdown = [ + "## Analysis", + "", + "This response contains representative prose with **inline styles**, a [link](https://example.com), and enough words to wrap across terminal rows at realistic widths. The implementation should preserve all observable behavior while avoiding unnecessary allocation.", + "", + "1. First item with explanatory text and a concrete example.", + "2. Second item with more explanatory text and another example.", + "3. Third item that ensures list rendering is represented.", + "", + "```ts", + "function render(width: number): string[] {", + " const lines: string[] = [];", + " for (const child of children) {", + " const childLines = child.render(width);", + " for (const line of childLines) lines.push(line);", + " }", + " const height = lines.length;", + " const start = Math.max(0, from);", + " const end = Math.min(height, start + count);", + " const visible = lines.slice(start, end);", + " updateLayout(height, viewportHeight);", + " return visible;", + "}", + "```", + "", + "> A quoted conclusion with enough text to exercise wrapping and inline rendering in the real Markdown component.", + "", + "Final paragraph with a concise recommendation and a note about correctness tests.", +].join("\n"); + +const header = new Container(); +header.addChild(new Text("pi benchmark\nheader line", 0, 0)); +const resources = new Container(); +resources.addChild(new Text("resource", 0, 0)); +const chat = new Container(); +let streamingMarkdown: Markdown | undefined; +for (let index = 0; index < COMPONENT_COUNT; index++) { + const markdown = new Markdown(index % 2 === 0 ? userMarkdown : assistantMarkdown, 1, 0, theme); + chat.addChild(markdown); + if (index === COMPONENT_COUNT - 1) streamingMarkdown = markdown; +} +if (!streamingMarkdown) throw new Error("Benchmark requires at least one assistant Markdown component"); + +const document = new Container(); +document.addChild(header); +document.addChild(resources); +document.addChild(chat); + +const transcript = new ScrollView(document, { follow: "none", primary: true }); +const dock = new VStack([new Text("editor\nfooter", 0, 0)]); +const root = new VStack([ + { component: transcript, basis: 0, grow: 1, minSize: 1 }, + { component: dock, basis: "auto", minSize: 1 }, +]); +const terminal = new NullTerminal(); +const tui = new TuiAltScreen(terminal, false, "/tmp/pi-tui-bench"); +tui.setLayoutRoot(root); +tui.start(); +tui.renderNow(); + +const totalLines = document.render(terminal.columns).length; +transcript.scrollTo(Math.floor(totalLines / 2)); +for (let frame = 0; frame < WARMUP_FRAMES; frame++) tui.renderNow(); + +function measure(name: string, frames: number, beforeFrame: (frame: number) => void): void { + const start = performance.now(); + for (let frame = 0; frame < frames; frame++) { + beforeFrame(frame); + tui.renderNow(); + } + const millisecondsPerFrame = (performance.now() - start) / frames; + console.log(`${name}: ${millisecondsPerFrame.toFixed(3)} ms/frame (${frames} frames)`); +} + +console.log(`components=${COMPONENT_COUNT} lines=${totalLines} viewport=${terminal.columns}x${terminal.rows}`); +measure("steady", 200, () => {}); +transcript.scrollTo(Math.floor(totalLines / 2)); +measure("scroll", 200, () => transcript.scrollBy(1)); +measure("streaming", 100, (frame) => { + streamingMarkdown.setText(`${assistantMarkdown}\n\nstream ${"x".repeat(frame + 1)}`); +}); +measure("resize", 10, (frame) => { + terminal.columns = frame % 2 === 0 ? COLUMNS - 1 : COLUMNS; +}); + +tui.stop(); diff --git a/packages/tui/test/autocomplete.test.ts b/packages/tui/test/autocomplete.test.ts new file mode 100644 index 0000000000000000000000000000000000000000..db9ac67a11879a096ef7cd20078ffaec0e783d8c --- /dev/null +++ b/packages/tui/test/autocomplete.test.ts @@ -0,0 +1,578 @@ +import assert from "node:assert"; +import { spawnSync } from "node:child_process"; +import { mkdirSync, mkdtempSync, rmSync, symlinkSync, writeFileSync } from "node:fs"; +import { tmpdir } from "node:os"; +import { dirname, join } from "node:path"; +import { afterEach, beforeEach, describe, it, test } from "node:test"; +import { CombinedAutocompleteProvider } from "../src/autocomplete.ts"; + +const resolveFdPath = (): string | null => { + const command = process.platform === "win32" ? "where" : "which"; + const result = spawnSync(command, ["fd"], { encoding: "utf-8" }); + if (result.status !== 0 || !result.stdout) { + return null; + } + + const firstLine = result.stdout.split(/\r?\n/).find(Boolean); + return firstLine ? firstLine.trim() : null; +}; + +type FolderStructure = { + dirs?: string[]; + files?: Record; +}; + +const setupFolder = (baseDir: string, structure: FolderStructure = {}): void => { + const dirs = structure.dirs ?? []; + const files = structure.files ?? {}; + + dirs.forEach((dir) => { + mkdirSync(join(baseDir, dir), { recursive: true }); + }); + Object.entries(files).forEach(([filePath, contents]) => { + const fullPath = join(baseDir, filePath); + mkdirSync(dirname(fullPath), { recursive: true }); + writeFileSync(fullPath, contents); + }); +}; + +const fdPath = resolveFdPath(); +const isFdInstalled = Boolean(fdPath); + +const requireFdPath = (): string => { + if (!fdPath) { + throw new Error("fd is not available"); + } + return fdPath; +}; + +const getSuggestions = ( + provider: CombinedAutocompleteProvider, + lines: string[], + cursorLine: number, + cursorCol: number, + force: boolean = false, +) => provider.getSuggestions(lines, cursorLine, cursorCol, { signal: new AbortController().signal, force }); + +describe("CombinedAutocompleteProvider", () => { + describe("extractPathPrefix", () => { + it("extracts / from 'hey /' when forced", async () => { + const provider = new CombinedAutocompleteProvider([], "/tmp"); + const lines = ["hey /"]; + const cursorLine = 0; + const cursorCol = 5; // After the "/" + + const result = await getSuggestions(provider, lines, cursorLine, cursorCol, true); + + assert.notEqual(result, null, "Should return suggestions for root directory"); + if (result) { + assert.strictEqual(result.prefix, "/", "Prefix should be '/'"); + } + }); + + it("extracts /A from '/A' when forced", async () => { + const provider = new CombinedAutocompleteProvider([], "/tmp"); + const lines = ["/A"]; + const cursorLine = 0; + const cursorCol = 2; // After the "A" + + const result = await getSuggestions(provider, lines, cursorLine, cursorCol, true); + + console.log("Result:", result); + // This might return null if /A doesn't match anything, which is fine + // We're mainly testing that the prefix extraction works + if (result) { + assert.strictEqual(result.prefix, "/A", "Prefix should be '/A'"); + } + }); + + it("does not trigger for slash commands", async () => { + const provider = new CombinedAutocompleteProvider([], "/tmp"); + const lines = ["/model"]; + const cursorLine = 0; + const cursorCol = 6; // After "model" + + const result = await getSuggestions(provider, lines, cursorLine, cursorCol, true); + + console.log("Result:", result); + assert.strictEqual(result, null, "Should not trigger for slash commands"); + }); + + it("triggers for absolute paths after slash command argument", async () => { + const provider = new CombinedAutocompleteProvider([], "/tmp"); + const lines = ["/command /"]; + const cursorLine = 0; + const cursorCol = 10; // After the second "/" + + const result = await getSuggestions(provider, lines, cursorLine, cursorCol, true); + + console.log("Result:", result); + assert.notEqual(result, null, "Should trigger for absolute paths in command arguments"); + if (result) { + assert.strictEqual(result.prefix, "/", "Prefix should be '/'"); + } + }); + }); + + describe("fd @ file suggestions", { skip: !isFdInstalled }, () => { + let rootDir = ""; + let baseDir = ""; + let outsideDir = ""; + + beforeEach(() => { + rootDir = mkdtempSync(join(tmpdir(), "pi-autocomplete-root-")); + baseDir = join(rootDir, "cwd"); + outsideDir = join(rootDir, "outside"); + mkdirSync(baseDir, { recursive: true }); + mkdirSync(outsideDir, { recursive: true }); + }); + + afterEach(() => { + rmSync(rootDir, { recursive: true, force: true }); + }); + + test("returns all files and folders for empty @ query", async () => { + setupFolder(baseDir, { + dirs: ["src"], + files: { + "README.md": "readme", + }, + }); + + const provider = new CombinedAutocompleteProvider([], baseDir, requireFdPath()); + const line = "@"; + const result = await getSuggestions(provider, [line], 0, line.length); + + const values = result?.items.map((item) => item.value).sort(); + assert.deepStrictEqual(values, ["@README.md", "@src/"].sort()); + }); + + test("matches file with extension in query", async () => { + setupFolder(baseDir, { + files: { + "file.txt": "content", + }, + }); + + const provider = new CombinedAutocompleteProvider([], baseDir, requireFdPath()); + const line = "@file.txt"; + const result = await getSuggestions(provider, [line], 0, line.length); + + const values = result?.items.map((item) => item.value); + assert.ok(values?.includes("@file.txt")); + }); + + test("filters are case insensitive", async () => { + setupFolder(baseDir, { + dirs: ["src"], + files: { + "README.md": "readme", + }, + }); + + const provider = new CombinedAutocompleteProvider([], baseDir, requireFdPath()); + const line = "@re"; + const result = await getSuggestions(provider, [line], 0, line.length); + + const values = result?.items.map((item) => item.value).sort(); + assert.deepStrictEqual(values, ["@README.md"]); + }); + + test("ranks directories before files", async () => { + setupFolder(baseDir, { + dirs: ["src"], + files: { + "src.txt": "text", + }, + }); + + const provider = new CombinedAutocompleteProvider([], baseDir, requireFdPath()); + const line = "@src"; + const result = await getSuggestions(provider, [line], 0, line.length); + + const firstValue = result?.items[0]?.value; + const hasSrcFile = result?.items?.some((item) => item.value === "@src.txt"); + assert.strictEqual(firstValue, "@src/"); + assert.ok(hasSrcFile); + }); + + test("returns nested file paths", async () => { + setupFolder(baseDir, { + files: { + "src/index.ts": "export {};\n", + }, + }); + + const provider = new CombinedAutocompleteProvider([], baseDir, requireFdPath()); + const line = "@index"; + const result = await getSuggestions(provider, [line], 0, line.length); + + const values = result?.items.map((item) => item.value); + assert.ok(values?.includes("@src/index.ts")); + }); + + test("matches deeply nested paths", async () => { + setupFolder(baseDir, { + files: { + "packages/tui/src/autocomplete.ts": "export {};", + "packages/ai/src/autocomplete.ts": "export {};", + }, + }); + + const provider = new CombinedAutocompleteProvider([], baseDir, requireFdPath()); + const line = "@tui/src/auto"; + const result = await getSuggestions(provider, [line], 0, line.length); + + const values = result?.items.map((item) => item.value); + assert.ok(values?.includes("@packages/tui/src/autocomplete.ts")); + assert.ok(!values?.includes("@packages/ai/src/autocomplete.ts")); + }); + + test("matches directory in middle of path with --full-path", async () => { + setupFolder(baseDir, { + files: { + "src/components/Button.tsx": "export {};", + "src/utils/helpers.ts": "export {};", + }, + }); + + const provider = new CombinedAutocompleteProvider([], baseDir, requireFdPath()); + const line = "@components/"; + const result = await getSuggestions(provider, [line], 0, line.length); + + const values = result?.items.map((item) => item.value); + assert.ok(values?.includes("@src/components/Button.tsx")); + assert.ok(!values?.includes("@src/utils/helpers.ts")); + }); + + test("scopes fuzzy search to relative directories and searches recursively", async () => { + setupFolder(outsideDir, { + files: { + "nested/alpha.ts": "export {};", + "nested/deeper/also-alpha.ts": "export {};", + "nested/deeper/zzz.ts": "export {};", + }, + }); + + const provider = new CombinedAutocompleteProvider([], baseDir, requireFdPath()); + const line = "@../outside/a"; + const result = await getSuggestions(provider, [line], 0, line.length); + + const values = result?.items.map((item) => item.value); + assert.ok(values?.includes("@../outside/nested/alpha.ts")); + assert.ok(values?.includes("@../outside/nested/deeper/also-alpha.ts")); + assert.ok(!values?.includes("@../outside/nested/deeper/zzz.ts")); + }); + + test("ranks shallower same-score @ matches before deeper matches", async () => { + setupFolder(baseDir, { + dirs: ["scope/aaa/venv/lib/python3.12/site-packages/pkg/core/profile", "scope/projects"], + }); + + const provider = new CombinedAutocompleteProvider([], baseDir, requireFdPath()); + const line = "@scope/pro"; + const result = await getSuggestions(provider, [line], 0, line.length); + + const values = result?.items.map((item) => item.value) ?? []; + assert.strictEqual(values[0], "@scope/projects/"); + assert.ok(values.includes("@scope/aaa/venv/lib/python3.12/site-packages/pkg/core/profile/")); + }); + + test("includes scoped direct children when recursive @ matches are flooded", async () => { + const floodedDirs = Array.from( + { length: 250 }, + (_, index) => + `scope/a${String(index + 1).padStart(3, "0")}/venv/lib/python3.12/site-packages/pkg/core/profile`, + ); + setupFolder(baseDir, { + dirs: ["scope/projects", ...floodedDirs], + }); + + const provider = new CombinedAutocompleteProvider([], baseDir, requireFdPath()); + const line = "@scope/pro"; + const result = await getSuggestions(provider, [line], 0, line.length); + + const values = result?.items.map((item) => item.value) ?? []; + assert.strictEqual(values[0], "@scope/projects/"); + assert.ok( + values.some((value) => value.includes("/profile/")), + "Should keep deep fuzzy matches after direct children", + ); + }); + + test("quotes paths with spaces for @ suggestions", async () => { + setupFolder(baseDir, { + dirs: ["my folder"], + files: { + "my folder/test.txt": "content", + }, + }); + + const provider = new CombinedAutocompleteProvider([], baseDir, requireFdPath()); + const line = "@my"; + const result = await getSuggestions(provider, [line], 0, line.length); + + const values = result?.items.map((item) => item.value); + assert.ok(values?.includes('@"my folder/"')); + }); + + test("includes hidden paths but excludes .git", async () => { + setupFolder(baseDir, { + dirs: [".pi", ".github", ".git"], + files: { + ".pi/config.json": "{}", + ".github/workflows/ci.yml": "name: ci", + ".git/config": "[core]", + }, + }); + + const provider = new CombinedAutocompleteProvider([], baseDir, requireFdPath()); + const line = "@"; + const result = await getSuggestions(provider, [line], 0, line.length); + + const values = result?.items.map((item) => item.value) ?? []; + assert.ok(values.includes("@.pi/")); + assert.ok(values.includes("@.github/")); + assert.ok(!values.some((value) => value === "@.git" || value.startsWith("@.git/"))); + }); + + test("follows symlinked directories for fuzzy @ search", async () => { + setupFolder(baseDir, { + files: { + "dir/some_file.txt": "real", + }, + }); + setupFolder(outsideDir, { + files: { + "some_file.txt": "symlinked", + }, + }); + symlinkSync("../outside", join(baseDir, "symlinked_dir")); + + const provider = new CombinedAutocompleteProvider([], baseDir, requireFdPath()); + const line = "@some"; + const result = await getSuggestions(provider, [line], 0, line.length); + + const values = result?.items.map((item) => item.value) ?? []; + assert.ok(values.includes("@dir/some_file.txt")); + assert.ok(values.includes("@symlinked_dir/some_file.txt")); + }); + + test("returns symlinked directories when matching their name", async () => { + setupFolder(outsideDir, { + files: { + "nested/file.txt": "symlinked", + }, + }); + symlinkSync("../outside", join(baseDir, "symlinked_dir")); + + const provider = new CombinedAutocompleteProvider([], baseDir, requireFdPath()); + const line = "@symlinked"; + const result = await getSuggestions(provider, [line], 0, line.length); + + const values = result?.items.map((item) => item.value) ?? []; + assert.ok(values.includes("@symlinked_dir/")); + }); + + test("returns symlinked files without requiring type l", async () => { + setupFolder(baseDir, { + files: { + "original.txt": "content", + }, + }); + const linkPath = join(baseDir, "link.txt"); + symlinkSync("original.txt", linkPath); + + const provider = new CombinedAutocompleteProvider([], baseDir, requireFdPath()); + const line = "@link"; + const result = await getSuggestions(provider, [line], 0, line.length); + + const values = result?.items.map((item) => item.value) ?? []; + assert.ok(values.includes("@link.txt")); + }); + + test("returns the same @ suggestions when the cwd path contains the query", async () => { + const normalBaseDir = join(rootDir, "cwd-normal"); + const queryInPathBaseDir = join(rootDir, "cwd-plan-repro"); + mkdirSync(normalBaseDir, { recursive: true }); + mkdirSync(queryInPathBaseDir, { recursive: true }); + + const structure = { + dirs: ["packages/coding-agent/examples/extensions/plan-mode"], + files: { + "packages/coding-agent/examples/extensions/plan-mode/README.md": "readme", + "packages/tui/docs/plan.md": "plan", + }, + }; + setupFolder(normalBaseDir, structure); + setupFolder(queryInPathBaseDir, structure); + + const query = "@plan"; + const normalProvider = new CombinedAutocompleteProvider([], normalBaseDir, requireFdPath()); + const queryInPathProvider = new CombinedAutocompleteProvider([], queryInPathBaseDir, requireFdPath()); + + const normalResult = await getSuggestions(normalProvider, [query], 0, query.length); + const queryInPathResult = await getSuggestions(queryInPathProvider, [query], 0, query.length); + + const normalize = (result: Awaited>) => + (result?.items ?? []).map((item) => `${item.label} :: ${item.description ?? ""}`).sort(); + + assert.deepStrictEqual(normalize(queryInPathResult), normalize(normalResult)); + assert.ok( + normalize(normalResult).includes("plan-mode/ :: packages/coding-agent/examples/extensions/plan-mode"), + ); + assert.ok(normalize(normalResult).includes("plan.md :: packages/tui/docs/plan.md")); + }); + + test("continues autocomplete inside quoted @ paths", async () => { + setupFolder(baseDir, { + files: { + "my folder/test.txt": "content", + "my folder/other.txt": "content", + }, + }); + + const provider = new CombinedAutocompleteProvider([], baseDir, requireFdPath()); + const line = '@"my folder/"'; + const result = await getSuggestions(provider, [line], 0, line.length - 1); + + assert.notEqual(result, null, "Should return suggestions for quoted folder path"); + const values = result?.items.map((item) => item.value); + assert.ok(values?.includes('@"my folder/test.txt"')); + assert.ok(values?.includes('@"my folder/other.txt"')); + }); + + test("applies quoted @ completion without duplicating closing quote", async () => { + setupFolder(baseDir, { + files: { + "my folder/test.txt": "content", + }, + }); + + const provider = new CombinedAutocompleteProvider([], baseDir, requireFdPath()); + const line = '@"my folder/te"'; + const cursorCol = line.length - 1; + const result = await getSuggestions(provider, [line], 0, cursorCol); + + assert.notEqual(result, null, "Should return suggestions for quoted @ path"); + const item = result?.items.find((entry) => entry.value === '@"my folder/test.txt"'); + assert.ok(item, "Should find test.txt suggestion"); + + const applied = provider.applyCompletion([line], 0, cursorCol, item!, result!.prefix); + assert.strictEqual(applied.lines[0], '@"my folder/test.txt" '); + }); + }); + + describe("dot-slash path completion", () => { + let baseDir = ""; + + beforeEach(() => { + baseDir = mkdtempSync(join(tmpdir(), "pi-autocomplete-")); + }); + + afterEach(() => { + rmSync(baseDir, { recursive: true, force: true }); + }); + + test("preserves ./ prefix when completing paths", async () => { + setupFolder(baseDir, { + files: { + "update.sh": "#!/bin/bash", + "utils.ts": "export {};", + }, + }); + + const provider = new CombinedAutocompleteProvider([], baseDir); + const line = "./up"; + const result = await getSuggestions(provider, [line], 0, line.length, true); + + assert.notEqual(result, null, "Should return suggestions for ./ path"); + const values = result?.items.map((item) => item.value); + assert.ok(values?.includes("./update.sh"), `Expected ./update.sh in ${JSON.stringify(values)}`); + }); + + test("preserves ./ prefix for directory completions", async () => { + setupFolder(baseDir, { + dirs: ["src"], + files: { + "src/index.ts": "export {};", + }, + }); + + const provider = new CombinedAutocompleteProvider([], baseDir); + const line = "./sr"; + const result = await getSuggestions(provider, [line], 0, line.length, true); + + assert.notEqual(result, null, "Should return suggestions for ./ directory path"); + const values = result?.items.map((item) => item.value); + assert.ok(values?.includes("./src/"), `Expected ./src/ in ${JSON.stringify(values)}`); + }); + }); + + describe("quoted path completion", () => { + let baseDir = ""; + + beforeEach(() => { + baseDir = mkdtempSync(join(tmpdir(), "pi-autocomplete-")); + }); + + afterEach(() => { + rmSync(baseDir, { recursive: true, force: true }); + }); + + test("quotes paths with spaces for direct completion", async () => { + setupFolder(baseDir, { + dirs: ["my folder"], + files: { + "my folder/test.txt": "content", + }, + }); + + const provider = new CombinedAutocompleteProvider([], baseDir); + const line = "my"; + const result = await getSuggestions(provider, [line], 0, line.length, true); + + assert.notEqual(result, null, "Should return suggestions for path completion"); + const values = result?.items.map((item) => item.value); + assert.ok(values?.includes('"my folder/"')); + }); + + test("continues completion inside quoted paths", async () => { + setupFolder(baseDir, { + files: { + "my folder/test.txt": "content", + "my folder/other.txt": "content", + }, + }); + + const provider = new CombinedAutocompleteProvider([], baseDir); + const line = '"my folder/"'; + const result = await getSuggestions(provider, [line], 0, line.length - 1, true); + + assert.notEqual(result, null, "Should return suggestions for quoted folder path"); + const values = result?.items.map((item) => item.value); + assert.ok(values?.includes('"my folder/test.txt"')); + assert.ok(values?.includes('"my folder/other.txt"')); + }); + + test("applies quoted completion without duplicating closing quote", async () => { + setupFolder(baseDir, { + files: { + "my folder/test.txt": "content", + }, + }); + + const provider = new CombinedAutocompleteProvider([], baseDir); + const line = '"my folder/te"'; + const cursorCol = line.length - 1; + const result = await getSuggestions(provider, [line], 0, cursorCol, true); + + assert.notEqual(result, null, "Should return suggestions for quoted path"); + const item = result?.items.find((entry) => entry.value === '"my folder/test.txt"'); + assert.ok(item, "Should find test.txt suggestion"); + + const applied = provider.applyCompletion([line], 0, cursorCol, item!, result!.prefix); + assert.strictEqual(applied.lines[0], '"my folder/test.txt"'); + }); + }); +}); diff --git a/packages/tui/test/bug-regression-isimageline-startswith-bug.test.ts b/packages/tui/test/bug-regression-isimageline-startswith-bug.test.ts new file mode 100644 index 0000000000000000000000000000000000000000..b3d25e835ff06b721b92978ff48e28289e879b63 --- /dev/null +++ b/packages/tui/test/bug-regression-isimageline-startswith-bug.test.ts @@ -0,0 +1,237 @@ +/** + * Bug regression test for isImageLine() crash scenario + * + * Bug: When isImageLine() used startsWith() and terminal doesn't support images, + * it would return false for lines containing image escape sequences, causing TUI to + * crash with "Rendered line exceeds terminal width" error. + * + * Fix: Changed to use includes() to detect escape sequences anywhere in the line. + * + * This test demonstrates: + * 1. The bug scenario with the old implementation + * 2. That the fix works correctly + */ + +import assert from "node:assert"; +import { describe, it } from "node:test"; + +describe("Bug regression: isImageLine() crash with image escape sequences", () => { + describe("Bug scenario: Terminal without image support", () => { + it("old implementation would return false, causing crash", () => { + /** + * OLD IMPLEMENTATION (buggy): + * ```typescript + * export function isImageLine(line: string): boolean { + * const prefix = getImageEscapePrefix(); + * return prefix !== null && line.startsWith(prefix); + * } + * ``` + * + * When terminal doesn't support images: + * - getImageEscapePrefix() returns null + * - isImageLine() returns false even for lines containing image sequences + * - TUI performs width check on line containing 300KB+ of base64 data + * - Crash: "Rendered line exceeds terminal width (304401 > 115)" + */ + + // Simulate old implementation behavior + const oldIsImageLine = (line: string, imageEscapePrefix: string | null): boolean => { + return imageEscapePrefix !== null && line.startsWith(imageEscapePrefix); + }; + + // When terminal doesn't support images, prefix is null + const terminalWithoutImageSupport = null; + + // Line containing image escape sequence with text before it (common bug scenario) + const lineWithImageSequence = + "Read image file [image/jpeg]\x1b]1337;File=size=800,600;inline=1:base64data...\x07"; + + // Old implementation would return false (BUG!) + const oldResult = oldIsImageLine(lineWithImageSequence, terminalWithoutImageSupport); + assert.strictEqual( + oldResult, + false, + "Bug: old implementation returns false for line containing image sequence when terminal has no image support", + ); + }); + + it("new implementation returns true correctly", async () => { + const { isImageLine } = await import("../src/terminal-image.ts"); + + // Line containing image escape sequence with text before it + const lineWithImageSequence = + "Read image file [image/jpeg]\x1b]1337;File=size=800,600;inline=1:base64data...\x07"; + + // New implementation should return true (FIX!) + const newResult = isImageLine(lineWithImageSequence); + assert.strictEqual(newResult, true, "Fix: new implementation returns true for line containing image sequence"); + }); + + it("new implementation detects Kitty sequences in any position", async () => { + const { isImageLine } = await import("../src/terminal-image.ts"); + + const scenarios = [ + "At start: \x1b_Ga=T,f=100,data...\x1b\\", + "Prefix \x1b_Ga=T,data...\x1b\\", + "Suffix text \x1b_Ga=T,data...\x1b\\ suffix", + "Middle \x1b_Ga=T,data...\x1b\\ more text", + // Very long line (simulating 300KB+ crash scenario) + `Text before \x1b_Ga=T,f=100${"A".repeat(300000)} text after`, + ]; + + for (const line of scenarios) { + assert.strictEqual(isImageLine(line), true, `Should detect Kitty sequence in: ${line.slice(0, 50)}...`); + } + }); + + it("new implementation detects iTerm2 sequences in any position", async () => { + const { isImageLine } = await import("../src/terminal-image.ts"); + + const scenarios = [ + "At start: \x1b]1337;File=size=100,100:base64...\x07", + "Prefix \x1b]1337;File=inline=1:data==\x07", + "Suffix text \x1b]1337;File=inline=1:data==\x07 suffix", + "Middle \x1b]1337;File=inline=1:data==\x07 more text", + // Very long line (simulating 304KB crash scenario) + `Text before \x1b]1337;File=size=800,600;inline=1:${"B".repeat(300000)} text after`, + ]; + + for (const line of scenarios) { + assert.strictEqual(isImageLine(line), true, `Should detect iTerm2 sequence in: ${line.slice(0, 50)}...`); + } + }); + }); + + describe("Integration: Tool execution scenario", () => { + /** + * This simulates what happens when the `read` tool reads an image file. + * The tool result contains both text and image content: + * + * ```typescript + * { + * content: [ + * { type: "text", text: "Read image file [image/jpeg]\n800x600" }, + * { type: "image", data: "base64...", mimeType: "image/jpeg" } + * ] + * } + * ``` + * + * When this is rendered, the image component creates escape sequences. + * If isImageLine() doesn't detect them, TUI crashes. + */ + + it("detects image sequences in read tool output", async () => { + const { isImageLine } = await import("../src/terminal-image.ts"); + + // Simulate output when read tool processes an image + // The line might have text from the read result plus the image escape sequence + const toolOutputLine = "Read image file [image/jpeg]\x1b]1337;File=size=800,600;inline=1:base64image...\x07"; + + assert.strictEqual(isImageLine(toolOutputLine), true, "Should detect image sequence in tool output line"); + }); + + it("detects Kitty sequences from Image component", async () => { + const { isImageLine } = await import("../src/terminal-image.ts"); + + // Kitty image component creates multi-line output with escape sequences + const kittyLine = "\x1b_Ga=T,f=100,t=f,d=base64data...\x1b\\\x1b_Gm=i=1;\x1b\\"; + + assert.strictEqual(isImageLine(kittyLine), true, "Should detect Kitty image component output"); + }); + + it("handles ANSI codes before image sequences", async () => { + const { isImageLine } = await import("../src/terminal-image.ts"); + + // Line might have styling (error, warning, etc.) before image data + const lines = [ + "\x1b[31mError\x1b[0m: \x1b]1337;File=inline=1:base64==\x07", + "\x1b[33mWarning\x1b[0m: \x1b_Ga=T,data...\x1b\\", + "\x1b[1mBold\x1b[0m \x1b]1337;File=:base64==\x07\x1b[0m", + ]; + + for (const line of lines) { + assert.strictEqual( + isImageLine(line), + true, + `Should detect image sequence after ANSI codes: ${line.slice(0, 30)}...`, + ); + } + }); + }); + + describe("Crash scenario simulation", () => { + it("does NOT crash on very long lines with image sequences", async () => { + const { isImageLine } = await import("../src/terminal-image.ts"); + + /** + * Simulate the exact crash scenario: + * - Line is 304,401 characters (the crash log showed 58649 > 115) + * - Contains image escape sequence somewhere in the middle + * - Old implementation would return false, causing TUI to do width check + * - New implementation returns true, skipping width check (preventing crash) + */ + + const base64Char = "A".repeat(100); + const iterm2Sequence = "\x1b]1337;File=size=800,600;inline=1:"; + + // Build a line that would cause the crash + const crashLine = + "Output: " + + iterm2Sequence + + base64Char.repeat(3040) + // ~304,000 chars + " end of output"; + + // Verify line is very long + assert(crashLine.length > 300000, "Test line should be > 300KB"); + + // New implementation should detect it (prevents crash) + const detected = isImageLine(crashLine); + assert.strictEqual(detected, true, "Should detect image sequence in very long line, preventing TUI crash"); + }); + + it("handles lines exactly matching crash log dimensions", async () => { + const { isImageLine } = await import("../src/terminal-image.ts"); + + /** + * Crash log showed: line 58649 chars wide, terminal width 115 + * Let's create a line with similar characteristics + */ + + const targetWidth = 58649; + const prefix = "Text"; + const sequence = "\x1b_Ga=T,f=100"; + const suffix = "End"; + const padding = "A".repeat(targetWidth - prefix.length - sequence.length - suffix.length); + const line = `${prefix}${sequence}${padding}${suffix}`; + + assert.strictEqual(line.length, 58649); + assert.strictEqual(isImageLine(line), true, "Should detect image sequence in 58649-char line"); + }); + }); + + describe("Negative cases: Don't false positive", () => { + it("does not detect images in regular long text", async () => { + const { isImageLine } = await import("../src/terminal-image.ts"); + + // Very long line WITHOUT image sequences + const longText = "A".repeat(100000); + + assert.strictEqual(isImageLine(longText), false, "Should not detect images in plain long text"); + }); + + it("does not detect images in lines with file paths", async () => { + const { isImageLine } = await import("../src/terminal-image.ts"); + + const filePaths = [ + "/path/to/1337/image.jpg", + "/usr/local/bin/File_converter", + "~/Documents/1337File_backup.png", + "./_G_test_file.txt", + ]; + + for (const path of filePaths) { + assert.strictEqual(isImageLine(path), false, `Should not falsely detect image sequence in path: ${path}`); + } + }); + }); +}); diff --git a/packages/tui/test/chat-simple.ts b/packages/tui/test/chat-simple.ts new file mode 100644 index 0000000000000000000000000000000000000000..5f2c2802e4523d2280220f64f1ced308b63d5fbe --- /dev/null +++ b/packages/tui/test/chat-simple.ts @@ -0,0 +1,130 @@ +/** + * Simple chat interface demo using tui.ts + */ + +import chalk from "chalk"; +import { CombinedAutocompleteProvider } from "../src/autocomplete.ts"; +import { Editor } from "../src/components/editor.ts"; +import { Loader } from "../src/components/loader.ts"; +import { Markdown } from "../src/components/markdown.ts"; +import { Text } from "../src/components/text.ts"; +import { ProcessTerminal } from "../src/terminal.ts"; +import type { TUI } from "../src/tui.ts"; +import { TuiMainScreen } from "../src/tui-main-screen.ts"; +import { defaultEditorTheme, defaultMarkdownTheme } from "./test-themes.ts"; + +// Create terminal +const terminal = new ProcessTerminal(); + +// Create TUI +const tui: TUI = new TuiMainScreen(terminal); + +// Create chat container with some initial messages +tui.addChild( + new Text("Welcome to Simple Chat!\n\nType your messages below. Type '/' for commands. Press Ctrl+C to exit."), +); + +// Create editor with autocomplete +const editor = new Editor(tui, defaultEditorTheme); + +// Set up autocomplete provider with slash commands and file completion +const autocompleteProvider = new CombinedAutocompleteProvider( + [ + { name: "delete", description: "Delete the last message" }, + { name: "clear", description: "Clear all messages" }, + ], + process.cwd(), +); +editor.setAutocompleteProvider(autocompleteProvider); + +tui.addChild(editor); + +// Focus the editor +tui.setFocus(editor); + +// Track if we're waiting for bot response +let isResponding = false; + +// Handle message submission +editor.onSubmit = (value: string) => { + // Prevent submission if already responding + if (isResponding) { + return; + } + + const trimmed = value.trim(); + + // Handle slash commands + if (trimmed === "/delete") { + const children = tui.children; + // Remove component before editor (if there are any besides the initial text) + if (children.length > 3) { + // children[0] = "Welcome to Simple Chat!" + // children[1] = "Type your messages below..." + // children[2...n-1] = messages + // children[n] = editor + children.splice(children.length - 2, 1); + } + tui.requestRender(); + return; + } + + if (trimmed === "/clear") { + const children = tui.children; + // Remove all messages but keep the welcome text and editor + children.splice(2, children.length - 3); + tui.requestRender(); + return; + } + + if (trimmed) { + isResponding = true; + editor.disableSubmit = true; + + const userMessage = new Markdown(value, 1, 1, defaultMarkdownTheme); + + const children = tui.children; + children.splice(children.length - 1, 0, userMessage); + + const loader = new Loader( + tui, + (s) => chalk.cyan(s), + (s) => chalk.dim(s), + "Thinking...", + ); + children.splice(children.length - 1, 0, loader); + + tui.requestRender(); + + setTimeout(() => { + tui.removeChild(loader); + + // Simulate a response + const responses = [ + "That's interesting! Tell me more.", + "I see what you mean.", + "Fascinating perspective!", + "Could you elaborate on that?", + "That makes sense to me.", + "I hadn't thought of it that way.", + "Great point!", + "Thanks for sharing that.", + ]; + const randomResponse = responses[Math.floor(Math.random() * responses.length)]; + + // Add assistant message with no background (transparent) + const botMessage = new Markdown(randomResponse, 1, 1, defaultMarkdownTheme); + children.splice(children.length - 1, 0, botMessage); + + // Re-enable submit + isResponding = false; + editor.disableSubmit = false; + + // Request render + tui.requestRender(); + }, 1000); + } +}; + +// Start the TUI +tui.start(); diff --git a/packages/tui/test/editor-history-keybindings.test.ts b/packages/tui/test/editor-history-keybindings.test.ts new file mode 100644 index 0000000000000000000000000000000000000000..c26a4cf26c668267b2953593fbf2c6864ab851bc --- /dev/null +++ b/packages/tui/test/editor-history-keybindings.test.ts @@ -0,0 +1,43 @@ +import assert from "node:assert"; +import { afterEach, describe, it } from "node:test"; +import { Editor } from "../src/components/editor.ts"; +import { KeybindingsManager, setKeybindings, TUI_KEYBINDINGS } from "../src/keybindings.ts"; +import { TuiMainScreen } from "../src/tui-main-screen.ts"; +import { defaultEditorTheme } from "./test-themes.ts"; +import { VirtualTerminal } from "./virtual-terminal.ts"; + +afterEach(() => { + setKeybindings(new KeybindingsManager(TUI_KEYBINDINGS)); +}); + +describe("Editor prompt history keybindings", () => { + it("browses history directly without first moving the cursor", () => { + setKeybindings( + new KeybindingsManager(TUI_KEYBINDINGS, { + "tui.editor.historyPrevious": "ctrl+p", + "tui.editor.historyNext": "ctrl+n", + }), + ); + const editor = new Editor(new TuiMainScreen(new VirtualTerminal()), defaultEditorTheme); + editor.addToHistory("older prompt"); + editor.addToHistory("newer\nmultiline prompt"); + editor.setText("draft"); + editor.handleInput("\x1b[D"); + editor.handleInput("\x1b[D"); + + editor.handleInput("\x10"); // Ctrl+P + assert.strictEqual(editor.getText(), "newer\nmultiline prompt"); + assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 0 }); + + editor.handleInput("\x10"); // Ctrl+P + assert.strictEqual(editor.getText(), "older prompt"); + + editor.handleInput("\x0e"); // Ctrl+N + assert.strictEqual(editor.getText(), "newer\nmultiline prompt"); + assert.deepStrictEqual(editor.getCursor(), { line: 1, col: 16 }); + + editor.handleInput("\x0e"); // Ctrl+N + assert.strictEqual(editor.getText(), "draft"); + assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 3 }); + }); +}); diff --git a/packages/tui/test/editor.test.ts b/packages/tui/test/editor.test.ts new file mode 100644 index 0000000000000000000000000000000000000000..73fddc822430a337bbf748fb28c86e4d3a162c29 --- /dev/null +++ b/packages/tui/test/editor.test.ts @@ -0,0 +1,4165 @@ +import assert from "node:assert"; +import { describe, it } from "node:test"; +import { stripVTControlCharacters } from "node:util"; +import { type AutocompleteProvider, CombinedAutocompleteProvider } from "../src/autocomplete.ts"; +import { Editor, wordWrapLine } from "../src/components/editor.ts"; +import type { TUI } from "../src/tui.ts"; +import { TuiMainScreen } from "../src/tui-main-screen.ts"; +import { visibleWidth } from "../src/utils.ts"; +import { defaultEditorTheme } from "./test-themes.ts"; +import { VirtualTerminal } from "./virtual-terminal.ts"; + +/** Create a TUI with a virtual terminal for testing */ +function createTestTUI(cols = 80, rows = 24): TUI { + return new TuiMainScreen(new VirtualTerminal(cols, rows)); +} + +/** Standard applyCompletion that replaces prefix with item.value */ +function applyCompletion( + lines: string[], + cursorLine: number, + cursorCol: number, + item: { value: string }, + prefix: string, +): { lines: string[]; cursorLine: number; cursorCol: number } { + const line = lines[cursorLine] || ""; + const before = line.slice(0, cursorCol - prefix.length); + const after = line.slice(cursorCol); + const newLines = [...lines]; + newLines[cursorLine] = before + item.value + after; + return { + lines: newLines, + cursorLine, + cursorCol: cursorCol - prefix.length + item.value.length, + }; +} + +async function flushAutocomplete(): Promise { + await Promise.resolve(); + await new Promise((resolve) => setImmediate(resolve)); +} + +describe("Editor component", () => { + describe("Prompt history navigation", () => { + it("does nothing on Up arrow when history is empty", () => { + const editor = new Editor(createTestTUI(), defaultEditorTheme); + + editor.handleInput("\x1b[A"); // Up arrow + + assert.strictEqual(editor.getText(), ""); + }); + + it("shows most recent history entry on Up arrow when editor is empty", () => { + const editor = new Editor(createTestTUI(), defaultEditorTheme); + + editor.addToHistory("first prompt"); + editor.addToHistory("second prompt"); + + editor.handleInput("\x1b[A"); // Up arrow + + assert.strictEqual(editor.getText(), "second prompt"); + }); + + it("cycles through history entries on repeated Up arrow", () => { + const editor = new Editor(createTestTUI(), defaultEditorTheme); + + editor.addToHistory("first"); + editor.addToHistory("second"); + editor.addToHistory("third"); + + editor.handleInput("\x1b[A"); // Up - shows "third" + assert.strictEqual(editor.getText(), "third"); + + editor.handleInput("\x1b[A"); // Up - shows "second" + assert.strictEqual(editor.getText(), "second"); + + editor.handleInput("\x1b[A"); // Up - shows "first" + assert.strictEqual(editor.getText(), "first"); + + editor.handleInput("\x1b[A"); // Up - stays at "first" (oldest) + assert.strictEqual(editor.getText(), "first"); + }); + + it("jumps to start before entering history from a non-empty draft", () => { + const editor = new Editor(createTestTUI(), defaultEditorTheme); + + editor.addToHistory("prompt"); + editor.setText("draft"); + editor.handleInput("\x1b[D"); + editor.handleInput("\x1b[D"); + + editor.handleInput("\x1b[A"); // Up - jumps to start before history browsing + assert.strictEqual(editor.getText(), "draft"); + assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 0 }); + + editor.handleInput("\x1b[A"); // Up at start - shows "prompt" + assert.strictEqual(editor.getText(), "prompt"); + + editor.handleInput("\x1b[B"); // Down - restores draft + assert.strictEqual(editor.getText(), "draft"); + assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 0 }); + }); + + it("navigates forward through history with Down arrow", () => { + const editor = new Editor(createTestTUI(), defaultEditorTheme); + + editor.addToHistory("first"); + editor.addToHistory("second"); + editor.addToHistory("third"); + editor.setText("draft"); + + // Go to oldest + editor.handleInput("\x1b[A"); // start of draft + editor.handleInput("\x1b[A"); // third + editor.handleInput("\x1b[A"); // second + editor.handleInput("\x1b[A"); // first + + // Navigate back + editor.handleInput("\x1b[B"); // second + assert.strictEqual(editor.getText(), "second"); + + editor.handleInput("\x1b[B"); // third + assert.strictEqual(editor.getText(), "third"); + + editor.handleInput("\x1b[B"); // draft + assert.strictEqual(editor.getText(), "draft"); + }); + + it("exits history mode when typing a character", () => { + const editor = new Editor(createTestTUI(), defaultEditorTheme); + + editor.addToHistory("old prompt"); + + editor.handleInput("\x1b[A"); // Up - shows "old prompt" + editor.handleInput("x"); // Type a character - exits history mode + + assert.strictEqual(editor.getText(), "xold prompt"); + }); + + it("exits history mode on setText", () => { + const editor = new Editor(createTestTUI(), defaultEditorTheme); + + editor.addToHistory("first"); + editor.addToHistory("second"); + + editor.handleInput("\x1b[A"); // Up - shows "second" + editor.setText(""); // External clear + + // Up should start fresh from most recent + editor.handleInput("\x1b[A"); + assert.strictEqual(editor.getText(), "second"); + }); + + it("does not add empty strings to history", () => { + const editor = new Editor(createTestTUI(), defaultEditorTheme); + + editor.addToHistory(""); + editor.addToHistory(" "); + editor.addToHistory("valid"); + + editor.handleInput("\x1b[A"); + assert.strictEqual(editor.getText(), "valid"); + + // Should not have more entries + editor.handleInput("\x1b[A"); + assert.strictEqual(editor.getText(), "valid"); + }); + + it("does not add consecutive duplicates to history", () => { + const editor = new Editor(createTestTUI(), defaultEditorTheme); + + editor.addToHistory("same"); + editor.addToHistory("same"); + editor.addToHistory("same"); + + editor.handleInput("\x1b[A"); // "same" + assert.strictEqual(editor.getText(), "same"); + + editor.handleInput("\x1b[A"); // stays at "same" (only one entry) + assert.strictEqual(editor.getText(), "same"); + }); + + it("allows non-consecutive duplicates in history", () => { + const editor = new Editor(createTestTUI(), defaultEditorTheme); + + editor.addToHistory("first"); + editor.addToHistory("second"); + editor.addToHistory("first"); // Not consecutive, should be added + + editor.handleInput("\x1b[A"); // "first" + assert.strictEqual(editor.getText(), "first"); + + editor.handleInput("\x1b[A"); // "second" + assert.strictEqual(editor.getText(), "second"); + + editor.handleInput("\x1b[A"); // "first" (older one) + assert.strictEqual(editor.getText(), "first"); + }); + + it("uses cursor movement instead of history when editor has content", () => { + const editor = new Editor(createTestTUI(), defaultEditorTheme); + + editor.addToHistory("history item"); + editor.setText("line1\nline2"); + + // Cursor is at end of line2, Up should move to line1 + editor.handleInput("\x1b[A"); // Up - cursor movement + + // Insert character to verify cursor position + editor.handleInput("X"); + + // X should be inserted in line1, not replace with history + assert.strictEqual(editor.getText(), "line1X\nline2"); + }); + + it("limits history to 100 entries", () => { + const editor = new Editor(createTestTUI(), defaultEditorTheme); + + // Add 105 entries + for (let i = 0; i < 105; i++) { + editor.addToHistory(`prompt ${i}`); + } + + // Navigate to oldest + for (let i = 0; i < 100; i++) { + editor.handleInput("\x1b[A"); + } + + // Should be at entry 5 (oldest kept), not entry 0 + assert.strictEqual(editor.getText(), "prompt 5"); + + // One more Up should not change anything + editor.handleInput("\x1b[A"); + assert.strictEqual(editor.getText(), "prompt 5"); + }); + + it("places cursor at start after browsing history upward", () => { + const editor = new Editor(createTestTUI(), defaultEditorTheme); + + editor.addToHistory("older entry"); + editor.addToHistory("line1\nline2\nline3"); + + editor.handleInput("\x1b[A"); // Up - shows multi-line entry at start + assert.strictEqual(editor.getText(), "line1\nline2\nline3"); + assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 0 }); + + editor.handleInput("\x1b[A"); // Up again - immediately navigates to older entry + assert.strictEqual(editor.getText(), "older entry"); + assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 0 }); + }); + + it("places cursor at end after browsing history downward", () => { + const editor = new Editor(createTestTUI(), defaultEditorTheme); + + editor.addToHistory("older entry"); + editor.addToHistory("line1\nline2\nline3"); + editor.addToHistory("newer entry"); + + editor.handleInput("\x1b[A"); // newer entry + editor.handleInput("\x1b[A"); // multi-line entry + editor.handleInput("\x1b[A"); // older entry + + editor.handleInput("\x1b[B"); // Down - shows multi-line entry at end + assert.strictEqual(editor.getText(), "line1\nline2\nline3"); + assert.deepStrictEqual(editor.getCursor(), { line: 2, col: 5 }); + + editor.handleInput("\x1b[B"); // Down again - immediately navigates to newer entry + assert.strictEqual(editor.getText(), "newer entry"); + }); + + it("allows opposite-direction cursor movement within multi-line history entry", () => { + const editor = new Editor(createTestTUI(), defaultEditorTheme); + + editor.addToHistory("line1\nline2\nline3"); + + editor.handleInput("\x1b[A"); // Up - shows entry at start + assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 0 }); + + editor.handleInput("\x1b[B"); // Down - cursor moves to line2 + assert.strictEqual(editor.getText(), "line1\nline2\nline3"); + assert.deepStrictEqual(editor.getCursor(), { line: 1, col: 0 }); + + editor.handleInput("\x1b[A"); // Up - cursor moves back to line1 + assert.strictEqual(editor.getText(), "line1\nline2\nline3"); + assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 0 }); + }); + }); + + describe("public state accessors", () => { + it("returns cursor position", () => { + const editor = new Editor(createTestTUI(), defaultEditorTheme); + + assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 0 }); + + editor.handleInput("a"); + editor.handleInput("b"); + editor.handleInput("c"); + + assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 3 }); + + editor.handleInput("\x1b[D"); // Left + assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 2 }); + }); + + it("returns lines as a defensive copy", () => { + const editor = new Editor(createTestTUI(), defaultEditorTheme); + editor.setText("a\nb"); + + const lines = editor.getLines(); + assert.deepStrictEqual(lines, ["a", "b"]); + + lines[0] = "mutated"; + assert.deepStrictEqual(editor.getLines(), ["a", "b"]); + }); + }); + + describe("Backslash+Enter newline workaround", () => { + it("inserts backslash immediately (no buffering)", () => { + const editor = new Editor(createTestTUI(), defaultEditorTheme); + + editor.handleInput("\\"); + + // Backslash should be visible immediately, not buffered + assert.strictEqual(editor.getText(), "\\"); + }); + + it("converts standalone backslash to newline on Enter", () => { + const editor = new Editor(createTestTUI(), defaultEditorTheme); + + editor.handleInput("\\"); + editor.handleInput("\r"); + + assert.strictEqual(editor.getText(), "\n"); + }); + + it("inserts backslash normally when followed by other characters", () => { + const editor = new Editor(createTestTUI(), defaultEditorTheme); + + editor.handleInput("\\"); + editor.handleInput("x"); + + assert.strictEqual(editor.getText(), "\\x"); + }); + + it("does not trigger newline when backslash is not immediately before cursor", () => { + const editor = new Editor(createTestTUI(), defaultEditorTheme); + let submitted = false; + + editor.onSubmit = () => { + submitted = true; + }; + + editor.handleInput("\\"); + editor.handleInput("x"); + editor.handleInput("\r"); + + // Should submit, not insert newline (backslash not at cursor) + assert.strictEqual(submitted, true); + }); + + it("only removes one backslash when multiple are present", () => { + const editor = new Editor(createTestTUI(), defaultEditorTheme); + + editor.handleInput("\\"); + editor.handleInput("\\"); + editor.handleInput("\\"); + assert.strictEqual(editor.getText(), "\\\\\\"); + + editor.handleInput("\r"); + // Only the last backslash is removed, newline inserted + assert.strictEqual(editor.getText(), "\\\\\n"); + }); + }); + + describe("Kitty CSI-u handling", () => { + it("ignores printable CSI-u sequences with unsupported modifiers", () => { + const editor = new Editor(createTestTUI(), defaultEditorTheme); + + editor.handleInput("\x1b[99;9u"); + + assert.strictEqual(editor.getText(), ""); + }); + + it("inserts shifted CSI-u letters as text", () => { + const editor = new Editor(createTestTUI(), defaultEditorTheme); + + editor.handleInput("\x1b[69;2u"); + + assert.strictEqual(editor.getText(), "E"); + }); + + it("inserts shifted xterm modifyOtherKeys letters as text", () => { + const editor = new Editor(createTestTUI(), defaultEditorTheme); + + editor.handleInput("\x1b[27;2;69~"); + + assert.strictEqual(editor.getText(), "E"); + }); + }); + + describe("Unicode text editing behavior", () => { + it("inserts mixed ASCII, umlauts, and emojis as literal text", () => { + const editor = new Editor(createTestTUI(), defaultEditorTheme); + + editor.handleInput("H"); + editor.handleInput("e"); + editor.handleInput("l"); + editor.handleInput("l"); + editor.handleInput("o"); + editor.handleInput(" "); + editor.handleInput("ä"); + editor.handleInput("ö"); + editor.handleInput("ü"); + editor.handleInput(" "); + editor.handleInput("😀"); + + const text = editor.getText(); + assert.strictEqual(text, "Hello äöü 😀"); + }); + + it("deletes single-code-unit unicode characters (umlauts) with Backspace", () => { + const editor = new Editor(createTestTUI(), defaultEditorTheme); + + editor.handleInput("ä"); + editor.handleInput("ö"); + editor.handleInput("ü"); + + // Delete the last character (ü) + editor.handleInput("\x7f"); // Backspace + + const text = editor.getText(); + assert.strictEqual(text, "äö"); + }); + + it("deletes multi-code-unit emojis with single Backspace", () => { + const editor = new Editor(createTestTUI(), defaultEditorTheme); + + editor.handleInput("😀"); + editor.handleInput("👍"); + + // Delete the last emoji (👍) - single backspace deletes whole grapheme cluster + editor.handleInput("\x7f"); // Backspace + + const text = editor.getText(); + assert.strictEqual(text, "😀"); + }); + + it("inserts characters at the correct position after cursor movement over umlauts", () => { + const editor = new Editor(createTestTUI(), defaultEditorTheme); + + editor.handleInput("ä"); + editor.handleInput("ö"); + editor.handleInput("ü"); + + // Move cursor left twice + editor.handleInput("\x1b[D"); // Left arrow + editor.handleInput("\x1b[D"); // Left arrow + + // Insert 'x' in the middle + editor.handleInput("x"); + + const text = editor.getText(); + assert.strictEqual(text, "äxöü"); + }); + + it("moves cursor across multi-code-unit emojis with single arrow key", () => { + const editor = new Editor(createTestTUI(), defaultEditorTheme); + + editor.handleInput("😀"); + editor.handleInput("👍"); + editor.handleInput("🎉"); + + // Move cursor left over last emoji (🎉) - single arrow moves over whole grapheme + editor.handleInput("\x1b[D"); // Left arrow + + // Move cursor left over second emoji (👍) + editor.handleInput("\x1b[D"); + + // Insert 'x' between first and second emoji + editor.handleInput("x"); + + const text = editor.getText(); + assert.strictEqual(text, "😀x👍🎉"); + }); + + it("preserves umlauts across line breaks", () => { + const editor = new Editor(createTestTUI(), defaultEditorTheme); + + editor.handleInput("ä"); + editor.handleInput("ö"); + editor.handleInput("ü"); + editor.handleInput("\n"); // new line + editor.handleInput("Ä"); + editor.handleInput("Ö"); + editor.handleInput("Ü"); + + const text = editor.getText(); + assert.strictEqual(text, "äöü\nÄÖÜ"); + }); + + it("replaces the entire document with unicode text via setText (paste simulation)", () => { + const editor = new Editor(createTestTUI(), defaultEditorTheme); + + // Simulate bracketed paste / programmatic replacement + editor.setText("Hällö Wörld! 😀 äöüÄÖÜß"); + + const text = editor.getText(); + assert.strictEqual(text, "Hällö Wörld! 😀 äöüÄÖÜß"); + }); + + it("moves cursor to document start on Ctrl+A and inserts at the beginning", () => { + const editor = new Editor(createTestTUI(), defaultEditorTheme); + + editor.handleInput("a"); + editor.handleInput("b"); + editor.handleInput("\x01"); // Ctrl+A (move to start) + editor.handleInput("x"); // Insert at start + + const text = editor.getText(); + assert.strictEqual(text, "xab"); + }); + + it("deletes words correctly with Ctrl+W and Alt+Backspace", () => { + const editor = new Editor(createTestTUI(), defaultEditorTheme); + + // Basic word deletion + editor.setText("foo bar baz"); + editor.handleInput("\x17"); // Ctrl+W + assert.strictEqual(editor.getText(), "foo bar "); + + // Trailing whitespace + editor.setText("foo bar "); + editor.handleInput("\x17"); + assert.strictEqual(editor.getText(), "foo "); + + // Punctuation run + editor.setText("foo bar..."); + editor.handleInput("\x17"); + assert.strictEqual(editor.getText(), "foo bar"); + + // ASCII punctuation inside Intl word-like segments preserves old boundaries + editor.setText("foo.bar"); + editor.handleInput("\x17"); + assert.strictEqual(editor.getText(), "foo."); + + editor.setText("foo:bar"); + editor.handleInput("\x17"); + assert.strictEqual(editor.getText(), "foo:"); + + // Delete across multiple lines + editor.setText("line one\nline two"); + editor.handleInput("\x17"); + assert.strictEqual(editor.getText(), "line one\nline "); + + // Delete empty line (merge) + editor.setText("line one\n"); + editor.handleInput("\x17"); + assert.strictEqual(editor.getText(), "line one"); + + // Grapheme safety (emoji as a word) + editor.setText("foo 😀😀 bar"); + editor.handleInput("\x17"); + assert.strictEqual(editor.getText(), "foo 😀😀 "); + editor.handleInput("\x17"); + assert.strictEqual(editor.getText(), "foo "); + + // Alt+Backspace + editor.setText("foo bar"); + editor.handleInput("\x1b\x7f"); // Alt+Backspace (legacy) + assert.strictEqual(editor.getText(), "foo "); + }); + + it("navigates words correctly with Ctrl+Left/Right", () => { + const editor = new Editor(createTestTUI(), defaultEditorTheme); + + editor.setText("foo bar... baz"); + // Cursor at end + + // Move left over baz + editor.handleInput("\x1b[1;5D"); // Ctrl+Left + assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 11 }); // after '...' + + // Move left over punctuation + editor.handleInput("\x1b[1;5D"); // Ctrl+Left + assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 7 }); // after 'bar' + + // Move left over bar + editor.handleInput("\x1b[1;5D"); // Ctrl+Left + assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 4 }); // after 'foo ' + + // Move right over bar + editor.handleInput("\x1b[1;5C"); // Ctrl+Right + assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 7 }); // at end of 'bar' + + // Move right over punctuation run + editor.handleInput("\x1b[1;5C"); // Ctrl+Right + assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 10 }); // after '...' + + // Move right skips space and lands after baz + editor.handleInput("\x1b[1;5C"); // Ctrl+Right + assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 14 }); // end of line + + // Test forward from start with leading whitespace + editor.setText(" foo bar"); + editor.handleInput("\x01"); // Ctrl+A to go to start + editor.handleInput("\x1b[1;5C"); // Ctrl+Right + assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 6 }); // after 'foo' + + // ASCII punctuation inside Intl word-like segments preserves old boundaries + editor.setText("foo.bar baz"); + editor.handleInput("\x1b[1;5D"); // Ctrl+Left over baz + assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 8 }); + editor.handleInput("\x1b[1;5D"); // Ctrl+Left over bar + assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 4 }); + editor.handleInput("\x1b[1;5D"); // Ctrl+Left over . + assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 3 }); + + editor.handleInput("\x01"); // Ctrl+A + editor.handleInput("\x1b[1;5C"); // Ctrl+Right over foo + assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 3 }); + editor.handleInput("\x1b[1;5C"); // Ctrl+Right over . + assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 4 }); + editor.handleInput("\x1b[1;5C"); // Ctrl+Right over bar + assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 7 }); + }); + + it("stops at fullwidth Chinese punctuation (issue #4972)", () => { + const editor = new Editor(createTestTUI(), defaultEditorTheme); + + // 你好,世界 = 你好(0-2) ,(2-3) 世界(3-5) + editor.setText("你好,世界"); + // Cursor at end (col 5) + + // Move left over 世界 + editor.handleInput("\x1b[1;5D"); // Ctrl+Left + assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 3 }); // after , + + // Move left over , + editor.handleInput("\x1b[1;5D"); // Ctrl+Left + assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 2 }); // after 你好 + + // Move left over 你好 + editor.handleInput("\x1b[1;5D"); // Ctrl+Left + assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 0 }); // start + + // Move right over 你好 + editor.handleInput("\x1b[1;5C"); // Ctrl+Right + assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 2 }); // after 你好 + + // Move right over , + editor.handleInput("\x1b[1;5C"); // Ctrl+Right + assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 3 }); // after , + + // Move right over 世界 + editor.handleInput("\x1b[1;5C"); // Ctrl+Right + assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 5 }); // end + }); + + it("handles mixed CJK and ASCII word movement", () => { + const editor = new Editor(createTestTUI(), defaultEditorTheme); + + // "hello你好,world世界" = hello(0-5) 你好(5-7) ,(7-8) world(8-13) 世界(13-15) + editor.setText("hello你好,world世界"); + // Cursor at end (col 15) + + // Move left over 世界 + editor.handleInput("\x1b[1;5D"); // Ctrl+Left + assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 13 }); // after 'world' + + // Move left over world + editor.handleInput("\x1b[1;5D"); // Ctrl+Left + assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 8 }); // after , + + // Move left over , + editor.handleInput("\x1b[1;5D"); // Ctrl+Left + assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 7 }); // after 你好 + + // Move left over 你好 + editor.handleInput("\x1b[1;5D"); // Ctrl+Left + assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 5 }); // after 'hello' + + // Move left over hello + editor.handleInput("\x1b[1;5D"); // Ctrl+Left + assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 0 }); // start + + // Forward from start + editor.handleInput("\x1b[1;5C"); // Ctrl+Right + assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 5 }); // after 'hello' + + editor.handleInput("\x1b[1;5C"); // Ctrl+Right + assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 7 }); // after 你好 + + editor.handleInput("\x1b[1;5C"); // Ctrl+Right + assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 8 }); // after , + + editor.handleInput("\x1b[1;5C"); // Ctrl+Right + assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 13 }); // after 'world' + + editor.handleInput("\x1b[1;5C"); // Ctrl+Right + assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 15 }); // end + }); + }); + + describe("Scroll indicators", () => { + it("centers scroll indicators on wide borders", () => { + const width = 40; + const editor = new Editor(createTestTUI(width), defaultEditorTheme); + editor.setText(Array.from({ length: 20 }, (_, index) => `line ${index}`).join("\n")); + + editor.render(width); + for (let index = 0; index < 10; index++) editor.handleInput("\x1b[A"); + + const lines = editor.render(width); + assert.strictEqual(stripVTControlCharacters(lines[0]!), `${"─".repeat(15)} ↑ 9 more ${"─".repeat(15)}`); + assert.strictEqual(stripVTControlCharacters(lines.at(-1)!), `${"─".repeat(15)} ↓ 4 more ${"─".repeat(15)}`); + }); + + it("keeps truncated scroll indicators within width and preserves their color (issue #6962)", () => { + const width = 10; + const borderColor = (text: string) => `\x1b[35m${text}\x1b[39m`; + const editor = new Editor(createTestTUI(width), { ...defaultEditorTheme, borderColor }); + editor.setText(Array.from({ length: 20 }, (_, index) => `line ${index}`).join("\n")); + + // Render once to initialize wrapping, then move the cursor so content remains above and below the viewport. + editor.render(width); + for (let index = 0; index < 10; index++) editor.handleInput("\x1b[A"); + + const lines = editor.render(width); + const topBorder = lines[0]!; + const bottomBorder = lines.at(-1)!; + + assert.match(stripVTControlCharacters(topBorder), /^─── ↑/); + assert.match(stripVTControlCharacters(bottomBorder), /^─── ↓/); + assert.strictEqual(topBorder, borderColor(stripVTControlCharacters(topBorder))); + assert.strictEqual(bottomBorder, borderColor(stripVTControlCharacters(bottomBorder))); + for (const line of lines) { + assert.strictEqual(visibleWidth(line), width, `line exceeds width ${width}: ${JSON.stringify(line)}`); + } + }); + }); + + describe("Grapheme-aware text wrapping", () => { + it("wraps lines correctly when text contains wide emojis", () => { + const editor = new Editor(createTestTUI(), defaultEditorTheme); + const width = 20; + + // ✅ is 2 columns wide, so "Hello ✅ World" is 14 columns + editor.setText("Hello ✅ World"); + const lines = editor.render(width); + + // All content lines (between borders) should fit within width + for (let i = 1; i < lines.length - 1; i++) { + const lineWidth = visibleWidth(lines[i]!); + assert.strictEqual(lineWidth, width, `Line ${i} has width ${lineWidth}, expected ${width}`); + } + }); + + it("wraps long text with emojis at correct positions", () => { + const editor = new Editor(createTestTUI(), defaultEditorTheme); + const width = 10; + + // Each ✅ is 2 columns. "✅✅✅✅✅" = 10 columns, fits exactly + // "✅✅✅✅✅✅" = 12 columns, needs wrap + editor.setText("✅✅✅✅✅✅"); + const lines = editor.render(width); + + // Should have 2 content lines (plus 2 border lines) + // First line: 5 emojis (10 cols), second line: 1 emoji (2 cols) + padding + for (let i = 1; i < lines.length - 1; i++) { + const lineWidth = visibleWidth(lines[i]!); + assert.strictEqual(lineWidth, width, `Line ${i} has width ${lineWidth}, expected ${width}`); + } + }); + + it("renders isolated Thai and Lao AM clusters without width drift", () => { + for (const text of ["ำabc", "ຳabc"]) { + const editor = new Editor(createTestTUI(), defaultEditorTheme); + const width = 8; + editor.setText(text); + + for (const line of editor.render(width)) { + assert.strictEqual(visibleWidth(line), width, `line width drift for ${JSON.stringify(text)}: ${line}`); + } + } + }); + + it("wraps CJK characters correctly (each is 2 columns wide)", () => { + const editor = new Editor(createTestTUI(), defaultEditorTheme); + const width = 10 + 1; // +1 col reserved for cursor + + // Each CJK char is 2 columns. "日本語テスト" = 6 chars = 12 columns + editor.setText("日本語テスト"); + const lines = editor.render(width); + + for (let i = 1; i < lines.length - 1; i++) { + const lineWidth = visibleWidth(lines[i]!); + assert.strictEqual(lineWidth, width, `Line ${i} has width ${lineWidth}, expected ${width}`); + } + + // Verify content split correctly + const contentLines = lines.slice(1, -1).map((l) => stripVTControlCharacters(l).trim()); + assert.strictEqual(contentLines.length, 2); + assert.strictEqual(contentLines[0], "日本語テス"); // 5 chars = 10 columns + assert.strictEqual(contentLines[1], "ト"); // 1 char = 2 columns (+ padding) + }); + + it("handles mixed ASCII and wide characters in wrapping", () => { + const editor = new Editor(createTestTUI(), defaultEditorTheme); + const width = 15 + 1; // +1 col reserved for cursor + + // "Test ✅ OK 日本" = 4 + 1 + 2 + 1 + 2 + 1 + 4 = 15 columns (fits in width-1=15) + editor.setText("Test ✅ OK 日本"); + const lines = editor.render(width); + + // Should fit in one content line + const contentLines = lines.slice(1, -1); + assert.strictEqual(contentLines.length, 1); + + const lineWidth = visibleWidth(contentLines[0]!); + assert.strictEqual(lineWidth, width); + }); + + it("renders cursor correctly on wide characters", () => { + const editor = new Editor(createTestTUI(), defaultEditorTheme); + const width = 20; + + editor.setText("A✅B"); + // Cursor should be at end (after B) + const lines = editor.render(width); + + // The cursor (reverse video space) should be visible + const contentLine = lines[1]!; + assert.ok(contentLine.includes("\x1b[7m"), "Should have reverse video cursor"); + + // Line should still be correct width + assert.strictEqual(visibleWidth(contentLine), width); + }); + + it("does not exceed terminal width with emoji at wrap boundary", () => { + const editor = new Editor(createTestTUI(), defaultEditorTheme); + const width = 11; + + // "0123456789✅" = 10 ASCII + 2-wide emoji = 12 columns + // Should wrap before the emoji since it would exceed width + editor.setText("0123456789✅"); + const lines = editor.render(width); + + for (let i = 1; i < lines.length - 1; i++) { + const lineWidth = visibleWidth(lines[i]!); + assert.ok(lineWidth <= width, `Line ${i} has width ${lineWidth}, exceeds max ${width}`); + } + }); + + it("shows cursor at end of line before wrap, wraps on next char", () => { + const width = 10; + for (const paddingX of [0, 1]) { + const editor = new Editor(createTestTUI(width + paddingX), defaultEditorTheme, { paddingX }); + + // Type 9 chars → fills layoutWidth exactly, cursor at end on same line + for (const ch of "aaaaaaaaa") editor.handleInput(ch); + let lines = editor.render(width + paddingX); + let contentLines = lines.slice(1, -1); + assert.strictEqual(contentLines.length, 1, "Should be 1 content line before wrap"); + assert.ok(contentLines[0]!.endsWith("\x1b[7m \x1b[0m"), "Cursor should be at end of line"); + + // Type 1 more → text wraps to second line + editor.handleInput("a"); + lines = editor.render(width + paddingX); + contentLines = lines.slice(1, -1); + assert.strictEqual(contentLines.length, 2, "Should wrap to 2 content lines"); + } + }); + }); + + describe("Word wrapping", () => { + it("wraps at word boundaries instead of mid-word", () => { + const editor = new Editor(createTestTUI(), defaultEditorTheme); + const width = 40; + + editor.setText("Hello world this is a test of word wrapping functionality"); + const lines = editor.render(width); + + // Get content lines (between borders) + const contentLines = lines.slice(1, -1).map((l) => stripVTControlCharacters(l).trim()); + + // Should NOT break mid-word + // Line 1 should end with a complete word + assert.ok(!contentLines[0]!.endsWith("-"), "Line should not end with hyphen (mid-word break)"); + + // Each content line should be complete words + for (const line of contentLines) { + // Words at end of line should be complete (no partial words) + const lastChar = line.trimEnd().slice(-1); + assert.ok(lastChar === "" || /[\w.,!?;:]/.test(lastChar), `Line ends unexpectedly with: "${lastChar}"`); + } + }); + + it("does not start lines with leading whitespace after word wrap", () => { + const editor = new Editor(createTestTUI(), defaultEditorTheme); + const width = 20; + + editor.setText("Word1 Word2 Word3 Word4 Word5 Word6"); + const lines = editor.render(width); + + // Get content lines (between borders) + const contentLines = lines.slice(1, -1); + + // No line should start with whitespace (except for padding at the end) + for (let i = 0; i < contentLines.length; i++) { + const line = stripVTControlCharacters(contentLines[i]!); + const trimmedStart = line.trimStart(); + // The line should either be all padding or start with a word character + if (trimmedStart.length > 0) { + assert.ok(!/^\s+\S/.test(line.trimEnd()), `Line ${i} starts with unexpected whitespace before content`); + } + } + }); + + it("breaks long words (URLs) at character level", () => { + const editor = new Editor(createTestTUI(), defaultEditorTheme); + const width = 30; + + editor.setText("Check https://example.com/very/long/path/that/exceeds/width here"); + const lines = editor.render(width); + + // All lines should fit within width + for (let i = 1; i < lines.length - 1; i++) { + const lineWidth = visibleWidth(lines[i]!); + assert.strictEqual(lineWidth, width, `Line ${i} has width ${lineWidth}, expected ${width}`); + } + }); + + it("preserves multiple spaces within words on same line", () => { + const editor = new Editor(createTestTUI(), defaultEditorTheme); + const width = 50; + + editor.setText("Word1 Word2 Word3"); + const lines = editor.render(width); + + const contentLine = stripVTControlCharacters(lines[1]!).trim(); + // Multiple spaces should be preserved + assert.ok(contentLine.includes("Word1 Word2"), "Multiple spaces should be preserved"); + }); + + it("handles empty string", () => { + const editor = new Editor(createTestTUI(), defaultEditorTheme); + const width = 40; + + editor.setText(""); + const lines = editor.render(width); + + // Should have border + empty content + border + assert.strictEqual(lines.length, 3); + }); + + it("handles single word that fits exactly", () => { + const editor = new Editor(createTestTUI(), defaultEditorTheme); + const width = 10 + 1; // +1 col reserved for cursor + + editor.setText("1234567890"); + const lines = editor.render(width); + + // Should have exactly 3 lines (top border, content, bottom border) + assert.strictEqual(lines.length, 3); + const contentLine = stripVTControlCharacters(lines[1]!); + assert.ok(contentLine.includes("1234567890"), "Content should contain the word"); + }); + + it("wraps word to next line when it ends exactly at terminal width", () => { + // "hello " (6) + "world" (5) = 11, but "world" is non-whitespace ending at width. + // Thus, wrap it to next line. The trailing space stays with "hello" on line 1 + const chunks = wordWrapLine("hello world test", 11); + + assert.strictEqual(chunks.length, 2); + assert.strictEqual(chunks[0]!.text, "hello "); + assert.strictEqual(chunks[1]!.text, "world test"); + }); + + it("keeps whitespace at terminal width boundary on same line", () => { + // "hello world " is exactly 12 chars (including trailing space) + // The space at position 12 should stay on the first line + const chunks = wordWrapLine("hello world test", 12); + + assert.strictEqual(chunks.length, 2); + assert.strictEqual(chunks[0]!.text, "hello world "); + assert.strictEqual(chunks[1]!.text, "test"); + }); + + it("handles unbreakable word filling width exactly followed by space", () => { + const chunks = wordWrapLine("aaaaaaaaaaaa aaaa", 12); + + assert.strictEqual(chunks.length, 2); + assert.strictEqual(chunks[0]!.text, "aaaaaaaaaaaa"); + assert.strictEqual(chunks[1]!.text, " aaaa"); + }); + + it("wraps word to next line when it fits width but not remaining space", () => { + const chunks = wordWrapLine(" aaaaaaaaaaaa", 12); + + assert.strictEqual(chunks.length, 2); + assert.strictEqual(chunks[0]!.text, " "); + assert.strictEqual(chunks[1]!.text, "aaaaaaaaaaaa"); + }); + + it("keeps word with multi-space and following word together when they fit", () => { + const chunks = wordWrapLine("Lorem ipsum dolor sit amet, consectetur", 30); + + assert.strictEqual(chunks.length, 2); + assert.strictEqual(chunks[0]!.text, "Lorem ipsum dolor sit "); + assert.strictEqual(chunks[1]!.text, "amet, consectetur"); + }); + + it("keeps word with multi-space and following word when they fill width exactly", () => { + const chunks = wordWrapLine("Lorem ipsum dolor sit amet, consectetur", 30); + + assert.strictEqual(chunks.length, 2); + assert.strictEqual(chunks[0]!.text, "Lorem ipsum dolor sit "); + assert.strictEqual(chunks[1]!.text, "amet, consectetur"); + }); + + it("splits when word plus multi-space plus word exceeds width", () => { + const chunks = wordWrapLine("Lorem ipsum dolor sit amet, consectetur", 30); + + assert.strictEqual(chunks.length, 3); + assert.strictEqual(chunks[0]!.text, "Lorem ipsum dolor sit "); + assert.strictEqual(chunks[1]!.text, "amet, "); + assert.strictEqual(chunks[2]!.text, "consectetur"); + }); + + it("breaks long whitespace at line boundary", () => { + const chunks = wordWrapLine("Lorem ipsum dolor sit amet, consectetur", 30); + + assert.strictEqual(chunks.length, 3); + assert.strictEqual(chunks[0]!.text, "Lorem ipsum dolor sit "); + assert.strictEqual(chunks[1]!.text, "amet, "); + assert.strictEqual(chunks[2]!.text, "consectetur"); + }); + + it("breaks long whitespace at line boundary 2", () => { + const chunks = wordWrapLine("Lorem ipsum dolor sit amet, consectetur", 30); + + assert.strictEqual(chunks.length, 3); + assert.strictEqual(chunks[0]!.text, "Lorem ipsum dolor sit "); + assert.strictEqual(chunks[1]!.text, "amet, "); + assert.strictEqual(chunks[2]!.text, " consectetur"); + }); + + it("breaks whitespace spanning full lines", () => { + const chunks = wordWrapLine("Lorem ipsum dolor sit amet, consectetur", 30); + + assert.strictEqual(chunks.length, 3); + assert.strictEqual(chunks[0]!.text, "Lorem ipsum dolor sit "); + assert.strictEqual(chunks[1]!.text, "amet, "); + assert.strictEqual(chunks[2]!.text, " consectetur"); + }); + + it("force-breaks when wide char after word boundary wrap still overflows", () => { + // " " (1) + "a"*186 (186) + "你" (2) = 189 visible width + // maxWidth = 187: backtracking to the space would leave 186 + 2 = 188 > 187, + // so the algorithm must force-break before the wide char instead. + const line = ` ${"a".repeat(186)}你`; + const chunks = wordWrapLine(line, 187); + + for (const chunk of chunks) { + assert.ok( + visibleWidth(chunk.text) <= 187, + `chunk "${chunk.text.slice(0, 20)}..." has visible width ${visibleWidth(chunk.text)}, expected <= 187`, + ); + } + // Verify no content is lost + const reconstructed = chunks.map((c) => line.slice(c.startIndex, c.endIndex)).join(""); + assert.strictEqual(reconstructed, line); + }); + + it("splits oversized atomic segment across multiple chunks", () => { + // Simulate a paste marker wider than maxWidth by passing pre-segmented data + const marker = "[paste #1 +20 lines]"; // 21 chars + const line = `A${marker}B`; + const segments: Intl.SegmentData[] = [ + { segment: "A", index: 0, input: line }, + { segment: marker, index: 1, input: line }, + { segment: "B", index: 1 + marker.length, input: line }, + ]; + + const chunks = wordWrapLine(line, 10, segments); + + // Every chunk must fit within maxWidth + for (const chunk of chunks) { + assert.ok( + visibleWidth(chunk.text) <= 10, + `chunk "${chunk.text}" has visible width ${visibleWidth(chunk.text)}, expected <= 10`, + ); + } + + // Verify no content is lost + const reconstructed = chunks.map((c) => line.slice(c.startIndex, c.endIndex)).join(""); + assert.strictEqual(reconstructed, line); + }); + + it("splits oversized atomic segment at start of line", () => { + const marker = "[paste #1 +20 lines]"; // 21 chars + const line = `${marker}B`; + const segments: Intl.SegmentData[] = [ + { segment: marker, index: 0, input: line }, + { segment: "B", index: marker.length, input: line }, + ]; + + const chunks = wordWrapLine(line, 10, segments); + + for (const chunk of chunks) { + assert.ok(visibleWidth(chunk.text) <= 10); + } + // "B" ends up on the last line (either alone or with the marker tail) + assert.strictEqual(chunks[chunks.length - 1]!.text.includes("B"), true); + + const reconstructed = chunks.map((c) => line.slice(c.startIndex, c.endIndex)).join(""); + assert.strictEqual(reconstructed, line); + }); + + it("splits oversized atomic segment at end of line", () => { + const marker = "[paste #1 +20 lines]"; // 21 chars + const line = `A${marker}`; + const segments: Intl.SegmentData[] = [ + { segment: "A", index: 0, input: line }, + { segment: marker, index: 1, input: line }, + ]; + + const chunks = wordWrapLine(line, 10, segments); + + for (const chunk of chunks) { + assert.ok(visibleWidth(chunk.text) <= 10); + } + assert.strictEqual(chunks[0]!.text, "A"); + + const reconstructed = chunks.map((c) => line.slice(c.startIndex, c.endIndex)).join(""); + assert.strictEqual(reconstructed, line); + }); + + it("splits consecutive oversized atomic segments", () => { + const m1 = "[paste #1 +20 lines]"; // 21 chars + const m2 = "[paste #2 +30 lines]"; // 21 chars + const line = `${m1}${m2}`; + const segments: Intl.SegmentData[] = [ + { segment: m1, index: 0, input: line }, + { segment: m2, index: m1.length, input: line }, + ]; + + const chunks = wordWrapLine(line, 10, segments); + + for (const chunk of chunks) { + assert.ok( + visibleWidth(chunk.text) <= 10, + `chunk "${chunk.text}" has visible width ${visibleWidth(chunk.text)}, expected <= 10`, + ); + } + + const reconstructed = chunks.map((c) => line.slice(c.startIndex, c.endIndex)).join(""); + assert.strictEqual(reconstructed, line); + }); + + it("wraps normally after oversized atomic segment", () => { + const marker = "[paste #1 +20 lines]"; // 21 chars + const line = `${marker} hello world`; + const segments: Intl.SegmentData[] = [ + { segment: marker, index: 0, input: line }, + { segment: " ", index: marker.length, input: line }, + { segment: "h", index: marker.length + 1, input: line }, + { segment: "e", index: marker.length + 2, input: line }, + { segment: "l", index: marker.length + 3, input: line }, + { segment: "l", index: marker.length + 4, input: line }, + { segment: "o", index: marker.length + 5, input: line }, + { segment: " ", index: marker.length + 6, input: line }, + { segment: "w", index: marker.length + 7, input: line }, + { segment: "o", index: marker.length + 8, input: line }, + { segment: "r", index: marker.length + 9, input: line }, + { segment: "l", index: marker.length + 10, input: line }, + { segment: "d", index: marker.length + 11, input: line }, + ]; + + const chunks = wordWrapLine(line, 10, segments); + + // All chunks must fit + for (const chunk of chunks) { + assert.ok( + visibleWidth(chunk.text) <= 10, + `chunk "${chunk.text}" has visible width ${visibleWidth(chunk.text)}, expected <= 10`, + ); + } + + // Last chunk should contain "world" (normal wrapping resumes) + assert.strictEqual(chunks[chunks.length - 1]!.text, "world"); + + const reconstructed = chunks.map((c) => line.slice(c.startIndex, c.endIndex)).join(""); + assert.strictEqual(reconstructed, line); + }); + }); + + describe("Kill ring", () => { + it("Ctrl+W saves deleted text to kill ring and Ctrl+Y yanks it", () => { + const editor = new Editor(createTestTUI(), defaultEditorTheme); + + editor.setText("foo bar baz"); + editor.handleInput("\x17"); // Ctrl+W - deletes "baz" + assert.strictEqual(editor.getText(), "foo bar "); + + // Move to beginning and yank + editor.handleInput("\x01"); // Ctrl+A + editor.handleInput("\x19"); // Ctrl+Y + assert.strictEqual(editor.getText(), "bazfoo bar "); + }); + + it("Ctrl+U saves deleted text to kill ring", () => { + const editor = new Editor(createTestTUI(), defaultEditorTheme); + + editor.setText("hello world"); + // Move cursor to middle + editor.handleInput("\x01"); // Ctrl+A (start) + editor.handleInput("\x1b[C"); // Right 5 times + editor.handleInput("\x1b[C"); + editor.handleInput("\x1b[C"); + editor.handleInput("\x1b[C"); + editor.handleInput("\x1b[C"); + editor.handleInput("\x1b[C"); // After "hello " + + editor.handleInput("\x15"); // Ctrl+U - deletes "hello " + assert.strictEqual(editor.getText(), "world"); + + editor.handleInput("\x19"); // Ctrl+Y + assert.strictEqual(editor.getText(), "hello world"); + }); + + it("Ctrl+K saves deleted text to kill ring", () => { + const editor = new Editor(createTestTUI(), defaultEditorTheme); + + editor.setText("hello world"); + editor.handleInput("\x01"); // Ctrl+A (start) + editor.handleInput("\x0b"); // Ctrl+K - deletes "hello world" + + assert.strictEqual(editor.getText(), ""); + + editor.handleInput("\x19"); // Ctrl+Y + assert.strictEqual(editor.getText(), "hello world"); + }); + + it("Ctrl+Y does nothing when kill ring is empty", () => { + const editor = new Editor(createTestTUI(), defaultEditorTheme); + + editor.setText("test"); + editor.handleInput("\x19"); // Ctrl+Y + assert.strictEqual(editor.getText(), "test"); + }); + + it("Alt+Y cycles through kill ring after Ctrl+Y", () => { + const editor = new Editor(createTestTUI(), defaultEditorTheme); + + // Create kill ring with multiple entries + editor.setText("first"); + editor.handleInput("\x17"); // Ctrl+W - deletes "first" + editor.setText("second"); + editor.handleInput("\x17"); // Ctrl+W - deletes "second" + editor.setText("third"); + editor.handleInput("\x17"); // Ctrl+W - deletes "third" + + // Kill ring now has: [first, second, third] + assert.strictEqual(editor.getText(), ""); + + editor.handleInput("\x19"); // Ctrl+Y - yanks "third" (most recent) + assert.strictEqual(editor.getText(), "third"); + + editor.handleInput("\x1by"); // Alt+Y - cycles to "second" + assert.strictEqual(editor.getText(), "second"); + + editor.handleInput("\x1by"); // Alt+Y - cycles to "first" + assert.strictEqual(editor.getText(), "first"); + + editor.handleInput("\x1by"); // Alt+Y - cycles back to "third" + assert.strictEqual(editor.getText(), "third"); + }); + + it("Alt+Y does nothing if not preceded by yank", () => { + const editor = new Editor(createTestTUI(), defaultEditorTheme); + + editor.setText("test"); + editor.handleInput("\x17"); // Ctrl+W - deletes "test" + editor.setText("other"); + + // Type something to break the yank chain + editor.handleInput("x"); + assert.strictEqual(editor.getText(), "otherx"); + + // Alt+Y should do nothing + editor.handleInput("\x1by"); // Alt+Y + assert.strictEqual(editor.getText(), "otherx"); + }); + + it("Alt+Y does nothing if kill ring has ≤1 entry", () => { + const editor = new Editor(createTestTUI(), defaultEditorTheme); + + editor.setText("only"); + editor.handleInput("\x17"); // Ctrl+W - deletes "only" + + editor.handleInput("\x19"); // Ctrl+Y - yanks "only" + assert.strictEqual(editor.getText(), "only"); + + editor.handleInput("\x1by"); // Alt+Y - should do nothing (only 1 entry) + assert.strictEqual(editor.getText(), "only"); + }); + + it("consecutive Ctrl+W accumulates into one kill ring entry", () => { + const editor = new Editor(createTestTUI(), defaultEditorTheme); + + editor.setText("one two three"); + editor.handleInput("\x17"); // Ctrl+W - deletes "three" + editor.handleInput("\x17"); // Ctrl+W - deletes "two " (prepended) + editor.handleInput("\x17"); // Ctrl+W - deletes "one " (prepended) + + assert.strictEqual(editor.getText(), ""); + + // Should be one combined entry + editor.handleInput("\x19"); // Ctrl+Y + assert.strictEqual(editor.getText(), "one two three"); + }); + + it("Ctrl+U accumulates multiline deletes including newlines", () => { + const editor = new Editor(createTestTUI(), defaultEditorTheme); + + // Start with multiline text, cursor at end + editor.setText("line1\nline2\nline3"); + // Cursor is at end of line3 (line 2, col 5) + + // Delete "line3" + editor.handleInput("\x15"); // Ctrl+U + assert.strictEqual(editor.getText(), "line1\nline2\n"); + + // Delete newline (at start of empty line 2, merges with line1) + editor.handleInput("\x15"); // Ctrl+U + assert.strictEqual(editor.getText(), "line1\nline2"); + + // Delete "line2" + editor.handleInput("\x15"); // Ctrl+U + assert.strictEqual(editor.getText(), "line1\n"); + + // Delete newline + editor.handleInput("\x15"); // Ctrl+U + assert.strictEqual(editor.getText(), "line1"); + + // Delete "line1" + editor.handleInput("\x15"); // Ctrl+U + assert.strictEqual(editor.getText(), ""); + + // All deletions accumulated into one entry: "line1\nline2\nline3" + editor.handleInput("\x19"); // Ctrl+Y + assert.strictEqual(editor.getText(), "line1\nline2\nline3"); + }); + + it("backward deletions prepend, forward deletions append during accumulation", () => { + const editor = new Editor(createTestTUI(), defaultEditorTheme); + + editor.setText("prefix|suffix"); + // Position cursor at | + editor.handleInput("\x01"); // Ctrl+A + for (let i = 0; i < 6; i++) editor.handleInput("\x1b[C"); // Move right 6 times + + editor.handleInput("\x0b"); // Ctrl+K - deletes "suffix" (forward) + editor.handleInput("\x0b"); // Ctrl+K - deletes "|" (forward, appended) + assert.strictEqual(editor.getText(), "prefix"); + + editor.handleInput("\x19"); // Ctrl+Y + assert.strictEqual(editor.getText(), "prefix|suffix"); + }); + + it("non-delete actions break kill accumulation", () => { + const editor = new Editor(createTestTUI(), defaultEditorTheme); + + // Delete "baz", then type "x" to break accumulation, then delete "x" + editor.setText("foo bar baz"); + editor.handleInput("\x17"); // Ctrl+W - deletes "baz" + assert.strictEqual(editor.getText(), "foo bar "); + + editor.handleInput("x"); // Typing breaks accumulation + assert.strictEqual(editor.getText(), "foo bar x"); + + editor.handleInput("\x17"); // Ctrl+W - deletes "x" (separate entry, not accumulated) + assert.strictEqual(editor.getText(), "foo bar "); + + // Yank most recent - should be "x", not "xbaz" + editor.handleInput("\x19"); // Ctrl+Y + assert.strictEqual(editor.getText(), "foo bar x"); + + // Cycle to previous - should be "baz" (separate entry) + editor.handleInput("\x1by"); // Alt+Y + assert.strictEqual(editor.getText(), "foo bar baz"); + }); + + it("non-yank actions break Alt+Y chain", () => { + const editor = new Editor(createTestTUI(), defaultEditorTheme); + + editor.setText("first"); + editor.handleInput("\x17"); // Ctrl+W + editor.setText("second"); + editor.handleInput("\x17"); // Ctrl+W + editor.setText(""); + + editor.handleInput("\x19"); // Ctrl+Y - yanks "second" + assert.strictEqual(editor.getText(), "second"); + + editor.handleInput("x"); // Type breaks yank chain + assert.strictEqual(editor.getText(), "secondx"); + + editor.handleInput("\x1by"); // Alt+Y - should do nothing + assert.strictEqual(editor.getText(), "secondx"); + }); + + it("kill ring rotation persists after cycling", () => { + const editor = new Editor(createTestTUI(), defaultEditorTheme); + + editor.setText("first"); + editor.handleInput("\x17"); // deletes "first" + editor.setText("second"); + editor.handleInput("\x17"); // deletes "second" + editor.setText("third"); + editor.handleInput("\x17"); // deletes "third" + editor.setText(""); + + // Ring: [first, second, third] + + editor.handleInput("\x19"); // Ctrl+Y - yanks "third" + editor.handleInput("\x1by"); // Alt+Y - cycles to "second", ring rotates + + // Now ring is: [third, first, second] + assert.strictEqual(editor.getText(), "second"); + + // Do something else + editor.handleInput("x"); + editor.setText(""); + + // New yank should get "second" (now at end after rotation) + editor.handleInput("\x19"); // Ctrl+Y + assert.strictEqual(editor.getText(), "second"); + }); + + it("consecutive deletions across lines coalesce into one entry", () => { + const editor = new Editor(createTestTUI(), defaultEditorTheme); + + // "1\n2\n3" with cursor at end, delete everything with Ctrl+W + editor.setText("1\n2\n3"); + editor.handleInput("\x17"); // Ctrl+W - deletes "3" + assert.strictEqual(editor.getText(), "1\n2\n"); + + editor.handleInput("\x17"); // Ctrl+W - deletes newline (merge with prev line) + assert.strictEqual(editor.getText(), "1\n2"); + + editor.handleInput("\x17"); // Ctrl+W - deletes "2" + assert.strictEqual(editor.getText(), "1\n"); + + editor.handleInput("\x17"); // Ctrl+W - deletes newline + assert.strictEqual(editor.getText(), "1"); + + editor.handleInput("\x17"); // Ctrl+W - deletes "1" + assert.strictEqual(editor.getText(), ""); + + // All deletions should have accumulated into one entry + editor.handleInput("\x19"); // Ctrl+Y + assert.strictEqual(editor.getText(), "1\n2\n3"); + }); + + it("Ctrl+K at line end deletes newline and coalesces", () => { + const editor = new Editor(createTestTUI(), defaultEditorTheme); + + // "ab" on line 1, "cd" on line 2, cursor at end of line 1 + editor.setText(""); + editor.handleInput("a"); + editor.handleInput("b"); + editor.handleInput("\n"); + editor.handleInput("c"); + editor.handleInput("d"); + // Move to end of first line + editor.handleInput("\x1b[A"); // Up arrow + editor.handleInput("\x05"); // Ctrl+E - end of line + + // Now at end of "ab", Ctrl+K should delete newline (merge with "cd") + editor.handleInput("\x0b"); // Ctrl+K - deletes newline + assert.strictEqual(editor.getText(), "abcd"); + + // Continue deleting + editor.handleInput("\x0b"); // Ctrl+K - deletes "cd" + assert.strictEqual(editor.getText(), "ab"); + + // Both deletions should accumulate + editor.handleInput("\x19"); // Ctrl+Y + assert.strictEqual(editor.getText(), "ab\ncd"); + }); + + it("handles yank in middle of text", () => { + const editor = new Editor(createTestTUI(), defaultEditorTheme); + + editor.setText("word"); + editor.handleInput("\x17"); // Ctrl+W - deletes "word" + editor.setText("hello world"); + + // Move to middle (after "hello ") + editor.handleInput("\x01"); // Ctrl+A + for (let i = 0; i < 6; i++) editor.handleInput("\x1b[C"); + + editor.handleInput("\x19"); // Ctrl+Y + assert.strictEqual(editor.getText(), "hello wordworld"); + }); + + it("handles yank-pop in middle of text", () => { + const editor = new Editor(createTestTUI(), defaultEditorTheme); + + // Create two kill ring entries + editor.setText("FIRST"); + editor.handleInput("\x17"); // Ctrl+W - deletes "FIRST" + editor.setText("SECOND"); + editor.handleInput("\x17"); // Ctrl+W - deletes "SECOND" + + // Ring: ["FIRST", "SECOND"] + + // Set up "hello world" and position cursor after "hello " + editor.setText("hello world"); + editor.handleInput("\x01"); // Ctrl+A - go to start of line + for (let i = 0; i < 6; i++) editor.handleInput("\x1b[C"); // Move right 6 + + // Yank "SECOND" in the middle + editor.handleInput("\x19"); // Ctrl+Y + assert.strictEqual(editor.getText(), "hello SECONDworld"); + + // Yank-pop replaces "SECOND" with "FIRST" + editor.handleInput("\x1by"); // Alt+Y + assert.strictEqual(editor.getText(), "hello FIRSTworld"); + }); + + it("multiline yank and yank-pop in middle of text", () => { + const editor = new Editor(createTestTUI(), defaultEditorTheme); + + // Create single-line entry + editor.setText("SINGLE"); + editor.handleInput("\x17"); // Ctrl+W - deletes "SINGLE" + + // Create multiline entry via consecutive Ctrl+U + editor.setText("A\nB"); + editor.handleInput("\x15"); // Ctrl+U - deletes "B" + editor.handleInput("\x15"); // Ctrl+U - deletes newline + editor.handleInput("\x15"); // Ctrl+U - deletes "A" + // Ring: ["SINGLE", "A\nB"] + + // Insert in middle of "hello world" + editor.setText("hello world"); + editor.handleInput("\x01"); // Ctrl+A + for (let i = 0; i < 6; i++) editor.handleInput("\x1b[C"); + + // Yank multiline "A\nB" + editor.handleInput("\x19"); // Ctrl+Y + assert.strictEqual(editor.getText(), "hello A\nBworld"); + + // Yank-pop replaces with "SINGLE" + editor.handleInput("\x1by"); // Alt+Y + assert.strictEqual(editor.getText(), "hello SINGLEworld"); + }); + + it("Alt+D deletes word forward and saves to kill ring", () => { + const editor = new Editor(createTestTUI(), defaultEditorTheme); + + editor.setText("hello world test"); + editor.handleInput("\x01"); // Ctrl+A - go to start + + editor.handleInput("\x1bd"); // Alt+D - deletes "hello" + assert.strictEqual(editor.getText(), " world test"); + + editor.handleInput("\x1bd"); // Alt+D - deletes " world" (skips whitespace, then word) + assert.strictEqual(editor.getText(), " test"); + + // Yank should get accumulated text + editor.handleInput("\x19"); // Ctrl+Y + assert.strictEqual(editor.getText(), "hello world test"); + }); + + it("Alt+D at end of line deletes newline", () => { + const editor = new Editor(createTestTUI(), defaultEditorTheme); + + editor.setText("line1\nline2"); + // Move to start of document, then to end of first line + editor.handleInput("\x1b[A"); // Up arrow - go to first line + editor.handleInput("\x05"); // Ctrl+E - end of line + + editor.handleInput("\x1bd"); // Alt+D - deletes newline (merges lines) + assert.strictEqual(editor.getText(), "line1line2"); + + editor.handleInput("\x19"); // Ctrl+Y + assert.strictEqual(editor.getText(), "line1\nline2"); + }); + }); + + describe("Undo", () => { + it("does nothing when undo stack is empty", () => { + const editor = new Editor(createTestTUI(), defaultEditorTheme); + + editor.handleInput("\x1b[45;5u"); // Ctrl+- (undo) + assert.strictEqual(editor.getText(), ""); + }); + + it("coalesces consecutive word characters into one undo unit", () => { + const editor = new Editor(createTestTUI(), defaultEditorTheme); + + editor.handleInput("h"); + editor.handleInput("e"); + editor.handleInput("l"); + editor.handleInput("l"); + editor.handleInput("o"); + editor.handleInput(" "); + editor.handleInput("w"); + editor.handleInput("o"); + editor.handleInput("r"); + editor.handleInput("l"); + editor.handleInput("d"); + assert.strictEqual(editor.getText(), "hello world"); + + // Undo removes " world" (space captured state before it, so we restore to "hello") + editor.handleInput("\x1b[45;5u"); // Ctrl+- (undo) + assert.strictEqual(editor.getText(), "hello"); + + // Undo removes "hello" + editor.handleInput("\x1b[45;5u"); // Ctrl+- (undo) + assert.strictEqual(editor.getText(), ""); + }); + + it("undoes spaces one at a time", () => { + const editor = new Editor(createTestTUI(), defaultEditorTheme); + + editor.handleInput("h"); + editor.handleInput("e"); + editor.handleInput("l"); + editor.handleInput("l"); + editor.handleInput("o"); + editor.handleInput(" "); + editor.handleInput(" "); + assert.strictEqual(editor.getText(), "hello "); + + editor.handleInput("\x1b[45;5u"); // Ctrl+- (undo) - removes second " " + assert.strictEqual(editor.getText(), "hello "); + + editor.handleInput("\x1b[45;5u"); // Ctrl+- (undo) - removes first " " + assert.strictEqual(editor.getText(), "hello"); + + editor.handleInput("\x1b[45;5u"); // Ctrl+- (undo) - removes "hello" + assert.strictEqual(editor.getText(), ""); + }); + + it("undoes newlines and signals next word to capture state", () => { + const editor = new Editor(createTestTUI(), defaultEditorTheme); + + editor.handleInput("h"); + editor.handleInput("e"); + editor.handleInput("l"); + editor.handleInput("l"); + editor.handleInput("o"); + editor.handleInput("\n"); + editor.handleInput("w"); + editor.handleInput("o"); + editor.handleInput("r"); + editor.handleInput("l"); + editor.handleInput("d"); + assert.strictEqual(editor.getText(), "hello\nworld"); + + editor.handleInput("\x1b[45;5u"); // Ctrl+- (undo) + assert.strictEqual(editor.getText(), "hello\n"); + + editor.handleInput("\x1b[45;5u"); // Ctrl+- (undo) + assert.strictEqual(editor.getText(), "hello"); + + editor.handleInput("\x1b[45;5u"); // Ctrl+- (undo) + assert.strictEqual(editor.getText(), ""); + }); + + it("undoes backspace", () => { + const editor = new Editor(createTestTUI(), defaultEditorTheme); + + editor.handleInput("h"); + editor.handleInput("e"); + editor.handleInput("l"); + editor.handleInput("l"); + editor.handleInput("o"); + editor.handleInput("\x7f"); // Backspace + assert.strictEqual(editor.getText(), "hell"); + + editor.handleInput("\x1b[45;5u"); // Ctrl+- (undo) + assert.strictEqual(editor.getText(), "hello"); + }); + + it("undoes forward delete", () => { + const editor = new Editor(createTestTUI(), defaultEditorTheme); + + editor.handleInput("h"); + editor.handleInput("e"); + editor.handleInput("l"); + editor.handleInput("l"); + editor.handleInput("o"); + editor.handleInput("\x01"); // Ctrl+A - go to start + editor.handleInput("\x1b[C"); // Right arrow + editor.handleInput("\x1b[3~"); // Delete key + assert.strictEqual(editor.getText(), "hllo"); + + editor.handleInput("\x1b[45;5u"); // Ctrl+- (undo) + assert.strictEqual(editor.getText(), "hello"); + }); + + it("undoes Ctrl+W (delete word backward)", () => { + const editor = new Editor(createTestTUI(), defaultEditorTheme); + + editor.handleInput("h"); + editor.handleInput("e"); + editor.handleInput("l"); + editor.handleInput("l"); + editor.handleInput("o"); + editor.handleInput(" "); + editor.handleInput("w"); + editor.handleInput("o"); + editor.handleInput("r"); + editor.handleInput("l"); + editor.handleInput("d"); + assert.strictEqual(editor.getText(), "hello world"); + + editor.handleInput("\x17"); // Ctrl+W + assert.strictEqual(editor.getText(), "hello "); + + editor.handleInput("\x1b[45;5u"); // Ctrl+- (undo) + assert.strictEqual(editor.getText(), "hello world"); + }); + + it("undoes Ctrl+K (delete to line end)", () => { + const editor = new Editor(createTestTUI(), defaultEditorTheme); + + editor.handleInput("h"); + editor.handleInput("e"); + editor.handleInput("l"); + editor.handleInput("l"); + editor.handleInput("o"); + editor.handleInput(" "); + editor.handleInput("w"); + editor.handleInput("o"); + editor.handleInput("r"); + editor.handleInput("l"); + editor.handleInput("d"); + editor.handleInput("\x01"); // Ctrl+A - go to start + for (let i = 0; i < 6; i++) editor.handleInput("\x1b[C"); // Move right 6 times + + editor.handleInput("\x0b"); // Ctrl+K + assert.strictEqual(editor.getText(), "hello "); + + editor.handleInput("\x1b[45;5u"); // Ctrl+- (undo) + assert.strictEqual(editor.getText(), "hello world"); + + editor.handleInput("|"); + assert.strictEqual(editor.getText(), "hello |world"); + }); + + it("undoes Ctrl+U (delete to line start)", () => { + const editor = new Editor(createTestTUI(), defaultEditorTheme); + + editor.handleInput("h"); + editor.handleInput("e"); + editor.handleInput("l"); + editor.handleInput("l"); + editor.handleInput("o"); + editor.handleInput(" "); + editor.handleInput("w"); + editor.handleInput("o"); + editor.handleInput("r"); + editor.handleInput("l"); + editor.handleInput("d"); + editor.handleInput("\x01"); // Ctrl+A - go to start + for (let i = 0; i < 6; i++) editor.handleInput("\x1b[C"); // Move right 6 times + + editor.handleInput("\x15"); // Ctrl+U + assert.strictEqual(editor.getText(), "world"); + + editor.handleInput("\x1b[45;5u"); // Ctrl+- (undo) + assert.strictEqual(editor.getText(), "hello world"); + }); + + it("undoes yank", () => { + const editor = new Editor(createTestTUI(), defaultEditorTheme); + + editor.handleInput("h"); + editor.handleInput("e"); + editor.handleInput("l"); + editor.handleInput("l"); + editor.handleInput("o"); + editor.handleInput(" "); + editor.handleInput("\x17"); // Ctrl+W - delete "hello " + editor.handleInput("\x19"); // Ctrl+Y - yank + assert.strictEqual(editor.getText(), "hello "); + + editor.handleInput("\x1b[45;5u"); // Ctrl+- (undo) + assert.strictEqual(editor.getText(), ""); + }); + + it("undoes single-line paste atomically", () => { + const editor = new Editor(createTestTUI(), defaultEditorTheme); + + editor.setText("hello world"); + editor.handleInput("\x01"); // Ctrl+A - go to start + for (let i = 0; i < 5; i++) editor.handleInput("\x1b[C"); // Move right 5 (after "hello", before space) + + // Simulate bracketed paste of "beep boop" + editor.handleInput("\x1b[200~beep boop\x1b[201~"); + assert.strictEqual(editor.getText(), "hellobeep boop world"); + + // Single undo should restore entire pre-paste state + editor.handleInput("\x1b[45;5u"); // Ctrl+- (undo) + assert.strictEqual(editor.getText(), "hello world"); + + editor.handleInput("|"); + assert.strictEqual(editor.getText(), "hello| world"); + }); + + it("does not trigger autocomplete during single-line paste", () => { + const editor = new Editor(createTestTUI(), defaultEditorTheme); + let suggestionCalls = 0; + + const mockProvider: AutocompleteProvider = { + getSuggestions: async () => { + suggestionCalls += 1; + return null; + }, + applyCompletion, + }; + + editor.setAutocompleteProvider(mockProvider); + editor.handleInput("\x1b[200~look at @node_modules/react/index.js please\x1b[201~"); + + assert.strictEqual(editor.getText(), "look at @node_modules/react/index.js please"); + assert.strictEqual(suggestionCalls, 0); + assert.strictEqual(editor.isShowingAutocomplete(), false); + }); + + it("decodes CSI-u Ctrl+letter sequences inside bracketed paste (tmux popup)", () => { + const editor = new Editor(createTestTUI(), defaultEditorTheme); + + // tmux popups with extended-keys-format=csi-u re-encode \n in pastes as + // \x1b[106;5u (Ctrl+J). Without decoding, the per-char filter strips ESC + // and leaks "[106;5u" between lines. See issue #3599. + editor.handleInput("\x1b[200~line1\x1b[106;5uline2\x1b[106;5uline3\x1b[201~"); + assert.strictEqual(editor.getText(), "line1\nline2\nline3"); + }); + + it("undoes multi-line paste atomically", () => { + const editor = new Editor(createTestTUI(), defaultEditorTheme); + + editor.setText("hello world"); + editor.handleInput("\x01"); // Ctrl+A - go to start + for (let i = 0; i < 5; i++) editor.handleInput("\x1b[C"); // Move right 5 (after "hello", before space) + + // Simulate bracketed paste of multi-line text + editor.handleInput("\x1b[200~line1\nline2\nline3\x1b[201~"); + assert.strictEqual(editor.getText(), "helloline1\nline2\nline3 world"); + + // Single undo should restore entire pre-paste state + editor.handleInput("\x1b[45;5u"); // Ctrl+- (undo) + assert.strictEqual(editor.getText(), "hello world"); + + editor.handleInput("|"); + assert.strictEqual(editor.getText(), "hello| world"); + }); + + it("undoes insertTextAtCursor atomically", () => { + const editor = new Editor(createTestTUI(), defaultEditorTheme); + + editor.setText("hello world"); + editor.handleInput("\x01"); // Ctrl+A - go to start + for (let i = 0; i < 5; i++) editor.handleInput("\x1b[C"); // Move right 5 (after "hello", before space) + + // Programmatic insertion (e.g., clipboard image path) + editor.insertTextAtCursor("/tmp/image.png"); + assert.strictEqual(editor.getText(), "hello/tmp/image.png world"); + + // Single undo should restore entire pre-insert state + editor.handleInput("\x1b[45;5u"); // Ctrl+- (undo) + assert.strictEqual(editor.getText(), "hello world"); + + editor.handleInput("|"); + assert.strictEqual(editor.getText(), "hello| world"); + }); + + it("insertTextAtCursor handles multiline text", () => { + const editor = new Editor(createTestTUI(), defaultEditorTheme); + + editor.setText("hello world"); + editor.handleInput("\x01"); // Ctrl+A - go to start + for (let i = 0; i < 5; i++) editor.handleInput("\x1b[C"); // Move right 5 (after "hello", before space) + + // Insert multiline text + editor.insertTextAtCursor("line1\nline2\nline3"); + assert.strictEqual(editor.getText(), "helloline1\nline2\nline3 world"); + + // Cursor should be at end of inserted text (after "line3", before " world") + const cursor = editor.getCursor(); + assert.strictEqual(cursor.line, 2); + assert.strictEqual(cursor.col, 5); // "line3".length + + // Single undo should restore entire pre-insert state + editor.handleInput("\x1b[45;5u"); // Ctrl+- (undo) + assert.strictEqual(editor.getText(), "hello world"); + }); + + it("insertTextAtCursor normalizes CRLF and CR line endings", () => { + const editor = new Editor(createTestTUI(), defaultEditorTheme); + + editor.setText(""); + + // Insert text with CRLF + editor.insertTextAtCursor("a\r\nb\r\nc"); + assert.strictEqual(editor.getText(), "a\nb\nc"); + + editor.handleInput("\x1b[45;5u"); // Undo + assert.strictEqual(editor.getText(), ""); + + // Insert text with CR only + editor.insertTextAtCursor("x\ry\rz"); + assert.strictEqual(editor.getText(), "x\ny\nz"); + }); + + it("undoes setText to empty string", () => { + const editor = new Editor(createTestTUI(), defaultEditorTheme); + + editor.handleInput("h"); + editor.handleInput("e"); + editor.handleInput("l"); + editor.handleInput("l"); + editor.handleInput("o"); + editor.handleInput(" "); + editor.handleInput("w"); + editor.handleInput("o"); + editor.handleInput("r"); + editor.handleInput("l"); + editor.handleInput("d"); + assert.strictEqual(editor.getText(), "hello world"); + + editor.setText(""); + assert.strictEqual(editor.getText(), ""); + + editor.handleInput("\x1b[45;5u"); // Ctrl+- (undo) + assert.strictEqual(editor.getText(), "hello world"); + }); + + it("clears undo stack on submit", () => { + const editor = new Editor(createTestTUI(), defaultEditorTheme); + let submitted = ""; + editor.onSubmit = (text) => { + submitted = text; + }; + + editor.handleInput("h"); + editor.handleInput("e"); + editor.handleInput("l"); + editor.handleInput("l"); + editor.handleInput("o"); + editor.handleInput("\r"); // Enter - submit + + assert.strictEqual(submitted, "hello"); + assert.strictEqual(editor.getText(), ""); + + // Undo should do nothing - stack was cleared + editor.handleInput("\x1b[45;5u"); // Ctrl+- (undo) + assert.strictEqual(editor.getText(), ""); + }); + + it("exits history browsing mode on undo", () => { + const editor = new Editor(createTestTUI(), defaultEditorTheme); + + // Add "hello" to history + editor.addToHistory("hello"); + assert.strictEqual(editor.getText(), ""); + + // Type "world" + editor.handleInput("w"); + editor.handleInput("o"); + editor.handleInput("r"); + editor.handleInput("l"); + editor.handleInput("d"); + assert.strictEqual(editor.getText(), "world"); + + // Ctrl+W - delete word + editor.handleInput("\x17"); // Ctrl+W + assert.strictEqual(editor.getText(), ""); + + // Press Up - enter history browsing, shows "hello" + editor.handleInput("\x1b[A"); // Up arrow + assert.strictEqual(editor.getText(), "hello"); + + // Undo should restore to "" (state before entering history browsing) + editor.handleInput("\x1b[45;5u"); // Ctrl+- (undo) + assert.strictEqual(editor.getText(), ""); + + // Undo again should restore to "world" (state before Ctrl+W) + editor.handleInput("\x1b[45;5u"); // Ctrl+- (undo) + assert.strictEqual(editor.getText(), "world"); + }); + + it("undo restores to pre-history state even after multiple history navigations", () => { + const editor = new Editor(createTestTUI(), defaultEditorTheme); + + // Add history entries + editor.addToHistory("first"); + editor.addToHistory("second"); + editor.addToHistory("third"); + + // Type something + editor.handleInput("c"); + editor.handleInput("u"); + editor.handleInput("r"); + editor.handleInput("r"); + editor.handleInput("e"); + editor.handleInput("n"); + editor.handleInput("t"); + assert.strictEqual(editor.getText(), "current"); + + // Clear editor + editor.handleInput("\x17"); // Ctrl+W + assert.strictEqual(editor.getText(), ""); + + // Navigate through history multiple times + editor.handleInput("\x1b[A"); // Up - "third" + assert.strictEqual(editor.getText(), "third"); + editor.handleInput("\x1b[A"); // Up - "second" + assert.strictEqual(editor.getText(), "second"); + editor.handleInput("\x1b[A"); // Up - "first" + assert.strictEqual(editor.getText(), "first"); + + // Undo should go back to "" (state before we started browsing), not intermediate states + editor.handleInput("\x1b[45;5u"); // Ctrl+- (undo) + assert.strictEqual(editor.getText(), ""); + + // Another undo goes back to "current" + editor.handleInput("\x1b[45;5u"); // Ctrl+- (undo) + assert.strictEqual(editor.getText(), "current"); + }); + + it("cursor movement starts new undo unit", () => { + const editor = new Editor(createTestTUI(), defaultEditorTheme); + + editor.handleInput("h"); + editor.handleInput("e"); + editor.handleInput("l"); + editor.handleInput("l"); + editor.handleInput("o"); + editor.handleInput(" "); + editor.handleInput("w"); + editor.handleInput("o"); + editor.handleInput("r"); + editor.handleInput("l"); + editor.handleInput("d"); + assert.strictEqual(editor.getText(), "hello world"); + + // Move cursor left 5 (to after "hello ") + for (let i = 0; i < 5; i++) editor.handleInput("\x1b[D"); + + // Type "lol" in the middle + editor.handleInput("l"); + editor.handleInput("o"); + editor.handleInput("l"); + assert.strictEqual(editor.getText(), "hello lolworld"); + + // Undo should restore to "hello world" (before inserting "lol") + editor.handleInput("\x1b[45;5u"); // Ctrl+- (undo) + assert.strictEqual(editor.getText(), "hello world"); + + editor.handleInput("|"); + assert.strictEqual(editor.getText(), "hello |world"); + }); + + it("no-op delete operations do not push undo snapshots", () => { + const editor = new Editor(createTestTUI(), defaultEditorTheme); + + editor.handleInput("h"); + editor.handleInput("e"); + editor.handleInput("l"); + editor.handleInput("l"); + editor.handleInput("o"); + assert.strictEqual(editor.getText(), "hello"); + + // Delete word on empty - multiple times (should be no-ops) + editor.handleInput("\x17"); // Ctrl+W - deletes "hello" + assert.strictEqual(editor.getText(), ""); + editor.handleInput("\x17"); // Ctrl+W - no-op (nothing to delete) + editor.handleInput("\x17"); // Ctrl+W - no-op + + // Single undo should restore "hello" + editor.handleInput("\x1b[45;5u"); // Ctrl+- (undo) + assert.strictEqual(editor.getText(), "hello"); + }); + + it("undoes autocomplete", async () => { + const editor = new Editor(createTestTUI(), defaultEditorTheme); + + // Create a mock autocomplete provider + const mockProvider: AutocompleteProvider = { + getSuggestions: async (lines, _cursorLine, cursorCol) => { + const text = lines[0] || ""; + const prefix = text.slice(0, cursorCol); + if (prefix === "di") { + return { + items: [{ value: "dist/", label: "dist/" }], + prefix: "di", + }; + } + return null; + }, + applyCompletion, + }; + + editor.setAutocompleteProvider(mockProvider); + + // Type "di" + editor.handleInput("d"); + editor.handleInput("i"); + assert.strictEqual(editor.getText(), "di"); + + // Press Tab to trigger autocomplete + editor.handleInput("\t"); + await flushAutocomplete(); + assert.strictEqual(editor.getText(), "dist/"); + assert.strictEqual(editor.isShowingAutocomplete(), false); + + // Undo should restore to "di" + editor.handleInput("\x1b[45;5u"); // Ctrl+- (undo) + assert.strictEqual(editor.getText(), "di"); + }); + }); + + describe("Autocomplete", () => { + it("auto-applies single force-file suggestion without showing menu", async () => { + const editor = new Editor(createTestTUI(), defaultEditorTheme); + + const mockProvider: AutocompleteProvider = { + getSuggestions: async (lines, _cursorLine, cursorCol, options) => { + if (!options.force) { + return null; + } + const text = lines[0] || ""; + const prefix = text.slice(0, cursorCol); + if (prefix === "Work") { + return { + items: [{ value: "Workspace/", label: "Workspace/" }], + prefix: "Work", + }; + } + return null; + }, + applyCompletion, + }; + + editor.setAutocompleteProvider(mockProvider); + + // Type "Work" + editor.handleInput("W"); + editor.handleInput("o"); + editor.handleInput("r"); + editor.handleInput("k"); + assert.strictEqual(editor.getText(), "Work"); + + // Press Tab - should auto-apply without showing menu + editor.handleInput("\t"); + await flushAutocomplete(); + assert.strictEqual(editor.getText(), "Workspace/"); + assert.strictEqual(editor.isShowingAutocomplete(), false); + + // Undo should restore to "Work" + editor.handleInput("\x1b[45;5u"); // Ctrl+- (undo) + assert.strictEqual(editor.getText(), "Work"); + }); + + it("shows menu when force-file has multiple suggestions", async () => { + const editor = new Editor(createTestTUI(), defaultEditorTheme); + + const mockProvider: AutocompleteProvider = { + getSuggestions: async (lines, _cursorLine, cursorCol, options) => { + if (!options.force) { + return null; + } + const text = lines[0] || ""; + const prefix = text.slice(0, cursorCol); + if (prefix === "src") { + return { + items: [ + { value: "src/", label: "src/" }, + { value: "src.txt", label: "src.txt" }, + ], + prefix: "src", + }; + } + return null; + }, + applyCompletion, + }; + + editor.setAutocompleteProvider(mockProvider); + + // Type "src" + editor.handleInput("s"); + editor.handleInput("r"); + editor.handleInput("c"); + assert.strictEqual(editor.getText(), "src"); + + // Press Tab - should show menu because there are multiple suggestions + editor.handleInput("\t"); + await flushAutocomplete(); + assert.strictEqual(editor.getText(), "src"); + assert.strictEqual(editor.isShowingAutocomplete(), true); + + // Press Tab again to accept first suggestion + editor.handleInput("\t"); + assert.strictEqual(editor.getText(), "src/"); + assert.strictEqual(editor.isShowingAutocomplete(), false); + }); + + it("keeps suggestions open when typing in force mode (Tab-triggered)", async () => { + const editor = new Editor(createTestTUI(), defaultEditorTheme); + + const allFiles = [ + { value: "readme.md", label: "readme.md" }, + { value: "package.json", label: "package.json" }, + { value: "src/", label: "src/" }, + { value: "dist/", label: "dist/" }, + ]; + + const mockProvider: AutocompleteProvider = { + getSuggestions: async (lines, _cursorLine, cursorCol, options) => { + const text = lines[0] || ""; + const prefix = text.slice(0, cursorCol); + const shouldMatch = options.force || prefix.includes("/") || prefix.startsWith("."); + if (!shouldMatch) { + return null; + } + const filtered = allFiles.filter((f) => f.value.toLowerCase().startsWith(prefix.toLowerCase())); + if (filtered.length > 0) { + return { items: filtered, prefix }; + } + return null; + }, + applyCompletion, + }; + + editor.setAutocompleteProvider(mockProvider); + + // Press Tab on empty prompt - should show all files (force mode) + editor.handleInput("\t"); + await flushAutocomplete(); + assert.strictEqual(editor.isShowingAutocomplete(), true); + + // Type "r" - should narrow to "readme.md" (force mode keeps suggestions open) + editor.handleInput("r"); + await flushAutocomplete(); + assert.strictEqual(editor.getText(), "r"); + assert.strictEqual(editor.isShowingAutocomplete(), true); + + // Type "e" - should still show "readme.md" + editor.handleInput("e"); + await flushAutocomplete(); + assert.strictEqual(editor.getText(), "re"); + assert.strictEqual(editor.isShowingAutocomplete(), true); + + // Accept with Tab + editor.handleInput("\t"); + assert.strictEqual(editor.getText(), "readme.md"); + assert.strictEqual(editor.isShowingAutocomplete(), false); + }); + + it("debounces @ autocomplete while typing", async () => { + const editor = new Editor(createTestTUI(), defaultEditorTheme); + let suggestionCalls = 0; + + const mockProvider: AutocompleteProvider = { + getSuggestions: async (lines, _cursorLine, cursorCol) => { + suggestionCalls += 1; + const text = (lines[0] || "").slice(0, cursorCol); + return { + items: [{ value: "@main.ts", label: "main.ts" }], + prefix: text, + }; + }, + applyCompletion, + }; + + editor.setAutocompleteProvider(mockProvider); + + editor.handleInput("@"); + editor.handleInput("m"); + editor.handleInput("a"); + editor.handleInput("i"); + + assert.strictEqual(suggestionCalls, 0); + assert.strictEqual(editor.isShowingAutocomplete(), false); + + await new Promise((resolve) => setTimeout(resolve, 50)); + await flushAutocomplete(); + + assert.strictEqual(suggestionCalls, 1); + assert.strictEqual(editor.isShowingAutocomplete(), true); + }); + + it("re-queries the autocomplete picker when the cursor moves back into the command name", async () => { + // Regression for earendil-works/pi#5496: arrowing left out of a slash + // command's argument region must re-query the picker, not leave the + // stale argument list showing. Before the fix, moveCursor() never + // called updateAutocomplete(), so `/cmd ` (argument menu) + Left kept + // displaying the arguments against a `/cmd` prefix — and a Tab there + // would concatenate the stale suggestion onto the partial command name. + const editor = new Editor(createTestTUI(), defaultEditorTheme); + + const mockProvider: AutocompleteProvider = { + getSuggestions: async (lines, _cursorLine, cursorCol) => { + const before = (lines[0] || "").slice(0, cursorCol); + if (!before.startsWith("/")) return null; + // Past the command name (a space before the cursor): offer arguments. + if (before.includes(" ")) { + return { + items: [ + { value: "repo", label: "repo" }, + { value: "message", label: "message" }, + { value: "help", label: "help" }, + ], + prefix: before.slice(before.indexOf(" ") + 1), + }; + } + // Inside the command name: offer the command name only. + return { items: [{ value: "cmd", label: "cmd" }], prefix: before }; + }, + applyCompletion, + }; + + editor.setAutocompleteProvider(mockProvider); + + // Type `/cmd ` so the picker ends up showing the argument list. + for (const ch of "/cmd ") { + editor.handleInput(ch); + await flushAutocomplete(); + } + assert.strictEqual(editor.getText(), "/cmd "); + assert.strictEqual(editor.isShowingAutocomplete(), true); + const atArg = editor + .render(80) + .map((l) => stripVTControlCharacters(l)) + .join("\n"); + assert.ok(atArg.includes("repo"), "argument menu should be visible at `/cmd `"); + + // Arrow Left back into the command name (`/cmd`). + editor.handleInput("\x1b[D"); + await flushAutocomplete(); + + // The picker must have re-queried: the stale argument items are gone + // (replaced by the command-name suggestion, or the picker closed). + const afterMove = editor + .render(80) + .map((l) => stripVTControlCharacters(l)) + .join("\n"); + assert.ok(!afterMove.includes("repo"), "stale argument menu must not survive the cursor move"); + assert.ok(!afterMove.includes("message"), "stale argument menu must not survive the cursor move"); + }); + + it("debounces # autocomplete while typing", async () => { + const editor = new Editor(createTestTUI(), defaultEditorTheme); + let suggestionCalls = 0; + + const mockProvider: AutocompleteProvider = { + getSuggestions: async (lines, _cursorLine, cursorCol) => { + suggestionCalls += 1; + const text = (lines[0] || "").slice(0, cursorCol); + return { + items: [{ value: "#2983", label: "#2983" }], + prefix: text, + }; + }, + applyCompletion, + }; + + editor.setAutocompleteProvider(mockProvider); + + editor.handleInput("#"); + editor.handleInput("2"); + editor.handleInput("9"); + editor.handleInput("8"); + + assert.strictEqual(suggestionCalls, 0); + assert.strictEqual(editor.isShowingAutocomplete(), false); + + await new Promise((resolve) => setTimeout(resolve, 50)); + await flushAutocomplete(); + + assert.strictEqual(suggestionCalls, 1); + assert.strictEqual(editor.isShowingAutocomplete(), true); + }); + + it("debounces custom triggerCharacters autocomplete while typing", async () => { + const editor = new Editor(createTestTUI(), defaultEditorTheme); + let suggestionCalls = 0; + + editor.setAutocompleteProvider({ + triggerCharacters: ["$"], + getSuggestions: async (lines, _cursorLine, cursorCol) => { + suggestionCalls += 1; + const prefix = (lines[0] || "").slice(0, cursorCol); + return { items: [{ value: "$skill-name", label: "skill-name" }], prefix }; + }, + applyCompletion, + }); + + editor.handleInput("$"); + editor.handleInput("s"); + editor.handleInput("k"); + + assert.strictEqual(suggestionCalls, 0); + await new Promise((resolve) => setTimeout(resolve, 50)); + await flushAutocomplete(); + + assert.strictEqual(suggestionCalls, 1); + assert.strictEqual(editor.isShowingAutocomplete(), true); + }); + + it("resets custom triggerCharacters when provider changes", async () => { + const editor = new Editor(createTestTUI(), defaultEditorTheme); + let suggestionCalls = 0; + + editor.setAutocompleteProvider({ + triggerCharacters: ["$"], + getSuggestions: async () => ({ items: [{ value: "$skill-name", label: "skill-name" }], prefix: "$" }), + applyCompletion, + }); + editor.setAutocompleteProvider({ + getSuggestions: async () => { + suggestionCalls += 1; + return { items: [{ value: "$skill-name", label: "skill-name" }], prefix: "$" }; + }, + applyCompletion, + }); + + editor.handleInput("$"); + editor.handleInput("s"); + await new Promise((resolve) => setTimeout(resolve, 50)); + await flushAutocomplete(); + + assert.strictEqual(suggestionCalls, 0); + assert.strictEqual(editor.isShowingAutocomplete(), false); + }); + + it("aborts active @ autocomplete when typing continues", async () => { + const editor = new Editor(createTestTUI(), defaultEditorTheme); + let aborts = 0; + + const mockProvider: AutocompleteProvider = { + getSuggestions: async (_lines, _cursorLine, _cursorCol, options) => { + return await new Promise((resolve) => { + const timeout = setTimeout(() => { + resolve({ items: [{ value: "@main.ts", label: "main.ts" }], prefix: "@main" }); + }, 500); + options.signal.addEventListener( + "abort", + () => { + aborts += 1; + clearTimeout(timeout); + resolve(null); + }, + { once: true }, + ); + }); + }, + applyCompletion, + }; + + editor.setAutocompleteProvider(mockProvider); + + editor.handleInput("@"); + editor.handleInput("m"); + editor.handleInput("a"); + editor.handleInput("i"); + await new Promise((resolve) => setTimeout(resolve, 250)); + editor.handleInput("n"); + await new Promise((resolve) => setTimeout(resolve, 50)); + + assert.strictEqual(aborts, 1); + }); + + it("hides autocomplete when backspacing slash command to empty", async () => { + const editor = new Editor(createTestTUI(), defaultEditorTheme); + + // Mock provider with slash commands + const mockProvider: AutocompleteProvider = { + getSuggestions: async (lines, _cursorLine, cursorCol) => { + const text = lines[0] || ""; + const prefix = text.slice(0, cursorCol); + // Only return slash command suggestions when line starts with / + if (prefix.startsWith("/")) { + const commands = [ + { value: "/model", label: "model", description: "Change model" }, + { value: "/help", label: "help", description: "Show help" }, + ]; + const query = prefix.slice(1); // Remove leading / + const filtered = commands.filter((c) => c.value.startsWith(query)); + if (filtered.length > 0) { + return { items: filtered, prefix }; + } + } + return null; + }, + applyCompletion, + }; + + editor.setAutocompleteProvider(mockProvider); + + // Type "/" - should show slash command suggestions + editor.handleInput("/"); + await flushAutocomplete(); + assert.strictEqual(editor.getText(), "/"); + assert.strictEqual(editor.isShowingAutocomplete(), true); + + // Backspace to delete "/" - should hide autocomplete completely + editor.handleInput("\x7f"); // Backspace + await flushAutocomplete(); + assert.strictEqual(editor.getText(), ""); + assert.strictEqual(editor.isShowingAutocomplete(), false); + }); + + it("applies exact typed slash-argument value on Enter even when first item is highlighted", async () => { + const editor = new Editor(createTestTUI(), defaultEditorTheme); + + // Mock provider for /argtest command with argument completions + const mockProvider: AutocompleteProvider = { + getSuggestions: async (lines, _cursorLine, cursorCol) => { + const text = lines[0] || ""; + const beforeCursor = text.slice(0, cursorCol); + + // Check if we're in argument completion context: "/argtest " + const argtestMatch = beforeCursor.match(/^\/argtest\s+(\S+)$/); + if (argtestMatch) { + const argumentText = argtestMatch[1]!; + const allArguments = [ + { value: "one", label: "one" }, + { value: "two", label: "two" }, + { value: "three", label: "three" }, + ]; + // Return all arguments that start with the typed prefix + const filtered = allArguments.filter((arg) => arg.value.startsWith(argumentText)); + if (filtered.length > 0) { + return { items: filtered, prefix: argumentText }; + } + } + return null; + }, + applyCompletion, + }; + + editor.setAutocompleteProvider(mockProvider); + + // Type "/argtest two" + editor.handleInput("/"); + editor.handleInput("a"); + editor.handleInput("r"); + editor.handleInput("g"); + editor.handleInput("t"); + editor.handleInput("e"); + editor.handleInput("s"); + editor.handleInput("t"); + editor.handleInput(" "); + editor.handleInput("t"); + editor.handleInput("w"); + editor.handleInput("o"); + + assert.strictEqual(editor.getText(), "/argtest two"); + await flushAutocomplete(); + assert.strictEqual(editor.isShowingAutocomplete(), true); + + // Press Enter - should apply the exact typed value "two", not the first item + editor.handleInput("\r"); + + // The exact typed value "two" should be retained + assert.strictEqual(editor.getText(), "/argtest two"); + }); + + it("selects first prefix match on Enter when typed arg is not exact match", async () => { + const editor = new Editor(createTestTUI(), defaultEditorTheme); + + // Mock provider for /argtest command with argument completions + const mockProvider: AutocompleteProvider = { + getSuggestions: async (lines, _cursorLine, cursorCol) => { + const text = lines[0] || ""; + const beforeCursor = text.slice(0, cursorCol); + + // Check if we're in argument completion context + const argtestMatch = beforeCursor.match(/^\/argtest\s+(\S+)$/); + if (argtestMatch) { + const argumentText = argtestMatch[1]!; + const allArguments = [ + { value: "two", label: "two" }, + { value: "three", label: "three" }, + { value: "twelve", label: "twelve" }, + ]; + // Return all items that start with the typed prefix + const filtered = allArguments.filter((arg) => arg.value.startsWith(argumentText)); + if (filtered.length > 0) { + return { items: filtered, prefix: argumentText }; + } + } + return null; + }, + applyCompletion, + }; + + editor.setAutocompleteProvider(mockProvider); + + // Type "/argtest t" - filtered to [two, three, twelve], prefix "t" matches "two" first + editor.handleInput("/"); + editor.handleInput("a"); + editor.handleInput("r"); + editor.handleInput("g"); + editor.handleInput("t"); + editor.handleInput("e"); + editor.handleInput("s"); + editor.handleInput("t"); + editor.handleInput(" "); + editor.handleInput("t"); + + await flushAutocomplete(); + assert.strictEqual(editor.isShowingAutocomplete(), true); + + // Press Enter - "t" prefix matches "two" (first in list), so "two" is applied + editor.handleInput("\r"); + assert.strictEqual(editor.getText(), "/argtest two"); + }); + + it("highlights unique prefix match as user types (before full exact match)", async () => { + const editor = new Editor(createTestTUI(), defaultEditorTheme); + + // Mock provider that returns all items unfiltered (like real extensions do) + const mockProvider: AutocompleteProvider = { + getSuggestions: async (lines, _cursorLine, cursorCol) => { + const text = lines[0] || ""; + const beforeCursor = text.slice(0, cursorCol); + + const argtestMatch = beforeCursor.match(/^\/argtest\s+(\S+)$/); + if (argtestMatch) { + const argumentText = argtestMatch[1]!; + // Return all items - provider does not filter + const allArguments = [ + { value: "one", label: "one" }, + { value: "two", label: "two" }, + { value: "three", label: "three" }, + ]; + return { items: allArguments, prefix: argumentText }; + } + return null; + }, + applyCompletion, + }; + + editor.setAutocompleteProvider(mockProvider); + + // Type "/argtest tw" - "tw" is a prefix of only "two" + editor.handleInput("/"); + editor.handleInput("a"); + editor.handleInput("r"); + editor.handleInput("g"); + editor.handleInput("t"); + editor.handleInput("e"); + editor.handleInput("s"); + editor.handleInput("t"); + editor.handleInput(" "); + editor.handleInput("t"); + editor.handleInput("w"); + + assert.strictEqual(editor.getText(), "/argtest tw"); + await flushAutocomplete(); + assert.strictEqual(editor.isShowingAutocomplete(), true); + + // Press Enter - "tw" uniquely matches "two", so "two" should be applied + editor.handleInput("\r"); + assert.strictEqual(editor.getText(), "/argtest two"); + }); + + it("selects first prefix match when multiple items match", async () => { + const editor = new Editor(createTestTUI(), defaultEditorTheme); + + // Mock provider that returns all items unfiltered + const mockProvider: AutocompleteProvider = { + getSuggestions: async (lines, _cursorLine, cursorCol) => { + const text = lines[0] || ""; + const beforeCursor = text.slice(0, cursorCol); + + const argtestMatch = beforeCursor.match(/^\/argtest\s+(\S+)$/); + if (argtestMatch) { + const argumentText = argtestMatch[1]!; + const allArguments = [ + { value: "one", label: "one" }, + { value: "two", label: "two" }, + { value: "three", label: "three" }, + ]; + return { items: allArguments, prefix: argumentText }; + } + return null; + }, + applyCompletion, + }; + + editor.setAutocompleteProvider(mockProvider); + + // Type "/argtest t" - "t" is a prefix of both "two" and "three" + editor.handleInput("/"); + editor.handleInput("a"); + editor.handleInput("r"); + editor.handleInput("g"); + editor.handleInput("t"); + editor.handleInput("e"); + editor.handleInput("s"); + editor.handleInput("t"); + editor.handleInput(" "); + editor.handleInput("t"); + + await flushAutocomplete(); + assert.strictEqual(editor.isShowingAutocomplete(), true); + + // Press Enter - "t" matches "two" first, so "two" is selected + editor.handleInput("\r"); + assert.strictEqual(editor.getText(), "/argtest two"); + }); + + it("works for built-in-style command argument completion path (model-like)", async () => { + const editor = new Editor(createTestTUI(), defaultEditorTheme); + + // Mock provider for /model command with model completions + const mockProvider: AutocompleteProvider = { + getSuggestions: async (lines, _cursorLine, cursorCol) => { + const text = lines[0] || ""; + const beforeCursor = text.slice(0, cursorCol); + + // Check if we're in /model argument completion context + // Use [^ ]+ to match any non-space characters (including hyphens) + const modelMatch = beforeCursor.match(/^\/model\s+(\S+)$/); + if (modelMatch) { + const modelText = modelMatch[1]!; + const allModels = [ + { value: "gpt-4o", label: "gpt-4o" }, + { value: "gpt-4o-mini", label: "gpt-4o-mini" }, + { value: "claude-sonnet", label: "claude-sonnet" }, + ]; + // Return all models that start with the typed prefix + const filtered = allModels.filter((m) => m.value.startsWith(modelText)); + if (filtered.length > 0) { + return { items: filtered, prefix: modelText }; + } + } + return null; + }, + applyCompletion, + }; + + editor.setAutocompleteProvider(mockProvider); + + // Type "/model gpt-4o-mini" - exact match for second item in list + editor.handleInput("/"); + editor.handleInput("m"); + editor.handleInput("o"); + editor.handleInput("d"); + editor.handleInput("e"); + editor.handleInput("l"); + editor.handleInput(" "); + editor.handleInput("g"); + editor.handleInput("p"); + editor.handleInput("t"); + editor.handleInput("-"); + editor.handleInput("4"); + editor.handleInput("o"); + editor.handleInput("-"); + editor.handleInput("m"); + editor.handleInput("i"); + editor.handleInput("n"); + editor.handleInput("i"); + + assert.strictEqual(editor.getText(), "/model gpt-4o-mini"); + await flushAutocomplete(); + assert.strictEqual(editor.isShowingAutocomplete(), true); + + // Press Enter - should retain exact typed value, not apply first highlighted item + editor.handleInput("\r"); + + // The exact typed value should be retained + assert.strictEqual(editor.getText(), "/model gpt-4o-mini"); + }); + + it("awaits async slash command argument completions", async () => { + const editor = new Editor(createTestTUI(), defaultEditorTheme); + const provider = new CombinedAutocompleteProvider( + [ + { + name: "load-skills", + description: "Load skills", + getArgumentCompletions: async (prefix) => + prefix.startsWith("s") ? [{ value: "skill-a", label: "skill-a" }] : null, + }, + ], + process.cwd(), + ); + editor.setAutocompleteProvider(provider); + editor.setText("/load-skills "); + + editor.handleInput("s"); + await flushAutocomplete(); + assert.strictEqual(editor.isShowingAutocomplete(), true); + + editor.handleInput("\t"); + assert.strictEqual(editor.getText(), "/load-skills skill-a"); + assert.strictEqual(editor.isShowingAutocomplete(), false); + }); + + it("ignores invalid slash command argument completion results", async () => { + const editor = new Editor(createTestTUI(), defaultEditorTheme); + const provider = new CombinedAutocompleteProvider( + [ + { + name: "load-skills", + description: "Load skills", + getArgumentCompletions: (() => "not-an-array") as unknown as ( + argumentPrefix: string, + ) => Promise<{ value: string; label: string }[] | null>, + }, + ], + process.cwd(), + ); + editor.setAutocompleteProvider(provider); + editor.setText("/load-skills "); + + editor.handleInput("s"); + await flushAutocomplete(); + assert.strictEqual(editor.isShowingAutocomplete(), false); + assert.strictEqual(editor.getText(), "/load-skills s"); + }); + + it("does not show argument completions when command has no argument completer", async () => { + const editor = new Editor(createTestTUI(), defaultEditorTheme); + const provider = new CombinedAutocompleteProvider( + [ + { name: "help", description: "Show help" }, + { + name: "model", + description: "Switch model", + getArgumentCompletions: () => [{ value: "claude-opus", label: "claude-opus" }], + }, + ], + process.cwd(), + ); + editor.setAutocompleteProvider(provider); + + editor.handleInput("/"); + editor.handleInput("h"); + editor.handleInput("e"); + await flushAutocomplete(); + assert.strictEqual(editor.isShowingAutocomplete(), true); + + editor.handleInput("\t"); + assert.strictEqual(editor.getText(), "/help "); + assert.strictEqual(editor.isShowingAutocomplete(), false); + }); + }); + + describe("Character jump (Ctrl+])", () => { + it("jumps forward to first occurrence of character on same line", () => { + const editor = new Editor(createTestTUI(), defaultEditorTheme); + + editor.setText("hello world"); + editor.handleInput("\x01"); // Ctrl+A - go to start + assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 0 }); + + editor.handleInput("\x1d"); // Ctrl+] (legacy sequence for ctrl+]) + editor.handleInput("o"); // Jump to first 'o' + + assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 4 }); // 'o' in "hello" + }); + + it("jumps forward to next occurrence after cursor", () => { + const editor = new Editor(createTestTUI(), defaultEditorTheme); + + editor.setText("hello world"); + editor.handleInput("\x01"); // Ctrl+A - go to start + // Move cursor to the 'o' in "hello" (col 4) + for (let i = 0; i < 4; i++) editor.handleInput("\x1b[C"); + assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 4 }); + + editor.handleInput("\x1d"); // Ctrl+] + editor.handleInput("o"); // Jump to next 'o' (in "world") + + assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 7 }); // 'o' in "world" + }); + + it("jumps forward across multiple lines", () => { + const editor = new Editor(createTestTUI(), defaultEditorTheme); + + editor.setText("abc\ndef\nghi"); + // Cursor is at end (line 2, col 3). Move to line 0 via up arrows, then Ctrl+A + editor.handleInput("\x1b[A"); // Up + editor.handleInput("\x1b[A"); // Up - now on line 0 + editor.handleInput("\x01"); // Ctrl+A - go to start of line + assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 0 }); + + editor.handleInput("\x1d"); // Ctrl+] + editor.handleInput("g"); // Jump to 'g' on line 3 + + assert.deepStrictEqual(editor.getCursor(), { line: 2, col: 0 }); + }); + + it("jumps backward to first occurrence before cursor on same line", () => { + const editor = new Editor(createTestTUI(), defaultEditorTheme); + + editor.setText("hello world"); + // Cursor at end (col 11) + assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 11 }); + + editor.handleInput("\x1b\x1d"); // Ctrl+Alt+] (ESC followed by Ctrl+]) + editor.handleInput("o"); // Jump to last 'o' before cursor + + assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 7 }); // 'o' in "world" + }); + + it("jumps backward across multiple lines", () => { + const editor = new Editor(createTestTUI(), defaultEditorTheme); + + editor.setText("abc\ndef\nghi"); + // Cursor at end of line 3 + assert.deepStrictEqual(editor.getCursor(), { line: 2, col: 3 }); + + editor.handleInput("\x1b\x1d"); // Ctrl+Alt+] + editor.handleInput("a"); // Jump to 'a' on line 1 + + assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 0 }); + }); + + it("does nothing when character is not found (forward)", () => { + const editor = new Editor(createTestTUI(), defaultEditorTheme); + + editor.setText("hello world"); + editor.handleInput("\x01"); // Ctrl+A - go to start + assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 0 }); + + editor.handleInput("\x1d"); // Ctrl+] + editor.handleInput("z"); // 'z' doesn't exist + + assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 0 }); // Cursor unchanged + }); + + it("does nothing when character is not found (backward)", () => { + const editor = new Editor(createTestTUI(), defaultEditorTheme); + + editor.setText("hello world"); + // Cursor at end + assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 11 }); + + editor.handleInput("\x1b\x1d"); // Ctrl+Alt+] + editor.handleInput("z"); // 'z' doesn't exist + + assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 11 }); // Cursor unchanged + }); + + it("is case-sensitive", () => { + const editor = new Editor(createTestTUI(), defaultEditorTheme); + + editor.setText("Hello World"); + editor.handleInput("\x01"); // Ctrl+A - go to start + assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 0 }); + + // Search for lowercase 'h' - should not find it (only 'H' exists) + editor.handleInput("\x1d"); // Ctrl+] + editor.handleInput("h"); + + assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 0 }); // Cursor unchanged + + // Search for uppercase 'W' - should find it + editor.handleInput("\x1d"); // Ctrl+] + editor.handleInput("W"); + + assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 6 }); // 'W' in "World" + }); + + it("cancels jump mode when Ctrl+] is pressed again", () => { + const editor = new Editor(createTestTUI(), defaultEditorTheme); + + editor.setText("hello world"); + editor.handleInput("\x01"); // Ctrl+A - go to start + assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 0 }); + + editor.handleInput("\x1d"); // Ctrl+] - enter jump mode + editor.handleInput("\x1d"); // Ctrl+] again - cancel + + // Type 'o' normally - should insert, not jump + editor.handleInput("o"); + assert.strictEqual(editor.getText(), "ohello world"); + }); + + it("cancels jump mode on Escape and processes the Escape", () => { + const editor = new Editor(createTestTUI(), defaultEditorTheme); + + editor.setText("hello world"); + editor.handleInput("\x01"); // Ctrl+A - go to start + assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 0 }); + + editor.handleInput("\x1d"); // Ctrl+] - enter jump mode + editor.handleInput("\x1b"); // Escape - cancel jump mode + + // Cursor should be unchanged (Escape itself doesn't move cursor in editor) + assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 0 }); + + // Type 'o' normally - should insert, not jump + editor.handleInput("o"); + assert.strictEqual(editor.getText(), "ohello world"); + }); + + it("cancels backward jump mode when Ctrl+Alt+] is pressed again", () => { + const editor = new Editor(createTestTUI(), defaultEditorTheme); + + editor.setText("hello world"); + // Cursor at end + assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 11 }); + + editor.handleInput("\x1b\x1d"); // Ctrl+Alt+] - enter backward jump mode + editor.handleInput("\x1b\x1d"); // Ctrl+Alt+] again - cancel + + // Type 'o' normally - should insert, not jump + editor.handleInput("o"); + assert.strictEqual(editor.getText(), "hello worldo"); + }); + + it("searches for special characters", () => { + const editor = new Editor(createTestTUI(), defaultEditorTheme); + + editor.setText("foo(bar) = baz;"); + editor.handleInput("\x01"); // Ctrl+A - go to start + assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 0 }); + + // Jump to '(' + editor.handleInput("\x1d"); // Ctrl+] + editor.handleInput("("); + + assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 3 }); + + // Jump to '=' + editor.handleInput("\x1d"); // Ctrl+] + editor.handleInput("="); + + assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 9 }); + }); + + it("handles empty text gracefully", () => { + const editor = new Editor(createTestTUI(), defaultEditorTheme); + + editor.setText(""); + assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 0 }); + + editor.handleInput("\x1d"); // Ctrl+] + editor.handleInput("x"); + + assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 0 }); // Cursor unchanged + }); + + it("resets lastAction when jumping", () => { + const editor = new Editor(createTestTUI(), defaultEditorTheme); + + editor.setText("hello world"); + editor.handleInput("\x01"); // Ctrl+A - go to start + + // Type to set lastAction to "type-word" + editor.handleInput("x"); + assert.strictEqual(editor.getText(), "xhello world"); + + // Jump forward + editor.handleInput("\x1d"); // Ctrl+] + editor.handleInput("o"); + + // Type more - should start a new undo unit (lastAction was reset) + editor.handleInput("Y"); + assert.strictEqual(editor.getText(), "xhellYo world"); + + // Undo should only undo "Y", not "x" as well + editor.handleInput("\x1b[45;5u"); // Ctrl+- (undo) + assert.strictEqual(editor.getText(), "xhello world"); + }); + }); + + describe("Sticky column", () => { + // Helper: position cursor at a specific line and column + function positionCursor(editor: Editor, line: number, col: number): void { + // Go to line 0 first + for (let i = 0; i < 20; i++) editor.handleInput("\x1b[A"); + // Go to target line + for (let i = 0; i < line; i++) editor.handleInput("\x1b[B"); + // Go to target col + editor.handleInput("\x01"); // Ctrl+A + for (let i = 0; i < col; i++) editor.handleInput("\x1b[C"); + } + + it("preserves target column when moving up through a shorter line", () => { + const editor = new Editor(createTestTUI(), defaultEditorTheme); + + // Line 0: "2222222222x222" (x at col 10) + // Line 1: "" (empty) + // Line 2: "1111111111_111111111111" (_ at col 10) + editor.setText("2222222222x222\n\n1111111111_111111111111"); + + // Position cursor on _ (line 2, col 10) + assert.deepStrictEqual(editor.getCursor(), { line: 2, col: 23 }); // At end + editor.handleInput("\x01"); // Ctrl+A - go to start of line + for (let i = 0; i < 10; i++) editor.handleInput("\x1b[C"); // Move right to col 10 + assert.deepStrictEqual(editor.getCursor(), { line: 2, col: 10 }); + + // Press Up - should move to empty line (col clamped to 0) + editor.handleInput("\x1b[A"); // Up arrow + assert.deepStrictEqual(editor.getCursor(), { line: 1, col: 0 }); + + // Press Up again - should move to line 0 at col 10 (on 'x') + editor.handleInput("\x1b[A"); // Up arrow + assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 10 }); + }); + + it("preserves target column when moving down through a shorter line", () => { + const editor = new Editor(createTestTUI(), defaultEditorTheme); + + editor.setText("1111111111_111\n\n2222222222x222222222222"); + + // Position cursor on _ (line 0, col 10) + editor.handleInput("\x1b[A"); // Up to line 1 + editor.handleInput("\x1b[A"); // Up to line 0 + editor.handleInput("\x01"); // Ctrl+A + for (let i = 0; i < 10; i++) editor.handleInput("\x1b[C"); + assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 10 }); + + // Press Down - should move to empty line (col clamped to 0) + editor.handleInput("\x1b[B"); // Down arrow + assert.deepStrictEqual(editor.getCursor(), { line: 1, col: 0 }); + + // Press Down again - should move to line 2 at col 10 (on 'x') + editor.handleInput("\x1b[B"); // Down arrow + assert.deepStrictEqual(editor.getCursor(), { line: 2, col: 10 }); + }); + + it("resets sticky column on horizontal movement (left arrow)", () => { + const editor = new Editor(createTestTUI(), defaultEditorTheme); + + editor.setText("1234567890\n\n1234567890"); + + // Start at line 2, col 5 + editor.handleInput("\x01"); // Ctrl+A + for (let i = 0; i < 5; i++) editor.handleInput("\x1b[C"); + assert.deepStrictEqual(editor.getCursor(), { line: 2, col: 5 }); + + // Move up through empty line + editor.handleInput("\x1b[A"); // Up - line 1, col 0 + editor.handleInput("\x1b[A"); // Up - line 0, col 5 (sticky) + assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 5 }); + + // Move left - resets sticky column + editor.handleInput("\x1b[D"); // Left + assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 4 }); + + // Move down twice + editor.handleInput("\x1b[B"); // Down - line 1, col 0 + editor.handleInput("\x1b[B"); // Down - line 2, col 4 (new sticky from col 4) + assert.deepStrictEqual(editor.getCursor(), { line: 2, col: 4 }); + }); + + it("resets sticky column on horizontal movement (right arrow)", () => { + const editor = new Editor(createTestTUI(), defaultEditorTheme); + + editor.setText("1234567890\n\n1234567890"); + + // Start at line 0, col 5 + editor.handleInput("\x1b[A"); // Up to line 1 + editor.handleInput("\x1b[A"); // Up to line 0 + editor.handleInput("\x01"); // Ctrl+A + for (let i = 0; i < 5; i++) editor.handleInput("\x1b[C"); + assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 5 }); + + // Move down through empty line + editor.handleInput("\x1b[B"); // Down - line 1, col 0 + editor.handleInput("\x1b[B"); // Down - line 2, col 5 (sticky) + assert.deepStrictEqual(editor.getCursor(), { line: 2, col: 5 }); + + // Move right - resets sticky column + editor.handleInput("\x1b[C"); // Right + assert.deepStrictEqual(editor.getCursor(), { line: 2, col: 6 }); + + // Move up twice + editor.handleInput("\x1b[A"); // Up - line 1, col 0 + editor.handleInput("\x1b[A"); // Up - line 0, col 6 (new sticky from col 6) + assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 6 }); + }); + + it("resets sticky column on typing", () => { + const editor = new Editor(createTestTUI(), defaultEditorTheme); + + editor.setText("1234567890\n\n1234567890"); + + // Start at line 2, col 8 + editor.handleInput("\x01"); // Ctrl+A + for (let i = 0; i < 8; i++) editor.handleInput("\x1b[C"); + + // Move up through empty line + editor.handleInput("\x1b[A"); // Up + editor.handleInput("\x1b[A"); // Up - line 0, col 8 + assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 8 }); + + // Type a character - resets sticky column + editor.handleInput("X"); + assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 9 }); + + // Move down twice + editor.handleInput("\x1b[B"); // Down - line 1, col 0 + editor.handleInput("\x1b[B"); // Down - line 2, col 9 (new sticky from col 9) + assert.deepStrictEqual(editor.getCursor(), { line: 2, col: 9 }); + }); + + it("resets sticky column on backspace", () => { + const editor = new Editor(createTestTUI(), defaultEditorTheme); + + editor.setText("1234567890\n\n1234567890"); + + // Start at line 2, col 8 + editor.handleInput("\x01"); // Ctrl+A + for (let i = 0; i < 8; i++) editor.handleInput("\x1b[C"); + + // Move up through empty line + editor.handleInput("\x1b[A"); // Up + editor.handleInput("\x1b[A"); // Up - line 0, col 8 + assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 8 }); + + // Backspace - resets sticky column + editor.handleInput("\x7f"); // Backspace + assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 7 }); + + // Move down twice + editor.handleInput("\x1b[B"); // Down - line 1, col 0 + editor.handleInput("\x1b[B"); // Down - line 2, col 7 (new sticky from col 7) + assert.deepStrictEqual(editor.getCursor(), { line: 2, col: 7 }); + }); + + it("resets sticky column on Ctrl+A (move to line start)", () => { + const editor = new Editor(createTestTUI(), defaultEditorTheme); + + editor.setText("1234567890\n\n1234567890"); + + // Start at line 2, col 8 + editor.handleInput("\x01"); // Ctrl+A + for (let i = 0; i < 8; i++) editor.handleInput("\x1b[C"); + + // Move up - establishes sticky col 8 + editor.handleInput("\x1b[A"); // Up - line 1, col 0 + + // Ctrl+A - resets sticky column to 0 + editor.handleInput("\x01"); // Ctrl+A + assert.deepStrictEqual(editor.getCursor(), { line: 1, col: 0 }); + + // Move up + editor.handleInput("\x1b[A"); // Up - line 0, col 0 (new sticky from col 0) + assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 0 }); + }); + + it("resets sticky column on Ctrl+E (move to line end)", () => { + const editor = new Editor(createTestTUI(), defaultEditorTheme); + + editor.setText("12345\n\n1234567890"); + + // Start at line 2, col 3 + editor.handleInput("\x01"); // Ctrl+A + for (let i = 0; i < 3; i++) editor.handleInput("\x1b[C"); + + // Move up through empty line - establishes sticky col 3 + editor.handleInput("\x1b[A"); // Up - line 1, col 0 + editor.handleInput("\x1b[A"); // Up - line 0, col 3 + assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 3 }); + + // Ctrl+E - resets sticky column to end + editor.handleInput("\x05"); // Ctrl+E + assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 5 }); + + // Move down twice + editor.handleInput("\x1b[B"); // Down - line 1, col 0 + editor.handleInput("\x1b[B"); // Down - line 2, col 5 (new sticky from col 5) + assert.deepStrictEqual(editor.getCursor(), { line: 2, col: 5 }); + }); + + it("resets sticky column on word movement (Ctrl+Left)", () => { + const editor = new Editor(createTestTUI(), defaultEditorTheme); + + editor.setText("hello world\n\nhello world"); + + // Start at end of line 2 (col 11) + assert.deepStrictEqual(editor.getCursor(), { line: 2, col: 11 }); + + // Move up through empty line - establishes sticky col 11 + editor.handleInput("\x1b[A"); // Up - line 1, col 0 + editor.handleInput("\x1b[A"); // Up - line 0, col 11 + assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 11 }); + + // Ctrl+Left - word movement resets sticky column + editor.handleInput("\x1b[1;5D"); // Ctrl+Left + assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 6 }); // Before "world" + + // Move down twice + editor.handleInput("\x1b[B"); // Down - line 1, col 0 + editor.handleInput("\x1b[B"); // Down - line 2, col 6 (new sticky from col 6) + assert.deepStrictEqual(editor.getCursor(), { line: 2, col: 6 }); + }); + + it("resets sticky column on word movement (Ctrl+Right)", () => { + const editor = new Editor(createTestTUI(), defaultEditorTheme); + + editor.setText("hello world\n\nhello world"); + + // Start at line 0, col 0 + editor.handleInput("\x1b[A"); // Up + editor.handleInput("\x1b[A"); // Up + editor.handleInput("\x01"); // Ctrl+A + assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 0 }); + + // Move down through empty line - establishes sticky col 0 + editor.handleInput("\x1b[B"); // Down - line 1, col 0 + editor.handleInput("\x1b[B"); // Down - line 2, col 0 + assert.deepStrictEqual(editor.getCursor(), { line: 2, col: 0 }); + + // Ctrl+Right - word movement resets sticky column + editor.handleInput("\x1b[1;5C"); // Ctrl+Right + assert.deepStrictEqual(editor.getCursor(), { line: 2, col: 5 }); // After "hello" + + // Move up twice + editor.handleInput("\x1b[A"); // Up - line 1, col 0 + editor.handleInput("\x1b[A"); // Up - line 0, col 5 (new sticky from col 5) + assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 5 }); + }); + + it("resets sticky column on undo", () => { + const editor = new Editor(createTestTUI(), defaultEditorTheme); + + editor.setText("1234567890\n\n1234567890"); + + // Go to line 0, col 8 + editor.handleInput("\x1b[A"); // Up to line 1 + editor.handleInput("\x1b[A"); // Up to line 0 + editor.handleInput("\x01"); // Ctrl+A + for (let i = 0; i < 8; i++) editor.handleInput("\x1b[C"); + assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 8 }); + + // Move down through empty line - establishes sticky col 8 + editor.handleInput("\x1b[B"); // Down - line 1, col 0 + editor.handleInput("\x1b[B"); // Down - line 2, col 8 (sticky) + assert.deepStrictEqual(editor.getCursor(), { line: 2, col: 8 }); + + // Type something to create undo state - this clears sticky and sets col to 9 + editor.handleInput("X"); + assert.strictEqual(editor.getText(), "1234567890\n\n12345678X90"); + assert.deepStrictEqual(editor.getCursor(), { line: 2, col: 9 }); + + // Move up - establishes new sticky col 9 + editor.handleInput("\x1b[A"); // Up - line 1, col 0 + editor.handleInput("\x1b[A"); // Up - line 0, col 9 + assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 9 }); + + // Undo - resets sticky column and restores cursor to line 2, col 8 + editor.handleInput("\x1b[45;5u"); // Ctrl+- (undo) + assert.strictEqual(editor.getText(), "1234567890\n\n1234567890"); + assert.deepStrictEqual(editor.getCursor(), { line: 2, col: 8 }); + + // Move up - should capture new sticky from restored col 8, not old col 9 + editor.handleInput("\x1b[A"); // Up - line 1, col 0 + editor.handleInput("\x1b[A"); // Up - line 0, col 8 (new sticky from restored position) + assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 8 }); + }); + + it("handles multiple consecutive up/down movements", () => { + const editor = new Editor(createTestTUI(), defaultEditorTheme); + + editor.setText("1234567890\nab\ncd\nef\n1234567890"); + + // Start at line 4, col 7 + editor.handleInput("\x01"); // Ctrl+A + for (let i = 0; i < 7; i++) editor.handleInput("\x1b[C"); + assert.deepStrictEqual(editor.getCursor(), { line: 4, col: 7 }); + + // Move up multiple times through short lines + editor.handleInput("\x1b[A"); // Up - line 3, col 2 (clamped) + editor.handleInput("\x1b[A"); // Up - line 2, col 2 (clamped) + editor.handleInput("\x1b[A"); // Up - line 1, col 2 (clamped) + editor.handleInput("\x1b[A"); // Up - line 0, col 7 (restored) + assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 7 }); + + // Move down multiple times - sticky should still be 7 + editor.handleInput("\x1b[B"); // Down - line 1, col 2 + editor.handleInput("\x1b[B"); // Down - line 2, col 2 + editor.handleInput("\x1b[B"); // Down - line 3, col 2 + editor.handleInput("\x1b[B"); // Down - line 4, col 7 (restored) + assert.deepStrictEqual(editor.getCursor(), { line: 4, col: 7 }); + }); + + it("moves correctly through wrapped visual lines without getting stuck", () => { + const tui = createTestTUI(15, 24); // Narrow terminal + const editor = new Editor(tui, defaultEditorTheme); + + // Line 0: short + // Line 1: 30 chars = wraps to 3 visual lines at width 10 (after padding) + editor.setText("short\n123456789012345678901234567890"); + editor.render(15); // This gives 14 layout width + + // Position at end of line 1 (col 30) + assert.deepStrictEqual(editor.getCursor(), { line: 1, col: 30 }); + + // Move up repeatedly - should traverse all visual lines of the wrapped text + // and eventually reach line 0 + editor.handleInput("\x1b[A"); // Up - to previous visual line within line 1 + assert.strictEqual(editor.getCursor().line, 1); + + editor.handleInput("\x1b[A"); // Up - another visual line + assert.strictEqual(editor.getCursor().line, 1); + + editor.handleInput("\x1b[A"); // Up - should reach line 0 + assert.strictEqual(editor.getCursor().line, 0); + }); + + it("handles setText resetting sticky column", () => { + const editor = new Editor(createTestTUI(), defaultEditorTheme); + + editor.setText("1234567890\n\n1234567890"); + + // Establish sticky column + editor.handleInput("\x01"); // Ctrl+A + for (let i = 0; i < 8; i++) editor.handleInput("\x1b[C"); + editor.handleInput("\x1b[A"); // Up + + // setText should reset sticky column + editor.setText("abcdefghij\n\nabcdefghij"); + assert.deepStrictEqual(editor.getCursor(), { line: 2, col: 10 }); // At end + + // Move up - should capture new sticky from current position (10) + editor.handleInput("\x1b[A"); // Up - line 1, col 0 + editor.handleInput("\x1b[A"); // Up - line 0, col 10 + assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 10 }); + }); + + it("sets preferredVisualCol when pressing right at end of prompt (last line)", () => { + const editor = new Editor(createTestTUI(), defaultEditorTheme); + + // Line 0: 20 chars with 'x' at col 10 + // Line 1: empty + // Line 2: 10 chars ending with '_' + editor.setText("111111111x1111111111\n\n333333333_"); + + // Go to line 0, press Ctrl+E (end of line) - col 20 + editor.handleInput("\x1b[A"); // Up to line 1 + editor.handleInput("\x1b[A"); // Up to line 0 + editor.handleInput("\x05"); // Ctrl+E - move to end of line + assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 20 }); + + // Move down to line 2 - cursor clamped to col 10 (end of line) + editor.handleInput("\x1b[B"); // Down to line 1, col 0 + editor.handleInput("\x1b[B"); // Down to line 2, col 10 (clamped) + assert.deepStrictEqual(editor.getCursor(), { line: 2, col: 10 }); + + // Press Right at end of prompt - nothing visible happens, but sets preferredVisualCol to 10 + editor.handleInput("\x1b[C"); // Right - can't move, but sets preferredVisualCol + assert.deepStrictEqual(editor.getCursor(), { line: 2, col: 10 }); // Still at same position + + // Move up twice to line 0 - should use preferredVisualCol (10) to land on 'x' + editor.handleInput("\x1b[A"); // Up to line 1, col 0 + editor.handleInput("\x1b[A"); // Up to line 0, col 10 (on 'x') + assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 10 }); + }); + + it("handles editor resizes when preferredVisualCol is on the same line", () => { + // Create editor with wider terminal + const tui = createTestTUI(80, 24); + const editor = new Editor(tui, defaultEditorTheme); + + editor.setText("12345678901234567890\n\n12345678901234567890"); + + // Start at line 2, col 15 + editor.handleInput("\x01"); // Ctrl+A + for (let i = 0; i < 15; i++) editor.handleInput("\x1b[C"); + + // Move up through empty line - establishes sticky col 15 + editor.handleInput("\x1b[A"); // Up + editor.handleInput("\x1b[A"); // Up - line 0, col 15 + assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 15 }); + + // Render with narrower width to simulate resize + editor.render(12); // Width 12 + + // Move down - sticky should be clamped to new width + editor.handleInput("\x1b[B"); // Down - line 1 + editor.handleInput("\x1b[B"); // Down - line 2, col should be clamped + assert.equal(editor.getCursor().col, 4); + }); + + it("handles editor resizes when preferredVisualCol is on a different line", () => { + const tui = createTestTUI(80, 24); + const editor = new Editor(tui, defaultEditorTheme); + + // Create a line that wraps into multiple visual lines at width 10 + // "12345678901234567890" = 20 chars, wraps to 2 visual lines at width 10 + editor.setText("short\n12345678901234567890"); + + // Go to line 1, col 15 + editor.handleInput("\x01"); // Ctrl+A + for (let i = 0; i < 15; i++) editor.handleInput("\x1b[C"); + assert.deepStrictEqual(editor.getCursor(), { line: 1, col: 15 }); + + // Move up to establish sticky col 15 + editor.handleInput("\x1b[A"); // Up to line 0 + // Line 0 has only 5 chars, so cursor at col 5 + assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 5 }); + + // Narrow the editor + editor.render(10); + + // Move down - preferredVisualCol was 15, but width is 10 + // Should land on line 1, clamped to width (visual col 9, which is logical col 9) + editor.handleInput("\x1b[B"); // Down to line 1 + assert.deepStrictEqual(editor.getCursor(), { line: 1, col: 8 }); + + // Move up + editor.handleInput("\x1b[A"); // Up - should go to line 0 + assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 5 }); // Line 0 only has 5 chars + + // Restore the original width + editor.render(80); + + // Move down - preferredVisualCol was kept at 15 + editor.handleInput("\x1b[B"); // Down to line 1 + assert.deepStrictEqual(editor.getCursor(), { line: 1, col: 15 }); + }); + + it("rewrapped lines: target fits current visual column", () => { + const tui = createTestTUI(80, 24); + const editor = new Editor(tui, defaultEditorTheme); + editor.setText("abcdefghijklmnopqr\n123456789012345678"); + + positionCursor(editor, 0, 18); + assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 18 }); + + // Narrow to width 10 (layoutWidth = 9). + // Line 0 last segment has visual col max 9, line 1 first segment max 8 + editor.render(10); + + // Move down: cursor clamps to 8 + editor.handleInput("\x1b[B"); + assert.deepStrictEqual(editor.getCursor(), { line: 1, col: 8 }); + + // Widen back. Move up, the current visual col wins + editor.render(80); + editor.handleInput("\x1b[A"); + assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 8 }); + + // Preferred was cleared by the rewrapped branch + editor.handleInput("\x1b[B"); + assert.deepStrictEqual(editor.getCursor(), { line: 1, col: 8 }); + }); + + it("rewrapped lines: target shorter than current visual column", () => { + const tui = createTestTUI(80, 24); + const editor = new Editor(tui, defaultEditorTheme); + editor.setText("abcdefghijklmnopqr\n123456789012345678\nab"); + + positionCursor(editor, 0, 18); + assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 18 }); + + // Narrow to width 10 (layoutWidth = 9). Moving down clamps to col 8 + editor.render(10); + editor.handleInput("\x1b[B"); + assert.deepStrictEqual(editor.getCursor(), { line: 1, col: 8 }); + + // Widen the editor + editor.render(80); + + // Move down to short line "ab". + // preferredVisualCol is replaced with current visual col (8), cursor clamps to 2 + editor.handleInput("\x1b[B"); + assert.deepStrictEqual(editor.getCursor(), { line: 2, col: 2 }); + + // Moving up restores to preferred col 8 + editor.handleInput("\x1b[A"); + assert.deepStrictEqual(editor.getCursor(), { line: 1, col: 8 }); + }); + }); + + describe("Paste marker atomic behavior", () => { + /** Helper: simulate a large paste that creates a marker */ + function pasteWithMarker(editor: Editor): string { + const bigContent = "line\n".repeat(20).trimEnd(); // 20 lines + editor.handleInput(`\x1b[200~${bigContent}\x1b[201~`); + // The editor replaces large pastes with a marker like "[paste #1 +20 lines]" + return editor.getText(); + } + + /** Helper: 12-line paste content with a distinguishing tag */ + function bigPaste(tag: string): string { + return Array.from({ length: 12 }, (_, i) => `${tag}${i}`).join("\n"); + } + + it("creates a paste marker for large pastes", () => { + const editor = new Editor(createTestTUI(), defaultEditorTheme); + const text = pasteWithMarker(editor); + assert.match(text, /\[paste #\d+ \+\d+ lines\]/); + }); + + it("treats paste marker as single unit for right arrow", () => { + const editor = new Editor(createTestTUI(), defaultEditorTheme); + editor.handleInput("A"); + pasteWithMarker(editor); + editor.handleInput("B"); + // Text: "A[paste #1 +20 lines]B", cursor at end + + // Go to start + editor.handleInput("\x01"); // Ctrl+A + assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 0 }); + + // Right arrow: should move past "A" + editor.handleInput("\x1b[C"); + assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 1 }); + + // Right arrow: should skip the entire marker + editor.handleInput("\x1b[C"); + const marker = editor.getText().match(/\[paste #\d+ \+\d+ lines\]/)![0]; + assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 1 + marker.length }); + + // Right arrow: should move past "B" + editor.handleInput("\x1b[C"); + assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 1 + marker.length + 1 }); + }); + + it("treats paste marker as single unit for left arrow", () => { + const editor = new Editor(createTestTUI(), defaultEditorTheme); + editor.handleInput("A"); + pasteWithMarker(editor); + editor.handleInput("B"); + // Cursor at end + + // Left arrow: past "B" + editor.handleInput("\x1b[D"); + const text = editor.getText(); + const marker = text.match(/\[paste #\d+ \+\d+ lines\]/)![0]; + assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 1 + marker.length }); + + // Left arrow: skip the entire marker + editor.handleInput("\x1b[D"); + assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 1 }); + + // Left arrow: past "A" + editor.handleInput("\x1b[D"); + assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 0 }); + }); + + it("treats paste marker as single unit for backspace", () => { + const editor = new Editor(createTestTUI(), defaultEditorTheme); + editor.handleInput("A"); + pasteWithMarker(editor); + editor.handleInput("B"); + + const text = editor.getText(); + const marker = text.match(/\[paste #\d+ \+\d+ lines\]/)![0]; + + // Position cursor right after the marker (before "B") + editor.handleInput("\x01"); // Ctrl+A + // Move past "A" and the marker + editor.handleInput("\x1b[C"); // past "A" + editor.handleInput("\x1b[C"); // past marker + assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 1 + marker.length }); + + // Backspace: should delete the entire marker at once + editor.handleInput("\x7f"); + assert.strictEqual(editor.getText(), "AB"); + assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 1 }); + }); + + it("treats paste marker as single unit for forward delete", () => { + const editor = new Editor(createTestTUI(), defaultEditorTheme); + editor.handleInput("A"); + pasteWithMarker(editor); + editor.handleInput("B"); + + // Position cursor on "A" (col 0) then move right once to be just before marker + editor.handleInput("\x01"); // Ctrl+A + editor.handleInput("\x1b[C"); // past "A", now at col 1 (start of marker) + + // Forward delete: should delete the entire marker at once + editor.handleInput("\x1b[3~"); // Delete key + assert.strictEqual(editor.getText(), "AB"); + assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 1 }); + }); + + it("treats paste marker as single unit for word movement", () => { + const editor = new Editor(createTestTUI(), defaultEditorTheme); + editor.handleInput("X"); + editor.handleInput(" "); + pasteWithMarker(editor); + editor.handleInput(" "); + editor.handleInput("Y"); + // Text: "X [paste #1 +20 lines] Y" + + const text = editor.getText(); + const marker = text.match(/\[paste #\d+ \+\d+ lines\]/)![0]; + + // Go to start + editor.handleInput("\x01"); // Ctrl+A + + // Ctrl+Right: skip "X" + editor.handleInput("\x1b[1;5C"); + assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 1 }); + + // Ctrl+Right: skip whitespace + marker (marker treated as single non-ws, non-punct unit) + editor.handleInput("\x1b[1;5C"); + assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 2 + marker.length }); + }); + + it("undo restores marker after backspace deletion", () => { + const editor = new Editor(createTestTUI(), defaultEditorTheme); + editor.handleInput("A"); + pasteWithMarker(editor); + editor.handleInput("B"); + + const textBefore = editor.getText(); + + // Position after marker + editor.handleInput("\x01"); + editor.handleInput("\x1b[C"); // past A + editor.handleInput("\x1b[C"); // past marker + + // Delete marker + editor.handleInput("\x7f"); + assert.strictEqual(editor.getText(), "AB"); + + // Undo + editor.handleInput("\x1b[45;5u"); + assert.strictEqual(editor.getText(), textBefore); + }); + + it("undo after paste marker deletion restores the paste registry", () => { + const editor = new Editor(createTestTUI(), defaultEditorTheme); + let submitted = ""; + editor.onSubmit = (t) => { + submitted = t; + }; + + const paste = bigPaste("alpha"); + editor.handleInput(`\x1b[200~${paste}\x1b[201~`); + editor.handleInput("\x7f"); // delete the marker + editor.handleInput("\x1b[45;5u"); // undo: restores marker text and registry + editor.handleInput("\r"); + assert.strictEqual(submitted, paste); + }); + + it("undo after deleting the first of two paste markers restores both registry entries", () => { + const editor = new Editor(createTestTUI(), defaultEditorTheme); + let submitted = ""; + editor.onSubmit = (t) => { + submitted = t; + }; + + const pasteA = bigPaste("alpha"); + const pasteB = bigPaste("beta"); + editor.handleInput(`\x1b[200~${pasteA}\x1b[201~`); // #1 = A + editor.handleInput(`\x1b[200~${pasteB}\x1b[201~`); // #2 = B, cursor at end + editor.handleInput("\x01"); // Ctrl+A + editor.handleInput("\x1b[C"); // right over marker #1 + editor.handleInput("\x7f"); // delete marker #1, renumbers #2 -> #1 + editor.handleInput("\x1b[45;5u"); // undo + editor.handleInput("\r"); + assert.strictEqual(submitted, pasteA + pasteB); + }); + + it("renumbers the paste registry in ascending id order when markers are out of order in text", () => { + const editor = new Editor(createTestTUI(), defaultEditorTheme); + let submitted = ""; + editor.onSubmit = (t) => { + submitted = t; + }; + + const pasteA = bigPaste("alpha"); + const pasteB = bigPaste("beta"); + const pasteC = bigPaste("gamma"); + editor.handleInput(`\x1b[200~${pasteA}\x1b[201~`); // #1 = A + editor.handleInput("\x01"); // Ctrl+A + editor.handleInput(`\x1b[200~${pasteB}\x1b[201~`); // #2 = B, text: [#2][#1] + editor.handleInput("\x01"); // Ctrl+A + editor.handleInput(`\x1b[200~${pasteC}\x1b[201~`); // #3 = C, text: [#3][#2][#1] + editor.handleInput("\x05"); // Ctrl+E + editor.handleInput("\x7f"); // delete marker #1, renumber #3 -> #2 and #2 -> #1 + editor.handleInput("\r"); + assert.strictEqual(submitted, pasteC + pasteB); + }); + + it("undo after setText restores paste markers and registry", () => { + const editor = new Editor(createTestTUI(), defaultEditorTheme); + let submitted = ""; + editor.onSubmit = (t) => { + submitted = t; + }; + + const paste = bigPaste("alpha"); + editor.handleInput(`\x1b[200~${paste}\x1b[201~`); + editor.setText("replacement"); + editor.handleInput("\x1b[45;5u"); // undo + editor.handleInput("\r"); + assert.strictEqual(submitted, paste); + }); + + it("handles multiple paste markers in same line", () => { + const editor = new Editor(createTestTUI(), defaultEditorTheme); + pasteWithMarker(editor); + editor.handleInput(" "); + pasteWithMarker(editor); + + const text = editor.getText(); + const markers = [...text.matchAll(/\[paste #\d+ \+\d+ lines\]/g)]; + assert.strictEqual(markers.length, 2); + + // Go to start + editor.handleInput("\x01"); + + // Right arrow: should skip first marker atomically + editor.handleInput("\x1b[C"); + assert.deepStrictEqual(editor.getCursor(), { line: 0, col: markers[0]![0].length }); + + // Right arrow: past space + editor.handleInput("\x1b[C"); + assert.deepStrictEqual(editor.getCursor(), { line: 0, col: markers[0]![0].length + 1 }); + + // Right arrow: should skip second marker atomically + editor.handleInput("\x1b[C"); + assert.deepStrictEqual(editor.getCursor(), { + line: 0, + col: markers[0]![0].length + 1 + markers[1]![0].length, + }); + }); + + it("does not treat manually typed marker-like text as atomic (no valid paste ID)", () => { + const editor = new Editor(createTestTUI(), defaultEditorTheme); + // Type text that matches the pattern but was typed manually (no paste entry) + const fakeMarker = "[paste #99 +5 lines]"; + for (const ch of fakeMarker) editor.handleInput(ch); + + assert.strictEqual(editor.getText(), fakeMarker); + + // No paste with ID 99 exists, so the marker is NOT treated atomically. + // Right arrow should move one grapheme at a time. + editor.handleInput("\x01"); // Ctrl+A + editor.handleInput("\x1b[C"); // Right + assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 1 }); // Just past "[" + }); + + it("does not crash when paste marker is wider than terminal width", () => { + // Reproduce: terminal width 8, paste marker "[paste #1 +47 lines]" (21 chars) + const tui = createTestTUI(); + const editor = new Editor(tui, defaultEditorTheme); + const bigContent = "line\n".repeat(47).trimEnd(); + editor.handleInput(`\x1b[200~${bigContent}\x1b[201~`); + + const text = editor.getText(); + const marker = text.match(/\[paste #\d+ \+\d+ lines\]/); + assert.ok(marker, "paste marker should be created"); + assert.ok(visibleWidth(marker[0]) > 8, "marker should be wider than render width"); + + // Render at very narrow width - should not throw + const lines = editor.render(8); + // Every rendered line must fit within the width (marker is split) + for (const line of lines) { + assert.ok( + visibleWidth(line) <= 8, + `line exceeds width 8: visible=${visibleWidth(line)} text=${JSON.stringify(line)}`, + ); + } + }); + + it("does not crash when text + paste marker exceeds terminal width with cursor on marker", () => { + // Reproduce: terminal width 54, text "b".repeat(35) + "[paste #1 +27 lines]" + "bbbb" + // Cursor lands on the paste marker after word-wrap, causing the rendered line + // to be 55 visible chars (1 over the width). + const tui = createTestTUI(); + const editor = new Editor(tui, defaultEditorTheme); + + // Type 35 'b' characters + for (let i = 0; i < 35; i++) editor.handleInput("b"); + + // Paste 27 lines + const bigContent = "line\n".repeat(27).trimEnd(); + editor.handleInput(`\x1b[200~${bigContent}\x1b[201~`); + + // Type a few more characters + for (let i = 0; i < 4; i++) editor.handleInput("b"); + + // Move cursor left to land on the paste marker + editor.handleInput("\x1b[D"); // past last 'b' + editor.handleInput("\x1b[D"); // past last 'b' + editor.handleInput("\x1b[D"); // past last 'b' + editor.handleInput("\x1b[D"); // past last 'b' + editor.handleInput("\x1b[D"); // now on the paste marker + + // Render at width 54 - should not throw + const renderWidth = 54; + const lines = editor.render(renderWidth); + for (const line of lines) { + assert.ok( + visibleWidth(line) <= renderWidth, + `line exceeds width ${renderWidth}: visible=${visibleWidth(line)} text=${JSON.stringify(line)}`, + ); + } + }); + + it("wordWrapLine re-checks overflow after backtracking to wrap opportunity", () => { + // Reproduce crash #2: " " + "b".repeat(35) + atomic_marker(20 chars) + "bbbb" + // layoutWidth=53. After wrapping at the space, the remaining 35 b's + marker = 55 + // must trigger a second force-break instead of silently overflowing. + const tui = createTestTUI(); + const editor = new Editor(tui, defaultEditorTheme); + + // Type a space, then 35 b's + editor.handleInput(" "); + for (let i = 0; i < 35; i++) editor.handleInput("b"); + + // Paste 27 lines to create marker + const bigContent = "line\n".repeat(27).trimEnd(); + editor.handleInput(`\x1b[200~${bigContent}\x1b[201~`); + + // Type trailing chars + for (let i = 0; i < 4; i++) editor.handleInput("b"); + + // Render at width 54 (contentWidth=54, layoutWidth=53 with paddingX=0) + const renderWidth = 54; + const lines = editor.render(renderWidth); + for (const line of lines) { + assert.ok( + visibleWidth(line) <= renderWidth, + `line exceeds width ${renderWidth}: visible=${visibleWidth(line)} text=${JSON.stringify(line)}`, + ); + } + }); + + it("expands large pasted content literally in getExpandedText", () => { + const editor = new Editor(createTestTUI(), defaultEditorTheme); + const pastedText = [ + "line 1", + "line 2", + "line 3", + "line 4", + "line 5", + "line 6", + "line 7", + "line 8", + "line 9", + "line 10", + "tokens $1 $2 $& $$ $` $' end", + ].join("\n"); + + editor.handleInput(`\x1b[200~${pastedText}\x1b[201~`); + + assert.match(editor.getText(), /\[paste #\d+ \+\d+ lines\]/); + assert.strictEqual(editor.getExpandedText(), pastedText); + }); + + it("snaps to the paste marker start when navigating down into it", () => { + const editor = new Editor(createTestTUI(), defaultEditorTheme); + + // Line 0: long enough text to establish a sticky column + editor.setText("12345678901234567890\n\nhello "); + + // Create a large paste to get a marker + const bigContent = "x".repeat(2000); + editor.handleInput(`\x1b[200~${bigContent}\x1b[201~`); + editor.render(80); + + const text = editor.getText(); + const _marker = text.match(/\[paste #\d+ \d+ chars\]/)![0]; + // Line 0: "12345678901234567890" + // Line 1: "" (empty) + // Line 2: "hello [paste #1 2000 chars]" + // marker starts at col 6 + + // Navigate to line 0, col 10 + editor.handleInput("\x1b[A"); // Up to line 1 + editor.handleInput("\x1b[A"); // Up to line 0 + editor.handleInput("\x01"); // Ctrl+A (start of line) + for (let i = 0; i < 10; i++) editor.handleInput("\x1b[C"); // Right 10 + assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 10 }); + + // Down to empty line + editor.handleInput("\x1b[B"); + assert.deepStrictEqual(editor.getCursor(), { line: 1, col: 0 }); + + // Down to paste marker line - sticky col 10 falls inside marker (starts at col 6). + // Cursor should snap to start of marker (col 6), not end (col 6 + marker.length). + editor.handleInput("\x1b[B"); + assert.deepStrictEqual(editor.getCursor(), { line: 2, col: 6 }); + }); + + it("preserves sticky column when navigating through paste marker line", () => { + const tui = createTestTUI(30, 24); + const editor = new Editor(tui, defaultEditorTheme); + + // Build: + // Line 0: "1234567890123456" (16 chars) + // Line 1: "" (empty) + // Line 2: "[paste #1 2000 chars]" (22 chars, paste marker) + // Line 3: "" (empty) + // Line 4: "abcdefghijklmnop" (16 chars) + for (const ch of "1234567890123456") editor.handleInput(ch); + editor.handleInput("\n"); + editor.handleInput("\n"); + editor.handleInput(`\x1b[200~${"x".repeat(2000)}\x1b[201~`); + editor.handleInput("\n"); + editor.handleInput("\n"); + for (const ch of "abcdefghijklmnop") editor.handleInput(ch); + editor.render(30); + + // Navigate to line 0, col 10 + for (let i = 0; i < 4; i++) editor.handleInput("\x1b[A"); // Up to line 0 + editor.handleInput("\x01"); // Ctrl+A + for (let i = 0; i < 10; i++) editor.handleInput("\x1b[C"); + assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 10 }); + + // Down to empty line - sticky col 10 established + editor.handleInput("\x1b[B"); + assert.deepStrictEqual(editor.getCursor(), { line: 1, col: 0 }); + + // Down to paste marker - cursor snapped to col 0 (start of marker) + editor.handleInput("\x1b[B"); + assert.deepStrictEqual(editor.getCursor(), { line: 2, col: 0 }); + + // Down to empty line + editor.handleInput("\x1b[B"); + assert.deepStrictEqual(editor.getCursor(), { line: 3, col: 0 }); + + // Down to last line - should restore sticky col 10 + editor.handleInput("\x1b[B"); + assert.deepStrictEqual(editor.getCursor(), { line: 4, col: 10 }); + }); + + it("does not get stuck moving down from a multi-visual-line paste marker", () => { + const tui = createTestTUI(20, 24); + const editor = new Editor(tui, defaultEditorTheme); + + // Build: + // Logical line 0: "abcdefgh" + marker(21 chars) + "ijklmnopqr" + // Logical line 1: "123456789012345678" + // + // Marker "[paste #1 +100 lines]" (21 chars) is wider than the + // terminal (20). Word-wrap splits at the space before "lines", + // producing: + // VL1: abcdefgh (startCol 0, len 8) + // VL2: [paste #1 +100 (startCol 8, len 15) <- marker head + // VL3: lines]ijklmnopqr (startCol 23, len 16) <- marker tail + content + // VL4: 123456789012345678 (line 1) + // + // On VL3 the marker tail "lines]" occupies visual cols 0-5. + // Content ("i") starts at visual col 6 = logical col 29. + for (const ch of "abcdefgh") editor.handleInput(ch); + const bigContent = "line\n".repeat(100).trimEnd(); + editor.handleInput(`\x1b[200~${bigContent}\x1b[201~`); + for (const ch of "ijklmnopqr") editor.handleInput(ch); + editor.handleInput("\n"); + for (const ch of "123456789012345678") editor.handleInput(ch); + editor.render(20); + + const text = editor.getText(); + const markerMatch = text.match(/\[paste #\d+ \+\d+ lines]/); + assert.ok(markerMatch, "paste marker should be created"); + const markerLen = markerMatch[0].length; // 21 + assert.ok(markerLen > 20, "marker should be wider than terminal"); + const markerStart = 8; + const markerEnd = markerStart + markerLen; // 29 + + // Navigate to line 0, col 6 (on "g"). Preferred col 6 is past the + // marker tail on VL3, so the cursor should land on content ("i" at + // col 29) without snapping back. + editor.handleInput("\x1b[A"); // Up to line 0 + editor.handleInput("\x01"); // Ctrl+A (start of line) + for (let i = 0; i < 6; i++) editor.handleInput("\x1b[C"); // Right to col 6 + assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 6 }); + + // Down: cursor lands on paste marker start + editor.handleInput("\x1b[B"); + assert.deepStrictEqual(editor.getCursor(), { line: 0, col: markerStart }); + + // Down again: preferred col 6 lands at VL3 col 29 ("i"), which is + // past the marker. Cursor stays on line 0. + editor.handleInput("\x1b[B"); + assert.strictEqual(editor.getCursor().line, 0); + assert.strictEqual(editor.getCursor().col, markerEnd); // col 29 = "i" + + // Up: back to paste marker + editor.handleInput("\x1b[A"); + assert.deepStrictEqual(editor.getCursor(), { line: 0, col: markerStart }); + + // Up again: back to col 6 ("g") + editor.handleInput("\x1b[A"); + assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 6 }); + }); + + it("skips marker continuation VLs when preferred col falls in marker tail", () => { + const tui = createTestTUI(20, 24); + const editor = new Editor(tui, defaultEditorTheme); + + // Same layout. Start at col 3 ("d"). Preferred col 3 maps to VL3 + // visual col 3 which is inside the "lines]" marker tail. + // moveToVisualLine detects the continuation VL and skips to VL4 + // (line 1). + // VL1: abcdefgh (startCol 0, len 8) + // VL2: [paste #1 +100 (startCol 8, len 15) <- marker head + // VL3: lines]ijklmnopqr (startCol 23, len 16) <- marker tail + content + // VL4: 123456789012345678 (line 1) + for (const ch of "abcdefgh") editor.handleInput(ch); + const bigContent = "line\n".repeat(100).trimEnd(); + editor.handleInput(`\x1b[200~${bigContent}\x1b[201~`); + for (const ch of "ijklmnopqr") editor.handleInput(ch); + editor.handleInput("\n"); + for (const ch of "123456789012345678") editor.handleInput(ch); + editor.render(20); + + // Navigate to line 0, col 3 (on "d") + editor.handleInput("\x1b[A"); // Up to line 0 + editor.handleInput("\x01"); // Ctrl+A + for (let i = 0; i < 3; i++) editor.handleInput("\x1b[C"); + assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 3 }); + + // Down: marker + editor.handleInput("\x1b[B"); + assert.strictEqual(editor.getCursor().col, 8); + + // Down: skips VL3 (col 3 in marker tail) and lands on line 1 + editor.handleInput("\x1b[B"); + assert.deepStrictEqual(editor.getCursor(), { line: 1, col: 3 }); + + // Round-trip back + editor.handleInput("\x1b[A"); + assert.strictEqual(editor.getCursor().col, 8); // marker + editor.handleInput("\x1b[A"); + assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 3 }); + }); + + it("submits large pasted content literally", () => { + const editor = new Editor(createTestTUI(), defaultEditorTheme); + const pastedText = [ + "line 1", + "line 2", + "line 3", + "line 4", + "line 5", + "line 6", + "line 7", + "line 8", + "line 9", + "line 10", + "tokens $1 $2 $& $$ $` $' end", + ].join("\n"); + let submitted = ""; + editor.onSubmit = (text) => { + submitted = text; + }; + + editor.handleInput(`\x1b[200~${pastedText}\x1b[201~`); + editor.handleInput("\r"); + + assert.strictEqual(submitted, pastedText); + }); + }); +}); diff --git a/packages/tui/test/fixtures/clipboard-reader.cjs b/packages/tui/test/fixtures/clipboard-reader.cjs new file mode 100644 index 0000000000000000000000000000000000000000..8d7e4a69ffc82abccaec60fce44bcfdeb14ccf43 --- /dev/null +++ b/packages/tui/test/fixtures/clipboard-reader.cjs @@ -0,0 +1,31 @@ +const { createHash } = require("node:crypto"); +const assert = require("node:assert/strict"); +const { readFileSync } = require("node:fs"); + +(async () => { +let ticks = 0; +const timer = setInterval(() => ticks++, 10); +const started = performance.now(); +try { + const helper = require(process.argv[2]); + const pending = helper[process.argv[3]](); + assert.ok(pending instanceof Promise, "Native reads must return promises"); + const value = await pending; + const bytes = typeof value === "string" || Buffer.isBuffer(value) ? Buffer.from(value) : undefined; + console.log(JSON.stringify({ + ok: true, + value: value === null || (typeof value === "string" && value.length <= 100) ? value : undefined, + unavailable: value === undefined || undefined, + length: bytes?.length, + hash: bytes && createHash("sha256").update(bytes).digest("hex"), + })); +} catch (error) { + console.log(JSON.stringify({ ok: false, error: String(error) })); +} finally { + clearInterval(timer); + if (performance.now() - started > 200) assert.ok(ticks > 5, "Clipboard I/O must not block the JS event loop"); + // Check before Node exits: process exit must not hide a leaked child or zombie. + assert.equal(readFileSync(`/proc/self/task/${process.pid}/children`, "utf8").trim(), ""); +} + +})().catch((error) => { console.error(error); process.exitCode = 1; }); diff --git a/packages/tui/test/fixtures/clipboard-worker-test.c b/packages/tui/test/fixtures/clipboard-worker-test.c new file mode 100644 index 0000000000000000000000000000000000000000..6f3523002831ced30f5b23f0bcc089d6371a00a7 --- /dev/null +++ b/packages/tui/test/fixtures/clipboard-worker-test.c @@ -0,0 +1,64 @@ +#include +#include +#include +#include + +static atomic_int allocations, reads; +static atomic_bool released; + +static void* tracked_calloc(size_t count, size_t size) { + void* pointer = calloc(count, size); + if (pointer) atomic_fetch_add(&allocations, 1); + return pointer; +} + +static void tracked_free(void* pointer) { + if (pointer) atomic_fetch_sub(&allocations, 1); + free(pointer); +} + +#define calloc tracked_calloc +#define free tracked_free +#include "../../native/linux/src/clipboard-worker.h" +#undef calloc +#undef free + +// Exercise the production executor with a controlled blocking callback, without X11. +static void read_clipboard(clipboard_job* job) { + job->data = tracked_calloc(5, 1); + assert(job->data); + memcpy(job->data, "text", 4); + job->length = 4; + job->format = job->operation == CLIPBOARD_IMAGE ? CLIPBOARD_BUFFER : CLIPBOARD_UTF8; + atomic_fetch_add(&reads, 1); + while (!atomic_load(&released)) { + struct timespec delay = {.tv_nsec = 1000000}; + nanosleep(&delay, 0); + } +} + +static napi_value release_reader(napi_env env, napi_callback_info info) { + (void)info; + atomic_store(&released, true); + return undefined_value(env); +} + +static napi_value get_state(napi_env env, napi_callback_info info) { + (void)info; + char state[80]; + pthread_mutex_lock(&worker_mutex); + snprintf(state, sizeof(state), "%d,%d,%d", atomic_load(&reads), atomic_load(&allocations), worker_busy); + pthread_mutex_unlock(&worker_mutex); + napi_create_string_utf8_fn create_string = (napi_create_string_utf8_fn)node_symbol("napi_create_string_utf8"); + napi_value result = 0; + create_string(env, state, NAPI_AUTO_LENGTH, &result); + return result; +} + +PI_NAPI_EXPORT napi_value napi_register_module_v1(napi_env env, napi_value exports) { + set_function_export(env, exports, "getText", get_clipboard_text); + set_function_export(env, exports, "getImage", get_clipboard_image); + set_function_export(env, exports, "release", release_reader); + set_function_export(env, exports, "state", get_state); + return exports; +} diff --git a/packages/tui/test/fixtures/clipboard-worker-test.cjs b/packages/tui/test/fixtures/clipboard-worker-test.cjs new file mode 100644 index 0000000000000000000000000000000000000000..aec9d256517048729f1f00ca3bcc2ad03d59ed4e --- /dev/null +++ b/packages/tui/test/fixtures/clipboard-worker-test.cjs @@ -0,0 +1,66 @@ +const assert = require("node:assert/strict"); +const { once } = require("node:events"); +const { readFile } = require("node:fs/promises"); +const { setTimeout: delay } = require("node:timers/promises"); +const { isMainThread, parentPort, Worker, workerData } = require("node:worker_threads"); + +async function waitFor(predicate) { + const deadline = performance.now() + 5000; + while (!predicate()) { + assert.ok(performance.now() < deadline, "Timed out waiting for the private reader"); + await delay(10); + } +} + +(async () => { + if (!isMainThread) { + const helper = require(workerData); + void helper.getText(); + await waitFor(() => helper.state().startsWith("1,")); + parentPort.postMessage("reading"); + return; + } + const [addon, mode] = process.argv.slice(2); + if (mode === "worker-exit") { + // The originating environment unloads while its private callback still waits. + const worker = new Worker(__filename, { workerData: addon }); + await once(worker, "message"); + await worker.terminate(); + const helper = require(addon); + assert.equal(helper.state(), "1,1,1", "The detached operation survives environment teardown"); + helper.release(); + await waitFor(() => helper.state() === "1,0,0"); + assert.equal(await helper.getText(), "text"); + console.log("passed"); + return; + } + const helper = require(addon); + const started = performance.now(); + const pending = helper.getText(); + await waitFor(() => helper.state().startsWith("1,")); + if (mode === "process-exit") { + assert.equal(await pending, undefined); + console.log("passed"); + return; // The private callback never finishes; Node must still exit naturally. + } + const concurrentStarted = performance.now(); + const concurrent = Array.from({ length: 20 }, (_, index) => helper[index % 2 ? "getText" : "getImage"]()); + // With UV_THREADPOOL_SIZE=2, a second blocked native task would delay this read. + await readFile(__filename); + assert.deepEqual(await Promise.all(concurrent), Array(20).fill(undefined)); + assert.ok(performance.now() - concurrentStarted < 1500, "Concurrent reads and filesystem I/O must remain available"); + assert.equal(await pending, undefined); + assert.ok(performance.now() - started < 4500, "The shared worker must leave its bounded wait"); + assert.equal(helper.state(), "1,1,1", "Only one private reader may remain blocked"); + assert.equal(await helper.getImage(), undefined); + assert.equal(helper.state(), "1,1,1", "Another request must not start another private reader"); + helper.release(); + await waitFor(() => helper.state() === "1,0,0"); + assert.equal(await helper.getText(), "text"); + assert.deepEqual(await helper.getImage(), Buffer.from("text")); + assert.equal(helper.state(), "3,0,0", "Late and successful results must all be freed"); + console.log("passed"); +})().catch((error) => { + console.error(error); + process.exitCode = 1; +}); diff --git a/packages/tui/test/fixtures/clipboard-x11-test.c b/packages/tui/test/fixtures/clipboard-x11-test.c new file mode 100644 index 0000000000000000000000000000000000000000..d021f308969d9ac74ea7c18904877a6e44814148 --- /dev/null +++ b/packages/tui/test/fixtures/clipboard-x11-test.c @@ -0,0 +1,177 @@ +#include +#include +#include +#include +#include +#include + +// Track only allocations made by the clipboard implementation, not libxcb. +// This makes the partial-transfer leak regression deterministic, unlike RSS. +static void* allocations[64]; +static size_t sizes[64]; +static size_t allocated_bytes; +static size_t peak_bytes; + +static void* tracked_realloc(void* pointer, size_t size) { + size_t index = 0; + while (index < 64 && allocations[index] != pointer) index++; + assert(index < 64); + void* result = realloc(pointer, size); + if (result) { + allocated_bytes = allocated_bytes - sizes[index] + size; + sizes[index] = size; + allocations[index] = result; + if (allocated_bytes > peak_bytes) peak_bytes = allocated_bytes; + } + return result; +} + +static void tracked_free(void* pointer) { + for (size_t index = 0; pointer && index < 64; index++) { + if (allocations[index] == pointer) { + allocated_bytes -= sizes[index]; + sizes[index] = 0; + allocations[index] = 0; + break; + } + } + free(pointer); +} + +#define realloc tracked_realloc +#define free tracked_free +#include "../../native/linux/src/linux-platform-x11.c" +#undef realloc +#undef free + +int main(int argc, char** argv) { + if (argc != 2) return 1; + if (strcmp(argv[1], "metadata") == 0) { + xcb_atom_t atom = XCB_ATOM_STRING; + property_data chunk = { + .data = (unsigned char*)&atom, .length = sizeof(atom), + .items = 1, .format = 32, .type = XCB_ATOM_ATOM, + }; + property_data result = {0}; + assert(append_property(&result, &chunk)); + assert(append_property(&result, &chunk)); + assert(result.length == 2 * sizeof(atom) && result.items == 2); + + chunk.format = 8; + chunk.items = sizeof(atom); + assert(!append_property(&result, &chunk)); + chunk.format = 32; + chunk.items = 1; + chunk.type = XCB_ATOM_STRING; + assert(!append_property(&result, &chunk)); + chunk.type = XCB_ATOM_ATOM; + chunk.length--; + assert(!append_property(&result, &chunk)); + assert(result.length == 2 * sizeof(atom) && result.items == 2); + + // The terminating chunk must agree with the preceding metadata too. + chunk.length = chunk.items = 0; + chunk.format = 8; + assert(!append_property(&result, &chunk)); + chunk.format = 32; + assert(append_property(&result, &chunk)); + tracked_free(result.data); + assert(allocated_bytes == 0); + puts("validated"); + return 0; + } + if (strcmp(argv[1], "text") == 0 || strcmp(argv[1], "image") == 0) { + x11_clipboard clipboard; + assert(open_clipboard(&clipboard)); + property_data result = {0}; + bool received = read_selection(&clipboard, strcmp(argv[1], "image") == 0, &result); + close_clipboard(&clipboard); + assert(!received); + assert(!result.data && !result.length); + assert(peak_bytes >= 8192); // Both the received chunk and accumulated data were allocated. + assert(allocated_bytes == 0); + puts("clean"); + return 0; + } + + x11_clipboard clipboard; + assert(open_clipboard(&clipboard)); + xcb_connection_t* connection = clipboard.connection; + xcb_atom_t text = intern_atom(&clipboard, "UTF8_STRING"); + xcb_atom_t image = intern_atom(&clipboard, "image/png"); + xcb_set_selection_owner(connection, clipboard.window, clipboard.clipboard, XCB_CURRENT_TIME); + // Wait for the server to process selection ownership before reporting ready. + assert(intern_atom(&clipboard, "READY") != XCB_NONE); + puts("ready"); + fflush(stdout); + + xcb_window_t requestor = XCB_NONE; + xcb_atom_t property = XCB_NONE; + xcb_atom_t target = XCB_NONE; + bool sent_chunk = false; + xcb_generic_event_t* event; + while ((event = xcb_wait_for_event(connection))) { + uint8_t type = event->response_type & 0x7f; + if (type == XCB_SELECTION_REQUEST) { + xcb_selection_request_event_t* request = (xcb_selection_request_event_t*)event; + puts("requested"); + fflush(stdout); + if (strcmp(argv[1], "idle") == 0) { + free(event); + continue; + } + xcb_atom_t response_property = request->property; + if (strcmp(argv[1], "direct-text") == 0) { + if (request->target == text) { + const char value[] = "café 日本語"; + xcb_change_property(connection, XCB_PROP_MODE_REPLACE, request->requestor, + request->property, text, 8, sizeof(value) - 1, value); + } else { + response_property = XCB_NONE; + } + } else if (request->target == clipboard.targets) { + xcb_atom_t targets[] = {text, image}; + xcb_change_property(connection, XCB_PROP_MODE_REPLACE, request->requestor, + request->property, XCB_ATOM_ATOM, 32, 2, targets); + } else { + requestor = request->requestor; + property = request->property; + target = request->target; + uint32_t mask = XCB_EVENT_MASK_PROPERTY_CHANGE; + xcb_change_window_attributes(connection, requestor, XCB_CW_EVENT_MASK, &mask); + uint32_t length = 8192; + xcb_change_property(connection, XCB_PROP_MODE_REPLACE, requestor, + property, clipboard.incr, 32, 1, &length); + } + union { + xcb_selection_notify_event_t selection; + char bytes[32]; + } notification = { .selection = { + .response_type = XCB_SELECTION_NOTIFY, + .time = request->time, + .requestor = request->requestor, + .selection = request->selection, + .target = request->target, + .property = response_property, + } }; + xcb_send_event(connection, false, request->requestor, XCB_EVENT_MASK_NO_EVENT, notification.bytes); + xcb_flush(connection); + } else if (type == XCB_PROPERTY_NOTIFY) { + xcb_property_notify_event_t* notification = (xcb_property_notify_event_t*)event; + if (notification->window == requestor && notification->atom == property && + notification->state == XCB_PROPERTY_DELETE) { + if (!sent_chunk) { + unsigned char chunk[4096] = {0}; + xcb_change_property(connection, XCB_PROP_MODE_REPLACE, requestor, property, target, 8, sizeof(chunk), chunk); + sent_chunk = true; + } else if (strcmp(argv[1], "invalid") == 0) { + // Reject the terminating chunk after allocating the preceding data. + xcb_change_property(connection, XCB_PROP_MODE_REPLACE, requestor, property, XCB_ATOM_ATOM, 32, 0, 0); + } // Otherwise never finish the transfer; the reader must time out. + xcb_flush(connection); + } + } + free(event); + } + close_clipboard(&clipboard); +} diff --git a/packages/tui/test/fuzzy.test.ts b/packages/tui/test/fuzzy.test.ts new file mode 100644 index 0000000000000000000000000000000000000000..af4662afaf73921556cb5bbb86587ee488d1d21c --- /dev/null +++ b/packages/tui/test/fuzzy.test.ts @@ -0,0 +1,112 @@ +import assert from "node:assert"; +import { describe, it } from "node:test"; +import { fuzzyFilter, fuzzyMatch } from "../src/fuzzy.ts"; + +describe("fuzzyMatch", () => { + it("empty query matches everything with score 0", () => { + const result = fuzzyMatch("", "anything"); + assert.strictEqual(result.matches, true); + assert.strictEqual(result.score, 0); + }); + + it("query longer than text does not match", () => { + const result = fuzzyMatch("longquery", "short"); + assert.strictEqual(result.matches, false); + }); + + it("exact match has good score", () => { + const result = fuzzyMatch("test", "test"); + assert.strictEqual(result.matches, true); + assert.ok(result.score < 0); // Should be negative due to consecutive bonuses + }); + + it("characters must appear in order", () => { + const matchInOrder = fuzzyMatch("abc", "aXbXc"); + assert.strictEqual(matchInOrder.matches, true); + + const matchOutOfOrder = fuzzyMatch("abc", "cba"); + assert.strictEqual(matchOutOfOrder.matches, false); + }); + + it("case insensitive matching", () => { + const result = fuzzyMatch("ABC", "abc"); + assert.strictEqual(result.matches, true); + + const result2 = fuzzyMatch("abc", "ABC"); + assert.strictEqual(result2.matches, true); + }); + + it("consecutive matches score better than scattered matches", () => { + const consecutive = fuzzyMatch("foo", "foobar"); + const scattered = fuzzyMatch("foo", "f_o_o_bar"); + + assert.strictEqual(consecutive.matches, true); + assert.strictEqual(scattered.matches, true); + assert.ok(consecutive.score < scattered.score); + }); + + it("word boundary matches score better", () => { + const atBoundary = fuzzyMatch("fb", "foo-bar"); + const notAtBoundary = fuzzyMatch("fb", "afbx"); + + assert.strictEqual(atBoundary.matches, true); + assert.strictEqual(notAtBoundary.matches, true); + assert.ok(atBoundary.score < notAtBoundary.score); + }); + + it("matches swapped alpha numeric tokens", () => { + const result = fuzzyMatch("codex52", "gpt-5.2-codex"); + assert.strictEqual(result.matches, true); + }); +}); + +describe("fuzzyFilter", () => { + it("empty query returns all items unchanged", () => { + const items = ["apple", "banana", "cherry"]; + const result = fuzzyFilter(items, "", (x: string) => x); + assert.deepStrictEqual(result, items); + }); + + it("filters out non-matching items", () => { + const items = ["apple", "banana", "cherry"]; + const result = fuzzyFilter(items, "an", (x: string) => x); + assert.ok(result.includes("banana")); + assert.ok(!result.includes("apple")); + assert.ok(!result.includes("cherry")); + }); + + it("sorts results by match quality", () => { + const items = ["a_p_p", "app", "application"]; + const result = fuzzyFilter(items, "app", (x: string) => x); + + // "app" should be first (exact consecutive match at start) + assert.strictEqual(result[0], "app"); + }); + + it("prioritizes exact matches over longer prefix matches", () => { + const items = ["clone", "cl"]; + const result = fuzzyFilter(items, "cl", (x: string) => x); + + assert.deepStrictEqual(result, ["cl", "clone"]); + }); + + it("works with custom getText function", () => { + const items = [ + { name: "foo", id: 1 }, + { name: "bar", id: 2 }, + { name: "foobar", id: 3 }, + ]; + const result = fuzzyFilter(items, "foo", (item: { name: string; id: number }) => item.name); + + assert.strictEqual(result.length, 2); + assert.ok(result.map((r) => r.name).includes("foo")); + assert.ok(result.map((r) => r.name).includes("foobar")); + }); + + it("matches slash-separated provider/model queries against reordered text", () => { + const item = { id: "gpt-5.5", provider: "openai-codex" }; + const result = fuzzyFilter([item], "openai-codex/gpt-5.5", (model) => `${model.id} ${model.provider}`); + + assert.deepStrictEqual(result, [item]); + }); +}); diff --git a/packages/tui/test/image-test.ts b/packages/tui/test/image-test.ts new file mode 100644 index 0000000000000000000000000000000000000000..73a26862d9f21ace7a08916fd81f5b3a0401145a --- /dev/null +++ b/packages/tui/test/image-test.ts @@ -0,0 +1,57 @@ +import { readFileSync } from "fs"; +import { Image } from "../src/components/image.ts"; +import { Spacer } from "../src/components/spacer.ts"; +import { Text } from "../src/components/text.ts"; +import { ProcessTerminal } from "../src/terminal.ts"; +import { getCapabilities, getImageDimensions } from "../src/terminal-image.ts"; +import type { TUI } from "../src/tui.ts"; +import { TuiMainScreen } from "../src/tui-main-screen.ts"; + +const testImagePath = process.argv[2] || "/tmp/test-image.png"; + +console.log("Terminal capabilities:", getCapabilities()); +console.log("Loading image from:", testImagePath); + +let imageBuffer: Buffer; +try { + imageBuffer = readFileSync(testImagePath); +} catch (_e) { + console.error(`Failed to load image: ${testImagePath}`); + console.error("Usage: npx tsx test/image-test.ts [path-to-image.png]"); + process.exit(1); +} + +const base64Data = imageBuffer.toString("base64"); +const dims = getImageDimensions(base64Data, "image/png"); + +console.log("Image dimensions:", dims); +console.log(""); + +const terminal = new ProcessTerminal(); +const tui: TUI = new TuiMainScreen(terminal); + +tui.addChild(new Text("Image Rendering Test", 1, 1)); +tui.addChild(new Spacer(1)); + +if (dims) { + tui.addChild( + new Image(base64Data, "image/png", { fallbackColor: (s) => `\x1b[33m${s}\x1b[0m` }, { maxWidthCells: 60 }, dims), + ); +} else { + tui.addChild(new Text("Could not parse image dimensions", 1, 0)); +} + +tui.addChild(new Spacer(1)); +tui.addChild(new Text("Press Ctrl+C to exit", 1, 0)); + +const editor = { + handleInput(data: string) { + if (data.charCodeAt(0) === 3) { + tui.stop(); + process.exit(0); + } + }, +}; + +tui.setFocus(editor as any); +tui.start(); diff --git a/packages/tui/test/input.test.ts b/packages/tui/test/input.test.ts new file mode 100644 index 0000000000000000000000000000000000000000..e1f3f81726fdbf1c4f552ad1dc397282fb6d8a9f --- /dev/null +++ b/packages/tui/test/input.test.ts @@ -0,0 +1,664 @@ +import assert from "node:assert"; +import { describe, it } from "node:test"; +import { Input } from "../src/components/input.ts"; +import { stripTerminalSequences, visibleWidth } from "../src/utils.ts"; + +describe("Input component", () => { + it("submits value including backslash on Enter", () => { + const input = new Input(); + let submitted: string | undefined; + + input.onSubmit = (value) => { + submitted = value; + }; + + // Type hello, then backslash, then Enter + input.handleInput("h"); + input.handleInput("e"); + input.handleInput("l"); + input.handleInput("l"); + input.handleInput("o"); + input.handleInput("\\"); + input.handleInput("\r"); + + // Input is single-line, no backslash+Enter workaround + assert.strictEqual(submitted, "hello\\"); + }); + + it("inserts backslash as regular character", () => { + const input = new Input(); + + input.handleInput("\\"); + input.handleInput("x"); + + assert.strictEqual(input.getValue(), "\\x"); + }); + + describe("render", () => { + it("supports a custom prompt and styled placeholder", () => { + const input = new Input({ + prompt: "", + placeholder: "Find transcript", + placeholderStyle: (text) => `\x1b[2m${text}\x1b[22m`, + }); + input.focused = true; + + const [empty] = input.render(20); + assert.ok(empty?.includes("\x1b[2m")); + assert.strictEqual(stripTerminalSequences(empty ?? "").trimEnd(), "Find transcript"); + + input.handleInput("n"); + const [populated] = input.render(20); + assert.strictEqual(stripTerminalSequences(populated ?? "").trimEnd(), "n"); + }); + + it("does not overflow with wide CJK and fullwidth text", () => { + const width = 93; + const cases = [ + "가나다라마바사아자차카타파하 한글 텍스트가 터미널 너비를 초과하면 크래시가 발생합니다 이것은 재현용 테스트입니다", + "これはテスト文章です。日本語のテキストが正しく表示されるかどうかを確認するためのサンプルテキストです。あいうえお", + "这是一段测试文本,用于验证中文字符在终端中的显示宽度是否被正确计算,如果不正确就会导致用户界面崩溃的问题", + "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklm", + ]; + const cursorPositions = [ + { label: "start", move: (_input: Input) => {} }, + { + label: "middle", + move: (input: Input) => { + for (let i = 0; i < 10; i++) input.handleInput("\x1b[C"); + }, + }, + { label: "end", move: (input: Input) => input.handleInput("\x05") }, + ]; + + for (const text of cases) { + for (const { label, move } of cursorPositions) { + const input = new Input(); + input.setValue(text); + input.focused = true; + move(input); + + const [line] = input.render(width); + assert.ok(line); + assert.ok(visibleWidth(line) <= width, `rendered line overflowed for ${text} at ${label}`); + } + } + }); + + it("keeps the cursor visible when horizontally scrolling wide text", () => { + const input = new Input(); + const width = 20; + const text = "가나다라마바사아자차카타파하"; + input.setValue(text); + input.focused = true; + input.handleInput("\x01"); + for (let i = 0; i < 5; i++) input.handleInput("\x1b[C"); + + const [line] = input.render(width); + assert.ok(line); + assert.ok(visibleWidth(line) <= width); + }); + }); + + describe("Kill ring", () => { + it("Ctrl+W saves deleted text to kill ring and Ctrl+Y yanks it", () => { + const input = new Input(); + + input.setValue("foo bar baz"); + // Move cursor to end + input.handleInput("\x05"); // Ctrl+E + + input.handleInput("\x17"); // Ctrl+W - deletes "baz" + assert.strictEqual(input.getValue(), "foo bar "); + + // Move to beginning and yank + input.handleInput("\x01"); // Ctrl+A + input.handleInput("\x19"); // Ctrl+Y + assert.strictEqual(input.getValue(), "bazfoo bar "); + }); + + it("Ctrl+W preserves ASCII punctuation boundaries", () => { + const input = new Input(); + + input.setValue("foo.bar"); + input.handleInput("\x05"); // Ctrl+E + input.handleInput("\x17"); // Ctrl+W - deletes "bar" + assert.strictEqual(input.getValue(), "foo."); + + input.setValue("foo:bar"); + input.handleInput("\x05"); // Ctrl+E + input.handleInput("\x17"); // Ctrl+W - deletes "bar" + assert.strictEqual(input.getValue(), "foo:"); + }); + + it("Ctrl+W handles Unicode word boundaries", () => { + const input = new Input(); + + // "你好世界。你好,世界" segments as: 你好|世界|。|你好|,|世界 + input.setValue("你好世界。你好,世界"); + input.handleInput("\x05"); // Ctrl+E + input.handleInput("\x17"); // Ctrl+W - deletes "世界" + assert.strictEqual(input.getValue(), "你好世界。你好,"); + input.handleInput("\x17"); // Ctrl+W - deletes "," + assert.strictEqual(input.getValue(), "你好世界。你好"); + input.handleInput("\x17"); // Ctrl+W - deletes "你好" + assert.strictEqual(input.getValue(), "你好世界。"); + input.handleInput("\x17"); // Ctrl+W - deletes "。" + assert.strictEqual(input.getValue(), "你好世界"); + input.handleInput("\x17"); // Ctrl+W - deletes "世界" + assert.strictEqual(input.getValue(), "你好"); + input.handleInput("\x17"); // Ctrl+W - deletes "你好" + assert.strictEqual(input.getValue(), ""); + }); + + it("Ctrl+U saves deleted text to kill ring", () => { + const input = new Input(); + + input.setValue("hello world"); + // Move cursor to after "hello " + input.handleInput("\x01"); // Ctrl+A + for (let i = 0; i < 6; i++) input.handleInput("\x1b[C"); + + input.handleInput("\x15"); // Ctrl+U - deletes "hello " + assert.strictEqual(input.getValue(), "world"); + + input.handleInput("\x19"); // Ctrl+Y + assert.strictEqual(input.getValue(), "hello world"); + }); + + it("Ctrl+K saves deleted text to kill ring", () => { + const input = new Input(); + + input.setValue("hello world"); + input.handleInput("\x01"); // Ctrl+A + input.handleInput("\x0b"); // Ctrl+K - deletes "hello world" + + assert.strictEqual(input.getValue(), ""); + + input.handleInput("\x19"); // Ctrl+Y + assert.strictEqual(input.getValue(), "hello world"); + }); + + it("Ctrl+Y does nothing when kill ring is empty", () => { + const input = new Input(); + + input.setValue("test"); + input.handleInput("\x05"); // Ctrl+E + input.handleInput("\x19"); // Ctrl+Y + assert.strictEqual(input.getValue(), "test"); + }); + + it("Alt+Y cycles through kill ring after Ctrl+Y", () => { + const input = new Input(); + + // Create kill ring with multiple entries + input.setValue("first"); + input.handleInput("\x05"); // Ctrl+E + input.handleInput("\x17"); // Ctrl+W - deletes "first" + input.setValue("second"); + input.handleInput("\x05"); // Ctrl+E + input.handleInput("\x17"); // Ctrl+W - deletes "second" + input.setValue("third"); + input.handleInput("\x05"); // Ctrl+E + input.handleInput("\x17"); // Ctrl+W - deletes "third" + + assert.strictEqual(input.getValue(), ""); + + input.handleInput("\x19"); // Ctrl+Y - yanks "third" + assert.strictEqual(input.getValue(), "third"); + + input.handleInput("\x1by"); // Alt+Y - cycles to "second" + assert.strictEqual(input.getValue(), "second"); + + input.handleInput("\x1by"); // Alt+Y - cycles to "first" + assert.strictEqual(input.getValue(), "first"); + + input.handleInput("\x1by"); // Alt+Y - cycles back to "third" + assert.strictEqual(input.getValue(), "third"); + }); + + it("Alt+Y does nothing if not preceded by yank", () => { + const input = new Input(); + + input.setValue("test"); + input.handleInput("\x05"); // Ctrl+E + input.handleInput("\x17"); // Ctrl+W - deletes "test" + input.setValue("other"); + input.handleInput("\x05"); // Ctrl+E + + // Type something to break the yank chain + input.handleInput("x"); + assert.strictEqual(input.getValue(), "otherx"); + + input.handleInput("\x1by"); // Alt+Y - should do nothing + assert.strictEqual(input.getValue(), "otherx"); + }); + + it("Alt+Y does nothing if kill ring has one entry", () => { + const input = new Input(); + + input.setValue("only"); + input.handleInput("\x05"); // Ctrl+E + input.handleInput("\x17"); // Ctrl+W - deletes "only" + + input.handleInput("\x19"); // Ctrl+Y - yanks "only" + assert.strictEqual(input.getValue(), "only"); + + input.handleInput("\x1by"); // Alt+Y - should do nothing + assert.strictEqual(input.getValue(), "only"); + }); + + it("consecutive Ctrl+W accumulates into one kill ring entry", () => { + const input = new Input(); + + input.setValue("one two three"); + input.handleInput("\x05"); // Ctrl+E + input.handleInput("\x17"); // Ctrl+W - deletes "three" + input.handleInput("\x17"); // Ctrl+W - deletes "two " + input.handleInput("\x17"); // Ctrl+W - deletes "one " + + assert.strictEqual(input.getValue(), ""); + + input.handleInput("\x19"); // Ctrl+Y + assert.strictEqual(input.getValue(), "one two three"); + }); + + it("non-delete actions break kill accumulation", () => { + const input = new Input(); + + input.setValue("foo bar baz"); + input.handleInput("\x05"); // Ctrl+E + input.handleInput("\x17"); // Ctrl+W - deletes "baz" + assert.strictEqual(input.getValue(), "foo bar "); + + input.handleInput("x"); // Typing breaks accumulation + assert.strictEqual(input.getValue(), "foo bar x"); + + input.handleInput("\x17"); // Ctrl+W - deletes "x" (separate entry) + assert.strictEqual(input.getValue(), "foo bar "); + + input.handleInput("\x19"); // Ctrl+Y - most recent is "x" + assert.strictEqual(input.getValue(), "foo bar x"); + + input.handleInput("\x1by"); // Alt+Y - cycle to "baz" + assert.strictEqual(input.getValue(), "foo bar baz"); + }); + + it("non-yank actions break Alt+Y chain", () => { + const input = new Input(); + + input.setValue("first"); + input.handleInput("\x05"); // Ctrl+E + input.handleInput("\x17"); // Ctrl+W + input.setValue("second"); + input.handleInput("\x05"); // Ctrl+E + input.handleInput("\x17"); // Ctrl+W + input.setValue(""); + + input.handleInput("\x19"); // Ctrl+Y - yanks "second" + assert.strictEqual(input.getValue(), "second"); + + input.handleInput("x"); // Breaks yank chain + assert.strictEqual(input.getValue(), "secondx"); + + input.handleInput("\x1by"); // Alt+Y - should do nothing + assert.strictEqual(input.getValue(), "secondx"); + }); + + it("kill ring rotation persists after cycling", () => { + const input = new Input(); + + input.setValue("first"); + input.handleInput("\x05"); // Ctrl+E + input.handleInput("\x17"); // deletes "first" + input.setValue("second"); + input.handleInput("\x05"); // Ctrl+E + input.handleInput("\x17"); // deletes "second" + input.setValue("third"); + input.handleInput("\x05"); // Ctrl+E + input.handleInput("\x17"); // deletes "third" + input.setValue(""); + + input.handleInput("\x19"); // Ctrl+Y - yanks "third" + input.handleInput("\x1by"); // Alt+Y - cycles to "second" + assert.strictEqual(input.getValue(), "second"); + + // Break chain and start fresh + input.handleInput("x"); + input.setValue(""); + + // New yank should get "second" (now at end after rotation) + input.handleInput("\x19"); // Ctrl+Y + assert.strictEqual(input.getValue(), "second"); + }); + + it("backward deletions prepend, forward deletions append during accumulation", () => { + const input = new Input(); + + input.setValue("prefix|suffix"); + // Position cursor at "|" + input.handleInput("\x01"); // Ctrl+A + for (let i = 0; i < 6; i++) input.handleInput("\x1b[C"); // Move right 6 + + input.handleInput("\x0b"); // Ctrl+K - deletes "|suffix" (forward) + assert.strictEqual(input.getValue(), "prefix"); + + input.handleInput("\x19"); // Ctrl+Y + assert.strictEqual(input.getValue(), "prefix|suffix"); + }); + + it("Alt+D deletes word forward and saves to kill ring", () => { + const input = new Input(); + + input.setValue("hello world test"); + input.handleInput("\x01"); // Ctrl+A + + input.handleInput("\x1bd"); // Alt+D - deletes "hello" + assert.strictEqual(input.getValue(), " world test"); + + input.handleInput("\x1bd"); // Alt+D - deletes " world" + assert.strictEqual(input.getValue(), " test"); + + // Yank should get accumulated text + input.handleInput("\x19"); // Ctrl+Y + assert.strictEqual(input.getValue(), "hello world test"); + }); + + it("Alt+D preserves ASCII punctuation boundaries", () => { + const input = new Input(); + + input.setValue("foo.bar baz"); + input.handleInput("\x01"); // Ctrl+A + input.handleInput("\x1bd"); // Alt+D - deletes "foo" + assert.strictEqual(input.getValue(), ".bar baz"); + input.handleInput("\x1bd"); // Alt+D - deletes "." + assert.strictEqual(input.getValue(), "bar baz"); + input.handleInput("\x1bd"); // Alt+D - deletes "bar" + assert.strictEqual(input.getValue(), " baz"); + }); + + it("Alt+D handles Unicode word boundaries", () => { + const input = new Input(); + + // "你好世界。你好,世界" segments as: 你好|世界|。|你好|,|世界 + input.setValue("你好世界。你好,世界"); + input.handleInput("\x01"); // Ctrl+A + input.handleInput("\x1bd"); // Alt+D - deletes "你好" + assert.strictEqual(input.getValue(), "世界。你好,世界"); + input.handleInput("\x1bd"); // Alt+D - deletes "世界" + assert.strictEqual(input.getValue(), "。你好,世界"); + input.handleInput("\x1bd"); // Alt+D - deletes "。" + assert.strictEqual(input.getValue(), "你好,世界"); + input.handleInput("\x1bd"); // Alt+D - deletes "你好" + assert.strictEqual(input.getValue(), ",世界"); + input.handleInput("\x1bd"); // Alt+D - deletes "," + assert.strictEqual(input.getValue(), "世界"); + input.handleInput("\x1bd"); // Alt+D - deletes "世界" + assert.strictEqual(input.getValue(), ""); + }); + + it("handles yank in middle of text", () => { + const input = new Input(); + + input.setValue("word"); + input.handleInput("\x05"); // Ctrl+E + input.handleInput("\x17"); // Ctrl+W - deletes "word" + input.setValue("hello world"); + // Move to middle (after "hello ") + input.handleInput("\x01"); // Ctrl+A + for (let i = 0; i < 6; i++) input.handleInput("\x1b[C"); + + input.handleInput("\x19"); // Ctrl+Y + assert.strictEqual(input.getValue(), "hello wordworld"); + }); + + it("handles yank-pop in middle of text", () => { + const input = new Input(); + + // Create two kill ring entries + input.setValue("FIRST"); + input.handleInput("\x05"); // Ctrl+E + input.handleInput("\x17"); // Ctrl+W - deletes "FIRST" + input.setValue("SECOND"); + input.handleInput("\x05"); // Ctrl+E + input.handleInput("\x17"); // Ctrl+W - deletes "SECOND" + + // Set up "hello world" and position cursor after "hello " + input.setValue("hello world"); + input.handleInput("\x01"); // Ctrl+A + for (let i = 0; i < 6; i++) input.handleInput("\x1b[C"); + + input.handleInput("\x19"); // Ctrl+Y - yanks "SECOND" + assert.strictEqual(input.getValue(), "hello SECONDworld"); + + input.handleInput("\x1by"); // Alt+Y - replaces with "FIRST" + assert.strictEqual(input.getValue(), "hello FIRSTworld"); + }); + }); + + describe("Undo", () => { + it("does nothing when undo stack is empty", () => { + const input = new Input(); + + input.handleInput("\x1b[45;5u"); // Ctrl+- (undo) + assert.strictEqual(input.getValue(), ""); + }); + + it("coalesces consecutive word characters into one undo unit", () => { + const input = new Input(); + + input.handleInput("h"); + input.handleInput("e"); + input.handleInput("l"); + input.handleInput("l"); + input.handleInput("o"); + input.handleInput(" "); + input.handleInput("w"); + input.handleInput("o"); + input.handleInput("r"); + input.handleInput("l"); + input.handleInput("d"); + assert.strictEqual(input.getValue(), "hello world"); + + // Undo removes " world" + input.handleInput("\x1b[45;5u"); // Ctrl+- (undo) + assert.strictEqual(input.getValue(), "hello"); + + // Undo removes "hello" + input.handleInput("\x1b[45;5u"); // Ctrl+- (undo) + assert.strictEqual(input.getValue(), ""); + }); + + it("undoes spaces one at a time", () => { + const input = new Input(); + + input.handleInput("h"); + input.handleInput("e"); + input.handleInput("l"); + input.handleInput("l"); + input.handleInput("o"); + input.handleInput(" "); + input.handleInput(" "); + assert.strictEqual(input.getValue(), "hello "); + + input.handleInput("\x1b[45;5u"); // Ctrl+- (undo) - removes second " " + assert.strictEqual(input.getValue(), "hello "); + + input.handleInput("\x1b[45;5u"); // Ctrl+- (undo) - removes first " " + assert.strictEqual(input.getValue(), "hello"); + + input.handleInput("\x1b[45;5u"); // Ctrl+- (undo) - removes "hello" + assert.strictEqual(input.getValue(), ""); + }); + + it("undoes backspace", () => { + const input = new Input(); + + input.handleInput("h"); + input.handleInput("e"); + input.handleInput("l"); + input.handleInput("l"); + input.handleInput("o"); + input.handleInput("\x7f"); // Backspace + assert.strictEqual(input.getValue(), "hell"); + + input.handleInput("\x1b[45;5u"); // Ctrl+- (undo) + assert.strictEqual(input.getValue(), "hello"); + }); + + it("undoes forward delete", () => { + const input = new Input(); + + input.handleInput("h"); + input.handleInput("e"); + input.handleInput("l"); + input.handleInput("l"); + input.handleInput("o"); + input.handleInput("\x01"); // Ctrl+A - go to start + input.handleInput("\x1b[C"); // Right arrow + input.handleInput("\x1b[3~"); // Delete key + assert.strictEqual(input.getValue(), "hllo"); + + input.handleInput("\x1b[45;5u"); // Ctrl+- (undo) + assert.strictEqual(input.getValue(), "hello"); + }); + + it("undoes Ctrl+W (delete word backward)", () => { + const input = new Input(); + + input.handleInput("h"); + input.handleInput("e"); + input.handleInput("l"); + input.handleInput("l"); + input.handleInput("o"); + input.handleInput(" "); + input.handleInput("w"); + input.handleInput("o"); + input.handleInput("r"); + input.handleInput("l"); + input.handleInput("d"); + assert.strictEqual(input.getValue(), "hello world"); + + input.handleInput("\x17"); // Ctrl+W + assert.strictEqual(input.getValue(), "hello "); + + input.handleInput("\x1b[45;5u"); // Ctrl+- (undo) + assert.strictEqual(input.getValue(), "hello world"); + }); + + it("undoes Ctrl+K (delete to line end)", () => { + const input = new Input(); + + input.handleInput("h"); + input.handleInput("e"); + input.handleInput("l"); + input.handleInput("l"); + input.handleInput("o"); + input.handleInput(" "); + input.handleInput("w"); + input.handleInput("o"); + input.handleInput("r"); + input.handleInput("l"); + input.handleInput("d"); + input.handleInput("\x01"); // Ctrl+A + for (let i = 0; i < 6; i++) input.handleInput("\x1b[C"); + + input.handleInput("\x0b"); // Ctrl+K + assert.strictEqual(input.getValue(), "hello "); + + input.handleInput("\x1b[45;5u"); // Ctrl+- (undo) + assert.strictEqual(input.getValue(), "hello world"); + }); + + it("undoes Ctrl+U (delete to line start)", () => { + const input = new Input(); + + input.handleInput("h"); + input.handleInput("e"); + input.handleInput("l"); + input.handleInput("l"); + input.handleInput("o"); + input.handleInput(" "); + input.handleInput("w"); + input.handleInput("o"); + input.handleInput("r"); + input.handleInput("l"); + input.handleInput("d"); + input.handleInput("\x01"); // Ctrl+A + for (let i = 0; i < 6; i++) input.handleInput("\x1b[C"); + + input.handleInput("\x15"); // Ctrl+U + assert.strictEqual(input.getValue(), "world"); + + input.handleInput("\x1b[45;5u"); // Ctrl+- (undo) + assert.strictEqual(input.getValue(), "hello world"); + }); + + it("undoes yank", () => { + const input = new Input(); + + input.handleInput("h"); + input.handleInput("e"); + input.handleInput("l"); + input.handleInput("l"); + input.handleInput("o"); + input.handleInput(" "); + input.handleInput("\x17"); // Ctrl+W - delete "hello " + input.handleInput("\x19"); // Ctrl+Y - yank + assert.strictEqual(input.getValue(), "hello "); + + input.handleInput("\x1b[45;5u"); // Ctrl+- (undo) + assert.strictEqual(input.getValue(), ""); + }); + + it("undoes paste atomically", () => { + const input = new Input(); + + input.setValue("hello world"); + input.handleInput("\x01"); // Ctrl+A + for (let i = 0; i < 5; i++) input.handleInput("\x1b[C"); + + // Simulate bracketed paste + input.handleInput("\x1b[200~beep boop\x1b[201~"); + assert.strictEqual(input.getValue(), "hellobeep boop world"); + + // Single undo should restore entire pre-paste state + input.handleInput("\x1b[45;5u"); // Ctrl+- (undo) + assert.strictEqual(input.getValue(), "hello world"); + }); + + it("undoes Alt+D (delete word forward)", () => { + const input = new Input(); + + input.setValue("hello world"); + input.handleInput("\x01"); // Ctrl+A + + input.handleInput("\x1bd"); // Alt+D - deletes "hello" + assert.strictEqual(input.getValue(), " world"); + + input.handleInput("\x1b[45;5u"); // Ctrl+- (undo) + assert.strictEqual(input.getValue(), "hello world"); + }); + + it("cursor movement starts new undo unit", () => { + const input = new Input(); + + input.handleInput("a"); + input.handleInput("b"); + input.handleInput("c"); + input.handleInput("\x01"); // Ctrl+A - movement breaks coalescing + input.handleInput("\x05"); // Ctrl+E + input.handleInput("d"); + input.handleInput("e"); + assert.strictEqual(input.getValue(), "abcde"); + + // Undo removes "de" (typed after movement) + input.handleInput("\x1b[45;5u"); // Ctrl+- (undo) + assert.strictEqual(input.getValue(), "abc"); + + // Undo removes "abc" + input.handleInput("\x1b[45;5u"); // Ctrl+- (undo) + assert.strictEqual(input.getValue(), ""); + }); + }); +}); diff --git a/packages/tui/test/key-tester.ts b/packages/tui/test/key-tester.ts new file mode 100644 index 0000000000000000000000000000000000000000..06373c7d76545425c6d1a0741b26fc5536471cb0 --- /dev/null +++ b/packages/tui/test/key-tester.ts @@ -0,0 +1,124 @@ +#!/usr/bin/env node +import { matchesKey } from "../src/keys.ts"; +import { ProcessTerminal } from "../src/terminal.ts"; +import type { Component, TUI } from "../src/tui.ts"; +import { TuiMainScreen } from "../src/tui-main-screen.ts"; +import { truncateToWidth } from "../src/utils.ts"; + +/** + * Simple key code logger component + */ +class KeyLogger implements Component { + private log: string[] = []; + private maxLines = 20; + private tui: TUI; + private terminal: ProcessTerminal; + + constructor(tui: TUI, terminal: ProcessTerminal) { + this.tui = tui; + this.terminal = terminal; + } + + handleInput(data: string): void { + // Handle Ctrl+C (raw or Kitty protocol) for exit + if (matchesKey(data, "ctrl+c")) { + this.tui.stop(); + console.log("\nExiting..."); + process.exit(0); + } + + // Convert to various representations + const hex = Buffer.from(data).toString("hex"); + const charCodes = Array.from(data) + .map((c) => c.charCodeAt(0)) + .join(", "); + const repr = data + .replace(/\x1b/g, "\\x1b") + .replace(/\r/g, "\\r") + .replace(/\n/g, "\\n") + .replace(/\t/g, "\\t") + .replace(/\x7f/g, "\\x7f"); + + const logLine = `Hex: ${hex.padEnd(20)} | Chars: [${charCodes.padEnd(15)}] | Repr: "${repr}"`; + + this.log.push(logLine); + + // Keep only last N lines + if (this.log.length > this.maxLines) { + this.log.shift(); + } + + // Request re-render to show the new log entry + this.tui.requestRender(); + } + + invalidate(): void { + // No cached state to invalidate currently + } + + private protocolName(): string { + if (this.terminal.kittyProtocolActive) return "kitty"; + if (this.terminal.modifyOtherKeysActive) return "modifyOtherKeys"; + return "legacy"; + } + + private fit(line: string, width: number): string { + return truncateToWidth(line, width).padEnd(width); + } + + render(width: number): string[] { + const lines: string[] = []; + + // Title + lines.push("=".repeat(width)); + lines.push(this.fit("Key Code Tester - Press keys to see their codes (Ctrl+C to exit)", width)); + lines.push(this.fit(`Protocol: ${this.protocolName()}`, width)); + lines.push("=".repeat(width)); + lines.push(""); + + // Log entries + for (const entry of this.log) { + lines.push(this.fit(entry, width)); + } + + // Fill remaining space + const remaining = Math.max(0, 25 - lines.length); + for (let i = 0; i < remaining; i++) { + lines.push("".padEnd(width)); + } + + // Footer + lines.push("=".repeat(width)); + lines.push(this.fit("Test these:", width)); + lines.push(this.fit(" - Shift + Enter (should show: \\x1b[13;2u with Kitty protocol)", width)); + lines.push(this.fit(" - Alt/Option + Enter", width)); + lines.push(this.fit(" - Option/Alt + Backspace", width)); + lines.push(this.fit(" - Cmd/Ctrl + Backspace", width)); + lines.push(this.fit(" - Regular Backspace", width)); + lines.push("=".repeat(width)); + + return lines; + } +} + +// Set up TUI +const terminal = new ProcessTerminal(); +const tui: TUI = new TuiMainScreen(terminal); +const logger = new KeyLogger(tui, terminal); + +tui.addChild(logger); +tui.setFocus(logger); + +// Handle Ctrl+C for clean exit (SIGINT still works for raw mode) +process.on("SIGINT", () => { + tui.stop(); + console.log("\nExiting..."); + process.exit(0); +}); + +// Start the TUI +tui.start(); + +// Protocol negotiation completes asynchronously after the first render. +// Refresh briefly/continuously so the displayed protocol state is not stale. +setInterval(() => tui.requestRender(), 100); diff --git a/packages/tui/test/keybindings.test.ts b/packages/tui/test/keybindings.test.ts new file mode 100644 index 0000000000000000000000000000000000000000..b0c827dde18e4c7f139ac2eeec19cb027f192d6e --- /dev/null +++ b/packages/tui/test/keybindings.test.ts @@ -0,0 +1,81 @@ +import assert from "node:assert"; +import { describe, it } from "node:test"; +import { KeybindingsManager, TUI_KEYBINDINGS } from "../src/keybindings.ts"; + +describe("KeybindingsManager", () => { + it("binds Ctrl+J as a default newline alias", () => { + const keybindings = new KeybindingsManager(TUI_KEYBINDINGS); + + assert.deepStrictEqual(keybindings.getKeys("tui.input.newLine"), ["shift+enter", "ctrl+j"]); + assert.strictEqual(keybindings.matches("\n", "tui.input.newLine"), true); + assert.strictEqual(keybindings.matches("\x1b[106;5u", "tui.input.newLine"), true); + }); + + it("binds modified and unmodified editor viewport navigation", () => { + const keybindings = new KeybindingsManager(TUI_KEYBINDINGS); + + assert.deepStrictEqual(keybindings.getKeys("tui.editor.cursorLineStart"), ["home", "ctrl+home", "ctrl+a"]); + assert.deepStrictEqual(keybindings.getKeys("tui.editor.cursorLineEnd"), ["end", "ctrl+end", "ctrl+e"]); + assert.deepStrictEqual(keybindings.getKeys("tui.editor.pageUp"), ["pageUp", "ctrl+pageUp"]); + assert.deepStrictEqual(keybindings.getKeys("tui.editor.pageDown"), ["pageDown", "ctrl+pageDown"]); + }); + + it("leaves dedicated prompt history navigation unbound by default", () => { + const keybindings = new KeybindingsManager(TUI_KEYBINDINGS); + + assert.deepStrictEqual(keybindings.getKeys("tui.editor.historyPrevious"), []); + assert.deepStrictEqual(keybindings.getKeys("tui.editor.historyNext"), []); + }); + + it("binds unmodified terminal viewport shortcuts to alternate-screen navigation", () => { + const keybindings = new KeybindingsManager(TUI_KEYBINDINGS); + + assert.deepStrictEqual(keybindings.getKeys("tui.altScreen.pageUp"), ["pageUp"]); + assert.deepStrictEqual(keybindings.getKeys("tui.altScreen.pageDown"), ["pageDown"]); + assert.deepStrictEqual(keybindings.getKeys("tui.altScreen.halfPageUp"), []); + assert.deepStrictEqual(keybindings.getKeys("tui.altScreen.halfPageDown"), []); + assert.deepStrictEqual(keybindings.getKeys("tui.altScreen.lineUp"), []); + assert.deepStrictEqual(keybindings.getKeys("tui.altScreen.lineDown"), []); + assert.deepStrictEqual(keybindings.getKeys("tui.altScreen.previousPrompt"), ["ctrl+shift+up", "ctrl+up"]); + assert.deepStrictEqual(keybindings.getKeys("tui.altScreen.nextPrompt"), ["ctrl+shift+down", "ctrl+down"]); + assert.deepStrictEqual(keybindings.getKeys("tui.altScreen.search"), ["ctrl+shift+f"]); + assert.deepStrictEqual(keybindings.getKeys("tui.altScreen.searchNext"), ["enter", "ctrl+g"]); + assert.deepStrictEqual(keybindings.getKeys("tui.altScreen.searchPrevious"), ["shift+enter", "ctrl+shift+g"]); + assert.deepStrictEqual(keybindings.getKeys("tui.altScreen.searchClose"), ["escape"]); + assert.deepStrictEqual(keybindings.getKeys("tui.altScreen.top"), ["home"]); + assert.deepStrictEqual(keybindings.getKeys("tui.altScreen.bottom"), ["end"]); + }); + + it("does not evict selector confirm when input submit is rebound", () => { + const keybindings = new KeybindingsManager(TUI_KEYBINDINGS, { + "tui.input.submit": ["enter", "ctrl+enter"], + }); + + assert.deepStrictEqual(keybindings.getKeys("tui.input.submit"), ["enter", "ctrl+enter"]); + assert.deepStrictEqual(keybindings.getKeys("tui.select.confirm"), ["enter"]); + }); + + it("does not evict cursor bindings when another action reuses the same key", () => { + const keybindings = new KeybindingsManager(TUI_KEYBINDINGS, { + "tui.select.up": ["up", "ctrl+p"], + }); + + assert.deepStrictEqual(keybindings.getKeys("tui.select.up"), ["up", "ctrl+p"]); + assert.deepStrictEqual(keybindings.getKeys("tui.editor.cursorUp"), ["up"]); + }); + + it("still reports direct user binding conflicts without evicting defaults", () => { + const keybindings = new KeybindingsManager(TUI_KEYBINDINGS, { + "tui.input.submit": "ctrl+x", + "tui.select.confirm": "ctrl+x", + }); + + assert.deepStrictEqual(keybindings.getConflicts(), [ + { + key: "ctrl+x", + keybindings: ["tui.input.submit", "tui.select.confirm"], + }, + ]); + assert.deepStrictEqual(keybindings.getKeys("tui.editor.cursorLeft"), ["left", "ctrl+b"]); + }); +}); diff --git a/packages/tui/test/keys.test.ts b/packages/tui/test/keys.test.ts new file mode 100644 index 0000000000000000000000000000000000000000..68f594359c1311c8cff69ff5dac924d2da9df5be --- /dev/null +++ b/packages/tui/test/keys.test.ts @@ -0,0 +1,633 @@ +/** + * Tests for keyboard input handling + */ + +import assert from "node:assert"; +import { describe, it } from "node:test"; +import { + decodeKittyPrintable, + decodePrintableKey, + Key, + matchesKey, + parseKey, + setKittyProtocolActive, +} from "../src/keys.ts"; + +function withEnv(name: string, value: string | undefined, fn: () => void): void { + const previous = process.env[name]; + if (value === undefined) delete process.env[name]; + else process.env[name] = value; + try { + fn(); + } finally { + if (previous === undefined) delete process.env[name]; + else process.env[name] = previous; + } +} + +function withEnvVars(vars: Record, fn: () => void): void { + const entries = Object.entries(vars); + const run = (index: number): void => { + if (index >= entries.length) { + fn(); + return; + } + const [name, value] = entries[index]!; + withEnv(name, value, () => run(index + 1)); + }; + run(0); +} + +describe("matchesKey", () => { + describe("Kitty protocol with alternate keys (non-Latin layouts)", () => { + // Kitty protocol flag 4 (Report alternate keys) sends: + // CSI codepoint:shifted:base ; modifier:event u + // Where base is the key in standard PC-101 layout + + it("should match Ctrl+c when pressing Ctrl+С (Cyrillic) with base layout key", () => { + setKittyProtocolActive(true); + // Cyrillic 'с' = codepoint 1089, Latin 'c' = codepoint 99 + // Format: CSI 1089::99;5u (codepoint::base;modifier with ctrl=4, +1=5) + const cyrillicCtrlC = "\x1b[1089::99;5u"; + assert.strictEqual(matchesKey(cyrillicCtrlC, "ctrl+c"), true); + setKittyProtocolActive(false); + }); + + it("should match Ctrl+d when pressing Ctrl+В (Cyrillic) with base layout key", () => { + setKittyProtocolActive(true); + // Cyrillic 'в' = codepoint 1074, Latin 'd' = codepoint 100 + const cyrillicCtrlD = "\x1b[1074::100;5u"; + assert.strictEqual(matchesKey(cyrillicCtrlD, "ctrl+d"), true); + setKittyProtocolActive(false); + }); + + it("should match Ctrl+z when pressing Ctrl+Я (Cyrillic) with base layout key", () => { + setKittyProtocolActive(true); + // Cyrillic 'я' = codepoint 1103, Latin 'z' = codepoint 122 + const cyrillicCtrlZ = "\x1b[1103::122;5u"; + assert.strictEqual(matchesKey(cyrillicCtrlZ, "ctrl+z"), true); + setKittyProtocolActive(false); + }); + + it("should match Ctrl+Shift+p with base layout key", () => { + setKittyProtocolActive(true); + // Cyrillic 'з' = codepoint 1079, Latin 'p' = codepoint 112 + // ctrl=4, shift=1, +1 = 6 + const cyrillicCtrlShiftP = "\x1b[1079::112;6u"; + assert.strictEqual(matchesKey(cyrillicCtrlShiftP, "ctrl+shift+p"), true); + setKittyProtocolActive(false); + }); + + it("should still match direct codepoint when no base layout key", () => { + setKittyProtocolActive(true); + // Latin ctrl+c without base layout key (terminal doesn't support flag 4) + const latinCtrlC = "\x1b[99;5u"; + assert.strictEqual(matchesKey(latinCtrlC, "ctrl+c"), true); + setKittyProtocolActive(false); + }); + + it("should match super-modified Kitty bindings, including combined modifiers", () => { + setKittyProtocolActive(true); + assert.strictEqual(matchesKey("\x1b[107;9u", "super+k"), true); + assert.strictEqual(matchesKey("\x1b[13;9u", "super+enter"), true); + assert.strictEqual(matchesKey("\x1b[107;13u", Key.ctrlSuper("k")), true); + assert.strictEqual(matchesKey("\x1b[107;13u", "ctrl+super+k"), true); + assert.strictEqual(matchesKey("\x1b[107;14u", "ctrl+shift+super+k"), true); + assert.strictEqual(matchesKey("\x1b[107;13u", "super+k"), false); + assert.strictEqual(parseKey("\x1b[107;9u"), "super+k"); + assert.strictEqual(parseKey("\x1b[13;9u"), "super+enter"); + assert.strictEqual(parseKey("\x1b[107;13u"), "ctrl+super+k"); + assert.strictEqual(parseKey("\x1b[107;14u"), "shift+ctrl+super+k"); + setKittyProtocolActive(false); + }); + + it("should match digit bindings via Kitty CSI-u", () => { + setKittyProtocolActive(true); + assert.strictEqual(matchesKey("\x1b[49u", "1"), true); + assert.strictEqual(matchesKey("\x1b[49;5u", "ctrl+1"), true); + assert.strictEqual(matchesKey("\x1b[49;5u", "ctrl+2"), false); + assert.strictEqual(parseKey("\x1b[49u"), "1"); + assert.strictEqual(parseKey("\x1b[49;5u"), "ctrl+1"); + setKittyProtocolActive(false); + }); + + it("should normalize Kitty keypad functional keys to logical digits, symbols, and navigation", () => { + setKittyProtocolActive(true); + assert.strictEqual(matchesKey("\x1b[57400u", "1"), true); + assert.strictEqual(matchesKey("\x1b[57410u", "/"), true); + assert.strictEqual(matchesKey("\x1b[57417u", "left"), true); + assert.strictEqual(matchesKey("\x1b[57426u", "delete"), true); + assert.strictEqual(parseKey("\x1b[57399u"), "0"); + assert.strictEqual(parseKey("\x1b[57409u"), "."); + assert.strictEqual(parseKey("\x1b[57413u"), "+"); + assert.strictEqual(parseKey("\x1b[57416u"), ","); + assert.strictEqual(parseKey("\x1b[57417u"), "left"); + assert.strictEqual(parseKey("\x1b[57418u"), "right"); + assert.strictEqual(parseKey("\x1b[57419u"), "up"); + assert.strictEqual(parseKey("\x1b[57420u"), "down"); + assert.strictEqual(parseKey("\x1b[57421u"), "pageUp"); + assert.strictEqual(parseKey("\x1b[57422u"), "pageDown"); + assert.strictEqual(parseKey("\x1b[57423u"), "home"); + assert.strictEqual(parseKey("\x1b[57424u"), "end"); + assert.strictEqual(parseKey("\x1b[57425u"), "insert"); + assert.strictEqual(parseKey("\x1b[57426u"), "delete"); + setKittyProtocolActive(false); + }); + + it("should handle shifted key in format", () => { + setKittyProtocolActive(true); + // Format with shifted key: CSI codepoint:shifted:base;modifier u + // Latin 'c' with shifted 'C' (67) and base 'c' (99) + const shiftedKey = "\x1b[99:67:99;2u"; // shift modifier = 1, +1 = 2 + assert.strictEqual(matchesKey(shiftedKey, "shift+c"), true); + setKittyProtocolActive(false); + }); + + it("should handle event type in format", () => { + setKittyProtocolActive(true); + // Format with event type: CSI codepoint::base;modifier:event u + // Cyrillic ctrl+c release event (event type 3) + const releaseEvent = "\x1b[1089::99;5:3u"; + assert.strictEqual(matchesKey(releaseEvent, "ctrl+c"), true); + setKittyProtocolActive(false); + }); + + it("should handle full format with shifted key, base key, and event type", () => { + setKittyProtocolActive(true); + // Full format: CSI codepoint:shifted:base;modifier:event u + // Cyrillic 'С' (shifted) with base 'c', Ctrl+Shift pressed, repeat event + // Cyrillic 'с' = 1089, Cyrillic 'С' = 1057, Latin 'c' = 99 + // ctrl=4, shift=1, +1 = 6, repeat event = 2 + const fullFormat = "\x1b[1089:1057:99;6:2u"; + assert.strictEqual(matchesKey(fullFormat, "ctrl+shift+c"), true); + setKittyProtocolActive(false); + }); + + it("should prefer codepoint for Latin letters even when base layout differs", () => { + setKittyProtocolActive(true); + // Dvorak Ctrl+K reports codepoint 'k' (107) and base layout 'v' (118) + const dvorakCtrlK = "\x1b[107::118;5u"; + assert.strictEqual(matchesKey(dvorakCtrlK, "ctrl+k"), true); + assert.strictEqual(matchesKey(dvorakCtrlK, "ctrl+v"), false); + setKittyProtocolActive(false); + }); + + it("should prefer codepoint for symbol keys even when base layout differs", () => { + setKittyProtocolActive(true); + // Dvorak Ctrl+/ reports codepoint '/' (47) and base layout '[' (91) + const dvorakCtrlSlash = "\x1b[47::91;5u"; + assert.strictEqual(matchesKey(dvorakCtrlSlash, "ctrl+/"), true); + assert.strictEqual(matchesKey(dvorakCtrlSlash, "ctrl+["), false); + setKittyProtocolActive(false); + }); + + it("should not match wrong key even with base layout", () => { + setKittyProtocolActive(true); + // Cyrillic ctrl+с with base 'c' should NOT match ctrl+d + const cyrillicCtrlC = "\x1b[1089::99;5u"; + assert.strictEqual(matchesKey(cyrillicCtrlC, "ctrl+d"), false); + setKittyProtocolActive(false); + }); + + it("should not match wrong modifiers even with base layout", () => { + setKittyProtocolActive(true); + // Cyrillic ctrl+с should NOT match ctrl+shift+c + const cyrillicCtrlC = "\x1b[1089::99;5u"; + assert.strictEqual(matchesKey(cyrillicCtrlC, "ctrl+shift+c"), false); + setKittyProtocolActive(false); + }); + }); + + describe("modifyOtherKeys matching", () => { + it("should match xterm modifyOtherKeys Ctrl+c", () => { + setKittyProtocolActive(false); + assert.strictEqual(matchesKey("\x1b[27;5;99~", "ctrl+c"), true); + assert.strictEqual(parseKey("\x1b[27;5;99~"), "ctrl+c"); + }); + + it("should match xterm modifyOtherKeys Ctrl+d", () => { + setKittyProtocolActive(false); + assert.strictEqual(matchesKey("\x1b[27;5;100~", "ctrl+d"), true); + assert.strictEqual(parseKey("\x1b[27;5;100~"), "ctrl+d"); + }); + + it("should match xterm modifyOtherKeys Ctrl+z", () => { + setKittyProtocolActive(false); + assert.strictEqual(matchesKey("\x1b[27;5;122~", "ctrl+z"), true); + assert.strictEqual(parseKey("\x1b[27;5;122~"), "ctrl+z"); + }); + + it("should match xterm modifyOtherKeys Enter variants", () => { + setKittyProtocolActive(false); + assert.strictEqual(matchesKey("\x1b[27;5;13~", "ctrl+enter"), true); + assert.strictEqual(matchesKey("\x1b[27;2;13~", "shift+enter"), true); + assert.strictEqual(matchesKey("\x1b[27;3;13~", "alt+enter"), true); + assert.strictEqual(parseKey("\x1b[27;5;13~"), "ctrl+enter"); + assert.strictEqual(parseKey("\x1b[27;2;13~"), "shift+enter"); + assert.strictEqual(parseKey("\x1b[27;3;13~"), "alt+enter"); + }); + + it("should match xterm modifyOtherKeys Tab variants", () => { + setKittyProtocolActive(false); + assert.strictEqual(matchesKey("\x1b[27;2;9~", "shift+tab"), true); + assert.strictEqual(matchesKey("\x1b[27;5;9~", "ctrl+tab"), true); + assert.strictEqual(matchesKey("\x1b[27;3;9~", "alt+tab"), true); + assert.strictEqual(parseKey("\x1b[27;2;9~"), "shift+tab"); + assert.strictEqual(parseKey("\x1b[27;5;9~"), "ctrl+tab"); + assert.strictEqual(parseKey("\x1b[27;3;9~"), "alt+tab"); + }); + + it("should match xterm modifyOtherKeys Backspace variants", () => { + setKittyProtocolActive(false); + assert.strictEqual(matchesKey("\x1b[27;1;127~", "backspace"), true); + assert.strictEqual(matchesKey("\x1b[27;5;127~", "ctrl+backspace"), true); + assert.strictEqual(matchesKey("\x1b[27;3;127~", "alt+backspace"), true); + assert.strictEqual(parseKey("\x1b[27;1;127~"), "backspace"); + assert.strictEqual(parseKey("\x1b[27;5;127~"), "ctrl+backspace"); + assert.strictEqual(parseKey("\x1b[27;3;127~"), "alt+backspace"); + }); + + it("should match xterm modifyOtherKeys Escape", () => { + setKittyProtocolActive(false); + assert.strictEqual(matchesKey("\x1b[27;1;27~", "escape"), true); + assert.strictEqual(parseKey("\x1b[27;1;27~"), "escape"); + }); + + it("should match xterm modifyOtherKeys Space variants", () => { + setKittyProtocolActive(false); + assert.strictEqual(matchesKey("\x1b[27;1;32~", "space"), true); + assert.strictEqual(matchesKey("\x1b[27;5;32~", "ctrl+space"), true); + assert.strictEqual(parseKey("\x1b[27;1;32~"), "space"); + assert.strictEqual(parseKey("\x1b[27;5;32~"), "ctrl+space"); + }); + + it("should match xterm modifyOtherKeys symbol combos", () => { + setKittyProtocolActive(false); + assert.strictEqual(matchesKey("\x1b[27;5;47~", "ctrl+/"), true); + assert.strictEqual(parseKey("\x1b[27;5;47~"), "ctrl+/"); + }); + + it("should match xterm modifyOtherKeys digit combos", () => { + setKittyProtocolActive(false); + assert.strictEqual(matchesKey("\x1b[27;5;49~", "ctrl+1"), true); + assert.strictEqual(matchesKey("\x1b[27;2;49~", "shift+1"), true); + assert.strictEqual(parseKey("\x1b[27;5;49~"), "ctrl+1"); + assert.strictEqual(parseKey("\x1b[27;2;49~"), "shift+1"); + }); + + it("should match xterm modifyOtherKeys shifted uppercase letters", () => { + setKittyProtocolActive(false); + assert.strictEqual(matchesKey("\x1b[27;2;69~", "shift+e"), true); + assert.strictEqual(matchesKey("\x1b[27;6;69~", "ctrl+shift+e"), true); + assert.strictEqual(parseKey("\x1b[27;2;69~"), "shift+e"); + assert.strictEqual(parseKey("\x1b[27;6;69~"), "shift+ctrl+e"); + }); + + it("should match Ctrl+Alt+letter via CSI-u when kitty inactive", () => { + setKittyProtocolActive(false); + assert.strictEqual(matchesKey("\x1b[104;7u", "ctrl+alt+h"), true); + assert.strictEqual(parseKey("\x1b[104;7u"), "ctrl+alt+h"); + }); + + it("should match Ctrl+Alt+letter via xterm modifyOtherKeys", () => { + setKittyProtocolActive(false); + assert.strictEqual(matchesKey("\x1b[27;7;104~", "ctrl+alt+h"), true); + assert.strictEqual(parseKey("\x1b[27;7;104~"), "ctrl+alt+h"); + }); + }); + + describe("Legacy key matching", () => { + it("should match legacy Ctrl+c", () => { + setKittyProtocolActive(false); + // Ctrl+c sends ASCII 3 (ETX) + assert.strictEqual(matchesKey("\x03", "ctrl+c"), true); + }); + + it("should match legacy Ctrl+d", () => { + setKittyProtocolActive(false); + // Ctrl+d sends ASCII 4 (EOT) + assert.strictEqual(matchesKey("\x04", "ctrl+d"), true); + }); + + it("should match escape key", () => { + assert.strictEqual(matchesKey("\x1b", "escape"), true); + }); + + it("should match legacy linefeed as enter", () => { + setKittyProtocolActive(false); + assert.strictEqual(matchesKey("\n", "enter"), true); + assert.strictEqual(parseKey("\n"), "enter"); + }); + + it("should treat linefeed as shift+enter when kitty active", () => { + setKittyProtocolActive(true); + assert.strictEqual(matchesKey("\n", "shift+enter"), true); + assert.strictEqual(matchesKey("\n", "enter"), false); + assert.strictEqual(parseKey("\n"), "shift+enter"); + setKittyProtocolActive(false); + }); + + it("should parse ctrl+space", () => { + setKittyProtocolActive(false); + assert.strictEqual(matchesKey("\x00", "ctrl+space"), true); + assert.strictEqual(parseKey("\x00"), "ctrl+space"); + }); + + it("should match legacy Ctrl+symbol", () => { + setKittyProtocolActive(false); + // Ctrl+\ sends ASCII 28 (File Separator) in legacy terminals + assert.strictEqual(matchesKey("\x1c", "ctrl+\\"), true); + assert.strictEqual(parseKey("\x1c"), "ctrl+\\"); + // Ctrl+] sends ASCII 29 (Group Separator) in legacy terminals + assert.strictEqual(matchesKey("\x1d", "ctrl+]"), true); + assert.strictEqual(parseKey("\x1d"), "ctrl+]"); + // Ctrl+_ sends ASCII 31 (Unit Separator) in legacy terminals + // Ctrl+- is on the same physical key on US keyboards + assert.strictEqual(matchesKey("\x1f", "ctrl+_"), true); + assert.strictEqual(matchesKey("\x1f", "ctrl+-"), true); + assert.strictEqual(parseKey("\x1f"), "ctrl+-"); + }); + + it("should match legacy Ctrl+Alt+symbol", () => { + setKittyProtocolActive(false); + // Ctrl+Alt+[ sends ESC followed by ESC (Ctrl+[ = ESC) + assert.strictEqual(matchesKey("\x1b\x1b", "ctrl+alt+["), true); + assert.strictEqual(parseKey("\x1b\x1b"), "ctrl+alt+["); + // Ctrl+Alt+\ sends ESC followed by ASCII 28 + assert.strictEqual(matchesKey("\x1b\x1c", "ctrl+alt+\\"), true); + assert.strictEqual(parseKey("\x1b\x1c"), "ctrl+alt+\\"); + // Ctrl+Alt+] sends ESC followed by ASCII 29 + assert.strictEqual(matchesKey("\x1b\x1d", "ctrl+alt+]"), true); + assert.strictEqual(parseKey("\x1b\x1d"), "ctrl+alt+]"); + // Ctrl+_ sends ASCII 31 (Unit Separator) in legacy terminals + // Ctrl+- is on the same physical key on US keyboards + assert.strictEqual(matchesKey("\x1b\x1f", "ctrl+alt+_"), true); + assert.strictEqual(matchesKey("\x1b\x1f", "ctrl+alt+-"), true); + assert.strictEqual(parseKey("\x1b\x1f"), "ctrl+alt+-"); + }); + + it("should treat raw 0x08 as plain backspace outside Windows Terminal", () => { + setKittyProtocolActive(false); + withEnv("WT_SESSION", undefined, () => { + assert.strictEqual(matchesKey("\x7f", "backspace"), true); + assert.strictEqual(matchesKey("\x7f", "ctrl+backspace"), false); + assert.strictEqual(parseKey("\x7f"), "backspace"); + assert.strictEqual(matchesKey("\x08", "backspace"), true); + assert.strictEqual(matchesKey("\x08", "ctrl+backspace"), false); + assert.strictEqual(parseKey("\x08"), "backspace"); + assert.strictEqual(matchesKey("\x08", "ctrl+h"), true); + }); + }); + + it("should treat raw 0x08 as ctrl+backspace in local Windows Terminal", () => { + setKittyProtocolActive(false); + withEnvVars( + { + WT_SESSION: "test-session", + SSH_CONNECTION: undefined, + SSH_CLIENT: undefined, + SSH_TTY: undefined, + }, + () => { + assert.strictEqual(matchesKey("\x08", "ctrl+backspace"), true); + assert.strictEqual(matchesKey("\x08", "backspace"), false); + assert.strictEqual(parseKey("\x08"), "ctrl+backspace"); + assert.strictEqual(matchesKey("\x08", "ctrl+h"), true); + }, + ); + }); + + it("should treat raw 0x08 as plain backspace in Windows Terminal over SSH", () => { + setKittyProtocolActive(false); + withEnvVars( + { + WT_SESSION: "test-session", + SSH_CONNECTION: "1 2 3 4", + SSH_CLIENT: "1 2 3", + SSH_TTY: "/dev/pts/1", + }, + () => { + assert.strictEqual(matchesKey("\x08", "ctrl+backspace"), false); + assert.strictEqual(matchesKey("\x08", "backspace"), true); + assert.strictEqual(parseKey("\x08"), "backspace"); + assert.strictEqual(matchesKey("\x08", "ctrl+h"), true); + }, + ); + }); + + it("should parse legacy alt-prefixed sequences when kitty inactive", () => { + setKittyProtocolActive(false); + assert.strictEqual(matchesKey("\x1b ", "alt+space"), true); + assert.strictEqual(parseKey("\x1b "), "alt+space"); + assert.strictEqual(matchesKey("\x1b\b", "alt+backspace"), true); + assert.strictEqual(parseKey("\x1b\b"), "alt+backspace"); + assert.strictEqual(matchesKey("\x1b\x03", "ctrl+alt+c"), true); + assert.strictEqual(parseKey("\x1b\x03"), "ctrl+alt+c"); + assert.strictEqual(matchesKey("\x1bB", "alt+left"), true); + assert.strictEqual(parseKey("\x1bB"), "alt+left"); + assert.strictEqual(matchesKey("\x1bF", "alt+right"), true); + assert.strictEqual(parseKey("\x1bF"), "alt+right"); + assert.strictEqual(matchesKey("\x1ba", "alt+a"), true); + assert.strictEqual(parseKey("\x1ba"), "alt+a"); + assert.strictEqual(matchesKey("\x1b1", "alt+1"), true); + assert.strictEqual(parseKey("\x1b1"), "alt+1"); + assert.strictEqual(matchesKey("\x1b,", "alt+,"), true); + assert.strictEqual(parseKey("\x1b,"), "alt+,"); + assert.strictEqual(matchesKey("\x1b.", "alt+."), true); + assert.strictEqual(parseKey("\x1b."), "alt+."); + assert.strictEqual(matchesKey("\x1by", "alt+y"), true); + assert.strictEqual(parseKey("\x1by"), "alt+y"); + assert.strictEqual(matchesKey("\x1bz", "alt+z"), true); + assert.strictEqual(parseKey("\x1bz"), "alt+z"); + + setKittyProtocolActive(true); + assert.strictEqual(matchesKey("\x1b ", "alt+space"), false); + assert.strictEqual(parseKey("\x1b "), undefined); + assert.strictEqual(matchesKey("\x1b\b", "alt+backspace"), true); + assert.strictEqual(parseKey("\x1b\b"), "alt+backspace"); + assert.strictEqual(matchesKey("\x1b\x03", "ctrl+alt+c"), false); + assert.strictEqual(parseKey("\x1b\x03"), undefined); + assert.strictEqual(matchesKey("\x1bB", "alt+left"), false); + assert.strictEqual(parseKey("\x1bB"), undefined); + assert.strictEqual(matchesKey("\x1bF", "alt+right"), false); + assert.strictEqual(parseKey("\x1bF"), undefined); + assert.strictEqual(matchesKey("\x1ba", "alt+a"), false); + assert.strictEqual(parseKey("\x1ba"), undefined); + assert.strictEqual(matchesKey("\x1b1", "alt+1"), false); + assert.strictEqual(parseKey("\x1b1"), undefined); + assert.strictEqual(matchesKey("\x1b,", "alt+,"), false); + assert.strictEqual(parseKey("\x1b,"), undefined); + assert.strictEqual(matchesKey("\x1b.", "alt+."), false); + assert.strictEqual(parseKey("\x1b."), undefined); + assert.strictEqual(matchesKey("\x1by", "alt+y"), false); + assert.strictEqual(parseKey("\x1by"), undefined); + setKittyProtocolActive(false); + }); + + it("should match arrow keys", () => { + assert.strictEqual(matchesKey("\x1b[A", "up"), true); + assert.strictEqual(matchesKey("\x1b[B", "down"), true); + assert.strictEqual(matchesKey("\x1b[C", "right"), true); + assert.strictEqual(matchesKey("\x1b[D", "left"), true); + }); + + it("should match SS3 arrows and home/end", () => { + assert.strictEqual(matchesKey("\x1bOA", "up"), true); + assert.strictEqual(matchesKey("\x1bOB", "down"), true); + assert.strictEqual(matchesKey("\x1bOC", "right"), true); + assert.strictEqual(matchesKey("\x1bOD", "left"), true); + assert.strictEqual(matchesKey("\x1bOH", "home"), true); + assert.strictEqual(matchesKey("\x1bOF", "end"), true); + }); + + it("should match xterm Ctrl-modified viewport navigation", () => { + assert.strictEqual(matchesKey("\x1b[1;5H", "ctrl+home"), true); + assert.strictEqual(matchesKey("\x1b[1;5F", "ctrl+end"), true); + assert.strictEqual(matchesKey("\x1b[5;5~", "ctrl+pageUp"), true); + assert.strictEqual(matchesKey("\x1b[6;5~", "ctrl+pageDown"), true); + assert.strictEqual(parseKey("\x1b[1;5H"), "ctrl+home"); + assert.strictEqual(parseKey("\x1b[1;5F"), "ctrl+end"); + assert.strictEqual(parseKey("\x1b[5;5~"), "ctrl+pageUp"); + assert.strictEqual(parseKey("\x1b[6;5~"), "ctrl+pageDown"); + }); + + it("should match legacy function keys and clear", () => { + assert.strictEqual(matchesKey("\x1bOP", "f1"), true); + assert.strictEqual(matchesKey("\x1b[24~", "f12"), true); + assert.strictEqual(matchesKey("\x1b[E", "clear"), true); + }); + + it("should match alt+arrows", () => { + assert.strictEqual(matchesKey("\x1bp", "alt+up"), true); + assert.strictEqual(matchesKey("\x1bp", "up"), false); + }); + + it("should match rxvt modifier sequences", () => { + assert.strictEqual(matchesKey("\x1b[a", "shift+up"), true); + assert.strictEqual(matchesKey("\x1bOa", "ctrl+up"), true); + assert.strictEqual(matchesKey("\x1b[2$", "shift+insert"), true); + assert.strictEqual(matchesKey("\x1b[2^", "ctrl+insert"), true); + assert.strictEqual(matchesKey("\x1b[7$", "shift+home"), true); + }); + }); +}); + +describe("decodeKittyPrintable", () => { + it("should decode Kitty keypad functional keys to printable characters", () => { + assert.strictEqual(decodeKittyPrintable("\x1b[57399u"), "0"); + assert.strictEqual(decodeKittyPrintable("\x1b[57400u"), "1"); + assert.strictEqual(decodeKittyPrintable("\x1b[57409u"), "."); + assert.strictEqual(decodeKittyPrintable("\x1b[57410u"), "/"); + assert.strictEqual(decodeKittyPrintable("\x1b[57411u"), "*"); + assert.strictEqual(decodeKittyPrintable("\x1b[57412u"), "-"); + assert.strictEqual(decodeKittyPrintable("\x1b[57413u"), "+"); + assert.strictEqual(decodeKittyPrintable("\x1b[57415u"), "="); + assert.strictEqual(decodeKittyPrintable("\x1b[57416u"), ","); + assert.strictEqual(decodeKittyPrintable("\x1b[57417u"), undefined); + }); +}); + +describe("decodePrintableKey", () => { + it("should decode printable xterm modifyOtherKeys sequences", () => { + assert.strictEqual(decodePrintableKey("\x1b[27;2;69~"), "E"); + assert.strictEqual(decodePrintableKey("\x1b[27;2;196~"), "Ä"); + assert.strictEqual(decodePrintableKey("\x1b[27;2;32~"), " "); + assert.strictEqual(decodePrintableKey("\x1b[27;2;13~"), undefined); + assert.strictEqual(decodePrintableKey("\x1b[27;6;69~"), undefined); + }); +}); + +describe("parseKey", () => { + describe("Kitty protocol with alternate keys", () => { + it("should return Latin key name when base layout key is present", () => { + setKittyProtocolActive(true); + // Cyrillic ctrl+с with base layout 'c' + const cyrillicCtrlC = "\x1b[1089::99;5u"; + assert.strictEqual(parseKey(cyrillicCtrlC), "ctrl+c"); + setKittyProtocolActive(false); + }); + + it("should prefer codepoint for Latin letters when base layout differs", () => { + setKittyProtocolActive(true); + // Dvorak Ctrl+K reports codepoint 'k' (107) and base layout 'v' (118) + const dvorakCtrlK = "\x1b[107::118;5u"; + assert.strictEqual(parseKey(dvorakCtrlK), "ctrl+k"); + setKittyProtocolActive(false); + }); + + it("should prefer codepoint for symbol keys when base layout differs", () => { + setKittyProtocolActive(true); + // Dvorak Ctrl+/ reports codepoint '/' (47) and base layout '[' (91) + const dvorakCtrlSlash = "\x1b[47::91;5u"; + assert.strictEqual(parseKey(dvorakCtrlSlash), "ctrl+/"); + setKittyProtocolActive(false); + }); + + it("should return key name from codepoint when no base layout", () => { + setKittyProtocolActive(true); + const latinCtrlC = "\x1b[99;5u"; + assert.strictEqual(parseKey(latinCtrlC), "ctrl+c"); + setKittyProtocolActive(false); + }); + + it("should parse shifted uppercase CSI-u letters as shift+letter", () => { + setKittyProtocolActive(true); + assert.strictEqual(matchesKey("\x1b[69;2u", "shift+e"), true); + assert.strictEqual(parseKey("\x1b[69;2u"), "shift+e"); + setKittyProtocolActive(false); + }); + + it("should ignore Kitty CSI-u with unsupported modifiers", () => { + setKittyProtocolActive(true); + assert.strictEqual(parseKey("\x1b[99;17u"), undefined); + setKittyProtocolActive(false); + }); + }); + + describe("Legacy key parsing", () => { + it("should parse legacy Ctrl+letter", () => { + setKittyProtocolActive(false); + assert.strictEqual(parseKey("\x03"), "ctrl+c"); + assert.strictEqual(parseKey("\x04"), "ctrl+d"); + }); + + it("should parse special keys", () => { + assert.strictEqual(parseKey("\x1b"), "escape"); + assert.strictEqual(parseKey("\t"), "tab"); + assert.strictEqual(parseKey("\r"), "enter"); + assert.strictEqual(parseKey("\n"), "enter"); + assert.strictEqual(parseKey("\x00"), "ctrl+space"); + assert.strictEqual(parseKey(" "), "space"); + assert.strictEqual(parseKey("1"), "1"); + assert.strictEqual(matchesKey("1", "1"), true); + }); + + it("should parse arrow keys", () => { + assert.strictEqual(parseKey("\x1b[A"), "up"); + assert.strictEqual(parseKey("\x1b[B"), "down"); + assert.strictEqual(parseKey("\x1b[C"), "right"); + assert.strictEqual(parseKey("\x1b[D"), "left"); + }); + + it("should parse SS3 arrows and home/end", () => { + assert.strictEqual(parseKey("\x1bOA"), "up"); + assert.strictEqual(parseKey("\x1bOB"), "down"); + assert.strictEqual(parseKey("\x1bOC"), "right"); + assert.strictEqual(parseKey("\x1bOD"), "left"); + assert.strictEqual(parseKey("\x1bOH"), "home"); + assert.strictEqual(parseKey("\x1bOF"), "end"); + }); + + it("should parse legacy function and modifier sequences", () => { + assert.strictEqual(parseKey("\x1bOP"), "f1"); + assert.strictEqual(parseKey("\x1b[24~"), "f12"); + assert.strictEqual(parseKey("\x1b[E"), "clear"); + assert.strictEqual(parseKey("\x1b[2^"), "ctrl+insert"); + assert.strictEqual(parseKey("\x1bp"), "alt+up"); + }); + + it("should parse double bracket pageUp", () => { + assert.strictEqual(parseKey("\x1b[[5~"), "pageUp"); + }); + }); +}); diff --git a/packages/tui/test/latex.test.ts b/packages/tui/test/latex.test.ts new file mode 100644 index 0000000000000000000000000000000000000000..6df4260cfcb39b60b73bca288205a86393f9217f --- /dev/null +++ b/packages/tui/test/latex.test.ts @@ -0,0 +1,505 @@ +import assert from "node:assert"; +import { describe, it } from "node:test"; +import { renderLatex } from "../src/index.ts"; + +type LatexCase = readonly [source: string, expected: string]; + +function defineCases(cases: readonly LatexCase[]): void { + for (const [source, expected] of cases) { + it(`renders ${JSON.stringify(source)}`, () => { + assert.strictEqual(renderLatex(source), expected); + }); + } +} + +describe("renderLatex", () => { + describe("Jacobian conjecture session using dollar delimiters", () => { + defineCases([ + [String.raw`\mathbb{C}^3 \to \mathbb{C}^3`, "ℂ³ → ℂ³"], + [ + String.raw`\{3x+2y,\; 27x^2-4z-1,\; x(x-1)(x+1)\} \quad\Rightarrow\quad x \in \{0, \pm 1\},`, + "{3x+2y, 27x²-4z-1, x(x-1)(x+1)} ⇒ x ∈ {0, ± 1},", + ], + [String.raw`F_1 = -\frac{1}{4x^2}.`, "F₁ = -1/(4x²)."], + ["-2", "-2"], + ["(0,0,-1/4)", "(0,0,-1/4)"], + ["(1,-3/2,13/2)", "(1,-3/2,13/2)"], + ["(1,1,1)", "(1,1,1)"], + ["(2,1,0)", "(2,1,0)"], + ["(-1/4, 0, 0)", "(-1/4, 0, 0)"], + [String.raw`\{(0,0,-1/4), (1,-3/2,13/2), (-1,3/2,13/2)\}`, "{(0,0,-1/4), (1,-3/2,13/2), (-1,3/2,13/2)}"], + ["(2,1,1)", "(2,1,1)"], + ["(7/3,-2/5,11/7)", "(7/3,-2/5,11/7)"], + [String.raw`\{y - p(x),\; q(x)\}`, "{y - p(x), q(x)}"], + [String.raw`\deg q = 3`, "deg q = 3"], + [String.raw`[\mathbb{C}(x,y,z):\mathbb{C}(F_1,F_2,F_3)] = 3`, "[ℂ(x,y,z):ℂ(F₁,F₂,F₃)] = 3"], + ["u = 1+xy", "u = 1+xy"], + ["G = u^2 z + y^2(4+3xy)", "G = u² z + y²(4+3xy)"], + ["F_1 = uG", "F₁ = uG"], + ["F_2 = y + 3xG", "F₂ = y + 3xG"], + ["x=0", "x = 0"], + ["F_2 = F_3 = 0", "F₂ = F₃ = 0"], + ["xy = -3/2", "xy = -3/2"], + ["x^2 z = 13/2", "x² z = 13/2"], + [String.raw`\mathbb{C}^*`, "ℂ^*"], + [String.raw`s \mapsto (s,\, -\tfrac{3}{2s},\, \tfrac{13}{2s^2})`, "s ↦ (s, -3/(2s), 13/(2s²))"], + ["X", "X"], + [String.raw`p_\pm`, "p_±"], + ["F(-x,-y,z) = (F_1, -F_2, -F_3)", "F(-x,-y,z) = (F₁, -F₂, -F₃)"], + ["p_0", "p₀"], + [String.raw`s \to \infty`, "s → ∞"], + ["(0,0,0)", "(0,0,0)"], + [String.raw`\Rightarrow`, "⇒"], + [String.raw`\ge 2`, "≥ 2"], + [String.raw`\ge 3`, "≥ 3"], + ["1", "1"], + [String.raw`\mathrm{diag}(-1/2,1,1)`, "diag(-1/2,1,1)"], + ["4+3xy", "4+3xy"], + ]); + }); + + describe("satellite calculation session using bracket delimiters", () => { + defineCases([ + [ + String.raw`E \approx \frac{0.1\ \text{lux}}{100\ \text{lm/W}} = 0.001\ \text{W/m}^2`, + "E ≈ (0.1 lux)/(100 lm/W) = 0.001 W/m²", + ], + [String.raw`\boxed{1\ \text{milliwatt per square metre}}`, "[1 milliwatt per square metre]"], + [String.raw`5\ \text{km}^2 = 5{,}000{,}000\ \text{m}^2`, "5 km² = 5,000,000 m²"], + [ + String.raw`P_{\text{light}} = 0.001 \times 5{,}000{,}000 += \boxed{5{,}000\ \text{W}}`, + "P_light = 0.001 × 5,000,000 = [5,000 W]", + ], + [ + String.raw`P_{\text{electric}} = 5\ \text{kW} \times 0.2 += \boxed{1\ \text{kW}}`, + "P_electric = 5 kW × 0.2 = [1 kW]", + ], + [String.raw`\pi(2.5\ \text{km})^2 = 19.6\ \text{km}^2`, "π(2.5 km)² = 19.6 km²"], + [ + String.raw`0.001\ \text{W/m}^2 \times 19.6 \times 10^6\ \text{m}^2 +\approx \boxed{20\ \text{kW optical}}`, + "0.001 W/m² × 19.6 × 10⁶ m² ≈ [20 kW optical]", + ], + [ + String.raw`1\ \text{kW} \times \frac{1}{3600}\ \text{hour} += \boxed{0.28\ \text{Wh}}`, + "1 kW × 1/3600 hour = [0.28 Wh]", + ], + ]); + }); + + describe("Jacobian conjecture sessions using parenthesis and bracket delimiters", () => { + defineCases([ + [ + String.raw`\det\!\left(\frac{\partial(F_1,F_2,F_3)}{\partial(x,y,z)}\right)=-2.`, + "det((∂(F₁,F₂,F₃))/(∂(x,y,z))) = -2.", + ], + [ + String.raw`\begin{aligned} +F(0,0,-\tfrac14)&=(-\tfrac14,0,0),\\ +F(1,-\tfrac32,\tfrac{13}2)&=(-\tfrac14,0,0),\\ +F(-1,\tfrac32,\tfrac{13}2)&=(-\tfrac14,0,0). +\end{aligned}`, + "F(0,0,-1/4) = (-1/4,0,0),\nF(1,-3/2,13/2) = (-1/4,0,0),\nF(-1,3/2,13/2) = (-1/4,0,0).", + ], + ["F=(F_1,F_2,F_3)", "F = (F₁,F₂,F₃)"], + ["F", "F"], + ["3", "3"], + ]); + }); + + describe("Jacobian matrix session using dollar delimiters", () => { + defineCases([ + [ + String.raw`J = \begin{pmatrix} +\frac{\partial f_1}{\partial x} & \frac{\partial f_1}{\partial y} & \frac{\partial f_1}{\partial z} \\ +\frac{\partial f_2}{\partial x} & \frac{\partial f_2}{\partial y} & \frac{\partial f_2}{\partial z} \\ +\frac{\partial f_3}{\partial x} & \frac{\partial f_3}{\partial y} & \frac{\partial f_3}{\partial z} +\end{pmatrix}`, + "J = ⎛ (∂ f₁)/(∂ x) │ (∂ f₁)/(∂ y) │ (∂ f₁)/(∂ z) ⎞\n ⎜ (∂ f₂)/(∂ x) │ (∂ f₂)/(∂ y) │ (∂ f₂)/(∂ z) ⎟\n ⎝ (∂ f₃)/(∂ x) │ (∂ f₃)/(∂ y) │ (∂ f₃)/(∂ z) ⎠", + ], + [ + String.raw`\begin{aligned} +f_1 &= (1+xy)^3 z + y^2(1+xy)(4+3xy) \\ +f_2 &= y + 3x(1+xy)^2 z + 3xy^2(4+3xy) \\ +f_3 &= 2x - 3x^2y - x^3z +\end{aligned}`, + "f₁ = (1+xy)³ z + y²(1+xy)(4+3xy)\nf₂ = y + 3x(1+xy)² z + 3xy²(4+3xy)\nf₃ = 2x - 3x²y - x³z", + ], + ["x, y, z", "x, y, z"], + ["(x, y, z)", "(x, y, z)"], + [String.raw`(0,\; 0,\; -\tfrac14)`, "(0, 0, -1/4)"], + [String.raw`(-\tfrac14,\; 0,\; 0)`, "(-1/4, 0, 0)"], + [String.raw`(1,\; -\tfrac32,\; \tfrac{13}{2})`, "(1, -3/2, 13/2)"], + [String.raw`(-1,\; \tfrac32,\; \tfrac{13}{2})`, "(-1, 3/2, 13/2)"], + [String.raw`(-\frac14, 0, 0)`, "(-1/4, 0, 0)"], + [String.raw`F: \mathbb{C}^3 \to \mathbb{C}^3`, "F: ℂ³ → ℂ³"], + [ + String.raw`F(0,0,-\tfrac14) = F(1,-\tfrac32,\tfrac{13}{2}) = F(-1,\tfrac32,\tfrac{13}{2}) = (-\tfrac14, 0, 0)`, + "F(0,0,-1/4) = F(1,-3/2,13/2) = F(-1,3/2,13/2) = (-1/4, 0, 0)", + ], + [String.raw`\mathbb{C}^3`, "ℂ³"], + [ + String.raw`\begin{aligned} +f_1 &= \frac{f_1^{\text{ut}}(u,t)}{x^2}, \quad +f_2 = \frac{f_2^{\text{ut}}(u,t)}{x}, \quad +f_3 = x\,(2 - 3u - t) +\end{aligned}`, + "f₁ = (f₁ᵘᵗ(u,t))/(x²), f₂ = (f₂ᵘᵗ(u,t))/x, f₃ = x (2 - 3u - t)", + ], + [String.raw`\det J_F`, "det J_F"], + [String.raw`(-\tfrac14, 0, 0)`, "(-1/4, 0, 0)"], + ["u = xy", "u = xy"], + ["t = x^2z", "t = x²z"], + [String.raw`x \neq 0`, "x ≠ 0"], + [String.raw`f_1^{\text{ut}}, f_2^{\text{ut}}`, "f₁ᵘᵗ, f₂ᵘᵗ"], + ["u,t", "u,t"], + ["x", "x"], + ["x, x^2", "x, x²"], + [String.raw`\mathbb{C}^n \to \mathbb{C}^n`, "ℂⁿ → ℂⁿ"], + [String.raw`n \geq 2`, "n ≥ 2"], + [String.raw`\mathbb{P}^3`, "ℙ³"], + ]); + }); + + describe("extended formulas from a renderer stress-test session", () => { + defineCases([ + [String.raw`e^{i\pi}+1=0`, "e^(iπ)+1 = 0"], + [ + String.raw`\boxed{ +\mathcal{Z}(\beta) += +\int_{\mathcal M} +\exp\!\left( +-\beta\left[ +\frac12 g^{ij}(x)\,\partial_i\phi\,\partial_j\phi ++V(\phi) +\right]\right) +\mathcal D\phi +}`, + "[Z(β) = ∫_M exp( -β[ 1/2 gⁱʲ(x) ∂ᵢϕ ∂ⱼϕ +V(ϕ) ]) Dϕ]", + ], + [ + String.raw`\begin{aligned} +\nabla_\mu T^{\mu\nu} +&= +\frac{1}{\sqrt{-g}} +\partial_\mu\!\left(\sqrt{-g}\,T^{\mu\nu}\right) ++\Gamma^\nu_{\mu\lambda}T^{\mu\lambda} +=0, \\[4pt] +R_{\mu\nu}-\frac12 Rg_{\mu\nu}+\Lambda g_{\mu\nu} +&= +\frac{8\pi G}{c^4}T_{\mu\nu}. +\end{aligned}`, + "∇_μ T^(μν) = 1/(√(-g)) ∂_μ(√(-g) T^(μν)) +Γ^ν_(μλ)T^(μλ) = 0,\nR_(μν)-1/2 Rg_(μν)+Λ g_(μν) = (8π G)/(c⁴)T_(μν).", + ], + [ + String.raw`f(z) += +\frac{1}{2\pi i} +\oint_{\gamma} +\frac{f(\zeta)}{\zeta-z}\,d\zeta, +\qquad +\det\!\begin{pmatrix} +\lambda-a & -b & 0\\ +-c & \lambda-d & -e\\ +0 & -f & \lambda-g +\end{pmatrix} +=0.`, + [ + "f(z) = 1/(2π i) ∮_γ (f(ζ))/(ζ-z) dζ, det⎛ λ-a │ -b │ 0 ⎞ = 0.", + `${" ".repeat(40)}⎜ -c │ λ-d │ -e ⎟`, + `${" ".repeat(40)}⎝ 0 │ -f │ λ-g ⎠`, + ].join("\n"), + ], + [ + String.raw`\Psi(x,t)= +\sum_{n=1}^{\infty} +\underbrace{ +c_n +\sqrt{\frac{2}{L}} +\sin\!\left(\frac{n\pi x}{L}\right) +}_{\text{spatial eigenmode}} +\exp\!\left(-\frac{i\hbar n^2\pi^2}{2mL^2}t\right), +\qquad +|\Psi(x,t)|^2 += +\begin{cases} +\Psi^\ast\Psi, & 0 { + assert.strictEqual( + renderLatex(String.raw`\sum_{i=0}^n \alpha_i + \int_0^\infty e^{-x^2}\,dx = \sqrt{\pi}`), + "∑ᵢ₌₀ⁿ αᵢ + ∫₀^∞ e^(-x²) dx = √π", + ); + }); + + it("renders common accents and binomial notation", () => { + assert.strictEqual( + renderLatex(String.raw`\binom{n}{k}+\vec{x}+\hat{y}+\overline{AB}`), + "(n choose k)+x⃗+ŷ+overline(AB)", + ); + }); + + it("renders extended symbols and negated relations", () => { + assert.strictEqual( + renderLatex(String.raw`\epsilon+\varepsilon+\varsigma+\varkappa+\oplus+\otimes+\therefore+\because`), + "ϵ+ε+ς+ϰ+⊕+⊗+∴+∵", + ); + assert.strictEqual(renderLatex(String.raw`A\not\subseteq B,\quad x\not\in X`), "A ⊈ B, x ∉ X"); + }); + + it("renders relational algebra join operators", () => { + assert.strictEqual(renderLatex(String.raw`R\bowtie S,\quad R\Join S`), "R ⋈ S, R ⋈ S"); + assert.strictEqual(renderLatex(String.raw`R\ltimes S,\quad R\rtimes S`), "R ⋉ S, R ⋊ S"); + assert.strictEqual( + renderLatex(String.raw`R\leftouterjoin S,\quad R\rightouterjoin S,\quad R\fullouterjoin S`), + "R ⟕ S, R ⟖ S, R ⟗ S", + ); + }); + + it("renders delimiter commands and invisible delimiters", () => { + assert.strictEqual( + renderLatex(String.raw`\lvert{x}\rvert+\lVert{v}\rVert+\left.\frac{dy}{dx}\right|_{x=0}`), + "|x|+‖v‖+dy/(dx)|ₓ₌₀", + ); + assert.strictEqual(renderLatex(String.raw`\left\lbrace x \middle| x>0 \right\rbrace`), "{ x | x > 0 }"); + }); + + it("renders named, modular, overlaid, and underlaid operators", () => { + assert.strictEqual(renderLatex(String.raw`\operatorname*{arg\,max}_{x\in X} f(x)`), "arg max[x∈X] f(x)"); + assert.strictEqual(renderLatex(String.raw`a\bmod n,\quad a\equiv b\pmod n`), "a mod n, a ≡ b (mod n)"); + assert.strictEqual(renderLatex(String.raw`\overset{!}{=}+\underset{n}{x}+\stackrel{def}{=}`), "=^!+xₙ+=ᵈᵉᶠ"); + }); + + it("renders indexed roots and additional accents and wrappers", () => { + assert.strictEqual( + renderLatex(String.raw`\sqrt[2]{x}+\sqrt[3]{x}+\sqrt[4]{x}+\sqrt[n]{x}+\sqrt[k]{x+1}`), + "√x+∛x+∜x+ⁿ√x+ᵏ√(x+1)", + ); + assert.strictEqual( + renderLatex(String.raw`\acute{x}+\grave{y}+\widehat{xyz}+\overrightarrow{AB}`), + "x́+ỳ+widehat(xyz)+overrightarrow(AB)", + ); + assert.strictEqual(renderLatex(String.raw`\textnormal{hello}+\mbox{world}+\boldsymbol{x}`), "hello+world+x"); + }); + + it("renders additional display environments", () => { + assert.strictEqual( + renderLatex(String.raw`\begin{equation}\begin{split}a&=b\\&=c\end{split}\end{equation}`), + "a = b\n= c", + ); + assert.strictEqual( + renderLatex(String.raw`\begin{alignedat}{2}a&=b&\quad c&=d\\e&=f&g&=h\end{alignedat}`), + "a = b c = d\ne = f g = h", + ); + }); + + it("uses natural case conditions and aligns matrix columns", () => { + assert.strictEqual( + renderLatex(String.raw`\begin{cases}a & x<0 \\ b & \text{if }x=0 \\ c & \text{otherwise}\end{cases}`), + "⎧ a if x < 0\n⎨ b if x = 0\n⎩ c otherwise", + ); + assert.strictEqual( + renderLatex(String.raw`\begin{pmatrix}1&200\\3000&4\end{pmatrix}`), + "⎛ 1 │ 200 ⎞\n⎝ 3000 │ 4 ⎠", + ); + }); + + it("composes matrices with fractions and adjacent matrices", () => { + assert.strictEqual( + renderLatex( + String.raw`R\left(\frac{\pi}{4}\right) += +\begin{pmatrix} +\frac{\sqrt{2}}{2} & -\frac{\sqrt{2}}{2}\\ +\frac{\sqrt{2}}{2} & \frac{\sqrt{2}}{2} +\end{pmatrix}.`, + { display: true }, + ), + " π\nR( ─ ) = ⎛ (√2)/2 │ -(√2)/2 ⎞\n 4 ⎝ (√2)/2 │ (√2)/2 ⎠.", + ); + assert.strictEqual( + renderLatex( + String.raw`\mathbf w += +R\left(\frac{\pi}{4}\right) +\begin{pmatrix}1\\0\end{pmatrix} += +\begin{pmatrix}\frac{\sqrt{2}}{2}\\\frac{\sqrt{2}}{2}\end{pmatrix}.`, + { display: true }, + ), + " π\nw = R( ─ ) ⎛ 1 ⎞ = ⎛ (√2)/2 ⎞\n 4 ⎝ 0 ⎠ ⎝ (√2)/2 ⎠.", + ); + assert.strictEqual( + renderLatex( + String.raw`A\mathbf e_1=\begin{pmatrix}\pi\\0\end{pmatrix},\qquad A\mathbf e_2=\begin{pmatrix}0\\\frac{1}{\pi}\end{pmatrix}.`, + { display: true }, + ), + "Ae₁ = ⎛ π ⎞, Ae₂ = ⎛ 0 ⎞\n ⎝ 0 ⎠ ⎝ 1/π ⎠.", + ); + assert.strictEqual( + renderLatex(String.raw`\sum_{i=0}^n x_i=\begin{pmatrix}a&b\\c&d\end{pmatrix}.`, { display: true }), + " n\n ∑ xᵢ = ⎛ a │ b ⎞\ni=0 ⎝ c │ d ⎠.", + ); + }); + + it("normalizes relation, multiplication, and named-operator spacing", () => { + for (const source of ["x=y", "x =y", "x=\ny", "x\n=\ny"]) { + assert.strictEqual(renderLatex(source), "x = y"); + } + assert.strictEqual(renderLatex("x_{i=0}"), "xᵢ₌₀"); + assert.strictEqual(renderLatex(String.raw`x\neq0`), "x ≠ 0"); + assert.strictEqual(renderLatex(String.raw`A\to B`), "A → B"); + assert.strictEqual(renderLatex(String.raw`\pi\cdot\frac{1}{\pi}`), "π · 1/π"); + assert.strictEqual(renderLatex(String.raw`\sin\theta`), "sin θ"); + assert.strictEqual(renderLatex(String.raw`\sin^2 x`), "sin² x"); + assert.strictEqual(renderLatex(String.raw`-\sin\theta`), "-sin θ"); + assert.strictEqual(renderLatex(String.raw`i\sin\theta`), "i sin θ"); + assert.strictEqual(renderLatex(String.raw`\det(A)`), "det(A)"); + }); + + it("treats a backslash followed by a line ending as control space", () => { + const source = String.raw`\boxed{ +(1,1,1),\ (1,1,2),\ (1,2,5),\ (1,5,13),\ (2,5,29),\ +(1,13,34),\ (1,34,89) +}.`; + assert.strictEqual( + renderLatex(source, { display: true }), + "[(1,1,1), (1,1,2), (1,2,5), (1,5,13), (2,5,29), (1,13,34), (1,34,89)].", + ); + assert.strictEqual(renderLatex("a\\\r\nb"), "a b"); + }); + + it("stacks operator limits in display mode", () => { + assert.strictEqual(renderLatex(String.raw`\sum_{i=0}^n x_i`, { display: true }), " n\n ∑ xᵢ\ni=0"); + assert.strictEqual(renderLatex(String.raw`\min_{x\in X} f(x)`, { display: true }), "min f(x)\nx∈X"); + assert.strictEqual( + renderLatex(String.raw`\operatorname*{arg\,max}_{x\in X} f(x)`, { + display: true, + }), + "arg max f(x)\n x∈X", + ); + assert.strictEqual(renderLatex(String.raw`\int\nolimits_0^1 f(x)\,dx`, { display: true }), "∫₀¹ f(x) dx"); + assert.strictEqual(renderLatex(String.raw`\int\limits_0^1 f(x)\,dx`, { display: true }), "1\n∫ f(x) dx\n0"); + }); + + it("uses the middle brace for intermediate case rows", () => { + assert.strictEqual( + renderLatex(String.raw`\begin{cases}a & x<0 \\ b & x=0 \\ c & x>0\end{cases}`), + "⎧ a if x < 0\n⎨ b if x = 0\n⎩ c if x > 0", + ); + }); + + it("stacks fractions in display mode", () => { + assert.strictEqual( + renderLatex(String.raw`x=\frac{-b\pm\sqrt{b^2-4ac}}{2a}`, { + display: true, + }), + " -b±√(b²-4ac)\nx = ────────────\n 2a", + ); + assert.strictEqual(renderLatex(String.raw`\frac{x^2+1}{x-1}`, { display: true }), "x²+1\n────\nx-1"); + assert.strictEqual(renderLatex("\\frac{1}\n{2}", { display: true }), "1\n─\n2"); + }); + + it("keeps nested display fractions linear", () => { + const cases: Array<[string, string]> = [ + [ + String.raw`\frac{\frac{x^2+1}{x-1}-\frac{2x}{x+1}}{\frac{x}{x^2-1}}`, + "(x²+1)/(x-1)-2x/(x+1)\n─────────────────────\n x/(x²-1)", + ], + [ + String.raw`\lim_{x\to 0}\frac{\frac{\sin x}{x}-1}{\frac{e^x-1}{x}-1}=0`, + " (sin x)/x-1\nlim ─────────── = 0\nx→0 (eˣ-1)/x-1", + ], + [ + String.raw`\frac{1+\frac{1}{1+\frac{1}{x}}}{1-\frac{1}{1-\frac{1}{x}}}`, + "1+1/(1+1/x)\n───────────\n1-1/(1-1/x)", + ], + ]; + for (const [source, expected] of cases) { + assert.strictEqual(renderLatex(source, { display: true }), expected); + } + }); + + it("keeps fractions linear in scripts and text-style fractions", () => { + assert.strictEqual(renderLatex(String.raw`e^{\frac{1}{2}}`, { display: true }), "e^(1/2)"); + assert.strictEqual(renderLatex(String.raw`\tfrac{1}{2}`, { display: true }), "1/2"); + }); + + it("returns undefined for unsupported commands", () => { + assert.strictEqual(renderLatex(String.raw`x + \unknown{y}`), undefined); + }); + + it("returns undefined for malformed groups and environments", () => { + const malformed = [String.raw`\frac{1}{x`, "x}", String.raw`\begin{matrix}1 & 2`, "x\\"]; + for (const source of malformed) { + assert.strictEqual(renderLatex(source), undefined); + } + }); +}); diff --git a/packages/tui/test/layout.test.ts b/packages/tui/test/layout.test.ts new file mode 100644 index 0000000000000000000000000000000000000000..5aa1cd347ce6c68b8f2091bc35bafe1c8e5b921b --- /dev/null +++ b/packages/tui/test/layout.test.ts @@ -0,0 +1,367 @@ +import assert from "node:assert"; +import { describe, it } from "node:test"; +import { HStack } from "../src/components/h-stack.ts"; +import { ScrollView } from "../src/components/scroll-view.ts"; +import { Text } from "../src/components/text.ts"; +import { VStack } from "../src/components/v-stack.ts"; +import { renderLayoutFrame } from "../src/layout.ts"; +import { encodeKitty, registerKittyImageMetadata } from "../src/terminal-image.ts"; +import { stripTerminalSequences } from "../src/utils.ts"; + +function visibleLines(lines: string[]): string[] { + return lines.map((line) => stripTerminalSequences(line).trimEnd()); +} + +describe("viewport layout", () => { + it("allocates vertical grow space deterministically", () => { + const frame = renderLayoutFrame( + new VStack([ + { component: new Text("top", 0, 0), basis: 1, shrink: 0 }, + { component: new Text("body", 0, 0), basis: 0, grow: 1 }, + ]), + 10, + 4, + () => {}, + ); + + assert.deepStrictEqual( + frame.root.children.map((child) => child.rect.height), + [1, 3], + ); + assert.deepStrictEqual(visibleLines(frame.lines), ["top", "body", "", ""]); + }); + + it("does not render fixed-basis scroll content during stack measurement", () => { + let renderCount = 0; + const transcript = new ScrollView({ + render: () => { + renderCount += 1; + return ["one", "two", "three"]; + }, + invalidate: () => {}, + }); + const root = new VStack([ + { component: transcript, basis: 0, grow: 1 }, + { component: new Text("dock", 0, 0), basis: "auto" }, + ]); + renderLayoutFrame(root, 10, 3, () => {}); + assert.strictEqual(renderCount, 1); + }); + + it("paints only clipped rows from very large scroll content", () => { + const lineCount = 1_000_000_000; + const lines: string[] = []; + lines.length = lineCount; + lines[lineCount - 4] = "before"; + lines[lineCount - 3] = "visible 1"; + lines[lineCount - 2] = "visible 2"; + lines[lineCount - 1] = "visible 3"; + const transcript = new ScrollView( + { + render: () => lines, + invalidate: () => {}, + }, + { follow: "end" }, + ); + + const frame = renderLayoutFrame(transcript, 10, 3, () => {}); + assert.deepStrictEqual(visibleLines(frame.lines), ["visible 1", "visible 2", "visible 3"]); + }); + + it("shrinks entries to their minimum sizes", () => { + const frame = renderLayoutFrame( + new VStack([ + { component: new Text("a1\na2\na3", 0, 0), shrink: 1, minSize: 1 }, + { component: new Text("b1\nb2\nb3", 0, 0), shrink: 0 }, + ]), + 10, + 4, + () => {}, + ); + + assert.deepStrictEqual( + frame.root.children.map((child) => child.rect.height), + [1, 3], + ); + assert.deepStrictEqual(visibleLines(frame.lines), ["a1", "b1", "b2", "b3"]); + }); + + it("includes nested minimum sizes in intrinsic stack measurement", () => { + const dock = new VStack([ + new Text("top1\ntop2\ntop3", 0, 0), + { component: new Text("selector", 0, 0), minSize: 3 }, + new Text("below", 0, 0), + { component: new Text("footer", 0, 0), minSize: 1 }, + ]); + const frame = renderLayoutFrame( + new VStack([ + { component: new Text("body", 0, 0), basis: 0, grow: 1, minSize: 1 }, + { component: dock, basis: "auto", minSize: 1 }, + ]), + 10, + 9, + () => {}, + ); + + assert.deepStrictEqual(visibleLines(frame.lines), [ + "body", + "top1", + "top2", + "top3", + "selector", + "", + "", + "below", + "footer", + ]); + }); + + it("omits gaps around invisible entries", () => { + const stack = new VStack( + [new Text("one", 0, 0), { component: new Text("hidden", 0, 0), visible: () => false }, new Text("two", 0, 0)], + { gap: 1 }, + ); + assert.deepStrictEqual( + stack.render(10).map((line) => line.trimEnd()), + ["one", "", "two"], + ); + }); + + it("crops Kitty images at a scroll view's lower boundary", () => { + const imageId = 124; + const imageLine = encodeKitty("AAAA", { columns: 2, rows: 3, imageId, moveCursor: false }); + registerKittyImageMetadata({ imageId, columns: 2, rows: 3, widthPx: 100, heightPx: 100 }); + const transcript = new ScrollView({ + render: () => ["one", "two", imageLine, "", ""], + invalidate: () => {}, + }); + const frame = renderLayoutFrame( + new VStack([{ component: transcript, basis: 0, grow: 1 }, new Text("dock", 0, 0)]), + 20, + 4, + () => {}, + ); + + assert.ok(frame.lines[2]?.includes("y=0,h=34,r=1")); + }); + + it("composes horizontal children at allocated widths", () => { + const frame = renderLayoutFrame( + new HStack([ + { component: new Text("left", 0, 0), basis: 6, shrink: 0 }, + { component: new Text("right", 0, 0), basis: 6, shrink: 0 }, + ]), + 12, + 1, + () => {}, + ); + assert.deepStrictEqual(visibleLines(frame.lines), ["left right"]); + }); + + it("does not paint zero-width horizontal children", () => { + const frame = renderLayoutFrame( + new HStack([ + { component: new Text("hidden", 0, 0), basis: 0, shrink: 0 }, + { component: new Text("shown", 0, 0), basis: 0, grow: 1 }, + ]), + 5, + 1, + () => {}, + ); + assert.deepStrictEqual(visibleLines(frame.lines), ["shown"]); + }); + + it("tracks follow-end state and returns unused scroll delta", () => { + const scrollView = new ScrollView(new Text("1\n2\n3\n4\n5\n6", 0, 0), { + follow: "end", + primary: true, + }); + renderLayoutFrame(scrollView, 10, 3, () => {}); + assert.strictEqual(scrollView.scrollTop, 3); + assert.strictEqual(scrollView.isFollowingEnd, true); + + assert.strictEqual(scrollView.scrollBy(-2), 0); + assert.strictEqual(scrollView.scrollTop, 1); + assert.strictEqual(scrollView.isFollowingEnd, false); + assert.strictEqual(scrollView.scrollBy(-3), -2); + assert.strictEqual(scrollView.scrollTop, 0); + assert.strictEqual(scrollView.scrollBy(10), 7); + assert.strictEqual(scrollView.scrollTop, 3); + assert.strictEqual(scrollView.isFollowingEnd, true); + }); + + it("renders a proportional glyph scrollbar with an expanded active thumb", async () => { + const sourceLines = ["abcd界", "abcde2", "abcde3", "abcde4", "abcde5", "abcde6", "abcde7", "abcde8"]; + const contentBackground = "\x1b[42m"; + const trackColor = "\x1b[38;5;2m"; + const thumbColor = "\x1b[38;5;1m"; + const scrollbarTrackStyle = (text: string) => `${trackColor}${text}\x1b[39m`; + const scrollbarThumbStyle = (text: string) => `${thumbColor}${text}\x1b[39m`; + const content = new Text(sourceLines.join("\n"), 0, 0, (text) => `${contentBackground}${text}\x1b[49m`); + const scrollView = new ScrollView(content, { + scrollbar: "auto", + scrollbarTrackStyle, + scrollbarThumbStyle, + scrollbarHideDelayMs: 10, + }); + const render = () => renderLayoutFrame(scrollView, 6, 4, () => {}).lines; + const visible = (lines: string[]) => lines.map(stripTerminalSequences); + + let lines = render(); + assert.deepStrictEqual(visible(lines), sourceLines.slice(0, 4)); + + scrollView.scrollBy(2); + lines = render(); + assert.deepStrictEqual(visible(lines), ["abcde│", "abcde┃", "abcde┃", "abcde│"]); + assert.deepStrictEqual( + lines.map((line) => line.includes(trackColor)), + [true, false, false, true], + ); + assert.deepStrictEqual( + lines.map((line) => line.includes(thumbColor)), + [false, true, true, false], + ); + + scrollView.setScrollbarActive(true); + lines = render(); + assert.deepStrictEqual(visible(lines), ["abcde│", "abcde█", "abcde█", "abcde│"]); + assert.deepStrictEqual( + lines.map((line) => line.includes(thumbColor)), + [false, true, true, false], + ); + assert.ok(lines[1]!.lastIndexOf(contentBackground) < lines[1]!.lastIndexOf(thumbColor)); + + scrollView.setScrollbarActive(false); + await new Promise((resolve) => setTimeout(resolve, 30)); + lines = render(); + assert.deepStrictEqual(visible(lines), sourceLines.slice(2, 6)); + + scrollView.scrollToEnd(); + lines = render(); + assert.deepStrictEqual(visible(lines), ["abcde│", "abcde│", "abcde┃", "abcde┃"]); + + scrollView.scrollToStart(); + lines = render(); + assert.strictEqual(visible(lines)[0], "abcd ┃"); + + const followedContent = new Text(sourceLines.join("\n"), 0, 0); + const followed = new ScrollView(followedContent, { + follow: "end", + scrollbar: "auto", + scrollbarTrackStyle, + scrollbarThumbStyle, + }); + renderLayoutFrame(followed, 6, 4, () => {}); + assert.strictEqual(followed.scrollTop, 4); + followedContent.setText(`${sourceLines.join("\n")}\nabcde9`); + const growthFrame = renderLayoutFrame(followed, 6, 4, () => {}); + assert.strictEqual(followed.scrollTop, 5); + assert.ok(growthFrame.lines.every((line) => !/[│┃]/.test(stripTerminalSequences(line)))); + + const fittingContent = new Text("1\n2", 0, 0); + const automatic = new ScrollView(fittingContent, { scrollbar: "auto", scrollbarThumbStyle }); + renderLayoutFrame(automatic, 6, 4, () => {}); + automatic.scrollBy(1); + assert.ok( + renderLayoutFrame(automatic, 6, 4, () => {}).lines.every((line) => !/[│┃]/.test(stripTerminalSequences(line))), + ); + + const alwaysFitting = new ScrollView(fittingContent, { scrollbar: "always", scrollbarThumbStyle }); + const alwaysFittingFrame = renderLayoutFrame(alwaysFitting, 6, 4, () => {}); + assert.strictEqual(alwaysFittingFrame.root.children[0]?.rect.width, 5); + assert.ok(visible(alwaysFittingFrame.lines).every((line) => line.endsWith("┃"))); + + const alwaysOverflowing = new ScrollView(content, { + scrollbar: "always", + scrollbarTrackStyle, + scrollbarThumbStyle, + }); + const alwaysOverflowingFrame = renderLayoutFrame(alwaysOverflowing, 6, 4, () => {}); + assert.strictEqual(alwaysOverflowingFrame.root.children[0]?.rect.width, 5); + assert.strictEqual(visible(alwaysOverflowingFrame.lines).filter((line) => line.endsWith("┃")).length, 2); + assert.strictEqual(visible(alwaysOverflowingFrame.lines).filter((line) => line.endsWith("│")).length, 2); + for (const line of alwaysOverflowingFrame.lines) { + const scrollbarStyleIndex = Math.max(line.lastIndexOf(trackColor), line.lastIndexOf(thumbColor)); + const reservedColumnResetIndex = line.lastIndexOf("\x1b[0m\x1b]8;;\x07", scrollbarStyleIndex); + assert.ok(reservedColumnResetIndex > line.lastIndexOf(contentBackground)); + } + + const thumbHeightFor = (contentHeight: number) => { + const sized = new ScrollView(new Text(Array.from({ length: contentHeight }, () => "x").join("\n"), 0, 0), { + scrollbar: "auto", + scrollbarThumbStyle, + }); + renderLayoutFrame(sized, 6, 20, () => {}); + sized.scrollBy(1); + return renderLayoutFrame(sized, 6, 20, () => {}).lines.filter((line) => + stripTerminalSequences(line).endsWith("┃"), + ).length; + }; + assert.strictEqual(thumbHeightFor(21), 19); + assert.strictEqual(thumbHeightFor(40), 10); + assert.strictEqual(thumbHeightFor(100), 4); + assert.strictEqual(thumbHeightFor(400), 2); + }); + + it("preserves only the underlying background beneath overlay scrollbar glyphs", () => { + const background = "\x1b[42m"; + const borderForeground = "\x1b[31m"; + const content = { + render: (width: number) => + Array.from( + { length: 8 }, + () => `${background}${"x".repeat(width - 1)}${borderForeground}│\x1b[39m\x1b[49m`, + ), + invalidate: () => {}, + }; + const scrollView = new ScrollView(content, { + scrollbar: "auto", + scrollbarTrackStyle: (text) => text, + scrollbarThumbStyle: (text) => text, + }); + renderLayoutFrame(scrollView, 6, 4, () => {}); + scrollView.scrollBy(1); + const frame = renderLayoutFrame(scrollView, 6, 4, () => {}); + + assert.deepStrictEqual(frame.lines.map(stripTerminalSequences), ["xxxxx│", "xxxxx┃", "xxxxx┃", "xxxxx│"]); + for (const line of frame.lines) { + assert.ok(line.includes(background)); + assert.ok(!line.includes(borderForeground)); + assert.ok(line.includes(`\x1b[0m\x1b]8;;\x07${background}`)); + } + }); + + it("updates reserved scrollbar layout at runtime", () => { + const scrollView = new ScrollView(new Text("123456", 0, 0), { scrollbar: "always" }); + const render = () => renderLayoutFrame(new HStack([scrollView], { align: "start" }), 6, 2, () => {}); + const always = render(); + assert.deepStrictEqual(visibleLines(always.lines), ["12345┃", "6 ┃"]); + assert.strictEqual(always.root.children[0]?.rect.width, 6); + assert.strictEqual(always.root.children[0]?.children[0]?.rect.width, 5); + + scrollView.setScrollbar("hidden"); + assert.strictEqual(render().root.children[0]?.children[0]?.rect.width, 6); + assert.strictEqual(scrollView.isScrollbarVisible, false); + }); + + it("measures nested scroll content from constrained child geometry", () => { + const inner = new ScrollView(new Text("1\n2\n3\n4\n5\n6", 0, 0)); + const outer = new ScrollView(new VStack([{ component: inner, basis: 2 }, new Text("tail", 0, 0)])); + renderLayoutFrame(outer, 10, 2, () => {}); + + assert.strictEqual(inner.viewportHeight, 2); + assert.strictEqual(outer.scrollBy(10), 9); + assert.strictEqual(outer.scrollTop, 1); + }); + + it("rebuilds geometry after content changes", () => { + const text = new Text("one", 0, 0); + const root = new VStack([text]); + const first = renderLayoutFrame(root, 10, 4, () => {}); + text.setText("one\ntwo\nthree"); + const second = renderLayoutFrame(root, 10, 4, () => {}); + + assert.strictEqual(first.root.children[0]?.lines?.length, 1); + assert.strictEqual(second.root.children[0]?.lines?.length, 3); + }); +}); diff --git a/packages/tui/test/markdown.test.ts b/packages/tui/test/markdown.test.ts new file mode 100644 index 0000000000000000000000000000000000000000..6347aa5d7621a92854c818039d2d26cf516c37c1 --- /dev/null +++ b/packages/tui/test/markdown.test.ts @@ -0,0 +1,1760 @@ +import assert from "node:assert"; +import { afterEach, describe, it } from "node:test"; +import type { Terminal as XtermTerminalType } from "@xterm/headless"; +import { Chalk } from "chalk"; +import { Markdown, type MarkdownTheme } from "../src/components/markdown.ts"; +import { resetCapabilitiesCache, setCapabilities } from "../src/terminal-image.ts"; +import type { Component, TUI } from "../src/tui.ts"; +import { TuiMainScreen } from "../src/tui-main-screen.ts"; +import { defaultMarkdownTheme } from "./test-themes.ts"; +import { VirtualTerminal } from "./virtual-terminal.ts"; + +// Force full color in CI so ANSI assertions are deterministic +const chalk = new Chalk({ level: 3 }); + +function getCell(terminal: VirtualTerminal, row: number, col: number) { + const xterm = (terminal as unknown as { xterm: XtermTerminalType }).xterm; + const buffer = xterm.buffer.active; + const line = buffer.getLine(buffer.viewportY + row); + assert.ok(line, `Missing buffer line at row ${row}`); + const cell = line.getCell(col); + assert.ok(cell, `Missing cell at row ${row} col ${col}`); + return cell; +} + +function stripAnsi(line: string): string { + return line.replace(/\x1b\[[0-9;]*m/g, ""); +} + +describe("Markdown component", () => { + describe("Transforms", () => { + it("caches transformed Markdown by source and available width", () => { + const calls: Array<{ source: string; availableWidth: number }> = []; + const markdown = new Markdown("source", 2, 0, defaultMarkdownTheme, undefined, { + transform: (source, availableWidth) => { + calls.push({ source, availableWidth }); + return `${source} ${availableWidth}`; + }, + }); + + assert.deepStrictEqual( + markdown.render(80).map((line) => stripAnsi(line).trim()), + ["source 76"], + ); + markdown.render(80); + assert.deepStrictEqual( + markdown.render(60).map((line) => stripAnsi(line).trim()), + ["source 56"], + ); + assert.deepStrictEqual(calls, [ + { source: "source", availableWidth: 76 }, + { source: "source", availableWidth: 56 }, + ]); + + markdown.setText("updated"); + assert.deepStrictEqual( + markdown.render(60).map((line) => stripAnsi(line).trim()), + ["updated 56"], + ); + assert.deepStrictEqual(calls.at(-1), { source: "updated", availableWidth: 56 }); + + markdown.invalidate(); + markdown.render(60); + assert.deepStrictEqual(calls.at(-1), { source: "updated", availableWidth: 56 }); + assert.strictEqual(calls.length, 4); + }); + }); + + describe("Lists", () => { + it("should render simple nested list", () => { + const markdown = new Markdown( + `- Item 1 + - Nested 1.1 + - Nested 1.2 +- Item 2`, + 0, + 0, + defaultMarkdownTheme, + ); + + const lines = markdown.render(80); + + // Check that we have content + assert.ok(lines.length > 0); + + // Strip ANSI codes for checking + const plainLines = lines.map((line) => line.replace(/\x1b\[[0-9;]*m/g, "")); + + // Check structure + assert.ok(plainLines.some((line) => line.includes("- Item 1"))); + assert.ok(plainLines.some((line) => line.includes(" - Nested 1.1"))); + assert.ok(plainLines.some((line) => line.includes(" - Nested 1.2"))); + assert.ok(plainLines.some((line) => line.includes("- Item 2"))); + }); + + it("should render deeply nested list", () => { + const markdown = new Markdown( + `- Level 1 + - Level 2 + - Level 3 + - Level 4`, + 0, + 0, + defaultMarkdownTheme, + ); + + const lines = markdown.render(80); + const plainLines = lines.map((line) => line.replace(/\x1b\[[0-9;]*m/g, "")); + + // Check proper indentation + assert.ok(plainLines.some((line) => line.includes("- Level 1"))); + assert.ok(plainLines.some((line) => line.includes(" - Level 2"))); + assert.ok(plainLines.some((line) => line.includes(" - Level 3"))); + assert.ok(plainLines.some((line) => line.includes(" - Level 4"))); + }); + + it("should render ordered nested list", () => { + const markdown = new Markdown( + `1. First + 1. Nested first + 2. Nested second +2. Second`, + 0, + 0, + defaultMarkdownTheme, + ); + + const lines = markdown.render(80); + const plainLines = lines.map((line) => line.replace(/\x1b\[[0-9;]*m/g, "")); + + assert.ok(plainLines.some((line) => line.includes("1. First"))); + assert.ok(plainLines.some((line) => line.includes(" 1. Nested first"))); + assert.ok(plainLines.some((line) => line.includes(" 2. Nested second"))); + assert.ok(plainLines.some((line) => line.includes("2. Second"))); + }); + + it("should normalize ordered list markers by default", () => { + const markdown = new Markdown("1. alpha\n1. beta\n1. gamma", 0, 0, defaultMarkdownTheme); + + const lines = markdown.render(80).map((line) => stripAnsi(line).trimEnd()); + + assert.deepStrictEqual(lines, ["1. alpha", "2. beta", "3. gamma"]); + }); + + it("should preserve source list markers when configured", () => { + const markdown = new Markdown( + " 4. forth\n 3. third\n\n10) ten\n7) seven\n\n+ plus\n* star\n- minus\n+", + 0, + 0, + defaultMarkdownTheme, + undefined, + { + preserveOrderedListMarkers: true, + }, + ); + + const lines = markdown.render(80).map((line) => stripAnsi(line).trimEnd()); + + assert.deepStrictEqual(lines, [ + "4. forth", + "3. third", + "", + "10) ten", + "7) seven", + "", + "+ plus", + "* star", + "- minus", + "+", + ]); + }); + + it("should render mixed ordered and unordered nested lists", () => { + const markdown = new Markdown( + `1. Ordered item + - Unordered nested + - Another nested +2. Second ordered + - More nested`, + 0, + 0, + defaultMarkdownTheme, + ); + + const lines = markdown.render(80); + const plainLines = lines.map((line) => line.replace(/\x1b\[[0-9;]*m/g, "")); + + assert.ok(plainLines.some((line) => line.includes("1. Ordered item"))); + assert.ok(plainLines.some((line) => line.includes(" - Unordered nested"))); + assert.ok(plainLines.some((line) => line.includes("2. Second ordered"))); + }); + + it("should render blank lines between loose list items", () => { + const markdown = new Markdown( + `1. Lorem ipsum dolor sit amet. + + Ut enim ad minim veniam. + +2. Duis aute irure dolor. + + Excepteur sint occaecat cupidatat. + +3. Beep boop`, + 0, + 0, + defaultMarkdownTheme, + ); + + const lines = markdown.render(80).map((line) => stripAnsi(line).trimEnd()); + + assert.deepStrictEqual(lines, [ + "1. Lorem ipsum dolor sit amet.", + "", + " Ut enim ad minim veniam.", + "", + "2. Duis aute irure dolor.", + "", + " Excepteur sint occaecat cupidatat.", + "", + "3. Beep boop", + ]); + }); + + it("should render task list markers", () => { + const markdown = new Markdown("- [ ] beep\n- [x] boop", 0, 0, defaultMarkdownTheme); + + const lines = markdown.render(80).map((line) => stripAnsi(line).trimEnd()); + + assert.deepStrictEqual(lines, ["- [ ] beep", "- [x] boop"]); + }); + + it("should maintain numbering when code blocks are not indented (LLM output)", () => { + // When code blocks aren't indented, marked parses each item as a separate list. + // We use token.start to preserve the original numbering. + const markdown = new Markdown( + `1. First item + +\`\`\`typescript +// code block +\`\`\` + +2. Second item + +\`\`\`typescript +// another code block +\`\`\` + +3. Third item`, + 0, + 0, + defaultMarkdownTheme, + ); + + const lines = markdown.render(80); + const plainLines = lines.map((line) => line.replace(/\x1b\[[0-9;]*m/g, "").trim()); + + // Find all lines that start with a number and period + const numberedLines = plainLines.filter((line) => /^\d+\./.test(line)); + + // Should have 3 numbered items + assert.strictEqual(numberedLines.length, 3, `Expected 3 numbered items, got: ${numberedLines.join(", ")}`); + + // Check the actual numbers + assert.ok(numberedLines[0].startsWith("1."), `First item should be "1.", got: ${numberedLines[0]}`); + assert.ok(numberedLines[1].startsWith("2."), `Second item should be "2.", got: ${numberedLines[1]}`); + assert.ok(numberedLines[2].startsWith("3."), `Third item should be "3.", got: ${numberedLines[2]}`); + }); + + it("should indent wrapped unordered list lines", () => { + const markdown = new Markdown("- alpha beta gamma delta epsilon", 0, 0, defaultMarkdownTheme); + + const lines = markdown.render(20).map((line) => stripAnsi(line).trimEnd()); + + assert.deepStrictEqual(lines, ["- alpha beta gamma", " delta epsilon"]); + }); + + it("should indent wrapped ordered list lines", () => { + const markdown = new Markdown("1. alpha beta gamma delta epsilon", 0, 0, defaultMarkdownTheme); + + const lines = markdown.render(20).map((line) => stripAnsi(line).trimEnd()); + + assert.deepStrictEqual(lines, ["1. alpha beta gamma", " delta epsilon"]); + }); + + it("should indent wrapped ordered list lines with multi-digit markers", () => { + const markdown = new Markdown("10. alpha beta gamma delta epsilon", 0, 0, defaultMarkdownTheme); + + const lines = markdown.render(21).map((line) => stripAnsi(line).trimEnd()); + + assert.deepStrictEqual(lines, ["10. alpha beta gamma", " delta epsilon"]); + }); + + it("should indent wrapped nested list lines", () => { + const markdown = new Markdown(`- parent\n - alpha beta gamma delta epsilon`, 0, 0, defaultMarkdownTheme); + + const lines = markdown.render(24).map((line) => stripAnsi(line).trimEnd()); + + assert.deepStrictEqual(lines, ["- parent", " - alpha beta gamma", " delta epsilon"]); + }); + + it("should indent wrapped nested list lines under ordered parents", () => { + const markdown = new Markdown(`1. parent\n - alpha beta gamma delta epsilon`, 0, 0, defaultMarkdownTheme); + + const lines = markdown.render(24).map((line) => stripAnsi(line).trimEnd()); + + assert.deepStrictEqual(lines, ["1. parent", " - alpha beta gamma", " delta epsilon"]); + }); + + it("should render and wrap blockquotes inside list items", () => { + const markdown = new Markdown("- > alpha beta gamma delta epsilon zeta", 0, 0, defaultMarkdownTheme); + + const lines = markdown.render(24).map((line) => stripAnsi(line).trimEnd()); + + assert.deepStrictEqual(lines, ["- │ alpha beta gamma", " │ delta epsilon zeta"]); + }); + + it("should render and wrap code blocks inside list items", () => { + const markdown = new Markdown( + "- ```ts\n alpha beta gamma delta epsilon zeta\n ```", + 0, + 0, + defaultMarkdownTheme, + ); + + const lines = markdown.render(24).map((line) => stripAnsi(line).trimEnd()); + + assert.deepStrictEqual(lines, ["- ```ts", " alpha beta gamma", " delta epsilon zeta", " ```"]); + }); + }); + + describe("Tables", () => { + it("should render simple table", () => { + const markdown = new Markdown( + `| Name | Age | +| --- | --- | +| Alice | 30 | +| Bob | 25 |`, + 0, + 0, + defaultMarkdownTheme, + ); + + const lines = markdown.render(80); + const plainLines = lines.map((line) => line.replace(/\x1b\[[0-9;]*m/g, "")); + + // Check table structure + assert.ok(plainLines.some((line) => line.includes("Name"))); + assert.ok(plainLines.some((line) => line.includes("Age"))); + assert.ok(plainLines.some((line) => line.includes("Alice"))); + assert.ok(plainLines.some((line) => line.includes("Bob"))); + // Check for table borders + assert.ok(plainLines.some((line) => line.includes("│"))); + assert.ok(plainLines.some((line) => line.includes("─"))); + }); + + it("should render row dividers between data rows", () => { + const markdown = new Markdown( + `| Name | Age | +| --- | --- | +| Alice | 30 | +| Bob | 25 |`, + 0, + 0, + defaultMarkdownTheme, + ); + + const lines = markdown.render(80); + const plainLines = lines.map((line) => line.replace(/\x1b\[[0-9;]*m/g, "")); + const dividerLines = plainLines.filter((line) => line.includes("┼")); + + assert.strictEqual(dividerLines.length, 2, "Expected header + row divider"); + }); + + it("should keep column width at least the longest word", () => { + const longestWord = "superlongword"; + const markdown = new Markdown( + `| Column One | Column Two | +| --- | --- | +| ${longestWord} short | otherword | +| small | tiny |`, + 0, + 0, + defaultMarkdownTheme, + ); + + const lines = markdown.render(32); + const plainLines = lines.map((line) => line.replace(/\x1b\[[0-9;]*m/g, "")); + const dataLine = plainLines.find((line) => line.includes(longestWord)); + assert.ok(dataLine, "Expected data row containing longest word"); + + const segments = dataLine.split("│").slice(1, -1); + const [firstSegment] = segments; + assert.ok(firstSegment, "Expected first column segment"); + const firstColumnWidth = firstSegment.length - 2; + + assert.ok( + firstColumnWidth >= longestWord.length, + `Expected first column width >= ${longestWord.length}, got ${firstColumnWidth}`, + ); + }); + + it("should render table with alignment", () => { + const markdown = new Markdown( + `| Left | Center | Right | +| :--- | :---: | ---: | +| A | B | C | +| Long text | Middle | End |`, + 0, + 0, + defaultMarkdownTheme, + ); + + const lines = markdown.render(80); + const plainLines = lines.map((line) => line.replace(/\x1b\[[0-9;]*m/g, "")); + + // Check headers + assert.ok(plainLines.some((line) => line.includes("Left"))); + assert.ok(plainLines.some((line) => line.includes("Center"))); + assert.ok(plainLines.some((line) => line.includes("Right"))); + // Check content + assert.ok(plainLines.some((line) => line.includes("Long text"))); + }); + + it("should handle tables with varying column widths", () => { + const markdown = new Markdown( + `| Short | Very long column header | +| --- | --- | +| A | This is a much longer cell content | +| B | Short |`, + 0, + 0, + defaultMarkdownTheme, + ); + + const lines = markdown.render(80); + + // Should render without errors + assert.ok(lines.length > 0); + + const plainLines = lines.map((line) => line.replace(/\x1b\[[0-9;]*m/g, "")); + assert.ok(plainLines.some((line) => line.includes("Very long column header"))); + assert.ok(plainLines.some((line) => line.includes("This is a much longer cell content"))); + }); + + it("should wrap table cells when table exceeds available width", () => { + const markdown = new Markdown( + `| Command | Description | Example | +| --- | --- | --- | +| npm install | Install all dependencies | npm install | +| npm run build | Build the project | npm run build |`, + 0, + 0, + defaultMarkdownTheme, + ); + + // Render at narrow width that forces wrapping + const lines = markdown.render(50); + const plainLines = lines.map((line) => line.replace(/\x1b\[[0-9;]*m/g, "").trimEnd()); + + // All lines should fit within width + for (const line of plainLines) { + assert.ok(line.length <= 50, `Line exceeds width 50: "${line}" (length: ${line.length})`); + } + + // Content should still be present (possibly wrapped across lines) + const allText = plainLines.join(" "); + assert.ok(allText.includes("Command"), "Should contain 'Command'"); + assert.ok(allText.includes("Description"), "Should contain 'Description'"); + assert.ok(allText.includes("npm install"), "Should contain 'npm install'"); + assert.ok(allText.includes("Install"), "Should contain 'Install'"); + }); + + it("should not leak wrapped link styles into table borders or plain cells", async () => { + const source = `| Link | Plain | +| --- | --- | +| [**one two three four five six**](https://example.com) | normal text |`; + + try { + for (const hyperlinks of [true, false]) { + setCapabilities({ images: null, trueColor: false, hyperlinks }); + const terminal = new VirtualTerminal(24, 16); + const tui: TUI = new TuiMainScreen(terminal); + tui.addChild(new Markdown(source, 0, 0, defaultMarkdownTheme)); + tui.start(); + + try { + await terminal.waitForRender(); + const viewport = terminal.getViewport(); + const row = viewport.findIndex((line) => line.includes("one") && line.includes("norm")); + assert.notStrictEqual(row, -1, `Missing wrapped table row: ${JSON.stringify(viewport)}`); + const line = viewport[row]; + const linkCol = line.indexOf("one"); + const separatorCol = line.indexOf("│", linkCol); + const plainCol = line.indexOf("norm"); + assert.ok(linkCol >= 0 && separatorCol > linkCol && plainCol > separatorCol); + assert.strictEqual(getCell(terminal, row, linkCol).isFgDefault(), false); + assert.strictEqual(getCell(terminal, row, separatorCol).isFgDefault(), true); + assert.strictEqual(getCell(terminal, row, plainCol).isFgDefault(), true); + assert.notStrictEqual(getCell(terminal, row, linkCol).isBold(), 0); + assert.strictEqual(getCell(terminal, row, separatorCol).isBold(), 0); + assert.strictEqual(getCell(terminal, row, plainCol).isBold(), 0); + + if (!hyperlinks) { + const urlRow = viewport.findIndex((viewportLine) => viewportLine.includes("https")); + assert.notStrictEqual(urlRow, -1, `Missing fallback URL row: ${JSON.stringify(viewport)}`); + const urlLine = viewport[urlRow]; + const urlCol = urlLine.indexOf("https"); + const urlSeparatorCol = urlLine.indexOf("│", urlCol); + const urlBorderCol = urlLine.lastIndexOf("│"); + assert.ok(urlCol >= 0 && urlSeparatorCol > urlCol && urlBorderCol > urlSeparatorCol); + assert.notStrictEqual(getCell(terminal, urlRow, urlCol).isDim(), 0); + assert.strictEqual(getCell(terminal, urlRow, urlSeparatorCol).isDim(), 0); + assert.strictEqual(getCell(terminal, urlRow, urlBorderCol).isDim(), 0); + } + } finally { + tui.stop(); + } + } + } finally { + resetCapabilitiesCache(); + } + }); + + it("should restore the enclosing style after a wrapped table link", async () => { + const quoteColor = 0x123456; + const theme: MarkdownTheme = { + ...defaultMarkdownTheme, + // Use a basic wrapper that does not automatically reopen itself after nested resets. + quote: (text) => `\x1b[38;2;18;52;86m${text}\x1b[39m`, + link: (text) => `\x1b[38;2;129;162;190m${text}\x1b[39m`, + }; + const source = `> | Link | Plain | +> | --- | --- | +> | [one two three four five six](https://example.com) | normal text |`; + + setCapabilities({ images: null, trueColor: true, hyperlinks: true }); + const terminal = new VirtualTerminal(28, 10); + const tui: TUI = new TuiMainScreen(terminal); + tui.addChild(new Markdown(source, 0, 0, theme)); + tui.start(); + + try { + await terminal.waitForRender(); + const viewport = terminal.getViewport(); + const row = viewport.findIndex((line) => line.includes("one") && line.includes("normal")); + assert.notStrictEqual(row, -1, `Missing wrapped blockquote table row: ${JSON.stringify(viewport)}`); + const line = viewport[row]; + const linkCol = line.indexOf("one"); + const separatorCol = line.indexOf("│", linkCol); + const plainCol = line.indexOf("normal"); + assert.ok(linkCol >= 0 && separatorCol > linkCol && plainCol > separatorCol); + + assert.notStrictEqual(getCell(terminal, row, linkCol).getFgColor(), quoteColor); + assert.strictEqual(getCell(terminal, row, separatorCol).getFgColor(), quoteColor); + assert.strictEqual(getCell(terminal, row, plainCol).getFgColor(), quoteColor); + + const finalRow = viewport.findIndex((line) => line.includes("five six")); + assert.notStrictEqual(finalRow, -1, `Missing final wrapped link row: ${JSON.stringify(viewport)}`); + const finalLine = viewport[finalRow]; + const finalLinkCol = finalLine.indexOf("five six"); + const finalSeparatorCol = finalLine.indexOf("│", finalLinkCol); + const finalBorderCol = finalLine.lastIndexOf("│"); + assert.ok(finalLinkCol >= 0 && finalSeparatorCol > finalLinkCol && finalBorderCol > finalSeparatorCol); + assert.strictEqual(getCell(terminal, finalRow, finalSeparatorCol).getFgColor(), quoteColor); + assert.strictEqual(getCell(terminal, finalRow, finalBorderCol).getFgColor(), quoteColor); + } finally { + tui.stop(); + resetCapabilitiesCache(); + } + }); + + it("should wrap long cell content to multiple lines", () => { + const markdown = new Markdown( + `| Header | +| --- | +| This is a very long cell content that should wrap |`, + 0, + 0, + defaultMarkdownTheme, + ); + + // Render at width that forces the cell to wrap + const lines = markdown.render(25); + const plainLines = lines.map((line) => line.replace(/\x1b\[[0-9;]*m/g, "").trimEnd()); + + // Should have multiple data rows due to wrapping + const dataRows = plainLines.filter((line) => line.startsWith("│") && !line.includes("─")); + assert.ok(dataRows.length > 2, `Expected wrapped rows, got ${dataRows.length} rows`); + + // All content should be preserved (may be split across lines) + const allText = plainLines.join(" "); + assert.ok(allText.includes("very long"), "Should preserve 'very long'"); + assert.ok(allText.includes("cell content"), "Should preserve 'cell content'"); + assert.ok(allText.includes("should wrap"), "Should preserve 'should wrap'"); + }); + + it("should wrap long unbroken tokens inside table cells (not only at line start)", () => { + // Pin to no-hyperlinks so width checks work on plain text without OSC 8 sequences. + setCapabilities({ images: null, trueColor: false, hyperlinks: false }); + const url = "https://example.com/this/is/a/very/long/url/that/should/wrap"; + const markdown = new Markdown( + `| Value | +| --- | +| prefix ${url} |`, + 0, + 0, + defaultMarkdownTheme, + ); + + const width = 30; + const lines = markdown.render(width); + resetCapabilitiesCache(); + const plainLines = lines.map((line) => line.replace(/\x1b\[[0-9;]*m/g, "").trimEnd()); + + for (const line of plainLines) { + assert.ok(line.length <= width, `Line exceeds width ${width}: "${line}" (length: ${line.length})`); + } + + // Borders should stay intact (exactly 2 vertical borders for a 1-col table) + const tableLines = plainLines.filter((line) => line.startsWith("│")); + assert.ok(tableLines.length > 0, "Expected table rows to render"); + for (const line of tableLines) { + const borderCount = line.split("│").length - 1; + assert.strictEqual(borderCount, 2, `Expected 2 borders, got ${borderCount}: "${line}"`); + } + + // Strip box drawing characters + whitespace so we can assert the URL is preserved + // even if it was split across multiple wrapped lines. + const extracted = plainLines.join("").replace(/[│├┤─\s]/g, ""); + assert.ok(extracted.includes("prefix"), "Should preserve 'prefix'"); + assert.ok(extracted.includes(url), "Should preserve URL"); + }); + + it("should wrap styled inline code inside table cells without breaking borders", () => { + const markdown = new Markdown( + `| Code | +| --- | +| \`averyveryveryverylongidentifier\` |`, + 0, + 0, + defaultMarkdownTheme, + ); + + const width = 20; + const lines = markdown.render(width); + const joinedOutput = lines.join("\n"); + assert.ok(joinedOutput.includes("\x1b[33m"), "Inline code should be styled (yellow)"); + + const plainLines = lines.map((line) => line.replace(/\x1b\[[0-9;]*m/g, "").trimEnd()); + for (const line of plainLines) { + assert.ok(line.length <= width, `Line exceeds width ${width}: "${line}" (length: ${line.length})`); + } + + const tableLines = plainLines.filter((line) => line.startsWith("│")); + for (const line of tableLines) { + const borderCount = line.split("│").length - 1; + assert.strictEqual(borderCount, 2, `Expected 2 borders, got ${borderCount}: "${line}"`); + } + }); + + it("should handle extremely narrow width gracefully", () => { + const markdown = new Markdown( + `| A | B | C | +| --- | --- | --- | +| 1 | 2 | 3 |`, + 0, + 0, + defaultMarkdownTheme, + ); + + // Very narrow width + const lines = markdown.render(15); + const plainLines = lines.map((line) => line.replace(/\x1b\[[0-9;]*m/g, "").trimEnd()); + + // Should not crash and should produce output + assert.ok(lines.length > 0, "Should produce output"); + + // Lines should not exceed width + for (const line of plainLines) { + assert.ok(line.length <= 15, `Line exceeds width 15: "${line}" (length: ${line.length})`); + } + }); + + it("should render table correctly when it fits naturally", () => { + const markdown = new Markdown( + `| A | B | +| --- | --- | +| 1 | 2 |`, + 0, + 0, + defaultMarkdownTheme, + ); + + // Wide width where table fits naturally + const lines = markdown.render(80); + const plainLines = lines.map((line) => line.replace(/\x1b\[[0-9;]*m/g, "").trimEnd()); + + // Should have proper table structure + const headerLine = plainLines.find((line) => line.includes("A") && line.includes("B")); + assert.ok(headerLine, "Should have header row"); + assert.ok(headerLine?.includes("│"), "Header should have borders"); + + const separatorLine = plainLines.find((line) => line.includes("├") && line.includes("┼")); + assert.ok(separatorLine, "Should have separator row"); + + const dataLine = plainLines.find((line) => line.includes("1") && line.includes("2")); + assert.ok(dataLine, "Should have data row"); + }); + + it("should respect paddingX when calculating table width", () => { + const markdown = new Markdown( + `| Column One | Column Two | +| --- | --- | +| Data 1 | Data 2 |`, + 2, // paddingX = 2 + 0, + defaultMarkdownTheme, + ); + + // Width 40 with paddingX=2 means contentWidth=36 + const lines = markdown.render(40); + const plainLines = lines.map((line) => line.replace(/\x1b\[[0-9;]*m/g, "").trimEnd()); + + // All lines should respect width + for (const line of plainLines) { + assert.ok(line.length <= 40, `Line exceeds width 40: "${line}" (length: ${line.length})`); + } + + // Table rows should have left padding + const tableRow = plainLines.find((line) => line.includes("│")); + assert.ok(tableRow?.startsWith(" "), "Table should have left padding"); + }); + + it("should not add a trailing blank line when table is the last rendered block", () => { + const markdown = new Markdown( + `| Name | +| --- | +| Alice |`, + 0, + 0, + defaultMarkdownTheme, + ); + + const lines = markdown.render(80); + const plainLines = lines.map((line) => line.replace(/\x1b\[[0-9;]*m/g, "").trimEnd()); + + assert.notStrictEqual( + plainLines.at(-1), + "", + `Expected table to end without a blank line: ${JSON.stringify(plainLines)}`, + ); + }); + }); + + describe("Combined features", () => { + it("should render lists and tables together", () => { + const markdown = new Markdown( + `# Test Document + +- Item 1 + - Nested item +- Item 2 + +| Col1 | Col2 | +| --- | --- | +| A | B |`, + 0, + 0, + defaultMarkdownTheme, + ); + + const lines = markdown.render(80); + const plainLines = lines.map((line) => line.replace(/\x1b\[[0-9;]*m/g, "")); + + // Check heading + assert.ok(plainLines.some((line) => line.includes("Test Document"))); + // Check list + assert.ok(plainLines.some((line) => line.includes("- Item 1"))); + assert.ok(plainLines.some((line) => line.includes(" - Nested item"))); + // Check table + assert.ok(plainLines.some((line) => line.includes("Col1"))); + assert.ok(plainLines.some((line) => line.includes("│"))); + }); + }); + + describe("LaTeX math", () => { + it("renders inline dollar and parenthesis delimiters", () => { + const markdown = new Markdown( + String.raw`A map $\mathbb{C}^3 \to \mathbb{C}^3$, $xy$, $x-y$, $-x$, $\frac{1}{2}$, and \(s \to \infty\).`, + 0, + 0, + defaultMarkdownTheme, + ); + + const lines = markdown.render(80).map((line) => stripAnsi(line).trimEnd()); + + assert.deepStrictEqual(lines, ["A map ℂ³ → ℂ³, xy, x-y, -x, 1/2, and s → ∞."]); + }); + + it("renders display dollar delimiters without Markdown escape corruption", () => { + const markdown = new Markdown( + String.raw`Before + +$$\{3x+2y,\; x \in \{0, \pm 1\}\}$$ + +after`, + 0, + 0, + defaultMarkdownTheme, + ); + + const lines = markdown.render(80).map((line) => stripAnsi(line).trimEnd()); + + assert.deepStrictEqual(lines, ["Before", "", "{3x+2y, x ∈ {0, ± 1}}", "", "after"]); + }); + + it("renders display bracket delimiters", () => { + const markdown = new Markdown( + String.raw`Before + +\[ +E \approx \frac{0.1\ \text{lux}}{100\ \text{lm/W}} +\] + +after`, + 0, + 0, + defaultMarkdownTheme, + ); + + const lines = markdown.render(80).map((line) => stripAnsi(line).trimEnd()); + + assert.deepStrictEqual(lines, ["Before", "", " 0.1 lux", "E ≈ ────────", " 100 lm/W", "", "after"]); + }); + + it("aligns matrix rows with the opening delimiter", () => { + const markdown = new Markdown( + String.raw`Consider the matrix + +\[ +A= +\begin{pmatrix} +\pi & 0\\ +0 & \frac{1}{\pi} +\end{pmatrix}. +\]`, + 0, + 0, + defaultMarkdownTheme, + ); + + const lines = markdown.render(80).map((line) => stripAnsi(line).trimEnd()); + + assert.deepStrictEqual(lines, ["Consider the matrix", "", "A = ⎛ π │ 0 ⎞", " ⎝ 0 │ 1/π ⎠."]); + }); + + it("renders lower limits beneath display operators", () => { + const markdown = new Markdown( + String.raw`\[ +\lim_{x\to 0}\frac{\frac{\sin x}{x}-1}{\frac{e^x-1}{x}-1}=0 +\]`, + 0, + 0, + defaultMarkdownTheme, + ); + + const lines = markdown.render(80).map((line) => stripAnsi(line).trimEnd()); + + assert.deepStrictEqual(lines, [" (sin x)/x-1", "lim ─────────── = 0", "x→0 (eˣ-1)/x-1"]); + }); + + it("renders math inside lists and tables", () => { + const markdown = new Markdown( + String.raw`- Formula: $F_1 = u^2$ + +| Value | +| --- | +| $\mathbb{C}^3$ |`, + 0, + 0, + defaultMarkdownTheme, + ); + + const lines = markdown.render(80).map((line) => stripAnsi(line).trimEnd()); + const output = lines.join("\n"); + + assert.ok(output.includes("- Formula: F₁ = u²")); + assert.ok(output.includes("│ ℂ³")); + }); + + it("does not treat currency, shell variables, or code spans as math", () => { + const source = "Costs $5 and $10 or $8k–$12k; use `$x$`, $HOME, and $" + "{PATH}."; + const markdown = new Markdown(source, 0, 0, defaultMarkdownTheme); + + const lines = markdown.render(80).map((line) => stripAnsi(line).trimEnd()); + + assert.deepStrictEqual(lines, ["Costs $5 and $10 or $8k–$12k; use $x$, $HOME, and $" + "{PATH}."]); + + const shellVariables = "Paths: $HOME/$USER and $XDG_CONFIG_HOME/$APP_CONFIG"; + const shellLines = new Markdown(shellVariables, 0, 0, defaultMarkdownTheme) + .render(80) + .map((line) => stripAnsi(line).trimEnd()); + assert.deepStrictEqual(shellLines, [shellVariables]); + }); + + it("preserves unsupported and incomplete LaTeX exactly", () => { + const cases = [String.raw`Unknown $x + \unknown{y}$ after`, String.raw`Streaming $\mathbb{C}^3`]; + + for (const source of cases) { + const markdown = new Markdown(source, 0, 0, defaultMarkdownTheme); + const lines = markdown.render(80).map((line) => stripAnsi(line).trimEnd()); + assert.deepStrictEqual(lines, [source]); + } + }); + + it("preserves incomplete backslash delimiters while streaming", () => { + const inline = new Markdown(String.raw`Map \(\mathbb{C}^3`, 0, 0, defaultMarkdownTheme); + assert.deepStrictEqual( + inline.render(80).map((line) => stripAnsi(line).trimEnd()), + [String.raw`Map \(\mathbb{C}^3`], + ); + + const display = new Markdown("\\[\nx^2", 0, 0, defaultMarkdownTheme); + assert.deepStrictEqual( + display.render(80).map((line) => stripAnsi(line).trimEnd()), + ["\\[", "x^2"], + ); + }); + + it("does not render LaTeX inside escaped delimiters or code fences", () => { + const source = [String.raw`Escaped \$x-y\$.`, "", "```text", String.raw`$\mathbb{C}^3$`, "```"].join("\n"); + const markdown = new Markdown(source, 0, 0, defaultMarkdownTheme); + const lines = markdown.render(80).map((line) => stripAnsi(line).trimEnd()); + + assert.deepStrictEqual(lines, ["Escaped $x-y$.", "", "```text", " $\\mathbb{C}^3$", "```"]); + }); + + it("allows LaTeX rendering to be disabled", () => { + const markdown = new Markdown( + String.raw`Map $\mathbb{C}^3 \to \mathbb{C}^3$`, + 0, + 0, + defaultMarkdownTheme, + undefined, + { + renderLatex: false, + }, + ); + + assert.deepStrictEqual( + markdown.render(80).map((line) => stripAnsi(line).trimEnd()), + [String.raw`Map $\mathbb{C}^3 \to \mathbb{C}^3$`], + ); + }); + + it("switches from raw to rendered math when a streamed delimiter closes", () => { + const markdown = new Markdown(String.raw`Map $\mathbb{C}^3`, 0, 0, defaultMarkdownTheme); + assert.deepStrictEqual( + markdown.render(80).map((line) => stripAnsi(line).trimEnd()), + [String.raw`Map $\mathbb{C}^3`], + ); + + markdown.setText(String.raw`Map $\mathbb{C}^3$`); + + assert.deepStrictEqual( + markdown.render(80).map((line) => stripAnsi(line).trimEnd()), + ["Map ℂ³"], + ); + }); + }); + + describe("Backslash escapes", () => { + it("should normalize escaped punctuation by default", () => { + const markdown = new Markdown(String.raw`"\"`, 0, 0, defaultMarkdownTheme); + + const lines = markdown.render(80).map((line) => stripAnsi(line).trimEnd()); + + assert.deepStrictEqual(lines, [`""`]); + }); + + it("should preserve source backslash escapes when configured", () => { + const markdown = new Markdown(String.raw`"\"`, 0, 0, defaultMarkdownTheme, undefined, { + preserveBackslashEscapes: true, + }); + + const lines = markdown.render(80).map((line) => stripAnsi(line).trimEnd()); + + assert.deepStrictEqual(lines, [String.raw`"\"`]); + }); + }); + + describe("Pre-styled text (thinking traces)", () => { + it("should preserve gray italic styling after inline code", () => { + // This replicates how thinking content is rendered in assistant-message.ts + const markdown = new Markdown( + "This is thinking with `inline code` and more text after", + 1, + 0, + defaultMarkdownTheme, + { + color: (text) => chalk.gray(text), + italic: true, + }, + ); + + const lines = markdown.render(80); + const joinedOutput = lines.join("\n"); + + // Should contain the inline code block + assert.ok(joinedOutput.includes("inline code")); + + // The output should have ANSI codes for gray (90) and italic (3) + assert.ok(joinedOutput.includes("\x1b[90m"), "Should have gray color code"); + assert.ok(joinedOutput.includes("\x1b[3m"), "Should have italic code"); + + // Verify that inline code is styled (theme uses yellow) + const hasCodeColor = joinedOutput.includes("\x1b[33m"); + assert.ok(hasCodeColor, "Should style inline code"); + }); + + it("should preserve gray italic styling after bold text", () => { + const markdown = new Markdown( + "This is thinking with **bold text** and more after", + 1, + 0, + defaultMarkdownTheme, + { + color: (text) => chalk.gray(text), + italic: true, + }, + ); + + const lines = markdown.render(80); + const joinedOutput = lines.join("\n"); + + // Should contain bold text + assert.ok(joinedOutput.includes("bold text")); + + // The output should have ANSI codes for gray (90) and italic (3) + assert.ok(joinedOutput.includes("\x1b[90m"), "Should have gray color code"); + assert.ok(joinedOutput.includes("\x1b[3m"), "Should have italic code"); + + // Should have bold codes (1 or 22 for bold on/off) + assert.ok(joinedOutput.includes("\x1b[1m"), "Should have bold code"); + }); + + it("should not leak styles into following lines when rendered in TUI", async () => { + class MarkdownWithInput implements Component { + public markdownLineCount = 0; + private readonly markdown: Markdown; + + constructor(markdown: Markdown) { + this.markdown = markdown; + } + + render(width: number): string[] { + const lines = this.markdown.render(width); + this.markdownLineCount = lines.length; + return [...lines, "INPUT"]; + } + + invalidate(): void { + this.markdown.invalidate(); + } + } + + const markdown = new Markdown("This is thinking with `inline code`", 1, 0, defaultMarkdownTheme, { + color: (text) => chalk.gray(text), + italic: true, + }); + + const terminal = new VirtualTerminal(80, 6); + const tui: TUI = new TuiMainScreen(terminal); + const component = new MarkdownWithInput(markdown); + tui.addChild(component); + tui.start(); + await terminal.waitForRender(); + + assert.ok(component.markdownLineCount > 0); + const inputRow = component.markdownLineCount; + assert.strictEqual(getCell(terminal, inputRow, 0).isItalic(), 0); + tui.stop(); + }); + }); + + describe("Spacing after code blocks", () => { + it("should have only one blank line between code block and following paragraph", () => { + const markdown = new Markdown( + `hello world + +\`\`\`js +const hello = "world"; +\`\`\` + +again, hello world`, + 0, + 0, + defaultMarkdownTheme, + ); + + const lines = markdown.render(80); + const plainLines = lines.map((line) => line.replace(/\x1b\[[0-9;]*m/g, "").trimEnd()); + + const closingBackticksIndex = plainLines.indexOf("```"); + assert.ok(closingBackticksIndex !== -1, "Should have closing backticks"); + + const afterBackticks = plainLines.slice(closingBackticksIndex + 1); + const emptyLineCount = afterBackticks.findIndex((line) => line !== ""); + + assert.strictEqual( + emptyLineCount, + 1, + `Expected 1 empty line after code block, but found ${emptyLineCount}. Lines after backticks: ${JSON.stringify(afterBackticks.slice(0, 5))}`, + ); + }); + + it("should normalize paragraph and code block spacing to one blank line", () => { + const cases = [ + `hello this is text +\`\`\` +code block +\`\`\` +more text`, + `hello this is text + +\`\`\` +code block +\`\`\` + +more text`, + ]; + const expectedLines = ["hello this is text", "", "```", " code block", "```", "", "more text"]; + + for (const text of cases) { + const markdown = new Markdown(text, 0, 0, defaultMarkdownTheme); + const lines = markdown.render(80); + const plainLines = lines.map((line) => line.replace(/\x1b\[[0-9;]*m/g, "").trimEnd()); + + assert.deepStrictEqual( + plainLines, + expectedLines, + `Unexpected spacing for markdown: ${JSON.stringify(text)}`, + ); + } + }); + + it("should not add a trailing blank line when code block is the last rendered block", () => { + const cases = ["```js\nconst hello = 'world';\n```", "hello world\n\n```js\nconst hello = 'world';\n```"]; + + for (const text of cases) { + const markdown = new Markdown(text, 0, 0, defaultMarkdownTheme); + const lines = markdown.render(80); + const plainLines = lines.map((line) => line.replace(/\x1b\[[0-9;]*m/g, "").trimEnd()); + + assert.notStrictEqual( + plainLines.at(-1), + "", + `Expected code block to end without a blank line: ${JSON.stringify(plainLines)}`, + ); + } + }); + }); + + describe("Spacing after dividers", () => { + it("should have only one blank line between divider and following paragraph", () => { + const markdown = new Markdown( + `hello world + +--- + +again, hello world`, + 0, + 0, + defaultMarkdownTheme, + ); + + const lines = markdown.render(80); + const plainLines = lines.map((line) => line.replace(/\x1b\[[0-9;]*m/g, "").trimEnd()); + + const dividerIndex = plainLines.findIndex((line) => line.includes("─")); + assert.ok(dividerIndex !== -1, "Should have divider"); + + const afterDivider = plainLines.slice(dividerIndex + 1); + const emptyLineCount = afterDivider.findIndex((line) => line !== ""); + + assert.strictEqual( + emptyLineCount, + 1, + `Expected 1 empty line after divider, but found ${emptyLineCount}. Lines after divider: ${JSON.stringify(afterDivider.slice(0, 5))}`, + ); + }); + + it("should not add a trailing blank line when divider is the last rendered block", () => { + const markdown = new Markdown("---", 0, 0, defaultMarkdownTheme); + const lines = markdown.render(80); + const plainLines = lines.map((line) => line.replace(/\x1b\[[0-9;]*m/g, "").trimEnd()); + + assert.notStrictEqual( + plainLines.at(-1), + "", + `Expected divider to end without a blank line: ${JSON.stringify(plainLines)}`, + ); + }); + }); + + describe("Spacing after headings", () => { + it("should have only one blank line between heading and following paragraph", () => { + const markdown = new Markdown( + `# Hello + +This is a paragraph`, + 0, + 0, + defaultMarkdownTheme, + ); + + const lines = markdown.render(80); + const plainLines = lines.map((line) => line.replace(/\x1b\[[0-9;]*m/g, "").trimEnd()); + + const headingIndex = plainLines.findIndex((line) => line.includes("Hello")); + assert.ok(headingIndex !== -1, "Should have heading"); + + const afterHeading = plainLines.slice(headingIndex + 1); + const emptyLineCount = afterHeading.findIndex((line) => line !== ""); + + assert.strictEqual( + emptyLineCount, + 1, + `Expected 1 empty line after heading, but found ${emptyLineCount}. Lines after heading: ${JSON.stringify(afterHeading.slice(0, 5))}`, + ); + }); + + it("should not add a trailing blank line when heading is the last rendered block", () => { + const markdown = new Markdown("# Hello", 0, 0, defaultMarkdownTheme); + const lines = markdown.render(80); + const plainLines = lines.map((line) => line.replace(/\x1b\[[0-9;]*m/g, "").trimEnd()); + + assert.notStrictEqual( + plainLines.at(-1), + "", + `Expected heading to end without a blank line: ${JSON.stringify(plainLines)}`, + ); + }); + }); + + describe("Spacing after blockquotes", () => { + it("should have only one blank line between blockquote and following paragraph", () => { + const markdown = new Markdown( + `hello world + +> This is a quote + +again, hello world`, + 0, + 0, + defaultMarkdownTheme, + ); + + const lines = markdown.render(80); + const plainLines = lines.map((line) => line.replace(/\x1b\[[0-9;]*m/g, "").trimEnd()); + + const quoteIndex = plainLines.findIndex((line) => line.includes("This is a quote")); + assert.ok(quoteIndex !== -1, "Should have blockquote"); + + const afterQuote = plainLines.slice(quoteIndex + 1); + const emptyLineCount = afterQuote.findIndex((line) => line !== ""); + + assert.strictEqual( + emptyLineCount, + 1, + `Expected 1 empty line after blockquote, but found ${emptyLineCount}. Lines after quote: ${JSON.stringify(afterQuote.slice(0, 5))}`, + ); + }); + + it("should not add a trailing blank line when blockquote is the last rendered block", () => { + const markdown = new Markdown("> This is a quote", 0, 0, defaultMarkdownTheme); + const lines = markdown.render(80); + const plainLines = lines.map((line) => line.replace(/\x1b\[[0-9;]*m/g, "").trimEnd()); + + assert.notStrictEqual( + plainLines.at(-1), + "", + `Expected blockquote to end without a blank line: ${JSON.stringify(plainLines)}`, + ); + }); + }); + + describe("Blockquotes with multiline content", () => { + it("should apply consistent styling to all lines in lazy continuation blockquote", () => { + // Markdown "lazy continuation" - second line without > is still part of the quote + const markdown = new Markdown( + `>Foo +bar`, + 0, + 0, + defaultMarkdownTheme, + { + color: (text) => chalk.magenta(text), // This should NOT be applied to blockquotes + }, + ); + + const lines = markdown.render(80); + + // Both lines should have the quote border + const plainLines = lines.map((line) => line.replace(/\x1b\[[0-9;]*m/g, "")); + const quotedLines = plainLines.filter((line) => line.startsWith("│ ")); + assert.strictEqual(quotedLines.length, 2, `Expected 2 quoted lines, got: ${JSON.stringify(plainLines)}`); + + // Both lines should have italic (from theme.quote styling) + const fooLine = lines.find((line) => line.includes("Foo")); + const barLine = lines.find((line) => line.includes("bar")); + assert.ok(fooLine, "Should have Foo line"); + assert.ok(barLine, "Should have bar line"); + + // Check that both have italic (\x1b[3m) - blockquotes use theme styling, not default message color + assert.ok(fooLine?.includes("\x1b[3m"), `Foo line should have italic: ${fooLine}`); + assert.ok(barLine?.includes("\x1b[3m"), `bar line should have italic: ${barLine}`); + + // Blockquotes should NOT have the default message color (magenta) + assert.ok(!fooLine?.includes("\x1b[35m"), `Foo line should NOT have magenta color: ${fooLine}`); + assert.ok(!barLine?.includes("\x1b[35m"), `bar line should NOT have magenta color: ${barLine}`); + }); + + it("should apply consistent styling to explicit multiline blockquote", () => { + const markdown = new Markdown( + `>Foo +>bar`, + 0, + 0, + defaultMarkdownTheme, + { + color: (text) => chalk.cyan(text), // This should NOT be applied to blockquotes + }, + ); + + const lines = markdown.render(80); + + // Both lines should have the quote border + const plainLines = lines.map((line) => line.replace(/\x1b\[[0-9;]*m/g, "")); + const quotedLines = plainLines.filter((line) => line.startsWith("│ ")); + assert.strictEqual(quotedLines.length, 2, `Expected 2 quoted lines, got: ${JSON.stringify(plainLines)}`); + + // Both lines should have italic (from theme.quote styling) + const fooLine = lines.find((line) => line.includes("Foo")); + const barLine = lines.find((line) => line.includes("bar")); + assert.ok(fooLine?.includes("\x1b[3m"), `Foo line should have italic: ${fooLine}`); + assert.ok(barLine?.includes("\x1b[3m"), `bar line should have italic: ${barLine}`); + + // Blockquotes should NOT have the default message color (cyan) + assert.ok(!fooLine?.includes("\x1b[36m"), `Foo line should NOT have cyan color: ${fooLine}`); + assert.ok(!barLine?.includes("\x1b[36m"), `bar line should NOT have cyan color: ${barLine}`); + }); + + it("should render list content inside blockquotes", () => { + const markdown = new Markdown( + `> 1. bla bla +> - nested bullet`, + 0, + 0, + defaultMarkdownTheme, + ); + + const lines = markdown.render(80); + const plainLines = lines.map((line) => line.replace(/\x1b\[[0-9;]*m/g, "")); + const quotedLines = plainLines.filter((line) => line.startsWith("│ ")); + + assert.ok( + quotedLines.some((line) => line.includes("1. bla bla")), + `Missing ordered list item: ${JSON.stringify(quotedLines)}`, + ); + assert.ok( + quotedLines.some((line) => line.includes("- nested bullet")), + `Missing unordered list item: ${JSON.stringify(quotedLines)}`, + ); + }); + + it("should wrap long blockquote lines and add border to each wrapped line", () => { + const longText = "This is a very long blockquote line that should wrap to multiple lines when rendered"; + const markdown = new Markdown(`> ${longText}`, 0, 0, defaultMarkdownTheme); + + // Render at narrow width to force wrapping + const lines = markdown.render(30); + const plainLines = lines.map((line) => line.replace(/\x1b\[[0-9;]*m/g, "").trimEnd()); + + // Filter to non-empty lines (exclude trailing blank line after blockquote) + const contentLines = plainLines.filter((line) => line.length > 0); + + // Should have multiple lines due to wrapping + assert.ok(contentLines.length > 1, `Expected multiple wrapped lines, got: ${JSON.stringify(contentLines)}`); + + // Every content line should start with the quote border + for (const line of contentLines) { + assert.ok(line.startsWith("│ "), `Wrapped line should have quote border: "${line}"`); + } + + // All content should be preserved + const allText = contentLines.join(" "); + assert.ok(allText.includes("very long"), "Should preserve 'very long'"); + assert.ok(allText.includes("blockquote"), "Should preserve 'blockquote'"); + assert.ok(allText.includes("multiple"), "Should preserve 'multiple'"); + }); + + it("should properly indent wrapped blockquote lines with styling", () => { + const markdown = new Markdown( + "> This is styled text that is long enough to wrap", + 0, + 0, + defaultMarkdownTheme, + { + color: (text) => chalk.yellow(text), // This should NOT be applied to blockquotes + italic: true, + }, + ); + + const lines = markdown.render(25); + const plainLines = lines.map((line) => line.replace(/\x1b\[[0-9;]*m/g, "").trimEnd()); + + // Filter to non-empty lines + const contentLines = plainLines.filter((line) => line.length > 0); + + // All lines should have the quote border + for (const line of contentLines) { + assert.ok(line.startsWith("│ "), `Line should have quote border: "${line}"`); + } + + // Check that italic is applied (from theme.quote) + const allOutput = lines.join("\n"); + assert.ok(allOutput.includes("\x1b[3m"), "Should have italic"); + + // Blockquotes should NOT have the default message color (yellow) + assert.ok(!allOutput.includes("\x1b[33m"), "Should NOT have yellow color from default style"); + }); + + it("should render inline formatting inside blockquotes and reapply quote styling after", () => { + const markdown = new Markdown("> Quote with **bold** and `code`", 0, 0, defaultMarkdownTheme); + + const lines = markdown.render(80); + const plainLines = lines.map((line) => line.replace(/\x1b\[[0-9;]*m/g, "")); + + // Should have the quote border + assert.ok( + plainLines.some((line) => line.startsWith("│ ")), + "Should have quote border", + ); + + // Content should be preserved + const allPlain = plainLines.join(" "); + assert.ok(allPlain.includes("Quote with"), "Should preserve 'Quote with'"); + assert.ok(allPlain.includes("bold"), "Should preserve 'bold'"); + assert.ok(allPlain.includes("code"), "Should preserve 'code'"); + + const allOutput = lines.join("\n"); + + // Should have bold styling (\x1b[1m) + assert.ok(allOutput.includes("\x1b[1m"), "Should have bold styling"); + + // Should have code styling (yellow = \x1b[33m from defaultMarkdownTheme) + assert.ok(allOutput.includes("\x1b[33m"), "Should have code styling (yellow)"); + + // Should have italic from quote styling (\x1b[3m) + assert.ok(allOutput.includes("\x1b[3m"), "Should have italic from quote styling"); + }); + }); + + describe("Heading with inline code", () => { + it("should preserve heading styling after inline code", () => { + const markdown = new Markdown("### Why `sourceInfo` should not be optional", 0, 0, defaultMarkdownTheme); + + const lines = markdown.render(80); + const joinedOutput = lines.join("\n"); + + // The heading theme is bold+cyan. After the yellow inline code, the heading + // styling (bold+cyan) must be restored so subsequent text is styled correctly. + // bold = \x1b[1m, cyan = \x1b[36m, yellow = \x1b[33m + assert.ok(joinedOutput.includes("\x1b[33m"), "Should have yellow for inline code"); + + // Find the position of "should not be optional" in the raw output. + // It must be preceded by heading style codes (bold+cyan), not appear unstyled. + const afterCodeIndex = joinedOutput.indexOf("should not be optional"); + assert.ok(afterCodeIndex > 0, "Should contain text after inline code"); + + // Look at the ANSI codes between the code span end and "should not be optional". + // There should be bold (\x1b[1m) and cyan (\x1b[36m) re-applied. + const precedingChunk = joinedOutput.slice(Math.max(0, afterCodeIndex - 40), afterCodeIndex); + assert.ok( + precedingChunk.includes("\x1b[1m"), + `Should re-apply bold before text after code: ${precedingChunk}`, + ); + assert.ok( + precedingChunk.includes("\x1b[36m"), + `Should re-apply cyan before text after code: ${precedingChunk}`, + ); + }); + + it("should preserve heading styling after inline code for h1", () => { + const markdown = new Markdown("# Title with `code` inside", 0, 0, defaultMarkdownTheme); + + const lines = markdown.render(80); + const joinedOutput = lines.join("\n"); + + const afterCodeIndex = joinedOutput.indexOf("inside"); + assert.ok(afterCodeIndex > 0, "Should contain text after inline code"); + + const precedingChunk = joinedOutput.slice(Math.max(0, afterCodeIndex - 40), afterCodeIndex); + // H1 uses heading + bold + underline + assert.ok(precedingChunk.includes("\x1b[1m"), `Should re-apply bold for h1: ${precedingChunk}`); + assert.ok(precedingChunk.includes("\x1b[36m"), `Should re-apply cyan for h1: ${precedingChunk}`); + assert.ok(precedingChunk.includes("\x1b[4m"), `Should re-apply underline for h1: ${precedingChunk}`); + }); + + it("should not leak h1 underline into padding when inline code is the last token", async () => { + const markdown = new Markdown("# Important distinction from `open()`", 0, 0, defaultMarkdownTheme); + const terminal = new VirtualTerminal(80, 4); + const tui: TUI = new TuiMainScreen(terminal); + tui.addChild(markdown); + tui.start(); + await terminal.waitForRender(); + + const renderedLine = markdown.render(80)[0]; + assert.ok(renderedLine, "Should render heading line"); + const contentWidth = renderedLine.replace(/\x1b\[[0-9;]*m/g, "").trimEnd().length; + assert.ok(contentWidth > 0, "Should have visible heading content"); + + for (let col = contentWidth; col < 80; col++) { + assert.strictEqual( + getCell(terminal, 0, col).isUnderline(), + 0, + `Expected no underline in padding at col ${col}`, + ); + } + + tui.stop(); + }); + + it("should preserve heading styling after bold text", () => { + const markdown = new Markdown("## Heading with **bold** and more", 0, 0, defaultMarkdownTheme); + + const lines = markdown.render(80); + const joinedOutput = lines.join("\n"); + + const afterBoldIndex = joinedOutput.indexOf("and more"); + assert.ok(afterBoldIndex > 0, "Should contain text after bold"); + + const precedingChunk = joinedOutput.slice(Math.max(0, afterBoldIndex - 40), afterBoldIndex); + assert.ok(precedingChunk.includes("\x1b[1m"), `Should re-apply bold for h2: ${precedingChunk}`); + assert.ok(precedingChunk.includes("\x1b[36m"), `Should re-apply cyan for h2: ${precedingChunk}`); + }); + }); + + describe("Strikethrough syntax", () => { + it("should render ~~text~~ as strikethrough", () => { + const markdown = new Markdown("Use ~~strikethrough~~ here", 0, 0, defaultMarkdownTheme); + + const lines = markdown.render(80); + const joinedOutput = lines.join("\n"); + const joinedPlain = lines.map((line) => line.replace(/\x1b\[[0-9;]*m/g, "")).join(" "); + + assert.ok(joinedOutput.includes("\x1b[9m"), "Should apply strikethrough styling"); + assert.ok(joinedPlain.includes("strikethrough"), "Should include struck text content"); + assert.ok(!joinedPlain.includes("~~strikethrough~~"), "Should not render delimiters as text"); + }); + + it("should keep ~text~ as plain text", () => { + const markdown = new Markdown("Use ~strikethrough~ literally", 0, 0, defaultMarkdownTheme); + + const lines = markdown.render(80); + const joinedOutput = lines.join("\n"); + const joinedPlain = lines.map((line) => line.replace(/\x1b\[[0-9;]*m/g, "")).join(" "); + + assert.ok(joinedPlain.includes("~strikethrough~"), "Single-tilde delimiters should remain visible"); + assert.ok(!joinedOutput.includes("\x1b[9m"), "Single-tilde text should not use strikethrough styling"); + }); + }); + + describe("Links", () => { + afterEach(() => { + resetCapabilitiesCache(); + }); + + it("should not duplicate URL for autolinked emails", () => { + // Hyperlinks capability does not affect the mailto: display check. + setCapabilities({ images: null, trueColor: false, hyperlinks: false }); + const markdown = new Markdown("Contact user@example.com for help", 0, 0, defaultMarkdownTheme); + + const lines = markdown.render(80); + const plainLines = lines.map((line) => line.replace(/\x1b\[[0-9;]*m/g, "")); + const joinedPlain = plainLines.join(" "); + + // Should contain the email once, not duplicated with mailto: + assert.ok(joinedPlain.includes("user@example.com"), "Should contain email"); + assert.ok(!joinedPlain.includes("mailto:"), "Should not show mailto: prefix for autolinked emails"); + }); + + it("should not duplicate URL for bare URLs", () => { + setCapabilities({ images: null, trueColor: false, hyperlinks: false }); + const markdown = new Markdown("Visit https://example.com for more", 0, 0, defaultMarkdownTheme); + + const lines = markdown.render(80); + const plainLines = lines.map((line) => line.replace(/\x1b\[[0-9;]*m/g, "")); + const joinedPlain = plainLines.join(" "); + + // URL should appear only once + const urlCount = (joinedPlain.match(/https:\/\/example\.com/g) || []).length; + assert.strictEqual(urlCount, 1, "URL should appear exactly once"); + }); + + it("should show URL in parentheses when hyperlinks are not supported", () => { + setCapabilities({ images: null, trueColor: false, hyperlinks: false }); + const markdown = new Markdown("[click here](https://example.com)", 0, 0, defaultMarkdownTheme); + + const lines = markdown.render(80); + const plainLines = lines.map((line) => line.replace(/\x1b\[[0-9;]*m/g, "")); + const joinedPlain = plainLines.join(" "); + + assert.ok(joinedPlain.includes("click here"), "Should contain link text"); + assert.ok(joinedPlain.includes("(https://example.com)"), "Should show URL in parentheses"); + }); + + it("should show mailto URL in parentheses when hyperlinks are not supported", () => { + setCapabilities({ images: null, trueColor: false, hyperlinks: false }); + const markdown = new Markdown("[Email me](mailto:test@example.com)", 0, 0, defaultMarkdownTheme); + + const lines = markdown.render(80); + const plainLines = lines.map((line) => line.replace(/\x1b\[[0-9;]*m/g, "")); + const joinedPlain = plainLines.join(" "); + + assert.ok(joinedPlain.includes("Email me"), "Should contain link text"); + assert.ok(joinedPlain.includes("(mailto:test@example.com)"), "Should show mailto URL in parentheses"); + }); + + it("should emit OSC 8 hyperlink sequence when terminal supports hyperlinks", () => { + setCapabilities({ images: null, trueColor: false, hyperlinks: true }); + const markdown = new Markdown("[click here](https://example.com)", 0, 0, defaultMarkdownTheme); + + const lines = markdown.render(80); + const joined = lines.join(""); + + // OSC 8 open: ESC ] 8 ; ; ESC \ + assert.ok(joined.includes("\x1b]8;;https://example.com\x1b\\"), "Should contain OSC 8 open sequence"); + // OSC 8 close: ESC ] 8 ; ; ESC \ + assert.ok(joined.includes("\x1b]8;;\x1b\\"), "Should contain OSC 8 close sequence"); + // Visible text is present + const plainLines = lines.map((line) => line.replace(/\x1b[^a-zA-Z]*[a-zA-Z]|\x1b\].*?\x1b\\/g, "")); + assert.ok(plainLines.join("").includes("click here"), "Should contain link text"); + // URL is NOT printed inline as plain text + const rawPlain = lines.map((line) => + line.replace(/\x1b\]8;;[^\x1b]*\x1b\\/g, "").replace(/\x1b\[[0-9;]*m/g, ""), + ); + assert.ok(!rawPlain.join("").includes("(https://example.com)"), "URL should not appear inline in parentheses"); + }); + + it("should use OSC 8 for mailto links when terminal supports hyperlinks", () => { + setCapabilities({ images: null, trueColor: false, hyperlinks: true }); + const markdown = new Markdown("[Email me](mailto:test@example.com)", 0, 0, defaultMarkdownTheme); + + const lines = markdown.render(80); + const joined = lines.join(""); + + assert.ok( + joined.includes("\x1b]8;;mailto:test@example.com\x1b\\"), + "Should contain OSC 8 open with mailto URL", + ); + assert.ok(joined.includes("\x1b]8;;\x1b\\"), "Should contain OSC 8 close sequence"); + }); + + it("should use OSC 8 for bare URLs when terminal supports hyperlinks", () => { + setCapabilities({ images: null, trueColor: false, hyperlinks: true }); + const markdown = new Markdown("Visit https://example.com for more", 0, 0, defaultMarkdownTheme); + + const lines = markdown.render(80); + const joined = lines.join(""); + + assert.ok(joined.includes("\x1b]8;;https://example.com\x1b\\"), "Should contain OSC 8 hyperlink"); + // URL should not also appear as raw parenthetical text + const rawPlain = lines.map((line) => + line.replace(/\x1b\]8;;[^\x1b]*\x1b\\/g, "").replace(/\x1b\[[0-9;]*m/g, ""), + ); + assert.ok(!rawPlain.join("").includes("(https://example.com)"), "URL should not appear twice"); + }); + }); + + describe("HTML-like tags in text", () => { + it("should render content with HTML-like tags as text", () => { + // When the model emits something like content in regular text, + // marked might treat it as HTML and hide the content + const markdown = new Markdown( + "This is text with hidden content that should be visible", + 0, + 0, + defaultMarkdownTheme, + ); + + const lines = markdown.render(80); + const plainLines = lines.map((line) => line.replace(/\x1b\[[0-9;]*m/g, "")); + const joinedPlain = plainLines.join(" "); + + // The content inside the tags should be visible + assert.ok( + joinedPlain.includes("hidden content") || joinedPlain.includes(""), + "Should render HTML-like tags or their content as text, not hide them", + ); + }); + + it("should render HTML tags in code blocks correctly", () => { + const markdown = new Markdown("```html\n
    Some HTML
    \n```", 0, 0, defaultMarkdownTheme); + + const lines = markdown.render(80); + const plainLines = lines.map((line) => line.replace(/\x1b\[[0-9;]*m/g, "")); + const joinedPlain = plainLines.join("\n"); + + // HTML in code blocks should be visible + assert.ok( + joinedPlain.includes("
    ") && joinedPlain.includes("
    "), + "Should render HTML in code blocks", + ); + }); + }); + + describe("Streaming code fences", () => { + it("stabilizes partial closing fence rendering", () => { + const cases = [ + { + input: "```ts\nconst x = 1;\n``", + expected: ["```ts", " const x = 1;", "```"], + }, + { + input: "```md\nnot a closing fence:\n``\n```", + expected: ["```md", " not a closing fence:", " ``", "```"], + }, + { + input: "```ts\n``", + expected: ["```ts", "", "```"], + }, + { + input: "````\n```", + expected: ["```", "", "```"], + }, + { + input: "~~~~~\n~~~~", + expected: ["```", "", "```"], + }, + { + input: "```md\nnot a closing fence:\n``\n```\n\nafter", + expected: ["```md", " not a closing fence:", " ``", "```", "", "after"], + }, + ]; + + for (const { input, expected } of cases) { + const markdown = new Markdown(input, 0, 0, defaultMarkdownTheme); + const lines = markdown.render(80).map((line) => stripAnsi(line).trimEnd()); + + assert.deepStrictEqual(lines, expected); + } + + const partial = new Markdown("```ts\nconst x = 1;\n``", 0, 0, defaultMarkdownTheme); + const complete = new Markdown("```ts\nconst x = 1;\n```", 0, 0, defaultMarkdownTheme); + + assert.strictEqual(partial.render(80).length, complete.render(80).length); + }); + }); +}); diff --git a/packages/tui/test/mouse-components.test.ts b/packages/tui/test/mouse-components.test.ts new file mode 100644 index 0000000000000000000000000000000000000000..cd661515bd63244d65155060ec5475f1974b62ce --- /dev/null +++ b/packages/tui/test/mouse-components.test.ts @@ -0,0 +1,249 @@ +import assert from "node:assert"; +import { describe, it } from "node:test"; +import { Editor, type EditorTheme } from "../src/components/editor.ts"; +import { Input } from "../src/components/input.ts"; +import { SelectList, type SelectListTheme } from "../src/components/select-list.ts"; +import { SettingsList, type SettingsListTheme } from "../src/components/settings-list.ts"; +import { Container, type TuiMouseEvent, type TuiMouseEventType } from "../src/tui.ts"; +import { TuiAltScreen } from "../src/tui-alt-screen.ts"; +import { VirtualTerminal } from "./virtual-terminal.ts"; + +function mouse(type: TuiMouseEventType, x: number, y: number, width = 80, height = 10): TuiMouseEvent { + return { + type, + button: "left", + x, + y, + screenX: x, + screenY: y, + width, + height, + shift: false, + alt: false, + ctrl: false, + ...(type === "click" ? { clickCount: 1 } : {}), + }; +} + +const selectTheme: SelectListTheme = { + selectedPrefix: (text) => text, + selectedText: (text) => text, + description: (text) => text, + scrollInfo: (text) => text, + noMatch: (text) => text, +}; + +const settingsTheme: SettingsListTheme = { + label: (text) => text, + value: (text) => text, + description: (text) => text, + cursor: "> ", + hint: (text) => text, +}; + +const editorTheme: EditorTheme = { + borderColor: (text) => text, + selectList: selectTheme, +}; + +class InputOverlay extends Container { + readonly input = new Input(); + + constructor() { + super(); + this.addChild(this.input); + } + + handleInput(data: string): void { + this.input.handleInput(data); + } +} + +describe("mouse-aware components", () => { + it("positions a single-line input cursor on press", () => { + const input = new Input(); + input.setValue("hello"); + input.render(20); + + assert.strictEqual(input.handleMouse(mouse("press", 4, 0, 20, 1))?.handled, true); + input.handleInput("X"); + assert.strictEqual(input.getValue(), "heXllo"); + }); + + it("selects and activates list rows", () => { + const list = new SelectList( + [ + { value: "a", label: "A" }, + { value: "b", label: "B" }, + { value: "c", label: "C" }, + { value: "d", label: "D" }, + { value: "e", label: "E" }, + ], + 3, + selectTheme, + ); + let selected: string | undefined; + list.onSelect = (item) => { + selected = item.value; + }; + + assert.strictEqual(list.handleMouse(mouse("press", 1, 2, 40, 3))?.handled, true); + assert.strictEqual(list.getSelectedItem()?.value, "c"); + assert.strictEqual(list.handleMouse(mouse("click", 1, 2, 40, 3))?.handled, true); + assert.strictEqual(selected, "c"); + }); + + it("activates settings rows", () => { + const changes: Array<{ id: string; value: string }> = []; + const list = new SettingsList( + [ + { id: "mode", label: "Mode", currentValue: "one", values: ["one", "two"] }, + { id: "other", label: "Other", currentValue: "off", values: ["off", "on"] }, + { id: "third", label: "Third", currentValue: "low", values: ["low", "high"] }, + { id: "fourth", label: "Fourth", currentValue: "x", values: ["x", "y"] }, + ], + 3, + settingsTheme, + (id, value) => changes.push({ id, value }), + () => {}, + ); + + list.handleMouse(mouse("press", 1, 2, 40, 5)); + list.handleMouse(mouse("click", 1, 2, 40, 5)); + assert.deepStrictEqual(changes, [{ id: "third", value: "high" }]); + }); + + for (const row of [0, 4]) { + it(`ignores hover and clicks visible select-list row ${row} after scrolling`, () => { + const list = new SelectList( + Array.from({ length: 12 }, (_, i) => ({ value: `item-${i}`, label: `Item ${i}` })), + 5, + selectTheme, + ); + const changes: string[] = []; + let selected: string | undefined; + list.onSelectionChange = (item) => changes.push(item.value); + list.onSelect = (item) => { + selected = item.value; + }; + list.setSelectedIndex(5); + list.handleMouse({ ...mouse("wheel", 1, row), wheelDelta: 1 }); + assert.strictEqual(list.getSelectedItem()?.value, "item-6"); + assert.deepStrictEqual(changes, ["item-6"]); + const before = list.render(80); + assert.match(before[row], new RegExp(`Item ${4 + row}$`)); + + for (const y of [0, 1, 2, 3, 4, row]) { + assert.strictEqual(list.handleMouse({ ...mouse("move", 1, y), button: "none" }), undefined); + assert.deepStrictEqual(list.render(80), before); + } + assert.strictEqual(list.getSelectedItem()?.value, "item-6"); + assert.deepStrictEqual(changes, ["item-6"]); + assert.strictEqual(selected, undefined); + + list.handleMouse(mouse("press", 1, row)); + list.render(80); + list.handleMouse(mouse("click", 1, row)); + assert.strictEqual(selected, `item-${4 + row}`); + assert.deepStrictEqual(changes, ["item-6", `item-${4 + row}`]); + }); + + it(`ignores hover and clicks visible settings row ${row} after scrolling`, () => { + const changes: Array<{ id: string; value: string }> = []; + const list = new SettingsList( + Array.from({ length: 12 }, (_, i) => ({ + id: `item-${i}`, + label: `Item ${i}`, + description: `Description ${i}`, + currentValue: "off", + values: ["off", "on"], + })), + 5, + settingsTheme, + (id, value) => changes.push({ id, value }), + () => {}, + { enableSearch: true }, + ); + list.selectItem("item-5"); + list.handleMouse({ ...mouse("wheel", 1, row + 2), wheelDelta: 1 }); + const before = list.render(80); + assert.match(before[4], /^> Item 6/); + assert.match(before[row + 2], new RegExp(`Item ${4 + row} `)); + + for (const y of [0, 1, 2, 3, 4, row]) { + assert.strictEqual(list.handleMouse({ ...mouse("move", 1, y + 2), button: "none" }), undefined); + assert.deepStrictEqual(list.render(80), before); + } + assert.deepStrictEqual(changes, []); + + list.handleMouse(mouse("press", 1, row + 2)); + list.render(80); + list.handleMouse(mouse("click", 1, row + 2)); + assert.deepStrictEqual(changes, [{ id: `item-${4 + row}`, value: "on" }]); + }); + } + + it("keeps a delegating overlay focused when its nested input is clicked", async () => { + const terminal = new VirtualTerminal(20, 4); + const tui = new TuiAltScreen(terminal); + const overlay = new InputOverlay(); + overlay.input.setValue("hi"); + tui.start(); + tui.showOverlay(overlay, { anchor: "top-left", width: 20 }); + await terminal.waitForRender(); + + terminal.sendInput("\x1b[<0;5;1M"); + terminal.sendInput("\x1b[<0;5;1m"); + terminal.sendInput("!"); + await terminal.waitForRender(); + + assert.strictEqual(overlay.input.getValue(), "hi!"); + assert.strictEqual(tui.getFocusedComponent(), overlay); + tui.stop(); + }); + + it("positions and focuses the multiline editor through alternate-screen dispatch", async () => { + const terminal = new VirtualTerminal(20, 6); + const tui = new TuiAltScreen(terminal); + const editor = new Editor(tui, editorTheme); + editor.setText("hello"); + tui.addChild(editor); + tui.start(); + await terminal.waitForRender(); + + terminal.sendInput("\x1b[<0;3;2M"); + terminal.sendInput("\x1b[<0;3;2m"); + terminal.sendInput("X"); + await terminal.waitForRender(); + + assert.strictEqual(editor.getText(), "heXllo"); + assert.strictEqual(tui.getFocusedComponent(), editor); + tui.stop(); + }); + + it("selects and copies editor text on drag instead of moving the cursor", async () => { + const terminal = new VirtualTerminal(20, 6); + const copied: string[] = []; + const tui = new TuiAltScreen(terminal, undefined, undefined, { + copySelection: async (text) => { + copied.push(text); + return true; + }, + }); + const editor = new Editor(tui, editorTheme); + editor.setText("hello world"); + tui.addChild(editor); + tui.start(); + await terminal.waitForRender(); + const cursorBefore = editor.getCursor(); + + terminal.sendInput("\x1b[<0;1;2M"); + terminal.sendInput("\x1b[<32;5;2M"); + terminal.sendInput("\x1b[<0;5;2m"); + await terminal.waitForRender(); + + assert.deepStrictEqual(copied, ["hello"]); + assert.deepStrictEqual(editor.getCursor(), cursorBefore); + tui.stop(); + }); +}); diff --git a/packages/tui/test/native-clipboard-linux.test.ts b/packages/tui/test/native-clipboard-linux.test.ts new file mode 100644 index 0000000000000000000000000000000000000000..a1499de45be08c6b01ec20c48178dd1cf63ea079 --- /dev/null +++ b/packages/tui/test/native-clipboard-linux.test.ts @@ -0,0 +1,247 @@ +import assert from "node:assert/strict"; +import { type ChildProcessWithoutNullStreams, execFile, execFileSync, spawn, spawnSync } from "node:child_process"; +import { createHash } from "node:crypto"; +import { once } from "node:events"; +import { mkdtempSync, rmSync } from "node:fs"; +import { createServer, type Socket } from "node:net"; +import { tmpdir } from "node:os"; +import { join } from "node:path"; +import { after, before, describe, it, type TestContext } from "node:test"; +import { fileURLToPath } from "node:url"; +import { promisify } from "node:util"; + +const exec = promisify(execFile); +const fixtures = fileURLToPath(new URL("./fixtures/", import.meta.url)); +const native = fileURLToPath(new URL("../native/linux/", import.meta.url)); +const supported = process.platform === "linux" && ["arm64", "x64"].includes(process.arch); +const dependenciesAvailable = + supported && + spawnSync("sh", ["-c", "command -v cc && command -v Xvfb && command -v xclip && pkg-config --exists xcb"], { + stdio: "ignore", + }).status === 0; + +interface ReaderResult { + ok: boolean; + value?: string | null; + unavailable?: boolean; + length?: number; + hash?: string; + error?: string; +} + +async function startServer( + t: TestContext, + command: string, + args: string[], + env: NodeJS.ProcessEnv, +): Promise<{ child: ChildProcessWithoutNullStreams; ready: string }> { + const child = spawn(command, args, { env, stdio: "pipe" }); + t.after(async () => { + if (child.exitCode === null && child.signalCode === null) { + const exited = once(child, "exit"); + child.kill("SIGKILL"); + await exited; + } + }); + let stderr = ""; + child.stderr.on("data", (data: Buffer) => { + stderr += data.toString(); + }); + const ready = await new Promise((resolve, reject) => { + child.once("error", reject); + child.once("exit", (code) => reject(new Error(`${command} exited with ${code}: ${stderr}`))); + child.stdout.once("data", (data: Buffer) => resolve(data.toString().trim())); + }); + return { child, ready }; +} + +async function readClipboard(method: string, env: NodeJS.ProcessEnv): Promise { + const { stdout } = await exec( + process.execPath, + [ + join(fixtures, "clipboard-reader.cjs"), + join(native, "prebuilds", `linux-${process.arch}`, "linux-platform-x11.node"), + method, + ], + { env, timeout: 6000 }, + ); + return JSON.parse(stdout) as ReaderResult; +} + +describe("native Linux clipboard", { skip: !dependenciesAvailable, timeout: 60000 }, () => { + let directory: string; + + async function startX11(t: TestContext) { + const { child, ready } = await startServer( + t, + "Xvfb", + ["-displayfd", "1", "-screen", "0", "640x480x24", "-nolisten", "tcp"], + process.env, + ); + return { child, env: { ...process.env, DISPLAY: `:${ready}` } }; + } + + before(() => { + directory = mkdtempSync(join(tmpdir(), "pi-clipboard-test-")); + const flags = ["-std=c11", "-D_POSIX_C_SOURCE=200809L", "-Wall", "-Wextra", "-Werror", "-pthread"]; + execFileSync("cc", [ + ...flags, + join(fixtures, "clipboard-x11-test.c"), + "-lxcb", + "-ldl", + "-o", + join(directory, "x11-test"), + ]); + execFileSync("cc", [ + ...flags, + "-shared", + "-fPIC", + "-nostdlib", // Match the prebuild's unversioned system-library imports. + ...(process.arch === "arm64" ? ["-mno-outline-atomics"] : []), // Inline the fixture's allocation counters. + "-Wl,-z,nodelete", + join(fixtures, "clipboard-worker-test.c"), + "-ldl", + "-o", + join(directory, "worker-test.node"), + ]); + }); + + after(() => { + if (directory) rmSync(directory, { recursive: true, force: true }); + }); + + it("validates X11 incremental property metadata before accumulating bytes", () => { + assert.equal(execFileSync(join(directory, "x11-test"), ["metadata"], { encoding: "utf8" }).trim(), "validated"); + }); + + for (const mode of ["concurrent", "process-exit", "worker-exit"]) { + it(`bounds private clipboard work and preserves cleanup: ${mode}`, async () => { + const { stdout } = await exec( + process.execPath, + [join(fixtures, "clipboard-worker-test.cjs"), join(directory, "worker-test.node"), mode], + { env: { ...process.env, UV_THREADPOOL_SIZE: "2" }, timeout: 10000 }, + ); + assert.equal(stdout.trim(), "passed"); + }); + } + + for (const method of ["getText", "getImage"]) { + it(`keeps JS responsive during stalled X11 connection setup: ${method}`, async (t) => { + const sockets = new Set(); + let connections = 0; + const server = createServer((socket) => { + connections++; + sockets.add(socket); + socket.on("data", () => {}); // Accept setup bytes without replying. + // libxcb owns setup blocking; release it after checking the JS timer. + const timer = setTimeout(() => socket.destroy(), 300); + t.after(() => clearTimeout(timer)); + socket.on("close", () => sockets.delete(socket)); + }); + t.after(async () => { + for (const socket of sockets) socket.destroy(); + await new Promise((resolve, reject) => server.close((error) => (error ? reject(error) : resolve()))); + }); + server.listen(0, "127.0.0.1"); + await once(server, "listening"); + const address = server.address(); + assert.ok(address && typeof address !== "string" && address.port > 6000); + const env = { ...process.env, DISPLAY: `127.0.0.1:${address.port - 6000}` }; + const started = performance.now(); + const result = await readClipboard(method, env); + assert.ok(performance.now() - started < 3500, "The worker must return after the connection closes"); + assert.deepEqual(result, { ok: true, unavailable: true }); + assert.ok(connections > 0, "The helper must reach the stalled X11 server"); + assert.equal(sockets.size, 0, "The failed connection must be closed"); + }); + } + + it("decodes X11 STRING as Latin-1", async (t) => { + const { env } = await startX11(t); + execFileSync("xclip", ["-selection", "clipboard", "-in", "-t", "STRING"], { + env, + input: Buffer.from("café £ÿ", "latin1"), + stdio: ["pipe", "ignore", "ignore"], + timeout: 5000, + }); + const result = await readClipboard("getText", env); + assert.equal(result.ok, true, result.error); + assert.equal(result.value, "café £ÿ"); + }); + + it("reads UTF8_STRING when X11 TARGETS is refused", async (t) => { + const { env } = await startX11(t); + await startServer(t, join(directory, "x11-test"), ["direct-text"], env); + const result = await readClipboard("getText", env); + assert.equal(result.ok, true, result.error); + assert.equal(result.value, "café 日本語"); + }); + + it("returns null for empty X11 clipboards and text-only selections", async (t) => { + const { env } = await startX11(t); + assert.deepEqual(await readClipboard("getText", env), { ok: true, value: null }); + assert.deepEqual(await readClipboard("getImage", env), { ok: true, value: null }); + execFileSync("xclip", ["-selection", "clipboard", "-in", "-t", "UTF8_STRING"], { + env, + input: "text only", + stdio: ["pipe", "ignore", "ignore"], + timeout: 5000, + }); + assert.deepEqual(await readClipboard("getImage", env), { ok: true, value: null }); + }); + + it("preserves X11 Unicode and incremental text/image transfers", async (t) => { + const { env } = await startX11(t); + for (const [target, method, bytes] of [ + ["UTF8_STRING", "getText", Buffer.from("café 日本語")], + ["UTF8_STRING", "getText", Buffer.alloc(4 * 1024 * 1024, 120)], + ["image/png", "getImage", Buffer.alloc(4 * 1024 * 1024, 123)], + ] as const) { + execFileSync("xclip", ["-selection", "clipboard", "-in", "-t", target], { + env, + input: bytes, + stdio: ["pipe", "ignore", "ignore"], + timeout: 5000, + }); + const result = await readClipboard(method, env); + assert.equal(result.ok, true, result.error); + assert.equal(result.length, bytes.length); + assert.equal(result.hash, createHash("sha256").update(bytes).digest("hex")); + } + }); + + for (const mode of ["disconnect", "timeout"]) { + it(`reports X11 transfer ${mode} as an exception, not an unavailable display`, async (t) => { + const { child, env } = await startX11(t); + const owner = await startServer(t, join(directory, "x11-test"), ["idle"], env); + const requested = once(owner.child.stdout, "data"); + const started = performance.now(); + const result = readClipboard("getText", env); + await requested; + if (mode === "disconnect") child.kill("SIGKILL"); + const failure = await result; + assert.equal(failure.ok, false); + assert.match(failure.error ?? "", /X11 clipboard/); + assert.ok(performance.now() - started < 3500, "The transfer must share the 2-second deadline"); + }); + } + + for (const content of ["text", "image"]) { + it(`frees partially received X11 ${content} after invalid metadata`, async (t) => { + const { env } = await startX11(t); + await startServer(t, join(directory, "x11-test"), ["invalid"], env); + const { stdout } = await exec(join(directory, "x11-test"), [content], { env, timeout: 6000 }); + assert.equal(stdout.trim(), "clean"); + }); + + it(`times out stalled X11 ${content} after a partial transfer without blocking JS`, async (t) => { + const { env } = await startX11(t); + await startServer(t, join(directory, "x11-test"), ["partial"], env); + const started = performance.now(); + const result = await readClipboard(content === "text" ? "getText" : "getImage", env); + assert.equal(result.ok, false); + assert.match(result.error ?? "", /X11 clipboard/); + assert.ok(performance.now() - started < 3500); + }); + } +}); diff --git a/packages/tui/test/native-module-path.test.ts b/packages/tui/test/native-module-path.test.ts new file mode 100644 index 0000000000000000000000000000000000000000..2727c33b3b9efa4962e4649838979c8f42486a35 --- /dev/null +++ b/packages/tui/test/native-module-path.test.ts @@ -0,0 +1,45 @@ +import assert from "node:assert"; +import { dirname, join, resolve } from "node:path"; +import { describe, it } from "node:test"; +import { pathToFileURL } from "node:url"; +import { getNativeModuleCandidates } from "../src/native-module-path.ts"; + +describe("getNativeModuleCandidates", () => { + it("resolves native helpers from the installed TUI package when the module is bundled elsewhere", () => { + const packageRoot = resolve("virtual", "node_modules", "@earendil-works", "pi-tui"); + const bundledModule = resolve("virtual", "pi-coding-agent", "dist", "bundle", "chunks", "chunk.js"); + const nativePath = join("native", "win32", "prebuilds", "win32-arm64", "win32-platform.node"); + + const candidates = getNativeModuleCandidates(nativePath, { + moduleUrl: pathToFileURL(bundledModule).href, + execPath: resolve("virtual", "node", "node.exe"), + resolvePackage: (specifier) => { + assert.equal(specifier, "@earendil-works/pi-tui"); + return join(packageRoot, "dist", "index.js"); + }, + }); + + assert.equal(candidates[0], join(packageRoot, nativePath)); + assert.ok(candidates.includes(join(dirname(bundledModule), "..", nativePath))); + }); + + it("keeps standalone binary fallbacks when the TUI package is unavailable", () => { + const bundledModule = resolve("virtual", "pi", "bundle", "chunks", "chunk.js"); + const execPath = resolve("virtual", "pi", "pi.exe"); + const nativePath = join("native", "darwin", "prebuilds", "darwin-arm64", "darwin-platform.node"); + + const candidates = getNativeModuleCandidates(nativePath, { + moduleUrl: pathToFileURL(bundledModule).href, + execPath, + resolvePackage: () => { + throw new Error("not installed"); + }, + }); + + assert.deepEqual(candidates, [ + join(dirname(bundledModule), "..", nativePath), + join(dirname(bundledModule), nativePath), + join(dirname(execPath), nativePath), + ]); + }); +}); diff --git a/packages/tui/test/native-platform.test.ts b/packages/tui/test/native-platform.test.ts new file mode 100644 index 0000000000000000000000000000000000000000..0fedd069a6cf46890580a35be04ad4e6321e41ad --- /dev/null +++ b/packages/tui/test/native-platform.test.ts @@ -0,0 +1,100 @@ +import assert from "node:assert/strict"; +import { readFileSync } from "node:fs"; +import { createRequire, Module } from "node:module"; +import { test } from "node:test"; +import { fileURLToPath } from "node:url"; +import { getNativeClipboard, getNativePlatformHelper } from "../src/native-platform.ts"; + +test("Linux ARM64 prebuild supports 64 KB system pages", () => { + const binary = readFileSync( + new URL("../native/linux/prebuilds/linux-arm64/linux-platform-x11.node", import.meta.url), + ); + assert.equal(binary.subarray(0, 4).toString(), "\x7fELF"); + const offset = Number(binary.readBigUInt64LE(32)); + const entrySize = binary.readUInt16LE(54); + const count = binary.readUInt16LE(56); + let loadSegments = 0; + for (let index = 0; index < count; index++) { + const entry = offset + index * entrySize; + if (binary.readUInt32LE(entry) !== 1) continue; + loadSegments++; + const fileOffset = binary.readBigUInt64LE(entry + 8); + const address = binary.readBigUInt64LE(entry + 16); + assert.equal((address - fileOffset) % 65536n, 0n); + assert.ok(binary.readBigUInt64LE(entry + 48) >= 65536n); + } + assert.ok(loadSegments > 0); +}); + +// Opt in on a Windows test desktop: this replaces the system clipboard contents. +test( + "writes Windows clipboard text through the native helper without command fallbacks", + { skip: process.platform !== "win32" || process.env.PI_TEST_NATIVE_CLIPBOARD !== "1" }, + async () => { + const clipboard = getNativeClipboard(); + assert.ok(clipboard?.setText); + for (const text of ["clipboard café 日本語", "", "second write"]) { + await clipboard.setText(text); + assert.equal(await clipboard.getText(), text); + assert.equal(await clipboard.getImage(), null); + } + }, +); + +test( + "uses the native platform helper directly as the clipboard API", + { skip: !["darwin", "win32"].includes(process.platform) || !["arm64", "x64"].includes(process.arch) }, + () => { + const clipboard = getNativeClipboard(); + assert.ok(clipboard); + assert.equal(typeof clipboard.getText, "function"); + assert.equal(typeof clipboard.getImage, "function"); + assert.equal(typeof clipboard.setText, "function"); + assert.equal(clipboard, getNativePlatformHelper()); + assert.equal(clipboard, getNativeClipboard()); + }, +); + +test("Linux loads X11 lazily and rechecks DISPLAY", { skip: !["arm64", "x64"].includes(process.arch) }, async (t) => { + const require = createRequire(import.meta.url); + const platform = Object.getOwnPropertyDescriptor(process, "platform")!; + const display = process.env.DISPLAY; + const waylandDisplay = process.env.WAYLAND_DISPLAY; + const modulePath = fileURLToPath( + new URL(`../native/linux/prebuilds/linux-${process.arch}/linux-platform-x11.node`, import.meta.url), + ); + const previous = require.cache[modulePath]; + t.after(() => { + Object.defineProperty(process, "platform", platform); + if (display === undefined) delete process.env.DISPLAY; + else process.env.DISPLAY = display; + if (waylandDisplay === undefined) delete process.env.WAYLAND_DISPLAY; + else process.env.WAYLAND_DISPLAY = waylandDisplay; + if (previous) require.cache[modulePath] = previous; + else delete require.cache[modulePath]; + }); + Object.defineProperty(process, "platform", { value: "linux" }); + delete process.env.DISPLAY; + process.env.WAYLAND_DISPLAY = "wayland-0"; + assert.equal(getNativeClipboard(), undefined); // Wayland paste uses wl-paste. + let available = false; + const helper = { + getText: t.mock.fn(async () => (available ? "X11" : undefined)), + getImage: t.mock.fn(async () => null), + }; + const module = new Module(modulePath); + module.exports = helper; + require.cache[modulePath] = module; + process.env.DISPLAY = ":0"; + const clipboard = getNativeClipboard()!; + assert.equal(clipboard, helper); + assert.equal(helper.getText.mock.callCount(), 0); + assert.equal(await clipboard.getText(), undefined); + available = true; + assert.equal(await clipboard.getText(), "X11"); + assert.equal(await clipboard.getImage(), null); + delete process.env.DISPLAY; + assert.equal(getNativeClipboard(), undefined); + process.env.DISPLAY = ":1"; + assert.equal(getNativeClipboard(), clipboard); +}); diff --git a/packages/tui/test/overlay-non-capturing.test.ts b/packages/tui/test/overlay-non-capturing.test.ts new file mode 100644 index 0000000000000000000000000000000000000000..075fed08be75b414aa7be4893c5b36b43e9cbff9 --- /dev/null +++ b/packages/tui/test/overlay-non-capturing.test.ts @@ -0,0 +1,1203 @@ +import assert from "node:assert"; +import { describe, it } from "node:test"; +import type { Component, Focusable } from "../src/tui.ts"; +import { Container, type TUI } from "../src/tui.ts"; +import { TuiMainScreen } from "../src/tui-main-screen.ts"; +import { VirtualTerminal } from "./virtual-terminal.ts"; + +class StaticOverlay implements Component { + private lines: string[]; + + constructor(lines: string[]) { + this.lines = lines; + } + + render(): string[] { + return this.lines; + } + + invalidate(): void {} +} + +class EmptyContent implements Component { + render(): string[] { + return []; + } + invalidate(): void {} +} + +class FocusableOverlay implements Component, Focusable { + focused = false; + inputs: string[] = []; + private lines: string[]; + + constructor(lines: string[]) { + this.lines = lines; + } + + handleInput(data: string): void { + this.inputs.push(data); + } + + render(): string[] { + return this.lines; + } + + invalidate(): void {} +} + +async function renderAndFlush(tui: TUI, terminal: VirtualTerminal): Promise { + tui.requestRender(true); + await new Promise((resolve) => process.nextTick(resolve)); + await terminal.waitForRender(); +} + +describe("TUI overlay non-capturing", () => { + describe("focus management", () => { + it("non-capturing overlay preserves focus on creation", async () => { + const terminal = new VirtualTerminal(80, 24); + const tui: TUI = new TuiMainScreen(terminal); + const editor = new FocusableOverlay(["EDITOR"]); + const overlay = new FocusableOverlay(["OVERLAY"]); + tui.addChild(new EmptyContent()); + tui.setFocus(editor); + tui.start(); + try { + tui.showOverlay(overlay, { nonCapturing: true }); + await renderAndFlush(tui, terminal); + assert.strictEqual(editor.focused, true); + assert.strictEqual(overlay.focused, false); + } finally { + tui.stop(); + } + }); + + it("focus() transfers focus to the overlay", async () => { + const terminal = new VirtualTerminal(80, 24); + const tui: TUI = new TuiMainScreen(terminal); + const editor = new FocusableOverlay(["EDITOR"]); + const overlay = new FocusableOverlay(["OVERLAY"]); + tui.addChild(new EmptyContent()); + tui.setFocus(editor); + tui.start(); + try { + const handle = tui.showOverlay(overlay, { nonCapturing: true }); + handle.focus(); + await renderAndFlush(tui, terminal); + assert.strictEqual(editor.focused, false); + assert.strictEqual(overlay.focused, true); + assert.strictEqual(handle.isFocused(), true); + } finally { + tui.stop(); + } + }); + + it("unfocus() restores previous focus", async () => { + const terminal = new VirtualTerminal(80, 24); + const tui: TUI = new TuiMainScreen(terminal); + const editor = new FocusableOverlay(["EDITOR"]); + const overlay = new FocusableOverlay(["OVERLAY"]); + tui.addChild(new EmptyContent()); + tui.setFocus(editor); + tui.start(); + try { + const handle = tui.showOverlay(overlay, { nonCapturing: true }); + handle.focus(); + handle.unfocus(); + await renderAndFlush(tui, terminal); + assert.strictEqual(editor.focused, true); + assert.strictEqual(overlay.focused, false); + assert.strictEqual(handle.isFocused(), false); + } finally { + tui.stop(); + } + }); + + it("setHidden(false) on non-capturing overlay does not auto-focus", async () => { + const terminal = new VirtualTerminal(80, 24); + const tui: TUI = new TuiMainScreen(terminal); + const editor = new FocusableOverlay(["EDITOR"]); + const overlay = new FocusableOverlay(["OVERLAY"]); + tui.addChild(new EmptyContent()); + tui.setFocus(editor); + tui.start(); + try { + const handle = tui.showOverlay(overlay, { nonCapturing: true }); + handle.setHidden(true); + handle.setHidden(false); + await renderAndFlush(tui, terminal); + assert.strictEqual(editor.focused, true); + assert.strictEqual(overlay.focused, false); + } finally { + tui.stop(); + } + }); + + it("hide() when overlay is not focused does not change focus", async () => { + const terminal = new VirtualTerminal(80, 24); + const tui: TUI = new TuiMainScreen(terminal); + const editor = new FocusableOverlay(["EDITOR"]); + const overlay = new FocusableOverlay(["OVERLAY"]); + tui.addChild(new EmptyContent()); + tui.setFocus(editor); + tui.start(); + try { + const handle = tui.showOverlay(overlay, { nonCapturing: true }); + handle.hide(); + await renderAndFlush(tui, terminal); + assert.strictEqual(editor.focused, true); + } finally { + tui.stop(); + } + }); + + it("hide() when focused restores focus correctly", async () => { + const terminal = new VirtualTerminal(80, 24); + const tui: TUI = new TuiMainScreen(terminal); + const editor = new FocusableOverlay(["EDITOR"]); + const overlay = new FocusableOverlay(["OVERLAY"]); + tui.addChild(new EmptyContent()); + tui.setFocus(editor); + tui.start(); + try { + const handle = tui.showOverlay(overlay, { nonCapturing: true }); + handle.focus(); + handle.hide(); + await renderAndFlush(tui, terminal); + assert.strictEqual(editor.focused, true); + assert.strictEqual(overlay.focused, false); + } finally { + tui.stop(); + } + }); + + it("capturing overlay removed with non-capturing below restores focus to editor", async () => { + const terminal = new VirtualTerminal(80, 24); + const tui: TUI = new TuiMainScreen(terminal); + const editor = new FocusableOverlay(["EDITOR"]); + const nonCapturing = new FocusableOverlay(["NC"]); + const capturing = new FocusableOverlay(["CAP"]); + tui.addChild(new EmptyContent()); + tui.setFocus(editor); + tui.start(); + try { + tui.showOverlay(nonCapturing, { nonCapturing: true }); + const handle = tui.showOverlay(capturing); + assert.strictEqual(capturing.focused, true); + handle.hide(); + await renderAndFlush(tui, terminal); + assert.strictEqual(editor.focused, true); + assert.strictEqual(nonCapturing.focused, false); + } finally { + tui.stop(); + } + }); + + it("sub-overlay cleanup then hideOverlay restores focus and input to editor", async () => { + const terminal = new VirtualTerminal(80, 24); + const tui: TUI = new TuiMainScreen(terminal); + const editor = new FocusableOverlay(["EDITOR"]); + const timer = new FocusableOverlay(["TIMER"]); + const controller = new FocusableOverlay(["CTRL"]); + tui.addChild(new EmptyContent()); + tui.setFocus(editor); + tui.start(); + try { + const timerHandle = tui.showOverlay(timer, { nonCapturing: true }); + tui.showOverlay(controller); + assert.strictEqual(controller.focused, true); + assert.strictEqual(editor.focused, false); + timerHandle.hide(); + tui.hideOverlay(); + await renderAndFlush(tui, terminal); + assert.strictEqual(editor.focused, true); + assert.strictEqual(controller.focused, false); + assert.strictEqual(timer.focused, false); + terminal.sendInput("x"); + await renderAndFlush(tui, terminal); + assert.deepStrictEqual(editor.inputs, ["x"]); + assert.deepStrictEqual(controller.inputs, []); + assert.deepStrictEqual(timer.inputs, []); + } finally { + tui.stop(); + } + }); + + it("removed focused child overlay does not become parent overlay fallback", async () => { + const terminal = new VirtualTerminal(80, 24); + const tui: TUI = new TuiMainScreen(terminal); + const editor = new FocusableOverlay(["EDITOR"]); + const child = new FocusableOverlay(["CHILD"]); + const parent = new FocusableOverlay(["PARENT"]); + tui.addChild(new EmptyContent()); + tui.setFocus(editor); + tui.start(); + try { + const childHandle = tui.showOverlay(child, { nonCapturing: true }); + childHandle.focus(); + const parentHandle = tui.showOverlay(parent); + assert.strictEqual(parent.focused, true); + + childHandle.hide(); + parentHandle.hide(); + terminal.sendInput("x"); + await renderAndFlush(tui, terminal); + + assert.deepStrictEqual(editor.inputs, ["x"]); + assert.deepStrictEqual(child.inputs, []); + assert.deepStrictEqual(parent.inputs, []); + assert.strictEqual(editor.focused, true); + } finally { + tui.stop(); + } + }); + + it("microtask-deferred sub-overlay pattern (showExtensionCustom simulation) restores focus", async () => { + const terminal = new VirtualTerminal(80, 24); + const tui: TUI = new TuiMainScreen(terminal); + const editor = new FocusableOverlay(["EDITOR"]); + const timer = new FocusableOverlay(["TIMER"]); + const controller = new FocusableOverlay(["CTRL"]); + tui.addChild(new EmptyContent()); + tui.setFocus(editor); + tui.start(); + try { + // Simulate showExtensionCustom: factory creates timer synchronously, + // then .then() pushes controller as a microtask + let timerHandle: ReturnType | null = null; + let doneFn: () => void = () => { + throw new Error("doneFn was not initialized"); + }; + + const overlayPromise = new Promise((resolve) => { + doneFn = () => { + if (!timerHandle) throw new Error("timerHandle was not initialized"); + timerHandle.hide(); + tui.hideOverlay(); + resolve(); + }; + timerHandle = tui.showOverlay(timer, { nonCapturing: true }); + // .then() runs as microtask — same as showExtensionCustom + Promise.resolve(controller).then((c) => { + tui.showOverlay(c); + }); + }); + + await Promise.resolve(); + await renderAndFlush(tui, terminal); + + assert.strictEqual(controller.focused, true); + assert.strictEqual(editor.focused, false); + + // Simulate Esc: cleanup + close (from inside handleInput) + doneFn(); + // Now await the promise (simulating showExtensionCustom resolving) + await overlayPromise; + await renderAndFlush(tui, terminal); + + assert.strictEqual(editor.focused, true, "editor should regain focus"); + assert.strictEqual(controller.focused, false); + assert.strictEqual(timer.focused, false); + + terminal.sendInput("x"); + await renderAndFlush(tui, terminal); + assert.deepStrictEqual(editor.inputs, ["x"], "editor should receive input after close"); + assert.deepStrictEqual(controller.inputs, []); + } finally { + tui.stop(); + } + }); + + it("handleInput redirection skips non-capturing overlays when focused overlay becomes invisible", async () => { + const terminal = new VirtualTerminal(80, 24); + const tui: TUI = new TuiMainScreen(terminal); + const editor = new FocusableOverlay(["EDITOR"]); + const fallbackCapturing = new FocusableOverlay(["FALLBACK"]); + const nonCapturing = new FocusableOverlay(["NC"]); + const primary = new FocusableOverlay(["PRIMARY"]); + let isVisible = true; + tui.addChild(new EmptyContent()); + tui.setFocus(editor); + tui.start(); + try { + tui.showOverlay(fallbackCapturing); + tui.showOverlay(nonCapturing, { nonCapturing: true }); + tui.showOverlay(primary, { visible: () => isVisible }); + assert.strictEqual(primary.focused, true); + isVisible = false; + terminal.sendInput("x"); + await renderAndFlush(tui, terminal); + assert.deepStrictEqual(primary.inputs, []); + assert.deepStrictEqual(nonCapturing.inputs, []); + assert.deepStrictEqual(fallbackCapturing.inputs, ["x"]); + assert.strictEqual(fallbackCapturing.focused, true); + } finally { + tui.stop(); + } + }); + + it("active base focus replacement receives close input before overlay restore", async () => { + const terminal = new VirtualTerminal(80, 24); + const tui: TUI = new TuiMainScreen(terminal); + const editor = new FocusableOverlay(["EDITOR"]); + const replacement = new FocusableOverlay(["REPLACEMENT"]); + const overlay = new FocusableOverlay(["OVERLAY"]); + overlay.handleInput = (data: string) => { + overlay.inputs.push(data); + if (data === "b") { + tui.setFocus(replacement); + } + }; + replacement.handleInput = (data: string) => { + replacement.inputs.push(data); + if (data === "\r") { + tui.setFocus(editor); + } + }; + tui.addChild(new EmptyContent()); + tui.setFocus(editor); + tui.start(); + try { + tui.showOverlay(overlay); + assert.strictEqual(overlay.focused, true); + terminal.sendInput("b"); + await renderAndFlush(tui, terminal); + assert.strictEqual(replacement.focused, true); + + terminal.sendInput("\r"); + await renderAndFlush(tui, terminal); + assert.deepStrictEqual(replacement.inputs, ["\r"]); + assert.deepStrictEqual(overlay.inputs, ["b"]); + assert.strictEqual(overlay.focused, true); + + terminal.sendInput("x"); + await renderAndFlush(tui, terminal); + assert.deepStrictEqual(overlay.inputs, ["b", "x"]); + } finally { + tui.stop(); + } + }); + + it("active replacement still receives input when it is another overlay preFocus", async () => { + const terminal = new VirtualTerminal(80, 24); + const tui: TUI = new TuiMainScreen(terminal); + const editor = new FocusableOverlay(["EDITOR"]); + const replacement = new FocusableOverlay(["REPLACEMENT"]); + const passive = new FocusableOverlay(["PASSIVE"]); + const overlay = new FocusableOverlay(["OVERLAY"]); + overlay.handleInput = (data: string) => { + overlay.inputs.push(data); + if (data === "b") { + tui.setFocus(replacement); + } + }; + replacement.handleInput = (data: string) => { + replacement.inputs.push(data); + if (data === "\r") { + tui.setFocus(editor); + } + }; + tui.addChild(new EmptyContent()); + tui.setFocus(editor); + tui.start(); + try { + tui.setFocus(replacement); + tui.showOverlay(passive, { nonCapturing: true }); + tui.setFocus(editor); + tui.showOverlay(overlay); + terminal.sendInput("b"); + await renderAndFlush(tui, terminal); + assert.strictEqual(replacement.focused, true); + + terminal.sendInput("1"); + terminal.sendInput("\r"); + await renderAndFlush(tui, terminal); + assert.deepStrictEqual(replacement.inputs, ["1", "\r"]); + assert.deepStrictEqual(overlay.inputs, ["b"]); + assert.strictEqual(overlay.focused, true); + } finally { + tui.stop(); + } + }); + + it("blocked replacement can move focus internally before overlay restore", async () => { + const terminal = new VirtualTerminal(80, 24); + const tui: TUI = new TuiMainScreen(terminal); + const base = new Container(); + const editor = new FocusableOverlay(["EDITOR"]); + const firstReplacement = new FocusableOverlay(["FIRST"]); + const secondReplacement = new FocusableOverlay(["SECOND"]); + const overlay = new FocusableOverlay(["OVERLAY"]); + overlay.handleInput = (data: string) => { + overlay.inputs.push(data); + if (data === "b") tui.setFocus(firstReplacement); + }; + firstReplacement.handleInput = (data: string) => { + firstReplacement.inputs.push(data); + if (data === "n") tui.setFocus(secondReplacement); + }; + secondReplacement.handleInput = (data: string) => { + secondReplacement.inputs.push(data); + if (data === "\r") { + base.clear(); + base.addChild(editor); + tui.setFocus(editor); + } + }; + base.addChild(editor); + base.addChild(firstReplacement); + base.addChild(secondReplacement); + tui.addChild(base); + tui.setFocus(editor); + tui.start(); + try { + tui.showOverlay(overlay); + terminal.sendInput("b"); + await renderAndFlush(tui, terminal); + terminal.sendInput("n"); + await renderAndFlush(tui, terminal); + terminal.sendInput("2"); + terminal.sendInput("\r"); + await renderAndFlush(tui, terminal); + + assert.deepStrictEqual(overlay.inputs, ["b"]); + assert.deepStrictEqual(firstReplacement.inputs, ["n"]); + assert.deepStrictEqual(secondReplacement.inputs, ["2", "\r"]); + assert.strictEqual(overlay.focused, true); + } finally { + tui.stop(); + } + }); + + it("removed replacement restores overlay even when overlay preFocus differs from next focus", async () => { + const terminal = new VirtualTerminal(80, 24); + const tui: TUI = new TuiMainScreen(terminal); + const base = new Container(); + const editor = new FocusableOverlay(["EDITOR"]); + const palette = new FocusableOverlay(["PALETTE"]); + const replacement = new FocusableOverlay(["REPLACEMENT"]); + const overlay = new FocusableOverlay(["OVERLAY"]); + overlay.handleInput = (data: string) => { + overlay.inputs.push(data); + if (data === "b") tui.setFocus(replacement); + }; + replacement.handleInput = (data: string) => { + replacement.inputs.push(data); + if (data === "\r") { + base.clear(); + base.addChild(editor); + tui.setFocus(editor); + } + }; + base.addChild(editor); + base.addChild(palette); + base.addChild(replacement); + tui.addChild(base); + tui.setFocus(palette); + tui.start(); + try { + tui.showOverlay(overlay); + terminal.sendInput("b"); + await renderAndFlush(tui, terminal); + terminal.sendInput("\r"); + terminal.sendInput("x"); + await renderAndFlush(tui, terminal); + + assert.deepStrictEqual(overlay.inputs, ["b", "x"]); + assert.deepStrictEqual(replacement.inputs, ["\r"]); + assert.deepStrictEqual(editor.inputs, []); + assert.strictEqual(overlay.focused, true); + } finally { + tui.stop(); + } + }); + + it("unfocus target releases a blocked overlay while replacement remains focused", async () => { + const terminal = new VirtualTerminal(80, 24); + const tui: TUI = new TuiMainScreen(terminal); + const fallback = new FocusableOverlay(["FALLBACK"]); + const target = new FocusableOverlay(["TARGET"]); + const replacement = new FocusableOverlay(["REPLACEMENT"]); + const overlay = new FocusableOverlay(["OVERLAY"]); + replacement.handleInput = (data: string) => { + replacement.inputs.push(data); + if (data === "\r") tui.setFocus(fallback); + }; + tui.addChild(new EmptyContent()); + tui.start(); + try { + const overlayHandle = tui.showOverlay(overlay); + overlay.handleInput = (data: string) => { + overlay.inputs.push(data); + if (data === "b") { + tui.setFocus(replacement); + overlayHandle.unfocus({ target }); + } + }; + + terminal.sendInput("b"); + await renderAndFlush(tui, terminal); + assert.strictEqual(replacement.focused, true); + terminal.sendInput("\r"); + terminal.sendInput("x"); + await renderAndFlush(tui, terminal); + + assert.deepStrictEqual(overlay.inputs, ["b"]); + assert.deepStrictEqual(replacement.inputs, ["\r"]); + assert.deepStrictEqual(fallback.inputs, []); + assert.deepStrictEqual(target.inputs, ["x"]); + } finally { + tui.stop(); + } + }); + + it("handleInput restores focus to a visible focused overlay after base focus steal", async () => { + const terminal = new VirtualTerminal(80, 24); + const tui: TUI = new TuiMainScreen(terminal); + const editor = new FocusableOverlay(["EDITOR"]); + const replacement = new FocusableOverlay(["REPLACEMENT"]); + const overlay = new FocusableOverlay(["OVERLAY"]); + tui.addChild(new EmptyContent()); + tui.setFocus(editor); + tui.start(); + try { + tui.showOverlay(overlay); + assert.strictEqual(overlay.focused, true); + tui.setFocus(replacement); + tui.setFocus(editor); + terminal.sendInput("x"); + await renderAndFlush(tui, terminal); + assert.deepStrictEqual(overlay.inputs, ["x"]); + assert.deepStrictEqual(editor.inputs, []); + assert.strictEqual(overlay.focused, true); + } finally { + tui.stop(); + } + }); + + it("handleInput restores focus to explicitly focused raw sub-overlay after base focus steal", async () => { + const terminal = new VirtualTerminal(80, 24); + const tui: TUI = new TuiMainScreen(terminal); + const editor = new FocusableOverlay(["EDITOR"]); + const controller = new FocusableOverlay(["CONTROLLER"]); + const subOverlay = new FocusableOverlay(["SUB"]); + tui.addChild(new EmptyContent()); + tui.setFocus(editor); + tui.start(); + try { + tui.showOverlay(controller); + const subHandle = tui.showOverlay(subOverlay, { nonCapturing: true }); + subHandle.focus(); + tui.setFocus(editor); + terminal.sendInput("x"); + await renderAndFlush(tui, terminal); + assert.deepStrictEqual(subOverlay.inputs, ["x"]); + assert.deepStrictEqual(controller.inputs, []); + assert.deepStrictEqual(editor.inputs, []); + } finally { + tui.stop(); + } + }); + + it("passive non-capturing overlay does not regain input after base focus", async () => { + const terminal = new VirtualTerminal(80, 24); + const tui: TUI = new TuiMainScreen(terminal); + const editor = new FocusableOverlay(["EDITOR"]); + const passive = new FocusableOverlay(["PASSIVE"]); + tui.addChild(new EmptyContent()); + tui.setFocus(editor); + tui.start(); + try { + tui.showOverlay(passive, { nonCapturing: true }); + terminal.sendInput("x"); + await renderAndFlush(tui, terminal); + assert.deepStrictEqual(editor.inputs, ["x"]); + assert.deepStrictEqual(passive.inputs, []); + assert.strictEqual(editor.focused, true); + } finally { + tui.stop(); + } + }); + + it("explicitly focused non-capturing overlay regains input after base focus steal", async () => { + const terminal = new VirtualTerminal(80, 24); + const tui: TUI = new TuiMainScreen(terminal); + const editor = new FocusableOverlay(["EDITOR"]); + const overlay = new FocusableOverlay(["NC"]); + tui.addChild(new EmptyContent()); + tui.setFocus(editor); + tui.start(); + try { + const handle = tui.showOverlay(overlay, { nonCapturing: true }); + handle.focus(); + tui.setFocus(editor); + terminal.sendInput("x"); + await renderAndFlush(tui, terminal); + assert.deepStrictEqual(overlay.inputs, ["x"]); + assert.deepStrictEqual(editor.inputs, []); + } finally { + tui.stop(); + } + }); + + it("unfocus() prevents visible overlay from regaining input", async () => { + const terminal = new VirtualTerminal(80, 24); + const tui: TUI = new TuiMainScreen(terminal); + const editor = new FocusableOverlay(["EDITOR"]); + const overlay = new FocusableOverlay(["OVERLAY"]); + tui.addChild(new EmptyContent()); + tui.setFocus(editor); + tui.start(); + try { + const handle = tui.showOverlay(overlay); + handle.unfocus(); + terminal.sendInput("x"); + await renderAndFlush(tui, terminal); + assert.deepStrictEqual(editor.inputs, ["x"]); + assert.deepStrictEqual(overlay.inputs, []); + assert.strictEqual(editor.focused, true); + } finally { + tui.stop(); + } + }); + + it("setFocus(null) explicitly clears visible overlay restore", async () => { + const terminal = new VirtualTerminal(80, 24); + const tui: TUI = new TuiMainScreen(terminal); + const overlay = new FocusableOverlay(["OVERLAY"]); + tui.addChild(new EmptyContent()); + tui.start(); + try { + tui.showOverlay(overlay); + tui.setFocus(null); + terminal.sendInput("x"); + await renderAndFlush(tui, terminal); + assert.deepStrictEqual(overlay.inputs, []); + assert.strictEqual(overlay.focused, false); + } finally { + tui.stop(); + } + }); + + it("blocked replacement setFocus(null) resumes the visible overlay", async () => { + const terminal = new VirtualTerminal(80, 24); + const tui: TUI = new TuiMainScreen(terminal); + const replacement = new FocusableOverlay(["REPLACEMENT"]); + const overlay = new FocusableOverlay(["OVERLAY"]); + replacement.handleInput = (data: string) => { + replacement.inputs.push(data); + if (data === "\r") tui.setFocus(null); + }; + overlay.handleInput = (data: string) => { + overlay.inputs.push(data); + if (data === "b") tui.setFocus(replacement); + }; + tui.addChild(new EmptyContent()); + tui.start(); + try { + tui.showOverlay(overlay); + terminal.sendInput("b"); + await renderAndFlush(tui, terminal); + terminal.sendInput("\r"); + terminal.sendInput("x"); + await renderAndFlush(tui, terminal); + assert.deepStrictEqual(replacement.inputs, ["\r"]); + assert.deepStrictEqual(overlay.inputs, ["b", "x"]); + assert.strictEqual(overlay.focused, true); + } finally { + tui.stop(); + } + }); + + it("temporarily invisible focused overlay falls back without losing restore eligibility", async () => { + const terminal = new VirtualTerminal(80, 24); + const tui: TUI = new TuiMainScreen(terminal); + const editor = new FocusableOverlay(["EDITOR"]); + const overlay = new FocusableOverlay(["OVERLAY"]); + let visible = true; + tui.addChild(new EmptyContent()); + tui.setFocus(editor); + tui.start(); + try { + tui.showOverlay(overlay, { visible: () => visible }); + tui.setFocus(editor); + visible = false; + terminal.sendInput("x"); + await renderAndFlush(tui, terminal); + assert.deepStrictEqual(editor.inputs, ["x"]); + assert.deepStrictEqual(overlay.inputs, []); + visible = true; + terminal.sendInput("y"); + await renderAndFlush(tui, terminal); + assert.deepStrictEqual(editor.inputs, ["x"]); + assert.deepStrictEqual(overlay.inputs, ["y"]); + } finally { + tui.stop(); + } + }); + + it("temporarily invisible focused overlay with null preFocus restores when visible again", async () => { + const terminal = new VirtualTerminal(80, 24); + const tui: TUI = new TuiMainScreen(terminal); + const overlay = new FocusableOverlay(["OVERLAY"]); + let visible = true; + tui.addChild(new EmptyContent()); + tui.start(); + try { + tui.showOverlay(overlay, { visible: () => visible }); + visible = false; + terminal.sendInput("x"); + await renderAndFlush(tui, terminal); + assert.deepStrictEqual(overlay.inputs, []); + visible = true; + terminal.sendInput("y"); + await renderAndFlush(tui, terminal); + assert.deepStrictEqual(overlay.inputs, ["y"]); + } finally { + tui.stop(); + } + }); + + it("cyclic overlay preFocus ancestry does not hang focus changes", async () => { + const terminal = new VirtualTerminal(80, 24); + const tui: TUI = new TuiMainScreen(terminal); + const editor = new FocusableOverlay(["EDITOR"]); + const overlay = new FocusableOverlay(["OVERLAY"]); + tui.addChild(new EmptyContent()); + tui.setFocus(overlay); + tui.start(); + try { + const handle = tui.showOverlay(overlay, { nonCapturing: true }); + handle.focus(); + tui.setFocus(editor); + terminal.sendInput("x"); + await renderAndFlush(tui, terminal); + assert.deepStrictEqual(editor.inputs, ["x"]); + assert.deepStrictEqual(overlay.inputs, []); + } finally { + tui.stop(); + } + }); + + it("handleInput restores the focus-order top overlay after base focus steal", async () => { + const terminal = new VirtualTerminal(80, 24); + const tui: TUI = new TuiMainScreen(terminal); + const editor = new FocusableOverlay(["EDITOR"]); + const lower = new FocusableOverlay(["LOWER"]); + const upper = new FocusableOverlay(["UPPER"]); + tui.addChild(new EmptyContent()); + tui.setFocus(editor); + tui.start(); + try { + const lowerHandle = tui.showOverlay(lower); + tui.showOverlay(upper); + lowerHandle.focus(); + tui.setFocus(editor); + terminal.sendInput("x"); + await renderAndFlush(tui, terminal); + assert.deepStrictEqual(lower.inputs, ["x"]); + assert.deepStrictEqual(upper.inputs, []); + assert.deepStrictEqual(editor.inputs, []); + } finally { + tui.stop(); + } + }); + + it("hideOverlay() does not reassign focus when topmost overlay is non-capturing", async () => { + const terminal = new VirtualTerminal(80, 24); + const tui: TUI = new TuiMainScreen(terminal); + const editor = new FocusableOverlay(["EDITOR"]); + const capturing = new FocusableOverlay(["CAP"]); + const nonCapturing = new FocusableOverlay(["NC"]); + tui.addChild(new EmptyContent()); + tui.setFocus(editor); + tui.start(); + try { + tui.showOverlay(capturing); + tui.showOverlay(nonCapturing, { nonCapturing: true }); + assert.strictEqual(capturing.focused, true); + tui.hideOverlay(); + await renderAndFlush(tui, terminal); + assert.strictEqual(capturing.focused, true); + } finally { + tui.stop(); + } + }); + + it("multiple capturing and non-capturing overlays restore focus through removals", async () => { + const terminal = new VirtualTerminal(80, 24); + const tui: TUI = new TuiMainScreen(terminal); + const editor = new FocusableOverlay(["EDITOR"]); + const c1 = new FocusableOverlay(["C1"]); + const n1 = new FocusableOverlay(["N1"]); + const c2 = new FocusableOverlay(["C2"]); + const n2 = new FocusableOverlay(["N2"]); + tui.addChild(new EmptyContent()); + tui.setFocus(editor); + tui.start(); + try { + const c1Handle = tui.showOverlay(c1); + tui.showOverlay(n1, { nonCapturing: true }); + const c2Handle = tui.showOverlay(c2); + tui.showOverlay(n2, { nonCapturing: true }); + assert.strictEqual(c2.focused, true); + c2Handle.hide(); + await renderAndFlush(tui, terminal); + assert.strictEqual(c1.focused, true); + c1Handle.hide(); + await renderAndFlush(tui, terminal); + assert.strictEqual(editor.focused, true); + } finally { + tui.stop(); + } + }); + + it("capturing overlay unfocus() on topmost capturing overlay falls back to preFocus", async () => { + const terminal = new VirtualTerminal(80, 24); + const tui: TUI = new TuiMainScreen(terminal); + const editor = new FocusableOverlay(["EDITOR"]); + const capturing = new FocusableOverlay(["CAP"]); + tui.addChild(new EmptyContent()); + tui.setFocus(editor); + tui.start(); + try { + const handle = tui.showOverlay(capturing); + assert.strictEqual(capturing.focused, true); + handle.unfocus(); + await renderAndFlush(tui, terminal); + assert.strictEqual(editor.focused, true); + assert.strictEqual(capturing.focused, false); + } finally { + tui.stop(); + } + }); + }); + + describe("no-op guards", () => { + it("focus() on hidden overlay is a no-op", async () => { + const terminal = new VirtualTerminal(80, 24); + const tui: TUI = new TuiMainScreen(terminal); + const editor = new FocusableOverlay(["EDITOR"]); + const overlay = new FocusableOverlay(["OVERLAY"]); + tui.addChild(new EmptyContent()); + tui.setFocus(editor); + tui.start(); + try { + const handle = tui.showOverlay(overlay, { nonCapturing: true }); + handle.setHidden(true); + handle.focus(); + await renderAndFlush(tui, terminal); + assert.strictEqual(editor.focused, true); + assert.strictEqual(handle.isFocused(), false); + } finally { + tui.stop(); + } + }); + + it("focus() after hide() is a no-op", async () => { + const terminal = new VirtualTerminal(80, 24); + const tui: TUI = new TuiMainScreen(terminal); + const editor = new FocusableOverlay(["EDITOR"]); + const overlay = new FocusableOverlay(["OVERLAY"]); + tui.addChild(new EmptyContent()); + tui.setFocus(editor); + tui.start(); + try { + const handle = tui.showOverlay(overlay, { nonCapturing: true }); + handle.hide(); + handle.focus(); + await renderAndFlush(tui, terminal); + assert.strictEqual(editor.focused, true); + assert.strictEqual(handle.isFocused(), false); + } finally { + tui.stop(); + } + }); + + it("unfocus() when overlay does not have focus is a no-op", async () => { + const terminal = new VirtualTerminal(80, 24); + const tui: TUI = new TuiMainScreen(terminal); + const editor = new FocusableOverlay(["EDITOR"]); + const overlay = new FocusableOverlay(["OVERLAY"]); + tui.addChild(new EmptyContent()); + tui.setFocus(editor); + tui.start(); + try { + const handle = tui.showOverlay(overlay, { nonCapturing: true }); + handle.unfocus(); + await renderAndFlush(tui, terminal); + assert.strictEqual(editor.focused, true); + assert.strictEqual(overlay.focused, false); + } finally { + tui.stop(); + } + }); + + it("unfocus() with null preFocus clears focus and does not route input back to overlay", async () => { + const terminal = new VirtualTerminal(80, 24); + const tui: TUI = new TuiMainScreen(terminal); + const overlay = new FocusableOverlay(["OVERLAY"]); + tui.addChild(new EmptyContent()); + tui.start(); + try { + const handle = tui.showOverlay(overlay); + assert.strictEqual(overlay.focused, true); + handle.unfocus(); + assert.strictEqual(overlay.focused, false); + terminal.sendInput("x"); + await renderAndFlush(tui, terminal); + assert.deepStrictEqual(overlay.inputs, []); + assert.strictEqual(handle.isFocused(), false); + } finally { + tui.stop(); + } + }); + }); + + describe("focus cycle prevention", () => { + it("toggle focus between non-capturing overlays then unfocus returns to editor", async () => { + const terminal = new VirtualTerminal(80, 24); + const tui: TUI = new TuiMainScreen(terminal); + const editor = new FocusableOverlay(["EDITOR"]); + const a = new FocusableOverlay(["A"]); + const b = new FocusableOverlay(["B"]); + tui.addChild(new EmptyContent()); + tui.setFocus(editor); + tui.start(); + try { + const aHandle = tui.showOverlay(a, { nonCapturing: true }); + const bHandle = tui.showOverlay(b, { nonCapturing: true }); + aHandle.focus(); + bHandle.focus(); + aHandle.focus(); + aHandle.unfocus(); + await renderAndFlush(tui, terminal); + assert.strictEqual(editor.focused, true); + assert.strictEqual(a.focused, false); + assert.strictEqual(b.focused, false); + } finally { + tui.stop(); + } + }); + + it("explicit unfocus target supports cycling between three overlays and editor", async () => { + const terminal = new VirtualTerminal(80, 24); + const tui: TUI = new TuiMainScreen(terminal); + const editor = new FocusableOverlay(["EDITOR"]); + const a = new FocusableOverlay(["A"]); + const b = new FocusableOverlay(["B"]); + const c = new FocusableOverlay(["C"]); + tui.addChild(new EmptyContent()); + tui.setFocus(editor); + tui.start(); + try { + const aHandle = tui.showOverlay(a); + const bHandle = tui.showOverlay(b); + const cHandle = tui.showOverlay(c); + + aHandle.focus(); + terminal.sendInput("a"); + await renderAndFlush(tui, terminal); + bHandle.focus(); + terminal.sendInput("b"); + await renderAndFlush(tui, terminal); + cHandle.focus(); + terminal.sendInput("c"); + await renderAndFlush(tui, terminal); + cHandle.unfocus({ target: editor }); + terminal.sendInput("e"); + await renderAndFlush(tui, terminal); + aHandle.focus(); + terminal.sendInput("A"); + await renderAndFlush(tui, terminal); + aHandle.unfocus({ target: editor }); + terminal.sendInput("E"); + await renderAndFlush(tui, terminal); + + assert.deepStrictEqual(a.inputs, ["a", "A"]); + assert.deepStrictEqual(b.inputs, ["b"]); + assert.deepStrictEqual(c.inputs, ["c"]); + assert.deepStrictEqual(editor.inputs, ["e", "E"]); + assert.strictEqual(editor.focused, true); + } finally { + tui.stop(); + } + }); + + it("explicit null unfocus target clears focus without restoring overlays", async () => { + const terminal = new VirtualTerminal(80, 24); + const tui: TUI = new TuiMainScreen(terminal); + const overlay = new FocusableOverlay(["OVERLAY"]); + tui.addChild(new EmptyContent()); + tui.start(); + try { + const handle = tui.showOverlay(overlay); + handle.unfocus({ target: null }); + terminal.sendInput("x"); + await renderAndFlush(tui, terminal); + assert.deepStrictEqual(overlay.inputs, []); + assert.strictEqual(handle.isFocused(), false); + } finally { + tui.stop(); + } + }); + + it("hiding focused overlay falls back to next visual-frontmost overlay", async () => { + const terminal = new VirtualTerminal(80, 24); + const tui: TUI = new TuiMainScreen(terminal); + const editor = new FocusableOverlay(["EDITOR"]); + const a = new FocusableOverlay(["A"]); + const b = new FocusableOverlay(["B"]); + const c = new FocusableOverlay(["C"]); + tui.addChild(new EmptyContent()); + tui.setFocus(editor); + tui.start(); + try { + const aHandle = tui.showOverlay(a); + const bHandle = tui.showOverlay(b); + tui.showOverlay(c); + aHandle.focus(); + bHandle.focus(); + bHandle.setHidden(true); + terminal.sendInput("x"); + await renderAndFlush(tui, terminal); + assert.deepStrictEqual(a.inputs, ["x"]); + assert.deepStrictEqual(c.inputs, []); + assert.strictEqual(a.focused, true); + } finally { + tui.stop(); + } + }); + }); + + describe("rendering order", () => { + it("focus() on already-focused overlay bumps visual order", async () => { + const terminal = new VirtualTerminal(20, 6); + const tui: TUI = new TuiMainScreen(terminal); + const editor = new FocusableOverlay(["EDITOR"]); + tui.addChild(new EmptyContent()); + tui.setFocus(editor); + tui.start(); + try { + const aHandle = tui.showOverlay(new StaticOverlay(["A"]), { row: 0, col: 0, width: 1, nonCapturing: true }); + tui.showOverlay(new StaticOverlay(["B"]), { row: 0, col: 0, width: 1, nonCapturing: true }); + aHandle.focus(); + tui.showOverlay(new StaticOverlay(["C"]), { row: 0, col: 0, width: 1, nonCapturing: true }); + await renderAndFlush(tui, terminal); + assert.strictEqual(terminal.getViewport()[0]?.charAt(0), "C"); + aHandle.focus(); + await renderAndFlush(tui, terminal); + assert.strictEqual(terminal.getViewport()[0]?.charAt(0), "A"); + assert.strictEqual(aHandle.isFocused(), true); + } finally { + tui.stop(); + } + }); + + it("default rendering order for overlapping overlays follows creation order", async () => { + const terminal = new VirtualTerminal(20, 6); + const tui: TUI = new TuiMainScreen(terminal); + tui.addChild(new EmptyContent()); + tui.start(); + try { + tui.showOverlay(new StaticOverlay(["A"]), { row: 0, col: 0, width: 1, nonCapturing: true }); + tui.showOverlay(new StaticOverlay(["B"]), { row: 0, col: 0, width: 1, nonCapturing: true }); + await renderAndFlush(tui, terminal); + assert.strictEqual(terminal.getViewport()[0]?.charAt(0), "B"); + } finally { + tui.stop(); + } + }); + + it("focus() on lower overlay renders it on top", async () => { + const terminal = new VirtualTerminal(20, 6); + const tui: TUI = new TuiMainScreen(terminal); + tui.addChild(new EmptyContent()); + tui.start(); + try { + const lower = tui.showOverlay(new StaticOverlay(["A"]), { row: 0, col: 0, width: 1, nonCapturing: true }); + tui.showOverlay(new StaticOverlay(["B"]), { row: 0, col: 0, width: 1, nonCapturing: true }); + await renderAndFlush(tui, terminal); + assert.strictEqual(terminal.getViewport()[0]?.charAt(0), "B"); + lower.focus(); + await renderAndFlush(tui, terminal); + assert.strictEqual(terminal.getViewport()[0]?.charAt(0), "A"); + } finally { + tui.stop(); + } + }); + + it("focusing middle overlay places it on top while preserving others relative order", async () => { + const terminal = new VirtualTerminal(20, 6); + const tui: TUI = new TuiMainScreen(terminal); + tui.addChild(new EmptyContent()); + tui.start(); + try { + tui.showOverlay(new StaticOverlay(["A"]), { row: 0, col: 0, width: 1, nonCapturing: true }); + const middle = tui.showOverlay(new StaticOverlay(["B"]), { row: 0, col: 0, width: 1, nonCapturing: true }); + const top = tui.showOverlay(new StaticOverlay(["C"]), { row: 0, col: 0, width: 1, nonCapturing: true }); + await renderAndFlush(tui, terminal); + assert.strictEqual(terminal.getViewport()[0]?.charAt(0), "C"); + middle.focus(); + await renderAndFlush(tui, terminal); + assert.strictEqual(terminal.getViewport()[0]?.charAt(0), "B"); + middle.hide(); + await renderAndFlush(tui, terminal); + assert.strictEqual(terminal.getViewport()[0]?.charAt(0), "C"); + top.hide(); + await renderAndFlush(tui, terminal); + assert.strictEqual(terminal.getViewport()[0]?.charAt(0), "A"); + } finally { + tui.stop(); + } + }); + + it("capturing overlay hidden and shown again renders on top after unhide", async () => { + const terminal = new VirtualTerminal(20, 6); + const tui: TUI = new TuiMainScreen(terminal); + tui.addChild(new EmptyContent()); + tui.start(); + try { + tui.showOverlay(new StaticOverlay(["A"]), { row: 0, col: 0, width: 1, nonCapturing: true }); + const capturing = tui.showOverlay(new StaticOverlay(["B"]), { row: 0, col: 0, width: 1 }); + await renderAndFlush(tui, terminal); + assert.strictEqual(terminal.getViewport()[0]?.charAt(0), "B"); + capturing.setHidden(true); + tui.showOverlay(new StaticOverlay(["C"]), { row: 0, col: 0, width: 1, nonCapturing: true }); + await renderAndFlush(tui, terminal); + assert.strictEqual(terminal.getViewport()[0]?.charAt(0), "C"); + capturing.setHidden(false); + await renderAndFlush(tui, terminal); + assert.strictEqual(terminal.getViewport()[0]?.charAt(0), "B"); + } finally { + tui.stop(); + } + }); + + it("unfocus() does not change visual order until another overlay is focused", async () => { + const terminal = new VirtualTerminal(20, 6); + const tui: TUI = new TuiMainScreen(terminal); + const editor = new FocusableOverlay(["EDITOR"]); + tui.addChild(new EmptyContent()); + tui.setFocus(editor); + tui.start(); + try { + const a = tui.showOverlay(new StaticOverlay(["A"]), { row: 0, col: 0, width: 1, nonCapturing: true }); + const b = tui.showOverlay(new StaticOverlay(["B"]), { row: 0, col: 0, width: 1, nonCapturing: true }); + await renderAndFlush(tui, terminal); + assert.strictEqual(terminal.getViewport()[0]?.charAt(0), "B"); + a.focus(); + await renderAndFlush(tui, terminal); + assert.strictEqual(terminal.getViewport()[0]?.charAt(0), "A"); + a.unfocus(); + await renderAndFlush(tui, terminal); + assert.strictEqual(terminal.getViewport()[0]?.charAt(0), "A"); + b.focus(); + await renderAndFlush(tui, terminal); + assert.strictEqual(terminal.getViewport()[0]?.charAt(0), "B"); + } finally { + tui.stop(); + } + }); + }); +}); diff --git a/packages/tui/test/overlay-options.test.ts b/packages/tui/test/overlay-options.test.ts new file mode 100644 index 0000000000000000000000000000000000000000..db50f5db9f10b3393f72940ca25274cdfb2716f8 --- /dev/null +++ b/packages/tui/test/overlay-options.test.ts @@ -0,0 +1,541 @@ +import assert from "node:assert"; +import { describe, it } from "node:test"; +import type { Component, TUI } from "../src/tui.ts"; +import { TuiMainScreen } from "../src/tui-main-screen.ts"; +import { VirtualTerminal } from "./virtual-terminal.ts"; + +class StaticOverlay implements Component { + private lines: string[]; + requestedWidth?: number; + + constructor(lines: string[], requestedWidth?: number) { + this.lines = lines; + this.requestedWidth = requestedWidth; + } + + render(width: number): string[] { + // Store the width we were asked to render at for verification + this.requestedWidth = width; + return this.lines; + } + + invalidate(): void {} +} + +class EmptyContent implements Component { + render(): string[] { + return []; + } + invalidate(): void {} +} + +async function renderAndFlush(tui: TUI, terminal: VirtualTerminal): Promise { + tui.requestRender(true); + await new Promise((resolve) => process.nextTick(resolve)); + await terminal.waitForRender(); +} + +describe("TUI overlay options", () => { + describe("width overflow protection", () => { + it("should truncate overlay lines that exceed declared width", async () => { + const terminal = new VirtualTerminal(80, 24); + const tui: TUI = new TuiMainScreen(terminal); + // Overlay declares width 20 but renders lines much wider + const overlay = new StaticOverlay(["X".repeat(100)]); + + tui.addChild(new EmptyContent()); + tui.showOverlay(overlay, { width: 20 }); + tui.start(); + await renderAndFlush(tui, terminal); + + // Should not crash, and no line should exceed terminal width + const viewport = terminal.getViewport(); + for (const line of viewport) { + // visibleWidth not available here, but line length is a rough check + // The important thing is it didn't crash + assert.ok(line !== undefined); + } + tui.stop(); + }); + + it("should handle overlay with complex ANSI sequences without crashing", async () => { + const terminal = new VirtualTerminal(80, 24); + const tui: TUI = new TuiMainScreen(terminal); + // Simulate complex ANSI content like the crash log showed + const complexLine = + "\x1b[48;2;40;50;40m \x1b[38;2;128;128;128mSome styled content\x1b[39m\x1b[49m" + + "\x1b]8;;http://example.com\x07link\x1b]8;;\x07" + + " more content ".repeat(10); + const overlay = new StaticOverlay([complexLine, complexLine, complexLine]); + + tui.addChild(new EmptyContent()); + tui.showOverlay(overlay, { width: 60 }); + tui.start(); + await renderAndFlush(tui, terminal); + + // Should not crash + const viewport = terminal.getViewport(); + assert.ok(viewport.length > 0); + tui.stop(); + }); + + it("should handle overlay composited on styled base content", async () => { + const terminal = new VirtualTerminal(80, 24); + const tui: TUI = new TuiMainScreen(terminal); + + // Base content with styling + class StyledContent implements Component { + render(width: number): string[] { + const styledLine = `\x1b[1m\x1b[38;2;255;0;0m${"X".repeat(width)}\x1b[0m`; + return [styledLine, styledLine, styledLine]; + } + invalidate(): void {} + } + + const overlay = new StaticOverlay(["OVERLAY"]); + + tui.addChild(new StyledContent()); + tui.showOverlay(overlay, { width: 20, anchor: "center" }); + tui.start(); + await renderAndFlush(tui, terminal); + + // Should not crash and overlay should be visible + const viewport = terminal.getViewport(); + const hasOverlay = viewport.some((line) => line?.includes("OVERLAY")); + assert.ok(hasOverlay, "Overlay should be visible"); + tui.stop(); + }); + + it("should handle wide characters at overlay boundary", async () => { + const terminal = new VirtualTerminal(80, 24); + const tui: TUI = new TuiMainScreen(terminal); + // Wide chars (each takes 2 columns) at the edge of declared width + const wideCharLine = "中文日本語한글テスト漢字"; // Mix of CJK chars + const overlay = new StaticOverlay([wideCharLine]); + + tui.addChild(new EmptyContent()); + tui.showOverlay(overlay, { width: 15 }); // Odd width to potentially hit boundary + tui.start(); + await renderAndFlush(tui, terminal); + + // Should not crash + const viewport = terminal.getViewport(); + assert.ok(viewport.length > 0); + tui.stop(); + }); + + it("should handle overlay positioned at terminal edge", async () => { + const terminal = new VirtualTerminal(80, 24); + const tui: TUI = new TuiMainScreen(terminal); + // Overlay positioned at right edge with content that exceeds declared width + const overlay = new StaticOverlay(["X".repeat(50)]); + + tui.addChild(new EmptyContent()); + // Position at col 60 with width 20 - should fit exactly at right edge + tui.showOverlay(overlay, { col: 60, width: 20 }); + tui.start(); + await renderAndFlush(tui, terminal); + + // Should not crash + const viewport = terminal.getViewport(); + assert.ok(viewport.length > 0); + tui.stop(); + }); + + it("should handle overlay on base content with OSC sequences", async () => { + const terminal = new VirtualTerminal(80, 24); + const tui: TUI = new TuiMainScreen(terminal); + + // Base content with OSC 8 hyperlinks (like file paths in agent output) + class HyperlinkContent implements Component { + render(width: number): string[] { + const link = `\x1b]8;;file:///path/to/file.ts\x07file.ts\x1b]8;;\x07`; + const line = `See ${link} for details ${"X".repeat(width - 30)}`; + return [line, line, line]; + } + invalidate(): void {} + } + + const overlay = new StaticOverlay(["OVERLAY-TEXT"]); + + tui.addChild(new HyperlinkContent()); + tui.showOverlay(overlay, { anchor: "center", width: 20 }); + tui.start(); + await renderAndFlush(tui, terminal); + + // Should not crash - this was the original bug scenario + const viewport = terminal.getViewport(); + assert.ok(viewport.length > 0); + tui.stop(); + }); + }); + + describe("width percentage", () => { + it("should render overlay at percentage of terminal width", async () => { + const terminal = new VirtualTerminal(100, 24); + const tui: TUI = new TuiMainScreen(terminal); + const overlay = new StaticOverlay(["test"]); + + tui.addChild(new EmptyContent()); + tui.showOverlay(overlay, { width: "50%" }); + tui.start(); + await renderAndFlush(tui, terminal); + + assert.strictEqual(overlay.requestedWidth, 50); + tui.stop(); + }); + + it("should respect minWidth when widthPercent results in smaller width", async () => { + const terminal = new VirtualTerminal(100, 24); + const tui: TUI = new TuiMainScreen(terminal); + const overlay = new StaticOverlay(["test"]); + + tui.addChild(new EmptyContent()); + tui.showOverlay(overlay, { width: "10%", minWidth: 30 }); + tui.start(); + await renderAndFlush(tui, terminal); + + assert.strictEqual(overlay.requestedWidth, 30); + tui.stop(); + }); + }); + + describe("anchor positioning", () => { + it("should position overlay at top-left", async () => { + const terminal = new VirtualTerminal(80, 24); + const tui: TUI = new TuiMainScreen(terminal); + const overlay = new StaticOverlay(["TOP-LEFT"]); + + tui.addChild(new EmptyContent()); + tui.showOverlay(overlay, { anchor: "top-left", width: 10 }); + tui.start(); + await renderAndFlush(tui, terminal); + + const viewport = terminal.getViewport(); + assert.ok(viewport[0]?.startsWith("TOP-LEFT"), `Expected TOP-LEFT at start, got: ${viewport[0]}`); + tui.stop(); + }); + + it("should position overlay at bottom-right", async () => { + const terminal = new VirtualTerminal(80, 24); + const tui: TUI = new TuiMainScreen(terminal); + const overlay = new StaticOverlay(["BTM-RIGHT"]); + + tui.addChild(new EmptyContent()); + tui.showOverlay(overlay, { anchor: "bottom-right", width: 10 }); + tui.start(); + await renderAndFlush(tui, terminal); + + const viewport = terminal.getViewport(); + // Should be on last row, ending at last column + const lastRow = viewport[23]; + assert.ok(lastRow?.includes("BTM-RIGHT"), `Expected BTM-RIGHT on last row, got: ${lastRow}`); + assert.ok(lastRow?.trimEnd().endsWith("BTM-RIGHT"), `Expected BTM-RIGHT at end, got: ${lastRow}`); + tui.stop(); + }); + + it("should position overlay at top-center", async () => { + const terminal = new VirtualTerminal(80, 24); + const tui: TUI = new TuiMainScreen(terminal); + const overlay = new StaticOverlay(["CENTERED"]); + + tui.addChild(new EmptyContent()); + tui.showOverlay(overlay, { anchor: "top-center", width: 10 }); + tui.start(); + await renderAndFlush(tui, terminal); + + const viewport = terminal.getViewport(); + // Should be on first row, centered horizontally + const firstRow = viewport[0]; + assert.ok(firstRow?.includes("CENTERED"), `Expected CENTERED on first row, got: ${firstRow}`); + // Check it's roughly centered (col 35 for width 10 in 80 col terminal) + const colIndex = firstRow?.indexOf("CENTERED") ?? -1; + assert.ok(colIndex >= 30 && colIndex <= 40, `Expected centered, got col ${colIndex}`); + tui.stop(); + }); + }); + + describe("margin", () => { + it("should clamp negative margins to zero", async () => { + const terminal = new VirtualTerminal(80, 24); + const tui: TUI = new TuiMainScreen(terminal); + const overlay = new StaticOverlay(["NEG-MARGIN"]); + + tui.addChild(new EmptyContent()); + // Negative margins should be treated as 0 + tui.showOverlay(overlay, { + anchor: "top-left", + width: 12, + margin: { top: -5, left: -10, right: 0, bottom: 0 }, + }); + tui.start(); + await renderAndFlush(tui, terminal); + + const viewport = terminal.getViewport(); + // Should be at row 0, col 0 (negative margins clamped to 0) + assert.ok(viewport[0]?.startsWith("NEG-MARGIN"), `Expected NEG-MARGIN at start of row 0, got: ${viewport[0]}`); + tui.stop(); + }); + + it("should respect margin as number", async () => { + const terminal = new VirtualTerminal(80, 24); + const tui: TUI = new TuiMainScreen(terminal); + const overlay = new StaticOverlay(["MARGIN"]); + + tui.addChild(new EmptyContent()); + tui.showOverlay(overlay, { anchor: "top-left", width: 10, margin: 5 }); + tui.start(); + await renderAndFlush(tui, terminal); + + const viewport = terminal.getViewport(); + // Should be on row 5 (not 0) due to margin + assert.ok(!viewport[0]?.includes("MARGIN"), "Should not be on row 0"); + assert.ok(!viewport[4]?.includes("MARGIN"), "Should not be on row 4"); + assert.ok(viewport[5]?.includes("MARGIN"), `Expected MARGIN on row 5, got: ${viewport[5]}`); + // Should start at col 5 (not 0) + const colIndex = viewport[5]?.indexOf("MARGIN") ?? -1; + assert.strictEqual(colIndex, 5, `Expected col 5, got ${colIndex}`); + tui.stop(); + }); + + it("should respect margin object", async () => { + const terminal = new VirtualTerminal(80, 24); + const tui: TUI = new TuiMainScreen(terminal); + const overlay = new StaticOverlay(["MARGIN"]); + + tui.addChild(new EmptyContent()); + tui.showOverlay(overlay, { + anchor: "top-left", + width: 10, + margin: { top: 2, left: 3, right: 0, bottom: 0 }, + }); + tui.start(); + await renderAndFlush(tui, terminal); + + const viewport = terminal.getViewport(); + assert.ok(viewport[2]?.includes("MARGIN"), `Expected MARGIN on row 2, got: ${viewport[2]}`); + const colIndex = viewport[2]?.indexOf("MARGIN") ?? -1; + assert.strictEqual(colIndex, 3, `Expected col 3, got ${colIndex}`); + tui.stop(); + }); + }); + + describe("offset", () => { + it("should apply offsetX and offsetY from anchor position", async () => { + const terminal = new VirtualTerminal(80, 24); + const tui: TUI = new TuiMainScreen(terminal); + const overlay = new StaticOverlay(["OFFSET"]); + + tui.addChild(new EmptyContent()); + tui.showOverlay(overlay, { anchor: "top-left", width: 10, offsetX: 10, offsetY: 5 }); + tui.start(); + await renderAndFlush(tui, terminal); + + const viewport = terminal.getViewport(); + assert.ok(viewport[5]?.includes("OFFSET"), `Expected OFFSET on row 5, got: ${viewport[5]}`); + const colIndex = viewport[5]?.indexOf("OFFSET") ?? -1; + assert.strictEqual(colIndex, 10, `Expected col 10, got ${colIndex}`); + tui.stop(); + }); + }); + + describe("percentage positioning", () => { + it("should position with rowPercent and colPercent", async () => { + const terminal = new VirtualTerminal(80, 24); + const tui: TUI = new TuiMainScreen(terminal); + const overlay = new StaticOverlay(["PCT"]); + + tui.addChild(new EmptyContent()); + // 50% should center both ways + tui.showOverlay(overlay, { width: 10, row: "50%", col: "50%" }); + tui.start(); + await renderAndFlush(tui, terminal); + + const viewport = terminal.getViewport(); + // Find the row with PCT + let foundRow = -1; + for (let i = 0; i < viewport.length; i++) { + if (viewport[i]?.includes("PCT")) { + foundRow = i; + break; + } + } + // Should be roughly centered vertically (row ~11-12 for 24 row terminal) + assert.ok(foundRow >= 10 && foundRow <= 13, `Expected centered row, got ${foundRow}`); + tui.stop(); + }); + + it("rowPercent 0 should position at top", async () => { + const terminal = new VirtualTerminal(80, 24); + const tui: TUI = new TuiMainScreen(terminal); + const overlay = new StaticOverlay(["TOP"]); + + tui.addChild(new EmptyContent()); + tui.showOverlay(overlay, { width: 10, row: "0%" }); + tui.start(); + await renderAndFlush(tui, terminal); + + const viewport = terminal.getViewport(); + assert.ok(viewport[0]?.includes("TOP"), `Expected TOP on row 0, got: ${viewport[0]}`); + tui.stop(); + }); + + it("rowPercent 100 should position at bottom", async () => { + const terminal = new VirtualTerminal(80, 24); + const tui: TUI = new TuiMainScreen(terminal); + const overlay = new StaticOverlay(["BOTTOM"]); + + tui.addChild(new EmptyContent()); + tui.showOverlay(overlay, { width: 10, row: "100%" }); + tui.start(); + await renderAndFlush(tui, terminal); + + const viewport = terminal.getViewport(); + assert.ok(viewport[23]?.includes("BOTTOM"), `Expected BOTTOM on last row, got: ${viewport[23]}`); + tui.stop(); + }); + }); + + describe("maxHeight", () => { + it("should truncate overlay to maxHeight", async () => { + const terminal = new VirtualTerminal(80, 24); + const tui: TUI = new TuiMainScreen(terminal); + const overlay = new StaticOverlay(["Line 1", "Line 2", "Line 3", "Line 4", "Line 5"]); + + tui.addChild(new EmptyContent()); + tui.showOverlay(overlay, { maxHeight: 3 }); + tui.start(); + await renderAndFlush(tui, terminal); + + const viewport = terminal.getViewport(); + const content = viewport.join("\n"); + assert.ok(content.includes("Line 1"), "Should include Line 1"); + assert.ok(content.includes("Line 2"), "Should include Line 2"); + assert.ok(content.includes("Line 3"), "Should include Line 3"); + assert.ok(!content.includes("Line 4"), "Should NOT include Line 4"); + assert.ok(!content.includes("Line 5"), "Should NOT include Line 5"); + tui.stop(); + }); + + it("should truncate overlay to maxHeightPercent", async () => { + const terminal = new VirtualTerminal(80, 10); + const tui: TUI = new TuiMainScreen(terminal); + // 10 lines in a 10 row terminal with 50% maxHeight should show 5 lines + const overlay = new StaticOverlay(["L1", "L2", "L3", "L4", "L5", "L6", "L7", "L8", "L9", "L10"]); + + tui.addChild(new EmptyContent()); + tui.showOverlay(overlay, { maxHeight: "50%" }); + tui.start(); + await renderAndFlush(tui, terminal); + + const viewport = terminal.getViewport(); + const content = viewport.join("\n"); + assert.ok(content.includes("L1"), "Should include L1"); + assert.ok(content.includes("L5"), "Should include L5"); + assert.ok(!content.includes("L6"), "Should NOT include L6"); + tui.stop(); + }); + }); + + describe("absolute positioning", () => { + it("row and col should override anchor", async () => { + const terminal = new VirtualTerminal(80, 24); + const tui: TUI = new TuiMainScreen(terminal); + const overlay = new StaticOverlay(["ABSOLUTE"]); + + tui.addChild(new EmptyContent()); + // Even with bottom-right anchor, row/col should win + tui.showOverlay(overlay, { anchor: "bottom-right", row: 3, col: 5, width: 10 }); + tui.start(); + await renderAndFlush(tui, terminal); + + const viewport = terminal.getViewport(); + assert.ok(viewport[3]?.includes("ABSOLUTE"), `Expected ABSOLUTE on row 3, got: ${viewport[3]}`); + const colIndex = viewport[3]?.indexOf("ABSOLUTE") ?? -1; + assert.strictEqual(colIndex, 5, `Expected col 5, got ${colIndex}`); + tui.stop(); + }); + }); + + describe("stacked overlays", () => { + it("should render multiple overlays with later ones on top", async () => { + const terminal = new VirtualTerminal(80, 24); + const tui: TUI = new TuiMainScreen(terminal); + + tui.addChild(new EmptyContent()); + + // First overlay at top-left + const overlay1 = new StaticOverlay(["FIRST-OVERLAY"]); + tui.showOverlay(overlay1, { anchor: "top-left", width: 20 }); + + // Second overlay at top-left (should cover part of first) + const overlay2 = new StaticOverlay(["SECOND"]); + tui.showOverlay(overlay2, { anchor: "top-left", width: 10 }); + + tui.start(); + await renderAndFlush(tui, terminal); + + const viewport = terminal.getViewport(); + // Second overlay should be visible (on top) + assert.ok(viewport[0]?.includes("SECOND"), `Expected SECOND on row 0, got: ${viewport[0]}`); + // Part of first overlay might still be visible after SECOND + // FIRST-OVERLAY is 13 chars, SECOND is 6 chars, so "OVERLAY" part might show + tui.stop(); + }); + + it("should handle overlays at different positions without interference", async () => { + const terminal = new VirtualTerminal(80, 24); + const tui: TUI = new TuiMainScreen(terminal); + + tui.addChild(new EmptyContent()); + + // Overlay at top-left + const overlay1 = new StaticOverlay(["TOP-LEFT"]); + tui.showOverlay(overlay1, { anchor: "top-left", width: 15 }); + + // Overlay at bottom-right + const overlay2 = new StaticOverlay(["BTM-RIGHT"]); + tui.showOverlay(overlay2, { anchor: "bottom-right", width: 15 }); + + tui.start(); + await renderAndFlush(tui, terminal); + + const viewport = terminal.getViewport(); + // Both should be visible + assert.ok(viewport[0]?.includes("TOP-LEFT"), `Expected TOP-LEFT on row 0, got: ${viewport[0]}`); + assert.ok(viewport[23]?.includes("BTM-RIGHT"), `Expected BTM-RIGHT on row 23, got: ${viewport[23]}`); + tui.stop(); + }); + + it("should properly hide overlays in stack order", async () => { + const terminal = new VirtualTerminal(80, 24); + const tui: TUI = new TuiMainScreen(terminal); + + tui.addChild(new EmptyContent()); + + // Show two overlays + const overlay1 = new StaticOverlay(["FIRST"]); + tui.showOverlay(overlay1, { anchor: "top-left", width: 10 }); + + const overlay2 = new StaticOverlay(["SECOND"]); + tui.showOverlay(overlay2, { anchor: "top-left", width: 10 }); + + tui.start(); + await renderAndFlush(tui, terminal); + + // Second should be visible + let viewport = terminal.getViewport(); + assert.ok(viewport[0]?.includes("SECOND"), "SECOND should be visible initially"); + + // Hide top overlay + tui.hideOverlay(); + await renderAndFlush(tui, terminal); + + // First should now be visible + viewport = terminal.getViewport(); + assert.ok(viewport[0]?.includes("FIRST"), "FIRST should be visible after hiding SECOND"); + + tui.stop(); + }); + }); +}); diff --git a/packages/tui/test/overlay-short-content.test.ts b/packages/tui/test/overlay-short-content.test.ts new file mode 100644 index 0000000000000000000000000000000000000000..3a3b423af170144be134951f91ba9a3bb185e877 --- /dev/null +++ b/packages/tui/test/overlay-short-content.test.ts @@ -0,0 +1,62 @@ +import assert from "node:assert"; +import { describe, it } from "node:test"; +import type { Component, TUI } from "../src/tui.ts"; +import { TuiMainScreen } from "../src/tui-main-screen.ts"; +import { VirtualTerminal } from "./virtual-terminal.ts"; + +class SimpleContent implements Component { + private lines: string[]; + + constructor(lines: string[]) { + this.lines = lines; + } + + render(): string[] { + return this.lines; + } + invalidate() {} +} + +class SimpleOverlay implements Component { + render(): string[] { + return ["OVERLAY_TOP", "OVERLAY_MID", "OVERLAY_BOT"]; + } + invalidate() {} +} + +describe("TUI overlay with short content", () => { + it("should render overlay when content is shorter than terminal height", async () => { + // Terminal has 24 rows, but content only has 3 lines + const terminal = new VirtualTerminal(80, 24); + const tui: TUI = new TuiMainScreen(terminal); + + // Only 3 lines of content + tui.addChild(new SimpleContent(["Line 1", "Line 2", "Line 3"])); + + // Show overlay centered - should be around row 10 in a 24-row terminal + const overlay = new SimpleOverlay(); + tui.showOverlay(overlay); + + // Trigger render + tui.start(); + await terminal.waitForRender(); + + const viewport = terminal.getViewport(); + const hasOverlay = viewport.some((line) => line.includes("OVERLAY")); + + console.log("Terminal rows:", terminal.rows); + console.log("Content lines: 3"); + console.log("Overlay visible:", hasOverlay); + + if (!hasOverlay) { + console.log("\nViewport contents:"); + for (let i = 0; i < viewport.length; i++) { + console.log(` [${i}]: "${viewport[i]}"`); + } + } + + assert.ok(hasOverlay, "Overlay should be visible when content is shorter than terminal"); + + tui.stop(); + }); +}); diff --git a/packages/tui/test/regression-overlay-cjk-boundary.test.ts b/packages/tui/test/regression-overlay-cjk-boundary.test.ts new file mode 100644 index 0000000000000000000000000000000000000000..5aa7f5c1608c0fbd9a27958896730f36b4f794bd --- /dev/null +++ b/packages/tui/test/regression-overlay-cjk-boundary.test.ts @@ -0,0 +1,46 @@ +import assert from "node:assert"; +import { describe, it } from "node:test"; +import { compositeTuiLine } from "../src/tui.ts"; +import { extractSegments, sliceByColumn, visibleWidth } from "../src/utils.ts"; + +describe("overlay CJK boundary regression", () => { + it("excludes a wide grapheme from before when overlay starts inside it", () => { + const segments = extractSegments("abcd让EFGH", 5, 9, 11, true); + + assert.strictEqual(segments.before, "abcd"); + assert.strictEqual(segments.beforeWidth, 4); + assert.strictEqual(visibleWidth(segments.before), segments.beforeWidth); + assert.strictEqual(segments.after, "H"); + assert.strictEqual(segments.afterWidth, 1); + }); + + it("keeps ASCII before-segment behavior at the same boundary", () => { + const segments = extractSegments("abcdG EFGH", 5, 9, 11, true); + + assert.strictEqual(segments.before, "abcdG"); + assert.strictEqual(segments.beforeWidth, 5); + assert.strictEqual(visibleWidth(segments.before), segments.beforeWidth); + }); + + it("composites an overlay at the requested column when it starts inside a wide grapheme", () => { + const out = compositeTuiLine("abcd让EFGH", "│XX│", 5, 4, 20); + const prefix = sliceByColumn(out, 0, 5, true); + const overlay = sliceByColumn(out, 5, 4, true); + + assert.strictEqual(out.includes("让"), false); + assert.strictEqual(visibleWidth(out), 20); + assert.strictEqual(visibleWidth(prefix), 5); + assert.strictEqual(visibleWidth(overlay), 4); + assert.strictEqual(overlay.includes("│XX│"), true); + }); + + it("composites an overlay when it starts at a wide grapheme boundary", () => { + const out = compositeTuiLine("abcd让EFGH", "│XX│", 4, 4, 20); + const overlay = sliceByColumn(out, 4, 4, true); + + assert.strictEqual(out.includes("让"), false); + assert.strictEqual(visibleWidth(out), 20); + assert.strictEqual(visibleWidth(overlay), 4); + assert.strictEqual(overlay.includes("│XX│"), true); + }); +}); diff --git a/packages/tui/test/regression-regional-indicator-width.test.ts b/packages/tui/test/regression-regional-indicator-width.test.ts new file mode 100644 index 0000000000000000000000000000000000000000..6803e2b1a7da8a9b45cc0df004178b561c54a7b1 --- /dev/null +++ b/packages/tui/test/regression-regional-indicator-width.test.ts @@ -0,0 +1,52 @@ +import assert from "node:assert"; +import { describe, it } from "node:test"; +import { visibleWidth, wrapTextWithAnsi } from "../src/utils.ts"; + +describe("regional indicator width regression", () => { + it("treats partial flag grapheme as full-width to avoid streaming render drift", () => { + // Repro context: + // During streaming, "🇨🇳" often appears as an intermediate "🇨" first. + // If "🇨" is measured as width 1 while terminal renders it as width 2, + // differential rendering can drift and leave stale characters on screen. + const partialFlag = "🇨"; + const listLine = " - 🇨"; + + assert.strictEqual(visibleWidth(partialFlag), 2); + assert.strictEqual(visibleWidth(listLine), 10); + }); + + it("wraps intermediate partial-flag list line before overflow", () => { + // Width 9 cannot fit " - 🇨" if 🇨 is width 2 (8 + 2 = 10). + // This must wrap to avoid terminal auto-wrap mismatch. + const wrapped = wrapTextWithAnsi(" - 🇨", 9); + + assert.strictEqual(wrapped.length, 2); + assert.strictEqual(visibleWidth(wrapped[0] || ""), 7); + assert.strictEqual(visibleWidth(wrapped[1] || ""), 2); + }); + + it("treats all regional-indicator singleton graphemes as width 2", () => { + for (let cp = 0x1f1e6; cp <= 0x1f1ff; cp++) { + const regionalIndicator = String.fromCodePoint(cp); + assert.strictEqual( + visibleWidth(regionalIndicator), + 2, + `Expected ${regionalIndicator} (U+${cp.toString(16).toUpperCase()}) to be width 2`, + ); + } + }); + + it("keeps full flag pairs at width 2", () => { + const samples = ["🇯🇵", "🇺🇸", "🇬🇧", "🇨🇳", "🇩🇪", "🇫🇷"]; + for (const flag of samples) { + assert.strictEqual(visibleWidth(flag), 2, `Expected ${flag} to be width 2`); + } + }); + + it("keeps common streaming emoji intermediates at stable width", () => { + const samples = ["👍", "👍🏻", "✅", "⚡", "⚡️", "👨", "👨‍💻", "🏳️‍🌈"]; + for (const sample of samples) { + assert.strictEqual(visibleWidth(sample), 2, `Expected ${sample} to be width 2`); + } + }); +}); diff --git a/packages/tui/test/regression-sigwinch-kill-eacces.test.ts b/packages/tui/test/regression-sigwinch-kill-eacces.test.ts new file mode 100644 index 0000000000000000000000000000000000000000..bb22a509a2230286b5f75fca5771faf44c0ac33a --- /dev/null +++ b/packages/tui/test/regression-sigwinch-kill-eacces.test.ts @@ -0,0 +1,62 @@ +import assert from "node:assert"; +import { after, describe, it } from "node:test"; +import { refreshTerminalDimensions } from "../src/terminal.ts"; + +describe("refreshTerminalDimensions", () => { + const originalKill = process.kill; + const originalPlatform = process.platform; + + it("does not throw when kill(2) returns EACCES for self-signal", () => { + Object.defineProperty(process, "platform", { value: "linux" }); + process.kill = ((): typeof process.kill => { + return (pid, _signal) => { + if (pid === process.pid) { + const err = new Error("kill EACCES") as NodeJS.ErrnoException; + err.code = "EACCES"; + throw err; + } + return originalKill.call(process, pid, _signal); + }; + })(); + + assert.doesNotThrow(() => { + refreshTerminalDimensions(); + }); + }); + + it("does not call kill on win32", () => { + Object.defineProperty(process, "platform", { value: "win32" }); + let killCalled = false; + process.kill = ((): typeof process.kill => { + return () => { + killCalled = true; + return true; + }; + })(); + + refreshTerminalDimensions(); + + assert.strictEqual(killCalled, false, "kill should not be called on win32"); + }); + + it("preserves other error codes", () => { + Object.defineProperty(process, "platform", { value: "linux" }); + process.kill = ((): typeof process.kill => { + return () => { + const err = new Error("kill EPERM") as NodeJS.ErrnoException; + err.code = "EPERM"; + throw err; + }; + })(); + + // EPERM is also ignored - the refresh is best-effort + assert.doesNotThrow(() => { + refreshTerminalDimensions(); + }); + }); + + after(() => { + process.kill = originalKill; + Object.defineProperty(process, "platform", { value: originalPlatform }); + }); +}); diff --git a/packages/tui/test/render-churn-bench.ts b/packages/tui/test/render-churn-bench.ts new file mode 100644 index 0000000000000000000000000000000000000000..103c9561bf7c947673b827d9367b844d8ad70f2e --- /dev/null +++ b/packages/tui/test/render-churn-bench.ts @@ -0,0 +1,203 @@ +/** + * Alt-screen render churn benchmark. + * + * Measures cumulative JS allocation and wall time for repeated TuiAltScreen + * frames on a layout mirroring pi's fullscreen interactive mode: + * VStack [ ScrollView(transcript), dock VStack [status, editor, footer] ]. + * + * Two scenarios: + * - static: nothing changes between frames (pure recomposite churn) + * - editor: one character appended to the editor per frame (doc scenario + * "30 editor updates") + * + * Allocation is estimated with the V8 sampling heap profiler including + * objects collected by minor/major GC, i.e. it measures churn, not retention. + * + * Run from packages/tui: node test/render-churn-bench.ts + */ + +import { Session } from "node:inspector/promises"; +import { performance } from "node:perf_hooks"; +import { ScrollView } from "../src/components/scroll-view.ts"; +import { Text } from "../src/components/text.ts"; +import { VStack } from "../src/components/v-stack.ts"; +import type { Terminal } from "../src/terminal.ts"; +import { type Component, Container, CURSOR_MARKER } from "../src/tui.ts"; +import { TuiAltScreen } from "../src/tui-alt-screen.ts"; + +const COLUMNS = 100; +const ROWS = 30; +const WARMUP_FRAMES = 20; +const FRAMES = 300; +const SAMPLING_INTERVAL = 4096; + +/** Terminal that discards output; keeps xterm parsing out of the measurement. */ +class NullTerminal implements Terminal { + bytesWritten = 0; + start(_onInput: (data: string) => void, _onResize: () => void): void {} + stop(): void {} + async drainInput(): Promise {} + write(data: string): void { + this.bytesWritten += data.length; + } + get columns(): number { + return COLUMNS; + } + get rows(): number { + return ROWS; + } + get kittyProtocolActive(): boolean { + return false; + } + moveBy(_lines: number): void {} + hideCursor(): void {} + showCursor(): void {} + clearLine(): void {} + clearFromCursor(): void {} + clearScreen(): void {} + setTitle(_title: string): void {} + setProgress(_active: boolean): void {} +} + +/** Editor stand-in: caches lines per (text, width), re-renders when text changes. */ +class EditorSim implements Component { + private text = ""; + private cachedText?: string; + private cachedWidth?: number; + private cachedLines?: string[]; + + append(char: string): void { + this.text += char; + } + + invalidate(): void { + this.cachedText = undefined; + this.cachedWidth = undefined; + this.cachedLines = undefined; + } + + render(width: number): string[] { + if (this.cachedLines && this.cachedText === this.text && this.cachedWidth === width) { + return this.cachedLines; + } + const border = `\x1b[90m${"─".repeat(Math.max(1, width - 2))}\x1b[39m`; + const lines = [border, ` > ${this.text}${CURSOR_MARKER}`, border]; + this.cachedText = this.text; + this.cachedWidth = width; + this.cachedLines = lines; + return lines; + } +} + +function buildTranscript(): Container { + const container = new Container(); + for (let i = 0; i < 150; i++) { + const styled = + i % 3 === 0 + ? `\x1b[1m\x1b[36muser ${i}\x1b[39m\x1b[22m message with some \x1b[33mstyled\x1b[39m content padding padding` + : `assistant ${i} plain response line with enough text to be representative of a transcript row`; + container.addChild(new Text(styled, 1, 0)); + } + return container; +} + +interface SamplingNode { + selfSize: number; + children: SamplingNode[]; +} + +function sumProfile(node: SamplingNode): number { + let total = node.selfSize; + for (const child of node.children) total += sumProfile(child); + return total; +} + +interface ScenarioResult { + allocatedBytes: number; + elapsedMs: number; + bytesWritten: number; +} + +async function runScenario( + session: Session, + terminal: NullTerminal, + tui: TuiAltScreen, + frame: (index: number) => void, +): Promise { + const writtenBefore = terminal.bytesWritten; + await session.post("HeapProfiler.startSampling", { + samplingInterval: SAMPLING_INTERVAL, + includeObjectsCollectedByMajorGC: true, + includeObjectsCollectedByMinorGC: true, + }); + const start = performance.now(); + for (let i = 0; i < FRAMES; i++) { + frame(i); + tui.renderNow(); + } + const elapsedMs = performance.now() - start; + const { profile } = await session.post("HeapProfiler.stopSampling"); + return { + allocatedBytes: sumProfile(profile.head as SamplingNode), + elapsedMs, + bytesWritten: terminal.bytesWritten - writtenBefore, + }; +} + +function report(name: string, result: ScenarioResult): void { + const perFrameKiB = result.allocatedBytes / FRAMES / 1024; + const totalMiB = result.allocatedBytes / 1024 / 1024; + const msPerFrame = result.elapsedMs / FRAMES; + console.log( + `${name.padEnd(8)} allocated ${totalMiB.toFixed(1).padStart(7)} MiB total ` + + `${perFrameKiB.toFixed(1).padStart(8)} KiB/frame ` + + `${msPerFrame.toFixed(3).padStart(7)} ms/frame ` + + `${(result.bytesWritten / FRAMES).toFixed(0).padStart(6)} written bytes/frame`, + ); +} + +async function main(): Promise { + const terminal = new NullTerminal(); + const tui = new TuiAltScreen(terminal, false, "/tmp/pi-tui-bench"); + + const transcript = buildTranscript(); + const editor = new EditorSim(); + const scrollView = new ScrollView(transcript, { + follow: "end", + primary: true, + overscroll: "chain", + scrollbar: "auto", + }); + const status = new Text("\x1b[2mstatus: idle\x1b[22m", 1, 0); + const footer = new Text("\x1b[2m~/workspaces/pi main 100k tokens\x1b[22m", 1, 0); + const dock = new VStack([ + { component: status, shrink: 1, minSize: 0 }, + { component: editor, shrink: 1, minSize: 3 }, + { component: footer, shrink: 1, minSize: 1 }, + ]); + const root = new VStack([ + { component: scrollView, basis: 0, grow: 1, shrink: 1, minSize: 1 }, + { component: dock, basis: "auto", grow: 0, shrink: 1, minSize: 1 }, + ]); + tui.setLayoutRoot(root); + tui.start(); + + for (let i = 0; i < WARMUP_FRAMES; i++) tui.renderNow(); + + const session = new Session(); + session.connect(); + + const staticResult = await runScenario(session, terminal, tui, () => {}); + const editorResult = await runScenario(session, terminal, tui, (i) => { + editor.append(String.fromCharCode(97 + (i % 26))); + }); + + session.disconnect(); + tui.stop(); + + console.log(`frames=${FRAMES} viewport=${COLUMNS}x${ROWS} transcript=${transcript.render(COLUMNS).length} lines`); + report("static", staticResult); + report("editor", editorResult); +} + +await main(); diff --git a/packages/tui/test/select-list.test.ts b/packages/tui/test/select-list.test.ts new file mode 100644 index 0000000000000000000000000000000000000000..b5c24212c1e1fcc567726ed2664d1e22752dd1a0 --- /dev/null +++ b/packages/tui/test/select-list.test.ts @@ -0,0 +1,116 @@ +import assert from "node:assert"; +import { describe, it } from "node:test"; +import { SelectList } from "../src/components/select-list.ts"; +import { visibleWidth } from "../src/utils.ts"; + +const testTheme = { + selectedPrefix: (text: string) => text, + selectedText: (text: string) => text, + description: (text: string) => text, + scrollInfo: (text: string) => text, + noMatch: (text: string) => text, +}; + +const visibleIndexOf = (line: string, text: string): number => { + const index = line.indexOf(text); + assert.notEqual(index, -1); + return visibleWidth(line.slice(0, index)); +}; + +describe("SelectList", () => { + it("normalizes multiline descriptions to single line", () => { + const items = [ + { + value: "test", + label: "test", + description: "Line one\nLine two\nLine three", + }, + ]; + + const list = new SelectList(items, 5, testTheme); + const rendered = list.render(100); + + assert.ok(rendered.length > 0); + assert.ok(!rendered[0].includes("\n")); + assert.ok(rendered[0].includes("Line one Line two Line three")); + }); + + it("keeps descriptions aligned when the primary text is truncated", () => { + const items = [ + { value: "short", label: "short", description: "short description" }, + { + value: "very-long-command-name-that-needs-truncation", + label: "very-long-command-name-that-needs-truncation", + description: "long description", + }, + ]; + + const list = new SelectList(items, 5, testTheme); + const rendered = list.render(80); + + assert.equal(visibleIndexOf(rendered[0], "short description"), visibleIndexOf(rendered[1], "long description")); + }); + + it("uses the configured minimum primary column width", () => { + const items = [ + { value: "a", label: "a", description: "first" }, + { value: "bb", label: "bb", description: "second" }, + ]; + + const list = new SelectList(items, 5, testTheme, { + minPrimaryColumnWidth: 12, + maxPrimaryColumnWidth: 20, + }); + const rendered = list.render(80); + + assert.equal(rendered[0].indexOf("first"), 14); + assert.equal(rendered[1].indexOf("second"), 14); + }); + + it("uses the configured maximum primary column width", () => { + const items = [ + { + value: "very-long-command-name-that-needs-truncation", + label: "very-long-command-name-that-needs-truncation", + description: "first", + }, + { value: "short", label: "short", description: "second" }, + ]; + + const list = new SelectList(items, 5, testTheme, { + minPrimaryColumnWidth: 12, + maxPrimaryColumnWidth: 20, + }); + const rendered = list.render(80); + + assert.equal(visibleIndexOf(rendered[0], "first"), 22); + assert.equal(visibleIndexOf(rendered[1], "second"), 22); + }); + + it("allows overriding primary truncation while preserving description alignment", () => { + const items = [ + { + value: "very-long-command-name-that-needs-truncation", + label: "very-long-command-name-that-needs-truncation", + description: "first", + }, + { value: "short", label: "short", description: "second" }, + ]; + + const list = new SelectList(items, 5, testTheme, { + minPrimaryColumnWidth: 12, + maxPrimaryColumnWidth: 12, + truncatePrimary: ({ text, maxWidth }) => { + if (text.length <= maxWidth) { + return text; + } + + return `${text.slice(0, Math.max(0, maxWidth - 1))}…`; + }, + }); + const rendered = list.render(80); + + assert.ok(rendered[0].includes("…")); + assert.equal(visibleIndexOf(rendered[0], "first"), visibleIndexOf(rendered[1], "second")); + }); +}); diff --git a/packages/tui/test/settings-list.test.ts b/packages/tui/test/settings-list.test.ts new file mode 100644 index 0000000000000000000000000000000000000000..07a7ff09fc5b566acd56764ca9ece3c19b5c507f --- /dev/null +++ b/packages/tui/test/settings-list.test.ts @@ -0,0 +1,58 @@ +import assert from "node:assert"; +import { describe, it } from "node:test"; +import { SettingsList, type SettingsListTheme } from "../src/components/settings-list.ts"; + +const testTheme: SettingsListTheme = { + label: (text) => text, + value: (text) => text, + description: (text) => text, + cursor: "> ", + hint: (text) => text, +}; + +const items = [ + { + id: "tui-mode", + label: "TUI mode", + currentValue: "regular", + values: ["regular", "fullscreen"], + }, +]; + +describe("SettingsList", () => { + it("includes spaces in an active search instead of changing the selected setting", () => { + const changes: Array<{ id: string; value: string }> = []; + const list = new SettingsList( + items.map((item) => ({ ...item })), + 10, + testTheme, + (id, value) => changes.push({ id, value }), + () => {}, + { enableSearch: true }, + ); + + for (const character of "TUI mode") list.handleInput(character); + + assert.deepStrictEqual(changes, []); + assert.match(list.render(80)[0] ?? "", /TUI mode/); + + list.handleInput("\r"); + assert.deepStrictEqual(changes, [{ id: "tui-mode", value: "fullscreen" }]); + }); + + it("keeps Space as a change shortcut before a search query is entered", () => { + const changes: Array<{ id: string; value: string }> = []; + const list = new SettingsList( + items.map((item) => ({ ...item })), + 10, + testTheme, + (id, value) => changes.push({ id, value }), + () => {}, + { enableSearch: true }, + ); + + list.handleInput(" "); + + assert.deepStrictEqual(changes, [{ id: "tui-mode", value: "fullscreen" }]); + }); +}); diff --git a/packages/tui/test/stdin-buffer.test.ts b/packages/tui/test/stdin-buffer.test.ts new file mode 100644 index 0000000000000000000000000000000000000000..4ed8a09136acc5da0f1cd5646e7af2107ce13e1b --- /dev/null +++ b/packages/tui/test/stdin-buffer.test.ts @@ -0,0 +1,526 @@ +/** + * Tests for StdinBuffer + * + * Based on code from OpenTUI (https://github.com/anomalyco/opentui) + * MIT License - Copyright (c) 2025 opentui + */ + +import assert from "node:assert"; +import { beforeEach, describe, it } from "node:test"; +import { matchesKey } from "../src/keys.ts"; +import { StdinBuffer } from "../src/stdin-buffer.ts"; + +describe("StdinBuffer", () => { + let buffer: StdinBuffer; + let emittedSequences: string[]; + + beforeEach(() => { + buffer = new StdinBuffer({ timeout: 10 }); + + // Collect emitted sequences + emittedSequences = []; + buffer.on("data", (sequence) => { + emittedSequences.push(sequence); + }); + }); + + // Helper to process data through the buffer + function processInput(data: string | Buffer): void { + buffer.process(data); + } + + // Helper to wait for async operations + async function wait(ms: number): Promise { + return new Promise((resolve) => setTimeout(resolve, ms)); + } + + describe("Regular Characters", () => { + it("should pass through regular characters immediately", () => { + processInput("a"); + assert.deepStrictEqual(emittedSequences, ["a"]); + }); + + it("should pass through multiple regular characters", () => { + processInput("abc"); + assert.deepStrictEqual(emittedSequences, ["a", "b", "c"]); + }); + + it("should handle unicode characters", () => { + processInput("hello 世界"); + assert.deepStrictEqual(emittedSequences, ["h", "e", "l", "l", "o", " ", "世", "界"]); + }); + }); + + describe("Complete Escape Sequences", () => { + it("should pass through complete mouse SGR sequences", () => { + const mouseSeq = "\x1b[<35;20;5m"; + processInput(mouseSeq); + assert.deepStrictEqual(emittedSequences, [mouseSeq]); + }); + + it("should pass through complete arrow key sequences", () => { + const upArrow = "\x1b[A"; + processInput(upArrow); + assert.deepStrictEqual(emittedSequences, [upArrow]); + }); + + it("should pass through complete function key sequences", () => { + const f1 = "\x1b[11~"; + processInput(f1); + assert.deepStrictEqual(emittedSequences, [f1]); + }); + + it("should pass through meta key sequences", () => { + const metaA = "\x1ba"; + processInput(metaA); + assert.deepStrictEqual(emittedSequences, [metaA]); + }); + + it("should pass through SS3 sequences", () => { + const ss3 = "\x1bOA"; + processInput(ss3); + assert.deepStrictEqual(emittedSequences, [ss3]); + }); + }); + + describe("Partial Escape Sequences", () => { + it("should buffer incomplete mouse SGR sequence", async () => { + processInput("\x1b"); + assert.deepStrictEqual(emittedSequences, []); + assert.strictEqual(buffer.getBuffer(), "\x1b"); + + processInput("[<35"); + assert.deepStrictEqual(emittedSequences, []); + assert.strictEqual(buffer.getBuffer(), "\x1b[<35"); + + processInput(";20;5m"); + assert.deepStrictEqual(emittedSequences, ["\x1b[<35;20;5m"]); + assert.strictEqual(buffer.getBuffer(), ""); + }); + + it("should buffer incomplete CSI sequence", () => { + processInput("\x1b["); + assert.deepStrictEqual(emittedSequences, []); + + processInput("1;"); + assert.deepStrictEqual(emittedSequences, []); + + processInput("5H"); + assert.deepStrictEqual(emittedSequences, ["\x1b[1;5H"]); + }); + + it("should buffer split across many chunks", () => { + processInput("\x1b"); + processInput("["); + processInput("<"); + processInput("3"); + processInput("5"); + processInput(";"); + processInput("2"); + processInput("0"); + processInput(";"); + processInput("5"); + processInput("m"); + + assert.deepStrictEqual(emittedSequences, ["\x1b[<35;20;5m"]); + }); + + it("should flush incomplete sequence after timeout", async () => { + processInput("\x1b[<35"); + assert.deepStrictEqual(emittedSequences, []); + + // Wait for timeout + await wait(15); + + assert.deepStrictEqual(emittedSequences, ["\x1b[<35"]); + }); + + it("should flush a lone ESC as Escape when CR arrives after the timeout", async () => { + // Legacy-mode Alt+Enter is ESC + CR; when the terminal/transport splits + // the bytes further apart than the timeout, ESC is flushed alone and the + // host sees Escape (interrupt) instead of Alt+Enter. This locks in the + // behavior so the configurable timeout in ProcessTerminal stays honest. + processInput("\x1b"); + await wait(20); // buffer timeout is 10ms in beforeEach + processInput("\r"); + + assert.deepStrictEqual(emittedSequences, ["\x1b", "\r"]); + assert.equal(matchesKey(emittedSequences[0] ?? "", "escape"), true); + }); + + it("should merge ESC + CR split across chunks within a larger timeout", async () => { + buffer = new StdinBuffer({ escapeTimeout: 100 }); + emittedSequences = []; + buffer.on("data", (sequence) => { + emittedSequences.push(sequence); + }); + + processInput("\x1b"); + await wait(20); // > 10ms default escapeTimeout, < 100ms configured escapeTimeout + processInput("\r"); + + assert.deepStrictEqual(emittedSequences, ["\x1b\r"]); + assert.equal(matchesKey(emittedSequences[0] ?? "", "alt+enter"), true); + }); + + it("does not apply the sequence timeout to a lone ESC", async () => { + buffer = new StdinBuffer({ timeout: 100 }); + emittedSequences = []; + buffer.on("data", (sequence) => { + emittedSequences.push(sequence); + }); + + processInput("\x1b"); + await wait(20); + processInput("\r"); + + assert.deepStrictEqual(emittedSequences, ["\x1b", "\r"]); + assert.equal(matchesKey(emittedSequences[0] ?? "", "escape"), true); + }); + + it("keeps fragmented mouse sequences buffered across delayed chunks by default", async () => { + const delayedBuffer = new StdinBuffer(); + const delayedSequences: string[] = []; + delayedBuffer.on("data", (sequence) => delayedSequences.push(sequence)); + + delayedBuffer.process("\x1b["); + await wait(20); + assert.deepStrictEqual(delayedSequences, []); + delayedBuffer.process("<65;48;39M"); + assert.deepStrictEqual(delayedSequences, ["\x1b[<65;48;39M"]); + delayedBuffer.destroy(); + }); + }); + + describe("Mixed Content", () => { + it("should handle characters followed by escape sequence", () => { + processInput("abc\x1b[A"); + assert.deepStrictEqual(emittedSequences, ["a", "b", "c", "\x1b[A"]); + }); + + it("should handle escape sequence followed by characters", () => { + processInput("\x1b[Aabc"); + assert.deepStrictEqual(emittedSequences, ["\x1b[A", "a", "b", "c"]); + }); + + it("should handle multiple complete sequences", () => { + processInput("\x1b[A\x1b[B\x1b[C"); + assert.deepStrictEqual(emittedSequences, ["\x1b[A", "\x1b[B", "\x1b[C"]); + }); + + it("should handle partial sequence with preceding characters", () => { + processInput("abc\x1b[<35"); + assert.deepStrictEqual(emittedSequences, ["a", "b", "c"]); + assert.strictEqual(buffer.getBuffer(), "\x1b[<35"); + + processInput(";20;5m"); + assert.deepStrictEqual(emittedSequences, ["a", "b", "c", "\x1b[<35;20;5m"]); + }); + }); + + describe("Kitty Keyboard Protocol", () => { + it("should handle Kitty CSI u press events", () => { + // Press 'a' in Kitty protocol + processInput("\x1b[97u"); + assert.deepStrictEqual(emittedSequences, ["\x1b[97u"]); + }); + + it("should handle Kitty CSI u release events", () => { + // Release 'a' in Kitty protocol + processInput("\x1b[97;1:3u"); + assert.deepStrictEqual(emittedSequences, ["\x1b[97;1:3u"]); + }); + + it("should handle batched Kitty press and release", () => { + // Press 'a', release 'a' batched together (common over SSH) + processInput("\x1b[97u\x1b[97;1:3u"); + assert.deepStrictEqual(emittedSequences, ["\x1b[97u", "\x1b[97;1:3u"]); + }); + + it("should handle multiple batched Kitty events", () => { + // Press 'a', release 'a', press 'b', release 'b' + processInput("\x1b[97u\x1b[97;1:3u\x1b[98u\x1b[98;1:3u"); + assert.deepStrictEqual(emittedSequences, ["\x1b[97u", "\x1b[97;1:3u", "\x1b[98u", "\x1b[98;1:3u"]); + }); + + it("should handle Kitty arrow keys with event type", () => { + // Up arrow press with event type + processInput("\x1b[1;1:1A"); + assert.deepStrictEqual(emittedSequences, ["\x1b[1;1:1A"]); + }); + + it("should handle Kitty functional keys with event type", () => { + // Delete key release + processInput("\x1b[3;1:3~"); + assert.deepStrictEqual(emittedSequences, ["\x1b[3;1:3~"]); + }); + + it("should split ESC+ESC+CSI into standalone ESC and the CSI sequence (WezTerm Escape key regression)", () => { + // WezTerm with enable_kitty_keyboard sends Escape key press as raw \x1b + // and the release as a full Kitty CSI-u sequence, concatenated. + // The buffer must not treat \x1b\x1b as a complete meta-key when the + // following byte starts a new escape sequence. + processInput("\x1b\x1b[27;129:3u"); + assert.deepStrictEqual(emittedSequences, ["\x1b", "\x1b[27;129:3u"]); + }); + + it("should split ESC+ESC+CSI with no modifier (no num_lock)", () => { + processInput("\x1b\x1b[27;1:3u"); + assert.deepStrictEqual(emittedSequences, ["\x1b", "\x1b[27;1:3u"]); + }); + + it("should still emit ESC+ESC as a single sequence when not followed by a new escape", () => { + // \x1b\x1b alone (no following CSI) stays as-is — e.g. ctrl+alt+[ + processInput("\x1b\x1b"); + assert.deepStrictEqual(emittedSequences, ["\x1b\x1b"]); + }); + + it("should handle plain characters mixed with Kitty sequences", () => { + // Plain 'a' followed by Kitty release + processInput("a\x1b[97;1:3u"); + assert.deepStrictEqual(emittedSequences, ["a", "\x1b[97;1:3u"]); + }); + + it("should drop raw duplicate character after matching Kitty printable sequence", () => { + processInput("\x1b[224uà"); + assert.deepStrictEqual(emittedSequences, ["\x1b[224u"]); + }); + + it("should drop raw duplicate character after matching Kitty printable sequence across chunks", () => { + processInput("\x1b[64u"); + processInput("@"); + assert.deepStrictEqual(emittedSequences, ["\x1b[64u"]); + }); + + it("should keep non-matching plain character after Kitty printable sequence", () => { + processInput("\x1b[97ub"); + assert.deepStrictEqual(emittedSequences, ["\x1b[97u", "b"]); + }); + + it("should keep raw character after modified Kitty printable sequence", () => { + processInput("\x1b[64;3u@"); + assert.deepStrictEqual(emittedSequences, ["\x1b[64;3u", "@"]); + }); + + it("should handle rapid typing simulation with Kitty protocol", () => { + // Simulates typing "hi" quickly with releases interleaved + processInput("\x1b[104u\x1b[104;1:3u\x1b[105u\x1b[105;1:3u"); + assert.deepStrictEqual(emittedSequences, ["\x1b[104u", "\x1b[104;1:3u", "\x1b[105u", "\x1b[105;1:3u"]); + }); + }); + + describe("Mouse Events", () => { + it("should handle mouse press event", () => { + processInput("\x1b[<0;10;5M"); + assert.deepStrictEqual(emittedSequences, ["\x1b[<0;10;5M"]); + }); + + it("should handle mouse release event", () => { + processInput("\x1b[<0;10;5m"); + assert.deepStrictEqual(emittedSequences, ["\x1b[<0;10;5m"]); + }); + + it("should handle mouse move event", () => { + processInput("\x1b[<35;20;5m"); + assert.deepStrictEqual(emittedSequences, ["\x1b[<35;20;5m"]); + }); + + it("should handle split mouse events", () => { + processInput("\x1b[<3"); + processInput("5;1"); + processInput("5;"); + processInput("10m"); + assert.deepStrictEqual(emittedSequences, ["\x1b[<35;15;10m"]); + }); + + it("should handle multiple mouse events", () => { + processInput("\x1b[<35;1;1m\x1b[<35;2;2m\x1b[<35;3;3m"); + assert.deepStrictEqual(emittedSequences, ["\x1b[<35;1;1m", "\x1b[<35;2;2m", "\x1b[<35;3;3m"]); + }); + + it("should handle old-style mouse sequence (ESC[M + 3 bytes)", () => { + processInput("\x1b[M abc"); + assert.deepStrictEqual(emittedSequences, ["\x1b[M ab", "c"]); + }); + + it("should buffer incomplete old-style mouse sequence", () => { + processInput("\x1b[M"); + assert.strictEqual(buffer.getBuffer(), "\x1b[M"); + + processInput(" a"); + assert.strictEqual(buffer.getBuffer(), "\x1b[M a"); + + processInput("b"); + assert.deepStrictEqual(emittedSequences, ["\x1b[M ab"]); + }); + }); + + describe("Edge Cases", () => { + it("should handle empty input", () => { + processInput(""); + // Empty string emits an empty data event + assert.deepStrictEqual(emittedSequences, [""]); + }); + + it("should handle lone escape character with timeout", async () => { + processInput("\x1b"); + assert.deepStrictEqual(emittedSequences, []); + + // After timeout, should emit + await wait(15); + assert.deepStrictEqual(emittedSequences, ["\x1b"]); + }); + + it("flushes a lone escape promptly with the longer default sequence timeout", async () => { + const defaultBuffer = new StdinBuffer(); + const defaultSequences: string[] = []; + defaultBuffer.on("data", (sequence) => defaultSequences.push(sequence)); + + defaultBuffer.process("\x1b"); + await wait(20); + assert.deepStrictEqual(defaultSequences, ["\x1b"]); + defaultBuffer.destroy(); + }); + + it("should handle lone escape character with explicit flush", () => { + processInput("\x1b"); + assert.deepStrictEqual(emittedSequences, []); + + const flushed = buffer.flush(); + assert.deepStrictEqual(flushed, ["\x1b"]); + }); + + it("should handle buffer input", () => { + processInput(Buffer.from("\x1b[A")); + assert.deepStrictEqual(emittedSequences, ["\x1b[A"]); + }); + + it("should handle very long sequences", () => { + const longSeq = `\x1b[${"1;".repeat(50)}H`; + processInput(longSeq); + assert.deepStrictEqual(emittedSequences, [longSeq]); + }); + }); + + describe("Flush", () => { + it("should flush incomplete sequences", () => { + processInput("\x1b[<35"); + const flushed = buffer.flush(); + assert.deepStrictEqual(flushed, ["\x1b[<35"]); + assert.strictEqual(buffer.getBuffer(), ""); + }); + + it("should return empty array if nothing to flush", () => { + const flushed = buffer.flush(); + assert.deepStrictEqual(flushed, []); + }); + + it("should emit flushed data via timeout", async () => { + processInput("\x1b[<35"); + assert.deepStrictEqual(emittedSequences, []); + + // Wait for timeout to flush + await wait(15); + + assert.deepStrictEqual(emittedSequences, ["\x1b[<35"]); + }); + }); + + describe("Clear", () => { + it("should clear buffered content without emitting", () => { + processInput("\x1b[<35"); + assert.strictEqual(buffer.getBuffer(), "\x1b[<35"); + + buffer.clear(); + assert.strictEqual(buffer.getBuffer(), ""); + assert.deepStrictEqual(emittedSequences, []); + }); + }); + + describe("Bracketed Paste", () => { + let emittedPaste: string[] = []; + + beforeEach(() => { + buffer = new StdinBuffer({ timeout: 10 }); + + // Collect emitted sequences + emittedSequences = []; + buffer.on("data", (sequence) => { + emittedSequences.push(sequence); + }); + + // Collect paste events + emittedPaste = []; + buffer.on("paste", (data) => { + emittedPaste.push(data); + }); + }); + + it("should emit paste event for complete bracketed paste", () => { + const pasteStart = "\x1b[200~"; + const pasteEnd = "\x1b[201~"; + const content = "hello world"; + + processInput(pasteStart + content + pasteEnd); + + assert.deepStrictEqual(emittedPaste, ["hello world"]); + assert.deepStrictEqual(emittedSequences, []); // No data events during paste + }); + + it("should handle paste arriving in chunks", () => { + processInput("\x1b[200~"); + assert.deepStrictEqual(emittedPaste, []); + + processInput("hello "); + assert.deepStrictEqual(emittedPaste, []); + + processInput("world\x1b[201~"); + assert.deepStrictEqual(emittedPaste, ["hello world"]); + assert.deepStrictEqual(emittedSequences, []); + }); + + it("should handle paste with input before and after", () => { + processInput("a"); + processInput("\x1b[200~pasted\x1b[201~"); + processInput("b"); + + assert.deepStrictEqual(emittedSequences, ["a", "b"]); + assert.deepStrictEqual(emittedPaste, ["pasted"]); + }); + + it("should handle paste with newlines", () => { + processInput("\x1b[200~line1\nline2\nline3\x1b[201~"); + + assert.deepStrictEqual(emittedPaste, ["line1\nline2\nline3"]); + assert.deepStrictEqual(emittedSequences, []); + }); + + it("should handle paste with unicode", () => { + processInput("\x1b[200~Hello 世界 🎉\x1b[201~"); + + assert.deepStrictEqual(emittedPaste, ["Hello 世界 🎉"]); + assert.deepStrictEqual(emittedSequences, []); + }); + }); + + describe("Destroy", () => { + it("should clear buffer on destroy", () => { + processInput("\x1b[<35"); + assert.strictEqual(buffer.getBuffer(), "\x1b[<35"); + + buffer.destroy(); + assert.strictEqual(buffer.getBuffer(), ""); + }); + + it("should clear pending timeouts on destroy", async () => { + processInput("\x1b[<35"); + buffer.destroy(); + + // Wait longer than timeout + await wait(15); + + // Should not have emitted anything + assert.deepStrictEqual(emittedSequences, []); + }); + }); +}); diff --git a/packages/tui/test/tab-width.test.ts b/packages/tui/test/tab-width.test.ts new file mode 100644 index 0000000000000000000000000000000000000000..b7396e4854cfb51435ae724b0e584ef19c41f5b6 --- /dev/null +++ b/packages/tui/test/tab-width.test.ts @@ -0,0 +1,88 @@ +import assert from "node:assert"; +import { describe, it } from "node:test"; +import type { Component, TUI } from "../src/tui.ts"; +import { TuiMainScreen } from "../src/tui-main-screen.ts"; +import { extractSegments, normalizeTerminalOutput, sliceWithWidth, visibleWidth } from "../src/utils.ts"; +import { VirtualTerminal } from "./virtual-terminal.ts"; + +class FullViewportContent implements Component { + render(width: number): string[] { + return ["base 0", "base 1", "base 2"].map((line) => line.padEnd(width)); + } + + invalidate(): void {} +} + +class CapturingVirtualTerminal extends VirtualTerminal { + private output = ""; + + override write(data: string): void { + this.output += data; + super.write(data); + } + + getOutput(): string { + return this.output; + } +} + +class TabStatusOverlay implements Component { + render(): string[] { + return ["\tX"]; + } + + invalidate(): void {} +} + +describe("tab width accounting", () => { + it("keeps slice helper widths consistent with visible width", () => { + const text = "out 192M\t.pi/skill-tests/results-ha"; + const slice = sliceWithWidth(text, 0, 10, true); + + assert.strictEqual(slice.text, "out 192M"); + assert.strictEqual(slice.width, 8); + assert.strictEqual(visibleWidth(slice.text), slice.width); + }); + + it("keeps overlay segment widths consistent with visible width", () => { + const text = "out 192M\t.pi/skill-tests/results-ha"; + const segments = extractSegments(text, 10, 13, 10, true); + + assert.strictEqual(segments.before, "out 192M"); + assert.strictEqual(segments.beforeWidth, 8); + assert.strictEqual(visibleWidth(segments.before), segments.beforeWidth); + + const tabFits = extractSegments(text, 11, 13, 10, true); + assert.strictEqual(tabFits.before, "out 192M\t"); + assert.strictEqual(tabFits.beforeWidth, 11); + assert.strictEqual(visibleWidth(tabFits.before), tabFits.beforeWidth); + }); + + it("keeps tabs inside terminal control sequences byte-identical", () => { + const controlSequences = [ + "\x1b]8;;https://example.test/a\tb\x07", + "\x1b]0;window\ttitle\x1b\\", + "\x1b_payload\tdata\x1b\\", + ]; + + for (const controlSequence of controlSequences) { + assert.strictEqual(normalizeTerminalOutput(`${controlSequence}label\ttext`), `${controlSequence}label text`); + } + }); + + it("keeps tab-containing overlays on one physical terminal row", async () => { + const terminal = new CapturingVirtualTerminal(16, 3); + const tui: TUI = new TuiMainScreen(terminal); + tui.addChild(new FullViewportContent()); + tui.showOverlay(new TabStatusOverlay(), { width: 4, row: 1, col: 4 }); + tui.start(); + + try { + await terminal.waitForRender(); + assert.deepStrictEqual(terminal.getViewport(), ["base 0 ", "base X ", "base 2 "]); + assert.ok(!terminal.getOutput().includes("\t")); + } finally { + tui.stop(); + } + }); +}); diff --git a/packages/tui/test/terminal-colors.test.ts b/packages/tui/test/terminal-colors.test.ts new file mode 100644 index 0000000000000000000000000000000000000000..229549732a2419172d53b04b7b5c636aa4ebeb2d --- /dev/null +++ b/packages/tui/test/terminal-colors.test.ts @@ -0,0 +1,252 @@ +import assert from "node:assert"; +import { describe, it } from "node:test"; +import { + type Component, + parseOsc11BackgroundColor, + parseTerminalColorSchemeReport, + type Terminal, + type TUI, + TuiMainScreen, +} from "../src/index.ts"; + +class TestTerminal implements Terminal { + private inputHandler?: (data: string) => void; + private resizeHandler?: () => void; + private readonly columnCount: number; + private readonly rowCount: number; + readonly writes: string[] = []; + + constructor(columnCount = 80, rowCount = 24) { + this.columnCount = columnCount; + this.rowCount = rowCount; + } + + start(onInput: (data: string) => void, onResize: () => void): void { + this.inputHandler = onInput; + this.resizeHandler = onResize; + } + + stop(): void { + this.inputHandler = undefined; + this.resizeHandler = undefined; + } + + async drainInput(_maxMs?: number, _idleMs?: number): Promise {} + + write(data: string): void { + this.writes.push(data); + } + + get columns(): number { + return this.columnCount; + } + + get rows(): number { + return this.rowCount; + } + + get kittyProtocolActive(): boolean { + return false; + } + + moveBy(_lines: number): void {} + + hideCursor(): void {} + + showCursor(): void {} + + clearLine(): void {} + + clearFromCursor(): void {} + + clearScreen(): void {} + + setTitle(_title: string): void {} + + setProgress(_active: boolean): void {} + + sendInput(data: string): void { + this.inputHandler?.(data); + } + + sendResize(): void { + this.resizeHandler?.(); + } +} + +class InputRecorder implements Component { + readonly inputs: string[] = []; + + render(_width: number): string[] { + return []; + } + + handleInput(data: string): void { + this.inputs.push(data); + } + + invalidate(): void {} +} + +const wait = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms)); + +describe("parseOsc11BackgroundColor", () => { + it("parses 16-bit OSC 11 rgb responses", () => { + assert.deepStrictEqual(parseOsc11BackgroundColor("\x1b]11;rgb:0000/8000/ffff\x07"), { + r: 0, + g: 128, + b: 255, + }); + }); + + it("parses OSC 11 hex responses", () => { + assert.deepStrictEqual(parseOsc11BackgroundColor("\x1b]11;#ffffff\x1b\\"), { r: 255, g: 255, b: 255 }); + assert.deepStrictEqual(parseOsc11BackgroundColor("\x1b]11;#000000\x07"), { r: 0, g: 0, b: 0 }); + }); + + it("rejects non-strict OSC 11 responses", () => { + assert.strictEqual(parseOsc11BackgroundColor(`x\x1b]11;#ffffff\x07`), undefined); + assert.strictEqual(parseOsc11BackgroundColor("\x1b]10;#ffffff\x07"), undefined); + assert.strictEqual(parseOsc11BackgroundColor("\x1b]11;#ffffff\x07x"), undefined); + }); +}); + +describe("parseTerminalColorSchemeReport", () => { + it("parses color scheme reports", () => { + assert.strictEqual(parseTerminalColorSchemeReport("\x1b[?997;1n"), "dark"); + assert.strictEqual(parseTerminalColorSchemeReport("\x1b[?997;2n"), "light"); + assert.strictEqual(parseTerminalColorSchemeReport("\x1b[?997;2n\x1b[?997;1n\x1b[?997;1n"), "dark"); + assert.strictEqual(parseTerminalColorSchemeReport("\x1b[?997;1n\x1b[?997;2n\x1b[?997;2n"), "light"); + assert.strictEqual(parseTerminalColorSchemeReport("\x1b[?997;3n"), undefined); + assert.strictEqual(parseTerminalColorSchemeReport("\x1b[?996n"), undefined); + assert.strictEqual(parseTerminalColorSchemeReport("x\x1b[?997;1n"), undefined); + }); +}); + +describe("TUI.queryTerminalBackgroundColor", () => { + it("writes OSC 11 query and resolves with the parsed RGB reply", async () => { + const terminal = new TestTerminal(); + const tui: TUI = new TuiMainScreen(terminal); + tui.start(); + try { + const query = tui.queryTerminalBackgroundColor({ timeoutMs: 1000 }); + assert.ok(terminal.writes.includes("\x1b]11;?\x07")); + + terminal.sendInput("\x1b]11;#ffffff\x07"); + + assert.deepStrictEqual(await query, { r: 255, g: 255, b: 255 }); + } finally { + tui.stop(); + } + }); + + it("consumes OSC 11 replies before input listeners and focused component dispatch", async () => { + const terminal = new TestTerminal(); + const tui: TUI = new TuiMainScreen(terminal); + const component = new InputRecorder(); + const listenerInputs: string[] = []; + tui.addChild(component); + tui.setFocus(component); + tui.addInputListener((data) => { + listenerInputs.push(data); + return undefined; + }); + tui.start(); + try { + const query = tui.queryTerminalBackgroundColor({ timeoutMs: 1000 }); + + terminal.sendInput("\x1b]11;#000000\x07"); + + assert.deepStrictEqual(await query, { r: 0, g: 0, b: 0 }); + assert.deepStrictEqual(listenerInputs, []); + assert.deepStrictEqual(component.inputs, []); + } finally { + tui.stop(); + } + }); + + it("consumes unparseable strict OSC 11 replies and resolves undefined", async () => { + const terminal = new TestTerminal(); + const tui: TUI = new TuiMainScreen(terminal); + const component = new InputRecorder(); + const listenerInputs: string[] = []; + tui.addChild(component); + tui.setFocus(component); + tui.addInputListener((data) => { + listenerInputs.push(data); + return undefined; + }); + tui.start(); + try { + const query = tui.queryTerminalBackgroundColor({ timeoutMs: 1000 }); + + terminal.sendInput("\x1b]11;not-a-color\x07"); + + assert.strictEqual(await query, undefined); + assert.deepStrictEqual(listenerInputs, []); + assert.deepStrictEqual(component.inputs, []); + } finally { + tui.stop(); + } + }); + + it("dispatches non-matching input normally while waiting for an OSC 11 reply", async () => { + const terminal = new TestTerminal(); + const tui: TUI = new TuiMainScreen(terminal); + const component = new InputRecorder(); + const listenerInputs: string[] = []; + tui.addChild(component); + tui.setFocus(component); + tui.addInputListener((data) => { + listenerInputs.push(data); + return undefined; + }); + tui.start(); + try { + let settled = false; + const query = tui.queryTerminalBackgroundColor({ timeoutMs: 1000 }).then((rgb) => { + settled = true; + return rgb; + }); + + terminal.sendInput("x"); + await Promise.resolve(); + + assert.strictEqual(settled, false); + assert.deepStrictEqual(listenerInputs, ["x"]); + assert.deepStrictEqual(component.inputs, ["x"]); + + terminal.sendInput("\x1b]11;#ffffff\x07"); + assert.deepStrictEqual(await query, { r: 255, g: 255, b: 255 }); + } finally { + tui.stop(); + } + }); + + it("keeps consuming a late OSC 11 reply after timeout", async () => { + const terminal = new TestTerminal(); + const tui: TUI = new TuiMainScreen(terminal); + const component = new InputRecorder(); + const listenerInputs: string[] = []; + tui.addChild(component); + tui.setFocus(component); + tui.addInputListener((data) => { + listenerInputs.push(data); + return undefined; + }); + tui.start(); + try { + const query = tui.queryTerminalBackgroundColor({ timeoutMs: 1 }); + await wait(5); + + assert.strictEqual(await query, undefined); + + terminal.sendInput("\x1b]11;#ffffff\x07"); + + assert.deepStrictEqual(listenerInputs, []); + assert.deepStrictEqual(component.inputs, []); + } finally { + tui.stop(); + } + }); +}); diff --git a/packages/tui/test/terminal-image.test.ts b/packages/tui/test/terminal-image.test.ts new file mode 100644 index 0000000000000000000000000000000000000000..069136d00012b959fbcaa1468dab3f924d57329d --- /dev/null +++ b/packages/tui/test/terminal-image.test.ts @@ -0,0 +1,700 @@ +/** + * Tests for terminal image detection and line handling + */ + +import assert from "node:assert"; +import { homedir } from "node:os"; +import { join } from "node:path"; +import { describe, it } from "node:test"; +import { Image } from "../src/components/image.ts"; +import { + cropKittyImageLine, + deleteAllKittyImages, + deleteAllKittyPlacements, + deleteKittyImage, + detectCapabilities, + encodeITerm2, + encodeKitty, + getCapabilities, + getKittyImageMetadata, + getKittyImagePlacement, + hyperlink, + imageFallback, + isImageLine, + registerKittyImageMetadata, + renderImage, + resetCapabilitiesCache, + setCapabilities, + setCapabilityOverrides, + setCellDimensions, +} from "../src/terminal-image.ts"; +import { visibleWidth } from "../src/utils.ts"; + +const ENV_KEYS = [ + "TERM", + "TERM_PROGRAM", + "TERMINAL_EMULATOR", + "COLORTERM", + "TMUX", + "KITTY_WINDOW_ID", + "GHOSTTY_RESOURCES_DIR", + "WEZTERM_PANE", + "ITERM_SESSION_ID", + "WT_SESSION", + "CMUX_WORKSPACE_ID", + "WARP_SESSION_ID", + "WARP_TERMINAL_SESSION_UUID", + "PI_HYPERLINKS", + "PI_IMAGE_PROTOCOL", + "PI_TRUE_COLOR", +] as const; + +function withEnv(overrides: Record, fn: () => T): T { + const saved: Record = {}; + for (const key of ENV_KEYS) { + saved[key] = process.env[key]; + delete process.env[key]; + } + try { + for (const [k, v] of Object.entries(overrides)) { + if (v === undefined) delete process.env[k]; + else process.env[k] = v; + } + return fn(); + } finally { + for (const key of ENV_KEYS) { + if (saved[key] === undefined) delete process.env[key]; + else process.env[key] = saved[key]; + } + } +} + +describe("isImageLine", () => { + describe("iTerm2 image protocol", () => { + it("should detect iTerm2 image escape sequence at start of line", () => { + // iTerm2 image escape sequence: ESC ]1337;File=... + const iterm2ImageLine = "\x1b]1337;File=size=100,100;inline=1:base64encodeddata==\x07"; + assert.strictEqual(isImageLine(iterm2ImageLine), true); + }); + + it("should detect iTerm2 image escape sequence with text before it", () => { + // Simulating a line that has text then image data (bug scenario) + const lineWithTextAndImage = "Some text \x1b]1337;File=size=100,100;inline=1:base64data==\x07 more text"; + assert.strictEqual(isImageLine(lineWithTextAndImage), true); + }); + + it("should detect iTerm2 image escape sequence in middle of long line", () => { + // Simulate a very long line with image data in the middle + const longLineWithImage = + "Text before image..." + "\x1b]1337;File=inline=1:verylongbase64data==" + "...text after"; + assert.strictEqual(isImageLine(longLineWithImage), true); + }); + + it("should detect iTerm2 image escape sequence at end of line", () => { + const lineWithImageAtEnd = "Regular text ending with \x1b]1337;File=inline=1:base64data==\x07"; + assert.strictEqual(isImageLine(lineWithImageAtEnd), true); + }); + + it("should detect minimal iTerm2 image escape sequence", () => { + const minimalImageLine = "\x1b]1337;File=:\x07"; + assert.strictEqual(isImageLine(minimalImageLine), true); + }); + }); + + describe("Kitty image protocol", () => { + it("should detect Kitty image escape sequence at start of line", () => { + // Kitty image escape sequence: ESC _G + const kittyImageLine = "\x1b_Ga=T,f=100,t=f,d=base64data...\x1b\\\x1b_Gm=i=1;\x1b\\"; + assert.strictEqual(isImageLine(kittyImageLine), true); + }); + + it("should detect Kitty image escape sequence with text before it", () => { + // Bug scenario: text + image data in same line + const lineWithTextAndKittyImage = "Output: \x1b_Ga=T,f=100;data...\x1b\\\x1b_Gm=i=1;\x1b\\"; + assert.strictEqual(isImageLine(lineWithTextAndKittyImage), true); + }); + + it("should detect Kitty image escape sequence with padding", () => { + // Kitty protocol adds padding to escape sequences + const kittyWithPadding = " \x1b_Ga=T,f=100...\x1b\\\x1b_Gm=i=1;\x1b\\ "; + assert.strictEqual(isImageLine(kittyWithPadding), true); + }); + }); + + describe("Bug regression tests", () => { + it("should detect image sequences in very long lines (304k+ chars)", () => { + // This simulates the crash scenario: a line with 304,401 chars + // containing image escape sequences somewhere + const base64Char = "A".repeat(100); // 100 chars of base64-like data + const imageSequence = "\x1b]1337;File=size=800,600;inline=1:"; + + // Build a long line with image sequence + const longLine = + "Text prefix " + + imageSequence + + base64Char.repeat(3000) + // ~300,000 chars + " suffix"; + + assert.strictEqual(longLine.length > 300000, true); + assert.strictEqual(isImageLine(longLine), true); + }); + + it("should detect image sequences when terminal doesn't support images", () => { + // The bug occurred when getImageEscapePrefix() returned null + // isImageLine should still detect image sequences regardless + const lineWithImage = "Read image file [image/jpeg]\x1b]1337;File=inline=1:base64data==\x07"; + assert.strictEqual(isImageLine(lineWithImage), true); + }); + + it("should detect image sequences with ANSI codes before them", () => { + // Text might have ANSI styling before image data + const lineWithAnsiAndImage = "\x1b[31mError output \x1b]1337;File=inline=1:image==\x07"; + assert.strictEqual(isImageLine(lineWithAnsiAndImage), true); + }); + + it("should detect image sequences with ANSI codes after them", () => { + const lineWithImageAndAnsi = "\x1b_Ga=T,f=100:data...\x1b\\\x1b_Gm=i=1;\x1b\\\x1b[0m reset"; + assert.strictEqual(isImageLine(lineWithImageAndAnsi), true); + }); + }); + + describe("Negative cases - lines without images", () => { + it("should not detect images in plain text lines", () => { + const plainText = "This is just a regular text line without any escape sequences"; + assert.strictEqual(isImageLine(plainText), false); + }); + + it("should not detect images in lines with only ANSI codes", () => { + const ansiText = "\x1b[31mRed text\x1b[0m and \x1b[32mgreen text\x1b[0m"; + assert.strictEqual(isImageLine(ansiText), false); + }); + + it("should not detect images in lines with cursor movement codes", () => { + const cursorCodes = "\x1b[1A\x1b[2KLine cleared and moved up"; + assert.strictEqual(isImageLine(cursorCodes), false); + }); + + it("should not detect images in lines with partial iTerm2 sequences", () => { + // Similar prefix but missing the complete sequence + const partialSequence = "Some text with ]1337;File but missing ESC at start"; + assert.strictEqual(isImageLine(partialSequence), false); + }); + + it("should not detect images in lines with partial Kitty sequences", () => { + // Similar prefix but missing the complete sequence + const partialSequence = "Some text with _G but missing ESC at start"; + assert.strictEqual(isImageLine(partialSequence), false); + }); + + it("should not detect images in empty lines", () => { + assert.strictEqual(isImageLine(""), false); + }); + + it("should not detect images in lines with newlines only", () => { + assert.strictEqual(isImageLine("\n"), false); + assert.strictEqual(isImageLine("\n\n"), false); + }); + }); + + describe("Mixed content scenarios", () => { + it("should detect images when line has both Kitty and iTerm2 sequences", () => { + const mixedLine = "Kitty: \x1b_Ga=T...\x1b\\\x1b_Gm=i=1;\x1b\\ iTerm2: \x1b]1337;File=inline=1:data==\x07"; + assert.strictEqual(isImageLine(mixedLine), true); + }); + + it("should detect image in line with multiple text and image segments", () => { + const complexLine = "Start \x1b]1337;File=img1==\x07 middle \x1b]1337;File=img2==\x07 end"; + assert.strictEqual(isImageLine(complexLine), true); + }); + + it("should not falsely detect image in line with file path containing keywords", () => { + // File path might contain "1337" or "File" but without escape sequences + const filePathLine = "/path/to/File_1337_backup/image.jpg"; + assert.strictEqual(isImageLine(filePathLine), false); + }); + }); +}); + +describe("detectCapabilities", () => { + it("defaults to hyperlinks: false for unknown terminals", () => { + withEnv({}, () => { + const caps = detectCapabilities(); + assert.strictEqual(caps.hyperlinks, false); + assert.strictEqual(caps.images, null); + }); + }); + + it("applies environment overrides", () => { + assert.deepStrictEqual( + withEnv({ PI_HYPERLINKS: "1", PI_IMAGE_PROTOCOL: "kitty", PI_TRUE_COLOR: "1" }, () => detectCapabilities()), + { images: "kitty", trueColor: true, hyperlinks: true }, + ); + assert.deepStrictEqual( + withEnv({ TERM_PROGRAM: "iterm.app", PI_HYPERLINKS: "0", PI_IMAGE_PROTOCOL: "none", PI_TRUE_COLOR: "0" }, () => + detectCapabilities(), + ), + { images: null, trueColor: false, hyperlinks: false }, + ); + }); + + it("preserves auto-detection for auto environment overrides", () => { + assert.deepStrictEqual( + withEnv( + { + TERM_PROGRAM: "ghostty", + PI_HYPERLINKS: "auto", + PI_IMAGE_PROTOCOL: "auto", + PI_TRUE_COLOR: "auto", + }, + () => detectCapabilities(), + ), + { images: "kitty", trueColor: true, hyperlinks: true }, + ); + }); + + it("applies and clears programmatic overrides", () => { + withEnv({ PI_HYPERLINKS: "1", PI_IMAGE_PROTOCOL: "kitty", PI_TRUE_COLOR: "1" }, () => { + setCapabilityOverrides({ images: null, trueColor: false, hyperlinks: false }); + try { + assert.deepStrictEqual(getCapabilities(), { images: null, trueColor: false, hyperlinks: false }); + setCapabilityOverrides({}); + assert.deepStrictEqual(getCapabilities(), { images: "kitty", trueColor: true, hyperlinks: true }); + } finally { + setCapabilityOverrides({}); + resetCapabilitiesCache(); + } + }); + }); + + it("bypasses the tmux probe when hyperlinks are overridden", () => { + let probed = false; + const caps = withEnv( + { TMUX: "/tmp/tmux-1000/default,1234,0", PI_HYPERLINKS: "1", PI_IMAGE_PROTOCOL: "kitty" }, + () => + detectCapabilities(() => { + probed = true; + return false; + }), + ); + assert.strictEqual(probed, false); + assert.strictEqual(caps.hyperlinks, true); + assert.strictEqual(caps.images, "kitty"); + }); + + it("enables hyperlinks under tmux when the client forwards them", () => { + withEnv({ TMUX: "/tmp/tmux-1000/default,1234,0", TERM_PROGRAM: "ghostty" }, () => { + const caps = detectCapabilities(() => true); + assert.strictEqual(caps.hyperlinks, true); + assert.strictEqual(caps.images, null); + }); + }); + + it("disables hyperlinks under tmux when the client does not forward them", () => { + withEnv({ TMUX: "/tmp/tmux-1000/default,1234,0", TERM_PROGRAM: "ghostty" }, () => { + const caps = detectCapabilities(() => false); + assert.strictEqual(caps.hyperlinks, false); + assert.strictEqual(caps.images, null); + }); + }); + + it("checks tmux capability when TERM starts with 'tmux'", () => { + withEnv({ TERM: "tmux-256color", TERM_PROGRAM: "iterm.app" }, () => { + const caps = detectCapabilities(() => true); + assert.strictEqual(caps.hyperlinks, true); + assert.strictEqual(caps.images, null); + + const caps2 = detectCapabilities(() => false); + assert.strictEqual(caps2.hyperlinks, false); + }); + }); + + it("forces hyperlinks: false when TERM starts with 'screen'", () => { + withEnv({ TERM: "screen-256color" }, () => { + const caps = detectCapabilities(); + assert.strictEqual(caps.hyperlinks, false); + assert.strictEqual(caps.images, null); + }); + }); + + it("enables hyperlinks for Ghostty", () => { + withEnv({ TERM_PROGRAM: "ghostty" }, () => { + const caps = detectCapabilities(); + assert.strictEqual(caps.hyperlinks, true); + }); + }); + + it("does not disable Ghostty images solely because cmux is present", () => { + withEnv({ TERM_PROGRAM: "ghostty", CMUX_WORKSPACE_ID: "workspace" }, () => { + const caps = detectCapabilities(); + assert.strictEqual(caps.images, "kitty"); + assert.strictEqual(caps.hyperlinks, true); + }); + }); + + it("enables hyperlinks for Kitty", () => { + withEnv({ KITTY_WINDOW_ID: "1" }, () => { + const caps = detectCapabilities(); + assert.strictEqual(caps.hyperlinks, true); + }); + }); + + it("enables hyperlinks for WezTerm", () => { + withEnv({ WEZTERM_PANE: "0" }, () => { + const caps = detectCapabilities(); + assert.strictEqual(caps.hyperlinks, true); + }); + }); + + it("enables images and hyperlinks for Warp via TERM_PROGRAM", () => { + withEnv({ TERM_PROGRAM: "WarpTerminal" }, () => { + const caps = detectCapabilities(); + assert.strictEqual(caps.images, "kitty"); + assert.strictEqual(caps.trueColor, true); + assert.strictEqual(caps.hyperlinks, true); + }); + }); + + it("enables images and hyperlinks for Warp via WARP_SESSION_ID", () => { + withEnv({ WARP_SESSION_ID: "some-session-id" }, () => { + const caps = detectCapabilities(); + assert.strictEqual(caps.images, "kitty"); + assert.strictEqual(caps.trueColor, true); + assert.strictEqual(caps.hyperlinks, true); + }); + }); + + it("enables images and hyperlinks for Warp via WARP_TERMINAL_SESSION_UUID", () => { + withEnv({ WARP_TERMINAL_SESSION_UUID: "d0e1a2e5-7ca7-44cd-9037-ac7222011161" }, () => { + const caps = detectCapabilities(); + assert.strictEqual(caps.images, "kitty"); + assert.strictEqual(caps.trueColor, true); + assert.strictEqual(caps.hyperlinks, true); + }); + }); + + it("disables images for Warp inside tmux", () => { + withEnv( + { + TERM_PROGRAM: "WarpTerminal", + TMUX: "/tmp/tmux-1000/default,1234,0", + TERM: "tmux-256color", + }, + () => { + const caps = detectCapabilities(() => true); + assert.strictEqual(caps.images, null); + assert.strictEqual(caps.hyperlinks, true); + }, + ); + }); + + it("enables hyperlinks for iTerm2", () => { + withEnv({ TERM_PROGRAM: "iterm.app" }, () => { + const caps = detectCapabilities(); + assert.strictEqual(caps.hyperlinks, true); + }); + }); + + it("enables hyperlinks for VSCode", () => { + withEnv({ TERM_PROGRAM: "vscode" }, () => { + const caps = detectCapabilities(); + assert.strictEqual(caps.hyperlinks, true); + }); + }); + + it("enables Alacritty capabilities for Zed", () => { + withEnv({ TERM_PROGRAM: "zed" }, () => { + assert.deepStrictEqual(detectCapabilities(), { images: null, trueColor: true, hyperlinks: true }); + }); + }); + + it("enables truecolor and hyperlinks for Windows Terminal outside multiplexers", () => { + withEnv({ WT_SESSION: "session", TERM: "xterm-256color" }, () => { + const caps = detectCapabilities(); + assert.strictEqual(caps.trueColor, true); + assert.strictEqual(caps.hyperlinks, true); + assert.strictEqual(caps.images, null); + }); + }); + + it("enables truecolor without hyperlinks for JetBrains terminal", () => { + withEnv({ TERMINAL_EMULATOR: "JetBrains-JediTerm", TERM: "xterm-256color" }, () => { + const caps = detectCapabilities(); + assert.strictEqual(caps.trueColor, true); + assert.strictEqual(caps.hyperlinks, false); + assert.strictEqual(caps.images, null); + }); + }); + + it("does not inherit Windows Terminal truecolor through tmux", () => { + withEnv({ WT_SESSION: "session", TMUX: "/tmp/tmux-1000/default,1234,0", TERM: "tmux-256color" }, () => { + const caps = detectCapabilities(() => false); + assert.strictEqual(caps.trueColor, false); + assert.strictEqual(caps.hyperlinks, false); + assert.strictEqual(caps.images, null); + }); + }); + + it("trusts explicit truecolor hints through tmux", () => { + withEnv({ COLORTERM: "truecolor", TMUX: "/tmp/tmux-1000/default,1234,0", TERM: "tmux-256color" }, () => { + const caps = detectCapabilities(() => false); + assert.strictEqual(caps.trueColor, true); + assert.strictEqual(caps.hyperlinks, false); + assert.strictEqual(caps.images, null); + }); + }); +}); + +describe("iTerm2 image encoding", () => { + it("includes the decoded payload size in OSC 1337 metadata", () => { + const sequence = encodeITerm2("AAAA", { width: 2, height: "auto" }); + assert.strictEqual(sequence, "\x1b]1337;File=inline=1;size=3;width=2;height=auto:AAAA\x07"); + }); +}); + +describe("Kitty image cursor movement", () => { + it("can request no terminal-side cursor movement", () => { + const sequence = encodeKitty("AAAA", { columns: 2, rows: 2, moveCursor: false }); + assert.ok(sequence.startsWith("\x1b_Ga=T,f=100,q=2,C=1,c=2,r=2;")); + }); + + it("suppresses Kitty replies for delete commands", () => { + assert.strictEqual(deleteKittyImage(42), "\x1b_Ga=d,d=I,i=42,q=2\x1b\\"); + assert.strictEqual(deleteAllKittyImages(), "\x1b_Ga=d,d=A,q=2\x1b\\"); + assert.strictEqual(deleteAllKittyPlacements(), "\x1b_Ga=d,d=a,q=2\x1b\\"); + }); + + it("preserves renderImage's default terminal-side cursor movement", () => { + setCapabilities({ images: "kitty", trueColor: true, hyperlinks: true }); + setCellDimensions({ widthPx: 10, heightPx: 10 }); + try { + const result = renderImage("AAAA", { widthPx: 20, heightPx: 20 }, { maxWidthCells: 2 }); + assert.ok(result); + assert.ok(!result.sequence.includes(",C=1,")); + assert.strictEqual(result.rows, 2); + } finally { + resetCapabilitiesCache(); + setCellDimensions({ widthPx: 9, heightPx: 18 }); + } + }); + + it("can opt renderImage into no terminal-side cursor movement", () => { + setCapabilities({ images: "kitty", trueColor: true, hyperlinks: true }); + setCellDimensions({ widthPx: 10, heightPx: 10 }); + try { + const result = renderImage("AAAA", { widthPx: 20, heightPx: 20 }, { maxWidthCells: 2, moveCursor: false }); + assert.ok(result); + assert.ok(result.sequence.includes(",C=1,")); + assert.strictEqual(result.rows, 2); + } finally { + resetCapabilitiesCache(); + setCellDimensions({ widthPx: 9, heightPx: 18 }); + } + }); + + it("registers metadata and crops a partially visible placement", () => { + setCapabilities({ images: "kitty", trueColor: true, hyperlinks: true }); + setCellDimensions({ widthPx: 10, heightPx: 10 }); + try { + const result = renderImage( + "AAAA", + { widthPx: 100, heightPx: 100 }, + { maxWidthCells: 3, imageId: 42, moveCursor: false }, + ); + assert.ok(result); + assert.deepStrictEqual(getKittyImageMetadata(result.sequence), { + imageId: 42, + columns: 3, + rows: 3, + widthPx: 100, + heightPx: 100, + }); + assert.ok(cropKittyImageLine(result.sequence, 2, 1).includes("y=66,h=34,r=1")); + } finally { + resetCapabilitiesCache(); + setCellDimensions({ widthPx: 9, heightPx: 18 }); + } + }); + + it("creates placement-only commands for uploaded and cropped images", () => { + registerKittyImageMetadata({ imageId: 42, columns: 3, rows: 3, widthPx: 100, heightPx: 100 }); + const transmission = encodeKitty("A".repeat(8192), { + columns: 3, + rows: 3, + imageId: 42, + moveCursor: false, + }); + const line = `left ${cropKittyImageLine(transmission, 2, 1)} right`; + const placement = getKittyImagePlacement(line); + assert.ok(placement); + assert.strictEqual(placement.transmissionBytes, line.length - "left ".length - " right".length); + assert.strictEqual(placement.estimatedDecodedBytes, 100 * 100 * 4); + assert.strictEqual(placement.sequence, "\x1b_Ga=p,q=2,C=1,c=3,i=42,y=66,h=34,r=1\x1b\\"); + assert.strictEqual(placement.replacementLine, `left ${placement.sequence} right`); + assert.ok(!placement.replacementLine.includes("AAAA")); + }); + + it("honors maxHeightCells by reducing rendered width", () => { + setCapabilities({ images: "kitty", trueColor: true, hyperlinks: true }); + setCellDimensions({ widthPx: 10, heightPx: 10 }); + try { + const result = renderImage("AAAA", { widthPx: 10, heightPx: 100 }, { maxWidthCells: 10, maxHeightCells: 5 }); + assert.ok(result); + assert.strictEqual(result.rows, 5); + assert.ok(result.sequence.includes(",c=1,r=5")); + } finally { + resetCapabilitiesCache(); + setCellDimensions({ widthPx: 9, heightPx: 18 }); + } + }); + + it("caps Image component height to a square pixel box by default", () => { + setCapabilities({ images: "kitty", trueColor: true, hyperlinks: true }); + setCellDimensions({ widthPx: 10, heightPx: 20 }); + try { + const image = new Image( + "AAAA", + "image/png", + { fallbackColor: (value) => value }, + { maxWidthCells: 10 }, + { widthPx: 10, heightPx: 100 }, + ); + const lines = image.render(12); + assert.strictEqual(lines.length, 5); + assert.ok(lines[0].includes(",c=1,r=5")); + } finally { + resetCapabilitiesCache(); + setCellDimensions({ widthPx: 9, heightPx: 18 }); + } + }); + + it("places image sequence on first line with empty padding rows", () => { + setCapabilities({ images: "kitty", trueColor: true, hyperlinks: true }); + setCellDimensions({ widthPx: 10, heightPx: 10 }); + try { + const image = new Image( + "AAAA", + "image/png", + { fallbackColor: (value) => value }, + { maxWidthCells: 2 }, + { widthPx: 20, heightPx: 20 }, + ); + const lines = image.render(4); + const imageId = image.getImageId(); + assert.strictEqual(typeof imageId, "number"); + assert.ok(lines[0].startsWith("\x1b_G")); + assert.ok(lines[0].includes(",C=1,")); + assert.ok(lines[0].includes(`,i=${imageId}`)); + assert.ok(lines[0].endsWith("\x1b\\")); + assert.deepStrictEqual(lines.slice(1, lines.length), [""]); + } finally { + resetCapabilitiesCache(); + setCellDimensions({ widthPx: 9, heightPx: 18 }); + } + }); + + it("truncates long image fallback lines to render width", () => { + setCapabilities({ images: null, trueColor: false, hyperlinks: false }); + try { + const longPath = join( + homedir(), + "images", + `${"generated-image-with-a-very-long-absolute-path".repeat(4)}.png`, + ); + const width = 40; + const image = new Image( + "AAAA", + "image/png", + { fallbackColor: (value) => `\x1b[33m${value}\x1b[0m` }, + { filename: longPath }, + { widthPx: 1280, heightPx: 720 }, + ); + const lines = image.render(width); + assert.strictEqual(lines.length, 1); + assert.ok( + visibleWidth(lines[0]) <= width, + `fallback line wider than ${width}: visible=${visibleWidth(lines[0])} raw=${JSON.stringify(lines[0])}`, + ); + assert.ok(lines[0].includes("..."), "expected ellipsis when truncating long fallback path"); + assert.ok(lines[0].includes("~"), "expected home-shortened path in fallback"); + } finally { + resetCapabilitiesCache(); + } + }); +}); + +describe("imageFallback", () => { + it("shortens home-prefixed absolute paths without hyperlinks", () => { + setCapabilities({ images: null, trueColor: false, hyperlinks: false }); + try { + const abs = join(homedir(), ".pi", "agent", "shot.png"); + const result = imageFallback("image/png", { widthPx: 1280, heightPx: 720 }, abs); + assert.strictEqual(result, "[Image: ~/.pi/agent/shot.png [image/png] 1280x720]"); + } finally { + resetCapabilitiesCache(); + } + }); + + it("wraps shortened absolute paths in OSC 8 file links when hyperlinks are enabled", () => { + setCapabilities({ images: null, trueColor: false, hyperlinks: true }); + try { + const abs = join(homedir(), ".pi", "agent", "shot.png"); + const result = imageFallback("image/png", { widthPx: 10, heightPx: 10 }, abs); + assert.ok(result.includes("\x1b]8;;file://"), "expected OSC 8 file link"); + assert.ok( + result.includes(abs.replaceAll("\\", "/")) || result.includes(abs), + "file URL should target absolute path", + ); + // Visible text must use ~/... not the expanded home path. + const visible = result.replace(/\x1b\]8;;.*?\x1b\\/g, ""); + assert.strictEqual(visible, "[Image: ~/.pi/agent/shot.png [image/png] 10x10]"); + } finally { + resetCapabilitiesCache(); + } + }); + + it("leaves bare basenames unchanged and does not hyperlink them", () => { + setCapabilities({ images: null, trueColor: false, hyperlinks: true }); + try { + const result = imageFallback("image/png", { widthPx: 1, heightPx: 1 }, "clankolas.png"); + assert.strictEqual(result, "[Image: clankolas.png [image/png] 1x1]"); + assert.ok(!result.includes("\x1b]8;"), "basename must not be hyperlinked"); + } finally { + resetCapabilitiesCache(); + } + }); + + it("omits filename segment when not provided", () => { + setCapabilities({ images: null, trueColor: false, hyperlinks: false }); + try { + assert.strictEqual(imageFallback("image/png", { widthPx: 8, heightPx: 6 }), "[Image: [image/png] 8x6]"); + } finally { + resetCapabilitiesCache(); + } + }); +}); + +describe("hyperlink", () => { + it("wraps text in OSC 8 open and close sequences", () => { + const result = hyperlink("click me", "https://example.com"); + assert.strictEqual(result, "\x1b]8;;https://example.com\x1b\\click me\x1b]8;;\x1b\\"); + }); + + it("preserves ANSI styling inside the hyperlink", () => { + const styled = "\x1b[4m\x1b[34mclick me\x1b[0m"; + const result = hyperlink(styled, "https://example.com"); + assert.ok(result.startsWith("\x1b]8;;https://example.com\x1b\\")); + assert.ok(result.includes(styled)); + assert.ok(result.endsWith("\x1b]8;;\x1b\\")); + }); + + it("works with empty text", () => { + const result = hyperlink("", "https://example.com"); + assert.strictEqual(result, "\x1b]8;;https://example.com\x1b\\\x1b]8;;\x1b\\"); + }); + + it("works with file:// URIs", () => { + const result = hyperlink("README.md", "file:///home/user/README.md"); + assert.ok(result.includes("file:///home/user/README.md")); + assert.ok(result.includes("README.md")); + }); +}); diff --git a/packages/tui/test/terminal.test.ts b/packages/tui/test/terminal.test.ts new file mode 100644 index 0000000000000000000000000000000000000000..80eadd6264c902b9c4fdd3fea3d72ee27c45b2e9 --- /dev/null +++ b/packages/tui/test/terminal.test.ts @@ -0,0 +1,300 @@ +import assert from "node:assert"; +import { describe, it, mock } from "node:test"; +import { setKittyProtocolActive } from "../src/keys.ts"; +import { + normalizeAppleTerminalInput, + normalizeNativeShiftEnterInput, + ProcessTerminal, + resolveEscapeTimeoutMs, +} from "../src/terminal.ts"; + +describe("resolveEscapeTimeoutMs", () => { + it("uses PI_TUI_ESC_TIMEOUT when configured", () => { + assert.equal(resolveEscapeTimeoutMs({ PI_TUI_ESC_TIMEOUT: "80" }), 80); + assert.equal(resolveEscapeTimeoutMs({ PI_TUI_ESC_TIMEOUT: "80", SSH_TTY: "/dev/pts/1" }), 80); + }); + + it("ignores invalid PI_TUI_ESC_TIMEOUT values", () => { + assert.equal(resolveEscapeTimeoutMs({ PI_TUI_ESC_TIMEOUT: "abc" }), 10); + assert.equal(resolveEscapeTimeoutMs({ PI_TUI_ESC_TIMEOUT: "0" }), 10); + assert.equal(resolveEscapeTimeoutMs({ PI_TUI_ESC_TIMEOUT: "-5" }), 10); + assert.equal(resolveEscapeTimeoutMs({ PI_TUI_ESC_TIMEOUT: "" }), 10); + }); + + it("defaults to 100ms over SSH", () => { + assert.equal(resolveEscapeTimeoutMs({ SSH_CONNECTION: "10.0.0.1 22" }), 100); + assert.equal(resolveEscapeTimeoutMs({ SSH_TTY: "/dev/pts/1" }), 100); + }); + + it("defaults to 10ms otherwise", () => { + assert.equal(resolveEscapeTimeoutMs({}), 10); + }); +}); + +describe("normalizeNativeShiftEnterInput", () => { + it("rewrites Return to CSI-u Shift+Enter when native Shift detection is enabled and Shift is pressed", () => { + assert.equal(normalizeNativeShiftEnterInput("\r", true, true), "\x1b[13;2u"); + }); + + it("leaves Return unchanged when native Shift detection is disabled", () => { + assert.equal(normalizeNativeShiftEnterInput("\r", false, true), "\r"); + }); + + it("leaves Return unchanged when Shift is not pressed", () => { + assert.equal(normalizeNativeShiftEnterInput("\r", true, false), "\r"); + }); + + it("leaves non-Return input unchanged", () => { + assert.equal(normalizeNativeShiftEnterInput("\x1b[13;2u", true, true), "\x1b[13;2u"); + assert.equal(normalizeNativeShiftEnterInput("a", true, true), "a"); + }); +}); + +describe("normalizeAppleTerminalInput", () => { + it("rewrites Apple Terminal Return to CSI-u Shift+Enter when Shift is pressed", () => { + assert.equal(normalizeAppleTerminalInput("\r", true, true), "\x1b[13;2u"); + }); + + it("leaves Apple Terminal Return unchanged when Shift is not pressed", () => { + assert.equal(normalizeAppleTerminalInput("\r", true, false), "\r"); + }); + + it("leaves non-Apple Terminal Return unchanged when Shift is pressed", () => { + assert.equal(normalizeAppleTerminalInput("\r", false, true), "\r"); + }); + + it("leaves non-Return input unchanged", () => { + assert.equal(normalizeAppleTerminalInput("\x1b[13;2u", true, true), "\x1b[13;2u"); + assert.equal(normalizeAppleTerminalInput("a", true, true), "a"); + }); +}); + +describe("ProcessTerminal Kitty keyboard protocol negotiation", () => { + type NegotiationHarness = { + terminal: ProcessTerminal; + writes: string[]; + send(data: string): void; + getInput(): string | undefined; + cleanup(): void; + }; + + function setupNegotiation(): NegotiationHarness { + const terminal = new ProcessTerminal(); + const writes: string[] = []; + let input: string | undefined; + let dataHandler: ((data: string) => void) | undefined; + let cleaned = false; + const previousWrite = process.stdout.write; + const previousOn = process.stdin.on; + + process.stdout.write = ((chunk: string | Uint8Array) => { + writes.push(String(chunk)); + return true; + }) as typeof process.stdout.write; + process.stdin.on = ((event: string | symbol, listener: (...args: unknown[]) => void) => { + if (event === "data") dataHandler = listener as (data: string) => void; + return process.stdin; + }) as typeof process.stdin.on; + + ( + terminal as unknown as { + inputHandler?: (data: string) => void; + queryAndEnableKittyProtocol(): void; + } + ).inputHandler = (data) => { + input = data; + }; + (terminal as unknown as { queryAndEnableKittyProtocol(): void }).queryAndEnableKittyProtocol(); + + return { + terminal, + writes, + send(data: string): void { + dataHandler?.(data); + }, + getInput(): string | undefined { + return input; + }, + cleanup(): void { + if (cleaned) return; + cleaned = true; + try { + terminal.stop(); + } finally { + process.stdout.write = previousWrite; + process.stdin.on = previousOn; + setKittyProtocolActive(false); + } + }, + }; + } + + it("queries Kitty mode before enabling modifyOtherKeys fallback", () => { + const harness = setupNegotiation(); + try { + assert.equal(harness.writes[0], "\x1b[>7u\x1b[?u\x1b[c"); + assert.equal(harness.writes.includes("\x1b[>4;2m"), false); + assert.equal(harness.terminal.kittyProtocolActive, false); + } finally { + harness.cleanup(); + } + }); + + it("activates Kitty mode for non-zero negotiated flags", () => { + const harness = setupNegotiation(); + try { + harness.send("\x1b[?7u"); + + assert.equal(harness.getInput(), undefined); + assert.equal(harness.terminal.kittyProtocolActive, true); + assert.equal(harness.writes.includes("\x1b[>4;2m"), false); + assert.equal(harness.writes.includes("\x1b[>4;0m"), false); + + harness.cleanup(); + assert.equal(harness.writes.filter((write) => write === "\x1b[4;0m"), false); + } finally { + harness.cleanup(); + } + }); + + it("falls back to modifyOtherKeys for zero Kitty flags", () => { + const harness = setupNegotiation(); + try { + harness.send("\x1b[?0u"); + + assert.equal(harness.getInput(), undefined); + assert.equal(harness.terminal.kittyProtocolActive, false); + assert.equal(harness.writes.filter((write) => write === "\x1b[>4;2m").length, 1); + + harness.cleanup(); + assert.equal(harness.writes.filter((write) => write === "\x1b[>4;0m").length, 1); + } finally { + harness.cleanup(); + } + }); + + it("falls back to modifyOtherKeys for device attributes without Kitty flags", () => { + const harness = setupNegotiation(); + try { + harness.send("\x1b[?62;4;52c"); + + assert.equal(harness.getInput(), undefined); + assert.equal(harness.terminal.kittyProtocolActive, false); + assert.equal(harness.writes.filter((write) => write === "\x1b[>4;2m").length, 1); + } finally { + harness.cleanup(); + } + }); + + it("forwards normal input while waiting for Kitty response", () => { + const harness = setupNegotiation(); + try { + harness.send("a"); + + assert.equal(harness.getInput(), "a"); + assert.equal(harness.terminal.kittyProtocolActive, false); + } finally { + harness.cleanup(); + } + }); + + it("tracks split Kitty confirmation", () => { + mock.timers.enable({ apis: ["setTimeout"] }); + const harness = setupNegotiation(); + try { + harness.send("\x1b[?7"); + mock.timers.tick(10); + + assert.equal(harness.getInput(), undefined); + + harness.send("u"); + + assert.equal(harness.terminal.kittyProtocolActive, true); + assert.equal(harness.writes.includes("\x1b[>4;2m"), false); + } finally { + harness.cleanup(); + mock.timers.reset(); + } + }); + + it("replays buffered CSI-prefix input when it is not a Kitty response", () => { + mock.timers.enable({ apis: ["setTimeout"] }); + const harness = setupNegotiation(); + try { + harness.send("\x1b["); + mock.timers.tick(50); // StdinBuffer sequence timeout, not the lone-ESC timeout + + assert.equal(harness.getInput(), undefined); + + mock.timers.tick(150); + + assert.equal(harness.getInput(), "\x1b["); + } finally { + harness.cleanup(); + mock.timers.reset(); + } + }); +}); + +describe("ProcessTerminal progress", () => { + it("writes a valid OSC 9;4 clear sequence", () => { + const terminal = new ProcessTerminal(); + const writes: string[] = []; + const previousWrite = process.stdout.write; + + process.stdout.write = ((chunk: string | Uint8Array) => { + writes.push(String(chunk)); + return true; + }) as typeof process.stdout.write; + + try { + terminal.setProgress(false); + assert.deepEqual(writes, ["\x1b]9;4;0\x07"]); + } finally { + process.stdout.write = previousWrite; + } + }); +}); + +describe("ProcessTerminal dimensions", () => { + it("falls back to COLUMNS and LINES before default dimensions", () => { + const previousColumnsDescriptor = Object.getOwnPropertyDescriptor(process.stdout, "columns"); + const previousRowsDescriptor = Object.getOwnPropertyDescriptor(process.stdout, "rows"); + const previousColumns = process.env.COLUMNS; + const previousLines = process.env.LINES; + + try { + Object.defineProperty(process.stdout, "columns", { value: undefined, configurable: true }); + Object.defineProperty(process.stdout, "rows", { value: undefined, configurable: true }); + process.env.COLUMNS = "123"; + process.env.LINES = "45"; + + const terminal = new ProcessTerminal(); + + assert.equal(terminal.columns, 123); + assert.equal(terminal.rows, 45); + } finally { + if (previousColumnsDescriptor) { + Object.defineProperty(process.stdout, "columns", previousColumnsDescriptor); + } else { + Reflect.deleteProperty(process.stdout, "columns"); + } + if (previousRowsDescriptor) { + Object.defineProperty(process.stdout, "rows", previousRowsDescriptor); + } else { + Reflect.deleteProperty(process.stdout, "rows"); + } + if (previousColumns === undefined) { + delete process.env.COLUMNS; + } else { + process.env.COLUMNS = previousColumns; + } + if (previousLines === undefined) { + delete process.env.LINES; + } else { + process.env.LINES = previousLines; + } + } + }); +}); diff --git a/packages/tui/test/test-themes.ts b/packages/tui/test/test-themes.ts new file mode 100644 index 0000000000000000000000000000000000000000..33e3c39e1a524d5ca2b2244ad7d42b055d1ace12 --- /dev/null +++ b/packages/tui/test/test-themes.ts @@ -0,0 +1,38 @@ +/** + * Default themes for TUI tests using chalk + */ + +import { Chalk } from "chalk"; +import type { EditorTheme, MarkdownTheme, SelectListTheme } from "../src/index.ts"; + +const chalk = new Chalk({ level: 3 }); + +export const defaultSelectListTheme: SelectListTheme = { + selectedPrefix: (text: string) => chalk.blue(text), + selectedText: (text: string) => chalk.bold(text), + description: (text: string) => chalk.dim(text), + scrollInfo: (text: string) => chalk.dim(text), + noMatch: (text: string) => chalk.dim(text), +}; + +export const defaultMarkdownTheme: MarkdownTheme = { + heading: (text: string) => chalk.bold.cyan(text), + link: (text: string) => chalk.blue(text), + linkUrl: (text: string) => chalk.dim(text), + code: (text: string) => chalk.yellow(text), + codeBlock: (text: string) => chalk.green(text), + codeBlockBorder: (text: string) => chalk.dim(text), + quote: (text: string) => chalk.italic(text), + quoteBorder: (text: string) => chalk.dim(text), + hr: (text: string) => chalk.dim(text), + listBullet: (text: string) => chalk.cyan(text), + bold: (text: string) => chalk.bold(text), + italic: (text: string) => chalk.italic(text), + strikethrough: (text: string) => chalk.strikethrough(text), + underline: (text: string) => chalk.underline(text), +}; + +export const defaultEditorTheme: EditorTheme = { + borderColor: (text: string) => chalk.dim(text), + selectList: defaultSelectListTheme, +}; diff --git a/packages/tui/test/truncate-to-width.test.ts b/packages/tui/test/truncate-to-width.test.ts new file mode 100644 index 0000000000000000000000000000000000000000..6240eca64500a044a640b85e1327d2c88e6fb559 --- /dev/null +++ b/packages/tui/test/truncate-to-width.test.ts @@ -0,0 +1,127 @@ +import assert from "node:assert"; +import { describe, it } from "node:test"; +import { normalizeTerminalOutput, truncateToWidth, visibleWidth } from "../src/utils.ts"; + +describe("truncateToWidth", () => { + it("keeps output within width for very large unicode input", () => { + const text = "🙂界".repeat(100_000); + const truncated = truncateToWidth(text, 40, "…"); + + assert.ok(visibleWidth(truncated) <= 40); + assert.strictEqual(truncated.endsWith("…\x1b[0m"), true); + }); + + it("preserves ANSI styling for kept text and resets before and after ellipsis", () => { + const text = `\x1b[31m${"hello ".repeat(1000)}\x1b[0m`; + const truncated = truncateToWidth(text, 20, "…"); + + assert.ok(visibleWidth(truncated) <= 20); + assert.strictEqual(truncated.includes("\x1b[31m"), true); + assert.strictEqual(truncated.endsWith("\x1b[0m…\x1b[0m"), true); + }); + + it("closes a BEL-terminated OSC 8 link when truncating its label", () => { + const open = "\x1b]8;;https://example.com\x07"; + const close = "\x1b]8;;\x07"; + const text = `${open}some-longer-label-here${close}`; + + assert.strictEqual(truncateToWidth(text, 15), `${open}some-longer-${close}\x1b[0m...\x1b[0m`); + }); + + it("handles malformed ANSI escape prefixes without hanging", () => { + const text = `abc\x1bnot-ansi ${"🙂".repeat(1000)}`; + const truncated = truncateToWidth(text, 20, "…"); + + assert.ok(visibleWidth(truncated) <= 20); + }); + + it("clips wide ellipsis safely and brackets it with resets", () => { + assert.strictEqual(truncateToWidth("abcdef", 1, "🙂"), ""); + assert.strictEqual(truncateToWidth("abcdef", 2, "🙂"), "\x1b[0m🙂\x1b[0m"); + assert.ok(visibleWidth(truncateToWidth("abcdef", 2, "🙂")) <= 2); + }); + + it("returns the original text when it already fits even if ellipsis is too wide", () => { + assert.strictEqual(truncateToWidth("a", 2, "🙂"), "a"); + assert.strictEqual(truncateToWidth("界", 2, "🙂"), "界"); + }); + + it("pads truncated output to requested width", () => { + const truncated = truncateToWidth("🙂界🙂界🙂界", 8, "…", true); + assert.strictEqual(visibleWidth(truncated), 8); + }); + + it("adds a trailing reset when truncating without an ellipsis", () => { + const truncated = truncateToWidth(`\x1b[31m${"hello".repeat(100)}`, 10, ""); + assert.ok(visibleWidth(truncated) <= 10); + assert.strictEqual(truncated.endsWith("\x1b[0m"), true); + }); + + it("keeps a contiguous prefix instead of skipping a wide grapheme and resuming later", () => { + const truncated = truncateToWidth("🙂\t界 \x1b_abc\x07", 7, "…", true); + assert.strictEqual(truncated, "🙂\t\x1b[0m…\x1b[0m "); + }); +}); + +describe("visibleWidth", () => { + it("counts tabs inline and skips ANSI inline", () => { + assert.strictEqual(visibleWidth("\t\x1b[31m界\x1b[0m"), 5); + }); + + it("counts Indic conjunct spacing code points within grapheme clusters", () => { + assert.strictEqual(visibleWidth("र्क"), 2); + assert.strictEqual(visibleWidth("नेटवर्क"), 5); + assert.strictEqual(visibleWidth("सर्वाधिकार सुरक्षित। ऑर्डर पर क्लिक करें"), 33); + assert.strictEqual(visibleWidth("র্ক"), 2); + assert.strictEqual(visibleWidth("ર્ક"), 2); + assert.strictEqual(visibleWidth("ର୍କ"), 2); + assert.strictEqual(visibleWidth("ర్క"), 2); + assert.strictEqual(visibleWidth("ര്‍ക"), 2); + }); + + it("keeps ordinary combining marks zero-width", () => { + assert.strictEqual(visibleWidth("e\u0301"), 1); + assert.strictEqual(visibleWidth("čřžůú"), 5); + assert.strictEqual(visibleWidth("שָׁ"), 1); + assert.strictEqual(visibleWidth("بّ"), 1); + assert.strictEqual(visibleWidth("རྐ"), 1); + assert.strictEqual(visibleWidth("ᜠ᜴"), 1); + assert.strictEqual(visibleWidth("가〮"), 2); + assert.strictEqual(visibleWidth("가〯"), 2); + }); + + it("keeps CJK and Japanese width accounting unchanged", () => { + assert.strictEqual(visibleWidth("网络"), 4); + assert.strictEqual(visibleWidth("ネットワーク"), 12); + assert.strictEqual(visibleWidth("が"), 2); + assert.strictEqual(visibleWidth("か\u3099"), 2); + }); + + it("counts Myanmar marks that terminals allocate cells for", () => { + assert.strictEqual(visibleWidth("ကာ"), 2); + assert.strictEqual(visibleWidth("ကေ"), 2); + assert.strictEqual(visibleWidth("က်"), 2); + assert.strictEqual(visibleWidth("ကျ"), 2); + assert.strictEqual(visibleWidth("ကြ"), 2); + assert.strictEqual(visibleWidth("ကဳ"), 2); + assert.strictEqual(visibleWidth("ကဴ"), 2); + assert.strictEqual(visibleWidth("ကဵ"), 2); + assert.strictEqual(visibleWidth("ကး"), 2); + assert.strictEqual(visibleWidth("ကို"), 1); + assert.strictEqual(visibleWidth("က္"), 1); + }); + + it("keeps Thai and Lao AM clusters at their normal cell width", () => { + assert.strictEqual(visibleWidth("ำ"), 1); + assert.strictEqual(visibleWidth("ຳ"), 1); + assert.strictEqual(visibleWidth("กำ"), 2); + assert.strictEqual(visibleWidth("ກຳ"), 2); + }); + + it("normalizes Thai and Lao AM vowels only for terminal output", () => { + assert.strictEqual(normalizeTerminalOutput("ำ"), "ํา"); + assert.strictEqual(normalizeTerminalOutput("ຳ"), "ໍາ"); + assert.strictEqual(visibleWidth(normalizeTerminalOutput("ำabc")), visibleWidth("ำabc")); + assert.strictEqual(visibleWidth(normalizeTerminalOutput("ຳabc")), visibleWidth("ຳabc")); + }); +}); diff --git a/packages/tui/test/truncated-text.test.ts b/packages/tui/test/truncated-text.test.ts new file mode 100644 index 0000000000000000000000000000000000000000..a25034e27f63ce62f7f41a7b0b33ede25b145d09 --- /dev/null +++ b/packages/tui/test/truncated-text.test.ts @@ -0,0 +1,129 @@ +import assert from "node:assert"; +import { describe, it } from "node:test"; +import { Chalk } from "chalk"; +import { TruncatedText } from "../src/components/truncated-text.ts"; +import { visibleWidth } from "../src/utils.ts"; + +// Force full color in CI so ANSI assertions are deterministic +const chalk = new Chalk({ level: 3 }); + +describe("TruncatedText component", () => { + it("pads output lines to exactly match width", () => { + const text = new TruncatedText("Hello world", 1, 0); + const lines = text.render(50); + + // Should have exactly one content line (no vertical padding) + assert.strictEqual(lines.length, 1); + + // Line should be exactly 50 visible characters + const visibleLen = visibleWidth(lines[0]); + assert.strictEqual(visibleLen, 50); + }); + + it("pads output with vertical padding lines to width", () => { + const text = new TruncatedText("Hello", 0, 2); + const lines = text.render(40); + + // Should have 2 padding lines + 1 content line + 2 padding lines = 5 total + assert.strictEqual(lines.length, 5); + + // All lines should be exactly 40 characters + for (const line of lines) { + assert.strictEqual(visibleWidth(line), 40); + } + }); + + it("truncates long text and pads to width", () => { + const longText = "This is a very long piece of text that will definitely exceed the available width"; + const text = new TruncatedText(longText, 1, 0); + const lines = text.render(30); + + assert.strictEqual(lines.length, 1); + + // Should be exactly 30 characters + assert.strictEqual(visibleWidth(lines[0]), 30); + + // Should contain ellipsis + const stripped = lines[0].replace(/\x1b\[[0-9;]*m/g, ""); + assert.ok(stripped.includes("...")); + }); + + it("preserves ANSI codes in output and pads correctly", () => { + const styledText = `${chalk.red("Hello")} ${chalk.blue("world")}`; + const text = new TruncatedText(styledText, 1, 0); + const lines = text.render(40); + + assert.strictEqual(lines.length, 1); + + // Should be exactly 40 visible characters (ANSI codes don't count) + assert.strictEqual(visibleWidth(lines[0]), 40); + + // Should preserve the color codes + assert.ok(lines[0].includes("\x1b[")); + }); + + it("truncates styled text and adds reset code before ellipsis", () => { + const longStyledText = chalk.red("This is a very long red text that will be truncated"); + const text = new TruncatedText(longStyledText, 1, 0); + const lines = text.render(20); + + assert.strictEqual(lines.length, 1); + + // Should be exactly 20 visible characters + assert.strictEqual(visibleWidth(lines[0]), 20); + + // Should contain reset code before ellipsis + assert.ok(lines[0].includes("\x1b[0m...")); + }); + + it("handles text that fits exactly", () => { + // With paddingX=1, available width is 30-2=28 + // "Hello world" is 11 chars, fits comfortably + const text = new TruncatedText("Hello world", 1, 0); + const lines = text.render(30); + + assert.strictEqual(lines.length, 1); + assert.strictEqual(visibleWidth(lines[0]), 30); + + // Should NOT contain ellipsis + const stripped = lines[0].replace(/\x1b\[[0-9;]*m/g, ""); + assert.ok(!stripped.includes("...")); + }); + + it("handles empty text", () => { + const text = new TruncatedText("", 1, 0); + const lines = text.render(30); + + assert.strictEqual(lines.length, 1); + assert.strictEqual(visibleWidth(lines[0]), 30); + }); + + it("stops at newline and only shows first line", () => { + const multilineText = "First line\nSecond line\nThird line"; + const text = new TruncatedText(multilineText, 1, 0); + const lines = text.render(40); + + assert.strictEqual(lines.length, 1); + assert.strictEqual(visibleWidth(lines[0]), 40); + + // Should only contain "First line" + const stripped = lines[0].replace(/\x1b\[[0-9;]*m/g, "").trim(); + assert.ok(stripped.includes("First line")); + assert.ok(!stripped.includes("Second line")); + assert.ok(!stripped.includes("Third line")); + }); + + it("truncates first line even with newlines in text", () => { + const longMultilineText = "This is a very long first line that needs truncation\nSecond line"; + const text = new TruncatedText(longMultilineText, 1, 0); + const lines = text.render(25); + + assert.strictEqual(lines.length, 1); + assert.strictEqual(visibleWidth(lines[0]), 25); + + // Should contain ellipsis and not second line + const stripped = lines[0].replace(/\x1b\[[0-9;]*m/g, ""); + assert.ok(stripped.includes("...")); + assert.ok(!stripped.includes("Second line")); + }); +}); diff --git a/packages/tui/test/tui-alt-screen.test.ts b/packages/tui/test/tui-alt-screen.test.ts new file mode 100644 index 0000000000000000000000000000000000000000..feb2ff13743bb5315e2b09423e595955d88a9061 --- /dev/null +++ b/packages/tui/test/tui-alt-screen.test.ts @@ -0,0 +1,1908 @@ +import assert from "node:assert"; +import { describe, it } from "node:test"; +import { + AltScreenSearchComponent, + AltScreenSearchIndex, + findAltScreenSearchMatches, +} from "../src/alt-screen-search.ts"; +import { HStack } from "../src/components/h-stack.ts"; +import { Image } from "../src/components/image.ts"; +import { MouseRegion } from "../src/components/mouse-region.ts"; +import { ScrollView } from "../src/components/scroll-view.ts"; +import { SelectList } from "../src/components/select-list.ts"; +import { Text } from "../src/components/text.ts"; +import { VStack } from "../src/components/v-stack.ts"; +import { getKeybindings, KeybindingsManager, setKeybindings, TUI_KEYBINDINGS } from "../src/keybindings.ts"; +import { + encodeKitty, + hyperlink, + registerKittyImageMetadata, + resetCapabilitiesCache, + setCapabilities, +} from "../src/terminal-image.ts"; +import type { TuiMouseEvent } from "../src/tui.ts"; +import { TuiAltScreen } from "../src/tui-alt-screen.ts"; +import { stripTerminalSequences, visibleWidth } from "../src/utils.ts"; +import { VirtualTerminal } from "./virtual-terminal.ts"; + +const OSC133_ZONE_START = "\x1b]133;A\x07"; + +class InputOverlay { + focused = false; + inputs: string[] = []; + + handleInput(data: string): void { + this.inputs.push(data); + } + + render(): string[] { + return ["overlay"]; + } + + invalidate(): void {} +} + +class RecordingTerminal extends VirtualTerminal { + readonly events: Array<{ type: "write"; data: string } | { type: "start" } | { type: "stop" }> = []; + + override start(onInput: (data: string) => void, onResize: () => void): void { + this.events.push({ type: "start" }); + super.start(onInput, onResize); + } + + override write(data: string): void { + this.events.push({ type: "write", data }); + super.write(data); + } + + override stop(): void { + this.events.push({ type: "stop" }); + super.stop(); + } +} + +describe("TuiAltScreen", () => { + it("renders a terminal-height viewport and preserves manual scroll position", async () => { + const terminal = new VirtualTerminal(20, 4); + const tui = new TuiAltScreen(terminal); + const text = new Text(Array.from({ length: 10 }, (_, index) => `line ${index + 1}`).join("\n"), 0, 0); + tui.addChild(text); + tui.start(); + await terminal.waitForRender(); + + assert.deepStrictEqual( + terminal.getViewport().map((line) => line.trimEnd()), + ["line 7", "line 8", "line 9", "line 10"], + ); + assert.strictEqual(tui.isFollowingOutput, true); + + terminal.sendInput("\x1b[<64;1;1M"); + await terminal.waitForRender(); + assert.deepStrictEqual( + terminal.getViewport().map((line) => line.trimEnd()), + ["line 6", "line 7", "line 8", "line 9"], + ); + assert.strictEqual(tui.viewportTop, 5); + assert.strictEqual(tui.isFollowingOutput, false); + + text.setText(Array.from({ length: 12 }, (_, index) => `line ${index + 1}`).join("\n")); + tui.requestRender(); + await terminal.waitForRender(); + assert.deepStrictEqual( + terminal.getViewport().map((line) => line.trimEnd()), + ["line 6", "line 7", "line 8", "line 9"], + ); + + tui.stop(); + }); + + it("shows a clickable jump-to-end indicator on the transcript's last row while scrolled up", async () => { + const terminal = new VirtualTerminal(30, 6); + const tui = new TuiAltScreen(terminal, undefined, undefined, { + scrollToEndIndicator: () => "\x1b[7m ↓ Jump to end \x1b[27m", + }); + const transcript = new ScrollView( + new Text(Array.from({ length: 8 }, (_, index) => `line ${index + 1}`).join("\n"), 0, 0), + { follow: "end", primary: true }, + ); + tui.setLayoutRoot( + new VStack([ + { component: transcript, basis: 0, grow: 1, minSize: 1 }, + { component: new Text("editor\nfooter", 0, 0), basis: "auto", minSize: 1 }, + ]), + ); + tui.start(); + await terminal.waitForRender(); + assert.ok(!terminal.getViewport().some((line) => line.includes("Jump to end"))); + + terminal.sendInput("\x1b[<64;1;1M"); + await terminal.waitForRender(); + assert.strictEqual(transcript.isFollowingEnd, false); + assert.strictEqual(terminal.getViewport()[3], "line 7 ↓ Jump to end "); + assert.strictEqual(terminal.getViewport()[4]?.trimEnd(), "editor"); + + // Pressing next to the label starts a selection instead of jumping. + terminal.sendInput("\x1b[<0;2;4M"); + terminal.sendInput("\x1b[<0;2;4m"); + await terminal.waitForRender(); + assert.strictEqual(transcript.isFollowingEnd, false); + + terminal.sendInput("\x1b[<0;15;4M"); + terminal.sendInput("\x1b[<0;15;4m"); + await terminal.waitForRender(); + assert.strictEqual(transcript.isFollowingEnd, true); + assert.deepStrictEqual( + terminal.getViewport().map((line) => line.trimEnd()), + ["line 5", "line 6", "line 7", "line 8", "editor", "footer"], + ); + tui.stop(); + }); + + it("leaves the scrollbar clickable when the jump-to-end indicator spans the transcript", async () => { + const terminal = new VirtualTerminal(30, 6); + const tui = new TuiAltScreen(terminal, undefined, undefined, { + scrollToEndIndicator: () => "↓".repeat(30), + }); + const transcript = new ScrollView( + new Text(Array.from({ length: 12 }, (_, index) => `line ${index + 1}`).join("\n"), 0, 0), + { follow: "end", primary: true, scrollbar: "always" }, + ); + tui.setLayoutRoot( + new VStack([ + { component: transcript, basis: 0, grow: 1, minSize: 1 }, + { component: new Text("editor\nfooter", 0, 0), basis: "auto", minSize: 1 }, + ]), + ); + tui.start(); + await terminal.waitForRender(); + + terminal.sendInput("\x1b[<64;1;1M"); + await terminal.waitForRender(); + assert.strictEqual(transcript.isFollowingEnd, false); + + // The indicator must not intercept a press on the scrollbar's last column. + terminal.sendInput("\x1b[<0;30;4M"); + terminal.sendInput("\x1b[<0;30;4m"); + await terminal.waitForRender(); + assert.strictEqual(transcript.isFollowingEnd, false); + tui.stop(); + }); + + it("never shows the jump-to-end indicator for a primary scroll view without follow-end", async () => { + const terminal = new VirtualTerminal(30, 3); + const tui = new TuiAltScreen(terminal, undefined, undefined, { + scrollToEndIndicator: () => " ↓ Jump to end ", + }); + const transcript = new ScrollView(new Text("one\ntwo\nthree\nfour\nfive", 0, 0), { primary: true }); + tui.setLayoutRoot(transcript); + tui.start(); + await terminal.waitForRender(); + + assert.strictEqual(transcript.isFollowingEnd, false); + assert.ok(!terminal.getViewport().some((line) => line.includes("Jump to end"))); + tui.stop(); + }); + + it("keeps an explicit dock fixed while the transcript scrolls", async () => { + const terminal = new VirtualTerminal(20, 6); + const tui = new TuiAltScreen(terminal); + const transcriptText = new Text(Array.from({ length: 8 }, (_, index) => `line ${index + 1}`).join("\n"), 0, 0); + const transcript = new ScrollView(transcriptText, { follow: "end", primary: true }); + const dock = new VStack([new Text("editor", 0, 0), new Text("footer", 0, 0)]); + tui.setLayoutRoot( + new VStack([ + { component: transcript, basis: 0, grow: 1, minSize: 1 }, + { component: dock, basis: "auto", minSize: 1 }, + ]), + ); + tui.start(); + await terminal.waitForRender(); + + assert.deepStrictEqual( + terminal.getViewport().map((line) => line.trimEnd()), + ["line 5", "line 6", "line 7", "line 8", "editor", "footer"], + ); + + // Wheel over the dock falls back to the primary transcript scroll view. + terminal.sendInput("\x1b[<64;1;6M"); + await terminal.waitForRender(); + assert.deepStrictEqual( + terminal.getViewport().map((line) => line.trimEnd()), + ["line 4", "line 5", "line 6", "line 7", "editor", "footer"], + ); + assert.strictEqual(transcript.isFollowingEnd, false); + + transcriptText.setText(Array.from({ length: 10 }, (_, index) => `line ${index + 1}`).join("\n")); + tui.requestRender(); + await terminal.waitForRender(); + assert.deepStrictEqual( + terminal.getViewport().map((line) => line.trimEnd()), + ["line 4", "line 5", "line 6", "line 7", "editor", "footer"], + ); + + tui.scrollToBottom(); + await terminal.waitForRender(); + assert.deepStrictEqual( + terminal.getViewport().map((line) => line.trimEnd()), + ["line 7", "line 8", "line 9", "line 10", "editor", "footer"], + ); + tui.stop(); + }); + + it("invalidates overlays with an explicit layout root", () => { + const tui = new TuiAltScreen(new VirtualTerminal()); + const overlay = new Text("overlay", 0, 0); + let invalidated = false; + overlay.invalidate = () => { + invalidated = true; + }; + tui.setLayoutRoot(new Text("root", 0, 0)); + tui.showOverlay(overlay); + + tui.invalidate(); + + assert.strictEqual(invalidated, true); + tui.stop(); + }); + + it("routes wheel input to the scroll view under the pointer", async () => { + const terminal = new VirtualTerminal(20, 4); + const tui = new TuiAltScreen(terminal); + const left = new ScrollView(new Text("a1\na2\na3\na4\na5\na6\na7", 0, 0), { + follow: "end", + primary: true, + }); + const right = new ScrollView(new Text("b1\nb2\nb3\nb4\nb5\nb6\nb7", 0, 0), { follow: "end" }); + tui.setLayoutRoot( + new HStack([ + { component: left, basis: 10, shrink: 0 }, + { component: right, basis: 10, shrink: 0 }, + ]), + ); + tui.start(); + await terminal.waitForRender(); + + terminal.sendInput("\x1b[<64;15;1M"); + await terminal.waitForRender(); + assert.strictEqual(left.scrollTop, 3); + assert.strictEqual(right.scrollTop, 2); + assert.deepStrictEqual( + terminal.getViewport().map((line) => line.trimEnd()), + ["a4 b3", "a5 b4", "a6 b5", "a7 b6"], + ); + tui.stop(); + }); + + it("scrolls faster while Alt is held during wheel input", async () => { + const terminal = new VirtualTerminal(20, 4); + const tui = new TuiAltScreen(terminal); + const text = new Text(Array.from({ length: 12 }, (_, index) => `line ${index + 1}`).join("\n"), 0, 0); + tui.addChild(text); + tui.start(); + await terminal.waitForRender(); + assert.strictEqual(tui.viewportTop, 8); + + // Alt modifier sets bit 8 on the wheel button (72 = 64 + 8). + terminal.sendInput("\x1b[<72;1;1M"); + await terminal.waitForRender(); + assert.strictEqual(tui.viewportTop, 3); + tui.stop(); + }); + + it("does not vertically redispatch misses through horizontal layout containers", async () => { + const terminal = new VirtualTerminal(20, 2); + const tui = new TuiAltScreen(terminal); + let selections = 0; + const list = new SelectList( + [ + { value: "a", label: "A" }, + { value: "b", label: "B" }, + ], + 2, + { + selectedPrefix: (text) => text, + selectedText: (text) => text, + description: (text) => text, + scrollInfo: (text) => text, + noMatch: (text) => text, + }, + ); + list.onSelect = () => { + selections += 1; + }; + tui.setLayoutRoot( + new HStack([ + { component: list, basis: 10 }, + { component: new Text("plain", 0, 0), basis: 10 }, + ]), + ); + tui.start(); + await terminal.waitForRender(); + + terminal.sendInput("\x1b[<0;15;1M"); + terminal.sendInput("\x1b[<0;15;1m"); + await terminal.waitForRender(); + assert.strictEqual(selections, 0); + tui.stop(); + }); + + it("uses button-motion tracking inside terminal multiplexers", () => { + const environmentKeys = ["TMUX", "ZELLIJ", "STY", "TERM"] as const; + const previousEnvironment = new Map(environmentKeys.map((key) => [key, process.env[key]])); + try { + for (const key of environmentKeys) delete process.env[key]; + process.env.TERM = "xterm-256color"; + const directTerminal = new RecordingTerminal(); + const directTui = new TuiAltScreen(directTerminal); + directTui.start(); + const directWrites = directTerminal.events + .filter((event): event is { type: "write"; data: string } => event.type === "write") + .map((event) => event.data) + .join(""); + assert.ok(directWrites.includes("\x1b[?1003h")); + directTui.stop(); + + const multiplexers = [ + { name: "tmux environment", environment: { TMUX: "/tmp/tmux/default,1,0" } }, + { name: "tmux TERM", environment: { TERM: "tmux-256color" } }, + { name: "Zellij environment", environment: { ZELLIJ: "0" } }, + { name: "Screen environment", environment: { STY: "123.session" } }, + { name: "Screen TERM", environment: { TERM: "screen-256color" } }, + ]; + for (const { name, environment } of multiplexers) { + for (const key of environmentKeys) delete process.env[key]; + for (const [key, value] of Object.entries(environment)) process.env[key] = value; + const terminal = new RecordingTerminal(); + const tui = new TuiAltScreen(terminal); + tui.start(); + const writes = terminal.events + .filter((event): event is { type: "write"; data: string } => event.type === "write") + .map((event) => event.data) + .join(""); + assert.ok(writes.includes("\x1b[?1002h"), `${name} should enable button-motion tracking`); + assert.ok(!writes.includes("\x1b[?1003h"), `${name} should not enable all-motion tracking`); + assert.ok(writes.includes("\x1b[?1006h"), `${name} should enable SGR mouse encoding`); + tui.stop(); + } + } finally { + for (const key of environmentKeys) { + const value = previousEnvironment.get(key); + if (value === undefined) delete process.env[key]; + else process.env[key] = value; + } + } + }); + + it("invokes the right-click paste handler only on Windows outside VS Code", () => { + const platformDescriptor = Object.getOwnPropertyDescriptor(process, "platform"); + const termProgram = process.env.TERM_PROGRAM; + assert.ok(platformDescriptor); + const terminal = new VirtualTerminal(); + let pasteCount = 0; + const tui = new TuiAltScreen(terminal, undefined, undefined, { + onRightClickPaste: () => { + pasteCount += 1; + }, + }); + try { + Object.defineProperty(process, "platform", { configurable: true, value: "win32" }); + delete process.env.TERM_PROGRAM; + tui.start(); + terminal.sendInput("\x1b[<2;1;1M"); + terminal.sendInput("\x1b[<2;1;1m"); + assert.strictEqual(pasteCount, 1); + + process.env.TERM_PROGRAM = "vscode"; + terminal.sendInput("\x1b[<2;1;1M"); + assert.strictEqual(pasteCount, 1); + + Object.defineProperty(process, "platform", { configurable: true, value: "linux" }); + delete process.env.TERM_PROGRAM; + terminal.sendInput("\x1b[<2;1;1M"); + assert.strictEqual(pasteCount, 1); + } finally { + tui.stop(); + Object.defineProperty(process, "platform", platformDescriptor); + if (termProgram === undefined) delete process.env.TERM_PROGRAM; + else process.env.TERM_PROGRAM = termProgram; + } + }); + + it("reveals an auto scrollbar when the pointer enters its hidden track", async () => { + const terminal = new RecordingTerminal(10, 5); + const tui = new TuiAltScreen(terminal); + const scrollView = new ScrollView( + new Text(Array.from({ length: 20 }, (_, index) => `line ${index + 1}`).join("\n"), 0, 0), + { primary: true, scrollbar: "auto", scrollbarHideDelayMs: 20 }, + ); + tui.setLayoutRoot(scrollView); + tui.start(); + await terminal.waitForRender(); + assert.strictEqual(scrollView.isScrollbarVisible, false); + + terminal.sendInput("\x1b[<35;10;3M"); + await terminal.waitForRender(); + assert.strictEqual(scrollView.isScrollbarVisible, true); + assert.strictEqual(scrollView.isScrollbarActive, true); + assert.ok(terminal.getViewport().some((line) => /[│█]/.test(line))); + + terminal.sendInput("\x1b[<35;9;3M"); + await new Promise((resolve) => setTimeout(resolve, 40)); + await terminal.waitForRender(); + assert.strictEqual(scrollView.isScrollbarVisible, false); + tui.stop(); + }); + + it("jumps to a scrollbar track position and continues dragging from there", async () => { + const terminal = new RecordingTerminal(10, 10); + const tui = new TuiAltScreen(terminal); + const scrollView = new ScrollView( + new Text(Array.from({ length: 50 }, (_, index) => `line ${index + 1}`).join("\n"), 0, 0), + { primary: true, scrollbar: "always" }, + ); + tui.setLayoutRoot(scrollView); + tui.start(); + await terminal.waitForRender(); + assert.strictEqual(scrollView.scrollTop, 0); + + terminal.sendInput("\x1b[<0;10;6M"); + await terminal.waitForRender(); + assert.strictEqual(scrollView.scrollTop, 20); + + terminal.sendInput("\x1b[<32;10;10M"); + await terminal.waitForRender(); + assert.strictEqual(scrollView.scrollTop, 40); + + terminal.sendInput("\x1b[<0;10;10m"); + await terminal.waitForRender(); + assert.ok(terminal.events.every((event) => event.type !== "write" || !event.data.includes("\x1b]52;c;"))); + tui.stop(); + }); + + it("chains unused wheel delta to an outer scroll view", async () => { + const terminal = new VirtualTerminal(20, 4); + const tui = new TuiAltScreen(terminal, undefined, undefined, { wheelScrollLines: 3 }); + const inner = new ScrollView(new Text("i1\ni2\ni3\ni4\ni5\ni6", 0, 0)); + const outer = new ScrollView( + new VStack([{ component: inner, basis: 2 }, new Text("tail1\ntail2\ntail3\ntail4\ntail5", 0, 0)]), + { primary: true }, + ); + tui.setLayoutRoot(outer); + tui.start(); + await terminal.waitForRender(); + + terminal.sendInput("\x1b[<65;1;1M"); + await terminal.waitForRender(); + assert.strictEqual(inner.scrollTop, 3); + assert.strictEqual(outer.scrollTop, 0); + + terminal.sendInput("\x1b[<65;1;1M"); + await terminal.waitForRender(); + assert.strictEqual(inner.scrollTop, 4); + assert.strictEqual(outer.scrollTop, 2); + tui.stop(); + }); + + it("supports configurable keyboard viewport navigation with four rows of page overlap", async () => { + const terminal = new VirtualTerminal(20, 8); + const tui = new TuiAltScreen(terminal); + tui.addChild(new Text(Array.from({ length: 12 }, (_, index) => `line ${index + 1}`).join("\n"), 0, 0)); + tui.start(); + await terminal.waitForRender(); + + terminal.sendInput("\x1b[57421u"); + terminal.sendInput("\x1b[57421;1:3u"); + await terminal.waitForRender(); + assert.deepStrictEqual( + terminal.getViewport().map((line) => line.trimEnd()), + ["line 1", "line 2", "line 3", "line 4", "line 5", "line 6", "line 7", "line 8"], + ); + + terminal.sendInput("\x1b[57422u"); + terminal.sendInput("\x1b[57422;1:3u"); + await terminal.waitForRender(); + assert.deepStrictEqual( + terminal.getViewport().map((line) => line.trimEnd()), + ["line 5", "line 6", "line 7", "line 8", "line 9", "line 10", "line 11", "line 12"], + ); + + terminal.sendInput("\x1bOH"); + await terminal.waitForRender(); + assert.deepStrictEqual( + terminal.getViewport().map((line) => line.trimEnd()), + ["line 1", "line 2", "line 3", "line 4", "line 5", "line 6", "line 7", "line 8"], + ); + + terminal.sendInput("\x1bOF"); + await terminal.waitForRender(); + assert.deepStrictEqual( + terminal.getViewport().map((line) => line.trimEnd()), + ["line 5", "line 6", "line 7", "line 8", "line 9", "line 10", "line 11", "line 12"], + ); + + tui.stop(); + }); + + it("searches normalized rendered transcript text across rows", () => { + assert.deepStrictEqual(findAltScreenSearchMatches(["alpha QUICK", "brown fox"], "quick brown"), [ + { + segments: [ + { row: 0, startCol: 6, endCol: 11 }, + { row: 1, startCol: 0, endCol: 5 }, + ], + }, + ]); + }); + + it("maps normalized ASCII and Unicode search matches back to rendered columns", () => { + assert.deepStrictEqual(findAltScreenSearchMatches(["\x1b[31mfoo bar\x1b[0m", "A界🙂éZ"], "oo bar\nA界🙂é"), [ + { + segments: [ + { row: 0, startCol: 1, endCol: 3 }, + { row: 0, startCol: 5, endCol: 8 }, + { row: 1, startCol: 0, endCol: 6 }, + ], + }, + ]); + }); + + it("reuses indexed transcript matches until the query or rendered lines change", () => { + const index = new AltScreenSearchIndex(); + const initial = index.search(["alpha needle", "omega"], "needle"); + assert.strictEqual(initial.changed, true); + assert.strictEqual(initial.matches.length, 1); + + const cached = index.search(["alpha needle", "omega"], "needle"); + assert.strictEqual(cached.changed, false); + assert.strictEqual(cached.matches, initial.matches); + + const changedQuery = index.search(["alpha needle", "omega"], "omega"); + assert.strictEqual(changedQuery.changed, true); + assert.notStrictEqual(changedQuery.matches, initial.matches); + assert.deepStrictEqual(changedQuery.matches[0]?.segments, [{ row: 1, startCol: 0, endCol: 5 }]); + + const changedLines = index.search(["alpha needle", "no match"], "omega"); + assert.strictEqual(changedLines.changed, true); + assert.deepStrictEqual(changedLines.matches, []); + }); + + it("renders transcript search with a muted placeholder and right-aligned controls", () => { + const component = new AltScreenSearchComponent(() => {}); + const rendered = component.render(48); + const lines = rendered.map((line) => stripTerminalSequences(line)); + + assert.strictEqual(lines.length, 3); + assert.ok(lines.every((line) => visibleWidth(line) === 48)); + assert.match(lines[0] ?? "", /^┌─+┐$/); + assert.match(lines[1] ?? "", /^│ Find in transcript +│$/); + assert.ok(rendered[1]?.includes("\x1b[2m")); + assert.match(lines[2] ?? "", /^└─+ ↑ Shift\+Enter · ↓ Enter ─┘$/); + const controls = lines[2] ?? ""; + assert.strictEqual(component.getNavigationDirectionAt(2, controls.indexOf("↑")), -1); + assert.strictEqual(component.getNavigationDirectionAt(2, controls.indexOf("Shift+Enter") + 5), -1); + assert.strictEqual(component.getNavigationDirectionAt(2, controls.indexOf("·")), undefined); + assert.strictEqual(component.getNavigationDirectionAt(2, controls.indexOf("↓")), 1); + assert.strictEqual(component.getNavigationDirectionAt(2, controls.lastIndexOf("Enter") + 2), 1); + + component.handleInput("n"); + component.setResult(0, 2); + const populatedRender = component.render(48); + const populated = populatedRender.map((line) => stripTerminalSequences(line)); + assert.ok(populated[1]?.includes("n")); + assert.ok(populated[1]?.includes("1/2")); + assert.ok(populatedRender[1]?.includes("\x1b[2m 1/2 \x1b[22m")); + assert.ok(!populated.some((line) => line.includes("Find in transcript"))); + }); + + it("navigates transcript search with hoverable arrow buttons and toggles it with its shortcut", async () => { + const terminal = new RecordingTerminal(120, 6); + const tui = new TuiAltScreen(terminal, undefined, undefined, { + searchNavigationButtonStyle: (text, hovered) => `${hovered ? "\x1b[45m" : "\x1b[44m"}${text}\x1b[49m`, + }); + tui.addChild(new Text("needle one\nmiddle\nneedle two\nend", 0, 0)); + tui.start(); + await terminal.waitForRender(); + + terminal.sendInput("\x1b[102;6u"); + terminal.sendInput("needle"); + await terminal.waitForRender(); + let viewport = terminal.getViewport(); + assert.ok(viewport.some((line) => line.includes("1/2"))); + assert.ok(viewport.some((line) => line.includes("↑ Shift+Enter · ↓ Enter"))); + + let arrowRow = viewport.findIndex((line) => line.includes("↑") && line.includes("↓")); + let arrowColumn = viewport[arrowRow]?.lastIndexOf("Enter") ?? -1; + assert.ok(arrowRow >= 0 && arrowColumn >= 0); + const hoverEventCount = terminal.events.length; + terminal.sendInput(`\x1b[<35;${arrowColumn + 1};${arrowRow + 1}M`); + await terminal.waitForRender(); + assert.ok( + terminal.events + .slice(hoverEventCount) + .some((event) => event.type === "write" && event.data.includes("\x1b[45m↓ Enter\x1b[49m")), + ); + terminal.sendInput(`\x1b[<0;${arrowColumn + 1};${arrowRow + 1}M`); + await terminal.waitForRender(); + viewport = terminal.getViewport(); + assert.ok(viewport.some((line) => line.includes("2/2"))); + assert.ok(viewport.some((line) => line.includes("↑ Shift+Enter · ↓ Enter"))); + + arrowRow = viewport.findIndex((line) => line.includes("↑") && line.includes("↓")); + arrowColumn = (viewport[arrowRow]?.indexOf("Shift+Enter") ?? -3) + 3; + assert.ok(arrowRow >= 0 && arrowColumn >= 0); + terminal.sendInput(`\x1b[<0;${arrowColumn + 1};${arrowRow + 1}M`); + await terminal.waitForRender(); + assert.ok(terminal.getViewport().some((line) => line.includes("1/2"))); + assert.ok(terminal.getViewport().some((line) => line.includes("↑ Shift+Enter · ↓ Enter"))); + + terminal.sendInput("\x1b[102;6u"); + await terminal.waitForRender(); + assert.ok(!terminal.getViewport().some((line) => line.includes("↑ Shift+Enter · ↓ Enter"))); + tui.stop(); + }); + + it("does not treat transcript box drawing as search navigation buttons", async () => { + const terminal = new VirtualTerminal(80, 10); + const tui = new TuiAltScreen(terminal); + tui.addChild( + new Text( + [ + "needle one", + "middle", + "needle two", + "filler", + "┌────────────────────────────────────────┐", + "│ box │", + "└────────────────────────────────────────┘", + "end", + ].join("\n"), + 0, + 0, + ), + ); + tui.start(); + await terminal.waitForRender(); + + terminal.sendInput("\x1b[102;6u"); + terminal.sendInput("needle"); + await terminal.waitForRender(); + let viewport = terminal.getViewport(); + assert.ok(viewport.some((line) => line.includes("1/2"))); + assert.ok(!viewport.some((line) => line.includes("2/2"))); + + const boxBottomRow = viewport.findIndex((line) => line.startsWith("└")); + assert.ok(boxBottomRow >= 0); + terminal.sendInput(`\x1b[<0;24;${boxBottomRow + 1}M`); + await terminal.waitForRender(); + + viewport = terminal.getViewport(); + assert.ok(viewport.some((line) => line.includes("1/2"))); + assert.ok(!viewport.some((line) => line.includes("2/2"))); + tui.stop(); + }); + + it("uses configured styles for current and non-current search matches", async () => { + const terminal = new RecordingTerminal(60, 4); + const tui = new TuiAltScreen(terminal, undefined, undefined, { + searchMatchStyle: (text) => `\x1b[41m${text}\x1b[49m`, + searchCurrentMatchStyle: (text) => `\x1b[42m${text}\x1b[49m`, + }); + tui.addChild(new Text("needle first\nmiddle\nneedle second\nend", 0, 0)); + tui.start(); + await terminal.waitForRender(); + + terminal.sendInput("\x1b[102;6u"); + terminal.sendInput("needle"); + await terminal.waitForRender(); + + assert.ok( + terminal.events.some((event) => event.type === "write" && event.data.includes("\x1b[42mneedle\x1b[49m")), + ); + assert.ok( + terminal.events.some((event) => event.type === "write" && event.data.includes("\x1b[41mneedle\x1b[49m")), + ); + tui.stop(); + }); + + it("searches the transcript with Ctrl+Shift+F and restores editor focus on close", async () => { + const terminal = new RecordingTerminal(60, 8); + const tui = new TuiAltScreen(terminal); + const transcriptText = new Text( + Array.from({ length: 12 }, (_, index) => { + if (index === 4) return "line 5 needle one"; + if (index === 9) return "line 10 needle two"; + return `line ${index + 1}`; + }).join("\n"), + 0, + 0, + ); + const transcript = new ScrollView(transcriptText, { follow: "end", primary: true }); + const editorInputs: string[] = []; + const editor = { + focused: false, + render: () => ["editor"], + invalidate: () => {}, + handleInput: (data: string) => editorInputs.push(data), + }; + tui.setLayoutRoot( + new VStack([ + { component: transcript, basis: 0, grow: 1, minSize: 1 }, + { component: editor, basis: 1, shrink: 0 }, + ]), + ); + tui.setFocus(editor); + tui.start(); + await terminal.waitForRender(); + + terminal.sendInput("\x1b[102;6u"); + terminal.sendInput("needle"); + await terminal.waitForRender(); + assert.strictEqual(transcript.isFollowingEnd, false); + assert.ok(terminal.getViewport().some((line) => line.includes("2/2"))); + assert.ok(terminal.getViewport().some((line) => line.includes("↑ Shift+Enter · ↓ Enter"))); + assert.ok(terminal.getViewport().some((line) => line.includes("line 10 needle two"))); + assert.deepStrictEqual(editorInputs, []); + assert.ok( + terminal.events.some((event) => event.type === "write" && event.data.includes("\x1b[1;7mneedle\x1b[22;27m")), + ); + + for (let index = 0; index < 6; index++) terminal.sendInput("\x1b[<64;1;4M"); + await terminal.waitForRender(); + assert.strictEqual(transcript.scrollTop, 0); + assert.ok(terminal.getViewport().some((line) => line.includes("needle") && line.includes("2/2"))); + + terminal.sendInput("\x07"); + await terminal.waitForRender(); + assert.ok(terminal.getViewport().some((line) => line.includes("1/2"))); + assert.ok(terminal.getViewport().some((line) => line.includes("line 5 needle one"))); + + terminal.sendInput("\x1b[103;6u"); + await terminal.waitForRender(); + assert.ok(terminal.getViewport().some((line) => line.includes("2/2"))); + assert.ok(terminal.getViewport().some((line) => line.includes("line 10 needle two"))); + + terminal.sendInput("\x1b"); + terminal.sendInput("x"); + await terminal.waitForRender(); + assert.ok(!terminal.getViewport().some((line) => line.includes("↑ Shift+Enter · ↓ Enter"))); + assert.deepStrictEqual(editorInputs, ["x"]); + + tui.stop(); + }); + + it("scrolls the transcript by half a page with custom bindings", async () => { + const originalKeybindings = getKeybindings(); + const terminal = new VirtualTerminal(20, 10); + const tui = new TuiAltScreen(terminal); + setKeybindings( + new KeybindingsManager(TUI_KEYBINDINGS, { + "tui.altScreen.halfPageUp": "ctrl+u", + "tui.altScreen.halfPageDown": "ctrl+d", + }), + ); + try { + tui.addChild(new Text(Array.from({ length: 30 }, (_, index) => `line ${index + 1}`).join("\n"), 0, 0)); + tui.start(); + await terminal.waitForRender(); + assert.strictEqual(tui.viewportTop, 20); + + terminal.sendInput("\x15"); + await terminal.waitForRender(); + assert.strictEqual(tui.viewportTop, 15); + + terminal.sendInput("\x04"); + await terminal.waitForRender(); + assert.strictEqual(tui.viewportTop, 20); + } finally { + tui.stop(); + setKeybindings(originalKeybindings); + } + }); + + it("scrolls the transcript by one line with custom bindings", async () => { + const originalKeybindings = getKeybindings(); + const terminal = new VirtualTerminal(20, 10); + const tui = new TuiAltScreen(terminal); + setKeybindings( + new KeybindingsManager(TUI_KEYBINDINGS, { + "tui.altScreen.lineUp": "ctrl+y", + "tui.altScreen.lineDown": "ctrl+e", + }), + ); + try { + tui.addChild(new Text(Array.from({ length: 30 }, (_, index) => `line ${index + 1}`).join("\n"), 0, 0)); + tui.start(); + await terminal.waitForRender(); + assert.strictEqual(tui.viewportTop, 20); + + terminal.sendInput("\x19"); + await terminal.waitForRender(); + assert.strictEqual(tui.viewportTop, 19); + + terminal.sendInput("\x05"); + await terminal.waitForRender(); + assert.strictEqual(tui.viewportTop, 20); + } finally { + tui.stop(); + setKeybindings(originalKeybindings); + } + }); + + it("routes Ctrl-modified viewport navigation to the focused component", async () => { + const terminal = new VirtualTerminal(20, 6); + const tui = new TuiAltScreen(terminal); + const transcript = new ScrollView( + new Text(Array.from({ length: 12 }, (_, index) => `line ${index + 1}`).join("\n"), 0, 0), + { follow: "end", primary: true }, + ); + const editorInputs: string[] = []; + const editor = { + focused: false, + render: () => ["editor"], + invalidate: () => {}, + handleInput: (data: string) => editorInputs.push(data), + }; + tui.setLayoutRoot( + new VStack([ + { component: transcript, basis: 0, grow: 1, minSize: 1 }, + { component: editor, basis: 1, shrink: 0 }, + ]), + ); + tui.setFocus(editor); + tui.start(); + await terminal.waitForRender(); + + terminal.sendInput("\x1bOH"); + await terminal.waitForRender(); + assert.strictEqual(transcript.scrollTop, 0); + assert.deepStrictEqual(editorInputs, []); + + const modifiedInputs = ["\x1b[1;5H", "\x1b[1;5F", "\x1b[5;5~", "\x1b[6;5~", "\x1b[57423;5u"]; + for (const input of modifiedInputs) terminal.sendInput(input); + terminal.sendInput("\x1b[57423;5:3u"); + await terminal.waitForRender(); + assert.strictEqual(transcript.scrollTop, 0); + assert.deepStrictEqual(editorInputs, modifiedInputs); + + terminal.sendInput("\x1b[6~"); + await terminal.waitForRender(); + assert.strictEqual(transcript.scrollTop, 1); + assert.deepStrictEqual(editorInputs, modifiedInputs); + + tui.stop(); + }); + + it("jumps between OSC 133 semantic prompt markers", async () => { + const terminal = new VirtualTerminal(20, 3); + const tui = new TuiAltScreen(terminal); + tui.addChild( + new Text( + [1, 2, 3, 4].flatMap((message) => [`${OSC133_ZONE_START}message ${message}`, "detail"]).join("\n"), + 0, + 0, + ), + ); + tui.start(); + await terminal.waitForRender(); + assert.strictEqual(tui.viewportTop, 5); + + terminal.sendInput("\x1b[57419;6u"); + terminal.sendInput("\x1b[57419;6:3u"); + await terminal.waitForRender(); + assert.strictEqual(tui.viewportTop, 4); + assert.strictEqual(terminal.getViewport()[0]?.trimEnd(), "message 3"); + + terminal.sendInput("\x1b[1;6A"); + await terminal.waitForRender(); + assert.strictEqual(tui.viewportTop, 2); + assert.strictEqual(terminal.getViewport()[0]?.trimEnd(), "message 2"); + + terminal.sendInput("\x1b[57420;6u"); + terminal.sendInput("\x1b[57420;6:3u"); + await terminal.waitForRender(); + assert.strictEqual(tui.viewportTop, 4); + assert.strictEqual(terminal.getViewport()[0]?.trimEnd(), "message 3"); + + terminal.sendInput("\x1b[1;6B"); + await terminal.waitForRender(); + assert.strictEqual(tui.viewportTop, 5); + assert.strictEqual(terminal.getViewport()[1]?.trimEnd(), "message 4"); + assert.strictEqual(tui.isFollowingOutput, true); + + tui.stop(); + }); + + it("does not emit Kitty graphics commands or OSC 133 zones in iTerm2", async () => { + setCapabilities({ images: "iterm2", trueColor: true, hyperlinks: true }); + try { + const terminal = new RecordingTerminal(20, 3); + const tui = new TuiAltScreen(terminal); + tui.addChild({ + render: () => ["\x1b]133;B\x07\x1b]133;C\x07\x1b]133;A\x07content"], + invalidate: () => {}, + }); + tui.addChild( + new Image( + "AAAA", + "image/png", + { fallbackColor: (value) => value }, + { filename: "example.png" }, + { widthPx: 10, heightPx: 10 }, + ), + ); + tui.start(); + await terminal.waitForRender(); + tui.stop(); + assert.ok(terminal.events.every((event) => event.type !== "write" || !event.data.includes("\x1b_G"))); + assert.ok(terminal.events.every((event) => event.type !== "write" || !event.data.includes("\x1b]133;"))); + assert.ok(terminal.events.every((event) => event.type !== "write" || !event.data.includes("\x1b]1337;File="))); + assert.ok(terminal.events.some((event) => event.type === "write" && event.data.includes("[Image:"))); + } finally { + resetCapabilitiesCache(); + } + }); + + it("clears stale iTerm2 image placements when they leave the viewport", async () => { + setCapabilities({ images: "iterm2", trueColor: true, hyperlinks: true }); + try { + const terminal = new RecordingTerminal(20, 3); + const tui = new TuiAltScreen(terminal); + const imageLine = "\x1b]1337;File=inline=1;width=2;height=auto:AAAA\x07"; + tui.addChild({ + render: () => [imageLine, "", "", "after", "more", "end"], + invalidate: () => {}, + }); + tui.start(); + await terminal.waitForRender(); + tui.scrollToTop(); + await terminal.waitForRender(); + const eventCount = terminal.events.length; + + tui.scrollBy(1); + await terminal.waitForRender(); + assert.ok( + terminal.events.slice(eventCount).some((event) => event.type === "write" && event.data.includes("\x1b[2J")), + ); + tui.stop(); + } finally { + resetCapabilitiesCache(); + } + }); + + it("crops a Kitty image whose first line is above the viewport", async () => { + const terminal = new RecordingTerminal(20, 3); + const tui = new TuiAltScreen(terminal); + const imageId = 123; + const imageLine = encodeKitty("AAAA", { columns: 2, rows: 3, imageId, moveCursor: false }); + registerKittyImageMetadata({ imageId, columns: 2, rows: 3, widthPx: 100, heightPx: 100 }); + tui.addChild({ + render: () => ["before", imageLine, "", "", "after", "end"], + invalidate: () => {}, + }); + tui.start(); + await terminal.waitForRender(); + + assert.strictEqual(tui.viewportTop, 3); + assert.ok( + terminal.events.some( + (event) => event.type === "write" && event.data.includes("i=123") && event.data.includes("y=66,h=34,r=1"), + ), + ); + + tui.stop(); + }); + + it("reuses moved Kitty images without dropping HStack siblings", async () => { + setCapabilities({ images: "kitty", trueColor: true, hyperlinks: true }); + try { + const terminal = new RecordingTerminal(20, 6); + const tui = new TuiAltScreen(terminal); + const label = new Text("left", 0, 0); + const image = new Image( + "A".repeat(8192), + "image/png", + { fallbackColor: (value) => value }, + {}, + { widthPx: 100, heightPx: 100 }, + ); + const header = new Text("header", 0, 0); + const row = new HStack([ + { component: label, basis: 10 }, + { component: image, basis: 10 }, + ]); + tui.setLayoutRoot( + new VStack([ + { component: header, basis: "auto" }, + { component: row, basis: 4 }, + ]), + ); + tui.start(); + await terminal.waitForRender(); + assert.ok(terminal.events.some((event) => event.type === "write" && event.data.includes("\x1b_Ga=T"))); + + const eventCount = terminal.events.length; + label.setText("changed"); + header.setText("header\nsecond"); + tui.requestRender(); + await terminal.waitForRender(); + const redrawWrites = terminal.events + .slice(eventCount) + .filter((event): event is { type: "write"; data: string } => event.type === "write") + .map((event) => event.data) + .join(""); + const placementIndex = redrawWrites.indexOf("\x1b_Ga=p,q=2"); + assert.ok(redrawWrites.includes("\x1b_Ga=d,d=a,q=2\x1b\\")); + assert.ok(placementIndex > redrawWrites.indexOf("changed")); + assert.ok(!redrawWrites.includes("\x1b_Ga=T")); + assert.ok(redrawWrites.length < 2000, `expected placement-only redraw, got ${redrawWrites.length} bytes`); + assert.ok(terminal.getViewport().some((line) => line.trimEnd() === "changed")); + tui.stop(); + } finally { + resetCapabilitiesCache(); + } + }); + + it("retains recently offscreen Kitty images for placement-only reuse", async () => { + setCapabilities({ images: "kitty", trueColor: true, hyperlinks: true }); + try { + const terminal = new RecordingTerminal(20, 1); + const tui = new TuiAltScreen(terminal); + const imageId = 321; + const imageLine = encodeKitty("AAAA", { columns: 2, rows: 1, imageId, moveCursor: false }); + registerKittyImageMetadata({ imageId, columns: 2, rows: 1, widthPx: 100, heightPx: 50 }); + tui.setLayoutRoot( + new ScrollView( + { + render: () => [imageLine, "after"], + invalidate: () => {}, + }, + { primary: true }, + ), + ); + tui.start(); + await terminal.waitForRender(); + assert.ok(terminal.events.some((event) => event.type === "write" && event.data.includes("\x1b_Ga=T"))); + + const eventCount = terminal.events.length; + tui.scrollBy(1); + await terminal.waitForRender(); + tui.scrollBy(-1); + await terminal.waitForRender(); + const reentryWrites = terminal.events + .slice(eventCount) + .filter((event): event is { type: "write"; data: string } => event.type === "write") + .map((event) => event.data) + .join(""); + assert.ok(reentryWrites.includes("\x1b_Ga=p,q=2")); + assert.ok(!reentryWrites.includes("\x1b_Ga=T")); + assert.ok(!reentryWrites.includes(`\x1b_Ga=d,d=I,i=${imageId},q=2\x1b\\`)); + tui.stop(); + } finally { + resetCapabilitiesCache(); + } + }); + + it("evicts the least recently visible Kitty image when the cache is full", async () => { + setCapabilities({ images: "kitty", trueColor: true, hyperlinks: true }); + try { + const terminal = new RecordingTerminal(20, 1); + const tui = new TuiAltScreen(terminal); + const firstImageId = 500; + const imageLines = Array.from({ length: 18 }, (_, index) => { + const imageId = firstImageId + index; + registerKittyImageMetadata({ imageId, columns: 2, rows: 1, widthPx: 100, heightPx: 50 }); + return encodeKitty("AAAA", { columns: 2, rows: 1, imageId, moveCursor: false }); + }); + tui.setLayoutRoot( + new ScrollView( + { + render: () => imageLines, + invalidate: () => {}, + }, + { primary: true }, + ), + ); + tui.start(); + await terminal.waitForRender(); + for (let index = 1; index < imageLines.length; index++) { + tui.scrollBy(1); + await terminal.waitForRender(); + } + assert.ok( + terminal.events.some( + (event) => event.type === "write" && event.data.includes(`\x1b_Ga=d,d=I,i=${firstImageId},q=2\x1b\\`), + ), + ); + + const eventCount = terminal.events.length; + tui.scrollToTop(); + await terminal.waitForRender(); + const reentryWrites = terminal.events + .slice(eventCount) + .filter((event): event is { type: "write"; data: string } => event.type === "write") + .map((event) => event.data) + .join(""); + assert.ok(reentryWrites.includes("\x1b_Ga=T")); + tui.stop(); + } finally { + resetCapabilitiesCache(); + } + }); + + it("evicts offscreen Kitty images when decoded raster memory exceeds the cache quota", async () => { + setCapabilities({ images: "kitty", trueColor: true, hyperlinks: true }); + try { + const terminal = new RecordingTerminal(20, 1); + const tui = new TuiAltScreen(terminal); + const firstImageId = 600; + const imageLines = Array.from({ length: 4 }, (_, index) => { + const imageId = firstImageId + index; + registerKittyImageMetadata({ imageId, columns: 2, rows: 1, widthPx: 3840, heightPx: 2160 }); + return encodeKitty("AAAA", { columns: 2, rows: 1, imageId, moveCursor: false }); + }); + tui.setLayoutRoot( + new ScrollView( + { + render: () => imageLines, + invalidate: () => {}, + }, + { primary: true }, + ), + ); + tui.start(); + await terminal.waitForRender(); + for (let index = 1; index < imageLines.length; index++) { + tui.scrollBy(1); + await terminal.waitForRender(); + } + assert.ok( + terminal.events.some( + (event) => event.type === "write" && event.data.includes(`\x1b_Ga=d,d=I,i=${firstImageId},q=2\x1b\\`), + ), + ); + tui.stop(); + } finally { + resetCapabilitiesCache(); + } + }); + + it("opens an OSC 8 hyperlink with specific or generic release codes, but not on drag", async () => { + const terminal = new RecordingTerminal(20, 3); + const openedUrls: string[] = []; + const tui = new TuiAltScreen(terminal, undefined, undefined, { + openUrl: (url) => openedUrls.push(url), + }); + const url = "https://example.com/path?q=1"; + const belUrl = "https://example.com/bel"; + const emojiUrl = "https://example.com/emoji"; + tui.addChild( + new Text( + `${hyperlink("link", url)}\n\x1b]8;;${belUrl}\x07link\x1b]8;;\x07\n${hyperlink("🙂", emojiUrl)}`, + 0, + 0, + ), + ); + tui.start(); + await terminal.waitForRender(); + + terminal.sendInput("\x1b[<0;2;1M"); + terminal.sendInput("\x1b[<3;2;1m"); + await terminal.waitForRender(); + assert.deepStrictEqual(openedUrls, [url]); + + terminal.sendInput("\x1b[<0;2;2M"); + terminal.sendInput("\x1b[<0;2;2m"); + await terminal.waitForRender(); + assert.deepStrictEqual(openedUrls, [url, belUrl]); + + terminal.sendInput("\x1b[<0;2;3M"); + terminal.sendInput("\x1b[<0;2;3m"); + await terminal.waitForRender(); + assert.deepStrictEqual(openedUrls, [url, belUrl, emojiUrl]); + + terminal.sendInput("\x1b[<0;2;1M"); + terminal.sendInput("\x1b[<32;4;1M"); + terminal.sendInput("\x1b[<0;4;1m"); + await terminal.waitForRender(); + assert.deepStrictEqual(openedUrls, [url, belUrl, emojiUrl]); + + tui.stop(); + }); + + it("selects visible text with the mouse and copies it with OSC 52 after a generic release", async () => { + const terminal = new RecordingTerminal(20, 4); + const tui = new TuiAltScreen(terminal); + tui.addChild(new Text("\x1b[1mal\x1b[0mpha\nbeta\ngamma\ndelta", 0, 0)); + tui.start(); + await terminal.waitForRender(); + + terminal.sendInput("\x1b[<0;1;1M"); + terminal.sendInput("\x1b[<32;4;2M"); + terminal.sendInput("\x1b[<3;4;2m"); + await terminal.waitForRender(); + + const expectedClipboardSequence = `\x1b]52;c;${Buffer.from("alpha\nbeta").toString("base64")}\x07`; + const clipboardWrites = terminal.events.filter( + (event) => event.type === "write" && event.data.includes("\x1b]52;c;"), + ); + assert.ok( + clipboardWrites.some((event) => event.type === "write" && event.data.includes(expectedClipboardSequence)), + JSON.stringify(clipboardWrites), + ); + assert.ok(terminal.events.some((event) => event.type === "write" && event.data.includes("\x1b[7m"))); + assert.ok( + terminal.events.some((event) => event.type === "write" && event.data.includes("al\x1b[0m\x1b[7mpha")), + "selection inverse must be reapplied after a reset inside the selection", + ); + assert.ok(terminal.getViewport().some((line) => line.includes("Copied!"))); + + tui.stop(); + }); + + it("uses an injected copySelection handler instead of OSC 52 and reports success", async () => { + const terminal = new RecordingTerminal(20, 4); + const copied: string[] = []; + const tui = new TuiAltScreen(terminal, undefined, undefined, { + copySelection: async (text) => { + copied.push(text); + return true; + }, + }); + tui.addChild(new Text("alpha\nbeta\ngamma\ndelta", 0, 0)); + tui.start(); + await terminal.waitForRender(); + + terminal.sendInput("\x1b[<0;1;1M"); + terminal.sendInput("\x1b[<32;4;2M"); + terminal.sendInput("\x1b[<0;4;2m"); + await terminal.waitForRender(); + + assert.deepStrictEqual(copied, ["alpha\nbeta"]); + assert.ok( + terminal.events.every((event) => event.type !== "write" || !event.data.includes("\x1b]52;c;")), + "must not emit OSC 52 when a copySelection handler is provided", + ); + assert.ok(terminal.getViewport().some((line) => line.includes("Copied!"))); + + tui.stop(); + }); + + it("leaves selections visible without copying when copyOnSelect is disabled", async () => { + const terminal = new RecordingTerminal(20, 4); + const copied: string[] = []; + const tui = new TuiAltScreen(terminal, undefined, undefined, { + copyOnSelect: false, + copySelection: async (text) => { + copied.push(text); + return true; + }, + }); + tui.addChild(new Text("alpha\nbeta\ngamma\ndelta", 0, 0)); + tui.start(); + await terminal.waitForRender(); + + terminal.sendInput("\x1b[<0;1;1M"); + terminal.sendInput("\x1b[<32;4;2M"); + terminal.sendInput("\x1b[<0;4;2m"); + await terminal.waitForRender(); + + assert.deepStrictEqual(copied, []); + assert.strictEqual(tui.hasActiveSelection(), true); + assert.ok(terminal.events.some((event) => event.type === "write" && event.data.includes("\x1b[7m"))); + assert.ok(terminal.getViewport().every((line) => !line.includes("Copied!"))); + + tui.stop(); + }); + + it("copies an active selection programmatically", async () => { + const terminal = new RecordingTerminal(20, 4); + const copied: string[] = []; + const tui = new TuiAltScreen(terminal, undefined, undefined, { + copySelection: async (text) => { + copied.push(text); + return true; + }, + }); + tui.addChild(new Text("alpha\nbeta\ngamma\ndelta", 0, 0)); + tui.start(); + await terminal.waitForRender(); + + assert.strictEqual(tui.hasActiveSelection(), false); + assert.strictEqual(await tui.copyActiveSelectionToClipboard(), false); + + terminal.sendInput("\x1b[<0;1;1M"); + terminal.sendInput("\x1b[<32;4;2M"); + terminal.sendInput("\x1b[<0;4;2m"); + await terminal.waitForRender(); + + assert.deepStrictEqual(copied, ["alpha\nbeta"]); + assert.strictEqual(tui.hasActiveSelection(), true); + + copied.length = 0; + assert.strictEqual(await tui.copyActiveSelectionToClipboard(), true); + await terminal.waitForRender(); + + assert.deepStrictEqual(copied, ["alpha\nbeta"]); + assert.ok(terminal.getViewport().some((line) => line.includes("Copied!"))); + + tui.stop(); + }); + + it("flashes an error when the injected copySelection handler fails", async () => { + const terminal = new RecordingTerminal(20, 4); + const tui = new TuiAltScreen(terminal, undefined, undefined, { + copySelection: async () => false, + }); + tui.addChild(new Text("alpha\nbeta\ngamma\ndelta", 0, 0)); + tui.start(); + await terminal.waitForRender(); + + terminal.sendInput("\x1b[<0;1;1M"); + terminal.sendInput("\x1b[<32;4;2M"); + terminal.sendInput("\x1b[<0;4;2m"); + await terminal.waitForRender(); + + assert.ok(terminal.getViewport().some((line) => line.includes("Copy failed"))); + assert.ok( + terminal.events.every((event) => event.type !== "write" || !event.data.includes("\x1b]52;c;")), + "must not emit OSC 52 when a copySelection handler is provided", + ); + + tui.stop(); + }); + + it("does not append whitespace to double-click word highlighting", async () => { + const terminal = new RecordingTerminal(20, 1); + const tui = new TuiAltScreen(terminal); + tui.addChild(new Text("foo bar", 0, 0)); + tui.start(); + await terminal.waitForRender(); + + terminal.sendInput("\x1b[<0;1;1M"); + terminal.sendInput("\x1b[<0;1;1m"); + terminal.sendInput("\x1b[<0;3;1M"); + await terminal.waitForRender(); + + assert.ok(terminal.events.some((event) => event.type === "write" && event.data.includes("foo\x1b[27m"))); + tui.stop(); + }); + + it("coalesces slash and hyphen separated segments for double-click word selection", async () => { + for (const { line, needle } of [ + { line: "extensions/starline/fixed-editor/compositor.ts", needle: "starline" }, + { line: "earendil-works/pi-tui", needle: "works" }, + ]) { + const copied: string[] = []; + const terminal = new RecordingTerminal(80, 1); + const tui = new TuiAltScreen(terminal, undefined, undefined, { + copySelection: async (text) => { + copied.push(text); + return true; + }, + }); + tui.addChild(new Text(line, 0, 0)); + tui.start(); + await terminal.waitForRender(); + + const oneBasedClickColumn = line.indexOf(needle) + 1; + terminal.sendInput(`\x1b[<0;${oneBasedClickColumn};1M`); + terminal.sendInput(`\x1b[<0;${oneBasedClickColumn};1m`); + terminal.sendInput(`\x1b[<0;${oneBasedClickColumn};1M`); + terminal.sendInput(`\x1b[<0;${oneBasedClickColumn};1m`); + await terminal.waitForRender(); + + assert.deepStrictEqual(copied, [line]); + tui.stop(); + } + }); + + it("highlights a complete whitespace segment during a word drag", async () => { + const terminal = new RecordingTerminal(20, 1); + const tui = new TuiAltScreen(terminal); + tui.addChild(new Text("foo bar", 0, 0)); + tui.start(); + await terminal.waitForRender(); + + terminal.sendInput("\x1b[<0;1;1M"); + terminal.sendInput("\x1b[<0;1;1m"); + terminal.sendInput("\x1b[<0;2;1M"); + terminal.sendInput("\x1b[<32;4;1M"); + await terminal.waitForRender(); + + assert.ok(terminal.events.some((event) => event.type === "write" && event.data.includes("foo \x1b[27m"))); + tui.stop(); + }); + + it("selects whole words on double click, extends word drags, and selects lines on triple click", async () => { + const terminal = new RecordingTerminal(20, 2); + const tui = new TuiAltScreen(terminal); + tui.addChild(new Text("zero alpha beta\ngamma delta", 0, 0)); + tui.start(); + await terminal.waitForRender(); + + // The second click lands on a different character in alpha. + terminal.sendInput("\x1b[<0;6;1M"); + terminal.sendInput("\x1b[<0;6;1m"); + terminal.sendInput("\x1b[<0;10;1M"); + terminal.sendInput("\x1b[<0;10;1m"); + await terminal.waitForRender(); + const alpha = `\x1b]52;c;${Buffer.from("alpha").toString("base64")}\x07`; + assert.ok(terminal.events.some((event) => event.type === "write" && event.data.includes(alpha))); + + // A double-click drag includes each word touched, rather than partial words. + terminal.sendInput("\x1b[<0;12;1M"); + terminal.sendInput("\x1b[<0;12;1m"); + terminal.sendInput("\x1b[<0;14;1M"); + terminal.sendInput("\x1b[<32;3;2M"); + terminal.sendInput("\x1b[<0;3;2m"); + await terminal.waitForRender(); + const words = `\x1b]52;c;${Buffer.from("beta\ngamma").toString("base64")}\x07`; + assert.ok(terminal.events.some((event) => event.type === "write" && event.data.includes(words))); + + terminal.sendInput("\x1b[<0;7;2M"); + terminal.sendInput("\x1b[<0;7;2m"); + terminal.sendInput("\x1b[<0;9;2M"); + terminal.sendInput("\x1b[<0;9;2m"); + terminal.sendInput("\x1b[<0;11;2M"); + terminal.sendInput("\x1b[<0;11;2m"); + await terminal.waitForRender(); + const line = `\x1b]52;c;${Buffer.from("gamma delta").toString("base64")}\x07`; + assert.ok(terminal.events.some((event) => event.type === "write" && event.data.includes(line))); + + tui.stop(); + }); + + it("does not repaint idle or zero-width selections on focus loss", async () => { + const terminal = new RecordingTerminal(20, 4); + const tui = new TuiAltScreen(terminal); + tui.addChild(new Text("alpha\nbeta\ngamma\ndelta", 0, 0)); + tui.start(); + await terminal.waitForRender(); + + const writeCount = () => terminal.events.filter((event) => event.type === "write").length; + const clipboardWriteCount = () => + terminal.events.filter((event) => event.type === "write" && event.data.includes("\x1b]52;c;")).length; + + const idleWriteCount = writeCount(); + terminal.sendInput("\x1b[O"); + terminal.sendInput("\x1b[I"); + await terminal.waitForRender(); + assert.strictEqual(writeCount(), idleWriteCount); + + // A completed click leaves a zero-width anchor, but later orphaned drag/release events must not extend it. + terminal.sendInput("\x1b[<0;1;1M"); + terminal.sendInput("\x1b[<0;1;1m"); + terminal.sendInput("\x1b[<32;4;2M"); + terminal.sendInput("\x1b[<0;4;2m"); + await terminal.waitForRender(); + assert.strictEqual(clipboardWriteCount(), 0); + + // Losing focus after a press without a drag cancels the press without repainting. + terminal.sendInput("\x1b[<0;1;3M"); + await terminal.waitForRender(); + const pressedWriteCount = writeCount(); + terminal.sendInput("\x1b[O"); + terminal.sendInput("\x1b[I"); + await terminal.waitForRender(); + assert.strictEqual(writeCount(), pressedWriteCount); + terminal.sendInput("\x1b[<32;4;2M"); + terminal.sendInput("\x1b[<0;4;2m"); + await terminal.waitForRender(); + assert.strictEqual(clipboardWriteCount(), 0); + assert.ok(terminal.events.some((event) => event.type === "write" && event.data.includes("\x1b[?1004h"))); + + tui.stop(); + assert.ok(terminal.events.some((event) => event.type === "write" && event.data.includes("\x1b[?1004l"))); + }); + + it("clears an active visible selection on focus loss and ignores orphan events", async () => { + const terminal = new RecordingTerminal(20, 4); + const tui = new TuiAltScreen(terminal); + tui.addChild(new Text("alpha\nbeta\ngamma\ndelta", 0, 0)); + tui.start(); + await terminal.waitForRender(); + + terminal.sendInput("\x1b[<0;1;1M"); + terminal.sendInput("\x1b[<32;4;2M"); + await terminal.waitForRender(); + const focusLossEventCount = terminal.events.length; + terminal.sendInput("\x1b[O"); + terminal.sendInput("\x1b[I"); + await terminal.waitForRender(); + const focusLossWrites = terminal.events + .slice(focusLossEventCount) + .filter((event): event is { type: "write"; data: string } => event.type === "write") + .map((event) => event.data) + .join(""); + assert.ok(focusLossWrites.includes("alpha")); + assert.ok(focusLossWrites.includes("beta")); + assert.ok(!focusLossWrites.includes("\x1b[7m")); + + terminal.sendInput("\x1b[<32;4;2M"); + terminal.sendInput("\x1b[<0;4;2m"); + await terminal.waitForRender(); + assert.ok(terminal.events.every((event) => event.type !== "write" || !event.data.includes("\x1b]52;c;"))); + tui.stop(); + }); + + it("retains a completed visible selection across focus changes", async () => { + const terminal = new RecordingTerminal(20, 4); + const tui = new TuiAltScreen(terminal); + tui.addChild(new Text("alpha\nbeta\ngamma\ndelta", 0, 0)); + tui.start(); + await terminal.waitForRender(); + + terminal.sendInput("\x1b[<0;1;1M"); + terminal.sendInput("\x1b[<32;4;2M"); + terminal.sendInput("\x1b[<0;4;2m"); + await terminal.waitForRender(); + const completedWriteCount = terminal.events.filter((event) => event.type === "write").length; + terminal.sendInput("\x1b[O"); + terminal.sendInput("\x1b[I"); + await terminal.waitForRender(); + assert.strictEqual(terminal.events.filter((event) => event.type === "write").length, completedWriteCount); + + const redrawEventCount = terminal.events.length; + tui.renderNow(true); + const redrawWrites = terminal.events + .slice(redrawEventCount) + .filter((event): event is { type: "write"; data: string } => event.type === "write") + .map((event) => event.data) + .join(""); + assert.ok(redrawWrites.includes("alpha")); + assert.ok(redrawWrites.includes("beta")); + assert.ok(redrawWrites.includes("\x1b[7m")); + tui.stop(); + }); + + it("stacks flash messages and collapses them as they expire", async () => { + const terminal = new VirtualTerminal(20, 4); + const tui = new TuiAltScreen(terminal); + tui.addChild(new Text("one\ntwo\nthree\nfour", 0, 0)); + tui.start(); + await terminal.waitForRender(); + + tui.flash("First", 80); + tui.flash("Second", 500); + await terminal.waitForRender(); + let viewport = terminal.getViewport(); + assert.ok(viewport[0]?.endsWith(" First ")); + assert.ok(viewport[1]?.endsWith(" Second ")); + + await new Promise((resolve) => setTimeout(resolve, 100)); + await terminal.waitForRender(); + viewport = terminal.getViewport(); + assert.ok(viewport[0]?.endsWith(" Second ")); + assert.ok(!viewport.some((line) => line.includes("First"))); + + tui.stop(); + }); + + it("auto-scrolls and extends a drag selection held at the viewport edge", async () => { + const terminal = new RecordingTerminal(20, 4); + const tui = new TuiAltScreen(terminal); + tui.addChild(new Text(Array.from({ length: 10 }, (_, index) => `line ${index + 1}`).join("\n"), 0, 0)); + tui.start(); + await terminal.waitForRender(); + assert.strictEqual(tui.viewportTop, 6); + + terminal.sendInput("\x1b[<0;1;3M"); + terminal.sendInput("\x1b[<32;1;1M"); + await new Promise((resolve) => setTimeout(resolve, 130)); + await terminal.waitForRender(); + + const selectionTop = tui.viewportTop; + assert.ok(selectionTop < 6, `expected auto-scroll above row 6, got ${selectionTop}`); + terminal.sendInput("\x1b[<0;1;1m"); + await terminal.waitForRender(); + + const selectedLines = Array.from({ length: 8 - selectionTop }, (_, index) => `line ${selectionTop + index + 1}`); + selectedLines.push("l"); + const expectedClipboardSequence = `\x1b]52;c;${Buffer.from(selectedLines.join("\n")).toString("base64")}\x07`; + assert.ok( + terminal.events.some((event) => event.type === "write" && event.data.includes(expectedClipboardSequence)), + JSON.stringify(terminal.events.filter((event) => event.type === "write" && event.data.includes("\x1b]52;c;"))), + ); + tui.stop(); + }); + + it("snaps mouse selection to CJK, emoji, and combining grapheme boundaries", async () => { + const terminal = new RecordingTerminal(20, 2); + const tui = new TuiAltScreen(terminal); + tui.addChild(new Text("A界🙂éZ", 0, 0)); + tui.start(); + await terminal.waitForRender(); + + const wideSelection = `\x1b]52;c;${Buffer.from("界🙂").toString("base64")}\x07`; + terminal.sendInput("\x1b[<0;3;1M"); + terminal.sendInput("\x1b[<32;4;1M"); + terminal.sendInput("\x1b[<0;4;1m"); + await terminal.waitForRender(); + assert.strictEqual( + terminal.events.filter((event) => event.type === "write" && event.data.includes(wideSelection)).length, + 1, + ); + + terminal.sendInput("\x1b[<0;5;1M"); + terminal.sendInput("\x1b[<32;2;1M"); + terminal.sendInput("\x1b[<0;2;1m"); + await terminal.waitForRender(); + assert.strictEqual( + terminal.events.filter((event) => event.type === "write" && event.data.includes(wideSelection)).length, + 2, + ); + + const combiningSelection = `\x1b]52;c;${Buffer.from("éZ").toString("base64")}\x07`; + terminal.sendInput("\x1b[<0;6;1M"); + terminal.sendInput("\x1b[<32;7;1M"); + terminal.sendInput("\x1b[<0;7;1m"); + await terminal.waitForRender(); + assert.ok(terminal.events.some((event) => event.type === "write" && event.data.includes(combiningSelection))); + + tui.stop(); + }); + + it("ignores horizontal trackpad wheel events", async () => { + const terminal = new VirtualTerminal(20, 4); + const tui = new TuiAltScreen(terminal); + tui.addChild(new Text(Array.from({ length: 8 }, (_, index) => `line ${index + 1}`).join("\n"), 0, 0)); + tui.start(); + await terminal.waitForRender(); + + terminal.sendInput("\x1b[<66;1;1M"); + terminal.sendInput("\x1b[<67;1;1M"); + await terminal.waitForRender(); + assert.strictEqual(tui.viewportTop, 4); + assert.deepStrictEqual( + terminal.getViewport().map((line) => line.trimEnd()), + ["line 5", "line 6", "line 7", "line 8"], + ); + + tui.stop(); + }); + + it("dispatches clicks to nested mouse regions without breaking drag selection", async () => { + const terminal = new RecordingTerminal(20, 2); + const tui = new TuiAltScreen(terminal); + let clicks = 0; + tui.addChild( + new MouseRegion(new Text("clickable\nselectable", 0, 0), (event) => { + if (event.type !== "click") return undefined; + clicks += 1; + return { handled: true }; + }), + ); + tui.start(); + await terminal.waitForRender(); + + terminal.sendInput("\x1b[<0;2;1M"); + terminal.sendInput("\x1b[<0;2;1m"); + await terminal.waitForRender(); + assert.strictEqual(clicks, 1); + + terminal.sendInput("\x1b[<0;1;1M"); + terminal.sendInput("\x1b[<32;4;2M"); + terminal.sendInput("\x1b[<0;4;2m"); + await terminal.waitForRender(); + assert.strictEqual(clicks, 1); + assert.ok(terminal.events.some((event) => event.type === "write" && event.data.includes("\x1b]52;c;"))); + tui.stop(); + }); + + it("focuses and captures drag gestures for mouse-aware components", async () => { + const terminal = new VirtualTerminal(20, 2); + const tui = new TuiAltScreen(terminal); + const events: string[] = []; + const component = { + render: () => ["control"], + invalidate: () => {}, + handleMouse: (event: TuiMouseEvent) => { + events.push(event.type); + return event.type === "press" ? { handled: true, capture: true, focus: true } : { handled: true }; + }, + }; + tui.addChild(component); + tui.start(); + await terminal.waitForRender(); + + terminal.sendInput("\x1b[<0;1;1M"); + terminal.sendInput("\x1b[<32;5;2M"); + terminal.sendInput("\x1b[<0;5;2m"); + await terminal.waitForRender(); + + assert.deepStrictEqual(events, ["press", "drag", "release"]); + assert.strictEqual(tui.getFocusedComponent(), component); + tui.stop(); + }); + + it("reports consecutive click counts to component-owned controls", async () => { + const terminal = new VirtualTerminal(20, 1); + const tui = new TuiAltScreen(terminal); + const clickCounts: number[] = []; + tui.addChild({ + render: () => ["control"], + invalidate: () => {}, + handleMouse: (event) => { + if (event.type === "press") return { handled: true }; + if (event.type === "click") { + clickCounts.push(event.clickCount ?? 0); + return { handled: true }; + } + return undefined; + }, + }); + tui.start(); + await terminal.waitForRender(); + + for (let count = 0; count < 3; count++) { + terminal.sendInput("\x1b[<0;1;1M"); + terminal.sendInput("\x1b[<0;1;1m"); + } + await terminal.waitForRender(); + assert.deepStrictEqual(clickCounts, [1, 2, 3]); + tui.stop(); + }); + + it("does not rerender for handled no-op pointer motion", async () => { + const terminal = new RecordingTerminal(20, 2); + const tui = new TuiAltScreen(terminal); + let renderCount = 0; + tui.addChild({ + render: () => { + renderCount += 1; + return ["hover target"]; + }, + invalidate: () => {}, + handleMouse: (event) => (event.type === "move" ? { handled: true } : undefined), + }); + tui.start(); + await terminal.waitForRender(); + const renderedBeforeMotion = renderCount; + const writesBeforeMotion = terminal.events.filter((event) => event.type === "write").length; + + terminal.sendInput("\x1b[<35;1;1M"); + await terminal.waitForRender(); + assert.strictEqual(renderCount, renderedBeforeMotion); + assert.strictEqual(terminal.events.filter((event) => event.type === "write").length, writesBeforeMotion); + tui.stop(); + }); + + it("lets mouse-aware components consume wheel events before viewport scrolling", async () => { + const terminal = new VirtualTerminal(20, 3); + const tui = new TuiAltScreen(terminal); + let wheelEvents = 0; + tui.addChild( + new MouseRegion( + new Text(Array.from({ length: 8 }, (_, index) => `line ${index + 1}`).join("\n"), 0, 0), + (event) => { + if (event.type !== "wheel") return undefined; + wheelEvents += 1; + return { handled: true }; + }, + ), + ); + tui.start(); + await terminal.waitForRender(); + const viewportTop = tui.viewportTop; + + terminal.sendInput("\x1b[<64;1;1M"); + await terminal.waitForRender(); + assert.strictEqual(wheelEvents, 1); + assert.strictEqual(tui.viewportTop, viewportTop); + tui.stop(); + }); + + it("restores keyboard state before leaving alt mode and prints the full document", async () => { + const terminal = new RecordingTerminal(20, 3); + const tui = new TuiAltScreen(terminal); + tui.addChild(new Text("first\nsecond\nthird\nfourth\nfifth\nsixth", 0, 0)); + tui.start(); + await terminal.waitForRender(); + tui.stop(); + + const startIndex = terminal.events.findIndex((event) => event.type === "start"); + const altScreenEnterIndex = terminal.events.findIndex( + (event) => event.type === "write" && event.data.includes("\x1b[?1049h"), + ); + const stopIndex = terminal.events.findIndex((event) => event.type === "stop"); + const mouseDisableIndex = terminal.events.findIndex( + (event) => event.type === "write" && event.data.includes("\x1b[?1006l"), + ); + const mainScreenRestoreIndex = terminal.events.findIndex( + (event) => event.type === "write" && event.data.includes("\x1b[?1049l"), + ); + assert.ok(altScreenEnterIndex >= 0 && altScreenEnterIndex < startIndex); + assert.ok(mouseDisableIndex >= 0 && mouseDisableIndex < stopIndex); + assert.ok(mainScreenRestoreIndex > stopIndex); + + const restoreEvent = terminal.events[mainScreenRestoreIndex]; + assert.strictEqual(restoreEvent?.type, "write"); + if (restoreEvent?.type === "write") { + assert.ok(restoreEvent.data.includes("first")); + assert.ok(restoreEvent.data.includes("second")); + assert.ok(restoreEvent.data.includes("third")); + assert.ok(restoreEvent.data.includes("fourth")); + assert.ok(restoreEvent.data.includes("fifth")); + assert.ok(restoreEvent.data.includes("sixth")); + assert.ok(restoreEvent.data.indexOf("first") < restoreEvent.data.indexOf("sixth")); + } + }); + + it("gives wheel and viewport keys to a focused overlay", async () => { + const terminal = new VirtualTerminal(20, 6); + const tui = new TuiAltScreen(terminal); + tui.addChild(new Text(Array.from({ length: 12 }, (_, index) => `line ${index + 1}`).join("\n"), 0, 0)); + const overlay = new InputOverlay(); + tui.start(); + await terminal.waitForRender(); + const topBefore = tui.viewportTop; + const handle = tui.showOverlay(overlay); + await terminal.waitForRender(); + assert.strictEqual(overlay.focused, true); + + const wheel = "\x1b[<64;10;3M"; + const keys = ["\x1b[5~", "\x1b[6~", "\x1bOH", "\x1bOF", wheel]; + for (const key of keys) terminal.sendInput(key); + await terminal.waitForRender(); + + assert.deepStrictEqual(overlay.inputs, keys); + assert.strictEqual(tui.viewportTop, topBefore); + + handle.hide(); + await terminal.waitForRender(); + terminal.sendInput("\x1b[5~"); + await terminal.waitForRender(); + assert.ok(tui.viewportTop < topBefore); + tui.stop(); + }); + + it("keeps viewport scrolling when an overlay is not focused", async () => { + const terminal = new VirtualTerminal(20, 6); + const tui = new TuiAltScreen(terminal); + const editor = new InputOverlay(); + tui.addChild(new Text(Array.from({ length: 12 }, (_, index) => `line ${index + 1}`).join("\n"), 0, 0)); + tui.setFocus(editor); + tui.start(); + await terminal.waitForRender(); + const topBefore = tui.viewportTop; + + const hidden = tui.showOverlay(new InputOverlay()); + hidden.setHidden(true); + const nonCapturing = new InputOverlay(); + tui.showOverlay(nonCapturing, { nonCapturing: true }); + const unfocused = new InputOverlay(); + const unfocusedHandle = tui.showOverlay(unfocused); + unfocusedHandle.unfocus(); + await terminal.waitForRender(); + assert.strictEqual(nonCapturing.focused, false); + assert.strictEqual(unfocused.focused, false); + + terminal.sendInput("\x1b[5~"); + terminal.sendInput("\x1b[<64;10;3M"); + await terminal.waitForRender(); + assert.ok(tui.viewportTop < topBefore); + assert.deepStrictEqual(nonCapturing.inputs, []); + assert.deepStrictEqual(unfocused.inputs, []); + tui.stop(); + }); + + it("keeps viewport scrolling while transcript search is focused", async () => { + const terminal = new VirtualTerminal(20, 6); + const tui = new TuiAltScreen(terminal); + tui.addChild(new Text(Array.from({ length: 12 }, (_, index) => `line ${index + 1}`).join("\n"), 0, 0)); + tui.start(); + await terminal.waitForRender(); + const topBefore = tui.viewportTop; + + terminal.sendInput("\x1b[102;6u"); + await terminal.waitForRender(); + assert.ok(terminal.getViewport().some((line) => line.includes("↑ ↓"))); + + terminal.sendInput("\x1b[5~"); + terminal.sendInput("\x1b[<64;1;4M"); + await terminal.waitForRender(); + assert.ok(tui.viewportTop < topBefore); + assert.ok(terminal.getViewport().some((line) => line.includes("↑ ↓"))); + tui.stop(); + }); +}); diff --git a/packages/tui/test/tui-cell-size-input.test.ts b/packages/tui/test/tui-cell-size-input.test.ts new file mode 100644 index 0000000000000000000000000000000000000000..c0e370b4fa717d9e2447c9bf132626f7983f6ccd --- /dev/null +++ b/packages/tui/test/tui-cell-size-input.test.ts @@ -0,0 +1,82 @@ +import assert from "node:assert"; +import { describe, it } from "node:test"; +import { getCellDimensions, resetCapabilitiesCache, setCellDimensions } from "../src/terminal-image.ts"; +import type { Component, TUI } from "../src/tui.ts"; +import { TuiMainScreen } from "../src/tui-main-screen.ts"; +import { VirtualTerminal } from "./virtual-terminal.ts"; + +class InputRecorder implements Component { + readonly inputs: string[] = []; + + render(): string[] { + return [""]; + } + + handleInput(data: string): void { + this.inputs.push(data); + } + + invalidate(): void {} +} + +function withImageTerminal(fn: () => T): T { + const prevTermProgram = process.env.TERM_PROGRAM; + const prevTerm = process.env.TERM; + const prevGhosttyResourcesDir = process.env.GHOSTTY_RESOURCES_DIR; + + process.env.TERM_PROGRAM = "ghostty"; + delete process.env.TERM; + delete process.env.GHOSTTY_RESOURCES_DIR; + resetCapabilitiesCache(); + + try { + return fn(); + } finally { + if (prevTermProgram === undefined) delete process.env.TERM_PROGRAM; + else process.env.TERM_PROGRAM = prevTermProgram; + if (prevTerm === undefined) delete process.env.TERM; + else process.env.TERM = prevTerm; + if (prevGhosttyResourcesDir === undefined) delete process.env.GHOSTTY_RESOURCES_DIR; + else process.env.GHOSTTY_RESOURCES_DIR = prevGhosttyResourcesDir; + resetCapabilitiesCache(); + } +} + +describe("TUI cell size responses", () => { + it("forwards bare escape even when a cell size query was sent at startup", () => { + withImageTerminal(() => { + const terminal = new VirtualTerminal(80, 24); + const tui: TUI = new TuiMainScreen(terminal); + const recorder = new InputRecorder(); + + tui.setFocus(recorder); + tui.start(); + + terminal.sendInput("\x1b"); + + assert.deepStrictEqual(recorder.inputs, ["\x1b"]); + tui.stop(); + }); + }); + + it("consumes cell size responses and still forwards later user input", () => { + withImageTerminal(() => { + setCellDimensions({ widthPx: 9, heightPx: 18 }); + + const terminal = new VirtualTerminal(80, 24); + const tui: TUI = new TuiMainScreen(terminal); + const recorder = new InputRecorder(); + + tui.setFocus(recorder); + tui.start(); + + terminal.sendInput("\x1b[6;20;10t"); + assert.deepStrictEqual(recorder.inputs, []); + assert.deepStrictEqual(getCellDimensions(), { widthPx: 10, heightPx: 20 }); + + terminal.sendInput("q"); + assert.deepStrictEqual(recorder.inputs, ["q"]); + tui.stop(); + }); + }); +}); diff --git a/packages/tui/test/tui-overlay-style-leak.test.ts b/packages/tui/test/tui-overlay-style-leak.test.ts new file mode 100644 index 0000000000000000000000000000000000000000..dc575059d692018a773e60fd24a17b53d2f6585a --- /dev/null +++ b/packages/tui/test/tui-overlay-style-leak.test.ts @@ -0,0 +1,81 @@ +import assert from "node:assert"; +import { describe, it } from "node:test"; +import type { Terminal as XtermTerminalType } from "@xterm/headless"; +import type { Component, TUI } from "../src/tui.ts"; +import { TuiMainScreen } from "../src/tui-main-screen.ts"; +import { VirtualTerminal } from "./virtual-terminal.ts"; + +class StaticLines implements Component { + private readonly lines: string[]; + + constructor(lines: string[]) { + this.lines = lines; + } + + render(): string[] { + return this.lines; + } + + invalidate(): void {} +} + +class StaticOverlay implements Component { + private readonly line: string; + + constructor(line: string) { + this.line = line; + } + + render(): string[] { + return [this.line]; + } + + invalidate(): void {} +} + +function getCellItalic(terminal: VirtualTerminal, row: number, col: number): number { + const xterm = (terminal as unknown as { xterm: XtermTerminalType }).xterm; + const buffer = xterm.buffer.active; + const line = buffer.getLine(buffer.viewportY + row); + assert.ok(line, `Missing buffer line at row ${row}`); + const cell = line.getCell(col); + assert.ok(cell, `Missing cell at row ${row} col ${col}`); + return cell.isItalic(); +} + +async function renderAndFlush(tui: TUI, terminal: VirtualTerminal): Promise { + tui.requestRender(true); + await new Promise((resolve) => process.nextTick(resolve)); + await terminal.waitForRender(); +} + +describe("TUI overlay compositing", () => { + it("should not leak styles when a trailing reset sits beyond the last visible column (no overlay)", async () => { + const width = 20; + const baseLine = `\x1b[3m${"X".repeat(width)}\x1b[23m`; + + const terminal = new VirtualTerminal(width, 6); + const tui: TUI = new TuiMainScreen(terminal); + tui.addChild(new StaticLines([baseLine, "INPUT"])); + tui.start(); + await renderAndFlush(tui, terminal); + assert.strictEqual(getCellItalic(terminal, 1, 0), 0); + tui.stop(); + }); + + it("should not leak styles when overlay slicing drops trailing SGR resets", async () => { + const width = 20; + const baseLine = `\x1b[3m${"X".repeat(width)}\x1b[23m`; + + const terminal = new VirtualTerminal(width, 6); + const tui: TUI = new TuiMainScreen(terminal); + tui.addChild(new StaticLines([baseLine, "INPUT"])); + + tui.showOverlay(new StaticOverlay("OVR"), { row: 0, col: 5, width: 3 }); + tui.start(); + await renderAndFlush(tui, terminal); + + assert.strictEqual(getCellItalic(terminal, 1, 0), 0); + tui.stop(); + }); +}); diff --git a/packages/tui/test/tui-render.test.ts b/packages/tui/test/tui-render.test.ts new file mode 100644 index 0000000000000000000000000000000000000000..67e0f3e47a3d733b8c6f8e9af84b2cc8b239f767 --- /dev/null +++ b/packages/tui/test/tui-render.test.ts @@ -0,0 +1,952 @@ +import assert from "node:assert"; +import { mkdtempSync, readFileSync, rmSync } from "node:fs"; +import { tmpdir } from "node:os"; +import { join } from "node:path"; +import { describe, it } from "node:test"; +import type { Terminal as XtermTerminalType } from "@xterm/headless"; +import { Image } from "../src/components/image.ts"; +import type { Terminal } from "../src/terminal.ts"; +import { + deleteKittyImage, + encodeKitty, + resetCapabilitiesCache, + setCapabilities, + setCellDimensions, +} from "../src/terminal-image.ts"; +import type { Component, TUI } from "../src/tui.ts"; +import { TuiMainScreen } from "../src/tui-main-screen.ts"; +import { VirtualTerminal } from "./virtual-terminal.ts"; + +class TestComponent implements Component { + lines: string[] = []; + render(_width: number): string[] { + return this.lines; + } + invalidate(): void {} +} + +class InputComponent extends TestComponent { + renderCount = 0; + + override render(width: number): string[] { + this.renderCount += 1; + return super.render(width); + } + + handleInput(data: string): void { + this.lines = [data]; + } +} + +const MAX_RENDER_WRITE_CHARS = 1024 * 1024; + +class BoundedWriteTerminal implements Terminal { + readonly writes: string[] = []; + columns = 80; + rows = 24; + readonly kittyProtocolActive = false; + + start(_onInput: (data: string) => void, _onResize: () => void): void {} + stop(): void {} + async drainInput(_maxMs?: number, _idleMs?: number): Promise {} + write(data: string): void { + this.writes.push(data); + } + moveBy(_lines: number): void {} + hideCursor(): void {} + showCursor(): void {} + clearLine(): void {} + clearFromCursor(): void {} + clearScreen(): void {} + setTitle(_title: string): void {} + setProgress(_active: boolean): void {} +} + +class LoggingVirtualTerminal extends VirtualTerminal { + private writes: string[] = []; + + override write(data: string): void { + this.writes.push(data); + super.write(data); + } + + getWrites(): string { + return this.writes.join(""); + } + + clearWrites(): void { + this.writes = []; + } +} + +async function withEnv(updates: Record, run: () => Promise): Promise { + const previousValues = new Map(); + for (const [key, value] of Object.entries(updates)) { + previousValues.set(key, process.env[key]); + if (value === undefined) { + delete process.env[key]; + } else { + process.env[key] = value; + } + } + + try { + return await run(); + } finally { + for (const [key, value] of previousValues) { + if (value === undefined) { + delete process.env[key]; + } else { + process.env[key] = value; + } + } + } +} + +function getCellItalic(terminal: VirtualTerminal, row: number, col: number): number { + const xterm = (terminal as unknown as { xterm: XtermTerminalType }).xterm; + const buffer = xterm.buffer.active; + const line = buffer.getLine(buffer.viewportY + row); + assert.ok(line, `Missing buffer line at row ${row}`); + const cell = line.getCell(col); + assert.ok(cell, `Missing cell at row ${row} col ${col}`); + return cell.isItalic(); +} + +describe("TUI render scheduling", () => { + it("renders keyboard input without waiting for a throttled frame", async () => { + const terminal = new VirtualTerminal(40, 10); + const tui: TUI = new TuiMainScreen(terminal); + const component = new InputComponent(); + component.lines = ["initial"]; + tui.addChild(component); + tui.setFocus(component); + tui.start(); + tui.renderNow(); + const renderCountBeforeInput = component.renderCount; + + // Queue a normal throttled render first. Keyboard input should preempt it. + component.lines = ["pending"]; + tui.requestRender(); + terminal.sendInput("first"); + terminal.sendInput("second"); + terminal.sendInput("typed"); + await new Promise((resolve) => process.nextTick(resolve)); + + assert.strictEqual(component.renderCount, renderCountBeforeInput + 1); + assert.deepStrictEqual(component.lines, ["typed"]); + tui.stop(); + }); +}); + +describe("TUI debug logging", () => { + it("writes redraw logs to the provided directory", async () => { + const logDir = mkdtempSync(join(tmpdir(), "pi-tui-log-")); + try { + await withEnv({ PI_TUI_DEBUG_REDRAW: "1" }, async () => { + const terminal = new VirtualTerminal(40, 10); + const tui: TUI = new TuiMainScreen(terminal, undefined, logDir); + const component = new TestComponent(); + tui.addChild(component); + component.lines = ["test"]; + tui.start(); + await terminal.waitForRender(); + + assert.match(readFileSync(join(logDir, "pi-tui-debug.log"), "utf-8"), /fullRender: first render/); + tui.stop(); + }); + } finally { + rmSync(logDir, { recursive: true, force: true }); + } + }); +}); + +describe("TUI bounded render output", () => { + it("splits a large full render without changing its output", () => { + const terminal = new BoundedWriteTerminal(); + const tui = new TuiMainScreen(terminal); + const component = new TestComponent(); + const kittyLine = `\x1b_Ga=T,f=100;${"A".repeat(1_200_000)}\x1b\\`; + component.lines = [kittyLine, kittyLine]; + tui.addChild(component); + + tui.renderNow(); + + assert.ok(terminal.writes.length > 2, "large output should be split across terminal writes"); + assert.ok( + terminal.writes.every((write) => write.length <= MAX_RENDER_WRITE_CHARS), + "each terminal write should stay below the configured limit", + ); + assert.strictEqual( + terminal.writes.join(""), + `\x1b[?2026h${kittyLine}\r\n${kittyLine}\x1b[?2026l`, + "chunking must preserve the synchronized render output", + ); + }); + + it("splits large differential updates without a full redraw", () => { + const terminal = new BoundedWriteTerminal(); + const tui = new TuiMainScreen(terminal); + const component = new TestComponent(); + tui.addChild(component); + component.lines = ["before"]; + tui.renderNow(); + terminal.writes.length = 0; + + const kittyLine = `\x1b_Ga=T,f=100;${"A".repeat(1_200_000)}\x1b\\`; + component.lines = ["before", kittyLine, kittyLine]; + tui.renderNow(); + + assert.ok(terminal.writes.length > 2, "large output should be split across terminal writes"); + assert.ok(terminal.writes.every((write) => write.length <= MAX_RENDER_WRITE_CHARS)); + const output = terminal.writes.join(""); + assert.ok(output.startsWith("\x1b[?2026h")); + assert.ok(output.endsWith("\x1b[?2026l")); + assert.ok(!output.includes("\x1b[2J"), "the update should stay on the differential render path"); + }); +}); + +/** Set each environment variable to `value`, returning a function that restores the previous state. */ +function overrideEnv(names: readonly string[], value: string): () => void { + const previousValues = names.map((name) => [name, process.env[name]] as const); + for (const name of names) { + process.env[name] = value; + } + return () => { + for (const [name, previousValue] of previousValues) { + if (previousValue === undefined) delete process.env[name]; + else process.env[name] = previousValue; + } + }; +} + +describe("TUI crash dump without configured log directory", () => { + it("writes the crash dump to the OS temp directory instead of a home-directory default", async () => { + // The TUI falls back to os.tmpdir() when no log directory is configured, so + // isolate the test by pointing the temp directory at a fresh directory rather + // than sharing the real one with concurrent test runs. os.tmpdir() reads + // TMPDIR on POSIX and TEMP/TMP on Windows, so override all three. + const crashDir = mkdtempSync(join(tmpdir(), "pi-tui-crash-")); + const crashLogPath = join(crashDir, "pi-tui-crash.log"); + const restoreTmpdirEnv = overrideEnv(["TMPDIR", "TEMP", "TMP"], crashDir); + try { + const terminal = new VirtualTerminal(40, 10); + const tui: TUI = new TuiMainScreen(terminal); + const component = new TestComponent(); + tui.addChild(component); + component.lines = ["ok"]; + tui.start(); + await terminal.waitForRender(); + + // Width overflow is detected in the differential render path + component.lines = ["ok", "x".repeat(60)]; + assert.throws( + () => tui.renderNow(), + (error: unknown) => { + assert.ok(error instanceof Error); + assert.ok(error.message.includes(crashLogPath), `error message should reference ${crashLogPath}`); + return true; + }, + ); + assert.match(readFileSync(crashLogPath, "utf-8"), /Terminal width: 40/); + } finally { + restoreTmpdirEnv(); + rmSync(crashDir, { recursive: true, force: true }); + } + }); +}); + +describe("TUI Kitty image cleanup", () => { + it("clears reserved Kitty image rows before drawing appended image placements", async () => { + setCapabilities({ images: "kitty", trueColor: true, hyperlinks: true }); + setCellDimensions({ widthPx: 10, heightPx: 10 }); + try { + const terminal = new LoggingVirtualTerminal(40, 10); + const tui: TUI = new TuiMainScreen(terminal); + const component = new TestComponent(); + tui.addChild(component); + + component.lines = ["before"]; + tui.start(); + await terminal.waitForRender(); + terminal.clearWrites(); + + const image = new Image( + "AAAA", + "image/png", + { fallbackColor: (value) => value }, + { maxWidthCells: 2 }, + { widthPx: 20, heightPx: 20 }, + ); + const imageLines = image.render(40); + const imageSequence = imageLines[0]; + component.lines = ["before", ...imageLines, "after"]; + tui.requestRender(); + await terminal.waitForRender(); + + const writes = terminal.getWrites(); + assert.ok( + writes.includes(`\x1b[2K\r\n\x1b[2K\x1b[1A${imageSequence}\x1b[1B`), + "reserved rows should be cleared before the image placement is drawn", + ); + assert.ok( + !writes.includes(`${imageSequence}\r\n\x1b[2K`), + "reserved row clears must not run after the image placement is drawn", + ); + + tui.stop(); + } finally { + resetCapabilitiesCache(); + setCellDimensions({ widthPx: 9, heightPx: 18 }); + } + }); + + it("falls back to full redraw when Kitty image pre-clear would scroll", async () => { + setCapabilities({ images: "kitty", trueColor: true, hyperlinks: true }); + setCellDimensions({ widthPx: 10, heightPx: 10 }); + try { + const terminal = new LoggingVirtualTerminal(40, 2); + const tui: TUI = new TuiMainScreen(terminal); + const component = new TestComponent(); + tui.addChild(component); + + component.lines = ["before"]; + tui.start(); + await terminal.waitForRender(); + const redrawsBeforeImage = tui.fullRedraws; + terminal.clearWrites(); + + const image = new Image( + "AAAA", + "image/png", + { fallbackColor: (value) => value }, + { maxWidthCells: 3 }, + { widthPx: 30, heightPx: 30 }, + ); + component.lines = ["before", ...image.render(40), "after"]; + tui.requestRender(); + await terminal.waitForRender(); + + assert.ok(tui.fullRedraws > redrawsBeforeImage, "unsafe image pre-clear should force a full redraw"); + assert.ok(terminal.getWrites().includes("\x1b[2J"), "fallback should clear and fully redraw"); + + tui.stop(); + } finally { + resetCapabilitiesCache(); + setCellDimensions({ widthPx: 9, heightPx: 18 }); + } + }); + + it("reserves Kitty image rows before drawing during full redraw fallbacks", async () => { + setCapabilities({ images: "kitty", trueColor: true, hyperlinks: true }); + setCellDimensions({ widthPx: 10, heightPx: 10 }); + try { + const terminal = new LoggingVirtualTerminal(40, 5); + const tui: TUI = new TuiMainScreen(terminal); + const component = new TestComponent(); + tui.addChild(component); + + component.lines = ["l0", "l1", "l2", "l3", "l4"]; + tui.start(); + await terminal.waitForRender(); + const redrawsBeforeImage = tui.fullRedraws; + terminal.clearWrites(); + + const image = new Image( + "AAAA", + "image/png", + { fallbackColor: (value) => value }, + { maxWidthCells: 3 }, + { widthPx: 30, heightPx: 30 }, + ); + const imageLines = image.render(40); + const imageSequence = imageLines[0]; + component.lines = ["l0", "l1", "l2", "l3", "l4", ...imageLines, "after"]; + tui.requestRender(); + await terminal.waitForRender(); + + const writes = terminal.getWrites(); + assert.ok(tui.fullRedraws > redrawsBeforeImage, "scrolling image append should force a full redraw"); + assert.ok( + writes.includes(`\r\n\r\n\x1b[2A${imageSequence}\x1b[2B`), + "full redraw should reserve visible image rows before drawing the placement", + ); + assert.ok( + !writes.includes(`${imageSequence}\r\n\x1b[0m`), + "full redraw must not write reserved padding rows after drawing the placement", + ); + + tui.stop(); + } finally { + resetCapabilitiesCache(); + setCellDimensions({ widthPx: 9, heightPx: 18 }); + } + }); + + it("does not use cursor-up placement for Kitty images taller than the viewport", async () => { + setCapabilities({ images: "kitty", trueColor: true, hyperlinks: true }); + setCellDimensions({ widthPx: 10, heightPx: 10 }); + try { + const terminal = new LoggingVirtualTerminal(40, 5); + const tui: TUI = new TuiMainScreen(terminal); + const component = new TestComponent(); + tui.addChild(component); + + component.lines = ["before"]; + tui.start(); + await terminal.waitForRender(); + terminal.clearWrites(); + + const image = new Image( + "AAAA", + "image/png", + { fallbackColor: (value) => value }, + { maxWidthCells: 6 }, + { widthPx: 60, heightPx: 60 }, + ); + const imageLines = image.render(40); + const imageSequence = imageLines[0]; + assert.ok(imageLines.length > terminal.rows, "test image should exceed the viewport height"); + + component.lines = ["before", ...imageLines, "after"]; + tui.requestRender(true); + await terminal.waitForRender(); + + const writes = terminal.getWrites(); + assert.ok(writes.includes(imageSequence), "image placement should be drawn"); + assert.ok( + !writes.includes(`\x1b[${imageLines.length - 1}A${imageSequence}`), + "taller-than-viewport images must keep the #4461 first-row placement path", + ); + + tui.stop(); + } finally { + resetCapabilitiesCache(); + setCellDimensions({ widthPx: 9, heightPx: 18 }); + } + }); + + it("deletes changed image ids before drawing moved placements", async () => { + const terminal = new LoggingVirtualTerminal(40, 10); + const tui: TUI = new TuiMainScreen(terminal); + const component = new TestComponent(); + tui.addChild(component); + + const oldImage = encodeKitty("AAAA", { columns: 2, rows: 2, imageId: 42, moveCursor: false }); + component.lines = ["top", oldImage]; + tui.start(); + await terminal.waitForRender(); + terminal.clearWrites(); + + const newImage = encodeKitty("BBBB", { columns: 2, rows: 1, imageId: 42, moveCursor: false }); + component.lines = [newImage, ""]; + tui.requestRender(); + await terminal.waitForRender(); + + const writes = terminal.getWrites(); + const deleteIndex = writes.indexOf(deleteKittyImage(42)); + const drawIndex = writes.indexOf(newImage); + assert.ok(deleteIndex >= 0, "changed old image should be deleted"); + assert.ok(drawIndex >= 0, "new image should be drawn"); + assert.ok(deleteIndex < drawIndex, "old image must be deleted before the new placement is drawn"); + + tui.stop(); + }); + + it("redraws image lines when an earlier reserved image row changes", async () => { + const terminal = new LoggingVirtualTerminal(40, 10); + const tui: TUI = new TuiMainScreen(terminal); + const component = new TestComponent(); + tui.addChild(component); + + const image = encodeKitty("AAAA", { columns: 2, rows: 2, imageId: 88, moveCursor: false }); + component.lines = ["", image]; + tui.start(); + await terminal.waitForRender(); + terminal.clearWrites(); + + component.lines = ["covered", image]; + tui.requestRender(); + await terminal.waitForRender(); + + const writes = terminal.getWrites(); + const deleteIndex = writes.indexOf(deleteKittyImage(88)); + const drawIndex = writes.indexOf(image); + assert.ok(deleteIndex >= 0, "image should be deleted when a reserved row changes"); + assert.ok(drawIndex >= 0, "unchanged image line should be redrawn after deleting the placement"); + assert.ok(deleteIndex < drawIndex, "old placement must be deleted before the image line is redrawn"); + assert.ok(!writes.includes("\x1b[2J"), "reserved row changes should not force a full redraw"); + + tui.stop(); + }); + + it("deletes previously rendered image ids during full redraws", async () => { + const terminal = new LoggingVirtualTerminal(40, 10); + const tui: TUI = new TuiMainScreen(terminal); + const component = new TestComponent(); + tui.addChild(component); + + component.lines = [encodeKitty("AAAA", { columns: 2, rows: 2, imageId: 77, moveCursor: false })]; + tui.start(); + await terminal.waitForRender(); + terminal.clearWrites(); + + component.lines = ["plain text"]; + tui.requestRender(true); + await terminal.waitForRender(); + + const writes = terminal.getWrites(); + const deleteIndex = writes.indexOf(deleteKittyImage(77)); + const clearIndex = writes.indexOf("\x1b[2J"); + assert.ok(deleteIndex >= 0, "previous image should be deleted during full redraw"); + assert.ok(clearIndex >= 0, "full redraw should clear the screen"); + assert.ok(deleteIndex < clearIndex, "old image should be deleted before the screen is cleared"); + + tui.stop(); + }); +}); + +describe("TUI resize handling", () => { + it("triggers full re-render when terminal height changes", async () => { + await withEnv({ TERMUX_VERSION: undefined }, async () => { + const terminal = new VirtualTerminal(40, 10); + const tui: TUI = new TuiMainScreen(terminal); + const component = new TestComponent(); + tui.addChild(component); + + component.lines = ["Line 0", "Line 1", "Line 2"]; + tui.start(); + await terminal.waitForRender(); + + const initialRedraws = tui.fullRedraws; + + // Resize height + terminal.resize(40, 15); + await terminal.waitForRender(); + + // Should have triggered a full redraw + assert.ok(tui.fullRedraws > initialRedraws, "Height change should trigger full redraw"); + + const viewport = terminal.getViewport(); + assert.ok(viewport[0]?.includes("Line 0"), "Content preserved after height change"); + + tui.stop(); + }); + }); + + it("skips full re-render on height changes in Termux", async () => { + await withEnv({ TERMUX_VERSION: "1" }, async () => { + const terminal = new LoggingVirtualTerminal(40, 10); + const tui: TUI = new TuiMainScreen(terminal); + const component = new TestComponent(); + tui.addChild(component); + + component.lines = Array.from({ length: 20 }, (_, i) => `Line ${i}`); + tui.start(); + await terminal.waitForRender(); + terminal.clearWrites(); + + const initialRedraws = tui.fullRedraws; + for (const height of [15, 8, 14, 11]) { + terminal.resize(40, height); + await terminal.waitForRender(); + } + + assert.strictEqual(tui.fullRedraws, initialRedraws, "Height change should not trigger full redraw"); + assert.ok(!terminal.getWrites().includes("\x1b[2J"), "Height change should not clear the screen"); + assert.ok(!terminal.getWrites().includes("\x1b[3J"), "Height change should not clear scrollback"); + + const viewport = terminal.getViewport(); + assert.ok(viewport.join("\n").includes("Line 19"), "Latest content remains visible after resize"); + + tui.stop(); + }); + }); + + it("triggers full re-render when terminal width changes", async () => { + const terminal = new VirtualTerminal(40, 10); + const tui: TUI = new TuiMainScreen(terminal); + const component = new TestComponent(); + tui.addChild(component); + + component.lines = ["Line 0", "Line 1", "Line 2"]; + tui.start(); + await terminal.waitForRender(); + + const initialRedraws = tui.fullRedraws; + + // Resize width + terminal.resize(60, 10); + await terminal.waitForRender(); + + // Should have triggered a full redraw + assert.ok(tui.fullRedraws > initialRedraws, "Width change should trigger full redraw"); + + tui.stop(); + }); +}); + +describe("TUI content shrinkage", () => { + it("clears empty rows when content shrinks significantly", async () => { + const terminal = new VirtualTerminal(40, 10); + const tui: TUI = new TuiMainScreen(terminal); + tui.setClearOnShrink(true); // Explicitly enable (may be disabled via env var) + const component = new TestComponent(); + tui.addChild(component); + + // Start with many lines + component.lines = ["Line 0", "Line 1", "Line 2", "Line 3", "Line 4", "Line 5"]; + tui.start(); + await terminal.waitForRender(); + + const initialRedraws = tui.fullRedraws; + + // Shrink to fewer lines + component.lines = ["Line 0", "Line 1"]; + tui.requestRender(); + await terminal.waitForRender(); + + // Should have triggered a full redraw to clear empty rows + assert.ok(tui.fullRedraws > initialRedraws, "Content shrinkage should trigger full redraw"); + + const viewport = terminal.getViewport(); + assert.ok(viewport[0]?.includes("Line 0"), "First line preserved"); + assert.ok(viewport[1]?.includes("Line 1"), "Second line preserved"); + // Lines below should be empty (cleared) + assert.strictEqual(viewport[2]?.trim(), "", "Line 2 should be cleared"); + assert.strictEqual(viewport[3]?.trim(), "", "Line 3 should be cleared"); + + tui.stop(); + }); + + it("handles shrink to single line", async () => { + const terminal = new VirtualTerminal(40, 10); + const tui: TUI = new TuiMainScreen(terminal); + tui.setClearOnShrink(true); // Explicitly enable (may be disabled via env var) + const component = new TestComponent(); + tui.addChild(component); + + component.lines = ["Line 0", "Line 1", "Line 2", "Line 3"]; + tui.start(); + await terminal.waitForRender(); + + // Shrink to single line + component.lines = ["Only line"]; + tui.requestRender(); + await terminal.waitForRender(); + + const viewport = terminal.getViewport(); + assert.ok(viewport[0]?.includes("Only line"), "Single line rendered"); + assert.strictEqual(viewport[1]?.trim(), "", "Line 1 should be cleared"); + + tui.stop(); + }); + + it("handles shrink to empty", async () => { + const terminal = new VirtualTerminal(40, 10); + const tui: TUI = new TuiMainScreen(terminal); + tui.setClearOnShrink(true); // Explicitly enable (may be disabled via env var) + const component = new TestComponent(); + tui.addChild(component); + + component.lines = ["Line 0", "Line 1", "Line 2"]; + tui.start(); + await terminal.waitForRender(); + + // Shrink to empty + component.lines = []; + tui.requestRender(); + await terminal.waitForRender(); + + const viewport = terminal.getViewport(); + // All lines should be empty + assert.strictEqual(viewport[0]?.trim(), "", "Line 0 should be cleared"); + assert.strictEqual(viewport[1]?.trim(), "", "Line 1 should be cleared"); + + tui.stop(); + }); +}); + +describe("TUI differential rendering", () => { + it("tracks cursor correctly when content shrinks with unchanged remaining lines", async () => { + const terminal = new VirtualTerminal(40, 10); + const tui: TUI = new TuiMainScreen(terminal); + const component = new TestComponent(); + tui.addChild(component); + + // Initial render: 5 identical lines + component.lines = ["Line 0", "Line 1", "Line 2", "Line 3", "Line 4"]; + tui.start(); + await terminal.waitForRender(); + + // Shrink to 3 lines, all identical to before (no content changes in remaining lines) + component.lines = ["Line 0", "Line 1", "Line 2"]; + tui.requestRender(); + await terminal.waitForRender(); + + // cursorRow should be 2 (last line of new content) + // Verify by doing another render with a change on line 1 + component.lines = ["Line 0", "CHANGED", "Line 2"]; + tui.requestRender(); + await terminal.waitForRender(); + + const viewport = terminal.getViewport(); + // Line 1 should show "CHANGED", proving cursor tracking was correct + assert.ok(viewport[1]?.includes("CHANGED"), `Expected "CHANGED" on line 1, got: ${viewport[1]}`); + + tui.stop(); + }); + + it("renders correctly when only a middle line changes (spinner case)", async () => { + const terminal = new VirtualTerminal(40, 10); + const tui: TUI = new TuiMainScreen(terminal); + const component = new TestComponent(); + tui.addChild(component); + + // Initial render + component.lines = ["Header", "Working...", "Footer"]; + tui.start(); + await terminal.waitForRender(); + + // Simulate spinner animation - only middle line changes + const spinnerFrames = ["|", "/", "-", "\\"]; + for (const frame of spinnerFrames) { + component.lines = ["Header", `Working ${frame}`, "Footer"]; + tui.requestRender(); + await terminal.waitForRender(); + + const viewport = terminal.getViewport(); + assert.ok(viewport[0]?.includes("Header"), `Header preserved: ${viewport[0]}`); + assert.ok(viewport[1]?.includes(`Working ${frame}`), `Spinner updated: ${viewport[1]}`); + assert.ok(viewport[2]?.includes("Footer"), `Footer preserved: ${viewport[2]}`); + } + + tui.stop(); + }); + + it("resets styles after each rendered line", async () => { + const terminal = new VirtualTerminal(20, 6); + const tui: TUI = new TuiMainScreen(terminal); + const component = new TestComponent(); + tui.addChild(component); + + component.lines = ["\x1b[3mItalic", "Plain"]; + tui.start(); + await terminal.waitForRender(); + + assert.strictEqual(getCellItalic(terminal, 1, 0), 0); + tui.stop(); + }); + + it("renders correctly when first line changes but rest stays same", async () => { + const terminal = new VirtualTerminal(40, 10); + const tui: TUI = new TuiMainScreen(terminal); + const component = new TestComponent(); + tui.addChild(component); + + component.lines = ["Line 0", "Line 1", "Line 2", "Line 3"]; + tui.start(); + await terminal.waitForRender(); + + // Change only first line + component.lines = ["CHANGED", "Line 1", "Line 2", "Line 3"]; + tui.requestRender(); + await terminal.waitForRender(); + + const viewport = terminal.getViewport(); + assert.ok(viewport[0]?.includes("CHANGED"), `First line changed: ${viewport[0]}`); + assert.ok(viewport[1]?.includes("Line 1"), `Line 1 preserved: ${viewport[1]}`); + assert.ok(viewport[2]?.includes("Line 2"), `Line 2 preserved: ${viewport[2]}`); + assert.ok(viewport[3]?.includes("Line 3"), `Line 3 preserved: ${viewport[3]}`); + + tui.stop(); + }); + + it("renders correctly when last line changes but rest stays same", async () => { + const terminal = new VirtualTerminal(40, 10); + const tui: TUI = new TuiMainScreen(terminal); + const component = new TestComponent(); + tui.addChild(component); + + component.lines = ["Line 0", "Line 1", "Line 2", "Line 3"]; + tui.start(); + await terminal.waitForRender(); + + // Change only last line + component.lines = ["Line 0", "Line 1", "Line 2", "CHANGED"]; + tui.requestRender(); + await terminal.waitForRender(); + + const viewport = terminal.getViewport(); + assert.ok(viewport[0]?.includes("Line 0"), `Line 0 preserved: ${viewport[0]}`); + assert.ok(viewport[1]?.includes("Line 1"), `Line 1 preserved: ${viewport[1]}`); + assert.ok(viewport[2]?.includes("Line 2"), `Line 2 preserved: ${viewport[2]}`); + assert.ok(viewport[3]?.includes("CHANGED"), `Last line changed: ${viewport[3]}`); + + tui.stop(); + }); + + it("renders correctly when multiple non-adjacent lines change", async () => { + const terminal = new VirtualTerminal(40, 10); + const tui: TUI = new TuiMainScreen(terminal); + const component = new TestComponent(); + tui.addChild(component); + + component.lines = ["Line 0", "Line 1", "Line 2", "Line 3", "Line 4"]; + tui.start(); + await terminal.waitForRender(); + + // Change lines 1 and 3, keep 0, 2, 4 the same + component.lines = ["Line 0", "CHANGED 1", "Line 2", "CHANGED 3", "Line 4"]; + tui.requestRender(); + await terminal.waitForRender(); + + const viewport = terminal.getViewport(); + assert.ok(viewport[0]?.includes("Line 0"), `Line 0 preserved: ${viewport[0]}`); + assert.ok(viewport[1]?.includes("CHANGED 1"), `Line 1 changed: ${viewport[1]}`); + assert.ok(viewport[2]?.includes("Line 2"), `Line 2 preserved: ${viewport[2]}`); + assert.ok(viewport[3]?.includes("CHANGED 3"), `Line 3 changed: ${viewport[3]}`); + assert.ok(viewport[4]?.includes("Line 4"), `Line 4 preserved: ${viewport[4]}`); + + tui.stop(); + }); + + it("handles transition from content to empty and back to content", async () => { + const terminal = new VirtualTerminal(40, 10); + const tui: TUI = new TuiMainScreen(terminal); + const component = new TestComponent(); + tui.addChild(component); + + // Start with content + component.lines = ["Line 0", "Line 1", "Line 2"]; + tui.start(); + await terminal.waitForRender(); + + let viewport = terminal.getViewport(); + assert.ok(viewport[0]?.includes("Line 0"), "Initial content rendered"); + + // Clear to empty + component.lines = []; + tui.requestRender(); + await terminal.waitForRender(); + + // Add content back - this should work correctly even after empty state + component.lines = ["New Line 0", "New Line 1"]; + tui.requestRender(); + await terminal.waitForRender(); + + viewport = terminal.getViewport(); + assert.ok(viewport[0]?.includes("New Line 0"), `New content rendered: ${viewport[0]}`); + assert.ok(viewport[1]?.includes("New Line 1"), `New content line 1: ${viewport[1]}`); + + tui.stop(); + }); + + it("full re-renders when deleted lines move the viewport upward", async () => { + const terminal = new VirtualTerminal(20, 5); + const tui: TUI = new TuiMainScreen(terminal); + const component = new TestComponent(); + tui.addChild(component); + + component.lines = Array.from({ length: 12 }, (_, i) => `Line ${i}`); + tui.start(); + await terminal.waitForRender(); + + const initialRedraws = tui.fullRedraws; + + component.lines = Array.from({ length: 7 }, (_, i) => `Line ${i}`); + tui.requestRender(); + await terminal.waitForRender(); + + assert.ok(tui.fullRedraws > initialRedraws, "Shrink should trigger a full redraw"); + assert.deepStrictEqual(terminal.getViewport(), ["Line 2", "Line 3", "Line 4", "Line 5", "Line 6"]); + + tui.stop(); + }); + + it("appends after a shrink without another full redraw once the viewport is reset", async () => { + const terminal = new VirtualTerminal(20, 5); + const tui: TUI = new TuiMainScreen(terminal); + const component = new TestComponent(); + tui.addChild(component); + + component.lines = Array.from({ length: 8 }, (_, i) => `Line ${i}`); + tui.start(); + await terminal.waitForRender(); + + const initialRedraws = tui.fullRedraws; + + component.lines = ["Line 0", "Line 1"]; + tui.requestRender(); + await terminal.waitForRender(); + + assert.ok(tui.fullRedraws > initialRedraws, "Shrink should reset the viewport with a full redraw"); + const redrawsAfterShrink = tui.fullRedraws; + + component.lines = ["Line 0", "Line 1", "Line 2"]; + tui.requestRender(); + await terminal.waitForRender(); + + assert.strictEqual(tui.fullRedraws, redrawsAfterShrink, "Append should stay on the differential path"); + assert.deepStrictEqual(terminal.getViewport(), ["Line 0", "Line 1", "Line 2", "", ""]); + + tui.stop(); + }); + + it("clears stale content when maxLinesRendered was inflated by a transient component", async () => { + const terminal = new VirtualTerminal(40, 10); + const tui: TUI = new TuiMainScreen(terminal); + const chat = new TestComponent(); + const editor = new TestComponent(); + tui.addChild(chat); + tui.addChild(editor); + + const longChat = Array.from({ length: 15 }, (_, i) => `Chat ${i}`); + const shortChat = Array.from({ length: 12 }, (_, i) => `Chat ${i}`); + const editorLines = ["Editor 0", "Editor 1", "Editor 2"]; + const selectorLines = Array.from({ length: 8 }, (_, i) => `Selector ${i}`); + + chat.lines = longChat; + editor.lines = editorLines; + tui.start(); + await terminal.waitForRender(); + + editor.lines = selectorLines; + tui.requestRender(); + await terminal.waitForRender(); + + editor.lines = editorLines; + tui.requestRender(); + await terminal.waitForRender(); + + const redrawsBeforeSwitch = tui.fullRedraws; + chat.lines = shortChat; + tui.requestRender(); + await terminal.waitForRender(); + + assert.ok(tui.fullRedraws > redrawsBeforeSwitch, "Branch switch should trigger a full redraw"); + + const viewport = terminal.getViewport(); + for (let i = 0; i < 10; i++) { + const line = viewport[i] ?? ""; + assert.ok(!line.includes("Chat 12"), `Stale "Chat 12" at viewport row ${i}`); + assert.ok(!line.includes("Chat 13"), `Stale "Chat 13" at viewport row ${i}`); + assert.ok(!line.includes("Chat 14"), `Stale "Chat 14" at viewport row ${i}`); + } + + assert.deepStrictEqual(viewport, [ + "Chat 5", + "Chat 6", + "Chat 7", + "Chat 8", + "Chat 9", + "Chat 10", + "Chat 11", + "Editor 0", + "Editor 1", + "Editor 2", + ]); + + tui.stop(); + }); +}); diff --git a/packages/tui/test/tui-shrink.test.ts b/packages/tui/test/tui-shrink.test.ts new file mode 100644 index 0000000000000000000000000000000000000000..dac98a4ee5ffe469dd4568e23d082a3e580f15a4 --- /dev/null +++ b/packages/tui/test/tui-shrink.test.ts @@ -0,0 +1,45 @@ +import assert from "node:assert"; +import { describe, it } from "node:test"; +import type { Component, TUI } from "../src/tui.ts"; +import { TuiMainScreen } from "../src/tui-main-screen.ts"; +import { VirtualTerminal } from "./virtual-terminal.ts"; + +class Lines implements Component { + private lines: string[]; + + constructor(lines: string[]) { + this.lines = lines; + } + + render(): string[] { + return this.lines; + } + + invalidate(): void {} +} + +describe("TUI shrinking content", () => { + it("clears all rendered lines when content shrinks to zero", async () => { + const terminal = new VirtualTerminal(40, 10); + const tui: TUI = new TuiMainScreen(terminal); + const content = new Lines(["first", "second", "third"]); + tui.addChild(content); + tui.start(); + await terminal.waitForRender(); + + assert.ok(terminal.getViewport().some((line) => line.includes("first"))); + assert.ok(terminal.getViewport().some((line) => line.includes("second"))); + assert.ok(terminal.getViewport().some((line) => line.includes("third"))); + + tui.clear(); + tui.requestRender(); + await terminal.waitForRender(); + + const viewport = terminal.getViewport(); + assert.ok(!viewport.some((line) => line.includes("first")), "first line should be cleared"); + assert.ok(!viewport.some((line) => line.includes("second")), "second line should be cleared"); + assert.ok(!viewport.some((line) => line.includes("third")), "third line should be cleared"); + + tui.stop(); + }); +}); diff --git a/packages/tui/test/viewport-overwrite-repro.ts b/packages/tui/test/viewport-overwrite-repro.ts new file mode 100644 index 0000000000000000000000000000000000000000..1826af06852eaa55123c408a939002c0e4940840 --- /dev/null +++ b/packages/tui/test/viewport-overwrite-repro.ts @@ -0,0 +1,108 @@ +/** + * TUI viewport overwrite repro + * + * Place this file at: packages/tui/test/viewport-overwrite-repro.ts + * Run from repo root: npx tsx packages/tui/test/viewport-overwrite-repro.ts + * + * For reliable repro, run in a small terminal (8-12 rows) or a tmux session: + * tmux new-session -d -s tui-bug -x 80 -y 12 + * tmux send-keys -t tui-bug "npx tsx packages/tui/test/viewport-overwrite-repro.ts" Enter + * tmux attach -t tui-bug + * + * Expected behavior: + * - PRE-TOOL lines remain visible above tool output. + * - POST-TOOL lines append after tool output without overwriting earlier content. + * + * Actual behavior (bug): + * - When content exceeds the viewport and new lines arrive after a tool-call pause, + * some earlier PRE-TOOL lines near the bottom are overwritten by POST-TOOL lines. + */ + +import { ProcessTerminal } from "../src/terminal.ts"; +import type { Component, TUI } from "../src/tui.ts"; +import { TuiMainScreen } from "../src/tui-main-screen.ts"; + +const sleep = (ms: number): Promise => new Promise((resolve) => setTimeout(resolve, ms)); + +class Lines implements Component { + private lines: string[] = []; + + set(lines: string[]): void { + this.lines = lines; + } + + append(lines: string[]): void { + this.lines.push(...lines); + } + + render(width: number): string[] { + return this.lines.map((line) => { + if (line.length > width) return line.slice(0, width); + return line.padEnd(width, " "); + }); + } + + invalidate(): void {} +} + +async function streamLines(buffer: Lines, label: string, count: number, delayMs: number, ui: TUI): Promise { + for (let i = 1; i <= count; i += 1) { + buffer.append([`${label} ${String(i).padStart(2, "0")}`]); + ui.requestRender(); + await sleep(delayMs); + } +} + +async function main(): Promise { + const ui: TUI = new TuiMainScreen(new ProcessTerminal()); + const buffer = new Lines(); + ui.addChild(buffer); + ui.start(); + + const height = ui.terminal.rows; + const preCount = height + 8; // Ensure content exceeds viewport + const toolCount = height + 12; // Tool output pushes further into scrollback + const postCount = 6; + + buffer.set([ + "TUI viewport overwrite repro", + `Viewport rows detected: ${height}`, + "(Resize to ~8-12 rows for best repro)", + "", + "=== PRE-TOOL STREAM ===", + ]); + ui.requestRender(); + await sleep(300); + + // Phase 1: Stream pre-tool text until viewport is exceeded. + await streamLines(buffer, "PRE-TOOL LINE", preCount, 30, ui); + + // Phase 2: Simulate tool call pause and tool output. + buffer.append(["", "--- TOOL CALL START ---", "(pause...)", ""]); + ui.requestRender(); + await sleep(700); + + await streamLines(buffer, "TOOL OUT", toolCount, 20, ui); + + // Phase 3: Post-tool streaming. This is where overwrite often appears. + buffer.append(["", "=== POST-TOOL STREAM ==="]); + ui.requestRender(); + await sleep(300); + await streamLines(buffer, "POST-TOOL LINE", postCount, 40, ui); + + // Leave the output visible briefly, then restore terminal state. + await sleep(1500); + ui.stop(); +} + +main().catch((error) => { + // Ensure terminal is restored if something goes wrong. + try { + const ui: TUI = new TuiMainScreen(new ProcessTerminal()); + ui.stop(); + } catch { + // Ignore restore errors. + } + process.stderr.write(`${String(error)}\n`); + process.exitCode = 1; +}); diff --git a/packages/tui/test/virtual-terminal.ts b/packages/tui/test/virtual-terminal.ts new file mode 100644 index 0000000000000000000000000000000000000000..4e067f4e5d549ab64f240d7870661b94fa29af8d --- /dev/null +++ b/packages/tui/test/virtual-terminal.ts @@ -0,0 +1,218 @@ +import type { Terminal as XtermTerminalType } from "@xterm/headless"; +import xterm from "@xterm/headless"; +import type { Terminal } from "../src/terminal.ts"; + +// Extract Terminal class from the module +const XtermTerminal = xterm.Terminal; + +/** + * Virtual terminal for testing using xterm.js for accurate terminal emulation + */ +export class VirtualTerminal implements Terminal { + private xterm: XtermTerminalType; + private inputHandler?: (data: string) => void; + private resizeHandler?: () => void; + private _columns: number; + private _rows: number; + + constructor(columns = 80, rows = 24) { + this._columns = columns; + this._rows = rows; + + // Create xterm instance with specified dimensions + this.xterm = new XtermTerminal({ + cols: columns, + rows: rows, + // Disable all interactive features for testing + disableStdin: true, + allowProposedApi: true, + }); + } + + start(onInput: (data: string) => void, onResize: () => void): void { + this.inputHandler = onInput; + this.resizeHandler = onResize; + // Enable bracketed paste mode for consistency with ProcessTerminal + this.xterm.write("\x1b[?2004h"); + } + + async drainInput(_maxMs?: number, _idleMs?: number): Promise { + // No-op for virtual terminal - no stdin to drain + } + + stop(): void { + // Disable bracketed paste mode + this.xterm.write("\x1b[?2004l"); + this.inputHandler = undefined; + this.resizeHandler = undefined; + } + + write(data: string): void { + this.xterm.write(data); + } + + get columns(): number { + return this._columns; + } + + get rows(): number { + return this._rows; + } + + get kittyProtocolActive(): boolean { + // Virtual terminal always reports Kitty protocol as active for testing + return true; + } + + moveBy(lines: number): void { + if (lines > 0) { + // Move down + this.xterm.write(`\x1b[${lines}B`); + } else if (lines < 0) { + // Move up + this.xterm.write(`\x1b[${-lines}A`); + } + // lines === 0: no movement + } + + hideCursor(): void { + this.xterm.write("\x1b[?25l"); + } + + showCursor(): void { + this.xterm.write("\x1b[?25h"); + } + + clearLine(): void { + this.xterm.write("\x1b[K"); + } + + clearFromCursor(): void { + this.xterm.write("\x1b[J"); + } + + clearScreen(): void { + this.xterm.write("\x1b[2J\x1b[H"); // Clear screen and move to home (1,1) + } + + setTitle(title: string): void { + // OSC 0;title BEL - set terminal window title + this.xterm.write(`\x1b]0;${title}\x07`); + } + + setProgress(_active: boolean): void {} + + // Test-specific methods not in Terminal interface + + /** + * Simulate keyboard input + */ + sendInput(data: string): void { + if (this.inputHandler) { + this.inputHandler(data); + } + } + + /** + * Resize the terminal + */ + resize(columns: number, rows: number): void { + this._columns = columns; + this._rows = rows; + this.xterm.resize(columns, rows); + if (this.resizeHandler) { + this.resizeHandler(); + } + } + + /** + * Wait for all pending writes to complete. Viewport and scroll buffer will be updated. + */ + async flush(): Promise { + // Write an empty string to ensure all previous writes are flushed + return new Promise((resolve) => { + this.xterm.write("", () => resolve()); + }); + } + + /** + * Flush and get viewport - convenience method for tests + */ + async flushAndGetViewport(): Promise { + await this.flush(); + return this.getViewport(); + } + + /** + * Get the visible viewport (what's currently on screen) + * Note: You should use getViewportAfterWrite() for testing after writing data + */ + getViewport(): string[] { + const lines: string[] = []; + const buffer = this.xterm.buffer.active; + + // Get only the visible lines (viewport) + for (let i = 0; i < this.xterm.rows; i++) { + const line = buffer.getLine(buffer.viewportY + i); + if (line) { + lines.push(line.translateToString(true)); + } else { + lines.push(""); + } + } + + return lines; + } + + /** + * Get the entire scroll buffer + */ + getScrollBuffer(): string[] { + const lines: string[] = []; + const buffer = this.xterm.buffer.active; + + // Get all lines in the buffer (including scrollback) + for (let i = 0; i < buffer.length; i++) { + const line = buffer.getLine(i); + if (line) { + lines.push(line.translateToString(true)); + } else { + lines.push(""); + } + } + + return lines; + } + + /** + * Clear the terminal viewport + */ + clear(): void { + this.xterm.clear(); + } + + /** + * Reset the terminal completely + */ + reset(): void { + this.xterm.reset(); + } + + /** + * Get cursor position + */ + getCursorPosition(): { x: number; y: number } { + const buffer = this.xterm.buffer.active; + return { + x: buffer.cursorX, + y: buffer.cursorY, + }; + } + + /** Wait for TUI's throttled render pipeline to settle. */ + async waitForRender(): Promise { + await new Promise((resolve) => process.nextTick(resolve)); + await new Promise((resolve) => setTimeout(resolve, 20)); + await this.flush(); + } +} diff --git a/packages/tui/test/word-navigation.test.ts b/packages/tui/test/word-navigation.test.ts new file mode 100644 index 0000000000000000000000000000000000000000..2e58e960bb00ba21297f15a687162596d8af4392 --- /dev/null +++ b/packages/tui/test/word-navigation.test.ts @@ -0,0 +1,191 @@ +import assert from "node:assert"; +import { describe, it } from "node:test"; +import { findWordBackward, findWordForward } from "../src/word-navigation.ts"; + +describe("findWordBackward", () => { + it("basic words: hello world", () => { + const text = "hello world"; + assert.strictEqual(findWordBackward(text, 11), 6); + assert.strictEqual(findWordBackward(text, 6), 0); + }); + + it("dotted: foo.bar", () => { + const text = "foo.bar"; + assert.strictEqual(findWordBackward(text, 7), 4); + assert.strictEqual(findWordBackward(text, 4), 3); + assert.strictEqual(findWordBackward(text, 3), 0); + }); + + it("colon: foo:bar", () => { + const text = "foo:bar"; + assert.strictEqual(findWordBackward(text, 7), 4); + assert.strictEqual(findWordBackward(text, 4), 3); + assert.strictEqual(findWordBackward(text, 3), 0); + }); + + it("path: path/to/file", () => { + const text = "path/to/file"; + assert.strictEqual(findWordBackward(text, 12), 8); + assert.strictEqual(findWordBackward(text, 8), 7); + // "/to" is one word-like segment with "/" as punctuation boundary + assert.strictEqual(findWordBackward(text, 7), 5); + assert.strictEqual(findWordBackward(text, 5), 4); + assert.strictEqual(findWordBackward(text, 4), 0); + }); + + it("CJK mixed", () => { + const text = "你好世界 test"; + assert.strictEqual(findWordBackward(text, text.length), 5); + // Intl.Segmenter treats each CJK char as a separate word-like segment + assert.strictEqual(findWordBackward(text, 5), 2); + assert.strictEqual(findWordBackward(text, 2), 0); + }); + + it("whitespace at boundaries", () => { + const text = " hello "; + assert.strictEqual(findWordBackward(text, 9), 2); + assert.strictEqual(findWordBackward(text, 2), 0); + }); + + it("punctuation run: foo...bar", () => { + const text = "foo...bar"; + assert.strictEqual(findWordBackward(text, 9), 6); + assert.strictEqual(findWordBackward(text, 6), 3); + assert.strictEqual(findWordBackward(text, 3), 0); + }); + + it("cursor at 0 returns 0", () => { + assert.strictEqual(findWordBackward("hello", 0), 0); + }); +}); + +describe("findWordForward", () => { + it("basic words: hello world", () => { + const text = "hello world"; + assert.strictEqual(findWordForward(text, 0), 5); + assert.strictEqual(findWordForward(text, 5), 11); + }); + + it("dotted: foo.bar", () => { + const text = "foo.bar"; + assert.strictEqual(findWordForward(text, 0), 3); + assert.strictEqual(findWordForward(text, 3), 4); + assert.strictEqual(findWordForward(text, 4), 7); + }); + + it("colon: foo:bar", () => { + const text = "foo:bar"; + assert.strictEqual(findWordForward(text, 0), 3); + assert.strictEqual(findWordForward(text, 3), 4); + assert.strictEqual(findWordForward(text, 4), 7); + }); + + it("path: path/to/file", () => { + const text = "path/to/file"; + assert.strictEqual(findWordForward(text, 0), 4); + assert.strictEqual(findWordForward(text, 4), 5); + assert.strictEqual(findWordForward(text, 5), 7); + assert.strictEqual(findWordForward(text, 7), 8); + assert.strictEqual(findWordForward(text, 8), 12); + }); + + it("CJK mixed", () => { + const text = "你好世界 test"; + const firstEnd = findWordForward(text, 0); + assert.ok(firstEnd > 0); + assert.ok(firstEnd <= 4); + // Walk to end + let pos = 0; + while (pos < text.length) { + const next = findWordForward(text, pos); + if (next === pos) break; + pos = next; + } + assert.strictEqual(pos, text.length); + }); + + it("whitespace at boundaries", () => { + const text = " hello "; + assert.strictEqual(findWordForward(text, 0), 7); + assert.strictEqual(findWordForward(text, 7), 9); + }); + + it("punctuation run: foo...bar", () => { + const text = "foo...bar"; + assert.strictEqual(findWordForward(text, 0), 3); + assert.strictEqual(findWordForward(text, 3), 6); + assert.strictEqual(findWordForward(text, 6), 9); + }); + + it("cursor at end returns end", () => { + assert.strictEqual(findWordForward("hello", 5), 5); + }); +}); + +describe("atomic segments", () => { + const marker = "[paste #1 +5 lines]"; + const text = `hello ${marker} world`; + const isAtomic = (s: string) => s === marker; + + // The functions slice text before calling segment(), so we map each expected + // substring to its pre-split segments. + const segmentMap = new Map([ + [ + text, // full text (not used but for clarity) + [ + { segment: "hello", index: 0, input: text, isWordLike: true }, + { segment: " ", index: 5, input: text, isWordLike: false }, + { segment: marker, index: 6, input: text, isWordLike: true }, + { segment: " ", index: 25, input: text, isWordLike: false }, + { segment: "world", index: 26, input: text, isWordLike: true }, + ], + ], + [ + // backward from end: slice(0, 31) = full text + text.slice(0, text.length), + [ + { segment: "hello", index: 0, input: text, isWordLike: true }, + { segment: " ", index: 5, input: text, isWordLike: false }, + { segment: marker, index: 6, input: text, isWordLike: true }, + { segment: " ", index: 25, input: text, isWordLike: false }, + { segment: "world", index: 26, input: text, isWordLike: true }, + ], + ], + [ + // backward from 26: slice(0, 26) = "hello [paste #1 +5 lines] " + text.slice(0, 26), + [ + { segment: "hello", index: 0, input: text, isWordLike: true }, + { segment: " ", index: 5, input: text, isWordLike: false }, + { segment: marker, index: 6, input: text, isWordLike: true }, + { segment: " ", index: 25, input: text, isWordLike: false }, + ], + ], + [ + // forward from 6: slice(6) = "[paste #1 +5 lines] world" + text.slice(6), + [ + { segment: marker, index: 0, input: text, isWordLike: true }, + { segment: " ", index: 19, input: text, isWordLike: false }, + { segment: "world", index: 20, input: text, isWordLike: true }, + ], + ], + ]); + + const opts = { + segment: (input: string) => segmentMap.get(input) ?? [], + isAtomicSegment: isAtomic, + }; + + it("backward skips word then stops before atomic marker", () => { + assert.strictEqual(findWordBackward(text, text.length, opts), 26); + }); + + it("backward skips whitespace then atomic marker as one unit", () => { + assert.strictEqual(findWordBackward(text, 26, opts), 6); + }); + + it("forward skips atomic marker as one unit", () => { + assert.strictEqual(findWordForward(text, 6, opts), 6 + marker.length); + }); +}); diff --git a/packages/tui/test/wrap-ansi.test.ts b/packages/tui/test/wrap-ansi.test.ts new file mode 100644 index 0000000000000000000000000000000000000000..618be2347d36f40f6146c33e1f86c9747519bb94 --- /dev/null +++ b/packages/tui/test/wrap-ansi.test.ts @@ -0,0 +1,266 @@ +import assert from "node:assert"; +import { describe, it } from "node:test"; +import { visibleWidth, wrapTextWithAnsi } from "../src/utils.ts"; + +describe("wrapTextWithAnsi", () => { + describe("underline styling", () => { + it("should not apply underline style before the styled text", () => { + const underlineOn = "\x1b[4m"; + const underlineOff = "\x1b[24m"; + const url = "https://example.com/very/long/path/that/will/wrap"; + const text = `read this thread ${underlineOn}${url}${underlineOff}`; + + const wrapped = wrapTextWithAnsi(text, 40); + + // First line should NOT contain underline code - it's just "read this thread" + assert.strictEqual(wrapped[0], "read this thread"); + + // Second line should start with underline, have URL content + assert.strictEqual(wrapped[1].startsWith(underlineOn), true); + assert.ok(wrapped[1].includes("https://")); + }); + + it("should not have whitespace before underline reset code", () => { + const underlineOn = "\x1b[4m"; + const underlineOff = "\x1b[24m"; + const textWithUnderlinedTrailingSpace = `${underlineOn}underlined text here ${underlineOff}more`; + + const wrapped = wrapTextWithAnsi(textWithUnderlinedTrailingSpace, 18); + + assert.ok(!wrapped[0].includes(` ${underlineOff}`)); + }); + + it("should not bleed underline to padding - each line should end with reset for underline only", () => { + const underlineOn = "\x1b[4m"; + const underlineOff = "\x1b[24m"; + const url = "https://example.com/very/long/path/that/will/definitely/wrap"; + const text = `prefix ${underlineOn}${url}${underlineOff} suffix`; + + const wrapped = wrapTextWithAnsi(text, 30); + + // Middle lines (with underlined content) should end with underline-off, not full reset + // Line 1 and 2 contain underlined URL parts + for (let i = 1; i < wrapped.length - 1; i++) { + const line = wrapped[i]; + if (line.includes(underlineOn)) { + // Should end with underline off, NOT full reset + assert.strictEqual(line.endsWith(underlineOff), true); + assert.strictEqual(line.endsWith("\x1b[0m"), false); + } + } + }); + }); + + describe("background color preservation", () => { + it("should preserve background color across wrapped lines without full reset", () => { + const bgBlue = "\x1b[44m"; + const reset = "\x1b[0m"; + const text = `${bgBlue}hello world this is blue background text${reset}`; + + const wrapped = wrapTextWithAnsi(text, 15); + + // Each line should have background color + for (const line of wrapped) { + assert.ok(line.includes(bgBlue)); + } + + // Middle lines should NOT end with full reset (kills background for padding) + for (let i = 0; i < wrapped.length - 1; i++) { + assert.strictEqual(wrapped[i].endsWith("\x1b[0m"), false); + } + }); + + it("should reset underline but preserve background when wrapping underlined text inside background", () => { + const underlineOn = "\x1b[4m"; + const underlineOff = "\x1b[24m"; + const reset = "\x1b[0m"; + + const text = `\x1b[41mprefix ${underlineOn}UNDERLINED_CONTENT_THAT_WRAPS${underlineOff} suffix${reset}`; + + const wrapped = wrapTextWithAnsi(text, 20); + + // All lines should have background color 41 (either as \x1b[41m or combined like \x1b[4;41m) + for (const line of wrapped) { + const hasBgColor = line.includes("[41m") || line.includes(";41m") || line.includes("[41;"); + assert.ok(hasBgColor); + } + + // Lines with underlined content should use underline-off at end, not full reset + for (let i = 0; i < wrapped.length - 1; i++) { + const line = wrapped[i]; + // If this line has underline on, it should end with underline off (not full reset) + if ( + (line.includes("[4m") || line.includes("[4;") || line.includes(";4m")) && + !line.includes(underlineOff) + ) { + assert.strictEqual(line.endsWith(underlineOff), true); + assert.strictEqual(line.endsWith("\x1b[0m"), false); + } + } + }); + }); + + describe("basic wrapping", () => { + it("should handle LF, CRLF, and CR line endings", () => { + assert.deepStrictEqual(wrapTextWithAnsi("first\nsecond\r\nthird\rfourth", 80), [ + "first", + "second", + "third", + "fourth", + ]); + }); + + it("should preserve ANSI state across CRLF and CR line endings", () => { + const red = "\x1b[31m"; + const reset = "\x1b[0m"; + + assert.deepStrictEqual(wrapTextWithAnsi(`${red}first\r\nsecond\rthird${reset}`, 80), [ + `${red}first`, + `${red}second`, + `${red}third${reset}`, + ]); + }); + + it("should wrap plain text correctly", () => { + const text = "hello world this is a test"; + const wrapped = wrapTextWithAnsi(text, 10); + + assert.ok(wrapped.length > 1); + for (const line of wrapped) { + assert.ok(visibleWidth(line) <= 10); + } + }); + + it("should break CJK runs at grapheme boundaries after Latin text", () => { + const text = "This is an example 中文汉字测试段落内容中文汉字测试段落内容."; + const wrapped = wrapTextWithAnsi(text, 40); + + assert.deepStrictEqual(wrapped, ["This is an example 中文汉字测试段落内容", "中文汉字测试段落内容."]); + for (const line of wrapped) { + assert.ok(visibleWidth(line) <= 40); + } + }); + + it("should preserve color codes when wrapping CJK runs", () => { + const red = "\x1b[31m"; + const reset = "\x1b[0m"; + const text = `${red}This is an example 中文汉字测试段落内容中文汉字测试段落内容.${reset}`; + const wrapped = wrapTextWithAnsi(text, 40); + + assert.strictEqual(wrapped.length, 2); + assert.strictEqual(wrapped[0], `${red}This is an example 中文汉字测试段落内容`); + assert.strictEqual(wrapped[1], `${red}中文汉字测试段落内容.${reset}`); + for (const line of wrapped) { + assert.ok(visibleWidth(line) <= 40); + } + }); + + it("should ignore OSC 133 semantic markers in visible width", () => { + const text = "\x1b]133;A\x07hello\x1b]133;B\x07"; + assert.strictEqual(visibleWidth(text), 5); + }); + + it("should ignore OSC sequences terminated with ST in visible width", () => { + const text = "\x1b]133;A\x1b\\hello\x1b]133;B\x1b\\"; + assert.strictEqual(visibleWidth(text), 5); + }); + + it("should treat isolated regional indicators as width 2", () => { + assert.strictEqual(visibleWidth("🇨"), 2); + assert.strictEqual(visibleWidth("🇨🇳"), 2); + }); + + it("should truncate trailing whitespace that exceeds width", () => { + const twoSpacesWrappedToWidth1 = wrapTextWithAnsi(" ", 1); + assert.ok(visibleWidth(twoSpacesWrappedToWidth1[0]) <= 1); + }); + + it("should preserve color codes across wraps", () => { + const red = "\x1b[31m"; + const reset = "\x1b[0m"; + const text = `${red}hello world this is red${reset}`; + + const wrapped = wrapTextWithAnsi(text, 10); + + // Each continuation line should start with red code + for (let i = 1; i < wrapped.length; i++) { + assert.strictEqual(wrapped[i].startsWith(red), true); + } + + // Middle lines should not end with full reset + for (let i = 0; i < wrapped.length - 1; i++) { + assert.strictEqual(wrapped[i].endsWith("\x1b[0m"), false); + } + }); + }); +}); + +describe("wrapTextWithAnsi with OSC 8 hyperlinks", () => { + it("re-emits OSC 8 open at the start of continuation lines", () => { + // A hyperlink whose text is long enough to wrap + const url = "https://example.com"; + // OSC 8 open + text that is 10 visible chars + OSC 8 close + const input = `\x1b]8;;${url}\x1b\\0123456789\x1b]8;;\x1b\\`; + const lines = wrapTextWithAnsi(input, 6); + + // Every line that contains visible text from inside the hyperlink + // should start with the OSC 8 open sequence (or be preceded by it). + for (const line of lines) { + // If the line has visible content it must begin with the OSC 8 re-open + // OR it is the line where the close appeared with no following content. + const stripped = line.replace(/\x1b\]8;;[^\x1b\x07]*\x1b\\/g, "").replace(/\x1b\[[0-9;]*m/g, ""); + if (stripped.trim().length > 0) { + assert.ok( + line.startsWith(`\x1b]8;;${url}\x1b\\`) || line.includes(`\x1b]8;;${url}\x1b\\`), + `Line "${line}" has visible text but no OSC 8 re-open`, + ); + } + } + }); + + it("closes OSC 8 before each line break", () => { + const url = "https://example.com"; + const input = `\x1b]8;;${url}\x1b\\0123456789\x1b]8;;\x1b\\`; + const lines = wrapTextWithAnsi(input, 6); + + for (let i = 0; i < lines.length - 1; i++) { + const line = lines[i]; + // Every non-final line that is inside a hyperlink should end with the close + if (line.includes(`\x1b]8;;${url}\x1b\\`)) { + assert.ok( + line.endsWith("\x1b]8;;\x1b\\"), + `Non-final line "${line}" is inside a hyperlink but does not close it`, + ); + } + } + }); + + it("preserves BEL terminators when wrapping OAuth-style hyperlinks", () => { + const url = `https://example.com/oauth/${"a".repeat(32)}`; + const input = `\x1b]8;;${url}\x07${url}\x1b]8;;\x07`; + const lines = wrapTextWithAnsi(input, 20); + + assert.ok(lines.length > 1); + for (const line of lines) { + assert.ok(line.includes(`\x1b]8;;${url}\x07`), `Line "${line}" does not reopen the hyperlink with BEL`); + assert.ok(!line.includes(`\x1b]8;;${url}\x1b\\`), `Line "${line}" reopens the hyperlink with ST`); + } + for (const line of lines.slice(0, -1)) { + assert.ok(line.endsWith("\x1b]8;;\x07"), `Line "${line}" does not close the hyperlink with BEL`); + } + }); + + it("does not emit OSC 8 sequences on lines that are outside the hyperlink", () => { + const url = "https://example.com"; + const input = `before \x1b]8;;${url}\x1b\\link\x1b]8;;\x1b\\ after`; + const lines = wrapTextWithAnsi(input, 80); + + // With width 80 everything fits on one line; there should be exactly one + // OSC 8 open and one OSC 8 close. + assert.strictEqual(lines.length, 1); + const openCount = (lines[0].match(/\x1b\]8;;https:[^\x1b]+\x1b\\/g) ?? []).length; + const closeCount = (lines[0].match(/\x1b\]8;;\x1b\\/g) ?? []).length; + assert.strictEqual(openCount, 1); + assert.strictEqual(closeCount, 1); + }); +});