Skip to content

Commit 1213cbf

Browse files
mikayla-makimikebenfield
authored andcommitted
Fix a bug where Anthropic completions would not work on nightly (#43287)
Follow up to: https://github.com/zed-industries/zed/pull/43185/files Release Notes: - N/A Co-authored-by: Michael <mbenfield@zed.dev>
1 parent f2ac1a1 commit 1213cbf

File tree

1 file changed

+14
-50
lines changed

1 file changed

+14
-50
lines changed

crates/anthropic/src/anthropic.rs

Lines changed: 14 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -393,7 +393,7 @@ impl Model {
393393
}
394394
}
395395

396-
pub fn beta_headers(&self) -> String {
396+
pub fn beta_headers(&self) -> Option<String> {
397397
let mut headers = vec![];
398398

399399
match self {
@@ -415,7 +415,11 @@ impl Model {
415415
_ => {}
416416
}
417417

418-
headers.join(",")
418+
if headers.is_empty() {
419+
None
420+
} else {
421+
Some(headers.join(","))
422+
}
419423
}
420424

421425
pub fn tool_model_id(&self) -> &str {
@@ -431,56 +435,12 @@ impl Model {
431435
}
432436
}
433437

434-
pub async fn complete(
435-
client: &dyn HttpClient,
436-
api_url: &str,
437-
api_key: &str,
438-
request: Request,
439-
beta_headers: String,
440-
) -> Result<Response, AnthropicError> {
441-
let uri = format!("{api_url}/v1/messages");
442-
let request_builder = HttpRequest::builder()
443-
.method(Method::POST)
444-
.uri(uri)
445-
.header("Anthropic-Version", "2023-06-01")
446-
.header("Anthropic-Beta", beta_headers)
447-
.header("X-Api-Key", api_key.trim())
448-
.header("Content-Type", "application/json");
449-
450-
let serialized_request =
451-
serde_json::to_string(&request).map_err(AnthropicError::SerializeRequest)?;
452-
let request = request_builder
453-
.body(AsyncBody::from(serialized_request))
454-
.map_err(AnthropicError::BuildRequestBody)?;
455-
456-
let mut response = client
457-
.send(request)
458-
.await
459-
.map_err(AnthropicError::HttpSend)?;
460-
let status_code = response.status();
461-
let mut body = String::new();
462-
response
463-
.body_mut()
464-
.read_to_string(&mut body)
465-
.await
466-
.map_err(AnthropicError::ReadResponse)?;
467-
468-
if status_code.is_success() {
469-
Ok(serde_json::from_str(&body).map_err(AnthropicError::DeserializeResponse)?)
470-
} else {
471-
Err(AnthropicError::HttpResponseError {
472-
status_code,
473-
message: body,
474-
})
475-
}
476-
}
477-
478438
pub async fn stream_completion(
479439
client: &dyn HttpClient,
480440
api_url: &str,
481441
api_key: &str,
482442
request: Request,
483-
beta_headers: String,
443+
beta_headers: Option<String>,
484444
) -> Result<BoxStream<'static, Result<Event, AnthropicError>>, AnthropicError> {
485445
stream_completion_with_rate_limit_info(client, api_url, api_key, request, beta_headers)
486446
.await
@@ -578,7 +538,7 @@ pub async fn stream_completion_with_rate_limit_info(
578538
api_url: &str,
579539
api_key: &str,
580540
request: Request,
581-
beta_headers: String,
541+
beta_headers: Option<String>,
582542
) -> Result<
583543
(
584544
BoxStream<'static, Result<Event, AnthropicError>>,
@@ -592,13 +552,17 @@ pub async fn stream_completion_with_rate_limit_info(
592552
};
593553
let uri = format!("{api_url}/v1/messages");
594554

595-
let request_builder = HttpRequest::builder()
555+
let mut request_builder = HttpRequest::builder()
596556
.method(Method::POST)
597557
.uri(uri)
598558
.header("Anthropic-Version", "2023-06-01")
599-
.header("Anthropic-Beta", beta_headers)
600559
.header("X-Api-Key", api_key.trim())
601560
.header("Content-Type", "application/json");
561+
562+
if let Some(beta_headers) = beta_headers {
563+
request_builder = request_builder.header("Anthropic-Beta", beta_headers);
564+
}
565+
602566
let serialized_request =
603567
serde_json::to_string(&request).map_err(AnthropicError::SerializeRequest)?;
604568
let request = request_builder

0 commit comments

Comments
 (0)