diff options
| author | salmonik <salmonik@tuta.io> | 2026-11-18 12:45:05 +0100 |
|---|---|---|
| committer | salmonik <salmonik@tuta.io> | 2026-11-18 12:45:05 +0100 |
| commit | 226c44dc770dd0ea42e322af4d0edd6c14ce14db (patch) | |
| tree | e41318cd028770a1d16408712a5549ef59e36622 /src/provider.rs | |
| parent | 0.10.0: --json, /models /model /forget, git-repo gate for --yolo (diff) | |
| download | agent686-226c44dc770dd0ea42e322af4d0edd6c14ce14db.tar.gz agent686-226c44dc770dd0ea42e322af4d0edd6c14ce14db.zip | |
0.11.0: /undo via git checkpoints, /compact, provider presets, leniency, system CA
- /undo resets to a git checkpoint taken before each turn
- /compact summarises and rewrites the transcript (ADR 0003, the one sanctioned rewrite)
- --provider presets for six OpenAI-compatible gateways, local ones lenient by default
- --lenient and --system-ca
- proxies already honoured via ureq; documented
Diffstat (limited to 'src/provider.rs')
| -rw-r--r-- | src/provider.rs | 81 |
1 files changed, 60 insertions, 21 deletions
diff --git a/src/provider.rs b/src/provider.rs index b08d1f2..c03dea2 100644 --- a/src/provider.rs +++ b/src/provider.rs | |||
| @@ -197,15 +197,22 @@ fn explain(status: u16, message: &str) -> String { | |||
| 197 | } | 197 | } |
| 198 | 198 | ||
| 199 | /// One client per turn: rebuilding it per step pays a TLS handshake on every | 199 | /// One client per turn: rebuilding it per step pays a TLS handshake on every |
| 200 | /// request, which is noticeable on an i686 core. | 200 | /// request, which is noticeable on an i686 core. `system_ca` verifies against |
| 201 | pub fn agent() -> ureq::Agent { | 201 | /// the OS trust store instead of bundled roots. |
| 202 | ureq::Agent::config_builder() | 202 | pub fn agent(system_ca: bool) -> ureq::Agent { |
| 203 | let mut builder = ureq::Agent::config_builder() | ||
| 203 | .http_status_as_error(false) | 204 | .http_status_as_error(false) |
| 204 | .timeout_connect(Some(Duration::from_secs(10))) | 205 | .timeout_connect(Some(Duration::from_secs(10))) |
| 205 | .timeout_recv_response(Some(Duration::from_secs(180))) | 206 | .timeout_recv_response(Some(Duration::from_secs(180))) |
| 206 | .timeout_per_read(Some(Duration::from_secs(180))) | 207 | .timeout_per_read(Some(Duration::from_secs(180))); |
| 207 | .build() | 208 | if system_ca { |
| 208 | .into() | 209 | builder = builder.tls_config( |
| 210 | ureq::tls::TlsConfig::builder() | ||
| 211 | .root_certs(ureq::tls::RootCerts::PlatformVerifier) | ||
| 212 | .build(), | ||
| 213 | ); | ||
| 214 | } | ||
| 215 | builder.build().into() | ||
| 209 | } | 216 | } |
| 210 | 217 | ||
| 211 | pub fn chat( | 218 | pub fn chat( |
| @@ -215,7 +222,7 @@ pub fn chat( | |||
| 215 | cancel: &AtomicBool, | 222 | cancel: &AtomicBool, |
| 216 | sink: &mut dyn Sink, | 223 | sink: &mut dyn Sink, |
| 217 | ) -> Result<Outcome, Error> { | 224 | ) -> Result<Outcome, Error> { |
| 218 | chat_with(&agent(), cfg, messages, tools, cancel, sink) | 225 | chat_with(&agent(cfg.system_ca), cfg, messages, tools, cancel, sink) |
| 219 | } | 226 | } |
| 220 | 227 | ||
| 221 | pub fn chat_with( | 228 | pub fn chat_with( |
| @@ -378,14 +385,18 @@ pub fn chat_with( | |||
| 378 | } | 385 | } |
| 379 | 386 | ||
| 380 | // A connection that closes early, or a 200 that is not SSE at all, must not | 387 | // A connection that closes early, or a 200 that is not SSE at all, must not |
| 381 | // be recorded as a complete answer. | 388 | // be recorded as a complete answer. Lenient mode tolerates local servers |
| 389 | // that finish with text but no [DONE]/finish_reason. | ||
| 382 | if !completed { | 390 | if !completed { |
| 383 | return Err(Error::Transport(format!( | 391 | let lenient_ok = cfg.lenient && outcome.tool_calls.is_empty() && !outcome.content.is_empty(); |
| 384 | "stream ended before completion ({} bytes of text, {} tool call(s)); \ | 392 | if !lenient_ok { |
| 385 | the turn was not recorded as answered", | 393 | return Err(Error::Transport(format!( |
| 386 | outcome.content.len(), | 394 | "stream ended before completion ({} bytes of text, {} tool call(s)); \ |
| 387 | outcome.tool_calls.len() | 395 | the turn was not recorded as answered", |
| 388 | ))); | 396 | outcome.content.len(), |
| 397 | outcome.tool_calls.len() | ||
| 398 | ))); | ||
| 399 | } | ||
| 389 | } | 400 | } |
| 390 | 401 | ||
| 391 | sink.done(&outcome, started.elapsed()); | 402 | sink.done(&outcome, started.elapsed()); |
| @@ -555,15 +566,43 @@ struct FunctionDelta { | |||
| 555 | arguments: Option<String>, | 566 | arguments: Option<String>, |
| 556 | } | 567 | } |
| 557 | 568 | ||
| 569 | /// A single non-streaming completion, used by `/compact` to summarise history. | ||
| 570 | pub fn summarize(cfg: &Config, messages: &[Value]) -> Result<String, Error> { | ||
| 571 | let body = serde_json::json!({ "model": cfg.model, "messages": messages, "stream": false }); | ||
| 572 | let bytes = serde_json::to_vec(&body).map_err(|e| Error::Transport(e.to_string()))?; | ||
| 573 | let mut response = agent(cfg.system_ca) | ||
| 574 | .post(&cfg.endpoint()) | ||
| 575 | .header("Content-Type", "application/json") | ||
| 576 | .header("Authorization", format!("Bearer {}", cfg.api_key)) | ||
| 577 | .header("User-Agent", concat!("agent686/", env!("CARGO_PKG_VERSION"))) | ||
| 578 | .header("x-opencode-session", &cfg.session) | ||
| 579 | .send(bytes) | ||
| 580 | .map_err(|e| Error::Transport(e.to_string()))?; | ||
| 581 | |||
| 582 | let status = response.status().as_u16(); | ||
| 583 | let text = response | ||
| 584 | .body_mut() | ||
| 585 | .with_config() | ||
| 586 | .limit(2 * 1024 * 1024) | ||
| 587 | .read_to_string() | ||
| 588 | .unwrap_or_default(); | ||
| 589 | if !(200..300).contains(&status) { | ||
| 590 | return Err(Error::Status { | ||
| 591 | code: status, | ||
| 592 | message: error_message(&text), | ||
| 593 | }); | ||
| 594 | } | ||
| 595 | let value: Value = serde_json::from_str(&text) | ||
| 596 | .map_err(|e| Error::Transport(format!("could not parse the completion: {e}")))?; | ||
| 597 | Ok(value["choices"][0]["message"]["content"] | ||
| 598 | .as_str() | ||
| 599 | .unwrap_or("") | ||
| 600 | .to_string()) | ||
| 601 | } | ||
| 602 | |||
| 558 | /// Lists the models the provider offers for this key, for `--doctor`. | 603 | /// Lists the models the provider offers for this key, for `--doctor`. |
| 559 | pub fn models(cfg: &Config) -> Result<Vec<String>, Error> { | 604 | pub fn models(cfg: &Config) -> Result<Vec<String>, Error> { |
| 560 | let agent: ureq::Agent = ureq::Agent::config_builder() | 605 | let agent = agent(cfg.system_ca); |
| 561 | .http_status_as_error(false) | ||
| 562 | .timeout_connect(Some(Duration::from_secs(10))) | ||
| 563 | .timeout_recv_response(Some(Duration::from_secs(20))) | ||
| 564 | .timeout_per_read(Some(Duration::from_secs(20))) | ||
| 565 | .build() | ||
| 566 | .into(); | ||
| 567 | 606 | ||
| 568 | let url = format!("{}/models", cfg.base_url.trim_end_matches('/')); | 607 | let url = format!("{}/models", cfg.base_url.trim_end_matches('/')); |
| 569 | let mut response = agent | 608 | let mut response = agent |
