mirror of
https://github.com/aiclientproxy/proxycast.git
synced 2026-09-24 23:10:56 +08:00
## Terminal AI Features - Add Terminal AI panel with command execution support - Add term_get_scrollback tool for reading terminal output history - Add terminal tool with command approval workflow - Add auto-execute mode for trusted commands - Add duplicate command detection to prevent AI loops - Improve command response format with clear success markers ## Screenshot Chat (Experimental) - Add screenshot chat window for quick AI conversations - Add global shortcut support for screenshot capture - Add experimental settings page ## Code Quality - Fix all compiler warnings with proper analysis - Remove unused imports and dead code - Add #[allow(dead_code)] for reserved/planned features - Remove deprecated terminal-plugin from recommendations ## Other Changes - Update model registry with new capabilities - Improve config observer system - Add webview panel position fields (reserved) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
5.6 KiB
5.6 KiB
Terminal Scrollback Tool 实现文档
概述
term_get_scrollback 工具允许 AI 读取终端的输出历史,而不是直接执行命令。这是一个只读工具,完全避免了 AI 重复执行命令的问题。
架构设计
后端(Rust)
-
TermScrollbackTool (
src-tauri/src/agent/tools/term_scrollback.rs)- 实现
Tooltrait - 使用响应通道机制(类似 TerminalTool)
- 通过 Tauri 事件系统与前端通信
- 实现
-
全局实例管理
- 使用
once_cell::sync::Lazy创建全局单例 - 在
setup.rs和runner.rs中初始化 AppHandle - 提供
get_term_scrollback_tool()获取全局实例
- 使用
-
Tauri 命令
agent_term_scrollback_response: 接收前端的响应
前端(TypeScript/React)
-
API 层 (
src/lib/api/agent.ts)TermScrollbackRequest: 请求类型TermScrollbackResponse: 响应类型sendTermScrollbackResponse(): 发送响应函数
-
Hook 层 (
src/components/terminal/ai/useTerminalAI.ts)- 监听
term_get_scrollback_request事件 - 读取终端输出历史
- 发送响应给后端
- 监听
工作流程
AI Agent
↓ (调用 term_get_scrollback 工具)
TermScrollbackTool
↓ (发送 term_get_scrollback_request 事件)
前端 useTerminalAI
↓ (读取终端输出)
↓ (调用 sendTermScrollbackResponse)
Tauri 命令 agent_term_scrollback_response
↓ (调用 handle_term_scrollback_response)
TermScrollbackTool
↓ (通过响应通道返回结果)
AI Agent (收到终端输出)
工具参数
{
"session_id": "终端会话 ID",
"line_start": 0, // 可选,起始行号(从 0 开始)
"count": 50 // 可选,读取行数
}
工具描述(给 AI 的说明)
Read terminal output history without executing commands.
This tool allows you to view the terminal's scrollback buffer (output history)
without executing any commands. Use this to:
- Check the results of previously executed commands
- Review terminal output before suggesting next steps
- Understand the current state of the terminal session
Parameters:
- session_id: Terminal session ID (required)
- line_start: Starting line number (optional, default: 0)
- count: Number of lines to read (optional, default: all lines)
Returns the terminal output as plain text.
IMPORTANT: This is a READ-ONLY tool. It does NOT execute commands.
使用场景
场景 1:纯只读模式(推荐)
配置:
- 移除
terminal工具 - 只保留
term_get_scrollback工具
优势:
- AI 只能读取输出,不能执行命令
- 完全避免重复执行问题
- 用户完全控制命令执行
工作流程:
- 用户手动在终端执行命令
- AI 使用
term_get_scrollback读取输出 - AI 根据输出提供建议
- 用户决定是否执行 AI 的建议
场景 2:混合模式(当前实现)
配置:
- 保留
terminal工具(需要审批) - 添加
term_get_scrollback工具
优势:
- AI 可以建议命令(需要审批)
- AI 也可以读取历史输出
- 灵活性更高
工作流程:
- AI 建议命令(通过
terminal工具) - 用户审批并执行
- AI 使用
term_get_scrollback读取输出 - AI 根据输出继续工作
测试步骤
1. 编译项目
cargo build --manifest-path src-tauri/Cargo.toml
2. 启动应用
npm run tauri dev
3. 测试工具
- 打开终端
- 执行一些命令(例如:
ls,pwd,echo hello) - 打开 AI 面板
- 发送消息:
请读取终端的输出历史 - AI 应该使用
term_get_scrollback工具读取输出 - 检查 AI 是否正确显示了终端输出
4. 验证日志
后端日志:
[TermScrollbackTool] 创建全局实例
[TermScrollbackTool] 设置全局 AppHandle
[TermScrollbackTool] AppHandle 设置成功,已验证
[TermScrollbackTool] 请求获取滚动缓冲区: session_id=xxx, request_id=xxx
[TermScrollbackTool] 已发送请求到前端: xxx
[TermScrollbackTool] 收到响应: request_id=xxx, success=true
前端日志:
[useTerminalAI] 收到终端滚动缓冲区请求: {...}
[useTerminalAI] 已发送滚动缓冲区响应: 0-50/100 行
故障排查
问题 1:AppHandle 未设置
症状:
[TermScrollbackTool] 警告:AppHandle 未设置!工具将无法正常工作
解决方案:
- 检查
setup.rs和runner.rs中是否调用了set_term_scrollback_tool_app_handle() - 确保在工具注册之前设置 AppHandle
问题 2:前端未收到事件
症状:
- 后端发送了事件,但前端没有日志
解决方案:
- 检查
useTerminalAI是否正确监听term_get_scrollback_request事件 - 确保
terminalSessionId不为空 - 检查浏览器控制台是否有错误
问题 3:响应超时
症状:
[TermScrollbackTool] 请求超时: xxx
解决方案:
- 检查前端是否正确发送响应
- 增加超时时间:
TermScrollbackTool::new().with_timeout(60) - 检查
getTerminalOutput()函数是否正常工作
下一步改进
-
添加过滤功能
- 支持正则表达式过滤
- 支持关键词搜索
-
添加格式化选项
- 支持 ANSI 颜色代码
- 支持纯文本输出
-
添加缓存机制
- 缓存最近的输出
- 减少重复读取
-
添加增量读取
- 只读取新增的输出
- 支持实时监控