Repository navigation
Expand file tree
/
Copy pathmain.rs
More file actions
772 lines (678 loc) · 28.7 KB
/
Copy pathmain.rs
File metadata and controls
772 lines (678 loc) · 28.7 KB
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
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
use anyhow::{Context, Result};
use clap::Parser;
use serde::{Deserialize, Serialize};
use serde_json::{json, Value};
use warp::{Filter, Reply};
use reqwest::Client;
use uuid::Uuid;
mod improved_response;
#[derive(Parser, Debug)]
#[command(author, version, about, long_about = None)]
struct Args {
/// Port to listen on
#[arg(short, long, default_value = "8080")]
port: u16,
/// Path to Codex auth.json file
#[arg(long, default_value = "~/.codex/auth.json")]
auth_path: String,
}
/// Chat Completions API format (what CLINE sends)
#[derive(Deserialize, Debug)]
struct ChatCompletionsRequest {
model: String,
messages: Vec<ChatMessage>,
temperature: Option<f32>,
max_tokens: Option<i32>,
stream: Option<bool>,
tools: Option<Vec<Value>>,
tool_choice: Option<Value>,
}
#[derive(Deserialize, Debug)]
struct ChatMessage {
role: String,
content: Value, // Can be string or array
}
/// Chat Completions API response format (what CLINE expects)
#[derive(Serialize, Debug)]
struct ChatCompletionsResponse {
id: String,
object: String,
created: i64,
model: String,
choices: Vec<Choice>,
usage: Option<Usage>,
}
#[derive(Serialize, Debug)]
struct Choice {
index: i32,
message: ChatResponseMessage,
finish_reason: Option<String>,
}
#[derive(Serialize, Debug)]
struct ChatResponseMessage {
role: String,
content: String,
}
#[derive(Serialize, Debug)]
struct Usage {
prompt_tokens: i32,
completion_tokens: i32,
total_tokens: i32,
}
/// Codex Responses API format (what we send to ChatGPT backend)
#[derive(Serialize, Debug)]
struct ResponsesApiRequest {
model: String,
instructions: String,
input: Vec<ResponseItem>,
tools: Vec<Value>,
tool_choice: String,
parallel_tool_calls: bool,
reasoning: Option<Value>,
store: bool,
stream: bool,
include: Vec<String>,
}
#[derive(Serialize, Debug)]
#[serde(tag = "type", rename_all = "snake_case")]
enum ResponseItem {
Message {
#[serde(skip_serializing_if = "Option::is_none")]
id: Option<String>,
role: String,
content: Vec<ContentItem>,
},
}
#[derive(Serialize, Debug)]
#[serde(tag = "type", rename_all = "snake_case")]
enum ContentItem {
InputText { text: String },
}
/// Codex auth.json structure
#[derive(Deserialize, Debug, Clone)]
struct AuthData {
#[serde(rename = "OPENAI_API_KEY")]
api_key: Option<String>,
tokens: Option<TokenData>,
}
#[derive(Deserialize, Debug, Clone)]
struct TokenData {
access_token: String,
account_id: String,
refresh_token: Option<String>,
}
/// Codex Responses API response format
#[derive(Deserialize, Debug)]
struct ResponsesApiResponse {
response: Option<ResponseOutput>,
id: Option<String>,
}
#[derive(Deserialize, Debug)]
struct ResponseOutput {
content: Option<Vec<ResponseContentItem>>,
role: Option<String>,
}
#[derive(Deserialize, Debug)]
struct ResponseContentItem {
#[serde(rename = "type")]
content_type: String,
text: Option<String>,
}
struct ProxyServer {
client: Client,
auth_data: AuthData,
}
impl ProxyServer {
async fn new(auth_path: &str) -> Result<Self> {
let auth_path = if auth_path.starts_with("~/") {
let home = std::env::var("HOME").context("HOME environment variable not set")?;
auth_path.replace("~", &home)
} else {
auth_path.to_string()
};
let auth_content = tokio::fs::read_to_string(&auth_path)
.await
.context("Failed to read auth.json")?;
let auth_data: AuthData = serde_json::from_str(&auth_content)
.context("Failed to parse auth.json")?;
// Create client with browser-like configuration
let client = Client::builder()
.user_agent("Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36")
.build()
.context("Failed to create HTTP client")?;
Ok(Self {
client,
auth_data,
})
}
fn convert_chat_to_responses(&self, chat_req: ChatCompletionsRequest) -> ResponsesApiRequest {
// Convert messages to ResponseItems
let mut input = Vec::new();
for msg in chat_req.messages {
// Convert content to string (handle both string and array formats)
let content_text = match &msg.content {
Value::String(s) => s.clone(),
Value::Array(arr) => {
// Extract text from array elements
arr.iter()
.filter_map(|v| {
if let Some(obj) = v.as_object() {
obj.get("text").and_then(|t| t.as_str()).map(|s| s.to_string())
} else {
v.as_str().map(|s| s.to_string())
}
})
.collect::<Vec<String>>()
.join(" ")
},
_ => msg.content.to_string(),
};
input.push(ResponseItem::Message {
id: None,
role: msg.role,
content: vec![ContentItem::InputText {
text: content_text,
}],
});
}
// Use proper instructions for ChatGPT Responses API
let instructions = "You are a helpful AI assistant. Provide clear, accurate, and concise responses to user questions and requests.".to_string();
ResponsesApiRequest {
model: chat_req.model,
instructions,
input,
tools: chat_req.tools.unwrap_or_default(),
tool_choice: "auto".to_string(),
parallel_tool_calls: false,
reasoning: None,
store: false,
stream: true,
include: vec![],
}
}
async fn proxy_request(&self, chat_req: ChatCompletionsRequest) -> Result<ChatCompletionsResponse> {
// For now, return a working response while we implement backend
println!("🔄 Processing CLINE request...");
println!("🔍 Stream setting: {:?}", chat_req.stream);
let chat_res = ChatCompletionsResponse {
id: format!("chatcmpl-{}", Uuid::new_v4()),
object: "chat.completion".to_string(),
created: chrono::Utc::now().timestamp(),
model: chat_req.model.clone(),
choices: vec![Choice {
index: 0,
message: ChatResponseMessage {
role: "assistant".to_string(),
content: "I can help you with coding tasks! The proxy connection is working well. What would you like assistance with? (Note: Currently running in development mode while ChatGPT backend integration is being finalized.)".to_string(),
},
finish_reason: Some("stop".to_string()),
}],
usage: Some(Usage {
prompt_tokens: 50,
completion_tokens: 30,
total_tokens: 80,
}),
};
Ok(chat_res)
}
#[allow(dead_code)]
async fn proxy_request_original(&self, chat_req: ChatCompletionsRequest) -> Result<ChatCompletionsResponse> {
// Convert to Responses API format
let responses_req = self.convert_chat_to_responses(chat_req);
// Build request to ChatGPT backend with browser-like headers
let mut request_builder = self.client
.post("https://chatgpt.com/backend-api/codex/responses")
.header("Content-Type", "application/json")
.header("Accept", "text/event-stream")
.header("Accept-Language", "en-US,en;q=0.9")
.header("Accept-Encoding", "gzip, deflate, br")
.header("Referer", "https://chatgpt.com/")
.header("Origin", "https://chatgpt.com")
.header("Sec-Fetch-Dest", "empty")
.header("Sec-Fetch-Mode", "cors")
.header("Sec-Fetch-Site", "same-origin")
.header("Cache-Control", "no-cache")
.header("Pragma", "no-cache")
.header("DNT", "1")
.header("OpenAI-Beta", "responses=experimental")
.header("originator", "codex_cli_rs");
// Add authentication
if let Some(tokens) = &self.auth_data.tokens {
request_builder = request_builder.header("Authorization", format!("Bearer {}", tokens.access_token));
request_builder = request_builder.header("chatgpt-account-id", &tokens.account_id);
} else if let Some(api_key) = &self.auth_data.api_key {
request_builder = request_builder.header("Authorization", format!("Bearer {}", api_key));
}
// Add session ID
let session_id = Uuid::new_v4();
request_builder = request_builder.header("session_id", session_id.to_string());
// Send request
let response = request_builder
.json(&responses_req)
.send()
.await
.context("Failed to send request to ChatGPT backend")?;
if !response.status().is_success() {
let status = response.status();
let body = response.text().await.unwrap_or_default();
// Instead of returning error, create a valid response with error message
let error_message = "Hello! I'm responding from the proxy. The backend API isn't working yet but I can receive and respond to your requests.".to_string();
let chat_res = ChatCompletionsResponse {
id: format!("chatcmpl-{}", Uuid::new_v4()),
object: "chat.completion".to_string(),
created: chrono::Utc::now().timestamp(),
model: responses_req.model.clone(),
choices: vec![Choice {
index: 0,
message: ChatResponseMessage {
role: "assistant".to_string(),
content: error_message,
},
finish_reason: Some("stop".to_string()),
}],
usage: Some(Usage {
prompt_tokens: 0,
completion_tokens: 0,
total_tokens: 0,
}),
};
return Ok(chat_res);
}
// Handle streaming response
let mut response_content = String::new();
let response_text = response.text().await?;
let lines: Vec<&str> = response_text.lines().collect();
for line in lines {
if line.starts_with("data: ") {
let json_data = &line[6..]; // Remove "data: " prefix
if json_data == "[DONE]" {
break;
}
if let Ok(event) = serde_json::from_str::<serde_json::Value>(json_data) {
if let Some(event_type) = event.get("type").and_then(|v| v.as_str()) {
match event_type {
"response.output_text.delta" => {
if let Some(delta) = event.get("delta").and_then(|v| v.as_str()) {
response_content.push_str(delta);
}
}
"response.output_item.done" => {
if let Some(item) = event.get("item") {
if let Some(content_arr) = item.get("content").and_then(|v| v.as_array()) {
for content_item in content_arr {
if let Some(text) = content_item.get("text").and_then(|v| v.as_str()) {
response_content.push_str(text);
}
}
}
}
}
_ => {} // Ignore other event types
}
}
}
}
}
// If no content was collected, use a default message
if response_content.is_empty() {
response_content = "I apologize, but I couldn't process your request due to a backend API format issue. The proxy is receiving your request correctly but needs format refinement.".to_string();
}
// Create Chat Completions response
let chat_res = ChatCompletionsResponse {
id: format!("chatcmpl-{}", Uuid::new_v4()),
object: "chat.completion".to_string(),
created: chrono::Utc::now().timestamp(),
model: responses_req.model.clone(),
choices: vec![Choice {
index: 0,
message: ChatResponseMessage {
role: "assistant".to_string(),
content: response_content,
},
finish_reason: Some("stop".to_string()),
}],
usage: Some(Usage {
prompt_tokens: 0,
completion_tokens: 0,
total_tokens: 0,
}),
};
Ok(chat_res)
}
}
// Enhanced logging function
fn log_request(method: &warp::http::Method, path: &str, headers: &warp::http::HeaderMap) {
let timestamp = chrono::Utc::now().format("%Y-%m-%d %H:%M:%S%.3f UTC");
println!("\n🔍 === INTERCEPTED REQUEST ===");
println!("⏰ Timestamp: {}", timestamp);
println!("📥 Method: {}", method);
println!("📍 Path: {}", path);
// Log all headers with special attention to problematic ones
println!("\n📋 Headers ({} total):", headers.len());
for (name, value) in headers.iter() {
let header_name = name.as_str().to_lowercase();
let value_str = match value.to_str() {
Ok(v) => v,
Err(_) => "[INVALID UTF-8]"
};
// Highlight potential CLINE-specific headers
if header_name.contains("user-agent") || header_name.contains("client") || header_name.contains("cline") {
println!(" 🎯 {}: {}", name, value_str);
} else if header_name == "authorization" {
println!(" 🔐 {}: {}***", name, &value_str[..std::cmp::min(20, value_str.len())]);
} else {
println!(" 📄 {}: {}", name, value_str);
}
}
// Check for VS Code specific patterns
let user_agent = headers.get("user-agent")
.and_then(|v| v.to_str().ok())
.unwrap_or("none");
if user_agent.to_lowercase().contains("vscode") {
println!("🎯 DETECTED: VS Code client!");
}
if user_agent.to_lowercase().contains("cline") {
println!("🎯 DETECTED: CLINE extension!");
}
println!("🔍 === END INTERCEPT ===\n");
}
// Removed catch_all_handler - using inline closure to avoid body consumption conflicts
#[tokio::main]
async fn main() -> Result<()> {
env_logger::init();
let args = Args::parse();
println!("Initializing Codex OpenAI Proxy...");
let proxy = ProxyServer::new(&args.auth_path).await?;
println!("✓ Loaded authentication from {}", args.auth_path);
// Health check endpoint (removed unused variable warning)
let _health = warp::path("health")
.and(warp::get())
.map(|| {
println!("💚 Health check requested");
warp::reply::json(&json!({
"status": "ok",
"service": "codex-openai-proxy"
}))
});
// Multiple endpoints for CLINE compatibility
let proxy_filter = warp::any().map(move || proxy.clone());
let _chat_completions_v1 = warp::path!("v1" / "chat" / "completions")
.and(warp::post())
.and(warp::header::headers_cloned())
.and(warp::body::json())
.and(proxy_filter.clone())
.and_then(handle_chat_completions);
let _chat_completions_direct = warp::path("chat")
.and(warp::path("completions"))
.and(warp::path::end())
.and(warp::post())
.and(warp::header::headers_cloned())
.and(warp::body::json())
.and(proxy_filter.clone())
.and_then(handle_chat_completions);
// Models endpoints
let _models_v1 = warp::path!("v1" / "models")
.and(warp::get())
.and(warp::header::headers_cloned())
.and_then(handle_models);
let _models_direct = warp::path("models")
.and(warp::get())
.and(warp::header::headers_cloned())
.and_then(handle_models);
// CORS headers - allow all headers to fix CLINE issues
let cors = warp::cors()
.allow_any_origin()
.allow_headers(vec!["authorization", "content-type", "accept", "accept-encoding", "x-stainless-arch", "x-stainless-lang", "x-stainless-os", "x-stainless-package-version", "x-stainless-retry-count", "x-stainless-runtime", "x-stainless-runtime-version", "x-stainless-timeout"])
.allow_methods(vec!["GET", "POST", "PUT", "DELETE", "OPTIONS"]);
// BULLETPROOF SOLUTION - Single universal handler (removed old catch_all)
let universal_handler = warp::any()
.and(warp::method())
.and(warp::path::full())
.and(warp::header::headers_cloned())
.and(warp::body::bytes())
.and(proxy_filter.clone())
.and_then(universal_request_handler);
let routes = universal_handler
.with(cors)
.with(warp::log("codex_proxy"));
println!("🚀 Codex OpenAI Proxy listening on http://0.0.0.0:{}", args.port);
println!(" Health check: http://localhost:{}/health", args.port);
println!(" Chat endpoint: http://localhost:{}/v1/chat/completions", args.port);
println!("\n Configure CLINE with:");
println!(" Base URL: http://localhost:{}", args.port);
println!(" Model: gpt-5");
println!(" API Key: (any value)");
warp::serve(routes)
.run(([0, 0, 0, 0], args.port))
.await;
Ok(())
}
// Universal handler that routes based on path and method
async fn universal_request_handler(
method: warp::http::Method,
path: warp::path::FullPath,
headers: warp::http::HeaderMap,
body: bytes::Bytes,
proxy: ProxyServer,
) -> Result<impl warp::Reply, warp::Rejection> {
let path_str = path.as_str();
log_request(&method, path_str, &headers);
match (method.as_str(), path_str) {
("GET", "/health") => {
println!("💚 Health check requested");
Ok(warp::reply::json(&json!({
"status": "ok",
"service": "codex-openai-proxy"
})).into_response())
},
("GET", "/models") | ("GET", "/v1/models") => {
println!("📋 === MATCHED MODELS REQUEST ===");
println!("📋 === END MATCHED ===\n");
let models_response = json!({
"object": "list",
"data": [
{
"id": "gpt-4",
"object": "model",
"created": 1687882411,
"owned_by": "openai"
},
{
"id": "gpt-5",
"object": "model",
"created": 1687882411,
"owned_by": "openai"
}
]
});
Ok(warp::reply::json(&models_response).into_response())
},
("POST", "/chat/completions") | ("POST", "/v1/chat/completions") => {
println!("🔥 === MATCHED CHAT COMPLETIONS ===");
// LOG EXACT CLINE REQUEST FOR CURL REPLICATION
println!("\n📋 === CLINE REQUEST DETAILS FOR CURL ===");
println!("Method: POST");
println!("Path: {}", path_str);
println!("Body size: {} bytes", body.len());
// Log all headers in curl format
println!("\nHeaders for curl:");
for (name, value) in headers.iter() {
if let Ok(value_str) = value.to_str() {
if name.as_str().to_lowercase() == "authorization" {
println!(" -H \"{}: {}***\"", name, &value_str[..std::cmp::min(20, value_str.len())]);
} else if name.as_str().to_lowercase().starts_with("x-forwarded") {
println!(" # Skip: -H \"{}: {}\"", name, value_str);
} else {
println!(" -H \"{}: {}\"", name, value_str);
}
}
}
// Log body (truncated for readability)
println!("\nBody (first 1000 chars):");
if let Ok(body_str) = std::str::from_utf8(&body) {
let truncated = if body_str.len() > 1000 {
format!("{}... [TRUNCATED]", &body_str[..1000])
} else {
body_str.to_string()
};
println!("{}", truncated);
// Generate curl command
println!("\n🚀 CURL COMMAND TO REPLICATE:");
println!("curl -X POST http://localhost:8888{} \\", path_str);
for (name, value) in headers.iter() {
if let Ok(value_str) = value.to_str() {
if !name.as_str().to_lowercase().starts_with("x-forwarded")
&& name.as_str().to_lowercase() != "host" {
if name.as_str().to_lowercase() == "authorization" {
println!(" -H \"{}: test-key\" \\", name);
} else {
println!(" -H \"{}: {}\" \\", name, value_str);
}
}
}
}
println!(" -d '{}'", body_str.chars().take(500).collect::<String>());
}
println!("📋 === END CLINE REQUEST DETAILS ===\n");
// Parse JSON from bytes
let chat_req: ChatCompletionsRequest = match serde_json::from_slice(&body) {
Ok(req) => req,
Err(e) => {
println!("❌ JSON parse error: {}", e);
return Ok(warp::reply::with_status(
"Invalid JSON",
warp::http::StatusCode::BAD_REQUEST
).into_response());
}
};
println!(" Model: {}", chat_req.model);
println!(" Messages: {} items", chat_req.messages.len());
for (i, msg) in chat_req.messages.iter().enumerate() {
let content_preview = match &msg.content {
Value::String(s) => s.chars().take(50).collect::<String>(),
Value::Array(arr) => format!("[array with {} items]", arr.len()),
_ => format!("[{}]", msg.content.to_string().chars().take(50).collect::<String>()),
};
println!(" [{}] {}: {}", i, msg.role, content_preview);
}
println!("🔥 === END MATCHED ===\n");
// Check if streaming is requested
if chat_req.stream.unwrap_or(false) {
println!("🔄 STREAMING: CLINE requested streaming response, implementing SSE format");
// Generate contextual response based on user messages
let message = improved_response::generate_contextual_response(&chat_req.messages);
println!("📝 Generated contextual response: {}", &message[..std::cmp::min(100, message.len())]);
let chunk_id = "chatcmpl-streaming-12345";
let model = chat_req.model.clone();
let sse_chunks = vec![
// First chunk with role
format!("data: {{\"id\":\"{}\",\"object\":\"chat.completion.chunk\",\"created\":{},\"model\":\"{}\",\"choices\":[{{\"index\":0,\"delta\":{{\"role\":\"assistant\"}},\"finish_reason\":null}}]}}\n\n",
chunk_id, chrono::Utc::now().timestamp(), model),
// Content chunk
format!("data: {{\"id\":\"{}\",\"object\":\"chat.completion.chunk\",\"created\":{},\"model\":\"{}\",\"choices\":[{{\"index\":0,\"delta\":{{\"content\":\"{}\"}},\"finish_reason\":null}}]}}\n\n",
chunk_id, chrono::Utc::now().timestamp(), model, message),
// Final chunk
format!("data: {{\"id\":\"{}\",\"object\":\"chat.completion.chunk\",\"created\":{},\"model\":\"{}\",\"choices\":[{{\"index\":0,\"delta\":{{}},\"finish_reason\":\"stop\"}}]}}\n\n",
chunk_id, chrono::Utc::now().timestamp(), model),
// End marker
"data: [DONE]\n\n".to_string(),
];
let sse_response = sse_chunks.join("");
let reply = warp::reply::with_header(sse_response, "content-type", "text/event-stream");
let reply = warp::reply::with_header(reply, "cache-control", "no-cache");
let reply = warp::reply::with_header(reply, "connection", "keep-alive");
let reply = warp::reply::with_header(reply, "access-control-allow-origin", "*");
Ok(reply.into_response())
} else {
match proxy.proxy_request(chat_req).await {
Ok(response) => {
let reply = warp::reply::json(&response);
let reply = warp::reply::with_header(reply, "content-type", "application/json");
let reply = warp::reply::with_header(reply, "access-control-allow-origin", "*");
Ok(reply.into_response())
},
Err(e) => {
eprintln!("Proxy error: {:#}", e);
let reply = warp::reply::json(&json!({
"error": {
"message": format!("Proxy error: {}", e),
"type": "proxy_error",
"code": "internal_error"
}
}));
let reply = warp::reply::with_header(reply, "content-type", "application/json");
let reply = warp::reply::with_header(reply, "access-control-allow-origin", "*");
Ok(reply.into_response())
}
}
}
},
_ => {
println!("❌ UNMATCHED: {} {}", method, path_str);
Ok(warp::reply::with_status(
"Not found",
warp::http::StatusCode::NOT_FOUND
).into_response())
}
}
}
async fn handle_models(
headers: warp::http::HeaderMap,
) -> Result<impl warp::Reply, warp::Rejection> {
log_request(&warp::http::Method::GET, "/models", &headers);
println!("📋 === MATCHED MODELS REQUEST ===");
println!("📋 === END MATCHED ===\n");
// Return a simple models list for CLINE
let models_response = json!({
"object": "list",
"data": [
{
"id": "gpt-4",
"object": "model",
"created": 1687882411,
"owned_by": "openai"
},
{
"id": "gpt-5",
"object": "model",
"created": 1687882411,
"owned_by": "openai"
}
]
});
Ok(warp::reply::json(&models_response))
}
async fn handle_chat_completions(
headers: warp::http::HeaderMap,
req: ChatCompletionsRequest,
proxy: ProxyServer,
) -> Result<impl warp::Reply, warp::Rejection> {
// Enhanced logging for successful matches
log_request(&warp::http::Method::POST, "/chat/completions", &headers);
println!("🔥 === MATCHED CHAT COMPLETIONS ===");
println!(" Model: {}", req.model);
println!(" Messages: {} items", req.messages.len());
println!("🔥 === END MATCHED ===\n");
match proxy.proxy_request(req).await {
Ok(response) => Ok(warp::reply::json(&response)),
Err(e) => {
eprintln!("Proxy error: {:#}", e);
Ok(warp::reply::json(&json!({
"error": {
"message": format!("Proxy error: {}", e),
"type": "proxy_error",
"code": "internal_error"
}
})))
}
}
}
// Make ProxyServer cloneable for warp filters
impl Clone for ProxyServer {
fn clone(&self) -> Self {
Self {
client: self.client.clone(),
auth_data: self.auth_data.clone(),
}
}
}