2026-03-31 19:19:04 +08:00
|
|
|
use super::{
|
2026-07-27 09:36:31 +08:00
|
|
|
classified, classified_with_request_auth_channel, detect_claude_client_surface,
|
|
|
|
|
is_gemini_cli_request, is_gemini_models_route, is_gemini_operation_route, ClassifiedRoute,
|
2026-03-31 19:19:04 +08:00
|
|
|
};
|
2026-07-27 09:36:31 +08:00
|
|
|
use crate::ai_serving::ApiOperation;
|
2026-03-31 19:19:04 +08:00
|
|
|
|
|
|
|
|
pub(super) fn classify_ai_public_route(
|
|
|
|
|
method: &http::Method,
|
|
|
|
|
normalized_path: &str,
|
|
|
|
|
headers: &http::HeaderMap,
|
|
|
|
|
) -> Option<ClassifiedRoute> {
|
2026-05-25 06:58:15 +08:00
|
|
|
if let Some(route) = classify_antigravity_v1internal_route(method, normalized_path) {
|
|
|
|
|
Some(route)
|
|
|
|
|
} else if method == http::Method::POST && normalized_path == "/v1/chat/completions" {
|
2026-03-31 19:19:04 +08:00
|
|
|
Some(classified(
|
|
|
|
|
"ai_public",
|
|
|
|
|
"openai",
|
|
|
|
|
"chat",
|
|
|
|
|
"openai:chat",
|
|
|
|
|
true,
|
|
|
|
|
))
|
2026-05-03 17:32:41 +08:00
|
|
|
} else if method == http::Method::POST && normalized_path == "/v1/embeddings" {
|
|
|
|
|
Some(classified(
|
|
|
|
|
"ai_public",
|
|
|
|
|
"openai",
|
|
|
|
|
"embedding",
|
|
|
|
|
"openai:embedding",
|
|
|
|
|
true,
|
|
|
|
|
))
|
|
|
|
|
} else if method == http::Method::POST && normalized_path == "/v1/rerank" {
|
|
|
|
|
Some(classified(
|
|
|
|
|
"ai_public",
|
|
|
|
|
"openai",
|
|
|
|
|
"rerank",
|
|
|
|
|
"openai:rerank",
|
|
|
|
|
true,
|
|
|
|
|
))
|
2026-07-31 10:59:15 +08:00
|
|
|
} else if (method == http::Method::POST
|
|
|
|
|
|| (method == http::Method::GET
|
|
|
|
|
&& normalized_path == "/v1/responses"
|
|
|
|
|
&& is_websocket_upgrade_request(headers)))
|
2026-03-31 19:19:04 +08:00
|
|
|
&& matches!(normalized_path, "/v1/responses" | "/v1/responses/compact")
|
|
|
|
|
{
|
|
|
|
|
if normalized_path.ends_with("/compact") {
|
|
|
|
|
Some(classified(
|
|
|
|
|
"ai_public",
|
|
|
|
|
"openai",
|
2026-04-26 20:32:55 +08:00
|
|
|
"responses:compact",
|
|
|
|
|
"openai:responses:compact",
|
2026-03-31 19:19:04 +08:00
|
|
|
true,
|
|
|
|
|
))
|
|
|
|
|
} else {
|
2026-04-26 20:32:55 +08:00
|
|
|
Some(classified(
|
|
|
|
|
"ai_public",
|
|
|
|
|
"openai",
|
|
|
|
|
"responses",
|
|
|
|
|
"openai:responses",
|
|
|
|
|
true,
|
|
|
|
|
))
|
2026-03-31 19:19:04 +08:00
|
|
|
}
|
2026-07-12 03:04:15 +08:00
|
|
|
} else if method == http::Method::POST && normalized_path == "/v1/alpha/search" {
|
|
|
|
|
Some(classified(
|
|
|
|
|
"ai_public",
|
|
|
|
|
"openai",
|
|
|
|
|
"search",
|
|
|
|
|
"openai:search",
|
|
|
|
|
true,
|
|
|
|
|
))
|
2026-04-22 21:09:29 +08:00
|
|
|
} else if method == http::Method::POST
|
|
|
|
|
&& matches!(
|
|
|
|
|
normalized_path,
|
2026-05-16 19:23:23 +08:00
|
|
|
"/v1/images/generations" | "/v1/images/edits"
|
2026-04-22 21:09:29 +08:00
|
|
|
)
|
|
|
|
|
{
|
|
|
|
|
Some(classified(
|
|
|
|
|
"ai_public",
|
|
|
|
|
"openai",
|
|
|
|
|
"image",
|
|
|
|
|
"openai:image",
|
|
|
|
|
true,
|
|
|
|
|
))
|
2026-03-31 19:19:04 +08:00
|
|
|
} else if method == http::Method::POST && normalized_path == "/v1/messages/count_tokens" {
|
2026-07-27 09:36:31 +08:00
|
|
|
let request_auth_channel = claude_request_auth_channel(headers);
|
|
|
|
|
Some(
|
|
|
|
|
classified_with_request_auth_channel(
|
|
|
|
|
"ai_public",
|
|
|
|
|
"claude",
|
|
|
|
|
"count_tokens",
|
|
|
|
|
request_auth_channel,
|
|
|
|
|
"claude:messages",
|
|
|
|
|
true,
|
|
|
|
|
)
|
|
|
|
|
.with_client_surface(detect_claude_client_surface(headers))
|
|
|
|
|
.with_api_operation(ApiOperation::ClaudeCountTokens),
|
|
|
|
|
)
|
2026-03-31 19:19:04 +08:00
|
|
|
} else if method == http::Method::POST && normalized_path == "/v1/messages" {
|
2026-07-27 09:36:31 +08:00
|
|
|
let request_auth_channel = claude_request_auth_channel(headers);
|
|
|
|
|
Some(
|
|
|
|
|
classified_with_request_auth_channel(
|
|
|
|
|
"ai_public",
|
|
|
|
|
"claude",
|
|
|
|
|
"messages",
|
|
|
|
|
request_auth_channel,
|
|
|
|
|
"claude:messages",
|
|
|
|
|
true,
|
|
|
|
|
)
|
|
|
|
|
.with_client_surface(detect_claude_client_surface(headers))
|
|
|
|
|
.with_api_operation(ApiOperation::ClaudeMessagesCreate),
|
|
|
|
|
)
|
2026-03-31 19:19:04 +08:00
|
|
|
} else if normalized_path.starts_with("/v1/videos") {
|
|
|
|
|
Some(classified(
|
|
|
|
|
"ai_public",
|
|
|
|
|
"openai",
|
|
|
|
|
"video",
|
|
|
|
|
"openai:video",
|
|
|
|
|
true,
|
|
|
|
|
))
|
2026-07-05 01:28:57 +08:00
|
|
|
} else if method == http::Method::POST
|
|
|
|
|
&& matches!(normalized_path, "/v1/interactions" | "/v1beta/interactions")
|
|
|
|
|
{
|
|
|
|
|
Some(classified_with_request_auth_channel(
|
|
|
|
|
"ai_public",
|
|
|
|
|
"gemini",
|
|
|
|
|
"interactions",
|
|
|
|
|
"api_key",
|
|
|
|
|
"gemini:interactions",
|
|
|
|
|
true,
|
|
|
|
|
))
|
2026-05-22 16:48:15 +08:00
|
|
|
} else if method == http::Method::POST && is_gemini_models_route(normalized_path) {
|
2026-03-31 19:19:04 +08:00
|
|
|
if normalized_path.ends_with(":predictLongRunning") {
|
|
|
|
|
Some(classified(
|
|
|
|
|
"ai_public",
|
|
|
|
|
"gemini",
|
|
|
|
|
"video",
|
|
|
|
|
"gemini:video",
|
|
|
|
|
true,
|
|
|
|
|
))
|
2026-05-17 14:24:32 +00:00
|
|
|
} else if normalized_path.ends_with(":embedContent")
|
|
|
|
|
|| normalized_path.ends_with(":batchEmbedContents")
|
|
|
|
|
{
|
|
|
|
|
Some(classified_with_request_auth_channel(
|
|
|
|
|
"ai_public",
|
|
|
|
|
"gemini",
|
|
|
|
|
"embedding",
|
|
|
|
|
"api_key",
|
|
|
|
|
"gemini:embedding",
|
|
|
|
|
true,
|
|
|
|
|
))
|
2026-03-31 19:19:04 +08:00
|
|
|
} else if is_gemini_cli_request(headers) {
|
2026-05-02 21:23:33 +08:00
|
|
|
Some(classified_with_request_auth_channel(
|
2026-04-29 09:25:19 +08:00
|
|
|
"ai_public",
|
|
|
|
|
"gemini",
|
2026-05-02 21:23:33 +08:00
|
|
|
"generate_content",
|
|
|
|
|
"bearer_like",
|
2026-04-29 09:25:19 +08:00
|
|
|
"gemini:generate_content",
|
|
|
|
|
true,
|
|
|
|
|
))
|
2026-03-31 19:19:04 +08:00
|
|
|
} else {
|
2026-05-02 21:23:33 +08:00
|
|
|
Some(classified_with_request_auth_channel(
|
2026-03-31 19:19:04 +08:00
|
|
|
"ai_public",
|
|
|
|
|
"gemini",
|
2026-04-29 09:25:19 +08:00
|
|
|
"generate_content",
|
2026-05-02 21:23:33 +08:00
|
|
|
"api_key",
|
2026-04-29 09:25:19 +08:00
|
|
|
"gemini:generate_content",
|
2026-03-31 19:19:04 +08:00
|
|
|
true,
|
|
|
|
|
))
|
|
|
|
|
}
|
2026-05-22 16:48:15 +08:00
|
|
|
} else if is_gemini_operation_method(method, normalized_path)
|
|
|
|
|
&& is_gemini_operation_route(normalized_path)
|
|
|
|
|
{
|
2026-03-31 19:19:04 +08:00
|
|
|
Some(classified(
|
|
|
|
|
"ai_public",
|
|
|
|
|
"gemini",
|
|
|
|
|
"video",
|
|
|
|
|
"gemini:video",
|
|
|
|
|
true,
|
|
|
|
|
))
|
2026-05-22 16:48:15 +08:00
|
|
|
} else if is_gemini_files_method(method, normalized_path) {
|
2026-03-31 19:19:04 +08:00
|
|
|
Some(classified(
|
|
|
|
|
"ai_public",
|
|
|
|
|
"gemini",
|
|
|
|
|
"files",
|
2026-04-29 09:25:19 +08:00
|
|
|
"gemini:files",
|
2026-03-31 19:19:04 +08:00
|
|
|
true,
|
|
|
|
|
))
|
|
|
|
|
} else {
|
|
|
|
|
None
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-05-22 16:48:15 +08:00
|
|
|
|
2026-07-27 09:36:31 +08:00
|
|
|
fn claude_request_auth_channel(headers: &http::HeaderMap) -> &'static str {
|
|
|
|
|
if crate::headers::header_value_str(headers, "x-api-key").is_some()
|
|
|
|
|
|| crate::headers::header_value_str(headers, "api-key").is_some()
|
|
|
|
|
{
|
|
|
|
|
"api_key"
|
|
|
|
|
} else if crate::headers::header_value_str(headers, http::header::AUTHORIZATION.as_str())
|
|
|
|
|
.is_some_and(|value| value.trim().to_ascii_lowercase().starts_with("bearer "))
|
|
|
|
|
{
|
|
|
|
|
"bearer_like"
|
|
|
|
|
} else {
|
|
|
|
|
"api_key"
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-31 10:59:15 +08:00
|
|
|
fn is_websocket_upgrade_request(headers: &http::HeaderMap) -> bool {
|
|
|
|
|
let has_upgrade_connection = headers
|
|
|
|
|
.get(http::header::CONNECTION)
|
|
|
|
|
.and_then(|value| value.to_str().ok())
|
|
|
|
|
.is_some_and(|value| {
|
|
|
|
|
value
|
|
|
|
|
.split(',')
|
|
|
|
|
.map(str::trim)
|
|
|
|
|
.any(|value| value.eq_ignore_ascii_case("upgrade"))
|
|
|
|
|
});
|
|
|
|
|
let has_websocket_upgrade = headers
|
|
|
|
|
.get(http::header::UPGRADE)
|
|
|
|
|
.and_then(|value| value.to_str().ok())
|
|
|
|
|
.is_some_and(|value| value.eq_ignore_ascii_case("websocket"));
|
|
|
|
|
|
|
|
|
|
has_upgrade_connection && has_websocket_upgrade
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-22 16:48:15 +08:00
|
|
|
fn is_gemini_operation_method(method: &http::Method, normalized_path: &str) -> bool {
|
|
|
|
|
method == http::Method::GET
|
|
|
|
|
|| (method == http::Method::POST && normalized_path.ends_with(":cancel"))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn is_gemini_files_method(method: &http::Method, normalized_path: &str) -> bool {
|
|
|
|
|
(method == http::Method::POST && normalized_path == "/upload/v1beta/files")
|
|
|
|
|
|| ((method == http::Method::GET || method == http::Method::DELETE)
|
|
|
|
|
&& normalized_path.starts_with("/v1beta/files"))
|
|
|
|
|
}
|
2026-05-28 11:58:00 +08:00
|
|
|
|
2026-05-25 06:58:15 +08:00
|
|
|
fn classify_antigravity_v1internal_route(
|
|
|
|
|
method: &http::Method,
|
|
|
|
|
normalized_path: &str,
|
|
|
|
|
) -> Option<ClassifiedRoute> {
|
|
|
|
|
if method != http::Method::POST {
|
|
|
|
|
return None;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let action = normalized_path.strip_prefix("/v1internal:")?;
|
|
|
|
|
let (route_kind, execution_runtime_candidate) = match action {
|
|
|
|
|
"loadCodeAssist" => ("load_code_assist", false),
|
|
|
|
|
"fetchAvailableModels" => ("fetch_available_models", false),
|
2026-07-06 03:43:42 +08:00
|
|
|
"retrieveUserQuotaSummary" => ("retrieve_user_quota_summary", false),
|
2026-05-25 06:58:15 +08:00
|
|
|
"fetchUserInfo" => ("fetch_user_info", false),
|
|
|
|
|
"fetchAdminControls" => ("fetch_admin_controls", false),
|
|
|
|
|
"setUserSettings" => ("set_user_settings", false),
|
|
|
|
|
"listExperiments" => ("list_experiments", false),
|
|
|
|
|
"recordCodeAssistMetrics" => ("record_code_assist_metrics", false),
|
2026-07-06 03:43:42 +08:00
|
|
|
"writeTrajectoryAcls" => ("write_trajectory_acls", false),
|
2026-05-25 06:58:15 +08:00
|
|
|
"streamGenerateContent" => ("stream_generate_content", true),
|
|
|
|
|
_ => return None,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
Some(classified_with_request_auth_channel(
|
|
|
|
|
"ai_public",
|
|
|
|
|
"antigravity",
|
|
|
|
|
route_kind,
|
|
|
|
|
"bearer_like",
|
|
|
|
|
"antigravity:v1internal",
|
|
|
|
|
execution_runtime_candidate,
|
|
|
|
|
))
|
|
|
|
|
}
|
2026-07-31 10:59:15 +08:00
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
|
mod tests {
|
|
|
|
|
use axum::http::header::{CONNECTION, UPGRADE};
|
|
|
|
|
use axum::http::{HeaderMap, HeaderValue, Method};
|
|
|
|
|
|
|
|
|
|
use super::classify_ai_public_route;
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn classifies_websocket_upgrade_on_responses_route() {
|
|
|
|
|
let mut headers = HeaderMap::new();
|
|
|
|
|
headers.insert(CONNECTION, HeaderValue::from_static("keep-alive, Upgrade"));
|
|
|
|
|
headers.insert(UPGRADE, HeaderValue::from_static("websocket"));
|
|
|
|
|
|
|
|
|
|
let route = classify_ai_public_route(&Method::GET, "/v1/responses", &headers)
|
|
|
|
|
.expect("Responses WebSocket should be an AI public route");
|
|
|
|
|
assert_eq!(route.route_class, "ai_public");
|
|
|
|
|
assert_eq!(route.route_family, "openai");
|
|
|
|
|
assert_eq!(route.route_kind, "responses");
|
|
|
|
|
assert_eq!(route.auth_endpoint_signature, "openai:responses");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn does_not_classify_plain_get_as_responses_websocket() {
|
|
|
|
|
assert!(
|
|
|
|
|
classify_ai_public_route(&Method::GET, "/v1/responses", &HeaderMap::new()).is_none()
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|