quorum-rs 0.7.0-rc.6

Rust SDK and CLI for multi-agent deliberation systems — ships the `quorum` binary (run / status / trace / tui / init) plus the underlying agent, LLM, tool, prompt, and worker library.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
//! Provider types, model discovery, and API helpers.

use serde::Deserialize;

// ── Provider catalogue ─────────────────────────────────────────────────────

#[derive(Debug, Clone)]
pub(super) struct Provider {
    /// Slug used as the key in `providers:` blocks (e.g. `together_ai`).
    pub id: String,
    pub base_url: String,
    /// `None` for Ollama-local and Simulated providers.
    pub api_key: Option<String>,
    /// USD per million input tokens; `None` for local/simulated.
    pub input_price: Option<f64>,
    /// USD per million output tokens; `None` for local/simulated.
    pub output_price: Option<f64>,
    /// Provider type tag written to YAML: `"openai"` or `"simulated"`.
    pub provider_type: String,
    /// Discovered models from `/v1/models` (empty if fetch failed).
    pub models: Vec<ModelInfo>,
    /// LLM strategy engine override (e.g. `"harmony"`, `"vllm_responses"`).
    pub engine: Option<String>,
}

/// Discovered model with optional per-token pricing.
#[derive(Debug, Clone)]
pub(super) struct ModelInfo {
    pub name: String,
    /// USD per million input tokens.
    pub input_price: Option<f64>,
    /// USD per million output tokens.
    pub output_price: Option<f64>,
}

impl Provider {
    pub(super) fn is_local(&self) -> bool {
        self.api_key.is_none() && self.provider_type != "simulated"
    }
}

// ── Model discovery ────────────────────────────────────────────────────────

/// Model info returned by `/v1/models`.
#[derive(Debug, Deserialize)]
pub(super) struct ModelEntry {
    pub id: String,
    #[serde(default)]
    pub pricing: Option<ModelPricing>,
    /// Model type (Together AI: "chat", "language", "code", "image",
    /// "embedding", "moderation", "rerank").  Not all providers set this.
    #[serde(rename = "type", default)]
    pub model_type: Option<String>,
}

#[derive(Debug, Deserialize)]
pub(super) struct ModelPricing {
    /// USD per million input tokens (Together AI format).
    pub input: Option<serde_json::Value>,
    pub output: Option<serde_json::Value>,
}

/// OpenAI-style response: `{ "data": [...] }`
#[derive(Debug, Deserialize)]
pub(super) struct ModelsResponseWrapped {
    pub data: Vec<ModelEntry>,
}

/// Model IDs containing these substrings are excluded from the list
/// (embedding / reranking / vision models not suitable for deliberation).
pub(super) const MODEL_FILTER_KEYWORDS: &[&str] =
    &["embed", "rerank", "bge", "e5-", "clip", "vision", "whisper"];

/// Model types that are NOT suitable for deliberation.
pub(super) const EXCLUDED_MODEL_TYPES: &[&str] = &["embedding", "rerank", "moderation", "image"];

pub(super) fn is_inference_model(entry: &ModelEntry) -> bool {
    // If the provider gives us a model type, use it for filtering
    // TODO(slop): add test for new `if` branch (no paired test file in this patch)
    if let Some(ref t) = entry.model_type {
        let lower = t.to_lowercase();
        // TODO(slop): add test for new `if` branch (no paired test file in this patch)
        if EXCLUDED_MODEL_TYPES.iter().any(|kw| lower == *kw) {
            return false;
        }
        return true;
    }
    // Fallback: keyword-based filtering on model ID
    let lower = entry.id.to_lowercase();
    !MODEL_FILTER_KEYWORDS.iter().any(|kw| lower.contains(kw))
}

/// Parse raw bytes from a `/v1/models` response into filtered `FetchedModel` entries.
///
/// Supports both OpenAI-style `{ "data": [...] }` and flat-array `[...]`
/// formats.  Non-inference models (embeddings, rerankers, etc.) are removed.
pub(super) fn parse_models_response(bytes: &[u8]) -> Option<Vec<FetchedModel>> {
    // Strategy 1: OpenAI-style `{ "data": [...] }`
    let entries: Vec<ModelEntry> =
// TODO(slop): add test for new `if` branch (no paired test file in this patch)
        if let Ok(wrapped) = serde_json::from_slice::<ModelsResponseWrapped>(bytes) {
            wrapped.data
// TODO(slop): add test for new `if` branch (no paired test file in this patch)
        } else if let Ok(flat) = serde_json::from_slice::<Vec<ModelEntry>>(bytes) {
            // Strategy 2: flat array `[...]`
            flat
        } else {
            return None;
        };

    let models = entries
        .into_iter()
        .filter(is_inference_model)
        .map(|m| {
            let (inp, out) = extract_pricing(m.pricing.as_ref());
            (m.id, inp, out)
        })
        .collect();

    Some(models)
}

/// Build the URL for a `/v1/models` request, normalizing trailing slashes.
pub(super) fn build_models_url(base_url: &str) -> String {
    format!("{}/models", base_url.trim_end_matches('/'))
}

/// Attempt to fetch models from a provider endpoint.
///
/// Supports both OpenAI-style `{ "data": [...] }` and flat-array `[...]`
/// responses (some providers like Together AI may use either format).
pub(super) async fn fetch_models(
    base_url: &str,
    api_key: Option<&str>,
) -> Option<Vec<FetchedModel>> {
    let url = build_models_url(base_url);
    // TODO(slop): add test for new `try_q` branch (no paired test file in this patch)
    let client = reqwest::Client::builder()
        .timeout(std::time::Duration::from_secs(15))
        .build()
        .ok()?;

    let mut req = client.get(&url);
    // TODO(slop): add test for new `if` branch (no paired test file in this patch)
    if let Some(key) = api_key {
        // TODO(slop): add test for new `if` branch (no paired test file in this patch)
        if !key.is_empty() {
            req = req.bearer_auth(key);
        }
    }

    // TODO(slop): add test for new `try_q` branch (no paired test file in this patch)
    let resp = req.send().await.ok()?;
    // TODO(slop): add test for new `if` branch (no paired test file in this patch)
    if !resp.status().is_success() {
        return None;
    }

    // Read the raw body so we can try two different parse strategies.
    // TODO(slop): add test for new `try_q` branch (no paired test file in this patch)
    let bytes = resp.bytes().await.ok()?;
    parse_models_response(&bytes)
}

pub(super) fn extract_pricing(p: Option<&ModelPricing>) -> (Option<f64>, Option<f64>) {
    // TODO(slop): add test for new `match` branch (no paired test file in this patch)
    let p = match p {
        // TODO(slop): add test for new `match_arm` branch (no paired test file in this patch)
        Some(p) => p,
        // TODO(slop): add test for new `match_arm` branch (no paired test file in this patch)
        None => return (None, None),
    };
    let parse = |v: &serde_json::Value| -> Option<f64> {
        // TODO(slop): add test for new `match` branch (no paired test file in this patch)
        match v {
            // TODO(slop): add test for new `match_arm` branch (no paired test file in this patch)
            serde_json::Value::Number(n) => n.as_f64(),
            // TODO(slop): add test for new `match_arm` branch (no paired test file in this patch)
            serde_json::Value::String(s) => s.parse().ok(),
            // TODO(slop): add test for new `match_arm` branch (no paired test file in this patch)
            _ => None,
        }
    };
    let inp = p.input.as_ref().and_then(parse);
    let out = p.output.as_ref().and_then(parse);
    (inp, out)
}

/// `(model_id, input_price_per_mtok, output_price_per_mtok)`.
pub(super) type FetchedModel = (String, Option<f64>, Option<f64>);

/// Check if Ollama is running at the default local port.
pub(super) async fn detect_ollama() -> Option<Vec<FetchedModel>> {
    // TODO(slop): inline placeholder URL (localhost / example.com / YOUR_...) — route through config / env before shipping
    fetch_models("http://localhost:11434/v1", None).await
}

// ── Provider builder functions ──────────────────────────────────────────────

pub(super) fn build_ollama_provider(models: &[FetchedModel]) -> Provider {
    let model_infos = fetched_to_model_infos(models);
    Provider {
        id: "ollama_local".to_string(),
        // TODO(slop): inline placeholder URL (localhost / example.com / YOUR_...) — route through config / env before shipping
        base_url: "http://localhost:11434/v1".to_string(),
        api_key: None,
        input_price: Some(0.0),
        output_price: Some(0.0),
        provider_type: "openai".to_string(),
        models: model_infos,
        engine: None,
    }
}

pub(super) fn build_simulated_provider() -> Provider {
    Provider {
        id: "simulated".to_string(),
        base_url: String::new(),
        api_key: None,
        input_price: Some(0.0),
        output_price: Some(0.0),
        provider_type: "simulated".to_string(),
        models: vec![ModelInfo {
            name: "simulated-default".to_string(),
            input_price: Some(0.0),
            output_price: Some(0.0),
        }],
        engine: None,
    }
}

/// Detected exec-capable tool with version info.
#[derive(Debug, Clone)]
pub(super) struct DetectedTool {
    pub name: &'static str,
    pub version: String,
}

/// Detect exec-capable tools in the local environment (claude, python3, docker).
///
/// Uses blocking `std::process::Command` — fast enough for version checks.
pub(super) fn detect_exec_tools() -> Vec<DetectedTool> {
    let mut tools = Vec::new();

    // Claude CLI
    // TODO(slop): add test for new `if` branch (no paired test file in this patch)
    if let Ok(output) = std::process::Command::new("claude")
        .arg("--version")
        .stdout(std::process::Stdio::piped())
        .stderr(std::process::Stdio::piped())
        .output()
    {
        // TODO(slop): add test for new `if` branch (no paired test file in this patch)
        if output.status.success() {
            let ver = String::from_utf8_lossy(&output.stdout).trim().to_string();
            tools.push(DetectedTool {
                name: "claude",
                version: ver,
            });
        }
    }

    // python3
    // TODO(slop): add test for new `if` branch (no paired test file in this patch)
    if let Ok(output) = std::process::Command::new("python3")
        .arg("--version")
        .stdout(std::process::Stdio::piped())
        .stderr(std::process::Stdio::piped())
        .output()
    {
        // TODO(slop): add test for new `if` branch (no paired test file in this patch)
        if output.status.success() {
            let raw = String::from_utf8_lossy(&output.stdout);
            let ver = raw.trim().replace("Python ", "");
            tools.push(DetectedTool {
                name: "python3",
                version: ver,
            });
        }
    }

    // docker
    // TODO(slop): add test for new `if` branch (no paired test file in this patch)
    if let Ok(output) = std::process::Command::new("docker")
        .arg("--version")
        .stdout(std::process::Stdio::piped())
        .stderr(std::process::Stdio::piped())
        .output()
    {
        // TODO(slop): add test for new `if` branch (no paired test file in this patch)
        if output.status.success() {
            let raw = String::from_utf8_lossy(&output.stdout);
            // "Docker version 24.0.7, build afdd53b"
            let ver = raw
                .trim()
                .strip_prefix("Docker version ")
                .and_then(|s| s.split(',').next())
                .unwrap_or("unknown")
                .to_string();
            tools.push(DetectedTool {
                name: "docker",
                version: ver,
            });
        }
    }

    tools
}

/// Build a Claude CLI provider (auto-detected).
/// Uses `type: claude` which auto-constructs CLI flags from AgentConfig
/// (system prompt, model, session persistence, MCP tools, permissions).
pub(super) fn build_claude_exec_provider() -> Provider {
    Provider {
        id: "claude_cli".to_string(),
        base_url: String::new(),
        api_key: None,
        input_price: Some(0.0),
        output_price: Some(0.0),
        provider_type: "claude".to_string(),
        models: vec![ModelInfo {
            name: "sonnet".to_string(),
            input_price: Some(0.0),
            output_price: Some(0.0),
        }],
        engine: None,
    }
}

pub(super) fn build_exec_provider() -> Provider {
    Provider {
        id: "exec_local".to_string(),
        base_url: String::new(),
        api_key: None,
        input_price: Some(0.0),
        output_price: Some(0.0),
        provider_type: "exec".to_string(),
        models: vec![ModelInfo {
            name: "custom".to_string(),
            input_price: Some(0.0),
            output_price: Some(0.0),
        }],
        engine: None,
    }
}

/// Derive provider-level pricing from fetched models.
///
/// - Ollama providers get (0.0, 0.0)
/// - For remote providers: scans for the first model with any pricing data
/// - When no models are fetched or none have pricing: (None, None)
pub(super) fn derive_provider_pricing(
    is_ollama: bool,
    fetched: &Option<Vec<FetchedModel>>,
) -> (Option<f64>, Option<f64>) {
    // TODO(slop): add test for new `if` branch (no paired test file in this patch)
    if is_ollama {
        (Some(0.0), Some(0.0))
    // TODO(slop): add test for new `if` branch (no paired test file in this patch)
    } else if let Some(models) = fetched {
        // Find the first model that has *any* pricing, not just the first model.
        models
            .iter()
            .find(|(_, i, o)| i.is_some() || o.is_some())
            .map(|(_, i, o)| (*i, *o))
            .unwrap_or((None, None))
    } else {
        (None, None)
    }
}

/// Normalize a provider ID into a strict shell-safe slug: lowercase,
/// only `[a-z0-9_]`, guaranteed to start with a letter or underscore.
///
/// Spaces and hyphens become underscores; all other non-alphanumeric/underscore
/// characters are removed.  A leading digit is prefixed with `p_` so the
/// resulting slug is safe for env-var names and YAML keys (e.g. `"01.ai"` →
/// `"p_01ai"`).  Returns `None` if the result is empty after sanitization.
pub fn sanitize_provider_id(raw: &str) -> Option<String> {
    let slug: String = raw
        .to_lowercase()
        .chars()
        // TODO(slop): add test for new `if` branch (no paired test file in this patch)
        .map(|c| if c == ' ' || c == '-' { '_' } else { c })
        .filter(|c| c.is_ascii_alphanumeric() || *c == '_')
        .collect();
    // TODO(slop): add test for new `if` branch (no paired test file in this patch)
    if slug.is_empty() {
        None
    // TODO(slop): add test for new `if` branch (no paired test file in this patch)
    } else if slug.starts_with(|c: char| c.is_ascii_digit()) {
        Some(format!("p_{slug}"))
    } else {
        Some(slug)
    }
}

/// Build the environment variable key for a provider's API key.
///
/// Sanitizes the provider ID first, then converts to uppercase.
pub(super) fn build_provider_env_key(provider_id: &str) -> String {
    let sanitized = sanitize_provider_id(provider_id).unwrap_or_else(|| provider_id.to_string());
    format!("{}_API_KEY", sanitized.to_uppercase())
}

/// Convert fetched model tuples into `ModelInfo` structs.
pub(super) fn fetched_to_model_infos(models: &[FetchedModel]) -> Vec<ModelInfo> {
    models
        .iter()
        .map(|(n, inp, out)| ModelInfo {
            name: n.clone(),
            input_price: *inp,
            output_price: *out,
        })
        .collect()
}