anthropic: Don't bail out on unknown model ID (#23782)

Marshall Bowers created

This PR fixes an issue introduced in
https://github.com/zed-industries/zed/pull/20551/ that would prevent
models with unknown IDs from working in the LLM service.

We only need to look up a model from its ID for the beta headers, and if
we can't find that particular model we should fall back to the default
beta headers instead of bailing out completely,

Release Notes:

- N/A

Change summary

crates/anthropic/src/anthropic.rs | 19 ++++++++++++++-----
1 file changed, 14 insertions(+), 5 deletions(-)

Detailed changes

crates/anthropic/src/anthropic.rs 🔗

@@ -148,8 +148,13 @@ impl Model {
         }
     }
 
+    pub const DEFAULT_BETA_HEADERS: &[&str] = &["prompt-caching-2024-07-31"];
+
     pub fn beta_headers(&self) -> String {
-        let mut headers = vec!["prompt-caching-2024-07-31".to_string()];
+        let mut headers = Self::DEFAULT_BETA_HEADERS
+            .into_iter()
+            .map(|header| header.to_string())
+            .collect::<Vec<_>>();
 
         if let Self::Custom {
             extra_beta_headers, ..
@@ -186,12 +191,14 @@ pub async fn complete(
     request: Request,
 ) -> Result<Response, AnthropicError> {
     let uri = format!("{api_url}/v1/messages");
-    let model = Model::from_id(&request.model)?;
+    let beta_headers = Model::from_id(&request.model)
+        .map(|model| model.beta_headers())
+        .unwrap_or_else(|_err| Model::DEFAULT_BETA_HEADERS.join(","));
     let request_builder = HttpRequest::builder()
         .method(Method::POST)
         .uri(uri)
         .header("Anthropic-Version", "2023-06-01")
-        .header("Anthropic-Beta", model.beta_headers())
+        .header("Anthropic-Beta", beta_headers)
         .header("X-Api-Key", api_key)
         .header("Content-Type", "application/json");
 
@@ -302,12 +309,14 @@ pub async fn stream_completion_with_rate_limit_info(
         stream: true,
     };
     let uri = format!("{api_url}/v1/messages");
-    let model = Model::from_id(&request.base.model)?;
+    let beta_headers = Model::from_id(&request.base.model)
+        .map(|model| model.beta_headers())
+        .unwrap_or_else(|_err| Model::DEFAULT_BETA_HEADERS.join(","));
     let request_builder = HttpRequest::builder()
         .method(Method::POST)
         .uri(uri)
         .header("Anthropic-Version", "2023-06-01")
-        .header("Anthropic-Beta", model.beta_headers())
+        .header("Anthropic-Beta", beta_headers)
         .header("X-Api-Key", api_key)
         .header("Content-Type", "application/json");
     let serialized_request =