Open History to see the last commit
1import type { ThreadStructureTurnItem } from "@t3tools/client-runtime/state/shell";
2import type { RunId, ToolActivityNativeAppReference } from "@t3tools/contracts";
3
4/** What the floating computer card shows until the live capture's first frame arrives. */
5export interface ComputerUsePreview {
6 readonly itemId: string;
7 readonly app: ToolActivityNativeAppReference | null;
8 readonly appName: string | null;
9 /** The newest computer use call is still running. */
10 readonly inProgress: boolean;
11}
12
13/**
14 * The newest computer use call in the thread, in any provider: Cua Driver
15 * calls carry the `computer` surface and the app they drive.
16 */
17export function selectLatestComputerUse(
18 turnItems: ReadonlyArray<ThreadStructureTurnItem>,
19): ComputerUsePreview | null {
20 for (let index = turnItems.length - 1; index >= 0; index -= 1) {
21 const item = turnItems[index]!;
22 if (item.type !== "dynamic_tool" || item.toolSurface !== "computer") continue;
23 const icon = item.toolIcon ?? item.toolSource?.icon;
24 return {
25 itemId: item.id,
26 app: icon?._tag === "native-app" ? icon.app : null,
27 appName: item.toolSource?.kind === "computer" ? item.toolSource.name : null,
28 inProgress: item.status === "running" || item.status === "pending",
29 };
30 }
31 return null;
32}
33
34/** Whether the running run has used the computer, so the composer can say so beside Stop. */
35export function isRunUsingComputer(
36 turnItems: ReadonlyArray<ThreadStructureTurnItem>,
37 runningRunId: RunId | null,
38): boolean {
39 if (runningRunId === null) return false;
40 return turnItems.some(
41 (item) =>
42 item.runId === runningRunId &&
43 item.type === "dynamic_tool" &&
44 item.toolSurface === "computer",
45 );
46}