Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion docs/app-guidelines.md
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,9 @@ Native's, and builds its own only when neither fits. The development build shows
- **A Bot's work shows as it happens, as in the web app.** From the moment a message is sent there
is one row under it that says what the Bot is doing and only changes its words: "Thinking…",
then each step in the web app's own words while it runs ("Search the web…"), then what the steps
were once they are over ("Took 2 steps"). The reply is written under it as it comes, as markdown,
did once they are over, in one phrase counted by kind as the web app's folded line says it
("Searched the web and read 2 pages"). Opened, each step is a line after its kind's icon, on a thin
branch from that row, and never what the tool answered (`mobile/src/run-steps.ts`). The reply is written under it as it comes, as markdown,
with what is half written closed or held back so no symbols show. Nothing is added to the page and
taken away again, and a reply never steps back to a shorter reading of itself: a run that ended
stays drawn from what the socket carried until the messages read back hold it in full
Expand Down
45 changes: 34 additions & 11 deletions mobile/app/conversation.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,12 @@ import {
} from "../src/live-run";
import { conversationOf, showConversation } from "../src/on-screen";
import { measure, useOrbFlight, type ViewHandle } from "../src/orb-flight";
import {
type KnownCalls,
knownCalls,
NO_CALLS,
stepLines,
} from "../src/run-steps";
import { connectAiRouteFor } from "../src/setup";
import { shownMessages } from "../src/shown-messages";
import { useLoad } from "../src/use-load";
Expand All @@ -128,16 +134,22 @@ type Item =
| { kind: "day"; key: string; label: string }
| { kind: "message"; key: string; message: ConversationMessage }
// `going` is the step of a live run that is still under way, in the present tense: "Searching the web".
| { kind: "steps"; key: string; steps: string[]; going?: string }
| {
kind: "steps";
key: string;
steps: ConversationMessage[];
going?: string;
}
| { kind: "approval"; key: string; request: Approval };

/**
* A step a Bot took rather than something it said: a tool's own line, or a finished reply that only
* called tools. A run of them folds into one line, as the web app's transcript folds them. A reply
* that drew cards is not a step: its cards are drawn.
* called tools. A run of them folds into one line, as the web app's transcript folds them, and is
* said in its words when it is drawn (`stepLines`). A reply that drew cards is not a step: its cards
* are drawn.
*/
function stepWords(message: ConversationMessage): string[] | null {
if (message.role === "tool") return [message.text || "Used a tool."];
function stepsOf(message: ConversationMessage): ConversationMessage[] | null {
if (message.role === "tool") return [message];
if (drawable(message).length > 0) return null;
if (
message.role === "bot" &&
Expand All @@ -147,9 +159,7 @@ function stepWords(message: ConversationMessage): string[] | null {
message.status !== "waiting" &&
message.status !== "failed"
)
return message.toolCalls && message.toolCalls > 1
? [`Used ${message.toolCalls} tools.`]
: ["Used a tool."];
return [message];
return null;
}

Expand Down Expand Up @@ -270,6 +280,13 @@ export function Conversation({
const liveNow = useRef(live);
liveNow.current = live;
const watcher = useRef<{ reconnect(): void } | null>(null);
/*
* Every tool call the socket carried here, the replay's runs that ended included, kept after their runs go: the
* messages read back hold a step as the tool's answer with no name, and it is named from these as the web app
* names it (../src/run-steps.ts).
*/
const [calls, setCalls] = useState<KnownCalls>(NO_CALLS);
useEffect(() => setCalls((held) => knownCalls(held, live)), [live]);
// Whether the socket has gone quiet on a live run: true until it carries an event, and QUIET_MS after its last.
const [quiet, setQuiet] = useState(true);
// The runs Stop was pressed for: they are ending on the server, and the button waits for them.
Expand All @@ -284,6 +301,7 @@ export function Conversation({
useEffect(() => {
if (!channelId || !person) return;
setLive([]);
setCalls(NO_CALLS);
setDelivered(null);
setAwaiting(null);
setStopAsked(new Set());
Expand Down Expand Up @@ -325,7 +343,10 @@ export function Conversation({
ended();
},
onCaughtUp(runId) {
setLive(caughtUp(replay ?? liveNow.current, runId));
const replayed = replay ?? liveNow.current;
// The runs that ended go from what is drawn live, but their steps are still named by what they called.
setCalls((held) => knownCalls(held, replayed));
setLive(caughtUp(replayed, runId));
replay = null;
// Whatever ended while the socket was away is in the messages now.
void refreshHistory();
Expand Down Expand Up @@ -584,7 +605,7 @@ export function Conversation({
label: dayAndTime(message.createdAt),
});
if (!Number.isNaN(at)) previous = at;
const steps = stepWords(message);
const steps = stepsOf(message);
// A step of a live run that is still going is said as what the Bot is doing, under the ones that are over.
const going = liveStep(message) === "running" ? message.text : undefined;
const last = out[out.length - 1];
Expand Down Expand Up @@ -751,6 +772,8 @@ export function Conversation({
className="flex-1"
inverted
data={items}
// A run of steps is said in the words of the calls the socket carried, which can come after its messages.
extraData={calls}
keyExtractor={(item) => item.key}
keyboardDismissMode="interactive"
keyboardShouldPersistTaps="handled"
Expand Down Expand Up @@ -800,7 +823,7 @@ export function Conversation({
label={`${item.going}…`}
/>
) : (
<StepsLine steps={item.steps} />
<StepsLine steps={stepLines(item.steps, calls)} />
)
) : item.kind === "approval" ? (
<ApprovalHere
Expand Down
127 changes: 102 additions & 25 deletions mobile/src/components/transcript.tsx
Original file line number Diff line number Diff line change
@@ -1,46 +1,104 @@
import { Button } from "heroui-native/button";
import { useState } from "react";
import { Pressable, View } from "react-native";
import { IconChevronDown, IconChevronRight, IconTool } from "../icons";
import {
IconChevronDown,
IconChevronRight,
IconFilePlus,
IconFileText,
IconFolder,
IconPlug,
IconSearch,
IconSubtask,
IconTerminal2,
IconTool,
IconWorld,
type TablerIcon,
} from "../icons";
import { type StepKind, type StepLine, summarise } from "../run-steps";
import { cn } from "./cn";
import { Icon } from "./icon";
import { Text } from "./text";

/** Each kind of step's icon, the web app's (`KIND_ICONS` in tool-activity.tsx). */
const KIND_ICONS: Record<StepKind, TablerIcon> = {
search: IconSearch,
fetch: IconFileText,
browser: IconWorld,
connector: IconPlug,
tool: IconTool,
file: IconFileText,
directory: IconFolder,
command: IconTerminal2,
saved: IconFilePlus,
subtask: IconSubtask,
};

/** A step as text: what was done, and what it was done to after it, muted. */
function StepText({ step, quiet }: { step: StepLine; quiet?: boolean }) {
return (
<Text
tone={quiet ? "muted" : undefined}
numberOfLines={1}
className="flex-1 text-sm leading-5"
>
{step.label}
{step.detail ? (
<Text
tone="muted"
className={cn("text-sm leading-5", quiet && "opacity-70")}
>{` ${step.detail}`}</Text>
) : null}
</Text>
);
}

/**
* What a Bot did between two things it said, folded into one quiet line, as the web app's transcript
* folds a run of steps: "Took 3 steps", which opens to each one. A single step is its own line. The
* transcript keeps the Bot's words and the person's in front, and the working out behind a tap.
* folds a run of steps: what they did, in one phrase ("Searched the web and read 2 pages"), which opens
* to each one. A single step is its own line. The transcript keeps the Bot's words and the person's in
* front, and the working out behind a tap.
*
* Each step is a line in the web app's words (../run-steps.ts), after its kind's icon: what was done,
* and what it was done to after it, muted, cut short at the end of the line. Never what the tool
* answered. Opened, the steps hang from the line on a thin branch, the last one closing it.
*/
export function StepsLine({ steps }: { steps: string[] }) {
export function StepsLine({ steps }: { steps: StepLine[] }) {
const [open, setOpen] = useState(false);
const [only] = steps;
if (!only) return null;
// As tall as a Bot's working row and set in as far (`MessageWorking`), which this row takes the place of when a
// run's steps are over: the words change, and nothing on the page moves.
if (steps.length === 1)
return (
<View className="min-h-6 flex-row items-start gap-2 py-0.5">
<View className="w-5 items-center pt-[3px]">
<Icon icon={IconTool} size={14} color="accent-muted" />
<Icon icon={KIND_ICONS[only.kind]} size={14} color="accent-muted" />
</View>
<Text tone="muted" className="flex-1 text-sm leading-5">
{steps[0]}
</Text>
<StepText step={only} quiet />
</View>
);
const summary = summarise(steps);
return (
<View>
<Pressable
accessibilityRole="button"
accessibilityState={{ expanded: open }}
accessibilityLabel={`Took ${steps.length} steps`}
accessibilityHint={open ? "Hides them" : "Shows each one"}
accessibilityLabel={summary}
accessibilityHint={open ? "Hides each step" : "Shows each step"}
hitSlop={8}
onPress={() => setOpen((now) => !now)}
className="min-h-6 flex-row items-center gap-2 self-start active:opacity-60"
className="min-h-6 max-w-full flex-row items-center gap-2 self-start active:opacity-60"
>
<View className="w-5 items-center">
<Icon icon={IconTool} size={14} color="accent-muted" />
<Icon icon={KIND_ICONS[only.kind]} size={14} color="accent-muted" />
</View>
<Text tone="muted" className="text-sm leading-5">
Took {steps.length} steps
<Text
tone="muted"
numberOfLines={1}
className="shrink text-sm leading-5"
>
{summary}
</Text>
<Icon
icon={open ? IconChevronDown : IconChevronRight}
Expand All @@ -49,17 +107,36 @@ export function StepsLine({ steps }: { steps: string[] }) {
/>
</Pressable>
{open ? (
<View className="mt-2 ml-2.5 gap-1.5 border-border border-l pl-[17px]">
{steps.map((step, index) => (
<Text
// biome-ignore lint/suspicious/noArrayIndexKey: the steps of one run, in their order
key={index}
tone="muted"
className="text-sm leading-5"
>
{step}
</Text>
))}
<View className="mt-1 ml-2.5">
{steps.map((step, index) => {
const last = index === steps.length - 1;
return (
<View
// biome-ignore lint/suspicious/noArrayIndexKey: the steps of one run, in their order
key={index}
className="min-h-7 flex-row items-center"
>
{/* The branch: down past every step but the last, which it turns into and ends at. */}
<View className="w-4 self-stretch">
<View
className={cn(
"absolute top-0 left-0 w-px bg-border",
last ? "h-1/2" : "bottom-0",
)}
/>
<View className="absolute top-1/2 left-0 h-px w-3 bg-border" />
</View>
<View className="flex-1 flex-row items-center gap-2 py-1">
<Icon
icon={KIND_ICONS[step.kind]}
size={14}
color="accent-muted"
/>
<StepText step={step} />
</View>
</View>
);
})}
</View>
) : null}
</View>
Expand Down
8 changes: 7 additions & 1 deletion mobile/src/icons.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ export { default as IconAlertCircle } from "@tabler/icons-react-native/IconAlert
export { default as IconAlertTriangle } from "@tabler/icons-react-native/IconAlertTriangle";
export { default as IconArchive } from "@tabler/icons-react-native/IconArchive";
export { default as IconArrowLeft } from "@tabler/icons-react-native/IconArrowLeft";
export { default as IconArrowsExchange } from "@tabler/icons-react-native/IconArrowsExchange";
export { default as IconArrowUp } from "@tabler/icons-react-native/IconArrowUp";
export { default as IconArrowsExchange } from "@tabler/icons-react-native/IconArrowsExchange";
export { default as IconBell } from "@tabler/icons-react-native/IconBell";
export { default as IconBook } from "@tabler/icons-react-native/IconBook";
export { default as IconBrain } from "@tabler/icons-react-native/IconBrain";
Expand Down Expand Up @@ -37,6 +37,9 @@ export { default as IconEdit } from "@tabler/icons-react-native/IconEdit";
export { default as IconEye } from "@tabler/icons-react-native/IconEye";
export { default as IconEyeOff } from "@tabler/icons-react-native/IconEyeOff";
export { default as IconFile } from "@tabler/icons-react-native/IconFile";
export { default as IconFilePlus } from "@tabler/icons-react-native/IconFilePlus";
export { default as IconFileText } from "@tabler/icons-react-native/IconFileText";
export { default as IconFolder } from "@tabler/icons-react-native/IconFolder";
export { default as IconHistory } from "@tabler/icons-react-native/IconHistory";
export { default as IconInfoCircle } from "@tabler/icons-react-native/IconInfoCircle";
export { default as IconKey } from "@tabler/icons-react-native/IconKey";
Expand All @@ -63,7 +66,10 @@ export { default as IconSelector } from "@tabler/icons-react-native/IconSelector
export { default as IconSettings } from "@tabler/icons-react-native/IconSettings";
export { default as IconShieldLock } from "@tabler/icons-react-native/IconShieldLock";
export { default as IconSparkles } from "@tabler/icons-react-native/IconSparkles";
export { default as IconSubtask } from "@tabler/icons-react-native/IconSubtask";
export { default as IconTerminal2 } from "@tabler/icons-react-native/IconTerminal2";
export { default as IconTool } from "@tabler/icons-react-native/IconTool";
export { default as IconTrash } from "@tabler/icons-react-native/IconTrash";
export { default as IconUsersGroup } from "@tabler/icons-react-native/IconUsersGroup";
export { default as IconWorld } from "@tabler/icons-react-native/IconWorld";
export { default as IconX } from "@tabler/icons-react-native/IconX";
18 changes: 16 additions & 2 deletions mobile/src/live-run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ type LiveTool = {
answered: boolean;
/** What the call answered, for a question the person was asked. */
result?: string;
/** The message the answer came in: the id the conversation's messages read the answer back by. */
resultId?: string;
};

/** A step's id as a message's, so a row keeps its place from the step's start to its end. */
Expand Down Expand Up @@ -266,11 +268,13 @@ export function reduce(runs: LiveRuns, event: LiveEvent): LiveRuns {
return withTool(runs, event, (tool) => ({ ...tool, done: true }));
case "TOOL_CALL_RESULT": {
const content = event.content;
const resultId = messageOf(event);
return withTool(runs, event, (tool) => ({
...tool,
done: true,
answered: true,
...(typeof content === "string" ? { result: content } : {}),
...(resultId ? { resultId } : {}),
}));
}
default:
Expand Down Expand Up @@ -327,6 +331,15 @@ export function liveStep(
return message.status === "running" ? "running" : "over";
}

/** The tool call one of a live run's steps stands for, by its id; null for any other message. */
export function liveStepCall(
message: Pick<ConversationMessage, "id" | "role">,
): string | null {
return message.role === "tool" && message.id.startsWith(STEP)
? message.id.slice(STEP.length)
: null;
}

/** What the person answered the questions a live run asked, by tool call. */
export function liveAnswers(runs: LiveRuns): Map<string, Answer> {
const out = new Map<string, Answer>();
Expand Down Expand Up @@ -415,8 +428,9 @@ export function sentAgain(runs: LiveRuns): LiveRuns {
*
* Each step the Bot takes follows the message it is part of, as a `tool` message in the web app's words for it
* (./shared-copy/tool-name.js): `running` while it is going and said as what it is doing, then over and said as what
* it did ({@link liveStep}). The read messages hold a step by the server's words for it and nothing else, so as
* many of the steps that are over are left out as the read messages hold since the person last spoke. After a step that is over, with nothing written yet, the Bot
* it did ({@link liveStep}). The read messages hold a step as the tool's answer and nothing else, which
* ./run-steps.ts says in the same words as here, so as many of the steps that are over are left out as the read
* messages hold since the person last spoke. After a step that is over, with nothing written yet, the Bot
* is thinking again, which is said as one more step under way ({@link THINKING}), on the same row.
*/
export function asMessages(
Expand Down
Loading
Loading