diff --git a/app/src/components/channels/chat-transcript.tsx b/app/src/components/channels/chat-transcript.tsx index 73d48f1f55..759d041806 100644 --- a/app/src/components/channels/chat-transcript.tsx +++ b/app/src/components/channels/chat-transcript.tsx @@ -202,14 +202,19 @@ import { } from "./telemetry-assistant-message"; import { ActivityAppMarks, + AppMark, + appOfCall, attachmentReadName, dropRepeatedAppCards, groupToolSteps, - LiveConnectorStep, + LineShowsApp, + onlyApp, + rowApps, ServerStepIcon, ToolActivity, type ToolGroup, type TranscriptItem, + useAppName, useDrawnTool, useReportsDesktopActivity, } from "./tool-activity"; @@ -225,6 +230,7 @@ import { type Turn, turnsOf, } from "./transcript-turns"; +import { TurnLine } from "./turn-line"; import { VoiceChatCard } from "./voice-chat-card"; type ChatTranscriptProps = { @@ -1949,7 +1955,6 @@ const ToolCallBody = memo(function ToolCallBody({ result, latest, spokeAfter, - liveConnector, }: { toolCallId: string; name: string; @@ -1959,7 +1964,6 @@ const ToolCallBody = memo(function ToolCallBody({ latest: boolean; /** The Bot said something after this call in its turn. See `ToolPlace`. */ spokeAfter: boolean; - liveConnector?: boolean; }) { const renderToolCall = useRenderToolCall(); const toolCall = useMemo( @@ -2029,7 +2033,6 @@ const ToolCallBody = memo(function ToolCallBody({ ) : ( { @@ -2130,8 +2134,8 @@ const ToolRow = memo(function ToolRow({ inFlight, latest, spokeAfter, - conversation, expanded, + folds, workAlone, }: ToolRowProps) { const [only] = steps; @@ -2143,13 +2147,16 @@ const ToolRow = memo(function ToolRow({ only && !workAlone && !isBrowserStep(only) && + // Drawn open among a turn line's rows, a lone app call is a step as theirs are, not a line of + // its own beside them (`ToolActivity`). Any other lone call keeps the line that says what it + // did in its own words, drawn there on the rows' grid (`KeepChevronSlot`). + !(expanded && folds && appOfCall(only.toolCall.function.name)) && // A subtask is drawn as work even alone, so a second call joining it does not change its shape. only.toolCall.function.name !== SUBAGENT_TOOL ? ( @@ -2182,7 +2188,6 @@ export function ServerToolLine({ args, result, place, - liveConnector, }: { name: string; /** What the call was given, as the model wrote it. Read only for what a step acted on. */ @@ -2190,7 +2195,6 @@ export function ServerToolLine({ result?: string; /** Where this call sits in the conversation, for an app card that may carry the task on. */ place?: CardPlace; - liveConnector?: boolean; }) { const running = result === undefined; // Cut off with its outcome unknown: named as it is while under way, since nobody knows it was done. @@ -2200,10 +2204,16 @@ export function ServerToolLine({ const appId = !role && name.startsWith("mcp__") ? name.split("__")[1] : undefined; const folders = useContext(FolderNamesContext); + const appName = useAppName(); + // On the one app the folded line above it shows (`LineShowsApp`), it doesn't draw its logo again. + const lineShows = useContext(LineShowsApp); // The file a file step read or saved, as a computer's own file steps name it; a step on a folder - // on the person's computer names the folder too ("todo.txt in share"). + // on the person's computer names the folder too ("todo.txt in share"). An app's step doesn't say + // its app's name: its logo does, and names it on hover (`AppMark`). const detail = - (appId && named.detail === appId ? connectorName(appId) : named.detail) ?? + (appId && (named.detail === appId || named.detail === connectorName(appId)) + ? undefined + : named.detail) ?? folderStepSubject(name, args, folders) ?? ownToolSubject(name, args) ?? // The attached file a read was of, by the name its answer carries. @@ -2262,12 +2272,6 @@ export function ServerToolLine({ ? readAnotherAccountPrompt(answer) : null; if (another) return ; - if (running && liveConnector && appId) - return ( -
- -
- ); /* * THAT TOOL'S OTHER ANSWERS ARE FOR THE MODEL. An app nobody can name comes back as plain text * telling the Bot to say so plainly and not to browse to it — an instruction, which read by the @@ -2334,7 +2338,15 @@ export function ServerToolLine({ {...(failed && ownToolFailedLabel(name) ? { failedLabel: ownToolFailedLabel(name) ?? undefined } : {})} - icon={} + {...(appId && appId === lineShows + ? {} + : { + icon: appId ? ( + + ) : ( + + ), + })} label={label} refused={refused} failed={failed} @@ -2671,6 +2683,21 @@ function TranscriptRenderersWatch({ ); } +/** + * How far a row of app steps is drawn up under one drawn like it, so the two sit as two steps of one + * list do, 8px apart: a "Worked for" line's rows are 12px apart, and an open list of steps keeps 4px + * above and below it, a call on a line of its own 6px. + */ +const JOINED = { list: "-mt-3", line: "-mt-4" } as const; + +/** + * Whether a row with an app's steps is drawn as an open list of them or as one call's line: in a + * "Worked for" line, a run of them is a list, a lone one in a run included (`ToolRow`). + */ +function stepsShape(item: TranscriptItem): "list" | "line" { + return item.kind === "tools" ? "list" : "line"; +} + export function ChatTranscript({ threadId, busy = false, @@ -2722,9 +2749,29 @@ export function ChatTranscript({ const taskThread = isTaskThread(threadId, items, queryClient); // What waits on the person's approval stays out of the fold, its card with it. const waiting = useWaitingToolCalls(queryClient); + /* + * A conversation's turn is one line from send to done (`TurnLine`): open while the Bot works, + * folded once it is done. A stopped turn is a turn that ended; its line stays open, since nothing + * outside it says what was done. + */ + // The turns that ran while this conversation was open: a turn with no steps folds its line away. + const watchedRef = useRef(new Set()); const folds = taskThread ? foldTurnWork(items, busy, waiting) - : foldTurnWork(items, busy || Boolean(stopped), waiting, "conversation"); + : foldTurnWork(items, busy, waiting, "conversation", watchedRef.current); + for (const work of folds.after?.values() ?? []) + if (work.running) watchedRef.current.add(work.askId); + /* + * Rows that have been part of a turn's line. One that comes out of it again (a step whose card + * the app came to draw only once it had loaded) has been on the page already: drawn on its own, it + * is at rest, not a row arriving, and does not fade in or type itself out a second time. + */ + const wereInLineRef = useRef(new Set()); + for (const id of folds.inside) wereInLineRef.current.add(id); + // The newest turn's line says "Thinking" itself, so the line of its own under the turn isn't drawn. + const turnLineLive = [...(folds.after?.values() ?? [])].some( + (work) => work.running, + ); // What the task read and made, for its Details pane beside the thread (`thread-work.ts`). useEffect(() => { if (taskThread && threadId) @@ -3187,8 +3234,8 @@ export function ChatTranscript({ > = turnStart} latest={index >= latestIndex} @@ -3249,17 +3296,39 @@ export function ChatTranscript({ threadId={taskThread ? threadId : undefined} work={work} > - {work.rows.map((row) => ( - - {rowContent(row.item, row.index, !taskThread)} - - ))} + row.item))} + > + {work.rows.map((row, at) => { + // App steps the Bot's words had split into rows of their own sit as one list: its + // words are drawn outside this line, and the gap they left says nothing in here. Only + // rows drawn alike: a call on a line of its own is a muted line, a list's steps aren't. + const above = work.rows[at - 1]; + const shape = stepsShape(row.item); + const joins = + above !== undefined && + stepsShape(above.item) === shape && + rowApps(above.item).last !== undefined && + rowApps(row.item).first !== undefined; + const drawn = rowContent(row.item, row.index, !taskThread); + return ( + + {joins ? ( +
{drawn}
+ ) : ( + drawn + )} +
+ ); + })} +
); if (taskWork.folds.inside.has(item.id)) return null; // A refused turn's notice, under the last row of the person's message it answered. const refusal = refusedUnder.get(item.id); + const turnLine = taskWork.folds.after?.get(item.id); /* * Under an answer the Bot drew: the way to keep it as an artifact, once its turn is over. Not on * a read-only row, whose presses go nowhere. @@ -3267,7 +3336,7 @@ export function ChatTranscript({ const ask = artifactAsks.get(item.id); const offersArtifact = ask !== undefined && onMakeArtifact !== undefined && !readOnly; - return ( + const row = ( - {rowContent(item, index)} + {wereInLineRef.current.has(item.id) ? ( + + {rowContent(item, index)} + + ) : ( + rowContent(item, index) + )} {/* * Beside the drawn answer, never around it: wrapping the row once its turn ended would mount * the interface again, and an interface drawn in a frame would load again. @@ -3313,6 +3388,47 @@ export function ChatTranscript({ ) : null} ); + if (!turnLine) return row; + const opening = openingProps(turnLine.id); + return [ + row, + /* + * A ROW OF ITS OWN, AFTER THE PERSON'S MESSAGE, FROM THE MOMENT THEY SEND. Keyed by the turn, so + * it is the same line from "Thinking" to "Worked through 2 steps". The column's gap under it is + * taken back: the line keeps its own, twelve pixels, the gap between the rows inside it, so the + * Bot's words below it, once they join it, are where they were, and a line that folds away + * takes its gap with it. + */ + + + {turnLine.rows.map((inside) => + inside.item.kind === "text" ? ( + // What the Bot said on the way: muted, at the size it was said, in its place. +
+ {rowContent(inside.item, inside.index, true)} +
+ ) : ( + + {rowContent(inside.item, inside.index, true)} + + ), + )} +
+
, + ]; }; const hasConnector = visible.some( @@ -3422,7 +3538,7 @@ export function ChatTranscript({ {items .slice(pages.from, pages.split) - .map((item, offset) => + .flatMap((item, offset) => renderRow(item, pages.from + offset), )} @@ -3436,7 +3552,8 @@ export function ChatTranscript({ reason={stopped} /> ) - ) : waitingOnTheModel || reconnectingNow ? ( + ) : (waitingOnTheModel || reconnectingNow) && + !turnLineLive ? ( ) : null} {working} @@ -3461,7 +3578,7 @@ export function ChatTranscript({ {items .slice(pages.split) - .map((item, offset) => + .flatMap((item, offset) => renderRow(item, pages.split + offset), )} diff --git a/app/src/components/channels/subtask-step.tsx b/app/src/components/channels/subtask-step.tsx index d826b3b93d..0766d268d9 100644 --- a/app/src/components/channels/subtask-step.tsx +++ b/app/src/components/channels/subtask-step.tsx @@ -14,6 +14,7 @@ import { StepOutput, StepRow, stepDetails, + stepOpens, } from "./tool-activity"; type ToolItem = Extract; @@ -134,9 +135,14 @@ export function SubtaskStep({ {handedBack ? : null} {step.output ? : null} {steps.length ? ( -
    - {foldRereads(steps).map((inner) => ( - +
      + {foldRereads(steps).map((inner, _, rows) => ( + ))}
    ) : null} diff --git a/app/src/components/channels/tool-activity.tsx b/app/src/components/channels/tool-activity.tsx index 70caa04ad3..a05ce0b0b6 100644 --- a/app/src/components/channels/tool-activity.tsx +++ b/app/src/components/channels/tool-activity.tsx @@ -50,7 +50,6 @@ import { IconFilePlus, IconFileText, IconFolder, - IconLoader2, IconPlug, IconSearch, IconSend, @@ -62,9 +61,15 @@ import { import { appVendorLogo, appVendorMark, + LOGO_TILE, PluginLogo, } from "@/components/plugins/plugin-logo"; import { settingsToolCardFor } from "@/components/settings-tools/settings-tool-card"; +import { + Tooltip, + TooltipContent, + TooltipTrigger, +} from "@/components/ui/tooltip"; import { useOverflows } from "@/hooks/use-overflows"; import { appsQueryOptions } from "@/lib/apps/queries"; import { readOutcome } from "@/lib/computers/activity"; @@ -109,7 +114,7 @@ import type { VisibleChatItem } from "./chat-messages"; import { codingOutcome, lookStep } from "./coding-tool-cards"; import { ResearchResults } from "./research-results"; import { SubtaskLiveLine, SubtaskStep } from "./subtask-step"; -import { ToolLine, useDrawnOnceOpened } from "./tool-line"; +import { KeepChevronSlot, ToolLine, useDrawnOnceOpened } from "./tool-line"; type ToolItem = Extract; @@ -541,21 +546,37 @@ export function stepDetails(step: ToolItem): StepDetails { return detailsOf(step, new Map()); } +/** An app's name from its key: as it was added here (`useAppName`), or its key in words. */ +export type AppName = (appId: string) => string; + /** * `stepDetails`, naming a folder step's folder from the conversation's folder names. `live`: the * steps' turn is still going. Once it is over, a command with no answer was stopped under way. + * `appName`: an app step's app, by the name it was added under (`useAppName`). */ export function stepDetailsIn( folders: FolderNames, live = true, + appName: AppName = connectorName, ): (step: ToolItem) => StepDetails { - return (step) => detailsOf(step, folders, live); + return (step) => detailsOf(step, folders, live, appName); +} + +/** + * The words beside an app's action, with the app said by its name: `readToolName` names it from its + * key ("Googlecalendar"), which only the apps added here know as "Google Calendar". + */ +function appDetail(detail: string, appId: string, appName: AppName): string { + return detail === appId || detail === connectorName(appId) + ? appName(appId) + : detail; } function detailsOf( step: ToolItem, folders: FolderNames, live = true, + appName: AppName = connectorName, ): StepDetails { if (cutOffStep(step.result)) { // Read as the step it was while under way, then marked: what the Bot was told is not shown. @@ -565,7 +586,7 @@ function detailsOf( subtask: _subtask, terminal: _terminal, ...details - } = detailsOf(underWay, folders); + } = detailsOf(underWay, folders, true, appName); return { ...details, // A subtask's own row reads its result; a cut-off one has none, so it is an ordinary step. @@ -654,7 +675,8 @@ function detailsOf( if (name === READ_RESULT_PART) { // The app's own answer, read on: its step is the app's, as the app's read is ("Used Fireflies"). const app = appOfRead(args); - const detail = partRead(args, part); + // Its app is its logo (`AppMark`), as on every app step: "List events, part 2 of 3". + const detail = partRead(args, part, appName, false); return { ...outcome, label: readToolName(name, outcome.pending).label, @@ -663,7 +685,7 @@ function detailsOf( inputs: [], pages: 0, ...(app - ? { kind: "connector", connector: connectorName(app), appId: app } + ? { kind: "connector", connector: appName(app), appId: app } : { kind: "tool" }), }; } @@ -738,14 +760,14 @@ function detailsOf( pages: Math.max(urls.length, 1), }; } + // Words beside the action that were only the app's name go: the row's logo says it (`AppMark`). + const shown = detail ? appDetail(detail, server, appName) : undefined; return { ...base, kind: "connector", - ...(detail - ? { detail: detail === server ? connectorName(server) : detail } - : {}), + ...(shown && shown !== appName(server) ? { detail: shown } : {}), pages: 0, - connector: connectorName(server), + connector: appName(server), appId: server, }; } @@ -783,18 +805,27 @@ function appOfRead(args: Record): string | undefined { return prefix === "mcp" && server ? server : undefined; } -/** The words beside a `read_result_part` step: see `readOf`. */ +/** + * The words beside a `read_result_part` step: see `readOf`. `withApp` false leaves the app out, for a + * row whose logo says it: "Get transcript, part 2 of 3". + */ function partRead( args: Record, part: { part?: number; parts?: number } | undefined, + appName: AppName, + withApp = true, ): string | undefined { const whole = readOf(args); const read = whole ? readToolName(whole) : undefined; - const named = read - ? read.detail - ? `${read.label} on ${read.detail}` - : read.label + const app = appOfRead(args); + const said = read?.detail + ? app + ? appDetail(read.detail, app, appName) + : read.detail : undefined; + // `withApp` false: the app is left out where its logo already says it (`AppMark`). + const on = withApp || !app || said !== appName(app) ? said : undefined; + const named = read ? (on ? `${read.label} on ${on}` : read.label) : undefined; const number = part?.part ?? (typeof args.part === "number" @@ -1209,7 +1240,9 @@ export function StepIcon({ aria-hidden className={`flex ${size} shrink-0 items-center justify-center [&_img]:size-full`} > - {logo || !Brand ? ( + {typeof logo === "string" && logo ? ( + + ) : logo || !Brand ? ( ) : ( @@ -1220,40 +1253,65 @@ export function StepIcon({ return ; } +/* + * A STEP'S LOGO KEEPS ITS INITIAL UNTIL THE PICTURE IS IN. Filmed, an app's step showed its initial + * while the apps were looked up, then nothing while the logo loaded, then the logo: three marks in + * half a second where one was meant. The initial stays under the picture until it has loaded, in the + * same box, so the mark goes from initial to logo once. A logo already loaded shows at once. + */ +function StepLogo({ logo, name }: { logo: string; name: string }) { + const [shown, setShown] = useState<"loading" | "loaded" | "failed">( + "loading", + ); + return ( + + {shown === "loaded" ? null : ( + // Drawn, not written: the letter is no part of what the row says. + + )} + {shown === "failed" ? null : ( + setShown("failed")} + onLoad={() => setShown("loaded")} + ref={(image) => { + if (image?.complete && image.naturalWidth > 0) setShown("loaded"); + }} + referrerPolicy="no-referrer" + src={logo} + /> + )} + + ); +} + type ActivityApp = { id: string; title: string; logo: string | null }; const ActivityApps = createContext( undefined, ); -export function LiveConnectorStep({ - appId, - label, -}: { - appId: string; - label: string; -}) { +/** + * An app's name as it was added here ("Google Calendar"), read from the apps the transcript looked + * up for its icons (`ActivityAppMarks`), or its key in words until they are in. + * + * ONE NAME FOR AN APP ON EVERY ROW. An app's step was named from its key, so Google Calendar's + * finished steps read "Googlecalendar", while its step still running, drawn by a component of its + * own, read "Google Calendar". Every step now names its app here, running or done. + */ +export function useAppName(): AppName { const apps = useContext(ActivityApps); - const title = - apps?.find((app) => app.id === appId)?.title ?? connectorName(appId); - return ( - - - - {title} - - · - - {label} - - - In progress - + return useCallback( + (appId: string) => + apps?.find((app) => app.id === appId)?.title ?? connectorName(appId), + [apps], ); } @@ -1283,30 +1341,69 @@ export function ActivityAppMarks({ * and a magnifying glass with its handle cut off reads as the letter C. */ function IconStack({ marks }: { marks: { kind: StepKind; appId?: string }[] }) { + const appName = useAppName(); + const inTurnLine = useContext(KeepChevronSlot); const icons = Array.from( new Map(marks.map((mark) => [mark.appId ?? mark.kind, mark])).values(), ); if (icons.length === 1) { const [mark] = icons; - if (mark?.appId) return ; + if (mark?.appId) + return ; + // In a turn line every row's mark is bare, as its steps' rows draw theirs (`StepMark`). + if (mark && inTurnLine) return ; } + /* + * AN APP'S LOGO NAMES IT WHEREVER IT IS. A step's row named its app on hover (`AppMark`), but the + * folded line above, "Worked through 8 steps" beside Gmail's, Slack's and Google Calendar's logos, + * named none: the same logo told something in one place and nothing in the other. + */ return ( - - {icons.map(({ kind, appId }, index) => ( - 0 ? "-ml-1.5" : "" - }`} - > - - - ))} + + {icons.map(({ kind, appId }, index) => { + const style = { zIndex: icons.length - index }; + const shape = `relative flex size-6 items-center justify-center rounded-full bg-muted text-muted-foreground ring-2 ring-background ${ + index > 0 ? "-ml-1.5" : "" + }`; + const icon = ( + + ); + return appId ? ( + + + } + > + {icon} + + {appName(appId)} + + ) : ( + + {icon} + + ); + })} ); } +/** The app a call is a step on (`mcp__googlecalendar__list_events`), or none for any other call. */ +export function appOfCall(name: string): string | undefined { + return serverStepKind(name) === "connector" ? name.split("__")[1] : undefined; +} + /** The kind of step a call the server ran is, as `stepDetails` names it: research, an app, or one of ours. */ function serverStepKind(name: string): StepKind { // A part of an attached file read: a file, as the folded row draws it. @@ -1363,48 +1460,187 @@ export function StepStatus({ return null; } +/** + * The one app a folded line already shows ("Used Google Calendar", or "Google Calendar Worked + * through 2 steps" beside its logo). Its steps on that app don't draw its logo again. + * + * ONE LOGO FOR ONE APP. Opened, "Used Google Calendar" listed three steps, each beside the Google + * Calendar logo the line above them had just drawn: the same mark four times, telling nothing the + * first didn't. Where the steps are on several apps, each still draws its own, since that is how + * they are told apart. + */ +export const LineShowsApp = createContext(undefined); + +export { KeepChevronSlot } from "./tool-line"; + +/** + * The one app the steps in `items` are on, leaving out steps on no app ("Looked for a tool"); none + * if they are on several apps, or on none. + * + * ONE RULE FOR THE LINE AND ITS ROWS. A turn that looked for a tool, then listed events, read + * "Google Calendar Worked through 2 steps", and opened to "List events" beside the Google + * Calendar logo again: the line named the one app among its logos, the rows asked whether every + * step was on it. Both now ask this. + */ +export function onlyApp(items: readonly TranscriptItem[]): string | undefined { + const apps = new Set(); + for (const item of items) { + const onApps = + item.kind === "tool" + ? [appOfCall(item.toolCall.function.name)] + : item.kind === "tools" + ? item.steps + .map(stepDetails) + .map((step) => + step.kind === "connector" ? step.appId : undefined, + ) + : []; + for (const app of onApps) if (app) apps.add(app); + } + const [app] = apps; + return apps.size === 1 ? app : undefined; +} + +/** + * An app's logo on a step's row, which says the app's name when the pointer rests on it, and to a + * screen reader. + * + * THE LOGO SAYS THE APP. Every app step read "List events Google Calendar", "Search messages + * Slack": the app's name again after every action, beside the logo that already said it. The row + * now says what was done, and its logo which app it was done on; the name is there on hover for a + * logo someone doesn't know. + */ +export function AppMark({ appId, name }: { appId: string; name: string }) { + return ( + + } + > + + + {name} + + ); +} + +/** Whether a step's row draws a mark: every step does, but one on the app its line shows (`shown`). */ +export function drawsMark( + step: StepDetails, + shown: string | undefined, +): boolean { + return !(step.kind === "connector" && step.appId && step.appId === shown); +} + +/** + * A step's mark: an app's logo named on hover (`AppMark`), or the icon for its kind of step. None for + * a step on the app the line above it already shows (`LineShowsApp`). + */ +function StepMark({ step }: { step: StepDetails }) { + const shown = useContext(LineShowsApp); + if (!drawsMark(step, shown)) return null; + return step.kind === "connector" && step.appId ? ( + + ) : ( + + ); +} + +/** + * Whether a step's row opens (`StepRow`): a subtask to its steps, a command to its terminal, and any + * other step but a browser one to what it was given and what came back, when it has either. + */ +export function stepOpens(step: StepDetails): boolean { + if (step.kind === "subtask" || step.terminal) return true; + return ( + step.kind !== "browser" && (step.inputs.length > 0 || Boolean(step.output)) + ); +} + +/** + * The apps a row of steps starts and ends on: a list of steps by its first and last, an app's call + * on a line of its own by its app. None for any other row, or a step that is no app's. + */ +export function rowApps(item: TranscriptItem): { + first?: string; + last?: string; +} { + const lone = + item.kind === "tool" + ? item + : item.kind === "tools" && item.steps.length === 1 + ? item.steps[0] + : undefined; + if (lone) { + const app = appOfCall(lone.toolCall.function.name); + return { first: app, last: app }; + } + if (item.kind !== "tools") return {}; + const steps = foldRereads(item.steps.map(stepDetails)); + const app = (step: StepDetails | undefined) => + step?.kind === "connector" ? step.appId : undefined; + return { first: app(steps[0]), last: app(steps.at(-1)) }; +} + /** One step, as a folded line opens to it. Also how a subtask's card lists the steps it took. */ export function StepRow({ step, active, - liveConnector, + chevronRoom = false, + markInChevron = false, }: { step: StepDetails; active: boolean; - liveConnector?: boolean; + /** + * Another row in its list opens (`stepOpens`): a row that doesn't keeps the chevron's room, so its + * mark and words line up with theirs. + */ + chevronRoom?: boolean; + /** + * The rows in its list that open draw no mark (their app is the line's): a row that doesn't open + * draws its mark where their chevron is, so its words start where theirs do. + */ + markInChevron?: boolean; }) { + const shown = useContext(LineShowsApp); if (step.kind === "subtask") return ; /* * The step still working shimmers, as the group's own line does while it runs. "In progress" in * muted text beside a label drawn exactly like the finished steps above it read as one more static * row: a person watching four "Fetch emails" lines and a fifth with no result took the Bot for dead. + * + * ONE ROW, RUNNING OR DONE. An app's step still running was drawn as a box of its own, the app + * first and the action after it, where every other row, its own finished steps beside it, says the + * action first and the app after, plain. Nothing on the box said more than the row does: it is the + * row, shimmering, with "In progress" where "Blocked" or "Didn't work" would be. */ const running = active && step.pending; - const line = - running && liveConnector && step.appId ? ( - - ) : ( - - - - {step.label} - - {step.href ? ( - - {step.detail ?? step.href} - - ) : step.detail ? ( - {step.detail} - ) : null} - + const lineWith = (mark: ReactNode) => ( + // Spaced as a line of its own is (`ToolLine`), so a step and a lone call line up in one list. + + {mark} + + {step.label} - ); + {step.href ? ( + + {step.detail ?? step.href} + + ) : step.detail ? ( + {step.detail} + ) : null} + + + ); + const line = lineWith(); const reason = step.failed && step.kind === "browser" ? (

    @@ -1422,13 +1658,28 @@ export function StepRow({ ); // A browser step says everything it has in its line. Anything else opens to what it was given // and what came back, which no other place in the transcript shows. - if (step.kind === "browser" || (!step.inputs.length && !step.output)) + if (!stepOpens(step)) { + /* + * THE MARK IN THE CHEVRON'S COLUMN. Under "Google Calendar Worked through 2 steps", "List + * events" is its chevron and its words, no logo; "Looked for a tool" kept the chevron's room and + * drew its wrench after it, so its words started a mark further in. Where the rows that open + * draw no mark, this one's stands where their chevron does, as wide, and its words start where + * theirs do. + */ + const inChevron = markInChevron && drawsMark(step, shown); return ( -

  1. - {line} +
  2. + {inChevron + ? lineWith( + + + , + ) + : line} {reason}
  3. ); + } return (
  4. @@ -1591,7 +1842,7 @@ export function WorkingNow({ steps }: { steps: readonly StepDetails[] }) { ) : (
  5. - + {step.label} @@ -1609,8 +1860,9 @@ export function WorkingNow({ steps }: { steps: readonly StepDetails[] }) { export function stepsUnderWay( items: readonly TranscriptItem[], folders: FolderNames, + appName?: AppName, ): StepDetails[] { - const details = stepDetailsIn(folders); + const details = stepDetailsIn(folders, true, appName); return items.flatMap((item) => item.kind === "tools" ? foldRereads(item.steps.map(details)).filter( @@ -1625,19 +1877,21 @@ export function stepsUnderWay( export function ToolActivity({ group, active, - conversation = false, expanded, }: { group: ToolGroup; /** This is the newest row of a turn still in flight, so a step without a result is running. */ active: boolean; - conversation?: boolean; expanded?: boolean; }) { const folders = useContext(FolderNamesContext); // A command with no answer runs while its turn does, whichever row it is in. const inFlight = useContext(TurnInFlightContext); - const steps = group.steps.map(stepDetailsIn(folders, active || inFlight)); + const lineShows = useContext(LineShowsApp); + const appName = useAppName(); + const steps = group.steps.map( + stepDetailsIn(folders, active || inFlight, appName), + ); // What the line opens to: a run of rereads is one row. See `foldRereads`. const rows = foldRereads(steps); const running = active && steps.some((step) => step.pending); @@ -1661,32 +1915,62 @@ export function ToolActivity({ * each step under way is a line of its own beneath it: the Bot's own, and each subtask with what it * was asked, the step it is on and how many it has taken. Opened, the group lists everything. */ + /* + * A RUN OF APP STEPS IS ITS APPS' LINE, RUNNING OR DONE. While it ran, a run of Google Calendar + * steps was listed open, one row each, and folded into "Used Google Calendar" only once every step + * had its answer. Now it is that line from the start, "Using Google Calendar…", shimmering while a + * step is under way, and opened, it lists the steps as it does once they are done. + */ + const apps = [ + ...new Set( + rows.map((step) => (step.kind === "connector" ? step.connector : null)), + ), + ]; + const appsOnly = apps.every(Boolean); + const using = + appsOnly && apps.length === 1 + ? `Using ${apps[0]}…` + : `Using ${plural(apps.length, "app", "apps")}…`; const live = running ? rows.filter((step) => step.pending && !step.interrupted) : []; - const showLiveSteps = - conversation && - inFlight && - rows.some((step) => step.pending && !step.interrupted) && - rows.every((step) => step.kind === "connector"); - const list = ( -
      + const keepChevronSlot = useContext(KeepChevronSlot); + const chevronRoom = keepChevronSlot || rows.some(stepOpens); + const oneApp = appsOnly && apps.length === 1 ? rows[0]?.appId : undefined; + // The app the line above these rows shows: their own line's, or, drawn open, the one around them. + const shown = expanded ? lineShows : oneApp; + // In a turn line the mark always stands after the chevron's room, so it never moves into it. + const markInChevron = + !keepChevronSlot && + chevronRoom && + !rows.some((step) => stepOpens(step) && drawsMark(step, shown)); + const rowsList = ( + // One size wherever it is opened: a folded line's own body is drawn smaller, a "Worked for" + // line's isn't, and the same steps read at two sizes in one conversation. +
        {rows.map((step) => ( ))}
      ); + // Folded, its own line shows its app; drawn open, the line it sits under does (`LineShowsApp`). + const list = expanded ? ( + rowsList + ) : ( + {rowsList} + ); const approvals = ( step.toolCall.id)} /> ); - if (expanded || showLiveSteps) { + if (expanded) { return ( <> {list} @@ -1703,15 +1987,19 @@ export function ToolActivity({ drawnWhenOpened icon={} openKey={group.id} - live={live.length ? : undefined} + live={ + live.length && !appsOnly ? : undefined + } label={ running && working ? working === 1 ? "Working on a subtask…" : `Working on ${working} subtasks…` - : running && current - ? `${current.label}…` - : summarise(steps) + : running && appsOnly + ? using + : running && current + ? `${current.label}…` + : summarise(steps) } detail={asides.length ? asides.join(" · ") : undefined} running={running} diff --git a/app/src/components/channels/tool-line.tsx b/app/src/components/channels/tool-line.tsx index 720f464af2..1e16170254 100644 --- a/app/src/components/channels/tool-line.tsx +++ b/app/src/components/channels/tool-line.tsx @@ -27,9 +27,25 @@ export const DrawnWhenOpened = createContext(false); */ const opened = new Set(); +/** + * The conversation turn lines the person opened or closed themselves, by turn (`TurnLine`), kept + * for this page only, as the lines above are. + */ +export const chosenTurnLines = new Map(); + +/** + * Inside a conversation's turn line (`TurnLine`), every row keeps the chevron's room, whether or not + * it opens yet: a step's row comes to open once its arguments are in, and the rows of one turn sit on + * one grid however they are grouped, so no row's words move when it, or a row beside it, can open. A + * line of its own drawn there (`ToolLine`) is one more row of that grid: the same room, no margin of + * its own, the same ink as the rows beside it. + */ +export const KeepChevronSlot = createContext(false); + /** Forget every line opened on this page, as a reload does. For tests. */ export function forgetOpenedLines(): void { opened.clear(); + chosenTurnLines.clear(); } /** @@ -89,6 +105,7 @@ export function ToolLine({ label, attempted, detail, + lead, running, refused, failed, @@ -128,6 +145,8 @@ export function ToolLine({ * step, where it is the reason and wraps in full. */ detail?: string; + /** What the line is about, said before what was done ("Google Calendar Worked through 2 steps"). */ + lead?: string; running?: boolean; /** A policy or a boundary said no. Final: nothing the Bot does differently will help. */ refused?: boolean; @@ -183,11 +202,14 @@ export function ToolLine({ const contents = useDrawnOnceOpened(!folded); // Inside a fold that draws once opened, whatever this line holds is at rest too. const atRestAbove = useContext(DrawnWhenOpened); + const inTurnLine = useContext(KeepChevronSlot); const tone = refused ? "text-destructive" : failed ? "text-warning" - : "text-muted-foreground"; + : inTurnLine + ? "" + : "text-muted-foreground"; /* * A REFUSED OR FAILED STEP'S DETAIL IS THE REASON, AND A REASON CUT OFF SAYS NOTHING. Truncated, @@ -207,6 +229,7 @@ export function ToolLine({ } ${saysWhy ? "flex-wrap" : ""}`} > {icon ? {icon} : null} + {lead ? {lead} : null} {/* * Free to shrink, so a long label (a folded run's "Read 1 file, searched for 2 patterns and * listed 1 directory") wraps in a phone-width column instead of running past it. The detail @@ -242,6 +265,11 @@ export function ToolLine({ {detail} ) : null} + {/* + * Said to a screen reader, which hears no shimmer: the same "In progress" a step in a list + * says beside its label (`StepStatus`), on every line still running, whatever it does. + */} + {running ? In progress : null} ); @@ -250,7 +278,15 @@ export function ToolLine({ * is done (a page read, then the pages it read) does not move. As a block, a line with an icon * taller than its words sat two or three pixels lower than the same line opening to something. */ - if (!children) return
      {text}
      ; + if (!children) + return ( +
      + {text} +
      + ); const remember = (event: SyntheticEvent) => { const open = event.currentTarget.open; @@ -263,7 +299,7 @@ export function ToolLine({ const line = (
      void): void { + let called = false; + const once = () => { + if (called) return; + called = true; + done(); + }; + animation.onfinish = once; + setTimeout(once, FOLD_MS + 120); +} + +/** The apps the steps in `items` are on, in the order they were first used. */ +function appsIn(items: readonly TranscriptItem[]): string[] { + const apps: string[] = []; + for (const item of items) { + const onApps = + item.kind === "tool" + ? [appOfCall(item.toolCall.function.name)] + : item.kind === "tools" + ? item.steps + .map(stepDetails) + .map((step) => + step.kind === "connector" ? step.appId : undefined, + ) + : []; + for (const app of onApps) if (app && !apps.includes(app)) apps.push(app); + } + return apps; +} + +/** How many of the steps in `items` were blocked or didn't work, leaving out those in `skip`. */ +function issuesIn( + items: readonly TranscriptItem[], + skip: ReadonlySet, +): number { + return items + .flatMap((item) => + item.kind === "tools" + ? item.steps.map(stepDetails) + : item.kind === "tool" + ? [stepDetails(item)] + : [], + ) + .filter((step) => (step.failed || step.refused) && !skip.has(step.id)) + .length; +} + +/** A step's mark, bare and 16px: its app's logo, named on hover, or the icon for its kind. */ +function MarkOf({ step }: { step: StepDetails }) { + return step.kind === "connector" && step.appId ? ( + + ) : ( + + ); +} + +const NOTHING_MENDED: ReadonlySet = new Set(); + +export function TurnLine({ + work, + reconnecting = false, + mended = NOTHING_MENDED, + children, +}: { + work: TurnWork; + /** Sends that delivered nothing and that the reply then made good (`mendedSends`). */ + mended?: ReadonlySet; + /** The Bot is waiting to reach its model again: the line says so, as "Thinking" did. */ + reconnecting?: boolean; + /** The rows the line holds, drawn as they are on their own. */ + children: ReactNode; +}) { + const folders = useContext(FolderNamesContext); + const appName = useAppName(); + const items = work.rows.map((row) => row.item); + const running = work.running; + const underWay = running ? stepsUnderWay(items, folders, appName) : []; + const current = underWay.at(-1); + const subtasks = underWay.filter((step) => step.kind === "subtask").length; + const apps = appsIn(items); + const steps = stepsIn(work); + /* + * Every step that was blocked or didn't work is counted on the line once the turn is done (while + * it runs, each row says its own). Only those nobody made good keep it open: a send made good a + * moment later (`mendedSends`) is counted, as its quiet line counts it, and what came of it is the + * file, in view under the line. + */ + const failures = issuesIn(items, mended); + const issues = running ? 0 : issuesIn(items, NOTHING_MENDED); + + /* + * OPEN WHILE IT WORKS, FOLDED ONCE IT IS DONE, UNLESS THE PERSON SAID OTHERWISE. A turn with no + * answer outside its line stays open: folded, nothing would be left to read under it. So does a + * turn in which a step was blocked or didn't work: what went wrong stays in view, as a failed step + * always has. + */ + const key = work.id; + const [open, setOpen] = useState( + () => chosen.get(key) ?? (running || !work.answered || failures > 0), + ); + const [drawn, setDrawn] = useState(open); + /* + * A TURN THAT TOOK NO STEPS LEAVES NO LINE. While the Bot thought and wrote, its line said so; once + * the answer is in, there is nothing for it to hold, and it folds away the way a line folds over + * its rows, so the answer rises into its place rather than jumping there. Read back, such a turn + * has no line at all (`foldConversationTurn`), and this ends where that starts. + */ + const empty = items.length === 0; + const [gone, setGone] = useState(!running && empty); + const whole = useRef(null); + const body = useRef(null); + const folding = useRef(null); + const wasRunning = useRef(running); + useEffect(() => { + const ended = wasRunning.current && !running; + wasRunning.current = running; + if (!ended) return; + const still = + typeof window.matchMedia !== "function" || + window.matchMedia("(prefers-reduced-motion: reduce)").matches; + if (empty) { + const line = whole.current; + if (!line || still || typeof line.animate !== "function") { + setGone(true); + return; + } + line.style.overflow = "hidden"; + const away = line.animate( + [ + { height: `${line.offsetHeight}px`, opacity: 1 }, + { height: "0px", opacity: 0 }, + ], + { + duration: FOLD_MS, + easing: "cubic-bezier(0.2, 0, 0, 1)", + fill: "forwards", + }, + ); + whenFolded(away, () => setGone(true)); + return; + } + if (chosen.has(key) || !work.answered || failures > 0) return; + const element = body.current; + if (!element || still || typeof element.animate !== "function") { + setOpen(false); + setDrawn(false); + return; + } + /* + * THE ONE MOTION IN A TURN. The rows fold up under the line, and the answer beneath rises with + * them, because it is laid out under the box whose height this animates. Nothing is rebuilt: the + * same line, the same rows, closed. + */ + const animation = element.animate( + [ + { height: `${element.offsetHeight}px`, opacity: 1 }, + { height: "0px", opacity: 0 }, + ], + { + duration: FOLD_MS, + easing: "cubic-bezier(0.2, 0, 0, 1)", + fill: "forwards", + }, + ); + folding.current = animation; + /* + * FOLDED, IT DRAWS NOTHING BENEATH IT, as every folded line doesn't (`useDrawnOnceOpened`): a + * long turn's thousands of rows are let go once they are out of sight, and drawn again, at rest, + * if the person opens it. + */ + whenFolded(animation, () => { + setOpen(false); + setDrawn(false); + }); + }, [running, key, work.answered, failures, empty]); + // What the person chose is kept while the line is on the page, and goes with it. + useEffect(() => () => void chosen.delete(key), [key]); + // Held folded until the line is closed, then let go, so it never springs open for a frame. + useLayoutEffect(() => { + if (open) return; + folding.current?.cancel(); + folding.current = null; + }, [open]); + + const label = running + ? reconnecting + ? "Reconnecting…" + : subtasks + ? subtasks === 1 + ? "Working on a subtask…" + : `Working on ${subtasks} subtasks…` + : current + ? current.kind === "connector" && current.connector + ? `Using ${current.connector}…` + : // The step's own row, right under the line, says what it is: not said twice. + "Working…" + : // The Bot's newest words are streaming in just below the line. + work.answered + ? "Writing…" + : // As the conversation has always said it: the one word, no ellipsis. + "Thinking" + : steps === 0 + ? "Worked on it" + : `Worked through ${steps} ${steps === 1 ? "step" : "steps"}`; + const lead = !running && apps.length === 1 ? appName(apps[0] ?? "") : null; + // After the app's name, what was done goes on in lower case: "Google Calendar worked through 2 steps". + const said = lead ? label.charAt(0).toLowerCase() + label.slice(1) : label; + const [firstStep] = items.flatMap((item) => + item.kind === "tools" + ? item.steps.map(stepDetails) + : item.kind === "tool" + ? [stepDetails(item)] + : [], + ); + /* + * ONE MARK, ALWAYS 16PX AND BARE: the step under way's, and once the turn is done the logo of each + * app it used, side by side. WHILE THE BOT THINKS, NONE: "Thinking" stands alone, as it always has + * (a spark beside it was disliked), with the mark's room kept empty, so the words don't move when a + * step's mark comes. + */ + const noMark = ; + const mark = running ? ( + current ? ( + + ) : ( + noMark + ) + ) : apps.length ? ( + + {apps.map((app) => ( + + ))} + + ) : firstStep ? ( + + ) : ( + noMark + ); + + const toggle = (event: SyntheticEvent) => { + const now = event.currentTarget.open; + if (now) setDrawn(true); + setOpen(now); + }; + + // Its row stays, at no height, with the room under it taken back (`TurnLine`'s row in the transcript). + if (gone) return