use std::path::Path;
use serde_json::json;
use crate::errors::Result;
use super::{
backup_and_write_json, backup_config_file, load_json_file, load_json_file_strict,
managed_rules_markdown, remove_legacy_rules_block, remove_managed_rules_file,
safe_write_json_file, write_managed_rules_file, AgentIntegration, DoctorCounters,
HealthcheckContext, InstallContext, InstallScope, RulesVariant,
};
const INSTRUCTIONS_ENTRY: &str = "tokensave.md";
pub struct OpenCodeIntegration;
impl AgentIntegration for OpenCodeIntegration {
fn name(&self) -> &'static str {
"OpenCode"
}
fn id(&self) -> &'static str {
"opencode"
}
fn supports_local(&self) -> bool {
true
}
fn install(&self, ctx: &InstallContext) -> Result<()> {
let config_path = opencode_config_path_for(ctx);
let rules_path = opencode_rules_path(&config_path);
let instructions_entry = instructions_entry_for(&ctx.scope, &rules_path);
install_mcp_server(&config_path, &ctx.tokensave_bin, &instructions_entry)?;
uninstall_prompt_rules(&opencode_prompt_path_for(ctx))?;
write_managed_rules_file(&rules_path, &managed_rules_markdown(RulesVariant::Generic))?;
crate::agent_note!();
crate::agent_note!("Setup complete. Next steps:");
crate::agent_note!(" 1. cd into your project and run: tokensave init");
crate::agent_note!(" 2. Start a new OpenCode session — tokensave tools are now available");
crate::agent_note!(" 3. OpenCode will prompt for approval on first use of each tool");
Ok(())
}
fn uninstall(&self, ctx: &InstallContext) -> Result<()> {
let config_path = opencode_config_path_for(ctx);
let rules_path = opencode_rules_path(&config_path);
let instructions_entry = instructions_entry_for(&ctx.scope, &rules_path);
uninstall_mcp_server(&config_path, &instructions_entry);
uninstall_prompt_rules(&opencode_prompt_path_for(ctx)).ok(); remove_managed_rules_file(&rules_path);
crate::agent_note!();
crate::agent_note!("Uninstall complete. Tokensave has been removed from OpenCode.");
crate::agent_note!("Start a new OpenCode session for changes to take effect.");
Ok(())
}
fn healthcheck(&self, dc: &mut DoctorCounters, ctx: &HealthcheckContext) {
crate::agent_note!("\n\x1b[1mOpenCode integration\x1b[0m");
doctor_check_config(dc, &ctx.home);
doctor_check_prompt(dc, &ctx.home);
}
fn is_detected(&self, home: &Path) -> bool {
home.join(".config").join("opencode").is_dir()
}
fn primary_config_path(&self, home: &Path) -> Option<std::path::PathBuf> {
Some(opencode_config_path(home))
}
fn has_tokensave(&self, home: &Path) -> bool {
let config_path = opencode_config_path(home);
if !config_path.exists() {
return false;
}
let json = super::load_json_file(&config_path);
json.get("mcp").and_then(|v| v.get("tokensave")).is_some()
}
}
fn opencode_config_path_for(ctx: &InstallContext) -> std::path::PathBuf {
match &ctx.scope {
InstallScope::Global => opencode_config_path(&ctx.home),
InstallScope::Local { project_path } => project_path.join("opencode.json"),
}
}
fn opencode_prompt_path_for(ctx: &InstallContext) -> std::path::PathBuf {
match &ctx.scope {
InstallScope::Global => opencode_prompt_path(&ctx.home),
InstallScope::Local { project_path } => project_path.join("AGENTS.md"),
}
}
fn opencode_rules_path(config_path: &Path) -> std::path::PathBuf {
config_path.parent().map_or_else(
|| std::path::PathBuf::from(INSTRUCTIONS_ENTRY),
|p| p.join(INSTRUCTIONS_ENTRY),
)
}
fn instructions_entry_for(scope: &InstallScope, rules_path: &Path) -> String {
match scope {
InstallScope::Global => rules_path.to_string_lossy().into_owned(),
InstallScope::Local { .. } => INSTRUCTIONS_ENTRY.to_string(),
}
}
fn opencode_config_path(home: &Path) -> std::path::PathBuf {
if let Ok(xdg) = std::env::var("XDG_CONFIG_HOME") {
let xdg_path = std::path::PathBuf::from(&xdg);
if xdg_path.starts_with(home) {
return xdg_path.join("opencode/opencode.json");
}
}
home.join(".config/opencode/opencode.json")
}
fn opencode_prompt_path(home: &Path) -> std::path::PathBuf {
let modern = home.join(".config/opencode/AGENTS.md");
if modern.exists() || home.join(".config/opencode").exists() {
return modern;
}
if let Ok(xdg) = std::env::var("XDG_CONFIG_HOME") {
let xdg_path = std::path::PathBuf::from(&xdg);
if xdg_path.starts_with(home) {
let xdg_dir = xdg_path.join("opencode");
if xdg_dir.exists() {
return xdg_dir.join("AGENTS.md");
}
}
}
home.join("AGENTS.md")
}
fn install_mcp_server(
config_path: &Path,
tokensave_bin: &str,
instructions_entry: &str,
) -> Result<()> {
let backup = backup_config_file(config_path)?;
let mut config = match load_json_file_strict(config_path) {
Ok(v) => v,
Err(e) => {
if let Some(ref b) = backup {
crate::agent_note!(" Backup preserved at: {}", b.display());
}
return Err(e);
}
};
let bin = crate::agents::preserve_mcp_command(
config.pointer("/mcp/tokensave/command"),
tokensave_bin,
);
config["mcp"]["tokensave"] = json!({
"type": "local",
"command": [bin, "serve"]
});
add_instructions_entry(&mut config, instructions_entry);
safe_write_json_file(config_path, &config, backup.as_deref())?;
crate::agent_note!(
"\x1b[32m✔\x1b[0m Added tokensave MCP server to {}",
config_path.display()
);
Ok(())
}
fn add_instructions_entry(config: &mut serde_json::Value, entry: &str) {
let mut arr: Vec<String> = config["instructions"]
.as_array()
.map(|a| {
a.iter()
.filter_map(|v| v.as_str().map(str::to_string))
.collect()
})
.unwrap_or_default();
if !arr.iter().any(|e| e == entry) {
arr.push(entry.to_string());
}
config["instructions"] =
serde_json::Value::Array(arr.into_iter().map(serde_json::Value::String).collect());
}
fn remove_instructions_entry(config: &mut serde_json::Value, entry: &str) -> bool {
let removed = if let Some(arr) = config
.get_mut("instructions")
.and_then(|v| v.as_array_mut())
{
let before = arr.len();
arr.retain(|v| v.as_str() != Some(entry));
arr.len() < before
} else {
false
};
if removed && config["instructions"].as_array().is_some_and(Vec::is_empty) {
if let Some(obj) = config.as_object_mut() {
obj.remove("instructions");
}
}
removed
}
fn uninstall_mcp_server(config_path: &Path, instructions_entry: &str) {
if !config_path.exists() {
return;
}
let Ok(contents) = std::fs::read_to_string(config_path) else {
return;
};
let Ok(mut config) = serde_json::from_str::<serde_json::Value>(&contents) else {
return;
};
let mcp_removed = config
.get_mut("mcp")
.and_then(|v| v.as_object_mut())
.is_some_and(|mcp| mcp.remove("tokensave").is_some());
if mcp_removed
&& config["mcp"]
.as_object()
.is_some_and(serde_json::Map::is_empty)
{
if let Some(obj) = config.as_object_mut() {
obj.remove("mcp");
}
}
let instructions_removed = remove_instructions_entry(&mut config, instructions_entry);
if !mcp_removed && !instructions_removed {
crate::agent_note!(
" No tokensave MCP server in {}, skipping",
config_path.display()
);
return;
}
let is_empty = config.as_object().is_some_and(serde_json::Map::is_empty);
if is_empty {
std::fs::remove_file(config_path).ok();
crate::agent_note!(
"\x1b[32m✔\x1b[0m Removed {} (was empty)",
config_path.display()
);
} else if backup_and_write_json(config_path, &config) {
crate::agent_note!(
"\x1b[32m✔\x1b[0m Removed tokensave MCP server from {}",
config_path.display()
);
}
}
fn uninstall_prompt_rules(prompt_path: &Path) -> Result<()> {
remove_legacy_rules_block(prompt_path, "## Prefer tokensave MCP tools", &[])
}
fn doctor_check_config(dc: &mut DoctorCounters, home: &Path) {
let config_path = opencode_config_path(home);
if !config_path.exists() {
dc.warn(&format!(
"{} not found — run `tokensave install --agent opencode` if you use OpenCode",
config_path.display()
));
return;
}
let config = load_json_file(&config_path);
let mcp_entry = &config["mcp"]["tokensave"];
if !mcp_entry.is_object() {
dc.fail(&format!(
"MCP server NOT registered in {} — run `tokensave install --agent opencode`",
config_path.display()
));
return;
}
dc.pass(&format!(
"MCP server registered in {}",
config_path.display()
));
let command = mcp_entry["command"].as_array();
let has_serve = command.is_some_and(|arr| arr.iter().any(|v| v.as_str() == Some("serve")));
if has_serve {
dc.pass("MCP server args include \"serve\"");
} else {
dc.fail("MCP server args missing \"serve\" — run `tokensave install --agent opencode`");
}
}
fn doctor_check_prompt(dc: &mut DoctorCounters, home: &Path) {
let config_path = opencode_config_path(home);
let rules_path = opencode_rules_path(&config_path);
if rules_path.exists() {
let has_rules = std::fs::read_to_string(&rules_path)
.unwrap_or_default()
.contains("tokensave");
if has_rules {
dc.pass("tokensave.md contains tokensave rules");
} else {
dc.fail(
"tokensave.md missing tokensave rules — run `tokensave install --agent opencode`",
);
}
} else {
dc.fail("tokensave.md does not exist — run `tokensave install --agent opencode`");
}
let config = load_json_file(&config_path);
let expected_entry = instructions_entry_for(&InstallScope::Global, &rules_path);
let wired = config["instructions"].as_array().is_some_and(|arr| {
arr.iter()
.any(|v| v.as_str() == Some(expected_entry.as_str()))
});
if wired {
dc.pass("opencode.json \"instructions\" includes tokensave.md");
} else {
dc.fail(
"opencode.json \"instructions\" missing tokensave.md — run `tokensave install --agent opencode`",
);
}
}