Skip to content
Open
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
/target
/logs
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ CCORP is designed to work seamlessly with Anthropic's Claude Code CLI:

```bash
export ANTHROPIC_BASE_URL=http://localhost:3000
export ANTHROPIC_AUTH_TOKEN="your_openrouter_api_key"
export ANTHROPIC_API_KEY="your_openrouter_api_key"
```

3. Run Claude Code as normal:
Expand Down
24 changes: 22 additions & 2 deletions src/anthropic_to_openai.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,30 @@ pub fn format_anthropic_to_openai(req: AnthropicRequest, settings: &Config) -> O
let mut openapi_messages = Vec::new();

if let Some(system) = req.system {
if let Some(system_str) = system.as_str() {
let system_text = if let Some(system_str) = system.as_str() {
// Handle string format: "system prompt text"
system_str.to_string()
} else if let Some(system_array) = system.as_array() {
// Handle array format: [{"type": "text", "text": "..."}]
system_array
.iter()
.filter_map(|block| {
if block["type"] == "text" {
block["text"].as_str().map(|s| s.to_string())
} else {
None
}
})
.collect::<Vec<String>>()
.join("\n\n")
} else {
String::new()
};

if !system_text.is_empty() {
openapi_messages.push(OpenAIMessage {
role: "system".to_string(),
content: Some(system_str.to_string()),
content: Some(system_text),
tool_calls: None,
tool_call_id: None,
});
Expand Down
9 changes: 9 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,15 @@ async fn messages_handler(
headers: HeaderMap,
Json(payload): Json<AnthropicRequest>,
) -> impl IntoResponse {
if let Some(path) = state.logging_path.as_ref() {
let timestamp = std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.unwrap()
.as_millis();
let anthropic_request_path = format!("{path}/{timestamp}-anthropic-request.json");
let anthropic_request_json = serde_json::to_string_pretty(&payload).unwrap();
std::fs::write(anthropic_request_path, anthropic_request_json).expect("Failed to write anthropic request log");
}
let settings_guard = state.config.read().await;
let openai_request = anthropic_to_openai::format_anthropic_to_openai(payload, &settings_guard);

Expand Down
20 changes: 20 additions & 0 deletions src/models.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,16 @@ use serde::{Deserialize, Serialize};

// Anthropic API Structs

#[derive(Debug, Serialize, Deserialize)]
pub struct AnthropicUsage {
pub input_tokens: u32,
pub output_tokens: u32,
#[serde(skip_serializing_if = "Option::is_none")]
pub cache_creation_input_tokens: Option<u32>,
#[serde(skip_serializing_if = "Option::is_none")]
pub cache_read_input_tokens: Option<u32>,
}

#[derive(Debug, Serialize, Deserialize)]
pub struct AnthropicMessage {
pub role: String,
Expand Down Expand Up @@ -32,10 +42,19 @@ pub struct AnthropicResponse {
pub stop_reason: String,
pub stop_sequence: Option<String>,
pub model: String,
pub usage: AnthropicUsage,
}

// OpenAI API Structs

#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct OpenAIUsage {
pub prompt_tokens: u32,
pub completion_tokens: u32,
#[serde(skip_serializing_if = "Option::is_none")]
pub total_tokens: Option<u32>,
}

#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct OpenAIMessage {
pub role: String,
Expand Down Expand Up @@ -78,6 +97,7 @@ pub struct OpenAIResponse {
pub id: String,
pub choices: Vec<OpenAIChoice>,
pub model: String,
pub usage: OpenAIUsage,
}

#[derive(Debug, Serialize, Deserialize, Clone)]
Expand Down
8 changes: 7 additions & 1 deletion src/openai_to_anthropic.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::models::*;
use crate::models::{AnthropicResponse, AnthropicUsage, OpenAIResponse};
use serde_json::json;

pub fn format_openai_to_anthropic(resp: OpenAIResponse) -> AnthropicResponse {
Expand Down Expand Up @@ -32,5 +32,11 @@ pub fn format_openai_to_anthropic(resp: OpenAIResponse) -> AnthropicResponse {
},
stop_sequence: None,
model: resp.model,
usage: AnthropicUsage {
input_tokens: resp.usage.prompt_tokens,
output_tokens: resp.usage.completion_tokens,
cache_creation_input_tokens: None,
cache_read_input_tokens: None,
},
}
}