Skip to main content

browser_automation_cli/
llm_local.rs

1//! One-shot optional LLM HTTP extract (XDG key only; no product env vars).
2//!
3//! Uses an OpenAI-compatible chat completions endpoint configured via XDG
4//! (`openrouter_api_key`, `llm_base_url`, `llm_model`). No telemetry.
5
6use std::time::Duration;
7
8use serde_json::{json, Value};
9
10use crate::error::{CliError, ErrorKind};
11use crate::xdg;
12
13/// Default OpenAI-compatible base URL (path ends before `/chat/completions`).
14pub const DEFAULT_LLM_BASE_URL: &str = "https://openrouter.ai/api/v1";
15
16/// Default model id when XDG `llm_model` is unset.
17pub const DEFAULT_LLM_MODEL: &str = "openai/gpt-4o-mini";
18
19/// Resolve API key from XDG only.
20pub fn require_api_key() -> Result<String, CliError> {
21    xdg::openrouter_api_key().ok_or_else(|| {
22        CliError::with_suggestion(
23            ErrorKind::Usage,
24            "LLM extract requires XDG openrouter_api_key",
25            "Run: browser-automation-cli config set openrouter_api_key <key>",
26        )
27    })
28}
29
30/// Base URL from XDG or default constant.
31pub fn base_url() -> String {
32    xdg::llm_base_url().unwrap_or_else(|| DEFAULT_LLM_BASE_URL.to_string())
33}
34
35/// Model from XDG or default constant.
36pub fn model() -> String {
37    xdg::llm_model().unwrap_or_else(|| DEFAULT_LLM_MODEL.to_string())
38}
39
40/// Call chat completions with retry/backoff (one-shot; no daemon).
41pub fn chat_completion(
42    system: &str,
43    user: &str,
44    schema_hint: Option<&str>,
45) -> Result<Value, CliError> {
46    let key = require_api_key()?;
47    let model = model();
48    let base = base_url().trim_end_matches('/').to_string();
49    let url = format!("{base}/chat/completions");
50
51    let mut user_content = user.to_string();
52    if let Some(schema) = schema_hint {
53        user_content.push_str("\n\nRespond with JSON matching this schema:\n");
54        user_content.push_str(schema);
55    }
56
57    let body = json!({
58        "model": model,
59        "messages": [
60            { "role": "system", "content": system },
61            { "role": "user", "content": user_content }
62        ],
63        "temperature": 0.2,
64    });
65
66    let client = reqwest::blocking::Client::builder()
67        .timeout(Duration::from_secs(60))
68        .user_agent("browser-automation-cli/0.1.2")
69        .build()
70        .map_err(|e| CliError::new(ErrorKind::Software, format!("llm client: {e}")))?;
71
72    let mut last_err = String::from("llm request failed");
73    let delays_ms = [200u64, 500, 1200];
74    for (attempt, delay) in delays_ms.iter().enumerate() {
75        let resp = client
76            .post(&url)
77            .header("Authorization", format!("Bearer {key}"))
78            .header("Content-Type", "application/json")
79            .json(&body)
80            .send();
81        match resp {
82            Ok(r) if r.status().is_success() => {
83                let v: Value = r.json().map_err(|e| {
84                    CliError::new(ErrorKind::Data, format!("llm response json: {e}"))
85                })?;
86                let answer = v
87                    .pointer("/choices/0/message/content")
88                    .and_then(|c| c.as_str())
89                    .unwrap_or("")
90                    .to_string();
91                return Ok(json!({
92                    "llm": true,
93                    "model": model,
94                    "base_url": base,
95                    "answer": answer,
96                    "raw": v,
97                    "attempt": attempt + 1,
98                }));
99            }
100            Ok(r) => {
101                last_err = format!("llm HTTP {}", r.status());
102                if r.status().as_u16() < 500 && r.status().as_u16() != 429 {
103                    break;
104                }
105            }
106            Err(e) => last_err = format!("llm: {e}"),
107        }
108        std::thread::sleep(Duration::from_millis(*delay));
109    }
110    Err(CliError::with_suggestion(
111        ErrorKind::Unavailable,
112        last_err,
113        "Check XDG openrouter_api_key, llm_base_url, llm_model and network reachability",
114    ))
115}
116
117/// Build extract+LLM payload from free text and optional question/schema.
118pub fn extract_with_llm(
119    source_text: &str,
120    question: Option<&str>,
121    schema_json: Option<&str>,
122) -> Result<Value, CliError> {
123    let q = question.unwrap_or("Summarize the key facts from the content.");
124    let system =
125        "You are a careful extraction assistant for a local CLI. Answer concisely. No telemetry.";
126    let user = format!("Question: {q}\n\nContent:\n{source_text}");
127    let mut out = chat_completion(system, &user, schema_json)?;
128    out["question"] = json!(q);
129    out["source_chars"] = json!(source_text.chars().count());
130    if let Some(s) = schema_json {
131        if let Ok(parsed) =
132            serde_json::from_str::<Value>(out.get("answer").and_then(|a| a.as_str()).unwrap_or(""))
133        {
134            out["json"] = parsed;
135        }
136        out["schema_requested"] = json!(true);
137        let _ = s;
138    }
139    Ok(out)
140}