π OpenHands: AI-Driven Development
fix(automations): keep the setup composer out of the Home draft (#18392)
Engel Nystd94b50f37afa
diff --git a/.pr/1-detail-actions-menu.png b/.pr/1-detail-actions-menu.pngdeleted file mode 100644index f9fa8b130d..0000000000Binary files a/.pr/1-detail-actions-menu.png and /dev/null differdiff --git a/.pr/2-edit-opens-setup-editor.png b/.pr/2-edit-opens-setup-editor.pngdeleted file mode 100644index 2ce949faec..0000000000Binary files a/.pr/2-edit-opens-setup-editor.png and /dev/null differdiff --git a/.pr/3-chat-input.png b/.pr/3-chat-input.pngdeleted file mode 100644index 19985c1cb7..0000000000Binary files a/.pr/3-chat-input.png and /dev/null differdiff --git a/__tests__/routes/automation-setup-new-composer-draft.test.tsx b/__tests__/routes/automation-setup-new-composer-draft.test.tsxnew file mode 100644index 0000000000..027c8a5fb2--- /dev/null+++ b/__tests__/routes/automation-setup-new-composer-draft.test.tsx@@ -0,0 +1,96 @@+import { fireEvent, screen } from "@testing-library/react";+import { MemoryRouter } from "react-router";+import {+ afterAll,+ beforeAll,+ beforeEach,+ describe,+ expect,+ it,+ vi,+} from "vitest";+import { renderWithProviders } from "test-utils";+import AutomationSetupNew from "#/routes/automation-setup-new";+import {+ PENDING_AUTOMATION_SETUP_ID,+ clearAutomationFormSession,+} from "#/api/automation-form-session";+import { HOME_PROMPT_DRAFT_KEY } from "#/hooks/chat/use-draft-persistence";++// The setup form itself is not under test; the docked composer is.+vi.mock(+ "#/components/features/automations/setup/automation-setup-panel",+ () => ({+ AutomationSetupPanel: () => <div data-testid="setup-panel" />,+ }),+);++const HOME_DRAFT = "Plan my Home task";+const SETUP_PROMPT = "Summarize overnight alerts";++function renderSetupRoute() {+ return renderWithProviders(+ <MemoryRouter initialEntries={["/automations/setup"]}>+ <AutomationSetupNew />+ </MemoryRouter>,+ { navigation: { currentPath: "/automations/setup", conversationId: null } },+ );+}++function typeIntoComposer(text: string) {+ const input = screen.getByTestId("chat-input");+ input.textContent = text;+ fireEvent.input(input);+}++describe("AutomationSetupNew docked composer draft", () => {+ beforeAll(() => {+ // jsdom has no innerText, which the composer reads its text from.+ Object.defineProperty(HTMLElement.prototype, "innerText", {+ configurable: true,+ get(this: HTMLElement) {+ return this.textContent ?? "";+ },+ });+ });++ afterAll(() => {+ Reflect.deleteProperty(HTMLElement.prototype, "innerText");+ });++ beforeEach(() => {+ clearAutomationFormSession(PENDING_AUTOMATION_SETUP_ID);+ sessionStorage.clear();+ localStorage.clear();+ });++ it("opens with an empty composer even when a Home draft exists", async () => {+ sessionStorage.setItem(HOME_PROMPT_DRAFT_KEY, HOME_DRAFT);++ renderSetupRoute();++ expect((await screen.findByTestId("chat-input")).textContent).toBe("");+ });++ it("does not leave a typed setup prompt behind as the Home draft", async () => {+ const { unmount } = renderSetupRoute();+ await screen.findByTestId("chat-input");++ typeIntoComposer(SETUP_PROMPT);+ unmount();++ expect(sessionStorage.getItem(HOME_PROMPT_DRAFT_KEY)).toBeNull();+ });++ it("keeps an earlier Home draft while the setup composer is edited and left", async () => {+ sessionStorage.setItem(HOME_PROMPT_DRAFT_KEY, HOME_DRAFT);+ const { unmount } = renderSetupRoute();+ await screen.findByTestId("chat-input");++ typeIntoComposer(SETUP_PROMPT);+ typeIntoComposer("");+ unmount();++ expect(sessionStorage.getItem(HOME_PROMPT_DRAFT_KEY)).toBe(HOME_DRAFT);+ });+});diff --git a/src/components/features/chat/custom-chat-input.tsx b/src/components/features/chat/custom-chat-input.tsxindex 3888c0a710..c876c7ffef 100644--- a/src/components/features/chat/custom-chat-input.tsx+++ b/src/components/features/chat/custom-chat-input.tsx@@ -18,6 +18,11 @@ export interface CustomChatInputProps {showButton?: boolean;/** Overrides the input's default placeholder text. */placeholder?: string;+ /**+ * Whether this composer saves and restores a draft. Defaults to true; turn+ * it off for composers that must not share the Home draft.+ */+ persistDraft?: boolean;onSubmit: (message: string) => void;onFocus?: () => void;onBlur?: () => void;@@ -35,6 +40,7 @@ export function CustomChatInput({hasStartedConversation,showButton = true,placeholder,+ persistDraft = true,onSubmit,onFocus,onBlur,@@ -82,7 +88,7 @@ export function CustomChatInput({// Custom hooksconst { chatInputRef, messageToSend, checkIsContentEmpty, saveDraft } =- useChatInputLogic();+ useChatInputLogic({ persistDraft });const syncCanSubmit = React.useCallback(() => {const text = chatInputRef.current?.innerText ?? "";diff --git a/src/components/features/chat/interactive-chat-box.tsx b/src/components/features/chat/interactive-chat-box.tsxindex 2eb6ae08a5..61392be629 100644--- a/src/components/features/chat/interactive-chat-box.tsx+++ b/src/components/features/chat/interactive-chat-box.tsx@@ -20,6 +20,8 @@ interface InteractiveChatBoxProps {hasStartedConversation?: boolean;showGitControlBar?: boolean;placeholder?: string;+ /** Whether the composer saves and restores a draft; see CustomChatInput. */+ persistDraft?: boolean;}export function InteractiveChatBox({@@ -28,6 +30,7 @@ export function InteractiveChatBox({hasStartedConversation,showGitControlBar = true,placeholder,+ persistDraft = true,}: InteractiveChatBoxProps) {const {images,@@ -84,6 +87,7 @@ export function InteractiveChatBox({isNewConversationPending={disabled}hasStartedConversation={hasStartedConversation}placeholder={placeholder}+ persistDraft={persistDraft}onSubmit={handleSubmit}onFilesPaste={handleUpload}/>diff --git a/src/hooks/chat/use-chat-input-logic.ts b/src/hooks/chat/use-chat-input-logic.tsindex e32b4e8abc..b21c5f4b1e 100644--- a/src/hooks/chat/use-chat-input-logic.ts+++ b/src/hooks/chat/use-chat-input-logic.ts@@ -11,9 +11,14 @@ import { useOptionalConversationId } from "#/hooks/use-conversation-id";import { useDraftPersistence } from "./use-draft-persistence";/**- * Hook for managing chat input content logic+ * Hook for managing chat input content logic.+ *+ * `persistDraft: false` turns off draft persistence for a composer that does+ * not own a draft (see `useDraftPersistence`).*/-export const useChatInputLogic = () => {+export const useChatInputLogic = ({+ persistDraft = true,+}: { persistDraft?: boolean } = {}) => {const chatInputRef = useRef<HTMLDivElement | null>(null);// Optional because the chat input also renders on the home page, where no// conversation route is mounted yet. Draft persistence is conversation-@@ -33,6 +38,7 @@ export const useChatInputLogic = () => {const { saveDraft, clearDraft } = useDraftPersistence(conversationId,chatInputRef,+ { persist: persistDraft },);// Apply only messages queued for this composer. On the home page (nodiff --git a/src/hooks/chat/use-draft-persistence.ts b/src/hooks/chat/use-draft-persistence.tsindex 0f7119333f..76b4525b59 100644--- a/src/hooks/chat/use-draft-persistence.ts+++ b/src/hooks/chat/use-draft-persistence.ts@@ -37,10 +37,15 @@ export const HOME_PROMPT_DRAFT_KEY = "oh:home-prompt-draft";* under the conversation's key. When `conversationId` is undefined (home page),* the draft is persisted to sessionStorage under `HOME_PROMPT_DRAFT_KEY` so it* survives navigation within the session but is discarded on tab close.+ *+ * Pass `persist: false` for a composer that owns no draft, such as the+ * automation setup composer: it then neither restores nor writes any draft,+ * so it cannot pick up or overwrite the Home prompt.*/export const useDraftPersistence = (conversationId: string | null | undefined,chatInputRef: React.RefObject<HTMLDivElement | null>,+ { persist = true }: { persist?: boolean } = {},) => {// Pass an empty string when conversationId is missing to keep the hook// call unconditional (rules of hooks). The early-return branches below@@ -150,6 +155,12 @@ export const useDraftPersistence = (return;}+ if (!persist) {+ hasRestoredRef.current = true;+ setIsRestored(true);+ return;+ }+const element = chatInputRef.current;if (!element) {return;@@ -191,10 +202,14 @@ export const useDraftPersistence = (hasRestoredRef.current = true;setIsRestored(true);- }, [chatInputRef, conversationId]);+ }, [chatInputRef, conversationId, persist]);// Debounced save function - called from onInput handlerconst saveDraft = useCallback(() => {+ if (!persist) {+ return;+ }+// Clear any pending saveif (saveTimeoutRef.current) {clearTimeout(saveTimeoutRef.current);@@ -253,10 +268,20 @@ export const useDraftPersistence = (setDraftMessage(text || null);}}, DRAFT_SAVE_DEBOUNCE_MS);- }, [chatInputRef, state.draftMessage, setDraftMessage, conversationId]);+ }, [+ chatInputRef,+ state.draftMessage,+ setDraftMessage,+ conversationId,+ persist,+ ]);// Clear draft - called after message delivery is confirmedconst clearDraft = useCallback(() => {+ if (!persist) {+ return;+ }+// Cancel any pending saveif (saveTimeoutRef.current) {clearTimeout(saveTimeoutRef.current);@@ -273,7 +298,7 @@ export const useDraftPersistence = (return;}setDraftMessage(null);- }, [conversationId, setDraftMessage]);+ }, [conversationId, setDraftMessage, persist]);// Flush the conversation input while it is still attached: reading innerText// after detachment loses line breaks between contentEditable block elements.@@ -287,7 +312,7 @@ export const useDraftPersistence = (useLayoutEffect(() => () => {flushPendingDraft();- if (!currentConversationIdRef.current) {+ if (persist && !currentConversationIdRef.current) {const text = lastHomeTextRef.current;try {if (text) {@@ -302,7 +327,7 @@ export const useDraftPersistence = (}}},- [flushPendingDraft],+ [flushPendingDraft, persist],);return {diff --git a/src/routes/automation-setup-new.tsx b/src/routes/automation-setup-new.tsxindex 035cf0f2e8..21fad271b0 100644--- a/src/routes/automation-setup-new.tsx+++ b/src/routes/automation-setup-new.tsx@@ -147,9 +147,13 @@ export default function AutomationSetupNew() {{composerTarget? createPortal(<ComposerDockedProvider enabled minimal>+ {/* No conversation route is mounted here, so a persisting+ composer would read and write the Home draft. The prompt+ belongs to this setup only and is not kept as a draft. */}<InteractiveChatBoxonSubmit={(content) => startConversationFromPrompt(content)}disabled={isPending || !draft}+ persistDraft={false}showGitControlBar={false}placeholder={t(I18nKey.AUTOMATION_SETUP$DOCKED_COMPOSER_PLACEHOLDER,