feat(ai): add OpenCode CLI provider
This commit is contained in:
1 parent
f711855ddf
commit
309e3aef5e
36 files changed
+1197
-30
No files matched your search
@@ -139,7 +139,7 @@ pub async fn run_agent_loop(
|
||||
let contract_system_prompt = augment_system_prompt_with_task_contract(system_prompt, task_contract, is_agent_mode);
|
||||
let system_prompt = contract_system_prompt.as_str();
|
||||
|
||||
if matches!(config.provider, AiProvider::CodexCli | AiProvider::ClaudeCodeCli | AiProvider::PiAgentCli) {
|
||||
if crate::ai::is_cli_provider(&config.provider) {
|
||||
let connection_name = {
|
||||
let configs = agent_ctx.state.configs.read().await;
|
||||
configs
|
||||
@@ -175,6 +175,14 @@ pub async fn run_agent_loop(
|
||||
);
|
||||
return crate::ai_pi_agent_cli::run_pi_agent(config, &prompt, options, cancelled, on_event).await;
|
||||
}
|
||||
if matches!(config.provider, AiProvider::OpenCodeCli) {
|
||||
let prompt = crate::ai_opencode_cli::build_opencode_prompt(
|
||||
system_prompt,
|
||||
messages,
|
||||
agent_ctx.sql_permissions.allow_writes,
|
||||
);
|
||||
return crate::ai_opencode_cli::run_opencode_agent(config, &prompt, options, cancelled, on_event).await;
|
||||
}
|
||||
let prompt =
|
||||
crate::ai_codex_cli::build_codex_prompt(system_prompt, messages, agent_ctx.sql_permissions.allow_writes);
|
||||
return crate::ai_codex_cli::run_codex_agent(config, &prompt, options, cancelled, on_event).await;
|
||||
|
||||
+90
-11
@@ -79,6 +79,8 @@ pub enum AiProvider {
|
||||
ClaudeCodeCli,
|
||||
#[serde(rename = "pi-agent-cli")]
|
||||
PiAgentCli,
|
||||
#[serde(rename = "opencode-cli")]
|
||||
OpenCodeCli,
|
||||
Custom,
|
||||
}
|
||||
|
||||
@@ -96,6 +98,7 @@ impl AiProvider {
|
||||
AiProvider::OpenaiCompatible => "openai-compatible",
|
||||
AiProvider::ClaudeCodeCli => "claude-code-cli",
|
||||
AiProvider::PiAgentCli => "pi-agent-cli",
|
||||
AiProvider::OpenCodeCli => "opencode-cli",
|
||||
AiProvider::CodexCli => "codex-cli",
|
||||
AiProvider::Custom => "custom",
|
||||
}
|
||||
@@ -373,6 +376,10 @@ pub struct AiConfig {
|
||||
pub pi_agent_cli_path: Option<String>,
|
||||
#[serde(default)]
|
||||
pub pi_agent_cli_env: HashMap<String, String>,
|
||||
#[serde(default)]
|
||||
pub opencode_cli_path: Option<String>,
|
||||
#[serde(default)]
|
||||
pub opencode_cli_env: HashMap<String, String>,
|
||||
}
|
||||
|
||||
fn default_enable_thinking() -> bool {
|
||||
@@ -380,10 +387,13 @@ fn default_enable_thinking() -> bool {
|
||||
}
|
||||
|
||||
/// Whether the provider is a CLI-based provider that goes through its own
|
||||
/// executable (claude-code, codex, pi) rather than through `with_retry` /
|
||||
/// executable (claude-code, codex, opencode, pi) rather than through `with_retry` /
|
||||
/// `with_stream_retry`.
|
||||
pub fn is_cli_provider(provider: &AiProvider) -> bool {
|
||||
matches!(provider, AiProvider::CodexCli | AiProvider::ClaudeCodeCli | AiProvider::PiAgentCli)
|
||||
matches!(
|
||||
provider,
|
||||
AiProvider::CodexCli | AiProvider::ClaudeCodeCli | AiProvider::PiAgentCli | AiProvider::OpenCodeCli
|
||||
)
|
||||
}
|
||||
|
||||
/// Merge the global `max_retries` setting into an `AiConfig`.
|
||||
@@ -612,6 +622,7 @@ pub fn resolve_endpoint(config: &AiConfig) -> String {
|
||||
| AiProvider::CodexCli
|
||||
| AiProvider::ClaudeCodeCli
|
||||
| AiProvider::PiAgentCli
|
||||
| AiProvider::OpenCodeCli
|
||||
| AiProvider::Gemini => unreachable!(),
|
||||
}
|
||||
}
|
||||
@@ -1064,7 +1075,7 @@ fn normalized_api_key(config: &AiConfig) -> &str {
|
||||
|
||||
fn validate_config(config: &AiConfig) -> Result<(), String> {
|
||||
crate::ai_effort::validate_runtime_effort(config)?;
|
||||
if matches!(config.provider, AiProvider::CodexCli | AiProvider::ClaudeCodeCli | AiProvider::PiAgentCli) {
|
||||
if is_cli_provider(&config.provider) {
|
||||
return Ok(());
|
||||
}
|
||||
if provider_requires_api_key(&config.provider) && config.api_key.trim().is_empty() {
|
||||
@@ -1080,7 +1091,7 @@ fn validate_config(config: &AiConfig) -> Result<(), String> {
|
||||
}
|
||||
|
||||
fn validate_model_list_config(config: &AiConfig) -> Result<(), String> {
|
||||
if matches!(config.provider, AiProvider::CodexCli | AiProvider::ClaudeCodeCli | AiProvider::PiAgentCli) {
|
||||
if is_cli_provider(&config.provider) {
|
||||
return Ok(());
|
||||
}
|
||||
if provider_requires_api_key(&config.provider) && config.api_key.trim().is_empty() {
|
||||
@@ -1459,6 +1470,7 @@ pub async fn list_models_core(config: &AiConfig) -> Result<Vec<AiModelInfo>, Str
|
||||
AiProvider::CodexCli => crate::ai_codex_cli::list_codex_models(config).await?,
|
||||
AiProvider::ClaudeCodeCli => crate::ai_claude_code_cli::list_claude_code_models(config).await?,
|
||||
AiProvider::PiAgentCli => crate::ai_pi_agent_cli::list_pi_agent_models(config).await?,
|
||||
AiProvider::OpenCodeCli => crate::ai_opencode_cli::list_opencode_models(config).await?,
|
||||
_ => {
|
||||
validate_model_list_config(config)?;
|
||||
let client = build_ai_http_client(config, 30)?;
|
||||
@@ -1481,7 +1493,9 @@ pub async fn list_models_core(config: &AiConfig) -> Result<Vec<AiModelInfo>, Str
|
||||
list_openai_compatible_models(&client, config).await?
|
||||
}
|
||||
}
|
||||
AiProvider::CodexCli | AiProvider::ClaudeCodeCli | AiProvider::PiAgentCli => unreachable!(),
|
||||
AiProvider::CodexCli | AiProvider::ClaudeCodeCli | AiProvider::PiAgentCli | AiProvider::OpenCodeCli => {
|
||||
unreachable!()
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
@@ -1500,6 +1514,10 @@ pub async fn resolve_model_effort_core(config: &AiConfig, model_id: &str) -> Res
|
||||
return crate::ai_pi_agent_cli::resolve_pi_agent_model_effort(config, model_id).await;
|
||||
}
|
||||
|
||||
if matches!(config.provider, AiProvider::OpenCodeCli) {
|
||||
return crate::ai_opencode_cli::resolve_opencode_model_effort(config, model_id).await;
|
||||
}
|
||||
|
||||
if matches!(config.provider, AiProvider::CodexCli | AiProvider::ClaudeCodeCli) {
|
||||
let models = list_models_core(config).await?;
|
||||
return Ok(models
|
||||
@@ -2028,6 +2046,9 @@ pub async fn test_connection_core(config: &AiConfig) -> Result<AiTestConnectionR
|
||||
if matches!(config.provider, AiProvider::PiAgentCli) {
|
||||
return crate::ai_pi_agent_cli::test_pi_agent_connection(config).await;
|
||||
}
|
||||
if matches!(config.provider, AiProvider::OpenCodeCli) {
|
||||
return crate::ai_opencode_cli::test_opencode_connection(config).await;
|
||||
}
|
||||
let mut resolved_config = config.clone();
|
||||
if resolved_config.model.trim().is_empty() {
|
||||
let model = list_models_core(&resolved_config)
|
||||
@@ -2383,7 +2404,7 @@ where
|
||||
pub async fn complete(request: &AiCompletionRequest) -> Result<String, String> {
|
||||
validate_config(&request.config)?;
|
||||
|
||||
if matches!(request.config.provider, AiProvider::CodexCli | AiProvider::ClaudeCodeCli | AiProvider::PiAgentCli) {
|
||||
if is_cli_provider(&request.config.provider) {
|
||||
return Err("CLI providers are only supported in DBX AI agent mode".to_string());
|
||||
}
|
||||
|
||||
@@ -2400,7 +2421,9 @@ pub async fn complete(request: &AiCompletionRequest) -> Result<String, String> {
|
||||
|
||||
match request.config.provider {
|
||||
AiProvider::Gemini => call_gemini(&client, request).await,
|
||||
AiProvider::CodexCli | AiProvider::ClaudeCodeCli | AiProvider::PiAgentCli => unreachable!(),
|
||||
AiProvider::CodexCli | AiProvider::ClaudeCodeCli | AiProvider::PiAgentCli | AiProvider::OpenCodeCli => {
|
||||
unreachable!()
|
||||
}
|
||||
AiProvider::Openai
|
||||
| AiProvider::Deepseek
|
||||
| AiProvider::Qwen
|
||||
@@ -2439,7 +2462,7 @@ pub async fn stream(
|
||||
) -> Result<(), String> {
|
||||
validate_config(&request.config)?;
|
||||
|
||||
if matches!(request.config.provider, AiProvider::CodexCli | AiProvider::ClaudeCodeCli | AiProvider::PiAgentCli) {
|
||||
if is_cli_provider(&request.config.provider) {
|
||||
return Err("CLI providers are only supported in DBX AI agent mode".to_string());
|
||||
}
|
||||
|
||||
@@ -2452,7 +2475,9 @@ pub async fn stream(
|
||||
|
||||
match request.config.provider {
|
||||
AiProvider::Gemini => stream_gemini(&client, session_id, request, cancelled, &on_chunk).await,
|
||||
AiProvider::CodexCli | AiProvider::ClaudeCodeCli | AiProvider::PiAgentCli => unreachable!(),
|
||||
AiProvider::CodexCli | AiProvider::ClaudeCodeCli | AiProvider::PiAgentCli | AiProvider::OpenCodeCli => {
|
||||
unreachable!()
|
||||
}
|
||||
AiProvider::Openai
|
||||
| AiProvider::Deepseek
|
||||
| AiProvider::Qwen
|
||||
@@ -3736,7 +3761,7 @@ pub async fn stream_with_tools(
|
||||
on_chunk: impl Fn(AiStreamChunk),
|
||||
) -> Result<(Vec<crate::agent_events::ToolCall>, Option<TokenUsage>), String> {
|
||||
validate_config(config)?;
|
||||
if matches!(config.provider, AiProvider::CodexCli | AiProvider::ClaudeCodeCli | AiProvider::PiAgentCli) {
|
||||
if is_cli_provider(&config.provider) {
|
||||
return Err("CLI providers are only supported through the DBX AI agent loop".to_string());
|
||||
}
|
||||
|
||||
@@ -4088,6 +4113,8 @@ mod tests {
|
||||
claude_code_cli_env: Default::default(),
|
||||
pi_agent_cli_path: None,
|
||||
pi_agent_cli_env: Default::default(),
|
||||
opencode_cli_path: None,
|
||||
opencode_cli_env: Default::default(),
|
||||
},
|
||||
system_prompt: "Be concise.".to_string(),
|
||||
messages: vec![AiMessage {
|
||||
@@ -4669,6 +4696,8 @@ mod tests {
|
||||
assert!(config.codex_cli_env.is_empty());
|
||||
assert!(config.pi_agent_cli_path.is_none());
|
||||
assert!(config.pi_agent_cli_env.is_empty());
|
||||
assert!(config.opencode_cli_path.is_none());
|
||||
assert!(config.opencode_cli_env.is_empty());
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -4694,6 +4723,8 @@ mod tests {
|
||||
claude_code_cli_env: Default::default(),
|
||||
pi_agent_cli_path: None,
|
||||
pi_agent_cli_env: Default::default(),
|
||||
opencode_cli_path: None,
|
||||
opencode_cli_env: Default::default(),
|
||||
};
|
||||
|
||||
let err = build_ai_http_client(&config, 1).unwrap_err();
|
||||
@@ -4724,6 +4755,8 @@ mod tests {
|
||||
claude_code_cli_env: Default::default(),
|
||||
pi_agent_cli_path: None,
|
||||
pi_agent_cli_env: Default::default(),
|
||||
opencode_cli_path: None,
|
||||
opencode_cli_env: Default::default(),
|
||||
};
|
||||
|
||||
build_ai_http_client(&config, 1).unwrap();
|
||||
@@ -4752,6 +4785,8 @@ mod tests {
|
||||
claude_code_cli_env: Default::default(),
|
||||
pi_agent_cli_path: None,
|
||||
pi_agent_cli_env: Default::default(),
|
||||
opencode_cli_path: None,
|
||||
opencode_cli_env: Default::default(),
|
||||
};
|
||||
|
||||
build_ai_http_client(&config, 1).unwrap();
|
||||
@@ -4780,6 +4815,8 @@ mod tests {
|
||||
claude_code_cli_env: Default::default(),
|
||||
pi_agent_cli_path: None,
|
||||
pi_agent_cli_env: Default::default(),
|
||||
opencode_cli_path: None,
|
||||
opencode_cli_env: Default::default(),
|
||||
};
|
||||
|
||||
assert_eq!(
|
||||
@@ -4812,6 +4849,8 @@ mod tests {
|
||||
claude_code_cli_env: Default::default(),
|
||||
pi_agent_cli_path: None,
|
||||
pi_agent_cli_env: Default::default(),
|
||||
opencode_cli_path: None,
|
||||
opencode_cli_env: Default::default(),
|
||||
};
|
||||
|
||||
assert_eq!(resolve_endpoint(&ollama), "http://localhost:11434/v1/chat/completions");
|
||||
@@ -4841,6 +4880,8 @@ mod tests {
|
||||
claude_code_cli_env: Default::default(),
|
||||
pi_agent_cli_path: None,
|
||||
pi_agent_cli_env: Default::default(),
|
||||
opencode_cli_path: None,
|
||||
opencode_cli_env: Default::default(),
|
||||
};
|
||||
|
||||
for provider in
|
||||
@@ -4889,6 +4930,8 @@ mod tests {
|
||||
claude_code_cli_env: Default::default(),
|
||||
pi_agent_cli_path: None,
|
||||
pi_agent_cli_env: Default::default(),
|
||||
opencode_cli_path: None,
|
||||
opencode_cli_env: Default::default(),
|
||||
};
|
||||
assert_eq!(resolve_model_list_endpoint(&openai).unwrap(), "https://api.openai.com/v1/models");
|
||||
|
||||
@@ -4913,6 +4956,8 @@ mod tests {
|
||||
claude_code_cli_env: Default::default(),
|
||||
pi_agent_cli_path: None,
|
||||
pi_agent_cli_env: Default::default(),
|
||||
opencode_cli_path: None,
|
||||
opencode_cli_env: Default::default(),
|
||||
};
|
||||
assert_eq!(resolve_model_list_endpoint(&claude).unwrap(), "https://api.anthropic.com/v1/models");
|
||||
}
|
||||
@@ -4940,6 +4985,8 @@ mod tests {
|
||||
claude_code_cli_env: Default::default(),
|
||||
pi_agent_cli_path: None,
|
||||
pi_agent_cli_env: Default::default(),
|
||||
opencode_cli_path: None,
|
||||
opencode_cli_env: Default::default(),
|
||||
};
|
||||
|
||||
assert!(uses_anthropic_messages_api(&config));
|
||||
@@ -5019,6 +5066,8 @@ mod tests {
|
||||
claude_code_cli_env: Default::default(),
|
||||
pi_agent_cli_path: None,
|
||||
pi_agent_cli_env: Default::default(),
|
||||
opencode_cli_path: None,
|
||||
opencode_cli_env: Default::default(),
|
||||
};
|
||||
|
||||
assert!(!uses_anthropic_messages_api(&config));
|
||||
@@ -5058,6 +5107,8 @@ mod tests {
|
||||
claude_code_cli_env: Default::default(),
|
||||
pi_agent_cli_path: None,
|
||||
pi_agent_cli_env: Default::default(),
|
||||
opencode_cli_path: None,
|
||||
opencode_cli_env: Default::default(),
|
||||
};
|
||||
assert_eq!(resolve_endpoint(&config), "https://api.example.com/v1/chat/completions");
|
||||
assert_eq!(resolve_model_list_endpoint(&config).unwrap(), "https://api.example.com/v1/models");
|
||||
@@ -5121,6 +5172,8 @@ mod tests {
|
||||
claude_code_cli_env: Default::default(),
|
||||
pi_agent_cli_path: None,
|
||||
pi_agent_cli_env: Default::default(),
|
||||
opencode_cli_path: None,
|
||||
opencode_cli_env: Default::default(),
|
||||
};
|
||||
|
||||
assert_eq!(resolve_endpoint(&config), "https://api.openai.com/v1/responses");
|
||||
@@ -5156,6 +5209,8 @@ mod tests {
|
||||
claude_code_cli_env: Default::default(),
|
||||
pi_agent_cli_path: None,
|
||||
pi_agent_cli_env: Default::default(),
|
||||
opencode_cli_path: None,
|
||||
opencode_cli_env: Default::default(),
|
||||
};
|
||||
|
||||
let api_key_headers = claude_headers(&config).unwrap();
|
||||
@@ -5271,6 +5326,8 @@ mod tests {
|
||||
claude_code_cli_env: Default::default(),
|
||||
pi_agent_cli_path: None,
|
||||
pi_agent_cli_env: Default::default(),
|
||||
opencode_cli_path: None,
|
||||
opencode_cli_env: Default::default(),
|
||||
};
|
||||
assert_eq!(resolve_ollama_show_endpoint(&config).unwrap(), "http://localhost:11434/api/show");
|
||||
|
||||
@@ -5303,6 +5360,8 @@ mod tests {
|
||||
claude_code_cli_env: Default::default(),
|
||||
pi_agent_cli_path: None,
|
||||
pi_agent_cli_env: Default::default(),
|
||||
opencode_cli_path: None,
|
||||
opencode_cli_env: Default::default(),
|
||||
};
|
||||
|
||||
assert_eq!(ollama_selected_model_tool_support(&config).await.unwrap(), Some(true));
|
||||
@@ -5384,6 +5443,8 @@ mod tests {
|
||||
claude_code_cli_env: Default::default(),
|
||||
pi_agent_cli_path: None,
|
||||
pi_agent_cli_env: Default::default(),
|
||||
opencode_cli_path: None,
|
||||
opencode_cli_env: Default::default(),
|
||||
};
|
||||
let models = vec![
|
||||
AiModelInfo::new("qwen3:0.6b", None),
|
||||
@@ -5645,6 +5706,8 @@ mod tests {
|
||||
claude_code_cli_env: Default::default(),
|
||||
pi_agent_cli_path: None,
|
||||
pi_agent_cli_env: Default::default(),
|
||||
opencode_cli_path: None,
|
||||
opencode_cli_env: Default::default(),
|
||||
};
|
||||
|
||||
let mut body = serde_json::json!({
|
||||
@@ -5856,6 +5919,8 @@ mod tests {
|
||||
claude_code_cli_env: Default::default(),
|
||||
pi_agent_cli_path: None,
|
||||
pi_agent_cli_env: Default::default(),
|
||||
opencode_cli_path: None,
|
||||
opencode_cli_env: Default::default(),
|
||||
};
|
||||
let mut body = serde_json::json!({
|
||||
"model": &config.model,
|
||||
@@ -5893,6 +5958,8 @@ mod tests {
|
||||
claude_code_cli_env: Default::default(),
|
||||
pi_agent_cli_path: None,
|
||||
pi_agent_cli_env: Default::default(),
|
||||
opencode_cli_path: None,
|
||||
opencode_cli_env: Default::default(),
|
||||
};
|
||||
let mut body = serde_json::json!({ "model": &config.model });
|
||||
|
||||
@@ -5936,6 +6003,8 @@ mod tests {
|
||||
claude_code_cli_env: Default::default(),
|
||||
pi_agent_cli_path: None,
|
||||
pi_agent_cli_env: Default::default(),
|
||||
opencode_cli_path: None,
|
||||
opencode_cli_env: Default::default(),
|
||||
};
|
||||
let mut body = serde_json::json!({ "model": &config.model });
|
||||
|
||||
@@ -5973,6 +6042,8 @@ mod tests {
|
||||
claude_code_cli_env: Default::default(),
|
||||
pi_agent_cli_path: None,
|
||||
pi_agent_cli_env: Default::default(),
|
||||
opencode_cli_path: None,
|
||||
opencode_cli_env: Default::default(),
|
||||
};
|
||||
let mut body = serde_json::json!({ "model": &config.model });
|
||||
|
||||
@@ -6006,6 +6077,8 @@ mod tests {
|
||||
claude_code_cli_env: Default::default(),
|
||||
pi_agent_cli_path: None,
|
||||
pi_agent_cli_env: Default::default(),
|
||||
opencode_cli_path: None,
|
||||
opencode_cli_env: Default::default(),
|
||||
};
|
||||
let mut body = serde_json::json!({ "model": &config.model });
|
||||
|
||||
@@ -6070,6 +6143,8 @@ mod tests {
|
||||
claude_code_cli_env: Default::default(),
|
||||
pi_agent_cli_path: None,
|
||||
pi_agent_cli_env: Default::default(),
|
||||
opencode_cli_path: None,
|
||||
opencode_cli_env: Default::default(),
|
||||
};
|
||||
let request = AiCompletionRequest {
|
||||
config: config.clone(),
|
||||
@@ -6128,6 +6203,8 @@ mod tests {
|
||||
claude_code_cli_env: Default::default(),
|
||||
pi_agent_cli_path: None,
|
||||
pi_agent_cli_env: Default::default(),
|
||||
opencode_cli_path: None,
|
||||
opencode_cli_env: Default::default(),
|
||||
};
|
||||
let mut body = serde_json::json!({
|
||||
"model": &config.model,
|
||||
@@ -6654,7 +6731,9 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn merge_global_max_retries_skips_cli_providers() {
|
||||
for provider in [AiProvider::CodexCli, AiProvider::ClaudeCodeCli, AiProvider::PiAgentCli] {
|
||||
for provider in
|
||||
[AiProvider::CodexCli, AiProvider::ClaudeCodeCli, AiProvider::PiAgentCli, AiProvider::OpenCodeCli]
|
||||
{
|
||||
let mut config = test_config(provider.clone());
|
||||
config.max_retries = None;
|
||||
merge_global_max_retries(&mut config, 0);
|
||||
|
||||
@@ -525,6 +525,7 @@ pub async fn run_claude_code_agent(
|
||||
CliAgentProcessSpec {
|
||||
command,
|
||||
env,
|
||||
env_remove: Vec::new(),
|
||||
current_dir: Some(isolated_cwd.path.clone()),
|
||||
stdin: Some(prompt.to_string()),
|
||||
dialect: CliAgentJsonlDialect::ClaudeCodePrint,
|
||||
@@ -580,6 +581,8 @@ mod tests {
|
||||
claude_code_cli_env: Default::default(),
|
||||
pi_agent_cli_path: None,
|
||||
pi_agent_cli_env: Default::default(),
|
||||
opencode_cli_path: None,
|
||||
opencode_cli_env: Default::default(),
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -34,11 +34,13 @@ pub struct CliAgentCommandSpec {
|
||||
pub enum CliAgentJsonlDialect {
|
||||
CodexExec,
|
||||
ClaudeCodePrint,
|
||||
OpenCodeRun,
|
||||
}
|
||||
|
||||
pub struct CliAgentProcessSpec {
|
||||
pub command: CliAgentCommandSpec,
|
||||
pub env: Vec<(String, String)>,
|
||||
pub env_remove: Vec<String>,
|
||||
pub current_dir: Option<PathBuf>,
|
||||
pub stdin: Option<String>,
|
||||
pub dialect: CliAgentJsonlDialect,
|
||||
@@ -217,6 +219,7 @@ struct ParsedCliAgentEvent {
|
||||
events: Vec<AgentEvent>,
|
||||
final_text: Option<String>,
|
||||
error: Option<String>,
|
||||
usage_delta: Option<TokenUsage>,
|
||||
}
|
||||
|
||||
pub fn parse_cli_jsonl_event(line: &str, dialect: CliAgentJsonlDialect) -> Option<Vec<AgentEvent>> {
|
||||
@@ -232,6 +235,7 @@ fn parse_cli_jsonl_line(line: &str, dialect: CliAgentJsonlDialect) -> ParsedCliA
|
||||
match dialect {
|
||||
CliAgentJsonlDialect::CodexExec => parse_codex_jsonl_line(line),
|
||||
CliAgentJsonlDialect::ClaudeCodePrint => parse_claude_code_jsonl_line(line),
|
||||
CliAgentJsonlDialect::OpenCodeRun => parse_open_code_jsonl_line(line),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -530,13 +534,108 @@ fn claude_code_error_message(value: &Value) -> String {
|
||||
.to_string()
|
||||
}
|
||||
|
||||
fn parse_open_code_jsonl_line(line: &str) -> ParsedCliAgentEvent {
|
||||
let Ok(value) = serde_json::from_str::<Value>(line) else {
|
||||
return ParsedCliAgentEvent::default();
|
||||
};
|
||||
|
||||
match value.get("type").and_then(Value::as_str).unwrap_or_default() {
|
||||
"text" => {
|
||||
let Some(text) = value.pointer("/part/text").and_then(Value::as_str).filter(|text| !text.is_empty()) else {
|
||||
return ParsedCliAgentEvent::default();
|
||||
};
|
||||
ParsedCliAgentEvent {
|
||||
events: vec![AgentEvent::TextDelta { delta: text.to_string() }],
|
||||
final_text: Some(text.to_string()),
|
||||
..Default::default()
|
||||
}
|
||||
}
|
||||
"reasoning" => {
|
||||
let Some(text) = value.pointer("/part/text").and_then(Value::as_str).filter(|text| !text.is_empty()) else {
|
||||
return ParsedCliAgentEvent::default();
|
||||
};
|
||||
ParsedCliAgentEvent {
|
||||
events: vec![AgentEvent::ReasoningDelta { delta: text.to_string() }],
|
||||
..Default::default()
|
||||
}
|
||||
}
|
||||
"tool_use" => parse_open_code_tool(&value),
|
||||
"step_finish" => {
|
||||
let input = value.pointer("/part/tokens/input").and_then(Value::as_u64).unwrap_or(0) as u32;
|
||||
let output = value.pointer("/part/tokens/output").and_then(Value::as_u64).unwrap_or(0) as u32;
|
||||
ParsedCliAgentEvent {
|
||||
usage_delta: (input > 0 || output > 0)
|
||||
.then_some(TokenUsage { input_tokens: input, output_tokens: output }),
|
||||
..Default::default()
|
||||
}
|
||||
}
|
||||
"error" => {
|
||||
let message = open_code_error_message(&value);
|
||||
ParsedCliAgentEvent {
|
||||
error: Some(message.clone()),
|
||||
events: vec![AgentEvent::Error { message }],
|
||||
..Default::default()
|
||||
}
|
||||
}
|
||||
_ => ParsedCliAgentEvent::default(),
|
||||
}
|
||||
}
|
||||
|
||||
fn parse_open_code_tool(value: &Value) -> ParsedCliAgentEvent {
|
||||
let part = &value["part"];
|
||||
let state = &part["state"];
|
||||
let status = state.get("status").and_then(Value::as_str).unwrap_or_default();
|
||||
if status != "completed" && status != "error" {
|
||||
return ParsedCliAgentEvent::default();
|
||||
}
|
||||
|
||||
let tool_call_id = part
|
||||
.get("callID")
|
||||
.and_then(Value::as_str)
|
||||
.or_else(|| part.get("call_id").and_then(Value::as_str))
|
||||
.or_else(|| part.get("id").and_then(Value::as_str))
|
||||
.unwrap_or("opencode-tool-call")
|
||||
.to_string();
|
||||
let tool_name = part.get("tool").and_then(Value::as_str).unwrap_or("opencode_tool").to_string();
|
||||
let args = state.get("input").cloned().unwrap_or_else(|| Value::Object(Default::default()));
|
||||
let result = state
|
||||
.get("output")
|
||||
.filter(|value| !value.is_null())
|
||||
.or_else(|| state.get("error").filter(|value| !value.is_null()))
|
||||
.cloned()
|
||||
.unwrap_or(Value::Null);
|
||||
|
||||
ParsedCliAgentEvent {
|
||||
events: vec![
|
||||
AgentEvent::ToolCallStart { tool_call_id: tool_call_id.clone(), tool_name: tool_name.clone(), args },
|
||||
AgentEvent::ToolCallEnd { tool_call_id, tool_name, result, is_error: status == "error" },
|
||||
],
|
||||
..Default::default()
|
||||
}
|
||||
}
|
||||
|
||||
fn open_code_error_message(value: &Value) -> String {
|
||||
value
|
||||
.pointer("/error/data/message")
|
||||
.and_then(Value::as_str)
|
||||
.or_else(|| value.pointer("/error/message").and_then(Value::as_str))
|
||||
.or_else(|| value.get("message").and_then(Value::as_str))
|
||||
.or_else(|| value.get("error").and_then(Value::as_str))
|
||||
.unwrap_or("OpenCode CLI failed")
|
||||
.to_string()
|
||||
}
|
||||
|
||||
pub async fn run_cli_jsonl_agent(
|
||||
spec: CliAgentProcessSpec,
|
||||
cancelled: &Notify,
|
||||
on_event: impl Fn(AgentEvent) + Send + Sync + 'static,
|
||||
) -> Result<String, String> {
|
||||
let mut command = cli_command(&spec.command.program);
|
||||
command.args(&spec.command.args).envs(spec.env.iter().map(|(key, value)| (key.as_str(), value.as_str())));
|
||||
command.args(&spec.command.args);
|
||||
for key in &spec.env_remove {
|
||||
command.env_remove(key);
|
||||
}
|
||||
command.envs(spec.env.iter().map(|(key, value)| (key.as_str(), value.as_str())));
|
||||
if let Some(current_dir) = &spec.current_dir {
|
||||
command.current_dir(current_dir);
|
||||
}
|
||||
@@ -544,6 +643,7 @@ pub async fn run_cli_jsonl_agent(
|
||||
.stdin(if spec.stdin.is_some() { Stdio::piped() } else { Stdio::null() })
|
||||
.stdout(Stdio::piped())
|
||||
.stderr(Stdio::piped())
|
||||
.kill_on_drop(true)
|
||||
.spawn()
|
||||
.map_err(|e| (spec.classify_spawn_error)(&e.to_string()))?;
|
||||
|
||||
@@ -565,6 +665,7 @@ pub async fn run_cli_jsonl_agent(
|
||||
|
||||
let mut final_text = String::new();
|
||||
let mut saw_agent_end = false;
|
||||
let mut total_usage = TokenUsage::default();
|
||||
let mut terminal_error: Option<String> = None;
|
||||
|
||||
loop {
|
||||
@@ -583,6 +684,9 @@ pub async fn run_cli_jsonl_agent(
|
||||
if let Some(text) = parsed.final_text {
|
||||
final_text.push_str(&text);
|
||||
}
|
||||
if let Some(usage) = parsed.usage_delta {
|
||||
total_usage.add(&usage);
|
||||
}
|
||||
for event in parsed.events {
|
||||
if matches!(event, AgentEvent::AgentEnd { .. }) {
|
||||
saw_agent_end = true;
|
||||
@@ -590,7 +694,7 @@ pub async fn run_cli_jsonl_agent(
|
||||
on_event(event);
|
||||
}
|
||||
if let Some(error) = parsed.error {
|
||||
terminal_error = Some(error);
|
||||
terminal_error = Some((spec.classify_run_error)(&error));
|
||||
let _ = child.kill().await;
|
||||
break;
|
||||
}
|
||||
@@ -615,7 +719,10 @@ pub async fn run_cli_jsonl_agent(
|
||||
}
|
||||
|
||||
if !saw_agent_end {
|
||||
on_event(AgentEvent::AgentEnd { input_tokens: None, output_tokens: None });
|
||||
on_event(AgentEvent::AgentEnd {
|
||||
input_tokens: (total_usage.input_tokens > 0).then_some(total_usage.input_tokens),
|
||||
output_tokens: (total_usage.output_tokens > 0).then_some(total_usage.output_tokens),
|
||||
});
|
||||
}
|
||||
|
||||
Ok(final_text)
|
||||
@@ -625,6 +732,7 @@ pub async fn run_cli_jsonl_agent(
|
||||
mod tests {
|
||||
use super::*;
|
||||
use std::process::Command as StdCommand;
|
||||
use std::sync::{Arc, Mutex};
|
||||
use std::time::{SystemTime, UNIX_EPOCH};
|
||||
use tokio::time::{sleep, timeout, Duration};
|
||||
|
||||
@@ -657,6 +765,7 @@ mod tests {
|
||||
],
|
||||
},
|
||||
env: vec![("DBX_TEST_ENV".to_string(), "from-env".to_string())],
|
||||
env_remove: Vec::new(),
|
||||
current_dir: None,
|
||||
stdin: None,
|
||||
dialect: CliAgentJsonlDialect::CodexExec,
|
||||
@@ -680,6 +789,7 @@ mod tests {
|
||||
],
|
||||
},
|
||||
env: Vec::new(),
|
||||
env_remove: Vec::new(),
|
||||
current_dir: None,
|
||||
stdin: Some("prompt from stdin".to_string()),
|
||||
dialect: CliAgentJsonlDialect::CodexExec,
|
||||
@@ -692,6 +802,47 @@ mod tests {
|
||||
assert_eq!(result, "prompt from stdin");
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn opencode_agent_aggregates_step_usage_and_emits_one_agent_end() {
|
||||
let spec = CliAgentProcessSpec {
|
||||
command: CliAgentCommandSpec {
|
||||
program: "sh".to_string(),
|
||||
args: vec![
|
||||
"-c".to_string(),
|
||||
concat!(
|
||||
"printf '%s\\n' ",
|
||||
"'{\"type\":\"step_finish\",\"part\":{\"tokens\":{\"input\":10,\"output\":2}}}' ",
|
||||
"'{\"type\":\"text\",\"part\":{\"text\":\"hello\"}}' ",
|
||||
"'{\"type\":\"step_finish\",\"part\":{\"tokens\":{\"input\":3,\"output\":4}}}'",
|
||||
)
|
||||
.to_string(),
|
||||
],
|
||||
},
|
||||
env: Vec::new(),
|
||||
env_remove: Vec::new(),
|
||||
current_dir: None,
|
||||
stdin: None,
|
||||
dialect: CliAgentJsonlDialect::OpenCodeRun,
|
||||
classify_spawn_error,
|
||||
classify_run_error,
|
||||
};
|
||||
let events = Arc::new(Mutex::new(Vec::new()));
|
||||
let captured = Arc::clone(&events);
|
||||
|
||||
let result = run_cli_jsonl_agent(spec, &Notify::new(), move |event| {
|
||||
captured.lock().unwrap().push(event);
|
||||
})
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
assert_eq!(result, "hello");
|
||||
let events = events.lock().unwrap();
|
||||
assert_eq!(events.iter().filter(|event| matches!(event, AgentEvent::AgentEnd { .. })).count(), 1);
|
||||
assert!(events
|
||||
.iter()
|
||||
.any(|event| { matches!(event, AgentEvent::AgentEnd { input_tokens: Some(13), output_tokens: Some(6) }) }));
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn jsonl_error_kills_and_waits_for_child() {
|
||||
let pid_file = std::env::temp_dir().join(format!(
|
||||
@@ -707,6 +858,7 @@ mod tests {
|
||||
let spec = CliAgentProcessSpec {
|
||||
command: CliAgentCommandSpec { program: "sh".to_string(), args: vec!["-c".to_string(), script] },
|
||||
env: Vec::new(),
|
||||
env_remove: Vec::new(),
|
||||
current_dir: None,
|
||||
stdin: None,
|
||||
dialect: CliAgentJsonlDialect::CodexExec,
|
||||
|
||||
@@ -970,6 +970,7 @@ pub async fn run_codex_agent(
|
||||
CliAgentProcessSpec {
|
||||
command,
|
||||
env,
|
||||
env_remove: Vec::new(),
|
||||
current_dir: None,
|
||||
stdin: Some(prompt.to_string()),
|
||||
dialect: CliAgentJsonlDialect::CodexExec,
|
||||
@@ -1037,6 +1038,8 @@ mod tests {
|
||||
claude_code_cli_env: Default::default(),
|
||||
pi_agent_cli_path: None,
|
||||
pi_agent_cli_env: Default::default(),
|
||||
opencode_cli_path: None,
|
||||
opencode_cli_env: Default::default(),
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -82,7 +82,11 @@ pub fn static_effort_capability(config: &AiConfig, model_id: &str) -> Option<AiE
|
||||
AiProvider::AnthropicCompatible | AiProvider::OpenaiCompatible | AiProvider::Custom => {
|
||||
Some(AiEffortCapability::FreeText { placeholder: None, source: AiCapabilitySource::Custom })
|
||||
}
|
||||
AiProvider::Claude | AiProvider::CodexCli | AiProvider::ClaudeCodeCli | AiProvider::PiAgentCli => None,
|
||||
AiProvider::Claude
|
||||
| AiProvider::CodexCli
|
||||
| AiProvider::ClaudeCodeCli
|
||||
| AiProvider::PiAgentCli
|
||||
| AiProvider::OpenCodeCli => None,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -187,6 +191,7 @@ pub fn registry_source_url(provider: &AiProvider) -> Option<&'static str> {
|
||||
| AiProvider::CodexCli
|
||||
| AiProvider::ClaudeCodeCli
|
||||
| AiProvider::PiAgentCli
|
||||
| AiProvider::OpenCodeCli
|
||||
| AiProvider::Custom => None,
|
||||
}
|
||||
}
|
||||
@@ -201,7 +206,11 @@ pub fn validate_runtime_effort(config: &AiConfig) -> Result<(), String> {
|
||||
|
||||
if matches!(
|
||||
config.provider,
|
||||
AiProvider::Claude | AiProvider::CodexCli | AiProvider::ClaudeCodeCli | AiProvider::PiAgentCli
|
||||
AiProvider::Claude
|
||||
| AiProvider::CodexCli
|
||||
| AiProvider::ClaudeCodeCli
|
||||
| AiProvider::PiAgentCli
|
||||
| AiProvider::OpenCodeCli
|
||||
) {
|
||||
return match selection {
|
||||
AiEffortSelection::Enum(value) if !value.trim().is_empty() => Ok(()),
|
||||
@@ -254,7 +263,7 @@ pub fn apply_runtime_effort(body: &mut Value, config: &AiConfig) {
|
||||
apply_openai_effort(object, &config.api_style, selection);
|
||||
}
|
||||
}
|
||||
AiProvider::CodexCli | AiProvider::ClaudeCodeCli | AiProvider::PiAgentCli => {}
|
||||
AiProvider::CodexCli | AiProvider::ClaudeCodeCli | AiProvider::PiAgentCli | AiProvider::OpenCodeCli => {}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -408,6 +417,8 @@ mod tests {
|
||||
claude_code_cli_env: HashMap::new(),
|
||||
pi_agent_cli_path: None,
|
||||
pi_agent_cli_env: Default::default(),
|
||||
opencode_cli_path: None,
|
||||
opencode_cli_env: Default::default(),
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -96,6 +96,7 @@ pub(crate) fn model_is_assistant_compatible(provider: &AiProvider, model_id: &st
|
||||
| AiProvider::CodexCli
|
||||
| AiProvider::ClaudeCodeCli
|
||||
| AiProvider::PiAgentCli
|
||||
| AiProvider::OpenCodeCli
|
||||
| AiProvider::MiniMax
|
||||
| AiProvider::Custom => true,
|
||||
}
|
||||
|
||||
@@ -0,0 +1,675 @@
|
||||
use crate::agent_events::AgentEvent;
|
||||
use crate::ai::{AiCapabilitySource, AiConfig, AiEffortCapability, AiModelInfo, AiTestConnectionResult};
|
||||
use crate::ai_cli_agent::{
|
||||
build_cli_agent_prompt, cli_command, dbx_mcp_scope_env, parse_cli_jsonl_event, run_cli_jsonl_agent,
|
||||
CliAgentCommandSpec, CliAgentJsonlDialect, CliAgentProcessSpec, CliAgentRunOptions,
|
||||
};
|
||||
use serde_json::{json, Map, Value};
|
||||
use std::collections::{BTreeMap, BTreeSet};
|
||||
use std::env;
|
||||
use std::path::{Path, PathBuf};
|
||||
use std::time::{Duration, Instant};
|
||||
use tokio::sync::Notify;
|
||||
|
||||
const OPENCODE_MODEL_DISCOVERY_TIMEOUT: Duration = Duration::from_secs(10);
|
||||
const OPENCODE_CONTROL_ENV: &[&str] = &[
|
||||
"OPENCODE_CONFIG",
|
||||
"OPENCODE_CONFIG_CONTENT",
|
||||
"OPENCODE_CONFIG_DIR",
|
||||
"OPENCODE_DB",
|
||||
"OPENCODE_PERMISSION",
|
||||
"OPENCODE_DISABLE_PROJECT_CONFIG",
|
||||
];
|
||||
|
||||
pub type OpenCodeRunOptions = CliAgentRunOptions;
|
||||
pub type OpenCodeCommandSpec = CliAgentCommandSpec;
|
||||
|
||||
struct OpenCodeIsolatedCwd {
|
||||
path: PathBuf,
|
||||
}
|
||||
|
||||
impl OpenCodeIsolatedCwd {
|
||||
fn create() -> Result<Self, String> {
|
||||
let path = env::temp_dir().join(format!("dbx-opencode-{}", uuid::Uuid::new_v4()));
|
||||
std::fs::create_dir(&path)
|
||||
.map_err(|error| format!("[openCodeRunFailed] Failed to create isolated OpenCode directory: {error}"))?;
|
||||
Ok(Self { path })
|
||||
}
|
||||
}
|
||||
|
||||
impl Drop for OpenCodeIsolatedCwd {
|
||||
fn drop(&mut self) {
|
||||
let _ = std::fs::remove_dir_all(&self.path);
|
||||
}
|
||||
}
|
||||
|
||||
fn opencode_program(config: &AiConfig) -> String {
|
||||
config
|
||||
.opencode_cli_path
|
||||
.as_deref()
|
||||
.map(str::trim)
|
||||
.filter(|value| !value.is_empty())
|
||||
.unwrap_or("opencode")
|
||||
.to_string()
|
||||
}
|
||||
|
||||
fn resolve_opencode_command(config: &AiConfig) -> Result<OpenCodeCommandSpec, String> {
|
||||
let program = opencode_program(config);
|
||||
if starts_with_env_assignment(&program) {
|
||||
return Err("[openCodeCliPathInvalid] OpenCode CLI path should contain only the executable path. Add environment variables in the OpenCode CLI environment variables section.".to_string());
|
||||
}
|
||||
|
||||
let program = if is_path_like_program(&program) { crate::path_utils::expand_tilde(&program) } else { program };
|
||||
let path = Path::new(&program);
|
||||
if path.is_dir() {
|
||||
return launchable_program_in_dir(path, "opencode")
|
||||
.map(opencode_command_for_program)
|
||||
.ok_or_else(|| {
|
||||
"[openCodeCliPathInvalid] OpenCode CLI path should point to the opencode executable or a directory containing opencode."
|
||||
.to_string()
|
||||
});
|
||||
}
|
||||
if is_path_like_program(&program) && !path.is_file() {
|
||||
return Err("[openCodeCliPathInvalid] OpenCode CLI executable does not exist.".to_string());
|
||||
}
|
||||
Ok(opencode_command_for_program(program))
|
||||
}
|
||||
|
||||
fn opencode_command_for_program(program: String) -> OpenCodeCommandSpec {
|
||||
#[cfg(windows)]
|
||||
if let Some(command) = windows_npm_opencode_shim_command(&program) {
|
||||
return command;
|
||||
}
|
||||
|
||||
OpenCodeCommandSpec { program, args: Vec::new() }
|
||||
}
|
||||
|
||||
#[cfg(windows)]
|
||||
fn windows_npm_opencode_shim_command(program: &str) -> Option<OpenCodeCommandSpec> {
|
||||
let path = Path::new(program);
|
||||
let extension = path.extension()?.to_str()?.to_ascii_lowercase();
|
||||
if extension != "cmd" && extension != "bat" {
|
||||
return None;
|
||||
}
|
||||
let executable = path.parent()?.join("node_modules").join("opencode-ai").join("bin").join("opencode.exe");
|
||||
executable
|
||||
.is_file()
|
||||
.then(|| OpenCodeCommandSpec { program: executable.to_string_lossy().to_string(), args: Vec::new() })
|
||||
}
|
||||
|
||||
fn launchable_program_in_dir(dir: &Path, program: &str) -> Option<String> {
|
||||
program_path_candidates(dir, program)
|
||||
.into_iter()
|
||||
.find(|candidate| candidate.is_file())
|
||||
.map(|path| path.to_string_lossy().to_string())
|
||||
}
|
||||
|
||||
#[cfg(not(windows))]
|
||||
fn program_path_candidates(dir: &Path, program: &str) -> Vec<PathBuf> {
|
||||
vec![dir.join(program)]
|
||||
}
|
||||
|
||||
#[cfg(windows)]
|
||||
fn program_path_candidates(dir: &Path, program: &str) -> Vec<PathBuf> {
|
||||
[".exe", ".cmd", ".bat", ""].iter().map(|extension| dir.join(format!("{program}{extension}"))).collect()
|
||||
}
|
||||
|
||||
fn is_path_like_program(program: &str) -> bool {
|
||||
program.contains('/') || program.contains('\\') || program.starts_with('~')
|
||||
}
|
||||
|
||||
fn starts_with_env_assignment(program: &str) -> bool {
|
||||
let Some(first_token) = program.split_whitespace().next() else {
|
||||
return false;
|
||||
};
|
||||
let Some((key, _)) = first_token.split_once('=') else {
|
||||
return false;
|
||||
};
|
||||
is_env_var_name(key)
|
||||
}
|
||||
|
||||
fn is_env_var_name(name: &str) -> bool {
|
||||
let mut chars = name.chars();
|
||||
let Some(first) = chars.next() else {
|
||||
return false;
|
||||
};
|
||||
(first == '_' || first.is_ascii_alphabetic()) && chars.all(|ch| ch == '_' || ch.is_ascii_alphanumeric())
|
||||
}
|
||||
|
||||
fn is_reserved_env_name(name: &str) -> bool {
|
||||
let upper = name.to_ascii_uppercase();
|
||||
upper.starts_with("DBX_MCP_") || OPENCODE_CONTROL_ENV.iter().any(|reserved| upper == *reserved)
|
||||
}
|
||||
|
||||
pub fn opencode_cli_env(config: &AiConfig) -> Result<Vec<(String, String)>, String> {
|
||||
let mut env = BTreeMap::new();
|
||||
for (key, value) in &config.opencode_cli_env {
|
||||
let key = key.trim();
|
||||
if key.is_empty() {
|
||||
continue;
|
||||
}
|
||||
if !is_env_var_name(key) {
|
||||
return Err(format!(
|
||||
"[openCodeEnvInvalid] Invalid OpenCode CLI environment variable name `{key}`. Use names like HTTPS_PROXY."
|
||||
));
|
||||
}
|
||||
if is_reserved_env_name(key) {
|
||||
return Err(format!(
|
||||
"[openCodeEnvReserved] `{key}` is managed by DBX for the isolated OpenCode session and cannot be set here."
|
||||
));
|
||||
}
|
||||
env.insert(key.to_string(), value.clone());
|
||||
}
|
||||
Ok(env.into_iter().collect())
|
||||
}
|
||||
|
||||
fn opencode_process_env(config: &AiConfig, runtime_config: Value) -> Result<Vec<(String, String)>, String> {
|
||||
let mut env = BTreeMap::from_iter(opencode_cli_env(config)?);
|
||||
env.insert("OPENCODE_DB".to_string(), ":memory:".to_string());
|
||||
env.insert("OPENCODE_DISABLE_PROJECT_CONFIG".to_string(), "1".to_string());
|
||||
env.insert("OPENCODE_CONFIG_CONTENT".to_string(), runtime_config.to_string());
|
||||
Ok(env.into_iter().collect())
|
||||
}
|
||||
|
||||
fn opencode_runtime_config(options: Option<&OpenCodeRunOptions>) -> Value {
|
||||
let mut config = Map::new();
|
||||
config.insert("permission".to_string(), json!({ "*": "deny", "dbx_*": "allow" }));
|
||||
|
||||
if let Some(options) = options {
|
||||
let command = options
|
||||
.mcp_server_command
|
||||
.as_ref()
|
||||
.cloned()
|
||||
.unwrap_or_else(|| CliAgentCommandSpec { program: "dbx-mcp-server".to_string(), args: Vec::new() });
|
||||
let mut command_parts = vec![command.program];
|
||||
command_parts.extend(command.args);
|
||||
let environment = dbx_mcp_scope_env(options)
|
||||
.into_iter()
|
||||
.map(|(name, value)| (name.to_string(), Value::String(value)))
|
||||
.collect::<Map<_, _>>();
|
||||
config.insert(
|
||||
"mcp".to_string(),
|
||||
json!({
|
||||
"dbx": {
|
||||
"type": "local",
|
||||
"command": command_parts,
|
||||
"enabled": true,
|
||||
"environment": environment
|
||||
}
|
||||
}),
|
||||
);
|
||||
}
|
||||
|
||||
Value::Object(config)
|
||||
}
|
||||
|
||||
fn opencode_selection_args(config: &AiConfig) -> Vec<String> {
|
||||
let mut args = Vec::new();
|
||||
let model = config.model.trim();
|
||||
if !model.is_empty() && !model.eq_ignore_ascii_case("default") {
|
||||
args.extend(["--model".to_string(), model.to_string()]);
|
||||
}
|
||||
if let Some(variant) = config.runtime_effort.as_ref().and_then(|selection| selection.cli_value()) {
|
||||
args.extend(["--variant".to_string(), variant]);
|
||||
}
|
||||
args
|
||||
}
|
||||
|
||||
pub fn build_opencode_command(config: &AiConfig) -> OpenCodeCommandSpec {
|
||||
let mut command = OpenCodeCommandSpec { program: opencode_program(config), args: Vec::new() };
|
||||
command.args.extend(["run".to_string(), "--format".to_string(), "json".to_string(), "--pure".to_string()]);
|
||||
command.args.extend(opencode_selection_args(config));
|
||||
command
|
||||
}
|
||||
|
||||
fn resolved_models_command(config: &AiConfig) -> Result<OpenCodeCommandSpec, String> {
|
||||
let mut command = resolve_opencode_command(config)?;
|
||||
command.args.extend(["models".to_string(), "--verbose".to_string(), "--pure".to_string()]);
|
||||
Ok(command)
|
||||
}
|
||||
|
||||
fn resolved_run_command(config: &AiConfig) -> Result<OpenCodeCommandSpec, String> {
|
||||
let resolved = resolve_opencode_command(config)?;
|
||||
let mut command = build_opencode_command(config);
|
||||
command.program = resolved.program;
|
||||
command.args.splice(0..0, resolved.args);
|
||||
Ok(command)
|
||||
}
|
||||
|
||||
pub fn build_opencode_prompt(system_prompt: &str, messages: &[crate::ai::AiMessage], allow_write_sql: bool) -> String {
|
||||
build_cli_agent_prompt("OpenCode", system_prompt, messages, allow_write_sql)
|
||||
}
|
||||
|
||||
pub async fn list_opencode_models(config: &AiConfig) -> Result<Vec<AiModelInfo>, String> {
|
||||
let command = resolved_models_command(config)?;
|
||||
let runtime = OpenCodeIsolatedCwd::create()?;
|
||||
let mut process = cli_command(&command.program);
|
||||
process.args(&command.args);
|
||||
for key in OPENCODE_CONTROL_ENV {
|
||||
process.env_remove(key);
|
||||
}
|
||||
process
|
||||
.envs(opencode_process_env(config, opencode_runtime_config(None))?)
|
||||
.current_dir(&runtime.path)
|
||||
.kill_on_drop(true);
|
||||
let output = tokio::time::timeout(OPENCODE_MODEL_DISCOVERY_TIMEOUT, process.output())
|
||||
.await
|
||||
.map_err(|_| "[openCodeTimeout] OpenCode model discovery timed out".to_string())?
|
||||
.map_err(|error| classify_opencode_spawn_error(&error.to_string()))?;
|
||||
|
||||
if !output.status.success() {
|
||||
return Err(classify_opencode_run_error(&combined_output(&output.stderr, &output.stdout)));
|
||||
}
|
||||
parse_opencode_models(&String::from_utf8_lossy(&output.stdout)).ok_or_else(|| {
|
||||
"[openCodeNotAuthenticated] OpenCode returned no configured models. Sign in or configure a provider in OpenCode, then retry."
|
||||
.to_string()
|
||||
})
|
||||
}
|
||||
|
||||
fn parse_opencode_models(stdout: &str) -> Option<Vec<AiModelInfo>> {
|
||||
let mut models = Vec::new();
|
||||
let mut seen = BTreeSet::new();
|
||||
let mut pending_id: Option<String> = None;
|
||||
let mut json_buffer = String::new();
|
||||
|
||||
for line in stdout.lines() {
|
||||
let trimmed = line.trim();
|
||||
if json_buffer.is_empty() {
|
||||
if trimmed.starts_with('{') {
|
||||
json_buffer.push_str(line);
|
||||
json_buffer.push('\n');
|
||||
} else if !trimmed.is_empty() {
|
||||
pending_id = Some(trimmed.to_string());
|
||||
}
|
||||
} else {
|
||||
json_buffer.push_str(line);
|
||||
json_buffer.push('\n');
|
||||
}
|
||||
|
||||
if json_buffer.is_empty() {
|
||||
continue;
|
||||
}
|
||||
match serde_json::from_str::<Value>(&json_buffer) {
|
||||
Ok(metadata) => {
|
||||
if let Some(info) = opencode_model_info(pending_id.take(), &metadata) {
|
||||
if seen.insert(info.id.clone()) {
|
||||
models.push(info);
|
||||
}
|
||||
}
|
||||
json_buffer.clear();
|
||||
}
|
||||
Err(error) if error.is_eof() => {}
|
||||
Err(_) => {
|
||||
json_buffer.clear();
|
||||
pending_id = None;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if models.is_empty() {
|
||||
return None;
|
||||
}
|
||||
if seen.insert("default".to_string()) {
|
||||
models.insert(0, AiModelInfo::new("default", Some("Default".to_string())));
|
||||
}
|
||||
Some(models)
|
||||
}
|
||||
|
||||
fn opencode_model_info(reported_id: Option<String>, metadata: &Value) -> Option<AiModelInfo> {
|
||||
let id = reported_id.or_else(|| {
|
||||
let provider = metadata.get("providerID").and_then(Value::as_str)?;
|
||||
let model = metadata.get("id").and_then(Value::as_str)?;
|
||||
Some(format!("{provider}/{model}"))
|
||||
})?;
|
||||
let id = id.trim();
|
||||
if id.is_empty() || !id.contains('/') {
|
||||
return None;
|
||||
}
|
||||
|
||||
let display_name = metadata
|
||||
.get("name")
|
||||
.and_then(Value::as_str)
|
||||
.map(str::trim)
|
||||
.filter(|name| !name.is_empty())
|
||||
.map(ToString::to_string);
|
||||
let variants = metadata
|
||||
.get("variants")
|
||||
.and_then(Value::as_object)
|
||||
.map(|variants| variants.keys().map(String::as_str).collect::<Vec<_>>())
|
||||
.unwrap_or_default();
|
||||
let mut info = AiModelInfo::new(id, display_name);
|
||||
info.supported_effort_levels = variants.iter().filter_map(|variant| variant.parse().ok()).collect();
|
||||
info.effort_capability = crate::ai_effort::dynamic_enum_capability(variants, AiCapabilitySource::LocalCli);
|
||||
Some(info)
|
||||
}
|
||||
|
||||
pub async fn resolve_opencode_model_effort(config: &AiConfig, model_id: &str) -> Result<AiEffortCapability, String> {
|
||||
Ok(list_opencode_models(config)
|
||||
.await?
|
||||
.into_iter()
|
||||
.find(|model| model.id == model_id)
|
||||
.and_then(|model| model.effort_capability)
|
||||
.unwrap_or(AiEffortCapability::Unsupported))
|
||||
}
|
||||
|
||||
pub async fn test_opencode_connection(config: &AiConfig) -> Result<AiTestConnectionResult, String> {
|
||||
let start = Instant::now();
|
||||
list_opencode_models(config).await?;
|
||||
Ok(AiTestConnectionResult {
|
||||
success: true,
|
||||
message: format!("OK - {}ms", start.elapsed().as_millis()),
|
||||
latency_ms: Some(start.elapsed().as_millis() as u64),
|
||||
model_used: config.model.trim().to_string(),
|
||||
error_category: None,
|
||||
})
|
||||
}
|
||||
|
||||
fn combined_output(stderr: &[u8], stdout: &[u8]) -> String {
|
||||
let stderr = String::from_utf8_lossy(stderr);
|
||||
let stdout = String::from_utf8_lossy(stdout);
|
||||
[stderr.trim(), stdout.trim()].into_iter().filter(|part| !part.is_empty()).collect::<Vec<_>>().join("\n")
|
||||
}
|
||||
|
||||
fn classify_opencode_spawn_error(message: &str) -> String {
|
||||
let lower = message.to_ascii_lowercase();
|
||||
if lower.contains("no such file") || lower.contains("not found") || lower.contains("cannot find") {
|
||||
format!("[openCodeNotInstalled] {message}")
|
||||
} else {
|
||||
format!("[openCodeRunFailed] {message}")
|
||||
}
|
||||
}
|
||||
|
||||
fn classify_opencode_run_error(message: &str) -> String {
|
||||
if message.starts_with("[openCode") || message.starts_with("[dbxMcpMissing]") {
|
||||
return message.to_string();
|
||||
}
|
||||
let lower = message.to_ascii_lowercase();
|
||||
if lower.contains("not authenticated")
|
||||
|| lower.contains("authentication required")
|
||||
|| lower.contains("unauthorized")
|
||||
|| lower.contains("please login")
|
||||
|| lower.contains("please sign in")
|
||||
{
|
||||
format!("[openCodeNotAuthenticated] {message}")
|
||||
} else if lower.contains("dbx-mcp-server") || lower.contains("enoent") {
|
||||
format!("[dbxMcpMissing] {message}")
|
||||
} else if lower.contains("mcp") && (lower.contains("dbx") || lower.contains("server")) {
|
||||
format!("[openCodeMcpStartupFailed] {message}")
|
||||
} else if lower.contains("json") || lower.contains("protocol") {
|
||||
format!("[openCodeProtocolError] {message}")
|
||||
} else {
|
||||
format!("[openCodeRunFailed] {message}")
|
||||
}
|
||||
}
|
||||
|
||||
pub fn parse_opencode_jsonl_event(line: &str) -> Option<Vec<AgentEvent>> {
|
||||
parse_cli_jsonl_event(line, CliAgentJsonlDialect::OpenCodeRun)
|
||||
}
|
||||
|
||||
pub async fn run_opencode_agent(
|
||||
config: &AiConfig,
|
||||
prompt: &str,
|
||||
options: OpenCodeRunOptions,
|
||||
cancelled: &Notify,
|
||||
on_event: impl Fn(AgentEvent) + Send + Sync + 'static,
|
||||
) -> Result<String, String> {
|
||||
let runtime = OpenCodeIsolatedCwd::create()?;
|
||||
let result = run_cli_jsonl_agent(
|
||||
CliAgentProcessSpec {
|
||||
command: resolved_run_command(config)?,
|
||||
env: opencode_process_env(config, opencode_runtime_config(Some(&options)))?,
|
||||
env_remove: OPENCODE_CONTROL_ENV.iter().map(|value| (*value).to_string()).collect(),
|
||||
current_dir: Some(runtime.path.clone()),
|
||||
stdin: Some(prompt.to_string()),
|
||||
dialect: CliAgentJsonlDialect::OpenCodeRun,
|
||||
classify_spawn_error: classify_opencode_spawn_error,
|
||||
classify_run_error: classify_opencode_run_error,
|
||||
},
|
||||
cancelled,
|
||||
on_event,
|
||||
)
|
||||
.await?;
|
||||
if result.trim().is_empty() {
|
||||
Err("[openCodeProtocolError] OpenCode completed without a text response".to_string())
|
||||
} else {
|
||||
Ok(result)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::ai::{AiApiStyle, AiAuthMethod, AiEffortSelection, AiProvider, AiReasoningLevel};
|
||||
use std::sync::{Arc, Mutex};
|
||||
|
||||
fn config(model: &str) -> AiConfig {
|
||||
AiConfig {
|
||||
provider: AiProvider::OpenCodeCli,
|
||||
api_key: String::new(),
|
||||
auth_method: AiAuthMethod::Bearer,
|
||||
endpoint: String::new(),
|
||||
model: model.to_string(),
|
||||
models: Vec::new(),
|
||||
api_style: AiApiStyle::Completions,
|
||||
proxy_enabled: false,
|
||||
proxy_url: String::new(),
|
||||
enable_thinking: true,
|
||||
reasoning_level: AiReasoningLevel::Default,
|
||||
runtime_effort: None,
|
||||
context_window: None,
|
||||
max_retries: None,
|
||||
codex_cli_path: None,
|
||||
codex_cli_env: Default::default(),
|
||||
claude_code_cli_path: None,
|
||||
claude_code_cli_env: Default::default(),
|
||||
pi_agent_cli_path: None,
|
||||
pi_agent_cli_env: Default::default(),
|
||||
opencode_cli_path: None,
|
||||
opencode_cli_env: Default::default(),
|
||||
}
|
||||
}
|
||||
|
||||
fn options() -> OpenCodeRunOptions {
|
||||
OpenCodeRunOptions {
|
||||
connection_id: "conn-1".to_string(),
|
||||
connection_name: "Local".to_string(),
|
||||
database: "demo".to_string(),
|
||||
schema: Some("public".to_string()),
|
||||
agent_mode: true,
|
||||
allow_writes: false,
|
||||
allow_dangerous: false,
|
||||
confirmed_write_sql: None,
|
||||
mcp_server_command: Some(CliAgentCommandSpec {
|
||||
program: "/opt/dbx/dbx-mcp-server".to_string(),
|
||||
args: vec!["--stdio".to_string()],
|
||||
}),
|
||||
}
|
||||
}
|
||||
|
||||
fn live_config() -> AiConfig {
|
||||
let mut config = config(
|
||||
&std::env::var("DBX_LIVE_OPENCODE_MODEL")
|
||||
.expect("set DBX_LIVE_OPENCODE_MODEL to an authenticated provider/model"),
|
||||
);
|
||||
config.opencode_cli_path = std::env::var("DBX_LIVE_OPENCODE_PATH").ok();
|
||||
config
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn builds_run_command_with_model_and_variant() {
|
||||
let mut config = config("openai/gpt-5.4");
|
||||
config.runtime_effort = Some(AiEffortSelection::Enum("high".to_string()));
|
||||
|
||||
let command = build_opencode_command(&config);
|
||||
|
||||
assert_eq!(command.program, "opencode");
|
||||
assert_eq!(
|
||||
command.args,
|
||||
["run", "--format", "json", "--pure", "--model", "openai/gpt-5.4", "--variant", "high"]
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn default_model_and_effort_are_omitted() {
|
||||
let command = build_opencode_command(&config("default"));
|
||||
|
||||
assert_eq!(command.args, ["run", "--format", "json", "--pure"]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn connection_check_uses_model_discovery_without_a_model_request() {
|
||||
let command = resolved_models_command(&config("default")).unwrap();
|
||||
|
||||
assert_eq!(command.program, "opencode");
|
||||
assert_eq!(command.args, ["models", "--verbose", "--pure"]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn runtime_config_scopes_mcp_and_denies_other_tools() {
|
||||
let runtime = opencode_runtime_config(Some(&options()));
|
||||
|
||||
assert_eq!(runtime.pointer("/permission/*").and_then(Value::as_str), Some("deny"));
|
||||
assert_eq!(runtime.pointer("/permission/dbx_*").and_then(Value::as_str), Some("allow"));
|
||||
assert_eq!(runtime.pointer("/mcp/dbx/command/0").and_then(Value::as_str), Some("/opt/dbx/dbx-mcp-server"));
|
||||
assert_eq!(runtime.pointer("/mcp/dbx/command/1").and_then(Value::as_str), Some("--stdio"));
|
||||
assert_eq!(
|
||||
runtime.pointer("/mcp/dbx/environment/DBX_MCP_SCOPE_CONNECTION_ID").and_then(Value::as_str),
|
||||
Some("conn-1")
|
||||
);
|
||||
assert_eq!(
|
||||
runtime.pointer("/mcp/dbx/environment/DBX_MCP_SCOPE_SCHEMA").and_then(Value::as_str),
|
||||
Some("public")
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn rejects_reserved_or_invalid_environment_names() {
|
||||
let mut invalid = config("default");
|
||||
invalid.opencode_cli_env.insert("BAD-NAME".to_string(), "1".to_string());
|
||||
assert!(opencode_cli_env(&invalid).unwrap_err().starts_with("[openCodeEnvInvalid]"));
|
||||
|
||||
let mut reserved = config("default");
|
||||
reserved.opencode_cli_env.insert("OPENCODE_DB".to_string(), "file.db".to_string());
|
||||
assert!(opencode_cli_env(&reserved).unwrap_err().starts_with("[openCodeEnvReserved]"));
|
||||
|
||||
reserved.opencode_cli_env.clear();
|
||||
reserved.opencode_cli_env.insert("DBX_MCP_ALLOW_WRITES".to_string(), "1".to_string());
|
||||
assert!(opencode_cli_env(&reserved).unwrap_err().starts_with("[openCodeEnvReserved]"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parses_verbose_models_and_preserves_variant_order() {
|
||||
let stdout = concat!(
|
||||
"openai/gpt-5.4\n",
|
||||
"{\n",
|
||||
" \"id\": \"gpt-5.4\",\n",
|
||||
" \"providerID\": \"openai\",\n",
|
||||
" \"name\": \"GPT-5.4\",\n",
|
||||
" \"variants\": {\"none\": {}, \"low\": {}, \"high\": {}, \"future\": {}}\n",
|
||||
"}\n",
|
||||
"custom/model\n",
|
||||
"{\"id\":\"model\",\"providerID\":\"custom\",\"name\":\"Custom\",\"variants\":{}}\n"
|
||||
);
|
||||
|
||||
let models = parse_opencode_models(stdout).unwrap();
|
||||
|
||||
assert_eq!(
|
||||
models.iter().map(|model| model.id.as_str()).collect::<Vec<_>>(),
|
||||
["default", "openai/gpt-5.4", "custom/model"]
|
||||
);
|
||||
assert_eq!(models[1].display_name.as_deref(), Some("GPT-5.4"));
|
||||
let AiEffortCapability::Enum { options, .. } = models[1].effort_capability.as_ref().unwrap() else {
|
||||
panic!("expected dynamic effort options");
|
||||
};
|
||||
assert_eq!(
|
||||
options.iter().map(|option| option.id.as_str()).collect::<Vec<_>>(),
|
||||
["none", "low", "high", "future"]
|
||||
);
|
||||
assert!(models[2].effort_capability.is_none());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parses_text_reasoning_tool_error_and_usage_events() {
|
||||
let text = parse_opencode_jsonl_event(r#"{"type":"text","part":{"type":"text","text":"hello"}}"#).unwrap();
|
||||
assert!(matches!(&text[0], AgentEvent::TextDelta { delta } if delta == "hello"));
|
||||
|
||||
let reasoning =
|
||||
parse_opencode_jsonl_event(r#"{"type":"reasoning","part":{"type":"reasoning","text":"thinking"}}"#)
|
||||
.unwrap();
|
||||
assert!(matches!(&reasoning[0], AgentEvent::ReasoningDelta { delta } if delta == "thinking"));
|
||||
|
||||
let tool = parse_opencode_jsonl_event(
|
||||
r#"{"type":"tool_use","part":{"id":"part-1","callID":"call-1","tool":"dbx_dbx_list_connections","state":{"status":"completed","input":{},"output":"ok"}}}"#,
|
||||
)
|
||||
.unwrap();
|
||||
assert_eq!(tool.len(), 2);
|
||||
assert!(
|
||||
matches!(&tool[0], AgentEvent::ToolCallStart { tool_call_id, tool_name, .. } if tool_call_id == "call-1" && tool_name == "dbx_dbx_list_connections")
|
||||
);
|
||||
assert!(matches!(&tool[1], AgentEvent::ToolCallEnd { result, is_error: false, .. } if result == "ok"));
|
||||
|
||||
let error = parse_opencode_jsonl_event(
|
||||
r#"{"type":"error","error":{"name":"UnknownError","data":{"message":"bad model"}}}"#,
|
||||
)
|
||||
.unwrap();
|
||||
assert!(matches!(&error[0], AgentEvent::Error { message } if message == "bad model"));
|
||||
|
||||
assert!(
|
||||
parse_opencode_jsonl_event(r#"{"type":"step_finish","part":{"tokens":{"input":10,"output":3}}}"#).is_none()
|
||||
);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
#[ignore = "requires an installed, authenticated OpenCode CLI and DBX_LIVE_OPENCODE_MODEL"]
|
||||
async fn live_model_discovery_and_connection_test() {
|
||||
let config = live_config();
|
||||
|
||||
let models = list_opencode_models(&config).await.unwrap();
|
||||
assert!(models.iter().any(|model| model.id == config.model));
|
||||
let result = test_opencode_connection(&config).await.unwrap();
|
||||
assert!(result.success);
|
||||
assert!(result.latency_ms.is_some());
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
#[ignore = "requires OpenCode, a running DBX MCP bridge, and DBX_LIVE_MCP_CONNECTION_NAME/DATABASE"]
|
||||
async fn live_scoped_dbx_mcp_agent_run() {
|
||||
let config = live_config();
|
||||
let connection_name = std::env::var("DBX_LIVE_MCP_CONNECTION_NAME")
|
||||
.expect("set DBX_LIVE_MCP_CONNECTION_NAME to a saved DBX connection");
|
||||
let database = std::env::var("DBX_LIVE_MCP_DATABASE").expect("set DBX_LIVE_MCP_DATABASE to a visible database");
|
||||
let mcp_command = std::env::var("DBX_LIVE_MCP_COMMAND").unwrap_or_else(|_| "dbx-mcp-server".to_string());
|
||||
let options = OpenCodeRunOptions {
|
||||
connection_id: String::new(),
|
||||
connection_name: connection_name.clone(),
|
||||
database,
|
||||
schema: None,
|
||||
agent_mode: false,
|
||||
allow_writes: false,
|
||||
allow_dangerous: false,
|
||||
confirmed_write_sql: None,
|
||||
mcp_server_command: Some(CliAgentCommandSpec { program: mcp_command, args: Vec::new() }),
|
||||
};
|
||||
let events = Arc::new(Mutex::new(Vec::new()));
|
||||
let captured = Arc::clone(&events);
|
||||
|
||||
let result = tokio::time::timeout(
|
||||
Duration::from_secs(90),
|
||||
run_opencode_agent(
|
||||
&config,
|
||||
"You must use the DBX MCP list-connections tool. Reply with the visible connection name only.",
|
||||
options,
|
||||
&Notify::new(),
|
||||
move |event| captured.lock().unwrap().push(event),
|
||||
),
|
||||
)
|
||||
.await
|
||||
.expect("OpenCode MCP smoke test timed out")
|
||||
.unwrap();
|
||||
|
||||
assert!(result.contains(&connection_name));
|
||||
assert!(events.lock().unwrap().iter().any(|event| {
|
||||
matches!(event, AgentEvent::ToolCallStart { tool_name, .. } if tool_name.contains("dbx_list_connections"))
|
||||
}));
|
||||
}
|
||||
}
|
||||
@@ -755,6 +755,8 @@ mod tests {
|
||||
claude_code_cli_env: HashMap::new(),
|
||||
pi_agent_cli_path: None,
|
||||
pi_agent_cli_env: HashMap::new(),
|
||||
opencode_cli_path: None,
|
||||
opencode_cli_env: HashMap::new(),
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1416,6 +1416,8 @@ mod tests {
|
||||
claude_code_cli_env: Default::default(),
|
||||
pi_agent_cli_path: None,
|
||||
pi_agent_cli_env: Default::default(),
|
||||
opencode_cli_path: None,
|
||||
opencode_cli_env: Default::default(),
|
||||
},
|
||||
}
|
||||
}
|
||||
@@ -2238,7 +2240,11 @@ mod tests {
|
||||
async fn sensitive_payload_ai_configs_some_saves_configs() {
|
||||
let storage = Storage::open(&temp_db_path("ai-cfg-some")).await.unwrap();
|
||||
|
||||
let cfg = make_test_config("synced", true);
|
||||
let mut cfg = make_test_config("synced", true);
|
||||
cfg.config.provider = crate::ai::AiProvider::OpenCodeCli;
|
||||
cfg.config.model = "openai/gpt-5.4-mini".to_string();
|
||||
cfg.config.opencode_cli_path = Some("/opt/homebrew/bin/opencode".to_string());
|
||||
cfg.config.opencode_cli_env.insert("HTTPS_PROXY".to_string(), "http://127.0.0.1:7890".to_string());
|
||||
let payload = SensitiveSyncPayload {
|
||||
connection_secrets: vec![],
|
||||
ai_configs: Some(vec![cfg]),
|
||||
@@ -2249,6 +2255,13 @@ mod tests {
|
||||
let loaded = storage.load_ai_configs().await.unwrap();
|
||||
assert_eq!(loaded.len(), 1);
|
||||
assert_eq!(loaded[0].name, "synced");
|
||||
assert!(matches!(loaded[0].config.provider, crate::ai::AiProvider::OpenCodeCli));
|
||||
assert_eq!(loaded[0].config.model, "openai/gpt-5.4-mini");
|
||||
assert_eq!(loaded[0].config.opencode_cli_path.as_deref(), Some("/opt/homebrew/bin/opencode"));
|
||||
assert_eq!(
|
||||
loaded[0].config.opencode_cli_env.get("HTTPS_PROXY").map(String::as_str),
|
||||
Some("http://127.0.0.1:7890")
|
||||
);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
|
||||
@@ -15,6 +15,7 @@ pub mod ai_cli_agent;
|
||||
pub mod ai_codex_cli;
|
||||
pub mod ai_effort;
|
||||
mod ai_model_filter;
|
||||
pub mod ai_opencode_cli;
|
||||
pub mod ai_pi_agent_cli;
|
||||
pub mod backend_error;
|
||||
pub mod changelog;
|
||||
|
||||
@@ -5467,6 +5467,8 @@ mod tests {
|
||||
claude_code_cli_env: std::collections::HashMap::new(),
|
||||
pi_agent_cli_path: None,
|
||||
pi_agent_cli_env: std::collections::HashMap::new(),
|
||||
opencode_cli_path: None,
|
||||
opencode_cli_env: std::collections::HashMap::new(),
|
||||
},
|
||||
}
|
||||
}
|
||||
@@ -5504,6 +5506,33 @@ mod tests {
|
||||
std::fs::remove_file(&db).ok();
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn opencode_cli_ai_config_roundtrip() {
|
||||
let db = temp_db_path("opencode-cli-ai-roundtrip");
|
||||
let storage = Storage::open(&db).await.unwrap();
|
||||
|
||||
let mut cfg = make_ai_config("opencode-cli", true);
|
||||
cfg.config.provider = AiProvider::OpenCodeCli;
|
||||
cfg.config.api_key.clear();
|
||||
cfg.config.endpoint.clear();
|
||||
cfg.config.model = "openai/gpt-5.4-mini".to_string();
|
||||
cfg.config.opencode_cli_path = Some("/opt/homebrew/bin/opencode".to_string());
|
||||
cfg.config.opencode_cli_env.insert("HTTPS_PROXY".to_string(), "http://127.0.0.1:7890".to_string());
|
||||
storage.save_ai_config_item(&cfg).await.unwrap();
|
||||
|
||||
let loaded = storage.load_ai_configs().await.unwrap();
|
||||
assert_eq!(loaded.len(), 1);
|
||||
assert!(matches!(loaded[0].config.provider, AiProvider::OpenCodeCli));
|
||||
assert_eq!(loaded[0].config.model, "openai/gpt-5.4-mini");
|
||||
assert_eq!(loaded[0].config.opencode_cli_path.as_deref(), Some("/opt/homebrew/bin/opencode"));
|
||||
assert_eq!(
|
||||
loaded[0].config.opencode_cli_env.get("HTTPS_PROXY").map(String::as_str),
|
||||
Some("http://127.0.0.1:7890")
|
||||
);
|
||||
|
||||
std::fs::remove_file(&db).ok();
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn anthropic_compatible_ai_config_roundtrip() {
|
||||
let db = temp_db_path("anthropic-compatible-ai-roundtrip");
|
||||
|
||||
Reference in new issue
Block a user