Skip to content

🪢 Open source agent evals & observability: Trace, evaluate, and improve LLM applications with one open platform.

feat(in-app-agent): Polish Langfuse Assistant interface (#15844)

Lysander Kieselcda12d0d8b83
diff --git a/web/src/features/in-app-agent/components/ControlledInAppAgentWindow.tsx b/web/src/features/in-app-agent/components/ControlledInAppAgentWindow.tsx
index 744d188b09..3c446195b4 100644
--- a/web/src/features/in-app-agent/components/ControlledInAppAgentWindow.tsx
+++ b/web/src/features/in-app-agent/components/ControlledInAppAgentWindow.tsx
@@ -21,6 +21,7 @@ import {
type BackgroundExecutionRunView,
} from "@/src/features/in-app-agent/lib/backgroundExecutionSession";
import { InAppAgentRunStatus } from "@langfuse/shared";
+import { isUnsettledInAppAgentRunStatus } from "@langfuse/shared/in-app-agent";
const SANDBOX_CONVERSATION_WRITE_LOCK_MESSAGE =
"Sandbox-enabled conversations become read-only after 8 hours. Start a new conversation to continue.";
@@ -131,6 +132,26 @@ export function ControlledInAppAgentWindow(
isSubmitting ||
pendingToolApprovals.length > 0 ||
displayedPendingToolApprovals.length > 0;
+ // Settle from the durable run, not from attach/animation. A finished
+ // attached conversation can still be `isRunning` while the watch connects.
+ // A hydrated in-flight run may have no `execution.run` yet — then `isRunning`
+ // is the only signal that the turn is still open.
+ const isRunUnsettled =
+ isSubmitting ||
+ pendingToolApprovals.length > 0 ||
+ displayedPendingToolApprovals.length > 0 ||
+ (execution.run
+ ? isUnsettledInAppAgentRunStatus(execution.run.status)
+ : isRunning);
+ // Both halves are needed. The status alone stays AWAITING_APPROVAL after the
+ // user decides, until the watch reports the resumed run, which would leave the
+ // drawer claiming to wait on someone who already answered. Pending approvals
+ // alone are not enough either: an always-allowed tool carries one and keeps
+ // executing.
+ const isAwaitingApproval =
+ execution.run?.status === InAppAgentRunStatus.AWAITING_APPROVAL &&
+ (pendingToolApprovals.length > 0 ||
+ displayedPendingToolApprovals.length > 0);
const displayError = selectedConversationIsWriteLocked
? ({
type: "generic",
@@ -177,6 +198,8 @@ export function ControlledInAppAgentWindow(
<InAppAgentWindow
error={displayError}
isAssistantTurnInProgress={isAssistantTurnInProgress}
+ isRunUnsettled={isRunUnsettled}
+ isAwaitingApproval={isAwaitingApproval}
isHeaderDragHandleEnabled={props.isHeaderDragHandleEnabled}
isExpanded={props.isExpanded}
isConversationInteractionDisabled={isConversationInteractionDisabled}
diff --git a/web/src/features/in-app-agent/components/InAppAgentExecution.clienttest.tsx b/web/src/features/in-app-agent/components/InAppAgentExecution.clienttest.tsx
index 73f8494c14..b8910a2272 100644
--- a/web/src/features/in-app-agent/components/InAppAgentExecution.clienttest.tsx
+++ b/web/src/features/in-app-agent/components/InAppAgentExecution.clienttest.tsx
@@ -404,6 +404,11 @@ describe("in-app agent execution", () => {
renderExecutionUi();
expect(await screen.findByText("Create the prompt")).toBeInTheDocument();
+ // The run is parked on this approval, so the drawer says so rather than
+ // narrating the tool it was about to call.
+ fireEvent.click(
+ screen.getByRole("button", { name: "Waiting for your approval…" }),
+ );
expect(screen.getByText("I need approval.")).toBeInTheDocument();
fireEvent.click(
screen.getByRole("button", {
@@ -592,7 +597,9 @@ describe("in-app agent execution", () => {
renderExecutionUi();
- expect(await screen.findByText("Calling 1 tool")).toBeInTheDocument();
+ expect(
+ await screen.findByRole("button", { name: "Browsing traces" }),
+ ).toBeInTheDocument();
});
it("keeps approval cancellation visibly stopping until hydration settles", async () => {
@@ -648,7 +655,7 @@ describe("in-app agent execution", () => {
).toHaveBeenCalledOnce();
});
expect(screen.getByRole("button", { name: "Stopping run" })).toBeDisabled();
- expect(screen.getByRole("status")).toHaveTextContent("Stopping the run…");
+ expect(screen.getByText("Stopping the run…")).toBeVisible();
});
it("does not observe a cached active run while the assistant is closed", async () => {
@@ -853,9 +860,7 @@ describe("in-app agent execution", () => {
).not.toBeInTheDocument();
});
expect(screen.getByText(finalText)).toBeVisible();
- expect(
- screen.getByText("The assistant is aware of this trace view."),
- ).toBeVisible();
+ expect(screen.getByText("Current trace view in context")).toBeVisible();
expect(screen.getByRole("button", { name: "Good response" })).toBeVisible();
});
diff --git a/web/src/features/in-app-agent/components/InAppAgentMessage.stories.tsx b/web/src/features/in-app-agent/components/InAppAgentMessage.stories.tsx
index b6d6e6920b..1488557b00 100644
--- a/web/src/features/in-app-agent/components/InAppAgentMessage.stories.tsx
+++ b/web/src/features/in-app-agent/components/InAppAgentMessage.stories.tsx
@@ -6,6 +6,8 @@ const meta = preview.meta({
component: InAppAgentMessage,
});
+const storyTimestamp = new Date("2026-08-06T11:49:13.000Z").getTime();
+
function copySelectedNode(node: Node, document: Document) {
const range = document.createRange();
range.selectNodeContents(node);
@@ -32,6 +34,7 @@ export const AssistantText = meta.story({
type: "text",
text: "Langfuse tracks traces, observations, scores, and metadata so teams can debug LLM applications.",
},
+ timestamp: storyTimestamp,
},
});
@@ -179,6 +182,73 @@ export const AssistantMarkdown = meta.story({
},
});
+export const VariantMatrix = meta.story({
+ args: {
+ role: "assistant",
+ content: { type: "text", text: "Assistant response" },
+ },
+ render: () => (
+ <div className="grid max-w-3xl grid-cols-[6rem_1fr] gap-x-4 gap-y-6 p-6">
+ <p className="text-muted-foreground text-xs">User · default</p>
+ <InAppAgentMessage
+ role="user"
+ content={{
+ type: "text",
+ text: "Compare today's traces with yesterday.",
+ }}
+ timestamp={storyTimestamp}
+ />
+ <p className="text-muted-foreground text-xs">User · compact</p>
+ <InAppAgentMessage
+ role="user"
+ isCompact
+ content={{ type: "text", text: "Only show production errors." }}
+ timestamp={storyTimestamp}
+ />
+ <p className="text-muted-foreground text-xs">Assistant · settled</p>
+ <InAppAgentMessage
+ role="assistant"
+ content={{
+ type: "text",
+ text: "The error rate fell by **0.6 percentage points** while trace volume increased.",
+ }}
+ timestamp={storyTimestamp}
+ onSubmitFeedback={fn()}
+ />
+ <p className="text-muted-foreground text-xs">Assistant · streaming</p>
+ <InAppAgentMessage
+ role="assistant"
+ content={{
+ type: "text",
+ text: "I’m comparing the slowest observations against their scores…",
+ isStreaming: true,
+ }}
+ timestamp={storyTimestamp}
+ />
+ <p className="text-muted-foreground text-xs">Assistant · long</p>
+ <InAppAgentMessage
+ role="assistant"
+ content={{
+ type: "text",
+ text: [
+ "The main regression is concentrated in retrieval:",
+ "",
+ "| Step | p95 |",
+ "| --- | ---: |",
+ "| Vector search | 480 ms |",
+ "| Reranking | 3.91 s |",
+ "",
+ "```ts",
+ "const bottleneck = 'document-reranking';",
+ "```",
+ ].join("\n"),
+ }}
+ timestamp={storyTimestamp}
+ />
+ </div>
+ ),
+});
+
export const CopyMarkdownSelectionInteraction = meta.story({
name: "(Test) Copy Markdown Selection",
args: {
@@ -433,12 +503,11 @@ export const RedirectActionWithParams = meta.story({
},
});
-export const LoadingToolCallGroup = meta.story({
+export const RunningToolCallGroup = meta.story({
args: {
role: "assistant",
content: {
type: "toolGroup",
- isLoading: true,
tools: [
{
type: "tool",
@@ -567,3 +636,50 @@ export const FeedbackPopoverInteraction = meta.story({
});
},
});
+
+export const CopyMessageInteraction = meta.story({
+ name: "(Test) Copy Whole Message",
+ args: {
+ role: "assistant",
+ content: {
+ type: "text",
+ text: "The **error rate** fell by `0.6 pp`.",
+ },
+ timestamp: storyTimestamp,
+ },
+ play: async ({ canvasElement }) => {
+ const canvas = within(canvasElement);
+ await userEvent.hover(canvasElement);
+ await userEvent.click(
+ await canvas.findByRole("button", { name: "Copy message" }),
+ );
+
+ await waitFor(() =>
+ expect(
+ canvas.getByRole("button", { name: "Message copied" }),
+ ).toBeVisible(),
+ );
+ },
+});
+
+export const UserTextWithoutActionsInteraction = meta.story({
+ name: "(Test) User Text Without Actions",
+ args: {
+ role: "user",
+ content: {
+ type: "text",
+ text: "How do I find failed traces from the last 24 hours?",
+ },
+ timestamp: storyTimestamp,
+ },
+ play: async ({ canvasElement }) => {
+ const canvas = within(canvasElement);
+
+ expect(
+ canvas.queryByRole("button", { name: "Copy message" }),
+ ).not.toBeInTheDocument();
+ expect(
+ canvas.queryByTestId("in-app-agent-message-actions"),
+ ).not.toBeInTheDocument();
+ },
+});
diff --git a/web/src/features/in-app-agent/components/InAppAgentMessage.tsx b/web/src/features/in-app-agent/components/InAppAgentMessage.tsx
index 9081df6c51..cc8ef87c47 100644
--- a/web/src/features/in-app-agent/components/InAppAgentMessage.tsx
+++ b/web/src/features/in-app-agent/components/InAppAgentMessage.tsx
@@ -38,7 +38,6 @@ import {
PopoverContent,
PopoverTrigger,
} from "@/src/components/ui/popover";
-import { useElementSize } from "@/src/hooks/useElementSize";
import { useCopyToClipboard } from "@/src/hooks/useCopyToClipboard";
import { useWatchedPromiseCallback } from "@/src/hooks/useWatchedPromiseCallback";
import useProjectIdFromURL from "@/src/hooks/useProjectIdFromURL";
@@ -70,6 +69,7 @@ export type InAppAgentMessageContent =
type: "text";
text: string;
feedback?: InAppAgentMessageFeedback;
+ isStreaming?: boolean;
redirectAction?: InAppAgentRedirectActionContent;
sources?: InAppAgentMessageSource[];
}
@@ -77,7 +77,6 @@ export type InAppAgentMessageContent =
| {
type: "toolGroup";
tools: InAppAgentToolCallContent[];
- isLoading?: boolean;
};
const parseAbsoluteUrl = (href: string): URL | null => {
@@ -153,6 +152,10 @@ export type InAppAgentMessageProps = {
content: InAppAgentMessageContent;
isCompact?: boolean;
isFeedbackDisabled?: boolean;
+ /** False for intermediate blocks rendered inside the activity drawer, which
+ * withholds the whole trailing row: copy, feedback, sources and timestamp. */
+ isFinalAnswer?: boolean;
+ timestamp?: number;
onSubmitFeedback?: (params: {
value: InAppAgentMessageFeedbackValue | null;
comment?: string | null;
@@ -164,6 +167,8 @@ export function InAppAgentMessage({
content,
isCompact = false,
isFeedbackDisabled = false,
+ isFinalAnswer = true,
+ timestamp,
onSubmitFeedback,
}: InAppAgentMessageProps) {
if (content.type === "redirectAction") {
@@ -171,26 +176,28 @@ export function InAppAgentMessage({
}
if (content.type === "toolGroup") {
- return (
- <ToolCallGroup
- tools={content.tools}
- isLoading={content.isLoading}
- isCompact={isCompact}
- />
- );
+ return <ToolCallGroup tools={content.tools} isCompact={isCompact} />;
}
if (content.type === "reasoning") {
return <InAppAgentReasoningBlock content={content} isCompact={isCompact} />;
}
- if (content.type === "text" && role === "assistant") {
+ if (content.type === "text") {
+ if (role === "user") {
+ return (
+ <MessageCard role={role} content={content} isCompact={isCompact} />
+ );
+ }
+
return (
- <AssistantMessageWithFeedback
+ <TextMessageWithActions
content={content}
isCompact={isCompact}
isFeedbackDisabled={isFeedbackDisabled}
onSubmitFeedback={onSubmitFeedback}
+ isFinalAnswer={isFinalAnswer}
+ timestamp={timestamp}
/>
);
}
@@ -215,13 +222,14 @@ const MessageCard = forwardRef<
<div
ref={ref}
className={cn(
- "max-w-full overflow-hidden wrap-break-word shadow-xs",
- isCompact
- ? "rounded-xl px-2.5 py-1 text-[0.775rem]"
- : "rounded-2xl px-3 py-1.5 text-sm",
+ "max-w-full overflow-hidden wrap-break-word",
+ isCompact ? "text-[0.775rem]" : "text-sm",
isUser
- ? "bg-primary text-primary-foreground"
- : "bg-card text-foreground border-border border",
+ ? cn(
+ "bg-muted text-foreground rounded-2xl",
+ isCompact ? "px-2.5 py-1" : "px-3 py-1.5",
+ )
+ : "text-foreground",
)}
>
{content.type === "loading" ? (
@@ -243,64 +251,146 @@ const MessageCard = forwardRef<
);
});
-function AssistantMessageWithFeedback({
+function TextMessageWithActions({
content,
isCompact,
isFeedbackDisabled,
onSubmitFeedback,
+ isFinalAnswer,
+ timestamp,
}: {
content: Extract<InAppAgentMessageContent, { type: "text" }>;
isCompact: boolean;
isFeedbackDisabled: boolean;
+ isFinalAnswer: boolean;
+ timestamp?: number;
onSubmitFeedback?: (params: {
value: InAppAgentMessageFeedbackValue | null;
comment?: string | null;
}) => Promise<void>;
}) {
- const [messageCardRef, messageCardSize] = useElementSize<HTMLDivElement>();
+ const messageCardRef = useRef<HTMLDivElement>(null);
+ const { copyRich, isCopied } = useCopyToClipboard({
+ successDuration: 1_500,
+ });
const sources = content.sources ?? [];
const hasSources = sources.length > 0;
- const hasActions = Boolean(onSubmitFeedback || hasSources);
+ const isSettled = !content.isStreaming;
+ const canSubmitFeedback = isSettled && onSubmitFeedback;
+ const hasActions = isSettled && isFinalAnswer;
+ const formattedTimestamp = timestamp
+ ? formatMessageTimestamp(timestamp)
+ : null;
+
+ const handleCopy = () => {
+ const renderedContent = messageCardRef.current?.querySelector(
+ "[data-in-app-agent-message-content]",
+ );
+ const htmlContainer = renderedContent?.cloneNode(true) as
+ | HTMLElement
+ | undefined;
+ htmlContainer
+ ?.querySelectorAll("[data-in-app-agent-code-copy-button]")
+ .forEach((node) => {
+ node.remove();
+ });
+
+ copyRich({
+ text: content.text,
+ html: htmlContainer?.innerHTML ?? content.text,
+ }).catch(() => undefined);
+ };
+
+ const actions = (
+ <>
+ <button
+ type="button"
+ className="text-muted-foreground hover:text-foreground focus-visible:ring-ring rounded-md p-1 outline-none focus-visible:ring-2"
+ aria-label={isCopied ? "Message copied" : "Copy message"}
+ title={isCopied ? "Copied" : "Copy message"}
+ onClick={handleCopy}
+ >
+ {isCopied ? (
+ <Check className={cn(isCompact ? "size-3" : "size-3.5")} />
+ ) : (
+ <Copy className={cn(isCompact ? "size-3" : "size-3.5")} />
+ )}
+ </button>
+ {canSubmitFeedback ? (
+ <MessageFeedbackControls
+ feedback={content.feedback}
+ isCompact={isCompact}
+ isFeedbackDisabled={isFeedbackDisabled}
+ onSubmitFeedback={canSubmitFeedback}
+ />
+ ) : null}
+ {hasSources ? (
+ <SourcesPopover sources={sources} isCompact={isCompact} />
+ ) : null}
+ </>
+ );
return (
- <div className="flex max-w-full flex-col items-start">
+ <div className="group/message flex max-w-full flex-col items-start">
<MessageCard
ref={messageCardRef}
role="assistant"
- content={content}
+ content={
+ // The turn's redirect belongs to the answer. Leaving it on the block
+ // that happened to carry it would offer the same action twice once
+ // the activity drawer is open.
+ isFinalAnswer ? content : { ...content, redirectAction: undefined }
+ }
isCompact={isCompact}
/>
{hasActions ? (
<div
- style={
- messageCardSize?.width
- ? { width: messageCardSize.width, maxWidth: "100%" }
- : undefined
- }
+ data-testid="in-app-agent-message-actions"
className={cn(
- "flex max-w-full min-w-50 flex-col items-start overflow-hidden",
- isCompact ? "mt-1.5" : "mt-2",
+ "flex min-h-6 max-w-full items-center gap-0.5",
+ isCompact ? "mt-0.5" : "mt-1",
)}
>
- <div className="flex w-full min-w-0 items-center gap-1">
- {onSubmitFeedback ? (
- <MessageFeedbackControls
- feedback={content.feedback}
- isCompact={isCompact}
- isFeedbackDisabled={isFeedbackDisabled}
- onSubmitFeedback={onSubmitFeedback}
- />
- ) : null}
- {hasSources ? (
- <SourcesPopover sources={sources} isCompact={isCompact} />
- ) : null}
- </div>
+ <div className="flex items-center gap-0.5">{actions}</div>
+ {formattedTimestamp ? (
+ <time
+ dateTime={formattedTimestamp.iso}
+ title={formattedTimestamp.full}
+ aria-label={`Sent ${formattedTimestamp.full}`}
+ suppressHydrationWarning
+ className="text-muted-foreground ml-1 text-[0.6875rem] opacity-0 transition-opacity group-focus-within/message:opacity-100 group-hover/message:opacity-100"
+ >
+ {formattedTimestamp.short}
+ </time>
+ ) : null}
</div>
) : null}
</div>
);
}
+function formatMessageTimestamp(timestamp: number) {
+ const date = new Date(timestamp);
+ const today = new Date();
+ const isToday =
+ date.getFullYear() === today.getFullYear() &&
+ date.getMonth() === today.getMonth() &&
+ date.getDate() === today.getDate();
+
+ return {
+ iso: date.toISOString(),
+ short: new Intl.DateTimeFormat(undefined, {
+ ...(isToday ? {} : { year: "numeric", month: "short", day: "numeric" }),
+ hour: "numeric",
+ minute: "2-digit",
+ }).format(date),
+ full: new Intl.DateTimeFormat(undefined, {
+ dateStyle: "full",
+ timeStyle: "medium",
+ }).format(date),
+ };
+}
+
function InAppAgentReasoningBlock({
content,
isCompact,
@@ -665,51 +755,26 @@ function RedirectActionButton({
function ToolCallGroup({
tools,
- isLoading = false,
isCompact = false,
}: {
tools: InAppAgentToolCallContent[];
- isLoading?: boolean;
isCompact?: boolean;
}) {
- const label = `${isLoading ? "Calling" : "Called"} ${tools.length} ${
- tools.length === 1 ? "tool" : "tools"
- }`;
- const [userToggled, setUserToggled] = useState<boolean | null>(null);
- const isOpen = userToggled ?? isLoading;
-
return (
- <details
- open={isOpen}
- onToggle={(event) => {
- if (event.currentTarget.open !== isOpen) {
- setUserToggled(event.currentTarget.open);
- }
- }}
+ <div
className={cn(
- "text-muted-foreground max-w-full",
+ "text-muted-foreground flex max-w-full flex-col gap-0",
isCompact ? "text-[0.775rem]" : "text-sm",
)}
>
- <summary className="hover:text-foreground focus-visible:ring-ring flex w-fit cursor-pointer list-none items-center gap-1.5 rounded-md px-1 py-0.5 text-xs leading-4 font-bold outline-none focus-visible:ring-2 focus-visible:ring-offset-2 [&::-webkit-details-marker]:hidden">
- <span className={cn(isLoading && styles.thinkingShimmer)}>{label}</span>
- <ChevronDown
- className={cn(
- "size-3.5 shrink-0 transition-transform",
- !isOpen && "-rotate-90",
- )}
+ {tools.map((tool, index) => (
+ <ToolCallDisclosure
+ key={`${tool.name}-${index}`}
+ tool={tool}
+ isCompact={isCompact}
/>
- </summary>
- <div className="mt-0.5 flex flex-col gap-0">
- {tools.map((tool, index) => (
- <ToolCallDisclosure
- key={`${tool.name}-${index}`}
- tool={tool}
- isCompact={isCompact}
- />
- ))}
- </div>
- </details>
+ ))}
+ </div>
);
}
@@ -721,12 +786,12 @@ function ToolCallDisclosure({
isCompact: boolean;
}) {
const status = tool.status;
- const displayName = getInAppAgentToolDisplayName(tool.name);
+ const toolName = getInAppAgentToolDisplayName(tool.name);
return (
<details className="group/tool min-w-0">
<summary
- aria-label={`${displayName}: ${status}`}
+ aria-label={`${toolName}: ${status}`}
className={cn(
"hover:text-foreground focus-visible:ring-ring flex cursor-pointer list-none items-center gap-1.5 rounded-md px-1 py-0.5 text-xs leading-4 font-bold outline-none focus-visible:ring-2 focus-visible:ring-offset-2 [&::-webkit-details-marker]:hidden",
isCompact && "px-0.5",
@@ -739,9 +804,9 @@ function ToolCallDisclosure({
status === "failed" && "text-destructive",
status === "denied" && "text-dark-yellow",
)}
- title={displayName}
+ title={toolName}
>
- {displayName}
+ {toolName}
</span>
</summary>
<div className={cn("mt-1.5 mb-1 ml-3 px-3", isCompact && "px-2.5")}>
@@ -790,6 +855,7 @@ function MessageText({
if (role === "user") {
return (
<p
+ data-in-app-agent-message-content
className={cn(
"whitespace-pre-wrap",
isCompact ? "leading-4" : "leading-4.5",
@@ -802,6 +868,7 @@ function MessageText({
return (
<div
+ data-in-app-agent-message-content
data-compact={isCompact}
onCopy={(event) => {
const browserSelection =
diff --git a/web/src/features/in-app-agent/components/InAppAgentWindow.clienttest.tsx b/web/src/features/in-app-agent/components/InAppAgentWindow.clienttest.tsx
index 4ea6015a4e..877e967d61 100644
--- a/web/src/features/in-app-agent/components/InAppAgentWindow.clienttest.tsx
+++ b/web/src/features/in-app-agent/components/InAppAgentWindow.clienttest.tsx
@@ -1,4 +1,10 @@
-import { fireEvent, render, screen, waitFor } from "@testing-library/react";
+import {
+ fireEvent,
+ render,
+ screen,
+ waitFor,
+ within,
+} from "@testing-library/react";
import { ScanSearch } from "lucide-react";
import { InAppAgentRunStatus } from "@langfuse/shared";
import { TooltipProvider } from "@/src/components/ui/tooltip";
@@ -100,7 +106,7 @@ function windowElement(
onSubmitFeedback: vi.fn(),
quickActionContext: "observability",
quickActionResetKey: "/project/project-1/traces",
- screenContextDescription: { type: "trace-list", hasAppliedFilters: true },
+ screenContextDescription: { type: "trace-list" },
selectedConversationId: undefined,
...overrides,
showCloseButton: false,
@@ -378,3 +384,543 @@ describe("ControlledInAppAgentWindow stop", () => {
expect(finishAnimation).toHaveBeenCalledOnce();
});
});
+
+describe("InAppAgentWindow activity", () => {
+ it("shows a waiting drawer, then Working once the first activity arrives", () => {
+ const userMessage = {
+ id: "user-1",
+ role: "user" as const,
+ content: { type: "text" as const, text: "Summarize recent errors." },
+ };
+ const { rerender } = render(
+ windowElement({
+ isAssistantTurnInProgress: true,
+ messages: [
+ userMessage,
+ {
+ id: "connecting",
+ role: "assistant",
+ content: { type: "loading", label: "Connecting..." },
+ },
+ ],
+ }),
+ );
+
+ const waitingTrigger = screen.getByRole("button", {
+ name: "There for you in a second…",
+ });
+ expect(waitingTrigger).toBeVisible();
+ expect(waitingTrigger).toHaveAttribute("aria-expanded", "false");
+ expect(screen.queryByText("Connecting...")).not.toBeInTheDocument();
+ expect(screen.queryByText("Thinking...")).not.toBeInTheDocument();
+ expect(
+ screen.queryByRole("status", { name: "Working…" }),
+ ).not.toBeInTheDocument();
+
+ fireEvent.click(waitingTrigger);
+ expect(waitingTrigger).toHaveAttribute("aria-expanded", "true");
+
+ const reasoning = {
+ id: "assistant-reasoning",
+ timestamp: new Date("2026-08-06T15:26:26.000Z").getTime(),
+ role: "assistant" as const,
+ content: {
+ type: "reasoning" as const,
+ text: "I will inspect the recent errors.",
+ isStreaming: false,
+ },
+ };
+
+ rerender(
+ windowElement({
+ isAssistantTurnInProgress: true,
+ messages: [userMessage, reasoning],
+ }),
+ );
+
+ const workingTrigger = screen.getByRole("button", { name: "Working…" });
+ expect(workingTrigger).toBeVisible();
+ expect(
+ screen.queryByRole("button", { name: "There for you in a second…" }),
+ ).not.toBeInTheDocument();
+ // The drawer the user opened while waiting is the same drawer now working.
+ expect(workingTrigger).toHaveAttribute("aria-expanded", "true");
+
+ rerender(
+ windowElement({
+ messages: [
+ userMessage,
+ reasoning,
+ {
+ id: "assistant-answer",
+ runId: "run-1",
+ timestamp: new Date("2026-08-06T15:27:17.000Z").getTime(),
+ role: "assistant",
+ content: { type: "text", text: "Two ingestion retries failed." },
+ },
+ ],
+ }),
+ );
+
+ // Settling must not tear the drawer down and collapse it under the user.
+ expect(
+ screen.getByRole("button", { name: "Worked for 51s" }),
+ ).toHaveAttribute("aria-expanded", "true");
+ });
+
+ it("labels a completed turn with compact minutes and leftover seconds", () => {
+ render(
+ windowElement({
+ messages: [
+ {
+ id: "user-1",
+ role: "user",
+ content: { type: "text", text: "Investigate latency" },
+ },
+ {
+ id: "assistant-reasoning",
+ timestamp: new Date("2026-08-06T15:26:26.000Z").getTime(),
+ role: "assistant",
+ content: {
+ type: "reasoning",
+ text: "Checking the slow traces.",
+ isStreaming: false,
+ },
+ },
+ {
+ id: "assistant-conclusion",
+ runId: "run-1",
+ timestamp: new Date("2026-08-06T15:29:55.000Z").getTime(),
+ role: "assistant",
+ content: { type: "text", text: "The reranker is the bottleneck." },
+ },
+ ],
+ }),
+ );
+
+ expect(
+ screen.getByRole("button", { name: "Worked for 3m 29s" }),
+ ).toBeVisible();
+ });
+
+ it("keeps the activity drawer collapsed and shows technical names when opened", () => {
+ render(
+ windowElement({
+ isAssistantTurnInProgress: true,
+ messages: [
+ {
+ id: "user-1",
+ role: "user",
+ content: { type: "text", text: "Why did latency increase?" },
+ },
+ {
+ id: "assistant-reasoning",
+ role: "assistant",
+ content: {
+ type: "reasoning",
+ text: "The slowest traces share a reranking step.",
+ isStreaming: false,
+ },
+ },
+ {
+ id: "assistant-tool-1",
+ role: "assistant",
+ content: {
+ type: "toolGroup",
+ tools: [
+ {
+ type: "tool",
+ name: "langfuse_getObservation",
+ status: "succeeded",
+ args: "{}",
+ },
+ ],
+ },
+ },
+ {
+ id: "assistant-reasoning-2",
+ role: "assistant",
+ content: {
+ type: "reasoning",
+ text: "The reranking step is slow across both outlier traces.",
+ isStreaming: false,
+ },
+ },
+ {
+ id: "assistant-tool-2",
+ role: "assistant",
+ content: {
+ type: "toolGroup",
+ tools: [
+ {
+ type: "tool",
+ name: "langfuse_listObservations",
+ status: "running",
+ args: "{}",
+ },
+ ],
+ },
+ },
+ ],
+ }),
+ );
+
+ const headline = screen.getByRole("button", {
+ name: "Looking at observations",
+ });
+ expect(headline).toBeVisible();
+ expect(headline).toHaveAttribute("aria-expanded", "false");
+ expect(screen.queryByText("Thought")).not.toBeInTheDocument();
+ expect(
+ screen.queryByText(
+ "The reranking step is slow across both outlier traces.",
+ ),
+ ).not.toBeInTheDocument();
+
+ fireEvent.click(headline);
+ expect(headline).toHaveAttribute("aria-expanded", "true");
+ expect(screen.getAllByText("Thought")).toHaveLength(2);
+ expect(screen.getByLabelText("getObservation: succeeded")).toBeVisible();
+ expect(screen.getByLabelText("listObservations: running")).toBeVisible();
+ expect(screen.queryByText("Calling 1 tool")).not.toBeInTheDocument();
+ });
+});
+
+describe("InAppAgentWindow composer", () => {
+ it("uses Reply after an assistant answer and the welcome copy on a fresh conversation", () => {
+ const { rerender } = render(windowElement());
+
+ expect(
+ screen.getByRole("textbox", { name: "Message the assistant" }),
+ ).toHaveAttribute("placeholder", "Let me know what I can do for you...");
+
+ rerender(
+ windowElement({
+ messages: [
+ {
+ id: "user-1",
+ role: "user",
+ content: { type: "text", text: "What failed overnight?" },
+ },
+ {
+ id: "assistant-1",
+ role: "assistant",
+ content: {
+ type: "text",
+ text: "Ingestion errors spiked after 2am.",
+ },
+ },
+ ],
+ }),
+ );
+
+ expect(
+ screen.getByRole("textbox", { name: "Message the assistant" }),
+ ).toHaveAttribute("placeholder", "Reply...");
+ });
+
+ it("does not settle mid-turn assistant text while the run is still working", () => {
+ render(
+ windowElement({
+ isAssistantTurnInProgress: true,
+ messages: [
+ {
+ id: "user-1",
+ role: "user",
+ content: { type: "text", text: "Cluster the failed traces." },
+ },
+ {
+ id: "assistant-tool",
+ role: "assistant",
+ content: {
+ type: "toolGroup",
+ tools: [
+ {
+ type: "tool",
+ name: "langfuse_listObservations",
+ status: "succeeded",
+ args: "{}",
+ },
+ ],
+ },
+ },
+ {
+ id: "assistant-mid-turn",
+ role: "assistant",
+ content: {
+ type: "text",
+ text: "Let me confirm by inspecting a couple of payloads.",
+ },
+ },
+ ],
+ }),
+ );
+
+ expect(
+ screen.queryByText("Let me confirm by inspecting a couple of payloads."),
+ ).not.toBeInTheDocument();
+ expect(
+ screen.queryByRole("button", { name: "Copy message" }),
+ ).not.toBeInTheDocument();
+ expect(
+ screen.queryByRole("button", { name: "Good response" }),
+ ).not.toBeInTheDocument();
+ expect(
+ screen.getByRole("textbox", { name: "Message the assistant" }),
+ ).toHaveAttribute("placeholder", "Let me know what I can do for you...");
+
+ fireEvent.click(
+ screen.getByRole("button", { name: "Browsing observations" }),
+ );
+ expect(
+ screen.getByText("Let me confirm by inspecting a couple of payloads."),
+ ).toBeVisible();
+ expect(
+ screen.queryByRole("button", { name: "Copy message" }),
+ ).not.toBeInTheDocument();
+ });
+
+ it("keeps a finished answer visible while the transcript is still revealing", () => {
+ render(
+ windowElement({
+ isAssistantTurnInProgress: true,
+ isRunUnsettled: false,
+ messages: [
+ {
+ id: "user-1",
+ role: "user",
+ content: { type: "text", text: "What changed?" },
+ },
+ {
+ id: "assistant-tool",
+ role: "assistant",
+ content: {
+ type: "toolGroup",
+ tools: [
+ {
+ type: "tool",
+ name: "langfuse_listObservations",
+ status: "succeeded",
+ args: "{}",
+ result: "{}",
+ },
+ ],
+ },
+ },
+ {
+ id: "assistant-answer",
+ role: "assistant",
+ content: {
+ type: "text",
+ text: "Observation volume stayed flat.",
+ },
+ },
+ ],
+ }),
+ );
+
+ expect(screen.getByText("Observation volume stayed flat.")).toBeVisible();
+ expect(
+ screen.getByRole("button", { name: "Copy message" }),
+ ).toBeInTheDocument();
+ expect(
+ screen.getByRole("textbox", { name: "Message the assistant" }),
+ ).toHaveAttribute("placeholder", "Reply...");
+ });
+});
+
+describe("InAppAgentWindow message actions", () => {
+ it("shows actions only for the final answer and copies that text block", async () => {
+ const writeText = vi.fn().mockResolvedValue(undefined);
+ Object.defineProperty(navigator, "clipboard", {
+ configurable: true,
+ value: { writeText },
+ });
+ vi.stubGlobal("ClipboardItem", undefined);
+
+ const activityMessages = [
+ {
+ id: "user-1",
+ role: "user" as const,
+ content: { type: "text" as const, text: "Investigate latency" },
+ },
+ {
+ id: "assistant-intro",
+ timestamp: new Date("2026-08-06T15:26:26.000Z").getTime(),
+ role: "assistant" as const,
+ content: {
+ type: "text" as const,
+ text: "I will inspect the slow traces.",
+ },
+ },
+ {
+ id: "assistant-reasoning",
+ role: "assistant" as const,
+ content: {
+ type: "reasoning" as const,
+ text: "This private reasoning is not part of the answer.",
+ isStreaming: false,
+ },
+ },
+ {
+ id: "assistant-tool",
+ role: "assistant" as const,
+ content: { type: "toolGroup" as const, tools: [] },
+ },
+ ];
+ const finalAnswer = {
+ id: "assistant-conclusion",
+ runId: "run-1",
+ timestamp: new Date("2026-08-06T15:27:17.204Z").getTime(),
+ role: "assistant" as const,
+ content: {
+ type: "text" as const,
+ text: "The reranker is the bottleneck.",
+ },
+ };
+
+ const { rerender } = render(
+ windowElement({
+ isAssistantTurnInProgress: true,
+ messages: activityMessages,
+ }),
+ );
+
+ const workingTrigger = screen.getByRole("button", { name: "Working…" });
+ expect(workingTrigger).toHaveAttribute("aria-expanded", "false");
+ expect(
+ screen.queryByText("I will inspect the slow traces."),
+ ).not.toBeInTheDocument();
+ expect(screen.getByText("Current trace view in context")).toBeVisible();
+ expect(
+ screen.queryByRole("button", { name: "Copy message" }),
+ ).not.toBeInTheDocument();
+
+ rerender(windowElement({ messages: activityMessages.concat(finalAnswer) }));
+
+ const activityTrigger = screen.getByRole("button", {
+ name: "Worked for 51s",
+ });
+ expect(activityTrigger).toHaveAttribute("aria-expanded", "false");
+ expect(
+ screen.queryByText("I will inspect the slow traces."),
+ ).not.toBeInTheDocument();
+ expect(screen.getByText("The reranker is the bottleneck.")).toBeVisible();
+
+ fireEvent.click(activityTrigger);
+ expect(activityTrigger).toHaveAttribute("aria-expanded", "true");
+ expect(screen.getByText("I will inspect the slow traces.")).toBeVisible();
+
+ const actionRows = screen.getAllByTestId("in-app-agent-message-actions");
+ expect(actionRows).toHaveLength(1);
+ expect(
+ screen.getAllByRole("button", { name: "Copy message" }),
+ ).toHaveLength(1);
+ expect(
+ within(actionRows[0]).getByRole("button", { name: "Good response" }),
+ ).toBeInTheDocument();
+ expect(
+ within(actionRows[0]).getByRole("button", { name: "Bad response" }),
+ ).toBeInTheDocument();
+ expect(actionRows[0].querySelector("time")).toHaveClass("opacity-0");
+
+ fireEvent.click(
+ within(actionRows[0]).getByRole("button", {
+ name: "Copy message",
+ }),
+ );
+
+ await waitFor(() => {
+ expect(writeText).toHaveBeenCalledWith("The reranker is the bottleneck.");
+ });
+ });
+
+ it("joins later answer texts into one visible reply after Working", async () => {
+ const writeText = vi.fn().mockResolvedValue(undefined);
+ Object.defineProperty(navigator, "clipboard", {
+ configurable: true,
+ value: { writeText },
+ });
+ vi.stubGlobal("ClipboardItem", undefined);
+
+ const analysis = "The traces are synthetic seed data, not real traffic.";
+ const closer = "I've prepared a link to the error-level traces.";
+
+ render(
+ windowElement({
+ messages: [
+ {
+ id: "user-1",
+ role: "user" as const,
+ content: {
+ type: "text" as const,
+ text: "What is going on with errors?",
+ },
+ },
+ {
+ id: "assistant-reasoning",
+ timestamp: new Date("2026-08-06T15:26:26.000Z").getTime(),
+ role: "assistant" as const,
+ content: {
+ type: "reasoning" as const,
+ text: "I should inspect the error traces first.",
+ isStreaming: false,
+ },
+ },
+ {
+ id: "assistant-tool",
+ role: "assistant" as const,
+ content: { type: "toolGroup" as const, tools: [] },
+ },
+ {
+ id: "assistant-analysis",
+ timestamp: new Date("2026-08-06T15:27:20.000Z").getTime(),
+ role: "assistant" as const,
+ content: {
+ type: "text" as const,
+ text: analysis,
+ redirectAction: {
+ type: "redirectAction" as const,
+ label: "Open error traces",
+ href: "/project/project-1/traces?level=ERROR",
+ },
+ },
+ },
+ {
+ id: "assistant-closer",
+ runId: "run-1",
+ timestamp: new Date("2026-08-06T15:27:48.000Z").getTime(),
+ role: "assistant" as const,
+ content: {
+ type: "text" as const,
+ text: closer,
+ },
+ },
+ ],
+ }),
+ );
+
+ const activityTrigger = screen.getByRole("button", {
+ name: /Worked for/,
+ });
+ expect(activityTrigger).toHaveAttribute("aria-expanded", "false");
+ expect(screen.getByText(analysis)).toBeVisible();
+ expect(screen.getByText(closer)).toBeVisible();
+ expect(
+ screen.getByRole("button", { name: "Open error traces" }),
+ ).toBeVisible();
+ expect(
+ screen.queryByText("I should inspect the error traces first."),
+ ).not.toBeInTheDocument();
+
+ fireEvent.click(activityTrigger);
+ expect(screen.getByText("Thought")).toBeVisible();
+ expect(screen.getByText(analysis)).toBeVisible();
+
+ fireEvent.click(screen.getByRole("button", { name: "Copy message" }));
+
+ await waitFor(() => {
+ expect(writeText).toHaveBeenCalledWith(`${analysis}\n\n${closer}`);
+ });
+ });
+});
diff --git a/web/src/features/in-app-agent/components/InAppAgentWindow.stories.tsx b/web/src/features/in-app-agent/components/InAppAgentWindow.stories.tsx
index fd8e06fba6..1d4a7ffe26 100644
--- a/web/src/features/in-app-agent/components/InAppAgentWindow.stories.tsx
+++ b/web/src/features/in-app-agent/components/InAppAgentWindow.stories.tsx
@@ -69,6 +69,7 @@ function StatefulInAppAgentWindow(args: InAppAgentWindowProps) {
const streamingSeedMessages: InAppAgentWindowMessage[] = [
{
id: "seed-user-1",
+ timestamp: new Date("2026-08-06T11:48:43.000Z").getTime(),
role: "user",
content: {
type: "text",
@@ -77,6 +78,7 @@ const streamingSeedMessages: InAppAgentWindowMessage[] = [
},
{
id: "seed-assistant-1",
+ timestamp: new Date("2026-08-06T11:49:13.000Z").getTime(),
role: "assistant",
content: {
type: "text",
@@ -705,6 +707,7 @@ const meta = preview.meta({
export const ToolApprovalRequired = meta.story({
args: {
isAssistantTurnInProgress: true,
+ isAwaitingApproval: true,
selectedConversationId: "conversation-1",
messages: [
{
@@ -739,6 +742,15 @@ export const ToolApprovalRequired = meta.story({
},
],
},
+ play: async ({ canvasElement }: { canvasElement: HTMLElement }) => {
+ const canvas = within(canvasElement);
+
+ // The run has stopped on the approval below, so the drawer must say so
+ // rather than narrate the tool it was about to call.
+ await expect(
+ canvas.getByRole("button", { name: "Waiting for your approval…" }),
+ ).toBeVisible();
+ },
});
export const Empty = meta.story({
@@ -919,32 +931,369 @@ export const Conversation = meta.story({
},
});
-export const Streaming = meta.story({
+export const GroupedAssistantTurn = meta.story({
+ args: {
+ isExpanded: true,
+ selectedConversationId: "conversation-1",
+ screenContextDescription: { type: "trace-list" },
+ messages: [
+ {
+ id: "grouped-user",
+ role: "user",
+ content: { type: "text", text: "Why did latency increase?" },
+ },
+ {
+ id: "grouped-intro",
+ timestamp: new Date("2026-08-06T15:26:26.000Z").getTime(),
+ role: "assistant",
+ content: {
+ type: "text",
+ text: "I will compare the slow traces with their observations.",
+ },
+ },
+ {
+ id: "grouped-reasoning",
+ role: "assistant",
+ content: {
+ type: "reasoning",
+ text: "The slowest traces share a reranking step.",
+ isStreaming: false,
+ },
+ },
+ {
+ id: "grouped-tool",
+ role: "assistant",
+ content: {
+ type: "toolGroup",
+ tools: [
+ {
+ type: "tool",
+ name: "langfuse_getObservations",
+ status: "succeeded",
+ args: JSON.stringify({ orderBy: "latency.desc", limit: 10 }),
+ result: JSON.stringify({ bottleneck: "document-reranking" }),
+ },
+ ],
+ },
+ },
+ {
+ id: "grouped-reasoning-2",
+ role: "assistant",
+ content: {
+ type: "reasoning",
+ text: "The reranking step is slow across both outlier traces.",
+ isStreaming: false,
+ },
+ },
+ {
+ id: "grouped-tool-2",
+ role: "assistant",
+ content: {
+ type: "toolGroup",
+ tools: [
+ {
+ type: "tool",
+ name: "langfuse_getTraces",
+ status: "succeeded",
+ args: JSON.stringify({ ids: ["trace-104", "trace-219"] }),
+ result: JSON.stringify({ count: 2 }),
+ },
+ {
+ type: "tool",
+ name: "langfuse_queryMetrics",
+ status: "succeeded",
+ args: JSON.stringify({ metric: "latency", aggregation: "p95" }),
+ result: JSON.stringify({ value: 5.82 }),
+ },
+ ],
+ },
+ },
+ {
+ id: "grouped-conclusion",
+ runId: "grouped-run",
+ timestamp: new Date("2026-08-06T15:27:17.000Z").getTime(),
+ role: "assistant",
+ content: {
+ type: "text",
+ text: "The latency increase comes from document reranking. Vector search remains stable.",
+ },
+ },
+ ],
+ },
+});
+
+const progressLogSeedMessages: InAppAgentWindowMessage[] = [
+ {
+ id: "progress-user",
+ role: "user",
+ content: { type: "text", text: "What changed in yesterday's dashboards?" },
+ },
+ {
+ id: "progress-reasoning",
+ timestamp: new Date("2026-08-06T15:26:26.000Z").getTime(),
+ role: "assistant",
+ content: {
+ type: "reasoning",
+ text: "I should read the dashboards first, then list recent observations.",
+ isStreaming: false,
+ },
+ },
+ {
+ id: "progress-tools-start",
+ role: "assistant",
+ content: {
+ type: "toolGroup",
+ tools: [
+ {
+ type: "tool",
+ name: "skill",
+ status: "succeeded",
+ args: JSON.stringify({ name: "error-analysis" }),
+ result: JSON.stringify({ ok: true }),
+ },
+ {
+ type: "tool",
+ name: "langfuseDocs_search",
+ status: "succeeded",
+ args: JSON.stringify({ query: "dashboards" }),
+ result: JSON.stringify({ hits: 2 }),
+ },
+ {
+ type: "tool",
+ name: "langfuse_getDashboard",
+ status: "succeeded",
+ args: JSON.stringify({ dashboardId: "dash-1" }),
+ result: JSON.stringify({ name: "Latency" }),
+ },
+ ],
+ },
+ },
+ {
+ id: "progress-reasoning-2",
+ role: "assistant",
+ content: {
+ type: "reasoning",
+ text: "The dashboard spike lines up with a reranking change. Checking observation volume next.",
+ isStreaming: false,
+ },
+ },
+ {
+ id: "progress-tool-latest",
+ role: "assistant",
+ content: {
+ type: "toolGroup",
+ tools: [
+ {
+ type: "tool",
+ name: "langfuse_getObservation",
+ status: "succeeded",
+ args: JSON.stringify({ observationId: "obs-1" }),
+ result: JSON.stringify({ name: "rerank" }),
+ },
+ {
+ type: "tool",
+ name: "langfuse_listObservations",
+ status: "running",
+ args: JSON.stringify({ limit: 20 }),
+ },
+ ],
+ },
+ },
+];
+
+export const ProgressLogWorking = meta.story({
args: {
isAssistantTurnInProgress: true,
+ isExpanded: true,
selectedConversationId: "conversation-1",
- messages: streamingSeedMessages,
+ messages: progressLogSeedMessages,
+ },
+});
+
+export const ProgressLogOpened = meta.story({
+ args: {
+ isAssistantTurnInProgress: true,
+ isExpanded: true,
+ selectedConversationId: "conversation-1",
+ messages: progressLogSeedMessages,
+ },
+ play: async ({ canvasElement }: { canvasElement: HTMLElement }) => {
+ const canvas = within(canvasElement);
+ await userEvent.click(
+ canvas.getByRole("button", { name: "Looking at observations" }),
+ );
+ await expect(canvas.getAllByText("Thought")).toHaveLength(2);
+ await expect(canvas.getByLabelText("skill: succeeded")).toBeVisible();
+ await expect(canvas.getByLabelText("search: succeeded")).toBeVisible();
+ await expect(
+ canvas.getByLabelText("getDashboard: succeeded"),
+ ).toBeVisible();
+ await expect(
+ canvas.getByLabelText("getObservation: succeeded"),
+ ).toBeVisible();
+ await expect(
+ canvas.getByLabelText("listObservations: running"),
+ ).toBeVisible();
},
- render: (args) => <StreamingInAppAgentWindow {...args} />,
});
-export const LoadingResponse = meta.story({
+export const ProgressLogMidTurnText = meta.story({
args: {
isAssistantTurnInProgress: true,
+ isExpanded: true,
+ selectedConversationId: "conversation-1",
messages: [
+ ...progressLogSeedMessages,
{
- id: "user-1",
+ id: "progress-mid-turn-text",
+ role: "assistant",
+ content: {
+ type: "text",
+ text: "The dashboard spike looks like reranking. Checking a few observation payloads next.",
+ },
+ },
+ ],
+ },
+});
+
+export const ProgressLogCompleted = meta.story({
+ args: {
+ isExpanded: true,
+ selectedConversationId: "conversation-1",
+ messages: [
+ ...progressLogSeedMessages.slice(0, -1),
+ {
+ id: "progress-tool-latest",
+ role: "assistant",
+ content: {
+ type: "toolGroup",
+ tools: [
+ {
+ type: "tool",
+ name: "langfuse_listObservations",
+ status: "succeeded",
+ args: JSON.stringify({ limit: 20 }),
+ result: JSON.stringify({ count: 20 }),
+ },
+ ],
+ },
+ },
+ {
+ id: "progress-answer",
+ runId: "progress-run",
+ timestamp: new Date("2026-08-06T15:27:17.000Z").getTime(),
+ role: "assistant",
+ content: {
+ type: "text",
+ text: "Yesterday's latency dashboard picked up a reranking spike. Observation volume stayed flat.",
+ },
+ },
+ ],
+ },
+});
+
+export const MultiBlockAnswer = meta.story({
+ args: {
+ isExpanded: true,
+ selectedConversationId: "conversation-1",
+ screenContextDescription: { type: "trace-list" },
+ messages: [
+ {
+ id: "multiblock-user",
role: "user",
+ content: { type: "text", text: "What is going on with errors?" },
+ },
+ {
+ id: "multiblock-reasoning",
+ timestamp: new Date("2026-08-06T15:26:26.000Z").getTime(),
+ role: "assistant",
+ content: {
+ type: "reasoning",
+ text: "I should inspect the error traces first.",
+ isStreaming: false,
+ },
+ },
+ {
+ id: "multiblock-tool",
+ role: "assistant",
+ content: {
+ type: "toolGroup",
+ tools: [
+ {
+ type: "tool",
+ name: "langfuse_getTraces",
+ status: "succeeded",
+ args: JSON.stringify({ level: "ERROR", limit: 20 }),
+ result: JSON.stringify({ count: 12, synthetic: true }),
+ },
+ ],
+ },
+ },
+ {
+ id: "multiblock-analysis",
+ timestamp: new Date("2026-08-06T15:27:20.000Z").getTime(),
+ role: "assistant",
content: {
type: "text",
- text: "Summarize recent ingestion errors.",
+ text: "The traces are synthetic seed data, not real traffic. Error volume is concentrated on the ingestion path.",
+ redirectAction: {
+ type: "redirectAction",
+ label: "Open error traces",
+ href: "/project/project-1/traces?level=ERROR",
+ },
},
},
{
- id: "assistant-1",
+ id: "multiblock-closer",
+ runId: "multiblock-run",
+ timestamp: new Date("2026-08-06T15:27:48.000Z").getTime(),
role: "assistant",
content: {
- type: "loading",
+ type: "text",
+ text: "I've prepared a link to the error-level traces.",
+ },
+ },
+ ],
+ },
+});
+
+export const LightConversation = meta.story({
+ globals: { theme: "light" },
+ args: {
+ isExpanded: true,
+ selectedConversationId: "conversation-1",
+ messages: streamingSeedMessages,
+ },
+});
+
+export const DarkConversation = meta.story({
+ globals: { theme: "dark" },
+ args: {
+ isExpanded: true,
+ selectedConversationId: "conversation-1",
+ messages: streamingSeedMessages,
+ },
+});
+
+export const Streaming = meta.story({
+ args: {
+ isAssistantTurnInProgress: true,
+ selectedConversationId: "conversation-1",
+ messages: streamingSeedMessages,
+ },
+ render: (args) => <StreamingInAppAgentWindow {...args} />,
+});
+
+export const Working = meta.story({
+ args: {
+ isAssistantTurnInProgress: true,
+ messages: [
+ {
+ id: "user-1",
+ role: "user",
+ content: {
+ type: "text",
+ text: "Summarize recent ingestion errors.",
},
},
],
@@ -1036,31 +1385,6 @@ export const LoadingAfterToolCall = meta.story({
},
});
-export const Connecting = meta.story({
- args: {
- isAssistantTurnInProgress: true,
- isConversationInteractionDisabled: true,
- messages: [
- {
- id: "user-1",
- role: "user",
- content: {
- type: "text",
- text: "Summarize recent ingestion errors.",
- },
- },
- {
- id: "connecting",
- role: "assistant",
- content: {
- type: "loading",
- label: "Connecting...",
- },
- },
- ],
- },
-});
-
export const Error = meta.story({
args: {
error: {
@@ -1169,12 +1493,17 @@ export const BackgroundHint = meta.story({
"Compare cost against last week",
);
await userEvent.click(canvas.getByRole("button", { name: "Send message" }));
- await expect(await canvas.findByRole("status")).toHaveTextContent(
- "I keep running in the background",
- );
+ await waitFor(() => {
+ expect(
+ canvas.getByText(/I keep running in the background/),
+ ).toBeVisible();
+ });
await waitFor(
- () => expect(canvas.queryByRole("status")).not.toBeInTheDocument(),
+ () =>
+ expect(
+ canvas.queryByText(/I keep running in the background/),
+ ).not.toBeInTheDocument(),
{ timeout: STORY_RUN_MS * 2 },
);
},
@@ -1196,11 +1525,6 @@ export const BackgroundRun = meta.story({
text: "Summarize recent ingestion errors.",
},
},
- {
- id: "assistant-1",
- role: "assistant",
- content: { type: "loading" },
- },
],
},
});
@@ -1217,11 +1541,6 @@ export const BackgroundRunStops = meta.story({
text: "Summarize recent ingestion errors.",
},
},
- {
- id: "assistant-1",
- role: "assistant",
- content: { type: "loading" },
- },
],
},
render: function Render(args) {
@@ -1517,19 +1836,22 @@ export const FeedbackControlsWaitForTurnEnd = meta.story({
},
play: async ({ canvasElement }: { canvasElement: HTMLElement }) => {
const canvas = within(canvasElement);
+ const midTurnText =
+ "I found a cluster of ingestion errors around malformed JSON payloads";
- await canvas.findByText(
- "I found a cluster of ingestion errors around malformed JSON payloads",
- );
+ expect(canvas.queryByText(midTurnText)).not.toBeInTheDocument();
+ expect(
+ canvas.queryByRole("button", { name: "Good response" }),
+ ).not.toBeInTheDocument();
+ expect(
+ canvas.queryByRole("button", { name: "Bad response" }),
+ ).not.toBeInTheDocument();
- await waitFor(() => {
- expect(
- canvas.queryByRole("button", { name: "Good response" }),
- ).not.toBeInTheDocument();
- expect(
- canvas.queryByRole("button", { name: "Bad response" }),
- ).not.toBeInTheDocument();
- });
+ await userEvent.click(canvas.getByRole("button", { name: "Working…" }));
+ await expect(canvas.getByText(midTurnText)).toBeVisible();
+ expect(
+ canvas.queryByRole("button", { name: "Good response" }),
+ ).not.toBeInTheDocument();
},
});
@@ -1577,6 +1899,8 @@ export const ProjectedMessageSubmitsFeedbackToSource = meta.story({
selectedConversationId: "conversation-1",
isAssistantTurnInProgress: false,
onSubmitFeedback: fn(),
+ // Two projected blocks join into one answer, and only the first carries the
+ // persisted id: feedback still has to reach that source message.
messages: [
{
id: "display-text-assistant-1-1",
@@ -1588,6 +1912,15 @@ export const ProjectedMessageSubmitsFeedbackToSource = meta.story({
text: "The errors were caused by malformed JSON payloads.",
},
},
+ {
+ id: "display-text-assistant-1-2",
+ runId: "run-1",
+ role: "assistant",
+ content: {
+ type: "text",
+ text: "Retrying them after the fix cleared the backlog.",
+ },
+ },
],
},
play: async ({ args, canvasElement }) => {
@@ -1596,6 +1929,10 @@ export const ProjectedMessageSubmitsFeedbackToSource = meta.story({
name: "Good response",
});
+ await expect(
+ canvas.getAllByRole("button", { name: "Copy message" }),
+ ).toHaveLength(1);
+
await userEvent.click(goodResponseButton);
await expect(args.onSubmitFeedback).toHaveBeenCalledWith({
messageId: "assistant-1",
@@ -1732,6 +2069,7 @@ export const ContinuedToolResultRendersOnce = meta.story({
const canvas = within(canvasElement);
await expect(canvas.queryByText("Called 2 tools")).not.toBeInTheDocument();
+ await userEvent.click(canvas.getByRole("button", { name: "Activity" }));
await expect(
canvas.getAllByLabelText("createDashboardWidget: succeeded"),
).toHaveLength(1);
@@ -1744,3 +2082,344 @@ export const ContinuedToolResultRendersOnce = meta.story({
).not.toBeInTheDocument();
},
});
+
+export const RedirectStaysActionableAfterMoreThinking = meta.story({
+ name: "(Test) Redirect Stays Actionable After More Thinking",
+ args: {
+ selectedConversationId: "conversation-1",
+ messages: [
+ {
+ id: "user-1",
+ role: "user",
+ content: { type: "text", text: "Take me to the failing traces." },
+ },
+ {
+ id: "assistant-reasoning-1",
+ timestamp: new Date("2026-08-06T15:26:26.000Z").getTime(),
+ role: "assistant",
+ content: {
+ type: "reasoning",
+ text: "The error traces view is the right destination.",
+ isStreaming: false,
+ },
+ },
+ {
+ id: "assistant-redirect",
+ role: "assistant",
+ content: {
+ type: "redirectAction",
+ label: "Open error traces",
+ href: "/project/project-1/traces?level=ERROR",
+ },
+ },
+ {
+ // A redirect tool result merges into a preceding text block rather than
+ // arriving standalone whenever there is one to merge into.
+ id: "assistant-ack",
+ role: "assistant",
+ content: {
+ type: "text",
+ text: "The dashboard shows the same spike.",
+ redirectAction: {
+ type: "redirectAction",
+ label: "Open the latency dashboard",
+ href: "/project/project-1/dashboards/latency",
+ },
+ },
+ },
+ {
+ id: "assistant-reasoning-2",
+ role: "assistant",
+ content: {
+ type: "reasoning",
+ text: "I should also explain what they will see.",
+ isStreaming: false,
+ },
+ },
+ {
+ id: "assistant-answer",
+ runId: "run-1",
+ timestamp: new Date("2026-08-06T15:27:17.000Z").getTime(),
+ role: "assistant",
+ content: { type: "text", text: "These are all timeouts." },
+ },
+ ],
+ },
+ play: async ({ canvasElement }: { canvasElement: HTMLElement }) => {
+ const canvas = within(canvasElement);
+
+ // The drawer stays collapsed, so a redirect parked inside it would be lost,
+ // whether it arrived standalone or merged into a mid-turn text block.
+ const drawer = canvas.getByRole("button", { name: /Worked for/ });
+ await expect(drawer).toHaveAttribute("aria-expanded", "false");
+ await expect(
+ canvas.getByRole("button", { name: "Open the latency dashboard" }),
+ ).toBeVisible();
+
+ // ...and opening the drawer must not offer the same action a second time.
+ await userEvent.click(drawer);
+ await expect(
+ canvas.getAllByRole("button", { name: "Open the latency dashboard" }),
+ ).toHaveLength(1);
+ },
+});
+
+export const SourcesReachTheSettledAnswer = meta.story({
+ name: "(Test) Sources Reach The Settled Answer",
+ args: {
+ selectedConversationId: "conversation-1",
+ messages: [
+ {
+ id: "user-1",
+ role: "user",
+ content: { type: "text", text: "How do I mask trace input?" },
+ },
+ {
+ id: "assistant-ack",
+ timestamp: new Date("2026-08-06T15:26:26.000Z").getTime(),
+ role: "assistant",
+ content: {
+ type: "text",
+ text: "The docs cover masking.",
+ sources: [
+ {
+ title: "Masking",
+ url: "https://langfuse.com/docs/observability/features/masking",
+ faviconUrl: "https://langfuse.com/favicon.ico",
+ },
+ ],
+ },
+ },
+ {
+ id: "assistant-reasoning",
+ role: "assistant",
+ content: {
+ type: "reasoning",
+ text: "Now I can summarise the masking setup.",
+ isStreaming: false,
+ },
+ },
+ {
+ id: "assistant-answer",
+ runId: "run-1",
+ timestamp: new Date("2026-08-06T15:27:17.000Z").getTime(),
+ role: "assistant",
+ content: { type: "text", text: "Set a mask function on the SDK." },
+ },
+ ],
+ },
+ play: async ({ canvasElement }: { canvasElement: HTMLElement }) => {
+ const canvas = within(canvasElement);
+
+ // The citation was earned by an intermediate block that lives in the
+ // collapsed drawer; it has to travel to the answer the user is reading.
+ await userEvent.click(canvas.getByRole("button", { name: "Sources" }));
+ // The popover portals into a layer container outside the canvas.
+ const source = await screen.findByRole("link", { name: /Masking/ });
+ await waitFor(() => expect(source).toBeVisible());
+ await expect(source).toHaveAttribute(
+ "href",
+ "https://langfuse.com/docs/observability/features/masking",
+ );
+ },
+});
+
+// Enough turns to overflow the conversation viewport so real scrolling happens.
+const scrollableTranscript: InAppAgentWindowMessage[] = Array.from(
+ { length: 12 },
+ (_, index): InAppAgentWindowMessage => ({
+ id: `scroll-${index}`,
+ role: index % 2 === 0 ? "user" : "assistant",
+ content: {
+ type: "text",
+ text:
+ index % 2 === 0
+ ? `Question ${index / 2 + 1} about the latency regression.`
+ : `Finding ${(index + 1) / 2}: the reranker dominates p95 for this segment, and the effect persists across retries.`,
+ },
+ }),
+);
+
+/**
+ * The scroller is a plain overflow container with no role of its own, and
+ * `overscroll-contain` is what distinguishes it from the composer textarea.
+ */
+function getConversationViewport(canvasElement: HTMLElement) {
+ const viewport = canvasElement.querySelector<HTMLElement>(
+ ".overscroll-contain",
+ );
+
+ if (!viewport) {
+ // `globalThis` because the Error story shadows the constructor here.
+ throw new globalThis.Error("Expected the assistant conversation viewport");
+ }
+
+ return viewport;
+}
+
+// Mirrors AUTO_SCROLL_THRESHOLD_PX in the component.
+function isViewportAtBottom(viewport: HTMLElement) {
+ return (
+ viewport.scrollHeight - viewport.scrollTop - viewport.clientHeight <= 50
+ );
+}
+
+export const LatestReattachesAutoFollow = meta.story({
+ name: "(Test) Latest Reattaches Auto Follow",
+ args: {
+ selectedConversationId: "conversation-1",
+ messages: scrollableTranscript,
+ },
+ play: async ({ canvasElement }: { canvasElement: HTMLElement }) => {
+ const canvas = within(canvasElement);
+ const viewport = getConversationViewport(canvasElement);
+ const latest = { name: "Scroll to latest message" };
+
+ // Mounting pins us to the newest message, so there is nothing to catch up to.
+ await waitFor(() => expect(isViewportAtBottom(viewport)).toBe(true));
+ await expect(canvas.queryByRole("button", latest)).not.toBeInTheDocument();
+
+ viewport.scrollTop = 0;
+ await waitFor(() =>
+ expect(canvas.getByRole("button", latest)).toBeVisible(),
+ );
+
+ await userEvent.click(canvas.getByRole("button", latest));
+
+ // The pill hides the moment we start travelling, so the smooth scroll's
+ // intermediate positions cannot make it flicker back in.
+ await expect(canvas.queryByRole("button", latest)).not.toBeInTheDocument();
+ await waitFor(() => expect(isViewportAtBottom(viewport)).toBe(true));
+ await expect(canvas.queryByRole("button", latest)).not.toBeInTheDocument();
+ },
+});
+
+export const LatestHidesWhenTheDrawerCollapses = meta.story({
+ name: "(Test) Latest Hides When The Drawer Collapses",
+ args: {
+ selectedConversationId: "conversation-1",
+ messages: [
+ {
+ id: "user-1",
+ role: "user",
+ content: { type: "text", text: "What changed yesterday?" },
+ },
+ {
+ id: "assistant-reasoning",
+ timestamp: new Date("2026-08-06T15:26:26.000Z").getTime(),
+ role: "assistant",
+ content: {
+ type: "reasoning",
+ // Long enough that expanding the drawer overflows the viewport and
+ // collapsing it fits again.
+ text: Array.from(
+ { length: 40 },
+ (_, index) =>
+ `Step ${index + 1}: compare yesterday's dashboard against the trailing week.`,
+ ).join("\n"),
+ isStreaming: false,
+ },
+ },
+ {
+ id: "assistant-answer",
+ runId: "run-1",
+ timestamp: new Date("2026-08-06T15:27:17.000Z").getTime(),
+ role: "assistant",
+ content: {
+ type: "text",
+ text: "Yesterday's latency dashboard picked up a reranking spike.",
+ },
+ },
+ ],
+ },
+ play: async ({ canvasElement }: { canvasElement: HTMLElement }) => {
+ const canvas = within(canvasElement);
+ const viewport = getConversationViewport(canvasElement);
+ const latest = { name: "Scroll to latest message" };
+
+ const drawer = canvas.getByRole("button", { name: /Worked for/ });
+ await expect(canvas.queryByRole("button", latest)).not.toBeInTheDocument();
+
+ // Read to the end of the opened drawer, then scroll back up. Going down
+ // first is what makes the scroll position actually change on the way up.
+ await userEvent.click(drawer);
+ await userEvent.click(canvas.getByText("Thought"));
+ viewport.scrollTop = viewport.scrollHeight;
+ await waitFor(() => expect(viewport.scrollTop).toBeGreaterThan(0));
+
+ viewport.scrollTop = 0;
+ await waitFor(() =>
+ expect(canvas.getByRole("button", latest)).toBeVisible(),
+ );
+
+ // Collapsing shrinks the transcript back under the viewport without any
+ // scroll event, so only the resize observer can retire the pill.
+ await userEvent.click(drawer);
+ await waitFor(() =>
+ expect(canvas.queryByRole("button", latest)).not.toBeInTheDocument(),
+ );
+ },
+});
+
+export const SendingReattachesAutoFollow = meta.story({
+ name: "(Test) Sending Reattaches Auto Follow",
+ args: {
+ selectedConversationId: "conversation-1",
+ messages: scrollableTranscript,
+ },
+ render: function Render(args) {
+ const [messages, setMessages] = useState(args.messages);
+
+ return (
+ <InAppAgentWindowStoryShell isExpanded={args.isExpanded}>
+ {({ isHeaderDragHandleEnabled }) => (
+ <InAppAgentWindow
+ {...args}
+ isHeaderDragHandleEnabled={isHeaderDragHandleEnabled}
+ messages={messages}
+ onSubmit={(input) => {
+ // The real provider appends a placeholder behind the user
+ // message, so the newest message is not the one that was sent.
+ setMessages((currentMessages) => [
+ ...currentMessages,
+ {
+ id: `sent-${currentMessages.length}`,
+ role: "user",
+ content: { type: "text", text: input },
+ },
+ {
+ id: `connecting-${currentMessages.length}`,
+ role: "assistant",
+ content: { type: "loading", label: "Connecting..." },
+ },
+ ]);
+ return true;
+ }}
+ />
+ )}
+ </InAppAgentWindowStoryShell>
+ );
+ },
+ play: async ({ canvasElement }: { canvasElement: HTMLElement }) => {
+ const canvas = within(canvasElement);
+ const viewport = getConversationViewport(canvasElement);
+ const latest = { name: "Scroll to latest message" };
+
+ viewport.scrollTop = 0;
+ await waitFor(() =>
+ expect(canvas.getByRole("button", latest)).toBeVisible(),
+ );
+
+ await userEvent.type(
+ canvas.getByRole("textbox", { name: "Message the assistant" }),
+ "And the error rate?",
+ );
+ await userEvent.click(canvas.getByRole("button", { name: "Send message" }));
+
+ // Saying something is a request to follow along again.
+ await waitFor(() =>
+ expect(canvas.queryByRole("button", latest)).not.toBeInTheDocument(),
+ );
+ await waitFor(() => expect(isViewportAtBottom(viewport)).toBe(true));
+ },
+});
diff --git a/web/src/features/in-app-agent/components/InAppAgentWindow.tsx b/web/src/features/in-app-agent/components/InAppAgentWindow.tsx
index 208a474292..158c23efcb 100644
--- a/web/src/features/in-app-agent/components/InAppAgentWindow.tsx
+++ b/web/src/features/in-app-agent/components/InAppAgentWindow.tsx
@@ -2,14 +2,18 @@
import {
type KeyboardEvent,
+ type ReactNode,
useCallback,
useEffect,
+ useMemo,
useRef,
useState,
} from "react";
import {
ArrowRight,
+ ArrowDown,
BotMessageSquare,
+ ChevronRight,
History,
Info,
Maximize2,
@@ -43,7 +47,11 @@ import {
type InAppAgentMessageContent,
type InAppAgentMessageRole,
} from "./InAppAgentMessage";
-import type { InAppAgentMessageFeedbackValue } from "@langfuse/shared/in-app-agent";
+import type {
+ InAppAgentMessageFeedbackValue,
+ InAppAgentMessageSource,
+} from "@langfuse/shared/in-app-agent";
+import { deduplicateBy } from "@/src/utils/arrays";
import type { InAppAgentScreenContextDescription } from "@/src/features/in-app-agent/context";
import type { InAppAgentActivityByConversationId } from "@/src/features/in-app-agent/lib/inAppAgentActivity";
import { ConversationActivityIndicator } from "@/src/features/in-app-agent/components/ConversationActivityIndicator";
@@ -51,9 +59,11 @@ import { InAppAgentBackgroundHint } from "@/src/features/in-app-agent/components
import { useInAppAgentBackgroundHint } from "@/src/features/in-app-agent/lib/useInAppAgentBackgroundHint";
import { InAppAgentToolCallCard } from "@/src/features/in-app-agent/components/InAppAgentToolCallCard";
import {
+ getInAppAgentActivityProgressLabel,
type InAppAgentError,
isInAppAgentRateLimited,
} from "@/src/features/in-app-agent/components/utils/utils";
+import messageStyles from "./InAppAgentMessage.module.css";
import styles from "./InAppAgentWindow.module.css";
import { assertUnreachable } from "@/src/utils/types";
import {
@@ -71,14 +81,24 @@ import { Tabs, TabsList, TabsTrigger } from "@/src/components/ui/tabs";
const AUTO_SCROLL_THRESHOLD_PX = 50;
const SCROLL_DIRECTION_TOLERANCE_PX = 1;
-function scrollViewportToBottom(viewport: HTMLDivElement | null) {
+function isViewportNearBottom(viewport: HTMLElement) {
+ return (
+ viewport.scrollHeight - viewport.scrollTop - viewport.clientHeight <=
+ AUTO_SCROLL_THRESHOLD_PX
+ );
+}
+
+function scrollViewportToBottom(
+ viewport: HTMLDivElement | null,
+ behavior: ScrollBehavior = "auto",
+) {
if (!viewport) {
return;
}
viewport.scrollTo({
top: viewport.scrollHeight,
- behavior: "auto",
+ behavior,
});
}
@@ -183,35 +203,35 @@ function formatScreenContextNotice(
description: InAppAgentScreenContextDescription,
) {
if (description.type === "page") {
- return "The assistant is aware of your current page.";
+ return "Current page in context";
}
if (description.type === "observation") {
- return "The assistant is aware that you're viewing this observation.";
+ return "Current observation in context";
}
if (description.type === "trace") {
- return "The assistant is aware that you're viewing this trace.";
+ return "Current trace in context";
}
if (description.type === "prompt") {
- return "The assistant is aware that you're viewing this prompt.";
+ return "Current prompt in context";
}
if (description.type === "session") {
- return "The assistant is aware that you're viewing this session.";
+ return "Current session in context";
}
if (description.type === "dataset") {
- return "The assistant is aware that you're viewing this dataset.";
+ return "Current dataset in context";
}
if (description.type === "datasetItem") {
- return "The assistant is aware that you're viewing this dataset item.";
+ return "Current dataset item in context";
}
if (description.type === "experimentRun") {
- return "The assistant is aware that you're viewing this experiment run.";
+ return "Current experiment run in context";
}
if (
@@ -229,9 +249,7 @@ function formatScreenContextNotice(
"datasets-list": "dataset",
}[description.type];
- return description.hasAppliedFilters
- ? `The assistant is aware of this ${listLabel} view and its filters.`
- : `The assistant is aware of this ${listLabel} view.`;
+ return `Current ${listLabel} view in context`;
}
return assertUnreachable(description);
@@ -241,10 +259,536 @@ export type InAppAgentWindowMessage = {
id: string;
feedbackMessageId?: string;
runId?: string;
+ timestamp?: number;
role: InAppAgentMessageRole;
content: InAppAgentMessageContent;
};
+type ConversationDisplayItem =
+ | {
+ type: "user";
+ message: InAppAgentWindowMessage;
+ hasPreviousMessage: boolean;
+ }
+ | {
+ type: "activity";
+ /** Identifies the turn, not its contents: the drawer must survive both
+ * the first activity arriving and the run settling without remounting,
+ * or an expanded drawer collapses under the user. */
+ key: string;
+ messages: InAppAgentWindowMessage[];
+ startTimestamp?: number;
+ endTimestamp?: number;
+ followsUser: boolean;
+ isInProgress: boolean;
+ }
+ | {
+ type: "assistant";
+ message: InAppAgentWindowMessage;
+ followsUser: boolean;
+ isFinalAnswer: boolean;
+ };
+
+type InAppAgentTextMessage = InAppAgentWindowMessage & {
+ content: Extract<InAppAgentMessageContent, { type: "text" }>;
+};
+
+type InAppAgentRedirectActionContent = Extract<
+ InAppAgentMessageContent,
+ { type: "redirectAction" }
+>;
+
+/**
+ * Fold a turn's answer blocks into the single bubble the user reads. Each field
+ * takes the last block that defined it, except `sources`, which arrive already
+ * unioned across the whole turn so a citation earned mid-turn is not lost.
+ */
+function joinAnswerMessages(
+ messages: InAppAgentWindowMessage[],
+ turnSources: InAppAgentMessageSource[],
+ turnRedirectAction: InAppAgentRedirectActionContent | undefined,
+): InAppAgentWindowMessage | null {
+ const textMessages = messages.filter(
+ (message): message is InAppAgentTextMessage =>
+ message.content.type === "text",
+ );
+ const lastText = textMessages.at(-1);
+
+ // A turn can end on a bare redirect with no text to attach it to.
+ if (!lastText) {
+ return messages.at(-1) ?? null;
+ }
+
+ const lastDefined = <T,>(
+ pick: (message: InAppAgentTextMessage) => T | undefined,
+ ): T | undefined =>
+ textMessages.reduce<T | undefined>(
+ (found, message) => pick(message) ?? found,
+ undefined,
+ );
+ const runId = lastDefined((message) => message.runId);
+ const feedbackMessageId = lastDefined((message) => message.feedbackMessageId);
+ const feedback = lastDefined((message) => message.content.feedback);
+
+ return {
+ ...lastText,
+ ...(runId ? { runId } : {}),
+ ...(feedbackMessageId ? { feedbackMessageId } : {}),
+ content: {
+ type: "text",
+ text: textMessages.map((message) => message.content.text).join("\n\n"),
+ ...(textMessages.some((message) => message.content.isStreaming)
+ ? { isStreaming: true }
+ : {}),
+ ...(turnSources.length > 0 ? { sources: turnSources } : {}),
+ ...(feedback ? { feedback } : {}),
+ ...(turnRedirectAction ? { redirectAction: turnRedirectAction } : {}),
+ },
+ };
+}
+
+function buildConversationDisplayItems(
+ messages: InAppAgentWindowMessage[],
+ isRunUnsettled: boolean,
+): ConversationDisplayItem[] {
+ const items: ConversationDisplayItem[] = [];
+ // A tool call awaiting approval is hoisted into its own card above the
+ // composer, and the drawer speaks for a turn that has produced nothing yet,
+ // so neither belongs in the transcript.
+ const visibleMessages = messages.flatMap((message) => {
+ if (message.content.type === "loading") {
+ return [];
+ }
+
+ if (message.content.type !== "toolGroup") {
+ return [message];
+ }
+
+ const visibleTools = message.content.tools.filter((tool) => !tool.approval);
+ if (visibleTools.length === 0) {
+ return [];
+ }
+
+ return [
+ {
+ ...message,
+ content: { ...message.content, tools: visibleTools },
+ } satisfies InAppAgentWindowMessage,
+ ];
+ });
+
+ for (let index = 0; index < visibleMessages.length; ) {
+ const message = visibleMessages[index];
+ if (!message) {
+ break;
+ }
+
+ if (message.role === "user") {
+ items.push({
+ type: "user",
+ message,
+ hasPreviousMessage: index > 0,
+ });
+ index++;
+ continue;
+ }
+
+ const turnStartIndex = index;
+ while (
+ index < visibleMessages.length &&
+ visibleMessages[index]?.role === "assistant"
+ ) {
+ index++;
+ }
+
+ const turnMessages = visibleMessages.slice(turnStartIndex, index);
+ const isInProgress = isRunUnsettled && index === visibleMessages.length;
+ const lastReasoningIndex = turnMessages.findLastIndex(
+ (turnMessage) => turnMessage.content.type === "reasoning",
+ );
+ // Trailing text is the answer; text before the turn's last thought is still
+ // work. A proposed redirect is always actionable, so it never stays behind
+ // in the drawer. Declared once, with the activity bucket as its exact
+ // complement, so no message can fall out of both.
+ const isAnswerPart = (
+ turnMessage: InAppAgentWindowMessage,
+ turnIndex: number,
+ ) =>
+ !isInProgress &&
+ (turnMessage.content.type === "redirectAction" ||
+ (turnMessage.content.type === "text" &&
+ turnIndex > lastReasoningIndex));
+ const answerMessages = turnMessages.filter(isAnswerPart);
+ const activityMessages = turnMessages.filter(
+ (turnMessage, turnIndex) => !isAnswerPart(turnMessage, turnIndex),
+ );
+ const turnSources = deduplicateBy(
+ turnMessages.flatMap((turnMessage) =>
+ turnMessage.content.type === "text"
+ ? (turnMessage.content.sources ?? [])
+ : [],
+ ),
+ (source) => source.url,
+ );
+ // A proposed redirect belongs to the turn, not to whichever block carried
+ // it: the tool result is merged into a preceding text message when there is
+ // one and emitted standalone when there is not, and either can land in the
+ // drawer.
+ const turnRedirectAction = turnMessages.reduce<
+ InAppAgentRedirectActionContent | undefined
+ >(
+ (found, turnMessage) =>
+ (turnMessage.content.type === "redirectAction"
+ ? turnMessage.content
+ : turnMessage.content.type === "text"
+ ? turnMessage.content.redirectAction
+ : undefined) ?? found,
+ undefined,
+ );
+ const joinedAnswer = joinAnswerMessages(
+ answerMessages,
+ turnSources,
+ turnRedirectAction,
+ );
+ const precedingMessage = visibleMessages[turnStartIndex - 1];
+ const followsUser = precedingMessage?.role === "user";
+
+ if (activityMessages.length > 0) {
+ items.push({
+ type: "activity",
+ key: getActivityKey(
+ followsUser ? precedingMessage?.id : turnMessages[0]?.id,
+ ),
+ messages: activityMessages,
+ startTimestamp: turnMessages.find(
+ (turnMessage) => turnMessage.timestamp !== undefined,
+ )?.timestamp,
+ endTimestamp:
+ joinedAnswer?.timestamp ?? activityMessages.at(-1)?.timestamp,
+ followsUser,
+ isInProgress,
+ });
+ }
+
+ if (joinedAnswer) {
+ items.push({
+ type: "assistant",
+ message: joinedAnswer,
+ followsUser: activityMessages.length === 0 && followsUser,
+ isFinalAnswer: !isInProgress,
+ });
+ }
+ }
+
+ const lastItem = items.at(-1);
+ if (isRunUnsettled && lastItem?.type === "user") {
+ // Same key the turn will claim once its first message lands, so the caret
+ // the user may already have opened is not torn down underneath them.
+ items.push({
+ type: "activity",
+ key: getActivityKey(lastItem.message.id),
+ messages: [],
+ followsUser: true,
+ isInProgress: true,
+ });
+ }
+
+ return items;
+}
+
+function getActivityKey(turnAnchorMessageId: string | undefined) {
+ return `activity-${turnAnchorMessageId ?? "start"}`;
+}
+
+function formatWorkedDuration(totalSeconds: number) {
+ const hours = Math.floor(totalSeconds / 3600);
+ const minutes = Math.floor((totalSeconds % 3600) / 60);
+ const seconds = totalSeconds % 60;
+
+ if (hours > 0) {
+ const parts = [`${hours}h`];
+ if (minutes > 0) {
+ parts.push(`${minutes}m`);
+ }
+ if (seconds > 0) {
+ parts.push(`${seconds}s`);
+ }
+ return parts.join(" ");
+ }
+
+ if (minutes > 0) {
+ return seconds > 0 ? `${minutes}m ${seconds}s` : `${minutes}m`;
+ }
+
+ return `${totalSeconds}s`;
+}
+
+function getActivityGroupLabel({
+ durationSeconds,
+ hasDetails,
+ isAwaitingApproval,
+ isInProgress,
+ toolNames,
+}: {
+ durationSeconds: number | null;
+ hasDetails: boolean;
+ isAwaitingApproval: boolean;
+ isInProgress: boolean;
+ toolNames: string[];
+}) {
+ if (!isInProgress) {
+ return durationSeconds === null
+ ? "Activity"
+ : `Worked for ${formatWorkedDuration(durationSeconds)}`;
+ }
+
+ // The run has stopped and owes the user a decision, so it must not keep
+ // narrating the last thing it did.
+ if (isAwaitingApproval) {
+ return "Waiting for your approval…";
+ }
+
+ return hasDetails
+ ? getInAppAgentActivityProgressLabel(toolNames)
+ : "There for you in a second…";
+}
+
+function AssistantActivityGroup({
+ endTimestamp,
+ isAwaitingApproval,
+ isCompact,
+ isInProgress,
+ messages,
+ startTimestamp,
+}: {
+ endTimestamp?: number;
+ isAwaitingApproval: boolean;
+ isCompact: boolean;
+ isInProgress: boolean;
+ messages: InAppAgentWindowMessage[];
+ startTimestamp?: number;
+}) {
+ const hasDetails = messages.length > 0;
+ const [isOpen, setIsOpen] = useState(false);
+ const durationSeconds =
+ startTimestamp !== undefined && endTimestamp !== undefined
+ ? Math.max(1, Math.round((endTimestamp - startTimestamp) / 1_000))
+ : null;
+ const toolNames = messages.flatMap((message) =>
+ message.content.type === "toolGroup"
+ ? message.content.tools.map((tool) => tool.name)
+ : [],
+ );
+ const label = getActivityGroupLabel({
+ durationSeconds,
+ hasDetails,
+ isAwaitingApproval,
+ isInProgress,
+ toolNames,
+ });
+
+ return (
+ <div className="w-full">
+ <button
+ type="button"
+ aria-expanded={isOpen}
+ aria-label={label}
+ className={cn(
+ "text-muted-foreground focus-visible:ring-ring flex w-full items-center gap-1 py-1 text-left outline-none focus-visible:ring-2",
+ isCompact ? "text-[0.775rem]" : "text-sm",
+ )}
+ onClick={() => {
+ setIsOpen((open) => !open);
+ }}
+ >
+ {/* Nothing is happening while we wait on the user, so hold the shimmer. */}
+ <span
+ className={cn(
+ isInProgress &&
+ !isAwaitingApproval &&
+ messageStyles.thinkingShimmer,
+ )}
+ >
+ {label}
+ </span>
+ <ChevronRight
+ aria-hidden="true"
+ className={cn("size-3.5 transition-transform", isOpen && "rotate-90")}
+ />
+ </button>
+ {isOpen && hasDetails ? (
+ <div className="flex flex-col gap-1 pt-2 pb-1">
+ {messages.map((message) => (
+ <InAppAgentMessage
+ key={message.id}
+ role={message.role}
+ content={message.content}
+ isCompact={isCompact}
+ isFinalAnswer={false}
+ />
+ ))}
+ </div>
+ ) : null}
+ </div>
+ );
+}
+
+/**
+ * Owns the conversation viewport and everything about following it: whether we
+ * are pinned to the newest message, the `Latest` escape hatch, and the smooth
+ * scroll it starts. Mount this keyed by conversation so switching gives it a
+ * fresh scroll position instead of an effect that resets one.
+ */
+function ConversationScroller({
+ children,
+ displayItems,
+}: {
+ children: ReactNode;
+ /** What is actually rendered, so the height can only change with it. Keying
+ * on the raw messages misses a turn that settles without new messages and
+ * reveals its answer. */
+ displayItems: ConversationDisplayItem[];
+}) {
+ const viewportRef = useRef<HTMLDivElement>(null);
+ // Sticky intent to follow, which survives scrolling down through the middle
+ // of the transcript; `isAtLatest` only says whether the pill is needed.
+ const isAutoScrollAttachedRef = useRef(true);
+ const isScrollingToLatestRef = useRef(false);
+ const previousScrollTopRef = useRef(0);
+ // The newest user message, rather than the newest message: a turn often
+ // appends a placeholder after it, which would mask the send.
+ const lastUserMessageId = displayItems.findLast(
+ (item) => item.type === "user",
+ )?.message.id;
+ const [followState, setFollowState] = useState({
+ lastUserMessageId,
+ isAtLatest: true,
+ });
+ // Saying something is a request to follow along again, even from far up.
+ // Derived rather than keyed on a remount, which would take the transcript —
+ // and every drawer the user has opened — down with it.
+ const hasNewUserMessage = followState.lastUserMessageId !== lastUserMessageId;
+ const isAtLatest = hasNewUserMessage || followState.isAtLatest;
+ if (hasNewUserMessage) {
+ isAutoScrollAttachedRef.current = true;
+ }
+ const setIsAtLatest = useCallback(
+ (nextIsAtLatest: boolean) => {
+ setFollowState({ lastUserMessageId, isAtLatest: nextIsAtLatest });
+ },
+ [lastUserMessageId],
+ );
+
+ useEffect(() => {
+ if (isAutoScrollAttachedRef.current) {
+ scrollViewportToBottom(viewportRef.current);
+ }
+ }, [displayItems]);
+
+ useEffect(() => {
+ const viewport = viewportRef.current;
+ if (!viewport || typeof ResizeObserver === "undefined") {
+ return;
+ }
+
+ // Content can shrink back under the viewport without a scroll event — a
+ // collapsing activity drawer does exactly that.
+ const syncIsAtLatest = () => {
+ if (isScrollingToLatestRef.current) {
+ return;
+ }
+
+ if (isViewportNearBottom(viewport)) {
+ isAutoScrollAttachedRef.current = true;
+ // The raw setter keeps this subscription off `lastUserMessageId`: a
+ // resize says nothing about which send we have caught up with.
+ setFollowState((state) => ({ ...state, isAtLatest: true }));
+ }
+ };
+
+ const observer = new ResizeObserver(syncIsAtLatest);
+ const content = viewport.firstElementChild;
+ if (content) {
+ observer.observe(content);
+ }
+ observer.observe(viewport);
+
+ return () => {
+ observer.disconnect();
+ };
+ }, []);
+
+ return (
+ <div className="relative min-h-0 flex-1">
+ <div
+ ref={viewportRef}
+ // `overscroll-contain`: the conversation is the only scroller, so its
+ // rubber-band must not chain out to whatever is behind it.
+ className="h-full overflow-y-auto overscroll-contain"
+ onScroll={(event) => {
+ const viewport = event.currentTarget;
+ const isNearBottom = isViewportNearBottom(viewport);
+ const scrolledUp =
+ viewport.scrollTop <
+ previousScrollTopRef.current - SCROLL_DIRECTION_TOLERANCE_PX;
+
+ // Our own smooth scroll emits a stream of events on the way down;
+ // only arriving, or the user overriding it, ends that.
+ if (isScrollingToLatestRef.current) {
+ if (isNearBottom) {
+ isScrollingToLatestRef.current = false;
+ isAutoScrollAttachedRef.current = true;
+ setIsAtLatest(true);
+ } else if (scrolledUp) {
+ isScrollingToLatestRef.current = false;
+ isAutoScrollAttachedRef.current = false;
+ setIsAtLatest(false);
+ }
+
+ previousScrollTopRef.current = viewport.scrollTop;
+ return;
+ }
+
+ if (scrolledUp && !isNearBottom) {
+ isAutoScrollAttachedRef.current = false;
+ } else if (isNearBottom) {
+ isAutoScrollAttachedRef.current = true;
+ }
+
+ setIsAtLatest(isNearBottom);
+ previousScrollTopRef.current = viewport.scrollTop;
+ }}
+ >
+ {children}
+ </div>
+ {!isAtLatest ? (
+ <Button
+ type="button"
+ variant="outline"
+ size="sm"
+ aria-label="Scroll to latest message"
+ className="bg-background absolute bottom-3 left-1/2 z-10 h-8 -translate-x-1/2 rounded-full px-3 shadow-sm"
+ onClick={() => {
+ isAutoScrollAttachedRef.current = true;
+ isScrollingToLatestRef.current = true;
+ setIsAtLatest(true);
+ const prefersReducedMotion =
+ typeof window.matchMedia === "function" &&
+ window.matchMedia("(prefers-reduced-motion: reduce)").matches;
+ scrollViewportToBottom(
+ viewportRef.current,
+ prefersReducedMotion ? "auto" : "smooth",
+ );
+ }}
+ >
+ <ArrowDown className="size-3.5" />
+ Latest
+ </Button>
+ ) : null}
+ </div>
+ );
+}
+
export type InAppAgentWindowConversation = {
id: string;
title: string | null;
@@ -278,6 +822,12 @@ export type InAppAgentWindowProps = {
executionUi: InAppAgentWindowExecutionUi;
hasMoreConversations: boolean;
isAssistantTurnInProgress: boolean;
+ /** True while the run itself is unfinished. Reveal animation of a finished
+ * turn must not hide the answer. Defaults to `isAssistantTurnInProgress`. */
+ isRunUnsettled?: boolean;
+ /** The run has stopped on a tool approval, so the open turn is parked on the
+ * user rather than working. */
+ isAwaitingApproval?: boolean;
isHeaderDragHandleEnabled?: boolean;
isExpanded: boolean;
isConversationInteractionDisabled: boolean;
@@ -384,6 +934,8 @@ export function InAppAgentWindow(props: InAppAgentWindowProps) {
executionUi,
hasMoreConversations,
isAssistantTurnInProgress,
+ isRunUnsettled = isAssistantTurnInProgress,
+ isAwaitingApproval = false,
isHeaderDragHandleEnabled = false,
isConversationInteractionDisabled,
isLoadingMoreConversations,
@@ -426,9 +978,6 @@ export function InAppAgentWindow(props: InAppAgentWindowProps) {
const executionStop = executionUi.stop;
const canStopRun = executionStop !== null && isAssistantTurnInProgress;
const isCancellingRun = executionStop?.status === "stopping";
- const viewportRef = useRef<HTMLDivElement>(null);
- const isAutoScrollAttachedRef = useRef(true);
- const previousScrollTopRef = useRef(0);
const inputRef = useRef<HTMLTextAreaElement>(null);
const previousIsInputDisabledRef = useRef(isComposerDisabled);
const previousIsAssistantTurnInProgressRef = useRef(
@@ -452,29 +1001,15 @@ export function InAppAgentWindow(props: InAppAgentWindowProps) {
? message.content.tools.filter((tool) => tool.approval)
: [],
);
- const visibleMessages = messages
- .map((message) => {
- if (message.content.type !== "toolGroup") {
- return message;
- }
-
- const visibleTools = message.content.tools.filter(
- (tool) => !tool.approval,
- );
-
- if (visibleTools.length === 0) {
- return null;
- }
-
- return {
- ...message,
- content: {
- ...message.content,
- tools: visibleTools,
- },
- } satisfies InAppAgentWindowMessage;
- })
- .filter((message): message is InAppAgentWindowMessage => message !== null);
+ // Rebuilding every turn on each composer keystroke is wasted work: the
+ // transcript only changes when the messages or the run's settledness do.
+ const displayItems = useMemo(
+ () => buildConversationDisplayItems(messages, isRunUnsettled),
+ [messages, isRunUnsettled],
+ );
+ const hasSettledAssistantReply = displayItems.some(
+ (item) => item.type === "assistant" && item.isFinalAnswer,
+ );
const backgroundHint = useInAppAgentBackgroundHint({
isRunActive: isAssistantTurnInProgress,
@@ -498,35 +1033,14 @@ export function InAppAgentWindow(props: InAppAgentWindowProps) {
backgroundHint.show();
}
- isAutoScrollAttachedRef.current = true;
-
setInput((currentInput) =>
currentInput.trim() === trimmedContent ? "" : currentInput,
);
-
- window.requestAnimationFrame(() => {
- scrollViewportToBottom(viewportRef.current);
- });
}
})
.catch(() => undefined);
};
- useEffect(() => {
- if (!isAutoScrollAttachedRef.current) {
- return;
- }
-
- scrollViewportToBottom(viewportRef.current);
- }, [messages]);
-
- useEffect(() => {
- isAutoScrollAttachedRef.current = true;
- previousScrollTopRef.current = 0;
-
- scrollViewportToBottom(viewportRef.current);
- }, [selectedConversationId]);
-
// Update after refs commit so setInputRef can compare the previous disabled state.
useEffect(() => {
previousIsInputDisabledRef.current = isComposerDisabled;
@@ -778,30 +1292,9 @@ export function InAppAgentWindow(props: InAppAgentWindowProps) {
</div>
</header>
<div className="relative flex min-h-0 flex-1 flex-col">
- <div
- ref={viewportRef}
- // `overscroll-contain`: the conversation is the only scroller, so its
- // rubber-band must not chain out to whatever is behind it.
- className="min-h-0 flex-1 overflow-y-auto overscroll-contain"
- onScroll={(event) => {
- const viewport = event.currentTarget;
- const distanceFromBottom =
- viewport.scrollHeight -
- viewport.scrollTop -
- viewport.clientHeight;
- const isNearBottom = distanceFromBottom <= AUTO_SCROLL_THRESHOLD_PX;
- const scrolledUp =
- viewport.scrollTop <
- previousScrollTopRef.current - SCROLL_DIRECTION_TOLERANCE_PX;
-
- if (scrolledUp && !isNearBottom) {
- isAutoScrollAttachedRef.current = false;
- } else if (isNearBottom) {
- isAutoScrollAttachedRef.current = true;
- }
-
- previousScrollTopRef.current = viewport.scrollTop;
- }}
+ <ConversationScroller
+ key={selectedConversationId}
+ displayItems={displayItems}
>
<div
className={cn(
@@ -811,9 +1304,9 @@ export function InAppAgentWindow(props: InAppAgentWindowProps) {
)}
>
{/* An empty transcript that is still loading is not an empty
- conversation. Offering the welcome screen and its quick actions
- mid-switch flashes the wrong thing and invites a click that
- would start a turn in the conversation being left behind. */}
+ conversation. Offering the welcome screen and its quick actions
+ mid-switch flashes the wrong thing and invites a click that
+ would start a turn in the conversation being left behind. */}
{messages.length === 0 &&
isSelectedConversationHydrating ? null : messages.length === 0 ? (
<div className="flex h-full w-full flex-1 flex-col items-center justify-center px-2">
@@ -842,48 +1335,64 @@ export function InAppAgentWindow(props: InAppAgentWindowProps) {
</div>
) : null}
- <ol className="flex w-full flex-col gap-3 pb-4">
- {visibleMessages.map((message, index) => {
- const hasFullWidthContent =
- message.content.type === "toolGroup" ||
- message.content.type === "redirectAction" ||
- message.content.type === "reasoning";
+ <ol className="flex w-full flex-col gap-1 pb-4">
+ {displayItems.map((item) => {
+ if (item.type === "user") {
+ return (
+ <li
+ key={item.message.id}
+ className={cn(
+ "ml-auto w-fit max-w-[92%]",
+ item.hasPreviousMessage && "mt-3",
+ )}
+ >
+ <InAppAgentMessage
+ role={item.message.role}
+ content={item.message.content}
+ isCompact={!isExpanded}
+ />
+ </li>
+ );
+ }
+
+ if (item.type === "activity") {
+ return (
+ <li
+ key={item.key}
+ className={cn("w-full", item.followsUser && "mt-3")}
+ >
+ <AssistantActivityGroup
+ messages={item.messages}
+ startTimestamp={item.startTimestamp}
+ endTimestamp={item.endTimestamp}
+ isCompact={!isExpanded}
+ isInProgress={item.isInProgress}
+ isAwaitingApproval={
+ item.isInProgress && isAwaitingApproval
+ }
+ />
+ </li>
+ );
+ }
- const nextUserMessageIndex = visibleMessages.findIndex(
- (nextMessage, nextIndex) =>
- nextIndex > index && nextMessage.role === "user",
- );
- const nextTurnStartIndex =
- nextUserMessageIndex === -1
- ? visibleMessages.length
- : nextUserMessageIndex;
- const isCurrentTurnInProgress =
- isAssistantTurnInProgress && nextUserMessageIndex === -1;
- const isLastMessageOfTurn = visibleMessages
- .slice(index + 1, nextTurnStartIndex)
- .every((nextMessage) => nextMessage.role !== "assistant");
+ const message = item.message;
const feedbackRunId =
- message.role === "assistant" &&
- message.content.type === "text" &&
- !isCurrentTurnInProgress &&
- isLastMessageOfTurn
+ message.content.type === "text" && item.isFinalAnswer
? message.runId
: undefined;
return (
<li
key={message.id}
- className={cn(
- "max-w-[92%]",
- hasFullWidthContent ? "w-full" : "w-fit",
- message.role === "user" && "ml-auto",
- )}
+ className={cn("w-full", item.followsUser && "mt-3")}
>
<InAppAgentMessage
role={message.role}
content={message.content}
isCompact={!isExpanded}
isFeedbackDisabled={isConversationInteractionDisabled}
+ isFinalAnswer={item.isFinalAnswer}
+ timestamp={message.timestamp}
onSubmitFeedback={
feedbackRunId
? (params) =>
@@ -905,7 +1414,7 @@ export function InAppAgentWindow(props: InAppAgentWindowProps) {
<InAppAgentGenericError error={error} isExpanded={isExpanded} />
)}
</div>
- </div>
+ </ConversationScroller>
{pendingToolCalls.length > 0 ? (
<div
className={cn(
@@ -935,36 +1444,6 @@ export function InAppAgentWindow(props: InAppAgentWindowProps) {
</div>
</div>
) : null}
- <div
- aria-hidden={isAssistantTurnInProgress}
- className={cn(
- "flex shrink-0 flex-col overflow-hidden transition-[max-height,opacity] duration-200 ease-out motion-reduce:transition-none",
- isAssistantTurnInProgress
- ? "max-h-0 opacity-0"
- : "max-h-40 opacity-100",
- )}
- >
- <div className="mb-2 px-2">
- <div
- className={cn(
- "flex w-full flex-col gap-1.5",
- isExpanded && "mx-auto max-w-3xl",
- )}
- >
- <p
- className={cn(
- "border-border bg-muted/60 text-foreground flex w-full items-center gap-1 rounded-lg border px-2 py-1",
- isExpanded ? "text-sm" : "text-xs",
- )}
- >
- <Info aria-hidden="true" className="size-3 shrink-0" />
- <span className="min-w-0 truncate" title={screenContextNotice}>
- {screenContextNotice}
- </span>
- </p>
- </div>
- </div>
- </div>
{backgroundNotice ? (
<div className="shrink-0 px-2 pb-2">
<div className={cn(isExpanded && "mx-auto max-w-3xl")}>
@@ -1039,9 +1518,8 @@ export function InAppAgentWindow(props: InAppAgentWindowProps) {
>
<form
className={cn(
- "relative flex w-full items-end gap-2 rounded-md",
- isExpanded &&
- "mx-auto max-w-3xl cursor-text flex-col border focus-within:ring focus-within:ring-blue-500 focus-within:ring-offset-0",
+ "border-input bg-background focus-within:ring-muted-foreground relative flex w-full cursor-text flex-col rounded-xl border shadow-xs focus-within:ring-2",
+ isExpanded && "mx-auto max-w-3xl",
)}
onClick={() => {
if (isExpanded) {
@@ -1072,21 +1550,26 @@ export function InAppAgentWindow(props: InAppAgentWindowProps) {
}}
disabled={isComposerDisabled}
aria-label="Message the assistant"
- placeholder="Let me know what I can do for you..."
+ placeholder={
+ hasSettledAssistantReply
+ ? "Reply..."
+ : "Let me know what I can do for you..."
+ }
rows={1}
- className={cn(
- "bg-background placeholder:text-foreground-tertiary w-full flex-1 resize-none overflow-y-auto rounded-md text-sm leading-5 disabled:cursor-not-allowed disabled:opacity-60",
- isExpanded
- ? "max-h-40 min-h-14 border-none ring-0"
- : "border-input max-h-40 min-h-8 px-3 py-1",
- )}
+ className="placeholder:text-foreground-tertiary max-h-40 min-h-9 w-full resize-none overflow-y-auto border-none bg-transparent px-3 pt-2 text-sm leading-5 shadow-none ring-0 outline-none disabled:cursor-not-allowed disabled:opacity-60"
/>
- {!isExpanded &&
- (canStopRun ? (
+ <div className="flex min-h-9 w-full items-center justify-between gap-2 px-2 pb-1.5">
+ <p className="text-muted-foreground flex min-w-0 items-center gap-1 text-xs">
+ <Info aria-hidden="true" className="size-3 shrink-0" />
+ <span className="truncate" title={screenContextNotice}>
+ {screenContextNotice}
+ </span>
+ </p>
+ {canStopRun ? (
<Button
type="button"
- size="icon"
- className="h-8 w-8 rounded-md border"
+ size="icon-sm"
+ className="w-8 shrink-0 rounded-full"
aria-label={isCancellingRun ? "Stopping run" : "Stop run"}
variant="outline"
disabled={isCancellingRun}
@@ -1094,53 +1577,20 @@ export function InAppAgentWindow(props: InAppAgentWindowProps) {
executionStop?.onStop();
}}
>
- <Square className="h-3 w-3" />
+ <Square className="size-3" />
</Button>
) : (
<Button
type="submit"
- size="icon"
- className="h-8 w-8 rounded-md border"
+ size="icon-sm"
+ className="w-8 shrink-0 rounded-full"
aria-label="Send message"
- variant="outline"
disabled={isSubmitDisabled || !input.trim()}
>
- <SendHorizontal className="h-4 w-4" />
+ <SendHorizontal className="size-3" />
</Button>
- ))}
-
- {isExpanded && (
- <div className="flex w-full justify-end p-1">
- {canStopRun ? (
- <Button
- type="button"
- variant="outline"
- className="h-8 w-fit rounded-md px-3"
- aria-label={isCancellingRun ? "Stopping run" : "Stop run"}
- disabled={isCancellingRun}
- onClick={(e) => {
- e.stopPropagation();
- executionStop?.onStop();
- }}
- >
- {isCancellingRun ? "Stopping" : "Stop"}
- <Square className="ml-2 h-3 w-3" />
- </Button>
- ) : (
- <Button
- type="submit"
- className="h-8 w-fit rounded-md px-3"
- aria-label="Send message"
- disabled={isSubmitDisabled || !input.trim()}
- onClick={(e) => {
- e.stopPropagation();
- }}
- >
- Send <SendHorizontal className="ml-2 h-4 w-4" />
- </Button>
- )}
- </div>
- )}
+ )}
+ </div>
</form>
</div>
</div>
diff --git a/web/src/features/in-app-agent/components/InAppAiAgentProvider.tsx b/web/src/features/in-app-agent/components/InAppAiAgentProvider.tsx
index d44bac23b7..16b0afa3b6 100644
--- a/web/src/features/in-app-agent/components/InAppAiAgentProvider.tsx
+++ b/web/src/features/in-app-agent/components/InAppAiAgentProvider.tsx
@@ -559,6 +559,9 @@ function InAppAiAgentProviderInner({
...message,
isLoading:
loadingEventIds.has(message.id) ||
+ (message.feedbackMessageId
+ ? loadingEventIds.has(message.feedbackMessageId)
+ : false) ||
(message.toolCalls?.some(
(toolCall) =>
toolCall.function.name !== IN_APP_AGENT_REDIRECT_TOOL_NAME &&
diff --git a/web/src/features/in-app-agent/components/utils/utils.clienttest.ts b/web/src/features/in-app-agent/components/utils/utils.clienttest.ts
index 5602e54b1c..b4b9dcfe70 100644
--- a/web/src/features/in-app-agent/components/utils/utils.clienttest.ts
+++ b/web/src/features/in-app-agent/components/utils/utils.clienttest.ts
@@ -1,7 +1,15 @@
import type { AgUiMessage } from "@langfuse/shared/in-app-agent";
+import { IN_APP_AGENT_REDIRECT_TOOL_NAME } from "@langfuse/shared/in-app-agent";
+import {
+ IN_APP_AGENT_LANGFUSE_MCP_TOOL_NAMES,
+ IN_APP_AGENT_SANDBOX_TOOL_NAMES,
+} from "@langfuse/shared/in-app-agent/server/tools";
import {
extractLangfuseDocsSources,
getInAppAgentToolDisplayName,
+ getInAppAgentActivityProgressLabel,
+ getInAppAgentToolProgressLabel,
+ getInAppAgentToolProgressLabelResolution,
getDrawerMessages,
getInAppAgentError,
isInAppAgentRateLimited,
@@ -19,6 +27,171 @@ describe("getInAppAgentToolDisplayName", () => {
});
});
+const KNOWN_IN_APP_AGENT_PROGRESS_TOOLS = [
+ ...[...IN_APP_AGENT_LANGFUSE_MCP_TOOL_NAMES].map(
+ (toolName) => `langfuse_${toolName}`,
+ ),
+ ...IN_APP_AGENT_SANDBOX_TOOL_NAMES,
+ IN_APP_AGENT_REDIRECT_TOOL_NAME,
+ "langfuseDocs_search",
+ "langfuseDocs_fetch",
+ "skill",
+];
+
+// Auto-parsed headlines that a human has reviewed. Add a new tool here only
+// after reading the suggested label in the failure output. Prefer an override
+// in utils.ts when the auto label is wrong.
+const ACCEPTED_AUTO_IN_APP_AGENT_PROGRESS_LABELS: Record<string, string> = {
+ langfuse_createAnnotationQueue: "Creating annotation queue",
+ langfuse_createComment: "Creating comment",
+ langfuse_createDashboard: "Creating dashboard",
+ langfuse_createDatasetRunItem: "Creating dataset run item",
+ langfuse_createEvaluationRule: "Creating evaluation rule",
+ langfuse_createModel: "Creating model",
+ langfuse_createScore: "Creating score",
+ langfuse_createScoreConfig: "Creating score config",
+ langfuse_deleteAnnotationQueueItem: "Deleting annotation queue item",
+ langfuse_deleteDashboard: "Deleting dashboard",
+ langfuse_deleteDatasetItem: "Deleting dataset item",
+ langfuse_deleteDatasetRun: "Deleting dataset run",
+ langfuse_deleteEvaluationRule: "Deleting evaluation rule",
+ langfuse_deleteEvaluator: "Deleting evaluator",
+ langfuse_deleteModel: "Deleting model",
+ langfuse_deleteScoreConfig: "Deleting score config",
+ langfuse_getAlert: "Inspecting alert",
+ langfuse_getAnnotationQueue: "Inspecting annotation queue",
+ langfuse_getAnnotationQueueItem: "Inspecting annotation queue item",
+ langfuse_getComment: "Inspecting comment",
+ langfuse_getDashboard: "Inspecting dashboard",
+ langfuse_getDataset: "Inspecting dataset",
+ langfuse_getDatasetItem: "Inspecting dataset item",
+ langfuse_getDatasetRun: "Inspecting dataset run",
+ langfuse_getEvaluationRule: "Inspecting evaluation rule",
+ langfuse_getEvaluator: "Inspecting evaluator",
+ langfuse_getMedia: "Inspecting media",
+ langfuse_getModel: "Inspecting model",
+ langfuse_getObservation: "Inspecting observation",
+ langfuse_getPrompt: "Inspecting prompt",
+ langfuse_getScore: "Inspecting score",
+ langfuse_getScoreConfig: "Inspecting score config",
+ langfuse_listAlerts: "Browsing alerts",
+ langfuse_listAnnotationQueueItems: "Browsing annotation queue items",
+ langfuse_listAnnotationQueues: "Browsing annotation queues",
+ langfuse_listComments: "Browsing comments",
+ langfuse_listDashboards: "Browsing dashboards",
+ langfuse_listDatasetItems: "Browsing dataset items",
+ langfuse_listDatasetRunItems: "Browsing dataset run items",
+ langfuse_listDatasetRuns: "Browsing dataset runs",
+ langfuse_listDatasets: "Browsing datasets",
+ langfuse_listEvaluationRules: "Browsing evaluation rules",
+ langfuse_listEvaluators: "Browsing evaluators",
+ langfuse_listExperimentItems: "Browsing experiment items",
+ langfuse_listExperiments: "Browsing experiments",
+ langfuse_listModels: "Browsing models",
+ langfuse_listObservations: "Browsing observations",
+ langfuse_listPrompts: "Browsing prompts",
+ langfuse_listScoreConfigs: "Browsing score configs",
+ langfuse_listScores: "Browsing scores",
+ langfuse_updateAnnotationQueueItem: "Updating annotation queue item",
+ langfuse_updateDashboard: "Updating dashboard",
+ langfuse_updateEvaluationRule: "Updating evaluation rule",
+ langfuse_updateScoreConfig: "Updating score config",
+ langfuse_upsertDataset: "Saving dataset",
+ langfuse_upsertDatasetItem: "Saving dataset item",
+ langfuse_upsertEvaluator: "Saving evaluator",
+};
+
+describe("getInAppAgentToolProgressLabel", () => {
+ it("requires a reviewed headline for every known tool", () => {
+ const unresolved: string[] = [];
+ const staleAccepted: string[] = [];
+
+ for (const toolName of KNOWN_IN_APP_AGENT_PROGRESS_TOOLS) {
+ const resolution = getInAppAgentToolProgressLabelResolution(toolName);
+ const acceptedAutoLabel =
+ ACCEPTED_AUTO_IN_APP_AGENT_PROGRESS_LABELS[toolName];
+
+ if (resolution.source !== "auto") {
+ if (acceptedAutoLabel !== undefined) {
+ staleAccepted.push(
+ `${toolName} is ${resolution.source} ("${resolution.label}") but still listed as accepted auto`,
+ );
+ }
+ continue;
+ }
+
+ if (acceptedAutoLabel === undefined) {
+ unresolved.push(`${toolName} → "${resolution.label}"`);
+ continue;
+ }
+
+ if (acceptedAutoLabel !== resolution.label) {
+ unresolved.push(
+ `${toolName} auto label is now "${resolution.label}" (accepted "${acceptedAutoLabel}")`,
+ );
+ }
+ }
+
+ const extraAccepted = Object.keys(
+ ACCEPTED_AUTO_IN_APP_AGENT_PROGRESS_LABELS,
+ ).filter(
+ (toolName) => !KNOWN_IN_APP_AGENT_PROGRESS_TOOLS.includes(toolName),
+ );
+
+ expect({
+ unresolved,
+ staleAccepted,
+ extraAccepted,
+ }).toEqual({
+ unresolved: [],
+ staleAccepted: [],
+ extraAccepted: [],
+ });
+ });
+
+ it.each([
+ ["docs_search", "Reading Langfuse docs"],
+ ["skill", "Learning skill"],
+ ["customThing", "Custom Thing"],
+ ])("labels %s as %s", (toolName, expected) => {
+ expect(getInAppAgentToolProgressLabel(toolName)).toBe(expected);
+ });
+
+ it("keeps one headline for consecutive tools that share a noun", () => {
+ expect(
+ getInAppAgentActivityProgressLabel([
+ "langfuse_getObservation",
+ "langfuse_listObservations",
+ ]),
+ ).toBe("Looking at observations");
+ expect(
+ getInAppAgentActivityProgressLabel(["langfuse_listObservations"]),
+ ).toBe("Browsing observations");
+ expect(
+ getInAppAgentActivityProgressLabel([
+ "langfuseDocs_search",
+ "langfuse_listObservations",
+ ]),
+ ).toBe("Browsing observations");
+ expect(
+ getInAppAgentActivityProgressLabel([
+ "langfuse_getObservationFilterValues",
+ "langfuse_getObservationFilterValues",
+ ]),
+ ).toBe("Looking up observation filters");
+ });
+
+ it("keeps a readable headline for every known tool", () => {
+ const awkward = /Looking at up\b|Propose Redirect/;
+ const labels = KNOWN_IN_APP_AGENT_PROGRESS_TOOLS.flatMap((toolName) => [
+ `${toolName} → ${getInAppAgentActivityProgressLabel([toolName])}`,
+ `${toolName}×2 → ${getInAppAgentActivityProgressLabel([toolName, toolName])}`,
+ ]);
+
+ expect(labels.filter((label) => awkward.test(label))).toEqual([]);
+ });
+});
+
describe("getInAppAgentError", () => {
const now = new Date("2026-07-08T20:00:54.997Z").getTime();
const rateLimitError = {
@@ -444,7 +617,35 @@ describe("getDrawerMessages", () => {
expect(mappedMessages).toHaveLength(2);
});
- it("collapses consecutive reasoning messages into one thought block", () => {
+ it("forwards text timestamps and streaming state to the message renderer", () => {
+ const mappedMessages = getDrawerMessages({
+ error: null,
+ isRunning: true,
+ messages: [
+ {
+ id: "assistant-1",
+ role: "assistant",
+ content: "Partial answer",
+ isLoading: true,
+ timestamp: 1_723_111_753_000,
+ },
+ ] satisfies InAppAiAgentMessage[],
+ });
+
+ expect(mappedMessages).toMatchObject([
+ {
+ id: "assistant-1",
+ timestamp: 1_723_111_753_000,
+ content: {
+ type: "text",
+ text: "Partial answer",
+ isStreaming: true,
+ },
+ },
+ ]);
+ });
+
+ it("keeps consecutive reasoning messages as separate thought blocks", () => {
const mappedMessages = getDrawerMessages({
error: null,
isRunning: true,
@@ -483,12 +684,30 @@ describe("getDrawerMessages", () => {
role: "assistant",
content: {
type: "reasoning",
- text: "Checking recent traces before querying metrics.\n\nThe p95 spike lines up with one endpoint.",
+ text: "Checking recent traces before querying metrics.",
+ isStreaming: false,
+ },
+ },
+ {
+ id: "reasoning-2",
+ role: "assistant",
+ content: {
+ type: "reasoning",
+ text: "The p95 spike lines up with one endpoint.",
+ isStreaming: false,
+ },
+ },
+ {
+ id: "reasoning-3",
+ role: "assistant",
+ content: {
+ type: "reasoning",
+ text: "",
isStreaming: true,
},
},
]);
- expect(mappedMessages).toHaveLength(2);
+ expect(mappedMessages).toHaveLength(4);
});
it("marks reasoning complete when a run stops before assistant text arrives", () => {
@@ -583,7 +802,6 @@ describe("getDrawerMessages", () => {
id: "tools-assistant-1",
content: {
type: "toolGroup",
- isLoading: true,
},
},
]);
@@ -666,7 +884,7 @@ describe("getDrawerMessages", () => {
},
{
id: "tools-assistant-1",
- content: { type: "toolGroup", isLoading: false },
+ content: { type: "toolGroup" },
},
{
id: "reasoning-2",
@@ -677,7 +895,7 @@ describe("getDrawerMessages", () => {
},
{
id: "tools-assistant-2",
- content: { type: "toolGroup", isLoading: true },
+ content: { type: "toolGroup" },
},
]);
});
@@ -743,14 +961,14 @@ describe("getDrawerMessages", () => {
{ id: "user-1" },
{
id: "tools-assistant-1",
- content: { type: "toolGroup", isLoading: true },
+ content: { type: "toolGroup" },
},
]);
expect(completedMessages).toMatchObject([
{ id: "user-1" },
{
id: "tools-assistant-1",
- content: { type: "toolGroup", isLoading: false },
+ content: { type: "toolGroup" },
},
]);
});
@@ -995,7 +1213,6 @@ describe("getDrawerMessages", () => {
role: "assistant",
content: {
type: "toolGroup",
- isLoading: false,
tools: [
{
type: "tool",
diff --git a/web/src/features/in-app-agent/components/utils/utils.ts b/web/src/features/in-app-agent/components/utils/utils.ts
index 4d084bd321..c85625d699 100644
--- a/web/src/features/in-app-agent/components/utils/utils.ts
+++ b/web/src/features/in-app-agent/components/utils/utils.ts
@@ -25,6 +25,7 @@ const InAppAiAgentMessageSchema = AgUiMessageSchema.and(
z.object({
isLoading: z.boolean().optional(),
feedbackMessageId: z.string().optional(),
+ timestamp: z.number().optional(),
}),
);
@@ -47,6 +48,210 @@ export function getInAppAgentToolDisplayName(toolName: string): string {
return toolName.replace(/^(?:docs_|langfuseDocs_|langfuse_)/, "");
}
+const IN_APP_AGENT_TOOL_PROGRESS_LABEL_OVERRIDES: Record<string, string> = {
+ addDashboardPlacement: "Adding widget to dashboard",
+ bash: "Running command",
+ createAnnotationQueueAssignment: "Assigning annotation queue",
+ createAnnotationQueueItem: "Adding to annotation queue",
+ createChatPrompt: "Creating chat prompt",
+ createDashboardWidget: "Creating widget",
+ createTextPrompt: "Creating text prompt",
+ deleteAnnotationQueueAssignment: "Unassigning annotation queue",
+ deleteDashboardPlacement: "Removing widget from dashboard",
+ deleteDashboardWidget: "Deleting widget",
+ edit: "Editing file",
+ getDashboardWidget: "Inspecting widget",
+ getHealth: "Checking health",
+ getMetricsSchema: "Checking metrics",
+ getObservationFieldSchema: "Checking observation fields",
+ getObservationFilterSchema: "Checking observation filters",
+ getObservationFilterValues: "Looking up observation filters",
+ getPromptUnresolved: "Inspecting prompt",
+ listDashboardWidgets: "Browsing widgets",
+ proposeRedirect: "Opening page",
+ queryMetrics: "Checking metrics",
+ read: "Reading file",
+ submitFeedback: "Submitting user feedback",
+ updateDashboardPlacement: "Moving widget",
+ updateDashboardWidget: "Updating widget",
+ updatePromptLabels: "Updating prompt labels",
+ write: "Writing file",
+};
+
+const IN_APP_AGENT_TOOL_PROGRESS_VERBS: Record<string, string> = {
+ add: "Adding",
+ create: "Creating",
+ delete: "Deleting",
+ get: "Inspecting",
+ list: "Browsing",
+ query: "Checking",
+ submit: "Submitting",
+ update: "Updating",
+ upsert: "Saving",
+};
+
+const IN_APP_AGENT_TOOL_PROGRESS_NOUNS: Array<[string, string]> = [
+ ["dashboard widgets", "widgets"],
+ ["dashboard widget", "widget"],
+ ["dashboard placement", "widget on dashboard"],
+ ["prompt unresolved", "prompt"],
+];
+
+// Verbs that read rather than change something: a run of them is one act of
+// looking, so a streak collapses to "Looking at <noun>".
+const IN_APP_AGENT_TOOL_PROGRESS_LOOKING_VERBS = new Set([
+ "Inspecting",
+ "Browsing",
+ "Checking",
+]);
+
+function splitInAppAgentToolNameWords(toolName: string): string[] {
+ return toolName
+ .replaceAll("_", " ")
+ .replace(/([a-z0-9])([A-Z])/g, "$1 $2")
+ .replace(/([A-Z]+)([A-Z][a-z])/g, "$1 $2")
+ .split(/\s+/)
+ .filter(Boolean);
+}
+
+function isInAppAgentDocsToolName(toolName: string) {
+ return /^(?:docs_|langfuseDocs_)/.test(toolName);
+}
+
+function isInAppAgentSkillToolName(toolName: string) {
+ const strippedName = getInAppAgentToolDisplayName(toolName);
+ return /(?:^|[-_])skill(?:$|[-_A-Z])/i.test(strippedName);
+}
+
+/**
+ * Read a camelCase tool name as an English verb and the thing it acts on, e.g.
+ * `listDashboardWidgets` → `{ verb: "Browsing", noun: "widgets" }`. The single
+ * source for both the auto headline and the streak-collapsing noun, so the two
+ * cannot drift apart. Null when the leading word is not a verb we translate;
+ * `noun` is empty for a bare verb such as `getHealth`'s sibling `bash`.
+ */
+function deriveInAppAgentToolVerbAndNoun(
+ strippedName: string,
+): { verb: string; noun: string } | null {
+ const [firstWord, ...rest] = splitInAppAgentToolNameWords(strippedName);
+ const verb = firstWord
+ ? IN_APP_AGENT_TOOL_PROGRESS_VERBS[firstWord.toLowerCase()]
+ : undefined;
+
+ if (!verb) {
+ return null;
+ }
+
+ const noun = rest.map((word) => word.toLowerCase()).join(" ");
+ const substitution = IN_APP_AGENT_TOOL_PROGRESS_NOUNS.find(
+ ([from]) => noun === from,
+ );
+
+ return { verb, noun: substitution?.[1] ?? noun };
+}
+
+function getAutoInAppAgentToolProgressLabel(strippedName: string): string {
+ const derived = deriveInAppAgentToolVerbAndNoun(strippedName);
+ if (derived) {
+ return derived.noun ? `${derived.verb} ${derived.noun}` : derived.verb;
+ }
+
+ const words = splitInAppAgentToolNameWords(strippedName);
+ if (words.length === 0) {
+ return strippedName;
+ }
+
+ return words
+ .map((word) => word.charAt(0).toUpperCase() + word.slice(1).toLowerCase())
+ .join(" ");
+}
+
+export function getInAppAgentToolProgressLabelResolution(toolName: string): {
+ source: "docs" | "skill" | "override" | "auto";
+ label: string;
+} {
+ if (isInAppAgentDocsToolName(toolName)) {
+ return { source: "docs", label: "Reading Langfuse docs" };
+ }
+
+ if (isInAppAgentSkillToolName(toolName)) {
+ return { source: "skill", label: "Learning skill" };
+ }
+
+ const strippedName = getInAppAgentToolDisplayName(toolName);
+ const override = IN_APP_AGENT_TOOL_PROGRESS_LABEL_OVERRIDES[strippedName];
+ if (override) {
+ return { source: "override", label: override };
+ }
+
+ return {
+ source: "auto",
+ label: getAutoInAppAgentToolProgressLabel(strippedName),
+ };
+}
+
+export function getInAppAgentToolProgressLabel(toolName: string): string {
+ return getInAppAgentToolProgressLabelResolution(toolName).label;
+}
+
+/** Singularised noun two tools must share to count as one act of looking. */
+function getInAppAgentToolProgressNounKey(toolName: string): string | null {
+ if (
+ isInAppAgentDocsToolName(toolName) ||
+ isInAppAgentSkillToolName(toolName)
+ ) {
+ return null;
+ }
+
+ const noun = deriveInAppAgentToolVerbAndNoun(
+ getInAppAgentToolDisplayName(toolName),
+ )?.noun;
+
+ return noun ? noun.replace(/s\b/g, "") : null;
+}
+
+export function getInAppAgentActivityProgressLabel(
+ toolNames: string[],
+): string {
+ const latestToolName = toolNames.at(-1);
+ if (!latestToolName) {
+ return "Working…";
+ }
+
+ const resolution = getInAppAgentToolProgressLabelResolution(latestToolName);
+ const latestNounKey = getInAppAgentToolProgressNounKey(latestToolName);
+ if (!latestNounKey) {
+ return resolution.label;
+ }
+
+ let streak = 1;
+ for (let index = toolNames.length - 2; index >= 0; index--) {
+ const toolName = toolNames[index];
+ if (
+ !toolName ||
+ getInAppAgentToolProgressNounKey(toolName) !== latestNounKey
+ ) {
+ break;
+ }
+ streak++;
+ }
+
+ // A hand-written override already reads as a sentence, so only the derived
+ // verb-and-noun form collapses.
+ const derived =
+ streak >= 2 && resolution.source === "auto"
+ ? deriveInAppAgentToolVerbAndNoun(
+ getInAppAgentToolDisplayName(latestToolName),
+ )
+ : null;
+
+ return derived &&
+ IN_APP_AGENT_TOOL_PROGRESS_LOOKING_VERBS.has(derived.verb) &&
+ derived.noun
+ ? `Looking at ${derived.noun}`
+ : resolution.label;
+}
+
const InAppAgentToolRejectionErrorSchema = z.object({
code: z.literal(IN_APP_AGENT_TOOL_REJECTION_ERROR_CODE),
message: z.string(),
@@ -227,7 +432,6 @@ export function getDrawerMessages({
const mappedMessages: InAppAgentWindowMessage[] = [];
let pendingTools: InAppAgentToolCallContent[] = [];
let pendingToolGroupId: string | null = null;
- let pendingToolGroupIsLoading = false;
let pendingSources: InAppAgentMessageSource[] = [];
const flushPendingTools = () => {
if (pendingTools.length === 0) {
@@ -240,12 +444,10 @@ export function getDrawerMessages({
content: {
type: "toolGroup",
tools: pendingTools,
- isLoading: pendingToolGroupIsLoading,
},
});
pendingTools = [];
pendingToolGroupId = null;
- pendingToolGroupIsLoading = false;
};
parsedMessages.forEach((message, index) => {
@@ -306,30 +508,9 @@ export function getDrawerMessages({
return;
}
- const previousMessage = mappedMessages[mappedMessages.length - 1];
-
- // Hydrated history flattens the text and tool content that separated
- // thinking phases live, leaving them adjacent; collapse them into one
- // block, mirroring how consecutive tool calls collapse into one group.
- if (
- previousMessage?.role === "assistant" &&
- previousMessage.content.type === "reasoning"
- ) {
- mappedMessages[mappedMessages.length - 1] = {
- ...previousMessage,
- content: {
- ...previousMessage.content,
- text: [previousMessage.content.text, message.content]
- .filter((text) => text.trim())
- .join("\n\n"),
- isStreaming,
- },
- };
- return;
- }
-
mappedMessages.push({
id: message.id,
+ timestamp: message.timestamp,
role,
content: {
type: "reasoning",
@@ -430,11 +611,6 @@ export function getDrawerMessages({
) ?? [])
: [];
const docsSources = extractLangfuseDocsSources(toolContent);
- const isToolGroupLoading =
- isRunning &&
- !error &&
- message.role === "assistant" &&
- message.isLoading === true;
if (role === "assistant" && toolContent.length > 0 && !text.trim()) {
if (docsSources.length > 0) {
@@ -443,7 +619,6 @@ export function getDrawerMessages({
pendingToolGroupId ??= `tools-${message.id}`;
pendingTools.push(...toolContent);
- pendingToolGroupIsLoading ||= isToolGroupLoading;
return;
}
@@ -462,6 +637,7 @@ export function getDrawerMessages({
mappedMessages.push({
id: message.id,
+ timestamp: message.timestamp,
...(message.role === "assistant" && message.runId
? { runId: message.runId }
: {}),
@@ -472,6 +648,9 @@ export function getDrawerMessages({
content: {
type: "text",
text,
+ ...(message.role === "assistant" && message.isLoading
+ ? { isStreaming: true }
+ : {}),
...(sources.length > 0 ? { sources } : {}),
...(message.role === "assistant" && message.feedback
? { feedback: message.feedback }
@@ -495,7 +674,6 @@ export function getDrawerMessages({
content: {
type: "toolGroup",
tools: toolContent,
- isLoading: isToolGroupLoading,
},
});
}
@@ -533,46 +711,6 @@ export function getDrawerMessages({
});
}
- const latestUserMessageIndex = mappedMessages.findLastIndex(
- (message) => message.role === "user",
- );
- const latestAssistantMessageIndex = mappedMessages.findLastIndex(
- (message, index) =>
- index > latestUserMessageIndex && message.role === "assistant",
- );
- const latestAssistantMessage = mappedMessages[latestAssistantMessageIndex];
-
- // Insert an optimistic loading message.
- if (
- isRunning &&
- !error &&
- latestUserMessageIndex >= 0 &&
- latestAssistantMessage?.content.type !== "text" &&
- latestAssistantMessage?.content.type !== "loading" &&
- latestAssistantMessage?.content.type !== "reasoning" &&
- latestAssistantMessage?.content.type !== "redirectAction"
- ) {
- if (latestAssistantMessage?.content.type === "toolGroup") {
- return mappedMessages;
- }
-
- const hasAssistantAnswer = mappedMessages.some(
- (message) =>
- message.role === "assistant" && message.content.type === "text",
- );
-
- return [
- ...mappedMessages,
- {
- id: hasAssistantAnswer ? "loading" : "connecting",
- role: "assistant",
- content: hasAssistantAnswer
- ? { type: "loading" }
- : { type: "loading", label: "Connecting..." },
- } satisfies InAppAgentWindowMessage,
- ];
- }
-
return mappedMessages;
}
diff --git a/web/src/features/in-app-agent/context.clienttest.ts b/web/src/features/in-app-agent/context.clienttest.ts
index 74a9b432c2..be5f9992f8 100644
--- a/web/src/features/in-app-agent/context.clienttest.ts
+++ b/web/src/features/in-app-agent/context.clienttest.ts
@@ -35,17 +35,17 @@ describe("getInAppAgentScreenContextDescription", () => {
{
name: "trace list without filters",
url: "/project/project-1/traces",
- expected: { type: "trace-list", hasAppliedFilters: false },
+ expected: { type: "trace-list" },
},
{
name: "trace list with structured filters",
url: "/project/project-1/traces?filter=level%3BstringOptions%3B%3Bany+of%3BERROR",
- expected: { type: "trace-list", hasAppliedFilters: true },
+ expected: { type: "trace-list" },
},
{
name: "observations list with full-text search",
url: "/project/project-1/observations?search=checkout&searchType=id",
- expected: { type: "observations-list", hasAppliedFilters: true },
+ expected: { type: "observations-list" },
},
{
name: "trace setup page",
@@ -88,12 +88,12 @@ describe("getInAppAgentScreenContextDescription", () => {
{
name: "sessions list",
url: "/project/project-1/sessions",
- expected: { type: "sessions-list", hasAppliedFilters: false },
+ expected: { type: "sessions-list" },
},
{
name: "prompts list",
url: "/project/project-1/prompts?filter=type%3BstringOptions%3B%3D%3Bchat",
- expected: { type: "prompts-list", hasAppliedFilters: true },
+ expected: { type: "prompts-list" },
},
{
name: "dataset detail",
@@ -113,7 +113,7 @@ describe("getInAppAgentScreenContextDescription", () => {
{
name: "datasets list",
url: "/project/project-1/datasets",
- expected: { type: "datasets-list", hasAppliedFilters: false },
+ expected: { type: "datasets-list" },
},
{
name: "unknown project page",
diff --git a/web/src/features/in-app-agent/context.ts b/web/src/features/in-app-agent/context.ts
index 1d04a5be97..98ebd6a49d 100644
--- a/web/src/features/in-app-agent/context.ts
+++ b/web/src/features/in-app-agent/context.ts
@@ -23,11 +23,11 @@ export type InAppAgentScreenContextDescription =
| { type: "dataset" }
| { type: "datasetItem" }
| { type: "experimentRun" }
- | { type: "trace-list"; hasAppliedFilters: boolean }
- | { type: "observations-list"; hasAppliedFilters: boolean }
- | { type: "sessions-list"; hasAppliedFilters: boolean }
- | { type: "prompts-list"; hasAppliedFilters: boolean }
- | { type: "datasets-list"; hasAppliedFilters: boolean };
+ | { type: "trace-list" }
+ | { type: "observations-list" }
+ | { type: "sessions-list" }
+ | { type: "prompts-list" }
+ | { type: "datasets-list" };
const CURRENT_URL_CONTEXT_DESCRIPTION = "current_url";
const QUICK_ACTION_KEY_CONTEXT_DESCRIPTION = "quick_action_key";
@@ -65,9 +65,6 @@ export function getInAppAgentScreenContextDescription(
const detailId = routeSegments[1];
const peekId = parsedUrl.searchParams.get("peek");
const observationId = parsedUrl.searchParams.get("observation");
- const hasAppliedFilters = ["filter", "search"].some((parameter) =>
- Boolean(parsedUrl.searchParams.get(parameter)?.trim()),
- );
if (
(section === "traces" && observationId && (detailId || peekId)) ||
@@ -81,11 +78,11 @@ export function getInAppAgentScreenContextDescription(
}
if (section === "traces" && !detailId) {
- return { type: "trace-list", hasAppliedFilters };
+ return { type: "trace-list" };
}
if (section === "observations" && !detailId) {
- return { type: "observations-list", hasAppliedFilters };
+ return { type: "observations-list" };
}
if (section === "prompts") {
@@ -123,7 +120,7 @@ export function getInAppAgentScreenContextDescription(
}
if (promptPathSegments.length === 0) {
- return { type: "prompts-list", hasAppliedFilters };
+ return { type: "prompts-list" };
}
}
@@ -132,7 +129,7 @@ export function getInAppAgentScreenContextDescription(
}
if (section === "sessions" && !detailId) {
- return { type: "sessions-list", hasAppliedFilters };
+ return { type: "sessions-list" };
}
if (section === "datasets" && detailId) {
@@ -148,7 +145,7 @@ export function getInAppAgentScreenContextDescription(
}
if (section === "datasets" && !detailId) {
- return { type: "datasets-list", hasAppliedFilters };
+ return { type: "datasets-list" };
}
return { type: "page" };
diff --git a/web/src/features/in-app-agent/components/inAppAgentDisplay.clienttest.ts b/web/src/features/in-app-agent/lib/display.clienttest.ts
similarity index 79%
rename from web/src/features/in-app-agent/components/inAppAgentDisplay.clienttest.ts
rename to web/src/features/in-app-agent/lib/display.clienttest.ts
index e0219f79b6..f729c40427 100644
--- a/web/src/features/in-app-agent/components/inAppAgentDisplay.clienttest.ts
+++ b/web/src/features/in-app-agent/lib/display.clienttest.ts
@@ -1,6 +1,7 @@
import type { AgUiMessage } from "@langfuse/shared/in-app-agent";
import {
createInAppAgentDisplayState,
+ deserializeInAppAgentDisplayState,
projectInAppAgentMessagesForDisplay,
recordInAppAgentMessagesForDisplay,
recordInAppAgentToolCallForDisplay,
@@ -21,6 +22,64 @@ const assistantToolMessage = {
} satisfies AgUiMessage;
describe("in-app agent display projection", () => {
+ it("keeps first-observed timestamps for canonical and projected text blocks", () => {
+ const initialMessages = [
+ { id: "user", role: "user", content: "Investigate this" },
+ { id: "assistant", role: "assistant", content: "First answer." },
+ ] satisfies AgUiMessage[];
+ const messagesWithReasoning = [
+ ...initialMessages,
+ {
+ id: "reasoning",
+ role: "reasoning",
+ content: "Another thought.",
+ },
+ ] satisfies AgUiMessage[];
+ const finalMessages = messagesWithReasoning.map((message) =>
+ message.id === "assistant"
+ ? { ...message, content: "First answer. Second answer." }
+ : message,
+ );
+ let displayState = createInAppAgentDisplayState();
+ displayState = recordInAppAgentMessagesForDisplay(
+ displayState,
+ initialMessages,
+ 1_000,
+ );
+ displayState = recordInAppAgentMessagesForDisplay(
+ displayState,
+ messagesWithReasoning,
+ 2_000,
+ );
+ displayState = recordInAppAgentMessagesForDisplay(
+ displayState,
+ finalMessages,
+ 3_000,
+ );
+
+ expect(
+ projectInAppAgentMessagesForDisplay(finalMessages, displayState).map(
+ (message) => ({ id: message.id, timestamp: message.timestamp }),
+ ),
+ ).toEqual([
+ { id: "user", timestamp: 1_000 },
+ { id: "assistant", timestamp: 1_000 },
+ { id: "reasoning", timestamp: 2_000 },
+ { id: "display-text-assistant-1", timestamp: 3_000 },
+ ]);
+ });
+
+ it("still hydrates a payload from the bundle that sent seen ids separately", () => {
+ const { messageTimestamps: _messageTimestamps, ...legacyState } = {
+ ...createInAppAgentDisplayState(),
+ seenMessageIds: ["assistant"],
+ };
+
+ expect(deserializeInAppAgentDisplayState(legacyState)).toEqual(
+ createInAppAgentDisplayState(),
+ );
+ });
+
it("keeps consecutive tool calls with the same parent together", () => {
const assistantMessage = {
id: "assistant-tools",
diff --git a/web/src/features/in-app-agent/lib/display.ts b/web/src/features/in-app-agent/lib/display.ts
index 4f0a2c7d09..97992ec31c 100644
--- a/web/src/features/in-app-agent/lib/display.ts
+++ b/web/src/features/in-app-agent/lib/display.ts
@@ -24,7 +24,9 @@ export type InAppAgentDisplayState = {
nativeToolCallParentMessageId: string | null;
latestNewMessageId: string | null;
nextOrder: number;
- seenMessageIds: ReadonlySet<string>;
+ /** Message id to when this client first saw it. Doubles as the processed-id
+ * ledger: a key exists exactly when the message has been recorded. */
+ messageTimestamps: Record<string, number>;
textByMessageId: Record<
string,
{
@@ -34,6 +36,7 @@ export type InAppAgentDisplayState = {
InAppAgentDisplayPlacement & {
id: string;
content: string;
+ timestamp?: number;
}
>;
}
@@ -43,6 +46,7 @@ export type InAppAgentDisplayState = {
type InAppAgentDisplayMessage = AgUiMessage & {
feedbackMessageId?: string;
+ timestamp?: number;
};
type InAppAgentToolCall = NonNullable<
@@ -55,9 +59,10 @@ const InAppAgentDisplayPlacementSchema = z.object({
});
/**
- * Wire form of {@link InAppAgentDisplayState}: identical except that the seen
- * message ids travel as an array. Validated rather than relying on the tRPC
- * transformer so the contract stays explicit and transport-independent.
+ * Wire form of {@link InAppAgentDisplayState}. Validated rather than relying on
+ * the tRPC transformer so the contract stays explicit and transport-independent.
+ * Nothing stores it: the server rebuilds the state from the event log on every
+ * conversation fetch, so the only skew to survive is a rolling deploy.
*
* The sidecar duplicates assistant text, and `publishedContent` is always
* `nativeContent` plus every segment's content joined in order, so it could be
@@ -71,7 +76,11 @@ export const SerializedInAppAgentDisplayStateSchema = z.object({
nativeToolCallParentMessageId: z.string().nullable(),
latestNewMessageId: z.string().nullable(),
nextOrder: z.number(),
- seenMessageIds: z.array(z.string()),
+ // Superseded by messageTimestamps' keys. Still written so a tab holding the
+ // previous bundle keeps parsing this payload across a rolling deploy;
+ // ignored on read and removable a release later.
+ seenMessageIds: z.array(z.string()).optional(),
+ messageTimestamps: z.record(z.string(), z.number()).default({}),
textByMessageId: z.record(
z.string(),
z.object({
@@ -81,6 +90,7 @@ export const SerializedInAppAgentDisplayStateSchema = z.object({
InAppAgentDisplayPlacementSchema.extend({
id: z.string(),
content: z.string(),
+ timestamp: z.number().optional(),
}),
),
}),
@@ -98,7 +108,7 @@ export type SerializedInAppAgentDisplayState = z.infer<
export function serializeInAppAgentDisplayState(
state: InAppAgentDisplayState,
): SerializedInAppAgentDisplayState {
- return { ...state, seenMessageIds: [...state.seenMessageIds] };
+ return { ...state, seenMessageIds: Object.keys(state.messageTimestamps) };
}
export function deserializeInAppAgentDisplayState(
@@ -109,10 +119,8 @@ export function deserializeInAppAgentDisplayState(
return createInAppAgentDisplayState();
}
- return {
- ...parsed.data,
- seenMessageIds: new Set(parsed.data.seenMessageIds),
- };
+ const { seenMessageIds: _legacySeenMessageIds, ...state } = parsed.data;
+ return state;
}
export function createInAppAgentDisplayState(): InAppAgentDisplayState {
@@ -121,7 +129,7 @@ export function createInAppAgentDisplayState(): InAppAgentDisplayState {
nativeToolCallParentMessageId: null,
latestNewMessageId: null,
nextOrder: 0,
- seenMessageIds: new Set(),
+ messageTimestamps: {},
textByMessageId: {},
toolCallPlacements: {},
};
@@ -130,8 +138,9 @@ export function createInAppAgentDisplayState(): InAppAgentDisplayState {
export function recordInAppAgentMessagesForDisplay(
state: InAppAgentDisplayState,
messages: readonly AgUiMessage[],
+ observedAt = Date.now(),
): InAppAgentDisplayState {
- const seenMessageIds = new Set(state.seenMessageIds);
+ const messageTimestamps = { ...state.messageTimestamps };
const textByMessageId = { ...state.textByMessageId };
let latestNewMessageId = state.latestNewMessageId;
let latestPlacement = state.latestPlacement;
@@ -139,11 +148,11 @@ export function recordInAppAgentMessagesForDisplay(
let nextOrder = state.nextOrder;
for (const message of messages) {
- if (seenMessageIds.has(message.id)) {
+ if (message.id in messageTimestamps) {
continue;
}
- seenMessageIds.add(message.id);
+ messageTimestamps[message.id] = observedAt;
latestNewMessageId = message.id;
latestPlacement = null;
nativeToolCallParentMessageId = null;
@@ -218,6 +227,7 @@ export function recordInAppAgentMessagesForDisplay(
...placement,
id: `display-text-${message.id}-${textState.segments.length + 1}`,
content: appendedContent,
+ timestamp: observedAt,
};
nextOrder += 1;
latestPlacement = placement;
@@ -234,7 +244,7 @@ export function recordInAppAgentMessagesForDisplay(
nativeToolCallParentMessageId,
latestNewMessageId,
nextOrder,
- seenMessageIds,
+ messageTimestamps,
textByMessageId,
};
}
@@ -349,6 +359,7 @@ export function projectInAppAgentMessagesForDisplay(
id: segment.id,
role: "assistant",
content: segment.content,
+ timestamp: segment.timestamp,
...(sourceMessage?.role === "assistant"
? {
runId: sourceMessage.runId,
@@ -368,10 +379,11 @@ export function projectInAppAgentMessagesForDisplay(
firstToolCallMessageIds.get(toolCall.id) !== messageId;
return messages.flatMap<InAppAgentDisplayMessage>((message) => {
- const projectedMessage =
+ const projectedMessage: InAppAgentDisplayMessage =
message.role === "assistant"
? {
...message,
+ timestamp: state.messageTimestamps[message.id],
content:
state.textByMessageId[message.id]?.nativeContent ??
message.content,
@@ -388,7 +400,12 @@ export function projectInAppAgentMessagesForDisplay(
return !placement || !messageIds.has(placement.anchorMessageId);
}),
}
- : message;
+ : // Reasoning is its own role here and starts most turns, so it carries
+ // the timestamp the "Worked for" duration measures from.
+ {
+ ...message,
+ timestamp: state.messageTimestamps[message.id],
+ };
const isEmptyDuplicateToolCallMessage =
message.role === "assistant" &&
projectedMessage.role === "assistant" &&
diff --git a/web/src/features/in-app-agent/server/conversationSnapshot.ts b/web/src/features/in-app-agent/server/conversationSnapshot.ts
index df3bf88e82..7d075efe29 100644
--- a/web/src/features/in-app-agent/server/conversationSnapshot.ts
+++ b/web/src/features/in-app-agent/server/conversationSnapshot.ts
@@ -29,7 +29,7 @@ export function getConversationSnapshotFromEvents(
let displayState = createInAppAgentDisplayState();
const deferredToolCallParents = new Map<string, string | undefined>();
- for (const { event, runId } of events) {
+ for (const { event, runId, createdAt } of events) {
if (event.type === EventType.TOOL_CALL_START) {
const toolCallId = getEventString(event, "toolCallId");
if (toolCallId) {
@@ -55,6 +55,7 @@ export function getConversationSnapshotFromEvents(
displayState = recordInAppAgentMessagesForDisplay(
displayState,
accumulator.getMessages(),
+ createdAt.getTime(),
);
}
diff --git a/web/src/hooks/useCopyToClipboard.clienttest.ts b/web/src/hooks/useCopyToClipboard.clienttest.ts
index 1226553bf1..36531eec79 100644
--- a/web/src/hooks/useCopyToClipboard.clienttest.ts
+++ b/web/src/hooks/useCopyToClipboard.clienttest.ts
@@ -55,6 +55,34 @@ describe("useCopyToClipboard", () => {
expect(result.current.isCopied).toBe(true);
});
+ it("falls back to plain text when the browser rejects a rich clipboard write", async () => {
+ const write = vi.fn().mockRejectedValue(new Error("unsupported type"));
+ const writeText = vi.fn().mockResolvedValue(undefined);
+ Object.defineProperty(navigator, "clipboard", {
+ configurable: true,
+ value: { write, writeText },
+ });
+ vi.stubGlobal("ClipboardItem", class ClipboardItemStub {});
+
+ const { result } = renderHook(() =>
+ useCopyToClipboard({ successDuration: 500 }),
+ );
+
+ await act(async () => {
+ await result.current.copyRich({
+ text: "The reranker is the bottleneck.",
+ html: "<p>The reranker is the bottleneck.</p>",
+ });
+ });
+
+ // The success state is only honest because the plain-text write landed.
+ expect(write).toHaveBeenCalledOnce();
+ expect(writeText).toHaveBeenCalledWith("The reranker is the bottleneck.");
+ expect(result.current.isCopied).toBe(true);
+
+ vi.unstubAllGlobals();
+ });
+
it("keeps the active copied state across rerenders and preserves the original timeout", async () => {
const { result, rerender } = renderHook(
({ successDuration }) => useCopyToClipboard({ successDuration }),
diff --git a/web/src/hooks/useCopyToClipboard.ts b/web/src/hooks/useCopyToClipboard.ts
index ab359544bf..61abfc234f 100644
--- a/web/src/hooks/useCopyToClipboard.ts
+++ b/web/src/hooks/useCopyToClipboard.ts
@@ -1,5 +1,8 @@
import { useEffect, useRef, useState } from "react";
-import { copyTextToClipboard } from "@/src/utils/clipboard";
+import {
+ copyRichTextToClipboard,
+ copyTextToClipboard,
+} from "@/src/utils/clipboard";
/**
* Copies text to the clipboard and exposes a temporary success state for UI feedback.
@@ -20,10 +23,10 @@ export function useCopyToClipboard({
};
}, []);
- const copy = async (text: string) => {
+ const runCopy = async (copyToClipboard: () => Promise<void> | void) => {
setIsCopied(true);
try {
- await copyTextToClipboard(text);
+ await copyToClipboard();
} finally {
if (timeoutRef.current) {
clearTimeout(timeoutRef.current);
@@ -35,5 +38,10 @@ export function useCopyToClipboard({
}
};
- return { copy, isCopied };
+ const copy = async (text: string) => runCopy(() => copyTextToClipboard(text));
+
+ const copyRich = async (content: { text: string; html: string }) =>
+ runCopy(() => copyRichTextToClipboard(content));
+
+ return { copy, copyRich, isCopied };
}
diff --git a/web/src/utils/clipboard.ts b/web/src/utils/clipboard.ts
index be4f96eecd..0d59ad5fe4 100644
--- a/web/src/utils/clipboard.ts
+++ b/web/src/utils/clipboard.ts
@@ -45,6 +45,40 @@ export const copyTextToClipboard = async (text: string) => {
return _unsafeNonSecureCopyToClipboard(text);
};
+/**
+ * Copy rich content while keeping a useful plain-text representation.
+ *
+ * Exposing `navigator.clipboard.write` is not the same as accepting a
+ * multi-MIME write: browsers reject unsupported types, and a rejection here
+ * would otherwise leave the clipboard untouched while the caller reports
+ * success. Fall back to the plain-text path so the copy still lands.
+ */
+export const copyRichTextToClipboard = async ({
+ text,
+ html,
+}: {
+ text: string;
+ html: string;
+}) => {
+ if (
+ typeof navigator.clipboard?.write === "function" &&
+ typeof ClipboardItem !== "undefined"
+ ) {
+ try {
+ return await navigator.clipboard.write([
+ new ClipboardItem({
+ "text/plain": new Blob([text], { type: "text/plain" }),
+ "text/html": new Blob([html], { type: "text/html" }),
+ }),
+ ]);
+ } catch {
+ // Fall through to the plain-text write below.
+ }
+ }
+
+ return copyTextToClipboard(text);
+};
+
/**
* Read text from the clipboard. Returns null when the async Clipboard API is
* unavailable (non-secure context) or the user/browser denied the read —