Skip to content

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

feat(in-app-agent): Allow users to start new conversations (or switch) while another one is running (#15851)

Lysander Kiesel41113c4dd7de
diff --git a/web/src/__tests__/server/in-app-agent-background-run.servertest.ts b/web/src/__tests__/server/in-app-agent-background-run.servertest.ts
index 4e886fb6e7..d4f1b808b3 100644
--- a/web/src/__tests__/server/in-app-agent-background-run.servertest.ts
+++ b/web/src/__tests__/server/in-app-agent-background-run.servertest.ts
@@ -244,12 +244,13 @@ describe("in-app agent background runs", () => {
const { caller, projectId, userId } = await createCaller();
const conversation = await createConversation({ projectId, userId });
- const { runId } = await caller.startRun({
+ const { conversationId, runId } = await caller.startRun({
projectId,
conversationId: conversation.id,
message: "why did these traces fail?",
context: [{ description: "current_url", value: "/project/x/traces" }],
});
+ expect(conversationId).toBe(conversation.id);
const run = await prisma.inAppAgentRun.findFirstOrThrow({
where: { id: runId, projectId },
diff --git a/web/src/features/in-app-agent/components/ControlledInAppAgentWindow.tsx b/web/src/features/in-app-agent/components/ControlledInAppAgentWindow.tsx
index 0be9a49ae8..2e1ab47e11 100644
--- a/web/src/features/in-app-agent/components/ControlledInAppAgentWindow.tsx
+++ b/web/src/features/in-app-agent/components/ControlledInAppAgentWindow.tsx
@@ -179,6 +179,7 @@ export function ControlledInAppAgentWindow(
isHeaderDragHandleEnabled={props.isHeaderDragHandleEnabled}
isExpanded={props.isExpanded}
isConversationInteractionDisabled={isConversationInteractionDisabled}
+ isSelectedConversationHydrating={isSelectedConversationHydrating}
disablePendingToolApprovalActions={selectedConversationIsWriteLocked}
messages={drawerMessages}
quickActionContext={quickActionContext}
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 d589d3479e..a06be3ad03 100644
--- a/web/src/features/in-app-agent/components/InAppAgentExecution.clienttest.tsx
+++ b/web/src/features/in-app-agent/components/InAppAgentExecution.clienttest.tsx
@@ -120,7 +120,10 @@ vi.mock("@/src/utils/api", () => ({
useInfiniteQuery: () => providerMocks.listQuery,
},
getConversation: {
- useQuery: () => providerMocks.conversationQuery,
+ useQuery: (...args: unknown[]) => {
+ providerMocks.getConversation(...args);
+ return providerMocks.conversationQuery;
+ },
},
deleteConversation: {
useMutation: () => ({ mutateAsync: vi.fn() }),
@@ -162,6 +165,53 @@ function ReopenAssistantButton() {
);
}
+function ConcurrentConversationProbe({
+ conversationId,
+}: {
+ conversationId: string | null;
+}) {
+ const { isSubmitting, selectConversation, submit } = useInAppAiAgent();
+
+ return (
+ <>
+ <p>{isSubmitting ? "Submitting" : "Ready"}</p>
+ <button
+ type="button"
+ onClick={() => {
+ submit("First question").catch(() => undefined);
+ }}
+ >
+ Submit
+ </button>
+ <button
+ type="button"
+ onClick={() => {
+ selectConversation(null);
+ }}
+ >
+ Leave conversation
+ </button>
+ <button
+ type="button"
+ disabled={!conversationId}
+ onClick={() => {
+ selectConversation(conversationId);
+ }}
+ >
+ Return to conversation
+ </button>
+ </>
+ );
+}
+
+function providerProbe(conversationId: string | null) {
+ return (
+ <InAppAiAgentProvider defaultOpen>
+ <ConcurrentConversationProbe conversationId={conversationId} />
+ </InAppAiAgentProvider>
+ );
+}
+
function renderExecutionUi({
defaultOpen = true,
includeReopenButton = false,
@@ -786,8 +836,14 @@ describe("in-app agent execution", () => {
},
};
providerMocks.startRun
- .mockResolvedValueOnce({ runId: "run-1" })
- .mockResolvedValueOnce({ runId: "run-2" });
+ .mockResolvedValueOnce({
+ conversationId: "conversation-1",
+ runId: "run-1",
+ })
+ .mockResolvedValueOnce({
+ conversationId: "conversation-1",
+ runId: "run-2",
+ });
providerMocks.cancelRun.mockResolvedValue({
cancelledImmediately: false,
status: InAppAgentRunStatus.RUNNING,
@@ -927,3 +983,207 @@ describe("in-app agent execution", () => {
});
});
});
+
+describe("in-app agent concurrent conversations", () => {
+ beforeEach(() => {
+ vi.stubGlobal(
+ "matchMedia",
+ vi.fn(() => ({
+ matches: true,
+ addEventListener: vi.fn(),
+ removeEventListener: vi.fn(),
+ })),
+ );
+ });
+
+ afterEach(() => {
+ vi.restoreAllMocks();
+ vi.clearAllMocks();
+ vi.unstubAllGlobals();
+ providerMocks.conversationQuery.data = undefined;
+ providerMocks.conversationQuery.error = null;
+ providerMocks.conversationQuery.isLoading = false;
+ window.sessionStorage.clear();
+ window.localStorage.clear();
+ });
+
+ it("starts a new conversation while another one is still running", async () => {
+ providerMocks.conversationQuery.data = {
+ conversation: { id: "conversation-1", isWriteLocked: false },
+ messages: [],
+ eventCursor: 3,
+ latestRun: {
+ id: "run-1",
+ status: InAppAgentRunStatus.RUNNING,
+ errorCode: null,
+ cancelRequested: false,
+ },
+ pendingToolApprovals: [],
+ };
+ providerMocks.startRun.mockImplementation(
+ async (input: { conversationId: string }) => ({
+ conversationId: input.conversationId,
+ runId: "run-2",
+ }),
+ );
+ window.sessionStorage.setItem(
+ "langfuse:in-app-ai-agent-selected-conversation:project-1",
+ JSON.stringify("conversation-1"),
+ );
+
+ renderExecutionUi();
+
+ const newConversationButton = await screen.findByRole("button", {
+ name: "Start new conversation",
+ });
+ expect(newConversationButton).toBeEnabled();
+ fireEvent.click(newConversationButton);
+
+ const input = await screen.findByRole("textbox", {
+ name: "Message the assistant",
+ });
+ fireEvent.change(input, { target: { value: "Second question" } });
+ const form = input.closest("form");
+ if (!form) {
+ throw new Error("Expected the assistant composer to render a form");
+ }
+ fireEvent.submit(form);
+
+ await waitFor(() => {
+ expect(providerMocks.startRun).toHaveBeenCalledWith(
+ expect.objectContaining({
+ projectId: "project-1",
+ message: "Second question",
+ }),
+ );
+ });
+ expect(providerMocks.cancelRun).not.toHaveBeenCalled();
+ });
+
+ it("waits for startRun before fetching a new conversation", async () => {
+ providerMocks.getConversation.mockClear();
+ providerMocks.startRun.mockReset();
+
+ let resolveStartRun:
+ | ((result: { runId: string; conversationId: string }) => void)
+ | undefined;
+ providerMocks.startRun.mockImplementation(
+ () =>
+ new Promise((resolve) => {
+ resolveStartRun = resolve;
+ }),
+ );
+
+ renderExecutionUi();
+
+ const input = screen.getByRole("textbox", {
+ name: "Message the assistant",
+ });
+ fireEvent.change(input, { target: { value: "First question" } });
+ fireEvent.click(screen.getByRole("button", { name: "Send message" }));
+
+ await waitFor(() => {
+ expect(providerMocks.startRun).toHaveBeenCalledOnce();
+ });
+ const conversationId = providerMocks.startRun.mock.calls[0]?.[0]
+ ?.conversationId as string;
+
+ expect(providerMocks.getConversation).toHaveBeenLastCalledWith(
+ expect.objectContaining({ conversationId }),
+ expect.objectContaining({ enabled: false }),
+ );
+
+ await act(async () => {
+ resolveStartRun?.({ runId: "run-1", conversationId });
+ });
+
+ await waitFor(() => {
+ expect(providerMocks.getConversation).toHaveBeenLastCalledWith(
+ expect.objectContaining({ conversationId }),
+ expect.objectContaining({ enabled: true }),
+ );
+ });
+ });
+
+ it("releases a conversation submit lock when switching away", async () => {
+ providerMocks.startRun.mockImplementation(
+ async (input: { conversationId: string }) => ({
+ conversationId: input.conversationId,
+ runId: "run-1",
+ }),
+ );
+ vi.spyOn(globalThis, "fetch").mockImplementation(
+ (_input, init) =>
+ new Promise((_resolve, reject) => {
+ init?.signal?.addEventListener("abort", () => {
+ reject(new DOMException("Aborted", "AbortError"));
+ });
+ }),
+ );
+
+ const { rerender } = render(providerProbe(null));
+ fireEvent.click(screen.getByRole("button", { name: "Submit" }));
+
+ await waitFor(() => {
+ expect(providerMocks.startRun).toHaveBeenCalledOnce();
+ });
+ const conversationId = providerMocks.startRun.mock.calls[0]?.[0]
+ ?.conversationId as string;
+ rerender(providerProbe(conversationId));
+
+ fireEvent.click(screen.getByRole("button", { name: "Leave conversation" }));
+ fireEvent.click(
+ screen.getByRole("button", { name: "Return to conversation" }),
+ );
+
+ expect(screen.getByText("Ready")).toBeInTheDocument();
+ });
+
+ it("keeps a failed new conversation provisional and preserves its messages on retry", async () => {
+ providerMocks.getConversation.mockClear();
+ providerMocks.startRun
+ .mockReset()
+ .mockRejectedValueOnce(new Error("Start failed"))
+ .mockImplementationOnce(() => new Promise<never>(() => undefined));
+
+ renderExecutionUi();
+
+ const input = screen.getByRole("textbox", {
+ name: "Message the assistant",
+ });
+ fireEvent.change(input, { target: { value: "First question" } });
+ fireEvent.click(screen.getByRole("button", { name: "Send message" }));
+
+ await waitFor(() => {
+ expect(providerMocks.startRun).toHaveBeenCalledOnce();
+ });
+ const conversationId = providerMocks.startRun.mock.calls[0]?.[0]
+ ?.conversationId as string;
+
+ await waitFor(() => {
+ expect(providerMocks.getConversation).toHaveBeenLastCalledWith(
+ expect.objectContaining({ conversationId }),
+ expect.objectContaining({ enabled: false }),
+ );
+ });
+ expect(screen.getByText("First question")).toBeVisible();
+
+ fireEvent.change(input, { target: { value: "Retry question" } });
+ fireEvent.click(screen.getByRole("button", { name: "Send message" }));
+
+ await waitFor(() => {
+ expect(providerMocks.startRun).toHaveBeenCalledTimes(2);
+ });
+ expect(screen.getByText("First question")).toBeVisible();
+ expect(screen.getByText("Retry question")).toBeVisible();
+ expect(providerMocks.capture).toHaveBeenCalledWith(
+ "in_app_agent:new_chat_started",
+ { entryPoint: "chat" },
+ );
+ expect(
+ providerMocks.capture.mock.calls.filter(
+ ([event]) => event === "in_app_agent:new_chat_started",
+ ),
+ ).toHaveLength(1);
+ });
+});
diff --git a/web/src/features/in-app-agent/components/InAppAgentWidgetComposer.clienttest.tsx b/web/src/features/in-app-agent/components/InAppAgentWidgetComposer.clienttest.tsx
index 9043072b2d..31988b445e 100644
--- a/web/src/features/in-app-agent/components/InAppAgentWidgetComposer.clienttest.tsx
+++ b/web/src/features/in-app-agent/components/InAppAgentWidgetComposer.clienttest.tsx
@@ -2,21 +2,23 @@ import { fireEvent, render, screen, waitFor } from "@testing-library/react";
import { InAppAgentWidgetComposer } from "./InAppAgentWidgetComposer";
-const openAssistant = vi.fn().mockReturnValue(true);
-const submit = vi.fn().mockResolvedValue(true);
+const agentContext = vi.hoisted(() => ({
+ isAvailable: true,
+ isRunning: false,
+ isSubmitting: false,
+ openAssistant: vi.fn().mockReturnValue(true),
+ submit: vi.fn().mockResolvedValue(true),
+}));
+const { openAssistant, submit } = agentContext;
vi.mock("./InAppAiAgentProvider", () => ({
- useInAppAiAgent: () => ({
- isAvailable: true,
- isRunning: false,
- isSubmitting: false,
- openAssistant,
- submit,
- }),
+ useInAppAiAgent: () => agentContext,
}));
describe("InAppAgentWidgetComposer", () => {
beforeEach(() => {
+ agentContext.isRunning = false;
+ agentContext.isSubmitting = false;
openAssistant.mockClear().mockReturnValue(true);
submit.mockClear().mockResolvedValue(true);
});
@@ -54,6 +56,28 @@ describe("InAppAgentWidgetComposer", () => {
).toBeDisabled();
});
+ it("starts a new widget conversation while the selected conversation is busy", async () => {
+ agentContext.isRunning = true;
+ agentContext.isSubmitting = true;
+ const onSubmitted = vi.fn();
+ render(<InAppAgentWidgetComposer onSubmitted={onSubmitted} />);
+
+ fireEvent.change(screen.getByLabelText("Describe the widget you want"), {
+ target: { value: "Show p95 latency" },
+ });
+ const submitButton = screen.getByRole("button", {
+ name: "Add with Langfuse Assistant",
+ });
+
+ expect(submitButton).toBeEnabled();
+ fireEvent.click(submitButton);
+
+ await waitFor(() => {
+ expect(submit).toHaveBeenCalledOnce();
+ });
+ expect(onSubmitted).toHaveBeenCalledOnce();
+ });
+
it("keeps the picker open and preserves the request when submit does not start", async () => {
submit.mockResolvedValue(false);
const onSubmitted = vi.fn();
diff --git a/web/src/features/in-app-agent/components/InAppAgentWidgetComposer.tsx b/web/src/features/in-app-agent/components/InAppAgentWidgetComposer.tsx
index 765f141886..6fd9f12210 100644
--- a/web/src/features/in-app-agent/components/InAppAgentWidgetComposer.tsx
+++ b/web/src/features/in-app-agent/components/InAppAgentWidgetComposer.tsx
@@ -26,8 +26,7 @@ export function InAppAgentWidgetComposer({
}: {
onSubmitted: () => void;
}) {
- const { isAvailable, isRunning, isSubmitting, openAssistant, submit } =
- useInAppAiAgent();
+ const { isAvailable, openAssistant, submit } = useInAppAiAgent();
const [request, setRequest] = useState("");
if (!isAvailable) {
@@ -38,7 +37,7 @@ export function InAppAgentWidgetComposer({
event.preventDefault();
const trimmedRequest = request.trim();
- if (!trimmedRequest || isRunning || isSubmitting) {
+ if (!trimmedRequest) {
return;
}
@@ -102,7 +101,7 @@ export function InAppAgentWidgetComposer({
className="h-8 w-8 shrink-0 rounded-md border"
variant="outline"
aria-label="Add with Langfuse Assistant"
- disabled={!request.trim() || isRunning || isSubmitting}
+ disabled={!request.trim()}
>
<SendHorizontal className="h-4 w-4" />
</Button>
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 399c07c376..eec074f762 100644
--- a/web/src/features/in-app-agent/components/InAppAgentWindow.clienttest.tsx
+++ b/web/src/features/in-app-agent/components/InAppAgentWindow.clienttest.tsx
@@ -82,6 +82,7 @@ function windowElement(
isAssistantTurnInProgress: false,
isExpanded: false,
isConversationInteractionDisabled: false,
+ isSelectedConversationHydrating: false,
isLoadingMoreConversations: false,
messages: [],
onApproveToolCall: vi.fn(),
@@ -204,31 +205,13 @@ describe("InAppAgentWindow quick actions", () => {
});
describe("ControlledInAppAgentWindow composer", () => {
- it("keeps a draft editable but prevents submitting it while rate limited", () => {
- const onSubmit = vi.fn().mockResolvedValue(true);
- render(
- windowElement({
- error: { type: "rate_limit", retryAt: Date.now() + 60_000 },
- onSubmit,
- }),
- );
-
- const input = screen.getByRole("textbox", {
- name: "Message the assistant",
- });
- fireEvent.change(input, { target: { value: "Follow up" } });
-
- expect(input).toHaveValue("Follow up");
- expect(input).toBeEnabled();
- expect(screen.getByRole("button", { name: "Send message" })).toBeDisabled();
-
- const form = input.closest("form");
- if (!form) {
- throw new Error("Expected the assistant composer to render a form");
- }
-
- fireEvent.submit(form);
- expect(onSubmit).not.toHaveBeenCalled();
+ beforeEach(() => {
+ controlledAgent.value.error = null;
+ controlledAgent.value.isRunning = true;
+ controlledAgent.value.isSelectedConversationHydrating = false;
+ controlledAgent.value.isSubmitting = false;
+ controlledAgent.value.pendingToolApprovals = [];
+ controlledAgent.value.selectedConversationIsWriteLocked = false;
});
it("keeps a draft editable but prevents submitting it while an assistant turn is active", () => {
@@ -265,7 +248,7 @@ describe("ControlledInAppAgentWindow composer", () => {
expect(onSubmit).not.toHaveBeenCalled();
});
- it("keeps navigation disabled while an approval is pending", () => {
+ it("blocks another turn here but still lets you leave", () => {
controlledAgent.value.isRunning = false;
controlledAgent.value.pendingToolApprovals = [{ id: "approval-1" }];
controlledAgent.value.submit = vi.fn();
@@ -287,10 +270,36 @@ describe("ControlledInAppAgentWindow composer", () => {
expect(screen.getByRole("button", { name: "Send message" })).toBeDisabled();
expect(
screen.getByRole("button", { name: "Start new conversation" }),
- ).toBeDisabled();
+ ).toBeEnabled();
expect(
screen.getByRole("button", { name: "Conversation history" }),
+ ).toBeEnabled();
+ });
+
+ it("lets you leave a read-only conversation", () => {
+ controlledAgent.value.isRunning = false;
+ controlledAgent.value.selectedConversationIsWriteLocked = true;
+
+ render(
+ <TooltipProvider>
+ <ControlledInAppAgentWindow
+ isExpanded={false}
+ onClose={vi.fn()}
+ onDeleteConversation={vi.fn()}
+ onExpandedChange={vi.fn()}
+ />
+ </TooltipProvider>,
+ );
+
+ expect(
+ screen.getByRole("textbox", { name: "Message the assistant" }),
).toBeDisabled();
+ expect(
+ screen.getByRole("button", { name: "Start new conversation" }),
+ ).toBeEnabled();
+ expect(
+ screen.getByRole("button", { name: "Conversation history" }),
+ ).toBeEnabled();
});
});
@@ -329,28 +338,3 @@ describe("ControlledInAppAgentWindow stop", () => {
expect(finishAnimation).toHaveBeenCalledOnce();
});
});
-
-describe("InAppAgentWindow focus", () => {
- it("refocuses the composer when an active turn completes", () => {
- const { rerender } = render(
- <>
- <button type="button">Other control</button>
- {windowElement({ isAssistantTurnInProgress: true })}
- </>,
- );
-
- screen.getByRole("button", { name: "Other control" }).focus();
- expect(screen.getByRole("button", { name: "Other control" })).toHaveFocus();
-
- rerender(
- <>
- <button type="button">Other control</button>
- {windowElement({ isAssistantTurnInProgress: false })}
- </>,
- );
-
- expect(
- screen.getByRole("textbox", { name: "Message the assistant" }),
- ).toHaveFocus();
- });
-});
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 3f82ab7a58..e3cdd68326 100644
--- a/web/src/features/in-app-agent/components/InAppAgentWindow.stories.tsx
+++ b/web/src/features/in-app-agent/components/InAppAgentWindow.stories.tsx
@@ -597,6 +597,7 @@ const meta = preview.meta({
executionUi: { notice: null, stop: null },
isExpanded: false,
isConversationInteractionDisabled: false,
+ isSelectedConversationHydrating: false,
conversations,
hasMoreConversations: false,
isLoadingMoreConversations: false,
@@ -625,7 +626,6 @@ const meta = preview.meta({
export const ToolApprovalRequired = meta.story({
args: {
isAssistantTurnInProgress: true,
- isConversationInteractionDisabled: true,
selectedConversationId: "conversation-1",
messages: [
{
@@ -1110,6 +1110,35 @@ export const BackgroundRunStops = meta.story({
},
});
+export const LoadingConversation = meta.story({
+ name: "(Test) Loading Conversation",
+ args: {
+ isConversationInteractionDisabled: true,
+ messages: [],
+ selectedConversationId: "conversation-1",
+ isSelectedConversationHydrating: true,
+ },
+ play: async ({ canvasElement }: { canvasElement: HTMLElement }) => {
+ const canvas = within(canvasElement);
+
+ await expect(
+ canvas.queryByText("Welcome to the Langfuse Assistant"),
+ ).not.toBeInTheDocument();
+ await expect(
+ canvas.queryByRole("button", { name: /^Create a prompt/ }),
+ ).not.toBeInTheDocument();
+ await expect(
+ canvas.getByRole("button", { name: "Start new conversation" }),
+ ).toBeEnabled();
+ await expect(
+ canvas.getByRole("button", { name: "Conversation history" }),
+ ).toBeEnabled();
+ await expect(
+ canvas.getByRole("textbox", { name: "Message the assistant" }),
+ ).toBeDisabled();
+ },
+});
+
export const RateLimited = meta.story({
name: "(Test) Rate Limited",
args: {
@@ -1148,17 +1177,25 @@ export const RateLimited = meta.story({
/>
);
},
- play: async ({ canvasElement }: { canvasElement: HTMLElement }) => {
+ play: async ({ args, canvasElement }) => {
const canvas = within(canvasElement);
const alert = canvas.getByRole("alert");
+ const textarea = canvas.getByRole("textbox", {
+ name: "Message the assistant",
+ });
await expect(alert).toHaveTextContent(
"You've reached the assistant request limit",
);
await expect(alert).toHaveTextContent("Try again in about");
+ await expect(textarea).toBeEnabled();
+ await userEvent.type(textarea, "Follow up");
+ await expect(textarea).toHaveValue("Follow up");
await expect(
- canvas.getByRole("textbox", { name: "Message the assistant" }),
- ).toBeEnabled();
+ canvas.getByRole("button", { name: "Send message" }),
+ ).toBeDisabled();
+ await userEvent.keyboard("{Enter}");
+ await expect(args.onSubmit).not.toHaveBeenCalled();
await expect(
canvas.getByRole("button", { name: "Approve" }),
).toBeDisabled();
@@ -1172,10 +1209,10 @@ export const RateLimited = meta.story({
).toBeDisabled();
await expect(
canvas.getByRole("button", { name: "Start new conversation" }),
- ).toBeDisabled();
+ ).toBeEnabled();
await expect(
canvas.getByRole("button", { name: "Conversation history" }),
- ).toBeDisabled();
+ ).toBeEnabled();
},
});
diff --git a/web/src/features/in-app-agent/components/InAppAgentWindow.tsx b/web/src/features/in-app-agent/components/InAppAgentWindow.tsx
index 585f16e686..ae3c05dcfe 100644
--- a/web/src/features/in-app-agent/components/InAppAgentWindow.tsx
+++ b/web/src/features/in-app-agent/components/InAppAgentWindow.tsx
@@ -274,6 +274,8 @@ export type InAppAgentWindowProps = {
isHeaderDragHandleEnabled?: boolean;
isExpanded: boolean;
isConversationInteractionDisabled: boolean;
+ /** Distinguishes a loading transcript from an empty conversation. */
+ isSelectedConversationHydrating: boolean;
isLoadingMoreConversations: boolean;
messages: InAppAgentWindowMessage[];
onExpandedChange: (isExpanded: boolean) => void;
@@ -378,6 +380,7 @@ export function InAppAgentWindow(props: InAppAgentWindowProps) {
isExpanded,
isConversationInteractionDisabled,
isLoadingMoreConversations,
+ isSelectedConversationHydrating,
messages,
onDeleteConversation,
onExpandedChange,
@@ -560,9 +563,6 @@ export function InAppAgentWindow(props: InAppAgentWindowProps) {
size="icon"
className="size-6 shrink-0"
onClick={onNewConversation}
- disabled={
- isConversationInteractionDisabled || isAssistantTurnInProgress
- }
aria-label="Start new conversation"
>
<Plus className="size-3" />
@@ -588,10 +588,6 @@ export function InAppAgentWindow(props: InAppAgentWindowProps) {
variant="ghost"
size="icon"
className="size-6 shrink-0"
- disabled={
- isConversationInteractionDisabled ||
- isAssistantTurnInProgress
- }
aria-label="Conversation history"
>
<History className="size-3" />
@@ -742,7 +738,12 @@ export function InAppAgentWindow(props: InAppAgentWindowProps) {
isExpanded ? "px-0" : "px-3",
)}
>
- {messages.length === 0 ? (
+ {/* 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. */}
+ {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">
<div>
<BotMessageSquare className="text-muted-foreground mx-auto h-7 w-7" />
diff --git a/web/src/features/in-app-agent/components/InAppAiAgentProvider.tsx b/web/src/features/in-app-agent/components/InAppAiAgentProvider.tsx
index 8d7efb80ac..745014c71f 100644
--- a/web/src/features/in-app-agent/components/InAppAiAgentProvider.tsx
+++ b/web/src/features/in-app-agent/components/InAppAiAgentProvider.tsx
@@ -295,7 +295,13 @@ function InAppAiAgentProviderInner({
{},
);
const [isExpanded, setIsExpanded] = useState(false);
- const [isSubmitting, setIsSubmitting] = useState(false);
+ // Key by conversation so another conversation cannot release its submit lock.
+ const [submittingConversationId, setSubmittingConversationId] = useState<
+ string | null
+ >(null);
+ const [unpersistedConversationIds, setUnpersistedConversationIds] = useState<
+ ReadonlySet<string>
+ >(() => new Set());
const [loadingEventIds, setLoadingEventIds] = useState<ReadonlySet<string>>(
() => new Set(),
);
@@ -306,7 +312,7 @@ function InAppAiAgentProviderInner({
useState<BackgroundExecutionSession | null>(null);
const toolCallNamesRef = useRef(new Map<string, string>());
const handledToolCallIdsRef = useRef(new Set<string>());
- const submitInFlightRef = useRef(false);
+ const submitInFlightRef = useRef<string | null>(null);
const conversationListQuery =
api.inAppAgent.listConversations.useInfiniteQuery(
@@ -322,7 +328,11 @@ function InAppAiAgentProviderInner({
conversationId: _selectedConversationId ?? "",
},
{
- enabled: open && Boolean(_selectedConversationId) && !isSubmitting,
+ // Provisional ids become fetchable after `startRun` acknowledges persistence.
+ enabled:
+ open &&
+ Boolean(_selectedConversationId) &&
+ !unpersistedConversationIds.has(_selectedConversationId ?? ""),
},
);
const deleteConversationMutation =
@@ -337,6 +347,9 @@ function InAppAiAgentProviderInner({
const selectedConversationId = isSelectedConversationNotFound
? null
: _selectedConversationId;
+ const isSubmitting =
+ submittingConversationId !== null &&
+ submittingConversationId === selectedConversationId;
const conversations = useMemo(
() =>
@@ -692,9 +705,17 @@ function InAppAiAgentProviderInner({
[clearLoadingEvents, updateLoadingEvent, utils],
);
- const releaseSubmitLock = useCallback(() => {
- submitInFlightRef.current = false;
- setIsSubmitting(false);
+ // Release only the caller's lock; a newer conversation may own it.
+ const releaseSubmitLock = useCallback((conversationId: string | null) => {
+ if (
+ conversationId !== null &&
+ submitInFlightRef.current !== conversationId
+ ) {
+ return;
+ }
+
+ submitInFlightRef.current = null;
+ setSubmittingConversationId(null);
}, []);
const getOrCreateAgent = useCallback(
@@ -726,13 +747,24 @@ function InAppAiAgentProviderInner({
threadId: conversationId,
initialMessages,
cursor: initialCursor,
- startRun: (params) =>
- startRunMutation.mutateAsync({
+ startRun: async (params) => {
+ const started = await startRunMutation.mutateAsync({
projectId,
conversationId,
message: params.message,
context: [...params.context],
- }),
+ });
+ setUnpersistedConversationIds((current) => {
+ if (!current.has(started.conversationId)) {
+ return current;
+ }
+
+ const next = new Set(current);
+ next.delete(started.conversationId);
+ return next;
+ });
+ return started;
+ },
});
agentRef.current = agent;
@@ -812,7 +844,7 @@ function InAppAiAgentProviderInner({
projectId,
conversationId,
});
- releaseSubmitLock();
+ releaseSubmitLock(conversationId);
},
});
backgroundSessionRef.current = nextBackgroundSession;
@@ -867,17 +899,23 @@ function InAppAiAgentProviderInner({
const selectConversation = useCallback(
(conversationId: string | null) => {
- if (isRunning || conversationId === _selectedConversationId) {
+ if (conversationId === _selectedConversationId) {
return;
}
setError((currentError) =>
isInAppAgentRateLimited(currentError) ? currentError : null,
);
+ releaseSubmitLock(_selectedConversationId);
resetAgent();
setSelectedConversationId(conversationId);
},
- [_selectedConversationId, isRunning, resetAgent, setSelectedConversationId],
+ [
+ _selectedConversationId,
+ releaseSubmitLock,
+ resetAgent,
+ setSelectedConversationId,
+ ],
);
const deleteConversation = useCallback(
@@ -936,43 +974,50 @@ function InAppAiAgentProviderInner({
const submit = useCallback(
async (content: string, options?: InAppAgentSubmitOptions) => {
+ // A new conversation must not inherit the selected conversation's guards.
+ const startsNewConversation =
+ options?.newConversation === true || !selectedConversationId;
+ const conversationId = startsNewConversation
+ ? createInAppAgentConversationId()
+ : selectedConversationId;
+ const shouldRestorePersistedMessages =
+ !startsNewConversation &&
+ !unpersistedConversationIds.has(conversationId ?? "");
+
if (
!content ||
- isRunning ||
+ !conversationId ||
isInAppAgentRateLimited(error) ||
- (options?.newConversation !== true &&
- isSelectedConversationHydrating) ||
- submitInFlightRef.current
+ submitInFlightRef.current === conversationId
+ ) {
+ return false;
+ }
+
+ if (
+ !startsNewConversation &&
+ (isRunning || isSelectedConversationHydrating)
) {
return false;
}
- submitInFlightRef.current = true;
- setIsSubmitting(true);
+ if (!startsNewConversation && selectedConversationIsWriteLocked) {
+ setError({
+ type: "generic",
+ message: SANDBOX_CONVERSATION_WRITE_LOCK_MESSAGE,
+ });
+ return false;
+ }
+
+ submitInFlightRef.current = conversationId;
+ setSubmittingConversationId(conversationId);
setError(null);
let startedRun = false;
try {
- const isNewConversation =
- options?.newConversation === true || !selectedConversationId;
-
- if (!isNewConversation && selectedConversationIsWriteLocked) {
- setError({
- type: "generic",
- message: SANDBOX_CONVERSATION_WRITE_LOCK_MESSAGE,
- });
- return false;
- }
-
- const conversationId = isNewConversation
- ? createInAppAgentConversationId()
- : selectedConversationId;
-
- if (!conversationId) {
- return false;
- }
-
- if (isNewConversation) {
+ if (startsNewConversation) {
+ setUnpersistedConversationIds((current) =>
+ new Set(current).add(conversationId),
+ );
setSelectedConversationId(conversationId);
}
@@ -980,9 +1025,9 @@ function InAppAiAgentProviderInner({
conversationQuery.data?.conversation.id === conversationId
? conversationQuery.data.messages
: undefined;
- const initialMessages = isNewConversation
- ? []
- : (storedMessages?.filter(isAgentConversationMessage) ?? []);
+ const initialMessages = shouldRestorePersistedMessages
+ ? (storedMessages?.filter(isAgentConversationMessage) ?? [])
+ : [];
// TODO: Avoid hydrating the full history once the agent client can send
// only the latest user turn; the server rebuilds history from persistence.
const agent = getOrCreateAgent(conversationId, initialMessages);
@@ -992,7 +1037,7 @@ function InAppAiAgentProviderInner({
}
// Start background turns from the persisted transcript and cursor.
- if (!isNewConversation) {
+ if (shouldRestorePersistedMessages) {
agent.setMessages(initialMessages);
}
@@ -1004,7 +1049,7 @@ function InAppAiAgentProviderInner({
agent.addMessage(userMessage);
const entryPoint = options?.entryPoint ?? "chat";
- if (isNewConversation) {
+ if (startsNewConversation) {
capture("in_app_agent:new_chat_started", { entryPoint });
}
capture("in_app_agent:new_chat_turn", { entryPoint });
@@ -1029,7 +1074,7 @@ function InAppAiAgentProviderInner({
return false;
} finally {
if (!startedRun) {
- releaseSubmitLock();
+ releaseSubmitLock(conversationId);
}
}
},
@@ -1046,6 +1091,7 @@ function InAppAiAgentProviderInner({
selectedConversationId,
selectedConversationIsWriteLocked,
setSelectedConversationId,
+ unpersistedConversationIds,
],
);
@@ -1152,7 +1198,7 @@ function InAppAiAgentProviderInner({
setIsExpanded(false);
backgroundSessionRef.current?.detach();
- releaseSubmitLock();
+ releaseSubmitLock(selectedConversationId);
}
if (nextOpen && selectedConversationId) {
diff --git a/web/src/features/in-app-agent/server/backgroundRunService.ts b/web/src/features/in-app-agent/server/backgroundRunService.ts
index b9d9542f08..63e996533b 100644
--- a/web/src/features/in-app-agent/server/backgroundRunService.ts
+++ b/web/src/features/in-app-agent/server/backgroundRunService.ts
@@ -259,7 +259,7 @@ export async function startBackgroundRun(params: {
aiTelemetryEnabled: params.aiTelemetryEnabled,
});
- return { runId: run.id };
+ return { conversationId: conversation.id, runId: run.id };
}
/** Soft-delete a conversation and cancel its unsettled runs atomically. */