Skip to content
Merged
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 crates/dim-acp/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ description = "Client-host ACP gateway over Dim's public RPC v3"

[dependencies]
tempfile = "3"
agent-client-protocol = { version = "=3.5.0", features = ["unstable_session_fork"] }
agent-client-protocol = { version = "=3.5.0", features = ["unstable_session_fork", "unstable_llm_providers"] }
dim-client.workspace = true
dim-agent-types.workspace = true
rutis.workspace = true
Expand Down
114 changes: 90 additions & 24 deletions crates/dim-acp/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -221,10 +221,14 @@ impl Config {
));
}
if let Some(current) = &self.model
&& self
.models
.iter()
.any(|model| model.id == current.model_id && model.provider == current.provider_id)
&& self.models.iter().any(|model| {
model.id == current.model_id
&& model.provider
== current
.catalog_provider_id
.as_deref()
.unwrap_or(&current.provider_id)
})
{
options.push(select(
"model",
Expand Down Expand Up @@ -284,6 +288,87 @@ impl Config {
Some((choices, adopted))
}

pub(crate) async fn recover_model(rpc: &Rpc, id: &str, value: &str) -> Result<Self, Error> {
let mut config = Self::read(rpc, id, None).await?;
config.set_model(rpc, id, value, true).await?;
#[derive(Deserialize)]
struct Current {
configuration: Option<AdoptedConfiguration>,
}
#[derive(Deserialize)]
#[serde(rename_all = "camelCase")]
struct AdoptedConfiguration {
model: ModelId,
capability_snapshot: Option<Box<dim_agent_types::ModelCapabilitySnapshot>>,
model_preferences: Option<dim_agent_types::ModelPreferences>,
}
let current: Current = rpc
.typed(
"session.model.get",
json!({"sessionId":id,"branchId":DEFAULT_BRANCH_ID}),
)
.await?;
let current = current.configuration.ok_or_else(Error::internal_error)?;
config.model = Some(Configuration {
provider_id: current.model.provider_id,
model_id: current.model.model_id,
catalog_provider_id: None,
capability_snapshot: current.capability_snapshot,
model_preferences: current.model_preferences,
});
Ok(config)
}

async fn set_model(
&mut self,
rpc: &Rpc,
id: &str,
value: &str,
require_expired_route: bool,
) -> Result<(), Error> {
#[derive(Deserialize)]
struct Adopted {
model: ModelId,
outcome: String,
}
#[derive(serde::Serialize)]
#[serde(rename_all = "camelCase")]
struct Select<'a> {
session_id: &'a str,
branch_id: &'static str,
provider_id: &'a str,
model_id: &'a str,
#[serde(skip_serializing_if = "Option::is_none")]
require_expired_route: Option<bool>,
}
let model = self
.models
.iter()
.find(|model| model.id == value)
.ok_or_else(Error::invalid_params)?;
let params = serde_json::to_value(Select {
session_id: id,
branch_id: DEFAULT_BRANCH_ID,
provider_id: &model.provider,
model_id: &model.id,
require_expired_route: require_expired_route.then_some(true),
})
.map_err(|_| Error::internal_error())?;
let result: Adopted = rpc.typed("session.model.update", params).await?;
if !matches!(result.outcome.as_str(), "adopted" | "unchanged") {
return Err(Error::internal_error());
}
self.model = Some(Configuration {
catalog_provider_id: (model.provider != result.model.provider_id)
.then(|| model.provider.clone()),
provider_id: result.model.provider_id,
model_id: result.model.model_id,
capability_snapshot: None,
model_preferences: None,
});
Ok(())
}

pub(crate) async fn set(
mut self,
rpc: &Rpc,
Expand Down Expand Up @@ -337,26 +422,7 @@ impl Config {
}
}
"model" => {
#[derive(Deserialize)]
struct Adopted {
model: ModelId,
outcome: String,
}
let model = self
.models
.iter()
.find(|model| model.id == value)
.ok_or_else(Error::invalid_params)?;
let result: Adopted = rpc.typed("session.model.update",json!({"sessionId":id,"branchId":DEFAULT_BRANCH_ID,"providerId":model.provider,"modelId":model.id})).await?;
if !matches!(result.outcome.as_str(), "adopted" | "unchanged") {
return Err(Error::internal_error());
}
self.model = Some(Configuration {
provider_id: result.model.provider_id,
model_id: result.model.model_id,
capability_snapshot: None,
model_preferences: None,
});
self.set_model(rpc, id, value, false).await?;
self.models = models(rpc).await?;
}
_ => return Err(Error::invalid_params()),
Expand Down
Loading