Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions crates/agentic-core/src/proxy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,48 @@ pub fn error_response(status: StatusCode, code: &str, message: &str) -> ProxyRes
}
}

/// Proxy a GET request to an arbitrary upstream path.
///
/// Applies the same header filtering and auth injection as [`proxy_request`].
/// Uses the non-streaming client; the response body is returned as a full
/// [`ProxyBody::Full`] payload.
pub async fn proxy_get(path: &str, request_headers: &HeaderMap, state: &ProxyState) -> ProxyResponse {
let llm_headers = filter_request_headers(request_headers, &state.config);
let base = state.config.llm_api_base.trim_end_matches('/');
let url = format!("{base}/{}", path.trim_start_matches('/'));

let llm_resp = match state.non_stream_client.get(&url).headers(llm_headers).send().await {
Ok(r) => r,
Err(e) if e.is_timeout() => {
warn!("upstream GET {path} timed out: {e}");
return error_response(StatusCode::GATEWAY_TIMEOUT, "upstream_timeout", "upstream timeout");
}
Err(e) => {
warn!("upstream GET {path} failed: {e}");
return error_response(StatusCode::BAD_GATEWAY, "upstream_unavailable", "upstream unavailable");
}
};

let status = StatusCode::from_u16(llm_resp.status().as_u16()).unwrap_or(StatusCode::BAD_GATEWAY);
let response_headers = filter_response_headers(llm_resp.headers());

match llm_resp.bytes().await {
Ok(payload) => ProxyResponse {
status,
headers: response_headers,
body: ProxyBody::Full(payload),
},
Err(e) => {
warn!("failed to read upstream GET {path} body: {e}");
error_response(
StatusCode::BAD_GATEWAY,
"upstream_unavailable",
"failed to read upstream response",
)
}
}
}

pub async fn proxy_request(request: ProxyRequest, state: &ProxyState) -> ProxyResponse {
let is_streaming = serde_json::from_slice::<Value>(&request.body)
.ok()
Expand Down
3 changes: 2 additions & 1 deletion crates/agentic-server/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use tower_http::cors::{AllowOrigin, Any, CorsLayer};
use agentic_core::executor::ExecutionContext;
use agentic_core::proxy::ProxyState;

use crate::handler::{conversations, health, ready, responses, responses_ws};
use crate::handler::{conversations, health, models, ready, responses, responses_ws};

/// Server-level configuration read from environment variables.
pub struct ServerConfig {
Expand Down Expand Up @@ -71,6 +71,7 @@ pub fn build_router(state: AppState, server_config: &ServerConfig) -> Router {
.route("/health", get(health))
.route("/ready", get(ready))
.route("/v1/conversations", post(conversations))
.route("/v1/models", get(models))
.route("/v1/responses", post(responses).get(responses_ws))
.layer(server_config.cors_layer())
.with_state(state)
Expand Down
Loading