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
23 changes: 23 additions & 0 deletions apps/web/src/components/ChatView.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import { resolveBotPresence } from "./bots/botPresence";
import { resolveThreadLastVisitedAt, resolveThreadStatusPill } from "./Sidebar.logic";
import { useUiStateStore } from "../uiStateStore";
import { ChildThreadApprovals } from "./chat/ChildThreadApprovals";
import { workspaceAreaProps } from "../lib/workspaceFocus";
import { sourceControlClients } from "@t3tools/client-runtime/source-control-clients";
Expand Down Expand Up @@ -1816,6 +1819,25 @@ export default function ChatView(props: ChatViewProps) {
reportedModelSelection,
);
const activeStanding = activeThreadShell?.standing ?? null;
const activeLocalLastVisitedAt = useUiStateStore((state) =>
activeThreadKey === null ? undefined : state.threadLastVisitedAtById[activeThreadKey],
);
const activeBotAvatarState = (() => {
if (!activeStanding || activeThreadShell === null) return undefined;
const pill = resolveThreadStatusPill({
thread: {
...activeThreadShell,
lastVisitedAt: resolveThreadLastVisitedAt(
activeThreadShell.lastVisitedAt,
activeLocalLastVisitedAt,
),
},
});
return resolveBotPresence(
pill === null ? null : { ...pill, errorClass: activeThreadShell.runtime?.lastErrorClass },
activeStanding.role,
).avatarState;
})();
const botDetailsOpen = useRightPanelStore(
(state) =>
activeStanding !== null &&
Expand Down Expand Up @@ -3447,6 +3469,7 @@ export default function ChatView(props: ChatViewProps) {
: {})}
{...(threadFindEnabled ? { onFindInThread: openThreadFindStable } : {})}
standing={activeStanding}
botAvatarState={activeBotAvatarState}
activityCollapsed={
isStandingBot && activeThreadKey !== null
? { on: activityCollapsed, onToggle: toggleActivityCollapsed }
Expand Down
40 changes: 33 additions & 7 deletions apps/web/src/components/bots/BotAvatar.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,39 @@ describe("BotAvatar", () => {
);
});

it("blinks only while running, and only when motion is allowed", () => {
expect(
renderToStaticMarkup(<BotAvatar avatarId="halo" accent="sky" state="running" />),
).toContain("motion-safe:animate-bot-blink");
expect(renderToStaticMarkup(<BotAvatar avatarId="halo" accent="sky" />)).not.toContain(
"animate-bot-blink",
);
it("blinks only while idle or running, and only when motion is allowed", () => {
for (const state of ["idle", "running"] as const) {
expect(
renderToStaticMarkup(<BotAvatar avatarId="halo" accent="sky" state={state} />),
).toContain("motion-safe:animate-bot-blink");
}
for (const state of ["waiting", "needs-you", "failed"] as const) {
expect(
renderToStaticMarkup(<BotAvatar avatarId="halo" accent="sky" state={state} />),
).not.toContain("animate-bot-blink");
}
});

it("gives each state its own motion, and never animates under reduced motion", () => {
const markup = (state: "idle" | "running" | "waiting" | "needs-you" | "failed") =>
renderToStaticMarkup(<BotAvatar avatarId="visor" accent="amber" state={state} />);
expect(markup("idle")).toContain("motion-safe:animate-bot-breathe");
const running = markup("running");
expect(running).toContain("motion-safe:animate-bot-scan");
expect(running).toContain("motion-safe:animate-bot-antenna");
expect(running).toContain("motion-safe:animate-status-pulse");
expect(markup("waiting")).toContain("motion-safe:animate-bot-wait");
for (const state of ["idle", "running", "waiting"] as const) {
expect(markup(state)).not.toMatch(/(^|\s)animate-/);
}
});

it("marks an antenna on every avatar so the working pulse has something to move", () => {
for (const avatarId of THREAD_STANDING_AVATAR_IDS) {
expect(
renderToStaticMarkup(<BotAvatar avatarId={avatarId} accent="teal" state="running" />),
).toContain("data-bot-antenna");
}
});

it("draws every avatar and falls back for unknown ids", () => {
Expand Down
50 changes: 30 additions & 20 deletions apps/web/src/components/bots/BotAvatar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ const AVATARS: Record<
head: (
<>
{FACE}
<path d="M12.8 2.5 11 5.5h2l-1.8 2.5" />
<path data-bot-antenna d="M12.8 2.5 11 5.5h2l-1.8 2.5" />
<path d="M9 17h6" />
</>
),
Expand All @@ -40,8 +40,8 @@ const AVATARS: Record<
head: (
<>
{FACE}
<path d="M8.5 5.5a3.5 3.5 0 0 0 7 0Z" />
<path d="M12 5.5V8" />
<path data-bot-antenna d="M8.5 5.5a3.5 3.5 0 0 0 7 0Z" />
<path data-bot-antenna d="M12 5.5V8" />
<path d="M10 17.2q2 1 4 0" />
</>
),
Expand All @@ -56,8 +56,8 @@ const AVATARS: Record<
head: (
<>
{FACE}
<path d="M12 8V5.5" />
<circle cx="12" cy="4.5" r="1" />
<path data-bot-antenna d="M12 8V5.5" />
<circle data-bot-antenna cx="12" cy="4.5" r="1" />
<path d="M10.5 17.5h3" />
</>
),
Expand All @@ -69,7 +69,7 @@ const AVATARS: Record<
head: (
<>
{FACE}
<path d="M8 8 6.5 5M16 8l1.5-3" />
<path data-bot-antenna d="M8 8 6.5 5M16 8l1.5-3" />
<path d="M9.5 17.5h5" />
</>
),
Expand All @@ -84,8 +84,8 @@ const AVATARS: Record<
head: (
<>
{FACE}
<path d="M12 8V4h3" />
<rect x="15" y="2.8" width="2.4" height="2.4" rx="0.6" />
<path data-bot-antenna d="M12 8V4h3" />
<rect data-bot-antenna x="15" y="2.8" width="2.4" height="2.4" rx="0.6" />
<path d="M9 17h6" />
</>
),
Expand All @@ -100,9 +100,9 @@ const AVATARS: Record<
head: (
<>
{FACE}
<path d="M9 8V5M15 8V5" />
<circle cx="9" cy="4" r="1" fill="currentColor" stroke="none" />
<circle cx="15" cy="4" r="1" fill="currentColor" stroke="none" />
<path data-bot-antenna d="M9 8V5M15 8V5" />
<circle data-bot-antenna cx="9" cy="4" r="1" fill="currentColor" stroke="none" />
<circle data-bot-antenna cx="15" cy="4" r="1" fill="currentColor" stroke="none" />
<path d="M9.5 17q2.5 1.5 5 0" />
</>
),
Expand All @@ -117,9 +117,9 @@ const AVATARS: Record<
head: (
<>
{FACE}
<path d="M12 8V5.5" />
<path d="M12 5.5q-3-.2-3.5-3 3 .2 3.5 3Z" />
<path d="M12 5.5q2.5-.4 3.2-2.6-2.6.2-3.2 2.6Z" />
<path data-bot-antenna d="M12 8V5.5" />
<path data-bot-antenna d="M12 5.5q-3-.2-3.5-3 3 .2 3.5 3Z" />
<path data-bot-antenna d="M12 5.5q2.5-.4 3.2-2.6-2.6.2-3.2 2.6Z" />
<path d="M10 17.2h4" />
</>
),
Expand All @@ -134,7 +134,7 @@ const AVATARS: Record<
head: (
<>
{FACE}
<ellipse cx="12" cy="4.5" rx="4.5" ry="1.4" />
<ellipse data-bot-antenna cx="12" cy="4.5" rx="4.5" ry="1.4" />
<path d="M10 17.5q2 .8 4 0" />
</>
),
Expand All @@ -149,7 +149,7 @@ const AVATARS: Record<
<>
{FACE}
<path d="M4 12H2.5M20 12h1.5" />
<path d="M12 8V6" />
<path data-bot-antenna d="M12 8V6" />
<path d="M8.5 16.5h7M8.5 18h7" />
</>
),
Expand All @@ -164,7 +164,7 @@ const AVATARS: Record<
head: (
<>
{FACE}
<path d="M7 8 9 5.5h6L17 8" />
<path data-bot-antenna d="M7 8 9 5.5h6L17 8" />
<path d="M17.6 14.8 19 19" />
<path d="M9.5 17.5h4" />
</>
Expand Down Expand Up @@ -226,7 +226,7 @@ export const BOT_ACCENT_CLASSES: Record<
},
};

export type BotAvatarState = "idle" | "running" | "needs-you" | "failed";
export type BotAvatarState = "idle" | "running" | "waiting" | "needs-you" | "failed";

export function BotAvatar(props: {
readonly avatarId: string;
Expand All @@ -246,6 +246,9 @@ export function BotAvatar(props: {
"inline-flex shrink-0 items-center justify-center rounded-full bg-current/8",
props.size === "lg" ? "size-9" : props.size === "md" ? "size-7" : "size-5",
accent.text,
state === "idle" && "motion-safe:animate-bot-breathe",
state === "running" && "motion-safe:animate-status-pulse",
state === "waiting" && "motion-safe:animate-bot-wait",
state === "needs-you" && cn("ring-2", accent.ring),
state === "failed" && "ring-2 ring-destructive/80",
props.className,
Expand All @@ -260,11 +263,18 @@ export function BotAvatar(props: {
strokeLinejoin="round"
className={props.size === "lg" ? "size-7" : props.size === "md" ? "size-5.5" : "size-4"}
>
{avatar.head}
<g className={cn(state === "running" && "**:data-bot-antenna:motion-safe:animate-bot-antenna")}>
{avatar.head}
</g>
{state === "running" ? (
<g className="motion-safe:animate-bot-scan">
<rect x="6.5" y="11" width="2.5" height="4" fill="currentColor" stroke="none" opacity="0.28" />
</g>
) : null}
<g
className={cn(
"origin-[12px_13px]",
state === "running" && "motion-safe:animate-bot-blink",
(state === "idle" || state === "running") && "motion-safe:animate-bot-blink",
)}
>
{avatar.eyes}
Expand Down
18 changes: 18 additions & 0 deletions apps/web/src/components/bots/botPresence.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,24 @@ describe("resolveBotPresence", () => {
});
});

it("holds a calm waiting state for waiting and usage-limited bots", () => {
expect(resolveBotPresence({ label: "Waiting" }, "Ops")).toEqual({
avatarState: "waiting",
line: "Waiting",
badge: null,
});
expect(resolveBotPresence({ label: "Failed", errorClass: "usage_limit" }, "Ops")).toEqual({
avatarState: "waiting",
line: "Limited",
badge: null,
});
expect(
resolveBotPresence({ label: "Failed", errorClass: "provider_error" }, "Ops").avatarState,
).toBe(
"failed",
);
});

it("falls back to the role line when idle", () => {
expect(resolveBotPresence(null, "Daily digest")).toEqual({
avatarState: "idle",
Expand Down
14 changes: 12 additions & 2 deletions apps/web/src/components/bots/botPresence.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import type { OrchestrationV2ProviderFailureClass } from "@t3tools/contracts";

import type { ThreadStatusPill } from "../Sidebar.logic";
import type { BotAvatarState } from "./BotAvatar";

Expand All @@ -11,20 +13,28 @@ export interface BotPresence {

const NEEDS_YOU = new Set(["Pending Approval", "Awaiting Input", "Guest Request", "Plan Ready"]);

/** The pill plus the failure class, which the pill label does not carry. */
export type BotPresenceInput = Pick<ThreadStatusPill, "label"> & {
readonly errorClass?: OrchestrationV2ProviderFailureClass | null | undefined;
};

/** Maps the thread's status pill onto a bot's quieter vocabulary. */
export function resolveBotPresence(
pill: Pick<ThreadStatusPill, "label"> | null,
pill: BotPresenceInput | null,
role: string | null | undefined,
): BotPresence {
const label = pill?.label ?? null;
if (label !== null && NEEDS_YOU.has(label))
return { avatarState: "needs-you", line: label, badge: "needs-you" };
if (label === "Working" || label === "Connecting")
return { avatarState: "running", line: label, badge: null };
// A usage limit is the bot waiting, not a failure: a calm glow, no failure ring.
if (label === "Failed" && pill?.errorClass === "usage_limit")
return { avatarState: "waiting", line: "Limited", badge: null };
if (label === "Failed") return { avatarState: "failed", line: "Failed", badge: null };
if (label === "Completed")
return { avatarState: "idle", line: role ?? "New reply", badge: "unread" };
if (label === "Waiting") return { avatarState: "idle", line: "Waiting", badge: null };
if (label === "Waiting") return { avatarState: "waiting", line: "Waiting", badge: null };
return { avatarState: "idle", line: role ?? null, badge: null };
}

Expand Down
12 changes: 10 additions & 2 deletions apps/web/src/components/chat/ChatHeader.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ import { cn } from "~/lib/utils";
import { textDirection } from "~/textDirection";
import { useClientSettings } from "../../hooks/useSettings";
import { GuestShareIndicator } from "./GuestShareControls";
import { BotAvatar } from "../bots/BotAvatar";
import { BotAvatar, type BotAvatarState } from "../bots/BotAvatar";

interface ChatHeaderProps {
activeThreadEnvironmentId: EnvironmentId;
Expand All @@ -61,6 +61,8 @@ interface ChatHeaderProps {
onFindInThread?: ((threadRef: ScopedThreadRef) => void) | undefined;
/** A standing bot shows who it is instead of the project/thread breadcrumb. */
standing?: ThreadStanding | null | undefined;
/** Drives the bot avatar's motion in the header; idle when absent. */
botAvatarState?: BotAvatarState | undefined;
/**
* Collapse agent, automation and system activity, or null when the thread has
* no toggle. A standing thread passes it.
Expand Down Expand Up @@ -92,6 +94,7 @@ export const ChatHeader = memo(function ChatHeader({
onOpenProjectSettings,
onFindInThread,
standing,
botAvatarState,
activityCollapsed = null,
}: ChatHeaderProps) {
const activeProjectName = activeProject?.isScratch ? "No project" : activeProject?.title;
Expand Down Expand Up @@ -266,7 +269,12 @@ export const ChatHeader = memo(function ChatHeader({
data-testid="bot-thread-header"
onContextMenu={handleHeaderContextMenu}
>
<BotAvatar avatarId={standing.avatarId} accent={standing.accent} size="md" />
<BotAvatar
avatarId={standing.avatarId}
accent={standing.accent}
size="md"
state={botAvatarState ?? "idle"}
/>
<button
type="button"
aria-label={`Bot actions for ${standing.name}`}
Expand Down
11 changes: 9 additions & 2 deletions apps/web/src/components/sidebar/SidebarBotsGroup.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,9 @@ const SidebarBotRow = memo(function SidebarBotRow(
const localLastVisitedAt = useUiStateStore((state) => state.threadLastVisitedAtById[threadKey]);
const lastVisitedAt = resolveThreadLastVisitedAt(thread.lastVisitedAt, localLastVisitedAt);
const isSelected = useThreadSelectionStore((state) => state.selectedThreadKeys.has(threadKey));
const pill = resolveThreadStatusPill({ thread: { ...thread, lastVisitedAt } });
const presence = resolveBotPresence(
resolveThreadStatusPill({ thread: { ...thread, lastVisitedAt } }),
pill === null ? null : { ...pill, errorClass: thread.runtime?.lastErrorClass },
standing.role,
);
const accent = BOT_ACCENT_CLASSES[standing.accent];
Expand Down Expand Up @@ -101,6 +102,12 @@ const SidebarBotRow = memo(function SidebarBotRow(
}`}
className={cn(
"group/sidebar-row relative flex h-8 w-full cursor-pointer items-center gap-2 overflow-hidden rounded-md px-2 text-left outline-none select-none focus-visible:ring-2 focus-visible:ring-inset focus-visible:ring-ring",
presence.avatarState === "running" &&
cn(
"before:absolute before:inset-y-1 before:left-0 before:w-0.5 before:rounded-full before:content-[''] motion-safe:before:animate-status-pulse",
accent.text,
"before:bg-current",
),
props.isActive
? "bg-sidebar-row-active text-sidebar-foreground light:ring-1 light:ring-inset light:ring-sidebar-border/70"
: isSelected
Expand Down Expand Up @@ -132,7 +139,7 @@ const SidebarBotRow = memo(function SidebarBotRow(
"min-w-0 flex-1 truncate text-xs",
presence.avatarState === "failed"
? "text-destructive-foreground"
: presence.badge === "needs-you"
: presence.badge === "needs-you" || presence.avatarState === "running"
? accent.text
: "text-sidebar-muted-foreground",
)}
Expand Down
Loading
Loading