Skip to content

🤯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.

✨ feat(agent): expose the main chat input editor as window.__mainEditor (#17170)

Arvin Xu895511e1558c
diff --git a/src/routes/(main)/agent/features/Conversation/ConversationArea.tsx b/src/routes/(main)/agent/features/Conversation/ConversationArea.tsx
index 282aa7248ad..f7c78227cf6 100644
--- a/src/routes/(main)/agent/features/Conversation/ConversationArea.tsx
+++ b/src/routes/(main)/agent/features/Conversation/ConversationArea.tsx
@@ -24,6 +24,7 @@ import { useChatStore } from '@/store/chat';
import { threadSelectors, topicSelectors } from '@/store/chat/selectors';
import { messageMapKey } from '@/store/chat/utils/messageMapKey';
+import ExposeMainEditor from './ExposeMainEditor';
import HeterogeneousChatInput from './HeterogeneousChatInput';
import MainChatInput from './MainChatInput';
import MessageFromUrl from './MainChatInput/MessageFromUrl';
@@ -145,6 +146,7 @@ const Conversation = memo(() => {
{isHeterogeneousAgent ? <HeterogeneousChatInput /> : <MainChatInput />}
</MessageForwardFooter>
)}
+ <ExposeMainEditor />
<ThreadHydration />
<ChatMiniMap />
<ForwardMessageDispatcher />
diff --git a/src/routes/(main)/agent/features/Conversation/ExposeMainEditor.tsx b/src/routes/(main)/agent/features/Conversation/ExposeMainEditor.tsx
new file mode 100644
index 00000000000..771fcadb50c
--- /dev/null
+++ b/src/routes/(main)/agent/features/Conversation/ExposeMainEditor.tsx
@@ -0,0 +1,22 @@
+'use client';
+
+import { memo } from 'react';
+
+import { useConversationStore } from '@/features/Conversation/store';
+
+import { useExposeMainEditor } from './useExposeMainEditor';
+
+/**
+ * Renders nothing — mounts the active composer's editor on `window.__mainEditor`.
+ * Must live inside ConversationProvider, which is why ConversationArea can't call the hook
+ * directly from its own body.
+ */
+const ExposeMainEditor = memo(() => {
+ useExposeMainEditor(useConversationStore((s) => s.editor));
+
+ return null;
+});
+
+ExposeMainEditor.displayName = 'ExposeMainEditor';
+
+export default ExposeMainEditor;
diff --git a/src/routes/(main)/agent/features/Conversation/useExposeMainEditor.test.ts b/src/routes/(main)/agent/features/Conversation/useExposeMainEditor.test.ts
new file mode 100644
index 00000000000..4af17cd3a1a
--- /dev/null
+++ b/src/routes/(main)/agent/features/Conversation/useExposeMainEditor.test.ts
@@ -0,0 +1,37 @@
+import { renderHook } from '@testing-library/react';
+import { afterEach, describe, expect, it } from 'vitest';
+
+import { type ChatInputEditor } from '@/features/ChatInput';
+
+import { useExposeMainEditor } from './useExposeMainEditor';
+
+const createEditor = () => ({ focus: () => {} }) as ChatInputEditor;
+
+describe('useExposeMainEditor', () => {
+ afterEach(() => {
+ window.__mainEditor = undefined;
+ });
+
+ it('mounts the editor on window', () => {
+ const editor = createEditor();
+ renderHook(() => useExposeMainEditor(editor));
+
+ expect(window.__mainEditor).toBe(editor);
+ });
+
+ it('leaves window untouched while the editor is not ready', () => {
+ renderHook(() => useExposeMainEditor(null));
+
+ expect(window.__mainEditor).toBeUndefined();
+ });
+
+ it('clears the handle on unmount so a stale editor is never exposed', () => {
+ const editor = createEditor();
+ const { unmount } = renderHook(() => useExposeMainEditor(editor));
+ expect(window.__mainEditor).toBe(editor);
+
+ unmount();
+
+ expect(window.__mainEditor).toBeUndefined();
+ });
+});
diff --git a/src/routes/(main)/agent/features/Conversation/useExposeMainEditor.ts b/src/routes/(main)/agent/features/Conversation/useExposeMainEditor.ts
new file mode 100644
index 00000000000..755e325caae
--- /dev/null
+++ b/src/routes/(main)/agent/features/Conversation/useExposeMainEditor.ts
@@ -0,0 +1,24 @@
+import { useEffect } from 'react';
+
+import { type ChatInputEditor } from '@/features/ChatInput';
+
+/**
+ * Exposes the conversation's chat input editor as `window.__mainEditor`, mirroring
+ * `window.__editor` for the canvas editor.
+ *
+ * Called from the conversation level so it covers both composers — MainChatInput and
+ * HeterogeneousChatInput. Deliberately not mounted inside the shared
+ * `features/Conversation/ChatInput`: AgentBuilder and FloatingChatPanel render that same input,
+ * and letting them claim the handle would leave it pointing at whichever mounted last.
+ */
+export const useExposeMainEditor = (editor: ChatInputEditor | null) => {
+ useEffect(() => {
+ if (!editor) return;
+
+ window.__mainEditor = editor;
+
+ return () => {
+ window.__mainEditor = undefined;
+ };
+ }, [editor]);
+};
diff --git a/src/types/global.d.ts b/src/types/global.d.ts
index 0a670c26c4d..a4baab3d323 100644
--- a/src/types/global.d.ts
+++ b/src/types/global.d.ts
@@ -4,6 +4,8 @@ import { type IEditor } from '@lobehub/editor';
import { type LobeCustomStylish, type LobeCustomToken } from '@lobehub/ui';
import { type AntdToken } from 'antd-style/lib/types/theme';
+import { type ChatInputEditor } from '@/features/ChatInput';
+
import { type SPAServerConfig } from './spaServerConfig';
declare module 'antd-style' {
@@ -23,6 +25,8 @@ declare global {
__LOBE_BOOT_T_HTML__?: number;
/** Dev-only: Zustand store snapshots via `getState()` keyed by store name */
__LOBE_STORES?: Record<string, () => unknown>;
+ /** Chat input editor of the main composer, mounted by MainChatInput while it is on screen */
+ __mainEditor?: ChatInputEditor;
__SERVER_CONFIG__: SPAServerConfig | undefined;
lobeEnv?: {
chromeVersion?: string;