Skip to content

feat(cursor): add chat command with CDP native key events#669

Open
cjb2667 wants to merge 1 commit intojackwener:mainfrom
cjb2667:feat/cursor-chat-command
Open

feat(cursor): add chat command with CDP native key events#669
cjb2667 wants to merge 1 commit intojackwener:mainfrom
cjb2667:feat/cursor-chat-command

Conversation

@cjb2667
Copy link
Copy Markdown

@cjb2667 cjb2667 commented Apr 1, 2026

Summary

  • Add opencli cursor chat "prompt" command that opens a new Cursor chat session and sends a prompt
  • Uses CDP native Input.dispatchKeyEvent instead of JS dispatchEvent to submit messages, fixing compatibility with Cursor v2.6.22+ Lexical editor
  • Supports --timeout parameter (default 60s) to control how long to wait for AI response

Motivation

The existing send and ask commands use JavaScript dispatchEvent(new KeyboardEvent(...)) to simulate Enter key press. However, Cursor's Lexical editor (since v2.6+) does not respond to synthetic keyboard events — only CDP-level native input events trigger the submit action.

This new chat command:

  1. Opens a fresh chat via Cmd+N (the correct shortcut for new chat, not Cmd+L)
  2. Injects text via document.execCommand('insertText')
  3. Submits via CDP Input.dispatchKeyEvent (keyDown + keyUp for Enter)
  4. Polls for AI response from [data-message-role="ai"] elements

Test plan

  • opencli cursor chat "请回复 hello world" → AI responds Hello world!
  • opencli cursor chat "请回复数字 42" → AI responds 42
  • Timeout parameter works: --timeout 120
  • New chat session is created each time (message count resets to 0)

Made with Cursor

- 使用 Cmd+N 打开新对话窗口(而非 Cmd+L)
- 通过 CDP Input.dispatchKeyEvent 发送 Enter 键,解决 Cursor v2.6.22
  Lexical 编辑器不响应 JS dispatchEvent 合成事件的问题
- 支持 --timeout 参数控制等待 AI 回复的超时时间(默认 60s)
- 从 [data-message-role="ai"] 的 .markdown-root 提取回复文本

Made-with: Cursor
Copy link
Copy Markdown
Contributor

@Astro-Han Astro-Han left a comment

Choose a reason for hiding this comment

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

A few concerns:

  1. Why not fix the existing commands? The PR says send/ask broke because Cursor's Lexical editor ignores synthetic JS keyboard events. The CDP Input.dispatchKeyEvent fix here is the right technique, but it should be applied to the existing send/ask commands rather than adding a parallel chat command that does the same thing differently. Users shouldn't need to know which command works with which Cursor version.

  2. (page as any).bridge bypasses the IPage abstraction. cdpPressEnter and cdpKeyCombo reach into the CDPBridge internal instance directly. This only works in direct CDP mode — in daemon mode (the default), bridge doesn't exist and this will throw at runtime with no useful error message. If Input.dispatchKeyEvent is needed, it should go through the IPage interface so both transport paths can support it.

  3. No tests. Even a basic unit test mocking the page object and verifying the call sequence (new chat → inject text → press Enter → poll for response) would catch regressions.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants