1use std::path::Path;
7
8use anyhow::{Context, bail};
9use serde_json::Value;
10use toml_edit::{DocumentMut, Item, Table};
11
12use super::{Env, by_name, known_names, probe_all};
13
14#[derive(Debug, Clone, PartialEq, Eq)]
18pub struct Candidate {
19 pub name: String,
20 pub hint: String,
21 pub config: Value,
22}
23
24pub fn probe_unconfigured(
29 configured: &std::collections::BTreeMap<String, Value>,
30) -> Vec<Candidate> {
31 let Some(env) = Env::from_env() else {
32 return Vec::new();
33 };
34 super::registry()
35 .iter()
36 .filter(|factory| !configured.contains_key(factory.name()))
37 .filter_map(|factory| {
38 factory.probe_default(&env).map(|config| Candidate {
39 name: factory.name().to_owned(),
40 hint: hint_for(&config),
41 config,
42 })
43 })
44 .collect()
45}
46
47pub fn discover(focus: Option<&str>) -> Vec<Candidate> {
52 let Some(env) = Env::from_env() else {
53 return Vec::new();
54 };
55 let candidates: Vec<Candidate> = match focus {
56 None => probe_all(&env)
57 .into_iter()
58 .map(|(name, config)| Candidate {
59 name: name.to_owned(),
60 hint: hint_for(&config),
61 config,
62 })
63 .collect(),
64 Some(name) => by_name(name)
65 .and_then(|factory| factory.probe_default(&env))
66 .map(|config| Candidate {
67 name: name.to_owned(),
68 hint: hint_for(&config),
69 config,
70 })
71 .into_iter()
72 .collect(),
73 };
74 candidates
75}
76
77fn hint_for(config: &Value) -> String {
82 if let Some(path) = config.get("path").and_then(Value::as_str) {
83 return crate::config::contract_home(Path::new(path))
84 .display()
85 .to_string();
86 }
87 if let Some(endpoint) = config.get("endpoint").and_then(Value::as_str) {
88 return endpoint.to_owned();
89 }
90 serde_json::to_string(config).unwrap_or_default()
91}
92
93pub fn prompt_and_persist(
101 config_path: &Path,
102 candidates: &[Candidate],
103 stdin_is_tty: bool,
104) -> anyhow::Result<Vec<Candidate>> {
105 if candidates.is_empty() {
106 bail!(
107 "no adapters detected in this environment; known adapters: {}",
108 known_names().join(", "),
109 );
110 }
111 if !stdin_is_tty {
112 bail!(
113 "[adapters] is empty and stdin is not a terminal; run `pond init --yes` to enable \
114 detected adapters, or add an [adapters.<adapter>] entry to {} (known adapters: {})",
115 config_path.display(),
116 known_names().join(", "),
117 );
118 }
119 let mut picker = cliclack::multiselect("Select adapters to register")
120 .initial_values(candidates.iter().map(|c| c.name.clone()).collect());
121 for candidate in candidates {
122 picker = picker.item(candidate.name.clone(), &candidate.name, &candidate.hint);
123 }
124 let selected: Vec<String> = match picker.interact() {
125 Ok(picks) => picks,
126 Err(error) if error.kind() == std::io::ErrorKind::Interrupted => {
129 bail!("no adapters selected; nothing to sync");
130 }
131 Err(error) => return Err(error).context("adapter picker prompt failed"),
132 };
133 if selected.is_empty() {
134 bail!("no adapters selected; nothing to sync");
135 }
136 let picks: Vec<Candidate> = candidates
137 .iter()
138 .filter(|candidate| selected.contains(&candidate.name))
139 .cloned()
140 .collect();
141 persist_accept(config_path, &picks)?;
142 Ok(picks)
143}
144
145pub fn apply_to_doc(
152 doc: &mut DocumentMut,
153 accepts: &[Candidate],
154 declines: &[&str],
155) -> anyhow::Result<()> {
156 if accepts.is_empty() && declines.is_empty() {
157 return Ok(());
158 }
159 if !doc.contains_key("adapters") {
160 let mut table = Table::new();
161 table.set_implicit(true);
162 doc.insert("adapters", Item::Table(table));
163 }
164 let adapters = adapters_table_mut(doc)?;
165 for pick in accepts {
166 let mut entry = json_to_toml_table(&pick.config).with_context(|| {
167 format!(
168 "pick for {:?} did not produce a TOML-shaped table",
169 pick.name
170 )
171 })?;
172 contract_path_key(&mut entry);
173 prepend_enabled(&mut entry, true);
174 adapters.insert(&pick.name, Item::Table(entry));
175 }
176 for name in declines {
177 let mut entry = Table::new();
178 prepend_enabled(&mut entry, false);
179 adapters.insert(name, Item::Table(entry));
180 }
181 Ok(())
182}
183
184fn contract_path_key(table: &mut Table) {
188 if let Some(path) = table.get("path").and_then(Item::as_str) {
189 let contracted = crate::config::contract_home(Path::new(path))
190 .display()
191 .to_string();
192 if contracted != path {
193 table["path"] = toml_edit::value(contracted);
194 }
195 }
196}
197
198pub fn persist_accept(config_path: &Path, picks: &[Candidate]) -> anyhow::Result<()> {
201 let mut doc = open_or_init(config_path)?;
202 apply_to_doc(&mut doc, picks, &[])?;
203 crate::config::write_config_file(config_path, &doc.to_string())?;
204 Ok(())
205}
206
207pub fn set_adapter_enabled(config_path: &Path, name: &str, enabled: bool) -> anyhow::Result<bool> {
211 let mut doc = open_or_init(config_path)?;
212 let adapters = adapters_table_mut(&mut doc)?;
213 let Some(entry) = adapters.get_mut(name).and_then(Item::as_table_mut) else {
214 return Ok(false);
215 };
216 entry.insert("enabled", toml_edit::value(enabled));
217 crate::config::write_config_file(config_path, &doc.to_string())?;
218 Ok(true)
219}
220
221fn open_or_init(config_path: &Path) -> anyhow::Result<DocumentMut> {
222 if let Some(parent) = config_path.parent() {
223 std::fs::create_dir_all(parent)
224 .with_context(|| format!("failed to create config dir {}", parent.display()))?;
225 }
226 let existing = if config_path.exists() {
227 std::fs::read_to_string(config_path)
228 .with_context(|| format!("failed to read {}", config_path.display()))?
229 } else {
230 String::new()
231 };
232 let mut doc: DocumentMut = existing
233 .parse()
234 .with_context(|| format!("failed to parse {} as TOML", config_path.display()))?;
235 if !doc.contains_key("adapters") {
236 let mut table = Table::new();
237 table.set_implicit(true);
238 doc.insert("adapters", Item::Table(table));
239 }
240 Ok(doc)
241}
242
243fn adapters_table_mut(doc: &mut DocumentMut) -> anyhow::Result<&mut Table> {
244 doc["adapters"]
245 .as_table_mut()
246 .ok_or_else(|| anyhow::anyhow!("config.toml has an `adapters` value that is not a table"))
247}
248
249fn prepend_enabled(table: &mut Table, value: bool) {
250 use toml_edit::value as tv;
251 let implicit = table.is_implicit();
252 let keys: Vec<String> = table.iter().map(|(k, _)| k.to_owned()).collect();
253 let mut existing: Vec<(String, Item)> = Vec::with_capacity(keys.len());
254 for key in keys {
255 if key == "enabled" {
256 continue;
257 }
258 if let Some(item) = table.remove(&key) {
259 existing.push((key, item));
260 }
261 }
262 table.remove("enabled");
263 let mut fresh = Table::new();
264 fresh.set_implicit(implicit);
265 fresh.insert("enabled", tv(value));
266 for (key, item) in existing {
267 fresh.insert(&key, item);
268 }
269 *table = fresh;
270}
271
272fn json_to_toml_table(value: &Value) -> anyhow::Result<Table> {
276 let Value::Object(map) = value else {
277 bail!("config blob must be a JSON object, got {value}");
278 };
279 let mut table = Table::new();
280 for (key, val) in map {
281 table[key] = json_to_toml_item(val)?;
282 }
283 Ok(table)
284}
285
286fn json_to_toml_item(value: &Value) -> anyhow::Result<Item> {
287 use toml_edit::{Array, InlineTable, Value as TomlValue, value as tv};
288 Ok(match value {
289 Value::Null => bail!("null is not representable in TOML"),
290 Value::Bool(b) => tv(*b),
291 Value::Number(n) => {
292 if let Some(i) = n.as_i64() {
293 tv(i)
294 } else if let Some(f) = n.as_f64() {
295 tv(f)
296 } else {
297 bail!("number {n} is not representable in TOML");
298 }
299 }
300 Value::String(s) => tv(s.clone()),
301 Value::Array(values) => {
302 let mut array = Array::new();
303 for v in values {
304 let item = json_to_toml_item(v)?;
305 let toml_value: TomlValue = item.into_value().map_err(|_| {
306 anyhow::anyhow!("array element {v} is not a scalar; nested tables in arrays")
307 })?;
308 array.push(toml_value);
309 }
310 Item::Value(TomlValue::Array(array))
311 }
312 Value::Object(_) => {
313 let table = json_to_toml_table(value)?;
314 let mut inline = InlineTable::new();
315 for (key, item) in table.iter() {
316 if let Some(v) = item.as_value() {
317 inline.insert(key, v.clone());
318 }
319 }
320 Item::Value(TomlValue::InlineTable(inline))
321 }
322 })
323}
324
325#[cfg(test)]
326mod tests {
327 #![allow(clippy::expect_used, clippy::unwrap_used)]
328
329 use super::*;
330 use serde_json::json;
331 use tempfile::TempDir;
332
333 #[test]
334 fn prompt_and_persist_errors_on_non_tty_stdin() {
335 let temp = TempDir::new().unwrap();
339 let config_path = temp.path().join("config.toml");
340 let candidates = vec![Candidate {
341 name: "claude-code".to_owned(),
342 hint: "/tmp/dummy".to_owned(),
343 config: json!({ "path": "/tmp/dummy" }),
344 }];
345 let err = prompt_and_persist(&config_path, &candidates, false)
346 .expect_err("non-tty stdin must error rather than hang");
347 let msg = err.to_string();
348 assert!(
349 msg.contains("not a terminal"),
350 "error should mention the non-tty branch: {msg}",
351 );
352 }
353
354 #[test]
355 fn apply_to_doc_writes_enabled_first_for_accepts_and_declines() {
356 let accept = Candidate {
357 name: "claude-code".to_owned(),
358 hint: "/tmp/cc".to_owned(),
359 config: json!({ "path": "/tmp/cc" }),
360 };
361 let mut doc = DocumentMut::new();
362 apply_to_doc(&mut doc, &[accept], &["opencode"]).unwrap();
363 let body = doc.to_string();
364 assert!(
366 body.contains("[adapters.claude-code]")
367 && body.contains("enabled = true")
368 && body.contains("path = \"/tmp/cc\""),
369 "expected accepted entry; got: {body}",
370 );
371 assert!(
373 body.contains("[adapters.opencode]") && body.contains("enabled = false"),
374 "expected declined entry; got: {body}",
375 );
376 }
377
378 #[test]
379 fn set_adapter_enabled_flips_in_place_and_reports_absence() {
380 let temp = TempDir::new().unwrap();
381 let config_path = temp.path().join("config.toml");
382 let accept = Candidate {
383 name: "claude-code".to_owned(),
384 hint: "/tmp/cc".to_owned(),
385 config: json!({ "path": "/tmp/cc" }),
386 };
387 persist_accept(&config_path, &[accept]).unwrap();
388
389 assert!(!set_adapter_enabled(&config_path, "opencode", false).unwrap());
391
392 assert!(set_adapter_enabled(&config_path, "claude-code", false).unwrap());
394 let body = std::fs::read_to_string(&config_path).unwrap();
395 assert!(
396 body.contains("enabled = false") && body.contains("path = \"/tmp/cc\""),
397 "flip must preserve the blob; got: {body}",
398 );
399 assert!(set_adapter_enabled(&config_path, "claude-code", true).unwrap());
400 assert!(
401 std::fs::read_to_string(&config_path)
402 .unwrap()
403 .contains("enabled = true")
404 );
405 }
406}