Skip to content

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

fix(agent): Refocus input after generation (#14707)

Ben Bachemaec2bc7428e9
diff --git a/.agents/skills/storybook/SKILL.md b/.agents/skills/storybook/SKILL.md
index 1398f796a8..8c9a0c04a3 100644
--- a/.agents/skills/storybook/SKILL.md
+++ b/.agents/skills/storybook/SKILL.md
@@ -31,10 +31,12 @@ Keep the existing component, but update it to use the newly created component fo
- Use "CSF Next" format by default.
- Cover only the relevant component by default.
- Avoid custom render functions by default.
+- Do not define helper components inside story files unless the helper component is reused by multiple stories. For one-off setup, inline the stateful render logic directly in the story `render` function.
- Never use `decorators` to add styling or layout to a story. Stories should render the components as they are.
- Use `satisfies` and typed Storybook metadata so invalid args, decorators, and play functions are type-checked.
- Use play functions to test user-relevant interactions after render, not to compensate for complex setup or hidden dependencies.
- Prefix stories whose primary purpose is interaction test coverage with `(Test)` in the Storybook display name, for example `name: "(Test) Opens Menu"`.
+- Keep `(Test)` stories sorted at the bottom of the file so showcase and product-facing stories stay grouped first.
- Name stories after the state they represent, not the implementation. Also do not include the component name in the story name.
- Prefer: `Default`, `Empty`, `WithLongName`, `Error`, `Disabled`, `Loading`
- Avoid: `Test1`, `CustomRenderExample`, `ButtonWithLongNameAndIcon`
diff --git a/web/src/ee/features/in-app-agent/components/InAppAgentWindow.stories.tsx b/web/src/ee/features/in-app-agent/components/InAppAgentWindow.stories.tsx
index e486cb32d0..4274105d0b 100644
--- a/web/src/ee/features/in-app-agent/components/InAppAgentWindow.stories.tsx
+++ b/web/src/ee/features/in-app-agent/components/InAppAgentWindow.stories.tsx
@@ -1,6 +1,6 @@
import preview from "../../../../../.storybook/preview";
import { type ReactNode, useEffect, useRef, useState } from "react";
-import { fn } from "storybook/test";
+import { expect, fn, userEvent, waitFor, within } from "storybook/test";
import {
InAppAgentWindow,
type InAppAgentWindowMessage,
@@ -800,3 +800,76 @@ export const Error = meta.story({
],
},
});
+
+export const RefocusAfterSubmit = {
+ name: "(Test) Refocus After Submit",
+ render: function Render(args: InAppAgentWindowProps) {
+ const [isExpanded, setIsExpanded] = useState(args.isExpanded);
+ const [isInputDisabled, setIsInputDisabled] = useState(false);
+ const [messages, setMessages] = useState<InAppAgentWindowMessage[]>([
+ {
+ id: "assistant-1",
+ role: "assistant",
+ content: {
+ type: "text",
+ text: "Assistant answer",
+ },
+ },
+ ]);
+
+ return (
+ <InAppAgentWindowStoryShell isExpanded={isExpanded}>
+ {({ isHeaderDragHandleEnabled }) => (
+ <InAppAgentWindow
+ {...args}
+ isHeaderDragHandleEnabled={isHeaderDragHandleEnabled}
+ isExpanded={isExpanded}
+ isInputDisabled={isInputDisabled}
+ messages={messages}
+ onExpandedChange={(isExpanded) => {
+ setIsExpanded(isExpanded);
+ args.onExpandedChange(isExpanded);
+ }}
+ onSubmit={(input) => {
+ setIsInputDisabled(true);
+ window.setTimeout(() => {
+ setMessages((currentMessages) => [
+ ...currentMessages,
+ {
+ id: `assistant-${currentMessages.length + 1}`,
+ role: "assistant",
+ content: {
+ type: "text",
+ text: `Answer for: ${input}`,
+ },
+ },
+ ]);
+ setIsInputDisabled(false);
+ }, 50);
+
+ args.onSubmit(input);
+ return true;
+ }}
+ />
+ )}
+ </InAppAgentWindowStoryShell>
+ );
+ },
+ play: async ({ canvasElement }: { canvasElement: HTMLElement }) => {
+ const canvas = within(canvasElement);
+ const textarea = canvas.getByLabelText("Ask the assistant a question");
+
+ await userEvent.type(textarea, "Check the latest latency regression");
+ await userEvent.click(canvas.getByRole("button", { name: "Send message" }));
+
+ await waitFor(() => {
+ expect(
+ canvas.getByText("Answer for: Check the latest latency regression"),
+ ).toBeInTheDocument();
+ });
+
+ await waitFor(() => {
+ expect(textarea).toHaveFocus();
+ });
+ },
+};
diff --git a/web/src/ee/features/in-app-agent/components/InAppAgentWindow.tsx b/web/src/ee/features/in-app-agent/components/InAppAgentWindow.tsx
index 9bd386a4df..67886c154c 100644
--- a/web/src/ee/features/in-app-agent/components/InAppAgentWindow.tsx
+++ b/web/src/ee/features/in-app-agent/components/InAppAgentWindow.tsx
@@ -137,6 +137,7 @@ export function InAppAgentWindow(props: InAppAgentWindowProps) {
const isAutoScrollAttachedRef = useRef(true);
const previousScrollTopRef = useRef(0);
const inputRef = useRef<HTMLTextAreaElement>(null);
+ const previousIsInputDisabledRef = useRef(isInputDisabled);
const [input, setInput] = useState("");
const [isConversationHistoryOpen, setIsConversationHistoryOpen] =
useState(false);
@@ -209,6 +210,17 @@ export function InAppAgentWindow(props: InAppAgentWindowProps) {
scrollViewportToBottom(viewportRef.current);
}, [selectedConversationId]);
+ useEffect(() => {
+ const wasInputDisabled = previousIsInputDisabledRef.current;
+ previousIsInputDisabledRef.current = isInputDisabled;
+
+ if (!wasInputDisabled || isInputDisabled) {
+ return;
+ }
+
+ inputRef.current?.focus();
+ }, [isInputDisabled]);
+
useEffect(() => {
const input = inputRef.current;