From 94a03a1bf2ac8ff9a1ce01d48bea400793bf1c29 Mon Sep 17 00:00:00 2001 From: hzt <3061613175@qq.com> Date: Fri, 20 Feb 2026 13:46:13 +0800 Subject: [PATCH] feat(copilot): add openChainlitCopilot and closeChainlitCopilot functions Add two new window functions for programmatic control of the copilot widget state, complementing the existing toggleChainlitCopilot. This allows host pages to explicitly open or close the copilot window without needing to track its current state, enabling use cases like auto-opening the chat after a delay without disturbing users who already opened it themselves. Closes #2579 --- libs/copilot/index.tsx | 2 ++ libs/copilot/src/app.tsx | 2 ++ libs/copilot/src/widget.tsx | 4 ++++ 3 files changed, 8 insertions(+) diff --git a/libs/copilot/index.tsx b/libs/copilot/index.tsx index 9dc840612e..d5c4d99bc4 100644 --- a/libs/copilot/index.tsx +++ b/libs/copilot/index.tsx @@ -30,6 +30,8 @@ declare global { mountChainlitWidget: (config: IWidgetConfig) => void; unmountChainlitWidget: () => void; toggleChainlitCopilot: () => void; + openChainlitCopilot: () => void; + closeChainlitCopilot: () => void; sendChainlitMessage: (message: IStep) => void; getChainlitCopilotThreadId: () => string | null; clearChainlitCopilotThreadId: (newThreadId?: string) => void; diff --git a/libs/copilot/src/app.tsx b/libs/copilot/src/app.tsx index 256ecabd1e..d499149438 100644 --- a/libs/copilot/src/app.tsx +++ b/libs/copilot/src/app.tsx @@ -22,6 +22,8 @@ declare global { interface Window { cl_shadowRootElement: HTMLDivElement; toggleChainlitCopilot: () => void; + openChainlitCopilot: () => void; + closeChainlitCopilot: () => void; theme?: { light: Record; dark: Record; diff --git a/libs/copilot/src/widget.tsx b/libs/copilot/src/widget.tsx index f41e72de3b..0eb28a0b82 100644 --- a/libs/copilot/src/widget.tsx +++ b/libs/copilot/src/widget.tsx @@ -32,11 +32,15 @@ const Widget = ({ config, error }: Props) => { useEffect(() => { window.toggleChainlitCopilot = () => setIsOpen((prev) => !prev); + window.openChainlitCopilot = () => setIsOpen(true); + window.closeChainlitCopilot = () => setIsOpen(false); window.getChainlitCopilotThreadId = getChainlitCopilotThreadId; window.clearChainlitCopilotThreadId = clearChainlitCopilotThreadId; return () => { window.toggleChainlitCopilot = () => console.error('Widget not mounted.'); + window.openChainlitCopilot = () => console.error('Widget not mounted.'); + window.closeChainlitCopilot = () => console.error('Widget not mounted.'); window.getChainlitCopilotThreadId = () => null; window.clearChainlitCopilotThreadId = () =>