From e803aa488c3a95193d3e3ae358b640c299f8f121 Mon Sep 17 00:00:00 2001 From: salmonik Date: Mon, 23 Nov 2026 20:36:22 +0100 Subject: License --- src/provider.rs | 61 +++++++++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 49 insertions(+), 12 deletions(-) (limited to 'src/provider.rs') diff --git a/src/provider.rs b/src/provider.rs index b5dca62..2f8bcfc 100644 --- a/src/provider.rs +++ b/src/provider.rs @@ -1,3 +1,10 @@ +/* + * This file is part of agent686 + * Copyright (C) 2026 Salmonik + * SPDX-License-Identifier: GPL-3.0-or-later + * https://git.2137697.xyz/agent686 + */ + use std::io::Read; use std::sync::atomic::{AtomicBool, Ordering}; use std::time::{Duration, Instant}; @@ -183,9 +190,7 @@ fn explain(status: u16, message: &str) -> String { ); } if status == 401 || status == 403 { - return format!( - "{message}\n\ncheck the key: OPENCODE_API_KEY or ~/.config/agent686/key" - ); + return format!("{message}\n\ncheck the key: OPENCODE_API_KEY or ~/.config/agent686/key"); } if status == 429 { return format!( @@ -261,7 +266,10 @@ pub fn chat_with( .header("Content-Type", "application/json") .header("Accept", "text/event-stream") .header("Authorization", format!("Bearer {}", cfg.api_key)) - .header("User-Agent", concat!("agent686/", env!("CARGO_PKG_VERSION"))); + .header( + "User-Agent", + concat!("agent686/", env!("CARGO_PKG_VERSION")), + ); // OpenCode requires a stable per-conversation id for routing and // prompt caching; a plain OpenAI-compatible endpoint must not get it. if cfg.provider.needs_session_header() { @@ -347,7 +355,14 @@ pub fn chat_with( Err(e) => return Err(Error::Transport(e.to_string())), }; for data in decoder.feed(&buf[..n]) { - if handle_event(&data, &mut outcome, &mut calls, &mut completed, &mut dropped, sink) { + if handle_event( + &data, + &mut outcome, + &mut calls, + &mut completed, + &mut dropped, + sink, + ) { done = true; break; } @@ -360,7 +375,14 @@ pub fn chat_with( // A final `data:` frame with no trailing newline is legitimate. if !done { for data in decoder.finish() { - if handle_event(&data, &mut outcome, &mut calls, &mut completed, &mut dropped, sink) { + if handle_event( + &data, + &mut outcome, + &mut calls, + &mut completed, + &mut dropped, + sink, + ) { break; } } @@ -389,7 +411,8 @@ pub fn chat_with( // be recorded as a complete answer. Lenient mode tolerates local servers // that finish with text but no [DONE]/finish_reason. if !completed { - let lenient_ok = cfg.lenient && outcome.tool_calls.is_empty() && !outcome.content.is_empty(); + let lenient_ok = + cfg.lenient && outcome.tool_calls.is_empty() && !outcome.content.is_empty(); if !lenient_ok { return Err(Error::Transport(format!( "stream ended before completion ({} bytes of text, {} tool call(s)); \ @@ -424,7 +447,11 @@ fn handle_event( } match serde_json::from_str::(data) { Ok(chunk) => { - if chunk.choices.iter().any(|choice| choice.finish_reason.is_some()) { + if chunk + .choices + .iter() + .any(|choice| choice.finish_reason.is_some()) + { *completed = true; } apply_chunk(&chunk, outcome, calls, sink); @@ -575,11 +602,16 @@ pub fn summarize(cfg: &Config, messages: &[Value]) -> Result { .post(&cfg.endpoint()) .header("Content-Type", "application/json") .header("Authorization", format!("Bearer {}", cfg.api_key)) - .header("User-Agent", concat!("agent686/", env!("CARGO_PKG_VERSION"))); + .header( + "User-Agent", + concat!("agent686/", env!("CARGO_PKG_VERSION")), + ); if cfg.provider.needs_session_header() { request = request.header("x-opencode-session", &cfg.session); } - let mut response = request.send(bytes).map_err(|e| Error::Transport(e.to_string()))?; + let mut response = request + .send(bytes) + .map_err(|e| Error::Transport(e.to_string()))?; let status = response.status().as_u16(); let text = response @@ -610,11 +642,16 @@ pub fn models(cfg: &Config) -> Result, Error> { let mut request = agent .get(&url) .header("Authorization", format!("Bearer {}", cfg.api_key)) - .header("User-Agent", concat!("agent686/", env!("CARGO_PKG_VERSION"))); + .header( + "User-Agent", + concat!("agent686/", env!("CARGO_PKG_VERSION")), + ); if cfg.provider.needs_session_header() { request = request.header("x-opencode-session", &cfg.session); } - let mut response = request.call().map_err(|e| Error::Transport(e.to_string()))?; + let mut response = request + .call() + .map_err(|e| Error::Transport(e.to_string()))?; let status = response.status().as_u16(); let body = response -- cgit v1.2.3