collab: Always use newest anthropic model version (#15978)

Bennet Bo Fenner and Thorsten created

When Anthropic releases a new version of their models, Zed AI users
should always get access to the new version even when using an old
version of zed.

Co-Authored-By: Thorsten <thorsten@zed.dev>

Release Notes:

- N/A

Co-authored-by: Thorsten <thorsten@zed.dev>

Change summary

crates/anthropic/src/anthropic.rs |  8 ++++----
crates/collab/src/llm.rs          | 16 +++++++++++++++-
2 files changed, 19 insertions(+), 5 deletions(-)

Detailed changes

crates/anthropic/src/anthropic.rs 🔗

@@ -16,13 +16,13 @@ pub const ANTHROPIC_API_URL: &'static str = "https://api.anthropic.com";
 #[derive(Clone, Debug, Default, Serialize, Deserialize, PartialEq, EnumIter)]
 pub enum Model {
     #[default]
-    #[serde(alias = "claude-3-5-sonnet", rename = "claude-3-5-sonnet-20240620")]
+    #[serde(rename = "claude-3-5-sonnet", alias = "claude-3-5-sonnet-20240620")]
     Claude3_5Sonnet,
-    #[serde(alias = "claude-3-opus", rename = "claude-3-opus-20240229")]
+    #[serde(rename = "claude-3-opus", alias = "claude-3-opus-20240229")]
     Claude3Opus,
-    #[serde(alias = "claude-3-sonnet", rename = "claude-3-sonnet-20240229")]
+    #[serde(rename = "claude-3-sonnet", alias = "claude-3-sonnet-20240229")]
     Claude3Sonnet,
-    #[serde(alias = "claude-3-haiku", rename = "claude-3-haiku-20240307")]
+    #[serde(rename = "claude-3-haiku", alias = "claude-3-haiku-20240307")]
     Claude3Haiku,
     #[serde(rename = "custom")]
     Custom {

crates/collab/src/llm.rs 🔗

@@ -137,11 +137,25 @@ async fn perform_completion(
                 .anthropic_api_key
                 .as_ref()
                 .context("no Anthropic AI API key configured on the server")?;
+
+            let mut request: anthropic::Request =
+                serde_json::from_str(&params.provider_request.get())?;
+
+            // Parse the model, throw away the version that was included, and then set a specific
+            // version that we control on the server.
+            // Right now, we use the version that's defined in `model.id()`, but we will likely
+            // want to change this code once a new version of an Anthropic model is released,
+            // so that users can use the new version, without having to update Zed.
+            request.model = match anthropic::Model::from_id(&request.model) {
+                Ok(model) => model.id().to_string(),
+                Err(_) => request.model,
+            };
+
             let chunks = anthropic::stream_completion(
                 &state.http_client,
                 anthropic::ANTHROPIC_API_URL,
                 api_key,
-                serde_json::from_str(&params.provider_request.get())?,
+                request,
                 None,
             )
             .await?;