User description
背景
Refs #579。#579 要的「语音快捷键触发 Claude Code / OpenCode Agent」中,Claude Code 后端已随 fast-agent / less-computer 落地,但 OpenCode 后端只有占位:coding_agent_provider 已有 opencode-cli 选项(标「即将支持」),但后端从不读它、永远跑 claude。本 PR 把 OpenCode 真正接上,照既有 coding_agent 适配模式实现,不另造架构。
改动清单
后端
coding_agent/opencode.rs(新增):OpenCode 适配器,与 Claude 适配器同形、复用同一套 CodingAgentRequest / CodingAgentEvent / CodingAgentError:
detect_opencode:opencode --version(复用 parse_claude_version 取 x.y.z)。
build_opencode_args:opencode run --format json --model <provider/model> --dir <cwd> [--dangerously-skip-permissions];prompt 作为 argv 位置参数(OpenCode 从命令行读,不像 claude 从 stdin)。
parse_opencode_json_line:解析 OpenCode NDJSON(text → Delta、tool_use → ToolUse、error → Error);OpenCode 的 text 是完整文本块且无带 cost 的终局 result 事件,故累计文本、EOF 处合成 Completed(cost_usd = None)。
run_opencode_agent:取消/超时 kill 子进程;护栏经 OPENCODE_CONFIG_CONTENT 注入。
coding_agent/args.rs:CodingAgentProvider 枚举(claude-code-cli / opencode-cli,未知回落 Claude)+ from_pref / default_exe。
coding_agent/guard.rs:build_opencode_guard_config —— 把高风险 bash 前缀翻译成 OpenCode permission.bash 的 deny glob(如 "rm -rf *": "deny")+ webfetch: deny,审批放行的前缀显式 allow。这是 Claude --settings deny 护栏在 OpenCode 的等价物。
coordinator/dictation_voice_agent.rs:run_less_computer_once 按 coding_agent_provider 分派 Claude / OpenCode 运行器。两条路径都 fail-closed(护栏配置生成失败即中止,绝不无护栏裸跑)。审批拦截探测 + 重跑放行链路本就 provider 无关,自动复用(OpenCode 重跑时把放行前缀传入 guard builder)。
coding_agent/commands.rs + lib.rs:coding_agent_detect_opencode 命令。
前端
CodingAgentSection.tsx:OpenCode 选项从「即将支持」改为可用;选中 OpenCode 后端时探测安装状态并提示(已安装显示版本 / 未安装提示先 npm i -g opencode-ai + opencode auth login)。
lib/ipc.ts:codingAgentDetectOpencode + OpenCodeDetection。
- i18n:
opencodeReady / opencodeMissing(zh-CN / en / ja / ko / zh-TW;替换原 providerOpenCodeSoon)。
安全
- 沿用语音→shell 路径既有安全姿态:
bypassPermissions 在语音路径降级为 acceptEdits 等价(保留护栏);OpenCode 用 permission deny 高风险 bash + 禁 webfetch,fail-closed。
- prompt 不进 argv 泄露? 注:OpenCode 设计上 prompt 走 argv(CLI 约定),这与 claude 走 stdin 不同 —— 会出现在进程列表里。语音转写一般非敏感,但已在 PR 描述标注;如需可后续探索
opencode serve + stdin 方案。
测试情况
cargo check(macOS)✅
cargo test --lib:492 通过(新增 10 个单测:provider 解析/默认 exe、OpenCode args/stream 解析、OpenCode 护栏 deny/放行)✅
npm run build(tsc + vite)✅
- [ ] 真机实测待办:① 安装
opencode 并 opencode auth login;② 设置→Less Computer 选 OpenCode 后端,确认安装提示正确;③ 语音触发一次任务,确认流式输出进聊天浮窗、能落地改动;④ 触发一次高风险命令(如 rm -rf)确认被护栏拦截并弹审批卡,Approve 后重跑放行。
为什么是 Refs 而非 Closes #579
#579 还含「语音润色模型配置」一项:prefs 字段(coding_agent_use_voice_polish / voice_polish_model_mode / voice_polish_provider_id / voice_polish_thinking_enabled)已存在但全链路未实现——语音 Agent 当前把原始转写直接送 agent,未经语音润色模型整理,也无对应 UI。该项不在本 PR 范围,故用 Refs #579。建议作为 #579 的剩余子项单独跟进。
🤖 Generated with Claude Code
PR Type
Enhancement, Tests
Description
-
OpenCode CLI 适配器(#579)
-
Agent 可选 OpenCode 后端
-
护栏配置双重路径
-
前端配置与检测提示
File Walkthrough
| Relevant files |
|---|
| Enhancement | 11 files
args.rsAdd CodingAgentProvider enum and prefs parsing |
+55/-0 |
commands.rsAdd coding_agent_detect_opencode command |
+29/-0 |
guard.rsAdd OpenCode guard config builder |
+72/-0 |
mod.rsRe-export opencode module and helpers |
+8/-4 |
opencode.rsNew OpenCode CLI adapter module |
+369/-0 |
dictation_voice_agent.rsDispatch agent runner by provider |
+106/-54 |
lib.rsRegister coding_agent_detect_opencode command |
+1/-0 |
types.rsAdd coding_agent_exe preference field |
+9/-0 |
ipc.tsAdd OpenCodeDetection interface and detect function |
+21/-0 |
types.tsAdd coding_agent_exe to UserPreferences |
+2/-0 |
CodingAgentSection.tsxAdd OpenCode detection hint and exe config |
+51/-1 |
|
| I18n | 5 files
en.tsAdd OpenCode detection i18n keys |
+3/-1 |
ja.tsAdd OpenCode detection i18n keys |
+3/-1 |
ko.tsAdd OpenCode detection i18n keys |
+3/-1 |
zh-CN.tsAdd OpenCode detection i18n keys |
+3/-1 |
zh-TW.tsAdd OpenCode detection i18n keys |
+3/-1 |
|
| Tests | 1 files
stylePrefs.test.tsUpdate test default with coding_agent_exe |
+1/-0 |
|
PR Reviewer Guide 🔍
(Review updated until commit https://github.com/Open-Less/openless/commit/331bafe33b51bef89d13a708b87e53be2da2d2ac)
Here are some key observations to aid the review process:
|
🎫 Ticket compliance analysis 🔶
579 - Partially compliant
Compliant requirements:
- OpenCode CLI 适配器实现(opencode.rs):检测、参数构建、NDJSON 解析、运行器。
- 护栏配置函数 build_opencode_guard_config:高风险 bash 前缀 deny、webfetch deny、文件级 deny。
- Provider 枚举 CodingAgentProvider 及 from_pref / default_exe。
- 检测命令 coding_agent_detect_opencode 并返回版本/安装状态。
- 设置页添加 OpenCode 选项,并显示安装/未安装提示。
- 可执行文件路径配置字段 coding_agent_exe 前后端。
- 权限模式映射:Plan 时不传 --dangerously-skip-permissions,其他模式传。
Non-compliant requirements:
- 语音润色大语言模型配置(voice polish model mode / custom provider etc.)。
- Voice Coding Agent 前端面板(VoiceCodingAgentPanel.tsx)。
- 语音 Agent 热键触发状态机(协调器部分未改动)。
- 历史记录、超时/取消前端处理(后端已实现但前端无对应 UI)。
Requires further human verification:
- OpenCode 权限模式 --dangerously-skip-permissions 与 guard 配置的交互行为:该 flag 是否完全绕过权限系统,导致护栏配置被忽略,需要实际测试验证。
- OpenCode NDJSON 输出格式是否与解析匹配(text 块是否完整?是否有其他字段?)。
- 文件级 deny 中的 tilde 表达式(~/Library/LaunchAgents/**)在 OpenCode 中是否支持。
|
| ⏱️ Estimated effort to review: 3 🔵🔵🔵⚪⚪ |
| 🧪 PR contains tests |
🔒 Security concerns
Sensitive information exposure: The prompt is passed as a command-line argument via .arg(&req.prompt). On Linux, command arguments may be visible to other processes via /proc/self/cmdline. This could leak sensitive user prompts. Consider passing prompt via stdin (like the Claude adapter does) or using platform-specific argument hiding mechanisms. Additionally, the --dangerously-skip-permissions flag may bypass the guard config's deny rules (see key issue above), which is a security concern that requires verification against OpenCode's actual behavior. |
⚡ Recommended focus areas for review
Missing final event on error
In run_opencode_agent, when an error event is parsed from the NDJSON stream (e.g., agent returns an error), the code sets got_error = true and sends the error event to the sink. However, after EOF, the function checks outcome.is_ok() and then conditionally sends a Completed or Error event only if status.success() && !got_error, or if !status.success() && !got_error. When got_error is true but the process exits successfully (status.success() == true), neither branch executes, and the function returns Ok(()) without sending any final event (Completed or Error) to the sink. This leaves the frontend without a terminal event, potentially causing the session to hang indefinitely.
loop {
tokio::select! {
biased;
_ = wait_cancel(&cancel) => {
let _ = child.start_kill();
let _ = sink.send(CodingAgentEvent::Cancelled { session_id: req.session_id.clone() });
outcome = Err(CodingAgentError::Cancelled);
break;
}
_ = tokio::time::sleep_until(deadline) => {
let _ = child.start_kill();
let _ = sink.send(CodingAgentEvent::Error {
session_id: req.session_id.clone(),
message: format!("运行超时({}s)", req.timeout_secs),
});
got_error = true;
outcome = Err(CodingAgentError::Timeout(req.timeout_secs));
break;
}
line = lines.next_line() => {
match line {
Ok(Some(l)) => {
if let Some(ev) = parse_opencode_json_line(&req.session_id, &l) {
match &ev {
CodingAgentEvent::Delta { text, .. } => accumulated.push_str(text),
CodingAgentEvent::Error { .. } => got_error = true,
_ => {}
}
let _ = sink.send(ev);
}
}
Ok(None) => break, // EOF:正常结束
Err(e) => {
outcome = Err(CodingAgentError::Io(e.to_string()));
break;
}
}
}
}
}
let status = child
.wait()
.await
.map_err(|e| CodingAgentError::Io(e.to_string()))?;
// 正常 EOF 收尾(没取消/超时/IO 错):进程成功且没报错 → 合成 Completed。
if outcome.is_ok() {
if status.success() && !got_error {
let _ = sink.send(CodingAgentEvent::Completed {
session_id: req.session_id.clone(),
text: accumulated.trim().to_string(),
cost_usd: None,
duration_ms: None,
});
return Ok(());
}
if !status.success() && !got_error {
// 非 0 退出且没解析到 error 事件:补一条 Error(取 stderr 末行作摘要)。
let stderr = match stderr_task {
Some(t) => t.await.unwrap_or_default(),
None => String::new(),
};
let summary = stderr.lines().last().unwrap_or("").trim().to_string();
let _ = sink.send(CodingAgentEvent::Error {
session_id: req.session_id.clone(),
message: if summary.is_empty() {
format!("agent 异常退出 (code={:?})", status.code())
} else {
summary
},
});
return Err(CodingAgentError::ProcessExit(status.code()));
}
}
Security: --dangerously-skip-permissions may bypass guard config
For permission modes other than Plan, the code passes --dangerously-skip-permissions to opencode. The comment assumes that the injected guard config (via OPENCODE_CONFIG_CONTENT) still applies even with this flag. However, if OpenCode's --dangerously-skip-permissions disables all permission checks including deny rules in the config, the guard becomes ineffective. This could allow unrestricted command execution. The behavior needs to be verified against OpenCode's actual implementation before this code is used with non-Plan modes in production.
if let Some(cwd) = &req.cwd {
args.push("--dir".into());
args.push(cwd.to_string_lossy().into_owned());
}
if skip_permissions_flag(req.permission_mode) {
args.push("--dangerously-skip-permissions".into());
}
// end-of-options:其后由运行器追加的 prompt 一律按位置参数处理,不再解析成 flag。
args.push("--".into());
args
|
Persistent review updated to latest commit https://github.com/Open-Less/openless/commit/7960ac4c7c74a3d4d81b76efd27449168a6fdc71
PR 合并冲突提示
当前状态: 本 PR 与 eta 已为 CONFLICTING,合并前必须 rebase。
与其他 open PR 的重叠:
| 文件 |
相关 PR |
说明 |
| coding_agent/guard.rs |
#688 |
双方均大改此文件,#688 合入后 rebase 本 PR 需手工合并 |
| coordinator/dictation_voice_agent.rs |
— |
与当前 eta 已冲突 |
| ypes.rs、i18n/*、ipc.ts |
#666 |
改动区域不同,一般可自动合并 |
| lib.rs |
#690、#666 |
改动较小,风险低 |
建议: 先 rebase 到最新 eta;若 #688 已合,重点检查 guard.rs 与 dictation_voice_agent.rs。
Persistent review updated to latest commit https://github.com/Open-Less/openless/commit/331bafe33b51bef89d13a708b87e53be2da2d2ac