chore: import upstream snapshot with attribution
FreeBSD Smoke / FreeBSD Smoke (x86_64) (push) Has been cancelled
CI / Quality Guardrails (push) Has been cancelled
CI / Build & Test (macos-latest) (push) Has been cancelled
CI / Build & Test (ubuntu-latest) (push) Has been cancelled
CI / Build & Test (windows-latest) (push) Has been cancelled
CI / Format (push) Has been cancelled
CI / PowerShell Syntax (push) Has been cancelled
CI / Windows Cross-Target Check (Linux) (push) Has been cancelled
FreeBSD Smoke / FreeBSD Smoke (x86_64) (push) Has been cancelled
CI / Quality Guardrails (push) Has been cancelled
CI / Build & Test (macos-latest) (push) Has been cancelled
CI / Build & Test (ubuntu-latest) (push) Has been cancelled
CI / Build & Test (windows-latest) (push) Has been cancelled
CI / Format (push) Has been cancelled
CI / PowerShell Syntax (push) Has been cancelled
CI / Windows Cross-Target Check (Linux) (push) Has been cancelled
This commit is contained in:
@@ -0,0 +1,932 @@
|
||||
pub mod request;
|
||||
pub mod stream;
|
||||
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::collections::HashMap;
|
||||
use std::path::PathBuf;
|
||||
use std::sync::{LazyLock, Mutex};
|
||||
use std::time::{Instant, SystemTime};
|
||||
|
||||
const CACHE_TTL_SECS: u64 = 24 * 60 * 60;
|
||||
const ENDPOINTS_CACHE_TTL_SECS: u64 = 60 * 60;
|
||||
const DEFAULT_CACHE_NAMESPACE: &str = "openrouter";
|
||||
|
||||
/// Default provider order for Kimi models when no local stats exist yet.
|
||||
/// Ordered for practical coding use: speed first, then cache quality, then cost.
|
||||
pub const KIMI_FALLBACK_PROVIDERS: &[&str] = &["Fireworks", "Moonshot AI", "Together", "DeepInfra"];
|
||||
|
||||
/// Known provider names for autocomplete when OpenRouter doesn't supply a list.
|
||||
const KNOWN_PROVIDERS: &[&str] = &[
|
||||
"Moonshot AI",
|
||||
"OpenAI",
|
||||
"Anthropic",
|
||||
"Fireworks",
|
||||
"Together",
|
||||
"DeepInfra",
|
||||
];
|
||||
|
||||
/// Short aliases to normalize provider input.
|
||||
const PROVIDER_ALIASES: &[(&str, &str)] = &[
|
||||
("moonshot", "Moonshot AI"),
|
||||
("moonshotai", "Moonshot AI"),
|
||||
("openai", "OpenAI"),
|
||||
("anthropic", "Anthropic"),
|
||||
("fireworks", "Fireworks"),
|
||||
("together", "Together"),
|
||||
("deepinfra", "DeepInfra"),
|
||||
];
|
||||
|
||||
/// Known OpenRouter provider names for autocomplete/fallback suggestions.
|
||||
pub fn known_providers() -> Vec<String> {
|
||||
KNOWN_PROVIDERS.iter().map(|p| (*p).to_string()).collect()
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct ModelInfo {
|
||||
pub id: String,
|
||||
#[serde(default)]
|
||||
pub name: String,
|
||||
pub context_length: Option<u64>,
|
||||
#[serde(default)]
|
||||
pub pricing: ModelPricing,
|
||||
#[serde(default)]
|
||||
pub created: Option<u64>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
|
||||
pub struct ModelPricing {
|
||||
#[serde(default, deserialize_with = "deserialize_optional_string_or_number")]
|
||||
pub prompt: Option<String>,
|
||||
#[serde(default, deserialize_with = "deserialize_optional_string_or_number")]
|
||||
pub completion: Option<String>,
|
||||
#[serde(
|
||||
default,
|
||||
rename = "input_cache_read",
|
||||
deserialize_with = "deserialize_optional_string_or_number"
|
||||
)]
|
||||
pub input_cache_read: Option<String>,
|
||||
#[serde(
|
||||
default,
|
||||
rename = "input_cache_write",
|
||||
deserialize_with = "deserialize_optional_string_or_number"
|
||||
)]
|
||||
pub input_cache_write: Option<String>,
|
||||
}
|
||||
|
||||
fn deserialize_optional_string_or_number<'de, D>(
|
||||
deserializer: D,
|
||||
) -> Result<Option<String>, D::Error>
|
||||
where
|
||||
D: serde::Deserializer<'de>,
|
||||
{
|
||||
let value = Option::<serde_json::Value>::deserialize(deserializer)?;
|
||||
Ok(match value {
|
||||
Some(serde_json::Value::String(value)) => Some(value),
|
||||
Some(serde_json::Value::Number(value)) => Some(value.to_string()),
|
||||
Some(serde_json::Value::Null) | None => None,
|
||||
Some(other) => {
|
||||
return Err(serde::de::Error::custom(format!(
|
||||
"expected string, number, or null for pricing value, got {other}"
|
||||
)));
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct EndpointInfo {
|
||||
pub provider_name: String,
|
||||
#[serde(default)]
|
||||
pub tag: Option<String>,
|
||||
#[serde(default)]
|
||||
pub pricing: ModelPricing,
|
||||
#[serde(default)]
|
||||
pub context_length: Option<u64>,
|
||||
#[serde(default)]
|
||||
pub max_completion_tokens: Option<u64>,
|
||||
#[serde(default)]
|
||||
pub quantization: Option<String>,
|
||||
#[serde(default)]
|
||||
pub uptime_last_30m: Option<f64>,
|
||||
#[serde(default)]
|
||||
pub latency_last_30m: Option<serde_json::Value>,
|
||||
#[serde(default)]
|
||||
pub throughput_last_30m: Option<serde_json::Value>,
|
||||
#[serde(default)]
|
||||
pub supports_implicit_caching: Option<bool>,
|
||||
#[serde(default)]
|
||||
pub status: Option<i32>,
|
||||
}
|
||||
|
||||
impl EndpointInfo {
|
||||
fn extract_p50(value: &serde_json::Value) -> Option<f64> {
|
||||
match value {
|
||||
serde_json::Value::Number(n) => n.as_f64(),
|
||||
serde_json::Value::Object(map) => map.get("p50").and_then(|v| v.as_f64()),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn detail_string(&self) -> String {
|
||||
let mut parts = Vec::new();
|
||||
if let Some(ref prompt) = self.pricing.prompt
|
||||
&& let Ok(p) = prompt.parse::<f64>()
|
||||
{
|
||||
parts.push(format!("in ${:.2}/M", p * 1e6));
|
||||
}
|
||||
if let Some(ref completion) = self.pricing.completion
|
||||
&& let Ok(c) = completion.parse::<f64>()
|
||||
{
|
||||
parts.push(format!("out ${:.2}/M", c * 1e6));
|
||||
}
|
||||
if let Some(ref cache_write) = self.pricing.input_cache_write
|
||||
&& let Ok(cw) = cache_write.parse::<f64>()
|
||||
&& cw > 0.0
|
||||
{
|
||||
parts.push(format!("cache write ${:.2}/M", cw * 1e6));
|
||||
}
|
||||
if let Some(ref cache_read) = self.pricing.input_cache_read
|
||||
&& let Ok(cr) = cache_read.parse::<f64>()
|
||||
&& cr > 0.0
|
||||
{
|
||||
parts.push(format!("cache read ${:.2}/M", cr * 1e6));
|
||||
}
|
||||
if let Some(uptime) = self.uptime_last_30m {
|
||||
parts.push(format!("{:.0}%", uptime));
|
||||
}
|
||||
if let Some(ref latency) = self.latency_last_30m
|
||||
&& let Some(l) = Self::extract_p50(latency)
|
||||
&& l > 0.0
|
||||
{
|
||||
parts.push(format!("{:.0}ms p50", l));
|
||||
}
|
||||
if let Some(ref tps) = self.throughput_last_30m
|
||||
&& let Some(t) = Self::extract_p50(tps)
|
||||
&& t > 0.0
|
||||
{
|
||||
parts.push(format!("{:.0}tps", t));
|
||||
}
|
||||
if let Some(cache) = self.supports_implicit_caching {
|
||||
parts.push(if cache { "cache on" } else { "cache off" }.to_string());
|
||||
}
|
||||
if let Some(ref q) = self.quantization
|
||||
&& q != "unknown"
|
||||
{
|
||||
parts.push(q.clone());
|
||||
}
|
||||
parts.join(", ")
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct DiskCache {
|
||||
pub cached_at: u64,
|
||||
#[serde(default, skip_serializing_if = "Option::is_none")]
|
||||
pub source_api_base: Option<String>,
|
||||
pub models: Vec<ModelInfo>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
struct DiskCacheMemoEntry {
|
||||
modified_at: Option<SystemTime>,
|
||||
cache: Option<DiskCache>,
|
||||
}
|
||||
|
||||
static DISK_CACHE_MEMO: LazyLock<Mutex<HashMap<PathBuf, DiskCacheMemoEntry>>> =
|
||||
LazyLock::new(|| Mutex::new(HashMap::new()));
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
struct EndpointsDiskCache {
|
||||
cached_at: u64,
|
||||
endpoints: Vec<EndpointInfo>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
struct EndpointsDiskCacheMemoEntry {
|
||||
modified_at: Option<SystemTime>,
|
||||
cache: Option<EndpointsDiskCache>,
|
||||
}
|
||||
|
||||
static ENDPOINTS_DISK_CACHE_MEMO: LazyLock<Mutex<HashMap<PathBuf, EndpointsDiskCacheMemoEntry>>> =
|
||||
LazyLock::new(|| Mutex::new(HashMap::new()));
|
||||
|
||||
#[derive(Debug, Default, Clone)]
|
||||
pub struct ModelsCache {
|
||||
pub models: Vec<ModelInfo>,
|
||||
pub fetched: bool,
|
||||
pub cached_at: Option<u64>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Default, Clone)]
|
||||
pub struct ModelCatalogRefreshState {
|
||||
pub in_flight: bool,
|
||||
pub last_attempt_unix: Option<u64>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub enum PinSource {
|
||||
Explicit,
|
||||
Observed,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct ProviderPin {
|
||||
pub model: String,
|
||||
pub provider: String,
|
||||
pub source: PinSource,
|
||||
pub allow_fallbacks: bool,
|
||||
pub last_cache_read: Option<Instant>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct ParsedProvider {
|
||||
pub name: String,
|
||||
pub allow_fallbacks: bool,
|
||||
}
|
||||
|
||||
pub fn normalize_provider_name(raw: &str) -> String {
|
||||
let trimmed = raw.trim();
|
||||
if trimmed.is_empty() {
|
||||
return String::new();
|
||||
}
|
||||
|
||||
let lower = trimmed.to_lowercase();
|
||||
for (alias, canonical) in PROVIDER_ALIASES {
|
||||
if lower == *alias {
|
||||
return (*canonical).to_string();
|
||||
}
|
||||
}
|
||||
|
||||
for known in KNOWN_PROVIDERS {
|
||||
if known.eq_ignore_ascii_case(trimmed) {
|
||||
return (*known).to_string();
|
||||
}
|
||||
}
|
||||
|
||||
let simplified: String = lower
|
||||
.chars()
|
||||
.filter(|c| c.is_ascii_alphanumeric())
|
||||
.collect();
|
||||
for known in KNOWN_PROVIDERS {
|
||||
let known_simple: String = known
|
||||
.to_lowercase()
|
||||
.chars()
|
||||
.filter(|c| c.is_ascii_alphanumeric())
|
||||
.collect();
|
||||
if known_simple == simplified {
|
||||
return (*known).to_string();
|
||||
}
|
||||
}
|
||||
|
||||
trimmed.to_string()
|
||||
}
|
||||
|
||||
pub fn parse_model_spec(raw: &str) -> (String, Option<ParsedProvider>) {
|
||||
let trimmed = raw.trim();
|
||||
if let Some((model, provider)) = trimmed.rsplit_once('@') {
|
||||
let model = model.trim();
|
||||
let mut provider = provider.trim();
|
||||
if model.is_empty() {
|
||||
return (trimmed.to_string(), None);
|
||||
}
|
||||
if provider.is_empty() {
|
||||
return (model.to_string(), None);
|
||||
}
|
||||
let mut allow_fallbacks = true;
|
||||
if provider.ends_with('!') {
|
||||
provider = provider.trim_end_matches('!').trim();
|
||||
allow_fallbacks = false;
|
||||
}
|
||||
if provider.is_empty() {
|
||||
return (model.to_string(), None);
|
||||
}
|
||||
if provider.eq_ignore_ascii_case("auto") {
|
||||
return (model.to_string(), None);
|
||||
}
|
||||
let provider = normalize_provider_name(provider);
|
||||
return (
|
||||
model.to_string(),
|
||||
Some(ParsedProvider {
|
||||
name: provider,
|
||||
allow_fallbacks,
|
||||
}),
|
||||
);
|
||||
}
|
||||
|
||||
(trimmed.to_string(), None)
|
||||
}
|
||||
|
||||
pub fn current_unix_secs() -> Option<u64> {
|
||||
std::time::SystemTime::now()
|
||||
.duration_since(std::time::UNIX_EPOCH)
|
||||
.ok()
|
||||
.map(|d| d.as_secs())
|
||||
}
|
||||
|
||||
fn sanitize_cache_namespace(raw: &str) -> String {
|
||||
let sanitized: String = raw
|
||||
.chars()
|
||||
.filter(|c| c.is_ascii_alphanumeric() || *c == '-' || *c == '_')
|
||||
.collect();
|
||||
if sanitized.is_empty() {
|
||||
DEFAULT_CACHE_NAMESPACE.to_string()
|
||||
} else {
|
||||
sanitized
|
||||
}
|
||||
}
|
||||
|
||||
fn configured_cache_namespace() -> String {
|
||||
let raw = std::env::var("JCODE_OPENROUTER_CACHE_NAMESPACE")
|
||||
.ok()
|
||||
.map(|v| v.trim().to_string())
|
||||
.filter(|v| !v.is_empty())
|
||||
.unwrap_or_else(|| DEFAULT_CACHE_NAMESPACE.to_string());
|
||||
|
||||
sanitize_cache_namespace(&raw)
|
||||
}
|
||||
|
||||
fn cache_path_for_namespace(namespace: &str) -> PathBuf {
|
||||
let namespace = sanitize_cache_namespace(namespace);
|
||||
if let Ok(path) = std::env::var("JCODE_HOME") {
|
||||
return PathBuf::from(path)
|
||||
.join("cache")
|
||||
.join(format!("{}_models.json", namespace));
|
||||
}
|
||||
|
||||
dirs::home_dir()
|
||||
.unwrap_or_else(|| PathBuf::from("."))
|
||||
.join(".jcode")
|
||||
.join("cache")
|
||||
.join(format!("{}_models.json", namespace))
|
||||
}
|
||||
|
||||
fn cache_path() -> PathBuf {
|
||||
cache_path_for_namespace(&configured_cache_namespace())
|
||||
}
|
||||
|
||||
fn disk_cache_modified_at(path: &PathBuf) -> Option<SystemTime> {
|
||||
std::fs::metadata(path).ok()?.modified().ok()
|
||||
}
|
||||
|
||||
fn fresh_disk_cache(cache: Option<DiskCache>) -> Option<DiskCache> {
|
||||
let now = current_unix_secs()?;
|
||||
let cache = cache?;
|
||||
if now.saturating_sub(cache.cached_at) < CACHE_TTL_SECS {
|
||||
Some(cache)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
fn load_disk_cache_entry_from_path(path: PathBuf) -> Option<DiskCache> {
|
||||
let modified_at = disk_cache_modified_at(&path);
|
||||
|
||||
if let Ok(memo) = DISK_CACHE_MEMO.lock()
|
||||
&& let Some(entry) = memo.get(&path)
|
||||
&& entry.modified_at == modified_at
|
||||
{
|
||||
return fresh_disk_cache(entry.cache.clone());
|
||||
}
|
||||
|
||||
let loaded = std::fs::read_to_string(&path)
|
||||
.ok()
|
||||
.and_then(|content| serde_json::from_str::<DiskCache>(&content).ok());
|
||||
|
||||
if let Ok(mut memo) = DISK_CACHE_MEMO.lock() {
|
||||
memo.insert(
|
||||
path,
|
||||
DiskCacheMemoEntry {
|
||||
modified_at,
|
||||
cache: loaded.clone(),
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
fresh_disk_cache(loaded)
|
||||
}
|
||||
|
||||
pub fn load_disk_cache_entry() -> Option<DiskCache> {
|
||||
load_disk_cache_entry_from_path(cache_path())
|
||||
}
|
||||
|
||||
pub fn load_disk_cache_entry_for_namespace(namespace: &str) -> Option<DiskCache> {
|
||||
load_disk_cache_entry_from_path(cache_path_for_namespace(namespace))
|
||||
}
|
||||
|
||||
pub fn load_disk_cache() -> Option<Vec<ModelInfo>> {
|
||||
load_disk_cache_entry().map(|cache| cache.models)
|
||||
}
|
||||
|
||||
pub fn load_model_pricing_disk_cache_public(model_id: &str) -> Option<ModelPricing> {
|
||||
load_disk_cache()?
|
||||
.into_iter()
|
||||
.find(|model| model.id == model_id)
|
||||
.map(|model| model.pricing)
|
||||
}
|
||||
|
||||
pub type ModelTimestampIndex = HashMap<String, u64>;
|
||||
|
||||
pub fn model_created_timestamp(model_id: &str) -> Option<u64> {
|
||||
let timestamps = load_model_timestamp_index();
|
||||
model_created_timestamp_from_index(model_id, ×tamps)
|
||||
}
|
||||
|
||||
pub fn model_created_timestamp_from_index(
|
||||
model_id: &str,
|
||||
timestamps: &ModelTimestampIndex,
|
||||
) -> Option<u64> {
|
||||
if let Some(ts) = timestamps.get(model_id).copied() {
|
||||
return Some(ts);
|
||||
}
|
||||
|
||||
let candidates = openrouter_id_candidates(model_id);
|
||||
for candidate in &candidates {
|
||||
if let Some(ts) = timestamps.get(candidate).copied() {
|
||||
return Some(ts);
|
||||
}
|
||||
}
|
||||
|
||||
None
|
||||
}
|
||||
|
||||
fn openrouter_id_candidates(model: &str) -> Vec<String> {
|
||||
let mut candidates = Vec::new();
|
||||
if model.starts_with("claude-") || model.starts_with("claude_") {
|
||||
candidates.push(format!("anthropic/{}", model));
|
||||
if let Some(pos) = model.rfind('-') {
|
||||
let mut dotted = model.to_string();
|
||||
dotted.replace_range(pos..pos + 1, ".");
|
||||
candidates.push(format!("anthropic/{}", dotted));
|
||||
}
|
||||
} else if model.starts_with("gpt-")
|
||||
|| model.starts_with("codex-")
|
||||
|| model.starts_with("o1")
|
||||
|| model.starts_with("o3")
|
||||
|| model.starts_with("o4")
|
||||
{
|
||||
candidates.push(format!("openai/{}", model));
|
||||
}
|
||||
candidates
|
||||
}
|
||||
|
||||
pub fn load_model_timestamp_index() -> ModelTimestampIndex {
|
||||
all_model_timestamps().into_iter().collect()
|
||||
}
|
||||
|
||||
pub fn all_model_timestamps() -> Vec<(String, u64)> {
|
||||
load_disk_cache_entry()
|
||||
.into_iter()
|
||||
.flat_map(|cache| cache.models)
|
||||
.filter_map(|m| normalize_model_created_timestamp(m.created).map(|t| (m.id, t)))
|
||||
.collect()
|
||||
}
|
||||
|
||||
fn normalize_model_created_timestamp(created: Option<u64>) -> Option<u64> {
|
||||
let ts = created?;
|
||||
// Model providers occasionally return malformed `created` values. Avoid
|
||||
// rendering obviously bogus dates such as "Apr 1993" in the model picker.
|
||||
const FIRST_PLAUSIBLE_MODEL_RELEASE_SECS: u64 = 1_577_836_800; // 2020-01-01
|
||||
const ONE_YEAR_SECS: u64 = 365 * 24 * 60 * 60;
|
||||
let now_plus_slack = std::time::SystemTime::now()
|
||||
.duration_since(std::time::UNIX_EPOCH)
|
||||
.map(|d| d.as_secs().saturating_add(ONE_YEAR_SECS))
|
||||
.unwrap_or(u64::MAX);
|
||||
|
||||
(FIRST_PLAUSIBLE_MODEL_RELEASE_SECS..=now_plus_slack)
|
||||
.contains(&ts)
|
||||
.then_some(ts)
|
||||
}
|
||||
|
||||
pub fn save_disk_cache(models: &[ModelInfo]) {
|
||||
save_disk_cache_with_source(models, None);
|
||||
}
|
||||
|
||||
pub fn save_disk_cache_with_source(models: &[ModelInfo], source_api_base: Option<&str>) {
|
||||
save_disk_cache_with_source_to_path(cache_path(), models, source_api_base);
|
||||
}
|
||||
|
||||
pub fn save_disk_cache_with_source_for_namespace(
|
||||
namespace: &str,
|
||||
models: &[ModelInfo],
|
||||
source_api_base: Option<&str>,
|
||||
) {
|
||||
save_disk_cache_with_source_to_path(
|
||||
cache_path_for_namespace(namespace),
|
||||
models,
|
||||
source_api_base,
|
||||
);
|
||||
}
|
||||
|
||||
fn save_disk_cache_with_source_to_path(
|
||||
path: PathBuf,
|
||||
models: &[ModelInfo],
|
||||
source_api_base: Option<&str>,
|
||||
) {
|
||||
if let Some(parent) = path.parent() {
|
||||
let _ = std::fs::create_dir_all(parent);
|
||||
}
|
||||
|
||||
let now = std::time::SystemTime::now()
|
||||
.duration_since(std::time::UNIX_EPOCH)
|
||||
.map(|d| d.as_secs())
|
||||
.unwrap_or(0);
|
||||
|
||||
let cache = DiskCache {
|
||||
cached_at: now,
|
||||
source_api_base: source_api_base.map(ToString::to_string),
|
||||
models: models.to_vec(),
|
||||
};
|
||||
|
||||
if let Ok(content) = serde_json::to_string(&cache) {
|
||||
let _ = std::fs::write(&path, content);
|
||||
}
|
||||
|
||||
if let Ok(mut memo) = DISK_CACHE_MEMO.lock() {
|
||||
memo.insert(
|
||||
path.clone(),
|
||||
DiskCacheMemoEntry {
|
||||
modified_at: disk_cache_modified_at(&path),
|
||||
cache: Some(cache),
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
fn endpoints_cache_path(model: &str) -> PathBuf {
|
||||
let safe_name = model.replace('/', "__");
|
||||
let namespace = configured_cache_namespace();
|
||||
dirs::home_dir()
|
||||
.unwrap_or_else(|| PathBuf::from("."))
|
||||
.join(".jcode")
|
||||
.join("cache")
|
||||
.join(format!("{}_endpoints_{}.json", namespace, safe_name))
|
||||
}
|
||||
|
||||
pub fn load_endpoints_disk_cache_public(model: &str) -> Option<(Vec<EndpointInfo>, u64)> {
|
||||
let path = endpoints_cache_path(model);
|
||||
let modified_at = disk_cache_modified_at(&path);
|
||||
let cache = if let Ok(memo) = ENDPOINTS_DISK_CACHE_MEMO.lock()
|
||||
&& let Some(entry) = memo.get(&path)
|
||||
&& entry.modified_at == modified_at
|
||||
{
|
||||
entry.cache.clone()?
|
||||
} else {
|
||||
let loaded = std::fs::read_to_string(&path)
|
||||
.ok()
|
||||
.and_then(|content| serde_json::from_str::<EndpointsDiskCache>(&content).ok());
|
||||
if let Ok(mut memo) = ENDPOINTS_DISK_CACHE_MEMO.lock() {
|
||||
memo.insert(
|
||||
path.clone(),
|
||||
EndpointsDiskCacheMemoEntry {
|
||||
modified_at,
|
||||
cache: loaded.clone(),
|
||||
},
|
||||
);
|
||||
}
|
||||
loaded?
|
||||
};
|
||||
if cache.endpoints.is_empty() {
|
||||
return None;
|
||||
}
|
||||
let now = std::time::SystemTime::now()
|
||||
.duration_since(std::time::UNIX_EPOCH)
|
||||
.ok()?
|
||||
.as_secs();
|
||||
let age = now.saturating_sub(cache.cached_at);
|
||||
Some((cache.endpoints, age))
|
||||
}
|
||||
|
||||
pub fn load_endpoints_disk_cache(model: &str) -> Option<Vec<EndpointInfo>> {
|
||||
let path = endpoints_cache_path(model);
|
||||
let modified_at = disk_cache_modified_at(&path);
|
||||
let cache = if let Ok(memo) = ENDPOINTS_DISK_CACHE_MEMO.lock()
|
||||
&& let Some(entry) = memo.get(&path)
|
||||
&& entry.modified_at == modified_at
|
||||
{
|
||||
entry.cache.clone()?
|
||||
} else {
|
||||
let loaded = std::fs::read_to_string(&path)
|
||||
.ok()
|
||||
.and_then(|content| serde_json::from_str::<EndpointsDiskCache>(&content).ok());
|
||||
if let Ok(mut memo) = ENDPOINTS_DISK_CACHE_MEMO.lock() {
|
||||
memo.insert(
|
||||
path.clone(),
|
||||
EndpointsDiskCacheMemoEntry {
|
||||
modified_at,
|
||||
cache: loaded.clone(),
|
||||
},
|
||||
);
|
||||
}
|
||||
loaded?
|
||||
};
|
||||
let now = std::time::SystemTime::now()
|
||||
.duration_since(std::time::UNIX_EPOCH)
|
||||
.ok()?
|
||||
.as_secs();
|
||||
if now - cache.cached_at < ENDPOINTS_CACHE_TTL_SECS {
|
||||
Some(cache.endpoints)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
pub fn save_endpoints_disk_cache(model: &str, endpoints: &[EndpointInfo]) {
|
||||
let path = endpoints_cache_path(model);
|
||||
if let Some(parent) = path.parent() {
|
||||
let _ = std::fs::create_dir_all(parent);
|
||||
}
|
||||
let now = std::time::SystemTime::now()
|
||||
.duration_since(std::time::UNIX_EPOCH)
|
||||
.map(|d| d.as_secs())
|
||||
.unwrap_or(0);
|
||||
let cache = EndpointsDiskCache {
|
||||
cached_at: now,
|
||||
endpoints: endpoints.to_vec(),
|
||||
};
|
||||
if let Ok(content) = serde_json::to_string(&cache) {
|
||||
let _ = std::fs::write(&path, content);
|
||||
}
|
||||
|
||||
if let Ok(mut memo) = ENDPOINTS_DISK_CACHE_MEMO.lock() {
|
||||
memo.insert(
|
||||
path.clone(),
|
||||
EndpointsDiskCacheMemoEntry {
|
||||
modified_at: disk_cache_modified_at(&path),
|
||||
cache: Some(cache),
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct ProviderRouting {
|
||||
pub order: Option<Vec<String>>,
|
||||
pub allow_fallbacks: bool,
|
||||
pub sort: Option<String>,
|
||||
pub preferred_min_throughput: Option<u32>,
|
||||
pub preferred_max_latency: Option<u32>,
|
||||
pub max_price: Option<f64>,
|
||||
pub require_parameters: Option<bool>,
|
||||
}
|
||||
|
||||
impl Default for ProviderRouting {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
order: None,
|
||||
allow_fallbacks: true,
|
||||
sort: None,
|
||||
preferred_min_throughput: None,
|
||||
preferred_max_latency: None,
|
||||
max_price: None,
|
||||
require_parameters: None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl ProviderRouting {
|
||||
pub fn is_empty(&self) -> bool {
|
||||
self.order.is_none()
|
||||
&& self.sort.is_none()
|
||||
&& self.preferred_min_throughput.is_none()
|
||||
&& self.preferred_max_latency.is_none()
|
||||
&& self.max_price.is_none()
|
||||
&& self.require_parameters.is_none()
|
||||
&& self.allow_fallbacks
|
||||
}
|
||||
}
|
||||
|
||||
pub fn parse_provider_routing_from_env() -> ProviderRouting {
|
||||
let mut routing = ProviderRouting::default();
|
||||
|
||||
if let Ok(providers) = std::env::var("JCODE_OPENROUTER_PROVIDER") {
|
||||
let order: Vec<String> = providers
|
||||
.split(',')
|
||||
.map(|s| s.trim().to_string())
|
||||
.filter(|s| !s.is_empty())
|
||||
.collect();
|
||||
if !order.is_empty() {
|
||||
routing.order = Some(order);
|
||||
}
|
||||
}
|
||||
|
||||
if std::env::var("JCODE_OPENROUTER_NO_FALLBACK").is_ok() {
|
||||
routing.allow_fallbacks = false;
|
||||
}
|
||||
|
||||
routing
|
||||
}
|
||||
|
||||
pub fn is_kimi_model(model: &str) -> bool {
|
||||
let lower = model.to_lowercase();
|
||||
lower.contains("moonshotai/") || lower.contains("kimi-k2") || lower.contains("kimi-k2.5")
|
||||
}
|
||||
|
||||
pub fn rank_providers_from_endpoints(endpoints: &[EndpointInfo]) -> Vec<String> {
|
||||
if endpoints.is_empty() {
|
||||
return Vec::new();
|
||||
}
|
||||
|
||||
let cache_available = endpoints.iter().any(|e| {
|
||||
e.supports_implicit_caching == Some(true)
|
||||
|| e.pricing
|
||||
.input_cache_read
|
||||
.as_deref()
|
||||
.and_then(|v| v.parse::<f64>().ok())
|
||||
.unwrap_or(0.0)
|
||||
> 0.0
|
||||
});
|
||||
|
||||
let mut candidates: Vec<&EndpointInfo> =
|
||||
endpoints.iter().filter(|e| e.status != Some(1)).collect();
|
||||
|
||||
if cache_available {
|
||||
let cache_candidates: Vec<&EndpointInfo> = candidates
|
||||
.iter()
|
||||
.filter(|e| {
|
||||
e.supports_implicit_caching == Some(true)
|
||||
|| e.pricing
|
||||
.input_cache_read
|
||||
.as_deref()
|
||||
.and_then(|v| v.parse::<f64>().ok())
|
||||
.unwrap_or(0.0)
|
||||
> 0.0
|
||||
})
|
||||
.copied()
|
||||
.collect();
|
||||
if !cache_candidates.is_empty() {
|
||||
candidates = cache_candidates;
|
||||
}
|
||||
}
|
||||
|
||||
if candidates.is_empty() {
|
||||
return Vec::new();
|
||||
}
|
||||
|
||||
let mut scored: Vec<(f64, &str)> = candidates
|
||||
.iter()
|
||||
.map(|e| {
|
||||
let throughput = e
|
||||
.throughput_last_30m
|
||||
.as_ref()
|
||||
.and_then(EndpointInfo::extract_p50)
|
||||
.unwrap_or(0.0);
|
||||
let uptime = e.uptime_last_30m.unwrap_or(0.0) / 100.0;
|
||||
let cost = e
|
||||
.pricing
|
||||
.prompt
|
||||
.as_deref()
|
||||
.and_then(|v| v.parse::<f64>().ok())
|
||||
.unwrap_or(0.0);
|
||||
let cost_score = if cost > 0.0 {
|
||||
1.0 / (1.0 + cost * 1e6)
|
||||
} else {
|
||||
0.5
|
||||
};
|
||||
|
||||
let score = 0.50 * throughput.min(200.0) / 200.0 + 0.30 * uptime + 0.20 * cost_score;
|
||||
|
||||
(score, e.provider_name.as_str())
|
||||
})
|
||||
.collect();
|
||||
|
||||
scored.sort_by(|a, b| b.0.partial_cmp(&a.0).unwrap_or(std::cmp::Ordering::Equal));
|
||||
scored
|
||||
.into_iter()
|
||||
.map(|(_, name)| name.to_string())
|
||||
.collect()
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn parse_model_spec_handles_provider_aliases_and_auto() {
|
||||
let (model, provider) = parse_model_spec("anthropic/claude-sonnet-4@Fireworks");
|
||||
assert_eq!(model, "anthropic/claude-sonnet-4");
|
||||
let provider = provider.expect("provider");
|
||||
assert_eq!(provider.name, "Fireworks");
|
||||
assert!(provider.allow_fallbacks);
|
||||
|
||||
let (model, provider) = parse_model_spec("anthropic/claude-sonnet-4@Fireworks!");
|
||||
assert_eq!(model, "anthropic/claude-sonnet-4");
|
||||
let provider = provider.expect("provider");
|
||||
assert_eq!(provider.name, "Fireworks");
|
||||
assert!(!provider.allow_fallbacks);
|
||||
|
||||
let (model, provider) = parse_model_spec("moonshotai/kimi-k2.5@moonshot");
|
||||
assert_eq!(model, "moonshotai/kimi-k2.5");
|
||||
let provider = provider.expect("provider");
|
||||
assert_eq!(provider.name, "Moonshot AI");
|
||||
|
||||
let (model, provider) = parse_model_spec("anthropic/claude-sonnet-4@auto");
|
||||
assert_eq!(model, "anthropic/claude-sonnet-4");
|
||||
assert!(provider.is_none());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn model_created_timestamp_from_index_handles_provider_aliases() {
|
||||
let timestamps = ModelTimestampIndex::from([
|
||||
("anthropic/claude-opus-4.7".to_string(), 100),
|
||||
("openai/gpt-5.4".to_string(), 200),
|
||||
("moonshotai/kimi-k2.6".to_string(), 300),
|
||||
]);
|
||||
|
||||
assert_eq!(
|
||||
model_created_timestamp_from_index("claude-opus-4-7", ×tamps),
|
||||
Some(100)
|
||||
);
|
||||
assert_eq!(
|
||||
model_created_timestamp_from_index("gpt-5.4", ×tamps),
|
||||
Some(200)
|
||||
);
|
||||
assert_eq!(
|
||||
model_created_timestamp_from_index("moonshotai/kimi-k2.6", ×tamps),
|
||||
Some(300)
|
||||
);
|
||||
assert_eq!(
|
||||
model_created_timestamp_from_index("unknown-model", ×tamps),
|
||||
None
|
||||
);
|
||||
}
|
||||
|
||||
fn make_endpoint(
|
||||
name: &str,
|
||||
throughput: f64,
|
||||
uptime: f64,
|
||||
cache: bool,
|
||||
cost: f64,
|
||||
) -> EndpointInfo {
|
||||
EndpointInfo {
|
||||
provider_name: name.to_string(),
|
||||
tag: None,
|
||||
pricing: ModelPricing {
|
||||
prompt: Some(format!("{:.10}", cost)),
|
||||
completion: None,
|
||||
input_cache_read: if cache {
|
||||
Some("0.00000007".to_string())
|
||||
} else {
|
||||
None
|
||||
},
|
||||
input_cache_write: None,
|
||||
},
|
||||
context_length: None,
|
||||
max_completion_tokens: None,
|
||||
quantization: None,
|
||||
uptime_last_30m: Some(uptime),
|
||||
latency_last_30m: None,
|
||||
throughput_last_30m: Some(serde_json::json!({"p50": throughput})),
|
||||
supports_implicit_caching: Some(cache),
|
||||
status: Some(0),
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn rank_providers_prioritizes_cache_then_speed() {
|
||||
let endpoints = vec![
|
||||
make_endpoint("FastCache", 50.0, 99.0, true, 0.0000002),
|
||||
make_endpoint("FasterNoCache", 60.0, 99.0, false, 0.0000001),
|
||||
];
|
||||
|
||||
let ranked = rank_providers_from_endpoints(&endpoints);
|
||||
assert_eq!(ranked.first().map(|s| s.as_str()), Some("FastCache"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn endpoint_detail_string_formats_common_fields() {
|
||||
let ep = EndpointInfo {
|
||||
provider_name: "TestProvider".to_string(),
|
||||
tag: None,
|
||||
pricing: ModelPricing {
|
||||
prompt: Some("0.00000045".to_string()),
|
||||
completion: Some("0.00000225".to_string()),
|
||||
input_cache_read: Some("0.00000007".to_string()),
|
||||
input_cache_write: None,
|
||||
},
|
||||
context_length: Some(131072),
|
||||
max_completion_tokens: Some(16384),
|
||||
quantization: Some("fp8".to_string()),
|
||||
uptime_last_30m: Some(99.2),
|
||||
latency_last_30m: None,
|
||||
throughput_last_30m: Some(serde_json::json!({"p50": 14.2})),
|
||||
supports_implicit_caching: Some(true),
|
||||
status: Some(0),
|
||||
};
|
||||
|
||||
let detail = ep.detail_string();
|
||||
assert!(detail.contains("$0.45/M"));
|
||||
assert!(detail.contains("99%"));
|
||||
assert!(detail.contains("14tps"));
|
||||
assert!(detail.contains("cache"));
|
||||
assert!(detail.contains("fp8"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn normalize_model_created_timestamp_rejects_implausible_dates() {
|
||||
assert_eq!(normalize_model_created_timestamp(Some(734_658_000)), None);
|
||||
assert_eq!(normalize_model_created_timestamp(Some(1_577_836_799)), None);
|
||||
assert_eq!(
|
||||
normalize_model_created_timestamp(Some(1_735_689_600)),
|
||||
Some(1_735_689_600)
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,662 @@
|
||||
use jcode_message_types::{
|
||||
ContentBlock, Message, Role, TOOL_OUTPUT_MISSING_TEXT, sanitize_tool_id,
|
||||
};
|
||||
use serde_json::Value;
|
||||
use std::collections::{HashMap, HashSet};
|
||||
|
||||
/// Normalize a tool `parameters` JSON schema for strict OpenAI-compatible
|
||||
/// endpoints (issue #446).
|
||||
///
|
||||
/// Some backends (LM Studio being the prominent example) validate
|
||||
/// `tools[].function.parameters` strictly and reject any object schema that
|
||||
/// lacks a `properties` field with HTTP 400. MCP servers commonly declare
|
||||
/// no-argument tools as a bare `{"type": "object"}`, and because the full tool
|
||||
/// array is sent on every request, one such tool makes the provider unusable.
|
||||
///
|
||||
/// This recursively inserts an empty `properties: {}` into every
|
||||
/// object-typed schema node that is missing it. The rewrite is semantically a
|
||||
/// no-op per JSON Schema, so it is safe to apply for every OpenAI-compatible
|
||||
/// endpoint rather than allow-listing strict ones.
|
||||
pub fn sanitize_tool_parameters_schema(schema: &Value) -> Value {
|
||||
fn walk(node: &mut Value) {
|
||||
let Some(obj) = node.as_object_mut() else {
|
||||
if let Some(items) = node.as_array_mut() {
|
||||
for item in items {
|
||||
walk(item);
|
||||
}
|
||||
}
|
||||
return;
|
||||
};
|
||||
|
||||
let is_object_type = match obj.get("type") {
|
||||
Some(Value::String(ty)) => ty == "object",
|
||||
Some(Value::Array(types)) => types.iter().any(|ty| ty.as_str() == Some("object")),
|
||||
_ => false,
|
||||
};
|
||||
if is_object_type {
|
||||
obj.entry("properties")
|
||||
.or_insert_with(|| Value::Object(serde_json::Map::new()));
|
||||
}
|
||||
|
||||
for (key, value) in obj.iter_mut() {
|
||||
match key.as_str() {
|
||||
// Schema maps: each value is a schema.
|
||||
"properties" | "patternProperties" | "$defs" | "definitions" => {
|
||||
if let Some(map) = value.as_object_mut() {
|
||||
for sub in map.values_mut() {
|
||||
walk(sub);
|
||||
}
|
||||
}
|
||||
}
|
||||
// Direct sub-schemas (or arrays of schemas).
|
||||
"items"
|
||||
| "additionalProperties"
|
||||
| "anyOf"
|
||||
| "oneOf"
|
||||
| "allOf"
|
||||
| "not"
|
||||
| "if"
|
||||
| "then"
|
||||
| "else"
|
||||
| "prefixItems"
|
||||
| "contains" => walk(value),
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// A bare `{}` / non-object parameters value is also rejected by strict
|
||||
// validators; OpenAI's spec models "no parameters" as an empty object
|
||||
// schema.
|
||||
let mut sanitized = if schema.is_object() {
|
||||
schema.clone()
|
||||
} else {
|
||||
serde_json::json!({ "type": "object" })
|
||||
};
|
||||
if let Some(obj) = sanitized.as_object_mut()
|
||||
&& obj.is_empty()
|
||||
{
|
||||
obj.insert("type".to_string(), Value::String("object".to_string()));
|
||||
}
|
||||
walk(&mut sanitized);
|
||||
sanitized
|
||||
}
|
||||
|
||||
/// Build OpenAI-compatible chat `messages` for OpenRouter/direct compatible providers.
|
||||
///
|
||||
/// This stays in the OpenRouter leaf crate so provider-specific message normalization,
|
||||
/// tool-call repair, and reasoning-content compatibility do not type-check inside
|
||||
/// `jcode-base` on every provider edit.
|
||||
pub fn build_chat_messages(
|
||||
messages: &[Message],
|
||||
system: &str,
|
||||
allow_reasoning: bool,
|
||||
include_reasoning_content: bool,
|
||||
allow_image_input: bool,
|
||||
) -> Vec<Value> {
|
||||
// Build messages in OpenAI format
|
||||
let mut api_messages = Vec::new();
|
||||
|
||||
// Add system message if provided
|
||||
if !system.is_empty() {
|
||||
api_messages.push(serde_json::json!({
|
||||
"role": "system",
|
||||
"content": system
|
||||
}));
|
||||
}
|
||||
|
||||
let content_from_parts = |parts: Vec<Value>| -> Option<Value> {
|
||||
if parts.is_empty() {
|
||||
return None;
|
||||
}
|
||||
if parts.len() == 1 {
|
||||
let part = &parts[0];
|
||||
let has_cache = part.get("cache_control").is_some();
|
||||
if !has_cache && let Some(text) = part.get("text").and_then(|v| v.as_str()) {
|
||||
return Some(serde_json::json!(text));
|
||||
}
|
||||
}
|
||||
Some(Value::Array(parts))
|
||||
};
|
||||
|
||||
let mut tool_result_last_pos: HashMap<String, usize> = HashMap::new();
|
||||
for (idx, msg) in messages.iter().enumerate() {
|
||||
if let Role::User = msg.role {
|
||||
for block in &msg.content {
|
||||
if let ContentBlock::ToolResult { tool_use_id, .. } = block {
|
||||
tool_result_last_pos.insert(tool_use_id.clone(), idx);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
let missing_output = format!("[Error] {}", TOOL_OUTPUT_MISSING_TEXT);
|
||||
let mut injected_missing = 0usize;
|
||||
let mut delayed_results = 0usize;
|
||||
let mut skipped_results = 0usize;
|
||||
let mut tool_calls_seen: HashSet<String> = HashSet::new();
|
||||
let mut pending_tool_results: HashMap<String, String> = HashMap::new();
|
||||
let mut used_tool_results: HashSet<String> = HashSet::new();
|
||||
|
||||
// Convert messages
|
||||
for (idx, msg) in messages.iter().enumerate() {
|
||||
match msg.role {
|
||||
Role::User => {
|
||||
let mut pending_user_parts: Vec<Value> = Vec::new();
|
||||
for block in &msg.content {
|
||||
match block {
|
||||
ContentBlock::Text {
|
||||
text,
|
||||
cache_control,
|
||||
} => {
|
||||
let mut part = serde_json::json!({
|
||||
"type": "text",
|
||||
"text": text
|
||||
});
|
||||
if let Some(cache_control) = cache_control {
|
||||
part["cache_control"] =
|
||||
serde_json::to_value(cache_control).unwrap_or(Value::Null);
|
||||
}
|
||||
pending_user_parts.push(part);
|
||||
}
|
||||
ContentBlock::Image { media_type, data } => {
|
||||
if allow_image_input {
|
||||
pending_user_parts.push(serde_json::json!({
|
||||
"type": "image_url",
|
||||
"image_url": {
|
||||
"url": format!("data:{};base64,{}", media_type, data)
|
||||
}
|
||||
}));
|
||||
} else {
|
||||
pending_user_parts.push(serde_json::json!({
|
||||
"type": "text",
|
||||
"text": format!(
|
||||
"[Image omitted: this provider/model does not support image input; media_type={}]",
|
||||
media_type
|
||||
)
|
||||
}));
|
||||
}
|
||||
}
|
||||
ContentBlock::ToolResult {
|
||||
tool_use_id,
|
||||
content,
|
||||
is_error,
|
||||
} => {
|
||||
if let Some(content) =
|
||||
content_from_parts(std::mem::take(&mut pending_user_parts))
|
||||
{
|
||||
api_messages.push(serde_json::json!({
|
||||
"role": "user",
|
||||
"content": content
|
||||
}));
|
||||
}
|
||||
|
||||
if used_tool_results.contains(tool_use_id) {
|
||||
skipped_results += 1;
|
||||
continue;
|
||||
}
|
||||
let output = if is_error == &Some(true) {
|
||||
format!("[Error] {}", content)
|
||||
} else {
|
||||
content.clone()
|
||||
};
|
||||
if tool_calls_seen.contains(tool_use_id) {
|
||||
api_messages.push(serde_json::json!({
|
||||
"role": "tool",
|
||||
"tool_call_id": sanitize_tool_id(tool_use_id),
|
||||
"content": output
|
||||
}));
|
||||
used_tool_results.insert(tool_use_id.clone());
|
||||
} else if pending_tool_results.contains_key(tool_use_id) {
|
||||
skipped_results += 1;
|
||||
} else {
|
||||
pending_tool_results.insert(tool_use_id.clone(), output);
|
||||
delayed_results += 1;
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
|
||||
if let Some(content) = content_from_parts(std::mem::take(&mut pending_user_parts)) {
|
||||
api_messages.push(serde_json::json!({
|
||||
"role": "user",
|
||||
"content": content
|
||||
}));
|
||||
}
|
||||
}
|
||||
Role::Assistant => {
|
||||
let mut text_content = String::new();
|
||||
let mut reasoning_content = String::new();
|
||||
let mut tool_calls = Vec::new();
|
||||
let mut post_tool_outputs: Vec<(String, String)> = Vec::new();
|
||||
let mut missing_tool_outputs: Vec<String> = Vec::new();
|
||||
|
||||
for block in &msg.content {
|
||||
match block {
|
||||
ContentBlock::Text { text, .. } => {
|
||||
text_content.push_str(text);
|
||||
}
|
||||
ContentBlock::Reasoning { text } => {
|
||||
reasoning_content.push_str(text);
|
||||
}
|
||||
ContentBlock::ToolUse {
|
||||
id, name, input, ..
|
||||
} => {
|
||||
let args = if input.is_object() {
|
||||
serde_json::to_string(input).unwrap_or_default()
|
||||
} else {
|
||||
"{}".to_string()
|
||||
};
|
||||
tool_calls.push(serde_json::json!({
|
||||
"id": sanitize_tool_id(id),
|
||||
"type": "function",
|
||||
"function": {
|
||||
"name": name,
|
||||
"arguments": args
|
||||
}
|
||||
}));
|
||||
tool_calls_seen.insert(id.clone());
|
||||
if let Some(output) = pending_tool_results.remove(id) {
|
||||
post_tool_outputs.push((id.clone(), output));
|
||||
used_tool_results.insert(id.clone());
|
||||
} else {
|
||||
let has_future_output = tool_result_last_pos
|
||||
.get(id)
|
||||
.map(|pos| *pos > idx)
|
||||
.unwrap_or(false);
|
||||
if !has_future_output {
|
||||
missing_tool_outputs.push(id.clone());
|
||||
used_tool_results.insert(id.clone());
|
||||
}
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
|
||||
let mut assistant_msg = serde_json::json!({
|
||||
"role": "assistant",
|
||||
});
|
||||
|
||||
if !text_content.is_empty() {
|
||||
assistant_msg["content"] = serde_json::json!(text_content);
|
||||
}
|
||||
|
||||
if !tool_calls.is_empty() {
|
||||
assistant_msg["tool_calls"] = serde_json::json!(tool_calls);
|
||||
}
|
||||
|
||||
let has_reasoning_content = !reasoning_content.is_empty();
|
||||
if allow_reasoning
|
||||
&& (include_reasoning_content || has_reasoning_content)
|
||||
&& (has_reasoning_content || !tool_calls.is_empty())
|
||||
{
|
||||
let reasoning_payload = if has_reasoning_content {
|
||||
reasoning_content.clone()
|
||||
} else {
|
||||
" ".to_string()
|
||||
};
|
||||
assistant_msg["reasoning_content"] = serde_json::json!(reasoning_payload);
|
||||
}
|
||||
|
||||
let has_text_content = !text_content.is_empty();
|
||||
let has_tool_calls = !tool_calls.is_empty();
|
||||
|
||||
// OpenAI-compatible providers require every assistant
|
||||
// message to carry `content` or `tool_calls`. An
|
||||
// interrupted turn can persist only a reasoning block; if
|
||||
// the provider does not accept a standalone
|
||||
// `reasoning_content` field (so it was not set above), this
|
||||
// would serialize to a bare `{"role":"assistant"}` and make
|
||||
// providers like DeepSeek reject the entire request with
|
||||
// 400 "Invalid assistant message: content or tool_calls
|
||||
// must be set", permanently wedging the session (issue
|
||||
// #321). Guarantee validity: when there is no text/tool
|
||||
// payload, only keep the turn if a provider-accepted
|
||||
// `reasoning_content` field is present, and in that case add
|
||||
// an explicit empty `content` so strict validators still
|
||||
// accept it. Otherwise drop the empty interrupted-thinking
|
||||
// artifact entirely (no tool outputs are possible without
|
||||
// tool calls).
|
||||
let keep_assistant_message = if has_text_content || has_tool_calls {
|
||||
true
|
||||
} else if assistant_msg.get("reasoning_content").is_some() {
|
||||
assistant_msg["content"] = serde_json::json!("");
|
||||
true
|
||||
} else {
|
||||
false
|
||||
};
|
||||
|
||||
if keep_assistant_message {
|
||||
api_messages.push(assistant_msg);
|
||||
|
||||
for (tool_call_id, output) in post_tool_outputs {
|
||||
api_messages.push(serde_json::json!({
|
||||
"role": "tool",
|
||||
"tool_call_id": sanitize_tool_id(&tool_call_id),
|
||||
"content": output
|
||||
}));
|
||||
}
|
||||
|
||||
if !missing_tool_outputs.is_empty() {
|
||||
injected_missing += missing_tool_outputs.len();
|
||||
for missing_id in missing_tool_outputs {
|
||||
api_messages.push(serde_json::json!({
|
||||
"role": "tool",
|
||||
"tool_call_id": sanitize_tool_id(&missing_id),
|
||||
"content": missing_output.clone()
|
||||
}));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if delayed_results > 0 {
|
||||
jcode_logging::info(&format!(
|
||||
"[openrouter] Delayed {} tool output(s) to preserve call ordering",
|
||||
delayed_results
|
||||
));
|
||||
}
|
||||
|
||||
if !pending_tool_results.is_empty() {
|
||||
skipped_results += pending_tool_results.len();
|
||||
}
|
||||
|
||||
if injected_missing > 0 {
|
||||
jcode_logging::info(&format!(
|
||||
"[openrouter] Injected {} synthetic tool output(s) to prevent API error",
|
||||
injected_missing
|
||||
));
|
||||
}
|
||||
if skipped_results > 0 {
|
||||
jcode_logging::info(&format!(
|
||||
"[openrouter] Filtered {} orphaned tool result(s) to prevent API error",
|
||||
skipped_results
|
||||
));
|
||||
}
|
||||
|
||||
// Safety pass: ensure tool-call messages include reasoning_content (when allowed)
|
||||
// and that every tool call has a matching tool output after it.
|
||||
let mut outputs_after: HashSet<String> = HashSet::new();
|
||||
let mut missing_by_index: Vec<Vec<String>> = vec![Vec::new(); api_messages.len()];
|
||||
|
||||
for (idx, msg) in api_messages.iter().enumerate().rev() {
|
||||
let role = msg.get("role").and_then(|v| v.as_str()).unwrap_or("");
|
||||
if role == "tool" {
|
||||
if let Some(id) = msg.get("tool_call_id").and_then(|v| v.as_str()) {
|
||||
outputs_after.insert(id.to_string());
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
if role == "assistant"
|
||||
&& let Some(tool_calls) = msg.get("tool_calls").and_then(|v| v.as_array())
|
||||
{
|
||||
for call in tool_calls {
|
||||
if let Some(id) = call.get("id").and_then(|v| v.as_str())
|
||||
&& !outputs_after.contains(id)
|
||||
{
|
||||
outputs_after.insert(id.to_string());
|
||||
missing_by_index[idx].push(id.to_string());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
let mut normalized = Vec::with_capacity(api_messages.len());
|
||||
let mut extra_outputs = 0usize;
|
||||
let mut missing_reasoning = 0usize;
|
||||
|
||||
for (idx, mut msg) in api_messages.into_iter().enumerate() {
|
||||
let role = msg.get("role").and_then(|v| v.as_str()).unwrap_or("");
|
||||
if role == "assistant"
|
||||
&& allow_reasoning
|
||||
&& msg.get("tool_calls").and_then(|v| v.as_array()).is_some()
|
||||
{
|
||||
let needs_reasoning = match msg.get("reasoning_content") {
|
||||
Some(value) => value.as_str().map(|s| s.trim().is_empty()).unwrap_or(true),
|
||||
None => true,
|
||||
};
|
||||
if needs_reasoning {
|
||||
msg["reasoning_content"] = serde_json::json!(" ");
|
||||
missing_reasoning += 1;
|
||||
}
|
||||
}
|
||||
|
||||
normalized.push(msg);
|
||||
|
||||
if let Some(missing) = missing_by_index.get(idx) {
|
||||
for id in missing {
|
||||
extra_outputs += 1;
|
||||
normalized.push(serde_json::json!({
|
||||
"role": "tool",
|
||||
"tool_call_id": id,
|
||||
"content": missing_output.clone()
|
||||
}));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
api_messages = normalized;
|
||||
|
||||
if missing_reasoning > 0 {
|
||||
jcode_logging::info(&format!(
|
||||
"[openrouter] Filled reasoning_content on {} tool-call message(s)",
|
||||
missing_reasoning
|
||||
));
|
||||
}
|
||||
if extra_outputs > 0 {
|
||||
jcode_logging::info(&format!(
|
||||
"[openrouter] Safety-injected {} missing tool output(s) at request build",
|
||||
extra_outputs
|
||||
));
|
||||
}
|
||||
|
||||
// Final safety pass: ensure every tool_call_id has at least one tool response after it.
|
||||
let mut tool_output_positions: HashMap<String, usize> = HashMap::new();
|
||||
for (idx, msg) in api_messages.iter().enumerate() {
|
||||
if msg.get("role").and_then(|v| v.as_str()) == Some("tool")
|
||||
&& let Some(id) = msg.get("tool_call_id").and_then(|v| v.as_str())
|
||||
{
|
||||
tool_output_positions.entry(id.to_string()).or_insert(idx);
|
||||
}
|
||||
}
|
||||
|
||||
let mut missing_after: HashSet<String> = HashSet::new();
|
||||
for (idx, msg) in api_messages.iter().enumerate() {
|
||||
if msg.get("role").and_then(|v| v.as_str()) != Some("assistant") {
|
||||
continue;
|
||||
}
|
||||
if let Some(tool_calls) = msg.get("tool_calls").and_then(|v| v.as_array()) {
|
||||
for call in tool_calls {
|
||||
if let Some(id) = call.get("id").and_then(|v| v.as_str()) {
|
||||
let has_after = tool_output_positions
|
||||
.get(id)
|
||||
.map(|pos| *pos > idx)
|
||||
.unwrap_or(false);
|
||||
if !has_after {
|
||||
missing_after.insert(id.to_string());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if !missing_after.is_empty() {
|
||||
for id in missing_after.iter() {
|
||||
api_messages.push(serde_json::json!({
|
||||
"role": "tool",
|
||||
"tool_call_id": id,
|
||||
"content": missing_output.clone()
|
||||
}));
|
||||
}
|
||||
jcode_logging::info(&format!(
|
||||
"[openrouter] Appended {} tool output(s) to satisfy call ordering",
|
||||
missing_after.len()
|
||||
));
|
||||
}
|
||||
|
||||
// Final pass: ensure tool outputs immediately follow assistant tool calls.
|
||||
let mut tool_output_map: HashMap<String, Value> = HashMap::new();
|
||||
for msg in &api_messages {
|
||||
if msg.get("role").and_then(|v| v.as_str()) == Some("tool")
|
||||
&& let Some(id) = msg.get("tool_call_id").and_then(|v| v.as_str())
|
||||
{
|
||||
let is_missing = msg
|
||||
.get("content")
|
||||
.and_then(|v| v.as_str())
|
||||
.map(|v| v == missing_output)
|
||||
.unwrap_or(false);
|
||||
match tool_output_map.get(id) {
|
||||
Some(existing) => {
|
||||
let existing_missing = existing
|
||||
.get("content")
|
||||
.and_then(|v| v.as_str())
|
||||
.map(|v| v == missing_output)
|
||||
.unwrap_or(false);
|
||||
if existing_missing && !is_missing {
|
||||
tool_output_map.insert(id.to_string(), msg.clone());
|
||||
}
|
||||
}
|
||||
None => {
|
||||
tool_output_map.insert(id.to_string(), msg.clone());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
let mut reordered: Vec<Value> = Vec::with_capacity(api_messages.len());
|
||||
let mut used_outputs: HashSet<String> = HashSet::new();
|
||||
let mut injected_ordered = 0usize;
|
||||
let mut dropped_orphans = 0usize;
|
||||
|
||||
for msg in api_messages.into_iter() {
|
||||
let role = msg.get("role").and_then(|v| v.as_str()).unwrap_or("");
|
||||
if role == "assistant" {
|
||||
let tool_calls = msg.get("tool_calls").and_then(|v| v.as_array()).cloned();
|
||||
if let Some(tool_calls) = tool_calls {
|
||||
if tool_calls.is_empty() {
|
||||
reordered.push(msg);
|
||||
continue;
|
||||
}
|
||||
reordered.push(msg);
|
||||
for call in tool_calls {
|
||||
if let Some(id) = call.get("id").and_then(|v| v.as_str()) {
|
||||
if let Some(tool_msg) = tool_output_map.get(id) {
|
||||
reordered.push(tool_msg.clone());
|
||||
used_outputs.insert(id.to_string());
|
||||
} else {
|
||||
injected_ordered += 1;
|
||||
reordered.push(serde_json::json!({
|
||||
"role": "tool",
|
||||
"tool_call_id": id,
|
||||
"content": missing_output.clone()
|
||||
}));
|
||||
used_outputs.insert(id.to_string());
|
||||
}
|
||||
}
|
||||
}
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
if role == "tool" {
|
||||
if let Some(id) = msg.get("tool_call_id").and_then(|v| v.as_str())
|
||||
&& used_outputs.contains(id)
|
||||
{
|
||||
dropped_orphans += 1;
|
||||
continue;
|
||||
}
|
||||
dropped_orphans += 1;
|
||||
continue;
|
||||
}
|
||||
|
||||
reordered.push(msg);
|
||||
}
|
||||
|
||||
api_messages = reordered;
|
||||
|
||||
if injected_ordered > 0 {
|
||||
jcode_logging::info(&format!(
|
||||
"[openrouter] Inserted {} tool output(s) to enforce call ordering",
|
||||
injected_ordered
|
||||
));
|
||||
}
|
||||
if dropped_orphans > 0 {
|
||||
jcode_logging::info(&format!(
|
||||
"[openrouter] Dropped {} orphaned tool output(s) during re-ordering",
|
||||
dropped_orphans
|
||||
));
|
||||
}
|
||||
|
||||
api_messages
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod sanitize_schema_tests {
|
||||
use super::sanitize_tool_parameters_schema;
|
||||
use serde_json::json;
|
||||
|
||||
#[test]
|
||||
fn bare_object_schema_gains_empty_properties() {
|
||||
// The no-argument MCP tool shape from issue #446.
|
||||
let sanitized = sanitize_tool_parameters_schema(&json!({"type": "object"}));
|
||||
assert_eq!(sanitized, json!({"type": "object", "properties": {}}));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn empty_and_non_object_schemas_become_empty_object_schema() {
|
||||
let expected = json!({"type": "object", "properties": {}});
|
||||
assert_eq!(sanitize_tool_parameters_schema(&json!({})), expected);
|
||||
assert_eq!(sanitize_tool_parameters_schema(&json!(null)), expected);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn existing_properties_and_unrelated_fields_are_preserved() {
|
||||
let schema = json!({
|
||||
"type": "object",
|
||||
"properties": {"path": {"type": "string", "description": "a path"}},
|
||||
"required": ["path"],
|
||||
"additionalProperties": false
|
||||
});
|
||||
assert_eq!(sanitize_tool_parameters_schema(&schema), schema);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn nested_object_schemas_are_sanitized_recursively() {
|
||||
let schema = json!({
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"config": {"type": "object"},
|
||||
"items": {"type": "array", "items": {"type": "object"}},
|
||||
"choice": {"anyOf": [{"type": "object"}, {"type": "string"}]}
|
||||
}
|
||||
});
|
||||
let sanitized = sanitize_tool_parameters_schema(&schema);
|
||||
assert_eq!(
|
||||
sanitized["properties"]["config"],
|
||||
json!({"type": "object", "properties": {}})
|
||||
);
|
||||
assert_eq!(
|
||||
sanitized["properties"]["items"]["items"],
|
||||
json!({"type": "object", "properties": {}})
|
||||
);
|
||||
assert_eq!(
|
||||
sanitized["properties"]["choice"]["anyOf"][0],
|
||||
json!({"type": "object", "properties": {}})
|
||||
);
|
||||
assert_eq!(
|
||||
sanitized["properties"]["choice"]["anyOf"][1],
|
||||
json!({"type": "string"})
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn type_arrays_including_object_are_sanitized() {
|
||||
let sanitized = sanitize_tool_parameters_schema(&json!({"type": ["object", "null"]}));
|
||||
assert_eq!(sanitized["properties"], json!({}));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,577 @@
|
||||
use anyhow::Result;
|
||||
use bytes::Bytes;
|
||||
use futures::Stream;
|
||||
use jcode_message_types::StreamEvent;
|
||||
use serde_json::Value;
|
||||
use std::collections::VecDeque;
|
||||
use std::pin::Pin;
|
||||
use std::sync::{Arc, Mutex};
|
||||
use std::task::{Context as TaskContext, Poll};
|
||||
use std::time::Instant;
|
||||
|
||||
use crate::{PinSource, ProviderPin};
|
||||
|
||||
fn truncated_stream_payload_context(data: &str) -> String {
|
||||
jcode_core::util::truncate_str(&data.trim().replace('\n', "\\n"), 240).to_string()
|
||||
}
|
||||
|
||||
pub struct OpenRouterStream {
|
||||
inner: Pin<Box<dyn Stream<Item = Result<Bytes, reqwest::Error>> + Send>>,
|
||||
buffer: String,
|
||||
pending: VecDeque<StreamEvent>,
|
||||
tool_call_accumulators: std::collections::BTreeMap<u64, ToolCallAccumulator>,
|
||||
/// Track if we've emitted the provider info (only emit once)
|
||||
provider_emitted: bool,
|
||||
model: String,
|
||||
provider_pin: Arc<Mutex<Option<ProviderPin>>>,
|
||||
reasoning_buffer: String,
|
||||
finish_reason: Option<String>,
|
||||
message_end_emitted: bool,
|
||||
}
|
||||
|
||||
#[derive(Default)]
|
||||
struct ToolCallAccumulator {
|
||||
id: String,
|
||||
name: String,
|
||||
arguments: String,
|
||||
}
|
||||
|
||||
impl OpenRouterStream {
|
||||
pub fn new(
|
||||
stream: impl Stream<Item = Result<Bytes, reqwest::Error>> + Send + 'static,
|
||||
model: String,
|
||||
provider_pin: Arc<Mutex<Option<ProviderPin>>>,
|
||||
) -> Self {
|
||||
Self {
|
||||
inner: Box::pin(stream),
|
||||
buffer: String::new(),
|
||||
pending: VecDeque::new(),
|
||||
tool_call_accumulators: std::collections::BTreeMap::new(),
|
||||
provider_emitted: false,
|
||||
model,
|
||||
provider_pin,
|
||||
reasoning_buffer: String::new(),
|
||||
finish_reason: None,
|
||||
message_end_emitted: false,
|
||||
}
|
||||
}
|
||||
|
||||
fn queue_message_end(&mut self) {
|
||||
if self.message_end_emitted {
|
||||
return;
|
||||
}
|
||||
|
||||
self.flush_tool_call_accumulators();
|
||||
self.message_end_emitted = true;
|
||||
self.pending.push_back(StreamEvent::MessageEnd {
|
||||
stop_reason: self.finish_reason.take(),
|
||||
});
|
||||
}
|
||||
|
||||
fn observe_provider(&mut self, provider: &str) {
|
||||
let mut pin = self
|
||||
.provider_pin
|
||||
.lock()
|
||||
.unwrap_or_else(|poisoned| poisoned.into_inner());
|
||||
if let Some(existing) = pin.as_ref() {
|
||||
if existing.source == PinSource::Explicit && existing.model == self.model {
|
||||
return;
|
||||
}
|
||||
if existing.source == PinSource::Observed
|
||||
&& existing.model == self.model
|
||||
&& existing.provider == provider
|
||||
{
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
*pin = Some(ProviderPin {
|
||||
model: self.model.clone(),
|
||||
provider: provider.to_string(),
|
||||
source: PinSource::Observed,
|
||||
allow_fallbacks: true,
|
||||
last_cache_read: None,
|
||||
});
|
||||
}
|
||||
|
||||
fn refresh_cache_pin(&mut self, provider: &str) {
|
||||
let mut pin = self
|
||||
.provider_pin
|
||||
.lock()
|
||||
.unwrap_or_else(|poisoned| poisoned.into_inner());
|
||||
if let Some(existing) = pin.as_mut()
|
||||
&& existing.model == self.model
|
||||
&& existing.provider == provider
|
||||
{
|
||||
existing.last_cache_read = Some(Instant::now());
|
||||
}
|
||||
}
|
||||
|
||||
fn push_completed_tool_call(&mut self, tc: ToolCallAccumulator) {
|
||||
if tc.id.trim().is_empty() {
|
||||
jcode_logging::warn(&format!(
|
||||
"OpenRouter SSE dropped incomplete tool call for model {}: missing id (name={} args_len={})",
|
||||
self.model,
|
||||
tc.name,
|
||||
tc.arguments.len()
|
||||
));
|
||||
return;
|
||||
}
|
||||
|
||||
if tc.name.trim().is_empty() {
|
||||
jcode_logging::warn(&format!(
|
||||
"OpenRouter SSE dropped incomplete tool call for model {}: missing name (id={} args_len={})",
|
||||
self.model,
|
||||
tc.id,
|
||||
tc.arguments.len()
|
||||
));
|
||||
return;
|
||||
}
|
||||
|
||||
self.pending.push_back(StreamEvent::ToolUseStart {
|
||||
id: tc.id,
|
||||
name: tc.name,
|
||||
});
|
||||
self.pending
|
||||
.push_back(StreamEvent::ToolInputDelta(tc.arguments));
|
||||
self.pending.push_back(StreamEvent::ToolUseEnd);
|
||||
}
|
||||
|
||||
fn flush_tool_call_accumulators(&mut self) {
|
||||
let calls = std::mem::take(&mut self.tool_call_accumulators);
|
||||
for (_index, tc) in calls {
|
||||
self.push_completed_tool_call(tc);
|
||||
}
|
||||
}
|
||||
|
||||
fn apply_tool_call_delta(
|
||||
&mut self,
|
||||
index: u64,
|
||||
id: Option<&str>,
|
||||
name: Option<&str>,
|
||||
arguments: Option<&str>,
|
||||
) {
|
||||
let incoming_id = id
|
||||
.map(str::trim)
|
||||
.filter(|value| !value.is_empty())
|
||||
.map(str::to_string);
|
||||
|
||||
if self
|
||||
.tool_call_accumulators
|
||||
.get(&index)
|
||||
.is_some_and(|existing| {
|
||||
incoming_id.as_ref().is_some_and(|incoming_id| {
|
||||
!existing.id.is_empty() && existing.id != *incoming_id
|
||||
})
|
||||
})
|
||||
&& let Some(previous) = self.tool_call_accumulators.remove(&index)
|
||||
{
|
||||
self.push_completed_tool_call(previous);
|
||||
}
|
||||
|
||||
let tc = self.tool_call_accumulators.entry(index).or_default();
|
||||
|
||||
if tc.id.is_empty()
|
||||
&& let Some(incoming_id) = incoming_id
|
||||
{
|
||||
tc.id = incoming_id;
|
||||
}
|
||||
|
||||
if tc.name.trim().is_empty()
|
||||
&& let Some(incoming_name) = name.map(str::trim).filter(|value| !value.is_empty())
|
||||
{
|
||||
tc.name = incoming_name.to_string();
|
||||
}
|
||||
|
||||
if let Some(args) = arguments {
|
||||
tc.arguments.push_str(args);
|
||||
}
|
||||
}
|
||||
|
||||
fn parse_next_event(&mut self) -> Option<StreamEvent> {
|
||||
if let Some(event) = self.pending.pop_front() {
|
||||
return Some(event);
|
||||
}
|
||||
|
||||
while let Some(pos) = self.buffer.find("\n\n") {
|
||||
// Extract this event and remove it (plus the "\n\n" separator) in
|
||||
// place. Reassigning `self.buffer = self.buffer[pos + 2..].to_string()`
|
||||
// copied and reallocated the entire remaining buffer on every event,
|
||||
// which is O(buffer^2) when one network chunk batches many SSE
|
||||
// events. `drain` removes the consumed prefix without reallocating.
|
||||
let event_str = self.buffer[..pos].to_string();
|
||||
self.buffer.drain(..pos + 2);
|
||||
|
||||
// Parse SSE event
|
||||
let mut data = None;
|
||||
for line in event_str.lines() {
|
||||
if let Some(d) = jcode_core::util::sse_data_line(line) {
|
||||
data = Some(d);
|
||||
}
|
||||
}
|
||||
|
||||
let data = match data {
|
||||
Some(d) => d,
|
||||
None => continue,
|
||||
};
|
||||
|
||||
if data == "[DONE]" {
|
||||
self.queue_message_end();
|
||||
return self.pending.pop_front();
|
||||
}
|
||||
|
||||
let parsed: Value = match serde_json::from_str(data) {
|
||||
Ok(v) => v,
|
||||
Err(error) => {
|
||||
jcode_logging::warn(&format!(
|
||||
"OpenRouter SSE JSON parse failed for model {}: {} payload={} ",
|
||||
self.model,
|
||||
error,
|
||||
truncated_stream_payload_context(data)
|
||||
));
|
||||
continue;
|
||||
}
|
||||
};
|
||||
|
||||
// Extract upstream provider info (only emit once)
|
||||
// OpenRouter returns "provider" field indicating which provider handled the request
|
||||
if !self.provider_emitted
|
||||
&& let Some(provider) = parsed.get("provider").and_then(|p| p.as_str())
|
||||
{
|
||||
self.provider_emitted = true;
|
||||
self.observe_provider(provider);
|
||||
self.pending.push_back(StreamEvent::UpstreamProvider {
|
||||
provider: provider.to_string(),
|
||||
});
|
||||
}
|
||||
|
||||
// Check for error
|
||||
if let Some(error) = parsed.get("error") {
|
||||
let message = error
|
||||
.get("message")
|
||||
.and_then(|v| v.as_str())
|
||||
.unwrap_or("OpenRouter error")
|
||||
.to_string();
|
||||
return Some(StreamEvent::Error {
|
||||
message,
|
||||
retry_after_secs: None,
|
||||
});
|
||||
}
|
||||
|
||||
// Parse choices
|
||||
if let Some(choices) = parsed.get("choices").and_then(|c| c.as_array()) {
|
||||
for choice in choices {
|
||||
if let Some(delta) = choice.get("delta").or_else(|| choice.get("message")) {
|
||||
if let Some(reasoning_content) = delta
|
||||
.get("reasoning_content")
|
||||
.or_else(|| delta.get("reasoning"))
|
||||
.and_then(|c| c.as_str())
|
||||
&& !reasoning_content.is_empty()
|
||||
{
|
||||
let reasoning_delta =
|
||||
if reasoning_content.starts_with(&self.reasoning_buffer) {
|
||||
&reasoning_content[self.reasoning_buffer.len()..]
|
||||
} else {
|
||||
reasoning_content
|
||||
};
|
||||
self.reasoning_buffer = reasoning_content.to_string();
|
||||
if !reasoning_delta.is_empty() {
|
||||
self.pending.push_back(StreamEvent::ThinkingDelta(
|
||||
reasoning_delta.to_string(),
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
// Text content
|
||||
if let Some(content) = delta.get("content").and_then(|c| c.as_str())
|
||||
&& !content.is_empty()
|
||||
{
|
||||
self.pending
|
||||
.push_back(StreamEvent::TextDelta(content.to_string()));
|
||||
}
|
||||
|
||||
// Tool calls
|
||||
if let Some(tool_calls) = delta.get("tool_calls").and_then(|t| t.as_array())
|
||||
{
|
||||
for tc in tool_calls {
|
||||
let index = tc.get("index").and_then(|i| i.as_u64()).unwrap_or(0);
|
||||
let function = tc.get("function");
|
||||
self.apply_tool_call_delta(
|
||||
index,
|
||||
tc.get("id").and_then(|i| i.as_str()),
|
||||
function
|
||||
.and_then(|f| f.get("name"))
|
||||
.and_then(|n| n.as_str()),
|
||||
function
|
||||
.and_then(|f| f.get("arguments"))
|
||||
.and_then(|a| a.as_str()),
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Check for finish reason
|
||||
if let Some(finish_reason) =
|
||||
choice.get("finish_reason").and_then(|f| f.as_str())
|
||||
{
|
||||
let finish_reason = finish_reason.trim();
|
||||
if !finish_reason.is_empty() {
|
||||
self.finish_reason = Some(finish_reason.to_string());
|
||||
}
|
||||
// Emit any pending tool calls.
|
||||
self.flush_tool_call_accumulators();
|
||||
|
||||
// Don't emit MessageEnd here - wait for [DONE]
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Extract usage if present
|
||||
if let Some(usage) = parsed.get("usage") {
|
||||
let input_tokens = usage.get("prompt_tokens").and_then(|t| t.as_u64());
|
||||
let output_tokens = usage.get("completion_tokens").and_then(|t| t.as_u64());
|
||||
|
||||
// OpenRouter returns cached tokens in various formats depending on provider:
|
||||
// - "cached_tokens" (OpenRouter's unified field)
|
||||
// - "prompt_tokens_details.cached_tokens" (OpenAI-style)
|
||||
// - "cache_read_input_tokens" (Anthropic-style, passed through)
|
||||
let cache_read_input_tokens = usage
|
||||
.get("cached_tokens")
|
||||
.and_then(|t| t.as_u64())
|
||||
.or_else(|| {
|
||||
usage
|
||||
.get("prompt_tokens_details")
|
||||
.and_then(|d| d.get("cached_tokens"))
|
||||
.and_then(|t| t.as_u64())
|
||||
})
|
||||
.or_else(|| {
|
||||
usage
|
||||
.get("cache_read_input_tokens")
|
||||
.and_then(|t| t.as_u64())
|
||||
});
|
||||
|
||||
// Cache creation tokens (Anthropic-style, passed through for some providers)
|
||||
let cache_creation_input_tokens = usage
|
||||
.get("cache_creation_input_tokens")
|
||||
.and_then(|t| t.as_u64());
|
||||
|
||||
// Refresh cache pin when we see cache activity
|
||||
if (cache_read_input_tokens.is_some() || cache_creation_input_tokens.is_some())
|
||||
&& let Some(provider) = parsed.get("provider").and_then(|p| p.as_str())
|
||||
{
|
||||
self.refresh_cache_pin(provider);
|
||||
}
|
||||
|
||||
if input_tokens.is_some()
|
||||
|| output_tokens.is_some()
|
||||
|| cache_read_input_tokens.is_some()
|
||||
{
|
||||
self.pending.push_back(StreamEvent::TokenUsage {
|
||||
input_tokens,
|
||||
output_tokens,
|
||||
cache_read_input_tokens,
|
||||
cache_creation_input_tokens,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
if let Some(event) = self.pending.pop_front() {
|
||||
return Some(event);
|
||||
}
|
||||
}
|
||||
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
impl Stream for OpenRouterStream {
|
||||
type Item = Result<StreamEvent>;
|
||||
|
||||
fn poll_next(mut self: Pin<&mut Self>, cx: &mut TaskContext<'_>) -> Poll<Option<Self::Item>> {
|
||||
loop {
|
||||
if let Some(event) = self.parse_next_event() {
|
||||
return Poll::Ready(Some(Ok(event)));
|
||||
}
|
||||
|
||||
match self.inner.as_mut().poll_next(cx) {
|
||||
Poll::Ready(Some(Ok(bytes))) => {
|
||||
if let Ok(text) = std::str::from_utf8(&bytes) {
|
||||
self.buffer.push_str(text);
|
||||
}
|
||||
}
|
||||
Poll::Ready(Some(Err(e))) => {
|
||||
return Poll::Ready(Some(Err(anyhow::anyhow!("Stream error: {}", e))));
|
||||
}
|
||||
Poll::Ready(None) => {
|
||||
// Stream ended - emit any pending tool call
|
||||
self.flush_tool_call_accumulators();
|
||||
if let Some(event) = self.pending.pop_front() {
|
||||
return Poll::Ready(Some(Ok(event)));
|
||||
}
|
||||
if !self.message_end_emitted {
|
||||
self.message_end_emitted = true;
|
||||
return Poll::Ready(Some(Ok(StreamEvent::MessageEnd {
|
||||
stop_reason: self.finish_reason.take(),
|
||||
})));
|
||||
}
|
||||
return Poll::Ready(None);
|
||||
}
|
||||
Poll::Pending => {
|
||||
return Poll::Pending;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use futures::StreamExt;
|
||||
|
||||
#[test]
|
||||
fn parse_next_event_ignores_malformed_json_chunks() {
|
||||
let provider_pin = Arc::new(std::sync::Mutex::new(None));
|
||||
let mut stream = OpenRouterStream::new(
|
||||
futures::stream::empty(),
|
||||
"test-model".to_string(),
|
||||
provider_pin,
|
||||
);
|
||||
stream.buffer = "data: {not-json}
|
||||
|
||||
"
|
||||
.to_string();
|
||||
|
||||
let event = stream.parse_next_event();
|
||||
|
||||
assert!(event.is_none());
|
||||
assert!(stream.pending.is_empty());
|
||||
assert!(stream.tool_call_accumulators.is_empty());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parse_next_event_accepts_reasoning_delta_alias() {
|
||||
let provider_pin = Arc::new(std::sync::Mutex::new(None));
|
||||
let mut stream = OpenRouterStream::new(
|
||||
futures::stream::empty(),
|
||||
"test-model".to_string(),
|
||||
provider_pin,
|
||||
);
|
||||
stream.buffer =
|
||||
"data: {\"choices\":[{\"delta\":{\"reasoning\":\"thinking\"}}]}\n\n".to_string();
|
||||
|
||||
let event = stream.parse_next_event();
|
||||
|
||||
assert!(matches!(event, Some(StreamEvent::ThinkingDelta(text)) if text == "thinking"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parse_next_event_propagates_finish_reason_to_message_end() {
|
||||
let provider_pin = Arc::new(std::sync::Mutex::new(None));
|
||||
let mut stream = OpenRouterStream::new(
|
||||
futures::stream::empty(),
|
||||
"test-model".to_string(),
|
||||
provider_pin,
|
||||
);
|
||||
stream.buffer =
|
||||
"data: {\"choices\":[{\"finish_reason\":\"length\"}]}\n\ndata: [DONE]\n\n".to_string();
|
||||
|
||||
let event = stream.parse_next_event();
|
||||
|
||||
assert!(matches!(
|
||||
event,
|
||||
Some(StreamEvent::MessageEnd { stop_reason: Some(reason) }) if reason == "length"
|
||||
));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn stream_eof_emits_message_end_with_finish_reason_without_done() {
|
||||
let provider_pin = Arc::new(std::sync::Mutex::new(None));
|
||||
let bytes = Bytes::from_static(
|
||||
b"data: {\"choices\":[{\"delta\":{},\"finish_reason\":\"max_tokens\"}]}\n\n",
|
||||
);
|
||||
let mut stream = OpenRouterStream::new(
|
||||
futures::stream::once(async move { Ok(bytes) }),
|
||||
"test-model".to_string(),
|
||||
provider_pin,
|
||||
);
|
||||
|
||||
let event = futures::executor::block_on(stream.next());
|
||||
|
||||
assert!(matches!(
|
||||
event,
|
||||
Some(Ok(StreamEvent::MessageEnd { stop_reason: Some(reason) })) if reason == "max_tokens"
|
||||
));
|
||||
assert!(futures::executor::block_on(stream.next()).is_none());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parse_next_event_coalesces_repeated_tool_call_id_chunks() {
|
||||
let provider_pin = Arc::new(std::sync::Mutex::new(None));
|
||||
let mut stream =
|
||||
OpenRouterStream::new(futures::stream::empty(), "glm-5".to_string(), provider_pin);
|
||||
|
||||
let chunk1 = serde_json::json!({
|
||||
"choices": [{
|
||||
"delta": {
|
||||
"tool_calls": [{
|
||||
"index": 0,
|
||||
"id": "call_1",
|
||||
"type": "function",
|
||||
"function": {"name": "bash", "arguments": ""}
|
||||
}]
|
||||
}
|
||||
}]
|
||||
});
|
||||
let chunk2 = serde_json::json!({
|
||||
"choices": [{
|
||||
"delta": {
|
||||
"tool_calls": [{
|
||||
"index": 0,
|
||||
"id": "call_1",
|
||||
"function": {"arguments": "{\"command\""}
|
||||
}]
|
||||
}
|
||||
}]
|
||||
});
|
||||
let chunk3 = serde_json::json!({
|
||||
"choices": [{
|
||||
"delta": {
|
||||
"tool_calls": [{
|
||||
"index": 0,
|
||||
"id": "call_1",
|
||||
"function": {"arguments": ":\"echo ok\"}"}
|
||||
}]
|
||||
},
|
||||
"finish_reason": "tool_calls"
|
||||
}]
|
||||
});
|
||||
stream.buffer =
|
||||
format!("data: {chunk1}\n\ndata: {chunk2}\n\ndata: {chunk3}\n\ndata: [DONE]\n\n");
|
||||
|
||||
let mut events = Vec::new();
|
||||
for _ in 0..8 {
|
||||
if let Some(event) = stream.parse_next_event() {
|
||||
events.push(event);
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
assert_eq!(events.len(), 4, "events: {events:?}");
|
||||
assert!(matches!(
|
||||
&events[0],
|
||||
StreamEvent::ToolUseStart { id, name } if id == "call_1" && name == "bash"
|
||||
));
|
||||
assert!(matches!(
|
||||
&events[1],
|
||||
StreamEvent::ToolInputDelta(args) if args == "{\"command\":\"echo ok\"}"
|
||||
));
|
||||
assert!(matches!(events[2], StreamEvent::ToolUseEnd));
|
||||
assert!(matches!(
|
||||
&events[3],
|
||||
StreamEvent::MessageEnd { stop_reason } if stop_reason.as_deref() == Some("tool_calls")
|
||||
));
|
||||
assert!(stream.tool_call_accumulators.is_empty());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user