import { Agent, MockProvider, type TurnDelta } from "@keywork/engine"; import { AppCore, type AppCoreOptions, type AppSnapshot } from "./app-core.ts"; import type { ConversationModel } from "./conversation-model.ts"; import { ConversationPane } from "./conversation-pane.ts"; import { definedOnly } from "./defined.ts"; import type { Rect, Screen } from "./geometry.ts"; import { type Chord, parseChord } from "./keys.ts"; import type { PaneFactory } from "./pane-kinds.ts"; import type { PointerEvent, ScrollDirection } from "./pointer.ts"; import type { WorkspaceState } from "./workspace-state.ts"; export interface AppProbeOptions extends Partial< Pick< AppCoreOptions, | "createPane" | "createFilePane" | "createBrowserPane" | "createSessionTreePane" | "createArcsPane" | "createArcPane" | "createMemoryPane" | "createMcpPane" | "createWorkspacesPane" | "createDiffPane" | "createContextPane" | "createPermissionsPane" | "createBotsPane" | "createTerminalPane" | "isDirectory" | "undo" | "exportSession" | "presets" | "inference" | "connections" | "arcs" | "focusedArc" | "bots" | "focusedBot" | "permissions" | "focusedPermissions" | "workspaces" | "workspaceSetup" | "currentModel" | "switchModel" | "externalEditor" | "clipboard" | "readImage" | "restoreWorkspace" | "initialWorkspace" | "saveWorkspace" | "onPaneClosed" > > { screen?: Screen; script?: TurnDelta[][]; } export class AppProbe { readonly core: AppCore; readonly screen: Screen; exited = false; private clockMs = 0; constructor(options: AppProbeOptions = {}) { this.screen = options.screen ?? { width: 120, height: 40 }; const screen = this.screen; const { script, screen: _screen, ...seams } = options; this.core = new AppCore({ ...definedOnly(seams), screen: () => screen, createPane: options.createPane ?? conversationPanes(script), onExit: () => { this.exited = true; }, }); this.core.start(); } keys(...specs: string[]): this { for (const spec of specs) this.press(parseChord(spec), spec.length === 1 ? spec : undefined); return this; } type(text: string): this { for (const character of text) this.press(printableChord(character), character); return this; } repeat(spec: string): this { this.clockMs += 1; this.core.handleKey(parseChord(spec), undefined, this.clockMs, true); return this; } paste(text: string): this { this.core.handlePaste(text); return this; } command(name: string): boolean { return this.core.runCommand(name); } click(x: number, y: number): this { this.point({ type: "down", x, y, button: 0 }); this.point({ type: "up", x, y, button: 0 }); return this; } hover(x: number, y: number): this { this.point({ type: "move", x, y }); return this; } drag(from: { x: number; y: number }, ...path: { x: number; y: number }[]): this { this.dragHold(from, ...path); const last = path.at(-1) ?? from; return this.release(last); } dragHold(from: { x: number; y: number }, ...path: { x: number; y: number }[]): this { this.point({ type: "down", x: from.x, y: from.y, button: 0 }); for (const at of path) this.point({ type: "drag", x: at.x, y: at.y, button: 0 }); return this; } release(at: { x: number; y: number }): this { this.point({ type: "up", x: at.x, y: at.y, button: 0 }); return this; } scroll(x: number, y: number, direction: ScrollDirection, delta = 1): this { this.point({ type: "scroll", x, y, scroll: { direction, delta } }); return this; } rect(id: string): Rect { const rect = this.core.layout.rects(this.screen).get(id); if (rect === undefined) throw new Error(`no pane rect for "${id}"`); return rect; } snapshot(): AppSnapshot { return this.core.snapshot(); } workspaceState(): WorkspaceState { return this.core.workspaceState(); } model(id = this.core.snapshot().focused): ConversationModel | undefined { const pane = id === undefined ? undefined : this.core.panes.get(id); return pane instanceof ConversationPane ? pane.model : undefined; } async settled(): Promise { for (const pane of this.core.panes.values()) await pane.settled?.(); return this; } private press(chord: Chord, sequence: string | undefined): void { this.clockMs += 1; this.core.handleKey(chord, sequence, this.clockMs); } private point(event: PointerEvent): void { this.core.handleMouse(event); } } function conversationPanes(script: TurnDelta[][] | undefined): PaneFactory { return (id, notify, commands, _resumeSessionId, draft) => { const agent = script === undefined ? undefined : new Agent({ provider: new MockProvider(script) }); return new ConversationPane(id, agent, notify, undefined, commands, { ...(draft !== undefined && { initialDraft: draft }), }); }; } function printableChord(character: string): Chord { return { name: character === " " ? "space" : character.toLowerCase(), ctrl: false, shift: false, meta: false, }; }