anyllm_proxy 0.9.0

HTTP proxy translating Anthropic Messages API to OpenAI Chat Completions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
// Axum handlers for batch file upload and job management.
// POST /v1/files, POST /v1/batches, GET /v1/batches/{id}, GET /v1/batches

use crate::backend::BackendClient;
use crate::server::state::AppState;
use anyllm_batch_engine::job::{BatchSubmission, ExecutionMode, SourceFormat, SubmissionItem};
use anyllm_translate::anthropic;
use anyllm_translate::mapping::errors_map::create_anthropic_error;
use axum::{
    extract::{Multipart, Path, Query, State},
    http::StatusCode,
    response::{IntoResponse, Json, Response},
};
use serde::Deserialize;
use std::io::{BufReader, Cursor};

/// POST /v1/files - Upload a JSONL batch file via multipart/form-data.
pub async fn upload_file(State(state): State<AppState>, mut multipart: Multipart) -> Response {
    let engine = match state.batch_engine.as_ref() {
        Some(e) => e.clone(),
        None => return service_unavailable("Batch storage not available"),
    };

    let mut purpose: Option<String> = None;
    let mut file_data: Option<bytes::Bytes> = None;
    let mut filename: Option<String> = None;

    while let Ok(Some(field)) = multipart.next_field().await {
        let field_name = field.name().unwrap_or("").to_string();
        match field_name.as_str() {
            "purpose" => purpose = field.text().await.ok(),
            "file" => {
                filename = field.file_name().map(|s| s.to_string());
                file_data = field.bytes().await.ok();
            }
            _ => {}
        }
    }

    match purpose.as_deref() {
        Some("batch") => {}
        Some(other) => {
            return bad_request(&format!(
                "Unsupported purpose: '{other}'. Only 'batch' is supported."
            ));
        }
        None => return bad_request("Missing required field 'purpose'"),
    }

    let data = match file_data {
        Some(d) if !d.is_empty() => d,
        _ => return bad_request("Missing or empty 'file' field"),
    };

    let validated =
        match anyllm_batch_engine::validate_jsonl(BufReader::new(Cursor::new(data.as_ref()))) {
            Ok(v) => v,
            Err(e) => return bad_request(&format!("Invalid JSONL: {e}")),
        };

    let file_id = format!("file-{}", uuid::Uuid::new_v4());
    let byte_size = data.len() as i64;
    let line_count = validated.line_count as i64;

    match engine
        .file_store
        .insert(
            &file_id,
            None,
            filename.as_deref(),
            data.as_ref(),
            line_count,
        )
        .await
    {
        Ok(()) => {
            let now_epoch = std::time::SystemTime::now()
                .duration_since(std::time::UNIX_EPOCH)
                .unwrap_or_default()
                .as_secs() as i64;
            let file_obj = serde_json::json!({
                "id": file_id,
                "object": "file",
                "bytes": byte_size,
                "created_at": now_epoch,
                "filename": filename,
                "purpose": "batch",
            });
            (StatusCode::OK, Json(file_obj)).into_response()
        }
        Err(e) => {
            tracing::error!(error = %e, "failed to store batch file");
            internal_error("Failed to store file")
        }
    }
}

/// Request body for POST /v1/batches.
#[derive(Deserialize)]
pub struct CreateBatchRequest {
    pub input_file_id: String,
    #[serde(default = "default_endpoint")]
    pub endpoint: String,
    #[serde(default = "default_completion_window")]
    pub completion_window: String,
    pub metadata: Option<serde_json::Value>,
    /// Optional per-batch webhook URL. Must be a public HTTP(S) URL.
    /// Validated against SSRF rules (rejects private/loopback/metadata IPs).
    pub webhook_url: Option<String>,
}

fn default_endpoint() -> String {
    "/v1/chat/completions".to_string()
}

fn default_completion_window() -> String {
    "24h".to_string()
}

/// POST /v1/batches - Create a new batch job.
pub async fn create_batch(
    State(state): State<AppState>,
    vk_ctx: Option<axum::Extension<crate::server::middleware::VirtualKeyContext>>,
    Json(req): Json<CreateBatchRequest>,
) -> Response {
    // Check backend support: only openai and azure are supported
    if !is_batch_supported(&state.backend) {
        return not_implemented(&format!(
            "Batch processing is not supported by the '{}' backend",
            state.backend_name
        ));
    }

    let engine = match state.batch_engine.as_ref() {
        Some(e) => e.clone(),
        None => return service_unavailable("Batch storage not available"),
    };

    // Read file content from file store.
    let content = match engine.file_store.get_content(&req.input_file_id).await {
        Ok(Some(c)) => c,
        Ok(None) => return bad_request(&format!("Input file '{}' not found", req.input_file_id)),
        Err(e) => {
            tracing::error!(error = %e, "failed to read batch file content");
            return internal_error("Failed to read file");
        }
    };

    // Parse JSONL into submission items.
    let items: Vec<SubmissionItem> = match parse_jsonl_items(&content) {
        Ok(items) => items,
        Err(e) => return bad_request(&format!("Invalid JSONL: {e}")),
    };

    // Enforce model allowlist policy for virtual keys against each batch item.
    if let Some(axum::Extension(ref ctx)) = vk_ctx {
        for item in &items {
            if !crate::server::policy::is_model_allowed(&item.model, &ctx.allowed_models) {
                return bad_request(&format!(
                    "Model '{}' in batch item '{}' is not allowed for this API key.",
                    item.model, item.custom_id
                ));
            }
        }
    }

    // Reject private/loopback/metadata targets to prevent SSRF.
    if let Some(ref url) = req.webhook_url {
        if let Err(e) = crate::config::validate_base_url(url) {
            return bad_request(&format!("Invalid webhook_url: {e}"));
        }
    }

    let execution_mode = if is_batch_supported(&state.backend) {
        ExecutionMode::Native {
            provider: state.backend_name.clone(),
        }
    } else {
        ExecutionMode::ProxyNative
    };

    let submission = BatchSubmission {
        items,
        execution_mode,
        input_file_id: req.input_file_id.clone(),
        key_id: None,
        webhook_url: req.webhook_url.clone(),
        metadata: req.metadata.clone(),
        priority: 0,
    };

    match engine.submit(submission).await {
        Ok(job) => (StatusCode::OK, Json(job_to_openai_response(&job))).into_response(),
        Err(anyllm_batch_engine::EngineError::FileNotFound(_)) => {
            bad_request(&format!("Input file '{}' not found", req.input_file_id))
        }
        Err(e) => {
            tracing::error!(error = %e, "failed to create batch job");
            internal_error("Failed to create batch job")
        }
    }
}

/// GET /v1/batches/{batch_id}
pub async fn get_batch(State(state): State<AppState>, Path(batch_id): Path<String>) -> Response {
    let engine = match state.batch_engine.as_ref() {
        Some(e) => e.clone(),
        None => return service_unavailable("Batch storage not available"),
    };

    match engine.get(&anyllm_batch_engine::BatchId(batch_id)).await {
        Ok(Some(job)) => (StatusCode::OK, Json(job_to_openai_response(&job))).into_response(),
        Ok(None) => {
            let err = create_anthropic_error(
                anthropic::ErrorType::NotFoundError,
                "Batch not found".to_string(),
                None,
            );
            (StatusCode::NOT_FOUND, Json(err)).into_response()
        }
        Err(e) => {
            tracing::error!(error = %e, "failed to fetch batch job");
            internal_error("Failed to fetch batch job")
        }
    }
}

/// Query parameters for GET /v1/batches.
#[derive(Deserialize)]
pub struct ListBatchesQuery {
    #[serde(default = "default_limit")]
    pub limit: u32,
    pub after: Option<String>,
}

fn default_limit() -> u32 {
    20
}

/// GET /v1/batches
pub async fn list_batches(
    State(state): State<AppState>,
    Query(query): Query<ListBatchesQuery>,
) -> Response {
    let engine = match state.batch_engine.as_ref() {
        Some(e) => e.clone(),
        None => return service_unavailable("Batch storage not available"),
    };

    let limit = query.limit.min(100);

    match engine.list(None, query.after.as_deref(), limit).await {
        Ok(jobs) => {
            let has_more = jobs.len() as u32 == limit;
            let first_id = jobs.first().map(|j| j.id.0.clone());
            let last_id = jobs.last().map(|j| j.id.0.clone());
            let data: Vec<serde_json::Value> = jobs.iter().map(job_to_openai_response).collect();
            let response = serde_json::json!({
                "object": "list",
                "data": data,
                "has_more": has_more,
                "first_id": first_id,
                "last_id": last_id,
            });
            (StatusCode::OK, Json(response)).into_response()
        }
        Err(e) => {
            tracing::error!(error = %e, "failed to list batch jobs");
            internal_error("Failed to list batch jobs")
        }
    }
}

/// POST /v1/batches/{batch_id}/cancel
pub async fn cancel_batch(State(state): State<AppState>, Path(batch_id): Path<String>) -> Response {
    let Some(engine) = state.batch_engine.as_ref() else {
        return not_implemented("batch engine not available");
    };

    let id = anyllm_batch_engine::BatchId(batch_id);
    match engine.cancel(&id).await {
        Ok(job) => (StatusCode::OK, Json(job_to_openai_response(&job))).into_response(),
        Err(anyllm_batch_engine::EngineError::Queue(anyllm_batch_engine::QueueError::NotFound)) => {
            not_found_response("batch not found")
        }
        Err(e) => internal_error(&e.to_string()),
    }
}

/// Map a BatchJob to an OpenAI-compatible batch response JSON.
pub fn job_to_openai_response(job: &anyllm_batch_engine::BatchJob) -> serde_json::Value {
    let created_epoch = iso8601_to_epoch(&job.created_at);
    let completed_epoch = job.completed_at.as_deref().map(iso8601_to_epoch);

    serde_json::json!({
        "id": job.id.0,
        "object": "batch",
        "endpoint": "/v1/chat/completions",
        "status": map_batch_status(&job.status),
        "input_file_id": job.input_file_id,
        "completion_window": "24h",
        "created_at": created_epoch,
        "request_counts": {
            "total": job.request_counts.total,
            "completed": job.request_counts.succeeded,
            "failed": job.request_counts.failed,
        },
        "metadata": job.metadata,
        "output_file_id": serde_json::Value::Null,
        "error_file_id": serde_json::Value::Null,
        "completed_at": completed_epoch,
    })
}

/// Map BatchEngine status to OpenAI batch status string.
fn map_batch_status(status: &anyllm_batch_engine::BatchStatus) -> &'static str {
    match status {
        anyllm_batch_engine::BatchStatus::Queued => "validating",
        anyllm_batch_engine::BatchStatus::Processing => "in_progress",
        anyllm_batch_engine::BatchStatus::Completed => "completed",
        anyllm_batch_engine::BatchStatus::Failed => "failed",
        anyllm_batch_engine::BatchStatus::Cancelling => "cancelling",
        anyllm_batch_engine::BatchStatus::Cancelled => "cancelled",
        anyllm_batch_engine::BatchStatus::Expired => "expired",
    }
}

/// Parse JSONL bytes into SubmissionItems.
fn parse_jsonl_items(content: &[u8]) -> Result<Vec<SubmissionItem>, String> {
    let mut items = Vec::new();
    let text = std::str::from_utf8(content).map_err(|e| format!("Invalid UTF-8: {e}"))?;
    for line in text.lines() {
        let line = line.trim();
        if line.is_empty() {
            continue;
        }
        let parsed: serde_json::Value =
            serde_json::from_str(line).map_err(|e| format!("Invalid JSON: {e}"))?;
        let obj = parsed.as_object().ok_or("Expected JSON object")?;
        let custom_id = obj
            .get("custom_id")
            .and_then(|v| v.as_str())
            .ok_or("Missing custom_id")?
            .to_string();
        let body = obj.get("body").cloned().unwrap_or(serde_json::Value::Null);
        let model = body
            .get("model")
            .and_then(|v| v.as_str())
            .unwrap_or("unknown")
            .to_string();
        items.push(SubmissionItem {
            custom_id,
            model,
            body,
            source_format: SourceFormat::OpenAI,
        });
    }
    Ok(items)
}

fn iso8601_to_epoch(s: &str) -> i64 {
    let parts: Vec<&str> = s.split('T').collect();
    if parts.len() != 2 {
        return 0;
    }
    let date_parts: Vec<u64> = parts[0].split('-').filter_map(|p| p.parse().ok()).collect();
    let time_str = parts[1].trim_end_matches('Z');
    let time_parts: Vec<u64> = time_str.split(':').filter_map(|p| p.parse().ok()).collect();
    if date_parts.len() != 3 || time_parts.len() != 3 {
        return 0;
    }
    let (y, m, d) = (date_parts[0], date_parts[1], date_parts[2]);
    let (hh, mm, ss) = (time_parts[0], time_parts[1], time_parts[2]);
    let y_adj = if m <= 2 { y - 1 } else { y };
    let era = y_adj / 400;
    let yoe = y_adj - era * 400;
    let m_adj = if m > 2 { m - 3 } else { m + 9 };
    let doy = (153 * m_adj + 2) / 5 + d - 1;
    let doe = yoe * 365 + yoe / 4 - yoe / 100 + doy;
    let days = era * 146097 + doe - 719468;
    (days * 86400 + hh * 3600 + mm * 60 + ss) as i64
}

/// Check if the backend supports batch processing (OpenAI and Azure only).
fn is_batch_supported(backend: &BackendClient) -> bool {
    matches!(
        backend,
        BackendClient::OpenAI(_) | BackendClient::AzureOpenAI(_)
    )
}

fn bad_request(msg: &str) -> Response {
    let err = create_anthropic_error(
        anthropic::ErrorType::InvalidRequestError,
        msg.to_string(),
        None,
    );
    (StatusCode::BAD_REQUEST, Json(err)).into_response()
}

fn not_implemented(msg: &str) -> Response {
    let err = create_anthropic_error(
        anthropic::ErrorType::InvalidRequestError,
        msg.to_string(),
        None,
    );
    (StatusCode::NOT_IMPLEMENTED, Json(err)).into_response()
}

fn service_unavailable(msg: &str) -> Response {
    let err = create_anthropic_error(anthropic::ErrorType::ApiError, msg.to_string(), None);
    (StatusCode::SERVICE_UNAVAILABLE, Json(err)).into_response()
}

fn internal_error(msg: &str) -> Response {
    let err = create_anthropic_error(anthropic::ErrorType::ApiError, msg.to_string(), None);
    (StatusCode::INTERNAL_SERVER_ERROR, Json(err)).into_response()
}

fn not_found_response(msg: &str) -> Response {
    let err = create_anthropic_error(anthropic::ErrorType::NotFoundError, msg.to_string(), None);
    (StatusCode::NOT_FOUND, Json(err)).into_response()
}

#[cfg(test)]
mod tests {
    #[test]
    fn validate_webhook_url_rejects_private_ip() {
        let result = crate::config::validate_base_url("http://169.254.169.254/metadata");
        assert!(result.is_err());
        assert!(result.unwrap_err().contains("private/loopback"));
    }

    #[test]
    fn validate_webhook_url_accepts_public_https() {
        let result = crate::config::validate_base_url("https://hooks.example.com/notify");
        assert!(result.is_ok());
    }
}