Skip to content

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

fix(in-app-agent): keep drafts editable while running (#15666)

Lysander Kiesel090e2d5d962c
diff --git a/web/src/features/in-app-agent/components/ControlledInAppAgentWindow.tsx b/web/src/features/in-app-agent/components/ControlledInAppAgentWindow.tsx
index a82b4890b4..c8edab0f13 100644
--- a/web/src/features/in-app-agent/components/ControlledInAppAgentWindow.tsx
+++ b/web/src/features/in-app-agent/components/ControlledInAppAgentWindow.tsx
@@ -71,13 +71,14 @@ export function ControlledInAppAgentWindow(
pendingToolApprovals,
shouldFlush: error !== null,
});
- const isInputDisabled =
+ const isConversationInteractionDisabled =
+ selectedConversationIsWriteLocked || isSelectedConversationHydrating;
+ const isAssistantTurnInProgress =
isRunning ||
isAnimating ||
isSubmitting ||
- selectedConversationIsWriteLocked ||
- isSelectedConversationHydrating ||
- pendingToolApprovals.length > 0;
+ pendingToolApprovals.length > 0 ||
+ displayedPendingToolApprovals.length > 0;
const displayError = selectedConversationIsWriteLocked
? ({
type: "generic",
@@ -123,12 +124,10 @@ export function ControlledInAppAgentWindow(
return (
<InAppAgentWindow
error={displayError}
- isAssistantTurnInProgress={
- isRunning || isAnimating || displayedPendingToolApprovals.length > 0
- }
+ isAssistantTurnInProgress={isAssistantTurnInProgress}
isHeaderDragHandleEnabled={props.isHeaderDragHandleEnabled}
isExpanded={props.isExpanded}
- isInputDisabled={isInputDisabled}
+ isConversationInteractionDisabled={isConversationInteractionDisabled}
disablePendingToolApprovalActions={selectedConversationIsWriteLocked}
messages={drawerMessages}
quickActionContext={quickActionContext}
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 3ae9815476..fe94ec86e2 100644
--- a/web/src/features/in-app-agent/components/InAppAgentWindow.clienttest.tsx
+++ b/web/src/features/in-app-agent/components/InAppAgentWindow.clienttest.tsx
@@ -5,13 +5,53 @@ import {
InAppAgentWindow,
type InAppAgentWindowProps,
} from "./InAppAgentWindow";
+import { ControlledInAppAgentWindow } from "./ControlledInAppAgentWindow";
const capture = vi.fn();
+const controlledAgent = vi.hoisted(() => ({
+ value: {
+ conversations: [],
+ error: null,
+ hasMoreConversations: false,
+ isLoadingMoreConversations: false,
+ isRunning: true,
+ isSelectedConversationHydrating: false,
+ isSubmitting: false,
+ invalidateConversations: vi.fn(),
+ liveMessageVersion: 0,
+ loadMoreConversations: vi.fn(),
+ messages: [],
+ pendingToolApprovals: [] as Array<{ id: string }>,
+ approveToolCall: vi.fn(),
+ rejectToolCall: vi.fn(),
+ selectedConversationId: undefined,
+ selectedConversationIsWriteLocked: false,
+ submit: vi.fn(),
+ submitFeedback: vi.fn(),
+ },
+}));
vi.mock("@/src/features/posthog-analytics/usePostHogClientCapture", () => ({
usePostHogClientCapture: () => capture,
}));
+vi.mock("next/router", () => ({
+ useRouter: () => ({ asPath: "/" }),
+}));
+
+vi.mock("./InAppAiAgentProvider", () => ({
+ useInAppAiAgent: () => controlledAgent.value,
+}));
+
+vi.mock("./useSmoothStreamingMessages", () => ({
+ useSmoothStreamingMessages: () => ({
+ isAnimating: false,
+ messages: [],
+ pendingToolApprovals: [],
+ runningToolCallIds: [],
+ }),
+}));
+
// jsdom does not implement Element scrolling.
Element.prototype.scrollTo = vi.fn();
@@ -26,7 +66,7 @@ function windowElement(
hasMoreConversations: false,
isAssistantTurnInProgress: false,
isExpanded: false,
- isInputDisabled: false,
+ isConversationInteractionDisabled: false,
isLoadingMoreConversations: false,
messages: [],
onApproveToolCall: vi.fn(),
@@ -143,3 +183,119 @@ describe("InAppAgentWindow quick actions", () => {
).toBeInTheDocument();
});
});
+
+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();
+ });
+
+ it("keeps a draft editable but prevents submitting it while an assistant turn is active", () => {
+ const onSubmit = vi.fn().mockResolvedValue(true);
+ controlledAgent.value.isRunning = true;
+ controlledAgent.value.pendingToolApprovals = [];
+ controlledAgent.value.submit = onSubmit;
+ render(
+ <TooltipProvider>
+ <ControlledInAppAgentWindow
+ isExpanded={false}
+ onClose={vi.fn()}
+ onDeleteConversation={vi.fn()}
+ onExpandedChange={vi.fn()}
+ />
+ </TooltipProvider>,
+ );
+
+ 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();
+ });
+
+ it("keeps navigation disabled while an approval is pending", () => {
+ controlledAgent.value.isRunning = false;
+ controlledAgent.value.pendingToolApprovals = [{ id: "approval-1" }];
+ controlledAgent.value.submit = vi.fn();
+
+ render(
+ <TooltipProvider>
+ <ControlledInAppAgentWindow
+ isExpanded={false}
+ onClose={vi.fn()}
+ onDeleteConversation={vi.fn()}
+ onExpandedChange={vi.fn()}
+ />
+ </TooltipProvider>,
+ );
+
+ expect(
+ screen.getByRole("textbox", { name: "Message the assistant" }),
+ ).toBeEnabled();
+ expect(screen.getByRole("button", { name: "Send message" })).toBeDisabled();
+ expect(
+ screen.getByRole("button", { name: "Start new conversation" }),
+ ).toBeDisabled();
+ expect(
+ screen.getByRole("button", { name: "Conversation history" }),
+ ).toBeDisabled();
+ });
+});
+
+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 966c45ec78..2941b146b7 100644
--- a/web/src/features/in-app-agent/components/InAppAgentWindow.stories.tsx
+++ b/web/src/features/in-app-agent/components/InAppAgentWindow.stories.tsx
@@ -589,7 +589,7 @@ const meta = preview.meta({
args: {
error: null,
isExpanded: false,
- isInputDisabled: false,
+ isConversationInteractionDisabled: false,
conversations,
hasMoreConversations: false,
isLoadingMoreConversations: false,
@@ -617,7 +617,7 @@ const meta = preview.meta({
export const ToolApprovalRequired = meta.story({
args: {
isAssistantTurnInProgress: true,
- isInputDisabled: true,
+ isConversationInteractionDisabled: true,
selectedConversationId: "conversation-1",
messages: [
{
@@ -867,7 +867,7 @@ export const LoadingResponse = meta.story({
export const LoadingAfterToolCall = meta.story({
args: {
isAssistantTurnInProgress: true,
- isInputDisabled: true,
+ isConversationInteractionDisabled: true,
messages: [
{
id: "user-1",
@@ -952,7 +952,7 @@ export const LoadingAfterToolCall = meta.story({
export const Connecting = meta.story({
args: {
isAssistantTurnInProgress: true,
- isInputDisabled: true,
+ isConversationInteractionDisabled: true,
messages: [
{
id: "user-1",
@@ -998,7 +998,7 @@ export const RateLimited = meta.story({
args: {
error: null,
isAssistantTurnInProgress: true,
- isInputDisabled: true,
+ isConversationInteractionDisabled: false,
messages: [
{
id: "approval-1",
@@ -1041,7 +1041,7 @@ export const RateLimited = meta.story({
await expect(alert).toHaveTextContent("Try again in about");
await expect(
canvas.getByRole("textbox", { name: "Message the assistant" }),
- ).toBeDisabled();
+ ).toBeEnabled();
await expect(
canvas.getByRole("button", { name: "Confirm" }),
).toBeDisabled();
@@ -1062,7 +1062,10 @@ export const RefocusAfterSubmit = meta.story({
},
render: function Render(args) {
const [isExpanded, setIsExpanded] = useState(args.isExpanded);
- const [isInputDisabled, setIsInputDisabled] = useState(false);
+ const [
+ isConversationInteractionDisabled,
+ setIsConversationInteractionDisabled,
+ ] = useState(false);
const [messages, setMessages] = useState<InAppAgentWindowMessage[]>([
{
id: "user-1",
@@ -1089,14 +1092,16 @@ export const RefocusAfterSubmit = meta.story({
{...args}
isHeaderDragHandleEnabled={isHeaderDragHandleEnabled}
isExpanded={isExpanded}
- isInputDisabled={isInputDisabled}
+ isConversationInteractionDisabled={
+ isConversationInteractionDisabled
+ }
messages={messages}
onExpandedChange={(isExpanded) => {
setIsExpanded(isExpanded);
args.onExpandedChange(isExpanded);
}}
onSubmit={(input) => {
- setIsInputDisabled(true);
+ setIsConversationInteractionDisabled(true);
window.setTimeout(() => {
setMessages((currentMessages) => [
...currentMessages,
@@ -1109,7 +1114,7 @@ export const RefocusAfterSubmit = meta.story({
},
},
]);
- setIsInputDisabled(false);
+ setIsConversationInteractionDisabled(false);
}, 50);
args.onSubmit(input);
@@ -1147,7 +1152,7 @@ export const FeedbackControlsWaitForTurnEnd = meta.story({
name: "(Test) Feedback Controls Wait For Turn End",
args: {
selectedConversationId: "conversation-1",
- isInputDisabled: true,
+ isConversationInteractionDisabled: true,
isAssistantTurnInProgress: true,
onSubmitFeedback: fn(),
messages: [
diff --git a/web/src/features/in-app-agent/components/InAppAgentWindow.tsx b/web/src/features/in-app-agent/components/InAppAgentWindow.tsx
index cc04c4c23a..780a4e6f9b 100644
--- a/web/src/features/in-app-agent/components/InAppAgentWindow.tsx
+++ b/web/src/features/in-app-agent/components/InAppAgentWindow.tsx
@@ -263,7 +263,7 @@ export type InAppAgentWindowProps = {
isAssistantTurnInProgress: boolean;
isHeaderDragHandleEnabled?: boolean;
isExpanded: boolean;
- isInputDisabled: boolean;
+ isConversationInteractionDisabled: boolean;
isLoadingMoreConversations: boolean;
messages: InAppAgentWindowMessage[];
onExpandedChange: (isExpanded: boolean) => void;
@@ -364,7 +364,7 @@ export function InAppAgentWindow(props: InAppAgentWindowProps) {
isAssistantTurnInProgress,
isHeaderDragHandleEnabled = false,
isExpanded,
- isInputDisabled: baseIsInputDisabled,
+ isConversationInteractionDisabled,
isLoadingMoreConversations,
messages,
onDeleteConversation,
@@ -390,12 +390,17 @@ export function InAppAgentWindow(props: InAppAgentWindowProps) {
// No auto-focus on mobile — it springs the keyboard and buries the panel.
const isMobile = useIsMobile();
const isRateLimited = isInAppAgentRateLimited(error);
- const isInputDisabled = baseIsInputDisabled || isRateLimited;
+ const isComposerDisabled = isConversationInteractionDisabled;
+ const isSubmitDisabled =
+ isComposerDisabled || isRateLimited || isAssistantTurnInProgress;
const viewportRef = useRef<HTMLDivElement>(null);
const isAutoScrollAttachedRef = useRef(true);
const previousScrollTopRef = useRef(0);
const inputRef = useRef<HTMLTextAreaElement>(null);
- const previousIsInputDisabledRef = useRef(isInputDisabled);
+ const previousIsInputDisabledRef = useRef(isComposerDisabled);
+ const previousIsAssistantTurnInProgressRef = useRef(
+ isAssistantTurnInProgress,
+ );
const [input, setInput] = useState("");
const [isConversationHistoryOpen, setIsConversationHistoryOpen] =
useState(false);
@@ -432,7 +437,7 @@ export function InAppAgentWindow(props: InAppAgentWindowProps) {
const submitInput = (content: string, options?: InAppAgentSubmitOptions) => {
const trimmedContent = content.trim();
- if (!trimmedContent || isInputDisabled) {
+ if (!trimmedContent || isSubmitDisabled) {
return;
}
@@ -470,8 +475,9 @@ export function InAppAgentWindow(props: InAppAgentWindowProps) {
// Update after refs commit so setInputRef can compare the previous disabled state.
useEffect(() => {
- previousIsInputDisabledRef.current = isInputDisabled;
- }, [isInputDisabled]);
+ previousIsInputDisabledRef.current = isComposerDisabled;
+ previousIsAssistantTurnInProgressRef.current = isAssistantTurnInProgress;
+ }, [isAssistantTurnInProgress, isComposerDisabled]);
const setInputRef = useCallback(
(input: HTMLTextAreaElement | null) => {
@@ -480,13 +486,16 @@ export function InAppAgentWindow(props: InAppAgentWindowProps) {
// Skip on mobile: refocusing after a turn re-springs the keyboard, even
// for the quick-action flow that never focused the input.
const shouldRefocusInput =
- previousIsInputDisabledRef.current && !isInputDisabled && !isMobile;
+ ((previousIsInputDisabledRef.current && !isComposerDisabled) ||
+ (previousIsAssistantTurnInProgressRef.current &&
+ !isAssistantTurnInProgress)) &&
+ !isMobile;
if (input && shouldRefocusInput) {
input.focus();
}
},
- [isInputDisabled, isMobile],
+ [isAssistantTurnInProgress, isComposerDisabled, isMobile],
);
useEffect(() => {
@@ -534,7 +543,9 @@ export function InAppAgentWindow(props: InAppAgentWindowProps) {
size="icon"
className="size-6 shrink-0"
onClick={onNewConversation}
- disabled={baseIsInputDisabled}
+ disabled={
+ isConversationInteractionDisabled || isAssistantTurnInProgress
+ }
aria-label="Start new conversation"
>
<Plus className="size-3" />
@@ -560,7 +571,10 @@ export function InAppAgentWindow(props: InAppAgentWindowProps) {
variant="ghost"
size="icon"
className="size-6 shrink-0"
- disabled={baseIsInputDisabled}
+ disabled={
+ isConversationInteractionDisabled ||
+ isAssistantTurnInProgress
+ }
aria-label="Conversation history"
>
<History className="size-3" />
@@ -607,7 +621,10 @@ export function InAppAgentWindow(props: InAppAgentWindowProps) {
variant="ghost"
size="icon-xs"
className="text-muted-foreground hover:text-destructive -mr-1.5 shrink-0"
- disabled={baseIsInputDisabled}
+ disabled={
+ isConversationInteractionDisabled ||
+ isAssistantTurnInProgress
+ }
aria-label="Delete conversation"
onClick={(event) => {
event.preventDefault();
@@ -717,7 +734,7 @@ export function InAppAgentWindow(props: InAppAgentWindowProps) {
key={`${selectedConversationId ?? "new"}:${quickActionResetKey}`}
focusedActions={focusedQuickActions}
initialContext={quickActionContext}
- isDisabled={isInputDisabled}
+ isDisabled={isSubmitDisabled}
onSelectAction={(action, context, position) => {
capture("in_app_agent:quick_action_started", {
quickActionKey: action.id,
@@ -776,7 +793,7 @@ export function InAppAgentWindow(props: InAppAgentWindowProps) {
role={message.role}
content={message.content}
isCompact={!isExpanded}
- isFeedbackDisabled={baseIsInputDisabled}
+ isFeedbackDisabled={isConversationInteractionDisabled}
onSubmitFeedback={
feedbackRunId
? (params) =>
@@ -935,7 +952,7 @@ export function InAppAgentWindow(props: InAppAgentWindowProps) {
event.currentTarget.form?.requestSubmit();
}
}}
- disabled={isInputDisabled}
+ disabled={isComposerDisabled}
aria-label="Message the assistant"
placeholder="Let me know what I can do for you..."
rows={1}
@@ -953,7 +970,7 @@ export function InAppAgentWindow(props: InAppAgentWindowProps) {
className="h-8 w-8 rounded-md border"
aria-label="Send message"
variant="outline"
- disabled={isInputDisabled || !input.trim()}
+ disabled={isSubmitDisabled || !input.trim()}
>
<SendHorizontal className="h-4 w-4" />
</Button>
@@ -965,7 +982,7 @@ export function InAppAgentWindow(props: InAppAgentWindowProps) {
type="submit"
className="h-8 w-fit rounded-md px-3"
aria-label="Send message"
- disabled={isInputDisabled || !input.trim()}
+ disabled={isSubmitDisabled || !input.trim()}
onClick={(e) => {
e.stopPropagation();
}}