V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
tohearts
V2EX  ›  Cursor

[分享] Opencli 用 CDP 协议从终端直控 Cursor IDE

  •  
  •   tohearts · 16 小时 18 分钟前 · 99 次点击

    发 Prompt 、切模型、翻历史、截图,11 条命令搞定

    做了个 opencli 插件,通过 Chrome DevTools Protocol 直接操控 Cursor IDE 。不是调 CLI ,是真的在操控 IDE 的 UI——你在终端发的 prompt ,打开 Cursor 对话还在。

    GitHub: https://github.com/toheart/cursor-agent

    解决什么问题

    Cursor 3 出了独立 Agent 窗口之后,我日常需要:

    • 在终端脚本里让 Cursor 跑任务,不想每次切窗口
    • 远程(手机/平板)给 Cursor 发 prompt ,回来打开 IDE 会话还在
    • 批量操作:切项目 → 切模型 → 发任务 → 截图存档,一套脚本搞定

    之前用 Cursor CLI 方案,四个问题一直没解决:上下文断裂( CLI 和 IDE 是两个会话)、展示效果差、粒度粗、配置麻烦。

    原理

    Cursor 是 Electron 应用 → 底层是 Chromium → 支持 CDP ( Chrome DevTools Protocol )。

    启动时加个参数:

    Cursor.exe --remote-debugging-port=9226
    

    然后通过 WebSocket 连上去,就能 Runtime.evaluate 执行 JS 、Input.insertText 输入文本、Input.dispatchMouseEvent 模拟点击、Page.captureScreenshot 截图。

    本质上就是把 F12 开发者工具能干的事,封装成了终端命令。

    11 条命令

    opencli cursor status          # 检查 CDP 连接
    opencli cursor list            # 列出所有窗口(自动识别 Agent/Editor )
    opencli cursor send "prompt"   # 发 Prompt 并等待 AI 回复
    opencli cursor read            # 读取当前对话内容
    opencli cursor model           # 列出可用模型
    opencli cursor model-switch "sonnet"  # 模糊匹配切模型
    opencli cursor history         # 翻历史会话
    opencli cursor screenshot      # 截图保存 PNG
    opencli cursor new-chat        # 新建对话
    opencli cursor project         # 列出项目( Agent 窗口)
    opencli cursor project-switch "my-app" # 切项目
    

    所有命令自动适配 Cursor 3 的 Agent 独立窗口和传统 Editor 嵌入式聊天面板,不需要手动指定窗口类型。

    实际效果

    列出窗口:

    ┌─────┬────────┬──────────────────────────────────────┐
    │ Idx │ Type   │ Title                                │
    ├─────┼────────┼──────────────────────────────────────┤
    │ 1   │ Editor │ blog-openclaw-soul.md - cursor-agent │
    │ 2   │ Agent  │ Cursor Agents                        │
    │ 3   │ Editor │ SKILL.md - ai-pipeline               │
    └─────┴────────┴──────────────────────────────────────┘
    

    切模型:

    ┌─────┬────────────┬────────┬─────────┐
    │ Idx │ Name       │ Tier   │ Current │
    ├─────┼────────────┼────────┼─────────┤
    │ 1   │ Composer 2 │ Fast   │         │
    │ 2   │ Sonnet 4.6 │ Medium │         │
    │ 3   │ Opus 4.6   │ High   │ ✓       │
    └─────┴────────────┴────────┴─────────┘
    

    和 CLI 方案的本质区别

    CLI 方案的会话活在 CLI 进程里——退了就没了。

    CDP 方案的会话活在 IDE 里——你从终端、从脚本、从远程触发的每一条 prompt ,都真实存在于 Cursor IDE 的对话历史中。回到电脑前打开 Cursor ,刚才的对话就在那里,直接接着聊。

    安装

    # 一行安装,零依赖( esbuild 内联打包,不需要 npm install )
    opencli plugin install github:toheart/cursor-agent/cursor-cdp
    
    # 确认连接
    opencli cursor status
    

    前提:Cursor 需要带 --remote-debugging-port=9226 参数启动(在快捷方式里加就行,需要先关闭所有 Cursor 窗口再重启)。

    自动化示例

    #!/bin/bash
    opencli cursor project-switch "my-backend"
    opencli cursor new-chat
    opencli cursor model-switch "opus"
    opencli cursor send "分析 src/auth 模块有没有安全隐患"
    opencli cursor screenshot --output report.png
    

    五行脚本,完整的自动化编程流程。


    项目开源,基于 opencli 插件体系: https://github.com/toheart/cursor-agent

    有问题楼下聊,欢迎 Star 。

    目前尚无回复
    关于   ·   帮助文档   ·   自助推广系统   ·   博客   ·   API   ·   FAQ   ·   Solana   ·   1000 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 43ms · UTC 19:01 · PVG 03:01 · LAX 12:01 · JFK 15:01
    ♥ Do have faith in what you're doing.