diff --git a/src/client/session.ts b/src/client/session.ts index e68d4ac..a6fd711 100644 --- a/src/client/session.ts +++ b/src/client/session.ts @@ -4,6 +4,7 @@ import { type OrchestrationGetFullThreadDiffInput, type ReviewDiffFileContentsInput, type OrchestrationV2Checkpoint, + type OrchestrationV2TurnItem, type TurnItemId, MessageId, ORCHESTRATION_PROTOCOL_HEADER, @@ -364,6 +365,12 @@ export class T3Session { return Effect.runPromise(this.requireConnection().rpc[ORCHESTRATION_V2_WS_METHODS.getFullThreadDiff](input)); } + /** One turn item in full: the server leaves long command output out of the thread (`outputOmitted`). */ + async turnItem(threadId: ThreadId, itemId: TurnItemId): Promise { + const result = await Effect.runPromise(this.requireConnection().rpc[ORCHESTRATION_V2_WS_METHODS.getTurnItem]({ threadId, itemId })); + return result.item ?? null; + } + diffFileContents(input: ReviewDiffFileContentsInput) { return Effect.runPromise(this.requireConnection().rpc[WS_METHODS.reviewGetDiffFileContents](input)); } diff --git a/src/ui/App.tsx b/src/ui/App.tsx index d692a3a..c35ad05 100644 --- a/src/ui/App.tsx +++ b/src/ui/App.tsx @@ -2,7 +2,7 @@ import type { KeyEvent, MouseEvent, ScrollBoxRenderable, Selection, TextareaRend import { useKeyboard, useRenderer, useTerminalDimensions } from "@opentui/react"; import { resolveComposerDispatchMode } from "@t3tools/client-runtime/state/composer-dispatch"; import { ATTACHMENT_ONLY_BOOTSTRAP_PROMPT } from "@t3tools/shared/composerContextReferences"; -import type { OrchestrationV2ThreadShell, ProviderApprovalDecision, ThreadId } from "@t3tools/contracts"; +import type { OrchestrationV2ThreadShell, ProviderApprovalDecision, ThreadId, TurnItemId } from "@t3tools/contracts"; import { homedir } from "node:os"; import { useCallback, useEffect, useMemo, useRef, useState } from "react"; @@ -925,13 +925,21 @@ export function App(props: { readonly session: T3Session; readonly config: Tui3C copyText(renderer, text); say(`Copied ${label}`); }; + /** Long output stays on the server until it's asked for. */ + const copyOmittedOutput = (source: { readonly threadId: ThreadId; readonly itemId: TurnItemId }) => () => { + say("Fetching the output…"); + session.turnItem(source.threadId, source.itemId).then( + (item) => (item?.type === "command_execution" && item.output ? copy("the output", item.output) : say("The command left no output.")), + (error) => say(`Could not fetch the output: ${error instanceof Error ? error.message : String(error)}`, true), + ); + }; /** The right-click menu on a message or command, as a picker like the thread menu. */ const openConversationMenu = (target: ConversationTarget) => { const items: PickerItem[] = target.kind === "command" ? [ { id: "copy-command", title: "Copy command", run: () => copy("the command", target.command) }, - ...(target.output ? [{ id: "copy-output", title: "Copy output", run: () => copy("the output", target.output) }] : []), + ...(target.output ? [{ id: "copy-output", title: "Copy output", run: () => copy("the output", target.output) }] : target.omitted && target.source ? [{ id: "copy-output", title: "Copy output", run: copyOmittedOutput(target.source) }] : []), ] : [ { id: "copy-message", title: "Copy message", run: () => copy("the message", target.text) }, diff --git a/src/ui/ThreadView.tsx b/src/ui/ThreadView.tsx index 0043c24..de556a0 100644 --- a/src/ui/ThreadView.tsx +++ b/src/ui/ThreadView.tsx @@ -1,8 +1,8 @@ import type { MouseEvent, ScrollBoxRenderable } from "@opentui/core"; -import type { ProviderApprovalDecision, ThreadId } from "@t3tools/contracts"; +import type { ProviderApprovalDecision, ThreadId, TurnItemId } from "@t3tools/contracts"; import { formatWorkingDurationLabel } from "@t3tools/client-runtime/state/models"; import { liveThoughtLine, workEntryDisplayIndicatesToolFailure, type ToolGroupSummaryKind } from "@t3tools/client-runtime/work-log/presentation"; -import { memo, useState, type RefObject } from "react"; +import { memo, useEffect, useState, type RefObject } from "react"; import { findTextSegments } from "../model/search.ts"; import { findThreadSearchOccurrences } from "@t3tools/shared/threadSearch"; import type { OrchestrationV2SearchThreadResult } from "@t3tools/contracts"; @@ -19,7 +19,8 @@ import { colors, getMarkdownStyle, toneColor } from "./theme.ts"; /** What a right-click in the conversation was on. */ export type ConversationTarget = | { readonly kind: "message"; readonly messageId: string; readonly role: "user" | "assistant" | "system"; readonly text: string } - | { readonly kind: "command"; readonly command: string; readonly output: string }; + /** `omitted`: the server left the output out of the thread; `source` is where to fetch it. */ + | { readonly kind: "command"; readonly command: string; readonly output: string; readonly omitted: boolean; readonly source: { readonly threadId: ThreadId; readonly itemId: TurnItemId } | null }; export interface ConversationMouse { readonly respond: (decision: ProviderApprovalDecision) => void; @@ -229,6 +230,28 @@ function UserMessage({ text, messageId, selected, onSelect, onMenu }: { readonly ); } +/** Output the server left out of the thread, fetched when its row opens. */ +function OmittedOutput({ session, source }: { readonly session: T3Session; readonly source: { readonly threadId: ThreadId; readonly itemId: TurnItemId } | null }) { + const [state, setState] = useState<{ readonly text: string } | { readonly error: string } | null>(null); + const threadId = source?.threadId; + const itemId = source?.itemId; + useEffect(() => { + if (!threadId || !itemId) return; + let cancelled = false; + session.turnItem(threadId, itemId).then( + (item) => !cancelled && setState({ text: item?.type === "command_execution" ? (item.output ?? "") : "" }), + (error) => !cancelled && setState({ error: error instanceof Error ? error.message : String(error) }), + ); + return () => { + cancelled = true; + }; + }, [session, threadId, itemId]); + if (!source) return The output wasn't sent with the thread.; + if (!state) return Loading output…; + if ("error" in state) return {`Could not load the output: ${state.error}`}; + return {state.text || "No output."}; +} + function WorkEntry({ entry, expanded: groupExpanded, label, onOpenThread, onMenu, session, streamCommandOutput, scrollRef }: { readonly entry: WorkLogEntry; readonly expanded: boolean; readonly label: string; readonly onOpenThread: (threadId: ThreadId) => void; readonly onMenu: (target: ConversationTarget) => void; readonly session: T3Session; readonly streamCommandOutput: boolean; readonly scrollRef: RefObject }) { const [open, setExpanded] = useState(null); const [hover, hoverProps] = useHover(); @@ -241,9 +264,12 @@ function WorkEntry({ entry, expanded: groupExpanded, label, onOpenThread, onMenu const tone = failed || entry.tone === "error" ? colors.bad : child ? colors.accent : entry.tone === "thinking" ? colors.faint : colors.muted; const clickable = Boolean(command || child); const text = {`${failed ? "✕" : glyphFor(entry)} ${firstLine(label)}${child ? " ↗" : ""}`}; + const source = projected ? { threadId: projected.sourceThreadId, itemId: projected.sourceItemId } : null; + const streaming = expanded && streamCommandOutput && source !== null; if (command) return - (isRightClick(event) ? onMenu({ kind: "command", command: command.input, output: command.output ?? "" }) : isLeftClick(event) ? setExpanded(!expanded) : undefined)}>{expanded ? "⌄ " : "› "}{text} - {expanded && streamCommandOutput && projected ? + (isRightClick(event) ? onMenu({ kind: "command", command: command.input, output: command.output ?? "", omitted: command.outputOmitted === true, source }) : isLeftClick(event) ? setExpanded(!expanded) : undefined)}>{expanded ? "⌄ " : "› "}{text} + {streaming ? + : expanded && command.outputOmitted ? : expanded ? {command.output || "No output."} : null} ; return child ? isLeftClick(event) && onOpenThread(child)}>{text} : text; diff --git a/test/app/app.test.tsx b/test/app/app.test.tsx index 01d5de3..def306b 100644 --- a/test/app/app.test.tsx +++ b/test/app/app.test.tsx @@ -1,6 +1,6 @@ import { afterEach, describe, expect, test } from "bun:test"; -import { withApproval, withQuestion } from "./fixtures.ts"; +import { omittedCommand, withApproval, withOmittedOutput, withQuestion } from "./fixtures.ts"; import { loadLayout } from "../../src/layout.ts"; import { fakeSession, renderApp, v2ThreadId } from "./harness.tsx"; @@ -243,6 +243,30 @@ describe("connection", () => { }); }); +describe("command output", () => { + const session = () => fakeSession({ projection: withOmittedOutput(), results: { turnItem: { ...omittedCommand, outputOmitted: false, output: "12 pass\n0 fail" } } }); + + test("output the server left out loads when the command opens", async () => { + const app = await open({ session: session() }); + const row = app.find("bun test"); + await app.click(row.x, row.y); + await app.settle(50); + expect(app.calls("turnItem").map((call) => call.args)).toEqual([[omittedCommand.threadId, omittedCommand.id]]); + expect(app.frame()).toContain("12 pass"); + }); + + test("Copy output fetches it first", async () => { + const app = await open({ session: session() }); + const row = app.find("bun test"); + await app.click(row.x, row.y, 2); + await app.type("copy output"); + await app.press("return"); + await app.settle(50); + expect(app.calls("turnItem")).toHaveLength(1); + expect(app.hints()).toBe("Copied the output"); + }); +}); + describe("mouse", () => { test("the header button opens the palette", async () => { const app = await open(); diff --git a/test/app/fixtures.ts b/test/app/fixtures.ts index 45689f1..ea0d1e8 100644 --- a/test/app/fixtures.ts +++ b/test/app/fixtures.ts @@ -31,3 +31,33 @@ export function withQuestion(): OrchestrationV2ThreadProjection { ], } as unknown as OrchestrationV2ThreadProjection; } + +/** A finished command whose long output the server left out of the thread. */ +export const omittedCommand = { + id: "item-cmd", + threadId: v2Projection.thread.id, + runId: null, + nodeId: null, + providerThreadId: null, + providerTurnId: null, + nativeItemRef: null, + parentItemId: null, + ordinal: 0, + status: "completed", + title: null, + startedAt: at, + completedAt: at, + updatedAt: at, + type: "command_execution", + input: "bun test", + outputOmitted: true, + exitCode: 0, +} as const; + +export function withOmittedOutput(): OrchestrationV2ThreadProjection { + return { + ...v2Projection, + turnItems: [omittedCommand], + visibleTurnItems: [{ position: 0, visibility: "local", sourceThreadId: omittedCommand.threadId, sourceItemId: omittedCommand.id, item: omittedCommand }], + } as unknown as OrchestrationV2ThreadProjection; +}