feat: add official Gemini v1beta API routes
Replace /v1/gemini with proper Gemini API paths:
- POST /v1beta/models/{model}:generateContent (sync)
- POST /v1beta/models/{model}:streamGenerateContent (streaming)
Model is extracted from URL path. Uses axum wildcard
catch-all since colons in path segments are not supported.
This commit is contained in:
@@ -1,9 +1,13 @@
|
|||||||
//! Gemini-native endpoint (/v1/gemini) — zero-translation tool call passthrough.
|
//! Gemini-native endpoint — zero-translation tool call passthrough.
|
||||||
|
//!
|
||||||
|
//! Routes:
|
||||||
|
//! POST /v1/gemini (custom, stream via body flag)
|
||||||
|
//! POST /v1beta/models/{model}:generateContent (sync, official Gemini API path)
|
||||||
|
//! POST /v1beta/models/{model}:streamGenerateContent (stream, official Gemini API path)
|
||||||
//!
|
//!
|
||||||
//! Accepts tools in Gemini `functionDeclarations` format directly,
|
//! Accepts tools in Gemini `functionDeclarations` format directly,
|
||||||
//! returns `functionCall` in Gemini format directly.
|
//! returns `functionCall` in Gemini format directly.
|
||||||
//! No OpenAI ↔ Gemini format conversion.
|
//! No OpenAI ↔ Gemini format conversion.
|
||||||
//! Supports both sync (`stream: false`) and streaming (`stream: true`) modes.
|
|
||||||
|
|
||||||
use axum::{
|
use axum::{
|
||||||
extract::State,
|
extract::State,
|
||||||
@@ -12,7 +16,7 @@ use axum::{
|
|||||||
};
|
};
|
||||||
use rand::Rng;
|
use rand::Rng;
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
use tracing::{info, warn};
|
use tracing::{debug, info, warn};
|
||||||
|
|
||||||
use super::models::{lookup_model, DEFAULT_MODEL, MODELS};
|
use super::models::{lookup_model, DEFAULT_MODEL, MODELS};
|
||||||
use super::polling::{
|
use super::polling::{
|
||||||
@@ -115,9 +119,51 @@ async fn build_usage_metadata(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) async fn handle_gemini(
|
/// POST /v1beta/*path — handles both :generateContent and :streamGenerateContent
|
||||||
|
///
|
||||||
|
/// Parses paths like:
|
||||||
|
/// /v1beta/models/gemini-3-flash:generateContent
|
||||||
|
/// /v1beta/models/gemini-3-flash:streamGenerateContent
|
||||||
|
pub(crate) async fn handle_gemini_v1beta(
|
||||||
State(state): State<Arc<AppState>>,
|
State(state): State<Arc<AppState>>,
|
||||||
Json(body): Json<GeminiRequest>,
|
axum::extract::Path(path): axum::extract::Path<String>,
|
||||||
|
Json(mut body): Json<GeminiRequest>,
|
||||||
|
) -> axum::response::Response {
|
||||||
|
// Expected path: "models/{model}:{action}"
|
||||||
|
let path = path.trim_start_matches('/');
|
||||||
|
let model_action = path.strip_prefix("models/").unwrap_or(path);
|
||||||
|
|
||||||
|
// Split on the LAST colon — model name may contain hyphens/numbers but not colons
|
||||||
|
if let Some(colon_pos) = model_action.rfind(':') {
|
||||||
|
let model = &model_action[..colon_pos];
|
||||||
|
let action = &model_action[colon_pos + 1..];
|
||||||
|
|
||||||
|
body.model = Some(model.to_string());
|
||||||
|
match action {
|
||||||
|
"generateContent" => body.stream = false,
|
||||||
|
"streamGenerateContent" => body.stream = true,
|
||||||
|
_ => {
|
||||||
|
return err_response(
|
||||||
|
StatusCode::BAD_REQUEST,
|
||||||
|
format!("Unknown action: {action}. Use :generateContent or :streamGenerateContent"),
|
||||||
|
"invalid_request_error",
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
return err_response(
|
||||||
|
StatusCode::BAD_REQUEST,
|
||||||
|
format!("Invalid path: /v1beta/{path}. Expected /v1beta/models/{{model}}:generateContent"),
|
||||||
|
"invalid_request_error",
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
handle_gemini_inner(state, body).await
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn handle_gemini_inner(
|
||||||
|
state: Arc<AppState>,
|
||||||
|
body: GeminiRequest,
|
||||||
) -> axum::response::Response {
|
) -> axum::response::Response {
|
||||||
info!(
|
info!(
|
||||||
"POST /v1/gemini model={} stream={}",
|
"POST /v1/gemini model={} stream={}",
|
||||||
@@ -209,7 +255,11 @@ pub(crate) async fn handle_gemini(
|
|||||||
count = tools.len(),
|
count = tools.len(),
|
||||||
"Stored Gemini-native tools for MITM injection"
|
"Stored Gemini-native tools for MITM injection"
|
||||||
);
|
);
|
||||||
|
} else {
|
||||||
|
state.mitm_store.clear_tools().await;
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
state.mitm_store.clear_tools().await;
|
||||||
}
|
}
|
||||||
if let Some(ref config) = body.tool_config {
|
if let Some(ref config) = body.tool_config {
|
||||||
state.mitm_store.set_tool_config(config.clone()).await;
|
state.mitm_store.set_tool_config(config.clone()).await;
|
||||||
@@ -317,6 +367,8 @@ pub(crate) async fn handle_gemini(
|
|||||||
|
|
||||||
// Send message
|
// Send message
|
||||||
state.mitm_store.set_active_cascade(&cascade_id).await;
|
state.mitm_store.set_active_cascade(&cascade_id).await;
|
||||||
|
// Store real user text for MITM injection — LS gets a dummy prompt
|
||||||
|
state.mitm_store.set_pending_user_text(user_text.clone()).await;
|
||||||
// Store image for MITM injection (LS doesn't forward images to Google API)
|
// Store image for MITM injection (LS doesn't forward images to Google API)
|
||||||
if let Some(ref img) = image {
|
if let Some(ref img) = image {
|
||||||
use base64::Engine;
|
use base64::Engine;
|
||||||
@@ -328,9 +380,24 @@ pub(crate) async fn handle_gemini(
|
|||||||
})
|
})
|
||||||
.await;
|
.await;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Pre-flight: install channel BEFORE send_message so the MITM proxy
|
||||||
|
// can grab it when the LS fires its API call.
|
||||||
|
let has_custom_tools = state.mitm_store.get_tools().await.is_some();
|
||||||
|
let mitm_rx = if has_custom_tools {
|
||||||
|
state.mitm_store.clear_response_async().await;
|
||||||
|
state.mitm_store.clear_upstream_error().await;
|
||||||
|
let _ = state.mitm_store.take_any_function_calls().await;
|
||||||
|
let (tx, rx) = tokio::sync::mpsc::channel(64);
|
||||||
|
state.mitm_store.set_channel(tx).await;
|
||||||
|
Some(rx)
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
};
|
||||||
|
|
||||||
match state
|
match state
|
||||||
.backend
|
.backend
|
||||||
.send_message_with_image(&cascade_id, &user_text, model.model_enum, image.as_ref())
|
.send_message_with_image(&cascade_id, ".", model.model_enum, image.as_ref())
|
||||||
.await
|
.await
|
||||||
{
|
{
|
||||||
Ok((200, _)) => {
|
Ok((200, _)) => {
|
||||||
@@ -341,6 +408,7 @@ pub(crate) async fn handle_gemini(
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
Ok((status, _)) => {
|
Ok((status, _)) => {
|
||||||
|
state.mitm_store.drop_channel().await;
|
||||||
return err_response(
|
return err_response(
|
||||||
StatusCode::BAD_GATEWAY,
|
StatusCode::BAD_GATEWAY,
|
||||||
format!("Antigravity returned {status}"),
|
format!("Antigravity returned {status}"),
|
||||||
@@ -348,6 +416,7 @@ pub(crate) async fn handle_gemini(
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
|
state.mitm_store.drop_channel().await;
|
||||||
return err_response(
|
return err_response(
|
||||||
StatusCode::BAD_GATEWAY,
|
StatusCode::BAD_GATEWAY,
|
||||||
format!("Send message failed: {e}"),
|
format!("Send message failed: {e}"),
|
||||||
@@ -360,9 +429,9 @@ pub(crate) async fn handle_gemini(
|
|||||||
let model_name = model_name.to_string();
|
let model_name = model_name.to_string();
|
||||||
let timeout = body.timeout;
|
let timeout = body.timeout;
|
||||||
if body.stream {
|
if body.stream {
|
||||||
gemini_stream(state, model_name, cascade_id, timeout).await
|
gemini_stream(state, model_name, cascade_id, timeout, mitm_rx).await
|
||||||
} else {
|
} else {
|
||||||
gemini_sync(state, model_name, cascade_id, timeout).await
|
gemini_sync(state, model_name, cascade_id, timeout, mitm_rx).await
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -373,26 +442,31 @@ async fn gemini_sync(
|
|||||||
model_name: String,
|
model_name: String,
|
||||||
cascade_id: String,
|
cascade_id: String,
|
||||||
timeout: u64,
|
timeout: u64,
|
||||||
|
mitm_rx: Option<tokio::sync::mpsc::Receiver<crate::mitm::store::MitmEvent>>,
|
||||||
) -> axum::response::Response {
|
) -> axum::response::Response {
|
||||||
let has_custom_tools = state.mitm_store.get_tools().await.is_some();
|
// Clear stale response and upstream errors (only if no pre-installed channel)
|
||||||
|
if mitm_rx.is_none() {
|
||||||
// Clear stale response and upstream errors
|
|
||||||
state.mitm_store.clear_response_async().await;
|
state.mitm_store.clear_response_async().await;
|
||||||
state.mitm_store.clear_upstream_error().await;
|
state.mitm_store.clear_upstream_error().await;
|
||||||
|
|
||||||
// ── MITM bypass: when tools active, poll MitmStore directly ──
|
|
||||||
if has_custom_tools {
|
|
||||||
let start = std::time::Instant::now();
|
|
||||||
while start.elapsed().as_secs() < timeout {
|
|
||||||
// Check for upstream errors from MITM (Google API errors)
|
|
||||||
if let Some(err) = state.mitm_store.take_upstream_error().await {
|
|
||||||
return upstream_err_response(&err);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check for function calls
|
// ── MITM bypass: channel-based pipeline when custom tools active ──
|
||||||
let captured = state.mitm_store.take_any_function_calls().await;
|
if let Some(mut rx) = mitm_rx {
|
||||||
if let Some(ref calls) = captured {
|
let start = std::time::Instant::now();
|
||||||
if !calls.is_empty() {
|
|
||||||
|
let mut acc_text = String::new();
|
||||||
|
let mut acc_thinking: Option<String> = None;
|
||||||
|
|
||||||
|
while let Some(event) = tokio::time::timeout(
|
||||||
|
std::time::Duration::from_secs(timeout.saturating_sub(start.elapsed().as_secs())),
|
||||||
|
rx.recv(),
|
||||||
|
).await.ok().flatten() {
|
||||||
|
use crate::mitm::store::MitmEvent;
|
||||||
|
match event {
|
||||||
|
MitmEvent::ThinkingDelta(t) => { acc_thinking = Some(t); }
|
||||||
|
MitmEvent::TextDelta(t) => { acc_text = t; }
|
||||||
|
MitmEvent::Usage(_) | MitmEvent::Grounding(_) => {}
|
||||||
|
MitmEvent::FunctionCall(calls) => {
|
||||||
let parts: Vec<serde_json::Value> = calls
|
let parts: Vec<serde_json::Value> = calls
|
||||||
.iter()
|
.iter()
|
||||||
.map(|fc| {
|
.map(|fc| {
|
||||||
@@ -404,7 +478,7 @@ async fn gemini_sync(
|
|||||||
})
|
})
|
||||||
})
|
})
|
||||||
.collect();
|
.collect();
|
||||||
|
state.mitm_store.drop_channel().await;
|
||||||
return Json(serde_json::json!({
|
return Json(serde_json::json!({
|
||||||
"candidates": [{
|
"candidates": [{
|
||||||
"content": {
|
"content": {
|
||||||
@@ -417,31 +491,31 @@ async fn gemini_sync(
|
|||||||
}))
|
}))
|
||||||
.into_response();
|
.into_response();
|
||||||
}
|
}
|
||||||
}
|
MitmEvent::ResponseComplete => {
|
||||||
|
if acc_text.is_empty() && acc_thinking.is_none() {
|
||||||
// Check for completed text response
|
// Empty response — continue waiting
|
||||||
if state.mitm_store.is_response_complete() {
|
continue;
|
||||||
let text = state
|
}
|
||||||
.mitm_store
|
if acc_text.is_empty() && acc_thinking.is_some() {
|
||||||
.take_response_text()
|
// Thinking-only — LS needs to make a follow-up request.
|
||||||
.await
|
// Reinstall channel and unblock gate.
|
||||||
.unwrap_or_default();
|
let (new_tx, new_rx) = tokio::sync::mpsc::channel(64);
|
||||||
let thinking = state.mitm_store.take_thinking_text().await;
|
state.mitm_store.set_channel(new_tx).await;
|
||||||
|
state.mitm_store.clear_request_in_flight();
|
||||||
// Guard against stale response_complete with no data
|
let _ = state.mitm_store.take_any_function_calls().await;
|
||||||
if text.is_empty() && thinking.is_none() {
|
rx = new_rx;
|
||||||
warn!("Gemini sync bypass: stale response_complete — clearing");
|
debug!(
|
||||||
state.mitm_store.clear_response_async().await;
|
"Gemini sync: thinking-only — new channel for follow-up, thinking_len={}",
|
||||||
tokio::time::sleep(tokio::time::Duration::from_millis(200)).await;
|
acc_thinking.as_ref().map(|t| t.len()).unwrap_or(0)
|
||||||
|
);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut parts: Vec<serde_json::Value> = Vec::new();
|
let mut parts: Vec<serde_json::Value> = Vec::new();
|
||||||
if let Some(ref t) = thinking {
|
if let Some(ref t) = acc_thinking {
|
||||||
parts.push(serde_json::json!({"text": t, "thought": true}));
|
parts.push(serde_json::json!({"text": t, "thought": true}));
|
||||||
}
|
}
|
||||||
parts.push(serde_json::json!({"text": text}));
|
parts.push(serde_json::json!({"text": acc_text}));
|
||||||
|
state.mitm_store.drop_channel().await;
|
||||||
return Json(serde_json::json!({
|
return Json(serde_json::json!({
|
||||||
"candidates": [{
|
"candidates": [{
|
||||||
"content": {
|
"content": {
|
||||||
@@ -455,11 +529,15 @@ async fn gemini_sync(
|
|||||||
}))
|
}))
|
||||||
.into_response();
|
.into_response();
|
||||||
}
|
}
|
||||||
|
MitmEvent::UpstreamError(err) => {
|
||||||
tokio::time::sleep(tokio::time::Duration::from_millis(200)).await;
|
state.mitm_store.drop_channel().await;
|
||||||
|
return upstream_err_response(&err);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Timeout — return proper error with status code
|
// Timeout
|
||||||
|
state.mitm_store.drop_channel().await;
|
||||||
return (
|
return (
|
||||||
axum::http::StatusCode::GATEWAY_TIMEOUT,
|
axum::http::StatusCode::GATEWAY_TIMEOUT,
|
||||||
Json(serde_json::json!({
|
Json(serde_json::json!({
|
||||||
@@ -543,36 +621,63 @@ async fn gemini_stream(
|
|||||||
model_name: String,
|
model_name: String,
|
||||||
cascade_id: String,
|
cascade_id: String,
|
||||||
timeout: u64,
|
timeout: u64,
|
||||||
|
mitm_rx: Option<tokio::sync::mpsc::Receiver<crate::mitm::store::MitmEvent>>,
|
||||||
) -> axum::response::Response {
|
) -> axum::response::Response {
|
||||||
let stream = async_stream::stream! {
|
let stream = async_stream::stream! {
|
||||||
let start = std::time::Instant::now();
|
let start = std::time::Instant::now();
|
||||||
let mut last_text = String::new();
|
let mut last_text = String::new();
|
||||||
let mut last_thinking = String::new();
|
let mut last_thinking = String::new();
|
||||||
let has_custom_tools = state.mitm_store.get_tools().await.is_some();
|
|
||||||
|
|
||||||
// Clear stale response
|
// Clear stale response (only if no pre-installed channel)
|
||||||
|
if mitm_rx.is_none() {
|
||||||
state.mitm_store.clear_response_async().await;
|
state.mitm_store.clear_response_async().await;
|
||||||
|
}
|
||||||
|
|
||||||
|
// ── MITM bypass path (custom tools active) ──
|
||||||
|
// Channel-based pipeline: read events directly from MITM proxy.
|
||||||
|
// Channel is pre-installed before send_message to avoid race conditions.
|
||||||
|
if let Some(mut rx) = mitm_rx {
|
||||||
|
let mut did_unblock_ls = false;
|
||||||
|
|
||||||
|
'channel_loop: while let Some(event) = tokio::time::timeout(
|
||||||
|
std::time::Duration::from_secs(timeout.saturating_sub(start.elapsed().as_secs())),
|
||||||
|
rx.recv(),
|
||||||
|
).await.ok().flatten() {
|
||||||
|
use crate::mitm::store::MitmEvent;
|
||||||
|
match event {
|
||||||
|
MitmEvent::ThinkingDelta(full_thinking) => {
|
||||||
|
if full_thinking.len() > last_thinking.len() {
|
||||||
|
let delta = full_thinking[last_thinking.len()..].to_string();
|
||||||
|
last_thinking = full_thinking;
|
||||||
|
|
||||||
while start.elapsed().as_secs() < timeout {
|
|
||||||
// Check for upstream errors from MITM (Google API errors)
|
|
||||||
if let Some(err) = state.mitm_store.take_upstream_error().await {
|
|
||||||
let error_msg = super::util::upstream_error_message(&err);
|
|
||||||
let error_type = super::util::upstream_error_type(&err);
|
|
||||||
yield Ok::<_, std::convert::Infallible>(Event::default().data(serde_json::to_string(&serde_json::json!({
|
yield Ok::<_, std::convert::Infallible>(Event::default().data(serde_json::to_string(&serde_json::json!({
|
||||||
"error": {
|
"candidates": [{
|
||||||
"message": error_msg,
|
"content": {
|
||||||
"type": error_type,
|
"parts": [{"text": delta, "thought": true}],
|
||||||
"code": err.status,
|
"role": "model",
|
||||||
|
},
|
||||||
|
}],
|
||||||
|
"modelVersion": model_name,
|
||||||
|
})).unwrap_or_default()));
|
||||||
}
|
}
|
||||||
})).unwrap()));
|
|
||||||
yield Ok(Event::default().data("[DONE]".to_string()));
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
|
MitmEvent::TextDelta(full_text) => {
|
||||||
|
if full_text.len() > last_text.len() {
|
||||||
|
let delta = full_text[last_text.len()..].to_string();
|
||||||
|
|
||||||
// ── Check for MITM-captured function calls FIRST ──
|
yield Ok(Event::default().data(serde_json::to_string(&serde_json::json!({
|
||||||
let captured = state.mitm_store.take_any_function_calls().await;
|
"candidates": [{
|
||||||
if let Some(ref calls) = captured {
|
"content": {
|
||||||
if !calls.is_empty() {
|
"parts": [{"text": delta}],
|
||||||
|
"role": "model",
|
||||||
|
},
|
||||||
|
}],
|
||||||
|
"modelVersion": model_name,
|
||||||
|
})).unwrap_or_default()));
|
||||||
|
last_text = full_text;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
MitmEvent::FunctionCall(calls) => {
|
||||||
let parts: Vec<serde_json::Value> = calls
|
let parts: Vec<serde_json::Value> = calls
|
||||||
.iter()
|
.iter()
|
||||||
.map(|fc| {
|
.map(|fc| {
|
||||||
@@ -598,51 +703,11 @@ async fn gemini_stream(
|
|||||||
"modelVersion": model_name,
|
"modelVersion": model_name,
|
||||||
})).unwrap_or_default()));
|
})).unwrap_or_default()));
|
||||||
yield Ok(Event::default().data("[DONE]"));
|
yield Ok(Event::default().data("[DONE]"));
|
||||||
|
state.mitm_store.drop_channel().await;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
MitmEvent::ResponseComplete => {
|
||||||
|
if !last_text.is_empty() {
|
||||||
// ── MITM bypass path (custom tools active) ──
|
|
||||||
if has_custom_tools {
|
|
||||||
// Stream thinking
|
|
||||||
if let Some(tc) = state.mitm_store.peek_thinking_text().await {
|
|
||||||
if tc.len() > last_thinking.len() {
|
|
||||||
let delta = tc[last_thinking.len()..].to_string();
|
|
||||||
last_thinking = tc;
|
|
||||||
|
|
||||||
yield Ok(Event::default().data(serde_json::to_string(&serde_json::json!({
|
|
||||||
"candidates": [{
|
|
||||||
"content": {
|
|
||||||
"parts": [{"text": delta, "thought": true}],
|
|
||||||
"role": "model",
|
|
||||||
},
|
|
||||||
}],
|
|
||||||
"modelVersion": model_name,
|
|
||||||
})).unwrap_or_default()));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Stream text
|
|
||||||
if let Some(text) = state.mitm_store.peek_response_text().await {
|
|
||||||
if !text.is_empty() && text.len() > last_text.len() {
|
|
||||||
let delta = &text[last_text.len()..];
|
|
||||||
|
|
||||||
yield Ok(Event::default().data(serde_json::to_string(&serde_json::json!({
|
|
||||||
"candidates": [{
|
|
||||||
"content": {
|
|
||||||
"parts": [{"text": delta}],
|
|
||||||
"role": "model",
|
|
||||||
},
|
|
||||||
}],
|
|
||||||
"modelVersion": model_name,
|
|
||||||
})).unwrap_or_default()));
|
|
||||||
last_text = text;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Check completion
|
|
||||||
let complete = state.mitm_store.is_response_complete();
|
|
||||||
if complete && !last_text.is_empty() {
|
|
||||||
// Final chunk with finishReason + usageMetadata
|
// Final chunk with finishReason + usageMetadata
|
||||||
let usage_meta = build_usage_metadata(&state.mitm_store, &cascade_id).await;
|
let usage_meta = build_usage_metadata(&state.mitm_store, &cascade_id).await;
|
||||||
yield Ok(Event::default().data(serde_json::to_string(&serde_json::json!({
|
yield Ok(Event::default().data(serde_json::to_string(&serde_json::json!({
|
||||||
@@ -657,17 +722,57 @@ async fn gemini_stream(
|
|||||||
"modelVersion": model_name,
|
"modelVersion": model_name,
|
||||||
})).unwrap_or_default()));
|
})).unwrap_or_default()));
|
||||||
yield Ok(Event::default().data("[DONE]"));
|
yield Ok(Event::default().data("[DONE]"));
|
||||||
|
state.mitm_store.drop_channel().await;
|
||||||
return;
|
return;
|
||||||
} else if complete && last_text.is_empty() && last_thinking.is_empty() {
|
} else if !last_thinking.is_empty() && !did_unblock_ls {
|
||||||
// Stale state — clear and retry
|
// Thinking-only response — LS needs follow-up API calls.
|
||||||
warn!("Gemini stream bypass: stale response_complete — clearing");
|
// Create a new channel and unblock the gate.
|
||||||
state.mitm_store.clear_response_async().await;
|
did_unblock_ls = true;
|
||||||
|
let (new_tx, new_rx) = tokio::sync::mpsc::channel(64);
|
||||||
|
state.mitm_store.set_channel(new_tx).await;
|
||||||
|
state.mitm_store.clear_request_in_flight();
|
||||||
|
let _ = state.mitm_store.take_any_function_calls().await;
|
||||||
|
rx = new_rx;
|
||||||
|
debug!(
|
||||||
|
"Gemini stream: thinking-only — new channel for follow-up, thinking_len={}",
|
||||||
|
last_thinking.len()
|
||||||
|
);
|
||||||
|
continue 'channel_loop;
|
||||||
|
}
|
||||||
|
// Empty response — continue waiting
|
||||||
|
}
|
||||||
|
MitmEvent::UpstreamError(err) => {
|
||||||
|
let error_msg = super::util::upstream_error_message(&err);
|
||||||
|
let error_type = super::util::upstream_error_type(&err);
|
||||||
|
yield Ok::<_, std::convert::Infallible>(Event::default().data(serde_json::to_string(&serde_json::json!({
|
||||||
|
"error": {
|
||||||
|
"message": error_msg,
|
||||||
|
"type": error_type,
|
||||||
|
"code": err.status,
|
||||||
|
}
|
||||||
|
})).unwrap()));
|
||||||
|
yield Ok(Event::default().data("[DONE]"));
|
||||||
|
state.mitm_store.drop_channel().await;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
MitmEvent::Usage(_) | MitmEvent::Grounding(_) => {}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
let poll_ms: u64 = rand::thread_rng().gen_range(150..300);
|
// Timeout or channel closed
|
||||||
tokio::time::sleep(tokio::time::Duration::from_millis(poll_ms)).await;
|
state.mitm_store.drop_channel().await;
|
||||||
continue;
|
yield Ok::<_, std::convert::Infallible>(Event::default().data(serde_json::to_string(&serde_json::json!({
|
||||||
|
"error": {
|
||||||
|
"message": format!("Timeout: no response from Google API after {timeout}s"),
|
||||||
|
"type": "upstream_error",
|
||||||
|
"code": 504,
|
||||||
}
|
}
|
||||||
|
})).unwrap()));
|
||||||
|
yield Ok(Event::default().data("[DONE]"));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
while start.elapsed().as_secs() < timeout {
|
||||||
|
|
||||||
// ── Normal LS path (no custom tools) ──
|
// ── Normal LS path (no custom tools) ──
|
||||||
if let Ok((status, data)) = state.backend.get_steps(&cascade_id).await {
|
if let Ok((status, data)) = state.backend.get_steps(&cascade_id).await {
|
||||||
|
|||||||
@@ -43,7 +43,10 @@ pub fn router(state: Arc<AppState>) -> Router {
|
|||||||
"/v1/chat/completions",
|
"/v1/chat/completions",
|
||||||
post(completions::handle_completions),
|
post(completions::handle_completions),
|
||||||
)
|
)
|
||||||
.route("/v1/gemini", post(gemini::handle_gemini))
|
.route(
|
||||||
|
"/v1beta/{*path}",
|
||||||
|
post(gemini::handle_gemini_v1beta),
|
||||||
|
)
|
||||||
.route("/v1/models", get(handle_models))
|
.route("/v1/models", get(handle_models))
|
||||||
.route("/v1/sessions", get(handle_list_sessions))
|
.route("/v1/sessions", get(handle_list_sessions))
|
||||||
.route("/v1/sessions/{id}", delete(handle_delete_session))
|
.route("/v1/sessions/{id}", delete(handle_delete_session))
|
||||||
@@ -67,7 +70,8 @@ async fn handle_root() -> Json<serde_json::Value> {
|
|||||||
"endpoints": [
|
"endpoints": [
|
||||||
"/v1/chat/completions",
|
"/v1/chat/completions",
|
||||||
"/v1/responses",
|
"/v1/responses",
|
||||||
"/v1/gemini",
|
"/v1beta/models/{model}:generateContent",
|
||||||
|
"/v1beta/models/{model}:streamGenerateContent",
|
||||||
|
|
||||||
"/v1/models",
|
"/v1/models",
|
||||||
"/v1/sessions",
|
"/v1/sessions",
|
||||||
|
|||||||
@@ -412,7 +412,8 @@ fn print_banner(
|
|||||||
println!(" \x1b[1mroutes\x1b[0m");
|
println!(" \x1b[1mroutes\x1b[0m");
|
||||||
println!(" \x1b[33m POST\x1b[0m /v1/responses");
|
println!(" \x1b[33m POST\x1b[0m /v1/responses");
|
||||||
println!(" \x1b[33m POST\x1b[0m /v1/chat/completions");
|
println!(" \x1b[33m POST\x1b[0m /v1/chat/completions");
|
||||||
println!(" \x1b[33m POST\x1b[0m /v1/gemini");
|
println!(" \x1b[33m POST\x1b[0m /v1beta/models/:model:generateContent");
|
||||||
|
println!(" \x1b[33m POST\x1b[0m /v1beta/models/:model:streamGenerateContent");
|
||||||
println!(" \x1b[32m GET \x1b[0m /v1/models");
|
println!(" \x1b[32m GET \x1b[0m /v1/models");
|
||||||
println!(" \x1b[32m GET \x1b[0m /v1/sessions");
|
println!(" \x1b[32m GET \x1b[0m /v1/sessions");
|
||||||
println!(" \x1b[31m DEL \x1b[0m /v1/sessions/:id");
|
println!(" \x1b[31m DEL \x1b[0m /v1/sessions/:id");
|
||||||
|
|||||||
Reference in New Issue
Block a user