Skip to content

armgit / alook

Public Imported from GitHub
0

Multi-Agent Collaboration for Team and Friends

refactor(web): extract chat-primitives/ presentation components (#303)

Gener58dac06bc4c5
diff --git a/src/web/src/components/agent-chat/message-list.tsx b/src/web/src/components/agent-chat/message-list.tsx
index e67527a4b..8537a5994 100644
--- a/src/web/src/components/agent-chat/message-list.tsx
+++ b/src/web/src/components/agent-chat/message-list.tsx
@@ -15,6 +15,7 @@ import { FileText, Flag, Copy, Check, MessageSquareQuote } from "lucide-react";
import { EmailCard } from "@/components/agent-chat/event-cards/email-card";
import { CalendarCard } from "@/components/agent-chat/event-cards/calendar-card";
import { IssueCard } from "@/components/agent-chat/event-cards/issue-card";
+import { MessageBubble, MessageCluster, AVATAR_SIZE, type BubblePosition } from "@/components/chat-primitives";
import { eventTypeFromMessage, type GroupPosition } from "@/components/agent-chat/chat-message-utils";
import { toast } from "sonner";
@@ -25,6 +26,10 @@ import { useLongPress } from "@/hooks/use-long-press";
const MENTION_ALLOWED_TAGS = { mention: ["data-agent-id"] };
const MENTION_LITERAL_TAGS = ["mention"];
+function toBubblePosition(gp: GroupPosition): BubblePosition {
+ return gp === "solo" ? "single" : gp;
+}
+
/**
* Whether an assistant message renders its own body (text bubble or, for a
* runtime-error message, its RuntimeErrorBlock) — independent of the live error
@@ -219,35 +224,10 @@ function PendingFileChips({
);
}
-// Bubble grouping (locked prototype v2): base radius 1.05rem; the corner FACING
-// the neighbor in the cluster tucks to 0.35rem. User bubbles tuck the RIGHT
-// edge; agent bubbles mirror to the LEFT edge.
-// first → tuck the bottom (corner toward the next bubble)
-// middle → tuck top + bottom
-// last → tuck the top (corner toward the previous bubble)
-const USER_BUBBLE_RADIUS: Record<GroupPosition, string> = {
- solo: "rounded-[1.05rem]",
- first: "rounded-[1.05rem] rounded-br-[0.35rem]",
- middle: "rounded-[1.05rem] rounded-tr-[0.35rem] rounded-br-[0.35rem]",
- last: "rounded-[1.05rem] rounded-tr-[0.35rem]",
-};
-
-const AGENT_BUBBLE_RADIUS: Record<GroupPosition, string> = {
- solo: "rounded-[1.05rem]",
- first: "rounded-[1.05rem] rounded-bl-[0.35rem]",
- middle: "rounded-[1.05rem] rounded-tl-[0.35rem] rounded-bl-[0.35rem]",
- last: "rounded-[1.05rem] rounded-tl-[0.35rem]",
-};
-// Slack/Discord cluster model (locked prototype): a TOP header [avatar] [name]
-// on the cluster's FIRST message (first/solo); bubbles/cards stack below it in
-// the gutter. Subsequent grouped messages get a spacer (no repeated
-// avatar/name). The avatar is TOP-aligned to the header — it anchors to the
-// name row, not a variable-height column, which dissolves the floating-avatar
-// bug. `forceSpacer` keeps the gutter width but never paints a header — for
-// items (e.g. an artifact card) that belong to the preceding message's cluster.
-const AVATAR_SIZE = 30; // matches the prototype's 30px gutter column
-const GUTTER_W = "w-[30px]";
+// Slack/Discord cluster model: delegates to <MessageCluster> from
+// chat-primitives. This wrapper adapts the product's AnimatedAvatar + the
+// `forceSpacer` concept into the primitive's props interface.
export function AgentRow({
groupPosition,
@@ -262,31 +242,24 @@ export function AgentRow({
forceSpacer?: boolean;
children: React.ReactNode;
}) {
- const isClusterHead =
- !forceSpacer && (groupPosition === "first" || groupPosition === "solo");
+ const effectivePosition: GroupPosition = forceSpacer ? "middle" : groupPosition;
return (
- <div className="flex justify-start items-start gap-2 min-w-0">
- <div className={cn(GUTTER_W, "shrink-0")} aria-hidden={!isClusterHead}>
- {isClusterHead && config && (
+ <MessageCluster
+ avatar={
+ config ? (
<AnimatedAvatar
config={config}
size={AVATAR_SIZE}
className="rounded-md"
isHovered={false}
/>
- )}
- </div>
- <div className="min-w-0 max-w-[86%] flex flex-col items-start gap-0.5">
- {isClusterHead && (
- // Top-aligned with the avatar (prototype v2): tight line-height + a
- // hair of top padding so the name's top edge lines up with the avatar.
- <span className="text-[0.85rem] font-semibold text-foreground leading-[1.15] pt-0.5 mb-1">
- {agentName}
- </span>
- )}
- {children}
- </div>
- </div>
+ ) : null
+ }
+ name={agentName}
+ position={effectivePosition}
+ >
+ {children}
+ </MessageCluster>
);
}
@@ -579,10 +552,11 @@ export const MessageItem = memo(function MessageItem({
</button>
);
})()}
- <div
+ <MessageBubble
+ variant="user"
+ position={toBubblePosition(groupPosition)}
className={cn(
- "max-w-[80%] px-3 py-1.5 bg-primary text-primary-foreground text-base relative",
- USER_BUBBLE_RADIUS[groupPosition],
+ "max-w-[80%] relative",
isSendFailed && "opacity-60",
)}
{...bubblePressHandlers}
@@ -605,7 +579,7 @@ export const MessageItem = memo(function MessageItem({
return <PendingFileChips pendingFiles={pendingFilesByMessage} messageId={msg.id} />;
})()}
{toolbar}
- </div>
+ </MessageBubble>
{actionSheet}
{isSendFailed && (
<button
@@ -697,15 +671,16 @@ export const MessageItem = memo(function MessageItem({
className="relative min-w-0 w-fit max-w-full"
{...bubblePressHandlers}
>
- <div className={cn(
- "markdown min-w-0 max-w-full px-3 py-1.5 bg-muted text-foreground text-base",
- AGENT_BUBBLE_RADIUS[groupPosition],
- // Persistent flagged rail on the bubble itself — glanceable
- // whether or not the toolbar/sheet is open (bubble-only).
- isFlagged && "shadow-[inset_2px_0_0_var(--foreground)]",
- )}>
+ <MessageBubble
+ variant="agent"
+ position={toBubblePosition(groupPosition)}
+ className={cn(
+ "markdown min-w-0 max-w-full",
+ isFlagged && "shadow-[inset_2px_0_0_var(--foreground)]",
+ )}
+ >
<Streamdown plugins={{ mermaid, cjk, math }} controls={{ code: { copy: true, download: false }, table: { copy: true, download: false, fullscreen: true } }} linkSafety={{ enabled: false }} allowedTags={MENTION_ALLOWED_TAGS} literalTagContent={MENTION_LITERAL_TAGS} components={mentionComponents}>{highlightMentions(msg.content, agents)}</Streamdown>
- </div>
+ </MessageBubble>
{isFlagged && <FlagDot />}
{toolbar}
{actionSheet}
diff --git a/src/web/src/components/chat-primitives/index.ts b/src/web/src/components/chat-primitives/index.ts
new file mode 100644
index 000000000..c9debb136
--- /dev/null
+++ b/src/web/src/components/chat-primitives/index.ts
@@ -0,0 +1,4 @@
+export { MessageBubble } from "./message-bubble";
+export type { BubblePosition } from "./message-bubble";
+
+export { MessageCluster, AVATAR_SIZE } from "./message-cluster";
diff --git a/src/web/src/components/chat-primitives/message-bubble.test.ts b/src/web/src/components/chat-primitives/message-bubble.test.ts
new file mode 100644
index 000000000..d16334a72
--- /dev/null
+++ b/src/web/src/components/chat-primitives/message-bubble.test.ts
@@ -0,0 +1,124 @@
+import { describe, it, expect } from "vitest";
+
+// Since we're in a node environment without jsdom, we test the logic/mapping
+// directly by importing the radius constants and verifying the component's
+// class composition logic.
+
+// Re-implement the mapping logic from the component to test in isolation.
+type BubbleVariant = "agent" | "user";
+type BubblePosition = "first" | "middle" | "last" | "single";
+
+const USER_RADIUS: Record<BubblePosition, string> = {
+ single: "rounded-[1.05rem]",
+ first: "rounded-[1.05rem] rounded-br-[0.35rem]",
+ middle: "rounded-[1.05rem] rounded-tr-[0.35rem] rounded-br-[0.35rem]",
+ last: "rounded-[1.05rem] rounded-tr-[0.35rem]",
+};
+
+const AGENT_RADIUS: Record<BubblePosition, string> = {
+ single: "rounded-[1.05rem]",
+ first: "rounded-[1.05rem] rounded-bl-[0.35rem]",
+ middle: "rounded-[1.05rem] rounded-tl-[0.35rem] rounded-bl-[0.35rem]",
+ last: "rounded-[1.05rem] rounded-tl-[0.35rem]",
+};
+
+function getRadius(variant: BubbleVariant, position: BubblePosition): string {
+ return variant === "user" ? USER_RADIUS[position] : AGENT_RADIUS[position];
+}
+
+function getColors(variant: BubbleVariant): string {
+ return variant === "user"
+ ? "bg-primary text-primary-foreground"
+ : "bg-muted text-foreground";
+}
+
+describe("MessageBubble radius logic", () => {
+ describe("user variant", () => {
+ it("single position has full rounding", () => {
+ expect(getRadius("user", "single")).toBe("rounded-[1.05rem]");
+ });
+
+ it("first position tucks bottom-right corner", () => {
+ expect(getRadius("user", "first")).toContain("rounded-br-[0.35rem]");
+ });
+
+ it("middle position tucks top-right and bottom-right corners", () => {
+ const radius = getRadius("user", "middle");
+ expect(radius).toContain("rounded-tr-[0.35rem]");
+ expect(radius).toContain("rounded-br-[0.35rem]");
+ });
+
+ it("last position tucks top-right corner only", () => {
+ const radius = getRadius("user", "last");
+ expect(radius).toContain("rounded-tr-[0.35rem]");
+ expect(radius).not.toContain("rounded-br-[0.35rem]");
+ });
+ });
+
+ describe("agent variant", () => {
+ it("single position has full rounding", () => {
+ expect(getRadius("agent", "single")).toBe("rounded-[1.05rem]");
+ });
+
+ it("first position tucks bottom-left corner", () => {
+ expect(getRadius("agent", "first")).toContain("rounded-bl-[0.35rem]");
+ });
+
+ it("middle position tucks top-left and bottom-left corners", () => {
+ const radius = getRadius("agent", "middle");
+ expect(radius).toContain("rounded-tl-[0.35rem]");
+ expect(radius).toContain("rounded-bl-[0.35rem]");
+ });
+
+ it("last position tucks top-left corner only", () => {
+ const radius = getRadius("agent", "last");
+ expect(radius).toContain("rounded-tl-[0.35rem]");
+ expect(radius).not.toContain("rounded-bl-[0.35rem]");
+ });
+ });
+
+ describe("color mapping", () => {
+ it("user variant uses primary colors", () => {
+ const colors = getColors("user");
+ expect(colors).toContain("bg-primary");
+ expect(colors).toContain("text-primary-foreground");
+ });
+
+ it("agent variant uses muted colors", () => {
+ const colors = getColors("agent");
+ expect(colors).toContain("bg-muted");
+ expect(colors).toContain("text-foreground");
+ });
+ });
+
+ describe("all positions are unique per variant", () => {
+ it("user has 4 distinct radius values", () => {
+ const positions: BubblePosition[] = ["single", "first", "middle", "last"];
+ const radii = positions.map((p) => getRadius("user", p));
+ expect(new Set(radii).size).toBe(4);
+ });
+
+ it("agent has 4 distinct radius values", () => {
+ const positions: BubblePosition[] = ["single", "first", "middle", "last"];
+ const radii = positions.map((p) => getRadius("agent", p));
+ expect(new Set(radii).size).toBe(4);
+ });
+ });
+
+ describe("symmetry — user tucks RIGHT, agent tucks LEFT", () => {
+ it("first: user=br, agent=bl", () => {
+ expect(getRadius("user", "first")).toContain("br");
+ expect(getRadius("agent", "first")).toContain("bl");
+ });
+
+ it("middle: user=tr+br, agent=tl+bl", () => {
+ expect(getRadius("user", "middle")).toMatch(/tr.*br/);
+ expect(getRadius("agent", "middle")).toMatch(/tl.*bl/);
+ });
+
+ it("last: user=tr, agent=tl", () => {
+ expect(getRadius("user", "last")).toContain("tr");
+ expect(getRadius("agent", "last")).toContain("tl");
+ });
+ });
+});
diff --git a/src/web/src/components/chat-primitives/message-bubble.tsx b/src/web/src/components/chat-primitives/message-bubble.tsx
new file mode 100644
index 000000000..c7ef3e319
--- /dev/null
+++ b/src/web/src/components/chat-primitives/message-bubble.tsx
@@ -0,0 +1,56 @@
+import type { ReactNode, HTMLAttributes } from "react";
+import { cn } from "@/lib/utils";
+
+type BubbleVariant = "agent" | "user";
+export type BubblePosition = "first" | "middle" | "last" | "single";
+
+interface MessageBubbleProps
+ extends Omit<HTMLAttributes<HTMLDivElement>, "children"> {
+ variant: BubbleVariant;
+ position: BubblePosition;
+ children: ReactNode;
+ className?: string;
+}
+
+const USER_RADIUS: Record<BubblePosition, string> = {
+ single: "rounded-[1.05rem]",
+ first: "rounded-[1.05rem] rounded-br-[0.35rem]",
+ middle: "rounded-[1.05rem] rounded-tr-[0.35rem] rounded-br-[0.35rem]",
+ last: "rounded-[1.05rem] rounded-tr-[0.35rem]",
+};
+
+const AGENT_RADIUS: Record<BubblePosition, string> = {
+ single: "rounded-[1.05rem]",
+ first: "rounded-[1.05rem] rounded-bl-[0.35rem]",
+ middle: "rounded-[1.05rem] rounded-tl-[0.35rem] rounded-bl-[0.35rem]",
+ last: "rounded-[1.05rem] rounded-tl-[0.35rem]",
+};
+
+export function MessageBubble({
+ variant,
+ position,
+ children,
+ className,
+ ...rest
+}: MessageBubbleProps) {
+ const radius =
+ variant === "user" ? USER_RADIUS[position] : AGENT_RADIUS[position];
+ const colors =
+ variant === "user"
+ ? "bg-primary text-primary-foreground"
+ : "bg-muted text-foreground";
+
+ return (
+ <div
+ className={cn(
+ "px-3 py-1.5 text-base",
+ radius,
+ colors,
+ className,
+ )}
+ {...rest}
+ >
+ {children}
+ </div>
+ );
+}
diff --git a/src/web/src/components/chat-primitives/message-cluster.test.ts b/src/web/src/components/chat-primitives/message-cluster.test.ts
new file mode 100644
index 000000000..064518f15
--- /dev/null
+++ b/src/web/src/components/chat-primitives/message-cluster.test.ts
@@ -0,0 +1,34 @@
+import { describe, it, expect } from "vitest";
+import type { ClusterPosition } from "./message-cluster";
+
+// Test the cluster logic: which positions show the avatar header vs spacer.
+// We test the decision logic that the component implements.
+
+function isClusterHead(position: ClusterPosition): boolean {
+ return position === "first" || position === "solo";
+}
+
+describe("MessageCluster position logic", () => {
+ it("'first' position shows avatar and name (cluster head)", () => {
+ expect(isClusterHead("first")).toBe(true);
+ });
+
+ it("'solo' position shows avatar and name (cluster head)", () => {
+ expect(isClusterHead("solo")).toBe(true);
+ });
+
+ it("'middle' position hides avatar and name", () => {
+ expect(isClusterHead("middle")).toBe(false);
+ });
+
+ it("'last' position hides avatar and name", () => {
+ expect(isClusterHead("last")).toBe(false);
+ });
+
+ describe("AVATAR_SIZE constant", () => {
+ it("exports a 30px avatar size matching the gutter column", async () => {
+ const { AVATAR_SIZE } = await import("./message-cluster");
+ expect(AVATAR_SIZE).toBe(30);
+ });
+ });
+});
diff --git a/src/web/src/components/chat-primitives/message-cluster.tsx b/src/web/src/components/chat-primitives/message-cluster.tsx
new file mode 100644
index 000000000..627b00056
--- /dev/null
+++ b/src/web/src/components/chat-primitives/message-cluster.tsx
@@ -0,0 +1,43 @@
+import type { ReactNode } from "react";
+import { cn } from "@/lib/utils";
+
+export type ClusterPosition = "first" | "middle" | "last" | "solo";
+
+export interface MessageClusterProps {
+ avatar: ReactNode;
+ name: string;
+ children: ReactNode;
+ position: ClusterPosition;
+ className?: string;
+}
+
+const AVATAR_SIZE = 30;
+const GUTTER_W = "w-[30px]";
+
+export function MessageCluster({
+ avatar,
+ name,
+ children,
+ position,
+ className,
+}: MessageClusterProps) {
+ const isClusterHead = position === "first" || position === "solo";
+
+ return (
+ <div className={cn("flex justify-start items-start gap-2 min-w-0", className)}>
+ <div className={cn(GUTTER_W, "shrink-0")} aria-hidden={!isClusterHead}>
+ {isClusterHead && avatar}
+ </div>
+ <div className="min-w-0 max-w-[86%] flex flex-col items-start gap-0.5">
+ {isClusterHead && (
+ <span className="text-[0.85rem] font-semibold text-foreground leading-[1.15] pt-0.5 mb-1">
+ {name}
+ </span>
+ )}
+ {children}
+ </div>
+ </div>
+ );
+}
+
+export { AVATAR_SIZE };