Skip to content

Comments

feat(copilot): add openChainlitCopilot and closeChainlitCopilot functions#2797

Open
hztBUAA wants to merge 1 commit intoChainlit:mainfrom
hztBUAA:feat/copilot-open-close-2579
Open

feat(copilot): add openChainlitCopilot and closeChainlitCopilot functions#2797
hztBUAA wants to merge 1 commit intoChainlit:mainfrom
hztBUAA:feat/copilot-open-close-2579

Conversation

@hztBUAA
Copy link

@hztBUAA hztBUAA commented Feb 20, 2026

Summary

Adds two new window functions for programmatic control of the copilot widget:

  • window.openChainlitCopilot() - explicitly opens the copilot chat window
  • window.closeChainlitCopilot() - explicitly closes the copilot chat window

These complement the existing window.toggleChainlitCopilot() function by allowing host pages to set a specific open/close state without needing to track the current state of the widget.

Motivation

As described in #2579, the toggle-only API is insufficient for common use cases like:

  • Auto-opening the chat after a delay (e.g., setTimeout(() => window.openChainlitCopilot(), 5000)) without disturbing users who already opened it
  • Closing the copilot in response to an external event without risking accidentally opening it
  • Integrating with external UI components that have explicit open/close semantics

Changes

  • libs/copilot/index.tsx: Added openChainlitCopilot and closeChainlitCopilot to the global Window interface type declarations
  • libs/copilot/src/app.tsx: Added the same type declarations to the duplicate Window interface
  • libs/copilot/src/widget.tsx: Implemented the two functions using setIsOpen(true) and setIsOpen(false), with proper cleanup on unmount (matching the existing pattern for toggleChainlitCopilot)

Test plan

  • Mount the copilot widget and call window.openChainlitCopilot() from the browser console - copilot should open
  • Call window.openChainlitCopilot() when already open - should remain open (no-op)
  • Call window.closeChainlitCopilot() - copilot should close
  • Call window.closeChainlitCopilot() when already closed - should remain closed (no-op)
  • Call window.toggleChainlitCopilot() - should still work as before
  • Unmount widget and call window.openChainlitCopilot() - should log error to console

Closes #2579


Summary by cubic

Add window.openChainlitCopilot() and window.closeChainlitCopilot() to let host pages explicitly open or close the Copilot chat. This complements the existing toggle and enables safe auto-open/close flows without tracking state.

  • New Features
    • Exposed openChainlitCopilot and closeChainlitCopilot on window; added type declarations in libs/copilot/index.tsx and libs/copilot/src/app.tsx.
    • Implemented in libs/copilot/src/widget.tsx via setIsOpen(true/false) with error logging when the widget is unmounted.

Written for commit 94a03a1. Summary will update on new commits.

…ions

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 Chainlit#2579
@dosubot dosubot bot added size:XS This PR changes 0-9 lines, ignoring generated files. frontend Pertains to the frontend. labels Feb 20, 2026
Copy link
Contributor

@cubic-dev-ai cubic-dev-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No issues found across 3 files

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

frontend Pertains to the frontend. size:XS This PR changes 0-9 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Copilot mode: add functions to programmatically open and close chatbot window

1 participant