diff options
Diffstat (limited to 'src/provider.rs')
| -rw-r--r-- | src/provider.rs | 61 |
1 files changed, 49 insertions, 12 deletions
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 @@ | |||
| 1 | /* | ||
| 2 | * This file is part of agent686 | ||
| 3 | * Copyright (C) 2026 Salmonik <salmon@2137697.xyz> | ||
| 4 | * SPDX-License-Identifier: GPL-3.0-or-later | ||
| 5 | * https://git.2137697.xyz/agent686 | ||
| 6 | */ | ||
| 7 | |||
| 1 | use std::io::Read; | 8 | use std::io::Read; |
| 2 | use std::sync::atomic::{AtomicBool, Ordering}; | 9 | use std::sync::atomic::{AtomicBool, Ordering}; |
| 3 | use std::time::{Duration, Instant}; | 10 | use std::time::{Duration, Instant}; |
| @@ -183,9 +190,7 @@ fn explain(status: u16, message: &str) -> String { | |||
| 183 | ); | 190 | ); |
| 184 | } | 191 | } |
| 185 | if status == 401 || status == 403 { | 192 | if status == 401 || status == 403 { |
| 186 | return format!( | 193 | return format!("{message}\n\ncheck the key: OPENCODE_API_KEY or ~/.config/agent686/key"); |
| 187 | "{message}\n\ncheck the key: OPENCODE_API_KEY or ~/.config/agent686/key" | ||
| 188 | ); | ||
| 189 | } | 194 | } |
| 190 | if status == 429 { | 195 | if status == 429 { |
| 191 | return format!( | 196 | return format!( |
| @@ -261,7 +266,10 @@ pub fn chat_with( | |||
| 261 | .header("Content-Type", "application/json") | 266 | .header("Content-Type", "application/json") |
| 262 | .header("Accept", "text/event-stream") | 267 | .header("Accept", "text/event-stream") |
| 263 | .header("Authorization", format!("Bearer {}", cfg.api_key)) | 268 | .header("Authorization", format!("Bearer {}", cfg.api_key)) |
| 264 | .header("User-Agent", concat!("agent686/", env!("CARGO_PKG_VERSION"))); | 269 | .header( |
| 270 | "User-Agent", | ||
| 271 | concat!("agent686/", env!("CARGO_PKG_VERSION")), | ||
| 272 | ); | ||
| 265 | // OpenCode requires a stable per-conversation id for routing and | 273 | // OpenCode requires a stable per-conversation id for routing and |
| 266 | // prompt caching; a plain OpenAI-compatible endpoint must not get it. | 274 | // prompt caching; a plain OpenAI-compatible endpoint must not get it. |
| 267 | if cfg.provider.needs_session_header() { | 275 | if cfg.provider.needs_session_header() { |
| @@ -347,7 +355,14 @@ pub fn chat_with( | |||
| 347 | Err(e) => return Err(Error::Transport(e.to_string())), | 355 | Err(e) => return Err(Error::Transport(e.to_string())), |
| 348 | }; | 356 | }; |
| 349 | for data in decoder.feed(&buf[..n]) { | 357 | for data in decoder.feed(&buf[..n]) { |
| 350 | if handle_event(&data, &mut outcome, &mut calls, &mut completed, &mut dropped, sink) { | 358 | if handle_event( |
| 359 | &data, | ||
| 360 | &mut outcome, | ||
| 361 | &mut calls, | ||
| 362 | &mut completed, | ||
| 363 | &mut dropped, | ||
| 364 | sink, | ||
| 365 | ) { | ||
| 351 | done = true; | 366 | done = true; |
| 352 | break; | 367 | break; |
| 353 | } | 368 | } |
| @@ -360,7 +375,14 @@ pub fn chat_with( | |||
| 360 | // A final `data:` frame with no trailing newline is legitimate. | 375 | // A final `data:` frame with no trailing newline is legitimate. |
| 361 | if !done { | 376 | if !done { |
| 362 | for data in decoder.finish() { | 377 | for data in decoder.finish() { |
| 363 | if handle_event(&data, &mut outcome, &mut calls, &mut completed, &mut dropped, sink) { | 378 | if handle_event( |
| 379 | &data, | ||
| 380 | &mut outcome, | ||
| 381 | &mut calls, | ||
| 382 | &mut completed, | ||
| 383 | &mut dropped, | ||
| 384 | sink, | ||
| 385 | ) { | ||
| 364 | break; | 386 | break; |
| 365 | } | 387 | } |
| 366 | } | 388 | } |
| @@ -389,7 +411,8 @@ pub fn chat_with( | |||
| 389 | // be recorded as a complete answer. Lenient mode tolerates local servers | 411 | // be recorded as a complete answer. Lenient mode tolerates local servers |
| 390 | // that finish with text but no [DONE]/finish_reason. | 412 | // that finish with text but no [DONE]/finish_reason. |
| 391 | if !completed { | 413 | if !completed { |
| 392 | let lenient_ok = cfg.lenient && outcome.tool_calls.is_empty() && !outcome.content.is_empty(); | 414 | let lenient_ok = |
| 415 | cfg.lenient && outcome.tool_calls.is_empty() && !outcome.content.is_empty(); | ||
| 393 | if !lenient_ok { | 416 | if !lenient_ok { |
| 394 | return Err(Error::Transport(format!( | 417 | return Err(Error::Transport(format!( |
| 395 | "stream ended before completion ({} bytes of text, {} tool call(s)); \ | 418 | "stream ended before completion ({} bytes of text, {} tool call(s)); \ |
| @@ -424,7 +447,11 @@ fn handle_event( | |||
| 424 | } | 447 | } |
| 425 | match serde_json::from_str::<Chunk>(data) { | 448 | match serde_json::from_str::<Chunk>(data) { |
| 426 | Ok(chunk) => { | 449 | Ok(chunk) => { |
| 427 | if chunk.choices.iter().any(|choice| choice.finish_reason.is_some()) { | 450 | if chunk |
| 451 | .choices | ||
| 452 | .iter() | ||
| 453 | .any(|choice| choice.finish_reason.is_some()) | ||
| 454 | { | ||
| 428 | *completed = true; | 455 | *completed = true; |
| 429 | } | 456 | } |
| 430 | apply_chunk(&chunk, outcome, calls, sink); | 457 | apply_chunk(&chunk, outcome, calls, sink); |
| @@ -575,11 +602,16 @@ pub fn summarize(cfg: &Config, messages: &[Value]) -> Result<String, Error> { | |||
| 575 | .post(&cfg.endpoint()) | 602 | .post(&cfg.endpoint()) |
| 576 | .header("Content-Type", "application/json") | 603 | .header("Content-Type", "application/json") |
| 577 | .header("Authorization", format!("Bearer {}", cfg.api_key)) | 604 | .header("Authorization", format!("Bearer {}", cfg.api_key)) |
| 578 | .header("User-Agent", concat!("agent686/", env!("CARGO_PKG_VERSION"))); | 605 | .header( |
| 606 | "User-Agent", | ||
| 607 | concat!("agent686/", env!("CARGO_PKG_VERSION")), | ||
| 608 | ); | ||
| 579 | if cfg.provider.needs_session_header() { | 609 | if cfg.provider.needs_session_header() { |
| 580 | request = request.header("x-opencode-session", &cfg.session); | 610 | request = request.header("x-opencode-session", &cfg.session); |
| 581 | } | 611 | } |
| 582 | let mut response = request.send(bytes).map_err(|e| Error::Transport(e.to_string()))?; | 612 | let mut response = request |
| 613 | .send(bytes) | ||
| 614 | .map_err(|e| Error::Transport(e.to_string()))?; | ||
| 583 | 615 | ||
| 584 | let status = response.status().as_u16(); | 616 | let status = response.status().as_u16(); |
| 585 | let text = response | 617 | let text = response |
| @@ -610,11 +642,16 @@ pub fn models(cfg: &Config) -> Result<Vec<String>, Error> { | |||
| 610 | let mut request = agent | 642 | let mut request = agent |
| 611 | .get(&url) | 643 | .get(&url) |
| 612 | .header("Authorization", format!("Bearer {}", cfg.api_key)) | 644 | .header("Authorization", format!("Bearer {}", cfg.api_key)) |
| 613 | .header("User-Agent", concat!("agent686/", env!("CARGO_PKG_VERSION"))); | 645 | .header( |
| 646 | "User-Agent", | ||
| 647 | concat!("agent686/", env!("CARGO_PKG_VERSION")), | ||
| 648 | ); | ||
| 614 | if cfg.provider.needs_session_header() { | 649 | if cfg.provider.needs_session_header() { |
| 615 | request = request.header("x-opencode-session", &cfg.session); | 650 | request = request.header("x-opencode-session", &cfg.session); |
| 616 | } | 651 | } |
| 617 | let mut response = request.call().map_err(|e| Error::Transport(e.to_string()))?; | 652 | let mut response = request |
| 653 | .call() | ||
| 654 | .map_err(|e| Error::Transport(e.to_string()))?; | ||
| 618 | 655 | ||
| 619 | let status = response.status().as_u16(); | 656 | let status = response.status().as_u16(); |
| 620 | let body = response | 657 | let body = response |
