Skip to content
Closed
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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,14 +112,14 @@ I tried crabcode specifically for these providers:
- [x] **minimax**
- [x] **fireworks**
- [x] **baseten**
- [x] **kimi-for-coding** (API key, Anthropic-protocol endpoint)

> Feel free to create an issue / add to this list if you tried

### Known unsupported providers

> I might work harder to support these in the future.

- Kimi For Coding Subscription - I keep getting 401 but it works in OpenCode, I may have to contact them first. **might support later**
- Gemini - It's OAuth + also very unsure. So currently no.
- Claude Code Subscription - Known to explicitly not like harnesses. So never will, sorry.

Expand Down
64 changes: 55 additions & 9 deletions src/aisdk/providers/anthropic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ pub struct Anthropic {
model_name: String,
provider_name: String,
reasoning_effort: Option<String>,
extra_headers: HashMap<String, String>,
}

impl Anthropic {
Expand All @@ -33,6 +34,7 @@ pub struct AnthropicBuilder {
model_name: Option<String>,
provider_name: Option<String>,
reasoning_effort: Option<String>,
extra_headers: HashMap<String, String>,
}

impl AnthropicBuilder {
Expand Down Expand Up @@ -61,6 +63,13 @@ impl AnthropicBuilder {
self
}

/// Extra headers declared by the provider catalog (e.g. a vendor-specific
/// `User-Agent`). Applied last so they can override defaults.
pub fn headers(mut self, headers: HashMap<String, String>) -> Self {
self.extra_headers = headers;
self
}

pub fn build(self) -> Result<Anthropic> {
Ok(Anthropic {
base_url: self
Expand All @@ -74,6 +83,7 @@ impl AnthropicBuilder {
.provider_name
.unwrap_or_else(|| "anthropic".to_string()),
reasoning_effort: self.reasoning_effort,
extra_headers: self.extra_headers,
})
}
}
Expand Down Expand Up @@ -183,15 +193,7 @@ impl Provider for Anthropic {
body["output_config"] = serde_json::json!({ "effort": effort });
}

let mut request_headers = reqwest::header::HeaderMap::new();
request_headers.insert(
reqwest::header::CONTENT_TYPE,
"application/json".parse().unwrap(),
);
if !self.api_key.is_empty() {
request_headers.insert("x-api-key", self.api_key.parse().unwrap());
}
request_headers.insert("anthropic-version", "2023-06-01".parse().unwrap());
let request_headers = build_anthropic_request_headers(&self.api_key, &self.extra_headers);

let client = reqwest::Client::builder()
.connect_timeout(std::time::Duration::from_secs(
Expand Down Expand Up @@ -252,6 +254,31 @@ impl Provider for Anthropic {
}
}

fn build_anthropic_request_headers(
api_key: &str,
extra_headers: &HashMap<String, String>,
) -> reqwest::header::HeaderMap {
let mut request_headers = reqwest::header::HeaderMap::new();
request_headers.insert(
reqwest::header::CONTENT_TYPE,
"application/json".parse().unwrap(),
);
if !api_key.is_empty() {
request_headers.insert("x-api-key", api_key.parse().unwrap());
}
request_headers.insert("anthropic-version", "2023-06-01".parse().unwrap());

for (name, value) in extra_headers {
if let (Ok(hn), Ok(hv)) = (
reqwest::header::HeaderName::from_bytes(name.as_bytes()),
reqwest::header::HeaderValue::from_str(value),
) {
request_headers.insert(hn, hv);
}
}
request_headers
}

fn anthropic_stream_chunk(
event_type: &str,
value: &serde_json::Value,
Expand Down Expand Up @@ -548,6 +575,25 @@ mod tests {
}
));
}

#[test]
fn kimi_for_coding_sends_cli_user_agent() {
let mut extra = HashMap::new();
extra.insert("User-Agent".to_string(), "KimiCLI/1.5".to_string());
let headers = build_anthropic_request_headers("", &extra);
assert_eq!(
headers
.get(reqwest::header::USER_AGENT)
.map(|v| v.to_str().unwrap()),
Some("KimiCLI/1.5")
);
}

#[test]
fn anthropic_default_sends_no_extra_headers() {
let headers = build_anthropic_request_headers("", &HashMap::new());
assert!(headers.get(reqwest::header::USER_AGENT).is_none());
}
}

fn anthropic_tool_output_content(tool: &crate::message::ToolOutputMessage) -> serde_json::Value {
Expand Down
2 changes: 2 additions & 0 deletions src/command/handlers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ pub fn handle_connect<'a>(
doc: String::new(),
env: Vec::new(),
npm: String::new(),
header: vec![],
models: HashMap::new(),
},
);
Expand All @@ -142,6 +143,7 @@ pub fn handle_connect<'a>(
doc: String::new(),
env: Vec::new(),
npm: String::new(),
header: vec![],
models: HashMap::new(),
},
);
Expand Down
9 changes: 9 additions & 0 deletions src/llm/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1249,6 +1249,15 @@ async fn stream_provider_request(
if let Some(key) = config.api_key.as_deref() {
builder = builder.api_key(key);
}
if let Some(provider_meta) =
crate::model::extensions::ModelExtensions::provider_for_request(
&config.provider_name,
)
{
if !provider_meta.header.is_empty() {
builder = builder.headers(provider_meta.header.into_iter().collect());
}
}
let provider = builder.build().map_err(|e| -> DynError { Box::new(e) })?;
stream_with_tools(
provider,
Expand Down
10 changes: 10 additions & 0 deletions src/model/discovery.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ pub struct Provider {
#[serde(default)]
pub npm: String,
#[serde(default)]
pub header: Vec<(String, String)>,
#[serde(default)]
pub models: HashMap<String, Model>,
}

Expand Down Expand Up @@ -502,6 +504,7 @@ impl Discovery {
doc: String::new(),
env: Vec::new(),
npm: String::new(),
header: vec![],
models: HashMap::new(),
},
);
Expand Down Expand Up @@ -548,6 +551,7 @@ impl Discovery {
doc: String::new(),
env: Vec::new(),
npm: String::new(),
header: vec![],
models: HashMap::new(),
});

Expand Down Expand Up @@ -966,6 +970,7 @@ mod tests {
doc: "https://catalog.example/docs".to_string(),
env: vec!["CATALOG_KEY".to_string()],
npm: "@ai-sdk/openai-compatible".to_string(),
header: vec![],
models: HashMap::from([(
"vision-model".to_string(),
Model {
Expand Down Expand Up @@ -1218,6 +1223,7 @@ mod tests {
doc: String::new(),
env: Vec::new(),
npm: String::new(),
header: vec![],
models: HashMap::new(),
},
);
Expand Down Expand Up @@ -1356,6 +1362,7 @@ mod tests {
doc: String::new(),
env: vec!["OPENCODE_API_KEY".to_string()],
npm: "@ai-sdk/openai-compatible".to_string(),
header: vec![],
models,
},
);
Expand Down Expand Up @@ -1397,6 +1404,7 @@ mod tests {
doc: String::new(),
env: vec!["XAI_API_KEY".to_string()],
npm: "@ai-sdk/xai".to_string(),
header: vec![],
models: HashMap::new(),
},
);
Expand Down Expand Up @@ -1435,6 +1443,7 @@ mod tests {
doc: String::new(),
env: Vec::new(),
npm: String::new(),
header: vec![],
models: HashMap::new(),
},
);
Expand Down Expand Up @@ -1468,6 +1477,7 @@ mod tests {
doc: String::new(),
env: Vec::new(),
npm: String::new(),
header: vec![],
models: HashMap::new(),
},
)]);
Expand Down
1 change: 1 addition & 0 deletions src/model/extensions/commandcode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ pub fn provider_from_models(models: Vec<CommandCodeModel>) -> crate::model::disc
doc: DOC_URL.to_string(),
env: vec![API_KEY_ENV.to_string()],
npm: NPM_PACKAGE.to_string(),
header: vec![],
models: models
.into_iter()
.filter(|model| !model.id.trim().is_empty())
Expand Down
Loading
Loading